X-Git-Url: https://git.hcoop.net/bpt/emacs.git/blobdiff_plain/7b1019e2781472c793d0bf74e2b9ee17894270b8..398a825b8d994882672906103f330ad8662d1ed6:/lisp/format.el diff --git a/lisp/format.el b/lisp/format.el index b58a265dcc..d4262e2d0e 100644 --- a/lisp/format.el +++ b/lisp/format.el @@ -1,7 +1,7 @@ ;;; format.el --- read and save files in multiple formats -;; Copyright (C) 1994, 1995, 1997, 1999, 2001, 2002, 2003, 2004, -;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc. +;; Copyright (C) 1994, 1995, 1997, 1999, 2001, 2002, 2003, 2004, 2005, +;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. ;; Author: Boris Goldowsky @@ -64,39 +64,42 @@ (put 'buffer-auto-save-file-format 'permanent-local t) (defvar format-alist - '((text/enriched "Extended MIME text/enriched format." - "Content-[Tt]ype:[ \t]*text/enriched" + ;; FIXME: maybe each item can be purecopied instead of just the strings. + `((text/enriched ,(purecopy "Extended MIME text/enriched format.") + ,(purecopy "Content-[Tt]ype:[ \t]*text/enriched") enriched-decode enriched-encode t enriched-mode) - (plain "ISO 8859-1 standard format, no text properties." + (plain ,(purecopy "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) - (TeX "TeX (encoding)" + (TeX ,(purecopy "TeX (encoding)") nil iso-tex2iso iso-iso2tex t nil) - (gtex "German TeX (encoding)" + (gtex ,(purecopy "German TeX (encoding)") nil iso-gtex2iso iso-iso2gtex t nil) - (html "HTML/SGML \"ISO 8879:1986//ENTITIES Added Latin 1//EN\" (encoding)" + (html ,(purecopy "HTML/SGML \"ISO 8879:1986//ENTITIES Added Latin 1//EN\" (encoding)") nil iso-sgml2iso iso-iso2sgml t nil) - (rot13 "rot13" + (rot13 ,(purecopy "rot13") nil - "tr a-mn-z n-za-m" "tr a-mn-z n-za-m" t nil) - (duden "Duden Ersatzdarstellung" + ,(purecopy "tr a-mn-z n-za-m") ,(purecopy "tr a-mn-z n-za-m") t nil) + (duden ,(purecopy "Duden Ersatzdarstellung") nil - "diac" iso-iso2duden t nil) - (de646 "German ASCII (ISO 646)" + ,(purecopy "diac") iso-iso2duden t nil) + (de646 ,(purecopy "German ASCII (ISO 646)") nil - "recode -f iso646-ge:latin1" "recode -f latin1:iso646-ge" t nil) - (denet "net German" + ,(purecopy "recode -f iso646-ge:latin1") + ,(purecopy "recode -f latin1:iso646-ge") t nil) + (denet ,(purecopy "net German") nil iso-german iso-cvt-read-only t nil) - (esnet "net Spanish" + (esnet ,(purecopy "net Spanish") nil 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). +Elements are of the form +\(NAME DOC-STR REGEXP FROM-FN TO-FN MODIFY MODE-FN PRESERVE). NAME is a symbol, which is stored in `buffer-file-format'. @@ -105,9 +108,9 @@ DOC-STR should be a single line providing more information about the 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. Use nil to avoid - matching at all for formats for which it isn't appropriate to - require explicit encoding/decoding. + it should match only files in that format. REGEXP may be nil, in + which case the format will never be applied automatically to a file. + Use this for formats that you only ever want to apply manually. FROM-FN is called to decode files in that format; it takes two args, BEGIN and END, and can make any modifications it likes, returning the new @@ -135,6 +138,8 @@ MODE-FN, if specified, is called when visiting a file with that format. PRESERVE, if non-nil, means that `format-write-file' should not remove this format from `buffer-file-format'.") +;;;###autoload +(put 'format-alist 'risky-local-variable t) ;;; Basic Functions (called from Lisp) @@ -224,6 +229,15 @@ For most purposes, consider using `format-encode-region' instead." (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) + (when (buffer-live-p copy-buf) + (with-current-buffer copy-buf + ;; Set write-region-post-annotation-function to + ;; delete the buffer once the write is done, but do + ;; it after running to-fn so it doesn't affect + ;; write-region calls in to-fn. + (set (make-local-variable + 'write-region-post-annotation-function) + 'kill-buffer))) nil) ;; Otherwise just call function, it will return annotations. (funcall to-fn from to orig-buf))))) @@ -358,13 +372,19 @@ one of the formats defined in `format-alist', or a list of such symbols." (setq format (cdr format))))))) (defun format-write-file (filename format &optional confirm) - "Write current buffer into file FILENAME using some FORMAT. -Make buffer visit that file and set the format as the default for future -saves. If the buffer is already visiting a file, you can specify a directory -name as FILENAME, to write a file of the same old name in that directory. - -If optional third arg CONFIRM is non-nil, ask for confirmation before -overwriting an existing file. Interactively, confirmation is required + "Write current buffer into FILENAME, using a format based on FORMAT. +Constructs the actual format starting from FORMAT, then appending +any elements from the value of `buffer-file-format' with a non-nil +`preserve' flag (see the documentation of `format-alist'), if they +are not already present in FORMAT. It then updates `buffer-file-format' +with this format, making it the default for future saves. + +If the buffer is already visiting a file, you can specify a +directory name as FILENAME, to write a file of the same old name +in that directory. + +If optional third arg CONFIRM is non-nil, asks for confirmation before +overwriting an existing file. Interactively, requires confirmation unless you supply a prefix argument." (interactive ;; Same interactive spec as write-file, plus format question.