2023/01/18

私のvimrc



"dein Scripts-----------------------------
if &compatible
  set nocompatible               " Be iMproved
endif

" Required:
set runtimepath+=$HOME/.cache/dein/repos/github.com/Shougo/dein.vim

" Required:
if dein#load_state(expand('~/.cache/dein'))
  call dein#begin(expand('~/.cache/dein'))

  " Let dein manage dein
  " Required:
  call dein#add(expand('~/.cache/dein/repos/github.com/Shougo/dein.vim'))

  " Add or remove your plugins here like this:
  "call dein#add('Shougo/neosnippet.vim')
  "call dein#add('Shougo/neosnippet-snippets')

  " Required:
  call dein#end()
  call dein#save_state()
endif

" Required:
filetype plugin indent on
syntax enable

call dein#add('lervag/vimtex')
call dein#add('junegunn/vim-easy-align')

" If you want to install not installed plugins on startup.
if dein#check_install()
  call dein#install()
endif

"End dein Scripts-------------------------


set textwidth=0
set number
set expandtab
set shiftwidth=4
set tabstop=4
set softtabstop=4
"set cin
set autoindent
set hlsearch
set scrolloff=3
set laststatus=2
set t_Co=256
syntax on

"mac (and windows?) font, linux font
"I think if the font is found, it will be used.
"The font setting for linux below is NOT tested yet (20210218), so maybe doesnt work.
set guifont=Menlo\ Regular:h13,Ubuntu\ Mono\ Regular\ 15

colorscheme molokai

set ffs=unix,dos,mac
set statusline=%t%m%r%h%w\ [%{&ff}]%<[%{&fenc}]%y%=\ %l,%L\ \ %P
set fileencodings=utf-8,sjis,euc-jp
set hidden
hi Comment ctermfg=246
hi CursorLine ctermbg=238
hi Visual guibg=#707050
hi LineNr guifg=#bbbbbb guibg=#293739

hi Conditional     guifg=#FF77AA               gui=bold
hi Identifier      guifg=#EE9922
hi Keyword         guifg=#FF77AA               gui=bold
hi MatchParen      guifg=#000000 guibg=#EE9922 gui=bold
hi Operator        guifg=#FF77AA
hi Repeat          guifg=#FF77AA               gui=bold
hi SpecialChar     guifg=#FF77AA               gui=bold
hi Statement       guifg=#FF77AA               gui=bold
hi StorageClass    guifg=#EE9922               gui=italic
hi Tag             guifg=#FF77AA               gui=italic
hi DiffChange      ctermfg=181   ctermbg=237
hi DiffText        ctermfg=282   ctermbg=253   cterm=bold

filetype indent on
set ruler
set cursorline
"set shortmess+=T
"set shortmess=a

"2-line command line below is related to opening zview files (*.z) without ungzipping by gzip.vim
"If command line is one line, the line is filled with warning
set cmdheight=2


"use gh, gj, gk, gl instead of hjkl-----------------------------
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
"Don't add register using x-----------------------------
nnoremap x "_x
noremap s <Nop>


"Spell checker-----------------------------
"details: https://qiita.com/crispy/items/9a49d7dc792740f062ab
set spelllang=en,cjk

fun! s:SpellConf()
  redir! => syntax
  silent syntax
  redir END

  set spell

  if syntax =~? '/<comment\>'
    syntax spell default
    syntax match SpellMaybeCode /\<\h\l*[_A-Z]\h\{-}\>/ contains=@NoSpell transparent containedin=Comment contained
  else
    syntax spell toplevel
    syntax match SpellMaybeCode /\<\h\l*[_A-Z]\h\{-}\>/ contains=@NoSpell transparent
  endif

  syntax cluster Spell add=SpellNotAscii,SpellMaybeCode
endfunc

augroup spell_check
  autocmd!
  autocmd BufReadPost,BufNewFile,Syntax * call s:SpellConf()
augroup END
"End of spell checker-----------------------------


"Commands defined by me-----------------------------
command W retab|set fenc=utf-8|w|set ff=unix|w
command Wq retab|set fenc=utf-8|w|set ff=unix|w|q
"count words
command Cw %s/\S\+//gn
"count lines
command Cl %s/^//gn
"convert 4 Spaces to 1 Tab
command St set noexpandtab | retab! 4
"convert 1 Tab to 4 Spaces
command Ts set expandtab | retab 4


"open zview files (*.z) without ungzipping by gzip.vim-----------------------------
let g:loaded_gzipPlugin=1
let g:loaded_gzip=1

