Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Syntax to use (let...) in key binding

7 views
Skip to first unread message

Tory S. Anderson

unread,
Mar 2, 2015, 10:38:06 AM3/2/15
to Emacs Help List
I'm guessing it's some stupid mistake, but why doesn't this work?
--8<---------------cut here---------------start------------->8---
;; Resume bookmark view or create it
(global-set-key [f10] '(let ((b "*Bookmark List*")) (if (get-buffer b) '(switch-to-buffer b) 'bookmark-bmenu-list)))
--8<---------------cut here---------------end--------------->8---

This results in:
command-execute: Wrong type argument: commandp, (let ((b "*Bookmark List*")) (if (get-buffer b) (quote (switch-to-buffer b)) (quote bookmark-bmenu-list)))

Is the "let" not returning a command like I want it to? debug-on-error doesn't seem to be helping me understand this.


Drew Adams

unread,
Mar 2, 2015, 10:55:21 AM3/2/15
to torys.a...@gmail.com, Emacs Help List
> I'm guessing it's some stupid mistake, but why doesn't this work?
>
> (global-set-key [f10] '(let ((b "*Bookmark List*")) (if (get-buffer b)
> '(switch-to-buffer b) 'bookmark-bmenu-list)))
>
> This results in:
> command-execute: Wrong type argument: commandp, (let ((b "*Bookmark List*"))
>
> Is the "let" not returning a command like I want it to? debug-on-error
> doesn't seem to be helping me understand this.

What gets bound (or tries to be bound) to the key is the sexp (let...),
not its value (which is a command). That sexp is not a command.

Remove the quote to have the (let...) be evaluated, and bind its value.

But that is likely not what you want. I'm guessing that you want to
bind the key to a command (which you would write) that would, itself,
bind `b' etc. and then invoke whatever other code you want.

Emanuel Berg

unread,
Mar 2, 2015, 6:45:58 PM3/2/15
to
torys.a...@gmail.com (Tory S. Anderson) writes:

> ... (global-set-key [f10] '(let ((b "*Bookmark
> List*")) (if (get-buffer b) '(switch-to-buffer b)
> 'bookmark-bmenu-list)))
>
> This results in: command-execute: Wrong type
> argument: commandp, (let ((b "*Bookmark List*")) (if
> (get-buffer b) (quote (switch-to-buffer b)) (quote
> bookmark-bmenu-list)))
>
> Is the "let" not returning a command like I want it
> to? debug-on-error doesn't seem to be helping me
> understand this.

There are two ways to do that.

The best way is to write a defun. It must be
(interactive) else you can't access it with a keydown
(it isn't a "command") or from M-x for that matter.

So:

(defun my-defun ()
(interactive)
; ... do stuff
)

Then you can use `define-key' or `global-set-key' as
below, just substitute keymap, key, and function
(command) name:

(define-key w3m-mode-map "g" 'w3m-goto-url-kill-current)
(global-set-key "\r" 'newline-and-indent)

The second way to do it is to use a so called
anonymous (inline) function, with lambda:

(global-set-key "\C-^" (lambda () (interactive) (message "hello")))

(Hold the control key and hit '6' to try after
evaluating.)

Lambdas make for compact code but it can get out of
hand pretty quickly if you need to add more code.
Better to do a proper defun so you can use it from
code (Elisp) and the M-x interface as well.

But whatever you do, don't quote that lambda... or
else!

Small last note: F10 isn't a good key for a shortcut
because you have to leave typing position (left hand:
asfd and right hand: jkl;) and reach to hit it (F10),
then reset. Speed kills!

--
underground experts united
0 new messages