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:
Tue, 28 Dec 2010 03:34:56 +0900
Content-Type:
text/plain
Parts/Attachments:
text/plain (101 lines)
Below are the essentials of what I came to know by exchanging messages with Martin at Nisus Soft and of some undocumented things I verified by myself.


- You can repeat the same commands several times without fearing a performance hit. I was told that NWP caches them. For example, recently I wrote such a routine in a macro.
<http://www2.odn.ne.jp/alt-quinon/files/NWPro/link/MakeLinkIndex_nwm.zip>

foreach $sel in $sels
	$attr = $sel.text.attributesAtIndex $sel.location
	if $pages{$attr.link} == undefined
		$pages{$attr.link} = Array.new
	end
	$page = $sel.text.pageNumberAtIndex $sel.location
	if $pages{$attr.link}.indexOfValue($page) == -1
		$pages{$attr.link}.appendValue $page
	end
	$page = $sel.text.pageNumberAtIndex($sel.bound - 1)
	if $pages{$attr.link}.indexOfValue($page) == -1
		$pages{$attr.link}.appendValue $page
	end
end

Looking odd? In such a case, usually you would use a variable storing $attr.link ($linkText in the code below). But there is no significant difference in performance (I did benchmark tests) between the code above and a common way of coding like this:

foreach $sel in $sels
	$attr = $sel.text.attributesAtIndex $sel.location
	$linkText = $attr.link
	if $pages{$linkText} == undefined
		$pages{$linkText} = Array.new
	end
	$page = $sel.text.pageNumberAtIndex $sel.location
	if $pages{$linkText}.indexOfValue($page) == -1
		$pages{$linkText}.appendValue $page
	end
	$page = $sel.text.pageNumberAtIndex($sel.bound - 1)
	if $pages{$linkText}.indexOfValue($page) == -1
		$pages{$linkText}.appendValue $page
	end
end


- "$textObject.find" is much faster than "Find" command because the former does not create selections actually. There is a huge difference when processing a very large number of occurrences.



- Something similar applies to "Push Target Selection ... Pop Target Selection" command. For example,

	$doc = Document.active
	$sels = $doc.text.findAll '(?<=\t)\S+(?=\t)', 'E'
	Push Target Selection $sels
		Set Font Name 'Times'
	Pop Target Selection

is much and much faster than

	Find All '(?<=\t)\S+(?=\t)', 'E'
	Set Font Name 'Times'

when there will be a large number of selections.


- When inserting a large number of footnotes, it is not recommendable to process from the document end to the document start. Then, NWP would be forced to update note numbers of all the already inserted footnotes each time you insert a new one.

Then, one inconvenience in processing from the document start is the necessity to update TextSelection objects when a new footnote has been inserted. For that, you can use a routine in a macro
	<http://www2.odn.ne.jp/alt-quinon/files/NWPro/footendnotes/ConvertHTMLNotes_nwm.zip>
written for converting notes in e-texts such as
	<http://socserv.mcmaster.ca/~econ/ugcm/3ll3/hume/treatise1.html>

In that macro, you will find...

$offset = 0

# $ref is a TextSelection object corresponding, for example, with "(47)" in the html file
foreach $ref in $noteRef
	# Extract the subtext of the first value of $noteText (array)
	$note = $noteText.dequeue.subtext
	# replace "47. " of "47. Sect. 4." with $defNoteText
	$note.findAndReplace $noteNum, $defNoteText, 'E-i'
	# Store the current $text.length in $previousTextLength
	$previousTextLength = $text.length
	# Modify the location of $ref so that it fits the current state of $text
	$ref.location -= $offset
	# Delete "(47)".
	$text.deleteInRange $ref.range
	# Insert a footnote with note text ($note)
	Note.insertFootnoteInTextAtIndex $text, $ref.location, $note
	# Increase $offset by (PreviousTextLength - CurrentTextLength)
	$offset += $previousTextLength - $text.length
end

The code above is based on the one given by Martin when I complaint about the performance of an oldest version of my macro, namely <http://www2.odn.ne.jp/alt-quinon/files/NWPro/footendnotes/Body2Note_nwm.zip>.


- And... probably you would already know but there is a long-standing bug in the interpretation of back slash enclosed by single quotes. The find expression in

	Find '\\footnote{[^}]+}', 'E'

will be interpreted as '\f' (page break) followed by 'ootnote{[^}]+}'. To work it around, you have to put three (or four?) back slashes. That's ugly. So I always use a hexadecimal notation \x{5C}.


Kino

ATOM RSS1 RSS2