editor/file-templates: general refactor
This commit is contained in:
parent
21cf1c2da6
commit
c0bffd4268
2 changed files with 23 additions and 22 deletions
|
@ -50,7 +50,7 @@ these properties:
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Library
|
;;; Library
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(cl-defun +file-templates--expand (pred &key project mode trigger ignore _when)
|
(cl-defun +file-templates--expand (pred &key project mode trigger ignore _when)
|
||||||
|
@ -70,11 +70,11 @@ evil is loaded and enabled)."
|
||||||
(unless yas-minor-mode
|
(unless yas-minor-mode
|
||||||
(yas-minor-mode-on))
|
(yas-minor-mode-on))
|
||||||
(when (and yas-minor-mode
|
(when (and yas-minor-mode
|
||||||
(when-let*
|
(when-let
|
||||||
((template (cl-find trigger (yas--all-templates (yas--get-snippet-tables mode))
|
(template (cl-find trigger (yas--all-templates (yas--get-snippet-tables mode))
|
||||||
:key #'yas--template-key :test #'equal)))
|
:key #'yas--template-key :test #'equal))
|
||||||
(yas-expand-snippet (yas--template-content template)))
|
(yas-expand-snippet (yas--template-content template)))
|
||||||
(and (featurep 'evil) evil-mode)
|
(and (featurep 'evil) evil-local-mode)
|
||||||
(and yas--active-field-overlay
|
(and yas--active-field-overlay
|
||||||
(overlay-buffer yas--active-field-overlay)
|
(overlay-buffer yas--active-field-overlay)
|
||||||
(overlay-get yas--active-field-overlay 'yas--field)))
|
(overlay-get yas--active-field-overlay 'yas--field)))
|
||||||
|
@ -84,18 +84,20 @@ evil is loaded and enabled)."
|
||||||
(defun +file-templates-get-short-path ()
|
(defun +file-templates-get-short-path ()
|
||||||
"Fetches a short file path for the header in Doom module templates."
|
"Fetches a short file path for the header in Doom module templates."
|
||||||
(let ((path (file-truename (or buffer-file-name default-directory))))
|
(let ((path (file-truename (or buffer-file-name default-directory))))
|
||||||
(cond ((string-match "/modules/\\(.+\\)$" path)
|
(save-match-data
|
||||||
(match-string 1 path))
|
(cond ((string-match "/modules/\\(.+\\)$" path)
|
||||||
((file-in-directory-p path doom-emacs-dir)
|
(match-string 1 path))
|
||||||
(file-relative-name path doom-emacs-dir))
|
((file-in-directory-p path doom-emacs-dir)
|
||||||
((abbreviate-file-name path)))))
|
(file-relative-name path doom-emacs-dir))
|
||||||
|
((abbreviate-file-name path))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +file-template-p (rule)
|
(defun +file-template-p (rule)
|
||||||
"Return t if RULE applies to the current buffer."
|
"Return t if RULE applies to the current buffer."
|
||||||
(let ((pred (car rule))
|
(let ((pred (car rule))
|
||||||
(plist (cdr rule)))
|
(plist (cdr rule)))
|
||||||
(and (cond ((and (stringp pred) buffer-file-name) (string-match-p pred buffer-file-name))
|
(and (cond ((and (stringp pred) buffer-file-name)
|
||||||
|
(string-match-p pred buffer-file-name))
|
||||||
((symbolp pred) (eq major-mode pred)))
|
((symbolp pred) (eq major-mode pred)))
|
||||||
(or (not (plist-member plist :when))
|
(or (not (plist-member plist :when))
|
||||||
(funcall (plist-get plist :when) buffer-file-name))
|
(funcall (plist-get plist :when) buffer-file-name))
|
||||||
|
@ -103,22 +105,21 @@ evil is loaded and enabled)."
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; Commands
|
;;; Commands
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +file-templates/insert-license ()
|
(defun +file-templates/insert-license ()
|
||||||
"Insert a license file template into the current file."
|
"Insert a license file template into the current file."
|
||||||
(interactive)
|
(interactive)
|
||||||
(require 'yasnippet)
|
(require 'yasnippet)
|
||||||
(let* ((templates
|
(let ((templates
|
||||||
(let ((yas-choose-tables-first nil) ; avoid prompts
|
(let (yas-choose-tables-first ; avoid prompts
|
||||||
(yas-choose-keys-first nil))
|
yas-choose-keys-first)
|
||||||
(cl-loop for tpl in (yas--all-templates (yas--get-snippet-tables 'text-mode))
|
(cl-loop for tpl in (yas--all-templates (yas--get-snippet-tables 'text-mode))
|
||||||
for uuid = (yas--template-uuid tpl)
|
for uuid = (yas--template-uuid tpl)
|
||||||
if (string-prefix-p "__license-" uuid)
|
if (string-prefix-p "__license-" uuid)
|
||||||
collect (cons (string-remove-prefix "__license-" uuid) tpl))))
|
collect (cons (string-remove-prefix "__license-" uuid) tpl)))))
|
||||||
(uuid (yas-choose-value (mapcar #'car templates))))
|
(when-let (uuid (yas-choose-value (mapcar #'car templates)))
|
||||||
(when uuid
|
|
||||||
(yas-expand-snippet (cdr (assoc uuid templates))))))
|
(yas-expand-snippet (cdr (assoc uuid templates))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
|
|
|
@ -114,7 +114,7 @@ must be non-read-only, empty, and there must be a rule in
|
||||||
(when (and (not buffer-read-only)
|
(when (and (not buffer-read-only)
|
||||||
(bobp) (eobp)
|
(bobp) (eobp)
|
||||||
(not (string-match-p "^ *\\*" (buffer-name))))
|
(not (string-match-p "^ *\\*" (buffer-name))))
|
||||||
(when-let* ((rule (cl-find-if #'+file-template-p +file-templates-alist)))
|
(when-let (rule (cl-find-if #'+file-template-p +file-templates-alist))
|
||||||
(apply #'+file-templates--expand rule))))
|
(apply #'+file-templates--expand rule))))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue