emacs/vi mode autodetection

--HG--
branch : pmacs2
This commit is contained in:
moculus 2009-04-23 15:47:54 +00:00
parent 78967c50c8
commit 3d06125ed5
2 changed files with 39 additions and 28 deletions

View File

@ -1,5 +1,9 @@
import re import re
# mode stuff (emacs/vi)
auto_mode_emacs = re.compile(r'-\*- ([^ ]+) -\*-')
auto_mode_vi = re.compile('(?:vim|vi|ex):.+?(?:syntax|syn)=([^ ]+)')
# lexing # lexing
reserved_token_names = re.compile(r'^(?:rules|null|start|end|middle[0-9]*)$') reserved_token_names = re.compile(r'^(?:rules|null|start|end|middle[0-9]*)$')
valid_token_name = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$') valid_token_name = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')
@ -14,7 +18,7 @@ shell_command = re.compile(r'^[^ ]+')
# whitespace regexes # whitespace regexes
leading_whitespace = re.compile('^ *') leading_whitespace = re.compile('^ *')
trailing_whitespace = re.compile(' *$') trailing_whitespace = re.compile(' *$')
whitespace = re.compile('^[ \n]*$') whitespace = re.compile(r'^[ \n]*$')
space = re.compile('^ *$') space = re.compile('^ *$')
leading_whitespace2 = re.compile('^( *?)(.*?)\n?$') leading_whitespace2 = re.compile('^( *?)(.*?)\n?$')
@ -25,12 +29,12 @@ word_char = re.compile('^[A-Za-z0-9_]$')
# perl regexes # perl regexes
perl_base = re.compile("^sub ") perl_base = re.compile("^sub ")
perl_hash_cleanup = re.compile(r"^( *)([^ ]+|'(?:\.|[^'\'])*'|\"(?:\.|[^\\\"]*)\")( *)(=>)( *)([^ ].*)$") perl_hash_cleanup = re.compile(r"^( *)([^ ]+|'(?:\.|[^'\'])*'|\"(?:\.|[^\\\"]*)\")( *)(=>)( *)([^ ].*)$")
perl_assign_cleanup = re.compile(r"^( *)((?:my |our )?[^ ]+)( *)(=(?!>))( *)([^ ].*)$") perl_assign_cleanup = re.compile("^( *)((?:my |our )?[^ ]+)( *)(=(?!>))( *)([^ ].*)$")
perl_function = re.compile(r"^ *sub ([A-Za-z_][A-Za-z0-9_]*)") perl_function = re.compile("^ *sub ([A-Za-z_][A-Za-z0-9_]*)")
# python regexes # python regexes
python_base = re.compile(r"^[^ ]") python_base = re.compile("^[^ ]")
python_dict_cleanup = re.compile(r"^( *)((?:[^'\":]|'(?:\.|[^\'])*'|\"(?:\.|[^\'])*)+?)( *)(:)( *)([^ ].*)$") python_dict_cleanup = re.compile(r"^( *)((?:[^'\":]|'(?:\.|[^\'])*'|\"(?:\.|[^\'])*)+?)( *)(:)( *)([^ ].*)$")
python_assign_cleanup = re.compile(r"^( *)([^ ]+)( *)(=)( *)([^ ].*)$") python_assign_cleanup = re.compile("^( *)([^ ]+)( *)(=)( *)([^ ].*)$")
python_scope = re.compile('^( *)(class|def) ([A-Za-z_][A-Za-z0-9_]*)') python_scope = re.compile('^( *)(class|def) ([A-Za-z_][A-Za-z0-9_]*)')
python_indent = re.compile('^( *)') python_indent = re.compile('^( *)')

View File

@ -45,24 +45,31 @@ class Window(object):
basename = os.path.basename(path) basename = os.path.basename(path)
ext = self._get_path_ext(path) ext = self._get_path_ext(path)
if path in self.application.mode_paths: # we don't want to accidentally end up searching binary (or pseudo-
mode_name = self.application.mode_paths[path] # binary) files. this is kind of a hack
elif basename in self.application.mode_basenames: if b.lines and len(b.lines[0]) < 4096:
mode_name = self.application.mode_basenames[basename] firstline = b.lines[0]
elif ext in self.application.mode_extensions: else:
mode_name = self.application.mode_extensions[ext] firstline = ''
elif len(b.lines) > 0 and \
b.lines[0].startswith('#!'): if path in a.mode_paths:
mode_name = a.mode_paths[path]
elif basename in a.mode_basenames:
mode_name = a.mode_basenames[basename]
elif ext in a.mode_extensions:
mode_name = a.mode_extensions[ext]
elif regex.auto_mode_emacs.search(firstline):
mode_name = regex.auto_mode_emacs.search(firstline).group(1)
elif regex.auto_mode_vi.search(firstline):
mode_name = regex.auto_mode_vi.search(firstline).group(1)
elif firstline.startswith('#!'):
line = b.lines[0] line = b.lines[0]
for word in self.application.mode_detection: for word in a.mode_detection:
if word in line: if word in line:
mode_name = self.application.mode_detection[word] mode_name = a.mode_detection[word]
if mode_name is None: cls = a.modes.get(mode_name, a.modes['fundamental'])
mode_name = "fundamental" self.set_mode(cls(self))
m = self.application.modes[mode_name](self)
self.set_mode(m)
b.add_window(self) b.add_window(self)
# private method used in window constructor # private method used in window constructor