Paste number 304865: Emacs context keys

Paste number 304865: Emacs context keys
Pasted by: tsdh
When:4 years, 9 months ago
Share:Tweet this! | http://paste.lisp.org/+6J8H
Channel:None
Paste contents:
Raw Source | XML | Display As
(defmacro th/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 \"<C-tab>\")
     (cond
      ((not (hs-already-hidden-p))
       'hs-hide-block)
      ((hs-already-hidden-p)
       'hs-show-block)))

This will make <C-tab> 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))))

;; Some usages

(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)))

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.