|
| 1 | +;;; ess-sesman.el --- description -*- lexical-binding: t; -*- |
| 2 | +;; |
| 3 | +;; Copyright (C) 2021 Trey Peacock |
| 4 | +;; |
| 5 | +;; Author: Trey Peacock <http://github/tpeacock19> |
| 6 | +;; Maintainer: Trey Peacock <[email protected]> |
| 7 | +;; Created: December 13, 2021 |
| 8 | +;; Modified: December 13, 2021 |
| 9 | +;; Version: 0.0.1 |
| 10 | +;; Keywords: |
| 11 | +;; Homepage: https://github.com/tpeacock19/ess-sesman |
| 12 | +;; Package-Requires: ((emacs 29.0.50) (cl-lib "0.5")) |
| 13 | +;; |
| 14 | +;; This file is not part of GNU Emacs. |
| 15 | +;; |
| 16 | +;; This file is free software; you can redistribute it and/or modify it |
| 17 | +;; under the terms of the GNU General Public License as published by the |
| 18 | +;; Free Software Foundation; either version 3, or (at your option) any |
| 19 | +;; later version. |
| 20 | + |
| 21 | +;; This program is distributed in the hope that it will be useful, but |
| 22 | +;; WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 24 | +;; General Public License for more details. |
| 25 | + |
| 26 | +;; For a full copy of the GNU General Public License |
| 27 | +;; see <http://www.gnu.org/licenses/>. |
| 28 | +;; |
| 29 | +;;; Commentary: |
| 30 | +;; |
| 31 | +;; description |
| 32 | +;; |
| 33 | +;;; Code: |
| 34 | + |
| 35 | +(require 'sesman) |
| 36 | +(require 'sesman-browser) |
| 37 | + |
| 38 | +(defun ess-sesman-sessions () |
| 39 | + "Return a list of all active ESS sessions." |
| 40 | + (sesman-sessions 'ESS)) |
| 41 | + |
| 42 | +(cl-defmethod sesman-more-relevant-p ((_system (eql ESS)) session1 session2) |
| 43 | + "Figure out if SESSION1 or SESSION2 is more relevant." |
| 44 | + (sesman-more-recent-p (cdr session1) (cdr session2))) |
| 45 | + |
| 46 | +(cl-defmethod sesman-project ((_system (eql ESS))) |
| 47 | + (ignore-errors (expand-file-name (directory-file-name (ess--project-root (project-current)))))) |
| 48 | + |
| 49 | +(cl-defmethod sesman-start-session ((_system (eql ESS))) |
| 50 | + "Start a connection of any type interactively. |
| 51 | +Session will be named after the LANG inferior repl." |
| 52 | + (pcase major-mode |
| 53 | + ('ess-r-mode |
| 54 | + (call-interactively #'run-ess-r)) |
| 55 | + ('ess-julia-mode |
| 56 | + (call-interactively #'run-ess-julia))) |
| 57 | + (cons (buffer-name) (current-buffer))) |
| 58 | + |
| 59 | +(cl-defmethod sesman-quit-session ((_system (eql ESS)) session) |
| 60 | + "Quit an ESS SESSION." |
| 61 | + (let ((repls (cdr session))) |
| 62 | + (cl-flet* ((quit (repl) |
| 63 | + (when (buffer-live-p repl) |
| 64 | + (with-current-buffer repl |
| 65 | + (let* ((inf-buf (inferior-ess-force)) |
| 66 | + (inf-proc (get-buffer-process inf-buf))) |
| 67 | + (ess-quit 'no-save) |
| 68 | + (inferior-ess--wait-for-exit inf-proc) |
| 69 | + (kill-buffer inf-buf)))))) |
| 70 | + (save-selected-window |
| 71 | + (mapc #'quit repls))))) |
| 72 | + |
| 73 | +(cl-defmethod ess-quit--override (arg &context (ess-dialect "R")) |
| 74 | + "With ARG, do not offer to save the workspace. |
| 75 | +Additionally, remove sesman object." |
| 76 | + (let ((cmd (format "base::q('%s')\n" (if arg "no" "default"))) |
| 77 | + (sprocess (ess-get-process ess-current-process-name))) |
| 78 | + (when (not sprocess) (error "No ESS process running")) |
| 79 | + (sesman-remove-object 'ESS nil (current-buffer) arg t) |
| 80 | + (ess-cleanup) |
| 81 | + (ess-send-string sprocess cmd))) |
| 82 | + |
| 83 | +(cl-defgeneric ess-quit--override (arg &context (ess-dialect "julia")) |
| 84 | + "Stop the inferior process. |
| 85 | +Additionally, remove sesman object." |
| 86 | + (let ((proc (ess-get-process))) |
| 87 | + (sesman-remove-object 'ESS nil (current-buffer) arg t) |
| 88 | + (ess-cleanup) |
| 89 | + (when ess-eval-visibly |
| 90 | + (goto-char (marker-position (process-mark proc))) |
| 91 | + (insert inferior-ess-exit-command)) |
| 92 | + (process-send-string proc inferior-ess-exit-command))) |
| 93 | + |
| 94 | +(cl-defmethod sesman-restart-session ((_system (eql ESS)) session) |
| 95 | + "Restart an ESS SESSION." |
| 96 | + (let ((ses-name (car session)) |
| 97 | + (repls (cdr session))) |
| 98 | + (cl-flet* ((restart (repl) |
| 99 | + (when (buffer-live-p repl) |
| 100 | + (with-current-buffer repl |
| 101 | + (inferior-ess-reload))))) |
| 102 | + (mapc #'restart repls)))) |
| 103 | + |
| 104 | +(defun ess--sesman-init-repl (&rest _) |
| 105 | + "Set local variables necessary for a new inf repl." |
| 106 | + (setq-local sesman-system 'ESS) |
| 107 | + (sesman-add-object 'ESS (buffer-name) (current-buffer) 'allow-new)) |
| 108 | + |
| 109 | +(defun ess--sesman-ensure-process-name () |
| 110 | + "Ensure ESS process name and sesman system are registered." |
| 111 | + (setq-local sesman-system 'ESS) |
| 112 | + (when (and (not ess-local-process-name) |
| 113 | + (sesman-current-session 'ESS)) |
| 114 | + (setq ess-local-process-name |
| 115 | + (process-name (get-buffer-process (cadr (sesman-current-session 'ESS))))))) |
| 116 | + |
| 117 | +(defun ess--sesman-switch-process-link (orig-fun &rest args) |
| 118 | + "Ensure sesman session switches to new process." |
| 119 | + (let ((buf (buffer-name)) |
| 120 | + (new-buf (apply orig-fun args))) |
| 121 | + (sesman-link-with-buffer buf (sesman-session-for-object 'ESS new-buf)))) |
| 122 | + |
| 123 | + |
| 124 | +;; ensure sesman set for script buffers |
| 125 | +(add-hook 'ess-r-mode-hook #'ess--sesman-ensure-process-name) |
| 126 | +(add-hook 'ess-julia-mode-hook #'ess--sesman-ensure-process-name) |
| 127 | + |
| 128 | +;; ensure sesman set for repl buffers |
| 129 | +(advice-add #'inferior-ess--set-major-mode :after #'ess--sesman-init-repl) |
| 130 | +(advice-add 'ess-switch-process :around #'ess--sesman-switch-process-link) |
| 131 | + |
| 132 | +(provide 'ess-sesman) |
| 133 | +;;; ess-sesman.el ends here |
0 commit comments