-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpel--install.el
More file actions
1430 lines (1281 loc) · 59.6 KB
/
Copy pathpel--install.el
File metadata and controls
1430 lines (1281 loc) · 59.6 KB
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
;;; pel--install.el --- PEL file and package installation and lazy loading. -*- lexical-binding: t; -*-
;; Created : Thursday, March 12 2026.
;; Author : Pierre Rouleau <prouleau001@gmail.com>
;; Time-stamp: <2026-05-13 14:03:57 EDT, updated by Pierre Rouleau>
;; This file is part of the PEL package.
;; This file is not part of GNU Emacs.
;; Copyright (C) 2026 Pierre Rouleau
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; --------------------------------------------------------------------------
;;; Commentary:
;;
;;
;; Lazy loading and package installation:
;; - `pel-install-github-file'
;; - `pel-install-github-files'
;; - `pel--install-github-files'
;; - `pel-install-files'
;; - `pel-install-file'
;; - `pel-url-copy-file'
;;
;; - `pel-soft-require-or-warn'
;; - `pel-require'
;; - `pel-package-installed-p'
;; - `pel-package-install'
;; - `pel--require-warn'
;;
;; - `pel-ensure-package-elpa'
;; - `pel--ensure-pkg-elpa'
;; - `pel--pin-package'
;; - `pel-archive-exists'
;; - `pel--package-ensure-elpa'
;; - `pel--package-install-elpa'
;;
;; Lazy loading and package installation:
;;
;; The first set of functions and macros provide mechanism to require, load,
;; autoload and byte-compiler declaration facilities.
;;
;;
;; @ `pel-require-at-load'
;; - `pel--require-at-load'
;; @ `pel-require-after-init'
;; - `pel--require-after-init'
;; @ `pel-eval-after-load'
;; @ `pel-set-auto-mode'
;; @ `pel-autoload-file'
;; @ `pel-declare-file'
;; Speedbar Support
;; - `pel-add-speedbar-extension'
;;
;; Major Mode Configuration
;; @ `pel-setup-major-mode'
;; @ `pel-config-major-mode'
;; @ `pel-local-set-f12-M-f12'
;; - `pel--auto-activate-fly'
;; - `pel--set-indent-control-variables'
;; - `pel-treesit-remap-available-for'
;; @ `pel--mode-hook-maybe-call'
;; @ `pel-eval-after-load'
;;
;;; --------------------------------------------------------------------------
;;; Dependencies:
;;
(require 'pel--base)
(require 'pel--indent) ; use: `pel-tab-width-control-variables'.
(require 'pel--options) ; use: `pel-use-tree-sitter'
(eval-when-compile
(require 'pel--macros)) ; use `pel-append-to' to generate code.
;;; --------------------------------------------------------------------------
;;; Code:
;;
;;* Package and file installation and lazy loading
;; ==============================================
;;
;; The first set install files downloaded from the internet with a specific
;; URL inside PEL utils directory.
;; These functions do not depend on Emacs package facility. Therefore they
;; can be used any time, including when PEL operates in fast-startup mode.
;;
;; - `pel-install-file' downloads and installs one file.
;; - `pel-install-files' downloads and installs one or several files from the
;; same web site.
;;
;; -> `pel-install-files'
;; -> `pel-install-file'
;; - `pel-url-copy-file'
;; The next set of functions does the same thing but provide logic
;; specific to build GitHub or Gitlab URLs.
;; They install the files inside PEL utils directory.
;; These functions do not depend on Emacs package facility. Therefore they
;; can be used any time, including when PEL operates in fast-startup mode.
;;
;; - `pel-install-github-files' downloads and installs one or several files
;; from GitHub specified user project branch.
;; - `pel-install-github-file' downloads and installs one file. That file
;; may have a name that differs from the URL used to download it. This is
;; mostly used when a file name has a character that cannot be part of a URL
;; and must be encoded differently.
;; - `pel-install-gitlab-file' downloads, installs and compile one file from
;; Gitlab.
;;
;; -> `pel-install-github-file'
;; . `pel-install-file'
;; -> `pel-install-github-files'
;; . `pel-install-files'
;; . `pel-install-file'
;; -> `pel-install-gitlab-file'
;; . `pel-install-file'
;; The next set of functions and macros provide logic to install Elpa
;; compliant packages when PEL is not running in fast-startup mode
;; and to require Emacs packages.
;;
;; -> - `pel-require'
;; - `pel-package-installed-p'
;; - `pel-package-install'
;; `pel-install-github-file'
;; -> @ `pel-ensure-package-elpa'
;; - `pel--ensure-pkg-elpa'
;; - `pel--pin-package'
;; - `pel-archive-exists'
;; - `pel--package-ensure-elpa'
;; - `pel--package-install-elpa'
;;
;; The next set of functions and macros provide logic to install
;; packages via quelpa. This allows installation of multi-file packages
;; inside the elpa directory as if they were elpa-compliant.
;; However, they install nothing when PEL runs in fast-startup mode.
;;
;; -> @ `pel-quelpa-install'
;; - `pel--quelpa-install'
;; The next set of macros, defined in pel--keys.macros.el, control the
;; loading and evaluation of features and code, mostly used in pel_keys.el
;;
;; @ `pel-require-at-load'
;; - `pel--require-at-load'
;; @ `pel-require-after-init'
;; - `pel--require-after-init'
;; @ `pel-eval-after-load'
;; @ `pel-set-auto-mode'
;; @ `pel-autoload-file'
;; @ `pel-declare-file'
;; [:todo 2026-03-09, by Pierre Rouleau: Modify all functions that download
;; and install files to return non-nil on success, nil on error to allow
;; pel_keys.el code to proceed only when the file is either present or just
;; downloaded. Ideally the functions would return 'present or 'downloaded and
;; the pel_keys.el code would not map commands when the command failed and the
;; file is not installed locally. There would not be any exception from
;; failing installation just error warnings displayed describing what went
;; wrong. This way problems would not stop Emacs initialization. For the
;; moment coding issues or permission failures may stop the
;; initialization.
;; However do this only once the fast startup works on all
;; version of Emacs as the extra code will slow down
;; startup a little.]
(defun pel-isa-http-404-error-p (&optional buffer)
"Return t if the BUFFER content is a 404 HTTP status error, nil otherwise."
(with-current-buffer (or buffer (current-buffer))
(save-excursion
(goto-char (point-min))
(forward-line 4)
(let ((limit (point)))
(goto-char (point-min))
(pel-as-boolean
(re-search-forward
"\
\\b404\\b\\(?:\\.[[:digit:]][[:digit:]]?\\)?[[:space:]]?\
\\(?::?[[:space:]]*Not Found\\b\\)"
limit
'noerror))))))
(defun pel-url-copy-file (url newname &optional ok-if-already-exists)
"Copy URL to NEWNAME. Both arguments must be strings.
Same as `url-copy-file' but detects URL to non-existing file reported as a
HTTP 404 error by the server.
If the NEWNAME file already exists, download it again when
OK-IF-ALREADY-EXISTS is non-nil otherwise treats this as an error.
On success return NEWNAME, the name of the created file.
On operation error, display a descriptive :error warning message and return
nil. Raise an error if the `url-copy-file' is not bound.
That should never happen if Emacs is installed properly."
(require 'url-handlers nil 'noerror)
(if (fboundp 'url-copy-file)
;; Try to download the file identified by the URL.
;; That function does not detect invalid URLS so we could get a "404:
;; Not Found"
(let ((tmp-fname (make-temp-file "pel-url-copy-file"))
(error-msg nil))
(unwind-protect
(condition-case err
;; `url-copy-file' does not complain when the server replies
;; with a "404: Not Found"; it simply stores it inside the
;; created file.
(if (url-copy-file url tmp-fname t)
;; Check that the file was properly downloaded by checking
;; if its content is "404: Not Found". If it is: set
;; error-msg with a descriptive problem.
(progn
(with-temp-buffer
(insert-file-contents tmp-fname)
(when (pel-isa-http-404-error-p (current-buffer))
(setq error-msg
(format
"Received 404 error for requested URL: %s"
url))))
(unless error-msg
(copy-file tmp-fname newname
;; Prevent prompt if `ok-if-already-exists'
;; was passed a number.
(pel-as-boolean ok-if-already-exists))))
(setq error-msg (format "Nothing received for URL: %s" url)))
(error
(setq error-msg
(format "Exception detected in url-copy-file: %s %s"
(car err)
(cdr err)))))
(when (file-exists-p tmp-fname)
(delete-file tmp-fname)))
;; After operation check if there was any error reported.
;; On success return the name of the created file.
;; On error: display an :error warning and return nil.
(if error-msg
(progn
(display-warning 'pel-url-copy-file
(format "Error installing URL %s to %s:\n%s"
url newname
error-msg)
:error)
nil)
;; success: return name of created file.
newname))
;; url-copy-file is not bound
(error "\
url-handlers.el `url-copy-file' not bound in pel-url-copy-file.\
Can't install anything!")))
(defun pel-install-file (url fname &optional refresh)
"Download, install a file FNAME from URL into PEL\\='s utility directory.
On success, byte compile that file and when Emacs use native compilation
then also build the native-compiled .eln file for it.
The utility directory is the \\='utils\\=' sub-directory of the Emacs
directory identified by the Emacs variable `user-emacs-directory'.
If this directory does not exist, the function creates it.
If the file already exists in the destination, no download
is done unless REFRESH is non-nil.
Returns non-nil when file was downloaded, nil otherwise.
Permission errors are raised but install failures are just reported
by warning to prevent init from failing."
(let* ((utils-dirname (file-name-as-directory
(expand-file-name "utils" user-emacs-directory)))
(target-fname (expand-file-name fname utils-dirname))
(subdir (file-name-directory fname))
(downloaded nil))
(if (file-in-directory-p target-fname utils-dirname)
(progn
;; create utils directory and sub-directory if required
(unless (file-exists-p utils-dirname)
(make-directory utils-dirname 'make-parents-if-needed))
(when subdir
(setq subdir (expand-file-name subdir utils-dirname))
(unless (file-exists-p subdir)
(make-directory subdir 'make-parents-if-needed)))
(when (or (not (file-exists-p target-fname))
refresh)
(message "Downloading %s" url)
(setq downloaded (pel-url-copy-file url target-fname refresh))
(when (and downloaded
(equal (file-name-extension target-fname) "el"))
(message "Byte compiling it to %s" target-fname)
(byte-compile-file target-fname)
(when (and (fboundp 'native-comp-available-p)
(fboundp 'native-compile-async)
(native-comp-available-p))
(native-compile-async target-fname)))))
(display-warning 'pel-install-file
(format "\
Cannot install %s inside PEL utils: it would be stored outside utils!\n\
Fix the file specification in pel_keys.el!" fname)
:error))
downloaded))
(defun pel-install-files (url-base fnames &optional refresh)
"Download & install files identified by their URL-BASE and FNAMES.
The URL-BASE is the common URL for the location of all files.
The FNAMES is a file name string or list of file name strings
identifying the name of the file located at that URL-BASE and
also the name of the file save locally into the PEL Emacs \\='utils\\='
directory. See `pel-install-file' for more info.
If a file already exists in the destination, no download
is done unless REFRESH is non-nil.
Permission errors are raised but install failures are just reported
by warning to prevent init from failing."
(dolist (fname (pel-list-of fnames))
(pel-install-file (pel-url-join url-base fname)
fname
refresh)))
;; -------
(defun pel-install-github-files (user-project-branch
fnames
&optional refresh)
"Download & install FNAMES from GitHub USER-PROJECT-BRANCH.
- USER-PROJECT-BRANCH is a GitHub user/project/branch name path
string. Something like \"pierre-rouleau/pel/master\".
If a depot file is stored in a depot sub-directory, include the
path of depot directory inside USER-PROJECT-BRANCH.
- FNAMES is a file name string or list of file names.
If a file already exists in the destination, no download is done
unless REFRESH is non-nil.
Permission errors are raised but install failures are just reported
by warning to prevent init from failing."
(pel-install-files (pel-url-join "https://raw.githubusercontent.com"
user-project-branch)
fnames
refresh))
(defun pel-install-github-file (user-project-branch
fname
&optional url-fname refresh)
"Download & install FNAME from GitHub USER-PROJECT-BRANCH/URL-FNAME.
- USER-PROJECT-BRANCH is a GitHub user/project/branch name path
string. Something like \"pierre-rouleau/pel/master\".
If a depot file is stored in a depot sub-directory, include the
path of depot directory inside USER-PROJECT-BRANCH.
- FNAME is the name of the file, with its .el extension.
- URL-FNAME is the name of the file as it appears in the
URL. This argument is only required when it differs from FNAME.
If a file already exists in the destination, no download
is done unless REFRESH is non-nil.
The function returns t if the file was downloaded, nil otherwise.
Permission errors are raised but install failures are just reported
by warning to prevent init from failing."
(pel-install-file (pel-url-join "https://raw.githubusercontent.com"
user-project-branch
(or url-fname fname))
fname
refresh))
;; --
(defun pel-install-gitlab-file (gitlab-user gitlab-project fname
&optional branch refresh)
"Download & install FNAME from Gitlab user and project into PEL utils.
GITLAB-USER is the name of Gitlab user.
GITLAB-PROJECT is the name of Gitlab project.
BRANCH is optional: it identifies the repo branch, and is \"master\" if not
specified.
If a file already exists in the destination, no download
is done unless REFRESH is non-nil.
The function returns t if the file was downloaded, nil otherwise.
Permission errors are raised but install failures are just reported
by warning to prevent init from failing."
(pel-install-file (format "https://gitlab.com/%s/%s/-/raw/%s/%s"
gitlab-user
gitlab-project
(or branch "master")
fname)
fname
refresh))
;; -------
(defun pel-package-install (pkg)
"Install package PKG, return t on success, nil otherwise.
PKG must be a symbol naming one of the available packages in one
of the archives listed in variable `package-archives'.
If the first attempt fails, the function refreshes the package
list and tries again. This prevents failing to install a package
when its version identified in the package list identifies an
obsolete version no longer supported by the Elpa archive site.
If the second attempt fails, then a error-level warning is logged
and the function returns nil"
;; package.el is part of Emacs but it's not loaded until required.
;; Load it lazily and check if the required functions are bound
;; to prevent byte-compiler warnings.
(let ((package-was-installed nil))
(if (and (require 'package nil 'noerror)
(fboundp 'package-install))
(condition-case-unless-debug err
(progn
(package-install pkg)
(setq package-was-installed t))
(error
(if (and (fboundp 'package-refresh-contents)
(fboundp 'package-read-all-archive-contents)
(boundp 'package-pinned-packages))
(progn
(message (format "Failed to install %s: %s
Refreshing package list and re-trying..."
pkg
(error-message-string err)))
(package-refresh-contents)
(condition-case-unless-debug err
(progn
(when (assoc pkg
(bound-and-true-p package-pinned-packages))
(package-read-all-archive-contents))
(package-install pkg)
(setq package-was-installed t))
(error
(display-warning
'pel-package-install
(format "After refresh, failed to install %s: %s"
pkg
(error-message-string err))
:error))))
(display-warning
'pel-package-install
(format "The package.el is not loaded properly.
Failed installation of %s.
Please verify the validity of your package-archives setup!"
pkg)
:error))))
(display-warning
'pel-package-install
(format "package-install is void. Can't install %s!
Please verify the validity of your package-archives setup!"
pkg)
:error))
package-was-installed))
(defun pel-package-installed-p (feature)
"Return t if FEATURE is installed, nil otherwise.
Load the package library if that's not already done."
(if (and (require 'package nil 'noerror)
(fboundp 'package-installed-p))
(package-installed-p feature)
(display-warning 'pel-package-installed-p
"Failed loading package.el to use package-installed-p!"
:error)
nil))
(defmacro pel-soft-require-or-warn (feature &rest body )
"Soft require FEATURE (unquoted symbol), display warning on failure.
if BODY is specified, execute it on success."
(declare (indent 1))
(let ((warning-name (intern (format "pel-use-%s" feature)))
(warning-text (format "Can't load %s; skipping." feature)))
(if body
`(if (require (quote ,feature) nil 'noerror)
(progn
,@body)
(display-warning (quote ,warning-name)
,warning-text
:error))
`(unless (require (quote ,feature) nil 'noerror)
(display-warning (quote ,warning-name)
,warning-text
:error)))))
(defun pel--require-warn (message)
"Utility - display warning with MESSAGE in `pel-require'."
(display-warning 'pel-require message :warning))
(defun pel-require (feature &optional package with-pel-install
fname url-fname)
"Load FEATURE if not already loaded, optionally try to install PACKAGE.
If the FEATURE is not already loaded, require it. If that fails,
then attempt to install the package if requested by the arguments and try
require the feature again.
FEATURE: a symbol, the feature to load if not already loaded.
PACKAGE: one of the following:
- nil: If not loaded, don't attempt to install; simply
display a warning that the feature is not loaded.
- `:install-when-missing': If not loaded attempt to install a package with
the same name as the feature.
- any other symbol: If FEATURE is not loaded, attempt to install the
package with the PACKAGE name.
If FEATURE is not loaded and a package must be installed (as specified by
PACKAGE argument), WITH-PEL-INSTALL describes how to install the package:
- nil: Install PACKAGE with `pel-package-install'.
- a string: Install PACKAGE with `pel-install-github-file'. In that case,
WITH-PEL-INSTALL must be the USER-PROJECT_BRANCH, and
the FNAME is the name of the .el file and URL-FNAME is the
explicit file URL if needed.
All 3 are passed to `pel-install-github-file'.
Generate a warning when failing to load the FEATURE, skipping requested
installation due to running in fast startup mode or failing to install
package.
Return the loading state of the FEATURE."
(unless (featurep feature)
(let ((feature-is-loaded (require feature nil 'noerror))
(try-final-load nil)
(install-failed nil))
(unless feature-is-loaded
;; required failed
(if package
(if (pel-in-fast-startup-p)
;; in fast startup don't attempt to install anything.
(pel--require-warn
(format
"%s not loaded, but skip installing %s during fast startup."
feature package))
;; in normal mode attempt to install package if requested
(let ((package-to-install (if (eq package :install-when-missing)
feature
package)))
(if with-pel-install
;; install using specified GitHub repository
(progn
(if (pel-install-github-file with-pel-install
fname url-fname)
(setq try-final-load t)
(setq install-failed t)))
;; install an elpa-compliant package if not already present
(if (pel-package-installed-p package-to-install)
(progn
(pel--require-warn
(format
"Failed loading %s (but package %s is installed!)"
feature package-to-install)))
(if (pel-package-install package-to-install)
(setq try-final-load t)
(setq install-failed t))))
(when install-failed
(pel--require-warn
(format "%s load failed. Tried installing %s also failed."
feature package-to-install)))
(when try-final-load
(require feature nil 'noerror)
(unless (featurep feature)
(pel--require-warn
(format
"Failed loading %s even after installing package %s!"
feature package-to-install))))))
(pel--require-warn
(format "Failed loading %s. No install requested." feature))))))
(featurep feature))
;; ---------------------------------------------------------------------------
;;
;; The following code defines the `pel-ensure-package-elpa' macro that PEL
;; uses to install Elpa-compliant packages.
;;
;; This is done to:
;; - Install a package when the appropriate pel-use variable is turned on.
;; - Does NOT install when byte-compiling the code.
;; - Does NOT install when PEL is operating in fast startup mode.
;; - Allow the selection of a Elpa site, just as the use-package :pin does.
;;
;; The `pel-ensure-package-elpa' macro uses the `pel--ensure-pkg-elpa'
;; function to reduce the amount of code generated and executed to the expense
;; of one function call.
;;
;; Credit: the package installation code was influenced by the
;; use-package library found at https://github.com/jwiegley/use-package
;; and now part of Emacs.
;;
;; PEL does not use the use-package library in attempt to reduce the overhead
;; and the startup time further.
(defun pel-archive-exists (archive)
"Return non-nil if specified package ARCHIVE is being used, nil otherwise.
The ARCHIVE argument may be a string or a symbol.
To get the URL of the existing package, take the cdr of the returned value."
(if (or (boundp 'package-archives)
(and (require 'package nil 'noerror)
(boundp 'package-archives)))
(with-no-warnings ; Emacs 30 Byte compiler does not see protection...
(assoc (pel-as-string archive) package-archives))
(display-warning 'pel-archive-exists
"package.el is not loaded: package-archives is void"
:error)
nil))
(defvar pel--pinned-packages nil
"List of packages that are associated with a specific Elpa archive.")
(defvar package-pinned-packages) ; prevent warning when accessing package var.
(defun pel--pin-package (package archive)
"Pin PACKAGE (a symbol) to ARCHIVE (a symbol or string)."
(let ((archive-name (pel-as-string archive)))
(if (pel-archive-exists archive-name)
(progn
(add-to-list 'pel--pinned-packages
(cons package (pel-as-string archive)))
(add-to-list 'package-pinned-packages
(cons package (pel-as-string archive))))
(error "\
Archive '%S' requested for package '%S' is not listed in package-archives!"
archive package)))
(unless (bound-and-true-p package--initialized)
(package-initialize t)))
(defun pel--package-install-elpa (package)
"Install PACKAGE (a symbol). On failure retry once and issue an error.
Packages in the Elpa archive sites are regularly updated and old
versions purged. Requesting an old version of a package may
occur when our local list is outdated.
When a failure occurs, refresh the local list and try again, also
generate a warning that identifies the error."
(declare-function package-install "package")
(declare-function package-refresh-contents "package")
(declare-function package-read-all-archive-contents "package")
(defvar package-archive-contents)
;;
(condition-case-unless-debug err
(package-install package)
(error
(message "Error trying to install %s : %s. \
Refreshing package list and trying again." package err)
(package-refresh-contents)
(package-read-all-archive-contents)
(if (assoc package package-archive-contents)
(package-install package)
(display-warning 'pel--install-package
(format "Failed locating package %s" package)
:error)))))
(defun pel--package-ensure-elpa (package)
"Install specified Emacs Lisp PACKAGE (a symbol).
DO NOT use this function directly inside your code.
Use the macro `pel-ensure-package-elpa' instead.
When a failure occurs, refresh the local list and try again, also
generate a warning that identifies the error."
(if (and (require 'package nil 'noerror)
(boundp 'package-archive-contents)
(fboundp 'package-read-all-archive-contents))
(condition-case-unless-debug err
(progn
(when (assoc package (bound-and-true-p
pel--pinned-packages))
(package-read-all-archive-contents))
(if (assoc package package-archive-contents)
(pel--package-install-elpa package)
(package-refresh-contents)
(when (assoc package (bound-and-true-p
pel--pinned-packages))
(package-read-all-archive-contents))
(pel--package-install-elpa package))
t)
(error
(display-warning 'pel-ensure-package-elpa
(format "Failed trying to install %s: %s"
package (error-message-string err))
:error)))
(display-warning 'pel-ensure-package-elpa
(format
"Cannot install %s: package.el is not properly loaded."
package)
:error)))
(defun pel--ensure-pkg-elpa (pkg &optional elpa-site)
"Install package PKG (a symbol) possibly from pinned ELPA-SITE.
If ELPA-SITE is non-nil it should be a symbol or string holding the name
of one of the Elpa repositories identified in the variable
`package-archives'.
When PEL operates in fast startup, nothing is done."
(unless (pel-in-fast-startup-p)
(when elpa-site
(pel--pin-package pkg elpa-site))
(pel--package-ensure-elpa pkg)))
(defmacro pel-ensure-package-elpa (pkg &optional from: pinned-site)
"Install package named PKG, optionally from specified PINNED-SITE.
PKG must be an unquoted symbol.
FROM: is just a tag.
When PINNED-SITE (a unquoted symbol) is specified use this as the Elpa
repository, which must be listed in the variable `package-archives'.
The FROM: argument must be present. It is cosmetics only.
The package list is refreshed before attempting installation to
prevent trying to install an obsolete version of a package that
is no longer present on the Elpa site.
When a failure occurs, refresh the local list and try again, also
generate a warning that identifies the error.
However, when PEL operates in fast startup, the macro creates no code."
(declare (indent 1))
(ignore from:)
(let ((pin-site-name (when pinned-site (symbol-name pinned-site))))
`(unless (or (pel-in-fast-startup-p)
(pel-package-installed-p (quote ,pkg)))
(pel--ensure-pkg-elpa (quote ,pkg) ,pin-site-name))))
;; -------
(defun pel--quelpa-install (package quelpa-specs)
"Install PACKAGE using specified QUELPA-SPECS.
Don't install it if already installed."
(unless (or (pel-in-fast-startup-p)
(pel-package-installed-p package))
(if (fboundp 'quelpa)
(quelpa quelpa-specs)
(display-warning
'pel-quelpa-install
(format "Please activate pel-use-quelpa to install %S"
quelpa-specs)))))
(defmacro pel-quelpa-install (quelpa-specs)
"Install the package identified by QUELPA-SPECS.
QUELPA-SPECS is an unquoted form that identifies the package
to install and how to install it. See `quelpa' documentation.
Don't install it if already installed or PEL in fast startup."
(declare (indent 1))
(if (listp quelpa-specs)
(let ((package (car quelpa-specs)))
(if (symbolp package)
`(pel--quelpa-install (quote ,package) (quote (,@quelpa-specs)))
(byte-compile-warn
"Invalid quelpa-spec: first element not a symbol: %S"
package)))
(byte-compile-warn "Invalid quelpa-specs: %S" quelpa-specs)
nil))
;; ---------------------------------------------------------------------------
;; Delay activation of Modes after processing of command line arguments
;; --------------------------------------------------------------------
(eval-and-compile
(defmacro pel-after-startup-do (&rest body)
"Schedule BODY execution after processing of command line arguments."
`(add-hook 'emacs-startup-hook
(lambda ()
,@body)
:append)))
;; ---------------------------------------------------------------------------
(defun pel--require-at-load (feature)
"Require specified FEATURE when loading only, not when compiling.
FEATURE must be a quoted symbol.
This is normally used by the macro `pel-require-at-load'."
(unless (require feature nil :noerror)
(display-warning 'pel-require-at-load
(format "Failed loading %s" feature)
:error)))
(defmacro pel-require-at-load (feature)
"Require specified FEATURE when loading only, not when compiling.
FEATURE must be an unquoted symbol representing the required
feature."
`(cl-eval-when 'load
(pel--require-at-load (quote ,feature))))
;; --
(defun pel--require-after-init (feature secs)
"Require specified FEATURE some SECS after initializing Emacs.
FEATURE must be a quoted symbol.
This is normally used by the macro `pel-require-after-init'."
(run-with-idle-timer secs nil
(function require)
feature nil :noerror))
(defmacro pel-require-after-init (feature secs)
"Require specified FEATURE some SECS after initializing Emacs.
Don't require the feature when compiling.
FEATURE must be an unquoted symbol representing the required
feature.
SECS may be an integer, a floating point number, or the internal
time format returned by, e.g., `current-idle-time’."
`(cl-eval-when 'load
(pel--require-after-init (quote ,feature) ,secs)))
;; --
(defmacro pel-set-auto-mode (mode for: &rest regexps)
"Activate automatic MODE for the list of file REGXEPS.
The FOR: argument is a cosmetic separator.
MODE must be an un-quoted symbol.
FOR: separator must be present. It is cosmetic only.
REGEXPS is on or several regular expression strings."
(declare (indent 0))
(ignore for:)
(let ((forms ()))
(setq forms
(dolist (regxp regexps (nreverse forms))
(push `(add-to-list 'auto-mode-alist
(quote (,regxp . ,mode)))
forms)))
`(progn
,@forms)))
;; --
(defmacro pel-autoload-file (fname for: &rest commands)
"Schedule the autoloading of FNAME for specified COMMANDS.
FNAME is either a string or an unquoted symbol.
The autoload is generated only when the command is not already bound.
Argument FOR: just a required separator keyword to make code look better.
The macro also generates a `declare-function' for each function in
COMMANDS preventing byte-compiler warnings on code referencing these
functions."
(declare (indent 0))
(ignore for:)
(let ((fname (if (stringp fname) fname (symbol-name fname)))
(decl-fcts ()))
(dolist (fct commands)
(push `(declare-function ,fct ,fname) decl-fcts))
(if (> (length commands) 1)
`(progn
(dolist (fct (quote (,@commands)))
(unless (fboundp fct)
(autoload fct ,fname nil :interactive)))
,@decl-fcts)
`(progn
(unless (fboundp (quote ,@commands))
(autoload (quote ,@commands) ,fname nil :interactive))
,@decl-fcts))))
;; --
(defmacro pel-declare-file (fname defines: &rest commands)
"Declare one or several COMMANDS to be defined in specified FNAME.
This does not generate any code. It prevents byte-compiler warnings.
DEFINES: is a cosmetic only argument that must be present."
(declare (indent 0))
(ignore defines:)
(let ((fname (if (stringp fname) fname (symbol-name fname)))
(decl-fcts ()))
(dolist (fct commands)
(push `(declare-function ,fct ,fname) decl-fcts))
`(progn
,@decl-fcts)))
;; --
(defun pel--eval-after-load-error (feature error)
"Display warning for FEATURE loaded by `pel-eval-after-load'.
The ERROR argument is the caught error."
(display-warning 'pel-eval-after-load
(format "Failed configuring %s: %s"
feature
error)
:error))
(defconst pel--ts-mode-with-fixer '(ada-ts-mode
dart-ts-mode
elixir-ts-mode
erlang-ts
go-ts-mode
js-ts-mode
lua-ts-mode
ruby-ts-mode
rust-ts-mode
zig-ts-mode)
"List of Tree Sitter modes *features* that require a mode fixer function.
NOTE: the symbols inside the list are the features provided by the Emacs
Lisp file that implements the Tree-Sitter mode. Most of them have a
name that ends with \\='-mode\\=' but not all; as an example, the Erlang
`erlang-ts-mode' function is implemented in the file erlang-ts.el which
provides erlang-ts.
For some major modes when the file implementing the Tree-sitter based major
mode is loaded, that file automatically inserts a file association to the
Tree-sitter based major mode at the beginning of the `auto-mode-alist',
essentially replacing the association previously installed.
For those modes, that new association must be removed from
`auto-mode-alist'. That's the job of the mode fixer function, called by
the code generated by the `pel-eval-after-load' just after the mode
feature file has been loaded.
The mode fixer function has a name that has a format like
pel--MODE-fixer, where MODE corresponds to the name taken from this
list.")
(defmacro pel-eval-after-load (features &rest body)
"Evaluate BODY after the FEATURES has been loaded.
FEATURE is either a feature symbol or a list of feature symbols.
Both must be unquoted.
A list of feature symbol is useful, for example, when the Tree-sitter
mode is provided by a different file than the classic major mode,
and the Tree-sitter mode file does not load the classic mode file."
(declare (indent 1))
(let ((code nil)
(feature-body nil))
(dolist (the-feature (if (listp features) features (list features)))
(setq feature-body nil)
(when (memq the-feature pel--ts-mode-with-fixer)
(let ((fixer-fct (intern (format "pel--%s-fixer" the-feature))))
(pel-append-to feature-body
`((when (fboundp (quote ,fixer-fct))
(,fixer-fct))))))
(pel-append-to feature-body
`((condition-case-unless-debug err
(progn ,@body)
(error (pel--eval-after-load-error (quote ,the-feature)
err)))))
(pel-append-to code
`((with-eval-after-load (quote ,the-feature)
,@feature-body))))
;; Return the generated code for all features.
`(progn
,@code)))
;; ---------------------------------------------------------------------------
;; Speedbar Support
;; ----------------
(defun pel-add-speedbar-extension (extension)
"Add Speedbar support for the specified file EXTENSION.
EXTENSION is either a string or a list of strings.
Each string is either:
- a complete filename,
- the file extension starting with a (non-quoted) period,
- a regular expression to express the above.
`pel-add-speedbar-extension' is a direct proxy to
`speedbar-add-supported-extension' with the ability to load the
speedbar file."
(require 'speedbar)
(declare-function speedbar-add-supported-extension "speedbar")
(speedbar-add-supported-extension extension))
;; ---------------------------------------------------------------------------
;;* Major Mode Configuration
;; ========================
(defun pel--mode-hook-maybe-call (fct mode hook &optional append)
"Schedule FCT as the MODE HOOK: call it if buffer is currently in that MODE.
The function FCT is added at the beginning of the hook list unless the
optional argument APPEND is non-nil, in which case it is added at the end."
(add-hook hook fct append)
;; if the current mode is the required mode also run the specified function
(if (eq major-mode mode)
(funcall fct)))
(defconst pel--tab-controlling-major-modes
'(cwl
dart ; dart-mode and dart-ts-mode ; differ; explicit logic is needed.
go
go-dot-mod
go-mod ; for go-mod-ts-mode
ibuffer
intel-hex
janet
js2 js3
lfe inferior-lfe
lisp arc clojure
makefile
nimscript
nix