├── README.md └── ftplugin └── python.vim /README.md: -------------------------------------------------------------------------------- 1 | # FuckPEP8 2 | 3 | This plugin is intended to make life feasible for those of us who are right in the head, and use the proper tools (tabs) for indentation. It uses the awesome detectindent plugin (http://www.vim.org/scripts/script.php?script_id=1171) to detect how many spaces the poor misguided souls that originally wrote a given Python file use, replace them with tabs, and switch it back before saving. 4 | 5 | Pull requests to undo other annoying pep8 insanity are welcome :-) 6 | 7 | Special thanks to Luca Invernizzi for the initial prototype of this concept! 8 | 9 | ## Install 10 | 11 | ### With [Vundle] 12 | Add these lines to your .vimrc 13 | ```vim 14 | Bundle 'zardus/fuckpep8' 15 | Bundle 'ciaranm/detectindent' 16 | ``` 17 | And execute: 18 | ```bash 19 | vim +BundleInstall +qall 20 | ``` 21 | 22 | ### Manually 23 | First, install [detectindent] and configure some reasonable defaults. 24 | 25 | Then, install fuckpep8. 26 | 27 | Then, make sure you have filetype plugins enabled in VIM: 28 | ```vim 29 | filetype plugin on 30 | ``` 31 | That's it! 32 | 33 | [Vundle]:http://github.com/gmarik/vundle 34 | [detectindent]:http://github.com/ciaranm/detectindent 35 | -------------------------------------------------------------------------------- /ftplugin/python.vim: -------------------------------------------------------------------------------- 1 | " Name: fuckpep8 (global plugin) 2 | " Version: 1.2 3 | " Author: Yan Shoshitaishvili 4 | " Updates: http://github.com/zardus/fuckpep8 5 | " Purpose: Make life feasible in the presense of crazy space-hippies. 6 | " Necessary because there are people in the world that are not 7 | " reasonable enough to use tabs :-( 8 | " 9 | " License: You may redistribute this plugin under the same terms as Vim 10 | " itself. 11 | " 12 | " Usage: None! It works as a filetype plugin. 13 | " 14 | " Requirements: Needs the awesome detectindex plugin 15 | " (http://www.vim.org/scripts/script.php?script_id=1171) 16 | 17 | function! UndoSmartRetab() 18 | let cr = changenr() 19 | let b:tabundos[cr] = 1 20 | if !has_key(b:tabundos, "max") || cr > b:tabundos["max"] 21 | let b:tabundos["max"] = cr 22 | endif 23 | let b:tabundos["win_" . cr] = winsaveview() 24 | retab! 25 | endfunction 26 | 27 | function! TabUndoEntryCount(from_cr) 28 | if !has_key(b:tabundos, a:from_cr - 1) 29 | return 0 30 | endif 31 | return 1 + TabUndoEntryCount(a:from_cr - 1) 32 | endfunction 33 | 34 | function! TabRedoEntryCount(from_cr) 35 | if !has_key(b:tabundos, a:from_cr) 36 | return 0 37 | endif 38 | return 1 + TabRedoEntryCount(a:from_cr + 1) 39 | endfunction 40 | 41 | function! RetabSmartUndo() 42 | let num_undo = TabUndoEntryCount(changenr()) 43 | 44 | let win = winsaveview() 45 | while num_undo > 0 46 | silent! :undo 47 | let num_undo -= 1 48 | endwhile 49 | call winrestview(win) 50 | 51 | :undo 52 | endfunction 53 | 54 | function! RetabSmartRedo() 55 | let num_undo = TabRedoEntryCount(changenr()) 56 | 57 | let win = winsaveview() 58 | while num_undo > 0 59 | silent! :redo 60 | let num_undo -= 1 61 | endwhile 62 | call winrestview(win) 63 | 64 | :redo 65 | endfunction 66 | 67 | function! FixIndent() 68 | let &l:tabstop = &l:shiftwidth 69 | 70 | if &l:expandtab 71 | " echom "Fixing indent" 72 | 73 | let b:tabified = 1 74 | let b:oldtabstop = &l:tabstop 75 | setlocal noexpandtab 76 | 77 | call UndoSmartRetab() 78 | 79 | setlocal tabstop=4 80 | setlocal softtabstop=4 81 | setlocal shiftwidth=4 82 | elseif !exists('b:tabified') 83 | let b:tabified = 0 84 | endif 85 | endfunction 86 | 87 | function! UnfixIndent() 88 | if exists('b:tabified') && b:tabified 89 | set expandtab 90 | let &l:tabstop=b:oldtabstop 91 | let &l:softtabstop=b:oldtabstop 92 | 93 | call UndoSmartRetab() 94 | endif 95 | endfunction 96 | 97 | function! DictToStr(d) 98 | redir => dstr 99 | silent! echo a:d 100 | redir end 101 | 102 | return strpart(dstr, 1) 103 | endfunction 104 | 105 | function! StrToDict(dstr) 106 | sandbox let d = eval(a:dstr) 107 | return d 108 | endfunction 109 | 110 | function! SaveTabUndos(filename) 111 | call writefile([ DictToStr(b:tabundos) ], a:filename) 112 | endfunction 113 | 114 | function! LoadTabUndos(filename) 115 | if filereadable(a:filename) 116 | let b:tabundos = StrToDict(readfile(a:filename)[0]) 117 | 118 | if !has_key(b:tabundos, "max") || b:tabundos["max"] > undotree()["seq_last"] 119 | " our undo file probably got invalidated from under us 120 | "echom "Clearing tabundos" 121 | let b:tabundos = { } 122 | endif 123 | else 124 | let b:tabundos = { } 125 | endif 126 | endfunction 127 | 128 | function! FuckIt() 129 | let b:tabundofile = undofile(bufname("%")) . "-fuckpep8" 130 | call LoadTabUndos(b:tabundofile) 131 | 132 | :DetectIndent 133 | call FixIndent() 134 | autocmd BufWritePre * if &filetype == "python" | call UnfixIndent() 135 | autocmd BufWritePost * if &filetype == "python" | call FixIndent() 136 | autocmd BufWritePost * if &filetype == "python" | call SaveTabUndos(b:tabundofile) 137 | 138 | map u :call RetabSmartUndo() 139 | map :call RetabSmartRedo() 140 | endfunction 141 | 142 | call FuckIt() 143 | --------------------------------------------------------------------------------