3

Suppose, I have 2 subdirectories for yasnippets:

~/.emacs.d/yasnippets/perl-mode
~/.emacs.d/yasnippets/php-mode

Currently I use the following code in my .emacs:

(defvar *my-emacs-lib-dir* "~/.emacs.d/")
(load (concat *my-emacs-lib-dir* "plugins/yasnippet/yasnippet"))
(setq yas/snippet-dirs nil)
(yas/initialize)

;; Develop and keep personal snippets under ~/emacs.d/yasnippets
(setq yas/root-directory (concat *my-emacs-lib-dir* "yasnippets"))
(yas/load-directory yas/root-directory)

So, it loads all the yasnippets in all the subdirectories of ~/.emacs.d/yasnippets.

Is it possible to make it load the yasnippets on demand? If I open a php file, and the snippets for php-mode were not loaded, load them. But not load everything on startup.

user4035
  • 22,508
  • 11
  • 59
  • 94

2 Answers2

3

If I remember correctly, in fresh versions, the loading of snippets will performed on demand, if you'll use recommended loading sequence:

(add-to-list 'load-path "~/path-to-yasnippet")
(require 'yasnippet)
(yas-global-mode 1)

You can also use the optional use-jit flag to the yas-load-directory function, that will force on demand loading of snippets from this directory. See description of this function (C-h f yas-load-directory)

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
  • to load snippets from other mode you can `M-x yas-activate-extra-mode` as well. – DJJ Sep 27 '14 at 15:46
0

Maybe something like this can work.

(defvar yas/loaded-php-snippets nil)

(defun yas/load-php-snippets()
   (if (not yas/loaded-php-snippets)
    (progn
      (yas/load-directory  (concat yas/root-directory) "/php-mode")
      (setq yas/loaded-php-snippets t))))

(add-hook 'php-mode-hook 'yas/loaded-php-snippets)

This is just an example but one could conceivably have map between mode-hooks and yas load directories and just load specific directories if they are not yet been loaded.

aakarsh
  • 320
  • 1
  • 7