Optimize file-template find-file hook function
This commit is contained in:
parent
d8ea65b968
commit
3f759dde23
2 changed files with 18 additions and 18 deletions
|
@ -91,18 +91,6 @@ evil is loaded and enabled)."
|
||||||
(file-relative-name path doom-emacs-dir))
|
(file-relative-name path doom-emacs-dir))
|
||||||
((abbreviate-file-name path))))))
|
((abbreviate-file-name path))))))
|
||||||
|
|
||||||
;;;###autoload
|
|
||||||
(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)))
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;;; Commands
|
;;; Commands
|
||||||
|
|
|
@ -114,16 +114,28 @@ information.")
|
||||||
(or (file-in-directory-p file doom-private-dir)
|
(or (file-in-directory-p file doom-private-dir)
|
||||||
(file-in-directory-p file doom-emacs-dir)))
|
(file-in-directory-p file doom-emacs-dir)))
|
||||||
|
|
||||||
|
(defun +file-template-p (rule)
|
||||||
|
"Return t if RULE applies to the current buffer."
|
||||||
|
(let ((pred (car rule))
|
||||||
|
(plist (cdr rule)))
|
||||||
|
(and (cond ((symbolp pred) (eq major-mode pred))
|
||||||
|
((and (stringp pred) buffer-file-name)
|
||||||
|
(string-match-p pred buffer-file-name))
|
||||||
|
((not (plist-member plist :when)) t)
|
||||||
|
((funcall (plist-get plist :when) buffer-file-name)))
|
||||||
|
rule)))
|
||||||
|
|
||||||
(defun +file-templates-check-h ()
|
(defun +file-templates-check-h ()
|
||||||
"Check if the current buffer is a candidate for file template expansion. It
|
"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
|
must be non-read-only, empty, and there must be a rule in
|
||||||
`+file-templates-alist' that applies to it."
|
`+file-templates-alist' that applies to it."
|
||||||
(when (and (not (file-exists-p (or (buffer-file-name) "")))
|
(and buffer-file-name
|
||||||
(not buffer-read-only)
|
(not buffer-read-only)
|
||||||
(bobp) (eobp)
|
(bobp) (eobp)
|
||||||
(not (string-match-p "^ *\\*" (buffer-name))))
|
(not (member (substring (buffer-name) 0 1) '("*" " ")))
|
||||||
(when-let (rule (cl-find-if #'+file-template-p +file-templates-alist))
|
(not (file-exists-p buffer-file-name))
|
||||||
(apply #'+file-templates--expand rule))))
|
(when-let (rule (cl-find-if #'+file-template-p +file-templates-alist))
|
||||||
|
(apply #'+file-templates--expand rule))))
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue