summaryrefslogtreecommitdiffstats
path: root/krusader/UserAction/expander.cpp
blob: 139b00c6d40895a8299a3caa2d7601599b60e682 (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
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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
//
// C++ Implementation: expander
//
// Description: 
//
//
// Author: Jonas B�r (C) 2004
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <algorithm>

#include "expander.h"

#include "../krusader.h"
#include "../krusaderview.h"
#include "../panelmanager.h"
#include "../Panel/listpanel.h"
#include "../Panel/panelfunc.h"
#include "../Panel/krview.h"
#include "../Synchronizer/synchronizergui.h"
#include "../Search/krsearchdialog.h"
#include "../GUI/profilemanager.h"
#include "../VFS/preservingcopyjob.h"
#include "../KViewer/krviewer.h"
#include "../krservices.h"

#ifdef __KJSEMBED__
#include "../KrJS/krjs.h"
#endif

#include <kdebug.h>
#include <kinputdialog.h>
#include <kstandarddirs.h>
#include <kmessagebox.h>
#include <ktempfile.h>
#include <tqstringlist.h>
#include <tqclipboard.h>

#include <functional>
using namespace std;

#define NEED_PANEL	if (panel==0) { panelMissingError(_expression,exp); return TQString(); }

#include "tstring.h"

TQValueList<const exp_placeholder*>& Expander::_placeholder()
{
	static TQValueList<const exp_placeholder*> ret;
	return ret;
}

void exp_placeholder::panelMissingError(const TQString &s, Expander& exp)
{
	exp.setError( Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Needed panel specification missing in expander %1").tqarg(s)) );
}

TQStringList exp_placeholder::fileList(const ListPanel* const panel,const TQString& type,const TQString& mask,const bool ommitPath,const bool useUrl,Expander& exp,const TQString& error)
{
   TQStringList items;
   if ( type.isEmpty() || type == "all" )
      panel->view->getItemsByMask( mask, &items );
   else if ( type == "files" )
      panel->view->getItemsByMask( mask, &items, false, true );
   else if ( type == "dirs" )
      panel->view->getItemsByMask( mask, &items, true, false );
   else if ( type == "selected" )
      panel->view->getSelectedItems( &items );
   else {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: Bad argument to %1: %2 is not valid item specifier").tqarg(error,type) ) );
      return TQString();
   }
   if ( !ommitPath ) {  // add the current path
      // translate to urls using vfs
      KURL::List* list = panel->func->files()->vfs_getFiles(&items);
      items.clear();
      // parse everything to a single qstring
      for (KURL::List::Iterator it = list->begin(); it != list->end(); ++it) {
         items.push_back(useUrl ? (*it).url() : (*it).path());
      }
      delete list;
   }

   return items;
}

namespace {

class exp_simpleplaceholder : public exp_placeholder
{
public:
	EXP_FUNC;
	virtual TagString expFunc ( const ListPanel*, const TQStringList&, const bool&, Expander& ) const=0;
};

/**
  * expands %_Path% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the path of the specified panel
  */
class exp_Path : public exp_simpleplaceholder {
	static const exp_Path instance;
   exp_Path();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * expands %_Count% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the number of items, which type is specified by the first Parameter
  */
class exp_Count : public exp_simpleplaceholder {
	static const exp_Count instance;
   exp_Count();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * expands %_Filter% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the correspondend filter (ie: "*.cpp")
  */
class exp_Filter : public exp_simpleplaceholder {
	static const exp_Filter instance;
   exp_Filter();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * expands %_Current% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the current item ( != the selected onec)
  */
class exp_Current : public exp_simpleplaceholder {
	static const exp_Current instance;
   exp_Current();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * expands %_List% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with a list of items, which type is specified by the first Parameter
  */
class exp_List : public exp_simpleplaceholder {
	static const exp_List instance;
   exp_List();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * expands %_ListFile% ('_' is replaced by 'a', 'o', 'r' or 'l' to indicate the active, other, right or left panel) with the name of a temporary file, containing a list of items, which type is specified by the first Parameter
  */
class exp_ListFile : public exp_simpleplaceholder {
	static const exp_ListFile instance;
   exp_ListFile();
public:
   SIMPLE_EXP_FUNC;
};
  
/**
  * expands %_Ask% ('_' is nessesary because there is no panel needed) with the return of an input-dialog
  */
class exp_Ask : public exp_simpleplaceholder {
	static const exp_Ask instance;
   exp_Ask();
public:
   SIMPLE_EXP_FUNC;
};
  
/**
  * This copies it's first Parameter to the clipboard
  */
class exp_Clipboard : public exp_placeholder {
	static const exp_Clipboard instance;
   exp_Clipboard();
public:
   EXP_FUNC;
};
  
/**
  * This selects all items by the mask given with the first Parameter
  */
class exp_Select : public exp_simpleplaceholder {
	static const exp_Select instance;
   exp_Select();
public:
   SIMPLE_EXP_FUNC;
};
  
/**
  * This changes the panel'spath to the value given with the first Parameter.
  */
class exp_Goto : public exp_simpleplaceholder {
	static const exp_Goto instance;
   exp_Goto();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * This is equal to 'cp <first Parameter> <second Parameter>'.
  */
class exp_Copy : public exp_placeholder {
	static const exp_Copy instance;
   exp_Copy();
public:
   EXP_FUNC;
};

/**
  * This is equal to 'mv <first Parameter> <second Parameter>'.
  */
class exp_Move : public exp_placeholder {
	static const exp_Move instance;
   exp_Move();
public:
   EXP_FUNC;
};
  
/**
  * This opens the synchronizer with a given profile
  */
class exp_Sync : public exp_simpleplaceholder {
	static const exp_Sync instance;
   exp_Sync();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * This opens the searchmodule with a given profile
  */
class exp_NewSearch : public exp_simpleplaceholder {
	static const exp_NewSearch instance;
   exp_NewSearch();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * This loads the panel-profile with a given name
  */
class exp_Profile : public exp_simpleplaceholder {
	static const exp_Profile instance;
   exp_Profile();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * This is setting marks in the string where he is later splitted up for each {all, selected, files, dirs}
  */
class exp_Each : public exp_simpleplaceholder {
	static const exp_Each instance;
   exp_Each();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * This sets the sorting on a specific colunm
  */
class exp_ColSort : public exp_simpleplaceholder {
	static const exp_ColSort instance;
   exp_ColSort();
public:
   SIMPLE_EXP_FUNC;
};

/**
  * This sets relation between the left and right panel
  */
class exp_PanelSize : public exp_simpleplaceholder {
	static const exp_PanelSize instance;
   exp_PanelSize();
public:
   SIMPLE_EXP_FUNC;
};

#ifdef __KJSEMBED__
/**
  * This sets relation between the left and right panel
  */
class exp_Script : public exp_simpleplaceholder {
	static const exp_Script instance;
   exp_Script();
public:
   SIMPLE_EXP_FUNC;
};

const exp_Script exp_Script::instance;

#endif

/**
  * This loads a file in the internal viewer
  */
class exp_View : public exp_simpleplaceholder {
	static const exp_View instance;
   exp_View();
public:
   SIMPLE_EXP_FUNC;
};

const exp_View exp_View::instance;
const exp_PanelSize exp_PanelSize::instance;
const exp_ColSort exp_ColSort::instance;
const exp_Each exp_Each::instance;
const exp_Profile exp_Profile::instance;
const exp_NewSearch exp_NewSearch::instance;
const exp_Sync exp_Sync::instance;
const exp_Move exp_Move::instance;
const exp_Copy exp_Copy::instance;
const exp_Goto exp_Goto::instance;
const exp_Select exp_Select::instance;
const exp_Clipboard exp_Clipboard::instance;
const exp_Ask exp_Ask::instance;
const exp_ListFile exp_ListFile::instance;
const exp_List exp_List::instance;
const exp_Current exp_Current::instance;
const exp_Filter exp_Filter::instance;
const exp_Count exp_Count::instance;
const exp_Path exp_Path::instance;

////////////////////////////////////////////////////////////
//////////////////////// utils ////////////////////////
////////////////////////////////////////////////////////////

/**
 * escapes everything that confuses bash in filenames
 * @param s String to manipulate
 * @return escaped string
 */
TQString bashquote( TQString s ) {
    /*
    // we _can_not_ use this function because it _encloses_ the sting in single-quots!
    // In this case quotes strings could not be concaternated anymore
    return KrServices::quote(s);
    */
    
    static const TQString evilstuff = "\\\"'`()[]{}!?;$&<>| \t\r\n";		// stuff that should get escaped
     
    for ( unsigned int i = 0; i < evilstuff.length(); ++i )
        s.replace( evilstuff[ i ], (TQString("\\") + evilstuff[ i ]) );

    return s;
}

TQString separateAndQuote(TQStringList list,const TQString& separator,const bool quote)
{
	if(quote)
		transform(list.begin(),list.end(),list.begin(),bashquote);
	return list.join(separator);
}
/////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// expander classes ////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

exp_Path::exp_Path() {
   _expression = "Path";
   _description = i18n("Panel's Path...");
   _needPanel = true;
   
   addParameter( exp_parameter( i18n("Automatically escape spaces"), "__yes", false ) );
}
TagString exp_Path::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool& useUrl, Expander& exp ) const {
   NEED_PANEL
   
   TQString result;
   
   if ( useUrl )
      result = panel->func->files()->vfs_getOrigin().url() + "/";
   else
      result = panel->func->files()->vfs_getOrigin().path() + "/";
         
   if ( parameter[0].lower() == "no" )  // don't escape spaces
      return TagString(result);
   else
      return TagString(bashquote(result));
}

exp_Count::exp_Count() {
   _expression = "Count";
   _description = i18n("Number of...");
   _needPanel = true;
   
   addParameter( exp_parameter( i18n("Count:"), "__choose:All;Files;Dirs;Selected", false ) );
}
TagString exp_Count::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool&, Expander& exp ) const {
   NEED_PANEL
   
   int n = -1;
   if ( parameter[ 0 ].isEmpty() || parameter[ 0 ].lower() == "all" )
      n = panel->view->numDirs() + panel->view->numFiles();
   else if ( parameter[ 0 ].lower() == "files" )
      n = panel->view->numFiles();
   else if ( parameter[ 0 ].lower() == "dirs" )
      n = panel->view->numDirs();
   else if ( parameter[ 0 ].lower() == "selected" )
      n = panel->view->numSelected();
   else {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: Bad argument to Count: %1 is not valid item specifier").tqarg(parameter[0]) ));
      return TQString();
   }

   return TagString(TQString("%1").tqarg( n ));
}

exp_Filter::exp_Filter() {
   _expression = "Filter";
   _description = i18n("Filter Mask (*.h, *.cpp, etc.)");
   _needPanel = true;
}
TagString exp_Filter::expFunc( const ListPanel* panel, const TQStringList&, const bool&, Expander& exp ) const {
   NEED_PANEL

   return panel->view->filterMask().nameFilter();
}

exp_Current::exp_Current() {
   _expression = "Current";
   _description = i18n("Current File (!= Selected File)...");
   _needPanel = true;

   addParameter( exp_parameter( i18n("Omit the current path (optional)"), "__no", false ) );
   addParameter( exp_parameter( i18n("Automatically escape spaces"), "__yes", false ) );
}
TagString exp_Current::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool& useUrl, Expander& exp ) const {
   NEED_PANEL
   
   TQString item = panel->view->getCurrentItem();

   TQString result;
   
   if ( parameter[0].lower() == "yes" )  // ommit the current path
      result = item;
   else {
      KURL url = panel->func->files()->vfs_getFile( item );
      if ( useUrl )
         result = url.url();
      else
         result = url.path();
   }

   if ( parameter[1].lower() == "no" )  // don't escape spaces
      return result;
   else
      return bashquote(result);
}

exp_List::exp_List() {
   _expression = "List";
   _description = i18n("Item List of...");
   _needPanel = true;

   addParameter( exp_parameter( i18n("Which items:"), "__choose:All;Files;Dirs;Selected", false ) );
   addParameter( exp_parameter( i18n("Separator between the items (optional):"), " ", false ) );
   addParameter( exp_parameter( i18n("Omit the current path (optional)"), "__no", false ) );
   addParameter( exp_parameter( i18n("Mask (optional, all but 'Selected'):"), "__select", false ) );
   addParameter( exp_parameter( i18n("Automatically escape spaces"), "__yes", false ) );
}
TagString exp_List::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool& useUrl, Expander& exp ) const {
   NEED_PANEL

   // get selected items from view
   TQStringList items;
   TQString mask;
   
   if ( parameter.count() <= 3 || parameter[3].isEmpty() )
      mask = "*";
   else
      mask = parameter[3];
   
   return separateAndQuote(
   		fileList(panel,
   			parameter.empty() ? TQString() : parameter[0].lower(),
   			mask, parameter.count() > 2 ? parameter[2].lower()=="yes" : false,
   			useUrl, exp, "List"),
   		parameter.count() > 1 ? parameter[1] : " ",
   		parameter.count() > 4 ? parameter[4].lower()=="yes" : true);
}

exp_ListFile::exp_ListFile() {
   _expression = "ListFile";
   _description = i18n("Filename of an Item List...");
   _needPanel = true;

   addParameter( exp_parameter( i18n("Which items:"), "__choose:All;Files;Dirs;Selected", false ) );
   addParameter( exp_parameter( i18n("Separator between the items (optional)"), "\n", false ) );
   addParameter( exp_parameter( i18n("Omit the current path (optional)"), "__no", false ) );
   addParameter( exp_parameter( i18n("Mask (optional, all but 'Selected'):"), "__select", false ) );
   addParameter( exp_parameter( i18n("Automatically escape spaces"), "__no", false ) );
}
TagString exp_ListFile::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool& useUrl, Expander& exp ) const {
   NEED_PANEL

   // get selected items from view
   TQStringList items;
   TQString mask;
   
   if ( parameter.count() <= 3 || parameter[3].isEmpty() )
      mask = "*";
   else
      mask = parameter[3];
   KTempFile tmpFile( locateLocal("tmp", "krusader"), ".itemlist" );
   
    if ( tmpFile.status() != 0 ) {
      setError(exp, Error(Error::S_FATAL,Error::C_WORLD, i18n("Expander: tempfile couldn't be opened (%1)" ).tqarg(strerror( tmpFile.status() )) ));
      return TQString();
    }
    
    TQTextStream stream( tmpFile.file() );
    stream << separateAndQuote(
    		fileList(panel,
    			parameter.empty() ? TQString() : parameter[0].lower(),
    			mask, parameter.count()>2 ? parameter[2].lower()=="yes" : false,
    			useUrl, exp, "ListFile"),
    		parameter.count() > 1 ? parameter[1] : "\n",
    		parameter.count() > 4 ? parameter[4].lower()=="yes" : true)
    	 << "\n";
    tmpFile.close();

   return tmpFile.name();
}

exp_Select::exp_Select() {
   _expression = "Select";
   _description = i18n("Manipulate the Selection...");
   _needPanel = true;

   addParameter( exp_parameter( i18n("Selection mask:"), "__select", true ) );
   addParameter( exp_parameter( i18n("Manipulate in which way:"), "__choose:Set;Add;Remove", false ) );
}
TagString exp_Select::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool& , Expander& exp) const {
   NEED_PANEL
   
   KRQuery mask;
    if ( parameter.count() <= 0 || parameter[0].isEmpty() )
       mask = KRQuery( "*" );
    else
       mask = KRQuery( parameter[0] );

    if ( parameter[1].lower() == "add")
       panel->view->select( mask );
    else if ( parameter[1].lower() == "remove")
       panel->view->unselect( mask );
    else { // parameter[1].lower() == "set" or isEmpty() or whatever
       panel->view->unselect( KRQuery( "*" ) );
       panel->view->select( mask );
    }

   return TQString();  // this doesn't return anything, that's normal!
}

exp_Goto::exp_Goto() {
   _expression = "Goto";
   _description = i18n("Jump to a Location...");
   _needPanel = true;
   
   addParameter( exp_parameter( i18n("Choose a path:"), "__goto", true ) );
   addParameter( exp_parameter( i18n("Open location in a new tab"), "__no", false ) );
}
TagString exp_Goto::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool&, Expander& exp ) const {
   NEED_PANEL
   
   bool newTab = false;
   if ( parameter[1].lower() == "yes" )
      newTab = true;
   
   if ( newTab ) {
      if ( panel == LEFT_PANEL)
         krApp->mainView->leftMng->slotNewTab( parameter[0] );
      else
         krApp->mainView->rightMng->slotNewTab( parameter[0] );
   }
   else {
      panel->func->openUrl( parameter[0], "" );
      const_cast<ListPanel*>(panel)->slotFocusOnMe();
   }
   
   return TQString();  // this doesn't return anything, that's normal!
}

/*
exp_Search::exp_Search() {
   _expression = "Search";
   _description = i18n("Search for files");
   _needPanel = true;

   addParameter( new exp_parameter( i18n("please choose the setting"), "__searchprofile", true ) );
   addParameter( new exp_parameter( i18n("open the search in a new tab"), "__yes", false ) );  //TODO: add this also to panel-dependent as soon as vfs support the display of search-results
}
*/

exp_Ask::exp_Ask() {
   _expression = "Ask";
   _description = i18n("Ask Parameter from User...");
   _needPanel = false;

   addParameter( exp_parameter( i18n("Question:"), "Where do you want do go today?", true ) );
   addParameter( exp_parameter( i18n("Preset (optional):"), "", false ) );
   addParameter( exp_parameter( i18n("Caption (optional):"), "", false ) );
}
TagString exp_Ask::expFunc( const ListPanel*, const TQStringList& parameter, const bool&, Expander& exp ) const {
   TQString caption, preset, result;
   
   if ( parameter.count() <= 2 || parameter[2].isEmpty() )
      caption = i18n("User Action");
   else
      caption = parameter[2];
   if ( parameter.count() <= 1 || parameter[1].isEmpty() )
      preset = TQString();
   else
      preset = parameter[1];
   
   bool ok;
   result = KInputDialog::getText(
		caption,
		parameter[0],
		preset,
		&ok );
   
   if (ok)
      return result;
   else {
		 setError(exp, Error(Error::S_ERROR,Error::C_USER,"User cancelled") );
      return TQString();
	 }
}

exp_Clipboard::exp_Clipboard() {
   _expression = "Clipboard";
   _description = i18n("Copy to Clipboard...");
   _needPanel = false;

   addParameter( exp_parameter( i18n("What to copy:"), "__placeholder", true ) );
   addParameter( exp_parameter( i18n("Append to current clipboard content with this separator (optional):"), "", false ) );
}
TagString exp_Clipboard::expFunc( const ListPanel*, const TagStringList& parameter, const bool&, Expander& exp ) const {
//    kdDebug() << "Expander::exp_Clipboard, parameter[0]: '" << parameter[0] << "', Clipboard: " << KApplication::tqclipboard()->text() << endl;
	TQStringList lst=splitEach(parameter[0]);
	if(!parameter[1].isSimple()) {
		setError(exp,Error(Error::S_FATAL,Error::C_SYNTAX,i18n("Expander: %Each% may not be in the second argument of %Clipboard%")));
		return TQString();
	}
    if ( parameter.count() <= 1 || parameter[1].string().isEmpty() || KApplication::tqclipboard()->text().isEmpty() )
       KApplication::tqclipboard()->setText( lst.join("\n") );
    else
       KApplication::tqclipboard()->setText( KApplication::tqclipboard()->text() + parameter[1].string() + lst.join("\n") );

   return TQString();  // this doesn't return anything, that's normal!
}

exp_Copy::exp_Copy() {
   _expression = "Copy";
   _description = i18n("Copy a File/Folder...");
   _needPanel = false;

   addParameter( exp_parameter( i18n("What to copy:"), "__placeholder", true ) );
   addParameter( exp_parameter( i18n("Where to copy:"), "__placeholder", true ) );
}
TagString exp_Copy::expFunc( const ListPanel*, const TagStringList& parameter, const bool&, Expander& exp ) const {

   // basically the parameter can already be used as URL, but since KURL has problems with ftp-proxy-urls (like ftp://username@proxyusername@url...) this is neccesary:
   TQStringList lst=splitEach( parameter[0] );
   if(!parameter[1].isSimple()) {
      setError(exp,Error(Error::S_FATAL,Error::C_SYNTAX,i18n("Expander: %Each% may not be in the second argument of %Copy%")));
      return TQString();
   }
   KURL::List src;
   for(TQStringList::const_iterator it=lst.begin(),end=lst.end();it!=end;++it)
      src.push_back(vfs::fromPathOrURL( *it ));
   // or transform(...) ?
   KURL dest = vfs::fromPathOrURL( parameter[1].string() );

   if ( !dest.isValid() || find_if(src.constBegin(),src.constEnd(),not1(mem_fun_ref(&KURL::isValid) ))!=src.end()) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: invalid URL's in %_Copy(\"src\", \"dest\")%") ));
      return TQString();
   }

   PreservingCopyJob::createCopyJob( PM_DEFAULT, src, dest, KIO::CopyJob::Copy, false, true );

   return TQString();  // this doesn't return everything, that's normal!
}

exp_Move::exp_Move() {
   _expression = "Move";
   _description = i18n("Move/Rename a File/Folder...");
   _needPanel = false;

   addParameter( exp_parameter( i18n("What to move/rename:"), "__placeholder", true ) );
   addParameter( exp_parameter( i18n("New target/name:"), "__placeholder", true ) );
}                    
TagString exp_Move::expFunc( const ListPanel*, const TagStringList& parameter, const bool& , Expander& exp ) const {
   // basically the parameter can already be used as URL, but since KURL has problems with ftp-proxy-urls (like ftp://username@proxyusername@url...) this is neccesary:
   TQStringList lst=splitEach( parameter[0] );
   if(!parameter[1].isSimple()) {
      setError(exp,Error(Error::S_FATAL,Error::C_SYNTAX,i18n("%Each% may not be in the second argument of %Move%")));
      return TQString();
   }
   KURL::List src;
   for(TQStringList::const_iterator it=lst.begin(),end=lst.end();it!=end;++it)
   src.push_back(vfs::fromPathOrURL( *it ));
   // or transform(...) ?
   KURL dest = vfs::fromPathOrURL( parameter[1].string() );

   if ( !dest.isValid() || find_if(src.constBegin(),src.constEnd(),not1(mem_fun_ref(&KURL::isValid) ))!=src.end()) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: invalid URL's in %_Move(\"src\", \"dest\")%") ));
      return TQString();
   }

   PreservingCopyJob::createCopyJob( PM_DEFAULT, src, dest, KIO::CopyJob::Move, false, true );

   return TQString();  // this doesn't return anything, that's normal!
}

exp_Sync::exp_Sync() {
   _expression = "Sync";
   _description = i18n("Load a Synchronizer Profile...");
   _needPanel = false;
   
   addParameter( exp_parameter( i18n("Choose a profile:"), "__syncprofile", true ) );
}
TagString exp_Sync::expFunc( const ListPanel*, const TQStringList& parameter, const bool&, Expander& exp ) const {
   if ( parameter[0].isEmpty() ) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: no profile specified for %_Sync(profile)%") ));
      return TQString();
   }

   new SynchronizerGUI( 0, parameter[0] );

   return TQString();  // this doesn't return everything, that's normal!
}

exp_NewSearch::exp_NewSearch() {
   _expression = "NewSearch";
   _description = i18n("Load a Searchmodule Profile...");
   _needPanel = false;
   
   addParameter( exp_parameter( i18n("Choose a profile:"), "__searchprofile", true ) );
}
TagString exp_NewSearch::expFunc( const ListPanel*, const TQStringList& parameter, const bool&, Expander& exp ) const {
   if ( parameter[0].isEmpty() ) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: no profile specified for %_NewSearch(profile)%") ));
      return TQString();
   }

   new KrSearchDialog( parameter[0], krApp );

   return TQString();  // this doesn't return everything, that's normal!
}

exp_Profile::exp_Profile() {
   _expression = "Profile";
   _description = i18n("Load a Panel Profile...");
   _needPanel = false;
   
   addParameter( exp_parameter( i18n("Choose a profile:"), "__panelprofile", true ) );
}
TagString exp_Profile::expFunc( const ListPanel*, const TQStringList& parameter, const bool&, Expander& exp ) const {
   if ( parameter[0].isEmpty() ) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: no profile specified for %_Profile(profile)%; abort...") ));
      return TQString();
   }
   
   MAIN_VIEW->profiles( parameter[0] );

   return TQString();  // this doesn't return everything, that's normal!
}

exp_Each::exp_Each() {
   _expression = "Each";
   _description = i18n("Separate Program Call for Each...");
   _needPanel = true;

   addParameter( exp_parameter( i18n("Which items:"), "__choose:All;Files;Dirs;Selected", false ) );
   addParameter( exp_parameter( i18n("Omit the current path (optional)"), "__no", false ) );
   addParameter( exp_parameter( i18n("Mask (optional, all but 'Selected'):"), "__select", false ) );
   addParameter( exp_parameter( i18n("Automatically escape spaces"), "__yes", false ) );
}
TagString exp_Each::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool& useUrl, Expander& exp ) const {
   NEED_PANEL
   
   TQString mask;
   if ( parameter.count() <= 2 || parameter[2].isEmpty() )
      mask = "*";
   else
      mask = parameter[2];

   TagString ret;
   TQStringList l = fileList(panel,
   		parameter.empty() ? TQString() : parameter[0].lower(),
   		mask, parameter.count() > 1 && parameter[1].lower()=="yes",
   		useUrl, exp, "Each");

   if(!(parameter.count()<=3 || parameter[3].lower()!="yes"))
      transform(l.begin(),l.end(),l.begin(),bashquote);

   ret.insertTag(0,l);
   return ret;
}

exp_ColSort::exp_ColSort() {
   _expression = "ColSort";
   _description = i18n("Set Sorting for This Panel...");
   _needPanel = true;
   
   addParameter( exp_parameter( i18n("Choose a column:"), "__choose:Name;Ext;Type;Size;Modified;Perms;rwx;Owner;Group", true ) );
   addParameter( exp_parameter( i18n("Choose a sort sequence:"), "__choose:Toggle;Asc;Desc", false ) );
}
TagString exp_ColSort::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool&, Expander& exp ) const {
   NEED_PANEL

   if ( parameter[0].isEmpty() ) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: no column specified for %_ColSort(column)%") ));
      return TQString();
   }
   
   int mode = (int) panel->view->sortMode();
   
   /* from Panel/krview.h:
      enum SortSpec { Name=0x1, 
                              Ext=0x2,
                              Size=0x4,
                              Type=0x8,
                              Modified=0x10,
                              Permissions=0x20,
                              KrPermissions=0x40,
                              Owner=0x80,
                              Group=0x100,
                              Descending=0x200,
                              DirsFirst=0x400,
                              IgnoreCase=0x800 };
   */
   
//   krOut << "start: exp_ColSort::expFunc" << endl;
   #define MODE_OUT krOut << TQString( "mode: %1" ).tqarg( mode, 0, 2 ) << endl; // displays mode in base-2
   //MODE_OUT
      
   if ( parameter.count() <= 1 || ( parameter[1].lower() != "asc" && parameter[1].lower() != "desc" ) ) {  //default == toggle
      if ( mode & KrViewProperties::Descending )
         mode &= ~KrViewProperties::Descending; // == asc
      else
         mode |= KrViewProperties::Descending; // == desc
   } else
   if ( parameter[1].lower() == "asc" ) {
      mode &= ~KrViewProperties::Descending;
   }
   else { // == desc
      mode |= KrViewProperties::Descending;
   }
   
   //MODE_OUT
   
   // clear all column-infromation:
   mode &= ~( KrViewProperties::Name | KrViewProperties::Ext | KrViewProperties::Size | KrViewProperties::Type | KrViewProperties::Modified | KrViewProperties::Permissions | KrViewProperties::KrPermissions | KrViewProperties::Owner | KrViewProperties::Group );
   
   MODE_OUT
   
   if ( parameter[0].lower() == "name" ) {
      mode |= KrViewProperties::Name;
   } else
   if ( parameter[0].lower() == "ext" ) {
      mode |= KrViewProperties::Ext;
   } else
   if ( parameter[0].lower() == "type" ) {
      mode |= KrViewProperties::Type;
   } else
   if ( parameter[0].lower() == "size" ) {
      mode |= KrViewProperties::Size;
   } else
   if ( parameter[0].lower() == "modified" ) {
      mode |= KrViewProperties::Modified;
   } else
   if ( parameter[0].lower() == "perms" ) {
      mode |= KrViewProperties::Permissions;
   } else
   if ( parameter[0].lower() == "rwx" ) {
      mode |= KrViewProperties::KrPermissions;
   } else
   if ( parameter[0].lower() == "owner" ) {
      mode |= KrViewProperties::Owner;
   } else
   if ( parameter[0].lower() == "group" ) {
      mode |= KrViewProperties::Group;
   } else {
      setError(exp, Error(Error::S_WARNING,Error::C_ARGUMENT,i18n("Expander: unknown column specified for %_ColSort(%1)%").tqarg(parameter[0]) ));
      return TQString();
   }
   
   //MODE_OUT
   panel->view->setSortMode( (KrViewProperties::SortSpec)mode );
