"""
Write_Shortcuts.py - A Jython macro for jEdit that writes
shortcuts for jEdit's actions to a new buffer.

Copyright (C) 2003 Ollie Rutherfurd <oliver@rutherfurd.net>

$Id: Write_Shortcuts.py 54 2003-11-03 16:05:35Z oliver $
"""

from java.io import ByteArrayOutputStream
from java.util import Properties

# not actual shortcuts
IGNORE = (
    'options.shortcuts.shortcut2',
)

HEADER = """
# jEdit shortcuts extracted by "Write_Shortcuts.py".
#
# "Write_Shortcuts.py can be found here:
#
#   http://www.rutherfurd.net/jEdit/macros/
#
# Append the properties below to your jEdit properties
# file to use these for yourself only.
#
# Save to your "properties" directory in jEdit's 
# home directory with an extension of ".props" to
# have these shortcuts shared for all jEdit users
# on a machine.

"""


def writeShortcuts(view):
    shortcuts = Properties()
    props = jEdit.getProperties()
    names = props.propertyNames()
    for name in names:
        if name in IGNORE:
            continue
        if name.endswith('.shortcut') or name.endswith('.shortcut2'):
            value = props.getProperty(name)
            shortcuts.put(name,value)
    f = ByteArrayOutputStream()
    shortcuts.save(f, None)
    jEdit.newFile(view)
    # remove carriage returns from created string because
    # jEdit wants \n separated lines
    view.getTextArea().setText(f.toString().replace('\r', ''))
    view.getTextArea().getBuffer().setMode(jEdit.getMode('properties'))
    view.getTextArea().setCaretPosition(0)
    view.getTextArea().setSelectedText(HEADER)


if __name__ in ('__main__','main'):
    writeShortcuts(init.view)

# :indentSize=4:lineSeparator=\r\n:noTabs=true:tabSize=4:

