2 -- ion/share/ioncore_wd.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2007.
6 -- See the included file LICENSE for details.
9 local savefile="saved_wd"
13 if pcall(function() return require('posix') end) then
17 local function checkdir(d)
21 local t, err=px.stat(d, "type")
25 local d2, err=px.readlink(d)
32 elseif t=="directory" then
35 return TR("Not a directory.")
40 local function regtreepath_i(reg)
41 local function f(s, v)
52 -- Change default working directory for new programs started in \var{reg}.
53 function ioncore.chdir_for(reg, dir)
54 assert(type(dir)=="string")
55 if dir=="" or dir==nil then
59 local ok, err=checkdir(dir)
68 -- Get default working directory for new programs started in \var{reg}.
69 function ioncore.get_dir_for(reg)
70 for r in regtreepath_i(reg) do
78 local function lookup_script_warn(script)
79 local script=ioncore.lookup_script(script)
81 warn(TR("Could not find %s", script))
87 local function lookup_runinxterm_warn(prog, title, wait)
88 local rx=lookup_script_warn("ion-runinxterm")
94 rx=rx.." -T "..string.shell_safe(title)
105 -- Run \var{cmd} with the environment variable DISPLAY set to point to the
106 -- root window of the X screen \var{reg} is on. If \var{cmd} is prefixed
107 -- by a colon (\code{:}), the following command is executed in an xterm
108 -- (or other terminal emulator) with the help of the \command{ion-runinxterm}
109 -- script. If the command is prefixed by two colons, \command{ion-runinxterm}
110 -- will ask you to press enter after the command is finished, even if it
111 -- returns succesfully.
112 function ioncore.exec_on(reg, cmd, merr_internal)
113 local _, _, col, c=string.find(cmd, "^[%s]*(:+)(.*)")
115 cmd=lookup_runinxterm_warn(c, nil, string.len(col)>1)
120 cmd='XTERMCMD='..string.shell_safe(XTERM)..' '..cmd
123 return ioncore.do_exec_on(reg, cmd, ioncore.get_dir_for(reg),
128 local function load_config()
129 local d=ioncore.read_savefile(savefile)
132 for nm, d in pairs(d) do
133 local r=ioncore.lookup_region(nm)
135 local ok, err=checkdir(d)
147 local function save_config()
149 for r, d in pairs(dirs) do
152 ioncore.write_savefile(savefile, t)
156 local function init()
158 ioncore.get_hook("ioncore_snapshot_hook"):add(save_config)
159 ioncore.get_hook("ioncore_post_layout_setup_hook"):add(load_config)