Revert the temporary workaround for bug #162
[sxemacs] / lisp / emod-utils.el
1 ;; emod-utils.el --- Lisp utils for emodules   -*- Emacs-Lisp -*-
2
3 ;; Copyright (C) 2008 Steve Youngs
4
5 ;; Author:     Steve Youngs <steve@sxemacs.org>
6 ;; Maintainer: SXEmacs Development Team <sxemacs-devel@sxemacs.org>
7 ;; Created:    <2008-05-01>
8 ;; Homepage:   http://www.sxemacs.org/
9 ;; Keywords:   util, module, emodule, dumped
10
11 ;; This file is part of SXEmacs.
12
13 ;; Redistribution and use in source and binary forms, with or without
14 ;; modification, are permitted provided that the following conditions
15 ;; are met:
16 ;;
17 ;; 1. Redistributions of source code must retain the above copyright
18 ;;    notice, this list of conditions and the following disclaimer.
19 ;;
20 ;; 2. Redistributions in binary form must reproduce the above copyright
21 ;;    notice, this list of conditions and the following disclaimer in the
22 ;;    documentation and/or other materials provided with the distribution.
23 ;;
24 ;; 3. Neither the name of the author nor the names of any contributors
25 ;;    may be used to endorse or promote products derived from this
26 ;;    software without specific prior written permission.
27 ;;
28 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
29 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 ;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
37 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
38 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
40 ;;; Commentary:
41 ;;
42 ;;    Here are a number of utils for interacting with emodules, such
43 ;;    as finding them, loading them.  That sort of thing.
44 ;;
45 ;;    This file is dumped with SXEmacs.
46
47 ;;; Todo:
48 ;;
49 ;;
50
51 ;;; Code:
52 (defvar emodule-completions nil
53   "List of emodules for use in completion with `load-module'.")
54
55 (defvar load-module-history nil
56   "History for `load-module'.")
57
58 (defun emodule-completions (&optional path)
59   "Return a list of emodules.
60
61 It searches through `module-load-path' by default, or PATH if that
62 optional argument is set.
63
64 PATH can be either a list of path strings, or it can be a colon
65 delimited path string."
66   (let ((dirs (or (if (stringp path)
67                       (split-string-by-char path ?:)
68                     path)
69                   module-load-path))
70         (types (mapfam
71                 #'(lambda (e)
72                     (replace-in-string e "\\." ""))
73                 :initiator "\\.\\("
74                 :terminator "\\)$"
75                 :separator "\\|"
76                 :result-type #'concat module-extensions))
77         completions)
78     (while dirs
79       (let ((files (directory-files-recur (car dirs) nil types 'list t 0)))
80         (when (and files (> (length files) 0))
81           (setq completions
82                 (append completions
83                         (mapfam
84                          #'file-name-sans-extension files
85                          :result-type #'list))))
86         (setq dirs (cdr dirs))))
87     (remove-duplicates (remove nil completions) :test #'string-equal)))
88
89 (defun locate-module (emod)
90   "Similar to `locate-library', but for emodules."
91   (interactive
92    (list (completing-read "Locate Emodule: "
93                           (mapfam #'list (or emodule-completions
94                                              (emodule-completions)))
95                           nil nil nil load-module-history)))
96   (unless emodule-completions
97     (setq emodule-completions (emodule-completions)))
98   (let* ((emod (file-name-sans-extension emod))
99          (location (locate-file emod module-load-path
100                                 module-extensions)))
101     (if (interactive-p)
102         (message "%s is: %s" emod location)
103       location)))
104
105 (defun load-module (emod)
106   "Similar to `load-library', but for emodules."
107   (interactive
108    (list (completing-read "Load emodule: "
109                           (mapfam #'list (or emodule-completions
110                                              (emodule-completions)))
111                           nil nil nil load-module-history)))
112   (unless emodule-completions
113     (setq emodule-completions (emodule-completions)))
114   (if (string-equal emod "")
115       (error 'invalid-argument emod)
116     (and-fboundp #'load-module-file
117       (load-module-file
118        (or (locate-module emod)
119            emod)))))
120
121 (defun list-modules ()
122   "Return a list of loaded emodules, display in echo area when interactive."
123   (interactive)
124   (and-fboundp #'list-loaded-modules
125     (let ((emods (list-loaded-modules)))
126       (if (interactive-p)
127           (message "Loaded emodules: %s"
128                    (mapfam nil emods :separator " " :result-type #'concat))
129         emods))))
130
131 (provide 'emod-utils)
132 ;;; emod-utils.el ends here