From 44ae580dbf54d6181e384efcf8ff8e35e16c7e8d Mon Sep 17 00:00:00 2001 From: Shy Date: Sun, 16 Apr 2017 11:27:15 +0200 Subject: [PATCH] Kitchentext reworked and fold marks added --- c4ctrl.vim | 261 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 162 insertions(+), 99 deletions(-) diff --git a/c4ctrl.vim b/c4ctrl.vim index 368e7e9..4e79465 100644 --- a/c4ctrl.vim +++ b/c4ctrl.vim @@ -1,12 +1,12 @@ " c4ctrl.vim: This Vim plugin makes some functionality of the c4ctrl command " line utility available from within Vim. " -" Last Change: 2017 Apr 13 +" Last Change: 2017 Apr 16 " Maintainer: Shy " License: This file is placed in the public domain. " -" Usage: C4ctrl [get | kitchentext | open PRESET | set [w] [p] [f] [-magic] | -" write PRESET] +" Usage: C4ctrl [get | kitchentext [REGISTER] | open PRESET | +" set [w] [p] [f] [-magic] | write PRESET] if exists("g:loaded_c4ctrl") finish @@ -14,11 +14,11 @@ endif let g:loaded_c4ctrl = 1 -function s:FindConfigDir() - " ************************************************************************ " - " Returns the path of c4ctrl configuration directory as string, " - " eg. '/home/somepony/.config/c4ctrl/'. " - " ************************************************************************ " +" ************************************************************************** " +" Utility function: Find the path to the c4ctrl configuration directory and " +" return it as string. " +" ************************************************************************** " +function s:FindConfigDir() " {{{1 " Return early if we already know it from an earlier invocation. if exists("s:config_dir") @@ -32,38 +32,51 @@ function s:FindConfigDir() endif if !isdirectory(s:config_dir) - redraw | echohl WarningMsg - echo "Error: could not access config directory:" s:config_dir."!" + redraw + echohl WarningMsg + echo "Error: could not access config directory: ".s:config_dir."!" echohl None return "" endif 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 " - " from within Vim. " - " Available commands are 'get', 'kitchentext', 'open', 'set' and 'write'. " - " Arguments: " - " prev_cursor_pos -- cursor position as returned by getcurpos() " - " mods -- modifiers (:command variable ) " - " first_line -- first line of range (:command ) " - " last_line -- last line of range (:command ) " - " command -- user command ('get', 'set' etc.) " - " [...] -- optional command options " - " ************************************************************************ " +" ************************************************************************** " +" Make some functionality of the 'c4ctrl' command line utility available " +" from within Vim. " +" Available commands are 'get', 'kitchentext', 'open', 'set' and 'write'. " +" Arguments: " +" prev_cursor_pos -- cursor position as returned by getcurpos() " +" mods -- modifiers (:command variable ) " +" first_line -- first line of range (:command ) " +" last_line -- last line of range (:command ) " +" command -- user command ('get', 'set' etc.) " +" [...] -- 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. let s:c4ctrl = "c4ctrl" - function! s:SynHighlight() - " ******************************************************************** " - " This function will be called after a preset has been loaded into the " - " buffer. It will be deleted at the end of C4ctrl(). " - " ******************************************************************** " + " ********************************************************************** " + " Utility function to print out a warning message. " + " ********************************************************************** " + 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 " Match topics @@ -83,64 +96,110 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang " Move the cursor somewhere more practical. call cursor(1,1) call search("^[^#].*=[ \t]*[0-9a-fA-F]", 'eW') - endfunction + + endfunction " }}}2 - " Check if we can excute c4ctrl or c4ctrl.py and modify the variable - " 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? if executable(s:c4ctrl.".py") let s:c4ctrl .= ".py" else - redraw | echohl WarningMsg - echo "Executable not found! Please put \"".s:c4ctrl."\" into your $PATH." - echohl None + call s:Warn("Executable not found! Please put \"".s:c4ctrl."\" into your $PATH.") unlet s:c4ctrl return endif - endif + endif " }}}2 - if stridx("get", a:command) == 0 - " *********************************** " - " Read current status into new buffer " - " *********************************** " + " *************************************************** " + " Command 'get': Read current status into new buffer. " + " *************************************************** " + if stridx("get", a:command) == 0 " {{{2 + execute a:mods "new" silent execute "0 read !" s:c4ctrl "-o -" if v:shell_error == 0 call s:SynHighlight() set nomodified " Mark as unmodified. else - redraw | echohl WarningMsg - echo printf("Error: %s returned exit code %d!", s:c4ctrl, v:shell_error) - echohl None + call s:Warn(printf("Error: %s returned exit code %d!", s:c4ctrl, v:shell_error)) endif + " }}}2 - elseif stridx("kitchentext", a:command) == 0 - " ************************************** " - " Send text in range to the Kitchenlight " - " ************************************** " - let kitchentext = "kitchentext" + " ***************************************************************** " + " Command 'kitchentext': Send given reister or text in range to the " + " Kitchenlight. " + " ***************************************************************** " + elseif stridx("kitchentext", a:command) == 0 " {{{2 + + let kitchentext = 'kitchentext' if !executable(kitchentext) - redraw | echohl WarningMsg - echo "Executable not found! Please put \"".kitchentext."\" into your $PATH." - echohl None + call s:Warn('Executable not found! Please put "'.kitchentext.'" into your $PATH.') return endif - let command_line = "kitchentext -f -d 150 -r -p" - " Strip leading white spaces. - let text = getline(a:first_line, a:last_line) - call map(text, "substitute(v:val, '^[ \t]*', '', '')") + let command_line = 'kitchentext -f -d 150 -r -p' + if exists('a:1') + " Use text from given register. + let text = getreg(a:1, 0, 1) + 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 - elseif stridx("open", a:command) == 0 - " ********************** " - " Edit an exiting preset " - " ********************** " + " ******************************************************* " + " Command 'open': Load an exiting preset into the buffer. " + " ******************************************************* " + elseif stridx("open", a:command) == 0 " {{{2 + if !exists("a:1") - redraw | echohl WarningMsg - echo "Missing filename!" - echohl None + call s:Warn("Missing filename!") return endif @@ -150,20 +209,20 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang endif let filename = s:config_dir . a:1 if !filereadable(filename) - redraw | echohl WarningMsg - echo "Error: could not open file" filename - echohl None + call s:Warn("Error: could not open file ".filename) return endif execute a:mods "new" execute "edit" fnameescape(filename) call s:SynHighlight() + " }}}2 - elseif stridx("set", a:command) == 0 - " ****************************** " - " Set preset from current buffer " - " ****************************** " + " *********************************************** " + " Command 'set': Apply range or buffer as preset. " + " *********************************************** " + elseif stridx("set", a:command) == 0 " {{{2 + " Let's start by building a command line. let command_line = s:c4ctrl let rooms_given = 0 @@ -190,15 +249,15 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang " Restore cursor position. call setpos('.', a:prev_cursor_pos) + " }}}2 - elseif stridx("write", substitute(a:command, "!$", "", "")) == 0 - " ********************************* " - " Save preset into config directory " - " ********************************* " + " ************************************************************* " + " Command 'write': Save buffer as preset into config directory. " + " ************************************************************* " + elseif stridx("write", substitute(a:command, "!$", "", "")) == 0 " {{{2 + if !exists("a:1") - redraw | echohl WarningMsg - echo "Missing filename!" - echohl None + call s:Warn("Missing filename!") return endif @@ -209,21 +268,21 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang 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. execute "saveas!" fnameescape(filename) else execute "saveas" fnameescape(filename) endif call s:SynHighlight() + " }}}2 - else - " ****************** " - " Unknown command oO " - " ****************** " - redraw | echohl WarningMsg - echo "Unknown command:" a:command - echohl None + " **************************** " + " Fallback on unknown command. " + " Error handling. " + " **************************** " + else " {{{2 + call s:Warn("Unknown command: ".a:command) echo "Valid commands are get, kitchentext, open, set and write" endif @@ -232,19 +291,23 @@ function C4ctrl(prev_cursor_pos, mods, first_line, last_line, command, ...) rang if exists("ret") echoerr ret endif - endif - + endif " }}}2 + + " {{{ Clean up environment after C4ctrl(). finally unlet! s:c4ctrl s:config_dir delfunction s:SynHighlight + delfunction s:Warn 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. let command_name = "C4ctrl" @@ -329,13 +392,13 @@ function s:C4ctrlCompletion(ArgLead, CmdLine, CursorPos) finally unlet! s:config_dir 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(), , , , ) -endif +endif " }}}1