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