annotate lisp/fast-lock.el @ 11284:0340dba1df7e

(prepare_menu_bars): Do the frame titles last.
author Richard M. Stallman <rms@gnu.org>
date Sat, 08 Apr 1995 00:19:28 +0000
parents ea9dda158056
children 8e3d4f515bbb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10243
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
1 ;;; fast-lock.el --- Automagic text properties saving for fast font-lock-mode.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
2
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1994 Free Software Foundation, Inc.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
4
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
5 ;; Author: Simon Marshall <Simon.Marshall@mail.esrin.esa.it>
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
6 ;; Keywords: faces files
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
7 ;; Version: 3.05
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
8
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
9 ;;; This file is part of GNU Emacs.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
10
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
14 ;; any later version.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
15
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
20
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
24
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
25 ;;; Commentary:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
26
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
27 ;; Purpose:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
28 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
29 ;; To make visiting a file in `font-lock-mode' faster by restoring its face
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
30 ;; text properties from automatically saved associated font lock cache files.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
31 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
32 ;; See also the face-lock package.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
33 ;; See also the lazy-lock package. (But don't use the two at the same time!)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
34
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
35 ;; Note that:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
36 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
37 ;; - A cache will be saved when visiting a compressed file using crypt++, but
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
38 ;; not be read. This is a "feature"/"consequence"/"bug" of crypt++.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
39
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
40 ;; Installation:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
41 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
42 ;; Put this file somewhere where Emacs can find it (i.e., in one of the paths
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
43 ;; in your `load-path'), `byte-compile-file' it, and put in your ~/.emacs:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
44 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
45 ;; (autoload 'turn-on-fast-lock "fast-lock"
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
46 ;; "Unconditionally turn on Fast Lock mode.")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
47 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
48 ;; (add-hook 'font-lock-mode-hook 'turn-on-fast-lock)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
49 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
50 ;; Start up a new Emacs and use font-lock as usual (except that you can use the
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
51 ;; so-called "gaudier" fontification regexps on big files without frustration).
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
52 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
53 ;; When you visit a file (which has `font-lock-mode' enabled) that has a
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
54 ;; corresponding font lock cache file associated with it, the font lock cache
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
55 ;; will be loaded from that file instead of being generated by font-lock code.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
56 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
57 ;; Font lock caches will be saved:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
58 ;; - For all buffers with Fast Lock mode enabled when you exit from Emacs.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
59 ;; - For a buffer with Fast Lock mode enabled when you kill the buffer.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
60 ;; To provide control over how such cache files are written automagically, see
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
61 ;; variable `fast-lock-cache-directories'. To provide control over which such
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
62 ;; cache files are written, see variables `fast-lock-save-others' and
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
63 ;; `fast-lock-save-size'. Only cache files which were generated using the same
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
64 ;; `font-lock-keywords' as you are using will be used.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
65 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
66 ;; As an illustration of the time saving, a 115k file of Emacs C code took 95
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
67 ;; seconds to fontify using Emacs 19.25 on a Sun SparcStation 2 LX (using
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
68 ;; `c-font-lock-keywords-2'). The font lock cache file takes around 2 seconds
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
69 ;; to load (with around 4 seconds to generate and save). Bite it. Believe it.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
70 ;; (For Lucid Emacs 19.10, the figures on a similar machine, file, and regexps,
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
71 ;; are 70, around 4, and around 4, seconds, respectively; for Emacs 19.28 the
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
72 ;; fontification takes around 26 seconds.)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
73
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
74 ;; Feedback:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
75 ;;
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
76 ;; Please send me bug reports, bug fixes, and extensions, so that I can
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
77 ;; merge them into the master source.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
78 ;; - Simon Marshall (Simon.Marshall@mail.esrin.esa.it)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
79
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
80 (require 'font-lock)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
81
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
82 (eval-when-compile
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
83 ;; Shut Emacs' byte-compiler up (cf. stop me getting mail from users).
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
84 (setq byte-compile-warnings '(free-vars callargs redefine)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
85
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
86 ;; User variables:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
87
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
88 (defvar fast-lock-cache-directories '("." "~/.emacs-flc")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
89 ; - `internal', keep each file's font lock cache file in the same file.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
90 ; - `external', keep each file's font lock cache file in the same directory.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
91 "Directories in which font lock cache files are saved and read.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
92 Each item should be either DIR or a cons pair of the form (REGEXP . DIR) where
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
93 DIR is a directory name (relative or absolute) and REGEXP is a regexp.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
94
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
95 An attempt will be made to save or read font lock cache files using these items
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
96 until one succeeds (i.e., until a readable or writable one is found). If an
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
97 item contains REGEXP, DIR is used only if the buffer file name matches REGEXP.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
98 For example:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
99
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
100 (list (cons (concat \"^\" (regexp-quote (expand-file-name \"~\"))) \".\")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
101 \"~/.emacs-flc\")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
102
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
103 would cause a file's current directory to be used if the file is under your
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
104 home directory hierarchy, and the absolute directory otherwise.")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
105
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
106 (defvar fast-lock-save-size (* 10 1024)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
107 "If non-nil, the minimum size for buffer files.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
108 Only buffer files at least this size can have associated font lock cache files
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
109 saved. If nil, means size is irrelevant.")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
110
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
111 (defvar fast-lock-save-others t
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
112 "If non-nil, save font lock cache files irrespective of file owner.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
113 If nil, means only buffer files owned by you have a font lock cache saved.")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
114
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
115 (defvar fast-lock-mode nil) ; for modeline
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
116 (defvar fast-lock-cache-timestamp nil) ; for saving/reading
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
117 (make-variable-buffer-local 'fast-lock-cache-timestamp)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
118
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
119 ;; Functions:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
120
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
121 (defun fast-lock-mode (&optional arg)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
122 "Toggle Fast Lock mode.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
123 With arg, turn Fast Lock mode on if and only if arg is positive and the buffer
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
124 is associated with a file.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
125
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
126 If Fast Lock mode is enabled, and the current buffer does not contain any text
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
127 properties, any associated font lock cache is used (by `fast-lock-read-cache')
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
128 if the same `font-lock-keywords' were used for the cache as you are using.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
129
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
130 Font lock caches will be saved:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
131 - For all buffers with Fast Lock mode enabled when you exit from Emacs.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
132 - For a buffer with Fast Lock mode enabled when you kill the buffer.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
133 Saving is done by `fast-lock-save-cache' and `fast-lock-save-caches'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
134
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
135 Various methods of control are provided for the font lock cache. In general,
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
136 see variable `fast-lock-cache-directories' and function `fast-lock-cache-name'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
137 For saving, see variables `fast-lock-save-others' and `fast-lock-save-size'."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
138 (interactive "P")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
139 (set (make-local-variable 'fast-lock-mode)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
140 (and (buffer-file-name)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
141 (if arg (> (prefix-numeric-value arg) 0) (not fast-lock-mode))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
142 (if (and fast-lock-mode (not font-lock-fontified))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
143 (fast-lock-read-cache)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
144
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
145 (defun fast-lock-read-cache ()
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
146 "Read the font lock cache for the current buffer.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
147 Returns t if the font lock cache file is read.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
148
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
149 The following criteria must be met for a font lock cache file to be read:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
150 - Fast Lock mode must be turned on in the buffer.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
151 - The buffer's `font-lock-keywords' must match the cache's.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
152 - The buffer file's timestamp must match the cache's.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
153 - Criteria imposed by `fast-lock-cache-directories'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
154
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
155 See also `fast-lock-save-cache' and `fast-lock-cache-name'."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
156 (interactive)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
157 (let ((directories fast-lock-cache-directories) directory
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
158 (modified (buffer-modified-p))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
159 (fontified font-lock-fontified))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
160 (set (make-local-variable 'font-lock-fontified) nil)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
161 ;; Keep trying directories until fontification is turned off.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
162 (while (and directories (not font-lock-fontified))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
163 (setq directory (fast-lock-cache-directory (car directories) nil)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
164 directories (cdr directories))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
165 (if directory
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
166 (condition-case nil
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
167 (load (fast-lock-cache-name directory) t t t)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
168 (error nil) (quit nil))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
169 (set-buffer-modified-p modified)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
170 (or font-lock-fontified (setq font-lock-fontified fontified))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
171
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
172 (defun fast-lock-save-cache (&optional buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
173 "Save the font lock cache of BUFFER or the current buffer.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
174 Returns t if the font lock cache file is saved.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
175
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
176 The following criteria must be met for a font lock cache file to be saved:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
177 - Fast Lock mode must be turned on in the buffer.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
178 - The buffer must be at least `fast-lock-save-size' bytes long.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
179 - The buffer file must be owned by you, or `fast-lock-save-others' must be t.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
180 - The buffer must contain at least one `face' text property.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
181 - The buffer file's timestamp must be different than its associated text
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
182 properties file's timestamp.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
183 - Criteria imposed by `fast-lock-cache-directories'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
184
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
185 See also `fast-lock-save-caches', `fast-lock-read-cache' and `fast-lock-mode'."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
186 (interactive)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
187 (let* ((bufile (buffer-file-name buffer))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
188 (buatts (and bufile (file-attributes bufile)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
189 (bufile-timestamp (nth 5 buatts))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
190 (bufuid (nth 2 buatts)) (saved nil))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
191 (save-excursion
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
192 (and buffer (set-buffer buffer))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
193 (if (and fast-lock-mode
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
194 ;; Only save if the timestamp of the file has changed.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
195 (not (equal fast-lock-cache-timestamp bufile-timestamp))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
196 ;; User's restrictions?
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
197 (or fast-lock-save-others (eq (user-uid) bufuid))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
198 (<= (or fast-lock-save-size 0) (buffer-size))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
199 ;; Only save if there are properties to save.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
200 (text-property-not-all (point-min) (point-max) 'face nil))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
201 (let ((directories fast-lock-cache-directories) directory)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
202 (while (and directories (not saved))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
203 (setq directory (fast-lock-cache-directory (car directories) t)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
204 directories (cdr directories))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
205 (if directory
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
206 (setq saved (fast-lock-save-cache-data
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
207 directory bufile-timestamp))))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
208 ;; Set the buffer's timestamp if saved.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
209 (and saved (setq fast-lock-cache-timestamp bufile-timestamp)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
210 saved))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
211
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
212 (defun fast-lock-save-cache-data (directory timestamp)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
213 ;; Save the file with the timestamp, if we can, in the given directory, as:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
214 ;; (fast-lock-cache-data Version=2 TIMESTAMP font-lock-keywords PROPERTIES).
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
215 (let ((buname (buffer-name))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
216 (tpfile (fast-lock-cache-name directory))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
217 (saved nil))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
218 (if (file-writable-p tpfile)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
219 (let ((tpbuf (generate-new-buffer " *fast-lock*")))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
220 (message "Saving %s font lock cache..." buname)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
221 (unwind-protect
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
222 (save-excursion
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
223 (print (list 'fast-lock-cache-data 2
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
224 (list 'quote timestamp)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
225 (list 'quote font-lock-keywords)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
226 (list 'quote (fast-lock-get-face-properties)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
227 tpbuf)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
228 (set-buffer tpbuf)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
229 (write-region (point-min) (point-max) tpfile nil 'quietly)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
230 (setq saved t))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
231 (kill-buffer tpbuf))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
232 (message "Saving %s font lock cache... done." buname)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
233 saved))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
234
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
235 ;; Miscellaneous functions:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
236
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
237 (defun turn-on-fast-lock ()
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
238 "Unconditionally turn on Fast Lock mode."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
239 (fast-lock-mode 1))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
240
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
241 (defun fast-lock-save-caches ()
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
242 "Save the font lock caches of all buffers.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
243 Returns list of cache save success of buffers in `buffer-list'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
244 See `fast-lock-save-cache' for details of save criteria."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
245 (mapcar 'fast-lock-save-cache (buffer-list)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
246
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
247 (defun fast-lock-cache-directory (directory create)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
248 "Return usable directory based on DIRECTORY.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
249 Returns nil if the directory does not exist, or, if CREATE non-nil, cannot be
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
250 created. DIRECTORY may be a string or a cons pair of the form (REGEXP . DIR).
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
251 See `fast-lock-cache-directories'."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
252 (let ((dir (cond ((not buffer-file-name)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
253 nil)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
254 ((stringp directory)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
255 directory)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
256 (t
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
257 (let ((bufile (expand-file-name
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
258 (abbreviate-file-name
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
259 (file-truename buffer-file-name))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
260 (case-fold-search nil))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
261 (if (string-match (car directory) bufile)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
262 (cdr directory)))))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
263 (cond ((not dir)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
264 nil)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
265 ((not create)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
266 (and (file-accessible-directory-p dir) dir))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
267 (t
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
268 (if (file-accessible-directory-p dir)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
269 dir
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
270 (condition-case nil (make-directory dir t) (error nil))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
271 (and (file-accessible-directory-p dir) dir))))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
272
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
273 (defun fast-lock-cache-name (directory)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
274 "Return full cache path name using caching DIRECTORY.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
275 If DIRECTORY is `.', the path is the buffer file name appended with `.flc'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
276 Otherwise, the path name is constructed from DIRECTORY and the buffer's true
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
277 abbreviated file name, with all `/' characters in the name replaced with `#'
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
278 characters, and appended with `.flc'.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
279
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
280 See `fast-lock-mode'."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
281 (if (string-equal directory ".")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
282 (concat buffer-file-name ".flc")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
283 (let* ((bufile (expand-file-name
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
284 (abbreviate-file-name (file-truename buffer-file-name))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
285 (chars-alist
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
286 (if (eq system-type 'emx)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
287 '((?/ . (?#)) (?# . (?# ?#)) (?: . (?\;)) (?\; . (?\; ?\;)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
288 '((?/ . (?#)) (?# . (?# ?#)))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
289 (mapchars
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
290 (function (lambda (c) (or (cdr (assq c chars-alist)) (list c))))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
291 (concat
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
292 (file-name-as-directory (expand-file-name directory))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
293 (mapconcat 'char-to-string (apply 'append (mapcar mapchars bufile)) "")
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
294 ".flc"))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
295
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
296 ;; Font lock cache processing functions:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
297
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
298 (defun fast-lock-cache-data (version timestamp keywords properties
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
299 &rest ignored)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
300 ;; Use the font lock cache PROPERTIES if we're using cache VERSION format 2,
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
301 ;; the current buffer's file timestamp matches the TIMESTAMP, and the current
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
302 ;; buffer's font-lock-keywords are the same as KEYWORDS.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
303 (let ((buf-timestamp (nth 5 (file-attributes buffer-file-name)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
304 (buname (buffer-name)) (inhibit-read-only t) (loaded t))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
305 (if (or (/= version 2)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
306 (not (equal timestamp buf-timestamp))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
307 (not (equal keywords font-lock-keywords)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
308 (setq loaded nil)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
309 (message "Loading %s font lock cache..." buname)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
310 (condition-case nil
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
311 (fast-lock-set-face-properties properties)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
312 (error (setq loaded nil)) (quit (setq loaded nil)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
313 (message "Loading %s font lock cache... done." buname))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
314 ;; If we used the text properties, stop fontification and keep timestamp.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
315 (setq font-lock-fontified loaded
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
316 fast-lock-cache-timestamp (and loaded timestamp))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
317
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
318 (defun fast-lock-get-face-properties (&optional buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
319 "Return a list of all `face' text properties in BUFFER.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
320 Each element of the list is of the form (VALUE START1 END1 START2 END2 ...)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
321 where VALUE is a `face' property value and STARTx and ENDx are positions."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
322 (save-excursion
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
323 (and buffer (set-buffer buffer))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
324 (save-restriction
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
325 (widen)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
326 (let ((start (text-property-not-all (point-min) (point-max) 'face nil))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
327 (limit (point-max)) end properties value cell)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
328 (while start
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
329 (setq end (next-single-property-change start 'face nil limit)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
330 value (get-text-property start 'face))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
331 ;; Make or add to existing list of regions with same `face' property.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
332 (if (setq cell (assq value properties))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
333 (setcdr cell (cons start (cons end (cdr cell))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
334 (setq properties (cons (list value start end) properties)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
335 (setq start (next-single-property-change end 'face)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
336 properties))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
337
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
338 (defun fast-lock-set-face-properties (properties &optional buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
339 "Set all `face' text properties to PROPERTIES in BUFFER.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
340 Any existing `face' text properties are removed first. Leaves BUFFER modified.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
341 See `fast-lock-get-face-properties' for the format of PROPERTIES."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
342 (save-excursion
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
343 (and buffer (set-buffer buffer))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
344 (save-restriction
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
345 (widen)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
346 (font-lock-unfontify-region (point-min) (point-max))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
347 (while properties
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
348 (let ((plist (list 'face (car (car properties))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
349 (regions (cdr (car properties))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
350 ;; Set the `face' property for each start/end region.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
351 (while regions
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
352 (set-text-properties (nth 0 regions) (nth 1 regions) plist buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
353 (setq regions (nthcdr 2 regions)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
354 (setq properties (cdr properties)))))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
355
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
356 ;; Functions for Lucid:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
357
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
358 (or (fboundp 'face-list)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
359 (defalias 'face-list 'list-faces))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
360
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
361 (if (save-match-data (string-match "Lucid" (emacs-version)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
362 ;; This is about a bazillion times faster at generating the cache in Lucid.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
363 (defun fast-lock-get-face-properties (&optional buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
364 "Return a list of all `face' text properties in BUFFER.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
365 Each element of the list is of the form (VALUE START1 END1 START2 END2 ...)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
366 where VALUE is a `face' property value and STARTx and ENDx are positions."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
367 (save-excursion
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
368 (and buffer (set-buffer buffer))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
369 (save-restriction
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
370 (widen)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
371 (let ((properties ()))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
372 (map-extents
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
373 (function (lambda (extent ignore)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
374 (let* ((face (extent-face extent))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
375 (start (extent-start-position extent))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
376 (end (extent-end-position extent))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
377 (facedata (assoc face properties)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
378 (if facedata
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
379 ;; Prepend the new start and end points onto the list.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
380 (setcdr facedata (cons start (cons end (cdr facedata))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
381 (setq properties (cons (list face start end) properties)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
382 ;; Return nil to keep `map-extents' going.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
383 nil))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
384 properties)))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
385
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
386 (if (save-match-data (string-match "Lucid" (emacs-version)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
387 ;; This is faster at using the cache in Lucid.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
388 (defun fast-lock-set-face-properties (properties &optional buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
389 "Set all `face' text properties to PROPERTIES in BUFFER.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
390 Any existing `face' text properties are removed first. Leaves BUFFER modified.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
391 See `fast-lock-get-face-properties' for the format of PROPERTIES."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
392 (save-excursion
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
393 (and buffer (set-buffer buffer))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
394 (save-restriction
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
395 (widen)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
396 (font-lock-unfontify-region (point-min) (point-max))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
397 (while properties
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
398 (let ((property (car (car properties)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
399 (regions (cdr (car properties))) extent)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
400 ;; Set the `face' property for each start/end region.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
401 (while regions
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
402 (setq extent (make-extent (nth 0 regions) (nth 1 regions))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
403 regions (nthcdr 2 regions))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
404 (set-extent-face extent property)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
405 (set-extent-property extent 'text-prop 'face))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
406 (setq properties (cdr properties))))))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
407
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
408 (if (and (boundp 'emacs-minor-version) (< emacs-minor-version 12))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
409 ;; Must be [LX]Emacs; fix the 19.11 (at least) `text-property-not-all' bug.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
410 (defun text-property-not-all (start end prop value &optional buffer)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
411 "Check text from START to END to see if PROP is ever not `eq' to VALUE.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
412 If so, return the position of the first character whose PROP is not
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
413 `eq' to VALUE. Otherwise, return nil."
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
414 (let ((maxend start))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
415 (map-extents
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
416 (function
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
417 (lambda (e ignore)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
418 ;;### no, actually, this is harder. We need to collect all props
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
419 ;; for a given character, and then determine whether no extent
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
420 ;; contributes the given value. Doing this without consing lots
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
421 ;; of lists is the tricky part.
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
422 (if (not (eq value (extent-property e prop)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
423 (max start maxend)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
424 (setq maxend (extent-end-position e))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
425 nil)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
426 nil start end buffer))))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
427
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
428 ;; Install ourselves:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
429
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
430 (or (assq 'fast-lock-mode minor-mode-alist)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
431 (setq minor-mode-alist (cons '(fast-lock-mode " Fast") minor-mode-alist)))
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
432
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
433 (add-hook 'kill-buffer-hook 'fast-lock-save-cache)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
434 (add-hook 'kill-emacs-hook 'fast-lock-save-caches)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
435
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
436 ;; Provide ourselves:
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
437
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
438 (provide 'fast-lock)
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
439
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
440 ;;; fast-lock.el ends here
ea9dda158056 Initial revision
Richard M. Stallman <rms@gnu.org>
parents:
diff changeset
441