Repository: hylang/hy

Issue:
Merge hy.completer and hy.readline_helpers
after merging in #213 I realized we should consolidate code in the completer and readline helpers



Discussion:

Resolving patch:
diff --git a/hy/cmdline.py b/hy/cmdline.py
--- a/hy/cmdline.py
+++ b/hy/cmdline.py
@@ -36,9 +36,7 @@
 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()
-
-    if not hr:
-        hr = HyREPL()
-    hr.interact("{appname} {version}".format(
-        appname=hy.__appname__,
-        version=hy.__version__
-    ))
+    with completion():
+        if not hr:
+            hr = HyREPL()
 
-    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
--- a/hy/completer.py
+++ b/hy/completer.py
@@ -1,4 +1,11 @@
 # Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
+# Copyright (c) 2013 Gergely Nagy <algernon@madhouse-project.org>
+# Copyright (c) 2013 James King <james@agentultra.com>
+# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
+# Copyright (c) 2013 Konrad Hinsen <konrad.hinsen@fastmail.net>
+# Copyright (c) 2013 Thom Neale <twneale@gmail.com>
+# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
+# Copyright (c) 2013 Ralph Moritz <ralph.moeritz@outlook.com>
 #
 # 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 @@ def complete(self, text, state):
         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
--- a/hy/readline_helpers.py
+++ /dev/null
@@ -1,68 +0,0 @@
-# -*- coding: utf-8 -*-
-# Copyright (c) 2013 Paul Tagliamonte <paultag@debian.org>
-# Copyright (c) 2013 Gergely Nagy <algernon@madhouse-project.org>
-# Copyright (c) 2013 James King <james@agentultra.com>
-# Copyright (c) 2013 Julien Danjou <julien@danjou.info>
-# Copyright (c) 2013 Konrad Hinsen <konrad.hinsen@fastmail.net>
-# Copyright (c) 2013 Thom Neale <twneale@gmail.com>
-# Copyright (c) 2013 Will Kahn-Greene <willg@bluesock.org>
-# Copyright (c) 2013 Ralph Möritz <ralph.moeritz@outlook.com>
-#
-# 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)


Test patch: