]> git.decadent.org.uk Git - ion3.git/blob - ioncore/ioncore_wd.lua
48bc7fcf84bc9f956efc9ffac38b923ca34e01dc
[ion3.git] / ioncore / ioncore_wd.lua
1 --
2 -- ion/share/ioncore_wd.lua
3 -- 
4 -- Copyright (c) Tuomo Valkonen 2004-2008.
5 --
6 -- See the included file LICENSE for details.
7 --
8
9 local savefile="saved_wd"
10 local dirs={}
11 local lfs
12
13 if pcall(function() return require('lfs') end) then
14     lfs=_G["lfs"]
15 end
16
17 local function checkdir(d)
18     if not lfs then
19         return true
20     else
21         local t, err=lfs.attributes(d, "mode")
22         if not t then
23             return nil, err
24         elseif t=="directory" then
25             return true
26         else
27             return nil, TR("Not a directory.")
28         end
29     end
30 end
31
32 local function regtreepath_i(reg)
33     local function f(s, v)
34         if v then
35             return v:manager()
36         else
37             return s
38         end
39     end
40     return f, reg, nil
41 end
42
43 --DOC
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
48         dirs[reg]=nil
49         return true
50     else 
51         local ok, err=checkdir(dir)
52         if ok then
53             dirs[reg]=dir
54         end
55         return ok, err
56     end
57 end
58
59 --DOC
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
63         if dirs[r] then
64             return dirs[r]
65         end
66     end
67 end
68
69
70 local function lookup_script_warn(script)
71     local script=ioncore.lookup_script(script)
72     if not script then
73         warn(TR("Could not find %s", script))
74     end
75     return script
76 end
77
78
79 local function lookup_runinxterm_warn(prog, title, wait)
80     local rx=lookup_script_warn("ion-runinxterm")
81     if rx then
82         rx="exec "..rx
83         if wait then
84             rx=rx.." -w"
85         end
86         if title then
87             rx=rx.." -T "..string.shell_safe(title)
88         end
89         if prog then
90             rx=rx.." -- "..prog
91         end
92     end
93     return rx
94 end
95
96
97 --DOC
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.
105 function ioncore.exec_on(reg, cmd, merr_internal)
106     local _, _, col, c=string.find(cmd, "^[%s]*(:+)(.*)")
107     if col then
108         cmd=lookup_runinxterm_warn(c, nil, string.len(col)>1)
109         if not cmd then
110             return
111         end
112         if XTERM then
113             cmd='XTERMCMD='..string.shell_safe(XTERM)..' '..cmd
114         end
115     end
116     return ioncore.do_exec_on(reg, cmd, ioncore.get_dir_for(reg), 
117                               merr_internal)
118 end
119
120
121 local function load_config()
122     local d=ioncore.read_savefile(savefile)
123     if d then
124         dirs={}
125         for nm, d in pairs(d) do
126             local r=ioncore.lookup_region(nm)
127             if r then
128                 local ok, err=checkdir(d)
129                 if ok then
130                     dirs[r]=d
131                 else
132                     warn(err)
133                 end
134             end
135         end
136     end
137 end
138
139
140 local function save_config()
141     local t={}
142     for r, d in pairs(dirs) do
143         local nm=obj_exists(r) and r:name()
144         if nm then
145             t[nm]=d
146         end
147     end
148     ioncore.write_savefile(savefile, t)
149 end
150
151
152 local function init()
153     load_config()
154     ioncore.get_hook("ioncore_snapshot_hook"):add(save_config)
155     ioncore.get_hook("ioncore_post_layout_setup_hook"):add(load_config)
156 end
157
158 init()
159