; Time-stamp: <2006-11-22 22:25:42 jcgs> ; previous edit seems to have been <91/04/09 10:21:06 john> ;; This program is free software; you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by the ;; Free Software Foundation; either version 2 of the License, or (at your ;; option) any later version. ;; This program is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; You should have received a copy of the GNU General Public License along ;; with this program; if not, write to the Free Software Foundation, Inc., ;; 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA (provide 'library-path) (defun clean-load-path () "Remove rubbish from the load path." (let* ((original-load-path load-path) (load-path nil) (lp original-load-path)) (while lp (let ((dir (car lp))) (if (file-directory-p dir) (setq load-path (cons dir load-path))) (setq lp (cdr lp)))) (setq load-path (if load-path (nreverse load-path) original-load-path)))) (clean-load-path) (defvar elibs nil "alist of library names to their file names.") (defvar load-path-matching-elibs nil "What the load-path was when elibs was last built.") (defun el-lib-list () "Return an alist of library names to their file names." (if (or (null elibs) (not (eq load-path load-path-matching-elibs)) ) (el-lib-build-list)) elibs) (defun el-lib-build-list () "Build alist of library names to their file names. The elc files are given in preference to .el as they are the better thing to load. Use elisp-file-more-readable-version if you want the more readable version." (interactive) (setq elibs nil) (dolist (directory (reverse load-path)) (when (and directory (file-directory-p directory) (file-readable-p directory)) (when (interactive-p) (message "Scanning load-path: %s" directory)) (dolist (file (directory-files (expand-file-name directory) t "\\.elc?$")) (when (or (string-match "c$" file nil) ;; don't store .el if .elc exists (not (file-exists-p (concat file "c")))) (pushnew (cons (file-base-name file) file) elibs :test 'equal))))) (setq load-path-matching-elibs load-path) (message "Scanning load path... done")) (defun read-library-name (prompt &optional initial) "Read the name of a library on load-path, completing, prompting with PROMPT." (completing-read (if initial (format "%s (default %s):" prompt initial) prompt) ; prompt (el-lib-list) ; table nil ; predicate t ; require-match nil ; initial-input nil ; hist initial ; def )) (defun lilo (libname) "LIbrary LOad with completion on library names when called interactively." (interactive (list (read-library-name "Load library: "))) (load-library libname)) (defun library-which (libname) "Show the name of the file loaded by load-library on LIBNAME." (interactive (list (read-library-name "Name of library to locate: "))) (let ((fullname (cdr (assoc libname (el-lib-list))))) (message "%s is in %s" libname fullname) fullname)) (defun list-libraries-matching (pattern) "List the libraries whose names match PATTERN." (interactive "sList libraries matching pattern: ") (let ((found nil) (libs (el-lib-list))) (while libs (message "%S?" (car libs)) (when (string-match pattern (caar libs)) (setq found (cons (car libs) found))) (setq libs (cdr libs))) (when (interactive-p) (let ((show found)) (with-output-to-temp-buffer (format "*Libraries matching %s*" pattern) (while show (princ (format "%s: %s\n" (caar show) (cdar show))) (setq show (cdr show)))) )) found)) (defun elisp-file-more-readable-version (elisp-file) "Return the more readable version of ELISP-FILE. If ELISP-FILE is an elc file, return the corresponding el file if there is one. Otherwise, return ELISP-FILE." (if (string-match "^\\(.+\\.el\\)c$" elisp-file) (let ((el-name (substring elisp-file (match-beginning 1) (match-end 1)))) (if (file-readable-p el-name) el-name elisp-file)) elisp-file)) (defun find-library (libname) "Find a file, completing on el and elc files on load-path." (interactive (list (read-library-name "Find library file: "))) (let* ((el-libs (el-lib-list)) (filename (elisp-file-more-readable-version (cdr (assoc libname el-libs))))) ;; (message "Find-library using load path %S and lib list %S" load-path el-libs) (when (interactive-p) (message "Library %s is in file %s" libname filename)) (if filename (find-file filename)))) ; end of library-path.el