all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Tassilo Horn <tassilo@member.fsf.org>
To: help-gnu-emacs@gnu.org
Subject: Re: elisp help: Getting the current word position. (closure?)
Date: Sat, 31 May 2008 18:06:17 +0200	[thread overview]
Message-ID: <877idav4p2.fsf@baldur.tsdh.de> (raw)
In-Reply-To: 693cc531-de12-476c-b82e-642d6e5910d0@u12g2000prd.googlegroups.com

cinsk <cinsky@gmail.com> writes:

Hi!

> (defun current-word-markers (&optional STRICT REALLY-WORD)
>   "Return the position of the current word"
>   (let ((old (symbol-function
>                 'buffer-substring-no-properties))
>         (saved-start (make-marker))
>         (saved-end (make-marker)) ret)
>
>     (fset 'buffer-substring-no-properties
>           (lambda (start end)
>             (let (ret)
>               (set-marker saved-start start)
>               (set-marker saved-end end)
>               (setq ret (funcall old start end))
>               ret)))
>     (setq ret (current-word STRICT REALLY-WORD))
>     (fset 'buffer-substring-no-properties old)
>     (if ret
>         (cons saved-start saved-end)
>       nil)))
>
>
> This function works.  With my short knowledge of LISP, I think the
> (lambda ...) form in `current-word-markers' is a closure, since it
> refers to the unbound variables (saved-start and saved-end).

Due to emacs' dynamic scoping they're bound when this function is
called.  Dynamic scoping basically means that a function can see all
variables its caller defined (recursively).  Here's an example:

--8<---------------cut here---------------start------------->8---
(defun foo ()
  (let ((foo-val "I'm a local variable of foo!"))
    (bar)))

(defun bar ()
  foo-val)

(foo) ;; C-x C-e ==> "I'm a local variable of foo!"
--8<---------------cut here---------------end--------------->8---

> Recently, I found that Emacs Lisp does not have "closures" according
> to "GNU Emacs Lisp Reference Manual 2.8".

Yes, that's true.

> Here are a few questions:
>
> 1. Does the (lambda ...) in `current-word-markers' is a closure???

No.

> 3. If not, enlighten me what is a closure.

This real closure won't work in elisp:

--8<---------------cut here---------------start------------->8---
(defun make-adder (n)
  (lambda (m)
    (+ n m)))

(fset 'adder (make-adder 3))
(adder 3) ;; C-x C-e
==>
Debugger entered--Lisp error: (void-variable n)
  (+ n m)
  adder(3)
  eval((adder 3))
  eval-last-sexp-1(nil)
  eval-last-sexp(nil)
  call-interactively(eval-last-sexp nil nil)
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo
-- 
Richard Stallman can coerce meaningful data from /dev/null.





      parent reply	other threads:[~2008-05-31 16:06 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-29  6:32 elisp help: Getting the current word position. (closure?) cinsk
2008-05-29  7:48 ` Johan Bockgård
2008-05-31 16:06 ` Tassilo Horn [this message]

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=877idav4p2.fsf@baldur.tsdh.de \
    --to=tassilo@member.fsf.org \
    --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.