Since the darcs ion scripts repository is only for ion2, here's an ion
script for ion3.  I use it to start & access commonly used
applications -- i.e., use the same key-press to run it, or go to it if
it's already running.  (Yes, I use screen in ion too....)

There's also a couple of emacs-interaction functions that work the same
way (eval some elisp in the running emacs iff emacs is running,
otherwise start it & eval the elisp).  This includes a replacement
querylib:query_editfile that uses (or starts) an emacs process instead
of ion-edit.

This version is for ion3, but I can probably dig up an old version for
ion2 if anyone's interested.  Let me know, if so.  The ion3 version is a
bit cleaner, because of the availability of ioncore.clientwin_list().

--
-- By Jeremy Hankins <[EMAIL PROTECTED]>
--
-- With these functions you can bind a key to start an application if
-- it's not already running, or go to it if it is.  You can use it by
-- doing something like this in your bindings file:
--
-- kpress(MOD1.."C", "app.byname('xterm -title shell', 'shell')"),
-- kpress(MOD1.."T", "app.byclass('emacs', 'Emacs')"),
--
-- The byname function expects an executable and a window title, the
-- byclass expects a class.
--
-- For emacs users there's also app.emacs_eval, and app.query_editfile,
-- which interacts with any currently running emacs process, or starts
-- emacs and run the given command.  app.query_editfile is a replacement
-- for query_lib.query_editfile to use the currently running emacs
-- rather than ion-edit.
--
-- NOTE: This requires ion3, but I can probably provide and old version
-- for ion2 on request.
--

if _LOADED["app"] then return end

app={}

include("querylib")

function app.match_class(class)
   local wins = ioncore.clientwin_list()
   local result = {}
   for i, win in wins do
      if class == win:get_ident().class then
         table.insert(result, win)
      end
   end
   return result
end

function app.byname(prog, name)
   local win = ioncore.lookup_clientwin(name)
   if win then
      win:goto()
   else
      ioncore.exec(prog)
   end
end

function app.byclass(prog, class)
   local win = app.match_class(class)[1]
   if win then
      win:goto()
   else
      ioncore.exec(prog)
   end
end

function app.emacs_eval(expr)
   local emacswin = app.match_class("Emacs")[1]
   if emacswin then
      ioncore.exec("gnuclient -batch -eval '"..expr.."'")
      emacswin:goto()
   else
      ioncore.exec("emacs -eval '"..expr.."'")
   end
end

function app.emacs_edit(mplex, file)
   app.emacs_eval("(find-file \""..file.."\")")
end

function app.query_editfile(mplex) 
   querylib.do_query(mplex,
                     'Edit file:',
                     querylib.get_initdir(),
                     app.emacs_edit,
                     querylib.file_completor)
end


_LOADED["app"]=true

-- 
Jeremy Hankins <[EMAIL PROTECTED]>
PGP fingerprint: 748F 4D16 538E 75D6 8333  9E10 D212 B5ED 37D0 0A03

Reply via email to