Rearranged logic for elinstall-actions-for-source-file
[elinstall.git] / elinstall.el
blobf211fbb06fca5226f341c1b5f5ac3897406ef265
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 (when
850 (and
851 (file-readable-p full-path)
852 (not (auto-save-file-name-p full-path)))
853 ;;$$IMPROVE ME add and use relevant control variables.
854 (let*
855 ((full-path
856 (expand-file-name filename dir))
857 (visited (get-file-buffer full-path))
858 (buf
859 (or
860 visited
861 ;;Visit the file cheaply.
862 ;;hack-local-variables can give errors.
863 (ignore-errors (autoload-find-file full-path))))
864 ;;Figure out whether to run some actions, by file local vars.
865 (autoloads-p
866 (ignore-errors
867 (with-current-buffer buf
868 (not no-update-autoloads))))
869 (def-file
871 (ignore-errors
872 (with-current-buffer buf
873 (if (local-variable-p 'generated-autoload-file)
874 (elinstall-expand-deffile-name
875 generated-autoload-file)
876 nil)))
877 def-file))
878 (compile-p
879 (and
880 (featurep 'byte-compile)
881 (string-match emacs-lisp-file-regexp filename)
882 (ignore-errors
883 (with-current-buffer buf
884 (not no-byte-compile)))
885 (let*
886 ((dest (byte-compile-dest-file full-path))
887 ;;$$PUNT - currently, we wouldn't have gotten
888 ;;here if we weren't intending to do everything.
889 (recompile-p nil)
890 (compile-new t))
891 (if (file-exists-p dest)
892 ;; File was already compiled.
893 (or recompile-p (file-newer-than-file-p full-path dest))
894 (or compile-new
895 (y-or-n-p (concat "Compile " filename "? "))))))))
897 (prog1
898 (list
899 (if compile-p
900 `(byte-compile ,full-path)
901 nil)
902 (if autoloads-p
903 `(add-file-autoloads
904 ,def-file
905 ;;load-name relative to a member of load-path
906 ,(file-name-sans-extension
907 (file-relative-name
908 full-path
909 load-path-element))
910 ,load-path-element
911 ,full-path)
912 nil))
913 (unless visited (kill-buffer-if-not-modified buf))))))
915 ;;;_ . elinstall-find-actions-by-spec-x
917 (defun elinstall-find-actions-by-spec-x (spec dir)
918 "Return a list of actions to do, controlled by SPEC."
919 (declare (special
920 load-path-element def-file add-to-load-path-p))
922 (if (consp spec)
923 ;;$$IMPROVE ME by adding the other cases in the design.
924 (case (car spec)
926 (let
927 ((new-dir
928 (expand-file-name
929 (second spec)
930 dir)))
932 (elinstall-find-actions-by-spec-x
933 (third spec)
934 new-dir)))
936 (all
937 (apply #'nconc
938 (mapcar
939 #'(lambda (sub-spec)
940 (elinstall-find-actions-by-spec-x
941 sub-spec
942 dir))
943 (cdr spec))))
945 (file
946 (elinstall-actions-for-source-file
947 filename dir))
948 ;;$$ADD ME control, rather than trying to bind all control
949 ;;variables so we can safely bind one, will use set and
950 ;;unwind-protect.
952 (dir
953 (let*
954 ((dirname
955 (expand-file-name
956 (second spec)
957 dir))
958 ;;Relative filenames
959 (elisp-source-files
960 (directory-files
961 dirname
962 nil
963 elinstall-elisp-regexp))
964 (load-path-here
965 (and
966 elisp-source-files ;;List not empty.
967 add-to-load-path-p))
968 (load-path-element
969 (if load-path-here
970 dirname
971 load-path-element)))
973 (append
974 ;;$$IMPROVE ME - remove the current deffile from
975 ;;this list.
976 ;;Maybe arrange to add this directory to load-path.
977 (if load-path-here
978 `((add-to-load-path
979 ,def-file
980 ,load-path-element))
981 '())
983 ;;$$IMPROVE ME - be controlled by a control variable.
984 ;;If any info files are present, do add-to-info-path
985 ;;too.
987 (elinstall-dir-has-info dirname)
988 `((add-to-info-path
989 ,def-file
990 "."))
991 '())
995 ;;$$FIXME Don't do directories, but maybe recurse on
996 ;;them, if a flag is set.
997 ;;Maybe could follow/not symlinks similarly.
998 (apply #'nconc
999 (mapcar
1000 #'(lambda (filename)
1001 (elinstall-actions-for-source-file
1002 filename
1003 dirname))
1004 elisp-source-files)))))
1006 (load-path
1007 (append
1008 `((add-to-load-path ,def-file ,dir))
1009 (let
1010 ((load-path-element dir))
1011 (elinstall-find-actions-by-spec-x spec dir))))
1014 (def-file
1015 (let
1016 ((def-file
1017 (expand-file-name
1018 (second spec)
1019 dir))
1020 (for-preload (third spec)))
1021 (assert (listp for-preload))
1022 (append
1023 (list
1025 (and for-preload (car for-preload))
1026 `(preload-file
1027 ,(car for-preload)
1028 ,def-file
1029 ,@(cdr for-preload))
1030 '()))
1032 (elinstall-find-actions-by-spec-x
1033 (fourth spec) dir)))))
1035 ;;$$IMPROVE ME by adding the other cases in the design.
1036 (case spec
1037 (t))))
1038 ;;;_ . elinstall-find-actions-by-spec
1039 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1042 (let
1043 ((load-path-element load-path-element)
1044 (def-file def-file)
1045 (add-to-load-path-p t))
1046 (declare (special
1047 load-path-element def-file add-to-load-path-p))
1049 (elinstall-find-actions-by-spec-x
1050 spec dir)))
1052 ;;;_ . high-level work
1053 ;;;_ , elinstall-get-relevant-load-path
1054 (defun elinstall-get-relevant-load-path (actions)
1056 (delq nil
1057 (mapcar
1058 #'(lambda (act)
1059 (case (car act)
1060 (add-to-load-path
1061 (second act))
1062 (t nil)))
1063 actions)))
1064 ;;;_ , elinstall-get-deffile-list
1065 (defun elinstall-get-deffile-list (stages)
1066 "Get a list of deffile names"
1068 (mapcar
1069 #'car
1070 (elinstall-stages->build-deffiles stages)))
1072 ;;;_ , elinstall-x
1073 (defun elinstall-x (dir spec &optional force)
1075 (let*
1077 ;;This is just the default deffile, spec can override it.
1078 (def-file
1079 (elinstall-expand-deffile-name nil))
1080 (actions
1081 (elinstall-find-actions-by-spec
1082 spec
1085 def-file))
1086 (stages (elinstall-segregate-actions actions))
1087 (use-load-path
1088 (elinstall-get-relevant-load-path
1089 actions)))
1091 (elinstall-stage-update-deffiles
1092 (elinstall-stages->build-deffiles stages)
1093 force
1094 use-load-path)
1095 (elinstall-stage-arrange-preloads
1096 (elinstall-stages->arrange-preloads stages)
1097 (elinstall-get-deffile-list stages)
1098 force)
1099 (elinstall-stage-byte-compile
1100 (elinstall-stages->byte-compile stages))
1102 ;;;_ , Entry points
1103 ;;;_ . elinstall
1104 ;;;###autoload
1105 (defun elinstall (project-name path spec &optional force)
1106 "Install elisp files.
1107 They need not be a formal package.
1109 Parameters:
1111 PROJECT-NAME - the name of the project
1113 PATH - Path to the project.
1114 Suggestion: (elinstall-directory-true-name)
1116 SPEC - a spec for the autoloads etc to make. It can be as simple as
1117 \(dir \"\.\") for installing one directory.
1119 If FORCE is t, install a package even if it has already been
1120 installed. Other non-nil cases of FORCE are reserved for future
1121 development."
1123 (when
1124 (and
1125 (or
1126 force
1127 (not (elinstall-already-installed project-name)))
1128 (yes-or-no-p (format "Re-install %s? " project-name)))
1129 (elinstall-x
1130 path
1131 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1132 force)
1133 (elinstall-record-installed project-name)))
1137 ;;;_ . elinstall-update-directory-autoloads
1138 ;;$$SPLIT ME - one to just classically update autoloads, one to
1139 ;;install a file.
1140 ;;$$TEST ME
1141 ;;;###autoload
1142 (defun elinstall-update-directory-autoloads (dir)
1145 (interactive "DInstall all elisp files from directory: ")
1148 (let
1149 ((def-file-name
1150 (elinstall-expand-deffile-name
1151 generated-autoload-file)))
1153 (elinstall-x
1155 `(def-file ,def-file-name (nil) (dir ".")))))
1159 ;;;_ . elinstall-update-file-autoloads
1160 ;;$$SPLIT ME - one to just classically update autoloads, one to
1161 ;;install a file.
1162 ;;$$TEST ME
1163 ;;;###autoload
1164 (defun elinstall-update-file-autoloads (file)
1167 (interactive "fInstall elisp file: ")
1168 (let
1169 ((def-file-name
1170 ;;This is the default. File local vars can override it.
1171 (elinstall-expand-deffile-name
1172 generated-autoload-file)))
1173 (elinstall-x
1174 file
1175 `(def-file ,def-file-name (nil) (file ,file)))))
1176 ;;;_. Footers
1177 ;;;_ , Provides
1179 (provide 'elinstall)
1181 ;;;_ * Local emacs vars.
1182 ;;;_ + Local variables:
1183 ;;;_ + mode: allout
1184 ;;;_ + End:
1186 ;;;_ , End
1187 ;;; elinstall.el ends here