From 69b265fba3346f22392ca56cd12067fc5e2e3974 Mon Sep 17 00:00:00 2001 From: Thomas von Dein Date: Thu, 15 Jun 2023 18:38:41 +0200 Subject: [PATCH] added 'define-context-key macro: Now I am able to define context sensitive key bindings. --- lisp/init-system.el | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lisp/init-system.el b/lisp/init-system.el index 5698e8b..bdc640c 100644 --- a/lisp/init-system.el +++ b/lisp/init-system.el @@ -237,5 +237,48 @@ (require 'iso-transl) +;; via: http://paste.lisp.org/display/304865 +;; examples: +;; +;; (th/define-context-key outline-minor-mode-map +;; (kbd "TAB") +;; (when (th/outline-context-p) +;; 'org-cycle)) +;; +;; ;; Jump out of a TeX macro when pressing TAB twice. +;; (th/define-context-key TeX-mode-map (kbd "TAB") +;; (when (and (= 1 (length (this-command-keys-vector))) +;; (equal last-command-event (elt (this-command-keys-vector) 0)) +;; (TeX-current-macro)) +;; #'th/TeX-goto-macro-end))) +;; +;; TODO: find uses :) +(defmacro define-context-key (keymap key dispatch) + "Define KEY in KEYMAP to execute according to DISPATCH. + +DISPATCH is a form that is evaluated and should return the +command to be executed. + +If DISPATCH returns nil, then the command normally bound to KEY +will be executed. + +Example: + + (th/define-context-key hs-minor-mode-map + (kbd \"\") + (cond + ((not (hs-already-hidden-p)) + 'hs-hide-block) + ((hs-already-hidden-p) + 'hs-show-block))) + +This will make show a hidden block. If the block is +shown, then it'll be hidden." + `(define-key ,keymap ,key + `(menu-item "context-key" ignore + :filter ,(lambda (&optional ignored) + ,dispatch)))) + + (provide 'init-system) ;;; init-system.el ends here