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