help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function-Problem


From: John Mastro
Subject: Re: Function-Problem
Date: Wed, 17 Aug 2016 10:33:19 -0700

Klaus Jantzen <k.d.jantzen@mailbox.org> wrote:
> (defun blank-line-p ()
>   "Returns t if line contains only blanks or empty"
>   (save-excursion
>     (beginning-of-line)
>     ; (if (looking-at-p "^[ ]+$\|^$") ; (1)
>     ; (if (looking-at-p "^[ ]+$")   ; (2)
>     ; (if (looking-at-p "^$")       ; (3)
>     (if (looking-at-p "^$\|^[ ]+$") ; (1a)
>         (progn ; (goto-char cpp) ; line is blank/empty
>                (message "t")
>                t)
>         (progn ; (goto-char cpp) ; line is not blank/empty
>                (message "nil")
>                nil)
>         )
>     )
>   ) ; end of 'blank-line-p'
>
> =====
>
> This leads to a problem with the regular expression;
>
> if-(3) works, if-(2) works, but the combined regular expression in
> if-(1) or if-(1a) does not work.
>
> Where is my error in the RE? Is the combination not allowed in
> 'lookig-at-p'?

This should do the trick: (looking-at-p "^[[:blank:]]*$")

The problem is that the + metacharacter means "one or more", whereas you
want to express "zero or more" spaces. The metacharacter for that is *.

I used the [:blank:] class because it seems to match what you're trying
to express, but of course you could replace [[:blank:]] with [ ] if you
only want to match spaces (i.e. do not want to match tabs).

The complete function would be:

(defun blank-line-p ()
  (save-excursion
    (beginning-of-line)
    (looking-at-p "^[[:blank:]]*$")))

Hope that helps

        John



reply via email to

[Prev in Thread] Current Thread [Next in Thread]