2012-05-21 46 views
8

我使用org-modeEmacs中記錄我的開發活動。我必須手工完成的任務之一是描述代碼區域。 Emacs有一個非常好的Bookmark List:創建書籤與CTRL - X[R 米,與CTRL一一列舉 - X[R 升。這是非常有用的,但不是我所需要的。Emacs組織模式:對文件的文本引用:行

組織模式具有鏈接的概念,命令org-store-link將記錄任何文件中當前位置的鏈接,該鏈接可以粘貼到組織文件中。這個問題是雙重的:

  • 它作爲一個組織鏈接存儲,並且鏈接的位置不是直接可見的(只是描述)。
  • 它存儲在格式file/search,這不是我想要的。

我需要以文本形式的書籤,這樣我就可以把它複製粘貼到組織模式,最終編輯它,如果需要的話,用一個簡單的格式是這樣的:

absolute-file-path:line 

這必須從當前點位置獲得。工作流是簡單:

  • 去,我想記錄
  • 調用一個函數的位置:position-to-kill-ring(我會結合這個鍵盤快捷鍵)
  • 轉到org-mode緩衝區。
  • 拉開位置。如果
  • 編輯需要(有時我需要通過相對路徑來改變絕對路徑,因爲我的代碼是在不同的機器上的不同位置)

不幸的是我的lisp是不存在的,所以我不知道如何去做這個。有沒有簡單的解決我的問題?

回答

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

格式必須爲'「%s ::%d」',即有兩個冒號 –

0

的elisp的初學者,我自己,我雖然它看作是一個很好的鍛鍊等瞧:

編輯:重寫了它使用的格式梅索德,但我仍然認爲不能將其存儲到殺環是在我的工作流程中侵入性較小(不瞭解你)。此外,我還添加了添加列位置的功能。

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

未經過廣泛測試,但爲我工作。剛剛點擊存儲文件和線存儲文件行和列來存儲當前位置和插入文件參考插入存儲的值在點。

5

您想要使用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會做正確的事(存儲行號作爲搜索字符串)和抄送Co(即)將打開該文件並轉到該行號。

你當然可以檢查其他模式和/或條件。

0

順便說一句,如果你想要比FILE:LINE更好的東西,你可以嘗試使用add-log-current-defun(在add-log.el中),它應該返回當前函數的名字。