1 ;;; format-spec.el --- functions for formatting arbitrary formatting strings
2 ;; Copyright (C) 1999 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
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)
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.
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
37 (goto-char (point-min))
38 (while (search-forward "%" nil t)
40 ;; Quoted percent sign.
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))
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-substring (point-min) (point-max))))
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."
64 (error "Invalid list of pairs"))
65 (push (cons (car pairs) (cadr pairs)) alist)
66 (setq pairs (cddr pairs)))
69 (provide 'format-spec)
71 ;;; format-spec.el ends here