From 1841a5d32370e8fe0b493dcbe588b89b8051f12e Mon Sep 17 00:00:00 2001 From: Arthur Miller Date: Mon, 27 Sep 2021 21:55:42 +0200 Subject: [PATCH] Run help-mode commands form any buffer * help-mode.el (help-go-back): Addapted to call form any buufer. (help-window): New function. (with-help-buffer): New macro. (help-go-forward): Addapted to call form any buufer. (help-view-source): Addapted to call form any buufer. (help-goto-info): Addapted to call form any buufer. (help-command-prefix): New map. (help-command-prefix-key): New customize option. Install help-command-prefix map in global-map on C-h M-h key. --- lisp/help-mode.el | 100 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 76 insertions(+), 24 deletions(-) diff --git a/lisp/help-mode.el b/lisp/help-mode.el index 0b404fe89f..932af304a7 100644 --- a/lisp/help-mode.el +++ b/lisp/help-mode.el @@ -48,6 +48,23 @@ help-mode-map map) "Keymap for Help mode.") +(defvar help-command-prefix + (let ((map (define-prefix-command 'help-command-prefix))) + (define-key map "l" #'help-go-back) + (define-key map "r" #'help-go-forward) + (define-key map "\C-c\C-b" #'help-go-back) + (define-key map "\C-c\C-f" #'help-go-forward) + (define-key map [XF86Back] #'help-go-back) + (define-key map [XF86Forward] #'help-go-forward) + (define-key map "\C-c\C-c" #'help-follow-symbol) + (define-key map "s" #'help-view-source) + (define-key map "i" #'help-goto-info) + (define-key map "c" #'help-customize) + (fset 'help-command-prefix help-command-prefix) + (setq help-command-prefix help-command-prefix) + map) + "Keymap for Help command prefix.") + (easy-menu-define help-mode-menu help-mode-map "Menu for Help mode." '("Help-Mode" @@ -149,6 +166,20 @@ help-mode-hook "Hook run by `help-mode'." :type 'hook :group 'help) + +(defcustom help-command-prefix-key "C-h M-h" + "The key HELP-COMMAND-PREFIX is bound to in the global map." + :type '(choice (string :tag "Key") (const :tag "no binding")) + :set (lambda (var key) + (when (and (boundp var) (symbol-value var)) + (define-key (current-global-map) + (read-kbd-macro (symbol-value var)) nil)) + (when key + (define-key (current-global-map) + (read-kbd-macro key) 'help-command-prefix)) + (set var key)) + :group 'help + :version "28.1") ;; Button types used by help @@ -402,7 +433,8 @@ help-mode-finish "Finalize Help mode setup in current buffer." (when (derived-mode-p 'help-mode) (setq buffer-read-only t) - (help-make-xrefs (current-buffer)))) + (help-make-xrefs (current-buffer)) + (set-window-dedicated-p (get-buffer-window (help-buffer)) t))) ;; Grokking cross-reference information in doc strings and ;; hyperlinking it. @@ -741,6 +773,21 @@ 'help-xref-interned ;; Navigation/hyperlinking with xrefs +(defun help-window () + "Return help-buffer window or nil help-buffer is not currently displayed." + (when (get-buffer "*Help*") + (get-buffer-window (get-buffer "*Help*")))) + +(defmacro with-help-buffer (&rest body) + "Execute the forms in BODY with HELP-BUFFER temporarily current. + +BODY will be executed onlu if HELP-BUFFER is displayed in a live +window on the screen." + (declare (indent 1) (debug t)) + `(when (help-window) + (with-current-buffer (help-buffer) + ,@body))) + (defun help-xref-go-back (buffer) "From BUFFER, go back to previous help buffer text using `help-xref-stack'." (let (item position method args) @@ -784,44 +831,49 @@ help-xref-go-forward (defun help-go-back () "Go back to previous topic in this help buffer." (interactive) - (if help-xref-stack - (help-xref-go-back (current-buffer)) - (user-error "No previous help buffer"))) + (with-help-buffer + (if help-xref-stack + (help-xref-go-back (current-buffer)) + (user-error "No previous help buffer")))) (defun help-go-forward () "Go to the next topic in this help buffer." (interactive) - (if help-xref-forward-stack - (help-xref-go-forward (current-buffer)) - (user-error "No next help buffer"))) + (with-help-buffer + (if help-xref-forward-stack + (help-xref-go-forward (current-buffer)) + (user-error "No next help buffer")))) (defun help-view-source () "View the source of the current help item." (interactive nil help-mode) - (unless (plist-get help-mode--current-data :file) - (error "Source file for the current help item is not defined")) - (help-function-def--button-function - (plist-get help-mode--current-data :symbol) - (plist-get help-mode--current-data :file) - (plist-get help-mode--current-data :type))) + (with-help-buffer + (unless (plist-get help-mode--current-data :file) + (error "Source file for the current help item is not defined")) + (help-function-def--button-function + (plist-get help-mode--current-data :symbol) + (plist-get help-mode--current-data :file) + (plist-get help-mode--current-data :type)))) (defun help-goto-info () "View the *info* node of the current help item." (interactive nil help-mode) - (unless help-mode--current-data - (error "No symbol to look up in the current buffer")) - (info-lookup-symbol (plist-get help-mode--current-data :symbol) - 'emacs-lisp-mode)) + (with-help-buffer + (unless help-mode--current-data + (error "No symbol to look up in the current buffer")) + (info-lookup-symbol (plist-get help-mode--current-data :symbol) + 'emacs-lisp-mode))) (defun help-customize () "Customize variable or face whose doc string is shown in the current buffer." (interactive nil help-mode) - (let ((sym (plist-get help-mode--current-data :symbol))) - (unless (or (boundp sym) (facep sym)) - (user-error "No variable or face to customize")) - (cond - ((boundp sym) (customize-variable sym)) - ((facep sym) (customize-face sym))))) + (with-help-buffer + (let ((sym (plist-get help-mode--current-data :symbol))) + (unless (or (boundp sym) (facep sym)) + (user-error "No variable or face to customize")) + (cond + ((boundp sym) (customize-variable sym)) + ((facep sym) (customize-face sym)))))) (defun help-do-xref (_pos function args) "Call the help cross-reference function FUNCTION with args ARGS. @@ -844,7 +896,7 @@ help-follow-mouse (defun help-follow () "Follow cross-reference at point. -For the cross-reference format, see `help-make-xrefs'." +For the cross-reference format, see `help-make-xrsefs'." (declare (obsolete nil "28.1")) (interactive) (user-error "No cross-reference here")) -- 2.33.0