Reply to topic  [ 10 posts ] 
Deleting the line you are in 
Author Message

Joined: 2007-04-12 14:59:36
Posts: 229
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)?


2009-04-12 07:50:05
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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:
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.


2009-04-13 07:43:54
Profile

Joined: 2007-04-12 14:59:36
Posts: 229
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.


2009-04-13 12:02:01
Profile

Joined: 2007-03-03 09:55:06
Posts: 494
Location: Europe
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.


2009-04-14 07:54:23
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
@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:
Find and Replace '.+', '_Line_To_Be_Deleted_', 'Esa'
Find and Replace '^.*_Line_To_Be_Deleted_.*(\n|$)', '', 'Ea'

_________________
philip


2009-04-15 18:49:13
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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:
Quote:
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.


2009-04-15 20:12:12
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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


2009-04-16 01:56:42
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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:
$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:
$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.


2009-04-16 03:24:19
Profile
User avatar

Joined: 2007-02-07 00:58:12
Posts: 876
Location: Japan
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


2009-04-16 07:35:06
Profile

Joined: 2008-05-17 04:02:32
Posts: 400
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:
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:
Send Selector 'selectParagraph:'
Send Selector 'delete:'


2010-09-15 08:05:22
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 10 posts ] 

Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software