//   krOut << "end: exp_ColSort::expFunc" << endl;
   return TQString();  // this doesn't return anything, that's normal!
}

exp_PanelSize::exp_PanelSize() {
   _expression = "PanelSize";
   _description = i18n("Set Relation Between the Panels...");
   _needPanel = true;
   
   addParameter( exp_parameter( i18n("Set the new size in percent:"), "__int:0;100;5;50", true ) );
}
TagString exp_PanelSize::expFunc( const ListPanel* panel, const TQStringList& parameter, const bool&, Expander& exp ) const {
   NEED_PANEL
   int newSize;
   
   if ( parameter[0].isEmpty() )
      newSize = 50;	//default is 50%
   else
      newSize = parameter[0].toInt();
   
   if ( newSize < 0 || newSize > 100 ) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: Value %1 out of range for %_PanelSize(percent)%. The first parameter has to be >0 and <100").tqarg(newSize)) );
      return TQString();
   }

    TQValueList<int> panelSizes = MAIN_VIEW->horiz_splitter->sizes();
    int totalSize = panelSizes[0] + panelSizes[1];
    
    if ( panel == LEFT_PANEL ) {
       panelSizes[0] = totalSize * newSize / 100;
       panelSizes[1] = totalSize * (100 - newSize) / 100;
    }
    else { // == RIGHT_PANEL
       panelSizes[0] = totalSize * (100 - newSize) / 100;
       panelSizes[1] = totalSize * newSize / 100;
    }
    
    MAIN_VIEW->horiz_splitter->setSizes( panelSizes );

   return TQString();  // this doesn't return everything, that's normal!
}

