unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Emanuel Berg <embe8573@student.uu.se>
To: help-gnu-emacs@gnu.org
Subject: Re: Syntax to use (let...) in key binding
Date: Tue, 03 Mar 2015 00:49:26 +0100	[thread overview]
Message-ID: <878uffrmd5.fsf@debian.uxu> (raw)
In-Reply-To: mailman.1150.1425310684.31049.help-gnu-emacs@gnu.org

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


       reply	other threads:[~2015-03-02 23:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1150.1425310684.31049.help-gnu-emacs@gnu.org>
2015-03-02 23:49 ` Emanuel Berg [this message]
2015-03-02 15:37 Syntax to use (let...) in key binding Tory S. Anderson
2015-03-02 15:55 ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=878uffrmd5.fsf@debian.uxu \
    --to=embe8573@student.uu.se \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).