Fully switched over to record-based staging
[elinstall.git] / elinstall.el
blob9f39685bccbb8635c84bdc8bdbcf5b2f0576f4d4
1 ;;;_ elinstall.el --- Automatically and flexibly install elisp files
3 ;;;_. Headers
4 ;;;_ , License
5 ;; Copyright (C) 2010 Tom Breton (Tehom)
7 ;; Author: Tom Breton (Tehom) <tehom@panix.com>
8 ;; Keywords: maint, tools, internal
10 ;; This file is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; This file is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;;_ , Commentary:
27 ;; Entry points:
28 ;; elinstall Use this for overall loading
30 ;; elinstall-arrange-preload - Use this for non-autogenerated
31 ;; files that need to be linked in.
33 ;; elinstall-update-directory-autoloads
34 ;; elinstall-update-file-autoloads
36 ;;;_ , Requires
38 (require 'autoload)
39 (require 'pp)
40 (require 'cus-edit) ;;Because we save "installedness" manually
43 ;;;_. Body
44 ;;;_ , Customizations
45 (defgroup elinstall
46 '()
47 "Customizations for elinstall"
48 :group 'elinstall)
50 (defcustom elinstall-default-priority
52 "Default priority for site-start"
53 :group 'elinstall
54 :type 'integer)
56 (defcustom elinstall-default-preload-target
57 "~/.emacs.d/site-start.d/"
58 "Default preload-target for registering autoloads"
59 :group 'elinstall
60 :type
61 '(choice
62 (const "~/.emacs.d/site-start.d/")
63 (const "/etc/emacs/site-start.d/")
64 (directory "" )
65 (const nil)
66 (const 'dot-emacs)))
69 (defcustom elinstall-already-installed
70 '()
71 "Things that have already been installed.
72 This exists for recording what has been installed. User interaction is not
73 contemplated at this time." )
74 ;;;_ , Types
75 ;;;_ . elinstall-stages
76 (defstruct (elinstall-stages
77 (:constructor elinstall-make-stages)
78 (:conc-name elinstall-stages->)
79 (:copier nil))
80 "The elinstall stages"
81 build-deffiles
82 run-tests
83 byte-compile
84 arrange-preloads)
86 ;;;_ , Data
87 ;;;_ . Regular expressions
88 ;;;_ , elinstall-elisp-regexp
89 (defconst elinstall-elisp-regexp
90 (let ((tmp nil))
91 (dolist
92 (suf (get-load-suffixes))
93 (unless (string-match "\\.elc" suf) (push suf tmp)))
94 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
95 "Regular expression that matches elisp files" )
97 ;;;_ , Utilities
98 ;;;_ . elinstall-directory-true-name
99 (defun elinstall-directory-true-name ()
100 "Get the true name of the directory the calling code lives in.
101 CAUTION: This is sensitive to where it's called. That's the point of it."
102 (file-name-directory
103 (if load-file-name
104 (file-truename load-file-name)
105 (file-truename buffer-file-name))))
106 ;;;_ . Checking installedness
107 ;;;_ , elinstall-already-installed
108 (defun elinstall-already-installed (project-name)
109 "Return non-nil if PROJECT-NAME has been installed."
110 (member project-name elinstall-already-installed))
112 ;;;_ , elinstall-record-installed
113 (defun elinstall-record-installed (project-name)
114 "Record that PROJECT-NAME has been installed."
116 (add-to-list 'elinstall-already-installed project-name)
117 (customize-save-variable
118 'elinstall-already-installed
119 elinstall-already-installed
120 "Set by elinstall-record-installed"))
121 ;;;_ , Work
122 ;;;_ . Doing actions
124 ;;;_ , Doing autoload actions (All adapted from autoload.el)
125 ;;;_ . Utilities about the action list
126 ;;;_ , elinstall-remove-autogen-action
127 (defun elinstall-remove-autogen-action (file actions)
128 "Return ACTIONS minus any add-file-autoloads on FILE removed."
130 (delq nil
131 (mapcar
132 #'(lambda (act)
133 (case (car act)
134 (add-file-autoloads
135 (if (equal file (third act))
137 act))
138 (t act)))
139 actions)))
140 ;;;_ , elinstall-get-autogen-action
141 (defun elinstall-get-autogen-action (file actions)
143 (let
144 ((the-act))
145 (dolist (act actions)
146 (case (car act)
147 (add-file-autoloads
148 (when (equal file (third act))
149 (setq the-act act)))))
150 the-act))
152 ;;;_ . Making autoloads
153 ;;;_ , elinstall-generate-file-autoloads
154 ;;override to allow slashed load-paths
155 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
156 ;;a new arg.
157 ;;`relative-to' can be:
158 ;; * nil: act as at present. Assume that FILE's immediate directory
159 ;;is in load-path.
160 ;; * t :: use default-directory
161 ;; * a string :: relative to it, as a filename
163 (defun elinstall-generate-file-autoloads (relative-name full-name)
164 "Insert at point a loaddefs autoload section for FILE.
165 Autoloads are generated for defuns and defmacros in FILE
166 marked by `generate-autoload-cookie' (which see).
167 If FILE is being visited in a buffer, the contents of the buffer
168 are used.
169 Return non-nil in the case where no autoloads were added at point.
171 FULL-NAME is the absolute name of the file.
172 RELATIVE-NAME is its name respective to some component of load-path."
173 (let* ((outbuf (current-buffer))
174 (autoloads-done '())
175 (print-length nil)
176 (print-readably t) ; This does something in Lucid Emacs.
177 (float-output-format nil)
178 (done-any nil)
179 (visited (get-file-buffer full-name))
180 (source-buf
181 (or visited
182 ;; It is faster to avoid visiting the file.
183 (ignore-errors (autoload-find-file full-name))))
184 output-start)
185 (if source-buf
186 (with-current-buffer source-buf
187 ;;$$MOVE ME - this should be checked in action-finding.
188 ;; Obey the no-update-autoloads file local variable.
189 (unless no-update-autoloads
190 (message "Generating autoloads for %s..." relative-name)
191 (setq output-start (with-current-buffer outbuf (point)))
192 (save-excursion
193 (save-restriction
194 (widen)
195 (goto-char (point-min))
196 (while (not (eobp))
197 (skip-chars-forward " \t\n\f")
198 (cond
199 ((looking-at (regexp-quote generate-autoload-cookie))
200 (search-forward generate-autoload-cookie)
201 (skip-chars-forward " \t")
202 (setq done-any t)
203 (if (eolp)
204 ;; Read the next form and make an autoload.
205 (let* ((form (prog1 (read (current-buffer))
206 (or (bolp) (forward-line 1))))
207 (autoload
208 (make-autoload form relative-name)))
209 (if autoload
210 (push (nth 1 form) autoloads-done)
211 (setq autoload form))
212 (let ((autoload-print-form-outbuf outbuf))
213 (autoload-print-form autoload)))
215 ;; Copy the rest of the line to the output.
216 (princ (buffer-substring
217 (progn
218 ;; Back up over whitespace, to preserve it.
219 (skip-chars-backward " \f\t")
220 (if (= (char-after (1+ (point))) ? )
221 ;; Eat one space.
222 (forward-char 1))
223 (point))
224 (progn (forward-line 1) (point)))
225 outbuf)))
226 ((looking-at ";")
227 ;; Don't read the comment.
228 (forward-line 1))
230 (forward-sexp 1)
231 (forward-line 1))))))
233 (when done-any
234 (with-current-buffer outbuf
235 (save-excursion
236 ;; Insert the section-header line which lists the file name
237 ;; and which functions are in it, etc.
238 (goto-char output-start)
239 (autoload-insert-section-header
240 outbuf autoloads-done relative-name full-name
241 (nth 5 (file-attributes full-name)))
242 (insert ";;; Generated autoloads from "
243 (autoload-trim-file-name full-name) "\n"))
244 (insert generate-autoload-section-trailer)))
245 (message "Generating autoloads for %s...done" relative-name))
247 (unless visited
248 ;; We created this buffer, so we should kill it.
249 (kill-buffer (current-buffer))))
250 (message "Could not load %s" relative-name))
252 (not done-any)))
255 ;;;_ , elinstall-deffile-insert-autoloads
256 (defun elinstall-deffile-insert-autoloads (file load-name)
257 "Update the autoloads for FILE in current buffer.
258 Return FILE if there was no autoload cookie in it, else nil.
260 Current buffer must be a loaddef-style file.
262 LOAD-NAME is the absolute name of the file.
263 RELATIVE-NAME is its name respective to some component of load-path."
264 (let (
265 (found nil)
266 (no-autoloads nil))
268 (save-excursion
269 (save-restriction
270 (widen)
271 (goto-char (point-min))
272 ;; Look for the section for FILE
273 (while (and (not found)
274 (search-forward generate-autoload-section-header nil t))
275 (let ((form (autoload-read-section-header)))
276 (cond
277 ((equal (nth 2 form) file)
278 ;; We found the section for this file.
279 (let ((begin (match-beginning 0)))
280 (progn
281 (search-forward generate-autoload-section-trailer)
282 (delete-region begin (point))
283 (setq found t))))
284 ((string< file (nth 2 form))
285 ;; We've come to a section alphabetically later than
286 ;; FILE. We assume the file is in order and so
287 ;; there must be no section for FILE. We will
288 ;; insert one before the section here.
289 (goto-char (match-beginning 0))
290 (setq found 'new)))))
291 (unless found
292 (progn
293 (setq found 'new)
294 ;; No later sections in the file. Put before the last page.
295 (goto-char (point-max))
296 (search-backward "\f" nil t)))
297 (setq no-autoloads
298 (elinstall-generate-file-autoloads file load-name))))
300 (if no-autoloads file nil)))
301 ;;;_ . Arranging to add to info-path and load-path
302 ;;;_ , elinstall-generate-add-to-path
303 (defun elinstall-generate-add-to-path (path-element type)
304 "Insert code at point to add PATH-ELEMENT to a path.
305 If TYPE is:
306 * `add-to-load-path', add to load-path
307 * `add-to-info-path', add to Info-default-directory-list
309 Current buffer must be a loaddef-style file."
310 (let ( (path-symbol
311 (case type
312 (add-to-load-path 'load-path)
313 (add-to-info-path 'Info-default-directory-list)))
314 (description
315 (case type
316 (add-to-load-path "load-path")
317 (add-to-info-path "info-path")))
318 (autoloads-done '())
319 (print-length nil)
320 (print-readably t) ; This does something in Lucid Emacs.
321 (float-output-format nil))
323 (message "Generating %s additions..." description)
325 (autoload-insert-section-header
326 (current-buffer) (list path-element) nil nil
327 nil)
328 (insert ";;; Generated path addition\n")
330 `(add-to-list ',path-symbol
331 (expand-file-name
332 ,(file-relative-name path-element)
333 (if load-file-name
334 (file-name-directory
335 (file-truename load-file-name)))))
336 (current-buffer))
338 (insert generate-autoload-section-trailer)
339 (message "Generating %s additions...done" description)))
342 ;;;_ , elinstall-deffile-insert-add-to-path
343 (defun elinstall-deffile-insert-add-to-path (path-element type)
344 "Insert code in current buffer to add PATH-ELEMENT to a path.
345 If TYPE is:
346 * `add-to-load-path', add to load-path
347 * `add-to-info-path', add to Info-default-directory-list
349 Current buffer must be a loaddef-style file."
350 (let (
351 (found nil)
352 (no-autoloads nil))
354 (save-excursion
355 (save-restriction
356 (widen)
357 (goto-char (point-min))
358 ;; Look for the section for PATH-ELEMENT
359 (while (and (not found)
360 (search-forward generate-autoload-section-header nil t))
361 (let ((form (autoload-read-section-header)))
362 (cond
363 ((and
364 (equal (nth 0 form) type)
365 (member (nth 1 form) path-element))
367 ;; We found the section for this add.
368 (let ((begin (match-beginning 0)))
369 (progn
370 (search-forward generate-autoload-section-trailer)
371 (delete-region begin (point))
372 (setq found t)))))))
374 (unless found
375 (progn
376 (setq found 'new)
377 ;; No later sections in the file. Put before the last page.
378 (goto-char (point-max))
379 (search-backward "\f" nil t)))
381 (elinstall-generate-add-to-path path-element type)))
383 ;;This never belongs in the no-autoloads section.
384 nil))
386 ;;;_ . elinstall-deffile-insert
388 (defun elinstall-deffile-insert (action)
389 "Insert autoloads etc into current file according to ACTION.
390 The format of ACTION is described in the design docs.
392 Return filename if this action belongs in the no-autoload section."
394 (when action
395 (case (car action)
396 (add-file-autoloads
397 (elinstall-deffile-insert-autoloads
398 (third action)
399 (fifth action)))
401 (add-to-load-path
402 (elinstall-deffile-insert-add-to-path
403 (third action)
404 'add-to-load-path)
405 nil)
407 (add-to-info-path
408 (elinstall-deffile-insert-add-to-path
409 (third action)
410 'add-to-info-path)
411 nil)
413 ((preload-file run-tests byte-compile)
414 (error "This case should not come here.")))))
416 ;;;_ . elinstall-prepare-deffile
417 (defun elinstall-prepare-deffile (deffile)
418 "Try to ensure that DEFFILE is available for receiving autoloads"
420 (autoload-ensure-default-file deffile)
421 (with-current-buffer (find-file-noselect deffile)
424 ;; We must read/write the file without any code conversion,
425 ;; but still decode EOLs.
426 (let ((coding-system-for-read 'raw-text))
428 ;; This is to make generated-autoload-file have Unix EOLs, so
429 ;; that it is portable to all platforms.
430 (setq buffer-file-coding-system 'raw-text-unix))
431 (or (> (buffer-size) 0)
432 (error "Autoloads file %s does not exist" buffer-file-name))
433 (or (file-writable-p buffer-file-name)
434 (error "Autoloads file %s is not writable"
435 buffer-file-name))))
437 ;;;_ . elinstall-update-deffile
439 ;;Adapted from autoload.el `update-directory-autoloads'.
441 (defun elinstall-update-deffile (target actions &optional
442 use-load-path force)
444 Update file TARGET with current autoloads as specified by ACTIONS.
445 Also remove any old definitions pointing to libraries that can no
446 longer be found.
448 ACTIONS must be a list of actions (See the format doc). Each one's
449 filename must be relative to some element of load-path.
451 USE-LOAD-PATH is a list to use as load-path. It should include
452 any new load-path that we are arranging to create. If it's not given,
453 load-path itself is used.
455 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
456 Other non-nil cases of FORCE are reserved for future development.
458 This uses `update-file-autoloads' (which see) to do its work.
459 In an interactive call, you must give one argument, the name
460 of a single directory."
461 (let
463 (use-load-path (or use-load-path load-path))
464 (this-time (current-time))
465 ;;files with no autoload cookies.
466 (no-autoloads nil))
468 (elinstall-prepare-deffile target)
469 (with-current-buffer
470 (find-file-noselect target)
471 (save-excursion
472 (setq actions
473 (elinstall-remove-autogen-action
474 (autoload-trim-file-name target)
475 actions))
477 (goto-char (point-min))
478 (while (search-forward generate-autoload-section-header nil t)
479 (let* ((form (autoload-read-section-header))
480 (file (nth 3 form)))
481 (cond ((and (consp file) (stringp (car file)))
482 ;; This is a list of files that have no
483 ;; autoload cookies.
484 ;; There shouldn't be more than one such entry.
485 ;; Remove the obsolete section.
486 (autoload-remove-section (match-beginning 0))
487 (let ((last-time (nth 4 form)))
488 (dolist (file file)
489 (let ((file-time (nth 5 (file-attributes file))))
490 (when (and file-time
491 (not (time-less-p last-time file-time)))
492 ;; file unchanged
493 (push file no-autoloads)
494 (setq actions
495 (elinstall-remove-autogen-action
496 file actions)))))))
497 ((not (stringp file)))
499 (let
500 ((file-path
501 (locate-library file nil use-load-path)))
502 (cond
503 ;;File doesn't exist, so remove its
504 ;;section.
505 ((not file-path)
506 (autoload-remove-section
507 (match-beginning 0)))
509 ;; File hasn't changed, so do nothing.
510 ((equal
511 (nth 4 form)
512 (nth 5 (file-attributes file-path)))
513 nil)
515 (elinstall-deffile-insert
516 (elinstall-get-autogen-action
517 file actions))))
519 (setq actions
520 (elinstall-remove-autogen-action
521 file actions))))))))
523 ;; Remaining actions have no existing autoload sections yet.
524 (setq no-autoloads
525 (append no-autoloads
526 (delq nil (mapcar #'elinstall-deffile-insert actions))))
527 (when no-autoloads
528 ;; Sort them for better readability.
529 (setq no-autoloads (sort no-autoloads 'string<))
530 ;; Add the `no-autoloads' section.
531 (goto-char (point-max))
532 (search-backward "\f" nil t)
533 (autoload-insert-section-header
534 (current-buffer) nil nil no-autoloads this-time)
535 (insert generate-autoload-section-trailer))
536 (save-buffer))))
539 ;;;_ , Doing actions to arrange preloads
540 ;;;_ . elinstall-insert-add-to-path
541 (defun elinstall-insert-add-to-path (new path-sym)
542 "Insert code to add NEW to PATH-SYM.
543 Insert this at point in current buffer."
544 (insert "\n")
546 `(add-to-list ',path-sym
547 (expand-file-name ,new
548 (if load-file-name
549 (file-name-directory
550 (file-truename load-file-name)))))
551 (current-buffer)))
553 ;;;_ . elinstall-insert-add-to-load-path
554 (defun elinstall-insert-add-to-load-path (new)
555 "Insert code to add NEW to load-path.
556 Insert this at point in current buffer."
557 (elinstall-insert-add-to-path new 'load-path))
559 ;;;_ . elinstall-insert-add-to-info-path
560 (defun elinstall-insert-add-to-info-path (new)
561 "Insert code to add NEW to info-path.
562 Insert this at point in current buffer."
563 (elinstall-insert-add-to-path new 'Info-default-directory-list))
565 ;;;_ . elinstall-symlink-on-emacs-start
566 (defun elinstall-symlink-on-emacs-start
567 (filename target-basename target-dir &optional priority force)
568 "Symlink to TARGET-BASENAME.el in TARGET-DIR
570 If PRIORITY is given, it will be used as the priority prefix,
571 otherwise elinstall-default-priority will be.
572 PRIORITY must be an integer or nil.
573 If FORCE is `t', do it regardless of timestamps etc.
574 Other non-nil cases of FORCE are reserved for future development."
575 (let*
577 (priority (or priority elinstall-default-priority))
578 (target-name-nodir
579 (format
580 "%d%s.el"
581 priority
582 target-basename))
583 (target
584 (expand-file-name target-name-nodir target-dir)))
587 (cond
588 ;;Path should already exist.
589 ((not
590 (file-exists-p target-dir))
591 (message "The target directory doesn't exist."))
592 ;;Target shouldn't already exist, but if force is given, let
593 ;;user override.
594 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
595 ;;do nothing even on force.
596 ((and
597 (file-exists-p target)
599 (not force)
600 (not
601 (yes-or-no-p
602 (format "Really overwrite %s? " project-name))))
603 (message "File %s already exists" target)))
606 (make-symbolic-link
607 filename
608 target
609 nil)))))
611 ;;;_ . elinstall-add-to-dot-emacs
612 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
613 "Add code to load FILENAME to .emacs.
614 FILENAME should not have an extension"
616 ;;Visit .emacs
617 (with-current-buffer (find-file-noselect dot-emacs-name)
618 (save-excursion
619 ;;add at end of file
620 (goto-char (point-max))
621 (insert "\n;;Added by elinstall")
622 (insert "\n;;Consider using my-site-start to manage .emacs\n")
623 (pp `(load ,filename) (current-buffer))
624 (save-buffer))))
627 ;;;_ . elinstall-arrange-preload
628 ;;;###autoload
629 (defun elinstall-arrange-preload (force filename basename &optional priority)
630 "Arrange for FILENAME to be loaded on emacs start.
631 FORCE has its usual meaning.
632 BASENAME and PRIORITY are used as arguments to
633 `elinstall-symlink-on-emacs-start'.
636 (let
637 ((preload-target elinstall-default-preload-target))
639 ;;Dispatch the possibilities.
640 (cond
641 ((eq preload-target 'dot-emacs)
642 (elinstall-add-to-dot-emacs "~/.emacs" filename))
643 ((stringp preload-target)
644 (elinstall-symlink-on-emacs-start
645 filename basename preload-target priority force))
646 (null preload-target
647 (message "Not arranging for preloads"))
649 (message "I don't recognize that")))))
651 ;;;_ , Cleanup actions
652 ;;Nothing yet. This will be another type of action.
654 ;;;_ . Segregating actions
655 ;;;_ , elinstall-remove-empty-segs
656 (defun elinstall-remove-empty-segs (segment-list)
657 "Return SEGMENT-LIST minus any segments that have no actions.
658 Intended only for the deffile stage data."
659 (delq nil
660 (mapcar
661 #'(lambda (segment)
662 (if (cdr segment)
663 segment
664 nil))
665 segment-list)))
667 ;;;_ , elinstall-segregate-actions
668 (defun elinstall-segregate-actions (actions)
669 "Return actions segregated by deffile.
671 Returns a list whose elements are each a cons of:
672 * deffile filename or nil
673 * A list of actions to be done for that deffile."
675 (let
677 (build-deffiles '())
678 (run-tests '())
679 (byte-compile '())
680 (arrange-preloads '()))
682 (dolist (act actions)
683 (when act
684 (case (car act)
685 ((add-file-autoloads
686 add-to-info-path
687 add-to-load-path)
688 (let*
689 ((deffile-name (second act))
690 (cell-already
691 (assoc deffile-name build-deffiles)))
692 (if cell-already
693 ;;There are already actions on this deffile.
694 ;;Splice this action in.
695 (setcdr cell-already
696 (cons act (cdr cell-already)))
697 ;;There are no actions on this deffile. Add a
698 ;;place for them and include this action.
699 (push (list deffile-name act) build-deffiles))))
700 (preload-file
701 (push act arrange-preloads))
702 (run-tests
703 (push act run-tests))
704 (byte-compile
705 (push act byte-compile)))))
707 (elinstall-make-stages
708 :build-deffiles
709 (elinstall-remove-empty-segs build-deffiles)
710 :run-tests
711 run-tests
712 :byte-compile
713 byte-compile
714 :arrange-preloads
715 arrange-preloads)))
721 ;;;_ . Finding actions
722 ;;;_ , Treating the parameter list
723 ;;;_ . elinstall-add-parameter
724 (defun elinstall-add-parameter (alist key new-value)
725 "Add a new value for KEY to ALIST"
727 (cons
728 (cons key new-value)
729 (assq-delete-all key (copy-list alist))))
731 ;;;_ . elinstall-get-parameter
732 (defun elinstall-get-parameter (alist key)
733 "Get the value of KEY from ALIST"
735 (cdr (assq key alist)))
736 ;;;_ . elinstall-expand-file-name
737 ;;$$OBSOLETE
739 (defun elinstall-expand-file-name (filename alist)
740 "Expand FILENAME by the value of `path' in ALIST"
741 (expand-file-name
742 filename
743 (elinstall-get-parameter alist 'path)))
744 ;;;_ , Finding deffiles
745 ;;;_ . elinstall-expand-deffile-name
746 (defun elinstall-expand-deffile-name (deffile)
747 "Expand DEFFILE autoload.el's way."
749 (expand-file-name (or deffile "loaddefs.el")
750 (expand-file-name "lisp"
751 source-directory)))
753 ;;;_ . elinstall-maybe-get-deffile
754 (defun elinstall-maybe-get-deffile (file)
755 "If FILE defined `generated-autoload-file', return it.
756 Otherwise return nil.
757 Return it as an absolute filename."
759 (save-excursion
760 ;;$$FIXME load buffer if it's not already loaded
761 (let*
762 ((existing-buffer (get-file-buffer file)))
764 ;; We want to get a value for generated-autoload-file from
765 ;; the local variables section if it's there.
766 ;;But if it's not loaded, we don't? Maybe should use
767 ;; `autoload-find-file' and load it.
768 (if existing-buffer
769 (set-buffer existing-buffer))
770 (if (local-variable-p 'generated-autoload-file)
771 (elinstall-expand-deffile-name
772 generated-autoload-file)
773 nil))))
776 ;;;_ , Workers
777 ;;;_ . elinstall-find-actions-for-file
778 (defun elinstall-find-actions-for-file
779 (filename load-path-element dir parameters)
780 "Return a list of actions to do for FILENAME.
781 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
782 `elinstall-find-actions-by-spec' "
784 (let
785 ((full-path
786 (expand-file-name filename dir)))
787 (list
788 `(add-file-autoloads
789 ,(elinstall-get-parameter
790 parameters 'def-file)
791 ;;load-name relative to a member of load-path
792 ,(file-name-sans-extension
793 (file-relative-name
794 full-path
795 load-path-element))
796 ,load-path-element ;;Is this still used?
797 ,full-path))))
799 ;;;_ . elinstall-find-actions-by-spec
801 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
802 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
804 LOAD-PATH-ELEMENT is the conceptual element of load-path that
805 surrounds DIR. It may not yet have been added to load-path."
806 (if (consp spec)
807 ;;$$IMPROVE ME by adding the other cases in the design.
808 (case (car spec)
810 (let
811 ((new-path
812 (expand-file-name
813 (second spec)
814 dir)))
816 (elinstall-find-actions-by-spec
817 (third spec)
818 load-path-element
819 new-path
820 parameters)))
822 (all
823 (apply #'nconc
824 (mapcar
825 #'(lambda (sub-spec)
826 (elinstall-find-actions-by-spec
827 sub-spec
828 load-path-element
830 parameters))
831 (cdr spec))))
833 (file
834 (elinstall-find-actions-for-file
835 filename load-path-element dir parameters))
837 (dir
838 (let*
839 ((dirname
840 (expand-file-name
841 (second spec)
842 dir))
843 (load-path-here
844 (not
845 (elinstall-get-parameter
846 parameters 'block-add-to-load-path)))
847 (load-path-element
848 (if load-path-here
849 dirname
850 load-path-element)))
852 (cons
853 ;;$$IMPROVE ME
854 ;;Do this only if there are loadable files.
855 (if load-path-here
856 `(add-to-load-path
857 ,(elinstall-get-parameter
858 parameters 'def-file)
859 ,load-path-element)
860 '())
861 ;;$$IMPROVE ME
862 ;;Do add-to-info-path too. But test if there are
863 ;;any info files present.
865 ;;$$IMPROVE ME
866 ;; We want to get a value for generated-autoload-file
867 ;; from the local variables section if it's there.
868 ;;Use `elinstall-maybe-get-deffile'
869 ;; Otherwise we'll use `def-file' in parameters.
871 ;;$$FIXME This isn't quite right. If directory
872 ;;itself is not in load-path, this will be wrong.
873 ;;Gotta know where our encompassing part of
874 ;;load-path is.
876 ;;$$ENCAP ME This should be shared with the
877 ;;treatment of (file FN)
879 ;;$$FIXME Don't do directories, but maybe recurse on
880 ;;them, if a flag is set. And since we definitely
881 ;;have a load-path element here,
882 ;;'block-add-to-load-path according to a parameter.
883 ;;Maybe could follow/not symlinks similarly.
884 (apply #'nconc
885 (mapcar
886 #'(lambda (filename)
887 (elinstall-find-actions-for-file
888 filename
889 load-path-element
890 dirname
891 parameters))
893 (directory-files
894 dirname
895 nil ;;Relative filenames
896 elinstall-elisp-regexp))))))
898 (def-file
899 (let
900 ((new-def-file
901 (expand-file-name
902 (second spec)
903 dir))
904 (for-preload (third spec)))
905 (assert (listp for-preload))
906 (append
907 (list
909 (and for-preload (car for-preload))
910 `(preload-file
911 ,(car for-preload)
912 ,new-def-file
913 ,@(cdr for-preload))
914 '()))
916 (elinstall-find-actions-by-spec
917 (fourth spec)
918 load-path-element
920 (elinstall-add-parameter parameters
921 'def-file new-def-file))))))
923 ;;$$IMPROVE ME by adding the other cases in the design.
924 (case spec
925 (t))))
926 ;;;_ . high-level work
927 ;;;_ , elinstall-get-relevant-load-path
928 (defun elinstall-get-relevant-load-path (actions)
930 (delq nil
931 (mapcar
932 #'(lambda (act)
933 (case (car act)
934 (add-to-load-path
935 (second act))
936 (t nil)))
937 actions)))
938 ;;;_ , elinstall-stage-update-deffiles
939 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
940 "Update any deffiles mentioned in SEGMENT-LIST.
941 FORCE and USE-LOAD-PATH have the same meaning as in
942 `elinstall-update-deffile'.
944 (mapcar
945 #'(lambda (segment)
946 (let*
947 ((deffile (car segment)))
948 (if (stringp deffile)
949 (elinstall-update-deffile deffile (cdr segment) force
950 use-load-path))))
951 segment-list))
952 ;;;_ , elinstall-stage-arrange-preloads
953 (defun elinstall-stage-arrange-preloads (actions deffiles-used)
954 "Arrange any preloads mentioned in ACTIONS."
956 (mapcar
957 #'(lambda (act)
958 (case (car act)
959 (preload-file
960 (let
961 ((proceed (car act)))
962 ;;$$TEST whether to proceed
963 (apply
964 #'elinstall-arrange-preload
965 force
966 (cddr act))))
968 (error
969 "elinstall-stage-arrange-preloads: Action not
970 recognized."))) )
971 actions))
973 ;;;_ , elinstall-stage-run-tests
974 (defun elinstall-stage-run-tests (actions)
975 "Run any tests mentioned in ACTIONS."
977 (mapcar
978 #'(lambda (act)
979 (case (car act)
980 (run-tests
981 ;;$$WRITE ME - not a high priority right now.
982 nil)
984 (error
985 "elinstall-stage-arrange-preloads: Action not
986 recognized."))) )
987 actions))
989 ;;;_ , elinstall-stage-byte-compile
990 (defun elinstall-stage-byte-compile (actions)
991 "Do any byte-compilation mentioned in ACTIONS."
993 (mapcar
994 #'(lambda (act)
995 (case (car act)
996 ;;$$IMPROVE ME Understand flags to control second
997 ;;argument (whether to load file after
998 ;;compilation)
999 (byte-compile
1000 (byte-compile-file (second act)))
1002 (error
1003 "elinstall-stage-arrange-preloads: Action not
1004 recognized."))) )
1005 actions))
1006 ;;;_ , elinstall-do-segment
1007 ;;$$OBSOLETE
1009 (defun elinstall-do-segment (segment force use-load-path)
1010 "Do all the actions in SEGMENT.
1011 FORCE has its usual meaning.
1012 USE-LOAD-PATH is the effective load-path."
1014 (let*
1015 ((deffile (car segment)))
1016 (if (stringp deffile)
1017 (elinstall-update-deffile deffile (cdr segment) force
1018 use-load-path)
1019 ;;Do other actions: link-in actions and cleanups.
1020 (mapcar
1021 #'(lambda (act)
1023 (case (car act)
1024 (preload-file
1025 (let
1026 ((proceed (car act)))
1027 (apply
1028 #'elinstall-arrange-preload
1029 force
1030 (cddr act))))
1031 (run-tests
1032 ;;$$WRITE ME - not a high priority right now.
1033 nil)
1035 ;;$$IMPROVE ME Understand flags to control second
1036 ;;argument (whether to load file after
1037 ;;compilation)
1038 (byte-compile
1039 (byte-compile-file (second act)))))
1041 (cdr segment)))))
1044 ;;;_ . Overall work
1045 ;;;_ , elinstall-x
1046 (defun elinstall-x (dir spec &optional force)
1048 (let*
1050 ;;This is just the default deffile, spec can override it.
1051 (def-file
1052 (elinstall-expand-deffile-name nil))
1053 (actions
1054 (elinstall-find-actions-by-spec
1055 spec
1059 (def-file . ,def-file ))))
1060 (stages (elinstall-segregate-actions actions))
1061 (use-load-path
1062 (elinstall-get-relevant-load-path
1063 actions)))
1065 (elinstall-stage-update-deffiles
1066 (elinstall-stages->build-deffiles stages)
1067 force
1068 use-load-path)
1069 (elinstall-stage-arrange-preloads
1070 (elinstall-stages->arrange-preloads stages)
1071 (mapcar
1072 #'car
1073 (elinstall-stages->build-deffiles stages)))
1076 ;;;_ . Entry points
1077 ;;;_ , elinstall
1078 ;;;###autoload
1079 (defun elinstall (project-name path spec &optional force)
1080 "Install elisp files.
1081 They need not be a formal package.
1083 Parameters:
1085 PROJECT-NAME - the name of the project
1087 PATH - Path to the project.
1088 Suggestion: (elinstall-directory-true-name)
1090 SPEC - a spec for the autoloads etc to make. It can be as simple as
1091 \(dir \"\.\") for installing one directory.
1093 If FORCE is t, install a package even if it has already been
1094 installed. Other non-nil cases of FORCE are reserved for future
1095 development."
1097 (when
1098 (and
1099 (or
1100 force
1101 (not (elinstall-already-installed project-name)))
1102 (yes-or-no-p (format "Re-install %s? " project-name)))
1103 (elinstall-x
1104 path
1105 `(def-file "loaddefs.el" (t ,project-name) ,spec)
1106 force)
1107 (elinstall-record-installed project-name)))
1111 ;;;_ , elinstall-update-directory-autoloads
1112 ;;$$TEST ME
1113 ;;;###autoload
1114 (defun elinstall-update-directory-autoloads (dir)
1117 (interactive "DInstall all elisp files from directory: ")
1120 (let
1121 ((def-file-name
1122 (elinstall-expand-deffile-name
1123 generated-autoload-file)))
1125 (elinstall-x
1127 `(def-file ,def-file-name (nil) (dir "."))
1132 ;;;_ , elinstall-update-file-autoloads
1133 ;;$$TEST ME
1134 ;;;###autoload
1135 (defun elinstall-update-file-autoloads (file)
1138 (interactive "fInstall elisp file: ")
1139 (let
1140 ((def-file-name
1142 (elinstall-maybe-get-deffile file)
1143 (elinstall-expand-deffile-name
1144 generated-autoload-file))))
1145 (elinstall
1146 file
1147 `(def-file ,def-file-name (nil) (file ,file)))))
1149 ;;;_. Footers
1150 ;;;_ , Provides
1152 (provide 'elinstall)
1154 ;;;_ * Local emacs vars.
1155 ;;;_ + Local variables:
1156 ;;;_ + mode: allout
1157 ;;;_ + End:
1159 ;;;_ , End
1160 ;;; elinstall.el ends here