Mercurial > emacs
annotate lisp/gnus/format-spec.el @ 90240:c30690dd6a7d
*** empty log message ***
author | Kenichi Handa <handa@m17n.org> |
---|---|
date | Fri, 14 Oct 2005 00:48:52 +0000 |
parents | 2d92f5c9d6ae |
children | 5e2d3828e89f |
rev | line source |
---|---|
31717 | 1 ;;; format-spec.el --- functions for formatting arbitrary formatting strings |
64754
fafd692d1e40
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
2 |
fafd692d1e40
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
3 ;; Copyright (C) 1999, 2000, 2002, 2003, 2004, |
fafd692d1e40
Update years in copyright notice; nfc.
Thien-Thi Nguyen <ttn@gnuvola.org>
parents:
64085
diff
changeset
|
4 ;; 2005 Free Software Foundation, Inc. |
31717 | 5 |
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> | |
7 ;; Keywords: tools | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
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) | |
14 ;; any later version. | |
15 | |
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. | |
20 | |
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 | |
64085 | 23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 ;; Boston, MA 02110-1301, USA. | |
31717 | 25 |
26 ;;; Commentary: | |
27 | |
28 ;;; Code: | |
29 | |
30 (eval-when-compile (require 'cl)) | |
31 | |
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 | |
36 to values." | |
37 (with-temp-buffer | |
38 (insert format) | |
39 (goto-char (point-min)) | |
40 (while (search-forward "%" nil t) | |
41 (cond | |
42 ;; Quoted percent sign. | |
43 ((eq (char-after) ?%) | |
44 (delete-char 1)) | |
45 ;; Valid format spec. | |
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)) | |
51 (unless val | |
52 (error "Invalid format character: %s" spec)) | |
53 (insert (format (concat "%" num "s") val)))) | |
54 ;; Signal an error on bogus format strings. | |
55 (t | |
56 (error "Invalid format string")))) | |
57 (buffer-string))) | |
58 | |
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." | |
63 (let (alist) | |
64 (while pairs | |
65 (unless (cdr pairs) | |
66 (error "Invalid list of pairs")) | |
67 (push (cons (car pairs) (cadr pairs)) alist) | |
68 (setq pairs (cddr pairs))) | |
69 (nreverse alist))) | |
70 | |
71 (provide 'format-spec) | |
72 | |
52401 | 73 ;;; arch-tag: c22d49cf-d167-445d-b7f1-2504d4173f53 |
31717 | 74 ;;; format-spec.el ends here |