3

Is it possible to write a macro for the problem explained in this discussion?

https://ask.libreoffice.org/t/can-you-auto-delete-the-space-before-an-auto-corrected-word/101757

If the following sequence is found, remove the first space.

<space>bys<space>

and keep only the second one. like this...

bys<space>

In other words, if " ed " is found, then remove the first space and join it with the earlier word. If that word is "work" then the new word will be "worked".

asked Feb 9, 2024 at 3:04
2
  • I don’t think that a regular macro will cope with the task, which should be solved using find " ed " and replace ed , with the "Find next" and "Replace" buttons Commented Feb 9, 2024 at 9:27
  • After joining the suffix with the earlier word, the new word should be a correct english word for e.g. worked Therefore hunspell tag added to the question. bugs.documentfoundation.org/show_bug.cgi?id=159652#c10 Commented Apr 16, 2024 at 9:11

1 Answer 1

2
+50

I visited the original post you referenced. The problem is that the author has a list of suffix that they would like to autocorrect and then join. However, because the words that need to join with suffix is vast, it would not be easy to define all combinations. In addition, it appears that autocorrect can't match starting with a space character. Here is a quick and easy macro that could do the job. Simply add more entries inside the ReplaceList Sub. When done, execute the ReplaceList sub.

Sub ReplaceList
 PolishReplace("bys", "byś")
 ' add more entries behind this
End Sub
Sub PolishReplace(sinFind,ドル sinRep$)
 REM Usage: 
 REM PolishReplace("Find string", "Replacement string")
 REM Will search the "Find string" with at least one space in front
 REM and replace with the Replacement string
 rem ----------------------------------------------------------------------
 rem define variables
 dim document as object
 dim dispatcher as object
 rem ----------------------------------------------------------------------
 rem get access to the document
 document = ThisComponent.CurrentController.Frame
 dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
 
 rem ----------------------------------------------------------------------
 dim args1(21) as new com.sun.star.beans.PropertyValue
 args1(0).Name = "SearchItem.StyleFamily"
 args1(0).Value = 2
 args1(1).Name = "SearchItem.CellType"
 args1(1).Value = 0
 args1(2).Name = "SearchItem.RowDirection"
 args1(2).Value = true
 args1(3).Name = "SearchItem.AllTables"
 args1(3).Value = false
 args1(4).Name = "SearchItem.SearchFiltered"
 args1(4).Value = false
 args1(5).Name = "SearchItem.Backward"
 args1(5).Value = false
 args1(6).Name = "SearchItem.Pattern"
 args1(6).Value = false
 args1(7).Name = "SearchItem.Content"
 args1(7).Value = false
 args1(8).Name = "SearchItem.AsianOptions"
 args1(8).Value = false
 args1(9).Name = "SearchItem.AlgorithmType"
 args1(9).Value = 1
 args1(10).Name = "SearchItem.SearchFlags"
 args1(10).Value = 65536
 args1(11).Name = "SearchItem.SearchString"
 args1(11).Value = "[ ]+" & sinFind
 args1(12).Name = "SearchItem.ReplaceString"
 args1(12).Value = sinRep
 args1(13).Name = "SearchItem.Locale"
 args1(13).Value = 255
 args1(14).Name = "SearchItem.ChangedChars"
 args1(14).Value = 2
 args1(15).Name = "SearchItem.DeletedChars"
 args1(15).Value = 2
 args1(16).Name = "SearchItem.InsertedChars"
 args1(16).Value = 2
 args1(17).Name = "SearchItem.TransliterateFlags"
 args1(17).Value = 1280
 args1(18).Name = "SearchItem.Command"
 args1(18).Value = 3
 args1(19).Name = "SearchItem.SearchFormatted"
 args1(19).Value = false
 args1(20).Name = "SearchItem.AlgorithmType2"
 args1(20).Value = 2
 args1(21).Name = "Quiet"
 args1(21).Value = true
 
 dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())
End Sub
answered Feb 17, 2024 at 3:31
Sign up to request clarification or add additional context in comments.

6 Comments

Very close. "bys" is replaced by "byś" and a space at the beginning is removed. But the question is slightly different. The resulting word should be a valid word as per spell check! After running your macro "do bys" becomes dobyś or "test bys" becomes "testbyś" but it does not check hunspell affix or dict file. This is very important.
With the way it's written now, I don't think it's possible to run the spell check on it as well because it does the search and replace in the entire document at once. The code will need to be rewritten to iterate over each replacement, and do spell check individually.
Ok. I understand that. But I do not need to change "bys" to "byś". Just make sure that the first space before the word is removed and the second space next to word is kept. There are about 8 to 10 such words (for e.g. 'ed', 'ing', 'bys').
This is working as expected. But can I use a single function instead of 2? gist.github.com/shantanuo/1ae004359c4aadfa67c514c0c9de9ba0
For your case, it could be simplified to a single function. You'll have to put what you want to replace inside a list, then put the main portion inside a loop. Should be pretty simple.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.