format-spec.el 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; format-spec.el --- functions for formatting arbitrary formatting strings
  2. ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: tools
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (defun format-spec (format specification)
  20. "Return a string based on FORMAT and SPECIFICATION.
  21. FORMAT is a string containing `format'-like specs like \"bash %u %k\",
  22. while SPECIFICATION is an alist mapping from format spec characters
  23. to values. Any text properties on a %-spec itself are propagated to
  24. the text that it generates."
  25. (with-temp-buffer
  26. (insert format)
  27. (goto-char (point-min))
  28. (while (search-forward "%" nil t)
  29. (cond
  30. ;; Quoted percent sign.
  31. ((eq (char-after) ?%)
  32. (delete-char 1))
  33. ;; Valid format spec.
  34. ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
  35. (let* ((num (match-string 1))
  36. (spec (string-to-char (match-string 2)))
  37. (val (cdr (assq spec specification))))
  38. (unless val
  39. (error "Invalid format character: `%%%c'" spec))
  40. ;; Pad result to desired length.
  41. (let ((text (format (concat "%" num "s") val)))
  42. ;; Insert first, to preserve text properties.
  43. (insert-and-inherit text)
  44. ;; Delete the specifier body.
  45. (delete-region (+ (match-beginning 0) (length text))
  46. (+ (match-end 0) (length text)))
  47. ;; Delete the percent sign.
  48. (delete-region (1- (match-beginning 0)) (match-beginning 0)))))
  49. ;; Signal an error on bogus format strings.
  50. (t
  51. (error "Invalid format string"))))
  52. (buffer-string)))
  53. (defun format-spec-make (&rest pairs)
  54. "Return an alist suitable for use in `format-spec' based on PAIRS.
  55. PAIRS is a list where every other element is a character and a value,
  56. starting with a character."
  57. (let (alist)
  58. (while pairs
  59. (unless (cdr pairs)
  60. (error "Invalid list of pairs"))
  61. (push (cons (car pairs) (cadr pairs)) alist)
  62. (setq pairs (cddr pairs)))
  63. (nreverse alist)))
  64. (provide 'format-spec)
  65. ;;; format-spec.el ends here