summaryrefslogtreecommitdiffstats
path: root/src/kile/kilestdtools.cpp
blob: c4c94296330356b7982e682fc2fd2e1618a38da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/***************************************************************************
    begin                : Thu Nov 27 2003
    copyright            : (C) 2003 by Jeroen Wijnhout
    email                : Jeroen.Wijnhout@kdemail.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "kilestdtools.h"

#include <tqfileinfo.h>
#include <tqregexp.h>

#include <kconfig.h>
#include <klocale.h>
#include <kate/document.h>
#include <kstandarddirs.h>

#include "kileconfig.h"
#include "kiletool.h"
#include "kiletoolmanager.h"
#include "kiletool_enums.h"
#include "kileinfo.h"
#include "kilelistselector.h"
#include "kiledocmanager.h"
#include "kiledocumentinfo.h"
#include "latexoutputinfo.h"

namespace KileTool
{
	Base* Factory::create(const TQString & tool, bool prepare /* = true */)
	{
		//perhaps we can find the tool in the config file
		if (m_config->hasGroup(groupFor(tool, m_config)))
		{
			m_config->setGroup(groupFor(tool, m_config));
			TQString toolClass = m_config->readEntry("class", "");

			if ( toolClass == "LaTeX")
				return new LaTeX(tool, m_manager, prepare);

			if ( toolClass == "LaTeXpreview")
				return new PreviewLaTeX(tool, m_manager, prepare);
				
			if ( toolClass == "ForwardDVI" )
				return new ForwardDVI(tool, m_manager, prepare);

			if ( toolClass == "ViewHTML" )
				return new ViewHTML(tool, m_manager, prepare);

			if ( toolClass == "ViewBib" )
				return new ViewBib(tool, m_manager, prepare);

			if ( toolClass == "Base" )
				return new Base(tool, m_manager, prepare);

			if ( toolClass == "Compile" )
				return new Compile(tool, m_manager, prepare);

			if ( toolClass == "Convert" )
				return new Convert(tool, m_manager, prepare);

			if ( toolClass == "Archive" )
				return new Archive(tool, m_manager, prepare);

			if ( toolClass == "View" )
				return new View(tool, m_manager, prepare);

			if ( toolClass == "Sequence" )
				return new Sequence(tool, m_manager, prepare);
		}

		//unknown tool, return 0
		return 0L;
	}

	void Factory::writeStdConfig()
	{
		TQString from_cfg = KGlobal::dirs()->findResource("appdata", "kilestdtools.rc");
		TQString to_cfg = KGlobal::dirs()->saveLocation("config") + "/kilerc";
		KConfig *pCfg = new KConfig(from_cfg, false, false);
		pCfg->copyTo(to_cfg, m_config);
	}

	/////////////// LaTeX ////////////////

	int LaTeX::m_reRun = 0;

	bool LaTeX::updateBibs()
	{
		KileDocument::TextInfo *docinfo = manager()->info()->docManager()->textInfoFor(source());
                if ( docinfo )
                {
			if ( manager()->info()->allBibliographies()->count() > 0 )
				return needsUpdate ( baseDir() + '/' + S() + ".bbl" , manager()->info()->lastModifiedFile(docinfo) );
		}

		return false;
	}

	bool LaTeX::updateIndex()
	{
		KileDocument::TextInfo *docinfo = manager()->info()->docManager()->textInfoFor(source());
		if ( docinfo )
		{
			const TQStringList *pckgs = manager()->info()->allPackages();
				if ( pckgs->contains("makeidx") )
					return needsUpdate ( baseDir() + '/' + S() + ".ind", manager()->info()->lastModifiedFile(docinfo) );
		}

		return false;
	}
	
	bool LaTeX::updateAsy()
	{
		KileDocument::TextInfo *docinfo = manager()->info()->docManager()->textInfoFor(source());
		if ( docinfo)
		{	
		
			const TQStringList *pckgs = manager()->info()->allPackages();
			if ( pckgs->contains("asymptote") )
			{
				static TQRegExp msg("File " + TQRegExp::escape(S()) + "_?\\d+_?.(?:eps|pdf|tex) does not exist");
				int sz =  manager()->info()->outputInfo()->size();
				for (int i = 0; i < sz; ++i )
				{
					if( (*manager()->info()->outputInfo())[i].message().contains(msg) )
						return true;
				}
			}
		}
		return false;
	}

	bool LaTeX::finish(int r)
	{
		KILE_DEBUG() << "==bool LaTeX::finish(" << r << ")=====" << endl;
		
		int nErrors = 0, nWarnings = 0;
		if ( filterLogfile() ) {
			checkErrors(nErrors,nWarnings);
		}
		
		if ( readEntry("autoRun") == "yes" )
			checkAutoRun(nErrors,nWarnings);

		return Compile::finish(r);
	}

	bool LaTeX::filterLogfile()
	{
		manager()->info()->outputFilter()->setSource(source());
		TQString log = baseDir() + '/' + S() + ".log";

		return manager()->info()->outputFilter()->Run(log);
	}

	void LaTeX::checkErrors(int &nErrors, int &nWarnings)
	{
		int nBadBoxes = 0;
		
		manager()->info()->outputFilter()->sendProblems();
		manager()->info()->outputFilter()->getErrorCount(&nErrors, &nWarnings, &nBadBoxes);
		TQString es = i18n("%n error", "%n errors", nErrors);
		TQString ws = i18n("%n warning", "%n warnings", nWarnings);
		TQString bs = i18n("%n badbox", "%n badboxes", nBadBoxes);

		sendMessage(Info, es +", " + ws + ", " + bs);
	
		//jump to first error
		if ( nErrors > 0 && (readEntry("jumpToFirstError") == "yes") ) {
			connect(this, TQT_SIGNAL(jumpToFirstError()), manager(), TQT_SIGNAL(jumpToFirstError()));
			emit(jumpToFirstError());
		}
	}
	
	void LaTeX::checkAutoRun(int nErrors, int nWarnings)
	{
		KILE_DEBUG() << "check for autorun, m_reRun is " << m_reRun << endl;
		//check for "rerun LaTeX" warnings
		bool reRan = false;
		if ( (m_reRun < 2) && (nErrors == 0) && (nWarnings > 0) )
		{
			int sz =  manager()->info()->outputInfo()->size();
			for (int i = 0; i < sz; ++i )
			{
				if ( (*manager()->info()->outputInfo())[i].type() == LatexOutputInfo::itmWarning
				&&  (*manager()->info()->outputInfo())[i].message().contains("Rerun") )
				{
					reRan = true;
					break;
				}
			}
		}

		
		if ( reRan )
			m_reRun++;
		else 
			m_reRun = 0;

		bool bibs = updateBibs();
		bool index = updateIndex();
		bool asy = updateAsy();
		
		if ( reRan )
		{
			KILE_DEBUG() << "rerunning LaTeX " << m_reRun << endl;
			Base *tool = manager()->factory()->create(name());
			tool->setSource(source());
			manager()->runNext(tool);
		}
		
		if ( bibs || index || asy )
		{
			Base *tool = manager()->factory()->create(name());
			tool->setSource(source());
			manager()->runNext(tool);

			if ( bibs )
			{
				KILE_DEBUG() << "need to run BibTeX" << endl;
				tool = manager()->factory()->create("BibTeX");
				tool->setSource(source());
				manager()->runNext(tool);
			}

			if ( index ) 
			{
				KILE_DEBUG() << "need to run MakeIndex" << endl;
				tool = manager()->factory()->create("MakeIndex");
				tool->setSource(source());
				manager()->runNext(tool);
			}
			
			if ( asy ) 
			{
				KILE_DEBUG() << "need to run asymptote" << endl;
				tool = manager()->factory()->create("Asymptote");
				tool->setSource(source());
				manager()->runNext(tool);
			}
			
		}
	}
	
	
	/////////////// PreviewLaTeX (dani) ////////////////

	// PreviewLatex makes three steps:
	// - filterLogfile()  : parse logfile and read info into InfoLists
	// - updateInfoLists(): change entries of temporary file into normal tex file
	// - checkErrors()    : count errors and warnings and emit signals   
	bool PreviewLaTeX::finish(int r)
	{
		KILE_DEBUG() << "==bool PreviewLaTeX::finish(" << r << ")=====" << endl;
		
		int nErrors = 0, nWarnings = 0;
		if ( filterLogfile() ) {
			manager()->info()->outputFilter()->updateInfoLists(m_filename,m_selrow,m_docrow);
			checkErrors(nErrors,nWarnings);
		}
		
		return Compile::finish(r);
	}

	void PreviewLaTeX::setPreviewInfo(const TQString &filename, int selrow,int docrow)
	{
		m_filename = filename;
		m_selrow = selrow;
		m_docrow = docrow;
	}
	
	bool ForwardDVI::determineTarget()
	{
		if (!View::determineTarget())
			return false;

		int para = manager()->info()->lineNumber();
		Kate::Document *doc = manager()->info()->activeTextDocument();
		TQString filepath;

		if (doc)
			filepath = doc->url().path();
		else
			return false;

		TQString texfile = manager()->info()->relativePath(baseDir(),filepath);
		m_urlstr = "file:" + targetDir() + '/' + target() + "#src:" + TQString::number(para+1) + ' ' + texfile; // space added, for files starting with numbers
		addDict("%dir_target", TQString());
		addDict("%target", m_urlstr);
		KILE_DEBUG() << "==KileTool::ForwardDVI::determineTarget()=============\n" << endl;
		KILE_DEBUG() << "\tusing  " << m_urlstr << endl;

		return true;
	}

	bool ViewBib::determineSource()
	{
		KILE_DEBUG() << "==ViewBib::determineSource()=======" << endl;
		if (!View::determineSource())
			return false;

		TQString path = source(true);
		TQFileInfo info(path);

		//get the bibliographies for this source
		const TQStringList *bibs = manager()->info()->allBibliographies(manager()->info()->docManager()->textInfoFor(path));
		KILE_DEBUG() << "\tfound " << bibs->count() << " bibs" << endl;
		if (bibs->count() > 0)
		{
			TQString bib = bibs->front();
			if (bibs->count() > 1)
			{
				//show dialog
				bool bib_selected = false;
				KileListSelector *dlg = new KileListSelector(*bibs, i18n("Select Bibliography"),i18n("Select a bibliography"));
				if (dlg->exec())
				{
					bib = (*bibs)[dlg->currentItem()];
					bib_selected = true;
					KILE_DEBUG() << "Bibliography selected : " << bib << endl;
				}
				delete dlg;
				
				if ( ! bib_selected ) {
					sendMessage(Warning, i18n("No bibliography selected."));
					return false;
				}
			}
			KILE_DEBUG() << "filename before: " << info.dirPath() << endl;
			setSource(manager()->info()->checkOtherPaths(info.dirPath(),bib + ".bib",KileInfo::bibinputs));	
		}
		else if( info.exists() ) //active doc is a bib file
		{
			KILE_DEBUG() << "filename before: " << info.dirPath() << endl;
			setSource(manager()->info()->checkOtherPaths(info.dirPath(),info.fileName(),KileInfo::bibinputs));
		}
		else
		{
			sendMessage(Error, i18n("No bibliographies found."));
			return false;
		}
		return true;
	}

	bool ViewHTML::determineTarget()
	{
		if (target().isNull())
		{
			//setRelativeBaseDir(S());
			TQString dir = readEntry("relDir");
			TQString trg = readEntry("target");

			if ( !dir.isEmpty() )
			{
				translate(dir);
				setRelativeBaseDir(dir);
			}

			if ( !trg.isEmpty() )
			{
				translate(trg);
				setTarget(trg);
			}

			//auto-detect the file to view
			if ( dir.isEmpty() && trg.isEmpty() )
			{
				TQFileInfo file1 = TQFileInfo(baseDir() + '/' + S() + "/index.html");
				TQFileInfo file2 = TQFileInfo(baseDir() + '/' + S() + ".html");

				bool read1 = file1.isReadable();
				bool read2 = file2.isReadable();

				if ( !read1 && !read2 )
				{
					sendMessage(Error, i18n("Unable to find %1 or %2; if you are trying to view some other HTML file, go to Settings->Configure Kile->Tools->ViewHTML->Advanced.").tqarg(file1.absFilePath()).tqarg(file2.absFilePath()));
					return false;
				}

				//both exist, take most recent
				if ( read1 && read2 )
				{
					read1 = file1.lastModified() > file2.lastModified();
					read2 = !read1;
				}

				if ( read1 )
				{
					dir = S();
					trg = "index.html";

					translate(dir);
					setRelativeBaseDir(dir);
					translate(trg);
					setTarget(trg);
				}
			}
		}

		return View::determineTarget();
	}
}

#include "kilestdtools.moc"