2016-05-01 12:31:25 +08:00
|
|
|
// Functions for reading a character of input from stdin.
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#if HAVE_NCURSES_H
|
|
|
|
#include <ncurses.h>
|
2014-12-07 16:41:15 +08:00
|
|
|
#elif HAVE_NCURSES_CURSES_H
|
|
|
|
#include <ncurses/curses.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#else
|
|
|
|
#include <curses.h>
|
|
|
|
#endif
|
2006-01-19 20:22:07 +08:00
|
|
|
#if HAVE_TERM_H
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <term.h>
|
2006-01-19 20:22:07 +08:00
|
|
|
#elif HAVE_NCURSES_TERM_H
|
|
|
|
#include <ncurses/term.h>
|
|
|
|
#endif
|
2016-07-19 23:52:59 +08:00
|
|
|
#include <stdlib.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <wctype.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-05-01 12:31:25 +08:00
|
|
|
#include <vector>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
2005-11-25 22:18:26 +08:00
|
|
|
#include "event.h"
|
2016-05-01 12:31:25 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
|
|
|
#include "input.h"
|
|
|
|
#include "input_common.h"
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "io.h"
|
2006-02-16 21:40:25 +08:00
|
|
|
#include "output.h"
|
2016-05-01 12:31:25 +08:00
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "signal.h" // IWYU pragma: keep
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2013-11-26 11:39:54 +08:00
|
|
|
#define DEFAULT_TERM L"ansi"
|
2014-01-22 17:00:44 +08:00
|
|
|
#define MAX_INPUT_FUNCTION_ARGS 20
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Struct representing a keybinding. Returned by input_get_mappings.
|
|
|
|
struct input_mapping_t {
|
|
|
|
/// Character sequence which generates this event.
|
|
|
|
wcstring seq;
|
|
|
|
/// Commands that should be evaluated by this mapping.
|
|
|
|
wcstring_list_t commands;
|
|
|
|
/// We wish to preserve the user-specified order. This is just an incrementing value.
|
2014-02-13 04:49:32 +08:00
|
|
|
unsigned int specification_order;
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Mode in which this command should be evaluated.
|
|
|
|
wcstring mode;
|
|
|
|
/// New mode that should be switched to after command evaluation.
|
|
|
|
wcstring sets_mode;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-01 07:11:32 +08:00
|
|
|
input_mapping_t(const wcstring &s, const std::vector<wcstring> &c,
|
2016-05-01 12:31:25 +08:00
|
|
|
const wcstring &m = DEFAULT_BIND_MODE, const wcstring &sm = DEFAULT_BIND_MODE)
|
|
|
|
: seq(s), commands(c), mode(m), sets_mode(sm) {
|
2016-11-03 12:54:57 +08:00
|
|
|
static unsigned int s_last_input_map_spec_order = 0;
|
|
|
|
specification_order = ++s_last_input_map_spec_order;
|
2014-02-13 04:49:32 +08:00
|
|
|
}
|
2012-01-01 07:57:30 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// A struct representing the mapping from a terminfo key name to a terminfo character sequence.
|
|
|
|
struct terminfo_mapping_t {
|
|
|
|
const wchar_t *name; // name of key
|
|
|
|
const char *seq; // character sequence generated on keypress
|
2012-02-08 15:15:32 +08:00
|
|
|
};
|
2007-09-26 00:14:47 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Names of all the input functions supported.
|
|
|
|
static const wchar_t *const name_arr[] = {L"beginning-of-line",
|
|
|
|
L"end-of-line",
|
|
|
|
L"forward-char",
|
|
|
|
L"backward-char",
|
|
|
|
L"forward-word",
|
|
|
|
L"backward-word",
|
|
|
|
L"forward-bigword",
|
|
|
|
L"backward-bigword",
|
|
|
|
L"history-search-backward",
|
|
|
|
L"history-search-forward",
|
|
|
|
L"delete-char",
|
|
|
|
L"backward-delete-char",
|
|
|
|
L"kill-line",
|
|
|
|
L"yank",
|
|
|
|
L"yank-pop",
|
|
|
|
L"complete",
|
|
|
|
L"complete-and-search",
|
|
|
|
L"beginning-of-history",
|
|
|
|
L"end-of-history",
|
|
|
|
L"backward-kill-line",
|
|
|
|
L"kill-whole-line",
|
|
|
|
L"kill-word",
|
|
|
|
L"kill-bigword",
|
|
|
|
L"backward-kill-word",
|
|
|
|
L"backward-kill-path-component",
|
|
|
|
L"backward-kill-bigword",
|
|
|
|
L"history-token-search-backward",
|
|
|
|
L"history-token-search-forward",
|
|
|
|
L"self-insert",
|
|
|
|
L"transpose-chars",
|
|
|
|
L"transpose-words",
|
|
|
|
L"upcase-word",
|
|
|
|
L"downcase-word",
|
|
|
|
L"capitalize-word",
|
|
|
|
L"vi-arg-digit",
|
|
|
|
L"vi-delete-to",
|
|
|
|
L"execute",
|
|
|
|
L"beginning-of-buffer",
|
|
|
|
L"end-of-buffer",
|
|
|
|
L"repaint",
|
|
|
|
L"force-repaint",
|
|
|
|
L"up-line",
|
|
|
|
L"down-line",
|
|
|
|
L"suppress-autosuggestion",
|
|
|
|
L"accept-autosuggestion",
|
|
|
|
L"begin-selection",
|
|
|
|
L"swap-selection-start-stop",
|
|
|
|
L"end-selection",
|
|
|
|
L"kill-selection",
|
|
|
|
L"forward-jump",
|
|
|
|
L"backward-jump",
|
|
|
|
L"and",
|
|
|
|
L"cancel"};
|
|
|
|
|
|
|
|
wcstring describe_char(wint_t c) {
|
2014-04-28 08:23:19 +08:00
|
|
|
wint_t initial_cmd_char = R_BEGINNING_OF_LINE;
|
2016-10-10 05:36:08 +08:00
|
|
|
long name_count = sizeof(name_arr) / sizeof(*name_arr);
|
2016-05-01 12:31:25 +08:00
|
|
|
if (c >= initial_cmd_char && c < initial_cmd_char + name_count) {
|
2014-01-22 08:08:35 +08:00
|
|
|
return format_string(L"%02x (%ls)", c, name_arr[c - initial_cmd_char]);
|
|
|
|
}
|
|
|
|
return format_string(L"%02x", c);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Internal code for each supported input function.
|
|
|
|
static const wchar_t code_arr[] = {R_BEGINNING_OF_LINE,
|
|
|
|
R_END_OF_LINE,
|
|
|
|
R_FORWARD_CHAR,
|
|
|
|
R_BACKWARD_CHAR,
|
|
|
|
R_FORWARD_WORD,
|
|
|
|
R_BACKWARD_WORD,
|
|
|
|
R_FORWARD_BIGWORD,
|
|
|
|
R_BACKWARD_BIGWORD,
|
|
|
|
R_HISTORY_SEARCH_BACKWARD,
|
|
|
|
R_HISTORY_SEARCH_FORWARD,
|
|
|
|
R_DELETE_CHAR,
|
|
|
|
R_BACKWARD_DELETE_CHAR,
|
|
|
|
R_KILL_LINE,
|
|
|
|
R_YANK,
|
|
|
|
R_YANK_POP,
|
|
|
|
R_COMPLETE,
|
|
|
|
R_COMPLETE_AND_SEARCH,
|
|
|
|
R_BEGINNING_OF_HISTORY,
|
|
|
|
R_END_OF_HISTORY,
|
|
|
|
R_BACKWARD_KILL_LINE,
|
|
|
|
R_KILL_WHOLE_LINE,
|
|
|
|
R_KILL_WORD,
|
|
|
|
R_KILL_BIGWORD,
|
|
|
|
R_BACKWARD_KILL_WORD,
|
|
|
|
R_BACKWARD_KILL_PATH_COMPONENT,
|
|
|
|
R_BACKWARD_KILL_BIGWORD,
|
|
|
|
R_HISTORY_TOKEN_SEARCH_BACKWARD,
|
|
|
|
R_HISTORY_TOKEN_SEARCH_FORWARD,
|
|
|
|
R_SELF_INSERT,
|
|
|
|
R_TRANSPOSE_CHARS,
|
|
|
|
R_TRANSPOSE_WORDS,
|
|
|
|
R_UPCASE_WORD,
|
|
|
|
R_DOWNCASE_WORD,
|
|
|
|
R_CAPITALIZE_WORD,
|
|
|
|
R_VI_ARG_DIGIT,
|
|
|
|
R_VI_DELETE_TO,
|
|
|
|
R_EXECUTE,
|
|
|
|
R_BEGINNING_OF_BUFFER,
|
|
|
|
R_END_OF_BUFFER,
|
|
|
|
R_REPAINT,
|
|
|
|
R_FORCE_REPAINT,
|
|
|
|
R_UP_LINE,
|
|
|
|
R_DOWN_LINE,
|
|
|
|
R_SUPPRESS_AUTOSUGGESTION,
|
|
|
|
R_ACCEPT_AUTOSUGGESTION,
|
|
|
|
R_BEGIN_SELECTION,
|
|
|
|
R_SWAP_SELECTION_START_STOP,
|
|
|
|
R_END_SELECTION,
|
|
|
|
R_KILL_SELECTION,
|
|
|
|
R_FORWARD_JUMP,
|
|
|
|
R_BACKWARD_JUMP,
|
|
|
|
R_AND,
|
|
|
|
R_CANCEL};
|
|
|
|
|
|
|
|
/// Mappings for the current input mode.
|
2012-02-08 15:15:32 +08:00
|
|
|
static std::vector<input_mapping_t> mapping_list;
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Terminfo map list.
|
2012-02-08 15:15:32 +08:00
|
|
|
static std::vector<terminfo_mapping_t> terminfo_mappings;
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
#define TERMINFO_ADD(key) \
|
|
|
|
{ (L## #key) + 4, key }
|
2012-02-08 15:15:32 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// List of all terminfo mappings.
|
2012-02-08 15:15:32 +08:00
|
|
|
static std::vector<terminfo_mapping_t> mappings;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Set to one when the input subsytem has been initialized.
|
2013-02-16 06:00:25 +08:00
|
|
|
static bool is_init = false;
|
2006-03-10 21:38:09 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Initialize terminfo.
|
2007-09-26 00:14:47 +08:00
|
|
|
static void input_terminfo_init();
|
2012-02-08 15:15:32 +08:00
|
|
|
|
2014-03-31 04:13:35 +08:00
|
|
|
static wchar_t input_function_args[MAX_INPUT_FUNCTION_ARGS];
|
|
|
|
static bool input_function_status;
|
|
|
|
static int input_function_args_index = 0;
|
2014-01-22 17:00:44 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Return the current bind mode.
|
|
|
|
wcstring input_get_bind_mode() {
|
2014-03-31 04:13:35 +08:00
|
|
|
env_var_t mode = env_get_string(FISH_BIND_MODE_VAR);
|
|
|
|
return mode.missing() ? DEFAULT_BIND_MODE : mode;
|
2013-12-31 08:52:41 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Set the current bind mode.
|
|
|
|
void input_set_bind_mode(const wcstring &bm) {
|
2016-09-05 06:54:25 +08:00
|
|
|
// Only set this if it differs to not execute variable handlers all the time.
|
|
|
|
if (input_get_bind_mode() != bm.c_str()) {
|
|
|
|
env_set(FISH_BIND_MODE_VAR, bm.c_str(), ENV_GLOBAL);
|
|
|
|
}
|
2013-12-31 08:52:41 +08:00
|
|
|
}
|
2006-07-24 04:52:03 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Returns the arity of a given input function.
|
2016-10-23 11:32:25 +08:00
|
|
|
static int input_function_arity(int function) {
|
|
|
|
return (function == R_FORWARD_JUMP || function == R_BACKWARD_JUMP) ? 1 : 0;
|
2014-01-22 17:00:44 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Sets the return status of the most recently executed input function.
|
|
|
|
void input_function_set_status(bool status) { input_function_status = status; }
|
2014-01-23 17:23:04 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Helper function to compare the lengths of sequences.
|
|
|
|
static bool length_is_greater_than(const input_mapping_t &m1, const input_mapping_t &m2) {
|
2014-02-13 04:49:32 +08:00
|
|
|
return m1.seq.size() > m2.seq.size();
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
static bool specification_order_is_less_than(const input_mapping_t &m1, const input_mapping_t &m2) {
|
2014-02-13 04:49:32 +08:00
|
|
|
return m1.specification_order < m2.specification_order;
|
|
|
|
}
|
2006-07-24 04:52:03 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Inserts an input mapping at the correct position. We sort them in descending order by length, so
|
|
|
|
/// that we test longer sequences first.
|
|
|
|
static void input_mapping_insert_sorted(const input_mapping_t &new_mapping) {
|
|
|
|
std::vector<input_mapping_t>::iterator loc = std::lower_bound(
|
|
|
|
mapping_list.begin(), mapping_list.end(), new_mapping, length_is_greater_than);
|
2014-02-13 04:49:32 +08:00
|
|
|
mapping_list.insert(loc, new_mapping);
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Adds an input mapping.
|
|
|
|
void input_mapping_add(const wchar_t *sequence, const wchar_t *const *commands, size_t commands_len,
|
|
|
|
const wchar_t *mode, const wchar_t *sets_mode) {
|
|
|
|
CHECK(sequence, );
|
|
|
|
CHECK(commands, );
|
|
|
|
CHECK(mode, );
|
|
|
|
CHECK(sets_mode, );
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// debug( 0, L"Add mapping from %ls to %ls in mode %ls", escape(sequence, ESCAPE_ALL).c_str(),
|
|
|
|
// escape(command, ESCAPE_ALL).c_str(), mode);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// Remove existing mappings with this sequence.
|
2014-03-31 04:13:35 +08:00
|
|
|
const wcstring_list_t commands_vector(commands, commands + commands_len);
|
2014-01-01 07:11:32 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (size_t i = 0; i < mapping_list.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
input_mapping_t &m = mapping_list.at(i);
|
2016-05-01 12:31:25 +08:00
|
|
|
if (m.seq == sequence && m.mode == mode) {
|
2014-01-01 07:11:32 +08:00
|
|
|
m.commands = commands_vector;
|
2014-01-15 18:39:19 +08:00
|
|
|
m.sets_mode = sets_mode;
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// Add a new mapping, using the next order.
|
2014-03-31 04:13:35 +08:00
|
|
|
const input_mapping_t new_mapping = input_mapping_t(sequence, commands_vector, mode, sets_mode);
|
|
|
|
input_mapping_insert_sorted(new_mapping);
|
2014-01-01 07:11:32 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
void input_mapping_add(const wchar_t *sequence, const wchar_t *command, const wchar_t *mode,
|
|
|
|
const wchar_t *sets_mode) {
|
2014-01-15 18:39:19 +08:00
|
|
|
input_mapping_add(sequence, &command, 1, mode, sets_mode);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Handle interruptions to key reading by reaping finshed jobs and propagating the interrupt to the
|
|
|
|
/// reader.
|
|
|
|
static int interrupt_handler() {
|
|
|
|
// Fire any pending events.
|
2012-11-19 08:30:30 +08:00
|
|
|
event_fire(NULL);
|
2016-05-01 12:31:25 +08:00
|
|
|
// Reap stray processes, including printing exit status messages.
|
|
|
|
if (job_reap(1)) reader_repaint_needed();
|
|
|
|
// Tell the reader an event occured.
|
|
|
|
if (reader_reading_interrupted()) {
|
|
|
|
// Return 3, i.e. the character read by a Control-C.
|
2012-11-19 08:30:30 +08:00
|
|
|
return 3;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return R_NULL;
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
void update_fish_color_support(void) {
|
2016-07-19 23:01:01 +08:00
|
|
|
// Detect or infer term256 support. If fish_term256 is set, we respect it;
|
2016-07-21 19:24:10 +08:00
|
|
|
// otherwise infer it from the TERM variable or use terminfo.
|
2012-03-06 06:18:16 +08:00
|
|
|
env_var_t fish_term256 = env_get_string(L"fish_term256");
|
2016-07-21 19:24:10 +08:00
|
|
|
env_var_t term = env_get_string(L"TERM");
|
2016-07-22 10:36:32 +08:00
|
|
|
bool support_term256 = false; // default to no support
|
2016-05-01 12:31:25 +08:00
|
|
|
if (!fish_term256.missing_or_empty()) {
|
2012-03-06 06:18:16 +08:00
|
|
|
support_term256 = from_string<bool>(fish_term256);
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"256 color support determined by 'fish_term256'");
|
2016-07-21 19:24:10 +08:00
|
|
|
} else if (term.find(L"256color") != wcstring::npos) {
|
2016-07-21 22:23:14 +08:00
|
|
|
// TERM=*256color*: Explicitly supported.
|
2016-07-21 19:24:10 +08:00
|
|
|
support_term256 = true;
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"256 color support enabled for '256color' in TERM");
|
2016-07-21 19:24:10 +08:00
|
|
|
} else if (term.find(L"xterm") != wcstring::npos) {
|
|
|
|
// Assume that all xterms are 256, except for OS X SnowLeopard
|
|
|
|
const env_var_t prog = env_get_string(L"TERM_PROGRAM");
|
|
|
|
const env_var_t progver = env_get_string(L"TERM_PROGRAM_VERSION");
|
|
|
|
if (prog == L"Apple_Terminal" && !progver.missing_or_empty()) {
|
2016-07-21 22:23:14 +08:00
|
|
|
// OS X Lion is version 300+, it has 256 color support
|
2016-07-21 19:24:10 +08:00
|
|
|
if (strtod(wcs2str(progver), NULL) > 300) {
|
2016-07-19 23:52:59 +08:00
|
|
|
support_term256 = true;
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"256 color support enabled for TERM=xterm + modern Terminal.app");
|
2016-07-19 23:52:59 +08:00
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
} else {
|
2016-07-21 19:24:10 +08:00
|
|
|
support_term256 = true;
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"256 color support enabled for TERM=xterm");
|
2012-04-01 15:53:47 +08:00
|
|
|
}
|
2016-07-21 19:24:10 +08:00
|
|
|
} else if (cur_term != NULL) {
|
|
|
|
// See if terminfo happens to identify 256 colors
|
|
|
|
support_term256 = (max_colors >= 256);
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"256 color support: using %d colors per terminfo", max_colors);
|
2016-07-21 19:24:10 +08:00
|
|
|
} else {
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"256 color support not enabled (yet)");
|
2012-03-06 06:18:16 +08:00
|
|
|
}
|
2014-09-20 06:37:31 +08:00
|
|
|
|
|
|
|
env_var_t fish_term24bit = env_get_string(L"fish_term24bit");
|
|
|
|
bool support_term24bit;
|
2016-05-01 12:31:25 +08:00
|
|
|
if (!fish_term24bit.missing_or_empty()) {
|
2014-09-20 06:37:31 +08:00
|
|
|
support_term24bit = from_string<bool>(fish_term24bit);
|
2016-07-21 22:23:14 +08:00
|
|
|
debug(2, L"'fish_term24bit' preference: 24-bit color %s",
|
|
|
|
support_term24bit ? L"enabled" : L"disabled");
|
2016-05-01 12:31:25 +08:00
|
|
|
} else {
|
|
|
|
// We don't attempt to infer term24 bit support yet.
|
2014-09-20 06:37:31 +08:00
|
|
|
support_term24bit = false;
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
color_support_t support = (support_term256 ? color_support_term256 : 0) |
|
|
|
|
(support_term24bit ? color_support_term24bit : 0);
|
2014-09-20 06:37:31 +08:00
|
|
|
output_set_color_support(support);
|
2012-03-06 06:18:16 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
int input_init() {
|
|
|
|
if (is_init) return 1;
|
2013-02-16 06:00:25 +08:00
|
|
|
is_init = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
input_common_init(&interrupt_handler);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-06-02 11:03:50 +08:00
|
|
|
int err_ret;
|
|
|
|
if (setupterm(NULL, STDOUT_FILENO, &err_ret) == ERR) {
|
2016-07-22 10:36:32 +08:00
|
|
|
debug(0, _(L"Could not set up terminal"));
|
2016-06-02 11:03:50 +08:00
|
|
|
env_var_t term = env_get_string(L"TERM");
|
2016-06-22 01:28:04 +08:00
|
|
|
if (term.missing_or_empty()) {
|
2016-06-26 16:23:26 +08:00
|
|
|
debug(0, _(L"TERM environment variable not set"));
|
2016-06-22 01:28:04 +08:00
|
|
|
} else {
|
2016-06-26 16:23:26 +08:00
|
|
|
debug(0, _(L"TERM environment variable set to '%ls'"), term.c_str());
|
|
|
|
debug(0, _(L"Check that this terminal type is supported on this system"));
|
2016-06-22 01:28:04 +08:00
|
|
|
}
|
2016-06-24 13:44:58 +08:00
|
|
|
|
2016-06-02 11:03:50 +08:00
|
|
|
env_set(L"TERM", DEFAULT_TERM, ENV_GLOBAL | ENV_EXPORT);
|
|
|
|
if (setupterm(NULL, STDOUT_FILENO, &err_ret) == ERR) {
|
2016-07-22 10:36:32 +08:00
|
|
|
debug(0,
|
|
|
|
_(L"Could not set up terminal using the fallback terminal type '%ls' - exiting"),
|
2016-06-02 11:03:50 +08:00
|
|
|
DEFAULT_TERM);
|
2013-11-26 11:39:54 +08:00
|
|
|
exit_without_destructors(1);
|
2016-06-02 11:03:50 +08:00
|
|
|
} else {
|
2016-06-26 16:23:26 +08:00
|
|
|
debug(0, _(L"Using fallback terminal type '%ls'"), DEFAULT_TERM);
|
2013-11-25 00:10:41 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
input_terminfo_init();
|
2014-09-20 06:37:31 +08:00
|
|
|
update_fish_color_support();
|
2012-03-06 02:44:08 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// If we have no keybindings, add a few simple defaults.
|
|
|
|
if (mapping_list.empty()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
input_mapping_add(L"", L"self-insert");
|
|
|
|
input_mapping_add(L"\n", L"execute");
|
2015-12-01 07:44:51 +08:00
|
|
|
input_mapping_add(L"\r", L"execute");
|
2012-11-19 08:30:30 +08:00
|
|
|
input_mapping_add(L"\t", L"complete");
|
|
|
|
input_mapping_add(L"\x3", L"commandline \"\"");
|
|
|
|
input_mapping_add(L"\x4", L"exit");
|
|
|
|
input_mapping_add(L"\x5", L"bind");
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
2005-10-02 21:40:46 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
void input_destroy() {
|
|
|
|
if (!is_init) return;
|
2013-02-16 06:00:25 +08:00
|
|
|
is_init = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
input_common_destroy();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (del_curterm(cur_term) == ERR) {
|
2012-11-19 08:30:30 +08:00
|
|
|
debug(0, _(L"Error while closing terminfo"));
|
|
|
|
}
|
2005-10-02 21:40:46 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
void input_function_push_arg(wchar_t arg) {
|
2014-01-22 17:00:44 +08:00
|
|
|
input_function_args[input_function_args_index++] = arg;
|
|
|
|
}
|
2014-01-01 07:11:32 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
wchar_t input_function_pop_arg() { return input_function_args[--input_function_args_index]; }
|
2014-01-22 17:00:44 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
void input_function_push_args(int code) {
|
2014-01-22 17:00:44 +08:00
|
|
|
int arity = input_function_arity(code);
|
2016-02-03 10:13:40 +08:00
|
|
|
std::vector<wchar_t> skipped;
|
2015-09-04 03:12:56 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (int i = 0; i < arity; i++) {
|
2015-09-04 03:12:56 +08:00
|
|
|
wchar_t arg;
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// Skip and queue up any function codes. See issue #2357.
|
2016-07-12 11:31:30 +08:00
|
|
|
while ((arg = input_common_readch(0)) >= R_MIN && arg <= R_MAX) {
|
2016-02-03 10:13:40 +08:00
|
|
|
skipped.push_back(arg);
|
2015-09-04 03:12:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
input_function_push_arg(arg);
|
2014-01-22 17:00:44 +08:00
|
|
|
}
|
2015-09-04 03:12:56 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// Push the function codes back into the input stream.
|
2016-02-03 10:13:40 +08:00
|
|
|
size_t idx = skipped.size();
|
2016-05-01 12:31:25 +08:00
|
|
|
while (idx--) {
|
2016-02-03 10:13:40 +08:00
|
|
|
input_common_next_ch(skipped.at(idx));
|
|
|
|
}
|
2014-01-22 17:00:44 +08:00
|
|
|
}
|
2014-01-01 07:11:32 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Perform the action of the specified binding. allow_commands controls whether fish commands
|
|
|
|
/// should be executed, or should be deferred until later.
|
|
|
|
static void input_mapping_execute(const input_mapping_t &m, bool allow_commands) {
|
|
|
|
// has_functions: there are functions that need to be put on the input queue
|
|
|
|
// has_commands: there are shell commands that need to be evaluated
|
2015-04-17 21:49:52 +08:00
|
|
|
bool has_commands = false, has_functions = false;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (wcstring_list_t::const_iterator it = m.commands.begin(), end = m.commands.end(); it != end;
|
|
|
|
++it) {
|
2015-08-17 09:45:42 +08:00
|
|
|
if (input_function_get_code(*it) != INPUT_CODE_NONE)
|
2015-04-17 21:49:52 +08:00
|
|
|
has_functions = true;
|
|
|
|
else
|
|
|
|
has_commands = true;
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// !has_functions && !has_commands: only set bind mode
|
|
|
|
if (!has_commands && !has_functions) {
|
2015-04-17 21:49:52 +08:00
|
|
|
input_set_bind_mode(m.sets_mode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (has_commands && !allow_commands) {
|
|
|
|
// We don't want to run commands yet. Put the characters back and return R_NULL.
|
|
|
|
for (wcstring::const_reverse_iterator it = m.seq.rbegin(), end = m.seq.rend(); it != end;
|
|
|
|
++it) {
|
2015-04-17 21:49:52 +08:00
|
|
|
input_common_next_ch(*it);
|
2014-03-31 04:13:35 +08:00
|
|
|
}
|
2015-04-17 21:49:52 +08:00
|
|
|
input_common_next_ch(R_NULL);
|
2016-05-01 12:31:25 +08:00
|
|
|
return; // skip the input_set_bind_mode
|
|
|
|
} else if (has_functions && !has_commands) {
|
|
|
|
// Functions are added at the head of the input queue.
|
|
|
|
for (wcstring_list_t::const_reverse_iterator it = m.commands.rbegin(),
|
|
|
|
end = m.commands.rend();
|
|
|
|
it != end; ++it) {
|
2015-04-17 21:49:52 +08:00
|
|
|
wchar_t code = input_function_get_code(*it);
|
|
|
|
input_function_push_args(code);
|
2015-04-06 02:07:17 +08:00
|
|
|
input_common_next_ch(code);
|
2014-03-31 04:13:35 +08:00
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
} else if (has_commands && !has_functions) {
|
|
|
|
// Execute all commands.
|
|
|
|
//
|
|
|
|
// FIXME(snnw): if commands add stuff to input queue (e.g. commandline -f execute), we won't
|
|
|
|
// see that until all other commands have also been run.
|
2015-04-17 21:49:52 +08:00
|
|
|
int last_status = proc_get_last_status();
|
2016-05-01 12:31:25 +08:00
|
|
|
for (wcstring_list_t::const_iterator it = m.commands.begin(), end = m.commands.end();
|
|
|
|
it != end; ++it) {
|
2015-04-17 21:49:52 +08:00
|
|
|
parser_t::principal_parser().eval(it->c_str(), io_chain_t(), TOP);
|
Fix `commandline` behavior in bind functions
When a key is bound to a fish function, if that function invokes
`commandline`, it gets a stale copy of the commandline. This is because
any keys passed to `self-insert` (the default) don't actually get added
to the commandline until a special character is processed, such as the
R_NULL that gets returned after running a binding for a fish command.
To fix this, don't allow fish commands to be run for bindings if we're
processing more than one key. When a key wants to invoke a fish command,
instead we push the invocation sequence back onto the input, followed by
an R_NULL, and return. This causes the input loop to break out and
update the commandline. When it starts up again, it will re-process the
keys and invoke the fish command.
This is primarily an issue with pasting text that includes bound keys in
it. Typed text is slow enough that fish will update the commandline
between each character.
---
I don't know of any way to write a test for this, but the issue can be
reproduced as follows:
> bind _ 'commandline -i _'
This binds _ to a command that inserts _. Typing the following works:
> echo wat_is_it
But if you copy that line and paste it instead of typing it, the end
result looks like
> _echo wat_isit
With this fix in place, the pasted output correctly matches the typed
output.
2014-08-23 05:02:29 +08:00
|
|
|
}
|
2015-04-17 21:49:52 +08:00
|
|
|
proc_set_last_status(last_status);
|
|
|
|
input_common_next_ch(R_NULL);
|
2016-05-01 12:31:25 +08:00
|
|
|
} else {
|
|
|
|
// Invalid binding, mixed commands and functions. We would need to execute these one by
|
|
|
|
// one.
|
2015-04-17 21:49:52 +08:00
|
|
|
input_common_next_ch(R_NULL);
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-10-31 16:15:50 +08:00
|
|
|
input_set_bind_mode(m.sets_mode);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Try reading the specified function mapping.
|
|
|
|
static bool input_mapping_is_match(const input_mapping_t &m) {
|
2014-01-01 07:11:32 +08:00
|
|
|
wint_t c = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
int j;
|
|
|
|
|
2016-07-12 11:31:30 +08:00
|
|
|
debug(2, L"trying to match mapping %ls", escape(m.seq.c_str(), ESCAPE_ALL).c_str());
|
2012-01-01 07:57:30 +08:00
|
|
|
const wchar_t *str = m.seq.c_str();
|
2016-05-01 12:31:25 +08:00
|
|
|
for (j = 0; str[j] != L'\0'; j++) {
|
2013-12-31 23:16:28 +08:00
|
|
|
bool timed = (j > 0 && iswcntrl(str[0]));
|
2013-12-31 21:53:29 +08:00
|
|
|
|
2012-06-03 06:43:18 +08:00
|
|
|
c = input_common_readch(timed);
|
2016-05-01 12:31:25 +08:00
|
|
|
if (str[j] != c) {
|
2012-06-03 06:43:18 +08:00
|
|
|
break;
|
2013-12-31 21:53:29 +08:00
|
|
|
}
|
2012-06-03 06:43:18 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (str[j] == L'\0') {
|
|
|
|
// debug(0, L"matched mapping %ls (%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(),
|
|
|
|
// m.command.c_str());
|
|
|
|
// We matched the entire sequence.
|
2014-01-01 07:11:32 +08:00
|
|
|
return true;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
|
|
|
|
2016-07-12 11:31:30 +08:00
|
|
|
// Reinsert the chars we read to be read again since we didn't match the bind sequence (i.e.,
|
|
|
|
// the input mapping).
|
2016-05-05 06:19:47 +08:00
|
|
|
input_common_next_ch(c);
|
|
|
|
for (int k = j - 1; k >= 0; k--) {
|
|
|
|
input_common_next_ch(m.seq[k]);
|
2012-01-01 07:57:30 +08:00
|
|
|
}
|
2013-12-31 23:16:28 +08:00
|
|
|
|
2014-01-01 07:11:32 +08:00
|
|
|
return false;
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
void input_queue_ch(wint_t ch) { input_common_queue_ch(ch); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
static void input_mapping_execute_matching_or_generic(bool allow_commands) {
|
2014-01-01 07:11:32 +08:00
|
|
|
const input_mapping_t *generic = NULL;
|
|
|
|
|
2014-03-31 04:13:35 +08:00
|
|
|
const wcstring bind_mode = input_get_bind_mode();
|
2014-01-15 19:04:52 +08:00
|
|
|
|
2016-10-10 05:36:08 +08:00
|
|
|
for (size_t i = 0; i < mapping_list.size(); i++) {
|
2014-01-01 07:11:32 +08:00
|
|
|
const input_mapping_t &m = mapping_list.at(i);
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// debug(0, L"trying mapping (%ls,%ls,%ls)\n", escape(m.seq.c_str(), ESCAPE_ALL).c_str(),
|
2014-01-15 18:39:19 +08:00
|
|
|
// m.mode.c_str(), m.sets_mode.c_str());
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (m.mode != bind_mode) {
|
|
|
|
// debug(0, L"skipping mapping because mode %ls != %ls\n", m.mode.c_str(),
|
|
|
|
// input_get_bind_mode().c_str());
|
2014-01-01 07:11:32 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (m.seq.length() == 0) {
|
2014-01-01 07:11:32 +08:00
|
|
|
generic = &m;
|
2016-05-01 12:31:25 +08:00
|
|
|
} else if (input_mapping_is_match(m)) {
|
Fix `commandline` behavior in bind functions
When a key is bound to a fish function, if that function invokes
`commandline`, it gets a stale copy of the commandline. This is because
any keys passed to `self-insert` (the default) don't actually get added
to the commandline until a special character is processed, such as the
R_NULL that gets returned after running a binding for a fish command.
To fix this, don't allow fish commands to be run for bindings if we're
processing more than one key. When a key wants to invoke a fish command,
instead we push the invocation sequence back onto the input, followed by
an R_NULL, and return. This causes the input loop to break out and
update the commandline. When it starts up again, it will re-process the
keys and invoke the fish command.
This is primarily an issue with pasting text that includes bound keys in
it. Typed text is slow enough that fish will update the commandline
between each character.
---
I don't know of any way to write a test for this, but the issue can be
reproduced as follows:
> bind _ 'commandline -i _'
This binds _ to a command that inserts _. Typing the following works:
> echo wat_is_it
But if you copy that line and paste it instead of typing it, the end
result looks like
> _echo wat_isit
With this fix in place, the pasted output correctly matches the typed
output.
2014-08-23 05:02:29 +08:00
|
|
|
input_mapping_execute(m, allow_commands);
|
2014-01-01 07:11:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (generic) {
|
Fix `commandline` behavior in bind functions
When a key is bound to a fish function, if that function invokes
`commandline`, it gets a stale copy of the commandline. This is because
any keys passed to `self-insert` (the default) don't actually get added
to the commandline until a special character is processed, such as the
R_NULL that gets returned after running a binding for a fish command.
To fix this, don't allow fish commands to be run for bindings if we're
processing more than one key. When a key wants to invoke a fish command,
instead we push the invocation sequence back onto the input, followed by
an R_NULL, and return. This causes the input loop to break out and
update the commandline. When it starts up again, it will re-process the
keys and invoke the fish command.
This is primarily an issue with pasting text that includes bound keys in
it. Typed text is slow enough that fish will update the commandline
between each character.
---
I don't know of any way to write a test for this, but the issue can be
reproduced as follows:
> bind _ 'commandline -i _'
This binds _ to a command that inserts _. Typing the following works:
> echo wat_is_it
But if you copy that line and paste it instead of typing it, the end
result looks like
> _echo wat_isit
With this fix in place, the pasted output correctly matches the typed
output.
2014-08-23 05:02:29 +08:00
|
|
|
input_mapping_execute(*generic, allow_commands);
|
2016-05-01 12:31:25 +08:00
|
|
|
} else {
|
2016-07-12 11:31:30 +08:00
|
|
|
debug(2, L"no generic found, ignoring char...");
|
2014-01-01 07:11:32 +08:00
|
|
|
wchar_t c = input_common_readch(0);
|
2016-07-12 11:31:30 +08:00
|
|
|
if (c == R_EOF) {
|
|
|
|
input_common_next_ch(c);
|
|
|
|
}
|
2014-01-01 07:11:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Helper function. Picks through the queue of incoming characters until we get to one that's not a
|
|
|
|
/// readline function.
|
|
|
|
static wchar_t input_read_characters_only() {
|
2015-07-20 15:10:58 +08:00
|
|
|
std::vector<wchar_t> functions_to_put_back;
|
|
|
|
wchar_t char_to_return;
|
2016-05-01 12:31:25 +08:00
|
|
|
for (;;) {
|
2015-07-20 15:10:58 +08:00
|
|
|
char_to_return = input_common_readch(0);
|
|
|
|
bool is_readline_function = (char_to_return >= R_MIN && char_to_return <= R_MAX);
|
2016-05-01 12:31:25 +08:00
|
|
|
// R_NULL and R_EOF are more control characters than readline functions, so check specially
|
|
|
|
// for those.
|
|
|
|
if (!is_readline_function || char_to_return == R_NULL || char_to_return == R_EOF) {
|
2015-07-20 15:10:58 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
// This is a readline function; save it off for later re-enqueing and try again.
|
2015-07-20 15:10:58 +08:00
|
|
|
functions_to_put_back.push_back(char_to_return);
|
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
// Restore any readline functions, in reverse to preserve their original order.
|
2015-07-20 15:10:58 +08:00
|
|
|
size_t idx = functions_to_put_back.size();
|
2016-05-01 12:31:25 +08:00
|
|
|
while (idx--) {
|
2015-07-20 15:10:58 +08:00
|
|
|
input_common_next_ch(functions_to_put_back.at(idx));
|
|
|
|
}
|
|
|
|
return char_to_return;
|
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
wint_t input_readch(bool allow_commands) {
|
2012-11-19 08:30:30 +08:00
|
|
|
CHECK_BLOCK(R_NULL);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
// Clear the interrupted flag.
|
2013-01-21 05:23:27 +08:00
|
|
|
reader_reset_interrupted();
|
2016-05-01 12:31:25 +08:00
|
|
|
// Search for sequence in mapping tables.
|
|
|
|
while (1) {
|
2014-01-01 07:11:32 +08:00
|
|
|
wchar_t c = input_common_readch(0);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (c >= R_MIN && c <= R_MAX) {
|
|
|
|
switch (c) {
|
|
|
|
case R_EOF: // if it's closed, then just return
|
2014-04-01 01:01:39 +08:00
|
|
|
{
|
2014-05-26 06:22:21 +08:00
|
|
|
return R_EOF;
|
2014-04-01 01:01:39 +08:00
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
case R_SELF_INSERT: {
|
|
|
|
// Issue #1595: ensure we only insert characters, not readline functions. The
|
|
|
|
// common case is that this will be empty.
|
2015-07-20 15:10:58 +08:00
|
|
|
return input_read_characters_only();
|
2014-04-01 01:01:39 +08:00
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
case R_AND: {
|
|
|
|
if (input_function_status) {
|
2014-04-01 01:01:39 +08:00
|
|
|
return input_readch();
|
|
|
|
}
|
2016-11-03 05:22:34 +08:00
|
|
|
c = input_common_readch(0);
|
|
|
|
while (c >= R_MIN && c <= R_MAX) {
|
|
|
|
c = input_common_readch(0);
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
|
|
|
input_common_next_ch(c);
|
|
|
|
return input_readch();
|
2014-04-01 01:01:39 +08:00
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
default: { return c; }
|
2014-04-01 01:01:39 +08:00
|
|
|
}
|
2016-05-01 12:31:25 +08:00
|
|
|
} else {
|
2015-04-06 02:07:17 +08:00
|
|
|
input_common_next_ch(c);
|
Fix `commandline` behavior in bind functions
When a key is bound to a fish function, if that function invokes
`commandline`, it gets a stale copy of the commandline. This is because
any keys passed to `self-insert` (the default) don't actually get added
to the commandline until a special character is processed, such as the
R_NULL that gets returned after running a binding for a fish command.
To fix this, don't allow fish commands to be run for bindings if we're
processing more than one key. When a key wants to invoke a fish command,
instead we push the invocation sequence back onto the input, followed by
an R_NULL, and return. This causes the input loop to break out and
update the commandline. When it starts up again, it will re-process the
keys and invoke the fish command.
This is primarily an issue with pasting text that includes bound keys in
it. Typed text is slow enough that fish will update the commandline
between each character.
---
I don't know of any way to write a test for this, but the issue can be
reproduced as follows:
> bind _ 'commandline -i _'
This binds _ to a command that inserts _. Typing the following works:
> echo wat_is_it
But if you copy that line and paste it instead of typing it, the end
result looks like
> _echo wat_isit
With this fix in place, the pasted output correctly matches the typed
output.
2014-08-23 05:02:29 +08:00
|
|
|
input_mapping_execute_matching_or_generic(allow_commands);
|
2016-05-01 12:31:25 +08:00
|
|
|
// Regarding allow_commands, we're in a loop, but if a fish command
|
Fix `commandline` behavior in bind functions
When a key is bound to a fish function, if that function invokes
`commandline`, it gets a stale copy of the commandline. This is because
any keys passed to `self-insert` (the default) don't actually get added
to the commandline until a special character is processed, such as the
R_NULL that gets returned after running a binding for a fish command.
To fix this, don't allow fish commands to be run for bindings if we're
processing more than one key. When a key wants to invoke a fish command,
instead we push the invocation sequence back onto the input, followed by
an R_NULL, and return. This causes the input loop to break out and
update the commandline. When it starts up again, it will re-process the
keys and invoke the fish command.
This is primarily an issue with pasting text that includes bound keys in
it. Typed text is slow enough that fish will update the commandline
between each character.
---
I don't know of any way to write a test for this, but the issue can be
reproduced as follows:
> bind _ 'commandline -i _'
This binds _ to a command that inserts _. Typing the following works:
> echo wat_is_it
But if you copy that line and paste it instead of typing it, the end
result looks like
> _echo wat_isit
With this fix in place, the pasted output correctly matches the typed
output.
2014-08-23 05:02:29 +08:00
|
|
|
// is executed, R_NULL is unread, so the next pass through the loop
|
|
|
|
// we'll break out and return it.
|
2012-07-07 07:25:10 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
std::vector<input_mapping_name_t> input_mapping_get_names() {
|
|
|
|
// Sort the mappings by the user specification order, so we can return them in the same order
|
|
|
|
// that the user specified them in.
|
2014-02-13 04:49:32 +08:00
|
|
|
std::vector<input_mapping_t> local_list = mapping_list;
|
|
|
|
std::sort(local_list.begin(), local_list.end(), specification_order_is_less_than);
|
2014-09-23 12:04:06 +08:00
|
|
|
std::vector<input_mapping_name_t> result;
|
2014-03-31 04:13:35 +08:00
|
|
|
result.reserve(local_list.size());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (size_t i = 0; i < local_list.size(); i++) {
|
2014-02-13 04:49:32 +08:00
|
|
|
const input_mapping_t &m = local_list.at(i);
|
2014-09-23 12:04:06 +08:00
|
|
|
result.push_back((input_mapping_name_t){m.seq, m.mode});
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2014-03-31 04:13:35 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
bool input_mapping_erase(const wcstring &sequence, const wcstring &mode) {
|
2012-01-01 07:57:30 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 08:30:30 +08:00
|
|
|
bool result = false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2014-09-23 12:25:51 +08:00
|
|
|
for (std::vector<input_mapping_t>::iterator it = mapping_list.begin(), end = mapping_list.end();
|
2016-05-01 12:31:25 +08:00
|
|
|
it != end; ++it) {
|
|
|
|
if (sequence == it->seq && mode == it->mode) {
|
2014-09-23 12:04:06 +08:00
|
|
|
mapping_list.erase(it);
|
2012-11-19 08:30:30 +08:00
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
bool input_mapping_get(const wcstring &sequence, const wcstring &mode, wcstring_list_t *out_cmds,
|
|
|
|
wcstring *out_sets_mode) {
|
2014-03-31 04:13:35 +08:00
|
|
|
bool result = false;
|
2016-05-01 12:31:25 +08:00
|
|
|
for (std::vector<input_mapping_t>::const_iterator it = mapping_list.begin(),
|
|
|
|
end = mapping_list.end();
|
|
|
|
it != end; ++it) {
|
|
|
|
if (sequence == it->seq && mode == it->mode) {
|
2014-09-23 12:04:06 +08:00
|
|
|
*out_cmds = it->commands;
|
|
|
|
*out_sets_mode = it->sets_mode;
|
2014-03-31 04:13:35 +08:00
|
|
|
result = true;
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2014-03-31 04:13:35 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2007-09-29 05:32:27 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
/// Add all terminfo mappings.
|
|
|
|
static void input_terminfo_init() {
|
|
|
|
const terminfo_mapping_t tinfos[] = {
|
2012-11-19 08:30:30 +08:00
|
|
|
TERMINFO_ADD(key_a1),
|
|
|
|
TERMINFO_ADD(key_a3),
|
|
|
|
TERMINFO_ADD(key_b2),
|
|
|
|
TERMINFO_ADD(key_backspace),
|
|
|
|
TERMINFO_ADD(key_beg),
|
|
|
|
TERMINFO_ADD(key_btab),
|
|
|
|
TERMINFO_ADD(key_c1),
|
|
|
|
TERMINFO_ADD(key_c3),
|
|
|
|
TERMINFO_ADD(key_cancel),
|
|
|
|
TERMINFO_ADD(key_catab),
|
|
|
|
TERMINFO_ADD(key_clear),
|
|
|
|
TERMINFO_ADD(key_close),
|
|
|
|
TERMINFO_ADD(key_command),
|
|
|
|
TERMINFO_ADD(key_copy),
|
|
|
|
TERMINFO_ADD(key_create),
|
|
|
|
TERMINFO_ADD(key_ctab),
|
|
|
|
TERMINFO_ADD(key_dc),
|
|
|
|
TERMINFO_ADD(key_dl),
|
|
|
|
TERMINFO_ADD(key_down),
|
|
|
|
TERMINFO_ADD(key_eic),
|
|
|
|
TERMINFO_ADD(key_end),
|
|
|
|
TERMINFO_ADD(key_enter),
|
|
|
|
TERMINFO_ADD(key_eol),
|
|
|
|
TERMINFO_ADD(key_eos),
|
|
|
|
TERMINFO_ADD(key_exit),
|
|
|
|
TERMINFO_ADD(key_f0),
|
|
|
|
TERMINFO_ADD(key_f1),
|
|
|
|
TERMINFO_ADD(key_f2),
|
|
|
|
TERMINFO_ADD(key_f3),
|
|
|
|
TERMINFO_ADD(key_f4),
|
|
|
|
TERMINFO_ADD(key_f5),
|
|
|
|
TERMINFO_ADD(key_f6),
|
|
|
|
TERMINFO_ADD(key_f7),
|
|
|
|
TERMINFO_ADD(key_f8),
|
|
|
|
TERMINFO_ADD(key_f9),
|
|
|
|
TERMINFO_ADD(key_f10),
|
|
|
|
TERMINFO_ADD(key_f11),
|
|
|
|
TERMINFO_ADD(key_f12),
|
|
|
|
TERMINFO_ADD(key_f13),
|
|
|
|
TERMINFO_ADD(key_f14),
|
|
|
|
TERMINFO_ADD(key_f15),
|
|
|
|
TERMINFO_ADD(key_f16),
|
|
|
|
TERMINFO_ADD(key_f17),
|
|
|
|
TERMINFO_ADD(key_f18),
|
|
|
|
TERMINFO_ADD(key_f19),
|
|
|
|
TERMINFO_ADD(key_f20),
|
2016-05-01 12:31:25 +08:00
|
|
|
#if 0
|
|
|
|
// I know of no keyboard with more than 20 function keys, so adding the rest here makes very
|
|
|
|
// little sense, since it will take up a lot of room in any listings (like tab completions),
|
|
|
|
// but with no benefit.
|
2012-11-19 08:30:30 +08:00
|
|
|
TERMINFO_ADD(key_f21),
|
|
|
|
TERMINFO_ADD(key_f22),
|
|
|
|
TERMINFO_ADD(key_f23),
|
|
|
|
TERMINFO_ADD(key_f24),
|
|
|
|
TERMINFO_ADD(key_f25),
|
|
|
|
TERMINFO_ADD(key_f26),
|
|
|
|
TERMINFO_ADD(key_f27),
|
|
|
|
TERMINFO_ADD(key_f28),
|
|
|
|
TERMINFO_ADD(key_f29),
|
|
|
|
TERMINFO_ADD(key_f30),
|
|
|
|
TERMINFO_ADD(key_f31),
|
|
|
|
TERMINFO_ADD(key_f32),
|
|
|
|
TERMINFO_ADD(key_f33),
|
|
|
|
TERMINFO_ADD(key_f34),
|
|
|
|
TERMINFO_ADD(key_f35),
|
|
|
|
TERMINFO_ADD(key_f36),
|
|
|
|
TERMINFO_ADD(key_f37),
|
|
|
|
TERMINFO_ADD(key_f38),
|
|
|
|
TERMINFO_ADD(key_f39),
|
|
|
|
TERMINFO_ADD(key_f40),
|
|
|
|
TERMINFO_ADD(key_f41),
|
|
|
|
TERMINFO_ADD(key_f42),
|
|
|
|
TERMINFO_ADD(key_f43),
|
|
|
|
TERMINFO_ADD(key_f44),
|
|
|
|
TERMINFO_ADD(key_f45),
|
|
|
|
TERMINFO_ADD(key_f46),
|
|
|
|
TERMINFO_ADD(key_f47),
|
|
|
|
TERMINFO_ADD(key_f48),
|
|
|
|
TERMINFO_ADD(key_f49),
|
|
|
|
TERMINFO_ADD(key_f50),
|
|
|
|
TERMINFO_ADD(key_f51),
|
|
|
|
TERMINFO_ADD(key_f52),
|
|
|
|
TERMINFO_ADD(key_f53),
|
|
|
|
TERMINFO_ADD(key_f54),
|
|
|
|
TERMINFO_ADD(key_f55),
|
|
|
|
TERMINFO_ADD(key_f56),
|
|
|
|
TERMINFO_ADD(key_f57),
|
|
|
|
TERMINFO_ADD(key_f58),
|
|
|
|
TERMINFO_ADD(key_f59),
|
|
|
|
TERMINFO_ADD(key_f60),
|
|
|
|
TERMINFO_ADD(key_f61),
|
|
|
|
TERMINFO_ADD(key_f62),
|
2016-05-01 12:31:25 +08:00
|
|
|
TERMINFO_ADD(key_f63),
|
|
|
|
#endif
|
2012-11-19 08:30:30 +08:00
|
|
|
TERMINFO_ADD(key_find),
|
|
|
|
TERMINFO_ADD(key_help),
|
|
|
|
TERMINFO_ADD(key_home),
|
|
|
|
TERMINFO_ADD(key_ic),
|
|
|
|
TERMINFO_ADD(key_il),
|
|
|
|
TERMINFO_ADD(key_left),
|
|
|
|
TERMINFO_ADD(key_ll),
|
|
|
|
TERMINFO_ADD(key_mark),
|
|
|
|
TERMINFO_ADD(key_message),
|
|
|
|
TERMINFO_ADD(key_move),
|
|
|
|
TERMINFO_ADD(key_next),
|
|
|
|
TERMINFO_ADD(key_npage),
|
|
|
|
TERMINFO_ADD(key_open),
|
|
|
|
TERMINFO_ADD(key_options),
|
|
|
|
TERMINFO_ADD(key_ppage),
|
|
|
|
TERMINFO_ADD(key_previous),
|
|
|
|
TERMINFO_ADD(key_print),
|
|
|
|
TERMINFO_ADD(key_redo),
|
|
|
|
TERMINFO_ADD(key_reference),
|
|
|
|
TERMINFO_ADD(key_refresh),
|
|
|
|
TERMINFO_ADD(key_replace),
|
|
|
|
TERMINFO_ADD(key_restart),
|
|
|
|
TERMINFO_ADD(key_resume),
|
|
|
|
TERMINFO_ADD(key_right),
|
|
|
|
TERMINFO_ADD(key_save),
|
|
|
|
TERMINFO_ADD(key_sbeg),
|
|
|
|
TERMINFO_ADD(key_scancel),
|
|
|
|
TERMINFO_ADD(key_scommand),
|
|
|
|
TERMINFO_ADD(key_scopy),
|
|
|
|
TERMINFO_ADD(key_screate),
|
|
|
|
TERMINFO_ADD(key_sdc),
|
|
|
|
TERMINFO_ADD(key_sdl),
|
|
|
|
TERMINFO_ADD(key_select),
|
|
|
|
TERMINFO_ADD(key_send),
|
|
|
|
TERMINFO_ADD(key_seol),
|
|
|
|
TERMINFO_ADD(key_sexit),
|
|
|
|
TERMINFO_ADD(key_sf),
|
|
|
|
TERMINFO_ADD(key_sfind),
|
|
|
|
TERMINFO_ADD(key_shelp),
|
|
|
|
TERMINFO_ADD(key_shome),
|
|
|
|
TERMINFO_ADD(key_sic),
|
|
|
|
TERMINFO_ADD(key_sleft),
|
|
|
|
TERMINFO_ADD(key_smessage),
|
|
|
|
TERMINFO_ADD(key_smove),
|
|
|
|
TERMINFO_ADD(key_snext),
|
|
|
|
TERMINFO_ADD(key_soptions),
|
|
|
|
TERMINFO_ADD(key_sprevious),
|
|
|
|
TERMINFO_ADD(key_sprint),
|
|
|
|
TERMINFO_ADD(key_sr),
|
|
|
|
TERMINFO_ADD(key_sredo),
|
|
|
|
TERMINFO_ADD(key_sreplace),
|
|
|
|
TERMINFO_ADD(key_sright),
|
|
|
|
TERMINFO_ADD(key_srsume),
|
|
|
|
TERMINFO_ADD(key_ssave),
|
|
|
|
TERMINFO_ADD(key_ssuspend),
|
|
|
|
TERMINFO_ADD(key_stab),
|
|
|
|
TERMINFO_ADD(key_sundo),
|
|
|
|
TERMINFO_ADD(key_suspend),
|
|
|
|
TERMINFO_ADD(key_undo),
|
|
|
|
TERMINFO_ADD(key_up)
|
2012-07-18 03:47:01 +08:00
|
|
|
};
|
|
|
|
const size_t count = sizeof tinfos / sizeof *tinfos;
|
|
|
|
terminfo_mappings.reserve(terminfo_mappings.size() + count);
|
|
|
|
terminfo_mappings.insert(terminfo_mappings.end(), tinfos, tinfos + count);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
bool input_terminfo_get_sequence(const wchar_t *name, wcstring *out_seq) {
|
2012-02-08 15:17:20 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const char *res = 0;
|
|
|
|
int err = ENOENT;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
CHECK(name, 0);
|
|
|
|
input_init();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (size_t i = 0; i < terminfo_mappings.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
const terminfo_mapping_t &m = terminfo_mappings.at(i);
|
2016-05-01 12:31:25 +08:00
|
|
|
if (!wcscmp(name, m.name)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
res = m.seq;
|
|
|
|
err = EILSEQ;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (!res) {
|
2012-11-19 08:30:30 +08:00
|
|
|
errno = err;
|
2013-04-16 04:07:17 +08:00
|
|
|
return false;
|
2013-05-05 17:33:17 +08:00
|
|
|
}
|
|
|
|
|
2013-04-16 04:07:17 +08:00
|
|
|
*out_seq = format_string(L"%s", res);
|
|
|
|
return true;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
bool input_terminfo_get_name(const wcstring &seq, wcstring *out_name) {
|
2012-11-19 08:30:30 +08:00
|
|
|
input_init();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (size_t i = 0; i < terminfo_mappings.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
terminfo_mapping_t &m = terminfo_mappings.at(i);
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (!m.seq) {
|
2012-11-19 08:30:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
const wcstring map_buf = format_string(L"%s", m.seq);
|
|
|
|
if (map_buf == seq) {
|
2014-07-08 01:45:26 +08:00
|
|
|
out_name->assign(m.name);
|
2012-01-01 07:57:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return false;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
wcstring_list_t input_terminfo_get_names(bool skip_null) {
|
2012-02-08 09:06:45 +08:00
|
|
|
wcstring_list_t result;
|
2012-02-08 15:15:32 +08:00
|
|
|
result.reserve(terminfo_mappings.size());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
input_init();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
for (size_t i = 0; i < terminfo_mappings.size(); i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
terminfo_mapping_t &m = terminfo_mappings.at(i);
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
if (skip_null && !m.seq) {
|
2012-11-19 08:30:30 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-08 15:15:32 +08:00
|
|
|
result.push_back(wcstring(m.name));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-02-08 09:06:45 +08:00
|
|
|
return result;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
wcstring_list_t input_function_get_names(void) {
|
2012-01-24 12:48:47 +08:00
|
|
|
size_t count = sizeof name_arr / sizeof *name_arr;
|
|
|
|
return wcstring_list_t(name_arr, name_arr + count);
|
2007-09-26 00:14:47 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 12:31:25 +08:00
|
|
|
wchar_t input_function_get_code(const wcstring &name) {
|
|
|
|
for (size_t i = 0; i < sizeof code_arr / sizeof *code_arr; i++) {
|
|
|
|
if (name == name_arr[i]) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return code_arr[i];
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2015-08-17 09:45:42 +08:00
|
|
|
return INPUT_CODE_NONE;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|