1 #include "Common.h"
2 
Tip(const String &,CodeEditor::MouseTip &)3 bool Debugger::Tip(const String&, CodeEditor::MouseTip&)
4 {
5 	return false;
6 }
7 
8 bool deactivation_save;
9 
DeactivationSave(bool b)10 void DeactivationSave(bool b) // On deactivation, make no prompts, ignore save errors
11 {
12 	deactivation_save = b;
13 }
14 
IsDeactivationSave()15 bool IsDeactivationSave()
16 {
17 	return deactivation_save;
18 }
19 
FinishSave(String tmpfile,String outfile)20 bool FinishSave(String tmpfile, String outfile)
21 {
22 	if(IsDeactivationSave()) {
23 		FileMove(tmpfile, outfile);
24 		return true;
25 	}
26 	Progress progress;
27 	int time = msecs();
28 	for(;;) {
29 		progress.SetTotal(10000);
30 		progress.SetText("Saving '" + GetFileName(outfile) + "'");
31 		if(!FileExists(tmpfile))
32 			return false;
33 		FileDelete(outfile);
34 		if(FileMove(tmpfile, outfile))
35 			return true;
36 		IdeConsoleFlush();
37 		Sleep(200);
38 		if(progress.SetPosCanceled((msecs() - time) % progress.GetTotal())) {
39 			int art = Prompt(Ctrl::GetAppName(), CtrlImg::exclamation(),
40 				"Unable to save current file.&"
41 				"Retry save, ignore it or save file to another location?",
42 				"Save as...", "Retry", "Ignore");
43 			if(art < 0)
44 				return false;
45 			if(art && AnySourceFs().ExecuteSaveAs())
46 				outfile = AnySourceFs();
47 			progress.SetPos(0);
48 		}
49 	}
50 }
51 
FinishSave(String outfile)52 bool FinishSave(String outfile)
53 {
54 	return FinishSave(outfile + ".$tmp", outfile);
55 }
56 
SaveFileFinish(const String & filename,const String & data)57 bool SaveFileFinish(const String& filename, const String& data)
58 {
59 	if(!SaveFile(filename + ".$tmp", data)) {
60 		if(IsDeactivationSave())
61 			return true;
62 		Exclamation("Error creating temporary file " + filename);
63 		return false;
64 	}
65 	return FinishSave(filename);
66 }
67 
SaveChangedFileFinish(const String & filename,const String & data)68 bool SaveChangedFileFinish(const String& filename, const String& data)
69 {
70 	if(data == LoadFile(filename))
71 		return true;
72 	return SaveFileFinish(filename, data);
73 }
74 
sWorkspaceCfg()75 VectorMap<String, String>& sWorkspaceCfg()
76 {
77 	static VectorMap<String, String> h;
78 	return h;
79 }
80 
sWorkspaceCfgFlush()81 Vector<Event<> >& sWorkspaceCfgFlush()
82 {
83 	static Vector<Event<> > h;
84 	return h;
85 }
86 
RegisterWorkspaceConfig(const char * name)87 void    RegisterWorkspaceConfig(const char *name)
88 {
89 	ASSERT(sWorkspaceCfg().Find(name) < 0);
90 	sWorkspaceCfg().Add(name);
91 }
92 
RegisterWorkspaceConfig(const char * name,Event<> WhenFlush)93 void    RegisterWorkspaceConfig(const char *name, Event<>  WhenFlush)
94 {
95 	RegisterWorkspaceConfig(name);
96 	sWorkspaceCfgFlush().Add(WhenFlush);
97 }
98 
99 
WorkspaceConfigData(const char * name)100 String& WorkspaceConfigData(const char *name)
101 {
102 	return sWorkspaceCfg().GetAdd(name);
103 }
104 
SerializeWorkspaceConfigs(Stream & s)105 void  SerializeWorkspaceConfigs(Stream& s)
106 {
107 	int i;
108 	for(i = 0; i < sWorkspaceCfgFlush().GetCount(); i++)
109 		sWorkspaceCfgFlush()[i]();
110 	VectorMap<String, String>& cfg = sWorkspaceCfg();
111 	int version = 0;
112 	s / version;
113 	int count = cfg.GetCount();
114 	s / count;
115 	for(i = 0; i < count; i++) {
116 		String name;
117 		if(s.IsStoring())
118 			name = cfg.GetKey(i);
119 		s % name;
120 		int q = cfg.Find(name);
121 		if(q >= 0)
122 			s % cfg[q];
123 		else
124 		{
125 			String dummy;
126 			s % dummy;
127 		}
128 	}
129 	s.Magic();
130 }
131 
GuiPackageResolver(const String & error,const String & path,int line)132 bool GuiPackageResolver(const String& error, const String& path, int line)
133 {
134 prompt:
135 	switch(Prompt(Ctrl::GetAppName(), CtrlImg::exclamation(),
136 	              error + "&while parsing package " + DeQtf(path),
137 		          "Edit \\& Retry", "Ignore",  "Stop")) {
138 	case 0:
139 		if(!PromptYesNo("Ignoring will damage package. Everything past the "
140 			            "point of error will be lost.&Do you want to continue ?"))
141 			goto prompt;
142 		return false;
143 	case 1: {
144 			TopWindow win;
145 			LineEdit edit;
146 			edit.Set(LoadFile(path));
147 			edit.SetCursor(edit.GetPos(line));
148 			win.Title(path);
149 			win.Add(edit.SizePos());
150 			win.Run();
151 			SaveFile(path, edit.Get());
152 		}
153 		return true;;
154 	case -1:
155 		exit(1);
156 	}
157 	return false;
158 }
159 
CleanModules()160 void CleanModules()
161 {
162 	for(int i = 0; i < GetIdeModuleCount(); i++)
163 		GetIdeModule(i).CleanUsc();
164 }
165 
IdeModuleUsc(CParser & p)166 bool IdeModuleUsc(CParser& p)
167 {
168 	for(int i = 0; i < GetIdeModuleCount(); i++)
169 		if(GetIdeModule(i).ParseUsc(p))
170 			return true;
171 	return false;
172 }
173 
ReadMacro(CParser & p)174 static void ReadMacro(CParser& p)
175 {
176 	IdeMacro macro;
177 	if(p.IsString()) {
178 		macro.menu = p.ReadString();
179 		if(p.Char(':'))
180 			macro.submenu = p.ReadString();
181 	}
182 	if(!p.IsChar('{'))
183 		macro.hotkey = ParseKeyDesc(p);
184 	EscLambda& l = macro.code.CreateLambda();
185 	const char *t = p.GetPtr();
186 	l.filename = p.GetFileName();
187 	l.line = p.GetLine();
188 	if(!p.Char('{'))
189 		p.ThrowError("missing '{'");
190 	SkipBlock(p);
191 	l.code = String(t, p.GetPtr());
192 	Array<IdeMacro>& mlist = UscMacros();
193 	if(macro.hotkey) {
194 		int f = FindFieldIndex(mlist, &IdeMacro::hotkey, macro.hotkey);
195 		if(f >= 0) {
196 			PutConsole(Format("%s(%d): duplicate macro hotkey %s\n", l.filename, l.line, GetKeyDesc(macro.hotkey)));
197 			const EscLambda& lambda = UscMacros()[f].code.GetLambda();
198 			PutConsole(Format("%s(%d): previously defined here\n", lambda.filename, lambda.line));
199 		}
200 	}
201 	if(!IsNull(macro.menu)) {
202 		for(int i = 0; i < mlist.GetCount(); i++)
203 			if(mlist[i].menu == macro.menu && mlist[i].submenu == macro.submenu) {
204 				PutConsole(Format("%s(%d): duplicate macro menu item (%s:%s)\n",
205 					l.filename, l.line, macro.menu, macro.submenu));
206 				const EscLambda& lambda = UscMacros()[i].code.GetLambda();
207 				PutConsole(Format("%s(%d): previously defined here\n", lambda.filename, lambda.line));
208 				break;
209 			}
210 	}
211 	mlist.Add(macro);
212 }
213 
214 INITBLOCK {
215 	Package::SetPackageResolver(GuiPackageResolver);
216 	UscSetCleanModules(CleanModules);
217 	SetIdeModuleUsc(IdeModuleUsc);
218 	UscSetReadMacro(ReadMacro);
219 }
220 
221 bool IdeExit;
222 bool IdeAgain;
223 
CopyFolder(const char * dst,const char * src,Progress * pi)224 bool CopyFolder(const char *dst, const char *src, Progress *pi)
225 {
226 	if(strcmp(src, dst) == 0)
227 		return true;
228 	RealizeDirectory(dst);
229 	if(pi)
230 		pi->SetText(dst);
231 	FindFile ff(AppendFileName(src, "*"));
232 	while(ff) {
233 		if(pi && pi->StepCanceled())
234 			return false;
235 		String s = AppendFileName(src, ff.GetName());
236 		String d = AppendFileName(dst, ff.GetName());
237 		if(ff.IsFolder())
238 			if(!CopyFolder(d, s, pi))
239 				return false;
240 		if(ff.IsFile())
241 			if(!SaveFile(d, LoadFile(s)))
242 				return false;
243 		ff.Next();
244 	}
245 	return true;
246 }
247 
HasSvn()248 bool HasSvn()
249 {
250 	String dummy;
251 	static bool b = Sys("svn", dummy) >= 0;
252 	return b;
253 }
254 
MaxAscent(Font f)255 int MaxAscent(Font f)
256 {
257 	return max(f.GetAscent(), f().Bold().GetAscent(),
258 	           f().Italic().GetAscent(), f().Bold().Italic().GetAscent());
259 }
260