I've made a little function for myself that replaces certain variables to text in an org file. I declare a bunch of variables in the beginning of the file and then the function replaces all references to the variable with the value. So for example
#+VAR:location=AAAAAA
text text text \v{location} text text text
becomes
text text text AAAAAA text text text
It's my first time really playing with elisp and I'm wondering if I used the language correctly and if there's possibility to improvement.
(defun mp/org-fill-variables ()
"Fill in the variables"
(interactive)
(let ((variable-count 0))
(goto-char 0)
(while (search-forward-regexp "^#\\+VAR:" nil t)
(set-mark-command nil)
(when (search-forward "=")
(backward-char)
(let ((variable (buffer-substring (region-beginning) (region-end))))
(forward-char)
(set-mark-command nil)
(end-of-line)
(let ((value (buffer-substring (region-beginning) (region-end))))
;; todo check if value non-nil
(replace-string-in-region (concat "\\v{" variable "}") value)
(setq variable-count (1+ variable-count))))))
(message "replaced %d variables" variable-count)))