Bugfix: set should have been setq
[elinstall.git] / elinstall.el
blobd7abc080860a3a2bf7189cf64e45e81bd1c7b5bc
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 ;;Tell the byte-compiler it's a variable.
81 (defvar elinstall-already-installed)
82 ;;;_ , Types
83 ;;;_ . elinstall-stages
84 (defstruct (elinstall-stages
85 (:constructor elinstall-make-stages)
86 (:conc-name elinstall-stages->)
87 (:copier nil))
88 "The elinstall stages"
89 build-deffiles
90 run-tests
91 byte-compile
92 arrange-preloads)
93 ;;;_ , Data
94 ;;;_ . Regular expressions
95 ;;;_ , elinstall-elisp-regexp
96 (defconst elinstall-elisp-regexp
97 (let ((tmp nil))
98 (dolist
99 (suf (get-load-suffixes))
100 (unless (string-match "\\.elc" suf) (push suf tmp)))
101 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
102 "Regular expression that matches elisp files" )
103 ;;;_ , Utilities
104 ;;;_ . elinstall-file-mod-time
105 (defsubst elinstall-file-mod-time (file)
106 "Return the modification time of FILE"
107 (nth 5 (file-attributes file)))
109 ;;;_ . elinstall-directory-true-name
110 (defun elinstall-directory-true-name ()
111 "Get the true name of the directory the calling code lives in.
112 CAUTION: This is sensitive to where it's called. That's the point of it."
113 (file-name-directory
114 (if load-file-name
115 (file-truename load-file-name)
116 (file-truename buffer-file-name))))
117 ;;;_ . Checking installedness
118 ;;;_ , elinstall-get-installation-record
119 (defun elinstall-get-installation-record (project-name)
120 "Return the installation record for PROJECT-NAME."
122 (assoc project-name elinstall-already-installed))
124 ;;;_ , elinstall-already-installed
125 (defun elinstall-already-installed (project-name)
126 "Return non-nil if PROJECT-NAME has been installed."
127 (elinstall-get-installation-record project-name))
129 ;;;_ , elinstall-record-installed
130 (defun elinstall-record-installed (project-name &optional version)
131 "Record that PROJECT-NAME has been installed."
132 (let
133 ((new-item
134 (list
135 project-name
136 (or version "0")
137 (current-time)
138 'installed))
139 (old-item
140 (elinstall-get-installation-record project-name))
141 (print-length nil)
142 (print-level nil))
143 (when old-item
144 (setq elinstall-already-installed
145 (delete old-item elinstall-already-installed)))
146 (push new-item elinstall-already-installed)
147 (customize-save-variable
148 'elinstall-already-installed
149 elinstall-already-installed
150 "Set by elinstall-record-installed")))
151 ;;;_ . Finding deffiles
152 ;;;_ , elinstall-expand-deffile-name
153 (defun elinstall-expand-deffile-name (deffile)
154 "Expand DEFFILE autoload.el's way."
156 (expand-file-name (or deffile "loaddefs.el")
157 (expand-file-name "lisp"
158 source-directory)))
160 ;;;_ , Work
161 ;;;_ . Doing actions
163 ;;;_ , Doing autoload actions (adapted from autoload.el)
164 ;;;_ . Utilities about the action list
165 ;;;_ , elinstall-remove-autogen-action
166 (defun elinstall-remove-autogen-action (file actions)
167 "Return ACTIONS minus any add-file-autoloads on FILE removed."
169 (delq nil
170 (mapcar
171 #'(lambda (act)
172 (case (car act)
173 (add-file-autoloads
174 (if (equal file (third act))
176 act))
177 (t act)))
178 actions)))
179 ;;;_ , elinstall-get-autogen-action
180 (defun elinstall-get-autogen-action (file actions)
182 (let
183 ((the-act))
184 (dolist (act actions)
185 (case (car act)
186 (add-file-autoloads
187 (when (equal file (third act))
188 (setq the-act act)))))
189 the-act))
190 ;;;_ . About printing to autoload file
191 ;;;_ , elinstall-insert-section-header
192 (defun elinstall-insert-section-header (outbuf form)
193 "Insert the section-header line,
194 which lists the file name and which functions are in it, etc."
195 (insert generate-autoload-section-header)
196 (prin1 form outbuf)
197 (terpri outbuf)
198 ;; Break that line at spaces, to avoid very long lines.
199 ;; Make each sub-line into a comment.
200 (with-current-buffer outbuf
201 (save-excursion
202 (forward-line -1)
203 (while (not (eolp))
204 (move-to-column 64)
205 (skip-chars-forward "^ \n")
206 (or (eolp)
207 (insert "\n" generate-autoload-section-continuation))))))
209 ;;;_ , elinstall-insert-autoload-section
210 (defun elinstall-insert-autoload-section (text form &optional comment-string)
211 "Insert TEXT into current buffer as an autoload section"
213 (let* (
214 (print-length nil)
215 (print-level nil)
216 ;; This does something in Lucid Emacs.
217 (print-readably t)
218 (float-output-format nil))
220 (elinstall-insert-section-header (current-buffer) form)
221 (when comment-string
222 (insert ";;; " comment-string "\n"))
223 (insert text)
224 (insert generate-autoload-section-trailer)))
226 ;;;_ . Making autoloads
227 ;;;_ , elinstall-make-autoload-action
228 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
229 "Return the autoloads for current buffer as a string"
231 ;;We put all the text into a temp buffer, then get that buffer's
232 ;;string.
233 (let
234 ((outbuf
235 (generate-new-buffer " *temp*"))
236 (autoloads-done '())
237 (short-name
238 (file-name-nondirectory full-path))
240 (print-length nil)
241 (print-level nil)
242 ;; Apparently this does something in Lucid Emacs.
243 (print-readably t)
244 (float-output-format nil)
246 ;;load-name relative to a member of load-path.
247 (relative-name
248 (file-name-sans-extension
249 (file-relative-name
250 full-path
251 load-path-element))))
253 (with-current-buffer buf
254 (unwind-protect
255 (save-excursion
256 (save-restriction
257 (widen)
258 (goto-char (point-min))
259 (message "Finding autoloads for %s..." short-name)
260 (while (not (eobp))
261 (skip-chars-forward " \t\n\f")
262 (cond
263 ((looking-at (regexp-quote generate-autoload-cookie))
264 (search-forward generate-autoload-cookie)
265 (skip-chars-forward " \t")
266 (if (eolp)
267 ;; Read the next form and make an autoload.
268 (let* ((form (prog1 (read (current-buffer))
269 (or (bolp) (forward-line 1))))
270 (autoload
271 (make-autoload form relative-name)))
272 (if autoload
273 (push (nth 1 form) autoloads-done)
274 (setq autoload form))
275 (let ((autoload-print-form-outbuf outbuf))
276 (autoload-print-form autoload)))
278 ;; Copy the rest of the line to the output.
279 (princ (buffer-substring
280 (progn
281 ;; Back up over whitespace,
282 ;; to preserve it.
283 (skip-chars-backward " \f\t")
284 (if (= (char-after (1+ (point))) ? )
285 ;; Eat one space.
286 (forward-char 1))
287 (point))
288 (progn (forward-line 1) (point)))
289 outbuf)))
290 ((looking-at ";")
291 ;; Don't read the comment.
292 (forward-line 1))
294 (forward-sexp 1)
295 (forward-line 1))))
296 (message "Finding autoloads for %s...done" short-name))
298 ;;Return this action. The temp buffer's contents is
299 ;;our final string.
300 `(add-file-autoloads
301 ,def-file
302 ,relative-name
303 ,full-path
304 ,(with-current-buffer outbuf (buffer-string))
305 ,autoloads-done))
307 ;;This in unwind-protected
308 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
311 ;;;_ , elinstall-generate-file-autoloads
313 (defun elinstall-generate-file-autoloads
314 (relative-name full-name text autoloads-done)
315 "Insert at point a loaddefs autoload section for FILE.
316 Autoloads are generated for defuns and defmacros in FILE
317 marked by `generate-autoload-cookie' (which see).
318 If FILE is being visited in a buffer, the contents of the buffer
319 are used.
320 Return non-nil in the case where no autoloads were added at point.
322 FULL-NAME is the absolute name of the file.
323 RELATIVE-NAME is its name respective to some component of load-path."
324 (if (not (equal text ""))
325 ;; Insert the section-header line which lists the file name and
326 ;; which functions are in it, etc.
327 (elinstall-insert-autoload-section
328 text
329 (list 'autoloads
330 autoloads-done
331 relative-name
332 (autoload-trim-file-name full-name)
333 (elinstall-file-mod-time full-name))
334 (concat
335 "Generated autoloads from "
336 (autoload-trim-file-name full-name)))
339 ;;;_ , elinstall-deffile-insert-autoloads
340 (defun elinstall-deffile-insert-autoloads (file args)
341 "Update the autoloads for FILE in current buffer.
342 Return FILE if there was no autoload cookie in it, else nil.
344 Current buffer must be a loaddef-style file.
346 LOAD-NAME is the absolute name of the file.
347 RELATIVE-NAME is its name respective to some component of load-path."
348 (let (
349 (found nil)
350 (no-autoloads nil))
352 (save-excursion
353 (save-restriction
354 (widen)
355 (goto-char (point-min))
356 ;; Look for the section for FILE
357 (while (and (not found)
358 (search-forward generate-autoload-section-header nil t))
359 (let ((form (autoload-read-section-header)))
360 (cond
361 ((equal (nth 2 form) file)
362 ;; We found the section for this file.
363 (let ((begin (match-beginning 0)))
364 (progn
365 (search-forward generate-autoload-section-trailer)
366 (delete-region begin (point))
367 (setq found t))))
368 ((string< file (nth 2 form))
369 ;; We've come to a section alphabetically later than
370 ;; FILE. We assume the file is in order and so
371 ;; there must be no section for FILE. We will
372 ;; insert one before the section here.
373 (goto-char (match-beginning 0))
374 (setq found 'new)))))
375 (unless found
376 (progn
377 (setq found 'new)
378 ;; No later sections in the file. Put before the last page.
379 (goto-char (point-max))
380 (search-backward "\f" nil t)))
381 (setq no-autoloads
382 (apply #'elinstall-generate-file-autoloads
383 file args))))
385 (if no-autoloads file nil)))
386 ;;;_ . Arranging to add to info-path and load-path
387 ;;;_ , elinstall-generate-add-to-path
388 (defun elinstall-generate-add-to-path (path-element type)
389 "Insert code at point to add PATH-ELEMENT to a path.
390 If TYPE is:
391 * `add-to-load-path', add to load-path
392 * `add-to-info-path', add to Info-additional-directory-list
394 Current buffer must be a loaddef-style file."
395 (let ( (path-symbol
396 (case type
397 (add-to-load-path 'load-path)
398 (add-to-info-path 'Info-additional-directory-list)
399 (t (error "Type not recognized"))))
400 (description
401 (case type
402 (add-to-load-path "load-path")
403 (add-to-info-path "info-path")))
404 (autoloads-done '())
405 (print-length nil)
406 (print-level nil)
407 ;; This does something in Lucid Emacs.
408 (print-readably t)
409 (float-output-format nil))
411 (message "Generating %s additions..." description)
413 (elinstall-insert-autoload-section
414 (pp-to-string
415 `(add-to-list ',path-symbol
416 (expand-file-name
417 ,(file-relative-name path-element)
418 (if load-file-name
419 (file-name-directory
420 (file-truename load-file-name))))))
421 (list type (list path-element) nil nil nil)
422 nil)
423 (message "Generating %s additions...done" description)))
426 ;;;_ , elinstall-deffile-insert-add-to-path
427 (defun elinstall-deffile-insert-add-to-path (path-element type)
428 "Insert code in current buffer 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-default-directory-list
433 Current buffer must be a loaddef-style file."
434 (let (
435 (found nil)
436 (no-autoloads nil))
438 (save-excursion
439 (save-restriction
440 (widen)
441 (goto-char (point-min))
442 ;; Look for the section for PATH-ELEMENT
443 (while (and (not found)
444 (search-forward generate-autoload-section-header nil t))
445 (let ((form (autoload-read-section-header)))
446 (cond
447 ((and
448 (equal (nth 0 form) type)
449 (member path-element (nth 1 form)))
451 ;; We found the section for this add.
452 (let ((begin (match-beginning 0)))
453 (progn
454 (search-forward generate-autoload-section-trailer)
455 (delete-region begin (point))
456 (setq found t)))))))
458 (unless found
459 (progn
460 (setq found 'new)
461 ;; No later sections in the file. Put before the last page.
462 (goto-char (point-max))
463 (search-backward "\f" nil t)))
465 (elinstall-generate-add-to-path path-element type)))
467 ;;This never belongs in the no-autoloads section.
468 nil))
469 ;;;_ . elinstall-deffile-insert
471 (defun elinstall-deffile-insert (action)
472 "Insert autoloads etc into current file according to ACTION.
473 The format of ACTION is described in the design docs.
475 Return filename if this action belongs in the no-autoload section."
477 (when action
478 (case (car action)
479 (add-file-autoloads
480 (elinstall-deffile-insert-autoloads
481 (third action)
482 (nthcdr 3 action)))
484 (add-to-load-path
485 (elinstall-deffile-insert-add-to-path
486 (third action)
487 'add-to-load-path)
488 nil)
490 (add-to-info-path
491 (elinstall-deffile-insert-add-to-path
492 (third action)
493 'add-to-info-path)
494 nil)
496 ((preload-file run-tests byte-compile)
497 (error "This case should not come here.")))))
499 ;;;_ . elinstall-prepare-deffile
500 (defun elinstall-prepare-deffile (deffile)
501 "Try to ensure that DEFFILE is available for receiving autoloads"
503 (autoload-ensure-default-file deffile)
504 (with-current-buffer (find-file-noselect deffile)
507 ;; We must read/write the file without any code conversion,
508 ;; but still decode EOLs.
509 (let ((coding-system-for-read 'raw-text))
511 ;; This is to make generated-autoload-file have Unix EOLs, so
512 ;; that it is portable to all platforms.
513 (setq buffer-file-coding-system 'raw-text-unix))
514 (or (> (buffer-size) 0)
515 (error "Autoloads file %s does not exist" buffer-file-name))
516 (or (file-writable-p buffer-file-name)
517 (error "Autoloads file %s is not writable"
518 buffer-file-name))))
520 ;;;_ . elinstall-update-deffile
522 ;;Adapted from autoload.el `update-directory-autoloads'.
524 (defun elinstall-update-deffile (target actions &optional
525 use-load-path force)
527 Update file TARGET with current autoloads as specified by ACTIONS.
528 Also remove any old definitions pointing to libraries that can no
529 longer be found.
531 ACTIONS must be a list of actions (See the format doc). Each one's
532 filename must be relative to some element of load-path.
534 USE-LOAD-PATH is a list to use as load-path. It should include
535 any new load-path that we are arranging to create. If it's not given,
536 load-path itself is used.
538 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
539 Other non-nil cases of FORCE are reserved for future development.
541 This uses `update-file-autoloads' (which see) to do its work.
542 In an interactive call, you must give one argument, the name
543 of a single directory."
544 (let
546 (use-load-path (or use-load-path load-path))
547 (this-time (current-time))
548 ;;files with no autoload cookies.
549 (no-autoloads nil))
551 (elinstall-prepare-deffile target)
552 (with-current-buffer
553 (find-file-noselect target)
554 (save-excursion
555 (setq actions
556 (elinstall-remove-autogen-action
557 (autoload-trim-file-name target)
558 actions))
560 (goto-char (point-min))
561 (while (search-forward generate-autoload-section-header nil t)
562 (let* ((form (autoload-read-section-header))
563 (file (nth 3 form)))
564 (cond ((and (consp file) (stringp (car file)))
565 ;; This is a list of files that have no
566 ;; autoload cookies.
567 ;; There shouldn't be more than one such entry.
568 ;; Remove the obsolete section.
569 (autoload-remove-section (match-beginning 0))
570 (let ((last-time (nth 4 form)))
571 (dolist (file file)
572 (let ((file-time (elinstall-file-mod-time file)))
573 (when (and file-time
574 (not (time-less-p last-time file-time)))
575 ;; file unchanged
576 (push file no-autoloads)
577 (setq actions
578 (elinstall-remove-autogen-action
579 file actions)))))))
580 ((not (stringp file)))
582 (let
583 ((file-path
584 (locate-library file nil use-load-path)))
585 (cond
586 ;;File doesn't exist, so remove its
587 ;;section.
588 ((not file-path)
589 (autoload-remove-section
590 (match-beginning 0)))
592 ;; File hasn't changed, so do nothing.
593 ((equal
594 (nth 4 form)
595 (elinstall-file-mod-time file-path))
596 nil)
598 (elinstall-deffile-insert
599 (elinstall-get-autogen-action
600 file actions))))
602 (setq actions
603 (elinstall-remove-autogen-action
604 file actions))))))))
606 ;; Remaining actions have no existing autoload sections yet.
607 (setq no-autoloads
608 (append no-autoloads
609 (delq nil (mapcar #'elinstall-deffile-insert actions))))
610 (when no-autoloads
611 ;; Sort them for better readability.
612 (setq no-autoloads (sort no-autoloads 'string<))
613 ;; Add the `no-autoloads' section.
614 (goto-char (point-max))
615 (search-backward "\f" nil t)
616 (elinstall-insert-autoload-section
618 (list 'autoloads nil nil no-autoloads this-time)))
619 (save-buffer))))
621 ;;;_ . elinstall-stage-update-deffiles
622 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
623 "Update any deffiles mentioned in SEGMENT-LIST.
624 FORCE and USE-LOAD-PATH have the same meaning as in
625 `elinstall-update-deffile'.
627 (mapcar
628 #'(lambda (segment)
629 (let*
630 ((deffile (car segment)))
631 (if (stringp deffile)
632 (elinstall-update-deffile deffile (cdr segment) force
633 use-load-path))))
634 segment-list))
636 ;;;_ , Doing actions to arrange preloads
637 ;;;_ . elinstall-symlink-on-emacs-start
638 (defun elinstall-symlink-on-emacs-start
639 (filename target-basename target-dir &optional priority force)
640 "Symlink to TARGET-BASENAME.el in TARGET-DIR
642 If PRIORITY is given, it will be used as the priority prefix,
643 otherwise elinstall-default-priority will be.
644 PRIORITY must be an integer or nil.
645 If FORCE is `t', do it regardless of timestamps etc.
646 Other non-nil cases of FORCE are reserved for future development."
647 (let*
649 (priority (or priority elinstall-default-priority))
650 (target-name-nodir
651 (format
652 "%d%s.el"
653 priority
654 target-basename))
655 (target
656 (expand-file-name target-name-nodir target-dir)))
659 (cond
660 ;;Path should already exist.
661 ((not
662 (file-exists-p target-dir))
663 (message "The target directory doesn't exist."))
664 ;;Target shouldn't already exist, but if force is given, let
665 ;;user override.
666 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
667 ;;do nothing even on force.
668 ((and
669 (file-exists-p target)
671 (not force)
672 (not
673 (yes-or-no-p
674 (format "Really overwrite %s? " target))))
675 (message "File %s already exists" target)))
678 (make-symbolic-link
679 filename
680 target
681 nil)))))
683 ;;;_ . elinstall-add-to-dot-emacs
684 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
685 "Add code to load FILENAME to .emacs.
686 FILENAME should not have an extension"
688 ;;Visit .emacs
689 (with-current-buffer (find-file-noselect dot-emacs-name)
690 (save-excursion
691 ;;add at end of file
692 (goto-char (point-max))
693 (insert "\n;;Added by elinstall")
694 (insert "\n;;Consider using my-site-start to manage .emacs\n")
695 (pp `(load ,filename) (current-buffer))
696 (save-buffer))))
699 ;;;_ . elinstall-arrange-preload
700 ;;;###autoload
701 (defun elinstall-arrange-preload (force filename basename &optional priority)
702 "Arrange for FILENAME to be loaded on emacs start.
703 FORCE has its usual meaning.
704 BASENAME and PRIORITY are used as arguments to
705 `elinstall-symlink-on-emacs-start'.
708 (let
709 ((preload-target elinstall-default-preload-target))
711 ;;Dispatch the possibilities.
712 (cond
713 ((eq preload-target 'dot-emacs)
714 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
715 ((stringp preload-target)
716 (elinstall-symlink-on-emacs-start
717 filename basename preload-target priority force))
718 ((null preload-target)
719 (message "Not arranging for preloads"))
721 (message "I don't recognize that")))))
722 ;;;_ . elinstall-stage-arrange-preloads
723 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
724 "Arrange any preloads mentioned in ACTIONS."
726 (mapcar
727 #'(lambda (act)
728 (case (car act)
729 (preload-file
730 (let*
731 ( (filename
732 (caddr act))
733 (proceed-p
734 (case (second act)
735 ((t) t)
736 ((nil) nil)
737 (if-used
738 (member filename deffiles-used)))))
740 (when proceed-p
741 (apply
742 #'elinstall-arrange-preload
743 force
744 (cddr act)))))
746 (error
747 "elinstall-stage-arrange-preloads: Action not
748 recognized."))) )
749 actions))
752 ;;;_ , Run tests
753 ;;;_ . elinstall-stage-run-tests
754 (defun elinstall-stage-run-tests (actions)
755 "Run any tests mentioned in ACTIONS."
757 (mapcar
758 #'(lambda (act)
759 (case (car act)
760 (run-tests
761 ;;$$WRITE ME - not a high priority right now.
762 nil)
764 (error
765 "elinstall-stage-run-tests: Action not
766 recognized."))) )
767 actions))
770 ;;;_ , Byte compile
771 ;;;_ . elinstall-stage-byte-compile
772 (defun elinstall-stage-byte-compile (actions)
773 "Do any byte-compilation mentioned in ACTIONS."
775 (mapcar
776 #'(lambda (act)
777 (case (car act)
778 ;;$$IMPROVE ME Understand flags to control second
779 ;;argument (whether to load file after
780 ;;compilation)
781 (byte-compile
782 (byte-compile-file (second act)))
784 (error
785 "elinstall-stage-byte-compile: Action not
786 recognized."))) )
787 actions))
788 ;;;_ . Segregating actions
789 ;;;_ , elinstall-remove-empty-segs
790 (defun elinstall-remove-empty-segs (segment-list)
791 "Return SEGMENT-LIST minus any segments that have no actions.
792 Intended only for the deffile stage data."
793 (delq nil
794 (mapcar
795 #'(lambda (segment)
796 (if (cdr segment)
797 segment
798 nil))
799 segment-list)))
801 ;;;_ , elinstall-segregate-actions
802 (defun elinstall-segregate-actions (actions)
803 "Return actions segregated by deffile.
805 Returns a list whose elements are each a cons of:
806 * deffile filename or nil
807 * A list of actions to be done for that deffile."
809 (let
811 (build-deffiles '())
812 (run-tests '())
813 (byte-compile '())
814 (arrange-preloads '()))
816 (dolist (act actions)
817 (when act
818 (case (car act)
819 ((add-file-autoloads
820 add-to-info-path
821 add-to-load-path)
822 (let*
823 ((deffile-name (second act))
824 (cell-already
825 (assoc deffile-name build-deffiles)))
826 (if cell-already
827 ;;There are already actions on this deffile.
828 ;;Splice this action in.
829 (setcdr cell-already
830 (cons act (cdr cell-already)))
831 ;;There are no actions on this deffile. Add a
832 ;;place for them and include this action.
833 (push (list deffile-name act) build-deffiles))))
834 (preload-file
835 (push act arrange-preloads))
836 (run-tests
837 (push act run-tests))
838 (byte-compile
839 (push act byte-compile)))))
841 (elinstall-make-stages
842 :build-deffiles
843 (elinstall-remove-empty-segs build-deffiles)
844 :run-tests
845 run-tests
846 :byte-compile
847 byte-compile
848 :arrange-preloads
849 arrange-preloads)))
850 ;;;_ . Finding actions
851 ;;;_ , Utility
852 ;;;_ . elinstall-dir-has-info
854 ;;$$IMPROVE ME - Can this test be made more precise?
855 (defun elinstall-dir-has-info (dir)
856 "Return non-nil if DIR has info files in it.
857 DIR should be an absolute path."
859 (string-match "/info/" dir)
860 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
861 ;;;_ . elinstall-directory-files
862 (defun elinstall-directory-files (dirname)
863 "Return a list of files in directory DIRNAME, minus certain files.
864 The following files are omitted:
865 * Dot files
866 * Files that match an entry in `block-in-subtree'.
867 Filenames are relative."
868 (declare (special
869 def-file block-in-dir block-in-subtree))
870 (let*
872 ;;Relative filenames of this directory's files.
873 (all-files
874 ;; Don't include dot files. If user really wants to
875 ;;explore one he can use (dir ".NAME") or (file ".NAME")
876 (directory-files dirname nil "[^\\.]"))
877 ;;We know our def-file isn't really source so remove it.
878 ;;We'd have removed it anyways after seeing file local vars.
879 (all-files
880 (remove def-file all-files))
882 (all-files
883 (delq nil
884 (mapcar
885 #'(lambda (filename)
887 (and
888 block-in-subtree
889 (some
890 #'(lambda (blocked)
891 (string-match blocked filename))
892 block-in-subtree))
894 filename))
895 all-files))))
896 all-files))
899 ;;;_ , Workers
900 ;;;_ . List of special variables used in this section
901 ;;load-path-element - The relevant element of load-path
902 ;;def-file - The file the autoload definitions etc will go into.
903 ;;add-to-load-path-p - Controls whether to add to load-path.
904 ;;recurse-dirs-p - Whether to recurse into subdirectories.
906 ;;block-in-dir - (NOT IMPLEMENTED) List of filenames to reject. They
907 ;;may include wildcards. They apply wrt the original directory.
909 ;;block-in-subtree - (NOT IMPLEMENTED) List of filenames to reject.
910 ;;They may include wildcards. They apply wrt any directory in the
911 ;;tree. Specifically, in the spec tree, which may differ from the
912 ;;file subtree.
914 (defconst elinstall-find-actions-control-vars
915 '(add-to-load-path-p recurse-dirs-p compile-p force-recompile-p)
916 "Control special variables that the find-actions tree recognizes" )
917 ;;;_ . elinstall-actions-for-source-file
918 (defun elinstall-actions-for-source-file (filename dir)
919 "Return a list of actions to do for FILENAME in DIR.
920 Special variables are as noted in \"List of special variables\"."
921 (declare (special
922 load-path-element def-file compile-p force-recompile-p))
923 (let
924 ((full-path
925 (expand-file-name filename dir)))
926 (when
927 (and
928 (file-readable-p full-path)
929 (not (auto-save-file-name-p full-path)))
930 ;;$$IMPROVE ME create and use relevant control variables.
931 (let*
933 (visited (get-file-buffer full-path))
934 (buf
935 (or
936 visited
937 ;;Visit the file cheaply.
938 ;;hack-local-variables can give errors.
939 (ignore-errors (autoload-find-file full-path))))
940 (def-file
942 (ignore-errors
943 (with-current-buffer buf
944 (if (local-variable-p 'generated-autoload-file)
945 (elinstall-expand-deffile-name
946 generated-autoload-file)
947 nil)))
948 def-file))
949 ;;Figure out whether to run some actions, by file local vars.
950 (autoloads-p
951 (ignore-errors
952 (with-current-buffer buf
953 (not no-update-autoloads))))
954 (do-compile-p
955 (and
956 (featurep 'byte-compile)
957 (string-match emacs-lisp-file-regexp filename)
958 (ignore-errors
959 (with-current-buffer buf
960 (not no-byte-compile)))
961 (let
962 ((dest (byte-compile-dest-file full-path)))
963 (if (file-exists-p dest)
964 ;; File was already compiled.
965 (or force-recompile-p
966 (file-newer-than-file-p full-path dest))
967 (or compile-p
968 (y-or-n-p (concat "Compile " filename "? "))))))))
970 (prog1
971 (list
972 (if do-compile-p
973 `(byte-compile ,full-path)
974 nil)
975 (if autoloads-p
976 (elinstall-make-autoload-action
977 buf def-file load-path-element full-path)
978 nil))
979 (unless visited (kill-buffer-if-not-modified buf)))))))
980 ;;;_ . elinstall-actions-for-dir
981 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
982 "Make actions for DIR.
983 Recurse just if RECURSE-DIRS-P"
984 (declare (special
985 load-path-element def-file add-to-load-path-p))
986 ;;This does not treat symlinks specially. $$IMPROVE ME it could
987 ;;treat/not treat them conditional on control variables.
988 (let*
990 (files (elinstall-directory-files dirname))
992 ;;Relative filenames of elisp source
993 (elisp-source-files
994 (delq nil
995 (mapcar
996 #'(lambda (filename)
998 (string-match elinstall-elisp-regexp filename)
999 filename
1000 nil))
1001 files)))
1003 ;;Absolute filenames of subdirectories.
1004 (sub-dirs
1005 (if recurse-dirs-p
1006 (delq nil
1007 (mapcar
1008 #'(lambda (filename)
1009 (let
1010 ((fn (expand-file-name filename dirname)))
1012 (file-directory-p fn)
1014 nil)))
1015 files))
1016 '()))
1018 (load-path-here-p
1019 (and
1020 elisp-source-files ;;If list is not empty.
1021 add-to-load-path-p))
1022 (load-path-element
1023 (if load-path-here-p
1024 dirname
1025 load-path-element)))
1027 (append
1028 ;;Sometimes arrange to add this directory to load-path.
1029 (if load-path-here-p
1030 `((add-to-load-path
1031 ,def-file
1032 ,load-path-element))
1033 '())
1035 ;;$$IMPROVE ME - be controlled by a control variable.
1036 ;;Sometimes add this directory to info path.
1038 (elinstall-dir-has-info dirname)
1039 `((add-to-info-path
1040 ,def-file
1041 "."))
1042 '())
1044 (apply #'nconc
1045 (mapcar
1046 #'(lambda (filename)
1047 (elinstall-actions-for-source-file
1048 filename
1049 dirname))
1050 elisp-source-files))
1052 (if recurse-dirs-p
1053 (apply #'nconc
1054 (mapcar
1055 #'(lambda (filename)
1056 (elinstall-find-actions-by-spec-x
1058 (expand-file-name
1059 filename
1060 dirname)))
1061 sub-dirs))
1062 '()))))
1064 ;;;_ . elinstall-find-actions-by-spec-x
1066 (defun elinstall-find-actions-by-spec-x (spec dir)
1067 "Return a list of actions to do, controlled by SPEC."
1068 (declare (special
1069 load-path-element def-file add-to-load-path-p
1070 recurse-dirs-p block-in-dir block-in-subtree))
1072 (if (consp spec)
1073 (case (car spec)
1074 (all
1075 ;;(all . SPEC*)
1076 (apply #'nconc
1077 (mapcar
1078 #'(lambda (sub-spec)
1079 (elinstall-find-actions-by-spec-x
1080 sub-spec
1081 dir))
1082 (cdr spec))))
1083 (block-in-subtree
1084 ;;(block-in-subtree (* FN) SPEC)
1085 (let
1086 ((block-in-subtree (second spec)))
1087 (elinstall-find-actions-by-spec-x
1088 (third spec)
1089 dir)))
1091 ;;Rather than trying to bind all control variables each time
1092 ;;thru, we use `set' and `unwind-protect'.
1093 (control
1094 ;;control TYPE DISPOSITION SPEC
1095 (let
1096 ((key (second spec))
1097 old-value)
1098 (if (memq key elinstall-find-actions-control-vars)
1099 (unwind-protect
1100 (progn
1101 (setq old-value (symbol-value key))
1102 (set key (third spec))
1103 (elinstall-find-actions-by-spec-x
1104 (fourth spec)
1105 dir))
1106 (set key old-value))
1107 (error "Unrecognized control variable %s" key))))
1110 (def-file
1111 ;;(def-file FN ARGS SPEC)
1112 (let
1113 ((def-file
1114 (expand-file-name
1115 (second spec)
1116 dir))
1117 (for-preload (third spec)))
1118 (assert (listp for-preload))
1119 (append
1120 (list
1122 (and for-preload (car for-preload))
1123 `(preload-file
1124 ,(car for-preload)
1125 ,def-file
1126 ,@(cdr for-preload))
1127 '()))
1129 (elinstall-find-actions-by-spec-x
1130 (fourth spec) dir))))
1132 (dir
1133 ;;(dir FN)
1134 (elinstall-actions-for-dir
1135 (expand-file-name
1136 (second spec)
1137 dir)
1138 nil))
1140 (file
1141 ;;(file FN)
1142 (elinstall-actions-for-source-file
1143 (second spec) dir))
1146 ;;(in FN SPEC)
1147 (let
1148 ((new-dir
1149 (expand-file-name
1150 (second spec)
1151 dir)))
1153 (elinstall-find-actions-by-spec-x
1154 (third spec)
1155 new-dir)))
1157 (load-path
1158 ;;(load-path SPEC)
1159 (append
1160 `((add-to-load-path ,def-file ,dir))
1161 (let
1162 ((load-path-element dir)
1163 (add-to-load-path-p nil))
1164 (elinstall-find-actions-by-spec-x
1165 (second spec)
1166 dir))))
1168 (matching
1169 ;;(matching PATTERN SPEC)
1170 (apply #'nconc
1171 (mapcar
1172 #'(lambda (dir)
1173 ;;$$PUNT Assume that `block-in-subtree' and
1174 ;;`block-in-dir' aren't matched.
1175 (elinstall-find-actions-by-spec-x
1176 (third spec)
1177 dir))
1178 (directory-files
1179 dir t (second spec)))))
1180 (preload
1181 ;;(preload FN SYM &rest ARGS)
1182 (let
1183 ((key (third spec)))
1185 (and (symbolp key) (symbol-value key))
1186 `((preload-file
1188 ,(expand-file-name (second spec) dir)
1189 ,@(nthcdr 3 spec)))
1190 '()))))
1192 ;;Single symbols
1193 (case spec
1194 (dir
1195 (elinstall-actions-for-dir dir nil))
1196 ((t)
1197 (elinstall-actions-for-dir dir t)))))
1199 ;;;_ . elinstall-find-actions-by-spec
1200 (defun elinstall-find-actions-by-spec
1201 (spec load-path-element dir def-file redo-old)
1202 "Find the list of actions to do according to SPEC."
1204 (let
1206 (def-file-time (elinstall-file-mod-time def-file))
1207 (add-to-load-path-p t)
1208 (recurse-dirs-p t)
1209 (force-recompile-p nil)
1210 (compile-p t)
1211 (block-in-dir '())
1212 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1214 (declare (special
1215 load-path-element def-file add-to-load-path-p
1216 recurse-dirs-p force-recompile-p compile-p
1217 block-in-dir block-in-subtree))
1219 (elinstall-find-actions-by-spec-x spec dir)))
1221 ;;;_ . high-level work
1222 ;;;_ , elinstall-get-relevant-load-path
1223 (defun elinstall-get-relevant-load-path (actions)
1225 (delq nil
1226 (mapcar
1227 #'(lambda (act)
1228 (case (car act)
1229 (add-to-load-path
1230 (second act))
1231 (t nil)))
1232 actions)))
1233 ;;;_ , elinstall-get-deffile-list
1234 (defun elinstall-get-deffile-list (stages)
1235 "Get a list of deffile names"
1237 (mapcar
1238 #'car
1239 (elinstall-stages->build-deffiles stages)))
1240 ;;;_ , elinstall-x
1241 (defun elinstall-x (dir spec &optional force)
1242 "High-level worker function to install elisp files."
1243 (let*
1245 ;;This is just the default deffile, spec can override it.
1246 (def-file
1247 (elinstall-expand-deffile-name nil))
1248 (actions
1249 (elinstall-find-actions-by-spec
1250 spec
1253 def-file
1254 (eq force 'redo-old)))
1255 (stages (elinstall-segregate-actions actions))
1256 (use-load-path
1257 (elinstall-get-relevant-load-path
1258 actions)))
1260 (elinstall-stage-update-deffiles
1261 (elinstall-stages->build-deffiles stages)
1262 force
1263 use-load-path)
1264 (elinstall-stage-arrange-preloads
1265 (elinstall-stages->arrange-preloads stages)
1266 (elinstall-get-deffile-list stages)
1267 force)
1268 (elinstall-stage-byte-compile
1269 (elinstall-stages->byte-compile stages))
1271 ;;;_ , Entry points
1272 ;;;_ . elinstall
1273 ;;;###autoload
1274 (defun elinstall (project-name path spec &optional force version-string)
1275 "Install elisp files.
1276 They need not be a formal package.
1278 Parameters:
1280 PROJECT-NAME - the name of the project
1282 PATH - Path to the project.
1283 Suggestion: (elinstall-directory-true-name)
1285 SPEC - a spec for the autoloads etc to make. It can be as simple as
1286 \(dir \"\.\") for installing one directory.
1288 If FORCE is t, install a package even if it has already been
1289 installed. Other non-nil cases of FORCE are reserved for future
1290 development."
1292 (when
1294 force
1295 (not (elinstall-already-installed project-name))
1296 (yes-or-no-p (format "Re-install %s? " project-name)))
1297 (elinstall-x
1298 path
1299 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1300 force)
1301 (elinstall-record-installed project-name version-string)))
1305 ;;;_ . elinstall-update-directory-autoloads
1307 ;;The control variables and values of `force' that would stop other
1308 ;;actions don't exist yet. Similarly for
1309 ;;`elinstall-update-file-autoloads'.
1310 ;;;###autoload
1311 (defun elinstall-update-directory-autoloads (dir)
1312 "Update autoloads for directory DIR"
1314 (interactive "DUpdate autoloads for all elisp files from directory: ")
1315 (elinstall-x
1317 `(control compile-p nil
1318 (dir "."))))
1320 ;;;_ . elinstall-update-directory
1321 ;;;###autoload
1322 (defun elinstall-update-directory (dir)
1323 "Update autoloads for directory DIR"
1325 (interactive "DInstall all elisp files from directory: ")
1326 (elinstall-x
1328 '(dir ".")))
1330 ;;;_ . elinstall-update-file-autoloads
1331 ;;;###autoload
1332 (defun elinstall-update-file-autoloads (file)
1333 "Update autoloads for elisp file FILE"
1335 (interactive "fUpdate autoloads for elisp file: ")
1336 (elinstall-x
1337 (file-name-directory file)
1338 `(control compile-p nil
1339 (file ,(file-name-nondirectory file)))))
1341 ;;;_ . elinstall-update-file
1342 ;;;###autoload
1343 (defun elinstall-update-file (file)
1344 "Install elisp file FILE"
1346 (interactive "fInstall elisp file: ")
1347 (elinstall-x
1348 (file-name-directory file)
1349 `(file ,(file-name-nondirectory file))))
1351 ;;;_. Footers
1352 ;;;_ , Provides
1354 (provide 'elinstall)
1356 ;;;_ * Local emacs vars.
1357 ;;;_ + Local variables:
1358 ;;;_ + mode: allout
1359 ;;;_ + End:
1361 ;;;_ , End
1362 ;;; elinstall.el ends here