Skip to content

Commit

Permalink
Simultaneous python and python3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
eapache committed Jun 9, 2016
1 parent 423a413 commit a425095
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
12 changes: 9 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 2.8.3)

project(cpsm)
option(PY3 "Build for python3 instead of python2." OFF)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include_directories(${PROJECT_SOURCE_DIR}/src)
set(CMAKE_BUILD_TYPE Release)
Expand Down Expand Up @@ -30,9 +31,14 @@ if(ICU_FOUND)
add_definitions(-DCPSM_CONFIG_ICU=1)
endif()

set(Python_ADDITIONAL_VERSIONS 3.5)
find_package(PythonInterp 3 REQUIRED)
find_package(PythonLibs 3 REQUIRED)
if(PY3)
set(Python_ADDITIONAL_VERSIONS 3.5 3.4)
find_package(PythonInterp 3 REQUIRED)
find_package(PythonLibs 3 REQUIRED)
else()
set(Python_ADDITIONAL_VERSIONS 2.7 2.6)
find_package(PythonLibrary REQUIRED)
endif()
include_directories(${PYTHON_INCLUDE_PATH})

find_package(Threads REQUIRED)
Expand Down
17 changes: 12 additions & 5 deletions autoload/cpsm.vim
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,26 @@ endif

let s:script_dir = escape(expand('<sfile>:p:h'), '\')

execute 'py3file ' . s:script_dir . '/cpsm.py'
if has('python3')
execute 'py3file ' . s:script_dir . '/cpsm.py'
else
execute 'pyfile ' . s:script_dir . '/cpsm.py'
endif

function cpsm#CtrlPMatch(items, str, limit, mmode, ispath, crfile, regex)
if !has('python3')
return ['ERROR: cpsm requires Vim built with Python support']
if !has('python3') && !has('python')
return ['ERROR: cpsm requires Vim built with Python or Python3 support']
endif
if empty(a:str) && g:cpsm_match_empty_query == 0
let s:results = a:items[0:(a:limit)]
let s:regexes = []
else
let s:match_crfile = exists('g:ctrlp_match_current_file') ? g:ctrlp_match_current_file : 0
py3 ctrlp_match()
if has('python3')
py3 ctrlp_match()
else
py ctrlp_match()
endif
endif
call clearmatches()
" Apply highlight regexes.
Expand All @@ -60,4 +68,3 @@ function cpsm#CtrlPMatch(items, str, limit, mmode, ispath, crfile, regex)
call matchadd('CtrlPLinePre', '^>')
return s:results
endfunction

8 changes: 7 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ set -o errexit
set -o nounset
set -o pipefail

if vim --version | grep -q +python3; then
PY3="ON"
else
PY3="OFF"
fi

mkdir -p build
{
cd build
cmake ..
cmake -DPY3:BOOL=$PY3 ..
make install
}
18 changes: 16 additions & 2 deletions src/python_extension_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ class PyListCtrlPMatchSourceState {
}
char* item_data;
Py_ssize_t item_size;
#if PY_MAJOR_VERSION >= 3
item_data = PyUnicode_AsUTF8AndSize(item_obj, &item_size);
if (!item_data) {
if (!item_data)
#else
if (PyString_AsStringAndSize(item_obj, &item_data, &item_size) < 0)
#endif
{
have_python_exception_ = true;
return false;
}
Expand Down Expand Up @@ -274,7 +279,12 @@ static PyObject* cpsm_ctrlp_match(PyObject* self, PyObject* args,
}
for (auto const& regex : highlight_regexes) {
PyObjPtr regex_str(
PyUnicode_FromStringAndSize(regex.data(), regex.size()));
#if PY_MAJOR_VERSION >= 3
PyUnicode_FromStringAndSize(regex.data(), regex.size())
#else
PyString_FromStringAndSize(regex.data(), regex.size())
#endif
);
if (!regex_str) {
return nullptr;
}
Expand All @@ -298,6 +308,7 @@ static PyMethodDef cpsm_py_methods[] = {
"Match strings with a CtrlP-compatible interface"},
{nullptr, nullptr, 0, nullptr}};

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"cpsm_py",
Expand All @@ -311,5 +322,8 @@ static struct PyModuleDef moduledef = {
};

PyMODINIT_FUNC PyInit_cpsm_py() { return PyModule_Create(&moduledef); }
#else
PyMODINIT_FUNC initcpsm_py() { Py_InitModule("cpsm_py", cpsm_py_methods); }
#endif

} /* extern "C" */

0 comments on commit a425095

Please sign in to comment.