12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- (eval-when-compile (require 'cl))
- (defun format-spec (format specification)
- "Return a string based on FORMAT and SPECIFICATION.
- FORMAT is a string containing `format'-like specs like \"bash %u %k\",
- while SPECIFICATION is an alist mapping from format spec characters
- to values. Any text properties on a %-spec itself are propagated to
- the text that it generates."
- (with-temp-buffer
- (insert format)
- (goto-char (point-min))
- (while (search-forward "%" nil t)
- (cond
-
- ((eq (char-after) ?%)
- (delete-char 1))
-
- ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
- (let* ((num (match-string 1))
- (spec (string-to-char (match-string 2)))
- (val (cdr (assq spec specification))))
- (unless val
- (error "Invalid format character: `%%%c'" spec))
-
- (let ((text (format (concat "%" num "s") val)))
-
- (insert-and-inherit text)
-
- (delete-region (+ (match-beginning 0) (length text))
- (+ (match-end 0) (length text)))
-
- (delete-region (1- (match-beginning 0)) (match-beginning 0)))))
-
- (t
- (error "Invalid format string"))))
- (buffer-string)))
- (defun format-spec-make (&rest pairs)
- "Return an alist suitable for use in `format-spec' based on PAIRS.
- PAIRS is a list where every other element is a character and a value,
- starting with a character."
- (let (alist)
- (while pairs
- (unless (cdr pairs)
- (error "Invalid list of pairs"))
- (push (cons (car pairs) (cadr pairs)) alist)
- (setq pairs (cddr pairs)))
- (nreverse alist)))
- (provide 'format-spec)
|