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

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

Need help for using a function as matcher in `font-lock-add-keywords'


From: Thorsten Jolitz
Subject: Need help for using a function as matcher in `font-lock-add-keywords'
Date: Tue, 16 Apr 2013 16:16:53 +0200
User-agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.3 (gnu/linux)

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





reply via email to

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