all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sebastian Wiesner <swiesner@lunaryorn.com>
To: Emanuel Berg <embe8573@student.uu.se>
Cc: help-gnu-emacs@gnu.org
Subject: Re: run-with-timer does not display message
Date: Sat, 19 Jul 2014 17:09:32 +0200	[thread overview]
Message-ID: <F2CD48DB-BB0D-4315-A12C-2824C6BE6BAA@lunaryorn.com> (raw)
In-Reply-To: <87d2d2e4ga.fsf@debian.uxu>

Am 18.07.2014 um 23:34 schrieb Emanuel Berg <embe8573@student.uu.se>:

> Stefan Monnier <monnier@iro.umontreal.ca> writes:
> 
>> Please don't quote your lambdas!
> 
> Do you mean in that case or never?
> 
> I have had problems with lambdas and both parameters
> and `let' bindings.

Enable lexical-binding in your Emacs Lisp files to avoid these.  

See https://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html and https://www.gnu.org/software/emacs/manual/html_node/elisp/Using-Lexical-Binding.html for details.

> For example, this works but not without the
> backtick/backquote (and the commas):

With lexical binding it does.

> (defun shortcut-to-file (key-prefix key file-prefix file)
>  (global-set-key
>   (format "%s%s" key-prefix key)
>   `(lambda ()
>      (interactive)
>      (do-show-file (format "%s%s" ,file-prefix ,file)) )))

With lexical binding it works without any quoting:

(defun shortcut-to-file (key-prefix key file-prefix file)
 (global-set-key
  (format "%s%s" key-prefix key)
  (lambda ()
    (interactive)
    (do-show-file (format "%s%s" file-prefix file)))))

The arguments are captured in a closure, and thus preserved when the lambda body is evaluated.

This is more efficient than your variant.  Lexical bindings are generally more efficient than dynamic ones, because local variables can be elided entirely, and the byte compiler can now inspect and byte-compile the lambda form.  

It’s also safer, because the byte compiler can now warn you about unused variables or free variables, which helps you to catch misspelled variable names.


  reply	other threads:[~2014-07-19 15:09 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-15 13:47 run-with-timer does not display message Matthias Pfeifer
2014-07-15 13:53 ` Thorsten Jolitz
2014-07-18 16:26   ` Stefan Monnier
     [not found]   ` <mailman.5631.1405701027.1147.help-gnu-emacs@gnu.org>
2014-07-18 21:34     ` Emanuel Berg
2014-07-19 15:09       ` Sebastian Wiesner [this message]
2014-07-19 15:27         ` Eli Zaretskii
2014-07-19 17:08           ` Stefan Monnier
2014-07-19 18:04             ` Drew Adams
     [not found]           ` <mailman.5702.1405789759.1147.help-gnu-emacs@gnu.org>
2014-07-19 17:43             ` Emanuel Berg
2014-07-19 18:12               ` Stefan Monnier
2014-07-19 20:06                 ` Lexical and Dynamic Scope Robert Thorpe
2014-07-20 12:15               ` run-with-timer does not display message Sebastian Wiesner
2014-07-21 13:26                 ` Stefan Monnier
     [not found]               ` <mailman.5708.1405793578.1147.help-gnu-emacs@gnu.org>
2014-07-20 19:27                 ` Emanuel Berg
2014-07-21 13:29                   ` Stefan Monnier
     [not found]                   ` <mailman.5813.1405950328.1147.help-gnu-emacs@gnu.org>
2014-07-21 22:17                     ` Emanuel Berg
2014-07-24  8:25                       ` Stefan Monnier
     [not found]               ` <mailman.5754.1405858540.1147.help-gnu-emacs@gnu.org>
2014-07-20 19:37                 ` Emanuel Berg
2014-07-20 20:11                   ` Drew Adams
2014-07-20 21:28                 ` dynamic and lexical scope, attempted summary with example (was: Re: run-with-timer does not display message) Emanuel Berg
2014-07-21  4:44                   ` dynamic and lexical scope, attempted summary with example Thien-Thi Nguyen
2014-07-21  6:36                     ` Thorsten Jolitz
     [not found]                     ` <mailman.5796.1405924598.1147.help-gnu-emacs@gnu.org>
2014-07-21 14:08                       ` Emanuel Berg
2014-07-21  9:23                   ` Michael Heerdegen
     [not found]                   ` <mailman.5792.1405917701.1147.help-gnu-emacs@gnu.org>
2014-07-21 13:36                     ` Emanuel Berg
2014-07-21 17:07                       ` Thien-Thi Nguyen
     [not found]                       ` <mailman.5826.1405962249.1147.help-gnu-emacs@gnu.org>
2014-07-21 22:11                         ` Emanuel Berg
     [not found]                   ` <mailman.5805.1405934637.1147.help-gnu-emacs@gnu.org>
2014-07-21 14:02                     ` Emanuel Berg
     [not found]       ` <mailman.5686.1405782584.1147.help-gnu-emacs@gnu.org>
2014-07-19 17:49         ` run-with-timer does not display message Emanuel Berg
     [not found] ` <mailman.5455.1405432451.1147.help-gnu-emacs@gnu.org>
2014-07-15 15:35   ` Emanuel Berg
     [not found] <mailman.5454.1405432029.1147.help-gnu-emacs@gnu.org>
2014-07-15 14:34 ` Joost Kremers
2014-07-15 15:18   ` jduthen
2014-07-15 15:20     ` Joost Kremers
2014-07-15 15:40       ` Drew Adams
     [not found]       ` <mailman.5461.1405438875.1147.help-gnu-emacs@gnu.org>
2014-07-15 15:52         ` Joost Kremers
2014-07-15 15:57           ` Emanuel Berg
     [not found]           ` <mailman.5463.1405440423.1147.help-gnu-emacs@gnu.org>
2014-07-15 16:07             ` Emanuel Berg
2014-07-15 16:12             ` Joost Kremers
     [not found]               ` <mailman.5466.1405442248.1147.help-gnu-emacs@gnu.org>
2014-07-16  1:12                 ` Joost Kremers
2014-07-17 10:03                   ` Matthias Pfeifer
     [not found]             ` <<slrnlsakmh.a5k.joost.m.kremers@j.kremers4.news.arnhem.chello.nl>
2014-07-15 16:37               ` Drew Adams
     [not found]         ` <<slrnlsaji8.a5k.joost.m.kremers@j.kremers4.news.arnhem.chello.nl>
2014-07-15 16:06           ` 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

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

  git send-email \
    --in-reply-to=F2CD48DB-BB0D-4315-A12C-2824C6BE6BAA@lunaryorn.com \
    --to=swiesner@lunaryorn.com \
    --cc=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.
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.