unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* empty-line-p
@ 2007-03-29 13:06 Andreas Roehler
  0 siblings, 0 replies; 11+ messages in thread
From: Andreas Roehler @ 2007-03-29 13:06 UTC (permalink / raw)
  To: help-gnu-emacs


Hi,

needed a check at several occassions, if the current line
contains printable characters.

What about the following to solve this?

(defun empty-line-p ()
  "Returns t if cursor is at an empty line "
  (interactive)
  (save-excursion
    (beginning-of-line)
  (if
      (looking-at "^[ \t\f\r]*$")
      t
    nil)))


__
Andreas Roehler

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
       [not found] <mailman.1577.1175177310.7795.help-gnu-emacs@gnu.org>
@ 2007-03-29 14:32 ` Joost Kremers
  2007-03-30  8:56   ` empty-line-p David Hansen
       [not found]   ` <mailman.1621.1175289074.7795.help-gnu-emacs@gnu.org>
  2007-03-30  5:36 ` empty-line-p Stefan Monnier
  1 sibling, 2 replies; 11+ messages in thread
From: Joost Kremers @ 2007-03-29 14:32 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Roehler wrote:
> needed a check at several occassions, if the current line
> contains printable characters.
>
> What about the following to solve this?
>
> (defun empty-line-p ()
>   "Returns t if cursor is at an empty line "
>   (interactive)
>   (save-excursion
>     (beginning-of-line)
>   (if
>       (looking-at "^[ \t\f\r]*$")
>       t
>     nil)))

you don't need the if-statement here:

(defun empty-line-p ()
  "Returns t if cursor is at an empty line "
  (interactive)
  (save-excursion
    (beginning-of-line)
    (looking-at "^[ \t\f\r]*$")))

looking-at already returns t or nil.

-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
       [not found] <mailman.1577.1175177310.7795.help-gnu-emacs@gnu.org>
  2007-03-29 14:32 ` empty-line-p Joost Kremers
@ 2007-03-30  5:36 ` Stefan Monnier
  2007-03-30  5:46   ` empty-line-p Barry Margolin
  2007-03-30  6:14   ` empty-line-p Andreas Roehler
  1 sibling, 2 replies; 11+ messages in thread
From: Stefan Monnier @ 2007-03-30  5:36 UTC (permalink / raw)
  To: help-gnu-emacs

>  (if (looking-at "^[ \t\f\r]*$")
>      t
>    nil)))

  (if <foo> t nil)

is an eta-regexp, which can be advantageously reduced to just <foo>.


        Stefan


PS: The classical eta-regexp is (lambda (x) (<foo> x)), which is equivalent to
just <foo>.  Another variant is (cons (car <foo>) (cdr <foo>)), which
eta-reduces to just <foo>.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-30  5:36 ` empty-line-p Stefan Monnier
@ 2007-03-30  5:46   ` Barry Margolin
  2007-04-01 20:54     ` empty-line-p Stefan Monnier
  2007-03-30  6:14   ` empty-line-p Andreas Roehler
  1 sibling, 1 reply; 11+ messages in thread
From: Barry Margolin @ 2007-03-30  5:46 UTC (permalink / raw)
  To: help-gnu-emacs

In article <jwv648j70t5.fsf-monnier+gnu.emacs.help@gnu.org>,
 Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> >  (if (looking-at "^[ \t\f\r]*$")
> >      t
> >    nil)))
> 
>   (if <foo> t nil)
> 
> is an eta-regexp, which can be advantageously reduced to just <foo>.
> 
> 
>         Stefan
> 
> 
> PS: The classical eta-regexp is (lambda (x) (<foo> x)), which is equivalent to
> just <foo>.  Another variant is (cons (car <foo>) (cdr <foo>)), which
> eta-reduces to just <foo>.

That last one is different, because the new cons is not eq to the 
original <foo>.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-30  5:36 ` empty-line-p Stefan Monnier
  2007-03-30  5:46   ` empty-line-p Barry Margolin
