Hello Kino,
On Dec 29, 2010, at 11:44 PM, Kino wrote:
> On the other hand,
>
> Find All '^', 'E'
>
> does not work because you cannot create more than a single zero-
> width selection.
Yes. It seems to. But does this mean that
$sels = $doc.text.findAll '^', 'E', '-am'
and
Find All '^', 'Es', '-am'
behave differently? I cannot test now, but if my memory is correct, I
think that in Classic Nisus Writer macro, the equivalent of "Find All
'^', 'Es', '-am'" used to select all the beginnings of lines, even if
the actual selections were "invisible"...
>
> There is another problem. Find All '^(.|\n)', 'Es', '-am' will
> select two or more consecutive '^\n' (empty paragraphs) as a single
> selection. Probably you could solve this problem by calculating the
> length of selections but that would make the macro complicated.
>
> Then, you can use containsSelection command to know which paragraph
> starts got by
>
> $doc.text.findAll '^', 'E', '-am'
>
> are contained in selections you made. Something like this...
>
> ### begin macro ###
>
....
Thank you for this example. I will study it later...
>
> ... Another idea is to examine subtexts of the selections.
>
>
> ### begin macro ###
>
...
I tried to study this last example. There was one problem: when a
selection begins from the middle of a line, the macro adds the line
number + tab before this selection. I tried to prevent this, and
combine the previous macro with this one. Here is my attempt:
### add_line_numbers ###
## if there is no selection, adds line number followed by a tab
## to every line beginning.
## if there are selections, adds line number followed by a tab
## to every line selected.
## Selections must start from the beginning of a line;
## otherwise, if there is only one selection, and that selection
## begins from the middle of a line, the macro exits with an
## error message.
## if there are several selections, and one or more of them begin
## from the middle of a line, these selections are ignored.
$doc = Document.active
$sels = $doc.textSelections
$paraStarts = Array.new
if ! $sels.firstValue.length # if there is no selection...
$paraStarts = $doc.text.findAll '^', 'E', '-am'
end
foreach $sel in $sels
if $sel.location != 0 # this is to verify if the selection begins
# just after a return character, or from the beginning of
the document
$previous_char = $doc.text.characterAtIndex ($sel.location - 1)
else
$previous_char = 10
end
if $previous_char == 10
$founds = $sel.subtext.findAll '^', 'E' # "where" argument does
not work for subtext
foreach $found in $founds
if $found.text.documentContentType == 'body' # it may be, for
example, a table cell
$range = Range.new $found.location + $sel.location, 0
$start = TextSelection.new $sel.text, $range
$paraStarts.appendValue $start
end
end
else
if $sels.count == 1
exit 'The selection start is not at the beginning of a line.
Exiting...'
# if there is only one selection, and it begins from the middle of
a line...
end
end
end
# the rest is the same
$tab = Cast to String "\t"
foreach $i, $start in reversed $paraStarts
$num = $i + 1 # $i: index number
$num &= $tab
$start.text.insertAtIndex $start.location, $num
end
### end macro ###
Thank you in advance for your comments.
Best regard,
Nobumi Iyanaga
Tokyo,
Japan
|