about summary refs log tree commit diff stats
path: root/lisp/+setup.el
blob: db5922399a2f773199ef971e5eb02c5d1f0e72e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
;;; +setup.el -- my `setup' commands -*- lexical-binding: t -*-

;; Author: Case Duckworth <acdw@acdw.net>

;; This file is NOT part of GNU Emacs.

;;; License:
;; Everyone is permitted to do whatever with this software, without
;; limitation.  This software comes without any warranty whatsoever,
;; but with two pieces of advice:
;; - Don't hurt yourself.
;; - Make good choices.

;;; Commentary:

;; `setup', by Philip Kaludercic, is a wonderful package that works
;; sort of like `use-package', but to my mind it's cleaner and easier
;; to extend.  These are my additions to the local macros provided by
;; the package.

;;; Code:

(require 'el-patch)
(require 'setup)
(require 'straight)
(require 'cl-lib)

(defun +setup-warn (message &rest args)
  "Warn the user that something bad happened in `setup'."
  (display-warning 'setup (format message args)))

(setup-define :quit
  'setup-quit
  :documentation "Quit the current `setup' form.
Good for commenting.")

(setup-define :face
    (lambda (face spec)
      `(custom-set-faces (list ,face ,spec 'now "Customized by `setup'.")))
  :documentation "Customize FACE with SPEC using `custom-set-faces'."
  :repeatable t)

(setup-define :load-after
    (lambda (&rest features)
      (let ((body `(require ',(setup-get 'feature))))
    (dolist (feature (nreverse features))
      (setq body `(with-eval-after-load ',feature ,body)))
    body))
  :documentation "Load the current feature after FEATURES.")

(setup-define :load-from
  (lambda (path)
    `(let ((path* (expand-file-name ,path)))
       (if (file-exists-p path*)
           (add-to-list 'load-path path*)
         ,(setup-quit))))
  :documentation "Add PATH to load path.
This macro can be used as NAME, and it will replace itself with
the nondirectory part of PATH.
If PATH does not exist, abort the evaluation."
  :shorthand (lambda (args)
               (intern
                (file-name-nondirectory
                 (directory-file-name (cadr args))))))


;;; Straight.el

(with-eval-after-load 'straight
  (defun setup--straight-handle-arg (arg var)
    (cond
     ((and (boundp var) (symbol-value var)) t)
     ((keywordp arg) (set var t))
     ((functionp arg) (set var nil) (funcall arg))
     ((listp arg) (set var nil) (eval arg :lexical))))

  (setup-define :straight
    (lambda (recipe &rest predicates)
      (let* ((skp (make-symbol "straight-keyword-p"))
             (straight-use-p
              (cl-every (lambda (f) (setup--straight-handle-arg f skp))
                        predicates))
             (form `(unless (and ,straight-use-p
                                 (condition-case e
                                     (straight-use-package ',recipe)
                                   (error
                                    (+setup-warn ":straight error: %S"
                                                 ',recipe)
                                    ,(setup-quit))
                                   (:success t)))
(defun setup--straight-handle-arg (arg var)
  (cond
   ((and (boundp var) (symbol-value var)) t)
   ((keywordp arg) (set var t))
   ((functionp arg) (set var nil) (funcall arg))
   ((listp arg) (set var nil) (eval arg :lexical))))

(setup-define :straight
  (lambda (recipe &rest predicates)
    (let* ((skp (make-symbol "straight-keyword-p"))
           (straight-use-p
            (cl-every (lambda (f) (setup--straight-handle-arg f skp))
                      predicates))
           (form `(unless (and ,straight-use-p
                               (condition-case e
                                   (straight-use-package ',recipe)
                                 (error
                                  (+setup-warn ":straight error: %S"
                                               ',recipe)
                                  ,(setup-quit))
                                 (:success t)))
                    ,(setup-quit))))
      ;; Keyword arguments --- :quit is special and should short-circuit
      (if (memq :quit predicates)
          (setq form `,(setup-quit))
        ;; Otherwise, handle the rest of them ...
        (when-let ((after (cadr (memq :after predicates))))
          (setq form `(with-eval-after-load ,(if (eq after t)
                                                 (setup-get 'feature)
                                               after)
                        ,form))))
      ;; Finally ...
      form))
  :documentation "Install RECIPE with `straight-use-package'.
If PREDICATES are given, only install RECIPE if all of them return non-nil.
The following keyword arguments are also recognized:
- :quit          --- immediately stop evaluating.  Good for commenting.
- :after FEATURE --- only install RECIPE after FEATURE is loaded.
                     If FEATURE is t, install RECIPE after the current feature."
  :repeatable nil
  :indent 1
  :shorthand (lambda (sexp)
               (let ((recipe (cadr sexp)))
                 (or (car-safe recipe) recipe))))                      ,(setup-quit))))
        ;; Keyword arguments --- :quit is special and should short-circuit
        (if (memq :quit predicates)
            (setq form `,(setup-quit))
          ;; Otherwise, handle the rest of them ...
          (when-let ((after (cadr (memq :after predicates))))
            (setq form `(with-eval-after-load ,(if (eq after t)
                                                   (setup-get 'feature)
                                                 after)
                          ,form))))
        ;; Finally ...
        form))
    :documentation "Install RECIPE with `straight-use-package'.
If PREDICATES are given, only install RECIPE if all of them return non-nil.
The following keyword arguments are also recognized:
- :quit          --- immediately stop evaluating.  Good for commenting.
- :after FEATURE --- only install RECIPE after FEATURE is loaded.
                     If FEATURE is t, install RECIPE after the current feature."
    :repeatable nil
    :indent 1
    :shorthand (lambda (sexp)
                 (let ((recipe (cadr sexp)))
                   (or (car-safe recipe) recipe)))))


;;; Redefines of `setup' forms

(setup-define :bind-into
  (lambda (feature-or-map &rest rest)
    (cl-loop for f/m in (ensure-list feature-or-map)
             collect (if (string-match-p "-map\\'" (symbol-name f/m))
                         `(:with-map ,f/m (:bind ,@rest))
                       `(:with-feature ,f/m (:bind ,@rest)))
             into forms
             finally return `(progn ,@forms)))
  :documentation "Bind into keys into the map(s) of FEATURE-OR-MAP.
FEATURE-OR-MAP can be a feature or map name or a list of them.
The arguments REST are handled as by `:bind'."
  :debug '(sexp &rest form sexp)
  :indent 1)

(setup-define :require
  (lambda (&rest features)
    (require 'cl-lib)
    (if features
        `(progn ,@(cl-loop for feature in features collect
                           `(unless (require ',feature nil t)
                              ,(setup-quit))))
      `(unless (require ',(setup-get 'feature) nil t)
         ,(setup-quit))))
  :documentation "Try to require FEATURE, or stop evaluating body.
This macro can be used as NAME, and it will replace itself with
the first FEATURE."
  :repeatable nil
  :shorthand #'cadr)

(provide '+setup)
;;; +setup.el ends here