+(defun yas/load-directory-1 (directory &optional parent)
+ "Really do the job of loading snippets from a directory
+hierarchy."
+ (let ((mode-sym (intern (file-name-nondirectory directory)))
+ (snippets nil))
+ (with-temp-buffer
+ (dolist (file (yas/directory-files directory t))
+ (when (file-readable-p file)
+ (insert-file-contents file nil nil nil t)
+ (push (cons (file-name-nondirectory file)
+ (yas/parse-template))
+ snippets))))
+ (yas/define-snippets mode-sym
+ snippets
+ parent)
+ (dolist (subdir (yas/directory-files directory nil))
+ (yas/load-directory-1 subdir mode-sym))))
+
+(defun yas/quote-string (string)
+ "Escape and quote STRING.
+foo\"bar\\! -> \"foo\\\"bar\\\\!\""
+ (concat "\""
+ (replace-regexp-in-string "[\\\"]"
+ "\\\\\\&"
+ string
+ t)
+ "\""))
+
+(defun yas/compile-bundle (yasnippet yasnippet-bundle snippet-roots)
+ "Compile snippets in SNIPPET-ROOTS to a single bundle file.
+SNIPPET-ROOTS is a list of root directories that contains the snippets
+definition. YASNIPPET is the yasnippet.el file path. YASNIPPET-BUNDLE
+is the output file of the compile result. Here's an example:
+
+ (yas/compile-bundle \"~/.emacs.d/plugins/yasnippet/yasnippet.el\"
+ \"~/.emacs.d/plugins/yasnippet-bundle.el\"
+ '(\"~/.emacs.d/plugins/yasnippet/snippets\"))"
+ (let ((dirs (or (and (listp snippet-roots) snippet-roots)
+ (list snippet-roots)))
+ (bundle-buffer nil))
+ (with-temp-buffer
+ (setq bundle-buffer (current-buffer))
+ (insert-file-contents yasnippet)
+ (goto-char (point-max))
+ (insert ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n")
+ (insert ";;;; Auto-generated code ;;;;\n")
+ (insert ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\n")
+ (insert "(yas/initialize)\n")
+ (flet ((yas/define-snippets
+ (mode snippets &optional parent)
+ (with-current-buffer bundle-buffer
+ (insert ";;; snippets for " (symbol-name mode) "\n")
+ (insert "(yas/define-snippets '" (symbol-name mode) "\n")
+ (insert "'(\n")
+ (dolist (snippet snippets)
+ (insert " ("
+ (yas/quote-string (car snippet))
+ (yas/quote-string (cadr snippet))
+ (if (caddr snippet)
+ (yas/quote-string (caddr snippet))
+ "nil")
+ ")\n"))
+ (insert " )\n")
+ (insert (if parent
+ (concat "'" (symbol-name parent))
+ "nil")
+ ")\n\n"))))
+ (dolist (dir dirs)
+ (dolist (subdir (yas/directory-files dir nil))
+ (yas/load-directory-1 subdir nil))))
+ (insert "(provide '"
+ (file-name-nondirectory
+ (file-name-sans-extension
+ yasnippet-bundle))
+ ")\n")
+ (setq buffer-file-name yasnippet-bundle)
+ (save-buffer))))
+