#ifdef __KJSEMBED__
exp_Script::exp_Script() {
   _expression = "Script";
   _description = i18n("Execute a JavaScript Extension...");
   _needPanel = false;

   addParameter( exp_parameter( i18n("Location of the script"), "", true ) );
   addParameter( exp_parameter( i18n("Set some variables for the execution (optional).\ni.e. \"return=return_var;foo=bar\", consult the handbook for more information"), "", false ) );
}
TagString exp_Script::expFunc( const ListPanel*, const TQStringList& parameter, const bool&, Expander& exp ) const {
   if ( parameter[0].isEmpty() ) {
      setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: no script specified for %_Script(script)%")) );
      return TQString();
   }

   TQString filename = parameter[0];
   if ( filename.find('/') && KURL::isRelativeURL(filename) ) {
      // this return the local version of the file if this exists. else the global one is returnd
      filename = locate( "data", "krusader/js/"+filename );
   }

  if ( ! krJS )
      krJS = new KrJS();

   KJS::ExecState *exec = krJS->globalExec();

   TQString jsReturn = TQString();
   if ( parameter[1].lower() == "yes" )	// to stay compatible with the old-style parameter
      jsReturn = "cmd";
   else {
      TQStringList jsVariables = TQStringList::split( ';', parameter[1] );
      TQString jsVariable, jsValue;
      for ( TQStringList::Iterator it = jsVariables.begin(); it != jsVariables.end(); ++it ) {
         jsVariable = (*it).section('=', 0, 0).stripWhiteSpace();
         jsValue = (*it).section('=', 1);
         if ( jsVariable == "return" )
            jsReturn = jsValue.stripWhiteSpace();
         else
            krJS->putValue( jsVariable, KJSEmbed::convertToValue(exec, jsValue ) );
      }
   }

   krJS->runFile( filename );

   if ( ! jsReturn.isEmpty() )
      return krJS->getValue( jsReturn ).toString( krJS->globalExec() ).qstring();
   else
      return TQString();
}
#endif

