1

In Libre-Office, is it possible to edit a selection with an external editor, such as Vim or Emacs?

To write this, I call Vim from Qutebrowser, i.e. mark the window and press ctrl+e. That opens an instance of Vim with whatever was in the window. When I close the buffer, its new contents are put into the window in Qutebrowser. Something similar in Libre-Office would help.

I ask, not because I want to use Libre-Office, but because I have to collaborate with someone who uses Word.

PS. Ideally, italics should be converted to something that can be edited in plain text, such as \it{this}.

7
  • Copy and paste? Nov 25, 2017 at 5:03
  • @Scott, yes, but that's not ideal.
    – Toothrot
    Nov 25, 2017 at 9:38
  • @fixer1234, is it the what or the why that is unclear to you? have you tried vim?
    – Toothrot
    Nov 25, 2017 at 11:24
  • As someone who uses both Vim and LO every day, it sounds like a good idea to me.
    – Jim K
    Nov 25, 2017 at 17:01
  • @fixer1234: Toothrot edited the question minutes after responding to my comment.   ISTM that revision explains what the OP wants.   By comparison, copy and paste would be cumbersome and error-prone. Nov 25, 2017 at 21:51

1 Answer 1

1

Use the following Python macro. Go to Tools -> Customize to run it at the press of a keystroke.

import os
import tempfile
import uno

def edit_with_vim():
    doc = XSCRIPTCONTEXT.getDocument()
    oVC = doc.getCurrentController().getViewCursor()
    data = oVC.getString()
    encoded_data = data.encode("utf8")
    fileTemp = tempfile.NamedTemporaryFile(delete = False)
    fileTemp.write(encoded_data)
    fileTemp.close()
    os.system('gvim -c "set encoding=utf8" %s' % (fileTemp.name))

g_exportedScripts = edit_with_vim,

EDIT:

After browsing the link above, see https://forum.openoffice.org/en/forum/viewtopic.php?f=74&t=12882 for a tutorial on customizing a key command to run the macro.

EDIT 2:

This code sends changes back to Writer.

import io
import os
from subprocess import call
import sys
import tempfile
import uno

def edit_with_vim():
    doc = XSCRIPTCONTEXT.getDocument()
    oVC = doc.getCurrentController().getViewCursor()
    textstring = oVC.getString()
    text_bytes = textstring.encode("utf8")
    tf = tempfile.NamedTemporaryFile(delete = False)
    tempfilename = tf.name
    tf.write(text_bytes)
    tf.close()
    if os.name == 'nt':
        GVIM = "C:/Windows/gvim.bat"
    else:
        GVIM = "/usr/bin/gvim"
    call([
        GVIM, "-f",
        "-c", '"set encoding=utf8"',
        tempfilename])
    with io.open(tempfilename, 'r+b') as fh:
        fh.seek(0)
        edited_bytes = fh.read()
    os.unlink(tempfilename)
    edited_string = edited_bytes.decode("utf8")
    edited_string = edited_string.strip()
    oVC.setString(edited_string)

g_exportedScripts = edit_with_vim,
10
  • Thanks, but it isn't obvious to me how to proceed after clicking /customize/.
    – Toothrot
    Nov 25, 2017 at 22:14
  • @Toothrot: See edited answer.
    – Jim K
    Nov 26, 2017 at 9:24
  • I don't have anything in macros/user/standard but module1, so the instruction for binding a key doesn't work.
    – Toothrot
    Nov 26, 2017 at 10:25
  • Did you see the link that says "Python macro" above? It does work, but you must follow the instructions. If you have already put the .py file in the correct location but it does not appear under "My Macros", then be sure that python-uno is installed. See christopher5106.github.io/office/2015/12/06/… for a tutorial.
    – Jim K
    Nov 26, 2017 at 10:59
  • Trying to, but so far I can't find the directories. I have no directory in ~ that's called .openoffice something or .libreoffice something.
    – Toothrot
    Nov 26, 2017 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .