mirror of
git://git.sv.gnu.org/emacs.git
synced 2026-06-14 04:21:24 +00:00
Call format-spec substitution functions in current buffer
* lisp/format-spec.el (format-spec): Call format-spec substitution functions in current buffer. I.e., in the same buffer in which substitution expressions are evaluated. Previously functions were instead called in the temporary buffer used to deal with FORMAT.
This commit is contained in:
parent
1538090889
commit
046db64044
1 changed files with 60 additions and 56 deletions
|
|
@ -87,62 +87,66 @@ any occurrences of \"%%\" in FORMAT verbatim in the result.
|
||||||
|
|
||||||
If SPLIT, instead of returning a single string, a list of strings
|
If SPLIT, instead of returning a single string, a list of strings
|
||||||
is returned, where each format spec is its own element."
|
is returned, where each format spec is its own element."
|
||||||
(with-temp-buffer
|
(let ((orig-buffer (current-buffer)))
|
||||||
(let ((split-start (point-min))
|
(with-temp-buffer
|
||||||
(split-result nil))
|
(let ((split-start (point-min))
|
||||||
(insert format)
|
(split-result nil))
|
||||||
(goto-char (point-min))
|
(insert format)
|
||||||
(while (search-forward "%" nil t)
|
(goto-char (point-min))
|
||||||
(cond
|
(while (search-forward "%" nil t)
|
||||||
;; Quoted percent sign.
|
(cond
|
||||||
((= (following-char) ?%)
|
;; Quoted percent sign.
|
||||||
(when (memq ignore-missing '(nil ignore delete))
|
((= (following-char) ?%)
|
||||||
(delete-char 1)))
|
(when (memq ignore-missing '(nil ignore delete))
|
||||||
;; Valid format spec.
|
(delete-char 1)))
|
||||||
((looking-at (rx (? (group (+ (in " 0<>^_-"))))
|
;; Valid format spec.
|
||||||
(? (group (+ digit)))
|
((looking-at (rx (? (group (+ (in " 0<>^_-"))))
|
||||||
(? (group ?. (+ digit)))
|
(? (group (+ digit)))
|
||||||
(group alpha)))
|
(? (group ?. (+ digit)))
|
||||||
(let* ((beg (point))
|
(group alpha)))
|
||||||
(end (match-end 0))
|
(let* ((beg (point))
|
||||||
(flags (match-string 1))
|
(end (match-end 0))
|
||||||
(width (match-string 2))
|
(flags (match-string 1))
|
||||||
(trunc (match-string 3))
|
(width (match-string 2))
|
||||||
(char (string-to-char (match-string 4)))
|
(trunc (match-string 3))
|
||||||
(text (let ((res (cdr (assq char specification))))
|
(char (string-to-char (match-string 4)))
|
||||||
(if (functionp res) (funcall res) res))))
|
(text (let ((res (cdr (assq char specification))))
|
||||||
(when (and split
|
(if (functionp res)
|
||||||
(not (= (1- beg) split-start)))
|
(with-current-buffer orig-buffer
|
||||||
(push (buffer-substring split-start (1- beg)) split-result))
|
(funcall res))
|
||||||
(cond (text
|
res))))
|
||||||
;; Handle flags.
|
(when (and split
|
||||||
(setq text (format-spec--do-flags
|
(not (= (1- beg) split-start)))
|
||||||
(format "%s" text)
|
(push (buffer-substring split-start (1- beg)) split-result))
|
||||||
(format-spec--parse-flags flags)
|
(cond (text
|
||||||
(and width (string-to-number width))
|
;; Handle flags.
|
||||||
(and trunc (car (read-from-string trunc 1)))))
|
(setq text (format-spec--do-flags
|
||||||
;; Insert first, to preserve text properties.
|
(format "%s" text)
|
||||||
(insert-and-inherit text)
|
(format-spec--parse-flags flags)
|
||||||
;; Delete the specifier body.
|
(and width (string-to-number width))
|
||||||
(delete-region (point) (+ end (length text)))
|
(and trunc (car (read-from-string trunc 1)))))
|
||||||
;; Delete the percent sign.
|
;; Insert first, to preserve text properties.
|
||||||
(delete-region (1- beg) beg))
|
(insert-and-inherit text)
|
||||||
((eq ignore-missing 'delete)
|
;; Delete the specifier body.
|
||||||
;; Delete the whole format spec.
|
(delete-region (point) (+ end (length text)))
|
||||||
(delete-region (1- beg) end))
|
;; Delete the percent sign.
|
||||||
((not ignore-missing)
|
(delete-region (1- beg) beg))
|
||||||
(error "Invalid format character: `%%%c'" char)))
|
((eq ignore-missing 'delete)
|
||||||
(when split
|
;; Delete the whole format spec.
|
||||||
(push (buffer-substring (1- beg) (point)) split-result)
|
(delete-region (1- beg) end))
|
||||||
(setq split-start (point)))))
|
((not ignore-missing)
|
||||||
;; Signal an error on bogus format strings.
|
(error "Invalid format character: `%%%c'" char)))
|
||||||
((not ignore-missing)
|
(when split
|
||||||
(error "Invalid format string"))))
|
(push (buffer-substring (1- beg) (point)) split-result)
|
||||||
(if (not split)
|
(setq split-start (point)))))
|
||||||
(buffer-string)
|
;; Signal an error on bogus format strings.
|
||||||
(unless (= split-start (point-max))
|
((not ignore-missing)
|
||||||
(push (buffer-substring split-start (point-max)) split-result))
|
(error "Invalid format string"))))
|
||||||
(nreverse split-result)))))
|
(if (not split)
|
||||||
|
(buffer-string)
|
||||||
|
(unless (= split-start (point-max))
|
||||||
|
(push (buffer-substring split-start (point-max)) split-result))
|
||||||
|
(nreverse split-result))))))
|
||||||
|
|
||||||
(defun format-spec--do-flags (str flags width trunc)
|
(defun format-spec--do-flags (str flags width trunc)
|
||||||
"Return STR formatted according to FLAGS, WIDTH, and TRUNC.
|
"Return STR formatted according to FLAGS, WIDTH, and TRUNC.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue