Emacs如何实现自动跳转到标签?

4

我希望find-tag自动接受默认选项(即光标所在的单词)并跳转到标签位置,无需提示。

这可行吗?

我还使用了Emacswiki中建议的版本的find-tag,在匹配的情况下重新运行ctags。因此,我想要像这样的东西:

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

谢谢!

5个回答

6

这是在谷歌上搜索“emacs查找标签无提示”的热门结果之一。对于不需要ctag再生逻辑的简单版本,关键似乎是:

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

对于我来说,这个是有效的:

(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)

2

以下是我的ctags设置,对我非常有效。我从这里借鉴得来。

(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)

注意:您可能需要以下内容:

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

谢谢。我会尝试查看,不过我更喜欢没有eproject的解决方案... - user673592
我尝试了你的设置,它在项目根目录中创建了一个TAGS文件,但是当我在树中更深的文件上执行M-.时,它会抱怨没有TAGS文件。如果我在那个目录中创建一个TAGS文件,它就可以工作了,但对于每个唯一的标签,它会给出2个匹配项(可能是因为有2个TAGS文件?)。所以我要么得到0个匹配项,要么得到2个匹配项,但无法正常工作...有什么想法是什么原因吗? - user673592
作为 etags-select 的作者,我可以建议您使用 etags-table。我真的应该给这些东西一个自己的 GitHub 页面... - scottfrazer
将我的评论移动到答案中。 - Cezar Halmagean

2

好的,我找到了一个折中的解决方案:

;; 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>"))

首先将find-tag绑定到一个永远不会使用的虚拟绑定中(这一步是必需的,以避免无限循环)。然后将M-.绑定到这个新绑定+<return>

有些丑陋,但是有效...如果有更好的答案(包括处理原始问题中描述的失败搜索),我会保持问题开放。


可以用!很烦恼的是emacs再次询问标签。太好了! - swdev

0

这是一个稍微修改过的版本,它可以加载依赖的 gem(在 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")))

0

Emacs 25 默认支持此功能。按下 M-.xref-find-definitions)跳转到定义处,按下 M-,xref-pop-marker-stack)则返回。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接