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:
Nobumi Iyanaga <[log in to unmask]>
Reply To:
Date:
Wed, 29 Dec 2010 21:33:18 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (90 lines)
Hello Kino,

I learn a lot from your replies and postings and I save them for  
future reference. Instead of replying to them and asking you more  
questions, I think I would learn more, if I try to write more macros,  
and ask you questions about them.

Here is another essay: it adds at the beginning of each line (of the  
main body text) the line number followed by a tab.

## add_line_numbers ##

$doc = Document.active

$sels = Array.new
$sels = $doc.text.findAll '^', 'E', '-am'

$tab = "\t"
$i = $sels.count
$num = ''
foreach $sel in reversed $sels
	$loc = $sel.location
	$num = "$i$tab"
	$num = Cast to string $num
	$sel.text.insertAtIndex $loc, $num
	$i = $i - 1
end

## end of macro ##

I tried this macro, because it "translates" somehow the pattern of  
the most common macros that we used to write for Classic Nisus  
Writer, that is:
1. Find locations of strings matching some pattern with a regex search;
2. visit in a loop all the found strings (with "SetSelect" in the  
Classic Nisus Writer macro), making some change(s) to each one.

This seems to work as expected. The most convenient thing here is the  
line:
$sels = $doc.text.findAll '^', 'E', '-am'
because it makes in a single line an array of all the "selectedTexts"  
which can be visited in the following loop. And this is very fast,  
because, I thing, it works in the variable (this would be the  
advantage of the "reference semantics"...??).

----

However, when I tried to modify this macro, so that when there is a  
selection of several lines (the selection must begin either at the  
beginning of the body text, or just after a return character...), it  
would add line numbers only to these lines (from 1 to n...), I am  
stumbled because of many problems. I tried to write something like this:

$sel = TextSelection.active
$sel_range = $sel.range
$sel_location = $sel_range.location
$if_slection_at_zero = false
if $sel.length
	if $sel_location == 0
		Find All '^(.|\n)', 'Es', '-am'
		$sels = $doc.textSelections
#		exit $sels.count
		$if_slection_at_zero = true
	else
		$previous_char = $the_text.characterAtIndex ($sel_location - 1)
		if $previous_char == 10
			Find All '^', 'Es', '-am'
			$sels = $doc.textSelections
#			exit $sels.count
		else
			exit 'The selection should be done on whole lines...'
		end
	end
else
...

My main problem is that the value of "$sels.count" cannot be what I  
expect (for example, when I select two or more lines in the middle of  
the document, the value of "$sels.count" is 1, instead of 2 or more...).

Could you please show me how you would do in such a situation?

Thank you in advance.

Best regard,

Nobumi Iyanaga
Tokyo,
Japan

ATOM RSS1 RSS2