Full customize spec for elinstall-restrain-install
[elinstall.git] / elinstall.el
blob8dafc0fb503946820684fee61211b8d97a217858
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 ;;$$PUNT for now. Figure out user restraints for this
212 ;;package.
213 (elinstall:*user-restraints*
214 nil))
215 (declare (special
216 elinstall:*pkg-restraints*
217 elinstall:*user-restraints*))
218 (apply func args)))
219 ;;;_ ,
220 (defun elinstall-proceed-p
221 (topic message-params &optional already-p)
222 "Return non-nil if actions on TOPIC should proceed.
223 Call this transitively only thru `elinstall-call-with-restraints'.
224 TOPIC is a symbol indicating the topic, such as `byte-compile'.
225 MESSAGE-PARAMS is a cons of:
226 * A list of format strings:
227 * To ask whether to do this action
228 * To ask whether to redo this action, for `ask-for-old'
229 * To report that this action was skipped because already done.
230 * The arguments to the formatter.
231 ALREADY-P is an extended boolean whether the task has been done
232 before, if caller can tell."
234 (check-type topic symbol)
235 (declare (special
236 elinstall:*pkg-restraints*
237 elinstall:*user-restraints*))
238 (unless (and
239 (boundp 'elinstall:*pkg-restraints*)
240 (boundp 'elinstall:*user-restraints*))
241 (error "elinstall-proceed-p called out of scope"))
243 (destructuring-bind
244 ((ask-prompt &optional redo-prompt noredo-msg) &rest message-args)
245 message-params
246 (let*
247 ( ;;Get the applicable cell. We look in several places.
248 (cell (assq topic elinstall:*user-restraints*))
249 (cell (or cell (assq t elinstall:*user-restraints*)))
250 (cell
251 ;;`t' means use the pkg-restraints value instead.
252 (if
253 (or (not cell) (eq (second cell) t))
254 (assq topic elinstall:*pkg-restraints*)
255 cell))
256 (cell (or cell (assq t elinstall:*pkg-restraints*)))
257 (treatment
258 ;;Default is to just update.
259 (if cell (second cell) 'update)))
260 (case treatment
261 ((nil)
262 nil)
263 ((t always)
265 (update
266 (if already-p
267 (progn
268 (apply #'message noredo-msg message-args)
269 nil)
271 (ask-for-old
272 (if already-p
273 (y-or-n-p (apply #'format redo-prompt message-args))
275 (ask
276 (y-or-n-p
277 (apply #'format ask-prompt message-args)))))))
279 ;;;_ , Work
280 ;;;_ . Doing actions
282 ;;;_ , Doing autoload actions (adapted from autoload.el)
283 ;;;_ . Utilities about the action list
284 ;;;_ , elinstall-remove-autogen-action
285 (defun elinstall-remove-autogen-action (file actions)
286 "Return ACTIONS minus any add-file-autoloads on FILE removed."
288 (delq nil
289 (mapcar
290 #'(lambda (act)
291 (case (car act)
292 (add-file-autoloads
293 (if (equal file (third act))
295 act))
296 (t act)))
297 actions)))
298 ;;;_ , elinstall-get-autogen-action
299 (defun elinstall-get-autogen-action (file actions)
301 (let
302 ((the-act))
303 (dolist (act actions)
304 (case (car act)
305 (add-file-autoloads
306 (when (equal file (third act))
307 (setq the-act act)))))
308 the-act))
309 ;;;_ . About printing to autoload file
310 ;;;_ , elinstall-insert-section-header
311 (defun elinstall-insert-section-header (outbuf form)
312 "Insert the section-header line,
313 which lists the file name and which functions are in it, etc."
314 (insert generate-autoload-section-header)
315 (prin1 form outbuf)
316 (terpri outbuf)
317 ;; Break that line at spaces, to avoid very long lines.
318 ;; Make each sub-line into a comment.
319 (with-current-buffer outbuf
320 (save-excursion
321 (forward-line -1)
322 (while (not (eolp))
323 (move-to-column 64)
324 (skip-chars-forward "^ \n")
325 (or (eolp)
326 (insert "\n" generate-autoload-section-continuation))))))
328 ;;;_ , elinstall-insert-autoload-section
329 (defun elinstall-insert-autoload-section (text form &optional comment-string)
330 "Insert TEXT into current buffer as an autoload section"
332 (let* (
333 (print-length nil)
334 (print-level nil)
335 ;; This does something in Lucid Emacs.
336 (print-readably t)
337 (float-output-format nil))
339 (elinstall-insert-section-header (current-buffer) form)
340 (when comment-string
341 (insert ";;; " comment-string "\n"))
342 (insert text)
343 (insert generate-autoload-section-trailer)))
345 ;;;_ . Making autoloads
346 ;;;_ , elinstall-make-autoload-action
347 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
348 "Return the autoloads for current buffer as a string"
350 ;;We put all the text into a temp buffer, then get that buffer's
351 ;;string.
352 (let
353 ((outbuf
354 (generate-new-buffer " *temp*"))
355 (autoloads-done '())
356 (short-name
357 (file-name-nondirectory full-path))
359 (print-length nil)
360 (print-level nil)
361 ;; Apparently this does something in Lucid Emacs.
362 (print-readably t)
363 (float-output-format nil)
365 ;;load-name relative to a member of load-path.
366 (relative-name
367 (file-name-sans-extension
368 (file-relative-name
369 full-path
370 load-path-element))))
372 (with-current-buffer buf
373 (unwind-protect
374 (save-excursion
375 (save-restriction
376 (widen)
377 (goto-char (point-min))
378 (message "Finding autoloads for %s..." short-name)
379 (while (not (eobp))
380 (skip-chars-forward " \t\n\f")
381 (cond
382 ((looking-at (regexp-quote generate-autoload-cookie))
383 (search-forward generate-autoload-cookie)
384 (skip-chars-forward " \t")
385 (if (eolp)
386 ;; Read the next form and make an autoload.
387 (let* ((form (prog1 (read (current-buffer))
388 (or (bolp) (forward-line 1))))
389 (autoload
390 (make-autoload form relative-name)))
391 (if autoload
392 (push (nth 1 form) autoloads-done)
393 (setq autoload form))
394 (let ((autoload-print-form-outbuf outbuf))
395 (autoload-print-form autoload)))
397 ;; Copy the rest of the line to the output.
398 (princ (buffer-substring
399 (progn
400 ;; Back up over whitespace,
401 ;; to preserve it.
402 (skip-chars-backward " \f\t")
403 (if (= (char-after (1+ (point))) ? )
404 ;; Eat one space.
405 (forward-char 1))
406 (point))
407 (progn (forward-line 1) (point)))
408 outbuf)))
409 ((looking-at ";")
410 ;; Don't read the comment.
411 (forward-line 1))
413 (forward-sexp 1)
414 (forward-line 1))))
415 (message "Finding autoloads for %s...done" short-name))
417 ;;Return this action. The temp buffer's contents is
418 ;;our final string.
419 `(add-file-autoloads
420 ,def-file
421 ,relative-name
422 ,full-path
423 ,(with-current-buffer outbuf (buffer-string))
424 ,autoloads-done))
426 ;;This in unwind-protected
427 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
430 ;;;_ , elinstall-generate-file-autoloads
432 (defun elinstall-generate-file-autoloads
433 (relative-name full-name text autoloads-done)
434 "Insert at point a loaddefs autoload section for FILE.
435 Autoloads are generated for defuns and defmacros in FILE
436 marked by `generate-autoload-cookie' (which see).
437 If FILE is being visited in a buffer, the contents of the buffer
438 are used.
439 Return non-nil in the case where no autoloads were added at point.
441 FULL-NAME is the absolute name of the file.
442 RELATIVE-NAME is its name respective to some component of load-path."
443 (if (not (equal text ""))
444 ;; Insert the section-header line which lists the file name and
445 ;; which functions are in it, etc.
446 (elinstall-insert-autoload-section
447 text
448 (list 'autoloads
449 autoloads-done
450 relative-name
451 (autoload-trim-file-name full-name)
452 (elinstall-file-mod-time full-name))
453 (concat
454 "Generated autoloads from "
455 (autoload-trim-file-name full-name)))
458 ;;;_ , elinstall-deffile-insert-autoloads
459 (defun elinstall-deffile-insert-autoloads (file args)
460 "Update the autoloads for FILE in current buffer.
461 Return FILE if there was no autoload cookie in it, else nil.
463 Current buffer must be a loaddef-style file.
465 LOAD-NAME is the absolute name of the file.
466 RELATIVE-NAME is its name respective to some component of load-path."
467 (let (
468 (found nil)
469 (no-autoloads nil))
471 (save-excursion
472 (save-restriction
473 (widen)
474 (goto-char (point-min))
475 ;; Look for the section for FILE
476 (while (and (not found)
477 (search-forward generate-autoload-section-header nil t))
478 (let ((form (autoload-read-section-header)))
479 (cond
480 ((equal (nth 2 form) file)
481 ;; We found the section for this file.
482 (let ((begin (match-beginning 0)))
483 (progn
484 (search-forward generate-autoload-section-trailer)
485 (delete-region begin (point))
486 (setq found t))))
487 ((string< file (nth 2 form))
488 ;; We've come to a section alphabetically later than
489 ;; FILE. We assume the file is in order and so
490 ;; there must be no section for FILE. We will
491 ;; insert one before the section here.
492 (goto-char (match-beginning 0))
493 (setq found 'new)))))
494 (unless found
495 (progn
496 (setq found 'new)
497 ;; No later sections in the file. Put before the last page.
498 (goto-char (point-max))
499 (search-backward "\f" nil t)))
500 (setq no-autoloads
501 (apply #'elinstall-generate-file-autoloads
502 file args))))
504 (if no-autoloads file nil)))
505 ;;;_ . Arranging to add to info-path and load-path
506 ;;;_ , elinstall-generate-add-to-path
507 (defun elinstall-generate-add-to-path (path-element type)
508 "Insert code at point to add PATH-ELEMENT to a path.
509 If TYPE is:
510 * `add-to-load-path', add to load-path
511 * `add-to-info-path', add to Info-additional-directory-list
513 Current buffer must be a loaddef-style file."
514 (let ( (path-symbol
515 (case type
516 (add-to-load-path 'load-path)
517 (add-to-info-path 'Info-additional-directory-list)
518 (t (error "Type not recognized"))))
519 (description
520 (case type
521 (add-to-load-path "load-path")
522 (add-to-info-path "info-path")))
523 (autoloads-done '())
524 (print-length nil)
525 (print-level nil)
526 ;; This does something in Lucid Emacs.
527 (print-readably t)
528 (float-output-format nil))
530 (message "Generating %s additions..." description)
532 (elinstall-insert-autoload-section
533 (pp-to-string
534 `(add-to-list ',path-symbol
535 (expand-file-name
536 ,(file-relative-name path-element)
537 (if load-file-name
538 (file-name-directory
539 (file-truename load-file-name))))))
540 (list type (list path-element) nil nil nil)
541 nil)
542 (message "Generating %s additions...done" description)))
545 ;;;_ , elinstall-deffile-insert-add-to-path
546 (defun elinstall-deffile-insert-add-to-path (path-element type)
547 "Insert code in current buffer to add PATH-ELEMENT to a path.
548 If TYPE is:
549 * `add-to-load-path', add to load-path
550 * `add-to-info-path', add to Info-default-directory-list
552 Current buffer must be a loaddef-style file."
553 (let (
554 (found nil)
555 (no-autoloads nil))
557 (save-excursion
558 (save-restriction
559 (widen)
560 (goto-char (point-min))
561 ;; Look for the section for PATH-ELEMENT
562 (while (and (not found)
563 (search-forward generate-autoload-section-header nil t))
564 (let ((form (autoload-read-section-header)))
565 (cond
566 ((and
567 (equal (nth 0 form) type)
568 (member path-element (nth 1 form)))
570 ;; We found the section for this add.
571 (let ((begin (match-beginning 0)))
572 (progn
573 (search-forward generate-autoload-section-trailer)
574 (delete-region begin (point))
575 (setq found t)))))))
577 (unless found
578 (progn
579 (setq found 'new)
580 ;; No later sections in the file. Put before the last page.
581 (goto-char (point-max))
582 (search-backward "\f" nil t)))
584 (elinstall-generate-add-to-path path-element type)))
586 ;;This never belongs in the no-autoloads section.
587 nil))
588 ;;;_ . elinstall-deffile-insert
590 (defun elinstall-deffile-insert (action)
591 "Insert autoloads etc into current file according to ACTION.
592 The format of ACTION is described in the design docs.
594 Return filename if this action belongs in the no-autoload section."
596 (when action
597 (case (car action)
598 (add-file-autoloads
599 (elinstall-deffile-insert-autoloads
600 (third action)
601 (nthcdr 3 action)))
603 (add-to-load-path
604 (elinstall-deffile-insert-add-to-path
605 (third action)
606 'add-to-load-path)
607 nil)
609 (add-to-info-path
610 (elinstall-deffile-insert-add-to-path
611 (third action)
612 'add-to-info-path)
613 nil)
615 ((preload-file run-tests byte-compile)
616 (error "This case should not come here.")))))
618 ;;;_ . elinstall-prepare-deffile
619 (defun elinstall-prepare-deffile (deffile)
620 "Try to ensure that DEFFILE is available for receiving autoloads"
622 (autoload-ensure-default-file deffile)
623 (with-current-buffer (find-file-noselect deffile)
626 ;; We must read/write the file without any code conversion,
627 ;; but still decode EOLs.
628 (let ((coding-system-for-read 'raw-text))
630 ;; This is to make generated-autoload-file have Unix EOLs, so
631 ;; that it is portable to all platforms.
632 (setq buffer-file-coding-system 'raw-text-unix))
633 (or (> (buffer-size) 0)
634 (error "Autoloads file %s does not exist" buffer-file-name))
635 (or (file-writable-p buffer-file-name)
636 (error "Autoloads file %s is not writable"
637 buffer-file-name))))
639 ;;;_ . elinstall-update-deffile
641 ;;Adapted from autoload.el `update-directory-autoloads'.
643 (defun elinstall-update-deffile (target actions &optional
644 use-load-path force)
646 Update file TARGET with current autoloads as specified by ACTIONS.
647 Also remove any old definitions pointing to libraries that can no
648 longer be found.
650 ACTIONS must be a list of actions (See the format doc). Each one's
651 filename must be relative to some element of load-path.
653 USE-LOAD-PATH is a list to use as load-path. It should include
654 any new load-path that we are arranging to create. If it's not given,
655 load-path itself is used.
657 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
658 Other non-nil cases of FORCE are reserved for future development.
660 This uses `update-file-autoloads' (which see) to do its work.
661 In an interactive call, you must give one argument, the name
662 of a single directory."
663 (let
665 (use-load-path (or use-load-path load-path))
666 (this-time (current-time))
667 ;;files with no autoload cookies.
668 (no-autoloads nil))
670 (elinstall-prepare-deffile target)
671 (with-current-buffer
672 (find-file-noselect target)
673 (save-excursion
674 (setq actions
675 (elinstall-remove-autogen-action
676 (autoload-trim-file-name target)
677 actions))
679 (goto-char (point-min))
680 (while (search-forward generate-autoload-section-header nil t)
681 (let* ((form (autoload-read-section-header))
682 (file (nth 3 form)))
683 (cond ((and (consp file) (stringp (car file)))
684 ;; This is a list of files that have no
685 ;; autoload cookies.
686 ;; There shouldn't be more than one such entry.
687 ;; Remove the obsolete section.
688 (autoload-remove-section (match-beginning 0))
689 (let ((last-time (nth 4 form)))
690 (dolist (file file)
691 (let ((file-time (elinstall-file-mod-time file)))
692 (when (and file-time
693 (not (time-less-p last-time file-time)))
694 ;; file unchanged
695 (push file no-autoloads)
696 (setq actions
697 (elinstall-remove-autogen-action
698 file actions)))))))
699 ((not (stringp file)))
701 (let
702 ((file-path
703 (locate-library file nil use-load-path)))
704 (cond
705 ;;File doesn't exist, so remove its
706 ;;section.
707 ((not file-path)
708 (autoload-remove-section
709 (match-beginning 0)))
711 ;; File hasn't changed, so do nothing.
712 ((equal
713 (nth 4 form)
714 (elinstall-file-mod-time file-path))
715 nil)
717 (elinstall-deffile-insert
718 (elinstall-get-autogen-action
719 file actions))))
721 (setq actions
722 (elinstall-remove-autogen-action
723 file actions))))))))
725 ;; Remaining actions have no existing autoload sections yet.
726 (setq no-autoloads
727 (append no-autoloads
728 (delq nil (mapcar #'elinstall-deffile-insert actions))))
729 (when no-autoloads
730 ;; Sort them for better readability.
731 (setq no-autoloads (sort no-autoloads 'string<))
732 ;; Add the `no-autoloads' section.
733 (goto-char (point-max))
734 (search-backward "\f" nil t)
735 (elinstall-insert-autoload-section
737 (list 'autoloads nil nil no-autoloads this-time)))
738 (save-buffer))))
740 ;;;_ . elinstall-stage-update-deffiles
741 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
742 "Update any deffiles mentioned in SEGMENT-LIST.
743 FORCE and USE-LOAD-PATH have the same meaning as in
744 `elinstall-update-deffile'.
746 (mapcar
747 #'(lambda (segment)
748 (let*
749 ((deffile (car segment)))
750 (if (stringp deffile)
751 (elinstall-update-deffile deffile (cdr segment) force
752 use-load-path))))
753 segment-list))
755 ;;;_ , Doing actions to arrange preloads
756 ;;;_ . elinstall-symlink-on-emacs-start
757 (defun elinstall-symlink-on-emacs-start
758 (filename target-basename target-dir &optional priority force)
759 "Symlink to TARGET-BASENAME.el in TARGET-DIR
761 If PRIORITY is given, it will be used as the priority prefix,
762 otherwise elinstall-default-priority will be.
763 PRIORITY must be an integer or nil.
764 If FORCE is `t', do it regardless of timestamps etc.
765 Other non-nil cases of FORCE are reserved for future development."
766 (let*
768 (priority (or priority elinstall-default-priority))
769 (target-name-nodir
770 (format
771 "%d%s.el"
772 priority
773 target-basename))
774 (target
775 (expand-file-name target-name-nodir target-dir)))
778 (cond
779 ;;Path should already exist.
780 ((not
781 (file-exists-p target-dir))
782 (message "The target directory doesn't exist."))
783 ;;Target shouldn't already exist, but if force is given, let
784 ;;user override.
785 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
786 ;;do nothing even on force.
787 ((and
788 (file-exists-p target)
790 (not force)
791 (not
792 (yes-or-no-p
793 (format "Really overwrite %s? " target))))
794 (message "File %s already exists" target)))
797 (make-symbolic-link
798 filename
799 target
800 nil)))))
802 ;;;_ . elinstall-add-to-dot-emacs
803 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
804 "Add code to load FILENAME to .emacs.
805 FILENAME should not have an extension"
807 ;;Visit .emacs
808 (with-current-buffer (find-file-noselect dot-emacs-name)
809 (save-excursion
810 ;;add at end of file
811 (goto-char (point-max))
812 (insert "\n;;Added by elinstall")
813 (insert "\n;;Consider using my-site-start to manage .emacs\n")
814 (pp `(load ,filename) (current-buffer))
815 (save-buffer))))
818 ;;;_ . elinstall-arrange-preload
819 ;;;###autoload
820 (defun elinstall-arrange-preload (force filename basename &optional priority)
821 "Arrange for FILENAME to be loaded on emacs start.
822 FORCE has its usual meaning.
823 BASENAME and PRIORITY are used as arguments to
824 `elinstall-symlink-on-emacs-start'.
827 (let
828 ((preload-target elinstall-default-preload-target))
830 ;;Dispatch the possibilities.
831 (cond
832 ((eq preload-target 'dot-emacs)
833 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
834 ((stringp preload-target)
835 (elinstall-symlink-on-emacs-start
836 filename basename preload-target priority force))
837 ((null preload-target)
838 (message "Not arranging for preloads"))
840 (message "I don't recognize that")))))
841 ;;;_ . elinstall-stage-arrange-preloads
842 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
843 "Arrange any preloads mentioned in ACTIONS."
845 (mapcar
846 #'(lambda (act)
847 (case (car act)
848 (preload-file
849 (let*
850 ( (filename
851 (caddr act))
852 (proceed-p
853 (case (second act)
854 ((t) t)
855 ((nil) nil)
856 (if-used
857 (member filename deffiles-used)))))
859 (when proceed-p
860 (apply
861 #'elinstall-arrange-preload
862 force
863 (cddr act)))))
865 (error
866 "elinstall-stage-arrange-preloads: Action not
867 recognized."))) )
868 actions))
871 ;;;_ , Run tests
872 ;;;_ . elinstall-stage-run-tests
873 (defun elinstall-stage-run-tests (actions)
874 "Run any tests mentioned in ACTIONS."
876 (mapcar
877 #'(lambda (act)
878 (case (car act)
879 (run-tests
880 ;;$$WRITE ME - not a high priority right now.
881 nil)
883 (error
884 "elinstall-stage-run-tests: Action not
885 recognized."))) )
886 actions))
889 ;;;_ , Byte compile
890 ;;;_ . elinstall-stage-byte-compile
891 (defun elinstall-stage-byte-compile (actions)
892 "Do any byte-compilation mentioned in ACTIONS."
894 (mapcar
895 #'(lambda (act)
896 (case (car act)
897 ;;$$IMPROVE ME Understand flags to control second
898 ;;argument (whether to load file after
899 ;;compilation)
900 (byte-compile
901 (byte-compile-file (second act)))
903 (error
904 "elinstall-stage-byte-compile: Action not
905 recognized."))) )
906 actions))
907 ;;;_ . Segregating actions
908 ;;;_ , elinstall-remove-empty-segs
909 (defun elinstall-remove-empty-segs (segment-list)
910 "Return SEGMENT-LIST minus any segments that have no actions.
911 Intended only for the deffile stage data."
912 (delq nil
913 (mapcar
914 #'(lambda (segment)
915 (if (cdr segment)
916 segment
917 nil))
918 segment-list)))
920 ;;;_ , elinstall-segregate-actions
921 (defun elinstall-segregate-actions (actions)
922 "Return actions segregated by deffile.
924 Returns a list whose elements are each a cons of:
925 * deffile filename or nil
926 * A list of actions to be done for that deffile."
928 (let
930 (build-deffiles '())
931 (run-tests '())
932 (byte-compile '())
933 (arrange-preloads '()))
935 (dolist (act actions)
936 (when act
937 (case (car act)
938 ((add-file-autoloads
939 add-to-info-path
940 add-to-load-path)
941 (let*
942 ((deffile-name (second act))
943 (cell-already
944 (assoc deffile-name build-deffiles)))
945 (if cell-already
946 ;;There are already actions on this deffile.
947 ;;Splice this action in.
948 (setcdr cell-already
949 (cons act (cdr cell-already)))
950 ;;There are no actions on this deffile. Add a
951 ;;place for them and include this action.
952 (push (list deffile-name act) build-deffiles))))
953 (preload-file
954 (push act arrange-preloads))
955 (run-tests
956 (push act run-tests))
957 (byte-compile
958 (push act byte-compile)))))
960 (elinstall-make-stages
961 :build-deffiles
962 (elinstall-remove-empty-segs build-deffiles)
963 :run-tests
964 run-tests
965 :byte-compile
966 byte-compile
967 :arrange-preloads
968 arrange-preloads)))
969 ;;;_ . Finding actions
970 ;;;_ , Utility
971 ;;;_ . elinstall-dir-has-info
973 ;;$$IMPROVE ME - Can this test be made more precise?
974 (defun elinstall-dir-has-info (dir)
975 "Return non-nil if DIR has info files in it.
976 DIR should be an absolute path."
978 (string-match "/info/" dir)
979 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
980 ;;;_ . elinstall-directory-files
981 (defun elinstall-directory-files (dirname)
982 "Return a list of files in directory DIRNAME, minus certain files.
983 The following files are omitted:
984 * Dot files
985 * Files that match an entry in `block-in-subtree'.
986 Filenames are relative."
987 (declare (special
988 def-file block-in-dir block-in-subtree))
989 (let*
991 ;;Relative filenames of this directory's files.
992 (all-files
993 ;; Don't include dot files. If user really wants to
994 ;;explore one he can use (dir ".NAME") or (file ".NAME")
995 (directory-files dirname nil "[^\\.]"))
996 ;;We know our def-file isn't really source so remove it.
997 ;;We'd have removed it anyways after seeing file local vars.
998 (all-files
999 (remove def-file all-files))
1001 (all-files
1002 (delq nil
1003 (mapcar
1004 #'(lambda (filename)
1006 (and
1007 block-in-subtree
1008 (some
1009 #'(lambda (blocked)
1010 (string-match blocked filename))
1011 block-in-subtree))
1013 filename))
1014 all-files))))
1015 all-files))
1018 ;;;_ , Workers
1019 ;;;_ . List of special variables used in this section
1020 ;;load-path-element - The relevant element of load-path
1021 ;;def-file - The file the autoload definitions etc will go into.
1022 ;;add-to-load-path-p - Controls whether to add to load-path.
1023 ;;recurse-dirs-p - Whether to recurse into subdirectories.
1025 ;;block-in-dir - (NOT IMPLEMENTED) List of filenames to reject. They
1026 ;;may include wildcards. They apply wrt the original directory.
1028 ;;block-in-subtree - (NOT IMPLEMENTED) List of filenames to reject.
1029 ;;They may include wildcards. They apply wrt any directory in the
1030 ;;tree. Specifically, in the spec tree, which may differ from the
1031 ;;file subtree.
1032 ;;byte-compile - whether to byte-compile at all, t by default.
1033 ;;autoloads - boolean whether to make autoloads, t by default.
1034 ;;preloads - boolean whether to set up preloads, t by default.
1035 (defconst elinstall-find-actions-control-vars
1036 '(add-to-load-path-p recurse-dirs-p byte-compile
1037 autoloads preloads)
1038 "Control special variables that the find-actions tree recognizes" )
1039 ;;;_ . elinstall-actions-for-source-file
1040 (defun elinstall-actions-for-source-file (filename dir)
1041 "Return a list of actions to do for FILENAME in DIR.
1042 Special variables are as noted in \"List of special variables\"."
1043 (declare (special
1044 load-path-element def-file byte-compile))
1045 (let
1046 ((full-path
1047 (expand-file-name filename dir)))
1048 (when
1049 (and
1050 (file-readable-p full-path)
1051 (not (auto-save-file-name-p full-path)))
1052 ;;$$IMPROVE ME Use more of the control variables.
1053 (let*
1055 (visited (get-file-buffer full-path))
1056 (buf
1057 (or
1058 visited
1059 ;;Visit the file cheaply.
1060 ;;hack-local-variables can give errors.
1061 (ignore-errors (autoload-find-file full-path))))
1062 (def-file
1064 (ignore-errors
1065 (with-current-buffer buf
1066 (if (local-variable-p 'generated-autoload-file)
1067 (elinstall-expand-deffile-name
1068 generated-autoload-file)
1069 nil)))
1070 def-file))
1071 ;;Figure out whether to run some actions, by file local vars.
1072 (autoloads-p
1073 (ignore-errors
1074 (with-current-buffer buf
1075 (not no-update-autoloads))))
1076 (do-compile-p
1077 (and
1078 byte-compile
1079 (featurep 'byte-compile)
1080 (string-match emacs-lisp-file-regexp filename)
1081 (ignore-errors
1082 (with-current-buffer buf
1083 (not no-byte-compile)))
1084 (elinstall-proceed-p 'byte-compile
1085 (list
1086 '( "Compile %s? "
1087 "Recompile %s? "
1088 "Already compiled %s.")
1089 filename)
1090 (let
1091 ((dest (byte-compile-dest-file full-path)))
1092 (and
1093 (file-exists-p dest)
1094 (file-newer-than-file-p full-path dest)))))))
1096 (prog1
1097 (list
1098 (if do-compile-p
1099 `(byte-compile ,full-path)
1100 nil)
1101 (if autoloads-p
1102 (elinstall-make-autoload-action
1103 buf def-file load-path-element full-path)
1104 nil))
1105 (unless visited (kill-buffer-if-not-modified buf)))))))
1106 ;;;_ . elinstall-actions-for-dir
1107 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
1108 "Make actions for DIR.
1109 Recurse just if RECURSE-DIRS-P"
1110 (declare (special
1111 load-path-element def-file add-to-load-path-p))
1112 ;;This does not treat symlinks specially. $$IMPROVE ME it could
1113 ;;treat/not treat them conditional on control variables.
1114 (let*
1116 (files (elinstall-directory-files dirname))
1118 ;;Relative filenames of elisp source
1119 (elisp-source-files
1120 (delq nil
1121 (mapcar
1122 #'(lambda (filename)
1124 (string-match elinstall-elisp-regexp filename)
1125 filename
1126 nil))
1127 files)))
1129 ;;Absolute filenames of subdirectories.
1130 (sub-dirs
1131 (if recurse-dirs-p
1132 (delq nil
1133 (mapcar
1134 #'(lambda (filename)
1135 (let
1136 ((fn (expand-file-name filename dirname)))
1138 (file-directory-p fn)
1140 nil)))
1141 files))
1142 '()))
1144 (load-path-here-p
1145 (and
1146 elisp-source-files ;;If list is not empty.
1147 add-to-load-path-p))
1148 (load-path-element
1149 (if load-path-here-p
1150 dirname
1151 load-path-element)))
1153 (append
1154 ;;Sometimes arrange to add this directory to load-path.
1155 (if load-path-here-p
1156 `((add-to-load-path
1157 ,def-file
1158 ,load-path-element))
1159 '())
1161 ;;$$IMPROVE ME - be controlled by a control variable.
1162 ;;Sometimes add this directory to info path.
1164 (elinstall-dir-has-info dirname)
1165 `((add-to-info-path
1166 ,def-file
1167 "."))
1168 '())
1170 (apply #'nconc
1171 (mapcar
1172 #'(lambda (filename)
1173 (elinstall-actions-for-source-file
1174 filename
1175 dirname))
1176 elisp-source-files))
1178 (if recurse-dirs-p
1179 (apply #'nconc
1180 (mapcar
1181 #'(lambda (filename)
1182 (elinstall-find-actions-by-spec-x
1184 (expand-file-name
1185 filename
1186 dirname)))
1187 sub-dirs))
1188 '()))))
1190 ;;;_ . elinstall-find-actions-by-spec-x
1192 (defun elinstall-find-actions-by-spec-x (spec dir)
1193 "Return a list of actions to do, controlled by SPEC."
1194 (declare (special
1195 load-path-element def-file add-to-load-path-p
1196 recurse-dirs-p block-in-dir block-in-subtree))
1198 (if (consp spec)
1199 (case (car spec)
1200 (all
1201 ;;(all . SPEC*)
1202 (apply #'nconc
1203 (mapcar
1204 #'(lambda (sub-spec)
1205 (elinstall-find-actions-by-spec-x
1206 sub-spec
1207 dir))
1208 (cdr spec))))
1209 (block-in-subtree
1210 ;;(block-in-subtree (* FN) SPEC)
1211 (let
1212 ((block-in-subtree (second spec)))
1213 (elinstall-find-actions-by-spec-x
1214 (third spec)
1215 dir)))
1217 ;;Rather than trying to bind all control variables each time
1218 ;;thru, we use `set' and `unwind-protect'.
1219 (control
1220 ;;control TYPE DISPOSITION SPEC
1221 (let
1222 ((key (second spec))
1223 old-value)
1224 (if (memq key elinstall-find-actions-control-vars)
1225 (unwind-protect
1226 (progn
1227 (setq old-value (symbol-value key))
1228 (set key (third spec))
1229 (elinstall-find-actions-by-spec-x
1230 (fourth spec)
1231 dir))
1232 (set key old-value))
1233 (error "Unrecognized control variable %s" key))))
1236 (def-file
1237 ;;(def-file FN ARGS SPEC)
1238 (let
1239 ((def-file
1240 (expand-file-name
1241 (second spec)
1242 dir))
1243 (for-preload (third spec)))
1244 (assert (listp for-preload))
1245 (append
1246 (list
1248 (and for-preload (car for-preload))
1249 `(preload-file
1250 ,(car for-preload)
1251 ,def-file
1252 ,@(cdr for-preload))
1253 '()))
1255 (elinstall-find-actions-by-spec-x
1256 (fourth spec) dir))))
1258 (dir
1259 ;;(dir FN)
1260 (elinstall-actions-for-dir
1261 (expand-file-name
1262 (second spec)
1263 dir)
1264 nil))
1266 (file
1267 ;;(file FN)
1268 (elinstall-actions-for-source-file
1269 (second spec) dir))
1272 ;;(in FN SPEC)
1273 (let
1274 ((new-dir
1275 (expand-file-name
1276 (second spec)
1277 dir)))
1279 (elinstall-find-actions-by-spec-x
1280 (third spec)
1281 new-dir)))
1283 (load-path
1284 ;;(load-path SPEC)
1285 (append
1286 `((add-to-load-path ,def-file ,dir))
1287 (let
1288 ((load-path-element dir)
1289 (add-to-load-path-p nil))
1290 (elinstall-find-actions-by-spec-x
1291 (second spec)
1292 dir))))
1294 (matching
1295 ;;(matching PATTERN SPEC)
1296 (apply #'nconc
1297 (mapcar
1298 #'(lambda (dir)
1299 ;;$$PUNT Assume that `block-in-subtree' and
1300 ;;`block-in-dir' aren't matched.
1301 (elinstall-find-actions-by-spec-x
1302 (third spec)
1303 dir))
1304 (directory-files
1305 dir t (second spec)))))
1306 (preload
1307 ;;(preload FN SYM &rest ARGS)
1308 (let
1309 ((key (third spec)))
1311 (and (symbolp key) (symbol-value key))
1312 `((preload-file
1314 ,(expand-file-name (second spec) dir)
1315 ,@(nthcdr 3 spec)))
1316 '()))))
1318 ;;Single symbols
1319 (case spec
1320 (dir
1321 (elinstall-actions-for-dir dir nil))
1322 ((t)
1323 (elinstall-actions-for-dir dir t)))))
1325 ;;;_ . elinstall-find-actions-by-spec
1326 (defun elinstall-find-actions-by-spec
1327 (spec load-path-element dir def-file redo-old)
1328 "Find the list of actions to do according to SPEC."
1330 (let
1332 (def-file-time (elinstall-file-mod-time def-file))
1333 (add-to-load-path-p t)
1334 (recurse-dirs-p t)
1335 (byte-compile t)
1336 (autoloads t)
1337 (preloads t)
1338 (block-in-dir '())
1339 (block-in-subtree '(".git" "RCS" "CVS" "SVN" "^tests\.el")))
1341 (declare (special
1342 load-path-element def-file add-to-load-path-p
1343 recurse-dirs-p byte-compile
1344 block-in-dir block-in-subtree))
1346 (elinstall-find-actions-by-spec-x spec dir)))
1348 ;;;_ . high-level work
1349 ;;;_ , elinstall-get-relevant-load-path
1350 (defun elinstall-get-relevant-load-path (actions)
1352 (delq nil
1353 (mapcar
1354 #'(lambda (act)
1355 (case (car act)
1356 (add-to-load-path
1357 (second act))
1358 (t nil)))
1359 actions)))
1360 ;;;_ , elinstall-get-deffile-list
1361 (defun elinstall-get-deffile-list (stages)
1362 "Get a list of deffile names"
1364 (mapcar
1365 #'car
1366 (elinstall-stages->build-deffiles stages)))
1367 ;;;_ , elinstall-x
1368 (defun elinstall-x (dir spec &optional force)
1369 "High-level worker function to install elisp files."
1370 (let*
1372 ;;This is just the default deffile, spec can override it.
1373 (def-file
1374 (elinstall-expand-deffile-name nil))
1375 (actions
1376 (elinstall-find-actions-by-spec
1377 spec
1380 def-file
1381 (eq force 'redo-old)))
1382 (stages (elinstall-segregate-actions actions))
1383 (use-load-path
1384 (elinstall-get-relevant-load-path
1385 actions)))
1387 (elinstall-stage-update-deffiles
1388 (elinstall-stages->build-deffiles stages)
1389 force
1390 use-load-path)
1391 (elinstall-stage-arrange-preloads
1392 (elinstall-stages->arrange-preloads stages)
1393 (elinstall-get-deffile-list stages)
1394 force)
1395 (elinstall-stage-byte-compile
1396 (elinstall-stages->byte-compile stages))
1398 ;;;_ , elinstall-package
1399 (defun elinstall-package (project-name path spec version-string)
1400 "Install elisp files. See doc for `elinstall'."
1401 (when
1402 (elinstall-proceed-p 'install
1403 (list
1404 '("Install %s? "
1405 "Re-install %s? "
1406 "Already installed %s.")
1407 project-name)
1408 (elinstall-already-installed project-name))
1409 (elinstall-x
1410 path
1411 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1412 force)
1413 (elinstall-record-installed project-name version-string)))
1415 ;;;_ , Entry points
1416 ;;;_ . elinstall
1417 ;;;###autoload
1418 (defun elinstall (project-name path spec &optional force version-string)
1419 "Install elisp files.
1420 They need not be a formal package.
1422 Parameters:
1424 PROJECT-NAME - the name of the project
1426 PATH - Path to the project.
1427 Suggestion: Use (elinstall-directory-true-name) to get the real
1428 current directoery name even from loaded files.
1430 SPEC - a spec for the autoloads etc to make. It can be as simple
1431 as `t'.
1433 If FORCE is `t', install a package even if it has already been
1434 installed. If it's a list or `nil', it's treated as a list of
1435 installation restraints. User customizations override this
1436 argument.
1438 VERSION-STRING, if given, must be a string of the version for this package."
1440 (elinstall-call-with-restraints
1441 (if (eq force t)
1442 '((install always))
1443 force)
1444 project-name
1445 #'elinstall-package
1446 project-name path spec version-string))
1448 ;;;_ . elinstall-update-directory-autoloads
1450 ;;;###autoload
1451 (defun elinstall-update-directory-autoloads (dir)
1452 "Update autoloads for directory DIR"
1454 (interactive "DUpdate autoloads for all elisp files from directory: ")
1455 (elinstall-call-with-restraints
1456 '((autoloads t)
1457 (t nil))
1459 #'elinstall-x
1461 '(dir ".")))
1463 ;;;_ . elinstall-update-directory
1464 ;;;###autoload
1465 (defun elinstall-update-directory (dir)
1466 "Update autoloads for directory DIR"
1468 (interactive "DInstall all elisp files from directory: ")
1469 (elinstall-call-with-restraints
1472 #'elinstall-x
1474 '(dir ".")))
1476 ;;;_ . elinstall-update-file-autoloads
1477 ;;;###autoload
1478 (defun elinstall-update-file-autoloads (file)
1479 "Update autoloads for elisp file FILE"
1481 (interactive "fUpdate autoloads for elisp file: ")
1482 (elinstall-call-with-restraints
1485 #'elinstall-x
1486 (file-name-directory file)
1487 `(file ,(file-name-nondirectory file))))
1489 ;;;_ . elinstall-update-file
1490 ;;;###autoload
1491 (defun elinstall-update-file (file)
1492 "Install elisp file FILE"
1494 (interactive "fInstall elisp file: ")
1495 (elinstall-call-with-restraints
1496 '((autoloads t)
1497 (t nil))
1499 #'elinstall-x
1500 (file-name-directory file)
1501 `(file ,(file-name-nondirectory file))))
1503 ;;;_. Footers
1504 ;;;_ , Provides
1506 (provide 'elinstall)
1508 ;;;_ * Local emacs vars.
1509 ;;;_ + Local variables:
1510 ;;;_ + mode: allout
1511 ;;;_ + End:
1513 ;;;_ , End
1514 ;;; elinstall.el ends here