unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Syntax highlighting fails on copy-paste
@ 2011-05-13 12:27 Deniz Dogan
  2011-05-13 21:31 ` Alp Aker
  0 siblings, 1 reply; 3+ messages in thread
From: Deniz Dogan @ 2011-05-13 12:27 UTC (permalink / raw)
  To: help-gnu-emacs

I've started writing my first "real" major mode, which I call 
guitar-mode. The code in its entirety can be viewed here: 
http://paste.lisp.org/display/121977

This is how I have implemented some basic syntax highlighting for chords 
  (@L78):

(defvar guitar-font-lock-keywords
   '((guitar-chord-matcher . ((1 'guitar-note-face)
                              (2 'guitar-chord-variation-face))))
   "Font lock keywords for guitar-mode.")

This works pretty well when _writing_ in a guitar-mode buffer but when I 
paste something from e.g. the web browser, not everything gets syntax 
highlighted until I edit the content around the chords.  The 
chords/lyrics I've tested this with are these: 
http://tabs.ultimate-guitar.com/r/red_hot_chili_peppers/under_the_bridge_crd.htm

So to summarize: when I copy-paste those chords/lyrics into a buffer 
with guitar-mode, not everything gets syntax highlighted until I edit 
the content around the chords.

Why is this?

Thanks,
Deniz Dogan



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

* Re: Syntax highlighting fails on copy-paste
  2011-05-13 12:27 Syntax highlighting fails on copy-paste Deniz Dogan
@ 2011-05-13 21:31 ` Alp Aker
  2011-05-14  8:21   ` Deniz Dogan
  0 siblings, 1 reply; 3+ messages in thread
From: Alp Aker @ 2011-05-13 21:31 UTC (permalink / raw)
  To: help-gnu-emacs

> So to summarize: when I copy-paste those chords/lyrics into a buffer 
> with guitar-mode, not everything gets syntax highlighted until I edit 
> the content around the chords.
> 
> Why is this?

It’s not a problem with pasting.  Your function `guitar-chord-matcher’ 
isn’t defined correctly.  When font-lock calls a function to provide 
match data for keyword fontification, it calls that function repeatedly 
until either (a) point reaches the limit of the region being fontified, 
or (b) the matching function returns nil.  Your function returns nil 
when it encounters an invalid chord, stopping fontification at that 
point.  What you want is for `guitar-chord-matcher’ to keep searching 
if it encounters an invalid chord.  So, something like:

(defun guitar-chord-matcher (limit)
  (with-syntax-table guitar-mode-syntax-table
    (catch 'found
      (while (re-search-forward guitar-full-chord-regexp limit t)
        (when (save-match-data
                (guitar-valid-chord-p (match-string 0)))
          (throw 'found t))))))

Although catch forms are slow, so you'll probably want to code it 
differently.





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

* Re: Syntax highlighting fails on copy-paste
  2011-05-13 21:31 ` Alp Aker
@ 2011-05-14  8:21   ` Deniz Dogan
  0 siblings, 0 replies; 3+ messages in thread
From: Deniz Dogan @ 2011-05-14  8:21 UTC (permalink / raw)
  To: help-gnu-emacs

On 2011-05-13 23:31, Alp Aker wrote:
>> So to summarize: when I copy-paste those chords/lyrics into a buffer
>> with guitar-mode, not everything gets syntax highlighted until I edit
>> the content around the chords.
>>
>> Why is this?
>
> It’s not a problem with pasting.  Your function `guitar-chord-matcher’
> isn’t defined correctly.  When font-lock calls a function to provide
> match data for keyword fontification, it calls that function repeatedly
> until either (a) point reaches the limit of the region being fontified,
> or (b) the matching function returns nil.  Your function returns nil
> when it encounters an invalid chord, stopping fontification at that
> point.  What you want is for `guitar-chord-matcher’ to keep searching
> if it encounters an invalid chord.  So, something like:
>
> (defun guitar-chord-matcher (limit)
>    (with-syntax-table guitar-mode-syntax-table
>      (catch 'found
>        (while (re-search-forward guitar-full-chord-regexp limit t)
>          (when (save-match-data
>                  (guitar-valid-chord-p (match-string 0)))
>            (throw 'found t))))))
>
> Although catch forms are slow, so you'll probably want to code it
> differently.
>
>
>

Thank you, that makes sense!



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

end of thread, other threads:[~2011-05-14  8:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-05-13 12:27 Syntax highlighting fails on copy-paste Deniz Dogan
2011-05-13 21:31 ` Alp Aker
2011-05-14  8:21   ` Deniz Dogan

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).