"Go to Next Comment" fails with nested comments

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
chazzo
Posts: 25
Joined: 2011-10-27 03:40:31
Location: Norfolk, UK

"Go to Next Comment" fails with nested comments

Post by chazzo »

The "Go to Next Comment" command doesn't seem to work as I would expect when comments are nested (i.e. when more than one separate comment is applied to the same piece of text). The first such comment is selected OK, but subsequent ones are ignored.

Is this a bug or a feature?

I'm posting here because outside the context of macros I don't see this as a big problem. But when I'm trying to use a macro to change the formatting of a whole bunch of comments, it's a nuisance. I'm borrowing heavily from this macro from Martin, and any macro using "Menu 'Go to Next Comment'" will see the same problem.

When comments are added via NWP's nice "+" button, "Go to Next Comment" shows them in their proper order, so I suppose in principle we should not need to use nested comments. But when working with Word users, a stack of "+" comments gets converted to individual comments, and the trouble begins. Besides, even in NWP it's nice to have the option of showing all comments at once, rather than flipping through them with the arrow buttons.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "Go to Next Comment" fails with nested comments

Post by phspaelti »

Well as pointed out on the other thread you should be able to find the answer in the file Help > Macro Language Reference.

There you'll find there is a document propery .allComments which will return the comments and then you should be able to loop through them with code

Code: Select all

$doc = Document.active
foreach $comment in $doc.allComments
    …
end
or something to that effect.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "Go to Next Comment" fails with nested comments

Post by phspaelti »

Anyhow just one more comment to the "Go to Next Comment" you mentioned and used in Martin's macro. That command does work for nested comments. I just tested it. However Martin's macro doesn't find them, since it uses the expedient of placing the selection after the comment to look for the next one. The macro could be improved by returning the cursor to the beginning of the selection instead of the end.
philip
User avatar
chazzo
Posts: 25
Joined: 2011-10-27 03:40:31
Location: Norfolk, UK

Re: "Go to Next Comment" fails with nested comments

Post by chazzo »

phspaelti wrote:

Code: Select all

$doc = Document.active
foreach $comment in $doc.allComments
    …
end
or something to that effect.
Thank you. After some fiddling:

Code: Select all

ForEach $comment in $doc.allComments
	$range = Range.new(0, $comment.text.length)
	$selection = TextSelection.new($comment.text, $range)
	$doc.setSelection($selection)
	Menu 'Paragraph Style:Comment'
End	
Is there a more elegant way to do that?
User avatar
chazzo
Posts: 25
Joined: 2011-10-27 03:40:31
Location: Norfolk, UK

Re: "Go to Next Comment" fails with nested comments

Post by chazzo »

phspaelti wrote:Anyhow just one more comment to the "Go to Next Comment" you mentioned and used in Martin's macro. That command does work for nested comments. I just tested it. However Martin's macro doesn't find them, since it uses the expedient of placing the selection after the comment to look for the next one. The macro could be improved by returning the cursor to the beginning of the selection instead of the end.
Odd, because for me "Go to Next Comment" skips nested comments even when I choose it from the main menu, so I was not surprised that it didn't work in a macro.

To be clear, my "nested comments" are multiple comments applied to the identical text range via the "Add Comment" command. If one comment's text range overlaps or encloses the other, then "Go to Next Comment" behaves as expected. And the same is true of comments created by clicking the "+" sign at the bottom of an existing comment.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "Go to Next Comment" fails with nested comments

Post by phspaelti »

chazzo wrote:

Code: Select all

ForEach $comment in $doc.allComments
	$range = Range.new(0, $comment.text.length)
	$selection = TextSelection.new($comment.text, $range)
	$doc.setSelection($selection)
	Menu 'Paragraph Style:Comment'
End	
Is there a more elegant way to do that?
Note that in the macro reference under Document Object Commands (selection additions) it says…
Nisus Macro Reference wrote:
3. A Text object following reference semantics, indicating all its text should be selected.
So in fact you can simplify the above to:

Code: Select all

ForEach $comment in $doc.allComments
	$doc.setSelection $comment.text
	Menu 'Paragraph Style:Comment'
End	
I was sort of wondering whether .setSelections might allow an array of texts as an argument (which would eliminate the need for the loop too!), but since one doesn't have access to the array of all and only the comment texts you'll need a loop anyhow.

One alternative (which doesn't save anything here) is to loop throughout $doc.allTexts, and check the texts for their .documentContentType. So like this:

Code: Select all

ForEach $text in $doc.allTexts
	if $text.documentContentType == "comment"
		$doc.setSelection $text
		Menu 'Paragraph Style:Comment'
	end
End	
And finally I was also trying to remember if it is possible to set the style of a text or text range directly without going through selection. Maybe that was just a feature request on my part.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "Go to Next Comment" fails with nested comments

Post by phspaelti »

chazzo wrote:To be clear, my "nested comments" are multiple comments applied to the identical text range via the "Add Comment" command.
Ah! I thought that Nisus didn't allow this kind of thing. (I really don't use comments too often.) I think Nisus doesn't allow such things to be created, but it probably respects them if they exist in an imported document. However the identity of the anchor text would create problems. Maybe Martin can enlighten us on this one.
philip
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: "Go to Next Comment" fails with nested comments

Post by martin »

chazzo wrote:Odd, because for me "Go to Next Comment" skips nested comments even when I choose it from the main menu, so I was not surprised that it didn't work in a macro.

To be clear, my "nested comments" are multiple comments applied to the identical text range via the "Add Comment" command. If one comment's text range overlaps or encloses the other, then "Go to Next Comment" behaves as expected.
I'd consider this a bug, especially as the "Go to Previous Comment" command has no troubles. I'll file it, thanks.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: "Go to Next Comment" fails with nested comments

