#!/usr/bin/env python
# lyx-bindings         -*- coding: iso-8859-1 -*-
# Copyright (c) 2005 Gnter Milde
# Released under the terms of the GNU General Public License (ver. 2 or later)

"""
Help and diagnose for LyX functions (lfuns) and LyX keybindings
"""

import os
from optparse import OptionParser
import logging


# Customizable values (defaults shown)
# ------------------------------------

from LyX import constants
# default timeout for reading the outpipe (in ms)
# constants.LYXSERVER_POLL_TIMEOUT = 1000 # default 1 s
# constants.LOG_LEVEL = logging.DEBUG # default INFO
constants.LOG_LEVEL = logging.WARN # default INFO

# now import with the set defaults
from LyX.bindings import *

usage = """%%prog [-k] [-s] [-w] [FILE]

parse FILE (default %s) for a list of LyX keybindings

Example `%%prog cua` will print the bindings set up by 
cua.bind and included files.
"""%constants.LYXRC_FILE

# parse command line options
parser = OptionParser(usage, version=constants.VERSION)

parser.add_option("-k", "--apropos", dest="pattern",
                  help="re.search for PATTERN in commented list of lyx functions")
parser.add_option("-s", "--showkey", dest="key",
                  help="show keybinding for KEY")
parser.add_option("-w", "--whereis", dest="lfun",
                 help="show keybinding for re pattern LFUN")


(options, args) = parser.parse_args()


# print options, args

if options.pattern:
    print "".join(apropos(options.pattern, *args))
elif options.key:
    print showkey(options.key, *args)
elif options.lfun:
    print_bindings(where_is(options.lfun, *args))
else:
    bindfiles, bindings = get_bindings(*args)
    print_bindings(bindings)
    if bindfiles:
        print "\nIncluded bindfiles: "\
        + ", ".join(map(os.path.basename, bindfiles))
