Ask Your Question
0

For any Macro programmer a cinch - I need one simple editing macro

asked 2013-12-14 11:03:38 +0100

Ioshka gravatar image

updated 2013-12-14 12:24:09 +0100

David gravatar image

Hello. I've just recently installed LibreOffice and so far am thoroughly delighted. I'm a translator so my job entails a lot of editing. I read the original (Italian), tap in the English and cancel the Italian... and so on and so forth till the end of the document.

Using Macro Record, I've made a complete set of keyboard-driven editing macros - that is complete except for the one and only one that requires a variable.

In short this is what I need and I'm sure that for anyone with programming smarts, it's a no-brainer.

I need the cursor to go back one step to the left, highlighting the character directly before its position [SHIFT LEFT-ARROW], memorize that single highlighted character (letter, number, punctuation mark), case INsensitive, into a variable, then hop back in place [ARROW RIGHT] and from there forward cancel all the characters up to and including the memorized character.

In old (ancient) Word Basic, automatically converted to Microsoft's VB... the operation looked like this:

Public Sub MAIN()
Dim WORD$
WordBasic.CharLeft 1, 1
WORD$ = WordBasic.[Selection$]()
WordBasic.CharRight 1
WordBasic.ExtendSelection
WordBasic.WW2_EditFind Find:=WORD$, WholeWord:=0, MatchCase:=0, Direction:=1 _
, Format:=0
WordBasic.EditCut
End Sub

To further illustrate what I need, imagine this sentence:

Mary went to the gym and then to Bob's house.

and Imagine that I wanted to cut it down to:

Mary went to Bob's house.

That would mean cutting out "the gym and then to"

With the macro I'm asking someone merciful to create for me that would mean:

Tapping in a "B" after "Mary went to " and hitting the assigned key combination.

The macro would "taste" and memorize the "B", jump back in place and kill all the text up to and including the next "B" (or "b") it finds.

Thank you!!!

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2015-07-20 21:46:49 +0100

doug gravatar image

Here is a macro that functions as you request. The match is case sensitive, and will monitor the position of the cursor, copy the character to the left, and then iterate to the right deleting characters until it reaches a match for the character to the left. The deletions can be reversed by ctrl-z.

Sub MemCharDeleteToRight

txtObj = ThisComponent.getText
cntrllr = ThisComponent.CurrentController
vcur = cntrllr.ViewCursor

If vcur.isCollapsed = False Then
  MsgBox "Collapse the selection and put cursor to the right of the memorized character to continue."
  Exit Sub
End If

vcur.goLeft(1, True)
memStr = vcur.string
vcur.setString("")

endTrigger = False

Do Until endTrigger = True
  vcur.goRight(1, True)
  selStr = vcur.String
  If selStr <> memStr Then 
    vcur.setString("")
  Else
    endTrigger = True
  End If
  vcur.collapseToEnd
Loop

End Sub
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-12-14 11:03:38 +0100

Seen: 248 times

Last updated: Jul 20 '15