2 -- ion/share/ioncore_wd.lua
4 -- Copyright (c) Tuomo Valkonen 2004-2009.
6 -- See the included file LICENSE for details.
9 local savefile
="saved_wd"
13 if pcall(function() return require('lfs') end) then
17 local function checkdir(d
)
21 local t
, err
=lfs
.attributes(d
, "mode")
24 elseif t
=="directory" then
27 return nil, TR("Not a directory.")
32 local function regtreepath_i(reg
)
33 local function f(s
, v
)
44 -- Change default working directory for new programs started in \var{reg}.
45 function ioncore
.chdir_for(reg
, dir
)
46 assert(dir
==nil or type(dir
)=="string")
47 if dir
=="" or dir
==nil then
51 local ok
, err
=checkdir(dir
)
60 -- Get default working directory for new programs started in \var{reg}.
61 function ioncore
.get_dir_for(reg
)
62 for r
in regtreepath_i(reg
) do
70 local function lookup_script_warn(script
)
71 local script
=ioncore
.lookup_script(script
)
73 warn(TR("Could not find %s", script
))
79 local function lookup_runinxterm_warn(prog
, title
, wait
)
80 local rx
=lookup_script_warn("ion-runinxterm")
87 rx
=rx
.." -T "..string.shell_safe(title
)
98 -- Run \var{cmd} with the environment variable DISPLAY set to point to the
99 -- root window of the X screen \var{reg} is on. If \var{cmd} is prefixed
100 -- by a colon (\code{:}), the following command is executed in an xterm
101 -- (or other terminal emulator) with the help of the \command{ion-runinxterm}
102 -- script. If the command is prefixed by two colons, \command{ion-runinxterm}
103 -- will ask you to press enter after the command is finished, even if it
104 -- returns succesfully.
106 -- For GUI commands, you might prefer to use mod\_query.exec\_on\_merr(), which
107 -- monitors the process's \code{stderr} and shows any output as warnings on
108 -- the screen instead of in notions own output.
109 function ioncore
.exec_on(reg
, cmd
, merr_internal
)
110 local _
, _
, col
, c
=string.find(cmd
, "^[%s]*(:+)(.*)")
112 cmd
=lookup_runinxterm_warn(c
, nil, string.len(col
)>1)
117 cmd
='XTERMCMD='..string.shell_safe(XTERM
)..' '..cmd
120 return ioncore
.do_exec_on(reg
, cmd
, ioncore
.get_dir_for(reg
),
125 local function load_config()
126 local d
=ioncore
.read_savefile(savefile
)
129 for nm
, d
in pairs(d
) do
130 local r
=ioncore
.lookup_region(nm
)
132 local ok
, err
=checkdir(d
)
144 local function save_config()
146 for r
, d
in pairs(dirs
) do
147 local nm
=obj_exists(r
) and r
:name()
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
)