Mercurial > hg > xemacs-beta
view lisp/format.el @ 5353:38e24b8be4ea
Improve the lexical scoping in #'block, #'return-from.
lisp/ChangeLog addition:
2011-02-07 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el:
* bytecomp.el (byte-compile-initial-macro-environment):
Shadow `block', `return-from' here, we implement them differently
when byte-compiling.
* bytecomp.el (byte-compile-active-blocks): New.
* bytecomp.el (byte-compile-block-1): New.
* bytecomp.el (byte-compile-return-from-1): New.
* bytecomp.el (return-from-1): New.
* bytecomp.el (block-1): New.
These are two aliases that exist to have their own associated
byte-compile functions, which functions implement `block' and
`return-from'.
* cl-extra.el (cl-macroexpand-all):
Fix a bug here when macros in the environment have been compiled.
* cl-macs.el (block):
* cl-macs.el (return):
* cl-macs.el (return-from):
Be more careful about lexical scope in these macros.
* cl.el:
* cl.el ('cl-block-wrapper): Removed.
* cl.el ('cl-block-throw): Removed.
These aren't needed in code generated by this XEmacs. They
shouldn't be needed in code generated by XEmacs 21.4, but if it
turns out the packages do need them, we can put them back.
2011-01-30 Mike Sperber <mike@xemacs.org>
* font-lock.el (font-lock-fontify-pending-extents): Don't fail if
`font-lock-mode' is unset, which can happen in the middle of
`revert-buffer'.
2011-01-23 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (delete):
* cl-macs.el (delq):
* cl-macs.el (remove):
* cl-macs.el (remq):
Don't use the compiler macro if these functions were given the
wrong number of arguments, as happens in lisp-tests.el.
* cl-seq.el (remove, remq): Removed.
I added these to subr.el, and forgot to remove them from here.
2011-01-22 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el (byte-compile-setq, byte-compile-set):
Remove kludge allowing keywords' values to be set, all the code
that does that is gone.
* cl-compat.el (elt-satisfies-test-p):
* faces.el (set-face-parent):
* faces.el (face-doc-string):
* gtk-font-menu.el:
* gtk-font-menu.el (gtk-reset-device-font-menus):
* msw-font-menu.el:
* msw-font-menu.el (mswindows-reset-device-font-menus):
* package-get.el (package-get-installedp):
* select.el (select-convert-from-image-data):
* sound.el:
* sound.el (load-sound-file):
* x-font-menu.el (x-reset-device-font-menus-core):
Don't quote keywords, they're self-quoting, and the
win from backward-compatibility is sufficiently small now that the
style problem overrides it.
2011-01-22 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (block, return-from): Require that NAME be a symbol
in these macros, as always documented in the #'block docstring and
as required by Common Lisp.
* descr-text.el (unidata-initialize-unihan-database):
Correct the use of non-symbols in #'block and #'return-from in
this function.
2011-01-15 Aidan Kehoe <kehoea@parhasard.net>
* cl-extra.el (concatenate): Accept more complicated TYPEs in this
function, handing the sequences over to #'coerce if we don't
understand them here.
* cl-macs.el (inline): Don't proclaim #'concatenate as inline, its
compiler macro is more useful than doing that.
2011-01-11 Aidan Kehoe <kehoea@parhasard.net>
* subr.el (delete, delq, remove, remq): Move #'remove, #'remq
here, they don't belong in cl-seq.el; move #'delete, #'delq here
from fns.c, implement them in terms of #'delete*, allowing support
for sequences generally.
* update-elc.el (do-autoload-commands): Use #'delete*, not #'delq
here, now the latter's no longer dumped.
* cl-macs.el (delete, delq): Add compiler macros transforming
#'delete and #'delq to #'delete* calls.
2011-01-10 Aidan Kehoe <kehoea@parhasard.net>
* dialog.el (make-dialog-box): Correct a misplaced parenthesis
here, thank you Mats Lidell in 87zkr9gqrh.fsf@mail.contactor.se !
2011-01-02 Aidan Kehoe <kehoea@parhasard.net>
* dialog.el (make-dialog-box):
* list-mode.el (display-completion-list):
These functions used to use cl-parsing-keywords; change them to
use defun* instead, fixing the build. (Not sure what led to me
not including this change in d1b17a33450b!)
2011-01-02 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (define-star-compiler-macros):
Make sure the form has ITEM and LIST specified before attempting
to change to calls with explicit tests; necessary for some tests
in lisp-tests.el to compile correctly.
(stable-union, stable-intersection): Add compiler macros for these
functions, in the same way we do for most of the other functions
in cl-seq.el.
2011-01-01 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (dolist, dotimes, do-symbols, macrolet)
(symbol-macrolet):
Define these macros with defmacro* instead of parsing the argument
list by hand, for the sake of style and readability; use backquote
where appropriate, instead of calling #'list and and friends, for
the same reason.
2010-12-30 Aidan Kehoe <kehoea@parhasard.net>
* x-misc.el (device-x-display):
Provide this function, documented in the Lispref for years, but
not existing previously. Thank you Julian Bradfield, thank you
Jeff Mincy.
2010-12-30 Aidan Kehoe <kehoea@parhasard.net>
* cl-seq.el:
Move the heavy lifting from this file to C. Dump the
cl-parsing-keywords macro, but don't use defun* for the functions
we define that do take keywords, dynamic scope lossage makes that
not practical.
* subr.el (sort, fillarray): Move these aliases here.
(map-plist): #'nsublis is now built-in, but at this point #'eql
isn't necessarily available as a test; use #'eq.
* obsolete.el (cl-delete-duplicates): Make this available for old
compiler macros and old code.
(memql): Document that this is equivalent to #'member*, and worse.
* cl.el (adjoin, subst): Removed. These are in C.
2010-12-30 Aidan Kehoe <kehoea@parhasard.net>
* simple.el (assoc-ignore-case): Remove a duplicate definition of
this function (it's already in subr.el).
* iso8859-1.el (char-width):
On non-Mule, make this function equivalent to that produced by
(constantly 1), but preserve its docstring.
* subr.el (subst-char-in-string): Define this in terms of
#'substitute, #'nsubstitute.
(string-width): Define this using #'reduce and #'char-width.
(char-width): Give this a simpler definition, it makes far more
sense to check for mule at load time and redefine, as we do in
iso8859-1.el.
(store-substring): Implement this in terms of #'replace, now
#'replace is cheap.
2010-12-30 Aidan Kehoe <kehoea@parhasard.net>
* update-elc.el (lisp-files-needed-for-byte-compilation)
(lisp-files-needing-early-byte-compilation):
cl-macs belongs in the former, not the latter, it is as
fundamental as bytecomp.el.
2010-12-30 Aidan Kehoe <kehoea@parhasard.net>
* cl.el:
Provde the Common Lisp program-error, type-error as error
symbols. This doesn't nearly go far enough for anyone using the
Common Lisp errors.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (delete-duplicates):
If the form has an incorrect number of arguments, don't attempt a
compiler macroexpansion.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (cl-safe-expr-p):
Forms that start with the symbol lambda are also safe.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (= < > <= >=):
For these functions' compiler macros, the optimisation is safe
even if the first and the last arguments have side effects, since
they're only used the once.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (inline-side-effect-free-compiler-macros):
Unroll a loop here at macro-expansion time, so these compiler
macros are compiled. Use #'eql instead of #'eq in a couple of
places for better style.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* cl-extra.el (notany, notevery): Avoid some dynamic scope
stupidity with local variable names in these functions, when they
weren't prefixed with cl-; go into some more detail in the doc
strings.
2010-12-29 Aidan Kehoe <kehoea@parhasard.net>
* byte-optimize.el (side-effect-free-fns): #'remove, #'remq are
free of side-effects.
(side-effect-and-error-free-fns):
Drop dot, dot-marker from the list.
2010-11-17 Aidan Kehoe <kehoea@parhasard.net>
* cl-extra.el (coerce):
In the argument list, name the first argument OBJECT, not X; the
former name was always used in the doc string and is clearer.
Handle vector type specifications which include the length of the
target sequence, error if there's a mismatch.
* cl-macs.el (cl-make-type-test): Handle type specifications
starting with the symbol 'eql.
2010-11-14 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (eql): Don't remove the byte-compile property of this
symbol. That was necessary to override a bug in bytecomp.el where
#'eql was confused with #'eq, which bug we no longer have.
If neither expression is constant, don't attempt to handle the
expression in this compiler macro, leave it to byte-compile-eql,
which produces better code anyway.
* bytecomp.el (eq): #'eql is not the function associated with the
byte-eq byte code.
(byte-compile-eql): Add an explicit compile method for this
function, for cases where the cl-macs compiler macro hasn't
reduced it to #'eq or #'equal.
2010-10-25 Aidan Kehoe <kehoea@parhasard.net>
Add compiler macros and compilation sanity-checking for various
functions that take keywords.
* byte-optimize.el (side-effect-free-fns): #'symbol-value is
side-effect free and not error free.
* bytecomp.el (byte-compile-normal-call): Check keyword argument
lists for sanity; store information about the positions where
keyword arguments start using the new byte-compile-keyword-start
property.
* cl-macs.el (cl-const-expr-val): Take a new optional argument,
cl-not-constant, defaulting to nil, in this function; return it if
the expression is not constant.
(cl-non-fixnum-number-p): Make this into a separate function, we
want to pass it to #'every.
(eql): Use it.
(define-star-compiler-macros): Use the same code to generate the
member*, assoc* and rassoc* compiler macros; special-case some
code in #'add-to-list in subr.el.
(remove, remq): Add compiler macros for these two functions, in
preparation for #'remove being in C.
(define-foo-if-compiler-macros): Transform (remove-if-not ...) calls to
(remove ... :if-not) at compile time, which will be a real win
once the latter is in C.
(define-substitute-if-compiler-macros)
(define-subst-if-compiler-macros): Similarly for these functions.
(delete-duplicates): Change this compiler macro to use
#'plists-equal; if we don't have information about the type of
SEQUENCE at compile time, don't bother attempting to inline the
call, the function will be in C soon enough.
(equalp): Remove an old commented-out compiler macro for this, if
we want to see it it's in version control.
(subst-char-in-string): Transform this to a call to nsubstitute or
nsubstitute, if that is appropriate.
* cl.el (ldiff): Don't call setf here, this makes for a load-time
dependency problem in cl-macs.el
2010-06-14 Stephen J. Turnbull <stephen@xemacs.org>
* term/vt100.el:
Refer to XEmacs, not GNU Emacs, in permissions.
* term/bg-mouse.el:
* term/sup-mouse.el:
Put copyright notice in canonical "Copyright DATE AUTHOR" form.
Refer to XEmacs, not GNU Emacs, in permissions.
* site-load.el:
Add permission boilerplate.
* mule/canna-leim.el:
* alist.el:
Refer to XEmacs, not APEL/this program, in permissions.
* mule/canna-leim.el:
Remove my copyright, I've assigned it to the FSF.
2010-06-14 Stephen J. Turnbull <stephen@xemacs.org>
* gtk.el:
* gtk-widget-accessors.el:
* gtk-package.el:
* gtk-marshal.el:
* gtk-compose.el:
* gnome.el:
Add copyright notice based on internal evidence.
2010-06-14 Stephen J. Turnbull <stephen@xemacs.org>
* easymenu.el: Add reference to COPYING to permission notice.
* gutter.el:
* gutter-items.el:
* menubar-items.el:
Fix typo "Xmacs" in permissions notice.
2010-06-14 Stephen J. Turnbull <stephen@xemacs.org>
* auto-save.el:
* font.el:
* fontconfig.el:
* mule/kinsoku.el:
Add "part of XEmacs" text to permission notice.
2010-10-14 Aidan Kehoe <kehoea@parhasard.net>
* byte-optimize.el (side-effect-free-fns):
* cl-macs.el (remf, getf):
* cl-extra.el (tailp, cl-set-getf, cl-do-remf):
* cl.el (ldiff, endp):
Tighten up Common Lisp compatibility for #'ldiff, #'endp, #'tailp;
add circularity checking for the first two.
#'cl-set-getf and #'cl-do-remf were Lisp implementations of
#'plist-put and #'plist-remprop; change the names to aliases,
changes the macros that use them to using #'plist-put and
#'plist-remprop directly.
2010-10-12 Aidan Kehoe <kehoea@parhasard.net>
* abbrev.el (fundamental-mode-abbrev-table, global-abbrev-table):
Create both these abbrev tables using the usual
#'define-abbrev-table calls, rather than attempting to
special-case them.
* cl-extra.el: Force cl-macs to be loaded here, if cl-extra.el is
being loaded interpreted. Previously other, later files would
redundantly call (load "cl-macs") when interpreted, it's more
reasonable to do it here, once.
* cmdloop.el (read-quoted-char-radix): Use defcustom here, we
don't have any dump-order dependencies that would prevent that.
* custom.el (eval-when-compile): Don't load cl-macs when
interpreted or when byte-compiling, rely on cl-extra.el in the
former case and the appropriate entry in bytecomp-load-hook in the
latter. Get rid of custom-declare-variable-list, we have no
dump-time dependencies that would require it.
* faces.el (eval-when-compile): Don't load cl-macs when
interpreted or when byte-compiling.
* packages.el: Remove some inaccurate comments.
* post-gc.el (cleanup-simple-finalizers): Use #'delete-if-not
here, now the order of preloaded-file-list has been changed to
make it available.
* subr.el (custom-declare-variable-list): Remove. No need for it.
Also remove a stub define-abbrev-table from this file, given the
current order of preloaded-file-list there's no need for it.
2010-10-10 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el (byte-compile-constp) Forms quoted with FUNCTION are
also constant.
(byte-compile-initial-macro-environment): In #'the, if FORM is
constant and does not match TYPE, warn at byte-compile time.
2010-10-10 Aidan Kehoe <kehoea@parhasard.net>
* backquote.el (bq-vector-contents, bq-list*): Remove; the former
is equivalent to (append VECTOR nil), the latter to (list* ...).
(bq-process-2): Use (append VECTOR nil) instead of using
#'bq-vector-contents to convert to a list.
(bq-process-1): Now we use list* instead of bq-list
* subr.el (list*): Moved from cl.el, since it is now required to
be available the first time a backquoted form is encountered.
* cl.el (list*): Move to subr.el.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* test-harness.el (Check-Message):
Add an omitted comma here, thank you the buildbot.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* hash-table.el (hash-table-key-list, hash-table-value-list)
(hash-table-key-value-alist, hash-table-key-value-plist):
Remove some useless #'nreverse calls in these files; our hash
tables have no order, it's not helpful to pretend they do.
* behavior.el (read-behavior):
Do the same in this file, in some code evidently copied from
hash-table.el.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* info.el (Info-insert-dir):
* format.el (format-deannotate-region):
* files.el (cd, save-buffers-kill-emacs):
Use #'some, #'every and related functions for applying boolean
operations to lists, instead of rolling our own ones that cons and
don't short-circuit.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el (byte-compile-initial-macro-environment):
* cl-macs.el (the):
Rephrase the docstring, make its implementation when compiling
files a little nicer.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* descr-text.el (unidata-initialize-unicodedata-database)
(unidata-initialize-unihan-database, describe-char-unicode-data)
(describe-char-unicode-data):
Wrap calls to the database functions with (with-fboundp ...),
avoiding byte compile warnings on builds without support for the
database functions.
(describe-char): (reduce #'max ...), not (apply #'max ...), no
need to cons needlessly.
(describe-char): Remove a redundant lambda wrapping
#'extent-properties.
(describe-char-unicode-data): Call #'nsubst when replacing "" with
nil in the result of #'split-string, instead of consing inside
mapcar.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* x-faces.el (x-available-font-sizes):
* specifier.el (let-specifier):
* package-ui.el (pui-add-required-packages):
* msw-faces.el (mswindows-available-font-sizes):
* modeline.el (modeline-minor-mode-menu):
* minibuf.el (minibuf-directory-files):
Replace the O2N (delq nil (mapcar (lambda (W) (and X Y)) Z)) with
the ON (mapcan (lambda (W) (and X (list Y))) Z) in these files.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (= < > <= >=):
When these functions are handed more than two arguments, and those
arguments have no side effects, transform to a series of two
argument calls, avoiding funcall in the byte-compiled code.
* mule/mule-cmds.el (finish-set-language-environment):
Take advantage of this change in a function called 256 times at
startup.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* bytecomp.el (byte-compile-function-form, byte-compile-quote)
(byte-compile-quote-form):
Warn at compile time, and error at runtime, if a (quote ...) or a
(function ...) form attempts to quote more than one object.
2010-09-16 Aidan Kehoe <kehoea@parhasard.net>
* byte-optimize.el (byte-optimize-apply): Transform (apply 'nconc
(mapcar ...)) to (mapcan ...); warn about use of the first idiom.
* update-elc.el (do-autoload-commands):
* packages.el (packages-find-package-library-path):
* frame.el (frame-list):
* extents.el (extent-descendants):
* etags.el (buffer-tag-table-files):
* dumped-lisp.el (preloaded-file-list):
* device.el (device-list):
* bytecomp-runtime.el (proclaim-inline, proclaim-notinline)
Use #'mapcan, not (apply #'nconc (mapcar ...) in all these files.
* bytecomp-runtime.el (eval-when-compile, eval-and-compile):
In passing, mention that these macros also evaluate the body when
interpreted.
tests/ChangeLog addition:
2011-02-07 Aidan Kehoe <kehoea@parhasard.net>
* automated/lisp-tests.el:
Test lexical scope for `block', `return-from'; add a
Known-Bug-Expect-Failure for a contorted example that fails when
byte-compiled.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Mon, 07 Feb 2011 12:01:24 +0000 |
parents | 3acaa0fc09be |
children | dbae25a8949d 308d34e9f07d |
line wrap: on
line source
;;; format.el --- read and save files in multiple formats ;; Copyright (c) 1994, 1995, 1997 Free Software Foundation ;; Author: Boris Goldowsky <boris@gnu.ai.mit.edu> ;; Keywords: extensions, dumped ;; This file is part of XEmacs. ;; XEmacs is free software; you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; XEmacs is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with XEmacs; see the file COPYING. If not, write to the Free ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ;; 02111-1307, USA. ;;; Synched up with: Emacs 20.2. ;;; Commentary: ;; This file is dumped with XEmacs. ;; This file defines a unified mechanism for saving & loading files stored ;; in different formats. `format-alist' contains information that directs ;; Emacs to call an encoding or decoding function when reading or writing ;; files that match certain conditions. ;; ;; When a file is visited, its format is determined by matching the ;; beginning of the file against regular expressions stored in ;; `format-alist'. If this fails, you can manually translate the buffer ;; using `format-decode-buffer'. In either case, the formats used are ;; listed in the variable `buffer-file-format', and become the default ;; format for saving the buffer. To save a buffer in a different format, ;; change this variable, or use `format-write-file'. ;; ;; Auto-save files are normally created in the same format as the visited ;; file, but the variable `auto-save-file-format' can be set to a ;; particularly fast or otherwise preferred format to be used for ;; auto-saving (or nil to do no encoding on auto-save files, but then you ;; risk losing any text-properties in the buffer). ;; ;; You can manually translate a buffer into or out of a particular format ;; with the functions `format-encode-buffer' and `format-decode-buffer'. ;; To translate just the region use the functions `format-encode-region' ;; and `format-decode-region'. ;; ;; You can define a new format by writing the encoding and decoding ;; functions, and adding an entry to `format-alist'. See enriched.el for ;; an example of how to implement a file format. There are various ;; functions defined in this file that may be useful for writing the ;; encoding and decoding functions: ;; * `format-annotate-region' and `format-deannotate-region' allow a ;; single alist of information to be used for encoding and decoding. ;; The alist defines a correspondence between strings in the file ;; ("annotations") and text-properties in the buffer. ;; * `format-replace-strings' is similarly useful for doing simple ;; string->string translations in a reversible manner. ;;; Code: (put 'buffer-file-format 'permanent-local t) (defvar format-alist '( ; (image/jpeg "JPEG image" "\377\330\377\340\000\020JFIF" ; image-decode-jpeg nil t image-mode) ; (image/gif "GIF image" "GIF8[79]" ; image-decode-gif nil t image-mode) ; (image/png "Portable Network Graphics" "\211PNG" ; image-decode-png nil t image-mode) ; (image/x-xpm "XPM image" "/\\* XPM \\*/" ; image-decode-xpm nil t image-mode) ; ;; TIFF files have lousy magic ; (image/tiff "TIFF image" "II\\*\000" ; image-decode-tiff nil t image-mode) ;; TIFF 6.0 big-endian ; (image/tiff "TIFF image" "MM\000\\*" ; image-decode-tiff nil t image-mode) ;; TIFF 6.0 little-endian (text/enriched "Extended MIME text/enriched format." "Content-[Tt]ype:[ \t]*text/enriched" enriched-decode enriched-encode t enriched-mode) (text/richtext "Extended MIME obsolete text/richtext format." "Content-[Tt]ype:[ \t]*text/richtext" richtext-decode richtext-encode t enriched-mode) (plain "ISO 8859-1 standard format, no text properties." ;; Plain only exists so that there is an obvious neutral choice in ;; the completion list. nil nil nil nil nil) ;; (ibm "IBM Code Page 850 (DOS)" ;; "1\\(^\\)" ;; "recode ibm-pc:latin1" "recode latin1:ibm-pc" t nil) ;; (mac "Apple Macintosh" ;; "1\\(^\\)" ;; "recode mac:latin1" "recode latin1:mac" t nil) ;; (hp "HP Roman8" ;; "1\\(^\\)" ;; "recode roman8:latin1" "recode latin1:roman8" t nil) ;; (TeX "TeX (encoding)" ;; "1\\(^\\)" ;; iso-tex2iso iso-iso2tex t nil) ;; (gtex "German TeX (encoding)" ;; "1\\(^\\)" ;; iso-gtex2iso iso-iso2gtex t nil) ;; (html "HTML (encoding)" ;; "1\\(^\\)" ;; "recode html:latin1" "recode latin1:html" t nil) ;; (rot13 "rot13" ;; "1\\(^\\)" ;; "tr a-mn-z n-za-m" "tr a-mn-z n-za-m" t nil) ;; (duden "Duden Ersatzdarstellung" ;; "1\\(^\\)" ;; "diac" iso-iso2duden t nil) ;; (de646 "German ASCII (ISO 646)" ;; "1\\(^\\)" ;; "recode iso646-ge:latin1" "recode latin1:iso646-ge" t nil) ;; (denet "net German" ;; "1\\(^\\)" ;; iso-german iso-cvt-read-only t nil) ;; (esnet "net Spanish" ;; "1\\(^\\)" ;; iso-spanish iso-cvt-read-only t nil) ) "List of information about understood file formats. Elements are of the form \(NAME DOC-STR REGEXP FROM-FN TO-FN MODIFY MODE-FN). NAME is a symbol, which is stored in `buffer-file-format'. DOC-STR should be a single line providing more information about the format. It is currently unused, but in the future will be shown to the user if they ask for more information. REGEXP is a regular expression to match against the beginning of the file; it should match only files in that format. FROM-FN is called to decode files in that format; it gets two args, BEGIN and END, and can make any modifications it likes, returning the new end. It must make sure that the beginning of the file no longer matches REGEXP, or else it will get called again. Alternatively, FROM-FN can be a string, which specifies a shell command (including options) to be used as a filter to perform the conversion. TO-FN is called to encode a region into that format; it is passed three arguments: BEGIN, END, and BUFFER. BUFFER is the original buffer that the data being written came from, which the function could use, for example, to find the values of local variables. TO-FN should either return a list of annotations like `write-region-annotate-functions', or modify the region and return the new end. Alternatively, TO-FN can be a string, which specifies a shell command (including options) to be used as a filter to perform the conversion. MODIFY, if non-nil, means the TO-FN wants to modify the region. If nil, TO-FN will not make any changes but will instead return a list of annotations. MODE-FN, if specified, is called when visiting a file with that format.") ;;; Basic Functions (called from Lisp) (defun format-encode-run-method (method from to &optional buffer) "Translate using function or shell script METHOD the text from FROM to TO. If METHOD is a string, it is a shell command; otherwise, it should be a Lisp function. BUFFER should be the buffer that the output originally came from." (if (stringp method) (save-current-buffer (set-buffer buffer) (with-output-to-temp-buffer "*Format Errors*" (shell-command-on-region from to method t nil)) (point)) (funcall method from to buffer))) (defun format-decode-run-method (method from to &optional buffer) "Decode using function or shell script METHOD the text from FROM to TO. If METHOD is a string, it is a shell command; otherwise, it should be a Lisp function." (if (stringp method) (progn (with-output-to-temp-buffer "*Format Errors*" (shell-command-on-region from to method t nil)) (point)) (funcall method from to))) (defun format-annotate-function (format from to orig-buf) "Return annotations for writing region as FORMAT. FORMAT is a symbol naming one of the formats defined in `format-alist', it must be a single symbol, not a list like `buffer-file-format'. FROM and TO delimit the region to be operated on in the current buffer. ORIG-BUF is the original buffer that the data came from. This function works like a function on `write-region-annotate-functions': it either returns a list of annotations, or returns with a different buffer current, which contains the modified text to write. For most purposes, consider using `format-encode-region' instead." ;; This function is called by write-region (actually build-annotations) ;; for each element of buffer-file-format. (let* ((info (assq format format-alist)) (to-fn (nth 4 info)) (modify (nth 5 info))) (if to-fn (if modify ;; To-function wants to modify region. Copy to safe place. (let ((copy-buf (get-buffer-create " *Format Temp*"))) (copy-to-buffer copy-buf from to) (set-buffer copy-buf) (format-insert-annotations write-region-annotations-so-far from) (format-encode-run-method to-fn (point-min) (point-max) orig-buf) nil) ;; Otherwise just call function, it will return annotations. (funcall to-fn from to orig-buf))))) (defun format-decode (format length &optional visit-flag) "Decode text from any known FORMAT. FORMAT is a symbol appearing in `format-alist' or a list of such symbols, or nil, in which case this function tries to guess the format of the data by matching against the regular expressions in `format-alist'. After a match is found and the region decoded, the alist is searched again from the beginning for another match. Second arg LENGTH is the number of characters following point to operate on. If optional third arg VISIT-FLAG is true, set `buffer-file-format' to the list of formats used, and call any mode functions defined for those formats. Returns the new length of the decoded region. For most purposes, consider using `format-decode-region' instead. This function is called by insert-file-contents whenever a file is read." (let ((mod (buffer-modified-p)) (begin (point)) (end (+ (point) length))) (if (null format) ;; Figure out which format it is in, remember list in `format'. (let ((try format-alist)) (while try (let* ((f (car try)) (regexp (nth 2 f)) (p (point))) (if (and regexp (looking-at regexp) (< (match-end 0) (+ begin length))) (progn (setq format (cons (car f) format)) ;; Decode it (if (nth 3 f) (setq end (format-decode-run-method (nth 3 f) begin end))) ;; Call visit function if required (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) ;; Safeguard against either of the functions changing pt. (goto-char p) ;; Rewind list to look for another format (setq try format-alist)) (setq try (cdr try)))))) ;; Deal with given format(s) (or (listp format) (setq format (list format))) (let ((do format) f) (while do (or (setq f (assq (car do) format-alist)) (error "Unknown format" (car do))) ;; Decode: (if (nth 3 f) (setq end (format-decode-run-method (nth 3 f) begin end))) ;; Call visit function if required (if (and visit-flag (nth 6 f)) (funcall (nth 6 f) 1)) (setq do (cdr do))))) (if visit-flag (setq buffer-file-format format)) (set-buffer-modified-p mod) ;; Return new length of region (- end begin))) ;;; ;;; Interactive functions & entry points ;;; (defun format-decode-buffer (&optional format) "Translate the buffer from some FORMAT. If the format is not specified, this function attempts to guess. `buffer-file-format' is set to the format used, and any mode-functions for the format are called." (interactive (list (format-read "Translate buffer from format (default: guess): "))) (save-excursion (goto-char (point-min)) (format-decode format (buffer-size) t))) (defun format-decode-region (from to &optional format) "Decode the region from some format. Arg FORMAT is optional; if omitted the format will be determined by looking for identifying regular expressions at the beginning of the region." (interactive (list (region-beginning) (region-end) (format-read "Translate region from format (default: guess): "))) (save-excursion (goto-char from) (format-decode format (- to from) nil))) (defun format-encode-buffer (&optional format) "Translate the buffer into FORMAT. FORMAT defaults to `buffer-file-format'. It is a symbol naming one of the formats defined in `format-alist', or a list of such symbols." (interactive (list (format-read (format "Translate buffer to format (default %s): " buffer-file-format)))) (format-encode-region (point-min) (point-max) format)) (defun format-encode-region (start end &optional format) "Translate the region into some FORMAT. FORMAT defaults to `buffer-file-format', it is a symbol naming one of the formats defined in `format-alist', or a list of such symbols." (interactive (list (region-beginning) (region-end) (format-read (format "Translate region to format (default %s): " buffer-file-format)))) (if (null format) (setq format buffer-file-format)) (if (symbolp format) (setq format (list format))) (save-excursion (goto-char end) (let ( ; (cur-buf (current-buffer)) (end (point-marker))) (while format (let* ((info (assq (car format) format-alist)) (to-fn (nth 4 info)) (modify (nth 5 info)) ;; result ) (if to-fn (if modify (setq end (format-encode-run-method to-fn start end (current-buffer))) (format-insert-annotations (funcall to-fn start end (current-buffer))))) (setq format (cdr format))))))) (defun format-write-file (filename format) "Write current buffer into a FILE using some FORMAT. Makes buffer visit that file and sets the format as the default for future saves. If the buffer is already visiting a file, you can specify a directory name as FILE, to write a file of the same old name in that directory." (interactive ;; Same interactive spec as write-file, plus format question. (let* ((file (if buffer-file-name (read-file-name "Write file: " nil nil nil nil) (read-file-name "Write file: " (cdr (assq 'default-directory (buffer-local-variables))) nil nil (buffer-name)))) (fmt (format-read (format "Write file `%s' in format: " (file-name-nondirectory file))))) (list file fmt))) (setq buffer-file-format format) (write-file filename)) (defun format-find-file (filename format) "Find the file FILE using data format FORMAT. If FORMAT is nil then do not do any format conversion." (interactive ;; Same interactive spec as write-file, plus format question. (let* ((file (read-file-name "Find file: ")) (fmt (format-read (format "Read file `%s' in format: " (file-name-nondirectory file))))) (list file fmt))) (let ((format-alist nil)) (find-file filename)) (if format (format-decode-buffer format))) (defun format-insert-file (filename format &optional start end) "Insert the contents of file FILE using data format FORMAT. If FORMAT is nil then do not do any format conversion. The optional third and fourth arguments START and END specify the part of the file to read. The return value is like the value of `insert-file-contents': a list (ABSOLUTE-FILE-NAME . SIZE)." (interactive ;; Same interactive spec as write-file, plus format question. (let* ((file (read-file-name "Find file: ")) (fmt (format-read (format "Read file `%s' in format: " (file-name-nondirectory file))))) (list file fmt))) (let (value size) (let ((format-alist nil)) (setq value (insert-file-contents filename nil start end)) (setq size (nth 1 value))) (if format (setq size (format-decode format size) value (cons (car value) size))) value)) (defun format-read (&optional prompt) "Read and return the name of a format. Return value is a list, like `buffer-file-format'; it may be nil. Formats are defined in `format-alist'. Optional arg is the PROMPT to use." (let* ((table (mapcar (lambda (x) (list (symbol-name (car x)))) format-alist)) (ans (completing-read (or prompt "Format: ") table nil t))) (if (not (equal "" ans)) (list (intern ans))))) ;;; ;;; Below are some functions that may be useful in writing encoding and ;;; decoding functions for use in format-alist. ;;; (defun format-replace-strings (alist &optional reverse start end) "Do multiple replacements on the buffer. ALIST is a list of (from . to) pairs, which should be proper arguments to `search-forward' and `replace-match' respectively. Optional 2nd arg REVERSE, if non-nil, means the pairs are (to . from), so that you can use the same list in both directions if it contains only literal strings. Optional args BEGIN and END specify a region of the buffer to operate on." (save-excursion (save-restriction (or start (setq start (point-min))) (if end (narrow-to-region (point-min) end)) (while alist (let ((from (if reverse (cdr (car alist)) (car (car alist)))) (to (if reverse (car (cdr alist)) (cdr (car alist))))) (goto-char start) (while (search-forward from nil t) (goto-char (match-beginning 0)) (insert to) (set-text-properties (- (point) (length to)) (point) (text-properties-at (point))) (delete-region (point) (+ (point) (- (match-end 0) (match-beginning 0))))) (setq alist (cdr alist))))))) ;;; Some list-manipulation functions that we need. (defun format-delq-cons (cons list) "Remove the given CONS from LIST by side effect, and return the new LIST. Since CONS could be the first element of LIST, write `\(setq foo \(format-delq-cons element foo))' to be sure of changing the value of `foo'." (if (eq cons list) (cdr list) (let ((p list)) (while (not (eq (cdr p) cons)) (if (null p) (error "format-delq-cons: not an element.")) (setq p (cdr p))) ;; Now (cdr p) is the cons to delete (setcdr p (cdr cons)) list))) ;; XEmacs: this is #'nset-exclusive-or with a :test of #'equal, though we ;; probably don't want to replace it right now. (defun format-make-relatively-unique (a b) "Delete common elements of lists A and B, return as pair. Compares using `equal'." (let* ((acopy (copy-sequence a)) (bcopy (copy-sequence b)) (tail acopy)) (while tail (let ((dup (member (car tail) bcopy)) (next (cdr tail))) (if dup (setq acopy (format-delq-cons tail acopy) bcopy (format-delq-cons dup bcopy))) (setq tail next))) (cons acopy bcopy))) (defun format-common-tail (a b) "Given two lists that have a common tail, return it. Compares with `equal', and returns the part of A that is equal to the equivalent part of B. If even the last items of the two are not equal, returns nil." (let ((la (length a)) (lb (length b))) ;; Make sure they are the same length (if (> la lb) (setq a (nthcdr (- la lb) a)) (setq b (nthcdr (- lb la) b)))) (while (not (equal a b)) (setq a (cdr a) b (cdr b))) a) (defun format-reorder (items order) "Arrange ITEMS to following partial ORDER. Elements of ITEMS equal to elements of ORDER will be rearranged to follow the ORDER. Unmatched items will go last." (if order (let ((item (member (car order) items))) (if item (cons (car item) (format-reorder (format-delq-cons item items) (cdr order))) (format-reorder items (cdr order)))) items)) (put 'face 'format-list-valued t) ; These text-properties take values (put 'unknown 'format-list-valued t) ; that are lists, the elements of which ; should be considered separately. ; See format-deannotate-region and ; format-annotate-region. ;;; ;;; Decoding ;;; (defun format-deannotate-region (from to translations next-fn) "Translate annotations in the region into text properties. This sets text properties between FROM to TO as directed by the TRANSLATIONS and NEXT-FN arguments. NEXT-FN is a function that searches forward from point for an annotation. It should return a list of 4 elements: \(BEGIN END NAME POSITIVE). BEGIN and END are buffer positions bounding the annotation, NAME is the name searched for in TRANSLATIONS, and POSITIVE should be non-nil if this annotation marks the beginning of a region with some property, or nil if it ends the region. NEXT-FN should return nil if there are no annotations after point. The basic format of the TRANSLATIONS argument is described in the documentation for the `format-annotate-region' function. There are some additional things to keep in mind for decoding, though: When an annotation is found, the TRANSLATIONS list is searched for a text-property name and value that corresponds to that annotation. If the text-property has several annotations associated with it, it will be used only if the other annotations are also in effect at that point. The first match found whose annotations are all present is used. The text property thus determined is set to the value over the region between the opening and closing annotations. However, if the text-property name has a non-nil `format-list-valued' property, then the value will be consed onto the surrounding value of the property, rather than replacing that value. There are some special symbols that can be used in the \"property\" slot of the TRANSLATIONS list: PARAMETER and FUNCTION \(spelled in uppercase). Annotations listed under the pseudo-property PARAMETER are considered to be arguments of the immediately surrounding annotation; the text between the opening and closing parameter annotations is deleted from the buffer but saved as a string. The surrounding annotation should be listed under the pseudo-property FUNCTION. Instead of inserting a text-property for this annotation, the function listed in the VALUE slot is called to make whatever changes are appropriate. The function's first two arguments are the START and END locations, and the rest of the arguments are any PARAMETERs found in that region. Any annotations that are found by NEXT-FN but not defined by TRANSLATIONS are saved as values of the `unknown' text-property \(which is list-valued). The TRANSLATIONS list should usually contain an entry of the form \(unknown \(nil format-annotate-value)) to write these unknown annotations back into the file." (save-excursion (save-restriction (narrow-to-region (point-min) to) (goto-char from) (let (next open-ans todo ;; loc unknown-ans) (while (setq next (funcall next-fn)) (let* ((loc (nth 0 next)) (end (nth 1 next)) (name (nth 2 next)) (positive (nth 3 next)) (found nil)) ;; Delete the annotation (delete-region loc end) (cond ;; Positive annotations are stacked, remembering location (positive (setq open-ans (cons `(,name ((,loc . nil))) open-ans))) ;; It is a negative annotation: ;; Close the top annotation & add its text property. ;; If the file's nesting is messed up, the close might not match ;; the top thing on the open-annotations stack. ;; If no matching annotation is open, just ignore the close. ((not (assoc name open-ans)) (message "Extra closing annotation (%s) in file" name)) ;; If one is open, but not on the top of the stack, close ;; the things in between as well. Set `found' when the real ;; one is closed. (t (while (not found) (let* ((top (car open-ans)) ; first on stack: should match. (top-name (car top)) ; text property name (top-extents (nth 1 top)) ; property regions (params (cdr (cdr top))) ; parameters (aalist translations) (matched nil)) (if (equal name top-name) (setq found t) (message "Improper nesting in file.")) ;; Look through property names in TRANSLATIONS (while aalist (let ((prop (car (car aalist))) (alist (cdr (car aalist)))) ;; And look through values for each property (while alist (let ((value (car (car alist))) (ans (cdr (car alist)))) (if (member top-name ans) ;; This annotation is listed, but still have to ;; check if multiple annotations are satisfied (if (notevery (lambda (r) (assoc r open-ans)) ans) nil ; multiple ans not satisfied ;; If there are multiple annotations going ;; into one text property, split up the other ;; annotations so they apply individually to ;; the other regions. (setcdr (car top-extents) loc) (let ((to-split ans) this-one extents) (while to-split (setq this-one (assoc (car to-split) open-ans) extents (nth 1 this-one)) (if (not (eq this-one top)) (setcar (cdr this-one) (format-subtract-regions extents top-extents))) (setq to-split (cdr to-split)))) ;; Set loop variables to nil so loop ;; will exit. (setq alist nil aalist nil matched t ;; pop annotation off stack. open-ans (cdr open-ans)) (let ((extents top-extents) (start (car (car top-extents))) (loc (cdr (car top-extents)))) (while extents (cond ;; Check for pseudo-properties ((eq prop 'PARAMETER) ;; A parameter of the top open ann: ;; delete text and use as arg. (if open-ans ;; (If nothing open, discard). (setq open-ans (cons (append (car open-ans) (list (buffer-substring start loc))) (cdr open-ans)))) (delete-region start loc)) ((eq prop 'FUNCTION) ;; Not a property, but a function. (let ((rtn (apply value start loc params))) (if rtn (setq todo (cons rtn todo))))) (t ;; Normal property/value pair (setq todo (cons (list start loc prop value) todo)))) (setq extents (cdr extents) start (car (car extents)) loc (cdr (car extents)))))))) (setq alist (cdr alist)))) (setq aalist (cdr aalist))) (unless matched ;; Didn't find any match for the annotation: ;; Store as value of text-property `unknown'. (setcdr (car top-extents) loc) (let ((extents top-extents) (start (car (car top-extents))) (loc (cdr (car top-extents)))) (while extents (setq open-ans (cdr open-ans) todo (cons (list start loc 'unknown top-name) todo) unknown-ans (cons name unknown-ans) extents (cdr extents) start (car (car extents)) loc (cdr (car extents)))))))))))) ;; Once entire file has been scanned, add the properties. (while todo (let* ((item (car todo)) (from (nth 0 item)) (to (nth 1 item)) (prop (nth 2 item)) (val (nth 3 item))) (if (numberp val) ; add to ambient value if numeric (format-property-increment-region from to prop val 0) (put-text-property from to prop (cond ((get prop 'format-list-valued) ; value gets consed onto ; list-valued properties (let ((prev (get-text-property from prop))) (cons val (if (listp prev) prev (list prev))))) (t val))))) ; normally, just set to val. (setq todo (cdr todo))) (if unknown-ans (message "Unknown annotations: %s" unknown-ans)))))) (defun format-subtract-regions (minu subtra) "Remove the regions in SUBTRAHEND from the regions in MINUEND. A region is a dotted pair (from . to). Both parameters are lists of regions. Each list must contain nonoverlapping, noncontiguous regions, in descending order. The result is also nonoverlapping, noncontiguous, and in descending order. The first element of MINUEND can have a cdr of nil, indicating that the end of that region is not yet known." (let* ((minuend (copy-alist minu)) (subtrahend (copy-alist subtra)) (m (car minuend)) (s (car subtrahend)) results) (while (and minuend subtrahend) (cond ;; The minuend starts after the subtrahend ends; keep it. ((> (car m) (cdr s)) (setq results (cons m results) minuend (cdr minuend) m (car minuend))) ;; The minuend extends beyond the end of the subtrahend. Chop it off. ((or (null (cdr m)) (> (cdr m) (cdr s))) (setq results (cons (cons (1+ (cdr s)) (cdr m)) results)) (setcdr m (cdr s))) ;; The subtrahend starts after the minuend ends; throw it away. ((< (cdr m) (car s)) (setq subtrahend (cdr subtrahend) s (car subtrahend))) ;; The subtrahend extends beyond the end of the minuend. Chop it off. (t ;(<= (cdr m) (cdr s))) (if (>= (car m) (car s)) (setq minuend (cdr minuend) m (car minuend)) (setcdr m (1- (car s))) (setq subtrahend (cdr subtrahend) s (car subtrahend)))))) (nconc (nreverse results) minuend))) ;; This should probably go somewhere other than format.el. Then again, ;; indent.el has alter-text-property. NOTE: We can also use ;; next-single-property-change instead of text-property-not-all, but then ;; we have to see if we passed TO. (defun format-property-increment-region (from to prop delta default) "Increment property PROP over the region between FROM and TO by the amount DELTA (which may be negative). If property PROP is nil anywhere in the region, it is treated as though it were DEFAULT." (let ((cur from) val newval next) (while cur (setq val (get-text-property cur prop) newval (+ (or val default) delta) next (text-property-not-all cur to prop val)) (put-text-property cur (or next to) prop newval) (setq cur next)))) ;;; ;;; Encoding ;;; (defun format-insert-annotations (list &optional offset) "Apply list of annotations to buffer as `write-region' would. Inserts each element of the given LIST of buffer annotations at its appropriate place. Use second arg OFFSET if the annotations' locations are not relative to the beginning of the buffer: annotations will be inserted at their location-OFFSET+1 \(ie, the offset is treated as the character number of the first character in the buffer)." (if (not offset) (setq offset 0) (setq offset (1- offset))) (let ((l (reverse list))) (while l (goto-char (- (car (car l)) offset)) (insert (cdr (car l))) (setq l (cdr l))))) (defun format-annotate-value (old new) "Return OLD and NEW as a \(close . open) annotation pair. Useful as a default function for TRANSLATIONS alist when the value of the text property is the name of the annotation that you want to use, as it is for the `unknown' text property." (cons (if old (list old)) (if new (list new)))) (defun format-annotate-region (from to trans format-fn ignore) "Generate annotations for text properties in the region. Searches for changes between FROM and TO, and describes them with a list of annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text properties not to consider; any text properties that are neither ignored nor listed in TRANSLATIONS are warned about. If you actually want to modify the region, give the return value of this function to `format-insert-annotations'. Format of the TRANSLATIONS argument: Each element is a list whose car is a PROPERTY, and the following elements are VALUES of that property followed by the names of zero or more ANNOTATIONS. Whenever the property takes on that value, the annotations \(as formatted by FORMAT-FN) are inserted into the file. When the property stops having that value, the matching negated annotation will be inserted \(it may actually be closed earlier and reopened, if necessary, to keep proper nesting). If the property's value is a list, then each element of the list is dealt with separately. If a VALUE is numeric, then it is assumed that there is a single annotation and each occurrence of it increments the value of the property by that number. Thus, given the entry \(left-margin \(4 \"indent\")), if the left margin changes from 4 to 12, two <indent> annotations will be generated. If the VALUE is nil, then instead of annotations, a function should be specified. This function is used as a default: it is called for all transitions not explicitly listed in the table. The function is called with two arguments, the OLD and NEW values of the property. It should return lists of annotations like `format-annotate-location' does. The same structure can be used in reverse for reading files." (let ((all-ans nil) ; All annotations - becomes return value (open-ans nil) ; Annotations not yet closed (loc nil) ; Current location (not-found nil)) ; Properties that couldn't be saved (while (or (null loc) (and (setq loc (next-property-change loc nil to)) (< loc to))) (or loc (setq loc from)) (let* ((ans (format-annotate-location loc (= loc from) ignore trans)) (neg-ans (format-reorder (aref ans 0) open-ans)) (pos-ans (aref ans 1)) (ignored (aref ans 2))) (setq not-found (append ignored not-found) ignore (append ignored ignore)) ;; First do the negative (closing) annotations (while neg-ans ;; Check if it's missing. This can happen (eg, a numeric property ;; going negative can generate closing annotations before there are ;; any open). Warn user & ignore. (if (not (member (car neg-ans) open-ans)) (message "Can't close %s: not open." (car neg-ans)) (while (not (equal (car neg-ans) (car open-ans))) ;; To close anno. N, need to first close ans 1 to N-1, ;; remembering to re-open them later. (setq pos-ans (cons (car open-ans) pos-ans)) (setq all-ans (cons (cons loc (funcall format-fn (car open-ans) nil)) all-ans)) (setq open-ans (cdr open-ans))) ;; Now remove the one we're really interested in from open list. (setq open-ans (cdr open-ans)) ;; And put the closing annotation here. (setq all-ans (cons (cons loc (funcall format-fn (car neg-ans) nil)) all-ans))) (setq neg-ans (cdr neg-ans))) ;; Now deal with positive (opening) annotations (let ( ; (p pos-ans) ) (while pos-ans (setq open-ans (cons (car pos-ans) open-ans)) (setq all-ans (cons (cons loc (funcall format-fn (car pos-ans) t)) all-ans)) (setq pos-ans (cdr pos-ans)))))) ;; Close any annotations still open (while open-ans (setq all-ans (cons (cons to (funcall format-fn (car open-ans) nil)) all-ans)) (setq open-ans (cdr open-ans))) (if not-found (message "These text properties could not be saved:\n %s" not-found)) (nreverse all-ans))) ;;; Internal functions for format-annotate-region. (defun format-annotate-location (loc all ignore trans) "Return annotation(s) needed at LOCATION. This includes any properties that change between LOC-1 and LOC. If ALL is true, don't look at previous location, but generate annotations for all non-nil properties. Third argument IGNORE is a list of text-properties not to consider. Return value is a vector of 3 elements: 1. List of names of the annotations to close 2. List of the names of annotations to open. 3. List of properties that were ignored or couldn't be annotated." (let* ((prev-loc (1- loc)) (before-plist (if all nil (text-properties-at prev-loc))) (after-plist (text-properties-at loc)) p negatives positives prop props not-found) ;; make list of all property names involved (setq p before-plist) (while p (if (not (memq (car p) props)) (setq props (cons (car p) props))) (setq p (cdr (cdr p)))) (setq p after-plist) (while p (if (not (memq (car p) props)) (setq props (cons (car p) props))) (setq p (cdr (cdr p)))) (while props (setq prop (car props) props (cdr props)) (if (memq prop ignore) nil ; If it's been ignored before, ignore it now. (let ((before (if all nil (car (cdr (memq prop before-plist))))) (after (car (cdr (memq prop after-plist))))) (if (equal before after) nil ; no change; ignore (let ((result (format-annotate-single-property-change prop before after trans))) (if (not result) (setq not-found (cons prop not-found)) (setq negatives (nconc negatives (car result)) positives (nconc positives (cdr result))))))))) (vector negatives positives not-found))) (defun format-annotate-single-property-change (prop old new trans) "Return annotations for PROPERTY changing from OLD to NEW. These are searched for in the TRANSLATIONS alist. If NEW does not appear in the list, but there is a default function, then that function is called. Annotations to open and to close are returned as a dotted pair." (let ((prop-alist (cdr (assoc prop trans))) ;; default ) (if (not prop-alist) nil ;; If either old or new is a list, have to treat both that way. (if (or (consp old) (consp new)) (let* ((old (if (listp old) old (list old))) (new (if (listp new) new (list new))) ;; (tail (format-common-tail old new)) close open) (while old (setq close (append (car (format-annotate-atomic-property-change prop-alist (car old) nil)) close) old (cdr old))) (while new (setq open (append (cdr (format-annotate-atomic-property-change prop-alist nil (car new))) open) new (cdr new))) (format-make-relatively-unique close open)) (format-annotate-atomic-property-change prop-alist old new))))) (defun format-annotate-atomic-property-change (prop-alist old new) "Internal function annotate a single property change. PROP-ALIST is the relevant segment of a TRANSLATIONS list. OLD and NEW are the values." (let (num-ann) ;; If old and new values are numbers, ;; look for a number in PROP-ALIST. (if (and (or (null old) (numberp old)) (or (null new) (numberp new))) (progn (setq num-ann prop-alist) (while (and num-ann (not (numberp (car (car num-ann))))) (setq num-ann (cdr num-ann))))) (if num-ann ;; Numerical annotation - use difference (progn ;; If property is numeric, nil means 0 (cond ((and (numberp old) (null new)) (setq new 0)) ((and (numberp new) (null old)) (setq old 0))) (let* ((entry (car num-ann)) (increment (car entry)) (n (ceiling (/ (float (- new old)) (float increment)))) (anno (car (cdr entry)))) (if (> n 0) (cons nil (make-list n anno)) (cons (make-list (- n) anno) nil)))) ;; Standard annotation (let ((close (and old (cdr (assoc old prop-alist)))) (open (and new (cdr (assoc new prop-alist))))) (if (or close open) (format-make-relatively-unique close open) ;; Call "Default" function, if any (let ((default (assq nil prop-alist))) (if default (funcall (car (cdr default)) old new)))))))) ;;; format.el ends here