![如何在运行的 emacs 中打开特定行的文件?](https://linux22.com/image/202691/%E5%A6%82%E4%BD%95%E5%9C%A8%E8%BF%90%E8%A1%8C%E7%9A%84%20emacs%20%E4%B8%AD%E6%89%93%E5%BC%80%E7%89%B9%E5%AE%9A%E8%A1%8C%E7%9A%84%E6%96%87%E4%BB%B6%EF%BC%9F.png)
可以从命令行运行 emacs,使用命令行参数打开第 n 行的文件,+n
如下所示:
$ emacs +n file
我想通过find-file
或其他方式从正在运行的 emacs 实例执行相同的操作。那可能吗 ?
答案1
您可以编写自己的函数:
(defun find-file-at-line (file line)
"Open FILE on LINE."
(interactive "fFile: \nNLine: \n")
(find-file file)
(goto-line line))
答案2
找到了解决方案emacs 维基这将增强 ffap 也选择行号并在找到文件后转到该文件号。
;
; have ffap pick up line number and goto-line
; found on emacswiki : https://www.emacswiki.org/emacs/FindFileAtPoint#h5o-6
;
(defvar ffap-file-at-point-line-number nil
"Variable to hold line number from the last `ffap-file-at-point' call.")
(defadvice ffap-file-at-point (after ffap-store-line-number activate)
"Search `ffap-string-at-point' for a line number pattern and
save it in `ffap-file-at-point-line-number' variable."
(let* ((string (ffap-string-at-point)) ;; string/name definition copied from `ffap-string-at-point'
(name
(or (condition-case nil
(and (not (string-match "//" string)) ; foo.com://bar
(substitute-in-file-name string))
(error nil))
string))
(line-number-string
(and (string-match ":[0-9]+" name)
(substring name (1+ (match-beginning 0)) (match-end 0))))
(line-number
(and line-number-string
(string-to-number line-number-string))))
(if (and line-number (> line-number 0))
(setq ffap-file-at-point-line-number line-number)
(setq ffap-file-at-point-line-number nil))))
(defadvice find-file-at-point (after ffap-goto-line-number activate)
"If `ffap-file-at-point-line-number' is non-nil goto this line."
(when ffap-file-at-point-line-number
(goto-line ffap-file-at-point-line-number)
(setq ffap-file-at-point-line-number nil)))