2 added 1 character in body
source | link
  1. You should use the new advice system `nadvice` instead of the old one.
  2. xref-find-definitions prompts the user already in its `interactive-form`. So you need to run `open-tags-file` in the interactive-form of your advice for the case that xref-find-definitions is called interactively.
  3. I use the interactive specification of xref-find-definitions of emacs 25.3.1 in my-xref-find-definitions-interactive-spec. It may be that you need to adapt that to your emacs version. See the comment in the code. A variant would be to query the interactive form of xref-find-definitions with the help of function interactive-form. But that would complicate the code.
  4. The code is untested. Please test it for yourself and comment whether it is working for you or not.
  5. You usage of tags-file-name looks a bit strange. I think youyour test should be (unless (stringp tags-file-name) ...). That would be more descriptive.

    (require 'xref)
    
    (defun open-tags-file ()
      "Use \"my_tag_file\" as tag file for xref."
      (interactive)
      (unless (stringp tags-file-name)
          (when (file-exists-p "my_tag_file")
            (visit-tags-table "my_tag_file"))))
    
    (defun my-xref-find-definitions-interactive-spec ()
      "Interactive form for `open-tags-file'."
      (open-tags-file)
      ;; The following is the interactive specification of `xref-find-definitions' in emacs 25.3.1.
      ;; It may be that you need to adapt it to your emacs version.
      (list (xref--read-identifier "Find definitions of: ")))
    
    (defun my-xref-find-definitions-before-ad (&rest _args)
      "Advice for `xref-find-definitions' that handles the \"my_tag_file\"."
      (interactive (my-xref-find-definitions-interactive-spec))
      (open-tags-file))
    
    (advice-add 'xref-find-definitions :before #'my-xref-find-definitions-before-ad)
    
  1. You should use the new advice system `nadvice` instead of the old one.
  2. xref-find-definitions prompts the user already in its `interactive-form`. So you need to run `open-tags-file` in the interactive-form of your advice for the case that xref-find-definitions is called interactively.
  3. I use the interactive specification of xref-find-definitions of emacs 25.3.1 in my-xref-find-definitions-interactive-spec. It may be that you need to adapt that to your emacs version. See the comment in the code. A variant would be to query the interactive form of xref-find-definitions with the help of function interactive-form. But that would complicate the code.
  4. The code is untested. Please test it for yourself and comment whether it is working for you or not.
  5. You usage of tags-file-name looks a bit strange. I think you test should be (unless (stringp tags-file-name) ...). That would be more descriptive.

    (require 'xref)
    
    (defun open-tags-file ()
      "Use \"my_tag_file\" as tag file for xref."
      (interactive)
      (unless (stringp tags-file-name)
          (when (file-exists-p "my_tag_file")
            (visit-tags-table "my_tag_file"))))
    
    (defun my-xref-find-definitions-interactive-spec ()
      "Interactive form for `open-tags-file'."
      (open-tags-file)
      ;; The following is the interactive specification of `xref-find-definitions' in emacs 25.3.1.
      ;; It may be that you need to adapt it to your emacs version.
      (list (xref--read-identifier "Find definitions of: ")))
    
    (defun my-xref-find-definitions-before-ad (&rest _args)
      "Advice for `xref-find-definitions' that handles the \"my_tag_file\"."
      (interactive (my-xref-find-definitions-interactive-spec))
      (open-tags-file))
    
    (advice-add 'xref-find-definitions :before #'my-xref-find-definitions-before-ad)
    
  1. You should use the new advice system `nadvice` instead of the old one.
  2. xref-find-definitions prompts the user already in its `interactive-form`. So you need to run `open-tags-file` in the interactive-form of your advice for the case that xref-find-definitions is called interactively.
  3. I use the interactive specification of xref-find-definitions of emacs 25.3.1 in my-xref-find-definitions-interactive-spec. It may be that you need to adapt that to your emacs version. See the comment in the code. A variant would be to query the interactive form of xref-find-definitions with the help of function interactive-form. But that would complicate the code.
  4. The code is untested. Please test it for yourself and comment whether it is working for you or not.
  5. You usage of tags-file-name looks a bit strange. I think your test should be (unless (stringp tags-file-name) ...). That would be more descriptive.

    (require 'xref)
    
    (defun open-tags-file ()
      "Use \"my_tag_file\" as tag file for xref."
      (interactive)
      (unless (stringp tags-file-name)
          (when (file-exists-p "my_tag_file")
            (visit-tags-table "my_tag_file"))))
    
    (defun my-xref-find-definitions-interactive-spec ()
      "Interactive form for `open-tags-file'."
      (open-tags-file)
      ;; The following is the interactive specification of `xref-find-definitions' in emacs 25.3.1.
      ;; It may be that you need to adapt it to your emacs version.
      (list (xref--read-identifier "Find definitions of: ")))
    
    (defun my-xref-find-definitions-before-ad (&rest _args)
      "Advice for `xref-find-definitions' that handles the \"my_tag_file\"."
      (interactive (my-xref-find-definitions-interactive-spec))
      (open-tags-file))
    
    (advice-add 'xref-find-definitions :before #'my-xref-find-definitions-before-ad)
    
1
source | link

  1. You should use the new advice system `nadvice` instead of the old one.
  2. xref-find-definitions prompts the user already in its `interactive-form`. So you need to run `open-tags-file` in the interactive-form of your advice for the case that xref-find-definitions is called interactively.
  3. I use the interactive specification of xref-find-definitions of emacs 25.3.1 in my-xref-find-definitions-interactive-spec. It may be that you need to adapt that to your emacs version. See the comment in the code. A variant would be to query the interactive form of xref-find-definitions with the help of function interactive-form. But that would complicate the code.
  4. The code is untested. Please test it for yourself and comment whether it is working for you or not.
  5. You usage of tags-file-name looks a bit strange. I think you test should be (unless (stringp tags-file-name) ...). That would be more descriptive.

    (require 'xref)
    
    (defun open-tags-file ()
      "Use \"my_tag_file\" as tag file for xref."
      (interactive)
      (unless (stringp tags-file-name)
          (when (file-exists-p "my_tag_file")
            (visit-tags-table "my_tag_file"))))
    
    (defun my-xref-find-definitions-interactive-spec ()
      "Interactive form for `open-tags-file'."
      (open-tags-file)
      ;; The following is the interactive specification of `xref-find-definitions' in emacs 25.3.1.
      ;; It may be that you need to adapt it to your emacs version.
      (list (xref--read-identifier "Find definitions of: ")))
    
    (defun my-xref-find-definitions-before-ad (&rest _args)
      "Advice for `xref-find-definitions' that handles the \"my_tag_file\"."
      (interactive (my-xref-find-definitions-interactive-spec))
      (open-tags-file))
    
    (advice-add 'xref-find-definitions :before #'my-xref-find-definitions-before-ad)