-
Notifications
You must be signed in to change notification settings - Fork 2
/
pjb-loader.el
452 lines (394 loc) · 14.3 KB
/
pjb-loader.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
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;**************************************************************************
;;;;FILE: loader.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Load pjb emacs sources.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal J. Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2010-02-25 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; GPL
;;;;
;;;; Copyright Pascal J. Bourguignon 2010 - 2011
;;;;
;;;; 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
;;;; 2 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, write to the Free
;;;; Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;**************************************************************************
(require 'cl)
(defun check-version-lock (from)
(if (file-exists-p "--version.lock")
(error "version lock")))
(defun load-stuff (files &optional show-messages)
(unwind-protect
(dolist (path files)
(if *pjb-load-noerror*
(condition-case cc
(load path *pjb-load-noerror* *pjb-load-silent*)
(error
(setq show-messages t)
(message (format "ERROR: %S" cc))))
(load path *pjb-load-noerror* *pjb-load-silent*)))
(when (file-exists-p "--version.lock")
(delete-file "--version.lock"))
(when show-messages
(switch-to-buffer "*Messages*")
(ignore-errors (split-window-vertically)))))
(defvar *pjb-sources* '())
(setf *pjb-sources*
'(
"emacs-uptime.el"
"pjb-advices.el"
"pjb-asdf.el"
"pjb-asm7090.el"
"pjb-blink.el"
"pjb-constants.el"
"pjb-color.el"
"pjb-cl-magic.el"
"pjb-cl.el"
"pjb-class.el"
"pjb-dodo.el"
"pjb-emacs.el"
"pjb-emacs-patches.el"
"pjb-emacs-balance-windows.el"
"pjb-insert-image.el"
"pjb-milliways.el"
"pjb-caps-mode.el"
"pjb-echo-keys.el"
"pjb-erc.el"
"pjb-erc-filter.el"
"pjb-erc-speak.el"
"pjb-eval.el"
"pjb-euro.el"
"pjb-font.el"
"pjb-html.el"
"pjb-list.el"
"pjb-gnus.el"
"pjb-mail.el"
"pjb-object.el"
"pjb-page.el"
"pjb-pgp.el"
"pjb-queue.el"
"pjb-ruby.el"
"pjb-shell.el"
"pjb-sources.el"
"pjb-clelp.el"
"pjb-state-coding.el"
"pjb-strings.el"
"pjb-utilities.el"
"pjb-work.el"
"pjb-xresources.el"
"pjb-thi.el"
"pjb-c-style.el"
"pjb-searches.el"
"pjb-termbin.el"
"pjb-java"
"pjb-objc-edit.el"
"pjb-objc-gen.el"
"pjb-objc-ide.el"
;; not yet ;; "pjb-objc-mode.el"
"pjb-objc-parser.el"
"android-classes.el"
"pjb-pl1.el" ; testing
))
(unless *pjb-light-emacs*
(setf *pjb-sources*
(append *pjb-sources*
'(
"pjb-unicode.el"
"pjb-computer-paper.el"
"pjb-cvs.el"
"pjb-cvspass.el"
"pjb-dot.el"
"pjb-graph.el"
"pjb-cl-faces.el"
"pjb-cl-magic-lambda-lists.el"
"pjb-i2p-expression.el"
"pjb-s2p-expression.el"
"pjb-layers.el"
"pjb-make-depends.el"
"pjb-roman.el"
"pjb-secouer.el"
"pjb-server.el"
"pjb-transpose.el"
"pjb-worldfact.el"
"pjb-banks.el"
"pjb-bourse.el"
"pjb-selftrade.el"
))))
(unless :obsolete
'(
"pjb-vm"
"pjb-vm-kill-file.el"
"pjb-banks-old.el"
"pjb-c.el"
"pjb-objc-mode.el"
"pjb-comint"
"slime-rpc.el"
"split.el"
))
;; (load-stuff
;; (let ((files-not-to-load
;; (append
;; ;; files NEVER to load.
;; '("pjb-emacs-cl"
;; "pjb-c"
;; "pjb-w3"
;; "pjb-objc-mode"
;; "pjb-banks-old")
;; (when *pjb-light-emacs*
;; ;; files NOT to load when light
;; '("pjb-banks"
;; "pjb-bourse"
;; "pjb-cl-faces"
;; "pjb-computer-paper"
;; "pjb-constants"
;; "pjb-cvs"
;; "pjb-cvspass"
;; "pjb-dot"
;; "pjb-graph"
;; "pjb-i2p-expression"
;; "pjb-invoices"
;; "pjb-layers"
;; "pjb-make-depends"
;; "pjb-roman"
;; "pjb-s2p-expression"
;; "pjb-secouer"
;; "pjb-selftrade"
;; "pjb-server"
;; "pjb-transpose"
;; "pjb-vm-kill-file"
;; "pjb-worldfact")))))
;; (remove-if
;; (lambda (file) (member* file files-not-to-load :test (function string=)))
;; (let ((home-path (namestring (user-homedir-pathname))))
;; (append
;; ;; all the files
;; (mapcar
;; (function pathname-name)
;; (file-expand-wildcards ; DIRECTORY doesn't work on "pjb-*" yet.
;; (if (file-directory-p (concatenate 'string home-path "src/public/emacs"))
;; (concatenate 'string home-path "src/public/emacs/pjb-*.el")
;; (get-directory :share-lisp "packages/com/informatimago/emacs/pjb-*.el"))))
;; (list ;; some additional dynamic data:
;; (concatenate 'string home-path ".emacs-devises")))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Let's make a topological sort of *pjb-sources* to load them in the
;;; right order.
;;;
;;; First search all the toplevel require forms in the pjb-sources.
;;; Then sort them.
;;; Finally loaded them.
(defvar *el-walk-sexps-end-marker*)
(defun skip-comments ()
"
DO: Move the point over spaces and lisp comments ( ;...\n or #| ... |# ),
in the current buffer.
RETURN: (not eof)
"
(interactive)
(let* ((comment-regexp "\\(#|\\([^|]\\||[^#]\\)*|#\\)\\|\\(;.*$\\)")
(space-or-comment (format "\\(%s\\)\\|\\(%s\\)"
"[ \t\n\v\f\r]+"
comment-regexp)) )
(while (looking-at space-or-comment)
(goto-char (match-end 0)))
(< (point) (point-max))))
(defun el-walk-sexps (fun)
"
DO: Recursively scan sexps from (point) in current buffer up to
the end-of-file or until scan-sexps raises a scan-error.
Call fun on each sexps and each of their children etc.
fun: A function (sexp start end)
sexp: The sexp parsed from a source file.
start: The point starting the sexp.
end: The point ending the sexp.
NOTE: All positions are kept in markers, so modifying the buffer between
start and end should be OK.
However ' or ` are passed as (quote ...) or (backquote ...)
to the function fun without reparsing the sexp inside them.
Ie. if you modify such a source, (which can be detected looking at
the character at start position), you still get the original sexp.
"
(let ((quote-stack '())
(start-stack '())
(*el-walk-sexps-end-marker* (make-marker))
quote-depth
start-m sexp)
(skip-comments)
(when (/= (point) (point-max))
;; gather the quotes:
(while (looking-at "['`] *")
;; quote or backquote
;; NOT NEEDED ANYMORE WITH GNU Emacs 21.
;; --- (push (set-marker (make-marker) (point)) start-stack)
;; --- (push (if (= (char-after) ?') 'quote 'backquote) quote-stack)
(forward-char 1)
(skip-comments))
;; get the sexp:
(setq start-m (set-marker (make-marker) (point)))
(forward-sexp 1)
(set-marker *el-walk-sexps-end-marker* (point))
;; (forward-sexp -1)
;; (assert (= (marker-position start-m) (point)) t)
(goto-char (marker-position start-m))
(setq sexp (sexp-at-point))
;; push the quotes on the sexp:
(setq quote-depth (length quote-stack))
(while quote-stack
(setq sexp (cons (pop quote-stack) (list sexp))))
;; process the quotes:
(setq start-stack (nreverse start-stack))
(dotimes (i quote-depth)
;; (message "sexp = %S\nstart = %S\nend = %S\n" sexp (marker-position (car start-stack)) *el-walk-sexps-end-marker*)
(funcall fun sexp
(marker-position (car start-stack)) *el-walk-sexps-end-marker*)
(set-marker (pop start-stack) nil)
(setq sexp (cadr sexp)))
;; process the sexp:
;; (message "sexp = %S\nstart = %S\nend = %S\n" sexp (marker-position start-m) *el-walk-sexps-end-marker*)
(funcall fun sexp (marker-position start-m) *el-walk-sexps-end-marker*)
;; (when *map-sexps-deeply*
;; (when (= (char-syntax (char-after (marker-position start-m))) 40) ;; "("
;; ;; then the subsexps:
;; (goto-char (marker-position start-m))
;; (down-list 1)
;; (loop
;; (condition-case nil
;; (el-walk-sexps fun)
;; (scan-error (return-from nil))))
;; (up-list 1)))
;; then go to the next sexp:
(goto-char (marker-position *el-walk-sexps-end-marker*))
(set-marker start-m nil)
(set-marker *el-walk-sexps-end-marker* nil)))
nil)
(defun el-map-sexps (source-file fun &rest cl-keys)
"
DO: Scan all toplevel sexps in the source file.
(skipping spaces and comment between top-level sexps).
fun: A function (sexp start end)
sexp: The sexp parsed from a source file.
start: The point starting the sexp.
end: The point ending the sexp.
NOTE: Scanning stops as soon as an error is detected by forward-sexp.
RETURN: The list of results from fun.
"
(save-excursion
(save-restriction
(let ((old-buffer (current-buffer))
(existing-buffer (get-buffer source-file))
last-bosexp)
(if existing-buffer
(switch-to-buffer existing-buffer)
(find-file source-file))
(widen)
(goto-char (point-min))
(while (< (point) (point-max))
(el-walk-sexps fun))
(if existing-buffer
(switch-to-buffer old-buffer)
(kill-buffer (current-buffer)))))))
(defun source-file-requires (path)
(let ((requires '()))
(el-map-sexps path
(lambda (form start end)
(when (and (listp form)
(eq 'require (first form)))
(push (let ((what (second form)))
(if (atom what)
what
(if (eq 'quote (first what))
(second what)
what)))
requires))))
requires))
(defun pathname-name* (path)
(cond ((string-match "^\\(.*/\\)?\\(.*\\)\\.\\([^.]*\\)\\(\\.~[0-9]+~\\)$" path)
(match-string 2 path))
((string-match "^\\(.*/\\)?\\(.*\\)\\.\\([^.]*\\)$" path)
(match-string 2 path))
((string-match "^\\(.*/\\)?\\(.*\\)$" path)
(match-string 2 path))
(t :unspecific)))
(defun topological-sort (nodes lessp)
"
RETURN: A list of NODES sorted topologically according to
the partial order function LESSP.
If there are cycles (discounting reflexivity),
then the list returned won't contain all the NODES.
"
(loop
with sorted = '()
with incoming = (map 'vector (lambda (to)
(loop
for from in nodes
when (and (not (eq from to))
(funcall lessp from to))
sum 1))
nodes)
with q = (loop
for node in nodes
for inco across incoming
when (zerop inco)
collect node)
while q
do (let ((n (pop q)))
(push n sorted)
(loop
for m in nodes
for i from 0
do (when (and (and (not (eq n m))
(funcall lessp n m))
(zerop (decf (aref incoming i))))
(push m q))))
finally (return (nreverse sorted))))
(defvar *pjb-sources-order*)
(setf *pjb-sources-order*
(mapcar (lambda (file)
(let ((path (concat (if load-file-name
(file-name-directory load-file-name)
(concat (getenv "HOME") "/src/public/emacs/"))
file)))
(cons (intern (pathname-name* file))
(source-file-requires path))))
*pjb-sources*))
(defun pjb-sources-lessp (a b)
(let ((aa (assoc a *pjb-sources-order*)))
(and aa (member b (cdr aa)))))
(defun check-pjb-sources-lessp ()
(let ((nodes (mapcar (lambda (path) (intern (pathname-name* path))) *pjb-sources*)))
(loop
for from in nodes
do (loop
for to in nodes
when (and (not (eq from to))
(pjb-sources-lessp from to)
(pjb-sources-lessp to from))
do (print (list from to))))))
(let ((sorted (topological-sort
(mapcar (lambda (path) (intern (pathname-name* path))) *pjb-sources*)
(function pjb-sources-lessp))))
(when (< (length sorted) (length *pjb-sources*))
(error "There are circularities in the requires of PJB sources."))
(setf *pjb-sources* (reverse (mapcar (lambda (name) (format "%s.el" name)) sorted))))
(load-stuff *pjb-sources* (not *pjb-load-silent*))
;;;; THE END ;;;;