From 212dcd0bf753f08c0127a26a71b673c734b45c02 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Thu, 5 Sep 2019 19:35:38 +0300 Subject: init commit, extracted vim config from i3rice --- .vim/plugin/airline.vim | 208 ++++++ .vim/plugin/dragvisuals.vim | 345 ++++++++++ .vim/plugin/gnupg.vim | 1287 ++++++++++++++++++++++++++++++++++++++ .vim/plugin/latexlivepreview.vim | 276 ++++++++ .vim/plugin/neomake.vim | 50 ++ .vim/plugin/node.vim | 47 ++ .vim/plugin/vmath.vim | 152 +++++ 7 files changed, 2365 insertions(+) create mode 100644 .vim/plugin/airline.vim create mode 100644 .vim/plugin/dragvisuals.vim create mode 100644 .vim/plugin/gnupg.vim create mode 100644 .vim/plugin/latexlivepreview.vim create mode 100644 .vim/plugin/neomake.vim create mode 100644 .vim/plugin/node.vim create mode 100644 .vim/plugin/vmath.vim (limited to '.vim/plugin') diff --git a/.vim/plugin/airline.vim b/.vim/plugin/airline.vim new file mode 100644 index 0000000..c91ff20 --- /dev/null +++ b/.vim/plugin/airline.vim @@ -0,0 +1,208 @@ +" MIT License. Copyright (c) 2013-2018 Bailey Ling et al. +" vim: et ts=2 sts=2 sw=2 + +scriptencoding utf-8 + +if &cp || v:version < 702 || (exists('g:loaded_airline') && g:loaded_airline) + finish +endif +let g:loaded_airline = 1 + +let s:airline_initialized = 0 +function! s:init() + if s:airline_initialized + return + endif + let s:airline_initialized = 1 + + call airline#extensions#load() + call airline#init#sections() + + let s:theme_in_vimrc = exists('g:airline_theme') + if s:theme_in_vimrc + try + let palette = g:airline#themes#{g:airline_theme}#palette + catch + call airline#util#warning(printf('Could not resolve airline theme "%s". Themes have been migrated to github.com/vim-airline/vim-airline-themes.', g:airline_theme)) + let g:airline_theme = 'dark' + endtry + silent call airline#switch_theme(g:airline_theme) + else + let g:airline_theme = 'dark' + silent call s:on_colorscheme_changed() + endif + + call airline#util#doautocmd('AirlineAfterInit') +endfunction + +let s:active_winnr = -1 +function! s:on_window_changed() + let s:active_winnr = winnr() + + if pumvisible() && (!&previewwindow || g:airline_exclude_preview) + return + endif + " Handle each window only once, since we might come here several times for + " different autocommands. + let l:key = [bufnr('%'), s:active_winnr, winnr('$'), tabpagenr(), &ft] + if get(g:, 'airline_last_window_changed', []) == l:key + \ && &stl is# '%!airline#statusline('.s:active_winnr.')' + \ && &ft !~? 'gitcommit' + " fugitive is special, it changes names and filetypes several times, + " make sure the caching does not get into its way + return + endif + let g:airline_last_window_changed = l:key + call s:init() + call airline#update_statusline() +endfunction + +function! s:on_colorscheme_changed() + call s:init() + unlet! g:airline#highlighter#normal_fg_hi + call airline#highlighter#reset_hlcache() + let g:airline_gui_mode = airline#init#gui_mode() + if !s:theme_in_vimrc + call airline#switch_matching_theme() + endif + + " couldn't find a match, or theme was defined, just refresh + call airline#load_theme() +endfunction + +function! airline#cmdwinenter(...) + call airline#extensions#apply_left_override('Command Line', '') +endfunction + +function! s:airline_toggle() + if exists("#airline") + augroup airline + au! + augroup END + augroup! airline + + if exists("s:stl") + let &stl = s:stl + endif + call airline#highlighter#reset_hlcache() + + call airline#util#doautocmd('AirlineToggledOff') + else + let s:stl = &statusline + augroup airline + autocmd! + + autocmd CmdwinEnter * + \ call airline#add_statusline_func('airline#cmdwinenter') + \ | call on_window_changed() + autocmd CmdwinLeave * call airline#remove_statusline_func('airline#cmdwinenter') + + autocmd GUIEnter,ColorScheme * call on_colorscheme_changed() + if exists("##OptionSet") + " Make sure that g_airline_gui_mode is refreshed + autocmd OptionSet termguicolors call on_colorscheme_changed() + endif + " Set all statuslines to inactive + autocmd FocusLost * call airline#update_statusline_focuslost() + " Refresh airline for :syntax off + autocmd SourcePre */syntax/syntax.vim + \ call airline#extensions#tabline#buffers#invalidate() + autocmd VimEnter,WinEnter,BufWinEnter,FileType,BufUnload * + \ call on_window_changed() + if exists('##CompleteDone') + autocmd CompleteDone * call on_window_changed() + endif + " non-trivial number of external plugins use eventignore=all, so we need to account for that + autocmd CursorMoved * + \ if winnr() != s:active_winnr + \ | call on_window_changed() + \ | endif + + autocmd VimResized * unlet! w:airline_lastmode | :call airline_refresh() + if exists('*timer_start') && exists('*funcref') + " do not trigger FocusGained on startup, it might erase the intro screen (see #1817) + " needs funcref() (needs 7.4.2137) and timers (7.4.1578) + let Handler=funcref('FocusGainedHandler') + let s:timer=timer_start(5000, Handler) + else + autocmd FocusGained * unlet! w:airline_lastmode | :call airline_refresh() + endif + + if exists("##TerminalOpen") + " Using the same function with the TermOpen autocommand + " breaks for Neovim see #1828, looks like a neovim bug. + autocmd TerminalOpen * :call airline#load_theme() " reload current theme for Terminal, forces the terminal extension to be loaded + endif + autocmd TabEnter * :unlet! w:airline_lastmode | let w:airline_active=1 + autocmd BufWritePost */autoload/airline/themes/*.vim + \ exec 'source '.split(globpath(&rtp, 'autoload/airline/themes/'.g:airline_theme.'.vim', 1), "\n")[0] + \ | call airline#load_theme() + augroup END + + if &laststatus < 2 + set laststatus=2 + endif + if s:airline_initialized + call s:on_window_changed() + endif + + call airline#util#doautocmd('AirlineToggledOn') + endif +endfunction + +function! s:get_airline_themes(a, l, p) + return airline#util#themes(a:a) +endfunction + +function! s:airline_theme(...) + if a:0 + try + call airline#switch_theme(a:1) + catch " discard error + endtry + else + echo g:airline_theme + endif +endfunction + +function! s:airline_refresh() + if !exists("#airline") + " disabled + return + endif + call airline#util#doautocmd('AirlineBeforeRefresh') + call airline#highlighter#reset_hlcache() + call airline#load_theme() + call airline#update_statusline() +endfunction + +function! s:FocusGainedHandler(timer) + if exists("s:timer") && a:timer == s:timer + augroup airline + au FocusGained * unlet! w:airline_lastmode | :call airline_refresh() + augroup END + endif +endfu + +function! s:airline_extensions() + let loaded = airline#extensions#get_loaded_extensions() + let files = split(globpath(&rtp, "autoload/airline/extensions/*.vim"), "\n") + call map(files, 'fnamemodify(v:val, ":t:r")') + if !empty(files) + echohl Title + echo printf("%-15s\t%s", "Extension", "Status") + echohl Normal + endif + for ext in sort(files) + echo printf("%-15s\t%sloaded", ext, (index(loaded, ext) == -1 ? 'not ' : '')) + endfor +endfunction + +command! -bar -nargs=? -complete=customlist,get_airline_themes AirlineTheme call airline_theme() +command! -bar AirlineToggleWhitespace call airline#extensions#whitespace#toggle() +command! -bar AirlineToggle call s:airline_toggle() +command! -bar AirlineRefresh call s:airline_refresh() +command! AirlineExtensions call s:airline_extensions() + +call airline#init#bootstrap() +call s:airline_toggle() diff --git a/.vim/plugin/dragvisuals.vim b/.vim/plugin/dragvisuals.vim new file mode 100644 index 0000000..12c4f5d --- /dev/null +++ b/.vim/plugin/dragvisuals.vim @@ -0,0 +1,345 @@ +" Vim global plugin for dragging virtual blocks +" Last change: Tue Jul 24 07:19:35 EST 2012 +" Maintainer: Damian Conway +" License: This file is placed in the public domain. + +"######################################################################### +"## ## +"## Add the following (uncommented) to your .vimrc... ## +"## ## +"## runtime plugin/dragvisuals.vim ## +"## ## +"## vmap DVB_Drag('left') ## +"## vmap DVB_Drag('right') ## +"## vmap DVB_Drag('down') ## +"## vmap DVB_Drag('up') ## +"## vmap D DVB_Duplicate() ## +"## ## +"## " Remove any introduced trailing whitespace after moving... ## +"## let g:DVB_TrimWS = 1 ## +"## ## +"## Or, if you use the arrow keys for normal motions, choose ## +"## four other keys for block dragging. For example: ## +"## ## +"## vmap h DVB_Drag('left') ## +"## vmap l DVB_Drag('right') ## +"## vmap j DVB_Drag('down') ## +"## vmap k DVB_Drag('up') ## +"## ## +"## Or: ## +"## ## +"## vmap DVB_Drag('left') ## +"## vmap DVB_Drag('right') ## +"## vmap DVB_Drag('down') ## +"## vmap DVB_Drag('up') ## +"## ## +"## Or even: ## +"## ## +"## vmap DVB_Drag('left') ## +"## vmap DVB_Drag('right') ## +"## vmap DVB_Drag('down') ## +"## vmap DVB_Drag('up') ## +"## ## +"######################################################################### + + +" If already loaded, we're done... +if exists("loaded_dragvirtualblocks") + finish +endif +let loaded_dragvirtualblocks = 1 + +" Preserve external compatibility options, then enable full vim compatibility... +let s:save_cpo = &cpo +set cpo&vim + +"====[ Implementation ]==================================== + +" Toggle this to stop trimming on drags... +if !exists('g:DVB_TrimWS') + let g:DVB_TrimWS = 1 +endif + +function! DVB_Drag (dir) + " No-op in Visual mode... + if mode() ==# 'v' + return "\gv" + + " Do Visual Line drag indirectly via temporary nmap + " (to ensure we have access to block position data)... + elseif mode() ==# 'V' + " Set up a temporary convenience... + exec "nnoremap M \Drag_Lines('".a:dir."')" + + " Return instructions to implement the move and reset selection... + return '"vyM' + + " Otherwise do Visual Block drag indirectly via temporary nmap + " (to ensure we have access to block position data)... + else + " Set up a temporary convenience... + exec "nnoremap M \Drag_Block('".a:dir."')" + + " Return instructions to implement the move and reset selection... + return '"vyM' + endif +endfunction + +" Duplicate selected block and place to the right... +function! DVB_Duplicate () + exec "nnoremap M \DuplicateBlock()" + return '"vyM' +endfunction + +function! s:DuplicateBlock () + nunmap M + " Locate block boundaries... + let [buf_left, line_left, col_left, offset_left ] = getpos("'<") + let [buf_right, line_right, col_right, offset_right] = getpos("'>") + + " Identify special '$' blocks... + let dollar_block = 0 + let start_col = min([col_left+offset_left, col_right+offset_right]) + let end_col = max([col_left+offset_left, col_right+offset_right]) + let visual_width = end_col - start_col + 1 + for visual_line in split(getreg("v"),"\n") + if strlen(visual_line) > visual_width + let dollar_block = 1 + let visual_width = strlen(visual_line) + endif + endfor + let square_up = (dollar_block ? (start_col+visual_width-2).'|' : '') + + set virtualedit=all + return 'gv'.square_up.'yPgv' + \. (visual_width-dollar_block) . 'lo' . (visual_width-dollar_block) . 'l' + \. "y:set virtualedit=block\gv" + \. (dollar_block ? 'o$' : '') +endfunction + + +" Kludge to hide change reporting inside implementation... +let s:NO_REPORT = ":let b:DVB_report=&report\:let &report=1000000000\" +let s:PREV_REPORT = ":let &report = b:DVB_report\" + + +" Drag in specified direction in Visual Line mode... +function! s:Drag_Lines (dir) + " Clean up the temporary convenience... + nunmap M + + " Locate block being shifted... + let [buf_left, line_left, col_left, offset_left ] = getpos("'<") + let [buf_right, line_right, col_right, offset_right] = getpos("'>") + + " Drag entire lines left if possible... + if a:dir == 'left' + " Are all lines indented at least one space??? + let lines = getline(line_left, line_right) + let all_indented = match(lines, '^[^ ]') == -1 + nohlsearch + + " If can't trim one space from start of each line, be a no-op... + if !all_indented + return 'gv' + + " Otherwise drag left by removing one space from start of each line... + else + return s:NO_REPORT + \ . "gv:s/^ //\" + \ . s:PREV_REPORT + \ . "gv" + endif + + " To drag entire lines right, add a space in column 1... + elseif a:dir == 'right' + return s:NO_REPORT + \ . "gv:s/^/ /\:nohlsearch\" + \ . s:PREV_REPORT + \ . "gv" + + " To drag entire lines upwards... + elseif a:dir == 'up' + let EOF = line('$') + + " Can't drag up if at first line... + if line_left == 1 || line_right == 1 + return 'gv' + + " Needs special handling at EOF (because cursor moves up on delete)... + elseif line_left == EOF || line_right == EOF + let height = line_right - line_left + let select_extra = height ? height . 'j' : "" + return s:NO_REPORT + \ . 'gvxP' + \ . s:PREV_REPORT + \ . 'V' . select_extra + + " Otherwise just cut-move-paste-reselect... + else + let height = line_right - line_left + let select_extra = height ? height . 'j' : "" + return s:NO_REPORT + \ . 'gvxkP' + \ . s:PREV_REPORT + \ . 'V' . select_extra + endif + + " To drag entire lines downwards... + elseif a:dir == 'down' + let EOF = line('$') + + " This is how much extra we're going to have to reselect... + let height = line_right - line_left + let select_extra = height ? height . 'j' : "" + + " Needs special handling at EOF (to push selection down into new space)... + if line_left == EOF || line_right == EOF + return "O\gv" + + " Otherwise, just cut-move-paste-reselect... + else + return s:NO_REPORT + \ . 'gvxp' + \ . s:PREV_REPORT + \ . 'V' . select_extra + endif + + endif +endfunction + +" Drag in specified direction in Visual Block mode... +function! s:Drag_Block (dir) + " Clean up the temporary convenience... + nunmap M + + " Locate block being shifted... + let [buf_left, line_left, col_left, offset_left ] = getpos("'<") + let [buf_right, line_right, col_right, offset_right] = getpos("'>") + + " Identify special '$' blocks... + let dollar_block = 0 + let start_col = min([col_left+offset_left, col_right+offset_right]) + let end_col = max([col_left+offset_left, col_right+offset_right]) + let visual_width = end_col - start_col + 1 + for visual_line in split(getreg("v"),"\n") + if strlen(visual_line) > visual_width + let dollar_block = 1 + let visual_width = strlen(visual_line) + endif + endfor + let square_up = (dollar_block ? (start_col+visual_width-2).'|' : '') + + " Drag left... + if a:dir == 'left' + "Can't drag left at left margin... + if col_left == 1 || col_right == 1 + return 'gv' + + " Otherwise reposition one column left (and optionally trim any whitespace)... + elseif g:DVB_TrimWS + " May need to be able to temporarily step past EOL... + let prev_ve = &virtualedit + set virtualedit=all + + " Are we moving past other text??? + let square_up_final = "" + if dollar_block + let lines = getline(line_left, line_right) + if match(lines, '^.\{'.(start_col-2).'}\S') >= 0 + let dollar_block = 0 + let square_up_final = (start_col+visual_width-3).'|' + endif + endif + + let vcol = start_col - 2 + return 'gv'.square_up.'xhP' + \ . s:NO_REPORT + \ . "gvhoho:s/\\s*$//\gv\" + \ . ':set virtualedit=' . prev_ve . "\" + \ . s:PREV_REPORT + \ . ":nohlsearch\gv" + \ . (dollar_block ? '$' : square_up_final ) + else + return 'gv'.square_up.'xhPgvhoho' + endif + + " Drag right... + elseif a:dir == 'right' + " May need to be able to temporarily step past EOL... + let prev_ve = &virtualedit + set virtualedit=all + + " Reposition block one column to the right... + if g:DVB_TrimWS + let vcol = start_col + return 'gv'.square_up.'xp' + \ . s:NO_REPORT + \ . "gvlolo" + \ . ":s/\\s*$//\gv\" + \ . ':set virtualedit=' . prev_ve . "\" + \ . s:PREV_REPORT + \ . (dollar_block ? 'gv$' : 'gv') + else + return 'gv'.square_up.'xp:set virtualedit=' . prev_ve . "\gvlolo" + endif + + " Drag upwards... + elseif a:dir == 'up' + " Can't drag upwards at top margin... + if line_left == 1 || line_right == 1 + return 'gv' + endif + + " May need to be able to temporarily step past EOL... + let prev_ve = &virtualedit + set virtualedit=all + + " If trimming whitespace, jump to just below block to do it... + if g:DVB_TrimWS + let height = line_right - line_left + 1 + return 'gv'.square_up.'xkPgvkoko"vy' + \ . height + \ . 'j:s/\s*$//' + \ . "\:nohlsearch\:set virtualedit=" + \ . prev_ve + \ . "\gv" + \ . (dollar_block ? '$' : '') + + " Otherwise just move and reselect... + else + return 'gv'.square_up.'xkPgvkoko"vy:set virtualedit=' + \ . prev_ve + \ . "\gv" + \ . (dollar_block ? '$' : '') + endif + + " Drag downwards... + elseif a:dir == 'down' + " May need to be able to temporarily step past EOL... + let prev_ve = &virtualedit + set virtualedit=all + + " If trimming whitespace, move to just above block to do it... + if g:DVB_TrimWS + return 'gv'.square_up.'xjPgvjojo"vyk:s/\s*$//' + \ . "\:nohlsearch\:set virtualedit=" + \ . prev_ve + \ . "\gv" + \ . (dollar_block ? '$' : '') + + " Otherwise just move and reselect... + else + return 'gv'.square_up.'xjPgvjojo"vy' + \ . "\:set virtualedit=" + \ . prev_ve + \ . "\gv" + \ . (dollar_block ? '$' : '') + endif + endif +endfunction + + +" Restore previous external compatibility options +let &cpo = s:save_cpo + diff --git a/.vim/plugin/gnupg.vim b/.vim/plugin/gnupg.vim new file mode 100644 index 0000000..97539c5 --- /dev/null +++ b/.vim/plugin/gnupg.vim @@ -0,0 +1,1287 @@ +" Name: gnupg.vim +" Last Change: 2012 May 31 +" Maintainer: James McCoy +" Original Author: Markus Braun +" Summary: Vim plugin for transparent editing of gpg encrypted files. +" License: This program is free software; you can redistribute it and/or +" modify it under the terms of the GNU General Public License +" as published by the Free Software Foundation; either version +" 2 of the License, or (at your option) any later version. +" See http://www.gnu.org/copyleft/gpl-2.0.txt +" +" Section: Documentation {{{1 +" +" Description: {{{2 +" +" This script implements transparent editing of gpg encrypted files. The +" filename must have a ".gpg", ".pgp" or ".asc" suffix. When opening such +" a file the content is decrypted, when opening a new file the script will +" ask for the recipients of the encrypted file. The file content will be +" encrypted to all recipients before it is written. The script turns off +" viminfo, swapfile, and undofile to increase security. +" +" Installation: {{{2 +" +" Copy the gnupg.vim file to the $HOME/.vim/plugin directory. +" Refer to ':help add-plugin', ':help add-global-plugin' and ':help +" runtimepath' for more details about Vim plugins. +" +" From "man 1 gpg-agent": +" +" ... +" You should always add the following lines to your .bashrc or whatever +" initialization file is used for all shell invocations: +" +" GPG_TTY=`tty` +" export GPG_TTY +" +" It is important that this environment variable always reflects the out‐ +" put of the tty command. For W32 systems this option is not required. +" ... +" +" Most distributions provide software to ease handling of gpg and gpg-agent. +" Examples are keychain or seahorse. +" +" Commands: {{{2 +" +" :GPGEditRecipients +" Opens a scratch buffer to change the list of recipients. Recipients that +" are unknown (not in your public key) are highlighted and have +" a prepended "!". Closing the buffer makes the changes permanent. +" +" :GPGViewRecipients +" Prints the list of recipients. +" +" :GPGEditOptions +" Opens a scratch buffer to change the options for encryption (symmetric, +" asymmetric, signing). Closing the buffer makes the changes permanent. +" WARNING: There is no check of the entered options, so you need to know +" what you are doing. +" +" :GPGViewOptions +" Prints the list of options. +" +" Variables: {{{2 +" +" g:GPGExecutable +" If set used as gpg executable, otherwise the system chooses what is run +" when "gpg" is called. Defaults to "gpg". +" +" g:GPGUseAgent +" If set to 0 a possible available gpg-agent won't be used. Defaults to 1. +" +" g:GPGPreferSymmetric +" If set to 1 symmetric encryption is preferred for new files. Defaults to 0. +" +" g:GPGPreferArmor +" If set to 1 armored data is preferred for new files. Defaults to 0 +" unless a "*.asc" file is being edited. +" +" g:GPGPreferSign +" If set to 1 signed data is preferred for new files. Defaults to 0. +" +" g:GPGDefaultRecipients +" If set, these recipients are used as defaults when no other recipient is +" defined. This variable is a Vim list. Default is unset. +" +" g:GPGUsePipes +" If set to 1, use pipes instead of temporary files when interacting with +" gnupg. When set to 1, this can cause terminal-based gpg agents to not +" display correctly when prompting for passwords. Defaults to 0. +" +" g:GPGHomedir +" If set, specifies the directory that will be used for GPG's homedir. +" This corresponds to gpg's --homedir option. This variable is a Vim +" string. +" +" Known Issues: {{{2 +" +" In some cases gvim can't decrypt files + +" This is caused by the fact that a running gvim has no TTY and thus gpg is +" not able to ask for the passphrase by itself. This is a problem for Windows +" and Linux versions of gvim and could not be solved unless a "terminal +" emulation" is implemented for gvim. To circumvent this you have to use any +" combination of gpg-agent and a graphical pinentry program: +" +" - gpg-agent only: +" you need to provide the passphrase for the needed key to gpg-agent +" in a terminal before you open files with gvim which require this key. +" +" - pinentry only: +" you will get a popup window every time you open a file that needs to +" be decrypted. +" +" - gpgagent and pinentry: +" you will get a popup window the first time you open a file that +" needs to be decrypted. +" +" Credits: {{{2 +" +" - Mathieu Clabaut for inspirations through his vimspell.vim script. +" - Richard Bronosky for patch to enable ".pgp" suffix. +" - Erik Remmelzwaal for patch to enable windows support and patient beta +" testing. +" - Lars Becker for patch to make gpg2 working. +" - Thomas Arendsen Hein for patch to convert encoding of gpg output. +" - Karl-Heinz Ruskowski for patch to fix unknown recipients and trust model +" and patient beta testing. +" - Giel van Schijndel for patch to get GPG_TTY dynamically. +" - Sebastian Luettich for patch to fix issue with symmetric encryption an set +" recipients. +" - Tim Swast for patch to generate signed files. +" - James Vega for patches for better '*.asc' handling, better filename +" escaping and better handling of multiple keyrings. +" +" Section: Plugin header {{{1 + +" guard against multiple loads {{{2 +if (exists("g:loaded_gnupg") || &cp || exists("#GnuPG")) + finish +endif +let g:loaded_gnupg = '2.5' +let s:GPGInitRun = 0 + +" check for correct vim version {{{2 +if (v:version < 702) + echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7.2' | echohl None + finish +endif + +" Section: Autocmd setup {{{1 + +augroup GnuPG + autocmd! + + " do the decryption + autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGInit(1) + autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGDecrypt(1) + autocmd BufReadCmd *.\(gpg\|asc\|pgp\) call s:GPGBufReadPost() + autocmd FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGInit(0) + autocmd FileReadCmd *.\(gpg\|asc\|pgp\) call s:GPGDecrypt(0) + + " convert all text to encrypted text before writing + autocmd BufWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGBufWritePre() + autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGInit(0) + autocmd BufWriteCmd,FileWriteCmd *.\(gpg\|asc\|pgp\) call s:GPGEncrypt() + + " cleanup on leaving vim + autocmd VimLeave *.\(gpg\|asc\|pgp\) call s:GPGCleanup() +augroup END + +" Section: Constants {{{1 + +let s:GPGMagicString = "\t \t" +let s:keyPattern = '\%(0x\)\=[[:xdigit:]]\{8,16}' + +" Section: Highlight setup {{{1 + +highlight default link GPGWarning WarningMsg +highlight default link GPGError ErrorMsg +highlight default link GPGHighlightUnknownRecipient ErrorMsg + +" Section: Functions {{{1 + +" Function: s:GPGInit(bufread) {{{2 +" +" initialize the plugin +" The bufread argument specifies whether this was called due to BufReadCmd +" +function s:GPGInit(bufread) + call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGInit(%d)", a:bufread)) + + " For FileReadCmd, we're reading the contents into another buffer. If that + " buffer is also destined to be encrypted, then these settings will have + " already been set, otherwise don't set them since it limits the + " functionality of the cleartext buffer. + if a:bufread + " we don't want a swap file, as it writes unencrypted data to disk + setl noswapfile + + " if persistent undo is present, disable it for this buffer + if exists('+undofile') + setl noundofile + endif + + " first make sure nothing is written to ~/.viminfo while editing + " an encrypted file. + set viminfo= + endif + + " the rest only has to be run once + if s:GPGInitRun + return + endif + + " check what gpg command to use + if (!exists("g:GPGExecutable")) + let g:GPGExecutable = "gpg --trust-model always" + endif + + " check if gpg-agent is allowed + if (!exists("g:GPGUseAgent")) + let g:GPGUseAgent = 1 + endif + + " check if symmetric encryption is preferred + if (!exists("g:GPGPreferSymmetric")) + let g:GPGPreferSymmetric = 0 + endif + + " check if armored files are preferred + if (!exists("g:GPGPreferArmor")) + " .asc files should be armored as that's what the extension is used for + if expand('') =~ '\.asc$' + let g:GPGPreferArmor = 1 + else + let g:GPGPreferArmor = 0 + endif + endif + + " check if signed files are preferred + if (!exists("g:GPGPreferSign")) + let g:GPGPreferSign = 0 + endif + + " start with empty default recipients if none is defined so far + if (!exists("g:GPGDefaultRecipients")) + let g:GPGDefaultRecipients = [] + endif + + " prefer not to use pipes since it can garble gpg agent display + if (!exists("g:GPGUsePipes")) + let g:GPGUsePipes = 0 + endif + + " allow alternate gnupg homedir + if (!exists('g:GPGHomedir')) + let g:GPGHomedir = '' + endif + + " print version + call s:GPGDebug(1, "gnupg.vim ". g:loaded_gnupg) + + " determine if gnupg can use the gpg-agent + if (exists("$GPG_AGENT_INFO") && g:GPGUseAgent == 1) + if (!exists("$GPG_TTY") && !has("gui_running")) + let $GPG_TTY = system("tty") + if (v:shell_error) + let $GPG_TTY = "" + echohl GPGError + echom "The GPG_TTY is not set and no TTY could be found using the `tty` command!" + echom "gpg-agent might not work." + echohl None + endif + endif + let s:GPGCommand = g:GPGExecutable . " --use-agent" + else + let s:GPGCommand = g:GPGExecutable . " --no-use-agent" + endif + + " don't use tty in gvim except for windows: we get their a tty for free. + " FIXME find a better way to avoid an error. + " with this solution only --use-agent will work + if (has("gui_running") && !has("gui_win32")) + let s:GPGCommand = s:GPGCommand . " --no-tty" + endif + + " setup shell environment for unix and windows + let s:shellredirsave = &shellredir + let s:shellsave = &shell + let s:shelltempsave = &shelltemp + " noshelltemp isn't currently supported on Windows, but it doesn't cause any + " errors and this future proofs us against requiring changes if Windows + " gains noshelltemp functionality + let s:shelltemp = !g:GPGUsePipes + if (has("unix")) + " unix specific settings + let s:shellredir = ">%s 2>&1" + let s:shell = '/bin/sh' + let s:stderrredirnull = '2>/dev/null' + let s:GPGCommand = "LANG=C LC_ALL=C " . s:GPGCommand + else + " windows specific settings + let s:shellredir = '>%s' + let s:shell = &shell + let s:stderrredirnull = '2>nul' + endif + + call s:GPGDebug(3, "shellredirsave: " . s:shellredirsave) + call s:GPGDebug(3, "shellsave: " . s:shellsave) + call s:GPGDebug(3, "shelltempsave: " . s:shelltempsave) + + call s:GPGDebug(3, "shell: " . s:shell) + call s:GPGDebug(3, "shellcmdflag: " . &shellcmdflag) + call s:GPGDebug(3, "shellxquote: " . &shellxquote) + call s:GPGDebug(3, "shellredir: " . s:shellredir) + call s:GPGDebug(3, "stderrredirnull: " . s:stderrredirnull) + + call s:GPGDebug(3, "shell implementation: " . resolve(s:shell)) + + " find the supported algorithms + let output = s:GPGSystem({ 'level': 2, 'args': '--version' }) + + let s:GPGPubkey = substitute(output, ".*Pubkey: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCipher = substitute(output, ".*Cipher: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGHash = substitute(output, ".*Hash: \\(.\\{-}\\)\n.*", "\\1", "") + let s:GPGCompress = substitute(output, ".*Compress.\\{-}: \\(.\\{-}\\)\n.*", "\\1", "") + + call s:GPGDebug(2, "public key algorithms: " . s:GPGPubkey) + call s:GPGDebug(2, "cipher algorithms: " . s:GPGCipher) + call s:GPGDebug(2, "hashing algorithms: " . s:GPGHash) + call s:GPGDebug(2, "compression algorithms: " . s:GPGCompress) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGInit()") + let s:GPGInitRun = 1 +endfunction + +" Function: s:GPGCleanup() {{{2 +" +" cleanup on leaving vim +" +function s:GPGCleanup() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCleanup()") + + " wipe out screen + new +only + redraw! + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCleanup()") +endfunction + +" Function: s:GPGDecrypt(bufread) {{{2 +" +" decrypt the buffer and find all recipients of the encrypted file +" The bufread argument specifies whether this was called due to BufReadCmd +" +function s:GPGDecrypt(bufread) + call s:GPGDebug(3, printf(">>>>>>>> Entering s:GPGDecrypt(%d)", a:bufread)) + + " get the filename of the current buffer + let filename = expand(":p") + + " clear GPGRecipients and GPGOptions + let b:GPGRecipients = g:GPGDefaultRecipients + let b:GPGOptions = [] + + " File doesn't exist yet, so nothing to decrypt + if empty(glob(filename)) + return + endif + + " Only let this if the file actually exists, otherwise GPG functionality + " will be disabled when editing a buffer that doesn't yet have a backing + " file + let b:GPGEncrypted = 0 + + " find the recipients of the file + let cmd = { 'level': 3 } + let cmd.args = '--verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 ' . shellescape(filename) + let output = s:GPGSystem(cmd) + + " Suppress the "N more lines" message when editing a file, not when reading + " the contents of a file into a buffer + let silent = a:bufread ? 'silent ' : '' + + let asymmPattern = 'gpg: public key is ' . s:keyPattern + " check if the file is symmetric/asymmetric encrypted + if (match(output, "gpg: encrypted with [[:digit:]]\\+ passphrase") >= 0) + " file is symmetric encrypted + let b:GPGEncrypted = 1 + call s:GPGDebug(1, "this file is symmetric encrypted") + + let b:GPGOptions += ["symmetric"] + + " find the used cipher algorithm + let cipher = substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") + if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) + let b:GPGOptions += ["cipher-algo " . cipher] + call s:GPGDebug(1, "cipher-algo is " . cipher) + else + echohl GPGWarning + echom "The cipher " . cipher . " is not known by the local gpg command. Using default!" + echo + echohl None + endif + elseif (match(output, asymmPattern) >= 0) + " file is asymmetric encrypted + let b:GPGEncrypted = 1 + call s:GPGDebug(1, "this file is asymmetric encrypted") + + let b:GPGOptions += ["encrypt"] + + " find the used public keys + let start = match(output, asymmPattern) + while (start >= 0) + let start = start + strlen("gpg: public key is ") + let recipient = matchstr(output, s:keyPattern, start) + call s:GPGDebug(1, "recipient is " . recipient) + let name = s:GPGNameToID(recipient) + if (strlen(name) > 0) + let b:GPGRecipients += [name] + call s:GPGDebug(1, "name of recipient is " . name) + else + let b:GPGRecipients += [recipient] + echohl GPGWarning + echom "The recipient \"" . recipient . "\" is not in your public keyring!" + echohl None + end + let start = match(output, asymmPattern, start) + endwhile + else + " file is not encrypted + let b:GPGEncrypted = 0 + call s:GPGDebug(1, "this file is not encrypted") + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + exe printf('%sr %s', silent, fnameescape(filename)) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") + return + endif + + " check if the message is armored + if (match(output, "gpg: armor header") >= 0) + call s:GPGDebug(1, "this file is armored") + let b:GPGOptions += ["armor"] + endif + + " finally decrypt the buffer content + " since even with the --quiet option passphrase typos will be reported, + " we must redirect stderr (using shell temporarily) + call s:GPGDebug(1, "decrypting file") + let cmd = { 'level': 1, 'ex': silent . 'r !' } + let cmd.args = '--quiet --decrypt ' . shellescape(filename, 1) + call s:GPGExecute(cmd) + + if (v:shell_error) " message could not be decrypted + echohl GPGError + let blackhole = input("Message could not be decrypted! (Press ENTER)") + echohl None + " Only wipeout the buffer if we were creating one to start with. + " FileReadCmd just reads the content into the existing buffer + if a:bufread + silent bwipeout! + endif + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") + return + endif + + " refresh screen + redraw! + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGDecrypt()") +endfunction + +" Function: s:GPGBufReadPost() {{{2 +" +" Handle functionality specific to opening a file for reading rather than +" reading the contents of a file into a buffer +" +function s:GPGBufReadPost() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufReadPost()") + " In order to make :undo a no-op immediately after the buffer is read, + " we need to do this dance with 'undolevels'. Actually discarding the undo + " history requires performing a change after setting 'undolevels' to -1 and, + " luckily, we have one we need to do (delete the extra line from the :r + " command) + let levels = &undolevels + set undolevels=-1 + silent 1delete + let &undolevels = levels + " call the autocommand for the file minus .gpg$ + silent execute ':doautocmd BufReadPost ' . fnameescape(expand(':r')) + call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufReadPost()") +endfunction + +" Function: s:GPGBufWritePre() {{{2 +" +" Handle functionality specific to saving an entire buffer to a file rather +" than saving a partial buffer +" +function s:GPGBufWritePre() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGBufWritePre()") + " call the autocommand for the file minus .gpg$ + silent execute ':doautocmd BufWritePre ' . fnameescape(expand(':r')) + call s:GPGDebug(2, 'called autocommand for ' . fnameescape(expand(':r'))) + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGBufWritePre()") +endfunction + +" Function: s:GPGEncrypt() {{{2 +" +" encrypts the buffer to all previous recipients +" +function s:GPGEncrypt() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEncrypt()") + + " store encoding and switch to a safe one + if (&fileencoding != &encoding) + let s:GPGEncoding = &encoding + let &encoding = &fileencoding + call s:GPGDebug(2, "encoding was \"" . s:GPGEncoding . "\", switched to \"" . &encoding . "\"") + else + let s:GPGEncoding = "" + call s:GPGDebug(2, "encoding and fileencoding are the same (\"" . &encoding . "\"), not switching") + endif + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGError + let blackhole = input("Message could not be encrypted! (Press ENTER)") + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") + return + endif + + " initialize GPGOptions if not happened before + if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) + let b:GPGOptions = [] + if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) + let b:GPGOptions += ["symmetric"] + let b:GPGRecipients = [] + else + let b:GPGOptions += ["encrypt"] + endif + if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) + let b:GPGOptions += ["armor"] + endif + if (exists("g:GPGPreferSign") && g:GPGPreferSign == 1) + let b:GPGOptions += ["sign"] + endif + call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) + endif + + " built list of options + let options = "" + for option in b:GPGOptions + let options = options . " --" . option . " " + endfor + + if (!exists('b:GPGRecipients')) + let b:GPGRecipients = [] + endif + + " check here again if all recipients are available in the keyring + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) + + " check if there are unknown recipients and warn + if (len(unknownrecipients) > 0) + echohl GPGWarning + echom "Please use GPGEditRecipients to correct!!" + echo + echohl None + + " Let user know whats happend and copy known_recipients back to buffer + let dummy = input("Press ENTER to quit") + endif + + " built list of recipients + if (len(recipients) > 0) + for gpgid in recipients + let options = options . " -r " . gpgid + endfor + endif + + " encrypt the buffer + let destfile = tempname() + let cmd = { 'level': 1, 'ex': "'[,']w !" } + let cmd.args = '--quiet --no-encrypt-to ' . options + let cmd.redirect = '>' . shellescape(destfile, 1) + call s:GPGExecute(cmd) + + " restore encoding + if (s:GPGEncoding != "") + let &encoding = s:GPGEncoding + call s:GPGDebug(2, "restored encoding \"" . &encoding . "\"") + endif + + if (v:shell_error) " message could not be encrypted + " Command failed, so clean up the tempfile + call delete(destfile) + echohl GPGError + let blackhole = input("Message could not be encrypted! (Press ENTER)") + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") + return + endif + + call rename(destfile, resolve(expand(''))) + setl nomodified + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEncrypt()") +endfunction + +" Function: s:GPGViewRecipients() {{{2 +" +" echo the recipients +" +function s:GPGViewRecipients() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewRecipients()") + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()") + return + endif + + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(b:GPGRecipients) + + echo 'This file has following recipients (Unknown recipients have a prepended "!"):' + " echo the recipients + for name in recipients + let name = s:GPGIDToName(name) + echo name + endfor + + " echo the unknown recipients + echohl GPGWarning + for name in unknownrecipients + let name = "!" . name + echo name + endfor + echohl None + + " check if there is any known recipient + if (len(recipients) == 0) + echohl GPGError + echom 'There are no known recipients!' + echohl None + endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewRecipients()") +endfunction + +" Function: s:GPGEditRecipients() {{{2 +" +" create a scratch buffer with all recipients to add/remove recipients +" +function s:GPGEditRecipients() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditRecipients()") + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()") + return + endif + + " only do this if it isn't already a GPGRecipients_* buffer + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) + + " save buffer name + let buffername = bufname("%") + let editbuffername = "GPGRecipients_" . buffername + + " check if this buffer exists + if (!bufexists(editbuffername)) + " create scratch buffer + execute 'silent! split ' . fnameescape(editbuffername) + + " add a autocommand to regenerate the recipients after a write + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() + else + if (bufwinnr(editbuffername) >= 0) + " switch to scratch buffer window + execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + else + " split scratch buffer window + execute 'silent! sbuffer ' . fnameescape(editbuffername) + + " add a autocommand to regenerate the recipients after a write + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishRecipientsBuffer() + endif + + " empty the buffer + silent %delete + endif + + " Mark the buffer as a scratch buffer + setlocal buftype=acwrite + setlocal bufhidden=hide + setlocal noswapfile + setlocal nowrap + setlocal nobuflisted + setlocal nonumber + + " so we know for which other buffer this edit buffer is + let b:GPGCorrespondingTo = buffername + + " put some comments to the scratch buffer + silent put ='GPG: ----------------------------------------------------------------------' + silent put ='GPG: Please edit the list of recipients, one recipient per line.' + silent put ='GPG: Unknown recipients have a prepended \"!\".' + silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically.' + silent put ='GPG: Data after recipients between and including \"(\" and \")\" is ignored.' + silent put ='GPG: Closing this buffer commits changes.' + silent put ='GPG: ----------------------------------------------------------------------' + + " get the recipients + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(getbufvar(b:GPGCorrespondingTo, "GPGRecipients")) + + " if there are no known or unknown recipients, use the default ones + if (len(recipients) == 0 && len(unknownrecipients) == 0) + if (type(g:GPGDefaultRecipients) == type([])) + let [ recipients, unknownrecipients ] = s:GPGCheckRecipients(g:GPGDefaultRecipients) + else + echohl GPGWarning + echom "g:GPGDefaultRecipients is not a Vim list, please correct this in your vimrc!" + echohl None + endif + endif + + " put the recipients in the scratch buffer + for name in recipients + let name = s:GPGIDToName(name) + silent put =name + endfor + + " put the unknown recipients in the scratch buffer + let syntaxPattern = "\\(nonexxistinwordinthisbuffer" + for name in unknownrecipients + let name = "!" . name + let syntaxPattern = syntaxPattern . "\\|" . fnameescape(name) + silent put =name + endfor + let syntaxPattern = syntaxPattern . "\\)" + + " define highlight + if (has("syntax") && exists("g:syntax_on")) + execute 'syntax match GPGUnknownRecipient "' . syntaxPattern . '"' + highlight clear GPGUnknownRecipient + highlight link GPGUnknownRecipient GPGHighlightUnknownRecipient + + syntax match GPGComment "^GPG:.*$" + execute 'syntax match GPGComment "' . s:GPGMagicString . '.*$"' + highlight clear GPGComment + highlight link GPGComment Comment + endif + + " delete the empty first line + silent 1delete + + " jump to the first recipient + silent $ + + endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditRecipients()") +endfunction + +" Function: s:GPGFinishRecipientsBuffer() {{{2 +" +" create a new recipient list from RecipientsBuffer +" +function s:GPGFinishRecipientsBuffer() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishRecipientsBuffer()") + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()") + return + endif + + " go to buffer before doing work + if (bufnr("%") != expand("")) + " switch to scratch buffer window + execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" + endif + + " delete the autocommand + autocmd! * + + " get the recipients from the scratch buffer + let recipients = [] + let lines = getline(1,"$") + for recipient in lines + let matches = matchlist(recipient, '^\(.\{-}\)\%(' . s:GPGMagicString . '(ID:\s\+\(' . s:keyPattern . '\)\s\+.*\)\=$') + + let recipient = matches[2] ? matches[2] : matches[1] + + " delete all spaces at beginning and end of the recipient + " also delete a '!' at the beginning of the recipient + let recipient = substitute(recipient, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + + " delete comment lines + let recipient = substitute(recipient, "^GPG:.*$", "", "") + + " only do this if the line is not empty + if (strlen(recipient) > 0) + let gpgid = s:GPGNameToID(recipient) + if (strlen(gpgid) > 0) + if (match(recipients, gpgid) < 0) + let recipients += [gpgid] + endif + else + if (match(recipients, recipient) < 0) + let recipients += [recipient] + echohl GPGWarning + echom "The recipient \"" . recipient . "\" is not in your public keyring!" + echohl None + endif + endif + endif + endfor + + " write back the new recipient list to the corresponding buffer and mark it + " as modified. Buffer is now for sure a encrypted buffer. + call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients) + call setbufvar(b:GPGCorrespondingTo, "&mod", 1) + call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1) + + " check if there is any known recipient + if (len(recipients) == 0) + echohl GPGError + echom 'There are no known recipients!' + echohl None + endif + + " reset modified flag + setl nomodified + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishRecipientsBuffer()") +endfunction + +" Function: s:GPGViewOptions() {{{2 +" +" echo the recipients +" +function s:GPGViewOptions() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGViewOptions()") + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()") + return + endif + + if (exists("b:GPGOptions")) + echo 'This file has following options:' + " echo the options + for option in b:GPGOptions + echo option + endfor + endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGViewOptions()") +endfunction + +" Function: s:GPGEditOptions() {{{2 +" +" create a scratch buffer with all recipients to add/remove recipients +" +function s:GPGEditOptions() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGEditOptions()") + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()") + return + endif + + " only do this if it isn't already a GPGOptions_* buffer + if (match(bufname("%"), "^\\(GPGRecipients_\\|GPGOptions_\\)") != 0 && match(bufname("%"), "\.\\(gpg\\|asc\\|pgp\\)$") >= 0) + + " save buffer name + let buffername = bufname("%") + let editbuffername = "GPGOptions_" . buffername + + " check if this buffer exists + if (!bufexists(editbuffername)) + " create scratch buffer + execute 'silent! split ' . fnameescape(editbuffername) + + " add a autocommand to regenerate the options after a write + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() + else + if (bufwinnr(editbuffername) >= 0) + " switch to scratch buffer window + execute 'silent! ' . bufwinnr(editbuffername) . "wincmd w" + else + " split scratch buffer window + execute 'silent! sbuffer ' . fnameescape(editbuffername) + + " add a autocommand to regenerate the options after a write + autocmd BufHidden,BufUnload,BufWriteCmd call s:GPGFinishOptionsBuffer() + endif + + " empty the buffer + silent %delete + endif + + " Mark the buffer as a scratch buffer + setlocal buftype=nofile + setlocal noswapfile + setlocal nowrap + setlocal nobuflisted + setlocal nonumber + + " so we know for which other buffer this edit buffer is + let b:GPGCorrespondingTo = buffername + + " put some comments to the scratch buffer + silent put ='GPG: ----------------------------------------------------------------------' + silent put ='GPG: THERE IS NO CHECK OF THE ENTERED OPTIONS!' + silent put ='GPG: YOU NEED TO KNOW WHAT YOU ARE DOING!' + silent put ='GPG: IF IN DOUBT, QUICKLY EXIT USING :x OR :bd.' + silent put ='GPG: Please edit the list of options, one option per line.' + silent put ='GPG: Please refer to the gpg documentation for valid options.' + silent put ='GPG: Lines beginning with \"GPG:\" are removed automatically.' + silent put ='GPG: Closing this buffer commits changes.' + silent put ='GPG: ----------------------------------------------------------------------' + + " put the options in the scratch buffer + let options = getbufvar(b:GPGCorrespondingTo, "GPGOptions") + + for option in options + silent put =option + endfor + + " delete the empty first line + silent 1delete + + " jump to the first option + silent $ + + " define highlight + if (has("syntax") && exists("g:syntax_on")) + syntax match GPGComment "^GPG:.*$" + highlight clear GPGComment + highlight link GPGComment Comment + endif + endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGEditOptions()") +endfunction + +" Function: s:GPGFinishOptionsBuffer() {{{2 +" +" create a new option list from OptionsBuffer +" +function s:GPGFinishOptionsBuffer() + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGFinishOptionsBuffer()") + + " guard for unencrypted files + if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) + echohl GPGWarning + echom "File is not encrypted, all GPG functions disabled!" + echohl None + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()") + return + endif + + " go to buffer before doing work + if (bufnr("%") != expand("")) + " switch to scratch buffer window + execute 'silent! ' . bufwinnr(expand("")) . "wincmd w" + endif + + " clear options and unknownOptions + let options = [] + let unknownOptions = [] + + " delete the autocommand + autocmd! * + + " get the options from the scratch buffer + let lines = getline(1, "$") + for option in lines + " delete all spaces at beginning and end of the option + " also delete a '!' at the beginning of the option + let option = substitute(option, "^[[:space:]!]*\\(.\\{-}\\)[[:space:]]*$", "\\1", "") + " delete comment lines + let option = substitute(option, "^GPG:.*$", "", "") + + " only do this if the line is not empty + if (strlen(option) > 0 && match(options, option) < 0) + let options += [option] + endif + endfor + + " write back the new option list to the corresponding buffer and mark it + " as modified + call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options) + call setbufvar(b:GPGCorrespondingTo, "&mod", 1) + + " reset modified flag + setl nomodified + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGFinishOptionsBuffer()") +endfunction + +" Function: s:GPGCheckRecipients(tocheck) {{{2 +" +" check if recipients are known +" Returns: two lists recipients and unknownrecipients +" +function s:GPGCheckRecipients(tocheck) + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGCheckRecipients()") + + let recipients = [] + let unknownrecipients = [] + + if (type(a:tocheck) == type([])) + for recipient in a:tocheck + let gpgid = s:GPGNameToID(recipient) + if (strlen(gpgid) > 0) + if (match(recipients, gpgid) < 0) + let recipients += [gpgid] + endif + else + if (match(unknownrecipients, recipient) < 0) + let unknownrecipients += [recipient] + echohl GPGWarning + echom "The recipient \"" . recipient . "\" is not in your public keyring!" + echohl None + endif + end + endfor + endif + + call s:GPGDebug(2, "recipients are: " . string(recipients)) + call s:GPGDebug(2, "unknown recipients are: " . string(unknownrecipients)) + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGCheckRecipients()") + return [ recipients, unknownrecipients ] +endfunction + +" Function: s:GPGNameToID(name) {{{2 +" +" find GPG key ID corresponding to a name +" Returns: ID for the given name +" +function s:GPGNameToID(name) + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGNameToID()") + + " ask gpg for the id for a name + let cmd = { 'level': 2 } + let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . shellescape(a:name) + let output = s:GPGSystem(cmd) + + " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, + " so convert it, if necessary + if (&encoding != "utf-8") + let output = iconv(output, "utf-8", &encoding) + endif + let lines = split(output, "\n") + + " parse the output of gpg + let pubseen = 0 + let counter = 0 + let gpgids = [] + let duplicates = {} + let choices = "The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" + for line in lines + + " check if this line has already been processed + if !has_key(duplicates, line) + let duplicates[line] = 1 + + let fields = split(line, ":") + + " search for the next uid + if pubseen + if (fields[0] == "uid") + let choices = choices . " " . fields[9] . "\n" + else + let pubseen = 0 + endif + " search for the next pub + else + if (fields[0] == "pub") + " Ignore keys which are not usable for encryption + if fields[11] !~? 'e' + continue + endif + + let identity = fields[4] + let gpgids += [identity] + if exists("*strftime") + let choices = choices . counter . ": ID: 0x" . identity . " created at " . strftime("%c", fields[5]) . "\n" + else + let choices = choices . counter . ": ID: 0x" . identity . "\n" + endif + let counter = counter+1 + let pubseen = 1 + endif + endif + endif + + endfor + + " counter > 1 means we have more than one results + let answer = 0 + if (counter > 1) + let choices = choices . "Enter number: " + let answer = input(choices, "0") + while (answer == "") + let answer = input("Enter number: ", "0") + endwhile + endif + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGNameToID()") + return get(gpgids, answer, "") +endfunction + +" Function: s:GPGIDToName(identity) {{{2 +" +" find name corresponding to a GPG key ID +" Returns: Name for the given ID +" +function s:GPGIDToName(identity) + call s:GPGDebug(3, ">>>>>>>> Entering s:GPGIDToName()") + + " TODO is the encryption subkey really unique? + + " ask gpg for the id for a name + let cmd = { 'level': 2 } + let cmd.args = '--quiet --with-colons --fixed-list-mode --list-keys ' . a:identity + let output = s:GPGSystem(cmd) + + " when called with "--with-colons" gpg encodes its output _ALWAYS_ as UTF-8, + " so convert it, if necessary + if (&encoding != "utf-8") + let output = iconv(output, "utf-8", &encoding) + endif + let lines = split(output, "\n") + + " parse the output of gpg + let pubseen = 0 + let uid = "" + for line in lines + let fields = split(line, ":") + + if !pubseen " search for the next pub + if (fields[0] == "pub") + " Ignore keys which are not usable for encryption + if fields[11] !~? 'e' + continue + endif + + let pubseen = 1 + endif + else " search for the next uid + if (fields[0] == "uid") + let pubseen = 0 + if exists("*strftime") + let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . " created at " . strftime("%c", fields[5]) . ")" + else + let uid = fields[9] . s:GPGMagicString . "(ID: 0x" . a:identity . ")" + endif + break + endif + endif + endfor + + call s:GPGDebug(3, "<<<<<<<< Leaving s:GPGIDToName()") + return uid +endfunction + +function s:GPGPreCmd() + let &shellredir = s:shellredir + let &shell = s:shell + let &shelltemp = s:shelltemp +endfunction + +function s:GPGPostCmd() + let &shellredir = s:shellredirsave + let &shell = s:shellsave + let &shelltemp = s:shelltempsave +endfunction + +" Function: s:GPGSystem(dict) {{{2 +" +" run g:GPGCommand using system(), logging the commandline and output +" Recognized keys are: +" level - Debug level at which the commandline and output will be logged +" args - Arguments to be given to g:GPGCommand +" +" Returns: command output +" +function s:GPGSystem(dict) + let commandline = printf('%s %s', s:GPGCommand, a:dict.args) + if (!empty(g:GPGHomedir)) + let commandline .= ' --homedir ' . shellescape(g:GPGHomedir) + endif + let commandline .= ' ' . s:stderrredirnull + call s:GPGDebug(a:dict.level, "command: ". commandline) + + call s:GPGPreCmd() + let output = system(commandline) + call s:GPGPostCmd() + + call s:GPGDebug(a:dict.level, "output: ". output) + return output +endfunction + +" Function: s:GPGExecute(dict) {{{2 +" +" run g:GPGCommand using :execute, logging the commandline +" Recognized keys are: +" level - Debug level at which the commandline will be logged +" args - Arguments to be given to g:GPGCommand +" ex - Ex command which will be :executed +" redirect - Shell redirect to use, if needed +" +function s:GPGExecute(dict) + let commandline = printf('%s%s %s', a:dict.ex, s:GPGCommand, a:dict.args) + if (!empty(g:GPGHomedir)) + let commandline .= ' --homedir ' . shellescape(g:GPGHomedir, 1) + endif + if (has_key(a:dict, 'redirect')) + let commandline .= ' ' . a:dict.redirect + endif + let commandline .= ' ' . s:stderrredirnull + call s:GPGDebug(a:dict.level, "command: " . commandline) + + call s:GPGPreCmd() + execute commandline + call s:GPGPostCmd() +endfunction + +" Function: s:GPGDebug(level, text) {{{2 +" +" output debug message, if this message has high enough importance +" only define function if GPGDebugLevel set at all +" +function s:GPGDebug(level, text) + if exists("g:GPGDebugLevel") && g:GPGDebugLevel >= a:level + if exists("g:GPGDebugLog") + execute "redir >> " . g:GPGDebugLog + silent echom "GnuPG: " . a:text + redir END + else + echom "GnuPG: " . a:text + endif + endif +endfunction + +" Section: Commands {{{1 + +command! GPGViewRecipients call s:GPGViewRecipients() +command! GPGEditRecipients call s:GPGEditRecipients() +command! GPGViewOptions call s:GPGViewOptions() +command! GPGEditOptions call s:GPGEditOptions() + +" Section: Menu {{{1 + +if (has("menu")) + amenu Plugin.GnuPG.View\ Recipients :GPGViewRecipients + amenu Plugin.GnuPG.Edit\ Recipients :GPGEditRecipients + amenu Plugin.GnuPG.View\ Options :GPGViewOptions + amenu Plugin.GnuPG.Edit\ Options :GPGEditOptions +endif + +" vim600: set foldmethod=marker foldlevel=0 : diff --git a/.vim/plugin/latexlivepreview.vim b/.vim/plugin/latexlivepreview.vim new file mode 100644 index 0000000..f4a315a --- /dev/null +++ b/.vim/plugin/latexlivepreview.vim @@ -0,0 +1,276 @@ +" Copyright (C) 2012 Hong Xu + +" This file is part of vim-live-preview. + +" vim-live-preview is free software: you can redistribute it and/or modify it +" under the terms of the GNU General Public License as published by the Free +" Software Foundation, either version 3 of the License, or (at your option) +" any later version. + +" vim-live-preview is distributed in the hope that it will be useful, but +" WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +" or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +" more details. + +" You should have received a copy of the GNU General Public License along with +" vim-live-preview. If not, see . + + +if v:version < 700 + finish +endif + +" Check whether this script is already loaded +if exists("g:loaded_vim_live_preview") + finish +endif +let g:loaded_vim_live_preview = 1 + +" Check mkdir feature +if (!exists("*mkdir")) + echohl ErrorMsg + echo 'vim-llp: mkdir required' + echohl None + finish +endif + +" Setup python +if (has('python3')) + let s:py_exe = 'python3' +elseif (has('python')) + let s:py_exe = 'python' +else + echohl ErrorMsg + echo 'vim-llp: python required' + echohl None + finish +endif + +let s:saved_cpo = &cpo +set cpo&vim + +let s:previewer = '' + +" Run a shell command in background +function! s:RunInBackground(cmd) + +execute s:py_exe "<< EEOOFF" + +try: + subprocess.Popen( + vim.eval('a:cmd'), + shell = True, + universal_newlines = True, + stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT) + +except: + pass +EEOOFF +endfunction + +function! s:Compile() + + if !exists('b:livepreview_buf_data') || + \ has_key(b:livepreview_buf_data, 'preview_running') == 0 + return + endif + + " Change directory to handle properly sourced files with \input and bib + " TODO: get rid of lcd + execute 'lcd ' . b:livepreview_buf_data['root_dir'] + + " Write the current buffer in a temporary file + silent exec 'write! ' . b:livepreview_buf_data['tmp_src_file'] + + call s:RunInBackground(b:livepreview_buf_data['run_cmd']) + + lcd - +endfunction + +function! s:StartPreview(...) + let b:livepreview_buf_data = {} + + let b:livepreview_buf_data['py_exe'] = s:py_exe + + " Create a temp directory for current buffer + execute s:py_exe "<< EEOOFF" +vim.command("let b:livepreview_buf_data['tmp_dir'] = '" + + tempfile.mkdtemp(prefix="vim-latex-live-preview-") + "'") +EEOOFF + + let b:livepreview_buf_data['tmp_src_file'] = + \ b:livepreview_buf_data['tmp_dir'] . + \ expand('%:p:r') + + " Guess the root file which will be compiled, using first the argument + " passed, then the first line declaration of the source file and + " eventually fallback to the current file. + " TODO: emulate -parse-first-line properly + let l:root_line = substitute(getline(1), + \ '\v^\s*\%\s*!tex\s*root\s*\=\s*(.*)\s*$', + \ '\1', '') + if (a:0 > 0) + let l:root_file = fnamemodify(a:1, ':p') + elseif (l:root_line != getline(1) && strlen(l:root_line) > 0) " TODO: existence of `% !TEX` declaration condition must be cleaned... + let l:root_file = fnamemodify(l:root_line, ':p') + else + let l:root_file = b:livepreview_buf_data['tmp_src_file'] + endif + + " Hack for complex project trees: recreate the tree in tmp_dir + " Build tree for tmp_src_file (copy of the current buffer) + let l:tmp_src_dir = fnamemodify(b:livepreview_buf_data['tmp_src_file'], ':p:h') + if (!isdirectory(l:tmp_src_dir)) + silent call mkdir(l:tmp_src_dir, 'p') + endif + " Build tree for root_file (main tex file, which might be tmp_src_file, + " ie. the current file) + if (l:root_file == b:livepreview_buf_data['tmp_src_file']) " if root file is the current file + let l:tmp_root_dir = l:tmp_src_dir + else + let l:tmp_root_dir = b:livepreview_buf_data['tmp_dir'] . fnamemodify(l:root_file, ':p:h') + if (!isdirectory(l:tmp_root_dir)) + silent call mkdir(l:tmp_root_dir, 'p') + endif + endif + + " Escape pathnames + let l:root_file = fnameescape(l:root_file) + let l:tmp_root_dir = fnameescape(l:tmp_root_dir) + let b:livepreview_buf_data['tmp_dir'] = fnameescape(b:livepreview_buf_data['tmp_dir']) + let b:livepreview_buf_data['tmp_src_file'] = fnameescape(b:livepreview_buf_data['tmp_src_file']) + + " Change directory to handle properly sourced files with \input and bib + " TODO: get rid of lcd + if (l:root_file == b:livepreview_buf_data['tmp_src_file']) " if root file is the current file + let b:livepreview_buf_data['root_dir'] = fnameescape(expand('%:p:h')) + else + let b:livepreview_buf_data['root_dir'] = fnamemodify(l:root_file, ':p:h') + endif + execute 'lcd ' . b:livepreview_buf_data['root_dir'] + + " Write the current buffer in a temporary file + silent exec 'write! ' . b:livepreview_buf_data['tmp_src_file'] + + let l:tmp_out_file = l:tmp_root_dir . '/' . + \ fnamemodify(l:root_file, ':t:r') . '.pdf' + + let b:livepreview_buf_data['run_cmd'] = + \ 'env ' . + \ 'TEXMFOUTPUT=' . l:tmp_root_dir . ' ' . + \ 'TEXINPUTS=' . l:tmp_root_dir + \ . ':' . b:livepreview_buf_data['root_dir'] + \ . ': ' . + \ s:engine . ' ' . + \ '-shell-escape ' . + \ '-interaction=nonstopmode ' . + \ '-output-directory=' . l:tmp_root_dir . ' ' . + \ l:root_file + " lcd can be avoided thanks to root_dir in TEXINPUTS + + silent call system(b:livepreview_buf_data['run_cmd']) + if v:shell_error != 0 + echo 'Failed to compile' + lcd - + return + endif + + " Enable compilation of bibliography: + let l:bib_files = split(glob(b:livepreview_buf_data['root_dir'] . '/**/*.bib')) " TODO: fails if unused bibfiles + if len(l:bib_files) > 0 + for bib_file in l:bib_files + let bib_fn = fnamemodify(bib_file, ':t') + call writefile(readfile(bib_file), + \ l:tmp_root_dir . '/' . bib_fn) " TODO: may fail if same bibfile names in different dirs + endfor + + " Update compile command with bibliography + let b:livepreview_buf_data['run_cmd'] = + \ 'env ' . + \ 'TEXMFOUTPUT=' . l:tmp_root_dir . ' ' . + \ 'TEXINPUTS=' . l:tmp_root_dir + \ . ':' . b:livepreview_buf_data['root_dir'] + \ . ': ' . + \ 'bibtex ' . l:tmp_root_dir . '/*.aux' . + \ ' && ' . + \ b:livepreview_buf_data['run_cmd'] + + silent call system(b:livepreview_buf_data['run_cmd']) + endif + if v:shell_error != 0 + echo 'Failed to compile bibliography' + lcd - + return + endif + + call s:RunInBackground(s:previewer . ' ' . l:tmp_out_file) + + lcd - + + let b:livepreview_buf_data['preview_running'] = 1 +endfunction + +" Initialization code +function! s:Initialize() + let l:ret = 0 + execute s:py_exe "<< EEOOFF" +try: + import vim + import tempfile + import subprocess + import os +except: + vim.command('let l:ret = 1') +EEOOFF + + if l:ret != 0 + return 'Python initialization failed.' + endif + + " Get the tex engine + if exists('g:livepreview_engine') + let s:engine = g:livepreview_engine + else + for possible_engine in ['pdflatex', 'xelatex'] + if executable(possible_engine) + let s:engine = possible_engine + break + endif + endfor + endif + + " Get the previewer + if exists('g:livepreview_previewer') + let s:previewer = g:livepreview_previewer + else + for possible_previewer in ['evince', 'okular'] + if executable(possible_previewer) + let s:previewer = possible_previewer + break + endif + endfor + endif + + return 0 +endfunction + + +let s:init_msg = s:Initialize() + +if type(s:init_msg) == type('') + echohl ErrorMsg + echo 'vim-live-preview: ' . s:init_msg + echohl None +endif + +unlet! s:init_msg + +command! -nargs=* LLPStartPreview call s:StartPreview() + +autocmd CursorHold,CursorHoldI,BufWritePost * call s:Compile() + +let &cpo = s:saved_cpo +unlet! s:saved_cpo + +" vim703: cc=80 +" vim:fdm=marker et ts=4 tw=78 sw=4 diff --git a/.vim/plugin/neomake.vim b/.vim/plugin/neomake.vim new file mode 100644 index 0000000..781871a --- /dev/null +++ b/.vim/plugin/neomake.vim @@ -0,0 +1,50 @@ +if exists('g:loaded_neomake') || &compatible + finish +endif +let g:loaded_neomake = 1 + +command! -nargs=* -bang -bar -complete=customlist,neomake#cmd#complete_makers + \ Neomake call neomake#Make(1, []) + +" These commands are available for clarity +command! -nargs=* -bar -complete=customlist,neomake#cmd#complete_makers + \ NeomakeProject Neomake! +command! -nargs=* -bar -complete=customlist,neomake#cmd#complete_makers + \ NeomakeFile Neomake + +command! -nargs=+ -bang -complete=shellcmd + \ NeomakeSh call neomake#ShCommand(0, ) +command! NeomakeListJobs call neomake#ListJobs() +command! -bang -nargs=1 -complete=custom,neomake#cmd#complete_jobs + \ NeomakeCancelJob call neomake#CancelJob(, 0) +command! -bang NeomakeCancelJobs call neomake#CancelJobs(0) + +command! -bang -bar -nargs=? -complete=customlist,neomake#cmd#complete_makers + \ NeomakeInfo call neomake#debug#display_info(0, ) + +command! -bang -bar NeomakeClean call neomake#cmd#clean(1) + +" Enable/disable/toggle commands. +command! -bar NeomakeToggle call neomake#cmd#toggle(g:) +command! -bar NeomakeToggleBuffer call neomake#cmd#toggle(b:) +command! -bar NeomakeToggleTab call neomake#cmd#toggle(t:) +command! -bar NeomakeDisable call neomake#cmd#disable(g:) +command! -bar NeomakeDisableBuffer call neomake#cmd#disable(b:) +command! -bar NeomakeDisableTab call neomake#cmd#disable(t:) +command! -bar NeomakeEnable call neomake#cmd#enable(g:) +command! -bar NeomakeEnableBuffer call neomake#cmd#enable(b:) +command! -bar NeomakeEnableTab call neomake#cmd#enable(t:) + +command! NeomakeStatus call neomake#cmd#display_status() + +" NOTE: experimental, no default mappings. +" NOTE: uses -addr=lines (default), and therefore negative counts do not work +" (see https://github.com/vim/vim/issues/3654). +command! -bar -count=1 NeomakeNextLoclist call neomake#list#next(, 1) +command! -bar -count=1 NeomakePrevLoclist call neomake#list#prev(, 1) +command! -bar -count=1 NeomakeNextQuickfix call neomake#list#next(, 0) +command! -bar -count=1 NeomakePrevQuickfix call neomake#list#prev(, 0) + +call neomake#setup#setup_autocmds() + +" vim: ts=4 sw=4 et diff --git a/.vim/plugin/node.vim b/.vim/plugin/node.vim new file mode 100644 index 0000000..a541292 --- /dev/null +++ b/.vim/plugin/node.vim @@ -0,0 +1,47 @@ +if exists("g:loaded_node") || &cp || v:version < 700 | finish | endif +let g:loaded_node = 1 + +let s:filetypes = ["javascript", "json", "jsx"] +if exists("g:node_filetypes") | let s:filetypes = g:node_filetypes | endif + +function! s:detect(dir) + if exists("b:node_root") | return | endif + let dir = a:dir + + while 1 + let is_node = 0 + let is_node = is_node || filereadable(dir . "/package.json") + let is_node = is_node || isdirectory(dir . "/node_modules") + if is_node | return node#initialize(dir) | endif + + let parent = fnamemodify(dir, ":h") + if parent == dir | return | endif + let dir = parent + endwhile +endfunction + +function! s:permutate(ft) + " Don't know right now how to detect javascript.jsx and other permutations + " without precomputing them in advance. Please let me know if you do. + return [a:ft, a:ft . ".*", "*." . a:ft, "*." . a:ft . ".*"] +endfunction + +function! s:flatten(list) + let values = [] + for value in a:list + if type(value) == type([]) | call extend(values, value) + else | add(values, value) + endif + endfor + return values +endfunction + +augroup Node + au! + au VimEnter * if empty(expand("")) | call s:detect(getcwd()) | endif + au BufRead,BufNewFile * call s:detect(expand(":p")) + + let s:filetype_patterns = s:flatten(map(s:filetypes, "permutate(v:val)")) + let s:filetype_patterns_joined = join(s:filetype_patterns, ",") + execute "au FileType " s:filetype_patterns_joined " call node#javascript()" +augroup end diff --git a/.vim/plugin/vmath.vim b/.vim/plugin/vmath.vim new file mode 100644 index 0000000..0e10044 --- /dev/null +++ b/.vim/plugin/vmath.vim @@ -0,0 +1,152 @@ +" Vim global plugin for math on visual regions +" Maintainer: Damian Conway +" License: This file is placed in the public domain. + +"###################################################################### +"## ## +"## To use: ## +"## ## +"## vmap ++ VMATH_YankAndAnalyse() ## +"## nmap ++ vip++ ## +"## ## +"## (or whatever keys you prefer to remap these actions to) ## +"## ## +"###################################################################### + + +" If already loaded, we're done... +if exists("loaded_vmath") + finish +endif +let loaded_vmath = 1 + +" Preserve external compatibility options, then enable full vim compatibility... +let s:save_cpo = &cpo +set cpo&vim + +" Grab visual selection and do simple math on it... +function! VMATH_YankAndAnalyse () + if &showmode + " Don't reselect the visual region if showmode is enabled + " because it will clobber the sum/avg/etc report with the + " "-- VISUAL --" message. + return "y:call VMATH_Analyse()\" + else + return "y:call VMATH_Analyse()\gv" + endif +endfunction + +" What to consider a number... +let s:NUM_PAT = '^[+-]\?\d\+\%([.]\d\+\)\?\([eE][+-]\?\d\+\)\?$' + +" How widely to space the report components... +let s:REPORT_GAP = 3 "spaces between components + +" Do simple math on current yank buffer... +function! VMATH_Analyse () + " Extract data from selection... + let selection = getreg('') + let raw_numbers = filter(split(selection), 'v:val =~ s:NUM_PAT') + let numbers = map(copy(raw_numbers), 'str2float(v:val)') + + " Results include a newline if original selection did... + let newline = selection =~ "\n" ? "\n" : "" + + " Calculate and en-register various interesting metrics... + let summation = len(numbers) ? join( numbers, ' + ') : '0' + call setreg('s', s:tidy( eval( summation ) )) " Sum --> register s + call setreg('a', s:average(raw_numbers) ) " Average --> register a + call setreg('x', s:tidy( s:max(numbers) )) " Max --> register x + call setreg('n', s:tidy( s:min(numbers) )) " Min --> register n + call setreg('r', @n . ' to ' . @x ) " Range --> register r + call setreg('c', len(numbers) ) " Count --> register c + + " Default paste buffer should depend on original contents (TODO) + call setreg('', @s ) + + " Report... + let gap = repeat(" ", s:REPORT_GAP) + highlight NormalUnderlined term=underline cterm=underline gui=underline + echohl NormalUnderlined + echo 's' + echohl NONE + echon 'um: ' . @s . gap + echohl NormalUnderlined + echon 'a' + echohl NONE + echon 'vg: ' . @a . gap + echon 'mi' + echohl NormalUnderlined + echon 'n' + echohl NONE + echon ': ' . @n . gap + echon 'ma' + echohl NormalUnderlined + echon 'x' + echohl NONE + echon ': ' . @x . gap + echohl NormalUnderlined + echon 'c' + echohl NONE + echon 'ount: ' . @c + +endfunction + +" Prettify numbers... +function! s:tidy (number) + let tidied = printf('%g', a:number) + return substitute(tidied, '[.]0\+$', '', '') +endfunction + +" Compute average with meaningful number of decimal places... +function! s:average (numbers) + " Compute average... + let summation = eval( len(a:numbers) ? join( a:numbers, ' + ') : '0' ) + let avg = 1.0 * summation / s:max([len(a:numbers), 1]) + + " Determine significant figures... + let min_decimals = 15 + for num in a:numbers + let decimals = strlen(matchstr(num, '[.]\d\+$')) - 1 + if decimals < min_decimals + let min_decimals = decimals + endif + endfor + + " Adjust answer... + return min_decimals > 0 ? printf('%0.'.min_decimals.'f', avg) + \ : string(avg) +endfunction + +" Reimplement these because the builtins don't handle floats (!!!) +function! s:max (numbers) + if !len(a:numbers) + return 0 + endif + let numbers = copy(a:numbers) + let maxnum = numbers[0] + for nextnum in numbers[1:] + if nextnum > maxnum + let maxnum = nextnum + endif + endfor + return maxnum +endfunction + +function! s:min (numbers) + if !len(a:numbers) + return 0 + endif + let numbers = copy(a:numbers) + let minnum = numbers[0] + for nextnum in numbers[1:] + if nextnum < minnum + let minnum = nextnum + endif + endfor + return minnum +endfunction + + +" Restore previous external compatibility options +let &cpo = s:save_cpo -- cgit v1.2.3