Is it possible to call xref-find-definitions to find the definition at the current point without being prompted to select the default?

I'd like to bind this to a key, so it immediately jumps to a definition. Currently it's prompting to select the default.

This command is prompting me for input every time:

(call-interactively 'xref-find-definitions)

The question is unclear. What identifier do you want it to find the definition of?

By default it uses the identifier at point - and it doesn't prompt you.

If there's no identifier at point, it prompts you.

(To be more precise, it depends on the current command. If (xref--prompt-p this-command) returns non-nil then it will also prompt you, even if there's an identifier at point. But (xref--prompt-p 'xref-find-definitions) returns nil.)

If you're being prompted, and you think there's an identifier at point, then provide a step-by-step recipe to repro, starting from emacs -Q (no init file).

This is the sexp that's apparently prompting you:

(xref--read-identifier "Find definitions of: ")

And this is the definition of that function:

(defun xref--read-identifier (prompt)
  "Return the identifier at point or read it from the minibuffer."
  (let* ((backend (xref-find-backend))
         (id (xref-backend-identifier-at-point backend)))
    (cond ((or current-prefix-arg
               (not id)
               (xref--prompt-p this-command))
           (completing-read (if id
                                (format "%s (default %s): "
                                        (substring prompt 0 (string-match
                                                             "[ :]+\\'" prompt))
                            (xref-backend-identifier-completion-table backend)
                            nil nil nil
                            'xref--read-identifier-history id))
          (t id))))

You can see that it shouldn't prompt, in the conditions I described. You can also use M-x debug-on-entry xref--read-identifier to see exactly why it's prompting you, if it is.

  • Clarified, I want to find the thing at (point)
    – ideasman42
    Apr 27 '20 at 2:07
  • 1
    I was wrapping this command from my own lambda, causing this-command check to trigger the prompt. Using (let ((xref-prompt-for-identifier nil)) ... ) resolves the issue, or setting this-command to the function being called.
    – ideasman42
    Apr 27 '20 at 2:17

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.