Debian Codegraph @

discover-my-major.el

raw ◀ ▶
1 Debian patch touch this file
1 ;;; discover-my-major.el --- Discover key bindings and their meaning for the current Emacs major mode
2
3 ;; Copyright (C) 2018, Steckerhalter
4
5 ;; Author: steckerhalter
•6 ;; Version: 1.0
7 ;; Package-Requires: ((makey "0.2"))
8 ;; URL: https://framagit.org/steckerhalter/discover-my-major
9 ;; Keywords: discover help major-mode keys
10
11 ;; This file is not part of GNU Emacs.
12
13 ;; This program is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
25
26 ;;; Commentary:
27
28 ;; Discover key bindings and their meaning for the current Emacs major mode
29
30 ;;; Code:
31
32 (require 'makey)
33
34 (defun dmm/major-mode-actions (&optional buffer)
35 "Returns a formatted list of major mode actions in BUFFER.
36 If BUFFER is not specified then use the current buffer."
37 (delq nil (mapcar 'dmm/format-binding (dmm/major-mode-bindings buffer))))
38
39 (defun dmm/mode-actions (mode &optional buffer)
40 "Return a formatted list of actions for MODE in BUFFER.
41 If BUFFER is not specified then use the current buffer."
42 (delq nil (mapcar 'dmm/format-binding (dmm/mode-bindings mode buffer))))
43
44 (defun dmm/mode-bindings (mode &optional buffer)
45 "Return a list with the bindings of MODE in BUFFER.
46 If BUFFER is not specified then use the current buffer."
47 (let* ((buffer (or buffer (current-buffer)))
48 (mode-name (if (symbolp mode)
49 (symbol-name mode)
50 mode))
51 (mode-string (dmm/mode-string mode-name)))
52 (cdr (assoc mode-string (dmm/descbinds-all-sections (current-buffer))))))
53
54 (defun dmm/major-mode-bindings (&optional buffer)
55 "Return a list with the bindings of BUFFER.
56 If BUFFER is not specified then use the current buffer."
57 (let ((buffer (or buffer (current-buffer))))
58 (dmm/mode-bindings major-mode)))
59
60 (defun dmm/mode-string (mode)
61 "Return the string describing MODE.
62 This string is at the start of the section describing MODE in the
63 output of `describe-buffer-bindings'."
64 (let* ((mode-name (if (symbolp mode)
65 (symbol-name mode)
66 mode))
67 (mode-string (if (eq mode-name (symbol-name major-mode))
68 "Major Mode Bindings:"
69 (format "`%s' Minor Mode Bindings:" mode-name))))
70 mode-string))
71
72 (defun dmm/mode-has-bindings-p (mode &optional buffer)
73 "Returns t if MODE has bindings defined in BUFFER.
74 If BUFFER is nil, checks for bindings in `current-buffer'. Returns nil
75 if MODE is not a mode symbol or mode name or if MODE has no actions in
76 BUFFER."
77 (let* ((mode-symbol (if (symbolp mode)
78 mode
79 (intern mode))))
80 (> (safe-length (dmm/mode-bindings mode-symbol (current-buffer))) 0)))
81
82 (defun dmm/doc-summary (f)
83 "Return the docstring for function F.
84
85 If F is not a function, return nil.
86
87 If F is is not documented, return a string indicating it is
88 undocumented."
89 (when (functionp f)
90 (let ((doc (documentation f)))
91 (if doc
92 (let* ((docstring (cdr (help-split-fundoc doc nil)))
93 (get-summary (lambda (str)
94 (string-match "^\\(.*\\)$" str)
95 (match-string 0 str))))
96 (funcall get-summary (if docstring docstring doc)))
97 (format "`%s' (not documented)" f)))))
98
99 (defun dmm/format-binding (item)
100 "Check if ITEM has documention and return the formatted action for ITEM."
101 (let* ((key (car item))
102 (str (cdr item))
103 (sym (intern-soft str))
104 (doc (dmm/doc-summary sym)))
105 (when doc
106 (list key doc sym))))
107
108 (defun dmm/descbinds-all-sections (buffer &optional prefix menus)
109 "Get the output from `describe-buffer-bindings' and parse the
110 result into a list with sections."
111 (with-temp-buffer
112 (let ((indent-tabs-mode t))
113 (describe-buffer-bindings buffer prefix menus))
114 (goto-char (point-min))
115 (let ((header-p (not (= (char-after) ?\f)))
116 sections header section)
117 (while (not (eobp))
118 (cond
119 (header-p
120 (setq header (buffer-substring-no-properties
121 (point)
122 (line-end-position)))
123 (setq header-p nil)
124 (forward-line 3))
125 ((= (char-after) ?\f)
126 (push (cons header (nreverse section)) sections)
127 (setq section nil)
128 (setq header-p t))
129 ((looking-at "^[ \t]*$")
130 ;; ignore
131 )
132 (t
133 (let ((binding-start (save-excursion
134 (and (re-search-forward "\t+" nil t)
135 (match-end 0))))
136 key binding)
137 (when binding-start
138 (setq key (buffer-substring-no-properties (point) binding-start)
139 key (replace-regexp-in-string"^[ \t\n]+" "" key)
140 key (replace-regexp-in-string"[ \t\n]+$" "" key))
141 (goto-char binding-start)
142 (setq binding (buffer-substring-no-properties
143 binding-start
144 (line-end-position)))
145 (unless (member binding '("self-insert-command"))
146 (push (cons key binding) section))))))
147 (forward-line))
148 (push (cons header (nreverse section)) sections)
149 (nreverse sections))))
150
151 (defun dmm/get-makey-func (group-name)
152 "If a makey function for GROUP-NAME is defind return the symbol, otherwise nil."
153 (intern-soft (concat "makey-key-mode-popup-" (symbol-name group-name))))
154
155 ;;;###autoload
156 (defun discover-my-major (arg)
157 "Create a makey popup listing all major-mode keys with their description.
158 If ARG is non-nil recreate the makey popup function even if it is already defined."
159 (interactive "P")
160 (let* ((group-name major-mode))
161 (when (or (not (dmm/get-makey-func group-name)) arg)
162 (makey-initialize-key-groups
163 (list `(,major-mode
164 (description ,(format
165 "Discover my Major: `%s' --- %s"
166 major-mode
167 (replace-regexp-in-string
168 "[\e\r\n\t]+" " "
169 (documentation major-mode))))
170 (actions ,(cons major-mode (dmm/major-mode-actions (current-buffer))))))))
171 (funcall (dmm/get-makey-func group-name))))
172
173 (defvar dmm/discover-my-mode-history nil
174 "History list for `dmm/discover-my-mode'.")
175
176 ;;;###autoload
177 (defun discover-my-mode (mode)
178 "Create a makey popup listing all MODE keys with their description."
179 (interactive
180 (let* ((active-modes (dmm/list-active-modes)))
181 (list
182 (completing-read "Discover mode: " active-modes (lambda (_) t) t nil 'dmm/discover-my-mode-history nil))))
183 (let* ((mode-name (if (symbolp mode)
184 (symbol-name mode)
185 mode))
186 (mode-symbol
187 (if (symbolp mode)
188 mode
189 (intern mode))))
190 (if (dmm/mode-has-bindings-p mode-symbol)
191 (makey-initialize-key-groups
192 (list `(,mode-symbol
193 (description ,(format
194 "Discover my Mode: `%s' --- %s"
195 mode-symbol
196 (replace-regexp-in-string
197 "[\e\r\n\t]+" " "
198 (documentation mode-symbol))))
199 (actions ,(cons mode-symbol (dmm/mode-actions mode-symbol (current-buffer)))))))
200 (error "Mode `%s' has no bindings in the current buffer." mode-name))
201 (funcall (dmm/get-makey-func mode-symbol))))
202
203 (defun dmm/list-active-modes ()
204 "Returns a list of the active modes in the current buffer."
205 (let ((active-modes))
206 (mapc (lambda (mode) (condition-case nil
207 (if (and (symbolp mode) (symbol-value mode))
208 (add-to-list 'active-modes (prin1-to-string mode)))
209 (error nil) ))
210 minor-mode-list)
211 active-modes))
212
213 (provide 'discover-my-major)
214
215 ;;; discover-my-major.el ends here