Hello again,
On Jan 1, 2011, at 11:08 PM, Nobumi Iyanaga wrote:
> can I understand that they are somehow equivalent of "SetSelect (x, y)" in Classic Nisus Writer macro...?
I have a vague feeling that your very long acquaintance with NW Classic macro -- much longer than mine -- tends to mislead you. NWP macro has nothing to do with NW Classic macro although you may believe to see something common, accidentally. Or perhaps my wording is wrong. For what we are discussing, is "offset" inappropriate? Perhaps. By it, I mean "zure" in Japanese...
Anyway I try to explain again, using another sample macro to insert line numbers. To simplify the code, it inserts them at the top of all paragraphs in the main body, regardless of actual selection(s).
# begin macro
$prefix = Cast to String '['
$suffix = Cast to String "]\t" # double quotes because a meta character \t (tabs) is there
$doc = Document.active
# Find all paragraph starts not in any/all text objects (-a)
# but in the main body (m) only,
$sels = $doc.text.findAll '^', 'E', '-am'
# $sels may contain matches in table cells.
# So we will check all values in $sels one by one
# from the last to the first and, if it belongs
# to a table cell, we remove it from $sels.
# "From the last to the first" because, otherwise,
# the removal of a value will change the index.
foreach $i, $sel in reversed $sels
if $sel.text.documentContentType == 'table'
$sels.removeValueAtIndex $i
end
end
$i = 1
$offset = 0
# Process from the document start to the end.
# Then, each time you insert a line number ($num),
# locations of the remaining numbers change.
# So we have to adjust $sel.location by calculating
# $offset (increase of character index).
foreach $sel in $sels
$num = $prefix & $i
$num &= $suffix # same as "$num = $num & $suffix"
$sel.text.insertAtIndex $sel.location + $offset, $num
$offset += $num.length # same as $offset = $offset + $num.length
$i += 1 # same as "$i = $i + 1"
end
# end macro
What makes NWP macro somewhat difficult to write is table(s) as native and essential element(s) of a document. In NW Classic, we just didn't care of tables. If we could exclude table cells from the find scope... But Martin rejected my request for "t" as a 'where' argument because a table may exists not only in the main body but also in footnote area, for example.
Anyway, in NW Classic macro, we would do in the following way. (The macro below is very unlikely to work -- I still have Nisus Guide 6.5 ;-) but I have forgot the Classic Nisus Way almost completely -- and, for the simplicity of the code, it ignores empty paragraphs.)
// begin macro
prefix = '['
suffix = ']' + NumToChar (9)
Find All '^.' 'g'
s->qpush (starts)
num = 1
offset = 0
Loop:
if (! s->size) exit
i = s->pop
i = i + offset
SetSelect (i, i)
clipboard = prefix + num + suffix
MacroPaste
len = length (clipboard)
offset = offset + len
num = num + 1
goto Loop
// end macro
As far as the last loop is concerned, a NWP macro code closer to the NW Classic macro code above would be...
foreach $sel in $sels
$num = $prefix & $i
$num &= $suffix
$range = Range.new $sel.location + $offset, 0
$updatedSelection = TextSelection.new $sel.text, $range
$doc.setSelection $updatedSelection
Type Text $num
$offset += $num.length
$i += 1
end
And... the reason why I wrote "Why the worst of all the three macros?" is that I requested containsSelection and containsRange commands (and I got them together with six [!] unrequested commands) just to avoid that kind of subtext trick when you want to do "Find All (or Replace All) in Selection" without creating actual selections and regardless of the active window. Perhaps it is my personal taste, but I feel the use of containsSelection to be much simpler.
Kino
|