help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Emanuel Berg
Subject: Re: Syntax to use (let...) in key binding
Date: Tue, 03 Mar 2015 00:49:26 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

address@hidden (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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]