如何在运行的 emacs 中打开特定行的文件?

如何在运行的 emacs 中打开特定行的文件?

可以从命令行运行 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)))

相关内容