viper-macs.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. ;;; viper-macs.el --- functions implementing keyboard macros for Viper
  2. ;; Copyright (C) 1994-1997, 2000-2015 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Package: viper
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (provide 'viper-macs)
  19. ;; compiler pacifier
  20. (defvar viper-ex-work-buf)
  21. (defvar viper-custom-file-name)
  22. (defvar viper-current-state)
  23. (defvar viper-fast-keyseq-timeout)
  24. (require 'viper-mous)
  25. (require 'viper-ex)
  26. ;; end pacifier
  27. (require 'viper-util)
  28. (require 'viper-keym)
  29. ;;; Variables
  30. ;; Register holding last macro.
  31. (defvar viper-last-macro-reg nil)
  32. ;; format of the elements of kbd alists:
  33. ;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr))
  34. ;; kbd macro alist for Vi state
  35. (defvar viper-vi-kbd-macro-alist nil)
  36. ;; same for insert/replace state
  37. (defvar viper-insert-kbd-macro-alist nil)
  38. ;; same for emacs state
  39. (defvar viper-emacs-kbd-macro-alist nil)
  40. ;; Internal var that passes info between start-kbd-macro and end-kbd-macro
  41. ;; in :map and :map!
  42. (defvar viper-kbd-macro-parameters nil)
  43. (defvar viper-this-kbd-macro nil
  44. "Vector of keys representing the name of currently running Viper kbd macro.")
  45. (defvar viper-last-kbd-macro nil
  46. "Vector of keys representing the name of last Viper keyboard macro.")
  47. (defcustom viper-repeat-from-history-key 'f12
  48. "Prefix key for accessing previously typed Vi commands.
  49. The previous command is accessible, as usual, via `.'. The command before this
  50. can be invoked as `<this key> 1', and the command before that, and the command
  51. before that one is accessible as `<this key> 2'.
  52. The notation for these keys is borrowed from XEmacs. Basically,
  53. a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
  54. `(meta control f1)'."
  55. :type 'sexp
  56. :group 'viper)
  57. ;;; Code
  58. (declare-function viper-change-state-to-insert "viper-cmd" ())
  59. ;; Ex map command
  60. (defun ex-map ()
  61. (let ((mod-char "")
  62. macro-name macro-body map-args ins)
  63. (save-window-excursion
  64. (set-buffer viper-ex-work-buf)
  65. (if (looking-at "!")
  66. (progn
  67. (setq ins t
  68. mod-char "!")
  69. (forward-char 1))))
  70. (setq map-args (ex-map-read-args mod-char)
  71. macro-name (car map-args)
  72. macro-body (cdr map-args))
  73. (setq viper-kbd-macro-parameters (list ins mod-char macro-name macro-body))
  74. (if macro-body
  75. (viper-end-mapping-kbd-macro 'ignore)
  76. (ex-fixup-history (format "map%s %S" mod-char
  77. (viper-display-macro macro-name)))
  78. ;; if defining macro for insert, switch there for authentic WYSIWYG
  79. (if ins (viper-change-state-to-insert))
  80. (start-kbd-macro nil)
  81. (define-key viper-vi-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
  82. (define-key viper-insert-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
  83. (define-key viper-emacs-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
  84. (message "Mapping %S in %s state. Type macro definition followed by `C-x )'"
  85. (viper-display-macro macro-name)
  86. (if ins "Insert" "Vi")))
  87. ))
  88. ;; Ex unmap
  89. (defun ex-unmap ()
  90. (let ((mod-char "")
  91. temp macro-name ins)
  92. (save-window-excursion
  93. (set-buffer viper-ex-work-buf)
  94. (if (looking-at "!")
  95. (progn
  96. (setq ins t
  97. mod-char "!")
  98. (forward-char 1))))
  99. (setq macro-name (ex-unmap-read-args mod-char))
  100. (setq temp (viper-fixup-macro (vconcat macro-name))) ;; copy and fixup
  101. (ex-fixup-history (format "unmap%s %S" mod-char
  102. (viper-display-macro temp)))
  103. (viper-unrecord-kbd-macro macro-name (if ins 'insert-state 'vi-state))
  104. ))
  105. ;; read arguments for ex-map
  106. (defun ex-map-read-args (variant)
  107. (let ((cursor-in-echo-area t)
  108. (key-seq [])
  109. temp key event message
  110. macro-name macro-body args)
  111. (condition-case nil
  112. (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
  113. " nil nil ")
  114. temp (read-from-string args)
  115. macro-name (car temp)
  116. macro-body (car (read-from-string args (cdr temp))))
  117. (error
  118. (signal
  119. 'error
  120. '("map: Macro name and body must be a quoted string or a vector"))))
  121. ;; We expect macro-name to be a vector, a string, or a quoted string.
  122. ;; In the second case, it will emerge as a symbol when read from
  123. ;; the above read-from-string. So we need to convert it into a string
  124. (if macro-name
  125. (cond ((vectorp macro-name) nil)
  126. ((stringp macro-name)
  127. (setq macro-name (vconcat macro-name)))
  128. (t (setq macro-name (vconcat (prin1-to-string macro-name)))))
  129. (message ":map%s <Macro Name>" variant)(sit-for 2)
  130. (while
  131. (not (member key
  132. '(?\C-m ?\n (control m) (control j) return linefeed)))
  133. (setq key-seq (vconcat key-seq (if key (vector key) [])))
  134. ;; the only keys available for editing are these-- no help while there
  135. (if (member
  136. key
  137. '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
  138. (setq key-seq (viper-subseq key-seq 0 (- (length key-seq) 2))))
  139. (setq message
  140. (format
  141. ":map%s %s"
  142. variant (if (> (length key-seq) 0)
  143. (prin1-to-string (viper-display-macro key-seq))
  144. "")))
  145. (message "%s" message)
  146. (setq event (viper-read-key))
  147. ;;(setq event (viper-read-event))
  148. (setq key
  149. (if (viper-mouse-event-p event)
  150. (progn
  151. (message "%s (No mouse---only keyboard keys, please)"
  152. message)
  153. (sit-for 2)
  154. nil)
  155. (viper-event-key event)))
  156. )
  157. (setq macro-name key-seq))
  158. (if (= (length macro-name) 0)
  159. (error "Can't map an empty macro name"))
  160. (setq macro-name (viper-fixup-macro macro-name))
  161. (if (viper-char-array-p macro-name)
  162. (setq macro-name (viper-char-array-to-macro macro-name)))
  163. (if macro-body
  164. (cond ((viper-char-array-p macro-body)
  165. (setq macro-body (viper-char-array-to-macro macro-body)))
  166. ((vectorp macro-body) nil)
  167. (t (error "map: Invalid syntax in macro definition"))))
  168. (setq cursor-in-echo-area nil)(sit-for 0) ; this overcomes xemacs tty bug
  169. (cons macro-name macro-body)))
  170. ;; read arguments for ex-unmap
  171. (defun ex-unmap-read-args (variant)
  172. (let ((cursor-in-echo-area t)
  173. (macro-alist (if (string= variant "!")
  174. viper-insert-kbd-macro-alist
  175. viper-vi-kbd-macro-alist))
  176. ;; these are disabled just in case, to avoid surprises when doing
  177. ;; completing-read
  178. viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode
  179. viper-emacs-kbd-minor-mode
  180. viper-vi-intercept-minor-mode viper-insert-intercept-minor-mode
  181. viper-emacs-intercept-minor-mode
  182. event message
  183. key key-seq macro-name)
  184. (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*"))
  185. (if (> (length macro-name) 0)
  186. ()
  187. (message ":unmap%s <Name>" variant) (sit-for 2)
  188. (while
  189. (not
  190. (member key '(?\C-m ?\n (control m) (control j) return linefeed)))
  191. (setq key-seq (vconcat key-seq (if key (vector key) [])))
  192. ;; the only keys available for editing are these-- no help while there
  193. (cond ((member
  194. key
  195. '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
  196. (setq key-seq (viper-subseq key-seq 0 (- (length key-seq) 2))))
  197. ((member key '(tab (control i) ?\t))
  198. (setq key-seq (viper-subseq key-seq 0 (1- (length key-seq))))
  199. (setq message
  200. (format
  201. ":unmap%s %s"
  202. variant (if (> (length key-seq) 0)
  203. (prin1-to-string
  204. (viper-display-macro key-seq))
  205. "")))
  206. (setq key-seq
  207. (viper-do-sequence-completion key-seq macro-alist message))
  208. ))
  209. (setq message
  210. (format
  211. ":unmap%s %s"
  212. variant (if (> (length key-seq) 0)
  213. (prin1-to-string
  214. (viper-display-macro key-seq))
  215. "")))
  216. (message "%s" message)
  217. (setq event (viper-read-key))
  218. ;;(setq event (viper-read-event))
  219. (setq key
  220. (if (viper-mouse-event-p event)
  221. (progn
  222. (message "%s (No mouse---only keyboard keys, please)"
  223. message)
  224. (sit-for 2)
  225. nil)
  226. (viper-event-key event)))
  227. )
  228. (setq macro-name key-seq))
  229. (if (= (length macro-name) 0)
  230. (error "Can't unmap an empty macro name"))
  231. ;; convert macro names into vector, if starts with a `['
  232. (if (memq (elt macro-name 0) '(?\[ ?\"))
  233. (car (read-from-string macro-name))
  234. (vconcat macro-name))
  235. ))
  236. (declare-function viper-change-state-to-vi "viper-cmd" ())
  237. ;; Terminate a Vi kbd macro.
  238. ;; optional argument IGNORE, if t, indicates that we are dealing with an
  239. ;; existing macro that needs to be registered, but there is no need to
  240. ;; terminate a kbd macro.
  241. (defun viper-end-mapping-kbd-macro (&optional ignore)
  242. (interactive)
  243. (define-key viper-vi-intercept-map "\C-x)" nil)
  244. (define-key viper-insert-intercept-map "\C-x)" nil)
  245. (define-key viper-emacs-intercept-map "\C-x)" nil)
  246. (if (and (not ignore)
  247. (or (not viper-kbd-macro-parameters)
  248. (not defining-kbd-macro)))
  249. (error "Not mapping a kbd-macro"))
  250. (let ((mod-char (nth 1 viper-kbd-macro-parameters))
  251. (ins (nth 0 viper-kbd-macro-parameters))
  252. (macro-name (nth 2 viper-kbd-macro-parameters))
  253. (macro-body (nth 3 viper-kbd-macro-parameters)))
  254. (setq viper-kbd-macro-parameters nil)
  255. (or ignore
  256. (progn
  257. (end-kbd-macro nil)
  258. (setq macro-body (viper-events-to-macro last-kbd-macro))
  259. ;; always go back to Vi, since this is where we started
  260. ;; defining macro
  261. (viper-change-state-to-vi)))
  262. (viper-record-kbd-macro macro-name
  263. (if ins 'insert-state 'vi-state)
  264. (viper-display-macro macro-body))
  265. (ex-fixup-history (format "map%s %S %S" mod-char
  266. (viper-display-macro macro-name)
  267. (viper-display-macro macro-body)))
  268. ))
  269. ;;; Recording, unrecording, executing
  270. ;; Accepts as macro names: strings and vectors.
  271. ;; strings must be strings of characters; vectors must be vectors of keys
  272. ;; in canonical form, which is essentially the form used in XEmacs.
  273. ;; More general definitions are inherited by more specific scopes:
  274. ;; global->major mode->buffer. More specific definitions override more general
  275. (defun viper-record-kbd-macro (macro-name state macro-body &optional scope)
  276. "Record a Vi macro.
  277. Can be used in `viper-custom-file-name' to define permanent macros.
  278. MACRO-NAME is a string of characters or a vector of keys. STATE is
  279. either `vi-state' or `insert-state'. It specifies the Viper state in which to
  280. define the macro. MACRO-BODY is a string that represents the keyboard macro.
  281. Optional SCOPE says whether the macro should be global \(t), mode-specific
  282. \(a major-mode symbol), or buffer-specific \(buffer name, a string).
  283. If SCOPE is nil, the user is asked to specify the scope."
  284. (let* (state-name keymap
  285. (macro-alist-var
  286. (cond ((eq state 'vi-state)
  287. (setq state-name "Vi state"
  288. keymap viper-vi-kbd-map)
  289. 'viper-vi-kbd-macro-alist)
  290. ((memq state '(insert-state replace-state))
  291. (setq state-name "Insert state"
  292. keymap viper-insert-kbd-map)
  293. 'viper-insert-kbd-macro-alist)
  294. (t
  295. (setq state-name "Emacs state"
  296. keymap viper-emacs-kbd-map)
  297. 'viper-emacs-kbd-macro-alist)
  298. ))
  299. new-elt old-elt old-sub-elt msg
  300. temp lis lis2)
  301. (if (= (length macro-name) 0)
  302. (error "Can't map an empty macro name"))
  303. ;; Macro-name is usually a vector. However, command history or macros
  304. ;; recorded in viper-custom-file-name may be recorded as strings.
  305. ;; So, convert to vectors.
  306. (setq macro-name (viper-fixup-macro macro-name))
  307. (if (viper-char-array-p macro-name)
  308. (setq macro-name (viper-char-array-to-macro macro-name)))
  309. (setq macro-body (viper-fixup-macro macro-body))
  310. (if (viper-char-array-p macro-body)
  311. (setq macro-body (viper-char-array-to-macro macro-body)))
  312. ;; don't ask if scope is given and is of the right type
  313. (or (eq scope t)
  314. (stringp scope)
  315. (and scope (symbolp scope))
  316. (progn
  317. (setq scope
  318. (cond
  319. ((y-or-n-p
  320. (format-message
  321. "Map this macro for buffer `%s' only? "
  322. (buffer-name)))
  323. (setq msg
  324. (format-message
  325. "%S is mapped to %s for %s in `%s'"
  326. (viper-display-macro macro-name)
  327. (viper-abbreviate-string
  328. (format
  329. "%S"
  330. (setq temp (viper-display-macro macro-body)))
  331. 14 "" ""
  332. (if (stringp temp) " ....\"" " ....]"))
  333. state-name (buffer-name)))
  334. (buffer-name))
  335. ((y-or-n-p
  336. (format-message
  337. "Map this macro for the major mode `%S' only? "
  338. major-mode))
  339. (setq msg
  340. (format-message
  341. "%S is mapped to %s for %s in `%S'"
  342. (viper-display-macro macro-name)
  343. (viper-abbreviate-string
  344. (format
  345. "%S"
  346. (setq temp (viper-display-macro macro-body)))
  347. 14 "" ""
  348. (if (stringp macro-body) " ....\"" " ....]"))
  349. state-name major-mode))
  350. major-mode)
  351. (t
  352. (setq msg
  353. (format
  354. "%S is globally mapped to %s in %s"
  355. (viper-display-macro macro-name)
  356. (viper-abbreviate-string
  357. (format
  358. "%S"
  359. (setq temp (viper-display-macro macro-body)))
  360. 14 "" ""
  361. (if (stringp macro-body) " ....\"" " ....]"))
  362. state-name))
  363. t)))
  364. (if (y-or-n-p
  365. (format "Save this macro in %s? "
  366. (viper-abbreviate-file-name viper-custom-file-name)))
  367. (viper-save-string-in-file
  368. (format "\n(viper-record-kbd-macro %S '%S %s '%S)"
  369. (viper-display-macro macro-name)
  370. state
  371. ;; if we don't let vector macro-body through %S,
  372. ;; the symbols `\.' `\[' etc will be converted into
  373. ;; characters, causing invalid read error on recorded
  374. ;; macros in viper-custom-file-name.
  375. ;; I am not sure is macro-body can still be a string at
  376. ;; this point, but I am preserving this option anyway.
  377. (if (vectorp macro-body)
  378. (format "%S" macro-body)
  379. macro-body)
  380. scope)
  381. viper-custom-file-name))
  382. (message "%s" msg)
  383. ))
  384. (setq new-elt
  385. (cons macro-name
  386. (cond ((eq scope t) (list nil nil (cons t nil)))
  387. ((symbolp scope)
  388. (list nil (list (cons scope nil)) (cons t nil)))
  389. ((stringp scope)
  390. (list (list (cons scope nil)) nil (cons t nil))))))
  391. (setq old-elt (assoc macro-name (eval macro-alist-var)))
  392. (if (null old-elt)
  393. (progn
  394. ;; insert new-elt in macro-alist-var and keep the list sorted
  395. (define-key
  396. keymap
  397. (vector (viper-key-to-emacs-key (aref macro-name 0)))
  398. 'viper-exec-mapped-kbd-macro)
  399. (setq lis (eval macro-alist-var))
  400. (while (and lis (string< (viper-array-to-string (car (car lis)))
  401. (viper-array-to-string macro-name)))
  402. (setq lis2 (cons (car lis) lis2))
  403. (setq lis (cdr lis)))
  404. (setq lis2 (reverse lis2))
  405. (set macro-alist-var (append lis2 (cons new-elt lis)))
  406. (setq old-elt new-elt)))
  407. (setq old-sub-elt
  408. (cond ((eq scope t) (viper-kbd-global-pair old-elt))
  409. ((symbolp scope) (assoc scope (viper-kbd-mode-alist old-elt)))
  410. ((stringp scope) (assoc scope (viper-kbd-buf-alist old-elt)))))
  411. (if old-sub-elt
  412. (setcdr old-sub-elt macro-body)
  413. (cond ((symbolp scope) (setcar (cdr (cdr old-elt))
  414. (cons (cons scope macro-body)
  415. (viper-kbd-mode-alist old-elt))))
  416. ((stringp scope) (setcar (cdr old-elt)
  417. (cons (cons scope macro-body)
  418. (viper-kbd-buf-alist old-elt))))))
  419. ))
  420. ;; macro name must be a vector of viper-style keys
  421. ;; viper-unrecord-kbd-macro doesn't have scope. Macro definitions are inherited
  422. ;; from global -> major mode -> buffer
  423. ;; More specific definition overrides more general
  424. ;; Can't unrecord definition for more specific, if a more general definition is
  425. ;; in effect
  426. (defun viper-unrecord-kbd-macro (macro-name state)
  427. "Delete macro MACRO-NAME from Viper STATE.
  428. MACRO-NAME must be a vector of viper-style keys. This command is used
  429. by Viper internally, but you can also use it in `viper-custom-file-name'
  430. to delete pre-defined macros supplied with Viper. The best way to avoid
  431. mistakes in macro names to be passed to this function is to use
  432. `viper-describe-kbd-macros' and copy the name from there."
  433. (let* (state-name keymap
  434. (macro-alist-var
  435. (cond ((eq state 'vi-state)
  436. (setq state-name "Vi state"
  437. keymap viper-vi-kbd-map)
  438. 'viper-vi-kbd-macro-alist)
  439. ((memq state '(insert-state replace-state))
  440. (setq state-name "Insert state"
  441. keymap viper-insert-kbd-map)
  442. 'viper-insert-kbd-macro-alist)
  443. (t
  444. (setq state-name "Emacs state"
  445. keymap viper-emacs-kbd-map)
  446. 'viper-emacs-kbd-macro-alist)
  447. ))
  448. buf-mapping mode-mapping global-mapping
  449. macro-pair macro-entry)
  450. ;; Macro-name is usually a vector. However, command history or macros
  451. ;; recorded in viper-custom-file-name may appear as strings.
  452. ;; So, convert to vectors.
  453. (setq macro-name (viper-fixup-macro macro-name))
  454. (if (viper-char-array-p macro-name)
  455. (setq macro-name (viper-char-array-to-macro macro-name)))
  456. (setq macro-entry (assoc macro-name (eval macro-alist-var)))
  457. (if (= (length macro-name) 0)
  458. (error "Can't unmap an empty macro name"))
  459. (if (null macro-entry)
  460. (error "%S is not mapped to a macro for %s in `%s'"
  461. (viper-display-macro macro-name)
  462. state-name (buffer-name)))
  463. (setq buf-mapping (viper-kbd-buf-pair macro-entry)
  464. mode-mapping (viper-kbd-mode-pair macro-entry)
  465. global-mapping (viper-kbd-global-pair macro-entry))
  466. (cond ((and (cdr buf-mapping)
  467. (or (and (not (cdr mode-mapping)) (not (cdr global-mapping)))
  468. (y-or-n-p
  469. (format-message "Unmap %S for `%s' only? "
  470. (viper-display-macro macro-name)
  471. (buffer-name)))))
  472. (setq macro-pair buf-mapping)
  473. (message "%S is unmapped for %s in `%s'"
  474. (viper-display-macro macro-name)
  475. state-name (buffer-name)))
  476. ((and (cdr mode-mapping)
  477. (or (not (cdr global-mapping))
  478. (y-or-n-p
  479. (format-message "Unmap %S for the major mode `%S' only? "
  480. (viper-display-macro macro-name)
  481. major-mode))))
  482. (setq macro-pair mode-mapping)
  483. (message "%S is unmapped for %s in %S"
  484. (viper-display-macro macro-name) state-name major-mode))
  485. ((cdr (setq macro-pair global-mapping))
  486. (message
  487. "Global mapping for %S in %s is removed"
  488. (viper-display-macro macro-name) state-name))
  489. (t (error "%S is not mapped to a macro for %s in `%s'"
  490. (viper-display-macro macro-name)
  491. state-name (buffer-name))))
  492. (setcdr macro-pair nil)
  493. (or (cdr buf-mapping)
  494. (cdr mode-mapping)
  495. (cdr global-mapping)
  496. (progn
  497. (set macro-alist-var (delq macro-entry (eval macro-alist-var)))
  498. (if (viper-can-release-key (aref macro-name 0)
  499. (eval macro-alist-var))
  500. (define-key
  501. keymap
  502. (vector (viper-key-to-emacs-key (aref macro-name 0)))
  503. nil))
  504. ))
  505. ))
  506. ;; Check if MACRO-ALIST has an entry for a macro name starting with
  507. ;; CHAR. If not, this indicates that the binding for this char
  508. ;; in viper-vi/insert-kbd-map can be released.
  509. (defun viper-can-release-key (char macro-alist)
  510. (let ((lis macro-alist)
  511. (can-release t)
  512. macro-name)
  513. (while (and lis can-release)
  514. (setq macro-name (car (car lis)))
  515. (if (eq char (aref macro-name 0))
  516. (setq can-release nil))
  517. (setq lis (cdr lis)))
  518. can-release))
  519. (defun viper-exec-mapped-kbd-macro (count)
  520. "Dispatch kbd macro."
  521. (interactive "P")
  522. (let* ((macro-alist (cond ((eq viper-current-state 'vi-state)
  523. viper-vi-kbd-macro-alist)
  524. ((memq viper-current-state
  525. '(insert-state replace-state))
  526. viper-insert-kbd-macro-alist)
  527. (t
  528. viper-emacs-kbd-macro-alist)))
  529. (unmatched-suffix "")
  530. ;; Macros and keys are executed with other macros turned off
  531. ;; For macros, this is done to avoid macro recursion
  532. viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode
  533. viper-emacs-kbd-minor-mode
  534. next-best-match keyseq event-seq
  535. macro-first-char macro-alist-elt macro-body
  536. command)
  537. (setq macro-first-char last-command-event
  538. event-seq (viper-read-fast-keysequence macro-first-char macro-alist)
  539. keyseq (viper-events-to-macro event-seq)
  540. macro-alist-elt (assoc keyseq macro-alist)
  541. next-best-match (viper-find-best-matching-macro macro-alist keyseq))
  542. (if (null macro-alist-elt)
  543. (setq macro-alist-elt (car next-best-match)
  544. unmatched-suffix (viper-subseq event-seq (cdr next-best-match))))
  545. (cond ((null macro-alist-elt))
  546. ((setq macro-body (viper-kbd-buf-definition macro-alist-elt)))
  547. ((setq macro-body (viper-kbd-mode-definition macro-alist-elt)))
  548. ((setq macro-body (viper-kbd-global-definition macro-alist-elt))))
  549. ;; when defining keyboard macro, don't use the macro mappings
  550. (if (and macro-body (not defining-kbd-macro))
  551. ;; block cmd executed as part of a macro from entering command history
  552. (let ((command-history command-history))
  553. (setq viper-this-kbd-macro (car macro-alist-elt))
  554. (execute-kbd-macro (viper-macro-to-events macro-body) count)
  555. (setq viper-this-kbd-macro nil
  556. viper-last-kbd-macro (car macro-alist-elt))
  557. (viper-set-unread-command-events unmatched-suffix))
  558. ;; If not a macro, or the macro is suppressed while defining another
  559. ;; macro, put keyseq back on the event queue
  560. (viper-set-unread-command-events event-seq)
  561. ;; if the user typed arg, then use it if prefix arg is not set by
  562. ;; some other command (setting prefix arg can happen if we do, say,
  563. ;; 2dw and there is a macro starting with 2. Then control will go to
  564. ;; this routine
  565. (or prefix-arg (setq prefix-arg count))
  566. (setq command (key-binding (read-key-sequence nil)))
  567. (if (commandp command)
  568. (command-execute command)
  569. (beep 1)))
  570. ))
  571. ;;; Displaying and completing macros
  572. (defun viper-describe-kbd-macros ()
  573. "Show currently defined keyboard macros."
  574. (interactive)
  575. (with-output-to-temp-buffer " *viper-info*"
  576. (princ "Macros in Vi state:\n===================\n")
  577. (mapc 'viper-describe-one-macro viper-vi-kbd-macro-alist)
  578. (princ "\n\nMacros in Insert and Replace states:\n====================================\n")
  579. (mapc 'viper-describe-one-macro viper-insert-kbd-macro-alist)
  580. (princ "\n\nMacros in Emacs state:\n======================\n")
  581. (mapcar 'viper-describe-one-macro viper-emacs-kbd-macro-alist)
  582. ))
  583. (defun viper-describe-one-macro (macro)
  584. (princ (format "\n *** Mappings for %S:\n ------------\n"
  585. (viper-display-macro (car macro))))
  586. (princ " ** Buffer-specific:")
  587. (if (viper-kbd-buf-alist macro)
  588. (mapc 'viper-describe-one-macro-elt (viper-kbd-buf-alist macro))
  589. (princ " none\n"))
  590. (princ "\n ** Mode-specific:")
  591. (if (viper-kbd-mode-alist macro)
  592. (mapc 'viper-describe-one-macro-elt (viper-kbd-mode-alist macro))
  593. (princ " none\n"))
  594. (princ "\n ** Global:")
  595. (if (viper-kbd-global-definition macro)
  596. (princ (format "\n %S" (cdr (viper-kbd-global-pair macro))))
  597. (princ " none"))
  598. (princ "\n"))
  599. (defun viper-describe-one-macro-elt (elt)
  600. (let ((name (car elt))
  601. (defn (cdr elt)))
  602. (princ (format "\n * %S:\n %S\n" name defn))))
  603. ;; check if SEQ is a prefix of some car of an element in ALIST
  604. (defun viper-keyseq-is-a-possible-macro (seq alist)
  605. (let ((converted-seq (viper-events-to-macro seq)))
  606. (eval (cons 'or
  607. (mapcar
  608. (lambda (elt) (viper-prefix-subseq-p converted-seq elt))
  609. (viper-this-buffer-macros alist))))))
  610. ;; whether SEQ1 is a prefix of SEQ2
  611. (defun viper-prefix-subseq-p (seq1 seq2)
  612. (let ((len1 (length seq1))
  613. (len2 (length seq2)))
  614. (if (<= len1 len2)
  615. (equal seq1 (viper-subseq seq2 0 len1)))))
  616. ;; find the longest common prefix
  617. (defun viper-common-seq-prefix (&rest seqs)
  618. (let* ((first (car seqs))
  619. (rest (cdr seqs))
  620. (pref [])
  621. (idx 0)
  622. len)
  623. (if (= (length seqs) 0)
  624. (setq len 0)
  625. (setq len (apply 'min (mapcar 'length seqs))))
  626. (while (< idx len)
  627. (if (eval (cons 'and
  628. (mapcar (lambda (s) (equal (elt first idx) (elt s idx)))
  629. rest)))
  630. (setq pref (vconcat pref (vector (elt first idx)))))
  631. (setq idx (1+ idx)))
  632. pref))
  633. ;; get all sequences that match PREFIX from a given A-LIST
  634. (defun viper-extract-matching-alist-members (pref alist)
  635. (delq nil (mapcar (lambda (elt) (if (viper-prefix-subseq-p pref elt) elt))
  636. (viper-this-buffer-macros alist))))
  637. (defun viper-do-sequence-completion (seq alist compl-message)
  638. (let* ((matches (viper-extract-matching-alist-members seq alist))
  639. (new-seq (apply 'viper-common-seq-prefix matches))
  640. )
  641. (cond ((and (equal seq new-seq) (= (length matches) 1))
  642. (message "%s (Sole completion)" compl-message)
  643. (sit-for 2))
  644. ((null matches)
  645. (message "%s (No match)" compl-message)
  646. (sit-for 2)
  647. (setq new-seq seq))
  648. ((member seq matches)
  649. (message "%s (Complete, but not unique)" compl-message)
  650. (sit-for 2)
  651. (viper-display-vector-completions matches))
  652. ((equal seq new-seq)
  653. (viper-display-vector-completions matches)))
  654. new-seq))
  655. (defun viper-display-vector-completions (list)
  656. (with-output-to-temp-buffer "*Completions*"
  657. (display-completion-list
  658. (mapcar 'prin1-to-string
  659. (mapcar 'viper-display-macro list)))))
  660. ;; alist is the alist of macros
  661. ;; str is the fast key sequence entered
  662. ;; returns: (matching-macro-def . unmatched-suffix-start-index)
  663. (defun viper-find-best-matching-macro (alist str)
  664. (let ((lis alist)
  665. (def-len 0)
  666. (str-len (length str))
  667. match unmatched-start-idx found macro-def)
  668. (while (and (not found) lis)
  669. (setq macro-def (car lis)
  670. def-len (length (car macro-def)))
  671. (if (and (>= str-len def-len)
  672. (equal (car macro-def) (viper-subseq str 0 def-len)))
  673. (if (or (viper-kbd-buf-definition macro-def)
  674. (viper-kbd-mode-definition macro-def)
  675. (viper-kbd-global-definition macro-def))
  676. (setq found t))
  677. )
  678. (setq lis (cdr lis)))
  679. (if found
  680. (setq match macro-def
  681. unmatched-start-idx def-len)
  682. (setq match nil
  683. unmatched-start-idx 0))
  684. (cons match unmatched-start-idx)))
  685. ;; returns a list of names of macros defined for the current buffer
  686. (defun viper-this-buffer-macros (macro-alist)
  687. (let (candidates)
  688. (setq candidates
  689. (mapcar (lambda (elt)
  690. (if (or (viper-kbd-buf-definition elt)
  691. (viper-kbd-mode-definition elt)
  692. (viper-kbd-global-definition elt))
  693. (car elt)))
  694. macro-alist))
  695. (setq candidates (delq nil candidates))))
  696. ;; if seq of Viper key symbols (representing a macro) can be converted to a
  697. ;; string--do so. Otherwise, do nothing.
  698. (defun viper-display-macro (macro-name-or-body)
  699. (cond ((viper-char-symbol-sequence-p macro-name-or-body)
  700. (mapconcat 'symbol-name macro-name-or-body ""))
  701. ((viper-char-array-p macro-name-or-body)
  702. (mapconcat 'char-to-string macro-name-or-body ""))
  703. (t macro-name-or-body)))
  704. ;; convert sequence of events (that came presumably from emacs kbd macro) into
  705. ;; Viper's macro, which is a vector of the form
  706. ;; [ desc desc ... ]
  707. ;; Each desc is either a symbol of (meta symb), (shift symb), etc.
  708. ;; Here we purge events that happen to be lists. In most cases, these events
  709. ;; got into a macro definition unintentionally; say, when the user moves mouse
  710. ;; during a macro definition, then something like (switch-frame ...) might get
  711. ;; in. Another reason for purging lists-events is that we can't store them in
  712. ;; textual form (say, in .emacs) and then read them back.
  713. (defun viper-events-to-macro (event-seq)
  714. (vconcat (delq nil (mapcar (lambda (elt) (if (consp elt)
  715. nil
  716. (viper-event-key elt)))
  717. event-seq))))
  718. ;; convert strings or arrays of characters to Viper macro form
  719. (defun viper-char-array-to-macro (array)
  720. (let ((vec (vconcat array))
  721. macro)
  722. (if (featurep 'xemacs)
  723. (setq macro (mapcar 'character-to-event vec))
  724. (setq macro vec))
  725. (vconcat (mapcar 'viper-event-key macro))))
  726. ;; For macros bodies and names, goes over MACRO and checks if all members are
  727. ;; names of keys (actually, it only checks if they are symbols or lists
  728. ;; if a digit is found, it is converted into a symbol (e.g., 0 -> \0, etc).
  729. ;; If MACRO is not a list or vector -- doesn't change MACRO.
  730. (defun viper-fixup-macro (macro)
  731. (let ((len (length macro))
  732. (idx 0)
  733. elt break)
  734. (if (or (vectorp macro) (listp macro))
  735. (while (and (< idx len) (not break))
  736. (setq elt (elt macro idx))
  737. (cond ((numberp elt)
  738. ;; fix number
  739. (if (and (<= 0 elt) (<= elt 9))
  740. (cond ((arrayp macro)
  741. (aset macro
  742. idx
  743. (intern (char-to-string (+ ?0 elt)))))
  744. ((listp macro)
  745. (setcar (nthcdr idx macro)
  746. (intern (char-to-string (+ ?0 elt)))))
  747. )))
  748. ((listp elt)
  749. (viper-fixup-macro elt))
  750. ((symbolp elt) nil)
  751. (t (setq break t)))
  752. (setq idx (1+ idx))))
  753. (if break
  754. (error "Wrong type macro component, symbol-or-listp, %S" elt)
  755. macro)))
  756. (defun viper-macro-to-events (macro-body)
  757. (vconcat (mapcar 'viper-key-to-emacs-key macro-body)))
  758. ;;; Reading fast key sequences
  759. ;; Assuming that CHAR was the first character in a fast succession of key
  760. ;; strokes, read the rest. Return the vector of keys that was entered in
  761. ;; this fast succession of key strokes.
  762. ;; A fast keysequence is one that is terminated by a pause longer than
  763. ;; viper-fast-keyseq-timeout.
  764. (defun viper-read-fast-keysequence (event macro-alist)
  765. (let ((lis (vector event))
  766. next-event)
  767. (while (and (viper-fast-keysequence-p)
  768. (viper-keyseq-is-a-possible-macro lis macro-alist))
  769. ;; Seems that viper-read-event is more robust here. We need to be able to
  770. ;; place these events on unread-command-events list. If we use
  771. ;; viper-read-key then events will be converted to keys, and sometimes
  772. ;; (e.g., (control \[)) those keys differ from the corresponding events.
  773. ;; So, do not use (setq next-event (viper-read-key))
  774. (setq next-event (viper-read-event))
  775. (or (viper-mouse-event-p next-event)
  776. (setq lis (vconcat lis (vector next-event)))))
  777. lis))
  778. ;;; Keyboard macros in registers
  779. ;; sets register to last-kbd-macro carefully.
  780. (defun viper-set-register-macro (reg)
  781. (if (get-register reg)
  782. (if (y-or-n-p "Register contains data. Overwrite? ")
  783. ()
  784. (error
  785. "Macro not saved in register. Can still be invoked via `C-x e'")))
  786. (set-register reg last-kbd-macro))
  787. (defun viper-register-macro (count)
  788. "Keyboard macros in registers - a modified @ command."
  789. (interactive "P")
  790. (let ((reg (downcase (read-char))))
  791. (cond ((or (and (<= ?a reg) (<= reg ?z)))
  792. (setq viper-last-macro-reg reg)
  793. (if defining-kbd-macro
  794. (progn
  795. (end-kbd-macro)
  796. (viper-set-register-macro reg))
  797. (execute-kbd-macro (get-register reg) count)))
  798. ((or (= ?@ reg) (= ?\^j reg) (= ?\^m reg))
  799. (if viper-last-macro-reg
  800. nil
  801. (error "No previous kbd macro"))
  802. (execute-kbd-macro (get-register viper-last-macro-reg) count))
  803. ((= ?\# reg)
  804. (start-kbd-macro count))
  805. ((= ?! reg)
  806. (setq reg (downcase (read-char)))
  807. (if (or (and (<= ?a reg) (<= reg ?z)))
  808. (progn
  809. (setq viper-last-macro-reg reg)
  810. (viper-set-register-macro reg))))
  811. (t
  812. (error "`%c': Unknown register" reg)))))
  813. (defun viper-global-execute ()
  814. "Call last keyboard macro for each line in the region."
  815. (if (> (point) (mark t)) (exchange-point-and-mark))
  816. (beginning-of-line)
  817. (call-last-kbd-macro)
  818. (while (< (point) (mark t))
  819. (forward-line 1)
  820. (beginning-of-line)
  821. (call-last-kbd-macro)))
  822. ;;; viper-macs.el ends here