Text manipulation

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
nattskatta
Posts: 7
Joined: 2007-07-06 00:52:34
Location: Lund, Sweden

Text manipulation

Post by nattskatta » 2011-09-01 01:08:40

I’m translating a book, and my (human) editor wants to have the page numbers of the original in the translated document. The page numbers are in the form “[123]” (left square bracket, 1–3 numerals, right square bracket) in a separate paragraph.

Currently, a Quickeys macro applies a paragraph style, types the brackets, and puts the cursor inside. It occurred to me that the functionality of this macro could be extended, not so much for convenience as a fun way to learn Nisus’ macro language.

What I want the macro to accomplish is to search backwards in the document to the previous page number, select the number part, increment this with +1, go back to the end, and paste the next page number complete with square brackets and paragraph style.

By scrupulous perusal of the Nisus macro reference, I found out how to find the previous page number (was I proud!).

Code: Select all

Find '\[[0-9]+\]', 'Emr'
finds backwards and selects strings like

[1]
[34]
[219]

Now I’m stuck on the hard part: how to manipulate the result. I must confess that I’m slightly dizzied by what seems to be a myriad of options in the macro reference. There must be a way to
1) select the numerical part of the found string (or to strip the brackets),
2) increment the resulting value with +1

I would appreciate any pointers in the right direction. The rest of the macro I believe I can figure out on my own.

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

Re: Text manipulation

Post by phspaelti » 2011-09-01 04:56:39

In the Nisus provided "Sum Selection" macro you'll find that the first line reads:

Code: Select all

$numbers = Read Selection
I bet that you can figure out what that does.
Now there are basically two approaches. Since your find command selects something like "[123]" you can either read the whole thing in, and strip off the brackets, or you can make sure to select just the number before reading in. There is a lot to be said for the first method, but I suggest trying the second to become more comfortable with the awesome powers of Nisus Powerfind (Pro).
In the Find window under "[gear] > Match" you will see "Preceded by" and "Followed by". Using those features you can tweak your Find expression to get this:

Code: Select all

Find '(?<=\[)[0-9]+(?=\])', 'Emr'
This will find the same thing as yours, but it will only select the number.
Finally you wanted to know how to increase the number. Nisus macro language allows the following method:

Code: Select all

$numbers += 1
Depending on your coding experience this way of writing things will be either clear as crystal or mud. But you wanted "pointers" so that's what you get. :)
If you want more elaboration just say so.
Happy macro writing.
philip

User avatar
nattskatta
Posts: 7
Joined: 2007-07-06 00:52:34
Location: Lund, Sweden

Re: Text manipulation

Post by nattskatta » 2011-09-01 08:43:24

Thanks ever so much, Philip! This was exactly what I was looking for. I really must check out Powerfind Pro; I wasn’t aware of the macroize thang.
With your help I’ve come up with this extremely spiffy macro:

Code: Select all

Find '(?<=\[)[0-9]+(?=\])', 'Emr'
$numbers = Read Selection
$numbers += 1
Select Document End
Menu ":Format:Paragraph Style:Sidnr orig"
Type Text "[$numbers]"
Type Text "\n"
Menu ":Format:Paragraph Style:Normal"
As you’ve probably already guessed, my programming skills are almost negligible – though to my great surprise, IIRC, I was able to do amazing things with the English-like macro language in the sadly deceased Wordperfect for Mac. Anyhow, it’s great to be part of the helpful Nisus community.

Post Reply