├── plugin └── redacted.vim ├── README.md ├── doc └── redacted.ptx └── autoload └── redacted.vim /plugin/redacted.vim: -------------------------------------------------------------------------------- 1 | " redacted.vim - The best way to ████ the ████ 2 | " Author: Daniel Ballester Marques 3 | " Version: 0.2 4 | " License: Same as Vim 5 | 6 | if exists("g:loaded_redacted") || &cp | finish | endif 7 | let g:loaded_redacted = 1 8 | 9 | augroup Redacted 10 | autocmd! 11 | autocmd BufNewFile,BufRead * call s:init() 12 | augroup END 13 | 14 | function! s:init() 15 | let b:redactedFile = expand('%:h') . "/." . expand('%:t') . ".redacted" 16 | if filereadable(b:redactedFile) 17 | let patterns = filter(readfile(b:redactedFile), 'v:val != ""') 18 | for pattern in patterns 19 | call redacted#redact(0, pattern) 20 | endfor 21 | endif 22 | endfunction 23 | 24 | function! Redact(pattern) 25 | call redacted#redact(0, a:pattern) 26 | endfunction 27 | 28 | command! -range=0 -bang Redact if 0 == 1 | 29 | \ call redacted#clear() | else | call redacted#redact(1) | endif 30 | 31 | command! RedactedW call redacted#persist() 32 | 33 | xnoremap Redact :call redacted#redact(1) 34 | nnoremap Redact :set opfunc=redacted#redactg@ 35 | 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # redacted.vim 2 | 3 | ![redacted](https://user-images.githubusercontent.com/15813674/33798938-fb1bd3e6-dd08-11e7-97bf-97c67ffe814d.png) 4 | 5 | ## How to use it 6 | 7 | Just select some text and use `:Redact` to redact it. Use `:Redact!` to clear 8 | the text. 9 | 10 | You can also map an operator with `Redact`: 11 | 12 | nmap r Redact 13 | vmap r Redact 14 | 15 | Use `:RedactedW` to persist your changes, so your file will still look the 16 | same if you close Vim and open it again. 17 | 18 | ## But *why*? 19 | 20 | I wrote Redacted because I often have to paraphrase something I wrote, but 21 | it's hard to do it when you're looking at the original words; it feels like 22 | there's no other way to say it. So I though, "it would be nice if I could read 23 | a sentence, hide it, and then try to come up with a new way to explain it 24 | without looking at the original text". So there you have it. You could also 25 | use it to make something like flash cards; you can cover the answers with 26 | Redacted and select them in visual mode when you want to see what's behind. 27 | 28 | ## See also 29 | 30 | You may also be interested in my other plugins: 31 | 32 | - [Ditto: highlight overused words](https://github.com/dbmrq/vim-ditto) :speak_no_evil: 33 | - [Dialect: project specific spellfiles](https://github.com/dbmrq/vim-dialect) :speech_balloon: 34 | - [Chalk: better fold markers](https://github.com/dbmrq/vim-chalk) :pencil2: 35 | - [Howdy: a tiny MRU start screen for Vim](https://github.com/dbmrq/vim-howdy) :wave: 36 | 37 | -------------------------------------------------------------------------------- /doc/redacted.ptx: -------------------------------------------------------------------------------- 1 | *redacted.ptx* The best way to ████ the ████. 2 | 3 | Author: Daniel Ballester Marques 4 | License: Same as Vim (see |license|) 5 | 6 | 7 | ============================================================================== 8 | COMMANDS *redacted-commands* 9 | 10 | :Redact *:Redact* 11 | ------- 12 | Redact some text. 13 | 14 | :Redact! *:Redact!* 15 | -------- 16 | Clear all redacted text. 17 | 18 | :RedactedW *:RedactedW* 19 | -------- 20 | Persist Redacted's highlighting, so your file will still look the same if you 21 | close it and open it again. 22 | 23 | 24 | ============================================================================== 25 | MAPPINGS *redacted-mappings* 26 | 27 | Redact *Redact* 28 | ------------ 29 | In normal mode it works as an operator, in visual mode it just redacts the 30 | text in the visual selection. 31 | 32 | 33 | ============================================================================== 34 | FUNCTIONS *redacted-functions* 35 | 36 | Redact('pattern') *Redact()* 37 | ------------ 38 | Use `call Redact('pattern')` to redact all text matching the specified regex 39 | pattern. 40 | 41 | 42 | vim:tw=78:ts=8:ft=help:norl: 43 | 44 | -------------------------------------------------------------------------------- /autoload/redacted.vim: -------------------------------------------------------------------------------- 1 | " redacted.vim - The best way to ████ the ████ 2 | " Author: Daniel Ballester Marques 3 | " Version: 0.2 4 | " License: Same as Vim 5 | 6 | if exists("g:autoloaded_redacted") || &cp | finish | endif 7 | let g:autoloaded_redacted = 1 8 | 9 | highlight Redacted ctermfg=black ctermbg=black guifg=black guibg=black 10 | 11 | function! s:strcut(s, l) 12 | let [s, l] = ['', 0] 13 | for c in split(a:s, '\zs') 14 | let s .= c 15 | if strlen(s) > a:l 16 | break 17 | endif 18 | endfor 19 | return s 20 | endfunction 21 | 22 | function! s:getSelectedPattern(visual) 23 | let start = a:visual ? getpos("'<") : getpos("'[") 24 | let end = a:visual ? getpos("'>") : getpos("']") 25 | let lines = getline(start[1], end[1]) 26 | if len(lines) == 0 27 | return 28 | endif 29 | let lines[-1] = s:strcut(lines[-1], end[2] - 1) 30 | let lines[0] = lines[0][start[2] - 1:] 31 | let escapedLines = [] 32 | for line in lines 33 | call add(escapedLines, escape(line, '\')) 34 | endfor 35 | return "\\V" . join(escapedLines, "\\n") 36 | endfunction 37 | 38 | function! s:getAllPatterns() 39 | let patterns = [] 40 | for pattern in get(b:, 'redacted', []) 41 | call add(patterns, pattern[1]) 42 | endfor 43 | return patterns 44 | endfunction 45 | 46 | function! redacted#redact(visual, ...) 47 | if a:0 48 | let pattern = a:1 49 | else 50 | let pattern = s:getSelectedPattern(a:visual) 51 | endif 52 | let match = [matchadd("Redacted", pattern), pattern] 53 | if !exists('b:redacted') | let b:redacted = [] | endif 54 | call add(b:redacted, match) 55 | endfunction 56 | 57 | function! redacted#clear(visual) 58 | if a:visual 59 | let allPatterns = s:getAllPatterns() 60 | let selectedPattern = s:getSelectedPattern(1) 61 | let index = index(allPatterns, selectedPattern) 62 | if index >= 0 63 | call matchdelete(b:redacted[index][0]) 64 | call remove(b:redacted, index) 65 | return 66 | endif 67 | endif 68 | for pattern in b:redacted 69 | silent! call matchdelete(pattern[0]) 70 | endfor 71 | let b:redacted = [] 72 | endfunction 73 | 74 | function! redacted#persist() 75 | let patterns = s:getAllPatterns() 76 | let error = writefile(patterns, b:redactedFile) 77 | if error == 0 78 | redraw | echo "Your Redacted patterns were saved to " . b:redactedFile 79 | else 80 | echom "There was a problem persisting your Redacted patters" 81 | endif 82 | endfunction 83 | 84 | --------------------------------------------------------------------------------