comparison lisp/format.el @ 72247:2d16125405b4

Docstring style and grammar tweaks; nfc.
author Thien-Thi Nguyen <ttn@gnuvola.org>
date Wed, 02 Aug 2006 22:51:44 +0000
parents 3bd95f4f2941
children b2e5081b9320 858cb33ae39d
comparison
equal deleted inserted replaced
72246:c3cdfa41017b 72247:2d16125405b4
115 format. It is currently unused, but in the future will be shown to 115 format. It is currently unused, but in the future will be shown to
116 the user if they ask for more information. 116 the user if they ask for more information.
117 117
118 REGEXP is a regular expression to match against the beginning of the file; 118 REGEXP is a regular expression to match against the beginning of the file;
119 it should match only files in that format. Use nil to avoid 119 it should match only files in that format. Use nil to avoid
120 matching at all for formats for which this isn't appropriate to 120 matching at all for formats for which it isn't appropriate to
121 require explicit encoding/decoding. 121 require explicit encoding/decoding.
122 122
123 FROM-FN is called to decode files in that format; it gets two args, BEGIN 123 FROM-FN is called to decode files in that format; it takes two args, BEGIN
124 and END, and can make any modifications it likes, returning the new 124 and END, and can make any modifications it likes, returning the new
125 end. It must make sure that the beginning of the file no longer 125 end. It must make sure that the beginning of the file no longer
126 matches REGEXP, or else it will get called again. 126 matches REGEXP, or else it will get called again.
127 Alternatively, FROM-FN can be a string, which specifies a shell command 127 Alternatively, FROM-FN can be a string, which specifies a shell command
128 (including options) to be used as a filter to perform the conversion. 128 (including options) to be used as a filter to perform the conversion.
129 129
130 TO-FN is called to encode a region into that format; it is passed three 130 TO-FN is called to encode a region into that format; it takes three
131 arguments: BEGIN, END, and BUFFER. BUFFER is the original buffer that 131 arguments: BEGIN, END, and BUFFER. BUFFER is the original buffer that
132 the data being written came from, which the function could use, for 132 the data being written came from, which the function could use, for
133 example, to find the values of local variables. TO-FN should either 133 example, to find the values of local variables. TO-FN should either
134 return a list of annotations like `write-region-annotate-functions', 134 return a list of annotations like `write-region-annotate-functions',
135 or modify the region and return the new end. 135 or modify the region and return the new end.
140 TO-FN will not make any changes but will instead return a list of 140 TO-FN will not make any changes but will instead return a list of
141 annotations. 141 annotations.
142 142
143 MODE-FN, if specified, is called when visiting a file with that format. 143 MODE-FN, if specified, is called when visiting a file with that format.
144 It is called with a single positive argument, on the assumption 144 It is called with a single positive argument, on the assumption
145 that it turns on some Emacs mode. 145 that this would turn on some minor mode.
146 146
147 PRESERVE, if non-nil, means that `format-write-file' should not remove 147 PRESERVE, if non-nil, means that `format-write-file' should not remove
148 this format from `buffer-file-formats'.") 148 this format from `buffer-file-formats'.")
149 149
150 ;;; Basic Functions (called from Lisp) 150 ;;; Basic Functions (called from Lisp)
151 151
152 (defun format-encode-run-method (method from to &optional buffer) 152 (defun format-encode-run-method (method from to &optional buffer)
153 "Translate using function or shell script METHOD the text from FROM to TO. 153 "Translate using METHOD the text from FROM to TO.
154 If METHOD is a string, it is a shell command; 154 If METHOD is a string, it is a shell command (including options);
155 otherwise, it should be a Lisp function. 155 otherwise, it should be a Lisp function.
156 BUFFER should be the buffer that the output originally came from." 156 BUFFER should be the buffer that the output originally came from."
157 (if (stringp method) 157 (if (stringp method)
158 (let ((error-buff (get-buffer-create "*Format Errors*")) 158 (let ((error-buff (get-buffer-create "*Format Errors*"))
159 (coding-system-for-read 'no-conversion) 159 (coding-system-for-read 'no-conversion)
171 (switch-to-buffer-other-window error-buff) 171 (switch-to-buffer-other-window error-buff)
172 (error "Format encoding failed"))) 172 (error "Format encoding failed")))
173 (funcall method from to buffer))) 173 (funcall method from to buffer)))
174 174
175 (defun format-decode-run-method (method from to &optional buffer) 175 (defun format-decode-run-method (method from to &optional buffer)
176 "Decode using function or shell script METHOD the text from FROM to TO. 176 "Decode using METHOD the text from FROM to TO.
177 If METHOD is a string, it is a shell command; otherwise, it should be 177 If METHOD is a string, it is a shell command (including options); otherwise,
178 a Lisp function. Decoding is done for the given BUFFER." 178 it should be a Lisp function. Decoding is done for the given BUFFER."
179 (if (stringp method) 179 (if (stringp method)
180 (let ((error-buff (get-buffer-create "*Format Errors*")) 180 (let ((error-buff (get-buffer-create "*Format Errors*"))
181 (coding-system-for-write 'no-conversion) 181 (coding-system-for-write 'no-conversion)
182 format-alist) 182 format-alist)
183 (with-current-buffer error-buff 183 (with-current-buffer error-buff
198 (point)) 198 (point))
199 (funcall method from to))) 199 (funcall method from to)))
200 200
201 (defun format-annotate-function (format from to orig-buf format-count) 201 (defun format-annotate-function (format from to orig-buf format-count)
202 "Return annotations for writing region as FORMAT. 202 "Return annotations for writing region as FORMAT.
203 FORMAT is a symbol naming one of the formats defined in `format-alist', 203 FORMAT is a symbol naming one of the formats defined in `format-alist'.
204 it must be a single symbol, not a list like `buffer-file-format'. 204 It must be a single symbol, not a list like `buffer-file-format'.
205 FROM and TO delimit the region to be operated on in the current buffer. 205 FROM and TO delimit the region to be operated on in the current buffer.
206 ORIG-BUF is the original buffer that the data came from. 206 ORIG-BUF is the original buffer that the data came from.
207 207
208 FORMAT-COUNT is an integer specifying how many times this function has 208 FORMAT-COUNT is an integer specifying how many times this function has
209 been called in the process of decoding ORIG-BUF. 209 been called in the process of decoding ORIG-BUF.
210 210
211 This function works like a function on `write-region-annotate-functions': 211 This function works like a function in `write-region-annotate-functions':
212 it either returns a list of annotations, or returns with a different buffer 212 it either returns a list of annotations, or returns with a different buffer
213 current, which contains the modified text to write. In the latter case, 213 current, which contains the modified text to write. In the latter case,
214 this function's value is nil. 214 this function's value is nil.
215 215
216 For most purposes, consider using `format-encode-region' instead." 216 For most purposes, consider using `format-encode-region' instead."
251 Second arg LENGTH is the number of characters following point to operate on. 251 Second arg LENGTH is the number of characters following point to operate on.
252 If optional third arg VISIT-FLAG is true, set `buffer-file-format' 252 If optional third arg VISIT-FLAG is true, set `buffer-file-format'
253 to the reverted list of formats used, and call any mode functions defined 253 to the reverted list of formats used, and call any mode functions defined
254 for those formats. 254 for those formats.
255 255
256 Returns the new length of the decoded region. 256 Return the new length of the decoded region.
257 257
258 For most purposes, consider using `format-decode-region' instead." 258 For most purposes, consider using `format-decode-region' instead."
259 (let ((mod (buffer-modified-p)) 259 (let ((mod (buffer-modified-p))
260 (begin (point)) 260 (begin (point))
261 (end (+ (point) length))) 261 (end (+ (point) length)))
310 ;;; Interactive functions & entry points 310 ;;; Interactive functions & entry points
311 ;;; 311 ;;;
312 312
313 (defun format-decode-buffer (&optional format) 313 (defun format-decode-buffer (&optional format)
314 "Translate the buffer from some FORMAT. 314 "Translate the buffer from some FORMAT.
315 If the format is not specified, this function attempts to guess. 315 If the format is not specified, attempt a regexp-based guess.
316 `buffer-file-format' is set to the format used, and any mode-functions 316 Set `buffer-file-format' to the format used, and call any
317 for the format are called." 317 format-specific mode functions."
318 (interactive 318 (interactive
319 (list (format-read "Translate buffer from format (default guess): "))) 319 (list (format-read "Translate buffer from format (default guess): ")))
320 (save-excursion 320 (save-excursion
321 (goto-char (point-min)) 321 (goto-char (point-min))
322 (format-decode format (buffer-size) t))) 322 (format-decode format (buffer-size) t)))
341 buffer-file-format)))) 341 buffer-file-format))))
342 (format-encode-region (point-min) (point-max) format)) 342 (format-encode-region (point-min) (point-max) format))
343 343
344 (defun format-encode-region (beg end &optional format) 344 (defun format-encode-region (beg end &optional format)
345 "Translate the region into some FORMAT. 345 "Translate the region into some FORMAT.
346 FORMAT defaults to `buffer-file-format', it is a symbol naming 346 FORMAT defaults to `buffer-file-format'. It is a symbol naming
347 one of the formats defined in `format-alist', or a list of such symbols." 347 one of the formats defined in `format-alist', or a list of such symbols."
348 (interactive 348 (interactive
349 (list (region-beginning) (region-end) 349 (list (region-beginning) (region-end)
350 (format-read (format "Translate region to format (default %s): " 350 (format-read (format "Translate region to format (default %s): "
351 buffer-file-format)))) 351 buffer-file-format))))
372 "Write current buffer into file FILENAME using some FORMAT. 372 "Write current buffer into file FILENAME using some FORMAT.
373 Make buffer visit that file and set the format as the default for future 373 Make buffer visit that file and set the format as the default for future
374 saves. If the buffer is already visiting a file, you can specify a directory 374 saves. If the buffer is already visiting a file, you can specify a directory
375 name as FILENAME, to write a file of the same old name in that directory. 375 name as FILENAME, to write a file of the same old name in that directory.
376 376
377 If optional third arg CONFIRM is non-nil, this function asks for 377 If optional third arg CONFIRM is non-nil, ask for confirmation before
378 confirmation before overwriting an existing file. Interactively, 378 overwriting an existing file. Interactively, confirmation is required
379 confirmation is required unless you supply a prefix argument." 379 unless you supply a prefix argument."
380 (interactive 380 (interactive
381 ;; Same interactive spec as write-file, plus format question. 381 ;; Same interactive spec as write-file, plus format question.
382 (let* ((file (if buffer-file-name 382 (let* ((file (if buffer-file-name
383 (read-file-name "Write file: " 383 (read-file-name "Write file: "
384 nil nil nil nil) 384 nil nil nil nil)
417 417
418 (defun format-insert-file (filename format &optional beg end) 418 (defun format-insert-file (filename format &optional beg end)
419 "Insert the contents of file FILENAME using data format FORMAT. 419 "Insert the contents of file FILENAME using data format FORMAT.
420 If FORMAT is nil then do not do any format conversion. 420 If FORMAT is nil then do not do any format conversion.
421 The optional third and fourth arguments BEG and END specify 421 The optional third and fourth arguments BEG and END specify
422 the part of the file to read. 422 the part (in bytes) of the file to read.
423 423
424 The return value is like the value of `insert-file-contents': 424 The return value is like the value of `insert-file-contents':
425 a list (ABSOLUTE-FILE-NAME SIZE)." 425 a list (ABSOLUTE-FILE-NAME SIZE)."
426 (interactive 426 (interactive
427 ;; Same interactive spec as write-file, plus format question. 427 ;; Same interactive spec as write-file, plus format question.
454 ;;; 454 ;;;
455 455
456 (defun format-replace-strings (alist &optional reverse beg end) 456 (defun format-replace-strings (alist &optional reverse beg end)
457 "Do multiple replacements on the buffer. 457 "Do multiple replacements on the buffer.
458 ALIST is a list of (FROM . TO) pairs, which should be proper arguments to 458 ALIST is a list of (FROM . TO) pairs, which should be proper arguments to
459 `search-forward' and `replace-match' respectively. 459 `search-forward' and `replace-match', respectively.
460 Optional 2nd arg REVERSE, if non-nil, means the pairs are (TO . FROM), so that 460 Optional second arg REVERSE, if non-nil, means the pairs are (TO . FROM),
461 you can use the same list in both directions if it contains only literal 461 so that you can use the same list in both directions if it contains only
462 strings. 462 literal strings.
463 Optional args BEG and END specify a region of the buffer on which to operate." 463 Optional args BEG and END specify a region of the buffer on which to operate."
464 (save-excursion 464 (save-excursion
465 (save-restriction 465 (save-restriction
466 (or beg (setq beg (point-min))) 466 (or beg (setq beg (point-min)))
467 (if end (narrow-to-region (point-min) end)) 467 (if end (narrow-to-region (point-min) end))
495 (setcdr p (cdr cons)) 495 (setcdr p (cdr cons))
496 list))) 496 list)))
497 497
498 (defun format-make-relatively-unique (a b) 498 (defun format-make-relatively-unique (a b)
499 "Delete common elements of lists A and B, return as pair. 499 "Delete common elements of lists A and B, return as pair.
500 Compares using `equal'." 500 Compare using `equal'."
501 (let* ((acopy (copy-sequence a)) 501 (let* ((acopy (copy-sequence a))
502 (bcopy (copy-sequence b)) 502 (bcopy (copy-sequence b))
503 (tail acopy)) 503 (tail acopy))
504 (while tail 504 (while tail
505 (let ((dup (member (car tail) bcopy)) 505 (let ((dup (member (car tail) bcopy))
509 (setq tail next))) 509 (setq tail next)))
510 (cons acopy bcopy))) 510 (cons acopy bcopy)))
511 511
512 (defun format-common-tail (a b) 512 (defun format-common-tail (a b)
513 "Given two lists that have a common tail, return it. 513 "Given two lists that have a common tail, return it.
514 Compares with `equal', and returns the part of A that is equal to the 514 Compare with `equal', and return the part of A that is equal to the
515 equivalent part of B. If even the last items of the two are not equal, 515 equivalent part of B. If even the last items of the two are not equal,
516 returns nil." 516 return nil."
517 (let ((la (length a)) 517 (let ((la (length a))
518 (lb (length b))) 518 (lb (length b)))
519 ;; Make sure they are the same length 519 ;; Make sure they are the same length
520 (if (> la lb) 520 (if (> la lb)
521 (setq a (nthcdr (- la lb) a)) 521 (setq a (nthcdr (- la lb) a))
532 (while (consp list) 532 (while (consp list)
533 (setq list (cdr list))) 533 (setq list (cdr list)))
534 (null list))) 534 (null list)))
535 535
536 (defun format-reorder (items order) 536 (defun format-reorder (items order)
537 "Arrange ITEMS to following partial ORDER. 537 "Arrange ITEMS to follow partial ORDER.
538 Elements of ITEMS equal to elements of ORDER will be rearranged to follow the 538 Elements of ITEMS equal to elements of ORDER will be rearranged
539 ORDER. Unmatched items will go last." 539 to follow the ORDER. Unmatched items will go last."
540 (if order 540 (if order
541 (let ((item (member (car order) items))) 541 (let ((item (member (car order) items)))
542 (if item 542 (if item
543 (cons (car item) 543 (cons (car item)
544 (format-reorder (format-delq-cons item items) 544 (format-reorder (format-delq-cons item items)
791 ;; This should probably go somewhere other than format.el. Then again, 791 ;; This should probably go somewhere other than format.el. Then again,
792 ;; indent.el has alter-text-property. NOTE: We can also use 792 ;; indent.el has alter-text-property. NOTE: We can also use
793 ;; next-single-property-change instead of text-property-not-all, but then 793 ;; next-single-property-change instead of text-property-not-all, but then
794 ;; we have to see if we passed TO. 794 ;; we have to see if we passed TO.
795 (defun format-property-increment-region (from to prop delta default) 795 (defun format-property-increment-region (from to prop delta default)
796 "Over the region between FROM and TO increment property PROP by amount DELTA. 796 "In the region from FROM to TO increment property PROP by amount DELTA.
797 DELTA may be negative. If property PROP is nil anywhere 797 DELTA may be negative. If property PROP is nil anywhere
798 in the region, it is treated as though it were DEFAULT." 798 in the region, it is treated as though it were DEFAULT."
799 (let ((cur from) val newval next) 799 (let ((cur from) val newval next)
800 (while cur 800 (while cur
801 (setq val (get-text-property cur prop) 801 (setq val (get-text-property cur prop)
808 ;;; Encoding 808 ;;; Encoding
809 ;;; 809 ;;;
810 810
811 (defun format-insert-annotations (list &optional offset) 811 (defun format-insert-annotations (list &optional offset)
812 "Apply list of annotations to buffer as `write-region' would. 812 "Apply list of annotations to buffer as `write-region' would.
813 Inserts each element of the given LIST of buffer annotations at its 813 Insert each element of the given LIST of buffer annotations at its
814 appropriate place. Use second arg OFFSET if the annotations' locations are 814 appropriate place. Use second arg OFFSET if the annotations' locations are
815 not relative to the beginning of the buffer: annotations will be inserted 815 not relative to the beginning of the buffer: annotations will be inserted
816 at their location-OFFSET+1 \(ie, the offset is treated as the position of 816 at their location-OFFSET+1 \(ie, the offset is treated as the position of
817 the first character in the buffer)." 817 the first character in the buffer)."
818 (if (not offset) 818 (if (not offset)
832 (cons (if old (list old)) 832 (cons (if old (list old))
833 (if new (list new)))) 833 (if new (list new))))
834 834
835 (defun format-annotate-region (from to translations format-fn ignore) 835 (defun format-annotate-region (from to translations format-fn ignore)
836 "Generate annotations for text properties in the region. 836 "Generate annotations for text properties in the region.
837 Searches for changes between FROM and TO, and describes them with a list of 837 Search for changes between FROM and TO, and describe them with a list of
838 annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text 838 annotations as defined by alist TRANSLATIONS and FORMAT-FN. IGNORE lists text
839 properties not to consider; any text properties that are neither ignored nor 839 properties not to consider; any text properties that are neither ignored nor
840 listed in TRANSLATIONS are warned about. 840 listed in TRANSLATIONS are warned about.
841 If you actually want to modify the region, give the return value of this 841 If you actually want to modify the region, give the return value of this
842 function to `format-insert-annotations'. 842 function to `format-insert-annotations'.
973 973
974 (defun format-annotate-single-property-change (prop old new translations) 974 (defun format-annotate-single-property-change (prop old new translations)
975 "Return annotations for property PROP changing from OLD to NEW. 975 "Return annotations for property PROP changing from OLD to NEW.
976 These are searched for in the translations alist TRANSLATIONS 976 These are searched for in the translations alist TRANSLATIONS
977 (see `format-annotate-region' for the format). 977 (see `format-annotate-region' for the format).
978 If NEW does not appear in the list, but there is a default function, then that 978 If NEW does not appear in the list, but there is a default function,
979 function is called. 979 then call that function.
980 Returns a cons of the form (CLOSE . OPEN) 980 Return a cons of the form (CLOSE . OPEN)
981 where CLOSE is a list of annotations to close 981 where CLOSE is a list of annotations to close
982 and OPEN is a list of annotations to open. 982 and OPEN is a list of annotations to open.
983 983
984 The annotations in CLOSE and OPEN need not be strings. 984 The annotations in CLOSE and OPEN need not be strings.
985 They can be whatever the FORMAT-FN in `format-annotate-region' 985 They can be whatever the FORMAT-FN in `format-annotate-region'
1014 new (cdr new))) 1014 new (cdr new)))
1015 (format-make-relatively-unique close open))) 1015 (format-make-relatively-unique close open)))
1016 (format-annotate-atomic-property-change prop-alist old new))))) 1016 (format-annotate-atomic-property-change prop-alist old new)))))
1017 1017
1018 (defun format-annotate-atomic-property-change (prop-alist old new) 1018 (defun format-annotate-atomic-property-change (prop-alist old new)
1019 "Internal function annotate a single property change. 1019 "Internal function to annotate a single property change.
1020 PROP-ALIST is the relevant element of a TRANSLATIONS list. 1020 PROP-ALIST is the relevant element of a TRANSLATIONS list.
1021 OLD and NEW are the values." 1021 OLD and NEW are the values."
1022 (let (num-ann) 1022 (let (num-ann)
1023 ;; If old and new values are numbers, 1023 ;; If old and new values are numbers,
1024 ;; look for a number in PROP-ALIST. 1024 ;; look for a number in PROP-ALIST.