Skip to content

Commit

Permalink
Vim-Support: Clean up the code
Browse files Browse the repository at this point in the history
- Internal changes and code clean-up.
  • Loading branch information
WolfgangMehner committed Nov 12, 2016
1 parent cc432f6 commit e28badf
Show file tree
Hide file tree
Showing 2 changed files with 209 additions and 106 deletions.
306 changes: 207 additions & 99 deletions plugin/vim-support.vim
@@ -1,19 +1,22 @@
"===============================================================================
"
" File: vim-support.vim
"
" Description: Vim support (VIM Version 7.0+)
"
" Description: Vim support (VIM Version 7.0+)
"
" Write Vim-plugins by inserting comments, statements,
" variables and builtins.
"
"
" See help file vimsupport.txt .
"
" VIM Version: 7.0+
"
" Author: Wolfgang Mehner <wolfgang-mehner@web.de>
" (formerly Fritz Mehner <mehner.fritz@web.de>)
"
" Version: see variable g:VimSupportVersion below
" Created: 14.01.2012
" Revision: 12.11.2016
" License: Copyright (c) 2012-2015, Fritz Mehner
" Copyright (c) 2016-2016, Wolfgang Mehner
" This program is free software; you can redistribute it and/or
Expand All @@ -26,47 +29,198 @@
" PURPOSE.
" See the GNU General Public License version 2 for more details.
"===============================================================================
"

"-------------------------------------------------------------------------------
" === Basic checks === {{{1
"-------------------------------------------------------------------------------

" need at least 7.0
if v:version < 700
echohl WarningMsg | echo 'plugin vim-support.vim needs Vim version >= 7'| echohl None
finish
echohl WarningMsg
echo 'The plugin vim-support.vim needs Vim version >= 7.'
echohl None
finish
endif
"
" Prevent duplicate loading:
"

" prevent duplicate loading
" need compatible
if exists("g:VimSupportVersion") || &cp
finish
finish
endif

let g:VimSupportVersion= "2.5pre" " version number of this script; do not change

"-------------------------------------------------------------------------------
" === Auxiliary functions === {{{1
"-------------------------------------------------------------------------------

"-------------------------------------------------------------------------------
" s:ApplyDefaultSetting : Write default setting to a global variable. {{{2
"
let g:VimSupportVersion= "2.4" " version number of this script; do not change
" Parameters:
" varname - name of the variable (string)
" value - default value (string)
" Returns:
" -
"
"=== FUNCTION ================================================================
" NAME: GetGlobalSetting {{{1
" DESCRIPTION: take over a global setting
" PARAMETERS: varname - variable to set
" RETURNS:
"===============================================================================
function! s:GetGlobalSetting ( varname )
if exists ( 'g:'.a:varname )
exe 'let s:'.a:varname.' = g:'.a:varname
" If g:<varname> does not exists, assign:
" g:<varname> = value
"-------------------------------------------------------------------------------

function! s:ApplyDefaultSetting ( varname, value )
if ! exists ( 'g:'.a:varname )
let { 'g:'.a:varname } = a:value
endif
endfunction " ---------- end of function s:ApplyDefaultSetting ----------

"-------------------------------------------------------------------------------
" s:ErrorMsg : Print an error message. {{{2
"
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------

function! s:ErrorMsg ( ... )
echohl WarningMsg
for line in a:000
echomsg line
endfor
echohl None
endfunction " ---------- end of function s:ErrorMsg ----------

"-------------------------------------------------------------------------------
" s:GetGlobalSetting : Get a setting from a global variable. {{{2
"
" Parameters:
" varname - name of the variable (string)
" glbname - name of the global variable (string, optional)
" Returns:
" -
"
" If 'glbname' is given, it is used as the name of the global variable.
" Otherwise the global variable will also be named 'varname'.
"
" If g:<glbname> exists, assign:
" s:<varname> = g:<glbname>
"-------------------------------------------------------------------------------

function! s:GetGlobalSetting ( varname, ... )
let lname = a:varname
let gname = a:0 >= 1 ? a:1 : lname
if exists ( 'g:'.gname )
let { 's:'.lname } = { 'g:'.gname }
endif
endfunction " ---------- end of function s:GetGlobalSetting ----------

"-------------------------------------------------------------------------------
" s:ImportantMsg : Print an important message. {{{2
"
"=== FUNCTION ================================================================
" NAME: ApplyDefaultSetting {{{1
" DESCRIPTION: make a local setting global
" PARAMETERS: varname - variable to set
" RETURNS:
"===============================================================================
function! s:ApplyDefaultSetting ( varname )
if ! exists ( 'g:'.a:varname )
exe 'let g:'.a:varname.' = s:'.a:varname
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------