exp_View::exp_View() {
   _expression = "View";
   _description = i18n("View File with Krusader's Internal Viewer...");
   _needPanel = false;

   addParameter( exp_parameter( i18n("Which file to view (normally '%aCurrent%'):"), "__placeholder", true ) );
   addParameter( exp_parameter( i18n("Choose a view mode:"), "__choose:generic;text;hex", false ) );
   //addParameter( exp_parameter( i18n("Choose a window-mode"), "__choose:tab;window;panel", false ) );
   //TODO: window-mode 'panel' should open the file in the third-hand viewer
   addParameter( exp_parameter( i18n("Choose a window mode:"), "__choose:tab;window", false ) );
}
TagString exp_View::expFunc( const ListPanel*, const TQStringList& parameter, const bool&, Expander& exp ) const {
   if ( parameter[0].isEmpty() ) {
			setError(exp, Error(Error::S_FATAL,Error::C_ARGUMENT,i18n("Expander: no file to view in %_View(filename)%")) );
      return TQString();
   }

   TQString viewMode, windowMode;
   if ( parameter.count() <= 1 || parameter[1].isEmpty() )
      viewMode = "generic";
   else
      viewMode = parameter[1];

   if ( parameter.count() <= 2 || parameter[2].isEmpty() )
      windowMode = "tab";
   else
      windowMode = parameter[2];

   KrViewer::Mode mode = KrViewer::Generic;
   if( viewMode == "text" ) mode = KrViewer::Text;
   else if( viewMode == "hex" ) mode = KrViewer::Hex;

	KrViewer::view(parameter[0],mode,(windowMode == "window"));
   //TODO: Call the viewer with viewMode and windowMode. Filename is in parameter[0].
   // It would be nice if parameter[0] could also be a space-separated filename-list (provided if the first parameter is %aList(selected)%)

   return TQString();  // this doesn't return everything, that's normal!
}

