From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Dmitry Gutov Newsgroups: gmane.emacs.bugs Subject: bug#43086: [PATCH] Allow tags backend to not query for TAGS file Date: Wed, 16 Sep 2020 13:53:46 +0300 Message-ID: References: <87d02yefr5.fsf@posteo.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------10F7F4AC33FDB703B10FCBB2" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="39047"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 Cc: 43086@debbugs.gnu.org To: "Philip K." Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Wed Sep 16 12:54:21 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kIV4v-000A0u-C4 for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 16 Sep 2020 12:54:21 +0200 Original-Received: from localhost ([::1]:42290 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kIV4t-00008E-Rr for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 16 Sep 2020 06:54:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:40240) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kIV4c-000080-F9 for bug-gnu-emacs@gnu.org; Wed, 16 Sep 2020 06:54:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:50034) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kIV4b-0000RN-MO for bug-gnu-emacs@gnu.org; Wed, 16 Sep 2020 06:54:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kIV4b-0000oE-Lg for bug-gnu-emacs@gnu.org; Wed, 16 Sep 2020 06:54:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Dmitry Gutov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 16 Sep 2020 10:54:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 43086 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 43086-submit@debbugs.gnu.org id=B43086.16002536393102 (code B ref 43086); Wed, 16 Sep 2020 10:54:01 +0000 Original-Received: (at 43086) by debbugs.gnu.org; 16 Sep 2020 10:53:59 +0000 Original-Received: from localhost ([127.0.0.1]:33347 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kIV4Z-0000ny-01 for submit@debbugs.gnu.org; Wed, 16 Sep 2020 06:53:59 -0400 Original-Received: from mail-lj1-f196.google.com ([209.85.208.196]:35616) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kIV4U-0000ni-Lj for 43086@debbugs.gnu.org; Wed, 16 Sep 2020 06:53:57 -0400 Original-Received: by mail-lj1-f196.google.com with SMTP id a15so5546517ljk.2 for <43086@debbugs.gnu.org>; Wed, 16 Sep 2020 03:53:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=WT3R+nuzcW4G+qXPJwXKEoBrFBOXIkk/kc4nSNFComo=; b=gFQqQghXhJ1UdxcKoqbtBp2ofhCzKmEXyGnl6w6LIq8fTomi1it74TR62sOvm5/VHw yE7R6mxcSKaiTNM8W3O70Nv/igBbIU+TnAcy+Sbx8Gkue2APOJEwCematDhxoWO+lrSX WkfO+xQbcmB2xAKYDUkPsGVQV7wUYy0GhCGAn4Az3IN89G7SFYXcG4eXHouKfJ3rB31A mXYhp6SijlQcJ8156bABGex07OZHKuMV5OCIFAbNTW4ketyrI+wJQl/Cw5lnfxfYCUTL eEdso6vyYqzMebPpC38F7wr6HPry5ea0By1oBaVxfFokogizCmN3PaiyFgWDNpfIWhEk 4H8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:cc:references:from:message-id :date:user-agent:mime-version:in-reply-to:content-language; bh=WT3R+nuzcW4G+qXPJwXKEoBrFBOXIkk/kc4nSNFComo=; b=UMlcQwKZTgwHYLbnmrcBGB56/7p2HE5qTiZGu+RPcX1BvoXnU2eVeGqNMKRvDxyWe6 JGEX4SoPkNRQilY4lAf6A1XnYFcUPrpAU6J5t6jawbr7XrysAwPHScDnmgpwAix6r5wr 7ffbcgF1Z8uphMqcX0n5vD36I+KR6TwOtebyuNvIIrBWXlbmaAYLY4zgZsgIzsFEYdYL JaO8V5e4vNB0+pW1lR7B7y030AtdInGArf32Kv3DiJc6/nizgjWi+txWrv9tzxt0//OM Xj2JwmuI1JY/IcgX+HdHeDKImrR7zoH2wJIxU8agSaepDaDTT+Ip2Vi9fntr6UOlZiMp nBEg== X-Gm-Message-State: AOAM532auVontSyQzKkBkrn3AbbANjD/p61e5qjj+Uvs292dTfa5b0VX 4gOtN65v7nUVu+YD9arzxTTPhQurzMQ= X-Google-Smtp-Source: ABdhPJyKOOjMKB0zCXe9GNmtA8ninklQffuhgSYiWtTJB21d6IAU+zdPV9MZAmcSTJeVWq7StMBdTA== X-Received: by 2002:a2e:b174:: with SMTP id a20mr8061560ljm.407.1600253628153; Wed, 16 Sep 2020 03:53:48 -0700 (PDT) Original-Received: from [192.168.0.104] ([94.229.108.16]) by smtp.googlemail.com with ESMTPSA id e9sm4495268lfn.237.2020.09.16.03.53.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 16 Sep 2020 03:53:47 -0700 (PDT) In-Reply-To: <87d02yefr5.fsf@posteo.net> Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:188163 Archived-At: This is a multi-part message in MIME format. --------------10F7F4AC33FDB703B10FCBB2 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 07.09.2020 00:50, Philip K. wrote: > Dmitry Gutov writes: > >> Hi! >> >> On 28.08.2020 15:50, Philip K. wrote: >> >>> the xref backend for etags can be annoying at times, especially in >>> combination with other backends. This patch should improve the >>> situation, by allowing the user to configure how and when the etags >>> backend is activated. The new user option etags-query-file would allow >>> the backend to never query a TAGS file, or conditionally, depending on >>> the existence of a TAGS file (in which case it can also be automatically >>> loaded). >> >> This is a interesting patch, but it calls for some discussion: >> >> - The possible values all look pretty clever, but there are a lot of >> them! Do we expect them all to be in demand? Ideally, I'd only leave 2-3 >> of them, to reduce the number of workflows we need to care about. > > I'm ok with that, the variable could also be turned into a hook if > reducing preconfigured options while making it easy to add new > behaviours. Not sure how the direct conversion to a hook would look like. In any case, when I talked about using a hook (find-file-hook in particular), the main benefit I had in mind is that the effect would cover all uses of etags.el. Meaning, also tags-completion-at-point-function. >> The rest could probably be set up in individual user configurations in >> find-file-hook (like Projectile does). > > I'm not familiar with how Projectile does this, or how this would work > in general? Could you give an example? projectile-mode adds projectile-find-file-hook-function to find-file-hook, which, upon visiting any file, looks for projectile-tags-file-name (usually "TAGS")'s presence in the root of the project. When such file exists, it calls (visit-tags-table tags-file t), to visit the tags table "locally". Since project.el does not have a minor mode, and out of general (admittedly handwavy) considerations of performance, we don't do that. But a user could customize their find-file-hook with a similar logic. If you like, you could also add a pre-existing function like that to project.el that a user could then just add to find-file-hook. >> - The variable name implies it affects how etags.el works globally, but >> the actual effect seems limited to the xref backend function. We should >> either rename it to something like etags-xref-query-file, or consider >> having it affect tags-completion-at-point-function as well. > > I'm fine with either, but the first option seems simpler, unless there > is still interest in maintaining the non-xref interface. There might be some other benefit to the latter, but it doesn't seem trivial, so the former sounds fine to me as well. >> - One current persistent annoyance is that currently >> xref-find-references doesn't work well in many files where the xref >> backend is the default one (etags) when ido-mode or icomplete-mode are >> enabled because it prompts for the tags file to do identifier >> completion. I wonder if the "no query" option will help with this, too. > > Unless I'm misunderstanding something, that's exactly the situation > that motivated me to write these patches (just because of Ivy not Ido). I set etags-query-file to nil and call xref-find-references with ido-mode enabled. And still get queried for a tags file. Because etags--xref-backend still returns 'etags, but then xref-backend-identifier-completion-table calls tags-lazy-completion-table, which does the prompting. If I make etags--xref-backend return nil, I simply get "No applicable method: xref-backend-identifier-completion-table, nil" instead. So a deeper change is needed. >>> I could imagine this might be extended to allow an auto-generate option, >>> but that feature seems out of scope of this patch, and probably would >>> require some interoperation with project.el. >> >> Indeed. Actually, I have an old, WIP patch for tag file auto-generation >> which, yes, uses project.el. I can post it again if you're curious. > > Sure, why not? Here it is. It might even be functional. The reason it's not installed yet, is I was looking to create a seamless user experience with it, when they don't need to know which tags file is visited, and when. And with tags being quickly updated after a file is changed and saved. The related discussion about necessary changes to etags fizzled out, however. --------------10F7F4AC33FDB703B10FCBB2 Content-Type: text/x-patch; charset=UTF-8; name="etags-project.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="etags-project.diff" diff --git a/lisp/progmodes/etags.el b/lisp/progmodes/etags.el index a31668e1ba..9da2143525 100644 --- a/lisp/progmodes/etags.el +++ b/lisp/progmodes/etags.el @@ -2109,7 +2109,9 @@ etags-xref-find-definitions-tag-order "Tag order used in `xref-backend-definitions' to look for definitions.") ;;;###autoload -(defun etags--xref-backend () 'etags) +(defun etags--xref-backend () + (etags--maybe-use-project-tags) + 'etags) (cl-defmethod xref-backend-identifier-at-point ((_backend (eql etags))) (find-tag--default)) @@ -2180,6 +2182,58 @@ xref-make-etags-location (nth 1 tag-info))) +;;; Simple tags generation, with automatic invalidation + +(defvar etags--project-tags-file nil) +(defvar etags--project-tags-root nil) + +(defun etags--maybe-use-project-tags () + (let (proj) + (when (and etags--project-tags-root + (not (file-in-directory-p default-directory + etags--project-tags-root))) + (etags--project-tags-cleanup)) + (when (and (not (or tags-file-name + tags-table-list)) + (setq proj (project-current))) + (etags--project-tags-generate proj) + ;; Invalidate the scanned tags after any change is written to disk. + (add-hook 'after-save-hook #'etags--project-tags-cleanup) + (visit-tags-table etags--project-tags-file)))) + +(defun etags--project-tags-generate (proj) + (let* ((root (cl-find default-directory + (project-roots proj) + :test #'file-in-directory-p)) + (default-directory root) + (files (all-completions "" (project-file-completion-table proj (list root)))) + (etags-command (executable-find "etags")) + ;; FIXME: List all extensions, or wait for etags fix. + ;; http://lists.gnu.org/archive/html/emacs-devel/2018-01/msg00323.html + (extensions '("rb" "js" "py" "pl" "el" "c" "cpp" "cc" "h" "hh" "hpp" + "java" "go" "cl" "lisp" "prolog" "php" "erl" "hrl" + "F" "f" "f90" "for" "cs" "a" "asm" "ads" "adb" "ada")) + (file-regexp (format "\\.%s\\'" (regexp-opt extensions)))) + (setq etags--project-tags-file (make-temp-file "emacs-project-tags-") + etags--project-tags-root root) + (with-temp-buffer + (mapc (lambda (f) + (when (string-match-p file-regexp f) + (insert f "\n"))) + files) + (shell-command-on-region (point-min) (point-max) + (format "%s - -o %s" etags-command etags--project-tags-file) + nil nil "*etags-project-tags-errors*" t)))) + +(defun etags--project-tags-cleanup () + (when etags--project-tags-file + (delete-file etags--project-tags-file) + (setq tags-file-name nil + tags-table-list nil + etags--project-tags-file nil + etags--project-tags-root nil)) + (remove-hook 'after-save-hook #'etags--project-tags-cleanup)) + (provide 'etags) ;;; etags.el ends here --------------10F7F4AC33FDB703B10FCBB2--