Removed stray variable setting.
[elinstall.git] / elinstall.el
blob1e0e8bfdad58be630bc5ea0c939ed90ceead4dfd
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-already-installed
72 (with-no-warnings
73 (defcustom elinstall-already-installed
74 '()
75 "(AUTOMATIC) Things that have already been installed.
76 This exists for recording what has been installed.
78 Though it's saved as customizable, user interaction is not
79 contemplated." ))
80 ;;;_ , Types
81 ;;;_ . elinstall-stages
82 (defstruct (elinstall-stages
83 (:constructor elinstall-make-stages)
84 (:conc-name elinstall-stages->)
85 (:copier nil))
86 "The elinstall stages"
87 build-deffiles
88 run-tests
89 byte-compile
90 arrange-preloads)
91 ;;;_ , Data
92 ;;;_ . Regular expressions
93 ;;;_ , elinstall-elisp-regexp
94 (defconst elinstall-elisp-regexp
95 (let ((tmp nil))
96 (dolist
97 (suf (get-load-suffixes))
98 (unless (string-match "\\.elc" suf) (push suf tmp)))
99 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
100 "Regular expression that matches elisp files" )
101 ;;;_ , Utilities
102 ;;;_ . elinstall-file-mod-time
103 (defsubst elinstall-file-mod-time (file)
104 "Return the modification time of FILE"
105 (nth 5 (file-attributes file)))
107 ;;;_ . elinstall-directory-true-name
108 (defun elinstall-directory-true-name ()
109 "Get the true name of the directory the calling code lives in.
110 CAUTION: This is sensitive to where it's called. That's the point of it."
111 (file-name-directory
112 (if load-file-name
113 (file-truename load-file-name)
114 (file-truename buffer-file-name))))
115 ;;;_ . Checking installedness
116 ;;;_ , elinstall-get-installation-record
117 (defun elinstall-get-installation-record (project-name)
118 "Return the installation record for PROJECT-NAME."
120 (assoc project-name elinstall-already-installed))
122 ;;;_ , elinstall-already-installed
123 (defun elinstall-already-installed (project-name)
124 "Return non-nil if PROJECT-NAME has been installed."
125 (elinstall-get-installation-record project-name))
127 ;;;_ , elinstall-record-installed
128 (defun elinstall-record-installed (project-name &optional version)
129 "Record that PROJECT-NAME has been installed."
130 (let
131 ((new-item
132 (list
133 project-name
134 (or version "0")
135 (current-time)
136 'installed))
137 (old-item
138 (elinstall-get-installation-record project-name))
139 (print-length nil)
140 (print-level nil))
141 (when old-item
142 (setq elinstall-already-installed
143 (delete old-item elinstall-already-installed)))
144 (push new-item elinstall-already-installed)
145 (customize-save-variable
146 'elinstall-already-installed
147 elinstall-already-installed
148 "Set by elinstall-record-installed")))
149 ;;;_ . Finding deffiles
150 ;;;_ , elinstall-expand-deffile-name
151 (defun elinstall-expand-deffile-name (deffile)
152 "Expand DEFFILE autoload.el's way."
154 (expand-file-name (or deffile "loaddefs.el")
155 (expand-file-name "lisp"
156 source-directory)))
158 ;;;_ , Work
159 ;;;_ . Doing actions
161 ;;;_ , Doing autoload actions (adapted from autoload.el)
162 ;;;_ . Utilities about the action list
163 ;;;_ , elinstall-remove-autogen-action
164 (defun elinstall-remove-autogen-action (file actions)
165 "Return ACTIONS minus any add-file-autoloads on FILE removed."
167 (delq nil
168 (mapcar
169 #'(lambda (act)
170 (case (car act)
171 (add-file-autoloads
172 (if (equal file (third act))
174 act))
175 (t act)))
176 actions)))
177 ;;;_ , elinstall-get-autogen-action
178 (defun elinstall-get-autogen-action (file actions)
180 (let
181 ((the-act))
182 (dolist (act actions)
183 (case (car act)
184 (add-file-autoloads
185 (when (equal file (third act))
186 (setq the-act act)))))
187 the-act))
188 ;;;_ . About printing to autoload file
189 ;;;_ , elinstall-insert-section-header
190 (defun elinstall-insert-section-header (outbuf form)
191 "Insert the section-header line,
192 which lists the file name and which functions are in it, etc."
193 (insert generate-autoload-section-header)
194 (prin1 form outbuf)
195 (terpri outbuf)
196 ;; Break that line at spaces, to avoid very long lines.
197 ;; Make each sub-line into a comment.
198 (with-current-buffer outbuf
199 (save-excursion
200 (forward-line -1)
201 (while (not (eolp))
202 (move-to-column 64)
203 (skip-chars-forward "^ \n")
204 (or (eolp)
205 (insert "\n" generate-autoload-section-continuation))))))
207 ;;;_ , elinstall-insert-autoload-section
208 (defun elinstall-insert-autoload-section (text form &optional comment-string)
209 "Insert TEXT into current buffer as an autoload section"
211 (let* (
212 (print-length nil)
213 (print-level nil)
214 ;; This does something in Lucid Emacs.
215 (print-readably t)
216 (float-output-format nil))
218 (elinstall-insert-section-header (current-buffer) form)
219 (when comment-string
220 (insert ";;; " comment-string "\n"))
221 (insert text)
222 (insert generate-autoload-section-trailer)))
224 ;;;_ . Making autoloads
225 ;;;_ , elinstall-make-autoload-action
226 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
227 "Return the autoloads for current buffer as a string"
229 ;;We put all the text into a temp buffer, then get that buffer's
230 ;;string.
231 (let
232 ((outbuf
233 (generate-new-buffer " *temp*"))
234 (autoloads-done '())
235 (short-name
236 (file-name-nondirectory full-path))
238 (print-length nil)
239 (print-level nil)
240 ;; Apparently this does something in Lucid Emacs.
241 (print-readably t)
242 (float-output-format nil)
244 ;;load-name relative to a member of load-path.
245 (relative-name
246 (file-name-sans-extension
247 (file-relative-name
248 full-path
249 load-path-element))))
251 (with-current-buffer buf
252 (unwind-protect
253 (save-excursion
254 (save-restriction
255 (widen)
256 (goto-char (point-min))
257 (message "Finding autoloads for %s..." short-name)
258 (while (not (eobp))
259 (skip-chars-forward " \t\n\f")
260 (cond
261 ((looking-at (regexp-quote generate-autoload-cookie))
262 (search-forward generate-autoload-cookie)
263 (skip-chars-forward " \t")
264 (if (eolp)
265 ;; Read the next form and make an autoload.
266 (let* ((form (prog1 (read (current-buffer))
267 (or (bolp) (forward-line 1))))
268 (autoload
269 (make-autoload form relative-name)))
270 (if autoload
271 (push (nth 1 form) autoloads-done)
272 (setq autoload form))
273 (let ((autoload-print-form-outbuf outbuf))
274 (autoload-print-form autoload)))
276 ;; Copy the rest of the line to the output.
277 (princ (buffer-substring
278 (progn
279 ;; Back up over whitespace,
280 ;; to preserve it.
281 (skip-chars-backward " \f\t")
282 (if (= (char-after (1+ (point))) ? )
283 ;; Eat one space.
284 (forward-char 1))
285 (point))
286 (progn (forward-line 1) (point)))
287 outbuf)))
288 ((looking-at ";")
289 ;; Don't read the comment.
290 (forward-line 1))
292 (forward-sexp 1)
293 (forward-line 1))))
294 (message "Finding autoloads for %s...done" short-name))
296 ;;Return this action. The temp buffer's contents is
297 ;;our final string.
298 `(add-file-autoloads
299 ,def-file
300 ,relative-name
301 ,full-path
302 ,(with-current-buffer outbuf (buffer-string))
303 ,autoloads-done))
305 ;;This in unwind-protected
306 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
309 ;;;_ , elinstall-generate-file-autoloads
311 (defun elinstall-generate-file-autoloads
312 (relative-name full-name text autoloads-done)
313 "Insert at point a loaddefs autoload section for FILE.
314 Autoloads are generated for defuns and defmacros in FILE
315 marked by `generate-autoload-cookie' (which see).
316 If FILE is being visited in a buffer, the contents of the buffer
317 are used.
318 Return non-nil in the case where no autoloads were added at point.
320 FULL-NAME is the absolute name of the file.
321 RELATIVE-NAME is its name respective to some component of load-path."
322 (if (not (equal text ""))
323 ;; Insert the section-header line which lists the file name and
324 ;; which functions are in it, etc.
325 (elinstall-insert-autoload-section
326 text
327 (list 'autoloads
328 autoloads-done
329 relative-name
330 (autoload-trim-file-name full-name)
331 (elinstall-file-mod-time full-name))
332 (concat
333 "Generated autoloads from "
334 (autoload-trim-file-name full-name)))
337 ;;;_ , elinstall-deffile-insert-autoloads
338 (defun elinstall-deffile-insert-autoloads (file args)
339 "Update the autoloads for FILE in current buffer.
340 Return FILE if there was no autoload cookie in it, else nil.
342 Current buffer must be a loaddef-style file.
344 LOAD-NAME is the absolute name of the file.
345 RELATIVE-NAME is its name respective to some component of load-path."
346 (let (
347 (found nil)
348 (no-autoloads nil))
350 (save-excursion
351 (save-restriction
352 (widen)
353 (goto-char (point-min))
354 ;; Look for the section for FILE
355 (while (and (not found)
356 (search-forward generate-autoload-section-header nil t))
357 (let ((form (autoload-read-section-header)))
358 (cond
359 ((equal (nth 2 form) file)
360 ;; We found the section for this file.
361 (let ((begin (match-beginning 0)))
362 (progn
363 (search-forward generate-autoload-section-trailer)
364 (delete-region begin (point))
365 (setq found t))))
366 ((string< file (nth 2 form))
367 ;; We've come to a section alphabetically later than
368 ;; FILE. We assume the file is in order and so
369 ;; there must be no section for FILE. We will
370 ;; insert one before the section here.
371 (goto-char (match-beginning 0))
372 (setq found 'new)))))
373 (unless found
374 (progn
375 (setq found 'new)
376 ;; No later sections in the file. Put before the last page.
377 (goto-char (point-max))
378 (search-backward "\f" nil t)))
379 (setq no-autoloads
380 (apply #'elinstall-generate-file-autoloads
381 file args))))
383 (if no-autoloads file nil)))
384 ;;;_ . Arranging to add to info-path and load-path
385 ;;;_ , elinstall-generate-add-to-path
386 (defun elinstall-generate-add-to-path (path-element type)
387 "Insert code at point to add PATH-ELEMENT to a path.
388 If TYPE is:
389 * `add-to-load-path', add to load-path
390 * `add-to-info-path', add to Info-additional-directory-list
392 Current buffer must be a loaddef-style file."
393 (let ( (path-symbol
394 (case type
395 (add-to-load-path 'load-path)
396 (add-to-info-path 'Info-additional-directory-list)
397 (t (error "Type not recognized"))))
398 (description
399 (case type
400 (add-to-load-path "load-path")
401 (add-to-info-path "info-path")))
402 (autoloads-done '())
403 (print-length nil)
404 (print-level nil)
405 ;; This does something in Lucid Emacs.
406 (print-readably t)
407 (float-output-format nil))
409 (message "Generating %s additions..." description)
411 (elinstall-insert-autoload-section
412 (pp-to-string
413 `(add-to-list ',path-symbol
414 (expand-file-name
415 ,(file-relative-name path-element)
416 (if load-file-name
417 (file-name-directory
418 (file-truename load-file-name))))))
419 (list type (list path-element) nil nil nil)
420 nil)
421 (message "Generating %s additions...done" description)))
424 ;;;_ , elinstall-deffile-insert-add-to-path
425 (defun elinstall-deffile-insert-add-to-path (path-element type)
426 "Insert code in current buffer to add PATH-ELEMENT to a path.
427 If TYPE is:
428 * `add-to-load-path', add to load-path
429 * `add-to-info-path', add to Info-default-directory-list
431 Current buffer must be a loaddef-style file."
432 (let (
433 (found nil)
434 (no-autoloads nil))
436 (save-excursion
437 (save-restriction
438 (widen)
439 (goto-char (point-min))
440 ;; Look for the section for PATH-ELEMENT
441 (while (and (not found)
442 (search-forward generate-autoload-section-header nil t))
443 (let ((form (autoload-read-section-header)))
444 (cond
445 ((and
446 (equal (nth 0 form) type)
447 (member path-element (nth 1 form)))
449 ;; We found the section for this add.
450 (let ((begin (match-beginning 0)))
451 (progn
452 (search-forward generate-autoload-section-trailer)
453 (delete-region begin (point))
454 (setq found t)))))))
456 (unless found
457 (progn
458 (setq found 'new)
459 ;; No later sections in the file. Put before the last page.
460 (goto-char (point-max))
461 (search-backward "\f" nil t)))
463 (elinstall-generate-add-to-path path-element type)))
465 ;;This never belongs in the no-autoloads section.
466 nil))
467 ;;;_ . elinstall-deffile-insert
469 (defun elinstall-deffile-insert (action)
470 "Insert autoloads etc into current file according to ACTION.
471 The format of ACTION is described in the design docs.
473 Return filename if this action belongs in the no-autoload section."
475 (when action
476 (case (car action)
477 (add-file-autoloads
478 (elinstall-deffile-insert-autoloads
479 (third action)
480 (nthcdr 3 action)))
482 (add-to-load-path
483 (elinstall-deffile-insert-add-to-path
484 (third action)
485 'add-to-load-path)
486 nil)
488 (add-to-info-path
489 (elinstall-deffile-insert-add-to-path
490 (third action)
491 'add-to-info-path)
492 nil)
494 ((preload-file run-tests byte-compile)
495 (error "This case should not come here.")))))
497 ;;;_ . elinstall-prepare-deffile
498 (defun elinstall-prepare-deffile (deffile)
499 "Try to ensure that DEFFILE is available for receiving autoloads"
501 (autoload-ensure-default-file deffile)
502 (with-current-buffer (find-file-noselect deffile)
505 ;; We must read/write the file without any code conversion,
506 ;; but still decode EOLs.
507 (let ((coding-system-for-read 'raw-text))
509 ;; This is to make generated-autoload-file have Unix EOLs, so
510 ;; that it is portable to all platforms.
511 (setq buffer-file-coding-system 'raw-text-unix))
512 (or (> (buffer-size) 0)
513 (error "Autoloads file %s does not exist" buffer-file-name))
514 (or (file-writable-p buffer-file-name)
515 (error "Autoloads file %s is not writable"
516 buffer-file-name))))
518 ;;;_ . elinstall-update-deffile
520 ;;Adapted from autoload.el `update-directory-autoloads'.
522 (defun elinstall-update-deffile (target actions &optional
523 use-load-path force)
525 Update file TARGET with current autoloads as specified by ACTIONS.
526 Also remove any old definitions pointing to libraries that can no
527 longer be found.
529 ACTIONS must be a list of actions (See the format doc). Each one's
530 filename must be relative to some element of load-path.
532 USE-LOAD-PATH is a list to use as load-path. It should include
533 any new load-path that we are arranging to create. If it's not given,
534 load-path itself is used.
536 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
537 Other non-nil cases of FORCE are reserved for future development.
539 This uses `update-file-autoloads' (which see) to do its work.
540 In an interactive call, you must give one argument, the name
541 of a single directory."
542 (let
544 (use-load-path (or use-load-path load-path))
545 (this-time (current-time))
546 ;;files with no autoload cookies.
547 (no-autoloads nil))
549 (elinstall-prepare-deffile target)
550 (with-current-buffer
551 (find-file-noselect target)
552 (save-excursion
553 (setq actions
554 (elinstall-remove-autogen-action
555 (autoload-trim-file-name target)
556 actions))
558 (goto-char (point-min))
559 (while (search-forward generate-autoload-section-header nil t)
560 (let* ((form (autoload-read-section-header))
561 (file (nth 3 form)))
562 (cond ((and (consp file) (stringp (car file)))
563 ;; This is a list of files that have no
564 ;; autoload cookies.
565 ;; There shouldn't be more than one such entry.
566 ;; Remove the obsolete section.
567 (autoload-remove-section (match-beginning 0))
568 (let ((last-time (nth 4 form)))
569 (dolist (file file)
570 (let ((file-time (elinstall-file-mod-time file)))
571 (when (and file-time
572 (not (time-less-p last-time file-time)))
573 ;; file unchanged
574 (push file no-autoloads)
575 (setq actions
576 (elinstall-remove-autogen-action
577 file actions)))))))
578 ((not (stringp file)))
580 (let
581 ((file-path
582 (locate-library file nil use-load-path)))
583 (cond
584 ;;File doesn't exist, so remove its
585 ;;section.
586 ((not file-path)
587 (autoload-remove-section
588 (match-beginning 0)))
590 ;; File hasn't changed, so do nothing.
591 ((equal
592 (nth 4 form)
593 (elinstall-file-mod-time file-path))
594 nil)
596 (elinstall-deffile-insert
597 (elinstall-get-autogen-action
598 file actions))))
600 (setq actions
601 (elinstall-remove-autogen-action
602 file actions))))))))
604 ;; Remaining actions have no existing autoload sections yet.
605 (setq no-autoloads
606 (append no-autoloads
607 (delq nil (mapcar #'elinstall-deffile-insert actions))))
608 (when no-autoloads
609 ;; Sort them for better readability.
610 (setq no-autoloads (sort no-autoloads 'string<))
611 ;; Add the `no-autoloads' section.
612 (goto-char (point-max))
613 (search-backward "\f" nil t)
614 (elinstall-insert-autoload-section
616 (list 'autoloads nil nil no-autoloads this-time)))
617 (save-buffer))))
619 ;;;_ . elinstall-stage-update-deffiles
620 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
621 "Update any deffiles mentioned in SEGMENT-LIST.
622 FORCE and USE-LOAD-PATH have the same meaning as in
623 `elinstall-update-deffile'.
625 (mapcar
626 #'(lambda (segment)
627 (let*
628 ((deffile (car segment)))
629 (if (stringp deffile)
630 (elinstall-update-deffile deffile (cdr segment) force
631 use-load-path))))
632 segment-list))
634 ;;;_ , Doing actions to arrange preloads
635 ;;;_ . elinstall-symlink-on-emacs-start
636 (defun elinstall-symlink-on-emacs-start
637 (filename target-basename target-dir &optional priority force)
638 "Symlink to TARGET-BASENAME.el in TARGET-DIR
640 If PRIORITY is given, it will be used as the priority prefix,
641 otherwise elinstall-default-priority will be.
642 PRIORITY must be an integer or nil.
643 If FORCE is `t', do it regardless of timestamps etc.
644 Other non-nil cases of FORCE are reserved for future development."
645 (let*
647 (priority (or priority elinstall-default-priority))
648 (target-name-nodir
649 (format
650 "%d%s.el"
651 priority
652 target-basename))
653 (target
654 (expand-file-name target-name-nodir target-dir)))
657 (cond
658 ;;Path should already exist.
659 ((not
660 (file-exists-p target-dir))
661 (message "The target directory doesn't exist."))
662 ;;Target shouldn't already exist, but if force is given, let
663 ;;user override.
664 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
665 ;;do nothing even on force.
666 ((and
667 (file-exists-p target)
669 (not force)
670 (not
671 (yes-or-no-p
672 (format "Really overwrite %s? " target))))
673 (message "File %s already exists" target)))
676 (make-symbolic-link
677 filename
678 target
679 nil)))))
681 ;;;_ . elinstall-add-to-dot-emacs
682 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
683 "Add code to load FILENAME to .emacs.
684 FILENAME should not have an extension"
686 ;;Visit .emacs
687 (with-current-buffer (find-file-noselect dot-emacs-name)
688 (save-excursion
689 ;;add at end of file
690 (goto-char (point-max))
691 (insert "\n;;Added by elinstall")
692 (insert "\n;;Consider using my-site-start to manage .emacs\n")
693 (pp `(load ,filename) (current-buffer))
694 (save-buffer))))
697 ;;;_ . elinstall-arrange-preload
698 ;;;###autoload
699 (defun elinstall-arrange-preload (force filename basename &optional priority)
700 "Arrange for FILENAME to be loaded on emacs start.
701 FORCE has its usual meaning.
702 BASENAME and PRIORITY are used as arguments to
703 `elinstall-symlink-on-emacs-start'.
706 (let
707 ((preload-target elinstall-default-preload-target))
709 ;;Dispatch the possibilities.
710 (cond
711 ((eq preload-target 'dot-emacs)
712 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
713 ((stringp preload-target)
714 (elinstall-symlink-on-emacs-start
715 filename basename preload-target priority force))
716 ((null preload-target)
717 (message "Not arranging for preloads"))
719 (message "I don't recognize that")))))
720 ;;;_ . elinstall-stage-arrange-preloads
721 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
722 "Arrange any preloads mentioned in ACTIONS."
724 (mapcar
725 #'(lambda (act)
726 (case (car act)
727 (preload-file
728 (let*
729 ( (filename
730 (caddr act))
731 (proceed-p
732 (case (second act)
733 ((t) t)
734 ((nil) nil)
735 (if-used
736 (member filename deffiles-used)))))
738 (when proceed-p
739 (apply
740 #'elinstall-arrange-preload
741 force
742 (cddr act)))))
744 (error
745 "elinstall-stage-arrange-preloads: Action not
746 recognized."))) )
747 actions))
750 ;;;_ , Run tests
751 ;;;_ . elinstall-stage-run-tests
752 (defun elinstall-stage-run-tests (actions)
753 "Run any tests mentioned in ACTIONS."
755 (mapcar
756 #'(lambda (act)
757 (case (car act)
758 (run-tests
759 ;;$$WRITE ME - not a high priority right now.
760 nil)
762 (error
763 "elinstall-stage-run-tests: Action not
764 recognized."))) )
765 actions))
768 ;;;_ , Byte compile
769 ;;;_ . elinstall-stage-byte-compile
770 (defun elinstall-stage-byte-compile (actions)
771 "Do any byte-compilation mentioned in ACTIONS."
773 (mapcar
774 #'(lambda (act)
775 (case (car act)
776 ;;$$IMPROVE ME Understand flags to control second
777 ;;argument (whether to load file after
778 ;;compilation)
779 (byte-compile
780 (byte-compile-file (second act)))
782 (error
783 "elinstall-stage-byte-compile: Action not
784 recognized."))) )
785 actions))
786 ;;;_ . Segregating actions
787 ;;;_ , elinstall-remove-empty-segs
788 (defun elinstall-remove-empty-segs (segment-list)
789 "Return SEGMENT-LIST minus any segments that have no actions.
790 Intended only for the deffile stage data."
791 (delq nil
792 (mapcar
793 #'(lambda (segment)
794 (if (cdr segment)
795 segment
796 nil))
797 segment-list)))
799 ;;;_ , elinstall-segregate-actions
800 (defun elinstall-segregate-actions (actions)
801 "Return actions segregated by deffile.
803 Returns a list whose elements are each a cons of:
804 * deffile filename or nil
805 * A list of actions to be done for that deffile."
807 (let
809 (build-deffiles '())
810 (run-tests '())
811 (byte-compile '())
812 (arrange-preloads '()))
814 (dolist (act actions)
815 (when act
816 (case (car act)
817 ((add-file-autoloads
818 add-to-info-path
819 add-to-load-path)
820 (let*
821 ((deffile-name (second act))
822 (cell-already
823 (assoc deffile-name build-deffiles)))
824 (if cell-already
825 ;;There are already actions on this deffile.
826 ;;Splice this action in.
827 (setcdr cell-already
828 (cons act (cdr cell-already)))
829 ;;There are no actions on this deffile. Add a
830 ;;place for them and include this action.
831 (push (list deffile-name act) build-deffiles))))
832 (preload-file
833 (push act arrange-preloads))
834 (run-tests
835 (push act run-tests))
836 (byte-compile
837 (push act byte-compile)))))
839 (elinstall-make-stages
840 :build-deffiles
841 (elinstall-remove-empty-segs build-deffiles)
842 :run-tests
843 run-tests
844 :byte-compile
845 byte-compile
846 :arrange-preloads
847 arrange-preloads)))
848 ;;;_ . Finding actions
849 ;;;_ , Informational
850 ;;;_ . elinstall-dir-has-info
852 ;;$$IMPROVE ME - Can this test be made more precise?
853 (defun elinstall-dir-has-info (dir)
854 "Return non-nil if DIR has info files in it.
855 DIR should be an absolute path."
857 (string-match "/info/" dir)
858 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
860 ;;;_ , Workers
861 ;;;_ . List of special variables used in this section
862 ;;load-path-element - The relevant element of load-path
863 ;;def-file - The file the autoload definitions etc will go into.
864 ;;add-to-load-path-p - Controls whether to add to load-path.
865 ;;recurse-dirs-p - Whether to recurse into subdirectories.
866 (defconst elinstall-find-actions-control-vars
867 '(add-to-load-path-p recurse-dirs-p compile-p force-recompile-p)
868 "Control special variables that the find-actions tree recognizes" )
869 ;;;_ . elinstall-actions-for-source-file
870 (defun elinstall-actions-for-source-file (filename dir)
871 "Return a list of actions to do for FILENAME in DIR.
872 Special variables are as noted in \"List of special variables\"."
873 (declare (special
874 load-path-element def-file compile-p force-recompile-p))
875 (let
876 ((full-path
877 (expand-file-name filename dir)))
878 (when
879 (and
880 (file-readable-p full-path)
881 (not (auto-save-file-name-p full-path)))
882 ;;$$IMPROVE ME create and use relevant control variables.
883 (let*
885 (visited (get-file-buffer full-path))
886 (buf
887 (or
888 visited
889 ;;Visit the file cheaply.
890 ;;hack-local-variables can give errors.
891 (ignore-errors (autoload-find-file full-path))))
892 (def-file
894 (ignore-errors
895 (with-current-buffer buf
896 (if (local-variable-p 'generated-autoload-file)
897 (elinstall-expand-deffile-name
898 generated-autoload-file)
899 nil)))
900 def-file))
901 ;;Figure out whether to run some actions, by file local vars.
902 (autoloads-p
903 (ignore-errors
904 (with-current-buffer buf
905 (not no-update-autoloads))))
906 (do-compile-p
907 (and
908 (featurep 'byte-compile)
909 (string-match emacs-lisp-file-regexp filename)
910 (ignore-errors
911 (with-current-buffer buf
912 (not no-byte-compile)))
913 (let
914 ((dest (byte-compile-dest-file full-path)))
915 (if (file-exists-p dest)
916 ;; File was already compiled.
917 (or force-recompile-p
918 (file-newer-than-file-p full-path dest))
919 (or compile-p
920 (y-or-n-p (concat "Compile " filename "? "))))))))
922 (prog1
923 (list
924 (if do-compile-p
925 `(byte-compile ,full-path)
926 nil)
927 (if autoloads-p
928 (elinstall-make-autoload-action
929 buf def-file load-path-element full-path)
930 nil))
931 (unless visited (kill-buffer-if-not-modified buf)))))))
932 ;;;_ . elinstall-actions-for-dir
933 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
934 "Make actions for DIR.
935 Recurse just if RECURSE-DIRS-P"
936 (declare (special
937 load-path-element def-file add-to-load-path-p))
938 ;;This does not treat symlinks specially. $$IMPROVE ME it could
939 ;;treat/not treat them conditional on control variables.
940 (let*
942 ;;Relative filenames of the source files. We know our
943 ;;loaddefs.el isn't really source so remove it. We'd have
944 ;;removed it anyways after seeing file local vars.
946 (elisp-source-files
947 (remove def-file
948 (directory-files
949 dirname
950 nil
951 elinstall-elisp-regexp)))
952 ;;Absolute filenames of subdirectories.
953 ;;Don't accept any directories beginning with dot. If user
954 ;;really wants to explore one he can use `(dir ".NAME")'.
955 (sub-dirs
956 (if recurse-dirs-p
957 (delq nil
958 (mapcar
959 #'(lambda (filename)
961 (file-directory-p filename)
962 filename
963 nil))
964 (directory-files
965 dirname t
966 "[^\\.]")))
967 '()))
969 (load-path-here
970 (and
971 elisp-source-files ;;If list is not empty.
972 add-to-load-path-p))
973 (load-path-element
974 (if load-path-here
975 dirname
976 load-path-element)))
978 (append
979 ;;Sometimes arrange to add this directory to load-path.
980 (if load-path-here
981 `((add-to-load-path
982 ,def-file
983 ,load-path-element))
984 '())
986 ;;$$IMPROVE ME - be controlled by a control variable.
987 ;;Sometimes add this directory to info path.
989 (elinstall-dir-has-info dirname)
990 `((add-to-info-path
991 ,def-file
992 "."))
993 '())
995 (apply #'nconc
996 (mapcar
997 #'(lambda (filename)
998 (elinstall-actions-for-source-file
999 filename
1000 dirname))
1001 elisp-source-files))
1003 (if recurse-dirs-p
1004 (apply #'nconc
1005 (mapcar
1006 #'(lambda (filename)
1007 (elinstall-find-actions-by-spec-x
1009 (expand-file-name
1010 filename
1011 dirname)))
1012 sub-dirs))
1013 '()))))
1015 ;;;_ . elinstall-find-actions-by-spec-x
1017 (defun elinstall-find-actions-by-spec-x (spec dir)
1018 "Return a list of actions to do, controlled by SPEC."
1019 (declare (special
1020 load-path-element def-file add-to-load-path-p
1021 recurse-dirs-p))
1023 (if (consp spec)
1024 (case (car spec)
1025 (all
1026 ;;(all . SPEC*)
1027 (apply #'nconc
1028 (mapcar
1029 #'(lambda (sub-spec)
1030 (elinstall-find-actions-by-spec-x
1031 sub-spec
1032 dir))
1033 (cdr spec))))
1035 ;;Rather than trying to bind all control variables each time
1036 ;;thru, we use `set' and `unwind-protect'.
1037 (control
1038 ;;control TYPE DISPOSITION SPEC
1039 (let
1040 ((key (second spec))
1041 old-value)
1042 (if (memq key elinstall-find-actions-control-vars)
1043 (unwind-protect
1044 (progn
1045 (set old-value (symbol-value key))
1046 (set key (third spec))
1047 (elinstall-find-actions-by-spec-x
1048 (second spec)
1049 dir))
1050 (set key old-value))
1051 (error "Unrecognized control variable %s" key))))
1054 (def-file
1055 ;;(def-file FN ARGS SPEC)
1056 (let
1057 ((def-file
1058 (expand-file-name
1059 (second spec)
1060 dir))
1061 (for-preload (third spec)))
1062 (assert (listp for-preload))
1063 (append
1064 (list
1066 (and for-preload (car for-preload))
1067 `(preload-file
1068 ,(car for-preload)
1069 ,def-file
1070 ,@(cdr for-preload))
1071 '()))
1073 (elinstall-find-actions-by-spec-x
1074 (fourth spec) dir))))
1076 (dir
1077 ;;(dir FN)
1078 (elinstall-actions-for-dir
1079 (expand-file-name
1080 (second spec)
1081 dir)
1082 nil))
1084 (file
1085 ;;(file FN)
1086 (elinstall-actions-for-source-file
1087 (second spec) dir))
1090 ;;(in FN SPEC)
1091 (let
1092 ((new-dir
1093 (expand-file-name
1094 (second spec)
1095 dir)))
1097 (elinstall-find-actions-by-spec-x
1098 (third spec)
1099 new-dir)))
1101 (load-path
1102 ;;(load-path SPEC)
1103 (append
1104 `((add-to-load-path ,def-file ,dir))
1105 (let
1106 ((load-path-element dir)
1107 (add-to-load-path-p nil))
1108 (elinstall-find-actions-by-spec-x
1109 (second spec)
1110 dir))))
1112 (matching
1113 ;;(matching PATTERN SPEC)
1114 (apply #'nconc
1115 (mapcar
1116 #'(lambda (dir)
1117 (elinstall-find-actions-by-spec-x
1118 (third spec)
1119 dir))
1120 (directory-files
1121 dir t (second spec)))))
1122 (preload
1123 ;;(preload FN SYM &rest ARGS)
1124 (let
1125 ((key (third spec)))
1127 (and (symbolp key) (symbol-value key))
1128 `((preload-file
1130 ,(expand-file-name (second spec) dir)
1131 ,@(nthcdr 3 spec)))
1132 '()))))
1134 ;;Single symbols
1135 (case spec
1136 (dir
1137 (elinstall-actions-for-dir dir nil))
1138 ((t)
1139 (elinstall-actions-for-dir dir t)))))
1141 ;;;_ . elinstall-find-actions-by-spec
1142 (defun elinstall-find-actions-by-spec
1143 (spec load-path-element dir def-file redo-old)
1144 "Find the list of actions to do according to SPEC."
1146 (let
1148 (def-file-time (elinstall-file-mod-time def-file))
1149 (add-to-load-path-p t)
1150 (recurse-dirs-p t)
1151 (force-recompile-p nil)
1152 (compile-p t))
1153 (declare (special
1154 load-path-element def-file add-to-load-path-p
1155 recurse-dirs-p force-recompile-p compile-p))
1157 (elinstall-find-actions-by-spec-x spec dir)))
1159 ;;;_ . high-level work
1160 ;;;_ , elinstall-get-relevant-load-path
1161 (defun elinstall-get-relevant-load-path (actions)
1163 (delq nil
1164 (mapcar
1165 #'(lambda (act)
1166 (case (car act)
1167 (add-to-load-path
1168 (second act))
1169 (t nil)))
1170 actions)))
1171 ;;;_ , elinstall-get-deffile-list
1172 (defun elinstall-get-deffile-list (stages)
1173 "Get a list of deffile names"
1175 (mapcar
1176 #'car
1177 (elinstall-stages->build-deffiles stages)))
1178 ;;;_ , elinstall-x
1179 (defun elinstall-x (dir spec &optional force)
1180 "High-level worker function to install elisp files."
1181 (let*
1183 ;;This is just the default deffile, spec can override it.
1184 (def-file
1185 (elinstall-expand-deffile-name nil))
1186 (actions
1187 (elinstall-find-actions-by-spec
1188 spec
1191 def-file
1192 (eq force 'redo-old)))
1193 (stages (elinstall-segregate-actions actions))
1194 (use-load-path
1195 (elinstall-get-relevant-load-path
1196 actions)))
1198 (elinstall-stage-update-deffiles
1199 (elinstall-stages->build-deffiles stages)
1200 force
1201 use-load-path)
1202 (elinstall-stage-arrange-preloads
1203 (elinstall-stages->arrange-preloads stages)
1204 (elinstall-get-deffile-list stages)
1205 force)
1206 (elinstall-stage-byte-compile
1207 (elinstall-stages->byte-compile stages))
1209 ;;;_ , Entry points
1210 ;;;_ . elinstall
1211 ;;;###autoload
1212 (defun elinstall (project-name path spec &optional force version-string)
1213 "Install elisp files.
1214 They need not be a formal package.
1216 Parameters:
1218 PROJECT-NAME - the name of the project
1220 PATH - Path to the project.
1221 Suggestion: (elinstall-directory-true-name)
1223 SPEC - a spec for the autoloads etc to make. It can be as simple as
1224 \(dir \"\.\") for installing one directory.
1226 If FORCE is t, install a package even if it has already been
1227 installed. Other non-nil cases of FORCE are reserved for future
1228 development."
1230 (when
1232 force
1233 (not (elinstall-already-installed project-name))
1234 (yes-or-no-p (format "Re-install %s? " project-name)))
1235 (elinstall-x
1236 path
1237 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1238 force)
1239 (elinstall-record-installed project-name version-string)))
1243 ;;;_ . elinstall-update-directory-autoloads
1245 ;;The control variables and values of `force' that would stop other
1246 ;;actions don't exist yet. Similarly for
1247 ;;`elinstall-update-file-autoloads'.
1248 ;;;###autoload
1249 (defun elinstall-update-directory-autoloads (dir)
1250 "Update autoloads for directory DIR"
1252 (interactive "DUpdate autoloads for all elisp files from directory: ")
1253 (elinstall-x
1255 `(control compile-p nil
1256 (dir "."))))
1258 ;;;_ . elinstall-update-directory
1259 ;;;###autoload
1260 (defun elinstall-update-directory (dir)
1261 "Update autoloads for directory DIR"
1263 (interactive "DInstall all elisp files from directory: ")
1264 (elinstall-x
1266 '(dir ".")))
1268 ;;;_ . elinstall-update-file-autoloads
1269 ;;;###autoload
1270 (defun elinstall-update-file-autoloads (file)
1271 "Update autoloads for elisp file FILE"
1273 (interactive "fUpdate autoloads for elisp file: ")
1274 (elinstall-x
1275 (file-name-directory file)
1276 `(control compile-p nil
1277 (file ,(file-name-nondirectory file)))))
1279 ;;;_ . elinstall-update-file
1280 ;;;###autoload
1281 (defun elinstall-update-file (file)
1282 "Install elisp file FILE"
1284 (interactive "fInstall elisp file: ")
1285 (elinstall-x
1286 (file-name-directory file)
1287 `(file ,(file-name-nondirectory file))))
1289 ;;;_. Footers
1290 ;;;_ , Provides
1292 (provide 'elinstall)
1294 ;;;_ * Local emacs vars.
1295 ;;;_ + Local variables:
1296 ;;;_ + mode: allout
1297 ;;;_ + End:
1299 ;;;_ , End
1300 ;;; elinstall.el ends here