hi,
i try to find out how to make 'emacs' highlite some text by a specified
number of characters.
example:
gray-black-grey-black.......
can anybody help me? got no answers in emacs newsgroups!
stephan
Stephan Reinberg <·········@dorf.rwth-aachen.de> writes:
< hi,
< i try to find out how to make 'emacs' highlite some text by a specified
< number of characters.
< example:
<
< gray-black-grey-black.......
<
< can anybody help me? got no answers in emacs newsgroups!
<
< stephan
Not too sure what you mean by number of characters. This might be
doable with `overlays'. They can be somewhat hairy because if you
happen to lose an overlay it's a real pain to find it again and your
buffer is all funkified green and purple with plenty of whatnots all
over the place.
Here is a quick rundown of overlays (as best I understand them).
(make-overlay start end buffer)
makes an overlay. This will do nothing but waste memory.
This on the otherhand,
(let ((overlay (make-overlay start end buffer)))
(put-overlay overlay 'face 'region)),
will change the face (color, underline, etc.) between START and END in
BUFFER to FACE.
Here's what I would do if I were trying to highlight rows.
(defvar *overlays* ())
(defmacro define-overlay (name row face)
"Define an active overlay called NAME for the entire row ROW using FACE"
(unless (integerp row)
(error "the row ROW must be an integer, not: %S" row))
`(progn
(defvar ,name (make-overlay (save-excursion
(goto-line ,row)
(point))
(save-excursion
(goto-line ,row)
(end-of-line)
(point))
(current-buffer)))
(overlay-put ,name 'evaporate t)
(overlay-put ,name 'window (selected-window))
(overlay-put ,name 'face ',face)
(push (cons ',name ,name) *overlays*)))
(defun kill-all-overlays ()
(dolist (overlay-list *overlays*)
(let ((name (car overlay-list))
(overlay (cdr overlay-list)))
(delete-overlay overlay)
;; help the garbage collector out
;; by removing the dead overlays/variables
(makunbound name)
(unintern name)))
(setq *overlays* ()))
You can then turn on color by doing something like the following.
(define-overlay *row-4* 4 region)
(define-overlay *row-6* 6 font-lock-type-face)
To remove all the overlays I would just eval the following.
(kill-all-overlays)
There is a way to have `defaults' for an overlay but I'm not too
familiar with it.
I used error for `row' because I don't feel like writing a `once-only'
macro in emacs lisp right now, it would probably not work very well
anyway. The `define-overlay' macro is fairly straight forward (if
you're familiar with macros).
(cl-prettyexpand '(define-overlay *row-4* 4 region))
=> [commentary is mine of course]
(progn
;; define a new variable `*row-4*'
(defvar *row-4*
;; create a new overlay and assign it to `*row-4*'
(make-overlay
;; start: beginning of line 4
(save-excursion
(goto-line 4)
(point))
;; end: end of line 4
(save-excursion
(goto-line 4)
(end-of-line)
(point))
;; optional buffer: current buffer
(current-buffer)))
;; The variable *row-4* has now been defined, just add some
;; properties. I like evaporate; there are others to choose from.
(overlay-put *row-4* 'evaporate t)
(overlay-put *row-4* 'window (selected-window))
(overlay-put *row-4* 'face 'region)
;; Now add the overlay `*row-4*' to the list *overlays*.
(setq *overlays* (cons (cons '*row-4* *row-4*) *overlays*)))
Hope this helps some. There is some info on overlays in the elisp
reference manual but surprisingly there aren't many (any?) examples.
I think there are some examples in isearch.el that may be helpful.