"""
Swap_Case.py - A Jython macro for jEdit that swaps the
case of selected text, or the entire buffer if no text
is selected.

Copyright 2003 (C) Ollie Rutherfurd <oliver@rutherfurd.net>

$Id: Swap_Case.py 28 2003-03-25 22:40:28Z oliver $
"""

def swapCase(textArea):
    textArea.getBuffer().beginCompoundEdit()
    try:
        selections = textArea.getSelection()
        if len(selections) == 0:
            textArea.setText(textArea.getText().swapcase())
        else:
            for sel in selections:
                start,end = sel.start,sel.end
                text = textArea.getText(start, end-start)
                textArea.getBuffer().remove(start, end-start)
                textArea.getBuffer().insert(start, text.swapcase())
    finally:
        textArea.getBuffer().endCompoundEdit()


if __name__ in ('__main__','main'):
    swapCase(init.textArea)

# :indentSize=4:lineSeparator=\n:noTabs=false:tabSize=4:

