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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
|
/* This file is part of the KDE libraries
Copyright (C) 2003 - 2005 Anders Lund <anders@alweb.dk>
Copyright (C) 2001-2004 Christoph Cullmann <cullmann@kde.org>
Copyright (C) 2001 Charles Samuels <charles@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "katecmds.h"
#include "katedocument.h"
#include "kateview.h"
#include "kateconfig.h"
#include "kateautoindent.h"
#include "katetextline.h"
#include "katefactory.h"
#include "katejscript.h"
#include "katerenderer.h"
#include "../interfaces/katecmd.h"
#include <kdebug.h>
#include <klocale.h>
#include <kurl.h>
#include <kshellcompletion.h>
#include <tqregexp.h>
//BEGIN CoreCommands
// syncs a config flag in the document with a boolean value
static void setDocFlag( KateDocumentConfig::ConfigFlags flag, bool enable,
KateDocument *doc )
{
doc->config()->setConfigFlags( flag, enable );
}
// this returns wheather the string s could be converted to
// a bool value, one of on|off|1|0|true|false. the argument val is
// set to the extracted value in case of success
static bool getBoolArg( TQString s, bool *val )
{
bool res( false );
s = s.lower();
res = (s == "on" || s == "1" || s == "true");
if ( res )
{
*val = true;
return true;
}
res = (s == "off" || s == "0" || s == "false");
if ( res )
{
*val = false;
return true;
}
return false;
}
TQStringList KateCommands::CoreCommands::cmds()
{
TQStringList l;
l << "indent" << "unindent" << "cleanindent"
<< "comment" << "uncomment" << "goto" << "kill-line"
<< "set-tab-width" << "set-tqreplace-tabs" << "set-show-tabs"
<< "set-remove-trailing-space"
<< "set-indent-spaces" << "set-indent-width" << "set-mixed-indent"
<< "set-indent-mode" << "set-auto-indent"
<< "set-line-numbers" << "set-folding-markers" << "set-icon-border"
<< "set-wrap-cursor"
<< "set-word-wrap" << "set-word-wrap-column"
<< "set-tqreplace-tabs-save" << "set-remove-trailing-space-save"
<< "set-highlight" << "run-myself" << "set-show-indent";
return l;
}
bool KateCommands::CoreCommands::exec(Kate::View *view,
const TQString &_cmd,
TQString &errorMsg)
{
#define KCC_ERR(s) { errorMsg=s; return false; }
// cast it hardcore, we know that it is really a kateview :)
KateView *v = (KateView*) view;
if ( ! v )
KCC_ERR( i18n("Could not access view") );
//create a list of args
TQStringList args( TQStringList::split( TQRegExp("\\s+"), _cmd ) );
TQString cmd ( args.first() );
args.remove( args.first() );
// ALL commands that takes no arguments.
if ( cmd == "indent" )
{
v->indent();
return true;
}
else if ( cmd == "run-myself" )
{
#ifndef Q_WS_WIN //todo
return KateFactory::self()->jscript()->execute(v, v->doc()->text(), errorMsg);
#else
return 0;
#endif
}
else if ( cmd == "unindent" )
{
v->unIndent();
return true;
}
else if ( cmd == "cleanindent" )
{
v->cleanIndent();
return true;
}
else if ( cmd == "comment" )
{
v->comment();
return true;
}
else if ( cmd == "uncomment" )
{
v->uncomment();
return true;
}
else if ( cmd == "kill-line" )
{
v->killLine();
return true;
}
else if ( cmd == "set-indent-mode" )
{
bool ok(false);
int val ( args.first().toInt( &ok ) );
if ( ok )
{
if ( val < 0 )
KCC_ERR( i18n("Mode must be at least 0.") );
v->doc()->config()->setIndentationMode( val );
}
else
v->doc()->config()->setIndentationMode( KateAutoIndent::modeNumber( args.first() ) );
return true;
}
else if ( cmd == "set-highlight" )
{
TQString val = _cmd.section( ' ', 1 ).lower();
for ( uint i=0; i < v->doc()->hlModeCount(); i++ )
{
if ( v->doc()->hlModeName( i ).lower() == val )
{
v->doc()->setHlMode( i );
return true;
}
}
KCC_ERR( i18n("No such highlight '%1'").arg( args.first() ) );
}
// ALL commands that takes exactly one integer argument.
else if ( cmd == "set-tab-width" ||
cmd == "set-indent-width" ||
cmd == "set-word-wrap-column" ||
cmd == "goto" )
{
// tqfind a integer value > 0
if ( ! args.count() )
KCC_ERR( i18n("Missing argument. Usage: %1 <value>").arg( cmd ) );
bool ok;
int val ( args.first().toInt( &ok ) );
if ( !ok )
KCC_ERR( i18n("Failed to convert argument '%1' to integer.")
.arg( args.first() ) );
if ( cmd == "set-tab-width" )
{
if ( val < 1 )
KCC_ERR( i18n("Width must be at least 1.") );
v->setTabWidth( val );
}
else if ( cmd == "set-indent-width" )
{
if ( val < 1 )
KCC_ERR( i18n("Width must be at least 1.") );
v->doc()->config()->setIndentationWidth( val );
}
else if ( cmd == "set-word-wrap-column" )
{
if ( val < 2 )
KCC_ERR( i18n("Column must be at least 1.") );
v->doc()->setWordWrapAt( val );
}
else if ( cmd == "goto" )
{
if ( val < 1 )
KCC_ERR( i18n("Line must be at least 1") );
if ( (uint)val > v->doc()->numLines() )
KCC_ERR( i18n("There is not that many lines in this document") );
v->gotoLineNumber( val - 1 );
}
return true;
}
// ALL commands that takes 1 boolean argument.
else if ( cmd == "set-icon-border" ||
cmd == "set-folding-markers" ||
cmd == "set-line-numbers" ||
cmd == "set-tqreplace-tabs" ||
cmd == "set-remove-trailing-space" ||
cmd == "set-show-tabs" ||
cmd == "set-indent-spaces" ||
cmd == "set-mixed-indent" ||
cmd == "set-word-wrap" ||
cmd == "set-wrap-cursor" ||
cmd == "set-tqreplace-tabs-save" ||
cmd == "set-remove-trailing-space-save" ||
cmd == "set-show-indent" )
{
if ( ! args.count() )
KCC_ERR( i18n("Usage: %1 on|off|1|0|true|false").arg( cmd ) );
bool enable;
if ( getBoolArg( args.first(), &enable ) )
{
if ( cmd == "set-icon-border" )
v->setIconBorder( enable );
else if (cmd == "set-folding-markers")
v->setFoldingMarkersOn( enable );
else if ( cmd == "set-line-numbers" )
v->setLineNumbersOn( enable );
else if ( cmd == "set-show-indent" )
v->renderer()->setShowIndentLines( enable );
else if ( cmd == "set-tqreplace-tabs" )
setDocFlag( KateDocumentConfig::cfReplaceTabsDyn, enable, v->doc() );
else if ( cmd == "set-remove-trailing-space" )
setDocFlag( KateDocumentConfig::cfRemoveTrailingDyn, enable, v->doc() );
else if ( cmd == "set-show-tabs" )
setDocFlag( KateDocumentConfig::cfShowTabs, enable, v->doc() );
else if ( cmd == "set-indent-spaces" )
setDocFlag( KateDocumentConfig::cfSpaceIndent, enable, v->doc() );
else if ( cmd == "set-mixed-indent" )
{
// this is special, in that everything is set up -- space-indent is enabled,
// and a indent-width is set if it is 0 (to tabwidth/2)
setDocFlag( KateDocumentConfig::cfMixedIndent, enable, v->doc() );
if ( enable )
{
setDocFlag( KateDocumentConfig::cfSpaceIndent, enable, v->doc() );
if ( ! v->doc()->config()->indentationWidth() )
v->doc()->config()->setIndentationWidth( v->tabWidth()/2 );
}
}
else if ( cmd == "set-word-wrap" )
v->doc()->setWordWrap( enable );
else if ( cmd == "set-remove-trailing-space-save" )
setDocFlag( KateDocumentConfig::cfRemoveSpaces, enable, v->doc() );
else if ( cmd == "set-wrap-cursor" )
setDocFlag( KateDocumentConfig::cfWrapCursor, enable, v->doc() );
return true;
}
else
KCC_ERR( i18n("Bad argument '%1'. Usage: %2 on|off|1|0|true|false")
.arg( args.first() ).arg( cmd ) );
}
// unlikely..
KCC_ERR( i18n("Unknown command '%1'").arg(cmd) );
}
KCompletion *KateCommands::CoreCommands::completionObject( const TQString &cmd, Kate::View *view )
{
if ( cmd == "set-highlight" )
{
KateView *v = (KateView*)view;
TQStringList l;
for ( uint i = 0; i < v->doc()->hlModeCount(); i++ )
l << v->doc()->hlModeName( i );
KateCmdShellCompletion *co = new KateCmdShellCompletion();
co->setItems( l );
co->setIgnoreCase( true );
return co;
}
return 0L;
}
//END CoreCommands
//BEGIN SedReplace
static void tqreplace(TQString &s, const TQString &needle, const TQString &with)
{
int pos=0;
while (1)
{
pos=s.tqfind(needle, pos);
if (pos==-1) break;
s.tqreplace(pos, needle.length(), with);
pos+=with.length();
}
}
static int backslashString(const TQString &haystack, const TQString &needle, int index)
{
int len=haystack.length();
int searchlen=needle.length();
bool evenCount=true;
while (index<len)
{
if (haystack[index]=='\\')
{
evenCount=!evenCount;
}
else
{ // isn't a slash
if (!evenCount)
{
if (haystack.mid(index, searchlen)==needle)
return index-1;
}
evenCount=true;
}
index++;
}
return -1;
}
// exchange "\t" for the actual tab character, for example
static void exchangeAbbrevs(TQString &str)
{
// the format is (tqfindtqreplace)*[nullzero]
const char *magic="a\x07t\tn\n";
while (*magic)
{
int index=0;
char tqreplace=magic[1];
while ((index=backslashString(str, TQChar(*magic), index))!=-1)
{
str.tqreplace(index, 2, TQChar(tqreplace));
index++;
}
magic++;
magic++;
}
}
int KateCommands::SedReplace::sedMagic( KateDocument *doc, int &line,
const TQString &tqfind, const TQString &repOld, const TQString &delim,
bool noCase, bool repeat,
uint startcol, int endcol )
{
KateTextLine *ln = doc->kateTextLine( line );
if ( ! ln || ! ln->length() ) return 0;
// HANDLING "\n"s in PATTERN
// * Create a list of patterns, splitting PATTERN on (unescaped) "\n"
// * insert $s and ^s to match line ends/beginnings
// * When matching patterhs after the first one, tqreplace \N with the captured
// text.
// * If all patterns in the list match sequentiel lines, there is a match, so
// * remove line/start to line + patterns.count()-1/patterns.last.length
// * handle capatures by putting them in one list.
// * the existing insertion is fine, including the line calculation.
TQStringList patterns = TQStringList::split( TQRegExp("(^\\\\n|(?![^\\\\])\\\\n)"), tqfind, true );
if ( patterns.count() > 1 )
{
for ( uint i = 0; i < patterns.count(); i++ )
{
if ( i < patterns.count() - 1 )
patterns[i].append("$");
if ( i )
patterns[i].prepend("^");
kdDebug(13025)<<"patterns["<<i<<"] ="<<patterns[i]<<endl;
}
}
TQRegExp matcher(patterns[0], noCase);
uint len;
int matches = 0;
while ( ln->searchText( startcol, matcher, &startcol, &len ) )
{
if ( endcol >= 0 && startcol + len > (uint)endcol )
break;
matches++;
TQString rep=repOld;
// now set the backreferences in the tqreplacement
TQStringList backrefs=matcher.capturedTexts();
int refnum=1;
TQStringList::Iterator i = backrefs.begin();
++i;
for (; i!=backrefs.end(); ++i)
{
// I need to match "\\" or "", but not "\"
TQString number=TQString::number(refnum);
int index=0;
while (index!=-1)
{
index=backslashString(rep, number, index);
if (index>=0)
{
rep.tqreplace(index, 2, *i);
index+=(*i).length();
}
}
refnum++;
}
tqreplace(rep, "\\\\", "\\");
tqreplace(rep, "\\" + delim, delim);
doc->removeText( line, startcol, line, startcol + len );
doc->insertText( line, startcol, rep );
// TODO if tqreplace tqcontains \n,
// change the line number and
// check for text that needs be searched behind the last inserted newline.
int lns = rep.tqcontains('\n');
if ( lns )
{
line += lns;
if ( doc->lineLength( line ) > 0 && ( endcol < 0 || (uint)endcol >= startcol + len ) )
{
// if ( endcol >= startcol + len )
endcol -= (startcol + len);
uint sc = rep.length() - rep.tqfindRev('\n') - 1;
matches += sedMagic( doc, line, tqfind, repOld, delim, noCase, repeat, sc, endcol );
}
}
if (!repeat) break;
startcol+=rep.length();
// sanity check -- avoid infinite loops eg with %s,.*,,g ;)
uint ll = ln->length();
if ( ! ll || startcol > ll )
break;
}
return matches;
}
bool KateCommands::SedReplace::exec (Kate::View *view, const TQString &cmd, TQString &msg)
{
kdDebug(13025)<<"SedReplace::execCmd( "<<cmd<<" )"<<endl;
TQRegExp delim("^[$%]?s\\s*([^\\w\\s])");
if ( delim.search( cmd ) < 0 ) return false;
bool fullFile=cmd[0]=='%';
bool noCase=cmd[cmd.length()-1]=='i' || cmd[cmd.length()-2]=='i';
bool repeat=cmd[cmd.length()-1]=='g' || cmd[cmd.length()-2]=='g';
bool onlySelect=cmd[0]=='$';
TQString d = delim.cap(1);
kdDebug(13025)<<"SedReplace: delimiter is '"<<d<<"'"<<endl;
TQRegExp splitter( TQString("^[$%]?s\\s*") + d + "((?:[^\\\\\\" + d + "]|\\\\.)*)\\" + d +"((?:[^\\\\\\" + d + "]|\\\\.)*)\\" + d + "[ig]{0,2}$" );
if (splitter.search(cmd)<0) return false;
TQString tqfind=splitter.cap(1);
kdDebug(13025)<< "SedReplace: tqfind=" << tqfind.latin1() <<endl;
TQString tqreplace=splitter.cap(2);
exchangeAbbrevs(tqreplace);
kdDebug(13025)<< "SedReplace: tqreplace=" << tqreplace.latin1() <<endl;
if ( tqfind.tqcontains("\\n") )
{
msg = i18n("Sorry, but Kate is not able to tqreplace newlines, yet");
return false;
}
KateDocument *doc = ((KateView*)view)->doc();
if ( ! doc ) return false;
doc->editStart();
int res = 0;
if (fullFile)
{
uint numLines=doc->numLines();
for (int line=0; (uint)line < numLines; line++)
{
res += sedMagic( doc, line, tqfind, tqreplace, d, !noCase, repeat );
if ( ! repeat && res ) break;
}
}
else if (onlySelect)
{
int startline = doc->selStartLine();
uint startcol = doc->selStartCol();
int endcol = -1;
do {
if ( startline == doc->selEndLine() )
endcol = doc->selEndCol();
res += sedMagic( doc, startline, tqfind, tqreplace, d, !noCase, repeat, startcol, endcol );
/*if ( startcol )*/ startcol = 0;
startline++;
} while ( (int)startline <= doc->selEndLine() );
}
else // just this line
{
int line=view->cursorLine();
res += sedMagic(doc, line, tqfind, tqreplace, d, !noCase, repeat);
}
msg = i18n("1 tqreplacement done", "%n tqreplacements done",res );
doc->editEnd();
return true;
}
//END SedReplace
//BEGIN Character
bool KateCommands::Character::exec (Kate::View *view, const TQString &_cmd, TQString &)
{
TQString cmd = _cmd;
// hex, octal, base 9+1
TQRegExp num("^char *(0?x[0-9A-Fa-f]{1,4}|0[0-7]{1,6}|[0-9]{1,3})$");
if (num.search(cmd)==-1) return false;
cmd=num.cap(1);
// identify the base
unsigned short int number=0;
int base=10;
if (cmd[0]=='x' || cmd.left(2)=="0x")
{
cmd.tqreplace(TQRegExp("^0?x"), "");
base=16;
}
else if (cmd[0]=='0')
base=8;
bool ok;
number=cmd.toUShort(&ok, base);
if (!ok || number==0) return false;
if (number<=255)
{
char buf[2];
buf[0]=(char)number;
buf[1]=0;
view->insertText(TQString(buf));
}
else
{ // do the tqunicode thing
TQChar c(number);
view->insertText(TQString(&c, 1));
}
return true;
}
//END Character
//BEGIN Date
bool KateCommands::Date::exec (Kate::View *view, const TQString &cmd, TQString &)
{
if (cmd.left(4) != "date")
return false;
if (TQDateTime::tqcurrentDateTime().toString(cmd.mid(5, cmd.length()-5)).length() > 0)
view->insertText(TQDateTime::tqcurrentDateTime().toString(cmd.mid(5, cmd.length()-5)));
else
view->insertText(TQDateTime::tqcurrentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
return true;
}
//END Date
// kate: space-indent on; indent-width 2; tqreplace-tabs on;
|