1 ;;;_ elinstall.el --- Automatically and flexibly install elisp files
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)
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.
28 ;; elinstall Use this for overall loading
30 ;; elinstall-link-on-emacs-start - Use this for non-autogenerated
31 ;; files that need to be linked in.
33 ;; elinstall-update-directory-autoloads
34 ;; elinstall-update-file-autoloads
40 (require 'cus-edit
) ;;Because we save "installedness" manually
47 "Customizations for elinstall"
50 (defcustom elinstall-default-priority
52 "Default priority for site-start"
56 (defcustom elinstall-default-preload-target
57 "~/.emacs.d/site-start.d/"
58 "Default preload-target for registering autoloads"
62 (const "~/.emacs.d/site-start.d/")
63 (const "/etc/emacs/site-start.d/")
68 (defcustom elinstall-already-installed
70 "Things that have already been installed.
71 This exists for recording what has been installed. User interaction is not
72 contemplated at this time." )
75 ;;;_ . Regular expressions
76 ;;;_ , elinstall-elisp-regexp
77 (defconst elinstall-elisp-regexp
80 (suf (get-load-suffixes))
81 (unless (string-match "\\.elc" suf
) (push suf tmp
)))
82 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
83 "Regular expression that matches elisp files" )
86 ;;;_ . elinstall-directory-true-name
87 (defun elinstall-directory-true-name ()
88 "Get the true name of the directory the calling code lives in.
89 CAUTION: This is sensitive to where it's called. That's the point of it."
92 (file-truename load-file-name
)
93 (file-truename buffer-file-name
))))
94 ;;;_ . Checking installedness
95 ;;;_ , elinstall-already-installed
96 (defun elinstall-already-installed (project-name)
97 "Return non-nil if PROJECT-NAME has been installed."
98 (member project-name elinstall-already-installed
))
100 ;;;_ , elinstall-record-installed
101 (defun elinstall-record-installed (project-name)
102 "Record that PROJECT-NAME has been installed."
104 (add-to-list 'elinstall-already-installed project-name
)
105 (customize-save-variable
106 'elinstall-already-installed
107 elinstall-already-installed
108 "Set by elinstall-record-installed"))
112 ;;;_ , Doing autoload actions (All adapted from autoload.el)
113 ;;;_ . Utilities about the action list
114 ;;;_ , elinstall-remove-autogen-action
115 (defun elinstall-remove-autogen-action (file actions
)
116 "Return ACTIONS minus any add-file-autoloads on FILE removed."
123 (if (equal file
(third act
))
128 ;;;_ , elinstall-get-autogen-action
129 (defun elinstall-get-autogen-action (file actions
)
133 (dolist (act actions
)
136 (when (equal file
(third act
))
137 (setq the-act act
)))))
140 ;;;_ . Making autoloads
141 ;;;_ , elinstall-generate-file-autoloads
142 ;;override to allow slashed load-paths
143 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
145 ;;`relative-to' can be:
146 ;; * nil: act as at present. Assume that FILE's immediate directory
148 ;; * t :: use default-directory
149 ;; * a string :: relative to it, as a filename
151 (defun elinstall-generate-file-autoloads (relative-name full-name
)
152 "Insert at point a loaddefs autoload section for FILE.
153 Autoloads are generated for defuns and defmacros in FILE
154 marked by `generate-autoload-cookie' (which see).
155 If FILE is being visited in a buffer, the contents of the buffer
157 Return non-nil in the case where no autoloads were added at point.
159 FULL-NAME is the absolute name of the file.
160 RELATIVE-NAME is its name respective to some component of load-path."
161 (let* ((outbuf (current-buffer))
164 (print-readably t
) ; This does something in Lucid Emacs.
165 (float-output-format nil
)
167 (visited (get-file-buffer full-name
))
170 ;; It is faster to avoid visiting the file.
171 (ignore-errors (autoload-find-file full-name
))))
174 (with-current-buffer source-buf
175 ;;$$MOVE ME - this should be checked in action-finding.
176 ;; Obey the no-update-autoloads file local variable.
177 (unless no-update-autoloads
178 (message "Generating autoloads for %s..." relative-name
)
179 (setq output-start
(with-current-buffer outbuf
(point)))
183 (goto-char (point-min))
185 (skip-chars-forward " \t\n\f")
187 ((looking-at (regexp-quote generate-autoload-cookie
))
188 (search-forward generate-autoload-cookie
)
189 (skip-chars-forward " \t")
192 ;; Read the next form and make an autoload.
193 (let* ((form (prog1 (read (current-buffer))
194 (or (bolp) (forward-line 1))))
195 (autoload (make-autoload form load-name
)))
197 (push (nth 1 form
) autoloads-done
)
198 (setq autoload form
))
199 (let ((autoload-print-form-outbuf outbuf
))
200 (autoload-print-form autoload
)))
202 ;; Copy the rest of the line to the output.
203 (princ (buffer-substring
205 ;; Back up over whitespace, to preserve it.
206 (skip-chars-backward " \f\t")
207 (if (= (char-after (1+ (point))) ?
)
211 (progn (forward-line 1) (point)))
214 ;; Don't read the comment.
218 (forward-line 1))))))
221 (with-current-buffer outbuf
223 ;; Insert the section-header line which lists the file name
224 ;; and which functions are in it, etc.
225 (goto-char output-start
)
226 (autoload-insert-section-header
227 outbuf autoloads-done relative-name full-name
228 (nth 5 (file-attributes full-name
)))
229 (insert ";;; Generated autoloads from "
230 (autoload-trim-file-name full-name
) "\n"))
231 (insert generate-autoload-section-trailer
)))
232 (message "Generating autoloads for %s...done" relative-name
))
235 ;; We created this buffer, so we should kill it.
236 (kill-buffer (current-buffer))))
237 (message "Could not load %s" relative-name
))
242 ;;;_ , elinstall-deffile-insert-autoloads
243 (defun elinstall-deffile-insert-autoloads (file load-name
)
244 "Update the autoloads for FILE in current buffer.
245 Return FILE if there was no autoload cookie in it, else nil.
247 Current buffer must be a loaddef-style file.
249 LOAD-NAME is the absolute name of the file.
250 RELATIVE-NAME is its name respective to some component of load-path."
258 (goto-char (point-min))
259 ;; Look for the section for FILE
260 (while (and (not found
)
261 (search-forward generate-autoload-section-header nil t
))
262 (let ((form (autoload-read-section-header)))
264 ((equal (nth 2 form
) file
)
265 ;; We found the section for this file.
266 (let ((begin (match-beginning 0)))
268 (search-forward generate-autoload-section-trailer
)
269 (delete-region begin
(point))
271 ((string< file
(nth 2 form
))
272 ;; We've come to a section alphabetically later than
273 ;; FILE. We assume the file is in order and so
274 ;; there must be no section for FILE. We will
275 ;; insert one before the section here.
276 (goto-char (match-beginning 0))
277 (setq found
'new
)))))
281 ;; No later sections in the file. Put before the last page.
282 (goto-char (point-max))
283 (search-backward "\f" nil t
)))
285 (elinstall-generate-file-autoloads file load-name
))))
287 (if no-autoloads file nil
)))
288 ;;;_ . Arranging to add to info-path and load-path
289 ;;;_ , elinstall-generate-add-to-path
290 (defun elinstall-generate-add-to-path (path-element type
)
291 "Insert code at point to add PATH-ELEMENT to a path.
293 * `add-to-load-path', add to load-path
294 * `add-to-info-path', add to Info-default-directory-list
296 Current buffer must be a loaddef-style file."
299 (add-to-load-path 'load-path
)
300 (add-to-info-path 'Info-default-directory-list
)))
303 (add-to-load-path "load-path")
304 (add-to-info-path "info-path")))
307 (print-readably t
) ; This does something in Lucid Emacs.
308 (float-output-format nil
))
310 (message "Generating %s additions..." description
)
312 (autoload-insert-section-header
313 (current-buffer) (list path-element
) nil nil
315 (insert ";;; Generated path addition\n")
317 `(add-to-list ',path-symbol
319 ,(file-relative-name path-element
)
322 (file-truename load-file-name
)))))
325 (insert generate-autoload-section-trailer
)
326 (message "Generating %s additions...done" description
)))
329 ;;;_ , elinstall-deffile-insert-add-to-path
330 (defun elinstall-deffile-insert-add-to-path (path-element type
)
331 "Insert code in current buffer to add PATH-ELEMENT to a path.
333 * `add-to-load-path', add to load-path
334 * `add-to-info-path', add to Info-default-directory-list
336 Current buffer must be a loaddef-style file."
344 (goto-char (point-min))
345 ;; Look for the section for PATH-ELEMENT
346 (while (and (not found
)
347 (search-forward generate-autoload-section-header nil t
))
348 (let ((form (autoload-read-section-header)))
351 (equal (nth 0 form
) type
)
352 (member (nth 1 form
) path-element
))
354 ;; We found the section for this add.
355 (let ((begin (match-beginning 0)))
357 (search-forward generate-autoload-section-trailer
)
358 (delete-region begin
(point))
364 ;; No later sections in the file. Put before the last page.
365 (goto-char (point-max))
366 (search-backward "\f" nil t
)))
368 (elinstall-generate-add-to-path path-element type
)))
370 ;;This never belongs in the no-autoloads section.
373 ;;;_ . elinstall-deffile-insert
375 (defun elinstall-deffile-insert (action)
376 "Insert autoloads etc into current file according to ACTION.
377 The format of ACTION is described in the design docs.
379 Return filename if this action belongs in the no-autoload section."
384 (elinstall-deffile-insert-autoloads
389 (elinstall-deffile-insert-add-to-path
395 (elinstall-deffile-insert-add-to-path
401 (error "This case should not come here.")))))
403 ;;;_ . elinstall-prepare-deffile
404 (defun elinstall-prepare-deffile (deffile)
405 "Try to ensure that DEFFILE is available for receiving autoloads"
407 (autoload-ensure-default-file deffile
)
408 (with-current-buffer (find-file-noselect deffile
)
411 ;; We must read/write the file without any code conversion,
412 ;; but still decode EOLs.
413 (let ((coding-system-for-read 'raw-text
))
415 ;; This is to make generated-autoload-file have Unix EOLs, so
416 ;; that it is portable to all platforms.
417 (setq buffer-file-coding-system
'raw-text-unix
))
418 (or (> (buffer-size) 0)
419 (error "Autoloads file %s does not exist" buffer-file-name
))
420 (or (file-writable-p buffer-file-name
)
421 (error "Autoloads file %s is not writable"
424 ;;;_ . elinstall-update-deffile
426 ;;Adapted from autoload.el `update-directory-autoloads'.
427 ;;Still being adapted:
429 ;; * Still need to treat add-to-info-path and
430 ;;add-to-load-path. Both recognize them and insert them.
431 ;; * Adapt `elinstall-update-file-autoloads' to understand actions.
433 ;; * Finding "file" among actions is rickety. Maybe knowing the
434 ;; respective load-path element would help.
436 (defun elinstall-update-deffile (target actions
&optional
439 Update file TARGET with current autoloads as specified by ACTIONS.
440 Also remove any old definitions pointing to libraries that can no
443 ACTIONS must be a list of actions (See the format doc). Each one's
444 filename must be relative to some element of load-path.
446 USE-LOAD-PATH is a list to use as load-path. It should include
447 any new load-path that we are arranging to create. If it's not given,
448 load-path itself is used.
450 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
451 Other non-nil cases of FORCE are reserved for future development.
453 This uses `update-file-autoloads' (which see) to do its work.
454 In an interactive call, you must give one argument, the name
455 of a single directory."
458 (use-load-path (or use-load-path load-path
))
459 (this-time (current-time))
460 ;;files with no autoload cookies.
463 (elinstall-prepare-deffile target
)
465 (find-file-noselect target
)
468 (elinstall-remove-autogen-action
469 (autoload-trim-file-name target
)
472 (goto-char (point-min))
473 (while (search-forward generate-autoload-section-header nil t
)
474 (let* ((form (autoload-read-section-header))
476 (cond ((and (consp file
) (stringp (car file
)))
477 ;; This is a list of files that have no
479 ;; There shouldn't be more than one such entry.
480 ;; Remove the obsolete section.
481 (autoload-remove-section (match-beginning 0))
482 (let ((last-time (nth 4 form
)))
484 (let ((file-time (nth 5 (file-attributes file
))))
486 (not (time-less-p last-time file-time
)))
488 (push file no-autoloads
)
490 (elinstall-remove-autogen-action
492 ((not (stringp file
)))
496 (locate-library file nil use-load-path
)))
498 ;;File doesn't exist, so remove its
501 (autoload-remove-section
502 (match-beginning 0)))
504 ;; File hasn't changed, so do nothing.
507 (nth 5 (file-attributes file-path
)))
510 (elinstall-deffile-insert
511 (elinstall-get-autogen-action
515 (elinstall-remove-autogen-action
518 ;; Remaining actions have no existing autoload sections yet.
521 (delq nil
(mapcar #'elinstall-deffile-insert actions
))))
523 ;; Sort them for better readability.
524 (setq no-autoloads
(sort no-autoloads
'string
<))
525 ;; Add the `no-autoloads' section.
526 (goto-char (point-max))
527 (search-backward "\f" nil t
)
528 (autoload-insert-section-header
529 (current-buffer) nil nil no-autoloads this-time
)
530 (insert generate-autoload-section-trailer
))
534 ;;;_ , Doing actions to arrange preloads
535 ;;;_ . elinstall-insert-add-to-path
536 (defun elinstall-insert-add-to-path (new path-sym
)
537 "Insert code to add NEW to PATH-SYM.
538 Insert this at point in current buffer."
541 `(add-to-list ',path-sym
542 (expand-file-name ,new
545 (file-truename load-file-name
)))))
548 ;;;_ . elinstall-insert-add-to-load-path
549 (defun elinstall-insert-add-to-load-path (new)
550 "Insert code to add NEW to load-path.
551 Insert this at point in current buffer."
552 (elinstall-insert-add-to-path new
'load-path
))
554 ;;;_ . elinstall-insert-add-to-info-path
555 (defun elinstall-insert-add-to-info-path (new)
556 "Insert code to add NEW to info-path.
557 Insert this at point in current buffer."
558 (elinstall-insert-add-to-path new
'Info-default-directory-list
))
560 ;;;_ . elinstall-symlink-on-emacs-start
561 (defun elinstall-symlink-on-emacs-start
562 (filename target-basename target-dir
&optional priority force
)
563 "Symlink to TARGET-BASENAME.el in TARGET-DIR
565 If PRIORITY is given, it will be used as the priority prefix,
566 otherwise elinstall-default-priority will be.
567 PRIORITY must be an integer or nil.
568 If FORCE is `t', do it regardless of timestamps etc.
569 Other non-nil cases of FORCE are reserved for future development."
572 (priority (or priority elinstall-default-priority
))
579 (expand-file-name target-name-nodir target-dir
)))
583 ;;Path should already exist.
585 (file-exists-p target-dir
))
586 (message "The target directory doesn't exist."))
587 ;;Target shouldn't already exist, but if force is given, let
589 ;;$$IMPROVE ME If it is a symlink pointing to the same place,
592 (file-exists-p target
)
597 (format "Really overwrite %s? " project-name
))))
598 (message "No symlink made to %s" target
)))
606 ;;;_ . elinstall-add-to-dot-emacs
607 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force
&rest r
)
608 "Add code to load FILENAME to .emacs.
609 FILENAME should not have an extension"
612 (with-current-buffer (find-file-noselect dot-emacs-name
)
615 (goto-char (point-max))
616 (insert "\n;;Added by elinstall")
617 (insert "\n;;Consider using my-site-start to manage .emacs\n")
618 (pp `(load ,filename
) (current-buffer))
622 ;;;_ . elinstall-link-on-emacs-start
624 (defun elinstall-link-on-emacs-start (filename force basename priority
)
627 ;;Figure out parameters, using defaults when needed.
629 ( (preload-target elinstall-default-preload-target
))
631 ;;Dispatch the possibilities.
633 ((eq preload-target
'dot-emacs
)
634 (elinstall-add-to-dot-emacs "~/.emacs" filename
))
635 ((stringp preload-target
)
636 (elinstall-symlink-on-emacs-start
637 filename basename preload-target priority force
))
640 (message "I don't recognize that")))))
642 ;;;_ , Cleanup actions
643 ;;Nothing yet. This will be another type of action.
645 ;;;_ . Segregating actions
646 ;;;_ , elinstall-segregate-actions
647 (defun elinstall-segregate-actions (actions)
648 "Return actions segregated by deffile.
650 Returns a list whose elements are each a cons of:
651 * deffile filename or nil
652 * A list of actions to be done for that deffile."
656 (last-segment (list nil
)))
658 (dolist (act actions
)
671 (assoc deffile-name segment-list
)
674 (setcdr cell
(cons act
(cdr cell
)))
684 (if (cdr last-segment
)
691 ;;;_ . Finding actions
692 ;;;_ , Treating the parameter list
693 ;;;_ . elinstall-add-parameter
694 (defun elinstall-add-parameter (alist key new-value
)
695 "Add a new value for KEY to ALIST"
699 (assq-delete-all key
(copy-list alist
))))
701 ;;;_ . elinstall-get-parameter
702 (defun elinstall-get-parameter (alist key
)
703 "Get the value of KEY from ALIST"
705 (cdr (assq key alist
)))
706 ;;;_ . elinstall-expand-file-name
709 (defun elinstall-expand-file-name (filename alist
)
710 "Expand FILENAME by the value of `path' in ALIST"
713 (elinstall-get-parameter alist
'path
)))
714 ;;;_ , Finding deffiles
715 ;;;_ . elinstall-expand-deffile-name
716 (defun elinstall-expand-deffile-name (deffile)
717 "Expand DEFFILE autoload.el's way."
719 (expand-file-name (or deffile
"loaddefs.el")
720 (expand-file-name "lisp"
723 ;;;_ . elinstall-maybe-get-deffile
724 (defun elinstall-maybe-get-deffile (file)
725 "If FILE defined `generated-autoload-file', return it.
726 Otherwise return nil.
727 Return it as an absolute filename."
730 ;;$$FIXME load buffer if it's not already loaded
732 ((existing-buffer (get-file-buffer file
)))
734 ;; We want to get a value for generated-autoload-file from
735 ;; the local variables section if it's there.
736 ;;But if it's not loaded, we don't? Maybe should use
737 ;; `autoload-find-file' and load it.
739 (set-buffer existing-buffer
))
740 (if (local-variable-p 'generated-autoload-file
)
741 (elinstall-expand-deffile-name
742 generated-autoload-file
)
747 ;;;_ , elinstall-find-actions-by-spec
749 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters
)
750 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
752 LOAD-PATH-ELEMENT is the conceptual element of load-path that
753 surrounds PATH. It may not yet have been added to load-path."
755 ;;$$IMPROVE ME by adding the other cases in the design.
764 (elinstall-find-actions-by-spec
774 (elinstall-find-actions-by-spec
792 (elinstall-get-parameter
793 parameters
'block-add-to-load-path
))))
796 ;;Do this only if there are loadable files.
799 ,(elinstall-get-parameter
800 parameters
'def-file
)
804 ;;Do add-to-info-path too. But test if there are
805 ;;any info files present.
808 ;; We want to get a value for generated-autoload-file
809 ;; from the local variables section if it's there.
810 ;;Use `elinstall-maybe-get-deffile'
811 ;; Otherwise we'll use `def-file' in parameters.
813 ;;$$FIXME This isn't quite right. If directory
814 ;;itself is not in load-path, this will be wrong.
815 ;;Gotta know where our encompassing part of
818 ;;$$ENCAP ME This should be shared with the
819 ;;treatment of (file FN)
821 ;;$$FIXME Don't do directories, but maybe recurse on
822 ;;them, if a flag is set. And since we definitely
823 ;;have a load-path element here,
824 ;;'block-add-to-load-path according to a parameter.
825 ;;Maybe could follow/not symlinks similarly.
830 (expand-file-name filename dirname
)))
832 ,(elinstall-get-parameter
833 parameters
'def-file
)
834 ,(file-name-sans-extension
838 ,load-path-element
;;Is this still used?
843 nil
;;Relative filenames
844 elinstall-elisp-regexp
)))))
852 (elinstall-find-actions-by-spec
856 (elinstall-add-parameter parameters
857 'def-file new-def-file
)))))
859 ;;$$IMPROVE ME by adding the other cases in the design.
862 ;;;_ . high-level work
863 ;;;_ , elinstall-get-relevant-load-path
864 (defun elinstall-get-relevant-load-path (actions)
875 ;;;_ , elinstall-do-segment
876 (defun elinstall-do-segment (segment force use-load-path
)
877 "Do all the actions in SEGMENT.
878 FORCE has its usual meaning.
879 USE-LOAD-PATH is the effective load-path."
881 ;;$$IMPROVE ME - this will have to know the additions to load-path.
883 ((deffile (car segment
)))
884 (if (stringp deffile
)
885 (elinstall-update-deffile deffile
(cdr segment
) force
887 ;;Do other actions: link-in actions and cleanups.
894 #'elinstall-link-on-emacs-start
898 (kill-buffer-if-not-modified (second act
)))
899 ;;$$IMPROVE ME Undertsnad flags to control second
900 ;;argument (whether to load file after
903 (byte-compile-file (second act
)))))
910 (defun elinstall-x (dir spec
&optional preload-target force
)
914 (elinstall-find-actions-by-spec
919 ;;$$RETHINK ME - maybe hand this work off to autoload?
920 ;;This is just the default loaddefs file, spec actually
922 (def-file .
,(elinstall-expand-deffile-name nil
) ))))
923 (segment-list (elinstall-segregate-actions actions
))
925 (elinstall-get-relevant-load-path
930 (elinstall-do-segment segment force use-load-path
))
935 (defun elinstall (project-name dir spec
&optional preload-target force
)
936 "Install elisp files.
938 They need not be presented as a package.
941 PROJECT-NAME - the name of the project
942 DIR - the root directory of the project.
943 Suggestion: (elinstall-directory-true-name)
945 SPEC - a spec for the autoloads etc to make. It can be as simple as
947 Suggestion: `(in ,(elinstall-directory-true-name) t)
948 PRELOAD-TARGET is where the autoload files are to be symlinked in. If
949 `nil' `elinstall-default-preload-target' is used instead.
951 If FORCE is t, install a package even if it has already been
953 Other non-nil cases of FORCE are reserved for future development."
959 (not (elinstall-already-installed project-name
)))
960 ;;$$RE-ADD ME - I commented it out just for development.
961 '(yes-or-no-p (format "Install %s " project-name
)))
962 (elinstall-x dir spec preload-target force
)
964 ;;$$RE-ADD ME - I commented it out just for development.
965 '(elinstall-record-installed project-name
)))
969 ;;;_ , elinstall-update-directory-autoloads
971 (defun elinstall-update-directory-autoloads (dir)
974 (interactive "DInstall all elisp files from directory: ")
979 (elinstall-expand-deffile-name
980 generated-autoload-file
)))
983 ;;;_ , elinstall-update-file-autoloads
985 (defun elinstall-update-file-autoloads (file)
988 (interactive "fInstall elisp file: ")
993 (elinstall-maybe-get-deffile file
)
994 (elinstall-expand-deffile-name
995 generated-autoload-file
))))
1005 (provide 'elinstall
)
1007 ;;;_ * Local emacs vars.
1008 ;;;_ + Local variables:
1013 ;;; elinstall.el ends here