Skip to content

Commit 2aa5f1d

Browse files
committed
Support both Python 2 and Python 3
- And specify MIT license
1 parent 7fd37b5 commit 2aa5f1d

File tree

4 files changed

+56
-27
lines changed

4 files changed

+56
-27
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# AutoCWD.vim
22

3-
*Currently, this plugin only supports the vim compiled with python 2 support (+python).*
4-
53
AutoCWD.vim automatically changes the current working directory (CWD) of vim when you change the current buffer (or window).
64
You can define patterns that may be included in a file path or buffer name, and corresponding working directories in your .vimrc.
75
<!--The predefined working directories can be the directory of the current buffer's file, absolute paths of specific directories, and so on.-->
@@ -20,8 +18,10 @@ Screenshot:
2018
- Manual install (not recommended)
2119
- Download this plugin and extract it in `~/.vim/`
2220

23-
This plugin requires a version of vim with python support. You can check your vim with `:echo has('python')`.
24-
- Linux: If your vim doesn't support python, one of the easiest solutions would be installing a more featured version of vim by `sudo apt-get install vim-gtk-py2`.
21+
## Requirements
22+
23+
This plugin requires a version of vim with Python 2 (+python) or Python 3 (+python3) support. You can check your vim with `:echo has('python')` or `:echo has('python3')`.
24+
- Ubuntu: If your vim doesn't support python, one of the easiest solutions would be installing a more featured version of vim by `sudo apt-get install vim-gtk`.
2525
- Windows: gvim for Windows is already equipped with python support.
2626

2727
## Usage

autoload/autocwd.vim

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" File: autoload/autocwd.vim
2-
" Description: Auto current working directory update system
2+
" Description: Automatic CWD(current working directory) updating system
33
" Author: yssl <http://github.com/yssl>
4-
" License:
4+
" License: MIT License
55

66
" wrappers
77
function! autocwd#PrintWorkDirs()
@@ -10,8 +10,19 @@ endfunction
1010

1111
" script variables
1212

13+
" Support for Python3 and Python2
14+
" from https://github.com/Valloric/YouCompleteMe
15+
function! s:UsingPython3()
16+
if has('python3')
17+
return 1
18+
endif
19+
return 0
20+
endfunction
21+
let s:using_python3 = s:UsingPython3()
22+
let s:pythonX_until_EOF = s:using_python3 ? "python3 << EOF" : "python << EOF"
23+
1324
" initialize python
14-
python << EOF
25+
exec s:pythonX_until_EOF
1526
import vim
1627
import fnmatch
1728

@@ -31,17 +42,17 @@ def toWidthColMat(rowMat):
3142
return colMat
3243

3344
def ltrunc(s, width, prefix=''):
34-
if width >= len(s): prefix = ''
35-
return prefix+s[-width+len(prefix):]
36-
45+
if width >= len(s): prefix = ''
46+
return prefix+s[-width+len(prefix):]
47+
3748
def rtrunc(s, width, postfix=''):
38-
if width >= len(s): postfix = ''
39-
return s[:width-len(postfix)]+postfix
49+
if width >= len(s): postfix = ''
50+
return s[:width-len(postfix)]+postfix
4051
EOF
4152

4253
" functions
4354
function! s:GetWorkDir(filepath)
44-
python << EOF
55+
exec s:pythonX_until_EOF
4556
filepath = vim.eval('a:filepath')
4657
patternwd_pairs = vim.eval('g:autocwd_patternwd_pairs')
4758
inpatternwd = False
@@ -50,7 +61,7 @@ for pattern, wd in patternwd_pairs:
5061
if '*REPO*' in wd:
5162
wd = wd.replace('*REPO*', findRepoDirFrom(filepath))
5263
wd = vim.eval('expand(\'%s\')'%wd)
53-
if os.path.isdir(wd):
64+
if os.path.isdir(wd):
5465
inpatternwd = True
5566
vim.command('return \'%s\''%os.path.abspath(wd))
5667
break
@@ -60,7 +71,7 @@ EOF
6071
endfunction
6172

6273
function! s:GetWorkDirPattern(filepath)
63-
python << EOF
74+
exec s:pythonX_until_EOF
6475
import fnmatch
6576
filepath = vim.eval('a:filepath')
6677
patternwd_pairs = vim.eval('g:autocwd_patternwd_pairs')
@@ -70,7 +81,7 @@ for pattern, wd in patternwd_pairs:
7081
if '*REPO*' in wd:
7182
wd = wd.replace('*REPO*', findRepoDirFrom(filepath))
7283
wd = vim.eval('expand(\'%s\')'%wd)
73-
if os.path.isdir(wd):
84+
if os.path.isdir(wd):
7485
inpatternwd = True
7586
vim.command('return \'%s\''%pattern)
7687
break
@@ -80,7 +91,7 @@ EOF
8091
endfunction
8192

