aboutsummarylogtreecommitdiffstats
path: root/nvim-wrapper
blob: 29d92cb4928a44bf929ed63031f66a78982222b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3

"""Run nvim inside gnome-terminal"""

import sys
import os
import subprocess
import dbus
from time import time


APP_ID = 'org.neovim'
CLASS = 'neovim'
NAME = 'Neovim'

def find_terminal_server():
    candidates = list(filter(os.path.exists, \
                            ('/usr/lib/gnome-terminal/gnome-terminal-server', # arch et al
                             '/usr/lib/gnome-terminal-server',
                             '/usr/libexec/gnome-terminal-server')))          # fedora 22
    if len(candidates) > 0:
        return candidates[0]
    else:
        print("nvim-wrapper needs gnome-terminal-server, but it wasn't found.")
        sys.exit()

SERVER_CMD = [
    find_terminal_server(),
    '--app-id', APP_ID,
    '--class', CLASS,
    '--name', NAME,
]
TERM_CMD = [
    'gnome-terminal',
    '--app-id', APP_ID,
    '--class', CLASS,
    '--name', NAME,
    '--hide-menubar',
    '-x',
    'nvim',
]
GTERM_PASSTHROUGH_OPTIONS = [
    '--full-screen',
    '--maximize',
    '--profile',
    '--geometry',
    '--working-directory',
    '--display'
]

def processArgv():
    argv = sys.argv[1:]
    gtermOptions = []
    nvimOptions = []

    for arg in argv:
        argParts = arg.split('=', 1)
        if argParts[0] in GTERM_PASSTHROUGH_OPTIONS:
            gtermOptions.append(arg)
        elif arg=='-f':
            gtermOptions.append('--wait')
        else:
            nvimOptions.append(arg)

    # launch the wrapper in the current directory by default
    if not any([o.startswith('--working-directory') for o in gtermOptions]):
        gtermOptions.append('--working-directory='+os.path.abspath(os.curdir))

    # enable title by default
    nvimOptions.append("+set title")

    # enable 24-bit color
    nvimOptions.append("+set termguicolors")

    return {
        'gterm': gtermOptions,
        'nvim': nvimOptions
    }

def main():
    """Run nvim inside gnome-terminal"""
    session_bus = dbus.SessionBus()

    # launch the terminal server with a custom app-id
    # and window class (so the .desktop file gets associated)
    if not session_bus.name_has_owner(APP_ID):
        subprocess.Popen(SERVER_CMD)

    # wait until the name is registered, or 2 seconds pass (when launching from
    # cold cache it might take more time)
    timeout = time() + 2
    while not session_bus.name_has_owner(APP_ID) and time() <= timeout:
        pass
    # launch nvim in a gnome-terminal instance
    if session_bus.name_has_owner(APP_ID):
        options = processArgv()
        cmd = [] + TERM_CMD[:-2] + options['gterm'] + TERM_CMD[-2:] + options['nvim']
        with open(os.devnull, 'wb') as fnull:
            p=subprocess.Popen(cmd,
                             stdout=fnull,
                             stderr=fnull)
        if '--wait' in options['gterm']:
            p.wait()

if __name__ == '__main__':
    main()