Rename compile-p -> byte-compile-p
[elinstall.git] / elinstall.el
blob2cb5e9ea8cbc552a2a1313044c53e2ecd219b797
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.
913 ;;byte-compile-p - whether to byte-compile at all, t by default. If it's
914 ;;`ask', prompt the user unless recompiling an out-of-date file.
915 ;;force-recompile-p - boolean whether to compile even up to date
916 ;;files, nil by default.
917 ;;make-autoloads - boolean whether to make autoloads, t by default.
918 ;;setup-preloads - boolean whether to set up preloads, t by default.
919 (defconst elinstall-find-actions-control-vars
920 '(add-to-load-path-p recurse-dirs-p byte-compile-p force-recompile-p
921 make-autoloads setup-preloads)
922 "Control special variables that the find-actions tree recognizes" )
923 ;;;_ . elinstall-actions-for-source-file
924 (defun elinstall-actions-for-source-file (filename dir)
925 "Return a list of actions to do for FILENAME in DIR.
926 Special variables are as noted in \"List of special variables\"."
927 (declare (special
928 load-path-element def-file byte-compile-p force-recompile-p))
929 (let
930 ((full-path
931 (expand-file-name filename dir)))
932 (when
933 (and
934 (file-readable-p full-path)
935 (not (auto-save-file-name-p full-path)))
936 ;;$$IMPROVE ME create and use relevant control variables.
937 (let*
939 (visited (get-file-buffer full-path))
940 (buf
941 (or
942 visited
943 ;;Visit the file cheaply.
944 ;;hack-local-variables can give errors.
945 (ignore-errors (autoload-find-file full-path))))
946 (def-file
948 (ignore-errors
949 (with-current-buffer buf
950 (if (local-variable-p 'generated-autoload-file)
951 (elinstall-expand-deffile-name
952 generated-autoload-file)
953 nil)))
954 def-file))
955 ;;Figure out whether to run some actions, by file local vars.
956 (autoloads-p
957 (ignore-errors
958 (with-current-buffer buf
959 (not no-update-autoloads))))
960 (do-compile-p
961 (and
962 byte-compile-p
963 (featurep 'byte-compile)
964 (string-match emacs-lisp-file-regexp filename)
965 (ignore-errors
966 (with-current-buffer buf
967 (not no-byte-compile)))
968 (let
969 ((dest (byte-compile-dest-file full-path)))
970 ;;$$CHANGE MY LOGIC
971 (if (file-exists-p dest)
972 ;; File was already compiled.
973 (or force-recompile-p
974 (file-newer-than-file-p full-path dest))
975 (or byte-compile-p
976 (y-or-n-p (concat "Compile " filename "? "))))))))
978 (prog1
979 (list
980 (if do-compile-p
981 `(byte-compile ,full-path)
982 nil)
983 (if autoloads-p
984 (elinstall-make-autoload-action
985 buf def-file load-path-element full-path)
986 nil))
987 (unless visited (kill-buffer-if-not-modified buf)))))))
988 ;;;_ . elinstall-actions-for-dir
989 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
990 "Make actions for DIR.
991 Recurse just if RECURSE-DIRS-P"
992 (declare (special
993 load-path-element def-file add-to-load-path-p))
994 ;;This does not treat symlinks specially. $$IMPROVE ME it could
995 ;;treat/not treat them conditional on control variables.
996 (let*
998 (files (elinstall-directory-files dirname))
1000 ;;Relative filenames of elisp source
1001 (elisp-source-files
1002 (delq nil
1003 (mapcar
1004 #'(lambda (filename)
1006 (string-match elinstall-elisp-regexp filename)
1007 filename
1008 nil))
1009 files)))
1011 ;;Absolute filenames of subdirectories.
1012 (sub-dirs
1013 (if recurse-dirs-p
1014 (delq nil
1015 (mapcar
1016 #'(lambda (filename)
1017 (let
1018 ((fn (expand-file-name filename dirname)))
1020 (file-directory-p fn)
1022 nil)))
1023 files))
1024 '()))
1026 (load-path-here-p
1027 (and
1028 elisp-source-files ;;If list is not empty.
1029 add-to-load-path-p))
1030 (load-path-element
1031 (if load-path-here-p
1032 dirname
1033 load-path-element)))
1035 (append
1036 ;;Sometimes arrange to add this directory to load-path.
1037 (if load-path-here-p
1038 `((add-to-load-path
1039 ,def-file
1040 ,load-path-element))
1041 '())
1043 ;;$$IMPROVE ME - be controlled by a control variable.
1044 ;;Sometimes add this directory to info path.
1046 (elinstall-dir-has-info dirname)
1047 `((add-to-info-path
1048 ,def-file
1049 "."))
1050 '())
1052 (apply #'nconc
1053 (mapcar
1054 #'(lambda (filename)
1055 (elinstall-actions-for-source-file
1056 filename
1057 dirname))
1058 elisp-source-files))
1060 (if recurse-dirs-p
1061 (apply #'nconc
1062 (mapcar
1063 #'(lambda (filename)
1064 (elinstall-find-actions-by-spec-x
1066 (expand-file-name
1067 filename
1068 dirname)))
1069 sub-dirs))
1070 '()))))
1072 ;;;_ . elinstall-find-actions-by-spec-x
1074 (defun elinstall-find-actions-by-spec-x (spec dir)
1075 "Return a list of actions to do, controlled by SPEC."
1076 (declare (special
1077 load-path-element def-file add-to-load-path-p
1078 recurse-dirs-p block-in-dir block-in-subtree))
1080 (if (consp spec)
1081 (case (car spec)
1082 (all
1083 ;;(all . SPEC*)
1084 (apply #'nconc
1085 (mapcar
1086 #'(lambda (sub-spec)
1087 (elinstall-find-actions-by-spec-x
1088 sub-spec
1089 dir))
1090 (cdr spec))))
1091 (block-in-subtree
1092 ;;(block-in-subtree (* FN) SPEC)
1093 (let
1094 ((block-in-subtree (second spec)))
1095 (elinstall-find-actions-by-spec-x
1096 (third spec)
1097 dir)))
1099 ;;Rather than trying to bind all control variables each time
1100 ;;thru, we use `set' and `unwind-protect'.
1101 (control
1102 ;;control TYPE DISPOSITION SPEC
1103 (let
1104 ((key (second spec))
1105 old-value)
1106 (if (memq key elinstall-find-actions-control-vars)
1107 (unwind-protect
1108 (progn
1109 (setq old-value (symbol-value key))
1110 (set key (third spec))
1111 (elinstall-find-actions-by-spec-x
1112 (fourth spec)
1113 dir))
1114 (set key old-value))
1115 (error "Unrecognized control variable %s" key))))
1118 (def-file
1119 ;;(def-file FN ARGS SPEC)
1120 (let
1121 ((def-file
1122 (expand-file-name
1123 (second spec)
1124 dir))
1125 (for-preload (third spec)))
1126 (assert (listp for-preload))
1127 (append
1128 (list
1130 (and for-preload (car for-preload))
1131 `(preload-file
1132 ,(car for-preload)
1133 ,def-file
1134 ,@(cdr for-preload))
1135 '()))
1137 (elinstall-find-actions-by-spec-x
1138 (fourth spec) dir))))
1140 (dir
1141 ;;(dir FN)
1142 (elinstall-actions-for-dir
1143 (expand-file-name
1144 (second spec)
1145 dir)
1146 nil))
1148 (file
1149 ;;(file FN)
1150 (elinstall-actions-for-source-file
1151 (second spec) dir))
1154 ;;(in FN SPEC)
1155 (let
1156 ((new-dir
1157 (expand-file-name
1158 (second spec)
1159 dir)))
1161 (elinstall-find-actions-by-spec-x
1162 (third spec)
1163 new-dir)))
1165 (load-path
1166 ;;(load-path SPEC)
1167 (append
1168 `((add-to-load-path ,def-file ,dir))
1169 (let
1170 ((load-path-element dir)
1171 (add-to-load-path-p nil))
1172 (elinstall-find-actions-by-spec-x
1173 (second spec)
1174 dir))))
1176 (matching
1177 ;;(matching PATTERN SPEC)
1178 (apply #'nconc
1179 (mapcar
1180 #'(lambda (dir)
1181 ;;$$PUNT Assume that `block-in-subtree' and
1182 ;;`block-in-dir' aren't matched.
1183 (elinstall-find-actions-by-spec-x
1184 (third spec)
1185 dir))
1186 (directory-files
1187 dir t (second spec)))))
1188 (preload
1189 ;;(preload FN SYM &rest ARGS)
1190 (let
1191 ((key (third spec)))
1193 (and (symbolp key) (symbol-value key))
1194 `((preload-file
1196 ,(expand-file-name (second spec) dir)
1197 ,@(nthcdr 3 spec)))
1198 '()))))
1200 ;;Single symbols
1201 (case spec
1202 (dir
1203 (elinstall-actions-for-dir dir nil))
1204 ((t)
1205 (elinstall-actions-for-dir dir t)))))
1207 ;;;_ . elinstall-find-actions-by-spec
1208 (defun elinstall-find-actions-by-spec
1209 (spec load-path-element dir def-file redo-old)
1210 "Find the list of actions to do according to SPEC."
1212 (let
1214 (def-file-time (elinstall-file-mod-time def-file))
1215 (add-to-load-path-p t)
1216 (recurse-dirs-p t)
1217 (force-recompile-p nil)
1218 (byte-compile-p t)
1219 (block-in-dir '())
1220 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1222 (declare (special
1223 load-path-element def-file add-to-load-path-p
1224 recurse-dirs-p force-recompile-p byte-compile-p
1225 block-in-dir block-in-subtree))
1227 (elinstall-find-actions-by-spec-x spec dir)))
1229 ;;;_ . high-level work
1230 ;;;_ , elinstall-get-relevant-load-path
1231 (defun elinstall-get-relevant-load-path (actions)
1233 (delq nil
1234 (mapcar
1235 #'(lambda (act)
1236 (case (car act)
1237 (add-to-load-path
1238 (second act))
1239 (t nil)))
1240 actions)))
1241 ;;;_ , elinstall-get-deffile-list
1242 (defun elinstall-get-deffile-list (stages)
1243 "Get a list of deffile names"
1245 (mapcar
1246 #'car
1247 (elinstall-stages->build-deffiles stages)))
1248 ;;;_ , elinstall-x
1249 (defun elinstall-x (dir spec &optional force)
1250 "High-level worker function to install elisp files."
1251 (let*
1253 ;;This is just the default deffile, spec can override it.
1254 (def-file
1255 (elinstall-expand-deffile-name nil))
1256 (actions
1257 (elinstall-find-actions-by-spec
1258 spec
1261 def-file
1262 (eq force 'redo-old)))
1263 (stages (elinstall-segregate-actions actions))
1264 (use-load-path
1265 (elinstall-get-relevant-load-path
1266 actions)))
1268 (elinstall-stage-update-deffiles
1269 (elinstall-stages->build-deffiles stages)
1270 force
1271 use-load-path)
1272 (elinstall-stage-arrange-preloads
1273 (elinstall-stages->arrange-preloads stages)
1274 (elinstall-get-deffile-list stages)
1275 force)
1276 (elinstall-stage-byte-compile
1277 (elinstall-stages->byte-compile stages))
1279 ;;;_ , Entry points
1280 ;;;_ . elinstall
1281 ;;;###autoload
1282 (defun elinstall (project-name path spec &optional force version-string)
1283 "Install elisp files.
1284 They need not be a formal package.
1286 Parameters:
1288 PROJECT-NAME - the name of the project
1290 PATH - Path to the project.
1291 Suggestion: (elinstall-directory-true-name)
1293 SPEC - a spec for the autoloads etc to make. It can be as simple as
1294 \(dir \"\.\") for installing one directory.
1296 If FORCE is t, install a package even if it has already been
1297 installed. Other non-nil cases of FORCE are reserved for future
1298 development."
1300 (when
1302 force
1303 (not (elinstall-already-installed project-name))
1304 (yes-or-no-p (format "Re-install %s? " project-name)))
1305 (elinstall-x
1306 path
1307 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1308 force)
1309 (elinstall-record-installed project-name version-string)))
1313 ;;;_ . elinstall-update-directory-autoloads
1315 ;;The control variables and values of `force' that would stop other
1316 ;;actions don't exist yet. Similarly for
1317 ;;`elinstall-update-file-autoloads'.
1318 ;;;###autoload
1319 (defun elinstall-update-directory-autoloads (dir)
1320 "Update autoloads for directory DIR"
1322 (interactive "DUpdate autoloads for all elisp files from directory: ")
1323 (elinstall-x
1325 `(control byte-compile-p nil
1326 (dir "."))))
1328 ;;;_ . elinstall-update-directory
1329 ;;;###autoload
1330 (defun elinstall-update-directory (dir)
1331 "Update autoloads for directory DIR"
1333 (interactive "DInstall all elisp files from directory: ")
1334 (elinstall-x
1336 '(dir ".")))
1338 ;;;_ . elinstall-update-file-autoloads
1339 ;;;###autoload
1340 (defun elinstall-update-file-autoloads (file)
1341 "Update autoloads for elisp file FILE"
1343 (interactive "fUpdate autoloads for elisp file: ")
1344 (elinstall-x
1345 (file-name-directory file)
1346 `(control byte-compile-p nil
1347 (file ,(file-name-nondirectory file)))))
1349 ;;;_ . elinstall-update-file
1350 ;;;###autoload
1351 (defun elinstall-update-file (file)
1352 "Install elisp file FILE"
1354 (interactive "fInstall elisp file: ")
1355 (elinstall-x
1356 (file-name-directory file)
1357 `(file ,(file-name-nondirectory file))))
1359 ;;;_. Footers
1360 ;;;_ , Provides
1362 (provide 'elinstall)
1364 ;;;_ * Local emacs vars.
1365 ;;;_ + Local variables:
1366 ;;;_ + mode: allout
1367 ;;;_ + End:
1369 ;;;_ , End
1370 ;;; elinstall.el ends here