emacs-pretest-bug
[Top][All Lists]
Advanced

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

display property strangeness


From: Nick Roberts
Subject: display property strangeness
Date: Sun, 13 Mar 2005 21:29:49 +1300

Chong Yidong writes:
 > This is a strange one. Evaluate the following two functions. Both of
 > them should do the same thing: replace each of the first 3 letters in
 > the buffer with the letter "A".
 > 
 > (defun foo ()
 >   (interactive)
 >   (let ((pos '(1 2 3)))
 >     (while pos
 >       (put-text-property (car pos) (1+ (car pos)) 'display "A")
 >       (setq pos (cdr pos)))))
 > 
 > (defun bar ()
 >   (interactive)
 >   (progn (put-text-property 1 2 'display "A")
 >          (put-text-property 2 3 'display "A")
 >          (put-text-property 3 4 'display "A")))
 > 
 > The amazing thing is that the first one doesn't work. Open up a new,
 > empty buffer and enter the string
 > 
 > 12345
 > 
 > Type M-x foo. You end up with
 > 
 > A45
 > 
 > Now undo that, and type M-x bar. That gives you the correct result:
 > 
 > AAA45
 > 
 > Why does the first function give nonsensical results?

Display properties are very strange. I'm not sure what the "correct" result
is but foo and bar give *different* results. If you copy A45 or AAA45 into
another Emacs or an xterm, you should get 12345 in both cases. Thats because
you haven't changed the text, only the way it is displayed. If you place
the cursor just after the A in A45 you will have to type DEL three times
to delete it. For some reason the first construct places the three A's on
top of each other. I have had similar problems with put-text-property and
copy-sequence seems to solve them:

(defun foo ()
  (interactive)
  (let ((pos '(1 2 3)))
    (while pos
      (setq string (copy-sequence "A"))
      (put-text-property (car pos) (1+ (car pos)) 'display string)
      (setq pos (cdr pos)))))


Strangely, not using the while construct also works:

(defun foo ()
  (interactive)
  (let ((pos '(1 2 3)))
      (put-text-property (car pos) (1+ (car pos)) 'display "A")
      (setq pos (cdr pos))
      (put-text-property (car pos) (1+ (car pos)) 'display "A")
      (setq pos (cdr pos))
      (put-text-property (car pos) (1+ (car pos)) 'display "A")
      (setq pos (cdr pos))))

It presumably has something to do with the underlying pointers in C. As I say
I'm not sure which result is right but I guess both functions should give the
same answer.


Nick




reply via email to

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