emacs-devel
[Top][All Lists]
Advanced

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

Re: How to avoid put-text-property setting buffer modified flag, and mes


From: Stefan Monnier
Subject: Re: How to avoid put-text-property setting buffer modified flag, and messing with the undo history?
Date: Thu, 09 Jun 2011 12:33:00 -0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

>> I'm trying to dynamically color the buffer using text properties. It
>> works as follows:
>> 
>        [snip]
>> 
>> 6. For coloring I use text properties, but they change buffer marking it
>> as modified and altering undo information, and I don't want that.

> Here is a snippet that i use for exactly the same purpose.
> I don't know if it is the best solution, but it works.

>    (let ((sbul buffer-undo-list)           ;; undo list for selected buffer
>          (bmp (buffer-modified-p))         ;; modify state
>          (buffer-read-only nil))           ;; allow change!
>        (put-text-property P1 P2 'face FACE);; change face (color)
>        (set-buffer-modified-p bmp)         ;; restore modify state
>        (setq buffer-undo-list sbul))       ;; restore undo

Compared to what with-silent-modifications does, here are the differences:

- if the code signal an error or `throw's during put-text-property, the
  buffer-undo-list might not be restored.
- same for buffer-modified flag.
- you use set-buffer-modified-p instead of restore-buffer-modified-p
  (minor optimization).
- you bind buffer-read-only rather than inhibit-read-only.
- many other details are missing such as binding buffer-file-name
  (which is needed to avoid file-lock checks) and deactivate-mark.

Just goes to show that getting it right is tricky and hence this rather
recent macro should have been added a long time ago.


        Stefan



reply via email to

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