#!/usr/bin/env python
# lyx-python          -*- coding: iso-8859-1 -*-
# Copyright (c) 2005 Gnter Milde
# Released under the terms of the GNU General Public License (ver. 2 or later)
"""LyX with an interactive Python client.
   
   Starts a python session and connects to a running LyX (or starts 
   a new LyX session) with an LyXClient instance
   
   All functions from `LyX.lfuns` and `LyX.pyfuns`
   are imported. The LyXClient instance is given the name `lyx`. 
   
   To send commands (LFUNs) to LyX, do
      lyx("message", "hello world")
   or
      message("hello world")
   
   To get some more help use one of
   
      print apropos(<regexp-pattern>)   # list lfuns whose name or description 
                                  # matches <pattern>
      where_is(<lfun>)            # get the keybinding of a lfun
      showkey(<key>)              # show the lfun bound to key
"""

# Needs the LyX-Server running (Set Edit>Preferences>Paths>Server-pipe)

import sys, code, logging
# Tab completion (taken from the example in module-rlcompleter.html)
try:
    import readline
except ImportError:
    print "Module readline not available."
else:
    import rlcompleter
    readline.parse_and_bind("tab: complete")

# 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

from LyX.lyxclient import lyx_remote
from LyX.lfuns import *
from LyX.pyfuns import *
from LyX.bindings import *  # overwrite the definitions in pyfuns
# comment this out if you wish the help functions from bindings
# to display in LyX

lyx = lyx_remote()
lyx.open()  # open now, so we see whether LyX runs (and start if not)

# interact with the user with a "normal" python prompt
code.interact(__doc__, local=globals())
