[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dict-tree b33203a 060/154: Manage loading and unloading
From: |
Stefan Monnier |
Subject: |
[elpa] externals/dict-tree b33203a 060/154: Manage loading and unloading of dictionaries automatically in predictive-mode |
Date: |
Mon, 14 Dec 2020 12:21:45 -0500 (EST) |
branch: externals/dict-tree
commit b33203adce92ce8339268995ec5bcedf87bd1736
Author: Toby Cubitt <toby-predictive@dr-qubit.org>
Commit: tsc25 <toby-predictive@dr-qubit.org>
Manage loading and unloading of dictionaries automatically in
predictive-mode
---
dict-tree.el | 117 ++++++++++++++++++++++++++++++++++++++++-------------------
1 file changed, 79 insertions(+), 38 deletions(-)
diff --git a/dict-tree.el b/dict-tree.el
index bee0630..b23ff3e 100644
--- a/dict-tree.el
+++ b/dict-tree.el
@@ -2444,45 +2444,50 @@ Interactively, FORCE is the prefix argument."
(defun dictree-load (file)
"Load a dictionary object from file FILE.
-Returns t if successful, nil otherwise.
+Returns the dictionary if successful, nil otherwise.
Interactively, FILE is read from the mini-buffer."
(interactive (list (completing-read
"Load dictionary: "
(apply-partially 'locate-file-completion-table
- load-path
- (get-load-suffixes)))))
+ load-path (get-load-suffixes)))))
;; sort out dictionary name and file name
(let (dictname dict)
- (cond
- ((and (> (length file) 4)
- (string= (substring file -4) ".elc"))
- (setq dictname (file-name-nondirectory (substring file 0 -4))))
- ((and (> (length file) 3)
- (string= (substring file -3) ".el"))
- (setq dictname (file-name-nondirectory (substring file 0 -3))))
- (t (setq dictname (file-name-nondirectory file))))
+ (setq dictname (file-name-nondirectory (file-name-sans-extension file)))
;; load the dictionary
- (unless (load file t)
- (error "Cannot open dictionary file: %s" file))
- (setq dict (eval (intern-soft dictname)))
- (when (not (dictree-p dict))
- (error "Error loading dictionary file: %s" file))
-
- ;; ensure the dictionary name and file name associated with the
- ;; dictionary match the file it was loaded from
- (when (and (string= (file-name-nondirectory file) file)
- (setq file (locate-file file load-path load-suffixes)))
- (setf (dictree-filename dict) file))
- (setf (dictree-name dict) dictname)
-
- ;; make sure the dictionary is in dictree-loaded-list (normally the lisp
- ;; code in the dictionary itself should do this, but just to make sure...)
- (unless (memq dict dictree-loaded-list)
- (push dict dictree-loaded-list))
- (message (format "Loaded dictionary %s" dictname))))
+ (if (not (load file t))
+ ;; if loading failed, throw error interactively, return nil
+ ;; non-interactively
+ (if (interactive-p)
+ (error "Cannot open dictionary file: %s" file)
+ nil)
+
+ (setq dict (eval (intern-soft dictname)))
+ (if (not (dictree-p dict))
+ ;; if loading failed, throw error interactively, return nil
+ ;; non-interactively
+ (if (interactive-p)
+ (error "Error loading dictionary file: %s" file)
+ nil)
+
+ ;; ensure the dictionary name and file name associated with the
+ ;; dictionary match the file it was loaded from
+ (when (and (string= (file-name-nondirectory file) file)
+ (setq file (locate-file file load-path load-suffixes)))
+ (setf (dictree-filename dict) file))
+ (setf (dictree-name dict) dictname)
+
+ ;; make sure the dictionary is in dictree-loaded-list (normally the
+ ;; lisp code in the dictionary itself should do this, but just to make
+ ;; sure...)
+ (unless (memq dict dictree-loaded-list)
+ (push dict dictree-loaded-list))
+ (message (format "Loaded dictionary %s" dictname))
+
+ ;; return dictionary
+ dict))))
@@ -3118,23 +3123,59 @@ OVERWRITE is the prefix argument, and TYPE is always
'string."
;; Minibuffer completion
(defvar dictree-history nil
- "History list for commands that read an existing ditionary name.")
+ "History list for commands that read a dictionary name.")
+
+(defvar dictree-loaded-history nil
+ "History list for commands that read the name of a loaded dictionary.")
-(defun read-dict (prompt &optional default dictlist)
+(defun read-dict (prompt &optional default dictlist allow-unloaded)
"Read the name of a dictionary with completion, and return it.
Prompt with PROMPT. By default, return DEFAULT. If DICTLIST is
-supplied, only complete on dictionaries in that list."
- (let (dictnames)
+supplied, only complete on dictionaries in that list.
+
+If ALLOW-UNLOADED is non-nil, also complete on the names of
+unloaded dictionaries (actually, on any Elisp file in the current
+`load-path' restricted to subdirectories of your home
+directory). If an unloaded dictionary is read, the name of the
+Elisp file will be returned, without extension, suitable for
+passing to `load-library'."
+ (let (dictname paths)
+ ;; when allowing unloaded dictionaries...
+ (when allow-unloaded
+ ;; get paths in load-path that are subdirectories of home directory
+ (dolist (d load-path)
+ (when (eq (aref d 0) ?~) (push d paths)))
+ ;; gather names of all Elisp libraries in this restricted load-path
+ (dolist (f (all-completions
+ "" (apply-partially 'locate-file-completion-table
+ paths (get-load-suffixes))))
+ (when (and (null (file-name-directory f))
+ (or (string= (file-name-extension f) "el")
+ (string= (file-name-extension f) "elc"))
+ (not (member (file-name-sans-extension f) dictname)))
+ (push (file-name-sans-extension f) dictname))))
+ ;; gather names of loaded dictionaries
(mapc (lambda (dict)
(unless (or (null (dictree-name dict))
- (member (dictree-name dict) dictnames))
- (push (list (dictree-name dict)) dictnames)))
+ (member (dictree-name dict) dictname))
+ (push (list (dictree-name dict)) dictname)))
(or dictlist dictree-loaded-list))
- (eval (intern-soft
- (completing-read prompt dictnames nil t nil
- 'dictree-history default)))))
+ ;; do completing-read
+ (setq dictname (completing-read prompt dictname nil t nil
+ (if allow-unloaded
+ 'dictree-history
+ 'dictree-loaded-history)
+ default))
+ ;; return dictionary
+ (if allow-unloaded
+ (or (and (condition-case nil
+ (dictree-p (eval (intern-soft dictname)))
+ (void-variable nil))
+ (eval (intern-soft dictname)))
+ dictname)
+ (eval (intern-soft dictname)))))
- [elpa] externals/dict-tree baa4931 028/154: Doesn't quite work - revert to breaking setf abstraction, (continued)
- [elpa] externals/dict-tree baa4931 028/154: Doesn't quite work - revert to breaking setf abstraction, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 5834dac 036/154: Replaced bare avl-trees, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 673fd2a 029/154: Abstract away the setcar used to set the data component of a cell, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 2700e21 035/154: Don't compile wrapped functions explicitly, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 70f99ee 037/154: Make certain dictionary commands (mostly saving and loading) interactive again., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 35346a4 041/154: Bug-fix in dictree--write-dict-code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 608fcd4 049/154: Bug-fix to dictree--write-dict-code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 179c0b5 045/154: Bug-fix to messages displayed by dictree-populate-from-file, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 6b24547 052/154: Allow "]" to be included in a negated character alternatives, by placing immediately after the "[^"., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree a61f90e 053/154: Bug-fix in dictree-wildcard-search, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree b33203a 060/154: Manage loading and unloading of dictionaries automatically in predictive-mode,
Stefan Monnier <=
- [elpa] externals/dict-tree 920d959 077/154: Replaced advice with cedet-edebug.el for pretty-printing, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 9946a1d 075/154: Dictionary now defaults to current dict in in most dictionary-related commands., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree aa1a45d 105/154: Enable lexical binding, and fix issues it picks up., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree ce48d61 104/154: Use symbol-value instead of eval., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 9eb7d29 014/154: Bumped version number and updated commentary., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 034a2d4 020/154: Fixed dictree-member-p bug; dictionaries no longer 'provide' feature., Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 104fd29 022/154: Roll back changes to read-dict - not clear that they're necessary even under windoze, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 6b05d61 025/154: Another bug-fix to the new cache policy code, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree daad3f1 040/154: Fixed bugs in dictree--write-dict-code and dictree-save, Stefan Monnier, 2020/12/14
- [elpa] externals/dict-tree 0aed5d4 043/154: Modified dictree--read-line to call data and plist load functions with null argument, Stefan Monnier, 2020/12/14