2005-09-20 21:26:39 +08:00
/** \file reader.c
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
2012-12-16 12:23:24 +08:00
backwards searches are also handled by consulting the list up until
2005-09-20 21:26:39 +08:00
the end of the list is reached , at which point regular searching will
commence .
*/
# include "config.h"
2012-01-17 00:56:47 +08:00
# include <algorithm>
2005-09-20 21:26:39 +08:00
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <errno.h>
# include <termios.h>
# include <sys/types.h>
# include <sys/stat.h>
2006-08-10 06:53:38 +08:00
# ifdef HAVE_SYS_IOCTL_H
2005-09-20 21:26:39 +08:00
# include <sys/ioctl.h>
2006-08-10 06:53:38 +08:00
# endif
2006-12-01 07:57:49 +08:00
# include <time.h>
2005-09-20 21:26:39 +08:00
# include <sys/time.h>
# include <sys/wait.h>
2005-10-13 22:08:33 +08:00
# include <sys/poll.h>
2005-09-20 21:26:39 +08:00
# include <unistd.h>
# include <wctype.h>
2012-02-03 07:05:08 +08:00
# include <stack>
2013-01-24 21:29:13 +08:00
# include <pthread.h>
2005-09-20 21:26:39 +08:00
# if HAVE_NCURSES_H
# include <ncurses.h>
# else
# include <curses.h>
# endif
2006-01-19 20:22:07 +08:00
# if HAVE_TERM_H
2005-09-20 21:26:39 +08:00
# include <term.h>
2006-01-19 20:22:07 +08:00
# elif HAVE_NCURSES_TERM_H
# include <ncurses/term.h>
# endif
2006-07-31 04:26:59 +08:00
# ifdef HAVE_SIGINFO_H
# include <siginfo.h>
# endif
2006-08-10 06:26:05 +08:00
# ifdef HAVE_SYS_SELECT_H
# include <sys/select.h>
# endif
2005-09-20 21:26:39 +08:00
# include <signal.h>
# include <fcntl.h>
# include <dirent.h>
# include <wchar.h>
2005-12-26 06:00:44 +08:00
# include <assert.h>
2006-02-28 21:17:16 +08:00
# include "fallback.h"
2005-09-20 21:26:39 +08:00
# include "util.h"
2006-02-28 21:17:16 +08:00
2005-09-20 21:26:39 +08:00
# include "wutil.h"
# include "highlight.h"
# include "reader.h"
# include "proc.h"
# include "parser.h"
# include "complete.h"
# include "history.h"
# include "common.h"
# include "sanity.h"
# include "env.h"
# include "exec.h"
# include "expand.h"
# include "tokenizer.h"
# include "kill.h"
# include "input_common.h"
# include "input.h"
# include "function.h"
# include "output.h"
2005-10-15 08:51:26 +08:00
# include "signal.h"
2006-10-02 00:02:58 +08:00
# include "screen.h"
2011-12-27 11:18:46 +08:00
# include "iothread.h"
2012-02-03 07:05:08 +08:00
# include "intern.h"
2012-02-19 10:54:36 +08:00
# include "path.h"
2006-01-31 00:51:50 +08:00
# include "parse_util.h"
2013-07-17 17:55:15 +08:00
# include "parser_keywords.h"
2013-10-09 17:03:50 +08:00
# include "parse_tree.h"
2014-01-15 07:39:53 +08:00
# include "pager.h"
2005-09-20 21:26:39 +08:00
/**
2005-12-31 00:29:19 +08:00
Maximum length of prefix string when printing completion
list . Longer prefixes will be ellipsized .
2005-09-20 21:26:39 +08:00
*/
2012-11-10 08:02:19 +08:00
# define PREFIX_MAX_LEN 9
2005-09-20 21:26:39 +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
2008-01-14 00:47:47 +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"
/**
The name of the function that prints the fish right prompt ( RPROMPT )
*/
# define RIGHT_PROMPT_FUNCTION_NAME L"fish_right_prompt"
2007-01-16 01:51:44 +08:00
2005-09-20 21:26:39 +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
2005-10-13 22:08:33 +08:00
/**
The maximum number of characters to read from the keyboard without
2005-10-26 18:51:02 +08:00
repainting . Note that this readahead will only occur if new
2012-12-16 12:23:24 +08:00
characters are available for reading , fish will never block for
2005-10-13 22:08:33 +08:00
more input without repainting .
*/
# define READAHEAD_MAX 256
2008-01-14 00:47:47 +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
2008-01-14 00:47:47 +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
2008-01-14 00:47:47 +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
2008-01-14 00:47:47 +08:00
/**
2012-12-16 12:23:24 +08:00
History search mode . This value means that we are performing a line
2008-01-14 00:47:47 +08:00
history search .
*/
2007-04-17 05:26:15 +08:00
# define LINE_SEARCH 1
2008-01-14 00:47:47 +08:00
/**
2012-12-16 12:23:24 +08:00
History search mode . This value means that we are performing a token
2008-01-14 00:47:47 +08:00
history search .
*/
2007-04-17 05:26:15 +08:00
# define TOKEN_SEARCH 2
2008-01-14 00:47:47 +08:00
/**
History search mode . This value means we are searching backwards .
*/
2007-04-17 05:26:15 +08:00
# define SEARCH_BACKWARD 0
2008-01-14 00:47:47 +08:00
/**
History search mode . This value means we are searching forwards .
*/
2007-04-17 05:26:15 +08:00
# define SEARCH_FORWARD 1
2013-02-06 04:18:22 +08:00
/* Any time the contents of a buffer changes, we update the generation count. This allows for our background highlighting thread to notice it and skip doing work that it would otherwise have to do. This variable should really be of some kind of interlocked or atomic type that guarantees we're not reading stale cache values. With C++11 we should use atomics, but until then volatile should work as well, at least on x86.*/
2013-01-23 07:19:29 +08:00
static volatile unsigned int s_generation_count ;
2013-01-24 21:29:13 +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. */
static pthread_key_t generation_count_key ;
2012-02-29 04:40:20 +08:00
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 ) ;
2014-07-26 03:23:42 +08:00
void editable_line_t : : insert_string ( const wcstring & str , size_t start , size_t len )
2014-01-27 06:47:15 +08:00
{
2014-07-26 03:23:42 +08:00
// Clamp the range to something valid
size_t string_length = str . size ( ) ;
start = mini ( start , string_length ) ;
len = mini ( len , string_length - start ) ;
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
2005-09-20 21:26:39 +08:00
/**
A struct describing the state of the interactive reader . These
2007-01-16 01:53:46 +08:00
states can be stacked , in case reader_readline ( ) calls are
nested . This happens when the ' read ' builtin is used .
2005-09-20 21:26:39 +08:00
*/
2011-12-28 10:41:38 +08:00
class reader_data_t
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
public :
/** String containing the whole current commandline */
2014-01-26 16:41:30 +08:00
editable_line_t command_line ;
2012-11-19 08:30:30 +08:00
2012-02-07 02:52:13 +08:00
/** String containing the autosuggestion */
wcstring autosuggestion ;
2005-09-20 21:26:39 +08:00
2014-01-16 10:21:38 +08:00
/** Current pager */
2014-01-20 15:52:35 +08:00
pager_t pager ;
2014-04-01 01:01:39 +08:00
2014-01-18 04:04:03 +08:00
/** Current page rendering */
page_rendering_t current_page_rendering ;
2014-04-01 01:01:39 +08:00
2012-11-18 18:43:35 +08:00
/** Whether autosuggesting is allowed at all */
bool allow_autosuggestion ;
/** When backspacing, we temporarily suppress autosuggestions */
2012-02-07 11:31:06 +08:00
bool suppress_autosuggestion ;
2013-07-17 15:38:04 +08:00
/** Whether abbreviations are expanded */
bool expand_abbreviations ;
2012-11-19 08:30:30 +08:00
/** The representation of the current screen contents */
screen_t screen ;
2012-02-06 08:42:24 +08:00
/** The history */
history_t * history ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/**
String containing the current search item
*/
wcstring search_buff ;
2006-10-01 23:59:18 +08:00
2012-02-06 08:42:24 +08:00
/* History search */
history_search_t history_search ;
2012-11-19 08:30:30 +08:00
/**
Saved position used by token history search
*/
int token_history_pos ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/**
Saved search string for token history search . Not handled by command_line_changed .
*/
wcstring token_history_buff ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/**
List for storing previous search results . Used to avoid duplicates .
*/
wcstring_list_t search_prev ;
/** The current position in search_prev */
size_t search_pos ;
2005-09-20 21:26:39 +08:00
2014-01-27 16:56:13 +08:00
bool is_navigating_pager_contents ( ) const
{
return this - > pager . is_navigating_contents ( ) ;
}
2014-04-01 01:01:39 +08:00
2014-01-27 06:47:15 +08:00
/* The line that is currently being edited. Typically the command line, but may be the search field */
editable_line_t * active_edit_line ( )
2012-11-19 08:30:30 +08:00
{
2014-01-27 16:56:13 +08:00
if ( this - > is_navigating_pager_contents ( ) & & this - > pager . is_search_field_shown ( ) )
{
return & this - > pager . search_field_line ;
}
else
{
return & this - > command_line ;
}
2012-11-19 08:30:30 +08:00
}
2012-02-29 04:40:20 +08:00
/** Do what we need to do whenever our command line changes */
2014-01-27 16:56:13 +08:00
void command_line_changed ( const editable_line_t * el ) ;
2005-09-20 21:26:39 +08:00
2014-02-17 11:56:13 +08:00
/** Do what we need to do whenever our pager selection */
void pager_selection_changed ( ) ;
2005-09-20 21:26:39 +08:00
2013-08-25 02:16:45 +08:00
/** Expand abbreviations at the current cursor position, minus backtrack_amt. */
bool expand_abbreviation_as_necessary ( size_t cursor_backtrack ) ;
2013-07-17 15:38:04 +08:00
2014-01-15 18:39:19 +08:00
/** Indicates whether a selection is currently active */
bool sel_active ;
2014-01-15 22:07:22 +08:00
/** The position of the cursor, when selection was initiated. */
size_t sel_begin_pos ;
2014-01-15 18:39:19 +08:00
/** The start position of the current selection, if one. */
size_t sel_start_pos ;
/** The stop position of the current selection, if one. */
size_t sel_stop_pos ;
2012-11-19 08:30:30 +08:00
/** Name of the current application */
wcstring app_name ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** The prompt commands */
wcstring left_prompt ;
2012-11-08 11:59:20 +08:00
wcstring right_prompt ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** The output of the last evaluation of the prompt command */
wcstring left_prompt_buff ;
/** The output of the last evaluation of the right prompt command */
wcstring right_prompt_buff ;
2014-01-27 16:56:13 +08:00
/* Completion support */
wcstring cycle_command_line ;
size_t cycle_cursor_pos ;
2012-11-19 08:30:30 +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 ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/** An array defining the block level at each character. */
std : : vector < int > indents ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/**
Function for tab completion
*/
2012-02-25 04:13:35 +08:00
complete_function_t complete_func ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
/**
Function for syntax highlighting
*/
highlight_function_t highlight_function ;
/**
Function for testing if the string can be returned
*/
int ( * test_func ) ( const wchar_t * ) ;
/**
When this is true , the reader will exit
*/
bool end_loop ;
/**
If this is true , exit reader even if there are running
jobs . This happens if we press e . g . ^ D twice .
*/
bool prev_end_loop ;
/** The current contents of the top item in the kill ring. */
wcstring kill_item ;
/**
Pointer to previous reader_data
*/
reader_data_t * next ;
/**
This variable keeps state on if we are in search mode , and
if yes , what mode
*/
int search_mode ;
/**
Keep track of whether any internal code has done something
which is known to require a repaint .
*/
bool repaint_needed ;
2012-12-16 12:23:24 +08:00
/** Whether a screen reset is needed after a repaint. */
2012-03-25 18:00:38 +08:00
bool screen_reset_needed ;
2012-11-19 08:30:30 +08:00
2013-01-21 05:23:27 +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
2012-07-18 03:47:01 +08:00
/** Constructor */
reader_data_t ( ) :
2012-11-18 18:43:35 +08:00
allow_autosuggestion ( 0 ) ,
2012-07-18 03:47:01 +08:00
suppress_autosuggestion ( 0 ) ,
2013-07-17 15:38:04 +08:00
expand_abbreviations ( 0 ) ,
2012-07-18 03:47:01 +08:00
history ( 0 ) ,
token_history_pos ( 0 ) ,
search_pos ( 0 ) ,
2014-01-15 18:39:19 +08:00
sel_active ( 0 ) ,
2014-01-15 22:07:22 +08:00
sel_begin_pos ( 0 ) ,
2014-01-15 18:39:19 +08:00
sel_start_pos ( 0 ) ,
sel_stop_pos ( 0 ) ,
2014-01-27 16:56:13 +08:00
cycle_cursor_pos ( 0 ) ,
2012-07-18 03:47:01 +08:00
complete_func ( 0 ) ,
highlight_function ( 0 ) ,
test_func ( 0 ) ,
end_loop ( 0 ) ,
prev_end_loop ( 0 ) ,
next ( 0 ) ,
search_mode ( 0 ) ,
repaint_needed ( 0 ) ,
2013-01-21 05:23:27 +08:00
screen_reset_needed ( 0 ) ,
2013-01-22 18:25:17 +08:00
exit_on_interrupt ( 0 )
2012-11-19 08:30:30 +08:00
{
}
2011-12-28 10:41:38 +08:00
} ;
2005-09-20 21:26:39 +08:00
2014-01-21 04:38:56 +08:00
/* Sets the command line contents, without clearing the pager */
static void reader_set_buffer_maintaining_pager ( const wcstring & b , size_t pos ) ;
2014-08-12 01:17:41 +08:00
/* Clears the pager */
static void clear_pager ( ) ;
2005-09-20 21:26:39 +08:00
/**
The current interactive reading context
*/
static reader_data_t * data = 0 ;
2007-02-01 07:58:10 +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 .
*/
static int is_interactive_read ;
2005-09-20 21:26:39 +08:00
/**
2005-12-31 00:29:19 +08:00
Flag for ending non - interactive shell
2005-09-20 21:26:39 +08:00
*/
static int end_loop = 0 ;
2012-03-04 07:20:30 +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
/**
Store the pid of the parent process , so the exit function knows whether it should reset the terminal or not .
*/
static pid_t original_pid ;
2006-01-24 04:40:14 +08:00
/**
This variable is set to true by the signal handler when ^ C is pressed
*/
2013-01-21 05:23:27 +08:00
static volatile int interrupted = 0 ;
2005-09-20 21:26:39 +08:00
2006-01-24 04:40:14 +08:00
2005-09-20 21:26:39 +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 ) ;
2013-09-23 09:11:53 +08:00
/** Mode on startup, which we restore on exit */
static struct termios terminal_mode_on_startup ;
/** Mode we use to execute programs */
static struct termios terminal_mode_for_executing_programs ;
2006-03-10 21:38:09 +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
2006-05-14 18:16:23 +08:00
/**
Variable to keep track of forced exits - see \ c reader_exit_forced ( ) ;
*/
static int exit_forced ;
2005-09-20 21:26:39 +08:00
2006-01-24 04:40:14 +08:00
/**
Give up control of terminal
*/
2005-09-20 21:26:39 +08:00
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
2012-11-19 08:30:30 +08:00
while ( 1 )
{
2013-09-23 09:11:53 +08:00
if ( tcsetattr ( 0 , TCSANOW , & terminal_mode_for_executing_programs ) )
2012-11-19 08:30:30 +08:00
{
if ( errno ! = EINTR )
{
debug ( 1 , _ ( L " Could not set terminal mode for new job " ) ) ;
wperror ( L " tcsetattr " ) ;
break ;
}
}
else
break ;
}
2005-09-20 21:26:39 +08:00
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
2014-01-15 18:39:19 +08:00
/**
Update the cursor position
*/
2014-03-30 05:19:45 +08:00
static void update_buff_pos ( editable_line_t * el , size_t buff_pos )
2014-01-15 18:39:19 +08:00
{
2014-03-30 05:19:45 +08:00
el - > position = buff_pos ;
if ( el = = & data - > command_line & & data - > sel_active )
2014-01-15 22:07:22 +08:00
{
2014-04-01 01:01:39 +08:00
if ( data - > sel_begin_pos < = buff_pos )
{
data - > sel_start_pos = data - > sel_begin_pos ;
data - > sel_stop_pos = buff_pos ;
}
else
{
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
}
2006-01-24 04:40:14 +08:00
/**
Grab control of terminal
*/
2005-09-20 21:26:39 +08:00
static void term_steal ( )
2006-01-31 03:53:10 +08:00
{
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
while ( 1 )
{
if ( tcsetattr ( 0 , TCSANOW , & shell_modes ) )
{
if ( errno ! = EINTR )
{
debug ( 1 , _ ( L " Could not set terminal mode for shell " ) ) ;
wperror ( L " tcsetattr " ) ;
break ;
}
}
else
break ;
}
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
common_handle_winch ( 0 ) ;
2005-09-20 21:26:39 +08:00
}
2006-05-14 18:16:23 +08:00
int reader_exit_forced ( )
{
2012-11-19 08:30:30 +08:00
return exit_forced ;
2006-05-14 18:16:23 +08:00
}
2013-01-06 07:21:42 +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 https://github.com/fish-shell/fish-shell/issues/335
wcstring full_line ;
if ( autosuggestion . size ( ) < = cmdline . size ( ) | | cmdline . empty ( ) )
{
// No or useless autosuggestion, or no command line
full_line = cmdline ;
}
else if ( string_prefixes_string ( cmdline , autosuggestion ) )
{
// No case disagreements, or no extra characters in the autosuggestion
full_line = autosuggestion ;
}
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
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 ;
if ( begin )
{
const wchar_t * end = begin + wcslen ( begin ) ;
last_token_contains_uppercase = ( std : : find_if ( begin , end , iswupper ) ! = end ) ;
}
if ( ! last_token_contains_uppercase )
{
// Use the autosuggestion's case
full_line = autosuggestion ;
}
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
full_line = cmdline ;
full_line . append ( autosuggestion , cmdline . size ( ) , autosuggestion . size ( ) - cmdline . size ( ) ) ;
}
}
return full_line ;
}
2007-10-05 22:59:19 +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 ;
2013-01-06 07:21:42 +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
2013-01-06 07:21:42 +08:00
// Combine the command and autosuggestion into one string
2014-01-27 16:56:13 +08:00
wcstring 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 ( ) ;
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
2014-03-30 05:19:45 +08:00
if ( data - > sel_active )
2014-01-15 22:07:22 +08:00
{
2014-03-30 05:19:45 +08:00
highlight_spec_t selection_color = highlight_make_background ( highlight_spec_selection ) ;
2014-04-01 01:01:39 +08:00
for ( size_t i = data - > sel_start_pos ; i < = std : : min ( len - 1 , 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 ) ;
2014-01-18 04:04:03 +08:00
// Re-render our completions page if necessary
2014-01-23 09:45:27 +08:00
// 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.
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
2012-11-19 08:30:30 +08:00
s_write ( & data - > screen ,
data - > left_prompt_buff ,
data - > right_prompt_buff ,
full_line ,
2014-01-27 16:56:13 +08:00
cmd_line - > size ( ) ,
2012-11-19 08:30:30 +08:00
& colors [ 0 ] ,
& indents [ 0 ] ,
2014-03-31 04:13:35 +08:00
cursor_position ,
2014-01-15 22:07:22 +08:00
data - > sel_start_pos ,
2014-03-30 05:19:45 +08:00
data - > sel_stop_pos ,
2014-01-27 16:56:13 +08:00
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
}
2013-02-21 10:18:08 +08:00
/** Internal helper function for handling killing parts of text. */
2014-01-27 06:55:41 +08:00
static void reader_kill ( editable_line_t * el , size_t begin_idx , size_t length , int mode , int newv )
2006-10-12 21:27:32 +08:00
{
2014-01-27 06:47:15 +08:00
const wchar_t * begin = el - > text . c_str ( ) + begin_idx ;
2012-11-19 08:30:30 +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 ) ;
}
else
{
2012-03-04 11:37:55 +08:00
wcstring old = data - > kill_item ;
2012-11-19 08:30:30 +08:00
if ( mode = = KILL_APPEND )
{
2012-03-04 11:37:55 +08:00
data - > kill_item . append ( begin , length ) ;
2012-11-19 08:30:30 +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
}
2006-10-12 21:27:32 +08:00
2012-11-19 08:30:30 +08:00
kill_replace ( old , data - > kill_item ) ;
}
2006-10-13 03:30:00 +08:00
2014-01-27 06:47:15 +08:00
if ( el - > position > begin_idx )
2012-11-19 08:30:30 +08:00
{
2012-10-09 14:47:32 +08:00
/* 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
2013-01-21 06:35:41 +08:00
2012-06-05 05:20:01 +08:00
/* This is called from a signal handler! */
2012-11-19 08:30:30 +08:00
void reader_handle_int ( int sig )
2005-10-06 06:37:08 +08:00
{
2012-11-19 08:30:30 +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
}
2012-02-03 07:05:08 +08:00
const wchar_t * reader_current_filename ( )
2005-09-20 21:26:39 +08:00
{
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
}
2012-11-19 08:30:30 +08:00
void reader_push_current_filename ( const wchar_t * fn )
2005-09-20 21:26:39 +08:00
{
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
}
2012-02-03 07:05:08 +08:00
void reader_pop_current_filename ( )
2005-09-20 21:26:39 +08:00
{
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
}
2012-02-07 03:34:49 +08:00
/** Make sure buffers are large enough to hold the current string length */
2014-01-27 16:56:13 +08:00
void reader_data_t : : command_line_changed ( const editable_line_t * el )
2012-11-19 08:30:30 +08:00
{
2012-02-29 04:40:20 +08:00
ASSERT_IS_MAIN_THREAD ( ) ;
2014-01-27 16:56:13 +08:00
if ( el = = & this - > command_line )
{
size_t len = this - > command_line . size ( ) ;
2012-11-19 08:30:30 +08:00
2014-04-01 01:01:39 +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. */
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
2014-04-01 01:01:39 +08:00
/* Update the gen count */
s_generation_count + + ;
}
2014-01-27 16:56:13 +08:00
else if ( el = = & this - > pager . search_field_line )
{
this - > pager . refilter_completions ( ) ;
2014-02-17 11:56:13 +08:00
this - > pager_selection_changed ( ) ;
}
}
void reader_data_t : : pager_selection_changed ( )
{
ASSERT_IS_MAIN_THREAD ( ) ;
const completion_t * completion = this - > pager . selected_completion ( this - > current_page_rendering ) ;
/* Update the cursor and command line */
size_t cursor_pos = this - > cycle_cursor_pos ;
wcstring new_cmd_line ;
if ( completion = = NULL )
{
new_cmd_line = this - > cycle_command_line ;
2014-01-27 16:56:13 +08:00
}
2014-02-17 11:56:13 +08:00
else
{
new_cmd_line = completion_apply_to_command_line ( completion - > completion , completion - > flags , this - > cycle_command_line , & cursor_pos , false ) ;
}
reader_set_buffer_maintaining_pager ( new_cmd_line , cursor_pos ) ;
/* Since we just inserted a completion, don't immediately do a new autosuggestion */
this - > suppress_autosuggestion = true ;
/* Trigger repaint (see #765) */
reader_repaint_needed ( ) ;
2005-09-20 21:26:39 +08:00
}
2013-07-17 15:38:04 +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 ( ) ;
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 ) ;
/* Determine the offset of this command substitution */
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
2013-10-09 17:03:50 +08:00
/* Parse this subcmd */
parse_node_tree_t parse_tree ;
2014-01-13 14:39:12 +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
2013-10-09 17:03:50 +08:00
/* Look for plain statements where the cursor is at the end of the command */
const parse_node_t * matching_cmd_node = NULL ;
const size_t len = parse_tree . size ( ) ;
for ( size_t i = 0 ; i < len ; i + + )
2013-07-17 15:38:04 +08:00
{
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
2013-10-09 17:03:50 +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
2013-10-09 17:03:50 +08:00
/* Skip decorated statements */
if ( parse_tree . decoration_for_plain_statement ( node ) ! = parse_statement_decoration_none )
continue ;
2013-07-17 15:38:04 +08:00
2013-10-09 17:03:50 +08:00
/* Get the command node. Skip it if we can't or it has no source */
const parse_node_t * cmd_node = parse_tree . get_child ( node , 0 , parse_token_type_string ) ;
if ( cmd_node = = NULL | | ! cmd_node - > has_source ( ) )
continue ;
2014-01-15 17:40:40 +08:00
2013-10-09 17:03:50 +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 )
2013-07-17 15:38:04 +08:00
{
2013-10-09 17:03:50 +08:00
/* Success! */
matching_cmd_node = cmd_node ;
2013-07-17 15:38:04 +08:00
break ;
}
}
2013-10-09 17:03:50 +08:00
/* Now if we found a command node, expand it */
2013-07-17 15:38:04 +08:00
bool result = false ;
2013-10-09 17:03:50 +08:00
if ( matching_cmd_node ! = NULL )
2013-07-17 15:38:04 +08:00
{
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 ;
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 )
{
output - > assign ( cmdline ) ;
2013-10-09 17:03:50 +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 ;
}
2013-08-25 02:16:45 +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 ( ) ;
if ( this - > expand_abbreviations & & el = = & data - > command_line )
2013-07-17 15:38:04 +08:00
{
2013-07-17 17:55:15 +08:00
/* 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 ) ;
if ( reader_expand_abbreviation_in_command ( el - > text , cursor_pos , & new_cmdline ) )
2013-07-17 15:38:04 +08:00
{
/* 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
2014-01-27 06:47:15 +08:00
el - > text . swap ( 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
2013-02-03 06:50:22 +08:00
/** Sorts and remove any duplicate completions in the list. */
static void sort_and_make_unique ( std : : vector < completion_t > & l )
2012-07-18 03:47:01 +08:00
{
2013-09-01 06:01:02 +08:00
sort ( l . begin ( ) , l . end ( ) , completion_t : : is_alphabetically_less_than ) ;
l . erase ( std : : unique ( l . begin ( ) , l . end ( ) , completion_t : : is_alphabetically_equal_to ) , l . end ( ) ) ;
2012-01-17 00:56:47 +08:00
}
2005-09-20 21:26:39 +08:00
2013-01-21 05:23:27 +08:00
void reader_reset_interrupted ( )
{
interrupted = 0 ;
}
2006-10-05 05:42:04 +08:00
int reader_interrupted ( )
2005-09-20 21:26:39 +08:00
{
2013-01-22 18:00:02 +08:00
int res = interrupted ;
2012-11-19 08:30:30 +08:00
if ( res )
2013-01-21 05:23:27 +08:00
{
2012-11-19 08:30:30 +08:00
interrupted = 0 ;
2013-01-21 05:23:27 +08:00
}
2013-01-22 18:00:02 +08:00
return res ;
}
int reader_reading_interrupted ( )
{
int res = reader_interrupted ( ) ;
2013-01-22 18:25:17 +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 ( ) ;
2013-02-12 15:16:50 +08:00
// We handled the interrupt ourselves, our caller doesn't need to
2013-01-22 18:57:47 +08:00
// handle it.
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
}
2013-02-06 04:18:22 +08:00
bool reader_thread_job_is_stale ( )
2013-01-23 07:19:29 +08:00
{
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2013-01-24 21:57:20 +08:00
return ( void * ) ( uintptr_t ) s_generation_count ! = pthread_getspecific ( generation_count_key ) ;
2013-01-23 07:19:29 +08:00
}
2014-08-03 12:01:40 +08:00
void reader_write_title ( const wcstring & cmd )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
const env_var_t term_str = env_get_string ( L " TERM " ) ;
/*
This is a pretty lame heuristic for detecting terminals that do
not support setting the title . If we recognise the terminal name
as that of a virtual terminal , we assume it supports setting the
title . If we recognise it as that of a console , we assume it
does not support setting the title . Otherwise we check the
2012-12-16 12:23:24 +08:00
ttyname and see if we believe it is a virtual terminal .
2012-11-19 08:30:30 +08:00
One situation in which this breaks down is with screen , since
screen supports setting the terminal title if the underlying
terminal does so , but will print garbage on terminals that
don ' t . Since we can ' t see the underlying terminal below screen
there is no way to fix this .
*/
if ( term_str . missing ( ) )
return ;
const wchar_t * term = term_str . c_str ( ) ;
2012-01-03 05:40:03 +08:00
bool recognized = false ;
2012-11-19 08:30:30 +08:00
recognized = recognized | | contains ( term , L " xterm " , L " screen " , L " nxterm " , L " rxvt " ) ;
2012-01-03 05:40:03 +08:00
recognized = recognized | | ! wcsncmp ( term , L " xterm- " , wcslen ( L " xterm- " ) ) ;
2012-06-20 19:55:28 +08:00
recognized = recognized | | ! wcsncmp ( term , L " screen- " , wcslen ( L " screen- " ) ) ;
2012-01-03 05:40:03 +08:00
2012-11-19 08:30:30 +08:00
if ( ! recognized )
{
char * n = ttyname ( STDIN_FILENO ) ;
if ( contains ( term , L " linux " ) )
{
return ;
}
2007-03-18 07:38:46 +08:00
2012-11-19 08:30:30 +08:00
if ( strstr ( n , " tty " ) | | strstr ( n , " /vc/ " ) )
return ;
}
2005-09-20 21:26:39 +08:00
2014-08-03 12:01:40 +08:00
wcstring fish_title_command = DEFAULT_TITLE ;
if ( function_exists ( L " fish_title " ) )
{
fish_title_command = L " fish_title " ;
if ( ! cmd . empty ( ) )
{
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 ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
proc_push_interactive ( 0 ) ;
2014-08-03 12:01:40 +08:00
if ( exec_subshell ( fish_title_command , lst , false /* do not apply exit status */ ) ! = - 1 )
2012-11-19 08:30:30 +08:00
{
2013-02-16 18:38:13 +08:00
if ( ! lst . empty ( ) )
2012-11-19 08:30:30 +08:00
{
writestr ( L " \x1b ]0; " ) ;
2013-02-16 18:38:13 +08:00
for ( size_t i = 0 ; i < lst . size ( ) ; i + + )
2012-11-19 08:30:30 +08:00
{
writestr ( lst . at ( i ) . c_str ( ) ) ;
}
writestr ( L " \7 " ) ;
}
}
proc_pop_interactive ( ) ;
set_color ( rgb_color_t : : reset ( ) , rgb_color_t : : reset ( ) ) ;
2005-09-20 21:26:39 +08:00
}
/**
2006-10-16 23:32:26 +08:00
Reexecute the prompt command . The output is inserted into data - > prompt_buff .
2005-09-20 21:26:39 +08:00
*/
2006-10-16 23:32:26 +08:00
static void exec_prompt ( )
2005-09-20 21:26:39 +08:00
{
2012-11-08 11:59:20 +08:00
/* 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
2013-02-01 07:57:08 +08:00
/* Do not allow the exit status of the prompts to leak through */
const bool apply_exit_status = false ;
2012-11-08 11:59:20 +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 ) ;
2013-01-28 05:44:32 +08:00
if ( ! data - > left_prompt . empty ( ) )
2012-11-08 11:59:20 +08:00
{
wcstring_list_t prompt_list ;
2013-02-06 08:11:46 +08:00
// ignore return status
exec_subshell ( data - > left_prompt , prompt_list , apply_exit_status ) ;
2013-01-28 05:44:32 +08:00
for ( size_t i = 0 ; i < prompt_list . size ( ) ; i + + )
2012-11-08 11:59:20 +08:00
{
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
2013-01-28 05:44:32 +08:00
if ( ! data - > right_prompt . empty ( ) )
2012-11-08 11:59:20 +08:00
{
wcstring_list_t prompt_list ;
2013-01-28 05:50:30 +08:00
// status is ignored
2013-02-06 08:11:46 +08:00
exec_subshell ( data - > right_prompt , prompt_list , apply_exit_status ) ;
2013-01-28 05:44:32 +08:00
for ( size_t i = 0 ; i < prompt_list . size ( ) ; i + + )
2012-11-08 11:59:20 +08:00
{
2013-01-28 05:44:32 +08:00
// Right prompt does not support multiple lines, so just concatenate all of them
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
2012-11-08 11:59:20 +08:00
/* Write the screen title */
2014-08-03 12:01:40 +08:00
reader_write_title ( L " " ) ;
2005-09-20 21:26:39 +08:00
}
void reader_init ( )
{
2013-01-24 21:29:13 +08:00
VOMIT_ON_FAILURE ( pthread_key_create ( & generation_count_key , NULL ) ) ;
2006-08-24 18:43:54 +08:00
2013-09-23 09:11:53 +08:00
/* Save the initial terminal mode */
tcgetattr ( STDIN_FILENO , & terminal_mode_on_startup ) ;
/* Set the mode used for program execution, initialized to the current mode */
memcpy ( & terminal_mode_for_executing_programs , & terminal_mode_on_startup , sizeof terminal_mode_for_executing_programs ) ;
terminal_mode_for_executing_programs . c_iflag & = ~ IXON ; /* disable flow control */
terminal_mode_for_executing_programs . c_iflag & = ~ IXOFF ; /* disable flow control */
2012-11-19 08:30:30 +08:00
2013-09-23 09:11:53 +08:00
/* Set the mode used for the terminal, initialized to the current mode */
memcpy ( & shell_modes , & terminal_mode_on_startup , sizeof shell_modes ) ;
2012-11-19 08:30:30 +08:00
shell_modes . c_lflag & = ~ ICANON ; /* turn off canonical mode */
shell_modes . c_lflag & = ~ ECHO ; /* turn off echo mode */
2013-09-23 09:11:53 +08:00
shell_modes . c_iflag & = ~ IXON ; /* disable flow control */
shell_modes . c_iflag & = ~ IXOFF ; /* disable flow control */
2006-03-10 21:38:09 +08:00
shell_modes . c_cc [ VMIN ] = 1 ;
shell_modes . c_cc [ VTIME ] = 0 ;
2012-11-19 08:30:30 +08:00
2013-09-23 09:11:53 +08:00
# if defined(_POSIX_VDISABLE)
2012-06-03 06:43:18 +08:00
// PCA disable VDSUSP (typically control-Y), which is a funny job control
// function available only on OS X and BSD systems
// This lets us use control-Y for yank instead
2013-10-27 06:27:39 +08:00
# ifdef VDSUSP
2012-11-19 08:30:30 +08:00
shell_modes . c_cc [ VDSUSP ] = _POSIX_VDISABLE ;
2013-10-27 06:27:39 +08:00
# endif
2012-11-19 08:30:30 +08:00
# endif
2005-09-20 21:26:39 +08:00
}
void reader_destroy ( )
{
2013-01-24 21:29:13 +08:00
pthread_key_delete ( generation_count_key ) ;
2005-09-20 21:26:39 +08:00
}
2013-10-27 06:22:20 +08:00
void restore_term_mode ( )
{
// 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
if ( getpid ( ) = = tcgetpgrp ( STDIN_FILENO ) )
{
tcsetattr ( STDIN_FILENO , TCSANOW , & terminal_mode_on_startup ) ;
}
}
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
void reader_exit ( int do_exit , int forced )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
if ( data )
data - > end_loop = do_exit ;
end_loop = do_exit ;
if ( forced )
exit_forced = 1 ;
2005-09-20 21:26:39 +08:00
}
2007-10-05 22:59:19 +08:00
void reader_repaint_needed ( )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
if ( data )
{
data - > repaint_needed = true ;
}
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
void reader_repaint_if_needed ( )
{
2012-12-03 03:53:34 +08:00
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
2012-12-03 03:53:34 +08:00
if ( needs_reset )
2012-11-19 08:30:30 +08:00
{
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 ;
}
2012-12-03 03:53:34 +08:00
if ( needs_repaint )
2012-11-19 08:30:30 +08:00
{
2012-03-25 18:00:38 +08:00
reader_repaint ( ) ;
/* reader_repaint clears repaint_needed */
}
}
2013-04-04 04:49:58 +08:00
static void reader_repaint_if_needed_one_arg ( void * unused )
{
reader_repaint_if_needed ( ) ;
}
2012-11-19 08:30:30 +08:00
void reader_react_to_color_change ( )
{
2013-04-04 04:49:58 +08:00
if ( ! data )
return ;
2013-05-05 17:33:17 +08:00
2013-04-04 04:49:58 +08:00
if ( ! data - > repaint_needed | | ! data - > screen_reset_needed )
2012-11-19 08:30:30 +08:00
{
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 ;
2013-04-04 04:49:58 +08:00
input_common_add_callback ( reader_repaint_if_needed_one_arg , NULL ) ;
2012-11-19 08:30:30 +08:00
}
2012-03-25 18:00:38 +08:00
}
2007-09-21 22:05:49 +08:00
2007-10-05 22:59:19 +08:00
2014-01-27 16:56:13 +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-04-01 01:01:39 +08:00
/* These commands always end paging */
2014-01-27 16:56:13 +08:00
case R_HISTORY_SEARCH_BACKWARD :
case R_HISTORY_SEARCH_FORWARD :
case R_BEGINNING_OF_HISTORY :
case R_END_OF_HISTORY :
case R_HISTORY_TOKEN_SEARCH_BACKWARD :
case R_HISTORY_TOKEN_SEARCH_FORWARD :
case R_ACCEPT_AUTOSUGGESTION :
2014-01-27 18:17:31 +08:00
case R_CANCEL :
2014-01-27 16:56:13 +08:00
return true ;
2014-04-01 01:01:39 +08:00
/* These commands never do */
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 :
default :
return false ;
2014-04-01 01:01:39 +08:00
/* 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
case R_EXECUTE :
return false ;
2014-04-01 01:01:39 +08:00
/* These commands operate on the search field if that's where the focus is */
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 :
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 :
case R_BACKWARD_KILL_WORD :
case R_BACKWARD_KILL_PATH_COMPONENT :
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 :
case R_END_OF_BUFFER :
return ! focused_on_search_field ;
}
}
2005-09-20 21:26:39 +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
2014-01-27 06:47:15 +08:00
if ( el - > position < = 0 )
2012-11-19 08:30:30 +08:00
return ;
2012-12-16 12:23:24 +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 ;
2012-11-19 08:30:30 +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 ) ;
2012-11-19 08:30:30 +08:00
}
2014-01-27 06:47:15 +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
2012-11-19 08:30:30 +08:00
reader_repaint ( ) ;
2006-10-02 00:02:58 +08:00
2005-09-20 21:26:39 +08:00
}
/**
Insert the characters of the string into the command line buffer
and print them to the screen using syntax highlighting , etc .
2014-07-26 03:23:42 +08:00
Optionally also expand abbreviations , after space characters .
2013-08-25 02:16:45 +08:00
Returns true if the string changed .
2005-09-20 21:26:39 +08:00
*/
2014-07-26 03:23:42 +08:00
static bool insert_string ( editable_line_t * el , const wcstring & str , bool allow_expand_abbreviations = false )
2005-09-20 21:26:39 +08:00
{
2012-02-06 17:45:16 +08:00
size_t len = str . size ( ) ;
2012-02-29 04:40:20 +08:00
if ( len = = 0 )
2013-08-25 02:16:45 +08:00
return false ;
2014-07-26 03:23:42 +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() */
size_t cursor = 0 ;
while ( cursor < len )
{
2014-09-20 04:59:53 +08:00
/* 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 " ;|&^>< " ;
2014-09-20 04:59:53 +08:00
size_t char_triggering_expansion_pos = allow_expand_abbreviations ? str . find_first_of ( expansion_triggering_chars , cursor ) : wcstring : : npos ;
bool has_expansion_triggering_char = ( char_triggering_expansion_pos ! = wcstring : : npos ) ;
size_t range_end = ( has_expansion_triggering_char ? char_triggering_expansion_pos + 1 : len ) ;
2014-07-26 03:23:42 +08:00
/* Insert from the cursor up to but not including the range end */
assert ( range_end > cursor ) ;
el - > insert_string ( str , cursor , range_end - cursor ) ;
update_buff_pos ( el , el - > position ) ;
data - > command_line_changed ( el ) ;
2014-09-20 04:59:53 +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 ;
}
2014-01-27 06:47:15 +08:00
if ( el = = & data - > command_line )
{
data - > suppress_autosuggestion = false ;
2014-07-26 03:23:42 +08:00
2014-01-27 06:47:15 +08:00
/* Syntax highlight. Note we must have that buff_pos > 0 because we just added something nonzero to its length */
assert ( el - > position > 0 ) ;
reader_super_highlight_me_plenty ( - 1 ) ;
}
2014-07-26 03:23:42 +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
/**
Insert the character into the command line buffer and print it to
the screen using syntax highlighting , etc .
*/
2014-07-26 03:23:42 +08:00
static bool insert_char ( editable_line_t * el , wchar_t c , bool allow_expand_abbreviations = false )
2006-11-02 21:50:19 +08:00
{
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
}
2012-05-06 04:34:09 +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 .
2013-02-03 06:50:22 +08:00
\ 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 .
2012-05-06 04:34:09 +08:00
\ return The completed string
*/
2013-02-03 06:50:22 +08:00
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 ( ) ;
2012-11-19 08:30:30 +08:00
bool add_space = ! ( flags & COMPLETE_NO_SPACE ) ;
2013-03-06 12:54:16 +08:00
bool do_replace = ! ! ( flags & COMPLETE_REPLACES_TOKEN ) ;
2012-11-19 08:30:30 +08:00
bool do_escape = ! ( 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
if ( do_replace )
{
size_t move_cursor ;
const wchar_t * begin , * end ;
wchar_t * escaped ;
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 ) ;
if ( do_escape )
{
2013-04-08 14:54:43 +08:00
/* Respect COMPLETE_DONT_ESCAPE_TILDES */
2013-05-05 17:33:17 +08:00
bool no_tilde = ! ! ( flags & COMPLETE_DONT_ESCAPE_TILDES ) ;
2013-04-08 14:54:43 +08:00
escaped = escape ( val , ESCAPE_ALL | ESCAPE_NO_QUOTED | ( no_tilde ? ESCAPE_NO_TILDE : 0 ) ) ;
2012-11-19 08:30:30 +08:00
sb . append ( escaped ) ;
move_cursor = wcslen ( escaped ) ;
free ( escaped ) ;
}
else
{
sb . append ( val ) ;
move_cursor = wcslen ( val ) ;
}
if ( add_space )
{
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 ;
2012-11-19 08:30:30 +08:00
}
else
{
2012-05-06 04:34:09 +08:00
wchar_t quote = L ' \0 ' ;
2012-05-09 17:33:42 +08:00
wcstring replaced ;
2012-11-19 08:30:30 +08:00
if ( do_escape )
{
2013-04-08 14:54:43 +08:00
/* 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 */
2012-07-07 05:34:53 +08:00
parse_util_get_parameter_info ( command_line , cursor_pos , & quote , NULL , NULL ) ;
2013-02-04 03:38:22 +08:00
2013-02-03 06:50:22 +08:00
/* 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 https://github.com/fish-shell/fish-shell/issues/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 ;
}
}
2013-02-04 03:38:22 +08:00
2012-07-07 05:34:53 +08:00
replaced = parse_util_escape_string_with_quote ( val_str , quote ) ;
2012-11-19 08:30:30 +08:00
}
else
{
replaced = val ;
}
2013-02-04 03:38:22 +08:00
2013-02-03 06:50:22 +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 - - ;
}
2013-02-04 03:38:22 +08:00
2013-02-03 06:50:22 +08:00
/* Perform the insertion and compute the new location */
2012-05-06 04:34:09 +08:00
wcstring result = command_line ;
2013-02-03 06:50:22 +08:00
result . insert ( insertion_point , replaced ) ;
size_t new_cursor_pos = insertion_point + replaced . size ( ) + ( back_into_trailing_quote ? 1 : 0 ) ;
2012-05-06 04:34:09 +08:00
if ( add_space )
{
2013-02-03 06:50:22 +08:00
if ( quote ! = L ' \0 ' & & unescaped_quote ( command_line , insertion_point ) ! = quote )
2012-05-06 04:34:09 +08:00
{
/* This is a quoted parameter, first print a quote */
result . insert ( new_cursor_pos + + , wcstring ( & quote , 1 ) ) ;
}
result . insert ( new_cursor_pos + + , L " " ) ;
}
* inout_cursor_pos = new_cursor_pos ;
return result ;
2012-11-19 08:30:30 +08:00
}
2012-05-06 04:34:09 +08:00
}
2005-09-20 21:26:39 +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
2008-01-14 00:47:47 +08:00
\ param flags A union of all flags describing the completion to insert . See the completion_t struct for more information on possible values .
2005-09-20 21:26:39 +08:00
*/
2013-02-03 06:50:22 +08:00
static void completion_insert ( const wchar_t * val , complete_flags_t flags )
2012-05-06 04:34:09 +08:00
{
2014-01-27 06:47:15 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
size_t cursor = el - > position ;
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-11-19 08:30:30 +08:00
2012-05-06 04:34:09 +08:00
/* Since we just inserted a completion, don't immediately do a new autosuggestion */
data - > suppress_autosuggestion = true ;
}
2012-11-19 08:30:30 +08:00
struct autosuggestion_context_t
{
2012-02-20 18:13:31 +08:00
wcstring search_string ;
wcstring autosuggestion ;
2012-05-06 04:34:09 +08:00
size_t cursor_pos ;
2012-02-16 16:24:27 +08:00
history_search_t searcher ;
file_detection_context_t detector ;
2012-02-19 10:54:36 +08:00
const wcstring working_directory ;
2012-07-21 11:39:31 +08:00
const env_vars_snapshot_t vars ;
2012-02-29 04:40:20 +08:00
const unsigned int generation_count ;
2012-11-19 08:30:30 +08:00
2012-05-06 04:34:09 +08:00
autosuggestion_context_t ( history_t * history , const wcstring & term , size_t pos ) :
2012-02-20 18:13:31 +08:00
search_string ( term ) ,
2012-05-06 04:34:09 +08:00
cursor_pos ( pos ) ,
2012-02-16 16:24:27 +08:00
searcher ( * history , term , HISTORY_SEARCH_TYPE_PREFIX ) ,
2014-03-29 14:22:03 +08:00
detector ( history ) ,
2013-04-27 15:45:38 +08:00
working_directory ( env_get_pwd_slash ( ) ) ,
2012-07-21 11:39:31 +08:00
vars ( env_vars_snapshot_t : : highlighting_keys ) ,
2013-11-30 15:44:26 +08:00
generation_count ( s_generation_count )
2012-02-16 16:24:27 +08:00
{
}
2012-11-19 08:30:30 +08:00
2012-03-26 13:41:22 +08:00
/* The function run in the background thread to determine an autosuggestion */
2012-11-19 08:30:30 +08:00
int threaded_autosuggest ( void )
{
2012-02-19 10:54:36 +08:00
ASSERT_IS_BACKGROUND_THREAD ( ) ;
2012-11-19 08:30:30 +08:00
2012-03-31 02:16:24 +08:00
/* If the main thread has moved on, skip all the work */
2012-11-19 08:30:30 +08:00
if ( generation_count ! = s_generation_count )
{
2012-02-29 04:40:20 +08:00
return 0 ;
}
2012-11-19 08:30:30 +08:00
2013-01-24 21:57:20 +08:00
VOMIT_ON_FAILURE ( pthread_setspecific ( generation_count_key , ( void * ) ( uintptr_t ) generation_count ) ) ;
2013-01-23 07:19:29 +08:00
2012-05-06 04:34:09 +08:00
/* Let's make sure we aren't using the empty string */
2012-11-19 08:30:30 +08:00
if ( search_string . empty ( ) )
{
2012-05-06 04:34:09 +08:00
return 0 ;
}
2012-11-19 08:30:30 +08:00
2013-04-08 07:40:14 +08:00
while ( ! reader_thread_job_is_stale ( ) & & searcher . go_backwards ( ) )
2012-11-19 08:30:30 +08:00
{
2012-02-19 10:54:36 +08:00
history_item_t item = searcher . current_item ( ) ;
2012-11-19 08:30:30 +08:00
2012-03-26 13:41: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
if ( autosuggest_validate_from_history ( item , detector , working_directory , vars ) )
{
2012-02-19 10:54:36 +08:00
/* The command autosuggestion was handled specially, so we're done */
2012-02-20 18:13:31 +08:00
this - > autosuggestion = searcher . current_string ( ) ;
2012-02-19 10:54:36 +08:00
return 1 ;
2012-02-20 18:13:31 +08:00
}
}
2012-11-19 08:30:30 +08:00
2013-04-08 07:40:14 +08:00
/* Maybe cancel here */
if ( reader_thread_job_is_stale ( ) )
return 0 ;
2012-03-31 02:16:24 +08:00
/* Try handling a special command like cd */
wcstring special_suggestion ;
2012-11-19 08:30:30 +08:00
if ( autosuggest_suggest_special ( search_string , working_directory , special_suggestion ) )
{
2012-03-31 02:16:24 +08:00
this - > autosuggestion = special_suggestion ;
return 1 ;
}
2013-05-05 17:33:17 +08:00
2013-04-08 07:40:14 +08:00
/* Maybe cancel here */
if ( reader_thread_job_is_stale ( ) )
return 0 ;
2012-11-19 08:30:30 +08:00
2012-05-06 04:34:09 +08:00
// Here we do something a little funny
// If the line ends with a space, and the cursor is not at the end,
2012-05-08 08:31:24 +08:00
// 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 ) ;
2012-05-06 04:34:09 +08:00
const bool cursor_at_end = ( this - > cursor_pos = = search_string . size ( ) ) ;
2012-07-07 05:34:53 +08:00
if ( ! cursor_at_end & & iswspace ( last_char ) )
return 0 ;
2012-11-24 03:12:22 +08:00
/* On the other hand, if the line ends with a quote, don't go dumping stuff after the quote */
2012-07-07 05:34:53 +08:00
if ( wcschr ( L " ' \" " , last_char ) & & cursor_at_end )
2012-05-06 04:34:09 +08:00
return 0 ;
2012-02-26 10:54:49 +08:00
/* Try normal completions */
std : : vector < completion_t > completions ;
2013-11-30 15:44:26 +08:00
complete ( search_string , completions , COMPLETION_REQUEST_AUTOSUGGESTION ) ;
2012-11-19 08:30:30 +08:00
if ( ! completions . empty ( ) )
{
2012-03-02 09:31:45 +08:00
const completion_t & comp = completions . at ( 0 ) ;
2012-05-07 04:36:51 +08:00
size_t cursor = this - > cursor_pos ;
2014-01-13 05:53:59 +08:00
this - > autosuggestion = completion_apply_to_command_line ( comp . completion , comp . flags , this - > search_string , & cursor , true /* append only */ ) ;
2012-02-26 10:54:49 +08:00
return 1 ;
2012-02-19 10:54:36 +08:00
}
2012-11-19 08:30:30 +08:00
2012-02-19 10:54:36 +08:00
return 0 ;
}
2012-02-16 16:24:27 +08:00
} ;
2012-11-19 08:30:30 +08:00
static int threaded_autosuggest ( autosuggestion_context_t * ctx )
{
2012-02-19 10:54:36 +08:00
return ctx - > threaded_autosuggest ( ) ;
2012-02-16 16:24:27 +08:00
}
2012-11-19 08:30:30 +08:00
static bool can_autosuggest ( void )
{
2012-03-06 01:34:31 +08:00
/* 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 " ;
return ! data - > suppress_autosuggestion & &
2012-11-19 08:30:30 +08:00
data - > history_search . is_at_end ( ) & &
2014-01-27 06:47:15 +08:00
el = = & data - > command_line & &
el - > text . find_first_not_of ( whitespace ) ! = wcstring : : npos ;
2012-02-16 16:24:27 +08:00
}
2012-11-19 08:30:30 +08:00
static void autosuggest_completed ( autosuggestion_context_t * ctx , int result )
{
2012-02-20 18:13:31 +08:00
if ( result & &
2012-11-19 08:30:30 +08:00
can_autosuggest ( ) & &
2014-01-27 06:47:15 +08:00
ctx - > search_string = = data - > command_line . text & &
2012-11-19 08:30:30 +08:00
string_prefixes_string_case_insensitive ( ctx - > search_string , ctx - > autosuggestion ) )
{
2012-02-16 16:24:27 +08:00
/* Autosuggestion is active and the search term has not changed, so we're good to go */
2012-02-20 18:13:31 +08:00
data - > autosuggestion = ctx - > autosuggestion ;
sanity_check ( ) ;
reader_repaint ( ) ;
2012-02-16 16:24:27 +08:00
}
delete ctx ;
}
2012-11-19 08:30:30 +08:00
static void update_autosuggestion ( void )
{
2012-02-07 02:52:13 +08:00
/* 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 ( ) ;
2012-11-19 08:30:30 +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 ( ) ;
autosuggestion_context_t * ctx = new autosuggestion_context_t ( data - > history , el - > text , el - > position ) ;
2012-02-16 16:24:27 +08:00
iothread_perform ( threaded_autosuggest , autosuggest_completed , ctx ) ;
}
2012-02-07 02:52:13 +08:00
}
2012-12-11 08:23:08 +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 )
2012-11-19 08:30:30 +08:00
{
if ( ! data - > autosuggestion . empty ( ) )
{
2014-08-12 01:17:41 +08:00
/* Accepting an autosuggestion clears the pager */
clear_pager ( ) ;
2012-07-16 05:02:34 +08:00
/* Accept the autosuggestion */
2012-12-11 08:23:08 +08:00
if ( full )
{
/* Just take the whole thing */
2014-01-26 16:41:30 +08:00
data - > command_line . text = data - > autosuggestion ;
2012-12-11 08:23:08 +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 ) ;
2012-12-11 08:23:08 +08:00
for ( size_t idx = data - > command_line . size ( ) ; idx < data - > autosuggestion . size ( ) ; idx + + )
{
wchar_t wc = data - > autosuggestion . at ( idx ) ;
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 ( ) ;
}
}
2014-01-20 15:52:35 +08:00
/* Ensure we have no pager contents */
static void clear_pager ( )
{
if ( data )
{
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
}
2014-01-27 18:17:31 +08:00
static void select_completion_in_direction ( enum selection_direction_t dir )
2014-01-19 04:42:53 +08:00
{
2014-01-27 18:17:31 +08:00
assert ( data ! = NULL ) ;
/* Note: this will probably trigger reader_selected_completion_changed, which will cause us to update stuff */
2014-02-17 11:56:13 +08:00
bool selection_changed = data - > pager . select_next_completion_in_direction ( dir , data - > current_page_rendering ) ;
if ( selection_changed )
{
data - > pager_selection_changed ( ) ;
}
2014-01-19 04:42:53 +08:00
}
2008-01-14 00:47:47 +08:00
/**
2006-12-01 07:57:49 +08:00
Flash the screen . This function only changed the color of the
current line , since the flash_screen sequnce is rather painful to
look at in most terminal emulators .
*/
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 ;
for ( size_t i = 0 ; i < el - > position ; i + + )
2012-11-19 08:30:30 +08:00
{
2014-01-15 17:01:25 +08:00
data - > colors . at ( i ) = highlight_spec_search_match < < 16 ;
2012-11-19 08:30:30 +08:00
}
reader_repaint ( ) ;
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
}
2008-01-14 09:00:32 +08:00
/**
Characters that may not be part of a token that is to be replaced
by a case insensitive completion .
*/
# define REPLACE_UNCLEAN L"$*?({})"
2008-01-14 03:32:21 +08:00
2008-01-14 09:00:32 +08:00
/**
Check if the specified string can be replaced by a case insensitive
2012-08-22 08:18:52 +08:00
completion with the specified flags .
2008-01-14 09:00:32 +08:00
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 .
*/
2012-11-19 08:30:30 +08:00
static bool reader_can_replace ( const wcstring & in , int flags )
2008-01-14 03:32:21 +08:00
{
2012-11-19 08:30:30 +08:00
const wchar_t * str = in . c_str ( ) ;
2008-01-14 03:32:21 +08:00
2012-11-19 08:30:30 +08:00
if ( flags & COMPLETE_DONT_ESCAPE )
{
return true ;
}
/*
Test characters that have a special meaning in any character position
*/
while ( * str )
{
if ( wcschr ( REPLACE_UNCLEAN , * str ) )
return false ;
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
}
2013-05-26 06:41:18 +08:00
/* Compare two completions, ordering completions with better match types first */
bool compare_completions_by_match_type ( const completion_t & a , const completion_t & b )
2012-08-22 08:18:52 +08:00
{
2013-09-01 06:01:02 +08:00
/* Compare match types, unless both completions are prefix (#923) in which case we always want to compare them alphabetically */
if ( a . match . type ! = fuzzy_match_prefix | | b . match . type ! = fuzzy_match_prefix )
2012-08-22 08:18:52 +08:00
{
2013-09-01 06:01:02 +08:00
int match_compare = a . match . compare ( b . match ) ;
if ( match_compare ! = 0 )
{
return match_compare < 0 ;
}
2012-08-22 08:18:52 +08:00
}
2013-05-26 06:41:18 +08:00
2012-08-22 08:18:52 +08:00
/* Compare using file comparison */
return wcsfilecmp ( a . completion . c_str ( ) , b . completion . c_str ( ) ) < 0 ;
}
2013-08-26 15:05:23 +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 )
2012-08-22 08:18:52 +08:00
{
2013-05-26 06:41:18 +08:00
fuzzy_match_type_t best_type = fuzzy_match_none ;
2013-08-25 17:10:22 +08:00
for ( size_t i = 0 ; i < comp . size ( ) ; i + + )
2013-05-26 06:41:18 +08:00
{
const completion_t & el = comp . at ( i ) ;
if ( el . match . type < best_type )
2013-08-25 17:10:22 +08:00
{
2013-05-26 06:41:18 +08:00
best_type = el . match . type ;
2013-08-25 17:10:22 +08:00
}
2013-05-26 06:41:18 +08:00
}
2013-08-26 15:05:23 +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) */
2013-08-18 10:43:13 +08:00
if ( best_type = = fuzzy_match_exact )
{
best_type = fuzzy_match_prefix ;
}
2013-08-25 17:10:22 +08:00
return best_type ;
}
/* Order completions such that case insensitive completions come first. */
static void prioritize_completions ( std : : vector < completion_t > & comp )
{
fuzzy_match_type_t best_type = get_best_match_type ( comp ) ;
2013-06-02 16:14:26 +08:00
2013-08-25 17:10:22 +08:00
/* Throw out completions whose match types are less suitable than the best. */
size_t i = comp . size ( ) ;
2013-05-26 06:41:18 +08:00
while ( i - - )
{
2013-08-18 10:43:13 +08:00
if ( comp . at ( i ) . match . type > best_type )
2013-05-26 06:41:18 +08:00
{
comp . erase ( comp . begin ( ) + i ) ;
}
}
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
/* Sort the remainder */
sort ( comp . begin ( ) , comp . end ( ) , compare_completions_by_match_type ) ;
2012-08-22 08:18:52 +08:00
}
2005-09-20 21:26:39 +08:00
/**
Handle the list of completions . This means the following :
2012-11-19 08:30:30 +08:00
2005-09-20 21:26:39 +08:00
- 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 .
2013-02-18 14:03:00 +08:00
- 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 .
2012-11-19 08:30:30 +08:00
2005-09-20 21:26:39 +08:00
\ param comp the list of completion strings
2014-01-28 23:30:05 +08:00
\ param continue_after_prefix_insertion If we have a shared prefix , whether to print the list of completions after inserting it .
2012-11-19 08:30:30 +08:00
2012-08-22 08:18:52 +08:00
Return true if we inserted text into the command line , false if we did not .
2005-09-20 21:26:39 +08:00
*/
2014-01-28 23:30:05 +08:00
static bool handle_completions ( const std : : vector < completion_t > & comp , bool continue_after_prefix_insertion )
2005-09-20 21:26:39 +08:00
{
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 ) ;
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 ) ;
/*
Check trivial cases
*/
switch ( comp . size ( ) )
2005-09-20 21:26:39 +08:00
{
2013-02-28 04:03:30 +08:00
/* No suitable completions found, flash screen and return */
2012-11-19 16:31:03 +08:00
case 0 :
{
reader_flash ( ) ;
done = true ;
success = false ;
break ;
}
2005-09-20 21:26:39 +08:00
2012-11-19 16:31:03 +08:00
/* Exactly one suitable completion found - insert it */
case 1 :
{
2005-09-20 21:26:39 +08:00
2012-11-19 16:31:03 +08:00
const completion_t & c = comp . at ( 0 ) ;
2005-09-20 21:26:39 +08:00
2012-11-19 16:31:03 +08:00
/*
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 { }
*/
2013-06-02 16:14:26 +08:00
if ( ! ( c . flags & COMPLETE_REPLACES_TOKEN ) | | reader_can_replace ( tok , c . flags ) )
2012-11-19 16:31:03 +08:00
{
completion_insert ( c . completion . c_str ( ) , c . flags ) ;
}
done = true ;
success = true ;
break ;
2012-11-19 08:30:30 +08:00
}
}
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
if ( ! done )
{
2013-08-25 17:10:22 +08:00
fuzzy_match_type_t best_match_type = get_best_match_type ( comp ) ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +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 ) ;
2013-08-18 10:43:13 +08:00
if ( el . match . type < = best_match_type & & ! ( el . flags & COMPLETE_REPLACES_TOKEN ) )
2012-11-19 08:30:30 +08:00
{
2013-05-26 06:41:18 +08:00
will_replace_token = false ;
break ;
2012-11-19 08:30:30 +08:00
}
}
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +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 + + )
2012-11-19 08:30:30 +08:00
{
2013-05-26 06:41:18 +08:00
const completion_t & el = comp . at ( i ) ;
2013-08-26 15:05:23 +08:00
/* Ignore completions with a less suitable match type than the best. */
2013-08-18 10:43:13 +08:00
if ( el . match . type > best_match_type )
2013-05-26 06:41:18 +08:00
continue ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
/* Only use completions that match replace_token */
2013-06-02 16:14:26 +08:00
bool completion_replace_token = ! ! ( el . flags & COMPLETE_REPLACES_TOKEN ) ;
2013-05-26 06:41:18 +08:00
if ( completion_replace_token ! = will_replace_token )
continue ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
/* Don't use completions that want to replace, if we cannot replace them */
if ( completion_replace_token & & ! reader_can_replace ( tok , el . flags ) )
continue ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
/* This completion survived */
surviving_completions . push_back ( el ) ;
}
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
/* 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 )
2012-11-19 08:30:30 +08:00
{
2013-05-26 06:41:18 +08:00
/* First entry, use the whole string */
common_prefix = el . completion ;
flags = el . flags ;
2012-11-19 08:30:30 +08:00
}
2013-05-26 06:41:18 +08:00
else
2012-11-19 08:30:30 +08:00
{
2013-05-26 06:41:18 +08:00
/* Determine the shared prefix length. */
size_t idx , max = mini ( common_prefix . size ( ) , el . completion . size ( ) ) ;
2013-06-02 16:14:26 +08:00
for ( idx = 0 ; idx < max ; idx + + )
{
2013-05-26 06:41:18 +08:00
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 ) ;
}
if ( ! matches )
break ;
}
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +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
2013-05-26 06:41:18 +08:00
/* Early out if we decide there's no common prefix */
if ( idx = = 0 )
break ;
2012-11-19 08:30:30 +08:00
}
}
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
if ( ! common_prefix . empty ( ) )
2012-11-19 08:30:30 +08:00
{
2013-05-26 06:41:18 +08:00
/* 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
}
2014-04-01 01:01:39 +08:00
2014-01-28 23:30:05 +08:00
if ( continue_after_prefix_insertion | | common_prefix . empty ( ) )
2012-11-19 08:30:30 +08:00
{
2014-01-28 23:30:05 +08:00
/* We didn't get a common prefix, or we want to print the list anyways. */
2013-05-26 06:41:18 +08:00
size_t len , prefix_start = 0 ;
wcstring prefix ;
2014-01-27 06:47:15 +08:00
parse_util_get_parameter_info ( el - > text , el - > position , NULL , & prefix_start , NULL ) ;
2012-11-19 08:30:30 +08:00
2014-01-27 06:47:15 +08:00
assert ( el - > position > = prefix_start ) ;
len = el - > position - prefix_start ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
if ( match_type_requires_full_replacement ( best_match_type ) )
{
// No prefix
prefix . clear ( ) ;
}
else if ( len < = PREFIX_MAX_LEN )
{
2014-01-27 06:47:15 +08:00
prefix . append ( el - > text , prefix_start , len ) ;
2013-05-26 06:41:18 +08:00
}
else
{
// append just the end of the string
prefix = wcstring ( & ellipsis_char , 1 ) ;
2014-01-27 06:47:15 +08:00
prefix . append ( el - > text , prefix_start + len - PREFIX_MAX_LEN , PREFIX_MAX_LEN ) ;
2013-05-26 06:41:18 +08:00
}
2012-11-19 08:30:30 +08:00
2014-04-01 01:01:39 +08:00
wchar_t quote ;
2014-01-27 06:47:15 +08:00
parse_util_get_parameter_info ( el - > text , el - > position , & quote , NULL , NULL ) ;
2013-02-18 14:20:31 +08:00
2014-03-16 04:46:15 +08:00
/* Update the pager data */
data - > pager . set_prefix ( prefix ) ;
data - > pager . set_completions ( surviving_completions ) ;
2012-11-19 08:30:30 +08:00
2014-03-16 04:46:15 +08:00
/* Invalidate our rendering */
data - > current_page_rendering = page_rendering_t ( ) ;
2014-04-01 01:01:39 +08:00
2014-03-16 04:46:15 +08:00
/* Modify the command line to reflect the new pager */
data - > pager_selection_changed ( ) ;
2014-04-01 01:01:39 +08:00
2014-01-27 16:56:13 +08:00
reader_repaint_needed ( ) ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
success = false ;
2012-11-19 08:30:30 +08:00
}
}
return success ;
}
2012-12-06 05:33:07 +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 )
{
bool we_think_we_are_orphaned = false ;
/* 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. */
if ( loop_count % 64 = = 0 )
{
if ( kill ( shell_pgid , 0 ) < 0 & & errno = = ESRCH )
{
we_think_we_are_orphaned = true ;
}
}
if ( ! we_think_we_are_orphaned & & loop_count % 128 = = 0 )
{
/* Try reading from the tty; if we get EIO we are orphaned. This is sort of bad because it may block. */
char * tty = ctermid ( NULL ) ;
if ( ! tty )
{
wperror ( L " ctermid " ) ;
exit_without_destructors ( 1 ) ;
}
/* Open the tty. Presumably this is stdin, but maybe not? */
int tty_fd = open ( tty , O_RDONLY | O_NONBLOCK ) ;
if ( tty_fd < 0 )
{
wperror ( L " open " ) ;
exit_without_destructors ( 1 ) ;
}
char tmp ;
if ( read ( tty_fd , & tmp , 1 ) < 0 & & errno = = EIO )
{
we_think_we_are_orphaned = true ;
}
close ( tty_fd ) ;
}
/* Just give up if we've done it a lot times */
if ( loop_count > 4096 )
{
we_think_we_are_orphaned = true ;
}
return we_think_we_are_orphaned ;
}
2012-11-19 08:30:30 +08:00
/**
Initialize data for interactive use
*/
static void reader_interactive_init ( )
{
/* See if we are running interactively. */
pid_t shell_pgid ;
input_init ( ) ;
kill_init ( ) ;
shell_pgid = getpgrp ( ) ;
/*
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 .
*/
2012-12-06 05:33:07 +08:00
if ( tcgetpgrp ( STDIN_FILENO ) ! = shell_pgid )
2012-11-19 08:30:30 +08:00
{
int block_count = 0 ;
int i ;
/*
Bummer , we are not in control of the terminal . Stop until
parent has given us control of it . Stopping in fish is a bit
of a challange , what with all the signal fidgeting , we need
to reset a bunch of signal state , making this coda a but
unobvious .
In theory , reseting signal handlers could cause us to miss
signal deliveries . In practice , this code should only be run
suring startup , when we ' re not waiting for any signals .
*/
while ( signal_is_blocked ( ) )
{
signal_unblock ( ) ;
block_count + + ;
}
signal_reset_handlers ( ) ;
2012-12-06 05:33:07 +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 .
2012-11-19 08:30:30 +08:00
*/
2012-12-06 05:33:07 +08:00
for ( unsigned long loop_count = 0 ; ; loop_count + + )
2012-11-19 08:30:30 +08:00
{
2012-12-06 05:33:07 +08:00
pid_t owner = tcgetpgrp ( STDIN_FILENO ) ;
shell_pgid = getpgrp ( ) ;
if ( owner < 0 & & errno = = ENOTTY )
{
// No TTY, cannot be interactive?
debug ( 1 ,
_ ( L " No TTY for interactive shell (tcgetpgrp failed) " ) ) ;
wperror ( L " setpgid " ) ;
exit_without_destructors ( 1 ) ;
}
if ( owner = = shell_pgid )
{
/* Success */
break ;
}
else
{
if ( check_for_orphaned_process ( loop_count , shell_pgid ) )
{
/* We're orphaned, so we just die. Another sad statistic. */
debug ( 1 ,
_ ( L " I appear to be an orphaned process, so I am quitting politely. My pid is %d. " ) , ( int ) getpid ( ) ) ;
exit_without_destructors ( 1 ) ;
}
/* Try stopping us */
int ret = killpg ( shell_pgid , SIGTTIN ) ;
if ( ret < 0 )
{
wperror ( L " killpg " ) ;
exit_without_destructors ( 1 ) ;
}
}
2012-11-19 08:30:30 +08:00
}
signal_set_handlers ( ) ;
for ( i = 0 ; i < block_count ; i + + )
{
signal_block ( ) ;
}
}
/* Put ourselves in our own process group. */
shell_pgid = getpid ( ) ;
if ( getpgrp ( ) ! = shell_pgid )
{
if ( setpgid ( shell_pgid , shell_pgid ) < 0 )
{
debug ( 1 ,
_ ( L " Couldn't put the shell in its own process group " ) ) ;
wperror ( L " setpgid " ) ;
2012-12-06 05:33:07 +08:00
exit_without_destructors ( 1 ) ;
2012-11-19 08:30:30 +08:00
}
}
/* Grab control of the terminal. */
if ( tcsetpgrp ( STDIN_FILENO , shell_pgid ) )
{
debug ( 1 ,
_ ( L " Couldn't grab control of terminal " ) ) ;
wperror ( L " tcsetpgrp " ) ;
exit_without_destructors ( 1 ) ;
}
common_handle_winch ( 0 ) ;
if ( tcsetattr ( 0 , TCSANOW , & shell_modes ) ) /* set the new modes */
{
wperror ( L " tcsetattr " ) ;
}
/*
We need to know our own pid so we ' ll later know if we are a
fork
*/
original_pid = getpid ( ) ;
env_set ( L " _ " , L " fish " , ENV_GLOBAL ) ;
}
/**
Destroy data for interactive use
*/
static void reader_interactive_destroy ( )
{
kill_destroy ( ) ;
writestr ( L " \n " ) ;
set_color ( rgb_color_t : : reset ( ) , rgb_color_t : : reset ( ) ) ;
input_destroy ( ) ;
}
void reader_sanity_check ( )
{
2014-01-03 04:37:50 +08:00
/* Note: 'data' is non-null if we are interactive, except in the testing environment */
if ( get_is_interactive ( ) & & data ! = NULL )
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:47:15 +08:00
if ( data - > command_line . position > data - > command_line . size ( ) )
2012-11-19 08:30:30 +08:00
sanity_lose ( ) ;
2007-10-06 18:54:53 +08:00
2014-01-27 06:55:41 +08:00
if ( data - > colors . size ( ) ! = data - > command_line . size ( ) )
2012-02-07 03:34:49 +08:00
sanity_lose ( ) ;
2012-11-19 08:30:30 +08:00
2014-01-27 06:55:41 +08:00
if ( data - > indents . size ( ) ! = data - > command_line . size ( ) )
2012-02-07 03:34:49 +08:00
sanity_lose ( ) ;
2012-11-19 08:30:30 +08:00
}
2005-09-20 21:26:39 +08:00
}
2012-06-13 00:41:30 +08:00
/**
2013-07-17 15:38:04 +08:00
Set the specified string as the current buffer .
2012-06-13 00:41:30 +08:00
*/
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 )
2012-06-13 00:41:30 +08:00
{
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
}
2013-09-22 07:27:37 +08:00
static void reader_replace_current_token ( const wchar_t * new_token )
2005-09-20 21:26:39 +08:00
{
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
2012-11-19 08:30:30 +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
2012-11-19 08:30:30 +08:00
if ( ! begin | | ! end )
return ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +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 ) ;
2012-11-19 08:30:30 +08:00
new_pos = ( begin - buff ) + wcslen ( new_token ) ;
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
}
2006-01-24 04:40:14 +08:00
/**
Reset the data structures associated with the token search
*/
2005-09-25 00:31:22 +08:00
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 ( ) ;
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 ;
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
2012-06-13 00:41:30 +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
}
2005-09-20 21:26:39 +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
*/
2012-11-19 08:30:30 +08:00
static void handle_token_history ( int forward , int reset )
2005-09-20 21:26:39 +08:00
{
2012-03-26 16:21:10 +08:00
/* Paranoia */
if ( ! data )
return ;
2012-11-19 08:30:30 +08:00
const wchar_t * str = 0 ;
long current_pos ;
if ( reset )
{
/*
Start a new token search using the current token
*/
reset_token_history ( ) ;
}
current_pos = data - > token_history_pos ;
if ( forward | | data - > search_pos + 1 < data - > search_prev . size ( ) )
{
if ( forward )
{
if ( data - > search_pos > 0 )
{
data - > search_pos - - ;
}
2012-02-03 06:27:13 +08:00
str = data - > search_prev . at ( data - > search_pos ) . c_str ( ) ;
2012-11-19 08:30:30 +08:00
}
else
{
data - > search_pos + + ;
2012-02-03 06:27:13 +08:00
str = data - > search_prev . at ( data - > search_pos ) . c_str ( ) ;
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 ( ) ;
}
else
{
if ( current_pos = = - 1 )
{
2012-02-06 15:22:18 +08:00
data - > token_history_buff . clear ( ) ;
2012-11-19 08:30:30 +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 ( ) ;
}
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 .
*/
const wcstring & last = data - > search_prev . back ( ) ;
if ( data - > search_buff ! = last )
{
str = wcsdup ( data - > search_buff . c_str ( ) ) ;
}
else
{
return ;
}
}
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 ) ;
for ( ; tok_has_next ( & tok ) ; tok_next ( & tok ) )
2012-11-19 08:30:30 +08:00
{
switch ( tok_last_type ( & tok ) )
{
2012-11-19 16:31:03 +08:00
case TOK_STRING :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( wcsstr ( tok_last ( & tok ) , data - > search_buff . c_str ( ) ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
//debug( 3, L"Found token at pos %d\n", tok_get_pos( &tok ) );
if ( tok_get_pos ( & tok ) > = current_pos )
{
break ;
}
//debug( 3, L"ok pos" );
const wcstring last_tok = tok_last ( & tok ) ;
if ( find ( data - > search_prev . begin ( ) , data - > search_prev . end ( ) , last_tok ) = = data - > search_prev . end ( ) )
{
data - > token_history_pos = tok_get_pos ( & tok ) ;
str = wcsdup ( tok_last ( & tok ) ) ;
}
2012-11-19 08:30:30 +08:00
}
}
2013-10-01 04:57:36 +08:00
break ;
2013-10-27 06:27:39 +08:00
2013-10-01 04:57:36 +08:00
default :
{
break ;
}
2012-11-19 08:30:30 +08:00
}
}
}
if ( str )
{
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 ) ;
2012-11-19 08:30:30 +08:00
}
else if ( ! reader_interrupted ( ) )
{
data - > token_history_pos = - 1 ;
handle_token_history ( 0 , 0 ) ;
}
}
2005-09-20 21:26:39 +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 .
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
\ param dir Direction to move / erase . 0 means move left , 1 means move right .
\ param erase Whether to erase the characters along the way or only move past them .
2008-01-14 00:47:47 +08:00
\ param new if the new kill item should be appended to the previous kill item or not .
2005-09-20 21:26:39 +08:00
*/
2012-11-19 08:30:30 +08:00
enum move_word_dir_t
{
2012-10-05 05:35:03 +08:00
MOVE_DIR_LEFT ,
MOVE_DIR_RIGHT
} ;
2012-11-19 18:41:57 +08:00
2014-01-27 06:47:15 +08:00
static void move_word ( editable_line_t * el , bool move_right , bool erase , enum move_word_style_t style , bool newv )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
/* 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 ;
2014-01-27 06:47:15 +08:00
if ( el - > position = = boundary )
2012-10-05 05:35:03 +08:00
return ;
2012-11-19 08:30:30 +08:00
2012-11-19 18:41:57 +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 ) ;
2014-01-27 06:47:15 +08:00
const wchar_t * const command_line = el - > text . c_str ( ) ;
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 ;
2012-11-19 18:41:57 +08:00
while ( buff_pos ! = boundary )
2012-10-05 05:35:03 +08:00
{
2012-11-19 18:41:57 +08:00
size_t idx = ( move_right ? buff_pos : buff_pos - 1 ) ;
wchar_t c = command_line [ idx ] ;
2012-10-05 05:35:03 +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
2012-11-19 18:41:57 +08:00
/* Always consume at least one character */
if ( buff_pos = = start_buff_pos )
buff_pos = ( move_right ? buff_pos + 1 : buff_pos - 1 ) ;
2014-01-27 06:55:41 +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(). */
2012-11-19 08:30:30 +08:00
if ( erase )
{
2012-11-19 18:41:57 +08:00
/* Don't autosuggest after a kill */
2014-01-27 06:55:41 +08:00
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
2012-11-19 18:41:57 +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 ) ;
2012-11-19 18:41:57 +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
}
2012-11-19 08:30:30 +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 ( ) ;
}
2005-09-20 21:26:39 +08:00
2012-11-19 18:41:57 +08:00
}
2005-09-20 21:26:39 +08:00
2012-02-06 17:45:16 +08:00
const wchar_t * reader_get_buffer ( void )
2005-09-20 21:26:39 +08:00
{
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
}
2012-11-19 08:30:30 +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
}
2014-01-21 04:38:56 +08:00
/* Sets the command line contents, without clearing the pager */
static void reader_set_buffer_maintaining_pager ( const wcstring & b , size_t pos )
2005-09-20 21:26:39 +08:00
{
2012-02-06 17:45:16 +08:00
/* 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
2012-08-05 04:02:44 +08:00
/* Don't set a position past the command line length */
if ( pos > command_line_len )
pos = command_line_len ;
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
2014-01-21 04:38:56 +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
}
2014-01-21 04:38:56 +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 ) ;
}
2005-09-20 21:26:39 +08:00
2012-08-05 04:47:56 +08:00
size_t reader_get_cursor_pos ( )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +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
}
2014-03-30 05:19:45 +08:00
bool reader_get_selection ( size_t * start , size_t * len )
2014-01-15 22:07:22 +08:00
{
2014-03-31 04:13:35 +08:00
bool result = false ;
if ( data ! = NULL & & data - > sel_active )
2014-01-15 22:07:22 +08:00
{
2014-03-31 04:13:35 +08:00
* start = data - > sel_start_pos ;
* len = std : : min ( data - > sel_stop_pos - data - > sel_start_pos + 1 , data - > command_line . size ( ) ) ;
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
}
2009-11-24 23:28:42 +08:00
# define ENV_CMD_DURATION L"CMD_DURATION"
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
2012-11-19 08:30:30 +08:00
if ( after - > tv_usec < before - > tv_usec )
{
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 ) ) ;
env_set ( ENV_CMD_DURATION , buf , ENV_EXPORT ) ;
2009-11-24 23:28:42 +08:00
}
2005-09-20 21:26:39 +08:00
2013-04-08 03:40:08 +08:00
void reader_run_command ( parser_t & parser , const wcstring & cmd )
2005-09-20 21:26:39 +08:00
{
2006-01-31 03:53:10 +08:00
2012-11-19 08:30:30 +08:00
struct timeval time_before , time_after ;
2005-09-20 21:26:39 +08:00
2013-04-08 03:40:08 +08:00
wcstring ft = tok_first ( cmd . c_str ( ) ) ;
2005-09-20 21:26:39 +08:00
2012-11-22 14:23:48 +08:00
if ( ! ft . empty ( ) )
env_set ( L " _ " , ft . c_str ( ) , ENV_GLOBAL ) ;
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
2012-11-19 08:30:30 +08:00
env_set ( L " _ " , program_name , ENV_GLOBAL ) ;
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
2006-01-31 03:53:10 +08:00
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
int reader_shell_test ( const wchar_t * b )
2005-09-20 21:26:39 +08:00
{
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
2014-01-09 10:20:55 +08:00
/* Append a newline, to act as a statement terminator */
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 ;
2014-07-25 05:41:15 +08:00
int res = parse_util_detect_errors ( bstr , & errors , true /* do accept incomplete */ ) ;
2012-11-19 08:30:30 +08:00
if ( res & PARSER_TEST_ERROR )
{
2014-01-09 10:20:55 +08:00
wcstring error_desc ;
parser_t : : principal_parser ( ) . get_backtrace ( bstr , errors , & error_desc ) ;
2014-01-15 17:40:40 +08:00
2013-12-17 08:52:23 +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
2014-01-09 10:20:55 +08:00
if ( ! string_suffixes_string ( L " \n " , error_desc ) )
2013-12-17 08:52:23 +08:00
{
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
}
/**
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 .
*/
2012-11-19 08:30:30 +08:00
static int default_test ( const wchar_t * b )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
return 0 ;
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
void reader_push ( const wchar_t * name )
2005-09-20 21:26:39 +08:00
{
2012-07-18 03:47:01 +08:00
reader_data_t * n = new reader_data_t ( ) ;
2012-11-19 08:30:30 +08:00
2012-02-06 08:42:24 +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
2012-11-19 08:30:30 +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
2012-11-19 08:30:30 +08:00
if ( data - > next = = 0 )
{
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
}
void reader_pop ( )
{
2012-11-19 08:30:30 +08:00
reader_data_t * n = data ;
if ( data = = 0 )
{
debug ( 0 , _ ( L " Pop null reader block " ) ) ;
sanity_lose ( ) ;
return ;
}
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
data = data - > next ;
2005-09-20 21:26:39 +08:00
2012-02-01 12:03:48 +08:00
/* Invoke the destructor to balance our new */
delete n ;
2006-01-31 03:53:10 +08:00
2012-11-19 08:30:30 +08:00
if ( data = = 0 )
{
reader_interactive_destroy ( ) ;
}
else
{
end_loop = 0 ;
//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
}
2012-11-19 08:30:30 +08:00
void reader_set_left_prompt ( const wcstring & new_prompt )
2012-11-08 11:59:20 +08:00
{
data - > left_prompt = new_prompt ;
}
void reader_set_right_prompt ( const wcstring & new_prompt )
2005-09-20 21:26:39 +08:00
{
2012-11-08 11:59:20 +08:00
data - > right_prompt = new_prompt ;
2005-09-20 21:26:39 +08:00
}
2012-11-18 18:43:35 +08:00
void reader_set_allow_autosuggesting ( bool flag )
{
data - > allow_autosuggestion = flag ;
}
2013-07-17 15:38:04 +08:00
void reader_set_expand_abbreviations ( bool flag )
{
data - > expand_abbreviations = flag ;
}
2012-11-19 08:30:30 +08:00
void reader_set_complete_function ( complete_function_t f )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
data - > complete_func = f ;
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
void reader_set_highlight_function ( highlight_function_t func )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
data - > highlight_function = func ;
2005-09-20 21:26:39 +08:00
}
2012-11-19 08:30:30 +08:00
void reader_set_test_function ( int ( * f ) ( const wchar_t * ) )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
data - > test_func = f ;
2005-09-20 21:26:39 +08:00
}
2013-01-22 18:25:17 +08:00
void reader_set_exit_on_interrupt ( bool i )
2013-01-21 05:23:27 +08:00
{
2013-01-22 18:25:17 +08:00
data - > exit_on_interrupt = i ;
2013-01-21 05:23:27 +08:00
}
2012-07-10 13:54:08 +08:00
void reader_import_history_if_necessary ( void )
{
/* Import history from bash, etc. if our current history is 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.
*/
const env_var_t var = env_get_string ( L " HISTFILE " ) ;
wcstring path = ( var . missing ( ) ? L " ~/.bash_history " : var ) ;
expand_tilde ( path ) ;
FILE * f = wfopen ( path , " r " ) ;
if ( f )
{
data - > history - > populate_from_bash ( f ) ;
fclose ( f ) ;
}
}
}
2011-12-27 11:18:46 +08:00
/** A class as the context pointer for a background (threaded) highlight operation. */
2012-11-19 08:30:30 +08:00
class background_highlight_context_t
{
2011-12-27 11:18:46 +08:00
public :
2012-02-29 04:40:20 +08:00
/** The string to highlight */
2012-11-19 08:30:30 +08:00
const wcstring string_to_highlight ;
/** Color buffer */
2014-01-15 17:01:25 +08:00
std : : vector < highlight_spec_t > colors ;
2012-11-19 08:30:30 +08:00
/** The position to use for bracket matching */
const size_t match_highlight_pos ;
/** Function for syntax highlighting */
const highlight_function_t highlight_function ;
2012-02-29 04:40:20 +08:00
/** Environment variables */
2012-07-21 11:39:31 +08:00
const env_vars_snapshot_t vars ;
2011-12-27 11:18:46 +08:00
2012-02-29 04:40:20 +08:00
/** When the request was made */
2012-02-04 10:39:41 +08:00
const double when ;
2012-11-19 08:30:30 +08:00
2012-02-29 04:40:20 +08:00
/** Gen count at the time the request was made */
const unsigned int generation_count ;
2012-11-19 08:30:30 +08:00
2012-08-05 04:02:44 +08:00
background_highlight_context_t ( const wcstring & pbuff , size_t phighlight_pos , highlight_function_t phighlight_func ) :
2012-02-06 17:45:16 +08:00
string_to_highlight ( pbuff ) ,
2012-08-05 04:02:44 +08:00
colors ( pbuff . size ( ) , 0 ) ,
2012-02-04 10:39:41 +08:00
match_highlight_pos ( phighlight_pos ) ,
highlight_function ( phighlight_func ) ,
2012-07-21 11:39:31 +08:00
vars ( env_vars_snapshot_t : : highlighting_keys ) ,
2012-02-29 04:40:20 +08:00
when ( timef ( ) ) ,
generation_count ( s_generation_count )
2011-12-27 11:18:46 +08:00
{
}
2012-11-19 08:30:30 +08:00
2014-03-27 09:49:09 +08:00
int perform_highlight ( )
2012-11-19 08:30:30 +08:00
{
2012-08-05 04:02:44 +08:00
if ( generation_count ! = s_generation_count )
{
2012-02-29 04:40:20 +08:00
// The gen count has changed, so don't do anything
return 0 ;
}
2012-08-05 04:02:44 +08:00
if ( ! string_to_highlight . empty ( ) )
{
2012-11-19 08:30:30 +08:00
highlight_function ( string_to_highlight , colors , match_highlight_pos , NULL /* error */ , vars ) ;
2012-02-18 10:24:47 +08:00
}
return 0 ;
2011-12-27 11:18:46 +08:00
}
} ;
2012-02-04 12:05:50 +08:00
/* Called to set the highlight flag for search results */
2012-11-19 08:30:30 +08:00
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 ) ;
2013-01-05 14:32:40 +08:00
if ( match_pos ! = wcstring : : npos )
2012-11-19 08:30:30 +08:00
{
2013-01-05 14:32:40 +08:00
size_t end = match_pos + needle . size ( ) ;
for ( size_t i = match_pos ; i < end ; i + + )
2012-11-19 08:30:30 +08:00
{
2014-01-15 17:01:25 +08:00
data - > colors . at ( i ) | = ( highlight_spec_search_match < < 16 ) ;
2012-11-19 08:30:30 +08:00
}
}
}
}
static void highlight_complete ( background_highlight_context_t * ctx , int result )
{
2012-02-06 08:42:24 +08:00
ASSERT_IS_MAIN_THREAD ( ) ;
2014-01-26 16:41:30 +08:00
if ( ctx - > string_to_highlight = = data - > command_line . text )
2012-11-19 08:30:30 +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. */
2014-01-27 06:55:41 +08:00
assert ( ctx - > colors . size ( ) = = data - > command_line . size ( ) ) ;
2012-10-04 03:19:27 +08:00
if ( data - > colors ! = ctx - > colors )
{
data - > colors . swap ( ctx - > colors ) ;
sanity_check ( ) ;
highlight_search ( ) ;
reader_repaint ( ) ;
}
2012-11-19 08:30:30 +08:00
}
/* Free our context */
2011-12-27 11:18:46 +08:00
delete ctx ;
}
2012-11-19 08:30:30 +08:00
static int threaded_highlight ( background_highlight_context_t * ctx )
{
2014-03-27 09:49:09 +08:00
return ctx - > perform_highlight ( ) ;
2011-12-27 11:18:46 +08:00
}
2005-09-20 21:26:39 +08:00
/**
Call specified external highlighting function and then do search
2006-09-25 01:57:23 +08:00
highlighting . Lastly , clear the background color under the cursor
2013-01-05 14:32:40 +08:00
to avoid repaint issues on terminals where e . g . syntax highlighting
2006-10-22 17:40:18 +08:00
maykes characters under the sursor unreadable .
2006-09-25 01:57:23 +08:00
2014-01-27 06:47:15 +08:00
\ param match_highlight_pos_adjust the adjustment to the position to use for bracket matching . This is added to the current cursor position and may be negative .
2006-10-16 23:32:26 +08:00
\ param error if non - null , any possible errors in the buffer are further descibed by the strings inserted into the specified arraylist
2014-03-27 09:49:09 +08:00
\ param no_io if true , do a highlight that does not perform I / O , synchronously . If false , perform an asynchronous highlight in the background , which may perform disk I / O .
2005-09-20 21:26:39 +08:00
*/
2014-03-27 09:49:09 +08:00
static void reader_super_highlight_me_plenty ( int match_highlight_pos_adjust , bool no_io )
2005-09-20 21:26:39 +08:00
{
2014-01-27 06:47:15 +08:00
const editable_line_t * el = & data - > command_line ;
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
2014-03-27 09:49:09 +08:00
highlight_function_t highlight_func = no_io ? highlight_shell_no_io : data - > highlight_function ;
background_highlight_context_t * ctx = new background_highlight_context_t ( el - > text , match_highlight_pos , highlight_func ) ;
if ( no_io )
{
// Highlighting without IO, we just do it
// Note that highlight_complete deletes ctx.
int result = ctx - > perform_highlight ( ) ;
highlight_complete ( ctx , result ) ;
}
else
{
// Highlighting including I/O proceeds in the background
2014-04-01 01:01:39 +08:00
iothread_perform ( threaded_highlight , highlight_complete , ctx ) ;
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
2012-02-16 16:24:27 +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 ;
2012-11-19 08:30:30 +08:00
if ( can_autosuggest ( ) & & ! suggest . empty ( ) & & string_prefixes_string_case_insensitive ( cmd , suggest ) )
{
2012-02-16 16:24:27 +08:00
/* The autosuggestion is still reasonable, so do nothing */
2012-11-19 08:30:30 +08:00
}
else
{
2012-02-16 16:24:27 +08:00
update_autosuggestion ( ) ;
}
2005-09-20 21:26:39 +08:00
}
2013-12-30 08:23:26 +08:00
bool shell_is_exiting ( )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
if ( get_is_interactive ( ) )
2014-01-03 04:37:50 +08:00
return job_list_is_empty ( ) & & data ! = NULL & & data - > end_loop ;
2012-11-19 08:30:30 +08:00
else
return end_loop ;
2005-09-20 21:26:39 +08:00
}
2007-09-26 17:01:59 +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 ( )
{
2012-11-19 08:30:30 +08:00
job_t * j ;
2012-11-07 20:23:04 +08:00
int stopped_jobs_count = 0 ;
2012-11-19 08:30:30 +08:00
int is_breakpoint = 0 ;
2013-12-21 09:41:21 +08:00
const parser_t & parser = parser_t : : principal_parser ( ) ;
2012-11-19 08:30:30 +08:00
2013-12-21 09:41:21 +08:00
for ( size_t i = 0 ; i < parser . block_count ( ) ; i + + )
2012-11-19 08:30:30 +08:00
{
2013-12-21 09:41:21 +08:00
if ( parser . block_at_index ( i ) - > type ( ) = = BREAKPOINT )
2012-11-19 08:30:30 +08:00
{
is_breakpoint = 1 ;
break ;
}
}
2012-01-30 08:36:21 +08:00
job_iterator_t jobs ;
while ( ( j = jobs . next ( ) ) )
2012-11-19 08:30:30 +08:00
{
2012-11-07 20:23:04 +08:00
if ( ! job_is_completed ( j ) & & ( job_is_stopped ( j ) ) )
2012-11-19 08:30:30 +08:00
{
2012-11-07 20:23:04 +08:00
stopped_jobs_count + + ;
2012-11-19 08:30:30 +08:00
break ;
}
}
2012-11-07 20:23:04 +08:00
if ( ! reader_exit_forced ( ) & & ! data - > prev_end_loop & & stopped_jobs_count & & ! is_breakpoint )
2012-11-19 08:30:30 +08:00
{
writestr ( _ ( L " There are stopped jobs. A second attempt to exit will enforce their termination. \n " ) ) ;
reader_exit ( 0 , 0 ) ;
data - > prev_end_loop = 1 ;
}
else
{
2012-07-16 03:46:40 +08:00
/* PCA: we used to only hangup jobs if stdin was closed. This prevented child processes from exiting. It's unclear to my why it matters if stdin is closed, but it seems to me if we're forcing an exit, we definitely want to hang up our processes.
2012-11-19 08:30:30 +08:00
2012-07-16 03:46:40 +08:00
See https : //github.com/fish-shell/fish-shell/issues/138
*/
2012-11-19 08:30:30 +08:00
if ( reader_exit_forced ( ) | | ! isatty ( 0 ) )
{
/*
We already know that stdin is a tty since we ' re
in interactive mode . If isatty returns false , it
means stdin must have been closed .
*/
job_iterator_t jobs ;
while ( ( j = jobs . next ( ) ) )
{
if ( ! job_is_completed ( j ) )
{
job_signal ( j , SIGHUP ) ;
}
}
}
}
2007-09-26 17:01:59 +08:00
}
2014-01-27 18:17:31 +08:00
static bool selection_is_at_top ( )
{
const pager_t * pager = & data - > pager ;
size_t row = pager - > get_selected_row ( data - > current_page_rendering ) ;
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 ) ;
if ( col ! = 0 & & col ! = PAGER_SELECTION_NONE )
return false ;
return true ;
}
2005-09-20 21:26:39 +08:00
/**
Read interactively . Read input from stdin while providing editing
facilities .
*/
2013-04-08 03:40:08 +08:00
static int read_i ( void )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
reader_push ( L " fish " ) ;
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 ( ) ;
data - > prev_end_loop = 0 ;
while ( ( ! data - > end_loop ) & & ( ! sanity_check ( ) ) )
{
event_fire_generic ( L " fish_prompt " ) ;
if ( function_exists ( LEFT_PROMPT_FUNCTION_NAME ) )
reader_set_left_prompt ( LEFT_PROMPT_FUNCTION_NAME ) ;
else
reader_set_left_prompt ( DEFAULT_PROMPT ) ;
if ( function_exists ( RIGHT_PROMPT_FUNCTION_NAME ) )
reader_set_right_prompt ( RIGHT_PROMPT_FUNCTION_NAME ) ;
else
reader_set_right_prompt ( L " " ) ;
/*
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
if ( data - > end_loop )
{
handle_end_loop ( ) ;
}
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 ) ;
2013-04-08 03:40:08 +08:00
reader_run_command ( parser , command ) ;
2012-11-19 08:30:30 +08:00
if ( data - > end_loop )
{
handle_end_loop ( ) ;
}
else
{
data - > prev_end_loop = 0 ;
}
}
}
reader_pop ( ) ;
return 0 ;
2005-09-20 21:26:39 +08:00
}
2005-10-20 20:06:10 +08:00
/**
Test if there are bytes available for reading on the specified file
descriptor
*/
2012-11-19 08:30:30 +08:00
static int can_read ( int fd )
2005-10-13 22:08:33 +08:00
{
2012-11-19 08:30:30 +08:00
struct timeval can_read_timeout = { 0 , 0 } ;
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
2005-10-20 19:27:54 +08:00
/**
2005-10-20 20:06:10 +08:00
Test if the specified character is in the private use area that
fish uses to store internal characters
2005-10-20 19:27:54 +08:00
*/
2012-11-19 08:30:30 +08:00
static int wchar_private ( wchar_t c )
2005-10-20 19:27:54 +08:00
{
2012-11-19 08:30:30 +08:00
return ( ( c > = 0xe000 ) & & ( c < = 0xf8ff ) ) ;
2005-10-20 19:27:54 +08:00
}
2005-09-20 21:26:39 +08:00
2006-11-20 10:19:34 +08:00
/**
Test if the specified character in the specified string is
2013-02-03 06:50:22 +08:00
backslashed . pos may be at the end of the string , which indicates
if there is a trailing backslash .
2006-11-20 10:19:34 +08:00
*/
2013-02-03 06:50:22 +08:00
static bool is_backslashed ( const wcstring & str , size_t pos )
2006-11-20 10:19:34 +08:00
{
2013-02-03 06:50:22 +08:00
/* 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 ;
2012-11-19 08:30:30 +08:00
while ( idx - - )
{
2013-02-03 06:50:22 +08:00
if ( str . at ( idx ) ! = L ' \\ ' )
2012-11-19 08:30:30 +08:00
break ;
count + + ;
}
return ( count % 2 ) = = 1 ;
2006-11-20 10:19:34 +08:00
}
2013-02-03 06:50:22 +08:00
static wchar_t unescaped_quote ( const wcstring & str , size_t pos )
{
wchar_t result = L ' \0 ' ;
if ( pos < str . size ( ) )
{
wchar_t c = str . at ( pos ) ;
if ( ( c = = L ' \' ' | | c = = L ' " ' ) & & ! is_backslashed ( str , pos ) )
{
result = c ;
}
}
return result ;
}
2006-11-20 10:19:34 +08:00
2014-08-19 20:28:08 +08:00
const wchar_t * reader_readline ( int nchars )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
wint_t c ;
int last_char = 0 ;
2012-08-05 04:47:56 +08:00
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 ;
int finished = 0 ;
struct termios old_modes ;
2012-12-03 18:25:08 +08:00
2012-11-25 13:06:42 +08:00
/* Coalesce redundant repaints. When we get a repaint, we set this to true, and skip repaints until we get something else. */
bool coalescing_repaints = false ;
2012-08-22 08:18:52 +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 ( ) ;
/*
2012-08-05 04:47:56 +08:00
get the current terminal modes . These will be restored when the
function returns .
*/
2012-11-19 08:30:30 +08:00
tcgetattr ( 0 , & old_modes ) ;
/* set the new modes */
if ( tcsetattr ( 0 , TCSANOW , & shell_modes ) )
{
wperror ( L " tcsetattr " ) ;
}
while ( ! finished & & ! data - > end_loop )
{
2014-08-19 20:28:08 +08:00
if ( 0 < nchars & & ( size_t ) nchars < = data - > command_line . size ( ) )
{
// we've already hit the specified character limit
finished = 1 ;
break ;
}
2012-11-19 08:30:30 +08:00
/*
2012-08-05 04:47:56 +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 )
*/
2012-11-19 08:30:30 +08:00
while ( 1 )
{
int was_interactive_read = is_interactive_read ;
is_interactive_read = 1 ;
c = input_readch ( ) ;
is_interactive_read = was_interactive_read ;
2013-12-27 19:58:42 +08:00
//fprintf(stderr, "C: %lx\n", (long)c);
2012-11-19 08:30:30 +08:00
if ( ( ( ! wchar_private ( c ) ) ) & & ( c > 31 ) & & ( c ! = 127 ) )
{
if ( can_read ( 0 ) )
2012-08-22 08:18:52 +08:00
{
2012-11-19 08:30:30 +08:00
wchar_t arr [ READAHEAD_MAX + 1 ] ;
2014-08-19 20:28:08 +08:00
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
memset ( arr , 0 , sizeof ( arr ) ) ;
arr [ 0 ] = c ;
2014-08-19 20:28:08 +08:00
for ( i = 1 ; i < limit ; + + i )
2012-08-22 08:18:52 +08:00
{
2012-11-19 08:30:30 +08:00
if ( ! can_read ( 0 ) )
{
c = 0 ;
break ;
}
Fix `commandline` behavior in bind functions
When a key is bound to a fish function, if that function invokes
`commandline`, it gets a stale copy of the commandline. This is because
any keys passed to `self-insert` (the default) don't actually get added
to the commandline until a special character is processed, such as the
R_NULL that gets returned after running a binding for a fish command.
To fix this, don't allow fish commands to be run for bindings if we're
processing more than one key. When a key wants to invoke a fish command,
instead we push the invocation sequence back onto the input, followed by
an R_NULL, and return. This causes the input loop to break out and
update the commandline. When it starts up again, it will re-process the
keys and invoke the fish command.
This is primarily an issue with pasting text that includes bound keys in
it. Typed text is slow enough that fish will update the commandline
between each character.
---
I don't know of any way to write a test for this, but the issue can be
reproduced as follows:
> bind _ 'commandline -i _'
This binds _ to a command that inserts _. Typing the following works:
> echo wat_is_it
But if you copy that line and paste it instead of typing it, the end
result looks like
> _echo wat_isit
With this fix in place, the pasted output correctly matches the typed
output.
2014-08-23 05:02:29 +08:00
// 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.
2014-09-04 14:05:48 +08:00
c = input_readch ( false ) ;
2012-11-19 08:30:30 +08:00
if ( ( ! wchar_private ( c ) ) & & ( c > 31 ) & & ( c ! = 127 ) )
{
arr [ i ] = c ;
c = 0 ;
}
else
break ;
2012-08-22 08:18:52 +08:00
}
2012-11-19 08:30:30 +08:00
2014-07-26 03:23:42 +08:00
insert_string ( & data - > command_line , arr , true ) ;
2012-11-19 08:30:30 +08:00
2012-08-22 08:18:52 +08:00
}
2012-11-19 08:30:30 +08:00
}
if ( c ! = 0 )
break ;
2014-08-19 20:28:08 +08:00
if ( 0 < nchars & & ( size_t ) nchars < = data - > command_line . size ( ) )
{
c = R_NULL ;
break ;
}
2012-11-19 08:30:30 +08:00
}
2012-11-25 13:06:42 +08:00
/* If we get something other than a repaint, then stop coalescing them */
if ( c ! = R_REPAINT )
coalescing_repaints = false ;
2012-11-19 08:30:30 +08:00
if ( last_char ! = R_YANK & & last_char ! = R_YANK_POP )
yank_len = 0 ;
2012-12-03 18:25:08 +08:00
2014-01-27 18:28:19 +08:00
/* Restore the text */
if ( c = = R_CANCEL & & data - > is_navigating_pager_contents ( ) )
2012-11-19 08:30:30 +08:00
{
2014-01-27 18:28:19 +08:00
set_command_line_and_position ( & data - > command_line , data - > cycle_command_line , data - > cycle_cursor_pos ) ;
}
2014-04-01 01:01:39 +08:00
2014-01-27 16:56:13 +08:00
/* Clear the pager if necessary */
bool focused_on_search_field = ( data - > active_edit_line ( ) = = & data - > pager . search_field_line ) ;
if ( command_ends_paging ( c , focused_on_search_field ) )
2014-01-20 15:52:35 +08:00
{
2014-01-27 16:56:13 +08:00
clear_pager ( ) ;
2014-01-20 15:52:35 +08:00
}
2014-03-30 05:19:45 +08:00
2014-01-22 08:25:55 +08:00
//fprintf(stderr, "\n\nchar: %ls\n\n", describe_char(c).c_str());
2012-11-19 08:30:30 +08:00
switch ( c )
{
2012-11-19 16:31:03 +08:00
/* go to beginning of line*/
case R_BEGINNING_OF_LINE :
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:47:15 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
while ( el - > position > 0 & & el - > text . at ( el - > position - 1 ) ! = L ' \n ' )
2012-11-19 16:31:03 +08:00
{
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
}
2012-11-19 16:31:03 +08:00
case R_END_OF_LINE :
{
2014-01-27 06:47:15 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
if ( el - > position < el - > size ( ) )
2012-11-19 16:31:03 +08:00
{
2014-01-27 06:47:15 +08:00
const wchar_t * buff = el - > text . c_str ( ) ;
while ( buff [ el - > position ] & &
buff [ el - > position ] ! = L ' \n ' )
2013-07-27 11:35:34 +08:00
{
2014-03-30 05:19:45 +08:00
update_buff_pos ( el , el - > position + 1 ) ;
2013-07-27 11:35:34 +08:00
}
}
else
{
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 ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +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 ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* go to EOL*/
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-15 18:39:19 +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
2012-11-19 16:31:03 +08:00
case R_NULL :
{
break ;
}
2014-04-01 01:01:39 +08:00
2014-01-27 18:17:31 +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 ;
}
2012-11-19 08:30:30 +08:00
2013-12-31 21:53:29 +08:00
case R_FORCE_REPAINT :
2012-11-19 16:31:03 +08:00
case R_REPAINT :
{
2012-11-25 13:06:42 +08:00
if ( ! coalescing_repaints )
{
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 ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case R_EOF :
{
exit_forced = 1 ;
data - > end_loop = 1 ;
2012-11-19 08:30:30 +08:00
break ;
2012-11-19 16:31:03 +08:00
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* complete */
case R_COMPLETE :
2014-01-27 16:56:13 +08:00
case R_COMPLETE_AND_SEARCH :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( ! data - > complete_func )
break ;
2014-01-27 06:47:15 +08:00
/* Use the command line only; it doesn't make sense to complete in any other line */
editable_line_t * el = & data - > command_line ;
2014-01-27 16:56:13 +08:00
if ( data - > is_navigating_pager_contents ( ) | | ( ! comp_empty & & last_char = = R_COMPLETE ) )
2012-11-19 08:30:30 +08:00
{
2014-02-21 05:30:26 +08:00
/* 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 )
2012-11-19 16:31:03 +08:00
{
2014-02-21 05:30:26 +08:00
data - > pager . set_fully_disclosed ( true ) ;
reader_repaint_needed ( ) ;
}
else
{
select_completion_in_direction ( c = = R_COMPLETE ? direction_next : direction_prev ) ;
2012-11-19 16:31:03 +08:00
}
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
else
{
/* Either the user hit tab only once, or we had no visible completion list. */
2013-05-25 14:53:10 +08:00
2013-05-20 16:40:24 +08:00
/* Remove a trailing backslash. This may trigger an extra repaint, but this is rare. */
2014-01-27 06:47:15 +08:00
if ( is_backslashed ( el - > text , el - > position ) )
2013-05-20 16:40:24 +08:00
{
remove_backward ( ) ;
}
/* Get the string; we have to do this after removing any trailing backslash */
2014-01-27 06:47:15 +08:00
const wchar_t * const buff = el - > text . c_str ( ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* Clear the completion list */
comp . clear ( ) ;
2013-02-04 03:38:22 +08:00
2013-02-03 06:50: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
2013-02-03 06:50: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 ;
2014-01-27 06:47:15 +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
2013-10-13 09:17:03 +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. */
if ( token_end > cmdsub_end ) token_end = cmdsub_end ;
2013-02-04 03:38:22 +08:00
2013-02-03 06:50:22 +08:00
/* Figure out how many steps to get from the current position to the end of the current token. */
size_t end_of_token_offset = token_end - buff ;
/* Move the cursor to the end */
2014-01-27 06:47:15 +08:00
if ( el - > position ! = end_of_token_offset )
2013-02-03 06:50:22 +08:00
{
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
2013-02-03 06:50: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 */
const wcstring buffcpy = wcstring ( cmdsub_begin , token_end ) ;
2013-03-22 08:44:51 +08:00
2013-03-06 12:54:16 +08:00
//fprintf(stderr, "Complete (%ls)\n", buffcpy.c_str());
2013-11-30 15:44:26 +08:00
data - > complete_func ( buffcpy , comp , COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_DESCRIPTIONS | COMPLETION_REQUEST_FUZZY_MATCH ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* Munge our completions */
2013-02-03 06:50:22 +08:00
sort_and_make_unique ( comp ) ;
2012-11-19 16:31:03 +08:00
prioritize_completions ( comp ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +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
2014-01-28 23:30:05 +08:00
bool continue_after_prefix_insertion = ( c = = R_COMPLETE_AND_SEARCH ) ;
comp_empty = handle_completions ( comp , continue_after_prefix_insertion ) ;
2012-11-19 08:30:30 +08:00
2014-01-27 16:56:13 +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 ( ) )
{
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 ( ) ;
}
2013-05-05 17:33:17 +08:00
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
/* kill */
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
2012-11-19 16:31:03 +08:00
while ( * end & & * end ! = L ' \n ' )
end + + ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( end = = begin & & * end )
end + + ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
size_t len = end - begin ;
if ( len )
{
2014-01-27 06:55:41 +08:00
reader_kill ( el , begin - buff , len , KILL_APPEND , last_char ! = R_KILL_LINE ) ;
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 ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
case R_BACKWARD_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
if ( el - > position > 0 )
2012-11-19 16:31:03 +08:00
{
2014-01-27 06:47:15 +08:00
const wchar_t * buff = el - > text . c_str ( ) ;
const wchar_t * end = & buff [ el - > position ] ;
2012-11-19 16:31:03 +08:00
const wchar_t * begin = end ;
2013-02-20 09:48:51 +08:00
2013-02-18 06:22:27 +08:00
/* Make sure we delete at least one character (see #580) */
begin - - ;
2013-02-20 09:48:51 +08:00
2013-02-18 06:22:27 +08:00
/* Delete until we hit a newline, or the beginning of the string */
2012-11-19 16:31:03 +08:00
while ( begin > buff & & * begin ! = L ' \n ' )
begin - - ;
2013-02-20 09:48:51 +08:00
2013-02-18 06:22:27 +08:00
/* If we landed on a newline, don't delete it */
2012-11-19 16:31:03 +08:00
if ( * begin = = L ' \n ' )
begin + + ;
2013-02-20 09:48:51 +08:00
2013-02-18 06:22:27 +08:00
assert ( end > = begin ) ;
2012-11-19 16:31:03 +08:00
size_t len = maxi < size_t > ( end - begin , 1 ) ;
begin = end - len ;
2014-01-27 06:55:41 +08:00
reader_kill ( el , begin - buff , len , KILL_PREPEND , last_char ! = R_BACKWARD_KILL_LINE ) ;
2012-11-19 16:31:03 +08:00
}
break ;
}
case R_KILL_WHOLE_LINE :
2012-11-19 08:30:30 +08:00
{
2014-07-14 05:05:52 +08:00
/* 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
2014-07-14 05:05:52 +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. */
size_t begin = el - > position ;
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
}
/* Push end forwards to just past the next newline, or just past the last char. */
size_t end = el - > position ;
while ( buff [ end ] ! = L ' \0 ' )
{
2012-11-19 16:31:03 +08:00
end + + ;
2014-07-14 05:05:52 +08:00
if ( buff [ end - 1 ] = = L ' \n ' )
{
break ;
}
}
assert ( end > = begin ) ;
2012-11-19 08:30:30 +08:00
2014-07-14 05:05:52 +08:00
if ( end > begin )
2012-11-19 16:31:03 +08:00
{
2014-07-14 05:05:52 +08:00
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
}
2012-11-19 16:31:03 +08:00
/* yank*/
case R_YANK :
2012-11-19 08:30:30 +08:00
{
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
}
2012-11-19 16:31:03 +08:00
/* rotate killring*/
case R_YANK_POP :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( yank_len )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
for ( size_t i = 0 ; i < yank_len ; i + + )
remove_backward ( ) ;
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 ;
}
/* Escape was pressed */
case L ' \x1b ' :
{
if ( data - > search_mode )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
data - > search_mode = NO_SEARCH ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( data - > token_history_pos = = - 1 )
{
//history_reset();
data - > history_search . go_to_end ( ) ;
2014-01-13 05:53:59 +08:00
reader_set_buffer ( data - > search_buff , data - > search_buff . size ( ) ) ;
2012-11-19 16:31:03 +08:00
}
else
{
reader_replace_current_token ( data - > search_buff . c_str ( ) ) ;
}
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 ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* delete backward*/
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
}
2012-11-19 16:31:03 +08:00
/* delete forward*/
case R_DELETE_CHAR :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
/**
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 ( ) ;
if ( el - > position < el - > size ( ) )
2012-11-19 16:31:03 +08:00
{
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 ;
}
2012-11-19 16:31:03 +08:00
/*
Evaluate . If the current command is unfinished , or if
the charater is escaped using a backslash , insert a
newline
*/
case R_EXECUTE :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
/* Delete any autosuggestion */
data - > autosuggestion . clear ( ) ;
2012-11-19 08:30:30 +08:00
2014-01-28 14:01:38 +08:00
/* If the user hits return while navigating the pager, it only clears the pager */
if ( data - > is_navigating_pager_contents ( ) )
{
clear_pager ( ) ;
break ;
}
2014-04-01 01:01:39 +08:00
2014-03-17 07:49:31 +08:00
/* The user may have hit return with pager contents, but while not navigating them. Clear the pager in that event. */
clear_pager ( ) ;
2014-01-27 06:47:15 +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
2013-03-13 04:00:37 +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 #163) */
2014-01-27 06:47:15 +08:00
if ( is_backslashed ( el - > text , el - > position ) )
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:47:15 +08:00
if ( el - > position > = el - > size ( ) | | iswspace ( el - > text . at ( el - > position ) ) )
2013-03-13 04:00:37 +08:00
{
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
}
2013-07-17 15:38:04 +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 ( ) ) ;
2013-07-17 17:55:15 +08:00
if ( command_test_result = = 0 | | command_test_result = = PARSER_TEST_INCOMPLETE )
2013-07-17 15:38:04 +08:00
{
/* 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 ) ;
2013-07-17 15:38:04 +08:00
if ( abbreviation_expanded )
{
2013-07-17 17:55:15 +08:00
/* 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
2014-03-27 09:49:09 +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 */
bool skip_io = ( command_test_result = = 0 ) ;
reader_super_highlight_me_plenty ( 0 , skip_io ) ;
2013-07-17 15:38:04 +08:00
}
}
switch ( command_test_result )
2012-11-19 16:31:03 +08:00
{
case 0 :
{
2013-03-12 01:55:47 +08:00
/* Finished command, execute it. Don't add items that start with a leading space. */
2014-01-27 06:47:15 +08:00
const editable_line_t * el = & data - > command_line ;
if ( data - > history ! = NULL & & ! el - > empty ( ) & & el - > text . at ( 0 ) ! = L ' ' )
2012-11-19 16:31:03 +08:00
{
2014-01-27 06:47:15 +08:00
data - > history - > add_with_file_detection ( el - > text ) ;
2012-11-19 16:31:03 +08:00
}
finished = 1 ;
2014-03-30 05:19:45 +08:00
update_buff_pos ( & data - > command_line , data - > command_line . size ( ) ) ;
2012-11-19 16:31:03 +08:00
reader_repaint ( ) ;
break ;
}
/*
We are incomplete , continue editing
*/
case PARSER_TEST_INCOMPLETE :
{
2014-01-27 16:56:13 +08:00
insert_char ( el , ' \n ' ) ;
2012-11-19 16:31:03 +08:00
break ;
}
/*
Result must be some combination including an
error . The error message will already be
printed , all we need to do is repaint
*/
default :
2012-11-19 08:30:30 +08:00
{
2012-11-25 08:42:25 +08:00
s_reset ( & data - > screen , screen_reset_abandon_line ) ;
2014-01-22 08:25:55 +08:00
reader_repaint_needed ( ) ;
2012-11-19 16:31:03 +08:00
break ;
2012-02-07 03:52:24 +08:00
}
2012-11-19 16:31:03 +08:00
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
/* History functions */
case R_HISTORY_SEARCH_BACKWARD :
case R_HISTORY_TOKEN_SEARCH_BACKWARD :
case R_HISTORY_SEARCH_FORWARD :
case R_HISTORY_TOKEN_SEARCH_FORWARD :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
int reset = 0 ;
if ( data - > search_mode = = NO_SEARCH )
{
reset = 1 ;
if ( ( c = = R_HISTORY_SEARCH_BACKWARD ) | |
( c = = R_HISTORY_SEARCH_FORWARD ) )
{
data - > search_mode = LINE_SEARCH ;
}
else
{
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 ) ;
2012-11-19 16:31:03 +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
2013-08-21 11:08:56 +08:00
/* Skip the autosuggestion as history unless it was truncated */
2012-11-19 16:31:03 +08:00
const wcstring & suggest = data - > autosuggestion ;
2013-08-21 11:08:56 +08:00
if ( ! suggest . empty ( ) & & ! data - > screen . autosuggestion_is_truncated )
2012-11-19 16:31:03 +08:00
{
data - > history_search . skip_matches ( wcstring_list_t ( & suggest , 1 + & suggest ) ) ;
}
2012-07-05 18:33:44 +08:00
}
2012-11-19 16:31:03 +08:00
switch ( data - > search_mode )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
case 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 08:30:30 +08:00
2012-11-19 16:31:03 +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 ( ) ;
}
2014-01-27 06:47:15 +08:00
set_command_line_and_position ( & data - > command_line , new_text , new_text . size ( ) ) ;
2012-11-19 08:30:30 +08:00
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 TOKEN_SEARCH :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
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 ) ;
}
break ;
2012-11-19 08:30:30 +08:00
}
}
2012-11-19 16:31:03 +08:00
break ;
}
/* Move left*/
case R_BACKWARD_CHAR :
{
2014-01-27 06:55:41 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
2014-01-27 18:22:34 +08:00
if ( data - > is_navigating_pager_contents ( ) )
2012-11-19 08:30:30 +08:00
{
2014-01-27 18:17:31 +08:00
select_completion_in_direction ( direction_west ) ;
2014-01-19 04:42:53 +08:00
}
2014-01-27 06:55:41 +08:00
else if ( el - > position > 0 )
2012-11-19 08:30:30 +08:00
{
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 ( ) ;
2012-11-19 08:30:30 +08:00
}
break ;
}
2012-11-19 16:31:03 +08:00
/* Move right*/
case R_FORWARD_CHAR :
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:55:41 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
2014-01-27 18:22:34 +08:00
if ( data - > is_navigating_pager_contents ( ) )
2012-11-19 08:30:30 +08:00
{
2014-01-27 18:17:31 +08:00
select_completion_in_direction ( direction_east ) ;
2014-01-19 04:42:53 +08:00
}
2014-01-27 06:55:41 +08:00
else if ( el - > position < el - > size ( ) )
2012-11-19 08:30:30 +08:00
{
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 ( ) ;
2012-11-19 08:30:30 +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
/* kill one word left */
case R_BACKWARD_KILL_WORD :
2012-12-21 09:45:35 +08:00
case R_BACKWARD_KILL_PATH_COMPONENT :
2012-11-19 16:31:03 +08:00
{
2012-12-21 09:45:35 +08:00
move_word_style_t style = ( 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 ) ;
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
}
2012-11-19 16:31:03 +08:00
/* kill one word right */
case R_KILL_WORD :
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:47:15 +08:00
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
}
2012-11-19 16:31:03 +08:00
/* move one word left*/
case R_BACKWARD_WORD :
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:47:15 +08:00
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
}
2012-11-19 16:31:03 +08:00
/* move one word right*/
case R_FORWARD_WORD :
2012-11-19 08:30:30 +08:00
{
2014-01-27 06:47:15 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
if ( el - > position < el - > size ( ) )
2012-12-11 08:23:08 +08:00
{
2014-01-27 06:47:15 +08:00
move_word ( el , MOVE_DIR_RIGHT , false /* do not erase */ , move_word_style_punctuation , false ) ;
2012-12-11 08:23:08 +08:00
}
else
{
accept_autosuggestion ( false /* accept only one word */ ) ;
}
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_BEGINNING_OF_HISTORY :
{
2014-01-27 06:47:15 +08:00
const editable_line_t * el = & data - > command_line ;
data - > history_search = history_search_t ( * data - > history , el - > text , HISTORY_SEARCH_TYPE_PREFIX ) ;
2012-11-19 16:31:03 +08:00
data - > history_search . go_to_beginning ( ) ;
if ( ! data - > history_search . is_at_end ( ) )
{
wcstring new_text = data - > history_search . current_string ( ) ;
2014-01-27 06:47:15 +08:00
set_command_line_and_position ( & data - > command_line , new_text , new_text . size ( ) ) ;
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 ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case R_END_OF_HISTORY :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
data - > history_search . go_to_end ( ) ;
break ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
case R_UP_LINE :
case R_DOWN_LINE :
{
2014-01-27 16:56:13 +08:00
if ( data - > is_navigating_pager_contents ( ) )
2014-01-25 07:59:18 +08:00
{
/* We are already navigating pager contents. */
selection_direction_t direction ;
if ( c = = R_DOWN_LINE )
{
/* Down arrow is always south */
direction = direction_south ;
}
2014-01-27 18:17:31 +08:00
else if ( selection_is_at_top ( ) )
2014-01-25 07:59:18 +08:00
{
/* Up arrow, but we are in the first column and first row. End navigation */
direction = direction_deselect ;
}
else
{
/* Up arrow, go north */
direction = direction_north ;
}
2014-04-01 01:01:39 +08:00
2014-01-25 07:59:18 +08:00
/* Now do the selection */
2014-01-27 18:17:31 +08:00
select_completion_in_direction ( direction ) ;
2014-01-25 07:59:18 +08:00
}
else if ( c = = R_DOWN_LINE & & ! data - > pager . empty ( ) )
2014-01-18 04:04:03 +08:00
{
2014-01-25 07:59:18 +08:00
/* 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 ) ;
2014-01-18 04:04:03 +08:00
}
2012-11-19 16:31:03 +08:00
else
{
2014-01-18 04:04:03 +08:00
/* 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 )
line_new = line_old - 1 ;
else
line_new = line_old + 1 ;
2014-04-01 01:01:39 +08:00
2014-01-27 06:47:15 +08:00
int line_count = parse_util_lineno ( el - > text . c_str ( ) , el - > size ( ) ) - 1 ;
2014-04-01 01:01:39 +08:00
2014-01-18 04:04:03 +08:00
if ( line_new > = 0 & & line_new < = line_count )
{
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
2014-01-27 06:47:15 +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
2014-01-27 06:47:15 +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
}
2014-04-01 01:01:39 +08:00
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_SUPPRESS_AUTOSUGGESTION :
{
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
}
2012-11-19 16:31:03 +08:00
case R_ACCEPT_AUTOSUGGESTION :
2012-11-19 08:30:30 +08:00
{
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
}
2012-11-19 16:31:03 +08:00
2013-05-21 03:42:34 +08:00
case R_TRANSPOSE_CHARS :
{
2014-01-27 06:47:15 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
if ( el - > size ( ) < 2 )
2013-05-25 14:53:10 +08:00
{
2013-05-21 03:42:34 +08:00
break ;
}
/* If the cursor is at the end, transpose the last two characters of the line */
2014-01-27 06:47:15 +08:00
if ( el - > position = = el - > size ( ) )
2013-05-21 03:42:34 +08:00
{
2014-03-30 05:19:45 +08:00
update_buff_pos ( el , el - > position - 1 ) ;
2013-05-21 03:42:34 +08:00
}
/*
Drag the character before the cursor forward over the character at the cursor , moving
the cursor forward as well .
*/
2014-01-27 06:47:15 +08:00
if ( el - > position > 0 )
2013-05-21 03:42:34 +08:00
{
2014-01-27 06:47:15 +08:00
wcstring local_cmd = el - > text ;
std : : swap ( local_cmd . at ( el - > position ) , local_cmd . at ( el - > position - 1 ) ) ;
set_command_line_and_position ( el , local_cmd , el - > position + 1 ) ;
2013-05-21 03:42:34 +08:00
}
break ;
}
2013-05-28 05:25:55 +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 ;
/* 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 ;
2014-01-15 22:07:22 +08:00
while ( buff_pos ! = len & & ! iswalnum ( buff [ buff_pos ] ) )
buff_pos + + ;
2014-03-30 05:19:45 +08:00
update_buff_pos ( el , buff_pos ) ;
2013-05-28 05:25:55 +08:00
2014-01-27 06:47:15 +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
/* In case we didn't find a token at or after the cursor... */
if ( tok_begin = = & buff [ len ] )
{
/* ...retry beginning from the previous token */
size_t pos = prev_end - & buff [ 0 ] ;
parse_util_token_extent ( buff , pos , & tok_begin , & tok_end , & prev_begin , & prev_end ) ;
}
/* 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
/* Compose new command line with swapped tokens */
wcstring new_buff ( buff , prev_begin - buff ) ;
new_buff . append ( tok ) ;
new_buff . append ( sep ) ;
new_buff . append ( prev ) ;
new_buff . append ( trail ) ;
2013-05-29 17:48:04 +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-10-27 06:27:39 +08:00
2013-09-10 00:46:16 +08:00
case R_UPCASE_WORD :
case R_DOWNCASE_WORD :
case R_CAPITALIZE_WORD :
{
2014-01-27 06:47:15 +08:00
editable_line_t * el = data - > active_edit_line ( ) ;
2013-09-11 11:46:46 +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
2013-09-11 11:46:46 +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 ) ;
for ( ; pos < el - > position ; pos + + )
2013-09-11 11:46:46 +08:00
{
2014-01-27 06:47:15 +08:00
wchar_t chr = el - > text . at ( pos ) ;
2013-10-27 06:27:39 +08:00
2013-09-11 11:46:46 +08:00
// We always change the case; this decides whether we go uppercase (true) or lowercase (false)
bool make_uppercase ;
if ( c = = R_CAPITALIZE_WORD )
make_uppercase = ! capitalized_first & & iswalnum ( chr ) ;
else
make_uppercase = ( c = = R_UPCASE_WORD ) ;
2013-10-27 06:27:39 +08:00
2013-09-11 11:46:46 +08:00
// Apply the operation and then record what we did
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 ;
}
2013-10-27 06:27:39 +08:00
2014-01-15 22:07: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
}
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
}
2014-01-18 17:18:29 +08:00
case R_KILL_SELECTION :
{
bool newv = ( last_char ! = R_KILL_SELECTION ) ;
size_t start , len ;
2014-03-30 05:19:45 +08:00
if ( reader_get_selection ( & start , & len ) )
2014-01-18 17:18:29 +08:00
{
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 ;
}
2014-01-22 17:00:44 +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
2014-03-30 05:19:45 +08:00
for ( size_t i = el - > position + 1 ; i < el - > size ( ) ; i + + )
2014-01-22 17:00:44 +08:00
{
2014-03-30 05:19:45 +08:00
if ( el - > at ( i ) = = target )
2014-01-22 17:00:44 +08:00
{
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 ;
}
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 ;
while ( tmp_pos - - )
2014-01-22 17:00:44 +08:00
{
2014-03-30 05:19:45 +08:00
if ( el - > at ( tmp_pos ) = = target )
2014-01-22 17:00:44 +08:00
{
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 ;
}
2012-11-19 16:31:03 +08:00
/* Other, if a normal character, we add it to the command */
default :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( ( ! wchar_private ( c ) ) & & ( ( ( c > 31 ) | | ( c = = L ' \n ' ) ) & & ( c ! = 127 ) ) )
{
2014-07-26 03:23:42 +08:00
bool allow_expand_abbreviations = false ;
2014-01-27 16:56:13 +08:00
if ( data - > is_navigating_pager_contents ( ) )
2014-01-26 16:41:30 +08:00
{
2014-01-27 16:56:13 +08:00
data - > pager . set_search_field_shown ( true ) ;
2014-01-26 16:41:30 +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
2012-11-19 16:31:03 +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
2014-01-28 23:30:05 +08:00
/* End paging upon inserting into the normal command line */
if ( el = = & data - > command_line )
{
clear_pager ( ) ;
}
2014-04-01 01:01:39 +08:00
2014-01-27 16:56:13 +08:00
2012-11-19 16:31:03 +08:00
}
else
{
/*
Low priority debug message . These can happen if
the user presses an unefined control
sequnece . No reason to report .
*/
debug ( 2 , _ ( L " Unknown keybinding %d " ) , c ) ;
}
break ;
2012-11-19 08:30:30 +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 ) )
{
data - > search_mode = NO_SEARCH ;
data - > search_buff . clear ( ) ;
data - > history_search . go_to_end ( ) ;
data - > token_history_pos = - 1 ;
}
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
}
writestr ( L " \n " ) ;
2013-07-17 17:55:15 +08:00
2014-01-16 10:21:38 +08:00
/* Ensure we have no pager contents when we exit */
2014-01-20 15:52:35 +08:00
if ( ! data - > pager . empty ( ) )
2014-01-16 10:21:38 +08:00
{
/* 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 */
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
2012-11-19 08:30:30 +08:00
if ( ! reader_exit_forced ( ) )
{
if ( tcsetattr ( 0 , TCSANOW , & old_modes ) ) /* return to previous mode */
{
wperror ( L " tcsetattr " ) ;
}
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
}
2007-09-21 22:05:49 +08:00
int reader_search_mode ( )
{
2012-11-19 08:30:30 +08:00
if ( ! data )
{
return - 1 ;
}
return ! ! data - > search_mode ;
2007-09-21 22:05:49 +08:00
}
2014-01-18 04:53:01 +08:00
int reader_has_pager_contents ( )
{
if ( ! data )
{
return - 1 ;
}
2014-01-19 04:42:53 +08:00
return ! data - > current_page_rendering . screen_data . empty ( ) ;
2007-09-21 22:05:49 +08:00
}
2014-01-27 16:56:13 +08:00
void reader_selected_completion_changed ( pager_t * pager )
{
/* Only interested in the top level pager */
if ( data = = NULL | | pager ! = & data - > pager )
return ;
2014-04-01 01:01:39 +08:00
2014-01-27 16:56:13 +08:00
const completion_t * completion = pager - > selected_completion ( data - > current_page_rendering ) ;
2014-04-01 01:01:39 +08:00
2014-01-27 16:56:13 +08:00
/* Update the cursor and command line */
size_t cursor_pos = data - > cycle_cursor_pos ;
wcstring new_cmd_line ;
2014-04-01 01:01:39 +08:00
2014-01-27 16:56:13 +08:00
if ( completion = = NULL )
{
new_cmd_line = data - > cycle_command_line ;
}
else
{
new_cmd_line = completion_apply_to_command_line ( completion - > completion , completion - > flags , data - > cycle_command_line , & cursor_pos , false ) ;
}
reader_set_buffer_maintaining_pager ( new_cmd_line , cursor_pos ) ;
/* Since we just inserted a completion, don't immediately do a new autosuggestion */
data - > suppress_autosuggestion = true ;
/* Trigger repaint (see #765) */
reader_repaint_needed ( ) ;
}
2007-09-21 22:05:49 +08:00
2005-09-20 21:26:39 +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 .
*/
2012-11-19 08:30:30 +08:00
static int read_ni ( int fd , const io_chain_t & io )
2005-09-20 21:26:39 +08:00
{
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 ;
wchar_t * buff = 0 ;
std : : vector < char > acc ;
2013-04-08 03:40:08 +08:00
int des = ( fd = = STDIN_FILENO ? dup ( STDIN_FILENO ) : fd ) ;
2012-11-19 08:30:30 +08:00
int res = 0 ;
if ( des = = - 1 )
{
wperror ( L " dup " ) ;
return 1 ;
}
in_stream = fdopen ( des , " r " ) ;
if ( in_stream ! = 0 )
{
while ( ! feof ( in_stream ) )
{
char buff [ 4096 ] ;
size_t c = fread ( buff , 1 , 4096 , in_stream ) ;
2013-04-08 03:40:08 +08:00
if ( ferror ( in_stream ) )
2012-11-19 08:30:30 +08:00
{
2013-04-08 03:40:08 +08:00
if ( errno = = EINTR )
{
2013-04-08 05:52:16 +08:00
/* We got a signal, just keep going. Be sure that we call insert() below because we may get data as well as EINTR. */
clearerr ( in_stream ) ;
2013-04-08 03:40:08 +08:00
}
else if ( ( errno = = EAGAIN | | errno = = EWOULDBLOCK ) & & make_fd_blocking ( des ) = = 0 )
{
2013-04-08 05:52:16 +08:00
/* We succeeded in making the fd blocking, keep going */
clearerr ( in_stream ) ;
2013-04-08 03:40:08 +08:00
}
2013-04-08 05:52:16 +08:00
else
{
/* Fatal error */
debug ( 1 , _ ( L " Error while reading from file descriptor " ) ) ;
2012-11-19 08:30:30 +08:00
2013-04-08 05:52:16 +08:00
/* Reset buffer on error. We won't evaluate incomplete files. */
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
2012-12-21 09:45:35 +08:00
const 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
2012-11-19 08:30:30 +08:00
if ( fclose ( in_stream ) )
{
debug ( 1 ,
_ ( L " Error while closing input stream " ) ) ;
wperror ( L " fclose " ) ;
res = 1 ;
}
2013-12-13 10:18:07 +08:00
parse_error_list_t errors ;
2014-07-25 05:41:15 +08:00
if ( ! parse_util_detect_errors ( str , & errors , false /* do not accept incomplete */ ) )
2012-11-19 08:30:30 +08:00
{
2012-12-20 05:31:06 +08:00
parser . eval ( str , io , TOP ) ;
2012-11-19 08:30:30 +08:00
}
else
{
2013-12-16 08:05:37 +08:00
wcstring sb ;
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
}
}
else
{
debug ( 1 ,
_ ( L " Error while opening input stream " ) ) ;
wperror ( L " fdopen " ) ;
free ( buff ) ;
res = 1 ;
}
return res ;
}
int reader_read ( int fd , const io_chain_t & io )
{
int res ;
/*
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 .
*/
int inter = ( ( fd = = STDIN_FILENO ) & & isatty ( STDIN_FILENO ) ) ;
proc_push_interactive ( inter ) ;
res = get_is_interactive ( ) ? read_i ( ) : read_ni ( fd , io ) ;
/*
If the exit command was called in a script , only exit the
script , not the program .
*/
if ( data )
data - > end_loop = 0 ;
end_loop = 0 ;
proc_pop_interactive ( ) ;
return res ;
2005-09-20 21:26:39 +08:00
}