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