Debian Codegraph @

vim.c

raw ◀ ▶
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 else
409 break;
410 } while ( *cp );
411
412 if ( ! *cp )
413 {
414 /*
415 * We have reached the end of the line without finding the command name.
416 * Read the next line and continue processing it as a command.
417 */
418 line = readVimLine();
419 parseCommand(line);
420 goto cleanUp;
421 }
422
423 do
424 {
425 vStringPut (name, (int) *cp);
426 ++cp;
427 } while (isalnum ((int) *cp) || *cp == '_');
428
429 vStringTerminate (name);
430 makeSimpleTag (name, VimKinds, K_COMMAND);
431 vStringClear (name);
432
433 cleanUp:
434 vStringDelete (name);
435
436 return cmdProcessed;
437 }
438
439 static void parseLet (const unsigned char *line)
440 {
441 vString *name = vStringNew ();
442
443 /* we've found a variable declared outside of a function!! */
444 const unsigned char *cp = line + 3;
445 const unsigned char *np = line;
446 /* get the name */
447 if (isspace ((int) *cp))
448 {
449 while (*cp && isspace ((int) *cp))
450 ++cp;
451
452 /*
453 * Ignore lets which set:
454 * & - local buffer vim settings
455 * @ - registers
456 * [ - Lists or Dictionaries
457 */
458 if (!*cp || *cp == '&' || *cp == '@' || *cp == '[' )
459 goto cleanUp;
460
461 /*
462 * Ignore vim variables which are read only
463 * v: - Vim variables.
464 */
465 np = cp;
466 ++np;
467 if ((int) *cp == 'v' && (int) *np == ':' )
468 goto cleanUp;
469
470 /* deal with spaces, $, @ and & */
471 while (*cp && *cp != '$' && !isalnum ((int) *cp))
472 ++cp;
473
474 if (!*cp)
475 goto cleanUp;
476
477 /* cp = skipPrefix (cp, &scope); */
478 do
479 {
480 if (!*cp)
481 break;
482
483 vStringPut (name, (int) *cp);
484 ++cp;
485 } while (isalnum ((int) *cp) || *cp == '_' || *cp == '#' || *cp == ':' || *cp == '$');
486 vStringTerminate (name);
487 makeSimpleTag (name, VimKinds, K_VARIABLE);
488 vStringClear (name);
489 }
490
491 cleanUp:
492 vStringDelete (name);
493 }
494
495 static boolean parseMap (const unsigned char *line)
496 {
497 vString *name = vStringNew ();
498
499 const unsigned char *cp = line;
500
501 /* Remove map */
502 while (*cp && isalnum ((int) *cp))
503 ++cp;
504
505 if ((int) *cp == '!')
506 ++cp;
507
508 /*
509 * Maps follow this basic format
510 * map
511 * nnoremap <silent> <F8> :Tlist<CR>
512 * map <unique> <Leader>scdt <Plug>GetColumnDataType
513 * inoremap ,,, <esc>diwi<<esc>pa><cr></<esc>pa><esc>kA
514 * inoremap <buffer> ( <C-R>=PreviewFunctionSignature()<LF>
515 *
516 * The Vim help shows the various special arguments available to a map:
517 * 1.2 SPECIAL ARGUMENTS *:map-arguments*
518 * <buffer>
519 * <silent>
520 * <script>
521 * <unique>
522 * <special>
523 * <expr>
524 *
525 * Strip the special arguments from the map command, this should leave
526 * the map name which we will use as the "name".
527 */
528
529 do
530 {
531 while (*cp && isspace ((int) *cp))
532 ++cp;
533
534 if (strncmp ((const char*) cp, "<Leader>", (size_t) 8) == 0)
535 break;
536
537 if (
538 strncmp ((const char*) cp, "<buffer>", (size_t) 8) == 0 ||
539 strncmp ((const char*) cp, "<silent>", (size_t) 8) == 0 ||
540 strncmp ((const char*) cp, "<script>", (size_t) 8) == 0 ||
541 strncmp ((const char*) cp, "<unique>", (size_t) 8) == 0
542 )
543 {
544 cp += 8;
545 continue;
546 }
547
548 if (strncmp ((const char*) cp, "<expr>", (size_t) 6) == 0)
549 {
550 cp += 6;
551 continue;
552 }
553
554 if (strncmp ((const char*) cp, "<special>", (size_t) 9) == 0)
555 {
556 cp += 9;
557 continue;
558 }
559
560 break;
561 } while (*cp);
562
563 do
564 {
565 vStringPut (name, (int) *cp);
566 ++cp;
567 } while (*cp && *cp != ' ');
568
569 vStringTerminate (name);
570 makeSimpleTag (name, VimKinds, K_MAP);
571 vStringClear (name);
572
573 vStringDelete (name);
574
575 return TRUE;
576 }
577
578 static boolean parseVimLine (const unsigned char *line)
579 {
580 boolean readNextLine = TRUE;
581
582 if ( (!strncmp ((const char*) line, "comp", (size_t) 4) == 0) &&
583 (!strncmp ((const char*) line, "comc", (size_t) 4) == 0) &&
584 (strncmp ((const char*) line, "com", (size_t) 3) == 0) )
585 {
586 readNextLine = parseCommand(line);
587 /* TODO - Handle parseCommand returning FALSE */
588 }
589
590 if (isMap(line))
591 {
592 parseMap(line);
593 }
594
595 if (strncmp ((const char*) line, "fu", (size_t) 2) == 0)
596 {
597 parseFunction(line);
598 }
599
600 if (strncmp ((const char*) line, "aug", (size_t) 3) == 0)
601 {
602 parseAutogroup(line);
603 }
604
605 if ( strncmp ((const char*) line, "let", (size_t) 3) == 0 )
606 {
607 parseLet(line);
608 }
609
610 return readNextLine;
611 }
612
613 static void parseVimFile (const unsigned char *line)
614 {
615 boolean readNextLine = TRUE;
616 line = readVimLine();
617
618 while (line != NULL)
619 {
620 readNextLine = parseVimLine(line);
621
622 if ( readNextLine )
623 line = readVimLine();
624
625 }
626 }
627
628 static void findVimTags (void)
629 {
630 const unsigned char *line;
631 /* TODO - change this into a structure */
632
633 line = '\0';
634
635 parseVimFile (line);
636 }
637
638 extern parserDefinition* VimParser (void)
639 {
640 static const char *const extensions [] = { "vim", NULL };
641 parserDefinition* def = parserNew ("Vim");
642 def->kinds = VimKinds;
643 def->kindCount = KIND_COUNT (VimKinds);
644 def->extensions = extensions;
645 def->parser = findVimTags;
646 return def;
647 }
648
649 /* vi:set tabstop=4 shiftwidth=4 noexpandtab: */