feature/file-templates: rewrite & fix wrong-number-of-args errors #602
This commit is contained in:
parent
4c73ac0111
commit
b806ff937c
2 changed files with 113 additions and 114 deletions
|
@ -66,8 +66,12 @@ evil is loaded and enabled)."
|
|||
;;;###autoload
|
||||
(defun +file-templates-get-short-path ()
|
||||
"Fetches a short file path for the header in Doom module templates."
|
||||
(when (string-match "/modules/\\(.+\\)$" buffer-file-truename)
|
||||
(match-string 1 buffer-file-truename)))
|
||||
(let ((path (file-truename (or buffer-file-name default-directory))))
|
||||
(cond ((string-match "/modules/\\(.+\\)$" path)
|
||||
(match-string 1 path))
|
||||
((file-in-directory-p path doom-emacs-dir)
|
||||
(file-relative-name path doom-emacs-dir))
|
||||
((abbreviate-file-name path)))))
|
||||
|
||||
|
||||
;;
|
||||
|
|
|
@ -6,55 +6,11 @@
|
|||
(expand-file-name "templates/" (file-name-directory load-file-name))
|
||||
"The path to a directory of yasnippet folders to use for file templates.")
|
||||
|
||||
(defvar +file-templates-alist ()
|
||||
"An alist of file template rules. The CAR of each rule is either a major mode
|
||||
symbol or regexp string. The CDR is a plist. See `doom--set:file-template' for
|
||||
more information.")
|
||||
|
||||
(defvar +file-templates-default-trigger "__"
|
||||
"The default yasnippet trigger key (a string) for file template rules that
|
||||
don't have a :trigger property in `+file-templates-alist'.")
|
||||
|
||||
|
||||
;;
|
||||
;; Bootstrap
|
||||
;;
|
||||
|
||||
(after! yasnippet
|
||||
(add-to-list 'yas-snippet-dirs '+file-templates-dir 'append #'eq))
|
||||
|
||||
(defun +file-template-p (rule)
|
||||
"Return t if RULE applies to the current buffer."
|
||||
(let ((pred (car rule))
|
||||
(plist (cdr rule)))
|
||||
(and (cond ((stringp pred) (string-match-p pred))
|
||||
((symbolp pred) (eq major-mode pred)))
|
||||
(or (not (plist-member plist :when))
|
||||
(funcall (plist-get plist :when) buffer-file-name))
|
||||
rule)))
|
||||
|
||||
(defun +file-templates|init ()
|
||||
"Check if the current buffer is a candidate for file template expansion. It
|
||||
must be non-read-only, empty, and there must be a rule in
|
||||
`+file-templates-alist' that applies to it."
|
||||
(when (and (not buffer-read-only)
|
||||
(bobp) (eobp))
|
||||
(when-let* ((rule (cl-find-if #'+file-template-p +file-templates-alist)))
|
||||
(apply #'+file-templates--expand rule))))
|
||||
|
||||
(add-hook 'find-file-hook #'+file-templates|init)
|
||||
|
||||
|
||||
;;
|
||||
;; File templates
|
||||
;;
|
||||
|
||||
(defun +file-templates-in-emacs-dirs-p (file)
|
||||
"Returns t if FILE is in Doom or your private directory."
|
||||
(or (file-in-directory-p file doom-private-dir)
|
||||
(file-in-directory-p file doom-emacs-dir)))
|
||||
|
||||
(setq +file-templates-alist
|
||||
(defvar +file-templates-alist
|
||||
`(;; General
|
||||
(gitignore-mode)
|
||||
(dockerfile-mode)
|
||||
|
@ -137,11 +93,50 @@ must be non-read-only, empty, and there must be a rule in
|
|||
;; Shell scripts
|
||||
("\\.zunit$" :trigger "__zunit" :mode sh-mode)
|
||||
(fish-mode)
|
||||
(sh-mode)
|
||||
))
|
||||
(sh-mode))
|
||||
"An alist of file template rules. The CAR of each rule is either a major mode
|
||||
symbol or regexp string. The CDR is a plist. See `doom--set:file-template' for
|
||||
more information.")
|
||||
|
||||
|
||||
;;
|
||||
;; Plugins
|
||||
;; Library
|
||||
;;
|
||||
|
||||
(defun +file-template-p (rule)
|
||||
"Return t if RULE applies to the current buffer."
|
||||
(let ((pred (car rule))
|
||||
(plist (cdr rule)))
|
||||
(and (cond ((and (stringp pred) buffer-file-name) (string-match-p pred buffer-file-name))
|
||||
((symbolp pred) (eq major-mode pred)))
|
||||
(or (not (plist-member plist :when))
|
||||
(funcall (plist-get plist :when) buffer-file-name))
|
||||
rule)))
|
||||
|
||||
(defun +file-templates-in-emacs-dirs-p (file)
|
||||
"Returns t if FILE is in Doom or your private directory."
|
||||
(or (file-in-directory-p file doom-private-dir)
|
||||
(file-in-directory-p file doom-emacs-dir)))
|
||||
|
||||
(defun +file-templates|check ()
|
||||
"Check if the current buffer is a candidate for file template expansion. It
|
||||
must be non-read-only, empty, and there must be a rule in
|
||||
`+file-templates-alist' that applies to it."
|
||||
(when (and (not buffer-read-only)
|
||||
(bobp) (eobp)
|
||||
(not (string-match-p "^ *\\*" (buffer-name))))
|
||||
(when-let* ((rule (cl-find-if #'+file-template-p +file-templates-alist)))
|
||||
(apply #'+file-templates--expand rule))))
|
||||
|
||||
|
||||
;;
|
||||
;; Bootstrap
|
||||
;;
|
||||
|
||||
(defun +file-templates|init ()
|
||||
(after! yasnippet
|
||||
(add-to-list 'yas-snippet-dirs '+file-templates-dir 'append #'eq))
|
||||
(add-hook 'find-file-hook #'+file-templates|check))
|
||||
|
||||
(add-hook 'doom-post-init-hook #'+file-templates|init)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue