unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* creating a function that works for active region or whole buffer
@ 2013-01-22 11:20 Luca Ferrari
  2013-01-22 14:37 ` Mark Skilbeck
  2013-01-22 16:01 ` Le Wang
  0 siblings, 2 replies; 10+ messages in thread
From: Luca Ferrari @ 2013-01-22 11:20 UTC (permalink / raw)
  To: help-gnu-emacs

Hi all,
I'd like to write a function that can be invoked when a region is
active, and therefore is limited to the region itself, or on the whole
buffer if not any region is active. Therefore in my function I placed
the following conditional to set the start-block and end-block lines
to the whole buffer or the whole region:


(if (not (null (region-beginning) ) )
          (progn

            (setq current-block-start-line (line-number-at-pos
(region-beginning) ) )
            (setq current-block-end-line   (line-number-at-pos
(region-end) ) ) )

                                        ; else mark the whole buffer
        (progn
          (setq current-block-start-line (line-number-at-pos (point-min) ) )
          (setq current-block-end-line   (line-number-at-pos (point-max) ) ) ) )

It seems to work, but when I mark a region, that remove the region
(i.e., unmark) and call the function again it seems that the function
has still the region-beginning and region-end marks (i.e., it does not
work on the whole buffer). Is there a smarter way to see if a region
is currently active?

Thanks,
Luca



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

* Re: creating a function that works for active region or whole buffer
  2013-01-22 11:20 creating a function that works for active region or whole buffer Luca Ferrari
@ 2013-01-22 14:37 ` Mark Skilbeck
  2013-01-22 16:01 ` Le Wang
  1 sibling, 0 replies; 10+ messages in thread
From: Mark Skilbeck @ 2013-01-22 14:37 UTC (permalink / raw)
  To: Luca Ferrari; +Cc: help-gnu-emacs

Perhaps with-region-or-buffer[1] will be of help.

-mgsk

[1] http://irrealblog.blogspot.co.uk/2011/03/region-or-buffer-revisited-in-writing_02.html

On Tue, Jan 22, 2013 at 12:20:54PM +0100, Luca Ferrari wrote:
> Hi all,
> I'd like to write a function that can be invoked when a region is
> active, and therefore is limited to the region itself, or on the whole
> buffer if not any region is active. Therefore in my function I placed
> the following conditional to set the start-block and end-block lines
> to the whole buffer or the whole region:
> 
> 
> (if (not (null (region-beginning) ) )
>           (progn
> 
>             (setq current-block-start-line (line-number-at-pos
> (region-beginning) ) )
>             (setq current-block-end-line   (line-number-at-pos
> (region-end) ) ) )
> 
>                                         ; else mark the whole buffer
>         (progn
>           (setq current-block-start-line (line-number-at-pos (point-min) ) )
>           (setq current-block-end-line   (line-number-at-pos (point-max) ) ) ) )
> 
> It seems to work, but when I mark a region, that remove the region
> (i.e., unmark) and call the function again it seems that the function
> has still the region-beginning and region-end marks (i.e., it does not
> work on the whole buffer). Is there a smarter way to see if a region
> is currently active?
> 
> Thanks,
> Luca



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

* Re: creating a function that works for active region or whole buffer
  2013-01-22 11:20 creating a function that works for active region or whole buffer Luca Ferrari
  2013-01-22 14:37 ` Mark Skilbeck
@ 2013-01-22 16:01 ` Le Wang
  2013-01-22 17:18   ` Ludwig, Mark
       [not found]   ` <mailman.18089.1358876370.855.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 10+ messages in thread
From: Le Wang @ 2013-01-22 16:01 UTC (permalink / raw)
  To: Luca Ferrari; +Cc: help-gnu-emacs

