* Function-Problem
@ 2016-08-17 10:49 Klaus Jantzen
2016-08-17 13:27 ` Function-Problem tomas
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: Klaus Jantzen @ 2016-08-17 10:49 UTC (permalink / raw)
To: emacs-list
Hello,
the attached function should return true if a line contains all blanks
or is empty.
Currently the function returns a quick 'nil' and then the message
"No catch for tag: --cl-block-nil--, nil"
What is my problem?
=====
(defun blank-line-p ()
"Returns t if line contains all blanks or is empty"
(let ((lep (line-end-position))
(cpp (point)) ; the current position
(res 0)
)
(beginning-of-line)
(if (re-search-forward "^[ ]+$\|^$" lep t)
(progn (goto-char cpp) ; line is blank/empty
(message "t")
t)
(progn (goto-char cpp) ; line is not blank/empty
(message "nil")
(return nil))
)
) ; end of let
) ; end of 'blank-line-p'
====
Thanks for any hint.
--
K.D.J.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Function-Problem
2016-08-17 10:49 Function-Problem Klaus Jantzen
@ 2016-08-17 13:27 ` tomas
2016-08-17 14:38 ` Function-Problem Drew Adams
2016-08-17 13:59 ` Function-Problem Óscar Fuentes
2016-08-17 17:06 ` Function-Problem Klaus Jantzen
2 siblings, 1 reply; 8+ messages in thread
From: tomas @ 2016-08-17 13:27 UTC (permalink / raw)
To: Klaus Jantzen; +Cc: emacs-list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Wed, Aug 17, 2016 at 12:49:11PM +0200, Klaus Jantzen wrote:
> Hello,
>
> the attached function should return true if a line contains all blanks
> or is empty.
>
> Currently the function returns a quick 'nil' and then the message
>
> "No catch for tag: --cl-block-nil--, nil"
>
> What is my problem?
>
> =====
> (defun blank-line-p ()
> "Returns t if line contains all blanks or is empty"
> (let ((lep (line-end-position))
> (cpp (point)) ; the current position
> (res 0)
> )
> (beginning-of-line)
> (if (re-search-forward "^[ ]+$\|^$" lep t)
> (progn (goto-char cpp) ; line is blank/empty
> (message "t")
> t)
> (progn (goto-char cpp) ; line is not blank/empty
> (message "nil")
> (return nil))
^^^^^^
I guess you just want to say `nil' there instead of `(return nil)'
(return is a wrapper around a CL exception which you haven't caught
"upstairs", thus the funny message).
> )
> ) ; end of let
> ) ; end of 'blank-line-p'
> ====
> Thanks for any hint.
Note that you can have it much easier. Cf `looking-at', e.g.
(beginning-of-line)
(looking-at "^[ ]*$")
might be roughly equivalent to your code snippet above.
Perhaps you want to use "save-excursion" if you don't want your
function to leave point at a different position it started:
(defun blank-line-p ()
(save-excursion
(beginning-of-line)
(looking-at "^[ ]*$")))
You might also want to refine the regexp to include other
whitespace characters besides of " " (e.g. the POSIX class
[:space:] or some such).
Regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
iEYEARECAAYFAle0ZlwACgkQBcgs9XrR2kZ8bwCfbUcZrZtjghtR38bOsGZi/8v1
JQcAn3kanTGRUvVCriIJz+RBYqhByEN0
=nw7B
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Function-Problem
2016-08-17 10:49 Function-Problem Klaus Jantzen
2016-08-17 13:27 ` Function-Problem tomas
@ 2016-08-17 13:59 ` Óscar Fuentes
2016-08-17 17:06 ` Function-Problem Klaus Jantzen
2 siblings, 0 replies; 8+ messages in thread
From: Óscar Fuentes @ 2016-08-17 13:59 UTC (permalink / raw)
To: help-gnu-emacs
Klaus Jantzen <k.d.jantzen@mailbox.org> writes:
> Hello,
>
> the attached function should return true if a line contains all blanks
> or is empty.
>
> Currently the function returns a quick 'nil' and then the message
>
> "No catch for tag: --cl-block-nil--, nil"
>
> What is my problem?
>
> =====
> (defun blank-line-p ()
> "Returns t if line contains all blanks or is empty"
> (let ((lep (line-end-position))
> (cpp (point)) ; the current position
Look into `save-excursion'.
> (res 0)
> )
> (beginning-of-line)
> (if (re-search-forward "^[ ]+$\|^$" lep t)
You need to escape the `\' above: "^[ ]+$\\|^$". Also, instead of using
`+' you could use `*' which matches zero or more instances and just use
"^[ ]*$".
> (progn (goto-char cpp) ; line is blank/empty
> (message "t")
> t)
> (progn (goto-char cpp) ; line is not blank/empty
> (message "nil")
> (return nil))
`return' does not belong here. It is a Common Lisp extension. Just say
`nil' the same way you say `t' at the end of the previous `progn'.
> )
> ) ; end of let
> ) ; end of 'blank-line-p'
> ====
> Thanks for any hint.
HTH.
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: Function-Problem
2016-08-17 13:27 ` Function-Problem tomas
@ 2016-08-17 14:38 ` Drew Adams
2016-08-17 14:49 ` Function-Problem tomas
0 siblings, 1 reply; 8+ messages in thread
From: Drew Adams @ 2016-08-17 14:38 UTC (permalink / raw)
To: tomas, Klaus Jantzen; +Cc: emacs-list
> Note that you can have it much easier. Cf `looking-at', e.g.
> (beginning-of-line)
> (looking-at "^[ ]*$")
`looking-at-p', if you are not interested in the match data.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Function-Problem
2016-08-17 14:38 ` Function-Problem Drew Adams
@ 2016-08-17 14:49 ` tomas
0 siblings, 0 replies; 8+ messages in thread
From: tomas @ 2016-08-17 14:49 UTC (permalink / raw)
To: Drew Adams; +Cc: emacs-list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Wed, Aug 17, 2016 at 07:38:33AM -0700, Drew Adams wrote:
> > Note that you can have it much easier. Cf `looking-at', e.g.
> > (beginning-of-line)
> > (looking-at "^[ ]*$")
>
> `looking-at-p', if you are not interested in the match data.
Yes, thanks :-)
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
iEYEARECAAYFAle0eXgACgkQBcgs9XrR2kbcPACcDBMtq/e1PeoCNv4i/nMTx+8p
jDsAn0csH/H193KHIUf7O46uBNtvvkUq
=qc9W
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Function-Problem
2016-08-17 10:49 Function-Problem Klaus Jantzen
2016-08-17 13:27 ` Function-Problem tomas
2016-08-17 13:59 ` Function-Problem Óscar Fuentes
@ 2016-08-17 17:06 ` Klaus Jantzen
2016-08-17 17:33 ` Function-Problem John Mastro
2 siblings, 1 reply; 8+ messages in thread
From: Klaus Jantzen @ 2016-08-17 17:06 UTC (permalink / raw)
To: emacs-list
On 08/17/2016 12:49 PM, Klaus Jantzen wrote:
> Hello,
>
> the attached function should return true if a line contains all blanks
> or is empty.
>
> Currently the function returns a quick 'nil' and then the message
>
> "No catch for tag: --cl-block-nil--, nil"
>
> What is my problem?
>
> =====
> (defun blank-line-p ()
> "Returns t if line contains all blanks or is empty"
> (let ((lep (line-end-position))
> (cpp (point)) ; the current position
> (res 0)
> )
> (beginning-of-line)
> (if (re-search-forward "^[ ]+$\|^$" lep t)
> (progn (goto-char cpp) ; line is blank/empty
> (message "t")
> t)
> (progn (goto-char cpp) ; line is not blank/empty
> (message "nil")
> (return nil))
> )
> ) ; end of let
> ) ; end of 'blank-line-p'
> ====
> Thanks for any hint.
Based on your suggestions, I rewrote the function as follows:
=====
(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'?
--
K.D.J.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Function-Problem
2016-08-17 17:06 ` Function-Problem Klaus Jantzen
@ 2016-08-17 17:33 ` John Mastro
2016-08-17 18:07 ` Function-Problem tomas
0 siblings, 1 reply; 8+ messages in thread
From: John Mastro @ 2016-08-17 17:33 UTC (permalink / raw)
To: emacs-list
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Function-Problem
2016-08-17 17:33 ` Function-Problem John Mastro
@ 2016-08-17 18:07 ` tomas
0 siblings, 0 replies; 8+ messages in thread
From: tomas @ 2016-08-17 18:07 UTC (permalink / raw)
To: John Mastro; +Cc: emacs-list
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Wed, Aug 17, 2016 at 10:33:19AM -0700, John Mastro wrote:
> 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 *.
[rest elided]
I concur. The regular expression "^[ ]+$\|^$" is just a roundabout way
to spell "^[ ]*$" (i.e. say "zero or more" instead of "one or more OR zero").
And a POSIX class makes it more general (or just leave out the [] if you
really mean only the space char). But...
the expression at (1) still should work. Why it doesn't?
Well, the escape char before the \ is seen by the string reader and
disappears! To pass one escape to the regexp you have to double it.
The correct spelling for (1) would be:
(looking-at-p "^[ ]+$\\|^$")
Note the double backslassh: one for the string syntax, one for the
regexp.
This is a very common trap. Especially because you don't need this
doubling in interactive input.
Regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
iEYEARECAAYFAle0p98ACgkQBcgs9XrR2kZvLQCfZelDtIAOx/0AWiGDvKuklrQ+
GJUAmwQFnHcEmlllr+ROR9uT+EEhlG50
=ur24
-----END PGP SIGNATURE-----
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2016-08-17 18:07 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-17 10:49 Function-Problem Klaus Jantzen
2016-08-17 13:27 ` Function-Problem tomas
2016-08-17 14:38 ` Function-Problem Drew Adams
2016-08-17 14:49 ` Function-Problem tomas
2016-08-17 13:59 ` Function-Problem Óscar Fuentes
2016-08-17 17:06 ` Function-Problem Klaus Jantzen
2016-08-17 17:33 ` Function-Problem John Mastro
2016-08-17 18:07 ` Function-Problem tomas
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.