-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathcore-configuration-layer.el
2241 lines (2096 loc) · 101 KB
/
core-configuration-layer.el
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; core-configuration-layer.el --- Spacemacs Core File
;;
;; Copyright (c) 2012-2017 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(require 'cl-lib)
(require 'eieio)
(require 'subr-x)
(require 'package)
(require 'warnings)
(require 'help-mode)
(require 'ht)
(require 'core-dotspacemacs)
(require 'core-funcs)
(require 'core-spacemacs-buffer)
(defvar configuration-layer--refresh-package-timeout dotspacemacs-elpa-timeout
"Timeout in seconds to reach a package archive page.")
(defconst configuration-layer-template-directory
(expand-file-name (concat spacemacs-core-directory "templates/"))
"Configuration layer templates directory.")
(defconst configuration-layer-directory
(expand-file-name (concat spacemacs-start-directory "layers/"))
"Spacemacs contribution layers base directory.")
(defconst configuration-layer-private-directory
(expand-file-name (concat spacemacs-start-directory "private/"))
"Spacemacs private layers base directory.")
(defconst configuration-layer-private-layer-directory
(let ((dotspacemacs-layer-dir
(when dotspacemacs-directory
(expand-file-name
(concat dotspacemacs-directory "layers/")))))
(if (and dotspacemacs-directory
(file-exists-p dotspacemacs-layer-dir))
dotspacemacs-layer-dir
configuration-layer-private-directory))
"Spacemacs default directory for private layers.")
(defun configuration-layer/elpa-directory (root)
"Evaluate the correct package subdirectory of ROOT. This is
done according to the value of `dotspacemacs-elpa-subdirectory'.
If it is nil, then ROOT is returned. Otherwise a subdirectory of
ROOT is returned."
(if (not dotspacemacs-elpa-subdirectory)
root
(let ((subdir (if (eq 'emacs-version dotspacemacs-elpa-subdirectory)
(format "%d%s%d"
emacs-major-version
version-separator
emacs-minor-version)
(eval dotspacemacs-elpa-subdirectory))))
(file-name-as-directory (expand-file-name subdir root)))))
(defun configuration-layer/get-elpa-package-install-directory (pkg)
"Return the install directory of elpa PKG. Return nil if it is not found."
(let ((elpa-dir package-user-dir))
(when (file-exists-p elpa-dir)
(let* ((pkg-match (concat "\\`" (symbol-name pkg) "-[0-9]+"))
(dir (car (directory-files elpa-dir 'full pkg-match))))
(when dir (file-name-as-directory dir))))))
(defvar configuration-layer-rollback-directory
(concat spacemacs-cache-directory ".rollback/")
"Spacemacs rollback directory.")
(defconst configuration-layer-rollback-info "rollback-info"
"Spacemacs rollback information file.")
(defclass cfgl-layer ()
((name :initarg :name
:type symbol
:documentation "Name of the layer.")
(dir :initarg :dir
:initform nil
:type (satisfies (lambda (x) (or (null x) (stringp x))))
:documentation "Absolute path to the layer directory.")
(packages :initarg :packages
:initform nil
:type list
:documentation "List of package symbols declared in this layer.")
(selected-packages :initarg :selected-packages
:initform 'all
:type (satisfies (lambda (x) (or (and (symbolp x) (eq 'all x))
(listp x))))
:documentation "List of selected package symbols.")
(variables :initarg :variables
:initform nil
:type list
:documentation "A list of variable-value pairs.")
(lazy-install :initarg :lazy-install
:initform nil
:type boolean
:documentation
"If non-nil then the layer needs to be installed")
(disabled :initarg :disabled-for
:initform nil
:type list
:documentation "A list of layers where this layer is disabled.")
(enabled :initarg :enabled-for
:initform 'unspecified
:type (satisfies (lambda (x) (or (listp x) (eq 'unspecified x))))
:documentation (concat "A list of layers where this layer is enabled. "
"(Takes precedence over `:disabled-for'.)")))
"A configuration layer.")
(defmethod cfgl-layer-owned-packages ((layer cfgl-layer) &optional props)
"Return the list of owned packages by LAYER.
If PROPS is non-nil then return packages as lists with their properties.
LAYER has to be installed for this method to work properly."
(delq nil (mapcar
(lambda (x)
(let* ((pkg-name (if (listp x) (car x) x))
(pkg (configuration-layer/get-package pkg-name)))
(when (eq (oref layer :name) (car (oref pkg :owners))) x)))
(cfgl-layer-get-packages layer props))))
(defmethod cfgl-layer-owned-packages ((layer nil) &optional props)
"Accept nil as argument and return nil."
nil)
(defmethod cfgl-layer-get-packages ((layer cfgl-layer) &optional props)
"Return the list of packages for LAYER.
If PROPS is non-nil then return packages as lists with their properties"
(let ((all (eq 'all (oref layer :selected-packages))))
(delq nil (mapcar
(lambda (x)
(let ((pkg-name (if (listp x) (car x) x)))
(when (or all (memq pkg-name
(oref layer :selected-packages)))
(if props x pkg-name))))
(oref layer :packages)))))
(defclass cfgl-package ()
((name :initarg :name
:type symbol
:documentation "Name of the package.")
(min-version :initarg :min-version
:initform nil
:type list
:documentation "Minimum version to install as a version list.")
(owners :initarg :owners
:initform nil
:type list
:documentation "The layer defining the init function.")
(pre-layers :initarg :pre-layers
:initform '()
:type list
:documentation "List of layers with a pre-init function.")
(post-layers :initarg :post-layers
:initform '()
:type list
:documentation "List of layers with a post-init function.")
(location :initarg :location
:initform elpa
:type (satisfies (lambda (x)
(or (stringp x)
(memq x '(built-in local site elpa))
(and (listp x) (eq 'recipe (car x))))))
:documentation "Location of the package.")
(toggle :initarg :toggle
:initform t
:type (satisfies (lambda (x) (or (symbolp x) (listp x))))
:documentation
"Package is enabled/installed if toggle evaluates to non-nil.")
(step :initarg :step
:initform nil
:type (satisfies (lambda (x) (member x '(nil bootstrap pre))))
:documentation "Initialization step.")
(lazy-install :initarg :lazy-install
:initform nil
:type boolean
:documentation
"If non-nil then the package needs to be installed")
(protected :initarg :protected
:initform nil
:type boolean
:documentation
"If non-nil then this package cannot be excluded.")
(excluded :initarg :excluded
:initform nil
:type boolean
:documentation
"If non-nil this package is excluded from all layers.")))
(defmethod cfgl-package-enabledp ((pkg cfgl-package) &optional inhibit-messages)
"Evaluate the `toggle' slot of passed PKG.
If INHIBIT-MESSAGES is non nil then any message emitted by the toggle evaluation
is ignored."
(let ((message-log-max (unless inhibit-messages message-log-max))
(toggle (oref pkg :toggle)))
(eval toggle)))
(defmethod cfgl-package-get-safe-owner ((pkg cfgl-package))
"Safe method to return the name of the layer which owns PKG."
;; The owner of a package is the first *used* layer in `:owners' slot.
;; Note: for packages in `configuration-layer--used-packages' the owner is
;; always the car of the `:owners' slot.
(let ((layers (oref pkg :owners)))
(while (and (consp layers)
(not (configuration-layer/layer-usedp (car layers))))
(pop layers))
(when (configuration-layer/layer-usedp (car layers))
(car layers))))
(defmethod cfgl-package-set-property ((pkg cfgl-package) slot value)
"Set SLOT to the given VALUE for the package PKG.
If `configuration-layer--package-properties-read-onlyp' is non-nil then VALUE
is not set for the given SLOT."
(unless configuration-layer--package-properties-read-onlyp
(eval `(oset pkg ,slot value))))
(defvar configuration-layer--elpa-archives
'(("melpa" . "melpa.org/packages/")
("org" . "orgmode.org/elpa/")
("gnu" . "elpa.gnu.org/packages/"))
;; '(("spacelpa" . "~/.emacs.d/.cache/spacelpa/"))
"List of ELPA archives required by Spacemacs.")
(defvar configuration-layer-exclude-all-layers nil
"If non nil then only the distribution layer is loaded.")
(defvar configuration-layer-force-distribution nil
"If set, bypass the user's choice `dotspacemacs-distribution'.")
(defvar configuration-layer--package-archives-refreshed nil
"Non nil if package archives have already been refreshed.")
(defvar configuration-layer--load-packages-files nil
"If non-nil force loading `packages.el' files when creating layer objects.")
(defvar configuration-layer--used-layers '()
"A non-sorted list of used layer names.")
(defvar configuration-layer--indexed-layers (make-hash-table :size 1024)
"Hash map to index `cfgl-layer' objects by their names.")
(defvar configuration-layer--used-packages '()
"An alphabetically sorted list of used package names.")
(defvar configuration-layer--indexed-packages (make-hash-table :size 2048)
"Hash map to index `cfgl-package' objects by their names.")
(defvar configuration-layer--used-distant-packages '()
"A list of all distant packages that are effectively used.")
(defvar configuration-layer--check-new-version-error-packages nil
"A list of all packages that were skipped during last update attempt.")
(defvar configuration-layer--protected-packages nil
"A list of packages that will be protected from removal as orphans.")
(defvar configuration-layer--lazy-mode-alist nil
"Association list where the key is a mode and the value a regexp.")
(defvar configuration-layer--inhibit-warnings nil
"If non-nil then warning message emitted by the layer system are ignored.")
(defvar configuration-layer--package-properties-read-onlyp nil
"If non-nil then package properties are read only and cannot be overriden by
`configuration-layer/make-package'.")
(defvar configuration-layer--declared-layers-usedp nil
"If non-nil then declared layers are considered to be used.")
(defvar configuration-layer-error-count nil
"Non nil indicates the number of errors occurred during the
installation of initialization.")
(defvar configuration-layer-categories '()
"List of strings corresponding to category names. A category is a
directory with a name starting with `+'.")
(defvar update-packages-alist '()
"Used to collect information about rollback packages in the
cache folder.")
(defun configuration-layer/initialize ()
"Initialize `package.el'."
(setq configuration-layer--refresh-package-timeout dotspacemacs-elpa-timeout)
(unless package--initialized
(setq configuration-layer-rollback-directory
(configuration-layer/elpa-directory configuration-layer-rollback-directory))
(setq package-user-dir
(configuration-layer/elpa-directory package-user-dir))
(setq package-archives (configuration-layer//resolve-package-archives
configuration-layer--elpa-archives))
;; optimization, no need to activate all the packages so early
(setq package-enable-at-startup nil)
(package-initialize 'noactivate)))
(defun configuration-layer//configure-quelpa ()
"Configure `quelpa' package."
(setq quelpa-verbose init-file-debug
quelpa-dir (concat spacemacs-cache-directory "quelpa/")
quelpa-build-dir (expand-file-name "build" quelpa-dir)
quelpa-persistent-cache-file (expand-file-name "cache" quelpa-dir)
quelpa-update-melpa-p nil)
(require 'quelpa))
(defun configuration-layer//package-archive-absolute-pathp (archive)
"Return t if ARCHIVE has an absolute path defined."
(let ((path (cdr archive)))
(or (string-match-p "http" path)
(string-prefix-p "~" path)
(string-prefix-p "/" path))))
(defun configuration-layer//package-archive-local-pathp (archive)
"Return t if ARCHIVE has a local path."
(let ((path (cdr archive)))
(or (string-prefix-p "~" path)
(string-prefix-p "/" path)
(string-prefix-p "\." path))))
(defun configuration-layer//resolve-package-archives (archives)
"Resolve HTTP handlers for each archive in ARCHIVES and return a list
of all reachable ones.
If the address of an archive already contains the protocol then this address is
left untouched.
The returned list has a `package-archives' compliant format."
(mapcar
(lambda (x)
(let ((aname (car x))
(apath (cdr x)))
(cons aname
(if (configuration-layer//package-archive-absolute-pathp x)
apath
(concat
(if (and dotspacemacs-elpa-https
(not spacemacs-insecure))
"https://"
"http://")
apath)))))
archives))
(defun configuration-layer/retrieve-package-archives (&optional quiet force)
"Retrieve all archives declared in current `package-archives'.
This function first performs a simple GET request with a timeout in order to
fix very long refresh time when an archive is not reachable.
Note that this simple GET is a heuristic to determine the availability
likelihood of an archive, so it can gives false positive if the archive
page is served but the archive is not.
If QUIET is non nil then the function does not print message in the Spacemacs
home buffer.
If FORCE is non nil then refresh the archives even if they have been already
refreshed during the current session."
(unless (and configuration-layer--package-archives-refreshed
(not force))
(setq configuration-layer--package-archives-refreshed t)
(let ((count (length package-archives))
(i 1))
(dolist (archive package-archives)
(let ((aname (car archive))
(apath (cdr archive)))
(unless quiet
(spacemacs-buffer/replace-last-line
(format "--> refreshing package archive: %s... [%s/%s]"
aname i count) t))
(spacemacs//redisplay)
(setq i (1+ i))
(unless
(and (not (configuration-layer//package-archive-local-pathp
archive))
(eq 'error
(with-timeout
(dotspacemacs-elpa-timeout
(progn
(display-warning
'spacemacs
(format
"\nError connection time out for %s repository!"
aname) :warning)
'error))
(condition-case err
(url-retrieve-synchronously apath)
('error
(display-warning
'spacemacs
(format
"\nError while contacting %s repository!"
aname) :warning)
'error)))))
(let ((package-archives (list archive)))
(package-refresh-contents)))))
(package-read-all-archive-contents)
(unless quiet (spacemacs-buffer/append "\n")))))
(defun configuration-layer/sync (&optional no-install)
"Synchronize declared layers in dotfile with spacemacs.
If NO-INSTALL is non nil then install steps are skipped."
(dotspacemacs|call-func dotspacemacs/layers "Calling dotfile layers...")
(setq dotspacemacs--configuration-layers-saved
dotspacemacs-configuration-layers)
(when (spacemacs-buffer//choose-banner)
(spacemacs-buffer//inject-version))
;; declare used layers then packages as soon as possible to resolve
;; usage and ownership
(configuration-layer/discover-layers)
(configuration-layer//declare-used-layers dotspacemacs-configuration-layers)
(configuration-layer//declare-used-packages configuration-layer--used-layers)
;; then load the functions and finally configure the layers
(configuration-layer//load-layers-files configuration-layer--used-layers
'("funcs.el"))
(configuration-layer//configure-layers configuration-layer--used-layers)
;; pre-filter some packages to save some time later in the loading process
(setq configuration-layer--used-distant-packages
(configuration-layer//get-distant-packages
configuration-layer--used-packages t))
;; load layers lazy settings
(configuration-layer/load-auto-layer-file)
;; install and/or uninstall packages
(unless no-install
(let ((packages
(append
;; install used packages
(configuration-layer/filter-objects
configuration-layer--used-distant-packages
(lambda (x)
(let ((pkg (configuration-layer/get-package x)))
(not (oref pkg :lazy-install)))))
;; also install all other packages if requested
(when (eq 'all dotspacemacs-install-packages)
(let (all-other-packages)
(dolist (layer (configuration-layer/get-layers-list))
(let ((configuration-layer--declared-layers-usedp nil)
(configuration-layer--load-packages-files t))
(configuration-layer/declare-layer layer)
(let* ((obj (configuration-layer/get-layer layer))
(pkgs (when obj (oref obj :packages))))
(configuration-layer/make-packages-from-layers
(list layer))
(dolist (pkg pkgs)
(let ((pkg-name (if (listp pkg) (car pkg) pkg)))
(add-to-list 'all-other-packages pkg-name))))))
(configuration-layer//get-distant-packages
all-other-packages nil))))))
(configuration-layer//install-packages packages)
(when (and (or (eq 'used dotspacemacs-install-packages)
(eq 'used-only dotspacemacs-install-packages))
(not configuration-layer-force-distribution)
(not configuration-layer-exclude-all-layers))
(configuration-layer/delete-orphan-packages packages))))
;; configure used packages
(configuration-layer//configure-packages configuration-layer--used-packages)
(configuration-layer//load-layers-files configuration-layer--used-layers
'("keybindings.el")))
(defun configuration-layer/load-auto-layer-file ()
"Load `auto-layer.el' file"
(let ((file (concat configuration-layer-directory "auto-layer.el")))
(when (file-exists-p file)
(spacemacs-buffer/message "Loading auto-layer file...")
(load-file file))))
(defun configuration-layer/create-layer ()
"Ask the user for a configuration layer name and the layer
directory to use. Create a layer with this name in the selected
layer directory."
(interactive)
(let* ((current-layer-paths (mapcar (lambda (dir) (expand-file-name dir))
(cl-pushnew
configuration-layer-private-layer-directory
dotspacemacs-configuration-layer-path)))
(other-choice "Another directory...")
(helm-lp-source
`((name . "Configuration Layer Paths")
(candidates . ,(append current-layer-paths
(list other-choice)))
(action . (lambda (c) c))))
(layer-path-sel (if (configuration-layer/layer-usedp 'ivy)
(ivy-read "Configuration layer path: "
(append current-layer-paths
(list other-choice)))
(helm :sources helm-lp-source
:prompt "Configuration layer path: ")))
(layer-path (cond
((string-equal layer-path-sel other-choice)
(read-directory-name (concat "Other configuration "
"layer path: ") "~/" ))
((member layer-path-sel current-layer-paths)
layer-path-sel)
(t
(error "Please select an option from the list"))))
(name (read-from-minibuffer "Configuration layer name: " ))
(layer-dir (concat layer-path "/" name)))
(cond
((string-equal "" name)
(message "Cannot create a configuration layer without a name."))
((file-exists-p layer-dir)
(message (concat "Cannot create configuration layer \"%s\", "
"this layer already exists.") name))
(t
(make-directory layer-dir t)
(configuration-layer//copy-template name "packages.el" layer-dir)
(when (y-or-n-p "Create readme?")
(configuration-layer//copy-template name "README.org" layer-dir))
(message "Configuration layer \"%s\" successfully created." name)))))
(defun configuration-layer//select-packages (layer-specs packages)
"Return the selected packages of LAYER-SPECS from given PACKAGES list."
(let* ((value (when (listp layer-specs)
(spacemacs/mplist-get layer-specs :packages)))
(selected-packages (if (and (not (null (car value)))
(listp (car value)))
(car value)
value)))
(cond
;; select packages
((and selected-packages
(not (memq (car selected-packages) '(all not))))
selected-packages)
;; unselect packages
((and selected-packages
(eq 'not (car selected-packages)))
(delq nil (mapcar (lambda (x)
(let ((pkg-name (if (listp x) (car x) x)))
(unless (memq pkg-name selected-packages)
pkg-name)))
packages)))
;; no package selections or all package selected
(t 'all))))
(defun configuration-layer/make-layer (layer-specs &optional obj usedp dir)
"Return a `cfgl-layer' object based on LAYER-SPECS.
If LOAD-PKGS is non-nil then load the `packages.el' file of the layer.
DIR is the directory where the layer is, if it is nil then search in the
indexed layers for the path."
(let* ((layer-name (if (listp layer-specs) (car layer-specs) layer-specs))
(obj (if obj obj (cfgl-layer (symbol-name layer-name)
:name layer-name)))
(dir (or dir (oref obj :dir))))
(if (or (null dir)
(and dir (not (file-exists-p dir))))
(configuration-layer//warning
"Cannot make layer %S without a valid directory!"
layer-name)
(let* ((dir (file-name-as-directory dir))
(disabled (when (listp layer-specs)
(spacemacs/mplist-get layer-specs :disabled-for)))
(enabled (if (and (listp layer-specs)
(memq :enabled-for layer-specs))
(spacemacs/mplist-get layer-specs :enabled-for)
'unspecified))
(variables (when (listp layer-specs)
(spacemacs/mplist-get layer-specs :variables)))
(packages-file (concat dir "packages.el"))
(packages
(if (and (or usedp configuration-layer--load-packages-files)
(file-exists-p packages-file))
(progn
(load packages-file)
(symbol-value (intern (format "%S-packages" layer-name))))
(oref obj :packages)))
(selected-packages (if packages
(configuration-layer//select-packages
layer-specs packages)
;; default value
'all)))
(oset obj :dir dir)
(when usedp
(oset obj :disabled-for disabled)
(oset obj :enabled-for enabled)
(oset obj :variables variables))
(when packages
(oset obj :packages packages)
(oset obj :selected-packages selected-packages))
obj))))
(defun configuration-layer/make-package (pkg layer-name &optional obj)
"Return a `cfgl-package' object based on PKG.
If OBJ is non nil then copy PKG properties into OBJ, otherwise create
a new object.
Properties that can be copied are `:location', `:step' and `:excluded'.
If TOGGLEP is nil then `:toggle' parameter is ignored."
(let* ((pkg-name (if (listp pkg) (car pkg) pkg))
(pkg-name-str (symbol-name pkg-name))
(layer (unless (eq 'dotfile layer-name)
(configuration-layer/get-layer layer-name)))
(min-version (when (listp pkg) (plist-get (cdr pkg) :min-version)))
(step (when (listp pkg) (plist-get (cdr pkg) :step)))
(toggle (when (listp pkg) (plist-get (cdr pkg) :toggle)))
(excluded (when (listp pkg) (plist-get (cdr pkg) :excluded)))
(location (when (listp pkg) (plist-get (cdr pkg) :location)))
(protected (when (listp pkg) (plist-get (cdr pkg) :protected)))
(init-func (intern (format "%S/init-%S"
layer-name pkg-name)))
(pre-init-func (intern (format "%S/pre-init-%S"
layer-name pkg-name)))
(post-init-func (intern (format "%S/post-init-%S"
layer-name pkg-name)))
(copyp (not (null obj)))
(obj (if obj obj (cfgl-package pkg-name-str :name pkg-name)))
(ownerp (or (and (eq 'dotfile layer-name)
(null (oref obj :owners)))
(fboundp init-func))))
(when min-version
(cfgl-package-set-property obj :min-version (version-to-list min-version)))
(when step (cfgl-package-set-property obj :step step))
(when toggle (cfgl-package-set-property obj :toggle toggle))
(cfgl-package-set-property obj :excluded
(and (configuration-layer/layer-usedp layer-name)
(or excluded (oref obj :excluded))))
(when location
(if (and (listp location)
(eq (car location) 'recipe)
(eq (plist-get (cdr location) :fetcher) 'local))
(cond
(layer (let ((path (expand-file-name
(format "%s%s/%s.el"
(configuration-layer/get-layer-local-dir
layer-name)
pkg-name-str pkg-name-str))))
(cfgl-package-set-property
obj :location `(recipe :fetcher file :path ,path))))
((eq 'dotfile layer-name)
;; TODO what is the local path for a packages owned by the dotfile?
nil))
(cfgl-package-set-property obj :location location)))
;; cannot override protected packages
(unless copyp
;; a bootstrap package is protected
(cfgl-package-set-property
obj :protected (or protected (eq 'bootstrap step)))
(when protected
(push pkg-name configuration-layer--protected-packages)))
(when ownerp
;; warn about mutliple owners
(when (and (oref obj :owners)
(not (memq layer-name (oref obj :owners))))
(configuration-layer//warning
(format (concat "More than one init function found for "
"package %S. Previous owner was %S, "
"replacing it with layer %S.")
pkg-name (car (oref obj :owners)) layer-name)))
;; last owner wins over the previous one
(object-add-to-list obj :owners layer-name))
;; check consistency between package and defined init functions
(unless (or ownerp
(eq 'dotfile layer-name)
(eq 'system layer-name)
(fboundp pre-init-func)
(fboundp post-init-func)
(oref obj :excluded))
(configuration-layer//warning
(format (concat "package %s not initialized in layer %s, "
"you may consider removing this package from "
"the package list or use the :toggle keyword "
"instead of a `when' form.")
pkg-name layer-name)))
;; check if toggle can be applied
(when (and (not ownerp)
(and (not (eq 'unspecified toggle))
toggle))
(configuration-layer//warning
(format (concat "Ignoring :toggle for package %s because "
"layer %S does not own it.")
pkg-name layer-name)))
(when (fboundp pre-init-func)
(object-add-to-list obj :pre-layers layer-name))
(when (fboundp post-init-func)
(object-add-to-list obj :post-layers layer-name))
obj))
(define-button-type 'help-dotfile-variable
:supertype 'help-xref
'help-function
(lambda (variable)
(with-current-buffer (find-file-noselect dotspacemacs-filepath)
(pop-to-buffer (current-buffer))
(goto-char (point-min))
;; try to exclude comments
(if (re-search-forward (format "^[a-z\s\\(\\-]*%s" variable)
nil 'noerror)
(beginning-of-line)
(message "Unable to find location in file"))))
'help-echo
(purecopy (concat "mouse-2, RET: "
"visit the Spacemacs dotfile where variable is defined.")))
(defun configuration-layer/describe-package (pkg-symbol
&optional layer-list pkg-list)
"Describe a package in the context of the configuration layer system."
(interactive
(list (intern
(completing-read "Package: " configuration-layer--used-packages))))
(let* ((pkg (configuration-layer/get-package pkg-symbol))
(owners (oref pkg :owners))
(owner (car owners)))
(with-help-window (help-buffer)
;; declaration location
(princ pkg-symbol)
(princ " is a package declared and configured ")
(cond
((eq 'dotfile owner)
(princ "by the variable `dotspacemacs-additional-packages' ")
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button 1 'help-variable
'dotspacemacs-additional-packages
dotspacemacs-filepath)))
(princ "in your `dotfile'.\n")
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button
1 'help-dotfile-variable 'dotspacemacs-additional-packages))))
((not (null owner))
(let* ((layer (configuration-layer/get-layer owner))
(path (concat (oref layer dir) "packages.el")))
(princ "by the layer `")
(princ owner)
(princ "'.\n")
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button
1 'help-function-def
(intern (format "%S/init-%S" owner pkg-symbol)) path)))))
(t
(princ "in an unknown place in the lisp parenthesis universe.\n")))
;; exclusion/protection
(if (oref pkg :protected)
(princ "\nThis package is protected and cannot be excluded.\n")
(when (oref pkg :excluded)
(princ "\nThis package is excluded and cannot be installed.\n")))
;; toggle
(unless (or (oref pkg :excluded) (eq t (oref pkg :toggle)))
(princ "\nA toggle is defined for this package, it is currently ")
(princ (if (cfgl-package-enabledp pkg t) "on" "off"))
(princ " because the following expression evaluates to ")
(princ (if (cfgl-package-enabledp pkg t) "t:\n" "nil:\n"))
(princ (oref pkg :toggle))
(princ "\n"))
(unless (oref pkg :excluded)
;; usage and installation
(if (not (configuration-layer/package-usedp pkg-symbol))
(princ "\nYou are not using this package.\n")
(princ "\nYou are using this package")
(if (or (memq (oref pkg :location) '(built-in local site))
(stringp (oref pkg :location)))
(princ ".\n")
(if (not (package-installed-p pkg-symbol))
(princ " but it is not yet installed.\n")
(princ ", it is currently installed ")
(if (featurep pkg-symbol)
(princ "and loaded.\n")
(princ "but it has not been loaded yet.\n")))))
(when (configuration-layer/package-lazy-installp pkg-symbol)
(princ
"\nThis package can be lazily installed using `auto-mode-alist'.\n")
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button 1 'help-variable 'auto-mode-alist)))
(when (assq pkg-symbol configuration-layer--lazy-mode-alist)
(princ (concat "Actually it will be installed when one of the "
"following files is opened: \n"))
(princ (cdr (assq pkg-symbol
configuration-layer--lazy-mode-alist)))
(princ "\n")))
;; source location
(let ((location (oref pkg :location)))
(cond
((eq 'built-in location)
(princ "\nThis is a built-in package distributed with Emacs.\n"))
((eq 'local location)
(let* ((layer (configuration-layer/get-layer owner))
(path (format "%slocal/%S" (oref layer dir) pkg-symbol)))
(princ (concat "\nThis is a local package whose source files "
"can be found in layer `"))
(princ owner)
(princ "'.\n")
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button 1 'help-package-def path)))))
((eq 'site location)
;; TODO find a way to find the location on disk and detect if it is
;; really installed
(princ "\nWhen used it must be installed by a third party.\n"))
((eq 'elpa location)
;; TODO find a way to find the ELPA repository
(princ "\nWhen used it is downloaded from an ELPA repository.\n"))
((and (listp location) (eq 'recipe (car location)))
(princ (concat "\nWhen used it is downloaded using `quelpa' "
"with the following recipe:\n"))
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button
1 'help-url "https://github.com/quelpa/quelpa")))
(princ location)
(princ "\n"))))
;; pre/post init functions
(when (or (oref pkg pre-layers) (oref pkg post-layers))
(princ (concat "\nAdditional configuration for this package "
"can be found in the following "))
(if (null layer-list)
(princ "used layers:\n")
(princ "layers:\n"))
(when (oref pkg pre-layers)
(princ "(pre-init) ")
(dolist (layer-sym (sort (oref pkg pre-layers) 'string<))
(let* ((layer (configuration-layer/get-layer layer-sym))
(path (concat (oref layer dir) "packages.el")))
(princ (concat "`" (symbol-name layer-sym) "'"))
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button
1 'help-function-def
(intern (format "%S/pre-init-%S" layer-sym pkg-symbol))
path))))
(princ " "))
(princ "\n"))
(when (oref pkg post-layers)
(princ "(post-init) ")
(dolist (layer-sym (sort (oref pkg post-layers) 'string<))
(let* ((layer (configuration-layer/get-layer layer-sym))
(path (concat (oref layer dir) "packages.el")))
(princ (concat "`" (symbol-name layer-sym) "'"))
(with-current-buffer standard-output
(save-excursion
(re-search-backward "`\\([^`']+\\)'" nil t)
(help-xref-button
1 'help-function-def
(intern (format "%S/post-init-%S" layer-sym pkg-symbol))
path))))
(princ " "))
(princ "\n"))))
(princ (concat "\nClick [here] to display an Emacs description "
"for this package.\n"))
(with-current-buffer standard-output
(save-excursion
(re-search-backward "\\(\\[.+\\]\\)" nil t)
(help-xref-button 1 'help-package pkg-symbol))))))
(defun configuration-layer//warning (msg &rest args)
"Display MSG as a warning message in buffer `*Messages*'.
If `configuration-layer--inhibit-warnings' is non nil then this function is a
no-op."
(unless configuration-layer--inhibit-warnings
(apply 'spacemacs-buffer/warning msg args)))
(defun configuration-layer//add-layer (layer &optional usedp)
"Add a LAYER object to the system.
USEDP non-nil means that PKG is a used layer."
(let ((layer-name (oref layer :name)))
(puthash layer-name layer configuration-layer--indexed-layers)
(when usedp
(add-to-list 'configuration-layer--used-layers layer-name))))
(defun configuration-layer/remove-layers (layer-names)
"Remove layers with LAYER-NAMES from used layers."
(mapc 'configuration-layer/remove-layer layer-names))
(defun configuration-layer/remove-layer (layer-name)
"Remove an used layer with name LAYER-NAME."
(setq configuration-layer--used-layers
(delq layer-name configuration-layer--used-layers)))
(defun configuration-layer/get-layer (layer-name)
"Return a layer object with name LAYER-NAME.
Return nil if layer object is not found."
(when (ht-contains? configuration-layer--indexed-layers layer-name)
(ht-get configuration-layer--indexed-layers layer-name)))
(defun configuration-layer/get-layers-list ()
"Return a list of all discovered layer symbols."
(ht-keys configuration-layer--indexed-layers))
(defun configuration-layer/get-layer-local-dir (layer)
"Return the value of SLOT for the given LAYER."
(let ((obj (ht-get configuration-layer--indexed-layers layer)))
(when obj (concat (oref obj :dir) "local/"))))
(defun configuration-layer/get-layer-path (layer)
"Return the path for LAYER symbol."
(let ((obj (ht-get configuration-layer--indexed-layers layer)))
(when obj (oref obj :dir))))
(defun configuration-layer//add-package (pkg &optional usedp)
"Add a PKG object to the system.
USEDP non-nil means that PKG is a used package."
(let ((pkg-name (oref pkg :name)))
(puthash pkg-name pkg configuration-layer--indexed-packages)
(when usedp
(add-to-list 'configuration-layer--used-packages pkg-name))))
(defun configuration-layer/get-packages-list ()
"Return a list of all package symbols."
(ht-keys configuration-layer--indexed-packages))
(defun configuration-layer/get-package (pkg-name)
"Return a package object with name PKG-NAME.
Return nil if package object is not found."
(when (ht-contains? configuration-layer--indexed-packages pkg-name)
(ht-get configuration-layer--indexed-packages pkg-name)))
(defun configuration-layer//sort-packages (packages)
"Return a sorted list of PACKAGES objects."
(sort packages (lambda (x y) (string< (symbol-name x) (symbol-name y)))))
(defun configuration-layer/make-all-packages (&optional skip-layer-discovery)
"Create objects for _all_ packages supported by Spacemacs.
If SKIP-LAYER-DISCOVERY is non-nil then do not check for new layers."
(let ((all-layers (configuration-layer/get-layers-list))
(configuration-layer--load-packages-files t)
(configuration-layer--package-properties-read-onlyp t)
(configuration-layer--inhibit-warnings t))
(unless skip-layer-discovery
(configuration-layer/discover-layers))
(configuration-layer/declare-layers all-layers)
(configuration-layer/make-packages-from-layers all-layers)))
(defun configuration-layer/make-packages-from-layers
(layer-names &optional usedp)
"Read the package lists of layers with name LAYER-NAMES and create packages.
USEDP if non-nil indicates that made packages are used packages.
DOTFILE if non-nil will process the dotfile `dotspacemacs-additional-packages'
variable as well."
(dolist (layer-name layer-names)
(let ((layer (configuration-layer/get-layer layer-name)))
(dolist (pkg (cfgl-layer-get-packages layer 'with-props))
(let* ((pkg-name (if (listp pkg) (car pkg) pkg))
(obj (configuration-layer/get-package pkg-name)))
(setq obj (configuration-layer/make-package pkg layer-name obj))
(configuration-layer//add-package
obj (and (cfgl-package-get-safe-owner obj) usedp)))))))
(defun configuration-layer/make-packages-from-dotfile (&optional usedp)
"Read the additonal packages declared in the dotfile and create packages.
USEDP if non-nil indicates that made packages are used packages."
(dolist (pkg dotspacemacs-additional-packages)
(let* ((pkg-name (if (listp pkg) (car pkg) pkg))
(obj (configuration-layer/get-package pkg-name)))
(if obj
(setq obj (configuration-layer/make-package pkg 'dotfile obj))
(setq obj (configuration-layer/make-package pkg 'dotfile)))
(configuration-layer//add-package obj usedp)))
(dolist (xpkg dotspacemacs-excluded-packages)
(let ((obj (configuration-layer/get-package xpkg)))
(unless obj
(setq obj (configuration-layer/make-package xpkg 'dotfile)))
(configuration-layer//add-package obj usedp)
(cfgl-package-set-property obj :excluded t))))
(defun configuration-layer/lazy-install (layer-name &rest props)
"Configure auto-installation of layer with name LAYER-NAME."
(declare (indent 1))
(when (configuration-layer//lazy-install-p layer-name)
(let ((extensions (spacemacs/mplist-get props :extensions))
(interpreter (plist-get props :interpreter)))
(when (configuration-layer/layer-usedp layer-name)
(let* ((layer (configuration-layer/get-layer layer-name))
(package-names (when layer (cfgl-layer-owned-packages layer))))
;; set lazy install flag for a layer if and only if its owned
;; distant packages are all not already installed
(let ((lazy
(or (eq 'all dotspacemacs-enable-lazy-installation)
(cl-reduce
(lambda (x y) (and x y))
(mapcar
(lambda (p)
(let ((pkg (configuration-layer/get-package p)))
(or (not (eq layer-name (car (oref pkg :owners))))
(null (package-installed-p
(oref pkg :name))))))
package-names)
:initial-value t))))
(oset layer :lazy-install lazy)
(dolist (pkg-name package-names)
(let ((pkg (configuration-layer/get-package pkg-name)))
(cfgl-package-set-property pkg :lazy-install lazy))))))
;; configure `auto-mode-alist'
(dolist (x extensions)
(let ((ext (car x))
(mode (cadr x)))
(add-to-list 'configuration-layer--lazy-mode-alist (cons mode ext))
(add-to-list
'auto-mode-alist
`(,ext . (lambda ()
(configuration-layer//auto-mode
',layer-name ',mode))))
))
;; configure `interpreter-mode-alist'