【问题标题】:How to turn filenames with line numbers into hyperlinks?如何将带有行号的文件名转换为超链接?
【发布时间】:2014-08-17 13:05:05
【问题描述】:

我是新手 emacs 用户,目前我正在尝试为 python 设置工作环境。我正在使用rope,但遇到了以下问题:虽然rope的“查找事件”命令工作正常,但它的结果被放在一个无模式的缓冲区中,要访问它们我必须复制文件名。

缓冲区内容为here

据我所知,编译模式提供了关闭功能到我想要的功能(即,在单击或按 RET 后在给定行上打开文件)。然而,事实上,启用编译模式只会导致文件名突出显示。

如果我没有正确理解,要处理行,我需要将项目提供到 compiler-error-regexp-alist 中,就像在以下 sn-p 中所做的那样(来自 emacs wiki

(require 'compile)

(let ((symbol  'compilation-ledger)
      (pattern '("^Error: \"\\([^\"\n]+?\\)\", line \\([0-9]+\\):" 1 2)))
  (cond ((eval-when-compile (boundp 'compilation-error-regexp-systems-list))
         ;; xemacs21
         (add-to-list 'compilation-error-regexp-alist-alist
                      (list symbol pattern))
         (compilation-build-compilation-error-regexp-alist))
        ((eval-when-compile (boundp 'compilation-error-regexp-alist-alist))
         ;; emacs22 up
         (add-to-list 'compilation-error-regexp-alist symbol)
         (add-to-list 'compilation-error-regexp-alist-alist
                      (cons symbol pattern)))
        (t
         ;; emacs21
         (add-to-list 'compilation-error-regexp-alist pattern))))

我应该如何修改它以使其与我的缓冲区一起使用?

有更好/更快的选择吗?

【问题讨论】:

  • find-file-at-point 可能会给你你想要的功能。我不使用 python,但你确定rope 将东西放在无模式缓冲区中吗?转到该缓冲区并使用 M-: major-mode 检查 - 一旦你得到了,你可以在该模式下将密钥绑定到 find-file-at-point

标签: emacs elisp


【解决方案1】:

通常,当文件名显示在缓冲区中时,打开文件的最快方法是

M-x ffap

M-x find-file-at-point的缩写)

如果你想自动打开文件,你可以定义你自己的函数:

(defun open-file-at-point ()
  (interactive)
  (let ((file (ffap-file-at-point)))
    (if file
        (find-file file)
      (error "No file at point"))))

也许将它绑定到一个键

(global-set-key (kbd "C-<return>") 'open-file-at-point)

如果您想使用compilation-mode,则必须将匹配的正则表达式添加到compilation-error-regexp-alist(-alist)。对于您的示例,以下似乎有效:

(add-to-list
 'compilation-error-regexp-alist
 'python-file-name)

(add-to-list
 'compilation-error-regexp-alist-alist
 (list
  'python-file-name
  (concat "\\(?1:.*?\\)"              ;; file name
          " : "                       ;; seperator
          "\\(?2:[[:digit:]]+\\)")    ;; line number
   1 2)) ;; subexpr 1 is the file name, subexp 2 is the line number

【讨论】:

  • 非常感谢!这解决了我在这个特殊情况下遇到的所有问题。虽然ffap 我们很有用,(而且我以前不知道),但访问编译模式现在可以为我的缓冲区做的给定行上的文件更是如此。没有更多的 M-g g )))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 2011-07-10
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多