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