OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

how to find a paragraph break?
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
tompaa
General User
General User


Joined: 18 Jun 2004
Posts: 12
Location: Zagreb, Croatia

PostPosted: Tue Nov 29, 2005 6:12 am    Post subject: how to find a paragraph break? Reply with quote

hello;

I want to delete all paragraph breaks inside a selection - basicaly to merge paragraphs in selection into one paragraph.

well, there is a metod "insertControlCharacter" to insert paragraph break, but how to find and/or replace one?

thanks...
Back to top
View user's profile Send private message
Gabor
Super User
Super User


Joined: 21 Sep 2003
Posts: 610
Location: Hungary (E-Europe)

PostPosted: Tue Nov 29, 2005 8:52 am    Post subject: regexp or else Reply with quote

Minimalist answer:
regexp is the terrible keyword (regular expressions). Look for it in help.
Short answer:
put $ in Find box
Click Regular expressions (in OOo2 click more first)
put a space or anything you want to substitute for the end of paragraph mark into the Replace box
OK
Long answer:
If you wish to search for such control characters regularly I would suggest to download Ian Laurenson's:
http://homepages.paradise.net.nz/hillview/OOo/IannzFindReplace.sxw
which is a Writer file with a macro embedded. By that you can easily change almost anything you wish.The site itself is this:
http://homepages.paradise.net.nz/hillview/OOo/
You may find it useful to check out other macros there.
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Tue Nov 29, 2005 9:06 am    Post subject: Reply with quote

Well Gabor's short answer will work for you but I thought you were looking for a code example.
Code:
Sub Main
oDoc = thisComponent
oVC = oDoc.CurrentController.getViewCursor
If Len(oVC.String) = 0 then Print "No Selected text!" : End
oStart = oDoc.Text.createTextCursorByRange(oVC.Start)
oEnd = oDoc.Text.createTextCursorByRange(oVC.End)
FandR = oDoc.createReplaceDescriptor
FandR.searchRegularExpression = true
REM 1st find blank paragraph and eliminate so we don't
'end up with muliple spaces.
FandR.setSearchString("^$")
Find = oDoc.FindNext(oStart,FandR)
Do While Not isNull(Find)
If oDoc.Text.compareRegionEnds(Find,oEnd) <> 1 then Exit Do
Find.String = "" 
Find.goLeft(1,false)
Find = oDoc.FindNext(Find.End,FandR)
Loop
REM Then replace other paragraph breaks with a space.
FandR.setSearchString("$")
Find = oDoc.FindNext(oStart,FandR)
Do While Not isNull(Find)
If oDoc.Text.compareRegionEnds(Find,oEnd) <> 1 then Exit Do
Find.String = " " 
Find.goLeft(1,false)
Find = oDoc.FindNext(Find.End,FandR)
Loop
End Sub
Back to top
View user's profile Send private message
tompaa
General User
General User


Joined: 18 Jun 2004
Posts: 12
Location: Zagreb, Croatia

PostPosted: Tue Nov 29, 2005 11:48 pm    Post subject: Reply with quote

thank guys. i've learn a bit more.
@ gabor: mmm, a bit confusing OO help: in oo help there is explanation for "$" just as "Only finds the search term if the term appears at the end of a paragraph." anyway, i'll have to become more familiar with regexp... thnx...

@ JohnV: thanks for the code; it works (of course), and it is helpful: there are good things to leran from it...
Back to top
View user's profile Send private message
PascalCRB
Newbie
Newbie


Joined: 20 Sep 2005
Posts: 3

PostPosted: Sat Apr 15, 2006 3:48 am    Post subject: finding carriage returns in Write Reply with quote

I'm using a Dutch version (interface) of OO ...
On my system the $ sign for finding paragraph signs does not work (nor does any other special character, like e.g. \t for finding tabs)

Help ?
Back to top
View user's profile Send private message
JohnV
Administrator
Administrator


Joined: 07 Mar 2003
Posts: 9183
Location: Lexinton, Kentucky, USA

PostPosted: Sat Apr 15, 2006 6:57 am    Post subject: Reply with quote

PascalCRB,

Have you checked the Regular Expressions box (under More Options in version 2)?
Back to top
View user's profile Send private message
PascalCRB
Newbie
Newbie


Joined: 20 Sep 2005
Posts: 3

PostPosted: Wed Apr 19, 2006 5:21 am    Post subject: finding & replacing Regular Expressions Reply with quote

Thanks for the useful hint...
I finally discovered the <more options> button, which appeared to be on the "Find & Replace" popup-window, and ... "check" means "select the chack box" rather than "verify & look whether you find regular expressions somewhere in the help function".

=> it works !! long live OpenOffice
Back to top
View user's profile Send private message
vicjoe
Newbie
Newbie


Joined: 02 Sep 2007
Posts: 3

PostPosted: Sun Sep 02, 2007 5:22 pm    Post subject: replace 2 cr's with one replace all Reply with quote

