2009-12-08 70 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