Showing posts with label LibreOffice. Show all posts
Showing posts with label LibreOffice. Show all posts

Wednesday, July 8, 2026

Configure Web Browser to be More Emacs-like

Emacs, LibreOffice Writer and Vivaldi are the productivity tools I use most often. So I like to incorporate a feature from one into the others. One example is the ability to initiate a search from selected text. I added this feature to LibreOffice as described here. Another nice feature is the ability to switch between the current buffer and the next (or previous) buffer with C-<TAB> and C-S-<TAB> that I added to Emacs1.

But I've also configured Vivaldi with a useful Emacs feature. Now it responds to C-S-k by invoking "Close tabs to the Right." Emacs devotees know that C-k is bound to kill-line, which "Kill[s] the rest of the current line...."2 If you imagine that the row of tabs is a line of text, it's natural to expect C-k (or C-S-k) would remove all the tabs to the right of the current tab.

Why did I choose the key combination C-S-k instead of C-k? I think at the time C-k performed another function. Or perhaps I felt that it was more appropriate to use two modifier keys for such a significant operation. I rarely need to delete tabs to the left; if I did, I'd bind C-M-S-k to "Close tabs to the Left."

You might utter the following complaint, "But C-S-k doesn't work for Gmail; it brings up a virtual keyboard instead!" Correct. So while we're on the topic of Vivaldi key configurations, I'd like to point out that it's possible to tell Vivaldi to ignore a website's key assignment, allowing the key to pass to Vivaldi. Just search for the setting "Browser Priority Shortcuts," click on one of the existing key combinations and press C-S-k to add it to the others.

I rely on the web browser's tab history a great deal, too. I'm working on implementing something vaguely similar for Emacs."