My goal: replace two carriage/paragraph returns with just one across an entire document (Replace All). I'm talking about the ones that display as a mirrored capital P with two stems, not the soft returns that are represented by a bent arrow.

What I've tried (in all cases 'more options' and checking 'regular expression' was done):
In Find field: $
In Replace field (nothing)
result: all paragraph returns are removed.

In Find field: $$
In Replace field (nothing)
result: "Search Key not Found" [grrr]

In Find field: $$
In Replace field: \n
result: "Search Key not Found"

In Find field: \n\n
In Replace field \n
result: "Search Key not Found"

In Find field: $^$
In Replace field (nothing)
result: "Search Key not Found"

In Find field: $^$
In Replace field: \n
result: "Search Key not Found"

I may have tried other combinations, but I've concluded that if one has a document where paragraphs are separated by two paragraph returns, it appears impossible to reduce the two to one paragraph return only per occurrence, unless I've missed something.

This is frustrating to me since, as and editor, it is one of the most common things I do when formatting peoples' papers where they don't have Invisibles turned on and use a combination of the return key and space bar to get the document looking the way they think it should. Since 1988, it has been a simple matter in MS Word to just Find: ^p^ Replace: ^p click Replace All, and it is done. Is this task possible in OOo v. 2.1?
Back to top
View user's profile Send private message
vicjoe
Newbie
Newbie


Joined: 02 Sep 2007
Posts: 3

PostPosted: Sun Sep 02, 2007 5:25 pm    Post subject: crucial typo in last line Reply with quote

Quote:
a simple matter in MS Word to just Find: ^p^ Replace: ^p click Replace All, and it is done.


That should read Find: ^p^p Replace: ^p click Replace All, and it is done.
Back to top
View user's profile Send private message
tompaa
General User
General User


Joined: 18 Jun 2004
Posts: 12
Location: Zagreb, Croatia

PostPosted: Sun Sep 02, 2007 11:43 pm    Post subject: Re: crucial typo in last line Reply with quote

vicjoe wrote:

That should read Find: ^p^p Replace: ^p click Replace All, and it is done.


hey vicjoe;
i don't know if it is possible find/replace like M$Word (it would be very helpfull).
but simple "workaround" is clearing all "empty" paragraphs.

so i made simple script: first ensure that there are no spaces before cr/lf, and than replace all empty paragraphs.

Code:
Sub dSpaces()
   dim zamijeni, Found, xFound, Cursor as object
      
   Cursor=mainDoc.Text.createTextCursor()
   zamijeni = mainDoc.createReplaceDescriptor()
   
   with zamijeni
      .SearchCaseSensitive = True
      .SearchRegularExpression = True
      .SearchString = "^ "
      .ReplaceString= ""
   end with
   mainDoc.replaceAll(zamijeni)
   
   zamijeni.SearchString = " $"
   mainDoc.replaceAll(zamijeni)   
   
   Cursor.gotoStart(False)
   xFound = mainDoc.findFirst(zamijeni)
   Do While not isNull(xFound)
      Found = mainDoc.findFirst(zamijeni)
      Do While not isNull(Found)
         mainDoc.replaceAll(zamijeni)
         Found = mainDoc.findNext(Found.End, zamijeni)
      Loop
      Cursor.gotoStart(False)
      xFound = mainDoc.findFirst(zamijeni)
   Loop
End sub

Sub dEnter()
   dim zamijeni as object
   
   zamijeni = mainDoc.createReplaceDescriptor()
   
   with zamijeni
      .SearchCaseSensitive = True
      .SearchRegularExpression = True
      .SearchString = "^$"
      .ReplaceString= ""
   end with
   mainDoc.replaceAll(zamijeni)
end sub


****
above code isn't optimised (nor example of good programming) since i am not programmer, but hope it will help.

have a nice day,
tompa
Back to top
View user's profile Send private message
vicjoe
Newbie
Newbie


Joined: 02 Sep 2007
Posts: 3

PostPosted: Mon Sep 03, 2007 12:25 am    Post subject: tompaa's script Reply with quote

Quote:
simple "workaround" is clearing all "empty" paragraphs.

so i made simple script: first ensure that there are no spaces before cr/lf, and than replace all empty paragraphs.


The empty paragraph routine already worked, but as you've deduced there is a problem with spaces before, and I would add, also after cr/lf. The routine will miss any cr/lf that have spaces after, as it is regarded as text and not empty. You would not believe the number of peoples' documents I get to edit wherein they have this compulsive typewriter habit of hitting the space bar a couple of times every time they hit the return key, let alone every time they type a period..

I'm actually running OOo 2.3 Alpha 26 August build for Mac at the moment; I didn't know it was scriptable. Crying or Very sad I'll give it a try, as well as adding for trailing spaces, and if successful will post results. If it isn't amenable to AppleScript, I'm adrift, as I have no other scripting experience, but am willing to learn if you have a URL for dummies.

Since by default searches are paragraph-based so there is no way of searching for text on either side of a paragraph marker, this is a challenge that you've started to address.[/i]
Back to top
View user's profile Send private message
jwr
OOo Advocate
OOo Advocate


Joined: 06 Sep 2006
Posts: 367
Location: Germany

PostPosted: Mon Sep 03, 2007 3:34 am    Post subject: Re: how to find a paragraph break? Reply with quote

tompaa wrote:
hello;
I want to delete all paragraph breaks inside a selection - basicaly to merge paragraphs in selection into one paragraph.


Hi Tompaa,

->> Be careful not to exceed the 64k-paragraph limit!
http://www.oooforum.org/forum/viewtopic.phtml?t=58006&highlight=paragraph+limit+64k
http://www.oooforum.org/forum/viewtopic.phtml?t=61381&highlight=paragraph+limit+64k
http://qa.openoffice.org/issues/show_bug.cgi?id=17171

Regards -Hans-
_________________
Windows 2K, OOo-2.3.0, MS-IE-6.0, Firefox-1.0.4, MS Office 97,
Say NO to MS OpenXML: http://www.noooxml.org/petition


Last edited by jwr on Thu Nov 29, 2007 3:09 am; edited 2 times in total
Back to top
View user's profile Send private message
oop
General User
General User


Joined: 21 Sep 2007
Posts: 14

PostPosted: Fri Sep 21, 2007 5:59 am    Post subject: Re: replace 2 cr's with one replace all Reply with quote

vicjoe wrote:
My goal: replace two carriage/paragraph returns with just one
You should search for an empty paragraph (which will select the second of any pair of carriage returns), and replace it with nothing (which will leave only the first carriage return).

In general:

To search for Tabs or Carriage Returns, the "Regular Expressions" box in the "More Options" part of the Search/Replace palette must be checked. This must be done every time you open the palette, because Open Office doesn't remember the setting.

To search for or replace with a Tab, use \t

To search for a Carriage Return, use $
To replace with a Carriage Return, use \n

(Long, complicated, unnecessary explanation of Carriage Returns:
There are two types of Carriage Returns
The normal Carriage Return created by pressing Enter is a 'Paragraph Break'
The other one, created by pressing Shift-Enter, is a 'Line Break'
To search for a Paragraph Break, use $
To search for a Line Break, use \n
To replace with a Paragraph Break, use \n
To replace a Line Break with a Paragraph Break, use \n for both Search and Replace)

To search for two Carriage Returns with nothing in between, use ^$
(That actually selects only the second of the two Carriage Returns)
To change two Carriage Returns to one, search for ^$ and replace with nothing
(That deletes the second of two carriage returns)
Back to top
View user's profile Send private message
marcosaluzzo
Newbie
Newbie


Joined: 24 Sep 2007
Posts: 2

PostPosted: Mon Sep 24, 2007 1:44 am    Post subject: Reply with quote

And if I want to substitute a paragraph break with a line break, how do you do that? I have tried putting search $ replace $, search $ replace \n, search \n replace \n but without success. Any help would be most appreciated.
Back to top
View user's profile Send private message
oop
General User
General User


Joined: 21 Sep 2007
Posts: 14

PostPosted: Mon Sep 24, 2007 4:52 am    Post subject: Reply with quote

I don't think there's any way to use a line-break as the Replace string, but someone at another thread has posted code for a programming routine to replace paragraph-breaks with line-breaks.

The way I would do it is with a keyboard macro, since I have a keyboard macro editor that I use constantly all day long. It takes about a half-hour to download, install, and learn to use a macro editor, and then you can use it to do almost any custom task in any program. If you want to try it, most macro editors can be used for free for 30 days. There are probably some freeware ones. One example of a shareware macro editor is Keyboard Express ($25 after 30 days). To use it, you just press Ctrl-Alt-Shift-X, then type the sequence of keystrokes that does what you want to do, then press Ctrl-Alt-Shift-X again, then assign a name and a hot-key to the macro. Then whenever you press that hot-key in that program, the sequence of keystrokes is repeated. So you could record crtl-F, $, alt-O, alt-X, enter, escape, shift-enter (which will find the next paragraph-break and replace it with a line-break), then assign it to function key F5, and then each time you press F5 the next paragraph break would be replaced with a line break. If you want to replace all of them, and there are hundreds in the document, you could then record the pressing of F5 20 times, and assign that macro to F6, and then you would only have to press F6 once to replace 20 paragraph-breaks.

And after writing all that, I've just noticed that Open Office has a built-in keyboard macro editor, at Tools > Macros > Record Macro. But I don't know how to use it. It may be worth your while to learn that.

I have a question about this, too. When do you use a line-break instead of a paragraph-break? How are they different? I've never used one, and I can't find anything in the Help that explains the difference.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group