utf: console 2
[scilab.git] / scilab / modules / console / src / c / windows / TermCompletion.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2010 - DIGITEO - Allan CORNET
4 *
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution.  The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 *
11 */
12
13 /*--------------------------------------------------------------------------*/
14 #include <string.h>
15 #include <stdlib.h>
16 #include "TermCompletion.h"
17 #include "sci_malloc.h"
18 #include "freeArrayOfString.h"
19 #include "localization.h"
20 #include "TermLine.h"
21 #include "TermConsole.h"
22 #include "getPartLine.h"
23 #include "getCommonPart.h"
24 #include "completion.h"
25 #include "scilines.h"
26 #include "os_string.h"
27 #include "completeLine.h"
28 #include "configvariable_interface.h"
29 /*--------------------------------------------------------------------------*/
30 static void displayCompletionDictionary(char **dictionary, int sizedictionary, char *namedictionary);
31 static char **concatenateStrings(int *sizearrayofstring, char *string1,
32                                  char *string2, char *string3,
33                                  char *string4, char *string5);
34 static void TermCompletionOnFiles(char **dictionaryFiles, int sizedictionaryFiles,
35                                   char *lineBeforeCaret, char *lineAfterCaret, char *filePattern, char *defaultPattern);
36 static void TermCompletionOnAll(char *lineBeforeCaret, char *lineAfterCaret, char *defaultPattern);
37 /*--------------------------------------------------------------------------*/
38 static void TermCompletionOnFiles(char **dictionaryFiles, int sizedictionaryFiles,
39                                   char *lineBeforeCaret, char *lineAfterCaret, char *filePattern, char *defaultPattern)
40 {
41     if (dictionaryFiles)
42     {
43         if (sizedictionaryFiles == 1)
44         {
45             char *newline = completeLine(lineBeforeCaret, dictionaryFiles[0], filePattern, defaultPattern, TRUE, lineAfterCaret);
46             if (newline)
47             {
48                 clearCurrentLine();
49                 copyLine(newline);
50                 FREE(newline);
51                 return;
52             }
53         }
54         else
55         {
56             char *common = getCommonPart(dictionaryFiles, sizedictionaryFiles);
57
58             displayCompletionDictionary(dictionaryFiles,
59                                         sizedictionaryFiles, gettext("File or Directory"));
60
61             displayPrompt();
62             newLine();
63
64             if (defaultPattern[0] == 0)
65             {
66                 int lennewline = (int)strlen(lineBeforeCaret) + (int)strlen(lineAfterCaret);
67                 char *newline = (char*)MALLOC(sizeof(char) * (lennewline + 1));
68
69                 clearCurrentLine();
70                 if (newline)
71                 {
72                     strcpy(newline, lineBeforeCaret);
73                     strcat(newline, lineAfterCaret);
74                     copyLine(newline);
75                     FREE(newline);
76                     newline = NULL;
77                 }
78             }
79             else if (common)
80             {
81                 char *newline = completeLine(lineBeforeCaret, common, filePattern, defaultPattern, TRUE, lineAfterCaret);
82                 if (newline)
83                 {
84                     clearCurrentLine();
85                     copyLine(newline);
86                     FREE(common);
87                     FREE(newline);
88                     return;
89                 }
90                 else
91                 {
92                     int lennewline = (int)strlen(lineBeforeCaret) + (int)strlen(lineAfterCaret);
93                     newline = (char*)MALLOC(sizeof(char) * (lennewline + 1));
94
95                     clearCurrentLine();
96                     if (newline)
97                     {
98                         strcpy(newline, lineBeforeCaret);
99                         strcat(newline, lineAfterCaret);
100                         copyLine(newline);
101                         FREE(newline);
102                         newline = NULL;
103                     }
104                 }
105                 FREE(common);
106                 common = NULL;
107             }
108         }
109     }
110 }
111 /*--------------------------------------------------------------------------*/
112 static void TermCompletionOnAll(char *lineBeforeCaret, char *lineAfterCaret, char *defaultPattern)
113 {
114     if (defaultPattern)
115     {
116         int numberWordFound = 0;
117         char **completionDictionaryFunctions = NULL;
118         int sizecompletionDictionaryFunctions = 0;
119
120         char **completionDictionaryCommandWords = NULL;
121         int sizecompletionDictionaryCommandWords = 0;
122
123         char **completionDictionaryMacros = NULL;
124         int sizecompletionDictionaryMacros = 0;
125
126         char **completionDictionaryVariables = NULL;
127         int sizecompletionDictionaryVariables = 0;
128
129         char **completionDictionaryHandleGraphicsProperties = NULL;
130         int sizecompletionDictionaryHandleGraphicsProperties = 0;
131
132         char **completionDictionaryFields = NULL;
133         int sizecompletionDictionaryFields = 0;
134
135         completionDictionaryFields = completionOnFields(lineBeforeCaret, defaultPattern, &sizecompletionDictionaryFields);
136
137         if ((completionDictionaryFields == NULL) && strcmp(defaultPattern, ""))
138         {
139             completionDictionaryFunctions = completionOnFunctions(defaultPattern, &sizecompletionDictionaryFunctions);
140             completionDictionaryCommandWords = completionOnCommandWords(defaultPattern, &sizecompletionDictionaryCommandWords);
141             completionDictionaryMacros = completionOnMacros(defaultPattern, &sizecompletionDictionaryMacros);
142             completionDictionaryVariables = completionOnVariablesWithoutMacros(defaultPattern, &sizecompletionDictionaryVariables);
143             completionDictionaryHandleGraphicsProperties = completionOnHandleGraphicsProperties(defaultPattern, &sizecompletionDictionaryHandleGraphicsProperties);
144         }
145         numberWordFound = sizecompletionDictionaryFunctions + sizecompletionDictionaryCommandWords +
146                           sizecompletionDictionaryMacros + sizecompletionDictionaryVariables +
147                           sizecompletionDictionaryHandleGraphicsProperties + sizecompletionDictionaryFields;
148
149         if (numberWordFound > 0)
150         {
151             if (numberWordFound == 1)
152             {
153                 char **completionDictionary = NULL;
154                 char *new_line = NULL;
155
156                 if (completionDictionaryFields)
157                 {
158                     completionDictionary = completionDictionaryFields;
159                 }
160                 if (completionDictionaryFunctions)
161                 {
162                     completionDictionary = completionDictionaryFunctions;
163                 }
164                 if (completionDictionaryCommandWords)
165                 {
166                     completionDictionary = completionDictionaryCommandWords;
167                 }
168                 if (completionDictionaryMacros)
169                 {
170                     completionDictionary = completionDictionaryMacros;
171                 }
172                 if (completionDictionaryVariables)
173                 {
174                     completionDictionary = completionDictionaryVariables;
175                 }
176                 if (completionDictionaryHandleGraphicsProperties)
177                 {
178                     completionDictionary = completionDictionaryHandleGraphicsProperties;
179                 }
180
181                 new_line = completeLine(lineBeforeCaret, completionDictionary[0], NULL, defaultPattern, FALSE, lineAfterCaret);
182                 if (new_line)
183                 {
184                     clearCurrentLine();
185                     copyLine(new_line);
186                     FREE(new_line);
187                 }
188             }
189             else
190             {
191                 char *commonAll = NULL;
192                 if (completionDictionaryFields)
193                 {
194                     commonAll = getCommonPart(completionDictionaryFields, sizecompletionDictionaryFields);
195                     displayCompletionDictionary(completionDictionaryFields, sizecompletionDictionaryFields, (char *)_("Scilab Fields"));
196                     freeArrayOfString(completionDictionaryFields, sizecompletionDictionaryFields);
197                 }
198                 else
199                 {
200                     char *commonFunctions = getCommonPart(completionDictionaryFunctions, sizecompletionDictionaryFunctions);
201                     char *commonCommandWords = getCommonPart(completionDictionaryCommandWords, sizecompletionDictionaryCommandWords);
202                     char *commonMacros = getCommonPart(completionDictionaryMacros, sizecompletionDictionaryMacros);
203                     char *commonVariables = getCommonPart(completionDictionaryVariables, sizecompletionDictionaryVariables);
204                     char *commonHandleGraphicsProperties = getCommonPart(completionDictionaryHandleGraphicsProperties, sizecompletionDictionaryHandleGraphicsProperties);
205
206                     int sizecommonsDictionary = 0;
207                     char **commonsDictionary = concatenateStrings(&sizecommonsDictionary, commonFunctions,
208                                                commonMacros, commonCommandWords, commonVariables, commonHandleGraphicsProperties);
209
210                     if (sizecommonsDictionary > 0)
211                     {
212                         if (sizecommonsDictionary == 1)
213                         {
214                             commonAll = os_strdup(commonsDictionary[0]);
215                         }
216                         else
217                         {
218                             commonAll = getCommonPart(commonsDictionary, sizecommonsDictionary);
219                         }
220                         freeArrayOfString(commonsDictionary, sizecommonsDictionary);
221                     }
222
223                     displayCompletionDictionary(completionDictionaryFunctions, sizecompletionDictionaryFunctions, (char *)_("Scilab Function"));
224                     displayCompletionDictionary(completionDictionaryCommandWords, sizecompletionDictionaryCommandWords, (char *)_("Scilab Command"));
225                     displayCompletionDictionary(completionDictionaryMacros, sizecompletionDictionaryMacros, (char *)_("Scilab Macro"));
226                     displayCompletionDictionary(completionDictionaryVariables, sizecompletionDictionaryVariables, (char *)_("Scilab Variable"));
227                     displayCompletionDictionary(completionDictionaryHandleGraphicsProperties, sizecompletionDictionaryHandleGraphicsProperties, (char *)_("Graphics handle field"));
228                     freeArrayOfString(completionDictionaryFunctions, sizecompletionDictionaryFunctions);
229                     freeArrayOfString(completionDictionaryCommandWords, sizecompletionDictionaryCommandWords);
230                     freeArrayOfString(completionDictionaryMacros, sizecompletionDictionaryMacros);
231                     freeArrayOfString(completionDictionaryVariables, sizecompletionDictionaryVariables);
232                     freeArrayOfString(completionDictionaryHandleGraphicsProperties, sizecompletionDictionaryHandleGraphicsProperties);
233                 }
234
235                 displayPrompt();
236                 newLine();
237
238                 if (commonAll)
239                 {
240                     char *newline = NULL;
241
242                     newline = completeLine(lineBeforeCaret, commonAll, NULL, defaultPattern, FALSE, lineAfterCaret);
243
244                     if (newline)
245                     {
246                         clearCurrentLine();
247                         copyLine(newline);
248                         FREE(newline);
249                     }
250                     FREE(commonAll);
251                     commonAll = NULL;
252                 }
253             }
254         }
255     }
256 }
257 /*--------------------------------------------------------------------------*/
258 void TermCompletion(void)
259 {
260     char *LineBeforeCaret = getLineBeforeCaret();
261     char *LineAfterCaret = getLineAfterCaret();
262     char *fileSearchedPattern = getFilePartLevel(LineBeforeCaret);
263     char *SearchedPattern = getPartLevel(LineBeforeCaret);
264
265
266     char **completionDictionaryFiles = NULL;
267     int sizecompletionDictionaryFiles = 0;
268
269     completionDictionaryFiles = completionOnFiles(fileSearchedPattern, &sizecompletionDictionaryFiles);
270     if (completionDictionaryFiles)
271     {
272         TermCompletionOnFiles(completionDictionaryFiles, sizecompletionDictionaryFiles,
273                               LineBeforeCaret, LineAfterCaret, fileSearchedPattern, SearchedPattern);
274
275         freeArrayOfString(completionDictionaryFiles, sizecompletionDictionaryFiles);
276     }
277     else
278     {
279         TermCompletionOnAll(LineBeforeCaret, LineAfterCaret, SearchedPattern);
280     }
281
282     if (LineBeforeCaret)
283     {
284         FREE(LineBeforeCaret);
285         LineBeforeCaret = NULL;
286     }
287     if (LineAfterCaret)
288     {
289         FREE(LineAfterCaret);
290         LineAfterCaret = NULL;
291     }
292     if (fileSearchedPattern)
293     {
294         FREE(fileSearchedPattern);
295         fileSearchedPattern = NULL;
296     }
297     if (SearchedPattern)
298     {
299         FREE(SearchedPattern);
300         SearchedPattern = NULL;
301     }
302 }
303 /*--------------------------------------------------------------------------*/
304 static void displayCompletionDictionary(char **dictionary, int sizedictionary, char *namedictionary)
305 {
306     if (dictionary)
307     {
308         int i = 0;
309         int lenCurrentLine = 0;
310
311         TerminalPrintf("\n");
312         TerminalPrintf(namedictionary);
313         TerminalPrintf(":");
314         TerminalPrintf("\n");
315
316         for (i = 0; i < sizedictionary; i++)
317         {
318             int newlenLine = lenCurrentLine + (int)strlen(dictionary[i]) + (int)strlen(" ");
319             if ( newlenLine >= (getConsoleWidth() - 10) )
320             {
321                 TerminalPrintf("\n");
322                 lenCurrentLine = 0;
323             }
324             else
325             {
326                 lenCurrentLine = newlenLine;
327             }
328
329             TerminalPrintf(dictionary[i]);
330             TerminalPrintf(" ");
331         }
332         TerminalPrintf("\n");
333     }
334 }
335 /*--------------------------------------------------------------------------*/
336 static char **concatenateStrings(int *sizearrayofstring, char *string1,
337                                  char *string2, char *string3,
338                                  char *string4, char *string5)
339 {
340     int newsize = 0;
341     char **arrayOfString = NULL;
342     *sizearrayofstring = 0;
343
344     if (string1)
345     {
346         newsize++;
347     }
348     if (string2)
349     {
350         newsize++;
351     }
352     if (string3)
353     {
354         newsize++;
355     }
356     if (string4)
357     {
358         newsize++;
359     }
360     if (string5)
361     {
362         newsize++;
363     }
364
365     if (newsize > 0)
366     {
367         arrayOfString = (char**)MALLOC(sizeof(char*) * (newsize));
368         if (arrayOfString)
369         {
370             int i = 0;
371             if (string1)
372             {
373                 arrayOfString[i] = string1;
374                 i++;
375             }
376             if (string2)
377             {
378                 arrayOfString[i] = string2;
379                 i++;
380             }
381             if (string3)
382             {
383                 arrayOfString[i] = string3;
384                 i++;
385             }
386             if (string4)
387             {
388                 arrayOfString[i] = string4;
389                 i++;
390             }
391             if (string5)
392             {
393                 arrayOfString[i] = string5;
394                 i++;
395             }
396             *sizearrayofstring = i;
397         }
398         else
399         {
400             *sizearrayofstring = 0;
401         }
402     }
403     return arrayOfString;
404 }
405 /*--------------------------------------------------------------------------*/