-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfont-lock+.el
490 lines (461 loc) · 21.9 KB
/
font-lock+.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
;;; font-lock+.el --- Enhancements to standard library `font-lock.el'.
;;
;; Filename: font-lock+.el
;; Description: Enhancements to standard library `font-lock.el'.
;; Author: Drew Adams
;; Maintainer: Drew Adams (concat "drew.adams" "@" "oracle" ".com")
;; Copyright (C) 2007-2022, Drew Adams, all rights reserved.
;; Created: Sun Mar 25 15:21:07 2007
;; Version: 0
;; Package-Requires: ()
;; Last-Updated: Sat Apr 2 11:24:39 2022 (-0700)
;; By: dradams
;; Update #: 233
;; URL: https://www.emacswiki.org/emacs/download/font-lock%2b.el
;; Doc URL: https://www.emacswiki.org/emacs/HighlightLibrary
;; Keywords: languages, faces, highlighting
;; Compatibility: GNU Emacs: 22.x, 23.x, 24.x, 25.x, 26.x
;;
;; Features that might be required by this library:
;;
;; `backquote', `bytecomp', `cconv', `cl-lib', `font-lock',
;; `macroexp', `syntax'.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Enhancements to standard library `font-lock.el'.
;;
;; This library tells font lock to ignore any text that has the text
;; property `font-lock-ignore'. This means, in particular, that font
;; lock will not erase or otherwise interfere with highlighting that
;; you apply using library `highlight.el'.
;;
;; Load this library after standard library `font-lock.el' (which
;; should be preloaded). Put this in your Emacs init file (~/.emacs):
;;
;; (require 'font-lock+)
;;
;;
;; Commands defined here:
;;
;; `font-lock-selection-off', `font-lock-selection-on',
;; `font-lock-selection-toggle'.
;;
;; Non-interactive functions defined here:
;;
;; `font-lock-selection--off/on',
;; `put-text-property-unless-ignore'.
;;
;;
;; ***** NOTE: The following functions defined in `font-lock.el'
;; have been REDEFINED HERE:
;;
;; `font-lock-append-text-property', `font-lock-apply-highlight',
;; `font-lock-apply-syntactic-highlight',
;; `font-lock-default-unfontify-region',
;; `font-lock-fillin-text-property',
;; `font-lock-fontify-anchored-keywords',
;; `font-lock-fontify-keywords-region',
;; `font-lock-fontify-syntactically-region',
;; `font-lock-prepend-text-property'.
;;
;; Suggested binding:
;;
;; (define-key ctl-x-map [(down-mouse-1)] 'font-lock-selection-toggle)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change Log:
;;
;; 2022/04/02 dadams
;; Added: font-lock-selection-off, font-lock-selection-on, font-lock-selection-toggle,
;; font-lock-selection--off/on.
;; 2014/08/30 dadams
;; Require cl.el when compile, for incf.
;; Load font-lock.el[c] when compile, for macro save-buffer-state.
;; font-lock-(prepend|append)-text-property:
;; Update for Emacs 24: Canonicalize old forms.
;; font-lock-fontify-syntactically-region: Updated for Emacs 24.
;; 2014/08/28 dadams
;; put-text-property-unless-ignore: Use next-single-property-change.
;; 2007/03/25 dadams
;; Created.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(require 'font-lock)
(eval-when-compile
(require 'cl) ;; incf
(load-library "font-lock")) ;; Macro save-buffer-state
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun font-lock-selection-toggle (start-event &optional msgp)
"Toggle font-locking for the text you select with the mouse."
(interactive "e\np")
(font-lock-selection--off/on start-event 'toggle msgp))
(defun font-lock-selection-off (start-event &optional msgp)
"Turn font-lock off for the text you select with the mouse."
(interactive "e\np")
(font-lock-selection--off/on start-event t msgp))
(defun font-lock-selection-on (start-event &optional msgp)
"Turn font-lock on for the text you select with the mouse."
(interactive "e\np")
(font-lock-selection--off/on start-event nil msgp))
(defun font-lock-selection--off/on (start-event &optional new-state msgp)
"Helper for commands that turn font-lock on/off for mouse selection."
(save-excursion
(run-hooks 'mouse-leave-buffer-hook) ; Let temporary modes like Isearch turn off.
(let* ((original-window (selected-window))
(echo-keystrokes 0)
(start-posn (event-start start-event))
(start-point (posn-point start-posn))
(end-point start-point)
(start-window (posn-window start-posn))
(new-state (if (eq 'toggle new-state)
(not (get-text-property start-point 'font-lock-ignore))
new-state)))
(with-current-buffer (window-buffer start-window)
(let ((read-only buffer-read-only)
(modified-p (buffer-modified-p))
(inhibit-modification-hooks t)
(font-lock-fontify-region-function 'ignore) ; Else `put-text-property' calls it.
event)
(setq buffer-read-only nil)
(track-mouse
(while (progn (setq event (read-event))
(or (mouse-movement-p event)
(memq (car-safe event) '(switch-frame select-window))))
(unless (memq (car-safe event) '(switch-frame select-window))
(setq end-point (posn-point (event-end event))))
(unless (integer-or-marker-p end-point)
(funcall (if (fboundp 'user-error) #'user-error #'error)
"Mouse dragged out of window"))
(font-lock-default-unfontify-region start-point end-point)
(put-text-property start-point end-point 'font-lock-ignore new-state)))
(setq buffer-read-only read-only)
(set-buffer-modified-p modified-p)))
(font-lock-mode) (font-lock-mode)
(when msgp (message "Font-lock now %s the text you selected"
(if new-state 'IGNORES 'HANDLES))))))
(defun put-text-property-unless-ignore (start end property value &optional object)
"`put-text-property', but ignore text with property `font-lock-ignore'."
(let ((here (min start end))
(end1 (max start end))
chg)
(while (< here end1)
(setq chg (next-single-property-change here 'font-lock-ignore object end1))
(unless (get-text-property here 'font-lock-ignore object)
(put-text-property here chg property value object))
(setq here chg))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Don't unfontify any text that has property `font-lock-ignore'.
;;
(defun font-lock-default-unfontify-region (beg end)
"Unfontify from BEG to END, except text with property `font-lock-ignore'."
(let ((here (min beg end))
(end1 (max beg end))
chg)
(while (< here end1)
(setq chg (next-single-property-change here 'font-lock-ignore nil end1))
(unless (get-text-property here 'font-lock-ignore)
(remove-list-of-text-properties
here chg (append font-lock-extra-managed-props
(if font-lock-syntactic-keywords
'(syntax-table face font-lock-multiline)
'(face font-lock-multiline)))))
(setq here chg))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Use `put-text-property-unless-ignore' instead of `put-text-property'.
;;
(defun font-lock-prepend-text-property (start end prop value &optional object)
"Prepend to one property of the text from START to END.
Arguments PROP and VALUE specify the property and value to prepend to the value
already in place. The resulting property values are always lists.
Optional argument OBJECT is the string or buffer containing the text."
(let ((val (if (listp value) value (list value)))
next prev)
(while (/= start end)
(setq next (next-single-property-change start prop object end)
prev (get-text-property start prop object))
;; Canonicalize old forms of face property.
(and (memq prop '(face font-lock-face)) (listp prev)
(or (keywordp (car prev)) (memq (car prev)
'(foreground-color background-color)))
(setq prev (list prev)))
(put-text-property-unless-ignore start next prop
(append val (if (listp prev) prev (list prev)))
object)
(setq start next))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Use `put-text-property-unless-ignore' instead of `put-text-property'.
;;
(defun font-lock-append-text-property (start end prop value &optional object)
"Append to one property of the text from START to END.
Arguments PROP and VALUE specify the property and value to append to the value
already in place. The resulting property values are always lists.
Optional argument OBJECT is the string or buffer containing the text."
(let ((val (if (listp value) value (list value)))
next prev)
(while (/= start end)
(setq next (next-single-property-change start prop object end)
prev (get-text-property start prop object))
;; Canonicalize old forms of face property.
(and (memq prop '(face font-lock-face)) (listp prev)
(or (keywordp (car prev)) (memq (car prev)
'(foreground-color background-color)))
(setq prev (list prev)))
(put-text-property-unless-ignore start next prop
(append (if (listp prev) prev (list prev)) val)
object)
(setq start next))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Use `put-text-property-unless-ignore' instead of `put-text-property'.
;;
(defun font-lock-fillin-text-property (start end prop value &optional object)
"Fill in one property of the text from START to END.
Arguments PROP and VALUE specify the property and value to put where none are
already in place. Therefore existing property values are not overwritten.
Optional argument OBJECT is the string or buffer containing the text."
(let ((start (text-property-any start end prop nil object))
next)
(while start
(setq next (next-single-property-change start prop object end))
(put-text-property-unless-ignore start next prop value object)
(setq start (text-property-any next end prop nil object)))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Use `put-text-property-unless-ignore' instead of `put-text-property'.
;;
(defun font-lock-apply-syntactic-highlight (highlight)
"Apply HIGHLIGHT following a match.
HIGHLIGHT should be of the form MATCH-HIGHLIGHT,
see `font-lock-syntactic-keywords'."
(let* ((match (nth 0 highlight))
(start (match-beginning match)) (end (match-end match))
(value (nth 1 highlight))
(override (nth 2 highlight)))
(if (not start)
;; No match but we might not signal an error.
(or (nth 3 highlight)
(error "No match %d in highlight %S" match highlight))
(when (and (consp value) (not (numberp (car value)))) (setq value (eval value)))
(when (stringp value) (setq value (string-to-syntax value)))
;; Flush the syntax-cache. I believe this is not necessary for
;; font-lock's use of syntax-ppss, but I'm not 100% sure and it can
;; still be necessary for other users of syntax-ppss anyway.
(syntax-ppss-after-change-function start)
(cond
((not override)
;; Cannot override existing fontification.
(or (text-property-not-all start end 'syntax-table nil)
(put-text-property-unless-ignore start end 'syntax-table value)))
((eq override t)
;; Override existing fontification.
(put-text-property-unless-ignore start end 'syntax-table value))
((eq override 'keep)
;; Keep existing fontification.
(font-lock-fillin-text-property start end 'syntax-table value))))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Use `put-text-property-unless-ignore' instead of `put-text-property'.
;;
;; (Parameter PPSS is used by Emacs 22 and 23, but not by Emacs 24.)
;;
(defun font-lock-fontify-syntactically-region (start end &optional loudly ppss)
"Put proper face on each string and comment between START and END.
START should be at the beginning of a line."
(when (fboundp 'syntax-propertize) ; Emacs 24+.
(syntax-propertize end)) ; Apply any needed syntax-table properties.
(let ((comment-end-regexp (or font-lock-comment-end-skip
(regexp-quote (replace-regexp-in-string
"^ *" "" comment-end))))
(state (or ppss (syntax-ppss start))) ; Find the `start' state.
face beg)
(when loudly (message "Fontifying %s... (syntactically...)" (buffer-name)))
(while (progn ; Find each interesting place between here and `end'.
(when (or (nth 3 state) (nth 4 state))
(setq face (funcall font-lock-syntactic-face-function state)
beg (max (nth 8 state) start)
state (parse-partial-sexp (point) end nil nil state
'syntax-table))
(when face (put-text-property-unless-ignore beg (point) 'face face))
(when (and (eq face 'font-lock-comment-face)
(or font-lock-comment-start-skip comment-start-skip))
;; Find the comment delimiters
;; and use font-lock-comment-delimiter-face for them.
(save-excursion
(goto-char beg)
(when (looking-at (or font-lock-comment-start-skip
comment-start-skip))
(put-text-property-unless-ignore
beg (match-end 0) 'face font-lock-comment-delimiter-face)))
(when (looking-back comment-end-regexp (point-at-bol) t)
(put-text-property-unless-ignore (match-beginning 0) (point) 'face
font-lock-comment-delimiter-face))))
(< (point) end))
(setq state (parse-partial-sexp (point) end nil nil state 'syntax-table)))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; 1. Use `put-text-property-unless-ignore' instead of `put-text-property'.
;; 2. Use `defun' instead of `defsubst', since we don't want to reproduce whole library.
;;
(defun font-lock-apply-highlight (highlight)
"Apply HIGHLIGHT following a match.
HIGHLIGHT should be of the form MATCH-HIGHLIGHT, see `font-lock-keywords'."
(let* ((match (nth 0 highlight))
(start (match-beginning match)) (end (match-end match))
(override (nth 2 highlight)))
(if (not start)
;; No match but we might not signal an error.
(or (nth 3 highlight) (error "No match %d in highlight %S" match highlight))
(let ((val (eval (nth 1 highlight))))
(when (eq (car-safe val) 'face)
(add-text-properties start end (cddr val))
(setq val (cadr val)))
(cond ((not (or val (eq override t)))
;; If `val' is nil, don't do anything. It is important to do it
;; explicitly, because when adding nil via things like
;; font-lock-append-text-property, the property is actually
;; changed from <face> to (<face>) which is undesirable. --Stef
nil)
((not override)
;; Cannot override existing fontification.
(unless (text-property-not-all start end 'face nil)
(put-text-property-unless-ignore start end 'face val)))
((eq override t)
;; Override existing fontification.
(put-text-property-unless-ignore start end 'face val))
((eq override 'prepend)
;; Prepend to existing fontification.
(font-lock-prepend-text-property start end 'face val))
((eq override 'append)
;; Append to existing fontification.
(font-lock-append-text-property start end 'face val))
((eq override 'keep)
;; Keep existing fontification.
(font-lock-fillin-text-property start end 'face val)))))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; 1. Use `put-text-property-unless-ignore' instead of `put-text-property'.
;; 2. Use `defun' instead of `defsubst', since we don't want to reproduce whole library.
;;
(defun font-lock-fontify-anchored-keywords (keywords limit)
"Fontify according to KEYWORDS until LIMIT.
KEYWORDS should be of the form MATCH-ANCHORED, see `font-lock-keywords',
LIMIT can be modified by the value of its PRE-MATCH-FORM."
(let ((matcher (nth 0 keywords))
(lowdarks (nthcdr 3 keywords))
(lead-start (match-beginning 0))
(pre-match-value (eval (nth 1 keywords))) ; Evaluate PRE-MATCH-FORM.
highlights)
;; Set LIMIT to value of PRE-MATCH-FORM or the end of line.
(if (not (and (numberp pre-match-value) (> pre-match-value (point))))
(setq limit (line-end-position))
(setq limit pre-match-value)
(when (and font-lock-multiline (>= limit (line-beginning-position 2)))
;; this is a multiline anchored match
;; (setq font-lock-multiline t)
(put-text-property-unless-ignore (if (= limit (line-beginning-position 2))
(1- limit)
(min lead-start (point)))
limit
'font-lock-multiline t)))
(save-match-data
;; Find an occurrence of `matcher' before `limit'.
(while (and (< (point) limit) (if (stringp matcher)
(re-search-forward matcher limit t)
(funcall matcher limit)))
;; Apply each highlight to this instance of `matcher'.
(setq highlights lowdarks)
(while highlights
(font-lock-apply-highlight (car highlights))
(setq highlights (cdr highlights)))))
;; Evaluate POST-MATCH-FORM.
(eval (nth 2 keywords))))
;; REPLACES ORIGINAL in `font-lock.el'.
;;
;; Use `put-text-property-unless-ignore' instead of `put-text-property'.
;;
(defun font-lock-fontify-keywords-region (start end &optional loudly)
"Fontify according to `font-lock-keywords' between START and END.
START should be at the beginning of a line.
LOUDLY, if non-nil, allows progress-meter bar."
(unless (eq (car font-lock-keywords) t)
(setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)))
(let ((case-fold-search font-lock-keywords-case-fold-search)
(keywords (cddr font-lock-keywords))
(bufname (buffer-name))
(count 0)
(pos (make-marker))
keyword matcher highlights)
;;
;; Fontify each item in `font-lock-keywords' from `start' to `end'.
(while keywords
(when loudly
(message "Fontifying %s... (regexps..%s)" bufname (make-string (incf count)
?.)))
;;
;; Find an occurrence of `matcher' from `start' to `end'.
(setq keyword (car keywords) matcher (car keyword))
(goto-char start)
(while (and (< (point) end)
(if (stringp matcher)
(re-search-forward matcher end t)
(funcall matcher end))
;; Beware empty string matches since they will
;; loop indefinitely.
(or (> (point) (match-beginning 0)) (progn (forward-char 1) t)))
(when (and font-lock-multiline
(>= (point) (save-excursion (goto-char (match-beginning 0))
(forward-line 1) (point))))
;; this is a multiline regexp match
;; (setq font-lock-multiline t)
(put-text-property-unless-ignore (if (= (point)
(save-excursion
(goto-char (match-beginning 0))
(forward-line 1) (point)))
(1- (point))
(match-beginning 0))
(point)
'font-lock-multiline t))
;; Apply each highlight to this instance of `matcher', which may be
;; specific highlights or more keywords anchored to `matcher'.
(setq highlights (cdr keyword))
(while highlights
(if (numberp (car (car highlights)))
(font-lock-apply-highlight (car highlights))
(set-marker pos (point))
(font-lock-fontify-anchored-keywords (car highlights) end)
;; Ensure forward progress. `pos' is a marker because anchored
;; keyword may add/delete text (this happens e.g. in grep.el).
(when (< (point) pos) (goto-char pos)))
(setq highlights (cdr highlights))))
(setq keywords (cdr keywords)))
(set-marker pos nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(provide 'font-lock+)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; font-lock+.el ends here