reddit / emacs

How to assign to variable value with environmental variable? (use-package)

rtwyyn · 2022-10-07 21:34
Guys, hi I have environmental variable CFG and usually to use it i do smng like this: (use-package fixed-pitch :demand t :load-path (lambda () (substitute-in-file-name "${CFG}/Emacs/fixed-pitch/")) ... ) But i can't make it work with yasnippet, instructions are: (setq yas-snippet-dirs '("~/.emacs.d/snippets" ;; personal snippets "/path/to/some/collection/" ;; foo-mode and bar-mode snippet collection "/path/to/yasnippet/yasmate/snippets" ;; the yasmate collection )) And i tried lots of things (commented out): (use-package yasnippet :ensure t :config (setq yas-snippet-dirs '(;; (substitute-in-file-name "${CFG}/Emacs/snippets/") ;; (lambda () (substitute-in-file-name "${CFG}/Emacs/snippets/")) ;; (concat (getenv "CFG") "/Emacs/snippets/") ;; (lambda () (concat (getenv "CFG") "/Emacs/snippets/")) "~/snippets/" ) ) (setq yas-indent-line 'fixed) (yas-global-mode 1) ) What's the right way to do it?
gopar · 2022-10-07 22:37
try: ``` (setq yas-snippet-dirs `(,(substitute-in-file-name "${CFG}/Emacs/snippets/"))) ;; So it would look like (setq yas-snippet-dirs `("~/.emacs.d/snippets" ;; personal snippets "/path/to/some/collection/" ;; foo-mode and bar-mode snippet collection "/path/to/yasnippet/yasmate/snippets" ;; the yasmate collection ,(substitute-in-file-name "${CFG}/Emacs/snippets/") )) ``` Ref: https://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html
eleven_cupfuls · 2022-10-08 17:24
When you quote the list `'(...` nothing inside it is evaluated, so the value is the literal thing that you typed, not the result of calling the function `substitute-in-file-name`. You can use backquote and unquote as u/gopar suggested, or you can create the list with the `list` function in an unquoted expression: (setq yas-snippet-dirs (list (substitute-in-file-name "${CFG}/Emacs/snippets") "~/snippets"))
rtwyyn · 2022-10-09 22:22
Thank you for your reply, it worked. One more question if you don't mind: if i want to pass only one value to setq, do i still have to use list? OR can i do just smng like: (setq yas-snippet-dirs substitute-in-file-name "${CFG}/Emacs/snippets") ?
eleven_cupfuls · 2022-10-10 18:45
This particular variable expects its value to be a list, so yes, you should always set it to a list. If you want to set something to a single string, you'd still need to *call* the `substitute-in-file-name` function (note the parens): (setq my-only-snippet-dir (substitute-in-file-name "${CFG}/Emacs/snippets"))
rtwyyn · 2022-10-14 21:30
got it, thx!