2016-08-29 22:49:24 +02:00
|
|
|
/* vi:set ts=8 sts=4 sw=4 noet:
|
2004-06-13 20:20:40 +00:00
|
|
|
*
|
|
|
|
* VIM - Vi IMproved by Bram Moolenaar
|
|
|
|
*
|
|
|
|
* Do ":help uganda" in Vim to read copying and usage conditions.
|
|
|
|
* Do ":help credits" in Vim to see a list of people who contributed.
|
|
|
|
* See README.txt for an overview of the Vim source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ex_cmds2.c: some more functions for command line commands
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vim.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If 'autowrite' option set, try to write the file.
|
|
|
|
* Careful: autocommands may make "buf" invalid!
|
|
|
|
*
|
|
|
|
* return FAIL for failure, OK otherwise
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 15:52:46 +01:00
|
|
|
autowrite(buf_T *buf, int forceit)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2007-02-13 05:19:30 +00:00
|
|
|
int r;
|
2016-07-10 22:11:16 +02:00
|
|
|
bufref_T bufref;
|
2007-02-13 05:19:30 +00:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (!(p_aw || p_awa) || !p_write
|
|
|
|
#ifdef FEAT_QUICKFIX
|
2019-12-01 21:41:28 +01:00
|
|
|
// never autowrite a "nofile" or "nowrite" buffer
|
2007-02-13 05:19:30 +00:00
|
|
|
|| bt_dontwrite(buf)
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
2007-02-13 05:19:30 +00:00
|
|
|
|| (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
return FAIL;
|
2016-07-10 22:11:16 +02:00
|
|
|
set_bufref(&bufref, buf);
|
2007-02-13 05:19:30 +00:00
|
|
|
r = buf_write_all(buf, forceit);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Writing may succeed but the buffer still changed, e.g., when there is a
|
|
|
|
// conversion error. We do want to return FAIL then.
|
2016-07-10 22:11:16 +02:00
|
|
|
if (bufref_valid(&bufref) && bufIsChanged(buf))
|
2007-02-13 05:19:30 +00:00
|
|
|
r = FAIL;
|
|
|
|
return r;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-08-30 13:07:17 +02:00
|
|
|
* Flush all buffers, except the ones that are readonly or are never written.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
autowrite_all(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
buf_T *buf;
|
|
|
|
|
|
|
|
if (!(p_aw || p_awa) || !p_write)
|
|
|
|
return;
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf)
|
2018-08-30 13:07:17 +02:00
|
|
|
if (bufIsChanged(buf) && !buf->b_p_ro && !bt_dontwrite(buf))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-07-10 22:11:16 +02:00
|
|
|
bufref_T bufref;
|
|
|
|
|
|
|
|
set_bufref(&bufref, buf);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
(void)buf_write_all(buf, FALSE);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// an autocommand may have deleted the buffer
|
2016-07-10 22:11:16 +02:00
|
|
|
if (!bufref_valid(&bufref))
|
2004-06-13 20:20:40 +00:00
|
|
|
buf = firstbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-09 03:31:51 +01:00
|
|
|
* Return TRUE if buffer was changed and cannot be abandoned.
|
|
|
|
* For flags use the CCGD_ values.
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
int
|
2016-01-30 15:52:46 +01:00
|
|
|
check_changed(buf_T *buf, int flags)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-07-10 22:11:16 +02:00
|
|
|
int forceit = (flags & CCGD_FORCEIT);
|
|
|
|
bufref_T bufref;
|
|
|
|
|
|
|
|
set_bufref(&bufref, buf);
|
2013-11-09 03:31:51 +01:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if ( !forceit
|
|
|
|
&& bufIsChanged(buf)
|
2013-11-09 03:31:51 +01:00
|
|
|
&& ((flags & CCGD_MULTWIN) || buf->b_nwindows <= 1)
|
|
|
|
&& (!(flags & CCGD_AW) || autowrite(buf, forceit) == FAIL))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
|
|
|
|
if ((p_confirm || cmdmod.confirm) && p_write)
|
|
|
|
{
|
|
|
|
buf_T *buf2;
|
|
|
|
int count = 0;
|
|
|
|
|
2013-11-09 03:31:51 +01:00
|
|
|
if (flags & CCGD_ALLBUF)
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf2)
|
2004-06-13 20:20:40 +00:00
|
|
|
if (bufIsChanged(buf2)
|
|
|
|
&& (buf2->b_ffname != NULL
|
|
|
|
# ifdef FEAT_BROWSE
|
|
|
|
|| cmdmod.browse
|
|
|
|
# endif
|
|
|
|
))
|
|
|
|
++count;
|
2016-07-10 22:11:16 +02:00
|
|
|
if (!bufref_valid(&bufref))
|
2019-12-01 21:41:28 +01:00
|
|
|
// Autocommand deleted buffer, oops! It's not changed now.
|
2004-06-13 20:20:40 +00:00
|
|
|
return FALSE;
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
dialog_changed(buf, count > 1);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2016-07-10 22:11:16 +02:00
|
|
|
if (!bufref_valid(&bufref))
|
2019-12-01 21:41:28 +01:00
|
|
|
// Autocommand deleted buffer, oops! It's not changed now.
|
2004-06-13 20:20:40 +00:00
|
|
|
return FALSE;
|
|
|
|
return bufIsChanged(buf);
|
|
|
|
}
|
|
|
|
#endif
|
2013-11-09 03:31:51 +01:00
|
|
|
if (flags & CCGD_EXCMD)
|
2017-08-17 16:55:13 +02:00
|
|
|
no_write_message();
|
2013-11-09 03:31:51 +01:00
|
|
|
else
|
2018-02-19 23:10:02 +01:00
|
|
|
no_write_message_nobang(curbuf);
|
2004-06-13 20:20:40 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
|
|
|
|
|
|
|
|
#if defined(FEAT_BROWSE) || defined(PROTO)
|
|
|
|
/*
|
|
|
|
* When wanting to write a file without a file name, ask the user for a name.
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
browse_save_fname(buf_T *buf)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (buf->b_fname == NULL)
|
|
|
|
{
|
|
|
|
char_u *fname;
|
|
|
|
|
2004-10-11 10:16:09 +00:00
|
|
|
fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
|
|
|
|
NULL, NULL, NULL, NULL, buf);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (fname != NULL)
|
|
|
|
{
|
|
|
|
if (setfname(buf, fname, NULL, TRUE) == OK)
|
|
|
|
buf->b_flags |= BF_NOTEDITED;
|
|
|
|
vim_free(fname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2011-05-19 18:26:40 +02:00
|
|
|
* Ask the user what to do when abandoning a changed buffer.
|
2004-06-13 20:20:40 +00:00
|
|
|
* Must check 'write' option first!
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
dialog_changed(
|
|
|
|
buf_T *buf,
|
2019-12-01 21:41:28 +01:00
|
|
|
int checkall) // may abandon all changed buffers
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2011-04-11 21:35:11 +02:00
|
|
|
char_u buff[DIALOG_MSG_SIZE];
|
2004-06-13 20:20:40 +00:00
|
|
|
int ret;
|
|
|
|
buf_T *buf2;
|
2012-04-25 17:32:18 +02:00
|
|
|
exarg_T ea;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2017-08-21 22:06:02 +02:00
|
|
|
dialog_msg(buff, _("Save changes to \"%s\"?"), buf->b_fname);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (checkall)
|
|
|
|
ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
|
|
|
|
else
|
|
|
|
ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
|
|
|
|
|
2019-05-09 21:48:37 +02:00
|
|
|
// Init ea pseudo-structure, this is needed for the check_overwrite()
|
|
|
|
// function.
|
2020-04-12 19:37:17 +02:00
|
|
|
CLEAR_FIELD(ea);
|
2012-04-25 17:32:18 +02:00
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (ret == VIM_YES)
|
|
|
|
{
|
|
|
|
#ifdef FEAT_BROWSE
|
2019-12-01 21:41:28 +01:00
|
|
|
// May get file name, when there is none
|
2004-06-13 20:20:40 +00:00
|
|
|
browse_save_fname(buf);
|
|
|
|
#endif
|
2012-04-25 17:32:18 +02:00
|
|
|
if (buf->b_fname != NULL && check_overwrite(&ea, buf,
|
|
|
|
buf->b_fname, buf->b_ffname, FALSE) == OK)
|
2019-12-01 21:41:28 +01:00
|
|
|
// didn't hit Cancel
|
2004-06-13 20:20:40 +00:00
|
|
|
(void)buf_write_all(buf, FALSE);
|
|
|
|
}
|
|
|
|
else if (ret == VIM_NO)
|
|
|
|
{
|
2019-06-08 18:07:21 +02:00
|
|
|
unchanged(buf, TRUE, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else if (ret == VIM_ALL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Write all modified files that can be written.
|
|
|
|
* Skip readonly buffers, these need to be confirmed
|
|
|
|
* individually.
|
|
|
|
*/
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf2)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (bufIsChanged(buf2)
|
|
|
|
&& (buf2->b_ffname != NULL
|
|
|
|
#ifdef FEAT_BROWSE
|
|
|
|
|| cmdmod.browse
|
|
|
|
#endif
|
|
|
|
)
|
|
|
|
&& !buf2->b_p_ro)
|
|
|
|
{
|
2016-07-10 22:11:16 +02:00
|
|
|
bufref_T bufref;
|
|
|
|
|
|
|
|
set_bufref(&bufref, buf2);
|
2004-06-13 20:20:40 +00:00
|
|
|
#ifdef FEAT_BROWSE
|
2019-12-01 21:41:28 +01:00
|
|
|
// May get file name, when there is none
|
2004-06-13 20:20:40 +00:00
|
|
|
browse_save_fname(buf2);
|
|
|
|
#endif
|
2012-04-25 17:32:18 +02:00
|
|
|
if (buf2->b_fname != NULL && check_overwrite(&ea, buf2,
|
|
|
|
buf2->b_fname, buf2->b_ffname, FALSE) == OK)
|
2019-12-01 21:41:28 +01:00
|
|
|
// didn't hit Cancel
|
2004-06-13 20:20:40 +00:00
|
|
|
(void)buf_write_all(buf2, FALSE);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// an autocommand may have deleted the buffer
|
2016-07-10 22:11:16 +02:00
|
|
|
if (!bufref_valid(&bufref))
|
2004-06-13 20:20:40 +00:00
|
|
|
buf2 = firstbuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ret == VIM_DISCARDALL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* mark all buffers as unchanged
|
|
|
|
*/
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf2)
|
2019-06-08 18:07:21 +02:00
|
|
|
unchanged(buf2, TRUE, FALSE);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return TRUE if the buffer "buf" can be abandoned, either by making it
|
|
|
|
* hidden, autowriting it or unloading it.
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 15:52:46 +01:00
|
|
|
can_abandon(buf_T *buf, int forceit)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2017-08-03 22:44:55 +02:00
|
|
|
return ( buf_hide(buf)
|
2004-06-13 20:20:40 +00:00
|
|
|
|| !bufIsChanged(buf)
|
|
|
|
|| buf->b_nwindows > 1
|
|
|
|
|| autowrite(buf, forceit) == OK
|
|
|
|
|| forceit);
|
|
|
|
}
|
|
|
|
|
2012-03-23 18:39:18 +01:00
|
|
|
/*
|
|
|
|
* Add a buffer number to "bufnrs", unless it's already there.
|
|
|
|
*/
|
|
|
|
static void
|
2016-01-30 15:52:46 +01:00
|
|
|
add_bufnum(int *bufnrs, int *bufnump, int nr)
|
2012-03-23 18:39:18 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < *bufnump; ++i)
|
|
|
|
if (bufnrs[i] == nr)
|
|
|
|
return;
|
|
|
|
bufnrs[*bufnump] = nr;
|
|
|
|
*bufnump = *bufnump + 1;
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* Return TRUE if any buffer was changed and cannot be abandoned.
|
|
|
|
* That changed buffer becomes the current buffer.
|
2018-03-10 20:28:12 +01:00
|
|
|
* When "unload" is TRUE the current buffer is unloaded instead of making it
|
2016-01-02 22:25:52 +01:00
|
|
|
* hidden. This is used for ":q!".
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
int
|
2016-01-30 15:52:46 +01:00
|
|
|
check_changed_any(
|
2019-12-01 21:41:28 +01:00
|
|
|
int hidden, // Only check hidden buffers
|
2016-01-30 15:52:46 +01:00
|
|
|
int unload)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2012-03-23 18:39:18 +01:00
|
|
|
int ret = FALSE;
|
2004-06-13 20:20:40 +00:00
|
|
|
buf_T *buf;
|
|
|
|
int save;
|
2012-03-23 18:39:18 +01:00
|
|
|
int i;
|
|
|
|
int bufnum = 0;
|
|
|
|
int bufcount = 0;
|
|
|
|
int *bufnrs;
|
|
|
|
tabpage_T *tp;
|
2004-06-13 20:20:40 +00:00
|
|
|
win_T *wp;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Make a list of all buffers, with the most important ones first.
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf)
|
2012-03-23 18:39:18 +01:00
|
|
|
++bufcount;
|
|
|
|
|
|
|
|
if (bufcount == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-05-28 23:08:19 +02:00
|
|
|
bufnrs = ALLOC_MULT(int, bufcount);
|
2012-03-23 18:39:18 +01:00
|
|
|
if (bufnrs == NULL)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// curbuf
|
2012-03-23 18:39:18 +01:00
|
|
|
bufnrs[bufnum++] = curbuf->b_fnum;
|
2018-03-10 20:28:12 +01:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// buffers in current tab
|
2012-03-23 18:39:18 +01:00
|
|
|
FOR_ALL_WINDOWS(wp)
|
|
|
|
if (wp->w_buffer != curbuf)
|
|
|
|
add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// buffers in other tabs
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_TABPAGES(tp)
|
2012-03-23 18:39:18 +01:00
|
|
|
if (tp != curtab)
|
2020-04-02 18:50:46 +02:00
|
|
|
FOR_ALL_WINDOWS_IN_TAB(tp, wp)
|
2012-03-23 18:39:18 +01:00
|
|
|
add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
|
2018-03-10 20:28:12 +01:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// any other buffer
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf)
|
2012-03-23 18:39:18 +01:00
|
|
|
add_bufnum(bufnrs, &bufnum, buf->b_fnum);
|
|
|
|
|
|
|
|
for (i = 0; i < bufnum; ++i)
|
|
|
|
{
|
|
|
|
buf = buflist_findnr(bufnrs[i]);
|
|
|
|
if (buf == NULL)
|
|
|
|
continue;
|
|
|
|
if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-07-10 22:11:16 +02:00
|
|
|
bufref_T bufref;
|
|
|
|
|
|
|
|
set_bufref(&bufref, buf);
|
2018-03-10 20:28:12 +01:00
|
|
|
#ifdef FEAT_TERMINAL
|
|
|
|
if (term_job_running(buf->b_term))
|
|
|
|
{
|
|
|
|
if (term_try_stop_job(buf) == FAIL)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
2019-12-01 21:41:28 +01:00
|
|
|
// Try auto-writing the buffer. If this fails but the buffer no
|
|
|
|
// longer exists it's not changed, that's OK.
|
2013-11-09 03:31:51 +01:00
|
|
|
if (check_changed(buf, (p_awa ? CCGD_AW : 0)
|
|
|
|
| CCGD_MULTWIN
|
2016-07-10 22:11:16 +02:00
|
|
|
| CCGD_ALLBUF) && bufref_valid(&bufref))
|
2019-12-01 21:41:28 +01:00
|
|
|
break; // didn't save - still changes
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-23 18:39:18 +01:00
|
|
|
if (i >= bufnum)
|
|
|
|
goto theend;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Get here if "buf" cannot be abandoned.
|
2012-03-23 18:39:18 +01:00
|
|
|
ret = TRUE;
|
2004-06-13 20:20:40 +00:00
|
|
|
exiting = FALSE;
|
|
|
|
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
|
|
|
|
/*
|
|
|
|
* When ":confirm" used, don't give an error message.
|
|
|
|
*/
|
|
|
|
if (!(p_confirm || cmdmod.confirm))
|
|
|
|
#endif
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// There must be a wait_return for this message, do_buffer()
|
|
|
|
// may cause a redraw. But wait_return() is a no-op when vgetc()
|
|
|
|
// is busy (Quit used from window menu), then make sure we don't
|
|
|
|
// cause a scroll up.
|
2006-04-07 21:40:07 +00:00
|
|
|
if (vgetc_busy > 0)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
msg_row = cmdline_row;
|
|
|
|
msg_col = 0;
|
|
|
|
msg_didout = FALSE;
|
|
|
|
}
|
2017-08-03 22:44:55 +02:00
|
|
|
if (
|
|
|
|
#ifdef FEAT_TERMINAL
|
|
|
|
term_job_running(buf->b_term)
|
2019-01-13 23:38:42 +01:00
|
|
|
? semsg(_("E947: Job still running in buffer \"%s\""),
|
2017-08-03 22:44:55 +02:00
|
|
|
buf->b_fname)
|
|
|
|
:
|
|
|
|
#endif
|
2019-01-13 23:38:42 +01:00
|
|
|
semsg(_("E162: No write since last change for buffer \"%s\""),
|
2012-10-03 18:25:00 +02:00
|
|
|
buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
save = no_wait_return;
|
|
|
|
no_wait_return = FALSE;
|
|
|
|
wait_return(FALSE);
|
|
|
|
no_wait_return = save;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Try to find a window that contains the buffer.
|
2004-06-13 20:20:40 +00:00
|
|
|
if (buf != curbuf)
|
2012-03-23 18:39:18 +01:00
|
|
|
FOR_ALL_TAB_WINDOWS(tp, wp)
|
2004-06-13 20:20:40 +00:00
|
|
|
if (wp->w_buffer == buf)
|
|
|
|
{
|
2016-07-10 22:11:16 +02:00
|
|
|
bufref_T bufref;
|
|
|
|
|
|
|
|
set_bufref(&bufref, buf);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2012-03-23 18:39:18 +01:00
|
|
|
goto_tabpage_win(tp, wp);
|
2018-03-04 18:08:14 +01:00
|
|
|
|
2019-03-02 10:13:42 +01:00
|
|
|
// Paranoia: did autocmd wipe out the buffer with changes?
|
2016-07-10 22:11:16 +02:00
|
|
|
if (!bufref_valid(&bufref))
|
2012-03-23 18:39:18 +01:00
|
|
|
goto theend;
|
|
|
|
goto buf_found;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
2012-03-23 18:39:18 +01:00
|
|
|
buf_found:
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Open the changed buffer in the current window.
|
2004-06-13 20:20:40 +00:00
|
|
|
if (buf != curbuf)
|
2016-01-02 22:25:52 +01:00
|
|
|
set_curbuf(buf, unload ? DOBUF_UNLOAD : DOBUF_GOTO);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2012-03-23 18:39:18 +01:00
|
|
|
theend:
|
|
|
|
vim_free(bufnrs);
|
|
|
|
return ret;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return FAIL if there is no file name, OK if there is one
|
|
|
|
* give error message for FAIL
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 15:52:46 +01:00
|
|
|
check_fname(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (curbuf->b_ffname == NULL)
|
|
|
|
{
|
2019-01-13 23:38:42 +01:00
|
|
|
emsg(_(e_noname));
|
2004-06-13 20:20:40 +00:00
|
|
|
return FAIL;
|
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* flush the contents of a buffer, unless it has no file name
|
|
|
|
*
|
|
|
|
* return FAIL for failure, OK otherwise
|
|
|
|
*/
|
|
|
|
int
|
2016-01-30 15:52:46 +01:00
|
|
|
buf_write_all(buf_T *buf, int forceit)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
buf_T *old_curbuf = curbuf;
|
|
|
|
|
|
|
|
retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
|
|
|
|
(linenr_T)1, buf->b_ml.ml_line_count, NULL,
|
|
|
|
FALSE, forceit, TRUE, FALSE));
|
|
|
|
if (curbuf != old_curbuf)
|
2004-07-12 15:53:54 +00:00
|
|
|
{
|
2017-03-16 17:23:31 +01:00
|
|
|
msg_source(HL_ATTR(HLF_W));
|
2019-01-19 17:43:09 +01:00
|
|
|
msg(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
|
2004-07-12 15:53:54 +00:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-09-08 18:46:31 +02:00
|
|
|
* ":argdo", ":windo", ":bufdo", ":tabdo", ":cdo", ":ldo", ":cfdo" and ":lfdo"
|
2004-06-13 20:20:40 +00:00
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
ex_listdo(exarg_T *eap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
2006-02-24 23:53:04 +00:00
|
|
|
win_T *wp;
|
|
|
|
tabpage_T *tp;
|
2015-02-27 20:33:37 +01:00
|
|
|
buf_T *buf = curbuf;
|
2004-06-13 20:20:40 +00:00
|
|
|
int next_fnum = 0;
|
2018-03-04 18:08:14 +01:00
|
|
|
#if defined(FEAT_SYN_HL)
|
2004-06-13 20:20:40 +00:00
|
|
|
char_u *save_ei = NULL;
|
|
|
|
#endif
|
2004-12-19 22:46:22 +00:00
|
|
|
char_u *p_shm_save;
|
2015-09-08 18:46:31 +02:00
|
|
|
#ifdef FEAT_QUICKFIX
|
2015-09-09 22:35:29 +02:00
|
|
|
int qf_size = 0;
|
2015-09-08 18:46:31 +02:00
|
|
|
int qf_idx;
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2016-02-23 18:55:43 +01:00
|
|
|
#ifndef FEAT_QUICKFIX
|
|
|
|
if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo ||
|
|
|
|
eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
|
|
|
|
{
|
|
|
|
ex_ni(eap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-03-04 18:08:14 +01:00
|
|
|
#if defined(FEAT_SYN_HL)
|
2006-04-05 20:41:53 +00:00
|
|
|
if (eap->cmdidx != CMD_windo && eap->cmdidx != CMD_tabdo)
|
2019-08-03 13:29:46 +02:00
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Don't do syntax HL autocommands. Skipping the syntax file is a
|
|
|
|
// great speed improvement.
|
2005-01-21 11:55:25 +00:00
|
|
|
save_ei = au_event_disable(",Syntax");
|
2019-08-03 13:29:46 +02:00
|
|
|
|
2020-04-02 18:50:46 +02:00
|
|
|
FOR_ALL_BUFFERS(buf)
|
2019-08-03 13:29:46 +02:00
|
|
|
buf->b_flags &= ~BF_SYN_SET;
|
|
|
|
buf = curbuf;
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
2014-08-06 18:17:11 +02:00
|
|
|
#ifdef FEAT_CLIPBOARD
|
|
|
|
start_global_changes();
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
if (eap->cmdidx == CMD_windo
|
2006-02-24 23:53:04 +00:00
|
|
|
|| eap->cmdidx == CMD_tabdo
|
2017-08-03 22:44:55 +02:00
|
|
|
|| buf_hide(curbuf)
|
2013-11-09 03:31:51 +01:00
|
|
|
|| !check_changed(curbuf, CCGD_AW
|
|
|
|
| (eap->forceit ? CCGD_FORCEIT : 0)
|
|
|
|
| CCGD_EXCMD))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
i = 0;
|
2019-12-01 21:41:28 +01:00
|
|
|
// start at the eap->line1 argument/window/buffer
|
2006-02-24 23:53:04 +00:00
|
|
|
wp = firstwin;
|
|
|
|
tp = first_tabpage;
|
2015-01-07 16:54:21 +01:00
|
|
|
switch (eap->cmdidx)
|
|
|
|
{
|
|
|
|
case CMD_windo:
|
|
|
|
for ( ; wp != NULL && i + 1 < eap->line1; wp = wp->w_next)
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
case CMD_tabdo:
|
|
|
|
for( ; tp != NULL && i + 1 < eap->line1; tp = tp->tp_next)
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
case CMD_argdo:
|
|
|
|
i = eap->line1 - 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-12-01 21:41:28 +01:00
|
|
|
// set pcmark now
|
2004-06-13 20:20:40 +00:00
|
|
|
if (eap->cmdidx == CMD_bufdo)
|
2015-09-08 18:46:31 +02:00
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Advance to the first listed buffer after "eap->line1".
|
2015-09-08 18:46:31 +02:00
|
|
|
for (buf = firstbuf; buf != NULL && (buf->b_fnum < eap->line1
|
2015-02-27 20:33:37 +01:00
|
|
|
|| !buf->b_p_bl); buf = buf->b_next)
|
|
|
|
if (buf->b_fnum > eap->line2)
|
|
|
|
{
|
|
|
|
buf = NULL;
|
|
|
|
break;
|
|
|
|
}
|
2015-09-08 18:46:31 +02:00
|
|
|
if (buf != NULL)
|
2015-02-27 20:33:37 +01:00
|
|
|
goto_buffer(eap, DOBUF_FIRST, FORWARD, buf->b_fnum);
|
2015-09-08 18:46:31 +02:00
|
|
|
}
|
|
|
|
#ifdef FEAT_QUICKFIX
|
|
|
|
else if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
|
|
|
|
|| eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
|
|
|
|
{
|
2019-05-04 15:05:28 +02:00
|
|
|
qf_size = qf_get_valid_size(eap);
|
2015-09-08 18:46:31 +02:00
|
|
|
if (qf_size <= 0 || eap->line1 > qf_size)
|
|
|
|
buf = NULL;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ex_cc(eap);
|
|
|
|
|
|
|
|
buf = curbuf;
|
|
|
|
i = eap->line1 - 1;
|
|
|
|
if (eap->addr_count <= 0)
|
2019-12-01 21:41:28 +01:00
|
|
|
// default is all the quickfix/location list entries
|
2015-09-08 18:46:31 +02:00
|
|
|
eap->line2 = qf_size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
|
|
|
setpcmark();
|
2019-12-01 21:41:28 +01:00
|
|
|
listcmd_busy = TRUE; // avoids setting pcmark below
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2015-02-27 20:33:37 +01:00
|
|
|
while (!got_int && buf != NULL)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (eap->cmdidx == CMD_argdo)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// go to argument "i"
|
2004-06-13 20:20:40 +00:00
|
|
|
if (i == ARGCOUNT)
|
|
|
|
break;
|
2019-12-01 21:41:28 +01:00
|
|
|
// Don't call do_argfile() when already there, it will try
|
|
|
|
// reloading the file.
|
2004-09-02 19:12:26 +00:00
|
|
|
if (curwin->w_arg_idx != i || !editing_arg_idx(curwin))
|
2004-12-19 22:46:22 +00:00
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Clear 'shm' to avoid that the file message overwrites
|
|
|
|
// any output from the command.
|
2004-12-19 22:46:22 +00:00
|
|
|
p_shm_save = vim_strsave(p_shm);
|
|
|
|
set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
do_argfile(eap, i);
|
2004-12-19 22:46:22 +00:00
|
|
|
set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
|
|
|
|
vim_free(p_shm_save);
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
if (curwin->w_arg_idx != i)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (eap->cmdidx == CMD_windo)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// go to window "wp"
|
2006-02-24 23:53:04 +00:00
|
|
|
if (!win_valid(wp))
|
|
|
|
break;
|
|
|
|
win_goto(wp);
|
2007-05-03 20:11:13 +00:00
|
|
|
if (curwin != wp)
|
2019-12-01 21:41:28 +01:00
|
|
|
break; // something must be wrong
|
2006-02-24 23:53:04 +00:00
|
|
|
wp = curwin->w_next;
|
|
|
|
}
|
|
|
|
else if (eap->cmdidx == CMD_tabdo)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// go to window "tp"
|
2006-02-24 23:53:04 +00:00
|
|
|
if (!valid_tabpage(tp))
|
2004-06-13 20:20:40 +00:00
|
|
|
break;
|
2013-05-06 04:50:35 +02:00
|
|
|
goto_tabpage_tp(tp, TRUE, TRUE);
|
2006-02-24 23:53:04 +00:00
|
|
|
tp = tp->tp_next;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else if (eap->cmdidx == CMD_bufdo)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Remember the number of the next listed buffer, in case
|
|
|
|
// ":bwipe" is used or autocommands do something strange.
|
2004-06-13 20:20:40 +00:00
|
|
|
next_fnum = -1;
|
|
|
|
for (buf = curbuf->b_next; buf != NULL; buf = buf->b_next)
|
|
|
|
if (buf->b_p_bl)
|
|
|
|
{
|
|
|
|
next_fnum = buf->b_fnum;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 16:54:21 +01:00
|
|
|
++i;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// execute the command
|
2004-06-13 20:20:40 +00:00
|
|
|
do_cmdline(eap->arg, eap->getline, eap->cookie,
|
|
|
|
DOCMD_VERBOSE + DOCMD_NOWAIT);
|
|
|
|
|
|
|
|
if (eap->cmdidx == CMD_bufdo)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Done?
|
2015-01-07 16:54:21 +01:00
|
|
|
if (next_fnum < 0 || next_fnum > eap->line2)
|
2004-06-13 20:20:40 +00:00
|
|
|
break;
|
2019-12-01 21:41:28 +01:00
|
|
|
// Check if the buffer still exists.
|
2016-07-24 22:04:11 +02:00
|
|
|
FOR_ALL_BUFFERS(buf)
|
2004-06-13 20:20:40 +00:00
|
|
|
if (buf->b_fnum == next_fnum)
|
|
|
|
break;
|
|
|
|
if (buf == NULL)
|
|
|
|
break;
|
2004-12-19 22:46:22 +00:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Go to the next buffer. Clear 'shm' to avoid that the file
|
|
|
|
// message overwrites any output from the command.
|
2004-12-19 22:46:22 +00:00
|
|
|
p_shm_save = vim_strsave(p_shm);
|
|
|
|
set_option_value((char_u *)"shm", 0L, (char_u *)"", 0);
|
2004-06-13 20:20:40 +00:00
|
|
|
goto_buffer(eap, DOBUF_FIRST, FORWARD, next_fnum);
|
2004-12-19 22:46:22 +00:00
|
|
|
set_option_value((char_u *)"shm", 0L, p_shm_save, 0);
|
|
|
|
vim_free(p_shm_save);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// If autocommands took us elsewhere, quit here.
|
2004-06-13 20:20:40 +00:00
|
|
|
if (curbuf->b_fnum != next_fnum)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-09-08 18:46:31 +02:00
|
|
|
#ifdef FEAT_QUICKFIX
|
|
|
|
if (eap->cmdidx == CMD_cdo || eap->cmdidx == CMD_ldo
|
|
|
|
|| eap->cmdidx == CMD_cfdo || eap->cmdidx == CMD_lfdo)
|
|
|
|
{
|
|
|
|
if (i >= qf_size || i >= eap->line2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
qf_idx = qf_get_cur_idx(eap);
|
|
|
|
|
|
|
|
ex_cnext(eap);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// If jumping to the next quickfix entry fails, quit here
|
2015-09-08 18:46:31 +02:00
|
|
|
if (qf_get_cur_idx(eap) == qf_idx)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
if (eap->cmdidx == CMD_windo)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
validate_cursor(); // cursor may have moved
|
2018-03-04 20:14:14 +01:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// required when 'scrollbind' has been set
|
2004-06-13 20:20:40 +00:00
|
|
|
if (curwin->w_p_scb)
|
|
|
|
do_check_scrollbind(TRUE);
|
|
|
|
}
|
2015-01-07 16:54:21 +01:00
|
|
|
|
|
|
|
if (eap->cmdidx == CMD_windo || eap->cmdidx == CMD_tabdo)
|
|
|
|
if (i+1 > eap->line2)
|
|
|
|
break;
|
|
|
|
if (eap->cmdidx == CMD_argdo && i >= eap->line2)
|
|
|
|
break;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
listcmd_busy = FALSE;
|
|
|
|
}
|
|
|
|
|
2018-03-04 18:08:14 +01:00
|
|
|
#if defined(FEAT_SYN_HL)
|
2005-02-02 23:07:25 +00:00
|
|
|
if (save_ei != NULL)
|
|
|
|
{
|
2019-08-03 13:29:46 +02:00
|
|
|
buf_T *bnext;
|
|
|
|
aco_save_T aco;
|
|
|
|
|
2005-02-02 23:07:25 +00:00
|
|
|
au_event_restore(save_ei);
|
2019-08-03 13:29:46 +02:00
|
|
|
|
|
|
|
for (buf = firstbuf; buf != NULL; buf = bnext)
|
|
|
|
{
|
|
|
|
bnext = buf->b_next;
|
|
|
|
if (buf->b_nwindows > 0 && (buf->b_flags & BF_SYN_SET))
|
|
|
|
{
|
|
|
|
buf->b_flags &= ~BF_SYN_SET;
|
|
|
|
|
|
|
|
// buffer was opened while Syntax autocommands were disabled,
|
|
|
|
// need to trigger them now.
|
|
|
|
if (buf == curbuf)
|
|
|
|
apply_autocmds(EVENT_SYNTAX, curbuf->b_p_syn,
|
2005-02-02 23:07:25 +00:00
|
|
|
curbuf->b_fname, TRUE, curbuf);
|
2019-08-03 13:29:46 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
aucmd_prepbuf(&aco, buf);
|
|
|
|
apply_autocmds(EVENT_SYNTAX, buf->b_p_syn,
|
|
|
|
buf->b_fname, TRUE, buf);
|
|
|
|
aucmd_restbuf(&aco);
|
|
|
|
}
|
|
|
|
|
|
|
|
// start over, in case autocommands messed things up.
|
|
|
|
bnext = firstbuf;
|
|
|
|
}
|
|
|
|
}
|
2005-02-02 23:07:25 +00:00
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
#endif
|
2014-08-06 18:17:11 +02:00
|
|
|
#ifdef FEAT_CLIPBOARD
|
|
|
|
end_global_changes();
|
|
|
|
#endif
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FEAT_EVAL
|
|
|
|
/*
|
|
|
|
* ":compiler[!] {name}"
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
ex_compiler(exarg_T *eap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char_u *buf;
|
|
|
|
char_u *old_cur_comp = NULL;
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
if (*eap->arg == NUL)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// List all compiler scripts.
|
2004-06-13 20:20:40 +00:00
|
|
|
do_cmdline_cmd((char_u *)"echo globpath(&rtp, 'compiler/*.vim')");
|
2019-12-01 21:41:28 +01:00
|
|
|
// ) keep the indenter happy...
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-05-24 18:54:09 +02:00
|
|
|
buf = alloc(STRLEN(eap->arg) + 14);
|
2004-06-13 20:20:40 +00:00
|
|
|
if (buf != NULL)
|
|
|
|
{
|
|
|
|
if (eap->forceit)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// ":compiler! {name}" sets global options
|
2004-06-13 20:20:40 +00:00
|
|
|
do_cmdline_cmd((char_u *)
|
|
|
|
"command -nargs=* CompilerSet set <args>");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// ":compiler! {name}" sets local options.
|
|
|
|
// To remain backwards compatible "current_compiler" is always
|
|
|
|
// used. A user's compiler plugin may set it, the distributed
|
|
|
|
// plugin will then skip the settings. Afterwards set
|
|
|
|
// "b:current_compiler" and restore "current_compiler".
|
|
|
|
// Explicitly prepend "g:" to make it work in a function.
|
2010-01-19 16:13:50 +01:00
|
|
|
old_cur_comp = get_var_value((char_u *)"g:current_compiler");
|
2004-06-13 20:20:40 +00:00
|
|
|
if (old_cur_comp != NULL)
|
|
|
|
old_cur_comp = vim_strsave(old_cur_comp);
|
|
|
|
do_cmdline_cmd((char_u *)
|
|
|
|
"command -nargs=* CompilerSet setlocal <args>");
|
|
|
|
}
|
2010-01-19 16:13:50 +01:00
|
|
|
do_unlet((char_u *)"g:current_compiler", TRUE);
|
2005-01-31 19:19:04 +00:00
|
|
|
do_unlet((char_u *)"b:current_compiler", TRUE);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
|
|
|
sprintf((char *)buf, "compiler/%s.vim", eap->arg);
|
2016-03-12 22:11:39 +01:00
|
|
|
if (source_runtime(buf, DIP_ALL) == FAIL)
|
2019-01-13 23:38:42 +01:00
|
|
|
semsg(_("E666: compiler not supported: %s"), eap->arg);
|
2004-06-13 20:20:40 +00:00
|
|
|
vim_free(buf);
|
|
|
|
|
|
|
|
do_cmdline_cmd((char_u *)":delcommand CompilerSet");
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Set "b:current_compiler" from "current_compiler".
|
2010-01-19 16:13:50 +01:00
|
|
|
p = get_var_value((char_u *)"g:current_compiler");
|
2004-06-13 20:20:40 +00:00
|
|
|
if (p != NULL)
|
|
|
|
set_internal_string_var((char_u *)"b:current_compiler", p);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Restore "current_compiler" for ":compiler {name}".
|
2004-06-13 20:20:40 +00:00
|
|
|
if (!eap->forceit)
|
|
|
|
{
|
|
|
|
if (old_cur_comp != NULL)
|
|
|
|
{
|
2010-01-19 16:13:50 +01:00
|
|
|
set_internal_string_var((char_u *)"g:current_compiler",
|
2004-06-13 20:20:40 +00:00
|
|
|
old_cur_comp);
|
|
|
|
vim_free(old_cur_comp);
|
|
|
|
}
|
|
|
|
else
|
2010-01-19 16:13:50 +01:00
|
|
|
do_unlet((char_u *)"g:current_compiler", TRUE);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-01-28 16:06:38 +01:00
|
|
|
#if defined(FEAT_PYTHON3) || defined(FEAT_PYTHON) || defined(PROTO)
|
|
|
|
|
|
|
|
# if (defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)) || defined(PROTO)
|
|
|
|
/*
|
|
|
|
* Detect Python 3 or 2, and initialize 'pyxversion'.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_pyxversion(void)
|
|
|
|
{
|
|
|
|
if (p_pyx == 0)
|
|
|
|
{
|
|
|
|
if (python3_enabled(FALSE))
|
|
|
|
p_pyx = 3;
|
|
|
|
else if (python_enabled(FALSE))
|
|
|
|
p_pyx = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Does a file contain one of the following strings at the beginning of any
|
|
|
|
* line?
|
|
|
|
* "#!(any string)python2" => returns 2
|
|
|
|
* "#!(any string)python3" => returns 3
|
|
|
|
* "# requires python 2.x" => returns 2
|
|
|
|
* "# requires python 3.x" => returns 3
|
|
|
|
* otherwise return 0.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
requires_py_version(char_u *filename)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
int requires_py_version = 0;
|
|
|
|
int i, lines;
|
|
|
|
|
|
|
|
lines = (int)p_mls;
|
|
|
|
if (lines < 0)
|
|
|
|
lines = 5;
|
|
|
|
|
|
|
|
file = mch_fopen((char *)filename, "r");
|
|
|
|
if (file != NULL)
|
|
|
|
{
|
|
|
|
for (i = 0; i < lines; i++)
|
|
|
|
{
|
|
|
|
if (vim_fgets(IObuff, IOSIZE, file))
|
|
|
|
break;
|
|
|
|
if (i == 0 && IObuff[0] == '#' && IObuff[1] == '!')
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Check shebang.
|
2017-01-28 16:06:38 +01:00
|
|
|
if (strstr((char *)IObuff + 2, "python2") != NULL)
|
|
|
|
{
|
|
|
|
requires_py_version = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strstr((char *)IObuff + 2, "python3") != NULL)
|
|
|
|
{
|
|
|
|
requires_py_version = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IObuff[21] = '\0';
|
|
|
|
if (STRCMP("# requires python 2.x", IObuff) == 0)
|
|
|
|
{
|
|
|
|
requires_py_version = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (STRCMP("# requires python 3.x", IObuff) == 0)
|
|
|
|
{
|
|
|
|
requires_py_version = 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
return requires_py_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source a python file using the requested python version.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
source_pyx_file(exarg_T *eap, char_u *fname)
|
|
|
|
{
|
|
|
|
exarg_T ex;
|
|
|
|
int v = requires_py_version(fname);
|
|
|
|
|
|
|
|
# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
|
|
|
|
init_pyxversion();
|
|
|
|
# endif
|
|
|
|
if (v == 0)
|
|
|
|
{
|
|
|
|
# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
|
2019-12-01 21:41:28 +01:00
|
|
|
// user didn't choose a preference, 'pyx' is used
|
2017-01-28 16:06:38 +01:00
|
|
|
v = p_pyx;
|
|
|
|
# elif defined(FEAT_PYTHON)
|
|
|
|
v = 2;
|
|
|
|
# elif defined(FEAT_PYTHON3)
|
|
|
|
v = 3;
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now source, if required python version is not supported show
|
|
|
|
* unobtrusive message.
|
|
|
|
*/
|
|
|
|
if (eap == NULL)
|
2020-04-12 19:37:17 +02:00
|
|
|
CLEAR_FIELD(ex);
|
2017-01-28 16:06:38 +01:00
|
|
|
else
|
|
|
|
ex = *eap;
|
|
|
|
ex.arg = fname;
|
|
|
|
ex.cmd = (char_u *)(v == 2 ? "pyfile" : "pyfile3");
|
|
|
|
|
|
|
|
if (v == 2)
|
|
|
|
{
|
|
|
|
# ifdef FEAT_PYTHON
|
|
|
|
ex_pyfile(&ex);
|
|
|
|
# else
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
|
|
|
_("W20: Required python version 2.x not supported, ignoring file: %s"),
|
|
|
|
fname);
|
2019-01-19 17:43:09 +01:00
|
|
|
msg((char *)IObuff);
|
2017-01-28 16:06:38 +01:00
|
|
|
# endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
# ifdef FEAT_PYTHON3
|
|
|
|
ex_py3file(&ex);
|
|
|
|
# else
|
|
|
|
vim_snprintf((char *)IObuff, IOSIZE,
|
|
|
|
_("W21: Required python version 3.x not supported, ignoring file: %s"),
|
|
|
|
fname);
|
2019-01-19 17:43:09 +01:00
|
|
|
msg((char *)IObuff);
|
2017-01-28 16:06:38 +01:00
|
|
|
# endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ":pyxfile {fname}"
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_pyxfile(exarg_T *eap)
|
|
|
|
{
|
|
|
|
source_pyx_file(eap, eap->arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ":pyx"
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_pyx(exarg_T *eap)
|
|
|
|
{
|
|
|
|
# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
|
|
|
|
init_pyxversion();
|
|
|
|
if (p_pyx == 2)
|
|
|
|
ex_python(eap);
|
|
|
|
else
|
|
|
|
ex_py3(eap);
|
|
|
|
# elif defined(FEAT_PYTHON)
|
|
|
|
ex_python(eap);
|
|
|
|
# elif defined(FEAT_PYTHON3)
|
|
|
|
ex_py3(eap);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ":pyxdo"
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ex_pyxdo(exarg_T *eap)
|
|
|
|
{
|
|
|
|
# if defined(FEAT_PYTHON) && defined(FEAT_PYTHON3)
|
|
|
|
init_pyxversion();
|
|
|
|
if (p_pyx == 2)
|
|
|
|
ex_pydo(eap);
|
|
|
|
else
|
|
|
|
ex_py3do(eap);
|
|
|
|
# elif defined(FEAT_PYTHON)
|
|
|
|
ex_pydo(eap);
|
|
|
|
# elif defined(FEAT_PYTHON3)
|
|
|
|
ex_py3do(eap);
|
|
|
|
# endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* ":checktime [buffer]"
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
ex_checktime(exarg_T *eap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
buf_T *buf;
|
|
|
|
int save_no_check_timestamps = no_check_timestamps;
|
|
|
|
|
|
|
|
no_check_timestamps = 0;
|
2019-12-01 21:41:28 +01:00
|
|
|
if (eap->addr_count == 0) // default is all buffers
|
2004-06-13 20:20:40 +00:00
|
|
|
check_timestamps(FALSE);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buf = buflist_findnr((int)eap->line2);
|
2019-12-01 21:41:28 +01:00
|
|
|
if (buf != NULL) // cannot happen?
|
2004-06-13 20:20:40 +00:00
|
|
|
(void)buf_check_timestamp(buf, FALSE);
|
|
|
|
}
|
|
|
|
no_check_timestamps = save_no_check_timestamps;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
|
|
|
|
&& (defined(FEAT_EVAL) || defined(FEAT_MULTI_LANG))
|
2010-07-27 22:41:43 +02:00
|
|
|
# define HAVE_GET_LOCALE_VAL
|
2016-02-16 15:06:59 +01:00
|
|
|
static char_u *
|
2016-01-30 15:52:46 +01:00
|
|
|
get_locale_val(int what)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2016-02-16 15:06:59 +01:00
|
|
|
char_u *loc;
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Obtain the locale value from the libraries.
|
2016-02-16 15:06:59 +01:00
|
|
|
loc = (char_u *)setlocale(what, NULL);
|
2004-06-13 20:20:40 +00:00
|
|
|
|
2019-02-17 17:44:42 +01:00
|
|
|
# ifdef MSWIN
|
2004-06-13 20:20:40 +00:00
|
|
|
if (loc != NULL)
|
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// setocale() returns something like "LC_COLLATE=<name>;LC_..." when
|
|
|
|
// one of the values (e.g., LC_CTYPE) differs.
|
2004-06-13 20:20:40 +00:00
|
|
|
p = vim_strchr(loc, '=');
|
|
|
|
if (p != NULL)
|
|
|
|
{
|
|
|
|
loc = ++p;
|
2019-12-01 21:41:28 +01:00
|
|
|
while (*p != NUL) // remove trailing newline
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
2006-04-07 21:40:07 +00:00
|
|
|
if (*p < ' ' || *p == ';')
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
*p = NUL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2019-02-17 17:44:42 +01:00
|
|
|
#ifdef MSWIN
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* On MS-Windows locale names are strings like "German_Germany.1252", but
|
|
|
|
* gettext expects "de". Try to translate one into another here for a few
|
|
|
|
* supported languages.
|
|
|
|
*/
|
|
|
|
static char_u *
|
|
|
|
gettext_lang(char_u *name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
static char *(mtable[]) = {
|
|
|
|
"afrikaans", "af",
|
|
|
|
"czech", "cs",
|
|
|
|
"dutch", "nl",
|
|
|
|
"german", "de",
|
|
|
|
"english_united kingdom", "en_GB",
|
|
|
|
"spanish", "es",
|
|
|
|
"french", "fr",
|
|
|
|
"italian", "it",
|
|
|
|
"japanese", "ja",
|
|
|
|
"korean", "ko",
|
|
|
|
"norwegian", "no",
|
|
|
|
"polish", "pl",
|
|
|
|
"russian", "ru",
|
|
|
|
"slovak", "sk",
|
|
|
|
"swedish", "sv",
|
|
|
|
"ukrainian", "uk",
|
|
|
|
"chinese_china", "zh_CN",
|
|
|
|
"chinese_taiwan", "zh_TW",
|
|
|
|
NULL};
|
|
|
|
|
|
|
|
for (i = 0; mtable[i] != NULL; i += 2)
|
|
|
|
if (STRNICMP(mtable[i], name, STRLEN(mtable[i])) == 0)
|
2016-02-16 15:06:59 +01:00
|
|
|
return (char_u *)mtable[i + 1];
|
2004-06-13 20:20:40 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(FEAT_MULTI_LANG) || defined(PROTO)
|
2018-11-05 20:25:52 +01:00
|
|
|
/*
|
|
|
|
* Return TRUE when "lang" starts with a valid language name.
|
|
|
|
* Rejects NULL, empty string, "C", "C.UTF-8" and others.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
is_valid_mess_lang(char_u *lang)
|
|
|
|
{
|
|
|
|
return lang != NULL && ASCII_ISALPHA(lang[0]) && ASCII_ISALPHA(lang[1]);
|
|
|
|
}
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* Obtain the current messages language. Used to set the default for
|
|
|
|
* 'helplang'. May return NULL or an empty string.
|
|
|
|
*/
|
|
|
|
char_u *
|
2016-01-30 15:52:46 +01:00
|
|
|
get_mess_lang(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
|
2010-07-27 22:41:43 +02:00
|
|
|
# ifdef HAVE_GET_LOCALE_VAL
|
2004-06-13 20:20:40 +00:00
|
|
|
# if defined(LC_MESSAGES)
|
2016-02-16 15:06:59 +01:00
|
|
|
p = get_locale_val(LC_MESSAGES);
|
2004-06-13 20:20:40 +00:00
|
|
|
# else
|
2019-12-01 21:41:28 +01:00
|
|
|
// This is necessary for Win32, where LC_MESSAGES is not defined and $LANG
|
|
|
|
// may be set to the LCID number. LC_COLLATE is the best guess, LC_TIME
|
|
|
|
// and LC_MONETARY may be set differently for a Japanese working in the
|
|
|
|
// US.
|
2016-02-16 15:06:59 +01:00
|
|
|
p = get_locale_val(LC_COLLATE);
|
2004-06-13 20:20:40 +00:00
|
|
|
# endif
|
|
|
|
# else
|
|
|
|
p = mch_getenv((char_u *)"LC_ALL");
|
2018-11-05 20:25:52 +01:00
|
|
|
if (!is_valid_mess_lang(p))
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
p = mch_getenv((char_u *)"LC_MESSAGES");
|
2018-11-05 20:25:52 +01:00
|
|
|
if (!is_valid_mess_lang(p))
|
2004-06-13 20:20:40 +00:00
|
|
|
p = mch_getenv((char_u *)"LANG");
|
|
|
|
}
|
|
|
|
# endif
|
2019-02-17 17:44:42 +01:00
|
|
|
# ifdef MSWIN
|
2004-06-13 20:20:40 +00:00
|
|
|
p = gettext_lang(p);
|
|
|
|
# endif
|
2018-11-05 20:25:52 +01:00
|
|
|
return is_valid_mess_lang(p) ? p : NULL;
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Complicated #if; matches with where get_mess_env() is used below.
|
2004-12-31 20:58:58 +00:00
|
|
|
#if (defined(FEAT_EVAL) && !((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
|
|
|
|
&& defined(LC_MESSAGES))) \
|
|
|
|
|| ((defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
|
|
|
|
&& !defined(LC_MESSAGES))
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* Get the language used for messages from the environment.
|
|
|
|
*/
|
|
|
|
static char_u *
|
2016-01-30 15:52:46 +01:00
|
|
|
get_mess_env(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char_u *p;
|
|
|
|
|
|
|
|
p = mch_getenv((char_u *)"LC_ALL");
|
|
|
|
if (p == NULL || *p == NUL)
|
|
|
|
{
|
|
|
|
p = mch_getenv((char_u *)"LC_MESSAGES");
|
|
|
|
if (p == NULL || *p == NUL)
|
|
|
|
{
|
|
|
|
p = mch_getenv((char_u *)"LANG");
|
|
|
|
if (p != NULL && VIM_ISDIGIT(*p))
|
2019-12-01 21:41:28 +01:00
|
|
|
p = NULL; // ignore something like "1043"
|
2010-07-27 22:41:43 +02:00
|
|
|
# ifdef HAVE_GET_LOCALE_VAL
|
2004-06-13 20:20:40 +00:00
|
|
|
if (p == NULL || *p == NUL)
|
2016-02-16 15:06:59 +01:00
|
|
|
p = get_locale_val(LC_CTYPE);
|
2004-06-13 20:20:40 +00:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(FEAT_EVAL) || defined(PROTO)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the "v:lang" variable according to the current locale setting.
|
|
|
|
* Also do "v:lc_time"and "v:ctype".
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
set_lang_var(void)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char_u *loc;
|
|
|
|
|
2010-07-27 22:41:43 +02:00
|
|
|
# ifdef HAVE_GET_LOCALE_VAL
|
2016-02-16 15:06:59 +01:00
|
|
|
loc = get_locale_val(LC_CTYPE);
|
2004-06-13 20:20:40 +00:00
|
|
|
# else
|
2019-12-01 21:41:28 +01:00
|
|
|
// setlocale() not supported: use the default value
|
2004-06-13 20:20:40 +00:00
|
|
|
loc = (char_u *)"C";
|
|
|
|
# endif
|
|
|
|
set_vim_var_string(VV_CTYPE, loc, -1);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// When LC_MESSAGES isn't defined use the value from $LC_MESSAGES, fall
|
|
|
|
// back to LC_CTYPE if it's empty.
|
2010-07-27 22:41:43 +02:00
|
|
|
# if defined(HAVE_GET_LOCALE_VAL) && defined(LC_MESSAGES)
|
2016-02-16 15:06:59 +01:00
|
|
|
loc = get_locale_val(LC_MESSAGES);
|
2004-06-13 20:20:40 +00:00
|
|
|
# else
|
|
|
|
loc = get_mess_env();
|
|
|
|
# endif
|
|
|
|
set_vim_var_string(VV_LANG, loc, -1);
|
|
|
|
|
2010-07-27 22:41:43 +02:00
|
|
|
# ifdef HAVE_GET_LOCALE_VAL
|
2016-02-16 15:06:59 +01:00
|
|
|
loc = get_locale_val(LC_TIME);
|
2004-06-13 20:20:40 +00:00
|
|
|
# endif
|
|
|
|
set_vim_var_string(VV_LC_TIME, loc, -1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-24 15:54:21 +01:00
|
|
|
#if defined(HAVE_LOCALE_H) || defined(X_LOCALE) \
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* ":language": Set the language (locale).
|
|
|
|
*/
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
ex_language(exarg_T *eap)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
char *loc;
|
|
|
|
char_u *p;
|
|
|
|
char_u *name;
|
|
|
|
int what = LC_ALL;
|
|
|
|
char *whatstr = "";
|
|
|
|
#ifdef LC_MESSAGES
|
|
|
|
# define VIM_LC_MESSAGES LC_MESSAGES
|
|
|
|
#else
|
|
|
|
# define VIM_LC_MESSAGES 6789
|
|
|
|
#endif
|
|
|
|
|
|
|
|
name = eap->arg;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Check for "messages {name}", "ctype {name}" or "time {name}" argument.
|
|
|
|
// Allow abbreviation, but require at least 3 characters to avoid
|
|
|
|
// confusion with a two letter language name "me" or "ct".
|
2004-06-13 20:20:40 +00:00
|
|
|
p = skiptowhite(eap->arg);
|
2017-03-12 20:10:05 +01:00
|
|
|
if ((*p == NUL || VIM_ISWHITE(*p)) && p - eap->arg >= 3)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (STRNICMP(eap->arg, "messages", p - eap->arg) == 0)
|
|
|
|
{
|
|
|
|
what = VIM_LC_MESSAGES;
|
|
|
|
name = skipwhite(p);
|
|
|
|
whatstr = "messages ";
|
|
|
|
}
|
|
|
|
else if (STRNICMP(eap->arg, "ctype", p - eap->arg) == 0)
|
|
|
|
{
|
|
|
|
what = LC_CTYPE;
|
|
|
|
name = skipwhite(p);
|
|
|
|
whatstr = "ctype ";
|
|
|
|
}
|
|
|
|
else if (STRNICMP(eap->arg, "time", p - eap->arg) == 0)
|
|
|
|
{
|
|
|
|
what = LC_TIME;
|
|
|
|
name = skipwhite(p);
|
|
|
|
whatstr = "time ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*name == NUL)
|
|
|
|
{
|
|
|
|
#ifndef LC_MESSAGES
|
|
|
|
if (what == VIM_LC_MESSAGES)
|
|
|
|
p = get_mess_env();
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
p = (char_u *)setlocale(what, NULL);
|
|
|
|
if (p == NULL || *p == NUL)
|
|
|
|
p = (char_u *)"Unknown";
|
2019-01-13 23:38:42 +01:00
|
|
|
smsg(_("Current %slanguage: \"%s\""), whatstr, p);
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifndef LC_MESSAGES
|
|
|
|
if (what == VIM_LC_MESSAGES)
|
|
|
|
loc = "";
|
|
|
|
else
|
|
|
|
#endif
|
2008-06-24 22:58:06 +00:00
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
loc = setlocale(what, (char *)name);
|
2008-06-24 22:58:06 +00:00
|
|
|
#if defined(FEAT_FLOAT) && defined(LC_NUMERIC)
|
2019-12-01 21:41:28 +01:00
|
|
|
// Make sure strtod() uses a decimal point, not a comma.
|
2008-06-24 22:58:06 +00:00
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
#endif
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
if (loc == NULL)
|
2019-01-13 23:38:42 +01:00
|
|
|
semsg(_("E197: Cannot set language to \"%s\""), name);
|
2004-06-13 20:20:40 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef HAVE_NL_MSG_CAT_CNTR
|
2019-12-01 21:41:28 +01:00
|
|
|
// Need to do this for GNU gettext, otherwise cached translations
|
|
|
|
// will be used again.
|
2004-06-13 20:20:40 +00:00
|
|
|
extern int _nl_msg_cat_cntr;
|
|
|
|
|
|
|
|
++_nl_msg_cat_cntr;
|
|
|
|
#endif
|
2019-12-01 21:41:28 +01:00
|
|
|
// Reset $LC_ALL, otherwise it would overrule everything.
|
2004-06-13 20:20:40 +00:00
|
|
|
vim_setenv((char_u *)"LC_ALL", (char_u *)"");
|
|
|
|
|
|
|
|
if (what != LC_TIME)
|
|
|
|
{
|
2019-12-01 21:41:28 +01:00
|
|
|
// Tell gettext() what to translate to. It apparently doesn't
|
|
|
|
// use the currently effective locale. Also do this when
|
|
|
|
// FEAT_GETTEXT isn't defined, so that shell commands use this
|
|
|
|
// value.
|
2004-06-13 20:20:40 +00:00
|
|
|
if (what == LC_ALL)
|
2005-09-29 18:26:07 +00:00
|
|
|
{
|
2004-06-13 20:20:40 +00:00
|
|
|
vim_setenv((char_u *)"LANG", name);
|
2013-06-28 20:36:30 +02:00
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Clear $LANGUAGE because GNU gettext uses it.
|
2013-06-28 20:36:30 +02:00
|
|
|
vim_setenv((char_u *)"LANGUAGE", (char_u *)"");
|
2019-02-17 17:44:42 +01:00
|
|
|
# ifdef MSWIN
|
2019-12-01 21:41:28 +01:00
|
|
|
// Apparently MS-Windows printf() may cause a crash when
|
|
|
|
// we give it 8-bit text while it's expecting text in the
|
|
|
|
// current locale. This call avoids that.
|
2005-09-29 18:26:07 +00:00
|
|
|
setlocale(LC_CTYPE, "C");
|
|
|
|
# endif
|
|
|
|
}
|
2004-06-13 20:20:40 +00:00
|
|
|
if (what != LC_CTYPE)
|
|
|
|
{
|
|
|
|
char_u *mname;
|
2019-02-17 17:44:42 +01:00
|
|
|
#ifdef MSWIN
|
2004-06-13 20:20:40 +00:00
|
|
|
mname = gettext_lang(name);
|
|
|
|
#else
|
|
|
|
mname = name;
|
|
|
|
#endif
|
|
|
|
vim_setenv((char_u *)"LC_MESSAGES", mname);
|
|
|
|
#ifdef FEAT_MULTI_LANG
|
|
|
|
set_helplang_default(mname);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ifdef FEAT_EVAL
|
2019-12-01 21:41:28 +01:00
|
|
|
// Set v:lang, v:lc_time and v:ctype to the final result.
|
2004-06-13 20:20:40 +00:00
|
|
|
set_lang_var();
|
2011-10-20 21:41:09 +02:00
|
|
|
# endif
|
|
|
|
# ifdef FEAT_TITLE
|
|
|
|
maketitle();
|
2004-06-13 20:20:40 +00:00
|
|
|
# endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
static char_u **locales = NULL; // Array of all available locales
|
2011-05-19 18:26:40 +02:00
|
|
|
|
2019-08-18 22:26:31 +02:00
|
|
|
# ifndef MSWIN
|
2017-01-12 20:28:25 +01:00
|
|
|
static int did_init_locales = FALSE;
|
2011-05-19 18:26:40 +02:00
|
|
|
|
2019-08-18 22:26:31 +02:00
|
|
|
/*
|
|
|
|
* Return an array of strings for all available locales + NULL for the
|
|
|
|
* last element. Return NULL in case of error.
|
|
|
|
*/
|
2011-05-19 18:26:40 +02:00
|
|
|
static char_u **
|
2016-01-30 15:52:46 +01:00
|
|
|
find_locales(void)
|
2011-05-19 18:26:40 +02:00
|
|
|
{
|
|
|
|
garray_T locales_ga;
|
|
|
|
char_u *loc;
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Find all available locales by running command "locale -a". If this
|
|
|
|
// doesn't work we won't have completion.
|
2011-05-19 18:26:40 +02:00
|
|
|
char_u *locale_a = get_cmd_output((char_u *)"locale -a",
|
2014-04-05 19:44:40 +02:00
|
|
|
NULL, SHELL_SILENT, NULL);
|
2011-05-19 18:26:40 +02:00
|
|
|
if (locale_a == NULL)
|
|
|
|
return NULL;
|
|
|
|
ga_init2(&locales_ga, sizeof(char_u *), 20);
|
|
|
|
|
2019-12-01 21:41:28 +01:00
|
|
|
// Transform locale_a string where each locale is separated by "\n"
|
|
|
|
// into an array of locale strings.
|
2011-05-19 18:26:40 +02:00
|
|
|
loc = (char_u *)strtok((char *)locale_a, "\n");
|
|
|
|
|
|
|
|
while (loc != NULL)
|
|
|
|
{
|
|
|
|
if (ga_grow(&locales_ga, 1) == FAIL)
|
|
|
|
break;
|
|
|
|
loc = vim_strsave(loc);
|
|
|
|
if (loc == NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
((char_u **)locales_ga.ga_data)[locales_ga.ga_len++] = loc;
|
|
|
|
loc = (char_u *)strtok(NULL, "\n");
|
|
|
|
}
|
|
|
|
vim_free(locale_a);
|
|
|
|
if (ga_grow(&locales_ga, 1) == FAIL)
|
|
|
|
{
|
|
|
|
ga_clear(&locales_ga);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
((char_u **)locales_ga.ga_data)[locales_ga.ga_len] = NULL;
|
|
|
|
return (char_u **)locales_ga.ga_data;
|
|
|
|
}
|
2019-08-18 22:26:31 +02:00
|
|
|
# endif
|
2017-01-12 20:28:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Lazy initialization of all available locales.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
init_locales(void)
|
|
|
|
{
|
2019-02-17 17:44:42 +01:00
|
|
|
# ifndef MSWIN
|
2017-01-12 20:28:25 +01:00
|
|
|
if (!did_init_locales)
|
|
|
|
{
|
|
|
|
did_init_locales = TRUE;
|
|
|
|
locales = find_locales();
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
}
|
2011-05-19 18:26:40 +02:00
|
|
|
|
|
|
|
# if defined(EXITFREE) || defined(PROTO)
|
|
|
|
void
|
2016-01-30 15:52:46 +01:00
|
|
|
free_locales(void)
|
2011-05-19 18:26:40 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (locales != NULL)
|
|
|
|
{
|
|
|
|
for (i = 0; locales[i] != NULL; i++)
|
|
|
|
vim_free(locales[i]);
|
2018-02-10 18:45:26 +01:00
|
|
|
VIM_CLEAR(locales);
|
2011-05-19 18:26:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
# endif
|
|
|
|
|
2004-06-13 20:20:40 +00:00
|
|
|
/*
|
|
|
|
* Function given to ExpandGeneric() to obtain the possible arguments of the
|
|
|
|
* ":language" command.
|
|
|
|
*/
|
|
|
|
char_u *
|
2016-01-30 15:52:46 +01:00
|
|
|
get_lang_arg(expand_T *xp UNUSED, int idx)
|
2004-06-13 20:20:40 +00:00
|
|
|
{
|
|
|
|
if (idx == 0)
|
|
|
|
return (char_u *)"messages";
|
|
|
|
if (idx == 1)
|
|
|
|
return (char_u *)"ctype";
|
|
|
|
if (idx == 2)
|
|
|
|
return (char_u *)"time";
|
2011-05-19 18:26:40 +02:00
|
|
|
|
|
|
|
init_locales();
|
|
|
|
if (locales == NULL)
|
|
|
|
return NULL;
|
|
|
|
return locales[idx - 3];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Function given to ExpandGeneric() to obtain the available locales.
|
|
|
|
*/
|
|
|
|
char_u *
|
2016-01-30 15:52:46 +01:00
|
|
|
get_locales(expand_T *xp UNUSED, int idx)
|
2011-05-19 18:26:40 +02:00
|
|
|
{
|
|
|
|
init_locales();
|
|
|
|
if (locales == NULL)
|
|
|
|
return NULL;
|
|
|
|
return locales[idx];
|
2004-06-13 20:20:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|