(require 'font-lock)
(if (and (eq system-type 'ms-dos) (not (msdos-long-file-names)))
(error "`fast-lock' was written for long file name systems"))
(eval-when-compile
(require 'cl)
(defmacro save-buffer-state (varlist &rest body)
"Bind variables according to VARLIST and eval BODY restoring buffer state."
(` (let* ((,@ (append varlist
'((modified (buffer-modified-p)) (buffer-undo-list t)
(inhibit-read-only t) (inhibit-point-motion-hooks t)
before-change-functions after-change-functions
deactivate-mark buffer-file-name buffer-file-truename))))
(,@ body)
(when (and (not modified) (buffer-modified-p))
(set-buffer-modified-p nil)))))
(put 'save-buffer-state 'lisp-indent-function 1)
(defmacro fast-lock-save-facep (face)
"Return non-nil if FACE is one of `fast-lock-save-faces'."
(` (or (null fast-lock-save-faces)
(if (symbolp (, face))
(memq (, face) fast-lock-save-faces)
(let ((faces (, face)))
(while (unless (memq (car faces) fast-lock-save-faces)
(setq faces (cdr faces))))
faces)))))
(or (fboundp 'with-temp-message)
(defmacro with-temp-message (message &rest body)
(` (let ((temp-message (, message)) current-message)
(unwind-protect
(progn
(when temp-message
(setq current-message (current-message))
(message temp-message))
(,@ body))
(when temp-message
(message current-message)))))))
(or (fboundp 'defcustom)
(defmacro defcustom (symbol value doc &rest args)
(` (defvar (, symbol) (, value) (, doc))))))
(defvar fast-lock-mode nil) (defvar fast-lock-cache-timestamp nil) (defvar fast-lock-cache-filename nil)
(defcustom fast-lock-minimum-size 25600
"*Minimum size of a buffer for cached fontification.
Only buffers more than this can have associated Font Lock cache files saved.
If nil, means cache files are never created.
If a list, each element should be a cons pair of the form (MAJOR-MODE . SIZE),
where MAJOR-MODE is a symbol or t (meaning the default). For example:
((c-mode . 25600) (c++-mode . 25600) (rmail-mode . 1048576))
means that the minimum size is 25K for buffers in C or C++ modes, one megabyte
for buffers in Rmail mode, and size is irrelevant otherwise."
:type '(choice (const :tag "none" nil)
(integer :tag "size")
(repeat :menu-tag "mode specific" :tag "mode specific"
:value ((t . nil))
(cons :tag "Instance"
(radio :tag "Mode"
(const :tag "all" t)
(symbol :tag "name"))
(radio :tag "Size"
(const :tag "none" nil)
(integer :tag "size")))))
:group 'fast-lock)
(defcustom fast-lock-cache-directories '("." "~/.emacs-flc")
"*Directories in which Font Lock cache files are saved and read.
Each item should be either DIR or a cons pair of the form (REGEXP . DIR) where
DIR is a directory name (relative or absolute) and REGEXP is a regexp.
An attempt will be made to save or read Font Lock cache files using these items
until one succeeds (i.e., until a readable or writable one is found). If an
item contains REGEXP, DIR is used only if the buffer file name matches REGEXP.
For example:
(let ((home (expand-file-name (abbreviate-file-name (file-truename \"~/\")))))
(list (cons (concat \"^\" (regexp-quote home)) \".\") \"~/.emacs-flc\"))
=>
((\"^/your/true/home/directory/\" . \".\") \"~/.emacs-flc\")
would cause a file's current directory to be used if the file is under your
home directory hierarchy, or otherwise the absolute directory `~/.emacs-flc'."
:type '(repeat (radio (directory :tag "directory")
(cons :tag "Matching"
(regexp :tag "regexp")
(directory :tag "directory"))))
:group 'fast-lock)
(defcustom fast-lock-save-events '(kill-buffer kill-emacs)
"*Events under which caches will be saved.
Valid events are `save-buffer', `kill-buffer' and `kill-emacs'.
If concurrent editing sessions use the same associated cache file for a file's
buffer, then you should add `save-buffer' to this list."
:type '(set (const :tag "buffer saving" save-buffer)
(const :tag "buffer killing" kill-buffer)
(const :tag "emacs killing" kill-emacs))
:group 'fast-lock)
(defcustom fast-lock-save-others t
"*If non-nil, save Font Lock cache files irrespective of file owner.
If nil, means only buffer files known to be owned by you can have associated
Font Lock cache files saved. Ownership may be unknown for networked files."
:type 'boolean
:group 'fast-lock)
(defcustom fast-lock-verbose font-lock-verbose
"*If non-nil, means show status messages for cache processing.
If a number, only buffers greater than this size have processing messages."
:type '(choice (const :tag "never" nil)
(other :tag "always" t)
(integer :tag "size"))
:group 'fast-lock)
(defvar fast-lock-save-faces
(when (save-match-data (string-match "XEmacs" (emacs-version)))
font-lock-face-list)
"Faces that will be saved in a Font Lock cache file.
If nil, means information for all faces will be saved.")
(defun fast-lock-mode (&optional arg)
"Toggle Fast Lock mode.
With arg, turn Fast Lock mode on if and only if arg is positive and the buffer
is associated with a file. Enable it automatically in your `~/.emacs' by:
(setq font-lock-support-mode 'fast-lock-mode)
If Fast Lock mode is enabled, and the current buffer does not contain any text
properties, any associated Font Lock cache is used if its timestamp matches the
buffer's file, and its `font-lock-keywords' match those that you are using.
Font Lock caches may be saved:
- When you save the file's buffer.
- When you kill an unmodified file's buffer.
- When you exit Emacs, for all unmodified or saved buffers.
Depending on the value of `fast-lock-save-events'.
See also the commands `fast-lock-read-cache' and `fast-lock-save-cache'.
Use \\[font-lock-fontify-buffer] to fontify the buffer if the cache is bad.
Various methods of control are provided for the Font Lock cache. In general,
see variable `fast-lock-cache-directories' and function `fast-lock-cache-name'.
For saving, see variables `fast-lock-minimum-size', `fast-lock-save-events',
`fast-lock-save-others' and `fast-lock-save-faces'."
(interactive "P")
(set (make-local-variable 'fast-lock-mode)
(and buffer-file-truename
(not (memq 'fast-lock-mode font-lock-inhibit-thing-lock))
(if arg (> (prefix-numeric-value arg) 0) (not fast-lock-mode))))
(if (and fast-lock-mode (not font-lock-mode))
(let ((font-lock-support-mode 'fast-lock-mode))
(font-lock-mode t))
(set (make-local-variable 'fast-lock-cache-timestamp) nil)
(set (make-local-variable 'fast-lock-cache-filename) nil)
(when (and fast-lock-mode (not font-lock-fontified))
(fast-lock-read-cache))))
(defun fast-lock-read-cache ()
"Read the Font Lock cache for the current buffer.
The following criteria must be met for a Font Lock cache file to be read:
- Fast Lock mode must be turned on in the buffer.
- The buffer must not be modified.
- The buffer's `font-lock-keywords' must match the cache's.
- The buffer file's timestamp must match the cache's.
- Criteria imposed by `fast-lock-cache-directories'.
See `fast-lock-mode'."
(interactive)
(let ((directories fast-lock-cache-directories)
(modified (buffer-modified-p)) (inhibit-read-only t)
(fontified font-lock-fontified))
(set (make-local-variable 'font-lock-fontified) nil)
(while (and directories (not font-lock-fontified))
(let ((directory (fast-lock-cache-directory (car directories) nil)))
(condition-case nil
(when directory
(setq fast-lock-cache-filename (fast-lock-cache-name directory))
(when (file-readable-p fast-lock-cache-filename)
(load fast-lock-cache-filename t t t)))
(error nil) (quit nil))
(setq directories (cdr directories))))
(set-buffer-modified-p modified)
(unless font-lock-fontified
(setq fast-lock-cache-filename nil font-lock-fontified fontified))))
(defun fast-lock-save-cache (&optional buffer)
"Save the Font Lock cache of BUFFER or the current buffer.
The following criteria must be met for a Font Lock cache file to be saved:
- Fast Lock mode must be turned on in the buffer.
- The event must be one of `fast-lock-save-events'.
- The buffer must be at least `fast-lock-minimum-size' bytes long.
- The buffer file must be owned by you, or `fast-lock-save-others' must be t.
- The buffer must contain at least one `face' text property.
- The buffer must not be modified.
- The buffer file's timestamp must be the same as the file's on disk.
- The on disk file's timestamp must be different than the buffer's cache.
- Criteria imposed by `fast-lock-cache-directories'.
See `fast-lock-mode'."
(interactive)
(save-excursion
(when buffer
(set-buffer buffer))
(let ((min-size (font-lock-value-in-major-mode fast-lock-minimum-size))
(file-timestamp (visited-file-modtime)) (saved nil))
(when (and fast-lock-mode
(not (buffer-modified-p))
(verify-visited-file-modtime (current-buffer))
(not (equal fast-lock-cache-timestamp file-timestamp))
(and min-size (>= (buffer-size) min-size))
(or fast-lock-save-others
(eq (user-uid) (nth 2 (file-attributes buffer-file-name))))
(text-property-not-all (point-min) (point-max) 'face nil))
(let ((directories fast-lock-cache-directories))
(while (and directories (memq saved '(nil error)))
(let* ((dir (fast-lock-cache-directory (car directories) t))
(file (and dir (fast-lock-cache-name dir))))
(when (and file (file-writable-p file))
(setq saved (fast-lock-save-cache-1 file file-timestamp)))
(setq directories (cdr directories)))))))))
(defun turn-on-fast-lock ()
"Unconditionally turn on Fast Lock mode."
(fast-lock-mode t))
(defun fast-lock-after-fontify-buffer ()
(when fast-lock-cache-filename
(if (file-writable-p fast-lock-cache-filename)
(delete-file fast-lock-cache-filename)
(message "File %s font lock cache cannot be deleted" (buffer-name))))
(setq fast-lock-cache-timestamp nil))
(defalias 'fast-lock-after-unfontify-buffer
'ignore)
(defun fast-lock-save-cache-after-save-file ()
(when (memq 'save-buffer fast-lock-save-events)
(fast-lock-save-cache)))
(defun fast-lock-save-cache-before-kill-buffer ()
(when (memq 'kill-buffer fast-lock-save-events)
(fast-lock-save-cache)))
(defun fast-lock-save-caches-before-kill-emacs ()
(when (memq 'kill-emacs fast-lock-save-events)
(mapcar 'fast-lock-save-cache (buffer-list))))
(defun fast-lock-cache-directory (directory create)
"Return usable directory based on DIRECTORY.
Returns nil if the directory does not exist, or, if CREATE non-nil, cannot be
created. DIRECTORY may be a string or a cons pair of the form (REGEXP . DIR).
See `fast-lock-cache-directories'."
(let ((dir
(cond ((not buffer-file-name)
nil)
((stringp directory)
directory)
(t
(let ((bufile (expand-file-name buffer-file-truename))
(case-fold-search nil))
(when (save-match-data (string-match (car directory) bufile))
(cdr directory)))))))
(cond ((not dir)
nil)
((file-accessible-directory-p dir)
dir)
(create
(condition-case nil
(progn (make-directory dir t) dir)
(error nil))))))
(defun fast-lock-cache-name (directory)
"Return full cache path name using caching DIRECTORY.
If DIRECTORY is `.', the path is the buffer file name appended with `.flc'.
Otherwise, the path name is constructed from DIRECTORY and the buffer's true
abbreviated file name, with all `/' characters in the name replaced with `#'
characters, and appended with `.flc'.
If the same file has different cache path names when edited on different
machines, e.g., on one machine the cache file name has the prefix `#home',
perhaps due to automount, try putting in your `~/.emacs' something like:
(setq directory-abbrev-alist (cons '(\"^/home/\" . \"/\") directory-abbrev-alist))
Emacs automagically removes the common `/tmp_mnt' automount prefix by default.
See `fast-lock-cache-directory'."
(if (string-equal directory ".")
(concat buffer-file-name ".flc")
(let* ((bufile (expand-file-name buffer-file-truename))
(chars-alist
(if (memq system-type '(emx windows-nt))
'((?/ . (?#)) (?# . (?# ?#)) (?: . (?\ '((?/ . (?#)) (?# . (?# ?#)))))
(mapchars
(function (lambda (c) (or (cdr (assq c chars-alist)) (list c))))))
(concat
(file-name-as-directory (expand-file-name directory))
(mapconcat 'char-to-string (apply 'append (mapcar mapchars bufile)) "")
".flc"))))
(defun fast-lock-save-cache-1 (file timestamp)
(let ((tpbuf (generate-new-buffer " *fast-lock*"))
(verbose (if (numberp fast-lock-verbose)
(> (buffer-size) fast-lock-verbose)
fast-lock-verbose))
(saved t))
(with-temp-message
(when verbose
(format "Saving %s font lock cache..." (buffer-name)))
(condition-case nil
(save-excursion
(print (list 'fast-lock-cache-data 3
(list 'quote timestamp)
(list 'quote font-lock-syntactic-keywords)
(list 'quote (fast-lock-get-syntactic-properties))
(list 'quote font-lock-keywords)
(list 'quote (fast-lock-get-face-properties)))
tpbuf)
(set-buffer tpbuf)
(write-region (point-min) (point-max) file nil 'quietly)
(setq fast-lock-cache-timestamp timestamp
fast-lock-cache-filename file))
(error (setq saved 'error)) (quit (setq saved 'quit)))
(kill-buffer tpbuf))
(cond ((eq saved 'quit)
(message "Saving %s font lock cache...quit" (buffer-name)))
((eq saved 'error)
(message "Saving %s font lock cache...failed" (buffer-name))))
saved))
(defun fast-lock-cache-data (version timestamp
syntactic-keywords syntactic-properties
keywords face-properties
&rest ignored)
(setq font-lock-syntactic-keywords (font-lock-eval-keywords
font-lock-syntactic-keywords))
(when font-lock-syntactic-keywords
(setq font-lock-syntactic-keywords (font-lock-compile-keywords
font-lock-syntactic-keywords)))
(when syntactic-keywords
(setq syntactic-keywords (font-lock-compile-keywords syntactic-keywords)))
(setq font-lock-keywords (font-lock-compile-keywords font-lock-keywords)
keywords (font-lock-compile-keywords keywords))
(let ((buf-timestamp (visited-file-modtime))
(verbose (if (numberp fast-lock-verbose)
(> (buffer-size) fast-lock-verbose)
fast-lock-verbose))
(loaded t))
(if (or (/= version 3)
(buffer-modified-p)
(not (equal timestamp buf-timestamp))
(not (equal syntactic-keywords font-lock-syntactic-keywords))
(not (equal keywords font-lock-keywords)))
(setq loaded nil)
(with-temp-message
(when verbose
(format "Loading %s font lock cache..." (buffer-name)))
(condition-case nil
(fast-lock-add-properties syntactic-properties face-properties)
(error (setq loaded 'error)) (quit (setq loaded 'quit))))
(cond ((eq loaded 'quit)
(message "Loading %s font lock cache...quit" (buffer-name)))
((eq loaded 'error)
(message "Loading %s font lock cache...failed" (buffer-name)))))
(setq font-lock-fontified (eq loaded t)
fast-lock-cache-timestamp (and (eq loaded t) timestamp))))
(defun fast-lock-get-face-properties ()
"Return a list of `face' text properties in the current buffer.
Each element of the list is of the form (VALUE START1 END1 START2 END2 ...)
where VALUE is a `face' property value and STARTx and ENDx are positions."
(save-restriction
(widen)
(let ((start (text-property-not-all (point-min) (point-max) 'face nil))
end properties value cell)
(while start
(setq end (next-single-property-change start 'face nil (point-max))
value (get-text-property start 'face))
(cond ((setq cell (assoc value properties))
(setcdr cell (cons start (cons end (cdr cell)))))
((fast-lock-save-facep value)
(push (list value start end) properties)))
(setq start (text-property-not-all end (point-max) 'face nil)))
properties)))
(defun fast-lock-get-syntactic-properties ()
"Return a list of `syntax-table' text properties in the current buffer.
See `fast-lock-get-face-properties'."
(save-restriction
(widen)
(let ((start (text-property-not-all (point-min) (point-max) 'syntax-table
nil))
end properties value cell)
(while start
(setq end (next-single-property-change start 'syntax-table nil
(point-max))
value (get-text-property start 'syntax-table))
(if (setq cell (assoc value properties))
(setcdr cell (cons start (cons end (cdr cell))))
(push (list value start end) properties))
(setq start (text-property-not-all end (point-max) 'syntax-table nil)))
properties)))
(defun fast-lock-add-properties (syntactic-properties face-properties)
"Add `syntax-table' and `face' text properties to the current buffer.
Any existing `syntax-table' and `face' text properties are removed first.
See `fast-lock-get-face-properties'."
(save-buffer-state (plist regions)
(save-restriction
(widen)
(font-lock-unfontify-region (point-min) (point-max))
(while syntactic-properties
(setq plist (list 'syntax-table (car (car syntactic-properties)))
regions (cdr (car syntactic-properties))
syntactic-properties (cdr syntactic-properties))
(while regions
(add-text-properties (nth 0 regions) (nth 1 regions) plist)
(setq regions (nthcdr 2 regions))))
(while face-properties
(setq plist (list 'face (car (car face-properties)))
regions (cdr (car face-properties))
face-properties (cdr face-properties))
(while regions
(add-text-properties (nth 0 regions) (nth 1 regions) plist)
(setq regions (nthcdr 2 regions)))))))
(when (save-match-data (string-match "XEmacs" (emacs-version)))
(defun fast-lock-get-face-properties ()
"Return a list of `face' text properties in the current buffer.
Each element of the list is of the form (VALUE START1 END1 START2 END2 ...)
where VALUE is a `face' property value and STARTx and ENDx are positions.
Only those `face' VALUEs in `fast-lock-save-faces' are returned."
(save-restriction
(widen)
(let ((properties ()) cell)
(map-extents
(function (lambda (extent ignore)
(let ((value (extent-face extent)))
(when (and value (fast-lock-save-facep value))
(let ((start (extent-start-position extent))
(end (extent-end-position extent)))
(if (setq cell (assoc value properties))
(setcdr cell (cons start (cons end (cdr cell))))
(push (list value start end) properties))))
nil))))
properties)))
(defalias 'fast-lock-get-syntactic-properties
'ignore)
(defun fast-lock-add-properties (syntactic-properties face-properties)
"Set `face' text properties in the current buffer.
Any existing `face' text properties are removed first.
See `fast-lock-get-face-properties'."
(save-restriction
(widen)
(font-lock-unfontify-region (point-min) (point-max))
(while face-properties
(let ((face (car (car face-properties)))
(regions (cdr (car face-properties))))
(while regions
(font-lock-set-face (nth 0 regions) (nth 1 regions) face)
(setq regions (nthcdr 2 regions)))
(setq face-properties (cdr face-properties))))
))
(add-hook 'font-lock-after-fontify-buffer-hook
'fast-lock-after-fontify-buffer))
(unless (boundp 'font-lock-syntactic-keywords)
(defvar font-lock-syntactic-keywords nil))
(unless (boundp 'font-lock-inhibit-thing-lock)
(defvar font-lock-inhibit-thing-lock nil))
(unless (fboundp 'font-lock-compile-keywords)
(defalias 'font-lock-compile-keywords 'identity))
(unless (fboundp 'font-lock-eval-keywords)
(defun font-lock-eval-keywords (keywords)
(if (symbolp keywords)
(font-lock-eval-keywords (if (fboundp keywords)
(funcall keywords)
(eval keywords)))
keywords)))
(unless (fboundp 'font-lock-value-in-major-mode)
(defun font-lock-value-in-major-mode (alist)
(if (consp alist)
(cdr (or (assq major-mode alist) (assq t alist)))
alist)))
(unless (fboundp 'current-message)
(defun current-message ()
""))
(add-hook 'after-save-hook 'fast-lock-save-cache-after-save-file)
(add-hook 'kill-buffer-hook 'fast-lock-save-cache-before-kill-buffer)
(add-hook 'kill-emacs-hook 'fast-lock-save-caches-before-kill-emacs)
(when (fboundp 'add-minor-mode)
(defvar fast-lock-mode nil)
(add-minor-mode 'fast-lock-mode nil))
(unless (assq 'fast-lock-mode minor-mode-alist)
(setq minor-mode-alist (append minor-mode-alist '((fast-lock-mode nil)))))
(provide 'fast-lock)