/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// end of expander classes ////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////

TagString exp_simpleplaceholder::expFunc( const ListPanel* p, const TagStringList& parameter, const bool& useUrl, Expander& exp) const 
{
	TQStringList lst;
	for(TagStringList::const_iterator it=parameter.begin(),end=parameter.end();it!=end;++it)
		if((*it).isSimple())
			lst.push_back((*it).string());
		else {
			setError(exp,Error(Error::S_FATAL,Error::C_SYNTAX,i18n("%Each% is not allowed in parameter to %1").tqarg(description())));
			return TQString();
		}
	return expFunc(p,lst,useUrl,exp);
}

}

ListPanel* Expander::getPanel( const char panelIndicator, const exp_placeholder* pl, Expander& exp ) {
   switch ( panelIndicator ) {
   case 'a':
      return ACTIVE_PANEL;
   case 'o':
      return OTHER_PANEL;
   case 'l':
      return LEFT_PANEL;
   case 'r':
      return RIGHT_PANEL;
   case '_':
      return 0;
   default:
		 exp.setError(Error(Error::S_FATAL,Error::C_SYNTAX,i18n("Expander: Bad panel specifier %1 in placeholder %2").tqarg(panelIndicator).tqarg(pl->description())));
      return 0;
   }
}

void Expander::expand( const TQString& stringToExpand, bool useUrl ) {
   TagString result = expandCurrent( stringToExpand, useUrl );
   if ( error() )
      return;
   
   if ( !result.isSimple() )
      resultList = splitEach( result );
   else
      resultList.append( result.string() );

//    krOut << resultList[0] << endl;
}

