Bugfix: file names are now absolute
[elinstall.git] / elinstall.el
blobcd698de243d9ed8fa9f23ae8fded49c3122422d1
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 (declare (special
868 def-file block-in-dir block-in-subtree))
869 (let*
871 ;;Relative filenames of this directory's files.
872 (all-files
873 ;; Don't include dot files. If user really wants to
874 ;;explore one he can use (dir ".NAME") or (file ".NAME")
875 (directory-files dirname nil "[^\\.]"))
876 ;;We know our def-file isn't really source so remove it.
877 ;;We'd have removed it anyways after seeing file local vars.
878 (all-files
879 (remove def-file all-files))
881 (all-files
882 (delq nil
883 (mapcar
884 #'(lambda (filename)
886 (and
887 block-in-subtree
888 (some
889 #'(lambda (blocked)
890 (string-match blocked filename))
891 block-in-subtree))
893 filename))
894 all-files))))
895 all-files))
898 ;;;_ , Workers
899 ;;;_ . List of special variables used in this section
900 ;;load-path-element - The relevant element of load-path
901 ;;def-file - The file the autoload definitions etc will go into.
902 ;;add-to-load-path-p - Controls whether to add to load-path.
903 ;;recurse-dirs-p - Whether to recurse into subdirectories.
905 ;;block-in-dir - (NOT IMPLEMENTED) List of filenames to reject. They
906 ;;may include wildcards. They apply wrt the original directory.
908 ;;block-in-subtree - (NOT IMPLEMENTED) List of filenames to reject.
909 ;;They may include wildcards. They apply wrt any directory in the
910 ;;tree. Specifically, in the spec tree, which may differ from the
911 ;;file subtree.
913 (defconst elinstall-find-actions-control-vars
914 '(add-to-load-path-p recurse-dirs-p compile-p force-recompile-p)
915 "Control special variables that the find-actions tree recognizes" )
916 ;;;_ . elinstall-actions-for-source-file
917 (defun elinstall-actions-for-source-file (filename dir)
918 "Return a list of actions to do for FILENAME in DIR.
919 Special variables are as noted in \"List of special variables\"."
920 (declare (special
921 load-path-element def-file compile-p force-recompile-p))
922 (let
923 ((full-path
924 (expand-file-name filename dir)))
925 (when
926 (and
927 (file-readable-p full-path)
928 (not (auto-save-file-name-p full-path)))
929 ;;$$IMPROVE ME create and use relevant control variables.
930 (let*
932 (visited (get-file-buffer full-path))
933 (buf
934 (or
935 visited
936 ;;Visit the file cheaply.
937 ;;hack-local-variables can give errors.
938 (ignore-errors (autoload-find-file full-path))))
939 (def-file
941 (ignore-errors
942 (with-current-buffer buf
943 (if (local-variable-p 'generated-autoload-file)
944 (elinstall-expand-deffile-name
945 generated-autoload-file)
946 nil)))
947 def-file))
948 ;;Figure out whether to run some actions, by file local vars.
949 (autoloads-p
950 (ignore-errors
951 (with-current-buffer buf
952 (not no-update-autoloads))))
953 (do-compile-p
954 (and
955 (featurep 'byte-compile)
956 (string-match emacs-lisp-file-regexp filename)
957 (ignore-errors
958 (with-current-buffer buf
959 (not no-byte-compile)))
960 (let
961 ((dest (byte-compile-dest-file full-path)))
962 (if (file-exists-p dest)
963 ;; File was already compiled.
964 (or force-recompile-p
965 (file-newer-than-file-p full-path dest))
966 (or compile-p
967 (y-or-n-p (concat "Compile " filename "? "))))))))
969 (prog1
970 (list
971 (if do-compile-p
972 `(byte-compile ,full-path)
973 nil)
974 (if autoloads-p
975 (elinstall-make-autoload-action
976 buf def-file load-path-element full-path)
977 nil))
978 (unless visited (kill-buffer-if-not-modified buf)))))))
979 ;;;_ . elinstall-actions-for-dir
980 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
981 "Make actions for DIR.
982 Recurse just if RECURSE-DIRS-P"
983 (declare (special
984 load-path-element def-file add-to-load-path-p))
985 ;;This does not treat symlinks specially. $$IMPROVE ME it could
986 ;;treat/not treat them conditional on control variables.
987 (let*
989 (files (elinstall-directory-files dirname))
991 ;;Relative filenames of elisp source
992 (elisp-source-files
993 (delq nil
994 (mapcar
995 #'(lambda (filename)
997 (string-match elinstall-elisp-regexp filename)
998 filename
999 nil))
1000 files)))
1002 ;;Absolute filenames of subdirectories.
1003 (sub-dirs
1004 (if recurse-dirs-p
1005 (delq nil
1006 (mapcar
1007 #'(lambda (filename)
1008 (let
1009 ((fn (expand-file-name
1010 filename
1011 dirname)))
1013 (file-directory-p fn)
1015 nil)))
1016 files))
1017 '()))
1019 (load-path-here-p
1020 (and
1021 elisp-source-files ;;If list is not empty.
1022 add-to-load-path-p))
1023 (load-path-element
1024 (if load-path-here-p
1025 dirname
1026 load-path-element)))
1028 (append
1029 ;;Sometimes arrange to add this directory to load-path.
1030 (if load-path-here-p
1031 `((add-to-load-path
1032 ,def-file
1033 ,load-path-element))
1034 '())
1036 ;;$$IMPROVE ME - be controlled by a control variable.
1037 ;;Sometimes add this directory to info path.
1039 (elinstall-dir-has-info dirname)
1040 `((add-to-info-path
1041 ,def-file
1042 "."))
1043 '())
1045 (apply #'nconc
1046 (mapcar
1047 #'(lambda (filename)
1048 (elinstall-actions-for-source-file
1049 filename
1050 dirname))
1051 elisp-source-files))
1053 (if recurse-dirs-p
1054 (apply #'nconc
1055 (mapcar
1056 #'(lambda (filename)
1057 (elinstall-find-actions-by-spec-x
1059 (expand-file-name
1060 filename
1061 dirname)))
1062 sub-dirs))
1063 '()))))
1065 ;;;_ . elinstall-find-actions-by-spec-x
1067 (defun elinstall-find-actions-by-spec-x (spec dir)
1068 "Return a list of actions to do, controlled by SPEC."
1069 (declare (special
1070 load-path-element def-file add-to-load-path-p
1071 recurse-dirs-p block-in-dir block-in-subtree))
1073 (if (consp spec)
1074 (case (car spec)
1075 (all
1076 ;;(all . SPEC*)
1077 (apply #'nconc
1078 (mapcar
1079 #'(lambda (sub-spec)
1080 (elinstall-find-actions-by-spec-x
1081 sub-spec
1082 dir))
1083 (cdr spec))))
1084 (block-in-subtree
1085 ;;(block-in-subtree (* FN) SPEC)
1086 (let
1087 ((block-in-subtree (second spec)))
1088 (elinstall-find-actions-by-spec-x
1089 (third spec)
1090 dir)))
1092 ;;Rather than trying to bind all control variables each time
1093 ;;thru, we use `set' and `unwind-protect'.
1094 (control
1095 ;;control TYPE DISPOSITION SPEC
1096 (let
1097 ((key (second spec))
1098 old-value)
1099 (if (memq key elinstall-find-actions-control-vars)
1100 (unwind-protect
1101 (progn
1102 (set old-value (symbol-value key))
1103 (set key (third spec))
1104 (elinstall-find-actions-by-spec-x
1105 (fourth spec)
1106 dir))
1107 (set key old-value))
1108 (error "Unrecognized control variable %s" key))))
1111 (def-file
1112 ;;(def-file FN ARGS SPEC)
1113 (let
1114 ((def-file
1115 (expand-file-name
1116 (second spec)
1117 dir))
1118 (for-preload (third spec)))
1119 (assert (listp for-preload))
1120 (append
1121 (list
1123 (and for-preload (car for-preload))
1124 `(preload-file
1125 ,(car for-preload)
1126 ,def-file
1127 ,@(cdr for-preload))
1128 '()))
1130 (elinstall-find-actions-by-spec-x
1131 (fourth spec) dir))))
1133 (dir
1134 ;;(dir FN)
1135 (elinstall-actions-for-dir
1136 (expand-file-name
1137 (second spec)
1138 dir)
1139 nil))
1141 (file
1142 ;;(file FN)
1143 (elinstall-actions-for-source-file
1144 (second spec) dir))
1147 ;;(in FN SPEC)
1148 (let
1149 ((new-dir
1150 (expand-file-name
1151 (second spec)
1152 dir)))
1154 (elinstall-find-actions-by-spec-x
1155 (third spec)
1156 new-dir)))
1158 (load-path
1159 ;;(load-path SPEC)
1160 (append
1161 `((add-to-load-path ,def-file ,dir))
1162 (let
1163 ((load-path-element dir)
1164 (add-to-load-path-p nil))
1165 (elinstall-find-actions-by-spec-x
1166 (second spec)
1167 dir))))
1169 (matching
1170 ;;(matching PATTERN SPEC)
1171 (apply #'nconc
1172 (mapcar
1173 #'(lambda (dir)
1174 ;;$$PUNT Assume that `block-in-subtree' and
1175 ;;`block-in-dir' aren't matched.
1176 (elinstall-find-actions-by-spec-x
1177 (third spec)
1178 dir))
1179 (directory-files
1180 dir t (second spec)))))
1181 (preload
1182 ;;(preload FN SYM &rest ARGS)
1183 (let
1184 ((key (third spec)))
1186 (and (symbolp key) (symbol-value key))
1187 `((preload-file
1189 ,(expand-file-name (second spec) dir)
1190 ,@(nthcdr 3 spec)))
1191 '()))))
1193 ;;Single symbols
1194 (case spec
1195 (dir
1196 (elinstall-actions-for-dir dir nil))
1197 ((t)
1198 (elinstall-actions-for-dir dir t)))))
1200 ;;;_ . elinstall-find-actions-by-spec
1201 (defun elinstall-find-actions-by-spec
1202 (spec load-path-element dir def-file redo-old)
1203 "Find the list of actions to do according to SPEC."
1205 (let
1207 (def-file-time (elinstall-file-mod-time def-file))
1208 (add-to-load-path-p t)
1209 (recurse-dirs-p t)
1210 (force-recompile-p nil)
1211 (compile-p t)
1212 (block-in-dir '())
1213 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1215 (declare (special
1216 load-path-element def-file add-to-load-path-p
1217 recurse-dirs-p force-recompile-p compile-p
1218 block-in-dir block-in-subtree))
1220 (elinstall-find-actions-by-spec-x spec dir)))
1222 ;;;_ . high-level work
1223 ;;;_ , elinstall-get-relevant-load-path
1224 (defun elinstall-get-relevant-load-path (actions)
1226 (delq nil
1227 (mapcar
1228 #'(lambda (act)
1229 (case (car act)
1230 (add-to-load-path
1231 (second act))
1232 (t nil)))
1233 actions)))
1234 ;;;_ , elinstall-get-deffile-list
1235 (defun elinstall-get-deffile-list (stages)
1236 "Get a list of deffile names"
1238 (mapcar
1239 #'car
1240 (elinstall-stages->build-deffiles stages)))
1241 ;;;_ , elinstall-x
1242 (defun elinstall-x (dir spec &optional force)
1243 "High-level worker function to install elisp files."
1244 (let*
1246 ;;This is just the default deffile, spec can override it.
1247 (def-file
1248 (elinstall-expand-deffile-name nil))
1249 (actions
1250 (elinstall-find-actions-by-spec
1251 spec
1254 def-file
1255 (eq force 'redo-old)))
1256 (stages (elinstall-segregate-actions actions))
1257 (use-load-path
1258 (elinstall-get-relevant-load-path
1259 actions)))
1261 (elinstall-stage-update-deffiles
1262 (elinstall-stages->build-deffiles stages)
1263 force
1264 use-load-path)
1265 (elinstall-stage-arrange-preloads
1266 (elinstall-stages->arrange-preloads stages)
1267 (elinstall-get-deffile-list stages)
1268 force)
1269 (elinstall-stage-byte-compile
1270 (elinstall-stages->byte-compile stages))
1272 ;;;_ , Entry points
1273 ;;;_ . elinstall
1274 ;;;###autoload
1275 (defun elinstall (project-name path spec &optional force version-string)
1276 "Install elisp files.
1277 They need not be a formal package.
1279 Parameters:
1281 PROJECT-NAME - the name of the project
1283 PATH - Path to the project.
1284 Suggestion: (elinstall-directory-true-name)
1286 SPEC - a spec for the autoloads etc to make. It can be as simple as
1287 \(dir \"\.\") for installing one directory.
1289 If FORCE is t, install a package even if it has already been
1290 installed. Other non-nil cases of FORCE are reserved for future
1291 development."
1293 (when
1295 force
1296 (not (elinstall-already-installed project-name))
1297 (yes-or-no-p (format "Re-install %s? " project-name)))
1298 (elinstall-x
1299 path
1300 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1301 force)
1302 (elinstall-record-installed project-name version-string)))
1306 ;;;_ . elinstall-update-directory-autoloads
1308 ;;The control variables and values of `force' that would stop other
1309 ;;actions don't exist yet. Similarly for
1310 ;;`elinstall-update-file-autoloads'.
1311 ;;;###autoload
1312 (defun elinstall-update-directory-autoloads (dir)
1313 "Update autoloads for directory DIR"
1315 (interactive "DUpdate autoloads for all elisp files from directory: ")
1316 (elinstall-x
1318 `(control compile-p nil
1319 (dir "."))))
1321 ;;;_ . elinstall-update-directory
1322 ;;;###autoload
1323 (defun elinstall-update-directory (dir)
1324 "Update autoloads for directory DIR"
1326 (interactive "DInstall all elisp files from directory: ")
1327 (elinstall-x
1329 '(dir ".")))
1331 ;;;_ . elinstall-update-file-autoloads
1332 ;;;###autoload
1333 (defun elinstall-update-file-autoloads (file)
1334 "Update autoloads for elisp file FILE"
1336 (interactive "fUpdate autoloads for elisp file: ")
1337 (elinstall-x
1338 (file-name-directory file)
1339 `(control compile-p nil
1340 (file ,(file-name-nondirectory file)))))
1342 ;;;_ . elinstall-update-file
1343 ;;;###autoload
1344 (defun elinstall-update-file (file)
1345 "Install elisp file FILE"
1347 (interactive "fInstall elisp file: ")
1348 (elinstall-x
1349 (file-name-directory file)
1350 `(file ,(file-name-nondirectory file))))
1352 ;;;_. Footers
1353 ;;;_ , Provides
1355 (provide 'elinstall)
1357 ;;;_ * Local emacs vars.
1358 ;;;_ + Local variables:
1359 ;;;_ + mode: allout
1360 ;;;_ + End:
1362 ;;;_ , End
1363 ;;; elinstall.el ends here