all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Re: Trouble using a lambda in the key-translation-map
@ 2012-07-19 19:26 Barry OReilly
  0 siblings, 0 replies; 3+ messages in thread
From: Barry OReilly @ 2012-07-19 19:26 UTC (permalink / raw)
  To: help-gnu-emacs

>>   (defun my-translate-key (prompt) (interactive) (kbd "C-c"))
>>   (define-key key-translation-map (kbd "C-e") (lambda (prompt)
>> (interactive) (kbd "C-c"))) ; Doesn't work
>>   (define-key key-translation-map (kbd "C-e") 'my-translate-key) ; Works

> Look at the description of define-key -- the third argument has to be a
> SYMBOL for it to be treated as a function.  Lambda doesn't return a
> function, it returns a list.

Yet this works:
   (define-key global-map (kbd "C-e") (lambda () (interactive)
(message "Inside C-e's lambda")))
C-e displays the message without going to the end of the line, as expected.

I thought wherever functions are passed, a lambda could be passed
instead.  The Elisp manual corroborates this in section 12.7 Anonymous
Functions: "Anonymous functions are valid wherever function names
are.".  I don't see where my misunderstanding is.

I tried another thing in scratch:
  (progn
    (fset 'foo (lambda (prompt) (message "Inside lambda.") (kbd "C-c")))
    (define-key key-translation-map (kbd "C-e") 'foo)
    )

This behaves as I'd expect; I see "Inside lambda" and a translation to
C-c.  Thus it would seem the type returned from the lambda form is not
the problem.

The reason I ask is because I'm trying to create a generalized way to
make key translations that either translate to another key or to
itself based on an evaluated predicate.  My approach was:

(defun make-conditional-key-translation (key-from key-to translate-keys-p)
  "Make a Key Translation such that if the translate-keys-p function
returns true,
key-from translates to key-to, else key-from translates to itself.
translate-keys-p
takes no args. "
  (define-key key-translation-map key-from
              (lambda (prompt)
                      (if (funcall translate-keys-p) key-to key-from)))
  )
(defun my-translate-keys-p ()
  "Returns whether conditional key translations should be active.  See
make-conditional-key-translation function. "
  (or (evil-motion-state-p) (evil-normal-state-p) (evil-visual-state-p))
  )
(make-conditional-key-translation (kbd "ce") (kbd "C-e") 'my-translate-keys-p)



^ permalink raw reply	[flat|nested] 3+ messages in thread
[parent not found: <mailman.5119.1342663755.855.help-gnu-emacs@gnu.org>]
* Trouble using a lambda in the key-translation-map
@ 2012-07-18 22:45 Barry OReilly
  0 siblings, 0 replies; 3+ messages in thread
From: Barry OReilly @ 2012-07-18 22:45 UTC (permalink / raw)
  To: help-gnu-emacs

As the Elisp manual describes, one can bind a key to a function in the
key-translation-map, and use that function to programmatically
determine what to translate the key to.
  http://www.gnu.org/software/emacs/manual/html_node/elisp/Translation-Keymaps.html

I got this to work for functions defined with defun, but can't get it
to work with a lambda.  In an 'emacs -q' session, I put this in the
scratch buffer:
  (defun my-translate-key (prompt) (interactive) (kbd "C-c"))
  (define-key key-translation-map (kbd "C-e") (lambda (prompt)
(interactive) (kbd "C-c"))) ; Doesn't work
  (define-key key-translation-map (kbd "C-e") 'my-translate-key) ; Works

I evaluate the first and second sexp, and find C-e still goes to the
end of the line.  When I next execute the third sexp, C-e now
translates to C-c.  Why doesn't the second sexp enable the translation
from C-e to C-c?



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-07-19 19:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-19 19:26 Trouble using a lambda in the key-translation-map Barry OReilly
     [not found] <mailman.5119.1342663755.855.help-gnu-emacs@gnu.org>
2012-07-19  3:45 ` Barry Margolin
  -- strict thread matches above, loose matches on Subject: below --
2012-07-18 22:45 Barry OReilly

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.