1
1
mirror of https://github.com/qvacua/vimr.git synced 2025-01-07 06:33:19 +03:00
vimr/VimR/vimr
2016-10-03 14:52:50 -07:00

63 lines
1.8 KiB
Python
Executable File

#!/usr/bin/python
import urllib
import subprocess
import argparse
import os
def call_open(arg, dry_run=False):
if dry_run:
print("/usr/bin/open {0}".format(arg))
else:
subprocess.call(["/usr/bin/open", arg])
def vimr(action, args=None, dry_run=False):
files = args.file
query_params = {
"cwd": args.cwd or os.getcwd()
}
if files:
query_params["file"] = [os.path.abspath(f) for f in files]
url = "vimr://{0}?{1}".format(action, urllib.urlencode(query_params, True).replace('+', '%20'))
call_open(url, dry_run)
class Action:
ACTIVATE = "activate"
OPEN = "open"
NEW_WINDOW = "open-in-new-window"
SEPARATE_WINDOWS = "open-in-separate-windows"
description = """
Open files in VimR: By default all files are open in tabs in the front most window or in a new window if there is none.
The working directory will be set to the current directory.
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument("--dry-run", action="store_true", dest="dry_run", help="Just print the 'open' command.")
parser.add_argument("--cwd", action="store", help="Set the working directory.")
group = parser.add_mutually_exclusive_group()
# no option => Open files in tabs in the front most window.
group.add_argument("-n", action="store_true", dest="new_window", help="Open files in tabs in a new window.")
group.add_argument("-s", action="store_true", dest="separate_windows", help="Open files in separate windows.")
parser.add_argument("file", nargs="*")
args = parser.parse_args()
dry_run = args.dry_run
if not args.file:
action = Action.ACTIVATE
elif args.new_window:
action = Action.NEW_WINDOW
elif args.separate_windows:
action = Action.SEPARATE_WINDOWS
else:
action = Action.OPEN
vimr(action, args, dry_run)