summaryrefslogtreecommitdiffstats
path: root/src/kile/kilehelp.cpp
blob: 3e89b3414277f83b88d9fffeda23751f0b7cc855 (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
/***************************************************************************
    date                 : Feb 12 2007
    version              : 0.30
    copyright            : (C) 2004-2007 by Holger Danielsson
    email                : holger.danielsson@versanet.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 "kilehelp.h"

#include <tqdir.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqregexp.h>
#include <tqfileinfo.h>

#include <kapplication.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include "kiledebug.h"
#include <klocale.h>
#include <kate/document.h>

#include "kiletool_enums.h"
#include "kiletoolmanager.h"
#include "kileviewmanager.h"
#include "kileinfo.h"
#include "kilelogwidget.h"
#include "kilestdtools.h"
#include "kileconfig.h"


// tbraun 27.06.2007
// it _looks_ like texlive 2007 has the same tqlayout than texlive 2005 so don't get confused about the variable names :)

namespace KileHelp
{

	Help::Help(KileDocument::EditorExtension *edit) : m_edit(edit), m_userhelp(0L)
	{
		readHelpList("latex-kile.lst",m_dictHelpKile);
		initTexDocumentation();
	}
	
	Help::~Help() 
	{
		delete m_userhelp;
	}
	
	void Help::initTexDocumentation()
	{
		// use documentation for teTeX v2.x, v3.x or TexLive2005
		m_texdocPath = KileConfig::location();
		
		// first check for TexLive2005
		TQString texref = m_texdocPath + "/english/tex-refs";
		TQDir dir(texref);
		if ( dir.exists() )
		{
			// we found TexLive2005
			m_texVersion = TEXLIVE2005;
			m_texReference = "/english/tex-refs/";
			readHelpList("latex-texlive-3.9.lst",m_dictHelpTex);
		}
		else
		{
			// now we check for tetex3
			dir.setPath(m_texdocPath + "/latex/tex-refs");
			if ( dir.exists() ) 
			{
				m_texVersion = TETEX3;
				// check if this is buggy tetex3.0 or an updated version with subdirectory 'html'
				dir.setPath(m_texdocPath + "/latex/tex-refs/html");
				m_texReference = ( dir.exists() ) 
				               ? "/latex/tex-refs/html/" : "/latex/tex-refs/";
				readHelpList("latex-tetex3.lst",m_dictHelpTex);
			}
			else
			{
				// we set it to tetex2 (what else should it be?)
				m_texVersion = TETEX2;
				m_texReference = "/latex/latex2e-html/";
				readHelpList("latex-tetex.lst",m_dictHelpTex);
			}
		}
	}

	////////////////////// update path to TeX documentation  //////////////////////

	void Help::update()
	{
		if ( m_texdocPath != KileConfig::location() )
			initTexDocumentation();
	}

	////////////////////// set parameter/initialize user help //////////////////////
	
	void Help::setUserhelp(KileTool::Manager *manager, KMenuBar *menubar)
	{ 
		m_manager = manager;
		m_userhelp = new UserHelp(manager,menubar);
	}
	
	void Help::enableUserhelpEntries(bool state)
	{ 
		if ( m_userhelp )
			m_userhelp->enableUserHelpEntries(state);
	}
	////////////////////// show help //////////////////////
	
	void Help::showHelpFile(const TQString &parameter)
	{
		KileTool::ViewHTML *tool = new KileTool::ViewHTML("ViewHTML", m_manager, false);
		tool->setFlags(KileTool::NeedSourceExists | KileTool::NeedSourceRead);
		//FIXME strip the #label part of the source (not the target),
		//somehow this is already done somewhere (by accident), 
		//bad to rely on it
		tool->setMsg(KileTool::NeedSourceExists, i18n("Sorry, could not find the teTeX documentation at %1; set the correct path in Settings->Configure Kile->Help."));
		tool->setSource(parameter);
		tool->setTargetPath(TQFileInfo(parameter).fileName());
		tool->prepareToRun();
		m_manager->run(tool);
	}

	void Help::helpKeyword()
	{
		//FIXME: we should have a better way to access the current view
		helpKeyword(m_manager->info()->viewManager()->currentTextView());
	}

////////////////////// Help: TexDoc //////////////////////
	
	void Help::helpDocBrowser()
	{
		KileDialog::TexDocDialog *dlg = new KileDialog::TexDocDialog();
		dlg->exec();
		delete dlg;
	}

////////////////////// Help: TeTeX //////////////////////

	void Help::helpTexGuide()
	{
		TQString filename;

		switch ( m_texVersion )
		{
			case TEXLIVE2005:
				filename = "english/texlive-en/live.html";
				break;
			case TETEX3:
				filename = "index.html";
				break;
			case TETEX2:
				filename = "newhelpindex.html";
				break;
			default:
				return;
		}

		showHelpFile( m_texdocPath + '/' + filename );
	}

////////////////////// Help: LaTeX //////////////////////

	void Help::helpLatex(Type type)
	{
		TQString link;
		
		if ( m_texVersion == TEXLIVE2005) 
		{
			switch ( type )
			{
				case HelpLatexIndex:
					link = "tex-refs.html#latex";
					break;
				case HelpLatexCommand:
					link = "tex-refs.html#tex-refs-idx";
					break;
				case HelpLatexSubject:
					link = "tex-refs.html#commands";
					break;
				case HelpLatexEnvironment:
					link = "tex-refs.html#env-latex";
					break;
				default:
					return;
			}
		}
		else if ( m_texVersion == TETEX3) 
		{
			switch ( type )
			{
				case HelpLatexIndex:
					link = "latex.html#latex";
					break;
				case HelpLatexCommand:
					link = "appendices.html#tex-refs-idx";
					break;
				case HelpLatexSubject:
					link = "latex.html#commands";
					break;
				case HelpLatexEnvironment:
					link = "latex.html#environments";
					break;
				default:
					return;
			}
		}
		else
		{
			switch ( type )
			{
				case HelpLatexCommand:
					link = "ltx-2.html#cmd";
					break;
				case HelpLatexSubject:
					link = "ltx-2.html#subj";
					break;
				case HelpLatexEnvironment:
					link = "ltx-2.html#env";
					break;
				case HelpLatexIndex:
					link = "ltx-2.html";
					break;
				default:
					return;
			}
		} 
		
		// show help file
		TQString texversion;
		if ( m_texVersion == TEXLIVE2005 )
			texversion = "TexLive 2005";
		else if ( m_texVersion == TETEX3 )
			texversion = "teTeX v3.x";
		else 
			texversion = "teTeX v2.x";
		KILE_DEBUG() << "TeX Version: "<< texversion << " link=" << link << endl;

		showHelpFile( m_texdocPath + m_texReference + link );
	}

	////////////////////// Help: Keyword //////////////////////

	// Context help: user either Kile LaTeX help or the help files shipped with teTeX,
	void Help::helpKeyword(Kate::View *view)                   // dani 04.08.2004
	{
		int type = (0 == KileConfig::use()) ? HelpKileRefs : HelpTexRefs;
		switch ( type )
		{
		case HelpTexRefs:
			helpTexRefsKeyword(view);
			break;
		case HelpKileRefs:
			helpKileRefsKeyword(view);
			break;
		}
	}

	void Help::helpTexRefsKeyword(Kate::View *view)
	{
		TQString word = getKeyword(view);
		KILE_DEBUG() << "keyword: " << word << endl;
		if ( !word.isNull() && m_dictHelpTex.tqcontains(word) )
		{
			KILE_DEBUG() << "about to show help for " << word << " (section " << m_dictHelpTex[word] << " )" << endl;
			showHelpFile( m_texdocPath + m_texReference + m_dictHelpTex[word] );
		}
		else
			noHelpAvailableFor(word);
	}

	void Help::helpKileRefsKeyword(Kate::View *view)
	{
		TQString kilehelp = KGlobal::dirs()->findResource("html","en/kile/latexhelp.html");
		KILE_DEBUG() << "kilehelp = " << kilehelp << endl;
		if ( ! kilehelp.isEmpty() )
		{
			TQString word = getKeyword(view);
			KILE_DEBUG() << "word = " << word << " " << m_dictHelpKile.tqcontains(word) << endl;
			if ( !word.isNull() && m_dictHelpKile.tqcontains(word) )
			{
				showHelpFile( kilehelp + '#' + m_dictHelpKile[word] );
			}
			else
				noHelpAvailableFor(word);
		}
	}

	void Help::noHelpAvailableFor(const TQString &word)
	{
		m_manager->info()->logWidget()->printMsg(KileTool::Error, i18n("Sorry, no help available for %1.").tqarg(word), i18n("Help"));
	}
	
	TQString Help::getKeyword(Kate::View *view)         
	{
		if ( !view )
			return TQString();
		
		// get current position
		uint row,col,col1,col2;
		TQString word;
		Kate::Document *doc = view->getDoc();
		view->cursorPositionReal(&row,&col);
		
		if ( m_edit->getCurrentWord(doc,row,col,KileDocument::EditorExtension::smTex,word,col1,col2) )
		   // There is no starred keyword in the references. So if     // dani 04.08.2004
			// we find one, we better try the unstarred keyword.
			if ( word.right(1) == "*" )
				return word.left( word.length()-1 );
			else
				return word;
		else
			return TQString();
	}

//////////////////// read help lists ////////////////////

	void Help::readHelpList(const TQString &filename,TQMap<TQString,TQString> &map)
	{
		TQString file = KGlobal::dirs()->findResource("appdata","help/" + filename);
		if ( file.isEmpty() )
		{
			KILE_DEBUG() << "   file not found: " << filename << endl;
			return;
		}

		KILE_DEBUG() << "   read file: " << filename << endl;
		TQRegExp reg("\\s*(\\S+)\\s*=>\\s*(\\S+)");

		TQFile f(file);
		if ( f.open(IO_ReadOnly) )
		{     // file opened successfully
			TQTextStream t( &f );         // use a text stream
			while ( ! t.eof() )
			{        // until end of file...
			TQString s = t.readLine().stripWhiteSpace();       // line of text excluding '\n'
			if ( ! (s.isEmpty() || s.at(0)=='#') )
			{
				int pos = reg.search(s);
				if ( pos != -1 )
				{
				map[reg.cap(1)] = reg.cap(2);
				}
			}
			}
			f.close();
		}
	}

}

#include "kilehelp.moc"