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.
37 (require 'cus-edit
) ;;Because we save "installedness" manually
44 "Customizations for elinstall"
47 (defcustom elinstall-default-priority
49 "Default priority for site-start"
53 (defcustom elinstall-default-target-dir
54 "~/.emacs.d/site-start.d/"
55 "Default target for registering autoloads"
59 (const "~/.emacs.d/site-start.d/")
60 (const "/etc/emacs/site-start.d/")
65 (defcustom elinstall-already-installed
67 "Things that have already been installed.
68 This exists for recording what has been installed. User interaction is not
69 contemplated at this time." )
72 ;;;_ . elinstall-directory-true-name
73 (defun elinstall-directory-true-name ()
74 "Get the true name of the directory the calling code lives in.
75 CAUTION: This is sensitive to where it's called. That's the point of it."
78 (file-truename load-file-name
)
79 (file-truename buffer-file-name
))))
80 ;;;_ . Checking installedness
81 ;;;_ , elinstall-already-installed
82 (defun elinstall-already-installed (project-name)
83 "Return non-nil if PROJECT-NAME has been installed."
84 (member project-name elinstall-already-installed
))
86 ;;;_ , elinstall-record-installed
87 (defun elinstall-record-installed (project-name)
88 "Record that PROJECT-NAME has been installed."
90 (add-to-list 'elinstall-already-installed project-name
)
91 (customize-save-variable
92 'elinstall-already-installed
93 elinstall-already-installed
94 "Set by elinstall-record-installed"))
96 ;;;_ , Regular expressions
97 ;;;_ . elinstall-elisp-regexp
98 (defconst elinstall-elisp-regexp
101 (suf (get-load-suffixes))
102 (unless (string-match "\\.elc" suf
) (push suf tmp
)))
103 (concat "^[^=.].*" (regexp-opt tmp t
) "\\'"))
104 "Regular expression that matches elisp files" )
108 ;;;_ , elinstall-insert-add-to-path
109 (defun elinstall-insert-add-to-path (new path-sym
)
110 "Insert code to add NEW to PATH-SYM.
111 Insert this at point in current buffer."
114 `(add-to-list ',path-sym
115 (expand-file-name ,new
118 (file-truename load-file-name
)))))
121 ;;;_ , elinstall-insert-add-to-load-path
122 (defun elinstall-insert-add-to-load-path (new)
123 "Insert code to add NEW to load-path.
124 Insert this at point in current buffer."
125 (elinstall-insert-add-to-path new
'load-path
))
127 ;;;_ , elinstall-insert-add-to-info-path
128 (defun elinstall-insert-add-to-info-path (new)
129 "Insert code to add NEW to info-path.
130 Insert this at point in current buffer."
131 (elinstall-insert-add-to-path new
'Info-default-directory-list
))
133 ;;;_ , Linking on start
134 ;;;_ . elinstall-insert-dir-autoloads
135 (defun elinstall-symlink-on-emacs-start
136 (filename target-basename target-dir
&optional priority force
)
137 "Symlink to TARGET-BASENAME.el in TARGET-DIR
139 If PRIORITY is given, it will be used as the priority prefix,
140 otherwise elinstall-default-priority will be.
141 PRIORITY must be an integer or nil.
142 If FORCE is `t', do it regardless of timestamps etc.
143 Other non-nil cases of FORCE are reserved for future development."
146 (priority (or priority elinstall-default-priority
))
153 (expand-file-name target-name-nodir target-dir
)))
157 ;;Path should already exist.
159 (file-exists-p target-dir
))
160 (message "The target directory doesn't exist."))
161 ;;Target shouldn't already exist, but if force is given, let
164 (file-exists-p target
)
169 (format "Really overwrite %s? " project-name
))))
170 (message "No symlink made to %s" target
)))
178 ;;;_ . elinstall-add-to-dot-emacs
179 (defun elinstall-add-to-dot-emacs (dot-emacs-name filename force
&rest r
)
180 "Add code to load FILENAME to .emacs.
181 FILENAME should not have an extension"
184 (with-current-buffer (find-file-noselect dot-emacs-name
)
187 (goto-char (point-max))
188 (insert "\n;;Added by elinstall")
189 (insert "\n;;Consider using my-site-start to manage .emacs\n")
190 (pp `(load ,filename
) (current-buffer))
194 ;;;_ . elinstall-link-on-emacs-start
196 (defun elinstall-link-on-emacs-start (filename force basename priority
)
199 ;;Figure out parameters, using defaults when needed.
201 ( (target-dir elinstall-default-target-dir
))
203 ;;Dispatch the possibilities.
205 ((eq target-dir
'dot-emacs
)
206 (elinstall-add-to-dot-emacs "~/.emacs" filename
))
207 ((stringp target-dir
)
208 (elinstall-symlink-on-emacs-start
209 filename basename target-dir priority force
))
212 (message "I don't recognize that")))))
214 ;;;_ , elinstall-insert-dir-autoloads
215 (defun elinstall-insert-dir-autoloads (autoloads-file directory
)
216 "Generate autoloads for DIRECTORY into AUTOLOADS-FILE."
217 (declare (special generated-autoload-file
))
218 (let ((generated-autoload-file autoloads-file
))
219 (elinstall-update-directory-autoloads directory
)))
221 ;;;_ , elinstall-segregate-actions
222 (defun elinstall-segregate-actions (actions)
223 "Return actions segregated by deffile.
225 Returns a list whose elements are each a cons of:
226 * deffile filename or nil
227 * A list of actions to be done for that deffile."
231 (dolist (act actions
)
242 (cell (assoc deffile-name segment-list
)))
244 (setcdr cell
(cons act
(cdr cell
)))
254 ;;;_ , elinstall-do-segment
255 (defun elinstall-do-segment (segment force
)
259 ((deffile (car segment
)))
262 ;;Do other actions, which will be all link-in actions.
265 (apply #'elinstall-link-on-emacs-start force
(cdr action
))
271 ;;;_ . Overrides (All adapted from autoload.el)
272 ;;;_ , elinstall-generate-file-autoloads
273 ;;override to allow slashed load-paths
274 ;;Quick and dirty: We just adapt `generate-file-autoloads' and add
276 ;;`relative-to' can be:
277 ;; * nil: act as at present. Assume that FILE's immediate directory
279 ;; * t :: use default-directory
280 ;; * a string :: relative to it, as a filename
282 (defun elinstall-generate-file-autoloads (file load-name
)
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
288 Return non-nil in the case where no autoloads were added at point."
289 (interactive "fGenerate autoloads for file: ")
290 (let ((outbuf (current-buffer))
293 (print-readably t
) ; This does something in Lucid Emacs.
294 (float-output-format nil
)
296 (visited (get-file-buffer file
))
299 ;; If the autoload section we create here uses an absolute
300 ;; file name for FILE in its header, and then Emacs is installed
301 ;; under a different path on another system,
302 ;; `update-autoloads-here' won't be able to find the files to be
303 ;; autoloaded. So, if FILE is in the same directory or a
304 ;; subdirectory of the current buffer's directory, we'll make it
305 ;; relative to the current buffer's directory.
306 (setq file
(expand-file-name file
))
307 (let* ((source-truename (file-truename file
))
308 (dir-truename (file-name-as-directory
309 (file-truename default-directory
)))
310 (len (length dir-truename
)))
311 (if (and (< len
(length source-truename
))
312 (string= dir-truename
(substring source-truename
0 len
)))
313 (setq file
(substring source-truename len
))))
315 (with-current-buffer (or visited
316 ;; It is faster to avoid visiting the file.
317 (autoload-find-file file
))
318 ;; Obey the no-update-autoloads file local variable.
319 (unless no-update-autoloads
320 (message "Generating autoloads for %s..." file
)
321 (setq output-start
(with-current-buffer outbuf
(point)))
325 (goto-char (point-min))
327 (skip-chars-forward " \t\n\f")
329 ((looking-at (regexp-quote generate-autoload-cookie
))
330 (search-forward generate-autoload-cookie
)
331 (skip-chars-forward " \t")
334 ;; Read the next form and make an autoload.
335 (let* ((form (prog1 (read (current-buffer))
336 (or (bolp) (forward-line 1))))
337 (autoload (make-autoload form load-name
)))
339 (push (nth 1 form
) autoloads-done
)
340 (setq autoload form
))
341 (let ((autoload-print-form-outbuf outbuf
))
342 (autoload-print-form autoload
)))
344 ;; Copy the rest of the line to the output.
345 (princ (buffer-substring
347 ;; Back up over whitespace, to preserve it.
348 (skip-chars-backward " \f\t")
349 (if (= (char-after (1+ (point))) ?
)
353 (progn (forward-line 1) (point)))
356 ;; Don't read the comment.
360 (forward-line 1))))))
363 (with-current-buffer outbuf
365 ;; Insert the section-header line which lists the file name
366 ;; and which functions are in it, etc.
367 (goto-char output-start
)
368 (autoload-insert-section-header
369 outbuf autoloads-done load-name file
370 (nth 5 (file-attributes file
)))
371 (insert ";;; Generated autoloads from "
372 (autoload-trim-file-name file
) "\n"))
373 (insert generate-autoload-section-trailer
)))
374 (message "Generating autoloads for %s...done" file
))
376 ;; We created this buffer, so we should kill it.
377 (kill-buffer (current-buffer))))
380 ;;;_ , elinstall-old-prepare-deffile
381 ;;Old code from autoload.
382 (defun elinstall-old-prepare-deffile (deffile)
385 (autoload-ensure-default-file deffile
)
386 (with-current-buffer (find-file-noselect deffile
)
389 ;; We must read/write the file without any code conversion,
390 ;; but still decode EOLs.
391 (let ((coding-system-for-read 'raw-text
))
393 ;; This is to make generated-autoload-file have Unix EOLs, so
394 ;; that it is portable to all platforms.
395 (setq buffer-file-coding-system
'raw-text-unix
))
396 (or (> (buffer-size) 0)
397 (error "Autoloads file %s does not exist" buffer-file-name
))
398 (or (file-writable-p buffer-file-name
)
399 (error "Autoloads file %s is not writable"
402 ;;;_ , elinstall-old-get-deffile
403 ;;Old code from autoload.
405 (defun elinstall-old-get-deffile (file)
406 "Return the appropriate loaddefs file for FILE."
410 ((existing-buffer (get-file-buffer file
)))
412 ;; We want to get a value for generated-autoload-file from
413 ;; the local variables section if it's there.
414 ;;But if it's not loaded, we don't? Maybe should use
415 ;; `autoload-find-file' and load it.
417 (set-buffer existing-buffer
))
418 ;;No, we only want it if a local variable forces it to this
420 (expand-file-name generated-autoload-file
421 (expand-file-name "lisp"
422 source-directory
)))))
426 ;;;_ , elinstall-deffile-insert
427 ;;This now operates in the current buffer. We already did most of
428 ;;this checking on setup.
430 ;; * buffer optional save,
431 ;; * converting file to load-name
432 ;; * checking for timestamp
433 (defun elinstall-deffile-insert-x (file load-name
)
434 "Update the autoloads for FILE in `generated-autoload-file'
435 \(which FILE might bind in its local variables).
437 LOAD-NAME is the full name of the file
440 Return FILE if there was no autoload cookie in it, else nil."
448 (goto-char (point-min))
449 ;; Look for the section for FILE
450 (while (and (not found
)
451 (search-forward generate-autoload-section-header nil t
))
452 (let ((form (autoload-read-section-header)))
454 ((string= (nth 2 form
) file
)
455 ;; We found the section for this file.
456 ;; Check if it is up to date.
457 (let ((begin (match-beginning 0)))
459 (search-forward generate-autoload-section-trailer
)
460 (delete-region begin
(point))
462 ((string< file
(nth 2 form
))
463 ;; We've come to a section alphabetically later than
464 ;; FILE. We assume the file is in order and so
465 ;; there must be no section for FILE. We will
466 ;; insert one before the section here.
467 (goto-char (match-beginning 0))
468 (setq found
'new
)))))
472 ;; No later sections in the file. Put before the last page.
473 (goto-char (point-max))
474 (search-backward "\f" nil t
)))
476 (elinstall-generate-file-autoloads file load-name
))))
478 (if no-autoloads file nil
)))
480 ;;;_ , elinstall-deffile-insert
482 (defun elinstall-deffile-insert (action)
483 "Insert autoloads or similar into current file according to ACTION.
484 The format of ACTION is described in the design docs.
486 Return filename if this action belongs in the no-autoload section."
494 (elinstall-deffile-insert-x
501 ;;Go to the right position, then call:
502 '(elinstall-insert-add-to-load-path)
505 ;;Similar, but for info-path.
507 'elinstall-insert-add-to-info-path
511 (error "This case should not come here.")))))
514 ;;;_ , elinstall-remove-autogen-action
515 (defun elinstall-remove-autogen-action (file actions
)
516 "Return ACTIONS minus any add-file-autoloads on FILE removed."
523 (if (equal file
(third act
))
527 ;;;_ , elinstall-get-autogen-action
528 (defun elinstall-get-autogen-action (file actions
)
532 (dolist (act actions
)
535 (when (equal file
(third act
))
536 (setq the-act act
)))))
539 ;;;_ , elinstall-update-deffile
541 ;;Adapted from autoload.el `update-directory-autoloads'.
542 ;;Still being adapted:
544 ;; * Still need to treat add-to-info-path and
545 ;;add-to-load-path. Both recognize them and insert them.
546 ;; * Adapt `elinstall-update-file-autoloads' to understand actions.
548 ;; * Finding "file" among actions is rickety. Maybe knowing the
549 ;; respective load-path element would help.
551 (defun elinstall-update-deffile (target actions
&optional
554 Update file TARGET with current autoloads as specified by ACTIONS.
555 Also remove any old definitions pointing to libraries that can no
558 ACTIONS must be a list of actions (See the format doc). Each one's
559 filename must be relative to some element of load-path.
561 USE-LOAD-PATH is a list to use as load-path. It should include
562 any new load-path that we are arranging to create. If it's not given,
563 load-path itself is used.
565 If FORCE is `t', do it regardless of timestamps etc. (Not implemented)
566 Other non-nil cases of FORCE are reserved for future development.
568 This uses `update-file-autoloads' (which see) to do its work.
569 In an interactive call, you must give one argument, the name
570 of a single directory."
573 (use-load-path (or use-load-path load-path
))
574 (this-time (current-time))
575 ;;files with no autoload cookies.
580 (find-file-noselect target
)
583 (elinstall-remove-autogen-action
584 (autoload-trim-file-name target
)
587 (goto-char (point-min))
588 (while (search-forward generate-autoload-section-header nil t
)
589 (let* ((form (autoload-read-section-header))
591 (cond ((and (consp file
) (stringp (car file
)))
592 ;; This is a list of files that have no
594 ;; There shouldn't be more than one such entry.
595 ;; Remove the obsolete section.
596 (autoload-remove-section (match-beginning 0))
597 (let ((last-time (nth 4 form
)))
599 (let ((file-time (nth 5 (file-attributes file
))))
601 (not (time-less-p last-time file-time
)))
603 (push file no-autoloads
)
605 (elinstall-remove-autogen-action
607 ((not (stringp file
)))
611 (locate-library file nil use-load-path
)))
613 ;;File doesn't exist, so remove its
616 (autoload-remove-section
617 (match-beginning 0)))
619 ;; File hasn't changed, so do nothing.
622 (nth 5 (file-attributes file-path
)))
625 (elinstall-deffile-insert
626 (elinstall-get-autogen-action file
))))
629 (elinstall-remove-autogen-action
632 ;; Remaining actions have no existing autoload sections yet.
635 (delq nil
(mapcar #'elinstall-deffile-insert actions
))))
637 ;; Sort them for better readability.
638 (setq no-autoloads
(sort no-autoloads
'string
<))
639 ;; Add the `no-autoloads' section.
640 (goto-char (point-max))
641 (search-backward "\f" nil t
)
642 (autoload-insert-section-header
643 (current-buffer) nil nil no-autoloads this-time
)
644 (insert generate-autoload-section-trailer
))
647 ;;;_ . Finding actions
648 ;;;_ , Treating the parameter list
649 ;;;_ . elinstall-add-parameter
650 (defun elinstall-add-parameter (alist key new-value
)
651 "Add a new value for KEY to ALIST"
655 (assq-delete-all key
(copy-list alist
))))
657 ;;;_ . elinstall-get-parameter
658 (defun elinstall-get-parameter (alist key
)
659 "Get the value of KEY from ALIST"
661 (cdr (assq key alist
)))
662 ;;;_ . elinstall-expand-filename
663 (defun elinstall-expand-filename (filename alist
)
664 "Expand FILENAME by the value of `path' in ALIST"
668 (elinstall-get-parameter alist
'path
)))
670 ;;;_ , elinstall-find-actions-by-spec
672 (defun elinstall-find-actions-by-spec (spec load-path-element path parameters
)
673 "Return a list of actions to do, controlled by SPEC and PARAMETERS.
675 LOAD-PATH-ELEMENT is the conceptual element of load-path that
676 surrounds PATH. It may not yet have been added to load-path."
678 ;;$$IMPROVE ME by adding the other cases in the design.
687 (elinstall-find-actions-by-spec
697 (elinstall-find-actions-by-spec
712 (elinstall-get-parameter
713 parameters
'block-add-to-load-path
))))
717 ,(elinstall-get-parameter
718 parameters
'def-file
)
722 ;; We want to get a value for generated-autoload-file
723 ;; from the local variables section if it's there.
724 ;; Otherwise we'll use `def-file' in parameters.
726 ;;$$FIXME This isn't quite right. If directory
727 ;;itself is not in load-path, this will be wrong.
728 ;;Gotta know where our encompassing part of
733 ,(elinstall-get-parameter
734 parameters
'def-file
)
736 (expand-filename filename path
)
742 nil
;;Relative filenames
743 elinstall-elisp-regexp
)))))
748 (elinstall-expand-filename
751 (elinstall-find-actions-by-spec
755 (elinstall-add-parameter parameters
756 'def-file new-def-file
)))))
758 ;;$$IMPROVE ME by adding the other cases in the design.
765 (defun elinstall-x (dir spec
&optional target force
)
773 (defun elinstall (project-name dir spec
&optional target force
)
774 "Install elisp files.
776 They need not be presented as a package.
779 PROJECT-NAME - the name of the project
780 DIR - the root directory of the project.
781 Suggestion: (elinstall-directory-true-name)
783 SPEC - a spec for the autoloads etc to make. It can be as simple as
785 Suggestion: `(in ,(elinstall-directory-true-name) t)
786 TARGET is where the autoload files are to be symlinked in. If
787 `nil' `elinstall-default-target' is used instead.
789 If FORCE is t, install a package even if it has already been
791 Other non-nil cases of FORCE are reserved for future development."
797 (not (elinstall-already-installed project-name
)))
798 ;;$$REMOVE ME - I commented it out just for development.
799 '(yes-or-no-p (format "Install %s " project-name
)))
802 (elinstall-find-actions-by-spec
807 ;;$$RETHINK ME - maybe hand this work off to autoload?
808 ;;This is just the default loaddefs file, spec actually
810 (def-file .
"elinstall-loaddefs.el" ))))
811 (segment-list (elinstall-segregate-actions actions
)))
815 (elinstall-do-segment segment force
))
817 ;;$$REMOVE ME - I commented it out just for development.
818 '(elinstall-record-installed project-name
))))
821 ;;;_ , elinstall-update-directory-autoloads
823 (defun elinstall-update-directory-autoloads (dir)
826 (interactive "DUpdate autoloads from directory: ")
829 ;;$$IMPROVE ME make DIR absolute
830 (format "Elisp files of %s" dir
)
833 (autoload-ensure-default-file
834 (expand-file-name generated-autoload-file
835 (expand-file-name "lisp" source-directory
)))))
838 ;;;_ , elinstall-update-file-autoloads
840 (defun elinstall-update-file-autoloads (file &optional save-after
)
843 (interactive "fUpdate autoloads for file: \np")
845 (format "Elisp file %s" file
)
848 (autoload-ensure-default-file
849 (expand-file-name generated-autoload-file
850 (expand-file-name "lisp" source-directory
))))
861 ;;;_ * Local emacs vars.
862 ;;;_ + Local variables:
867 ;;; elinstall.el ends here