Search code examples
libreoffice-basichunspell

A macro to remove the first space


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".


Solution

  • 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