mirror of
https://github.com/qvacua/vimr.git
synced 2024-12-29 16:56:40 +03:00
71 lines
1.9 KiB
Python
Executable File
71 lines
1.9 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 abspath(path):
|
|
return os.path.abspath(os.path.expanduser(path))
|
|
|
|
|
|
def vimr(action, args=None, dry_run=False):
|
|
files = args.file
|
|
|
|
cwd = os.getcwd()
|
|
if args.cwd is not None:
|
|
cwd = abspath(args.cwd)
|
|
|
|
query_params = {
|
|
"cwd": cwd
|
|
}
|
|
|
|
if files:
|
|
query_params["file"] = [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)
|