New function elinstall-proceed-p
[elinstall.git] / elinstall.el
blobd49979375d8988b61904db23870745709cfbce07
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.
24 ;;;_ , Version
25 ;;Version: 1.0
27 ;;;_ , Commentary:
29 ;; Entry points:
30 ;; elinstall Use this for overall loading
32 ;; elinstall-arrange-preload - Use this for non-autogenerated
33 ;; files that need to be linked in.
35 ;; elinstall-update-directory-autoloads
36 ;; elinstall-update-file-autoloads
38 ;;;_ , Requires
40 (require 'autoload)
41 (require 'pp)
42 (require 'cus-edit) ;;Because we save "installedness" manually
43 (require 'byte-compile nil t) ;;
46 ;;;_. Body
47 ;;;_ , Customizations
48 ;;;_ . Group
49 (defgroup elinstall
50 '()
51 "Customizations for elinstall"
52 :group 'development)
53 ;;;_ . elinstall-default-priority
54 (defcustom elinstall-default-priority
56 "Default priority for site-start"
57 :group 'elinstall
58 :type 'integer)
59 ;;;_ . elinstall-default-preload-target
60 (defcustom elinstall-default-preload-target
61 "~/.emacs.d/site-start.d/"
62 "Default preload-target for registering autoloads"
63 :group 'elinstall
64 :type
65 '(choice
66 (const "~/.emacs.d/site-start.d/")
67 (const "/etc/emacs/site-start.d/")
68 (directory "" )
69 (const nil)
70 (const 'dot-emacs)))
71 ;;;_ . elinstall-restrain-install
72 (defcustom elinstall-restrain-install '()
73 "Restrain elinstall for specific packages"
74 :group 'elinstall
75 :type
76 '(repeat
77 (list
78 (choice :format "%[%t%]: %v"
79 (string :tag "Package name")
80 (const :tag "All packages" t))
81 ;;Spec
82 )))
84 ;;;_ . elinstall-already-installed
85 (with-no-warnings
86 (defcustom elinstall-already-installed
87 '()
88 "(AUTOMATIC) Things that have already been installed.
89 This exists for recording what has been installed.
91 Though it's saved as customizable, user interaction is not
92 contemplated." ))
93 ;;Tell the byte-compiler it's a variable.
94 (defvar elinstall-already-installed)
95 ;;;_ , Types
96 ;;;_ . elinstall-stages
97 (defstruct (elinstall-stages
98 (:constructor elinstall-make-stages)
99 (:conc-name elinstall-stages->)
100 (:copier nil))
101 "The elinstall stages"
102 build-deffiles
103 run-tests
104 byte-compile
105 arrange-preloads)
106 ;;;_ , Data
107 ;;;_ . Regular expressions
108 ;;;_ , elinstall-elisp-regexp
109 (defconst elinstall-elisp-regexp
110 (let ((tmp nil))
111 (dolist
112 (suf (get-load-suffixes))
113 (unless (string-match "\\.elc" suf) (push suf tmp)))
114 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
115 "Regular expression that matches elisp files" )
116 ;;;_ , Utilities
117 ;;;_ . elinstall-file-mod-time
118 (defsubst elinstall-file-mod-time (file)
119 "Return the modification time of FILE"
120 (nth 5 (file-attributes file)))
122 ;;;_ . elinstall-directory-true-name
123 (defun elinstall-directory-true-name ()
124 "Get the true name of the directory the calling code lives in.
125 CAUTION: This is sensitive to where it's called. That's the point of it."
126 (file-name-directory
127 (if load-file-name
128 (file-truename load-file-name)
129 (file-truename buffer-file-name))))
130 ;;;_ . Checking installedness
131 ;;;_ , elinstall-get-installation-record
132 (defun elinstall-get-installation-record (project-name)
133 "Return the installation record for PROJECT-NAME."
135 (assoc project-name elinstall-already-installed))
137 ;;;_ , elinstall-already-installed
138 (defun elinstall-already-installed (project-name)
139 "Return non-nil if PROJECT-NAME has been installed."
140 (elinstall-get-installation-record project-name))
142 ;;;_ , elinstall-record-installed
143 (defun elinstall-record-installed (project-name &optional version)
144 "Record that PROJECT-NAME has been installed."
145 (let
146 ((new-item
147 (list
148 project-name
149 (or version "0")
150 (current-time)
151 'installed))
152 (old-item
153 (elinstall-get-installation-record project-name))
154 (print-length nil)
155 (print-level nil))
156 (when old-item
157 (setq elinstall-already-installed
158 (delete old-item elinstall-already-installed)))
159 (push new-item elinstall-already-installed)
160 (customize-save-variable
161 'elinstall-already-installed
162 elinstall-already-installed
163 "Set by elinstall-record-installed")))
164 ;;;_ . Finding deffiles
165 ;;;_ , elinstall-expand-deffile-name
166 (defun elinstall-expand-deffile-name (deffile)
167 "Expand DEFFILE autoload.el's way."
169 (expand-file-name (or deffile "loaddefs.el")
170 (expand-file-name "lisp"
171 source-directory)))
172 ;;;_ . Checking restraint specs
173 (defun elinstall-proceed-p (topic restraints-1 restraints-2 old-p
174 redo-prompt ask-prompt)
175 "Return non-nil if actions on TOPIC should proceed."
177 (check-type topic symbol)
178 (let*
179 ( (cell (assq topic restraints-1))
180 (cell
181 (if
182 (or (not cell) (eq (second cell) t))
183 (assq topic restraints-2)
184 cell)))
185 (case (second cell)
186 ((nil)
187 nil)
188 ((t always)
190 (update
191 (not old-p))
192 (ask-for-old
193 (if old-p
194 (y-or-n-p redo-prompt)
196 (ask
197 (y-or-n-p ask-prompt)))))
199 ;;;_ , Work
200 ;;;_ . Doing actions
202 ;;;_ , Doing autoload actions (adapted from autoload.el)
203 ;;;_ . Utilities about the action list
204 ;;;_ , elinstall-remove-autogen-action
205 (defun elinstall-remove-autogen-action (file actions)
206 "Return ACTIONS minus any add-file-autoloads on FILE removed."
208 (delq nil
209 (mapcar
210 #'(lambda (act)
211 (case (car act)
212 (add-file-autoloads
213 (if (equal file (third act))
215 act))
216 (t act)))
217 actions)))
218 ;;;_ , elinstall-get-autogen-action
219 (defun elinstall-get-autogen-action (file actions)
221 (let
222 ((the-act))
223 (dolist (act actions)
224 (case (car act)
225 (add-file-autoloads
226 (when (equal file (third act))
227 (setq the-act act)))))
228 the-act))
229 ;;;_ . About printing to autoload file
230 ;;;_ , elinstall-insert-section-header
231 (defun elinstall-insert-section-header (outbuf form)
232 "Insert the section-header line,
233 which lists the file name and which functions are in it, etc."
234 (insert generate-autoload-section-header)
235 (prin1 form outbuf)
236 (terpri outbuf)
237 ;; Break that line at spaces, to avoid very long lines.
238 ;; Make each sub-line into a comment.
239 (with-current-buffer outbuf
240 (save-excursion
241 (forward-line -1)
242 (while (not (eolp))
243 (move-to-column 64)
244 (skip-chars-forward "^ \n")
245 (or (eolp)
246 (insert "\n" generate-autoload-section-continuation))))))
248 ;;;_ , elinstall-insert-autoload-section
249 (defun elinstall-insert-autoload-section (text form &optional comment-string)
250 "Insert TEXT into current buffer as an autoload section"
252 (let* (
253 (print-length nil)
254 (print-level nil)
255 ;; This does something in Lucid Emacs.
256 (print-readably t)
257 (float-output-format nil))
259 (elinstall-insert-section-header (current-buffer) form)
260 (when comment-string
261 (insert ";;; " comment-string "\n"))
262 (insert text)
263 (insert generate-autoload-section-trailer)))
265 ;;;_ . Making autoloads
266 ;;;_ , elinstall-make-autoload-action
267 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
268 "Return the autoloads for current buffer as a string"
270 ;;We put all the text into a temp buffer, then get that buffer's
271 ;;string.
272 (let
273 ((outbuf
274 (generate-new-buffer " *temp*"))
275 (autoloads-done '())
276 (short-name
277 (file-name-nondirectory full-path))
279 (print-length nil)
280 (print-level nil)
281 ;; Apparently this does something in Lucid Emacs.
282 (print-readably t)
283 (float-output-format nil)
285 ;;load-name relative to a member of load-path.
286 (relative-name
287 (file-name-sans-extension
288 (file-relative-name
289 full-path
290 load-path-element))))
292 (with-current-buffer buf
293 (unwind-protect
294 (save-excursion
295 (save-restriction
296 (widen)
297 (goto-char (point-min))
298 (message "Finding autoloads for %s..." short-name)
299 (while (not (eobp))
300 (skip-chars-forward " \t\n\f")
301 (cond
302 ((looking-at (regexp-quote generate-autoload-cookie))
303 (search-forward generate-autoload-cookie)
304 (skip-chars-forward " \t")
305 (if (eolp)
306 ;; Read the next form and make an autoload.
307 (let* ((form (prog1 (read (current-buffer))
308 (or (bolp) (forward-line 1))))
309 (autoload
310 (make-autoload form relative-name)))
311 (if autoload
312 (push (nth 1 form) autoloads-done)
313 (setq autoload form))
314 (let ((autoload-print-form-outbuf outbuf))
315 (autoload-print-form autoload)))
317 ;; Copy the rest of the line to the output.
318 (princ (buffer-substring
319 (progn
320 ;; Back up over whitespace,
321 ;; to preserve it.
322 (skip-chars-backward " \f\t")
323 (if (= (char-after (1+ (point))) ? )
324 ;; Eat one space.
325 (forward-char 1))
326 (point))
327 (progn (forward-line 1) (point)))
328 outbuf)))
329 ((looking-at ";")
330 ;; Don't read the comment.
331 (forward-line 1))
333 (forward-sexp 1)
334 (forward-line 1))))
335 (message "Finding autoloads for %s...done" short-name))
337 ;;Return this action. The temp buffer's contents is
338 ;;our final string.
339 `(add-file-autoloads
340 ,def-file
341 ,relative-name
342 ,full-path
343 ,(with-current-buffer outbuf (buffer-string))
344 ,autoloads-done))
346 ;;This in unwind-protected
347 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
350 ;;;_ , elinstall-generate-file-autoloads
352 (defun elinstall-generate-file-autoloads
353 (relative-name full-name text autoloads-done)
354 "Insert at point a loaddefs autoload section for FILE.
355 Autoloads are generated for defuns and defmacros in FILE
356 marked by `generate-autoload-cookie' (which see).
357 If FILE is being visited in a buffer, the contents of the buffer
358 are used.
359 Return non-nil in the case where no autoloads were added at point.
361 FULL-NAME is the absolute name of the file.
362 RELATIVE-NAME is its name respective to some component of load-path."
363 (if (not (equal text ""))
364 ;; Insert the section-header line which lists the file name and
365 ;; which functions are in it, etc.
366 (elinstall-insert-autoload-section
367 text
368 (list 'autoloads
369 autoloads-done
370 relative-name
371 (autoload-trim-file-name full-name)
372 (elinstall-file-mod-time full-name))
373 (concat
374 "Generated autoloads from "
375 (autoload-trim-file-name full-name)))
378 ;;;_ , elinstall-deffile-insert-autoloads
379 (defun elinstall-deffile-insert-autoloads (file args)
380 "Update the autoloads for FILE in current buffer.
381 Return FILE if there was no autoload cookie in it, else nil.
383 Current buffer must be a loaddef-style file.
385 LOAD-NAME is the absolute name of the file.
386 RELATIVE-NAME is its name respective to some component of load-path."
387 (let (
388 (found nil)
389 (no-autoloads nil))
391 (save-excursion
392 (save-restriction
393 (widen)
394 (goto-char (point-min))
395 ;; Look for the section for FILE
396 (while (and (not found)
397 (search-forward generate-autoload-section-header nil t))
398 (let ((form (autoload-read-section-header)))
399 (cond
400 ((equal (nth 2 form) file)
401 ;; We found the section for this file.
402 (let ((begin (match-beginning 0)))
403 (progn
404 (search-forward generate-autoload-section-trailer)
405 (delete-region begin (point))
406 (setq found t))))
407 ((string< file (nth 2 form))
408 ;; We've come to a section alphabetically later than
409 ;; FILE. We assume the file is in order and so
410 ;; there must be no section for FILE. We will
411 ;; insert one before the section here.
412 (goto-char (match-beginning 0))
413 (setq found 'new)))))
414 (unless found
415 (progn
416 (setq found 'new)
417 ;; No later sections in the file. Put before the last page.
418 (goto-char (point-max))
419 (search-backward "\f" nil t)))
420 (setq no-autoloads
421 (apply #'elinstall-generate-file-autoloads
422 file args))))
424 (if no-autoloads file nil)))
425 ;;;_ . Arranging to add to info-path and load-path
426 ;;;_ , elinstall-generate-add-to-path
427 (defun elinstall-generate-add-to-path (path-element type)
428 "Insert code at point to add PATH-ELEMENT to a path.
429 If TYPE is:
430 * `add-to-load-path', add to load-path
431 * `add-to-info-path', add to Info-additional-directory-list
433 Current buffer must be a loaddef-style file."
434 (let ( (path-symbol
435 (case type
436 (add-to-load-path 'load-path)
437 (add-to-info-path 'Info-additional-directory-list)
438 (t (error "Type not recognized"))))
439 (description
440 (case type
441 (add-to-load-path "load-path")
442 (add-to-info-path "info-path")))
443 (autoloads-done '())
444 (print-length nil)
445 (print-level nil)
446 ;; This does something in Lucid Emacs.
447 (print-readably t)
448 (float-output-format nil))
450 (message "Generating %s additions..." description)
452 (elinstall-insert-autoload-section
453 (pp-to-string
454 `(add-to-list ',path-symbol
455 (expand-file-name
456 ,(file-relative-name path-element)
457 (if load-file-name
458 (file-name-directory
459 (file-truename load-file-name))))))
460 (list type (list path-element) nil nil nil)
461 nil)
462 (message "Generating %s additions...done" description)))
465 ;;;_ , elinstall-deffile-insert-add-to-path
466 (defun elinstall-deffile-insert-add-to-path (path-element type)
467 "Insert code in current buffer to add PATH-ELEMENT to a path.
468 If TYPE is:
469 * `add-to-load-path', add to load-path
470 * `add-to-info-path', add to Info-default-directory-list
472 Current buffer must be a loaddef-style file."
473 (let (
474 (found nil)
475 (no-autoloads nil))
477 (save-excursion
478 (save-restriction
479 (widen)
480 (goto-char (point-min))
481 ;; Look for the section for PATH-ELEMENT
482 (while (and (not found)
483 (search-forward generate-autoload-section-header nil t))
484 (let ((form (autoload-read-section-header)))
485 (cond
486 ((and
487 (equal (nth 0 form) type)
488 (member path-element (nth 1 form)))
490 ;; We found the section for this add.
491 (let ((begin (match-beginning 0)))
492 (progn
493 (search-forward generate-autoload-section-trailer)
494 (delete-region begin (point))
495 (setq found t)))))))
497 (unless found
498 (progn
499 (setq found 'new)
500 ;; No later sections in the file. Put before the last page.
501 (goto-char (point-max))
502 (search-backward "\f" nil t)))
504 (elinstall-generate-add-to-path path-element type)))
506 ;;This never belongs in the no-autoloads section.
507 nil))
508 ;;;_ . elinstall-deffile-insert
510 (defun elinstall-deffile-insert (action)
511 "Insert autoloads etc into current file according to ACTION.
512 The format of ACTION is described in the design docs.
514 Return filename if this action belongs in the no-autoload section."
516 (when action
517 (case (car action)
518 (add-file-autoloads
519 (elinstall-deffile-insert-autoloads
520 (third action)
521 (nthcdr 3 action)))
523 (add-to-load-path
524 (elinstall-deffile-insert-add-to-path
525 (third action)
526 'add-to-load-path)
527 nil)
529 (add-to-info-path
530 (elinstall-deffile-insert-add-to-path
531 (third action)
532 'add-to-info-path)
533 nil)
535 ((preload-file run-tests byte-compile)
536 (error "This case should not come here.")))))
538 ;;;_ . elinstall-prepare-deffile
539 (defun elinstall-prepare-deffile (deffile)
540 "Try to ensure that DEFFILE is available for receiving autoloads"
542 (autoload-ensure-default-file deffile)
543 (with-current-buffer (find-file-noselect deffile)
546 ;; We must read/write the file without any code conversion,
547 ;; but still decode EOLs.
548 (let ((coding-system-for-read 'raw-text))
550 ;; This is to make generated-autoload-file have Unix EOLs, so
551 ;; that it is portable to all platforms.
552 (setq buffer-file-coding-system 'raw-text-unix))
553 (or (> (buffer-size) 0)
554 (error "Autoloads file %s does not exist" buffer-file-name))
555 (or (file-writable-p buffer-file-name)
556 (error "Autoloads file %s is not writable"
557 buffer-file-name))))
559 ;;;_ . elinstall-update-deffile
561 ;;Adapted from autoload.el `update-directory-autoloads'.
563 (defun elinstall-update-deffile (target actions &optional
564 use-load-path force)
566 Update file TARGET with current autoloads as specified by ACTIONS.
567 Also remove any old definitions pointing to libraries that can no
568 longer be found.
570 ACTIONS must be a list of actions (See the format doc). Each one's
571 filename must be relative to some element of load-path.
573 USE-LOAD-PATH is a list to use as load-path. It should include
574 any new load-path that we are arranging to create. If it's not given,
575 load-path itself is used.
577 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
578 Other non-nil cases of FORCE are reserved for future development.
580 This uses `update-file-autoloads' (which see) to do its work.
581 In an interactive call, you must give one argument, the name
582 of a single directory."
583 (let
585 (use-load-path (or use-load-path load-path))
586 (this-time (current-time))
587 ;;files with no autoload cookies.
588 (no-autoloads nil))
590 (elinstall-prepare-deffile target)
591 (with-current-buffer
592 (find-file-noselect target)
593 (save-excursion
594 (setq actions
595 (elinstall-remove-autogen-action
596 (autoload-trim-file-name target)
597 actions))
599 (goto-char (point-min))
600 (while (search-forward generate-autoload-section-header nil t)
601 (let* ((form (autoload-read-section-header))
602 (file (nth 3 form)))
603 (cond ((and (consp file) (stringp (car file)))
604 ;; This is a list of files that have no
605 ;; autoload cookies.
606 ;; There shouldn't be more than one such entry.
607 ;; Remove the obsolete section.
608 (autoload-remove-section (match-beginning 0))
609 (let ((last-time (nth 4 form)))
610 (dolist (file file)
611 (let ((file-time (elinstall-file-mod-time file)))
612 (when (and file-time
613 (not (time-less-p last-time file-time)))
614 ;; file unchanged
615 (push file no-autoloads)
616 (setq actions
617 (elinstall-remove-autogen-action
618 file actions)))))))
619 ((not (stringp file)))
621 (let
622 ((file-path
623 (locate-library file nil use-load-path)))
624 (cond
625 ;;File doesn't exist, so remove its
626 ;;section.
627 ((not file-path)
628 (autoload-remove-section
629 (match-beginning 0)))
631 ;; File hasn't changed, so do nothing.
632 ((equal
633 (nth 4 form)
634 (elinstall-file-mod-time file-path))
635 nil)
637 (elinstall-deffile-insert
638 (elinstall-get-autogen-action
639 file actions))))
641 (setq actions
642 (elinstall-remove-autogen-action
643 file actions))))))))
645 ;; Remaining actions have no existing autoload sections yet.
646 (setq no-autoloads
647 (append no-autoloads
648 (delq nil (mapcar #'elinstall-deffile-insert actions))))
649 (when no-autoloads
650 ;; Sort them for better readability.
651 (setq no-autoloads (sort no-autoloads 'string<))
652 ;; Add the `no-autoloads' section.
653 (goto-char (point-max))
654 (search-backward "\f" nil t)
655 (elinstall-insert-autoload-section
657 (list 'autoloads nil nil no-autoloads this-time)))
658 (save-buffer))))
660 ;;;_ . elinstall-stage-update-deffiles
661 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
662 "Update any deffiles mentioned in SEGMENT-LIST.
663 FORCE and USE-LOAD-PATH have the same meaning as in
664 `elinstall-update-deffile'.
666 (mapcar
667 #'(lambda (segment)
668 (let*
669 ((deffile (car segment)))
670 (if (stringp deffile)
671 (elinstall-update-deffile deffile (cdr segment) force
672 use-load-path))))
673 segment-list))
675 ;;;_ , Doing actions to arrange preloads
676 ;;;_ . elinstall-symlink-on-emacs-start
677 (defun elinstall-symlink-on-emacs-start
678 (filename target-basename target-dir &optional priority force)
679 "Symlink to TARGET-BASENAME.el in TARGET-DIR
681 If PRIORITY is given, it will be used as the priority prefix,
682 otherwise elinstall-default-priority will be.
683 PRIORITY must be an integer or nil.
684 If FORCE is `t', do it regardless of timestamps etc.
685 Other non-nil cases of FORCE are reserved for future development."
686 (let*
688 (priority (or priority elinstall-default-priority))
689 (target-name-nodir
690 (format
691 "%d%s.el"
692 priority
693 target-basename))
694 (target
695 (expand-file-name target-name-nodir target-dir)))
698 (cond
699 ;;Path should already exist.
700 ((not
701 (file-exists-p target-dir))
702 (message "The target directory doesn't exist."))
703 ;;Target shouldn't already exist, but if force is given, let
704 ;;user override.
705 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
706 ;;do nothing even on force.
707 ((and
708 (file-exists-p target)
710 (not force)
711 (not
712 (yes-or-no-p
713 (format "Really overwrite %s? " target))))
714 (message "File %s already exists" target)))
717 (make-symbolic-link
718 filename
719 target
720 nil)))))
722 ;;;_ . elinstall-add-to-dot-emacs
723 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
724 "Add code to load FILENAME to .emacs.
725 FILENAME should not have an extension"
727 ;;Visit .emacs
728 (with-current-buffer (find-file-noselect dot-emacs-name)
729 (save-excursion
730 ;;add at end of file
731 (goto-char (point-max))
732 (insert "\n;;Added by elinstall")
733 (insert "\n;;Consider using my-site-start to manage .emacs\n")
734 (pp `(load ,filename) (current-buffer))
735 (save-buffer))))
738 ;;;_ . elinstall-arrange-preload
739 ;;;###autoload
740 (defun elinstall-arrange-preload (force filename basename &optional priority)
741 "Arrange for FILENAME to be loaded on emacs start.
742 FORCE has its usual meaning.
743 BASENAME and PRIORITY are used as arguments to
744 `elinstall-symlink-on-emacs-start'.
747 (let
748 ((preload-target elinstall-default-preload-target))
750 ;;Dispatch the possibilities.
751 (cond
752 ((eq preload-target 'dot-emacs)
753 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
754 ((stringp preload-target)
755 (elinstall-symlink-on-emacs-start
756 filename basename preload-target priority force))
757 ((null preload-target)
758 (message "Not arranging for preloads"))
760 (message "I don't recognize that")))))
761 ;;;_ . elinstall-stage-arrange-preloads
762 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
763 "Arrange any preloads mentioned in ACTIONS."
765 (mapcar
766 #'(lambda (act)
767 (case (car act)
768 (preload-file
769 (let*
770 ( (filename
771 (caddr act))
772 (proceed-p
773 (case (second act)
774 ((t) t)
775 ((nil) nil)
776 (if-used
777 (member filename deffiles-used)))))
779 (when proceed-p
780 (apply
781 #'elinstall-arrange-preload
782 force
783 (cddr act)))))
785 (error
786 "elinstall-stage-arrange-preloads: Action not
787 recognized."))) )
788 actions))
791 ;;;_ , Run tests
792 ;;;_ . elinstall-stage-run-tests
793 (defun elinstall-stage-run-tests (actions)
794 "Run any tests mentioned in ACTIONS."
796 (mapcar
797 #'(lambda (act)
798 (case (car act)
799 (run-tests
800 ;;$$WRITE ME - not a high priority right now.
801 nil)
803 (error
804 "elinstall-stage-run-tests: Action not
805 recognized."))) )
806 actions))
809 ;;;_ , Byte compile
810 ;;;_ . elinstall-stage-byte-compile
811 (defun elinstall-stage-byte-compile (actions)
812 "Do any byte-compilation mentioned in ACTIONS."
814 (mapcar
815 #'(lambda (act)
816 (case (car act)
817 ;;$$IMPROVE ME Understand flags to control second
818 ;;argument (whether to load file after
819 ;;compilation)
820 (byte-compile
821 (byte-compile-file (second act)))
823 (error
824 "elinstall-stage-byte-compile: Action not
825 recognized."))) )
826 actions))
827 ;;;_ . Segregating actions
828 ;;;_ , elinstall-remove-empty-segs
829 (defun elinstall-remove-empty-segs (segment-list)
830 "Return SEGMENT-LIST minus any segments that have no actions.
831 Intended only for the deffile stage data."
832 (delq nil
833 (mapcar
834 #'(lambda (segment)
835 (if (cdr segment)
836 segment
837 nil))
838 segment-list)))
840 ;;;_ , elinstall-segregate-actions
841 (defun elinstall-segregate-actions (actions)
842 "Return actions segregated by deffile.
844 Returns a list whose elements are each a cons of:
845 * deffile filename or nil
846 * A list of actions to be done for that deffile."
848 (let
850 (build-deffiles '())
851 (run-tests '())
852 (byte-compile '())
853 (arrange-preloads '()))
855 (dolist (act actions)
856 (when act
857 (case (car act)
858 ((add-file-autoloads
859 add-to-info-path
860 add-to-load-path)
861 (let*
862 ((deffile-name (second act))
863 (cell-already
864 (assoc deffile-name build-deffiles)))
865 (if cell-already
866 ;;There are already actions on this deffile.
867 ;;Splice this action in.
868 (setcdr cell-already
869 (cons act (cdr cell-already)))
870 ;;There are no actions on this deffile. Add a
871 ;;place for them and include this action.
872 (push (list deffile-name act) build-deffiles))))
873 (preload-file
874 (push act arrange-preloads))
875 (run-tests
876 (push act run-tests))
877 (byte-compile
878 (push act byte-compile)))))
880 (elinstall-make-stages
881 :build-deffiles
882 (elinstall-remove-empty-segs build-deffiles)
883 :run-tests
884 run-tests
885 :byte-compile
886 byte-compile
887 :arrange-preloads
888 arrange-preloads)))
889 ;;;_ . Finding actions
890 ;;;_ , Utility
891 ;;;_ . elinstall-dir-has-info
893 ;;$$IMPROVE ME - Can this test be made more precise?
894 (defun elinstall-dir-has-info (dir)
895 "Return non-nil if DIR has info files in it.
896 DIR should be an absolute path."
898 (string-match "/info/" dir)
899 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
900 ;;;_ . elinstall-directory-files
901 (defun elinstall-directory-files (dirname)
902 "Return a list of files in directory DIRNAME, minus certain files.
903 The following files are omitted:
904 * Dot files
905 * Files that match an entry in `block-in-subtree'.
906 Filenames are relative."
907 (declare (special
908 def-file block-in-dir block-in-subtree))
909 (let*
911 ;;Relative filenames of this directory's files.
912 (all-files
913 ;; Don't include dot files. If user really wants to
914 ;;explore one he can use (dir ".NAME") or (file ".NAME")
915 (directory-files dirname nil "[^\\.]"))
916 ;;We know our def-file isn't really source so remove it.
917 ;;We'd have removed it anyways after seeing file local vars.
918 (all-files
919 (remove def-file all-files))
921 (all-files
922 (delq nil
923 (mapcar
924 #'(lambda (filename)
926 (and
927 block-in-subtree
928 (some
929 #'(lambda (blocked)
930 (string-match blocked filename))
931 block-in-subtree))
933 filename))
934 all-files))))
935 all-files))
938 ;;;_ , Workers
939 ;;;_ . List of special variables used in this section
940 ;;load-path-element - The relevant element of load-path
941 ;;def-file - The file the autoload definitions etc will go into.
942 ;;add-to-load-path-p - Controls whether to add to load-path.
943 ;;recurse-dirs-p - Whether to recurse into subdirectories.
945 ;;block-in-dir - (NOT IMPLEMENTED) List of filenames to reject. They
946 ;;may include wildcards. They apply wrt the original directory.
948 ;;block-in-subtree - (NOT IMPLEMENTED) List of filenames to reject.
949 ;;They may include wildcards. They apply wrt any directory in the
950 ;;tree. Specifically, in the spec tree, which may differ from the
951 ;;file subtree.
952 ;;byte-compile - whether to byte-compile at all, t by default. If it's
953 ;;`ask', prompt the user unless recompiling an out-of-date file.
954 ;;make-autoloads - boolean whether to make autoloads, t by default.
955 ;;setup-preloads - boolean whether to set up preloads, t by default.
956 (defconst elinstall-find-actions-control-vars
957 '(add-to-load-path-p recurse-dirs-p byte-compile
958 make-autoloads setup-preloads)
959 "Control special variables that the find-actions tree recognizes" )
960 ;;;_ . elinstall-actions-for-source-file
961 (defun elinstall-actions-for-source-file (filename dir)
962 "Return a list of actions to do for FILENAME in DIR.
963 Special variables are as noted in \"List of special variables\"."
964 (declare (special
965 load-path-element def-file byte-compile))
966 (let
967 ((full-path
968 (expand-file-name filename dir)))
969 (when
970 (and
971 (file-readable-p full-path)
972 (not (auto-save-file-name-p full-path)))
973 ;;$$IMPROVE ME create and use relevant control variables.
974 (let*
976 (visited (get-file-buffer full-path))
977 (buf
978 (or
979 visited
980 ;;Visit the file cheaply.
981 ;;hack-local-variables can give errors.
982 (ignore-errors (autoload-find-file full-path))))
983 (def-file
985 (ignore-errors
986 (with-current-buffer buf
987 (if (local-variable-p 'generated-autoload-file)
988 (elinstall-expand-deffile-name
989 generated-autoload-file)
990 nil)))
991 def-file))
992 ;;Figure out whether to run some actions, by file local vars.
993 (autoloads-p
994 (ignore-errors
995 (with-current-buffer buf
996 (not no-update-autoloads))))
997 (do-compile-p
998 (and
999 byte-compile
1000 (featurep 'byte-compile)
1001 (string-match emacs-lisp-file-regexp filename)
1002 (ignore-errors
1003 (with-current-buffer buf
1004 (not no-byte-compile)))
1005 (let*
1006 ((dest (byte-compile-dest-file full-path))
1007 (yes-1
1008 (cond
1009 ((file-exists-p dest)
1010 (or (eq byte-compile 'always)
1011 (file-newer-than-file-p full-path dest)))
1012 ((memq byte-compile '(t update ask))
1013 t))))
1014 (if (eq byte-compile 'ask)
1015 (and
1016 yes-1
1017 (y-or-n-p
1018 (concat "Compile " filename "? ")))
1019 yes-1)))))
1021 (prog1
1022 (list
1023 (if do-compile-p
1024 `(byte-compile ,full-path)
1025 nil)
1026 (if autoloads-p
1027 (elinstall-make-autoload-action
1028 buf def-file load-path-element full-path)
1029 nil))
1030 (unless visited (kill-buffer-if-not-modified buf)))))))
1031 ;;;_ . elinstall-actions-for-dir
1032 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
1033 "Make actions for DIR.
1034 Recurse just if RECURSE-DIRS-P"
1035 (declare (special
1036 load-path-element def-file add-to-load-path-p))
1037 ;;This does not treat symlinks specially. $$IMPROVE ME it could
1038 ;;treat/not treat them conditional on control variables.
1039 (let*
1041 (files (elinstall-directory-files dirname))
1043 ;;Relative filenames of elisp source
1044 (elisp-source-files
1045 (delq nil
1046 (mapcar
1047 #'(lambda (filename)
1049 (string-match elinstall-elisp-regexp filename)
1050 filename
1051 nil))
1052 files)))
1054 ;;Absolute filenames of subdirectories.
1055 (sub-dirs
1056 (if recurse-dirs-p
1057 (delq nil
1058 (mapcar
1059 #'(lambda (filename)
1060 (let
1061 ((fn (expand-file-name filename dirname)))
1063 (file-directory-p fn)
1065 nil)))
1066 files))
1067 '()))
1069 (load-path-here-p
1070 (and
1071 elisp-source-files ;;If list is not empty.
1072 add-to-load-path-p))
1073 (load-path-element
1074 (if load-path-here-p
1075 dirname
1076 load-path-element)))
1078 (append
1079 ;;Sometimes arrange to add this directory to load-path.
1080 (if load-path-here-p
1081 `((add-to-load-path
1082 ,def-file
1083 ,load-path-element))
1084 '())
1086 ;;$$IMPROVE ME - be controlled by a control variable.
1087 ;;Sometimes add this directory to info path.
1089 (elinstall-dir-has-info dirname)
1090 `((add-to-info-path
1091 ,def-file
1092 "."))
1093 '())
1095 (apply #'nconc
1096 (mapcar
1097 #'(lambda (filename)
1098 (elinstall-actions-for-source-file
1099 filename
1100 dirname))
1101 elisp-source-files))
1103 (if recurse-dirs-p
1104 (apply #'nconc
1105 (mapcar
1106 #'(lambda (filename)
1107 (elinstall-find-actions-by-spec-x
1109 (expand-file-name
1110 filename
1111 dirname)))
1112 sub-dirs))
1113 '()))))
1115 ;;;_ . elinstall-find-actions-by-spec-x
1117 (defun elinstall-find-actions-by-spec-x (spec dir)
1118 "Return a list of actions to do, controlled by SPEC."
1119 (declare (special
1120 load-path-element def-file add-to-load-path-p
1121 recurse-dirs-p block-in-dir block-in-subtree))
1123 (if (consp spec)
1124 (case (car spec)
1125 (all
1126 ;;(all . SPEC*)
1127 (apply #'nconc
1128 (mapcar
1129 #'(lambda (sub-spec)
1130 (elinstall-find-actions-by-spec-x
1131 sub-spec
1132 dir))
1133 (cdr spec))))
1134 (block-in-subtree
1135 ;;(block-in-subtree (* FN) SPEC)
1136 (let
1137 ((block-in-subtree (second spec)))
1138 (elinstall-find-actions-by-spec-x
1139 (third spec)
1140 dir)))
1142 ;;Rather than trying to bind all control variables each time
1143 ;;thru, we use `set' and `unwind-protect'.
1144 (control
1145 ;;control TYPE DISPOSITION SPEC
1146 (let
1147 ((key (second spec))
1148 old-value)
1149 (if (memq key elinstall-find-actions-control-vars)
1150 (unwind-protect
1151 (progn
1152 (setq old-value (symbol-value key))
1153 (set key (third spec))
1154 (elinstall-find-actions-by-spec-x
1155 (fourth spec)
1156 dir))
1157 (set key old-value))
1158 (error "Unrecognized control variable %s" key))))
1161 (def-file
1162 ;;(def-file FN ARGS SPEC)
1163 (let
1164 ((def-file
1165 (expand-file-name
1166 (second spec)
1167 dir))
1168 (for-preload (third spec)))
1169 (assert (listp for-preload))
1170 (append
1171 (list
1173 (and for-preload (car for-preload))
1174 `(preload-file
1175 ,(car for-preload)
1176 ,def-file
1177 ,@(cdr for-preload))
1178 '()))
1180 (elinstall-find-actions-by-spec-x
1181 (fourth spec) dir))))
1183 (dir
1184 ;;(dir FN)
1185 (elinstall-actions-for-dir
1186 (expand-file-name
1187 (second spec)
1188 dir)
1189 nil))
1191 (file
1192 ;;(file FN)
1193 (elinstall-actions-for-source-file
1194 (second spec) dir))
1197 ;;(in FN SPEC)
1198 (let
1199 ((new-dir
1200 (expand-file-name
1201 (second spec)
1202 dir)))
1204 (elinstall-find-actions-by-spec-x
1205 (third spec)
1206 new-dir)))
1208 (load-path
1209 ;;(load-path SPEC)
1210 (append
1211 `((add-to-load-path ,def-file ,dir))
1212 (let
1213 ((load-path-element dir)
1214 (add-to-load-path-p nil))
1215 (elinstall-find-actions-by-spec-x
1216 (second spec)
1217 dir))))
1219 (matching
1220 ;;(matching PATTERN SPEC)
1221 (apply #'nconc
1222 (mapcar
1223 #'(lambda (dir)
1224 ;;$$PUNT Assume that `block-in-subtree' and
1225 ;;`block-in-dir' aren't matched.
1226 (elinstall-find-actions-by-spec-x
1227 (third spec)
1228 dir))
1229 (directory-files
1230 dir t (second spec)))))
1231 (preload
1232 ;;(preload FN SYM &rest ARGS)
1233 (let
1234 ((key (third spec)))
1236 (and (symbolp key) (symbol-value key))
1237 `((preload-file
1239 ,(expand-file-name (second spec) dir)
1240 ,@(nthcdr 3 spec)))
1241 '()))))
1243 ;;Single symbols
1244 (case spec
1245 (dir
1246 (elinstall-actions-for-dir dir nil))
1247 ((t)
1248 (elinstall-actions-for-dir dir t)))))
1250 ;;;_ . elinstall-find-actions-by-spec
1251 (defun elinstall-find-actions-by-spec
1252 (spec load-path-element dir def-file redo-old)
1253 "Find the list of actions to do according to SPEC."
1255 (let
1257 (def-file-time (elinstall-file-mod-time def-file))
1258 (add-to-load-path-p t)
1259 (recurse-dirs-p t)
1260 (byte-compile t)
1261 (make-autoloads t)
1262 (setup-preloads t)
1263 (block-in-dir '())
1264 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1266 (declare (special
1267 load-path-element def-file add-to-load-path-p
1268 recurse-dirs-p byte-compile
1269 block-in-dir block-in-subtree))
1271 (elinstall-find-actions-by-spec-x spec dir)))
1273 ;;;_ . high-level work
1274 ;;;_ , elinstall-get-relevant-load-path
1275 (defun elinstall-get-relevant-load-path (actions)
1277 (delq nil
1278 (mapcar
1279 #'(lambda (act)
1280 (case (car act)
1281 (add-to-load-path
1282 (second act))
1283 (t nil)))
1284 actions)))
1285 ;;;_ , elinstall-get-deffile-list
1286 (defun elinstall-get-deffile-list (stages)
1287 "Get a list of deffile names"
1289 (mapcar
1290 #'car
1291 (elinstall-stages->build-deffiles stages)))
1292 ;;;_ , elinstall-x
1293 (defun elinstall-x (dir spec &optional force)
1294 "High-level worker function to install elisp files."
1295 (let*
1297 ;;This is just the default deffile, spec can override it.
1298 (def-file
1299 (elinstall-expand-deffile-name nil))
1300 (actions
1301 (elinstall-find-actions-by-spec
1302 spec
1305 def-file
1306 (eq force 'redo-old)))
1307 (stages (elinstall-segregate-actions actions))
1308 (use-load-path
1309 (elinstall-get-relevant-load-path
1310 actions)))
1312 (elinstall-stage-update-deffiles
1313 (elinstall-stages->build-deffiles stages)
1314 force
1315 use-load-path)
1316 (elinstall-stage-arrange-preloads
1317 (elinstall-stages->arrange-preloads stages)
1318 (elinstall-get-deffile-list stages)
1319 force)
1320 (elinstall-stage-byte-compile
1321 (elinstall-stages->byte-compile stages))
1323 ;;;_ , Entry points
1324 ;;;_ . elinstall
1325 ;;;###autoload
1326 (defun elinstall (project-name path spec &optional force version-string)
1327 "Install elisp files.
1328 They need not be a formal package.
1330 Parameters:
1332 PROJECT-NAME - the name of the project
1334 PATH - Path to the project.
1335 Suggestion: Use (elinstall-directory-true-name) to get the real
1336 current directoery name even from loaded files.
1338 SPEC - a spec for the autoloads etc to make. It can be as simple as
1339 \(dir \"\.\") for installing one directory.
1341 If FORCE is t, install a package even if it has already been
1342 installed. If it's a list, it's treated as a list of
1343 installation restraints. User customizations override this
1344 argument."
1346 ;;$$IMPROVE ME - override these with user spec if there's one given
1347 ;;for this package.
1348 (let*
1349 ((restraints
1350 (if (eq force t)
1351 '((install 'always))
1352 force)))
1353 (when ;;(elinstall-proceed-p 'install restraints)
1355 ;;$$ENCAP ME
1356 (let*
1357 ((cell (assq 'install restraints)))
1358 (and cell (eq (second cell) 'always)))
1359 (not (elinstall-already-installed project-name))
1360 (yes-or-no-p (format "Re-install %s? " project-name)))
1361 (elinstall-x
1362 path
1363 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1364 force)
1365 (elinstall-record-installed project-name version-string))))
1369 ;;;_ . elinstall-update-directory-autoloads
1371 ;;;###autoload
1372 (defun elinstall-update-directory-autoloads (dir)
1373 "Update autoloads for directory DIR"
1375 (interactive "DUpdate autoloads for all elisp files from directory: ")
1376 (elinstall-x
1378 `(control byte-compile nil
1379 (dir "."))))
1381 ;;;_ . elinstall-update-directory
1382 ;;;###autoload
1383 (defun elinstall-update-directory (dir)
1384 "Update autoloads for directory DIR"
1386 (interactive "DInstall all elisp files from directory: ")
1387 (elinstall-x
1389 '(dir ".")))
1391 ;;;_ . elinstall-update-file-autoloads
1392 ;;;###autoload
1393 (defun elinstall-update-file-autoloads (file)
1394 "Update autoloads for elisp file FILE"
1396 (interactive "fUpdate autoloads for elisp file: ")
1397 (elinstall-x
1398 (file-name-directory file)
1399 `(control byte-compile nil
1400 (file ,(file-name-nondirectory file)))))
1402 ;;;_ . elinstall-update-file
1403 ;;;###autoload
1404 (defun elinstall-update-file (file)
1405 "Install elisp file FILE"
1407 (interactive "fInstall elisp file: ")
1408 (elinstall-x
1409 (file-name-directory file)
1410 `(file ,(file-name-nondirectory file))))
1412 ;;;_. Footers
1413 ;;;_ , Provides
1415 (provide 'elinstall)
1417 ;;;_ * Local emacs vars.
1418 ;;;_ + Local variables:
1419 ;;;_ + mode: allout
1420 ;;;_ + End:
1422 ;;;_ , End
1423 ;;; elinstall.el ends here