Post by martin »

chazzo wrote:

Code: Select all

ForEach $comment in $doc.allComments
	$range = Range.new(0, $comment.text.length)
	$selection = TextSelection.new($comment.text, $range)
	$doc.setSelection($selection)
	Menu 'Paragraph Style:Comment'
End	
Is there a more elegant way to do that?
Philip already provided some elegant ways to do that with a macro, but here's another:

Code: Select all

Find All '(?:.|\n)+', 'E', '-ac'
Menu 'Paragraph Style:Comment'
That leverages the Find's "where" argument to only match text in the comment areas.

I'd also point out that you don't necessarily need a macro for this. You can just place the selection into any comment, Select All to select all text in that comment, and Select All again to select all text in all comments. Then you can manipulate and format all comments however you like.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: "Go to Next Comment" fails with nested comments

Post by martin »

phspaelti wrote:I was sort of wondering whether .setSelections might allow an array of texts as an argument (which would eliminate the need for the loop too!), but since one doesn't have access to the array of all and only the comment texts you'll need a loop anyhow.
If you did happen to have an array of all comment text objects, then ".setSelections" would indeed accept that. It actually accepts any of the objects mentioned in the documentation for ".setSelection" and should be updated to reflect that.
I think Nisus doesn't allow such things to be created, but it probably respects them if they exist in an imported document. However the identity of the anchor text would create problems. Maybe Martin can enlighten us on this one.
NWP does indeed let the users create such comments, where multiple comments are applied to exactly the same marked/anchoring text in the document, and in general shouldn't have any trouble handling or manipulating them– the bug with "Go To Next Comment" Charles found aside of course.
User avatar
chazzo
Posts: 25
Joined: 2011-10-27 03:40:31
Location: Norfolk, UK

Re: "Go to Next Comment" fails with nested comments

Post by chazzo »

martin wrote:I'd consider this a bug, especially as the "Go to Previous Comment" command has no troubles. I'll file it, thanks.
I hadn't noticed that "Go to Previous Comment" behaves differently. But the definitions of "previous" and "next" are also quite strange. I've just placed five successive comments on the same text range (using Esc to move back to the document text so as not to disturb the selection). The display order was 3, 5, 4, 1, 2. And when I did it again I got a different order :-)

I commonly get a stack of comments like this when exchanging documents, and it helps to be able to read them in the order they were created.
User avatar
chazzo
Posts: 25
Joined: 2011-10-27 03:40:31
Location: Norfolk, UK

Re: "Go to Next Comment" fails with nested comments

Post by chazzo »

martin wrote:Philip already provided some elegant ways to do that with a macro, but here's another:

Code: Select all

Find All '(?:.|\n)+', 'E', '-ac'
Menu 'Paragraph Style:Comment'
That leverages the Find's "where" argument to only match text in the comment areas.
Thank you (and Philip for the previous simplification). That's very useful, and so are some of the other options for searching from a macro. I didn't realise, for instance, that this is the only way to search for a specific footnote reference.
martin wrote:I'd also point out that you don't necessarily need a macro for this. You can just place the selection into any comment, Select All to select all text in that comment, and Select All again to select all text in all comments. Then you can manipulate and format all comments however you like.
Aha! In a footnote, one round of Select All selects all text in all footnotes. Needing to apply it twice for comments is clever, but inconsistent?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: "Go to Next Comment" fails with nested comments

Post by phspaelti »

chazzo wrote:Aha! In a footnote, one round of Select All selects all text in all footnotes. Needing to apply it twice for comments is clever, but inconsistent?
Well, it's due to the difference in how they are handled. Footnotes all live together inside a single text object (and Nisus "cleverly" uses tabs, returns and page breaks to separate them, much to Kino's chagrin). But Comments and Table cells each form their own text object.

I assume the reason for the difference treatment has to do with how they need to be formatted. Footnotes all have to be formatted together, so one can figure out how much will fit on a page. But with table cells each cell forms a complete object and formatting of one should't affect the neighbours. For comments too it makes more sense to treat them as completely separate entities, even though independent formatting is perhaps not that crucial.
philip
User avatar
chazzo
Posts: 25
Joined: 2011-10-27 03:40:31
Location: Norfolk, UK

Re: "Go to Next Comment" fails with nested comments

Post by chazzo »

phspaelti wrote:Well, it's due to the difference in how they are handled. Footnotes all live together inside a single text object (and Nisus "cleverly" uses tabs, returns and page breaks to separate them, much to Kino's chagrin). But Comments and Table cells each form their own text object.
Thank you -- that makes perfect sense.

The footnotes in my current document were faultlessly created from comments using your own macro. I imagine converting footnotes to comments would be harder, if that's what Kino is struggling with.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: "Go to Next Comment" fails with nested comments

Post by martin »

chazzo wrote:Aha! In a footnote, one round of Select All selects all text in all footnotes. Needing to apply it twice for comments is clever, but inconsistent?
That's a valid point, and perhaps we should change it. Personally I find that the double "Select All" process makes more sense to me, and it gives the user more flexibility. The only downside is lower discoverability.

Philip is exactly right with his explanation on this, which he no doubt smartly deduced from his experience with the Nisus macro language:
phspaelti wrote:Footnotes all live together inside a single text object (and Nisus "cleverly" uses tabs, returns and page breaks to separate them, much to Kino's chagrin). But Comments and Table cells each form their own text object.

I assume the reason for the difference treatment has to do with how they need to be formatted. Footnotes all have to be formatted together, so one can figure out how much will fit on a page.
A lot of this is due to the way Nisus Writer leverages Apple's text layout engine, and the need to have footnote content flow between pages or lay out adjacent footnotes naturally.
Post Reply