[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

elisp help: Getting the current word position. (closure?)

From: cinsk
Subject: elisp help: Getting the current word position. (closure?)
Date: Wed, 28 May 2008 23:32:39 -0700 (PDT)
User-agent: G2/1.0

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

(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'

(defun current-word-markers (&optional STRICT REALLY-WORD)
  "Return the position of the current word"
  (let ((old (symbol-function
        (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))
    (setq ret (current-word STRICT REALLY-WORD))
    (fset 'buffer-substring-no-properties old)
    (if ret
        (cons saved-start saved-end)

`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

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

Thank you in advance.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]