all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Need help for using a function as matcher in `font-lock-add-keywords'
@ 2013-04-16 14:16 Thorsten Jolitz
  2013-04-16 15:57 ` Thorsten Jolitz
  0 siblings, 1 reply; 2+ messages in thread
From: Thorsten Jolitz @ 2013-04-16 14:16 UTC (permalink / raw)
  To: help-gnu-emacs


Hi List, 

to avoid problems with extreme slowdown during buffer fontification, I
try to 

- do a simple regexp search for outline-regexp first
- then calculate the outline-level of a matched headline
- then determine the right font for that headline-level

It seemed to me that using a function as matcher in
`font-lock-add-keywords' would be the right thing in this case, but what
I have until now does not work - the function is actually never entered
and thus fontification doesn't happen. 

I'm not experienced with the fontification mechanism of Emacs and would
be grateful for any hint what could be the problem. 

This is what I got right now:

,---------------------------------------------------------------------
| (defvar outshine-level-X "1"
|   "Stores headline level of matched headline")
| (make-variable-buffer-local 'outshine-level-X)
| 
| ;; adapted from http://tinyurl.com/cwad2km
| (defun outshine-match-next-headline (limit)
|   "Function to be used as matcher in `font-lock-add-keywords'.
| Matcher can be either the regexp to search for, or the function
| name to call to make the search (called with one argument, the
| LIMIT of the search; it should return non-nil, move point, and
| set match-data' appropriately if it succeeds like
| `re-search-forward' would)."
|   (let ((start (point))
|         (outshine-fontify-whole-heading-line ""))
|     ;; Look for outline-regexp
|     (when (re-search-forward (outshine-calc-outline-regexp) limit t)
|       (let ((level (outshine-calc-outline-level)))
|         ;; goto beginning of headline text
|         (goto-char (match-end 0))
|         ;; match headline
|         (looking-at (concat
|                      "\\(.*\\)"
|                      "\\("
|                      outshine-outline-heading-end-regexp
|                      "\\)"))
|         ;; move point
|         (forward-line)
|         ;; set variable used to determine outline-level
|         (setq outshine-level-X level)))))
| 
| 
| ;; Org-style highlighting of the headings
| (defun outshine-fontify-headlines (outline-regexp)
|   "Fontify headline-levels 1-8 with Org-mode like colors."
|   (let ((header-level
|          (string-to-number
|           outshine-level-X)))
|   (font-lock-add-keywords
|    nil
|    `((outshine-match-next-headline
|       1
|       ,(cond
|         ((eq header-level 1) 'outshine-level-1)
|         ((eq header-level 2) 'outshine-level-2)
|         ((eq header-level 3) 'outshine-level-3)
|         ((eq header-level 4) 'outshine-level-4)
|         ((eq header-level 5) 'outshine-level-5)
|         ((eq header-level 6) 'outshine-level-6)
|         ((eq header-level 7) 'outshine-level-7)
|         ((eq header-level 8) 'outshine-level-8))
|       t)))))
`---------------------------------------------------------------------

-- 
cheers,
Thorsten





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

* Re: Need help for using a function as matcher in `font-lock-add-keywords'
  2013-04-16 14:16 Need help for using a function as matcher in `font-lock-add-keywords' Thorsten Jolitz
@ 2013-04-16 15:57 ` Thorsten Jolitz
  0 siblings, 0 replies; 2+ messages in thread
From: Thorsten Jolitz @ 2013-04-16 15:57 UTC (permalink / raw)
  To: help-gnu-emacs

Thorsten Jolitz <tjolitz@gmail.com> writes:

> - do a simple regexp search for outline-regexp first
> - then calculate the outline-level of a matched headline
> - then determine the right font for that headline-level
>
> It seemed to me that using a function as matcher in
> `font-lock-add-keywords' would be the right thing in this case, but what
> I have until now does not work - the function is actually never entered
> and thus fontification doesn't happen. 

after some modification of the code I posted I now get this  as result
of 'font-lock-add-key-words':

,------------------------------------------------------------
| Result: (outshine-match-next-headline 0 outshine-level-1 t)
`------------------------------------------------------------

but fontification does not work, 'outshine-match-next-headline' is never
entered, and I get the following error:

,-------------------------------------------------------------------------------
| Error during redisplay: (jit-lock-function 1) signaled (invalid-function 1)
| Error during redisplay: (jit-lock-function 531) signaled (invalid-function 1)
| Error during redisplay: (jit-lock-function 1050) signaled (invalid-function 1)
| Error during redisplay: (jit-lock-function 1555) signaled (invalid-function 1)
| Error during redisplay: (jit-lock-function 2066) signaled (invalid-function 1)
`-------------------------------------------------------------------------------

Is there a syntax-error in this line? 

,----------------------------------------------------
| (outshine-match-next-headline 0 outshine-level-1 t)
`----------------------------------------------------

Examples given in 
(info "(elisp)Search-based Fontification")
look similar:

,-------------------------------
| ("foo\\|bar" 0 foo-bar-face t)
`-------------------------------

-- 
cheers,
Thorsten




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

end of thread, other threads:[~2013-04-16 15:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-04-16 14:16 Need help for using a function as matcher in `font-lock-add-keywords' Thorsten Jolitz
2013-04-16 15:57 ` Thorsten Jolitz

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.