From bfe3ef4f3411971eb6304539b3f2e96803036807 Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Fri, 3 May 2019 07:35:29 +0200
Subject: [PATCH 1/7] add startup script for ipython and python repl

---
 scripts/nvim_startup.py | 65 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100755 scripts/nvim_startup.py

diff --git a/scripts/nvim_startup.py b/scripts/nvim_startup.py
new file mode 100755
index 00000000..02b15d6c
--- /dev/null
+++ b/scripts/nvim_startup.py
@@ -0,0 +1,65 @@
+""" Startup script for IPython that will connect to a running NeoVim instance
+automatically or ask the user which NeoVim instance to connect to.
+After connection there is a variable called "nvim" that gives acccess to the
+endpoint.
+"""
+import atexit
+from glob import glob
+import pynvim
+
+
+class NVimAutoAttachException(Exception):
+    '''Exception class that will be raised by this script'''
+
+
+def __let_user_choose_instance(nvim_instances):
+    for idx, inst in enumerate(nvim_instances):
+        with pynvim.attach('socket', path=inst) as nvim_endpoint:
+            nvim_endpoint.command(
+                f'echon "I am Instance Nr {idx}: " $NVIM_LISTEN_ADDRESS'
+            )
+
+    for idx, inst in enumerate(nvim_instances):
+        print(f'Instance Nr {idx}: {inst}')
+
+    while True:
+        try:
+            print(
+                'Which Nvim instance should I connect to? '
+                '(Look at the bottom of the vim instance you want to connect '
+                'to)'
+            )
+            inst_nr = int(input('Connect to Instance Nr: '))
+            chosen_inst = nvim_instances[inst_nr]
+            break
+        except (ValueError, IndexError):
+            continue
+
+    return chosen_inst
+
+
+def tell_nvim_that_python_exited():
+    'will inform Nvim about Python exit'
+    nvim.command('echo "IPython disconnected."')
+
+
+def autoattach_to_nvim(glob_expr='/tmp/nvim*/0'):
+    '''Returns a nvim endpoint if there is only one instance.
+    If there are more instances, asks the user which one to pick.
+    Raises NVimAutoAttachException if there are no NVim instances.
+    '''
+    nvim_instances = glob(glob_expr)
+    if not nvim_instances:
+        raise NVimAutoAttachException(
+            'Could not find any running NVim instances.'
+        )
+    if len(nvim_instances) > 1:
+        chosen_inst = __let_user_choose_instance(nvim_instances)
+    else:
+        chosen_inst = nvim_instances[0]
+    return pynvim.attach('socket', path=chosen_inst)
+
+
+nvim = autoattach_to_nvim()
+nvim.command('echo "Connected to IPython."')
+atexit.register(tell_nvim_that_python_exited)

From b7cc64195483d7b605eca5d8fd8fcea778754cb0 Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Fri, 3 May 2019 07:50:50 +0200
Subject: [PATCH 2/7] make nvim_startup.py Python2 compatible and Update
 connection messages

---
 scripts/nvim_startup.py | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/scripts/nvim_startup.py b/scripts/nvim_startup.py
index 02b15d6c..61136c0c 100755
--- a/scripts/nvim_startup.py
+++ b/scripts/nvim_startup.py
@@ -16,11 +16,13 @@ def __let_user_choose_instance(nvim_instances):
     for idx, inst in enumerate(nvim_instances):
         with pynvim.attach('socket', path=inst) as nvim_endpoint:
             nvim_endpoint.command(
-                f'echon "I am Instance Nr {idx}: " $NVIM_LISTEN_ADDRESS'
+                'echon "I am Instance Nr {}: " $NVIM_LISTEN_ADDRESS'.format(
+                    idx
+                )
             )
 
     for idx, inst in enumerate(nvim_instances):
-        print(f'Instance Nr {idx}: {inst}')
+        print('Instance Nr {}: {}'.format(idx, inst))
 
     while True:
         try:
@@ -38,11 +40,6 @@ def __let_user_choose_instance(nvim_instances):
     return chosen_inst
 
 
