aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2015-01-06 23:02:59 -0200
committerArtur Malabarba <bruce.connor.am@gmail.com>2015-01-07 15:48:31 -0200
commitf64d2922ed44dcacd3a0fd06eb494e94bd128bbe (patch)
treefe73c8704ada8c305e9222e5f76186247d6592c1
parent17d41cf40e36d0f0a7c48c60a7bf8880b659c438 (diff)
Define sx-format-replacements
Use FORMAT-STRING to format the values in ALIST.
-rw-r--r--sx.el24
1 files changed, 24 insertions, 0 deletions
diff --git a/sx.el b/sx.el
index e080271..6112510 100644
--- a/sx.el
+++ b/sx.el
@@ -337,6 +337,30 @@ removed from the display name before it is returned."
(format "[%s]" (car kar)) (cdr kar) string)))
string))
+(defun sx-format-replacements (format alist)
+ "Use FORMAT-STRING to format the values in ALIST.
+ALIST is a list with elements of the form (CHAR . STRING).
+The value is a copy of FORMAT-STRING, but with certain constructs
+replaced by text as given by ALIST.
+
+The construct is a `%' character followed by any other character.
+The replacement is the STRING corresponding to CHAR in ALIST.
+
+The %% construct is special, it is replaced with a single %, even
+if ALIST contains a different string at the ?% entry."
+ (let ((alist (cons '(?% . "%") alist)))
+ (with-temp-buffer
+ (insert format)
+ (goto-char (point-min))
+ (while (search-forward "%" nil 'noerror)
+ (delete-char -1)
+ (unless (eobp)
+ (insert
+ (or (cdr (assq (char-after) alist))
+ (error "ALIST has no value for `%c'" (char-after))))
+ (delete-char 1)))
+ (buffer-string))))
+
(defcustom sx-init-hook nil
"Hook run when SX initializes.