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