Question

I'm working on a Kate plugin written in Python that generates a large amount of text too big to display it in a popup. So I want Kate to open a new unnamed file and display the text in it.

Is there a way to do this in Python (apart from running a subprocess echo text | kate --stdin)?

Was it helpful?

Solution

I found it out myself:

import kate
from kate import documentManager as dm
from PyKDE4.kdecore import KUrl


text = "Lorem ipsum dolor sit amet"

# Open a new empty document
doc = dm.openUrl(KUrl())
# Open an existing file
doc = dm.openUrl(KUrl('/path/to/file.ext'))

# Activate view
kate.application.activeMainWindow().activateView(doc)

# Insert text
pos = kate.activeView().cursorPosition()
doc.insertText(pos, text)

OTHER TIPS

You can use directly a pipe:

>>> f = os.popen("kate --stdin", "w")
>>> f.write("toto")
>>> f.close()

Now kate open a file with "toto" in it.

A more modern solution is to use the subprocess module:

>>> sp = subprocess.Popen(["/usr/bin/kate", "--stdin"], stdin=subprocess.PIPE, shell=False)
>>> sp.stdin.write("toto")
>>> sp.stdin.close()

As specified in the command, it doesn't use a shell.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top