From 748f54b2f9067d52793a2c63c34b79c1af501fb1 Mon Sep 17 00:00:00 2001 From: Ralph Moritz Date: Sat, 6 Jul 2013 17:43:51 +0200 Subject: [PATCH] Move code from hy.readline_helpers into new hy.completer.completion context manager so we can "wrap" readline completion around REPL interaction. --- hy/cmdline.py | 20 +++++-------- hy/completer.py | 53 ++++++++++++++++++++++++++++++-- hy/readline_helpers.py | 68 ------------------------------------------ 3 files changed, 59 insertions(+), 82 deletions(-) delete mode 100644 hy/readline_helpers.py diff --git a/hy/cmdline.py b/hy/cmdline.py index b3dfe10..12a0503 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -36,9 +36,7 @@ from hy.lex.states import Idle, LexException from hy.lex.machine import Machine from hy.compiler import hy_compile from hy.core import process - -from hy.readline_helpers import read_history_file, write_history_file -import hy.completer +from hy.completer import completion from hy.models.expression import HyExpression from hy.models.string import HyString @@ -183,16 +181,14 @@ def run_repl(hr=None): sys.ps1 = "=> " sys.ps2 = "... " - history = read_history_file() + with completion(): + if not hr: + hr = HyREPL() - if not hr: - hr = HyREPL() - hr.interact("{appname} {version}".format( - appname=hy.__appname__, - version=hy.__version__ - )) - - write_history_file(history) + hr.interact("{appname} {version}".format( + appname=hy.__appname__, + version=hy.__version__ + )) return 0 diff --git a/hy/completer.py b/hy/completer.py index be75cd2..b0c5018 100644 --- a/hy/completer.py +++ b/hy/completer.py @@ -1,4 +1,11 @@ # Copyright (c) 2013 Paul Tagliamonte +# Copyright (c) 2013 Gergely Nagy +# Copyright (c) 2013 James King +# Copyright (c) 2013 Julien Danjou +# Copyright (c) 2013 Konrad Hinsen +# Copyright (c) 2013 Thom Neale +# Copyright (c) 2013 Will Kahn-Greene +# Copyright (c) 2013 Ralph Moritz # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), @@ -18,9 +25,22 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. +import os + +docomplete = True + +try: + import readline +except ImportError: + try: + import pyreadline.rlmain + import pyreadline.unicode_helper # NOQA + import readline + except ImportError: + docomplete = False + import hy.macros import hy.compiler -from hy.readline_helpers import set_completer try: import __builtin__ @@ -58,4 +78,33 @@ class Completer(object): except IndexError: return None -set_completer(Completer().complete, "()[]{} ") + +class completion(object): + delims = "()[]{} " + + def __init__(self, completer=None): + if not completer: + completer = Completer() + + self.completer = completer + + def __enter__(self): + if not docomplete: + return + + readline.set_completer(self.completer.complete) + readline.set_completer_delims(self.delims) + + self.history = os.path.expanduser("~/.hy-history") + readline.parse_and_bind("set blink-matching-paren on") + + try: + readline.read_history_file(self.history) + except IOError: + open(self.history, 'a').close() + + readline.parse_and_bind("tab: complete") + + def __exit__(self, type, value, tb): + if docomplete: + readline.write_history_file(self.history) diff --git a/hy/readline_helpers.py b/hy/readline_helpers.py deleted file mode 100644 index dfa03e9..0000000 --- a/hy/readline_helpers.py +++ /dev/null @@ -1,68 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright (c) 2013 Paul Tagliamonte -# Copyright (c) 2013 Gergely Nagy -# Copyright (c) 2013 James King -# Copyright (c) 2013 Julien Danjou -# Copyright (c) 2013 Konrad Hinsen -# Copyright (c) 2013 Thom Neale -# Copyright (c) 2013 Will Kahn-Greene -# Copyright (c) 2013 Ralph Möritz -# -# Permission is hereby granted, free of charge, to any person obtaining a -# copy of this software and associated documentation files (the "Software"), -# to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, -# and/or sell copies of the Software, and to permit persons to whom the -# Software is furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. - -import os - -noop = False - -try: - import readline -except ImportError: - try: - import pyreadline.rlmain - import pyreadline.unicode_helper # NOQA - import readline - except ImportError: - noop = True - - -def set_completer(completer, delims): - if not noop: - readline.set_completer(completer) - readline.set_completer_delims(delims) - - -def read_history_file(): - if noop: - return None - - history = os.path.expanduser("~/.hy-history") - readline.parse_and_bind("set blink-matching-paren on") - - try: - readline.read_history_file(history) - except IOError: - open(history, 'a').close() - - readline.parse_and_bind("tab: complete") - return history - - -def write_history_file(history): - if not noop: - readline.write_history_file(history)