2009-12-08 66 views
4

我正在编写一个例程来测试以查看点是否在实际行结束处。Elisp函数返回标记而不是正确的值

(defun end-of-line-p() 
    "T if there is only \w* between point and end of line" 
    (interactive) 
    (save-excursion 
    (set-mark-command nil)  ;mark where we are 
    (move-end-of-line nil)  ;move to the end of the line 
    (let ((str (buffer-substring (mark) (point)))) ;; does any non-ws text exist in the region? return false 
     (if (string-match-p "\W*" str) 
     t 
    nil)))) 

问题是,运行它时,我在minibuffer窗口中看到“标记集”,而不是T或nil。

+5

的Emacs Lisp编程提示,如果你看的文档字符串'设置标记command'的底部,你会看到: 新手的Emacs Lisp程序员经常尝试使用大关错误的目的。有关更多信息,请参阅“set-mark”的文档。 – 2009-12-08 18:32:08

回答

1

有一个内置函数eolp。 (编辑:但是这不是你想要实现的,是吧..)

这里是我的版本的功能(尽管你将有更彻底地比我测试):


(defun end-of-line-p() 
    "true if there is only [ \t] between point and end of line" 
    (interactive) 
    (let (
     (point-initial (point)) ; save point for returning 
     (result t) 
     ) 
    (move-end-of-line nil) ; move point to end of line 
    (skip-chars-backward " \t" (point-min)) ; skip backwards over whitespace 
    (if (> (point) point-initial) 
     (setq result nil) 
    ) 
    (goto-char point-initial) ; restore where we were 
    result 
    ) 
) 
+0

我错误的eolp只返回T,如果它在行的最后一个字符。 – 2009-12-08 18:25:46

+0

很明显'eolp'不会做你想要的,如果这个点在尾随的空白处,就会返回t。我已经添加了示例代码,它似乎可以完成你想要的功能,而无需保存 - 游览 - 它确实修改了点,但是当我完成时我还原了它。 – 2009-12-08 18:33:03

+0

您可能必须纠正边缘情况 - 例如如果重点是在一行中的最后一个字符而不是空格? – 2009-12-08 18:36:32

8

(looking-at-p "\\s-*$")

+0

这真的是我正在寻找的功能,但我没有在emacs文档中找到它。 – 2009-12-08 21:42:54

+0

哦,这真的很好! +1 – 2009-12-09 00:36:02