Search code examples
stringemacslispelispyasnippet

Emacs lisp: evaluating list of strings


I am new to elisp but I am trying to spice up my .emacs a little.

I am trying to define some paths, but having problems with creating a list of paths (and setting the list for YaSnippet more specifically).

When I evaluate the list I get a list of the symbol name (and not the symbol values as yassnippet want).

I got the code working but have feeling that there is a better way to do this?

Here is the working code:

;; some paths
(setq my-snippets-path "~/.emacs.d/snippets")
(setq default-snippets-path "~/.emacs.d/site-lisp/yasnippet/snippets")

;; set the yas/root-directory to a list of the paths
(setq yas/root-directory `(,my-snippets-path ,default-snippets-path))

;; load the directories
(mapc 'yas/load-directory yas/root-directory)

Solution

  • If you evaluate a list of strings, the result depends on the value of the list items. The best way to test that is to launch the ielm repl (M-x ielm), and enter:

    ELISP> '("abc" "def" "ghi")
    ("abc" "def" "ghi")
    

    The quoted list of string evaluates to the list value. If you store the value of the list in a variable, and then evaluate the variable, ELisp will complain that the function abc is unknown.

    ELISP> (setq my-list '("abc" "def" "ghi"))
    ("abc" "def" "ghi")
    
    ELISP> (eval my-list)
    *** Eval error ***  Invalid function: "abc"
    

    For the yasnippet directory configuration, you should just set yas-snippet-dir instead, e.g.

    (add-to-list 'load-path
                  "~/.emacs.d/plugins/yasnippet")
    (require 'yasnippet)
    
    (setq yas-snippet-dirs
          '("~/.emacs.d/snippets"            ;; personal snippets
            "/path/to/yasnippet/snippets"    ;; the default collection
            "/path/to/other/snippets"        ;; add any other folder with a snippet collection
            ))
    
    (yas-global-mode 1)
    

    Edit:
    The use of yas/root-directory has been deprecated. From the documentation of yasnippet.el

    `yas-snippet-dirs'
    
    The directory where user-created snippets are to be
    stored. Can also be a list of directories. In that case,
    when used for bulk (re)loading of snippets (at startup or
    via `yas-reload-all'), directories appearing earlier in
    the list shadow other dir's snippets. Also, the first
    directory is taken as the default for storing the user's
    new snippets.
    
    The deprecated `yas/root-directory' aliases this variable
    for backward-compatibility.