Deleting the line you are in
Posted: 2009-04-12 07:50:05
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)?
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
Code: Select all
Find and Replace '.+', '_Line_To_Be_Deleted_', 'Esa'
Find and Replace '^.*_Line_To_Be_Deleted_.*(\n|$)', '', 'Ea'
I had forgotten ".noteAtIndex" command.Is there a simple way to get a note object to which a selection belongs, for example?
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
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
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.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.
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.Kino wrote:I think something like Select Current Paragraph, Select Current Page, Select Current List Item, etc. would be useful.
Code: Select all
Menu ':Edit:Select:Select Paragraph'
Menu ':Edit:Delete'
Code: Select all
Send Selector 'selectParagraph:'
Send Selector 'delete:'