Line data Source code
1 : ;;; format-spec.el --- functions for formatting arbitrary formatting strings
2 :
3 : ;; Copyright (C) 1999-2017 Free Software Foundation, Inc.
4 :
5 : ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 : ;; Keywords: tools
7 :
8 : ;; This file is part of GNU Emacs.
9 :
10 : ;; GNU Emacs is free software: you can redistribute it and/or modify
11 : ;; it under the terms of the GNU General Public License as published by
12 : ;; the Free Software Foundation, either version 3 of the License, or
13 : ;; (at your option) any later version.
14 :
15 : ;; GNU Emacs is distributed in the hope that it will be useful,
16 : ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : ;; GNU General Public License for more details.
19 :
20 : ;; You should have received a copy of the GNU General Public License
21 : ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 :
23 : ;;; Commentary:
24 :
25 : ;;; Code:
26 :
27 : (eval-when-compile (require 'cl))
28 :
29 : (defun format-spec (format specification)
30 : "Return a string based on FORMAT and SPECIFICATION.
31 : FORMAT is a string containing `format'-like specs like \"bash %u %k\",
32 : while SPECIFICATION is an alist mapping from format spec characters
33 : to values. Any text properties on a %-spec itself are propagated to
34 : the text that it generates."
35 142 : (with-temp-buffer
36 142 : (insert format)
37 142 : (goto-char (point-min))
38 142 : (while (search-forward "%" nil t)
39 0 : (cond
40 : ;; Quoted percent sign.
41 0 : ((eq (char-after) ?%)
42 0 : (delete-char 1))
43 : ;; Valid format spec.
44 0 : ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
45 0 : (let* ((num (match-string 1))
46 0 : (spec (string-to-char (match-string 2)))
47 0 : (val (assq spec specification)))
48 0 : (unless val
49 0 : (error "Invalid format character: `%%%c'" spec))
50 0 : (setq val (cdr val))
51 : ;; Pad result to desired length.
52 0 : (let ((text (format (concat "%" num "s") val)))
53 : ;; Insert first, to preserve text properties.
54 0 : (insert-and-inherit text)
55 : ;; Delete the specifier body.
56 0 : (delete-region (+ (match-beginning 0) (length text))
57 0 : (+ (match-end 0) (length text)))
58 : ;; Delete the percent sign.
59 0 : (delete-region (1- (match-beginning 0)) (match-beginning 0)))))
60 : ;; Signal an error on bogus format strings.
61 : (t
62 142 : (error "Invalid format string"))))
63 142 : (buffer-string)))
64 :
65 : (defun format-spec-make (&rest pairs)
66 : "Return an alist suitable for use in `format-spec' based on PAIRS.
67 : PAIRS is a list where every other element is a character and a value,
68 : starting with a character."
69 142 : (let (alist)
70 497 : (while pairs
71 355 : (unless (cdr pairs)
72 355 : (error "Invalid list of pairs"))
73 710 : (push (cons (car pairs) (cadr pairs)) alist)
74 355 : (setq pairs (cddr pairs)))
75 142 : (nreverse alist)))
76 :
77 : (provide 'format-spec)
78 :
79 : ;;; format-spec.el ends here
|