Kitchentext reworked and fold marks added
This commit is contained in:
parent
104e6d8dab
commit
44ae580dbf
1 changed files with 162 additions and 99 deletions
261
c4ctrl.vim
261
c4ctrl.vim
|
@ -1,12 +1,12 @@
|
||||||
" c4ctrl.vim: This Vim plugin makes some functionality of the c4ctrl command
|
" c4ctrl.vim: This Vim plugin makes some functionality of the c4ctrl command
|
||||||
" line utility available from within Vim.
|
" line utility available from within Vim.
|
||||||
"
|
"
|
||||||
" Last Change: 2017 Apr 13
|
" Last Change: 2017 Apr 16
|
||||||
" Maintainer: Shy
|
" Maintainer: Shy
|
||||||
" License: This file is placed in the public domain.
|
" License: This file is placed in the public domain.
|
||||||
"
|
"
|
||||||
" Usage: C4ctrl [get | kitchentext | open PRESET | set [w] [p] [f] [-magic] |
|
" Usage: C4ctrl [get | kitchentext [REGISTER] | open PRESET |
|
||||||
" write PRESET]
|
" set [w] [p] [f] [-magic] | write PRESET]
|
||||||
|
|
||||||
if exists("g:loaded_c4ctrl")
|
if exists("g:loaded_c4ctrl")
|
||||||
finish
|
finish
|
||||||
|
@ -14,11 +14,11 @@ endif
|
||||||
let g:loaded_c4ctrl = 1
|
let g:loaded_c4ctrl = 1
|
||||||
|
|
||||||
|
|
||||||
function s:FindConfigDir()
|
" ************************************************************************** "
|
||||||
" ************************************************************************ "
|
" Utility function: Find the path to the c4ctrl configuration directory and "
|
||||||
" Returns the path of c4ctrl configuration directory as string, "
|
" return it as string. "
|
||||||
" eg. '/home/somepony/.config/c4ctrl/'. "
|
" ************************************************************************** "
|
||||||
" ************************************************************************ "
|
function s:FindConfigDir() " {{{1
|
||||||
|
|
||||||
" Return early if we already know it from an earlier invocation.
|
" Return early if we already know it from an earlier invocation.
|
||||||
if exists("s:config_dir")
|
if exists("s:config_dir")
|
||||||
|
@ -32,38 +32,51 @@ function s:FindConfigDir()
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if !isdirectory(s:config_dir)
|
if !isdirectory(s:config_dir)
|
||||||
redraw | echohl WarningMsg
|
redraw
|
||||||
echo "Error: could not access config directory:" s:config_dir."!"
|
echohl WarningMsg
|
||||||
|
echo "Error: could not access config directory: ".s:config_dir."!"
|
||||||
echohl None
|
echohl None
|
||||||
return ""
|
return ""
|
||||||
endif
|
endif
|
||||||
return s:config_dir
|
return s:config_dir
|
||||||
endfunction
|
endfunction " }}}1
|
||||||
|
|
||||||
|
|
||||||
function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) range
|
" ************************************************************************** "
|
||||||
" ************************************************************************ "
|
" Make some functionality of the 'c4ctrl' command line utility available "
|
||||||
" Make some functionality of the 'c4ctrl' command line utility available "
|
" from within Vim. "
|
||||||
" from within Vim. "
|
" Available commands are 'get', 'kitchentext', 'open', 'set' and 'write'. "
|
||||||
" Available commands are 'get', 'kitchentext', 'open', 'set' and 'write'. "
|
" Arguments: "
|
||||||
" Arguments: "
|
" prev_cursor_pos -- cursor position as returned by getcurpos() "
|
||||||
" prev_cursor_pos -- cursor position as returned by getcurpos() "
|
" mods -- modifiers (:command variable <f-mods>) "
|
||||||
" mods -- modifiers (:command variable <f-mods>) "
|
" first_line -- first line of range (:command <line1>) "
|
||||||
" first_line -- first line of range (:command <line1>) "
|
" last_line -- last line of range (:command <line2>) "
|
||||||
" last_line -- last line of range (:command <line2>) "
|
" command -- user command ('get', 'set' etc.) "
|
||||||
" command -- user command ('get', 'set' etc.) "
|
" [...] -- optional command options "
|
||||||
" [...] -- optional command options "
|
" ************************************************************************** "
|
||||||
" ************************************************************************ "
|
function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) range " {{{1
|
||||||
|
|
||||||
|
try " We utilize the finally section to clean up the environment later on.
|
||||||
|
|
||||||
try " We utilize the finally section to delete some variables and functions.
|
|
||||||
" Name of the executable.
|
" Name of the executable.
|
||||||
let s:c4ctrl = "c4ctrl"
|
let s:c4ctrl = "c4ctrl"
|
||||||
|
|
||||||
function! s:SynHighlight()
|
" ********************************************************************** "
|
||||||
" ******************************************************************** "
|
" Utility function to print out a warning message. "
|
||||||
" This function will be called after a preset has been loaded into the "
|
" ********************************************************************** "
|
||||||
" buffer. It will be deleted at the end of C4ctrl(). "
|
function! s:Warn(message) " {{{2
|
||||||
" ******************************************************************** "
|
|
||||||
|
redraw
|
||||||
|
echohl WarningMsg
|
||||||
|
echo a:message
|
||||||
|
echohl None
|
||||||
|
|
||||||
|
endfunction " }}}2
|
||||||
|
|
||||||
|
" ********************************************************************** "
|
||||||
|
" Utility function to be called after a preset has been loaded. "
|
||||||
|
" ********************************************************************** "
|
||||||
|
function! s:SynHighlight() " {{{2
|
||||||
|
|
||||||
syn clear
|
syn clear
|
||||||
" Match topics
|
" Match topics
|
||||||
|
@ -83,64 +96,110 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang
|
||||||
" Move the cursor somewhere more practical.
|
" Move the cursor somewhere more practical.
|
||||||
call cursor(1,1)
|
call cursor(1,1)
|
||||||
call search("^[^#].*=[ \t]*[0-9a-fA-F]", 'eW')
|
call search("^[^#].*=[ \t]*[0-9a-fA-F]", 'eW')
|
||||||
endfunction
|
|
||||||
|
|
||||||
" Check if we can excute c4ctrl or c4ctrl.py and modify the variable
|
endfunction " }}}2
|
||||||
" s:c4ctrl accordingly.
|
|
||||||
if !executable(s:c4ctrl)
|
" Check if we can execute c4ctrl or c4ctrl.py and modify the variable
|
||||||
|
" s:c4ctrl accordingly if needed.
|
||||||
|
if !executable(s:c4ctrl) " {{{2
|
||||||
" Maybe we just need to add .py to the command?
|
" Maybe we just need to add .py to the command?
|
||||||
if executable(s:c4ctrl.".py")
|
if executable(s:c4ctrl.".py")
|
||||||
let s:c4ctrl .= ".py"
|
let s:c4ctrl .= ".py"
|
||||||
else
|
else
|
||||||
redraw | echohl WarningMsg
|
call s:Warn("Executable not found! Please put \"".s:c4ctrl."\" into your $PATH.")
|
||||||
echo "Executable not found! Please put \"".s:c4ctrl."\" into your $PATH."
|
|
||||||
echohl None
|
|
||||||
unlet s:c4ctrl
|
unlet s:c4ctrl
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
endif
|
endif " }}}2
|
||||||
|
|
||||||
|
" *************************************************** "
|
||||||
|
" Command 'get': Read current status into new buffer. "
|
||||||
|
" *************************************************** "
|
||||||
|
if stridx("get", a:command) == 0 " {{{2
|
||||||
|
|
||||||
if stridx("get", a:command) == 0
|
|
||||||
" *********************************** "
|
|
||||||
" Read current status into new buffer "
|
|
||||||
" *********************************** "
|
|
||||||
execute a:mods "new"
|
execute a:mods "new"
|
||||||
silent execute "0 read !" s:c4ctrl "-o -"
|
silent execute "0 read !" s:c4ctrl "-o -"
|
||||||
if v:shell_error == 0
|
if v:shell_error == 0
|
||||||
call s:SynHighlight()
|
call s:SynHighlight()
|
||||||
set nomodified " Mark as unmodified.
|
set nomodified " Mark as unmodified.
|
||||||
else
|
else
|
||||||
redraw | echohl WarningMsg
|
call s:Warn(printf("Error: %s returned exit code %d!", s:c4ctrl, v:shell_error))
|
||||||
echo printf("Error: %s returned exit code %d!", s:c4ctrl, v:shell_error)
|
|
||||||
echohl None
|
|
||||||
endif
|
endif
|
||||||
|
" }}}2
|
||||||
|
|
||||||
elseif stridx("kitchentext", a:command) == 0
|
" ***************************************************************** "
|
||||||
" ************************************** "
|
" Command 'kitchentext': Send given reister or text in range to the "
|
||||||
" Send text in range to the Kitchenlight "
|
" Kitchenlight. "
|
||||||
" ************************************** "
|
" ***************************************************************** "
|
||||||
let kitchentext = "kitchentext"
|
elseif stridx("kitchentext", a:command) == 0 " {{{2
|
||||||
|
|
||||||
|
let kitchentext = 'kitchentext'
|
||||||
if !executable(kitchentext)
|
if !executable(kitchentext)
|
||||||
redraw | echohl WarningMsg
|
call s:Warn('Executable not found! Please put "'.kitchentext.'" into your $PATH.')
|
||||||
echo "Executable not found! Please put \"".kitchentext."\" into your $PATH."
|
|
||||||
echohl None
|
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let command_line = "kitchentext -f -d 150 -r -p"
|
let command_line = 'kitchentext -f -d 150 -r -p'
|
||||||
" Strip leading white spaces.
|
if exists('a:1')
|
||||||
let text = getline(a:first_line, a:last_line)
|
" Use text from given register.
|
||||||
call map(text, "substitute(v:val, '^[ \t]*', '', '')")
|
let text = getreg(a:1, 0, 1)
|
||||||
let ret = system(command_line, text)
|
if text == []
|
||||||
|
call s:Warn('Warning: register "'.a:1.'" is empty!")
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
else
|
||||||
|
" Use text in range.
|
||||||
|
let text = getline(a:first_line, a:last_line)
|
||||||
|
|
||||||
|
" Check if user marked a substring using visual selection.
|
||||||
|
" Note: stridx() returns '0' when the second parameter evaluates to an
|
||||||
|
" empty string. Thus the leading space in the first parameter.
|
||||||
|
if stridx(' v', visualmode()) > 0 && stridx(histget('', -1), "'<,'>") != -1
|
||||||
|
let visual_start = getpos("'<")
|
||||||
|
let visual_end = getpos("'>")
|
||||||
|
|
||||||
|
" Better safe than sorry: lets check if the last visual selection
|
||||||
|
" starts and ends on the same lines as the range we were given.
|
||||||
|
if visual_start[1] == a:first_line && visual_end[1] == a:last_line
|
||||||
|
if visualmode() == 'v'
|
||||||
|
" Beware: text[0] and text[-1] may be the same line of text.
|
||||||
|
" Thus this somewhat counter-intuitive order.
|
||||||
|
let text[-1] = strpart(text[-1], 0, visual_end[2])
|
||||||
|
let text[0] = strpart(text[0], (visual_start[2] - 1))
|
||||||
|
else " Box selection.
|
||||||
|
call map(text, 'strpart(v:val, 0, '.visual_end[2].')')
|
||||||
|
call map(text, 'strpart(v:val, '.(visual_start[2] -1).')')
|
||||||
|
endif
|
||||||
|
endif " visual_start[1] ...
|
||||||
|
|
||||||
|
else " stridx(' v', ...
|
||||||
|
" No visual selection then.
|
||||||
|
" Let's warn the user if she's about to put the whole buffer on the
|
||||||
|
" Kitchenlight.
|
||||||
|
if a:first_line != a:last_line && a:first_line == 1 && a:last_line == line('$')
|
||||||
|
let responce = input('Really send the whole buffer to the Kitchenlight? [y/N]: ')
|
||||||
|
if responce != 'y' && responce != 'Y'
|
||||||
|
redraw
|
||||||
|
echo 'Canceled.'
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
endif " a:firt_line != a:last_line ...
|
||||||
|
|
||||||
|
endif " stridx(' v', ...
|
||||||
|
endif " exists('a:1')
|
||||||
|
|
||||||
|
" Strip any leading white spaces.
|
||||||
|
call map(text, 'substitute(v:val, "^[ \t]*", "", "")')
|
||||||
|
let ret = system(command_line, text)
|
||||||
|
" }}}2
|
||||||
|
|
||||||
|
" ******************************************************* "
|
||||||
|
" Command 'open': Load an exiting preset into the buffer. "
|
||||||
|
" ******************************************************* "
|
||||||
|
elseif stridx("open", a:command) == 0 " {{{2
|
||||||
|
|
||||||
elseif stridx("open", a:command) == 0
|
|
||||||
" ********************** "
|
|
||||||
" Edit an exiting preset "
|
|
||||||
" ********************** "
|
|
||||||
if !exists("a:1")
|
if !exists("a:1")
|
||||||
redraw | echohl WarningMsg
|
call s:Warn("Missing filename!")
|
||||||
echo "Missing filename!"
|
|
||||||
echohl None
|
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
@ -150,20 +209,20 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang
|
||||||
endif
|
endif
|
||||||
let filename = s:config_dir . a:1
|
let filename = s:config_dir . a:1
|
||||||
if !filereadable(filename)
|
if !filereadable(filename)
|
||||||
redraw | echohl WarningMsg
|
call s:Warn("Error: could not open file ".filename)
|
||||||
echo "Error: could not open file" filename
|
|
||||||
echohl None
|
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
execute a:mods "new"
|
execute a:mods "new"
|
||||||
execute "edit" fnameescape(filename)
|
execute "edit" fnameescape(filename)
|
||||||
call s:SynHighlight()
|
call s:SynHighlight()
|
||||||
|
" }}}2
|
||||||
|
|
||||||
|
" *********************************************** "
|
||||||
|
" Command 'set': Apply range or buffer as preset. "
|
||||||
|
" *********************************************** "
|
||||||
|
elseif stridx("set", a:command) == 0 " {{{2
|
||||||
|
|
||||||
elseif stridx("set", a:command) == 0
|
|
||||||
" ****************************** "
|
|
||||||
" Set preset from current buffer "
|
|
||||||
" ****************************** "
|
|
||||||
" Let's start by building a command line.
|
" Let's start by building a command line.
|
||||||
let command_line = s:c4ctrl
|
let command_line = s:c4ctrl
|
||||||
let rooms_given = 0
|
let rooms_given = 0
|
||||||
|
@ -190,15 +249,15 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang
|
||||||
|
|
||||||
" Restore cursor position.
|
" Restore cursor position.
|
||||||
call setpos('.', a:prev_cursor_pos)
|
call setpos('.', a:prev_cursor_pos)
|
||||||
|
" }}}2
|
||||||
|
|
||||||
|
" ************************************************************* "
|
||||||
|
" Command 'write': Save buffer as preset into config directory. "
|
||||||
|
" ************************************************************* "
|
||||||
|
elseif stridx("write", substitute(a:command, "!$", "", "")) == 0 " {{{2
|
||||||
|
|
||||||
elseif stridx("write", substitute(a:command, "!$", "", "")) == 0
|
|
||||||
" ********************************* "
|
|
||||||
" Save preset into config directory "
|
|
||||||
" ********************************* "
|
|
||||||
if !exists("a:1")
|
if !exists("a:1")
|
||||||
redraw | echohl WarningMsg
|
call s:Warn("Missing filename!")
|
||||||
echo "Missing filename!"
|
|
||||||
echohl None
|
|
||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
@ -209,21 +268,21 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang
|
||||||
|
|
||||||
let filename = s:config_dir . a:1
|
let filename = s:config_dir . a:1
|
||||||
|
|
||||||
if strridx(a:command, "!") + 1 == len(a:command)
|
if strridx(a:command, "!") == (len(a:command) - 1)
|
||||||
" Force if a '!' was appended to the command.
|
" Force if a '!' was appended to the command.
|
||||||
execute "saveas!" fnameescape(filename)
|
execute "saveas!" fnameescape(filename)
|
||||||
else
|
else
|
||||||
execute "saveas" fnameescape(filename)
|
execute "saveas" fnameescape(filename)
|
||||||
endif
|
endif
|
||||||
call s:SynHighlight()
|
call s:SynHighlight()
|
||||||
|
" }}}2
|
||||||
|
|
||||||
else
|
" **************************** "
|
||||||
" ****************** "
|
" Fallback on unknown command. "
|
||||||
" Unknown command oO "
|
" Error handling. "
|
||||||
" ****************** "
|
" **************************** "
|
||||||
redraw | echohl WarningMsg
|
else " {{{2
|
||||||
echo "Unknown command:" a:command
|
call s:Warn("Unknown command: ".a:command)
|
||||||
echohl None
|
|
||||||
echo "Valid commands are get, kitchentext, open, set and write"
|
echo "Valid commands are get, kitchentext, open, set and write"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
@ -232,19 +291,23 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang
|
||||||
if exists("ret")
|
if exists("ret")
|
||||||
echoerr ret
|
echoerr ret
|
||||||
endif
|
endif
|
||||||
endif
|
endif " }}}2
|
||||||
|
|
||||||
|
" {{{ Clean up environment after C4ctrl().
|
||||||
finally
|
finally
|
||||||
unlet! s:c4ctrl s:config_dir
|
unlet! s:c4ctrl s:config_dir
|
||||||
delfunction s:SynHighlight
|
delfunction s:SynHighlight
|
||||||
|
delfunction s:Warn
|
||||||
endtry
|
endtry
|
||||||
endfunction
|
" }}}
|
||||||
|
|
||||||
|
endfunction " }}}1
|
||||||
|
|
||||||
|
|
||||||
function s:C4ctrlCompletion(ArgLead, CmdLine, CursorPos)
|
" ************************************************************************** "
|
||||||
" ************************************************************************ "
|
" Custom command line completion. "
|
||||||
" Custom command line completion. "
|
" ************************************************************************** "
|
||||||
" ************************************************************************ "
|
function s:C4ctrlCompletion(ArgLead, CmdLine, CursorPos) " {{{1
|
||||||
|
|
||||||
" The name of the command we are adding to Vim.
|
" The name of the command we are adding to Vim.
|
||||||
let command_name = "C4ctrl"
|
let command_name = "C4ctrl"
|
||||||
|
@ -329,13 +392,13 @@ function s:C4ctrlCompletion(ArgLead, CmdLine, CursorPos)
|
||||||
finally
|
finally
|
||||||
unlet! s:config_dir
|
unlet! s:config_dir
|
||||||
endtry
|
endtry
|
||||||
endfunction
|
endfunction " }}}1
|
||||||
|
|
||||||
|
|
||||||
if !exists(":C4ctrl")
|
" ************************************************************************** "
|
||||||
" ********************** "
|
" Add our command to Vim. "
|
||||||
" Add our command to Vim "
|
" ************************************************************************** "
|
||||||
" ********************** "
|
if !exists(":C4ctrl") " {{{1
|
||||||
command -nargs=+ -complete=custom,s:C4ctrlCompletion -range=% C4ctrl call C4ctrl(getcurpos(), <f-mods>, <line1>, <line2>, <f-args>)
|
command -nargs=+ -complete=custom,s:C4ctrlCompletion -range=% C4ctrl call C4ctrl(getcurpos(), <f-mods>, <line1>, <line2>, <f-args>)
|
||||||
endif
|
endif " }}}1
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue