How to expand yasnippet snippet inside html file

513 views Asked by At

I have a lisp script that creates an empty html file:

(let ((mode "html-mode"))

  (funcall (intern mode)))

(write-region "" nil "index.html")

Then i'm using yasnippet to generate the basic html file: I have a snippet named "base" (i hit TAB key to expand it)

Is there a way to use this snippet in my lisp script?

i try, with no succes to use (yas/expand-snippet base)

Thanks.

Goulven.

EDIT

Using the code of abo-abo, I got something that works well:

(defun create-web-site-test()
  (interactive)
  (setq msg (concatenate 'string "Create web site in : " default-directory))
  (if (y-or-n-p msg)
    (progn  
     (write-region "" nil "./index.html")
     (find-file "./index.html")
     (html-mode)    
     (insert "\nbase")
     (yas/expand)
     (save-buffer)
     (kill-buffer))
    (progn
      ;; Give up
      (message "Ok, nothing will be donne, bybye...")
      )))

I just need to set the current directory to the right place using Mx cd. There is probably a more effective solution, without opening the file in a buffer. But this one is already pretty cool. Thanks abo-abo

1

There are 1 answers

3
abo-abo On BEST ANSWER

This should work:

(progn
  (html-mode)
  (insert-file-contents "~/index.html")
  (insert "\nbase")
  (yas/expand))

And please do try to read the elisp manual. It's very good.

UPD

I've updated the function. I see that you're new. To get a good answer you need to state clearly what you want to do. And if this answer is useful, don't forget to up-vote and accept.

Also, some comments on your code: concat should be used for strings, instead of concatenate. And it's best not to use y-or-n-p - just do what you want to do, and if you want to abort, use C-g.

(defun create-test (directory)
  (interactive "D")
  (with-current-buffer (find-file-noselect 
                        (concat directory "index.html"))
    (delete-region (point-min) (point-max))
    (html-mode)
    (insert "base")
    (yas/expand)
    (save-buffer)
    (kill-buffer)))