tadhg.com
tadhg.com
 

jEdit Macros in Python

16:57 Fri 17 Jul 2009. Updated: 17:52 18 Jul 2009
[, , , , ]

jEdit has long been my text editor of choice, and I’m using it more than ever now that I’m writing more or less everything in it. I’ve been waiting a while for 4.3 to come out, but overall I remain quite happy with it. I do occasionally wonder about switching to vim or Emacs, but jEdit’s generally been able to do whatever I wanted it to.

I haven’t done much scripting with it, though. I recently came up with some use cases for scripts—involving reStructuredText, naturally—but I was a little reluctant to do the scripting because it involves Java and I really want to keep my current focus on Python and JavaScript.

I was therefore rather happy to discover the existence of the JythonInterpreter plugin, which makes it possible to write macros for jEdit in Python.

I grabbed it and to try it out wrote a macro that looks for all the substitutions and link references in a file, then places corresponding lines at the bottom. That is, if you had the following reStructuredText:

`jEdit`_ has long been my text editor of choice, and I'm using it more than ever now that |I'm writing more or less everything in it|.

and you ran this script, you’d have this:

`jEdit`_ has long been my text editor of choice, and I'm using it more than ever now that |I'm writing more or less everything in it|.

.. |I'm writing more or less everything in it|
.. _jEdit:

Thus saving me the hassle of copying and pasting all of them manually. It works pretty well so far. Here’s the source if any jEdit and reStructuredText people are reading this (remember that you need the JythonInterpreter plugin to make it work):

def get_references():
    """
    jEdit macro that finds all of the substitutions and links in the file and places corresponding references to them at the bottom of the file.
    """
    import re

    # get local reference to textArea
    textArea = view.getTextArea()

    # current caret:
    caret = textArea.getCaretPosition()

    lines = textArea.getText().split('\n')

    #Substitutions:
    subs = {
        "find": re.compile("\|[^\|]+\|"),
        "replace": lambda hit: ".. %s " % hit
    }
    #Links:
    lrefs = {
        "find": re.compile("`[^`]+`_"),
        "replace": lambda hit: ".. _%s: " % (hit[1:-2])
    }

    #Citations and footnotes:
    frefs = {
        "find": re.compile("\[[^`]+\]_"),
        "replace": lambda hit: ".. %s " % (hit[0:-1])
    }

    #the function to run for each operation:
    def run_ops(lines, opdict, matchlist):
        for line in lines:
            if not line.startswith(".. "):
                search = opdict["find"].findall(line)
                for hit in search:
                    hit = opdict["replace"](hit)
                    matchlist.append(hit)
        return matchlist

    #run the operations in desired order:
    matches = []
    for op in [subs, lrefs, frefs]:
        run_ops(lines, op, matches)

    #Put the matches into a string:
    mstring = "\n".join(matches)
    mstring = "\n\n%s" % mstring

    #Go to the end of the buffer and insert the string:
    textArea.goToBufferEnd(0)
    endcaret = textArea.getCaretPosition()
    textArea.getBuffer().insert(endcaret, mstring)

    #Go back to the original cursor position:
    textArea.setCaretPosition(caret)

if __name__ == '__main__':
    get_references()

2 Responses to “jEdit Macros in Python”

  1. Jason Says:

    Hey Thanks for posting this. I started using Jedit for writing Python and I love it!
    There are some macros I would like to write in Python, but I can’t seem to get JythonInterpreter to work correctly.

    Have you had any trouble?

    Jedit 4.3
    Windows xp sp3
    Python 2.6.1
    jdk1.6.0_17
    jre6
    javafx-sdk1.2
    Jython2.5.1

    Here’s the error when I try to run any Python Macro (including yours):

    Jython Error:
    an AWT error occured running Jython
    Traceback (innermost last):
    (no code object) at line 0
    TypeError: readonly class or attribute: view

    Thanks in advance

  2. Tadhg Says:

    I haven’t upgraded to jEdit 4.3 yet, so I haven’t run into that problem. It’s fairly recent, while the JythonInterpreter plugin is from 2006, so I’m not that surprised that there are issues. I’d try asking on the jEdit forums. I’m hoping that the authors of the JythonInterpreter plugin make sure it works—and update to Jython 2.5.

    I’ll comment here when I do upgrade, hopefully with whatever tip is necessary to make the plugin work with 4.3.

Leave a Reply