æãç³ã¿å«ãã®ç·ãä¸å¤ã§Folding freakã«ã¾ã§ãªã£ã話
ç¶ç·¨â折り畳み嫌いの男が一夜でFolding freakにまでなった話2 - cafegale
Vim Advent Calendar 2011 23æ¥ç®å¤©çèªçæ¥æ å½ã¯ç§@LeafCageã§ãã
ã¿ãªãã折り畳み(Fording)ã¯ä½¿ã£ã¦ãã§ããããï¼
ç§ã¯æãç³ã¿ãå«ãã§ããã
æãç³ã¿ããã¡ãªçç±ã¯å¤§ãã2ã¤ããã¾ãã
- è¦ãç®ãæªã
- æä½æ§ãæªã
ãã®2ã¤ãã«ã¤ã¼ã³ãã¦å¿«é©ãªæãç³ã¿ç°å¢ãæ§ç¯ãããã¨ã«ãã¾ãã
è¦ãç®ã®ã«ã¤ã¼ã³
ã¾ããè¦ãç®ã«ã¤ãã¦ã§ãã
Vimæ¨æºã®æãç³ã¿è¡¨ç¤ºã¯ã¤ã³ãã³ããå´©ãããæåã®è¡ã®å
容ãåããã«ããã¨ãããã¨ã§ç´æçã§ã¯ããã¾ããã
ããã§æãç³ã¿ããã¹ããçæããé¢æ°ãå·®ãæ¿ãã¦ãããèªåã«æã¾ããããã«è¡¨ç¤ºãå¤ãã¦ãã¾ãã¾ãããã
æ¨æºã§ã¯set foldtext=foldtext()ã«ãªã£ã¦ãã¾ããããããèªåã§ç¨æããé¢æ°ã«å·®ãæ¿ãã¾ãã
é©ç¨ä¾ã¯ä»¥ä¸ã®æãã§ãã
å
¨ã¦å±éããå³ï¼Afterã ã¨æãç³ãã§ããç¶æ
ã§ãã¤ã³ãã³ããåããï¼
æãç³ãã§ãã¦ãã¤ã³ãã³ããèå¥ã§ããå¿
è¦ãªæ
å ±ãåããããã表示ãããããã«ãªãã¾ãããï¼å¾ãã«[è¡æ°ãæãç³ã¿ã¬ãã«]ã表示ãããããã«ãªã£ã¦ãã¾ãï¼
ç§ã使ã£ãé¢æ°ã¯ä»¥ä¸ã«ããã¾ãï¼こちらã®ãã®ãåèã«ããã¦é ãã¾ããï¼ã
GitHub - LeafCage/foldCC.vim: 折り畳み関数と、現在階層を表示する機構を提供
set foldtext=FoldCCtext()ã§ãã®é¢æ°ã使ãããã«ãªãã¾ã*1ãä»ã«set foldcolumnã§ã¦ã£ã³ãã¦å·¦ç«¯ã«ãã©ã¼ã«ãã£ã³ã°ã®ã¬ã¤ããåºç¾ãããset fillcharsã®Foldã®fillæåãåé¤ãã¦ããã¨ããã§ãããã
set foldtext=FoldCCtext() set foldcolumn=5 set fillchars=vert:\|
æãç³ã¿ããã¹ãã®ä½ãæ¹ã«ã¤ãã¦ã¯:h fold-foldtextãè¦ãã¨ããã§ãããã
ããªãããããããæ¸ããã¦ãã¾ãã
æãç³ã¿ã®è²ã¯highlight Foldedã§ã左端ã®ãã©ã¼ã«ãã£ã³ã°ã®ã¬ã¤ãã¯highlight FoldColumnã§è¨å®ã§ãã¾ãã
è¦ãããããã¤ä¸ç®ã§æãç³ã¿ã¨ãããè²ãè¨å®ãã¦ããã¨ããã§ãããã
hi Folded gui=bold term=standout ctermbg=LightGrey ctermfg=DarkBlue guibg=Grey30 guifg=Grey80 hi FoldColumn gui=bold term=standout ctermbg=LightGrey ctermfg=DarkBlue guibg=Grey guifg=DarkBlue
æä½æ§ã®ã«ã¤ã¼ã³
次ã«æä½æ§ã«ã¤ãã¦ã§ãã
æ¨æºã§ã¯Foldingé¢ä¿ã«ã¯zã§å§ã¾ãã³ãã³ããç¨æããã¦ãã¾ãããæããã«æ¼ãã¥ãããç¡çã«ä½¿ãã¨å°æãç ´æããã®ã§ãã使ãã³ãã³ãã¯å¥ã®ãã¼ã«ããããã¾ãããã
noremap [space] <nop> nmap <Space> [space] noremap [space]j zj noremap [space]k zk noremap [space]n ]z noremap [space]p [z noremap [space]h zc noremap [space]l zo noremap [space]a za noremap [space]m zM noremap [space]i zMzv noremap [space]r zR noremap [space]f zf
]z[zã¯æãç³ã¿ãéããã«æãç³ã¿ããæãããã¨ãã«ä¾¿å©ã§ããããã巨大ãªæãç³ã¿ã®æ«å°¾ãå
é ã確èªãããã¨ãã«ä½¿ã£ã¦ãã¾ãã
æãç³ã¿ã®ãã©ã¦ã¸ã³ã°ã«ã¯zjzkã使ãã¾ãã
zMzvã¯ä¾¿å©ã§ããä»ç¾å¨ã«ã¼ã½ã«ãããã¨ãã以å¤ã®æãç³ã¿ãéãã¦ããã¾ãã
Folding freakã«ãªã£ã¦ããã¨èªåãç·¨éãã¦ããã¨ãã以å¤ã®ãã©ã¼ã«ãã£ã³ã°ãéãã¦ããã®ã許ããªããªã£ã¦ãã¾ããã¾ããåã«ç¾å¨å°ã®æãç³ã¿ãå±éããã®ã«ã使ãã¾ãã
ä½ããåç
§ããªããç·¨éããã¨ãã¯ã¦ã£ã³ãã¦åå²ãä½µç¨ããã¨ããã§ãããã
ä¸æ¹ã®ã¦ã£ã³ãã¦ã§ã¯æãç³ã¿ãä»æ¹ã®ã¦ã£ã³ãã¦ã§åããããã¡ã®æãç³ã¿ç®æãéããç¶æ
ã«ããã¨ãããã¨ãå¯è½ã§ãã
zfï¼æãç³ã¿ãä½æï¼ã¨zdï¼æãç³ã¿ãåé¤ï¼ã¯Folding freakã«ã¨ã£ã¦å¿
é ã³ãã³ãã§ããç§ã¯zfãã»ã¨ãã©ãã¸ã¥ã¢ã«é¸æã§ä½ã£ã¦ãã¾ããã'kana/vim-textobj-indent'*2ã使ãã¨zfiiã§åä¸ã¤ã³ãã³ãã®åºã¾ããä¸æ°ã«foldãããã¨ãã§ãã¾ãã
foldmethod
æãç³ã¿ã®è©±ãããã¨ãã«ãããã¾ãåãã«è¨ãã¹ãã§ããããç§ã¯foldmethodã¯markerã«ãã¦ãã¾ãããããã¦ããã¨'{{{'ã®ããè¡ã§ãã©ã¼ã«ãã£ã³ã°éå§ã'}}}'ã®ããè¡ã§ãã©ã¼ã«ãã£ã³ã°çµäºãã¾ãã
foldmethodã«ã¤ãã¦
manual æãç³ã¿ã¯æåã§è¨å®ããã(.viminfoãã¡ã¤ã«ã«æ
å ±ãè¨é²ãããï¼)
indent çããã¤ã³ãã³ãã®è¡ã§æãç³ã¿ãä½ãã
expr ãªãã·ã§ã³ 'foldexpr' ã§æ·±ããè¨å®ããã
marker ãã¼ã«ã¼ã§æãç³ã¿ãæå®ããã
syntax æ§æ強調表示ã®ãã¼ã¯ã¼ãã使ã£ã¦æå®ããã
diff å¤æ´ããã¦ããªãããã¹ããæãç³ãã
ãã©ã¼ã«ãã£ã³ã°ã®éå§ã¨çµäºä½ç½®ãä»ã®ç°å¢ã§éããã¨ããå¤ãããã«éããã¨ãã§ãããèªåã®æãä½ç½®ã«æãç³ã¿ãä½ããã¨ãã§ããã¨ãããã¨ã§ç§ã¯markerãããã©ã«ãã«ãã¦ãã¾ãã
set foldmethod=marker
ç§å¯å µå¨
æãç³ã¿ãè¤æ°ãã巨大ãªãã¡ã¤ã«ã§ãã°ãã°èµ·ãããã¨ã¨ããã°ãèªåãä»ã©ãã«ããã®ãåãããªããªããããããè¿·åã§ãã
ããã§è¿·åã解æ¶ããããã®ç§å¯å
µå¨ãç¨æãã¾ããã
ä»èªåãããæãç³ã¿ã¨ãã®è¦ªãæãã¦ãããé¢æ°ãFoldCCnavi()ã§ãã
ããã¯å
ã»ã©ã®ãã¡ã¤ã«ã«å«ã¾ãã¦ãã¾ãã
noremap [space]g :echo FoldCCnavi()<CR>
ãã®ããã«ãèªåãä»ã©ã®é層ã«ããã®ããã³ãã³ãã©ã¤ã³ã§æãã¦ããã¾ãã
ï¼ãã®ä¾ã ã¨ãããã¼ããããã¨ããæãç³ã¿ã®ä¸ã®ããè¨å®ã»fixã«è¿ããã¨ããæãç³ã¿ã®ä¸ã®ã*ã§æ¬¡ã®åèªã«ç§»åãããªããã¨ããæãç³ã¿ã«ãããã¨ãåããã¾ãï¼
èªåãã©ã®æãç³ã¿ã®ä¸ã«ããã®ãåããã°èªåã®ä½ç½®ã«ã¤ãã¦ããããã®è¦å½ãã¤ãããã¾ãããã¤ã§ãèªåã®ä½ç½®ã確èªã§ããã®ã§ã巨大ãªãã¡ã¤ã«ãç·¨éã»é²è¦§ãã¦ããã¨ããå¦ãªå®å¿æãããã¾ãã
zfã§æ¿å ¥ãããæãç³ã¿ã/*ã*/ã®ã³ã¡ã³ãã§å²ã¾ãã¦ãã®ãæ°ã«ãããªãå ´å
'commentstring'ãè¨å®ãã¾ãããã
ä½ãè¨å®ãã¦ããªããã¡ã¤ã«ã ã¨zfã§/*{{{*/ã/*}}}*/ãæ¿å
¥ããã¾ãã
ããã¯æ¨æºã§set commentstring=/*%s*/ã«ãªã£ã¦ããããã§ããï¼ããã¯å¯¾è±¡æåå%sã/*ã¨*/ã§æãã ãã³ã¡ã³ãã§ããã¨ãããã¨ã示ãã¦ãã¾ãï¼
ãã¡ã¤ã«ã¿ã¤ãå®ç¾©ãã¡ã¤ã«(.vim/ftdetect/ã®ä¸ã«ãããã¡ã¤ã«ï¼ãautocmdã§ç¹å®ã®ãã¡ã¤ã«ã®commentstringãè¨å®ãã¾ãããã
ä¾ãã°ã.vimperatorãã¡ã¤ã«ã§ã¯vimã¨åãã³ã¡ã³ãå½¢å¼ã使ãããã®ã§ã
autocmd BufRead,BufNewFile .vimperatorrc setfiletype Vimperator|setlocal commentstring=\ \"%s
ã¨ãã¾ãã
ã¾ããä½ãè¨å®ãã¦ããªããã¡ã¤ã«ãéããã¨ãã®æ¨æºã³ã¡ã³ãã¢ã¦ãå½¢å¼ã/* */ã§ãããã¨ã¯æ»
å¤ã«ãªãã®ã§ã.vimrcã«ä»¥ä¸ã®ããã«æ¸ãã¦ããã¾ãã
set commentstring=%s
ããã§ãä½ãè¨å®ãã¦ããªããã¡ã¤ã«ã«ã¯zfã§ç´ç²ãªfoldmarkerã®ã¿ãæ¿å ¥ãããããã«ãªãã¾ããã
ç§ã¯ãã¤ãæè¿Foldingã«ã¤ãã¦è¨å®ãæ´ãã¾ããããããã¾ã§ã¨ãã¡ã¤ã«æä½ã®ç°å¢ã大ããå¤ããã¾ããã
Foldingãæ´»ç¨ããã¨1ã¤ã®ãã¡ã¤ã«ã§é層çãªããã¹ããæ¸ãã¦ç®¡çã楽ã«ãªãã¾ãã
ãã¾ã
unite-outlineã«æãç³ã¿ã表示ããæ©è½ãããããã§ãã
:Unite outline:foldingsã§æãç³ã¿ä¸è¦§ã表示ã§ããããã§ãããä½è
æ§ã«ããã¨åºæ¥ãè¯ããããªãã®ã§éæ¨å¥¨ã®æ©è½ãããã§ãã
24æ¥ç®ãã¤ã´ã®æ
å½ã¯@hinagishiããã§ãã
ããã§ã¯è¯ãã¯ãªã¹ãã¹ã¤ã´ãã
*1:ãã®é¢æ°ã¯ãã¾ãã«å¹ ãçãã¦ã£ã³ãã¦ã§ä½¿ãã¨è¡¨ç¤ºããããããªãã¾ãã
*2:'kana/vim-textobj-user'(https://github.com/kana/vim-textobj-user)ãå¿ è¦ã§ã