Remove lisp/tests/mml-gpghome/random_seed from version control
[gnus.git] / lisp / format-spec.el
blob39ea2710b89ab51e5f877232d4e40e0b938db4e8
1 ;;; format-spec.el --- functions for formatting arbitrary formatting strings
3 ;; Copyright (C) 1999-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: tools
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (eval-when-compile (require 'cl))
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 (with-temp-buffer
36 (insert format)
37 (goto-char (point-min))
38 (while (search-forward "%" nil t)
39 (cond
40 ;; Quoted percent sign.
41 ((eq (char-after) ?%)
42 (delete-char 1))
43 ;; Valid format spec.
44 ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
45 (let* ((num (match-string 1))
46 (spec (if (featurep 'xemacs)
47 (char-to-int (string-to-char (match-string 2)))
48 (string-to-char (match-string 2))))
49 (val (assq spec specification)))
50 (unless val
51 (error "Invalid format character: `%%%c'" spec))
52 (setq val (cdr val))
53 ;; Pad result to desired length.
54 (let ((text (format (concat "%" num "s") val)))
55 ;; Insert first, to preserve text properties.
56 (insert-and-inherit text)
57 ;; Delete the specifier body.
58 (delete-region (+ (match-beginning 0) (length text))
59 (+ (match-end 0) (length text)))
60 ;; Delete the percent sign.
61 (delete-region (1- (match-beginning 0)) (match-beginning 0)))))
62 ;; Signal an error on bogus format strings.
64 (error "Invalid format string"))))
65 (buffer-string)))
67 (defun format-spec-make (&rest pairs)
68 "Return an alist suitable for use in `format-spec' based on PAIRS.
69 PAIRS is a list where every other element is a character and a value,
70 starting with a character."
71 (let (alist)
72 (while pairs
73 (unless (cdr pairs)
74 (error "Invalid list of pairs"))
75 (push (cons (if (featurep 'xemacs)
76 (if (characterp (car pairs))
77 (char-to-int (car pairs))
78 (car pairs))
79 (car pairs))
80 (cadr pairs))
81 alist)
82 (setq pairs (cddr pairs)))
83 (nreverse alist)))
85 (provide 'format-spec)
87 ;;; format-spec.el ends here