Changed strategy for parameters to finding actions; now uses special variables.
[elinstall.git] / elinstall.el
blobc6b551547045d3935fb4ea4447f6916d4d8bbb35
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)))
70 ;;;_ . elinstall-already-installed
71 (defcustom elinstall-already-installed
72 '()
73 "(AUTOMATIC) Things that have already been installed.
74 This exists for recording what has been installed. User interaction is not
75 contemplated at this time." )
76 ;;;_ , Types
77 ;;;_ . elinstall-stages
78 (defstruct (elinstall-stages
79 (:constructor elinstall-make-stages)
80 (:conc-name elinstall-stages->)
81 (:copier nil))
82 "The elinstall stages"
83 build-deffiles
84 run-tests
85 byte-compile
86 arrange-preloads)
87 ;;;_ , Data
88 ;;;_ . Regular expressions
89 ;;;_ , elinstall-elisp-regexp
90 (defconst elinstall-elisp-regexp
91 (let ((tmp nil))
92 (dolist
93 (suf (get-load-suffixes))
94 (unless (string-match "\\.elc" suf) (push suf tmp)))
95 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
96 "Regular expression that matches elisp files" )
97 ;;;_ , Utilities
98 ;;;_ . elinstall-directory-true-name
99 (defun elinstall-directory-true-name ()
100 "Get the true name of the directory the calling code lives in.
101 CAUTION: This is sensitive to where it's called. That's the point of it."
102 (file-name-directory
103 (if load-file-name
104 (file-truename load-file-name)
105 (file-truename buffer-file-name))))
106 ;;;_ . Checking installedness
107 ;;;_ , elinstall-already-installed
108 (defun elinstall-already-installed (project-name)
109 "Return non-nil if PROJECT-NAME has been installed."
110 (member project-name elinstall-already-installed))
112 ;;;_ , elinstall-record-installed
113 (defun elinstall-record-installed (project-name)
114 "Record that PROJECT-NAME has been installed."
116 (add-to-list 'elinstall-already-installed project-name)
117 (customize-save-variable
118 'elinstall-already-installed
119 elinstall-already-installed
120 "Set by elinstall-record-installed"))
121 ;;;_ , Work
122 ;;;_ . Doing actions
124 ;;;_ , Doing autoload actions (adapted from autoload.el)
125 ;;;_ . Utilities about the action list
126 ;;;_ , elinstall-remove-autogen-action
127 (defun elinstall-remove-autogen-action (file actions)
128 "Return ACTIONS minus any add-file-autoloads on FILE removed."
130 (delq nil
131 (mapcar
132 #'(lambda (act)
133 (case (car act)
134 (add-file-autoloads
135 (if (equal file (third act))
137 act))
138 (t act)))
139 actions)))
140 ;;;_ , elinstall-get-autogen-action
141 (defun elinstall-get-autogen-action (file actions)
143 (let
144 ((the-act))
145 (dolist (act actions)
146 (case (car act)
147 (add-file-autoloads
148 (when (equal file (third act))
149 (setq the-act act)))))
150 the-act))
151 ;;;_ . elinstall-insert-section-header
152 (defun elinstall-insert-section-header (outbuf form)
153 "Insert the section-header line,
154 which lists the file name and which functions are in it, etc."
155 (insert generate-autoload-section-header)
156 (prin1 form outbuf)
157 (terpri outbuf)
158 ;; Break that line at spaces, to avoid very long lines.
159 ;; Make each sub-line into a comment.
160 (with-current-buffer outbuf
161 (save-excursion
162 (forward-line -1)
163 (while (not (eolp))
164 (move-to-column 64)
165 (skip-chars-forward "^ \n")
166 (or (eolp)
167 (insert "\n" generate-autoload-section-continuation))))))
169 ;;;_ . Making autoloads
170 ;;;_ , elinstall-generate-file-autoloads
171 ;;override to allow slashed load-paths
172 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
173 ;;a new arg.
174 ;;`relative-to' can be:
175 ;; * nil: act as at present. Assume that FILE's immediate directory
176 ;;is in load-path.
177 ;; * t :: use default-directory
178 ;; * a string :: relative to it, as a filename
180 (defun elinstall-generate-file-autoloads (relative-name full-name)
181 "Insert at point a loaddefs autoload section for FILE.
182 Autoloads are generated for defuns and defmacros in FILE
183 marked by `generate-autoload-cookie' (which see).
184 If FILE is being visited in a buffer, the contents of the buffer
185 are used.
186 Return non-nil in the case where no autoloads were added at point.
188 FULL-NAME is the absolute name of the file.
189 RELATIVE-NAME is its name respective to some component of load-path."
190 (let* ((outbuf (current-buffer))
191 (autoloads-done '())
192 (print-length nil)
193 (print-readably t) ; This does something in Lucid Emacs.
194 (float-output-format nil)
195 (done-any nil)
196 (visited (get-file-buffer full-name))
197 (source-buf
198 (or visited
199 ;; It is faster to avoid visiting the file.
200 (ignore-errors (autoload-find-file full-name))))
201 output-start)
202 (if source-buf
203 (with-current-buffer source-buf
204 ;;$$MOVE ME - this should be checked in action-finding.
205 ;; Obey the no-update-autoloads file local variable.
206 (unless no-update-autoloads
207 (message "Generating autoloads for %s..." relative-name)
208 (setq output-start (with-current-buffer outbuf (point)))
209 (save-excursion
210 (save-restriction
211 (widen)
212 (goto-char (point-min))
213 (while (not (eobp))
214 (skip-chars-forward " \t\n\f")
215 (cond
216 ((looking-at (regexp-quote generate-autoload-cookie))
217 (search-forward generate-autoload-cookie)
218 (skip-chars-forward " \t")
219 (setq done-any t)
220 (if (eolp)
221 ;; Read the next form and make an autoload.
222 (let* ((form (prog1 (read (current-buffer))
223 (or (bolp) (forward-line 1))))
224 (autoload
225 (make-autoload form relative-name)))
226 (if autoload
227 (push (nth 1 form) autoloads-done)
228 (setq autoload form))
229 (let ((autoload-print-form-outbuf outbuf))
230 (autoload-print-form autoload)))
232 ;; Copy the rest of the line to the output.
233 (princ (buffer-substring
234 (progn
235 ;; Back up over whitespace, to preserve it.
236 (skip-chars-backward " \f\t")
237 (if (= (char-after (1+ (point))) ? )
238 ;; Eat one space.
239 (forward-char 1))
240 (point))
241 (progn (forward-line 1) (point)))
242 outbuf)))
243 ((looking-at ";")
244 ;; Don't read the comment.
245 (forward-line 1))
247 (forward-sexp 1)
248 (forward-line 1))))))
250 (when done-any
251 (with-current-buffer outbuf
252 (save-excursion
253 ;; Insert the section-header line which lists the file name
254 ;; and which functions are in it, etc.
255 (goto-char output-start)
256 (elinstall-insert-section-header
257 outbuf
258 (list 'autoloads
259 autoloads-done
260 relative-name
261 (autoload-trim-file-name full-name)
262 (nth 5 (file-attributes full-name))))
264 (insert ";;; Generated autoloads from "
265 (autoload-trim-file-name full-name) "\n"))
266 (insert generate-autoload-section-trailer)))
267 (message "Generating autoloads for %s...done" relative-name))
269 (unless visited
270 ;; We created this buffer, so we should kill it.
271 (kill-buffer (current-buffer))))
272 (message "Could not load %s" relative-name))
274 (not done-any)))
277 ;;;_ , elinstall-deffile-insert-autoloads
278 (defun elinstall-deffile-insert-autoloads (file load-name)
279 "Update the autoloads for FILE in current buffer.
280 Return FILE if there was no autoload cookie in it, else nil.
282 Current buffer must be a loaddef-style file.
284 LOAD-NAME is the absolute name of the file.
285 RELATIVE-NAME is its name respective to some component of load-path."
286 (let (
287 (found nil)
288 (no-autoloads nil))
290 (save-excursion
291 (save-restriction
292 (widen)
293 (goto-char (point-min))
294 ;; Look for the section for FILE
295 (while (and (not found)
296 (search-forward generate-autoload-section-header nil t))
297 (let ((form (autoload-read-section-header)))
298 (cond
299 ((equal (nth 2 form) file)
300 ;; We found the section for this file.
301 (let ((begin (match-beginning 0)))
302 (progn
303 (search-forward generate-autoload-section-trailer)
304 (delete-region begin (point))
305 (setq found t))))
306 ((string< file (nth 2 form))
307 ;; We've come to a section alphabetically later than
308 ;; FILE. We assume the file is in order and so
309 ;; there must be no section for FILE. We will
310 ;; insert one before the section here.
311 (goto-char (match-beginning 0))
312 (setq found 'new)))))
313 (unless found
314 (progn
315 (setq found 'new)
316 ;; No later sections in the file. Put before the last page.
317 (goto-char (point-max))
318 (search-backward "\f" nil t)))
319 (setq no-autoloads
320 (elinstall-generate-file-autoloads file load-name))))
322 (if no-autoloads file nil)))
323 ;;;_ . Arranging to add to info-path and load-path
324 ;;;_ , elinstall-generate-add-to-path
325 (defun elinstall-generate-add-to-path (path-element type)
326 "Insert code at point to add PATH-ELEMENT to a path.
327 If TYPE is:
328 * `add-to-load-path', add to load-path
329 * `add-to-info-path', add to Info-default-directory-list
331 Current buffer must be a loaddef-style file."
332 (let ( (path-symbol
333 (case type
334 (add-to-load-path 'load-path)
335 (add-to-info-path 'Info-default-directory-list)
336 (t (error "Type not recognized"))))
337 (description
338 (case type
339 (add-to-load-path "load-path")
340 (add-to-info-path "info-path")))
341 (autoloads-done '())
342 (print-length nil)
343 (print-readably t) ; This does something in Lucid Emacs.
344 (float-output-format nil))
346 (message "Generating %s additions..." description)
349 (elinstall-insert-section-header
350 (current-buffer)
351 (list type (list path-element) nil nil nil))
353 (insert ";;; Generated path addition\n")
355 `(add-to-list ',path-symbol
356 (expand-file-name
357 ,(file-relative-name path-element)
358 (if load-file-name
359 (file-name-directory
360 (file-truename load-file-name)))))
361 (current-buffer))
363 (insert generate-autoload-section-trailer)
364 (message "Generating %s additions...done" description)))
367 ;;;_ , elinstall-deffile-insert-add-to-path
368 (defun elinstall-deffile-insert-add-to-path (path-element type)
369 "Insert code in current buffer to add PATH-ELEMENT to a path.
370 If TYPE is:
371 * `add-to-load-path', add to load-path
372 * `add-to-info-path', add to Info-default-directory-list
374 Current buffer must be a loaddef-style file."
375 (let (
376 (found nil)
377 (no-autoloads nil))
379 (save-excursion
380 (save-restriction
381 (widen)
382 (goto-char (point-min))
383 ;; Look for the section for PATH-ELEMENT
384 (while (and (not found)
385 (search-forward generate-autoload-section-header nil t))
386 (let ((form (autoload-read-section-header)))
387 (cond
388 ((and
389 (equal (nth 0 form) type)
390 (member path-element (nth 1 form)))
392 ;; We found the section for this add.
393 (let ((begin (match-beginning 0)))
394 (progn
395 (search-forward generate-autoload-section-trailer)
396 (delete-region begin (point))
397 (setq found t)))))))
399 (unless found
400 (progn
401 (setq found 'new)
402 ;; No later sections in the file. Put before the last page.
403 (goto-char (point-max))
404 (search-backward "\f" nil t)))
406 (elinstall-generate-add-to-path path-element type)))
408 ;;This never belongs in the no-autoloads section.
409 nil))
410 ;;;_ . elinstall-deffile-insert
412 (defun elinstall-deffile-insert (action)
413 "Insert autoloads etc into current file according to ACTION.
414 The format of ACTION is described in the design docs.
416 Return filename if this action belongs in the no-autoload section."
418 (when action
419 (case (car action)
420 (add-file-autoloads
421 (elinstall-deffile-insert-autoloads
422 (third action)
423 (fifth action)))
425 (add-to-load-path
426 (elinstall-deffile-insert-add-to-path
427 (third action)
428 'add-to-load-path)
429 nil)
431 (add-to-info-path
432 (elinstall-deffile-insert-add-to-path
433 (third action)
434 'add-to-info-path)
435 nil)
437 ((preload-file run-tests byte-compile)
438 (error "This case should not come here.")))))
440 ;;;_ . elinstall-prepare-deffile
441 (defun elinstall-prepare-deffile (deffile)
442 "Try to ensure that DEFFILE is available for receiving autoloads"
444 (autoload-ensure-default-file deffile)
445 (with-current-buffer (find-file-noselect deffile)
448 ;; We must read/write the file without any code conversion,
449 ;; but still decode EOLs.
450 (let ((coding-system-for-read 'raw-text))
452 ;; This is to make generated-autoload-file have Unix EOLs, so
453 ;; that it is portable to all platforms.
454 (setq buffer-file-coding-system 'raw-text-unix))
455 (or (> (buffer-size) 0)
456 (error "Autoloads file %s does not exist" buffer-file-name))
457 (or (file-writable-p buffer-file-name)
458 (error "Autoloads file %s is not writable"
459 buffer-file-name))))
461 ;;;_ . elinstall-update-deffile
463 ;;Adapted from autoload.el `update-directory-autoloads'.
465 (defun elinstall-update-deffile (target actions &optional
466 use-load-path force)
468 Update file TARGET with current autoloads as specified by ACTIONS.
469 Also remove any old definitions pointing to libraries that can no
470 longer be found.
472 ACTIONS must be a list of actions (See the format doc). Each one's
473 filename must be relative to some element of load-path.
475 USE-LOAD-PATH is a list to use as load-path. It should include
476 any new load-path that we are arranging to create. If it's not given,
477 load-path itself is used.
479 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
480 Other non-nil cases of FORCE are reserved for future development.
482 This uses `update-file-autoloads' (which see) to do its work.
483 In an interactive call, you must give one argument, the name
484 of a single directory."
485 (let
487 (use-load-path (or use-load-path load-path))
488 (this-time (current-time))
489 ;;files with no autoload cookies.
490 (no-autoloads nil))
492 (elinstall-prepare-deffile target)
493 (with-current-buffer
494 (find-file-noselect target)
495 (save-excursion
496 (setq actions
497 (elinstall-remove-autogen-action
498 (autoload-trim-file-name target)
499 actions))
501 (goto-char (point-min))
502 (while (search-forward generate-autoload-section-header nil t)
503 (let* ((form (autoload-read-section-header))
504 (file (nth 3 form)))
505 (cond ((and (consp file) (stringp (car file)))
506 ;; This is a list of files that have no
507 ;; autoload cookies.
508 ;; There shouldn't be more than one such entry.
509 ;; Remove the obsolete section.
510 (autoload-remove-section (match-beginning 0))
511 (let ((last-time (nth 4 form)))
512 (dolist (file file)
513 (let ((file-time (nth 5 (file-attributes file))))
514 (when (and file-time
515 (not (time-less-p last-time file-time)))
516 ;; file unchanged
517 (push file no-autoloads)
518 (setq actions
519 (elinstall-remove-autogen-action
520 file actions)))))))
521 ((not (stringp file)))
523 (let
524 ((file-path
525 (locate-library file nil use-load-path)))
526 (cond
527 ;;File doesn't exist, so remove its
528 ;;section.
529 ((not file-path)
530 (autoload-remove-section
531 (match-beginning 0)))
533 ;; File hasn't changed, so do nothing.
534 ((equal
535 (nth 4 form)
536 (nth 5 (file-attributes file-path)))
537 nil)
539 (elinstall-deffile-insert
540 (elinstall-get-autogen-action
541 file actions))))
543 (setq actions
544 (elinstall-remove-autogen-action
545 file actions))))))))
547 ;; Remaining actions have no existing autoload sections yet.
548 (setq no-autoloads
549 (append no-autoloads
550 (delq nil (mapcar #'elinstall-deffile-insert actions))))
551 (when no-autoloads
552 ;; Sort them for better readability.
553 (setq no-autoloads (sort no-autoloads 'string<))
554 ;; Add the `no-autoloads' section.
555 (goto-char (point-max))
556 (search-backward "\f" nil t)
558 (elinstall-insert-section-header
559 (current-buffer)
560 (list 'autoloads nil nil no-autoloads this-time))
561 (insert generate-autoload-section-trailer))
562 (save-buffer))))
564 ;;;_ . elinstall-stage-update-deffiles
565 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
566 "Update any deffiles mentioned in SEGMENT-LIST.
567 FORCE and USE-LOAD-PATH have the same meaning as in
568 `elinstall-update-deffile'.
570 (mapcar
571 #'(lambda (segment)
572 (let*
573 ((deffile (car segment)))
574 (if (stringp deffile)
575 (elinstall-update-deffile deffile (cdr segment) force
576 use-load-path))))
577 segment-list))
579 ;;;_ , Doing actions to arrange preloads
580 ;;;_ . elinstall-symlink-on-emacs-start
581 (defun elinstall-symlink-on-emacs-start
582 (filename target-basename target-dir &optional priority force)
583 "Symlink to TARGET-BASENAME.el in TARGET-DIR
585 If PRIORITY is given, it will be used as the priority prefix,
586 otherwise elinstall-default-priority will be.
587 PRIORITY must be an integer or nil.
588 If FORCE is `t', do it regardless of timestamps etc.
589 Other non-nil cases of FORCE are reserved for future development."
590 (let*
592 (priority (or priority elinstall-default-priority))
593 (target-name-nodir
594 (format
595 "%d%s.el"
596 priority
597 target-basename))
598 (target
599 (expand-file-name target-name-nodir target-dir)))
602 (cond
603 ;;Path should already exist.
604 ((not
605 (file-exists-p target-dir))
606 (message "The target directory doesn't exist."))
607 ;;Target shouldn't already exist, but if force is given, let
608 ;;user override.
609 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
610 ;;do nothing even on force.
611 ((and
612 (file-exists-p target)
614 (not force)
615 (not
616 (yes-or-no-p
617 (format "Really overwrite %s? " target))))
618 (message "File %s already exists" target)))
621 (make-symbolic-link
622 filename
623 target
624 nil)))))
626 ;;;_ . elinstall-add-to-dot-emacs
627 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
628 "Add code to load FILENAME to .emacs.
629 FILENAME should not have an extension"
631 ;;Visit .emacs
632 (with-current-buffer (find-file-noselect dot-emacs-name)
633 (save-excursion
634 ;;add at end of file
635 (goto-char (point-max))
636 (insert "\n;;Added by elinstall")
637 (insert "\n;;Consider using my-site-start to manage .emacs\n")
638 (pp `(load ,filename) (current-buffer))
639 (save-buffer))))
642 ;;;_ . elinstall-arrange-preload
643 ;;;###autoload
644 (defun elinstall-arrange-preload (force filename basename &optional priority)
645 "Arrange for FILENAME to be loaded on emacs start.
646 FORCE has its usual meaning.
647 BASENAME and PRIORITY are used as arguments to
648 `elinstall-symlink-on-emacs-start'.
651 (let
652 ((preload-target elinstall-default-preload-target))
654 ;;Dispatch the possibilities.
655 (cond
656 ((eq preload-target 'dot-emacs)
657 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
658 ((stringp preload-target)
659 (elinstall-symlink-on-emacs-start
660 filename basename preload-target priority force))
661 ((null preload-target)
662 (message "Not arranging for preloads"))
664 (message "I don't recognize that")))))
665 ;;;_ . elinstall-stage-arrange-preloads
666 (defun elinstall-stage-arrange-preloads (actions deffiles-used)
667 "Arrange any preloads mentioned in ACTIONS."
669 (mapcar
670 #'(lambda (act)
671 (case (car act)
672 (preload-file
673 (let*
674 ( (filename
675 (caddr act))
676 (proceed-p
677 (case (second act)
678 ((t) t)
679 ((nil) nil)
680 (if-used
681 (member filename deffiles-used)))))
683 (when proceed-p
684 (apply
685 #'elinstall-arrange-preload
686 force
687 (cddr act)))))
689 (error
690 "elinstall-stage-arrange-preloads: Action not
691 recognized."))) )
692 actions))
695 ;;;_ , Run tests
696 ;;;_ . elinstall-stage-run-tests
697 (defun elinstall-stage-run-tests (actions)
698 "Run any tests mentioned in ACTIONS."
700 (mapcar
701 #'(lambda (act)
702 (case (car act)
703 (run-tests
704 ;;$$WRITE ME - not a high priority right now.
705 nil)
707 (error
708 "elinstall-stage-run-tests: Action not
709 recognized."))) )
710 actions))
713 ;;;_ , Byte compile
714 ;;;_ . elinstall-stage-byte-compile
715 (defun elinstall-stage-byte-compile (actions)
716 "Do any byte-compilation mentioned in ACTIONS."
718 (mapcar
719 #'(lambda (act)
720 (case (car act)
721 ;;$$IMPROVE ME Understand flags to control second
722 ;;argument (whether to load file after
723 ;;compilation)
724 (byte-compile
725 (byte-compile-file (second act)))
727 (error
728 "elinstall-stage-byte-compile: Action not
729 recognized."))) )
730 actions))
731 ;;;_ . Segregating actions
732 ;;;_ , elinstall-remove-empty-segs
733 (defun elinstall-remove-empty-segs (segment-list)
734 "Return SEGMENT-LIST minus any segments that have no actions.
735 Intended only for the deffile stage data."
736 (delq nil
737 (mapcar
738 #'(lambda (segment)
739 (if (cdr segment)
740 segment
741 nil))
742 segment-list)))
744 ;;;_ , elinstall-segregate-actions
745 (defun elinstall-segregate-actions (actions)
746 "Return actions segregated by deffile.
748 Returns a list whose elements are each a cons of:
749 * deffile filename or nil
750 * A list of actions to be done for that deffile."
752 (let
754 (build-deffiles '())
755 (run-tests '())
756 (byte-compile '())
757 (arrange-preloads '()))
759 (dolist (act actions)
760 (when act
761 (case (car act)
762 ((add-file-autoloads
763 add-to-info-path
764 add-to-load-path)
765 (let*
766 ((deffile-name (second act))
767 (cell-already
768 (assoc deffile-name build-deffiles)))
769 (if cell-already
770 ;;There are already actions on this deffile.
771 ;;Splice this action in.
772 (setcdr cell-already
773 (cons act (cdr cell-already)))
774 ;;There are no actions on this deffile. Add a
775 ;;place for them and include this action.
776 (push (list deffile-name act) build-deffiles))))
777 (preload-file
778 (push act arrange-preloads))
779 (run-tests
780 (push act run-tests))
781 (byte-compile
782 (push act byte-compile)))))
784 (elinstall-make-stages
785 :build-deffiles
786 (elinstall-remove-empty-segs build-deffiles)
787 :run-tests
788 run-tests
789 :byte-compile
790 byte-compile
791 :arrange-preloads
792 arrange-preloads)))
793 ;;;_ . Finding actions
794 ;;;_ , Treating the parameter list
795 ;;;_ . elinstall-add-parameter
796 (defun elinstall-add-parameter (alist key new-value)
797 "Add a new value for KEY to ALIST"
799 (cons
800 (cons key new-value)
801 (assq-delete-all key (copy-list alist))))
803 ;;;_ . elinstall-get-parameter
804 (defun elinstall-get-parameter (alist key)
805 "Get the value of KEY from ALIST"
807 (cdr (assq key alist)))
808 ;;;_ . elinstall-expand-file-name
809 ;;$$OBSOLETE
811 (defun elinstall-expand-file-name (filename alist)
812 "Expand FILENAME by the value of `path' in ALIST"
813 (expand-file-name
814 filename
815 (elinstall-get-parameter alist 'path)))
816 ;;;_ , Finding deffiles
817 ;;;_ . elinstall-expand-deffile-name
818 (defun elinstall-expand-deffile-name (deffile)
819 "Expand DEFFILE autoload.el's way."
821 (expand-file-name (or deffile "loaddefs.el")
822 (expand-file-name "lisp"
823 source-directory)))
825 ;;;_ . elinstall-maybe-get-deffile
826 (defun elinstall-maybe-get-deffile (file)
827 "If FILE defined `generated-autoload-file', return it.
828 Otherwise return nil.
829 Return it as an absolute filename."
831 (save-excursion
832 ;;$$FIXME load buffer if it's not already loaded
833 (let*
834 ((existing-buffer (get-file-buffer file)))
836 ;; We want to get a value for generated-autoload-file from
837 ;; the local variables section if it's there.
838 ;;But if it's not loaded, we don't? Maybe should use
839 ;; `autoload-find-file' and load it.
840 (if existing-buffer
841 (set-buffer existing-buffer))
842 (if (local-variable-p 'generated-autoload-file)
843 (elinstall-expand-deffile-name
844 generated-autoload-file)
845 nil))))
846 ;;;_ , Informational
847 ;;;_ . elinstall-dir-has-info
849 ;;$$IMPROVE ME - Can this test be made more precise?
850 (defun elinstall-dir-has-info (dir)
851 "Return non-nil if DIR has info files in it.
852 DIR should be an absolute path."
854 (string-match "/info/" dir)
855 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
857 ;;;_ . Workers
858 ;;;_ , List of special variables used here
859 ;;load-path-element - The relevant element of load-path
860 ;;def-file - The file the autoload definitions etc will go into.
861 ;;add-to-load-path-p - Controls whether to add to load-path.
863 ;;;_ , elinstall-actions-for-source-file
864 (defun elinstall-actions-for-source-file (filename dir)
865 "Return a list of actions to do for FILENAME in DIR.
866 Special variables are as noted in \"List of special variables\"."
867 (declare (special
868 load-path-element))
870 ;;$$IMPROVE ME take and treat a "force" argument.
871 (let*
872 ((full-path
873 (expand-file-name filename dir))
874 ;;$$IMPROVE ME - take into account local no-byte-compile flag.
875 (compile-p
876 (and
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 "? "))))))))
894 (list
895 (if compile-p
896 `(byte-compile ,full-path)
897 '())
898 `(add-file-autoloads
899 ,def-file
900 ;;load-name relative to a member of load-path
901 ,(file-name-sans-extension
902 (file-relative-name
903 full-path
904 load-path-element))
905 ,load-path-element
906 ,full-path))))
907 ;;;_ , elinstall-find-actions-by-spec-x
909 (defun elinstall-find-actions-by-spec-x (spec path)
910 "Return a list of actions to do, controlled by SPEC."
911 (declare (special
912 load-path-element def-file add-to-load-path-p))
914 (if (consp spec)
915 ;;$$IMPROVE ME by adding the other cases in the design.
916 (case (car spec)
918 (let
919 ((new-path
920 (expand-file-name
921 (second spec)
922 dir)))
924 (elinstall-find-actions-by-spec-x
925 (third spec)
926 new-path)))
928 (all
929 (apply #'nconc
930 (mapcar
931 #'(lambda (sub-spec)
932 (elinstall-find-actions-by-spec-x
933 sub-spec
934 dir))
935 (cdr spec))))
937 (file
938 (elinstall-actions-for-source-file
939 filename dir))
940 ;;$$ADD ME control, rather than trying to bind all control
941 ;;variables so we can safely bind one, will use set and
942 ;;unwind-protect.
944 (dir
945 (let*
946 ((dirname
947 (expand-file-name
948 (second spec)
949 dir))
950 ;;Relative filenames
951 (elisp-source-files
952 (directory-files
953 dirname
954 nil
955 elinstall-elisp-regexp))
956 (load-path-here
957 (and
958 elisp-source-files ;;List not empty.
959 add-to-load-path-p))
960 (load-path-element
961 (if load-path-here
962 dirname
963 load-path-element)))
965 (append
966 ;;$$IMPROVE ME - remove the current deffile from
967 ;;this list.
968 ;;Maybe arrange to add this directory to load-path.
969 (if load-path-here
970 `((add-to-load-path
971 ,def-file
972 ,load-path-element))
973 '())
975 ;;$$IMPROVE ME - be controlled by a control variable.
976 ;;If any info files are present, do add-to-info-path
977 ;;too.
979 (elinstall-dir-has-info dirname)
980 `((add-to-info-path
981 ,def-file
982 "."))
983 '())
986 ;;$$IMPROVE ME
987 ;; We want to get a value for generated-autoload-file
988 ;; from the local variables section if it's there.
989 ;;Use `elinstall-maybe-get-deffile'
992 ;;$$FIXME Don't do directories, but maybe recurse on
993 ;;them, if a flag is set.
994 ;;Maybe could follow/not symlinks similarly.
995 (apply #'nconc
996 (mapcar
997 #'(lambda (filename)
998 (elinstall-actions-for-source-file
999 filename
1000 dirname))
1001 elisp-source-files)))))
1003 (load-path
1004 (append
1005 `((add-to-load-path ,def-file ,path))
1006 (let
1007 ((load-path-element path))
1008 (elinstall-find-actions-by-spec-x path dir))))
1011 (def-file
1012 (let
1013 ((def-file
1014 (expand-file-name
1015 (second spec)
1016 dir))
1017 (for-preload (third spec)))
1018 (assert (listp for-preload))
1019 (append
1020 (list
1022 (and for-preload (car for-preload))
1023 `(preload-file
1024 ,(car for-preload)
1025 ,def-file
1026 ,@(cdr for-preload))
1027 '()))
1029 (elinstall-find-actions-by-spec-x
1030 (fourth spec) dir)))))
1032 ;;$$IMPROVE ME by adding the other cases in the design.
1033 (case spec
1034 (t))))
1035 ;;;_ , elinstall-find-actions-by-spec
1036 (defun elinstall-find-actions-by-spec (spec load-path-element path def-file)
1039 (let
1040 ((load-path-element load-path-element)
1041 (def-file def-file)
1042 (add-to-load-path-p t))
1043 (declare (special
1044 load-path-element def-file add-to-load-path-p))
1046 (elinstall-find-actions-by-spec-x
1047 spec path)))
1049 ;;;_ . high-level work
1050 ;;;_ , elinstall-get-relevant-load-path
1051 (defun elinstall-get-relevant-load-path (actions)
1053 (delq nil
1054 (mapcar
1055 #'(lambda (act)
1056 (case (car act)
1057 (add-to-load-path
1058 (second act))
1059 (t nil)))
1060 actions)))
1061 ;;;_ , elinstall-get-deffile-list
1062 (defun elinstall-get-deffile-list (stages)
1063 "Get a list of deffile names"
1065 (mapcar
1066 #'car
1067 (elinstall-stages->build-deffiles stages)))
1069 ;;;_ , elinstall-x
1070 (defun elinstall-x (dir spec &optional force)
1072 (let*
1074 ;;This is just the default deffile, spec can override it.
1075 (def-file
1076 (elinstall-expand-deffile-name nil))
1077 (actions
1078 (elinstall-find-actions-by-spec
1079 spec
1082 def-file))
1083 (stages (elinstall-segregate-actions actions))
1084 (use-load-path
1085 (elinstall-get-relevant-load-path
1086 actions)))
1088 (elinstall-stage-update-deffiles
1089 (elinstall-stages->build-deffiles stages)
1090 force
1091 use-load-path)
1092 (elinstall-stage-arrange-preloads
1093 (elinstall-stages->arrange-preloads stages)
1094 (elinstall-get-deffile-list stages))
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
1167 (elinstall-maybe-get-deffile file)
1168 (elinstall-expand-deffile-name
1169 generated-autoload-file))))
1170 (elinstall-x
1171 file
1172 `(def-file ,def-file-name (nil) (file ,file)))))
1173 ;;;_. Footers
1174 ;;;_ , Provides
1176 (provide 'elinstall)
1178 ;;;_ * Local emacs vars.
1179 ;;;_ + Local variables:
1180 ;;;_ + mode: allout
1181 ;;;_ + End:
1183 ;;;_ , End
1184 ;;; elinstall.el ends here