all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Multi-line font-lock parser
@ 2009-08-10 19:19 Teemu Likonen
  2009-08-10 21:32 ` Thierry Volpiatto
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Teemu Likonen @ 2009-08-10 19:19 UTC (permalink / raw)
  To: help-gnu-emacs

I'd like to write font-lock code which highlights the first line that
(1) is non-empty and (2) does not start with a "#" comment character.
This requires some multi-line parsing so plain regular expressions won't
suffice.

I'm too stupid to understand how to implement this kind of parsing. I
can write a code which finds such a line but I don't know how to
integrate it to font-lock. I don't understand the (info "(elisp) Font
Lock Multiline") manual either. So I'd _really_ appreciate if someone
posted an example code here.


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

* Re: Multi-line font-lock parser
  2009-08-10 19:19 Multi-line font-lock parser Teemu Likonen
@ 2009-08-10 21:32 ` Thierry Volpiatto
       [not found] ` <mailman.4341.1249940353.2239.help-gnu-emacs@gnu.org>
  2009-08-18  1:29 ` Glenn Morris
  2 siblings, 0 replies; 7+ messages in thread
From: Thierry Volpiatto @ 2009-08-10 21:32 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,
Teemu Likonen <tlikonen@iki.fi> writes:

> I'd like to write font-lock code which highlights the first line that
> (1) is non-empty and (2) does not start with a "#" comment character.
> This requires some multi-line parsing so plain regular expressions won't
> suffice.
>
> I'm too stupid to understand how to implement this kind of parsing. I
> can write a code which finds such a line but I don't know how to
> integrate it to font-lock. I don't understand the (info "(elisp) Font
> Lock Multiline") manual either. So I'd _really_ appreciate if someone
> posted an example code here.
>
Did you try to use add-text-properties?
Something like:

