58 lines
1.6 KiB
Python
Executable File
58 lines
1.6 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
"""
|
|
This file will call `ctags` and then `vim` if called without arguments.
|
|
It will call `vim` immediately if called with arguments.
|
|
|
|
Recommend renaming as just `vim` and placing in your path for convenience.
|
|
|
|
Yes I know it would be very easy to implement this as a bash script, but
|
|
this python implementation works too, so whatever.
|
|
"""
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
TAGS_FILE_PATH = os.path.abspath(os.path.join(".", "tags"))
|
|
CTAGS_PATH = "/usr/bin/ctags"
|
|
DOT_CTAGS_PATH = "$HOME/.ctags"
|
|
VIM_PATH = "/usr/bin/vim"
|
|
|
|
# call vim directly if we're opening a specific file
|
|
if len(sys.argv) >= 2:
|
|
vim_args = [VIM_PATH] + sys.argv[1:]
|
|
os.execv(VIM_PATH, vim_args)
|
|
|
|
# call ctags
|
|
try:
|
|
if os.path.exists(TAGS_FILE_PATH):
|
|
os.rename(TAGS_FILE_PATH, TAGS_FILE_PATH + ".bak")
|
|
if os.path.exists(DOT_CTAGS_PATH):
|
|
with open(DOT_CTAGS_PATH, "r") as f:
|
|
ctags_text = f.read()
|
|
else:
|
|
print("Warning: .ctags not found!", file=sys.stderr)
|
|
ctags_text = ""
|
|
ctags_cmd = [CTAGS_PATH]
|
|
for line in ctags_text.split("\n"):
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
ctags_cmd.append(line)
|
|
proc = subprocess.Popen(
|
|
ctags_cmd,
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
)
|
|
stdout, stderr = proc.communicate()
|
|
if stderr:
|
|
print(stderr, file=sys.stderr)
|
|
sys.exit(1)
|
|
except PermissionError:
|
|
pass
|
|
|
|
# call vim- if we get here `sys.argv[1:]` is an empty list
|
|
vim_args = [VIM_PATH] + sys.argv[1:]
|
|
os.execv(VIM_PATH, vim_args)
|