Deleting the line you are in

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 259
Joined: 2007-04-12 14:59:36

Deleting the line you are in

Post by js »

What is in fact the simplest way to delete through a macro the line that has the cursor (or even better: all lines that have a selection)?
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Deleting the line you are in

Post by Kino »

This is an interesting problem. Sometimes it is very difficult for a macro to do what is or looks pretty easy for a human being.

I don't know what to do with selections in notes. Is there a simple way to get a note object to which a selection belongs, for example? So the following macro treats selections in the main body only, ignoring those in the other text objects.

Code: Select all

Require Pro Version 1.2

$doc = Document.active
if $doc == undefined  # if there is no open document...
	exit  # exit silently
end

$text = $doc.text
$sels = $doc.textSelections
$i = $sels.count - 1

# remove "selections not belonging to the main body or belonging to the same paragraph" from $sels

while $i >= 0
	if $sels[$i].text == $text  # if $sels[$i] belongs to the main body...
		if $i > 0
			if $sels[$i-1].text == $text  # if $sels[$i-1] belongs to the main body...
				$selBetween = TextSelection.newWithLocationAndBound $text, $sels[$i-1].location, $sels[$i].bound
				$return = $selBetween.subtext.find '\n|\f', 'E'
				if $return == undefined  # if there is no LF nor Break between the two selections...
					$sels.removeValueAtIndex $i
				end
			end
		end
	else  # i.e.$sels[$i] does not belong to the main body...
		$sels.removeValueAtIndex $i
	end
	$i -= 1
end

foreach $sel in reversed $sels  # select a paragraph in which $sel is situated
	$doc.setSelection $sel  # make a selection corresponding to $sel
	if Select Next Paragraph
		Select Previous Paragraph
	elsif Select Previous Paragraph
		Select Next Paragraph
	else
		Select Paragraph 1  # a document consisting of a single paragraph
	end
	Menu ':Edit:Delete'
end
Bug: if the document consists of a single paragraph, that paragraph will be deleted even when the caret is situated after LF.

I think something like Select Current Paragraph, Select Current Page, Select Current List Item, etc. would be useful.
js
Posts: 259
Joined: 2007-04-12 14:59:36

Re: Deleting the line you are in

Post by js »

This works great, thank you.
So far I used a method without macros that is something of a trick:
Let‘s say you got a multiple selection through Powerfind or manually. If you add an arbitrary tab now, the ruler symbol will appear on the lower window bar, and „select all“ will select the lines with the original selections which you can then delete. But of course your macro is easier to use.
Groucho
Posts: 497
Joined: 2007-03-03 09:55:06
Location: Europe

Re: Deleting the line you are in

Post by Groucho »

Hello, js

Here is another way to do the same.
1) Open the Find window.
2) Select PowerFind Pro.
3) Type as follows:

Find what: .*Text_to_Find.*(\n|$)
Replace with: (leave this field empty)

4) Click Replace All.

Where Text_to_Find is the word(s) you are looking for.

Cheers, Henry.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Deleting the line you are in

Post by phspaelti »

@Groucho: But js wanted to start from a selection that he already had. So your method doesn't really apply here.

It did however give me an idea for a much simpler approach than Kino's. How about the following:

Code: Select all

Find and Replace '.+', '_Line_To_Be_Deleted_', 'Esa'
Find and Replace '^.*_Line_To_Be_Deleted_.*(\n|$)', '', 'Ea'
philip
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Deleting the line you are in

Post by Kino »

But any of your approaches (of you two) does not work for paragraphs having a footnote or for selections in note text. This problem, pointed out by Nobumi, is perhaps the most troublesome when writing a macro.
http://nisus.com/forum/viewtopic.php?p=13052#p13052
I don't know if the original poster's documents have footnotes, though.

And I was wrong in saying:
Is there a simple way to get a note object to which a selection belongs, for example?
I had forgotten ".noteAtIndex" command.

Edit: I think you'd better take account of \f (Page/Section Break). Nisus Writer treats text ending with \f as a paragraph not only in the Statistics panel but also in anchors such ^ and $. And ^ matches nothing in a note text if it consists of a single paragraph (when you do Find Previous '^', 'E', for example) but matches the paragraph start in the body text to which the note belongs. Also . (any character) excludes \n and not \f. So sometimes you are forced to make a find expression more complicated.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Deleting the line you are in

Post by phspaelti »

Kino, thanks for your reply. I must say that I hardly ever use footnotes, so I really don't think about them. For the same reason I have not usually read your and Nobumi's comments on these matters very carefully

