From 3fc492055487657ffec27904b847ea49e0143f3b Mon Sep 17 00:00:00 2001 From: smileboy <294101042@qq.com> Date: Fri, 15 Jan 2016 12:27:39 +0800 Subject: [PATCH 1/3] update the config file --- examples/config/pyvimrc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/config/pyvimrc b/examples/config/pyvimrc index 6de61de..9302589 100644 --- a/examples/config/pyvimrc +++ b/examples/config/pyvimrc @@ -53,7 +53,7 @@ def configure(editor): editor_buffer = editor.current_editor_buffer if editor_buffer is not None: - if editor_buffer.filename is None: + if editor_buffer.get_display_name() is None: editor.show_message("File doesn't have a filename. Please save first.") return else: @@ -63,7 +63,7 @@ def configure(editor): # `CommandLineInterface.run_in_terminal` to go to the background and # not destroy the window layout. def execute(): - call(['python', editor_buffer.filename]) + call(['python', editor_buffer.get_display_name()]) six.moves.input('Press enter to continue...') editor.cli.run_in_terminal(execute) From fe2ea012d4e351e318ac8ffcaff6832c80677385 Mon Sep 17 00:00:00 2001 From: smileboy <294101042@qq.com> Date: Fri, 15 Jan 2016 22:35:03 +0800 Subject: [PATCH 2/3] use the libmagic and python-magic to decide the file type, it's more convenience --- pyvim/lexer.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyvim/lexer.py b/pyvim/lexer.py index 9454b40..4e901ab 100644 --- a/pyvim/lexer.py +++ b/pyvim/lexer.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals from prompt_toolkit.layout.lexers import Lexer -from pygments.lexers import get_lexer_for_filename +from pygments.lexers import get_lexer_for_mimetype from pygments.token import Token from pygments.util import ClassNotFound @@ -26,7 +26,9 @@ def get_tokens(self, cli, text): if location: # Create an instance of the correct lexer class. try: - lexer = get_lexer_for_filename(location, stripnl=False, stripall=False, ensurenl=False) + import magic + mimetype = magic.from_file(location, mime=True) + lexer = get_lexer_for_mimetype(mimetype) except ClassNotFound: pass else: From 7c282f8279c2ada445c4ac317f03e37ea617dcdc Mon Sep 17 00:00:00 2001 From: smileboy <294101042@qq.com> Date: Sat, 16 Jan 2016 00:55:44 +0800 Subject: [PATCH 3/3] use cascade way to determine the file type --- pyvim/lexer.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pyvim/lexer.py b/pyvim/lexer.py index 4e901ab..b2e8f55 100644 --- a/pyvim/lexer.py +++ b/pyvim/lexer.py @@ -2,6 +2,7 @@ from prompt_toolkit.layout.lexers import Lexer from pygments.lexers import get_lexer_for_mimetype +from pygments.lexers import get_lexer_for_filename from pygments.token import Token from pygments.util import ClassNotFound @@ -26,11 +27,16 @@ def get_tokens(self, cli, text): if location: # Create an instance of the correct lexer class. try: - import magic - mimetype = magic.from_file(location, mime=True) - lexer = get_lexer_for_mimetype(mimetype) + lexer = get_lexer_for_filename(location, stripnl=False, stripall=False, ensurenl=False) except ClassNotFound: - pass + try: + import magic + mimetype = magic.from_file(location, mime=True) + lexer = get_lexer_for_mimetype(mimetype) + except ClassNotFound: + pass + else: + return lexer.get_tokens(text) else: return lexer.get_tokens(text)