diff options
| author | Boris Goldowsky | 1995-03-17 18:14:30 +0000 |
|---|---|---|
| committer | Boris Goldowsky | 1995-03-17 18:14:30 +0000 |
| commit | 029894b956eb692f481cb26228672940d3fe1b34 (patch) | |
| tree | 0a4ee2c33928b6366af8e1da482be2594a56d0be | |
| parent | 0d420e8889d70c3a1056d76a84785ca37be2f888 (diff) | |
| download | emacs-029894b956eb692f481cb26228672940d3fe1b34.tar.gz emacs-029894b956eb692f481cb26228672940d3fe1b34.zip | |
Initial revision
| -rw-r--r-- | lisp/format.el | 752 |
1 files changed, 752 insertions, 0 deletions
diff --git a/lisp/format.el b/lisp/format.el new file mode 100644 index 00000000000..6942cfb3593 --- /dev/null +++ b/lisp/format.el | |||
| @@ -0,0 +1,752 @@ | |||
| 1 | ;;; format.el -- read and save files in multiple formats | ||
| 2 | ;; Copyright (c) 1994 Free Software Foundation | ||
| 3 | |||
| 4 | ;; Author: Boris Goldowsky <boris@cs.rochester.edu> | ||
| 5 | |||
| 6 | ;; This file is part of GNU Emacs. | ||
| 7 | |||
| 8 | ;; GNU Emacs is free software; you can redistribute it and/or modify | ||
| 9 | ;; it under the terms of the GNU General Public License as published by | ||
| 10 | ;; the Free Software Foundation; either version 2, or (at your option) | ||
| 11 | ;; any later version. | ||
| 12 | ;; | ||
| 13 | ;; GNU Emacs is distributed in the hope that it will be useful, | ||
| 14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | ;; GNU General Public License for more details. | ||
| 17 | ;; | ||
| 18 | ;; You should have received a copy of the GNU General Public License | ||
| 19 | ;; along with GNU Emacs; see the file COPYING. If not, write to | ||
| 20 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | ||
| 21 | |||
| 22 | ;;; Commentary: | ||
| 23 | ;; This file defines a unified mechanism for saving & loading files stored in | ||
| 24 | ;; different formats. `format-alist' contains information that directs | ||
| 25 | ;; Emacs to call an encoding or decoding function when reading or writing | ||
| 26 | ;; files that match certain conditions. | ||
| 27 | ;; | ||
| 28 | ;; When a file is visited, its format is determined by matching the beginning | ||
| 29 | ;; of the file against regular expressions stored in `format-alist'. If this | ||
| 30 | ;; fails, you can manually translate the buffer using `format-decode-buffer'. | ||
| 31 | ;; In either case, the formats used are listed in the variable | ||
| 32 | ;; `buffer-file-format', and become the default format for saving the buffer. | ||
| 33 | ;; To save a buffer in a different format, change this variable, or use | ||
| 34 | ;; `format-write-file'. | ||
| 35 | ;; | ||
| 36 | ;; Auto-save files are normally created in the same format as the visited | ||
| 37 | ;; file, but the variable `auto-save-file-format' can be set to a particularly | ||
| 38 | ;; fast or otherwise preferred format to be used for auto-saving (or nil to do | ||
| 39 | ;; no encoding on auto-save files, but then you risk losing any | ||
| 40 | ;; text-properties in the buffer). | ||
| 41 | ;; | ||
| 42 | ;; You can manually translate a buffer into or out of a particular format with | ||
| 43 | ;; the functions `format-encode-buffer' and `format-decode-buffer'. | ||
| 44 | ;; To translate just the region use the functions `format-encode-region' and | ||
| 45 | ;; `format-decode-region'. | ||
| 46 | ;; | ||
| 47 | ;; You can define a new format by writing the encoding and decoding functions, | ||
| 48 | ;; and adding an entry to `format-alist'. See enriched.el for an example of | ||
| 49 | ;; how to implement a file format. There are various functions defined | ||
| 50 | ;; in this file that may be useful for writing the encoding and decoding | ||
| 51 | ;; functions: | ||
| 52 | ;; * `format-annotate-region' and `format-deannotate-region' allow a single | ||
| 53 | ;; alist of information to be used for encoding and decoding. The alist | ||
| 54 | ;; defines a correspondence between strings in the file ("annotations") | ||
| 55 | ;; and text-properties in the buffer. | ||
| 56 | ;; * `format-replace-strings' is similarly useful for doing simple | ||
| 57 | ;; string->string translations in a reversible manner. | ||
| 58 | |||
| 59 | (put 'buffer-file-format 'permanent-local t) | ||
| 60 | |||
| 61 | (defconst format-alist | ||
| 62 | '((text/enriched "Extended MIME text/enriched format." | ||
| 63 | "Content-[Tt]ype:[ \t]*text/enriched" | ||
| 64 | enriched-decode enriched-encode t enriched-mode) | ||
| 65 | (plain "Standard ASCII format, no text properties." | ||
| 66 | ;; Plain only exists so that there is an obvious neutral choice in | ||
| 67 | ;; the completion list. | ||
| 68 | nil nil nil nil nil)) | ||
| 69 | "List of information about understood file formats. | ||
| 70 | Elements are of the form \(NAME DOC-STR REGEXP FROM-FN TO-FN MODIFY MODE-FN). | ||
| 71 | NAME is a symbol, which is stored in `buffer-file-format'. | ||
| 72 | DOC-STR should be a single line providing more information about the | ||
| 73 | format. It is currently unused, but in the future will be shown to | ||
| 74 | the user if they ask for more information. | ||
| 75 | REGEXP is a regular expression to match against the beginning of the file; | ||
| 76 | it should match only files in that format. | ||
| 77 | FROM-FN is called to decode files in that format; it gets two args, BEGIN | ||
| 78 | and END, and can make any modifications it likes, returning the new | ||
| 79 | end. It must make sure that the beginning of the file no longer | ||
| 80 | matches REGEXP, or else it will get called again. | ||
| 81 | TO-FN is called to encode a region into that format; it is also passed BEGIN | ||
| 82 | and END, and either returns a list of annotations like | ||
| 83 | `write-region-annotate-functions', or modifies the region and returns | ||
| 84 | the new end. | ||
| 85 | MODIFY, if non-nil, means the TO-FN modifies the region. If nil, TO-FN may | ||
| 86 | not make any changes and should return a list of annotations. | ||
| 87 | MODE-FN, if specified, is called when visiting a file with that format.") | ||
| 88 | |||
| 89 | ;;; Basic Functions (called from Lisp) | ||
| 90 | |||
| 91 | (defun format-annotate-function (format from to) | ||
| 92 | "Returns annotations for writing region as FORMAT. | ||
| 93 | FORMAT is a symbol naming one of the formats defined in `format-alist', | ||
| 94 | it must be a single symbol, not a list like `buffer-file-format'. | ||
| 95 | This function works like a function on `write-region-annotate-functions': | ||
| 96 | it either returns a list of annotations, or returns with a different buffer | ||
| 97 | current, which contains the modified text to write. | ||
| 98 | |||
| 99 | For most purposes, consider using `format-encode-region' instead." | ||
| 100 | ;; This function is called by write-region (actually build-annotations) | ||
| 101 | ;; for each element of buffer-file-format. | ||
| 102 | (let* ((info (assq format format-alist)) | ||
| 103 | (to-fn (nth 4 info)) | ||
| 104 | (modify (nth 5 info))) | ||
| 105 | (if to-fn | ||
| 106 | (if modify | ||
| 107 | ;; To-function wants to modify region. Copy to safe place. | ||
| 108 | (let ((copy-buf (get-buffer-create " *Format Temp*"))) | ||
| 109 | (copy-to-buffer copy-buf from to) | ||
| 110 | (set-buffer copy-buf) | ||
| 111 | (format-insert-annotations write-region-annotations-so-far from) | ||
| 112 | (funcall to-fn (point-min) (point-max)) | ||
| 113 | nil) | ||
| 114 | ;; Otherwise just call function, it will return annotations. | ||
| 115 | (funcall to-fn from to))))) | ||
| 116 | |||
| 117 | (defun format-decode (format length &optional visit-flag) | ||
| 118 | ;; This function is called by insert-file-contents whenever a file is read. | ||
| 119 | "Decode text from any known FORMAT. | ||
| 120 | FORMAT is a symbol appearing in `format-alist' or a list of such symbols, | ||
| 121 | or nil, in which case this function tries to guess the format of the data by | ||
| 122 | matching against the regular expressions in `format-alist'. After a match is | ||
| 123 | found and the region decoded, the alist is searched again from the beginning | ||
| 124 | for another match. | ||
| 125 | |||
| 126 | Second arg LENGTH is the number of characters following point to operate on. | ||
| 127 | If optional third arg VISIT-FLAG is true, set `buffer-file-format' | ||
| 128 | to the list of formats used, and call any mode functions defined for those | ||
| 129 | formats. | ||
| 130 | |||
| 131 | Returns the new length of the decoded region. | ||
| 132 | |||
| 133 | For most purposes, consider using `format-decode-region' instead." | ||
| 134 | (let ((mod (buffer-modified-p)) | ||
| 135 | (begin (point)) | ||
| 136 | (end (+ (point) length))) | ||
| 137 | (if (null format) | ||
| 138 | ;; Figure out which format it is in, remember list in `format'. | ||
| 139 | (let ((try format-alist)) | ||
| 140 | (while try | ||
| 141 | (let* ((f (car try)) | ||
| 142 | (regexp (nth 2 f)) | ||
| 143 | (p (point))) | ||
| 144 | (if (and regexp (looking-at regexp) | ||
| 145 | (< (match-end 0) (+ begin length))) | ||
| 146 | (progn | ||
| 147 | (setq format (cons (car f) format)) | ||
| 148 | ;; Decode it | ||
| 149 | (if (nth 3 f) (setq end (funcall (nth 3 f) begin end))) | ||
| 150 | ;; Call visit function if required | ||
| 151 | (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) | ||
| 152 | ;; Safeguard against either of the functions changing pt. | ||
| 153 | (goto-char p) | ||
| 154 | ;; Rewind list to look for another format | ||
| 155 | (setq try format-alist)) | ||
| 156 | (setq try (cdr try)))))) | ||
| 157 | ;; Deal with given format(s) | ||
| 158 | (or (listp format) (setq format (list format))) | ||
| 159 | (let ((do format) f) | ||
| 160 | (while do | ||
| 161 | (or (setq f (assq (car do) format-alist)) | ||
| 162 | (error "Unknown format" (car do))) | ||
| 163 | ;; Decode: | ||
| 164 | (if (nth 3 f) (setq end (funcall (nth 3 f) begin end))) | ||
| 165 | ;; Call visit function if required | ||
| 166 | (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) | ||
| 167 | (setq do (cdr do))))) | ||
| 168 | (if visit-flag | ||
| 169 | (setq buffer-file-format format)) | ||
| 170 | (set-buffer-modified-p mod) | ||
| 171 | ;; Return new length of region | ||
| 172 | (- end begin))) | ||
| 173 | |||
| 174 | ;;; | ||
| 175 | ;;; Interactive functions & entry points | ||
| 176 | ;;; | ||
| 177 | |||
| 178 | (defun format-decode-buffer (&optional format) | ||
| 179 | "Translate the buffer from some FORMAT. | ||
| 180 | If the format is not specified, this function attempts to guess. | ||
| 181 | `buffer-file-format' is set to the format used, and any mode-functions | ||
| 182 | for the format are called." | ||
| 183 | (interactive | ||
| 184 | (list (format-read "Translate buffer from format (default: guess): "))) | ||
| 185 | (save-excursion | ||
| 186 | (goto-char (point-min)) | ||
| 187 | (format-decode format (buffer-size) t))) | ||
| 188 | |||
| 189 | (defun format-decode-region (from to &optional format) | ||
| 190 | "Decode the region from some format. | ||
| 191 | Arg FORMAT is optional; if omitted the format will be determined by looking | ||
| 192 | for identifying regular expressions at the beginning of the region." | ||
| 193 | (interactive | ||
| 194 | (list (region-beginning) (region-end) | ||
| 195 | (format-read "Translate region from format (default: guess): "))) | ||
| 196 | (save-excursion | ||
| 197 | (goto-char from) | ||
| 198 | (format-decode format (- to from) nil))) | ||
| 199 | |||
| 200 | (defun format-encode-buffer (&optional format) | ||
| 201 | "Translate the buffer into FORMAT. | ||
| 202 | FORMAT defaults to `buffer-file-format'. It is a symbol naming one of the | ||
| 203 | formats defined in `format-alist', or a list of such symbols." | ||
| 204 | (interactive | ||
| 205 | (list (format-read (format "Translate buffer to format (default %s): " | ||
| 206 | buffer-file-format)))) | ||
| 207 | (format-encode-region (point-min) (point-max) format)) | ||
| 208 | |||
| 209 | (defun format-encode-region (beg end &optional format) | ||
| 210 | "Translate the region into some FORMAT. | ||
| 211 | FORMAT defaults to `buffer-file-format', it is a symbol naming | ||
| 212 | one of the formats defined in `format-alist', or a list of such symbols." | ||
| 213 | (interactive | ||
| 214 | (list (region-beginning) (region-end) | ||
| 215 | (format-read (format "Translate region to format (default %s): " | ||
| 216 | buffer-file-format)))) | ||
| 217 | (if (null format) (setq format buffer-file-format)) | ||
| 218 | (if (symbolp format) (setq format (list format))) | ||
| 219 | (save-excursion | ||
| 220 | (goto-char end) | ||
| 221 | (let ((cur-buf (current-buffer)) | ||
| 222 | (end (point-marker))) | ||
| 223 | (while format | ||
| 224 | (let* ((info (assq (car format) format-alist)) | ||
| 225 | (to-fn (nth 4 info)) | ||
| 226 | (modify (nth 5 info)) | ||
| 227 | result) | ||
| 228 | (if to-fn | ||
| 229 | (if modify | ||
| 230 | (setq end (funcall to-fn beg end)) | ||
| 231 | (format-insert-annotations (funcall to-fn beg end)))) | ||
| 232 | (setq format (cdr format))))))) | ||
| 233 | |||
| 234 | (defun format-write-file (filename format) | ||
| 235 | "Write current buffer into a FILE using some FORMAT. | ||
| 236 | Makes buffer visit that file and sets the format as the default for future | ||
| 237 | saves. If the buffer is already visiting a file, you can specify a directory | ||
| 238 | name as FILE, to write a file of the same old name in that directory." | ||
| 239 | (interactive | ||
| 240 | ;; Same interactive spec as write-file, plus format question. | ||
| 241 | (let* ((file (if buffer-file-name | ||
| 242 | (read-file-name "Write file: " | ||
| 243 | nil nil nil nil) | ||
| 244 | (read-file-name "Write file: " | ||
| 245 | (cdr (assq 'default-directory | ||
| 246 | (buffer-local-variables))) | ||
| 247 | nil nil (buffer-name)))) | ||
| 248 | (fmt (format-read (format "Write file `%s' in format: " | ||
| 249 | (file-name-nondirectory file))))) | ||
| 250 | (list file fmt))) | ||
| 251 | (setq buffer-file-format format) | ||
| 252 | (write-file filename)) | ||
| 253 | |||
| 254 | (defun format-read (&optional prompt) | ||
| 255 | "Read and return the name of a format. | ||
| 256 | Return value is a list, like `buffer-file-format'; it may be nil. | ||
| 257 | Formats are defined in `format-alist'. Optional arg is the PROMPT to use." | ||
| 258 | (let* ((table (mapcar (lambda (x) (list (symbol-name (car x)))) | ||
| 259 | format-alist)) | ||
| 260 | (ans (completing-read (or prompt "Format: ") table nil t))) | ||
| 261 | (if (not (equal "" ans)) (list (intern ans))))) | ||
| 262 | |||
| 263 | |||
| 264 | ;;; | ||
| 265 | ;;; Below are some functions that may be useful in writing encoding and | ||
| 266 | ;;; decoding functions for use in format-alist. | ||
| 267 | ;;; | ||
| 268 | |||
| 269 | (defun format-replace-strings (alist &optional reverse beg end) | ||
| 270 | "Do multiple replacements on the buffer. | ||
| 271 | ALIST is a list of (from . to) pairs, which should be proper arguments to | ||
| 272 | `search-forward' and `replace-match' respectively. | ||
| 273 | Optional 2nd arg REVERSE, if non-nil, means the pairs are (to . from), so that | ||
| 274 | you can use the same list in both directions if it contains only literal | ||
| 275 | strings. | ||
| 276 | Optional args BEGIN and END specify a region of the buffer to operate on." | ||
| 277 | (save-excursion | ||
| 278 | (save-restriction | ||
| 279 | (or beg (setq beg (point-min))) | ||
| 280 | (if end (narrow-to-region (point-min) end)) | ||
| 281 | (while alist | ||
| 282 | (let ((from (if reverse (cdr (car alist)) (car (car alist)))) | ||
| 283 | (to (if reverse (car (cdr alist)) (cdr (car alist))))) | ||
| 284 | (goto-char beg) | ||
| 285 | (while (search-forward from nil t) | ||
| 286 | (goto-char (match-beginning 0)) | ||
| 287 | (insert to) | ||
| 288 | (set-text-properties (- (point) (length to)) (point) | ||
| 289 | (text-properties-at (point))) | ||
| 290 | (delete-region (point) (+ (point) (- (match-end 0) | ||
| 291 | (match-beginning 0))))) | ||
| 292 | (setq alist (cdr alist))))))) | ||
| 293 | |||
| 294 | ;;; Some list-manipulation functions that we need. | ||
| 295 | |||
| 296 | (defun format-delq-cons (cons list) | ||
| 297 | "Remove the given CONS from LIST by side effect, | ||
| 298 | and return the new LIST. Since CONS could be the first element | ||
| 299 | of LIST, write `\(setq foo \(format-delq-cons element foo))' to be sure of | ||
| 300 | changing the value of `foo'." | ||
| 301 | (if (eq cons list) | ||
| 302 | (cdr list) | ||
| 303 | (let ((p list)) | ||
| 304 | (while (not (eq (cdr p) cons)) | ||
| 305 | (if (null p) (error "format-delq-cons: not an element.")) | ||
| 306 | (setq p (cdr p))) | ||
| 307 | ;; Now (cdr p) is the cons to delete | ||
| 308 | (setcdr p (cdr cons)) | ||
| 309 | list))) | ||
| 310 | |||
| 311 | (defun format-make-relatively-unique (a b) | ||
| 312 | "Delete common elements of lists A and B, return as pair. | ||
| 313 | Compares using `equal'." | ||
| 314 | (let* ((acopy (copy-sequence a)) | ||
| 315 | (bcopy (copy-sequence b)) | ||
| 316 | (tail acopy)) | ||
| 317 | (while tail | ||
| 318 | (let ((dup (member (car tail) bcopy)) | ||
| 319 | (next (cdr tail))) | ||
| 320 | (if dup (setq acopy (format-delq-cons tail acopy) | ||
| 321 | bcopy (format-delq-cons dup bcopy))) | ||
| 322 | (setq tail next))) | ||
| 323 | (cons acopy bcopy))) | ||
| 324 | |||
| 325 | (defun format-common-tail (a b) | ||
| 326 | "Given two lists that have a common tail, return it. | ||
| 327 | Compares with `equal', and returns the part of A that is equal to the | ||
| 328 | equivalent part of B. If even the last items of the two are not equal, | ||
| 329 | returns nil." | ||
| 330 | (let ((la (length a)) | ||
| 331 | (lb (length b))) | ||
| 332 | ;; Make sure they are the same length | ||
| 333 | (if (> la lb) | ||
| 334 | (setq a (nthcdr (- la lb) a)) | ||
| 335 | (setq b (nthcdr (- lb la) b)))) | ||
| 336 | (while (not (equal a b)) | ||
| 337 | (setq a (cdr a) | ||
| 338 | b (cdr b))) | ||
| 339 | a) | ||
| 340 | |||
| 341 | (defun format-reorder (items order) | ||
| 342 | "Arrange ITEMS to following partial ORDER. | ||
| 343 | Elements of ITEMS equal to elements of ORDER will be rearranged to follow the | ||
| 344 | ORDER. Unmatched items will go last." | ||
| 345 | (if order | ||
| 346 | (let ((item (member (car order) items))) | ||
| 347 | (if item | ||
| 348 | (cons (car item) | ||
| 349 | (format-reorder (format-delq-cons item items) | ||
| 350 | (cdr order))) | ||
| 351 | (format-reorder items (cdr order)))) | ||
| 352 | items)) | ||
| 353 | |||
| 354 | (put 'face 'format-list-valued t) ; These text-properties take values | ||
| 355 | (put 'unknown 'format-list-valued t) ; that are lists, the elements of which | ||
| 356 | ; should be considered separately. | ||
| 357 | ; See format-deannotate-region and | ||
| 358 | ; format-annotate-region. | ||
| 359 | |||
| 360 | ;;; | ||
| 361 | ;;; Decoding | ||
| 362 | ;;; | ||
| 363 | |||
| 364 | (defun format-deannotate-region (from to translations next-fn) | ||
| 365 | "Translate annotations in the region into text properties. | ||
| 366 | This sets text properties between FROM to TO as directed by the | ||
| 367 | TRANSLATIONS and NEXT-FN arguments. | ||
| 368 | |||
| 369 | NEXT-FN is a function that searches forward from point for an annotation. | ||
| 370 | It should return a list of 4 elements: \(BEGIN END NAME POSITIVE). BEGIN and | ||
| 371 | END are buffer positions bounding the annotation, NAME is the name searched | ||
| 372 | for in TRANSLATIONS, and POSITIVE should be non-nil if this annotation marks | ||
| 373 | the beginning of a region with some property, or nil if it ends the region. | ||
| 374 | NEXT-FN should return nil if there are no annotations after point. | ||
| 375 | |||
| 376 | The basic format of the TRANSLATIONS argument is described in the | ||
| 377 | documentation for the `format-annotate-region' function. There are some | ||
| 378 | additional things to keep in mind for decoding, though: | ||
| 379 | |||
| 380 | When an annotation is found, the TRANSLATIONS list is searched for a | ||
| 381 | text-property name and value that corresponds to that annotation. If the | ||
| 382 | text-property has several annotations associated with it, it will be used only | ||
| 383 | if the other annotations are also in effect at that point. The first match | ||
| 384 | found whose annotations are all present is used. | ||
| 385 | |||
| 386 | The text property thus determined is set to the value over the region between | ||
| 387 | the opening and closing annotations. However, if the text-property name has a | ||
| 388 | non-nil `format-list-valued' property, then the value will be consed onto the | ||
| 389 | surrounding value of the property, rather than replacing that value. | ||
| 390 | |||
| 391 | There are some special symbols that can be used in the \"property\" slot of | ||
| 392 | the TRANSLATIONS list: PARAMETER and FUNCTION \(spelled in uppercase). | ||
| 393 | Annotations listed under the pseudo-property PARAMETER are considered to be | ||
| 394 | arguments of the immediately surrounding annotation; the text between the | ||
| 395 | opening and closing parameter annotations is deleted from the buffer but saved | ||
| 396 | as a string. The surrounding annotation should be listed under the | ||
| 397 | pseudo-property FUNCTION. Instead of inserting a text-property for this | ||
| 398 | annotation, the function listed in the VALUE slot is called to make whatever | ||
| 399 | changes are appropriate. The function's first two arguments are the START and | ||
| 400 | END locations, and the rest of the arguments are any PARAMETERs found in that | ||
| 401 | region. | ||
| 402 | |||
| 403 | Any annotations that are found by NEXT-FN but not defined by TRANSLATIONS | ||
| 404 | are saved as values of the `unknown' text-property \(which is list-valued). | ||
| 405 | The TRANSLATIONS list should usually contain an entry of the form | ||
| 406 | \(unknown \(nil format-annotate-value)) | ||
| 407 | to write these unknown annotations back into the file." | ||
| 408 | (save-excursion | ||
| 409 | (save-restriction | ||
| 410 | (narrow-to-region (point-min) to) | ||
| 411 | (goto-char from) | ||
| 412 | (let (next open-ans todo loc unknown-ans) | ||
| 413 | (while (setq next (funcall next-fn)) | ||
| 414 | (let* ((loc (nth 0 next)) | ||
| 415 | (end (nth 1 next)) | ||
| 416 | (name (nth 2 next)) | ||
| 417 | (positive (nth 3 next)) | ||
| 418 | (found nil)) | ||
| 419 | |||
| 420 | ;; Delete the annotation | ||
| 421 | (delete-region loc end) | ||
| 422 | (if positive | ||
| 423 | ;; Positive annotations are stacked, remembering location | ||
| 424 | (setq open-ans (cons (list name loc) open-ans)) | ||
| 425 | ;; It is a negative annotation: | ||
| 426 | ;; Close the top annotation & add its text property. | ||
| 427 | ;; If the file's nesting is messed up, the close might not match | ||
| 428 | ;; the top thing on the open-annotations stack. | ||
| 429 | ;; If no matching annotation is open, just ignore the close. | ||
| 430 | (if (not (assoc name open-ans)) | ||
| 431 | (message "Extra closing annotation (%s) in file" name) | ||
| 432 | ;; If one is open, but not on the top of the stack, close | ||
| 433 | ;; the things in between as well. Set `found' when the real | ||
| 434 | ;; oneis closed. | ||
| 435 | (while (not found) | ||
| 436 | (let* ((top (car open-ans)) ; first on stack: should match. | ||
| 437 | (top-name (car top)) | ||
| 438 | (start (car (cdr top))) ; location of start | ||
| 439 | (params (cdr (cdr top))) ; parameters | ||
| 440 | (aalist translations) | ||
| 441 | (matched nil)) | ||
| 442 | (if (equal name top-name) | ||
| 443 | (setq found t) | ||
| 444 | (message "Improper nesting in file.")) | ||
| 445 | ;; Look through property names in TRANSLATIONS | ||
| 446 | (while aalist | ||
| 447 | (let ((prop (car (car aalist))) | ||
| 448 | (alist (cdr (car aalist)))) | ||
| 449 | ;; And look through values for each property | ||
| 450 | (while alist | ||
| 451 | (let ((value (car (car alist))) | ||
| 452 | (ans (cdr (car alist)))) | ||
| 453 | (if (member top-name ans) | ||
| 454 | ;; This annotation is listed, but still have to | ||
| 455 | ;; check if multiple annotations are satisfied | ||
| 456 | (if (member 'nil (mapcar | ||
| 457 | (lambda (r) | ||
| 458 | (assoc r open-ans)) | ||
| 459 | ans)) | ||
| 460 | nil ; multiple ans not satisfied | ||
| 461 | ;; Yes, use the current property name & | ||
| 462 | ;; value. Set loop variables to nil so loop | ||
| 463 | ;; will exit. | ||
| 464 | (setq alist nil aalist nil matched t | ||
| 465 | ;; pop annotation off stack. | ||
| 466 | open-ans (cdr open-ans)) | ||
| 467 | (cond | ||
| 468 | ;; Check for pseudo-properties | ||
| 469 | ((eq prop 'PARAMETER) | ||
| 470 | ;; This is a parameter of the top open ann: | ||
| 471 | ;; delete text and use as arg. | ||
| 472 | (if open-ans | ||
| 473 | ;; (If nothing open, discard). | ||
| 474 | (setq open-ans | ||
| 475 | (cons (append (car open-ans) | ||
| 476 | (list | ||
| 477 | (buffer-substring | ||
| 478 | start loc))) | ||
| 479 | (cdr open-ans)))) | ||
| 480 | (delete-region start loc)) | ||
| 481 | ((eq prop 'FUNCTION) | ||
| 482 | ;; Not a property, but a function to call. | ||
| 483 | (let ((rtn (apply value start loc params))) | ||
| 484 | (if rtn (setq todo (cons rtn todo))))) | ||
| 485 | (t | ||
| 486 | ;; Normal property/value pair | ||
| 487 | (setq todo | ||
| 488 | (cons (list start loc prop value) | ||
| 489 | todo))))))) | ||
| 490 | (setq alist (cdr alist)))) | ||
| 491 | (setq aalist (cdr aalist))) | ||
| 492 | (if matched | ||
| 493 | nil | ||
| 494 | ;; Didn't find any match for the annotation: | ||
| 495 | ;; Store as value of text-property `unknown'. | ||
| 496 | (setq open-ans (cdr open-ans)) | ||
| 497 | (setq todo (cons (list start loc 'unknown top-name) | ||
| 498 | todo)) | ||
| 499 | (setq unknown-ans (cons name unknown-ans))))))))) | ||
| 500 | |||
| 501 | ;; Once entire file has been scanned, add the properties. | ||
| 502 | (while todo | ||
| 503 | (let* ((item (car todo)) | ||
| 504 | (from (nth 0 item)) | ||
| 505 | (to (nth 1 item)) | ||
| 506 | (prop (nth 2 item)) | ||
| 507 | (val (nth 3 item))) | ||
| 508 | |||
| 509 | (put-text-property | ||
| 510 | from to prop | ||
| 511 | (cond ((numberp val) ; add to ambient value if numeric | ||
| 512 | (+ val (or (get-text-property from prop) 0))) | ||
| 513 | ((get prop 'format-list-valued) ; value gets consed onto | ||
| 514 | ; list-valued properties | ||
| 515 | (let ((prev (get-text-property from prop))) | ||
| 516 | (cons val (if (listp prev) prev (list prev))))) | ||
| 517 | (t val)))) ; normally, just set to val. | ||
| 518 | (setq todo (cdr todo))) | ||
| 519 | |||
| 520 | (if unknown-ans | ||
| 521 | (message "Unknown annotations: %s" unknown-ans)))))) | ||
| 522 | |||
| 523 | ;;; | ||
| 524 | ;;; Encoding | ||
| 525 | ;;; | ||
| 526 | |||
| 527 | (defun format-insert-annotations (list &optional offset) | ||
| 528 | "Apply list of annotations to buffer as `write-region' would. | ||
| 529 | Inserts each element of the given LIST of buffer annotations at its | ||
| 530 | appropriate place. Use second arg OFFSET if the annotations' locations are | ||
| 531 | not relative to the beginning of the buffer: annotations will be inserted | ||
| 532 | at their location-OFFSET+1 \(ie, the offset is treated as the character number | ||
| 533 | of the first character in the buffer)." | ||
| 534 | (if (not offset) | ||
| 535 | (setq offset 0) | ||
| 536 | (setq offset (1- offset))) | ||
| 537 | (let ((l (reverse list))) | ||
| 538 | (while l | ||
| 539 | (goto-char (- (car (car l)) offset)) | ||
| 540 | (insert (cdr (car l))) | ||
| 541 | (setq l (cdr l))))) | ||
| 542 | |||
| 543 | (defun format-annotate-value (old new) | ||
| 544 | "Return OLD and NEW as a \(close . open) annotation pair. | ||
| 545 | Useful as a default function for TRANSLATIONS alist when the value of the text | ||
| 546 | property is the name of the annotation that you want to use, as it is for the | ||
| 547 | `unknown' text property." | ||
| 548 | (cons (if old (list old)) | ||
| 549 | (if new (list new)))) | ||
| 550 | |||
| 551 | (defun format-annotate-region (from to trans format-fn ignore) | ||
| 552 | "Generate annotations for text properties in the region. | ||
| 553 | Searches for changes between FROM and TO, and describes them with a list of | ||
| 554 | annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text | ||
| 555 | properties not to consider; any text properties that are neither ignored nor | ||
| 556 | listed in TRANSLATIONS are warned about. | ||
| 557 | If you actually want to modify the region, give the return value of this | ||
| 558 | function to `format-insert-annotations'. | ||
| 559 | |||
| 560 | Format of the TRANSLATIONS argument: | ||
| 561 | |||
| 562 | Each element is a list whose car is a PROPERTY, and the following | ||
| 563 | elements are VALUES of that property followed by the names of zero or more | ||
| 564 | ANNOTATIONS. Whenever the property takes on that value, the annotations | ||
| 565 | \(as formatted by FORMAT-FN) are inserted into the file. | ||
| 566 | When the property stops having that value, the matching negated annotation | ||
| 567 | will be inserted \(it may actually be closed earlier and reopened, if | ||
| 568 | necessary, to keep proper nesting). | ||
| 569 | |||
| 570 | If the property's value is a list, then each element of the list is dealt with | ||
| 571 | separately. | ||
| 572 | |||
| 573 | If a VALUE is numeric, then it is assumed that there is a single annotation | ||
| 574 | and each occurrence of it increments the value of the property by that number. | ||
| 575 | Thus, given the entry \(left-margin \(4 \"indent\")), if the left margin | ||
| 576 | changes from 4 to 12, two <indent> annotations will be generated. | ||
| 577 | |||
| 578 | If the VALUE is nil, then instead of annotations, a function should be | ||
| 579 | specified. This function is used as a default: it is called for all | ||
| 580 | transitions not explicitly listed in the table. The function is called with | ||
| 581 | two arguments, the OLD and NEW values of the property. It should return | ||
| 582 | lists of annotations like `format-annotate-location' does. | ||
| 583 | |||
| 584 | The same structure can be used in reverse for reading files." | ||
| 585 | (let ((all-ans nil) ; All annotations - becomes return value | ||
| 586 | (open-ans nil) ; Annotations not yet closed | ||
| 587 | (loc nil) ; Current location | ||
| 588 | (not-found nil)) ; Properties that couldn't be saved | ||
| 589 | (while (or (null loc) | ||
| 590 | (and (setq loc (next-property-change loc nil to)) | ||
| 591 | (< loc to))) | ||
| 592 | (or loc (setq loc from)) | ||
| 593 | (let* ((ans (format-annotate-location loc (= loc from) ignore trans)) | ||
| 594 | (neg-ans (format-reorder (aref ans 0) open-ans)) | ||
| 595 | (pos-ans (aref ans 1)) | ||
| 596 | (ignored (aref ans 2))) | ||
| 597 | (setq not-found (append ignored not-found) | ||
| 598 | ignore (append ignored ignore)) | ||
| 599 | ;; First do the negative (closing) annotations | ||
| 600 | (while neg-ans | ||
| 601 | ;; Check if it's missing. This can happen (eg, a numeric property | ||
| 602 | ;; going negative can generate closing annotations before there are | ||
| 603 | ;; any open). Warn user & ignore. | ||
| 604 | (if (not (member (car neg-ans) open-ans)) | ||
| 605 | (message "Can't close %s: not open." (car neg-ans)) | ||
| 606 | (while (not (equal (car neg-ans) (car open-ans))) | ||
| 607 | ;; To close anno. N, need to first close ans 1 to N-1, | ||
| 608 | ;; remembering to re-open them later. | ||
| 609 | (setq pos-ans (cons (car open-ans) pos-ans)) | ||
| 610 | (setq all-ans | ||
| 611 | (cons (cons loc (funcall format-fn (car open-ans) nil)) | ||
| 612 | all-ans)) | ||
| 613 | (setq open-ans (cdr open-ans))) | ||
| 614 | ;; Now remove the one we're really interested in from open list. | ||
| 615 | (setq open-ans (cdr open-ans)) | ||
| 616 | ;; And put the closing annotation here. | ||
| 617 | (setq all-ans | ||
| 618 | (cons (cons loc (funcall format-fn (car neg-ans) nil)) | ||
| 619 | all-ans))) | ||
| 620 | (setq neg-ans (cdr neg-ans))) | ||
| 621 | ;; Now deal with positive (opening) annotations | ||
| 622 | (let ((p pos-ans)) | ||
| 623 | (while pos-ans | ||
| 624 | (setq open-ans (cons (car pos-ans) open-ans)) | ||
| 625 | (setq all-ans | ||
| 626 | (cons (cons loc (funcall format-fn (car pos-ans) t)) | ||
| 627 | all-ans)) | ||
| 628 | (setq pos-ans (cdr pos-ans)))))) | ||
| 629 | |||
| 630 | ;; Close any annotations still open | ||
| 631 | (while open-ans | ||
| 632 | (setq all-ans | ||
| 633 | (cons (cons to (funcall format-fn (car open-ans) nil)) | ||
| 634 | all-ans)) | ||
| 635 | (setq open-ans (cdr open-ans))) | ||
| 636 | (if not-found | ||
| 637 | (message "These text properties could not be saved:\n %s" | ||
| 638 | not-found)) | ||
| 639 | (nreverse all-ans))) | ||
| 640 | |||
| 641 | ;;; Internal functions for format-annotate-region. | ||
| 642 | |||
| 643 | (defun format-annotate-location (loc all ignore trans) | ||
| 644 | "Return annotation(s) needed at LOCATION. | ||
| 645 | This includes any properties that change between LOC-1 and LOC. | ||
| 646 | If ALL is true, don't look at previous location, but generate annotations for | ||
| 647 | all non-nil properties. | ||
| 648 | Third argument IGNORE is a list of text-properties not to consider. | ||
| 649 | |||
| 650 | Return value is a vector of 3 elements: | ||
| 651 | 1. List of names of the annotations to close | ||
| 652 | 2. List of the names of annotations to open. | ||
| 653 | 3. List of properties that were ignored or couldn't be annotated." | ||
| 654 | (let* ((prev-loc (1- loc)) | ||
| 655 | (before-plist (if all nil (text-properties-at prev-loc))) | ||
| 656 | (after-plist (text-properties-at loc)) | ||
| 657 | p negatives positives prop props not-found) | ||
| 658 | ;; make list of all property names involved | ||
| 659 | (setq p before-plist) | ||
| 660 | (while p | ||
| 661 | (if (not (memq (car p) props)) | ||
| 662 | (setq props (cons (car p) props))) | ||
| 663 | (setq p (cdr (cdr p)))) | ||
| 664 | (setq p after-plist) | ||
| 665 | (while p | ||
| 666 | (if (not (memq (car p) props)) | ||
| 667 | (setq props (cons (car p) props))) | ||
| 668 | (setq p (cdr (cdr p)))) | ||
| 669 | |||
| 670 | (while props | ||
| 671 | (setq prop (car props) | ||
| 672 | props (cdr props)) | ||
| 673 | (if (memq prop ignore) | ||
| 674 | nil ; If it's been ignored before, ignore it now. | ||
| 675 | (let ((before (if all nil (car (cdr (memq prop before-plist))))) | ||
| 676 | (after (car (cdr (memq prop after-plist))))) | ||
| 677 | (if (equal before after) | ||
| 678 | nil ; no change; ignore | ||
| 679 | (let ((result (format-annotate-single-property-change | ||
| 680 | prop before after trans))) | ||
| 681 | (if (not result) | ||
| 682 | (setq not-found (cons prop not-found)) | ||
| 683 | (setq negatives (nconc negatives (car result)) | ||
| 684 | positives (nconc positives (cdr result))))))))) | ||
| 685 | (vector negatives positives not-found))) | ||
| 686 | |||
| 687 | (defun format-annotate-single-property-change (prop old new trans) | ||
| 688 | "Return annotations for PROPERTY changing from OLD to NEW. | ||
| 689 | These are searched for in the TRANSLATIONS alist. | ||
| 690 | If NEW does not appear in the list, but there is a default function, then that | ||
| 691 | function is called. | ||
| 692 | Annotations to open and to close are returned as a dotted pair." | ||
| 693 | (let ((prop-alist (cdr (assoc prop trans))) | ||
| 694 | default) | ||
| 695 | (if (not prop-alist) | ||
| 696 | nil | ||
| 697 | ;; If property is numeric, nil means 0 | ||
| 698 | (cond ((and (numberp old) (null new)) | ||
| 699 | (setq new 0)) | ||
| 700 | ((and (numberp new) (null old)) | ||
| 701 | (setq old 0))) | ||
| 702 | ;; If either old or new is a list, have to treat both that way. | ||
| 703 | (if (or (consp old) (consp new)) | ||
| 704 | (let* ((old (if (listp old) old (list old))) | ||
| 705 | (new (if (listp new) new (list new))) | ||
| 706 | (tail (format-common-tail old new)) | ||
| 707 | close open) | ||
| 708 | (while old | ||
| 709 | (setq close | ||
| 710 | (append (car (format-annotate-atomic-property-change | ||
| 711 | prop-alist (car old) nil)) | ||
| 712 | close) | ||
| 713 | old (cdr old))) | ||
| 714 | (while new | ||
| 715 | (setq open | ||
| 716 | (append (cdr (format-annotate-atomic-property-change | ||
| 717 | prop-alist nil (car new))) | ||
| 718 | open) | ||
| 719 | new (cdr new))) | ||
| 720 | (format-make-relatively-unique close open)) | ||
| 721 | (format-annotate-atomic-property-change prop-alist old new))))) | ||
| 722 | |||
| 723 | (defun format-annotate-atomic-property-change (prop-alist old new) | ||
| 724 | "Internal function annotate a single property change. | ||
| 725 | PROP-ALIST is the relevant segement of a TRANSLATIONS list. | ||
| 726 | OLD and NEW are the values." | ||
| 727 | (cond | ||
| 728 | ;; Numerical annotation - use difference | ||
| 729 | ((and (numberp old) (numberp new)) | ||
| 730 | (let* ((entry (progn | ||
| 731 | (while (and (car (car prop-alist)) | ||
| 732 | (not (numberp (car (car prop-alist))))) | ||
| 733 | (setq prop-alist (cdr prop-alist))) | ||
| 734 | (car prop-alist))) | ||
| 735 | (increment (car (car prop-alist))) | ||
| 736 | (n (ceiling (/ (float (- new old)) (float increment)))) | ||
| 737 | (anno (car (cdr (car prop-alist))))) | ||
| 738 | (if (> n 0) | ||
| 739 | (cons nil (make-list n anno)) | ||
| 740 | (cons (make-list (- n) anno) nil)))) | ||
| 741 | |||
| 742 | ;; Standard annotation | ||
| 743 | (t (let ((close (and old (cdr (assoc old prop-alist)))) | ||
| 744 | (open (and new (cdr (assoc new prop-alist))))) | ||
| 745 | (if (or close open) | ||
| 746 | (format-make-relatively-unique close open) | ||
| 747 | ;; Call "Default" function, if any | ||
| 748 | (let ((default (assq nil prop-alist))) | ||
| 749 | (if default | ||
| 750 | (funcall (car (cdr default)) old new)))))))) | ||
| 751 | |||
| 752 | ;; format.el ends here | ||