[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Adding a "quick-help" menu
From: |
Philip Kaludercic |
Subject: |
Re: Adding a "quick-help" menu |
Date: |
Sat, 17 Sep 2022 14:53:56 +0000 |
Philip Kaludercic <philipk@posteo.net> writes:
> Philip Kaludercic <philipk@posteo.net> writes:
>
>> There is a fork of mg (MicroEmacs)[0] that binds C-h q to a command that
>> pops up a buffer with these contents:
>>
>> FILE BUFFER WINDOW MARK/KILL MISC
>> C-x C-c exit C-x C-k close C-0 only other C-space mark C-_ undo
>> C-x C-f find C-x k other C-1 only this C-w wipe C-s search
>> C-x C-s save C-x C-b list C-2 split two C-k close C-r
>> r-search
>> C-x s all C-x b switch C-x ^ enlarge C-y yank M-% replace
>> C-x i insert C-x g goto ln C-x o other win C-x C-x swap M-q
>> reformat
>> ______________________________________________________________________________
>> C-h q toggle quick help | C-h t show tutorial | C-h b show key
>> bindings
>>
>> I notice that in GNU Emacs C-h q is bound to `help-quit', which does
>> nothing if you are not in the help command loop. Would having a
>> "quick-help" menu along these lines for common operations make sense for
>> GNU Emacs as well?
>>
>> [0] https://github.com/troglobit/mg
>
> Here is a quick sketch of how this could look like:
I've further refined the sketch and how have a patch:
>From d0899b748b0047e1d1c773b06076faeba2535fee Mon Sep 17 00:00:00 2001
From: Philip Kaludercic <philipk@posteo.net>
Date: Sat, 17 Sep 2022 16:52:01 +0200
Subject: [PATCH] Add a quick-help menu
* lisp/help.el (help-map): Bind 'help-quit-or-quick' instead of 'help-quit'
(help-quick-sections): Add variable.
(help-quick): Add main command.
(help-quit-or-quick): Add auxiliary command.
---
lisp/help.el | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 108 insertions(+), 1 deletion(-)
diff --git a/lisp/help.el b/lisp/help.el
index 92b87cf799..089db8c2dd 100644
--- a/lisp/help.el
+++ b/lisp/help.el
@@ -112,7 +112,7 @@ help-map
(define-key map "v" 'describe-variable)
(define-key map "w" 'where-is)
(define-key map "x" 'describe-command)
- (define-key map "q" 'help-quit)
+ (define-key map "q" 'help-quit-or-quick)
map)
"Keymap for characters following the Help key.")
@@ -125,11 +125,118 @@ global-map
(defvar help-button-cache nil)
+
+(defvar help-quick-sections
+ '(("FILE"
+ (save-buffers-kill-terminal . "exit")
+ (find-file . "find")
+ (write-file . "write")
+ (save-buffer . "save")
+ (save-some-buffers . "all"))
+ ("BUFFER"
+ (kill-buffer . "kill")
+ (list-buffers . "list")
+ (switch-to-buffer . "switch")
+ (goto-line . "goto line")
+ (read-only-mode . "read only"))
+ ("WINDOW"
+ (delete-window . "only other")
+ (delete-other-windows . "only this")
+ (split-window-below . "split vert.")
+ (split-window-right . "split horiz.")
+ (other-window . "other"))
+ ("MARK/KILL"
+ (set-mark-command . "mark")
+ (kill-region . "wipe")
+ (kill-line . "kill")
+ (yank . "yank")
+ (exchange-point-and-mark . "swap"))
+ ("PROJECT"
+ (project-switch-project . "switch")
+ (project-find-file . "find file")
+ (project-find-regexp . "search")
+ (project-query-replace-regexp . "replace")
+ (project-compile . "compile"))
+ ("MISC"
+ (undo . "undo")
+ (isearch-forward . "search")
+ (isearch-backward . "rev-search")
+ (query-replace . "replace")
+ (fill-paragraph . "reformat"))))
+
+;; Inspired by a mg fork (https://github.com/troglobit/mg)
+(defun help-quick ()
+ "Display a quick-help buffer."
+ (interactive)
+ (with-current-buffer (get-buffer-create "*Quick Help*")
+ (let ((inhibit-read-only t) (padding 2) blocks)
+
+ ;; Go through every section and prepare a text-rectangle to be
+ ;; inserted later.
+ (dolist (section help-quick-sections)
+ (let ((max-key-len 0) (max-cmd-len 0) keys)
+ (dolist (ent (reverse (cdr section)))
+ (let* ((bind (where-is-internal (car ent) nil t))
+ (key (if bind
+ (propertize
+ (key-description bind)
+ 'face 'help-key-binding)
+ (propertize "N.B." 'face 'error))))
+ (setq max-cmd-len (max (length (cdr ent)) max-cmd-len)
+ max-key-len (max (length key) max-key-len))
+ (push (cons key (cdr ent)) keys)))
+ (let ((fmt (format "%%-%ds %%-%ds%s" max-key-len max-cmd-len
+ (make-string padding ?\s)))
+ (width (+ max-key-len 1 max-cmd-len padding)))
+ (push `(,width
+ ,(propertize
+ (concat
+ (car section)
+ (make-string (- width (length (car section))) ?\s))
+ 'face 'bold)
+ ,@(mapcar (lambda (ent)
+ (format fmt (car ent) (cdr ent)))
+ keys))
+ blocks))))
+
+ ;; Insert each rectangle in order until they don't fit into the
+ ;; frame any more, in which case the next sections are inserted
+ ;; in a new "line".
+ (erase-buffer)
+ (dolist (block (nreverse blocks))
+ (when (> (+ (car block) (current-column)) (frame-width))
+ (goto-char (point-max))
+ (newline 2))
+ (save-excursion
+ (insert-rectangle (cdr block)))
+ (end-of-line))
+ (delete-trailing-whitespace))
+
+ ;; Display the buffer at the bottom of the page and shrink it
+ ;; immediately.
+ (help-mode)
+ (with-selected-window (display-buffer-at-bottom (current-buffer) '())
+ (fit-window-to-buffer))
+ (message
+ (substitute-command-keys "Toggle the quick help buffer using
\\[help-quit-or-quick]."))))
+
(defun help-quit ()
"Just exit from the Help command's command loop."
(interactive)
nil)
+(defun help-quit-or-quick ()
+ "Call `help-quit' or `help-quick' depending on the context."
+ (interactive)
+ (cond
+ (help-buffer-under-preparation
+ ;; FIXME: There should be a better way to detect if we are in the
+ ;; help command loop.
+ (help-quit))
+ ((and-let* ((window (get-buffer-window "*Quick Help*")))
+ (quit-window t window)))
+ ((help-quick))))
+
(defvar help-return-method nil
"What to do to \"exit\" the help buffer.
This is a list
--
2.37.3
Next to the above sections this patch also includes a few commands for
project management. Perhaps there are a few more ideas on what could be
added?
- Re: which-key, (continued)
- RE: [External] : Re: Adding a "quick-help" menu, Drew Adams, 2022/09/17
- Re: Adding a "quick-help" menu, Stefan Kangas, 2022/09/16
- Re: Adding a "quick-help" menu, Philip Kaludercic, 2022/09/16
- RE: [External] : Re: Adding a "quick-help" menu, Drew Adams, 2022/09/16
- RE: [External] : Re: Adding a "quick-help" menu, Drew Adams, 2022/09/16
- Re: Adding a "quick-help" menu, Stefan Monnier, 2022/09/16
- Re: Adding a "quick-help" menu, Po Lu, 2022/09/16
Re: Adding a "quick-help" menu, Philip Kaludercic, 2022/09/16
- Re: Adding a "quick-help" menu,
Philip Kaludercic <=
Re: Adding a "quick-help" menu, Po Lu, 2022/09/16
Re: Adding a "quick-help" menu, Philip Kaludercic, 2022/09/17