control now checks that its sym is a control variable
[elinstall.git] / elinstall.el
blobe65a823a1ff0e0b359e66fe6af7e54327403f7dd
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.
25 ;;;_ , Commentary:
27 ;; Entry points:
28 ;; elinstall Use this for overall loading
30 ;; elinstall-arrange-preload - Use this for non-autogenerated
31 ;; files that need to be linked in.
33 ;; elinstall-update-directory-autoloads
34 ;; elinstall-update-file-autoloads
36 ;;;_ , Requires
38 (require 'autoload)
39 (require 'pp)
40 (require 'cus-edit) ;;Because we save "installedness" manually
41 (require 'byte-compile nil t) ;;
44 ;;;_. Body
45 ;;;_ , Customizations
46 ;;;_ . Group
47 (defgroup elinstall
48 '()
49 "Customizations for elinstall"
50 :group 'development)
51 ;;;_ . elinstall-default-priority
52 (defcustom elinstall-default-priority
54 "Default priority for site-start"
55 :group 'elinstall
56 :type 'integer)
57 ;;;_ . elinstall-default-preload-target
58 (defcustom elinstall-default-preload-target
59 "~/.emacs.d/site-start.d/"
60 "Default preload-target for registering autoloads"
61 :group 'elinstall
62 :type
63 '(choice
64 (const "~/.emacs.d/site-start.d/")
65 (const "/etc/emacs/site-start.d/")
66 (directory "" )
67 (const nil)
68 (const 'dot-emacs)))
69 ;;;_ . elinstall-already-installed
70 (defvar elinstall-already-installed
71 '()
72 "(AUTOMATIC) Things that have already been installed.
73 This exists for recording what has been installed.
75 Though it's saved as customizable, user interaction is not
76 contemplated." )
77 ;;;_ , Types
78 ;;;_ . elinstall-stages
79 (defstruct (elinstall-stages
80 (:constructor elinstall-make-stages)
81 (:conc-name elinstall-stages->)
82 (:copier nil))
83 "The elinstall stages"
84 build-deffiles
85 run-tests
86 byte-compile
87 arrange-preloads)
88 ;;;_ , Data
89 ;;;_ . Regular expressions
90 ;;;_ , elinstall-elisp-regexp
91 (defconst elinstall-elisp-regexp
92 (let ((tmp nil))
93 (dolist
94 (suf (get-load-suffixes))
95 (unless (string-match "\\.elc" suf) (push suf tmp)))
96 (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
97 "Regular expression that matches elisp files" )
98 ;;;_ , Utilities
99 ;;;_ . elinstall-directory-true-name
100 (defun elinstall-directory-true-name ()
101 "Get the true name of the directory the calling code lives in.
102 CAUTION: This is sensitive to where it's called. That's the point of it."
103 (file-name-directory
104 (if load-file-name
105 (file-truename load-file-name)
106 (file-truename buffer-file-name))))
107 ;;;_ . Checking installedness
108 ;;;_ , elinstall-already-installed
109 (defun elinstall-already-installed (project-name)
110 "Return non-nil if PROJECT-NAME has been installed."
111 (member project-name elinstall-already-installed))
113 ;;;_ , elinstall-record-installed
114 (defun elinstall-record-installed (project-name)
115 "Record that PROJECT-NAME has been installed."
117 (add-to-list 'elinstall-already-installed project-name)
118 (customize-save-variable
119 'elinstall-already-installed
120 elinstall-already-installed
121 "Set by elinstall-record-installed"))
122 ;;;_ . Finding deffiles
123 ;;;_ , elinstall-expand-deffile-name
124 (defun elinstall-expand-deffile-name (deffile)
125 "Expand DEFFILE autoload.el's way."
127 (expand-file-name (or deffile "loaddefs.el")
128 (expand-file-name "lisp"
129 source-directory)))
131 ;;;_ , Work
132 ;;;_ . Doing actions
134 ;;;_ , Doing autoload actions (adapted from autoload.el)
135 ;;;_ . Utilities about the action list
136 ;;;_ , elinstall-remove-autogen-action
137 (defun elinstall-remove-autogen-action (file actions)
138 "Return ACTIONS minus any add-file-autoloads on FILE removed."
140 (delq nil
141 (mapcar
142 #'(lambda (act)
143 (case (car act)
144 (add-file-autoloads
145 (if (equal file (third act))
147 act))
148 (t act)))
149 actions)))
150 ;;;_ , elinstall-get-autogen-action
151 (defun elinstall-get-autogen-action (file actions)
153 (let
154 ((the-act))
155 (dolist (act actions)
156 (case (car act)
157 (add-file-autoloads
158 (when (equal file (third act))
159 (setq the-act act)))))
160 the-act))
161 ;;;_ . About printing to autoload file
162 ;;;_ , elinstall-insert-section-header
163 (defun elinstall-insert-section-header (outbuf form)
164 "Insert the section-header line,
165 which lists the file name and which functions are in it, etc."
166 (insert generate-autoload-section-header)
167 (prin1 form outbuf)
168 (terpri outbuf)
169 ;; Break that line at spaces, to avoid very long lines.
170 ;; Make each sub-line into a comment.
171 (with-current-buffer outbuf
172 (save-excursion
173 (forward-line -1)
174 (while (not (eolp))
175 (move-to-column 64)
176 (skip-chars-forward "^ \n")
177 (or (eolp)
178 (insert "\n" generate-autoload-section-continuation))))))
180 ;;;_ , elinstall-insert-autoload-section
181 (defun elinstall-insert-autoload-section (text form &optional comment-string)
182 "Insert TEXT into current buffer as an autoload section"
184 (let* ( ;; This does something in Lucid Emacs.
185 (print-length nil)
186 (print-readably t)
187 (float-output-format nil))
189 (elinstall-insert-section-header (current-buffer) form)
190 (when comment-string
191 (insert ";;; " comment-string "\n"))
192 (insert text)
193 (insert generate-autoload-section-trailer)))
195 ;;;_ . Making autoloads
196 ;;;_ , elinstall-make-autoload-action
197 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
198 "Return the autoloads for current buffer as a string"
200 ;;We put all the text into a temp buffer, then get that buffer's
201 ;;string.
202 (let
203 ((outbuf
204 (generate-new-buffer " *temp*"))
205 (autoloads-done '())
206 (short-name
207 (file-name-nondirectory full-path))
209 ;; Apparently this does something in Lucid Emacs.
210 (print-length nil)
211 (print-readably t)
212 (float-output-format nil)
214 ;;load-name relative to a member of load-path.
215 (relative-name
216 (file-name-sans-extension
217 (file-relative-name
218 full-path
219 load-path-element))))
221 (with-current-buffer buf
222 (unwind-protect
223 (save-excursion
224 (save-restriction
225 (widen)
226 (goto-char (point-min))
227 (message "Finding autoloads for %s..." short-name)
228 (while (not (eobp))
229 (skip-chars-forward " \t\n\f")
230 (cond
231 ((looking-at (regexp-quote generate-autoload-cookie))
232 (search-forward generate-autoload-cookie)
233 (skip-chars-forward " \t")
234 (setq done-any t)
235 (if (eolp)
236 ;; Read the next form and make an autoload.
237 (let* ((form (prog1 (read (current-buffer))
238 (or (bolp) (forward-line 1))))
239 (autoload
240 (make-autoload form relative-name)))
241 (if autoload
242 (push (nth 1 form) autoloads-done)
243 (setq autoload form))
244 (let ((autoload-print-form-outbuf outbuf))
245 (autoload-print-form autoload)))
247 ;; Copy the rest of the line to the output.
248 (princ (buffer-substring
249 (progn
250 ;; Back up over whitespace,
251 ;; to preserve it.
252 (skip-chars-backward " \f\t")
253 (if (= (char-after (1+ (point))) ? )
254 ;; Eat one space.
255 (forward-char 1))
256 (point))
257 (progn (forward-line 1) (point)))
258 outbuf)))
259 ((looking-at ";")
260 ;; Don't read the comment.
261 (forward-line 1))
263 (forward-sexp 1)
264 (forward-line 1))))
265 (message "Finding autoloads for %s...done" short-name))
267 ;;Return this action. The temp buffer's contents is
268 ;;our final string.
269 `(add-file-autoloads
270 ,def-file
271 ,relative-name
272 ,full-path
273 ,(with-current-buffer outbuf (buffer-string))
274 ,autoloads-done))
276 ;;This in unwind-protected
277 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
280 ;;;_ , elinstall-generate-file-autoloads
282 (defun elinstall-generate-file-autoloads
283 (relative-name full-name text autoloads-done)
284 "Insert at point a loaddefs autoload section for FILE.
285 Autoloads are generated for defuns and defmacros in FILE
286 marked by `generate-autoload-cookie' (which see).
287 If FILE is being visited in a buffer, the contents of the buffer
288 are used.
289 Return non-nil in the case where no autoloads were added at point.
291 FULL-NAME is the absolute name of the file.
292 RELATIVE-NAME is its name respective to some component of load-path."
293 (if (not (equal text ""))
294 ;; Insert the section-header line which lists the file name and
295 ;; which functions are in it, etc.
296 (elinstall-insert-autoload-section
297 text
298 (list 'autoloads
299 autoloads-done
300 relative-name
301 (autoload-trim-file-name full-name)
302 (nth 5 (file-attributes full-name)))
303 (concat
304 "Generated autoloads from "
305 (autoload-trim-file-name full-name)))
308 ;;;_ , elinstall-deffile-insert-autoloads
309 (defun elinstall-deffile-insert-autoloads (file args)
310 "Update the autoloads for FILE in current buffer.
311 Return FILE if there was no autoload cookie in it, else nil.
313 Current buffer must be a loaddef-style file.
315 LOAD-NAME is the absolute name of the file.
316 RELATIVE-NAME is its name respective to some component of load-path."
317 (let (
318 (found nil)
319 (no-autoloads nil))
321 (save-excursion
322 (save-restriction
323 (widen)
324 (goto-char (point-min))
325 ;; Look for the section for FILE
326 (while (and (not found)
327 (search-forward generate-autoload-section-header nil t))
328 (let ((form (autoload-read-section-header)))
329 (cond
330 ((equal (nth 2 form) file)
331 ;; We found the section for this file.
332 (let ((begin (match-beginning 0)))
333 (progn
334 (search-forward generate-autoload-section-trailer)
335 (delete-region begin (point))
336 (setq found t))))
337 ((string< file (nth 2 form))
338 ;; We've come to a section alphabetically later than
339 ;; FILE. We assume the file is in order and so
340 ;; there must be no section for FILE. We will
341 ;; insert one before the section here.
342 (goto-char (match-beginning 0))
343 (setq found 'new)))))
344 (unless found
345 (progn
346 (setq found 'new)
347 ;; No later sections in the file. Put before the last page.
348 (goto-char (point-max))
349 (search-backward "\f" nil t)))
350 (setq no-autoloads
351 (apply #'elinstall-generate-file-autoloads
352 file args))))
354 (if no-autoloads file nil)))
355 ;;;_ . Arranging to add to info-path and load-path
356 ;;;_ , elinstall-generate-add-to-path
357 (defun elinstall-generate-add-to-path (path-element type)
358 "Insert code at point to add PATH-ELEMENT to a path.
359 If TYPE is:
360 * `add-to-load-path', add to load-path
361 * `add-to-info-path', add to Info-additional-directory-list
363 Current buffer must be a loaddef-style file."
364 (let ( (path-symbol
365 (case type
366 (add-to-load-path 'load-path)
367 (add-to-info-path 'Info-additional-directory-list)
368 (t (error "Type not recognized"))))
369 (description
370 (case type
371 (add-to-load-path "load-path")
372 (add-to-info-path "info-path")))
373 (autoloads-done '())
374 (print-length nil)
375 (print-readably t) ; This does something in Lucid Emacs.
376 (float-output-format nil))
378 (message "Generating %s additions..." description)
380 (elinstall-insert-autoload-section
381 (pp-to-string
382 `(add-to-list ',path-symbol
383 (expand-file-name
384 ,(file-relative-name path-element)
385 (if load-file-name
386 (file-name-directory
387 (file-truename load-file-name))))))
388 (list type (list path-element) nil nil nil)
389 nil)
390 (message "Generating %s additions...done" description)))
393 ;;;_ , elinstall-deffile-insert-add-to-path
394 (defun elinstall-deffile-insert-add-to-path (path-element type)
395 "Insert code in current buffer to add PATH-ELEMENT to a path.
396 If TYPE is:
397 * `add-to-load-path', add to load-path
398 * `add-to-info-path', add to Info-default-directory-list
400 Current buffer must be a loaddef-style file."
401 (let (
402 (found nil)
403 (no-autoloads nil))
405 (save-excursion
406 (save-restriction
407 (widen)
408 (goto-char (point-min))
409 ;; Look for the section for PATH-ELEMENT
410 (while (and (not found)
411 (search-forward generate-autoload-section-header nil t))
412 (let ((form (autoload-read-section-header)))
413 (cond
414 ((and
415 (equal (nth 0 form) type)
416 (member path-element (nth 1 form)))
418 ;; We found the section for this add.
419 (let ((begin (match-beginning 0)))
420 (progn
421 (search-forward generate-autoload-section-trailer)
422 (delete-region begin (point))
423 (setq found t)))))))
425 (unless found
426 (progn
427 (setq found 'new)
428 ;; No later sections in the file. Put before the last page.
429 (goto-char (point-max))
430 (search-backward "\f" nil t)))
432 (elinstall-generate-add-to-path path-element type)))
434 ;;This never belongs in the no-autoloads section.
435 nil))
436 ;;;_ . elinstall-deffile-insert
438 (defun elinstall-deffile-insert (action)
439 "Insert autoloads etc into current file according to ACTION.
440 The format of ACTION is described in the design docs.
442 Return filename if this action belongs in the no-autoload section."
444 (when action
445 (case (car action)
446 (add-file-autoloads
447 (elinstall-deffile-insert-autoloads
448 (third action)
449 (nthcdr 3 action)))
451 (add-to-load-path
452 (elinstall-deffile-insert-add-to-path
453 (third action)
454 'add-to-load-path)
455 nil)
457 (add-to-info-path
458 (elinstall-deffile-insert-add-to-path
459 (third action)
460 'add-to-info-path)
461 nil)
463 ((preload-file run-tests byte-compile)
464 (error "This case should not come here.")))))
466 ;;;_ . elinstall-prepare-deffile
467 (defun elinstall-prepare-deffile (deffile)
468 "Try to ensure that DEFFILE is available for receiving autoloads"
470 (autoload-ensure-default-file deffile)
471 (with-current-buffer (find-file-noselect deffile)
474 ;; We must read/write the file without any code conversion,
475 ;; but still decode EOLs.
476 (let ((coding-system-for-read 'raw-text))
478 ;; This is to make generated-autoload-file have Unix EOLs, so
479 ;; that it is portable to all platforms.
480 (setq buffer-file-coding-system 'raw-text-unix))
481 (or (> (buffer-size) 0)
482 (error "Autoloads file %s does not exist" buffer-file-name))
483 (or (file-writable-p buffer-file-name)
484 (error "Autoloads file %s is not writable"
485 buffer-file-name))))
487 ;;;_ . elinstall-update-deffile
489 ;;Adapted from autoload.el `update-directory-autoloads'.
491 (defun elinstall-update-deffile (target actions &optional
492 use-load-path force)
494 Update file TARGET with current autoloads as specified by ACTIONS.
495 Also remove any old definitions pointing to libraries that can no
496 longer be found.
498 ACTIONS must be a list of actions (See the format doc). Each one's
499 filename must be relative to some element of load-path.
501 USE-LOAD-PATH is a list to use as load-path. It should include
502 any new load-path that we are arranging to create. If it's not given,
503 load-path itself is used.
505 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
506 Other non-nil cases of FORCE are reserved for future development.
508 This uses `update-file-autoloads' (which see) to do its work.
509 In an interactive call, you must give one argument, the name
510 of a single directory."
511 (let
513 (use-load-path (or use-load-path load-path))
514 (this-time (current-time))
515 ;;files with no autoload cookies.
516 (no-autoloads nil))
518 (elinstall-prepare-deffile target)
519 (with-current-buffer
520 (find-file-noselect target)
521 (save-excursion
522 (setq actions
523 (elinstall-remove-autogen-action
524 (autoload-trim-file-name target)
525 actions))
527 (goto-char (point-min))
528 (while (search-forward generate-autoload-section-header nil t)
529 (let* ((form (autoload-read-section-header))
530 (file (nth 3 form)))
531 (cond ((and (consp file) (stringp (car file)))
532 ;; This is a list of files that have no
533 ;; autoload cookies.
534 ;; There shouldn't be more than one such entry.
535 ;; Remove the obsolete section.
536 (autoload-remove-section (match-beginning 0))
537 (let ((last-time (nth 4 form)))
538 (dolist (file file)
539 (let ((file-time (nth 5 (file-attributes file))))
540 (when (and file-time
541 (not (time-less-p last-time file-time)))
542 ;; file unchanged
543 (push file no-autoloads)
544 (setq actions
545 (elinstall-remove-autogen-action
546 file actions)))))))
547 ((not (stringp file)))
549 (let
550 ((file-path
551 (locate-library file nil use-load-path)))
552 (cond
553 ;;File doesn't exist, so remove its
554 ;;section.
555 ((not file-path)
556 (autoload-remove-section
557 (match-beginning 0)))
559 ;; File hasn't changed, so do nothing.
560 ((equal
561 (nth 4 form)
562 (nth 5 (file-attributes file-path)))
563 nil)
565 (elinstall-deffile-insert
566 (elinstall-get-autogen-action
567 file actions))))
569 (setq actions
570 (elinstall-remove-autogen-action
571 file actions))))))))
573 ;; Remaining actions have no existing autoload sections yet.
574 (setq no-autoloads
575 (append no-autoloads
576 (delq nil (mapcar #'elinstall-deffile-insert actions))))
577 (when no-autoloads
578 ;; Sort them for better readability.
579 (setq no-autoloads (sort no-autoloads 'string<))
580 ;; Add the `no-autoloads' section.
581 (goto-char (point-max))
582 (search-backward "\f" nil t)
583 (elinstall-insert-autoload-section
585 (list 'autoloads nil nil no-autoloads this-time)))
586 (save-buffer))))
588 ;;;_ . elinstall-stage-update-deffiles
589 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
590 "Update any deffiles mentioned in SEGMENT-LIST.
591 FORCE and USE-LOAD-PATH have the same meaning as in
592 `elinstall-update-deffile'.
594 (mapcar
595 #'(lambda (segment)
596 (let*
597 ((deffile (car segment)))
598 (if (stringp deffile)
599 (elinstall-update-deffile deffile (cdr segment) force
600 use-load-path))))
601 segment-list))
603 ;;;_ , Doing actions to arrange preloads
604 ;;;_ . elinstall-symlink-on-emacs-start
605 (defun elinstall-symlink-on-emacs-start
606 (filename target-basename target-dir &optional priority force)
607 "Symlink to TARGET-BASENAME.el in TARGET-DIR
609 If PRIORITY is given, it will be used as the priority prefix,
610 otherwise elinstall-default-priority will be.
611 PRIORITY must be an integer or nil.
612 If FORCE is `t', do it regardless of timestamps etc.
613 Other non-nil cases of FORCE are reserved for future development."
614 (let*
616 (priority (or priority elinstall-default-priority))
617 (target-name-nodir
618 (format
619 "%d%s.el"
620 priority
621 target-basename))
622 (target
623 (expand-file-name target-name-nodir target-dir)))
626 (cond
627 ;;Path should already exist.
628 ((not
629 (file-exists-p target-dir))
630 (message "The target directory doesn't exist."))
631 ;;Target shouldn't already exist, but if force is given, let
632 ;;user override.
633 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
634 ;;do nothing even on force.
635 ((and
636 (file-exists-p target)
638 (not force)
639 (not
640 (yes-or-no-p
641 (format "Really overwrite %s? " target))))
642 (message "File %s already exists" target)))
645 (make-symbolic-link
646 filename
647 target
648 nil)))))
650 ;;;_ . elinstall-add-to-dot-emacs
651 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
652 "Add code to load FILENAME to .emacs.
653 FILENAME should not have an extension"
655 ;;Visit .emacs
656 (with-current-buffer (find-file-noselect dot-emacs-name)
657 (save-excursion
658 ;;add at end of file
659 (goto-char (point-max))
660 (insert "\n;;Added by elinstall")
661 (insert "\n;;Consider using my-site-start to manage .emacs\n")
662 (pp `(load ,filename) (current-buffer))
663 (save-buffer))))
666 ;;;_ . elinstall-arrange-preload
667 ;;;###autoload
668 (defun elinstall-arrange-preload (force filename basename &optional priority)
669 "Arrange for FILENAME to be loaded on emacs start.
670 FORCE has its usual meaning.
671 BASENAME and PRIORITY are used as arguments to
672 `elinstall-symlink-on-emacs-start'.
675 (let
676 ((preload-target elinstall-default-preload-target))
678 ;;Dispatch the possibilities.
679 (cond
680 ((eq preload-target 'dot-emacs)
681 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
682 ((stringp preload-target)
683 (elinstall-symlink-on-emacs-start
684 filename basename preload-target priority force))
685 ((null preload-target)
686 (message "Not arranging for preloads"))
688 (message "I don't recognize that")))))
689 ;;;_ . elinstall-stage-arrange-preloads
690 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
691 "Arrange any preloads mentioned in ACTIONS."
693 (mapcar
694 #'(lambda (act)
695 (case (car act)
696 (preload-file
697 (let*
698 ( (filename
699 (caddr act))
700 (proceed-p
701 (case (second act)
702 ((t) t)
703 ((nil) nil)
704 (if-used
705 (member filename deffiles-used)))))
707 (when proceed-p
708 (apply
709 #'elinstall-arrange-preload
710 force
711 (cddr act)))))
713 (error
714 "elinstall-stage-arrange-preloads: Action not
715 recognized."))) )
716 actions))
719 ;;;_ , Run tests
720 ;;;_ . elinstall-stage-run-tests
721 (defun elinstall-stage-run-tests (actions)
722 "Run any tests mentioned in ACTIONS."
724 (mapcar
725 #'(lambda (act)
726 (case (car act)
727 (run-tests
728 ;;$$WRITE ME - not a high priority right now.
729 nil)
731 (error
732 "elinstall-stage-run-tests: Action not
733 recognized."))) )
734 actions))
737 ;;;_ , Byte compile
738 ;;;_ . elinstall-stage-byte-compile
739 (defun elinstall-stage-byte-compile (actions)
740 "Do any byte-compilation mentioned in ACTIONS."
742 (mapcar
743 #'(lambda (act)
744 (case (car act)
745 ;;$$IMPROVE ME Understand flags to control second
746 ;;argument (whether to load file after
747 ;;compilation)
748 (byte-compile
749 (byte-compile-file (second act)))
751 (error
752 "elinstall-stage-byte-compile: Action not
753 recognized."))) )
754 actions))
755 ;;;_ . Segregating actions
756 ;;;_ , elinstall-remove-empty-segs
757 (defun elinstall-remove-empty-segs (segment-list)
758 "Return SEGMENT-LIST minus any segments that have no actions.
759 Intended only for the deffile stage data."
760 (delq nil
761 (mapcar
762 #'(lambda (segment)
763 (if (cdr segment)
764 segment
765 nil))
766 segment-list)))
768 ;;;_ , elinstall-segregate-actions
769 (defun elinstall-segregate-actions (actions)
770 "Return actions segregated by deffile.
772 Returns a list whose elements are each a cons of:
773 * deffile filename or nil
774 * A list of actions to be done for that deffile."
776 (let
778 (build-deffiles '())
779 (run-tests '())
780 (byte-compile '())
781 (arrange-preloads '()))
783 (dolist (act actions)
784 (when act
785 (case (car act)
786 ((add-file-autoloads
787 add-to-info-path
788 add-to-load-path)
789 (let*
790 ((deffile-name (second act))
791 (cell-already
792 (assoc deffile-name build-deffiles)))
793 (if cell-already
794 ;;There are already actions on this deffile.
795 ;;Splice this action in.
796 (setcdr cell-already
797 (cons act (cdr cell-already)))
798 ;;There are no actions on this deffile. Add a
799 ;;place for them and include this action.
800 (push (list deffile-name act) build-deffiles))))
801 (preload-file
802 (push act arrange-preloads))
803 (run-tests
804 (push act run-tests))
805 (byte-compile
806 (push act byte-compile)))))
808 (elinstall-make-stages
809 :build-deffiles
810 (elinstall-remove-empty-segs build-deffiles)
811 :run-tests
812 run-tests
813 :byte-compile
814 byte-compile
815 :arrange-preloads
816 arrange-preloads)))
817 ;;;_ . Finding actions
818 ;;;_ , Informational
819 ;;;_ . elinstall-dir-has-info
821 ;;$$IMPROVE ME - Can this test be made more precise?
822 (defun elinstall-dir-has-info (dir)
823 "Return non-nil if DIR has info files in it.
824 DIR should be an absolute path."
826 (string-match "/info/" dir)
827 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
829 ;;;_ , Workers
830 ;;;_ . List of special variables used in this section
831 ;;load-path-element - The relevant element of load-path
832 ;;def-file - The file the autoload definitions etc will go into.
833 ;;add-to-load-path-p - Controls whether to add to load-path.
834 ;;recurse-dirs-p - Whether to recurse into subdirectories.
835 (defconst elinstall-find-actions-control-vars
836 '(add-to-load-path-p recurse-dirs-p)
837 "Control special variables that the find-actions tree recognizes" )
838 ;;;_ . elinstall-actions-for-source-file
839 (defun elinstall-actions-for-source-file (filename dir)
840 "Return a list of actions to do for FILENAME in DIR.
841 Special variables are as noted in \"List of special variables\"."
842 (declare (special
843 load-path-element def-file))
844 (let
845 ((full-path
846 (expand-file-name filename dir)))
847 (when
848 (and
849 (file-readable-p full-path)
850 (not (auto-save-file-name-p full-path)))
851 ;;$$IMPROVE ME create and use relevant control variables.
852 (let*
854 (visited (get-file-buffer full-path))
855 (buf
856 (or
857 visited
858 ;;Visit the file cheaply.
859 ;;hack-local-variables can give errors.
860 (ignore-errors (autoload-find-file full-path))))
861 (def-file
863 (ignore-errors
864 (with-current-buffer buf
865 (if (local-variable-p 'generated-autoload-file)
866 (elinstall-expand-deffile-name
867 generated-autoload-file)
868 nil)))
869 def-file))
870 ;;Figure out whether to run some actions, by file local vars.
871 (autoloads-p
872 (ignore-errors
873 (with-current-buffer buf
874 (not no-update-autoloads))))
875 (compile-p
876 (and
877 (featurep 'byte-compile)
878 (string-match emacs-lisp-file-regexp filename)
879 (ignore-errors
880 (with-current-buffer buf
881 (not no-byte-compile)))
882 (let*
883 ((dest (byte-compile-dest-file full-path))
884 ;;$$PUNT - currently, we wouldn't have
885 ;;gotten here if we weren't intending to do
886 ;;everything so set force variables accordingly.
887 (recompile-p nil)
888 (compile-new t))
889 (if (file-exists-p dest)
890 ;; File was already compiled.
891 (or recompile-p (file-newer-than-file-p full-path dest))
892 (or compile-new
893 (y-or-n-p (concat "Compile " filename "? "))))))))
895 (prog1
896 (list
897 (if compile-p
898 `(byte-compile ,full-path)
899 nil)
900 (if autoloads-p
901 (elinstall-make-autoload-action
902 buf def-file load-path-element full-path)
903 nil))
904 (unless visited (kill-buffer-if-not-modified buf)))))))
905 ;;;_ . elinstall-actions-for-dir
906 (defun elinstall-actions-for-dir (dirname &optional recurse-dirs-p)
907 "Make actions for DIR.
908 Recurse just if RECURSE-DIRS-P"
909 (declare (special
910 load-path-element def-file add-to-load-path-p))
911 ;;This does not treat symlinks specially. $$IMPROVE ME it could
912 ;;treat/not treat them conditional on control variables.
913 (let*
915 ;;Relative filenames of the source files. We know our
916 ;;loaddefs.el isn't really source so remove it. We'd have
917 ;;removed it anyways after seeing file local vars.
919 (elisp-source-files
920 (remove def-file
921 (directory-files
922 dirname
923 nil
924 elinstall-elisp-regexp)))
925 ;;Absolute filenames of subdirectories.
926 ;;Don't accept any directories beginning with dot. If user
927 ;;really wants to explore one he can use `(dir ".NAME")'.
928 (sub-dirs
929 (if recurse-dirs-p
930 (delq nil
931 (mapcar
932 #'(lambda (filename)
934 (file-directory-p filename)
935 filename
936 nil))
937 (directory-files
938 dirname t
939 "[^\\.]")))
940 '()))
942 (load-path-here
943 (and
944 elisp-source-files ;;If list is not empty.
945 add-to-load-path-p))
946 (load-path-element
947 (if load-path-here
948 dirname
949 load-path-element)))
951 (append
952 ;;Sometimes arrange to add this directory to load-path.
953 (if load-path-here
954 `((add-to-load-path
955 ,def-file
956 ,load-path-element))
957 '())
959 ;;$$IMPROVE ME - be controlled by a control variable.
960 ;;Sometimes add this directory to info path.
962 (elinstall-dir-has-info dirname)
963 `((add-to-info-path
964 ,def-file
965 "."))
966 '())
968 (apply #'nconc
969 (mapcar
970 #'(lambda (filename)
971 (elinstall-actions-for-source-file
972 filename
973 dirname))
974 elisp-source-files))
976 (if recurse-dirs-p
977 (apply #'nconc
978 (mapcar
979 #'(lambda (filename)
980 (elinstall-find-actions-by-spec-x
982 (expand-file-name
983 filename
984 dirname)))
985 sub-dirs))
986 '()))))
988 ;;;_ . elinstall-find-actions-by-spec-x
990 (defun elinstall-find-actions-by-spec-x (spec dir)
991 "Return a list of actions to do, controlled by SPEC."
992 (declare (special
993 load-path-element def-file add-to-load-path-p
994 recurse-dirs-p))
996 (if (consp spec)
997 (case (car spec)
999 ;;(in FN SPEC)
1000 (let
1001 ((new-dir
1002 (expand-file-name
1003 (second spec)
1004 dir)))
1006 (elinstall-find-actions-by-spec-x
1007 (third spec)
1008 new-dir)))
1010 (all
1011 ;;(all . SPEC*)
1012 (apply #'nconc
1013 (mapcar
1014 #'(lambda (sub-spec)
1015 (elinstall-find-actions-by-spec-x
1016 sub-spec
1017 dir))
1018 (cdr spec))))
1019 (matching
1020 ;;(matching PATTERN SPEC)
1021 (apply #'nconc
1022 (mapcar
1023 #'(lambda (dir)
1024 (elinstall-find-actions-by-spec-x
1025 (third spec)
1026 dir))
1027 (directory-files
1028 dir t (second spec))))
1030 (file
1031 ;;(file FN)
1032 (elinstall-actions-for-source-file
1033 (second spec) dir))
1035 ;;Rather than trying to bind all control variables each time
1036 ;;thru, we use `set' and `unwind-protect'.
1037 (control
1038 ;;control TYPE DISPOSITION SPEC
1039 (let
1040 ((key (second spec))
1041 old-value)
1042 (if (memq key elinstall-find-actions-control-vars)
1043 (unwind-protect
1044 (progn
1045 (set old-value (symbol-value key))
1046 (set key (third spec))
1047 (elinstall-find-actions-by-spec-x
1048 (second spec)
1049 dir))
1050 (set key old-value))
1051 (error "Unrecognized control variable %s" key))))
1054 (dir
1055 ;;(dir FN)
1056 (elinstall-actions-for-dir
1057 (expand-file-name
1058 (second spec)
1059 dir)
1060 nil))
1063 (load-path
1064 ;;(load-path SPEC)
1065 (append
1066 `((add-to-load-path ,def-file ,dir))
1067 (let
1068 ((load-path-element dir))
1069 (elinstall-find-actions-by-spec-x
1070 (second spec)
1071 dir))))
1074 (def-file
1075 ;;(def-file FN ARGS SPEC)
1076 (let
1077 ((def-file
1078 (expand-file-name
1079 (second spec)
1080 dir))
1081 (for-preload (third spec)))
1082 (assert (listp for-preload))
1083 (append
1084 (list
1086 (and for-preload (car for-preload))
1087 `(preload-file
1088 ,(car for-preload)
1089 ,def-file
1090 ,@(cdr for-preload))
1091 '()))
1093 (elinstall-find-actions-by-spec-x
1094 (fourth spec) dir)))))
1097 ;;Single symbols
1098 (case spec
1099 (dir
1100 (elinstall-actions-for-dir dir nil))
1101 ((t)
1102 (elinstall-actions-for-dir dir t)))))
1104 ;;;_ . elinstall-find-actions-by-spec
1105 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1108 (let
1109 ((load-path-element load-path-element)
1110 (def-file def-file)
1111 (add-to-load-path-p t)
1112 (recurse-dirs-p t))
1113 (declare (special
1114 load-path-element def-file add-to-load-path-p
1115 recurse-dirs-p))
1117 (elinstall-find-actions-by-spec-x
1118 spec dir)))
1120 ;;;_ . high-level work
1121 ;;;_ , elinstall-get-relevant-load-path
1122 (defun elinstall-get-relevant-load-path (actions)
1124 (delq nil
1125 (mapcar
1126 #'(lambda (act)
1127 (case (car act)
1128 (add-to-load-path
1129 (second act))
1130 (t nil)))
1131 actions)))
1132 ;;;_ , elinstall-get-deffile-list
1133 (defun elinstall-get-deffile-list (stages)
1134 "Get a list of deffile names"
1136 (mapcar
1137 #'car
1138 (elinstall-stages->build-deffiles stages)))
1140 ;;;_ , elinstall-x
1141 (defun elinstall-x (dir spec &optional force)
1143 (let*
1145 ;;This is just the default deffile, spec can override it.
1146 (def-file
1147 (elinstall-expand-deffile-name nil))
1148 (actions
1149 (elinstall-find-actions-by-spec
1150 spec
1153 def-file))
1154 (stages (elinstall-segregate-actions actions))
1155 (use-load-path
1156 (elinstall-get-relevant-load-path
1157 actions)))
1159 (elinstall-stage-update-deffiles
1160 (elinstall-stages->build-deffiles stages)
1161 force
1162 use-load-path)
1163 (elinstall-stage-arrange-preloads
1164 (elinstall-stages->arrange-preloads stages)
1165 (elinstall-get-deffile-list stages)
1166 force)
1167 (elinstall-stage-byte-compile
1168 (elinstall-stages->byte-compile stages))
1170 ;;;_ , Entry points
1171 ;;;_ . elinstall
1172 ;;;###autoload
1173 (defun elinstall (project-name path spec &optional force)
1174 "Install elisp files.
1175 They need not be a formal package.
1177 Parameters:
1179 PROJECT-NAME - the name of the project
1181 PATH - Path to the project.
1182 Suggestion: (elinstall-directory-true-name)
1184 SPEC - a spec for the autoloads etc to make. It can be as simple as
1185 \(dir \"\.\") for installing one directory.
1187 If FORCE is t, install a package even if it has already been
1188 installed. Other non-nil cases of FORCE are reserved for future
1189 development."
1191 (when
1192 (and
1193 (or
1194 force
1195 (not (elinstall-already-installed project-name)))
1196 (yes-or-no-p (format "Re-install %s? " project-name)))
1197 (elinstall-x
1198 path
1199 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1200 force)
1201 (elinstall-record-installed project-name)))
1205 ;;;_ . elinstall-update-directory-autoloads
1206 ;;$$SPLIT ME - one to just classically update autoloads, one to
1207 ;;install a file.
1208 ;;$$TEST ME
1209 ;;;###autoload
1210 (defun elinstall-update-directory-autoloads (dir)
1213 (interactive "DInstall all elisp files from directory: ")
1216 (let
1217 ((def-file-name
1218 (elinstall-expand-deffile-name
1219 generated-autoload-file)))
1221 (elinstall-x
1223 `(def-file ,def-file-name (nil) (dir ".")))))
1227 ;;;_ . elinstall-update-file-autoloads
1228 ;;$$SPLIT ME - one to just classically update autoloads, one to
1229 ;;install a file.
1230 ;;$$TEST ME
1231 ;;;###autoload
1232 (defun elinstall-update-file-autoloads (file)
1235 (interactive "fInstall elisp file: ")
1236 (let
1237 ((def-file-name
1238 ;;This is the default. File local vars can override it.
1239 (elinstall-expand-deffile-name
1240 generated-autoload-file)))
1241 (elinstall-x
1242 file
1243 `(def-file ,def-file-name (nil) (file ,file)))))
1244 ;;;_. Footers
1245 ;;;_ , Provides
1247 (provide 'elinstall)
1249 ;;;_ * Local emacs vars.
1250 ;;;_ + Local variables:
1251 ;;;_ + mode: allout
1252 ;;;_ + End:
1254 ;;;_ , End
1255 ;;; elinstall.el ends here