TagString Expander::expandCurrent( const TQString& stringToExpand, bool useUrl ) {
   TagString result;
	 TQString exp = TQString();
   TagString tmpResult;
   int begin, end, i;
//    int brackets = 0;
//    bool inQuotes = false;
   unsigned int idx = 0;
   while ( idx < stringToExpand.length() ) {
      if ( ( begin = stringToExpand.find( '%', idx ) ) == -1 ) break;
      if ( ( end = findEnd( stringToExpand, begin ) ) == -1 ) {
         setError(Error(Error::S_FATAL,Error::C_SYNTAX,i18n("Error: unterminated % in Expander::expandCurrent")) );
         return TQString();
      }

      result += stringToExpand.mid( idx, begin - idx ); // copy until the start of %exp%

      // get the expression, and expand it using the correct expander function
      exp = stringToExpand.mid( begin + 1, end - begin - 1 );
//       kdDebug() << "------------- exp: '" << exp << "'" << endl;
      if ( exp == "" )
        result += TQString(TQChar('%'));
      else {
        TagStringList parameter = separateParameter( &exp, useUrl );
        if ( error() )
           return TQString();
        char panelIndicator = exp.lower()[0].latin1();
        exp.replace( 0, 1, "" );
        for ( i = 0; i < placeholderCount(); ++i )
           if ( exp == placeholder( i )->expression() ) {
//               kdDebug() << "---------------------------------------" << endl;
              tmpResult = placeholder( i )->expFunc( getPanel( panelIndicator,placeholder(i),*this ), parameter, useUrl, *this );
              if ( error() ) {
                 return TQString();
              }
              else
                 result += tmpResult;
//               kdDebug() << "---------------------------------------" << endl;
              break;
           }
         if ( i == placeholderCount() ) { // didn't find an expander
            setError(Error(Error::S_FATAL,Error::C_SYNTAX,i18n("Error: unrecognized %%%1%2%% in Expander::expand").tqarg(panelIndicator).tqarg(exp)) );
           return TQString();
         }
      } //else
      idx = end + 1;
   }
   // copy the rest of the string
   result += stringToExpand.mid( idx );
//    kdDebug() << "============== result '" << result << "'" << endl;
   return result;
}

