New spec head "matching". Changed comments. Bugfixes.
[elinstall.git] / elinstall.el
blob4798c699483abc227563ec08856529463f3c84a3
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 ;;;_ . Finding deffiles
123 ;;;_ , elinstall-expand-deffile-name
124 (defun elinstall-expand-deffile-name (deffile)
125 "Expand DEFFILE autoload.el's way."
127 (expand-file-name (or deffile "loaddefs.el")
128 (expand-file-name "lisp"
129 source-directory)))
131 ;;;_ , Work
132 ;;;_ . Doing actions
134 ;;;_ , Doing autoload actions (adapted from autoload.el)
135 ;;;_ . Utilities about the action list
136 ;;;_ , elinstall-remove-autogen-action
137 (defun elinstall-remove-autogen-action (file actions)
138 "Return ACTIONS minus any add-file-autoloads on FILE removed."
140 (delq nil
141 (mapcar
142 #'(lambda (act)
143 (case (car act)
144 (add-file-autoloads
145 (if (equal file (third act))
147 act))
148 (t act)))
149 actions)))
150 ;;;_ , elinstall-get-autogen-action
151 (defun elinstall-get-autogen-action (file actions)
153 (let
154 ((the-act))
155 (dolist (act actions)
156 (case (car act)
157 (add-file-autoloads
158 (when (equal file (third act))
159 (setq the-act act)))))
160 the-act))
161 ;;;_ . About printing to autoload file
162 ;;;_ , elinstall-insert-section-header
163 (defun elinstall-insert-section-header (outbuf form)
164 "Insert the section-header line,
165 which lists the file name and which functions are in it, etc."
166 (insert generate-autoload-section-header)
167 (prin1 form outbuf)
168 (terpri outbuf)
169 ;; Break that line at spaces, to avoid very long lines.
170 ;; Make each sub-line into a comment.
171 (with-current-buffer outbuf
172 (save-excursion
173 (forward-line -1)
174 (while (not (eolp))
175 (move-to-column 64)
176 (skip-chars-forward "^ \n")
177 (or (eolp)
178 (insert "\n" generate-autoload-section-continuation))))))
180 ;;;_ , elinstall-insert-autoload-section
181 (defun elinstall-insert-autoload-section (text form &optional comment-string)
182 "Insert TEXT into current buffer as an autoload section"
184 (let* ( ;; This does something in Lucid Emacs.
185 (print-length nil)
186 (print-readably t)
187 (float-output-format nil))
189 (elinstall-insert-section-header (current-buffer) form)
190 (when comment-string
191 (insert ";;; " comment-string "\n"))
192 (insert text)
193 (insert generate-autoload-section-trailer)))
195 ;;;_ . Making autoloads
196 ;;;_ , elinstall-make-autoload-action
197 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
198 "Return the autoloads for current buffer as a string"
200 ;;We put all the text into a temp buffer, then get that buffer's
201 ;;string.
202 (let
203 ((outbuf
204 (generate-new-buffer " *temp*"))
205 (autoloads-done '())
206 (short-name
207 (file-name-nondirectory full-path))
209 ;; Apparently this does something in Lucid Emacs.
210 (print-length nil)
211 (print-readably t)
212 (float-output-format nil)
214 ;;load-name relative to a member of load-path.
215 (relative-name
216 (file-name-sans-extension
217 (file-relative-name
218 full-path
219 load-path-element))))
221 (with-current-buffer buf
222 (unwind-protect
223 (save-excursion
224 (save-restriction
225 (widen)
226 (goto-char (point-min))
227 (message "Finding autoloads for %s..." short-name)
228 (while (not (eobp))
229 (skip-chars-forward " \t\n\f")
230 (cond
231 ((looking-at (regexp-quote generate-autoload-cookie))
232 (search-forward generate-autoload-cookie)
233 (skip-chars-forward " \t")
234 (setq done-any t)
235 (if (eolp)
236 ;; Read the next form and make an autoload.
237 (let* ((form (prog1 (read (current-buffer))
238 (or (bolp) (forward-line 1))))
239 (autoload
240 (make-autoload form relative-name)))
241 (if autoload
242 (push (nth 1 form) autoloads-done)
243 (setq autoload form))
244 (let ((autoload-print-form-outbuf outbuf))
245 (autoload-print-form autoload)))
247 ;; Copy the rest of the line to the output.
248 (princ (buffer-substring
249 (progn
250 ;; Back up over whitespace,
251 ;; to preserve it.
252 (skip-chars-backward " \f\t")
253 (if (= (char-after (1+ (point))) ? )
254 ;; Eat one space.
255 (forward-char 1))
256 (point))
257 (progn (forward-line 1) (point)))
258 outbuf)))
259 ((looking-at ";")
260 ;; Don't read the comment.
261 (forward-line 1))
263 (forward-sexp 1)
264 (forward-line 1))))
265 (message "Finding autoloads for %s...done" short-name))
267 ;;Return this action. The temp buffer's contents is
268 ;;our final string.
269 `(add-file-autoloads
270 ,def-file
271 ,relative-name
272 ,full-path
273 ,(with-current-buffer outbuf (buffer-string))
274 ,autoloads-done))
276 ;;This in unwind-protected
277 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
280 ;;;_ , elinstall-generate-file-autoloads
282 (defun elinstall-generate-file-autoloads
283 (relative-name full-name text autoloads-done)
284 "Insert at point a loaddefs autoload section for FILE.
285 Autoloads are generated for defuns and defmacros in FILE
286 marked by `generate-autoload-cookie' (which see).
287 If FILE is being visited in a buffer, the contents of the buffer
288 are used.
289 Return non-nil in the case where no autoloads were added at point.
291 FULL-NAME is the absolute name of the file.
292 RELATIVE-NAME is its name respective to some component of load-path."
293 (if (not (equal text ""))
294 ;; Insert the section-header line which lists the file name and
295 ;; which functions are in it, etc.
296 (elinstall-insert-autoload-section
297 text
298 (list 'autoloads
299 autoloads-done
300 relative-name
301 (autoload-trim-file-name full-name)
302 (nth 5 (file-attributes full-name)))
303 (concat
304 "Generated autoloads from "
305 (autoload-trim-file-name full-name)))
308 ;;;_ , elinstall-deffile-insert-autoloads
309 (defun elinstall-deffile-insert-autoloads (file args)
310 "Update the autoloads for FILE in current buffer.
311 Return FILE if there was no autoload cookie in it, else nil.
313 Current buffer must be a loaddef-style file.
315 LOAD-NAME is the absolute name of the file.
316 RELATIVE-NAME is its name respective to some component of load-path."
317 (let (
318 (found nil)
319 (no-autoloads nil))
321 (save-excursion
322 (save-restriction
323 (widen)
324 (goto-char (point-min))
325 ;; Look for the section for FILE
326 (while (and (not found)
327 (search-forward generate-autoload-section-header nil t))
328 (let ((form (autoload-read-section-header)))
329 (cond
330 ((equal (nth 2 form) file)
331 ;; We found the section for this file.
332 (let ((begin (match-beginning 0)))
333 (progn
334 (search-forward generate-autoload-section-trailer)
335 (delete-region begin (point))
336 (setq found t))))
337 ((string< file (nth 2 form))
338 ;; We've come to a section alphabetically later than
339 ;; FILE. We assume the file is in order and so
340 ;; there must be no section for FILE. We will
341 ;; insert one before the section here.
342 (goto-char (match-beginning 0))
343 (setq found 'new)))))
344 (unless found
345 (progn
346 (setq found 'new)
347 ;; No later sections in the file. Put before the last page.
348 (goto-char (point-max))
349 (search-backward "\f" nil t)))
350 (setq no-autoloads
351 (apply #'elinstall-generate-file-autoloads
352 file args))))
354 (if no-autoloads file nil)))
355 ;;;_ . Arranging to add to info-path and load-path
356 ;;;_ , elinstall-generate-add-to-path
357 (defun elinstall-generate-add-to-path (path-element type)
358 "Insert code at point to add PATH-ELEMENT to a path.
359 If TYPE is:
360 * `add-to-load-path', add to load-path
361 * `add-to-info-path', add to Info-additional-directory-list
363 Current buffer must be a loaddef-style file."
364 (let ( (path-symbol
365 (case type
366 (add-to-load-path 'load-path)
367 (add-to-info-path 'Info-additional-directory-list)
368 (t (error "Type not recognized"))))
369 (description
370 (case type
371 (add-to-load-path "load-path")
372 (add-to-info-path "info-path")))
373 (autoloads-done '())
374 (print-length nil)
375 (print-readably t) ; This does something in Lucid Emacs.
376 (float-output-format nil))
378 (message "Generating %s additions..." description)
380 (elinstall-insert-autoload-section
381 (pp-to-string
382 `(add-to-list ',path-symbol
383 (expand-file-name
384 ,(file-relative-name path-element)
385 (if load-file-name
386 (file-name-directory
387 (file-truename load-file-name))))))
388 (list type (list path-element) nil nil nil)
389 nil)
390 (message "Generating %s additions...done" description)))
393 ;;;_ , elinstall-deffile-insert-add-to-path
394 (defun elinstall-deffile-insert-add-to-path (path-element type)
395 "Insert code in current buffer to add PATH-ELEMENT to a path.
396 If TYPE is:
397 * `add-to-load-path', add to load-path
398 * `add-to-info-path', add to Info-default-directory-list
400 Current buffer must be a loaddef-style file."
401 (let (
402 (found nil)
403 (no-autoloads nil))
405 (save-excursion
406 (save-restriction
407 (widen)
408 (goto-char (point-min))
409 ;; Look for the section for PATH-ELEMENT
410 (while (and (not found)
411 (search-forward generate-autoload-section-header nil t))
412 (let ((form (autoload-read-section-header)))
413 (cond
414 ((and
415 (equal (nth 0 form) type)
416 (member path-element (nth 1 form)))
418 ;; We found the section for this add.
419 (let ((begin (match-beginning 0)))
420 (progn
421 (search-forward generate-autoload-section-trailer)
422 (delete-region begin (point))
423 (setq found t)))))))
425 (unless found
426 (progn
427 (setq found 'new)
428 ;; No later sections in the file. Put before the last page.
429 (goto-char (point-max))
430 (search-backward "\f" nil t)))
432 (elinstall-generate-add-to-path path-element type)))
434 ;;This never belongs in the no-autoloads section.
435 nil))
436 ;;;_ . elinstall-deffile-insert
438 (defun elinstall-deffile-insert (action)
439 "Insert autoloads etc into current file according to ACTION.
440 The format of ACTION is described in the design docs.
442 Return filename if this action belongs in the no-autoload section."
444 (when action
445 (case (car action)
446 (add-file-autoloads
447 (elinstall-deffile-insert-autoloads
448 (third action)
449 (nthcdr 3 action)))
451 (add-to-load-path
452 (elinstall-deffile-insert-add-to-path
453 (third action)
454 'add-to-load-path)
455 nil)
457 (add-to-info-path
458 (elinstall-deffile-insert-add-to-path
459 (third action)
460 'add-to-info-path)
461 nil)
463 ((preload-file run-tests byte-compile)
464 (error "This case should not come here.")))))
466 ;;;_ . elinstall-prepare-deffile
467 (defun elinstall-prepare-deffile (deffile)
468 "Try to ensure that DEFFILE is available for receiving autoloads"
470 (autoload-ensure-default-file deffile)
471 (with-current-buffer (find-file-noselect deffile)
474 ;; We must read/write the file without any code conversion,
475 ;; but still decode EOLs.
476 (let ((coding-system-for-read 'raw-text))
478 ;; This is to make generated-autoload-file have Unix EOLs, so
479 ;; that it is portable to all platforms.
480 (setq buffer-file-coding-system 'raw-text-unix))
481 (or (> (buffer-size) 0)
482 (error "Autoloads file %s does not exist" buffer-file-name))
483 (or (file-writable-p buffer-file-name)
484 (error "Autoloads file %s is not writable"
485 buffer-file-name))))
487 ;;;_ . elinstall-update-deffile
489 ;;Adapted from autoload.el `update-directory-autoloads'.
491 (defun elinstall-update-deffile (target actions &optional
492 use-load-path force)
494 Update file TARGET with current autoloads as specified by ACTIONS.
495 Also remove any old definitions pointing to libraries that can no
496 longer be found.
498 ACTIONS must be a list of actions (See the format doc). Each one's
499 filename must be relative to some element of load-path.
501 USE-LOAD-PATH is a list to use as load-path. It should include
502 any new load-path that we are arranging to create. If it's not given,
503 load-path itself is used.
505 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
506 Other non-nil cases of FORCE are reserved for future development.
508 This uses `update-file-autoloads' (which see) to do its work.
509 In an interactive call, you must give one argument, the name
510 of a single directory."
511 (let
513 (use-load-path (or use-load-path load-path))
514 (this-time (current-time))
515 ;;files with no autoload cookies.
516 (no-autoloads nil))
518 (elinstall-prepare-deffile target)
519 (with-current-buffer
520 (find-file-noselect target)
521 (save-excursion
522 (setq actions
523 (elinstall-remove-autogen-action
524 (autoload-trim-file-name target)
525 actions))
527 (goto-char (point-min))
528 (while (search-forward generate-autoload-section-header nil t)
529 (let* ((form (autoload-read-section-header))
530 (file (nth 3 form)))
531 (cond ((and (consp file) (stringp (car file)))
532 ;; This is a list of files that have no
533 ;; autoload cookies.
534 ;; There shouldn't be more than one such entry.
535 ;; Remove the obsolete section.
536 (autoload-remove-section (match-beginning 0))
537 (let ((last-time (nth 4 form)))
538 (dolist (file file)
539 (let ((file-time (nth 5 (file-attributes file))))
540 (when (and file-time
541 (not (time-less-p last-time file-time)))
542 ;; file unchanged
543 (push file no-autoloads)
544 (setq actions
545 (elinstall-remove-autogen-action
546 file actions)))))))
547 ((not (stringp file)))
549 (let
550 ((file-path
551 (locate-library file nil use-load-path)))
552 (cond
553 ;;File doesn't exist, so remove its
554 ;;section.
555 ((not file-path)
556 (autoload-remove-section
557 (match-beginning 0)))
559 ;; File hasn't changed, so do nothing.
560 ((equal
561 (nth 4 form)
562 (nth 5 (file-attributes file-path)))
563 nil)
565 (elinstall-deffile-insert
566 (elinstall-get-autogen-action
567 file actions))))
569 (setq actions
570 (elinstall-remove-autogen-action
571 file actions))))))))
573 ;; Remaining actions have no existing autoload sections yet.
574 (setq no-autoloads
575 (append no-autoloads
576 (delq nil (mapcar #'elinstall-deffile-insert actions))))
577 (when no-autoloads
578 ;; Sort them for better readability.
579 (setq no-autoloads (sort no-autoloads 'string<))
580 ;; Add the `no-autoloads' section.
581 (goto-char (point-max))
582 (search-backward "\f" nil t)
583 (elinstall-insert-autoload-section
585 (list 'autoloads nil nil no-autoloads this-time)))
586 (save-buffer))))
588 ;;;_ . elinstall-stage-update-deffiles
589 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
590 "Update any deffiles mentioned in SEGMENT-LIST.
591 FORCE and USE-LOAD-PATH have the same meaning as in
592 `elinstall-update-deffile'.
594 (mapcar
595 #'(lambda (segment)
596 (let*
597 ((deffile (car segment)))
598 (if (stringp deffile)
599 (elinstall-update-deffile deffile (cdr segment) force
600 use-load-path))))
601 segment-list))
603 ;;;_ , Doing actions to arrange preloads
604 ;;;_ . elinstall-symlink-on-emacs-start
605 (defun elinstall-symlink-on-emacs-start
606 (filename target-basename target-dir &optional priority force)
607 "Symlink to TARGET-BASENAME.el in TARGET-DIR
609 If PRIORITY is given, it will be used as the priority prefix,
610 otherwise elinstall-default-priority will be.
611 PRIORITY must be an integer or nil.
612 If FORCE is `t', do it regardless of timestamps etc.
613 Other non-nil cases of FORCE are reserved for future development."
614 (let*
616 (priority (or priority elinstall-default-priority))
617 (target-name-nodir
618 (format
619 "%d%s.el"
620 priority
621 target-basename))
622 (target
623 (expand-file-name target-name-nodir target-dir)))
626 (cond
627 ;;Path should already exist.
628 ((not
629 (file-exists-p target-dir))
630 (message "The target directory doesn't exist."))
631 ;;Target shouldn't already exist, but if force is given, let
632 ;;user override.
633 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
634 ;;do nothing even on force.
635 ((and
636 (file-exists-p target)
638 (not force)
639 (not
640 (yes-or-no-p
641 (format "Really overwrite %s? " target))))
642 (message "File %s already exists" target)))
645 (make-symbolic-link
646 filename
647 target
648 nil)))))
650 ;;;_ . elinstall-add-to-dot-emacs
651 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
652 "Add code to load FILENAME to .emacs.
653 FILENAME should not have an extension"
655 ;;Visit .emacs
656 (with-current-buffer (find-file-noselect dot-emacs-name)
657 (save-excursion
658 ;;add at end of file
659 (goto-char (point-max))
660 (insert "\n;;Added by elinstall")
661 (insert "\n;;Consider using my-site-start to manage .emacs\n")
662 (pp `(load ,filename) (current-buffer))
663 (save-buffer))))
666 ;;;_ . elinstall-arrange-preload
667 ;;;###autoload
668 (defun elinstall-arrange-preload (force filename basename &optional priority)
669 "Arrange for FILENAME to be loaded on emacs start.
670 FORCE has its usual meaning.
671 BASENAME and PRIORITY are used as arguments to
672 `elinstall-symlink-on-emacs-start'.
675 (let
676 ((preload-target elinstall-default-preload-target))
678 ;;Dispatch the possibilities.
679 (cond
680 ((eq preload-target 'dot-emacs)
681 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
682 ((stringp preload-target)
683 (elinstall-symlink-on-emacs-start
684 filename basename preload-target priority force))
685 ((null preload-target)
686 (message "Not arranging for preloads"))
688 (message "I don't recognize that")))))
689 ;;;_ . elinstall-stage-arrange-preloads
690 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
691 "Arrange any preloads mentioned in ACTIONS."
693 (mapcar
694 #'(lambda (act)
695 (case (car act)
696 (preload-file
697 (let*
698 ( (filename
699 (caddr act))
700 (proceed-p
701 (case (second act)
702 ((t) t)
703 ((nil) nil)
704 (if-used
705 (member filename deffiles-used)))))
707 (when proceed-p
708 (apply
709 #'elinstall-arrange-preload
710 force
711 (cddr act)))))
713 (error
714 "elinstall-stage-arrange-preloads: Action not
715 recognized."))) )
716 actions))
719 ;;;_ , Run tests
720 ;;;_ . elinstall-stage-run-tests
721 (defun elinstall-stage-run-tests (actions)
722 "Run any tests mentioned in ACTIONS."
724 (mapcar
725 #'(lambda (act)
726 (case (car act)
727 (run-tests
728 ;;$$WRITE ME - not a high priority right now.
729 nil)
731 (error
732 "elinstall-stage-run-tests: Action not
733 recognized."))) )
734 actions))
737 ;;;_ , Byte compile
738 ;;;_ . elinstall-stage-byte-compile
739 (defun elinstall-stage-byte-compile (actions)
740 "Do any byte-compilation mentioned in ACTIONS."
742 (mapcar
743 #'(lambda (act)
744 (case (car act)
745 ;;$$IMPROVE ME Understand flags to control second
746 ;;argument (whether to load file after
747 ;;compilation)
748 (byte-compile
749 (byte-compile-file (second act)))
751 (error
752 "elinstall-stage-byte-compile: Action not
753 recognized."))) )
754 actions))
755 ;;;_ . Segregating actions
756 ;;;_ , elinstall-remove-empty-segs
757 (defun elinstall-remove-empty-segs (segment-list)
758 "Return SEGMENT-LIST minus any segments that have no actions.
759 Intended only for the deffile stage data."
760 (delq nil
761 (mapcar
762 #'(lambda (segment)
763 (if (cdr segment)
764 segment
765 nil))
766 segment-list)))
768 ;;;_ , elinstall-segregate-actions
769 (defun elinstall-segregate-actions (actions)
770 "Return actions segregated by deffile.
772 Returns a list whose elements are each a cons of:
773 * deffile filename or nil
774 * A list of actions to be done for that deffile."
776 (let
778 (build-deffiles '())
779 (run-tests '())
780 (byte-compile '())
781 (arrange-preloads '()))
783 (dolist (act actions)
784 (when act
785 (case (car act)
786 ((add-file-autoloads
787 add-to-info-path
788 add-to-load-path)
789 (let*
790 ((deffile-name (second act))
791 (cell-already
792 (assoc deffile-name build-deffiles)))
793 (if cell-already
794 ;;There are already actions on this deffile.
795 ;;Splice this action in.
796 (setcdr cell-already
797 (cons act (cdr cell-already)))
798 ;;There are no actions on this deffile. Add a
799 ;;place for them and include this action.
800 (push (list deffile-name act) build-deffiles))))
801 (preload-file
802 (push act arrange-preloads))
803 (run-tests
804 (push act run-tests))
805 (byte-compile
806 (push act byte-compile)))))
808 (elinstall-make-stages
809 :build-deffiles
810 (elinstall-remove-empty-segs build-deffiles)
811 :run-tests
812 run-tests
813 :byte-compile
814 byte-compile
815 :arrange-preloads
816 arrange-preloads)))
817 ;;;_ . Finding actions
818 ;;;_ , Informational
819 ;;;_ . elinstall-dir-has-info
821 ;;$$IMPROVE ME - Can this test be made more precise?
822 (defun elinstall-dir-has-info (dir)
823 "Return non-nil if DIR has info files in it.
824 DIR should be an absolute path."
826 (string-match "/info/" dir)
827 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
829 ;;;_ , Workers
830 ;;;_ . List of special variables used in this section
831 ;;load-path-element - The relevant element of load-path
832 ;;def-file - The file the autoload definitions etc will go into.
833 ;;add-to-load-path-p - Controls whether to add to load-path.
834 ;;recurse-dirs-p - Whether to recurse into subdirectories.
836 ;;;_ . elinstall-actions-for-source-file
837 (defun elinstall-actions-for-source-file (filename dir)
838 "Return a list of actions to do for FILENAME in DIR.
839 Special variables are as noted in \"List of special variables\"."
840 (declare (special
841 load-path-element def-file))
842 (let
843 ((full-path
844 (expand-file-name filename dir)))
845 (when
846 (and
847 (file-readable-p full-path)
848 (not (auto-save-file-name-p full-path)))
849 ;;$$IMPROVE ME create and use relevant control variables.
850 (let*
852 (visited (get-file-buffer full-path))
853 (buf
854 (or
855 visited
856 ;;Visit the file cheaply.
857 ;;hack-local-variables can give errors.
858 (ignore-errors (autoload-find-file full-path))))
859 (def-file
861 (ignore-errors
862 (with-current-buffer buf
863 (if (local-variable-p 'generated-autoload-file)
864 (elinstall-expand-deffile-name
865 generated-autoload-file)
866 nil)))
867 def-file))
868 ;;Figure out whether to run some actions, by file local vars.
869 (autoloads-p
870 (ignore-errors
871 (with-current-buffer buf
872 (not no-update-autoloads))))
873 (compile-p
874 (and
875 (featurep 'byte-compile)
876 (string-match emacs-lisp-file-regexp filename)
877 (ignore-errors
878 (with-current-buffer buf
879 (not no-byte-compile)))
880 (let*
881 ((dest (byte-compile-dest-file full-path))
882 ;;$$PUNT - currently, we wouldn't have
883 ;;gotten here if we weren't intending to do
884 ;;everything so set force variables accordingly.
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 (elinstall-make-autoload-action
900 buf def-file load-path-element full-path)
901 nil))
902 (unless visited (kill-buffer-if-not-modified buf)))))))
903 ;;;_ . elinstall-actions-for-dir
904 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
905 "Make actions for DIR.
906 Recurse just if RECURSE-DIRS-P"
907 (declare (special
908 load-path-element def-file add-to-load-path-p))
909 ;;This does not treat symlinks specially. $$IMPROVE ME it could
910 ;;treat/not treat them conditional on control variables.
911 (let*
913 ;;Relative filenames of the source files. We know our
914 ;;loaddefs.el isn't really source so remove it. We'd have
915 ;;removed it anyways after seeing file local vars.
917 (elisp-source-files
918 (remove def-file
919 (directory-files
920 dirname
921 nil
922 elinstall-elisp-regexp)))
923 ;;Absolute filenames of subdirectories.
924 ;;Don't accept any directories beginning with dot. If user
925 ;;really wants to explore one he can use `(dir ".NAME")'.
926 (sub-dirs
927 (if recurse-dirs-p
928 (delq nil
929 (mapcar
930 #'(lambda (filename)
932 (file-directory-p filename)
933 filename
934 nil))
935 (directory-files
936 dirname t
937 "[^\\.]")))
938 '()))
940 (load-path-here
941 (and
942 elisp-source-files ;;If list is not empty.
943 add-to-load-path-p))
944 (load-path-element
945 (if load-path-here
946 dirname
947 load-path-element)))
949 (append
950 ;;Sometimes arrange to add this directory to load-path.
951 (if load-path-here
952 `((add-to-load-path
953 ,def-file
954 ,load-path-element))
955 '())
957 ;;$$IMPROVE ME - be controlled by a control variable.
958 ;;Sometimes add this directory to info path.
960 (elinstall-dir-has-info dirname)
961 `((add-to-info-path
962 ,def-file
963 "."))
964 '())
966 (apply #'nconc
967 (mapcar
968 #'(lambda (filename)
969 (elinstall-actions-for-source-file
970 filename
971 dirname))
972 elisp-source-files))
974 (if recurse-dirs-p
975 (apply #'nconc
976 (mapcar
977 #'(lambda (filename)
978 (elinstall-find-actions-by-spec-x
980 (expand-file-name
981 filename
982 dirname)))
983 sub-dirs))
984 '()))))
986 ;;;_ . elinstall-find-actions-by-spec-x
988 (defun elinstall-find-actions-by-spec-x (spec dir)
989 "Return a list of actions to do, controlled by SPEC."
990 (declare (special
991 load-path-element def-file add-to-load-path-p
992 recurse-dirs-p))
994 (if (consp spec)
995 (case (car spec)
997 ;;in FN SPEC
998 (let
999 ((new-dir
1000 (expand-file-name
1001 (second spec)
1002 dir)))
1004 (elinstall-find-actions-by-spec-x
1005 (third spec)
1006 new-dir)))
1008 (all
1009 ;;all SPEC*
1010 (apply #'nconc
1011 (mapcar
1012 #'(lambda (sub-spec)
1013 (elinstall-find-actions-by-spec-x
1014 sub-spec
1015 dir))
1016 (cdr spec))))
1017 (matching
1018 ;;matching PATTERN SPEC
1019 (apply #'nconc
1020 (mapcar
1021 #'(lambda (dir)
1022 (elinstall-find-actions-by-spec-x
1023 (third spec)
1024 dir))
1025 (directory-files
1026 dir t (second spec))))
1028 (file
1029 ;;file FN
1030 (elinstall-actions-for-source-file
1031 (second spec) dir))
1033 ;;$$ADD ME control, rather than trying to bind all control
1034 ;;variables so we can safely bind one, will use set and
1035 ;;unwind-protect.
1037 (dir
1038 ;;dir FN
1039 (elinstall-actions-for-dir
1040 (expand-file-name
1041 (second spec)
1042 dir)
1043 nil))
1046 (load-path
1047 ;;load-path SPEC
1048 (append
1049 `((add-to-load-path ,def-file ,dir))
1050 (let
1051 ((load-path-element dir))
1052 (elinstall-find-actions-by-spec-x
1053 (second spec)
1054 dir))))
1057 (def-file
1058 ;;def-file FN ARGS SPEC
1059 (let
1060 ((def-file
1061 (expand-file-name
1062 (second spec)
1063 dir))
1064 (for-preload (third spec)))
1065 (assert (listp for-preload))
1066 (append
1067 (list
1069 (and for-preload (car for-preload))
1070 `(preload-file
1071 ,(car for-preload)
1072 ,def-file
1073 ,@(cdr for-preload))
1074 '()))
1076 (elinstall-find-actions-by-spec-x
1077 (fourth spec) dir)))))
1079 ;;Single symbols
1080 (case spec
1081 (dir
1082 (elinstall-actions-for-dir dir nil))
1083 ((t)
1084 (elinstall-actions-for-dir dir t)))))
1086 ;;;_ . elinstall-find-actions-by-spec
1087 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1090 (let
1091 ((load-path-element load-path-element)
1092 (def-file def-file)
1093 (add-to-load-path-p t)
1094 (recurse-dirs-p t))
1095 (declare (special
1096 load-path-element def-file add-to-load-path-p
1097 recurse-dirs-p))
1099 (elinstall-find-actions-by-spec-x
1100 spec dir)))
1102 ;;;_ . high-level work
1103 ;;;_ , elinstall-get-relevant-load-path
1104 (defun elinstall-get-relevant-load-path (actions)
1106 (delq nil
1107 (mapcar
1108 #'(lambda (act)
1109 (case (car act)
1110 (add-to-load-path
1111 (second act))
1112 (t nil)))
1113 actions)))
1114 ;;;_ , elinstall-get-deffile-list
1115 (defun elinstall-get-deffile-list (stages)
1116 "Get a list of deffile names"
1118 (mapcar
1119 #'car
1120 (elinstall-stages->build-deffiles stages)))
1122 ;;;_ , elinstall-x
1123 (defun elinstall-x (dir spec &optional force)
1125 (let*
1127 ;;This is just the default deffile, spec can override it.
1128 (def-file
1129 (elinstall-expand-deffile-name nil))
1130 (actions
1131 (elinstall-find-actions-by-spec
1132 spec
1135 def-file))
1136 (stages (elinstall-segregate-actions actions))
1137 (use-load-path
1138 (elinstall-get-relevant-load-path
1139 actions)))
1141 (elinstall-stage-update-deffiles
1142 (elinstall-stages->build-deffiles stages)
1143 force
1144 use-load-path)
1145 (elinstall-stage-arrange-preloads
1146 (elinstall-stages->arrange-preloads stages)
1147 (elinstall-get-deffile-list stages)
1148 force)
1149 (elinstall-stage-byte-compile
1150 (elinstall-stages->byte-compile stages))
1152 ;;;_ , Entry points
1153 ;;;_ . elinstall
1154 ;;;###autoload
1155 (defun elinstall (project-name path spec &optional force)
1156 "Install elisp files.
1157 They need not be a formal package.
1159 Parameters:
1161 PROJECT-NAME - the name of the project
1163 PATH - Path to the project.
1164 Suggestion: (elinstall-directory-true-name)
1166 SPEC - a spec for the autoloads etc to make. It can be as simple as
1167 \(dir \"\.\") for installing one directory.
1169 If FORCE is t, install a package even if it has already been
1170 installed. Other non-nil cases of FORCE are reserved for future
1171 development."
1173 (when
1174 (and
1175 (or
1176 force
1177 (not (elinstall-already-installed project-name)))
1178 (yes-or-no-p (format "Re-install %s? " project-name)))
1179 (elinstall-x
1180 path
1181 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1182 force)
1183 (elinstall-record-installed project-name)))
1187 ;;;_ . elinstall-update-directory-autoloads
1188 ;;$$SPLIT ME - one to just classically update autoloads, one to
1189 ;;install a file.
1190 ;;$$TEST ME
1191 ;;;###autoload
1192 (defun elinstall-update-directory-autoloads (dir)
1195 (interactive "DInstall all elisp files from directory: ")
1198 (let
1199 ((def-file-name
1200 (elinstall-expand-deffile-name
1201 generated-autoload-file)))
1203 (elinstall-x
1205 `(def-file ,def-file-name (nil) (dir ".")))))
1209 ;;;_ . elinstall-update-file-autoloads
1210 ;;$$SPLIT ME - one to just classically update autoloads, one to
1211 ;;install a file.
1212 ;;$$TEST ME
1213 ;;;###autoload
1214 (defun elinstall-update-file-autoloads (file)
1217 (interactive "fInstall elisp file: ")
1218 (let
1219 ((def-file-name
1220 ;;This is the default. File local vars can override it.
1221 (elinstall-expand-deffile-name
1222 generated-autoload-file)))
1223 (elinstall-x
1224 file
1225 `(def-file ,def-file-name (nil) (file ,file)))))
1226 ;;;_. Footers
1227 ;;;_ , Provides
1229 (provide 'elinstall)
1231 ;;;_ * Local emacs vars.
1232 ;;;_ + Local variables:
1233 ;;;_ + mode: allout
1234 ;;;_ + End:
1236 ;;;_ , End
1237 ;;; elinstall.el ends here