@ 2007-03-30  6:14   ` Andreas Roehler
  1 sibling, 0 replies; 11+ messages in thread
From: Andreas Roehler @ 2007-03-30  6:14 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs, Joost Kremers

Stefan Monnier schrieb:
>>  (if (looking-at "^[ \t\f\r]*$")
>>      t
>>    nil)))
>>     
>
>   (if <foo> t nil)
>
> is an eta-regexp, which can be advantageously reduced to just <foo>.
>
>
>         Stefan
>
>
> PS: The classical eta-regexp is (lambda (x) (<foo> x)), which is equivalent to
> just <foo>.  Another variant is (cons (car <foo>) (cdr <foo>)), which
> eta-reduces to just <foo>.
>   

Thanks a lot both.

Rewrote it in order to let the user decide, which chars
should be considered as "empty".

Also provided an interactive spec.

__
Andreas Roehler


(defcustom empty-line-p-chars "^[ \t\f\r]*$"
  "Permitted chars on a line,
which should be considered as \"empty\" nonetheless"
 :type 'regexp
 :group 'convenience)

(defun empty-line-p (&optional ispec)
  "Returns t if cursor is at an empty line, nil otherwise.
Displays result in minibuffer when called interactive."
  (interactive "p")
  (save-excursion
    (beginning-of-line)
    (when ispec
      (message "%s" (looking-at empty-line-p-chars)))
    (looking-at empty-line-p-chars)))

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-29 14:32 ` empty-line-p Joost Kremers
@ 2007-03-30  8:56   ` David Hansen
  2007-03-31  5:49     ` empty-line-p Andreas Roehler
       [not found]   ` <mailman.1621.1175289074.7795.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 11+ messages in thread
From: David Hansen @ 2007-03-30  8:56 UTC (permalink / raw)
  To: help-gnu-emacs

On 29 Mar 2007 14:32:05 GMT Joost Kremers wrote:

> Andreas Roehler wrote:
>> needed a check at several occassions, if the current line
>> contains printable characters.
>>
>> What about the following to solve this?
>>
>> (defun empty-line-p ()
>>   "Returns t if cursor is at an empty line "
>>   (interactive)
>>   (save-excursion
>>     (beginning-of-line)
>>   (if
>>       (looking-at "^[ \t\f\r]*$")
>>       t
>>     nil)))
>
> you don't need the if-statement here:
>
> (defun empty-line-p ()
>   "Returns t if cursor is at an empty line "
>   (interactive)
>   (save-excursion
>     (beginning-of-line)
>     (looking-at "^[ \t\f\r]*$")))
>
> looking-at already returns t or nil.

And to avoid some painful bug hunting (happened to me with nearly
the same code) wrap it in a `save-match-data'.

David

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-30  8:56   ` empty-line-p David Hansen
@ 2007-03-31  5:49     ` Andreas Roehler
  2007-03-31 15:17       ` empty-line-p Xavier Maillard
  0 siblings, 1 reply; 11+ messages in thread
From: Andreas Roehler @ 2007-03-31  5:49 UTC (permalink / raw)
  To: David Hansen; +Cc: help-gnu-emacs

David Hansen schrieb:
> On 29 Mar 2007 14:32:05 GMT Joost Kremers wrote:
>
>   
>> Andreas Roehler wrote:
>>     
>>> needed a check at several occassions, if the current line
>>> contains printable characters.
>>>
>>> What about the following to solve this?
>>>
>>> (defun empty-line-p ()
>>>   "Returns t if cursor is at an empty line "
>>>   (interactive)
>>>   (save-excursion
>>>     (beginning-of-line)
>>>   (if
>>>       (looking-at "^[ \t\f\r]*$")
>>>       t
>>>     nil)))
>>>       
>> you don't need the if-statement here:
>>
>> (defun empty-line-p ()
>>   "Returns t if cursor is at an empty line "
>>   (interactive)
>>   (save-excursion
>>     (beginning-of-line)
>>     (looking-at "^[ \t\f\r]*$")))
>>
>> looking-at already returns t or nil.
>>     
>
> And to avoid some painful bug hunting (happened to me with nearly
> the same code)
What about to collect minor tools like this somewhere?

>  wrap it in a `save-match-data'.
>
> David
>   

OK, thanks. So I got this now:

(defcustom empty-line-p-chars "^[ \t\f\r]*$"
  "empty-line-p-chars"
 :type 'regexp
 :group 'convenience)

(defun empty-line-p (&optional ispec)
  "Returns t if cursor is at an empty line, nil otherwise.
Displays result in minibuffer when called interactive."
  (interactive "p")
  (save-excursion
    (beginning-of-line)
    (save-match-data
      (when ispec
    (message "%s" (looking-at empty-line-p-chars)))
      (looking-at empty-line-p-chars))))


;;;;;

Andreas

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-31  5:49     ` empty-line-p Andreas Roehler
@ 2007-03-31 15:17       ` Xavier Maillard
  2007-04-06 14:44         ` empty-line-p Andreas Roehler
  0 siblings, 1 reply; 11+ messages in thread
From: Xavier Maillard @ 2007-03-31 15:17 UTC (permalink / raw)
  To: Andreas Roehler; +Cc: david.hansen, help-gnu-emacs


   >  wrap it in a `save-match-data'.
   >
   > David
   >   

   OK, thanks. So I got this now:

Here is what I would do instead:

(defun empty-line-p (&optional ispec)
  "Returns t if cursor is at an empty line, nil otherwise.
Displays result in minibuffer when called interactive."
  (interactive "p")
  (save-excursion
    (beginning-of-line)
    (save-match-data
      (let ((res (looking-at empty-line-p-chars)))
	(or (and ispec
		 (message "%s" res))
	    res)))))


Xavier

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-30  5:46   ` empty-line-p Barry Margolin
@ 2007-04-01 20:54     ` Stefan Monnier
  0 siblings, 0 replies; 11+ messages in thread
From: Stefan Monnier @ 2007-04-01 20:54 UTC (permalink / raw)
  To: help-gnu-emacs

>> >  (if (looking-at "^[ \t\f\r]*$")
>> >      t
>> >    nil)))
>> 
>> (if <foo> t nil)
>> 
>> is an eta-regexp, which can be advantageously reduced to just <foo>.
>> 
>> 
>> Stefan
>> 
>> 
>> PS: The classical eta-regexp is (lambda (x) (<foo> x)), which is equivalent to
>> just <foo>.  Another variant is (cons (car <foo>) (cdr <foo>)), which
>> eta-reduces to just <foo>.

