all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Syntax to use (let...) in key binding
@ 2015-03-02 15:37 Tory S. Anderson
  2015-03-02 15:55 ` Drew Adams
  0 siblings, 1 reply; 3+ messages in thread
From: Tory S. Anderson @ 2015-03-02 15:37 UTC (permalink / raw
  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. 




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

* RE: Syntax to use (let...) in key binding
  2015-03-02 15:37 Syntax to use (let...) in key binding Tory S. Anderson
@ 2015-03-02 15:55 ` Drew Adams
  0 siblings, 0 replies; 3+ messages in thread
From: Drew Adams @ 2015-03-02 15:55 UTC (permalink / raw
  To: torys.anderson, 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.



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

* Re: Syntax to use (let...) in key binding
       [not found] <mailman.1150.1425310684.31049.help-gnu-emacs@gnu.org>
@ 2015-03-02 23:49 ` Emanuel Berg
  0 siblings, 0 replies; 3+ messages in thread
From: Emanuel Berg @ 2015-03-02 23:49 UTC (permalink / raw
  To: help-gnu-emacs

torys.anderson@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


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

end of thread, other threads:[~2015-03-02 23:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-02 15:37 Syntax to use (let...) in key binding Tory S. Anderson
2015-03-02 15:55 ` Drew Adams
     [not found] <mailman.1150.1425310684.31049.help-gnu-emacs@gnu.org>
2015-03-02 23:49 ` Emanuel Berg

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.