all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How can I be notified whenever point of a buffer changed?
@ 2007-04-12  4:00 Ender
  2007-04-12  8:09 ` Kai Grossjohann
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Ender @ 2007-04-12  4:00 UTC (permalink / raw)
  To: help-gnu-emacs

Hi, guys,

I want a user defined function be called whenever point of a desired
buffer was changed, how can I do this stuff? Any help will be
appreciated.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How can I be notified whenever point of a buffer changed?
  2007-04-12  4:00 How can I be notified whenever point of a buffer changed? Ender
@ 2007-04-12  8:09 ` Kai Grossjohann
       [not found] ` <mailman.1978.1176365678.7795.help-gnu-emacs@gnu.org>
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Kai Grossjohann @ 2007-04-12  8:09 UTC (permalink / raw)
  To: help-gnu-emacs

"Ender" <oldcode@gmail.com> writes:

> I want a user defined function be called whenever point of a desired
> buffer was changed, how can I do this stuff?

The following info node explains the point-left and point-entered text
properties:

    (elisp)Special Properties

I found it like this:

M-x apropos RET point.*hook RET

This mentioned a variable inhibit-point-motion-hooks.  The
documentation of this variable mentions the point-left and
point-entered text properties.

So I went to the elisp info file (Emacs Lisp Reference), hit the i key
to search the index and entered point-left as the search string.

Kai

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How can I be notified whenever point of a buffer changed?
       [not found] ` <mailman.1978.1176365678.7795.help-gnu-emacs@gnu.org>
@ 2007-04-13  2:21   ` Ender
  2007-04-13 10:22     ` Kai Grossjohann
  0 siblings, 1 reply; 6+ messages in thread
From: Ender @ 2007-04-13  2:21 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 12, 4:09 pm, Kai Grossjohann <k...@emptydomain.de> wrote:
> The following info node explains the point-left and point-entered text
> properties:
>
>     (elisp)Special Properties
>
> I found it like this:
>
> M-x apropos RET point.*hook RET
>
> This mentioned a variable inhibit-point-motion-hooks.  The
> documentation of this variable mentions the point-left and
> point-entered text properties.
>
> So I went to the elisp info file (Emacs Lisp Reference), hit the i key
> to search the index and entered point-left as the search string.

Thanks Kai, you are so kindhearted and your message helps me a lot.
But I still have problem. I have tried with following elisp demo code:

(defun text-entered-hook (old-point new-point)
  (save-current-buffer
    (set-buffer (get-buffer-create "*scratch*"))
    (insert "From " (number-to-string old-point) " to " (number-to-
string new-point) "\n")))

(setq default-text-properties '(point-left nil))
(setq default-text-properties '(point-entered text-entered-hook))

After M-x eval-buffer,  I found two problem with the code:

1). Not all the point change will trigger text-entered-hook and I
can't find any rule of which kind of point changing will trigger the
hook. It confused me.
2). When the hook was triggered, it will be called twice. Yes I know
why this happens (info node of point-left/point-entered explained
clearly), but how can I fix it?

Thanks for your help again Kai, you are a good man. :-)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How can I be notified whenever point of a buffer changed?
  2007-04-13  2:21   ` Ender
@ 2007-04-13 10:22     ` Kai Grossjohann
  0 siblings, 0 replies; 6+ messages in thread
From: Kai Grossjohann @ 2007-04-13 10:22 UTC (permalink / raw)
  To: help-gnu-emacs

"Ender" <oldcode@gmail.com> writes:

