viper-macs.el 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. ;;; viper-macs.el --- functions implementing keyboard macros for Viper
  2. ;; Copyright (C) 1994-1997, 2000-2012 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. ;; loading happens only in non-interactive compilation
  25. ;; in order to spare non-viperized emacs from being viperized
  26. (if noninteractive
  27. (eval-when-compile
  28. (require 'viper-cmd)
  29. ))
  30. ;; end pacifier
  31. (require 'viper-util)
  32. (require 'viper-keym)
  33. ;;; Variables
  34. ;; Register holding last macro.
  35. (defvar viper-last-macro-reg nil)
  36. ;; format of the elements of kbd alists:
  37. ;; (name ((buf . macr)...(buf . macr)) ((maj-mode . macr)...) (t . macr))
  38. ;; kbd macro alist for Vi state
  39. (defvar viper-vi-kbd-macro-alist nil)
  40. ;; same for insert/replace state
  41. (defvar viper-insert-kbd-macro-alist nil)
  42. ;; same for emacs state
  43. (defvar viper-emacs-kbd-macro-alist nil)
  44. ;; Internal var that passes info between start-kbd-macro and end-kbd-macro
  45. ;; in :map and :map!
  46. (defvar viper-kbd-macro-parameters nil)
  47. (defvar viper-this-kbd-macro nil
  48. "Vector of keys representing the name of currently running Viper kbd macro.")
  49. (defvar viper-last-kbd-macro nil
  50. "Vector of keys representing the name of last Viper keyboard macro.")
  51. (defcustom viper-repeat-from-history-key 'f12
  52. "Prefix key for accessing previously typed Vi commands.
  53. The previous command is accessible, as usual, via `.'. The command before this
  54. can be invoked as `<this key> 1', and the command before that, and the command
  55. before that one is accessible as `<this key> 2'.
  56. The notation for these keys is borrowed from XEmacs. Basically,
  57. a key is a symbol, e.g., `a', `\\1', `f2', etc., or a list, e.g.,
  58. `(meta control f1)'."
  59. :type 'sexp
  60. :group 'viper)
  61. ;;; Code
  62. ;; Ex map command
  63. (defun ex-map ()
  64. (let ((mod-char "")
  65. macro-name macro-body map-args ins)
  66. (save-window-excursion
  67. (set-buffer viper-ex-work-buf)
  68. (if (looking-at "!")
  69. (progn
  70. (setq ins t
  71. mod-char "!")
  72. (forward-char 1))))
  73. (setq map-args (ex-map-read-args mod-char)
  74. macro-name (car map-args)
  75. macro-body (cdr map-args))
  76. (setq viper-kbd-macro-parameters (list ins mod-char macro-name macro-body))
  77. (if macro-body
  78. (viper-end-mapping-kbd-macro 'ignore)
  79. (ex-fixup-history (format "map%s %S" mod-char
  80. (viper-display-macro macro-name)))
  81. ;; if defining macro for insert, switch there for authentic WYSIWYG
  82. (if ins (viper-change-state-to-insert))
  83. (start-kbd-macro nil)
  84. (define-key viper-vi-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
  85. (define-key viper-insert-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
  86. (define-key viper-emacs-intercept-map "\C-x)" 'viper-end-mapping-kbd-macro)
  87. (message "Mapping %S in %s state. Type macro definition followed by `C-x )'"
  88. (viper-display-macro macro-name)
  89. (if ins "Insert" "Vi")))
  90. ))
  91. ;; Ex unmap
  92. (defun ex-unmap ()
  93. (let ((mod-char "")
  94. temp macro-name ins)
  95. (save-window-excursion
  96. (set-buffer viper-ex-work-buf)
  97. (if (looking-at "!")
  98. (progn
  99. (setq ins t
  100. mod-char "!")
  101. (forward-char 1))))
  102. (setq macro-name (ex-unmap-read-args mod-char))
  103. (setq temp (viper-fixup-macro (vconcat macro-name))) ;; copy and fixup
  104. (ex-fixup-history (format "unmap%s %S" mod-char
  105. (viper-display-macro temp)))
  106. (viper-unrecord-kbd-macro macro-name (if ins 'insert-state 'vi-state))
  107. ))
  108. ;; read arguments for ex-map
  109. (defun ex-map-read-args (variant)
  110. (let ((cursor-in-echo-area t)
  111. (key-seq [])
  112. temp key event message
  113. macro-name macro-body args)
  114. (condition-case nil
  115. (setq args (concat (ex-get-inline-cmd-args ".*map[!]*[ \t]?" "\n\C-m")
  116. " nil nil ")
  117. temp (read-from-string args)
  118. macro-name (car temp)
  119. macro-body (car (read-from-string args (cdr temp))))
  120. (error
  121. (signal
  122. 'error
  123. '("map: Macro name and body must be a quoted string or a vector"))))
  124. ;; We expect macro-name to be a vector, a string, or a quoted string.
  125. ;; In the second case, it will emerge as a symbol when read from
  126. ;; the above read-from-string. So we need to convert it into a string
  127. (if macro-name
  128. (cond ((vectorp macro-name) nil)
  129. ((stringp macro-name)
  130. (setq macro-name (vconcat macro-name)))
  131. (t (setq macro-name (vconcat (prin1-to-string macro-name)))))
  132. (message ":map%s <Macro Name>" variant)(sit-for 2)
  133. (while
  134. (not (member key
  135. '(?\C-m ?\n (control m) (control j) return linefeed)))
  136. (setq key-seq (vconcat key-seq (if key (vector key) [])))
  137. ;; the only keys available for editing are these-- no help while there
  138. (if (member
  139. key
  140. '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
  141. (setq key-seq (viper-subseq key-seq 0 (- (length key-seq) 2))))
  142. (setq message
  143. (format
  144. ":map%s %s"
  145. variant (if (> (length key-seq) 0)
  146. (prin1-to-string (viper-display-macro key-seq))
  147. "")))
  148. (message "%s" message)
  149. (setq event (viper-read-key))
  150. ;;(setq event (viper-read-event))
  151. (setq key
  152. (if (viper-mouse-event-p event)
  153. (progn
  154. (message "%s (No mouse---only keyboard keys, please)"
  155. message)
  156. (sit-for 2)
  157. nil)
  158. (viper-event-key event)))
  159. )
  160. (setq macro-name key-seq))
  161. (if (= (length macro-name) 0)
  162. (error "Can't map an empty macro name"))
  163. (setq macro-name (viper-fixup-macro macro-name))
  164. (if (viper-char-array-p macro-name)
  165. (setq macro-name (viper-char-array-to-macro macro-name)))
  166. (if macro-body
  167. (cond ((viper-char-array-p macro-body)
  168. (setq macro-body (viper-char-array-to-macro macro-body)))
  169. ((vectorp macro-body) nil)
  170. (t (error "map: Invalid syntax in macro definition"))))
  171. (setq cursor-in-echo-area nil)(sit-for 0) ; this overcomes xemacs tty bug
  172. (cons macro-name macro-body)))
  173. ;; read arguments for ex-unmap
  174. (defun ex-unmap-read-args (variant)
  175. (let ((cursor-in-echo-area t)
  176. (macro-alist (if (string= variant "!")
  177. viper-insert-kbd-macro-alist
  178. viper-vi-kbd-macro-alist))
  179. ;; these are disabled just in case, to avoid surprises when doing
  180. ;; completing-read
  181. viper-vi-kbd-minor-mode viper-insert-kbd-minor-mode
  182. viper-emacs-kbd-minor-mode
  183. viper-vi-intercept-minor-mode viper-insert-intercept-minor-mode
  184. viper-emacs-intercept-minor-mode
  185. event message
  186. key key-seq macro-name)
  187. (setq macro-name (ex-get-inline-cmd-args ".*unma?p?[!]*[ \t]*"))
  188. (if (> (length macro-name) 0)
  189. ()
  190. (message ":unmap%s <Name>" variant) (sit-for 2)
  191. (while
  192. (not
  193. (member key '(?\C-m ?\n (control m) (control j) return linefeed)))
  194. (setq key-seq (vconcat key-seq (if key (vector key) [])))
  195. ;; the only keys available for editing are these-- no help while there
  196. (cond ((member
  197. key
  198. '(?\b ?\d '^? '^H (control h) (control \?) backspace delete))
  199. (setq key-seq (viper-subseq key-seq 0 (- (length key-seq) 2))))
  200. ((member key '(tab (control i) ?\t))
  201. (setq key-seq (viper-subseq key-seq 0 (1- (length key-seq))))
  202. (setq message
  203. (format
  204. ":unmap%s %s"
  205. variant (if (> (length key-seq) 0)
  206. (prin1-to-string
  207. (viper-display-macro key-seq))
  208. "")))
  209. (setq key-seq
  210. (viper-do-sequence-completion key-seq macro-alist message))
  211. ))
  212. (setq message
  213. (format
  214. ":unmap%s %s"
  215. variant (if (> (length key-seq) 0)
  216. (prin1-to-string
  217. (viper-display-macro key-seq))
  218. "")))
  219. (message "%s" message)
  220. (setq event (viper-read-key))
  221. ;;(setq event (viper-read-event))
  222. (setq key
  223. (if (viper-mouse-event-p event)
  224. (progn
  225. (message "%s (No mouse---only keyboard keys, please)"
  226. message)
  227. (sit-for 2)
  228. nil)
  229. (viper-event-key event)))
  230. )
  231. (setq macro-name key-seq))
  232. (if (= (length macro-name) 0)
  233. (error "Can't unmap an empty macro name"))
  234. ;; convert macro names into vector, if starts with a `['
  235. (if (memq (elt macro-name 0) '(?\[ ?\"))
  236. (car (read-from-string macro-name))
  237. (vconcat macro-name))
  238. ))
  239. ;; Terminate a Vi kbd macro.
  240. ;; optional argument IGNORE, if t, indicates that we are dealing with an
  241. ;; existing macro that needs to be registered, but there is no need to
  242. ;; terminate a kbd macro.
  243. (defun viper-end-mapping-kbd-macro (&optional ignore)
  244. (interactive)
  245. (define-key viper-vi-intercept-map "\C-x)" nil)
  246. (define-key viper-insert-intercept-map "\C-x)" nil)
  247. (define-key viper-emacs-intercept-map "\C-x)" nil)
  248. (if (and (not ignore)
  249. (or (not viper-kbd-macro-parameters)
  250. (not defining-kbd-macro)))
  251. (error "Not mapping a kbd-macro"))
  252. (let ((mod-char (nth 1 viper-kbd-macro-parameters))
  253. (ins (nth 0 viper-kbd-macro-parameters))
  254. (macro-name (nth 2 viper-kbd-macro-parameters))
  255. (macro-body (nth 3 viper-kbd-macro-parameters)))
  256. (setq viper-kbd-macro-parameters nil)
  257. (or ignore
  258. (progn
  259. (end-kbd-macro nil)
  260. (setq macro-body (viper-events-to-macro last-kbd-macro))
  261. ;; always go back to Vi, since this is where we started
  262. ;; defining macro
  263. (viper-change-state-to-vi)))
  264. (viper-record-kbd-macro macro-name
  265. (if ins 'insert-state 'vi-state)
  266. (viper-display-macro macro-body))
  267. (ex-fixup-history (format "map%s %S %S" mod-char
  268. (viper-display-macro macro-name)
  269. (viper-display-macro macro-body)))
  270. ))
  271. ;;; Recording, unrecording, executing
  272. ;; Accepts as macro names: strings and vectors.
  273. ;; strings must be strings of characters; vectors must be vectors of keys
  274. ;; in canonical form, which is essentially the form used in XEmacs.
  275. ;; More general definitions are inherited by more specific scopes:
  276. ;; global->major mode->buffer. More specific definitions override more general
  277. (defun viper-record-kbd-macro (macro-name state macro-body &optional scope)
  278. "Record a Vi macro. Can be used in `.viper' file to define permanent macros.
  279. MACRO-NAME is a string of characters or a vector of keys. STATE is
  280. either `vi-state' or `insert-state'. It specifies the Viper state in which to
  281. define the macro. MACRO-BODY is a string that represents the keyboard macro.
  282. Optional SCOPE says whether the macro should be global \(t\), mode-specific
  283. \(a major-mode symbol\), or buffer-specific \(buffer name, a string\).
  284. If SCOPE is nil, the user is asked to specify the scope."
  285. (let* (state-name keymap
  286. (macro-alist-var
  287. (cond ((eq state 'vi-state)
  288. (setq state-name "Vi state"
  289. keymap viper-vi-kbd-map)
  290. 'viper-vi-kbd-macro-alist)
  291. ((memq state '(insert-state replace-state))
  292. (setq state-name "Insert state"
  293. keymap viper-insert-kbd-map)
  294. 'viper-insert-kbd-macro-alist)
  295. (t
  296. (setq state-name "Emacs state"
  297. keymap viper-emacs-kbd-map)
  298. 'viper-emacs-kbd-macro-alist)
  299. ))
  300. new-elt old-elt old-sub-elt msg
  301. temp lis lis2)
  302. (if (= (length macro-name) 0)
  303. (error "Can't map an empty macro name"))
  304. ;; Macro-name is usually a vector. However, command history or macros
  305. ;; recorded in ~/.viper may be recorded as strings. So, convert to
  306. ;; vectors.
  307. (setq macro-name (viper-fixup-macro macro-name))
  308. (if (viper-char-array-p macro-name)
  309. (setq macro-name (viper-char-array-to-macro macro-name)))
  310. (setq macro-body (viper-fixup-macro macro-body))
  311. (if (viper-char-array-p macro-body)
  312. (setq macro-body (viper-char-array-to-macro macro-body)))
  313. ;; don't ask if scope is given and is of the right type
  314. (or (eq scope t)
  315. (stringp scope)
  316. (and scope (symbolp scope))
  317. (progn
  318. (setq scope
  319. (cond
  320. ((y-or-n-p
  321. (format
  322. "Map this macro for buffer `%s' only? "
  323. (buffer-name)))
  324. (setq msg
  325. (format
  326. "%S is mapped to %s for %s in `%s'"
  327. (viper-display-macro macro-name)
  328. (viper-abbreviate-string
  329. (format
  330. "%S"
  331. (setq temp (viper-display-macro macro-body)))
  332. 14 "" ""
  333. (if (stringp temp) " ....\"" " ....]"))
  334. state-name (buffer-name)))
  335. (buffer-name))
  336. ((y-or-n-p
  337. (format
  338. "Map this macro for the major mode `%S' only? "
  339. major-mode))
  340. (setq msg
  341. (format
  342. "%S is mapped to %s for %s in `%S'"
  343. (viper-display-macro macro-name)
  344. (viper-abbreviate-string
  345. (format
  346. "%S"
  347. (setq temp (viper-display-macro macro-body)))
  348. 14 "" ""
  349. (if (stringp macro-body) " ....\"" " ....]"))
  350. state-name major-mode))
  351. major-mode)
  352. (t
  353. (setq msg
  354. (format
  355. "%S is globally mapped to %s in %s"
  356. (viper-display-macro macro-name)
  357. (viper-abbreviate-string
  358. (format
  359. "%S"
  360. (setq temp (viper-display-macro macro-body)))
  361. 14 "" ""
  362. (if (stringp macro-body) " ....\"" " ....]"))
  363. state-name))
  364. t)))
  365. (if (y-or-n-p
  366. (format "Save this macro in %s? "
  367. (viper-abbreviate-file-name viper-custom-file-name)))
  368. (viper-save-string-in-file
  369. (format "\n(viper-record-kbd-macro %S '%S %s '%S)"
  370. (viper-display-macro macro-name)
  371. state
  372. ;; if we don't let vector macro-body through %S,
  373. ;; the symbols `\.' `\[' etc will be converted into
  374. ;; characters, causing invalid read error on recorded
  375. ;; macros in .viper.
  376. ;; I am not sure is macro-body can still be a string at
  377. ;; this point, but I am preserving this option anyway.
  378. (if (vectorp macro-body)
  379. (format "%S" macro-body)
  380. macro-body)
  381. scope)
  382. viper-custom-file-name))
  383. (message "%s" msg)
  384. ))
  385. (setq new-elt
  386. (cons macro-name
  387. (cond ((eq scope t) (list nil nil (cons t nil)))
  388. ((symbolp scope)
  389. (list nil (list (cons scope nil)) (cons t nil)))
  390. ((stringp scope)
  391. (list (list (cons scope nil)) nil (cons t nil))))))
  392. (setq old-elt (assoc macro-name (eval macro-alist-var)))
  393. (if (null old-elt)
  394. (progn
  395. ;; insert new-elt in macro-alist-var and keep the list sorted
  396. (define-key
  397. keymap
  398. (vector (viper-key-to-emacs-key (aref macro-name 0)))
  399. 'viper-exec-mapped-kbd-macro)
  400. (setq lis (eval macro-alist-var))
  401. (while (and lis (string< (viper-array-to-string (car (car lis)))
  402. (viper-array-to-string macro-name)))
  403. (setq lis2 (cons (car lis) lis2))
  404. (setq lis (cdr lis)))
  405. (setq lis2 (reverse lis2))
  406. (set macro-alist-var (append lis2 (cons new-elt lis)))
  407. (setq old-elt new-elt)))
  408. (setq old-sub-elt
  409. (cond ((eq scope t) (viper-kbd-global-pair old-elt))
  410. ((symbolp scope) (assoc scope (viper-kbd-mode-alist old-elt)))
  411. ((stringp scope) (assoc scope (viper-kbd-buf-alist old-elt)))))
  412. (if old-sub-elt
  413. (setcdr old-sub-elt macro-body)
  414. (cond ((symbolp scope) (setcar (cdr (cdr old-elt))
  415. (cons (cons scope macro-body)
  416. (viper-kbd-mode-alist old-elt))))
  417. ((stringp scope) (setcar (cdr old-elt)
  418. (cons (cons scope macro-body)
  419. (viper-kbd-buf-alist old-elt))))))
  420. ))
  421. ;; macro name must be a vector of viper-style keys
  422. ;; viper-unrecord-kbd-macro doesn't have scope. Macro definitions are inherited
  423. ;; from global -> major mode -> buffer
  424. ;; More specific definition overrides more general
  425. ;; Can't unrecord definition for more specific, if a more general definition is
  426. ;; in effect
  427. (defun viper-unrecord-kbd-macro (macro-name state)
  428. "Delete macro MACRO-NAME from Viper STATE.
  429. MACRO-NAME must be a vector of viper-style keys. This command is used by Viper
  430. internally, but the user can also use it in ~/.viper to delete pre-defined
  431. macros supplied with Viper. The best way to avoid mistakes in macro names to
  432. be passed to this function is to use viper-describe-kbd-macros and copy the
  433. name from there."
  434. (let* (state-name keymap
  435. (macro-alist-var
  436. (cond ((eq state 'vi-state)
  437. (setq state-name "Vi state"
  438. keymap viper-vi-kbd-map)
  439. 'viper-vi-kbd-macro-alist)
  440. ((memq state '(insert-state replace-state))
  441. (setq state-name "Insert state"
  442. keymap viper-insert-kbd-map)
  443. 'viper-insert-kbd-macro-alist)
  444. (t
  445. (setq state-name "Emacs state"
  446. keymap viper-emacs-kbd-map)
  447. 'viper-emacs-kbd-macro-alist)
  448. ))
  449. buf-mapping mode-mapping global-mapping
  450. macro-pair macro-entry)
  451. ;; Macro-name is usually a vector. However, command history or macros
  452. ;; recorded in ~/.viper may appear as strings. 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 "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 "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