Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; Keyboard macro enhancement. If you call this, instead of
- ;; kbd-macro-query, it will prompt the user for a value. This value
- ;; will then be inserted into the buffer. Every time you call the
- ;; macro, you can provide a different value.
- ;;
- ;; Alternatively, you can call this with a prefix argument. If you do
- ;; this, you will be prompted for a symbol name. Instead of the value
- ;; being inserted into the buffer, it will be saved in the symbol
- ;; variable. You can then manipulate it or do whatever you want with
- ;; that symbol as part of the keyboard macro. Just, when you do this,
- ;; make sure you don't use minibuffer history at all when defining the
- ;; macro, or you can get some unexpected behavior if you save your
- ;; macro for later use and try it a few hours later!
- (defun config:macro-query (symbol)
- (interactive
- (list (when current-prefix-arg
- (intern (read-from-minibuffer "symbol: ")))))
- (cl-flet ((internal-exit ()
- (interactive)
- (exit-recursive-edit)))
- (let ((making-macro defining-kbd-macro) ;; Save value.
- (temp-map (make-sparse-keymap)))
- ;; Temporarily bind what is normally C-M-c (exit-recursive-edit)
- ;; to RET, so RET will work in the spawned minibuffer.
- (set-keymap-parent temp-map minibuffer-local-map)
- (substitute-key-definition 'exit-minibuffer #'internal-exit temp-map)
- (let ((exit-fn (set-transient-map temp-map (-const t))))
- (cl-flet ((also-quit-minibuffer ()
- ;; When this is called (advice after
- ;; recursive-edit), this-command should be
- ;; whatever was just used to exit the recursive
- ;; edit / minibuffer. Usually RET. Push that onto
- ;; the unread commands, and it will immediately
- ;; get picked up and executed. We also want to use
- ;; this moment to turn off the transient map.
- (funcall exit-fn)
- (when making-macro
- (setq unread-command-events
- (nconc (listify-key-sequence (this-command-keys))
- unread-command-events)))))
- (advice-add 'recursive-edit :after #'also-quit-minibuffer)
- (unwind-protect
- (let ((input (minibuffer-with-setup-hook
- (lambda ()
- (kbd-macro-query t))
- (read-from-minibuffer "Value: "))))
- (if symbol
- (set symbol input)
- (insert input)))
- ;; Ensure that the advice and minibuffer map goes back to
- ;; normal.
- (advice-remove 'recursive-edit #'also-quit-minibuffer)
- (funcall exit-fn)))))))
- (global-set-key (kbd "C-x Q") 'config:macro-query)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement