-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtextedit.py
More file actions
executable file
·98 lines (91 loc) · 2.88 KB
/
textedit.py
File metadata and controls
executable file
·98 lines (91 loc) · 2.88 KB
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
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
import os
import subprocess
import shlex
import io
import urllib.parse
from configparser import ConfigParser
defaults = """
[editor]
editor = gvim
command = --remote +:{line}:normal{start} "{file}"
[script]
verbose = true
focus = false
"""
def parseurl(url):
if ":" not in url:
raise ValueError("Could not parse url {0}".format(url))
parts = url.split(":")
if len(parts) != 5:
raise ValueError("Could not parse url {0}".format(url))
fileposition = {
"file": parts[1].replace("///", "/"),
"line": int(parts[2]),
"start": int(parts[3]),
"end": int(parts[4])
}
fileposition["file"] = urllib.parse.unquote(fileposition["file"])
fileposition["exists"] = os.path.isfile(fileposition["file"])
return fileposition
def invokeeditor(fileposition, config, logfile):
cmd = [config.get("editor", "editor")]
if "vim" in cmd[0]:
fileposition["start"] += 1
if fileposition:
position = config.get("editor", "command").format(**fileposition)
cmd += shlex.split(position)
if logfile:
logfile.write("running editor...\n")
logfile.write(repr(cmd) + "\n")
subprocess.call(cmd)
if config.getboolean("script", "focus"):
import time
filename = fileposition["file"].split("/")[-1]
focus = ["xdotool", "search", "--name", filename, "windowactivate"]
if logfile:
logfile.write("focussing editor...\n")
logfile.write(repr(focus) + "\n")
subprocess.call(focus)
time.sleep(0.5)
subprocess.call(focus)
def getconfig():
config = ConfigParser()
config.read_string(defaults)
configfilename = os.path.expanduser("~/.config/lytextedit.cfg")
filesread = config.read(configfilename)
if configfilename not in filesread:
# write default config
with open(configfilename, "w") as configfile:
config.write(configfile)
return config
if __name__ == "__main__":
cmdline = sys.argv
try:
url = sys.argv[1]
except IndexError:
url = None
config = getconfig()
if config.getboolean("script", "verbose"):
logfile = open("/tmp/lytextedit.log", "a")
logfile.write("configuration read...\n")
else:
logfile = False
if url:
fileposition = parseurl(url)
if logfile:
logfile.write("url {0} parsed...\n".format(url))
logfile.write(repr(fileposition) + "\n")
if not fileposition["exists"]:
if logfile:
logfile.write("could not find file {0}\n".format(fileposition["file"]))
fileposition = False
else:
fileposition = False
if logfile:
logfile.write("no url...\n")
invokeeditor(fileposition, config, logfile)
if logfile:
logfile.close()