8293
function! s:BuildAllWinPropMat(propTypes)
83-
python << EOF
94+
exec s:pythonX_until_EOF
8495
vim.command('let mat = []')
8596
curwin = vim.current.window
8697
curwin_r = 0
@@ -112,7 +123,7 @@ for r in range(len(vim.windows)):
112123

113124
elif type=='workdir':
114125
dir = vim.eval('s:GetWorkDir(\'%s\')'%getWinName(bufname, buftype))
115-
#print '|%s|%s|%s|'%(bufname,buftype,dir)
126+
#print('|%s|%s|%s|'%(bufname,buftype,dir))
116127
vim.command('call add(mat[-1], \'%s\')'%dir)
117128

118129
elif type=='workdir_pattern':
@@ -126,7 +137,7 @@ endfunction
126137

127138
function! s:PrintWorkDirs()
128139
let a:type = 'workdir'
129-
python << EOF
140+
exec s:pythonX_until_EOF
130141
vim.command('let propTypes = ["iscurwin", "winnr", "winname", "workdir_pattern", "workdir"]')
131142
wpMat = vim.eval('s:BuildAllWinPropMat(propTypes)')
132143
propTypes = vim.eval('propTypes')
@@ -178,6 +189,5 @@ if user_input.isdigit():
178189
winnum = int(vim.eval('winnr("$")'))
179190
if winidx<=winnum:
180191
vim.command('%dwincmd w'%winidx)
181-
182192
EOF
183193
endfunction

doc/autocwd.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ when you change the current buffer (or window).
2121
You can define patterns that may be included in a file path or buffer name,
2222
and corresponding working directories in your .vimrc.
2323

24-
This plugin requires a version of vim with python support.
25-
You can check your vim with `:echo has('python')`.
24+
This plugin requires a version of vim with Python 2 (+python) or Python 3 (+python3)
25+
support. You can check your vim with `:echo has('python')` or `:echo has('python3')`.
2626

2727
==============================================================================
2828
2. Usage *autocwd-usage*

plugin/autocwd.vim

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
" File: plugin/autocwd.vim
2-
" Description: Auto current working directory update system
2+
" Description: Automatic CWD(current working directory) updating system
33
" Author: yssl <http://github.com/yssl>
4-
" License:
4+
" License: MIT License
55

66
if exists("g:loaded_autocwd") || &cp
77
finish
@@ -11,6 +11,14 @@ let s:keepcpo = &cpo
1111
set cpo&vim
1212
"""""""""""""""""""""""""""""""""""""""""""""
1313

14+
" vim version checking
15+
if !has('python3') && !has('python')
16+
echohl WarningMsg
17+
echomsg 'AutoCWD.vim unavailable: requires vim with Python support'
18+
echohl None
19+
finish
20+
endif
21+
1422
" global variables
1523
if !exists('g:autocwd_patternwd_pairs')
1624
let g:autocwd_patternwd_pairs = []
@@ -35,8 +43,19 @@ augroup AutoCWDAutoCmds
3543
autocmd BufLeave * call s:OnLeaveBuf()
3644
augroup END
3745

46+
" Support for Python3 and Python2
47+
" from https://github.com/Valloric/YouCompleteMe
48+
function! s:UsingPython3()
49+
if has('python3')
50+
return 1
51+
endif
52+
return 0
53+
endfunction
54+
let s:using_python3 = s:UsingPython3()
55+
let s:pythonX_until_EOF = s:using_python3 ? "python3 << EOF" : "python << EOF"
56+
3857
" initialize python
39-
python << EOF
58+
exec s:pythonX_until_EOF
4059
import vim
4160
import os, fnmatch
4261

@@ -105,7 +124,7 @@ function! s:OnLeaveBuf()
105124
endfunction
106125

107126
function! s:ExistPattern(bufname, buftype)
108-
python << EOF
127+
exec s:pythonX_until_EOF
109128
bufname = vim.eval('a:bufname')
110129
buftype = vim.eval('a:buftype')
111130
filepath = getWinName(bufname, buftype)
@@ -116,7 +135,7 @@ for pattern, wd in patternwd_pairs:
116135
if '*REPO*' in wd:
117136
wd = wd.replace('*REPO*', findRepoDirFrom(filepath))
118137
wd = vim.eval('expand(\'%s\')'%wd)
119-
if os.path.isdir(wd):
138+
if os.path.isdir(wd):
120139
inpatternwd = True
121140
vim.command('return [1, \'%s\']'%wd)
122141
break

0 commit comments

Comments
 (0)