Emacs组织模式:文件的文本引用:行

dan*_*ast 8 lisp emacs bookmarks org-mode

我用org-modeEmacs来记录我的开发活动.我必须不断手动完成的任务之一是描述代码区域.Emacs有一个非常好的书签列表:创建一个书签CTRL- x r m与列举了CTRL- x r l.这非常有用,但不是我需要的.

组织模式具有链接的概念,该命令org-store-link将记录任何文件中当前位置的链接,该链接可以粘贴到组织文件.这个问题有两方面:

  • 它存储为组织链接,链接位置不直接可见(仅描述).
  • 它以格式存储file/search,这不是我想要的.

我需要以文本形式提供书签,以便我可以将其粘贴到org-mode中,如果需要,可以使用以下简单格式结束编辑:

absolute-file-path:line
Run Code Online (Sandbox Code Playgroud)

这必须从当前的位置获得.工作流程非常简单:

  • 转到我要记录的位置
  • 调用函数:( position-to-kill-ring我将它绑定到键盘快捷键)
  • 转到org-mode缓冲区.
  • Yank的位置.
  • 根据需要进行编辑(有时我需要通过相对路径更改绝对路径,因为我的代码位于不同机器的不同位置)

不幸的是我lisp不存在,所以我不知道该怎么做.我的问题有一个简单的解决方案吗?

phi*_*ils 12

(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)))))
Run Code Online (Sandbox Code Playgroud)

  • 格式必须是"%s ::%d",即有两个冒号 (2认同)

bzg*_*bzg 6

你想使用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)))))
Run Code Online (Sandbox Code Playgroud)

然后M-x org-store-link RET将做正确的事情(存储行号作为搜索字符串)和C-c C-o(即M-x org-open-at-point RET)将打开文件并转到此行号.

您当然可以检查其他模式和/或条件.