Why does this function print non-zero integers on non-empty lines?
;; taken from https://emacs.stackexchange.com/a/16826/10896
(defun current-line-empty-p ()
(interactive)
(setq answer (string-match-p "^\\s-*$" (thing-at-point 'line)))
(message "%s" answer)
answer)
I would like the answer to be displayed as t
or nil
. But on a non-empty line, it prints 35 or some other non-zero integer depending on which line I called it via M-x
. On blank lines it prints 0.
Sure non-zero means false and zero means true but I am curious why this behaviour happens? I would have thought string-match-p
already returned booleans.
string-match
which is likestring-match-p
but does not change the match data.string-match
returns the index of the first match of the regexp or nil if the regexp did not match at all. – Tobias Nov 23 '18 at 01:39C-h f <function>
). Saying "I would have thoughtstring-match-p
already returned booleans." is silly when the function tells you that it works likestring-match
and that docstring explicitly tells you it returns the "index of start of first match" which is obviously not the same thing as returningt
. The documentation is really good. Ask questions here if it's still unclear, but make sure you're actually checking the documentation first and foremost. – phils Nov 23 '18 at 05:34nil
is boolean false, and anything else is boolean true. Therefore predicate functions frequently return something other thant
for true results, because that other value is potentially more useful, while still being 'true' in a boolean test. – phils Nov 23 '18 at 05:38nil
means false, and both zero and non-zero means true (provided, of course, that the non-zero is also non-nil). – phils Nov 23 '18 at 05:43string-blank-p
from subr-x.el, i.e.,(string-blank-p (buffer-substring (line-beginning-position) (line-end-position)))
. – xuchunyang Nov 23 '18 at 07:07