0

I've successfully created a Tag file and could wade through the code using keys like M-,, but every time I need to choose the tag file for searching, so, I am looking for a way to hook the tag file I created with Emacs , found one reference, but not sure how to execute it on git-bash or how execute it on Emacs command-line, Helps appreciated.......

https://www.gnu.org/software/emacs/manual/html_node/emacs/Select-Tags-Table.html

3
  • What do you mean by "hook"? That term has a specific meaning in Emacs, but I don't think you're using it that way. Can you explain in more detail what you're trying to achieve? – phils Jan 2 '20 at 21:38
  • Meaning: Attach the TAG with Emacs during the startup, so that, editor won't nag me "where is the TAG file???" when I am using ctags key. – RaGa__M Jan 3 '20 at 6:04
  • If you always want to load the same tag file, you need to execute elisp code at emacs startup to set tags-file-name. Do you already have customizations? Add code to ~/emacs.d/init.el like: (setq tags-file-name (expand-file-name “~/src/path-to-tags”)) – InHarmsWay Jan 3 '20 at 12:47
0

M-, executes xref-pop-marker-stacker it just go back the position before you start code navigation.

M-. executes xref-find-definitions which does the code navigation thing.

So your question is not clear what exactly you want to achieve. M-, will always be successful since you already finished code navigation and the original start position is "marked".

You need only set global variable tag-file-name or tags-table-list once. And you will never be asked to set either of them again unless:

  • for some reason, you set the above flags as buffer local variable directly or indirectly. Besides, only one of the above two variables should be set. It's better to run C-h v tag-file-name or check tags-table-list before you running xref-find-definitions, make sure it's not empty.

  • The current file is binding a different xref-backend. Eval expression (xref-find-backend) and make sure it's etags before running xref-find-definition

I suggest you using counsel-etags for code navigation. It works out of box and has many improvements.

For example, it can use ctags to update tags file automatically, and ctags is running as background process which does block the Emacs UI interaction.

Here is function to update tags file,

(defun counsel-etags-async-shell-command (command tags-file)
  "Execute string COMMAND and create TAGS-FILE asynchronously."
  (let* ((proc (start-process "Shell" nil shell-file-name shell-command-switch command)))
    (set-process-sentinel
     proc
     `(lambda (process signal)
        (let* ((status (process-status process)))
          (when (memq status '(exit signal))
            (cond
             ((string= (substring signal 0 -1) "finished")
              (let* ((cmd (car (cdr (cdr (process-command process))))))
                (if counsel-etags-debug (message "`%s` executed." cmd))
                ;; reload tags-file
                (when (and ,tags-file (file-exists-p ,tags-file))
                  (run-hook-with-args 'counsel-etags-after-update-tags-hook ,tags-file)
                  (message "Tags file %s was created." ,tags-file))))
             (t
              (message "Failed to create tags file.\nerror=%s\ncommand=%s"
                       signal
                       ,command)))))))))

As you can see, the code is not simple. There are lots of technical know how. Since I've already provided a robust solution (Yes, I'm the developer), don't waste time to re-invent the wheel.

5
  • 1
    TBH, the question is how to get things fixed given a scenario X, and if I am reading it correctly, you answer is: get off of that scenario X. – RaGa__M Jan 3 '20 at 6:08
  • I'm not sure what Q you have answered here but it's not the original one which is simply how to get emacs to auto open the local tags file for a certains file/project. – RichieHH Feb 3 '20 at 11:43
  • @RichieHH, I did answer the question , see "You need only set global variable tag-file-name or tags-table-list once." – chen bin Feb 3 '20 at 13:07
  • Fair enough, but it's more than global. Many projects have their own tags files. Emacs shold switch based on project. Maybe projectile probably does? Another option, if supported, is to switch to lsp and have "live" tags and cross referencing. – RichieHH Feb 3 '20 at 14:28
  • Could use buffer local variable or directory local variable, see gnu.org/software/emacs/manual/html_node/elisp/… and gnu.org/software/emacs/manual/html_node/emacs/… – chen bin Feb 4 '20 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.