Emacs org-mode:文件行的文本引用

9
我正在使用Emacs中的org-mode来记录我的开发活动。其中一个我必须手动持续完成的任务是描述代码区域。Emacs有一个非常好的书签列表:用CTRL-xrm创建一个书签,用CTRL-xrl列出它们。这非常有用,但不完全符合我的需求。
Org-mode有链接的概念,命令org-store-link将记录任何文件中当前位置的链接,可以将其粘贴到org文件中。这个问题有两个方面:
  • 它被存储为org链接,链接的位置不直接可见(只有描述)。
  • 它以file/search的格式存储,这不是我想要的。

我需要将书签以文本形式呈现,这样我就可以将其复制粘贴到org-mode中,并进行必要的编辑,格式简单如下:

absolute-file-path:line

这必须从当前点位置获取。工作流程如下:

  • 转到要记录的位置
  • 调用函数:position-to-kill-ring(我会将其绑定到键盘快捷键)
  • 转到org-mode缓冲区。
  • 粘贴该位置。
  • 如果需要,进行编辑(有时我需要通过相对路径更改绝对路径,因为我的代码在不同机器上的位置不同)

不幸的是,我不懂lisp,所以我不知道该怎么做。是否有简单的解决方案可以解决我的问题?

5个回答

14
(defun position-to-kill-ring ()
  "Copy to the kill ring a string in the format \"file-name:line-number\"
for the current buffer's file name, and the line number at point."
  (interactive)
  (kill-new
   (format "%s:%d" (buffer-file-name) (save-restriction
                                        (widen) (line-number-at-pos)))))

2
格式必须为"%s::%d",即两个冒号。 - Marlon León

7
你希望使用org-create-file-search-functionsorg-execute-file-search-functions钩子。
例如,如果你需要在文本模式文件中执行所述搜索,请使用以下代码:
(add-hook 'org-create-file-search-functions
      '(lambda ()
         (when (eq major-mode 'text-mode)
           (number-to-string (line-number-at-pos)))))

(add-hook 'org-execute-file-search-functions
      '(lambda (search-string)
         (when (eq major-mode 'text-mode)
           (goto-line (string-to-number search-string)))))

然后执行M-x org-store-link RET将会完成正确的操作(存储行号作为搜索字符串),执行C-c C-o(即M-x org-open-at-point RET)将打开文件并跳转到此行。

你当然可以检查其他模式和/或条件。


0
;; Insert a org link to the function in the next window
(defun insert-org-link-to-func ()
  (interactive)
  (insert (with-current-buffer (window-buffer (next-window))
        (org-make-link-string
         (concat "file:" (buffer-file-name)
             "::" (number-to-string (line-number-at-pos)))
         (which-function)
         ))))

这个函数生成一个链接,链接的描述是函数名。 打开两个窗口,一个是org文件,另一个是源代码。 然后输入M-x insert-org-link-to-func RET


0

作为一个elisp初学者,我认为这是一个很好的练习,于是就有了这个:

编辑:使用格式方法重写了它,但我仍然认为不将其存储到kill-ring中对我的工作流程更少干扰(不知道你呢)。此外,我还添加了添加列位置的功能。

(defvar current-file-reference ""  "Global variable to store the current file reference")

(defun store-file-line-and-col ()
  "Stores the current file, line and column point is at in a string in format \"file-name:line-number-column-number\". Insert the string using \"insert-file-reference\"."
  (interactive)
  (setq current-file-reference (format "%s:%d:%d" (buffer-file-name) (line-number-at-pos) (current-column))))
(defun store-file-and-line ()
  "Stores the current file and line oint is at in a string in format \"file-name:line-number\". Insert the string using \"insert-file-reference\"."
  (interactive)
 (setq current-file-reference (format "%s:%d" (buffer-file-name) (line-number-at-pos))))

(defun insert-file-reference ()
  "Inserts the value stored for current-file-reference at point."
  (interactive)
  (if (string= "" current-file-reference)
      (message "No current file/line/column set!")
    (insert current-file-reference)))

没有进行广泛测试,但适用于我。只需点击store-file-and-linestore-file-line-and-col以存储当前位置,并使用insert-file-reference在指定位置插入已存储的值。


0

顺便说一下,如果你想要比FILE:LINE更好的东西,可以尝试使用add-log-current-defun(在add-log.el中),它应该会返回当前函数的名称。


在Python中,这会给出一个带点的函数名,例如class.method.subfunction。 - George Moutsopoulos

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