aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiu Hui2023-08-22 13:52:03 +0800
committerEli Zaretskii2023-08-31 12:30:43 +0300
commit017bf0f99ad549398c4a55445e69f64861c8fefd (patch)
treea9468889e2b57cfbb21fe93a66f35ceeafc3f875
parent4adedd2996188589fbae4a8c99fe39a6f54347d7 (diff)
downloademacs-017bf0f99ad549398c4a55445e69f64861c8fefd.tar.gz
emacs-017bf0f99ad549398c4a55445e69f64861c8fefd.zip
strokes: Support running a function when no stroke matches
* lisp/strokes.el (strokes-no-match-function): New variable. (strokes-no-match-default): New function. (strokes-execute-stroke): Run `strokes-no-match-function' when no stroke matches. (Bug#65449)
-rw-r--r--lisp/strokes.el25
1 files changed, 16 insertions, 9 deletions
diff --git a/lisp/strokes.el b/lisp/strokes.el
index 694677ada0b..5bfcab5f9e2 100644
--- a/lisp/strokes.el
+++ b/lisp/strokes.el
@@ -266,6 +266,14 @@ able to see the strokes. This be helpful for people who don't like
266the delay in switching to the strokes buffer." 266the delay in switching to the strokes buffer."
267 :type 'boolean) 267 :type 'boolean)
268 268
269(defvar strokes-no-match-function 'strokes-no-match-default
270 "Function run by `strokes-execute-stroke' when no stroke matches.
271The function is called with two arguments, the stroke and the
272closest match returned by `strokes-match-stroke'. It can be used
273to show detailed information about the unmatched stroke or
274perform some fallback action. The default function
275`strokes-no-match-default' simply signals an error.")
276
269;;; internal variables... 277;;; internal variables...
270 278
271(defvar strokes-window-configuration nil 279(defvar strokes-window-configuration nil
@@ -838,14 +846,16 @@ Optional EVENT is acceptable as the starting event of the stroke."
838 (goto-char (point-min)) 846 (goto-char (point-min))
839 (bury-buffer))))))) 847 (bury-buffer)))))))
840 848
849(defun strokes-no-match-default (&rest _)
850 "Signal an error when no stroke matches."
851 (error
852 "No stroke matches; see variable `strokes-minimum-match-score'"))
853
841(defun strokes-execute-stroke (stroke) 854(defun strokes-execute-stroke (stroke)
842 "Given STROKE, execute the command which corresponds to it. 855 "Given STROKE, execute the command which corresponds to it.
843The command will be executed provided one exists for that stroke, 856The command will be executed provided one exists for that stroke,
844based on the variable `strokes-minimum-match-score'. 857based on the variable `strokes-minimum-match-score'. If no
845If no stroke matches, nothing is done and return value is nil." 858stroke matches, `strokes-no-match-function' is called."
846 ;; FIXME: Undocument return value. It is not documented for all cases,
847 ;; and doesn't allow differentiating between no stroke matches and
848 ;; command-execute returning nil, anyway.
849 (let* ((match (strokes-match-stroke stroke strokes-global-map)) 859 (let* ((match (strokes-match-stroke stroke strokes-global-map))
850 (command (car match)) 860 (command (car match))
851 (score (cdr match))) 861 (score (cdr match)))
@@ -859,10 +869,7 @@ If no stroke matches, nothing is done and return value is nil."
859 strokes-file)) 869 strokes-file))
860 (strokes-load-user-strokes)) 870 (strokes-load-user-strokes))
861 (error "No strokes defined; use `strokes-global-set-stroke'"))) 871 (error "No strokes defined; use `strokes-global-set-stroke'")))
862 (t 872 (t (funcall strokes-no-match-function stroke match)))))
863 (error
864 "No stroke matches; see variable `strokes-minimum-match-score'")
865 nil))))
866 873
867;;;###autoload 874;;;###autoload
868(defun strokes-do-stroke (event) 875(defun strokes-do-stroke (event)