Alexander Kriegisch committed 1953b14

Kconfig lexer: recognise Trac setting 'tab_width'

Kconfig lexer now uses a trick in order to recognise that it is used
from within Trac: Via call stack inspection it finds a runtime
setting corresponding to:

trac.ini, [mimeviewer], tab_width

Comments (0)

Files changed (1)


+import inspect
 def _rx_indent(level):
     # Kconfig *always* interprets a tab as 8 spaces, so this is the default.
     # Edit this if you are in an environment where KconfigLexer gets expanded
-    # input (tabs expanded to spaces) and the expansion tab width is != 8,
-    # e.g. in connection with Trac (trac.ini, [mimeviewer], tab_width).
+    # input (tabs expanded to spaces) and the expansion tab width is != 8.
     # Value range here is 2 <= {tab_width} <= 8.
     tab_width = 8
+    # Special case: Pygments is called by Trac.
+    # Inspect call-stack and find out the value of Mimeviewer.tab_width which
+    # has previously been read from trac.ini, [mimeviewer], tab_width.
+    for frame in inspect.stack():
+        if frame[1].endswith('/trac/mimeview/'):
+            tab_width=inspect.getargvalues(frame[0]).locals['self'].tab_width
+            #print "Detected by introspection: tab_width =", tab_width
+            break
     # Regex matching a given indentation {level}, assuming that indentation is
     # a multiple of {tab_width}. In other cases there might be problems.
     return r'(?:\t| {1,%s}\t| {%s}){%s}.*\n' % (tab_width-1, tab_width, level)
     flags = 0
     def call_indent(level):
-        # If indentation >= {level} is detected, enter state 'indent{level}'
+        # If indentation >= {level} is detected, enter state 'indent{level}'.
+        # Attention: This heuristic is not perfect, but should work for 99% of
+        # files with clean indentation (i.e. multiples of {tab_width}).
         return (_rx_indent(level), String.Doc, 'indent%s' % level)
     def do_indent(level):
         'help': [
             # Skip blank lines after help token, if any
             (r'\s*\n', Text),
-            # Determine the first help line's indentation level heuristically(!).
-            # Attention: this is not perfect, but works for 99% of "normal"
-            # indentation schemes up to a max. indentation level of 7.
+            # Determine first help text line's indentation level (max. 7) and
+            # enter corresponding lexer state to handle the rest
             ('', Text, '#pop'),  # for incomplete help sections without text
-        # Handle text for indentation levels 7 to 1
+        # Handle help text for indentation levels 7 to 1
         'indent7': do_indent(7),
         'indent6': do_indent(6),
         'indent5': do_indent(5),
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.