Towards having elinstall-proceed-p take simpler parameters
[elinstall.git] / elinstall.el
blobe3bd056e1a14110d1ca32a19a78c023198a7874a
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 ask-prompt &optional already-p redo-prompt noredo-msg)
194 "Return non-nil if actions on TOPIC should proceed.
195 Call this transitively only thru `elinstall-call-with-restraints'"
197 (check-type topic symbol)
198 (declare (special
199 elinstall:*pkg-restraints*
200 elinstall:*user-restraints*))
201 (unless (and
202 (boundp 'elinstall:*pkg-restraints*)
203 (boundp 'elinstall:*user-restraints*))
204 (error "elinstall-proceed-p called out of scope"))
206 (let*
207 ( (cell (assq topic elinstall:*user-restraints*))
208 (cell (or cell (assq t elinstall:*user-restraints*)))
209 (cell
210 ;;`t' means use the pkg-restraints value instead.
211 (if
212 (or (not cell) (eq (second cell) t))
213 (assq topic elinstall:*pkg-restraints*)
214 cell))
215 (cell (or cell (assq t elinstall:*pkg-restraints*)))
216 ;;Default is to just update.
217 (treatment
218 (if cell (second cell) 'update)))
219 (case treatment
220 ((nil)
221 nil)
222 ((t always)
224 (update
225 (if already-p
226 (progn
227 (message noredo-msg)
228 nil)
230 (ask-for-old
231 (if already-p
232 (y-or-n-p redo-prompt)
234 (ask
235 (y-or-n-p ask-prompt)))))
237 ;;;_ , Work
238 ;;;_ . Doing actions
240 ;;;_ , Doing autoload actions (adapted from autoload.el)
241 ;;;_ . Utilities about the action list
242 ;;;_ , elinstall-remove-autogen-action
243 (defun elinstall-remove-autogen-action (file actions)
244 "Return ACTIONS minus any add-file-autoloads on FILE removed."
246 (delq nil
247 (mapcar
248 #'(lambda (act)
249 (case (car act)
250 (add-file-autoloads
251 (if (equal file (third act))
253 act))
254 (t act)))
255 actions)))
256 ;;;_ , elinstall-get-autogen-action
257 (defun elinstall-get-autogen-action (file actions)
259 (let
260 ((the-act))
261 (dolist (act actions)
262 (case (car act)
263 (add-file-autoloads
264 (when (equal file (third act))
265 (setq the-act act)))))
266 the-act))
267 ;;;_ . About printing to autoload file
268 ;;;_ , elinstall-insert-section-header
269 (defun elinstall-insert-section-header (outbuf form)
270 "Insert the section-header line,
271 which lists the file name and which functions are in it, etc."
272 (insert generate-autoload-section-header)
273 (prin1 form outbuf)
274 (terpri outbuf)
275 ;; Break that line at spaces, to avoid very long lines.
276 ;; Make each sub-line into a comment.
277 (with-current-buffer outbuf
278 (save-excursion
279 (forward-line -1)
280 (while (not (eolp))
281 (move-to-column 64)
282 (skip-chars-forward "^ \n")
283 (or (eolp)
284 (insert "\n" generate-autoload-section-continuation))))))
286 ;;;_ , elinstall-insert-autoload-section
287 (defun elinstall-insert-autoload-section (text form &optional comment-string)
288 "Insert TEXT into current buffer as an autoload section"
290 (let* (
291 (print-length nil)
292 (print-level nil)
293 ;; This does something in Lucid Emacs.
294 (print-readably t)
295 (float-output-format nil))
297 (elinstall-insert-section-header (current-buffer) form)
298 (when comment-string
299 (insert ";;; " comment-string "\n"))
300 (insert text)
301 (insert generate-autoload-section-trailer)))
303 ;;;_ . Making autoloads
304 ;;;_ , elinstall-make-autoload-action
305 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
306 "Return the autoloads for current buffer as a string"
308 ;;We put all the text into a temp buffer, then get that buffer's
309 ;;string.
310 (let
311 ((outbuf
312 (generate-new-buffer " *temp*"))
313 (autoloads-done '())
314 (short-name
315 (file-name-nondirectory full-path))
317 (print-length nil)
318 (print-level nil)
319 ;; Apparently this does something in Lucid Emacs.
320 (print-readably t)
321 (float-output-format nil)
323 ;;load-name relative to a member of load-path.
324 (relative-name
325 (file-name-sans-extension
326 (file-relative-name
327 full-path
328 load-path-element))))
330 (with-current-buffer buf
331 (unwind-protect
332 (save-excursion
333 (save-restriction
334 (widen)
335 (goto-char (point-min))
336 (message "Finding autoloads for %s..." short-name)
337 (while (not (eobp))
338 (skip-chars-forward " \t\n\f")
339 (cond
340 ((looking-at (regexp-quote generate-autoload-cookie))
341 (search-forward generate-autoload-cookie)
342 (skip-chars-forward " \t")
343 (if (eolp)
344 ;; Read the next form and make an autoload.
345 (let* ((form (prog1 (read (current-buffer))
346 (or (bolp) (forward-line 1))))
347 (autoload
348 (make-autoload form relative-name)))
349 (if autoload
350 (push (nth 1 form) autoloads-done)
351 (setq autoload form))
352 (let ((autoload-print-form-outbuf outbuf))
353 (autoload-print-form autoload)))
355 ;; Copy the rest of the line to the output.
356 (princ (buffer-substring
357 (progn
358 ;; Back up over whitespace,
359 ;; to preserve it.
360 (skip-chars-backward " \f\t")
361 (if (= (char-after (1+ (point))) ? )
362 ;; Eat one space.
363 (forward-char 1))
364 (point))
365 (progn (forward-line 1) (point)))
366 outbuf)))
367 ((looking-at ";")
368 ;; Don't read the comment.
369 (forward-line 1))
371 (forward-sexp 1)
372 (forward-line 1))))
373 (message "Finding autoloads for %s...done" short-name))
375 ;;Return this action. The temp buffer's contents is
376 ;;our final string.
377 `(add-file-autoloads
378 ,def-file
379 ,relative-name
380 ,full-path
381 ,(with-current-buffer outbuf (buffer-string))
382 ,autoloads-done))
384 ;;This in unwind-protected
385 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
388 ;;;_ , elinstall-generate-file-autoloads
390 (defun elinstall-generate-file-autoloads
391 (relative-name full-name text autoloads-done)
392 "Insert at point a loaddefs autoload section for FILE.
393 Autoloads are generated for defuns and defmacros in FILE
394 marked by `generate-autoload-cookie' (which see).
395 If FILE is being visited in a buffer, the contents of the buffer
396 are used.
397 Return non-nil in the case where no autoloads were added at point.
399 FULL-NAME is the absolute name of the file.
400 RELATIVE-NAME is its name respective to some component of load-path."
401 (if (not (equal text ""))
402 ;; Insert the section-header line which lists the file name and
403 ;; which functions are in it, etc.
404 (elinstall-insert-autoload-section
405 text
406 (list 'autoloads
407 autoloads-done
408 relative-name
409 (autoload-trim-file-name full-name)
410 (elinstall-file-mod-time full-name))
411 (concat
412 "Generated autoloads from "
413 (autoload-trim-file-name full-name)))
416 ;;;_ , elinstall-deffile-insert-autoloads
417 (defun elinstall-deffile-insert-autoloads (file args)
418 "Update the autoloads for FILE in current buffer.
419 Return FILE if there was no autoload cookie in it, else nil.
421 Current buffer must be a loaddef-style file.
423 LOAD-NAME is the absolute name of the file.
424 RELATIVE-NAME is its name respective to some component of load-path."
425 (let (
426 (found nil)
427 (no-autoloads nil))
429 (save-excursion
430 (save-restriction
431 (widen)
432 (goto-char (point-min))
433 ;; Look for the section for FILE
434 (while (and (not found)
435 (search-forward generate-autoload-section-header nil t))
436 (let ((form (autoload-read-section-header)))
437 (cond
438 ((equal (nth 2 form) file)
439 ;; We found the section for this file.
440 (let ((begin (match-beginning 0)))
441 (progn
442 (search-forward generate-autoload-section-trailer)
443 (delete-region begin (point))
444 (setq found t))))
445 ((string< file (nth 2 form))
446 ;; We've come to a section alphabetically later than
447 ;; FILE. We assume the file is in order and so
448 ;; there must be no section for FILE. We will
449 ;; insert one before the section here.
450 (goto-char (match-beginning 0))
451 (setq found 'new)))))
452 (unless found
453 (progn
454 (setq found 'new)
455 ;; No later sections in the file. Put before the last page.
456 (goto-char (point-max))
457 (search-backward "\f" nil t)))
458 (setq no-autoloads
459 (apply #'elinstall-generate-file-autoloads
460 file args))))
462 (if no-autoloads file nil)))
463 ;;;_ . Arranging to add to info-path and load-path
464 ;;;_ , elinstall-generate-add-to-path
465 (defun elinstall-generate-add-to-path (path-element type)
466 "Insert code at point to add PATH-ELEMENT to a path.
467 If TYPE is:
468 * `add-to-load-path', add to load-path
469 * `add-to-info-path', add to Info-additional-directory-list
471 Current buffer must be a loaddef-style file."
472 (let ( (path-symbol
473 (case type
474 (add-to-load-path 'load-path)
475 (add-to-info-path 'Info-additional-directory-list)
476 (t (error "Type not recognized"))))
477 (description
478 (case type
479 (add-to-load-path "load-path")
480 (add-to-info-path "info-path")))
481 (autoloads-done '())
482 (print-length nil)
483 (print-level nil)
484 ;; This does something in Lucid Emacs.
485 (print-readably t)
486 (float-output-format nil))
488 (message "Generating %s additions..." description)
490 (elinstall-insert-autoload-section
491 (pp-to-string
492 `(add-to-list ',path-symbol
493 (expand-file-name
494 ,(file-relative-name path-element)
495 (if load-file-name
496 (file-name-directory
497 (file-truename load-file-name))))))
498 (list type (list path-element) nil nil nil)
499 nil)
500 (message "Generating %s additions...done" description)))
503 ;;;_ , elinstall-deffile-insert-add-to-path
504 (defun elinstall-deffile-insert-add-to-path (path-element type)
505 "Insert code in current buffer to add PATH-ELEMENT to a path.
506 If TYPE is:
507 * `add-to-load-path', add to load-path
508 * `add-to-info-path', add to Info-default-directory-list
510 Current buffer must be a loaddef-style file."
511 (let (
512 (found nil)
513 (no-autoloads nil))
515 (save-excursion
516 (save-restriction
517 (widen)
518 (goto-char (point-min))
519 ;; Look for the section for PATH-ELEMENT
520 (while (and (not found)
521 (search-forward generate-autoload-section-header nil t))
522 (let ((form (autoload-read-section-header)))
523 (cond
524 ((and
525 (equal (nth 0 form) type)
526 (member path-element (nth 1 form)))
528 ;; We found the section for this add.
529 (let ((begin (match-beginning 0)))
530 (progn
531 (search-forward generate-autoload-section-trailer)
532 (delete-region begin (point))
533 (setq found t)))))))
535 (unless found
536 (progn
537 (setq found 'new)
538 ;; No later sections in the file. Put before the last page.
539 (goto-char (point-max))
540 (search-backward "\f" nil t)))
542 (elinstall-generate-add-to-path path-element type)))
544 ;;This never belongs in the no-autoloads section.
545 nil))
546 ;;;_ . elinstall-deffile-insert
548 (defun elinstall-deffile-insert (action)
549 "Insert autoloads etc into current file according to ACTION.
550 The format of ACTION is described in the design docs.
552 Return filename if this action belongs in the no-autoload section."
554 (when action
555 (case (car action)
556 (add-file-autoloads
557 (elinstall-deffile-insert-autoloads
558 (third action)
559 (nthcdr 3 action)))
561 (add-to-load-path
562 (elinstall-deffile-insert-add-to-path
563 (third action)
564 'add-to-load-path)
565 nil)
567 (add-to-info-path
568 (elinstall-deffile-insert-add-to-path
569 (third action)
570 'add-to-info-path)
571 nil)
573 ((preload-file run-tests byte-compile)
574 (error "This case should not come here.")))))
576 ;;;_ . elinstall-prepare-deffile
577 (defun elinstall-prepare-deffile (deffile)
578 "Try to ensure that DEFFILE is available for receiving autoloads"
580 (autoload-ensure-default-file deffile)
581 (with-current-buffer (find-file-noselect deffile)
584 ;; We must read/write the file without any code conversion,
585 ;; but still decode EOLs.
586 (let ((coding-system-for-read 'raw-text))
588 ;; This is to make generated-autoload-file have Unix EOLs, so
589 ;; that it is portable to all platforms.
590 (setq buffer-file-coding-system 'raw-text-unix))
591 (or (> (buffer-size) 0)
592 (error "Autoloads file %s does not exist" buffer-file-name))
593 (or (file-writable-p buffer-file-name)
594 (error "Autoloads file %s is not writable"
595 buffer-file-name))))
597 ;;;_ . elinstall-update-deffile
599 ;;Adapted from autoload.el `update-directory-autoloads'.
601 (defun elinstall-update-deffile (target actions &optional
602 use-load-path force)
604 Update file TARGET with current autoloads as specified by ACTIONS.
605 Also remove any old definitions pointing to libraries that can no
606 longer be found.
608 ACTIONS must be a list of actions (See the format doc). Each one's
609 filename must be relative to some element of load-path.
611 USE-LOAD-PATH is a list to use as load-path. It should include
612 any new load-path that we are arranging to create. If it's not given,
613 load-path itself is used.
615 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
616 Other non-nil cases of FORCE are reserved for future development.
618 This uses `update-file-autoloads' (which see) to do its work.
619 In an interactive call, you must give one argument, the name
620 of a single directory."
621 (let
623 (use-load-path (or use-load-path load-path))
624 (this-time (current-time))
625 ;;files with no autoload cookies.
626 (no-autoloads nil))
628 (elinstall-prepare-deffile target)
629 (with-current-buffer
630 (find-file-noselect target)
631 (save-excursion
632 (setq actions
633 (elinstall-remove-autogen-action
634 (autoload-trim-file-name target)
635 actions))
637 (goto-char (point-min))
638 (while (search-forward generate-autoload-section-header nil t)
639 (let* ((form (autoload-read-section-header))
640 (file (nth 3 form)))
641 (cond ((and (consp file) (stringp (car file)))
642 ;; This is a list of files that have no
643 ;; autoload cookies.
644 ;; There shouldn't be more than one such entry.
645 ;; Remove the obsolete section.
646 (autoload-remove-section (match-beginning 0))
647 (let ((last-time (nth 4 form)))
648 (dolist (file file)
649 (let ((file-time (elinstall-file-mod-time file)))
650 (when (and file-time
651 (not (time-less-p last-time file-time)))
652 ;; file unchanged
653 (push file no-autoloads)
654 (setq actions
655 (elinstall-remove-autogen-action
656 file actions)))))))
657 ((not (stringp file)))
659 (let
660 ((file-path
661 (locate-library file nil use-load-path)))
662 (cond
663 ;;File doesn't exist, so remove its
664 ;;section.
665 ((not file-path)
666 (autoload-remove-section
667 (match-beginning 0)))
669 ;; File hasn't changed, so do nothing.
670 ((equal
671 (nth 4 form)
672 (elinstall-file-mod-time file-path))
673 nil)
675 (elinstall-deffile-insert
676 (elinstall-get-autogen-action
677 file actions))))
679 (setq actions
680 (elinstall-remove-autogen-action
681 file actions))))))))
683 ;; Remaining actions have no existing autoload sections yet.
684 (setq no-autoloads
685 (append no-autoloads
686 (delq nil (mapcar #'elinstall-deffile-insert actions))))
687 (when no-autoloads
688 ;; Sort them for better readability.
689 (setq no-autoloads (sort no-autoloads 'string<))
690 ;; Add the `no-autoloads' section.
691 (goto-char (point-max))
692 (search-backward "\f" nil t)
693 (elinstall-insert-autoload-section
695 (list 'autoloads nil nil no-autoloads this-time)))
696 (save-buffer))))
698 ;;;_ . elinstall-stage-update-deffiles
699 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
700 "Update any deffiles mentioned in SEGMENT-LIST.
701 FORCE and USE-LOAD-PATH have the same meaning as in
702 `elinstall-update-deffile'.
704 (mapcar
705 #'(lambda (segment)
706 (let*
707 ((deffile (car segment)))
708 (if (stringp deffile)
709 (elinstall-update-deffile deffile (cdr segment) force
710 use-load-path))))
711 segment-list))
713 ;;;_ , Doing actions to arrange preloads
714 ;;;_ . elinstall-symlink-on-emacs-start
715 (defun elinstall-symlink-on-emacs-start
716 (filename target-basename target-dir &optional priority force)
717 "Symlink to TARGET-BASENAME.el in TARGET-DIR
719 If PRIORITY is given, it will be used as the priority prefix,
720 otherwise elinstall-default-priority will be.
721 PRIORITY must be an integer or nil.
722 If FORCE is `t', do it regardless of timestamps etc.
723 Other non-nil cases of FORCE are reserved for future development."
724 (let*
726 (priority (or priority elinstall-default-priority))
727 (target-name-nodir
728 (format
729 "%d%s.el"
730 priority
731 target-basename))
732 (target
733 (expand-file-name target-name-nodir target-dir)))
736 (cond
737 ;;Path should already exist.
738 ((not
739 (file-exists-p target-dir))
740 (message "The target directory doesn't exist."))
741 ;;Target shouldn't already exist, but if force is given, let
742 ;;user override.
743 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
744 ;;do nothing even on force.
745 ((and
746 (file-exists-p target)
748 (not force)
749 (not
750 (yes-or-no-p
751 (format "Really overwrite %s? " target))))
752 (message "File %s already exists" target)))
755 (make-symbolic-link
756 filename
757 target
758 nil)))))
760 ;;;_ . elinstall-add-to-dot-emacs
761 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
762 "Add code to load FILENAME to .emacs.
763 FILENAME should not have an extension"
765 ;;Visit .emacs
766 (with-current-buffer (find-file-noselect dot-emacs-name)
767 (save-excursion
768 ;;add at end of file
769 (goto-char (point-max))
770 (insert "\n;;Added by elinstall")
771 (insert "\n;;Consider using my-site-start to manage .emacs\n")
772 (pp `(load ,filename) (current-buffer))
773 (save-buffer))))
776 ;;;_ . elinstall-arrange-preload
777 ;;;###autoload
778 (defun elinstall-arrange-preload (force filename basename &optional priority)
779 "Arrange for FILENAME to be loaded on emacs start.
780 FORCE has its usual meaning.
781 BASENAME and PRIORITY are used as arguments to
782 `elinstall-symlink-on-emacs-start'.
785 (let
786 ((preload-target elinstall-default-preload-target))
788 ;;Dispatch the possibilities.
789 (cond
790 ((eq preload-target 'dot-emacs)
791 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
792 ((stringp preload-target)
793 (elinstall-symlink-on-emacs-start
794 filename basename preload-target priority force))
795 ((null preload-target)
796 (message "Not arranging for preloads"))
798 (message "I don't recognize that")))))
799 ;;;_ . elinstall-stage-arrange-preloads
800 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
801 "Arrange any preloads mentioned in ACTIONS."
803 (mapcar
804 #'(lambda (act)
805 (case (car act)
806 (preload-file
807 (let*
808 ( (filename
809 (caddr act))
810 (proceed-p
811 (case (second act)
812 ((t) t)
813 ((nil) nil)
814 (if-used
815 (member filename deffiles-used)))))
817 (when proceed-p
818 (apply
819 #'elinstall-arrange-preload
820 force
821 (cddr act)))))
823 (error
824 "elinstall-stage-arrange-preloads: Action not
825 recognized."))) )
826 actions))
829 ;;;_ , Run tests
830 ;;;_ . elinstall-stage-run-tests
831 (defun elinstall-stage-run-tests (actions)
832 "Run any tests mentioned in ACTIONS."
834 (mapcar
835 #'(lambda (act)
836 (case (car act)
837 (run-tests
838 ;;$$WRITE ME - not a high priority right now.
839 nil)
841 (error
842 "elinstall-stage-run-tests: Action not
843 recognized."))) )
844 actions))
847 ;;;_ , Byte compile
848 ;;;_ . elinstall-stage-byte-compile
849 (defun elinstall-stage-byte-compile (actions)
850 "Do any byte-compilation mentioned in ACTIONS."
852 (mapcar
853 #'(lambda (act)
854 (case (car act)
855 ;;$$IMPROVE ME Understand flags to control second
856 ;;argument (whether to load file after
857 ;;compilation)
858 (byte-compile
859 (byte-compile-file (second act)))
861 (error
862 "elinstall-stage-byte-compile: Action not
863 recognized."))) )
864 actions))
865 ;;;_ . Segregating actions
866 ;;;_ , elinstall-remove-empty-segs
867 (defun elinstall-remove-empty-segs (segment-list)
868 "Return SEGMENT-LIST minus any segments that have no actions.
869 Intended only for the deffile stage data."
870 (delq nil
871 (mapcar
872 #'(lambda (segment)
873 (if (cdr segment)
874 segment
875 nil))
876 segment-list)))
878 ;;;_ , elinstall-segregate-actions
879 (defun elinstall-segregate-actions (actions)
880 "Return actions segregated by deffile.
882 Returns a list whose elements are each a cons of:
883 * deffile filename or nil
884 * A list of actions to be done for that deffile."
886 (let
888 (build-deffiles '())
889 (run-tests '())
890 (byte-compile '())
891 (arrange-preloads '()))
893 (dolist (act actions)
894 (when act
895 (case (car act)
896 ((add-file-autoloads
897 add-to-info-path
898 add-to-load-path)
899 (let*
900 ((deffile-name (second act))
901 (cell-already
902 (assoc deffile-name build-deffiles)))
903 (if cell-already
904 ;;There are already actions on this deffile.
905 ;;Splice this action in.
906 (setcdr cell-already
907 (cons act (cdr cell-already)))
908 ;;There are no actions on this deffile. Add a
909 ;;place for them and include this action.
910 (push (list deffile-name act) build-deffiles))))
911 (preload-file
912 (push act arrange-preloads))
913 (run-tests
914 (push act run-tests))
915 (byte-compile
916 (push act byte-compile)))))
918 (elinstall-make-stages
919 :build-deffiles
920 (elinstall-remove-empty-segs build-deffiles)
921 :run-tests
922 run-tests
923 :byte-compile
924 byte-compile
925 :arrange-preloads
926 arrange-preloads)))
927 ;;;_ . Finding actions
928 ;;;_ , Utility
929 ;;;_ . elinstall-dir-has-info
931 ;;$$IMPROVE ME - Can this test be made more precise?
932 (defun elinstall-dir-has-info (dir)
933 "Return non-nil if DIR has info files in it.
934 DIR should be an absolute path."
936 (string-match "/info/" dir)
937 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
938 ;;;_ . elinstall-directory-files
939 (defun elinstall-directory-files (dirname)
940 "Return a list of files in directory DIRNAME, minus certain files.
941 The following files are omitted:
942 * Dot files
943 * Files that match an entry in `block-in-subtree'.
944 Filenames are relative."
945 (declare (special
946 def-file block-in-dir block-in-subtree))
947 (let*
949 ;;Relative filenames of this directory's files.
950 (all-files
951 ;; Don't include dot files. If user really wants to
952 ;;explore one he can use (dir ".NAME") or (file ".NAME")
953 (directory-files dirname nil "[^\\.]"))
954 ;;We know our def-file isn't really source so remove it.
955 ;;We'd have removed it anyways after seeing file local vars.
956 (all-files
957 (remove def-file all-files))
959 (all-files
960 (delq nil
961 (mapcar
962 #'(lambda (filename)
964 (and
965 block-in-subtree
966 (some
967 #'(lambda (blocked)
968 (string-match blocked filename))
969 block-in-subtree))
971 filename))
972 all-files))))
973 all-files))
976 ;;;_ , Workers
977 ;;;_ . List of special variables used in this section
978 ;;load-path-element - The relevant element of load-path
979 ;;def-file - The file the autoload definitions etc will go into.
980 ;;add-to-load-path-p - Controls whether to add to load-path.
981 ;;recurse-dirs-p - Whether to recurse into subdirectories.
983 ;;block-in-dir - (NOT IMPLEMENTED) List of filenames to reject. They
984 ;;may include wildcards. They apply wrt the original directory.
986 ;;block-in-subtree - (NOT IMPLEMENTED) List of filenames to reject.
987 ;;They may include wildcards. They apply wrt any directory in the
988 ;;tree. Specifically, in the spec tree, which may differ from the
989 ;;file subtree.
990 ;;byte-compile - whether to byte-compile at all, t by default.
991 ;;autoloads - boolean whether to make autoloads, t by default.
992 ;;preloads - boolean whether to set up preloads, t by default.
993 (defconst elinstall-find-actions-control-vars
994 '(add-to-load-path-p recurse-dirs-p byte-compile
995 autoloads preloads)
996 "Control special variables that the find-actions tree recognizes" )
997 ;;;_ . elinstall-actions-for-source-file
998 (defun elinstall-actions-for-source-file (filename dir)
999 "Return a list of actions to do for FILENAME in DIR.
1000 Special variables are as noted in \"List of special variables\"."
1001 (declare (special
1002 load-path-element def-file byte-compile))
1003 (let
1004 ((full-path
1005 (expand-file-name filename dir)))
1006 (when
1007 (and
1008 (file-readable-p full-path)
1009 (not (auto-save-file-name-p full-path)))
1010 ;;$$IMPROVE ME Use more of the control variables.
1011 (let*
1013 (visited (get-file-buffer full-path))
1014 (buf
1015 (or
1016 visited
1017 ;;Visit the file cheaply.
1018 ;;hack-local-variables can give errors.
1019 (ignore-errors (autoload-find-file full-path))))
1020 (def-file
1022 (ignore-errors
1023 (with-current-buffer buf
1024 (if (local-variable-p 'generated-autoload-file)
1025 (elinstall-expand-deffile-name
1026 generated-autoload-file)
1027 nil)))
1028 def-file))
1029 ;;Figure out whether to run some actions, by file local vars.
1030 (autoloads-p
1031 (ignore-errors
1032 (with-current-buffer buf
1033 (not no-update-autoloads))))
1034 (do-compile-p
1035 (and
1036 byte-compile
1037 (featurep 'byte-compile)
1038 (string-match emacs-lisp-file-regexp filename)
1039 (ignore-errors
1040 (with-current-buffer buf
1041 (not no-byte-compile)))
1042 (elinstall-proceed-p 'byte-compile
1043 (concat "Compile " filename "? ")
1044 (let
1045 ((dest (byte-compile-dest-file full-path)))
1046 (and
1047 (file-exists-p dest)
1048 (file-newer-than-file-p full-path dest)))
1049 (concat "Recompile " filename "? ")
1050 (concat "Already compiled " filename "."))
1051 '(elinstall-proceed-p 'byte-compile
1052 (list
1053 '( "Compile %s? "
1054 "Recompile %s? "
1055 "Already compiled %s.")
1056 filename)
1057 (let
1058 ((dest (byte-compile-dest-file full-path)))
1059 (and
1060 (file-exists-p dest)
1061 (file-newer-than-file-p full-path dest)))))))
1063 (prog1
1064 (list
1065 (if do-compile-p
1066 `(byte-compile ,full-path)
1067 nil)
1068 (if autoloads-p
1069 (elinstall-make-autoload-action
1070 buf def-file load-path-element full-path)
1071 nil))
1072 (unless visited (kill-buffer-if-not-modified buf)))))))
1073 ;;;_ . elinstall-actions-for-dir
1074 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
1075 "Make actions for DIR.
1076 Recurse just if RECURSE-DIRS-P"
1077 (declare (special
1078 load-path-element def-file add-to-load-path-p))
1079 ;;This does not treat symlinks specially. $$IMPROVE ME it could
1080 ;;treat/not treat them conditional on control variables.
1081 (let*
1083 (files (elinstall-directory-files dirname))
1085 ;;Relative filenames of elisp source
1086 (elisp-source-files
1087 (delq nil
1088 (mapcar
1089 #'(lambda (filename)
1091 (string-match elinstall-elisp-regexp filename)
1092 filename
1093 nil))
1094 files)))
1096 ;;Absolute filenames of subdirectories.
1097 (sub-dirs
1098 (if recurse-dirs-p
1099 (delq nil
1100 (mapcar
1101 #'(lambda (filename)
1102 (let
1103 ((fn (expand-file-name filename dirname)))
1105 (file-directory-p fn)
1107 nil)))
1108 files))
1109 '()))
1111 (load-path-here-p
1112 (and
1113 elisp-source-files ;;If list is not empty.
1114 add-to-load-path-p))
1115 (load-path-element
1116 (if load-path-here-p
1117 dirname
1118 load-path-element)))
1120 (append
1121 ;;Sometimes arrange to add this directory to load-path.
1122 (if load-path-here-p
1123 `((add-to-load-path
1124 ,def-file
1125 ,load-path-element))
1126 '())
1128 ;;$$IMPROVE ME - be controlled by a control variable.
1129 ;;Sometimes add this directory to info path.
1131 (elinstall-dir-has-info dirname)
1132 `((add-to-info-path
1133 ,def-file
1134 "."))
1135 '())
1137 (apply #'nconc
1138 (mapcar
1139 #'(lambda (filename)
1140 (elinstall-actions-for-source-file
1141 filename
1142 dirname))
1143 elisp-source-files))
1145 (if recurse-dirs-p
1146 (apply #'nconc
1147 (mapcar
1148 #'(lambda (filename)
1149 (elinstall-find-actions-by-spec-x
1151 (expand-file-name
1152 filename
1153 dirname)))
1154 sub-dirs))
1155 '()))))
1157 ;;;_ . elinstall-find-actions-by-spec-x
1159 (defun elinstall-find-actions-by-spec-x (spec dir)
1160 "Return a list of actions to do, controlled by SPEC."
1161 (declare (special
1162 load-path-element def-file add-to-load-path-p
1163 recurse-dirs-p block-in-dir block-in-subtree))
1165 (if (consp spec)
1166 (case (car spec)
1167 (all
1168 ;;(all . SPEC*)
1169 (apply #'nconc
1170 (mapcar
1171 #'(lambda (sub-spec)
1172 (elinstall-find-actions-by-spec-x
1173 sub-spec
1174 dir))
1175 (cdr spec))))
1176 (block-in-subtree
1177 ;;(block-in-subtree (* FN) SPEC)
1178 (let
1179 ((block-in-subtree (second spec)))
1180 (elinstall-find-actions-by-spec-x
1181 (third spec)
1182 dir)))
1184 ;;Rather than trying to bind all control variables each time
1185 ;;thru, we use `set' and `unwind-protect'.
1186 (control
1187 ;;control TYPE DISPOSITION SPEC
1188 (let
1189 ((key (second spec))
1190 old-value)
1191 (if (memq key elinstall-find-actions-control-vars)
1192 (unwind-protect
1193 (progn
1194 (setq old-value (symbol-value key))
1195 (set key (third spec))
1196 (elinstall-find-actions-by-spec-x
1197 (fourth spec)
1198 dir))
1199 (set key old-value))
1200 (error "Unrecognized control variable %s" key))))
1203 (def-file
1204 ;;(def-file FN ARGS SPEC)
1205 (let
1206 ((def-file
1207 (expand-file-name
1208 (second spec)
1209 dir))
1210 (for-preload (third spec)))
1211 (assert (listp for-preload))
1212 (append
1213 (list
1215 (and for-preload (car for-preload))
1216 `(preload-file
1217 ,(car for-preload)
1218 ,def-file
1219 ,@(cdr for-preload))
1220 '()))
1222 (elinstall-find-actions-by-spec-x
1223 (fourth spec) dir))))
1225 (dir
1226 ;;(dir FN)
1227 (elinstall-actions-for-dir
1228 (expand-file-name
1229 (second spec)
1230 dir)
1231 nil))
1233 (file
1234 ;;(file FN)
1235 (elinstall-actions-for-source-file
1236 (second spec) dir))
1239 ;;(in FN SPEC)
1240 (let
1241 ((new-dir
1242 (expand-file-name
1243 (second spec)
1244 dir)))
1246 (elinstall-find-actions-by-spec-x
1247 (third spec)
1248 new-dir)))
1250 (load-path
1251 ;;(load-path SPEC)
1252 (append
1253 `((add-to-load-path ,def-file ,dir))
1254 (let
1255 ((load-path-element dir)
1256 (add-to-load-path-p nil))
1257 (elinstall-find-actions-by-spec-x
1258 (second spec)
1259 dir))))
1261 (matching
1262 ;;(matching PATTERN SPEC)
1263 (apply #'nconc
1264 (mapcar
1265 #'(lambda (dir)
1266 ;;$$PUNT Assume that `block-in-subtree' and
1267 ;;`block-in-dir' aren't matched.
1268 (elinstall-find-actions-by-spec-x
1269 (third spec)
1270 dir))
1271 (directory-files
1272 dir t (second spec)))))
1273 (preload
1274 ;;(preload FN SYM &rest ARGS)
1275 (let
1276 ((key (third spec)))
1278 (and (symbolp key) (symbol-value key))
1279 `((preload-file
1281 ,(expand-file-name (second spec) dir)
1282 ,@(nthcdr 3 spec)))
1283 '()))))
1285 ;;Single symbols
1286 (case spec
1287 (dir
1288 (elinstall-actions-for-dir dir nil))
1289 ((t)
1290 (elinstall-actions-for-dir dir t)))))
1292 ;;;_ . elinstall-find-actions-by-spec
1293 (defun elinstall-find-actions-by-spec
1294 (spec load-path-element dir def-file redo-old)
1295 "Find the list of actions to do according to SPEC."
1297 (let
1299 (def-file-time (elinstall-file-mod-time def-file))
1300 (add-to-load-path-p t)
1301 (recurse-dirs-p t)
1302 (byte-compile t)
1303 (autoloads t)
1304 (preloads t)
1305 (block-in-dir '())
1306 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1308 (declare (special
1309 load-path-element def-file add-to-load-path-p
1310 recurse-dirs-p byte-compile
1311 block-in-dir block-in-subtree))
1313 (elinstall-find-actions-by-spec-x spec dir)))
1315 ;;;_ . high-level work
1316 ;;;_ , elinstall-get-relevant-load-path
1317 (defun elinstall-get-relevant-load-path (actions)
1319 (delq nil
1320 (mapcar
1321 #'(lambda (act)
1322 (case (car act)
1323 (add-to-load-path
1324 (second act))
1325 (t nil)))
1326 actions)))
1327 ;;;_ , elinstall-get-deffile-list
1328 (defun elinstall-get-deffile-list (stages)
1329 "Get a list of deffile names"
1331 (mapcar
1332 #'car
1333 (elinstall-stages->build-deffiles stages)))
1334 ;;;_ , elinstall-x
1335 (defun elinstall-x (dir spec &optional force)
1336 "High-level worker function to install elisp files."
1337 (let*
1339 ;;This is just the default deffile, spec can override it.
1340 (def-file
1341 (elinstall-expand-deffile-name nil))
1342 (actions
1343 (elinstall-find-actions-by-spec
1344 spec
1347 def-file
1348 (eq force 'redo-old)))
1349 (stages (elinstall-segregate-actions actions))
1350 (use-load-path
1351 (elinstall-get-relevant-load-path
1352 actions)))
1354 (elinstall-stage-update-deffiles
1355 (elinstall-stages->build-deffiles stages)
1356 force
1357 use-load-path)
1358 (elinstall-stage-arrange-preloads
1359 (elinstall-stages->arrange-preloads stages)
1360 (elinstall-get-deffile-list stages)
1361 force)
1362 (elinstall-stage-byte-compile
1363 (elinstall-stages->byte-compile stages))
1365 ;;;_ , elinstall-package
1366 (defun elinstall-package (project-name path spec version-string)
1367 "Install elisp files. See doc for `elinstall'."
1368 (when
1369 (elinstall-proceed-p 'install
1370 (format "Install %s? " project-name)
1371 (elinstall-already-installed project-name)
1372 (format "Re-install %s? " project-name)
1373 (format "Already installed %s. " project-name))
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