Programming
emacs elisp
Updated Mon, 12 Sep 2022 07:24:41 GMT

Emacs -- creating an interactive only quasi-post-command-hook


I'm interested in creating an interactive-only quasi-post-command-hook that runs based upon user-defined-criteria, such as:

  • Up (interactive);

  • Down (interactive);

  • Left (interactive);

  • Right (interactive);

  • Any (interactive) key that inserts text into the buffer -- e.g., aA-zZ; 0-9; space; !@#$%^&*()-+=.;, return/enter, delete/backspace, etc.

  • Mouse scroll-wheel (up / down) (interactive).

I believe that the post-command-hook includes more, and I'd like to limit / control when the hook is activated.

Any guidance on how to create such a hook would be appreciated.


May 3, 2013: Draft example based upon the answer by @phils below.

(add-hook 'post-command-hook 'quasi-post-command-hook)
(defvar quasi-this-command-functions '(next-line previous-line left-char right-char
  self-insert-command newline delete-backward-char delete-forward-char
  indent-for-tab-command mwheel-scroll lawlist-mwheel-scroll end-of-visual-line
  beginning-of-visual-line end-of-buffer beginning-of-buffer lawlist-forward-entity
  lawlist-backward-entity left-word right-word forward-word backward-word)
"Variable list of functions that trigger the `quasi-post-command-hook`.")
(defvar quasi-major-mode-inclusions '(text-mode emacs-lisp-mode)
"Variable list of major modes where the `quasi-post-command-hook` operates.")
(defun quasi-post-command-hook ()
  (unless (minibufferp)
    (when
      (and
        (memq major-mode quasi-major-mode-inclusions)
        (memq this-command quasi-this-command-functions))
      (message "this-command:  %s" this-command))))



Solution

You probably want to test the this-command variables.

e.g. C-u M-x apropos-variable RET this RET:

real-this-command
   This is like `this-command', except that commands should never
   modify it.
this-command
   The command now being executed.
this-command-keys-shift-translated
   Non-nil if the key sequence activating this command was
   shift-translated.
this-original-command
   The command bound to the current key sequence before remapping.