Added variable declaration for elinstall-already-installed
[elinstall.git] / elinstall.el
blob3a343f9b18479b46f697b6f89001fd65167bd087
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 ;;;_ , Informational
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\\)?$")))
862 ;;;_ , Workers
863 ;;;_ . List of special variables used in this section
864 ;;load-path-element - The relevant element of load-path
865 ;;def-file - The file the autoload definitions etc will go into.
866 ;;add-to-load-path-p - Controls whether to add to load-path.
867 ;;recurse-dirs-p - Whether to recurse into subdirectories.
868 (defconst elinstall-find-actions-control-vars
869 '(add-to-load-path-p recurse-dirs-p compile-p force-recompile-p)
870 "Control special variables that the find-actions tree recognizes" )
871 ;;;_ . elinstall-actions-for-source-file
872 (defun elinstall-actions-for-source-file (filename dir)
873 "Return a list of actions to do for FILENAME in DIR.
874 Special variables are as noted in \"List of special variables\"."
875 (declare (special
876 load-path-element def-file compile-p force-recompile-p))
877 (let
878 ((full-path
879 (expand-file-name filename dir)))
880 (when
881 (and
882 (file-readable-p full-path)
883 (not (auto-save-file-name-p full-path)))
884 ;;$$IMPROVE ME create and use relevant control variables.
885 (let*
887 (visited (get-file-buffer full-path))
888 (buf
889 (or
890 visited
891 ;;Visit the file cheaply.
892 ;;hack-local-variables can give errors.
893 (ignore-errors (autoload-find-file full-path))))
894 (def-file
896 (ignore-errors
897 (with-current-buffer buf
898 (if (local-variable-p 'generated-autoload-file)
899 (elinstall-expand-deffile-name
900 generated-autoload-file)
901 nil)))
902 def-file))
903 ;;Figure out whether to run some actions, by file local vars.
904 (autoloads-p
905 (ignore-errors
906 (with-current-buffer buf
907 (not no-update-autoloads))))
908 (do-compile-p
909 (and
910 (featurep 'byte-compile)
911 (string-match emacs-lisp-file-regexp filename)
912 (ignore-errors
913 (with-current-buffer buf
914 (not no-byte-compile)))
915 (let
916 ((dest (byte-compile-dest-file full-path)))
917 (if (file-exists-p dest)
918 ;; File was already compiled.
919 (or force-recompile-p
920 (file-newer-than-file-p full-path dest))
921 (or compile-p
922 (y-or-n-p (concat "Compile " filename "? "))))))))
924 (prog1
925 (list
926 (if do-compile-p
927 `(byte-compile ,full-path)
928 nil)
929 (if autoloads-p
930 (elinstall-make-autoload-action
931 buf def-file load-path-element full-path)
932 nil))
933 (unless visited (kill-buffer-if-not-modified buf)))))))
934 ;;;_ . elinstall-actions-for-dir
935 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
936 "Make actions for DIR.
937 Recurse just if RECURSE-DIRS-P"
938 (declare (special
939 load-path-element def-file add-to-load-path-p))
940 ;;This does not treat symlinks specially. $$IMPROVE ME it could
941 ;;treat/not treat them conditional on control variables.
942 (let*
944 ;;Relative filenames of the source files. We know our
945 ;;loaddefs.el isn't really source so remove it. We'd have
946 ;;removed it anyways after seeing file local vars.
948 (elisp-source-files
949 (remove def-file
950 (directory-files
951 dirname
952 nil
953 elinstall-elisp-regexp)))
954 ;;Absolute filenames of subdirectories.
955 ;;Don't accept any directories beginning with dot. If user
956 ;;really wants to explore one he can use `(dir ".NAME")'.
957 (sub-dirs
958 (if recurse-dirs-p
959 (delq nil
960 (mapcar
961 #'(lambda (filename)
963 (file-directory-p filename)
964 filename
965 nil))
966 (directory-files
967 dirname t
968 "[^\\.]")))
969 '()))
971 (load-path-here
972 (and
973 elisp-source-files ;;If list is not empty.
974 add-to-load-path-p))
975 (load-path-element
976 (if load-path-here
977 dirname
978 load-path-element)))
980 (append
981 ;;Sometimes arrange to add this directory to load-path.
982 (if load-path-here
983 `((add-to-load-path
984 ,def-file
985 ,load-path-element))
986 '())
988 ;;$$IMPROVE ME - be controlled by a control variable.
989 ;;Sometimes add this directory to info path.
991 (elinstall-dir-has-info dirname)
992 `((add-to-info-path
993 ,def-file
994 "."))
995 '())
997 (apply #'nconc
998 (mapcar
999 #'(lambda (filename)
1000 (elinstall-actions-for-source-file
1001 filename
1002 dirname))
1003 elisp-source-files))
1005 (if recurse-dirs-p
1006 (apply #'nconc
1007 (mapcar
1008 #'(lambda (filename)
1009 (elinstall-find-actions-by-spec-x
1011 (expand-file-name
1012 filename
1013 dirname)))
1014 sub-dirs))
1015 '()))))
1017 ;;;_ . elinstall-find-actions-by-spec-x
1019 (defun elinstall-find-actions-by-spec-x (spec dir)
1020 "Return a list of actions to do, controlled by SPEC."
1021 (declare (special
1022 load-path-element def-file add-to-load-path-p
1023 recurse-dirs-p))
1025 (if (consp spec)
1026 (case (car spec)
1027 (all
1028 ;;(all . SPEC*)
1029 (apply #'nconc
1030 (mapcar
1031 #'(lambda (sub-spec)
1032 (elinstall-find-actions-by-spec-x
1033 sub-spec
1034 dir))
1035 (cdr spec))))
1037 ;;Rather than trying to bind all control variables each time
1038 ;;thru, we use `set' and `unwind-protect'.
1039 (control
1040 ;;control TYPE DISPOSITION SPEC
1041 (let
1042 ((key (second spec))
1043 old-value)
1044 (if (memq key elinstall-find-actions-control-vars)
1045 (unwind-protect
1046 (progn
1047 (set old-value (symbol-value key))
1048 (set key (third spec))
1049 (elinstall-find-actions-by-spec-x
1050 (second spec)
1051 dir))
1052 (set key old-value))
1053 (error "Unrecognized control variable %s" key))))
1056 (def-file
1057 ;;(def-file FN ARGS SPEC)
1058 (let
1059 ((def-file
1060 (expand-file-name
1061 (second spec)
1062 dir))
1063 (for-preload (third spec)))
1064 (assert (listp for-preload))
1065 (append
1066 (list
1068 (and for-preload (car for-preload))
1069 `(preload-file
1070 ,(car for-preload)
1071 ,def-file
1072 ,@(cdr for-preload))
1073 '()))
1075 (elinstall-find-actions-by-spec-x
1076 (fourth spec) dir))))
1078 (dir
1079 ;;(dir FN)
1080 (elinstall-actions-for-dir
1081 (expand-file-name
1082 (second spec)
1083 dir)
1084 nil))
1086 (file
1087 ;;(file FN)
1088 (elinstall-actions-for-source-file
1089 (second spec) dir))
1092 ;;(in FN SPEC)
1093 (let
1094 ((new-dir
1095 (expand-file-name
1096 (second spec)
1097 dir)))
1099 (elinstall-find-actions-by-spec-x
1100 (third spec)
1101 new-dir)))
1103 (load-path
1104 ;;(load-path SPEC)
1105 (append
1106 `((add-to-load-path ,def-file ,dir))
1107 (let
1108 ((load-path-element dir)
1109 (add-to-load-path-p nil))
1110 (elinstall-find-actions-by-spec-x
1111 (second spec)
1112 dir))))
1114 (matching
1115 ;;(matching PATTERN SPEC)
1116 (apply #'nconc
1117 (mapcar
1118 #'(lambda (dir)
1119 (elinstall-find-actions-by-spec-x
1120 (third spec)
1121 dir))
1122 (directory-files
1123 dir t (second spec)))))
1124 (preload
1125 ;;(preload FN SYM &rest ARGS)
1126 (let
1127 ((key (third spec)))
1129 (and (symbolp key) (symbol-value key))
1130 `((preload-file
1132 ,(expand-file-name (second spec) dir)
1133 ,@(nthcdr 3 spec)))
1134 '()))))
1136 ;;Single symbols
1137 (case spec
1138 (dir
1139 (elinstall-actions-for-dir dir nil))
1140 ((t)
1141 (elinstall-actions-for-dir dir t)))))
1143 ;;;_ . elinstall-find-actions-by-spec
1144 (defun elinstall-find-actions-by-spec
1145 (spec load-path-element dir def-file redo-old)
1146 "Find the list of actions to do according to SPEC."
1148 (let
1150 (def-file-time (elinstall-file-mod-time def-file))
1151 (add-to-load-path-p t)
1152 (recurse-dirs-p t)
1153 (force-recompile-p nil)
1154 (compile-p t))
1155 (declare (special
1156 load-path-element def-file add-to-load-path-p
1157 recurse-dirs-p force-recompile-p compile-p))
1159 (elinstall-find-actions-by-spec-x spec dir)))
1161 ;;;_ . high-level work
1162 ;;;_ , elinstall-get-relevant-load-path
1163 (defun elinstall-get-relevant-load-path (actions)
1165 (delq nil
1166 (mapcar
1167 #'(lambda (act)
1168 (case (car act)
1169 (add-to-load-path
1170 (second act))
1171 (t nil)))
1172 actions)))
1173 ;;;_ , elinstall-get-deffile-list
1174 (defun elinstall-get-deffile-list (stages)
1175 "Get a list of deffile names"
1177 (mapcar
1178 #'car
1179 (elinstall-stages->build-deffiles stages)))
1180 ;;;_ , elinstall-x
1181 (defun elinstall-x (dir spec &optional force)
1182 "High-level worker function to install elisp files."
1183 (let*
1185 ;;This is just the default deffile, spec can override it.
1186 (def-file
1187 (elinstall-expand-deffile-name nil))
1188 (actions
1189 (elinstall-find-actions-by-spec
1190 spec
1193 def-file
1194 (eq force 'redo-old)))
1195 (stages (elinstall-segregate-actions actions))
1196 (use-load-path
1197 (elinstall-get-relevant-load-path
1198 actions)))
1200 (elinstall-stage-update-deffiles
1201 (elinstall-stages->build-deffiles stages)
1202 force
1203 use-load-path)
1204 (elinstall-stage-arrange-preloads
1205 (elinstall-stages->arrange-preloads stages)
1206 (elinstall-get-deffile-list stages)
1207 force)
1208 (elinstall-stage-byte-compile
1209 (elinstall-stages->byte-compile stages))
1211 ;;;_ , Entry points
1212 ;;;_ . elinstall
1213 ;;;###autoload
1214 (defun elinstall (project-name path spec &optional force version-string)
1215 "Install elisp files.
1216 They need not be a formal package.
1218 Parameters:
1220 PROJECT-NAME - the name of the project
1222 PATH - Path to the project.
1223 Suggestion: (elinstall-directory-true-name)
1225 SPEC - a spec for the autoloads etc to make. It can be as simple as
1226 \(dir \"\.\") for installing one directory.
1228 If FORCE is t, install a package even if it has already been
1229 installed. Other non-nil cases of FORCE are reserved for future
1230 development."
1232 (when
1234 force
1235 (not (elinstall-already-installed project-name))
1236 (yes-or-no-p (format "Re-install %s? " project-name)))
1237 (elinstall-x
1238 path
1239 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1240 force)
1241 (elinstall-record-installed project-name version-string)))
1245 ;;;_ . elinstall-update-directory-autoloads
1247 ;;The control variables and values of `force' that would stop other
1248 ;;actions don't exist yet. Similarly for
1249 ;;`elinstall-update-file-autoloads'.
1250 ;;;###autoload
1251 (defun elinstall-update-directory-autoloads (dir)
1252 "Update autoloads for directory DIR"
1254 (interactive "DUpdate autoloads for all elisp files from directory: ")
1255 (elinstall-x
1257 `(control compile-p nil
1258 (dir "."))))
1260 ;;;_ . elinstall-update-directory
1261 ;;;###autoload
1262 (defun elinstall-update-directory (dir)
1263 "Update autoloads for directory DIR"
1265 (interactive "DInstall all elisp files from directory: ")
1266 (elinstall-x
1268 '(dir ".")))
1270 ;;;_ . elinstall-update-file-autoloads
1271 ;;;###autoload
1272 (defun elinstall-update-file-autoloads (file)
1273 "Update autoloads for elisp file FILE"
1275 (interactive "fUpdate autoloads for elisp file: ")
1276 (elinstall-x
1277 (file-name-directory file)
1278 `(control compile-p nil
1279 (file ,(file-name-nondirectory file)))))
1281 ;;;_ . elinstall-update-file
1282 ;;;###autoload
1283 (defun elinstall-update-file (file)
1284 "Install elisp file FILE"
1286 (interactive "fInstall elisp file: ")
1287 (elinstall-x
1288 (file-name-directory file)
1289 `(file ,(file-name-nondirectory file))))
1291 ;;;_. Footers
1292 ;;;_ , Provides
1294 (provide 'elinstall)
1296 ;;;_ * Local emacs vars.
1297 ;;;_ + Local variables:
1298 ;;;_ + mode: allout
1299 ;;;_ + End:
1301 ;;;_ , End
1302 ;;; elinstall.el ends here