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