Add helper function to replace variables in strings

Only a thin but hopefully helpful wrapper around `replace-regexp-in-string`.
This commit is contained in:
Daniel - 2022-06-13 21:15:49 +02:00
parent 1ac4e44f86
commit e2181fa87a
No known key found for this signature in database
GPG Key ID: 784AA8DF0CCDF625
2 changed files with 15 additions and 1 deletions

View File

@ -628,7 +628,8 @@
keyboard-quit-context+
db/convert-lf-to-crlf-in-buffer
db/convert-crlf-to-lf-in-buffer
db/sync-magit-repos-from-projectile))
db/sync-magit-repos-from-projectile
db/replace-variables-in-string))
(use-package db-hydras
:commands (hydra-toggle/body

View File

@ -464,6 +464,19 @@ Does not replace CRLF with CRCRLF, and so on."
(shr-render-buffer (find-file-noselect file))
(delete-trailing-whitespace))
(defun db/replace-variables-in-string (string var-map)
"Replace variables in STRING as per VAR-MAP.
VAR-MAP is an alist mapping variable names (strings or symbols)
to values. Variables are strings of alphabetic characters (no
numbers allowed)."
(replace-regexp-in-string "[[:alpha:]]+"
#'(lambda (var)
(format "%s" (alist-get var var-map
var ; default value
nil ; not relevant REMOVE parameter
#'string=)))
string))
;;; Base45 Decoding