From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Boruch Baum Newsgroups: gmane.emacs.bugs Subject: bug#43709: minibuffer keybinding cheatsheet and launcher [CODE SUBMISSION] Date: Tue, 29 Sep 2020 12:59:57 -0400 Message-ID: <20200929165957.ibj67dnyaem6nezg@E15-2016.optimum.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="6mzweytivy3davt4" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32156"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: NeoMutt/20180716 To: 43709@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Sep 29 19:04:05 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kNJ2q-0008DV-Sp for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 29 Sep 2020 19:04:04 +0200 Original-Received: from localhost ([::1]:45070 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kNJ2p-0008Tf-E4 for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 29 Sep 2020 13:04:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40392) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kNIzv-0007vb-G6 for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2020 13:01:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:45489) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kNIzu-0000Vr-24 for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2020 13:01:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kNIzu-0000to-0T for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2020 13:01:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Boruch Baum Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 29 Sep 2020 17:01:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 43709 X-GNU-PR-Package: emacs X-Debbugs-Original-To: Emacs Bug Reporting Original-Received: via spool by submit@debbugs.gnu.org id=B.16013988153366 (code B ref -1); Tue, 29 Sep 2020 17:01:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 29 Sep 2020 17:00:15 +0000 Original-Received: from localhost ([127.0.0.1]:57035 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kNIz8-0000sD-RD for submit@debbugs.gnu.org; Tue, 29 Sep 2020 13:00:15 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:60046) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kNIz6-0000s5-Aw for submit@debbugs.gnu.org; Tue, 29 Sep 2020 13:00:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40116) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kNIz4-0007RN-H9 for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2020 13:00:10 -0400 Original-Received: from mout.gmx.net ([212.227.17.20]:57707) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kNIz0-0000Ar-B9 for bug-gnu-emacs@gnu.org; Tue, 29 Sep 2020 13:00:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1601398800; bh=x/prd6ycfv8FgCfheIkxELMB8n5KRBZory+qMGm3XWQ=; h=X-UI-Sender-Class:Date:From:To:Subject; b=emmIY8WjGc+t7bbduRr/4oWY4Kx3Z1PcUk/NzxidfBBwNqvt490CDCxD6hciaLr1e KL16meXRFmE0Pd7nNp/+HxylFUxH6xJQwUWEYE8xWW1eW6QjP45j7SQiR75OQWWLrp 6Q1pL2jvD8pfxbwgyVzT6B8VljfLRAbUL6kHvcIk= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Original-Received: from E15-2016.optimum.net ([72.89.170.172]) by mail.gmx.com (mrgmx104 [212.227.17.174]) with ESMTPSA (Nemesis) id 1MeCtj-1kwvJQ3SwZ-00bH1Y for ; Tue, 29 Sep 2020 19:00:00 +0200 Content-Disposition: inline X-Provags-ID: V03:K1:4ODrfoiEzcs3tS0x/EgbwGQL+a11iB/h/CuTfLO+FTfoGyvIQy8 AGI9XT6U5mMBqilfUSc1bmpMYRF2xP/3r0x02QkF13EvcR4jVNlRoxBqZIpIsTxktD6WdKe nbZNKAuqtTwMKyqkIA2sDUCZUj2r9k/MjPW/piKi94NbC4jEdvn9AKhCllwL2E+6aAyJzJJ OMBwUSTGakQXbusNAIbJA== X-UI-Out-Filterresults: notjunk:1;V03:K0:wRy5TLbBJUU=:1rdq8k2YDF8Nj9k/j2NCDP ybirLQCSqKHELkZAieVJvIa7DBAfyBmaro36798prRB+B/GYoygOHwnnfKmgIbTAMLmUXfBUz stCnA7DG7V9WK02lqzl/hzIjyCMmrdwZN50n/TO6dbF7a0hUuhYizKuN1VAvzWfETByc5g2oe mRZZF7SReJVzspU6sakMuv0taPwkdjPnHC8Ph5YFmYSJz5nf6FRKGp4qyvh9txIu4CLRAGv27 InQJKS3hKsZgGEebtRjBO6dQaIUJsn/VXf5kF9AnkD6ZI6w6Jrk6ZEqc8UvDMpjkZCQAh4WF6 pXTVIfAg5L+B4NuMhnvXLbz2slpLKwjLPXFnPDJfXoLFzN+3WjnCJ0vZtfPcUgQ0kJMMtUxPI tFcgdEvUb+xqkTqpnEXLsgHbnw4sYxQPf1hD+QBTdGFsHkVfEa7TVQjkCtG3OOP9vcrllxraC Q9ucWgzLbAmcREeoe/GUxhszYe7VfcfPcY8649sRZoWfcz9ipcz+mZ/tQ7EVqYO2940R5zvTf iHDHAYcVb6WWLaVwnr5hbvVNgXWCYqqkqUHPLX6Rpdxybofr6RIPk/UgELZ75jr9Zb1X7zqfc yH9LAs008jZM7D/6Pvk82pD7E+inboZfqHBh1HSUeIsvAR0GRyf5KYC2ukwYnw0zQ3/5lOfZq 9roSAgmJc+sFRQaFNjlzusBvgQXynF7SESOmVj/qlBfDv5qK8zXhCUKq374sqeJomSgGCAwiC eX7stGGVS+79jSpNurJ7t3eFkig2L7DPumtzBzTyovdKSCuGG0//jq2Mgbr5ZX6nOHVtSEJt Received-SPF: pass client-ip=212.227.17.20; envelope-from=boruch_baum@gmx.com; helo=mout.gmx.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/09/29 13:00:00 X-ACL-Warn: Detected OS = Linux 3.11 and newer X-Spam_score_int: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.614, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:189282 Archived-At: --6mzweytivy3davt4 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable If you've ever used packages such as `ivy' or `magit', you've probably benefited from each's custom combination keybinding cheatsheet and launcher: `hydra' in the case of `ivy', and `transient' for `magit'. The attached package `key-assist' attempts to offer a generic and very simple alternative requiring only the `completing-read' function commonly used in core vanilla emacs. `key-assist' is trivial to implement "on-the-fly" interactively for any buffer, and programmatically much simpler to customize that either `hydra' or `transient'. And did I mention that it only requires `completing-read'? =2D- hkp://keys.gnupg.net CA45 09B5 5351 7C11 A9D1 7286 0036 9E45 1595 8BC0 --6mzweytivy3davt4 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: attachment; filename="key-assist.el" Content-Transfer-Encoding: quoted-printable ;;; key-assist.el --- minibuffer keybinding cheatsheet and launcher -*- le= xical-binding: t -*- ;; Copyright =A9 2020, Boruch Baum ;; Available for assignment to the Free Software Foundation, Inc. ;; Author: Boruch Baum ;; Maintainer: Boruch Baum ;; Keywords: minibuffer keybindings ;; Package: key-assist ;; This file is NOT part of GNU Emacs. ;; This is free software: you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; This software is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with this software. If not, see . ;; ;;; Commentary: ;; If you've ever used packages such as `ivy' or `magit', you've ;; probably benefited from each's custom combination keybinding ;; cheatsheet and launcher: `hydra' in the case of `ivy', and ;; `transient' for `magit'. The current package `key-assist' ;; attempts to offer a generic and very simple alternative requiring ;; only the `completing-read' function commonly used in core vanilla ;; emacs. `key-assist' is trivial to implement "on-the-fly" ;; interactively for any buffer, and programmatically much simpler ;; to customize that either `hydra' or `transient'. And did I ;; mention that it only requires `completing-read'? ;; ;;; Dependencies: ;; `cl-seq' - For `cl-position', but `cl-lib' is long part of all ;; modern emcasen. ;; ;;; Installation: ;; 1) Evaluate or load this file. ;; ;;; Interactive operation: ;; Run M-x key-assist from the buffer of interest. Specify a ;; selection (or don't), press to view the presentation, and ;; then either exit with your new-found knowledge of the command ;; keybindings, or use standard emacs tab completion to select an ;; option, and press to perform the action. ;; ;; If you choose not to respond to the initial prompt, a list of ;; keybindings and command descriptions will be generated based upon ;; the first word of the buffer's major mode. For, example, in a ;; `w3m' buffer, the list will be of all interactive functions ;; beginning `w3m-'. This works out to be great as a default, but ;; isn't always useful. For example, in an `emacs-lisp-mode' buffer, ;; what would you expect it to usefully produce? At the other ;; extreme might be a case of a buffer too many obscure keybindings ;; of little use. ;; You can also respond to the prompt with your own regexp of ;; commands to show, or with the name of a keymap of your choice. ;; For the purposes of `key-assist', a regexp can be just a ;; substring, without REQUIRING any leading or trailing globs. ;; In all cases, note that the package can only present keybindings ;; currently active in the current buffer, so if a sub-package ;; hasn't been loaded yet, that package's keybindings would not be ;; presented. Also note that the commands are presented sorted by ;; keybinding length, alphabetically. ;; ;;; Programmatic operation: ;; See the docstrings for functions `key-assist' and ;; `key-assist--get-cmds'. ;; ;;; Configuration: ;; Two variables are available to exclude items from the ;; presentation list: `key-assist-exclude-cmds' and ;; `key-assist-exclude-regexps'. See there for further information. ;; ;;; Compatability ;; Tested with emacs 26.1 in debian. ;; TODO: ;; ;; * In a vanilla emacs environment, the sorting appears wrong ;; seemingly partially because of how the completion buffer is ;; constructed, bottom-up. This isn't an issue when using one of ;; the supplemental minibuffer completion packages. =0C ;; ;;; Code (require 'cl-seq) ;; cl-member, cl-position ;; ;;; Variables (defvar key-assist-exclude-cmds '(ignore self-insert-command digit-argument negative-argument describe-mode) "List of commands to always exclude from `key-assist' output.") (defvar key-assist-exclude-regexps '("-mouse-") "List of regexps of commands to exclude from `key-assist' output.") ;; TODO: Don't depend upon a mouse command having the word '-mouse-' in it= . ;; ;;; Internal functions (defun key-assist--get-keybinding (cmd &optional key-map) "Return a string with CMD's shortest keybinding." (let (shortest) (dolist (key (mapcar 'key-description (where-is-internal cmd key-map nil t))) (when (or (not shortest) (> (length shortest) (length key))) (setq shortest key))) shortest)) (defun key-assist--get-description (cmd) "Return a string with CMD's description. CMD is a symbol of an interactive command." ;; TODO: Change hard-coded length to an ARG. (let ((doc (documentation cmd t))) (format "\t%s" (if (or (not doc) (not (string-match "\n" doc)) (zerop (match-beginning 0))) (concat (symbol-name cmd) " (not documented)") (substring doc 0 (match-beginning 0)))))) (defun key-assist--vet-cmd (cmd result-list) "Check whether CMD should be on a `key-assist' list. See `key-assist-exclude-cmds' and `key-assist-exclude-regexps'." (and (symbolp cmd) (commandp cmd) (not (cl-member cmd result-list :test (lambda (cmd l) (equal cmd (nth 1 l))))) ; (assq= cmd result-list)) (not (memq cmd key-assist-exclude-cmds)) (let ((not-found t)) (mapc (lambda (x) (when (string-match x (format "%s" cmd)) (setq not-found nil))) key-assist-exclude-regexps) not-found))) (defun key-assist--parse-cmd (cmd result-list &optional key-map) "Extract commands and shortest keybindings from a keymap. This is an internal function used by `key-assist'. Returns a list whose elements are a keybinding string, a command symbol, and a description string." (when (key-assist--vet-cmd cmd result-list) (let* ((key-map (when (keymapp key-map) key-map)) (shortest (key-assist--get-keybinding cmd key-map))) (when shortest (list shortest cmd (concat shortest (key-assist--get-description c= md))))))) (defun key-assist--get-cmds (&optional spec nosort nofinish) "Return a list of commands, keybindings, and descriptions. Returns a list of CONS, whose CAR is the command, and whose CDR is a string of the form \"shortest-keybinding tab-character command-description\". Optional arg SPEC may be a regexp string of desired commands. If NIL, a regexp is generated based upon the first word of the buffer's major mode. SPEC may also be a keymap of desired commands. In both of these cases, the resulting list is sorted alphabetically by keybinding length. SPEC has additional options of being either a list of commands, or a list of CONS whose CAR is a command, and whose CDR is either a description-string or a function which returns a description string. A final programmatic option is for SPEC to be any combination of the above options. For that most complex case, the first list element of SPEC must be the symbol 'collection. For none of these additional options is sorting performed. Optional arg NOSORT can be a function to replace the default sort algorithm with the programmer's desired post-processing, or some other non-nil value for no sorting at all. If a function, it should accept a single list of elements (keybinding-string commandp description-string) and should return a list of elements (anything commandp description-string). Optional arg NOFINSH return a list in `key-assist--parse-cmd' format instead of the list of CONS described above. It is used internally for processing 'collection lists." (setq spec (cond ((or (not spec) (and (stringp spec) (zerop (length spec)))) (let ((str (symbol-name major-mode))) (substring str 0 (1+ (string-match "-" str))))) ((and (stringp spec) (boundp (intern spec)) (keymapp (symbol-value (intern spec)))) (symbol-value (intern spec))) (t spec))) (let (name result-elem (result-list '())) (cond ((keymapp spec) (let (cmd) (dolist (elem spec) (cond ((atom elem)) ;; eg. 'keymap ((listp (setq cmd (cdr elem)))) ;; TODO: possibly also embedde= d keymap? ((commandp cmd) ;; this excludes 'menubar (when (setq result-elem (key-assist--parse-cmd cmd result-li= st)) (push result-elem result-list))))))) ((stringp spec) (mapatoms (lambda (x) (and (commandp x) (string-match spec (setq name (symbol-name x))) (when (setq result-elem (key-assist--parse-cmd x result-list)) (push result-elem result-list)))))) ((listp spec) (cond ((eq (car spec) 'collection) (dolist (collection-element (cdr spec)) ;; Maybe it's more efficient to sort each collection element? (let ((temp-list (key-assist--get-cmds collection-element 'nos= ort 'nofinish))) (dolist (elem temp-list) (push elem result-list))))) ((commandp (car spec)) (dolist (cmd spec) (when (setq result-elem (key-assist--parse-cmd cmd result-list= )) (push result-elem result-list)))) (t ; spec is a list of CONS (cmd . (or string function)) (dolist (elem spec) (when (key-assist--vet-cmd (car elem) result-list) (let ((shortest (key-assist--get-keybinding (car elem)))) (when shortest (push (list shortest (car elem) (if (stringp (cadr elem)) (cadr elem) (funcall (cadr elem)))) result-list)))))))) (t (error "Improper SPEC format."))) (when (not nosort) (setq result-list (if (functionp nosort) (funcall nosort result-list) (sort result-list (lambda (a b) (cond ((=3D (length (car a)) (length (car b))) (string< (car a) (car b))) ((< (length (car a)) (length (car b)))) (t nil))))))) (if nofinish result-list (mapcar (lambda (x) (cons (nth 1 x) (nth 2 x))) result-list)))) ;; ;;; Interactive functions (defun key-assist (&optional spec prompt nosort) "Prompt to eval a locally relevant function, with hints and keybindings. Press TAB to see the hints. Interactively, the optional arg SPEC is either a regexp string for candidate commands to match, or a keymap from which to prepare the hints. If NIL, a regexp is generated based upon the first word of the buffer's major mode. Results are presented sorted alphabetically by keybinding length. Programmatically, optional arg PROMPT can be used to customize the prompt. For the further programmatic options of SPEC and for a description of arg NOSORT, see function `key-assist--get-cmds'. See also variables `key-assist-exclude-regexps' and `key-assist-exclude-cmds'." (interactive "MOptional: Enter command regexp or keymap name: ") (let* ((prompt (or prompt "Select: ")) (commands (key-assist--get-cmds spec nosort)) (choices (mapcar 'cdr commands)) choice minibuffer-history) (if (not choices) (user-error "No choices found.") (while (not (setq choice (cl-position (substring-no-properties ; Is -no-properties necessar= y? (completing-read prompt choices nil t)) choices :test 'equal)))) (funcall (car (nth choice commands)))))) =0C ;; ;;; Conclusion (provide 'key-assist) ;;; key-assist.el ends here --6mzweytivy3davt4--