From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Barry OReilly Newsgroups: gmane.emacs.help Subject: Re: Trouble using a lambda in the key-translation-map Date: Thu, 19 Jul 2012 15:26:15 -0400 Message-ID: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: dough.gmane.org 1342725985 31868 80.91.229.3 (19 Jul 2012 19:26:25 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 19 Jul 2012 19:26:25 +0000 (UTC) To: help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jul 19 21:26:25 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SrwMi-0000gv-DZ for geh-help-gnu-emacs@m.gmane.org; Thu, 19 Jul 2012 21:26:24 +0200 Original-Received: from localhost ([::1]:54855 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SrwMh-000581-8b for geh-help-gnu-emacs@m.gmane.org; Thu, 19 Jul 2012 15:26:23 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:53536) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SrwMb-00057Y-1a for help-gnu-emacs@gnu.org; Thu, 19 Jul 2012 15:26:17 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SrwMa-0000Og-2x for help-gnu-emacs@gnu.org; Thu, 19 Jul 2012 15:26:16 -0400 Original-Received: from mail-qc0-f169.google.com ([209.85.216.169]:55185) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SrwMZ-0000Oc-V0 for help-gnu-emacs@gnu.org; Thu, 19 Jul 2012 15:26:16 -0400 Original-Received: by qcsd16 with SMTP id d16so2188939qcs.0 for ; Thu, 19 Jul 2012 12:26:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=dDBlWRwirxkjED60AclWtAzwcTBkwyDYNtyM0cV0wKA=; b=cRtc0kOpbGsiuXjaSqedhIwJpo685KqnuJF2tPRhq6vO58DiPxxv906xP7UW+FDxcO qdwJ2+WyAgfLgWBzAjKJsv6ahIXhv6jcXsrKoyFodFjIyt7r23gUVbDo0F4sCyjpoyxG BWIYfqVIDjUOzAjPOnKmXG8MwUaO2XJBREjxCT90geNZy8B20Si82U/4UhMmS5p9mmy5 aewVw0BinJ5nran0YRX6BL28HTcU4wmCS+iuxMbY+k/ceZlk60P56EBwEWdmUvQrpXSB TEvjoVN4tVzIXRnOKzWUjrRNf4DZHqNXOyrmOXxo955ve2YmeUd8vU8o6/iJsoa+EkWO TgIg== Original-Received: by 10.60.2.3 with SMTP id 3mr4205072oeq.0.1342725975259; Thu, 19 Jul 2012 12:26:15 -0700 (PDT) Original-Received: by 10.182.179.35 with HTTP; Thu, 19 Jul 2012 12:26:15 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.216.169 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:85945 Archived-At: >> (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)