Repository: hylang/hy

Issue:
Make hy-history location configurable
How about an environment variable like `HY_HISTORY` that allows the user to change the location of `~/.hy-history`.

Discussion:
allison-casey: A documentation page for the repl was recently added in #1947. Can you add a short note about this new env var there
Kodiologist: I like the feature. But I don't think a test should touch a file outside the source tree. Maybe just drop `test_history_default_location`.
datschy: > But I don't think a test should touch a file outside the source tree. Maybe just drop `test_history_default_location`.

Yeah, I was a little torn apart because of it myself. Removed the test.

Kodiologist: Inline `is_readline_available` and `skip_no_readline`, then squash everything but the AUTHORS addition, and we should be good to go.

Patch:
diff --git a/AUTHORS b/AUTHORS
index 95565fe3..10476df7 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -105,3 +105,4 @@
 * Joshua Munn <public@elysee-munn.family>
 * Peter Andreev <appa@gmx.co.uk>
 * Sunjay Cauligi <scauligi@eng.ucsd.edu>
+* David Tscheppen <david.tscheppen@gmail.com>
diff --git a/NEWS.rst b/NEWS.rst
index 3631d995..827e6d12 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -11,6 +11,7 @@ Breaking Changes
 New Features
 ------------------------------
 * New contrib module `destructure` for Clojure-style destructuring.
+* Location of history file now configurable via environment variable `HY_HISTORY`
 
 Bug Fixes
 ------------------------------
diff --git a/docs/language/repl.rst b/docs/language/repl.rst
index d90902bb..76f28631 100644
--- a/docs/language/repl.rst
+++ b/docs/language/repl.rst
@@ -62,6 +62,19 @@ Once an exception has been thrown in an interactive session, the most recent exc
   <class 'ZeroDivisionError'>
 
 
+.. _repl-configuration:
+
+Configuration
+-------------
+
+.. _history-location:
+
+Location of history
+^^^^^^^^^^^^^^^^^^^
+
+The default location for the history of REPL inputs is ``~/.hy-history``. This can be changed by setting the environment variable ``HY_HISTORY`` to your preferred location. For example, if you are using Bash, it can be set with ``export HY_HISTORY=/path/to/my/.custom-hy-history``.
+
+
 --------
 
 .. [#] https://en.wikipedia.org/wiki/Read-eval-print_loop
diff --git a/hy/completer.py b/hy/completer.py
index e29a3176..b492d4c5 100644
--- a/hy/completer.py
+++ b/hy/completer.py
@@ -117,7 +117,8 @@ def completion(completer=None):
         readline.set_completer(completer.complete)
         readline.set_completer_delims(delims)
 
-        history = os.path.expanduser("~/.hy-history")
+        history = os.environ.get(
+            "HY_HISTORY", os.path.expanduser("~/.hy-history"))
         readline.parse_and_bind("set blink-matching-paren on")
 
         try:


Test patch:
diff --git a/tests/test_completer.py b/tests/test_completer.py
new file mode 100644
index 00000000..54970b5a
--- /dev/null
+++ b/tests/test_completer.py
@@ -0,0 +1,26 @@
+import os
+import sys
+from hy.completer import completion
+import pytest
+import tempfile
+
+
+@pytest.mark.skipif(
+    "readline" not in sys.modules,
+    reason="Module 'readline' is not available.")
+def test_history_custom_location():
+    import readline
+
+    expected_entry = '(print "Hy, custom history file!")'
+
+    with tempfile.TemporaryDirectory() as tmp:
+        history_location = tmp + os.sep + ".hy-custom-history"
+        os.environ["HY_HISTORY"] = history_location
+
+        with completion():
+            readline.clear_history()
+            readline.add_history(expected_entry)
+
+        with open(history_location, "r") as hf:
+            actual_entry = hf.readline()
+            assert expected_entry in actual_entry