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

Emacs组织模式:对文件的文本引用:行,emacs,lisp,org-mode,bookmarks,Emacs,Lisp,Org Mode,Bookmarks,我正在Emacs中使用org模式来记录我的开发活动。我必须不断手动完成的任务之一是描述代码区域Emacs有一个非常好的方法:用CTRL-XRM创建书签,用CTRL-XRL列出它们。这是非常有用的,但不是我所需要的 组织模式有链接的概念,命令Org-store-link将在任何文件中记录到当前位置的链接,该链接可以粘贴到组织文件中。这有两个问题: 它存储为组织链接,链接位置不直接可见(仅显示描述) 它以文件/search的格式存储,这不是我想要的 我需要有文本形式的书签,以便我可以复制粘贴到组

我正在
Emacs
中使用
org模式
来记录我的开发活动。我必须不断手动完成的任务之一是描述代码区域
Emacs
有一个非常好的方法:用CTRL-XRM创建书签,用CTRL-XRL列出它们。这是非常有用的,但不是我所需要的

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

  • 它存储为组织链接,链接位置不直接可见(仅显示描述)
  • 它以
    文件/search
    的格式存储,这不是我想要的
我需要有文本形式的书签,以便我可以复制粘贴到组织模式,如果需要,结束编辑,使用如下简单格式:

absolute-file-path:line
这必须从当前点位置获得。工作流程将非常简单,如下所示:

  • 去我想记录的位置
  • 调用函数:
    positiontokill ring
    (我会将其绑定到键盘快捷键)
  • 转到
    组织模式
    缓冲区
  • 猛拉这个位置
  • 根据需要编辑(有时我需要按相对路径更改绝对路径,因为我的代码位于不同机器的不同位置)
不幸的是,我的
lisp
不存在,因此我不知道如何做到这一点。我的问题有一个简单的解决方案吗?

(取消压井环的位置()
“将格式为\“文件名:行号\”的字符串复制到杀死环”
用于当前缓冲区的文件名和点处的行号。“
(互动)
(杀死新的
(格式“%s:%d”(缓冲区文件名)(保存限制)
(加宽)(位置(()()))处的行号)

作为一名elisp初学者,我认为这是一个很好的练习,瞧:

编辑:使用format 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)))

没有广泛测试,但为我工作。只需点击存储文件和行存储文件行和列即可存储当前位置,点击插入文件引用即可在点插入存储值。

您要使用
组织创建文件搜索功能
组织执行文件搜索功能
挂钩

例如,如果需要对文本模式文件进行描述的搜索,请使用以下命令:

(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 C-o(即
M-x org open at point RET
)将打开文件并转到该行号


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

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

;; 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生成以函数名作为描述的链接。 打开两个窗口,一个是org文件,另一个是src代码。
然后
M-x插入组织链接到func-RET

格式必须是
%s::%d“
,即,对于两个colonsIn python,这会给出一个虚线函数名,例如class.method.subfunction。