summaryrefslogtreecommitdiffstats
path: root/src/UiGuiHighlighter.cpp
blob: ac74909e8d3dec7a9c3cda50671c15e1694c120c (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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/***************************************************************************
 *   Copyright (C) 2006-2012 by Thomas Schweitzer                          *
 *   thomas-schweitzer(at)arcor.de                                         *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2.0 as   *
 *   published by the Free Software Foundation.                            *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program in the file LICENSE.GPL; if not, write to the *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "UiGuiHighlighter.h"

#include "SettingsPaths.h"

#include <QSettings>
#include <QMenu>
#include <QScrollBar>
#include <QCoreApplication>

#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexer.h>
#include <Qsci/qscilexerbash.h>
#include <Qsci/qscilexerbatch.h>
#include <Qsci/qscilexercmake.h>
#include <Qsci/qscilexercpp.h>
#include <Qsci/qscilexercsharp.h>
#include <Qsci/qscilexercss.h>
#include <Qsci/qscilexerd.h>
#include <Qsci/qscilexerdiff.h>
#if ( QSCINTILLA_VERSION >= 0x020300 )
#include <Qsci/qscilexerfortran.h>
#include <Qsci/qscilexerfortran77.h>
#endif
#include <Qsci/qscilexerhtml.h>
#include <Qsci/qscilexeridl.h>
#include <Qsci/qscilexerjava.h>
#include <Qsci/qscilexerjavascript.h>
#include <Qsci/qscilexerlua.h>
#include <Qsci/qscilexermakefile.h>
#if ( QSCINTILLA_VERSION >= 0x020300 )
#include <Qsci/qscilexerpascal.h>
#endif
#include <Qsci/qscilexerperl.h>
#if ( QSCINTILLA_VERSION >= 0x020300 )
#include <Qsci/qscilexerpostscript.h>
#endif
#include <Qsci/qscilexerpov.h>
#include <Qsci/qscilexerproperties.h>
#include <Qsci/qscilexerpython.h>
#include <Qsci/qscilexerruby.h>
#if ( QSCINTILLA_VERSION >= 0x020400 )
#include <Qsci/qscilexerspice.h>
#endif
#include <Qsci/qscilexersql.h>
#if ( QSCINTILLA_VERSION >= 0x020300 )
#include <Qsci/qscilexertcl.h>
#endif
#include <Qsci/qscilexertex.h>
#if ( QSCINTILLA_VERSION >= 0x020400 )
#include <Qsci/qscilexerverilog.h>
#endif
#include <Qsci/qscilexervhdl.h>
#if ( QSCINTILLA_VERSION >= 0x020300 )
#include <Qsci/qscilexerxml.h>
#include <Qsci/qscilexeryaml.h>
#endif

//! \defgroup grp_EditorComponent All concerning editor widget.

/*!
    \class UiGuiHighlighter
    \ingroup grp_EditorComponent
    \brief UiGuiHighlighter used for selecting the syntax highlighter/lexer for the QsciScintilla component.
*/

/*!
    \brief The constructor initializes some regular expressions and keywords to identify cpp tokens
 */
UiGuiHighlighter::UiGuiHighlighter(QsciScintilla *parent) : QObject(parent) {
    _qsciEditorParent = parent;

    // Create the highlighter _settings object from the UiGuiSyntaxHighlightConfig.ini file.
    _settings = new QSettings(SettingsPaths::getSettingsPath() + "/UiGuiSyntaxHighlightConfig.ini", QSettings::IniFormat, this);

    _highlightningIsOn = true;

    _mapHighlighternameToExtension["Bash"] = QStringList() << "sh";
    _mapHighlighternameToExtension["Batch"] = QStringList() << "bat";
    _mapHighlighternameToExtension["CMake"] = QStringList() << "cmake";
    _mapHighlighternameToExtension["C++"] = QStringList() << "c" << "h" << "cpp" << "hpp" << "cxx" << "hxx";
    _mapHighlighternameToExtension["C#"] = QStringList() << "cs";
    _mapHighlighternameToExtension["CSS"] = QStringList() << "css";
    _mapHighlighternameToExtension["D"] = QStringList() << "d";
    _mapHighlighternameToExtension["Diff"] = QStringList() << "diff";
#if ( QSCINTILLA_VERSION >= 0x020300 )
    _mapHighlighternameToExtension["Fortran"] = QStringList() << "f" << "for" << "f90";
    _mapHighlighternameToExtension["Fortran77"] = QStringList() << "f77";
#endif
    _mapHighlighternameToExtension["HTML"] = QStringList() << "html" << "htm";
    _mapHighlighternameToExtension["IDL"] = QStringList() << "idl";
    _mapHighlighternameToExtension["Java"] = QStringList() << "java";
    _mapHighlighternameToExtension["JavaScript"] = QStringList() << "js";
    _mapHighlighternameToExtension["LUA"] = QStringList() << "lua";
    _mapHighlighternameToExtension["Makefile"] = QStringList() << "makefile";
#if ( QSCINTILLA_VERSION >= 0x020300 )
    _mapHighlighternameToExtension["Pascal"] = QStringList() << "pas";
#endif
    _mapHighlighternameToExtension["Perl"] = QStringList() << "perl" << "pl" << "pm";
    _mapHighlighternameToExtension["PHP"] = QStringList() << "php";
#if ( QSCINTILLA_VERSION >= 0x020300 )
    _mapHighlighternameToExtension["PostScript"] = QStringList() << "ps" << "eps" << "pdf" << "ai" << "fh";
#endif
    _mapHighlighternameToExtension["POV"] = QStringList() << "pov";
    _mapHighlighternameToExtension["Ini"] = QStringList() << "ini";
    _mapHighlighternameToExtension["Python"] = QStringList() << "py";
    _mapHighlighternameToExtension["Ruby"] = QStringList() << "rub" << "rb";
#if ( QSCINTILLA_VERSION >= 0x020400 )
    _mapHighlighternameToExtension["Spice"] = QStringList() << "cir";
#endif
    _mapHighlighternameToExtension["SQL"] = QStringList() << "sql";
#if ( QSCINTILLA_VERSION >= 0x020300 )
    _mapHighlighternameToExtension["TCL"] = QStringList() << "tcl";
#endif
    _mapHighlighternameToExtension["TeX"] = QStringList() << "tex";
#if ( QSCINTILLA_VERSION >= 0x020400 )
    _mapHighlighternameToExtension["Verilog"] = QStringList() << "v" << "vh";
#endif
    _mapHighlighternameToExtension["VHDL"] = QStringList() << "vhdl";
    _mapHighlighternameToExtension["XML"] = QStringList() << "xml";
#if ( QSCINTILLA_VERSION >= 0x020300 )
    _mapHighlighternameToExtension["YAML"] = QStringList() << "yaml";
#endif

    _lexer = NULL;

    // This code is only for testing.
    /*
    foreach(QStringList extensionList, _mapHighlighternameToExtension.values() ) {
        setLexerForExtension( extensionList.at(0) );
    }
    */

    // Set default highlighter to C++ highlighter.
    setLexerForExtension( "cpp" );
}


/*!
    \brief Returns the available highlighters as QStringList.
 */
QStringList UiGuiHighlighter::getAvailableHighlighters() {
    return _mapHighlighternameToExtension.keys();
}


/*!
    \brief This slot handles signals coming from selecting another syntax highlighter.
 */
void UiGuiHighlighter::setHighlighterByAction(QAction* highlighterAction) {
    QString highlighterName = highlighterAction->text();
    setLexerForExtension( _mapHighlighternameToExtension[highlighterName].first() );
    //TODO: This is really no nice way. How do it better?
    // Need to do this "text update" to update the syntax highlighting. Otherwise highlighting is wrong.
    int scrollPos = _qsciEditorParent->verticalScrollBar()->value();
    _qsciEditorParent->setText( _qsciEditorParent->text() );
    _qsciEditorParent->verticalScrollBar()->setValue(scrollPos);
}


/*!
    \brief Turns the syntax parser on.
*/
void UiGuiHighlighter::turnHighlightOn() {
    _highlightningIsOn = true;
    _qsciEditorParent->setLexer(_lexer);
    readCurrentSettings("");
}

/*!
    \brief Turns the syntax parser off.
*/
void UiGuiHighlighter::turnHighlightOff() {
    _highlightningIsOn = false;
    _qsciEditorParent->setLexer();
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
    _qsciEditorParent->setFont( QFont("Courier", 10, QFont::Normal) );
    _qsciEditorParent->setMarginsFont( QFont("Courier", 10, QFont::Normal) );
#else
    _qsciEditorParent->setFont( QFont("Monospace", 10, QFont::Normal) );
    _qsciEditorParent->setMarginsFont( QFont("Monospace", 10, QFont::Normal) );
#endif
}


/*!
    \brief Read the settings for the current lexer from the settings file.
 */
//TODO: Refactor this function so that the coding style and variable names suit better.
bool UiGuiHighlighter::readCurrentSettings( const char *prefix ) {
    bool ok, flag, rc = true;
    int num;
    QString key;

    // Reset lists containing fonts and colors for each style
    _fontForStyles.clear();
    _colorForStyles.clear();

    // Read the styles.
    for (int i = 0; i < 128; ++i) {
        // Ignore invalid styles.
        if ( _lexer->description(i).isEmpty() )
            continue;

        key.sprintf( "%s/%s/style%d/", prefix, _lexer->language(), i );
        key.replace("+", "p");

        // Read the foreground color.
        ok = _settings->contains(key + "color");
        num = _settings->value(key + "color", 0).toInt();

        if (ok)
            setColor( QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i );
        else
            rc = false;

        // Read the end-of-line fill.
        ok = _settings->contains(key + "eolfill");
        flag = _settings->value(key + "eolfill", false).toBool();

        if (ok)
            _lexer->setEolFill( flag, i );
        else
            rc = false;

        // Read the font
        QStringList fdesc;

        ok = _settings->contains(key + "font");
        fdesc = _settings->value(key + "font").toStringList();

        if (ok && fdesc.count() == 5) {
            QFont f;

#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
            f.setFamily(fdesc[0]);
#else
            if ( fdesc[0].contains("courier", Qt::CaseInsensitive) )
                f.setFamily("Monospace");
            else
                f.setFamily(fdesc[0]);
#endif
            f.setPointSize(fdesc[1].toInt());
            f.setBold(fdesc[2].toInt());
            f.setItalic(fdesc[3].toInt());
            f.setUnderline(fdesc[4].toInt());

            setFont(f, i);
        }
        else
            rc = false;

        // Read the background color.
        ok = _settings->contains(key + "paper");
        num = _settings->value(key + "paper", 0).toInt();

        if (ok)
            _lexer->setPaper( QColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i );
        else
            rc = false;
    }

    // Read the properties.
    key.sprintf( "%s/%s/properties/", prefix, _lexer->language() );

    _lexer->refreshProperties();

    return rc;
}


/*!
    \brief Write the settings for the current lexer to the settings file.
 */
void UiGuiHighlighter::writeCurrentSettings( const char *prefix ) {
    QString key;

    // Write the styles.
    for (int i = 0; i < 128; ++i) {
        // Ignore invalid styles.
        if ( _lexer->description(i).isEmpty() )
            continue;

        int num;
        QColor c;

        key.sprintf( "%s/%s/style%d/", prefix, _lexer->language(), i );
        key.replace("+", "p");

        // Write style name
        _settings->setValue( key + "", _lexer->description(i) );

        // Write the foreground color.
        if ( _colorForStyles.contains(i) ) {
            c = _colorForStyles[i];
        }
        else {
            c = _lexer->color(i);
        }
        num = (c.red() << 16) | (c.green() << 8) | c.blue();

        _settings->setValue(key + "color", num);

        // Write the end-of-line fill.
        _settings->setValue( key + "eolfill", _lexer->eolFill(i) );

        // Write the font
        QStringList fdesc;
        QString fmt("%1");
        QFont f;

        if ( _fontForStyles.contains(i) ) {
            f = _fontForStyles[i];
        }
        else {
            f = _lexer->font(i);
        }

        fdesc += f.family();
        fdesc += fmt.arg( f.pointSize() );

        // The casts are for Borland.
        fdesc += fmt.arg( (int)f.bold() );
        fdesc += fmt.arg( (int)f.italic() );
        fdesc += fmt.arg( (int)f.underline() );

        _settings->setValue(key + "font", fdesc);

        // Write the background color.
        c = _lexer->paper(i);
        num = (c.red() << 16) | (c.green() << 8) | c.blue();

        _settings->setValue(key + "paper", num);
    }
}


/*!
    \brief Sets the \a color for the given \a style.
 */
void UiGuiHighlighter::setColor(const QColor &color, int style) {
    _colorForStyles[style] = color;
    _lexer->setColor( color, style );
}


/*!
    \brief Sets the \a font for the given \a style.
 */
void UiGuiHighlighter::setFont(const QFont &font, int style) {
    _fontForStyles[style] = font;
    _lexer->setFont( font, style );
}


/*!
    \brief Sets the to be used lexer by giving his name.
 */
void UiGuiHighlighter::setLexerByName( QString lexerName ) {
    setLexerForExtension( _mapHighlighternameToExtension[lexerName].first() );
}


/*!
    \brief Sets the proper highlighter / lexer for the given file \a extension. Returns the index of the used lexer in the list.
 */
int UiGuiHighlighter::setLexerForExtension( QString extension ) {
    int indexOfHighlighter = 0;
    extension = extension.toLower();

    if ( _lexer != NULL ) {
        writeCurrentSettings("");
        delete _lexer;
    }

    if ( extension == "cpp" || extension == "hpp" || extension == "c" || extension == "h" || extension == "cxx" || extension == "hxx" ) {
        _lexer = new QsciLexerCPP();
    }
    else if ( extension == "sh" ) {
        _lexer = new QsciLexerBash();
    }
    else if ( extension == "bat" ) {
        _lexer = new QsciLexerBatch();
    }
    else if ( extension == "cmake" ) {
        _lexer = new QsciLexerCMake();
    }
    else if ( extension == "cs" ) {
        _lexer = new QsciLexerCSharp();
    }
    else if ( extension == "css" ) {
        _lexer = new QsciLexerCSS();
    }
    else if ( extension == "d" ) {
        _lexer = new QsciLexerD();
    }
    else if ( extension == "diff" ) {
        _lexer = new QsciLexerDiff();
    }
#if ( QSCINTILLA_VERSION >= 0x020300 )
    else if ( extension == "f" || extension == "for" || extension == "f90" ) {
        _lexer = new QsciLexerFortran();
    }
    else if ( extension == "f77" ) {
        _lexer = new QsciLexerFortran77();
    }
#endif
    else if ( extension == "html" || extension == "htm" ) {
        _lexer = new QsciLexerHTML();
    }
    else if ( extension == "idl" ) {
        _lexer = new QsciLexerIDL();
    }
    else if ( extension == "java" ) {
        _lexer = new QsciLexerJava();
    }
    else if ( extension == "js" ) {
        _lexer = new QsciLexerJavaScript();
    }
    else if ( extension == "lua" ) {
        _lexer = new QsciLexerLua();
    }
    else if ( extension == "makefile" ) {
        _lexer = new QsciLexerMakefile();
    }
#if ( QSCINTILLA_VERSION >= 0x020300 )
    else if ( extension == "pas" ) {
        _lexer = new QsciLexerPascal();
    }
#endif
    else if ( extension == "perl" || extension == "pl" || extension == "pm" ) {
        _lexer = new QsciLexerPerl();
    }
    else if ( extension == "php" ) {
        _lexer = new QsciLexerHTML();
    }
#if ( QSCINTILLA_VERSION >= 0x020300 )
    else if ( extension == "ps" || extension == "eps" || extension == "pdf" || extension == "ai" || extension == "fh") {
        _lexer = new QsciLexerPostScript();
    }
#endif
    else if ( extension == "pov" ) {
        _lexer = new QsciLexerPOV();
    }
    else if ( extension == "ini" ) {
        _lexer = new QsciLexerProperties();
    }
    else if ( extension == "py" ) {
        _lexer = new QsciLexerPython();
    }
    else if ( extension == "rub" || extension == "rb" ) {
        _lexer = new QsciLexerRuby();
    }
#if ( QSCINTILLA_VERSION >= 0x020400 )
    else if ( extension == "spice?" ) {
        _lexer = new QsciLexerSpice();
    }
#endif
    else if ( extension == "sql" ) {
        _lexer = new QsciLexerSQL();
    }
#if ( QSCINTILLA_VERSION >= 0x020300 )
    else if ( extension == "tcl" ) {
        _lexer = new QsciLexerTCL();
    }
#endif
    else if ( extension == "tex" ) {
        _lexer = new QsciLexerTeX();
    }
#if ( QSCINTILLA_VERSION >= 0x020400 )
    else if ( extension == "vlog?" ) {
        _lexer = new QsciLexerVerilog();
    }
#endif
    else if ( extension == "vhdl" ) {
        _lexer = new QsciLexerVHDL();
    }
    else if ( extension == "xml" ) {
#if ( QSCINTILLA_VERSION >= 0x020300 )
        _lexer = new QsciLexerXML();
#else
        _lexer = new QsciLexerHTML();
#endif
    }
#if ( QSCINTILLA_VERSION >= 0x020300 )
    else if ( extension == "yaml" ) {
        _lexer = new QsciLexerYAML();
    }
#endif
    else {
        _lexer = new QsciLexerCPP();
        extension = "cpp";
    }

    // Find the index of the selected _lexer.
    indexOfHighlighter = 0;
    while ( !_mapHighlighternameToExtension.values().at(indexOfHighlighter).contains(extension) ) {
        indexOfHighlighter++;
    }

    // Set the _lexer for the QScintilla widget.
    if ( _highlightningIsOn ) {
        _qsciEditorParent->setLexer(_lexer);
    }

    // Read the _settings for the _lexer properties from file.
    readCurrentSettings("");

    return indexOfHighlighter;
}