Mercurial > hg > xemacs-beta
annotate lisp/format.el @ 5746:b8c2808b33d4
Document #'events-to-keys some more, use it less.
lisp/ChangeLog addition:
2013-07-10 Aidan Kehoe <kehoea@parhasard.net>
* minibuf.el (get-user-response):
* cmdloop.el (y-or-n-p-minibuf):
No need to call #'events-to-keys in these two functions,
#'lookup-key accepts events directly.
* keymap.el:
* keymap.el (events-to-keys):
Document this function some more.
Stop passing strings through unexamined, treat them as vectors of
characters.
Event keys are never integers, remove some code that only ran if
(integerp (event-key ce)).
Event keys are never numbers, don't check for that.
Don't create (menu-selection call-interactively function-name)
keystrokes for menu choices, #'character-to-event doesn't
understand that syntax, so nothing uses it.
Don't ever accept mouse events, #'character-to-event doesn't
accept our synthesising of them.
src/ChangeLog addition:
2013-07-10 Aidan Kehoe <kehoea@parhasard.net>
* keymap.c:
* keymap.c (key_desc_list_to_event):
Drop the allow_menu_events argument.
Don't accept lists starting with Qmenu_selection as describing
keys, nothing generates them in a way this function
understands. The intention is reasonable but the implementation
was never documented and never finished.
* keymap.c (syms_of_keymap):
Drop Qmenu_selection.
* events.c (Fcharacter_to_event):
* keymap.h:
Drop the allow_menu_events argument to key_desc_list_to_event.
| author | Aidan Kehoe <kehoea@parhasard.net> |
|---|---|
| date | Wed, 10 Jul 2013 14:14:30 +0100 |
| parents | ac37a5f7e5be |
| children |
| rev | line source |
|---|---|
| 428 | 1 ;;; format.el --- read and save files in multiple formats |
| 2 | |
| 3 ;; Copyright (c) 1994, 1995, 1997 Free Software Foundation | |
| 4 | |
| 5 ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu> | |
| 6 ;; Keywords: extensions, dumped | |
| 7 | |
| 8 ;; This file is part of XEmacs. | |
| 9 | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
10 ;; XEmacs is free software: you can redistribute it and/or modify it |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
11 ;; under the terms of the GNU General Public License as published by the |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
12 ;; Free Software Foundation, either version 3 of the License, or (at your |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
13 ;; option) any later version. |
| 428 | 14 |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
15 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
16 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
17 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
18 ;; for more details. |
| 428 | 19 |
| 20 ;; You should have received a copy of the GNU General Public License | |
|
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5270
diff
changeset
|
21 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
| 428 | 22 |
| 23 ;;; Synched up with: Emacs 20.2. | |
| 24 | |
| 25 ;;; Commentary: | |
| 26 | |
| 27 ;; This file is dumped with XEmacs. | |
| 28 | |
| 29 ;; This file defines a unified mechanism for saving & loading files stored | |
| 30 ;; in different formats. `format-alist' contains information that directs | |
| 31 ;; Emacs to call an encoding or decoding function when reading or writing | |
| 32 ;; files that match certain conditions. | |
| 33 ;; | |
| 34 ;; When a file is visited, its format is determined by matching the | |
| 35 ;; beginning of the file against regular expressions stored in | |
| 36 ;; `format-alist'. If this fails, you can manually translate the buffer | |
| 37 ;; using `format-decode-buffer'. In either case, the formats used are | |
| 38 ;; listed in the variable `buffer-file-format', and become the default | |
| 39 ;; format for saving the buffer. To save a buffer in a different format, | |
| 40 ;; change this variable, or use `format-write-file'. | |
| 41 ;; | |
| 42 ;; Auto-save files are normally created in the same format as the visited | |
| 43 ;; file, but the variable `auto-save-file-format' can be set to a | |
| 44 ;; particularly fast or otherwise preferred format to be used for | |
| 45 ;; auto-saving (or nil to do no encoding on auto-save files, but then you | |
| 46 ;; risk losing any text-properties in the buffer). | |
| 47 ;; | |
| 48 ;; You can manually translate a buffer into or out of a particular format | |
| 49 ;; with the functions `format-encode-buffer' and `format-decode-buffer'. | |
| 50 ;; To translate just the region use the functions `format-encode-region' | |
| 51 ;; and `format-decode-region'. | |
| 52 ;; | |
| 53 ;; You can define a new format by writing the encoding and decoding | |
| 54 ;; functions, and adding an entry to `format-alist'. See enriched.el for | |
| 55 ;; an example of how to implement a file format. There are various | |
| 56 ;; functions defined in this file that may be useful for writing the | |
| 57 ;; encoding and decoding functions: | |
| 58 ;; * `format-annotate-region' and `format-deannotate-region' allow a | |
| 59 ;; single alist of information to be used for encoding and decoding. | |
| 60 ;; The alist defines a correspondence between strings in the file | |
| 61 ;; ("annotations") and text-properties in the buffer. | |
| 62 ;; * `format-replace-strings' is similarly useful for doing simple | |
| 63 ;; string->string translations in a reversible manner. | |
| 64 | |
| 65 ;;; Code: | |
| 66 | |
| 67 (put 'buffer-file-format 'permanent-local t) | |
| 68 | |
| 69 (defvar format-alist | |
| 70 '( | |
| 71 ; (image/jpeg "JPEG image" "\377\330\377\340\000\020JFIF" | |
| 72 ; image-decode-jpeg nil t image-mode) | |
| 73 ; (image/gif "GIF image" "GIF8[79]" | |
| 74 ; image-decode-gif nil t image-mode) | |
| 75 ; (image/png "Portable Network Graphics" "\211PNG" | |
| 76 ; image-decode-png nil t image-mode) | |
| 77 ; (image/x-xpm "XPM image" "/\\* XPM \\*/" | |
| 78 ; image-decode-xpm nil t image-mode) | |
| 79 | |
| 80 ; ;; TIFF files have lousy magic | |
| 81 ; (image/tiff "TIFF image" "II\\*\000" | |
| 82 ; image-decode-tiff nil t image-mode) ;; TIFF 6.0 big-endian | |
| 83 ; (image/tiff "TIFF image" "MM\000\\*" | |
| 84 ; image-decode-tiff nil t image-mode) ;; TIFF 6.0 little-endian | |
| 85 | |
| 86 (text/enriched "Extended MIME text/enriched format." | |
| 87 "Content-[Tt]ype:[ \t]*text/enriched" | |
| 88 enriched-decode enriched-encode t enriched-mode) | |
| 89 (text/richtext "Extended MIME obsolete text/richtext format." | |
| 90 "Content-[Tt]ype:[ \t]*text/richtext" | |
| 91 richtext-decode richtext-encode t enriched-mode) | |
| 92 (plain "ISO 8859-1 standard format, no text properties." | |
| 93 ;; Plain only exists so that there is an obvious neutral choice in | |
| 94 ;; the completion list. | |
| 95 nil nil nil nil nil) | |
| 96 ;; (ibm "IBM Code Page 850 (DOS)" | |
| 97 ;; "1\\(^\\)" | |
| 98 ;; "recode ibm-pc:latin1" "recode latin1:ibm-pc" t nil) | |
| 99 ;; (mac "Apple Macintosh" | |
| 100 ;; "1\\(^\\)" | |
| 101 ;; "recode mac:latin1" "recode latin1:mac" t nil) | |
| 102 ;; (hp "HP Roman8" | |
| 103 ;; "1\\(^\\)" | |
| 104 ;; "recode roman8:latin1" "recode latin1:roman8" t nil) | |
| 105 ;; (TeX "TeX (encoding)" | |
| 106 ;; "1\\(^\\)" | |
| 107 ;; iso-tex2iso iso-iso2tex t nil) | |
| 108 ;; (gtex "German TeX (encoding)" | |
| 109 ;; "1\\(^\\)" | |
| 110 ;; iso-gtex2iso iso-iso2gtex t nil) | |
| 111 ;; (html "HTML (encoding)" | |
| 112 ;; "1\\(^\\)" | |
| 113 ;; "recode html:latin1" "recode latin1:html" t nil) | |
| 114 ;; (rot13 "rot13" | |
| 115 ;; "1\\(^\\)" | |
| 116 ;; "tr a-mn-z n-za-m" "tr a-mn-z n-za-m" t nil) | |
| 117 ;; (duden "Duden Ersatzdarstellung" | |
| 118 ;; "1\\(^\\)" | |
| 119 ;; "diac" iso-iso2duden t nil) | |
| 120 ;; (de646 "German ASCII (ISO 646)" | |
| 121 ;; "1\\(^\\)" | |
| 122 ;; "recode iso646-ge:latin1" "recode latin1:iso646-ge" t nil) | |
| 123 ;; (denet "net German" | |
| 124 ;; "1\\(^\\)" | |
| 125 ;; iso-german iso-cvt-read-only t nil) | |
| 126 ;; (esnet "net Spanish" | |
| 127 ;; "1\\(^\\)" | |
| 128 ;; iso-spanish iso-cvt-read-only t nil) | |
| 129 ) | |
| 130 "List of information about understood file formats. | |
| 131 Elements are of the form \(NAME DOC-STR REGEXP FROM-FN TO-FN MODIFY MODE-FN). | |
| 132 | |
| 133 NAME is a symbol, which is stored in `buffer-file-format'. | |
| 134 | |
| 135 DOC-STR should be a single line providing more information about the | |
| 136 format. It is currently unused, but in the future will be shown to | |
| 137 the user if they ask for more information. | |
| 138 | |
| 139 REGEXP is a regular expression to match against the beginning of the file; | |
| 140 it should match only files in that format. | |
| 141 | |
| 142 FROM-FN is called to decode files in that format; it gets two args, BEGIN | |
| 143 and END, and can make any modifications it likes, returning the new | |
| 144 end. It must make sure that the beginning of the file no longer | |
| 145 matches REGEXP, or else it will get called again. | |
| 146 Alternatively, FROM-FN can be a string, which specifies a shell command | |
| 147 (including options) to be used as a filter to perform the conversion. | |
| 148 | |
| 149 TO-FN is called to encode a region into that format; it is passed three | |
| 150 arguments: BEGIN, END, and BUFFER. BUFFER is the original buffer that | |
| 151 the data being written came from, which the function could use, for | |
| 152 example, to find the values of local variables. TO-FN should either | |
| 153 return a list of annotations like `write-region-annotate-functions', | |
| 154 or modify the region and return the new end. | |
| 155 Alternatively, TO-FN can be a string, which specifies a shell command | |
| 156 (including options) to be used as a filter to perform the conversion. | |
| 157 | |
| 158 MODIFY, if non-nil, means the TO-FN wants to modify the region. If nil, | |
| 159 TO-FN will not make any changes but will instead return a list of | |
| 160 annotations. | |
| 161 | |
| 162 MODE-FN, if specified, is called when visiting a file with that format.") | |
| 163 | |
| 164 ;;; Basic Functions (called from Lisp) | |
| 165 | |
| 166 (defun format-encode-run-method (method from to &optional buffer) | |
| 167 "Translate using function or shell script METHOD the text from FROM to TO. | |
| 168 If METHOD is a string, it is a shell command; | |
| 169 otherwise, it should be a Lisp function. | |
| 170 BUFFER should be the buffer that the output originally came from." | |
| 171 (if (stringp method) | |
| 172 (save-current-buffer | |
| 173 (set-buffer buffer) | |
| 174 (with-output-to-temp-buffer "*Format Errors*" | |
| 175 (shell-command-on-region from to method t nil)) | |
| 176 (point)) | |
| 177 (funcall method from to buffer))) | |
| 178 | |
| 179 (defun format-decode-run-method (method from to &optional buffer) | |
| 180 "Decode using function or shell script METHOD the text from FROM to TO. | |
| 181 If METHOD is a string, it is a shell command; | |
| 182 otherwise, it should be a Lisp function." | |
| 183 (if (stringp method) | |
| 184 (progn | |
| 185 (with-output-to-temp-buffer "*Format Errors*" | |
| 186 (shell-command-on-region from to method t nil)) | |
| 187 (point)) | |
| 188 (funcall method from to))) | |
| 189 | |
| 190 (defun format-annotate-function (format from to orig-buf) | |
| 191 "Return annotations for writing region as FORMAT. | |
| 192 FORMAT is a symbol naming one of the formats defined in `format-alist', | |
| 193 it must be a single symbol, not a list like `buffer-file-format'. | |
| 194 FROM and TO delimit the region to be operated on in the current buffer. | |
| 195 ORIG-BUF is the original buffer that the data came from. | |
| 196 This function works like a function on `write-region-annotate-functions': | |
| 197 it either returns a list of annotations, or returns with a different buffer | |
| 198 current, which contains the modified text to write. | |
| 199 | |
| 200 For most purposes, consider using `format-encode-region' instead." | |
| 201 ;; This function is called by write-region (actually build-annotations) | |
| 202 ;; for each element of buffer-file-format. | |
| 203 (let* ((info (assq format format-alist)) | |
| 204 (to-fn (nth 4 info)) | |
| 205 (modify (nth 5 info))) | |
| 206 (if to-fn | |
| 207 (if modify | |
| 208 ;; To-function wants to modify region. Copy to safe place. | |
| 209 (let ((copy-buf (get-buffer-create " *Format Temp*"))) | |
| 210 (copy-to-buffer copy-buf from to) | |
| 211 (set-buffer copy-buf) | |
| 212 (format-insert-annotations write-region-annotations-so-far from) | |
| 213 (format-encode-run-method to-fn (point-min) (point-max) orig-buf) | |
| 214 nil) | |
| 215 ;; Otherwise just call function, it will return annotations. | |
| 216 (funcall to-fn from to orig-buf))))) | |
| 217 | |
| 218 (defun format-decode (format length &optional visit-flag) | |
| 219 "Decode text from any known FORMAT. | |
| 220 FORMAT is a symbol appearing in `format-alist' or a list of such symbols, | |
| 221 or nil, in which case this function tries to guess the format of the data by | |
| 222 matching against the regular expressions in `format-alist'. After a match is | |
| 223 found and the region decoded, the alist is searched again from the beginning | |
| 224 for another match. | |
| 225 | |
| 226 Second arg LENGTH is the number of characters following point to operate on. | |
| 227 If optional third arg VISIT-FLAG is true, set `buffer-file-format' | |
| 228 to the list of formats used, and call any mode functions defined for those | |
| 229 formats. | |
| 230 | |
| 231 Returns the new length of the decoded region. | |
| 232 | |
| 233 For most purposes, consider using `format-decode-region' instead. | |
| 234 | |
| 235 This function is called by insert-file-contents whenever a file is read." | |
| 236 (let ((mod (buffer-modified-p)) | |
| 237 (begin (point)) | |
| 238 (end (+ (point) length))) | |
| 239 (if (null format) | |
| 240 ;; Figure out which format it is in, remember list in `format'. | |
| 241 (let ((try format-alist)) | |
| 242 (while try | |
| 243 (let* ((f (car try)) | |
| 244 (regexp (nth 2 f)) | |
| 245 (p (point))) | |
| 246 (if (and regexp (looking-at regexp) | |
| 247 (< (match-end 0) (+ begin length))) | |
| 248 (progn | |
| 249 (setq format (cons (car f) format)) | |
| 250 ;; Decode it | |
| 251 (if (nth 3 f) | |
| 252 (setq end (format-decode-run-method (nth 3 f) begin end))) | |
| 253 ;; Call visit function if required | |
| 254 (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) | |
| 255 ;; Safeguard against either of the functions changing pt. | |
| 256 (goto-char p) | |
| 257 ;; Rewind list to look for another format | |
| 258 (setq try format-alist)) | |
| 259 (setq try (cdr try)))))) | |
| 260 ;; Deal with given format(s) | |
| 261 (or (listp format) (setq format (list format))) | |
| 262 (let ((do format) f) | |
| 263 (while do | |
| 264 (or (setq f (assq (car do) format-alist)) | |
| 265 (error "Unknown format" (car do))) | |
| 266 ;; Decode: | |
| 267 (if (nth 3 f) | |
| 268 (setq end (format-decode-run-method (nth 3 f) begin end))) | |
| 269 ;; Call visit function if required | |
| 270 (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) | |
| 271 (setq do (cdr do))))) | |
| 272 (if visit-flag | |
| 273 (setq buffer-file-format format)) | |
| 274 (set-buffer-modified-p mod) | |
| 275 ;; Return new length of region | |
| 276 (- end begin))) | |
| 277 | |
| 278 ;;; | |
| 279 ;;; Interactive functions & entry points | |
| 280 ;;; | |
| 281 | |
| 282 (defun format-decode-buffer (&optional format) | |
| 283 "Translate the buffer from some FORMAT. | |
| 284 If the format is not specified, this function attempts to guess. | |
| 285 `buffer-file-format' is set to the format used, and any mode-functions | |
| 286 for the format are called." | |
| 287 (interactive | |
| 288 (list (format-read "Translate buffer from format (default: guess): "))) | |
| 289 (save-excursion | |
| 290 (goto-char (point-min)) | |
| 291 (format-decode format (buffer-size) t))) | |
| 292 | |
| 293 (defun format-decode-region (from to &optional format) | |
| 294 "Decode the region from some format. | |
| 295 Arg FORMAT is optional; if omitted the format will be determined by looking | |
| 296 for identifying regular expressions at the beginning of the region." | |
| 297 (interactive | |
| 298 (list (region-beginning) (region-end) | |
| 299 (format-read "Translate region from format (default: guess): "))) | |
| 300 (save-excursion | |
| 301 (goto-char from) | |
| 302 (format-decode format (- to from) nil))) | |
| 303 | |
| 304 (defun format-encode-buffer (&optional format) | |
| 305 "Translate the buffer into FORMAT. | |
| 306 FORMAT defaults to `buffer-file-format'. It is a symbol naming one of the | |
| 307 formats defined in `format-alist', or a list of such symbols." | |
| 308 (interactive | |
| 309 (list (format-read (format "Translate buffer to format (default %s): " | |
| 310 buffer-file-format)))) | |
| 311 (format-encode-region (point-min) (point-max) format)) | |
| 312 | |
| 444 | 313 (defun format-encode-region (start end &optional format) |
| 428 | 314 "Translate the region into some FORMAT. |
| 315 FORMAT defaults to `buffer-file-format', it is a symbol naming | |
| 316 one of the formats defined in `format-alist', or a list of such symbols." | |
| 317 (interactive | |
| 318 (list (region-beginning) (region-end) | |
| 319 (format-read (format "Translate region to format (default %s): " | |
| 320 buffer-file-format)))) | |
| 321 (if (null format) (setq format buffer-file-format)) | |
| 322 (if (symbolp format) (setq format (list format))) | |
| 323 (save-excursion | |
| 324 (goto-char end) | |
| 325 (let ( ; (cur-buf (current-buffer)) | |
| 326 (end (point-marker))) | |
| 327 (while format | |
| 328 (let* ((info (assq (car format) format-alist)) | |
| 329 (to-fn (nth 4 info)) | |
| 330 (modify (nth 5 info)) | |
| 331 ;; result | |
| 332 ) | |
| 333 (if to-fn | |
| 334 (if modify | |
| 444 | 335 (setq end (format-encode-run-method to-fn start end |
| 428 | 336 (current-buffer))) |
| 337 (format-insert-annotations | |
| 444 | 338 (funcall to-fn start end (current-buffer))))) |
| 428 | 339 (setq format (cdr format))))))) |
| 340 | |
| 341 (defun format-write-file (filename format) | |
| 342 "Write current buffer into a FILE using some FORMAT. | |
| 343 Makes buffer visit that file and sets the format as the default for future | |
| 344 saves. If the buffer is already visiting a file, you can specify a directory | |
| 345 name as FILE, to write a file of the same old name in that directory." | |
| 346 (interactive | |
| 347 ;; Same interactive spec as write-file, plus format question. | |
| 348 (let* ((file (if buffer-file-name | |
| 349 (read-file-name "Write file: " | |
| 350 nil nil nil nil) | |
| 351 (read-file-name "Write file: " | |
| 352 (cdr (assq 'default-directory | |
| 353 (buffer-local-variables))) | |
| 354 nil nil (buffer-name)))) | |
| 355 (fmt (format-read (format "Write file `%s' in format: " | |
| 356 (file-name-nondirectory file))))) | |
| 357 (list file fmt))) | |
| 358 (setq buffer-file-format format) | |
| 359 (write-file filename)) | |
| 360 | |
| 361 (defun format-find-file (filename format) | |
| 362 "Find the file FILE using data format FORMAT. | |
| 363 If FORMAT is nil then do not do any format conversion." | |
| 364 (interactive | |
| 365 ;; Same interactive spec as write-file, plus format question. | |
| 366 (let* ((file (read-file-name "Find file: ")) | |
| 367 (fmt (format-read (format "Read file `%s' in format: " | |
| 368 (file-name-nondirectory file))))) | |
| 369 (list file fmt))) | |
| 370 (let ((format-alist nil)) | |
| 371 (find-file filename)) | |
| 372 (if format | |
| 373 (format-decode-buffer format))) | |
| 374 | |
| 444 | 375 (defun format-insert-file (filename format &optional start end) |
| 428 | 376 "Insert the contents of file FILE using data format FORMAT. |
| 377 If FORMAT is nil then do not do any format conversion. | |
| 444 | 378 The optional third and fourth arguments START and END specify |
| 428 | 379 the part of the file to read. |
| 380 | |
| 381 The return value is like the value of `insert-file-contents': | |
| 382 a list (ABSOLUTE-FILE-NAME . SIZE)." | |
| 383 (interactive | |
| 384 ;; Same interactive spec as write-file, plus format question. | |
| 385 (let* ((file (read-file-name "Find file: ")) | |
| 386 (fmt (format-read (format "Read file `%s' in format: " | |
| 387 (file-name-nondirectory file))))) | |
| 388 (list file fmt))) | |
| 389 (let (value size) | |
| 390 (let ((format-alist nil)) | |
| 444 | 391 (setq value (insert-file-contents filename nil start end)) |
| 428 | 392 (setq size (nth 1 value))) |
| 393 (if format | |
| 394 (setq size (format-decode format size) | |
| 395 value (cons (car value) size))) | |
| 396 value)) | |
| 397 | |
| 398 (defun format-read (&optional prompt) | |
| 399 "Read and return the name of a format. | |
| 400 Return value is a list, like `buffer-file-format'; it may be nil. | |
| 401 Formats are defined in `format-alist'. Optional arg is the PROMPT to use." | |
| 402 (let* ((table (mapcar (lambda (x) (list (symbol-name (car x)))) | |
| 403 format-alist)) | |
| 404 (ans (completing-read (or prompt "Format: ") table nil t))) | |
| 405 (if (not (equal "" ans)) (list (intern ans))))) | |
| 406 | |
| 407 | |
| 408 ;;; | |
| 409 ;;; Below are some functions that may be useful in writing encoding and | |
| 410 ;;; decoding functions for use in format-alist. | |
| 411 ;;; | |
| 412 | |
| 444 | 413 (defun format-replace-strings (alist &optional reverse start end) |
| 428 | 414 "Do multiple replacements on the buffer. |
| 415 ALIST is a list of (from . to) pairs, which should be proper arguments to | |
| 416 `search-forward' and `replace-match' respectively. | |
| 417 Optional 2nd arg REVERSE, if non-nil, means the pairs are (to . from), so that | |
| 418 you can use the same list in both directions if it contains only literal | |
| 419 strings. | |
| 420 Optional args BEGIN and END specify a region of the buffer to operate on." | |
| 421 (save-excursion | |
| 422 (save-restriction | |
| 444 | 423 (or start (setq start (point-min))) |
| 428 | 424 (if end (narrow-to-region (point-min) end)) |
| 425 (while alist | |
| 426 (let ((from (if reverse (cdr (car alist)) (car (car alist)))) | |
| 427 (to (if reverse (car (cdr alist)) (cdr (car alist))))) | |
| 444 | 428 (goto-char start) |
| 428 | 429 (while (search-forward from nil t) |
| 430 (goto-char (match-beginning 0)) | |
| 431 (insert to) | |
| 432 (set-text-properties (- (point) (length to)) (point) | |
| 433 (text-properties-at (point))) | |
| 434 (delete-region (point) (+ (point) (- (match-end 0) | |
| 435 (match-beginning 0))))) | |
| 436 (setq alist (cdr alist))))))) | |
| 437 | |
| 438 (put 'face 'format-list-valued t) ; These text-properties take values | |
| 439 (put 'unknown 'format-list-valued t) ; that are lists, the elements of which | |
| 440 ; should be considered separately. | |
| 441 ; See format-deannotate-region and | |
| 442 ; format-annotate-region. | |
| 443 | |
| 444 ;;; | |
| 445 ;;; Decoding | |
| 446 ;;; | |
| 447 | |
| 448 (defun format-deannotate-region (from to translations next-fn) | |
| 449 "Translate annotations in the region into text properties. | |
| 450 This sets text properties between FROM to TO as directed by the | |
| 451 TRANSLATIONS and NEXT-FN arguments. | |
| 452 | |
| 453 NEXT-FN is a function that searches forward from point for an annotation. | |
| 454 It should return a list of 4 elements: \(BEGIN END NAME POSITIVE). BEGIN and | |
| 455 END are buffer positions bounding the annotation, NAME is the name searched | |
| 456 for in TRANSLATIONS, and POSITIVE should be non-nil if this annotation marks | |
| 457 the beginning of a region with some property, or nil if it ends the region. | |
| 458 NEXT-FN should return nil if there are no annotations after point. | |
| 459 | |
| 460 The basic format of the TRANSLATIONS argument is described in the | |
| 461 documentation for the `format-annotate-region' function. There are some | |
| 462 additional things to keep in mind for decoding, though: | |
| 463 | |
| 464 When an annotation is found, the TRANSLATIONS list is searched for a | |
| 465 text-property name and value that corresponds to that annotation. If the | |
| 466 text-property has several annotations associated with it, it will be used only | |
| 467 if the other annotations are also in effect at that point. The first match | |
| 468 found whose annotations are all present is used. | |
| 469 | |
| 470 The text property thus determined is set to the value over the region between | |
| 471 the opening and closing annotations. However, if the text-property name has a | |
| 472 non-nil `format-list-valued' property, then the value will be consed onto the | |
| 473 surrounding value of the property, rather than replacing that value. | |
| 474 | |
| 475 There are some special symbols that can be used in the \"property\" slot of | |
| 476 the TRANSLATIONS list: PARAMETER and FUNCTION \(spelled in uppercase). | |
| 477 Annotations listed under the pseudo-property PARAMETER are considered to be | |
| 478 arguments of the immediately surrounding annotation; the text between the | |
| 479 opening and closing parameter annotations is deleted from the buffer but saved | |
| 480 as a string. The surrounding annotation should be listed under the | |
| 481 pseudo-property FUNCTION. Instead of inserting a text-property for this | |
| 482 annotation, the function listed in the VALUE slot is called to make whatever | |
| 483 changes are appropriate. The function's first two arguments are the START and | |
| 484 END locations, and the rest of the arguments are any PARAMETERs found in that | |
| 485 region. | |
| 486 | |
| 487 Any annotations that are found by NEXT-FN but not defined by TRANSLATIONS | |
| 488 are saved as values of the `unknown' text-property \(which is list-valued). | |
| 489 The TRANSLATIONS list should usually contain an entry of the form | |
| 490 \(unknown \(nil format-annotate-value)) | |
| 491 to write these unknown annotations back into the file." | |
| 492 (save-excursion | |
| 493 (save-restriction | |
| 494 (narrow-to-region (point-min) to) | |
| 495 (goto-char from) | |
| 496 (let (next open-ans todo | |
| 497 ;; loc | |
| 498 unknown-ans) | |
| 499 (while (setq next (funcall next-fn)) | |
| 500 (let* ((loc (nth 0 next)) | |
| 501 (end (nth 1 next)) | |
| 502 (name (nth 2 next)) | |
| 503 (positive (nth 3 next)) | |
| 504 (found nil)) | |
| 505 | |
| 506 ;; Delete the annotation | |
| 507 (delete-region loc end) | |
| 508 (cond | |
| 509 ;; Positive annotations are stacked, remembering location | |
| 510 (positive (setq open-ans (cons `(,name ((,loc . nil))) open-ans))) | |
| 511 ;; It is a negative annotation: | |
| 512 ;; Close the top annotation & add its text property. | |
| 513 ;; If the file's nesting is messed up, the close might not match | |
| 514 ;; the top thing on the open-annotations stack. | |
| 515 ;; If no matching annotation is open, just ignore the close. | |
| 516 ((not (assoc name open-ans)) | |
| 517 (message "Extra closing annotation (%s) in file" name)) | |
| 518 ;; If one is open, but not on the top of the stack, close | |
| 519 ;; the things in between as well. Set `found' when the real | |
| 520 ;; one is closed. | |
| 521 (t | |
| 522 (while (not found) | |
| 523 (let* ((top (car open-ans)) ; first on stack: should match. | |
| 524 (top-name (car top)) ; text property name | |
| 525 (top-extents (nth 1 top)) ; property regions | |
| 526 (params (cdr (cdr top))) ; parameters | |
| 527 (aalist translations) | |
| 528 (matched nil)) | |
| 529 (if (equal name top-name) | |
| 530 (setq found t) | |
| 531 (message "Improper nesting in file.")) | |
| 532 ;; Look through property names in TRANSLATIONS | |
| 533 (while aalist | |
| 534 (let ((prop (car (car aalist))) | |
| 535 (alist (cdr (car aalist)))) | |
| 536 ;; And look through values for each property | |
| 537 (while alist | |
| 538 (let ((value (car (car alist))) | |
| 539 (ans (cdr (car alist)))) | |
| 540 (if (member top-name ans) | |
| 541 ;; This annotation is listed, but still have to | |
| 542 ;; check if multiple annotations are satisfied | |
|
5270
3acaa0fc09be
Use #'some, #'every, etc. for composing boolean operations on lists.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
543 (if (notevery (lambda (r) (assoc r open-ans)) |
|
3acaa0fc09be
Use #'some, #'every, etc. for composing boolean operations on lists.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4607
diff
changeset
|
544 ans) |
| 428 | 545 nil ; multiple ans not satisfied |
| 546 ;; If there are multiple annotations going | |
| 547 ;; into one text property, split up the other | |
| 548 ;; annotations so they apply individually to | |
| 549 ;; the other regions. | |
| 550 (setcdr (car top-extents) loc) | |
| 551 (let ((to-split ans) this-one extents) | |
| 552 (while to-split | |
| 553 (setq this-one | |
| 554 (assoc (car to-split) open-ans) | |
| 555 extents (nth 1 this-one)) | |
| 556 (if (not (eq this-one top)) | |
| 557 (setcar (cdr this-one) | |
| 558 (format-subtract-regions | |
| 559 extents top-extents))) | |
| 560 (setq to-split (cdr to-split)))) | |
| 561 ;; Set loop variables to nil so loop | |
| 562 ;; will exit. | |
| 563 (setq alist nil aalist nil matched t | |
| 564 ;; pop annotation off stack. | |
| 565 open-ans (cdr open-ans)) | |
| 566 (let ((extents top-extents) | |
| 567 (start (car (car top-extents))) | |
| 568 (loc (cdr (car top-extents)))) | |
| 569 (while extents | |
| 570 (cond | |
| 571 ;; Check for pseudo-properties | |
| 572 ((eq prop 'PARAMETER) | |
| 573 ;; A parameter of the top open ann: | |
| 574 ;; delete text and use as arg. | |
| 575 (if open-ans | |
| 576 ;; (If nothing open, discard). | |
| 577 (setq open-ans | |
| 578 (cons | |
| 579 (append (car open-ans) | |
| 580 (list | |
| 581 (buffer-substring | |
| 582 start loc))) | |
| 583 (cdr open-ans)))) | |
| 584 (delete-region start loc)) | |
| 585 ((eq prop 'FUNCTION) | |
| 586 ;; Not a property, but a function. | |
| 587 (let ((rtn | |
| 588 (apply value start loc params))) | |
| 589 (if rtn (setq todo (cons rtn todo))))) | |
| 590 (t | |
| 591 ;; Normal property/value pair | |
| 592 (setq todo | |
| 593 (cons (list start loc prop value) | |
| 594 todo)))) | |
| 595 (setq extents (cdr extents) | |
| 596 start (car (car extents)) | |
| 597 loc (cdr (car extents)))))))) | |
| 598 (setq alist (cdr alist)))) | |
| 599 (setq aalist (cdr aalist))) | |
| 600 (unless matched | |
| 601 ;; Didn't find any match for the annotation: | |
| 602 ;; Store as value of text-property `unknown'. | |
| 603 (setcdr (car top-extents) loc) | |
| 604 (let ((extents top-extents) | |
| 605 (start (car (car top-extents))) | |
| 606 (loc (cdr (car top-extents)))) | |
| 607 (while extents | |
| 608 (setq open-ans (cdr open-ans) | |
| 609 todo (cons (list start loc 'unknown top-name) | |
| 610 todo) | |
| 611 unknown-ans (cons name unknown-ans) | |
| 612 extents (cdr extents) | |
| 613 start (car (car extents)) | |
| 614 loc (cdr (car extents)))))))))))) | |
| 615 | |
| 616 ;; Once entire file has been scanned, add the properties. | |
| 617 (while todo | |
| 618 (let* ((item (car todo)) | |
| 619 (from (nth 0 item)) | |
| 620 (to (nth 1 item)) | |
| 621 (prop (nth 2 item)) | |
| 622 (val (nth 3 item))) | |
| 623 | |
| 624 (if (numberp val) ; add to ambient value if numeric | |
| 625 (format-property-increment-region from to prop val 0) | |
| 626 (put-text-property | |
| 627 from to prop | |
| 628 (cond ((get prop 'format-list-valued) ; value gets consed onto | |
| 629 ; list-valued properties | |
| 630 (let ((prev (get-text-property from prop))) | |
| 631 (cons val (if (listp prev) prev (list prev))))) | |
| 632 (t val))))) ; normally, just set to val. | |
| 633 (setq todo (cdr todo))) | |
| 634 | |
| 635 (if unknown-ans | |
| 636 (message "Unknown annotations: %s" unknown-ans)))))) | |
| 637 | |
| 638 (defun format-subtract-regions (minu subtra) | |
| 639 "Remove the regions in SUBTRAHEND from the regions in MINUEND. A region | |
| 640 is a dotted pair (from . to). Both parameters are lists of regions. Each | |
| 641 list must contain nonoverlapping, noncontiguous regions, in descending | |
| 642 order. The result is also nonoverlapping, noncontiguous, and in descending | |
| 643 order. The first element of MINUEND can have a cdr of nil, indicating that | |
| 644 the end of that region is not yet known." | |
| 645 (let* ((minuend (copy-alist minu)) | |
| 646 (subtrahend (copy-alist subtra)) | |
| 647 (m (car minuend)) | |
| 648 (s (car subtrahend)) | |
| 649 results) | |
| 650 (while (and minuend subtrahend) | |
| 651 (cond | |
| 652 ;; The minuend starts after the subtrahend ends; keep it. | |
| 653 ((> (car m) (cdr s)) | |
| 654 (setq results (cons m results) | |
| 655 minuend (cdr minuend) | |
| 656 m (car minuend))) | |
| 657 ;; The minuend extends beyond the end of the subtrahend. Chop it off. | |
| 658 ((or (null (cdr m)) (> (cdr m) (cdr s))) | |
| 659 (setq results (cons (cons (1+ (cdr s)) (cdr m)) results)) | |
| 660 (setcdr m (cdr s))) | |
| 661 ;; The subtrahend starts after the minuend ends; throw it away. | |
| 662 ((< (cdr m) (car s)) | |
| 663 (setq subtrahend (cdr subtrahend) s (car subtrahend))) | |
| 664 ;; The subtrahend extends beyond the end of the minuend. Chop it off. | |
| 665 (t ;(<= (cdr m) (cdr s))) | |
| 666 (if (>= (car m) (car s)) | |
| 667 (setq minuend (cdr minuend) m (car minuend)) | |
| 668 (setcdr m (1- (car s))) | |
| 669 (setq subtrahend (cdr subtrahend) s (car subtrahend)))))) | |
| 670 (nconc (nreverse results) minuend))) | |
| 671 | |
| 672 ;; This should probably go somewhere other than format.el. Then again, | |
| 673 ;; indent.el has alter-text-property. NOTE: We can also use | |
| 674 ;; next-single-property-change instead of text-property-not-all, but then | |
| 675 ;; we have to see if we passed TO. | |
| 676 (defun format-property-increment-region (from to prop delta default) | |
| 677 "Increment property PROP over the region between FROM and TO by the | |
| 678 amount DELTA (which may be negative). If property PROP is nil anywhere | |
| 679 in the region, it is treated as though it were DEFAULT." | |
| 680 (let ((cur from) val newval next) | |
| 681 (while cur | |
| 682 (setq val (get-text-property cur prop) | |
| 683 newval (+ (or val default) delta) | |
| 684 next (text-property-not-all cur to prop val)) | |
| 685 (put-text-property cur (or next to) prop newval) | |
| 686 (setq cur next)))) | |
| 687 | |
| 688 ;;; | |
| 689 ;;; Encoding | |
| 690 ;;; | |
| 691 | |
| 692 (defun format-insert-annotations (list &optional offset) | |
| 693 "Apply list of annotations to buffer as `write-region' would. | |
| 694 Inserts each element of the given LIST of buffer annotations at its | |
| 695 appropriate place. Use second arg OFFSET if the annotations' locations are | |
| 696 not relative to the beginning of the buffer: annotations will be inserted | |
| 697 at their location-OFFSET+1 \(ie, the offset is treated as the character number | |
| 698 of the first character in the buffer)." | |
| 699 (if (not offset) | |
| 700 (setq offset 0) | |
| 701 (setq offset (1- offset))) | |
| 702 (let ((l (reverse list))) | |
| 703 (while l | |
| 704 (goto-char (- (car (car l)) offset)) | |
| 705 (insert (cdr (car l))) | |
| 706 (setq l (cdr l))))) | |
| 707 | |
| 708 (defun format-annotate-value (old new) | |
| 709 "Return OLD and NEW as a \(close . open) annotation pair. | |
| 710 Useful as a default function for TRANSLATIONS alist when the value of the text | |
| 711 property is the name of the annotation that you want to use, as it is for the | |
| 712 `unknown' text property." | |
| 713 (cons (if old (list old)) | |
| 714 (if new (list new)))) | |
| 715 | |
| 716 (defun format-annotate-region (from to trans format-fn ignore) | |
| 717 "Generate annotations for text properties in the region. | |
| 718 Searches for changes between FROM and TO, and describes them with a list of | |
| 719 annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text | |
| 720 properties not to consider; any text properties that are neither ignored nor | |
| 721 listed in TRANSLATIONS are warned about. | |
| 722 If you actually want to modify the region, give the return value of this | |
| 723 function to `format-insert-annotations'. | |
| 724 | |
| 725 Format of the TRANSLATIONS argument: | |
| 726 | |
| 727 Each element is a list whose car is a PROPERTY, and the following | |
| 728 elements are VALUES of that property followed by the names of zero or more | |
| 729 ANNOTATIONS. Whenever the property takes on that value, the annotations | |
| 730 \(as formatted by FORMAT-FN) are inserted into the file. | |
| 731 When the property stops having that value, the matching negated annotation | |
| 732 will be inserted \(it may actually be closed earlier and reopened, if | |
| 733 necessary, to keep proper nesting). | |
| 734 | |
| 735 If the property's value is a list, then each element of the list is dealt with | |
| 736 separately. | |
| 737 | |
| 738 If a VALUE is numeric, then it is assumed that there is a single annotation | |
| 739 and each occurrence of it increments the value of the property by that number. | |
| 740 Thus, given the entry \(left-margin \(4 \"indent\")), if the left margin | |
| 741 changes from 4 to 12, two <indent> annotations will be generated. | |
| 742 | |
| 743 If the VALUE is nil, then instead of annotations, a function should be | |
| 744 specified. This function is used as a default: it is called for all | |
| 745 transitions not explicitly listed in the table. The function is called with | |
| 746 two arguments, the OLD and NEW values of the property. It should return | |
| 747 lists of annotations like `format-annotate-location' does. | |
| 748 | |
| 749 The same structure can be used in reverse for reading files." | |
| 750 (let ((all-ans nil) ; All annotations - becomes return value | |
| 751 (open-ans nil) ; Annotations not yet closed | |
| 752 (loc nil) ; Current location | |
| 753 (not-found nil)) ; Properties that couldn't be saved | |
| 754 (while (or (null loc) | |
| 755 (and (setq loc (next-property-change loc nil to)) | |
| 756 (< loc to))) | |
| 757 (or loc (setq loc from)) | |
| 758 (let* ((ans (format-annotate-location loc (= loc from) ignore trans)) | |
|
5365
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
759 (neg-ans (sort* (aref ans 0) '< |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
760 :key #'(lambda (object) |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
761 (or |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
762 (position object open-ans :test 'equal) |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
763 most-positive-fixnum)))) |
| 428 | 764 (pos-ans (aref ans 1)) |
| 765 (ignored (aref ans 2))) | |
| 766 (setq not-found (append ignored not-found) | |
| 767 ignore (append ignored ignore)) | |
| 768 ;; First do the negative (closing) annotations | |
| 769 (while neg-ans | |
| 770 ;; Check if it's missing. This can happen (eg, a numeric property | |
| 771 ;; going negative can generate closing annotations before there are | |
| 772 ;; any open). Warn user & ignore. | |
| 773 (if (not (member (car neg-ans) open-ans)) | |
| 774 (message "Can't close %s: not open." (car neg-ans)) | |
| 775 (while (not (equal (car neg-ans) (car open-ans))) | |
| 776 ;; To close anno. N, need to first close ans 1 to N-1, | |
| 777 ;; remembering to re-open them later. | |
| 778 (setq pos-ans (cons (car open-ans) pos-ans)) | |
| 779 (setq all-ans | |
| 780 (cons (cons loc (funcall format-fn (car open-ans) nil)) | |
| 781 all-ans)) | |
| 782 (setq open-ans (cdr open-ans))) | |
| 783 ;; Now remove the one we're really interested in from open list. | |
| 784 (setq open-ans (cdr open-ans)) | |
| 785 ;; And put the closing annotation here. | |
| 786 (setq all-ans | |
| 787 (cons (cons loc (funcall format-fn (car neg-ans) nil)) | |
| 788 all-ans))) | |
| 789 (setq neg-ans (cdr neg-ans))) | |
| 790 ;; Now deal with positive (opening) annotations | |
| 791 (let ( ; (p pos-ans) | |
| 792 ) | |
| 793 (while pos-ans | |
| 794 (setq open-ans (cons (car pos-ans) open-ans)) | |
| 795 (setq all-ans | |
| 796 (cons (cons loc (funcall format-fn (car pos-ans) t)) | |
| 797 all-ans)) | |
| 798 (setq pos-ans (cdr pos-ans)))))) | |
| 799 | |
| 800 ;; Close any annotations still open | |
| 801 (while open-ans | |
| 802 (setq all-ans | |
| 803 (cons (cons to (funcall format-fn (car open-ans) nil)) | |
| 804 all-ans)) | |
| 805 (setq open-ans (cdr open-ans))) | |
| 806 (if not-found | |
| 807 (message "These text properties could not be saved:\n %s" | |
| 808 not-found)) | |
| 809 (nreverse all-ans))) | |
| 810 | |
| 811 ;;; Internal functions for format-annotate-region. | |
| 812 | |
| 813 (defun format-annotate-location (loc all ignore trans) | |
| 814 "Return annotation(s) needed at LOCATION. | |
| 815 This includes any properties that change between LOC-1 and LOC. | |
| 816 If ALL is true, don't look at previous location, but generate annotations for | |
| 817 all non-nil properties. | |
| 818 Third argument IGNORE is a list of text-properties not to consider. | |
| 819 | |
| 820 Return value is a vector of 3 elements: | |
| 821 1. List of names of the annotations to close | |
| 822 2. List of the names of annotations to open. | |
| 823 3. List of properties that were ignored or couldn't be annotated." | |
| 824 (let* ((prev-loc (1- loc)) | |
| 825 (before-plist (if all nil (text-properties-at prev-loc))) | |
| 826 (after-plist (text-properties-at loc)) | |
| 827 p negatives positives prop props not-found) | |
| 828 ;; make list of all property names involved | |
| 829 (setq p before-plist) | |
| 830 (while p | |
| 831 (if (not (memq (car p) props)) | |
| 832 (setq props (cons (car p) props))) | |
| 833 (setq p (cdr (cdr p)))) | |
| 834 (setq p after-plist) | |
| 835 (while p | |
| 836 (if (not (memq (car p) props)) | |
| 837 (setq props (cons (car p) props))) | |
| 838 (setq p (cdr (cdr p)))) | |
| 839 | |
| 840 (while props | |
| 841 (setq prop (car props) | |
| 842 props (cdr props)) | |
| 843 (if (memq prop ignore) | |
| 844 nil ; If it's been ignored before, ignore it now. | |
| 845 (let ((before (if all nil (car (cdr (memq prop before-plist))))) | |
| 846 (after (car (cdr (memq prop after-plist))))) | |
| 847 (if (equal before after) | |
| 848 nil ; no change; ignore | |
| 849 (let ((result (format-annotate-single-property-change | |
| 850 prop before after trans))) | |
| 851 (if (not result) | |
| 852 (setq not-found (cons prop not-found)) | |
| 853 (setq negatives (nconc negatives (car result)) | |
| 854 positives (nconc positives (cdr result))))))))) | |
| 855 (vector negatives positives not-found))) | |
| 856 | |
| 857 (defun format-annotate-single-property-change (prop old new trans) | |
| 858 "Return annotations for PROPERTY changing from OLD to NEW. | |
| 859 These are searched for in the TRANSLATIONS alist. | |
| 860 If NEW does not appear in the list, but there is a default function, then that | |
| 861 function is called. | |
| 862 Annotations to open and to close are returned as a dotted pair." | |
| 863 (let ((prop-alist (cdr (assoc prop trans))) | |
| 864 ;; default | |
| 865 ) | |
| 866 (if (not prop-alist) | |
| 867 nil | |
| 868 ;; If either old or new is a list, have to treat both that way. | |
| 869 (if (or (consp old) (consp new)) | |
| 870 (let* ((old (if (listp old) old (list old))) | |
| 871 (new (if (listp new) new (list new))) | |
| 872 close open) | |
| 873 (while old | |
| 874 (setq close | |
| 875 (append (car (format-annotate-atomic-property-change | |
| 876 prop-alist (car old) nil)) | |
| 877 close) | |
| 878 old (cdr old))) | |
| 879 (while new | |
| 880 (setq open | |
| 881 (append (cdr (format-annotate-atomic-property-change | |
| 882 prop-alist nil (car new))) | |
| 883 open) | |
| 884 new (cdr new))) | |
|
5365
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
885 (cons |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
886 (set-difference close open :stable t) |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
887 (set-difference open close :stable t))) |
| 428 | 888 (format-annotate-atomic-property-change prop-alist old new))))) |
| 889 | |
| 890 (defun format-annotate-atomic-property-change (prop-alist old new) | |
| 891 "Internal function annotate a single property change. | |
| 892 PROP-ALIST is the relevant segment of a TRANSLATIONS list. | |
| 893 OLD and NEW are the values." | |
| 894 (let (num-ann) | |
| 895 ;; If old and new values are numbers, | |
| 896 ;; look for a number in PROP-ALIST. | |
| 897 (if (and (or (null old) (numberp old)) | |
| 898 (or (null new) (numberp new))) | |
| 899 (progn | |
| 900 (setq num-ann prop-alist) | |
| 901 (while (and num-ann (not (numberp (car (car num-ann))))) | |
| 902 (setq num-ann (cdr num-ann))))) | |
| 903 (if num-ann | |
| 904 ;; Numerical annotation - use difference | |
| 905 (progn | |
| 906 ;; If property is numeric, nil means 0 | |
| 907 (cond ((and (numberp old) (null new)) | |
| 908 (setq new 0)) | |
| 909 ((and (numberp new) (null old)) | |
| 910 (setq old 0))) | |
| 911 | |
| 912 (let* ((entry (car num-ann)) | |
| 913 (increment (car entry)) | |
| 914 (n (ceiling (/ (float (- new old)) (float increment)))) | |
| 915 (anno (car (cdr entry)))) | |
| 916 (if (> n 0) | |
| 917 (cons nil (make-list n anno)) | |
| 918 (cons (make-list (- n) anno) nil)))) | |
| 919 | |
| 920 ;; Standard annotation | |
| 921 (let ((close (and old (cdr (assoc old prop-alist)))) | |
| 922 (open (and new (cdr (assoc new prop-alist))))) | |
| 923 (if (or close open) | |
|
5365
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
924 (cons |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
925 (set-difference close open :stable t) |
|
dbae25a8949d
Remove redundant functions, format.el, use functions from cl*.el instead.
Aidan Kehoe <kehoea@parhasard.net>
parents:
5270
diff
changeset
|
926 (set-difference open close :stable t)) |
| 428 | 927 ;; Call "Default" function, if any |
| 928 (let ((default (assq nil prop-alist))) | |
| 929 (if default | |
| 930 (funcall (car (cdr default)) old new)))))))) | |
| 931 | |
| 932 ;;; format.el ends here |
