Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elisp function returning mark instead of the right value

Tags:

emacs

lisp

elisp

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.

like image 277
Paul Nathan Avatar asked Jan 23 '23 21:01

Paul Nathan


2 Answers

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

like image 55
huaiyuan Avatar answered Jan 25 '23 10:01

huaiyuan


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
    )
  )
like image 43
PP. Avatar answered Jan 25 '23 11:01

PP.