2013-04-12 55 views
-1

我有下面的文本(與實際的選項卡中,而不是\ t),我需要在「描述」後面的選項卡後,並獲取所有文本直到緩衝區結束。elisp錯誤的類型參數:整數或標記-p錯誤

key1\tval1  
key2\tval2 
key3\tval3  
Description\tlots and lots and lots and lots and lots lots 
and lots and lots and lots and lots lots and lots and lots and 
lots and lots lots and lots and lots and lots and lots lots and 

lots lots and lots and lots and lots and lots lots and lots lots 

and lots and lots and lots and lots lots and lots lots and lots 

and lots and lots and lots lots and lots lots and lots and lots 
and lots and lots lots and lots lots and lots and lots and lots 
and lots lots and lots lots and lots and lots and lots and lots 

這裏是口齒不清功能:

(defun find-description() 
    (interactive) 
    (goto-char (point-min)) 
    (when (re-search-forward "Description\t") 
    (setq myStr (buffer-substring (point) (end-of-line))) 
    (goto-char (point-max)) 
    (insert "\n\n\ndescription=") 
    (insert myStr) 
    ) 
) 

而這種失敗的(setq符合錯誤:

Wrong type argument: integer-or-marker-p, nil 

我認爲正則表達式搜索後,該點會剛剛在說明\ t之後,爲什麼不設置變量的工作?

+2

'行結束符* *移動*到行尾而不是返回它。你想使用'line-end-position'代替。 – Stefan

回答

3

end-of-line不是標記或位置,因此試圖在buffer-substring中使用該標記或位置是導致錯誤消息的原因。簡單的解決方法是在移動到行結束後獲取緩衝區位置;

(let ((beg (point)) 
    (end-of-line) 
    (setq myStr (buffer-substring beg (point))) 

還要注意https://stackoverflow.com/a/15974319/874188其中指出line-end-position作爲這個更簡單的修復。

你也可以重構這個以避免臨時變量例如通過搜索"Description\t\([^\n]*\)"並拉出匹配的子字符串,但我想任何一種方式都可以。

由此,通過將debug-on-error設置爲真值來檢查回溯會很快顯示出問題的原因。

+0

感謝您使用backtrace的提示 - 將對此進行調查。 –

+0

另外BTW,一個使用正則表達式搜索的庫函數應該將它包裝在['save-match-data']中(http://www.gnu.org/software/emacs/manual/html_node/elisp/Saving-Match-Data的.html)。 – tripleee