-def tell_nvim_that_python_exited():
-    'will inform Nvim about Python exit'
-    nvim.command('echo "IPython disconnected."')
-
-
 def autoattach_to_nvim(glob_expr='/tmp/nvim*/0'):
     '''Returns a nvim endpoint if there is only one instance.
     If there are more instances, asks the user which one to pick.
@@ -61,5 +58,5 @@ def autoattach_to_nvim(glob_expr='/tmp/nvim*/0'):
 
 
 nvim = autoattach_to_nvim()
-nvim.command('echo "Connected to IPython."')
-atexit.register(tell_nvim_that_python_exited)
+nvim.command('echo "Connected to Python REPL."')
+atexit.register(lambda: nvim.command('echo "Python REPL disconnected."'))

From b66e967b4594fabf0d8d64b2faf4bc07cee00160 Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Fri, 3 May 2019 07:59:50 +0200
Subject: [PATCH 3/7] update docstring of nvim_startup.py

---
 scripts/nvim_startup.py | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/scripts/nvim_startup.py b/scripts/nvim_startup.py
index 61136c0c..0cea6c3a 100755
--- a/scripts/nvim_startup.py
+++ b/scripts/nvim_startup.py
@@ -1,7 +1,7 @@
-""" Startup script for IPython that will connect to a running NeoVim instance
-automatically or ask the user which NeoVim instance to connect to.
-After connection there is a variable called "nvim" that gives acccess to the
-endpoint.
+""" Startup script for IPython an Python REPL that will connect to a running 
+NeoVim instance automatically or ask the user which NeoVim instance to connect 
+to. After connection there is a variable called "nvim" that gives acccess to 
+the endpoint.
 """
 import atexit
 from glob import glob

From df6478d94b0d70a5f4cee16d977260799aa3c66d Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Fri, 3 May 2019 12:54:26 +0200
Subject: [PATCH 4/7] update README.md

---
 README.md | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 7e3605ab..5bb25bb1 100644
--- a/README.md
+++ b/README.md
@@ -74,7 +74,24 @@ For details about testing and troubleshooting, see the
 [development](http://pynvim.readthedocs.io/en/latest/development.html)
 documentation.
 
-#### Usage through the python REPL
+#### Autoconnect in Python/IPython REPL
+
+You can explore and use pynvim through IPython. For easy connection use the
+startup script in scripts/nvim_startup.py like so:
+```bash
+$ ipython -i scripts/nvim_startup.py
+# or if you prefer the Python REPL instead of IPython
+$ python -i scripts/nvim_startup.py
+```
+
+This will leave you with an Python/IPython Session that has a variable named "nvim" which 
+represents a connection to your current NVim instance.
+If you have more than one instance running, the script will ask at startup to which
+instance it shall connect to. Afterwards see [this section](README.md#usage-through-the-python-repl)
+for examples on how to use the API.
+
+
+#### Manual usage through the python REPL
 
 A number of different transports are supported, but the simplest way to get
 started is with the python REPL. First, start Nvim with a known address (or use

From 15b8ee6fea3a5c51b52a79bd63d1f2d24eb65ed6 Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Fri, 3 May 2019 12:57:41 +0200
Subject: [PATCH 5/7] fix section link in README.md

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 5bb25bb1..de38acf3 100644
--- a/README.md
+++ b/README.md
@@ -87,7 +87,7 @@ $ python -i scripts/nvim_startup.py
 This will leave you with an Python/IPython Session that has a variable named "nvim" which 
 represents a connection to your current NVim instance.
 If you have more than one instance running, the script will ask at startup to which
-instance it shall connect to. Afterwards see [this section](README.md#usage-through-the-python-repl)
+instance it shall connect to. Afterwards see [the next section](README.md#manual-usage-through-the-python-repl)
 for examples on how to use the API.
 
 

From 8defc0240803f0b30f9ed7af18325e3643995814 Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Fri, 3 May 2019 12:59:30 +0200
Subject: [PATCH 6/7] update README.md

---
 README.md | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/README.md b/README.md
index de38acf3..0f81f14b 100644
--- a/README.md
+++ b/README.md
@@ -76,7 +76,7 @@ documentation.
 
 #### Autoconnect in Python/IPython REPL
 
-You can explore and use pynvim through IPython. For easy connection use the
+You can explore and use pynvim through Python/IPython REPL. For easy connection use the
 startup script in scripts/nvim_startup.py like so:
 ```bash
 $ ipython -i scripts/nvim_startup.py

From 7307d96fb5ea18c1c067897723f093587407915d Mon Sep 17 00:00:00 2001
From: Salo <christopher@saloman.de>
Date: Mon, 6 May 2019 02:19:53 +0200
Subject: [PATCH 7/7] rename NVimAutoAttachException to NvimAutoAttachException

---
 scripts/nvim_startup.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/scripts/nvim_startup.py b/scripts/nvim_startup.py
index 0cea6c3a..1e85bd18 100755
--- a/scripts/nvim_startup.py
+++ b/scripts/nvim_startup.py
@@ -8,7 +8,7 @@
 import pynvim
 
 
-class NVimAutoAttachException(Exception):
+class NvimAutoAttachException(Exception):
     '''Exception class that will be raised by this script'''
 
 
@@ -43,11 +43,11 @@ def __let_user_choose_instance(nvim_instances):
 def autoattach_to_nvim(glob_expr='/tmp/nvim*/0'):
     '''Returns a nvim endpoint if there is only one instance.
     If there are more instances, asks the user which one to pick.
-    Raises NVimAutoAttachException if there are no NVim instances.
+    Raises NvimAutoAttachException if there are no NVim instances.
     '''
     nvim_instances = glob(glob_expr)
     if not nvim_instances:
-        raise NVimAutoAttachException(
+        raise NvimAutoAttachException(
             'Could not find any running NVim instances.'
         )
     if len(nvim_instances) > 1: