Debian Codegraph @

vim.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * $Id: vim.c 762 2010-07-28 11:38:19Z dfishburn $
3 *
4 * Copyright (c) 2000-2003, Darren Hiebert
5 *
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
8 *
9 * Thanks are due to Jay Glanville for significant improvements.
10 *
11 * This module contains functions for generating tags for user-defined
12 * functions for the Vim editor.
13 */
14
15 /*
16 * INCLUDE FILES
17 */
18 #include "general.h" /* must always come first */
19
20 #include <string.h>
21 #include <setjmp.h>
22 #ifdef DEBUG
23 #include <stdio.h>
24 #endif
25
26
27 #include "parse.h"
28 #include "read.h"
29 #include "vstring.h"
30
31 #if 0
32 typedef struct sLineInfo {
33 tokenType type;
34 keywordId keyword;
35 vString * string;
36 vString * scope;
37 unsigned long lineNumber;
38 fpos_t filePosition;
39 } lineInfo;
40 #endif
41
42 /*
43 * DATA DEFINITIONS
44 */
45 typedef enum {
46 K_AUGROUP,
47 K_COMMAND,
48 K_FUNCTION,
49 K_MAP,
50 K_VARIABLE
51 } vimKind;
52
53 static kindOption VimKinds [] = {
54 { TRUE, 'a', "augroup", "autocommand groups" },
55 { TRUE, 'c', "command", "user-defined commands" },
56 { TRUE, 'f', "function", "function definitions" },
57 { TRUE, 'm', "map", "maps" },
58 { TRUE, 'v', "variable", "variable definitions" },
59 };
60
61 /*
62 * DATA DECLARATIONS
63 */
64
65 #if 0
66 typedef enum eException {
67 ExceptionNone, ExceptionEOF
68 } exception_t;
69 #endif
70
71 /*
72 * DATA DEFINITIONS
73 */
74
75 #if 0
76 static jmp_buf Exception;
77 #endif
78
79 /*
80 * FUNCTION DEFINITIONS
81 */
82
83 /* This function takes a char pointer, tries to find a scope separator in the
84 * string, and if it does, returns a pointer to the character after the colon,
85 * and the character defining the scope.
86 * If a colon is not found, it returns the original pointer.
87 */
88 static const unsigned char* skipPrefix (const unsigned char* name, int *scope)
89 {
90 const unsigned char* result = name;
91 int counter;
92 size_t length;
93 length = strlen((const char*)name);
94 if (scope != NULL)
95 *scope = '\0';
96 if (length > 3 && name[1] == ':')
97 {
98 if (scope != NULL)
99 *scope = *name;
100 result = name + 2;
101 }
102 else if (length > 5 && strncasecmp ((const char*) name, "<SID>", (size_t) 5) == 0)
103 {
104 if (scope != NULL)
105 *scope = *name;
106 result = name + 5;
107 }
108 else
109 {
110 /*
111 * Vim7 check for dictionaries or autoload function names
112 */
113 counter = 0;
114 do
115 {
116 switch ( name[counter] )
117 {
118 case '.':
119 /* Set the scope to d - Dictionary */
120 *scope = 'd';
121 break;
122 case '#':
123 /* Set the scope to a - autoload */
124 *scope = 'a';
125 break;
126 }
127 ++counter;
128 } while (isalnum ((int) name[counter]) ||
129 name[counter] == '_' ||
130 name[counter] == '.' ||
131 name[counter] == '#'
132 );
133 }
134 return result;
135 }
136
137 static boolean isMap (const unsigned char* line)
138 {
139 /*
140 * There are many different short cuts for specifying a map.
141 * This routine should capture all the permutations.
142 */
143 if (
144 strncmp ((const char*) line, "map", (size_t) 3) == 0 ||
145 strncmp ((const char*) line, "nm", (size_t) 2) == 0 ||
146 strncmp ((const char*) line, "nma", (size_t) 3) == 0 ||
147 strncmp ((const char*) line, "nmap", (size_t) 4) == 0 ||
148 strncmp ((const char*) line, "vm", (size_t) 2) == 0 ||
149 strncmp ((const char*) line, "vma", (size_t) 3) == 0 ||
150 strncmp ((const char*) line, "vmap", (size_t) 4) == 0 ||
151 strncmp ((const char*) line, "om", (size_t) 2) == 0 ||
152 strncmp ((const char*) line, "oma", (size_t) 3) == 0 ||
153 strncmp ((const char*) line, "omap", (size_t) 4) == 0 ||
154 strncmp ((const char*) line, "im", (size_t) 2) == 0 ||
155 strncmp ((const char*) line, "ima", (size_t) 3) == 0 ||
156 strncmp ((const char*) line, "imap", (size_t) 4) == 0 ||
157 strncmp ((const char*) line, "lm", (size_t) 2) == 0 ||
158 strncmp ((const char*) line, "lma", (size_t) 3) == 0 ||
159 strncmp ((const char*) line, "lmap", (size_t) 4) == 0 ||
160 strncmp ((const char*) line, "cm", (size_t) 2) == 0 ||
161 strncmp ((const char*) line, "cma", (size_t) 3) == 0 ||
162 strncmp ((const char*) line, "cmap", (size_t) 4) == 0 ||
163 strncmp ((const char*) line, "no", (size_t) 2) == 0 ||
164 strncmp ((const char*) line, "nor", (size_t) 3) == 0 ||
165 strncmp ((const char*) line, "nore", (size_t) 4) == 0 ||
166 strncmp ((const char*) line, "norem", (size_t) 5) == 0 ||
167 strncmp ((const char*) line, "norema", (size_t) 6) == 0 ||
168 strncmp ((const char*) line, "noremap", (size_t) 7) == 0 ||
169 strncmp ((const char*) line, "nno", (size_t) 3) == 0 ||
170 strncmp ((const char*) line, "nnor", (size_t) 4) == 0 ||
171 strncmp ((const char*) line, "nnore", (size_t) 5) == 0 ||
172 strncmp ((const char*) line, "nnorem", (size_t) 6) == 0 ||
173 strncmp ((const char*) line, "nnorema", (size_t) 7) == 0 ||
174 strncmp ((const char*) line, "nnoremap", (size_t) 8) == 0 ||
175 strncmp ((const char*) line, "vno", (size_t) 3) == 0 ||
176 strncmp ((const char*) line, "vnor", (size_t) 4) == 0 ||
177 strncmp ((const char*) line, "vnore", (size_t) 5) == 0 ||
178 strncmp ((const char*) line, "vnorem", (size_t) 6) == 0 ||
179 strncmp ((const char*) line, "vnorema", (size_t) 7) == 0 ||
180 strncmp ((const char*) line, "vnoremap", (size_t) 8) == 0 ||
181 strncmp ((const char*) line, "ono", (size_t) 3) == 0 ||
182 strncmp ((const char*) line, "onor", (size_t) 4) == 0 ||
183 strncmp ((const char*) line, "onore", (size_t) 5) == 0 ||
184 strncmp ((const char*) line, "onorem", (size_t) 6) == 0 ||
185 strncmp ((const char*) line, "onorema", (size_t) 7) == 0 ||
186 strncmp ((const char*) line, "onoremap", (size_t) 8) == 0 ||
187 strncmp ((const char*) line, "ino", (size_t) 3) == 0 ||
188 strncmp ((const char*) line, "inor", (size_t) 4) == 0 ||
189 strncmp ((const char*) line, "inore", (size_t) 5) == 0 ||
190 strncmp ((const char*) line, "inorem", (size_t) 6) == 0 ||
191 strncmp ((const char*) line, "inorema", (size_t) 7) == 0 ||
192 strncmp ((const char*) line, "inoremap", (size_t) 8) == 0 ||
193 strncmp ((const char*) line, "lno", (size_t) 3) == 0 ||
194 strncmp ((const char*) line, "lnor", (size_t) 4) == 0 ||
195 strncmp ((const char*) line, "lnore", (size_t) 5) == 0 ||
196 strncmp ((const char*) line, "lnorem", (size_t) 6) == 0 ||
197 strncmp ((const char*) line, "lnorema", (size_t) 7) == 0 ||
198 strncmp ((const char*) line, "lnoremap", (size_t) 8) == 0 ||
199 strncmp ((const char*) line, "cno", (size_t) 3) == 0 ||
200 strncmp ((const char*) line, "cnor", (size_t) 4) == 0 ||
201 strncmp ((const char*) line, "cnore", (size_t) 5) == 0 ||
202 strncmp ((const char*) line, "cnorem", (size_t) 6) == 0 ||
203 strncmp ((const char*) line, "cnorema", (size_t) 7) == 0 ||
204 strncmp ((const char*) line, "cnoremap", (size_t) 8) == 0
205 )
206 return TRUE;
207
208 return FALSE;
209 }
210
211 static const unsigned char * readVimLine (void)
212 {
213 const unsigned char *line;
214
215 while ((line = fileReadLine ()) != NULL)
216 {
217 while (isspace ((int) *line))
218 ++line;
219
220 if ((int) *line == '"')
221 continue; /* skip comment */
222
223 break;
224 }
225
226 return line;
227 }
228
229 static void parseFunction (const unsigned char *line)
230 {
231 vString *name = vStringNew ();
232 /* boolean inFunction = FALSE; */
233 int scope;
234
235 const unsigned char *cp = line + 1;
236
237 if ((int) *++cp == 'n' && (int) *++cp == 'c' &&
238 (int) *++cp == 't' && (int) *++cp == 'i' &&
239 (int) *++cp == 'o' && (int) *++cp == 'n')
240 ++cp;
241 if ((int) *cp == '!')
242 ++cp;
243 if (isspace ((int) *cp))
244 {
245 while (*cp && isspace ((int) *cp))
246 ++cp;
247
248 if (*cp)
249 {
250 cp = skipPrefix (cp, &scope);
251 if (isupper ((int) *cp) ||
252 scope == 's' || /* script scope */
253 scope == '<' || /* script scope */
254 scope == 'd' || /* dictionary */
255 scope == 'a') /* autoload */
256 {
257 do
258 {
259 vStringPut (name, (int) *cp);
260 ++cp;
261 } while (isalnum ((int) *cp) || *cp == '_' || *cp == '.' || *cp == '#');
262 vStringTerminate (name);
263 makeSimpleTag (name, VimKinds, K_FUNCTION);
264 vStringClear (name);
265 }
266 }
267 }
268
269 /* TODO - update struct to indicate inside function */
270 while ((line = readVimLine ()) != NULL)
271 {
272 /*
273 * Vim7 added the for/endfo[r] construct, so we must first
274 * check for an "endfo", before a "endf"
275 */
276 if ( (!strncmp ((const char*) line, "endfo", (size_t) 5) == 0) &&
277 (strncmp ((const char*) line, "endf", (size_t) 4) == 0) )
278 break;
279 /* TODO - call parseVimLine */
280 }
281 vStringDelete (name);
282 }
283
284 static void parseAutogroup (const unsigned char *line)
285 {
286 vString *name = vStringNew ();
287
288 /* Found Autocommand Group (augroup) */
289 const unsigned char *cp = line + 2;
290 if ((int) *++cp == 'r' && (int) *++cp == 'o' &&
291 (int) *++cp == 'u' && (int) *++cp == 'p')
292 ++cp;
293 if (isspace ((int) *cp))
294 {
295 while (*cp && isspace ((int) *cp))
296 ++cp;
297
298 if (*cp)
299 {
300 if (strncasecmp ((const char*) cp, "end", (size_t) 3) != 0)
301 {
302 do
303 {
304 vStringPut (name, (int) *cp);
305 ++cp;
306 } while (isalnum ((int) *cp) || *cp == '_');
307 vStringTerminate (name);
308 makeSimpleTag (name, VimKinds, K_AUGROUP);
309 vStringClear (name);
310 }
311 }
312 }
313 vStringDelete (name);
314 }
315
316 static boolean parseCommand (const unsigned char *line)
317 {
318 vString *name = vStringNew ();
319 boolean cmdProcessed = TRUE;
320
321 /*
322 * Found a user-defined command
323 *
324 * They can have many options preceeded by a dash
325 * command! -nargs=+ -complete Select :call s:DB_execSql("select " . <q-args>)
326 * The name of the command should be the first word not preceeded by a dash
327 *
328 */
329 const unsigned char *cp = line;
330
331 if ( (int) *cp == '\\' )
332 {
333 /*
334 * We are recursively calling this function is the command
335 * has been continued on to the next line
336 *
337 * Vim statements can be continued onto a newline using a \
338 * to indicate the previous line is continuing.
339 *
340 * com -nargs=1 -bang -complete=customlist,EditFileComplete
341 * \ EditFile edit<bang> <args>
342 *
343 * If the following lines do not have a line continuation
344 * the command must not be spanning multiple lines and should
345 * be synatically incorrect.
346 */
347 if ((int) *cp == '\\')
348 ++cp;
349
350 while (*cp && isspace ((int) *cp))
351 ++cp;
352 }
353 else if ( (!strncmp ((const char*) line, "comp", (size_t) 4) == 0) &&
354 (!strncmp ((const char*) line, "comc", (size_t) 4) == 0) &&
355 (strncmp ((const char*) line, "com", (size_t) 3) == 0) )
356 {
357 cp += 2;
358 if ((int) *++cp == 'm' && (int) *++cp == 'a' &&
359 (int) *++cp == 'n' && (int) *++cp == 'd')
360 ++cp;
361
362 if ((int) *cp == '!')
363 ++cp;
364
365 if ((int) *cp != ' ')
366 {
367 /*
368 * :command must be followed by a space. If it is not, it is
369 * not a valid command.
370 * Treat the line as processed and continue.
371 */
372 cmdProcessed = TRUE;
373 goto cleanUp;
374 }
375
376 while (*cp && isspace ((int) *cp))
377 ++cp;
378 }
379 else
380 {
381 /*
382 * We are recursively calling this function. If it does not start
383 * with "com" or a line continuation character, we have moved off
384 * the command line and should let the other routines parse this file.
385 */
386 cmdProcessed = FALSE;
387 goto cleanUp;
388 }
389
390 /*
391 * Strip off any spaces and options which are part of the command.
392 * These should preceed the command name.
393 */
394 do
395 {
396 if (isspace ((int) *cp))
397 {
398 ++cp;
399 }
400 else if (*cp == '-')
401 {
402 /*
403 * Read until the next space which separates options or the name
404 */
405 while (*cp && !isspace ((int) *cp))
406 ++cp;
407 }
408 } while ( *cp && !isalnum ((int) *cp) );
409
410 if ( ! *cp )
411 {
412 /*
413 * We have reached the end of the line without finding the command name.
414 * Read the next line and continue processing it as a command.
415 */
416 line = readVimLine();
417 parseCommand(line);
418 goto cleanUp;
419 }
420
421 do
422 {
423 vStringPut (name, (int) *cp);
424 ++cp;
425 } while (isalnum ((int) *cp) || *cp == '_');
426
427 vStringTerminate (name);
428 makeSimpleTag (name, VimKinds, K_COMMAND);
429 vStringClear (name);
430
431 cleanUp:
432 vStringDelete (name);
433
434 return cmdProcessed;
435 }
436
437 static void parseLet (const unsigned char *line)
438 {
439 vString *name = vStringNew ();
440
441 /* we've found a variable declared outside of a function!! */
442 const unsigned char *cp = line + 3;
443 const unsigned char *np = line;
444 /* get the name */
445 if (isspace ((int) *cp))
446 {
447 while (*cp && isspace ((int) *cp))
448 ++cp;
449
450 /*
451 * Ignore lets which set:
452 * & - local buffer vim settings
453 * @ - registers
454 * [ - Lists or Dictionaries
455 */
456 if (!*cp || *cp == '&' || *cp == '@' || *cp == '[' )
457 goto cleanUp;
458
459 /*
460 * Ignore vim variables which are read only
461 * v: - Vim variables.
462 */
463 np = cp;
464 ++np;
465 if ((int) *cp == 'v' && (int) *np == ':' )
466 goto cleanUp;
467
468 /* deal with spaces, $, @ and & */
469 while (*cp && *cp != '$' && !isalnum ((int) *cp))
470 ++cp;
471
472 if (!*cp)
473 goto cleanUp;
474
475 /* cp = skipPrefix (cp, &scope); */
476 do
477 {
478 if (!*cp)
479 break;
480
481 vStringPut (name, (int) *cp);
482 ++cp;
483 } while (isalnum ((int) *cp) || *cp == '_' || *cp == '#' || *cp == ':' || *cp == '$');
484 vStringTerminate (name);
485 makeSimpleTag (name, VimKinds, K_VARIABLE);
486 vStringClear (name);
487 }
488
489 cleanUp:
490 vStringDelete (name);
491 }
492
493 static boolean parseMap (const unsigned char *line)
494 {
495 vString *name = vStringNew ();
496
497 const unsigned char *cp = line;
498
499 /* Remove map */
500 while (*cp && isalnum ((int) *cp))
501 ++cp;
502
503 if ((int) *cp == '!')
504 ++cp;
505
506 /*
507 * Maps follow this basic format
508 * map
509 * nnoremap <silent> <F8> :Tlist<CR>
510 * map <unique> <Leader>scdt <Plug>GetColumnDataType
511 * inoremap ,,, <esc>diwi<<esc>pa><cr></<esc>pa><esc>kA
512 * inoremap <buffer> ( <C-R>=PreviewFunctionSignature()<LF>
513 *
514 * The Vim help shows the various special arguments available to a map:
515 * 1.2 SPECIAL ARGUMENTS *:map-arguments*
516 * <buffer>
517 * <silent>
518 * <script>
519 * <unique>
520 * <special>
521 * <expr>
522 *
523 * Strip the special arguments from the map command, this should leave
524 * the map name which we will use as the "name".
525 */
526
527 do
528 {
529 while (*cp && isspace ((int) *cp))
530 ++cp;
531
532 if (strncmp ((const char*) cp, "<Leader>", (size_t) 8) == 0)
533 break;
534
535 if (
536 strncmp ((const char*) cp, "<buffer>", (size_t) 8) == 0 ||
537 strncmp ((const char*) cp, "<silent>", (size_t) 8) == 0 ||
538 strncmp ((const char*) cp, "<script>", (size_t) 8) == 0 ||
539 strncmp ((const char*) cp, "<unique>", (size_t) 8) == 0
540 )
541 {
542 cp += 8;
543 continue;
544 }
545
546 if (strncmp ((const char*) cp, "<expr>", (size_t) 6) == 0)
547 {
548 cp += 6;
549 continue;
550 }
551
552 if (strncmp ((const char*) cp, "<special>", (size_t) 9) == 0)
553 {
554 cp += 9;
555 continue;
556 }
557
558 break;
559 } while (*cp);
560
561 do
562 {
563 vStringPut (name, (int) *cp);
564 ++cp;
565 } while (*cp && *cp != ' ');
566
567 vStringTerminate (name);
568 makeSimpleTag (name, VimKinds, K_MAP);
569 vStringClear (name);
570
571 vStringDelete (name);
572
573 return TRUE;
574 }
575
576 static boolean parseVimLine (const unsigned char *line)
577 {
578 boolean readNextLine = TRUE;
579
580 if ( (!strncmp ((const char*) line, "comp", (size_t) 4) == 0) &&
581 (!strncmp ((const char*) line, "comc", (size_t) 4) == 0) &&
582 (strncmp ((const char*) line, "com", (size_t) 3) == 0) )
583 {
584 readNextLine = parseCommand(line);
585 /* TODO - Handle parseCommand returning FALSE */
586 }
587
588 if (isMap(line))
589 {
590 parseMap(line);
591 }
592
593 if (strncmp ((const char*) line, "fu", (size_t) 2) == 0)
594 {
595 parseFunction(line);
596 }
597
598 if (strncmp ((const char*) line, "aug", (size_t) 3) == 0)
599 {
600 parseAutogroup(line);
601 }
602
603 if ( strncmp ((const char*) line, "let", (size_t) 3) == 0 )
604 {
605 parseLet(line);
606 }
607
608 return readNextLine;
609 }
610
611 static void parseVimFile (const unsigned char *line)
612 {
613 boolean readNextLine = TRUE;
614 line = readVimLine();
615
616 while (line != NULL)
617 {
618 readNextLine = parseVimLine(line);
619
620 if ( readNextLine )
621 line = readVimLine();
622
623 }
624 }
625
626 static void findVimTags (void)
627 {
628 const unsigned char *line;
629 /* TODO - change this into a structure */
630
631 line = '\0';
632
633 parseVimFile (line);
634 }
635
636 extern parserDefinition* VimParser (void)
637 {
638 static const char *const extensions [] = { "vim", NULL };
639 parserDefinition* def = parserNew ("Vim");
640 def->kinds = VimKinds;
641 def->kindCount = KIND_COUNT (VimKinds);
642 def->extensions = extensions;
643 def->parser = findVimTags;
644 return def;
645 }
646
647 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */