Seunghyun Yoo

Posts | Development | About

[EN] Toggle highlighting current selection for LibreOffice Writer

Problem

In LibreOffice Writer, there is no default keyboard shortcut for highlighting current selection. Tools > Customize > Keyboard can assign a customized keyboard shortcut. It seems that there are three relevant functions (two ‘Highlight Color’s, ‘Highlight Fill’) but none of them work in a way I expected.

Solution

I created a macro that reads the current cursor’s background color and set the background color accordingly.

sub SetHighlightCurrentSelection(optional backColor)	
	dim document as object 
	dim dispatcher as object
	document = ThisComponent.CurrentController.Frame
	dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")	
	dim args1(0) as new com.sun.star.beans.PropertyValue
	args1(0).Name = "BackColor"
	args1(0).Value = backColor
	dispatch = dispatcher.executeDispatch(document, ".uno:CharBackgroundExt", "", 0, args1())
end sub

sub ToggleHighlightCurrentSelection
	cursor = ThisComponent.CurrentController.ViewCursor
	if cursor.CharBackColor = -1 then
		rem yellow
		call SetHighlightCurrentSelection(16773632)
	else
		call SetHighlightCurrentSelection(None)
	end if
end sub