1 ;;; format-spec.el --- functions for formatting arbitrary formatting strings
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 (eval-when-compile (require 'cl))
32 (defun format-spec (format specification)
33 "Return a string based on FORMAT and SPECIFICATION.
34 FORMAT is a string containing `format'-like specs like \"bash %u %k\",
35 while SPECIFICATION is an alist mapping from format spec characters
39 (goto-char (point-min))
40 (while (search-forward "%" nil t)
42 ;; Quoted percent sign.
46 ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
47 (let* ((num (match-string 1))
48 (spec (string-to-char (match-string 2)))
49 (val (cdr (assq spec specification))))
50 (delete-region (1- (match-beginning 0)) (match-end 0))
52 (error "Invalid format character: `%%%c'" spec))
53 (insert (format (concat "%" num "s") val))))
54 ;; Signal an error on bogus format strings.
56 (error "Invalid format string"))))
59 (defun format-spec-make (&rest pairs)
60 "Return an alist suitable for use in `format-spec' based on PAIRS.
61 PAIRS is a list where every other element is a character and a value,
62 starting with a character."
66 (error "Invalid list of pairs"))
67 (push (cons (car pairs) (cadr pairs)) alist)
68 (setq pairs (cddr pairs)))
71 (provide 'format-spec)
73 ;;; format-spec.el ends here