Search code examples
pythonkde-plasmakate

How do I create a new file view in Kate with Python/Pâté?


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)?


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)