Bugfix
[elinstall.git] / elinstall.el
blob0bb5106ca8b7ce5498396a14476f7ca49cf8f337
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 (delq nil
659 (mapcar
660 #'(lambda (segment)
661 (if (cdr segment)
662 segment
663 nil))
664 segment-list)))
666 ;;;_ , elinstall-segregate-actions
667 (defun elinstall-segregate-actions (actions)
668 "Return actions segregated by deffile.
670 Returns a list whose elements are each a cons of:
671 * deffile filename or nil
672 * A list of actions to be done for that deffile."
674 (let
676 (build-deffiles '())
677 (run-tests '())
678 (byte-compile '())
679 (arrange-preloads '()))
681 (dolist (act actions)
682 (when act
683 (case (car act)
684 ((add-file-autoloads
685 add-to-info-path
686 add-to-load-path)
687 (let*
688 ((deffile-name (second act))
689 (cell-already
690 (assoc deffile-name build-deffiles)))
691 (if cell-already
692 ;;There are already actions on this deffile.
693 ;;Splice this action in.
694 (setcdr cell-already
695 (cons act (cdr cell-already)))
696 ;;There are no actions on this deffile. Add a
697 ;;place for them and include this action.
698 (push (list deffile-name act) build-deffiles))))
699 (preload-file
700 (push act arrange-preloads))
701 (run-tests
702 (push act run-tests))
703 (byte-compile
704 (push act byte-compile)))))
706 (elinstall-make-stages
707 :build-deffiles
708 (elinstall-remove-empty-segs build-deffiles)
709 :run-tests
710 (elinstall-remove-empty-segs run-tests)
711 :byte-compile
712 (elinstall-remove-empty-segs byte-compile)
713 :arrange-preloads
714 (elinstall-remove-empty-segs arrange-preloads))))
720 ;;;_ . Finding actions
721 ;;;_ , Treating the parameter list
722 ;;;_ . elinstall-add-parameter
723 (defun elinstall-add-parameter (alist key new-value)
724 "Add a new value for KEY to ALIST"
726 (cons
727 (cons key new-value)
728 (assq-delete-all key (copy-list alist))))
730 ;;;_ . elinstall-get-parameter
731 (defun elinstall-get-parameter (alist key)
732 "Get the value of KEY from ALIST"
734 (cdr (assq key alist)))
735 ;;;_ . elinstall-expand-file-name
736 ;;$$OBSOLETE
738 (defun elinstall-expand-file-name (filename alist)
739 "Expand FILENAME by the value of `path' in ALIST"
740 (expand-file-name
741 filename
742 (elinstall-get-parameter alist 'path)))
743 ;;;_ , Finding deffiles
744 ;;;_ . elinstall-expand-deffile-name
745 (defun elinstall-expand-deffile-name (deffile)
746 "Expand DEFFILE autoload.el's way."
748 (expand-file-name (or deffile "loaddefs.el")
749 (expand-file-name "lisp"
750 source-directory)))
752 ;;;_ . elinstall-maybe-get-deffile
753 (defun elinstall-maybe-get-deffile (file)
754 "If FILE defined `generated-autoload-file', return it.
755 Otherwise return nil.
756 Return it as an absolute filename."
758 (save-excursion
759 ;;$$FIXME load buffer if it's not already loaded
760 (let*
761 ((existing-buffer (get-file-buffer file)))
763 ;; We want to get a value for generated-autoload-file from
764 ;; the local variables section if it's there.
765 ;;But if it's not loaded, we don't? Maybe should use
766 ;; `autoload-find-file' and load it.
767 (if existing-buffer
768 (set-buffer existing-buffer))
769 (if (local-variable-p 'generated-autoload-file)
770 (elinstall-expand-deffile-name
771 generated-autoload-file)
772 nil))))
775 ;;;_ , Workers
776 ;;;_ . elinstall-find-actions-for-file
777 (defun elinstall-find-actions-for-file
778 (filename load-path-element dir parameters)
779 "Return a list of actions to do for FILENAME.
780 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
781 `elinstall-find-actions-by-spec' "
783 (let
784 ((full-path
785 (expand-file-name filename dir)))
786 (list
787 `(add-file-autoloads
788 ,(elinstall-get-parameter
789 parameters 'def-file)
790 ;;load-name relative to a member of load-path
791 ,(file-name-sans-extension
792 (file-relative-name
793 full-path
794 load-path-element))
795 ,load-path-element ;;Is this still used?
796 ,full-path))))
798 ;;;_ . elinstall-find-actions-by-spec
800 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
801 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
803 LOAD-PATH-ELEMENT is the conceptual element of load-path that
804 surrounds DIR. It may not yet have been added to load-path."
805 (if (consp spec)
806 ;;$$IMPROVE ME by adding the other cases in the design.
807 (case (car spec)
809 (let
810 ((new-path
811 (expand-file-name
812 (second spec)
813 dir)))
815 (elinstall-find-actions-by-spec
816 (third spec)
817 load-path-element
818 new-path
819 parameters)))
821 (all
822 (apply #'nconc
823 (mapcar
824 #'(lambda (sub-spec)
825 (elinstall-find-actions-by-spec
826 sub-spec
827 load-path-element
829 parameters))
830 (cdr spec))))
832 (file
833 (elinstall-find-actions-for-file
834 filename load-path-element dir parameters))
836 (dir
837 (let*
838 ((dirname
839 (expand-file-name
840 (second spec)
841 dir))
842 (load-path-here
843 (not
844 (elinstall-get-parameter
845 parameters 'block-add-to-load-path)))
846 (load-path-element
847 (if load-path-here
848 dirname
849 load-path-element)))
851 (cons
852 ;;$$IMPROVE ME
853 ;;Do this only if there are loadable files.
854 (if load-path-here
855 `(add-to-load-path
856 ,(elinstall-get-parameter
857 parameters 'def-file)
858 ,load-path-element)
859 '())
860 ;;$$IMPROVE ME
861 ;;Do add-to-info-path too. But test if there are
862 ;;any info files present.
864 ;;$$IMPROVE ME
865 ;; We want to get a value for generated-autoload-file
866 ;; from the local variables section if it's there.
867 ;;Use `elinstall-maybe-get-deffile'
868 ;; Otherwise we'll use `def-file' in parameters.
870 ;;$$FIXME This isn't quite right. If directory
871 ;;itself is not in load-path, this will be wrong.
872 ;;Gotta know where our encompassing part of
873 ;;load-path is.
875 ;;$$ENCAP ME This should be shared with the
876 ;;treatment of (file FN)
878 ;;$$FIXME Don't do directories, but maybe recurse on
879 ;;them, if a flag is set. And since we definitely
880 ;;have a load-path element here,
881 ;;'block-add-to-load-path according to a parameter.
882 ;;Maybe could follow/not symlinks similarly.
883 (apply #'nconc
884 (mapcar
885 #'(lambda (filename)
886 (elinstall-find-actions-for-file
887 filename
888 load-path-element
889 dirname
890 parameters))
892 (directory-files
893 dirname
894 nil ;;Relative filenames
895 elinstall-elisp-regexp))))))
897 (def-file
898 (let
899 ((new-def-file
900 (expand-file-name
901 (second spec)
902 dir))
903 (for-preload (third spec)))
904 (assert (listp for-preload))
905 (append
906 (list
908 (and for-preload (car for-preload))
909 `(preload-file
910 ,(car for-preload)
911 ,new-def-file
912 ,@(cdr for-preload))
913 '()))
915 (elinstall-find-actions-by-spec
916 (fourth spec)
917 load-path-element
919 (elinstall-add-parameter parameters
920 'def-file new-def-file))))))
922 ;;$$IMPROVE ME by adding the other cases in the design.
923 (case spec
924 (t))))
925 ;;;_ . high-level work
926 ;;;_ , elinstall-get-relevant-load-path
927 (defun elinstall-get-relevant-load-path (actions)
929 (delq nil
930 (mapcar
931 #'(lambda (act)
932 (case (car act)
933 (add-to-load-path
934 (second act))
935 (t nil)))
936 actions)))
937 ;;;_ , elinstall-stage-update-deffiles
938 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
940 (mapcar
941 #'(lambda (segment)
942 (let*
943 ((deffile (car segment)))
944 (if (stringp deffile)
945 (elinstall-update-deffile deffile (cdr segment) force
946 use-load-path))))
947 segment-list))
948 ;;;_ , elinstall-stage-arrange-preloads
949 ;;;_ , elinstall-stage-run-tests
950 ;;;_ , elinstall-stage-byte-compile
952 ;;;_ , elinstall-do-segment
953 ;;$$OBSOLESCENT
954 (defun elinstall-do-segment (segment force use-load-path)
955 "Do all the actions in SEGMENT.
956 FORCE has its usual meaning.
957 USE-LOAD-PATH is the effective load-path."
959 (let*
960 ((deffile (car segment)))
961 (if (stringp deffile)
962 (elinstall-update-deffile deffile (cdr segment) force
963 use-load-path)
964 ;;Do other actions: link-in actions and cleanups.
965 (mapcar
966 #'(lambda (act)
968 (case (car act)
969 (preload-file
970 (let
971 ((proceed (car act)))
972 (apply
973 #'elinstall-arrange-preload
974 force
975 (cddr act))))
976 (run-tests
977 ;;$$WRITE ME - not a high priority right now.
978 nil)
980 ;;$$IMPROVE ME Understand flags to control second
981 ;;argument (whether to load file after
982 ;;compilation)
983 (byte-compile
984 (byte-compile-file (second act)))))
986 (cdr segment)))))
989 ;;;_ . Overall work
990 ;;;_ , elinstall-x
991 (defun elinstall-x (dir spec &optional force)
993 (let*
995 ;;This is just the default deffile, spec can override it.
996 (def-file
997 (elinstall-expand-deffile-name nil))
998 (actions
999 (elinstall-find-actions-by-spec
1000 spec
1004 (def-file . ,def-file ))))
1005 (stages (elinstall-segregate-actions actions))
1006 (use-load-path
1007 (elinstall-get-relevant-load-path
1008 actions)))
1010 (elinstall-stage-update-deffiles
1011 (elinstall-stages->build-deffiles stages)
1012 force
1013 use-load-path)
1014 (elinstall-do-segment
1015 (elinstall-stages->arrange-preloads stages)
1016 force
1017 use-load-path)
1020 ;;;_ . Entry points
1021 ;;;_ , elinstall
1022 ;;;###autoload
1023 (defun elinstall (project-name path spec &optional force)
1024 "Install elisp files.
1025 They need not be a formal package.
1027 Parameters:
1029 PROJECT-NAME - the name of the project
1031 PATH - Path to the project.
1032 Suggestion: (elinstall-directory-true-name)
1034 SPEC - a spec for the autoloads etc to make. It can be as simple as
1035 \(dir \"\.\") for installing one directory.
1037 If FORCE is t, install a package even if it has already been
1038 installed. Other non-nil cases of FORCE are reserved for future
1039 development."
1041 (when
1042 (and
1043 (or
1044 force
1045 (not (elinstall-already-installed project-name)))
1046 (yes-or-no-p (format "Re-install %s? " project-name)))
1047 (elinstall-x
1048 path
1049 `(def-file "loaddefs.el" (t ,project-name) ,spec)
1050 force)
1051 (elinstall-record-installed project-name)))
1055 ;;;_ , elinstall-update-directory-autoloads
1056 ;;$$TEST ME
1057 ;;;###autoload
1058 (defun elinstall-update-directory-autoloads (dir)
1061 (interactive "DInstall all elisp files from directory: ")
1064 (let
1065 ((def-file-name
1066 (elinstall-expand-deffile-name
1067 generated-autoload-file)))
1069 (elinstall-x
1071 `(def-file ,def-file-name (nil) (dir "."))
1076 ;;;_ , elinstall-update-file-autoloads
1077 ;;$$TEST ME
1078 ;;;###autoload
1079 (defun elinstall-update-file-autoloads (file)
1082 (interactive "fInstall elisp file: ")
1083 (let
1084 ((def-file-name
1086 (elinstall-maybe-get-deffile file)
1087 (elinstall-expand-deffile-name
1088 generated-autoload-file))))
1089 (elinstall
1090 file
1091 `(def-file ,def-file-name (nil) (file ,file)))))
1093 ;;;_. Footers
1094 ;;;_ , Provides
1096 (provide 'elinstall)
1098 ;;;_ * Local emacs vars.
1099 ;;;_ + Local variables:
1100 ;;;_ + mode: allout
1101 ;;;_ + End:
1103 ;;;_ , End
1104 ;;; elinstall.el ends here