On Tue, Jan 22, 2013 at 7:20 PM, Luca Ferrari <fluca1978@infinito.it> wrote:
> Hi all,
> I'd like to write a function that can be invoked when a region is
> active, and therefore is limited to the region itself, or on the whole
> buffer if not any region is active. Therefore in my function I placed
> the following conditional to set the start-block and end-block lines
> to the whole buffer or the whole region:
>
>
> (if (not (null (region-beginning) ) )
>           (progn
>
>             (setq current-block-start-line (line-number-at-pos
> (region-beginning) ) )
>             (setq current-block-end-line   (line-number-at-pos
> (region-end) ) ) )
>
>                                         ; else mark the whole buffer
>         (progn
>           (setq current-block-start-line (line-number-at-pos (point-min) ) )
>           (setq current-block-end-line   (line-number-at-pos (point-max) ) ) ) )

Don't focus on line numbers, focus on buffer positions.

> It seems to work, but when I mark a region, that remove the region
> (i.e., unmark) and call the function again it seems that the function
> has still the region-beginning and region-end marks (i.e., it does not
> work on the whole buffer). Is there a smarter way to see if a region
> is currently active?

See mark-active-p, use-region-p documentation.

When you have this function written, if you post the whole thing here,
I'm sure people will give you some ideas on improving your code.


-- 
Le



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

* RE: creating a function that works for active region or whole buffer
  2013-01-22 16:01 ` Le Wang
@ 2013-01-22 17:18   ` Ludwig, Mark
       [not found]   ` <mailman.18089.1358876370.855.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: Ludwig, Mark @ 2013-01-22 17:18 UTC (permalink / raw)
  To: Le Wang, Luca Ferrari; +Cc: help-gnu-emacs

> From: Le Wang
> Sent: Tuesday, January 22, 2013 10:02 AM
> To: Luca Ferrari
> Cc: help-gnu-emacs
> Subject: Re: creating a function that works for active region or whole buffer
> 
> On Tue, Jan 22, 2013 at 7:20 PM, Luca Ferrari <fluca1978@infinito.it> wrote:
> > Hi all,
> > I'd like to write a function that can be invoked when a region is
> > active, and therefore is limited to the region itself, or on the whole
> > buffer if not any region is active. Therefore in my function I placed
> > the following conditional to set the start-block and end-block lines
> > to the whole buffer or the whole region:
> >
> >
> > (if (not (null (region-beginning) ) )
> >           (progn
> >
> >             (setq current-block-start-line (line-number-at-pos
> > (region-beginning) ) )
> >             (setq current-block-end-line   (line-number-at-pos
> > (region-end) ) ) )
> >
> >                                         ; else mark the whole buffer
> >         (progn
> >           (setq current-block-start-line (line-number-at-pos (point-min) ) )
> >           (setq current-block-end-line   (line-number-at-pos (point-max) ) ) ) )
> 
> Don't focus on line numbers, focus on buffer positions.

Right!

> > It seems to work, but when I mark a region, that remove the region
> > (i.e., unmark) and call the function again it seems that the function
> > has still the region-beginning and region-end marks (i.e., it does not
> > work on the whole buffer). Is there a smarter way to see if a region
> > is currently active?
> 
> See mark-active-p, use-region-p documentation.

I recommend just using (point-min) and (point-max) as these work appropriately in and out of a narrowed-region.

Cheers,
Mark




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

* Re: creating a function that works for active region or whole buffer
       [not found]   ` <mailman.18089.1358876370.855.help-gnu-emacs@gnu.org>
@ 2013-01-22 19:57     ` Barry Margolin
  2013-01-23  7:41       ` Luca Ferrari
  0 siblings, 1 reply; 10+ messages in thread
From: Barry Margolin @ 2013-01-22 19:57 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.18089.1358876370.855.help-gnu-emacs@gnu.org>,
 "Ludwig, Mark" <ludwig.mark@siemens.com> wrote:

> > From: Le Wang
> > Sent: Tuesday, January 22, 2013 10:02 AM
> > To: Luca Ferrari
> > Cc: help-gnu-emacs
> > Subject: Re: creating a function that works for active region or whole 
> > buffer
> > 
> > On Tue, Jan 22, 2013 at 7:20 PM, Luca Ferrari <fluca1978@infinito.it> 
> > wrote:
> > > Hi all,
> > > I'd like to write a function that can be invoked when a region is
> > > active, and therefore is limited to the region itself, or on the whole
> > > buffer if not any region is active. Therefore in my function I placed
> > > the following conditional to set the start-block and end-block lines
> > > to the whole buffer or the whole region:
> > >
> > >
> > > (if (not (null (region-beginning) ) )
> > >           (progn
> > >
> > >             (setq current-block-start-line (line-number-at-pos
> > > (region-beginning) ) )
> > >             (setq current-block-end-line   (line-number-at-pos
> > > (region-end) ) ) )
> > >
> > >                                         ; else mark the whole buffer
> > >         (progn
> > >           (setq current-block-start-line (line-number-at-pos (point-min) 
> > >           ) )
> > >           (setq current-block-end-line   (line-number-at-pos (point-max) 
> > >           ) ) ) )
> > 
> > Don't focus on line numbers, focus on buffer positions.
> 
> Right!
> 
> > > It seems to work, but when I mark a region, that remove the region
> > > (i.e., unmark) and call the function again it seems that the function
> > > has still the region-beginning and region-end marks (i.e., it does not
> > > work on the whole buffer). Is there a smarter way to see if a region
> > > is currently active?
> > 
> > See mark-active-p, use-region-p documentation.
> 
> I recommend just using (point-min) and (point-max) as these work 
> appropriately in and out of a narrowed-region.

Who said anything about narrowing?

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***


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

* Re: creating a function that works for active region or whole buffer
  2013-01-22 19:57     ` Barry Margolin
@ 2013-01-23  7:41       ` Luca Ferrari
  2013-01-23  7:43         ` Luca Ferrari
  0 siblings, 1 reply; 10+ messages in thread
From: Luca Ferrari @ 2013-01-23  7:41 UTC (permalink / raw)
  To: help-gnu-emacs

Correct! Since I'm not interested in separating spaces from other
parts of the string I fixed the regexp to "^[ \t]*\\(.+\\):[ \t\n]*"
(at least one letter before the colon must be present).

Thanks guys!
Luca



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

* Re: creating a function that works for active region or whole buffer
  2013-01-23  7:41       ` Luca Ferrari
@ 2013-01-23  7:43         ` Luca Ferrari
  2013-01-28 14:02           ` Stefan Monnier
  0 siblings, 1 reply; 10+ messages in thread
From: Luca Ferrari @ 2013-01-23  7:43 UTC (permalink / raw)
  To: help-gnu-emacs

Ops...right answer to the wrong thread, sorry!
However below my function that is a simple indent behavior
(suggestions are welcome):

defun dataflex-indent-region-or-buffer ()
"This function indents the whole buffer or, in the case a region is
active, the active region."
  (interactive)

  (let ( (current-block-end-line 0 ) (current-block-start-line 0)
(current-indentation-step 0) (next-line-indentation-step 0) )
    (progn


      ;;  if using a region indent only such region, otherwise the whole buffer
      (if (use-region-p)
          ;;  using a region...
          (setq current-block-start-line (line-number-at-pos
(region-beginning) )
                current-block-end-line   (line-number-at-pos (region-end) ) )
        ;;  ...else use the whole buffer
        (setq current-block-start-line (line-number-at-pos (point-min) )
              current-block-end-line   (line-number-at-pos (point-max) ) ) )



      ;;  go to the starting line
      (goto-line current-block-start-line)

      ;;  go to the beginning of the line
      (beginning-of-line)

    (while (<= current-block-start-line current-block-end-line )
      (if (looking-at "^[ \t]*\\(IF\\|WHILE\\|BEGIN\\|LOOP\\)")
          ;; the BEGIN line has to be indented at the current level,
and the next
          ;;  line at a deeper level
          (setq next-line-indentation-step (+ current-indentation-step
dataflex-mode-indent-step) )
        ;;   else if looking at an END line remove the indentation
        (if (looking-at "^[ \t]*\\(END\\|RETURN\\|ABORT\\)")
            (progn
              (setq current-indentation-step (-
current-indentation-step dataflex-mode-indent-step) )
              (setq next-line-indentation-step current-indentation-step ) )
          ;;  else if this is a label line reset the indentation
          (if (looking-at  "^[ \t]*\\(.+\\):[ \t\n]*")
              (setq current-indentation-step 0
next-line-indentation-step dataflex-mode-indent-step-labels) ) ) )


      ;;  security check: do not indent at negative offset
      (if (< current-indentation-step 0 )
          (setq current-indentation-step 0) )
      (if (< next-line-indentation-step 0 )
          (setq next-line-indentation-step 0 ) )

      ;;  do the indent of the current line and go forward
      (indent-line-to current-indentation-step)
      (setq current-indentation-step next-line-indentation-step)
      (setq current-block-start-line (1+ current-block-start-line))
      (forward-line 1) ) ) ) )



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