1 To get the C-<TAB> and C-S-<TAB> to switch buffers, just add this to your init file:

  (keymap-global-set "C-<tab>" 'next-buffer)
  (keymap-global-set "C-S-<tab>" 'previous-buffer)

2 https://www.gnu.org/software/emacs/manual/html_node/emacs/Killing-by-Lines.html

Friday, July 3, 2026

Circumvent LibreOffice Macros with Templates

Instead of writing LibreOffice macros to change a default behavior, consider customizing the default template.

Creating macros is fun for me. But I agree, automating LibreOffice with macros is daunting, and the recorder is a poor way to begin writing code.1

Recently I wanted to change the default anchor position of an image. When I insert an image by pasting, I want its anchor is to be "As Character" as opposed to paragraph, which was the current default.

I tried to record the process:

  • Paste
  • F4 (properties)
  • As Character
  • OK
  • Press Enter

The recorder captured the Paste and Enter keys well. Unfortunately, it choked on the macro's primary element -- applying the As Character property.

But then I wondered, "Can the default anchor position be set to "As Character" in Options?" "No, it cannot," I answered.2 "But you can change it in the default template. Here's how:"

  • Start LibreOffice Writer. This should present a new, blank document. But if it doesn't, press Ctrl+N.
  • Press Alt+2 to open the Styles sidebar.
  • Click the Frame Styles icon. (Please see Figure 1, Frame Styles below.)
  • An image of the sidebar in which Styles is selected.  The Frame Styles button has been clicked, and the mouse pointer hovers over on the icon so that the "Frame Styles" tooltip shows.
    Figure 1 -- Frame Styles

  • Right click on Graphics and choose Edit Style... as shown in Figure 2.
  • Image shows how the interface appears after right-clicking the Graphics style element.  The context menu shows three choices: New, Edit Style and Hide
    Figure 2 -- Edit Graphics Style

  • Choose "Type" on the left. Its icon looks an anchor.
  • Under Anchor, click the radio button next to As character.
  • Review the other settings and change whatever other defaults that don't make sense to you. I recommend clicking the checkbox to lock Height to Width (aka Aspect Ratio in other applications). Mine was unchecked, and I always found myself clicking it.
  • Choose File | Templates | Save as Template as shown in Figure 3. Note this is different from doing File | Save As and then choosing the OTT file type. And yes, you will be saving the image in the template, but it can be removed later.
  • The image shows the three options after clicking File and then Templates from the menu
    Figure 3 -- Save as Template Menu Selection

  • Enter a name for the template. I entered DEMO for this How-To, but I tend to give my templates more description names that include the paper size, such as "4 x 6 index card" or "5 x 7 greeting card LS."
  • Choose a category to enable the Save button.
  • Click the checkbox labeled "Set as default template" to ensure that every subsequent new Writer document will be derived from the new template.
  • Note that these three choices (Name, Category, Default) can be changed in the Template Manager (Ctrl+Shift+N). The dialog should look similar to the one in Figure 4, below.

  • The image is a screenshot of the Save Template Dialog box, where the template name is "DEMO," the category "My Templates" is selected and the checkbox indicates that it should be set as the default template
    Figure 4 -- The Save Template Dialog Box


  • Press Ctrl+N to create a new document. Paste an image into it to test the new template. Hopefully3 you'll get the desired result.

1 "When the macro recorder is not able to solve a specific problem, the usual solution is to write code using the OpenOffice.org objects. Unfortunately, there is a steep learning curve for the OOo objects."
2 Sometimes I gain insight into a problem by splitting myself into student and teacher.
3 I write "Hopefully" because as I repeated these steps to test them, I managed to obliterate my own default template. And I couldn't seem to fix it without restarting Writer.

Wednesday, June 24, 2026

Add Web Search to LibreOffice Writer

Introduction

In Vivaldi1 I can right click on a word or selected phrase and search for it. But in LibreOffice Writer, I'd have to copy the selection to my clipboard, switch to the browser, paste it into the search bar and then press Enter. I longed to have a generic search capability for Writer. So I created it.

Well, first I searched for it, and I found a macro that was designed to do something similar2. It provided the Basic code I needed to read the selected text.3 It had to be modified somewhat to work on my computer, but it was a tremendous help.

Then I built on it. The collection of macros that I wrote allows a user to perform the search using several search engines. And it's amenable to configuration in the Context Menu.

Workflow Summary -- Search the Internet for selected text
  • Select some text.
  • Right click on it.
  • Search the Internet for it.
  • Use any one of these these preconfigured search engines:
    • DuckDuckGo
    • Google
    • LinkedIn
    • Wikipedia
    • Open Library
    • The Free Dictionary
    • And you can add your own!

Detail

Web Search is a LibreOffice Basic module that contains the macros needed to perform an Internet search of selected text.

Program Flow

A unique macro is dedicated to each search engine. The user selects text and then invokes the macro that corresponds to the desired search engine.

The invoked macro sets an index that points to the search engine URL in an array. Then it invokes the Main subroutine.

Main calls an Initialize routine, which loads the path to the web browser executable and all the Search Engine URLs.

Main performs two checks. First it verifies that the search engine index is within the bounds of the array of search engine URLs. The out-of-the-box state is that the index should be in the range 0 to 5.

The second check is that Main verifies that text has been selected in the Writer document.

If and only if the previous two checks are successful, then Main calls the InvokeSearch routine; the search engine URL and the search terms are its arguments. If either check fails, Main and the invoked macro terminate; no action is performed.

InvokeSearch replaces a token in the search engine URL with the search terms. Then it calls the system shell, which opens the web browser with the conditioned search engine URL as an argument. The shell process is asynchronous.

The web browser takes focus and displays search results.

Finally, the three procedures (two subroutines and a function) are removed from the call stack, and the global variables are cleared.

Context Menu Setup

The LibreOffice Context Menu can be configured to provide easy access to each Search Engine macro. The user can right click on selected text, choose "Search on Web...," and then choose the submenu item that corresponds to the desired search engine. In Figure 1 below, the DuckDuckGo search engine is selected. Thus each Web Search submenu item is assigned a unique macro.

Figure 1: The SearchWeb Context Menu


Configure the context menu as shown in Figure 2, below. Access this dialog window by selecting Tools | Customize on the menu and then click the Context Menu tab.

Figure 2: Customizing the Context Menu


The corresponding XML content is contained in the text.xml file, which is located here in the user's profile:

AppData\Roaming\LibreOffice\4\user\config\soffice.cfg\modules\swriter\popupmenu\

The first 20 lines of text.xml are provided in the listing below. Note that the menu items for Open Library and The Free Dictionary are wrapped.

It may be easier to edit this file than to use the Customize form that's shown above.

Contents of text.xml

<?xml version="1.0" encoding="UTF-8"?>
-
<menu:menupopup xmlns:menu="http://openoffice.org/2001/menu">
  -
  <menu:menu menu:label="Search on Web..." menu:id="Search on Web...">
    -
    <menu:menupopup>
	<menu:menuitem menu:label="D DuckDuckGo" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEDuckDuckGo?language=Basic&location=application"/>
	<menu:menuitem menu:label="G Google" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEGoogle?language=Basic&location=application"/>
	<menu:menuitem menu:label="L LinkedIn" menu:id="vnd.sun.star.script:Standard.SearchWeb.SELinkedIn?language=Basic&location=application"/>
	<menu:menuitem menu:label="O Open Library" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEOpenLibrary?language=Basic&location=application"/>
	<menu:menuitem menu:label="F The Free Dictionary" menu:id="vnd.sun.star.script:Standard.SearchWeb.SETheFreeDictionary?language=Basic&location=application"/>
	<menu:menuitem menu:label="W Wikipedia" menu:id="vnd.sun.star.script:Standard.SearchWeb.SEWikipedia?language=Basic&location=application"/>
    </menu:menupopup>
  </menu:menu>
  <menu:menuitem menu:id=".uno:NoBreak"/>
  <menu:menuseparator/>
  <menu:menuitem menu:id=".uno:Cut"/>
  <menu:menuitem menu:id=".uno:Copy"/>
  <menu:menuitem menu:id=".uno:Paste"/>


1 Vivaldi, https://vivaldi.com/desktop/
2 Ratslinger's post in this thread on the LibreOffice help forum provided a solution way back in September 2018 https://ask.libreoffice.org/t/link-to-browser-in-writer/35943/6
3 Here's a function that returns the selected text; words in a phrase are separated+with+plus+signs+instead+of+spaces:
    Private Function GetSelection() As String
      ' Returns Selected Text
      ' 2025-12-22 Created
      Dim oSelections, oFirstSelection As Object
      oSelections = ThisComponent.CurrentSelection
      oFirstSelection = oSelections.getByIndex(0)
      strTemp = oFirstSelection.String
      If strTemp <> "" Then
        GetSelection = Replace(strTemp, " ", "+")
      End If
    End Function