Debian Codegraph @

discover-my-major.el

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