Skip to content

Commit

Permalink
Merge pull request #11 from emmanueltouzery/filetype_cheats
Browse files Browse the repository at this point in the history
add support for filetype-specific cheats
  • Loading branch information
lifepillar authored Dec 19, 2023
2 parents ae237b0 + 94bba43 commit 22c505b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ set the following variable in your `.vimrc`:
In this case, I recommend that you copy `cheat40.txt` into your `.vim` folder
and modify it to suit your needs.

You can also add filetype-specific cheats, by adding `cheat40_<filetype>.txt`
files in your `runtimepath`. If such files are present, they'll be used instead
of the builtin documentation and for that filetype, normal `cheat40.txt` files
will also be ignored.

The syntax of a cheat sheet is very simple:

- foldable sections use Vim's default markers (`{{{` and `}}}`) (see `:h
Expand Down
25 changes: 18 additions & 7 deletions autoload/cheat40.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,31 @@ function! s:split(path) abort
return map(split,'substitute(v:val,''\\\([\\,]\)'',''\1'',"g")')
endfunction

function! s:add_fname(fname)
let found = v:false
for glob in reverse(s:split(&runtimepath))
for cs in filter(map(filter(split(glob(glob), "\n"), 'v:val !~ "'.a:fname.'"'), 'v:val.s:slash()."'.a:fname.'.txt"'), 'filereadable(v:val)')
execute "$read" cs
let found = v:true
endfor
endfor
return found
endfunction

fun! cheat40#open(newtab)
let name_with_ft = "cheat40_".&ft
if a:newtab
tabnew +setlocal\ buftype=nofile\ bufhidden=hide\ nobuflisted\ noswapfile\ winfixwidth
else
botright 40vnew +setlocal\ buftype=nofile\ bufhidden=hide\ nobuflisted\ noswapfile\ winfixwidth
endif
if get(g:, 'cheat40_use_default', 1)
execute '$read' s:cheat40_dir.s:slash().'cheat40.txt'
let found_filetype = s:add_fname(name_with_ft)
if !found_filetype
if get(g:, 'cheat40_use_default', 1)
execute '$read' s:cheat40_dir.s:slash().'cheat40.txt'
endif
call s:add_fname("cheat40")
endif
for glob in reverse(s:split(&runtimepath))
for cs in filter(map(filter(split(glob(glob), "\n"), 'v:val !~ "cheat40"'), 'v:val.s:slash()."cheat40.txt"'), 'filereadable(v:val)')
execute "$read" cs
endfor
endfor
norm ggd_
setlocal foldmethod=marker foldtext=substitute(getline(v:foldstart),'\\s\\+{{{.*$','','')
execute 'setlocal foldlevel='.get(g:, 'cheat40_foldlevel', 1)
Expand Down

0 comments on commit 22c505b

Please sign in to comment.