* Re: creating a function that works for active region or whole buffer
  2013-01-23  7:43         ` Luca Ferrari
@ 2013-01-28 14:02           ` Stefan Monnier
  2013-02-01 10:36             ` Luca Ferrari
  0 siblings, 1 reply; 10+ messages in thread
From: Stefan Monnier @ 2013-01-28 14:02 UTC (permalink / raw)
  To: help-gnu-emacs

>           (setq current-block-start-line (line-number-at-pos
> (region-beginning) )
>                 current-block-end-line   (line-number-at-pos (region-end) ) )
>         ;;  ...else use the whole buffer
>         (setq current-block-start-line (line-number-at-pos (point-min) )
>               current-block-end-line   (line-number-at-pos (point-max) ) ) )

goto-line and line-number-at-pos are expensive operations that count
(and re-count and re-re-count every time) the number of LF chars from
the beginning of the buffer.


        Stefan




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

* Re: creating a function that works for active region or whole buffer
  2013-01-28 14:02           ` Stefan Monnier
@ 2013-02-01 10:36             ` Luca Ferrari
  2013-02-01 13:55               ` Stefan Monnier
  0 siblings, 1 reply; 10+ messages in thread
From: Luca Ferrari @ 2013-02-01 10:36 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: help-gnu-emacs

On Mon, Jan 28, 2013 at 3:02 PM, Stefan Monnier
<monnier@iro.umontreal.ca> wrote:
> goto-line and line-number-at-pos are expensive operations that count
> (and re-count and re-re-count every time) the number of LF chars from
> the beginning of the buffer.
>

I'm using them only at the beginning of the function, and therefore
once per function call, that does not sound very expensive to me. What
do you suggest instead?

Luca



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

* Re: creating a function that works for active region or whole buffer
  2013-02-01 10:36             ` Luca Ferrari
@ 2013-02-01 13:55               ` Stefan Monnier
  0 siblings, 0 replies; 10+ messages in thread
From: Stefan Monnier @ 2013-02-01 13:55 UTC (permalink / raw)
  To: Luca Ferrari; +Cc: help-gnu-emacs

>> goto-line and line-number-at-pos are expensive operations that count
>> (and re-count and re-re-count every time) the number of LF chars from
>> the beginning of the buffer.
> I'm using them only at the beginning of the function, and therefore
> once per function call, that does not sound very expensive to me.
> What do you suggest instead?

Use buffer positions.  I.e. (point) and (goto-char POS).
After (goto-char POS) you may call (beginning-of-line) or (end-of-line)
if you want to make sure you're at a line boundary.


        Stefan



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

end of thread, other threads:[~2013-02-01 13:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-01-22 11:20 creating a function that works for active region or whole buffer Luca Ferrari
2013-01-22 14:37 ` Mark Skilbeck
2013-01-22 16:01 ` Le Wang
2013-01-22 17:18   ` Ludwig, Mark
     [not found]   ` <mailman.18089.1358876370.855.help-gnu-emacs@gnu.org>
2013-01-22 19:57     ` Barry Margolin
2013-01-23  7:41       ` Luca Ferrari
2013-01-23  7:43         ` Luca Ferrari
2013-01-28 14:02           ` Stefan Monnier
2013-02-01 10:36             ` Luca Ferrari
2013-02-01 13:55               ` Stefan Monnier

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).