Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab5ca15

Browse files
committed
Fix by Eric Raymond: make the code that looks for various bits of
tab-setting magic much smarter, more correct, and more easily extensible.
1 parent a1f0a8f commit ab5ca15

1 file changed

Lines changed: 30 additions & 15 deletions

File tree

Parser/tokenizer.c

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -564,24 +564,39 @@ PyTokenizer_Get(tok, p_start, p_end)
564564
/* Set start of current token */
565565
tok->start = tok->cur - 1;
566566

567-
/* Skip comment */
567+
/* Skip comment, while looking for tab-setting magic */
568568
if (c == '#') {
569-
/* Hack to allow overriding the tabsize in the file.
570-
This is also recognized by vi, when it occurs near the
571-
beginning or end of the file. (Will vi never die...?)
572-
For Python it must be at the beginning of the file! */
573-
/* XXX The real vi syntax is actually different :-( */
574-
/* XXX Should recognize Emacs syntax, too */
575-
int x;
576-
if (sscanf(tok->cur,
577-
" vi:set tabsize=%d:", &x) == 1 &&
578-
x >= 1 && x <= 40) {
579-
/* PySys_WriteStderr("# vi:set tabsize=%d:\n", x); */
580-
tok->tabsize = x;
581-
}
569+
static char *tabforms[] = {
570+
"tab-width:", /* Emacs */
571+
":tabstop=", /* vim, full form */
572+
":ts=", /* vim, abbreviated form */
573+
"set tabsize=", /* will vi never die? */
574+
/* more templates can be added here to support other editors */
575+
};
576+
char cbuf[80];
577+
char *tp, **cp;
578+
tp = cbuf;
582579
do {
580+
*tp++ = c = tok_nextc(tok);
581+
} while (c != EOF && c != '\n' &&
582+
tp - cbuf + 1 < sizeof(cbuf));
583+
*tp = '\0';
584+
for (cp = tabforms;
585+
cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]);
586+
cp++) {
587+
if ((tp = strstr(cbuf, *cp))) {
588+
int newsize = atoi(tp + strlen(*cp));
589+
590+
if (newsize >= 1 && newsize <= 40) {
591+
tok->tabsize = newsize;
592+
PySys_WriteStderr(
593+
"Tab size set to %d\n",
594+
newsize);
595+
}
596+
}
597+
}
598+
while (c != EOF && c != '\n')
583599
c = tok_nextc(tok);
584-
} while (c != EOF && c != '\n');
585600
}
586601

587602
/* Check for EOF and errors now */

0 commit comments

Comments
 (0)