emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: mykie.el


From: Tassilo Horn
Subject: Re: mykie.el
Date: Thu, 09 Jan 2014 09:13:38 +0100
User-agent: Gnus/5.130008 (Ma Gnus v0.8) Emacs/24.3.50 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> Shall I name this `define-key-contextual' or something else?
>
> I don't see anything "define-key ish" in there.  It's a macro that
> lets you define a *command*.

FWIW, I'd much prefer the define-key-like approach you've suggested
earlier this thread (sorry, can't look it up right now), i.e., where you
can add different clauses separately.  Ideally, it would be cool if it
allowed to define the same key in different keymaps and still be working
as expected.  E.g.:

  (define-context-key outline-minor-mode-map (kbd "TAB")
    :predicate outline-heading-p
    :action org-cycle)

  (define-context-key emacs-lisp-mode-map (kbd "TAB")
    :predicate beginning-of-defun-p
    :action eval-defun)

And then in a elisp buffer with outline-minor-mode enabled, if I'm on a
outline heading, it would cycle through the outline states, if I'm at
the beginning of a defun, if would eval it, and if I'm on neither of
those, then it does what TAB would do usually.

I already do have such a macro, but it doesn't allow defining multiple
separate predicate-action combos for the same key.  Falling back to the
"do what TAB would do by default" does work, however.  (Actually, you
helped me implementing it.)

--8<---------------cut here---------------start------------->8---
(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))))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]