tags:

views:

108

answers:

2

I'm writing a routine to test to see if point is at the practical end of line.

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

The problem is, when running it, I see "mark set" in the minibuffer window, instead of T or nil.

+1  A: 

There is a built-in function called eolp. (edit: but this wasn't what you were trying to achieve, was it..)

Here is my version of the function (although you will have to test it more thoroughly than I did):


(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
    )
  )
PP
I misread eolp to only return T if it was at the final character of the line.
Paul Nathan
Obviously `eolp` doesn't do what you were intending, which was to return t if the point was in trailing whitespace. I've added example code which seems to do what you want without needed to save-excursion - it does modify the point however I restore it when I'm finished.
PP
You might have to correct for edge cases - e.g. what if the point is on the last character in a line and it's not whitespace?
PP
Sorry to whomever edited my answer but if you don't like my line formatting then too bad; I prefer it this way myself.
PP
@PP: huaiyuan answered a really good answer I believe.
Paul Nathan
+7  A: 

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

huaiyuan
This is really the function I was looking for, but I didn't find it in the emacs documentation.
Paul Nathan
Oh this is really good! +1
PP