NISUS Archives

December 2010

NISUS@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Reply To:
Date:
Fri, 31 Dec 2010 12:55:16 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (117 lines)
Hello Nobumi,

On Dec 30, 2010, at 11:42 PM, Nobumi Iyanaga wrote:

> But does this mean that
> 
> $sels = $doc.text.findAll '^', 'E', '-am'
> 
> and
> 
> 	Find All '^', 'Es', '-am'
> 
> behave differently?

Yes. Or rather, they return different kinds of value. 'Find All' returns NumFound.

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

That is different from what I remember. With a NW Classic Macro equivalent, you could get just NumFound.

>> ... Another idea is to examine subtexts of the selections.
>> 
>> 
>> ### begin macro ###
>> 
> ...
> 
> I tried to study this last example.

Why the worst of all the three macros? ;-)

> 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

In that construction, the next foreach loop will be executed even when

	$paraStarts = $doc.text.findAll '^', 'E', '-am'

has been done.

> 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

In the current version of NWP at least, ^ matches not only after \n but also after \f (0x000C = 12).

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

I think 'exit' is not a good idea. If there is multiple selections AND if this one is not the first, the macro has already inserted line numbers in previous selections. So preferably the macro tries to find the paragraph start for such a selection. That is NWP's standard behaviour. ':Format:List:Number List' works in such a way.

Then, we can do something like this.

# begin macro

$prefix = Cast to String ''
$suffix = Cast to String "\t"

$doc = Document.active
$sels = $doc.textSelections

$paraStarts = Array.new
if ! $sels.firstValue.length
	$sels = $doc.text.findAll '^(?=\p{Any})', 'E', '-am'
	foreach $sel in $sels
		$paraStarts.appendValue $sel.location
	end
else
	foreach $sel in $sels
		$sel.range = $sel.range.unionRange $sel.text.rangeOfParagraphAtIndex($sel.location)
		$founds = $sel.subtext.findAll '^', 'E'
		foreach $found in $founds
			if $found.text.documentContentType == 'body'
				$paraStarts.appendValue $found.location + $sel.location
			end
		end
	end
end

Select Start

foreach $i, $start in reversed $paraStarts
	$doc.text.insertAtIndex $start, $suffix
	$doc.text.insertAtIndex $start, $i + 1
	$doc.text.insertAtIndex $start, $prefix
end

# end macro


Kino

ATOM RSS1 RSS2