2017-11-24 16:32:14 +00:00
|
|
|
;;; macros.el --- non-primitive commands for keyboard macros -*- lexical-binding:t -*-
|
1992-05-30 22:12:04 +00:00
|
|
|
|
2024-01-02 01:47:10 +00:00
|
|
|
;; Copyright (C) 1985-1987, 1992, 1994-1995, 2001-2024 Free Software
|
2013-01-01 09:11:05 +00:00
|
|
|
;; Foundation, Inc.
|
1989-10-31 16:00:07 +00:00
|
|
|
|
2019-05-25 20:43:06 +00:00
|
|
|
;; Maintainer: emacs-devel@gnu.org
|
1993-03-18 21:29:42 +00:00
|
|
|
;; Keywords: abbrev
|
2010-08-29 16:17:13 +00:00
|
|
|
;; Package: emacs
|
1992-07-22 04:22:30 +00:00
|
|
|
|
1989-10-31 16:00:07 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
2008-05-06 08:06:51 +00:00
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
1989-10-31 16:00:07 +00:00
|
|
|
;; it under the terms of the GNU General Public License as published by
|
2008-05-06 08:06:51 +00:00
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
1989-10-31 16:00:07 +00:00
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
2017-09-13 22:52:52 +00:00
|
|
|
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
|
1989-10-31 16:00:07 +00:00
|
|
|
|
1993-03-22 03:27:18 +00:00
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Extension commands for keyboard macros. These permit you to assign
|
|
|
|
;; a name to the last-defined keyboard macro, expand and insert the
|
2021-09-14 06:43:18 +00:00
|
|
|
;; Lisp corresponding to a macro, query the user from within a macro,
|
1993-03-22 03:27:18 +00:00
|
|
|
;; or apply a macro to each line in the reason.
|
|
|
|
|
1992-07-16 21:47:34 +00:00
|
|
|
;;; Code:
|
1989-10-31 16:00:07 +00:00
|
|
|
|
2017-11-24 16:32:14 +00:00
|
|
|
(require 'kmacro)
|
|
|
|
|
1991-05-09 21:50:34 +00:00
|
|
|
;;;###autoload
|
2017-11-24 16:32:14 +00:00
|
|
|
(defalias 'name-last-kbd-macro #'kmacro-name-last-macro)
|
1989-10-31 16:00:07 +00:00
|
|
|
|
2019-05-05 17:24:15 +00:00
|
|
|
(defun macros--insert-vector-macro (definition)
|
|
|
|
"Print DEFINITION, a vector, into the current buffer."
|
2019-08-23 11:55:09 +00:00
|
|
|
(insert ?\[
|
|
|
|
(mapconcat (lambda (event)
|
|
|
|
(or (prin1-char event)
|
|
|
|
(prin1-to-string event)))
|
|
|
|
definition
|
|
|
|
" ")
|
|
|
|
?\]))
|
2019-05-05 17:24:15 +00:00
|
|
|
|
1991-05-09 21:50:34 +00:00
|
|
|
;;;###autoload
|
1989-10-31 16:00:07 +00:00
|
|
|
(defun insert-kbd-macro (macroname &optional keys)
|
2014-02-08 03:03:50 +00:00
|
|
|
"Insert in buffer the definition of kbd macro MACRONAME, as Lisp code.
|
|
|
|
MACRONAME should be a symbol.
|
1991-03-15 20:39:25 +00:00
|
|
|
Optional second arg KEYS means also record the keys it is on
|
1992-10-23 09:31:22 +00:00
|
|
|
\(this is the prefix argument, when calling interactively).
|
1989-10-31 16:00:07 +00:00
|
|
|
|
1991-03-15 20:39:25 +00:00
|
|
|
This Lisp code will, when executed, define the kbd macro with the same
|
|
|
|
definition it has now. If you say to record the keys, the Lisp code
|
|
|
|
will also rebind those keys to the macro. Only global key bindings
|
|
|
|
are recorded since executing this Lisp code always makes global
|
|
|
|
bindings.
|
1989-10-31 16:00:07 +00:00
|
|
|
|
1992-10-23 09:31:22 +00:00
|
|
|
To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
|
1989-10-31 16:00:07 +00:00
|
|
|
use this command, and then save the file."
|
2004-11-28 23:57:47 +00:00
|
|
|
(interactive (list (intern (completing-read "Insert kbd macro (name): "
|
|
|
|
obarray
|
2017-11-24 16:32:14 +00:00
|
|
|
#'kmacro-keyboard-macro-p
|
2004-11-06 11:49:55 +00:00
|
|
|
t))
|
|
|
|
current-prefix-arg))
|
1992-10-23 09:38:44 +00:00
|
|
|
(let (definition)
|
|
|
|
(if (string= (symbol-name macroname) "")
|
|
|
|
(progn
|
|
|
|
(setq macroname 'last-kbd-macro definition last-kbd-macro)
|
|
|
|
(insert "(setq "))
|
|
|
|
(setq definition (symbol-function macroname))
|
2022-04-02 00:07:33 +00:00
|
|
|
;; Prefer `defalias' over `fset' since it additionally keeps
|
|
|
|
;; track of the file where the users added it, and it interacts
|
|
|
|
;; better with `advice-add' (and hence things like ELP).
|
|
|
|
(insert "(defalias '"))
|
1992-10-23 09:38:44 +00:00
|
|
|
(prin1 macroname (current-buffer))
|
|
|
|
(insert "\n ")
|
2023-02-24 19:18:24 +00:00
|
|
|
(when (or (stringp definition) (vectorp definition))
|
|
|
|
(setq definition (kmacro (kmacro--to-vector definition))))
|
2022-04-02 00:07:33 +00:00
|
|
|
(if (kmacro-p definition)
|
|
|
|
(let ((vecdef (kmacro--keys definition))
|
|
|
|
(counter (kmacro--counter definition))
|
|
|
|
(format (kmacro--format definition)))
|
|
|
|
(insert "(kmacro ")
|
|
|
|
(prin1 (key-description vecdef) (current-buffer))
|
|
|
|
;; FIXME: Do we really want to store the counter?
|
|
|
|
(unless (and (equal counter 0) (equal format "%d"))
|
|
|
|
(insert " ")
|
|
|
|
(prin1 counter (current-buffer))
|
|
|
|
(insert " ")
|
|
|
|
(prin1 format (current-buffer)))
|
|
|
|
(insert ")"))
|
|
|
|
;; FIXME: Shouldn't this signal an error?
|
|
|
|
(prin1 definition (current-buffer)))
|
1992-10-23 09:38:44 +00:00
|
|
|
(insert ")\n")
|
|
|
|
(if keys
|
2022-01-23 15:48:42 +00:00
|
|
|
(let ((keys (or (and (symbol-function macroname)
|
|
|
|
(where-is-internal (symbol-function macroname)
|
|
|
|
'(keymap)))
|
2017-02-22 02:31:24 +00:00
|
|
|
(where-is-internal macroname '(keymap)))))
|
1992-10-23 09:38:44 +00:00
|
|
|
(while keys
|
|
|
|
(insert "(global-set-key ")
|
|
|
|
(prin1 (car keys) (current-buffer))
|
|
|
|
(insert " '")
|
|
|
|
(prin1 macroname (current-buffer))
|
|
|
|
(insert ")\n")
|
|
|
|
(setq keys (cdr keys)))))))
|
1989-10-31 16:00:07 +00:00
|
|
|
|
1991-05-09 21:50:34 +00:00
|
|
|
;;;###autoload
|
1989-10-31 16:00:07 +00:00
|
|
|
(defun kbd-macro-query (flag)
|
|
|
|
"Query user during kbd macro execution.
|
2021-09-22 18:26:40 +00:00
|
|
|
|
|
|
|
With prefix argument FLAG, enter recursive edit, reading
|
|
|
|
keyboard commands even within a kbd macro. You can give
|
|
|
|
different commands each time the macro executes.
|
|
|
|
|
|
|
|
Without prefix argument, ask whether to continue running the
|
|
|
|
macro.
|
|
|
|
|
1993-05-09 18:02:10 +00:00
|
|
|
Your options are: \\<query-replace-map>
|
2021-09-22 18:26:40 +00:00
|
|
|
|
1993-05-09 18:02:10 +00:00
|
|
|
\\[act] Finish this iteration normally and continue with the next.
|
|
|
|
\\[skip] Skip the rest of this iteration, and start the next.
|
|
|
|
\\[exit] Stop the macro entirely right now.
|
|
|
|
\\[recenter] Redisplay the screen, then ask again.
|
|
|
|
\\[edit] Enter recursive edit; ask again when you exit from that."
|
1989-10-31 16:00:07 +00:00
|
|
|
(interactive "P")
|
1996-05-29 17:17:34 +00:00
|
|
|
(or executing-kbd-macro
|
1989-10-31 16:00:07 +00:00
|
|
|
defining-kbd-macro
|
2015-06-16 00:41:54 +00:00
|
|
|
(user-error "Not defining or executing kbd macro"))
|
1989-10-31 16:00:07 +00:00
|
|
|
(if flag
|
1996-05-29 17:17:34 +00:00
|
|
|
(let (executing-kbd-macro defining-kbd-macro)
|
1989-10-31 16:00:07 +00:00
|
|
|
(recursive-edit))
|
1996-05-29 17:17:34 +00:00
|
|
|
(if (not executing-kbd-macro)
|
1989-10-31 16:00:07 +00:00
|
|
|
nil
|
1993-05-09 17:58:55 +00:00
|
|
|
(let ((loop t)
|
|
|
|
(msg (substitute-command-keys
|
|
|
|
"Proceed with macro?\\<query-replace-map>\
|
1993-05-12 23:56:18 +00:00
|
|
|
(\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
|
1989-10-31 16:00:07 +00:00
|
|
|
(while loop
|
1996-05-29 17:17:34 +00:00
|
|
|
(let ((key (let ((executing-kbd-macro nil)
|
1993-05-09 17:58:55 +00:00
|
|
|
(defining-kbd-macro nil))
|
1996-01-25 00:55:56 +00:00
|
|
|
(message "%s" msg)
|
1993-05-09 17:58:55 +00:00
|
|
|
(read-event)))
|
|
|
|
def)
|
|
|
|
(setq key (vector key))
|
|
|
|
(setq def (lookup-key query-replace-map key))
|
|
|
|
(cond ((eq def 'act)
|
1989-10-31 16:00:07 +00:00
|
|
|
(setq loop nil))
|
1993-05-09 17:58:55 +00:00
|
|
|
((eq def 'skip)
|
1989-10-31 16:00:07 +00:00
|
|
|
(setq loop nil)
|
1996-05-29 17:17:34 +00:00
|
|
|
(setq executing-kbd-macro ""))
|
1993-05-09 17:58:55 +00:00
|
|
|
((eq def 'exit)
|
1989-10-31 16:00:07 +00:00
|
|
|
(setq loop nil)
|
1996-05-29 17:17:34 +00:00
|
|
|
(setq executing-kbd-macro t))
|
1993-05-09 17:58:55 +00:00
|
|
|
((eq def 'recenter)
|
1989-10-31 16:00:07 +00:00
|
|
|
(recenter nil))
|
1993-05-09 17:58:55 +00:00
|
|
|
((eq def 'edit)
|
1996-05-29 17:17:34 +00:00
|
|
|
(let (executing-kbd-macro defining-kbd-macro)
|
1993-05-09 17:58:55 +00:00
|
|
|
(recursive-edit)))
|
|
|
|
((eq def 'quit)
|
|
|
|
(setq quit-flag t))
|
|
|
|
(t
|
|
|
|
(or (eq def 'help)
|
|
|
|
(ding))
|
|
|
|
(with-output-to-temp-buffer "*Help*"
|
|
|
|
(princ
|
|
|
|
(substitute-command-keys
|
1993-06-09 11:59:12 +00:00
|
|
|
"Specify how to proceed with keyboard macro execution.
|
1993-05-09 17:58:55 +00:00
|
|
|
Possibilities: \\<query-replace-map>
|
|
|
|
\\[act] Finish this iteration normally and continue with the next.
|
|
|
|
\\[skip] Skip the rest of this iteration, and start the next.
|
|
|
|
\\[exit] Stop the macro entirely right now.
|
|
|
|
\\[recenter] Redisplay the screen, then ask again.
|
1994-11-09 05:49:07 +00:00
|
|
|
\\[edit] Enter recursive edit; ask again when you exit from that."))
|
* x-dnd.el (x-dnd-maybe-call-test-function):
* window.el (split-window-vertically):
* whitespace.el (whitespace-help-on):
* vc-rcs.el (vc-rcs-consult-headers):
* userlock.el (ask-user-about-lock-help)
(ask-user-about-supersession-help):
* type-break.el (type-break-force-mode-line-update):
* time-stamp.el (time-stamp-conv-warn):
* terminal.el (te-set-output-log, te-more-break, te-filter)
(te-sentinel,terminal-emulator):
* term.el (make-term, term-exec, term-sentinel, term-read-input-ring)
(term-write-input-ring, term-check-source, term-start-output-log):
(term-display-buffer-line, term-dynamic-list-completions):
(term-ansi-make-term, serial-term):
* subr.el (selective-display):
* strokes.el (strokes-xpm-to-compressed-string, strokes-decode-buffer)
(strokes-encode-buffer, strokes-xpm-for-compressed-string):
* speedbar.el (speedbar-buffers-tail-notes, speedbar-buffers-item-info)
(speedbar-reconfigure-keymaps, speedbar-add-localized-speedbar-support)
(speedbar-remove-localized-speedbar-support)
(speedbar-set-mode-line-format, speedbar-create-tag-hierarchy)
(speedbar-update-special-contents, speedbar-buffer-buttons-engine)
(speedbar-buffers-line-directory):
* simple.el (shell-command-on-region, append-to-buffer)
(prepend-to-buffer):
* shadowfile.el (shadow-save-todo-file):
* scroll-bar.el (scroll-bar-set-window-start, scroll-bar-drag-1)
(scroll-bar-maybe-set-window-start):
* sb-image.el (speedbar-image-dump):
* saveplace.el (save-place-alist-to-file, save-places-to-alist)
(load-save-place-alist-from-file):
* ps-samp.el (ps-print-message-from-summary):
* ps-print.el (ps-flush-output, ps-insert-file, ps-get-boundingbox)
(ps-background-image, ps-begin-job, ps-do-despool):
* ps-bdf.el (bdf-find-file, bdf-read-font-info):
* printing.el (pr-interface, pr-ps-file-print, pr-find-buffer-visiting)
(pr-ps-message-from-summary, pr-lpr-message-from-summary):
(pr-call-process, pr-file-list, pr-interface-save):
* novice.el (disabled-command-function)
(enable-command, disable-command):
* mouse.el (mouse-buffer-menu-alist):
* mouse-copy.el (mouse-kill-preserving-secondary):
* macros.el (kbd-macro-query):
* ledit.el (ledit-go-to-lisp, ledit-go-to-liszt):
* informat.el (batch-info-validate):
* ido.el (ido-copy-current-word, ido-initiate-auto-merge):
* hippie-exp.el (try-expand-dabbrev-visible):
* help-mode.el (help-make-xrefs):
* help-fns.el (describe-variable):
* generic-x.el (bat-generic-mode-run-as-comint):
* finder.el (finder-mouse-select):
* find-dired.el (find-dired-sentinel):
* filesets.el (filesets-file-close):
* files.el (list-directory):
* faces.el (list-faces-display, describe-face):
* facemenu.el (list-colors-display):
* ezimage.el (ezimage-image-association-dump, ezimage-image-dump):
* epg.el (epg--process-filter, epg-cancel):
* epa.el (epa--marked-keys, epa--select-keys, epa-display-info)
(epa--read-signature-type):
* emerge.el (emerge-copy-as-kill-A, emerge-copy-as-kill-B)
(emerge-file-names):
* ehelp.el (electric-helpify):
* ediff.el (ediff-regions-wordwise, ediff-regions-linewise):
* ediff-vers.el (rcs-ediff-view-revision):
* ediff-util.el (ediff-setup):
* ediff-mult.el (ediff-append-custom-diff):
* ediff-diff.el (ediff-exec-process, ediff-process-sentinel)
(ediff-wordify):
* echistory.el (Electric-command-history-redo-expression):
* dos-w32.el (find-file-not-found-set-buffer-file-coding-system):
* disp-table.el (describe-display-table):
* dired.el (dired-find-buffer-nocreate):
* dired-aux.el (dired-rename-subdir, dired-dwim-target-directory):
* dabbrev.el (dabbrev--same-major-mode-p):
* chistory.el (list-command-history):
* apropos.el (apropos-documentation):
* allout.el (allout-obtain-passphrase):
(allout-copy-exposed-to-buffer):
(allout-verify-passphrase): Use with-current-buffer.
2009-11-13 22:19:45 +00:00
|
|
|
(with-current-buffer standard-output
|
1994-11-09 05:49:07 +00:00
|
|
|
(help-mode)))))))))))
|
1991-05-09 21:50:34 +00:00
|
|
|
|
1991-05-17 00:15:22 +00:00
|
|
|
;;;###autoload
|
|
|
|
(defun apply-macro-to-region-lines (top bottom &optional macro)
|
2004-08-31 23:38:53 +00:00
|
|
|
"Apply last keyboard macro to all lines in the region.
|
|
|
|
For each line that begins in the region, move to the beginning of
|
|
|
|
the line, and run the last keyboard macro.
|
1991-05-17 00:15:22 +00:00
|
|
|
|
|
|
|
When called from lisp, this function takes two arguments TOP and
|
|
|
|
BOTTOM, describing the current region. TOP must be before BOTTOM.
|
|
|
|
The optional third argument MACRO specifies a keyboard macro to
|
|
|
|
execute.
|
|
|
|
|
|
|
|
This is useful for quoting or unquoting included text, adding and
|
|
|
|
removing comments, or producing tables where the entries are regular.
|
|
|
|
|
|
|
|
For example, in Usenet articles, sections of text quoted from another
|
|
|
|
author are indented, or have each line start with `>'. To quote a
|
|
|
|
section of text, define a keyboard macro which inserts `>', put point
|
|
|
|
and mark at opposite ends of the quoted section, and use
|
2024-01-21 13:09:21 +00:00
|
|
|
\\[apply-macro-to-region-lines] to mark the entire section.
|
1991-05-17 00:15:22 +00:00
|
|
|
|
|
|
|
Suppose you wanted to build a keyword table in C where each entry
|
|
|
|
looked like this:
|
|
|
|
|
2003-02-04 12:29:42 +00:00
|
|
|
{ \"foo\", foo_data, foo_function },
|
1991-05-17 00:15:22 +00:00
|
|
|
{ \"bar\", bar_data, bar_function },
|
|
|
|
{ \"baz\", baz_data, baz_function },
|
|
|
|
|
|
|
|
You could enter the names in this format:
|
|
|
|
|
|
|
|
foo
|
|
|
|
bar
|
|
|
|
baz
|
|
|
|
|
|
|
|
and write a macro to massage a word into a table entry:
|
|
|
|
|
|
|
|
\\C-x (
|
|
|
|
\\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
|
|
|
|
\\C-x )
|
|
|
|
|
|
|
|
and then select the region of un-tablified names and use
|
2024-01-21 13:09:21 +00:00
|
|
|
\\[apply-macro-to-region-lines] to build the table from the names."
|
1991-05-17 00:15:22 +00:00
|
|
|
(interactive "r")
|
1991-08-14 22:50:16 +00:00
|
|
|
(or macro
|
|
|
|
(progn
|
|
|
|
(if (null last-kbd-macro)
|
2015-06-16 00:41:54 +00:00
|
|
|
(user-error "No keyboard macro has been defined"))
|
1991-08-14 22:50:16 +00:00
|
|
|
(setq macro last-kbd-macro)))
|
1991-05-17 00:15:22 +00:00
|
|
|
(save-excursion
|
2004-08-31 23:38:53 +00:00
|
|
|
(let ((end-marker (copy-marker bottom))
|
1991-11-06 01:23:44 +00:00
|
|
|
next-line-marker)
|
1991-05-17 00:15:22 +00:00
|
|
|
(goto-char top)
|
|
|
|
(if (not (bolp))
|
|
|
|
(forward-line 1))
|
1991-11-06 01:23:44 +00:00
|
|
|
(setq next-line-marker (point-marker))
|
|
|
|
(while (< next-line-marker end-marker)
|
|
|
|
(goto-char next-line-marker)
|
1991-05-18 18:43:24 +00:00
|
|
|
(save-excursion
|
1991-11-06 01:23:44 +00:00
|
|
|
(forward-line 1)
|
|
|
|
(set-marker next-line-marker (point)))
|
|
|
|
(save-excursion
|
2002-09-09 22:44:44 +00:00
|
|
|
(let ((mark-active nil))
|
2010-02-04 05:14:36 +00:00
|
|
|
(execute-kbd-macro macro))))
|
1991-11-06 01:23:44 +00:00
|
|
|
(set-marker end-marker nil)
|
|
|
|
(set-marker next-line-marker nil))))
|
1991-05-17 00:15:22 +00:00
|
|
|
|
1993-12-23 04:52:56 +00:00
|
|
|
;;;###autoload (define-key ctl-x-map "q" 'kbd-macro-query)
|
1992-05-30 22:12:04 +00:00
|
|
|
|
1997-06-22 18:57:55 +00:00
|
|
|
(provide 'macros)
|
|
|
|
|
1992-05-30 22:12:04 +00:00
|
|
|
;;; macros.el ends here
|