From 888dc1988283e9aa0dfd0ba21a836b07e6cd6bef Mon Sep 17 00:00:00 2001 From: Ralph Moritz Date: Sat, 29 Jun 2013 16:23:48 +0200 Subject: [PATCH 1/5] Get Hy working on Windows by making readline use conditional. --- AUTHORS | 2 +- hy/cmdline.py | 17 ++++------- hy/completer.py | 9 ++---- hy/readline_helpers.py | 65 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 6 +++- 5 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 hy/readline_helpers.py diff --git a/AUTHORS b/AUTHORS index 9136050..3630a44 100644 --- a/AUTHORS +++ b/AUTHORS @@ -14,4 +14,4 @@ * Thomas Ballinger * Morten Linderud * Guillermo Vayá - +* Ralph Möritz diff --git a/hy/cmdline.py b/hy/cmdline.py index 0393e6a..2ea7ec7 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -28,7 +28,6 @@ import ast import code import optparse import os -import readline import sys import hy @@ -40,13 +39,13 @@ from hy.core import process from hy.importer import ast_compile, import_buffer_to_module import hy.completer +from hy.readline_helpers import read_history_file, write_history_file from hy.macros import macro, require from hy.models.expression import HyExpression from hy.models.string import HyString from hy.models.symbol import HySymbol - _machine = Machine(Idle, 1, 0) try: @@ -185,15 +184,7 @@ def run_repl(hr=None): sys.ps1 = "=> " sys.ps2 = "... " - 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") + history = read_history_file() if not hr: hr = HyREPL() @@ -201,7 +192,9 @@ def run_repl(hr=None): appname=hy.__appname__, version=hy.__version__ )) - readline.write_history_file(history) + + write_history_file(history) + return 0 diff --git a/hy/completer.py b/hy/completer.py index d02be23..f88a880 100644 --- a/hy/completer.py +++ b/hy/completer.py @@ -20,6 +20,7 @@ import hy.macros import hy.compiler +from hy.readline_helpers import set_completer try: import __builtin__ @@ -57,11 +58,5 @@ class Completer(object): except IndexError: return None +set_completer(Completer().complete, "()[]{} ") -try: - import readline -except ImportError: - pass -else: - readline.set_completer(Completer().complete) - readline.set_completer_delims("()[]{} ") diff --git a/hy/readline_helpers.py b/hy/readline_helpers.py new file mode 100644 index 0000000..3818b4d --- /dev/null +++ b/hy/readline_helpers.py @@ -0,0 +1,65 @@ +# -*- 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 + 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) diff --git a/setup.py b/setup.py index 7fac3f3..38ef5f5 100755 --- a/setup.py +++ b/setup.py @@ -22,16 +22,20 @@ from hy import __appname__, __version__ from setuptools import setup - +import os long_description = """Hy is a Python <--> Lisp layer. It helps make things work nicer, and lets Python and the Hy lisp variant play nice together. """ +install_requires = [] +if os.name == 'nt': + install_requires.append('pyreadline==2.0') setup( name=__appname__, version=__version__, + install_requires=install_requires, scripts=[ "bin/hy", "bin/hyc", From 56ccdf97e3ac0185101ce78b4897eb4a1cc9f7ac Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Thu, 4 Jul 2013 21:31:17 -0400 Subject: [PATCH 2/5] Style fixes. --- hy/cmdline.py | 11 +++++------ hy/completer.py | 1 - hy/readline_helpers.py | 5 ++++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/hy/cmdline.py b/hy/cmdline.py index 2ea7ec7..b3dfe10 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -24,27 +24,26 @@ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -import ast -import code import optparse -import os +import code +import ast import sys import hy +from hy.importer import ast_compile, import_buffer_to_module 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.importer import ast_compile, import_buffer_to_module -import hy.completer from hy.readline_helpers import read_history_file, write_history_file +import hy.completer -from hy.macros import macro, require from hy.models.expression import HyExpression from hy.models.string import HyString from hy.models.symbol import HySymbol +from hy.macros import macro, require _machine = Machine(Idle, 1, 0) diff --git a/hy/completer.py b/hy/completer.py index f88a880..be75cd2 100644 --- a/hy/completer.py +++ b/hy/completer.py @@ -59,4 +59,3 @@ class Completer(object): return None set_completer(Completer().complete, "()[]{} ") - diff --git a/hy/readline_helpers.py b/hy/readline_helpers.py index 3818b4d..dfa03e9 100644 --- a/hy/readline_helpers.py +++ b/hy/readline_helpers.py @@ -35,16 +35,18 @@ try: except ImportError: try: import pyreadline.rlmain - import pyreadline.unicode_helper + 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 @@ -60,6 +62,7 @@ def read_history_file(): readline.parse_and_bind("tab: complete") return history + def write_history_file(history): if not noop: readline.write_history_file(history) From 5554c50671baae50b208b218731984531f428749 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Thu, 4 Jul 2013 22:25:40 -0400 Subject: [PATCH 3/5] Updating NEWS entries. --- NEWS | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/NEWS b/NEWS index 044150d..f5edc74 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,29 @@ +Changes from Hy 0.9.8 + + [ Syntax Fixes ] + + * Macros are now module-specific, and must be required when used. (KH) + * Added a few more string escapes to the compiler (Thomas Ballinger) + * Keywords are pseudo-callable again, to get the value out of a dict. (PT) + * Empty expression is now the same as an empty vector. (Guillermo Vaya) + + [ Language Changes ] + + * HyDicts (quoted dicts or internal HST repr) are now lists + that compiled down to dicts by the Compiler later on. (ND) + * Macros can be constants as well. (KH) + * Add eval-when-compile and eval-and-compile (KH) + * Add break and continue to Hy (Morten Linderud) + * Core language libraries added. As example, I've included `take` and + `drop` in this release. More to come (PT) + + [ Misc. Fixes ] + + * Ensure compiler errors are always "user friendly" (JD) + * Hy REPL quitter repr adjusted to match Hy syntax (Morten Linderud) + * Windows will no longer break due to missing readline (Ralph Moritz) + + Changes from Hy 0.9.7 [ Syntax Fixes ] From 748f54b2f9067d52793a2c63c34b79c1af501fb1 Mon Sep 17 00:00:00 2001 From: Ralph Moritz Date: Sat, 6 Jul 2013 17:43:51 +0200 Subject: [PATCH 4/5] 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) From 2c4c95725e858eea83acb26ae8b3da72ceaf86c4 Mon Sep 17 00:00:00 2001 From: Paul Tagliamonte Date: Sat, 6 Jul 2013 13:43:05 -0400 Subject: [PATCH 5/5] Style fixes. --- hy/cmdline.py | 2 +- hy/completer.py | 36 ++++++++++++++++-------------------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/hy/cmdline.py b/hy/cmdline.py index 12a0503..cc262b1 100644 --- a/hy/cmdline.py +++ b/hy/cmdline.py @@ -188,7 +188,7 @@ def run_repl(hr=None): hr.interact("{appname} {version}".format( appname=hy.__appname__, version=hy.__version__ - )) + )) return 0 diff --git a/hy/completer.py b/hy/completer.py index b0c5018..4a6df63 100644 --- a/hy/completer.py +++ b/hy/completer.py @@ -26,6 +26,7 @@ # DEALINGS IN THE SOFTWARE. import os +from contextlib import contextmanager docomplete = True @@ -79,32 +80,27 @@ class Completer(object): return None -class completion(object): +@contextmanager +def completion(completer=None): delims = "()[]{} " + if not completer: + completer = Completer() - def __init__(self, completer=None): - if not completer: - completer = Completer() - - self.completer = completer + if docomplete: + readline.set_completer(completer.complete) + readline.set_completer_delims(delims) - 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") + history = os.path.expanduser("~/.hy-history") readline.parse_and_bind("set blink-matching-paren on") - + try: - readline.read_history_file(self.history) + readline.read_history_file(history) except IOError: - open(self.history, 'a').close() + open(history, 'a').close() readline.parse_and_bind("tab: complete") - def __exit__(self, type, value, tb): - if docomplete: - readline.write_history_file(self.history) + yield + + if docomplete: + readline.write_history_file(history)