Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Trouble using a lambda in the key-translation-map
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Barry OReilly  
View profile  
 More options Jul 18 2012, 6:45 pm
Newsgroups: gnu.emacs.help
From: Barry OReilly <gundaeti...@gmail.com>
Date: Wed, 18 Jul 2012 18:45:08 -0400
Local: Wed, Jul 18 2012 6:45 pm
Subject: Trouble using a lambda in the key-translation-map
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-...

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?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry Margolin  
View profile  
 More options Jul 18 2012, 11:45 pm
Newsgroups: gnu.emacs.help
From: Barry Margolin <bar...@alum.mit.edu>
Date: Wed, 18 Jul 2012 23:45:09 -0400
Local: Wed, Jul 18 2012 11:45 pm
Subject: Re: Trouble using a lambda in the key-translation-map
In article <mailman.5119.1342663755.855.help-gnu-em...@gnu.org>,
 Barry OReilly <gundaeti...@gmail.com> wrote:

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.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Barry OReilly  
View profile  
 More options Jul 19 2012, 3:26 pm
Newsgroups: gnu.emacs.help
From: Barry OReilly <gundaeti...@gmail.com>
Date: Thu, 19 Jul 2012 15:26:15 -0400
Local: Thurs, Jul 19 2012 3:26 pm
Subject: Re: Trouble using a lambda in the key-translation-map

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »