Split the file and dir entry points: just autoloads / full.
[elinstall.git] / elinstall.el
blob0a90840e44910e17b1b94e53bf095a9c97dd9780
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.
835 (defconst elinstall-find-actions-control-vars
836 '(add-to-load-path-p recurse-dirs-p compile-p force-recompile-p)
837 "Control special variables that the find-actions tree recognizes" )
838 ;;;_ . elinstall-actions-for-source-file
839 (defun elinstall-actions-for-source-file (filename dir)
840 "Return a list of actions to do for FILENAME in DIR.
841 Special variables are as noted in \"List of special variables\"."
842 (declare (special
843 load-path-element def-file compile-p force-recompile-p))
844 (let
845 ((full-path
846 (expand-file-name filename dir)))
847 (when
848 (and
849 (file-readable-p full-path)
850 (not (auto-save-file-name-p full-path)))
851 ;;$$IMPROVE ME create and use relevant control variables.
852 (let*
854 (visited (get-file-buffer full-path))
855 (buf
856 (or
857 visited
858 ;;Visit the file cheaply.
859 ;;hack-local-variables can give errors.
860 (ignore-errors (autoload-find-file full-path))))
861 (def-file
863 (ignore-errors
864 (with-current-buffer buf
865 (if (local-variable-p 'generated-autoload-file)
866 (elinstall-expand-deffile-name
867 generated-autoload-file)
868 nil)))
869 def-file))
870 ;;Figure out whether to run some actions, by file local vars.
871 (autoloads-p
872 (ignore-errors
873 (with-current-buffer buf
874 (not no-update-autoloads))))
875 (do-compile-p
876 (and
877 (featurep 'byte-compile)
878 (string-match emacs-lisp-file-regexp filename)
879 (ignore-errors
880 (with-current-buffer buf
881 (not no-byte-compile)))
882 (let
883 ((dest (byte-compile-dest-file full-path)))
884 (if (file-exists-p dest)
885 ;; File was already compiled.
886 (or force-recompile-p
887 (file-newer-than-file-p full-path dest))
888 (or compile-p
889 (y-or-n-p (concat "Compile " filename "? "))))))))
891 (prog1
892 (list
893 (if do-compile-p
894 `(byte-compile ,full-path)
895 nil)
896 (if autoloads-p
897 (elinstall-make-autoload-action
898 buf def-file load-path-element full-path)
899 nil))
900 (unless visited (kill-buffer-if-not-modified buf)))))))
901 ;;;_ . elinstall-actions-for-dir
902 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
903 "Make actions for DIR.
904 Recurse just if RECURSE-DIRS-P"
905 (declare (special
906 load-path-element def-file add-to-load-path-p))
907 ;;This does not treat symlinks specially. $$IMPROVE ME it could
908 ;;treat/not treat them conditional on control variables.
909 (let*
911 ;;Relative filenames of the source files. We know our
912 ;;loaddefs.el isn't really source so remove it. We'd have
913 ;;removed it anyways after seeing file local vars.
915 (elisp-source-files
916 (remove def-file
917 (directory-files
918 dirname
919 nil
920 elinstall-elisp-regexp)))
921 ;;Absolute filenames of subdirectories.
922 ;;Don't accept any directories beginning with dot. If user
923 ;;really wants to explore one he can use `(dir ".NAME")'.
924 (sub-dirs
925 (if recurse-dirs-p
926 (delq nil
927 (mapcar
928 #'(lambda (filename)
930 (file-directory-p filename)
931 filename
932 nil))
933 (directory-files
934 dirname t
935 "[^\\.]")))
936 '()))
938 (load-path-here
939 (and
940 elisp-source-files ;;If list is not empty.
941 add-to-load-path-p))
942 (load-path-element
943 (if load-path-here
944 dirname
945 load-path-element)))
947 (append
948 ;;Sometimes arrange to add this directory to load-path.
949 (if load-path-here
950 `((add-to-load-path
951 ,def-file
952 ,load-path-element))
953 '())
955 ;;$$IMPROVE ME - be controlled by a control variable.
956 ;;Sometimes add this directory to info path.
958 (elinstall-dir-has-info dirname)
959 `((add-to-info-path
960 ,def-file
961 "."))
962 '())
964 (apply #'nconc
965 (mapcar
966 #'(lambda (filename)
967 (elinstall-actions-for-source-file
968 filename
969 dirname))
970 elisp-source-files))
972 (if recurse-dirs-p
973 (apply #'nconc
974 (mapcar
975 #'(lambda (filename)
976 (elinstall-find-actions-by-spec-x
978 (expand-file-name
979 filename
980 dirname)))
981 sub-dirs))
982 '()))))
984 ;;;_ . elinstall-find-actions-by-spec-x
986 (defun elinstall-find-actions-by-spec-x (spec dir)
987 "Return a list of actions to do, controlled by SPEC."
988 (declare (special
989 load-path-element def-file add-to-load-path-p
990 recurse-dirs-p))
992 (if (consp spec)
993 (case (car spec)
995 ;;(in FN SPEC)
996 (let
997 ((new-dir
998 (expand-file-name
999 (second spec)
1000 dir)))
1002 (elinstall-find-actions-by-spec-x
1003 (third spec)
1004 new-dir)))
1006 (all
1007 ;;(all . SPEC*)
1008 (apply #'nconc
1009 (mapcar
1010 #'(lambda (sub-spec)
1011 (elinstall-find-actions-by-spec-x
1012 sub-spec
1013 dir))
1014 (cdr spec))))
1015 (matching
1016 ;;(matching PATTERN SPEC)
1017 (apply #'nconc
1018 (mapcar
1019 #'(lambda (dir)
1020 (elinstall-find-actions-by-spec-x
1021 (third spec)
1022 dir))
1023 (directory-files
1024 dir t (second spec))))
1026 (file
1027 ;;(file FN)
1028 (elinstall-actions-for-source-file
1029 (second spec) dir))
1031 ;;Rather than trying to bind all control variables each time
1032 ;;thru, we use `set' and `unwind-protect'.
1033 (control
1034 ;;control TYPE DISPOSITION SPEC
1035 (let
1036 ((key (second spec))
1037 old-value)
1038 (if (memq key elinstall-find-actions-control-vars)
1039 (unwind-protect
1040 (progn
1041 (set old-value (symbol-value key))
1042 (set key (third spec))
1043 (elinstall-find-actions-by-spec-x
1044 (second spec)
1045 dir))
1046 (set key old-value))
1047 (error "Unrecognized control variable %s" key))))
1050 (dir
1051 ;;(dir FN)
1052 (elinstall-actions-for-dir
1053 (expand-file-name
1054 (second spec)
1055 dir)
1056 nil))
1059 (load-path
1060 ;;(load-path SPEC)
1061 (append
1062 `((add-to-load-path ,def-file ,dir))
1063 (let
1064 ((load-path-element dir))
1065 (elinstall-find-actions-by-spec-x
1066 (second spec)
1067 dir))))
1070 (def-file
1071 ;;(def-file FN ARGS SPEC)
1072 (let
1073 ((def-file
1074 (expand-file-name
1075 (second spec)
1076 dir))
1077 (for-preload (third spec)))
1078 (assert (listp for-preload))
1079 (append
1080 (list
1082 (and for-preload (car for-preload))
1083 `(preload-file
1084 ,(car for-preload)
1085 ,def-file
1086 ,@(cdr for-preload))
1087 '()))
1089 (elinstall-find-actions-by-spec-x
1090 (fourth spec) dir)))))
1093 ;;Single symbols
1094 (case spec
1095 (dir
1096 (elinstall-actions-for-dir dir nil))
1097 ((t)
1098 (elinstall-actions-for-dir dir t)))))
1100 ;;;_ . elinstall-find-actions-by-spec
1101 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1104 (let
1105 ((load-path-element load-path-element)
1106 (def-file def-file)
1107 (add-to-load-path-p t)
1108 (recurse-dirs-p t)
1109 (force-recompile-p nil)
1110 (compile-p t))
1111 (declare (special
1112 load-path-element def-file add-to-load-path-p
1113 recurse-dirs-p force-recompile-p compile-p))
1115 (elinstall-find-actions-by-spec-x
1116 spec dir)))
1118 ;;;_ . high-level work
1119 ;;;_ , elinstall-get-relevant-load-path
1120 (defun elinstall-get-relevant-load-path (actions)
1122 (delq nil
1123 (mapcar
1124 #'(lambda (act)
1125 (case (car act)
1126 (add-to-load-path
1127 (second act))
1128 (t nil)))
1129 actions)))
1130 ;;;_ , elinstall-get-deffile-list
1131 (defun elinstall-get-deffile-list (stages)
1132 "Get a list of deffile names"
1134 (mapcar
1135 #'car
1136 (elinstall-stages->build-deffiles stages)))
1137 ;;;_ , elinstall-x
1138 (defun elinstall-x (dir spec &optional force)
1139 "High-level worker function to install elisp files."
1140 (let*
1142 ;;This is just the default deffile, spec can override it.
1143 (def-file
1144 (elinstall-expand-deffile-name nil))
1145 (actions
1146 (elinstall-find-actions-by-spec
1147 spec
1150 def-file))
1151 (stages (elinstall-segregate-actions actions))
1152 (use-load-path
1153 (elinstall-get-relevant-load-path
1154 actions)))
1156 (elinstall-stage-update-deffiles
1157 (elinstall-stages->build-deffiles stages)
1158 force
1159 use-load-path)
1160 (elinstall-stage-arrange-preloads
1161 (elinstall-stages->arrange-preloads stages)
1162 (elinstall-get-deffile-list stages)
1163 force)
1164 (elinstall-stage-byte-compile
1165 (elinstall-stages->byte-compile stages))
1167 ;;;_ , Entry points
1168 ;;;_ . elinstall
1169 ;;;###autoload
1170 (defun elinstall (project-name path spec &optional force)
1171 "Install elisp files.
1172 They need not be a formal package.
1174 Parameters:
1176 PROJECT-NAME - the name of the project
1178 PATH - Path to the project.
1179 Suggestion: (elinstall-directory-true-name)
1181 SPEC - a spec for the autoloads etc to make. It can be as simple as
1182 \(dir \"\.\") for installing one directory.
1184 If FORCE is t, install a package even if it has already been
1185 installed. Other non-nil cases of FORCE are reserved for future
1186 development."
1188 (when
1189 (and
1190 (or
1191 force
1192 (not (elinstall-already-installed project-name)))
1193 (yes-or-no-p (format "Re-install %s? " project-name)))
1194 (elinstall-x
1195 path
1196 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1197 force)
1198 (elinstall-record-installed project-name)))
1202 ;;;_ . elinstall-update-directory-autoloads
1204 ;;The control variables and values of `force' that would stop other
1205 ;;actions don't exist yet. Similarly for
1206 ;;`elinstall-update-file-autoloads'.
1207 ;;;###autoload
1208 (defun elinstall-update-directory-autoloads (dir)
1209 "Update autoloads for directory DIR"
1211 (interactive "DUpdate autoloads for all elisp files from directory: ")
1212 (elinstall-x
1214 `(control compile-p nil
1215 (dir "."))))
1217 ;;;_ . elinstall-update-directory
1218 ;;;###autoload
1219 (defun elinstall-update-directory (dir)
1220 "Update autoloads for directory DIR"
1222 (interactive "DInstall all elisp files from directory: ")
1223 (elinstall-x
1225 `(def-file ,def-file-name (nil)
1226 (dir "."))))
1228 ;;;_ . elinstall-update-file-autoloads
1229 ;;;###autoload
1230 (defun elinstall-update-file-autoloads (file)
1231 "Update autoloads for elisp file FILE"
1233 (interactive "fUpdate autoloads for elisp file: ")
1234 (elinstall-x
1235 file
1236 `(control compile-p nil
1237 (file ,file))))
1239 ;;;_ . elinstall-update-file
1240 ;;;###autoload
1241 (defun elinstall-update-file (file)
1242 "Install elisp file FILE"
1244 (interactive "fInstall elisp file: ")
1245 (elinstall-x
1246 file
1247 `(file ,file)))
1249 ;;;_. Footers
1250 ;;;_ , Provides
1252 (provide 'elinstall)
1254 ;;;_ * Local emacs vars.
1255 ;;;_ + Local variables:
1256 ;;;_ + mode: allout
1257 ;;;_ + End:
1259 ;;;_ , End
1260 ;;; elinstall.el ends here