However after reading about them, and then trying some tests of my own, I have to say that NWPs behavior with these things is just flaky. The difference between endnote and footnote reference can only be considered a bug. I also cannot understand the behavior of "return" characters after footnotes. "." does not match return characters. But at the end of a group of footnotes on a single page there is a character that is matched by "." Also very strange is the difference between ".$" and ".\n". The first will find the last character of every line of every footnote, and also an extra character after the last footnote in the document. ".\n" finds the last character of every footnote line, except for the last footnote line on every page.

There is also some kind of "safety" with the return characters at the end of footnotes. Find ".+\n" will match the last line of a footnote, but if you try to use the same expression with Replace, NW does not carry out the replacement. (It leaves the line selected). Presumably removing the return would cause problems for the handling of the footnotes, so I can understand this, but some kind of warning might be good. I guess similar issues prevent matching "^" to the beginning of the footnote. Otherwise it would be possible to (accidentally) insert something before the footnote marker, or worse accidentally replace the footnote marker and thereby causing more serious problems.

Also a question: Is it possible to find footnote reference markers with Powerfind? Or is it necessary to use a macro?

Finally, thank you for the comment on the "\f", which I use quite often. I am generally happy with the fact that NWP handles page breaks more like real paragraph markers. (The handling of page breaks was something that I always disliked about NW Classic.) But now I think that find should treat "\f" like a paragraph marker as well. So "." should not match "\f".
philip
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Deleting the line you are in

Post by Kino »

Yes, this is flaky and confusing. White space characters (or something looking such) between note texts are treated as \n when "One Note per Line" is checked in Note Styles and as \t when it is not checked. And those at the end of each page are treated as \f.

As you noticed, Find '\t|\n|\f', 'E' finds them but you cannot replace them with anything or nothing. And you cannot limit the scope of Find to a given text object. So sometimes you have to filter text selection objects by something like this.

Code: Select all

$doc = Document.active
$text = $doc.text
Find All '[\n\f]+', 'E'
$sels = $doc.textSelections
$i = $sels.count - 1
while $i >= 0
	if $sels[$i].text != $text  # not belonging to the main body text object
		$sels.removeValueAtIndex $i
	end
	$i -= 1
end
$doc.setSelections $sels

And it is impossible to find note references using PowerFind Pro. Until the release of NW Pro 1.1, We, Nobumi and me, had been complaining about those oddities and limitations, which made it impossible to write macros à la NW Classic which manipulate footnotes/endnotes. However, NW Pro 1.1 came with a new macro system having many commands related to note objects. Then, we began to play with the new toy, neglecting old problems ;-)

In my limited experience, I think most things can be done with the new macro language and often more efficiently, except in rare cases such as the one discussed in this thread. For example, you can select all note references in the main body in the following manner.

Code: Select all

$doc = Document.active
$text = $doc.text
$notes = $doc.allNotes
$sels = Array.new
foreach $note in $notes
	$sel = TextSelection.new $text, $note.documentTextRange
	$sels.appendValue $sel
end
$doc.setSelections $sels
However, this kind of code (get character ranges of all note references at the beginning of a macro) is not necessary any more for many macros, e.g. a macro for converting real footnotes into in-line notes.

There is another oddity slightly related. Word boundary \b and \> does not work for characters followed by a note reference. For example, Find All '\b\p{L}+\b', 'E' fails to find "word²". So you have to use w option to make a word list macro work properly with a document having footnotes.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Deleting the line you are in

Post by phspaelti »

Kino wrote:Yes, this is flaky and confusing. White space characters (or something looking such) between note texts are treated as \n when "One Note per Line" is checked in Note Styles and as \t when it is not checked. And those at the end of each page are treated as \f.
Aha. Explained that way it all makes sense. So I take back what I said about it being flaky. But while it may make sense to the Nisus developers to implement things that way underneath the hood, it's unfortunately not very useful for the user. From the user's perspective all notes should behave the same way.
philip
Kino
Posts: 400
Joined: 2008-05-17 04:02:32

Re: Deleting the line you are in

Post by Kino »

Kino wrote:I think something like Select Current Paragraph, Select Current Page, Select Current List Item, etc. would be useful.
As many must have noticed, that kind of complication is not necessary anymore, IIRC since NWP 1.3, thanks to their continuous and assiduous effort to improve NWP. Those select commands support non-contiguous selections, btw.

Code: Select all

Menu ':Edit:Select:Select Paragraph'
Menu ':Edit:Delete'
Alternatively you can use Send Selector so that the macro works with all localized interfaces without modification.

Code: Select all

Send Selector 'selectParagraph:'
Send Selector 'delete:'
Post Reply