> (defun text-entered-hook (old-point new-point)
>   (save-current-buffer
>     (set-buffer (get-buffer-create "*scratch*"))
>     (insert "From " (number-to-string old-point) " to " (number-to-
> string new-point) "\n")))
>
> (setq default-text-properties '(point-left nil))
> (setq default-text-properties '(point-entered text-entered-hook))

The first line sets default-text-properties to a value, and the second
line sets it to a new value.  The second line overwrites the effect of
the first line.

Did you mean the following?

(setq default-text-properties 
      '(point-left nil
        point-entered text-entered-hook))

> After M-x eval-buffer,  I found two problem with the code:
>
> 1). Not all the point change will trigger text-entered-hook and I
> can't find any rule of which kind of point changing will trigger the
> hook. It confused me.

Only when the text property changes, then the hook is called.  So you
would have to have two functions, and set the text property of the
first, third, fifth, ... character in the buffer to the first
function, and the text property of the second, fourth, sixth,
... character in the buffer to the second function.

Ayee.

I think these text properties were not the right solution to your
original problem.  Let me go back and reread your original posting.

> 2). When the hook was triggered, it will be called twice. Yes I know
> why this happens (info node of point-left/point-entered explained
> clearly), but how can I fix it?

I'm afraid you'll have to write your function so that it is harmless
that it is called twice.


Kai

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How can I be notified whenever point of a buffer changed?
  2007-04-12  4:00 How can I be notified whenever point of a buffer changed? Ender
  2007-04-12  8:09 ` Kai Grossjohann
       [not found] ` <mailman.1978.1176365678.7795.help-gnu-emacs@gnu.org>
@ 2007-04-13 10:25 ` Kai Grossjohann
       [not found] ` <mailman.2027.1176460480.7795.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 6+ messages in thread
From: Kai Grossjohann @ 2007-04-13 10:25 UTC (permalink / raw)
  To: help-gnu-emacs

"Ender" <oldcode@gmail.com> writes:

> I want a user defined function be called whenever point of a desired
> buffer was changed, how can I do this stuff? Any help will be
> appreciated.

Perhaps you have to go for post-command-hook (or pre-command-hook).
The function would record the last seen value of point, look whether
point has changed, and do its thing if so.  (Obviously, you would also
have to update the last seen value of point.)

(defvar kai-last-point nil)
(make-variable-buffer-local 'kai-last-point)

(defun kai-motion-hook ()
  (unless (equal (point) kai-last-point)
    (setq kai-last-point (point))
    ... do stuff here ...))

(add-hook 'post-command-hook 'kai-motion-hook)

This is untested.

Kai

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How can I be notified whenever point of a buffer changed?
       [not found] ` <mailman.2027.1176460480.7795.help-gnu-emacs@gnu.org>
@ 2007-04-17  7:39   ` Ender
  0 siblings, 0 replies; 6+ messages in thread
From: Ender @ 2007-04-17  7:39 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 13, 6:25 pm, Kai Grossjohann <k...@emptydomain.de> wrote:
> Perhaps you have to go for post-command-hook (or pre-command-hook).
> The function would record the last seen value of point, look whether
> point has changed, and do its thing if so.  (Obviously, you would also
> have to update the last seen value of point.)
>
> (defvar kai-last-point nil)
> (make-variable-buffer-local 'kai-last-point)
>
> (defun kai-motion-hook ()
>   (unless (equal (point) kai-last-point)
>     (setq kai-last-point (point))
>     ... do stuff here ...))
>
> (add-hook 'post-command-hook 'kai-motion-hook)
>
> This is untested.
>
> Kai

Thank you very much, It works! And thanks for your patience to replay
newbie's question although the question is stupid sometimes :)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-04-17  7:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-12  4:00 How can I be notified whenever point of a buffer changed? Ender
2007-04-12  8:09 ` Kai Grossjohann
     [not found] ` <mailman.1978.1176365678.7795.help-gnu-emacs@gnu.org>
2007-04-13  2:21   ` Ender
2007-04-13 10:22     ` Kai Grossjohann
2007-04-13 10:25 ` Kai Grossjohann
     [not found] ` <mailman.2027.1176460480.7795.help-gnu-emacs@gnu.org>
2007-04-17  7:39   ` Ender

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.