Commit b44f0c45 authored by Lars Ingebrigtsen's avatar Lars Ingebrigtsen

Allow passing unknown specs to format-spec

* lisp/format-spec.el (format-spec): Allow passing through format
strings that have no specs (to be able to act as a filter).  Also
add an example.
* test/lisp/format-spec-tests.el (test-format-spec): Add tests for
the new functionality.
parent b41ddb7a
Pipeline #2373 passed with stage
in 54 minutes and 51 seconds
...@@ -24,40 +24,55 @@ ...@@ -24,40 +24,55 @@
;;; Code: ;;; Code:
(defun format-spec (format specification) (defun format-spec (format specification &optional only-present)
"Return a string based on FORMAT and SPECIFICATION. "Return a string based on FORMAT and SPECIFICATION.
FORMAT is a string containing `format'-like specs like \"bash %u %k\", FORMAT is a string containing `format'-like specs like \"su - %u %k\",
while SPECIFICATION is an alist mapping from format spec characters while SPECIFICATION is an alist mapping from format spec characters
to values. Any text properties on a %-spec itself are propagated to to values.
the text that it generates."
For instance:
(format-spec \"su - %u %k\"
`((?u . ,(user-login-name))
(?k . \"ls\")))
Any text properties on a %-spec itself are propagated to the text
that it generates.
If ONLY-PRESENT, format spec characters not present in
SPECIFICATION are ignored, and the \"%\" characters are left
where they are, including \"%%\" strings."
(with-temp-buffer (with-temp-buffer
(insert format) (insert format)
(goto-char (point-min)) (goto-char (point-min))
(while (search-forward "%" nil t) (while (search-forward "%" nil t)
(cond (cond
;; Quoted percent sign. ;; Quoted percent sign.
((eq (char-after) ?%) ((eq (char-after) ?%)
(delete-char 1)) (unless only-present
;; Valid format spec. (delete-char 1)))
((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)") ;; Valid format spec.
(let* ((num (match-string 1)) ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
(spec (string-to-char (match-string 2))) (let* ((num (match-string 1))
(val (assq spec specification))) (spec (string-to-char (match-string 2)))
(unless val (val (assq spec specification)))
(error "Invalid format character: `%%%c'" spec)) (if (not val)
(setq val (cdr val)) (unless only-present
;; Pad result to desired length. (error "Invalid format character: `%%%c'" spec))
(let ((text (format (concat "%" num "s") val))) (setq val (cdr val))
;; Insert first, to preserve text properties. ;; Pad result to desired length.
(insert-and-inherit text) (let ((text (format (concat "%" num "s") val)))
;; Delete the specifier body. ;; Insert first, to preserve text properties.
(delete-region (+ (match-beginning 0) (length text)) (insert-and-inherit text)
(+ (match-end 0) (length text))) ;; Delete the specifier body.
;; Delete the percent sign. (delete-region (+ (match-beginning 0) (length text))
(delete-region (1- (match-beginning 0)) (match-beginning 0))))) (+ (match-end 0) (length text)))
;; Signal an error on bogus format strings. ;; Delete the percent sign.
(t (delete-region (1- (match-beginning 0)) (match-beginning 0))))))
(error "Invalid format string")))) ;; Signal an error on bogus format strings.
(t
(unless only-present
(error "Invalid format string")))))
(buffer-string))) (buffer-string)))
(defun format-spec-make (&rest pairs) (defun format-spec-make (&rest pairs)
......
...@@ -23,11 +23,21 @@ ...@@ -23,11 +23,21 @@
(require 'format-spec) (require 'format-spec)
(ert-deftest test-format-spec () (ert-deftest test-format-spec ()
(should (equal (format-spec "foo %b zot" '((?b . "bar"))) (should (equal (format-spec "foo %b zot" `((?b . "bar")))
"foo bar zot")) "foo bar zot"))
(should (equal (format-spec "foo %-10b zot" '((?b . "bar"))) (should (equal (format-spec "foo %-10b zot" '((?b . "bar")))
"foo bar zot")) "foo bar zot"))
(should (equal (format-spec "foo %10b zot" '((?b . "bar"))) (should (equal (format-spec "foo %10b zot" '((?b . "bar")))
"foo bar zot"))) "foo bar zot")))
(ert-deftest test-format-unknown ()
(should (eq (condition-case _
(format-spec "foo %b %z zot" '((?b . "bar")))
(error :error))
:error))
(should (equal (format-spec "foo %b %z zot" '((?b . "bar")) t)
"foo bar %z zot"))
(should (equal (format-spec "foo %b %z %% zot" '((?b . "bar")) t)
"foo bar %z %% zot")))
;;; format-spec-tests.el ends here ;;; format-spec-tests.el ends here
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment