2016-05-04 01:36:22 +08:00
|
|
|
// Functions for reading data from stdin and passing to the parser. If stdin is a keyboard, it
|
|
|
|
// supplies a killring, history, syntax highlighting, tab-completion and various other interactive
|
|
|
|
// features.
|
|
|
|
//
|
|
|
|
// Internally the interactive mode functions rely in the functions of the input library to read
|
|
|
|
// individual characters of input.
|
|
|
|
//
|
|
|
|
// Token search is handled incrementally. Actual searches are only done on when searching backwards,
|
|
|
|
// since the previous results are saved. The last search position is remembered and a new search
|
|
|
|
// continues from the last search position. All search results are saved in the list 'search_prev'.
|
|
|
|
// When the user searches forward, i.e. presses Alt-down, the list is consulted for previous search
|
|
|
|
// result, and subsequent backwards searches are also handled by consulting the list up until the
|
|
|
|
// end of the list is reached, at which point regular searching will commence.
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
// IWYU pragma: no_include <type_traits>
|
2016-05-04 01:36:22 +08:00
|
|
|
#include <errno.h>
|
2016-12-20 12:35:57 +08:00
|
|
|
#include <fcntl.h>
|
2016-05-04 01:36:22 +08:00
|
|
|
#include <pthread.h>
|
2016-12-20 12:35:57 +08:00
|
|
|
#ifdef HAVE_SIGINFO_H
|
|
|
|
#include <siginfo.h>
|
|
|
|
#endif
|
|
|
|
#include <signal.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdio.h>
|
2016-05-04 01:36:22 +08:00
|
|
|
#include <stdlib.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <string.h>
|
2016-12-20 12:35:57 +08:00
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
2016-05-04 01:36:22 +08:00
|
|
|
#include <sys/time.h>
|
2016-12-20 12:35:57 +08:00
|
|
|
#include <sys/types.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <termios.h>
|
2006-12-01 07:57:49 +08:00
|
|
|
#include <time.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <unistd.h>
|
2016-12-20 12:35:57 +08:00
|
|
|
#include <wchar.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <wctype.h>
|
2016-12-20 12:35:57 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
#include <algorithm>
|
2017-03-27 04:44:27 +08:00
|
|
|
#include <atomic>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <csignal>
|
|
|
|
#include <functional>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2017-01-27 12:00:43 +08:00
|
|
|
#include <stack>
|
2005-12-26 06:00:44 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "color.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "common.h"
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "complete.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "env.h"
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "event.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "exec.h"
|
|
|
|
#include "expand.h"
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "function.h"
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "highlight.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "input.h"
|
|
|
|
#include "input_common.h"
|
2012-02-03 07:05:08 +08:00
|
|
|
#include "intern.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.h"
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "iothread.h"
|
|
|
|
#include "kill.h"
|
|
|
|
#include "output.h"
|
|
|
|
#include "pager.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "parse_constants.h"
|
2016-05-04 01:36:22 +08:00
|
|
|
#include "parse_tree.h"
|
|
|
|
#include "parse_util.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "sanity.h"
|
|
|
|
#include "screen.h"
|
|
|
|
#include "signal.h"
|
|
|
|
#include "tokenizer.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-01-07 13:14:29 +08:00
|
|
|
// Name of the variable that tells how long it took, in milliseconds, for the previous
|
|
|
|
// interactive command to complete.
|
|
|
|
#define ENV_CMD_DURATION L"CMD_DURATION"
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Maximum length of prefix string when printing completion list. Longer prefixes will be
|
|
|
|
/// ellipsized.
|
2012-11-10 08:02:19 +08:00
|
|
|
#define PREFIX_MAX_LEN 9
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// A simple prompt for reading shell commands that does not rely on fish specific commands, meaning
|
|
|
|
/// it will work even if fish is not installed. This is used by read_i.
|
2013-08-14 17:21:17 +08:00
|
|
|
#define DEFAULT_PROMPT L"echo -n \"$USER@\"(hostname|cut -d . -f 1)' '(__fish_pwd)'> '"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The name of the function that prints the fish prompt.
|
2012-11-08 11:59:20 +08:00
|
|
|
#define LEFT_PROMPT_FUNCTION_NAME L"fish_prompt"
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The name of the function that prints the fish right prompt (RPROMPT).
|
2012-11-08 11:59:20 +08:00
|
|
|
#define RIGHT_PROMPT_FUNCTION_NAME L"fish_right_prompt"
|
|
|
|
|
2017-06-20 13:57:32 +08:00
|
|
|
/// The name of the function to use in place of the left prompt if we're in the debugger context.
|
|
|
|
#define DEBUG_PROMPT_FUNCTION_NAME L"fish_breakpoint_prompt"
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The name of the function for getting the input mode indicator.
|
2015-06-05 04:31:48 +08:00
|
|
|
#define MODE_PROMPT_FUNCTION_NAME L"fish_mode_prompt"
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The default title for the reader. This is used by reader_readline.
|
2013-08-14 17:21:17 +08:00
|
|
|
#define DEFAULT_TITLE L"echo $_ \" \"; __fish_pwd"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The maximum number of characters to read from the keyboard without repainting. Note that this
|
|
|
|
/// readahead will only occur if new characters are available for reading, fish will never block for
|
|
|
|
/// more input without repainting.
|
2005-10-13 22:08:33 +08:00
|
|
|
#define READAHEAD_MAX 256
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// A mode for calling the reader_kill function. In this mode, the new string is appended to the
|
|
|
|
/// current contents of the kill buffer.
|
2006-10-12 21:27:32 +08:00
|
|
|
#define KILL_APPEND 0
|
2016-05-04 01:36:22 +08:00
|
|
|
|
|
|
|
/// A mode for calling the reader_kill function. In this mode, the new string is prepended to the
|
|
|
|
/// current contents of the kill buffer.
|
2006-10-12 21:27:32 +08:00
|
|
|
#define KILL_PREPEND 1
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// History search mode. This value means that no search is currently performed.
|
2007-04-17 05:26:15 +08:00
|
|
|
#define NO_SEARCH 0
|
2016-05-04 01:36:22 +08:00
|
|
|
|
|
|
|
/// History search mode. This value means that we are performing a line history search.
|
2007-04-17 05:26:15 +08:00
|
|
|
#define LINE_SEARCH 1
|
2016-05-04 01:36:22 +08:00
|
|
|
|
|
|
|
/// History search mode. This value means that we are performing a token history search.
|
2007-04-17 05:26:15 +08:00
|
|
|
#define TOKEN_SEARCH 2
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// History search mode. This value means we are searching backwards.
|
2007-04-17 05:26:15 +08:00
|
|
|
#define SEARCH_BACKWARD 0
|
2016-05-04 01:36:22 +08:00
|
|
|
|
|
|
|
/// History search mode. This value means we are searching forwards.
|
2007-04-17 05:26:15 +08:00
|
|
|
#define SEARCH_FORWARD 1
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Any time the contents of a buffer changes, we update the generation count. This allows for our
|
2017-03-27 04:44:27 +08:00
|
|
|
/// background threads to notice it and skip doing work that they would otherwise have to do.
|
|
|
|
static std::atomic<unsigned int> s_generation_count;
|
2013-01-23 07:19:29 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// This pthreads generation count is set when an autosuggestion background thread starts up, so it
|
|
|
|
/// can easily check if the work it is doing is no longer useful.
|
2013-01-24 21:29:13 +08:00
|
|
|
static pthread_key_t generation_count_key;
|
2012-02-29 04:40:20 +08:00
|
|
|
|
2017-03-27 04:44:27 +08:00
|
|
|
/// Helper to get the generation count
|
|
|
|
static unsigned int read_generation_count() {
|
|
|
|
return s_generation_count.load(std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
static void set_command_line_and_position(editable_line_t *el, const wcstring &new_str, size_t pos);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void editable_line_t::insert_string(const wcstring &str, size_t start, size_t len) {
|
|
|
|
// Clamp the range to something valid.
|
2014-07-26 03:23:42 +08:00
|
|
|
size_t string_length = str.size();
|
2016-10-21 09:53:31 +08:00
|
|
|
start = mini(start, string_length); //!OCLINT(parameter reassignment)
|
|
|
|
len = mini(len, string_length - start); //!OCLINT(parameter reassignment)
|
2014-07-26 03:23:42 +08:00
|
|
|
this->text.insert(this->position, str, start, len);
|
|
|
|
this->position += len;
|
2014-01-27 06:47:15 +08:00
|
|
|
}
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// A struct describing the state of the interactive reader. These states can be stacked, in case
|
|
|
|
/// reader_readline() calls are nested. This happens when the 'read' builtin is used.
|
|
|
|
class reader_data_t {
|
|
|
|
public:
|
|
|
|
/// String containing the whole current commandline.
|
2014-01-26 16:41:30 +08:00
|
|
|
editable_line_t command_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// String containing the autosuggestion.
|
2012-02-07 02:52:13 +08:00
|
|
|
wcstring autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Current pager.
|
2014-01-20 15:52:35 +08:00
|
|
|
pager_t pager;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Current page rendering.
|
2014-01-18 04:04:03 +08:00
|
|
|
page_rendering_t current_page_rendering;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Whether autosuggesting is allowed at all.
|
2012-11-18 18:43:35 +08:00
|
|
|
bool allow_autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// When backspacing, we temporarily suppress autosuggestions.
|
2012-02-07 11:31:06 +08:00
|
|
|
bool suppress_autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Whether abbreviations are expanded.
|
2013-07-17 15:38:04 +08:00
|
|
|
bool expand_abbreviations;
|
2017-04-10 02:48:21 +08:00
|
|
|
/// Silent mode used for password input on the read command
|
|
|
|
bool silent;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The representation of the current screen contents.
|
2012-11-19 08:30:30 +08:00
|
|
|
screen_t screen;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The history.
|
2012-02-06 08:42:24 +08:00
|
|
|
history_t *history;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// String containing the current search item.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring search_buff;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// History search.
|
2012-02-06 08:42:24 +08:00
|
|
|
history_search_t history_search;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Saved position used by token history search.
|
2015-07-26 14:05:47 +08:00
|
|
|
size_t token_history_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Saved search string for token history search. Not handled by command_line_changed.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring token_history_buff;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// List for storing previous search results. Used to avoid duplicates.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t search_prev;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The current position in search_prev.
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t search_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Indicates whether a selection is currently active.
|
2014-01-15 18:39:19 +08:00
|
|
|
bool sel_active;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The position of the cursor, when selection was initiated.
|
2014-01-15 22:07:22 +08:00
|
|
|
size_t sel_begin_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The start position of the current selection, if one.
|
2014-01-15 18:39:19 +08:00
|
|
|
size_t sel_start_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The stop position of the current selection, if one.
|
2014-01-15 18:39:19 +08:00
|
|
|
size_t sel_stop_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Name of the current application.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring app_name;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The prompt commands.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring left_prompt;
|
2012-11-08 11:59:20 +08:00
|
|
|
wcstring right_prompt;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The output of the last evaluation of the prompt command.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring left_prompt_buff;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The output of the last evaluation of the right prompt command.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring right_prompt_buff;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Completion support.
|
2014-01-27 16:56:13 +08:00
|
|
|
wcstring cycle_command_line;
|
|
|
|
size_t cycle_cursor_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Color is the syntax highlighting for buff. The format is that color[i] is the
|
|
|
|
/// classification (according to the enum in highlight.h) of buff[i].
|
2014-01-15 17:01:25 +08:00
|
|
|
std::vector<highlight_spec_t> colors;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// An array defining the block level at each character.
|
2012-11-19 08:30:30 +08:00
|
|
|
std::vector<int> indents;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Function for tab completion.
|
2012-02-25 04:13:35 +08:00
|
|
|
complete_function_t complete_func;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Function for syntax highlighting.
|
2012-11-19 08:30:30 +08:00
|
|
|
highlight_function_t highlight_function;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Function for testing if the string can be returned.
|
2015-05-03 08:49:38 +08:00
|
|
|
parser_test_error_bits_t (*test_func)(const wchar_t *);
|
2016-05-04 01:36:22 +08:00
|
|
|
/// When this is true, the reader will exit.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool end_loop;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// If this is true, exit reader even if there are running jobs. This happens if we press e.g.
|
|
|
|
/// ^D twice.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool prev_end_loop;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The current contents of the top item in the kill ring.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring kill_item;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Pointer to previous reader_data.
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_data_t *next;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// This variable keeps state on if we are in search mode, and if yes, what mode.
|
2012-11-19 08:30:30 +08:00
|
|
|
int search_mode;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Keep track of whether any internal code has done something which is known to require a
|
|
|
|
/// repaint.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool repaint_needed;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Whether a screen reset is needed after a repaint.
|
2012-03-25 18:00:38 +08:00
|
|
|
bool screen_reset_needed;
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Whether the reader should exit on ^C.
|
2013-01-22 18:25:17 +08:00
|
|
|
bool exit_on_interrupt;
|
2013-01-21 05:23:27 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
bool is_navigating_pager_contents() const { return this->pager.is_navigating_contents(); }
|
|
|
|
|
|
|
|
/// The line that is currently being edited. Typically the command line, but may be the search
|
|
|
|
/// field.
|
|
|
|
editable_line_t *active_edit_line() {
|
|
|
|
if (this->is_navigating_pager_contents() && this->pager.is_search_field_shown()) {
|
|
|
|
return &this->pager.search_field_line;
|
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return &this->command_line;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
|
|
|
|
/// Do what we need to do whenever our command line changes.
|
|
|
|
void command_line_changed(const editable_line_t *el);
|
|
|
|
|
|
|
|
/// Do what we need to do whenever our pager selection.
|
|
|
|
void pager_selection_changed();
|
|
|
|
|
|
|
|
/// Expand abbreviations at the current cursor position, minus backtrack_amt.
|
|
|
|
bool expand_abbreviation_as_necessary(size_t cursor_backtrack);
|
|
|
|
|
|
|
|
/// Constructor
|
|
|
|
reader_data_t()
|
2017-04-10 02:48:21 +08:00
|
|
|
: allow_autosuggestion(false),
|
|
|
|
suppress_autosuggestion(false),
|
|
|
|
expand_abbreviations(false),
|
|
|
|
silent(false),
|
2016-05-04 01:36:22 +08:00
|
|
|
history(0),
|
|
|
|
token_history_pos(0),
|
|
|
|
search_pos(0),
|
2017-04-10 02:48:21 +08:00
|
|
|
sel_active(false),
|
2016-05-04 01:36:22 +08:00
|
|
|
sel_begin_pos(0),
|
|
|
|
sel_start_pos(0),
|
|
|
|
sel_stop_pos(0),
|
|
|
|
cycle_cursor_pos(0),
|
|
|
|
complete_func(0),
|
|
|
|
highlight_function(0),
|
|
|
|
test_func(0),
|
2017-04-10 02:48:21 +08:00
|
|
|
end_loop(false),
|
|
|
|
prev_end_loop(false),
|
2016-05-04 01:36:22 +08:00
|
|
|
next(0),
|
|
|
|
search_mode(0),
|
2017-04-10 02:48:21 +08:00
|
|
|
repaint_needed(false),
|
|
|
|
screen_reset_needed(false),
|
|
|
|
exit_on_interrupt(false) {}
|
2011-12-28 10:41:38 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Sets the command line contents, without clearing the pager.
|
2014-01-21 04:38:56 +08:00
|
|
|
static void reader_set_buffer_maintaining_pager(const wcstring &b, size_t pos);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Clears the pager.
|
2014-08-12 01:17:41 +08:00
|
|
|
static void clear_pager();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The current interactive reading context.
|
|
|
|
static reader_data_t *data = 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// This flag is set to true when fish is interactively reading from stdin. It changes how a ^C is
|
|
|
|
/// handled by the fish interrupt handler.
|
2017-01-22 05:33:46 +08:00
|
|
|
static volatile sig_atomic_t is_interactive_read;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Flag for ending non-interactive shell.
|
2005-09-20 21:26:39 +08:00
|
|
|
static int end_loop = 0;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// The stack containing names of files that are being parsed.
|
2012-07-25 13:31:31 +08:00
|
|
|
static std::stack<const wchar_t *, std::vector<const wchar_t *> > current_filename;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// This variable is set to true by the signal handler when ^C is pressed.
|
2017-01-22 05:33:46 +08:00
|
|
|
static volatile sig_atomic_t interrupted = 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Prototypes for a bunch of functions defined later on.
|
2013-02-03 06:50:22 +08:00
|
|
|
static bool is_backslashed(const wcstring &str, size_t pos);
|
|
|
|
static wchar_t unescaped_quote(const wcstring &str, size_t pos);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Mode on startup, which we restore on exit.
|
2013-09-23 09:11:53 +08:00
|
|
|
static struct termios terminal_mode_on_startup;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Mode we use to execute programs.
|
2016-11-03 12:54:57 +08:00
|
|
|
static struct termios tty_modes_for_external_cmds;
|
2013-09-23 09:11:53 +08:00
|
|
|
|
2014-03-27 09:49:09 +08:00
|
|
|
static void reader_super_highlight_me_plenty(int highlight_pos_adjust = 0, bool no_io = false);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Variable to keep track of forced exits - see \c reader_exit_forced();
|
2017-01-25 07:15:38 +08:00
|
|
|
static bool exit_forced;
|
2006-05-14 18:16:23 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Give up control of terminal.
|
|
|
|
static void term_donate() {
|
2012-11-19 08:30:30 +08:00
|
|
|
set_color(rgb_color_t::normal(), rgb_color_t::normal());
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
while (1) {
|
2016-12-15 11:21:36 +08:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &tty_modes_for_external_cmds) == -1) {
|
2017-01-11 13:52:10 +08:00
|
|
|
if (errno == EIO) redirect_tty_output();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (errno != EINTR) {
|
2012-11-19 08:30:30 +08:00
|
|
|
debug(1, _(L"Could not set terminal mode for new job"));
|
|
|
|
wperror(L"tcsetattr");
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Update the cursor position.
|
|
|
|
static void update_buff_pos(editable_line_t *el, size_t buff_pos) {
|
2014-03-30 05:19:45 +08:00
|
|
|
el->position = buff_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el == &data->command_line && data->sel_active) {
|
|
|
|
if (data->sel_begin_pos <= buff_pos) {
|
2014-04-01 01:01:39 +08:00
|
|
|
data->sel_start_pos = data->sel_begin_pos;
|
|
|
|
data->sel_stop_pos = buff_pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2014-04-01 01:01:39 +08:00
|
|
|
data->sel_start_pos = buff_pos;
|
|
|
|
data->sel_stop_pos = data->sel_begin_pos;
|
|
|
|
}
|
2014-01-15 22:07:22 +08:00
|
|
|
}
|
2014-01-15 18:39:19 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Grab control of terminal.
|
|
|
|
static void term_steal() {
|
|
|
|
while (1) {
|
2016-12-15 11:21:36 +08:00
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &shell_modes) == -1) {
|
2017-01-11 13:52:10 +08:00
|
|
|
if (errno == EIO) redirect_tty_output();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (errno != EINTR) {
|
2012-11-19 08:30:30 +08:00
|
|
|
debug(1, _(L"Could not set terminal mode for shell"));
|
2016-12-15 11:21:36 +08:00
|
|
|
perror("tcsetattr");
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-01-19 05:54:54 +08:00
|
|
|
invalidate_termsize();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 07:15:38 +08:00
|
|
|
bool reader_exit_forced() { return exit_forced; }
|
2006-05-14 18:16:23 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Given a command line and an autosuggestion, return the string that gets shown to the user.
|
|
|
|
wcstring combine_command_and_autosuggestion(const wcstring &cmdline,
|
|
|
|
const wcstring &autosuggestion) {
|
|
|
|
// We want to compute the full line, containing the command line and the autosuggestion They may
|
|
|
|
// disagree on whether characters are uppercase or lowercase Here we do something funny: if the
|
|
|
|
// last token of the command line contains any uppercase characters, we use its case. Otherwise
|
|
|
|
// we use the case of the autosuggestion. This is an idea from issue #335.
|
2013-01-06 07:21:42 +08:00
|
|
|
wcstring full_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (autosuggestion.size() <= cmdline.size() || cmdline.empty()) {
|
|
|
|
// No or useless autosuggestion, or no command line.
|
2013-01-06 07:21:42 +08:00
|
|
|
full_line = cmdline;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (string_prefixes_string(cmdline, autosuggestion)) {
|
|
|
|
// No case disagreements, or no extra characters in the autosuggestion.
|
2013-01-06 07:21:42 +08:00
|
|
|
full_line = autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// We have an autosuggestion which is not a prefix of the command line, i.e. a case
|
|
|
|
// disagreement. Decide whose case we want to use.
|
2013-01-06 07:21:42 +08:00
|
|
|
const wchar_t *begin = NULL, *cmd = cmdline.c_str();
|
|
|
|
parse_util_token_extent(cmd, cmdline.size() - 1, &begin, NULL, NULL, NULL);
|
|
|
|
bool last_token_contains_uppercase = false;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (begin) {
|
2013-01-06 07:21:42 +08:00
|
|
|
const wchar_t *end = begin + wcslen(begin);
|
|
|
|
last_token_contains_uppercase = (std::find_if(begin, end, iswupper) != end);
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!last_token_contains_uppercase) {
|
|
|
|
// Use the autosuggestion's case.
|
2013-01-06 07:21:42 +08:00
|
|
|
full_line = autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Use the command line case for its characters, then append the remaining characters in
|
|
|
|
// the autosuggestion. Note that we know that autosuggestion.size() > cmdline.size() due
|
|
|
|
// to the first test above.
|
2013-01-06 07:21:42 +08:00
|
|
|
full_line = cmdline;
|
2016-05-04 01:36:22 +08:00
|
|
|
full_line.append(autosuggestion, cmdline.size(),
|
|
|
|
autosuggestion.size() - cmdline.size());
|
2013-01-06 07:21:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return full_line;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Repaint the entire commandline. This means reset and clear the commandline, write the prompt,
|
|
|
|
/// perform syntax highlighting, write the commandline and move the cursor.
|
|
|
|
static void reader_repaint() {
|
2014-01-27 16:56:13 +08:00
|
|
|
editable_line_t *cmd_line = &data->command_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
// Update the indentation.
|
2014-01-27 16:56:13 +08:00
|
|
|
data->indents = parse_util_compute_indents(cmd_line->text);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-04-10 02:48:21 +08:00
|
|
|
wcstring full_line;
|
|
|
|
if (data->silent) {
|
|
|
|
full_line = wcstring(cmd_line->text.length(), obfuscation_read_char);
|
|
|
|
} else {
|
|
|
|
// Combine the command and autosuggestion into one string.
|
|
|
|
full_line = combine_command_and_autosuggestion(cmd_line->text, data->autosuggestion);
|
|
|
|
}
|
2013-01-08 18:39:22 +08:00
|
|
|
|
2012-08-05 06:11:43 +08:00
|
|
|
size_t len = full_line.size();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (len < 1) len = 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-01-15 17:01:25 +08:00
|
|
|
std::vector<highlight_spec_t> colors = data->colors;
|
|
|
|
colors.resize(len, highlight_spec_autosuggestion);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->sel_active) {
|
2014-03-30 05:19:45 +08:00
|
|
|
highlight_spec_t selection_color = highlight_make_background(highlight_spec_selection);
|
2016-12-31 05:52:41 +08:00
|
|
|
for (size_t i = data->sel_start_pos; i < std::min(len, data->sel_stop_pos); i++) {
|
2014-03-30 05:19:45 +08:00
|
|
|
colors[i] = selection_color;
|
|
|
|
}
|
2014-01-15 22:07:22 +08:00
|
|
|
}
|
|
|
|
|
2012-02-07 02:52:13 +08:00
|
|
|
std::vector<int> indents = data->indents;
|
|
|
|
indents.resize(len);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Re-render our completions page if necessary. We set the term size to 1 less than the true
|
|
|
|
// term height. This means we will always show the (bottom) line of the prompt.
|
2014-01-23 09:45:27 +08:00
|
|
|
data->pager.set_term_size(maxi(1, common_get_width()), maxi(1, common_get_height() - 1));
|
2014-01-20 15:52:35 +08:00
|
|
|
data->pager.update_rendering(&data->current_page_rendering);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-27 16:56:13 +08:00
|
|
|
bool focused_on_pager = data->active_edit_line() == &data->pager.search_field_line;
|
|
|
|
size_t cursor_position = focused_on_pager ? data->pager.cursor_position() : cmd_line->position;
|
2012-02-07 02:52:13 +08:00
|
|
|
|
2017-05-02 12:44:30 +08:00
|
|
|
s_write(&data->screen, data->left_prompt_buff, data->right_prompt_buff, full_line,
|
|
|
|
cmd_line->size(), &colors[0], &indents[0], cursor_position,
|
|
|
|
data->current_page_rendering, focused_on_pager);
|
2012-11-08 11:59:20 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
data->repaint_needed = false;
|
2007-10-05 22:59:19 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Internal helper function for handling killing parts of text.
|
|
|
|
static void reader_kill(editable_line_t *el, size_t begin_idx, size_t length, int mode, int newv) {
|
2014-01-27 06:47:15 +08:00
|
|
|
const wchar_t *begin = el->text.c_str() + begin_idx;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (newv) {
|
2012-03-04 11:37:55 +08:00
|
|
|
data->kill_item = wcstring(begin, length);
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_add(data->kill_item);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-03-04 11:37:55 +08:00
|
|
|
wcstring old = data->kill_item;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (mode == KILL_APPEND) {
|
2012-03-04 11:37:55 +08:00
|
|
|
data->kill_item.append(begin, length);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-03-04 11:37:55 +08:00
|
|
|
data->kill_item = wcstring(begin, length);
|
|
|
|
data->kill_item.append(old);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
kill_replace(old, data->kill_item);
|
|
|
|
}
|
2006-10-13 03:30:00 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position > begin_idx) {
|
|
|
|
// Move the buff position back by the number of characters we deleted, but don't go past
|
|
|
|
// buff_pos.
|
2014-01-27 06:47:15 +08:00
|
|
|
size_t backtrack = mini(el->position - begin_idx, length);
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position - backtrack);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
el->text.erase(begin_idx, length);
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(el);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
2006-10-12 21:27:32 +08:00
|
|
|
}
|
2006-05-14 18:16:23 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// This is called from a signal handler!
|
2016-10-10 05:38:26 +08:00
|
|
|
void reader_handle_sigint() {
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!is_interactive_read) {
|
2012-06-05 05:20:01 +08:00
|
|
|
parser_t::skip_all_blocks();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
interrupted = 1;
|
2005-10-06 06:37:08 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
const wchar_t *reader_current_filename() {
|
2012-02-03 07:05:08 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
return current_filename.empty() ? NULL : current_filename.top();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_push_current_filename(const wchar_t *fn) {
|
2012-02-03 07:05:08 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
current_filename.push(intern(fn));
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_pop_current_filename() {
|
2012-02-03 07:05:08 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 08:30:30 +08:00
|
|
|
current_filename.pop();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Make sure buffers are large enough to hold the current string length.
|
|
|
|
void reader_data_t::command_line_changed(const editable_line_t *el) {
|
2012-02-29 04:40:20 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el == &this->command_line) {
|
2014-01-27 16:56:13 +08:00
|
|
|
size_t len = this->command_line.size();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// When we grow colors, propagate the last color (if any), under the assumption that usually
|
|
|
|
// it will be correct. If it is, it avoids a repaint.
|
2014-04-01 01:01:39 +08:00
|
|
|
highlight_spec_t last_color = colors.empty() ? highlight_spec_t() : colors.back();
|
|
|
|
colors.resize(len, last_color);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-04-01 01:01:39 +08:00
|
|
|
indents.resize(len);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Update the gen count.
|
2014-04-01 01:01:39 +08:00
|
|
|
s_generation_count++;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (el == &this->pager.search_field_line) {
|
2014-01-27 16:56:13 +08:00
|
|
|
this->pager.refilter_completions();
|
2014-02-17 11:56:13 +08:00
|
|
|
this->pager_selection_changed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_data_t::pager_selection_changed() {
|
2014-02-17 11:56:13 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
|
|
|
|
|
|
|
const completion_t *completion = this->pager.selected_completion(this->current_page_rendering);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Update the cursor and command line.
|
2014-02-17 11:56:13 +08:00
|
|
|
size_t cursor_pos = this->cycle_cursor_pos;
|
|
|
|
wcstring new_cmd_line;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (completion == NULL) {
|
2014-02-17 11:56:13 +08:00
|
|
|
new_cmd_line = this->cycle_command_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
new_cmd_line =
|
|
|
|
completion_apply_to_command_line(completion->completion, completion->flags,
|
|
|
|
this->cycle_command_line, &cursor_pos, false);
|
2014-02-17 11:56:13 +08:00
|
|
|
}
|
|
|
|
reader_set_buffer_maintaining_pager(new_cmd_line, cursor_pos);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Trigger repaint (see issue #765).
|
2014-02-17 11:56:13 +08:00
|
|
|
reader_repaint_needed();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Expand abbreviations at the given cursor position. Does NOT inspect 'data'.
|
|
|
|
bool reader_expand_abbreviation_in_command(const wcstring &cmdline, size_t cursor_pos,
|
|
|
|
wcstring *output) {
|
|
|
|
// See if we are at "command position". Get the surrounding command substitution, and get the
|
|
|
|
// extent of the first token.
|
|
|
|
const wchar_t *const buff = cmdline.c_str();
|
2013-07-17 15:38:04 +08:00
|
|
|
const wchar_t *cmdsub_begin = NULL, *cmdsub_end = NULL;
|
|
|
|
parse_util_cmdsubst_extent(buff, cursor_pos, &cmdsub_begin, &cmdsub_end);
|
|
|
|
assert(cmdsub_begin != NULL && cmdsub_begin >= buff);
|
|
|
|
assert(cmdsub_end != NULL && cmdsub_end >= cmdsub_begin);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Determine the offset of this command substitution.
|
2013-07-17 15:38:04 +08:00
|
|
|
const size_t subcmd_offset = cmdsub_begin - buff;
|
|
|
|
|
|
|
|
const wcstring subcmd = wcstring(cmdsub_begin, cmdsub_end - cmdsub_begin);
|
2013-10-09 17:03:50 +08:00
|
|
|
const size_t subcmd_cursor_pos = cursor_pos - subcmd_offset;
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Parse this subcmd.
|
2013-10-09 17:03:50 +08:00
|
|
|
parse_node_tree_t parse_tree;
|
2016-05-04 01:36:22 +08:00
|
|
|
parse_tree_from_string(subcmd,
|
|
|
|
parse_flag_continue_after_error | parse_flag_accept_incomplete_tokens,
|
|
|
|
&parse_tree, NULL);
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Look for plain statements where the cursor is at the end of the command.
|
2013-10-09 17:03:50 +08:00
|
|
|
const parse_node_t *matching_cmd_node = NULL;
|
|
|
|
const size_t len = parse_tree.size();
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = 0; i < len; i++) {
|
2013-10-09 17:03:50 +08:00
|
|
|
const parse_node_t &node = parse_tree.at(i);
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Only interested in plain statements with source.
|
|
|
|
if (node.type != symbol_plain_statement || !node.has_source()) continue;
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Skip decorated statements.
|
2013-10-09 17:03:50 +08:00
|
|
|
if (parse_tree.decoration_for_plain_statement(node) != parse_statement_decoration_none)
|
|
|
|
continue;
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Get the command node. Skip it if we can't or it has no source.
|
2013-10-09 17:03:50 +08:00
|
|
|
const parse_node_t *cmd_node = parse_tree.get_child(node, 0, parse_token_type_string);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (cmd_node == NULL || !cmd_node->has_source()) continue;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Now see if its source range contains our cursor, including at the end.
|
|
|
|
if (subcmd_cursor_pos >= cmd_node->source_start &&
|
|
|
|
subcmd_cursor_pos <= cmd_node->source_start + cmd_node->source_length) {
|
|
|
|
// Success!
|
2013-10-09 17:03:50 +08:00
|
|
|
matching_cmd_node = cmd_node;
|
2013-07-17 15:38:04 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Now if we found a command node, expand it.
|
2013-07-17 15:38:04 +08:00
|
|
|
bool result = false;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (matching_cmd_node != NULL) {
|
2013-10-09 17:03:50 +08:00
|
|
|
assert(matching_cmd_node->type == parse_token_type_string);
|
|
|
|
const wcstring token = matching_cmd_node->get_source(subcmd);
|
2013-07-17 15:38:04 +08:00
|
|
|
wcstring abbreviation;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (expand_abbreviation(token, &abbreviation)) {
|
|
|
|
// There was an abbreviation! Replace the token in the full command. Maintain the
|
|
|
|
// relative position of the cursor.
|
|
|
|
if (output != NULL) {
|
2013-07-17 15:38:04 +08:00
|
|
|
output->assign(cmdline);
|
2016-05-04 01:36:22 +08:00
|
|
|
output->replace(subcmd_offset + matching_cmd_node->source_start,
|
|
|
|
matching_cmd_node->source_length, abbreviation);
|
2013-07-17 15:38:04 +08:00
|
|
|
}
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Expand abbreviations at the current cursor position, minus the given cursor backtrack. This may
|
|
|
|
/// change the command line but does NOT repaint it. This is to allow the caller to coalesce
|
|
|
|
/// repaints.
|
|
|
|
bool reader_data_t::expand_abbreviation_as_necessary(size_t cursor_backtrack) {
|
2013-07-17 15:38:04 +08:00
|
|
|
bool result = false;
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (this->expand_abbreviations && el == &data->command_line) {
|
|
|
|
// Try expanding abbreviations.
|
2013-07-17 15:38:04 +08:00
|
|
|
wcstring new_cmdline;
|
2014-01-27 06:47:15 +08:00
|
|
|
size_t cursor_pos = el->position - mini(el->position, cursor_backtrack);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (reader_expand_abbreviation_in_command(el->text, cursor_pos, &new_cmdline)) {
|
|
|
|
// We expanded an abbreviation! The cursor moves by the difference in the command line
|
|
|
|
// lengths.
|
2014-01-27 06:47:15 +08:00
|
|
|
size_t new_buff_pos = el->position + new_cmdline.size() - el->text.size();
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2017-01-27 08:14:50 +08:00
|
|
|
el->text = std::move(new_cmdline);
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, new_buff_pos);
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(el);
|
2013-07-17 15:38:04 +08:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2007-02-19 07:25:20 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_reset_interrupted() { interrupted = 0; }
|
2013-01-21 05:23:27 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
int reader_interrupted() {
|
2013-01-22 18:00:02 +08:00
|
|
|
int res = interrupted;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (res) {
|
|
|
|
interrupted = 0;
|
2013-01-21 05:23:27 +08:00
|
|
|
}
|
2013-01-22 18:00:02 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
int reader_reading_interrupted() {
|
2013-01-22 18:00:02 +08:00
|
|
|
int res = reader_interrupted();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (res && data && data->exit_on_interrupt) {
|
2013-01-21 05:23:27 +08:00
|
|
|
reader_exit(1, 0);
|
2013-01-22 18:19:01 +08:00
|
|
|
parser_t::skip_all_blocks();
|
2016-05-04 01:36:22 +08:00
|
|
|
// We handled the interrupt ourselves, our caller doesn't need to handle it.
|
2013-01-22 18:57:47 +08:00
|
|
|
return 0;
|
2013-01-21 05:23:27 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
bool reader_thread_job_is_stale() {
|
2013-01-23 07:19:29 +08:00
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
2017-03-27 04:44:27 +08:00
|
|
|
void *current_count = (void *)(uintptr_t)read_generation_count();
|
|
|
|
return current_count != pthread_getspecific(generation_count_key);
|
2013-01-23 07:19:29 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 08:03:19 +08:00
|
|
|
void reader_write_title(const wcstring &cmd, bool reset_cursor_position) {
|
|
|
|
if (!term_supports_setting_title()) return;
|
|
|
|
|
2014-08-03 12:01:40 +08:00
|
|
|
wcstring fish_title_command = DEFAULT_TITLE;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (function_exists(L"fish_title")) {
|
2014-08-03 12:01:40 +08:00
|
|
|
fish_title_command = L"fish_title";
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!cmd.empty()) {
|
2014-08-03 12:01:40 +08:00
|
|
|
fish_title_command.append(L" ");
|
|
|
|
fish_title_command.append(parse_util_escape_string_with_quote(cmd, L'\0'));
|
|
|
|
}
|
2014-07-14 23:34:15 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2011-12-29 04:36:47 +08:00
|
|
|
wcstring_list_t lst;
|
2012-11-19 08:30:30 +08:00
|
|
|
proc_push_interactive(0);
|
2016-10-23 02:21:13 +08:00
|
|
|
if (exec_subshell(fish_title_command, lst, false /* ignore exit status */) != -1 &&
|
|
|
|
!lst.empty()) {
|
2016-12-30 04:24:56 +08:00
|
|
|
fputws(L"\e]0;", stdout);
|
2016-10-23 02:21:13 +08:00
|
|
|
for (size_t i = 0; i < lst.size(); i++) {
|
2016-12-21 08:35:43 +08:00
|
|
|
fputws(lst.at(i).c_str(), stdout);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-08-06 06:08:39 +08:00
|
|
|
ignore_result(write(STDOUT_FILENO, "\a", 1));
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2017-01-20 12:58:12 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
proc_pop_interactive();
|
|
|
|
set_color(rgb_color_t::reset(), rgb_color_t::reset());
|
2016-05-04 01:36:22 +08:00
|
|
|
if (reset_cursor_position && !lst.empty()) {
|
|
|
|
// Put the cursor back at the beginning of the line (issue #2453).
|
2017-08-12 22:54:26 +08:00
|
|
|
ignore_result(write(STDOUT_FILENO, "\r", 1));
|
2016-04-30 03:14:10 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Reexecute the prompt command. The output is inserted into data->prompt_buff.
|
|
|
|
static void exec_prompt() {
|
|
|
|
// Clear existing prompts.
|
2012-11-05 15:21:37 +08:00
|
|
|
data->left_prompt_buff.clear();
|
2012-11-08 11:59:20 +08:00
|
|
|
data->right_prompt_buff.clear();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Do not allow the exit status of the prompts to leak through.
|
2013-02-01 07:57:08 +08:00
|
|
|
const bool apply_exit_status = false;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If we have any prompts, they must be run non-interactively.
|
|
|
|
if (data->left_prompt.size() || data->right_prompt.size()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
proc_push_interactive(0);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Prepend any mode indicator to the left prompt (issue #1988).
|
|
|
|
if (function_exists(MODE_PROMPT_FUNCTION_NAME)) {
|
2015-06-05 04:31:48 +08:00
|
|
|
wcstring_list_t mode_indicator_list;
|
|
|
|
exec_subshell(MODE_PROMPT_FUNCTION_NAME, mode_indicator_list, apply_exit_status);
|
2016-05-04 01:36:22 +08:00
|
|
|
// We do not support multiple lines in the mode indicator, so just concatenate all of
|
|
|
|
// them.
|
|
|
|
for (size_t i = 0; i < mode_indicator_list.size(); i++) {
|
2015-06-05 04:31:48 +08:00
|
|
|
data->left_prompt_buff += mode_indicator_list.at(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!data->left_prompt.empty()) {
|
2012-11-08 11:59:20 +08:00
|
|
|
wcstring_list_t prompt_list;
|
2016-05-04 01:36:22 +08:00
|
|
|
// Ignore return status.
|
2013-02-06 08:11:46 +08:00
|
|
|
exec_subshell(data->left_prompt, prompt_list, apply_exit_status);
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = 0; i < prompt_list.size(); i++) {
|
2013-01-28 05:44:32 +08:00
|
|
|
if (i > 0) data->left_prompt_buff += L'\n';
|
|
|
|
data->left_prompt_buff += prompt_list.at(i);
|
2012-11-08 11:59:20 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!data->right_prompt.empty()) {
|
2012-11-08 11:59:20 +08:00
|
|
|
wcstring_list_t prompt_list;
|
2016-05-04 01:36:22 +08:00
|
|
|
// Status is ignored.
|
2013-02-06 08:11:46 +08:00
|
|
|
exec_subshell(data->right_prompt, prompt_list, apply_exit_status);
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = 0; i < prompt_list.size(); i++) {
|
|
|
|
// Right prompt does not support multiple lines, so just concatenate all of them.
|
2013-01-28 05:44:32 +08:00
|
|
|
data->right_prompt_buff += prompt_list.at(i);
|
2012-11-08 11:59:20 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-08 11:59:20 +08:00
|
|
|
proc_pop_interactive();
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Write the screen title. Do not reset the cursor position: exec_prompt is called when there
|
|
|
|
// may still be output on the line from the previous command (#2499) and we need our PROMPT_SP
|
|
|
|
// hack to work.
|
2016-04-30 03:14:10 +08:00
|
|
|
reader_write_title(L"", false);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_init() {
|
2017-02-15 13:09:15 +08:00
|
|
|
DIE_ON_FAILURE(pthread_key_create(&generation_count_key, NULL));
|
2006-08-24 18:43:54 +08:00
|
|
|
|
2017-01-07 13:14:29 +08:00
|
|
|
// Ensure this var is present even before an interactive command is run so that if it is used
|
|
|
|
// in a function like `fish_prompt` or `fish_right_prompt` it is defined at the time the first
|
2017-08-11 08:46:08 +08:00
|
|
|
// prompt is written.
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(ENV_CMD_DURATION, ENV_UNEXPORT, L"0");
|
2017-01-07 13:14:29 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Save the initial terminal mode.
|
2013-09-23 09:11:53 +08:00
|
|
|
tcgetattr(STDIN_FILENO, &terminal_mode_on_startup);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Set the mode used for program execution, initialized to the current mode.
|
2016-11-03 12:54:57 +08:00
|
|
|
memcpy(&tty_modes_for_external_cmds, &terminal_mode_on_startup,
|
|
|
|
sizeof tty_modes_for_external_cmds);
|
|
|
|
tty_modes_for_external_cmds.c_iflag &= ~IXON; // disable flow control
|
|
|
|
tty_modes_for_external_cmds.c_iflag &= ~IXOFF; // disable flow control
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Set the mode used for the terminal, initialized to the current mode.
|
2013-09-23 09:11:53 +08:00
|
|
|
memcpy(&shell_modes, &terminal_mode_on_startup, sizeof shell_modes);
|
2016-05-25 11:42:50 +08:00
|
|
|
|
|
|
|
shell_modes.c_iflag &= ~ICRNL; // disable mapping CR (\cM) to NL (\cJ)
|
|
|
|
shell_modes.c_iflag &= ~INLCR; // disable mapping NL (\cJ) to CR (\cM)
|
|
|
|
shell_modes.c_iflag &= ~IXON; // disable flow control
|
|
|
|
shell_modes.c_iflag &= ~IXOFF; // disable flow control
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
shell_modes.c_lflag &= ~ICANON; // turn off canonical mode
|
|
|
|
shell_modes.c_lflag &= ~ECHO; // turn off echo mode
|
2016-05-25 11:42:50 +08:00
|
|
|
shell_modes.c_lflag &= ~IEXTEN; // turn off handling of discard and lnext characters
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
shell_modes.c_cc[VMIN] = 1;
|
|
|
|
shell_modes.c_cc[VTIME] = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// We don't use term_steal because this can fail if fd 0 isn't associated with a tty and this
|
|
|
|
// function is run regardless of whether stdin is tied to a tty. This is harmless in that case.
|
|
|
|
// We do it unconditionally because disabling ICRNL mode (see above) needs to be done at the
|
|
|
|
// earliest possible moment. Doing it here means it will be done within approximately 1 ms of
|
|
|
|
// the start of the shell rather than 250 ms (or more) when reader_interactive_init is
|
|
|
|
// eventually called.
|
2015-12-26 06:47:30 +08:00
|
|
|
//
|
|
|
|
// TODO: Remove this condition when issue #2315 and #1041 are addressed.
|
2016-05-04 01:36:22 +08:00
|
|
|
if (is_interactive_session) {
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &shell_modes);
|
2015-12-26 06:47:30 +08:00
|
|
|
}
|
2017-06-19 06:38:52 +08:00
|
|
|
|
|
|
|
// We do this not because we actually need the window size but for its side-effect of correctly
|
|
|
|
// setting the COLUMNS and LINES env vars.
|
|
|
|
get_current_winsize();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_destroy() { pthread_key_delete(generation_count_key); }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-02-11 10:47:02 +08:00
|
|
|
/// Restore the term mode if we own the terminal. It's important we do this before
|
|
|
|
/// restore_foreground_process_group, otherwise we won't think we own the terminal.
|
2016-05-04 01:36:22 +08:00
|
|
|
void restore_term_mode() {
|
2017-02-11 10:47:02 +08:00
|
|
|
if (getpid() != tcgetpgrp(STDIN_FILENO)) return;
|
|
|
|
|
|
|
|
if (tcsetattr(STDIN_FILENO, TCSANOW, &terminal_mode_on_startup) == -1 && errno == EIO) {
|
|
|
|
redirect_tty_output();
|
2013-10-27 06:22:20 +08:00
|
|
|
}
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_exit(int do_exit, int forced) {
|
|
|
|
if (data) data->end_loop = do_exit;
|
|
|
|
end_loop = do_exit;
|
2017-01-25 07:15:38 +08:00
|
|
|
if (forced) exit_forced = true;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_repaint_needed() {
|
|
|
|
if (data) {
|
2012-11-19 08:30:30 +08:00
|
|
|
data->repaint_needed = true;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_repaint_if_needed() {
|
|
|
|
if (data == NULL) return;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2012-12-03 03:53:34 +08:00
|
|
|
bool needs_reset = data->screen_reset_needed;
|
|
|
|
bool needs_repaint = needs_reset || data->repaint_needed;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (needs_reset) {
|
2013-01-08 18:56:45 +08:00
|
|
|
exec_prompt();
|
2012-11-25 08:42:25 +08:00
|
|
|
s_reset(&data->screen, screen_reset_current_line_and_prompt);
|
2012-03-25 18:00:38 +08:00
|
|
|
data->screen_reset_needed = false;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (needs_repaint) {
|
|
|
|
reader_repaint(); // reader_repaint clears repaint_needed
|
2012-03-25 18:00:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_react_to_color_change() {
|
|
|
|
if (!data) return;
|
2013-05-05 17:33:17 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!data->repaint_needed || !data->screen_reset_needed) {
|
2012-04-01 08:24:11 +08:00
|
|
|
data->repaint_needed = true;
|
2012-03-25 18:00:38 +08:00
|
|
|
data->screen_reset_needed = true;
|
2017-01-22 09:15:45 +08:00
|
|
|
input_common_add_callback(reader_repaint_if_needed);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-03-25 18:00:38 +08:00
|
|
|
}
|
2007-09-21 22:05:49 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Indicates if the given command char ends paging.
|
|
|
|
static bool command_ends_paging(wchar_t c, bool focused_on_search_field) {
|
|
|
|
switch (c) {
|
2014-01-27 16:56:13 +08:00
|
|
|
case R_HISTORY_SEARCH_BACKWARD:
|
|
|
|
case R_HISTORY_SEARCH_FORWARD:
|
|
|
|
case R_HISTORY_TOKEN_SEARCH_BACKWARD:
|
|
|
|
case R_HISTORY_TOKEN_SEARCH_FORWARD:
|
|
|
|
case R_ACCEPT_AUTOSUGGESTION:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_CANCEL: {
|
|
|
|
// These commands always end paging.
|
2014-01-27 16:56:13 +08:00
|
|
|
return true;
|
2016-05-04 01:36:22 +08:00
|
|
|
}
|
2014-01-27 16:56:13 +08:00
|
|
|
case R_COMPLETE:
|
|
|
|
case R_COMPLETE_AND_SEARCH:
|
|
|
|
case R_BACKWARD_CHAR:
|
|
|
|
case R_FORWARD_CHAR:
|
|
|
|
case R_UP_LINE:
|
|
|
|
case R_DOWN_LINE:
|
|
|
|
case R_NULL:
|
|
|
|
case R_REPAINT:
|
|
|
|
case R_SUPPRESS_AUTOSUGGESTION:
|
2015-03-03 05:08:29 +08:00
|
|
|
case R_BEGINNING_OF_HISTORY:
|
2016-11-03 09:29:14 +08:00
|
|
|
case R_END_OF_HISTORY: {
|
|
|
|
// These commands never end paging.
|
2014-01-27 16:56:13 +08:00
|
|
|
return false;
|
2016-05-04 01:36:22 +08:00
|
|
|
}
|
|
|
|
case R_EXECUTE: {
|
|
|
|
// R_EXECUTE does end paging, but only executes if it was not paging. So it's handled
|
|
|
|
// specially.
|
2014-01-28 14:01:38 +08:00
|
|
|
return false;
|
2016-05-04 01:36:22 +08:00
|
|
|
}
|
2014-01-27 16:56:13 +08:00
|
|
|
case R_BEGINNING_OF_LINE:
|
|
|
|
case R_END_OF_LINE:
|
|
|
|
case R_FORWARD_WORD:
|
|
|
|
case R_BACKWARD_WORD:
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
case R_FORWARD_BIGWORD:
|
|
|
|
case R_BACKWARD_BIGWORD:
|
2014-01-27 16:56:13 +08:00
|
|
|
case R_DELETE_CHAR:
|
|
|
|
case R_BACKWARD_DELETE_CHAR:
|
|
|
|
case R_KILL_LINE:
|
|
|
|
case R_YANK:
|
|
|
|
case R_YANK_POP:
|
|
|
|
case R_BACKWARD_KILL_LINE:
|
|
|
|
case R_KILL_WHOLE_LINE:
|
|
|
|
case R_KILL_WORD:
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
case R_KILL_BIGWORD:
|
2014-01-27 16:56:13 +08:00
|
|
|
case R_BACKWARD_KILL_WORD:
|
|
|
|
case R_BACKWARD_KILL_PATH_COMPONENT:
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
case R_BACKWARD_KILL_BIGWORD:
|
2014-01-27 16:56:13 +08:00
|
|
|
case R_SELF_INSERT:
|
|
|
|
case R_TRANSPOSE_CHARS:
|
|
|
|
case R_TRANSPOSE_WORDS:
|
|
|
|
case R_UPCASE_WORD:
|
|
|
|
case R_DOWNCASE_WORD:
|
|
|
|
case R_CAPITALIZE_WORD:
|
|
|
|
case R_VI_ARG_DIGIT:
|
|
|
|
case R_VI_DELETE_TO:
|
|
|
|
case R_BEGINNING_OF_BUFFER:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_END_OF_BUFFER: {
|
|
|
|
// These commands operate on the search field if that's where the focus is.
|
|
|
|
return !focused_on_search_field;
|
|
|
|
}
|
2016-11-03 09:29:14 +08:00
|
|
|
default: { return false; }
|
2014-01-27 16:56:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Remove the previous character in the character buffer and on the screen using syntax
|
|
|
|
/// highlighting, etc.
|
|
|
|
static void remove_backward() {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position <= 0) return;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Fake composed character sequences by continuing to delete until we delete a character of
|
|
|
|
// width at least 1.
|
2012-07-16 01:45:18 +08:00
|
|
|
int width;
|
2016-05-04 01:36:22 +08:00
|
|
|
do {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position - 1);
|
2014-01-27 06:47:15 +08:00
|
|
|
width = fish_wcwidth(el->text.at(el->position));
|
|
|
|
el->text.erase(el->position, 1);
|
2016-05-04 01:36:22 +08:00
|
|
|
} while (width == 0 && el->position > 0);
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(el);
|
2012-02-07 11:31:06 +08:00
|
|
|
data->suppress_autosuggestion = true;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2015-07-20 15:45:44 +08:00
|
|
|
reader_repaint_needed();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Insert the characters of the string into the command line buffer and print them to the screen
|
|
|
|
/// using syntax highlighting, etc. Optionally also expand abbreviations, after space characters.
|
|
|
|
/// Returns true if the string changed.
|
|
|
|
static bool insert_string(editable_line_t *el, const wcstring &str,
|
|
|
|
bool allow_expand_abbreviations = false) {
|
2012-02-06 17:45:16 +08:00
|
|
|
size_t len = str.size();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (len == 0) return false;
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Start inserting. If we are expanding abbreviations, we have to do this after every space (see
|
|
|
|
// #1434), so look for spaces. We try to do this efficiently (rather than the simpler character
|
|
|
|
// at a time) to avoid expensive work in command_line_changed().
|
2014-07-26 03:23:42 +08:00
|
|
|
size_t cursor = 0;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (cursor < len) {
|
|
|
|
// Determine the position of the next expansion-triggering char (possibly none), and the end
|
|
|
|
// of the range we wish to insert.
|
2014-09-20 05:05:33 +08:00
|
|
|
const wchar_t *expansion_triggering_chars = L" ;|&^><";
|
2016-05-04 01:36:22 +08:00
|
|
|
size_t char_triggering_expansion_pos =
|
|
|
|
allow_expand_abbreviations ? str.find_first_of(expansion_triggering_chars, cursor)
|
|
|
|
: wcstring::npos;
|
2014-09-20 04:59:53 +08:00
|
|
|
bool has_expansion_triggering_char = (char_triggering_expansion_pos != wcstring::npos);
|
2016-05-04 01:36:22 +08:00
|
|
|
size_t range_end =
|
|
|
|
(has_expansion_triggering_char ? char_triggering_expansion_pos + 1 : len);
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Insert from the cursor up to but not including the range end.
|
2014-07-26 03:23:42 +08:00
|
|
|
assert(range_end > cursor);
|
|
|
|
el->insert_string(str, cursor, range_end - cursor);
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2014-07-26 03:23:42 +08:00
|
|
|
update_buff_pos(el, el->position);
|
|
|
|
data->command_line_changed(el);
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If we got an expansion trigger, then the last character we inserted was it (i.e. was a
|
|
|
|
// space). Expand abbreviations.
|
|
|
|
if (has_expansion_triggering_char && allow_expand_abbreviations) {
|
2014-07-26 03:23:42 +08:00
|
|
|
assert(range_end > 0);
|
2014-09-20 04:59:53 +08:00
|
|
|
assert(wcschr(expansion_triggering_chars, str.at(range_end - 1)));
|
2014-07-26 03:23:42 +08:00
|
|
|
data->expand_abbreviation_as_necessary(1);
|
|
|
|
}
|
|
|
|
cursor = range_end;
|
|
|
|
}
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el == &data->command_line) {
|
2014-01-27 06:47:15 +08:00
|
|
|
data->suppress_autosuggestion = false;
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Syntax highlight. Note we must have that buff_pos > 0 because we just added something
|
|
|
|
// nonzero to its length.
|
2014-01-27 06:47:15 +08:00
|
|
|
assert(el->position > 0);
|
|
|
|
reader_super_highlight_me_plenty(-1);
|
|
|
|
}
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2013-08-25 02:16:45 +08:00
|
|
|
return true;
|
|
|
|
}
|
2006-11-02 21:50:19 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Insert the character into the command line buffer and print it to the screen using syntax
|
|
|
|
/// highlighting, etc.
|
|
|
|
static bool insert_char(editable_line_t *el, wchar_t c, bool allow_expand_abbreviations = false) {
|
2014-07-26 03:23:42 +08:00
|
|
|
return insert_string(el, wcstring(1, c), allow_expand_abbreviations);
|
2006-11-02 21:50:19 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Insert the string in the given command line at the given cursor position. The function checks if
|
|
|
|
/// the string is quoted or not and correctly escapes the string.
|
|
|
|
///
|
|
|
|
/// \param val the string to insert
|
|
|
|
/// \param flags A union of all flags describing the completion to insert. See the completion_t
|
|
|
|
/// struct for more information on possible values.
|
|
|
|
/// \param command_line The command line into which we will insert
|
|
|
|
/// \param inout_cursor_pos On input, the location of the cursor within the command line. On output,
|
|
|
|
/// the new desired position.
|
|
|
|
/// \param append_only Whether we can only append to the command line, or also modify previous
|
|
|
|
/// characters. This is used to determine whether we go inside a trailing quote.
|
|
|
|
///
|
|
|
|
/// \return The completed string
|
|
|
|
wcstring completion_apply_to_command_line(const wcstring &val_str, complete_flags_t flags,
|
|
|
|
const wcstring &command_line, size_t *inout_cursor_pos,
|
|
|
|
bool append_only) {
|
2012-05-06 04:34:09 +08:00
|
|
|
const wchar_t *val = val_str.c_str();
|
2016-10-21 12:14:40 +08:00
|
|
|
bool add_space = !static_cast<bool>(flags & COMPLETE_NO_SPACE);
|
|
|
|
bool do_replace = static_cast<bool>(flags & COMPLETE_REPLACES_TOKEN);
|
|
|
|
bool do_escape = !static_cast<bool>(flags & COMPLETE_DONT_ESCAPE);
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2012-05-06 04:34:09 +08:00
|
|
|
const size_t cursor_pos = *inout_cursor_pos;
|
2013-02-03 06:50:22 +08:00
|
|
|
bool back_into_trailing_quote = false;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (do_replace) {
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t move_cursor;
|
|
|
|
const wchar_t *begin, *end;
|
|
|
|
|
2012-05-06 04:34:09 +08:00
|
|
|
const wchar_t *buff = command_line.c_str();
|
2012-11-19 08:30:30 +08:00
|
|
|
parse_util_token_extent(buff, cursor_pos, &begin, 0, 0, 0);
|
|
|
|
end = buff + cursor_pos;
|
|
|
|
|
|
|
|
wcstring sb(buff, begin - buff);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (do_escape) {
|
|
|
|
// Respect COMPLETE_DONT_ESCAPE_TILDES.
|
2016-10-21 12:14:40 +08:00
|
|
|
bool no_tilde = static_cast<bool>(flags & COMPLETE_DONT_ESCAPE_TILDES);
|
2017-03-14 11:19:08 +08:00
|
|
|
wcstring escaped = escape_string(
|
|
|
|
val, ESCAPE_ALL | ESCAPE_NO_QUOTED | (no_tilde ? ESCAPE_NO_TILDE : 0));
|
2012-11-19 08:30:30 +08:00
|
|
|
sb.append(escaped);
|
2014-09-26 09:20:03 +08:00
|
|
|
move_cursor = escaped.size();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
sb.append(val);
|
|
|
|
move_cursor = wcslen(val);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (add_space) {
|
2012-11-19 08:30:30 +08:00
|
|
|
sb.append(L" ");
|
|
|
|
move_cursor += 1;
|
|
|
|
}
|
|
|
|
sb.append(end);
|
|
|
|
|
2012-05-06 04:34:09 +08:00
|
|
|
size_t new_cursor_pos = (begin - buff) + move_cursor;
|
|
|
|
*inout_cursor_pos = new_cursor_pos;
|
|
|
|
return sb;
|
2016-05-05 06:19:47 +08:00
|
|
|
}
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
wchar_t quote = L'\0';
|
|
|
|
wcstring replaced;
|
|
|
|
if (do_escape) {
|
|
|
|
// Note that we ignore COMPLETE_DONT_ESCAPE_TILDES here. We get away with this because
|
|
|
|
// unexpand_tildes only operates on completions that have COMPLETE_REPLACES_TOKEN set,
|
|
|
|
// but we ought to respect them.
|
|
|
|
parse_util_get_parameter_info(command_line, cursor_pos, "e, NULL, NULL);
|
|
|
|
|
|
|
|
// If the token is reported as unquoted, but ends with a (unescaped) quote, and we can
|
|
|
|
// modify the command line, then delete the trailing quote so that we can insert within
|
|
|
|
// the quotes instead of after them. See issue #552.
|
|
|
|
if (quote == L'\0' && !append_only && cursor_pos > 0) {
|
|
|
|
// The entire token is reported as unquoted...see if the last character is an
|
|
|
|
// unescaped quote.
|
|
|
|
wchar_t trailing_quote = unescaped_quote(command_line, cursor_pos - 1);
|
|
|
|
if (trailing_quote != L'\0') {
|
|
|
|
quote = trailing_quote;
|
|
|
|
back_into_trailing_quote = true;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
replaced = parse_util_escape_string_with_quote(val_str, quote);
|
|
|
|
} else {
|
|
|
|
replaced = val;
|
|
|
|
}
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-05 06:19:47 +08:00
|
|
|
size_t insertion_point = cursor_pos;
|
|
|
|
if (back_into_trailing_quote) {
|
|
|
|
// Move the character back one so we enter the terminal quote.
|
|
|
|
assert(insertion_point > 0);
|
|
|
|
insertion_point--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the insertion and compute the new location.
|
|
|
|
wcstring result = command_line;
|
|
|
|
result.insert(insertion_point, replaced);
|
2016-05-25 11:42:50 +08:00
|
|
|
size_t new_cursor_pos = insertion_point + replaced.size() + (back_into_trailing_quote ? 1 : 0);
|
2016-05-05 06:19:47 +08:00
|
|
|
if (add_space) {
|
|
|
|
if (quote != L'\0' && unescaped_quote(command_line, insertion_point) != quote) {
|
|
|
|
// This is a quoted parameter, first print a quote.
|
|
|
|
result.insert(new_cursor_pos++, wcstring("e, 1));
|
2012-05-06 04:34:09 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
result.insert(new_cursor_pos++, L" ");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
*inout_cursor_pos = new_cursor_pos;
|
|
|
|
return result;
|
2012-05-06 04:34:09 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Insert the string at the current cursor position. The function checks if the string is quoted or
|
|
|
|
/// not and correctly escapes the string.
|
|
|
|
///
|
|
|
|
/// \param val the string to insert
|
|
|
|
/// \param flags A union of all flags describing the completion to insert. See the completion_t
|
|
|
|
/// struct for more information on possible values.
|
|
|
|
static void completion_insert(const wchar_t *val, complete_flags_t flags) {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
|
|
|
size_t cursor = el->position;
|
2016-05-04 01:36:22 +08:00
|
|
|
wcstring new_command_line = completion_apply_to_command_line(val, flags, el->text, &cursor,
|
|
|
|
false /* not append only */);
|
2014-01-21 04:38:56 +08:00
|
|
|
reader_set_buffer_maintaining_pager(new_command_line, cursor);
|
2012-05-06 04:34:09 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 01:27:11 +08:00
|
|
|
struct autosuggestion_result_t {
|
|
|
|
wcstring suggestion;
|
2012-02-20 18:13:31 +08:00
|
|
|
wcstring search_string;
|
2017-01-25 01:27:11 +08:00
|
|
|
};
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-25 01:27:11 +08:00
|
|
|
// Returns a function that can be invoked (potentially
|
|
|
|
// on a background thread) to determine the autosuggestion
|
2017-01-27 12:00:43 +08:00
|
|
|
static std::function<autosuggestion_result_t(void)> get_autosuggestion_performer(
|
|
|
|
const wcstring &search_string, size_t cursor_pos, history_t *history) {
|
2017-03-27 04:44:27 +08:00
|
|
|
const unsigned int generation_count = read_generation_count();
|
2017-01-25 01:27:11 +08:00
|
|
|
const wcstring working_directory(env_get_pwd_slash());
|
|
|
|
env_vars_snapshot_t vars(env_vars_snapshot_t::highlighting_keys);
|
|
|
|
// TODO: suspicious use of 'history' here
|
|
|
|
// This is safe because histories are immortal, but perhaps
|
|
|
|
// this should use shared_ptr
|
|
|
|
return [=]() -> autosuggestion_result_t {
|
2017-01-25 09:52:28 +08:00
|
|
|
ASSERT_IS_BACKGROUND_THREAD();
|
|
|
|
|
2017-01-25 01:27:11 +08:00
|
|
|
const autosuggestion_result_t nothing = {};
|
2016-05-04 01:36:22 +08:00
|
|
|
// If the main thread has moved on, skip all the work.
|
2017-03-27 04:44:27 +08:00
|
|
|
if (generation_count != read_generation_count()) {
|
2017-01-25 01:27:11 +08:00
|
|
|
return nothing;
|
2012-02-29 04:40:20 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-02-15 13:09:15 +08:00
|
|
|
DIE_ON_FAILURE(
|
2017-01-27 12:00:43 +08:00
|
|
|
pthread_setspecific(generation_count_key, (void *)(uintptr_t)generation_count));
|
2013-01-23 07:19:29 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Let's make sure we aren't using the empty string.
|
|
|
|
if (search_string.empty()) {
|
2017-01-25 01:27:11 +08:00
|
|
|
return nothing;
|
2012-05-06 04:34:09 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-25 01:27:11 +08:00
|
|
|
history_search_t searcher(*history, search_string, HISTORY_SEARCH_TYPE_PREFIX);
|
2016-05-04 01:36:22 +08:00
|
|
|
while (!reader_thread_job_is_stale() && searcher.go_backwards()) {
|
2012-02-19 10:54:36 +08:00
|
|
|
history_item_t item = searcher.current_item();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Skip items with newlines because they make terrible autosuggestions.
|
|
|
|
if (item.str().find('\n') != wcstring::npos) continue;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-25 09:52:28 +08:00
|
|
|
if (autosuggest_validate_from_history(item, working_directory, vars)) {
|
2016-05-04 01:36:22 +08:00
|
|
|
// The command autosuggestion was handled specially, so we're done.
|
2017-01-25 01:27:11 +08:00
|
|
|
return {searcher.current_string(), search_string};
|
2012-02-20 18:13:31 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Maybe cancel here.
|
2017-01-25 01:27:11 +08:00
|
|
|
if (reader_thread_job_is_stale()) return nothing;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Here we do something a little funny. If the line ends with a space, and the cursor is not
|
|
|
|
// at the end, don't use completion autosuggestions. It ends up being pretty weird seeing
|
|
|
|
// stuff get spammed on the right while you go back to edit a line
|
2012-07-07 05:34:53 +08:00
|
|
|
const wchar_t last_char = search_string.at(search_string.size() - 1);
|
2017-01-25 01:27:11 +08:00
|
|
|
const bool cursor_at_end = (cursor_pos == search_string.size());
|
|
|
|
if (!cursor_at_end && iswspace(last_char)) return nothing;
|
2012-07-07 05:34:53 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// On the other hand, if the line ends with a quote, don't go dumping stuff after the quote.
|
2017-01-25 01:27:11 +08:00
|
|
|
if (wcschr(L"'\"", last_char) && cursor_at_end) return nothing;
|
2012-05-06 04:34:09 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Try normal completions.
|
2012-02-26 10:54:49 +08:00
|
|
|
std::vector<completion_t> completions;
|
2017-08-19 07:52:39 +08:00
|
|
|
complete(search_string, &completions, COMPLETION_REQUEST_AUTOSUGGESTION);
|
2016-02-07 07:06:33 +08:00
|
|
|
completions_sort_and_prioritize(&completions);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!completions.empty()) {
|
2012-03-02 09:31:45 +08:00
|
|
|
const completion_t &comp = completions.at(0);
|
2017-01-25 01:27:11 +08:00
|
|
|
size_t cursor = cursor_pos;
|
2017-01-27 12:00:43 +08:00
|
|
|
wcstring suggestion = completion_apply_to_command_line(
|
|
|
|
comp.completion, comp.flags, search_string, &cursor, true /* append only */);
|
2017-01-25 01:27:11 +08:00
|
|
|
return {std::move(suggestion), search_string};
|
2012-02-19 10:54:36 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-25 01:27:11 +08:00
|
|
|
return nothing;
|
|
|
|
};
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
static bool can_autosuggest(void) {
|
|
|
|
// We autosuggest if suppress_autosuggestion is not set, if we're not doing a history search,
|
|
|
|
// and our command line contains a non-whitespace character.
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = data->active_edit_line();
|
2012-03-06 01:34:31 +08:00
|
|
|
const wchar_t *whitespace = L" \t\r\n\v";
|
2016-05-04 01:36:22 +08:00
|
|
|
return !data->suppress_autosuggestion && data->history_search.is_at_end() &&
|
|
|
|
el == &data->command_line && el->text.find_first_not_of(whitespace) != wcstring::npos;
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 01:27:11 +08:00
|
|
|
// Called after an autosuggestion has been computed on a background thread
|
|
|
|
static void autosuggest_completed(autosuggestion_result_t result) {
|
2017-01-27 12:00:43 +08:00
|
|
|
if (!result.suggestion.empty() && can_autosuggest() &&
|
2017-01-25 01:27:11 +08:00
|
|
|
result.search_string == data->command_line.text &&
|
|
|
|
string_prefixes_string_case_insensitive(result.search_string, result.suggestion)) {
|
2016-05-04 01:36:22 +08:00
|
|
|
// Autosuggestion is active and the search term has not changed, so we're good to go.
|
2017-01-25 01:27:11 +08:00
|
|
|
data->autosuggestion = std::move(result.suggestion);
|
2012-02-20 18:13:31 +08:00
|
|
|
sanity_check();
|
|
|
|
reader_repaint();
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
static void update_autosuggestion(void) {
|
|
|
|
// Updates autosuggestion. We look for an autosuggestion if the command line is non-empty and if
|
|
|
|
// we're not doing a history search.
|
2012-02-16 16:24:27 +08:00
|
|
|
data->autosuggestion.clear();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->allow_autosuggestion && !data->suppress_autosuggestion &&
|
|
|
|
!data->command_line.empty() && data->history_search.is_at_end()) {
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = data->active_edit_line();
|
2017-01-25 01:27:11 +08:00
|
|
|
auto performer = get_autosuggestion_performer(el->text, el->position, data->history);
|
|
|
|
iothread_perform(performer, &autosuggest_completed);
|
2012-02-16 16:24:27 +08:00
|
|
|
}
|
2012-02-07 02:52:13 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Accept any autosuggestion by replacing the command line with it. If full is true, take the whole
|
|
|
|
// thing; if it's false, then take only the first "word".
|
|
|
|
static void accept_autosuggestion(bool full) {
|
|
|
|
if (!data->autosuggestion.empty()) {
|
|
|
|
// Accepting an autosuggestion clears the pager.
|
2014-08-12 01:17:41 +08:00
|
|
|
clear_pager();
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Accept the autosuggestion.
|
|
|
|
if (full) {
|
|
|
|
// Just take the whole thing.
|
2014-01-26 16:41:30 +08:00
|
|
|
data->command_line.text = data->autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Accept characters up to a word separator.
|
2012-12-21 09:37:09 +08:00
|
|
|
move_word_state_machine_t state(move_word_style_punctuation);
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t idx = data->command_line.size(); idx < data->autosuggestion.size(); idx++) {
|
2012-12-11 08:23:08 +08:00
|
|
|
wchar_t wc = data->autosuggestion.at(idx);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!state.consume_char(wc)) break;
|
2014-01-26 16:41:30 +08:00
|
|
|
data->command_line.text.push_back(wc);
|
2012-12-11 08:23:08 +08:00
|
|
|
}
|
|
|
|
}
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(&data->command_line, data->command_line.size());
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(&data->command_line);
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2012-07-16 05:02:34 +08:00
|
|
|
reader_repaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Ensure we have no pager contents.
|
|
|
|
static void clear_pager() {
|
|
|
|
if (data) {
|
2014-01-20 15:52:35 +08:00
|
|
|
data->pager.clear();
|
|
|
|
data->current_page_rendering = page_rendering_t();
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2014-01-20 15:52:35 +08:00
|
|
|
}
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
static void select_completion_in_direction(enum selection_direction_t dir) {
|
2014-01-27 18:17:31 +08:00
|
|
|
assert(data != NULL);
|
2016-05-04 01:36:22 +08:00
|
|
|
bool selection_changed =
|
|
|
|
data->pager.select_next_completion_in_direction(dir, data->current_page_rendering);
|
|
|
|
if (selection_changed) {
|
2014-02-17 11:56:13 +08:00
|
|
|
data->pager_selection_changed();
|
|
|
|
}
|
2014-01-19 04:42:53 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Flash the screen. This function changes the color of the current line momentarily and sends a
|
|
|
|
/// BEL to maybe flash the screen or emite a sound, depending on how it is configured.
|
|
|
|
static void reader_flash() {
|
2012-11-19 08:30:30 +08:00
|
|
|
struct timespec pollint;
|
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = &data->command_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = 0; i < el->position; i++) {
|
|
|
|
data->colors.at(i) = highlight_spec_search_match << 16;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
reader_repaint();
|
2017-08-12 22:54:26 +08:00
|
|
|
ignore_result(write(STDOUT_FILENO, "\a", 1));
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
pollint.tv_sec = 0;
|
|
|
|
pollint.tv_nsec = 100 * 1000000;
|
|
|
|
nanosleep(&pollint, NULL);
|
2006-12-01 07:57:49 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2006-12-01 07:57:49 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
2006-12-01 07:57:49 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Characters that may not be part of a token that is to be replaced by a case insensitive
|
|
|
|
/// completion.
|
2008-01-14 09:00:32 +08:00
|
|
|
#define REPLACE_UNCLEAN L"$*?({})"
|
2008-01-14 03:32:21 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Check if the specified string can be replaced by a case insensitive completion with the
|
|
|
|
/// specified flags.
|
|
|
|
///
|
|
|
|
/// Advanced tokens like those containing {}-style expansion can not at the moment be replaced,
|
|
|
|
/// other than if the new token is already an exact replacement, e.g. if the COMPLETE_DONT_ESCAPE
|
|
|
|
/// flag is set.
|
|
|
|
static bool reader_can_replace(const wcstring &in, int flags) {
|
|
|
|
const wchar_t *str = in.c_str();
|
2008-01-14 03:32:21 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (flags & COMPLETE_DONT_ESCAPE) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
|
|
|
|
// Test characters that have a special meaning in any character position.
|
|
|
|
while (*str) {
|
|
|
|
if (wcschr(REPLACE_UNCLEAN, *str)) return false;
|
2012-11-19 08:30:30 +08:00
|
|
|
str++;
|
|
|
|
}
|
2008-01-14 03:32:21 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return true;
|
2012-08-22 08:18:52 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Determine the best match type for a set of completions.
|
|
|
|
static fuzzy_match_type_t get_best_match_type(const std::vector<completion_t> &comp) {
|
2013-05-26 06:41:18 +08:00
|
|
|
fuzzy_match_type_t best_type = fuzzy_match_none;
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = 0; i < comp.size(); i++) {
|
2016-02-04 06:19:28 +08:00
|
|
|
best_type = std::min(best_type, comp.at(i).match.type);
|
2013-05-26 06:41:18 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
// If the best type is an exact match, reduce it to prefix match. Otherwise a tab completion
|
|
|
|
// will only show one match if it matches a file exactly. (see issue #959).
|
|
|
|
if (best_type == fuzzy_match_exact) {
|
2013-08-18 10:43:13 +08:00
|
|
|
best_type = fuzzy_match_prefix;
|
|
|
|
}
|
2013-08-25 17:10:22 +08:00
|
|
|
return best_type;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Handle the list of completions. This means the following:
|
|
|
|
///
|
|
|
|
/// - If the list is empty, flash the terminal.
|
|
|
|
/// - If the list contains one element, write the whole element, and if the element does not end on
|
|
|
|
/// a '/', '@', ':', or a '=', also write a trailing space.
|
|
|
|
/// - If the list contains multiple elements with a common prefix, write the prefix.
|
|
|
|
/// - If the list contains multiple elements without a common prefix, call run_pager to display a
|
|
|
|
/// list of completions. Depending on terminal size and the length of the list, run_pager may either
|
|
|
|
/// show less than a screenfull and exit or use an interactive pager to allow the user to scroll
|
|
|
|
/// through the completions.
|
|
|
|
///
|
|
|
|
/// \param comp the list of completion strings
|
2016-11-03 12:54:57 +08:00
|
|
|
/// \param cont_after_prefix_insertion If we have a shared prefix, whether to print the list of
|
2016-05-04 01:36:22 +08:00
|
|
|
/// completions after inserting it.
|
|
|
|
///
|
|
|
|
/// Return true if we inserted text into the command line, false if we did not.
|
|
|
|
static bool handle_completions(const std::vector<completion_t> &comp,
|
2016-11-03 12:54:57 +08:00
|
|
|
bool cont_after_prefix_insertion) {
|
2012-11-19 08:30:30 +08:00
|
|
|
bool done = false;
|
2012-08-22 08:18:52 +08:00
|
|
|
bool success = false;
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = &data->command_line;
|
|
|
|
const wchar_t *begin, *end, *buff = el->text.c_str();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
parse_util_token_extent(buff, el->position, &begin, 0, 0, 0);
|
2016-05-04 01:36:22 +08:00
|
|
|
end = buff + el->position;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
const wcstring tok(begin, end - begin);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Check trivial cases.
|
2016-11-20 07:45:08 +08:00
|
|
|
size_t size = comp.size();
|
2016-10-23 11:32:25 +08:00
|
|
|
if (size == 0) {
|
|
|
|
// No suitable completions found, flash screen and return.
|
|
|
|
reader_flash();
|
|
|
|
done = true;
|
|
|
|
success = false;
|
|
|
|
} else if (size == 1) {
|
|
|
|
// Exactly one suitable completion found - insert it.
|
|
|
|
const completion_t &c = comp.at(0);
|
|
|
|
|
|
|
|
// If this is a replacement completion, check that we know how to replace it, e.g. that
|
|
|
|
// the token doesn't contain evil operators like {}.
|
|
|
|
if (!(c.flags & COMPLETE_REPLACES_TOKEN) || reader_can_replace(tok, c.flags)) {
|
|
|
|
completion_insert(c.completion.c_str(), c.flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
done = true;
|
|
|
|
success = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
if (done) {
|
|
|
|
return success;
|
|
|
|
}
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
fuzzy_match_type_t best_match_type = get_best_match_type(comp);
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// Determine whether we are going to replace the token or not. If any commands of the best
|
|
|
|
// type do not require replacement, then ignore all those that want to use replacement.
|
|
|
|
bool will_replace_token = true;
|
|
|
|
for (size_t i = 0; i < comp.size(); i++) {
|
|
|
|
const completion_t &el = comp.at(i);
|
|
|
|
if (el.match.type <= best_match_type && !(el.flags & COMPLETE_REPLACES_TOKEN)) {
|
|
|
|
will_replace_token = false;
|
|
|
|
break;
|
2013-05-26 06:41:18 +08:00
|
|
|
}
|
2016-10-31 05:16:32 +08:00
|
|
|
}
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// Decide which completions survived. There may be a lot of them; it would be nice if we could
|
|
|
|
// figure out how to avoid copying them here.
|
|
|
|
std::vector<completion_t> surviving_completions;
|
|
|
|
for (size_t i = 0; i < comp.size(); i++) {
|
|
|
|
const completion_t &el = comp.at(i);
|
|
|
|
// Ignore completions with a less suitable match type than the best.
|
|
|
|
if (el.match.type > best_match_type) continue;
|
|
|
|
|
|
|
|
// Only use completions that match replace_token.
|
|
|
|
bool completion_replace_token = static_cast<bool>(el.flags & COMPLETE_REPLACES_TOKEN);
|
|
|
|
if (completion_replace_token != will_replace_token) continue;
|
|
|
|
|
|
|
|
// Don't use completions that want to replace, if we cannot replace them.
|
|
|
|
if (completion_replace_token && !reader_can_replace(tok, el.flags)) continue;
|
|
|
|
|
|
|
|
// This completion survived.
|
|
|
|
surviving_completions.push_back(el);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool use_prefix = false;
|
|
|
|
if (match_type_shares_prefix(best_match_type)) {
|
|
|
|
// Try to find a common prefix to insert among the surviving completions.
|
|
|
|
wcstring common_prefix;
|
|
|
|
complete_flags_t flags = 0;
|
|
|
|
bool prefix_is_partial_completion = false;
|
|
|
|
for (size_t i = 0; i < surviving_completions.size(); i++) {
|
|
|
|
const completion_t &el = surviving_completions.at(i);
|
|
|
|
if (i == 0) {
|
|
|
|
// First entry, use the whole string.
|
|
|
|
common_prefix = el.completion;
|
|
|
|
flags = el.flags;
|
|
|
|
} else {
|
|
|
|
// Determine the shared prefix length.
|
|
|
|
size_t idx, max = mini(common_prefix.size(), el.completion.size());
|
|
|
|
for (idx = 0; idx < max; idx++) {
|
|
|
|
wchar_t ac = common_prefix.at(idx), bc = el.completion.at(idx);
|
|
|
|
bool matches = (ac == bc);
|
|
|
|
// If we are replacing the token, allow case to vary.
|
|
|
|
if (will_replace_token && !matches) {
|
|
|
|
// Hackish way to compare two strings in a case insensitive way,
|
|
|
|
// hopefully better than towlower().
|
|
|
|
matches = (wcsncasecmp(&ac, &bc, 1) == 0);
|
2013-05-26 06:41:18 +08:00
|
|
|
}
|
2016-10-31 05:16:32 +08:00
|
|
|
if (!matches) break;
|
|
|
|
}
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// idx is now the length of the new common prefix.
|
|
|
|
common_prefix.resize(idx);
|
|
|
|
prefix_is_partial_completion = true;
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// Early out if we decide there's no common prefix.
|
|
|
|
if (idx == 0) break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-10-31 05:16:32 +08:00
|
|
|
}
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// Determine if we use the prefix. We use it if it's non-empty and it will actually make
|
|
|
|
// the command line longer. It may make the command line longer by virtue of not using
|
|
|
|
// REPLACE_TOKEN (so it always appends to the command line), or by virtue of replacing
|
|
|
|
// the token but being longer than it.
|
|
|
|
use_prefix = common_prefix.size() > (will_replace_token ? tok.size() : 0);
|
|
|
|
assert(!use_prefix || !common_prefix.empty());
|
|
|
|
|
|
|
|
if (use_prefix) {
|
|
|
|
// We got something. If more than one completion contributed, then it means we have
|
|
|
|
// a prefix; don't insert a space after it.
|
|
|
|
if (prefix_is_partial_completion) flags |= COMPLETE_NO_SPACE;
|
|
|
|
completion_insert(common_prefix.c_str(), flags);
|
|
|
|
success = true;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-10-31 05:16:32 +08:00
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-11-03 12:54:57 +08:00
|
|
|
if (!cont_after_prefix_insertion && use_prefix) {
|
2016-10-31 05:16:32 +08:00
|
|
|
return success;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// We didn't get a common prefix, or we want to print the list anyways.
|
|
|
|
size_t len, prefix_start = 0;
|
|
|
|
wcstring prefix;
|
|
|
|
parse_util_get_parameter_info(el->text, el->position, NULL, &prefix_start, NULL);
|
2013-06-02 16:14:26 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
assert(el->position >= prefix_start);
|
|
|
|
len = el->position - prefix_start;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
if (will_replace_token || match_type_requires_full_replacement(best_match_type)) {
|
|
|
|
prefix.clear(); // no prefix
|
|
|
|
} else if (len <= PREFIX_MAX_LEN) {
|
|
|
|
prefix.append(el->text, prefix_start, len);
|
|
|
|
} else {
|
|
|
|
// Append just the end of the string.
|
|
|
|
prefix = wcstring(&ellipsis_char, 1);
|
|
|
|
prefix.append(el->text, prefix_start + len - PREFIX_MAX_LEN, PREFIX_MAX_LEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
wchar_t quote;
|
|
|
|
parse_util_get_parameter_info(el->text, el->position, "e, NULL, NULL);
|
|
|
|
// Update the pager data.
|
|
|
|
data->pager.set_prefix(prefix);
|
|
|
|
data->pager.set_completions(surviving_completions);
|
|
|
|
// Invalidate our rendering.
|
|
|
|
data->current_page_rendering = page_rendering_t();
|
|
|
|
// Modify the command line to reflect the new pager.
|
|
|
|
data->pager_selection_changed();
|
|
|
|
reader_repaint_needed();
|
|
|
|
return false;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Return true if we believe ourselves to be orphaned. loop_count is how many times we've tried to
|
|
|
|
/// stop ourselves via SIGGTIN.
|
|
|
|
static bool check_for_orphaned_process(unsigned long loop_count, pid_t shell_pgid) {
|
2012-12-06 05:33:07 +08:00
|
|
|
bool we_think_we_are_orphaned = false;
|
2016-05-04 01:36:22 +08:00
|
|
|
// Try kill-0'ing the process whose pid corresponds to our process group ID. It's possible this
|
|
|
|
// will fail because we don't have permission to signal it. But more likely it will fail because
|
|
|
|
// it no longer exists, and we are orphaned.
|
2016-10-23 02:21:13 +08:00
|
|
|
if (loop_count % 64 == 0 && kill(shell_pgid, 0) < 0 && errno == ESRCH) {
|
|
|
|
we_think_we_are_orphaned = true;
|
2012-12-06 05:33:07 +08:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:07:47 +08:00
|
|
|
// Try reading from the tty; if we get EIO we are orphaned. This is sort of bad because it
|
|
|
|
// may block.
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!we_think_we_are_orphaned && loop_count % 128 == 0) {
|
2017-05-10 12:39:25 +08:00
|
|
|
#ifdef HAVE_CTERMID_R
|
|
|
|
char buf[L_ctermid];
|
|
|
|
char *tty = ctermid_r(buf);
|
|
|
|
#else
|
2012-12-06 05:33:07 +08:00
|
|
|
char *tty = ctermid(NULL);
|
2017-05-10 12:39:25 +08:00
|
|
|
#endif
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!tty) {
|
2012-12-06 05:33:07 +08:00
|
|
|
wperror(L"ctermid");
|
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Open the tty. Presumably this is stdin, but maybe not?
|
2012-12-06 05:33:07 +08:00
|
|
|
int tty_fd = open(tty, O_RDONLY | O_NONBLOCK);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (tty_fd < 0) {
|
2012-12-06 05:33:07 +08:00
|
|
|
wperror(L"open");
|
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char tmp;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (read(tty_fd, &tmp, 1) < 0 && errno == EIO) {
|
2012-12-06 05:33:07 +08:00
|
|
|
we_think_we_are_orphaned = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(tty_fd);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Just give up if we've done it a lot times.
|
|
|
|
if (loop_count > 4096) {
|
2012-12-06 05:33:07 +08:00
|
|
|
we_think_we_are_orphaned = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return we_think_we_are_orphaned;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Initialize data for interactive use.
|
|
|
|
static void reader_interactive_init() {
|
|
|
|
// See if we are running interactively.
|
2012-11-19 08:30:30 +08:00
|
|
|
pid_t shell_pgid;
|
|
|
|
|
2017-07-16 12:21:24 +08:00
|
|
|
if (!input_initialized) init_input();
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_init();
|
|
|
|
shell_pgid = getpgrp();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// This should enable job control on fish, even if our parent process did not enable it for us.
|
|
|
|
|
|
|
|
// Check if we are in control of the terminal, so that we don't do semi-expensive things like
|
|
|
|
// reset signal handlers unless we really have to, which we often don't.
|
|
|
|
if (tcgetpgrp(STDIN_FILENO) != shell_pgid) {
|
|
|
|
// Bummer, we are not in control of the terminal. Stop until parent has given us control of
|
2017-08-07 05:46:12 +08:00
|
|
|
// it.
|
2016-05-04 01:36:22 +08:00
|
|
|
//
|
|
|
|
// In theory, reseting signal handlers could cause us to miss signal deliveries. In
|
2017-08-07 05:46:12 +08:00
|
|
|
// practice, this code should only be run during startup, when we're not waiting for any
|
2016-05-04 01:36:22 +08:00
|
|
|
// signals.
|
2012-11-19 08:30:30 +08:00
|
|
|
signal_reset_handlers();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Ok, signal handlers are taken out of the picture. Stop ourself in a loop until we are in
|
|
|
|
// control of the terminal. However, the call to signal(SIGTTIN) may silently not do
|
|
|
|
// anything if we are orphaned.
|
|
|
|
//
|
|
|
|
// As far as I can tell there's no really good way to detect that we are orphaned. One way
|
|
|
|
// is to just detect if the group leader exited, via kill(shell_pgid, 0). Another
|
|
|
|
// possibility is that read() from the tty fails with EIO - this is more reliable but it's
|
|
|
|
// harder, because it may succeed or block. So we loop for a while, trying those strategies.
|
|
|
|
// Eventually we just give up and assume we're orphaend.
|
|
|
|
for (unsigned long loop_count = 0;; loop_count++) {
|
2012-12-06 05:33:07 +08:00
|
|
|
pid_t owner = tcgetpgrp(STDIN_FILENO);
|
|
|
|
shell_pgid = getpgrp();
|
2017-01-11 13:52:10 +08:00
|
|
|
if (owner == -1 && errno == ENOTTY) {
|
2012-12-06 05:33:07 +08:00
|
|
|
// No TTY, cannot be interactive?
|
2017-01-11 13:52:10 +08:00
|
|
|
redirect_tty_output();
|
2016-05-04 01:36:22 +08:00
|
|
|
debug(1, _(L"No TTY for interactive shell (tcgetpgrp failed)"));
|
2012-12-06 05:33:07 +08:00
|
|
|
wperror(L"setpgid");
|
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
if (owner == shell_pgid) {
|
|
|
|
break; // success
|
|
|
|
} else {
|
|
|
|
if (check_for_orphaned_process(loop_count, shell_pgid)) {
|
|
|
|
// We're orphaned, so we just die. Another sad statistic.
|
2017-05-02 12:44:30 +08:00
|
|
|
const wchar_t *fmt =
|
|
|
|
_(L"I appear to be an orphaned process, so I am quitting politely. "
|
|
|
|
L"My pid is %d.");
|
|
|
|
debug(1, fmt, (int)getpid());
|
2012-12-06 05:33:07 +08:00
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Try stopping us.
|
2012-12-06 05:33:07 +08:00
|
|
|
int ret = killpg(shell_pgid, SIGTTIN);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (ret < 0) {
|
2012-12-06 05:33:07 +08:00
|
|
|
wperror(L"killpg");
|
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
signal_set_handlers();
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Put ourselves in our own process group.
|
2012-11-19 08:30:30 +08:00
|
|
|
shell_pgid = getpid();
|
2016-10-23 02:21:13 +08:00
|
|
|
if (getpgrp() != shell_pgid && setpgid(shell_pgid, shell_pgid) < 0) {
|
2016-12-15 11:21:36 +08:00
|
|
|
debug(0, _(L"Couldn't put the shell in its own process group"));
|
2016-10-23 02:21:13 +08:00
|
|
|
wperror(L"setpgid");
|
|
|
|
exit_without_destructors(1);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Grab control of the terminal.
|
2016-12-15 11:21:36 +08:00
|
|
|
if (tcsetpgrp(STDIN_FILENO, shell_pgid) == -1) {
|
|
|
|
if (errno == ENOTTY) redirect_tty_output();
|
|
|
|
debug(0, _(L"Couldn't grab control of terminal"));
|
2012-11-19 08:30:30 +08:00
|
|
|
wperror(L"tcsetpgrp");
|
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
|
|
|
|
2017-01-19 05:54:54 +08:00
|
|
|
invalidate_termsize();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-12-15 11:21:36 +08:00
|
|
|
// Set the new modes.
|
|
|
|
if (tcsetattr(0, TCSANOW, &shell_modes) == -1) {
|
2017-01-11 13:52:10 +08:00
|
|
|
if (errno == EIO) redirect_tty_output();
|
2012-11-19 08:30:30 +08:00
|
|
|
wperror(L"tcsetattr");
|
|
|
|
}
|
|
|
|
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(L"_", ENV_GLOBAL, L"fish");
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Destroy data for interactive use.
|
|
|
|
static void reader_interactive_destroy() {
|
2012-11-19 08:30:30 +08:00
|
|
|
kill_destroy();
|
|
|
|
set_color(rgb_color_t::reset(), rgb_color_t::reset());
|
|
|
|
input_destroy();
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_sanity_check() {
|
|
|
|
// Note: 'data' is non-null if we are interactive, except in the testing environment.
|
2016-05-15 11:35:54 +08:00
|
|
|
if (shell_is_interactive() && data != NULL) {
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->command_line.position > data->command_line.size()) sanity_lose();
|
|
|
|
if (data->colors.size() != data->command_line.size()) sanity_lose();
|
|
|
|
if (data->indents.size() != data->command_line.size()) sanity_lose();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Set the specified string as the current buffer.
|
|
|
|
static void set_command_line_and_position(editable_line_t *el, const wcstring &new_str,
|
|
|
|
size_t pos) {
|
2014-01-27 06:47:15 +08:00
|
|
|
el->text = new_str;
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, pos);
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(el);
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2014-01-27 18:28:19 +08:00
|
|
|
reader_repaint_needed();
|
2012-06-13 00:41:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
static void reader_replace_current_token(const wcstring &new_token) {
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *begin, *end;
|
|
|
|
size_t new_pos;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Find current token.
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
|
|
|
const wchar_t *buff = el->text.c_str();
|
|
|
|
parse_util_token_extent(buff, el->position, &begin, &end, 0, 0);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!begin || !end) return;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Make new string.
|
2012-03-04 13:59:45 +08:00
|
|
|
wcstring new_buff(buff, begin - buff);
|
|
|
|
new_buff.append(new_token);
|
|
|
|
new_buff.append(end);
|
2016-05-04 01:36:22 +08:00
|
|
|
new_pos = (begin - buff) + new_token.size();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
set_command_line_and_position(el, new_buff, new_pos);
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Reset the data structures associated with the token search.
|
|
|
|
static void reset_token_history() {
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = data->active_edit_line();
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *begin, *end;
|
2014-01-27 06:47:15 +08:00
|
|
|
const wchar_t *buff = el->text.c_str();
|
|
|
|
parse_util_token_extent((wchar_t *)buff, el->position, &begin, &end, 0, 0);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
data->search_buff.clear();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (begin) {
|
2012-02-04 12:01:01 +08:00
|
|
|
data->search_buff.append(begin, end - begin);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
data->token_history_pos = -1;
|
2016-05-04 01:36:22 +08:00
|
|
|
data->search_pos = 0;
|
2012-02-04 12:01:01 +08:00
|
|
|
data->search_prev.clear();
|
|
|
|
data->search_prev.push_back(data->search_buff);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
data->history_search =
|
|
|
|
history_search_t(*data->history, data->search_buff, HISTORY_SEARCH_TYPE_CONTAINS);
|
2005-09-25 00:31:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Handles a token search command.
|
|
|
|
///
|
|
|
|
/// \param forward if the search should be forward or reverse
|
|
|
|
/// \param reset whether the current token should be made the new search token
|
|
|
|
static void handle_token_history(int forward, int reset) {
|
|
|
|
if (!data) return;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2015-07-20 17:34:57 +08:00
|
|
|
wcstring str;
|
2015-07-26 14:05:47 +08:00
|
|
|
size_t current_pos;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (reset) {
|
|
|
|
// Start a new token search using the current token.
|
2012-11-19 08:30:30 +08:00
|
|
|
reset_token_history();
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
current_pos = data->token_history_pos;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (forward || data->search_pos + 1 < data->search_prev.size()) {
|
|
|
|
if (forward) {
|
|
|
|
if (data->search_pos > 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
data->search_pos--;
|
|
|
|
}
|
2015-07-20 17:34:57 +08:00
|
|
|
str = data->search_prev.at(data->search_pos);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
data->search_pos++;
|
2015-07-20 17:34:57 +08:00
|
|
|
str = data->search_prev.at(data->search_pos);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
reader_replace_current_token(str);
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
if (current_pos == size_t(-1)) {
|
2012-02-06 15:22:18 +08:00
|
|
|
data->token_history_buff.clear();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Search for previous item that contains this substring.
|
|
|
|
if (data->history_search.go_backwards()) {
|
2012-02-16 16:24:27 +08:00
|
|
|
data->token_history_buff = data->history_search.current_string();
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
current_pos = data->token_history_buff.size();
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->token_history_buff.empty()) {
|
|
|
|
// We have reached the end of the history - check if the history already contains the
|
|
|
|
// search string itself, if so return, otherwise add it.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wcstring &last = data->search_prev.back();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->search_buff != last) {
|
2015-07-20 17:34:57 +08:00
|
|
|
str = data->search_buff;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// debug( 3, L"new '%ls'", data->token_history_buff.c_str() );
|
2012-11-22 09:48:35 +08:00
|
|
|
tokenizer_t tok(data->token_history_buff.c_str(), TOK_ACCEPT_UNFINISHED);
|
2015-07-26 14:05:47 +08:00
|
|
|
tok_t token;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (tok.next(&token)) {
|
2017-05-26 11:46:38 +08:00
|
|
|
if (token.type != TOK_STRING) continue;
|
|
|
|
if (token.text.find(data->search_buff) == wcstring::npos) continue;
|
|
|
|
if (token.offset >= current_pos) continue;
|
|
|
|
|
|
|
|
auto found = find(data->search_prev.begin(), data->search_prev.end(), token.text);
|
|
|
|
if (found == data->search_prev.end()) {
|
|
|
|
data->token_history_pos = token.offset;
|
|
|
|
str = token.text;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!str.empty()) {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_replace_current_token(str);
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
2012-08-05 04:47:56 +08:00
|
|
|
data->search_pos = data->search_prev.size();
|
2012-02-03 06:27:13 +08:00
|
|
|
data->search_prev.push_back(str);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (!reader_interrupted()) {
|
|
|
|
data->token_history_pos = -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
handle_token_history(0, 0);
|
|
|
|
}
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-06-06 09:46:04 +08:00
|
|
|
enum move_word_dir_t { MOVE_DIR_LEFT, MOVE_DIR_RIGHT };
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Move buffer position one word or erase one word. This function updates both the internal buffer
|
|
|
|
/// and the screen. It is used by M-left, M-right and ^W to do block movement or block erase.
|
|
|
|
///
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param move_right true if moving right
|
2016-05-04 01:36:22 +08:00
|
|
|
/// \param erase Whether to erase the characters along the way or only move past them.
|
2016-06-06 09:46:04 +08:00
|
|
|
/// \param newv if the new kill item should be appended to the previous kill item or not.
|
2016-05-04 01:36:22 +08:00
|
|
|
static void move_word(editable_line_t *el, bool move_right, bool erase,
|
|
|
|
enum move_word_style_t style, bool newv) {
|
|
|
|
// Return if we are already at the edge.
|
2014-01-27 06:55:41 +08:00
|
|
|
const size_t boundary = move_right ? el->size() : 0;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position == boundary) return;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// When moving left, a value of 1 means the character at index 0.
|
2012-12-21 09:37:09 +08:00
|
|
|
move_word_state_machine_t state(style);
|
2016-05-04 01:36:22 +08:00
|
|
|
const wchar_t *const command_line = el->text.c_str();
|
2014-01-27 06:47:15 +08:00
|
|
|
const size_t start_buff_pos = el->position;
|
2012-11-19 18:41:57 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
size_t buff_pos = el->position;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (buff_pos != boundary) {
|
2012-11-19 18:41:57 +08:00
|
|
|
size_t idx = (move_right ? buff_pos : buff_pos - 1);
|
|
|
|
wchar_t c = command_line[idx];
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!state.consume_char(c)) break;
|
2012-11-19 18:41:57 +08:00
|
|
|
buff_pos = (move_right ? buff_pos + 1 : buff_pos - 1);
|
2012-10-05 05:35:03 +08:00
|
|
|
}
|
2006-10-13 00:13:17 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Always consume at least one character.
|
|
|
|
if (buff_pos == start_buff_pos) buff_pos = (move_right ? buff_pos + 1 : buff_pos - 1);
|
2012-11-19 18:41:57 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If we are moving left, buff_pos-1 is the index of the first character we do not delete
|
|
|
|
// (possibly -1). If we are moving right, then buff_pos is that index - possibly el->size().
|
|
|
|
if (erase) {
|
|
|
|
// Don't autosuggest after a kill.
|
|
|
|
if (el == &data->command_line) {
|
2014-04-01 01:01:39 +08:00
|
|
|
data->suppress_autosuggestion = true;
|
2014-01-27 06:55:41 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (move_right) {
|
2014-01-27 06:55:41 +08:00
|
|
|
reader_kill(el, start_buff_pos, buff_pos - start_buff_pos, KILL_APPEND, newv);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2014-01-27 06:55:41 +08:00
|
|
|
reader_kill(el, buff_pos, start_buff_pos - buff_pos, KILL_PREPEND, newv);
|
2012-11-19 18:41:57 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, buff_pos);
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
|
|
|
}
|
2012-11-19 18:41:57 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
const wchar_t *reader_get_buffer(void) {
|
2011-12-27 11:18:46 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2014-01-27 06:47:15 +08:00
|
|
|
return data ? data->command_line.text.c_str() : NULL;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
history_t *reader_get_history(void) {
|
2012-02-06 08:42:24 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-19 08:30:30 +08:00
|
|
|
return data ? data->history : NULL;
|
2012-02-06 08:42:24 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Sets the command line contents, without clearing the pager.
|
|
|
|
static void reader_set_buffer_maintaining_pager(const wcstring &b, size_t pos) {
|
|
|
|
// Callers like to pass us pointers into ourselves, so be careful! I don't know if we can use
|
|
|
|
// operator= with a pointer to our interior, so use an intermediate.
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t command_line_len = b.size();
|
2014-01-26 16:41:30 +08:00
|
|
|
data->command_line.text = b;
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(&data->command_line);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Don't set a position past the command line length.
|
2016-10-21 09:53:31 +08:00
|
|
|
if (pos > command_line_len) pos = command_line_len; //!OCLINT(parameter reassignment)
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(&data->command_line, pos);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Clear history search and pager contents.
|
2012-11-19 08:30:30 +08:00
|
|
|
data->search_mode = NO_SEARCH;
|
|
|
|
data->search_buff.clear();
|
|
|
|
data->history_search.go_to_end();
|
2009-02-21 23:44:20 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint_needed();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Sets the command line contents, clearing the pager.
|
|
|
|
void reader_set_buffer(const wcstring &b, size_t pos) {
|
|
|
|
if (!data) return;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-21 04:38:56 +08:00
|
|
|
clear_pager();
|
|
|
|
reader_set_buffer_maintaining_pager(b, pos);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
size_t reader_get_cursor_pos() {
|
2016-05-05 06:19:47 +08:00
|
|
|
if (!data) return (size_t)-1;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
return data->command_line.position;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
bool reader_get_selection(size_t *start, size_t *len) {
|
2014-03-31 04:13:35 +08:00
|
|
|
bool result = false;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data != NULL && data->sel_active) {
|
2014-03-31 04:13:35 +08:00
|
|
|
*start = data->sel_start_pos;
|
2016-12-31 05:52:41 +08:00
|
|
|
*len = std::min(data->sel_stop_pos - data->sel_start_pos, data->command_line.size());
|
2014-03-31 04:13:35 +08:00
|
|
|
result = true;
|
2014-01-15 22:07:22 +08:00
|
|
|
}
|
2014-03-31 04:13:35 +08:00
|
|
|
return result;
|
2014-01-15 22:07:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void set_env_cmd_duration(struct timeval *after, struct timeval *before) {
|
2012-11-19 08:30:30 +08:00
|
|
|
time_t secs = after->tv_sec - before->tv_sec;
|
|
|
|
suseconds_t usecs = after->tv_usec - before->tv_usec;
|
|
|
|
wchar_t buf[16];
|
2009-11-24 23:28:42 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (after->tv_usec < before->tv_usec) {
|
2012-11-19 08:30:30 +08:00
|
|
|
usecs += 1000000;
|
|
|
|
secs -= 1;
|
|
|
|
}
|
2009-11-24 23:28:42 +08:00
|
|
|
|
2014-08-02 06:55:36 +08:00
|
|
|
swprintf(buf, 16, L"%d", (secs * 1000) + (usecs / 1000));
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(ENV_CMD_DURATION, ENV_UNEXPORT, buf);
|
2009-11-24 23:28:42 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_run_command(parser_t &parser, const wcstring &cmd) {
|
2012-11-19 08:30:30 +08:00
|
|
|
struct timeval time_before, time_after;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2015-07-26 15:58:32 +08:00
|
|
|
wcstring ft = tok_first(cmd);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-08-06 06:08:39 +08:00
|
|
|
if (!ft.empty()) env_set_one(L"_", ENV_GLOBAL, ft);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2014-08-03 12:01:40 +08:00
|
|
|
reader_write_title(cmd);
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
term_donate();
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
gettimeofday(&time_before, NULL);
|
2009-11-24 23:28:42 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
parser.eval(cmd, io_chain_t(), TOP);
|
|
|
|
job_reap(1);
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
gettimeofday(&time_after, NULL);
|
|
|
|
set_env_cmd_duration(&time_after, &time_before);
|
2009-11-24 23:28:42 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
term_steal();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-08-06 06:08:39 +08:00
|
|
|
env_set_one(L"_", ENV_GLOBAL, program_name);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
#ifdef HAVE__PROC_SELF_STAT
|
2012-11-19 08:30:30 +08:00
|
|
|
proc_update_jiffies();
|
2005-09-20 21:26:39 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
parser_test_error_bits_t reader_shell_test(const wchar_t *b) {
|
2014-01-09 10:20:55 +08:00
|
|
|
assert(b != NULL);
|
2013-12-13 10:18:07 +08:00
|
|
|
wcstring bstr = b;
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Append a newline, to act as a statement terminator.
|
2014-01-09 10:20:55 +08:00
|
|
|
bstr.push_back(L'\n');
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2013-12-16 08:05:37 +08:00
|
|
|
parse_error_list_t errors;
|
2016-05-04 01:36:22 +08:00
|
|
|
parser_test_error_bits_t res =
|
|
|
|
parse_util_detect_errors(bstr, &errors, true /* do accept incomplete */);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (res & PARSER_TEST_ERROR) {
|
2014-01-09 10:20:55 +08:00
|
|
|
wcstring error_desc;
|
2017-06-18 13:36:56 +08:00
|
|
|
parser_t::principal_parser().get_backtrace(bstr, errors, error_desc);
|
2014-01-15 17:40:40 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Ensure we end with a newline. Also add an initial newline, because it's likely the user
|
|
|
|
// just hit enter and so there's junk on the current line.
|
|
|
|
if (!string_suffixes_string(L"\n", error_desc)) {
|
2014-01-09 10:20:55 +08:00
|
|
|
error_desc.push_back(L'\n');
|
2013-12-17 08:52:23 +08:00
|
|
|
}
|
2014-01-09 10:20:55 +08:00
|
|
|
fwprintf(stderr, L"\n%ls", error_desc.c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Test if the given string contains error. Since this is the error detection for general purpose,
|
|
|
|
/// there are no invalid strings, so this function always returns false.
|
2016-10-10 05:38:26 +08:00
|
|
|
///
|
|
|
|
/// TODO: Possibly remove this. It is called from only only one place: reader_push().Since it always
|
|
|
|
/// returns a static result it's not clear why it's needed.
|
|
|
|
static parser_test_error_bits_t default_test(const wchar_t *b) {
|
|
|
|
UNUSED(b);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-07-28 12:32:49 +08:00
|
|
|
void reader_change_history(const wchar_t *name) {
|
|
|
|
data->history->save();
|
|
|
|
data->history = &history_t::history_with_name(name);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_push(const wchar_t *name) {
|
2012-07-18 03:47:01 +08:00
|
|
|
reader_data_t *n = new reader_data_t();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
n->history = &history_t::history_with_name(name);
|
2012-11-19 08:30:30 +08:00
|
|
|
n->app_name = name;
|
|
|
|
n->next = data;
|
2006-10-02 00:02:58 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
data = n;
|
2006-10-02 00:02:58 +08:00
|
|
|
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(&data->command_line);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->next == 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_interactive_init();
|
|
|
|
}
|
2006-10-16 23:32:26 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
exec_prompt();
|
|
|
|
reader_set_highlight_function(&highlight_universal);
|
|
|
|
reader_set_test_function(&default_test);
|
|
|
|
reader_set_left_prompt(L"");
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_pop() {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_data_t *n = data;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data == 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
debug(0, _(L"Pop null reader block"));
|
|
|
|
sanity_lose();
|
|
|
|
return;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
data = data->next;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Invoke the destructor to balance our new.
|
2012-02-01 12:03:48 +08:00
|
|
|
delete n;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data == 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_interactive_destroy();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
end_loop = 0;
|
2016-05-04 01:36:22 +08:00
|
|
|
// history_set_mode( data->app_name.c_str() );
|
2012-11-25 08:42:25 +08:00
|
|
|
s_reset(&data->screen, screen_reset_abandon_line);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_left_prompt(const wcstring &new_prompt) { data->left_prompt = new_prompt; }
|
2012-11-08 11:59:20 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_right_prompt(const wcstring &new_prompt) { data->right_prompt = new_prompt; }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_allow_autosuggesting(bool flag) { data->allow_autosuggestion = flag; }
|
2012-11-18 18:43:35 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_expand_abbreviations(bool flag) { data->expand_abbreviations = flag; }
|
2013-07-17 15:38:04 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_complete_function(complete_function_t f) { data->complete_func = f; }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_highlight_function(highlight_function_t func) { data->highlight_function = func; }
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_test_function(parser_test_error_bits_t (*f)(const wchar_t *)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
data->test_func = f;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_set_exit_on_interrupt(bool i) { data->exit_on_interrupt = i; }
|
2013-01-21 05:23:27 +08:00
|
|
|
|
2017-04-10 02:48:21 +08:00
|
|
|
void reader_set_silent_status(bool flag) { data->silent = flag; }
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
void reader_import_history_if_necessary(void) {
|
|
|
|
// Import history from older location (config path) if our current history is empty.
|
|
|
|
if (data->history && data->history->is_empty()) {
|
2015-09-19 11:47:38 +08:00
|
|
|
data->history->populate_from_config_path();
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Import history from bash, etc. if our current history is still empty.
|
|
|
|
if (data->history && data->history->is_empty()) {
|
|
|
|
// Try opening a bash file. We make an effort to respect $HISTFILE; this isn't very complete
|
|
|
|
// (AFAIK it doesn't have to be exported), and to really get this right we ought to ask bash
|
|
|
|
// itself. But this is better than nothing.
|
2017-08-28 15:25:41 +08:00
|
|
|
const auto var = env_get(L"HISTFILE");
|
|
|
|
wcstring path = (var ? var->as_string() : L"~/.bash_history");
|
2012-07-10 13:54:08 +08:00
|
|
|
expand_tilde(path);
|
|
|
|
FILE *f = wfopen(path, "r");
|
2016-05-04 01:36:22 +08:00
|
|
|
if (f) {
|
2012-07-10 13:54:08 +08:00
|
|
|
data->history->populate_from_bash(f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Called to set the highlight flag for search results.
|
|
|
|
static void highlight_search(void) {
|
|
|
|
if (!data->search_buff.empty() && !data->history_search.is_at_end()) {
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = &data->command_line;
|
2013-01-05 14:32:40 +08:00
|
|
|
const wcstring &needle = data->search_buff;
|
2014-01-27 06:47:15 +08:00
|
|
|
size_t match_pos = el->text.find(needle);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (match_pos != wcstring::npos) {
|
2013-01-05 14:32:40 +08:00
|
|
|
size_t end = match_pos + needle.size();
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = match_pos; i < end; i++) {
|
|
|
|
data->colors.at(i) |= (highlight_spec_search_match << 16);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 00:23:24 +08:00
|
|
|
struct highlight_result_t {
|
|
|
|
std::vector<highlight_spec_t> colors;
|
|
|
|
wcstring text;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void highlight_complete(highlight_result_t result) {
|
2012-02-06 08:42:24 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2017-01-25 00:23:24 +08:00
|
|
|
if (result.text == data->command_line.text) {
|
2016-05-04 01:36:22 +08:00
|
|
|
// The data hasn't changed, so swap in our colors. The colors may not have changed, so do
|
|
|
|
// nothing if they have not.
|
2017-01-25 00:23:24 +08:00
|
|
|
assert(result.colors.size() == data->command_line.size());
|
|
|
|
if (data->colors != result.colors) {
|
|
|
|
data->colors = std::move(result.colors);
|
2012-10-04 03:19:27 +08:00
|
|
|
sanity_check();
|
|
|
|
highlight_search();
|
|
|
|
reader_repaint();
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:23:24 +08:00
|
|
|
// Given text, bracket matching position, and whether IO is allowed,
|
|
|
|
// return a function that performs highlighting. The function may be invoked on a background thread.
|
|
|
|
static std::function<highlight_result_t(void)> get_highlight_performer(const wcstring &text,
|
2017-01-27 12:00:43 +08:00
|
|
|
long match_highlight_pos,
|
|
|
|
bool no_io) {
|
2017-01-25 00:23:24 +08:00
|
|
|
env_vars_snapshot_t vars(env_vars_snapshot_t::highlighting_keys);
|
2017-03-27 04:44:27 +08:00
|
|
|
unsigned int generation_count = read_generation_count();
|
2017-01-25 00:23:24 +08:00
|
|
|
highlight_function_t highlight_func = no_io ? highlight_shell_no_io : data->highlight_function;
|
|
|
|
return [=]() -> highlight_result_t {
|
2017-03-27 04:44:27 +08:00
|
|
|
if (generation_count != read_generation_count()) {
|
2017-01-25 00:23:24 +08:00
|
|
|
// The gen count has changed, so don't do anything.
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
if (text.empty()) {
|
|
|
|
return {};
|
|
|
|
}
|
2017-02-15 13:09:15 +08:00
|
|
|
DIE_ON_FAILURE(
|
2017-01-27 12:00:43 +08:00
|
|
|
pthread_setspecific(generation_count_key, (void *)(uintptr_t)generation_count));
|
2017-01-25 00:23:24 +08:00
|
|
|
std::vector<highlight_spec_t> colors(text.size(), 0);
|
|
|
|
highlight_func(text, colors, match_highlight_pos, NULL /* error */, vars);
|
|
|
|
return {std::move(colors), text};
|
|
|
|
};
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Call specified external highlighting function and then do search highlighting. Lastly, clear the
|
|
|
|
/// background color under the cursor to avoid repaint issues on terminals where e.g. syntax
|
|
|
|
/// highlighting maykes characters under the sursor unreadable.
|
|
|
|
///
|
|
|
|
/// \param match_highlight_pos_adjust the adjustment to the position to use for bracket matching.
|
2016-06-06 09:46:04 +08:00
|
|
|
/// This is added to the current cursor position and may be negative.
|
2016-05-04 01:36:22 +08:00
|
|
|
/// \param no_io if true, do a highlight that does not perform I/O, synchronously. If false, perform
|
2016-06-06 09:46:04 +08:00
|
|
|
/// an asynchronous highlight in the background, which may perform disk I/O.
|
2016-05-04 01:36:22 +08:00
|
|
|
static void reader_super_highlight_me_plenty(int match_highlight_pos_adjust, bool no_io) {
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = &data->command_line;
|
2016-06-24 13:44:58 +08:00
|
|
|
assert(el != NULL);
|
2014-01-27 06:47:15 +08:00
|
|
|
long match_highlight_pos = (long)el->position + match_highlight_pos_adjust;
|
|
|
|
assert(match_highlight_pos >= 0);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2012-02-07 03:34:49 +08:00
|
|
|
reader_sanity_check();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-01-25 00:23:24 +08:00
|
|
|
auto highlight_performer = get_highlight_performer(el->text, match_highlight_pos, no_io);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (no_io) {
|
2017-01-25 00:23:24 +08:00
|
|
|
// Highlighting without IO, we just do it.
|
|
|
|
highlight_complete(highlight_performer());
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Highlighting including I/O proceeds in the background.
|
2017-01-25 00:23:24 +08:00
|
|
|
iothread_perform(highlight_performer, &highlight_complete);
|
2014-03-27 09:49:09 +08:00
|
|
|
}
|
2012-02-04 12:05:50 +08:00
|
|
|
highlight_search();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Here's a hack. Check to see if our autosuggestion still applies; if so, don't recompute it.
|
|
|
|
// Since the autosuggestion computation is asynchronous, this avoids "flashing" as you type into
|
|
|
|
// the autosuggestion.
|
2014-01-27 06:47:15 +08:00
|
|
|
const wcstring &cmd = el->text, &suggest = data->autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (can_autosuggest() && !suggest.empty() &&
|
|
|
|
string_prefixes_string_case_insensitive(cmd, suggest)) {
|
2016-11-02 11:42:02 +08:00
|
|
|
; // the autosuggestion is still reasonable, so do nothing
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-02-16 16:24:27 +08:00
|
|
|
update_autosuggestion();
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
bool shell_is_exiting() {
|
2016-11-03 05:42:33 +08:00
|
|
|
if (shell_is_interactive()) {
|
2014-01-03 04:37:50 +08:00
|
|
|
return job_list_is_empty() && data != NULL && data->end_loop;
|
2016-11-03 05:42:33 +08:00
|
|
|
}
|
|
|
|
return end_loop;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2017-08-09 17:56:04 +08:00
|
|
|
static void bg_job_warning() {
|
|
|
|
fputws(_(L"There are still jobs active:\n"), stdout);
|
|
|
|
fputws(_(L"\n PID Command\n"), stdout);
|
|
|
|
|
|
|
|
job_iterator_t jobs;
|
|
|
|
while (job_t *j = jobs.next()) {
|
|
|
|
if (!job_is_completed(j)) {
|
|
|
|
fwprintf(stdout, L"%6d %ls\n", j->pgid, j->command_wcstr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fputws(L"\n", stdout);
|
|
|
|
fputws(_(L"Use `disown PID` to let them live independently from fish.\n"), stdout);
|
|
|
|
fputws(_(L"A second attempt to exit will terminate them.\n"), stdout);
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// This function is called when the main loop notices that end_loop has been set while in
|
|
|
|
/// interactive mode. It checks if it is ok to exit.
|
|
|
|
static void handle_end_loop() {
|
2017-01-25 07:15:38 +08:00
|
|
|
job_iterator_t jobs;
|
|
|
|
|
2016-12-20 12:35:57 +08:00
|
|
|
if (!reader_exit_forced()) {
|
|
|
|
const parser_t &parser = parser_t::principal_parser();
|
|
|
|
for (size_t i = 0; i < parser.block_count(); i++) {
|
|
|
|
if (parser.block_at_index(i)->type() == BREAKPOINT) {
|
|
|
|
// We're executing within a breakpoint so we do not want to terminate the shell and
|
|
|
|
// kill background jobs.
|
|
|
|
return;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 07:15:38 +08:00
|
|
|
bool bg_jobs = false;
|
2017-01-27 06:47:32 +08:00
|
|
|
while (const job_t *j = jobs.next()) {
|
2017-01-25 07:15:38 +08:00
|
|
|
if (!job_is_completed(j)) {
|
|
|
|
bg_jobs = true;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-01-25 07:15:38 +08:00
|
|
|
if (!data->prev_end_loop && bg_jobs) {
|
2017-08-09 17:56:04 +08:00
|
|
|
bg_job_warning();
|
2017-01-25 07:15:38 +08:00
|
|
|
reader_exit(0, 0);
|
|
|
|
data->prev_end_loop = 1;
|
|
|
|
return;
|
|
|
|
}
|
2016-12-20 12:35:57 +08:00
|
|
|
}
|
|
|
|
|
2016-12-21 07:49:55 +08:00
|
|
|
// Kill remaining jobs before exiting.
|
2016-12-20 12:35:57 +08:00
|
|
|
jobs.reset();
|
|
|
|
while (job_t *j = jobs.next()) {
|
|
|
|
if (!job_is_completed(j)) {
|
|
|
|
if (job_is_stopped(j)) job_signal(j, SIGCONT);
|
|
|
|
job_signal(j, SIGHUP);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2007-09-26 17:01:59 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
static bool selection_is_at_top() {
|
2014-01-27 18:17:31 +08:00
|
|
|
const pager_t *pager = &data->pager;
|
|
|
|
size_t row = pager->get_selected_row(data->current_page_rendering);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (row != 0 && row != PAGER_SELECTION_NONE) return false;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-27 18:17:31 +08:00
|
|
|
size_t col = pager->get_selected_column(data->current_page_rendering);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (col != 0 && col != PAGER_SELECTION_NONE) return false;
|
2014-01-27 18:17:31 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Read interactively. Read input from stdin while providing editing facilities.
|
|
|
|
static int read_i(void) {
|
2017-07-01 08:13:02 +08:00
|
|
|
reader_push(history_session_id().c_str());
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_set_complete_function(&complete);
|
|
|
|
reader_set_highlight_function(&highlight_shell);
|
|
|
|
reader_set_test_function(&reader_shell_test);
|
|
|
|
reader_set_allow_autosuggesting(true);
|
2013-07-17 15:38:04 +08:00
|
|
|
reader_set_expand_abbreviations(true);
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_import_history_if_necessary();
|
|
|
|
|
|
|
|
parser_t &parser = parser_t::principal_parser();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
data->prev_end_loop = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
while ((!data->end_loop) && (!sanity_check())) {
|
2012-11-19 08:30:30 +08:00
|
|
|
event_fire_generic(L"fish_prompt");
|
2017-06-20 13:57:32 +08:00
|
|
|
|
|
|
|
if (is_breakpoint && function_exists(DEBUG_PROMPT_FUNCTION_NAME)) {
|
|
|
|
reader_set_left_prompt(DEBUG_PROMPT_FUNCTION_NAME);
|
|
|
|
} else if (function_exists(LEFT_PROMPT_FUNCTION_NAME)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_set_left_prompt(LEFT_PROMPT_FUNCTION_NAME);
|
2017-06-20 13:57:32 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_set_left_prompt(DEFAULT_PROMPT);
|
2017-06-20 13:57:32 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2017-06-20 13:57:32 +08:00
|
|
|
if (function_exists(RIGHT_PROMPT_FUNCTION_NAME)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_set_right_prompt(RIGHT_PROMPT_FUNCTION_NAME);
|
2017-06-20 13:57:32 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_set_right_prompt(L"");
|
2017-06-20 13:57:32 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Put buff in temporary string and clear buff, so that we can handle a call to
|
|
|
|
// reader_set_buffer during evaluation.
|
2014-08-19 20:28:08 +08:00
|
|
|
const wchar_t *tmp = reader_readline(0);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->end_loop) {
|
2012-11-19 08:30:30 +08:00
|
|
|
handle_end_loop();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (tmp) {
|
2014-03-30 05:19:45 +08:00
|
|
|
const wcstring command = tmp;
|
|
|
|
update_buff_pos(&data->command_line, 0);
|
2014-01-26 16:41:30 +08:00
|
|
|
data->command_line.text.clear();
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(&data->command_line);
|
2014-07-17 00:33:25 +08:00
|
|
|
wcstring_list_t argv(1, command);
|
|
|
|
event_fire_generic(L"fish_preexec", &argv);
|
2013-04-08 03:40:08 +08:00
|
|
|
reader_run_command(parser, command);
|
2014-07-17 00:33:25 +08:00
|
|
|
event_fire_generic(L"fish_postexec", &argv);
|
2016-05-04 01:36:22 +08:00
|
|
|
// Allow any pending history items to be returned in the history array.
|
|
|
|
if (data->history) {
|
2015-04-20 17:04:17 +08:00
|
|
|
data->history->resolve_pending();
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->end_loop) {
|
2012-11-19 08:30:30 +08:00
|
|
|
handle_end_loop();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
data->prev_end_loop = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 13:57:32 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_pop();
|
|
|
|
return 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Test if there are bytes available for reading on the specified file descriptor.
|
|
|
|
static int can_read(int fd) {
|
|
|
|
struct timeval can_read_timeout = {0, 0};
|
2012-11-19 08:30:30 +08:00
|
|
|
fd_set fds;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
FD_ZERO(&fds);
|
2005-10-25 19:22:47 +08:00
|
|
|
FD_SET(fd, &fds);
|
|
|
|
return select(fd + 1, &fds, 0, 0, &can_read_timeout) == 1;
|
2005-10-13 22:08:33 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Test if the specified character in the specified string is backslashed. pos may be at the end of
|
|
|
|
/// the string, which indicates if there is a trailing backslash.
|
|
|
|
static bool is_backslashed(const wcstring &str, size_t pos) {
|
|
|
|
// note pos == str.size() is OK.
|
|
|
|
if (pos > str.size()) return false;
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2013-02-03 06:50:22 +08:00
|
|
|
size_t count = 0, idx = pos;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (idx--) {
|
|
|
|
if (str.at(idx) != L'\\') break;
|
2012-11-19 08:30:30 +08:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (count % 2) == 1;
|
2006-11-20 10:19:34 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
static wchar_t unescaped_quote(const wcstring &str, size_t pos) {
|
2013-02-03 06:50:22 +08:00
|
|
|
wchar_t result = L'\0';
|
2016-05-04 01:36:22 +08:00
|
|
|
if (pos < str.size()) {
|
2013-02-03 06:50:22 +08:00
|
|
|
wchar_t c = str.at(pos);
|
2016-05-04 01:36:22 +08:00
|
|
|
if ((c == L'\'' || c == L'"') && !is_backslashed(str, pos)) {
|
2013-02-03 06:50:22 +08:00
|
|
|
result = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Returns true if the last token is a comment.
|
|
|
|
static bool text_ends_in_comment(const wcstring &text) {
|
2015-05-03 09:22:20 +08:00
|
|
|
tokenizer_t tok(text.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SHOW_COMMENTS | TOK_SQUASH_ERRORS);
|
2015-07-26 15:12:36 +08:00
|
|
|
tok_t token;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (tok.next(&token)) {
|
2016-11-03 05:22:34 +08:00
|
|
|
; // pass
|
2015-05-03 09:22:20 +08:00
|
|
|
}
|
2015-07-26 15:12:36 +08:00
|
|
|
return token.type == TOK_COMMENT;
|
2015-05-03 09:22:20 +08:00
|
|
|
}
|
2006-11-20 10:19:34 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
const wchar_t *reader_readline(int nchars) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wint_t c;
|
2016-05-04 01:36:22 +08:00
|
|
|
int last_char = 0;
|
|
|
|
size_t yank_len = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *yank_str;
|
|
|
|
bool comp_empty = true;
|
|
|
|
std::vector<completion_t> comp;
|
2016-05-04 01:36:22 +08:00
|
|
|
int finished = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
struct termios old_modes;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Coalesce redundant repaints. When we get a repaint, we set this to true, and skip repaints
|
|
|
|
// until we get something else.
|
2012-11-25 13:06:42 +08:00
|
|
|
bool coalescing_repaints = false;
|
2012-08-22 08:18:52 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// The command line before completion.
|
2014-01-27 16:56:13 +08:00
|
|
|
data->cycle_command_line.clear();
|
|
|
|
data->cycle_cursor_pos = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
data->search_buff.clear();
|
|
|
|
data->search_mode = NO_SEARCH;
|
|
|
|
|
|
|
|
exec_prompt();
|
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2012-11-25 08:42:25 +08:00
|
|
|
s_reset(&data->screen, screen_reset_abandon_line);
|
2012-11-19 08:30:30 +08:00
|
|
|
reader_repaint();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Get the current terminal modes. These will be restored when the function returns.
|
2017-01-11 13:52:10 +08:00
|
|
|
if (tcgetattr(STDIN_FILENO, &old_modes) == -1 && errno == EIO) redirect_tty_output();
|
2016-05-04 01:36:22 +08:00
|
|
|
// Set the new modes.
|
2016-12-15 11:21:36 +08:00
|
|
|
if (tcsetattr(0, TCSANOW, &shell_modes) == -1) {
|
2017-01-11 13:52:10 +08:00
|
|
|
if (errno == EIO) redirect_tty_output();
|
2012-11-19 08:30:30 +08:00
|
|
|
wperror(L"tcsetattr");
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
while (!finished && !data->end_loop) {
|
|
|
|
if (0 < nchars && (size_t)nchars <= data->command_line.size()) {
|
|
|
|
// We've already hit the specified character limit.
|
2014-08-19 20:28:08 +08:00
|
|
|
finished = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Sometimes strange input sequences seem to generate a zero byte. I believe these simply
|
|
|
|
// mean a character was pressed but it should be ignored. (Example: Trying to add a tilde
|
|
|
|
// (~) to digit).
|
|
|
|
while (1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int was_interactive_read = is_interactive_read;
|
|
|
|
is_interactive_read = 1;
|
2016-05-04 01:36:22 +08:00
|
|
|
c = input_readch();
|
2012-11-19 08:30:30 +08:00
|
|
|
is_interactive_read = was_interactive_read;
|
2017-01-03 13:11:53 +08:00
|
|
|
// fwprintf(stderr, L"C: %lx\n", (long)c);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-23 02:21:13 +08:00
|
|
|
if (((!fish_reserved_codepoint(c))) && (c > 31) && (c != 127) && can_read(0)) {
|
|
|
|
wchar_t arr[READAHEAD_MAX + 1];
|
|
|
|
size_t i;
|
|
|
|
size_t limit = 0 < nchars ? std::min((size_t)nchars - data->command_line.size(),
|
|
|
|
(size_t)READAHEAD_MAX)
|
|
|
|
: READAHEAD_MAX;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-23 02:21:13 +08:00
|
|
|
memset(arr, 0, sizeof(arr));
|
|
|
|
arr[0] = c;
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-10-23 02:21:13 +08:00
|
|
|
for (i = 1; i < limit; ++i) {
|
|
|
|
if (!can_read(0)) {
|
|
|
|
c = 0;
|
|
|
|
break;
|
2015-03-24 02:58:57 +08:00
|
|
|
}
|
2016-10-23 02:21:13 +08:00
|
|
|
// Only allow commands on the first key; otherwise, we might have data we
|
|
|
|
// need to insert on the commandline that the commmand might need to be able
|
|
|
|
// to see.
|
|
|
|
c = input_readch(false);
|
|
|
|
if (!fish_reserved_codepoint(c) && c > 31 && c != 127) {
|
|
|
|
arr[i] = c;
|
|
|
|
c = 0;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
editable_line_t *el = data->active_edit_line();
|
|
|
|
insert_string(el, arr, true);
|
|
|
|
|
|
|
|
// End paging upon inserting into the normal command line.
|
|
|
|
if (el == &data->command_line) {
|
|
|
|
clear_pager();
|
2012-08-22 08:18:52 +08:00
|
|
|
}
|
2016-10-23 02:21:13 +08:00
|
|
|
last_char = c;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (c != 0) break;
|
2014-08-19 20:28:08 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (0 < nchars && (size_t)nchars <= data->command_line.size()) {
|
2014-08-19 20:28:08 +08:00
|
|
|
c = R_NULL;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If we get something other than a repaint, then stop coalescing them.
|
|
|
|
if (c != R_REPAINT) coalescing_repaints = false;
|
2012-11-25 13:06:42 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (last_char != R_YANK && last_char != R_YANK_POP) yank_len = 0;
|
2012-12-03 18:25:08 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Restore the text.
|
|
|
|
if (c == R_CANCEL && data->is_navigating_pager_contents()) {
|
|
|
|
set_command_line_and_position(&data->command_line, data->cycle_command_line,
|
|
|
|
data->cycle_cursor_pos);
|
2014-01-27 18:28:19 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Clear the pager if necessary.
|
2014-01-27 16:56:13 +08:00
|
|
|
bool focused_on_search_field = (data->active_edit_line() == &data->pager.search_field_line);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (command_ends_paging(c, focused_on_search_field)) {
|
2014-01-27 16:56:13 +08:00
|
|
|
clear_pager();
|
2014-01-20 15:52:35 +08:00
|
|
|
}
|
2017-01-03 13:11:53 +08:00
|
|
|
// fwprintf(stderr, L"\n\nchar: %ls\n\n", describe_char(c).c_str());
|
2014-03-30 05:19:45 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
switch (c) {
|
|
|
|
// Go to beginning of line.
|
|
|
|
case R_BEGINNING_OF_LINE: {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
while (el->position > 0 && el->text.at(el->position - 1) != L'\n') {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position - 1);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_END_OF_LINE: {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position < el->size()) {
|
2014-01-27 06:47:15 +08:00
|
|
|
const wchar_t *buff = el->text.c_str();
|
2016-05-04 01:36:22 +08:00
|
|
|
while (buff[el->position] && buff[el->position] != L'\n') {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position + 1);
|
2013-07-27 11:35:34 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2013-07-27 11:35:34 +08:00
|
|
|
accept_autosuggestion(true);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BEGINNING_OF_BUFFER: {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(&data->command_line, 0);
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_END_OF_BUFFER: {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(&data->command_line, data->command_line.size());
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_NULL: {
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_CANCEL: {
|
|
|
|
// The only thing we can cancel right now is paging, which we handled up above.
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-12-31 21:53:29 +08:00
|
|
|
case R_FORCE_REPAINT:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_REPAINT: {
|
|
|
|
if (!coalescing_repaints) {
|
2012-11-25 13:06:42 +08:00
|
|
|
coalescing_repaints = true;
|
|
|
|
exec_prompt();
|
2012-11-25 17:26:58 +08:00
|
|
|
s_reset(&data->screen, screen_reset_current_line_and_prompt);
|
2012-12-03 03:53:34 +08:00
|
|
|
data->screen_reset_needed = false;
|
2012-11-25 13:06:42 +08:00
|
|
|
reader_repaint();
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_EOF: {
|
2017-01-25 07:15:38 +08:00
|
|
|
exit_forced = true;
|
2016-05-04 01:36:22 +08:00
|
|
|
data->end_loop = 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
case R_COMPLETE:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_COMPLETE_AND_SEARCH: {
|
|
|
|
if (!data->complete_func) break;
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Use the command line only; it doesn't make sense to complete in any other line.
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = &data->command_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->is_navigating_pager_contents() ||
|
|
|
|
(!comp_empty && last_char == R_COMPLETE)) {
|
|
|
|
// The user typed R_COMPLETE more than once in a row. If we are not yet fully
|
|
|
|
// disclosed, then become so; otherwise cycle through our available completions.
|
|
|
|
if (data->current_page_rendering.remaining_to_disclose > 0) {
|
2014-02-21 05:30:26 +08:00
|
|
|
data->pager.set_fully_disclosed(true);
|
|
|
|
reader_repaint_needed();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
select_completion_in_direction(c == R_COMPLETE ? direction_next
|
|
|
|
: direction_prev);
|
2014-02-21 05:30:26 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Either the user hit tab only once, or we had no visible completion list.
|
|
|
|
// Remove a trailing backslash. This may trigger an extra repaint, but this is
|
|
|
|
// rare.
|
|
|
|
if (is_backslashed(el->text, el->position)) {
|
2013-05-20 16:40:24 +08:00
|
|
|
remove_backward();
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Get the string; we have to do this after removing any trailing backslash.
|
|
|
|
const wchar_t *const buff = el->text.c_str();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Clear the completion list.
|
2012-11-19 16:31:03 +08:00
|
|
|
comp.clear();
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Figure out the extent of the command substitution surrounding the cursor.
|
|
|
|
// This is because we only look at the current command substitution to form
|
|
|
|
// completions - stuff happening outside of it is not interesting.
|
2013-05-20 16:40:24 +08:00
|
|
|
const wchar_t *cmdsub_begin, *cmdsub_end;
|
2014-01-27 06:47:15 +08:00
|
|
|
parse_util_cmdsubst_extent(buff, el->position, &cmdsub_begin, &cmdsub_end);
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Figure out the extent of the token within the command substitution. Note we
|
|
|
|
// pass cmdsub_begin here, not buff.
|
2013-05-20 16:40:24 +08:00
|
|
|
const wchar_t *token_begin, *token_end;
|
2016-05-04 01:36:22 +08:00
|
|
|
parse_util_token_extent(cmdsub_begin, el->position - (cmdsub_begin - buff),
|
|
|
|
&token_begin, &token_end, 0, 0);
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Hack: the token may extend past the end of the command substitution, e.g. in
|
|
|
|
// (echo foo) the last token is 'foo)'. Don't let that happen.
|
2013-10-13 09:17:03 +08:00
|
|
|
if (token_end > cmdsub_end) token_end = cmdsub_end;
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Figure out how many steps to get from the current position to the end of the
|
|
|
|
// current token.
|
2013-02-03 06:50:22 +08:00
|
|
|
size_t end_of_token_offset = token_end - buff;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Move the cursor to the end.
|
|
|
|
if (el->position != end_of_token_offset) {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, end_of_token_offset);
|
2013-02-03 06:50:22 +08:00
|
|
|
reader_repaint();
|
|
|
|
}
|
2013-02-04 03:38:22 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Construct a copy of the string from the beginning of the command substitution
|
|
|
|
// up to the end of the token we're completing.
|
2013-02-03 06:50:22 +08:00
|
|
|
const wcstring buffcpy = wcstring(cmdsub_begin, token_end);
|
2013-03-22 08:44:51 +08:00
|
|
|
|
2017-01-03 13:11:53 +08:00
|
|
|
// fwprintf(stderr, L"Complete (%ls)\n", buffcpy.c_str());
|
2016-05-04 01:36:22 +08:00
|
|
|
complete_flags_t complete_flags = COMPLETION_REQUEST_DEFAULT |
|
|
|
|
COMPLETION_REQUEST_DESCRIPTIONS |
|
|
|
|
COMPLETION_REQUEST_FUZZY_MATCH;
|
2017-08-19 07:52:39 +08:00
|
|
|
data->complete_func(buffcpy, &comp, complete_flags);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Munge our completions.
|
2016-02-07 07:06:33 +08:00
|
|
|
completions_sort_and_prioritize(&comp);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Record our cycle_command_line.
|
2014-01-27 16:56:13 +08:00
|
|
|
data->cycle_command_line = el->text;
|
|
|
|
data->cycle_cursor_pos = el->position;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-11-03 12:54:57 +08:00
|
|
|
bool cont_after_prefix_insertion = (c == R_COMPLETE_AND_SEARCH);
|
|
|
|
comp_empty = handle_completions(comp, cont_after_prefix_insertion);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Show the search field if requested and if we printed a list of completions.
|
|
|
|
if (c == R_COMPLETE_AND_SEARCH && !comp_empty && !data->pager.empty()) {
|
2014-01-27 16:56:13 +08:00
|
|
|
data->pager.set_search_field_shown(true);
|
2014-01-27 18:17:31 +08:00
|
|
|
select_completion_in_direction(direction_next);
|
2014-01-27 16:56:13 +08:00
|
|
|
reader_repaint_needed();
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_KILL_LINE: {
|
2014-01-27 06:55:41 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2014-01-27 06:47:15 +08:00
|
|
|
const wchar_t *buff = el->text.c_str();
|
|
|
|
const wchar_t *begin = &buff[el->position];
|
2012-11-19 16:31:03 +08:00
|
|
|
const wchar_t *end = begin;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
while (*end && *end != L'\n') end++;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (end == begin && *end) end++;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
size_t len = end - begin;
|
|
|
|
if (len) {
|
|
|
|
reader_kill(el, begin - buff, len, KILL_APPEND, last_char != R_KILL_LINE);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_KILL_LINE: {
|
2014-01-27 06:55:41 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-10-31 05:16:32 +08:00
|
|
|
if (el->position <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const wchar_t *buff = el->text.c_str();
|
|
|
|
const wchar_t *end = &buff[el->position];
|
|
|
|
const wchar_t *begin = end;
|
2013-02-20 09:48:51 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
begin--; // make sure we delete at least one character (see issue #580)
|
2013-02-20 09:48:51 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// Delete until we hit a newline, or the beginning of the string.
|
|
|
|
while (begin > buff && *begin != L'\n') begin--;
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2016-10-31 05:16:32 +08:00
|
|
|
// If we landed on a newline, don't delete it.
|
|
|
|
if (*begin == L'\n') begin++;
|
|
|
|
assert(end >= begin);
|
|
|
|
size_t len = maxi<size_t>(end - begin, 1);
|
|
|
|
begin = end - len;
|
|
|
|
reader_kill(el, begin - buff, len, KILL_PREPEND, last_char != R_BACKWARD_KILL_LINE);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_KILL_WHOLE_LINE: {
|
|
|
|
// We match the emacs behavior here: "kills the entire line including the following
|
|
|
|
// newline".
|
2014-01-27 06:55:41 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2014-01-27 06:47:15 +08:00
|
|
|
const wchar_t *buff = el->text.c_str();
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Back up to the character just past the previous newline, or go to the beginning
|
|
|
|
// of the command line. Note that if the position is on a newline, visually this
|
|
|
|
// looks like the cursor is at the end of a line. Therefore that newline is NOT the
|
|
|
|
// beginning of a line; this justifies the -1 check.
|
2014-07-14 05:05:52 +08:00
|
|
|
size_t begin = el->position;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (begin > 0 && buff[begin - 1] != L'\n') {
|
2012-11-19 08:30:30 +08:00
|
|
|
begin--;
|
2014-07-14 05:05:52 +08:00
|
|
|
}
|
2015-09-19 11:47:38 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Push end forwards to just past the next newline, or just past the last char.
|
2014-07-14 05:05:52 +08:00
|
|
|
size_t end = el->position;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (buff[end] != L'\0') {
|
2012-11-19 16:31:03 +08:00
|
|
|
end++;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (buff[end - 1] == L'\n') {
|
2014-07-14 05:05:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(end >= begin);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (end > begin) {
|
|
|
|
reader_kill(el, begin, end - begin, KILL_APPEND,
|
|
|
|
last_char != R_KILL_WHOLE_LINE);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_YANK: {
|
2012-11-19 16:31:03 +08:00
|
|
|
yank_str = kill_yank();
|
2014-01-27 16:56:13 +08:00
|
|
|
insert_string(data->active_edit_line(), yank_str);
|
2012-11-19 08:30:30 +08:00
|
|
|
yank_len = wcslen(yank_str);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_YANK_POP: {
|
|
|
|
if (yank_len) {
|
|
|
|
for (size_t i = 0; i < yank_len; i++) remove_backward();
|
2012-11-19 16:31:03 +08:00
|
|
|
|
|
|
|
yank_str = kill_yank_rotate();
|
2014-01-27 16:56:13 +08:00
|
|
|
insert_string(data->active_edit_line(), yank_str);
|
2012-11-19 16:31:03 +08:00
|
|
|
yank_len = wcslen(yank_str);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
// Escape was pressed.
|
2016-12-24 08:37:00 +08:00
|
|
|
case L'\e': {
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->search_mode) {
|
|
|
|
data->search_mode = NO_SEARCH;
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2016-10-10 05:36:08 +08:00
|
|
|
if (data->token_history_pos == (size_t)-1) {
|
2016-05-04 01:36:22 +08:00
|
|
|
// history_reset();
|
2012-11-19 16:31:03 +08:00
|
|
|
data->history_search.go_to_end();
|
2014-01-13 05:53:59 +08:00
|
|
|
reader_set_buffer(data->search_buff, data->search_buff.size());
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2015-07-20 17:34:57 +08:00
|
|
|
reader_replace_current_token(data->search_buff);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
data->search_buff.clear();
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_DELETE_CHAR: {
|
2012-11-19 08:30:30 +08:00
|
|
|
remove_backward();
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_DELETE_CHAR: {
|
|
|
|
// Remove the current character in the character buffer and on the screen using
|
|
|
|
// syntax highlighting, etc.
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position < el->size()) {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position + 1);
|
2012-11-19 16:31:03 +08:00
|
|
|
remove_backward();
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
// Evaluate. If the current command is unfinished, or if the charater is escaped using a
|
|
|
|
// backslash, insert a newline.
|
|
|
|
case R_EXECUTE: {
|
|
|
|
// If the user hits return while navigating the pager, it only clears the pager.
|
|
|
|
if (data->is_navigating_pager_contents()) {
|
2014-01-28 14:01:38 +08:00
|
|
|
clear_pager();
|
|
|
|
break;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-26 03:23:50 +08:00
|
|
|
// Delete any autosuggestion.
|
|
|
|
data->autosuggestion.clear();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// The user may have hit return with pager contents, but while not navigating them.
|
|
|
|
// Clear the pager in that event.
|
2014-03-17 07:49:31 +08:00
|
|
|
clear_pager();
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// We only execute the command line.
|
2014-01-27 16:56:13 +08:00
|
|
|
editable_line_t *el = &data->command_line;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Allow backslash-escaped newlines, but only if the following character is
|
|
|
|
// whitespace, or we're at the end of the text (see issue #613) and not in a comment
|
|
|
|
// (issue #1255).
|
|
|
|
if (is_backslashed(el->text, el->position)) {
|
2015-05-03 09:22:20 +08:00
|
|
|
bool continue_on_next_line = false;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position >= el->size()) {
|
|
|
|
continue_on_next_line = !text_ends_in_comment(el->text);
|
|
|
|
} else {
|
2015-05-03 09:22:20 +08:00
|
|
|
continue_on_next_line = iswspace(el->text.at(el->position));
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
if (continue_on_next_line) {
|
2014-01-27 16:56:13 +08:00
|
|
|
insert_char(el, '\n');
|
2013-03-13 04:00:37 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// See if this command is valid.
|
2014-01-27 06:47:15 +08:00
|
|
|
int command_test_result = data->test_func(el->text.c_str());
|
2016-05-04 01:36:22 +08:00
|
|
|
if (command_test_result == 0 || command_test_result == PARSER_TEST_INCOMPLETE) {
|
|
|
|
// This command is valid, but an abbreviation may make it invalid. If so, we
|
|
|
|
// will have to test again.
|
2013-08-25 02:16:45 +08:00
|
|
|
bool abbreviation_expanded = data->expand_abbreviation_as_necessary(1);
|
2016-05-04 01:36:22 +08:00
|
|
|
if (abbreviation_expanded) {
|
|
|
|
// It's our reponsibility to rehighlight and repaint. But everything we do
|
|
|
|
// below triggers a repaint.
|
2014-01-27 06:47:15 +08:00
|
|
|
command_test_result = data->test_func(el->text.c_str());
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If the command is OK, then we're going to execute it. We still want to do
|
|
|
|
// syntax highlighting, but a synchronous variant that performs no I/O, so
|
|
|
|
// as not to block the user.
|
2014-03-27 09:49:09 +08:00
|
|
|
bool skip_io = (command_test_result == 0);
|
|
|
|
reader_super_highlight_me_plenty(0, skip_io);
|
2013-07-17 15:38:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-23 13:12:22 +08:00
|
|
|
if (command_test_result == 0) {
|
|
|
|
// Finished command, execute it. Don't add items that start with a leading
|
|
|
|
// space.
|
|
|
|
const editable_line_t *el = &data->command_line;
|
|
|
|
if (data->history != NULL && !el->empty() && el->text.at(0) != L' ') {
|
|
|
|
data->history->add_pending_with_file_detection(el->text);
|
2012-02-07 03:52:24 +08:00
|
|
|
}
|
2016-10-23 13:12:22 +08:00
|
|
|
finished = 1;
|
|
|
|
update_buff_pos(&data->command_line, data->command_line.size());
|
|
|
|
reader_repaint();
|
|
|
|
} else if (command_test_result == PARSER_TEST_INCOMPLETE) {
|
|
|
|
// We are incomplete, continue editing.
|
|
|
|
insert_char(el, '\n');
|
|
|
|
} else {
|
|
|
|
// Result must be some combination including an error. The error message will
|
|
|
|
// already be printed, all we need to do is repaint.
|
|
|
|
s_reset(&data->screen, screen_reset_abandon_line);
|
|
|
|
reader_repaint_needed();
|
2012-02-07 03:59:34 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
case R_HISTORY_SEARCH_BACKWARD:
|
|
|
|
case R_HISTORY_TOKEN_SEARCH_BACKWARD:
|
|
|
|
case R_HISTORY_SEARCH_FORWARD:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_HISTORY_TOKEN_SEARCH_FORWARD: {
|
2012-11-19 16:31:03 +08:00
|
|
|
int reset = 0;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->search_mode == NO_SEARCH) {
|
2012-11-19 16:31:03 +08:00
|
|
|
reset = 1;
|
2016-05-04 01:36:22 +08:00
|
|
|
if ((c == R_HISTORY_SEARCH_BACKWARD) || (c == R_HISTORY_SEARCH_FORWARD)) {
|
2012-11-19 16:31:03 +08:00
|
|
|
data->search_mode = LINE_SEARCH;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-11-19 16:31:03 +08:00
|
|
|
data->search_mode = TOKEN_SEARCH;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
const editable_line_t *el = &data->command_line;
|
|
|
|
data->search_buff.append(el->text);
|
2016-05-04 01:36:22 +08:00
|
|
|
data->history_search = history_search_t(*data->history, data->search_buff,
|
|
|
|
HISTORY_SEARCH_TYPE_CONTAINS);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-07-30 12:24:26 +08:00
|
|
|
// Always skip history entries that exactly match what has been typed so far.
|
|
|
|
wcstring_list_t skip_list;
|
|
|
|
skip_list.push_back(data->command_line.text);
|
2012-11-19 16:31:03 +08:00
|
|
|
const wcstring &suggest = data->autosuggestion;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!suggest.empty() && !data->screen.autosuggestion_is_truncated) {
|
2016-07-30 12:24:26 +08:00
|
|
|
// Also skip the autosuggestion in the history unless it was truncated.
|
|
|
|
skip_list.push_back(suggest);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-07-30 12:24:26 +08:00
|
|
|
data->history_search.skip_matches(skip_list);
|
2012-07-05 18:33:44 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
if (data->search_mode == LINE_SEARCH) {
|
|
|
|
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
|
|
|
|
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
|
|
|
|
data->history_search.go_backwards();
|
|
|
|
} else {
|
|
|
|
if (!data->history_search.go_forwards()) {
|
|
|
|
// If you try to go forwards past the end, we just go to the end.
|
|
|
|
data->history_search.go_to_end();
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-10-23 11:32:25 +08:00
|
|
|
wcstring new_text;
|
|
|
|
if (data->history_search.is_at_end()) {
|
|
|
|
new_text = data->search_buff;
|
|
|
|
} else {
|
|
|
|
new_text = data->history_search.current_string();
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2016-10-23 11:32:25 +08:00
|
|
|
set_command_line_and_position(&data->command_line, new_text, new_text.size());
|
|
|
|
} else if (data->search_mode == TOKEN_SEARCH) {
|
|
|
|
if ((c == R_HISTORY_SEARCH_BACKWARD) ||
|
|
|
|
(c == R_HISTORY_TOKEN_SEARCH_BACKWARD)) {
|
|
|
|
handle_token_history(SEARCH_BACKWARD, reset);
|
|
|
|
} else {
|
|
|
|
handle_token_history(SEARCH_FORWARD, reset);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_CHAR: {
|
2014-01-27 06:55:41 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->is_navigating_pager_contents()) {
|
2014-01-27 18:17:31 +08:00
|
|
|
select_completion_in_direction(direction_west);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (el->position > 0) {
|
|
|
|
update_buff_pos(el, el->position - 1);
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_FORWARD_CHAR: {
|
2014-01-27 06:55:41 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->is_navigating_pager_contents()) {
|
2014-01-27 18:17:31 +08:00
|
|
|
select_completion_in_direction(direction_east);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (el->position < el->size()) {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position + 1);
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2012-12-11 08:23:08 +08:00
|
|
|
accept_autosuggestion(true);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
case R_BACKWARD_KILL_WORD:
|
2012-12-21 09:45:35 +08:00
|
|
|
case R_BACKWARD_KILL_PATH_COMPONENT:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_KILL_BIGWORD: {
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
move_word_style_t style =
|
2016-05-04 01:36:22 +08:00
|
|
|
(c == R_BACKWARD_KILL_BIGWORD
|
|
|
|
? move_word_style_whitespace
|
|
|
|
: c == R_BACKWARD_KILL_PATH_COMPONENT ? move_word_style_path_components
|
|
|
|
: move_word_style_punctuation);
|
|
|
|
bool newv = (last_char != R_BACKWARD_KILL_WORD &&
|
|
|
|
last_char != R_BACKWARD_KILL_PATH_COMPONENT &&
|
|
|
|
last_char != R_BACKWARD_KILL_BIGWORD);
|
2014-01-27 06:47:15 +08:00
|
|
|
move_word(data->active_edit_line(), MOVE_DIR_LEFT, true /* erase */, style, newv);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_KILL_WORD: {
|
|
|
|
move_word(data->active_edit_line(), MOVE_DIR_RIGHT, true /* erase */,
|
|
|
|
move_word_style_punctuation, last_char != R_KILL_WORD);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_KILL_BIGWORD: {
|
|
|
|
move_word(data->active_edit_line(), MOVE_DIR_RIGHT, true /* erase */,
|
|
|
|
move_word_style_whitespace, last_char != R_KILL_BIGWORD);
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_WORD: {
|
|
|
|
move_word(data->active_edit_line(), MOVE_DIR_LEFT, false /* do not erase */,
|
|
|
|
move_word_style_punctuation, false);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_BIGWORD: {
|
|
|
|
move_word(data->active_edit_line(), MOVE_DIR_LEFT, false /* do not erase */,
|
|
|
|
move_word_style_whitespace, false);
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_FORWARD_WORD: {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position < el->size()) {
|
|
|
|
move_word(el, MOVE_DIR_RIGHT, false /* do not erase */,
|
|
|
|
move_word_style_punctuation, false);
|
|
|
|
} else {
|
2012-12-11 08:23:08 +08:00
|
|
|
accept_autosuggestion(false /* accept only one word */);
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_FORWARD_BIGWORD: {
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->position < el->size()) {
|
|
|
|
move_word(el, MOVE_DIR_RIGHT, false /* do not erase */,
|
|
|
|
move_word_style_whitespace, false);
|
|
|
|
} else {
|
Add 'bigword' vi key bindings
- Add four new functions: forward-bigword, backward-bigword,
kill-bigword, backward-kill-bigword
- Add new enum move_word_style_whitespace and related state machine
method
- Change vi key bindings to operate on bigwords: B, gE, W, E, dW, diW,
daW, dE, dB, dgE, cW, ciW, caW, cE, cB, cgE, yW, yiW, yaW, yE, yB,
ygE
2015-05-31 06:44:25 +08:00
|
|
|
accept_autosuggestion(false /* accept only one word */);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BEGINNING_OF_HISTORY: {
|
|
|
|
if (data->is_navigating_pager_contents()) {
|
|
|
|
select_completion_in_direction(direction_page_north);
|
2015-03-03 05:08:29 +08:00
|
|
|
} else {
|
|
|
|
const editable_line_t *el = &data->command_line;
|
2016-05-04 01:36:22 +08:00
|
|
|
data->history_search =
|
|
|
|
history_search_t(*data->history, el->text, HISTORY_SEARCH_TYPE_PREFIX);
|
2015-03-03 05:08:29 +08:00
|
|
|
data->history_search.go_to_beginning();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!data->history_search.is_at_end()) {
|
2015-03-03 05:08:29 +08:00
|
|
|
wcstring new_text = data->history_search.current_string();
|
2016-05-04 01:36:22 +08:00
|
|
|
set_command_line_and_position(&data->command_line, new_text,
|
|
|
|
new_text.size());
|
2015-03-03 05:08:29 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_END_OF_HISTORY: {
|
|
|
|
if (data->is_navigating_pager_contents()) {
|
2015-03-03 05:08:29 +08:00
|
|
|
select_completion_in_direction(direction_page_south);
|
|
|
|
} else {
|
|
|
|
data->history_search.go_to_end();
|
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
case R_UP_LINE:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_DOWN_LINE: {
|
|
|
|
if (data->is_navigating_pager_contents()) {
|
|
|
|
// We are already navigating pager contents.
|
2014-01-25 07:59:18 +08:00
|
|
|
selection_direction_t direction;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (c == R_DOWN_LINE) {
|
|
|
|
// Down arrow is always south.
|
2014-01-25 07:59:18 +08:00
|
|
|
direction = direction_south;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (selection_is_at_top()) {
|
|
|
|
// Up arrow, but we are in the first column and first row. End navigation.
|
2014-01-25 07:59:18 +08:00
|
|
|
direction = direction_deselect;
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Up arrow, go north.
|
2014-01-25 07:59:18 +08:00
|
|
|
direction = direction_north;
|
|
|
|
}
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Now do the selection.
|
2014-01-27 18:17:31 +08:00
|
|
|
select_completion_in_direction(direction);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if (c == R_DOWN_LINE && !data->pager.empty()) {
|
|
|
|
// We pressed down with a non-empty pager contents, begin navigation.
|
2014-01-27 18:17:31 +08:00
|
|
|
select_completion_in_direction(direction_south);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Not navigating the pager contents.
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
|
|
|
int line_old = parse_util_get_line_from_offset(el->text, el->position);
|
2014-01-18 04:04:03 +08:00
|
|
|
int line_new;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-18 04:04:03 +08:00
|
|
|
if (c == R_UP_LINE)
|
2016-05-04 01:36:22 +08:00
|
|
|
line_new = line_old - 1;
|
2014-01-18 04:04:03 +08:00
|
|
|
else
|
2016-05-04 01:36:22 +08:00
|
|
|
line_new = line_old + 1;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
int line_count = parse_util_lineno(el->text.c_str(), el->size()) - 1;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (line_new >= 0 && line_new <= line_count) {
|
2014-01-18 04:04:03 +08:00
|
|
|
size_t base_pos_new;
|
|
|
|
size_t base_pos_old;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-18 04:04:03 +08:00
|
|
|
int indent_old;
|
|
|
|
int indent_new;
|
|
|
|
size_t line_offset_old;
|
|
|
|
size_t total_offset_new;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
base_pos_new = parse_util_get_offset_from_line(el->text, line_new);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
base_pos_old = parse_util_get_offset_from_line(el->text, line_old);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-18 04:04:03 +08:00
|
|
|
assert(base_pos_new != (size_t)(-1) && base_pos_old != (size_t)(-1));
|
|
|
|
indent_old = data->indents.at(base_pos_old);
|
|
|
|
indent_new = data->indents.at(base_pos_new);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
line_offset_old =
|
|
|
|
el->position - parse_util_get_offset_from_line(el->text, line_old);
|
|
|
|
total_offset_new = parse_util_get_offset(
|
|
|
|
el->text, line_new, line_offset_old - 4 * (indent_new - indent_old));
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, total_offset_new);
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2014-01-18 04:04:03 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_SUPPRESS_AUTOSUGGESTION: {
|
2012-11-19 16:31:03 +08:00
|
|
|
data->suppress_autosuggestion = true;
|
|
|
|
data->autosuggestion.clear();
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_ACCEPT_AUTOSUGGESTION: {
|
2012-12-11 08:23:08 +08:00
|
|
|
accept_autosuggestion(true);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_TRANSPOSE_CHARS: {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
if (el->size() < 2) {
|
2013-05-21 03:42:34 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If the cursor is at the end, transpose the last two characters of the line.
|
|
|
|
if (el->position == el->size()) {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, el->position - 1);
|
2013-05-21 03:42:34 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Drag the character before the cursor forward over the character at the cursor,
|
|
|
|
// moving the cursor forward as well.
|
|
|
|
if (el->position > 0) {
|
2014-01-27 06:47:15 +08:00
|
|
|
wcstring local_cmd = el->text;
|
2016-05-04 01:36:22 +08:00
|
|
|
std::swap(local_cmd.at(el->position), local_cmd.at(el->position - 1));
|
2014-01-27 06:47:15 +08:00
|
|
|
set_command_line_and_position(el, local_cmd, el->position + 1);
|
2013-05-21 03:42:34 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_TRANSPOSE_WORDS: {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
|
|
|
size_t len = el->size();
|
|
|
|
const wchar_t *buff = el->text.c_str();
|
2013-05-28 05:25:55 +08:00
|
|
|
const wchar_t *tok_begin, *tok_end, *prev_begin, *prev_end;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If we are not in a token, look for one ahead.
|
2014-03-30 05:19:45 +08:00
|
|
|
size_t buff_pos = el->position;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (buff_pos != len && !iswalnum(buff[buff_pos])) buff_pos++;
|
2014-01-15 22:07:22 +08:00
|
|
|
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, buff_pos);
|
2013-05-28 05:25:55 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
parse_util_token_extent(buff, el->position, &tok_begin, &tok_end, &prev_begin,
|
|
|
|
&prev_end);
|
2013-05-28 05:25:55 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// In case we didn't find a token at or after the cursor...
|
|
|
|
if (tok_begin == &buff[len]) {
|
|
|
|
// ...retry beginning from the previous token.
|
2013-05-28 05:25:55 +08:00
|
|
|
size_t pos = prev_end - &buff[0];
|
2016-05-04 01:36:22 +08:00
|
|
|
parse_util_token_extent(buff, pos, &tok_begin, &tok_end, &prev_begin,
|
|
|
|
&prev_end);
|
2013-05-28 05:25:55 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Make sure we have two tokens.
|
|
|
|
if (prev_begin < prev_end && tok_begin < tok_end && tok_begin > prev_begin) {
|
2013-06-02 16:14:05 +08:00
|
|
|
const wcstring prev(prev_begin, prev_end - prev_begin);
|
|
|
|
const wcstring sep(prev_end, tok_begin - prev_end);
|
|
|
|
const wcstring tok(tok_begin, tok_end - tok_begin);
|
|
|
|
const wcstring trail(tok_end, &buff[len] - tok_end);
|
2013-05-28 05:25:55 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Compose new command line with swapped tokens.
|
2013-05-28 05:25:55 +08:00
|
|
|
wcstring new_buff(buff, prev_begin - buff);
|
|
|
|
new_buff.append(tok);
|
|
|
|
new_buff.append(sep);
|
|
|
|
new_buff.append(prev);
|
|
|
|
new_buff.append(trail);
|
2016-05-04 01:36:22 +08:00
|
|
|
// Put cursor right after the second token.
|
2014-01-27 06:47:15 +08:00
|
|
|
set_command_line_and_position(el, new_buff, tok_end - buff);
|
2013-05-28 05:25:55 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-09-10 00:46:16 +08:00
|
|
|
case R_UPCASE_WORD:
|
|
|
|
case R_DOWNCASE_WORD:
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_CAPITALIZE_WORD: {
|
2014-01-27 06:47:15 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2016-05-04 01:36:22 +08:00
|
|
|
// For capitalize_word, whether we've capitalized a character so far.
|
2013-09-10 00:46:16 +08:00
|
|
|
bool capitalized_first = false;
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// We apply the operation from the current location to the end of the word.
|
2014-01-27 06:47:15 +08:00
|
|
|
size_t pos = el->position;
|
|
|
|
move_word(el, MOVE_DIR_RIGHT, false, move_word_style_punctuation, false);
|
2016-05-04 01:36:22 +08:00
|
|
|
for (; pos < el->position; pos++) {
|
2014-01-27 06:47:15 +08:00
|
|
|
wchar_t chr = el->text.at(pos);
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// We always change the case; this decides whether we go uppercase (true) or
|
|
|
|
// lowercase (false).
|
2013-09-11 11:46:46 +08:00
|
|
|
bool make_uppercase;
|
|
|
|
if (c == R_CAPITALIZE_WORD)
|
2016-05-04 01:36:22 +08:00
|
|
|
make_uppercase = !capitalized_first && iswalnum(chr);
|
2013-09-11 11:46:46 +08:00
|
|
|
else
|
|
|
|
make_uppercase = (c == R_UPCASE_WORD);
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Apply the operation and then record what we did.
|
2013-09-11 11:46:46 +08:00
|
|
|
if (make_uppercase)
|
|
|
|
chr = towupper(chr);
|
|
|
|
else
|
|
|
|
chr = towlower(chr);
|
2013-10-27 06:27:39 +08:00
|
|
|
|
2014-01-26 16:41:30 +08:00
|
|
|
data->command_line.text.at(pos) = chr;
|
2013-09-11 11:46:46 +08:00
|
|
|
capitalized_first = capitalized_first || make_uppercase;
|
2013-09-10 00:46:16 +08:00
|
|
|
}
|
2014-01-27 16:56:13 +08:00
|
|
|
data->command_line_changed(el);
|
2014-01-27 06:47:15 +08:00
|
|
|
reader_super_highlight_me_plenty();
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_needed();
|
2013-09-10 00:46:16 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BEGIN_SELECTION: {
|
2014-04-01 01:01:39 +08:00
|
|
|
data->sel_active = true;
|
|
|
|
data->sel_begin_pos = data->command_line.position;
|
|
|
|
data->sel_start_pos = data->command_line.position;
|
|
|
|
data->sel_stop_pos = data->command_line.position;
|
|
|
|
break;
|
2014-01-15 22:07:22 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_SWAP_SELECTION_START_STOP: {
|
2016-03-20 09:08:23 +08:00
|
|
|
if (!data->sel_active) break;
|
|
|
|
size_t tmp = data->sel_begin_pos;
|
|
|
|
data->sel_begin_pos = data->command_line.position;
|
|
|
|
data->sel_start_pos = data->command_line.position;
|
|
|
|
editable_line_t *el = data->active_edit_line();
|
|
|
|
update_buff_pos(el, tmp);
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_END_SELECTION: {
|
2014-04-01 01:01:39 +08:00
|
|
|
data->sel_active = false;
|
|
|
|
data->sel_start_pos = data->command_line.position;
|
|
|
|
data->sel_stop_pos = data->command_line.position;
|
|
|
|
break;
|
2014-01-15 22:07:22 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_KILL_SELECTION: {
|
2014-01-18 17:18:29 +08:00
|
|
|
bool newv = (last_char != R_KILL_SELECTION);
|
|
|
|
size_t start, len;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (reader_get_selection(&start, &len)) {
|
2014-03-30 05:19:45 +08:00
|
|
|
reader_kill(&data->command_line, start, len, KILL_APPEND, newv);
|
2014-01-18 17:18:29 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_FORWARD_JUMP: {
|
2014-03-30 05:19:45 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2014-01-22 17:00:44 +08:00
|
|
|
wchar_t target = input_function_pop_arg();
|
2014-01-23 17:23:04 +08:00
|
|
|
bool status = false;
|
2014-01-22 17:00:44 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
for (size_t i = el->position + 1; i < el->size(); i++) {
|
|
|
|
if (el->at(i) == target) {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, i);
|
2014-01-23 17:23:04 +08:00
|
|
|
status = true;
|
2014-01-22 17:00:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 17:23:04 +08:00
|
|
|
input_function_set_status(status);
|
2014-03-30 05:19:45 +08:00
|
|
|
reader_repaint_needed();
|
2014-01-22 17:00:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
case R_BACKWARD_JUMP: {
|
2014-03-30 05:19:45 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2014-01-22 17:00:44 +08:00
|
|
|
wchar_t target = input_function_pop_arg();
|
2014-01-23 17:23:04 +08:00
|
|
|
bool status = false;
|
2014-01-22 17:00:44 +08:00
|
|
|
|
2014-03-30 05:19:45 +08:00
|
|
|
size_t tmp_pos = el->position;
|
2016-05-04 01:36:22 +08:00
|
|
|
while (tmp_pos--) {
|
|
|
|
if (el->at(tmp_pos) == target) {
|
2014-03-30 05:19:45 +08:00
|
|
|
update_buff_pos(el, tmp_pos);
|
2014-01-23 17:23:04 +08:00
|
|
|
status = true;
|
2014-01-22 17:00:44 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-23 17:23:04 +08:00
|
|
|
input_function_set_status(status);
|
2014-03-30 05:19:45 +08:00
|
|
|
reader_repaint_needed();
|
2014-01-22 17:00:44 +08:00
|
|
|
break;
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
default: {
|
|
|
|
// Other, if a normal character, we add it to the command.
|
2016-10-18 07:23:29 +08:00
|
|
|
if (!fish_reserved_codepoint(c) && (c >= L' ' || c == L'\n' || c == L'\r') &&
|
|
|
|
c != 0x7F) {
|
2014-07-26 03:23:42 +08:00
|
|
|
bool allow_expand_abbreviations = false;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (data->is_navigating_pager_contents()) {
|
2014-01-27 16:56:13 +08:00
|
|
|
data->pager.set_search_field_shown(true);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2014-07-26 03:23:42 +08:00
|
|
|
allow_expand_abbreviations = true;
|
2014-01-26 16:41:30 +08:00
|
|
|
}
|
2013-07-17 17:55:15 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Regular character.
|
2014-01-28 23:30:05 +08:00
|
|
|
editable_line_t *el = data->active_edit_line();
|
2014-07-26 03:23:42 +08:00
|
|
|
insert_char(data->active_edit_line(), c, allow_expand_abbreviations);
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// End paging upon inserting into the normal command line.
|
|
|
|
if (el == &data->command_line) {
|
2014-01-28 23:30:05 +08:00
|
|
|
clear_pager();
|
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2016-07-12 11:31:30 +08:00
|
|
|
// This can happen if the user presses a control char we don't recognize. No
|
|
|
|
// reason to report this to the user unless they've enabled debugging output.
|
|
|
|
debug(2, _(L"Unknown key binding 0x%X"), c);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if ((c != R_HISTORY_SEARCH_BACKWARD) && (c != R_HISTORY_SEARCH_FORWARD) &&
|
|
|
|
(c != R_HISTORY_TOKEN_SEARCH_BACKWARD) && (c != R_HISTORY_TOKEN_SEARCH_FORWARD) &&
|
|
|
|
(c != R_NULL) && (c != R_REPAINT) && (c != R_FORCE_REPAINT)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
data->search_mode = NO_SEARCH;
|
|
|
|
data->search_buff.clear();
|
|
|
|
data->history_search.go_to_end();
|
2016-05-04 01:36:22 +08:00
|
|
|
data->token_history_pos = -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
last_char = c;
|
2014-04-01 01:01:39 +08:00
|
|
|
|
2014-01-22 08:25:55 +08:00
|
|
|
reader_repaint_if_needed();
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
2017-08-12 22:54:26 +08:00
|
|
|
ignore_result(write(STDOUT_FILENO, "\n", 1));
|
2013-07-17 17:55:15 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Ensure we have no pager contents when we exit.
|
|
|
|
if (!data->pager.empty()) {
|
|
|
|
// Clear to end of screen to erase the pager contents.
|
|
|
|
// TODO: this may fail if eos doesn't exist, in which case we should emit newlines.
|
2014-01-16 10:21:38 +08:00
|
|
|
screen_force_clear_to_end();
|
2014-01-20 15:52:35 +08:00
|
|
|
data->pager.clear();
|
2014-01-16 10:21:38 +08:00
|
|
|
}
|
2013-07-17 17:55:15 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!reader_exit_forced()) {
|
2016-12-15 11:21:36 +08:00
|
|
|
if (tcsetattr(0, TCSANOW, &old_modes) == -1) {
|
2017-01-11 13:52:10 +08:00
|
|
|
if (errno == EIO) redirect_tty_output();
|
2016-05-04 01:36:22 +08:00
|
|
|
wperror(L"tcsetattr"); // return to previous mode
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
set_color(rgb_color_t::reset(), rgb_color_t::reset());
|
|
|
|
}
|
|
|
|
|
2014-01-27 06:47:15 +08:00
|
|
|
return finished ? data->command_line.text.c_str() : NULL;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
int reader_search_mode() {
|
|
|
|
if (!data) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2016-10-24 04:58:12 +08:00
|
|
|
return data->search_mode == NO_SEARCH ? 0 : 1;
|
2007-09-21 22:05:49 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
int reader_has_pager_contents() {
|
|
|
|
if (!data) {
|
2014-01-18 04:53:01 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
return !data->current_page_rendering.screen_data.empty();
|
2007-09-21 22:05:49 +08:00
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
/// Read non-interactively. Read input from stdin without displaying the prompt, using syntax
|
|
|
|
/// highlighting. This is used for reading scripts and init files.
|
|
|
|
static int read_ni(int fd, const io_chain_t &io) {
|
2012-01-23 13:40:08 +08:00
|
|
|
parser_t &parser = parser_t::principal_parser();
|
2012-11-19 08:30:30 +08:00
|
|
|
FILE *in_stream;
|
2016-05-04 01:36:22 +08:00
|
|
|
wchar_t *buff = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
std::vector<char> acc;
|
|
|
|
|
2013-04-08 03:40:08 +08:00
|
|
|
int des = (fd == STDIN_FILENO ? dup(STDIN_FILENO) : fd);
|
2016-05-04 01:36:22 +08:00
|
|
|
int res = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (des == -1) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wperror(L"dup");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_stream = fdopen(des, "r");
|
2016-05-04 01:36:22 +08:00
|
|
|
if (in_stream != 0) {
|
|
|
|
while (!feof(in_stream)) {
|
2012-11-19 08:30:30 +08:00
|
|
|
char buff[4096];
|
|
|
|
size_t c = fread(buff, 1, 4096, in_stream);
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (ferror(in_stream)) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
// We got a signal, just keep going. Be sure that we call insert() below because
|
|
|
|
// we may get data as well as EINTR.
|
2013-04-08 05:52:16 +08:00
|
|
|
clearerr(in_stream);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else if ((errno == EAGAIN || errno == EWOULDBLOCK) &&
|
|
|
|
make_fd_blocking(des) == 0) {
|
|
|
|
// We succeeded in making the fd blocking, keep going.
|
2013-04-08 05:52:16 +08:00
|
|
|
clearerr(in_stream);
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
// Fatal error.
|
2013-04-08 05:52:16 +08:00
|
|
|
debug(1, _(L"Error while reading from file descriptor"));
|
2016-05-04 01:36:22 +08:00
|
|
|
// Reset buffer on error. We won't evaluate incomplete files.
|
2013-04-08 05:52:16 +08:00
|
|
|
acc.clear();
|
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
acc.insert(acc.end(), buff, buff + c);
|
|
|
|
}
|
2012-12-20 05:31:06 +08:00
|
|
|
|
2014-11-02 07:25:28 +08:00
|
|
|
wcstring str = acc.empty() ? wcstring() : str2wcstring(&acc.at(0), acc.size());
|
2012-03-04 14:48:21 +08:00
|
|
|
acc.clear();
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
if (fclose(in_stream)) {
|
|
|
|
debug(1, _(L"Error while closing input stream"));
|
2012-11-19 08:30:30 +08:00
|
|
|
wperror(L"fclose");
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// Swallow a BOM (issue #1518).
|
|
|
|
if (!str.empty() && str.at(0) == UTF8_BOM_WCHAR) {
|
2014-11-02 07:25:28 +08:00
|
|
|
str.erase(0, 1);
|
|
|
|
}
|
|
|
|
|
2013-12-13 10:18:07 +08:00
|
|
|
parse_error_list_t errors;
|
2016-02-28 16:44:20 +08:00
|
|
|
parse_node_tree_t tree;
|
2016-05-04 01:36:22 +08:00
|
|
|
if (!parse_util_detect_errors(str, &errors, false /* do not accept incomplete */, &tree)) {
|
2017-01-27 07:36:12 +08:00
|
|
|
parser.eval(str, io, TOP, std::move(tree));
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
2013-12-16 08:05:37 +08:00
|
|
|
wcstring sb;
|
2017-06-18 13:36:56 +08:00
|
|
|
parser.get_backtrace(str, errors, sb);
|
2012-12-20 05:31:06 +08:00
|
|
|
fwprintf(stderr, L"%ls", sb.c_str());
|
|
|
|
res = 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2016-05-04 01:36:22 +08:00
|
|
|
} else {
|
|
|
|
debug(1, _(L"Error while opening input stream"));
|
2012-11-19 08:30:30 +08:00
|
|
|
wperror(L"fdopen");
|
|
|
|
free(buff);
|
2016-05-04 01:36:22 +08:00
|
|
|
res = 1;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
int reader_read(int fd, const io_chain_t &io) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int res;
|
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If reader_read is called recursively through the '.' builtin, we need to preserve
|
|
|
|
// is_interactive. This, and signal handler setup is handled by
|
|
|
|
// proc_push_interactive/proc_pop_interactive.
|
2017-01-11 13:52:10 +08:00
|
|
|
int inter = 0;
|
|
|
|
// This block is a hack to work around https://sourceware.org/bugzilla/show_bug.cgi?id=20632.
|
|
|
|
// See also, commit 396bf12. Without the need for this workaround we would just write:
|
|
|
|
// int inter = ((fd == STDIN_FILENO) && isatty(STDIN_FILENO));
|
|
|
|
if (fd == STDIN_FILENO) {
|
|
|
|
struct termios t;
|
|
|
|
int a_tty = isatty(STDIN_FILENO);
|
|
|
|
if (a_tty) {
|
|
|
|
inter = 1;
|
|
|
|
} else if (tcgetattr(STDIN_FILENO, &t) == -1 && errno == EIO) {
|
|
|
|
redirect_tty_output();
|
|
|
|
inter = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
proc_push_interactive(inter);
|
|
|
|
|
2016-05-15 11:35:54 +08:00
|
|
|
res = shell_is_interactive() ? read_i() : read_ni(fd, io);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-04 01:36:22 +08:00
|
|
|
// If the exit command was called in a script, only exit the script, not the program.
|
|
|
|
if (data) data->end_loop = 0;
|
2012-11-19 08:30:30 +08:00
|
|
|
end_loop = 0;
|
|
|
|
|
|
|
|
proc_pop_interactive();
|
|
|
|
return res;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|