branch: master commit 395db6d7723c67e86eb171feececb546b9a66a4b Author: rocky <ro...@gnu.org> Commit: rocky <ro...@gnu.org>
Simplify eshell/comint/shell mode testing. Thanks to pythonnut: http://emacs.stackexchange.com/users/2642/pythonnut http://emacs.stackexchange.com/questions/10387/how-can-i-tell-if-a-buffer-is-a-comint-buffer-eshell-buffer-or-neither/10390?iemail=1&noredirect=1#10390 --- realgud/common/send.el | 29 +++++++++++++---------------- realgud/common/track-mode.el | 32 +++++++++++++++++++------------- realgud/common/utils.el | 15 +++++++++++++++ test/test-utils.el | 24 ++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 29 deletions(-) diff --git a/realgud/common/send.el b/realgud/common/send.el index 85f26b5..7ec0051 100644 --- a/realgud/common/send.el +++ b/realgud/common/send.el @@ -18,41 +18,38 @@ (require 'comint) (require 'eshell) (require 'load-relative) -(require-relative-list '("window") "realgud-") +(require-relative-list '("utils" "window") "realgud-") (require-relative-list '("buffer/helper") "realgud-buffer-") (declare-function realgud-get-cmdbuf 'realgud-buffer-helper) (declare-function comint-goto-process-mark 'comint) (declare-function comint-send-input 'comint) +(declare-function realgud:canonic-major-mode 'realgud-utils) (defun realgud:send-input () "Sends command buffer line either to comint or eshell" (interactive) - ;; FIXME DRY with code in realgud:send-command-common() and track-mode.el - (cond ((and (boundp 'eshell-mode) eshell-mode) - (eshell-send-input)) - ((and (boundp 'comint-prompt-regexp) - (comint-check-proc (current-buffer))) - (comint-send-input)) - ('t (error "We can only handle comint or eshell buffers"))) - ) + (let ((mode (realgud:canonic-major-mode))) + (cond ((eq mode 'eshell) + (eshell-send-input)) + ((eq mode 'comint) + (comint-send-input)) + ))) (defun realgud:send-command-common (process command-str) "Assume we are in a comint buffer. Insert COMMAND-STR and send that input onto the process." (if (eq 'run (process-status process)) - (progn - ;; FIXME DRY with code in realgud:send-input() and track-mode.el - (cond ((and (boundp 'eshell-mode) eshell-mode) + (let ((mode (realgud:canonic-major-mode))) + (cond ((eq mode 'eshell) (goto-char eshell-last-output-end) (setq eshell-last-output-start (setq realgud-last-output-start (point-marker)))) - ((and (boundp 'comint-prompt-regexp) - (comint-check-proc (current-buffer))) + ((eq mode 'comint) + (comint-check-proc (current-buffer)) (comint-goto-process-mark) (setq comint-last-output-start - (setq realgud-last-output-start (point-marker)))) - ('t (error "We can only handle cmint or eshell buffers"))) + (setq realgud-last-output-start (point-marker))))) (insert command-str) (realgud:send-input) ) diff --git a/realgud/common/track-mode.el b/realgud/common/track-mode.el index 4199181..31539b3 100644 --- a/realgud/common/track-mode.el +++ b/realgud/common/track-mode.el @@ -23,7 +23,7 @@ (require-relative-list '("core" "helper" "track" "loc" "lochist" "file" "fringe" "window" "regexp" "menu" "backtrace-mode" - "send" "shortkey") "realgud-") + "send" "shortkey" "utils") "realgud-") (require-relative-list '("buffer/command") "realgud-buffer-") @@ -37,6 +37,8 @@ 'realgud-buffer-command) (declare-function realgud-cmdbuf-info-set? 'realgud-buffer-command) +(declare-function realgud:canonic-major-mode + 'realgud-utils) (defvar realgud-track-mode-map (let ((map (copy-keymap shell-mode-map))) @@ -138,14 +140,14 @@ of this mode." (set (make-local-variable 'tool-bar-map) realgud:tool-bar-map) ;; FIXME DRY with code in send.el - (cond ((and (boundp 'eshell-mode) eshell-mode) - (add-hook 'eshell-output-filter-functions - 'realgud-track-eshell-output-filter-hook)) - ((and (boundp 'comint-prompt-regexp) - (comint-check-proc (current-buffer))) - (add-hook 'comint-output-filter-functions - 'realgud-track-comint-output-filter-hook)) - ('t (error "We can only handle comint or eshell buffers"))) + (let ((mode (realgud:canonic-major-mode))) + (cond ((eq mode 'eshell) + (add-hook 'eshell-output-filter-functions + 'realgud-track-eshell-output-filter-hook)) + ((eq mode 'comint) + (add-hook 'comint-output-filter-functions + 'realgud-track-comint-output-filter-hook)) + )) (run-mode-hooks 'realgud-track-mode-hook)) ;; else (progn @@ -155,10 +157,14 @@ of this mode." ) (kill-local-variable 'realgud:tool-bar-map) (realgud-fringe-erase-history-arrows) - (remove-hook 'comint-output-filter-functions - 'realgud-track-comint-output-filter-hook) - (remove-hook 'eshell-output-filter-functions - 'realgud-track-eshell-output-filter-hook) + (let ((mode (realgud:canonic-major-mode))) + (cond ((eq mode 'eshell) + (remove-hook 'eshell-output-filter-functions + 'realgud-track-eshell-output-filter-hook)) + ((eq mode 'comint) + (remove-hook 'comint-output-filter-functions + 'realgud-track-comint-output-filter-hook)) + )) (let* ((cmd-process (get-buffer-process (current-buffer))) (status (if cmd-process (list (propertize (format ":%s" diff --git a/realgud/common/utils.el b/realgud/common/utils.el index aede640..66f6509 100644 --- a/realgud/common/utils.el +++ b/realgud/common/utils.el @@ -1,4 +1,6 @@ (require 'load-relative) +(require 'comint) +(require 'eshell) (defun realgud:strip (str) "Remove leading and tailing whitespace from STR." @@ -15,4 +17,17 @@ (t (append (realgud:flatten (car mylist)) (realgud:flatten (cdr mylist)))))) +(defun realgud:canonic-major-mode() + "Return + - 'eshell if we are in eshell-mode, + - 'comint if the major comint-mode or shell-mode +Or raise an error if neither." + + (cond ((eq major-mode 'eshell-mode) + 'eshell) + ((or (eq major-mode 'comint-mode) (eq major-mode 'shell-mode)) + 'comint) + ('t (error "We can only handle comint, shell or eshell buffers")) + )) + (provide-me "realgud-") diff --git a/test/test-utils.el b/test/test-utils.el index 318a2e7..7605557 100644 --- a/test/test-utils.el +++ b/test/test-utils.el @@ -1,9 +1,14 @@ (require 'test-simple) +(require 'comint) +(require 'eshell) +(require 'shell) + (load-file "../realgud/common/utils.el") (declare-function realgud:flatten 'realgud-utils) (declare-function realgud:strip 'realgud-regexp) (declare-function __FILE__ 'load-relative) +(declare-function realgud:canonic-major-mode 'realgud-utils) (test-simple-start) @@ -19,4 +24,23 @@ (assert-equal '(abc) (realgud:flatten '(abc))) (assert-equal '(abc def h i j) (realgud:flatten '(abc (def (h) i) j))) +(note "realgud:canonic-major-mode") + +(assert-raises error (realgud:canonic-major-mode) + "Not in eshell, comint or shell-mode") + +(with-temp-buffer + (comint-mode) + (assert-equal 'comint (realgud:canonic-major-mode))) + +(with-temp-buffer + (eshell-mode) + (assert-equal 'eshell (realgud:canonic-major-mode))) + +;; (with-temp-buffer +;; (start-process "bogus" (current-buffer) "sleep" "1") +;; (shell-mode) +;; (assert-equal 'comint (realgud:canonic-major-mode)) +;; ) + (end-tests)