#!/usr/bin/env python
# lyx-Mx         -*- 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 lyxclient ('minibuffer-console').
   
   * Starts python and connects to a running LyX session (or starts 
     a new LyX session) with an lyx.LyXClient instance

   * The user is prompted for lyx functions (LFUNS), that are sent to LyX via
     the serverpipes
   
     Basically, all that works in the minibuffer (M-x line) works here.

   * A list of all LFUNS is available at 
     http://wiki.lyx.org/pmwiki.php/LyX/LyxFunctions
         
     See also the (incomplete/outdated) Reference.lyx in LYXDIR/doc
     (Help>Reference)
      
   * Besides being a nice test of the LyXClient, this also provides a means
     to drag and drop the output of a LFUN to e.g. a bug-reprot or fan mail
   
     Solves Bugzilla Bug 197
       "Allow simple use of lyxserver by running lyx --server or similar"
     (if you count running lyx-Mx as 'similar')
   
   * Needs the LyX-Server running (Set Edit>Preferences>Paths>Server-pipe)
   
   *  To quit type "quit" or "exit" or press Ctrl-D.
"""

import sys, os, logging
from LyX import lyxclient

# Customisable values (defaults shown)
# ------------------------------------

abort_commands = ["quit", "exit"]

# lyxclient.read_timeout = 1000           # default timeout for reading the outpipe (in ms)
lyxclient.logger.setLevel(logging.WARN)   # default INFO

client = lyxclient.lyx_remote()

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

# interact with the user
print __doc__
while True:
    line = raw_input("M-x: ")                # read from stdin
    if not line:                             # empty input line
        continue
    if line in abort_commands:
        break
    print client(*line.split())              # print to stdout
