From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: cinsk Newsgroups: gmane.emacs.help Subject: elisp help: Getting the current word position. (closure?) Date: Wed, 28 May 2008 23:32:39 -0700 (PDT) Organization: http://groups.google.com Message-ID: <693cc531-de12-476c-b82e-642d6e5910d0@u12g2000prd.googlegroups.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1212181826 19051 80.91.229.12 (30 May 2008 21:10:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 30 May 2008 21:10:26 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri May 30 23:11:08 2008 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1K2BsO-0000w2-Jf for geh-help-gnu-emacs@m.gmane.org; Fri, 30 May 2008 23:11:04 +0200 Original-Received: from localhost ([127.0.0.1]:34332 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1K2Brc-0006fN-Rm for geh-help-gnu-emacs@m.gmane.org; Fri, 30 May 2008 17:10:16 -0400 Original-Path: news.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!postnews.google.com!u12g2000prd.googlegroups.com!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 89 Original-NNTP-Posting-Host: 210.94.41.89 Original-X-Trace: posting.google.com 1212042759 11643 127.0.0.1 (29 May 2008 06:32:39 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Thu, 29 May 2008 06:32:39 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u12g2000prd.googlegroups.com; posting-host=210.94.41.89; posting-account=njEU7goAAACogg_6wI_bgyzQXAw4yM6w User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux i686; ko; rv:1.8.1.12) Gecko/20080306 Firefox/2.0.0.12,gzip(gfe),gzip(gfe) X-HTTP-Via: 1.1 Samsung_proxy5:8080 (DataReactor/4.0.6) Original-Xref: news.stanford.edu gnu.emacs.help:158989 X-Mailman-Approved-At: Fri, 30 May 2008 17:09:59 -0400 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:54369 Archived-At: 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.