├── .github └── workflows │ └── ci.yml ├── .gitignore ├── LICENSE ├── README.md ├── autoload └── auto_origami.vim ├── doc └── auto-origami.txt ├── plugin └── auto_origami.vim ├── scripts ├── install-ci.sh ├── test-ci.sh └── test.sh └── test ├── foldenable.vader ├── origami.vader └── variables.vader /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: [push] 3 | 4 | jobs: 5 | build-and-test: 6 | runs-on: ${{ matrix.os }} 7 | strategy: 8 | max-parallel: 4 9 | matrix: 10 | os: [ubuntu-16.04, ubuntu-18.04, macos-latest] 11 | 12 | steps: 13 | - uses: actions/checkout@master 14 | - name: Install dependencies 15 | run: ./scripts/install-ci.sh 16 | - name: Test 17 | run: ./scripts/test-ci.sh 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | doc/tags 2 | sample/* 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 David Ben Knoble 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-auto-origami 2 | 3 | [![This project is considered finished](https://img.shields.io/badge/status-finished-success.svg)](https://benknoble.github.io/status/finished/) 4 | 5 | Automatically set vim's 'foldcolumn' when folds present 6 | 7 | Check it out on [vim.org](http://www.vim.org/scripts/script.php?script_id=5601) 8 | too. 9 | 10 | ## Installation 11 | 12 | I recommend using [Pathogen](https://github.com/tpope/vim-pathogen) for managing 13 | plugins. 14 | 15 | Then it's just a quick 16 | 17 | ```bash 18 | cd ~/.vim/bundle 19 | git clone https://github.com/benknoble/vim-auto-origami.git 20 | ``` 21 | 22 | ## Quickstart 23 | 24 | Drop the following in your vimrc, and open a file with some folds 25 | ```vim 26 | augroup autofoldcolumn 27 | au! 28 | 29 | " Or whatever autocmd-events you want 30 | au CursorHold,BufWinEnter,WinEnter * AutoOrigamiFoldColumn 31 | augroup END 32 | ``` 33 | 34 | ## Usage 35 | 36 | Check out the docs (`:help auto-origami`). 37 | -------------------------------------------------------------------------------- /autoload/auto_origami.vim: -------------------------------------------------------------------------------- 1 | " auto_origami.vim Automatically setting your foldcolumn, whenever you want! 2 | " Maintainer: David Knoble 3 | " github @benknoble 4 | 5 | if exists("g:autoloaded_auto_origami") 6 | finish 7 | endif 8 | 9 | let g:autoloaded_auto_origami = 1 10 | 11 | let s:save_cpo = &cpo 12 | set cpo&vim 13 | 14 | " Revised from 15 | " http://stackoverflow.com/questions/8757168/gvim-automatic-show-foldcolumn-when-there-are-folds-in-a-file 16 | function! auto_origami#Foldcolumn() 17 | 18 | " Early exit: if 'nofoldenable' set, return the default value 19 | if ! &foldenable 20 | return g:auto_origami_default 21 | endif 22 | 23 | let l:old_belloff=&belloff " save belloff setting 24 | set belloff=error " don't beep when we cause an error 25 | let l:winview=winsaveview() " save window and cursor position 26 | 27 | let l:foldcolumn = s:HasFolds() ? 28 | \ g:auto_origami_foldcolumn : 29 | \ g:auto_origami_default 30 | 31 | let &belloff=l:old_belloff " restore belloff setting 32 | call winrestview(l:winview) " restore window/cursor position 33 | 34 | return l:foldcolumn 35 | endfunction 36 | 37 | function! s:HasFolds() 38 | let l:foldsexist=s:HasFoldsInner() 39 | 40 | if l:foldsexist 41 | return v:true 42 | else 43 | " Move to the end of the current fold and check again in case the 44 | " cursor was on the sole fold in the file when we checked 45 | if line('.')!=1 46 | keepjumps normal! [z 47 | keepjumps normal! k 48 | else 49 | keepjumps normal! ]z 50 | keepjumps normal! j 51 | endif 52 | let l:foldsexist=s:HasFoldsInner() 53 | if l:foldsexist 54 | return v:true 55 | endif 56 | end 57 | 58 | return v:false 59 | endfunction 60 | 61 | " Attempt to move between folds, checking line numbers to see if it worked. 62 | " If it did, there are folds. 63 | function! s:HasFoldsInner() 64 | if foldlevel(line('.')) > 0 65 | return 1 66 | endif 67 | let l:origline=line('.') 68 | keepjumps normal! zk 69 | if l:origline==line('.') 70 | keepjumps normal! zj 71 | if l:origline==line('.') 72 | return 0 73 | else 74 | return 1 75 | endif 76 | else 77 | return 1 78 | endif 79 | return 0 80 | endfunction 81 | 82 | let &cpo = s:save_cpo 83 | unlet s:save_cpo 84 | -------------------------------------------------------------------------------- /doc/auto-origami.txt: -------------------------------------------------------------------------------- 1 | *auto-origami.txt* Automatically set the foldcolumn 2 | *auto-origami* *vim-auto-origami* 3 | 4 | ____ __ __ ______ ___ ~ 5 | / T| T T| T / \ ~ 6 | Y o || | || |Y Y ~ 7 | | || | |l_j l_j| O | ~ 8 | | _ || : | | | | | ~ 9 | | | |l | | | l ! ~ 10 | l__j__j \__,_j l__j \___/ ~ 11 | ~ 12 | ___ ____ ____ ____ ____ ___ ___ ____ ~ 13 | / \ | \ l j / T / T| T Tl j ~ 14 | Y Y| D ) | T Y __jY o || _ _ | | T ~ 15 | | O || / | | | T || || \_/ | | | ~ 16 | | || \ | | | l_ || _ || | | | | ~ 17 | l !| . Y j l | || | || | | j l ~ 18 | \___/ l__j\_j|____jl___,_jl__j__jl___j___j|____j ~ 19 | ~ 20 | ============================================================================== 21 | CONTENTS *auto-origami-contents* 22 | 23 | 1. Intro ........................................... |auto-origami-intro| 24 | 2. Usage ........................................... |auto-origami-usage| 25 | 3. Commands ..................................... |auto-origami-commands| 26 | 4. Configuration ........................... |auto-origami-configuration| 27 | 5. License ....................................... |auto-origami-license| 28 | 6. Bugs ............................................. |auto-origami-bugs| 29 | 7. Contribution.............................. |auto-origami-contribution| 30 | 8. Changelog.................................... |auto-origami-changelog| 31 | 9. Credits ....................................... |auto-origami-credits| 32 | 33 | ============================================================================== 34 | INTRODUCTION *auto-origami-intro* 35 | 36 | Auto Origami started as a Google & StackOverflow search, evolved into a vimrc 37 | function, then finally made it's way in to its very own plugin. Its concept is 38 | simple: automatically set 'foldcolumn' when there are folds present. 39 | 40 | Why "Auto Origami"? I like Origami as a name for a plugin about folds, but 41 | someone else did too, and had used it. I tacked on "Auto" to distinguish them. 42 | 43 | ============================================================================== 44 | USAGE *auto-origami-usage* 45 | 46 | Fastest plugin use ever: > 47 | vim somefiles-with-folds 48 | :AutoOrigamiFoldColumn 49 | < 50 | 51 | If that seems like some extra typing for a plugin that has "Auto" in 52 | the name, drop this in your vimrc: > 53 | augroup auto_origami 54 | au! 55 | 56 | au CursorHold,BufWinEnter,WinEnter * AutoOrigamiFoldColumn 57 | augroup END 58 | > 59 | 60 | Of course, feel free to use any |autocmd-events| you want. 61 | 62 | ============================================================================== 63 | COMMANDS *auto-origami-commands* 64 | 65 | *:AutoOrigamiFoldColumn* 66 | 67 | Set the fold column to |g:auto_origami_foldcolumn| if folds are present, 68 | |g:auto_origami_default| otherwise. 69 | 70 | As of |v1.1.0|, |auto-origami| will always use the |g:auto_origami_default| 71 | value when 'nofoldenable' is set. With folds disabled, there should be no 72 | distracting 'foldcolumn'. 73 | 74 | ============================================================================== 75 | CONFIGURATION *auto-origami-configuration* 76 | 77 | Default values shown 78 | 79 | *g:auto_origami_foldcolumn* 80 | * 'foldcolumn' value if there are folds > 81 | let g:auto_origami_foldcolumn = 2 82 | < 83 | 84 | *g:auto_origami_default* 85 | * 'foldcolumn' value if there are no folds > 86 | let g:auto_origami_default = 0 87 | < 88 | 89 | ============================================================================== 90 | LICENSE *auto-origami-license* 91 | 92 | MIT License. Copyright © 2017 David Knoble. 93 | Full text available at 94 | https://github.com/benknoble/vim-auto-origami/blog/master/LICENSE 95 | 96 | ============================================================================== 97 | BUGS *auto-origami-bugs* 98 | 99 | At the time of this writing, no major bugs have yet been found. 100 | 101 | If you find one and want to report it, you can do so at 102 | https://github.com/benknoble/vim-auto-origami/issues 103 | 104 | ============================================================================== 105 | CONTRIBUTION *auto-origami-contribution* 106 | 107 | Do you want to submit a new feature, bugfix, or otherwise hack on Auto 108 | Origami? Submit pull requests to 109 | https://github.com/benknoble/vim-auto-origami/pulls 110 | 111 | See the code for style guidelines. 112 | 113 | ============================================================================== 114 | CHANGELOG *auto-origami-changelog* 115 | 116 | *v1.1.6* 21 May 2020 117 | * Use `:normal!` to avoid issues with mappings. 118 | 119 | *v1.1.5* 25 July 2019 120 | * Make AutoOrigamiFoldColumn a helptag 121 | 122 | *v1.1.4* 22 May 2018 123 | * Internal refactoring 124 | 125 | *v1.1.3* 16 May 2018 126 | * Fix the spelling of "colum" ("column") in variables 127 | * Enable integration testing 128 | 129 | *v1.1.2* 15 May 2018 130 | * Internal changes to default variable setup 131 | 132 | *v1.1.1* 14 April 2018 133 | * Don't load in compatible mode 134 | 135 | *v1.1.0* 19 February 2018 136 | * Exit early with default value if 'nofoldenable' is set 137 | 138 | *v1.0.1* 15 October 2017 139 | * Update Documentation to better reflect usage 140 | 141 | *v1.0.0* 27 August 2017 142 | * Initial release 143 | 144 | ============================================================================== 145 | CREDITS *auto-origami-credits* 146 | 147 | Originally developed by benknoble 148 | 149 | Git repository: https://github.com/benknoble/vim-auto-origami 150 | 151 | Special thanks to StackOverflow user @SnoringFrog for his answer to a simple 152 | question. 153 | 154 | 155 | 156 | vim:tw=78:ts=8:ft=help:norl: 157 | -------------------------------------------------------------------------------- /plugin/auto_origami.vim: -------------------------------------------------------------------------------- 1 | " auto_origami.vim Automatically setting your foldcolumn, whenever you want! 2 | " Maintainer: David Knoble 3 | " github @benknoble 4 | 5 | if exists("g:loaded_auto_origami") || &compatible 6 | finish 7 | endif 8 | 9 | let g:loaded_auto_origami = 1 10 | 11 | if !has("folding") 12 | echohl ErrorMsg 13 | echom "Auto Origami needs the +folding feature!" 14 | echohl None 15 | finish 16 | endif 17 | 18 | let s:save_cpo = &cpo 19 | set cpo&vim 20 | 21 | let g:auto_origami_foldcolumn = get(g:, 'auto_origami_foldcolumn', 2) 22 | 23 | let g:auto_origami_default = get(g:, 'auto_origami_default', 0) 24 | 25 | command AutoOrigamiFoldColumn let &foldcolumn = auto_origami#Foldcolumn() 26 | 27 | let &cpo = s:save_cpo 28 | unlet s:save_cpo 29 | -------------------------------------------------------------------------------- /scripts/install-ci.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | dir="$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd )" 6 | plugin_dir="$( dirname "$dir" )" 7 | 8 | install_vim8() { 9 | if [[ "$(uname)" = Darwin ]]; then 10 | brew update && brew install vim 11 | else 12 | sudo add-apt-repository ppa:jonathonf/vim 13 | sudo apt-get update 14 | sudo apt-get install vim 15 | fi 16 | } 17 | 18 | install_vader() { 19 | git clone https://github.com/junegunn/vader.vim.git "$plugin_dir/vader" 20 | } 21 | 22 | install() { 23 | install_vim8 24 | install_vader 25 | } 26 | 27 | main() { 28 | install 29 | } 30 | 31 | main 32 | -------------------------------------------------------------------------------- /scripts/test-ci.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | dir="$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd )" 6 | plugin_dir="$( dirname "$dir" )" 7 | 8 | check_vim8() { 9 | # check for version 8 10 | vim --version | grep -q 'IMproved 8' 11 | } 12 | 13 | run_tests() { 14 | vim -Nu <(cat << VIMRC 15 | set rtp+=$plugin_dir/vader 16 | set rtp+=$plugin_dir 17 | VIMRC 18 | ) -c "Vader! ${plugin_dir}/test/*" > /dev/null 19 | } 20 | 21 | main() { 22 | check_vim8 23 | run_tests 24 | } 25 | 26 | main 27 | -------------------------------------------------------------------------------- /scripts/test.sh: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env bash 2 | 3 | set -euo pipefail 4 | 5 | dir="$( cd "$( dirname "${BASH_SOURCE[0]}")" && pwd )" 6 | plugin_dir="$( dirname "$dir" )" 7 | 8 | run_tests() { 9 | vim -Nu <(cat << VIMRC 10 | set rtp+=$plugin_dir 11 | VIMRC 12 | ) -c "Vader! ${plugin_dir}/test/*" > /dev/null 13 | } 14 | 15 | main() { 16 | run_tests 17 | } 18 | 19 | main 20 | -------------------------------------------------------------------------------- /test/foldenable.vader: -------------------------------------------------------------------------------- 1 | Before (set up auto origami): 2 | set foldmethod=marker 3 | set nofoldenable 4 | 5 | Given (a file with no folds): 6 | This is some text 7 | 8 | Execute (AutoOrigamiFoldColumn): 9 | AutoOrigamiFoldColumn 10 | 11 | Then (assert the foldcolumn is 0): 12 | AssertEqual 0, &foldcolumn 13 | 14 | Given vim (a file with a fold): 15 | " One Fold {{{ 16 | 17 | " End }}} 18 | 19 | Execute (AutoOrigamiFoldColumn): 20 | AutoOrigamiFoldColumn 21 | 22 | Then (assert the foldcolumn is 0): 23 | AssertEqual 0, &foldcolumn 24 | 25 | Given vim (a file with nested folds): 26 | " Fold {{{ 27 | 28 | " Nested Fold {{{ 29 | " End Fold }}} 30 | 31 | " End Fold }}} 32 | 33 | Execute (AutoOrigamiFoldColumn): 34 | AutoOrigamiFoldColumn 35 | 36 | Then (assert the foldcolumn is 0): 37 | AssertEqual 0, &foldcolumn 38 | 39 | Given vim (a file with multiple folds): 40 | " Fold {{{ 41 | 42 | " Nested Fold {{{ 43 | " End Fold }}} 44 | 45 | " End Fold }}} 46 | 47 | " Fold {{{ 48 | 49 | " Nested Fold {{{ 50 | " End Fold }}} 51 | 52 | " End Fold }}} 53 | 54 | Execute (AutoOrigamiFoldColumn): 55 | AutoOrigamiFoldColumn 56 | 57 | Then (assert the foldcolumn is 0): 58 | AssertEqual 0, &foldcolumn 59 | -------------------------------------------------------------------------------- /test/origami.vader: -------------------------------------------------------------------------------- 1 | Before (set up auto origami): 2 | set foldmethod=marker 3 | set foldenable 4 | 5 | Given (a file with no folds): 6 | This is some text 7 | 8 | Execute (AutoOrigamiFoldColumn): 9 | AutoOrigamiFoldColumn 10 | 11 | Then (assert the foldcolumn is 0): 12 | AssertEqual 0, &foldcolumn 13 | 14 | Given vim (a file with a fold): 15 | " One Fold {{{ 16 | 17 | " End }}} 18 | 19 | Execute (AutoOrigamiFoldColumn): 20 | AutoOrigamiFoldColumn 21 | 22 | Then (assert the foldcolumn is 2): 23 | AssertEqual 2, &foldcolumn 24 | 25 | Given vim (a file with nested folds): 26 | " Fold {{{ 27 | 28 | " Nested Fold {{{ 29 | " End Fold }}} 30 | 31 | " End Fold }}} 32 | 33 | Execute (AutoOrigamiFoldColumn): 34 | AutoOrigamiFoldColumn 35 | 36 | Then (assert the foldcolumn is 2): 37 | AssertEqual 2, &foldcolumn 38 | 39 | Given vim (a file with multiple folds): 40 | " Fold {{{ 41 | 42 | " Nested Fold {{{ 43 | " End Fold }}} 44 | 45 | " End Fold }}} 46 | 47 | " Fold {{{ 48 | 49 | " Nested Fold {{{ 50 | " End Fold }}} 51 | 52 | " End Fold }}} 53 | 54 | Execute (AutoOrigamiFoldColumn): 55 | AutoOrigamiFoldColumn 56 | 57 | Then (assert the foldcolumn is 2): 58 | AssertEqual 2, &foldcolumn 59 | -------------------------------------------------------------------------------- /test/variables.vader: -------------------------------------------------------------------------------- 1 | Before (set up auto origami): 2 | set foldmethod=marker 3 | set foldenable 4 | let g:auto_origami_foldcolumn = 4 5 | let g:auto_origami_default = 5 6 | 7 | Given (a file with no folds): 8 | This is some text 9 | 10 | Execute (AutoOrigamiFoldColumn): 11 | AutoOrigamiFoldColumn 12 | 13 | Then (assert the foldcolumn is 5): 14 | AssertEqual 5, &foldcolumn 15 | 16 | Given vim (a file with a fold): 17 | " One Fold {{{ 18 | 19 | " End }}} 20 | 21 | Execute (AutoOrigamiFoldColumn): 22 | AutoOrigamiFoldColumn 23 | 24 | Then (assert the foldcolumn is 4): 25 | AssertEqual 4, &foldcolumn 26 | 27 | Given vim (a file with nested folds): 28 | " Fold {{{ 29 | 30 | " Nested Fold {{{ 31 | " End Fold }}} 32 | 33 | " End Fold }}} 34 | 35 | Execute (AutoOrigamiFoldColumn): 36 | AutoOrigamiFoldColumn 37 | 38 | Then (assert the foldcolumn is 4): 39 | AssertEqual 4, &foldcolumn 40 | 41 | Given vim (a file with multiple folds): 42 | " Fold {{{ 43 | 44 | " Nested Fold {{{ 45 | " End Fold }}} 46 | 47 | " End Fold }}} 48 | 49 | " Fold {{{ 50 | 51 | " Nested Fold {{{ 52 | " End Fold }}} 53 | 54 | " End Fold }}} 55 | 56 | Execute (AutoOrigamiFoldColumn): 57 | AutoOrigamiFoldColumn 58 | 59 | Then (assert the foldcolumn is 4): 60 | AssertEqual 4, &foldcolumn 61 | --------------------------------------------------------------------------------