Used new if-used case for default deffile.
[elinstall.git] / elinstall.el
blobb7b9e78c5e1b1209225f2db1be4cefd6c6d50280
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)
85 ;;;_ , Data
86 ;;;_ . Regular expressions
87 ;;;_ , elinstall-elisp-regexp
88 (defconst elinstall-elisp-regexp
89 (let ((tmp nil))
90 (dolist
91 (suf (get-load-suffixes))
92 (unless (string-match "\\.elc" suf) (push suf tmp)))
93 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
94 "Regular expression that matches elisp files" )
96 ;;;_ , Utilities
97 ;;;_ . elinstall-directory-true-name
98 (defun elinstall-directory-true-name ()
99 "Get the true name of the directory the calling code lives in.
100 CAUTION: This is sensitive to where it's called. That's the point of it."
101 (file-name-directory
102 (if load-file-name
103 (file-truename load-file-name)
104 (file-truename buffer-file-name))))
105 ;;;_ . Checking installedness
106 ;;;_ , elinstall-already-installed
107 (defun elinstall-already-installed (project-name)
108 "Return non-nil if PROJECT-NAME has been installed."
109 (member project-name elinstall-already-installed))
111 ;;;_ , elinstall-record-installed
112 (defun elinstall-record-installed (project-name)
113 "Record that PROJECT-NAME has been installed."
115 (add-to-list 'elinstall-already-installed project-name)
116 (customize-save-variable
117 'elinstall-already-installed
118 elinstall-already-installed
119 "Set by elinstall-record-installed"))
120 ;;;_ , Work
121 ;;;_ . Doing actions
123 ;;;_ , Doing autoload actions (adapted from autoload.el)
124 ;;;_ . Utilities about the action list
125 ;;;_ , elinstall-remove-autogen-action
126 (defun elinstall-remove-autogen-action (file actions)
127 "Return ACTIONS minus any add-file-autoloads on FILE removed."
129 (delq nil
130 (mapcar
131 #'(lambda (act)
132 (case (car act)
133 (add-file-autoloads
134 (if (equal file (third act))
136 act))
137 (t act)))
138 actions)))
139 ;;;_ , elinstall-get-autogen-action
140 (defun elinstall-get-autogen-action (file actions)
142 (let
143 ((the-act))
144 (dolist (act actions)
145 (case (car act)
146 (add-file-autoloads
147 (when (equal file (third act))
148 (setq the-act act)))))
149 the-act))
151 ;;;_ . Making autoloads
152 ;;;_ , elinstall-generate-file-autoloads
153 ;;override to allow slashed load-paths
154 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
155 ;;a new arg.
156 ;;`relative-to' can be:
157 ;; * nil: act as at present. Assume that FILE's immediate directory
158 ;;is in load-path.
159 ;; * t :: use default-directory
160 ;; * a string :: relative to it, as a filename
162 (defun elinstall-generate-file-autoloads (relative-name full-name)
163 "Insert at point a loaddefs autoload section for FILE.
164 Autoloads are generated for defuns and defmacros in FILE
165 marked by `generate-autoload-cookie' (which see).
166 If FILE is being visited in a buffer, the contents of the buffer
167 are used.
168 Return non-nil in the case where no autoloads were added at point.
170 FULL-NAME is the absolute name of the file.
171 RELATIVE-NAME is its name respective to some component of load-path."
172 (let* ((outbuf (current-buffer))
173 (autoloads-done '())
174 (print-length nil)
175 (print-readably t) ; This does something in Lucid Emacs.
176 (float-output-format nil)
177 (done-any nil)
178 (visited (get-file-buffer full-name))
179 (source-buf
180 (or visited
181 ;; It is faster to avoid visiting the file.
182 (ignore-errors (autoload-find-file full-name))))
183 output-start)
184 (if source-buf
185 (with-current-buffer source-buf
186 ;;$$MOVE ME - this should be checked in action-finding.
187 ;; Obey the no-update-autoloads file local variable.
188 (unless no-update-autoloads
189 (message "Generating autoloads for %s..." relative-name)
190 (setq output-start (with-current-buffer outbuf (point)))
191 (save-excursion
192 (save-restriction
193 (widen)
194 (goto-char (point-min))
195 (while (not (eobp))
196 (skip-chars-forward " \t\n\f")
197 (cond
198 ((looking-at (regexp-quote generate-autoload-cookie))
199 (search-forward generate-autoload-cookie)
200 (skip-chars-forward " \t")
201 (setq done-any t)
202 (if (eolp)
203 ;; Read the next form and make an autoload.
204 (let* ((form (prog1 (read (current-buffer))
205 (or (bolp) (forward-line 1))))
206 (autoload
207 (make-autoload form relative-name)))
208 (if autoload
209 (push (nth 1 form) autoloads-done)
210 (setq autoload form))
211 (let ((autoload-print-form-outbuf outbuf))
212 (autoload-print-form autoload)))
214 ;; Copy the rest of the line to the output.
215 (princ (buffer-substring
216 (progn
217 ;; Back up over whitespace, to preserve it.
218 (skip-chars-backward " \f\t")
219 (if (= (char-after (1+ (point))) ? )
220 ;; Eat one space.
221 (forward-char 1))
222 (point))
223 (progn (forward-line 1) (point)))
224 outbuf)))
225 ((looking-at ";")
226 ;; Don't read the comment.
227 (forward-line 1))
229 (forward-sexp 1)
230 (forward-line 1))))))
232 (when done-any
233 (with-current-buffer outbuf
234 (save-excursion
235 ;; Insert the section-header line which lists the file name
236 ;; and which functions are in it, etc.
237 (goto-char output-start)
238 (autoload-insert-section-header
239 outbuf autoloads-done relative-name full-name
240 (nth 5 (file-attributes full-name)))
241 (insert ";;; Generated autoloads from "
242 (autoload-trim-file-name full-name) "\n"))
243 (insert generate-autoload-section-trailer)))
244 (message "Generating autoloads for %s...done" relative-name))
246 (unless visited
247 ;; We created this buffer, so we should kill it.
248 (kill-buffer (current-buffer))))
249 (message "Could not load %s" relative-name))
251 (not done-any)))
254 ;;;_ , elinstall-deffile-insert-autoloads
255 (defun elinstall-deffile-insert-autoloads (file load-name)
256 "Update the autoloads for FILE in current buffer.
257 Return FILE if there was no autoload cookie in it, else nil.
259 Current buffer must be a loaddef-style file.
261 LOAD-NAME is the absolute name of the file.
262 RELATIVE-NAME is its name respective to some component of load-path."
263 (let (
264 (found nil)
265 (no-autoloads nil))
267 (save-excursion
268 (save-restriction
269 (widen)
270 (goto-char (point-min))
271 ;; Look for the section for FILE
272 (while (and (not found)
273 (search-forward generate-autoload-section-header nil t))
274 (let ((form (autoload-read-section-header)))
275 (cond
276 ((equal (nth 2 form) file)
277 ;; We found the section for this file.
278 (let ((begin (match-beginning 0)))
279 (progn
280 (search-forward generate-autoload-section-trailer)
281 (delete-region begin (point))
282 (setq found t))))
283 ((string< file (nth 2 form))
284 ;; We've come to a section alphabetically later than
285 ;; FILE. We assume the file is in order and so
286 ;; there must be no section for FILE. We will
287 ;; insert one before the section here.
288 (goto-char (match-beginning 0))
289 (setq found 'new)))))
290 (unless found
291 (progn
292 (setq found 'new)
293 ;; No later sections in the file. Put before the last page.
294 (goto-char (point-max))
295 (search-backward "\f" nil t)))
296 (setq no-autoloads
297 (elinstall-generate-file-autoloads file load-name))))
299 (if no-autoloads file nil)))
300 ;;;_ . Arranging to add to info-path and load-path
301 ;;;_ , elinstall-generate-add-to-path
302 (defun elinstall-generate-add-to-path (path-element type)
303 "Insert code at point to add PATH-ELEMENT to a path.
304 If TYPE is:
305 * `add-to-load-path', add to load-path
306 * `add-to-info-path', add to Info-default-directory-list
308 Current buffer must be a loaddef-style file."
309 (let ( (path-symbol
310 (case type
311 (add-to-load-path 'load-path)
312 (add-to-info-path 'Info-default-directory-list)))
313 (description
314 (case type
315 (add-to-load-path "load-path")
316 (add-to-info-path "info-path")))
317 (autoloads-done '())
318 (print-length nil)
319 (print-readably t) ; This does something in Lucid Emacs.
320 (float-output-format nil))
322 (message "Generating %s additions..." description)
324 (autoload-insert-section-header
325 (current-buffer) (list path-element) nil nil
326 nil)
327 (insert ";;; Generated path addition\n")
329 `(add-to-list ',path-symbol
330 (expand-file-name
331 ,(file-relative-name path-element)
332 (if load-file-name
333 (file-name-directory
334 (file-truename load-file-name)))))
335 (current-buffer))
337 (insert generate-autoload-section-trailer)
338 (message "Generating %s additions...done" description)))
341 ;;;_ , elinstall-deffile-insert-add-to-path
342 (defun elinstall-deffile-insert-add-to-path (path-element type)
343 "Insert code in current buffer to add PATH-ELEMENT to a path.
344 If TYPE is:
345 * `add-to-load-path', add to load-path
346 * `add-to-info-path', add to Info-default-directory-list
348 Current buffer must be a loaddef-style file."
349 (let (
350 (found nil)
351 (no-autoloads nil))
353 (save-excursion
354 (save-restriction
355 (widen)
356 (goto-char (point-min))
357 ;; Look for the section for PATH-ELEMENT
358 (while (and (not found)
359 (search-forward generate-autoload-section-header nil t))
360 (let ((form (autoload-read-section-header)))
361 (cond
362 ((and
363 (equal (nth 0 form) type)
364 (member (nth 1 form) path-element))
366 ;; We found the section for this add.
367 (let ((begin (match-beginning 0)))
368 (progn
369 (search-forward generate-autoload-section-trailer)
370 (delete-region begin (point))
371 (setq found t)))))))
373 (unless found
374 (progn
375 (setq found 'new)
376 ;; No later sections in the file. Put before the last page.
377 (goto-char (point-max))
378 (search-backward "\f" nil t)))
380 (elinstall-generate-add-to-path path-element type)))
382 ;;This never belongs in the no-autoloads section.
383 nil))
385 ;;;_ . elinstall-deffile-insert
387 (defun elinstall-deffile-insert (action)
388 "Insert autoloads etc into current file according to ACTION.
389 The format of ACTION is described in the design docs.
391 Return filename if this action belongs in the no-autoload section."
393 (when action
394 (case (car action)
395 (add-file-autoloads
396 (elinstall-deffile-insert-autoloads
397 (third action)
398 (fifth action)))
400 (add-to-load-path
401 (elinstall-deffile-insert-add-to-path
402 (third action)
403 'add-to-load-path)
404 nil)
406 (add-to-info-path
407 (elinstall-deffile-insert-add-to-path
408 (third action)
409 'add-to-info-path)
410 nil)
412 ((preload-file run-tests byte-compile)
413 (error "This case should not come here.")))))
415 ;;;_ . elinstall-prepare-deffile
416 (defun elinstall-prepare-deffile (deffile)
417 "Try to ensure that DEFFILE is available for receiving autoloads"
419 (autoload-ensure-default-file deffile)
420 (with-current-buffer (find-file-noselect deffile)
423 ;; We must read/write the file without any code conversion,
424 ;; but still decode EOLs.
425 (let ((coding-system-for-read 'raw-text))
427 ;; This is to make generated-autoload-file have Unix EOLs, so
428 ;; that it is portable to all platforms.
429 (setq buffer-file-coding-system 'raw-text-unix))
430 (or (> (buffer-size) 0)
431 (error "Autoloads file %s does not exist" buffer-file-name))
432 (or (file-writable-p buffer-file-name)
433 (error "Autoloads file %s is not writable"
434 buffer-file-name))))
436 ;;;_ . elinstall-update-deffile
438 ;;Adapted from autoload.el `update-directory-autoloads'.
440 (defun elinstall-update-deffile (target actions &optional
441 use-load-path force)
443 Update file TARGET with current autoloads as specified by ACTIONS.
444 Also remove any old definitions pointing to libraries that can no
445 longer be found.
447 ACTIONS must be a list of actions (See the format doc). Each one's
448 filename must be relative to some element of load-path.
450 USE-LOAD-PATH is a list to use as load-path. It should include
451 any new load-path that we are arranging to create. If it's not given,
452 load-path itself is used.
454 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
455 Other non-nil cases of FORCE are reserved for future development.
457 This uses `update-file-autoloads' (which see) to do its work.
458 In an interactive call, you must give one argument, the name
459 of a single directory."
460 (let
462 (use-load-path (or use-load-path load-path))
463 (this-time (current-time))
464 ;;files with no autoload cookies.
465 (no-autoloads nil))
467 (elinstall-prepare-deffile target)
468 (with-current-buffer
469 (find-file-noselect target)
470 (save-excursion
471 (setq actions
472 (elinstall-remove-autogen-action
473 (autoload-trim-file-name target)
474 actions))
476 (goto-char (point-min))
477 (while (search-forward generate-autoload-section-header nil t)
478 (let* ((form (autoload-read-section-header))
479 (file (nth 3 form)))
480 (cond ((and (consp file) (stringp (car file)))
481 ;; This is a list of files that have no
482 ;; autoload cookies.
483 ;; There shouldn't be more than one such entry.
484 ;; Remove the obsolete section.
485 (autoload-remove-section (match-beginning 0))
486 (let ((last-time (nth 4 form)))
487 (dolist (file file)
488 (let ((file-time (nth 5 (file-attributes file))))
489 (when (and file-time
490 (not (time-less-p last-time file-time)))
491 ;; file unchanged
492 (push file no-autoloads)
493 (setq actions
494 (elinstall-remove-autogen-action
495 file actions)))))))
496 ((not (stringp file)))
498 (let
499 ((file-path
500 (locate-library file nil use-load-path)))
501 (cond
502 ;;File doesn't exist, so remove its
503 ;;section.
504 ((not file-path)
505 (autoload-remove-section
506 (match-beginning 0)))
508 ;; File hasn't changed, so do nothing.
509 ((equal
510 (nth 4 form)
511 (nth 5 (file-attributes file-path)))
512 nil)
514 (elinstall-deffile-insert
515 (elinstall-get-autogen-action
516 file actions))))
518 (setq actions
519 (elinstall-remove-autogen-action
520 file actions))))))))
522 ;; Remaining actions have no existing autoload sections yet.
523 (setq no-autoloads
524 (append no-autoloads
525 (delq nil (mapcar #'elinstall-deffile-insert actions))))
526 (when no-autoloads
527 ;; Sort them for better readability.
528 (setq no-autoloads (sort no-autoloads 'string<))
529 ;; Add the `no-autoloads' section.
530 (goto-char (point-max))
531 (search-backward "\f" nil t)
532 (autoload-insert-section-header
533 (current-buffer) nil nil no-autoloads this-time)
534 (insert generate-autoload-section-trailer))
535 (save-buffer))))
537 ;;;_ . elinstall-stage-update-deffiles
538 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
539 "Update any deffiles mentioned in SEGMENT-LIST.
540 FORCE and USE-LOAD-PATH have the same meaning as in
541 `elinstall-update-deffile'.
543 (mapcar
544 #'(lambda (segment)
545 (let*
546 ((deffile (car segment)))
547 (if (stringp deffile)
548 (elinstall-update-deffile deffile (cdr segment) force
549 use-load-path))))
550 segment-list))
552 ;;;_ , Doing actions to arrange preloads
553 ;;;_ . elinstall-insert-add-to-path
554 (defun elinstall-insert-add-to-path (new path-sym)
555 "Insert code to add NEW to PATH-SYM.
556 Insert this at point in current buffer."
557 (insert "\n")
559 `(add-to-list ',path-sym
560 (expand-file-name ,new
561 (if load-file-name
562 (file-name-directory
563 (file-truename load-file-name)))))
564 (current-buffer)))
566 ;;;_ . elinstall-insert-add-to-load-path
567 (defun elinstall-insert-add-to-load-path (new)
568 "Insert code to add NEW to load-path.
569 Insert this at point in current buffer."
570 (elinstall-insert-add-to-path new 'load-path))
572 ;;;_ . elinstall-insert-add-to-info-path
573 (defun elinstall-insert-add-to-info-path (new)
574 "Insert code to add NEW to info-path.
575 Insert this at point in current buffer."
576 (elinstall-insert-add-to-path new 'Info-default-directory-list))
578 ;;;_ . elinstall-symlink-on-emacs-start
579 (defun elinstall-symlink-on-emacs-start
580 (filename target-basename target-dir &optional priority force)
581 "Symlink to TARGET-BASENAME.el in TARGET-DIR
583 If PRIORITY is given, it will be used as the priority prefix,
584 otherwise elinstall-default-priority will be.
585 PRIORITY must be an integer or nil.
586 If FORCE is `t', do it regardless of timestamps etc.
587 Other non-nil cases of FORCE are reserved for future development."
588 (let*
590 (priority (or priority elinstall-default-priority))
591 (target-name-nodir
592 (format
593 "%d%s.el"
594 priority
595 target-basename))
596 (target
597 (expand-file-name target-name-nodir target-dir)))
600 (cond
601 ;;Path should already exist.
602 ((not
603 (file-exists-p target-dir))
604 (message "The target directory doesn't exist."))
605 ;;Target shouldn't already exist, but if force is given, let
606 ;;user override.
607 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
608 ;;do nothing even on force.
609 ((and
610 (file-exists-p target)
612 (not force)
613 (not
614 (yes-or-no-p
615 (format "Really overwrite %s? " project-name))))
616 (message "File %s already exists" target)))
619 (make-symbolic-link
620 filename
621 target
622 nil)))))
624 ;;;_ . elinstall-add-to-dot-emacs
625 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
626 "Add code to load FILENAME to .emacs.
627 FILENAME should not have an extension"
629 ;;Visit .emacs
630 (with-current-buffer (find-file-noselect dot-emacs-name)
631 (save-excursion
632 ;;add at end of file
633 (goto-char (point-max))
634 (insert "\n;;Added by elinstall")
635 (insert "\n;;Consider using my-site-start to manage .emacs\n")
636 (pp `(load ,filename) (current-buffer))
637 (save-buffer))))
640 ;;;_ . elinstall-arrange-preload
641 ;;;###autoload
642 (defun elinstall-arrange-preload (force filename basename &optional priority)
643 "Arrange for FILENAME to be loaded on emacs start.
644 FORCE has its usual meaning.
645 BASENAME and PRIORITY are used as arguments to
646 `elinstall-symlink-on-emacs-start'.
649 (let
650 ((preload-target elinstall-default-preload-target))
652 ;;Dispatch the possibilities.
653 (cond
654 ((eq preload-target 'dot-emacs)
655 (elinstall-add-to-dot-emacs "~/.emacs" filename))
656 ((stringp preload-target)
657 (elinstall-symlink-on-emacs-start
658 filename basename preload-target priority force))
659 (null preload-target
660 (message "Not arranging for preloads"))
662 (message "I don't recognize that")))))
663 ;;;_ . elinstall-stage-arrange-preloads
664 (defun elinstall-stage-arrange-preloads (actions deffiles-used)
665 "Arrange any preloads mentioned in ACTIONS."
667 (mapcar
668 #'(lambda (act)
669 (case (car act)
670 (preload-file
671 (let*
672 ( (filename
673 (caddr act))
674 (proceed
675 (case (car act)
676 (t t)
677 ((nil) nil)
678 (if-used
679 (member filename deffiles-used)))))
681 (when proceed
682 (apply
683 #'elinstall-arrange-preload
684 force
685 (cddr act)))))
687 (error
688 "elinstall-stage-arrange-preloads: Action not
689 recognized."))) )
690 actions))
693 ;;;_ , Run tests
694 ;;;_ . elinstall-stage-run-tests
695 (defun elinstall-stage-run-tests (actions)
696 "Run any tests mentioned in ACTIONS."
698 (mapcar
699 #'(lambda (act)
700 (case (car act)
701 (run-tests
702 ;;$$WRITE ME - not a high priority right now.
703 nil)
705 (error
706 "elinstall-stage-run-tests: Action not
707 recognized."))) )
708 actions))
711 ;;;_ , Byte compile
712 ;;;_ . elinstall-stage-byte-compile
713 (defun elinstall-stage-byte-compile (actions)
714 "Do any byte-compilation mentioned in ACTIONS."
716 (mapcar
717 #'(lambda (act)
718 (case (car act)
719 ;;$$IMPROVE ME Understand flags to control second
720 ;;argument (whether to load file after
721 ;;compilation)
722 (byte-compile
723 (byte-compile-file (second act)))
725 (error
726 "elinstall-stage-byte-compile: Action not
727 recognized."))) )
728 actions))
730 ;;;_ . Segregating actions
731 ;;;_ , elinstall-remove-empty-segs
732 (defun elinstall-remove-empty-segs (segment-list)
733 "Return SEGMENT-LIST minus any segments that have no actions.
734 Intended only for the deffile stage data."
735 (delq nil
736 (mapcar
737 #'(lambda (segment)
738 (if (cdr segment)
739 segment
740 nil))
741 segment-list)))
743 ;;;_ , elinstall-segregate-actions
744 (defun elinstall-segregate-actions (actions)
745 "Return actions segregated by deffile.
747 Returns a list whose elements are each a cons of:
748 * deffile filename or nil
749 * A list of actions to be done for that deffile."
751 (let
753 (build-deffiles '())
754 (run-tests '())
755 (byte-compile '())
756 (arrange-preloads '()))
758 (dolist (act actions)
759 (when act
760 (case (car act)
761 ((add-file-autoloads
762 add-to-info-path
763 add-to-load-path)
764 (let*
765 ((deffile-name (second act))
766 (cell-already
767 (assoc deffile-name build-deffiles)))
768 (if cell-already
769 ;;There are already actions on this deffile.
770 ;;Splice this action in.
771 (setcdr cell-already
772 (cons act (cdr cell-already)))
773 ;;There are no actions on this deffile. Add a
774 ;;place for them and include this action.
775 (push (list deffile-name act) build-deffiles))))
776 (preload-file
777 (push act arrange-preloads))
778 (run-tests
779 (push act run-tests))
780 (byte-compile
781 (push act byte-compile)))))
783 (elinstall-make-stages
784 :build-deffiles
785 (elinstall-remove-empty-segs build-deffiles)
786 :run-tests
787 run-tests
788 :byte-compile
789 byte-compile
790 :arrange-preloads
791 arrange-preloads)))
797 ;;;_ . Finding actions
798 ;;;_ , Treating the parameter list
799 ;;;_ . elinstall-add-parameter
800 (defun elinstall-add-parameter (alist key new-value)
801 "Add a new value for KEY to ALIST"
803 (cons
804 (cons key new-value)
805 (assq-delete-all key (copy-list alist))))
807 ;;;_ . elinstall-get-parameter
808 (defun elinstall-get-parameter (alist key)
809 "Get the value of KEY from ALIST"
811 (cdr (assq key alist)))
812 ;;;_ . elinstall-expand-file-name
813 ;;$$OBSOLETE
815 (defun elinstall-expand-file-name (filename alist)
816 "Expand FILENAME by the value of `path' in ALIST"
817 (expand-file-name
818 filename
819 (elinstall-get-parameter alist 'path)))
820 ;;;_ , Finding deffiles
821 ;;;_ . elinstall-expand-deffile-name
822 (defun elinstall-expand-deffile-name (deffile)
823 "Expand DEFFILE autoload.el's way."
825 (expand-file-name (or deffile "loaddefs.el")
826 (expand-file-name "lisp"
827 source-directory)))
829 ;;;_ . elinstall-maybe-get-deffile
830 (defun elinstall-maybe-get-deffile (file)
831 "If FILE defined `generated-autoload-file', return it.
832 Otherwise return nil.
833 Return it as an absolute filename."
835 (save-excursion
836 ;;$$FIXME load buffer if it's not already loaded
837 (let*
838 ((existing-buffer (get-file-buffer file)))
840 ;; We want to get a value for generated-autoload-file from
841 ;; the local variables section if it's there.
842 ;;But if it's not loaded, we don't? Maybe should use
843 ;; `autoload-find-file' and load it.
844 (if existing-buffer
845 (set-buffer existing-buffer))
846 (if (local-variable-p 'generated-autoload-file)
847 (elinstall-expand-deffile-name
848 generated-autoload-file)
849 nil))))
852 ;;;_ , Workers
853 ;;;_ . elinstall-find-actions-for-file
854 (defun elinstall-find-actions-for-file
855 (filename load-path-element dir parameters)
856 "Return a list of actions to do for FILENAME.
857 LOAD-PATH-ELEMENT, DIR, and PARAMETERS are interpreted as in
858 `elinstall-find-actions-by-spec' "
860 (let
861 ((full-path
862 (expand-file-name filename dir)))
863 (list
864 `(add-file-autoloads
865 ,(elinstall-get-parameter
866 parameters 'def-file)
867 ;;load-name relative to a member of load-path
868 ,(file-name-sans-extension
869 (file-relative-name
870 full-path
871 load-path-element))
872 ,load-path-element ;;Is this still used?
873 ,full-path))))
875 ;;;_ . elinstall-find-actions-by-spec
877 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters)
878 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
880 LOAD-PATH-ELEMENT is the conceptual element of load-path that
881 surrounds DIR. It may not yet have been added to load-path."
882 (if (consp spec)
883 ;;$$IMPROVE ME by adding the other cases in the design.
884 (case (car spec)
886 (let
887 ((new-path
888 (expand-file-name
889 (second spec)
890 dir)))
892 (elinstall-find-actions-by-spec
893 (third spec)
894 load-path-element
895 new-path
896 parameters)))
898 (all
899 (apply #'nconc
900 (mapcar
901 #'(lambda (sub-spec)
902 (elinstall-find-actions-by-spec
903 sub-spec
904 load-path-element
906 parameters))
907 (cdr spec))))
909 (file
910 (elinstall-find-actions-for-file
911 filename load-path-element dir parameters))
913 (dir
914 (let*
915 ((dirname
916 (expand-file-name
917 (second spec)
918 dir))
919 (load-path-here
920 (not
921 (elinstall-get-parameter
922 parameters 'block-add-to-load-path)))
923 (load-path-element
924 (if load-path-here
925 dirname
926 load-path-element)))
928 (cons
929 ;;$$IMPROVE ME
930 ;;Do this only if there are loadable files.
931 (if load-path-here
932 `(add-to-load-path
933 ,(elinstall-get-parameter
934 parameters 'def-file)
935 ,load-path-element)
936 '())
937 ;;$$IMPROVE ME
938 ;;Do add-to-info-path too. But test if there are
939 ;;any info files present.
941 ;;$$IMPROVE ME
942 ;; We want to get a value for generated-autoload-file
943 ;; from the local variables section if it's there.
944 ;;Use `elinstall-maybe-get-deffile'
945 ;; Otherwise we'll use `def-file' in parameters.
947 ;;$$FIXME This isn't quite right. If directory
948 ;;itself is not in load-path, this will be wrong.
949 ;;Gotta know where our encompassing part of
950 ;;load-path is.
952 ;;$$ENCAP ME This should be shared with the
953 ;;treatment of (file FN)
955 ;;$$FIXME Don't do directories, but maybe recurse on
956 ;;them, if a flag is set. And since we definitely
957 ;;have a load-path element here,
958 ;;'block-add-to-load-path according to a parameter.
959 ;;Maybe could follow/not symlinks similarly.
960 (apply #'nconc
961 (mapcar
962 #'(lambda (filename)
963 (elinstall-find-actions-for-file
964 filename
965 load-path-element
966 dirname
967 parameters))
969 (directory-files
970 dirname
971 nil ;;Relative filenames
972 elinstall-elisp-regexp))))))
974 (def-file
975 (let
976 ((new-def-file
977 (expand-file-name
978 (second spec)
979 dir))
980 (for-preload (third spec)))
981 (assert (listp for-preload))
982 (append
983 (list
985 (and for-preload (car for-preload))
986 `(preload-file
987 ,(car for-preload)
988 ,new-def-file
989 ,@(cdr for-preload))
990 '()))
992 (elinstall-find-actions-by-spec
993 (fourth spec)
994 load-path-element
996 (elinstall-add-parameter parameters
997 'def-file new-def-file))))))
999 ;;$$IMPROVE ME by adding the other cases in the design.
1000 (case spec
1001 (t))))
1002 ;;;_ . high-level work
1003 ;;;_ , elinstall-get-relevant-load-path
1004 (defun elinstall-get-relevant-load-path (actions)
1006 (delq nil
1007 (mapcar
1008 #'(lambda (act)
1009 (case (car act)
1010 (add-to-load-path
1011 (second act))
1012 (t nil)))
1013 actions)))
1014 ;;;_ , elinstall-get-deffile-list
1015 (defun elinstall-get-deffile-list (stages)
1016 "Get a list of deffile names"
1018 (mapcar
1019 #'car
1020 (elinstall-stages->build-deffiles stages)))
1022 ;;;_ , elinstall-x
1023 (defun elinstall-x (dir spec &optional force)
1025 (let*
1027 ;;This is just the default deffile, spec can override it.
1028 (def-file
1029 (elinstall-expand-deffile-name nil))
1030 (actions
1031 (elinstall-find-actions-by-spec
1032 spec
1036 (def-file . ,def-file ))))
1037 (stages (elinstall-segregate-actions actions))
1038 (use-load-path
1039 (elinstall-get-relevant-load-path
1040 actions)))
1042 (elinstall-stage-update-deffiles
1043 (elinstall-stages->build-deffiles stages)
1044 force
1045 use-load-path)
1046 (elinstall-stage-arrange-preloads
1047 (elinstall-stages->arrange-preloads stages)
1048 (elinstall-get-deffile-list stages))
1051 ;;;_ . Entry points
1052 ;;;_ , elinstall
1053 ;;;###autoload
1054 (defun elinstall (project-name path spec &optional force)
1055 "Install elisp files.
1056 They need not be a formal package.
1058 Parameters:
1060 PROJECT-NAME - the name of the project
1062 PATH - Path to the project.
1063 Suggestion: (elinstall-directory-true-name)
1065 SPEC - a spec for the autoloads etc to make. It can be as simple as
1066 \(dir \"\.\") for installing one directory.
1068 If FORCE is t, install a package even if it has already been
1069 installed. Other non-nil cases of FORCE are reserved for future
1070 development."
1072 (when
1073 (and
1074 (or
1075 force
1076 (not (elinstall-already-installed project-name)))
1077 (yes-or-no-p (format "Re-install %s? " project-name)))
1078 (elinstall-x
1079 path
1080 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1081 force)
1082 (elinstall-record-installed project-name)))
1086 ;;;_ , elinstall-update-directory-autoloads
1087 ;;$$TEST ME
1088 ;;;###autoload
1089 (defun elinstall-update-directory-autoloads (dir)
1092 (interactive "DInstall all elisp files from directory: ")
1095 (let
1096 ((def-file-name
1097 (elinstall-expand-deffile-name
1098 generated-autoload-file)))
1100 (elinstall-x
1102 `(def-file ,def-file-name (nil) (dir ".")))))
1106 ;;;_ , elinstall-update-file-autoloads
1107 ;;$$TEST ME
1108 ;;;###autoload
1109 (defun elinstall-update-file-autoloads (file)
1112 (interactive "fInstall elisp file: ")
1113 (let
1114 ((def-file-name
1116 (elinstall-maybe-get-deffile file)
1117 (elinstall-expand-deffile-name
1118 generated-autoload-file))))
1119 (elinstall
1120 file
1121 `(def-file ,def-file-name (nil) (file ,file)))))
1123 ;;;_. Footers
1124 ;;;_ , Provides
1126 (provide 'elinstall)
1128 ;;;_ * Local emacs vars.
1129 ;;;_ + Local variables:
1130 ;;;_ + mode: allout
1131 ;;;_ + End:
1133 ;;;_ , End
1134 ;;; elinstall.el ends here