* elisp help: Getting the current word position. (closure?)
@ 2008-05-29 6:32 cinsk
2008-05-29 7:48 ` Johan Bockgård
2008-05-31 16:06 ` Tassilo Horn
0 siblings, 2 replies; 3+ messages in thread
From: cinsk @ 2008-05-29 6:32 UTC (permalink / raw)
To: help-gnu-emacs
Is there any clean way to get the position of the current word?
I implement my own function to do this, and it is working. But I'm
afraid that my implementation is not a nice way to do what I want.
Could somebody give me some advice on this?
I wanted to get the position of the beginning/end of the current word
to insert some text around the current word. While experimenting
various functions, I found `current-word' that returns the word that
point is on. After reading it's definition in simple.el, I decided
that the implementing something similar to `current-word' is beyond my
ability. So I want to use current-word to get the position of the
current word.
My `current-word' definition looks like (GNU Emacs 22.2.9999 on
Gentoo):
(defun current-word (&optional ...)
(let* ((start (point))
(end (point))
...)
;; Calculating the proper value of START and END.
(unless (= start end)
(buffer-substring-no-properties start end))))
My idea is to redefine `buffer-substring-no-properties' so that I can
save the START and END value passed to
`buffer-substring-no-properties' in the end of the `current-word'
definition.
(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)))
`current-word-markers' returns a dotted pair with two markers; one for
the beginning of the current word, and another for the end of the
current word.
It redefines `buffer-substring-no-properties' so that new function can
save the passed START and END value somewhere in
`current-word-markers', and call the original function body using
`funcall'. After getting what are needed, `current-word-markers'
restore the original function body of
`buffer-substring-no-properties'.
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).
Recently, I found that Emacs Lisp does not have "closures" according
to "GNU Emacs Lisp Reference Manual 2.8".
Here are a few questions:
1. Does the (lambda ...) in `current-word-markers' is a closure???
3. If not, enlighten me what is a closure.
2. If yes, is my function is safe?
3. Anyway, is there any better way (or predefined function) to do
this?
Thank you in advance.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: elisp help: Getting the current word position. (closure?)
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
1 sibling, 0 replies; 3+ messages in thread
From: Johan Bockgård @ 2008-05-29 7:48 UTC (permalink / raw)
To: help-gnu-emacs
cinsk <cinsky@gmail.com> writes:
> 3. Anyway, is there any better way (or predefined function) to do
> this?
(bounds-of-thing-at-point 'word)
--
Johan Bockgård
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: elisp help: Getting the current word position. (closure?)
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
1 sibling, 0 replies; 3+ messages in thread
From: Tassilo Horn @ 2008-05-31 16:06 UTC (permalink / raw)
To: help-gnu-emacs
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-05-31 16:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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.