(1) Push every new
this-command onto a list containing any previously pushed
(2) Limit the list to a variable length (e.g., 3 or 5), and chop off the oldest.
(3) Compare whether all elements of the list are
(defvar this-command-list nil) (defvar maximum-elements 3) (defun pre-command-hook-fn () (push this-command this-command-list) (if (> (length this-command-list) maximum-elements) (setq this-command-list (delq (last this-command-list) this-command-list))) (TEST IF ALL ELEMENTS of `this-command-list' ARE THE SAME)) (add-hook 'pre-command-hook 'pre-command-hook-fn)
Background (aka why): I am instituting a concept similar to the deplacement-command list used by flyspell -- i.e., if
this-command is the same as
last-command and it is also a
memq of a list of certain deplacement commands, then don't do a costly function. The difference here is that I want to compare a variable number of previous commands with the current command -- e.g., if I move the cursor (consecutively) 3 or 5 times to the left, then skip the costly function that would ordinarily run every command loop.
EDIT: The following is a working solution based on the answer by @phils below. I thought it important to correct the mistake above which used
delq instead of
(require 'cl) (defvar this-command-list nil) (defvar maximum-elements 3) (defun pre-command-hook-fn () (push this-command this-command-list) (when (> (length this-command-list) maximum-elements) (setq this-command-list (nbutlast this-command-list))) (message "test: %s" (eq (length (cl-remove-duplicates this-command-list)) 1))) (add-hook 'pre-command-hook 'pre-command-hook-fn)