help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: How to programmatically highlight region?


From: Ehud Karni
Subject: Re: How to programmatically highlight region?
Date: Wed, 22 Nov 2006 15:36:33 +0200

On Wed, 22 Nov 2006 10:38:18 Mathias Dahl wrote:
>
> "Mirko" <mvukovic@nycap.rr.com> writes:
>
> > If I have a string such as "self.foo" I would like to see only
> > foo(underlined), i.e. make the "self." invisible, and change the
> > appearance of foo.
> >
> > I think I understand how I could change the appearance of foo, but
> > the info on invisibility left me confused as how I can hide text.
>
> Then you could experiment with the invisible-propery of faces.

If it a temporary way of showing it (a_la isearch) I would have
used overlays, Something like:

(let ((ov (make-overlay BEG END)))
    (overlay-put ov 'face FACE)        ;; change appearance
    (overlay-put ov 'priority 99))

(let ((ov (make-overlay BEG END)))
    (overlay-put ov 'invisible t)      ;; make invisible
    (overlay-put ov 'priority 99))

You should accumulate the overlays in a list. To undo all your changes
(face/invisibility) just do: (mapc 'delete-overlay your-overlay-list)

This is almost how it is done in isearch.el (the deletion of the
overlays is less efficient).

A real working example is bellow.

Ehud.

;; ================ Hi(ghlight)-mark 
======================================================

(defvar himark-overlay-list nil "list of high-mark overlays (himark-unset 
deletes them).")
(make-variable-buffer-local 'himark-overlay-list)
(defvar himark-overlay-face 'highlight "Name of face (quoted symbol) to use for 
himark.
e.g. (setq himark-overlay-face 'modeline)
Use list-faces-display to see all available faces")

(defun himark-unset () "Remove himark overlay"
  (interactive)
       (and faces-on
            (mapc 'delete-overlay himark-overlay-list)
            (setq himark-overlay-list nil)))


(defun himark-set () "Highlight all occurrence of string in this buffer)"
  (interactive)
       (let (ov
             (pos (point))
             (string (read-string "String to highlight: ")))
           (goto-char (point-min))
           (while (search-forward string nil t)
               (setq ov (make-overlay (match-beginning 0)
                                      (match-end 0)))
               (overlay-put ov 'face himark-overlay-face)
               (overlay-put ov 'priority 98)
               (push ov himark-overlay-list))
           (goto-char pos)))


--
 Ehud Karni           Tel: +972-3-7966-561  /"\
 Mivtach - Simon      Fax: +972-3-7966-667  \ /  ASCII Ribbon Campaign
 Insurance agencies   (USA) voice mail and   X   Against   HTML   Mail
 http://www.mvs.co.il  FAX:  1-815-5509341  / \
 GnuPG: 98EA398D <http://www.keyserver.net/>    Better Safe Than Sorry




reply via email to

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