Skip to content

Commit 1b15a71

Browse files
authored
Merge pull request #423 from macvim-dev/experimental/pythonhomeopt
[Experimental] Set pythonhome and pythonthreehome automatically
2 parents f9248a4 + e3e3a15 commit 1b15a71

File tree

8 files changed

+99
-10
lines changed

8 files changed

+99
-10
lines changed

runtime/doc/options.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5921,6 +5921,21 @@ A jump table for the options with a short description can be found at |Q_op|.
59215921
DYNAMIC_PYTHON_DLL, which was specified at compile time.
59225922
Environment variables are expanded |:set_env|.
59235923
This option cannot be set from a |modeline| or in the |sandbox|, for
5924+
security reasons.
5925+
5926+
*'pythonhome'*
5927+
'pythonhome' string (default "")
5928+
global
5929+
{not in Vi}
5930+
{only available when compiled with the |+python/dyn|
5931+
feature}
5932+
Note: EXPERIMENTAL. It may be changed or removed in the future.
5933+
Specifies the name of the Python 2.x home directory. When 'pythonhome'
5934+
and the PYTHONHOME environment variable are not set, PYTHON_HOME,
5935+
which was specified at compile time, will be used for the Python 2.x
5936+
home directory.
5937+
Environment variables are expanded |:set_env|.
5938+
This option cannot be set from a |modeline| or in the |sandbox|, for
59245939
security reasons.
59255940

59265941
*'pythonthreedll'*
@@ -5933,6 +5948,21 @@ A jump table for the options with a short description can be found at |Q_op|.
59335948
DYNAMIC_PYTHON3_DLL, which was specified at compile time.
59345949
Environment variables are expanded |:set_env|.
59355950
This option cannot be set from a |modeline| or in the |sandbox|, for
5951+
security reasons.
5952+
5953+
*'pythonthreehome'*
5954+
'pythonthreehome' string (default "")
5955+
global
5956+
{not in Vi}
5957+
{only available when compiled with the |+python3/dyn|
5958+
feature}
5959+
Note: EXPERIMENTAL. It may be changed or removed in the future.
5960+
Specifies the name of the Python 3 home directory. When
5961+
'pythonthreehome' and the PYTHONHOME environment variable are not set,
5962+
PYTHON3_HOME, which was specified at compile time, will be used for
5963+
the Python 3 home directory.
5964+
Environment variables are expanded |:set_env|.
5965+
This option cannot be set from a |modeline| or in the |sandbox|, for
59365966
security reasons.
59375967

59385968
*'quoteescape'* *'qe'*

runtime/doc/quickref.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,9 @@ Short explanation of each option: *option-list*
840840
'prompt' 'prompt' enable prompt in Ex mode
841841
'pumheight' 'ph' maximum height of the popup menu
842842
'pythondll' name of the Python 2 dynamic library
843+
'pythonhome' name of the Python 2 home directory
843844
'pythonthreedll' name of the Python 3 dynamic library
845+
'pythonthreehome' name of the Python 3 home directory
844846
'quoteescape' 'qe' escape characters used in a string
845847
'readonly' 'ro' disallow writing the buffer
846848
'redrawtime' 'rdt' timeout for 'hlsearch' and |:match| highlighting

runtime/optwin.vim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,18 @@ if exists("&pythondll")
13451345
call append("$", "pythondll\tname of the Python 2 dynamic library")
13461346
call <SID>OptionG("pythondll", &pythondll)
13471347
endif
1348+
if exists("&pythonhome")
1349+
call append("$", "pythonhome\tname of the Python 2 home directory")
1350+
call <SID>OptionG("pythonhome", &pythonhome)
1351+
endif
13481352
if exists("&pythonthreedll")
13491353
call append("$", "pythonthreedll\tname of the Python 3 dynamic library")
13501354
call <SID>OptionG("pythonthreedll", &pythonthreedll)
13511355
endif
1356+
if exists("&pythonthreehome")
1357+
call append("$", "pythonthreehome\tname of the Python 3 home directory")
1358+
call <SID>OptionG("pythonthreehome", &pythonthreehome)
1359+
endif
13521360
if exists("&rubydll")
13531361
call append("$", "rubydll\tname of the Ruby dynamic library")
13541362
call <SID>OptionG("rubydll", &rubydll)

