emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master ba57f1a: Search upward from current dir for the def


From: Lars Ingebrigtsen
Subject: [Emacs-diffs] master ba57f1a: Search upward from current dir for the default TAGS file
Date: Tue, 8 Oct 2019 12:44:04 -0400 (EDT)

branch: master
commit ba57f1a4273cabb53cbae86ad34b0a4bf01e1513
Author: Hong Xu <address@hidden>
Commit: Lars Ingebrigtsen <address@hidden>

    Search upward from current dir for the default TAGS file
    
    * doc/emacs/maintaining.texi (Select Tags Table): Update the doc
    of `visit-tags-table' (bug#37518).
    * lisp/progmodes/etags.el (tags--find-default-tags-dir-recursively)
    (visit-tags-table): Search upward from current dir for the default
    TAGS file.
---
 doc/emacs/maintaining.texi | 11 ++++++-----
 lisp/progmodes/etags.el    | 31 +++++++++++++++++++++++++------
 2 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/doc/emacs/maintaining.texi b/doc/emacs/maintaining.texi
index 519667d..ef448dd 100644
--- a/doc/emacs/maintaining.texi
+++ b/doc/emacs/maintaining.texi
@@ -2666,11 +2666,12 @@ etags --language=none \
 @subsection Selecting a Tags Table
 
 @findex visit-tags-table
-  Emacs has at any time at most one @dfn{selected} tags table.  All the
-commands for working with tags tables use the selected one.  To select
-a tags table, type @kbd{M-x visit-tags-table}, which reads the tags
-table file name as an argument, with @file{TAGS} in the default
-directory as the default.
+  Emacs has at any time at most one @dfn{selected} tags table.  All
+the commands for working with tags tables use the selected one.  To
+select a tags table, type @kbd{M-x visit-tags-table}, which reads the
+tags table file name as an argument, with @file{TAGS} defaulting to
+the first directory that contains a file named @file{TAGS} encountered
+when recursively searching upward from the default directory.
 
 @vindex tags-file-name
   Emacs does not actually read in the tags table contents until you
diff --git a/lisp/progmodes/etags.el b/lisp/progmodes/etags.el
index c40422d..906ab37 100644
--- a/lisp/progmodes/etags.el
+++ b/lisp/progmodes/etags.el
@@ -274,6 +274,19 @@ buffer-local and set them to nil."
   (setq buffer-undo-list t)
   (initialize-new-tags-table))
 
+(defun tags--find-default-tags-dir-recursively (current-dir)
+  "Find the directory in which the default TAGS file lives.
+It is the first directory that contains a file named TAGS
+encountered when recursively searching upward from CURRENT-DIR."
+  (let ((tag-filename (expand-file-name "TAGS" current-dir)))
+    (if (file-exists-p tag-filename)
+        current-dir
+      (let ((parent-dir
+             (file-name-directory (directory-file-name current-dir))))
+        (if (string= parent-dir current-dir)  ;; root dir is reached
+            nil
+          (tags--find-default-tags-dir-recursively parent-dir))))))
+
 ;;;###autoload
 (defun visit-tags-table (file &optional local)
   "Tell tags commands to use tags table file FILE.
@@ -286,12 +299,18 @@ from Lisp, if the optional arg LOCAL is non-nil, set the 
local value.
 When you find a tag with \\[find-tag], the buffer it finds the tag
 in is given a local value of this variable which is the name of the tags
 file the tag was in."
-  (interactive (list (read-file-name "Visit tags table (default TAGS): "
-                                    default-directory
-                                    (expand-file-name "TAGS"
-                                                      default-directory)
-                                    t)
-                    current-prefix-arg))
+  (interactive
+   (let ((default-tag-dir
+           (or (tags--find-default-tags-dir-recursively default-directory)
+               default-directory)))
+     (list (read-file-name
+            "Visit tags table (default TAGS): "
+            ;; default to TAGS from default-directory up to root.
+            default-tag-dir
+            (expand-file-name "TAGS" default-tag-dir)
+            t)
+           current-prefix-arg)))
+
   (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
   ;; Bind tags-file-name so we can control below whether the local or
   ;; global value gets set.



reply via email to

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