3

I am doing some Drupal programming and have created yasnippets for common functions: db_select, db_delete etc.

I want to load them only when I am working on a Drupal file and not for a common php file. The task is this:

given a list of yasnippet files and the path of the currently opening file

if(path contains '/sites/all/modules')
{
    load all the snippets from the list
}

How to do it?

user4035
  • 22,508
  • 11
  • 59
  • 94
  • why? what's a couple of snippet files going to change? – event_jr Sep 21 '12 at 10:05
  • 1
    I will reduce the number of snippets in list for php. Which is too huge. – user4035 Sep 21 '12 at 12:10
  • If you use a specific mode for drupal and another for php I guess you could have it so that drupal mode inherits all the snippets from php but not the other way around. That way you can have the additional snippets only in drupal mode. – N.N. Sep 21 '12 at 12:45
  • That would work but you still have to load the appropriate mode and that would depend on which directory/project you're in. –  Sep 27 '12 at 17:19
  • You may be interested in this: http://drupal.org/project/emacs – phils Sep 27 '12 at 23:58
  • "You may be interested in this: drupal.org/project/emacs" - yes, very good project. – user4035 Sep 28 '12 at 01:29

1 Answers1

2

You have two options:

  1. Create a new mode called drupal-mode that inherits everything from php-mode and then define snippets for that mode, check out Derived Modes in the Emacs Manual on how to do that
  2. Add a function to the find-file hook that checks to see if the current buffer is in a particular directory and then load the snippets you want

Either way you'll be doing things based on the current directory or filename.

find-file-hook example

I haven't tested this code but the concept is there. You'll probably have to check the yas/load-directory-1 function to something else or mess around with parameters.

(defun load-drupal-snippets ()
  (when (string-match-p "/sites/all/modules" (file-name-directory (buffer-file-name)))
    (yas--load-directory-1 "/path/to/drupal/snippets/" 'php-mode 'text-mode)))

(add-hook 'find-file-hook 'load-drupal-snippets)
BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91