unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* syntax highlighting on the fly
@ 2014-03-07 11:43 lee
  2014-03-07 12:20 ` Jambunathan K
  2014-03-07 12:24 ` Rainer M Krug
  0 siblings, 2 replies; 7+ messages in thread
From: lee @ 2014-03-07 11:43 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

is something available with emacs to do to syntax highlighting on the
fly?

"On the fly" means: You have a mode with syntax highlighting which does
most of the highlighting.  Now you work on a file (source code for some
program) in a buffer with your mode enabled, and you decide that for
this particular file, you want "foobar" to be highlighted.

You´d have to somehow tell emacs to do this, for example by adding a
comment like


// highlight: foobar


to your file.  You could also do it like


#define foobar 25


(since you´re using #define anyway) and have all occurances of "foobar"
highlighted.

It´s probably possible to write something for this, and I wouldn´t even
know where to start.  Perhaps it´s already built in and I only don´t
know about it?


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax highlighting on the fly
  2014-03-07 11:43 syntax highlighting on the fly lee
@ 2014-03-07 12:20 ` Jambunathan K
  2014-03-07 13:17   ` lee
  2014-03-07 12:24 ` Rainer M Krug
  1 sibling, 1 reply; 7+ messages in thread
From: Jambunathan K @ 2014-03-07 12:20 UTC (permalink / raw)
  To: help-gnu-emacs


(info "(emacs) Font Lock")

(info "(emacs) Highlight Interactively")

Put your cursor after the closing ")" and do C-x C-e.  Hint: Search for
FIXME in the first node.

lee <lee@yun.yagibdah.de> writes:

> Hi,
>
> is something available with emacs to do to syntax highlighting on the
> fly?
>
> "On the fly" means: You have a mode with syntax highlighting which does
> most of the highlighting.  Now you work on a file (source code for some
> program) in a buffer with your mode enabled, and you decide that for
> this particular file, you want "foobar" to be highlighted.
>
> You´d have to somehow tell emacs to do this, for example by adding a
> comment like
>
>
> // highlight: foobar
>
>
> to your file.  You could also do it like
>
>
> #define foobar 25
>
>
> (since you´re using #define anyway) and have all occurances of "foobar"
> highlighted.
>
> It´s probably possible to write something for this, and I wouldn´t even
> know where to start.  Perhaps it´s already built in and I only don´t
> know about it?




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax highlighting on the fly
  2014-03-07 11:43 syntax highlighting on the fly lee
  2014-03-07 12:20 ` Jambunathan K
@ 2014-03-07 12:24 ` Rainer M Krug
  2014-03-07 15:24   ` lee
  1 sibling, 1 reply; 7+ messages in thread
From: Rainer M Krug @ 2014-03-07 12:24 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1067 bytes --]

lee <lee@yun.yagibdah.de> writes:

> Hi,
>
> is something available with emacs to do to syntax highlighting on the
> fly?
>
> "On the fly" means: You have a mode with syntax highlighting which does
> most of the highlighting.  Now you work on a file (source code for some
> program) in a buffer with your mode enabled, and you decide that for
> this particular file, you want "foobar" to be highlighted.
>
> You´d have to somehow tell emacs to do this, for example by adding a
> comment like
>
>
> // highlight: foobar
>
>
> to your file.  You could also do it like
>
>
> #define foobar 25

I like this idea - add highlighting of certain words by using
file-local-variables.

Please keep us posted.

Rainer

>
>
> (since you´re using #define anyway) and have all occurances of "foobar"
> highlighted.
>
> It´s probably possible to write something for this, and I wouldn´t even
> know where to start.  Perhaps it´s already built in and I only don´t
> know about it?

-- 
Rainer M. Krug

email: RMKrug<at>gmail<dot>com

[-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax highlighting on the fly
  2014-03-07 12:20 ` Jambunathan K
@ 2014-03-07 13:17   ` lee
  0 siblings, 0 replies; 7+ messages in thread
From: lee @ 2014-03-07 13:17 UTC (permalink / raw)
  To: help-gnu-emacs

Jambunathan K <kjambunathan@gmail.com> writes:

> (info "(emacs) Font Lock")
>
> (info "(emacs) Highlight Interactively")
>
> Put your cursor after the closing ")" and do C-x C-e.  Hint: Search for
> FIXME in the first node.
>
> lee <lee@yun.yagibdah.de> writes:
>
>> Hi,
>>
>> is something available with emacs to do to syntax highlighting on the
>> fly?
>>
>> "On the fly" means: You have a mode with syntax highlighting which does
>> most of the highlighting.  Now you work on a file (source code for some
>> program) in a buffer with your mode enabled, and you decide that for
>> this particular file, you want "foobar" to be highlighted.
>>
>> You´d have to somehow tell emacs to do this, for example by adding a
>> comment like
>>
>>
>> // highlight: foobar
>>
>>
>> to your file.  You could also do it like
>>
>>
>> #define foobar 25
>>
>>
>> (since you´re using #define anyway) and have all occurances of "foobar"
>> highlighted.
>>
>> It´s probably possible to write something for this, and I wouldn´t even
>> know where to start.  Perhaps it´s already built in and I only don´t
>> know about it?

Thank you for the pointers!  Highlighting interactively (with
hi-lock-mode) is exactly what I´m looking for :)


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax highlighting on the fly
  2014-03-07 12:24 ` Rainer M Krug
@ 2014-03-07 15:24   ` lee
  2014-03-07 17:06     ` Drew Adams
  0 siblings, 1 reply; 7+ messages in thread
From: lee @ 2014-03-07 15:24 UTC (permalink / raw)
  To: help-gnu-emacs

Rainer M Krug <Rainer@krugs.de> writes:

> lee <lee@yun.yagibdah.de> writes:
>
>> Hi,
>>
>> is something available with emacs to do to syntax highlighting on the
>> fly?
>>
>> "On the fly" means: You have a mode with syntax highlighting which does
>> most of the highlighting.  Now you work on a file (source code for some
>> program) in a buffer with your mode enabled, and you decide that for
>> this particular file, you want "foobar" to be highlighted.
>>
>> You´d have to somehow tell emacs to do this, for example by adding a
>> comment like
>>
>>
>> // highlight: foobar
>>
>>
>> to your file.  You could also do it like
>>
>>
>> #define foobar 25
>
> I like this idea - add highlighting of certain words by using
> file-local-variables.
>
> Please keep us posted.

Provided that hi-lock-mode is enabled, the following might be useful.
Bind to a key if you like, perhaps C-x w .


(defun my-hi-lock-add ()
  "Add the symbol at point to the patterns highlighted through
hi-lock-mode; then write the current patterns to the beginning of
the file."
  (interactive)
  (let* ((regexp (hi-lock-regexp-okay (find-tag-default-as-symbol-regexp))))
    (hi-lock-set-pattern regexp font-lock-comment-delimiter-face))
  (save-excursion
    (beginning-of-buffer)
    (hi-lock-write-interactive-patterns)))


The point is that I wanted to use a particular face
(font-lock-comment-delimiter-face in this case) and not change the
defaults picked by hi-lock-mode (since they appear to be global).  The
added benefit is that the current patterns are automatically written to
the top of the file.

What bothers me a little is that if hi-lock.el.gz changes, my function
might cease to work.  Perhaps there´s a better way to do this?


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



^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: syntax highlighting on the fly
  2014-03-07 15:24   ` lee
@ 2014-03-07 17:06     ` Drew Adams
  2014-03-07 18:43       ` lee
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2014-03-07 17:06 UTC (permalink / raw)
  To: lee, help-gnu-emacs

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.

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




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: syntax highlighting on the fly
  2014-03-07 17:06     ` Drew Adams
@ 2014-03-07 18:43       ` lee
  0 siblings, 0 replies; 7+ messages in thread
From: lee @ 2014-03-07 18:43 UTC (permalink / raw)
  To: help-gnu-emacs

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.



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-03-07 18:43 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-07 11:43 syntax highlighting on the fly lee
2014-03-07 12:20 ` Jambunathan K
2014-03-07 13:17   ` lee
2014-03-07 12:24 ` Rainer M Krug
2014-03-07 15:24   ` lee
2014-03-07 17:06     ` Drew Adams
2014-03-07 18:43       ` lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).