emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master eea93a8aaa: Add new user option 'find-library-include-other-files


From: Lars Ingebrigtsen
Subject: master eea93a8aaa: Add new user option 'find-library-include-other-files'
Date: Sat, 5 Feb 2022 18:29:57 -0500 (EST)

branch: master
commit eea93a8aaac30690c6e864f2556010d3b62f4eee
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    Add new user option 'find-library-include-other-files'
    
    * lisp/emacs-lisp/find-func.el (read-library-name--find-files):
    New function (bug#15735).
    (read-library-name): Use it.
    (find-library-include-other-files): New user option.
---
 etc/NEWS                     |  5 +++++
 lisp/emacs-lisp/find-func.el | 43 ++++++++++++++++++++++++++++++++++++-------
 2 files changed, 41 insertions(+), 7 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index e90cf19c16..b432da5d48 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -135,6 +135,11 @@ An autoload definition appears just as a (defun . NAME) 
and the
 
 * Changes in Emacs 29.1
 
+---
+** New user option 'find-library-include-other-files'.
+If non-nil, commands like 'M-x find-library' will only include library
+files in the completion alternatives.
+
 ** New command 'sqlite-mode-open-file' for examining an sqlite3 file.
 This uses the new 'sqlite-mode' which allows listing the tables in a
 DB file, and examining and modifying the columns and the contents of
diff --git a/lisp/emacs-lisp/find-func.el b/lisp/emacs-lisp/find-func.el
index 6eac25c100..571087c963 100644
--- a/lisp/emacs-lisp/find-func.el
+++ b/lisp/emacs-lisp/find-func.el
@@ -183,6 +183,16 @@ See the functions `find-function' and `find-variable'."
   :group 'find-function
   :version "20.3")
 
+(defcustom find-library-include-other-files t
+  "If non-nil, `read-library-name' will also include non-library files.
+This affects commands like `read-library'.
+
+If nil, only library files (i.e., \".el\" files) will be offered
+for completion."
+  :type 'boolean
+  :version "29.1"
+  :group 'find-function)
+
 ;;; Functions:
 
 (defun find-library-suffixes ()
@@ -302,7 +312,10 @@ TYPE should be nil to find a function, or `defvar' to find 
a variable."
 Interactively, prompt for LIBRARY using the one at or near point.
 
 This function searches `find-library-source-path' if non-nil, and
-`load-path' otherwise."
+`load-path' otherwise.
+
+See the `find-library-include-other-files' user option for
+customizing the candidate completions."
   (interactive (list (read-library-name)))
   (prog1
       (switch-to-buffer (find-file-noselect (find-library-name library)))
@@ -317,8 +330,6 @@ in a directory under `load-path' (or 
`find-library-source-path',
 if non-nil)."
   (let* ((dirs (or find-library-source-path load-path))
          (suffixes (find-library-suffixes))
-         (table (apply-partially 'locate-file-completion-table
-                                 dirs suffixes))
          (def (if (eq (function-called-at-point) 'require)
                   ;; `function-called-at-point' may return 'require
                   ;; with `point' anywhere on this line.  So wrap the
@@ -332,10 +343,28 @@ if non-nil)."
                         (thing-at-point 'symbol))
                     (error nil))
                 (thing-at-point 'symbol))))
-    (when (and def (not (test-completion def table)))
-      (setq def nil))
-    (completing-read (format-prompt "Library name" def)
-                     table nil nil nil nil def)))
+    (if find-library-include-other-files
+        (let ((table (apply-partially #'locate-file-completion-table
+                                      dirs suffixes)))
+          (when (and def (not (test-completion def table)))
+            (setq def nil))
+          (completing-read (format-prompt "Library name" def)
+                           table nil nil nil nil def))
+      (let ((files (read-library-name--find-files dirs suffixes)))
+        (when (and def (not (member def files)))
+          (setq def nil))
+        (completing-read (format-prompt "Library name" def)
+                         files nil t nil nil def)))))
+
+(defun read-library-name--find-files (dirs suffixes)
+  "Return a list of all files in DIRS that match SUFFIXES."
+  (let ((files nil)
+        (regexp (concat (regexp-opt suffixes) "\\'")))
+    (dolist (dir dirs)
+      (dolist (file (ignore-errors (directory-files dir nil regexp t)))
+        (and (string-match regexp file)
+             (push (substring file 0 (match-beginning 0)) files))))
+    files))
 
 ;;;###autoload
 (defun find-library-other-window (library)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]