mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-13 21:13:04 +00:00
yas/compile-directory: now works :-)
This commit is contained in:
parent
9dedf91209
commit
0bdb370c06
142
yasnippet.el
142
yasnippet.el
@ -1604,15 +1604,46 @@ TEMPLATES is a list of `yas/template'."
|
|||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Loading snippets from files
|
;; Loading snippets from files
|
||||||
;;
|
;;
|
||||||
(defun yas/load-directory-1 (directory mode-sym &optional no-compiled-snippets)
|
(defun yas/load-yas-setup-file (file)
|
||||||
|
(load file 'noerror))
|
||||||
|
|
||||||
|
(defun yas/load-directory (top-level-dir &optional nojit)
|
||||||
|
"Load snippet definition from directory hierarchy under TOP-LEVEL-DIR.
|
||||||
|
|
||||||
|
Below TOP-LEVEL-DIR each directory is a mode name."
|
||||||
|
(interactive "DSelect the root directory: ")
|
||||||
|
(unless (file-directory-p top-level-dir)
|
||||||
|
(error "%s is not a directory" top-level-dir))
|
||||||
|
(unless yas/snippet-dirs
|
||||||
|
(setq yas/snippet-dirs top-level-dir))
|
||||||
|
(dolist (dir (yas/subdirs top-level-dir))
|
||||||
|
(let* ((major-mode-and-parents (yas/compute-major-mode-and-parents
|
||||||
|
(concat dir "/dummy")))
|
||||||
|
(mode-sym (car major-mode-and-parents))
|
||||||
|
(parents (cdr major-mode-and-parents)))
|
||||||
|
(let ((form `(yas/load-directory-1 ,dir
|
||||||
|
',mode-sym
|
||||||
|
',parents)))
|
||||||
|
(if (or (called-interactively-p)
|
||||||
|
nojit)
|
||||||
|
(eval form)
|
||||||
|
(yas/schedule-jit mode-sym form)))))
|
||||||
|
(when (interactive-p)
|
||||||
|
(yas/message 3 "Loaded snippets from %s." top-level-dir)))
|
||||||
|
|
||||||
|
(defun yas/load-directory-1 (directory mode-sym parents &optional no-compiled-snippets)
|
||||||
"Recursively load snippet templates from DIRECTORY."
|
"Recursively load snippet templates from DIRECTORY."
|
||||||
(unless (file-exists-p (concat directory "/" ".yas-skip"))
|
(unless (file-exists-p (concat directory "/" ".yas-skip"))
|
||||||
;; Load .yas-setup.el files wherever we find them
|
|
||||||
;;
|
|
||||||
(load (expand-file-name ".yas-setup" directory) 'noerror)
|
|
||||||
(if (and (not no-compiled-snippets)
|
(if (and (not no-compiled-snippets)
|
||||||
(load (expand-file-name ".yas-compiled-snippets" directory) 'noerror))
|
(load (expand-file-name ".yas-compiled-snippets" directory) 'noerror))
|
||||||
(message "[yas] Loading much faster .yas-compiled-snippets from %s" directory)
|
(message "[yas] Loading much faster .yas-compiled-snippets from %s" directory)
|
||||||
|
(yas/load-directory-2 directory mode-sym parents))))
|
||||||
|
|
||||||
|
(defun yas/load-directory-2 (directory mode-sym parents)
|
||||||
|
(yas/define-parents mode-sym parents)
|
||||||
|
;; Load .yas-setup.el files wherever we find them
|
||||||
|
;;
|
||||||
|
(yas/load-yas-setup-file (expand-file-name ".yas-setup" directory))
|
||||||
(let* ((default-directory directory)
|
(let* ((default-directory directory)
|
||||||
(snippet-defs nil))
|
(snippet-defs nil))
|
||||||
;; load the snippet files
|
;; load the snippet files
|
||||||
@ -1629,35 +1660,11 @@ TEMPLATES is a list of `yas/template'."
|
|||||||
;; now recurse to a lower level
|
;; now recurse to a lower level
|
||||||
;;
|
;;
|
||||||
(dolist (subdir (yas/subdirs directory))
|
(dolist (subdir (yas/subdirs directory))
|
||||||
(yas/load-directory-1 subdir
|
(yas/load-directory-2 subdir
|
||||||
mode-sym
|
mode-sym
|
||||||
t))))))
|
nil))))
|
||||||
|
|
||||||
(defun yas/load-directory (top-level-dir)
|
(defun yas/load-snippet-dirs (&optional nojit)
|
||||||
"Load snippet definition from directory hierarchy under TOP-LEVEL-DIR.
|
|
||||||
|
|
||||||
Below TOP-LEVEL-DIR., each directory is a mode name."
|
|
||||||
(interactive "DSelect the root directory: ")
|
|
||||||
(unless (file-directory-p top-level-dir)
|
|
||||||
(error "%s is not a directory" top-level-dir))
|
|
||||||
(unless yas/snippet-dirs
|
|
||||||
(setq yas/snippet-dirs top-level-dir))
|
|
||||||
(dolist (dir (yas/subdirs top-level-dir))
|
|
||||||
(let* ((major-mode-and-parents (yas/compute-major-mode-and-parents
|
|
||||||
(concat dir "/dummy")))
|
|
||||||
(mode-sym (car major-mode-and-parents))
|
|
||||||
(parents (cdr major-mode-and-parents)))
|
|
||||||
(puthash mode-sym (remove-duplicates
|
|
||||||
(append parents
|
|
||||||
(gethash mode-sym yas/parents)))
|
|
||||||
yas/parents)
|
|
||||||
(yas/schedule-jit mode-sym
|
|
||||||
`(yas/load-directory-1 ,dir
|
|
||||||
',mode-sym))))
|
|
||||||
(when (interactive-p)
|
|
||||||
(yas/message 3 "Loaded snippets from %s." top-level-dir)))
|
|
||||||
|
|
||||||
(defun yas/load-snippet-dirs ()
|
|
||||||
"Reload the directories listed in `yas/snippet-dirs' or
|
"Reload the directories listed in `yas/snippet-dirs' or
|
||||||
prompt the user to select one."
|
prompt the user to select one."
|
||||||
(let (errors)
|
(let (errors)
|
||||||
@ -1665,14 +1672,14 @@ Below TOP-LEVEL-DIR., each directory is a mode name."
|
|||||||
(dolist (directory (reverse (yas/snippet-dirs)))
|
(dolist (directory (reverse (yas/snippet-dirs)))
|
||||||
(condition-case oops
|
(condition-case oops
|
||||||
(progn
|
(progn
|
||||||
(yas/load-directory directory)
|
(yas/load-directory directory nojit)
|
||||||
(yas/message 3 "Loaded %s" directory))
|
(yas/message 3 "Loaded %s" directory))
|
||||||
(error (push oops errors)
|
(error (push oops errors)
|
||||||
(yas/message 3 "Check your `yas/snippet-dirs': %s" (second oops)))))
|
(yas/message 3 "Check your `yas/snippet-dirs': %s" (second oops)))))
|
||||||
(call-interactively 'yas/load-directory))
|
(call-interactively 'yas/load-directory))
|
||||||
errors))
|
errors))
|
||||||
|
|
||||||
(defun yas/reload-all (&optional with-jit)
|
(defun yas/reload-all (&optional nojit)
|
||||||
"Reload all snippets and rebuild the YASnippet menu. "
|
"Reload all snippets and rebuild the YASnippet menu. "
|
||||||
(interactive "p")
|
(interactive "p")
|
||||||
(let ((errors))
|
(let ((errors))
|
||||||
@ -1695,7 +1702,7 @@ Below TOP-LEVEL-DIR., each directory is a mode name."
|
|||||||
;; Reload the directories listed in `yas/snippet-dirs' or prompt
|
;; Reload the directories listed in `yas/snippet-dirs' or prompt
|
||||||
;; the user to select one.
|
;; the user to select one.
|
||||||
;;
|
;;
|
||||||
(setq errors (yas/load-snippet-dirs))
|
(setq errors (yas/load-snippet-dirs nojit))
|
||||||
;; Reload the direct keybindings
|
;; Reload the direct keybindings
|
||||||
;;
|
;;
|
||||||
(yas/direct-keymaps-reload)
|
(yas/direct-keymaps-reload)
|
||||||
@ -1717,30 +1724,25 @@ foo\"bar\\! -> \"foo\\\"bar\\\\!\""
|
|||||||
"For backward compatibility, enable `yas/minor-mode' globally"
|
"For backward compatibility, enable `yas/minor-mode' globally"
|
||||||
(yas/global-mode 1))
|
(yas/global-mode 1))
|
||||||
|
|
||||||
(defun yas/compile-top-level-dir (top-level-dir)
|
(defun yas/compile-directory (top-level-dir)
|
||||||
"Create .yas-compiled-snippets.el files under subdirs of TOP-LEVEL-DIR."
|
"Create .yas-compiled-snippets.el files under subdirs of TOP-LEVEL-DIR.
|
||||||
|
|
||||||
|
This works by stubbing a few functions, then calling
|
||||||
|
`yas/load-directory'."
|
||||||
(interactive "DTop level snippet directory?")
|
(interactive "DTop level snippet directory?")
|
||||||
(dolist (dir (yas/subdirs top-level-dir))
|
(flet ((yas/load-yas-setup-file
|
||||||
(yas/compile-snippets dir)))
|
(file)
|
||||||
|
(let ((elfile (concat file ".el")))
|
||||||
(defun yas/compile-snippets (input-dir &optional output-file)
|
(when (file-exists-p elfile)
|
||||||
"Compile snippets files in INPUT-DIR to OUTPUT-FILE file.
|
(insert ";;; .yas-setup.el support file if any:\n;;;\n")
|
||||||
|
(insert-file-contents elfile))))
|
||||||
Prompts for INPUT-DIR and OUTPUT-FILE if called-interactively"
|
(yas/define-parents
|
||||||
(interactive (let* ((input-dir (read-directory-name "Snippet dir "))
|
(mode parents)
|
||||||
(output-file (let ((ido-everywhere nil))
|
(insert ";;; Parent definitions:\n;;;\n")
|
||||||
(read-file-name "Output file "
|
(insert (pp-to-string `(yas/define-parents ',mode ',parents))))
|
||||||
input-dir nil nil
|
(yas/define-snippets
|
||||||
".yas-compiled-snippets.el"
|
|
||||||
nil))))
|
|
||||||
(list input-dir output-file)))
|
|
||||||
(let ((default-directory input-dir))
|
|
||||||
(with-temp-file (setq output-file (or output-file ".yas-compiled-snippets.el"))
|
|
||||||
(flet ((yas/define-snippets
|
|
||||||
(mode snippets)
|
(mode snippets)
|
||||||
(insert (format ";;; %s - automatically compiled snippets for `%s' , do not edit!\n"
|
(insert ";;; Snippet definitions:\n;;;\n")
|
||||||
(file-name-nondirectory output-file) mode))
|
|
||||||
(insert ";;;\n")
|
|
||||||
(let ((literal-snippets (list)))
|
(let ((literal-snippets (list)))
|
||||||
(dolist (snippet snippets)
|
(dolist (snippet snippets)
|
||||||
(let ((key (first snippet))
|
(let ((key (first snippet))
|
||||||
@ -1762,17 +1764,16 @@ Prompts for INPUT-DIR and OUTPUT-FILE if called-interactively"
|
|||||||
,binding
|
,binding
|
||||||
,uuid)
|
,uuid)
|
||||||
literal-snippets)))
|
literal-snippets)))
|
||||||
(insert (pp-to-string `(yas/define-snippets ',mode ',literal-snippets ',parent-or-parents)))
|
(insert (pp-to-string `(yas/define-snippets ',mode ',literal-snippets)))
|
||||||
(insert "\n\n")
|
(insert "\n\n")))
|
||||||
(insert (format ";;; %s - automatically compiled snippets for `%s' end here\n"
|
(yas/load-directory-1
|
||||||
(file-name-nondirectory output-file) mode))
|
(dir mode parents &rest ignore)
|
||||||
(insert ";;;"))))
|
(let ((output-file (concat (file-name-as-directory dir) ".yas-compiled-snippets.el")))
|
||||||
(yas/load-directory-1 input-dir nil 'no-compiled-snippets))))
|
(with-temp-file output-file
|
||||||
|
(insert (format ";;; Compiled snippets and support files for `%s'\n" mode))
|
||||||
(if (and (called-interactively-p)
|
(yas/load-directory-2 dir mode parents)
|
||||||
(yes-or-no-p (format "Open the resulting file (%s)? "
|
(insert (format ";;; Do not edit! File generated at %s\n" (current-time-string)))))))
|
||||||
(expand-file-name output-file))))
|
(yas/load-directory top-level-dir 'im-compiling-so-no-jit-ok?)))
|
||||||
(find-file-other-window output-file)))
|
|
||||||
|
|
||||||
|
|
||||||
;;; JIT loading
|
;;; JIT loading
|
||||||
@ -1798,6 +1799,13 @@ Prompts for INPUT-DIR and OUTPUT-FILE if called-interactively"
|
|||||||
yas/version
|
yas/version
|
||||||
") -- pluskid <pluskid@gmail.com>/joaotavora <joaotavora@gmail.com>")))
|
") -- pluskid <pluskid@gmail.com>/joaotavora <joaotavora@gmail.com>")))
|
||||||
|
|
||||||
|
(defun yas/define-parents (mode parents)
|
||||||
|
"Add PARENTS to the list of MODE's parents"
|
||||||
|
(puthash mode-sym (remove-duplicates
|
||||||
|
(append parents
|
||||||
|
(gethash mode-sym yas/parents)))
|
||||||
|
yas/parents))
|
||||||
|
|
||||||
(defun yas/define-snippets (mode snippets)
|
(defun yas/define-snippets (mode snippets)
|
||||||
"Define SNIPPETS for MODE.
|
"Define SNIPPETS for MODE.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user