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

问题描述 投票:8回答:5

我在org-mode使用Emacs来记录我的开发活动。我必须不断手动完成的任务之一是描述代码区域。 Emacs有一个非常好的Bookmark List:用CTRL-x r m创建一个书签,用CTRL-x r l列出它们。这非常有用,但不是我需要的。

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

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

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

absolute-file-path:line

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

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

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

emacs lisp org-mode bookmarks
5个回答
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)))))

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)))))

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

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


0
投票

一个elisp初学者自己,虽然它是一个很好的锻炼和瞧:

编辑:使用格式methode重写它,但我仍然认为不将它存储到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-line或store-file-line-and-col来存储当前位置和insert-file-reference以在点处插入存储的值。


0
投票

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


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)
         ))))

此func生成带有函数名称的链接作为描述。打开两个窗口,一个是组织文件,另一个是src代码。然后M-x insert-org-link-to-func RET

© www.soinside.com 2019 - 2024. All rights reserved.