-1

我有下面的文本(用实际的制表符代替 \t),我需要在“描述”之后的制表符后面获取所有文本,直到缓冲区结束。

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

这是lisp函数:

(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

我假设在正则表达式搜索之后,该点将在 Description\t 之后。那么为什么设置变量不起作用呢?

4

1 回答 1

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为真值获得的回溯将很快揭示问题的原因。

于 2013-04-12T20:00:06.320 回答