Question

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?

Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top