TQStringList Expander::splitEach( TagString stringToSplit ) {
	if(stringToSplit.isSimple()) {
// 		krOut << stringToSplit.string() << endl;
		return stringToSplit.string();
	}
	pair<uint,TQStringList> pl=*stringToSplit.tagsBegin();
	stringToSplit.eraseTag(stringToSplit.tagsBegin());
	TQStringList ret;
	for(TQStringList::const_iterator it=pl.second.begin(),end=pl.second.end();it!=end;++it) {
		TagString s=stringToSplit;
		s.insert(pl.first,*it);
		ret+=splitEach(s);
         }
	return ret;
//    kdDebug() << "stringToSplit: " << stringToSplit << endl;
}

TagStringList Expander::separateParameter( TQString* const exp, bool useUrl ) {
   TagStringList parameter;
   TQStringList parameter1;
   TQString result;
   int begin, end;
   if ( ( begin = exp->find( '(' ) ) != -1 ) {
      if ( ( end = exp->findRev( ')' ) ) == -1 ) {
         setError(Error(Error::S_FATAL,Error::C_SYNTAX,i18n("Error: missing ')' in Expander::separateParameter") ));
         return TagStringList();
      }
      result = exp->mid( begin + 1, end - begin - 1 );
      *exp = exp->left( begin );
   
      bool inQuotes = false;
      unsigned int idx = 0;
      begin = 0;
      while ( idx < result.length() ) {
         if ( result[ idx ].latin1() == '\\' ) {
            if ( result[ idx+1 ].latin1() == '"')
               result.replace( idx, 1, "" );
         }
         if ( result[ idx ].latin1() == '"' )
            inQuotes = !inQuotes;
         if ( result[ idx ].latin1() == ',' && !inQuotes ) {
            parameter1.append( result.mid( begin, idx - begin) );
            begin = idx + 1;
//             krOut << " ---- parameter: " << parameter.join(";") << endl;
         }
         idx++;
      }
      parameter1.append( result.mid( begin, idx - begin) );  //don't forget the last one
      
      for (TQStringList::Iterator it = parameter1.begin(); it != parameter1.end(); ++it) {
         *it = (*it).stripWhiteSpace();
         if ( (*it).left(1) == "\"" )
            *it = (*it).mid(1, (*it).length() - 2 );
         parameter.push_back(expandCurrent( *it, useUrl ));
         if ( error() )
            return TagStringList();
      }
   }
      
//    krOut << "------- exp: " << *exp << " ---- parameter: " << parameter.join(";") << endl;
   return parameter;
}

int Expander::findEnd( const TQString& str, int start ) {
   int end = str.find( '%', start + 1 );
   if ( end == -1 )
      return end;
   int bracket = str.find( '(', start + 1 );
   if ( end < bracket || bracket == -1 )
      return end;
      
   unsigned int idx = bracket+1;
   bool inQuotes = false;
   int depth=1;
   while ( idx < str.length() ) {
      switch (str[ idx ].latin1()) {
      case '\\':
         idx ++;
         break;
      case '"':
         inQuotes = !inQuotes;
         break;
      case '(':
         if(!inQuotes)
            depth++;
         break;
      case ')':
         if(!inQuotes)
            --depth;
         break;
      case '%':
         if(depth==0)
            return idx;
      } //switch
      idx++;
   } //while
   // failsafe
   return -1;
}