Question

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))))
Was it helpful?

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.

OTHER TIPS

Enumerating the commands you know should trigger this hook (using this-command) is of course an option, but "it doesn't scale". Maybe you could do better if you tried to characterize what those commands have in common with each other and that the other commands don't have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top