Implemented "write", removed "gate", renamed "edit" to "open"
This commit is contained in:
parent
85f242b2e5
commit
1ca22a14c3
1 changed files with 47 additions and 18 deletions
65
c4ctrl.vim
65
c4ctrl.vim
|
@ -1,10 +1,10 @@
|
||||||
" Vim plugin to use some functionality of c4ctrl from within Vim.
|
" Vim plugin to use some functionality of c4ctrl from within Vim.
|
||||||
"
|
"
|
||||||
" Last Change: 2017 Mar 27
|
" Last Change: 2017 Mar 28
|
||||||
" Maintainer: Shy
|
" Maintainer: Shy
|
||||||
" License: This file is placed in the public domain.
|
" License: This file is placed in the public domain.
|
||||||
"
|
"
|
||||||
" Usage: C4ctrl [edit $name] [gate] [get] [set [w][p][f]] [text]
|
" Usage: C4ctrl [get] [open $preset] [set [w][p][f]] [text] [write]
|
||||||
|
|
||||||
if exists("g:loaded_c4ctrl")
|
if exists("g:loaded_c4ctrl")
|
||||||
finish
|
finish
|
||||||
|
@ -34,6 +34,7 @@ endfunction
|
||||||
function C4ctrl(cmd, ...)
|
function C4ctrl(cmd, ...)
|
||||||
let s:c4ctrl = "c4ctrl"
|
let s:c4ctrl = "c4ctrl"
|
||||||
|
|
||||||
|
" Check if we can excute c4ctrl
|
||||||
if !executable(s:c4ctrl)
|
if !executable(s:c4ctrl)
|
||||||
" Maybe we judt need to add .py to the command?
|
" Maybe we judt need to add .py to the command?
|
||||||
if executable(s:c4ctrl.".py")
|
if executable(s:c4ctrl.".py")
|
||||||
|
@ -44,7 +45,13 @@ function C4ctrl(cmd, ...)
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if stridx("edit", a:cmd) == 0
|
if stridx("get", a:cmd) == 0
|
||||||
|
" Read current status into new buffer
|
||||||
|
new
|
||||||
|
set filetype=conf
|
||||||
|
silent execute "0 read !" s:c4ctrl "-o -"
|
||||||
|
|
||||||
|
elseif stridx("open", a:cmd) == 0
|
||||||
" Edit an exiting preset
|
" Edit an exiting preset
|
||||||
if !exists("a:1")
|
if !exists("a:1")
|
||||||
echo "Missing filename!"
|
echo "Missing filename!"
|
||||||
|
@ -62,16 +69,6 @@ function C4ctrl(cmd, ...)
|
||||||
endif
|
endif
|
||||||
execute "new" fnameescape(s:fn)
|
execute "new" fnameescape(s:fn)
|
||||||
|
|
||||||
elseif stridx("gate", a:cmd) == 0
|
|
||||||
" Open gate
|
|
||||||
silent let s:ret = system(s:c4ctrl." -g")
|
|
||||||
|
|
||||||
elseif stridx("get", a:cmd) == 0
|
|
||||||
" Read current status into new buffer
|
|
||||||
new
|
|
||||||
set filetype=conf
|
|
||||||
silent execute "0 read !" s:c4ctrl "-o -"
|
|
||||||
|
|
||||||
elseif stridx("set", a:cmd) == 0
|
elseif stridx("set", a:cmd) == 0
|
||||||
" Set preset from current buffer
|
" Set preset from current buffer
|
||||||
" Let's start by building a command line
|
" Let's start by building a command line
|
||||||
|
@ -104,11 +101,39 @@ function C4ctrl(cmd, ...)
|
||||||
let s:ret = system(printf("%s -k text,%s", s:c4ctrl, shellescape(s:txt)))
|
let s:ret = system(printf("%s -k text,%s", s:c4ctrl, shellescape(s:txt)))
|
||||||
|
|
||||||
unlet! s:txt
|
unlet! s:txt
|
||||||
|
|
||||||
|
elseif stridx("write", substitute(a:cmd, "!$", "", "")) == 0
|
||||||
|
" Save preset to config directory
|
||||||
|
if !exists("a:1")
|
||||||
|
echo "Missing filename!"
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:cfgdir = s:FindConfigDir()
|
||||||
|
if s:cfgdir == ""
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:fn = s:cfgdir . a:1
|
||||||
|
|
||||||
|
if strridx(a:cmd, "!") + 1 == len(a:cmd)
|
||||||
|
" Force if a ! was appended to the command
|
||||||
|
execute "saveas!" fnameescape(s:fn)
|
||||||
|
else
|
||||||
|
execute "saveas" fnameescape(s:fn)
|
||||||
|
endif
|
||||||
|
|
||||||
|
else
|
||||||
|
" Unknown command oO
|
||||||
|
echo "Unknown command:" a:cmd
|
||||||
|
echo "Valid commands are get, open, set, text and write"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
" Echo return if shell exited with an error
|
" Echo return if shell exited with an error
|
||||||
if v:shell_error
|
if v:shell_error
|
||||||
echoerr s:ret
|
if exists("s:ret")
|
||||||
|
echoerr s:ret
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
unlet! s:c4ctrl s:ret s:cfgdir
|
unlet! s:c4ctrl s:ret s:cfgdir
|
||||||
|
@ -116,23 +141,27 @@ endfunction
|
||||||
|
|
||||||
" Custom command line completion
|
" Custom command line completion
|
||||||
function s:C4ctrlCompletion(ArgLead, CmdLine, CursorPos)
|
function s:C4ctrlCompletion(ArgLead, CmdLine, CursorPos)
|
||||||
if stridx(a:CmdLine, "edit") != -1
|
if stridx(a:CmdLine, "open") != -1
|
||||||
let s:cfgdir = s:FindConfigDir()
|
let s:cfgdir = s:FindConfigDir()
|
||||||
if s:cfgdir == ""
|
if s:cfgdir == ""
|
||||||
return ""
|
return ""
|
||||||
endif
|
endif
|
||||||
return join(map(glob(s:cfgdir."*", 0, 1), "fnamemodify(v:val, ':t')"), "\n")
|
return join(map(glob(s:cfgdir."*", 0, 1), "fnamemodify(v:val, ':t')"), "\n")
|
||||||
elseif stridx(a:CmdLine, "gate") != -1
|
|
||||||
return ""
|
|
||||||
elseif stridx(a:CmdLine, "get") != -1
|
elseif stridx(a:CmdLine, "get") != -1
|
||||||
return ""
|
return ""
|
||||||
elseif stridx(a:CmdLine, "set") != -1
|
elseif stridx(a:CmdLine, "set") != -1
|
||||||
return "w\np\nf"
|
return "w\np\nf"
|
||||||
elseif stridx(a:CmdLine, "text") != -1
|
elseif stridx(a:CmdLine, "text") != -1
|
||||||
return ""
|
return ""
|
||||||
|
elseif stridx(a:CmdLine, "write") != -1
|
||||||
|
let s:cfgdir = s:FindConfigDir()
|
||||||
|
if s:cfgdir == ""
|
||||||
|
return ""
|
||||||
|
endif
|
||||||
|
return join(map(glob(s:cfgdir."*", 0, 1), "fnamemodify(v:val, ':t')"), "\n")
|
||||||
endif
|
endif
|
||||||
|
|
||||||
return "edit\ngate\nget\nset\ntext"
|
return "get\nopen\nset\ntext\nwrite"
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
if !exists(":C4ctrl")
|
if !exists(":C4ctrl")
|
||||||
|
|
Loading…
Reference in a new issue