function! s:ImportantMsg ( ... )
echohl Search
echo join ( a:000, "\n" )
echohl None
endfunction " ---------- end of function s:ImportantMsg ----------

"-------------------------------------------------------------------------------
" s:SID : Return the <SID>. {{{2
"
" Parameters:
" -
" Returns:
" SID - the SID of the script (string)
"-------------------------------------------------------------------------------

function! s:SID ()
return matchstr ( expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$' )
endfunction " ---------- end of function s:SID ----------

"-------------------------------------------------------------------------------
" s:UserInput : Input after a highlighted prompt. {{{2
"
" Parameters:
" prompt - the prompt (string)
" text - the default input (string)
" compl - completion (string, optional)
" clist - list, if 'compl' is "customlist" (list, optional)
" Returns:
" input - the user input, an empty sting if the user hit <ESC> (string)
"-------------------------------------------------------------------------------

function! s:UserInput ( prompt, text, ... )

echohl Search " highlight prompt
call inputsave() " preserve typeahead
if a:0 == 0 || a:1 == ''
let retval = input( a:prompt, a:text )
elseif a:1 == 'customlist'
let s:UserInputList = a:2
let retval = input( a:prompt, a:text, 'customlist,<SNR>'.s:SID().'_UserInputEx' )
let s:UserInputList = []
else
let retval = input( a:prompt, a:text, a:1 )
endif
endfunction " ---------- end of function s:ApplyDefaultSetting ----------
call inputrestore() " restore typeahead
echohl None " reset highlighting

let retval = substitute( retval, '^\s\+', "", "" ) " remove leading whitespaces
let retval = substitute( retval, '\s\+$', "", "" ) " remove trailing whitespaces

return retval

endfunction " ---------- end of function s:UserInput ----------

"-------------------------------------------------------------------------------
" s:UserInputEx : ex-command for s:UserInput. {{{3
"-------------------------------------------------------------------------------
function! s:UserInputEx ( ArgLead, CmdLine, CursorPos )
if empty( a:ArgLead )
return copy( s:UserInputList )
endif
return filter( copy( s:UserInputList ), 'v:val =~ ''\V\<'.escape(a:ArgLead,'\').'\w\*''' )
endfunction " ---------- end of function s:UserInputEx ----------
" }}}3
"-------------------------------------------------------------------------------

"-------------------------------------------------------------------------------
" s:WarningMsg : Print a warning/error message. {{{2
"
"------------------------------------------------------------------------------
" *** PLATFORM SPECIFIC ITEMS *** {{{1
"------------------------------------------------------------------------------
" Parameters:
" line1 - a line (string)
" line2 - a line (string)
" ... - ...
" Returns:
" -
"-------------------------------------------------------------------------------

function! s:WarningMsg ( ... )
echohl WarningMsg
echo join ( a:000, "\n" )
echohl None
endfunction " ---------- end of function s:WarningMsg ----------

" }}}2
"-------------------------------------------------------------------------------

"-------------------------------------------------------------------------------
" === Module setup === {{{1
"-------------------------------------------------------------------------------

"-------------------------------------------------------------------------------
" == Platform specific items == {{{2
"-------------------------------------------------------------------------------

let s:MSWIN = has("win16") || has("win32") || has("win64") || has("win95")
let s:UNIX = has("unix") || has("macunix") || has("win32unix")
"
Expand Down Expand Up @@ -128,24 +282,23 @@ endif
"
let s:Vim_AdditionalTemplates = mmtemplates#config#GetFt ( 'vim' )
let s:Vim_CodeSnippets = s:plugin_dir.'/vim-support/codesnippets/'
"
"----------------------------------------------------------------------
" *** MODUL GLOBAL VARIABLES *** {{{1
"----------------------------------------------------------------------
"

"-------------------------------------------------------------------------------
" == Various settings == {{{2
"-------------------------------------------------------------------------------

let s:Vim_CreateMenusDelayed= 'yes'
let s:Vim_MenuVisible = 'no'
let s:Vim_GuiSnippetBrowser = 'gui' " gui / commandline
let s:Vim_LoadMenus = 'yes' " load the menus?
let s:Vim_RootMenu = '&Vim' " name of the root menu
let s:Vim_CreateMapsForHelp = 'no' " create maps for modifiable help buffers as well
"
let s:Vim_MapLeader = '' " default: do not overwrite 'maplocalleader'

let s:Vim_LineEndCommColDefault = 49
let s:VimStartComment = '"'
let s:Vim_Printheader = "%<%f%h%m%< %=%{strftime('%x %X')} Page %N"
let s:Vim_TemplateJumpTarget = '<+\i\++>\|{+\i\++}\|<-\i\+->\|{-\i\+-}'
"

call s:GetGlobalSetting ( 'Vim_GuiSnippetBrowser' )
call s:GetGlobalSetting ( 'Vim_LoadMenus' )
call s:GetGlobalSetting ( 'Vim_RootMenu' )
Expand All @@ -158,59 +311,13 @@ call s:GetGlobalSetting ( 'Vim_CodeSnippets' )
call s:GetGlobalSetting ( 'Vim_CreateMenusDelayed' )
call s:GetGlobalSetting ( 'Vim_LineEndCommColDefault' )

call s:ApplyDefaultSetting ( 'Vim_MapLeader' )
"
let s:Vim_Printheader = escape( s:Vim_Printheader, ' %' )
"
"=== FUNCTION ================================================================
" NAME: SaveOption {{{1
" DESCRIPTION: save a Vim option
" PARAMETERS: option - option name
" ... - characters to be escaped (optional)
" RETURNS:
"===============================================================================
function! s:SaveOption ( option, ... )
exe 'let escaped =&'.a:option
if a:0 > 0
let escaped = escape( escaped, a:1 )
endif
exe 'let s:'.a:option.'_saved = "'.escaped.'"'
endfunction " ---------- end of function s:SaveOption ----------
"
"=== FUNCTION ================================================================
" NAME: RestoreOption {{{1
" DESCRIPTION: restore a Vim option
" PARAMETERS: option - option to be restored
" RETURNS:
"===============================================================================
function! s:RestoreOption ( option )
exe 'let saved = s:'.a:option.'_saved'
exe ':setlocal '.a:option.'="'.saved.'"'
endfunction " ---------- end of function s:RestoreOption ----------
"
"=== FUNCTION ================================================================
" NAME: Vim_Input {{{1
" DESCRIPTION: Input after a highlighted prompt
" PARAMETERS: prompt - prompt string
" defaultreply - default reply
" ... - completion
" RETURNS: reply
"===============================================================================
function! Vim_Input ( prompt, defaultreply, ... )
echohl Search " highlight prompt
call inputsave() " preserve typeahead
if a:0 == 0 || empty(a:1)
let retval =input( a:prompt, a:defaultreply )
else
let retval =input( a:prompt, a:defaultreply, a:1 )
endif
call inputrestore() " restore typeahead
echohl None " reset highlighting
let retval = substitute( retval, '^\s\+', '', '' ) " remove leading whitespaces
let retval = substitute( retval, '\s\+$', '', '' ) " remove trailing whitespaces
return retval
endfunction " ---------- end of function Vim_Input ----------
"
call s:ApplyDefaultSetting ( 'Vim_MapLeader', '' ) " default: do not overwrite 'maplocalleader'

let s:Vim_Printheader = escape( s:Vim_Printheader, ' %' )

" }}}2
"-------------------------------------------------------------------------------

"=== FUNCTION ================================================================
" NAME: Vim_AdjustLineEndComm {{{1
" DESCRIPTION: adjust end-of-line comments
Expand Down Expand Up @@ -324,7 +431,7 @@ function! Vim_GetLineEndCommCol ()
if actcol+1 == virtcol("$")
let b:Vim_LineEndCommentColumn = ''
while match( b:Vim_LineEndCommentColumn, '^\s*\d\+\s*$' ) < 0
let b:Vim_LineEndCommentColumn = Vim_Input( 'start line-end comment at virtual column : ', actcol, '' )
let b:Vim_LineEndCommentColumn = s:UserInput( 'start line-end comment at virtual column : ', actcol, '' )
endwhile
else
let b:Vim_LineEndCommentColumn = virtcol(".")
Expand Down Expand Up @@ -1111,9 +1218,9 @@ function! Vim_RemoveGuiMenus ()
endif
endfunction " ---------- end of function Vim_RemoveGuiMenus ----------

"----------------------------------------------------------------------
" *** SETUP PLUGIN *** {{{1
"----------------------------------------------------------------------
"-------------------------------------------------------------------------------
" === Setup: Templates, toolbox and menus === {{{1
"-------------------------------------------------------------------------------

call Vim_ToolMenu()

Expand All @@ -1136,6 +1243,7 @@ if has( 'autocmd' )

endif
" }}}1
"
"-------------------------------------------------------------------------------

" =====================================================================================
" vim: tabstop=2 shiftwidth=2 foldmethod=marker

0 comments on commit e28badf

Please sign in to comment.