"Modify vim status line
augroup my_status_line
    autocmd!
    autocmd BufEnter,BufLeave,BufWritePost * if &ff=~'dos' | hi StatusLine ctermfg=124 ctermbg=253 guifg=#af0000 guibg=#dadada | else | hi StatusLine term=bold,reverse cterm=bold,reverse ctermfg=238 ctermbg=253 gui=bold,reverse guifg=#293739 guibg=#dadada | endif
augroup END


"when searching, always use regular expression-----------------------------
nnoremap <expr> / _(":%s/<Cursor>/&/gn")


"when searching, display hit number -----------------------------
function! s:move_cursor_pos_mapping(str, ...)
    let left = get(a:, 1, "<Left>")
    let lefts = join(map(split(matchstr(a:str, '.*<Cursor>\zs.*\ze'), '.\zs'), 'left'), "")
    return substitute(a:str, '<Cursor>', '', '') . lefts
endfunction

function! _(str)
    return s:move_cursor_pos_mapping(a:str, "\<Left>")
endfunction



" Vimtex settings-----------------------------
let g:tex_flavor='latex'
let g:vimtex_compiler_latexmk = {
      \ 'background': 1,
      \ 'build_dir': '',
      \ 'continuous': 1,
      \ 'options': [''
      \],
      \}
let g:vimtex_view_method = 'skim'
let g:vimtex_quickfix_open_on_warning = 0 "0: Display only error messages without warning messages
let g:vimtex_imaps_enabled = 0 "0: dont use shortcut to input latex commad

"fun syntex()
"    syn match texComment "\(\\gsout\s\+.\|\\gsout\s*{.*}\)"
"    "syn match texRed /\(\\red\s\+.\|\\red\s*{.*}\)/
"    "hi texRed guifg=#FF0000
"endfun

"以下texRed関連の設定はあとで整理すること
"hi texRed guifg=#FF0000 ctermfg=9
"highlight link texRed Error
augroup my_tex
    autocmd!
    "    autocmd Syntax tex call syntex()
        "Below command: make a part beginning from \gsout gray in tex with my stylefile
        autocmd Syntax tex syn region xBlock start="{" end="}" contained transparent
        autocmd Syntax tex syn region texComment matchgroup=texComment start="\\gsout{" end="}" contains=xBlock
        autocmd Syntax tex syn region texComment matchgroup=texComment start="\\comment{" end="}" contains=xBlock
"        autocmd Syntax tex syn match texComment /\(\\gsout\s\+.\|\\gsout\s*{[^{}]*}\)/
        "以下の2行を有効化するほうが、highlightはうまくいく。だが、:Clearのmatchと形式が違うため、highlightされても消えない箇所が発生する。
        """autocmd Syntax tex syn region texComment matchgroup=texComment start="\\gsout{" end="}" contains=xBlock
        """autocmd Syntax tex syn region xBlock start="{" end="}" contained transparent

"        autocmd Syntax tex syn match texComment /\(\\gsout\s\+.\|\\gsout\s*{.*}\)/

    "make the text color red after \red (my original command)
        "autocmd Syntax tex syn match texStatement /\(\\red\s\+.\|\\red\s*{.*}\)/
        "もともとは下記
        """autocmd Syntax tex syn match texSection /\(\\green\s\+.\|\\green\s*{[^{}]*}\)/
        "以下の2行(もとは2行あった。今は1行)を有効化するほうが、highlightはうまくいく。だが、:Clearのmatchと形式が違うため、highlightされても消えない箇所が発生する。pythonのプログラムを使うことにしたため、大丈夫。
        autocmd Syntax tex syn region texSection matchgroup=texSection start="\\green{" end="}" contains=xBlock, texInput
"        autocmd Syntax tex syn match texTypeStyle /\(\\red\s\+.\|\\red\s*{.*}\)/

    "make the text color red after \red (my original command)
        "autocmd Syntax tex syn match texStatement /\(\\red\s\+.\|\\red\s*{.*}\)/
        "autocmd Syntax tex syn match texBoldStyle /\(\\red\s\+.\|\\red\s*{[^{}]*}\)/
        "autocmd Syntax tex syn match texStatement /\(\\red\s\+.\|\\red\s*{[^{}]*}\)/
        autocmd Syntax tex syn region texInput matchgroup=texInput start="\\memo{" end="}" contains=xBlock
"        autocmd Syntax tex syn match texInput /\(\\memo\s\+.\|\\memo\s*{[^{}]*}\)/
        "autocmd Syntax tex syn match texInput /\(\\doit\s\+.\|\\doit\s*{[^{}]*}\)/
        "autocmd Syntax tex syn match texTypeStyle /\(\\red\s\+.\|\\red\s*{.*}\)/

    "highlight not solved comment (\cmt{...}) by teachers
    "regexでのmatchよりmatchgroup...start...end...のほうがvimの動作が高速。regexだと、scroll時にちょっと引っかかる。
    autocmd Syntax tex syn region texInput matchgroup=texInput start=/\\begin{lstlisting}\[language=cmt\]/ end=/\\end{lstlisting}/
    autocmd Syntax tex syn region texInput matchgroup=texInput start=/\\begin{cmt}/ end=/\\end{cmt}/
"        autocmd Syntax tex syn match texInput /\\begin{lstlisting}\[language=cmt\]\_.\{-}\\end{lstlisting}/
"        autocmd Syntax tex syn match texInput /\\begin{cmt}\_.\{-}\\end{cmt}/
    "highlight solved comment (\cmt{...}) by teachers
    autocmd Syntax tex syn region texComment matchgroup=texComment start="\\begin{lstlisting}\[language=cmtg\]" end="\\end{lstlisting}"
    autocmd Syntax tex syn region texComment matchgroup=texComment start="\\begin{cmtg}" end="\\end{cmtg}"
"        autocmd Syntax tex syn match texComment /\\begin{lstlisting}\[language=cmtg\]\_.\{-}\\end{lstlisting}/
"        autocmd Syntax tex syn match texComment /\\begin{cmtg}\_.\{-}\\end{cmtg}/
"
        autocmd Syntax tex syn match texStatement /\\cite/
        "autocmd Syntax tex syn match texStatement /\(\\cite\s\+.\|\\cite\s*{[^{}]*}\)/

    " For tex file, larger linespace (line height) and different font is set.
    autocmd Syntax tex set linespace=6 | set guifont=Ricty-Regular:h16 "SourceHanCodeJP-Regular:h13
    " For quickfix message window height (default is 10)
    autocmd FileType qf 6wincmd_
augroup END
"syntax match texStatement /\\red\s*{.*}/
"highlight link texRed Identifier
"autocmd Syntax * syn match texRed /\(\\red\s\+.\|\\red\s*{.*}\)/
"hi Keyword guifg=#FF0000
"hi link texRed Keyword
"syn match Keyword /\(\\red\s\+.\|\\red\s*{.*}\)/

"When converting tex into html by pandoc, remove \green{}, \begin{itemize}, \end{itemize} \item \gsout{} \dd{}
    "command Clear %s/\(\\item\|\\green\s*{[^{}]*}\|\\begin{itemize}\|\\end{itemize}\|\\gsout\s*{[^{}]*}\|\~\\\\\)//g | %s/\(\\begin{lstlisting}\[language=cmtg\]\_.\{-}\\end{lstlisting}\|\\begin{cmtg}\_.\{-}\\end{cmtg}\)//g | %g/\\dd/norm!knd%
    "command Clearx %s/\\gsout/&/gn | 
"Convert tex into html with pandoc
"%:t:r means get only filename not fullpath. This is needed for browsersync --index option.
let g:texfilename = expand("%:t:r")
let g:current_dir = expand("%:h")
"function! Rpl_lstlisting()
"    "pandoc ignore options for listing style, but set it as class in html. So
"    "style in html is set by other css defined by me
"    %s/\\begin{cmt}/\\begin{lstlisting}[language=cmt]/g
"    %s/\\end{cmt}/\\end{lstlisting}/g
"    %s/\\begin{cmtg}/\\begin{lstlisting}[language=cmtg]/g
"    %s/\\end{cmtg}/\\end{lstlisting}/g
"endfunction

"let pandoc_com = "pandoc '" . texfilename . ".tex' -s -o '" . texfilename . ".html' --pdf-engine lualatex --bibliography='" . texfilename . ".bib' -N -F pandoc-crossref --citeproc -M \"crossrefYaml=" . texfilename . ".yaml\" "
""--webtex --mathjax
""lualatex -V documentclass=bxjsarticle
"let g:mod_eqref_html_com = "mod_eqref_html.py_relsym '" . texfilename . ".tex' '" . texfilename . ".html' "

"let pandoc_com_docx = "pandoc '" . texfilename . ".tex' -s -o '" . texfilename . ".docx' --pdf-engine lualatex --bibliography='" . texfilename . ".bib' -N -F pandoc-crossref --citeproc -M \"crossrefYaml=" . texfilename . ".yaml\" "
""-M tableEqns=true 
"let g:mod_eqref_docx_com = "mod_eqref_docx.py_relsym '" . texfilename . ".tex' '" . texfilename . ".docx' "
let prepost_proc = "conv_tex2.py_relsym " . texfilename . ".tex"

"Remove \green{}, \begin{itemize}, \end{itemize} \item \gsout{} \dd{} and save
"as an OTHER tex file (..._temp.tex)
    command Clear echo system(prepost_proc . " -c" )
    command Clearv echo system(prepost_proc . " -c -v" )

"Latex converter, display help.
    command Pdh echo system(prepost_proc . " -h" )

"pdf output. You can also use \ll (vimtex command).
    command Pdl echo system(prepost_proc . " -l &" )
    command Pdlv echo system(prepost_proc . " -l -v" )
    command Pdlc echo system(prepost_proc . " -l -c &" )
    command Pdlcv echo system(prepost_proc . " -l -c -v" )

"html output. Fast compiling tex is useful to check when writing.
    command Pd echo system(prepost_proc . " -p" )
    command Pdv echo system(prepost_proc . " -p -v" )
    command Pdc echo system(prepost_proc . " -p -c" )
    command Pdcv echo system(prepost_proc . " -p -c -v" )
    "command Pd call Rpl_lstlisting() | w | echo system(pandoc_com . " --csl 'mycsl.csl' --mathjax") | echo system(mod_eqref_html_com)
    "command Pdv call Rpl_lstlisting() | w | echo system(pandoc_com . " --csl 'mycsl.csl' --mathjax --verbose") | echo system(mod_eqref_html_com . " -v")

"html output for dropbox paper. Some not-converted equations should be newly placed between $$ BY YOUR HAND.
    command Pdd echo system(prepost_proc . " -d" )
    command Pddv echo system(prepost_proc . " -d -v" )
    command Pddc echo system(prepost_proc . " -d -c" )
    command Pddcv echo system(prepost_proc . " -d -c -v" )
    "command Pdd call Rpl_lstlisting() | w | echo system(pandoc_com . " --csl 'mycsl-with-title.csl' ") | echo system(mod_eqref_html_com . " -d" )
    "command Pddv call Rpl_lstlisting() | w | echo system(pandoc_com . " --csl 'mycsl-with-title.csl' --verbose") | echo system(mod_eqref_html_com . " -d -v" )

"For Test. Originally for --mimetex option, but it was removed from the new version pandoc in 2018.
    "command Pdt call Rpl_lstlisting() | w | echo system(pandoc_com . " --csl 'mycsl.csl' --webtex ") | echo system(mod_eqref_html_com)
    "http://localhost:8000/cgi-bin/mimetex.cgi?

"word output for dropbox paper
    command Pdw echo system(prepost_proc . " -w" )
    command Pdwv echo system(prepost_proc . " -w -v" )
    command Pdwc echo system(prepost_proc . " -w -c" )
    command Pdwcv echo system(prepost_proc . " -w -c -v" )
    "command Pdw call Rpl_lstlisting() | w | echo system(pandoc_com_docx . " --csl 'mycsl.csl' --mathjax") | echo system(mod_eqref_docx_com)
    "command Pdwv call Rpl_lstlisting() | w | echo system(pandoc_com_docx . " --csl 'mycsl.csl' --mathjax --verbose") | echo system(mod_eqref_docx_com . " -v")
    "command Pandoc echo pandoc_com
    command Pdf echo system(prepost_proc . " -f" )
    command Pdfv echo system(prepost_proc . " -f -v" )




" Command to launch browser-sync to auto reload html generated by pandoc e.g.
let browser_sync_com = "browser-sync start --server --files '" . texfilename . ".html' --index \"" . texfilename . ".html\" --no-notify --browser='google chrome' --logLevel silent & "
"--no-open "This option is to avoid opening browser automatically when starting browser-sync
command Bs echo browser_sync_com | echo system(browser_sync_com)
"command Bs echo browser_sync_com

"Browser sysnc kill. Killing macOS open command to open the link  in browser is also needed. So the second command is needed.
let kill_browser_sync = "ps -ef | grep browser-sync | grep '" . texfilename . ".html' | awk '{print $2}' | xargs kill"
let kill_browser_open = "ps -ef | grep open | grep 'http://localhost:30..' | awk '{print $2}' | xargs kill "
command Bsk echo kill_browser_sync | echo system(kill_browser_sync) | echo kill_browser_open | echo system(kill_browser_open)
"command Bsk echo kill_browser_sync
" kill and start browser-sync ( Bsr = BrowserSyncRestart )
command Bsr echo system(kill_browser_sync) | echo system(kill_browser_open) | echo system(browser_sync_com)
" Grep processes related to browsersync
command Bsp echo system("ps -ef | grep browser-sync | grep -v grep") | echo system("ps -ef | grep open | grep 'http://localhost:30..' | grep -v grep")
"kill all browser-sync processes
let kill_all_browser_sync = "ps -ef | grep browser-sync | awk '{print $2}' | xargs kill"
command Bska echo kill_all_browser_sync | echo system(kill_all_browser_sync) | echo kill_browser_open | echo system(kill_browser_open)





" Vim-Easy-Align settings-----------------------------
" Start interactive EasyAlign in visual mode (e.g. vipga)
xmap ga <Plug>(EasyAlign)

" Start interactive EasyAlign for a motion/text object (e.g. gaip)
nmap ga <Plug>(EasyAlign)


" Sandwich Something Selected in Visual Mode-----------------------------
" % Note: You can NOT sandwich multiple lines-----------------------------
" After visual selection, replace the selection with that one sandwiched by $
" <silent>: suppress error report, <SID>: use 's:' defined function
xnoremap <silent> s$ :call <SID>add_brackets( '$' , '$' )<CR>
" After visual selection, replace the selection with that one sandwiched by "
xnoremap <silent> s" :call <SID>add_brackets( '"' , '"' )<CR>
" After visual selection, replace the selection with that one sandwiched by '
xnoremap <silent> s' :call <SID>add_brackets( "'" , "'" )<CR>
" After visual selection, replace the selection with that one sandwiched by ()
xnoremap <silent> s( :call <SID>add_brackets( '(' , ')' )<CR>
xnoremap <silent> s) :call <SID>add_brackets( '(' , ')' )<CR>
" After visual selection, replace the selection with that one sandwiched by {}
xnoremap <silent> s{ :call <SID>add_brackets( '{' , '}' )<CR>
xnoremap <silent> s} :call <SID>add_brackets( '{' , '}' )<CR>
" After visual selection, replace the selection with that one sandwiched by []
xnoremap <silent> s[ :call <SID>add_brackets( '[' , ']' )<CR>
xnoremap <silent> s] :call <SID>add_brackets( '[' , ']' )<CR>
" After visual selection, replace the selection with that one sandwiched by <>
xnoremap <silent> s< :call <SID>add_brackets( '<' , '>' )<CR>
xnoremap <silent> s> :call <SID>add_brackets( '<' , '>' )<CR>
" After visual selection, replace the selection with that one sandwiched by <!--  --> (html comment)
xnoremap <silent> sh :call <SID>add_brackets( '<!-- ' , ' -->' )<CR>
" After visual selection, replace the selection with that one sandwiched by /*  */ (c comment)
xnoremap <silent> sc :call <SID>add_brackets( '/* ' , ' */' )<CR>
" After visual selection, replace the selection with that one sandwiched by $_{}$
xnoremap <silent> sb :call <SID>add_brackets( '\tb{' , '}' )<CR>
"       xnoremap <silent> sb :call <SID>add_brackets( '$_{' , '}$' )<CR>

" After visual selection, replace the selection with that one sandwiched by $^{}$
xnoremap <silent> sp :call <SID>add_brackets( '\tp{' , '}' )<CR>
"       xnoremap <silent> sp :call <SID>add_brackets( '$^{' , '}$' )<CR>

" After visual selection, replace the selection with that one sandwiched by \te{}
xnoremap <silent> st :call <SID>add_brackets( '\te{' , '}' )<CR>

" To make Italic, after visual selection, replace the selection with that one sandwiched by \emph{}
xnoremap <silent> se :call <SID>add_brackets( '\emph{' , '}' )<CR>
" To make the sentence gray and strike-outed, after visual selection, replace the selection with that one sandwiched by \gsout{}
xnoremap <silent> so :call <SID>add_brackets( '\gsout{' , '}' )<CR>
" To make the sentence highlighted, after visual selection, replace the selection with that one sandwiched by \hl{}
xnoremap <silent> sh :call <SID>add_brackets( '\hl{' , '}' )<CR>
" To avoid errors due to \hl and \cite or math expression, after visual selection, replace the selection with that one sandwiched by \mbox{}
xnoremap <silent> sm :call <SID>add_brackets( '\mbox{' , '}' )<CR>


" After visual selection, replace the selection with that one sandwiched by $^{}$
function! s:add_brackets(w_begin, w_end)
    " escape unnamed register
    let @y = @0
    " gv: select something selected in visual mode again in normal mode
    " "zc: change something and add it to buffer z
    silent normal gv"zc
    " modify buffer z
    let @z = a:w_begin . @z . a:w_end
    " paste (modified) buffer z
    silent normal "zp
    " return original unnamed register to unnamed register
    let @" = @y
endfunction