Project:
(1) Push every new this-command
onto a list containing any previously pushed this-command
.
(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 eq
.
(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 nbutlast
.
(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)
this-function
was one of the last five functions called?eq
-- e.g.,'(left-char left-char left-char left-char left-char)
-- if they are all the same, then throw at
. Essentially, I don't mind if my costly function runs a couple of times consecutively, but if I'm just repeating the same thing again and again like holding down the arrow key, then I'd like to skip the costly function -- e.g., if its 3 or 5 times in a row, then start skipping the costly function (whatever that may be).