1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
2 * Copyright (C) 2003-2004 Martin Baehr
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * $Id: applauncher.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
25 //before using this file, patch the paths for watchforchanges.vim and golden_ratio.vim
35 void upload(object editor, string file, int last_mtime, object obj, object xslobj, function|void exit_callback, string|void olderrors)
37 int exit_status = editor->status();
38 object new_stat = file_stat(file);
41 string oldcontent = obj->get_content(); //currently changing
43 if((content!=oldcontent)&&(oldcontent!=("sTeam connection lost."||""))&&obj&&(i==1))
46 send_message("File changed on server.\n");
47 //Not needed - Stdio.write_file(file, oldcontent||"", 0600);
48 last_mtime = new_stat->mtime;
51 send_message(sprintf("%s is gone!", file));
53 if (new_stat && new_stat->mtime > last_mtime)
55 new_mtime = new_stat->mtime;
56 newcontent = Stdio.read_file(file);
57 if (!stringp(newcontent))
58 send_message(sprintf("failed to read %s", file));
61 if (stringp(newcontent) && newcontent != content && oldcontent!="sTeam connection lost.")
64 content = newcontent; //update initial content to new after saving
65 mixed result=obj->set_content(newcontent);
66 string message=sprintf("File saved - upload: %O\n", result);
67 olderrors = UNDEFINED;
68 send_message(message);
69 count=0; //so that compile status can be rewritten for newfile
72 result=xslobj->load_xml_structure();
73 message=sprintf("%O: load xml struct: %O", xslobj, result);
74 send_message(message);
77 if(oldcontent=="sTeam connection lost.")
80 send_message("Disconnected\n");
85 send_message("Connected back\n");
92 if(obj->get_class()=="DocLpc") //if pike script .
94 array errors = obj->get_errors();
95 string newerrors = sprintf("%O", errors);
96 if (newerrors != olderrors)
98 olderrors = newerrors;
99 send_message("-----------------------------------------\n");
101 send_message("Compiled successfully\n");
104 foreach(errors, string err)
106 send_message("Compilation failed\n");
108 send_message("-----------------------------------------\n");
111 call_out(upload, 1, editor, file, new_mtime, obj, xslobj, exit_callback, olderrors);
113 else if (exit_callback)
115 exit_callback(editor->wait());
121 void update(object obj)
126 array edit(object obj)
128 #if constant(Crypto.Random)
129 dir="/tmp/"+(MIME.encode_base64(Crypto.Random.random_string(10), 1)-("/"))+System.getpid();
131 dir="/tmp/"+(MIME.encode_base64(Crypto.randomness.pike_random()->read(10), 1)-("/"))+System.getpid();
133 string filename=obj->get_object_id()+"-"+obj->get_identifier();
135 debugfile = filename+"-disp";
137 content=obj->get_content(); //made content global, this is content when vim starts and remains same. oldcontent keeps changing in upload function.
138 //werror("%O\n", content);
139 Stdio.write_file(dir+"/"+filename, content||"", 0600);
141 Stdio.write_file(dir+"/"+debugfile, "This is your log window\n", 0600);
143 //array command=({ "screen", "-X", "screen", "vi", dir+"/"+filename });
144 //array command=({ "vim", "--servername", "VIM", "--remote-wait", dir+"/"+filename });
145 string enveditor = getenv("EDITOR");
146 string name = dir+"/"+debugfile;
147 if((enveditor=="VIM")||(enveditor=="vim")) //full path to .vim files to be mentioned
148 command=({ "vim","-S", "/usr/local/lib/steam/tools/watchforchanges.vim", "-S", "/usr/local/lib/steam/tools/golden_ratio.vim", dir+"/"+filename, "-c","set splitbelow", "-c" ,sprintf("split|view %s",name), "-c", "wincmd w"});
149 else if(enveditor=="emacs")
150 command=({ "emacs", "--eval","(add-hook 'emacs-startup-hook 'toggle-window-spt)", "--eval", "(global-auto-revert-mode t)", dir+"/"+filename, dir+"/"+debugfile, "--eval", "(setq buffer-read-only t)", "--eval", sprintf("(setq frame-title-format \"%s\")",obj->get_identifier()) , "--eval", "(windmove-up)", "--eval", "(enlarge-window 5)"});
152 command=({ "vi","-S", "/usr/local/lib/steam/tools/watchforchanges.vim", "-S", "/usr/local/lib/steam/tools/golden_ratio.vim", dir+"/"+filename, "-c","set splitbelow", "-c" ,sprintf("split|view %s",name), "-c", "wincmd w"});
154 object editor=Process.create_process(command,
155 ([ "cwd":getenv("HOME"), "env":getenv(), "stdin":Stdio.stdin, "stdout":Stdio.stdout, "stderr":Stdio.stderr ]));
156 return ({ editor, dir+"/"+filename });
159 int send_message(string message)
163 Process.create_process(({ "screen", "-X", "wall", message }));
164 else if (getenv("TMUX"))
165 Process.create_process(({ "tmux", "display-message", message }));
167 werror("%s\n", message);
169 Stdio.append_file(dir+"/"+debugfile, message||"", 0600); //result buffer
172 int applaunch(object obj, function exit_callback)
175 if(obj->get_identifier()[sizeof(obj->get_identifier())-8..]==".xsl.xml")
178 obj->get_identifier()[..sizeof(obj->get_identifier())-9]+ ".xsl";
179 xslobj=obj->get_environment()->get_object_byname(xslname);
184 [editor, file]=edit(obj);
186 //while(!(status=editor->status()))
188 send_message(sprintf("(opened %O %s)\n", obj, file));
189 call_out(upload, 1, editor, file, file_stat(file)->mtime, obj, xslobj, exit_callback);
191 // signal(signum("SIGINT"), prompt);