diff options
Diffstat (limited to 'lisp/+init.el')
-rw-r--r-- | lisp/+init.el | 117 |
1 files changed, 0 insertions, 117 deletions
diff --git a/lisp/+init.el b/lisp/+init.el deleted file mode 100644 index 903f2dc..0000000 --- a/lisp/+init.el +++ /dev/null | |||
@@ -1,117 +0,0 @@ | |||
1 | ;;; +init.el --- extra init.el stuff -*- lexical-binding: t -*- | ||
2 | |||
3 | ;;; Commentary: | ||
4 | |||
5 | ;; Yes, I edit my init.el often enough I need to write a mode for it. The | ||
6 | ;; sorting function is based on code from | ||
7 | ;; https://github.com/alphapapa/unpackaged.el | ||
8 | |||
9 | ;;; Code: | ||
10 | |||
11 | (require '+lisp) | ||
12 | |||
13 | ;;; Sort `setup' forms | ||
14 | |||
15 | (defun +init--sexp-setup-p (sexp-str &optional head) | ||
16 | "Is SEXP-STR a `setup' form, optionally with a HEAD form?" | ||
17 | (let ((head (if (and head (symbolp head)) | ||
18 | (symbol-name head) | ||
19 | head))) | ||
20 | (and (string-match-p (rx (: bos (* whitespace) "(setup")) sexp-str) | ||
21 | (if head | ||
22 | (string-match-p (concat "\\`.*" head) sexp-str) | ||
23 | t)))) | ||
24 | |||
25 | (defun +init-sort () | ||
26 | "Sort init.el. | ||
27 | Sort based on the following heuristic: `setup' forms (the | ||
28 | majority of my init.el) are sorted after everything else, and | ||
29 | within that group, forms with a HEAD of `:require' are sorted | ||
30 | first, and `:straight' HEADs are sorted last. All other forms | ||
31 | are sorted lexigraphically." | ||
32 | (interactive) | ||
33 | ;; I have to make my own "version" of `save-excursion', since the mark and | ||
34 | ;; point are lost (I think that's the problem) when sorting the buffer. | ||
35 | (let* ((current-point (point)) | ||
36 | (current-defun (beginning-of-defun)) | ||
37 | (defun-point (- current-point (point))) | ||
38 | (current-defun-re (buffer-substring-no-properties (line-beginning-position) | ||
39 | (line-end-position)))) | ||
40 | (widen) ; It makes no sense to `save-restriction' | ||
41 | (+lisp-sort-sexps | ||
42 | (point-min) (point-max) | ||
43 | ;; Key function | ||
44 | nil | ||
45 | ;; Sort function | ||
46 | (lambda (s1 s2) | ||
47 | (let ((s1 (cdr s1)) (s2 (cdr s2))) | ||
48 | (cond | ||
49 | ;; Sort everything /not/ `setup' /before/ `setup' | ||
50 | ((and (+init--sexp-setup-p s1) | ||
51 | (not (+init--sexp-setup-p s2))) | ||
52 | nil) | ||
53 | ((and (+init--sexp-setup-p s2) | ||
54 | (not (+init--sexp-setup-p s1))) | ||
55 | t) | ||
56 | ;; otherwise... | ||
57 | (t (let ((s1-straight (+init--sexp-setup-p s1 :straight)) | ||
58 | (s2-straight (+init--sexp-setup-p s2 :straight)) | ||
59 | (s1-require (+init--sexp-setup-p s1 :require)) | ||
60 | (s2-require (+init--sexp-setup-p s2 :require))) | ||
61 | (cond | ||
62 | ;; `:straight' setups have extra processing | ||
63 | ((and s1-straight s2-straight) | ||
64 | (let* ((r (rx (: ":straight" (? "-when") (* space) (? "(")))) | ||
65 | (s1 (replace-regexp-in-string r "" s1)) | ||
66 | (s2 (replace-regexp-in-string r "" s2))) | ||
67 | (string< s1 s2))) | ||
68 | ;; `:require' setups go first | ||
69 | ((and s1-require (not s2-require)) t) | ||
70 | ((and s2-require (not s1-require)) nil) | ||
71 | ;; `:straight' setups go last | ||
72 | ((and s1-straight (not s2-straight)) nil) | ||
73 | ((and s2-straight (not s1-straight)) t) | ||
74 | ;; otherwise, sort lexigraphically | ||
75 | (t (string< s1 s2))))))))) | ||
76 | ;; Return to original point relative to the defun we were in | ||
77 | (ignore-errors (goto-char (point-min)) | ||
78 | (re-search-forward current-defun-re) | ||
79 | (beginning-of-defun) | ||
80 | (goto-char (+ (point) defun-point))))) | ||
81 | |||
82 | (defun +init-sort-then-save () | ||
83 | "Sort init.el, then save it." | ||
84 | (interactive) | ||
85 | (+init-sort) | ||
86 | (if (fboundp #'user-save-buffer) | ||
87 | (user-save-buffer) | ||
88 | (save-buffer))) | ||
89 | |||
90 | ;;; Add `setup' forms to `imenu-generic-expression' | ||
91 | |||
92 | (defun +init-add-setup-to-imenu () | ||
93 | "Recognize `setup' forms in `imenu'." | ||
94 | ;; `imenu-generic-expression' automatically becomes buffer-local when set | ||
95 | (setf (alist-get "Setup" imenu-generic-expression nil nil #'equal) | ||
96 | (list | ||
97 | (rx (: "(setup" (+ space) | ||
98 | (group (? "(") (* nonl)))) | ||
99 | 1)) | ||
100 | (when (boundp 'consult-imenu-config) | ||
101 | (setf (alist-get ?s | ||
102 | (plist-get | ||
103 | (alist-get 'emacs-lisp-mode consult-imenu-config) | ||
104 | :types)) | ||
105 | '("Setup")))) | ||
106 | |||
107 | ;;; Major mode | ||
108 | |||
109 | ;;;###autoload | ||
110 | (define-derived-mode +init-mode emacs-lisp-mode "Init.el" | ||
111 | "`emacs-lisp-mode', but with a few specialized bits and bobs for init.el.") | ||
112 | |||
113 | ;;;###autoload | ||
114 | (add-to-list 'auto-mode-alist '("/init\\.el\\'" . +init-mode)) | ||
115 | |||
116 | (provide '+init) | ||
117 | ;;; +init.el ends here | ||