edmacro.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. ;;; edmacro.el --- keyboard macro editor
  2. ;; Copyright (C) 1993-1994, 2001-2015 Free Software Foundation, Inc.
  3. ;; Author: Dave Gillespie <daveg@synaptics.com>
  4. ;; Maintainer: Dave Gillespie <daveg@synaptics.com>
  5. ;; Version: 2.01
  6. ;; Keywords: abbrev
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;;; Usage:
  20. ;;
  21. ;; The `C-x C-k e' (`edit-kbd-macro') command edits a keyboard macro
  22. ;; in a special buffer. It prompts you to type a key sequence,
  23. ;; which should be one of:
  24. ;;
  25. ;; * RET or `C-x e' (call-last-kbd-macro), to edit the most
  26. ;; recently defined keyboard macro.
  27. ;;
  28. ;; * `M-x' followed by a command name, to edit a named command
  29. ;; whose definition is a keyboard macro.
  30. ;;
  31. ;; * `C-h l' (view-lossage), to edit the 300 most recent keystrokes
  32. ;; and install them as the "current" macro.
  33. ;;
  34. ;; * any key sequence whose definition is a keyboard macro.
  35. ;;
  36. ;; This file includes a version of `insert-kbd-macro' that uses the
  37. ;; more readable format defined by these routines.
  38. ;;
  39. ;; Also, the `read-kbd-macro' command parses the region as
  40. ;; a keyboard macro, and installs it as the "current" macro.
  41. ;; This and `format-kbd-macro' can also be called directly as
  42. ;; Lisp functions.
  43. ;; Type `C-h m', or see the documentation for `edmacro-mode' below,
  44. ;; for information about the format of written keyboard macros.
  45. ;; `edit-kbd-macro' formats the macro with one command per line,
  46. ;; including the command names as comments on the right. If the
  47. ;; formatter gets confused about which keymap was used for the
  48. ;; characters, the command-name comments will be wrong but that
  49. ;; won't hurt anything.
  50. ;; With a prefix argument, `edit-kbd-macro' will format the
  51. ;; macro in a more concise way that omits the comments.
  52. ;;; Code:
  53. (require 'cl-lib)
  54. (require 'kmacro)
  55. ;;; The user-level commands for editing macros.
  56. (defcustom edmacro-eight-bits nil
  57. "Non-nil if `edit-kbd-macro' should leave 8-bit characters intact.
  58. Default nil means to write characters above \\177 in octal notation."
  59. :type 'boolean
  60. :group 'kmacro)
  61. (defvar edmacro-mode-map
  62. (let ((map (make-sparse-keymap)))
  63. (define-key map "\C-c\C-c" 'edmacro-finish-edit)
  64. (define-key map "\C-c\C-q" 'edmacro-insert-key)
  65. map))
  66. (defvar edmacro-store-hook)
  67. (defvar edmacro-finish-hook)
  68. (defvar edmacro-original-buffer)
  69. ;;;###autoload
  70. (defun edit-kbd-macro (keys &optional prefix finish-hook store-hook)
  71. "Edit a keyboard macro.
  72. At the prompt, type any key sequence which is bound to a keyboard macro.
  73. Or, type `C-x e' or RET to edit the last keyboard macro, `C-h l' to edit
  74. the last 300 keystrokes as a keyboard macro, or `\\[execute-extended-command]' to edit a macro by
  75. its command name.
  76. With a prefix argument, format the macro in a more concise way."
  77. (interactive "kKeyboard macro to edit (C-x e, M-x, C-h l, or keys): \nP")
  78. (when keys
  79. (let ((cmd (if (arrayp keys) (key-binding keys) keys))
  80. (mac nil) (mac-counter nil) (mac-format nil)
  81. kmacro)
  82. (cond (store-hook
  83. (setq mac keys)
  84. (setq cmd nil))
  85. ((or (memq cmd '(call-last-kbd-macro kmacro-call-macro
  86. kmacro-end-or-call-macro kmacro-end-and-call-macro))
  87. (member keys '("\r" [return])))
  88. (or last-kbd-macro
  89. (y-or-n-p "No keyboard macro defined. Create one? ")
  90. (keyboard-quit))
  91. (setq mac (or last-kbd-macro ""))
  92. (setq keys nil)
  93. (setq cmd 'last-kbd-macro))
  94. ((eq cmd 'execute-extended-command)
  95. (setq cmd (read-command "Name of keyboard macro to edit: "))
  96. (if (string-equal cmd "")
  97. (error "No command name given"))
  98. (setq keys nil)
  99. (setq mac (symbol-function cmd)))
  100. ((memq cmd '(view-lossage electric-view-lossage))
  101. (setq mac (recent-keys))
  102. (setq keys nil)
  103. (setq cmd 'last-kbd-macro))
  104. ((null cmd)
  105. (error "Key sequence %s is not defined" (key-description keys)))
  106. ((symbolp cmd)
  107. (setq mac (symbol-function cmd)))
  108. (t
  109. (setq mac cmd)
  110. (setq cmd nil)))
  111. (when (setq kmacro (kmacro-extract-lambda mac))
  112. (setq mac (car kmacro)
  113. mac-counter (nth 1 kmacro)
  114. mac-format (nth 2 kmacro)))
  115. (unless (arrayp mac)
  116. (error "Key sequence %s is not a keyboard macro"
  117. (key-description keys)))
  118. (message "Formatting keyboard macro...")
  119. (let* ((oldbuf (current-buffer))
  120. (mmac (edmacro-fix-menu-commands mac))
  121. (fmt (edmacro-format-keys mmac 1))
  122. (fmtv (edmacro-format-keys mmac (not prefix)))
  123. (buf (get-buffer-create "*Edit Macro*")))
  124. (message "Formatting keyboard macro...done")
  125. (switch-to-buffer buf)
  126. (kill-all-local-variables)
  127. (use-local-map edmacro-mode-map)
  128. (setq buffer-read-only nil)
  129. (setq major-mode 'edmacro-mode)
  130. (setq mode-name "Edit Macro")
  131. (set (make-local-variable 'edmacro-original-buffer) oldbuf)
  132. (set (make-local-variable 'edmacro-finish-hook) finish-hook)
  133. (set (make-local-variable 'edmacro-store-hook) store-hook)
  134. (erase-buffer)
  135. (insert ";; Keyboard Macro Editor. Press C-c C-c to finish; "
  136. "press C-x k RET to cancel.\n")
  137. (insert ";; Original keys: " fmt "\n")
  138. (unless store-hook
  139. (insert "\nCommand: " (if cmd (symbol-name cmd) "none") "\n")
  140. (let ((gkeys (where-is-internal (or cmd mac) '(keymap))))
  141. (if (and keys (not (member keys gkeys)))
  142. (setq gkeys (cons keys gkeys)))
  143. (if gkeys
  144. (while gkeys
  145. (insert "Key: " (edmacro-format-keys (pop gkeys) 1) "\n"))
  146. (insert "Key: none\n")))
  147. (when (and mac-counter mac-format)
  148. (insert (format "Counter: %d\nFormat: \"%s\"\n" mac-counter mac-format))))
  149. (insert "\nMacro:\n\n")
  150. (save-excursion
  151. (insert fmtv "\n"))
  152. (recenter '(4))
  153. (when (eq mac mmac)
  154. (set-buffer-modified-p nil))
  155. (run-hooks 'edmacro-format-hook)))))
  156. ;;; The next two commands are provided for convenience and backward
  157. ;;; compatibility.
  158. ;;;###autoload
  159. (defun edit-last-kbd-macro (&optional prefix)
  160. "Edit the most recently defined keyboard macro."
  161. (interactive "P")
  162. (edit-kbd-macro 'call-last-kbd-macro prefix))
  163. ;;;###autoload
  164. (defun edit-named-kbd-macro (&optional prefix)
  165. "Edit a keyboard macro which has been given a name by `name-last-kbd-macro'."
  166. (interactive "P")
  167. (edit-kbd-macro 'execute-extended-command prefix))
  168. ;;;###autoload
  169. (defun read-kbd-macro (start &optional end)
  170. "Read the region as a keyboard macro definition.
  171. The region is interpreted as spelled-out keystrokes, e.g., \"M-x abc RET\".
  172. See documentation for `edmacro-mode' for details.
  173. Leading/trailing \"C-x (\" and \"C-x )\" in the text are allowed and ignored.
  174. The resulting macro is installed as the \"current\" keyboard macro.
  175. In Lisp, may also be called with a single STRING argument in which case
  176. the result is returned rather than being installed as the current macro.
  177. The result will be a string if possible, otherwise an event vector.
  178. Second argument NEED-VECTOR means to return an event vector always."
  179. (interactive "r")
  180. (if (stringp start)
  181. (edmacro-parse-keys start end)
  182. (setq last-kbd-macro (edmacro-parse-keys (buffer-substring start end)))))
  183. ;;;###autoload
  184. (defun format-kbd-macro (&optional macro verbose)
  185. "Return the keyboard macro MACRO as a human-readable string.
  186. This string is suitable for passing to `read-kbd-macro'.
  187. Second argument VERBOSE means to put one command per line with comments.
  188. If VERBOSE is `1', put everything on one line. If VERBOSE is omitted
  189. or nil, use a compact 80-column format."
  190. (and macro (symbolp macro) (setq macro (symbol-function macro)))
  191. (edmacro-format-keys (or macro last-kbd-macro) verbose))
  192. ;;; Commands for *Edit Macro* buffer.
  193. (defun edmacro-finish-edit ()
  194. (interactive)
  195. (unless (eq major-mode 'edmacro-mode)
  196. (error
  197. "This command is valid only in buffers created by `edit-kbd-macro'"))
  198. (run-hooks 'edmacro-finish-hook)
  199. (let ((cmd nil) (keys nil) (no-keys nil)
  200. (mac-counter nil) (mac-format nil)
  201. (top (point-min)))
  202. (goto-char top)
  203. (let ((case-fold-search nil))
  204. (while (cond ((looking-at "[ \t]*\\($\\|;;\\|REM[ \t\n]\\)")
  205. t)
  206. ((looking-at "Command:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
  207. (when edmacro-store-hook
  208. (error "\"Command\" line not allowed in this context"))
  209. (let ((str (buffer-substring (match-beginning 1)
  210. (match-end 1))))
  211. (unless (equal str "")
  212. (setq cmd (and (not (equal str "none"))
  213. (intern str)))
  214. (and (fboundp cmd) (not (arrayp (symbol-function cmd)))
  215. (not (get cmd 'kmacro))
  216. (not (y-or-n-p
  217. (format "Command %s is already defined; %s"
  218. cmd "proceed? ")))
  219. (keyboard-quit))))
  220. t)
  221. ((looking-at "Key:\\(.*\\)$")
  222. (when edmacro-store-hook
  223. (error "\"Key\" line not allowed in this context"))
  224. (let ((key (edmacro-parse-keys
  225. (buffer-substring (match-beginning 1)
  226. (match-end 1)))))
  227. (unless (equal key "")
  228. (if (equal key "none")
  229. (setq no-keys t)
  230. (push key keys)
  231. (let ((b (key-binding key)))
  232. (and b (commandp b) (not (arrayp b))
  233. (not (kmacro-extract-lambda b))
  234. (or (not (fboundp b))
  235. (not (or (arrayp (symbol-function b))
  236. (get b 'kmacro))))
  237. (not (y-or-n-p
  238. (format "Key %s is already defined; %s"
  239. (edmacro-format-keys key 1)
  240. "proceed? ")))
  241. (keyboard-quit))))))
  242. t)
  243. ((looking-at "Counter:[ \t]*\\([^ \t\n]*\\)[ \t]*$")
  244. (when edmacro-store-hook
  245. (error "\"Counter\" line not allowed in this context"))
  246. (let ((str (buffer-substring (match-beginning 1)
  247. (match-end 1))))
  248. (unless (equal str "")
  249. (setq mac-counter (string-to-number str))))
  250. t)
  251. ((looking-at "Format:[ \t]*\"\\([^\n]*\\)\"[ \t]*$")
  252. (when edmacro-store-hook
  253. (error "\"Format\" line not allowed in this context"))
  254. (let ((str (buffer-substring (match-beginning 1)
  255. (match-end 1))))
  256. (unless (equal str "")
  257. (setq mac-format str)))
  258. t)
  259. ((looking-at "Macro:[ \t\n]*")
  260. (goto-char (match-end 0))
  261. nil)
  262. ((eobp) nil)
  263. (t (error "Expected a `Macro:' line")))
  264. (forward-line 1))
  265. (setq top (point)))
  266. (let* ((buf (current-buffer))
  267. (str (buffer-substring top (point-max)))
  268. (modp (buffer-modified-p))
  269. (obuf edmacro-original-buffer)
  270. (store-hook edmacro-store-hook)
  271. (finish-hook edmacro-finish-hook))
  272. (unless (or cmd keys store-hook (equal str ""))
  273. (error "No command name or keys specified"))
  274. (when modp
  275. (when (buffer-name obuf)
  276. (set-buffer obuf))
  277. (message "Compiling keyboard macro...")
  278. (let ((mac (edmacro-parse-keys str)))
  279. (message "Compiling keyboard macro...done")
  280. (if store-hook
  281. (funcall store-hook mac)
  282. (when (eq cmd 'last-kbd-macro)
  283. (setq last-kbd-macro (and (> (length mac) 0) mac))
  284. (setq cmd nil))
  285. (when cmd
  286. (if (= (length mac) 0)
  287. (fmakunbound cmd)
  288. (fset cmd
  289. (if (and mac-counter mac-format)
  290. (kmacro-lambda-form mac mac-counter mac-format)
  291. mac))))
  292. (if no-keys
  293. (when cmd
  294. (cl-loop for key in (where-is-internal cmd '(keymap)) do
  295. (global-unset-key key)))
  296. (when keys
  297. (if (= (length mac) 0)
  298. (cl-loop for key in keys do (global-unset-key key))
  299. (cl-loop for key in keys do
  300. (global-set-key key
  301. (or cmd
  302. (if (and mac-counter mac-format)
  303. (kmacro-lambda-form
  304. mac mac-counter mac-format)
  305. mac))))))))))
  306. (kill-buffer buf)
  307. (when (buffer-name obuf)
  308. (switch-to-buffer obuf))
  309. (when finish-hook
  310. (funcall finish-hook)))))
  311. (defun edmacro-insert-key (key)
  312. "Insert the written name of a key in the buffer."
  313. (interactive "kKey to insert: ")
  314. (if (bolp)
  315. (insert (edmacro-format-keys key t) "\n")
  316. (insert (edmacro-format-keys key) " ")))
  317. (defun edmacro-mode ()
  318. "\\<edmacro-mode-map>Keyboard Macro Editing mode. Press \
  319. \\[edmacro-finish-edit] to save and exit.
  320. To abort the edit, just kill this buffer with \\[kill-buffer] RET.
  321. Press \\[edmacro-insert-key] to insert the name of any key by typing the key.
  322. The editing buffer contains a \"Command:\" line and any number of
  323. \"Key:\" lines at the top. These are followed by a \"Macro:\" line
  324. and the macro itself as spelled-out keystrokes: `C-x C-f foo RET'.
  325. The \"Command:\" line specifies the command name to which the macro
  326. is bound, or \"none\" for no command name. Write \"last-kbd-macro\"
  327. to refer to the current keyboard macro (as used by \\[call-last-kbd-macro]).
  328. The \"Key:\" lines specify key sequences to which the macro is bound,
  329. or \"none\" for no key bindings.
  330. You can edit these lines to change the places where the new macro
  331. is stored.
  332. Format of keyboard macros during editing:
  333. Text is divided into \"words\" separated by whitespace. Except for
  334. the words described below, the characters of each word go directly
  335. as characters of the macro. The whitespace that separates words
  336. is ignored. Whitespace in the macro must be written explicitly,
  337. as in \"foo SPC bar RET\".
  338. * The special words RET, SPC, TAB, DEL, LFD, ESC, and NUL represent
  339. special control characters. The words must be written in uppercase.
  340. * A word in angle brackets, e.g., <return>, <down>, or <f1>, represents
  341. a function key. (Note that in the standard configuration, the
  342. function key <return> and the control key RET are synonymous.)
  343. You can use angle brackets on the words RET, SPC, etc., but they
  344. are not required there.
  345. * Keys can be written by their ASCII code, using a backslash followed
  346. by up to six octal digits. This is the only way to represent keys
  347. with codes above \\377.
  348. * One or more prefixes M- (meta), C- (control), S- (shift), A- (alt),
  349. H- (hyper), and s- (super) may precede a character or key notation.
  350. For function keys, the prefixes may go inside or outside of the
  351. brackets: C-<down> = <C-down>. The prefixes may be written in
  352. any order: M-C-x = C-M-x.
  353. Prefixes are not allowed on multi-key words, e.g., C-abc, except
  354. that the Meta prefix is allowed on a sequence of digits and optional
  355. minus sign: M--123 = M-- M-1 M-2 M-3.
  356. * The `^' notation for control characters also works: ^M = C-m.
  357. * Double angle brackets enclose command names: <<next-line>> is
  358. shorthand for M-x next-line RET.
  359. * Finally, REM or ;; causes the rest of the line to be ignored as a
  360. comment.
  361. Any word may be prefixed by a multiplier in the form of a decimal
  362. number and `*': 3*<right> = <right> <right> <right>, and
  363. 10*foo = foofoofoofoofoofoofoofoofoofoo.
  364. Multiple text keys can normally be strung together to form a word,
  365. but you may need to add whitespace if the word would look like one
  366. of the above notations: `; ; ;' is a keyboard macro with three
  367. semicolons, but `;;;' is a comment. Likewise, `\\ 1 2 3' is four
  368. keys but `\\123' is a single key written in octal, and `< right >'
  369. is seven keys but `<right>' is a single function key. When in
  370. doubt, use whitespace."
  371. (interactive)
  372. (error "This mode can be enabled only by `edit-kbd-macro'"))
  373. (put 'edmacro-mode 'mode-class 'special)
  374. ;;; Formatting a keyboard macro as human-readable text.
  375. (defun edmacro-format-keys (macro &optional verbose)
  376. (setq macro (edmacro-fix-menu-commands macro))
  377. (let* ((maps (current-active-maps))
  378. (pkeys '(end-macro ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9 ?- ?\C-u
  379. ?\M-- ?\M-0 ?\M-1 ?\M-2 ?\M-3 ?\M-4 ?\M-5 ?\M-6
  380. ?\M-7 ?\M-8 ?\M-9))
  381. (mdigs (nthcdr 13 pkeys))
  382. (maxkey (if edmacro-eight-bits 255 127))
  383. (case-fold-search nil)
  384. (res-words '("NUL" "TAB" "LFD" "RET" "ESC" "SPC" "DEL" "REM"))
  385. (rest-mac (vconcat macro [end-macro]))
  386. (res "")
  387. (len 0)
  388. (one-line (eq verbose 1)))
  389. (if one-line (setq verbose nil))
  390. (when (stringp macro)
  391. (cl-loop for i below (length macro) do
  392. (when (>= (aref rest-mac i) 128)
  393. (cl-incf (aref rest-mac i) (- ?\M-\^@ 128)))))
  394. (while (not (eq (aref rest-mac 0) 'end-macro))
  395. (let* ((prefix
  396. (or (and (integerp (aref rest-mac 0))
  397. (memq (aref rest-mac 0) mdigs)
  398. (memq (key-binding (cl-subseq rest-mac 0 1))
  399. '(digit-argument negative-argument))
  400. (let ((i 1))
  401. (while (memq (aref rest-mac i) (cdr mdigs))
  402. (cl-incf i))
  403. (and (not (memq (aref rest-mac i) pkeys))
  404. (prog1 (vconcat "M-" (cl-subseq rest-mac 0 i) " ")
  405. (cl-callf cl-subseq rest-mac i)))))
  406. (and (eq (aref rest-mac 0) ?\C-u)
  407. (eq (key-binding [?\C-u]) 'universal-argument)
  408. (let ((i 1))
  409. (while (eq (aref rest-mac i) ?\C-u)
  410. (cl-incf i))
  411. (and (not (memq (aref rest-mac i) pkeys))
  412. (prog1 (cl-loop repeat i concat "C-u ")
  413. (cl-callf cl-subseq rest-mac i)))))
  414. (and (eq (aref rest-mac 0) ?\C-u)
  415. (eq (key-binding [?\C-u]) 'universal-argument)
  416. (let ((i 1))
  417. (when (eq (aref rest-mac i) ?-)
  418. (cl-incf i))
  419. (while (memq (aref rest-mac i)
  420. '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9))
  421. (cl-incf i))
  422. (and (not (memq (aref rest-mac i) pkeys))
  423. (prog1 (vconcat "C-u " (cl-subseq rest-mac 1 i) " ")
  424. (cl-callf cl-subseq rest-mac i)))))))
  425. (bind-len (apply 'max 1
  426. (cl-loop for map in maps
  427. for b = (lookup-key map rest-mac)
  428. when b collect b)))
  429. (key (cl-subseq rest-mac 0 bind-len))
  430. (fkey nil) tlen tkey
  431. (bind (or (cl-loop for map in maps for b = (lookup-key map key)
  432. thereis (and (not (integerp b)) b))
  433. (and (setq fkey (lookup-key local-function-key-map rest-mac))
  434. (setq tlen fkey tkey (cl-subseq rest-mac 0 tlen)
  435. fkey (lookup-key local-function-key-map tkey))
  436. (cl-loop for map in maps
  437. for b = (lookup-key map fkey)
  438. when (and (not (integerp b)) b)
  439. do (setq bind-len tlen key tkey)
  440. and return b
  441. finally do (setq fkey nil)))))
  442. (first (aref key 0))
  443. (text
  444. (cl-loop for i from bind-len below (length rest-mac)
  445. for ch = (aref rest-mac i)
  446. while (and (integerp ch)
  447. (> ch 32) (< ch maxkey) (/= ch 92)
  448. (eq (key-binding (char-to-string ch))
  449. 'self-insert-command)
  450. (or (> i (- (length rest-mac) 2))
  451. (not (eq ch (aref rest-mac (+ i 1))))
  452. (not (eq ch (aref rest-mac (+ i 2))))))
  453. finally return i))
  454. desc)
  455. (if (stringp bind) (setq bind nil))
  456. (cond ((and (eq bind 'self-insert-command) (not prefix)
  457. (> text 1) (integerp first)
  458. (> first 32) (<= first maxkey) (/= first 92)
  459. (progn
  460. (if (> text 30) (setq text 30))
  461. (setq desc (concat (cl-subseq rest-mac 0 text)))
  462. (when (string-match "^[ACHMsS]-." desc)
  463. (setq text 2)
  464. (cl-callf substring desc 0 2))
  465. (not (string-match
  466. "^;;\\|^<.*>$\\|^\\\\[0-9]+$\\|^[0-9]+\\*."
  467. desc))))
  468. (when (or (string-match "^\\^.$" desc)
  469. (member desc res-words))
  470. (setq desc (mapconcat 'char-to-string desc " ")))
  471. (when verbose
  472. (setq bind (format "%s * %d" bind text)))
  473. (setq bind-len text))
  474. ((and (eq bind 'execute-extended-command)
  475. (> text bind-len)
  476. (memq (aref rest-mac text) '(return 13))
  477. (progn
  478. (setq desc (concat (cl-subseq rest-mac bind-len text)))
  479. (commandp (intern-soft desc))))
  480. (if (commandp (intern-soft desc)) (setq bind desc))
  481. (setq desc (format "<<%s>>" desc))
  482. (setq bind-len (1+ text)))
  483. (t
  484. (setq desc (mapconcat
  485. (function
  486. (lambda (ch)
  487. (cond
  488. ((integerp ch)
  489. (concat
  490. (cl-loop for pf across "ACHMsS"
  491. for bit in '(?\A-\^@ ?\C-\^@ ?\H-\^@
  492. ?\M-\^@ ?\s-\^@ ?\S-\^@)
  493. when (/= (logand ch bit) 0)
  494. concat (format "%c-" pf))
  495. (let ((ch2 (logand ch (1- (lsh 1 18)))))
  496. (cond ((<= ch2 32)
  497. (pcase ch2
  498. (0 "NUL") (9 "TAB") (10 "LFD")
  499. (13 "RET") (27 "ESC") (32 "SPC")
  500. (_
  501. (format "C-%c"
  502. (+ (if (<= ch2 26) 96 64)
  503. ch2)))))
  504. ((= ch2 127) "DEL")
  505. ((<= ch2 maxkey) (char-to-string ch2))
  506. (t (format "\\%o" ch2))))))
  507. ((symbolp ch)
  508. (format "<%s>" ch))
  509. (t
  510. (error "Unrecognized item in macro: %s" ch)))))
  511. (or fkey key) " "))))
  512. (if prefix
  513. (setq desc (concat (edmacro-sanitize-for-string prefix) desc)))
  514. (unless (string-match " " desc)
  515. (let ((times 1) (pos bind-len))
  516. (while (not (cl-mismatch rest-mac rest-mac
  517. :start1 0 :end1 bind-len
  518. :start2 pos :end2 (+ bind-len pos)))
  519. (cl-incf times)
  520. (cl-incf pos bind-len))
  521. (when (> times 1)
  522. (setq desc (format "%d*%s" times desc))
  523. (setq bind-len (* bind-len times)))))
  524. (setq rest-mac (cl-subseq rest-mac bind-len))
  525. (if verbose
  526. (progn
  527. (unless (equal res "") (cl-callf concat res "\n"))
  528. (cl-callf concat res desc)
  529. (when (and bind (or (stringp bind) (symbolp bind)))
  530. (cl-callf concat res
  531. (make-string (max (- 3 (/ (length desc) 8)) 1) 9)
  532. ";; " (if (stringp bind) bind (symbol-name bind))))
  533. (setq len 0))
  534. (if (and (> (+ len (length desc) 2) 72) (not one-line))
  535. (progn
  536. (cl-callf concat res "\n ")
  537. (setq len 1))
  538. (unless (equal res "")
  539. (cl-callf concat res " ")
  540. (cl-incf len)))
  541. (cl-callf concat res desc)
  542. (cl-incf len (length desc)))))
  543. res))
  544. (defun edmacro-sanitize-for-string (seq)
  545. "Convert a key sequence vector SEQ into a string.
  546. The string represents the same events; Meta is indicated by bit 7.
  547. This function assumes that the events can be stored in a string."
  548. (setq seq (copy-sequence seq))
  549. (cl-loop for i below (length seq) do
  550. (when (logand (aref seq i) 128)
  551. (setf (aref seq i) (logand (aref seq i) 127))))
  552. seq)
  553. (defun edmacro-fix-menu-commands (macro &optional noerror)
  554. (if (vectorp macro)
  555. (let (result)
  556. ;; Make a list of the elements.
  557. (setq macro (append macro nil))
  558. (dolist (ev macro)
  559. (cond ((atom ev)
  560. (push ev result))
  561. ((eq (car ev) 'help-echo))
  562. ((eq (car ev) 'switch-frame))
  563. ((equal ev '(menu-bar))
  564. (push 'menu-bar result))
  565. ((equal (cl-cadadr ev) '(menu-bar))
  566. (push (vector 'menu-bar (car ev)) result))
  567. ;; It would be nice to do pop-up menus, too, but not enough
  568. ;; info is recorded in macros to make this possible.
  569. (noerror
  570. ;; Just ignore mouse events.
  571. nil)
  572. (t
  573. (error "Macros with mouse clicks are not %s"
  574. "supported by this command"))))
  575. ;; Reverse them again and make them back into a vector.
  576. (vconcat (nreverse result)))
  577. macro))
  578. ;;; Parsing a human-readable keyboard macro.
  579. (defun edmacro-parse-keys (string &optional need-vector)
  580. (let ((case-fold-search nil)
  581. (len (length string)) ; We won't alter string in the loop below.
  582. (pos 0)
  583. (res []))
  584. (while (and (< pos len)
  585. (string-match "[^ \t\n\f]+" string pos))
  586. (let* ((word-beg (match-beginning 0))
  587. (word-end (match-end 0))
  588. (word (substring string word-beg len))
  589. (times 1)
  590. key)
  591. ;; Try to catch events of the form "<as df>".
  592. (if (string-match "\\`<[^ <>\t\n\f][^>\t\n\f]*>" word)
  593. (setq word (match-string 0 word)
  594. pos (+ word-beg (match-end 0)))
  595. (setq word (substring string word-beg word-end)
  596. pos word-end))
  597. (when (string-match "\\([0-9]+\\)\\*." word)
  598. (setq times (string-to-number (substring word 0 (match-end 1))))
  599. (setq word (substring word (1+ (match-end 1)))))
  600. (cond ((string-match "^<<.+>>$" word)
  601. (setq key (vconcat (if (eq (key-binding [?\M-x])
  602. 'execute-extended-command)
  603. [?\M-x]
  604. (or (car (where-is-internal
  605. 'execute-extended-command))
  606. [?\M-x]))
  607. (substring word 2 -2) "\r")))
  608. ((and (string-match "^\\(\\([ACHMsS]-\\)*\\)<\\(.+\\)>$" word)
  609. (progn
  610. (setq word (concat (substring word (match-beginning 1)
  611. (match-end 1))
  612. (substring word (match-beginning 3)
  613. (match-end 3))))
  614. (not (string-match
  615. "\\<\\(NUL\\|RET\\|LFD\\|ESC\\|SPC\\|DEL\\)$"
  616. word))))
  617. (setq key (list (intern word))))
  618. ((or (equal word "REM") (string-match "^;;" word))
  619. (setq pos (string-match "$" string pos)))
  620. (t
  621. (let ((orig-word word) (prefix 0) (bits 0))
  622. (while (string-match "^[ACHMsS]-." word)
  623. (cl-incf bits (cdr (assq (aref word 0)
  624. '((?A . ?\A-\^@) (?C . ?\C-\^@)
  625. (?H . ?\H-\^@) (?M . ?\M-\^@)
  626. (?s . ?\s-\^@) (?S . ?\S-\^@)))))
  627. (cl-incf prefix 2)
  628. (cl-callf substring word 2))
  629. (when (string-match "^\\^.$" word)
  630. (cl-incf bits ?\C-\^@)
  631. (cl-incf prefix)
  632. (cl-callf substring word 1))
  633. (let ((found (assoc word '(("NUL" . "\0") ("RET" . "\r")
  634. ("LFD" . "\n") ("TAB" . "\t")
  635. ("ESC" . "\e") ("SPC" . " ")
  636. ("DEL" . "\177")))))
  637. (when found (setq word (cdr found))))
  638. (when (string-match "^\\\\[0-7]+$" word)
  639. (cl-loop for ch across word
  640. for n = 0 then (+ (* n 8) ch -48)
  641. finally do (setq word (vector n))))
  642. (cond ((= bits 0)
  643. (setq key word))
  644. ((and (= bits ?\M-\^@) (stringp word)
  645. (string-match "^-?[0-9]+$" word))
  646. (setq key (cl-loop for x across word
  647. collect (+ x bits))))
  648. ((/= (length word) 1)
  649. (error "%s must prefix a single character, not %s"
  650. (substring orig-word 0 prefix) word))
  651. ((and (/= (logand bits ?\C-\^@) 0) (stringp word)
  652. ;; We used to accept . and ? here,
  653. ;; but . is simply wrong,
  654. ;; and C-? is not used (we use DEL instead).
  655. (string-match "[@-_a-z]" word))
  656. (setq key (list (+ bits (- ?\C-\^@)
  657. (logand (aref word 0) 31)))))
  658. (t
  659. (setq key (list (+ bits (aref word 0)))))))))
  660. (when key
  661. (cl-loop repeat times do (cl-callf vconcat res key)))))
  662. (when (and (>= (length res) 4)
  663. (eq (aref res 0) ?\C-x)
  664. (eq (aref res 1) ?\()
  665. (eq (aref res (- (length res) 2)) ?\C-x)
  666. (eq (aref res (- (length res) 1)) ?\)))
  667. (setq res (cl-subseq res 2 -2)))
  668. (if (and (not need-vector)
  669. (cl-loop for ch across res
  670. always (and (characterp ch)
  671. (let ((ch2 (logand ch (lognot ?\M-\^@))))
  672. (and (>= ch2 0) (<= ch2 127))))))
  673. (concat (cl-loop for ch across res
  674. collect (if (= (logand ch ?\M-\^@) 0)
  675. ch (+ ch 128))))
  676. res)))
  677. (provide 'edmacro)
  678. ;;; edmacro.el ends here