From: cinsk <cinsky@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: elisp help: Getting the current word position. (closure?)
Date: Wed, 28 May 2008 23:32:39 -0700 (PDT) [thread overview]
Message-ID: <693cc531-de12-476c-b82e-642d6e5910d0@u12g2000prd.googlegroups.com> (raw)
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.
next reply other threads:[~2008-05-29 6:32 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-29 6:32 cinsk [this message]
2008-05-29 7:48 ` elisp help: Getting the current word position. (closure?) Johan Bockgård
2008-05-31 16:06 ` Tassilo Horn
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=693cc531-de12-476c-b82e-642d6e5910d0@u12g2000prd.googlegroups.com \
--to=cinsky@gmail.com \
--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.