/* vi:set ts=8 sts=4 sw=4 noet:
*
* 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.
*/
/*
* ui.c: functions that handle the user interface.
* 1. Keyboard input stuff, and a bit of windowing stuff. These are called
* before the machine specific stuff (mch_*) so that we can call the GUI
* stuff instead if the GUI is running.
* 2. Input buffer stuff.
*/
#include "vim.h"
void
ui_write(char_u *s, int len, int console UNUSED)
{
#ifdef FEAT_GUI
if (gui.in_use && !gui.dying && !gui.starting
# ifndef NO_CONSOLE
&& !console
# endif
)
{
gui_write(s, len);
if (p_wd)
gui_wait_for_chars(p_wd, typebuf.tb_change_cnt);
return;
}
#endif
#ifndef NO_CONSOLE
// Don't output anything in silent mode ("ex -s") unless 'verbose' set
if (!(silent_mode && p_verbose == 0))
{
# if !defined(MSWIN)
char_u *tofree = NULL;
if (output_conv.vc_type != CONV_NONE)
{
// Convert characters from 'encoding' to 'termencoding'.
tofree = string_convert(&output_conv, s, &len);
if (tofree != NULL)
s = tofree;
}
# endif
mch_write(s, len);
# if defined(HAVE_FSYNC)
if (console && s[len - 1] == '\n')
vim_fsync(1);
# endif
# if !defined(MSWIN)
if (output_conv.vc_type != CONV_NONE)
vim_free(tofree);
# endif
}
#endif
}
#if defined(UNIX) || defined(VMS) || defined(PROTO) || defined(MSWIN)
/*
* When executing an external program, there may be some typed characters that
* are not consumed by it. Give them back to ui_inchar() and they are stored
* here for the next call.
*/
static char_u *ta_str = NULL;
static int ta_off; // offset for next char to use when ta_str != NULL
static int ta_len; // length of ta_str when it's not NULL
void
ui_inchar_undo(char_u *s, int len)
{
char_u *new;
int newlen;
newlen = len;
if (ta_str != NULL)
newlen += ta_len - ta_off;
new = alloc(newlen);
if (new != NULL)
{
if (ta_str != NULL)
{
mch_memmove(new, ta_str + ta_off, (size_t)(ta_len - ta_off));
mch_memmove(new + ta_len - ta_off, s, (size_t)len);
vim_free(ta_str);
}
else
mch_memmove(new, s, (size_t)len);
ta_str = new;
ta_len = newlen;
ta_off = 0;
}
}
#endif
/*
* ui_inchar(): low level input function.
* Get characters from the keyboard.
* Return the number of characters that are available.
* If "wtime" == 0 do not wait for characters.
* If "wtime" == -1 wait forever for characters.
* If "wtime" > 0 wait "wtime" milliseconds for a character.
*
* "tb_change_cnt" is the value of typebuf.tb_change_cnt if "buf" points into
* it. When typebuf.tb_change_cnt changes (e.g., when a message is received
* from a remote client) "buf" can no longer be used. "tb_change_cnt" is NULL
* otherwise.
*/
int
ui_inchar(
char_u *buf,
int maxlen,
long wtime, // don't use "time", MIPS cannot handle it
int tb_change_cnt)
{
int retval = 0;
#if defined(FEAT_GUI) && (defined(UNIX) || defined(VMS))
/*
* Use the typeahead if there is any.
*/
if (ta_str != NULL)
{
if