2 -- ion/share/ioncore_wd.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
6 -- Ion is free software; you can redistribute it and/or modify it under
7 -- the terms of the GNU Lesser General Public License as published by
8 -- the Free Software Foundation; either version 2.1 of the License, or
9 -- (at your option) any later version.
12 local savefile="saved_wd"
16 if pcall(function() return require('posix') end) then
20 local function checkdir(d)
24 local t, err=px.stat(d, "type")
28 local d2, err=px.readlink(d)
35 elseif t=="directory" then
38 return TR("Not a directory.")
43 local function regtreepath_i(reg)
44 local function f(s, v)
55 -- Change default working directory for new programs started in \var{reg}.
56 function ioncore.chdir_for(reg, dir)
57 assert(type(dir)=="string")
58 if dir=="" or dir==nil then
62 local ok, err=checkdir(dir)
71 -- Get default working directory for new programs started in \var{reg}.
72 function ioncore.get_dir_for(reg)
73 for r in regtreepath_i(reg) do
81 local function lookup_script_warn(script)
82 local script=ioncore.lookup_script(script)
84 warn(TR("Could not find %s", script))
90 local function lookup_runinxterm_warn(prog, title, wait)
91 local rx=lookup_script_warn("ion-runinxterm")
97 rx=rx.." -T "..string.shell_safe(title)
108 -- Run \var{cmd} with the environment variable DISPLAY set to point to the
109 -- root window of the X screen \var{reg} is on. If \var{cmd} is prefixed
110 -- by a colon (\code{:}), the following command is executed in an xterm
111 -- (or other terminal emulator) with the help of the \command{ion-runinxterm}
112 -- script. If the command is prefixed by two colons, \command{ion-runinxterm}
113 -- will ask you to press enter after the command is finished, even if it
114 -- returns succesfully.
115 function ioncore.exec_on(reg, cmd, merr_internal)
116 local _, _, col, c=string.find(cmd, "^[%s]*(:+)(.*)")
118 cmd=lookup_runinxterm_warn(c, nil, string.len(col)>1)
123 cmd='XTERMCMD='..string.shell_safe(XTERM)..' '..cmd
126 return ioncore.do_exec_rw(reg:rootwin_of(), cmd,
127 ioncore.get_dir_for(reg),
132 local function load_config()
133 local d=ioncore.read_savefile(savefile)
136 for nm, d in pairs(d) do
137 local r=ioncore.lookup_region(nm)
139 local ok, err=checkdir(d)
151 local function save_config()
153 for r, d in pairs(dirs) do
156 ioncore.write_savefile(savefile, t)
160 local function init()
162 ioncore.get_hook("ioncore_snapshot_hook"):add(save_config)
163 ioncore.get_hook("ioncore_post_layout_setup_hook"):add(load_config)