> That last one is different, because the new cons is not eq to the 
> original <foo>.

Indeed, the eta rules are generally not supported 100% by most languages:
there are always some "minor" semantic differences.  Aside from the problem
you mentioned for the `cons' case, the `if' case changes the result when the
tested value can be non-t and non-nil, and the `lambda' case can change the
behavior when the function is called with more than 1 argument.

So most compilers don't apply such "optimizations", which makes it that much
more valuable to apply them by hand.


        Stefan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
       [not found]   ` <mailman.1621.1175289074.7795.help-gnu-emacs@gnu.org>
@ 2007-04-01 20:57     ` Stefan Monnier
  0 siblings, 0 replies; 11+ messages in thread
From: Stefan Monnier @ 2007-04-01 20:57 UTC (permalink / raw)
  To: help-gnu-emacs

> And to avoid some painful bug hunting (happened to me with nearly
> the same code) wrap it in a `save-match-data'.

I'd advise against such a change.

The cost of save-match-data is fairly high, so instead of using it
everywhere, the coding convention in elisp is to be careful that between
a regexp-match and the use of match-beginning (and friends) there should be
almost no other operation since most operations (other than the few known
safe ones) may trash the match-data.


        Stefan

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: empty-line-p
  2007-03-31 15:17       ` empty-line-p Xavier Maillard
@ 2007-04-06 14:44         ` Andreas Roehler
  0 siblings, 0 replies; 11+ messages in thread
From: Andreas Roehler @ 2007-04-06 14:44 UTC (permalink / raw)
  To: Xavier Maillard
  Cc: help-gnu-emacs, David Hansen, Barry Margolin, Stefan Monnier,
	Joost Kremers

Xavier Maillard schrieb:
>    >  wrap it in a `save-match-data'.
>    >
>    > David
>    >   
>
>    OK, thanks. So I got this now:
>
> Here is what I would do instead:
>
> (defun empty-line-p (&optional ispec)
>   "Returns t if cursor is at an empty line, nil otherwise.
> Displays result in minibuffer when called interactive."
>   (interactive "p")
>   (save-excursion
>     (beginning-of-line)
>     (save-match-data
>       (let ((res (looking-at empty-line-p-chars)))
> 	(or (and ispec
> 		 (message "%s" res))
> 	    res)))))
>
>
> Xavier
>

That would probably work, however, as far as the form
is conceived as a subroutine, it doesn't seem the best
solution.

Assume you introduced `let', because in

    (when ispec
      (message "%s" (looking-at empty-line-p-chars)))
      (looking-at empty-line-p-chars))))

`looking-at' is performed two times.

Thought that repeat won't matter, because when called
interactively, time isn't at stake.

Otherwise `let' would maybe called again and
again from inside a program, whereas `let' isn't really
needed here.

Meanwhile thought to reconcile
`save-match-data' proposed by David Hansen with Stefan
Monnier's objections concerning speed.

The form below should `save-match-data', when called as

(empty-line-p t)

and without arg not.

(defun empty-line-p (&optional arg ispec)
  "Returns t if cursor is at an empty line, nil otherwise.
Displays result in minibuffer when called interactive."
  (interactive "P\np")
  (save-excursion
    (beginning-of-line)
    (if arg
    (save-match-data
      (if ispec
          (message "%s" (looking-at empty-line-p-chars))
        (looking-at empty-line-p-chars)))
      (if ispec
      (message "%s" (looking-at empty-line-p-chars))
    (looking-at empty-line-p-chars)))))

(defcustom empty-line-p-chars "^[ \t\f\r]*$"
  "empty-line-p-chars"
 :type 'regexp
 :group 'convenience)


Thanks all

Andreas Roehler

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2007-04-06 14:44 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-29 13:06 empty-line-p Andreas Roehler
     [not found] <mailman.1577.1175177310.7795.help-gnu-emacs@gnu.org>
2007-03-29 14:32 ` empty-line-p Joost Kremers
2007-03-30  8:56   ` empty-line-p David Hansen
2007-03-31  5:49     ` empty-line-p Andreas Roehler
2007-03-31 15:17       ` empty-line-p Xavier Maillard
2007-04-06 14:44         ` empty-line-p Andreas Roehler
     [not found]   ` <mailman.1621.1175289074.7795.help-gnu-emacs@gnu.org>
2007-04-01 20:57     ` empty-line-p Stefan Monnier
2007-03-30  5:36 ` empty-line-p Stefan Monnier
2007-03-30  5:46   ` empty-line-p Barry Margolin
2007-04-01 20:54     ` empty-line-p Stefan Monnier
2007-03-30  6:14   ` empty-line-p Andreas Roehler

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).