|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Helper script for the feature provided by the IncludeEditLink setting.""" |
| 4 | + |
| 5 | +import os |
| 6 | +import sys |
| 7 | +from email import message_from_file |
| 8 | +from subprocess import run |
| 9 | + |
| 10 | +editor = 'Vim' # your favorite editor |
| 11 | + |
| 12 | +defaultArgs = [editor, ' +{line}', '{filename}'] |
| 13 | + |
| 14 | +# add an entry for your favorite editor here if it does not already exist |
| 15 | +editorArgs = { |
| 16 | + 'Emacs': |
| 17 | + ['gnuclient', '+{line}', '{filename}'], |
| 18 | + 'Geany': |
| 19 | + ['geany', '-l', '{line}', '{filename}'], |
| 20 | + 'Geany (Windows)': |
| 21 | + ['C:/Program Files (x86)/Geany/bin/geany.exe', |
| 22 | + '-l', '{line}', '{filename}'], |
| 23 | + 'gedit': |
| 24 | + ['gedit', '+{line}', '{filename}'], |
| 25 | + 'jEdit': |
| 26 | + ['jedit', '{filename}', '+line:{line}'], |
| 27 | + 'jEdit (Windows)': |
| 28 | + ['C:/Program Files/jEdit/jedit.exe', '{filename}', '+line:{line}'], |
| 29 | + 'Kate': |
| 30 | + ['kate', '-u', '-l', '{line}', '{filename}'], |
| 31 | + 'Komodo': |
| 32 | + ['komodo', '-l', '{line}', '{filename}'], |
| 33 | + 'KWrite': |
| 34 | + ['kwrite', '--line', '{line}', '{filename}'], |
| 35 | + 'Notepad++ (Windows)': |
| 36 | + ['C:/Program Files/Notepad++/notepad++.exe', '-n{line}', '{filename}'], |
| 37 | + 'PSPad (Windows)': |
| 38 | + ['C:/Program Files (x86)/PSPad editor/PSPad.exe', |
| 39 | + '-{line}', '{filename}'], |
| 40 | + 'SciTE': |
| 41 | + ['scite', '{filename}', '-goto:{line}'], |
| 42 | + 'SciTE (Windows)': |
| 43 | + ['C:/wscite/SciTE.exe', '{filename}', '-goto:{line}'], |
| 44 | + 'Sublime Text (Windows)': |
| 45 | + ['C:/Program Files/Sublime Text 3/subl.exe', '{filename}:{line}'], |
| 46 | + 'Vim': |
| 47 | + ['gvim', '+{line}', '{filename}'], |
| 48 | +} |
| 49 | + |
| 50 | + |
| 51 | +def transform(params): |
| 52 | + """Transform EditFile parameters. |
| 53 | +
|
| 54 | + As an example, if you are under Windows and your edit file |
| 55 | + has a Unix filename, then it is transformed to a UNC path. |
| 56 | + """ |
| 57 | + filename = params['filename'] |
| 58 | + if os.sep == '\\' and filename.startswith('/'): |
| 59 | + filename = os.path.normpath(filename[1:]) |
| 60 | + hostname = params['hostname'].split(':', 1)[0] |
| 61 | + smbPath = fr'\\{hostname}\root' |
| 62 | + filename = os.path.join(smbPath, filename) |
| 63 | + params['filename'] = filename |
| 64 | + return params |
| 65 | + |
| 66 | + |
| 67 | +def openFile(params): |
| 68 | + """Open editor with file specified in parameters.""" |
| 69 | + params = {key.lower(): value for key, value in params.items()} |
| 70 | + params = transform(params) |
| 71 | + args = editorArgs.get(editor, defaultArgs) |
| 72 | + args[1:] = [arg.format(**params) for arg in args[1:]] |
| 73 | + print(' '.join(args)) |
| 74 | + run(args) |
| 75 | + |
| 76 | + |
| 77 | +def parseFile(filename): |
| 78 | + """Parse the Webware EditFile.""" |
| 79 | + openFile(message_from_file(open(filename))) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + parseFile(sys.argv[1]) |
0 commit comments