Cleanups after changeover
[elinstall.git] / elinstall.el
blobc59584f35eff6dd60c223bd6a719dca6a1f6ecf9
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 ;;;_ . elinstall-insert-section-header
162 (defun elinstall-insert-section-header (outbuf form)
163 "Insert the section-header line,
164 which lists the file name and which functions are in it, etc."
165 (insert generate-autoload-section-header)
166 (prin1 form outbuf)
167 (terpri outbuf)
168 ;; Break that line at spaces, to avoid very long lines.
169 ;; Make each sub-line into a comment.
170 (with-current-buffer outbuf
171 (save-excursion
172 (forward-line -1)
173 (while (not (eolp))
174 (move-to-column 64)
175 (skip-chars-forward "^ \n")
176 (or (eolp)
177 (insert "\n" generate-autoload-section-continuation))))))
179 ;;;_ . Making autoloads
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 ;;;_ , elinstall-make-autoload-action
196 (defun elinstall-make-autoload-action (buf def-file load-path-element full-path)
197 "Return the autoloads for current buffer as a string"
199 ;;We put all the text into a temp buffer, then get that buffer's
200 ;;string.
201 (let
202 ((outbuf
203 (generate-new-buffer " *temp*"))
204 (autoloads-done '())
205 (short-name
206 (buffer-name buf))
208 ;; Apparently this does something in Lucid Emacs.
209 (print-length nil)
210 (print-readably t)
211 (float-output-format nil)
213 ;;load-name relative to a member of load-path.
214 (relative-name
215 (file-name-sans-extension
216 (file-relative-name
217 full-path
218 load-path-element))))
220 (with-current-buffer buf
221 (unwind-protect
222 (save-excursion
223 (save-restriction
224 (widen)
225 (goto-char (point-min))
226 (message "Finding autoloads for %s..." short-name)
227 (while (not (eobp))
228 (skip-chars-forward " \t\n\f")
229 (cond
230 ((looking-at (regexp-quote generate-autoload-cookie))
231 (search-forward generate-autoload-cookie)
232 (skip-chars-forward " \t")
233 (setq done-any t)
234 (if (eolp)
235 ;; Read the next form and make an autoload.
236 (let* ((form (prog1 (read (current-buffer))
237 (or (bolp) (forward-line 1))))
238 (autoload
239 (make-autoload form relative-name)))
240 (if autoload
241 (push (nth 1 form) autoloads-done)
242 (setq autoload form))
243 (let ((autoload-print-form-outbuf outbuf))
244 (autoload-print-form autoload)))
246 ;; Copy the rest of the line to the output.
247 (princ (buffer-substring
248 (progn
249 ;; Back up over whitespace,
250 ;; to preserve it.
251 (skip-chars-backward " \f\t")
252 (if (= (char-after (1+ (point))) ? )
253 ;; Eat one space.
254 (forward-char 1))
255 (point))
256 (progn (forward-line 1) (point)))
257 outbuf)))
258 ((looking-at ";")
259 ;; Don't read the comment.
260 (forward-line 1))
262 (forward-sexp 1)
263 (forward-line 1))))
264 (message "Finding autoloads for %s...done" short-name))
266 ;;Return this action. The temp buffer's contents is
267 ;;our final string.
268 `(add-file-autoloads
269 ,def-file
270 ,relative-name
271 ,full-path
272 ,(with-current-buffer outbuf (buffer-string))
273 ,autoloads-done))
275 ;;This in unwind-protected
276 (when (buffer-live-p outbuf) (kill-buffer outbuf))))))
279 ;;;_ , elinstall-generate-file-autoloads
281 (defun elinstall-generate-file-autoloads
282 (relative-name full-name text autoloads-done)
283 "Insert at point a loaddefs autoload section for FILE.
284 Autoloads are generated for defuns and defmacros in FILE
285 marked by `generate-autoload-cookie' (which see).
286 If FILE is being visited in a buffer, the contents of the buffer
287 are used.
288 Return non-nil in the case where no autoloads were added at point.
290 FULL-NAME is the absolute name of the file.
291 RELATIVE-NAME is its name respective to some component of load-path."
292 (if (not (equal text ""))
293 ;; Insert the section-header line which lists the file name and
294 ;; which functions are in it, etc.
295 (elinstall-insert-autoload-section
296 text
297 (list 'autoloads
298 autoloads-done
299 relative-name
300 (autoload-trim-file-name full-name)
301 (nth 5 (file-attributes full-name)))
302 (concat
303 "Generated autoloads from "
304 (autoload-trim-file-name full-name)))
307 ;;;_ , elinstall-deffile-insert-autoloads
308 (defun elinstall-deffile-insert-autoloads (file args)
309 "Update the autoloads for FILE in current buffer.
310 Return FILE if there was no autoload cookie in it, else nil.
312 Current buffer must be a loaddef-style file.
314 LOAD-NAME is the absolute name of the file.
315 RELATIVE-NAME is its name respective to some component of load-path."
316 (let (
317 (found nil)
318 (no-autoloads nil))
320 (save-excursion
321 (save-restriction
322 (widen)
323 (goto-char (point-min))
324 ;; Look for the section for FILE
325 (while (and (not found)
326 (search-forward generate-autoload-section-header nil t))
327 (let ((form (autoload-read-section-header)))
328 (cond
329 ((equal (nth 2 form) file)
330 ;; We found the section for this file.
331 (let ((begin (match-beginning 0)))
332 (progn
333 (search-forward generate-autoload-section-trailer)
334 (delete-region begin (point))
335 (setq found t))))
336 ((string< file (nth 2 form))
337 ;; We've come to a section alphabetically later than
338 ;; FILE. We assume the file is in order and so
339 ;; there must be no section for FILE. We will
340 ;; insert one before the section here.
341 (goto-char (match-beginning 0))
342 (setq found 'new)))))
343 (unless found
344 (progn
345 (setq found 'new)
346 ;; No later sections in the file. Put before the last page.
347 (goto-char (point-max))
348 (search-backward "\f" nil t)))
349 (setq no-autoloads
350 (apply #'elinstall-generate-file-autoloads
351 file args))))
353 (if no-autoloads file nil)))
354 ;;;_ . Arranging to add to info-path and load-path
355 ;;;_ , elinstall-generate-add-to-path
356 (defun elinstall-generate-add-to-path (path-element type)
357 "Insert code at point to add PATH-ELEMENT to a path.
358 If TYPE is:
359 * `add-to-load-path', add to load-path
360 * `add-to-info-path', add to Info-default-directory-list
362 Current buffer must be a loaddef-style file."
363 (let ( (path-symbol
364 (case type
365 (add-to-load-path 'load-path)
366 (add-to-info-path 'Info-default-directory-list)
367 (t (error "Type not recognized"))))
368 (description
369 (case type
370 (add-to-load-path "load-path")
371 (add-to-info-path "info-path")))
372 (autoloads-done '())
373 (print-length nil)
374 (print-readably t) ; This does something in Lucid Emacs.
375 (float-output-format nil))
377 (message "Generating %s additions..." description)
380 (elinstall-insert-section-header
381 (current-buffer)
382 (list type (list path-element) nil nil nil))
384 (insert ";;; Generated path addition\n")
386 `(add-to-list ',path-symbol
387 (expand-file-name
388 ,(file-relative-name path-element)
389 (if load-file-name
390 (file-name-directory
391 (file-truename load-file-name)))))
392 (current-buffer))
394 (insert generate-autoload-section-trailer)
395 (message "Generating %s additions...done" description)))
398 ;;;_ , elinstall-deffile-insert-add-to-path
399 (defun elinstall-deffile-insert-add-to-path (path-element type)
400 "Insert code in current buffer to add PATH-ELEMENT to a path.
401 If TYPE is:
402 * `add-to-load-path', add to load-path
403 * `add-to-info-path', add to Info-default-directory-list
405 Current buffer must be a loaddef-style file."
406 (let (
407 (found nil)
408 (no-autoloads nil))
410 (save-excursion
411 (save-restriction
412 (widen)
413 (goto-char (point-min))
414 ;; Look for the section for PATH-ELEMENT
415 (while (and (not found)
416 (search-forward generate-autoload-section-header nil t))
417 (let ((form (autoload-read-section-header)))
418 (cond
419 ((and
420 (equal (nth 0 form) type)
421 (member path-element (nth 1 form)))
423 ;; We found the section for this add.
424 (let ((begin (match-beginning 0)))
425 (progn
426 (search-forward generate-autoload-section-trailer)
427 (delete-region begin (point))
428 (setq found t)))))))
430 (unless found
431 (progn
432 (setq found 'new)
433 ;; No later sections in the file. Put before the last page.
434 (goto-char (point-max))
435 (search-backward "\f" nil t)))
437 (elinstall-generate-add-to-path path-element type)))
439 ;;This never belongs in the no-autoloads section.
440 nil))
441 ;;;_ . elinstall-deffile-insert
443 (defun elinstall-deffile-insert (action)
444 "Insert autoloads etc into current file according to ACTION.
445 The format of ACTION is described in the design docs.
447 Return filename if this action belongs in the no-autoload section."
449 (when action
450 (case (car action)
451 (add-file-autoloads
452 (elinstall-deffile-insert-autoloads
453 (third action)
454 (nthcdr 3 action)))
456 (add-to-load-path
457 (elinstall-deffile-insert-add-to-path
458 (third action)
459 'add-to-load-path)
460 nil)
462 (add-to-info-path
463 (elinstall-deffile-insert-add-to-path
464 (third action)
465 'add-to-info-path)
466 nil)
468 ((preload-file run-tests byte-compile)
469 (error "This case should not come here.")))))
471 ;;;_ . elinstall-prepare-deffile
472 (defun elinstall-prepare-deffile (deffile)
473 "Try to ensure that DEFFILE is available for receiving autoloads"
475 (autoload-ensure-default-file deffile)
476 (with-current-buffer (find-file-noselect deffile)
479 ;; We must read/write the file without any code conversion,
480 ;; but still decode EOLs.
481 (let ((coding-system-for-read 'raw-text))
483 ;; This is to make generated-autoload-file have Unix EOLs, so
484 ;; that it is portable to all platforms.
485 (setq buffer-file-coding-system 'raw-text-unix))
486 (or (> (buffer-size) 0)
487 (error "Autoloads file %s does not exist" buffer-file-name))
488 (or (file-writable-p buffer-file-name)
489 (error "Autoloads file %s is not writable"
490 buffer-file-name))))
492 ;;;_ . elinstall-update-deffile
494 ;;Adapted from autoload.el `update-directory-autoloads'.
496 (defun elinstall-update-deffile (target actions &optional
497 use-load-path force)
499 Update file TARGET with current autoloads as specified by ACTIONS.
500 Also remove any old definitions pointing to libraries that can no
501 longer be found.
503 ACTIONS must be a list of actions (See the format doc). Each one's
504 filename must be relative to some element of load-path.
506 USE-LOAD-PATH is a list to use as load-path. It should include
507 any new load-path that we are arranging to create. If it's not given,
508 load-path itself is used.
510 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
511 Other non-nil cases of FORCE are reserved for future development.
513 This uses `update-file-autoloads' (which see) to do its work.
514 In an interactive call, you must give one argument, the name
515 of a single directory."
516 (let
518 (use-load-path (or use-load-path load-path))
519 (this-time (current-time))
520 ;;files with no autoload cookies.
521 (no-autoloads nil))
523 (elinstall-prepare-deffile target)
524 (with-current-buffer
525 (find-file-noselect target)
526 (save-excursion
527 (setq actions
528 (elinstall-remove-autogen-action
529 (autoload-trim-file-name target)
530 actions))
532 (goto-char (point-min))
533 (while (search-forward generate-autoload-section-header nil t)
534 (let* ((form (autoload-read-section-header))
535 (file (nth 3 form)))
536 (cond ((and (consp file) (stringp (car file)))
537 ;; This is a list of files that have no
538 ;; autoload cookies.
539 ;; There shouldn't be more than one such entry.
540 ;; Remove the obsolete section.
541 (autoload-remove-section (match-beginning 0))
542 (let ((last-time (nth 4 form)))
543 (dolist (file file)
544 (let ((file-time (nth 5 (file-attributes file))))
545 (when (and file-time
546 (not (time-less-p last-time file-time)))
547 ;; file unchanged
548 (push file no-autoloads)
549 (setq actions
550 (elinstall-remove-autogen-action
551 file actions)))))))
552 ((not (stringp file)))
554 (let
555 ((file-path
556 (locate-library file nil use-load-path)))
557 (cond
558 ;;File doesn't exist, so remove its
559 ;;section.
560 ((not file-path)
561 (autoload-remove-section
562 (match-beginning 0)))
564 ;; File hasn't changed, so do nothing.
565 ((equal
566 (nth 4 form)
567 (nth 5 (file-attributes file-path)))
568 nil)
570 (elinstall-deffile-insert
571 (elinstall-get-autogen-action
572 file actions))))
574 (setq actions
575 (elinstall-remove-autogen-action
576 file actions))))))))
578 ;; Remaining actions have no existing autoload sections yet.
579 (setq no-autoloads
580 (append no-autoloads
581 (delq nil (mapcar #'elinstall-deffile-insert actions))))
582 (when no-autoloads
583 ;; Sort them for better readability.
584 (setq no-autoloads (sort no-autoloads 'string<))
585 ;; Add the `no-autoloads' section.
586 (goto-char (point-max))
587 (search-backward "\f" nil t)
589 (elinstall-insert-section-header
590 (current-buffer)
591 (list 'autoloads nil nil no-autoloads this-time))
592 (insert generate-autoload-section-trailer))
593 (save-buffer))))
595 ;;;_ . elinstall-stage-update-deffiles
596 (defun elinstall-stage-update-deffiles (segment-list force use-load-path)
597 "Update any deffiles mentioned in SEGMENT-LIST.
598 FORCE and USE-LOAD-PATH have the same meaning as in
599 `elinstall-update-deffile'.
601 (mapcar
602 #'(lambda (segment)
603 (let*
604 ((deffile (car segment)))
605 (if (stringp deffile)
606 (elinstall-update-deffile deffile (cdr segment) force
607 use-load-path))))
608 segment-list))
610 ;;;_ , Doing actions to arrange preloads
611 ;;;_ . elinstall-symlink-on-emacs-start
612 (defun elinstall-symlink-on-emacs-start
613 (filename target-basename target-dir &optional priority force)
614 "Symlink to TARGET-BASENAME.el in TARGET-DIR
616 If PRIORITY is given, it will be used as the priority prefix,
617 otherwise elinstall-default-priority will be.
618 PRIORITY must be an integer or nil.
619 If FORCE is `t', do it regardless of timestamps etc.
620 Other non-nil cases of FORCE are reserved for future development."
621 (let*
623 (priority (or priority elinstall-default-priority))
624 (target-name-nodir
625 (format
626 "%d%s.el"
627 priority
628 target-basename))
629 (target
630 (expand-file-name target-name-nodir target-dir)))
633 (cond
634 ;;Path should already exist.
635 ((not
636 (file-exists-p target-dir))
637 (message "The target directory doesn't exist."))
638 ;;Target shouldn't already exist, but if force is given, let
639 ;;user override.
640 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
641 ;;do nothing even on force.
642 ((and
643 (file-exists-p target)
645 (not force)
646 (not
647 (yes-or-no-p
648 (format "Really overwrite %s? " target))))
649 (message "File %s already exists" target)))
652 (make-symbolic-link
653 filename
654 target
655 nil)))))
657 ;;;_ . elinstall-add-to-dot-emacs
658 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force &rest r)
659 "Add code to load FILENAME to .emacs.
660 FILENAME should not have an extension"
662 ;;Visit .emacs
663 (with-current-buffer (find-file-noselect dot-emacs-name)
664 (save-excursion
665 ;;add at end of file
666 (goto-char (point-max))
667 (insert "\n;;Added by elinstall")
668 (insert "\n;;Consider using my-site-start to manage .emacs\n")
669 (pp `(load ,filename) (current-buffer))
670 (save-buffer))))
673 ;;;_ . elinstall-arrange-preload
674 ;;;###autoload
675 (defun elinstall-arrange-preload (force filename basename &optional priority)
676 "Arrange for FILENAME to be loaded on emacs start.
677 FORCE has its usual meaning.
678 BASENAME and PRIORITY are used as arguments to
679 `elinstall-symlink-on-emacs-start'.
682 (let
683 ((preload-target elinstall-default-preload-target))
685 ;;Dispatch the possibilities.
686 (cond
687 ((eq preload-target 'dot-emacs)
688 (elinstall-add-to-dot-emacs "~/.emacs" filename force))
689 ((stringp preload-target)
690 (elinstall-symlink-on-emacs-start
691 filename basename preload-target priority force))
692 ((null preload-target)
693 (message "Not arranging for preloads"))
695 (message "I don't recognize that")))))
696 ;;;_ . elinstall-stage-arrange-preloads
697 (defun elinstall-stage-arrange-preloads (actions deffiles-used force)
698 "Arrange any preloads mentioned in ACTIONS."
700 (mapcar
701 #'(lambda (act)
702 (case (car act)
703 (preload-file
704 (let*
705 ( (filename
706 (caddr act))
707 (proceed-p
708 (case (second act)
709 ((t) t)
710 ((nil) nil)
711 (if-used
712 (member filename deffiles-used)))))
714 (when proceed-p
715 (apply
716 #'elinstall-arrange-preload
717 force
718 (cddr act)))))
720 (error
721 "elinstall-stage-arrange-preloads: Action not
722 recognized."))) )
723 actions))
726 ;;;_ , Run tests
727 ;;;_ . elinstall-stage-run-tests
728 (defun elinstall-stage-run-tests (actions)
729 "Run any tests mentioned in ACTIONS."
731 (mapcar
732 #'(lambda (act)
733 (case (car act)
734 (run-tests
735 ;;$$WRITE ME - not a high priority right now.
736 nil)
738 (error
739 "elinstall-stage-run-tests: Action not
740 recognized."))) )
741 actions))
744 ;;;_ , Byte compile
745 ;;;_ . elinstall-stage-byte-compile
746 (defun elinstall-stage-byte-compile (actions)
747 "Do any byte-compilation mentioned in ACTIONS."
749 (mapcar
750 #'(lambda (act)
751 (case (car act)
752 ;;$$IMPROVE ME Understand flags to control second
753 ;;argument (whether to load file after
754 ;;compilation)
755 (byte-compile
756 (byte-compile-file (second act)))
758 (error
759 "elinstall-stage-byte-compile: Action not
760 recognized."))) )
761 actions))
762 ;;;_ . Segregating actions
763 ;;;_ , elinstall-remove-empty-segs
764 (defun elinstall-remove-empty-segs (segment-list)
765 "Return SEGMENT-LIST minus any segments that have no actions.
766 Intended only for the deffile stage data."
767 (delq nil
768 (mapcar
769 #'(lambda (segment)
770 (if (cdr segment)
771 segment
772 nil))
773 segment-list)))
775 ;;;_ , elinstall-segregate-actions
776 (defun elinstall-segregate-actions (actions)
777 "Return actions segregated by deffile.
779 Returns a list whose elements are each a cons of:
780 * deffile filename or nil
781 * A list of actions to be done for that deffile."
783 (let
785 (build-deffiles '())
786 (run-tests '())
787 (byte-compile '())
788 (arrange-preloads '()))
790 (dolist (act actions)
791 (when act
792 (case (car act)
793 ((add-file-autoloads
794 add-to-info-path
795 add-to-load-path)
796 (let*
797 ((deffile-name (second act))
798 (cell-already
799 (assoc deffile-name build-deffiles)))
800 (if cell-already
801 ;;There are already actions on this deffile.
802 ;;Splice this action in.
803 (setcdr cell-already
804 (cons act (cdr cell-already)))
805 ;;There are no actions on this deffile. Add a
806 ;;place for them and include this action.
807 (push (list deffile-name act) build-deffiles))))
808 (preload-file
809 (push act arrange-preloads))
810 (run-tests
811 (push act run-tests))
812 (byte-compile
813 (push act byte-compile)))))
815 (elinstall-make-stages
816 :build-deffiles
817 (elinstall-remove-empty-segs build-deffiles)
818 :run-tests
819 run-tests
820 :byte-compile
821 byte-compile
822 :arrange-preloads
823 arrange-preloads)))
824 ;;;_ . Finding actions
825 ;;;_ , Informational
826 ;;;_ . elinstall-dir-has-info
828 ;;$$IMPROVE ME - Can this test be made more precise?
829 (defun elinstall-dir-has-info (dir)
830 "Return non-nil if DIR has info files in it.
831 DIR should be an absolute path."
833 (string-match "/info/" dir)
834 (directory-files dir nil "\\.info\\(-[0-9]+\\)?\\(\\.gz\\)?$")))
836 ;;;_ , Workers
837 ;;;_ . List of special variables used in this section
838 ;;load-path-element - The relevant element of load-path
839 ;;def-file - The file the autoload definitions etc will go into.
840 ;;add-to-load-path-p - Controls whether to add to load-path.
842 ;;;_ . elinstall-actions-for-source-file
843 (defun elinstall-actions-for-source-file (filename dir)
844 "Return a list of actions to do for FILENAME in DIR.
845 Special variables are as noted in \"List of special variables\"."
846 (declare (special
847 load-path-element def-file))
848 (let
849 ((full-path
850 (expand-file-name filename dir)))
851 (when
852 (and
853 (file-readable-p full-path)
854 (not (auto-save-file-name-p full-path)))
855 ;;$$IMPROVE ME create and use relevant control variables.
856 (let*
858 (visited (get-file-buffer full-path))
859 (buf
860 (or
861 visited
862 ;;Visit the file cheaply.
863 ;;hack-local-variables can give errors.
864 (ignore-errors (autoload-find-file full-path))))
865 (def-file
867 (ignore-errors
868 (with-current-buffer buf
869 (if (local-variable-p 'generated-autoload-file)
870 (elinstall-expand-deffile-name
871 generated-autoload-file)
872 nil)))
873 def-file))
874 ;;Figure out whether to run some actions, by file local vars.
875 (autoloads-p
876 (ignore-errors
877 (with-current-buffer buf
878 (not no-update-autoloads))))
879 (compile-p
880 (and
881 (featurep 'byte-compile)
882 (string-match emacs-lisp-file-regexp filename)
883 (ignore-errors
884 (with-current-buffer buf
885 (not no-byte-compile)))
886 (let*
887 ((dest (byte-compile-dest-file full-path))
888 ;;$$PUNT - currently, we wouldn't have
889 ;;gotten here if we weren't intending to do
890 ;;everything so set force variables accordingly.
891 (recompile-p nil)
892 (compile-new t))
893 (if (file-exists-p dest)
894 ;; File was already compiled.
895 (or recompile-p (file-newer-than-file-p full-path dest))
896 (or compile-new
897 (y-or-n-p (concat "Compile " filename "? "))))))))
899 (prog1
900 (list
901 (if compile-p
902 `(byte-compile ,full-path)
903 nil)
904 (if autoloads-p
905 (elinstall-make-autoload-action
906 buf def-file load-path-element full-path)
907 nil))
908 (unless visited (kill-buffer-if-not-modified buf)))))))
910 ;;;_ . elinstall-find-actions-by-spec-x
912 (defun elinstall-find-actions-by-spec-x (spec dir)
913 "Return a list of actions to do, controlled by SPEC."
914 (declare (special
915 load-path-element def-file add-to-load-path-p))
917 (if (consp spec)
918 ;;$$IMPROVE ME by adding the other cases in the design.
919 (case (car spec)
921 (let
922 ((new-dir
923 (expand-file-name
924 (second spec)
925 dir)))
927 (elinstall-find-actions-by-spec-x
928 (third spec)
929 new-dir)))
931 (all
932 (apply #'nconc
933 (mapcar
934 #'(lambda (sub-spec)
935 (elinstall-find-actions-by-spec-x
936 sub-spec
937 dir))
938 (cdr spec))))
940 (file
941 (elinstall-actions-for-source-file
942 filename dir))
943 ;;$$ADD ME control, rather than trying to bind all control
944 ;;variables so we can safely bind one, will use set and
945 ;;unwind-protect.
947 (dir
948 (let*
949 ((dirname
950 (expand-file-name
951 (second spec)
952 dir))
953 ;;Relative filenames
954 (elisp-source-files
955 (directory-files
956 dirname
957 nil
958 elinstall-elisp-regexp))
959 (load-path-here
960 (and
961 elisp-source-files ;;List not empty.
962 add-to-load-path-p))
963 (load-path-element
964 (if load-path-here
965 dirname
966 load-path-element)))
968 (append
969 ;;$$IMPROVE ME - remove the current deffile from
970 ;;this list.
971 ;;Maybe arrange to add this directory to load-path.
972 (if load-path-here
973 `((add-to-load-path
974 ,def-file
975 ,load-path-element))
976 '())
978 ;;$$IMPROVE ME - be controlled by a control variable.
979 ;;If any info files are present, do add-to-info-path
980 ;;too.
982 (elinstall-dir-has-info dirname)
983 `((add-to-info-path
984 ,def-file
985 "."))
986 '())
990 ;;$$FIXME Don't do directories, but maybe recurse on
991 ;;them, if a flag is set.
992 ;;Maybe could follow/not symlinks similarly.
993 (apply #'nconc
994 (mapcar
995 #'(lambda (filename)
996 (elinstall-actions-for-source-file
997 filename
998 dirname))
999 elisp-source-files)))))
1001 (load-path
1002 (append
1003 `((add-to-load-path ,def-file ,dir))
1004 (let
1005 ((load-path-element dir))
1006 (elinstall-find-actions-by-spec-x spec dir))))
1009 (def-file
1010 (let
1011 ((def-file
1012 (expand-file-name
1013 (second spec)
1014 dir))
1015 (for-preload (third spec)))
1016 (assert (listp for-preload))
1017 (append
1018 (list
1020 (and for-preload (car for-preload))
1021 `(preload-file
1022 ,(car for-preload)
1023 ,def-file
1024 ,@(cdr for-preload))
1025 '()))
1027 (elinstall-find-actions-by-spec-x
1028 (fourth spec) dir)))))
1030 ;;$$IMPROVE ME by adding the other cases in the design.
1031 (case spec
1032 (t))))
1033 ;;;_ . elinstall-find-actions-by-spec
1034 (defun elinstall-find-actions-by-spec (spec load-path-element dir def-file)
1037 (let
1038 ((load-path-element load-path-element)
1039 (def-file def-file)
1040 (add-to-load-path-p t))
1041 (declare (special
1042 load-path-element def-file add-to-load-path-p))
1044 (elinstall-find-actions-by-spec-x
1045 spec dir)))
1047 ;;;_ . high-level work
1048 ;;;_ , elinstall-get-relevant-load-path
1049 (defun elinstall-get-relevant-load-path (actions)
1051 (delq nil
1052 (mapcar
1053 #'(lambda (act)
1054 (case (car act)
1055 (add-to-load-path
1056 (second act))
1057 (t nil)))
1058 actions)))
1059 ;;;_ , elinstall-get-deffile-list
1060 (defun elinstall-get-deffile-list (stages)
1061 "Get a list of deffile names"
1063 (mapcar
1064 #'car
1065 (elinstall-stages->build-deffiles stages)))
1067 ;;;_ , elinstall-x
1068 (defun elinstall-x (dir spec &optional force)
1070 (let*
1072 ;;This is just the default deffile, spec can override it.
1073 (def-file
1074 (elinstall-expand-deffile-name nil))
1075 (actions
1076 (elinstall-find-actions-by-spec
1077 spec
1080 def-file))
1081 (stages (elinstall-segregate-actions actions))
1082 (use-load-path
1083 (elinstall-get-relevant-load-path
1084 actions)))
1086 (elinstall-stage-update-deffiles
1087 (elinstall-stages->build-deffiles stages)
1088 force
1089 use-load-path)
1090 (elinstall-stage-arrange-preloads
1091 (elinstall-stages->arrange-preloads stages)
1092 (elinstall-get-deffile-list stages)
1093 force)
1094 (elinstall-stage-byte-compile
1095 (elinstall-stages->byte-compile stages))
1097 ;;;_ , Entry points
1098 ;;;_ . elinstall
1099 ;;;###autoload
1100 (defun elinstall (project-name path spec &optional force)
1101 "Install elisp files.
1102 They need not be a formal package.
1104 Parameters:
1106 PROJECT-NAME - the name of the project
1108 PATH - Path to the project.
1109 Suggestion: (elinstall-directory-true-name)
1111 SPEC - a spec for the autoloads etc to make. It can be as simple as
1112 \(dir \"\.\") for installing one directory.
1114 If FORCE is t, install a package even if it has already been
1115 installed. Other non-nil cases of FORCE are reserved for future
1116 development."
1118 (when
1119 (and
1120 (or
1121 force
1122 (not (elinstall-already-installed project-name)))
1123 (yes-or-no-p (format "Re-install %s? " project-name)))
1124 (elinstall-x
1125 path
1126 `(def-file "loaddefs.el" (if-used ,project-name) ,spec)
1127 force)
1128 (elinstall-record-installed project-name)))
1132 ;;;_ . elinstall-update-directory-autoloads
1133 ;;$$SPLIT ME - one to just classically update autoloads, one to
1134 ;;install a file.
1135 ;;$$TEST ME
1136 ;;;###autoload
1137 (defun elinstall-update-directory-autoloads (dir)
1140 (interactive "DInstall all elisp files from directory: ")
1143 (let
1144 ((def-file-name
1145 (elinstall-expand-deffile-name
1146 generated-autoload-file)))
1148 (elinstall-x
1150 `(def-file ,def-file-name (nil) (dir ".")))))
1154 ;;;_ . elinstall-update-file-autoloads
1155 ;;$$SPLIT ME - one to just classically update autoloads, one to
1156 ;;install a file.
1157 ;;$$TEST ME
1158 ;;;###autoload
1159 (defun elinstall-update-file-autoloads (file)
1162 (interactive "fInstall elisp file: ")
1163 (let
1164 ((def-file-name
1165 ;;This is the default. File local vars can override it.
1166 (elinstall-expand-deffile-name
1167 generated-autoload-file)))
1168 (elinstall-x
1169 file
1170 `(def-file ,def-file-name (nil) (file ,file)))))
1171 ;;;_. Footers
1172 ;;;_ , Provides
1174 (provide 'elinstall)
1176 ;;;_ * Local emacs vars.
1177 ;;;_ + Local variables:
1178 ;;;_ + mode: allout
1179 ;;;_ + End:
1181 ;;;_ , End
1182 ;;; elinstall.el ends here