add line number to a selected text

Get help using and writing Nisus Writer Pro macros.
Post Reply
Nobumi Iyanaga
Posts: 158
Joined: 2007-01-17 05:46:17
Location: Tokyo, Japan
Contact:

add line number to a selected text

Post by Nobumi Iyanaga »

Hello,

I would like to write a macro that add line number (from 1 to x...) to the beginning of each paragraph in a selected text. The macro should be able to skip paragraphs depending on some conditions that can be detected by grep patterns (for example blank lines, "^\n", or lines beginning with "<", etc.).

The ideal would be to be able to begin the numbering after a line beginning with "<" (that is the next line), until another line beginning with the same sign, and then renewing the numbering from the next line. Here is an example:

<p. 1>
aaaaa
bbbbb
ccccc

ddddd
<p. 2>
aaaaa
bbbbb
ccccc

should be transformed like the following:

<p. 1>
1 aaaaa
2 bbbbb
3 ccccc

4 ddddd
<p. 2>
1 aaaaa
2 bbbbb
3 ccccc

and so on...

Thank you very much in advance for any help.
Best regards,

Nobumi Iyanaga
Tokyo,
Japan

User avatar
phspaelti
Posts: 1134
Joined: 2007-02-07 00:58:12
Location: Japan

Re: add line number to a selected text

Post by phspaelti »

Hello Nobumi, It's been a while :wink:

The first thing to do for this problem is to decide on a reasonable algorithm for how to do this. There is one problem though. Humans would just proceed from front to back, but as I'm sure you know, adding such numbers works best in reverse. That can only be done, if you first know how many there are in each section.

If you are happy working via the visual interface (usually a bit slower) then the following algorithm should work:
  1. Find all document sections that begin with a line starting with '<'
  2. Select the sections from the first step, in reverse order, one at a time
  3. For each section find in Selection (can only be done in the visual interface) all the lines you want to number
  4. Insert the numbers in reverse order (count down style)
  5. Repeat for next section
I don't like to do things via the visual interface, so I'm going to explain how to do this via the macro language.
Since this is part of a recurring problem, I'm putting here a general explanation about how to solve this. (See the attached document)
Assigning selections to headings.pdf
(68.7 KiB) Downloaded 144 times
Okay, so here is the algorithm:
  1. Find the paragraphs starting with '<'. These are the 'headings'
  2. Find the paragraphs you want to number
  3. Create a list of paragraph numbers to match the paragraphs as follows
  4. Set the counter to 1
  5. Run through the paragraphs until you come to a new heading, then reset the counter
  6. Go through the paragraphs in reverse order and insert the paragraph numbers
Here is a macro that works along these lines:
Nobumi's line numbering macro.nwm
(7.91 KiB) Downloaded 140 times
Last edited by phspaelti on 2020-04-24 00:35:34, edited 1 time in total.
philip

Nobumi Iyanaga
Posts: 158
Joined: 2007-01-17 05:46:17
Location: Tokyo, Japan
Contact:

Re: add line number to a selected text

Post by Nobumi Iyanaga »

Hello Philip,

Thanks a lot! I customized the grep pattern for my use, and it worked perfectly.

However, my document is very complicated, and cannot be processed at once. So, if possible, I would like to be able to run a similar macro on selections... Is it possible...?

Thank you in advance.
Best regards,

Nobumi Iyanaga
Tokyo,
Japan

User avatar
phspaelti
Posts: 1134
Joined: 2007-02-07 00:58:12
Location: Japan

Re: add line number to a selected text

Post by phspaelti »

Limiting such macros to selections is actually a pain, since those selections exist only in the GUI. For that kind of macro, it might make more sense to go with variant 1.

But I have adjusted it anyway :D Of course the results will only be as sensible as the selection you run them on. (Should work for non-contiguous selections too.)
Attachments
Nobumi's line numbering macro.nwm
Version 2
(10.7 KiB) Downloaded 137 times
philip

Nobumi Iyanaga
Posts: 158
Joined: 2007-01-17 05:46:17
Location: Tokyo, Japan
Contact:

Re: add line number to a selected text

Post by Nobumi Iyanaga »

Hello Philip,

Thank you for your new macro. I tried it on my documents. It works perfectly.

Thank you so much again!
Best regards,

Nobumi Iyanaga
Tokyo,
Japan

Post Reply