src/MacVim/vimrc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,25 @@ set backspace+=indent,eol,start
1313
" the entire MacVim menu is set up in a nib file which currently only is
1414
" translated to English).
1515
set langmenu=none
16+
17+
" Python2
18+
" MacVim uses Homebrew python2 if installed, otherwise configured one
19+
if exists("&pythondll") && exists("&pythonhome")
20+
if filereadable("/usr/local/Frameworks/Python.framework/Versions/2.7/Python")
21+
" Homebrew python 2.7
22+
set pythondll=/usr/local/Frameworks/Python.framework/Versions/2.7/Python
23+
set pythonhome=/usr/local/Frameworks/Python.framework/Versions/2.7
24+
endif
25+
endif
26+
27+
" Python3
28+
" MacVim uses Homebrew python3 if installed, next try to use python.org binary
29+
if exists("&pythonthreedll") && exists("&pythonthreehome") &&
30+
\ !filereadable(&pythonthreedll)
31+
if filereadable("/Library/Frameworks/Python.framework/Versions/3.5/Python")
32+
" https://www.python.org/downloads/mac-osx/
33+
set pythonthreedll=/Library/Frameworks/Python.framework/Versions/3.5/Python
34+
set pythonthreehome=/Library/Frameworks/Python.framework/Versions/3.5
35+
endif
36+
endif
37+

src/if_python.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,13 +933,17 @@ Python_Init(void)
933933
EMSG(_("E263: Sorry, this command is disabled, the Python library could not be loaded."));
934934
goto fail;
935935
}
936-
#endif
937936

938-
#ifdef PYTHON_HOME
939-
# ifdef DYNAMIC_PYTHON
940-
if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
941-
# endif
937+
if (p_pyhome && *p_pyhome != '\0')
938+
Py_SetPythonHome((char *)p_pyhome);
939+
# ifdef PYTHON_HOME
940+
else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
942941
Py_SetPythonHome(PYTHON_HOME);
942+
# endif
943+
#else
944+
# ifdef PYTHON_HOME
945+
Py_SetPythonHome(PYTHON_HOME);
946+
# endif
943947
#endif
944948

945949
init_structs();

src/if_python3.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -860,12 +860,25 @@ Python3_Init(void)
860860

861861
init_structs();
862862

863-
864-
#ifdef PYTHON3_HOME
865-
# ifdef DYNAMIC_PYTHON3
866-
if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
867-
# endif
863+
#ifdef DYNAMIC_PYTHON3
864+
if (p_py3home && *p_py3home != '\0')
865+
{
866+
int len;
867+
wchar_t *buf;
868+
len = mbstowcs(NULL, (char *)p_py3home, 0) + 1;
869+
buf = (wchar_t *)alloc(len * sizeof(wchar_t));
870+
if (buf && mbstowcs(buf, (char *)p_py3home, len) != (size_t)-1)
871+
Py_SetPythonHome(buf);
872+
vim_free(buf);
873+
}
874+
# ifdef PYTHON3_HOME
875+
else if (mch_getenv((char_u *)"PYTHONHOME") == NULL)
868876
Py_SetPythonHome(PYTHON3_HOME);
877+
# endif
878+
#else
879+
# ifdef PYTHON3_HOME
880+
Py_SetPythonHome(PYTHON3_HOME);
881+
# endif
869882
#endif
870883

871884
PyImport_AppendInittab("vim", Py3Init_vim);

src/option.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2187,12 +2187,20 @@ static struct vimoption options[] =
21872187
(char_u *)&p_py3dll, PV_NONE,
21882188
{(char_u *)DYNAMIC_PYTHON3_DLL, (char_u *)0L}
21892189
SCRIPTID_INIT},
2190+
{"pythonthreehome", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
2191+
(char_u *)&p_py3home, PV_NONE,
2192+
{(char_u *)NULL, (char_u *)0L}
2193+
SCRIPTID_INIT},
21902194
#endif
21912195
#if defined(DYNAMIC_PYTHON)
21922196
{"pythondll", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
21932197
(char_u *)&p_pydll, PV_NONE,
21942198
{(char_u *)DYNAMIC_PYTHON_DLL, (char_u *)0L}
21952199
SCRIPTID_INIT},
2200+
{"pythonhome", NULL, P_STRING|P_EXPAND|P_VI_DEF|P_SECURE,
2201+
(char_u *)&p_pyhome, PV_NONE,
2202+
{(char_u *)NULL, (char_u *)0L}
2203+
SCRIPTID_INIT},
21962204
#endif
21972205
{"quoteescape", "qe", P_STRING|P_ALLOCED|P_VI_DEF,
21982206
#ifdef FEAT_TEXTOBJ

src/option.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,11 @@ EXTERN char_u *p_perldll; /* 'perldll' */
706706
#endif
707707
#if defined(DYNAMIC_PYTHON3)
708708
EXTERN char_u *p_py3dll; /* 'pythonthreedll' */
709+
EXTERN char_u *p_py3home; /* 'pythonthreehome' */
709710
#endif
710711
#if defined(DYNAMIC_PYTHON)
711712
EXTERN char_u *p_pydll; /* 'pythondll' */
713+
EXTERN char_u *p_pyhome; /* 'pythonhome' */
712714
#endif
713715
#ifdef FEAT_RELTIME
714716
EXTERN long p_rdt; /* 'redrawtime' */

0 commit comments

Comments
 (0)