2

I am not sure whether this is just a simple mistake of my code. But I just cannot figure out what's the problem is, so please point it out.

aaaaa

(progn 
  (setq ol-list nil) 
  (dolist (pos '(1 2 3 4))
    (let ( (ol (make-overlay pos (1+ pos) (current-buffer))) )
      (overlay-put ol 'display "X")
      (print ol)
      (setq ol-list
            (nconc ol-list (list ol)))
      );; let
    )  ;; dolist
  ) 


(progn 
  (dolist (ol ol-list)
    (delete-overlay ol))
  (setq ol-list nil) ) 

Put the above code snippet at the beginning your "lisp-mode" buffer, and eval (C-x C-e) each progn section.

On my emacs, the first code section will make the "aaaaa" to "Xa". But I think the result should be "XXXXa". So where is the problem?

winterTTr
  • 1,739
  • 1
  • 10
  • 30

1 Answers1

5

See the Emacs Lisp manual, section 38.15.1:

For replacing display specifications, “the text that has the property” means all the consecutive characters that have the same Lisp object as their display property; these characters are replaced as a single unit.

The manual goes on to explain how to do what you want. You need to allocate a different string for each overlay. Something like this:

(overlay-put ol 'display (concat "X"))

I also have a couple of suggestions to make your code more Lisp-like:

  • There's no need to put closing parentheses on their own line (see this answer). Emacs shows you matching parentheses, and does automatic indentation, so just leave the closing parentheses where they fall.

  • Your loops will look nicer if you use the loop facility and mapcar respectively.

(require 'cl)

(setq ol-list
  (loop for pos from 1 upto 4
        collect (let ((ol (make-overlay pos (1+ pos))))
                  (overlay-put ol 'display (concat "X"))
                  ol)))

(mapcar #'delete-overlay ol-list)
Community
  • 1
  • 1
Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
  • Well, thanks so much for the answer as well as sincere suggestion. I don't know the rule about "replacing display" before. This helps me a lot. – winterTTr Jul 12 '11 at 12:05