New signature for elinstall-proceed-p
[elinstall.git] / elinstall.el
blob9c41de43734b8227a0c983f85580c61cabf6f808
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-restrain-install
72 (defcustom elinstall-restrain-install '()
73 "Restrain elinstall for specific packages"
74 :group 'elinstall
75 :type
76 '(repeat
77 (list
78 (choice :format "%[%t%]: %v"
79 (string :tag "Package name")
80 (const :tag "All packages" t))
81 ;;Spec
82 )))
84 ;;;_ . elinstall-already-installed
85 (with-no-warnings
86 (defcustom elinstall-already-installed
87 '()
88 "(AUTOMATIC) Things that have already been installed.
89 This exists for recording what has been installed.
91 Though it's saved as customizable, user interaction is not
92 contemplated." ))
93 ;;Tell the byte-compiler it's a variable.
94 (defvar elinstall-already-installed)
95 ;;;_ , Types
96 ;;;_ . elinstall-stages
97 (defstruct (elinstall-stages
98 (:constructor elinstall-make-stages)
99 (:conc-name elinstall-stages->)
100 (:copier nil))
101 "The elinstall stages"
102 build-deffiles
103 run-tests
104 byte-compile
105 arrange-preloads)
106 ;;;_ , Data
107 ;;;_ . Regular expressions
108 ;;;_ , elinstall-elisp-regexp
109 (defconst elinstall-elisp-regexp
110 (let ((tmp nil))
111 (dolist
112 (suf (get-load-suffixes))
113 (unless (string-match "\\.elc" suf) (push suf tmp)))
114 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
115 "Regular expression that matches elisp files" )
116 ;;;_ , Utilities
117 ;;;_ . elinstall-file-mod-time
118 (defsubst elinstall-file-mod-time (file)
119 "Return the modification time of FILE"
120 (nth 5 (file-attributes file)))
122 ;;;_ . elinstall-directory-true-name
123 (defun elinstall-directory-true-name ()
124 "Get the true name of the directory the calling code lives in.
125 CAUTION: This is sensitive to where it's called. That's the point of it."
126 (file-name-directory
127 (if load-file-name
128 (file-truename load-file-name)
129 (file-truename buffer-file-name))))
130 ;;;_ . Checking installedness
131 ;;;_ , elinstall-get-installation-record
132 (defun elinstall-get-installation-record (project-name)
133 "Return the installation record for PROJECT-NAME."
135 (assoc project-name elinstall-already-installed))
137 ;;;_ , elinstall-already-installed
138 (defun elinstall-already-installed (project-name)
139 "Return non-nil if PROJECT-NAME has been installed."
140 (elinstall-get-installation-record project-name))
142 ;;;_ , elinstall-record-installed
143 (defun elinstall-record-installed (project-name &optional version)
144 "Record that PROJECT-NAME has been installed."
145 (let
146 ((new-item
147 (list
148 project-name
149 (or version "0")
150 (current-time)
151 'installed))
152 (old-item
153 (elinstall-get-installation-record project-name))
154 (print-length nil)
155 (print-level nil))
156 (when old-item
157 (setq elinstall-already-installed
158 (delete old-item elinstall-already-installed)))
159 (push new-item elinstall-already-installed)
160 (customize-save-variable
161 'elinstall-already-installed
162 elinstall-already-installed
163 "Set by elinstall-record-installed")))
164 ;;;_ . Finding deffiles
165 ;;;_ , elinstall-expand-deffile-name
166 (defun elinstall-expand-deffile-name (deffile)
167 "Expand DEFFILE autoload.el's way."
169 (expand-file-name (or deffile "loaddefs.el")
170 (expand-file-name "lisp"
171 source-directory)))
172 ;;;_ . Checking restraint specs
173 ;;;_ , elinstall-call-with-restraints
174 (defun elinstall-call-with-restraints (restraints package func &rest args)
175 "Call FUNC with ARGS, in scope of RESTRAINTS.
176 RESTRAINTS is a list of package restraints. User restraints for the
177 given package will also be applied in scope.
179 PACKAGE can be `t' or a string naming a package."
181 (let*
182 ((elinstall:*pkg-restraints* restraints)
183 ;;$$PUNT for now. Figure out user restraints for this
184 ;;package.
185 (elinstall:*user-restraints*
186 nil))
187 (declare (special
188 elinstall:*pkg-restraints*
189 elinstall:*user-restraints*))
190 (apply func args)))
191 ;;;_ ,
192 (defun elinstall-proceed-p
193 (topic message-params &optional already-p)
194 "Return non-nil if actions on TOPIC should proceed.
195 Call this transitively only thru `elinstall-call-with-restraints'.
196 TOPIC is a symbol indicating the topic, such as `byte-compile'.
197 MESSAGE-PARAMS is a cons of:
198 * A list of format strings:
199 * To ask whether to do this action
200 * To ask whether to redo this action, for `ask-for-old'
201 * To report that this action was skipped because already done.
202 * The arguments to the formatter.
203 ALREADY-P is an extended boolean whether the task has been done
204 before, if caller can tell."
206 (check-type topic symbol)
207 (declare (special
208 elinstall:*pkg-restraints*
209 elinstall:*user-restraints*))
210 (unless (and
211 (boundp 'elinstall:*pkg-restraints*)
212 (boundp 'elinstall:*user-restraints*))
213 (error "elinstall-proceed-p called out of scope"))
215 (destructuring-bind
216 ((ask-prompt &optional redo-prompt noredo-msg) &rest message-args)
217 message-params
218 (let*
219 ( ;;Get the applicable cell. We look in several places.
220 (cell (assq topic elinstall:*user-restraints*))
221 (cell (or cell (assq t elinstall:*user-restraints*)))
222 (cell
223 ;;`t' means use the pkg-restraints value instead.
224 (if
225 (or (not cell) (eq (second cell) t))
226 (assq topic elinstall:*pkg-restraints*)
227 cell))
228 (cell (or cell (assq t elinstall:*pkg-restraints*)))
229 (treatment
230 ;;Default is to just update.
231 (if cell (second cell) 'update)))
232 (case treatment
233 ((nil)
234 nil)
235 ((t always)
237 (update
238 (if already-p
239 (progn
240 (apply #'message noredo-msg message-args)
241 nil)
243 (ask-for-old
244 (if already-p
245 (y-or-n-p (apply #'format redo-prompt message-args))
247 (ask
248 (y-or-n-p
249 (apply #'format ask-prompt message-args)))))))
251 ;;;_ , Work
252 ;;;_ . Doing actions
254 ;;;_ , Doing autoload actions (adapted from autoload.el)
255 ;;;_ . Utilities about the action list
256 ;;;_ , elinstall-remove-autogen-action
257 (defun elinstall-remove-autogen-action (file actions)
258 "Return ACTIONS minus any add-file-autoloads on FILE removed."
260 (delq nil
261 (mapcar
262 #'(lambda (act)
263 (case (car act)
264 (add-file-autoloads
265 (if (equal file (third act))
267 act))
268 (t act)))
269 actions)))
270 ;;;_ , elinstall-get-autogen-action
271 (defun elinstall-get-autogen-action (file actions)
273 (let
274 ((the-act))
275 (dolist (act actions)
276 (case (car act)
277 (add-file-autoloads
278 (when (equal file (third act))
279 (setq the-act act)))))
280 the-act))
281 ;;;_ . About printing to autoload file
282 ;;;_ , elinstall-insert-section-header
283 (defun elinstall-insert-section-header (outbuf form)
284 "Insert the section-header line,
285 which lists the file name and which functions are in it, etc."
286 (insert generate-autoload-section-header)
287 (prin1 form outbuf)
288 (terpri outbuf)
289 ;; Break that line at spaces, to avoid very long lines.
290 ;; Make each sub-line into a comment.
291 (with-current-buffer outbuf
292 (save-excursion
293 (forward-line -1)
294 (while (not (eolp))
295 (move-to-column 64)
296 (skip-chars-forward "^ \n")
297 (or (eolp)
298 (insert "\n" generate-autoload-section-continuation))))))
300 ;;;_ , elinstall-insert-autoload-section
301 (defun elinstall-insert-autoload-section (text form &optional comment-string)
302 "Insert TEXT into current buffer as an autoload section"
304 (let* (
305 (print-length nil)
306 (print-level nil)
307 ;; This does something in Lucid Emacs.
308 (print-readably t)
309 (float-output-format nil))
311 (elinstall-insert-section-header (current-buffer) form)
312 (when comment-string
313 (insert ";;; " comment-string "\n"))
314 (insert text)
315 (insert generate-autoload-section-trailer)))
317 ;;;_ . Making autoloads
318 ;;;_ , elinstall-make-autoload-action
319 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
320 "Return the autoloads for current buffer as a string"
322 ;;We put all the text into a temp buffer, then get that buffer's
323 ;;string.
324 (let
325 ((outbuf
326 (generate-new-buffer " *temp*"))
327 (autoloads-done '())
328 (short-name
329 (file-name-nondirectory full-path))
331 (print-length nil)
332 (print-level nil)
333 ;; Apparently this does something in Lucid Emacs.
334 (print-readably t)
335 (float-output-format nil)
337 ;;load-name relative to a member of load-path.
338 (relative-name
339 (file-name-sans-extension
340 (file-relative-name
341 full-path
342 load-path-element))))
344 (with-current-buffer buf
345 (unwind-protect
346 (save-excursion
347 (save-restriction
348 (widen)
349 (goto-char (point-min))
350 (message "Finding autoloads for %s..." short-name)
351 (while (not (eobp))
352 (skip-chars-forward " \t\n\f")
353 (cond
354 ((looking-at (regexp-quote generate-autoload-cookie))
355 (search-forward generate-autoload-cookie)
356 (skip-chars-forward " \t")
357 (if (eolp)
358 ;; Read the next form and make an autoload.
359 (let* ((form (prog1 (read (current-buffer))
360 (or (bolp) (forward-line 1))))
361 (autoload
362 (make-autoload form relative-name)))
363 (if autoload
364 (push (nth 1 form) autoloads-done)
365 (setq autoload form))
366 (let ((autoload-print-form-outbuf outbuf))
367 (autoload-print-form autoload)))
369 ;; Copy the rest of the line to the output.
370 (princ (buffer-substring
371 (progn
372 ;; Back up over whitespace,
373 ;; to preserve it.
374 (skip-chars-backward " \f\t")
375 (if (= (char-after (1+ (point))) ? )
376 ;; Eat one space.
377 (forward-char 1))
378 (point))
379 (progn (forward-line 1) (point)))
380 outbuf)))
381 ((looking-at ";")
382 ;; Don't read the comment.
383 (forward-line 1))
385 (forward-sexp 1)
386 (forward-line 1))))
387 (message "Finding autoloads for %s...done" short-name))
389 ;;Return this action. The temp buffer's contents is
390 ;;our final string.
391 `(add-file-autoloads
392 ,def-file
393 ,relative-name
394 ,full-path
395 ,(with-current-buffer outbuf (buffer-string))
396 ,autoloads-done))
398 ;;This in unwind-protected
399 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
402 ;;;_ , elinstall-generate-file-autoloads
404 (defun elinstall-generate-file-autoloads
405 (relative-name full-name text autoloads-done)
406 "Insert at point a loaddefs autoload section for FILE.
407 Autoloads are generated for defuns and defmacros in FILE
408 marked by `generate-autoload-cookie' (which see).
409 If FILE is being visited in a buffer, the contents of the buffer
410 are used.
411 Return non-nil in the case where no autoloads were added at point.
413 FULL-NAME is the absolute name of the file.
414 RELATIVE-NAME is its name respective to some component of load-path."
415 (if (not (equal text ""))
416 ;; Insert the section-header line which lists the file name and
417 ;; which functions are in it, etc.
418 (elinstall-insert-autoload-section
419 text
420 (list 'autoloads
421 autoloads-done
422 relative-name
423 (autoload-trim-file-name full-name)
424 (elinstall-file-mod-time full-name))
425 (concat
426 "Generated autoloads from "
427 (autoload-trim-file-name full-name)))
430 ;;;_ , elinstall-deffile-insert-autoloads
431 (defun elinstall-deffile-insert-autoloads (file args)
432 "Update the autoloads for FILE in current buffer.
433 Return FILE if there was no autoload cookie in it, else nil.
435 Current buffer must be a loaddef-style file.
437 LOAD-NAME is the absolute name of the file.
438 RELATIVE-NAME is its name respective to some component of load-path."
439 (let (
440 (found nil)
441 (no-autoloads nil))
443 (save-excursion
444 (save-restriction
445 (widen)
446 (goto-char (point-min))
447 ;; Look for the section for FILE
448 (while (and (not found)
449 (search-forward generate-autoload-section-header nil t))
450 (let ((form (autoload-read-section-header)))
451 (cond
452 ((equal (nth 2 form) file)
453 ;; We found the section for this file.
454 (let ((begin (match-beginning 0)))
455 (progn
456 (search-forward generate-autoload-section-trailer)
457 (delete-region begin (point))
458 (setq found t))))
459 ((string< file (nth 2 form))
460 ;; We've come to a section alphabetically later than
461 ;; FILE. We assume the file is in order and so
462 ;; there must be no section for FILE. We will
463 ;; insert one before the section here.
464 (goto-char (match-beginning 0))
465 (setq found 'new)))))
466 (unless found
467 (progn
468 (setq found 'new)
469 ;; No later sections in the file. Put before the last page.
470 (goto-char (point-max))
471 (search-backward "\f" nil t)))
472 (setq no-autoloads
473 (apply #'elinstall-generate-file-autoloads
474 file args))))
476 (if no-autoloads file nil)))
477 ;;;_ . Arranging to add to info-path and load-path
478 ;;;_ , elinstall-generate-add-to-path
479 (defun elinstall-generate-add-to-path (path-element type)
480 "Insert code at point to add PATH-ELEMENT to a path.
481 If TYPE is:
482 * `add-to-load-path', add to load-path
483 * `add-to-info-path', add to Info-additional-directory-list
485 Current buffer must be a loaddef-style file."
486 (let ( (path-symbol
487 (case type
488 (add-to-load-path 'load-path)
489 (add-to-info-path 'Info-additional-directory-list)
490 (t (error "Type not recognized"))))
491 (description
492 (case type
493 (add-to-load-path "load-path")
494 (add-to-info-path "info-path")))
495 (autoloads-done '())
496 (print-length nil)
497 (print-level nil)
498 ;; This does something in Lucid Emacs.
499 (print-readably t)
500 (float-output-format nil))
502 (message "Generating %s additions..." description)
504 (elinstall-insert-autoload-section
505 (pp-to-string
506 `(add-to-list ',path-symbol
507 (expand-file-name
508 ,(file-relative-name path-element)
509 (if load-file-name
510 (file-name-directory
511 (file-truename load-file-name))))))
512 (list type (list path-element) nil nil nil)
513 nil)
514 (message "Generating %s additions...done" description)))
517 ;;;_ , elinstall-deffile-insert-add-to-path
518 (defun elinstall-deffile-insert-add-to-path (path-element type)
519 "Insert code in current buffer to add PATH-ELEMENT to a path.
520 If TYPE is:
521 * `add-to-load-path', add to load-path
522 * `add-to-info-path', add to Info-default-directory-list
524 Current buffer must be a loaddef-style file."
525 (let (
526 (found nil)
527 (no-autoloads nil))
529 (save-excursion
530 (save-restriction
531 (widen)
532 (goto-char (point-min))
533 ;; Look for the section for PATH-ELEMENT
534 (while (and (not found)
535 (search-forward generate-autoload-section-header nil t))
536 (let ((form (autoload-read-section-header)))
537 (cond
538 ((and
539 (equal (nth 0 form) type)
540 (member path-element (nth 1 form)))
542 ;; We found the section for this add.
543 (let ((begin (match-beginning 0)))
544 (progn
545 (search-forward generate-autoload-section-trailer)
546 (delete-region begin (point))
547 (setq found t)))))))
549 (unless found
550 (progn
551 (setq found 'new)
552 ;; No later sections in the file. Put before the last page.
553 (goto-char (point-max))
554 (search-backward "\f" nil t)))
556 (elinstall-generate-add-to-path path-element type)))
558 ;;This never belongs in the no-autoloads section.
559 nil))
560 ;;;_ . elinstall-deffile-insert
562 (defun elinstall-deffile-insert (action)
563 "Insert autoloads etc into current file according to ACTION.
564 The format of ACTION is described in the design docs.
566 Return filename if this action belongs in the no-autoload section."
568 (when action
569 (case (car action)
570 (add-file-autoloads
571 (elinstall-deffile-insert-autoloads
572 (third action)
573 (nthcdr 3 action)))
575 (add-to-load-path
576 (elinstall-deffile-insert-add-to-path
577 (third action)
578 'add-to-load-path)
579 nil)
581 (add-to-info-path
582 (elinstall-deffile-insert-add-to-path
583 (third action)
584 'add-to-info-path)
585 nil)
587 ((preload-file run-tests byte-compile)
588 (error "This case should not come here.")))))
590 ;;;_ . elinstall-prepare-deffile
591 (defun elinstall-prepare-deffile (deffile)
592 "Try to ensure that DEFFILE is available for receiving autoloads"
594 (autoload-ensure-default-file deffile)
595 (with-current-buffer (find-file-noselect deffile)
598 ;; We must read/write the file without any code conversion,
599 ;; but still decode EOLs.
600 (let ((coding-system-for-read 'raw-text))
602 ;; This is to make generated-autoload-file have Unix EOLs, so
603 ;; that it is portable to all platforms.
604 (setq buffer-file-coding-system 'raw-text-unix))
605 (or (> (buffer-size) 0)
606 (error "Autoloads file %s does not exist" buffer-file-name))
607 (or (file-writable-p buffer-file-name)
608 (error "Autoloads file %s is not writable"
609 buffer-file-name))))
611 ;;;_ . elinstall-update-deffile
613 ;;Adapted from autoload.el `update-directory-autoloads'.
615 (defun elinstall-update-deffile (target actions &optional
616 use-load-path force)
618 Update file TARGET with current autoloads as specified by ACTIONS.
619 Also remove any old definitions pointing to libraries that can no
620 longer be found.
622 ACTIONS must be a list of actions (See the format doc). Each one's
623 filename must be relative to some element of load-path.
625 USE-LOAD-PATH is a list to use as load-path. It should include
626 any new load-path that we are arranging to create. If it's not given,
627 load-path itself is used.
629 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
630 Other non-nil cases of FORCE are reserved for future development.
632 This uses `update-file-autoloads' (which see) to do its work.
633 In an interactive call, you must give one argument, the name
634 of a single directory."
635 (let
637 (use-load-path (or use-load-path load-path))
638 (this-time (current-time))
639 ;;files with no autoload cookies.
640 (no-autoloads nil))
642 (elinstall-prepare-deffile target)
643 (with-current-buffer
644 (find-file-noselect target)
645 (save-excursion
646 (setq actions
647 (elinstall-remove-autogen-action
648 (autoload-trim-file-name target)
649 actions))
651 (goto-char (point-min))
652 (while (search-forward generate-autoload-section-header nil t)
653 (let* ((form (autoload-read-section-header))
654 (file (nth 3 form)))
655 (cond ((and (consp file) (stringp (car file)))
656 ;; This is a list of files that have no
657 ;; autoload cookies.
658 ;; There shouldn't be more than one such entry.
659 ;; Remove the obsolete section.
660 (autoload-remove-section (match-beginning 0))
661 (let ((last-time (nth 4 form)))
662 (dolist (file file)
663 (let ((file-time (elinstall-file-mod-time file)))
664 (when (and file-time
665 (not (time-less-p last-time file-time)))
666 ;; file unchanged
667 (push file no-autoloads)
668 (setq actions
669 (elinstall-remove-autogen-action
670 file actions)))))))
671 ((not (stringp file)))
673 (let
674 ((file-path
675 (locate-library file nil use-load-path)))
676 (cond
677 ;;File doesn't exist, so remove its
678 ;;section.
679 ((not file-path)
680 (autoload-remove-section
681 (match-beginning 0)))
683 ;; File hasn't changed, so do nothing.
684 ((equal
685 (nth 4 form)
686 (elinstall-file-mod-time file-path))
687 nil)
689 (elinstall-deffile-insert
690 (elinstall-get-autogen-action
691 file actions))))
693 (setq actions
694 (elinstall-remove-autogen-action
695 file actions))))))))
697 ;; Remaining actions have no existing autoload sections yet.
698 (setq no-autoloads
699 (append no-autoloads
700 (delq nil (mapcar #'elinstall-deffile-insert actions))))
701 (when no-autoloads
702 ;; Sort them for better readability.
703 (setq no-autoloads (sort no-autoloads 'string<))
704 ;; Add the `no-autoloads' section.
705 (goto-char (point-max))
706 (search-backward "\f" nil t)
707 (elinstall-insert-autoload-section
709 (list 'autoloads nil nil no-autoloads this-time)))
710 (save-buffer))))
712 ;;;_ . elinstall-stage-update-deffiles
713 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
714 "Update any deffiles mentioned in SEGMENT-LIST.
715 FORCE and USE-LOAD-PATH have the same meaning as in
716 `elinstall-update-deffile'.
718 (mapcar
719 #'(lambda (segment)
720 (let*
721 ((deffile (car segment)))
722 (if (stringp deffile)
723 (elinstall-update-deffile deffile (cdr segment) force
724 use-load-path))))
725 segment-list))
727 ;;;_ , Doing actions to arrange preloads
728 ;;;_ . elinstall-symlink-on-emacs-start
729 (defun elinstall-symlink-on-emacs-start
730 (filename target-basename target-dir &optional priority force)
731 "Symlink to TARGET-BASENAME.el in TARGET-DIR
733 If PRIORITY is given, it will be used as the priority prefix,
734 otherwise elinstall-default-priority will be.
735 PRIORITY must be an integer or nil.
736 If FORCE is `t', do it regardless of timestamps etc.
737 Other non-nil cases of FORCE are reserved for future development."
738 (let*
740 (priority (or priority elinstall-default-priority))
741 (target-name-nodir
742 (format
743 "%d%s.el"
744 priority
745 target-basename))
746 (target
747 (expand-file-name target-name-nodir target-dir)))
750 (cond
751 ;;Path should already exist.
752 ((not
753 (file-exists-p target-dir))
754 (message "The target directory doesn't exist."))
755 ;;Target shouldn't already exist, but if force is given, let
756 ;;user override.
757 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
758 ;;do nothing even on force.
759 ((and
760 (file-exists-p target)
762 (not force)
763 (not
764 (yes-or-no-p
765 (format "Really overwrite %s? " target))))
766 (message "File %s already exists" target)))
769 (make-symbolic-link
770 filename
771 target
772 nil)))))
774 ;;;_ . elinstall-add-to-dot-emacs
775 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
776 "Add code to load FILENAME to .emacs.
777 FILENAME should not have an extension"
779 ;;Visit .emacs
780 (with-current-buffer (find-file-noselect dot-emacs-name)
781 (save-excursion
782 ;;add at end of file
783 (goto-char (point-max))
784 (insert "\n;;Added by elinstall")
785 (insert "\n;;Consider using my-site-start to manage .emacs\n")
786 (pp `(load ,filename) (current-buffer))
787 (save-buffer))))
790 ;;;_ . elinstall-arrange-preload
791 ;;;###autoload
792 (defun elinstall-arrange-preload (force filename basename &optional priority)
793 "Arrange for FILENAME to be loaded on emacs start.
794 FORCE has its usual meaning.
795 BASENAME and PRIORITY are used as arguments to
796 `elinstall-symlink-on-emacs-start'.
799 (let
800 ((preload-target elinstall-default-preload-target))
802 ;;Dispatch the possibilities.
803 (cond
804 ((eq preload-target 'dot-emacs)
805 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
806 ((stringp preload-target)
807 (elinstall-symlink-on-emacs-start
808 filename basename preload-target priority force))
809 ((null preload-target)
810 (message "Not arranging for preloads"))
812 (message "I don't recognize that")))))
813 ;;;_ . elinstall-stage-arrange-preloads
814 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
815 "Arrange any preloads mentioned in ACTIONS."
817 (mapcar
818 #'(lambda (act)
819 (case (car act)
820 (preload-file
821 (let*
822 ( (filename
823 (caddr act))
824 (proceed-p
825 (case (second act)
826 ((t) t)
827 ((nil) nil)
828 (if-used
829 (member filename deffiles-used)))))
831 (when proceed-p
832 (apply
833 #'elinstall-arrange-preload
834 force
835 (cddr act)))))
837 (error
838 "elinstall-stage-arrange-preloads: Action not
839 recognized."))) )
840 actions))
843 ;;;_ , Run tests
844 ;;;_ . elinstall-stage-run-tests
845 (defun elinstall-stage-run-tests (actions)
846 "Run any tests mentioned in ACTIONS."
848 (mapcar
849 #'(lambda (act)
850 (case (car act)
851 (run-tests
852 ;;$$WRITE ME - not a high priority right now.
853 nil)
855 (error
856 "elinstall-stage-run-tests: Action not
857 recognized."))) )
858 actions))
861 ;;;_ , Byte compile
862 ;;;_ . elinstall-stage-byte-compile
863 (defun elinstall-stage-byte-compile (actions)
864 "Do any byte-compilation mentioned in ACTIONS."
866 (mapcar
867 #'(lambda (act)
868 (case (car act)
869 ;;$$IMPROVE ME Understand flags to control second
870 ;;argument (whether to load file after
871 ;;compilation)
872 (byte-compile
873 (byte-compile-file (second act)))
875 (error
876 "elinstall-stage-byte-compile: Action not
877 recognized."))) )
878 actions))
879 ;;;_ . Segregating actions
880 ;;;_ , elinstall-remove-empty-segs
881 (defun elinstall-remove-empty-segs (segment-list)
882 "Return SEGMENT-LIST minus any segments that have no actions.
883 Intended only for the deffile stage data."
884 (delq nil
885 (mapcar
886 #'(lambda (segment)
887 (if (cdr segment)
888 segment
889 nil))
890 segment-list)))
892 ;;;_ , elinstall-segregate-actions
893 (defun elinstall-segregate-actions (actions)
894 "Return actions segregated by deffile.
896 Returns a list whose elements are each a cons of:
897 * deffile filename or nil
898 * A list of actions to be done for that deffile."
900 (let
902 (build-deffiles '())
903 (run-tests '())
904 (byte-compile '())
905 (arrange-preloads '()))
907 (dolist (act actions)
908 (when act
909 (case (car act)
910 ((add-file-autoloads
911 add-to-info-path
912 add-to-load-path)
913 (let*
914 ((deffile-name (second act))
915 (cell-already
916 (assoc deffile-name build-deffiles)))
917 (if cell-already
918 ;;There are already actions on this deffile.
919 ;;Splice this action in.
920 (setcdr cell-already
921 (cons act (cdr cell-already)))
922 ;;There are no actions on this deffile. Add a
923 ;;place for them and include this action.
924 (push (list deffile-name act) build-deffiles))))
925 (preload-file
926 (push act arrange-preloads))
927 (run-tests
928 (push act run-tests))
929 (byte-compile
930 (push act byte-compile)))))
932 (elinstall-make-stages
933 :build-deffiles
934 (elinstall-remove-empty-segs build-deffiles)
935 :run-tests
936 run-tests
937 :byte-compile
938 byte-compile
939 :arrange-preloads
940 arrange-preloads)))
941 ;;;_ . Finding actions
942 ;;;_ , Utility
943 ;;;_ . elinstall-dir-has-info
945 ;;$$IMPROVE ME - Can this test be made more precise?
946 (defun elinstall-dir-has-info (dir)
947 "Return non-nil if DIR has info files in it.
948 DIR should be an absolute path."
950 (string-match "/info/" dir)
951 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
952 ;;;_ . elinstall-directory-files
953 (defun elinstall-directory-files (dirname)
954 "Return a list of files in directory DIRNAME, minus certain files.
955 The following files are omitted:
956 * Dot files
957 * Files that match an entry in `block-in-subtree'.
958 Filenames are relative."
959 (declare (special
960 def-file block-in-dir block-in-subtree))
961 (let*
963 ;;Relative filenames of this directory's files.
964 (all-files
965 ;; Don't include dot files. If user really wants to
966 ;;explore one he can use (dir ".NAME") or (file ".NAME")
967 (directory-files dirname nil "[^\\.]"))
968 ;;We know our def-file isn't really source so remove it.
969 ;;We'd have removed it anyways after seeing file local vars.
970 (all-files
971 (remove def-file all-files))
973 (all-files
974 (delq nil
975 (mapcar
976 #'(lambda (filename)
978 (and
979 block-in-subtree
980 (some
981 #'(lambda (blocked)
982 (string-match blocked filename))
983 block-in-subtree))
985 filename))
986 all-files))))
987 all-files))
990 ;;;_ , Workers
991 ;;;_ . List of special variables used in this section
992 ;;load-path-element - The relevant element of load-path
993 ;;def-file - The file the autoload definitions etc will go into.
994 ;;add-to-load-path-p - Controls whether to add to load-path.
995 ;;recurse-dirs-p - Whether to recurse into subdirectories.
997 ;;block-in-dir - (NOT IMPLEMENTED) List of filenames to reject. They
998 ;;may include wildcards. They apply wrt the original directory.
1000 ;;block-in-subtree - (NOT IMPLEMENTED) List of filenames to reject.
1001 ;;They may include wildcards. They apply wrt any directory in the
1002 ;;tree. Specifically, in the spec tree, which may differ from the
1003 ;;file subtree.
1004 ;;byte-compile - whether to byte-compile at all, t by default.
1005 ;;autoloads - boolean whether to make autoloads, t by default.
1006 ;;preloads - boolean whether to set up preloads, t by default.
1007 (defconst elinstall-find-actions-control-vars
1008 '(add-to-load-path-p recurse-dirs-p byte-compile
1009 autoloads preloads)
1010 "Control special variables that the find-actions tree recognizes" )
1011 ;;;_ . elinstall-actions-for-source-file
1012 (defun elinstall-actions-for-source-file (filename dir)
1013 "Return a list of actions to do for FILENAME in DIR.
1014 Special variables are as noted in \"List of special variables\"."
1015 (declare (special
1016 load-path-element def-file byte-compile))
1017 (let
1018 ((full-path
1019 (expand-file-name filename dir)))
1020 (when
1021 (and
1022 (file-readable-p full-path)
1023 (not (auto-save-file-name-p full-path)))
1024 ;;$$IMPROVE ME Use more of the control variables.
1025 (let*
1027 (visited (get-file-buffer full-path))
1028 (buf
1029 (or
1030 visited
1031 ;;Visit the file cheaply.
1032 ;;hack-local-variables can give errors.
1033 (ignore-errors (autoload-find-file full-path))))
1034 (def-file
1036 (ignore-errors
1037 (with-current-buffer buf
1038 (if (local-variable-p 'generated-autoload-file)
1039 (elinstall-expand-deffile-name
1040 generated-autoload-file)
1041 nil)))
1042 def-file))
1043 ;;Figure out whether to run some actions, by file local vars.
1044 (autoloads-p
1045 (ignore-errors
1046 (with-current-buffer buf
1047 (not no-update-autoloads))))
1048 (do-compile-p
1049 (and
1050 byte-compile
1051 (featurep 'byte-compile)
1052 (string-match emacs-lisp-file-regexp filename)
1053 (ignore-errors
1054 (with-current-buffer buf
1055 (not no-byte-compile)))
1056 (elinstall-proceed-p 'byte-compile
1057 (list
1058 '( "Compile %s? "
1059 "Recompile %s? "
1060 "Already compiled %s.")
1061 filename)
1062 (let
1063 ((dest (byte-compile-dest-file full-path)))
1064 (and
1065 (file-exists-p dest)
1066 (file-newer-than-file-p full-path dest)))))))
1068 (prog1
1069 (list
1070 (if do-compile-p
1071 `(byte-compile ,full-path)
1072 nil)
1073 (if autoloads-p
1074 (elinstall-make-autoload-action
1075 buf def-file load-path-element full-path)
1076 nil))
1077 (unless visited (kill-buffer-if-not-modified buf)))))))
1078 ;;;_ . elinstall-actions-for-dir
1079 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
1080 "Make actions for DIR.
1081 Recurse just if RECURSE-DIRS-P"
1082 (declare (special
1083 load-path-element def-file add-to-load-path-p))
1084 ;;This does not treat symlinks specially. $$IMPROVE ME it could
1085 ;;treat/not treat them conditional on control variables.
1086 (let*
1088 (files (elinstall-directory-files dirname))
1090 ;;Relative filenames of elisp source
1091 (elisp-source-files
1092 (delq nil
1093 (mapcar
1094 #'(lambda (filename)
1096 (string-match elinstall-elisp-regexp filename)
1097 filename
1098 nil))
1099 files)))
1101 ;;Absolute filenames of subdirectories.
1102 (sub-dirs
1103 (if recurse-dirs-p
1104 (delq nil
1105 (mapcar
1106 #'(lambda (filename)
1107 (let
1108 ((fn (expand-file-name filename dirname)))
1110 (file-directory-p fn)
1112 nil)))
1113 files))
1114 '()))
1116 (load-path-here-p
1117 (and
1118 elisp-source-files ;;If list is not empty.
1119 add-to-load-path-p))
1120 (load-path-element
1121 (if load-path-here-p
1122 dirname
1123 load-path-element)))
1125 (append
1126 ;;Sometimes arrange to add this directory to load-path.
1127 (if load-path-here-p
1128 `((add-to-load-path
1129 ,def-file
1130 ,load-path-element))
1131 '())
1133 ;;$$IMPROVE ME - be controlled by a control variable.
1134 ;;Sometimes add this directory to info path.
1136 (elinstall-dir-has-info dirname)
1137 `((add-to-info-path
1138 ,def-file
1139 "."))
1140 '())
1142 (apply #'nconc
1143 (mapcar
1144 #'(lambda (filename)
1145 (elinstall-actions-for-source-file
1146 filename
1147 dirname))
1148 elisp-source-files))
1150 (if recurse-dirs-p
1151 (apply #'nconc
1152 (mapcar
1153 #'(lambda (filename)
1154 (elinstall-find-actions-by-spec-x
1156 (expand-file-name
1157 filename
1158 dirname)))
1159 sub-dirs))
1160 '()))))
1162 ;;;_ . elinstall-find-actions-by-spec-x
1164 (defun elinstall-find-actions-by-spec-x (spec dir)
1165 "Return a list of actions to do, controlled by SPEC."
1166 (declare (special
1167 load-path-element def-file add-to-load-path-p
1168 recurse-dirs-p block-in-dir block-in-subtree))
1170 (if (consp spec)
1171 (case (car spec)
1172 (all
1173 ;;(all . SPEC*)
1174 (apply #'nconc
1175 (mapcar
1176 #'(lambda (sub-spec)
1177 (elinstall-find-actions-by-spec-x
1178 sub-spec
1179 dir))
1180 (cdr spec))))
1181 (block-in-subtree
1182 ;;(block-in-subtree (* FN) SPEC)
1183 (let
1184 ((block-in-subtree (second spec)))
1185 (elinstall-find-actions-by-spec-x
1186 (third spec)
1187 dir)))
1189 ;;Rather than trying to bind all control variables each time
1190 ;;thru, we use `set' and `unwind-protect'.
1191 (control
1192 ;;control TYPE DISPOSITION SPEC
1193 (let
1194 ((key (second spec))
1195 old-value)
1196 (if (memq key elinstall-find-actions-control-vars)
1197 (unwind-protect
1198 (progn
1199 (setq old-value (symbol-value key))
1200 (set key (third spec))
1201 (elinstall-find-actions-by-spec-x
1202 (fourth spec)
1203 dir))
1204 (set key old-value))
1205 (error "Unrecognized control variable %s" key))))
1208 (def-file
1209 ;;(def-file FN ARGS SPEC)
1210 (let
1211 ((def-file
1212 (expand-file-name
1213 (second spec)
1214 dir))
1215 (for-preload (third spec)))
1216 (assert (listp for-preload))
1217 (append
1218 (list
1220 (and for-preload (car for-preload))
1221 `(preload-file
1222 ,(car for-preload)
1223 ,def-file
1224 ,@(cdr for-preload))
1225 '()))
1227 (elinstall-find-actions-by-spec-x
1228 (fourth spec) dir))))
1230 (dir
1231 ;;(dir FN)
1232 (elinstall-actions-for-dir
1233 (expand-file-name
1234 (second spec)
1235 dir)
1236 nil))
1238 (file
1239 ;;(file FN)
1240 (elinstall-actions-for-source-file
1241 (second spec) dir))
1244 ;;(in FN SPEC)
1245 (let
1246 ((new-dir
1247 (expand-file-name
1248 (second spec)
1249 dir)))
1251 (elinstall-find-actions-by-spec-x
1252 (third spec)
1253 new-dir)))
1255 (load-path
1256 ;;(load-path SPEC)
1257 (append
1258 `((add-to-load-path ,def-file ,dir))
1259 (let
1260 ((load-path-element dir)
1261 (add-to-load-path-p nil))
1262 (elinstall-find-actions-by-spec-x
1263 (second spec)
1264 dir))))
1266 (matching
1267 ;;(matching PATTERN SPEC)
1268 (apply #'nconc
1269 (mapcar
1270 #'(lambda (dir)
1271 ;;$$PUNT Assume that `block-in-subtree' and
1272 ;;`block-in-dir' aren't matched.
1273 (elinstall-find-actions-by-spec-x
1274 (third spec)
1275 dir))
1276 (directory-files
1277 dir t (second spec)))))
1278 (preload
1279 ;;(preload FN SYM &rest ARGS)
1280 (let
1281 ((key (third spec)))
1283 (and (symbolp key) (symbol-value key))
1284 `((preload-file
1286 ,(expand-file-name (second spec) dir)
1287 ,@(nthcdr 3 spec)))
1288 '()))))
1290 ;;Single symbols
1291 (case spec
1292 (dir
1293 (elinstall-actions-for-dir dir nil))
1294 ((t)
1295 (elinstall-actions-for-dir dir t)))))
1297 ;;;_ . elinstall-find-actions-by-spec
1298 (defun elinstall-find-actions-by-spec
1299 (spec load-path-element dir def-file redo-old)
1300 "Find the list of actions to do according to SPEC."
1302 (let
1304 (def-file-time (elinstall-file-mod-time def-file))
1305 (add-to-load-path-p t)
1306 (recurse-dirs-p t)
1307 (byte-compile t)
1308 (autoloads t)
1309 (preloads t)
1310 (block-in-dir '())
1311 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1313 (declare (special
1314 load-path-element def-file add-to-load-path-p
1315 recurse-dirs-p byte-compile
1316 block-in-dir block-in-subtree))
1318 (elinstall-find-actions-by-spec-x spec dir)))
1320 ;;;_ . high-level work
1321 ;;;_ , elinstall-get-relevant-load-path
1322 (defun elinstall-get-relevant-load-path (actions)
1324 (delq nil
1325 (mapcar
1326 #'(lambda (act)
1327 (case (car act)
1328 (add-to-load-path
1329 (second act))
1330 (t nil)))
1331 actions)))
1332 ;;;_ , elinstall-get-deffile-list
1333 (defun elinstall-get-deffile-list (stages)
1334 "Get a list of deffile names"
1336 (mapcar
1337 #'car
1338 (elinstall-stages->build-deffiles stages)))
1339 ;;;_ , elinstall-x
1340 (defun elinstall-x (dir spec &optional force)
1341 "High-level worker function to install elisp files."
1342 (let*
1344 ;;This is just the default deffile, spec can override it.
1345 (def-file
1346 (elinstall-expand-deffile-name nil))
1347 (actions
1348 (elinstall-find-actions-by-spec
1349 spec
1352 def-file
1353 (eq force 'redo-old)))
1354 (stages (elinstall-segregate-actions actions))
1355 (use-load-path
1356 (elinstall-get-relevant-load-path
1357 actions)))
1359 (elinstall-stage-update-deffiles
1360 (elinstall-stages->build-deffiles stages)
1361 force
1362 use-load-path)
1363 (elinstall-stage-arrange-preloads
1364 (elinstall-stages->arrange-preloads stages)
1365 (elinstall-get-deffile-list stages)
1366 force)
1367 (elinstall-stage-byte-compile
1368 (elinstall-stages->byte-compile stages))
1370 ;;;_ , elinstall-package
1371 (defun elinstall-package (project-name path spec version-string)
1372 "Install elisp files. See doc for `elinstall'."
1373 (when
1374 (elinstall-proceed-p 'install
1375 (list
1376 '("Install %s? "
1377 "Re-install %s? "
1378 "Already installed %s.")
1379 project-name)
1380 (elinstall-already-installed project-name))
1381 (elinstall-x
1382 path
1383 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1384 force)
1385 (elinstall-record-installed project-name version-string)))
1387 ;;;_ , Entry points
1388 ;;;_ . elinstall
1389 ;;;###autoload
1390 (defun elinstall (project-name path spec &optional force version-string)
1391 "Install elisp files.
1392 They need not be a formal package.
1394 Parameters:
1396 PROJECT-NAME - the name of the project
1398 PATH - Path to the project.
1399 Suggestion: Use (elinstall-directory-true-name) to get the real
1400 current directoery name even from loaded files.
1402 SPEC - a spec for the autoloads etc to make. It can be as simple
1403 as `t'.
1405 If FORCE is `t', install a package even if it has already been
1406 installed. If it's a list or `nil', it's treated as a list of
1407 installation restraints. User customizations override this
1408 argument.
1410 VERSION-STRING, if given, must be a string of the version for this package."
1412 (elinstall-call-with-restraints
1413 (if (eq force t)
1414 '((install always))
1415 force)
1416 project-name
1417 #'elinstall-package
1418 project-name path spec version-string))
1420 ;;;_ . elinstall-update-directory-autoloads
1422 ;;;###autoload
1423 (defun elinstall-update-directory-autoloads (dir)
1424 "Update autoloads for directory DIR"
1426 (interactive "DUpdate autoloads for all elisp files from directory: ")
1427 (elinstall-call-with-restraints
1428 '((autoloads t)
1429 (t nil))
1431 #'elinstall-x
1433 '(dir ".")))
1435 ;;;_ . elinstall-update-directory
1436 ;;;###autoload
1437 (defun elinstall-update-directory (dir)
1438 "Update autoloads for directory DIR"
1440 (interactive "DInstall all elisp files from directory: ")
1441 (elinstall-call-with-restraints
1444 #'elinstall-x
1446 '(dir ".")))
1448 ;;;_ . elinstall-update-file-autoloads
1449 ;;;###autoload
1450 (defun elinstall-update-file-autoloads (file)
1451 "Update autoloads for elisp file FILE"
1453 (interactive "fUpdate autoloads for elisp file: ")
1454 (elinstall-call-with-restraints
1457 #'elinstall-x
1458 (file-name-directory file)
1459 `(file ,(file-name-nondirectory file))))
1461 ;;;_ . elinstall-update-file
1462 ;;;###autoload
1463 (defun elinstall-update-file (file)
1464 "Install elisp file FILE"
1466 (interactive "fInstall elisp file: ")
1467 (elinstall-call-with-restraints
1468 '((autoloads t)
1469 (t nil))
1471 #'elinstall-x
1472 (file-name-directory file)
1473 `(file ,(file-name-nondirectory file))))
1475 ;;;_. Footers
1476 ;;;_ , Provides
1478 (provide 'elinstall)
1480 ;;;_ * Local emacs vars.
1481 ;;;_ + Local variables:
1482 ;;;_ + mode: allout
1483 ;;;_ + End:
1485 ;;;_ , End
1486 ;;; elinstall.el ends here