progfolio / doct

DOCT: Declarative Org Capture Templates for Emacs
GNU General Public License v3.0
380 stars 8 forks source link

declarative yasnippet templates #15

Closed Luis-Henriquez-Perez closed 4 years ago

Luis-Henriquez-Perez commented 4 years ago

I just wanted to mention that I really like this idea and I would love to see it applied to yasnippet templates. I don't like having snippets in a multiple separate files. And I don't like writing snippets in non lisp. Sometimes I end up with one snippet that's a huge long line.

Here's an example of what it might look like:

(define-snippet! help-function
  (:mode org-mode)
  (:key "hfn")
  (:body "[[helpfn:$1][%s]]$0")
  (:1 (yas-auto-next (yas-choose-value (void-list-symbols #'functionp)))))

Here's what the macro I wrote to do this looks like:

(defmacro define-snippet! (name &rest properties)
  "Define a yasnippet snippet.
NAME is the description of the snippet."
  (-let* (((&alist :mode :key :body) properties)
          (placeholders (thread-last properties
                          (-filter #'yasnippet--placeholder-p+)
                          (-map #'yasnippet--to-placeholder+))))
    `(after! yasnippet
       (with-temp-buffer
         (insert ,(apply #'format
                         (s-join "\n" (-snoc yasnippet-template+ body))
                         (append (list 'snippet-mode name key)
                                 placeholders)))
         (yas-load-snippet-buffer ',mode)))))

It uses the dash libraries and some helper functions/macros I wrote. I might end up writing a package for this I'm not sure yet. I just wanted to ask you if this is something that seems interesting to you.

progfolio commented 4 years ago

Thanks for sharing! It's an interesting idea. I think it's worth experimenting to see if the syntax ends up being easier to reason about and maintain. However, I don't use yasnippet as heavily as I do org-capture, so unfortunately I can't comment much on your implementation. Some general notes:

(defsnippet
  '(:name "help-function"
          :mode org-mode
          :keys "hfn"
          :body "[[helpfn:$1][%s]]$0"
          :1 (yas-auto-next (yas-choose-value (void-list-symbols #'functionp)))))

I encourage you to continue experimenting with it. If you do end up writing a package, make sure to keep me posted. I'll definitely look it over and offer up any advice I can.

Luis-Henriquez-Perez commented 4 years ago

I am very grateful for your advice and I will keep you posted. Thank you.