Files and their local vars are now explored in find-action stage.
[elinstall.git] / elinstall.el
blobf457bed96db371e31b41f9cbe0b9df397836166e
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
41 (require 'byte-compile nil t) ;;
44 ;;;_. Body
45 ;;;_ , Customizations
46 ;;;_ . Group
47 (defgroup elinstall
48 '()
49 "Customizations for elinstall"
50 :group 'development)
51 ;;;_ . elinstall-default-priority
52 (defcustom elinstall-default-priority
54 "Default priority for site-start"
55 :group 'elinstall
56 :type 'integer)
57 ;;;_ . elinstall-default-preload-target
58 (defcustom elinstall-default-preload-target
59 "~/.emacs.d/site-start.d/"
60 "Default preload-target for registering autoloads"
61 :group 'elinstall
62 :type
63 '(choice
64 (const "~/.emacs.d/site-start.d/")
65 (const "/etc/emacs/site-start.d/")
66 (directory "" )
67 (const nil)
68 (const 'dot-emacs)))
69 ;;;_ . elinstall-already-installed
70 (defvar elinstall-already-installed
71 '()
72 "(AUTOMATIC) Things that have already been installed.
73 This exists for recording what has been installed.
75 Though it's saved as customizable, user interaction is not
76 contemplated." )
77 ;;;_ , Types
78 ;;;_ . elinstall-stages
79 (defstruct (elinstall-stages
80 (:constructor elinstall-make-stages)
81 (:conc-name elinstall-stages->)
82 (:copier nil))
83 "The elinstall stages"
84 build-deffiles
85 run-tests
86 byte-compile
87 arrange-preloads)
88 ;;;_ , Data
89 ;;;_ . Regular expressions
90 ;;;_ , elinstall-elisp-regexp
91 (defconst elinstall-elisp-regexp
92 (let ((tmp nil))
93 (dolist
94 (suf (get-load-suffixes))
95 (unless (string-match "\\.elc" suf) (push suf tmp)))
96 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
97 "Regular expression that matches elisp files" )
98 ;;;_ , Utilities
99 ;;;_ . elinstall-directory-true-name
100 (defun elinstall-directory-true-name ()
101 "Get the true name of the directory the calling code lives in.
102 CAUTION: This is sensitive to where it's called. That's the point of it."
103 (file-name-directory
104 (if load-file-name
105 (file-truename load-file-name)
106 (file-truename buffer-file-name))))
107 ;;;_ . Checking installedness
108 ;;;_ , elinstall-already-installed
109 (defun elinstall-already-installed (project-name)
110 "Return non-nil if PROJECT-NAME has been installed."
111 (member project-name elinstall-already-installed))
113 ;;;_ , elinstall-record-installed
114 (defun elinstall-record-installed (project-name)
115 "Record that PROJECT-NAME has been installed."
117 (add-to-list 'elinstall-already-installed project-name)
118 (customize-save-variable
119 'elinstall-already-installed
120 elinstall-already-installed
121 "Set by elinstall-record-installed"))
122 ;;;_ , Work
123 ;;;_ . Doing actions
125 ;;;_ , Doing autoload actions (adapted from autoload.el)
126 ;;;_ . Utilities about the action list
127 ;;;_ , elinstall-remove-autogen-action
128 (defun elinstall-remove-autogen-action (file actions)
129 "Return ACTIONS minus any add-file-autoloads on FILE removed."
131 (delq nil
132 (mapcar
133 #'(lambda (act)
134 (case (car act)
135 (add-file-autoloads
136 (if (equal file (third act))
138 act))
139 (t act)))
140 actions)))
141 ;;;_ , elinstall-get-autogen-action
142 (defun elinstall-get-autogen-action (file actions)
144 (let
145 ((the-act))
146 (dolist (act actions)
147 (case (car act)
148 (add-file-autoloads
149 (when (equal file (third act))
150 (setq the-act act)))))
151 the-act))
152 ;;;_ . elinstall-insert-section-header
153 (defun elinstall-insert-section-header (outbuf form)
154 "Insert the section-header line,
155 which lists the file name and which functions are in it, etc."
156 (insert generate-autoload-section-header)
157 (prin1 form outbuf)
158 (terpri outbuf)
159 ;; Break that line at spaces, to avoid very long lines.
160 ;; Make each sub-line into a comment.
161 (with-current-buffer outbuf
162 (save-excursion
163 (forward-line -1)
164 (while (not (eolp))
165 (move-to-column 64)
166 (skip-chars-forward "^ \n")
167 (or (eolp)
168 (insert "\n" generate-autoload-section-continuation))))))
170 ;;;_ . Making autoloads
171 ;;;_ , elinstall-generate-file-autoloads
172 ;;override to allow slashed load-paths
173 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
174 ;;a new arg.
175 ;;`relative-to' can be:
176 ;; * nil: act as at present. Assume that FILE's immediate directory
177 ;;is in load-path.
178 ;; * t :: use default-directory
179 ;; * a string :: relative to it, as a filename
181 (defun elinstall-generate-file-autoloads (relative-name full-name)
182 "Insert at point a loaddefs autoload section for FILE.
183 Autoloads are generated for defuns and defmacros in FILE
184 marked by `generate-autoload-cookie' (which see).
185 If FILE is being visited in a buffer, the contents of the buffer
186 are used.
187 Return non-nil in the case where no autoloads were added at point.
189 FULL-NAME is the absolute name of the file.
190 RELATIVE-NAME is its name respective to some component of load-path."
191 (let* ((outbuf (current-buffer))
192 (autoloads-done '())
193 (print-length nil)
194 (print-readably t) ; This does something in Lucid Emacs.
195 (float-output-format nil)
196 (done-any nil)
197 (visited (get-file-buffer full-name))
198 (source-buf
199 (or visited
200 ;; It is faster to avoid visiting the file.
201 (ignore-errors (autoload-find-file full-name))))
202 output-start)
203 (if source-buf
204 (with-current-buffer source-buf
205 ;;$$MOVE ME - this should be checked in action-finding.
206 ;; Obey the no-update-autoloads file local variable.
207 (unless no-update-autoloads
208 (message "Generating autoloads for %s..." relative-name)
209 (setq output-start (with-current-buffer outbuf (point)))
210 (save-excursion
211 (save-restriction
212 (widen)
213 (goto-char (point-min))
214 (while (not (eobp))
215 (skip-chars-forward " \t\n\f")
216 (cond
217 ((looking-at (regexp-quote generate-autoload-cookie))
218 (search-forward generate-autoload-cookie)
219 (skip-chars-forward " \t")
220 (setq done-any t)
221 (if (eolp)
222 ;; Read the next form and make an autoload.
223 (let* ((form (prog1 (read (current-buffer))
224 (or (bolp) (forward-line 1))))
225 (autoload
226 (make-autoload form relative-name)))
227 (if autoload
228 (push (nth 1 form) autoloads-done)
229 (setq autoload form))
230 (let ((autoload-print-form-outbuf outbuf))
231 (autoload-print-form autoload)))
233 ;; Copy the rest of the line to the output.
234 (princ (buffer-substring
235 (progn
236 ;; Back up over whitespace, to preserve it.
237 (skip-chars-backward " \f\t")
238 (if (= (char-after (1+ (point))) ? )
239 ;; Eat one space.
240 (forward-char 1))
241 (point))
242 (progn (forward-line 1) (point)))
243 outbuf)))
244 ((looking-at ";")
245 ;; Don't read the comment.
246 (forward-line 1))
248 (forward-sexp 1)
249 (forward-line 1))))))
251 (when done-any
252 (with-current-buffer outbuf
253 (save-excursion
254 ;; Insert the section-header line which lists the file name
255 ;; and which functions are in it, etc.
256 (goto-char output-start)
257 (elinstall-insert-section-header
258 outbuf
259 (list 'autoloads
260 autoloads-done
261 relative-name
262 (autoload-trim-file-name full-name)
263 (nth 5 (file-attributes full-name))))
265 (insert ";;; Generated autoloads from "
266 (autoload-trim-file-name full-name) "\n"))
267 (insert generate-autoload-section-trailer)))
268 (message "Generating autoloads for %s...done" relative-name))
270 (unless visited
271 ;; We created this buffer, so we should kill it.
272 (kill-buffer (current-buffer))))
273 (message "Could not load %s" relative-name))
275 (not done-any)))
278 ;;;_ , elinstall-deffile-insert-autoloads
279 (defun elinstall-deffile-insert-autoloads (file load-name)
280 "Update the autoloads for FILE in current buffer.
281 Return FILE if there was no autoload cookie in it, else nil.
283 Current buffer must be a loaddef-style file.
285 LOAD-NAME is the absolute name of the file.
286 RELATIVE-NAME is its name respective to some component of load-path."
287 (let (
288 (found nil)
289 (no-autoloads nil))
291 (save-excursion
292 (save-restriction
293 (widen)
294 (goto-char (point-min))
295 ;; Look for the section for FILE
296 (while (and (not found)
297 (search-forward generate-autoload-section-header nil t))
298 (let ((form (autoload-read-section-header)))
299 (cond
300 ((equal (nth 2 form) file)
301 ;; We found the section for this file.
302 (let ((begin (match-beginning 0)))
303 (progn
304 (search-forward generate-autoload-section-trailer)
305 (delete-region begin (point))
306 (setq found t))))
307 ((string< file (nth 2 form))
308 ;; We've come to a section alphabetically later than
309 ;; FILE. We assume the file is in order and so
310 ;; there must be no section for FILE. We will
311 ;; insert one before the section here.
312 (goto-char (match-beginning 0))
313 (setq found 'new)))))
314 (unless found
315 (progn
316 (setq found 'new)
317 ;; No later sections in the file. Put before the last page.
318 (goto-char (point-max))
319 (search-backward "\f" nil t)))
320 (setq no-autoloads
321 (elinstall-generate-file-autoloads file load-name))))
323 (if no-autoloads file nil)))
324 ;;;_ . Arranging to add to info-path and load-path
325 ;;;_ , elinstall-generate-add-to-path
326 (defun elinstall-generate-add-to-path (path-element type)
327 "Insert code at point to add PATH-ELEMENT to a path.
328 If TYPE is:
329 * `add-to-load-path', add to load-path
330 * `add-to-info-path', add to Info-default-directory-list
332 Current buffer must be a loaddef-style file."
333 (let ( (path-symbol
334 (case type
335 (add-to-load-path 'load-path)
336 (add-to-info-path 'Info-default-directory-list)
337 (t (error "Type not recognized"))))
338 (description
339 (case type
340 (add-to-load-path "load-path")
341 (add-to-info-path "info-path")))
342 (autoloads-done '())
343 (print-length nil)
344 (print-readably t) ; This does something in Lucid Emacs.
345 (float-output-format nil))
347 (message "Generating %s additions..." description)
350 (elinstall-insert-section-header
351 (current-buffer)
352 (list type (list path-element) nil nil nil))
354 (insert ";;; Generated path addition\n")
356 `(add-to-list ',path-symbol
357 (expand-file-name
358 ,(file-relative-name path-element)
359 (if load-file-name
360 (file-name-directory
361 (file-truename load-file-name)))))
362 (current-buffer))
364 (insert generate-autoload-section-trailer)
365 (message "Generating %s additions...done" description)))
368 ;;;_ , elinstall-deffile-insert-add-to-path
369 (defun elinstall-deffile-insert-add-to-path (path-element type)
370 "Insert code in current buffer to add PATH-ELEMENT to a path.
371 If TYPE is:
372 * `add-to-load-path', add to load-path
373 * `add-to-info-path', add to Info-default-directory-list
375 Current buffer must be a loaddef-style file."
376 (let (
377 (found nil)
378 (no-autoloads nil))
380 (save-excursion
381 (save-restriction
382 (widen)
383 (goto-char (point-min))
384 ;; Look for the section for PATH-ELEMENT
385 (while (and (not found)
386 (search-forward generate-autoload-section-header nil t))
387 (let ((form (autoload-read-section-header)))
388 (cond
389 ((and
390 (equal (nth 0 form) type)
391 (member path-element (nth 1 form)))
393 ;; We found the section for this add.
394 (let ((begin (match-beginning 0)))
395 (progn
396 (search-forward generate-autoload-section-trailer)
397 (delete-region begin (point))
398 (setq found t)))))))
400 (unless found
401 (progn
402 (setq found 'new)
403 ;; No later sections in the file. Put before the last page.
404 (goto-char (point-max))
405 (search-backward "\f" nil t)))
407 (elinstall-generate-add-to-path path-element type)))
409 ;;This never belongs in the no-autoloads section.
410 nil))
411 ;;;_ . elinstall-deffile-insert
413 (defun elinstall-deffile-insert (action)
414 "Insert autoloads etc into current file according to ACTION.
415 The format of ACTION is described in the design docs.
417 Return filename if this action belongs in the no-autoload section."
419 (when action
420 (case (car action)
421 (add-file-autoloads
422 (elinstall-deffile-insert-autoloads
423 (third action)
424 (fifth action)))
426 (add-to-load-path
427 (elinstall-deffile-insert-add-to-path
428 (third action)
429 'add-to-load-path)
430 nil)
432 (add-to-info-path
433 (elinstall-deffile-insert-add-to-path
434 (third action)
435 'add-to-info-path)
436 nil)
438 ((preload-file run-tests byte-compile)
439 (error "This case should not come here.")))))
441 ;;;_ . elinstall-prepare-deffile
442 (defun elinstall-prepare-deffile (deffile)
443 "Try to ensure that DEFFILE is available for receiving autoloads"
445 (autoload-ensure-default-file deffile)
446 (with-current-buffer (find-file-noselect deffile)
449 ;; We must read/write the file without any code conversion,
450 ;; but still decode EOLs.
451 (let ((coding-system-for-read 'raw-text))
453 ;; This is to make generated-autoload-file have Unix EOLs, so
454 ;; that it is portable to all platforms.
455 (setq buffer-file-coding-system 'raw-text-unix))
456 (or (> (buffer-size) 0)
457 (error "Autoloads file %s does not exist" buffer-file-name))
458 (or (file-writable-p buffer-file-name)
459 (error "Autoloads file %s is not writable"
460 buffer-file-name))))
462 ;;;_ . elinstall-update-deffile
464 ;;Adapted from autoload.el `update-directory-autoloads'.
466 (defun elinstall-update-deffile (target actions &optional
467 use-load-path force)
469 Update file TARGET with current autoloads as specified by ACTIONS.
470 Also remove any old definitions pointing to libraries that can no
471 longer be found.
473 ACTIONS must be a list of actions (See the format doc). Each one's
474 filename must be relative to some element of load-path.
476 USE-LOAD-PATH is a list to use as load-path. It should include
477 any new load-path that we are arranging to create. If it's not given,
478 load-path itself is used.
480 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
481 Other non-nil cases of FORCE are reserved for future development.
483 This uses `update-file-autoloads' (which see) to do its work.
484 In an interactive call, you must give one argument, the name
485 of a single directory."
486 (let
488 (use-load-path (or use-load-path load-path))
489 (this-time (current-time))
490 ;;files with no autoload cookies.
491 (no-autoloads nil))
493 (elinstall-prepare-deffile target)
494 (with-current-buffer
495 (find-file-noselect target)
496 (save-excursion
497 (setq actions
498 (elinstall-remove-autogen-action
499 (autoload-trim-file-name target)
500 actions))
502 (goto-char (point-min))
503 (while (search-forward generate-autoload-section-header nil t)
504 (let* ((form (autoload-read-section-header))
505 (file (nth 3 form)))
506 (cond ((and (consp file) (stringp (car file)))
507 ;; This is a list of files that have no
508 ;; autoload cookies.
509 ;; There shouldn't be more than one such entry.
510 ;; Remove the obsolete section.
511 (autoload-remove-section (match-beginning 0))
512 (let ((last-time (nth 4 form)))
513 (dolist (file file)
514 (let ((file-time (nth 5 (file-attributes file))))
515 (when (and file-time
516 (not (time-less-p last-time file-time)))
517 ;; file unchanged
518 (push file no-autoloads)
519 (setq actions
520 (elinstall-remove-autogen-action
521 file actions)))))))
522 ((not (stringp file)))
524 (let
525 ((file-path
526 (locate-library file nil use-load-path)))
527 (cond
528 ;;File doesn't exist, so remove its
529 ;;section.
530 ((not file-path)
531 (autoload-remove-section
532 (match-beginning 0)))
534 ;; File hasn't changed, so do nothing.
535 ((equal
536 (nth 4 form)
537 (nth 5 (file-attributes file-path)))
538 nil)
540 (elinstall-deffile-insert
541 (elinstall-get-autogen-action
542 file actions))))
544 (setq actions
545 (elinstall-remove-autogen-action
546 file actions))))))))
548 ;; Remaining actions have no existing autoload sections yet.
549 (setq no-autoloads
550 (append no-autoloads
551 (delq nil (mapcar #'elinstall-deffile-insert actions))))
552 (when no-autoloads
553 ;; Sort them for better readability.
554 (setq no-autoloads (sort no-autoloads 'string<))
555 ;; Add the `no-autoloads' section.
556 (goto-char (point-max))
557 (search-backward "\f" nil t)
559 (elinstall-insert-section-header
560 (current-buffer)
561 (list 'autoloads nil nil no-autoloads this-time))
562 (insert generate-autoload-section-trailer))
563 (save-buffer))))
565 ;;;_ . elinstall-stage-update-deffiles
566 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
567 "Update any deffiles mentioned in SEGMENT-LIST.
568 FORCE and USE-LOAD-PATH have the same meaning as in
569 `elinstall-update-deffile'.
571 (mapcar
572 #'(lambda (segment)
573 (let*
574 ((deffile (car segment)))
575 (if (stringp deffile)
576 (elinstall-update-deffile deffile (cdr segment) force
577 use-load-path))))
578 segment-list))
580 ;;;_ , Doing actions to arrange preloads
581 ;;;_ . elinstall-symlink-on-emacs-start
582 (defun elinstall-symlink-on-emacs-start
583 (filename target-basename target-dir &optional priority force)
584 "Symlink to TARGET-BASENAME.el in TARGET-DIR
586 If PRIORITY is given, it will be used as the priority prefix,
587 otherwise elinstall-default-priority will be.
588 PRIORITY must be an integer or nil.
589 If FORCE is `t', do it regardless of timestamps etc.
590 Other non-nil cases of FORCE are reserved for future development."
591 (let*
593 (priority (or priority elinstall-default-priority))
594 (target-name-nodir
595 (format
596 "%d%s.el"
597 priority
598 target-basename))
599 (target
600 (expand-file-name target-name-nodir target-dir)))
603 (cond
604 ;;Path should already exist.
605 ((not
606 (file-exists-p target-dir))
607 (message "The target directory doesn't exist."))
608 ;;Target shouldn't already exist, but if force is given, let
609 ;;user override.
610 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
611 ;;do nothing even on force.
612 ((and
613 (file-exists-p target)
615 (not force)
616 (not
617 (yes-or-no-p
618 (format "Really overwrite %s? " target))))
619 (message "File %s already exists" target)))
622 (make-symbolic-link
623 filename
624 target
625 nil)))))
627 ;;;_ . elinstall-add-to-dot-emacs
628 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
629 "Add code to load FILENAME to .emacs.
630 FILENAME should not have an extension"
632 ;;Visit .emacs
633 (with-current-buffer (find-file-noselect dot-emacs-name)
634 (save-excursion
635 ;;add at end of file
636 (goto-char (point-max))
637 (insert "\n;;Added by elinstall")
638 (insert "\n;;Consider using my-site-start to manage .emacs\n")
639 (pp `(load ,filename) (current-buffer))
640 (save-buffer))))
643 ;;;_ . elinstall-arrange-preload
644 ;;;###autoload
645 (defun elinstall-arrange-preload (force filename basename &optional priority)
646 "Arrange for FILENAME to be loaded on emacs start.
647 FORCE has its usual meaning.
648 BASENAME and PRIORITY are used as arguments to
649 `elinstall-symlink-on-emacs-start'.
652 (let
653 ((preload-target elinstall-default-preload-target))
655 ;;Dispatch the possibilities.
656 (cond
657 ((eq preload-target 'dot-emacs)
658 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
659 ((stringp preload-target)
660 (elinstall-symlink-on-emacs-start
661 filename basename preload-target priority force))
662 ((null preload-target)
663 (message "Not arranging for preloads"))
665 (message "I don't recognize that")))))
666 ;;;_ . elinstall-stage-arrange-preloads
667 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
668 "Arrange any preloads mentioned in ACTIONS."
670 (mapcar
671 #'(lambda (act)
672 (case (car act)
673 (preload-file
674 (let*
675 ( (filename
676 (caddr act))
677 (proceed-p
678 (case (second act)
679 ((t) t)
680 ((nil) nil)
681 (if-used
682 (member filename deffiles-used)))))
684 (when proceed-p
685 (apply
686 #'elinstall-arrange-preload
687 force
688 (cddr act)))))
690 (error
691 "elinstall-stage-arrange-preloads: Action not
692 recognized."))) )
693 actions))
696 ;;;_ , Run tests
697 ;;;_ . elinstall-stage-run-tests
698 (defun elinstall-stage-run-tests (actions)
699 "Run any tests mentioned in ACTIONS."
701 (mapcar
702 #'(lambda (act)
703 (case (car act)
704 (run-tests
705 ;;$$WRITE ME - not a high priority right now.
706 nil)
708 (error
709 "elinstall-stage-run-tests: Action not
710 recognized."))) )
711 actions))
714 ;;;_ , Byte compile
715 ;;;_ . elinstall-stage-byte-compile
716 (defun elinstall-stage-byte-compile (actions)
717 "Do any byte-compilation mentioned in ACTIONS."
719 (mapcar
720 #'(lambda (act)
721 (case (car act)
722 ;;$$IMPROVE ME Understand flags to control second
723 ;;argument (whether to load file after
724 ;;compilation)
725 (byte-compile
726 (byte-compile-file (second act)))
728 (error
729 "elinstall-stage-byte-compile: Action not
730 recognized."))) )
731 actions))
732 ;;;_ . Segregating actions
733 ;;;_ , elinstall-remove-empty-segs
734 (defun elinstall-remove-empty-segs (segment-list)
735 "Return SEGMENT-LIST minus any segments that have no actions.
736 Intended only for the deffile stage data."
737 (delq nil
738 (mapcar
739 #'(lambda (segment)
740 (if (cdr segment)
741 segment
742 nil))
743 segment-list)))
745 ;;;_ , elinstall-segregate-actions
746 (defun elinstall-segregate-actions (actions)
747 "Return actions segregated by deffile.
749 Returns a list whose elements are each a cons of:
750 * deffile filename or nil
751 * A list of actions to be done for that deffile."
753 (let
755 (build-deffiles '())
756 (run-tests '())
757 (byte-compile '())
758 (arrange-preloads '()))
760 (dolist (act actions)
761 (when act
762 (case (car act)
763 ((add-file-autoloads
764 add-to-info-path
765 add-to-load-path)
766 (let*
767 ((deffile-name (second act))
768 (cell-already
769 (assoc deffile-name build-deffiles)))
770 (if cell-already
771 ;;There are already actions on this deffile.
772 ;;Splice this action in.
773 (setcdr cell-already
774 (cons act (cdr cell-already)))
775 ;;There are no actions on this deffile. Add a
776 ;;place for them and include this action.
777 (push (list deffile-name act) build-deffiles))))
778 (preload-file
779 (push act arrange-preloads))
780 (run-tests
781 (push act run-tests))
782 (byte-compile
783 (push act byte-compile)))))
785 (elinstall-make-stages
786 :build-deffiles
787 (elinstall-remove-empty-segs build-deffiles)
788 :run-tests
789 run-tests
790 :byte-compile
791 byte-compile
792 :arrange-preloads
793 arrange-preloads)))
794 ;;;_ . Finding actions
795 ;;;_ , Treating the parameter list
796 ;;;_ . elinstall-add-parameter
797 (defun elinstall-add-parameter (alist key new-value)
798 "Add a new value for KEY to ALIST"
800 (cons
801 (cons key new-value)
802 (assq-delete-all key (copy-list alist))))
804 ;;;_ . elinstall-get-parameter
805 (defun elinstall-get-parameter (alist key)
806 "Get the value of KEY from ALIST"
808 (cdr (assq key alist)))
809 ;;;_ . elinstall-expand-file-name
810 ;;$$OBSOLETE
812 (defun elinstall-expand-file-name (filename alist)
813 "Expand FILENAME by the value of `path' in ALIST"
814 (expand-file-name
815 filename
816 (elinstall-get-parameter alist 'path)))
817 ;;;_ , Finding deffiles
818 ;;;_ . elinstall-expand-deffile-name
819 (defun elinstall-expand-deffile-name (deffile)
820 "Expand DEFFILE autoload.el's way."
822 (expand-file-name (or deffile "loaddefs.el")
823 (expand-file-name "lisp"
824 source-directory)))
826 ;;;_ , Informational
827 ;;;_ . elinstall-dir-has-info
829 ;;$$IMPROVE ME - Can this test be made more precise?
830 (defun elinstall-dir-has-info (dir)
831 "Return non-nil if DIR has info files in it.
832 DIR should be an absolute path."
834 (string-match "/info/" dir)
835 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
837 ;;;_ , Workers
838 ;;;_ . List of special variables used here
839 ;;load-path-element - The relevant element of load-path
840 ;;def-file - The file the autoload definitions etc will go into.
841 ;;add-to-load-path-p - Controls whether to add to load-path.
843 ;;;_ . elinstall-actions-for-source-file
844 (defun elinstall-actions-for-source-file (filename dir)
845 "Return a list of actions to do for FILENAME in DIR.
846 Special variables are as noted in \"List of special variables\"."
847 (declare (special
848 load-path-element def-file))
849 ;;$$IMPROVE ME add and use relevant control variables.
850 (let*
851 ((full-path
852 (expand-file-name filename dir))
853 (visited (get-file-buffer full-path))
854 (buf
855 (or
856 visited
857 ;;Visit the file cheaply.
858 ;;hack-local-variables can give errors.
859 (ignore-errors (autoload-find-file full-path))))
860 ;;Figure out whether to run some actions, by file local vars.
861 (autoloads-p
862 (ignore-errors
863 (with-current-buffer buf
864 (not no-update-autoloads))))
865 (def-file
867 (ignore-errors
868 (with-current-buffer buf
869 (if (local-variable-p 'generated-autoload-file)
870 (elinstall-expand-deffile-name
871 generated-autoload-file)
872 nil)))
873 def-file))
874 (compile-p
875 (and
876 (with-current-buffer buf (not no-byte-compile))
877 (featurep 'byte-compile)
878 (string-match emacs-lisp-file-regexp filename)
879 (file-readable-p full-path)
880 (not (auto-save-file-name-p full-path))
881 (let*
882 ((dest (byte-compile-dest-file full-path))
883 ;;$$PUNT - currently, we wouldn't have gotten
884 ;;here if we weren't intending to do everything.
885 (recompile-p nil)
886 (compile-new t))
887 (if (file-exists-p dest)
888 ;; File was already compiled.
889 (or recompile-p (file-newer-than-file-p full-path dest))
890 (or compile-new
891 (y-or-n-p (concat "Compile " filename "? "))))))))
893 (prog1
894 (list
895 (if compile-p
896 `(byte-compile ,full-path)
897 nil)
898 (if autoloads-p
899 `(add-file-autoloads
900 ,def-file
901 ;;load-name relative to a member of load-path
902 ,(file-name-sans-extension
903 (file-relative-name
904 full-path
905 load-path-element))
906 ,load-path-element
907 ,full-path)
908 nil))
909 (unless visited (kill-buffer-if-not-modified buf)))))
911 ;;;_ . elinstall-find-actions-by-spec-x
913 (defun elinstall-find-actions-by-spec-x (spec dir)
914 "Return a list of actions to do, controlled by SPEC."
915 (declare (special
916 load-path-element def-file add-to-load-path-p))
918 (if (consp spec)
919 ;;$$IMPROVE ME by adding the other cases in the design.
920 (case (car spec)
922 (let
923 ((new-dir
924 (expand-file-name
925 (second spec)
926 dir)))
928 (elinstall-find-actions-by-spec-x
929 (third spec)
930 new-dir)))
932 (all
933 (apply #'nconc
934 (mapcar
935 #'(lambda (sub-spec)
936 (elinstall-find-actions-by-spec-x
937 sub-spec
938 dir))
939 (cdr spec))))
941 (file
942 (elinstall-actions-for-source-file
943 filename dir))
944 ;;$$ADD ME control, rather than trying to bind all control
945 ;;variables so we can safely bind one, will use set and
946 ;;unwind-protect.
948 (dir
949 (let*
950 ((dirname
951 (expand-file-name
952 (second spec)
953 dir))
954 ;;Relative filenames
955 (elisp-source-files
956 (directory-files
957 dirname
958 nil
959 elinstall-elisp-regexp))
960 (load-path-here
961 (and
962 elisp-source-files ;;List not empty.
963 add-to-load-path-p))
964 (load-path-element
965 (if load-path-here
966 dirname
967 load-path-element)))
969 (append
970 ;;$$IMPROVE ME - remove the current deffile from
971 ;;this list.
972 ;;Maybe arrange to add this directory to load-path.
973 (if load-path-here
974 `((add-to-load-path
975 ,def-file
976 ,load-path-element))
977 '())
979 ;;$$IMPROVE ME - be controlled by a control variable.
980 ;;If any info files are present, do add-to-info-path
981 ;;too.
983 (elinstall-dir-has-info dirname)
984 `((add-to-info-path
985 ,def-file
986 "."))
987 '())
991 ;;$$FIXME Don't do directories, but maybe recurse on
992 ;;them, if a flag is set.
993 ;;Maybe could follow/not symlinks similarly.
994 (apply #'nconc
995 (mapcar
996 #'(lambda (filename)
997 (elinstall-actions-for-source-file
998 filename
999 dirname))
1000 elisp-source-files)))))
1002 (load-path
1003 (append
1004 `((add-to-load-path ,def-file ,dir))
1005 (let
1006 ((load-path-element dir))
1007 (elinstall-find-actions-by-spec-x spec dir))))
1010 (def-file
1011 (let
1012 ((def-file
1013 (expand-file-name
1014 (second spec)
1015 dir))
1016 (for-preload (third spec)))
1017 (assert (listp for-preload))
1018 (append
1019 (list
1021 (and for-preload (car for-preload))
1022 `(preload-file
1023 ,(car for-preload)
1024 ,def-file
1025 ,@(cdr for-preload))
1026 '()))
1028 (elinstall-find-actions-by-spec-x
1029 (fourth spec) dir)))))
1031 ;;$$IMPROVE ME by adding the other cases in the design.
1032 (case spec
1033 (t))))
1034 ;;;_ . elinstall-find-actions-by-spec
1035 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1038 (let
1039 ((load-path-element load-path-element)
1040 (def-file def-file)
1041 (add-to-load-path-p t))
1042 (declare (special
1043 load-path-element def-file add-to-load-path-p))
1045 (elinstall-find-actions-by-spec-x
1046 spec dir)))
1048 ;;;_ . high-level work
1049 ;;;_ , elinstall-get-relevant-load-path
1050 (defun elinstall-get-relevant-load-path (actions)
1052 (delq nil
1053 (mapcar
1054 #'(lambda (act)
1055 (case (car act)
1056 (add-to-load-path
1057 (second act))
1058 (t nil)))
1059 actions)))
1060 ;;;_ , elinstall-get-deffile-list
1061 (defun elinstall-get-deffile-list (stages)
1062 "Get a list of deffile names"
1064 (mapcar
1065 #'car
1066 (elinstall-stages->build-deffiles stages)))
1068 ;;;_ , elinstall-x
1069 (defun elinstall-x (dir spec &optional force)
1071 (let*
1073 ;;This is just the default deffile, spec can override it.
1074 (def-file
1075 (elinstall-expand-deffile-name nil))
1076 (actions
1077 (elinstall-find-actions-by-spec
1078 spec
1081 def-file))
1082 (stages (elinstall-segregate-actions actions))
1083 (use-load-path
1084 (elinstall-get-relevant-load-path
1085 actions)))
1087 (elinstall-stage-update-deffiles
1088 (elinstall-stages->build-deffiles stages)
1089 force
1090 use-load-path)
1091 (elinstall-stage-arrange-preloads
1092 (elinstall-stages->arrange-preloads stages)
1093 (elinstall-get-deffile-list stages)
1094 force)
1095 (elinstall-stage-byte-compile
1096 (elinstall-stages->byte-compile stages))
1098 ;;;_ , Entry points
1099 ;;;_ . elinstall
1100 ;;;###autoload
1101 (defun elinstall (project-name path spec &optional force)
1102 "Install elisp files.
1103 They need not be a formal package.
1105 Parameters:
1107 PROJECT-NAME - the name of the project
1109 PATH - Path to the project.
1110 Suggestion: (elinstall-directory-true-name)
1112 SPEC - a spec for the autoloads etc to make. It can be as simple as
1113 \(dir \"\.\") for installing one directory.
1115 If FORCE is t, install a package even if it has already been
1116 installed. Other non-nil cases of FORCE are reserved for future
1117 development."
1119 (when
1120 (and
1121 (or
1122 force
1123 (not (elinstall-already-installed project-name)))
1124 (yes-or-no-p (format "Re-install %s? " project-name)))
1125 (elinstall-x
1126 path
1127 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1128 force)
1129 (elinstall-record-installed project-name)))
1133 ;;;_ . elinstall-update-directory-autoloads
1134 ;;$$SPLIT ME - one to just classically update autoloads, one to
1135 ;;install a file.
1136 ;;$$TEST ME
1137 ;;;###autoload
1138 (defun elinstall-update-directory-autoloads (dir)
1141 (interactive "DInstall all elisp files from directory: ")
1144 (let
1145 ((def-file-name
1146 (elinstall-expand-deffile-name
1147 generated-autoload-file)))
1149 (elinstall-x
1151 `(def-file ,def-file-name (nil) (dir ".")))))
1155 ;;;_ . elinstall-update-file-autoloads
1156 ;;$$SPLIT ME - one to just classically update autoloads, one to
1157 ;;install a file.
1158 ;;$$TEST ME
1159 ;;;###autoload
1160 (defun elinstall-update-file-autoloads (file)
1163 (interactive "fInstall elisp file: ")
1164 (let
1165 ((def-file-name
1166 ;;This is the default. File local vars can override it.
1167 (elinstall-expand-deffile-name
1168 generated-autoload-file)))
1169 (elinstall-x
1170 file
1171 `(def-file ,def-file-name (nil) (file ,file)))))
1172 ;;;_. Footers
1173 ;;;_ , Provides
1175 (provide 'elinstall)
1177 ;;;_ * Local emacs vars.
1178 ;;;_ + Local variables:
1179 ;;;_ + mode: allout
1180 ;;;_ + End:
1182 ;;;_ , End
1183 ;;; elinstall.el ends here