unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Re: Using widgets and timers simultaneously
@ 2007-05-01 12:14 martin rudalics
  2007-05-01 13:30 ` Pierre Lorenzon
  0 siblings, 1 reply; 6+ messages in thread
From: martin rudalics @ 2007-05-01 12:14 UTC (permalink / raw)
  To: pollock.nageoire; +Cc: help-gnu-emacs

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

^ permalink raw reply	[flat|nested] 6+ messages in thread
[parent not found: <mailman.12.1177964905.32220.help-gnu-emacs@gnu.org>]
* Using widgets and timers simultaneously
@ 2007-04-30 20:21 Pierre Lorenzon
  0 siblings, 0 replies; 6+ messages in thread
From: Pierre Lorenzon @ 2007-04-30 20:21 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,

I.d like to use widgets and timers in conjunction. What I'd
like to be able to do is to set up a timer which will refresh a
certain widget periodically. Here is the code I wrote but
certainly I'd like to do more sophisticated tasks than simply
displaying the time !

--- Code ---

(defvar my-widget nil)
(defvar my-timer)

(define-widget
  'timed
  'string
  ""
  :value-to-internal
  (lambda (widget value) (current-time-string)))


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

(switch-to-buffer (get-buffer-create "*Clock*"))
(setq my-widget (widget-create 'timed))
(use-local-map widget-keymap)
(widget-setup)
(setq my-timer (run-at-time nil 5 'refresh))

--- End Code ---

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. 

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.

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. 

Bests

Pierre

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

end of thread, other threads:[~2007-05-08 13:11 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-01 12:14 Using widgets and timers simultaneously martin rudalics
2007-05-01 13:30 ` Pierre Lorenzon
2007-05-02  8:36   ` martin rudalics
2007-05-08 13:11     ` Pierre Lorenzon
     [not found] <mailman.12.1177964905.32220.help-gnu-emacs@gnu.org>
2007-05-01 16:26 ` Johan Bockgård
  -- strict thread matches above, loose matches on Subject: below --
2007-04-30 20:21 Pierre Lorenzon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).