[Python-checkins] CVS: python/dist/src/Parser tokenizer.c,2.38,2.39

Guido van Rossum python-dev@python.org
Thu, 30 Mar 2000 19:52:30 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Parser
In directory eric:/projects/python/develop/guido/src/Parser

Modified Files:
	tokenizer.c 
Log Message:
Fix by Eric Raymond: make the code that looks for various bits of
tab-setting magic much smarter, more correct, and more easily
extensible.


Index: tokenizer.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Parser/tokenizer.c,v
retrieving revision 2.38
retrieving revision 2.39
diff -C2 -r2.38 -r2.39
*** tokenizer.c	2000/03/10 22:56:54	2.38
--- tokenizer.c	2000/03/31 00:52:27	2.39
***************
*** 565,586 ****
  	tok->start = tok->cur - 1;
  	
! 	/* Skip comment */
  	if (c == '#') {
! 		/* Hack to allow overriding the tabsize in the file.
! 		   This is also recognized by vi, when it occurs near the
! 		   beginning or end of the file.  (Will vi never die...?)
! 		   For Python it must be at the beginning of the file! */
! 		/* XXX The real vi syntax is actually different :-( */
! 		/* XXX Should recognize Emacs syntax, too */
! 		int x;
! 		if (sscanf(tok->cur,
! 				" vi:set tabsize=%d:", &x) == 1 &&
! 						x >= 1 && x <= 40) {
! 			/* PySys_WriteStderr("# vi:set tabsize=%d:\n", x); */
! 			tok->tabsize = x;
! 		}
  		do {
  			c = tok_nextc(tok);
- 		} while (c != EOF && c != '\n');
  	}
  	
--- 565,601 ----
  	tok->start = tok->cur - 1;
  	
! 	/* Skip comment, while looking for tab-setting magic */
  	if (c == '#') {
! 		static char *tabforms[] = {
! 			"tab-width:",		/* Emacs */
! 			":tabstop=",		/* vim, full form */
! 			":ts=",			/* vim, abbreviated form */
! 			"set tabsize=",		/* will vi never die? */
! 		/* more templates can be added here to support other editors */
! 		};
! 		char cbuf[80];
! 		char *tp, **cp;
! 		tp = cbuf;
  		do {
+ 			*tp++ = c = tok_nextc(tok);
+ 		} while (c != EOF && c != '\n' &&
+ 			 tp - cbuf + 1 < sizeof(cbuf));
+ 		*tp = '\0';
+ 		for (cp = tabforms; 
+ 		     cp < tabforms + sizeof(tabforms)/sizeof(tabforms[0]);
+ 		     cp++) {
+ 			if ((tp = strstr(cbuf, *cp))) {
+ 				int newsize = atoi(tp + strlen(*cp));
+ 
+ 				if (newsize >= 1 && newsize <= 40) {
+ 					tok->tabsize = newsize;
+ 					PySys_WriteStderr(
+ 						"Tab size set to %d\n",
+ 						newsize);
+ 				}
+ 			}
+ 		}
+ 		while (c != EOF && c != '\n')
  			c = tok_nextc(tok);
  	}