,----
| (defun test-prop ()
|   (interactive)
|   (when (re-search-forward "^[a-z]+")
|     (beginning-of-line)
|     (add-text-properties (1- (point-at-bol)) (point-at-eol) '(font-lock-face font-lock-comment-face))))
`----

should work.

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France





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

* Re: Multi-line font-lock parser
       [not found] ` <mailman.4341.1249940353.2239.help-gnu-emacs@gnu.org>
@ 2009-08-11  4:28   ` Teemu Likonen
  2009-08-11  5:48     ` Thierry Volpiatto
       [not found]     ` <mailman.4364.1249970086.2239.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 7+ messages in thread
From: Teemu Likonen @ 2009-08-11  4:28 UTC (permalink / raw)
  To: help-gnu-emacs

On 2009-08-10 23:32 (+0200), Thierry Volpiatto wrote:
> Hi,
> Teemu Likonen <tlikonen@iki.fi> writes:
>
>> I'd like to write font-lock code which highlights the first line that
>> (1) is non-empty and (2) does not start with a "#" comment character.
>> This requires some multi-line parsing so plain regular expressions won't
>> suffice.
>>
>> I'm too stupid to understand how to implement this kind of parsing. I
>> can write a code which finds such a line but I don't know how to
>> integrate it to font-lock. I don't understand the (info "(elisp) Font
>> Lock Multiline") manual either. So I'd _really_ appreciate if someone
>> posted an example code here.
>>
> Did you try to use add-text-properties?
> Something like:
>
> ,----
> | (defun test-prop ()
> |   (interactive)
> |   (when (re-search-forward "^[a-z]+")
> |     (beginning-of-line)
> |     (add-text-properties (1- (point-at-bol)) (point-at-eol) '(font-lock-face font-lock-comment-face))))
> `----
>
> should work.

Thanks but I don't know where to start. Care to elaborate how to
integrate that with font-lock? I mean, who is going to run this
test-prop function?


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

* Re: Multi-line font-lock parser
  2009-08-11  4:28   ` Teemu Likonen
@ 2009-08-11  5:48     ` Thierry Volpiatto
       [not found]     ` <mailman.4364.1249970086.2239.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 7+ messages in thread
From: Thierry Volpiatto @ 2009-08-11  5:48 UTC (permalink / raw)
  To: help-gnu-emacs

Teemu Likonen <tlikonen@iki.fi> writes:

> On 2009-08-10 23:32 (+0200), Thierry Volpiatto wrote:
>> Hi,
>> Teemu Likonen <tlikonen@iki.fi> writes:
>>
>>> I'd like to write font-lock code which highlights the first line that
>>> (1) is non-empty and (2) does not start with a "#" comment character.
>>> This requires some multi-line parsing so plain regular expressions won't
>>> suffice.
>>>
>>> I'm too stupid to understand how to implement this kind of parsing. I
>>> can write a code which finds such a line but I don't know how to
>>> integrate it to font-lock. I don't understand the (info "(elisp) Font
>>> Lock Multiline") manual either. So I'd _really_ appreciate if someone
>>> posted an example code here.
>>>
>> Did you try to use add-text-properties?
>> Something like:
>>
>> ,----
>> | (defun test-prop ()
>> |   (interactive)
>> |   (when (re-search-forward "^[a-z]+")
>> |     (beginning-of-line)
>> |     (add-text-properties (1- (point-at-bol)) (point-at-eol) '(font-lock-face font-lock-comment-face))))
>> `----
>>
>> should work.
>
> Thanks but I don't know where to start. Care to elaborate how to
> integrate that with font-lock? I mean, who is going to run this
> test-prop function?
>
Why do you want to integrate something to fontlock?
If i understand well what you want to do e.g Adding properties to the
first line that don't start with "#" and is not an empty line, the
simple code above suffice.

,----
| X ==> point
| 
| # One line starting with #.
| 
| the line we want to add text properties to.
`----

If you start to X (point) just running M-x test-prop will highlight the
line you want.

Is it what you want to do?


-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France





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

* Re: Multi-line font-lock parser
       [not found]     ` <mailman.4364.1249970086.2239.help-gnu-emacs@gnu.org>
@ 2009-08-11  6:18       ` Teemu Likonen
  2009-08-11  7:55         ` Thierry Volpiatto
  0 siblings, 1 reply; 7+ messages in thread
From: Teemu Likonen @ 2009-08-11  6:18 UTC (permalink / raw)
  To: help-gnu-emacs

On 2009-08-11 07:48 (+0200), Thierry Volpiatto wrote:
> Why do you want to integrate something to fontlock?

To have it automatically updated when the buffer changes. I guess that's
what the font-lock is for. No?

> ,----
> | X ==> point
> | 
> | # One line starting with #.
> | 
> | the line we want to add text properties to.
> `----
>
> If you start to X (point) just running M-x test-prop will highlight
> the line you want.
>
> Is it what you want to do?

It's not all. The intend is to highlight buffer's first line which is
not empty nor start with # character. Editing the buffer should update
this automatically.

So about your example, let's say that I later remove the leading comment
character "# " from the third line. Now that changed line should
automatically highlighted and the same highlighting removed from other
lines. I assume such things can be done with font-locks?


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

* Re: Multi-line font-lock parser
  2009-08-11  6:18       ` Teemu Likonen
@ 2009-08-11  7:55         ` Thierry Volpiatto
  0 siblings, 0 replies; 7+ messages in thread
From: Thierry Volpiatto @ 2009-08-11  7:55 UTC (permalink / raw)
  To: help-gnu-emacs

Teemu Likonen <tlikonen@iki.fi> writes:

> On 2009-08-11 07:48 (+0200), Thierry Volpiatto wrote:
>> Why do you want to integrate something to fontlock?
>
> To have it automatically updated when the buffer changes. I guess that's
> what the font-lock is for. No?
>
>> ,----
>> | X ==> point
>> | 
>> | # One line starting with #.
>> | 
>> | the line we want to add text properties to.
>> `----
>>
>> If you start to X (point) just running M-x test-prop will highlight
>> the line you want.
>>
>> Is it what you want to do?
>
> It's not all. The intend is to highlight buffer's first line which is
> not empty nor start with # character. Editing the buffer should update
> this automatically.
>
> So about your example, let's say that I later remove the leading comment
> character "# " from the third line. Now that changed line should
> automatically highlighted and the same highlighting removed from other
> lines. I assume such things can be done with font-locks?

Not sure.

However you can use hook to do that (think to add 
(goto-char (point-min)) in test-prop):

(add-hook 'after-save-hook 'test-prop)

Each time you modify buffer and you hit C-x C-s, your line will be
highlighted.

Do the same for find-file.

To do that in real time, i don't know, except with a timer that check
you file all the n seconds.

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France





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

* Re: Multi-line font-lock parser
  2009-08-10 19:19 Multi-line font-lock parser Teemu Likonen
  2009-08-10 21:32 ` Thierry Volpiatto
       [not found] ` <mailman.4341.1249940353.2239.help-gnu-emacs@gnu.org>
@ 2009-08-18  1:29 ` Glenn Morris
  2 siblings, 0 replies; 7+ messages in thread
From: Glenn Morris @ 2009-08-18  1:29 UTC (permalink / raw)
  To: help-gnu-emacs

Teemu Likonen wrote:

> I'd like to write font-lock code which highlights the first line that
> (1) is non-empty and (2) does not start with a "#" comment character.
> This requires some multi-line parsing so plain regular expressions won't
> suffice.

The following is probably dumb, but may give you the idea.

(defun my-font-lock-example (limit)
  ;; We must move point, set match data, and return non-nil if there
  ;; is something to highlight.  We must only return nil when there
  ;; are no more matches before LIMIT.
  (when (and (re-search-forward "^[^#\n].*" limit t)
             (not (save-excursion
                    (save-match-data (re-search-backward "^[^#\n]" nil t 2)))))
    ;; Need to rescan if insert text anywhere before current match and
    ;; the start of the buffer. If delete the current match, need to
    ;; rescan up to the next match or the end. Yuck.
    (put-text-property (point-min)
                       (save-excursion
                         (save-match-data
                           (or (re-search-forward "^[^#\n].*" nil t)
                               (point-max)))) 'font-lock-multiline t)
    t))

(setq foo-font-lock-keywords
      '((my-font-lock-example . font-lock-warning-face)
        ("FOO" . font-lock-constant-face)))

(define-derived-mode foo-mode fundamental-mode "foo"
  "foo"
  (set (make-local-variable 'font-lock-defaults)
       '(foo-font-lock-keywords t)))


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

end of thread, other threads:[~2009-08-18  1:29 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-10 19:19 Multi-line font-lock parser Teemu Likonen
2009-08-10 21:32 ` Thierry Volpiatto
     [not found] ` <mailman.4341.1249940353.2239.help-gnu-emacs@gnu.org>
2009-08-11  4:28   ` Teemu Likonen
2009-08-11  5:48     ` Thierry Volpiatto
     [not found]     ` <mailman.4364.1249970086.2239.help-gnu-emacs@gnu.org>
2009-08-11  6:18       ` Teemu Likonen
2009-08-11  7:55         ` Thierry Volpiatto
2009-08-18  1:29 ` Glenn Morris

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.