Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VIM 24.99 KB | None | 0 0
  1. "=====================================================================
  2. " Vundle stuff
  3. "=====================================================================
  4. set nocompatible               " be iMproved
  5. filetype off                   " required!
  6.  
  7. set rtp+=~/.vim/bundle/vundle/
  8. set rtp+=/usr/local/go/misc/vim " install go stuff while we're at it
  9. call vundle#rc()
  10.  
  11. " let Vundle manage Vundle
  12. " required!
  13. Bundle 'gmarik/vundle'
  14.  
  15. " My Bundles here:
  16. "
  17. " original repos on github
  18. Bundle 'tpope/vim-fugitive'
  19. "Bundle 'https://github.com/wincent/Command-T.git'
  20. Bundle "kien/ctrlp.vim"
  21. Bundle 'taglist.vim'
  22. Bundle 'altercation/vim-colors-solarized'
  23. "Bundle 'godlygeek/tabular'
  24. Bundle "junegunn/vim-easy-align"
  25. " GitGutter shows a sign on the left to show what lines in a file have changed
  26. "Bundle "airblade/vim-gitgutter"
  27. Bundle 'scrooloose/nerdtree'
  28.  
  29. " Magic completion for c like languages, python
  30. "Bundle "scrooloose/syntastic"
  31.  
  32. "Bundle "Valloric/YouCompleteMe"
  33.  
  34. " Snippets
  35. Bundle "SirVer/ultisnips"
  36. " Snippets for twitter bootstrap
  37.  
  38. " Easymotion
  39. Bundle "Lokaltog/vim-easymotion"
  40.  
  41. " Matchit (lets me match html tags with %)
  42. Bundle "matchit.zip"
  43.  
  44. " Ack.vim for in editor searching the quickfix list
  45. Bundle "mileszs/ack.vim"
  46. " Same thing, but using 'ag' instead of 'ack'
  47. Bundle "rking/ag.vim"
  48.  
  49. " Named bookmarks. Let's try this out.
  50. "Bundle "AndrewRadev/simple_bookmarks.vim"
  51.  
  52. " Vim Start Screen (with recently opened files)
  53. Bundle "mhinz/vim-startify"
  54.  
  55. Bundle "scrooloose/nerdcommenter"
  56.  
  57. " Automatically change working dir to match project (where the .git folder is
  58. " found)
  59. Bundle "airblade/vim-rooter"
  60.  
  61. " Local vimrc
  62. Bundle "embear/vim-localvimrc"
  63.  
  64. " Python text objects
  65. Bundle "michaeljsmith/vim-indent-object"
  66.  
  67. " Surround, repeat
  68. Bundle "tpope/vim-surround"
  69. Bundle "tpope/vim-repeat"
  70.  
  71. " Airline -- lightweight statusbar line
  72. Bundle "bling/vim-airline"
  73.  
  74. Bundle "JavaScript-Indent"
  75.  
  76. " Lets you split or join things -- <div>a</div> onto 3 lines, for example
  77. Bundle "AndrewRadev/splitjoin.vim"
  78.  
  79. " ...
  80. " Once you add your source above, do reload vim and do :BundleInstall
  81.  
  82. filetype plugin indent on     " required!
  83.  
  84.  
  85. " =====================================================================
  86. " Personal preferences
  87. " =====================================================================
  88. " Change leader to be ,
  89. let mapleader = ","
  90.  
  91. " Startify settings
  92. let g:startify_bookmarks = [
  93.            \ '~/.vimrc',
  94.            \ '~/dev/config',
  95.            \ ] " Always show this in Startify
  96. let g:startify_skiplist = [
  97.            \ 'COMMIT_EDITMSG',
  98.            \ $VIMRUNTIME .'/doc',
  99.            \ 'bundle/.*/doc'
  100.            \ ]
  101. nnoremap <leader>s :Startify<CR>
  102. let g:startify_files_number = 20
  103. let g:startify_custom_header = map(split(system('fortune | cowsay'), '\n'), '" ". v:val') + ['','']
  104.  
  105.  
  106. "let g:UltiSnipsSnippetsDir="~/.vim/mysnippets"
  107. let g:UltiSnipsExpandTrigger="<c-j>"
  108. let g:UltiSnipsJumpForwardTrigger="<c-j>"
  109. let g:UltiSnipsJumpBackwardTrigger="<c-k>"
  110. let g:UltiSnipsListSnippets="<c-l>"
  111. let g:UltiSnipsEditSplit="vertical"
  112. nnoremap <C-e> :UltiSnipsEdit<CR>
  113.  
  114. let g:gitgutter_enabled = 1
  115.  
  116. let g:rooter_patterns = ['Rakefile', '.git/', 'buildenv.mk']
  117.  
  118. " Add bufferlist to top (unless tabs are open)
  119. let g:airline#extensions#tabline#enabled = 1
  120. " Make it just show the filename, not the full path
  121. let g:airline#extensions#tabline#fnamemod = ':t'
  122. let NERDTreeIgnore = ['\.pyc$', '\.class$']
  123.  
  124. set cursorline
  125.  
  126. set ttymouse=xterm2
  127. if has("gui_running")
  128.     set mouse=a
  129. else
  130.     set mouse=
  131. endif
  132. set ff=unix
  133. set ignorecase
  134. set smartcase
  135.  
  136. " fix slight delay after pressing ESC then O
  137. " http://ksjoberg.com/vim-esckeys.html
  138. " set noesckeys
  139. set timeout timeoutlen=1000 ttimeoutlen=100
  140.  
  141. " So, after building from source, my backspace got messed up. Perhaps
  142. " the debian repo changes the default behavior here? Weird. This fixes:
  143. set backspace=indent,eol,start
  144.  
  145. "colorscheme torte
  146. syntax enable
  147. set background=dark
  148. colorscheme solarized
  149. let g:solarized_termcolors=256
  150.  
  151. syntax on
  152. set guioptions-=m
  153. " Don't litter my darn folders!:
  154. set nobackup
  155. set nowritebackup
  156. set noswapfile
  157. " Indentation prefs:
  158. set tabstop=4
  159. set shiftwidth=4
  160. set expandtab
  161. " Other:
  162. set hlsearch
  163. set incsearch
  164. set number
  165. set relativenumber
  166. set showcmd " when visually selecting N lines, you'll see that number in the lower right
  167. " This beauty remembers where you were the last time you edited the file, and returns to the same position:
  168. au BufReadPost * if line("'\"") > 0|if line("'\"") <= line("$")|exe("norm '\"")|else|exe "norm $"|endif|endif
  169. " Make F2 map to toggle numbers:
  170. nnoremap <F2> :set nonumber!<CR>
  171. :set guioptions-=T  "remove toolbar
  172. "set autochdir " current dir given by file
  173. " Make 'Y' yank to the end of the line:
  174. nnoremap Y y$
  175. " <C-y> will now do an OS level copy:
  176. map <C-y> "+y
  177. " ... and <C-p> will paste it
  178. nnoremap <C-p> "+p
  179. set undofile
  180. set undodir=~/.vim/undodir
  181.  
  182. " Make jj and jk (in insert mode) equal escape
  183. imap jj <Esc>
  184. imap jk <Esc>
  185.  
  186. " n always searches forward (even after a ? search)
  187. noremap <silent> n /<CR>
  188. " N always searches backward (even after a / search)
  189. noremap <silent> N ?<CR>
  190.  
  191. " Exhuberant ctags install is here:
  192. let Tlist_Ctags_Cmd = '/usr/bin/ctags'
  193. let Tlist_Show_One_File = 1 " Just show the current buffer
  194. let Tlist_Use_Right_Window = 1
  195. let Tlist_WinWidth = 40
  196.  
  197. set ut=1000 " Update every 1 second (works for taglist)
  198. " Looks for tags file 1) where it us, else 2) up a folder (until root)
  199. set tags=tags;/
  200. " Omni completion brings up a menu for even just 1 item
  201. set completeopt=menuone
  202. set nocrb " Cursorbind is an evil, evil thing
  203.  
  204. " If I move my cursor to the bottom 5 lines on screen, scroll down to keep
  205. " some context
  206. "set scrolloff=5
  207.  
  208. " C-n toggles numbers on and off
  209. map <C-n> :set rnu! number!<CR>
  210.  
  211.  
  212. set wildignore=*.o,*.obj,*.pyc,*.class,*.mod.c,*.cmd,*.ko
  213. let g:netrw_list_hide= '.*\.swp$,.*\.pyc,.*\.o,.*\.obj,.*\.class,.*\.mod\.c,.*\.cmd,.*\.ko'
  214.  
  215. " =====================================================================
  216. " Filetype specific stuff
  217. " =====================================================================
  218. filetype on
  219. filetype plugin on
  220. filetype plugin indent on
  221. " Use less.vim to syntax highlight less css files:
  222. au BufNewFile,BufRead *.less set filetype=less
  223. au BufNewFile,BufRead *.in set noexpandtab " Don't expand in .in files (buildroot configuration files)
  224.  
  225.  
  226. "autocmd FileType c setlocal autoindent
  227. "autocmd FileType c setlocal cindent
  228. "autocmd FileType cpp setlocal autoindent
  229. "autocmd FileType cpp setlocal cindent
  230. "autocmd FileType php setlocal autoindent
  231. "autocmd FileType php setlocal cindent
  232. "set cindent -- this is screwing up trying to put # in normal files
  233. set autoindent
  234. "autocmd FileType make setlocal noexpandtab "Turns tabs off for makefiles
  235. " Python:
  236. " Python files will indent #comments:
  237. autocmd BufRead *.py inoremap # X<c-h>#
  238. " Autoload python crazy script
  239. "if !exists("autocommands_loaded")
  240. "   let autocommands_loaded = 1
  241. "     autocmd BufRead,BufNewFile,FileReadPost *.py source ~/.vim/python
  242. "endif
  243.  
  244. " CTRL-Tab is next tab
  245. noremap <C-Tab> :tabnext<CR>
  246. inoremap <C-Tab> <ESC>:tabnext<CR>
  247. cnoremap <C-Tab> :tabnext<CR>
  248. onoremap <C-Tab> :tabnext<CR>
  249.  
  250. " CTRL-Shift-Tab is previous tab
  251. noremap <C-S-Tab> :tabprev<CR>
  252. inoremap <C-S-Tab> <ESC>:tabprev<CR>
  253. cnoremap <C-S-Tab> :tabprev<CR>
  254. onoremap <C-S-Tab> :tabprev<CR>
  255.  
  256. " Make <C-f> go to item in the quickfix list, <C-b> go to the previous item
  257. noremap <C-b> :cp<CR>
  258. noremap <C-f> :cn<CR>
  259.  
  260. " Make it easy to get move to the far left and right windows:
  261. nnoremap L <C-w>5l
  262. nnoremap H <C-w>5h
  263. nnoremap <C-l> <C-w>l
  264. nnoremap <C-h> <C-w>h
  265. nnoremap <C-j> <C-w>j
  266. nnoremap <C-k> <C-w>k
  267.  
  268. " Abbreviations
  269. function! EatChar(pat)
  270.     let c = nr2char(getchar(0))
  271.     return (c =~ a:pat) ? '' : c
  272. endfunction
  273.  
  274. function! MakeSpacelessIabbrev(from, to)
  275.     execute "iabbrev <silent> ".a:from." ".a:to."<C-R>=EatChar('\\s')<CR>"
  276. endfunction
  277.  
  278. call MakeSpacelessIabbrev('CC_',  'CC_EXE_PATH=/opt/armgcc/bin')
  279.  
  280.  
  281. "ga* does a grep and puts results in the quickfix window
  282. " The sequence of evil hackery:
  283. " 1) Mark location globally with ("mZ")
  284. " 2) Clear the quickfix list (":call setqflist([])<CR>")
  285. " 3) Set hightlighting by making this local search too, but staying put ("*N")
  286. " 4) Run :Ag command
  287. "     (":Ag --literal --word-regexp <cword> <CR>")
  288. " 5) Hit the first result to reset focus to main window ("<CR>")
  289. " 6) Go to wherever we were before this whole mess ('Z)
  290. " 7) Reset focus to quickfix window
  291. nnoremap <silent> g* mZ:call setqflist([])<CR>*N:Ag --literal --word-regexp <cword> <CR><CR>'Z:copen<CR>
  292. nnoremap <silent> ga* mZ:call setqflist([])<CR>*N:Ag --literal --word-regexp <cword> <CR><CR>'Z:copen<CR>
  293. nnoremap <silent> gah mZ:call setqflist([])<CR>*N:Ag -G "h$" --literal --word-regexp <cword> <CR><CR>'Z:copen<CR>
  294.  
  295. "gf now uses AckFile to do results (none that start with a period)
  296. nnoremap <silent> gf :call setqflist([])<CR>:AckFile "^[^.].*<cfile>"<CR>
  297. "
  298. " <C-space> now gets rid of highlighting and then does the redraw
  299. nnoremap <silent> <C-space> :noh<CR><C-k>
  300. " above works in gui, this works in terminal. no idea why.
  301. nnoremap <silent> <C-@> :noh<CR><C-k>
  302.  
  303.  
  304. nnoremap <silent> <Leader>f :NERDTreeFind<CR>zz
  305.  
  306.  
  307. let g:CommandTMaxHeight=20
  308. let g:CommandTMaxFiles=80000
  309. let g:CommandTMaxCachedDirectories=10
  310. let g:CommandTNeverShowDotFiles=1
  311.  
  312. nnoremap <silent> <Leader>t :CtrlP<CR>
  313. let g:ctrlp_max_files = 100000
  314.  
  315.  
  316. " Ack motions ------------------------------------------------------------- {{{
  317.  
  318.  
  319. " Motions to Ack for things.  Works with pretty much everything, including:
  320. " "
  321. " "   w, W, e, E, b, B, t*, f*, i*, a*, and custom text objects
  322. " "
  323. " " Awesome.
  324. " "
  325. " " Note: If the text covered by a motion contains a newline it won't work.
  326. " Ack
  327. " " searches line-by-line.
  328.  
  329. nnoremap <silent> \a :set opfunc=<SID>AckMotion<CR>g@
  330. xnoremap <silent> \a :<C-U>call <SID>AckMotion(visualmode())<CR>
  331.  
  332. function! s:CopyMotionForType(type)
  333.     if a:type ==# 'v'
  334.         silent execute "normal! `<" . a:type . "`>y"
  335.     elseif a:type ==# 'char'
  336.         silent execute "normal! `[v`]y"
  337.     endif
  338. endfunction
  339.  
  340. function! s:AckMotion(type) abort
  341.     let reg_save = @@
  342.  
  343.     call s:CopyMotionForType(a:type)
  344.  
  345.     execute "normal! :Ack! --literal " . shellescape(@@) . "\<cr>"
  346.  
  347.     let @@ = reg_save
  348. endfunction
  349.  
  350. " }}}
  351.  
  352. " =====================================================================
  353. " Tabularize
  354. " =====================================================================
  355. "nmap <Leader>a= :Tabularize /=<CR>
  356. "vmap <Leader>a= :Tabularize /=<CR>
  357. "nmap <Leader>a: :Tabularize /:<CR>
  358. "vmap <Leader>a: :Tabularize /:<CR>
  359. "nmap <Leader>aa :Tabularize /\S\+$<CR>
  360. "vmap <Leader>aa :Tabularize /\S\+$<CR>
  361. "
  362. ""nmap <Leader>a: :Tabularize /:\zs<CR>
  363. ""vmap <Leader>a: :Tabularize /:\zs<CR>
  364. "
  365. "" This means that inserting a | will automatically Tabularize. Cool!
  366. "" https://gist.github.com/287147
  367. "inoremap <silent> <Bar>   <Bar><Esc>:call <SID>align()<CR>a
  368. "
  369. "function! s:align()
  370. "  let p = '^\s*|\s.*\s|\s*$'
  371. "  if exists(':Tabularize') && getline('.') =~# '^\s*|' && (getline(line('.')-1) =~# p || getline(line('.')+1) =~# p)
  372. "    let column = strlen(substitute(getline('.')[0:col('.')],'[^|]','','g'))
  373. "    let position = strlen(matchstr(getline('.')[0:col('.')],'.*|\s*\zs.*'))
  374. "    Tabularize/|/l1
  375. "    normal! 0
  376. "    call search(repeat('[^|]*|',column).'\s\{-\}'.repeat('.',position),'ce',line('.'))
  377. "  endif
  378. "endfunction
  379. "
  380. " =====================================================================
  381. " Easy Align
  382. " =====================================================================
  383. " Start interactive EasyAlign in visual mode (e.g. vip<Enter>)
  384. vmap <Enter> <Plug>(EasyAlign)
  385.  
  386. " Start interactive EasyAlign for a motion/text object (e.g. <Leader>aip)
  387. nmap <Leader>a <Plug>(EasyAlign)
  388.  
  389.  
  390.  
  391. "Fugitive stuff
  392. let g:fugitive_summary_format = '%s ---- (%an, %aE, %ad, %ar)'
  393. "Automatically clear fugitive buffers back out - see http://vimcasts.org/episodes/fugitive-vim-browsing-the-git-object-database/
  394. autocmd BufReadPost fugitive://* set bufhidden=delete
  395.  
  396. " Hint from: http://vimcasts.org/episodes/fugitive-vim-browsing-the-git-object-database/
  397. " Give myself parent directories '..' when browsing tree
  398. autocmd User fugitive
  399.   \ if fugitive#buffer().type() =~# '^\%(tree\|blob\)$' |
  400.   \   nnoremap <buffer> .. :edit %:h<CR> |
  401.   \ endif
  402.  
  403. nnoremap <silent> <c-g> :Gstatus<CR>
  404. nnoremap <silent> <leader>d :Gdiff<CR>
  405. nnoremap <silent> gd :diffoff<CR>
  406.  
  407.  
  408. " Status line stuff:
  409. set statusline=
  410. set statusline +=%1*\ %<%f%*            "relative path
  411. set statusline +=%1*%r%*                "read only flag
  412. set statusline +=%1*%m%*                "modified flag
  413. set statusline +=%1*\ \ \ \             "a few spaces
  414. set statusline+=%3*%{fugitive#statusline()} " fugitive git workspace
  415. set statusline +=%2*%=%5l%*             "current line
  416. set statusline +=%2*/%L%*               "total lines
  417. set statusline +=%2*%4v\ %*             "virtual column number
  418. set statusline +=%2*0x%04B\ %*          "character under cursor
  419. " Colors for statusline
  420. hi User1 guifg=White guibg=DarkBlue ctermfg=White ctermbg=DarkBlue
  421. hi User2 guifg=LightGray guibg=DarkBlue ctermfg=LightGray ctermbg=DarkBlue
  422. hi User3 guifg=Red guibg=DarkBlue ctermfg=Red ctermbg=DarkBlue
  423. hi User5 guifg=White guibg=DarkBlue ctermfg=White ctermbg=DarkBlue
  424.  
  425. set laststatus=2  " Always show status line
  426.  
  427. " Use ranger as vim file manager
  428. function! Ranger()
  429.     " Get a temp file name without creating it
  430.     let tmpfile = substitute(system('mktemp -u'), '\n', '', '')
  431.     " Launch ranger, passing it the temp file name
  432.     silent exec '!RANGER_RETURN_FILE='.tmpfile.' ranger'
  433.     " If the temp file has been written by ranger
  434.     if filereadable(tmpfile)
  435.         " Get the selected file name from the temp file
  436.         let filetoedit = system('cat '.tmpfile)
  437.         exec 'edit '.filetoedit
  438.         call delete(tmpfile)
  439.     endif
  440.     redraw!
  441. endfunction
  442.  
  443. nmap <leader>r :call Ranger()<cr>
  444.  
  445. let g:EasyMotion_leader_key = '<Space>'
  446.  
  447. function! SaveIfValidBuffer()
  448.     let bufname = expand('%:p')
  449.     if bufname != ""
  450.         write
  451.     endif
  452. endfunction
  453. " Use <Enter> to save the file... If it's valid (ignore quickfix, etc). Add a
  454. " second <CR> at the end to keep the normal behavior
  455. "nnoremap <CR> :call SaveIfValidBuffer()<CR><CR>
  456.  
  457. "cabbrev w nope
  458.  
  459. " =====================================================================
  460. " Git Gutter stuff
  461. " =====================================================================
  462. highlight clear SignColumn " without this the column colorscheme looks funny
  463. nnoremap <leader>p :GitGutterPrevHunk<CR>
  464. nnoremap <leader>n :GitGutterNextHunk<CR>
  465.  
  466. " Local vimrc - don't ask, just take .lvimrc changes
  467. let g:localvimrc_ask=0
  468. let g:localvimrc_sandbox=0
  469.  
  470. " Python import:
  471. " This is a kinda clumsy way to guess at how to import a given thingy
  472. " Basically, hover over a word and try to figure out what the appropriate
  473. " import statement is to add -- grep through the project for import
  474. " statements and sort then by frequency, and then take the most frequent one
  475. " (You'll have to double check it -- don't worry, your cursor will go to the
  476. " import line. Two ticks ('') will take you back.)
  477. function! ImportComplete()
  478.     let word = expand("<cword>")
  479.     let output = system('git grep -h "^from.*' . word . '" | sort | uniq -c | sort -rn | head -n1 | sed "s/^ *[0-9]* *//"')
  480.     execute "normal! G?^from\<cr>"
  481.     call append(line('.'), output)
  482.     " Delete the garbage newline thingy at the end of that line
  483.     normal j$x0
  484. endfunction
  485. nnoremap <leader>i :call ImportComplete()<CR>
  486.  
  487. " http://vimcasts.org/episodes/tidying-whitespace/
  488. function! Preserve(command)
  489.   " Preparation: save last search, and cursor position.
  490.   let _s=@/
  491.   let l = line(".")
  492.   let c = col(".")
  493.   " Do the business:
  494.   execute a:command
  495.   " Clean up: restore previous search history, and cursor position
  496.   let @/=_s
  497.   call cursor(l, c)
  498. endfunction
  499. " Delete all trailing whitespace
  500. nmap _$ :call Preserve("%s/\\s\\+$//e")<CR>
  501. " Format the whole document:
  502. nmap _= :call Preserve("normal gg=G")<CR>
  503. " Delete debug printfs (c)
  504. nmap _d :call Preserve("%s/^\\s*dbg_printf(\"%s:%d reached.n\", __FUNCTION__, __LINE__);\\s*$\\n//e")<CR>
  505.  
  506. nmap gj :SplitjoinSplit<cr>
  507. nmap gk :SplitjoinJoin<cr>
  508.  
  509. " =====================================================================
  510. " ========================== PERFORCE =================================
  511. " =====================================================================
  512. function! ShowHL()
  513.     echohl Conceal | echo "Conceal Test characters."
  514.     echohl Cursor | echo "Cursor Test characters."
  515.     echohl CursorIM | echo "CursorIM Test characters."
  516.     echohl CursorColumn | echo "CursorColumn Test characters."
  517.     echohl CursorLine | echo "CursorLine Test characters."
  518.     echohl Directory | echo "Directory Test characters."
  519.     echohl DiffAdd | echo "DiffAdd Test characters."
  520.     echohl DiffChange | echo "DiffChange Test characters."
  521.     echohl DiffDelete | echo "DiffDelete Test characters."
  522.     echohl DiffText | echo "DiffText Test characters."
  523.     echohl ErrorMsg | echo "ErrorMsg Test characters."
  524.     echohl VertSplit | echo "VertSplit Test characters."
  525.     echohl Folded | echo "Folded Test characters."
  526.     echohl FoldColumn | echo "FoldColumn Test characters."
  527.     echohl SignColumn | echo "SignColumn Test characters."
  528.     echohl IncSearch | echo "IncSearch Test characters."
  529.     echohl LineNr | echo "LineNr Test characters."
  530.     echohl MatchParen | echo "MatchParen Test characters."
  531.     echohl ModeMsg | echo "ModeMsg Test characters."
  532.     echohl MoreMsg | echo "MoreMsg Test characters."
  533.     echohl NonText | echo "NonText Test characters."
  534.     echohl Normal | echo "Normal Test characters."
  535.     echohl Pmenu | echo "Pmenu Test characters."
  536.     echohl PmenuSel | echo "PmenuSel Test characters."
  537.     echohl PmenuSbar | echo "PmenuSbar Test characters."
  538.     echohl PmenuThumb | echo "PmenuThumb Test characters."
  539.     echohl Question | echo "Question Test characters."
  540.     echohl Search | echo "Search Test characters."
  541.     echohl SpecialKey | echo "SpecialKey Test characters."
  542.     echohl SpellBad | echo "SpellBad Test characters."
  543.     echohl SpellCap | echo "SpellCap Test characters."
  544.     echohl SpellLocal | echo "SpellLocal Test characters."
  545.     echohl SpellRare | echo "SpellRare Test characters."
  546.     echohl StatusLine | echo "StatusLine Test characters."
  547.     echohl StatusLineNC | echo "StatusLineNC Test characters."
  548.     echohl TabLine | echo "TabLine Test characters."
  549.     echohl TabLineFill | echo "TabLineFill Test characters."
  550.     echohl TabLineSel | echo "TabLineSel Test characters."
  551.     echohl Title | echo "Title Test characters."
  552.     echohl Visual | echo "Visual Test characters."
  553.     echohl VisualNOS | echo "VisualNOS Test characters."
  554.     echohl WarningMsg | echo "WarningMsg Test characters."
  555.     echohl WildMenu | echo "WildMenu Test characters."
  556.     echohl None
  557. endfunction
  558.  
  559. " Perforce commands:
  560. function! P4FormatOutput(output)
  561.     let lines = split(a:output,'\n')
  562.     for line in lines
  563.         let line = "   " . line
  564.  
  565.         " Green stuff:
  566.         if line =~ ' - updating'
  567.             echohl Question | echo line | echohl None
  568.         elseif line =~ ' - added'
  569.             echohl Question | echo line | echohl None
  570.         elseif line =~ ' - opened for edit'
  571.             echohl Question | echo line | echohl None
  572.         elseif line =~ ' - currently opened for edit'
  573.             echohl Question | echo line | echohl None
  574.         elseif line =~ '- was edit, reverted'
  575.             echohl Question | echo line | echohl None
  576.         elseif line =~ '- deleted as'
  577.             echohl Question | echo line | echohl None
  578.  
  579.         " Default gray
  580.         elseif line =~ ' - edit default change'
  581.             echohl None | echo line
  582.         elseif line =~ ' - add default change'
  583.             echohl None | echo line
  584.         elseif line =~ 'File(s) up-to-date.'
  585.             echohl None | echo line
  586.  
  587.         " Red
  588.         elseif line =~ 'file(s) not in client view.'
  589.             echohl WarningMsg | echo line | echohl None
  590.         elseif line =~ '- file(s) not opened on this client.'
  591.             echohl WarningMsg | echo line | echohl None
  592.  
  593.         " Underlined red
  594.         elseif line =~ ' - also opened by'
  595.             echohl SpellBad | echo line | echohl None
  596.  
  597.         " Default case
  598.         else
  599.             echohl WarningMsg | echo line | echohl None
  600.         endif
  601.     endfor
  602.     " Add a few blank lines at the end
  603.     echo ""
  604.     echo ""
  605. endfunction
  606.  
  607. function! P4Func(arg)
  608.     let refresh_qfix = 1
  609.     if $P4CLIENT == ""
  610.         echohl ErrorMsg | echo "P4CLIENT isn't set!" | echohl None
  611.         return
  612.     endif
  613.     "echo a:arg
  614.     "echo expand('%:p')
  615.     if a:arg == 'sync'
  616.         let myCommand = 'p4 sync'
  617.     elseif a:arg == 'opened'
  618.         call OpenPerf()
  619.         return
  620.     elseif a:arg == 'revert'
  621.         " I've accidentally reverted a lot of work. I'm not going to do that
  622.         " again. Reverts now make a copy of the file and put it in ~/tmp/vimbu
  623.         let output = system('mkdir -p ~/.vimbu')
  624.         let output = system('cp ' . expand('%:p') . ' ~/.vimbu/')
  625.  
  626.         let myCommand = 'p4 ' . a:arg . ' ' . expand('%:p')
  627.     elseif a:arg == 'diff'
  628.         let output = system('mkdir -p ~/tmp/p4') " temp folder for files to diff against
  629.         let myCommand = 'p4 print -o ~/tmp/p4/_ ' . expand('%:p') " Grab a perforce copy of the file
  630.     else
  631.         let myCommand = 'p4 ' . a:arg . ' ' . expand('%:p')
  632.     endif
  633.  
  634.     " run command
  635.     let output = system(myCommand)
  636.  
  637.     " Do we need to reload the current file? (and update NERDTree)
  638.     if a:arg == 'edit' || a:arg == 'revert'
  639.         " Refresh NERDTree
  640.         silent NERDTreeFind
  641.         "sleep 3 " When above command hits disk we open the wrong file. It appears that the above command is non blocking?
  642.         silent normal ro
  643.  
  644.         " Force reload of file
  645.         silent :e!
  646.  
  647.         " mark wherever I am into Z
  648.         "silent normal mZ
  649.         "silent NERDTreeFind
  650.         " Refresh the tree, reopen my file
  651.         "silent normal Ro
  652.         " Sometimes, on the above refresh, our tree shifts and we opened the
  653.         " wrong file. Let's ensure that we go back where we were:
  654.         "silent normal 'Z
  655.     elseif a:arg == 'diff'
  656.         if output =~ '^//'
  657.             " Successful checkouts will start with the remote path name,
  658.             " beginning with '//'
  659.             let diff_file = split( output )[0]
  660.             let diff_file = system( 'basename ' . diff_file )
  661.             " Tweak the string name.
  662.             "   Example was: wifi_debug_serial.c#12
  663.             "       becomes: wifi_debug_serial__12.c
  664.             " By ending with the right filetype, we preserve pretty syntax
  665.             " highlighting, etc
  666.             let diff_file = '~/tmp/p4/' . system( 'echo "' . diff_file . '" | sed "s/\.\(.*\)#\(.*\)/__\2.\1/" ')
  667.             let _ = system( 'rm -f ' . diff_file ) " Kill read only old version if it exists
  668.             let _ = system( 'cp ~/tmp/p4/_   ' . diff_file )
  669.         else
  670.             let diff_file = '/dev/null'
  671.         endif
  672.  
  673.         diffthis " original
  674.         vsplit
  675.         execute 'edit ' . diff_file
  676.         diffthis " diff_file
  677.  
  678.         cclose   " let the diff line up nicely
  679.         let refresh_qfix = 0
  680.  
  681.         wincmd l " put focus on the important window
  682.     endif
  683.  
  684.     if refresh_qfix
  685.         " Open (and refresh) Perforce version quickfix
  686.         " Then move focus back up
  687.         call OpenPerf()
  688.         wincmd k
  689.         redraw
  690.    endif
  691.  
  692.     echo '$ ' . myCommand
  693.     call P4FormatOutput(output)
  694.  
  695. endfunction
  696.  
  697. function! OpenPerf()
  698.     if $P4CLIENT == ""
  699.         echohl ErrorMsg | echo "P4CLIENT isn't set!" | echohl None
  700.         return
  701.     endif
  702.     let myCommand = 'p4 opened'
  703.  
  704.     " run command
  705.     let output = system(myCommand)
  706.  
  707.     let lines = split(output,'\n')
  708.     let qflist = [ { 'text' : 'Perforce checked out files:'} ]
  709.     for line in lines
  710.         "Each line looks like this:
  711.         " //unity/...pipr.c#3 - edit default change (text)
  712.         let parts = split(line, '#')
  713.         if(len(parts) > 1)
  714.             let text = parts[1]
  715.             " p4 where <unity path> gives output like this:
  716.             "//unity/...../pipr.c //ballen_unity_sandstone/.../pipr.c /Users/ballen/dev/ballen_unity_sandstone/common/devices/pipr/src/pipr.c
  717.             let filename = split(system('p4 where ' . parts[0]), ' ')[2]
  718.             let filename = substitute(filename, '\n', '', '') " remove newline
  719.  
  720.             " In cases of a full path, remove as much as we can
  721.             let mydir = getcwd() . '/'
  722.             if filename =~ "^" . mydir
  723.                 let filename = substitute(filename, mydir, '', '') " remove newline
  724.             endif
  725.  
  726.             let dic = {'filename' : filename, 'text' : text}
  727.             call add(qflist, dic)
  728.         endif
  729.     endfor
  730.     call setqflist(qflist)
  731.     copen " Set the focus there
  732.     return
  733. endfunction
  734.  
  735. :map <silent> <Space>p :call OpenPerf()<CR>
  736. command! -nargs=1 Perf call P4Func(<f-args>)
  737.  
  738. set clipboard=unnamed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement