Advertisement
Guest User

Untitled

a guest
Nov 12th, 2023
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.86 KB | None | 0 0
  1. ;; Keyboard macro enhancement. If you call this, instead of
  2. ;; kbd-macro-query, it will prompt the user for a value. This value
  3. ;; will then be inserted into the buffer. Every time you call the
  4. ;; macro, you can provide a different value.
  5. ;;
  6. ;; Alternatively, you can call this with a prefix argument. If you do
  7. ;; this, you will be prompted for a symbol name. Instead of the value
  8. ;; being inserted into the buffer, it will be saved in the symbol
  9. ;; variable. You can then manipulate it or do whatever you want with
  10. ;; that symbol as part of the keyboard macro. Just, when you do this,
  11. ;; make sure you don't use minibuffer history at all when defining the
  12. ;; macro, or you can get some unexpected behavior if you save your
  13. ;; macro for later use and try it a few hours later!
  14. (defun config:macro-query (symbol)
  15.   (interactive
  16.    (list (when current-prefix-arg
  17.            (intern (read-from-minibuffer "symbol: ")))))
  18.   (cl-flet ((internal-exit ()
  19.               (interactive)
  20.               (exit-recursive-edit)))
  21.     (let ((making-macro defining-kbd-macro)  ;; Save value.
  22.           (temp-map (make-sparse-keymap)))
  23.       ;; Temporarily bind what is normally C-M-c (exit-recursive-edit)
  24.       ;; to RET, so RET will work in the spawned minibuffer.
  25.       (set-keymap-parent temp-map minibuffer-local-map)
  26.       (substitute-key-definition 'exit-minibuffer #'internal-exit temp-map)
  27.       (let ((exit-fn (set-transient-map temp-map (-const t))))
  28.         (cl-flet ((also-quit-minibuffer ()
  29.                     ;; When this is called (advice after
  30.                     ;; recursive-edit), this-command should be
  31.                     ;; whatever was just used to exit the recursive
  32.                     ;; edit / minibuffer. Usually RET. Push that onto
  33.                     ;; the unread commands, and it will immediately
  34.                     ;; get picked up and executed. We also want to use
  35.                     ;; this moment to turn off the transient map.
  36.                     (funcall exit-fn)
  37.                     (when making-macro
  38.                       (setq unread-command-events
  39.                             (nconc (listify-key-sequence (this-command-keys))
  40.                                    unread-command-events)))))
  41.           (advice-add 'recursive-edit :after #'also-quit-minibuffer)
  42.           (unwind-protect
  43.               (let ((input (minibuffer-with-setup-hook
  44.                                (lambda ()
  45.                                  (kbd-macro-query t))
  46.                              (read-from-minibuffer "Value: "))))
  47.                 (if symbol
  48.                     (set symbol input)
  49.                   (insert input)))
  50.             ;; Ensure that the advice and minibuffer map goes back to
  51.             ;; normal.
  52.             (advice-remove 'recursive-edit #'also-quit-minibuffer)
  53.             (funcall exit-fn)))))))
  54. (global-set-key (kbd "C-x Q") 'config:macro-query)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement