vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

racket.vim (2913B)


      1 " Vim filetype plugin
      2 " Language:             Racket
      3 " Maintainer:           D. Ben Knoble <ben.knoble+github@gmail.com>
      4 " Previous Maintainer:  Will Langstroth <will@langstroth.com>
      5 " URL:                  https://github.com/benknoble/vim-racket
      6 " Last Change:          2025 Aug 09
      7 " 2026 Mar 31 by Vim project: use shellescape for the K mapping
      8 " 2026 Apr 01 by Vim project: make K mapping more robust for shell injection
      9 " 2026 Jun 27 by Vim Project: add recommended style guard
     10 
     11 if exists("b:did_ftplugin")
     12   finish
     13 endif
     14 let b:did_ftplugin = 1
     15 
     16 let s:cpo_save = &cpo
     17 set cpo&vim
     18 
     19 " quick hack to allow adding values
     20 setlocal iskeyword=@,!,#-',*-:,<-Z,a-z,~,_,94
     21 
     22 if get(g:, 'racket_recommended_style',
     23       \ get(g:, 'filetype_recommended_style', 1))
     24   setlocal shiftwidth=2 softtabstop=2
     25 endif
     26 
     27 " Enable auto begin new comment line when continuing from an old comment line
     28 setlocal comments=:;;;;,:;;;,:;;,:;
     29 setlocal formatoptions+=r
     30 
     31 setlocal commentstring=;;\ %s
     32 
     33 setlocal formatprg=raco\ fmt
     34 
     35 " Undo our settings when the filetype changes away from Racket
     36 " (this should be amended if settings/mappings are added above!)
     37 let b:undo_ftplugin =
     38       \  "setlocal iskeyword< shiftwidth< softtabstop< comments< formatoptions< formatprg<"
     39       \. " | setlocal commentstring<"
     40 
     41 if !exists("no_plugin_maps") && !exists("no_racket_maps")
     42   " Simply setting keywordprg like this works:
     43   "    setlocal keywordprg=raco\ docs
     44   " but then vim says:
     45   "    "press ENTER or type a command to continue"
     46   " We avoid the annoyance of having to hit enter by remapping K directly.
     47   function s:RacketDoc(word) abort
     48     execute 'silent !raco docs --' shellescape(a:word, v:true)
     49     redraw!
     50   endfunction
     51   nnoremap <buffer> <Plug>RacketDoc :call <SID>RacketDoc(expand('<cword>'))<CR>
     52   nmap <buffer> K <Plug>RacketDoc
     53 
     54   " For the visual mode K mapping, it's slightly more convoluted to get the
     55   " selected text:
     56   function! s:Racket_visual_doc()
     57     try
     58       let l:old_a = @a
     59       normal! gv"ay
     60       call system("raco docs -- ". string(shellescape(@a)))
     61       redraw!
     62       return @a
     63     finally
     64       let @a = l:old_a
     65     endtry
     66   endfunction
     67 
     68   xnoremap <buffer> <Plug>RacketDoc :call <SID>Racket_visual_doc()<cr>
     69   xmap <buffer> K <Plug>RacketDoc
     70 
     71   let b:undo_ftplugin .=
     72       \  " | silent! execute 'nunmap <buffer> K'"
     73       \. " | silent! execute 'xunmap <buffer> K'"
     74 endif
     75 
     76 if (has("gui_win32") || has("gui_gtk")) && !exists("b:browsefilter")
     77   let b:browsefilter =
     78         \  "Racket Source Files (*.rkt, *.rktl)\t*.rkt;*.rktl\n"
     79   if has("win32")
     80     let b:browsefilter .= "All Files (*.*)\t*\n"
     81   else
     82     let b:browsefilter .= "All Files (*)\t*\n"
     83   endif
     84   let b:undo_ftplugin .= " | unlet! b:browsefilter"
     85 endif
     86 
     87 if exists("loaded_matchit") && !exists("b:match_words")
     88   let b:match_words = '#|:|#'
     89   let b:undo_ftplugin .= " | unlet! b:match_words"
     90 endif
     91 
     92 let &cpo = s:cpo_save
     93 unlet s:cpo_save