(isearch-complete1): Don't allocate unnecessarily.
[emacs.git] / lisp / gnus / format-spec.el
blob6cd39ede7214b5e94d1b6693947e599b3eaea42e
1 ;;; format-spec.el --- functions for formatting arbitrary formatting strings
2 ;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: tools
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (defun format-spec (format specification)
31 "Return a string based on FORMAT and SPECIFICATION.
32 FORMAT is a string containing `format'-like specs like \"bash %u %k\",
33 while SPECIFICATION is an alist mapping from format spec characters
34 to values."
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 (string-to-char (match-string 2)))
47 (val (cdr (assq spec specification))))
48 (delete-region (1- (match-beginning 0)) (match-end 0))
49 (unless val
50 (error "Invalid format character: %s" spec))
51 (insert (format (concat "%" num "s") val))))
52 ;; Signal an error on bogus format strings.
54 (error "Invalid format string"))))
55 (buffer-string)))
57 (defun format-spec-make (&rest pairs)
58 "Return an alist suitable for use in `format-spec' based on PAIRS.
59 PAIRS is a list where every other element is a character and a value,
60 starting with a character."
61 (let (alist)
62 (while pairs
63 (unless (cdr pairs)
64 (error "Invalid list of pairs"))
65 (push (cons (car pairs) (cadr pairs)) alist)
66 (setq pairs (cddr pairs)))
67 (nreverse alist)))
69 (provide 'format-spec)
71 ;;; format-spec.el ends here