help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Using widgets and timers simultaneously


From: martin rudalics
Subject: Re: Using widgets and timers simultaneously
Date: Tue, 01 May 2007 14:14:59 +0200
User-agent: Mozilla Thunderbird 1.0 (Windows/20041206)

> (defun refresh ()
>   (let ((pos (point)))
>     (widget-delete my-widget)
>     (setq my-widget (widget-create 'timed))
>     (goto-char pos))
>   (widget-setup))

It occurs to me that in `refresh' you set up `my-widget' in the buffer
that's current at that time, hence your widget might eventually appear
in all your buffers.

> Indeed it works i.e. time is redisplayed every 5 seconds. But
> when I try to move the cursor in the buffer the behavior might
> become some how erratic ! In fact especially after using up and
> down arrow keys, the widget is no longer well displayed. The
> formating characters appear "%{" "%[" or things like that just
> as if the delete-backward-char method in the creating method
> would not have any effect.

I recall that widgets may get screwed up when text is replaced at text
boundaries.  In principle you should make sure yourself that mutable
text is surrounded by immutable one.

> Can somebody explain me why ; and give me an indication to solve
> this question ? You certainly might say that widgets are not
> adapted to display parts of a buffer that must be periodically
> refreshed and that I should use something like overlays. Sure
> but it would be much more convenient for me if I could use
> widgets and moreover widgets displaying is  more or less based
> on overlays.

Zero-length overlays are a mess but you could try something like the
following:

(defvar my-clock-overlay)
(defvar my-clock-timer)

(defun my-refresh ()
  (when (overlayp my-clock-overlay)
    (overlay-put my-clock-overlay 'after-string (current-time-string))))

(with-current-buffer (get-buffer-create "*Clock*")
  (setq my-clock-overlay (make-overlay (point) (point) nil t))
  (setq my-clock-timer (run-at-time nil 5 'my-refresh)))

Experiencing with code like this you soon will encounter similar
problems as the widget library - but at least you don't have to
examine the code of wid-edit.el to resolve them ;-).

> Notice that if I replace the (let ((pos ...)) form in the
> refresh method by a save-excursion the cursor always goes at
> the beginning of the buffer as if all marks were killed by the
> widget displayin mechanism.

I suppose point-marker is relocated when you re-create 'timed.  It
inherently deletes the entire line and creates it anew.  Any markers
within the deleted region move to the front of it.





reply via email to

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