summaryrefslogtreecommitdiffstats
path: root/src/kile/kileerrorhandler.cpp
blob: 6a5c60b36d39303561a60acb80bea1cbd7bd7024 (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
/***************************************************************************
    begin                : Tue May 25 2004
    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 "kileerrorhandler.h"

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

#include <klocale.h>
#include <kurl.h>
#include <kate/document.h>
#include <kate/view.h>

#include "kiletool_enums.h"
#include "kilelogwidget.h"
#include "kileoutputwidget.h"
#include "kileinfo.h"
#include "latexoutputfilter.h"
#include "latexoutputinfo.h"
#include "kiledocmanager.h"
#include "kilesidebar.h"

KileErrorHandler::KileErrorHandler(TQObject *tqparent, KileInfo *info, const char *name)
 : TQObject(tqparent, name), m_ki(info), m_nCurrentError(-1)
{
}


KileErrorHandler::~KileErrorHandler()
{
}


void KileErrorHandler::reset()
{
	m_ki->setLogPresent(false);
	m_nCurrentError = -1;
}

void KileErrorHandler::ViewLog()
{
	m_ki->outputView()->showPage(m_ki->logWidget());
	m_ki->setLogPresent(false);

	TQString cn = m_ki->getCompileName();
	if ( m_ki->outputFilter()->source() !=  cn )
	{
		m_ki->outputFilter()->setSource(cn);
		m_ki->outputFilter()->Run(cn.tqreplace(TQRegExp("\\..*$"),".log"));
	}

	TQString log = m_ki->outputFilter()->log();

	if (!log.isNull())
	{
		m_ki->logWidget()->setText(log);
		m_ki->logWidget()->highlight();
		m_ki->logWidget()->scrollToBottom();
		m_ki->setLogPresent(true);
	}
	else
	{
		m_ki->logWidget()->printProblem(KileTool::Error, i18n("Cannot open log file; did you run LaTeX?"));
	}
}

void KileErrorHandler::jumpToFirstError()
{
	int sz = m_ki->outputInfo()->size();
	for (int i = 0; i < sz; ++i )
	{
		if ( (*m_ki->outputInfo())[i].type() == LatexOutputInfo::itmError )
		{
			jumpToProblem(&(*m_ki->outputInfo())[i]);
			m_nCurrentError = i;
			m_ki->logWidget()->highlightByIndex(i, sz, -1);
			break;
		}
	}
}

void KileErrorHandler::jumpToProblem(OutputInfo *info)
{
	TQString file = m_ki->getFullFromPrettyName(info->source());

	if ( !file.isNull() )
	{
		m_ki->docManager()->fileOpen(KURL::fromPathOrURL(file));
		int line = info->sourceLine() > 0 ? (info->sourceLine() - 1) : 0;

		Kate::Document *doc = m_ki->docManager()->docFor(KURL::fromPathOrURL(file));
		if ( doc ) 
		{
			Kate::View* view = (Kate::View*)doc->views().first();
			if (view) view->setCursorPosition(line, 0);
		}
	}
}

void KileErrorHandler::showLogResults(const TQString &src)
{
	m_ki->logWidget()->clear();
	m_ki->setLogPresent(false);
	m_ki->outputFilter()->setSource(src);
	TQFileInfo fi(src);
	TQString lf = fi.dirPath(true) + '/' + fi.baseName(true) + ".log";
	m_ki->logWidget()->printMsg(KileTool::Info, i18n("Detecting errors (%1), please wait ...").tqarg(lf), i18n("Log") );
	if ( ! m_ki->outputFilter()->Run( lf ) )
	{
		
		m_ki->outputFilter()->setSource(TQString());
		return;
	}
	else
		m_ki->logWidget()->printMsg(KileTool::Info, i18n("Done."), i18n("Log") );
}

void KileErrorHandler::jumpToProblem(int type, bool forward)
{
	static LatexOutputInfoArray::iterator it;

	//if the current log file does not belong to the files the user is viewing
	//reparse the correct log file
	TQString cn = m_ki->getCompileName();
	bool correctlogfile = (cn == m_ki->outputFilter()->source());
	if ( ! correctlogfile ) showLogResults(cn);

	if (!m_ki->outputInfo()->isEmpty())
	{
		int sz = m_ki->outputInfo()->size();
		int pl = forward ? 1 : -1;
		bool found = false;

		//look for next problem of requested type
		for ( int i = 0; i < sz; ++i )
		{
			//always look at the whole outputInfo array, but start
			//at the problem adjacent to the current error
			//if we go beyond the bounds of the array we use
			//a simple "modulo" calculation to get within bounds again
			int index = (m_nCurrentError + (i + 1) *pl) % sz;
			while ( index < 0 ) index += sz;

			if ( (*m_ki->outputInfo())[index].type() == type )
			{
				m_nCurrentError = index;
				found = true;
				break;
			}
		}

		if ( !found ) return;

		//If the log file is being viewed, use this to jump to the errors,
		//otherwise, use the error summary display
		if (m_ki->logPresent())
			m_ki->logWidget()->highlight( (*m_ki->outputInfo())[m_nCurrentError].outputLine(), pl );
 		else
 			m_ki->logWidget()->highlightByIndex(m_nCurrentError, sz, pl);

		jumpToProblem(&(*m_ki->outputInfo())[m_nCurrentError]);
	}

	if (m_ki->outputInfo()->isEmpty() && correctlogfile)
	{
		m_ki->logWidget()->append("\n<font color=\"#008800\">"+ i18n("No LaTeX errors detected.") + "</font>");
	}
}

void KileErrorHandler::NextError()
{
	jumpToProblem(LatexOutputInfo::itmError, true);
}

void KileErrorHandler::PreviousError()
{
	jumpToProblem(LatexOutputInfo::itmError, false);
}

void KileErrorHandler::NextWarning()
{
	jumpToProblem(LatexOutputInfo::itmWarning, true);
}

void KileErrorHandler::PreviousWarning()
{
	jumpToProblem(LatexOutputInfo::itmWarning, false);
}

void KileErrorHandler::NextBadBox()
{
	jumpToProblem(LatexOutputInfo::itmBadBox, true);
}

void KileErrorHandler::PreviousBadBox()
{
	jumpToProblem(LatexOutputInfo::itmBadBox, false);
}

#include "kileerrorhandler.moc"