user673592
user673592

Reputation: 2120

Automatically jump to tag in Emacs

I'd like to have find-tag automatically accept the default option (i.e. the word at point) and jump to the tag postion without prompting.

Is this possible?

I'm also using the advised version of find-tag from Emacswiki, that in case of match re-runs ctags. So I'd like something like this:

is current word a known tag?
-> yes: jump to it without further confirmation
-> no: rerun ctags
is it known now?
-> yes: jump to it without further confirmation
-> no: prompt user for input

Thank you!

Upvotes: 4

Views: 2311

Answers (5)

Satya Mishra
Satya Mishra

Reputation: 111

Emacs 25 does this by default. M-. (xref-find-definitions) jumps to the definition and M-, (xref-pop-marker-stack) pops back.

Upvotes: 0

Michael Bosworth
Michael Bosworth

Reputation: 2183

This is one of the top hits on Google for "find tags emacs no prompt." For the simple version of that - without the ctag-regeneration logic the poster mentioned - it seems the key is:

(find-tag (find-tag-default))

So, for me, this works:

(defun find-tag-no-prompt ()
  "Jump to the tag at point without prompting"
  (interactive)
  (find-tag (find-tag-default)))
;; don't prompt when finding a tag
(global-set-key (kbd "M-.") 'find-tag-no-prompt)

Upvotes: 6

Cezar Halmagean
Cezar Halmagean

Reputation: 902

here's a slightly modified version that loads dependent gems (useful in ruby on rails)

  (defun build-ctags ()
      (interactive)
      (message "building project tags")
      (let ((default-directory (eproject-root)))
        (shell-command (concat "exctags -e -R --extra=+fq --exclude=db --exclude=test --exclude=.git --exclude=public -f TAGS * " (trim-string (shell-command-to-string "rvm gemdir")) "/gems/*"))
        (visit-project-tags)
        (message "tags built successfully")))

Upvotes: 0

user673592
user673592

Reputation: 2120

Well, I found a hack-y solution:

;; auto jump
(global-set-key (kbd "C-x C-M->") 'find-tag) ; bind to some unused placeholder
(global-set-key (kbd "M-.") (kbd "C-x C-M-> <return>"))

First bind find-tag to some dummy binding that you'll never use anyway (this step is necessary to avoid infinite loops). Then bind M-. to this new binding + <return>.

Ugly, but works... I'll leave the question open if somebody has a better answer (including handling of failed search as described in the original question).

Upvotes: 2

hbin
hbin

Reputation: 2737

Here is my settings for ctags, works awesome for me. I borrow it from here.

(require 'eproject)
(require 'etags-select)

(defun build-ctags ()
  (interactive)
  (message "building project tags")
  (let ((root (eproject-root)))
    (shell-command
     (concat "ctags-exuberant -e -R --extra=+fq --exclude=db --exclude=test --exclude=.git --exclude=public -f " root "TAGS " root)))
  (visit-project-tags)
  (message "tags built successfully"))

(defun visit-project-tags ()
  (interactive)
  (let ((tags-file (concat (eproject-root) "TAGS")))
    (visit-tags-table tags-file)
    (message (concat "Loaded " tags-file))))

(defun hbin-find-tag ()
  "Borrow from http://mattbriggs.net/blog/2012/03/18/awesome-emacs-plugins-ctags/"
  (interactive)
  (if (file-exists-p (concat (eproject-root) "TAGS"))
      (visit-project-tags)
    (build-ctags))
  (etags-select-find-tag-at-point))

(global-set-key (kbd "M-.") 'hbin-find-tag)

PS: you may be need these:

git://github.com/jrockway/eproject.git
git://github.com/emacsmirror/etags-select.git

Upvotes: 2

Related Questions