Reply to topic  [ 3 posts ] 
Text manipulation 
Author Message
User avatar

Joined: 2007-07-06 00:52:34
Posts: 7
Location: Lund, Sweden
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:
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.


2011-09-01 01:08:40
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
In the Nisus provided "Sum Selection" macro you'll find that the first line reads:
Code:
$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:
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:
$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


2011-09-01 04:56:39
Profile
User avatar

Joined: 2007-07-06 00:52:34
Posts: 7
Location: Lund, Sweden
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:
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.


2011-09-01 08:43:24
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 3 posts ] 

Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software