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

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

Re: syntax highlighting on the fly


From: lee
Subject: Re: syntax highlighting on the fly
Date: Fri, 07 Mar 2014 19:43:43 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Drew Adams <drew.adams@oracle.com> writes:

> See http://stackoverflow.com/a/21997150/729907, which is about
> highlighting the symbol at point or under the mouse pointer.
> There are multiple approaches with different advantages.

Thanks!  Those don´t really seem to be what I´m looking for, though.

> (Remember too that `C-s C-w C-w...' will also highlight
> occurrences of text at point, albeit transiently.)

Hm, I have bound C-s to isearch-forward-regexp ...


So far, hi-lock-mode is great.  I have found that when writing new
patterns to the buffer, they first need to be removed to avoid adding
them multiple times.  This has lead to:


(defun lsl-hi-lock-remove ()
  "Remove hi-lock-mode patterns from the beginning of the buffer.

You may want to customize hi-lock-file-patterns-range when you
have many patterns."
  (interactive)
  (let ((startpos nil)
        (target-regexp (concat "\\<" hi-lock-file-patterns-prefix ":")))
    (save-excursion
      (save-restriction
        (widen)
        (goto-char (point-min))
        (re-search-forward target-regexp
                           (+ (point) hi-lock-file-patterns-range) t)
        (beginning-of-line)
        (setq startpos (point))
        (while (and (re-search-forward target-regexp (+ (point) 100) t)
                    (not (looking-at "\\s-*end")))
          (end-of-line)
          (condition-case nil
              (delete-region startpos (point))
            (error (message "Invalid pattern list expression at %d" 
(line-number-at-pos)))))))))


(defun lsl-hi-lock-add (whichface)
  "Add the symbol at point to the patterns highlighted through
hi-lock-mode; then write the current patterns to the beginning of
the file.

The argument whichface specifies which face to use for the
highlighting.

You may want to customize hi-lock-file-patterns-range when you
have many patterns."
  (lsl-hi-lock-remove)
  (let* ((regexp (hi-lock-regexp-okay (find-tag-default-as-symbol-regexp))))
    (hi-lock-set-pattern regexp whichface))
  (save-excursion
    (goto-char (point-min))
    (hi-lock-write-interactive-patterns)))


(defun lsl-hi-lock-constant ()
  (interactive)
  (lsl-hi-lock-add font-lock-comment-delimiter-face))


(defun lsl-hi-lock-functionlike ()
  (interactive)
  (lsl-hi-lock-add font-lock-warning-face))


I have bound (lsl-hi-lock-constant) and (lsl-hi-lock-functionlike) to
C-w c and C-w f, respectively.


But there is a problem:  When I restart emacs and visit a buffer that
has hi-lock patterns at the beginning, they are applied just fine, but
they cannot be written to the buffer again.  It´s like hi-lock-mode
doesn´t know that they exist, though the highlighting works as expected.

Only newly created patterns can be written to the buffer.  That means
when I use (lsl-hi-lock-constant), the existing patterns are removed and
only the new one is added.

Is there a way to make the existing patterns, as specified in the
buffer, known to hi-lock-mode in such a way that they can be added to
the buffer again like the new ones can with
(hi-lock-write-interactive-patterns)?

Is this a bug?  Isn´t hi-lock-mode supposed to know which patterns are
in use and to be able to write them to the buffer?


And another question: Is there a way to apply the patterns for a number
of buffers?  Suppose I work on some project which involves several
files.  Some of these files use a constant "foobar".  Now I would have
to add to all of these files a pattern to highlight "foobar".  How do I
make it so that, let´s say, hi-lock-mode reads the patterns from a
common file that holds all patterns relevant for the project so that I
don´t need to add them to every file in question?

Please don´t tell me that what I´m looking for doesn´t exist ...


-- 
Knowledge is volatile and fluid.  Software is power.



reply via email to

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