Text manipulation
Page 1 of 1

Author:  nattskatta [ 2011-09-01 01:08:40 ]
Post subject:  Text manipulation

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!).
Find '\[[0-9]+\]', 'Emr'

finds backwards and selects strings like


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.

Author:  phspaelti [ 2011-09-01 04:56:39 ]
Post subject:  Re: Text manipulation

In the Nisus provided "Sum Selection" macro you'll find that the first line reads:
$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:
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:
$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.

Author:  nattskatta [ 2011-09-01 08:43:24 ]
Post subject:  Re: Text manipulation

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

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group