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"
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
2012-02-07 02:52:13 +08:00
/* A color is an int */
typedef int color_t ;
2013-07-17 15:38:04 +08:00
static void set_command_line_and_position ( const wcstring & new_str , size_t pos ) ;
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 */
wcstring command_line ;
2012-02-07 02:52:13 +08:00
/** String containing the autosuggestion */
wcstring autosuggestion ;
2005-09-20 21:26: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
2012-02-06 17:53:03 +08:00
/** Length of the command */
2012-11-19 08:30:30 +08:00
size_t command_length ( ) const
{
return command_line . size ( ) ;
}
2012-02-29 04:40:20 +08:00
/** Do what we need to do whenever our command line changes */
void command_line_changed ( void ) ;
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
2012-11-19 08:30:30 +08:00
/** The current position of the cursor in buff. */
size_t buff_pos ;
2005-09-20 21:26:39 +08:00
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 ;
/**
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 ] .
*/
2012-02-07 02:52:13 +08:00
std : : vector < color_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 ) ,
buff_pos ( 0 ) ,
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
/**
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
2012-11-19 08:30:30 +08:00
static void reader_super_highlight_me_plenty ( size_t pos ) ;
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
}
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 ( )
{
2013-01-06 07:21:42 +08:00
// Update the indentation
2013-01-25 06:59:52 +08:00
parser_t : : principal_parser ( ) . test ( data - > command_line . c_str ( ) , & data - > indents [ 0 ] ) ;
2012-11-19 08:30:30 +08:00
2013-01-06 07:21:42 +08:00
// Combine the command and autosuggestion into one string
wcstring full_line = combine_command_and_autosuggestion ( data - > command_line , 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
2012-02-07 02:52:13 +08:00
std : : vector < color_t > colors = data - > colors ;
2012-02-07 11:31:06 +08:00
colors . resize ( len , HIGHLIGHT_AUTOSUGGESTION ) ;
2012-11-19 08:30:30 +08:00
2012-02-07 02:52:13 +08:00
std : : vector < int > indents = data - > indents ;
indents . resize ( len ) ;
2012-11-19 08:30:30 +08:00
s_write ( & data - > screen ,
data - > left_prompt_buff ,
data - > right_prompt_buff ,
full_line ,
data - > command_length ( ) ,
& colors [ 0 ] ,
& indents [ 0 ] ,
data - > buff_pos ) ;
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-18 14:20:31 +08:00
static void reader_repaint_without_autosuggestion ( )
{
2013-02-21 10:18:08 +08:00
// Swap in an empty autosuggestion, repaint, then swap it out
wcstring saved_autosuggestion ;
data - > autosuggestion . swap ( saved_autosuggestion ) ;
2013-02-18 14:20:31 +08:00
reader_repaint ( ) ;
2013-02-21 10:18:08 +08:00
data - > autosuggestion . swap ( saved_autosuggestion ) ;
2013-02-18 14:20:31 +08:00
}
2013-02-21 10:18:08 +08:00
/** Internal helper function for handling killing parts of text. */
2012-11-19 08:30:30 +08:00
static void reader_kill ( size_t begin_idx , size_t length , int mode , int newv )
2006-10-12 21:27:32 +08:00
{
2012-02-06 17:45:16 +08:00
const wchar_t * begin = data - > command_line . 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
2012-11-19 08:30:30 +08:00
if ( data - > buff_pos > begin_idx )
{
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 */
size_t backtrack = mini ( data - > buff_pos - begin_idx , length ) ;
data - > buff_pos - = backtrack ;
2012-11-19 08:30:30 +08:00
}
2012-02-06 17:45:16 +08:00
data - > command_line . erase ( begin_idx , length ) ;
2012-02-29 04:40:20 +08:00
data - > command_line_changed ( ) ;
2012-11-19 08:30:30 +08:00
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
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 */
2012-11-19 08:30:30 +08:00
void reader_data_t : : command_line_changed ( )
{
2012-02-29 04:40:20 +08:00
ASSERT_IS_MAIN_THREAD ( ) ;
2012-02-18 10:24:47 +08:00
size_t len = command_length ( ) ;
2012-11-19 08:30:30 +08:00
2012-10-04 03:19:27 +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. */
color_t last_color = colors . empty ( ) ? color_t ( ) : colors . back ( ) ;
colors . resize ( len , last_color ) ;
2012-11-19 08:30:30 +08:00
2012-02-18 10:24:47 +08:00
indents . resize ( len ) ;
2012-11-19 08:30:30 +08:00
2012-02-29 04:40:20 +08:00
/* Update the gen count */
s_generation_count + + ;
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 ;
/* Parse this subcmd */
parse_node_tree_t parse_tree ;
parse_t parser ;
parser . parse ( subcmd , parse_flag_continue_after_error | parse_flag_accept_incomplete_tokens , & parse_tree , NULL ) ;
/* 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 ) ;
/* Only interested in plain statements with source */
if ( node . type ! = symbol_plain_statement | | ! node . has_source ( ) )
continue ;
/* Skip decorated statements */
if ( parse_tree . decoration_for_plain_statement ( node ) ! = parse_statement_decoration_none )
continue ;
/* 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 ;
/* 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 ;
if ( this - > expand_abbreviations )
{
2013-07-17 17:55:15 +08:00
/* Try expanding abbreviations */
2013-07-17 15:38:04 +08:00
wcstring new_cmdline ;
2013-08-25 02:16:45 +08:00
size_t cursor_pos = this - > buff_pos - mini ( this - > buff_pos , cursor_backtrack ) ;
if ( reader_expand_abbreviation_in_command ( this - > command_line , 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. */
size_t new_buff_pos = this - > buff_pos + new_cmdline . size ( ) - this - > command_line . size ( ) ;
this - > command_line . swap ( new_cmdline ) ;
data - > buff_pos = new_buff_pos ;
2013-07-17 17:55:15 +08:00
data - > command_line_changed ( ) ;
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
}
2005-09-20 21:26:39 +08:00
void reader_write_title ( )
{
2012-11-19 08:30:30 +08:00
const wchar_t * title ;
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 ;
2010-10-06 20:58:13 +08:00
2007-03-18 07:38:46 +08:00
2012-11-19 08:30:30 +08:00
}
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
title = function_exists ( L " fish_title " ) ? L " fish_title " : DEFAULT_TITLE ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
if ( wcslen ( title ) = = 0 )
return ;
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 ) ;
2013-02-01 07:57:08 +08:00
if ( exec_subshell ( title , 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 */
2012-11-19 08:30:30 +08:00
reader_write_title ( ) ;
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-09-23 09:11:53 +08:00
# ifdef VDSUSP
2012-11-19 08:30:30 +08:00
shell_modes . c_cc [ VDSUSP ] = _POSIX_VDISABLE ;
2013-09-23 09:11:53 +08:00
# endif
2012-11-19 08:30:30 +08:00
# endif
2005-09-20 21:26:39 +08:00
}
void reader_destroy ( )
{
2013-09-23 09:11:53 +08:00
tcsetattr ( 0 , TCSANOW , & terminal_mode_on_startup ) ;
2013-01-24 21:29:13 +08:00
pthread_key_delete ( generation_count_key ) ;
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
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 ( )
{
2012-11-19 08:30:30 +08:00
if ( data - > buff_pos < = 0 )
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
{
2012-07-16 01:45:18 +08:00
data - > buff_pos - = 1 ;
width = fish_wcwidth ( data - > command_line . at ( data - > buff_pos ) ) ;
2012-11-19 08:30:30 +08:00
data - > command_line . erase ( data - > buff_pos , 1 ) ;
}
while ( width = = 0 & & data - > buff_pos > 0 ) ;
2012-02-29 04:40:20 +08:00
data - > command_line_changed ( ) ;
2012-02-07 11:31:06 +08:00
data - > suppress_autosuggestion = true ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
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 .
2013-08-25 02:16:45 +08:00
Optionally also expand abbreviations .
Returns true if the string changed .
2005-09-20 21:26:39 +08:00
*/
2013-08-25 02:16:45 +08:00
static bool insert_string ( const wcstring & str , bool should_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 ;
2012-11-19 08:30:30 +08:00
2012-02-06 17:45:16 +08:00
data - > command_line . insert ( data - > buff_pos , str ) ;
data - > buff_pos + = len ;
2012-02-29 04:40:20 +08:00
data - > command_line_changed ( ) ;
2012-02-07 11:31:06 +08:00
data - > suppress_autosuggestion = false ;
2012-11-19 08:30:30 +08:00
2013-08-25 02:16:45 +08:00
if ( should_expand_abbreviations )
data - > expand_abbreviation_as_necessary ( 1 ) ;
2012-11-19 08:30:30 +08:00
/* Syntax highlight. Note we must have that buff_pos > 0 because we just added something nonzero to its length */
2012-08-05 04:02:44 +08:00
assert ( data - > buff_pos > 0 ) ;
2012-11-19 08:30:30 +08:00
reader_super_highlight_me_plenty ( data - > buff_pos - 1 ) ;
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 .
*/
2013-08-25 02:16:45 +08:00
static bool insert_char ( wchar_t c , bool should_expand_abbreviations = false )
2006-11-02 21:50:19 +08:00
{
2013-08-25 02:16:45 +08:00
return insert_string ( wcstring ( 1 , c ) , should_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
{
size_t cursor = data - > buff_pos ;
2013-02-03 06:50:22 +08:00
wcstring new_command_line = completion_apply_to_command_line ( val , flags , data - > command_line , & cursor , false /* not append only */ ) ;
2012-05-06 04:34:09 +08:00
reader_set_buffer ( 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 ;
}
2013-02-14 05:22:29 +08:00
/* Return an escaped path to fish_pager */
static wcstring escaped_fish_pager_path ( void )
{
wcstring result ;
const env_var_t bin_dir = env_get_string ( L " __fish_bin_dir " ) ;
if ( bin_dir . missing_or_empty ( ) )
{
/* This isn't good, hope our normal command stuff can find it */
result = L " fish_pager " ;
}
else
{
result = escape_string ( bin_dir + L " /fish_pager " , ESCAPE_ALL ) ;
}
return result ;
}
2005-09-20 21:26:39 +08:00
/**
2006-11-02 21:50:19 +08:00
Run the fish_pager command to display the completion list . If the
fish_pager outputs any text , it is inserted into the input
backbuffer .
2012-11-19 08:30:30 +08:00
\ param prefix the string to display before every completion .
2006-11-02 21:50:19 +08:00
\ param is_quoted should be set if the argument is quoted . This will change the display style .
\ param comp the list of completions to display
2005-09-20 21:26:39 +08:00
*/
2012-11-19 08:30:30 +08:00
static void run_pager ( const wcstring & prefix , int is_quoted , const std : : vector < completion_t > & comp )
2005-09-20 21:26:39 +08:00
{
2012-03-04 13:59:45 +08:00
wcstring msg ;
2012-11-19 08:30:30 +08:00
wcstring prefix_esc ;
char * foo ;
2013-02-01 07:57:08 +08:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
shared_ptr < io_buffer_t > in_buff ( io_buffer_t : : create ( true , 3 ) ) ;
shared_ptr < io_buffer_t > out_buff ( io_buffer_t : : create ( false , 4 ) ) ;
2013-02-01 07:57:08 +08:00
2013-01-30 18:22:38 +08:00
// The above may fail e.g. if we have too many open fds
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
if ( in_buff . get ( ) = = NULL | | out_buff . get ( ) = = NULL )
2013-01-30 18:22:38 +08:00
return ;
2012-11-19 08:30:30 +08:00
wchar_t * escaped_separator ;
if ( prefix . empty ( ) )
{
prefix_esc = L " \" \" " ;
}
else
{
prefix_esc = escape_string ( prefix , 1 ) ;
}
2013-02-15 07:50:24 +08:00
2013-02-14 05:22:29 +08:00
const wcstring pager_path = escaped_fish_pager_path ( ) ;
const wcstring cmd = format_string ( L " %ls -c 3 -r 4 %ls -p %ls " ,
2013-02-15 07:50:24 +08:00
// L"valgrind --track-fds=yes --log-file=pager.txt --leak-check=full ./%ls %d %ls",
pager_path . c_str ( ) ,
is_quoted ? L " -q " : L " " ,
prefix_esc . c_str ( ) ) ;
2012-11-19 08:30:30 +08:00
escaped_separator = escape ( COMPLETE_SEP_STR , 1 ) ;
2013-06-02 16:14:26 +08:00
2012-11-19 08:30:30 +08:00
for ( size_t i = 0 ; i < comp . size ( ) ; i + + )
{
long base_len = - 1 ;
const completion_t & el = comp . at ( i ) ;
2013-06-02 16:14:26 +08:00
2012-11-19 08:30:30 +08:00
wcstring completion_text ;
wcstring description_text ;
// Note that an empty completion is perfectly sensible here, e.g. tab-completing 'foo' with a file called 'foo' and another called 'foobar'
2013-05-26 06:41:18 +08:00
if ( ( el . flags & COMPLETE_REPLACES_TOKEN ) & & match_type_shares_prefix ( el . match . type ) )
2012-11-19 08:30:30 +08:00
{
2013-05-26 06:41:18 +08:00
// Compute base_len if we have not yet
2012-11-19 08:30:30 +08:00
if ( base_len = = - 1 )
2012-02-06 17:45:16 +08:00
{
const wchar_t * begin , * buff = data - > command_line . c_str ( ) ;
2012-11-19 08:30:30 +08:00
parse_util_token_extent ( buff , data - > buff_pos , & begin , 0 , 0 , 0 ) ;
2012-02-06 17:45:16 +08:00
base_len = data - > buff_pos - ( begin - buff ) ;
}
2012-11-19 08:30:30 +08:00
completion_text = escape_string ( el . completion . c_str ( ) + base_len , ESCAPE_ALL | ESCAPE_NO_QUOTED ) ;
}
else
{
completion_text = escape_string ( el . completion , ESCAPE_ALL | ESCAPE_NO_QUOTED ) ;
}
if ( ! el . description . empty ( ) )
2012-02-06 17:45:16 +08:00
{
2012-11-19 08:30:30 +08:00
description_text = escape_string ( el . description , true ) ;
2012-02-06 17:45:16 +08:00
}
2007-02-09 17:33:50 +08:00
2012-08-21 04:09:21 +08:00
/* It's possible (even common) to have an empty completion with no description. An example would be completing 'foo' with extant files 'foo' and 'foobar'. But fish_pager ignores blank lines. So if our completion text is empty, always include a description, even if it's empty.
*/
msg . reserve ( msg . size ( ) + completion_text . size ( ) + description_text . size ( ) + 2 ) ;
msg . append ( completion_text ) ;
if ( ! description_text . empty ( ) | | completion_text . empty ( ) )
{
msg . append ( escaped_separator ) ;
msg . append ( description_text ) ;
}
msg . push_back ( L ' \n ' ) ;
}
2007-02-09 17:33:50 +08:00
2012-11-19 08:30:30 +08:00
free ( escaped_separator ) ;
foo = wcs2str ( msg . c_str ( ) ) ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
in_buff - > out_buffer_append ( foo , strlen ( foo ) ) ;
2012-11-19 08:30:30 +08:00
free ( foo ) ;
2013-01-30 18:22:38 +08:00
term_donate ( ) ;
2012-01-23 13:57:30 +08:00
parser_t & parser = parser_t : : principal_parser ( ) ;
2012-08-15 15:57:56 +08:00
io_chain_t io_chain ;
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
io_chain . push_back ( out_buff ) ;
io_chain . push_back ( in_buff ) ;
2012-11-19 08:30:30 +08:00
parser . eval ( cmd , io_chain , TOP ) ;
term_steal ( ) ;
2005-09-20 22:51:00 +08:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
out_buff - > read ( ) ;
2006-01-31 03:53:10 +08:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
const char zero = 0 ;
out_buff - > out_buffer_append ( & zero , 1 ) ;
2006-01-31 03:53:10 +08:00
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
const char * out_data = out_buff - > out_buffer_ptr ( ) ;
if ( out_data )
2012-11-19 08:30:30 +08:00
{
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
const wcstring str = str2wcstring ( out_data ) ;
2012-12-20 05:31:06 +08:00
size_t idx = str . size ( ) ;
while ( idx - - )
2012-11-19 08:30:30 +08:00
{
2012-12-20 05:31:06 +08:00
input_unreadch ( str . at ( idx ) ) ;
2012-11-19 08:30:30 +08:00
}
}
2005-09-20 21:26:39 +08:00
}
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-27 12:11:34 +08:00
wcstring_list_t commands_to_load ;
2012-02-29 04:40:20 +08:00
const unsigned int generation_count ;
2012-11-19 08:30:30 +08:00
2012-02-28 07:33:46 +08:00
// don't reload more than once
bool has_tried_reloading ;
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 ) ,
2012-02-19 10:54:36 +08:00
detector ( history , term ) ,
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 ) ,
2012-02-29 04:40:20 +08:00
generation_count ( s_generation_count ) ,
2012-02-28 07:33:46 +08:00
has_tried_reloading ( false )
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-03-06 12:54:16 +08:00
complete ( search_string , completions , COMPLETION_REQUEST_AUTOSUGGESTION , & this - > commands_to_load ) ;
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 ;
2013-02-03 06:50:22 +08:00
this - > autosuggestion = completion_apply_to_command_line ( comp . completion . c_str ( ) , 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. */
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 ( ) & &
data - > command_line . 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-27 12:11:34 +08:00
/* Extract the commands to load */
wcstring_list_t commands_to_load ;
ctx - > commands_to_load . swap ( commands_to_load ) ;
2012-11-19 08:30:30 +08:00
2012-02-27 12:11:34 +08:00
/* If we have autosuggestions to load, load them and try again */
2012-02-28 07:33:46 +08:00
if ( ! result & & ! commands_to_load . empty ( ) & & ! ctx - > has_tried_reloading )
2012-02-27 12:11:34 +08:00
{
2012-02-28 07:33:46 +08:00
ctx - > has_tried_reloading = true ;
2012-03-02 06:56:34 +08:00
for ( wcstring_list_t : : const_iterator iter = commands_to_load . begin ( ) ; iter ! = commands_to_load . end ( ) ; + + iter )
2012-02-27 12:11:34 +08:00
{
complete_load ( * iter , false ) ;
}
iothread_perform ( threaded_autosuggest , autosuggest_completed , ctx ) ;
return ;
}
2012-11-19 08:30:30 +08:00
2012-02-20 18:13:31 +08:00
if ( result & &
2012-11-19 08:30:30 +08:00
can_autosuggest ( ) & &
ctx - > search_string = = data - > command_line & &
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 ( ) )
{
2012-05-06 04:34:09 +08:00
autosuggestion_context_t * ctx = new autosuggestion_context_t ( data - > history , data - > command_line , data - > buff_pos ) ;
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 ( ) )
{
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 */
data - > command_line = data - > autosuggestion ;
}
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 ;
data - > command_line . push_back ( wc ) ;
}
}
2012-07-16 05:02:34 +08:00
data - > buff_pos = data - > command_line . size ( ) ;
data - > command_line_changed ( ) ;
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
reader_repaint ( ) ;
}
}
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 ;
for ( size_t i = 0 ; i < data - > buff_pos ; i + + )
{
data - > colors . at ( i ) = HIGHLIGHT_SEARCH_MATCH < < 16 ;
}
reader_repaint ( ) ;
pollint . tv_sec = 0 ;
pollint . tv_nsec = 100 * 1000000 ;
nanosleep ( & pollint , NULL ) ;
2006-12-01 07:57:49 +08:00
2012-11-19 08:30:30 +08:00
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
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
}
/* Given a list of completions, get the completion at an index past *inout_idx, and then increment it. inout_idx should be initialized to (size_t)(-1) for the first call. */
static const completion_t * cycle_competions ( const std : : vector < completion_t > & comp , const wcstring & command_line , size_t * inout_idx )
{
const size_t size = comp . size ( ) ;
if ( size = = 0 )
return NULL ;
2013-09-11 12:13:47 +08:00
// note start_idx will be set to -1 initially, so that when it gets incremented we start at 0
2012-08-22 08:18:52 +08:00
const size_t start_idx = * inout_idx ;
size_t idx = start_idx ;
2013-09-11 12:13:47 +08:00
2012-08-22 08:18:52 +08:00
const completion_t * result = NULL ;
2013-09-11 12:13:47 +08:00
size_t remaining = comp . size ( ) ;
while ( remaining - - )
2012-08-22 08:18:52 +08:00
{
/* Bump the index */
idx = ( idx + 1 ) % size ;
2012-11-19 08:30:30 +08:00
2012-08-22 08:18:52 +08:00
/* Get the completion */
const completion_t & c = comp . at ( idx ) ;
2012-11-19 08:30:30 +08:00
2012-08-22 08:18:52 +08:00
/* Try this completion */
2013-06-02 16:14:26 +08:00
if ( ! ( c . flags & COMPLETE_REPLACES_TOKEN ) | | reader_can_replace ( command_line , c . flags ) )
2012-08-22 08:18:52 +08:00
{
/* Success */
result = & c ;
break ;
}
}
2012-11-19 08:30:30 +08:00
2012-08-22 08:18:52 +08:00
* inout_idx = idx ;
return result ;
2008-01-14 03:32:21 +08:00
}
2006-12-01 07:57:49 +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
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
*/
2012-11-19 08:30:30 +08:00
static bool handle_completions ( const std : : vector < completion_t > & comp )
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 ;
2012-11-19 08:30:30 +08:00
const wchar_t * begin , * end , * buff = data - > command_line . c_str ( ) ;
2005-09-20 21:26:39 +08:00
2012-11-19 08:30:30 +08:00
parse_util_token_extent ( buff , data - > buff_pos , & begin , 0 , 0 , 0 ) ;
end = buff + data - > buff_pos ;
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
}
else
{
2013-05-26 06:41:18 +08:00
/* We didn't get a common prefix. Print the list. */
size_t len , prefix_start = 0 ;
wcstring prefix ;
parse_util_get_parameter_info ( data - > command_line , data - > buff_pos , NULL , & prefix_start , NULL ) ;
2012-11-19 08:30:30 +08:00
2013-05-26 06:41:18 +08:00
assert ( data - > buff_pos > = prefix_start ) ;
len = data - > buff_pos - 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 )
{
prefix . append ( data - > command_line , prefix_start , len ) ;
}
else
{
// append just the end of the string
prefix = wcstring ( & ellipsis_char , 1 ) ;
prefix . append ( data - > command_line , prefix_start + len - PREFIX_MAX_LEN , PREFIX_MAX_LEN ) ;
}
2012-11-19 08:30:30 +08:00
2013-05-26 06:41:18 +08:00
{
int is_quoted ;
2012-11-19 08:30:30 +08:00
2013-05-26 06:41:18 +08:00
wchar_t quote ;
parse_util_get_parameter_info ( data - > command_line , data - > buff_pos , & quote , NULL , NULL ) ;
is_quoted = ( quote ! = L ' \0 ' ) ;
2013-02-18 14:20:31 +08:00
2013-05-26 06:41:18 +08:00
/* Clear the autosuggestion from the old commandline before abandoning it (see #561) */
if ( ! data - > autosuggestion . empty ( ) )
reader_repaint_without_autosuggestion ( ) ;
2012-11-19 08:30:30 +08:00
2013-05-26 06:41:18 +08:00
write_loop ( 1 , " \n " , 1 ) ;
2013-06-02 16:14:26 +08:00
2013-05-26 06:41:18 +08:00
run_pager ( prefix , is_quoted , surviving_completions ) ;
}
s_reset ( & data - > screen , screen_reset_abandon_line ) ;
reader_repaint ( ) ;
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 ( )
{
if ( get_is_interactive ( ) )
{
if ( ! data )
sanity_lose ( ) ;
if ( ! ( data - > buff_pos < = data - > command_length ( ) ) )
sanity_lose ( ) ;
2007-10-06 18:54:53 +08:00
2012-02-18 10:24:47 +08:00
if ( data - > colors . size ( ) ! = data - > command_length ( ) )
2012-02-07 03:34:49 +08:00
sanity_lose ( ) ;
2012-11-19 08:30:30 +08:00
2012-02-18 10:24:47 +08:00
if ( data - > indents . size ( ) ! = data - > command_length ( ) )
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
*/
2012-11-19 08:30:30 +08:00
static void set_command_line_and_position ( const wcstring & new_str , size_t pos )
2012-06-13 00:41:30 +08:00
{
data - > command_line = new_str ;
data - > command_line_changed ( ) ;
data - > buff_pos = pos ;
2012-11-19 08:30:30 +08:00
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
2012-06-13 00:41:30 +08:00
reader_repaint ( ) ;
}
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 */
2012-02-06 17:45:16 +08:00
const wchar_t * buff = data - > command_line . c_str ( ) ;
2013-09-22 07:38:57 +08:00
parse_util_token_extent ( buff , data - > buff_pos , & 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 ) ;
2012-06-13 00:41:30 +08:00
set_command_line_and_position ( 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 ( )
{
2012-11-19 08:30:30 +08:00
const wchar_t * begin , * end ;
2012-02-06 17:45:16 +08:00
const wchar_t * buff = data - > command_line . c_str ( ) ;
2012-11-19 08:30:30 +08:00
parse_util_token_extent ( ( wchar_t * ) buff , data - > buff_pos , & begin , & end , 0 , 0 ) ;
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 ) ;
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
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
wcstring item = data - > history_search . current_string ( ) ;
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 ;
default :
{
break ;
}
2012-11-19 08:30:30 +08:00
}
}
}
if ( str )
{
reader_replace_current_token ( str ) ;
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
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
2012-12-21 09:37:09 +08:00
static void move_word ( 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 */
2012-10-05 05:35:03 +08:00
const size_t boundary = move_right ? data - > command_length ( ) : 0 ;
if ( data - > buff_pos = = boundary )
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 ) ;
2012-11-19 18:41:57 +08:00
const wchar_t * const command_line = data - > command_line . c_str ( ) ;
const size_t start_buff_pos = data - > buff_pos ;
size_t buff_pos = data - > buff_pos ;
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 ) ;
/* 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 data->command_length(). */
2012-11-19 08:30:30 +08:00
if ( erase )
{
2012-11-19 18:41:57 +08:00
/* Don't autosuggest after a kill */
2012-10-05 05:35:03 +08:00
data - > suppress_autosuggestion = true ;
2012-11-19 08:30:30 +08:00
2012-11-19 18:41:57 +08:00
if ( move_right )
{
reader_kill ( start_buff_pos , buff_pos - start_buff_pos , KILL_APPEND , newv ) ;
}
else
{
reader_kill ( buff_pos , start_buff_pos - buff_pos , KILL_PREPEND , newv ) ;
}
2012-11-19 08:30:30 +08:00
}
else
{
2012-11-19 18:41:57 +08:00
data - > buff_pos = 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 ( ) ;
2012-11-19 08:30:30 +08:00
return data ? data - > command_line . 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
}
2012-11-19 08:30:30 +08:00
void reader_set_buffer ( const wcstring & b , size_t pos )
2005-09-20 21:26:39 +08:00
{
2012-11-19 08:30:30 +08:00
if ( ! data )
return ;
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 ( ) ;
2012-02-23 03:07:34 +08:00
data - > command_line = b ;
2012-02-29 04:40:20 +08:00
data - > command_line_changed ( ) ;
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
2012-08-05 04:02:44 +08:00
data - > buff_pos = pos ;
2005-09-20 21:26:39 +08:00
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
2012-11-19 08:30:30 +08:00
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
reader_repaint_needed ( ) ;
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
2012-11-19 08:30:30 +08:00
return data - > buff_pos ;
2005-09-20 21:26:39 +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
2012-11-19 08:30:30 +08:00
if ( secs < 1 )
{
env_remove ( ENV_CMD_DURATION , 0 ) ;
}
else
{
if ( secs < 10 ) // 10 secs
{
swprintf ( buf , 16 , L " %lu.%02us " , secs , usecs / 10000 ) ;
}
else if ( secs < 60 ) // 1 min
{
swprintf ( buf , 16 , L " %lu.%01us " , secs , usecs / 100000 ) ;
}
else if ( secs < 600 ) // 10 mins
{
swprintf ( buf , 16 , L " %lum %lu.%01us " , secs / 60 , secs % 60 , usecs / 100000 ) ;
}
else if ( secs < 5400 ) // 1.5 hours
{
swprintf ( buf , 16 , L " %lum %lus " , secs / 60 , secs % 60 ) ;
}
else
{
swprintf ( buf , 16 , L " %.1fh " , secs / 3600.0 ) ;
}
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
2012-11-19 08:30:30 +08:00
reader_write_title ( ) ;
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
{
2013-01-25 06:59:52 +08:00
int res = parser_t : : principal_parser ( ) . test ( b ) ;
2012-11-19 08:30:30 +08:00
if ( res & PARSER_TEST_ERROR )
{
wcstring sb ;
2006-10-02 00:02:58 +08:00
2012-11-19 08:30:30 +08:00
const int tmp [ 1 ] = { 0 } ;
const int tmp2 [ 1 ] = { 0 } ;
2012-11-08 11:59:20 +08:00
const wcstring empty ;
2012-11-19 08:30:30 +08:00
s_write ( & data - > screen ,
empty ,
empty ,
empty ,
0 ,
tmp ,
tmp2 ,
0 ) ;
2013-01-25 06:59:52 +08:00
parser_t : : principal_parser ( ) . test ( b , NULL , & sb , L " fish " ) ;
2012-11-19 08:30:30 +08:00
fwprintf ( stderr , L " %ls " , sb . c_str ( ) ) ;
}
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
2012-11-19 08:30:30 +08:00
data - > command_line_changed ( ) ;
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 */
std : : vector < color_t > colors ;
/** 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
int threaded_highlight ( )
{
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 ( ) )
{
2013-01-05 14:32:40 +08:00
const wcstring & needle = data - > search_buff ;
size_t match_pos = data - > command_line . find ( needle ) ;
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
{
2013-01-05 14:32:40 +08:00
data - > colors . at ( i ) | = ( HIGHLIGHT_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 ( ) ;
2012-11-19 08:30:30 +08:00
if ( ctx - > string_to_highlight = = data - > command_line )
{
/* The data hasn't changed, so swap in our colors. The colors may not have changed, so do nothing if they have not. */
2012-02-18 10:24:47 +08:00
assert ( ctx - > colors . size ( ) = = data - > command_length ( ) ) ;
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 )
{
2012-02-18 10:24:47 +08:00
return ctx - > threaded_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
\ param match_highlight_pos the position to use for bracket matching . This need not be the same as the surrent cursor position
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
2005-09-20 21:26:39 +08:00
*/
2012-11-19 08:30:30 +08:00
static void reader_super_highlight_me_plenty ( size_t match_highlight_pos )
2005-09-20 21:26:39 +08:00
{
2012-02-07 03:34:49 +08:00
reader_sanity_check ( ) ;
2012-11-19 08:30:30 +08:00
background_highlight_context_t * ctx = new background_highlight_context_t ( data - > command_line , match_highlight_pos , data - > highlight_function ) ;
iothread_perform ( threaded_highlight , highlight_complete , ctx ) ;
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. */
const wcstring & cmd = data - > command_line , & 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
}
int exit_status ( )
{
2012-11-19 08:30:30 +08:00
if ( get_is_interactive ( ) )
return job_list_is_empty ( ) & & data - > end_loop ;
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 ;
block_t * b ;
parser_t & parser = parser_t : : principal_parser ( ) ;
for ( b = parser . current_block ;
b ;
b = b - > outer )
{
if ( b - > type ( ) = = BREAKPOINT )
{
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
}
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 .
*/
2013-04-08 03:40:08 +08:00
const wchar_t * tmp = reader_readline ( ) ;
2012-11-19 08:30:30 +08:00
if ( data - > end_loop )
{
handle_end_loop ( ) ;
}
else if ( tmp )
{
2013-04-08 03:40:08 +08:00
wcstring command = tmp ;
2012-11-19 08:30:30 +08:00
data - > buff_pos = 0 ;
2012-02-06 17:53:03 +08:00
data - > command_line . clear ( ) ;
2012-02-29 04:40:20 +08:00
data - > command_line_changed ( ) ;
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
2013-04-08 03:40:08 +08:00
const wchar_t * reader_readline ( void )
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 cycle index in our completion list */
size_t completion_cycle_idx = ( size_t ) ( - 1 ) ;
2012-11-19 08:30:30 +08:00
2012-08-22 08:18:52 +08:00
/* The command line before completion */
wcstring cycle_command_line ;
2013-08-29 19:06:04 +08:00
size_t cycle_cursor_pos = 0 ;
2012-11-19 08:30:30 +08:00
data - > search_buff . clear ( ) ;
data - > search_mode = NO_SEARCH ;
exec_prompt ( ) ;
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
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 )
{
/*
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 ;
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 ] ;
int i ;
memset ( arr , 0 , sizeof ( arr ) ) ;
arr [ 0 ] = c ;
for ( i = 1 ; i < READAHEAD_MAX ; i + + )
2012-08-22 08:18:52 +08:00
{
2012-11-19 08:30:30 +08:00
if ( ! can_read ( 0 ) )
{
c = 0 ;
break ;
}
c = input_readch ( ) ;
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
insert_string ( arr ) ;
2012-08-22 08:18:52 +08:00
}
2012-11-19 08:30:30 +08:00
}
if ( c ! = 0 )
break ;
}
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
2012-11-19 08:30:30 +08:00
const wchar_t * buff = data - > command_line . c_str ( ) ;
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
{
2012-11-19 16:31:03 +08:00
while ( ( data - > buff_pos > 0 ) & &
( buff [ data - > buff_pos - 1 ] ! = L ' \n ' ) )
{
data - > buff_pos - - ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
reader_repaint ( ) ;
break ;
2012-11-19 08:30:30 +08:00
}
2012-11-19 16:31:03 +08:00
case R_END_OF_LINE :
{
2013-07-27 11:35:34 +08:00
if ( data - > buff_pos < data - > command_length ( ) )
2012-11-19 16:31:03 +08:00
{
2013-07-27 11:35:34 +08:00
while ( buff [ data - > buff_pos ] & &
buff [ data - > buff_pos ] ! = L ' \n ' )
{
data - > buff_pos + + ;
}
}
else
{
accept_autosuggestion ( true ) ;
2012-11-19 16:31:03 +08:00
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
reader_repaint ( ) ;
break ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case R_BEGINNING_OF_BUFFER :
{
data - > buff_pos = 0 ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
reader_repaint ( ) ;
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 :
{
data - > buff_pos = data - > command_length ( ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
reader_repaint ( ) ;
break ;
}
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
case R_NULL :
{
reader_repaint_if_needed ( ) ;
break ;
}
2012-11-19 08:30:30 +08:00
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 :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
if ( ! data - > complete_func )
break ;
if ( ! comp_empty & & last_char = = R_COMPLETE )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
/* The user typed R_COMPLETE more than once in a row. Cycle through our available completions */
const completion_t * next_comp = cycle_competions ( comp , cycle_command_line , & completion_cycle_idx ) ;
if ( next_comp ! = NULL )
{
size_t cursor_pos = cycle_cursor_pos ;
2013-02-03 06:50:22 +08:00
const wcstring new_cmd_line = completion_apply_to_command_line ( next_comp - > completion , next_comp - > flags , cycle_command_line , & cursor_pos , false ) ;
2012-11-19 16:31:03 +08:00
reader_set_buffer ( new_cmd_line , cursor_pos ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* Since we just inserted a completion, don't immediately do a new autosuggestion */
data - > suppress_autosuggestion = true ;
2013-05-20 15:31:27 +08:00
/* Trigger repaint (see #765) */
reader_repaint_if_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
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. */
if ( is_backslashed ( data - > command_line , data - > buff_pos ) )
{
remove_backward ( ) ;
}
/* Get the string; we have to do this after removing any trailing backslash */
2013-02-03 06:50:22 +08:00
const wchar_t * const buff = data - > command_line . 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 ;
2013-02-03 06:50:22 +08:00
parse_util_cmdsubst_extent ( buff , data - > buff_pos , & 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 ;
2013-02-03 06:50:22 +08:00
parse_util_token_extent ( cmdsub_begin , data - > buff_pos - ( cmdsub_begin - buff ) , & token_begin , & token_end , 0 , 0 ) ;
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 */
if ( data - > buff_pos ! = end_of_token_offset )
{
data - > buff_pos = end_of_token_offset ;
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-05-26 06:41:18 +08:00
data - > complete_func ( buffcpy , comp , COMPLETION_REQUEST_DEFAULT | COMPLETION_REQUEST_DESCRIPTIONS | COMPLETION_REQUEST_FUZZY_MATCH , NULL ) ;
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 */
cycle_command_line = data - > command_line ;
cycle_cursor_pos = data - > buff_pos ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
comp_empty = handle_completions ( comp ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
/* Start the cycle at the beginning */
completion_cycle_idx = ( size_t ) ( - 1 ) ;
2013-05-05 17:33:17 +08:00
2013-04-05 02:51:23 +08:00
/* Repaint */
reader_repaint_if_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
/* kill */
case R_KILL_LINE :
{
const wchar_t * buff = data - > command_line . c_str ( ) ;
const wchar_t * begin = & buff [ data - > buff_pos ] ;
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 )
{
reader_kill ( begin - buff , len , KILL_APPEND , last_char ! = R_KILL_LINE ) ;
}
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 :
{
if ( data - > buff_pos > 0 )
{
const wchar_t * buff = data - > command_line . c_str ( ) ;
const wchar_t * end = & buff [ data - > buff_pos ] ;
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 ;
reader_kill ( begin - buff , len , KILL_PREPEND , last_char ! = R_BACKWARD_KILL_LINE ) ;
}
break ;
}
case R_KILL_WHOLE_LINE :
2012-11-19 08:30:30 +08:00
{
2012-02-06 17:45:16 +08:00
const wchar_t * buff = data - > command_line . c_str ( ) ;
2012-11-19 08:30:30 +08:00
const wchar_t * end = & buff [ data - > buff_pos ] ;
const wchar_t * begin = end ;
2012-11-19 16:31:03 +08:00
size_t len ;
2012-11-19 08:30:30 +08:00
while ( begin > buff & & * begin ! = L ' \n ' )
begin - - ;
if ( * begin = = L ' \n ' )
begin + + ;
2012-11-19 16:31:03 +08:00
len = maxi < size_t > ( end - begin , 0 ) ;
2012-11-19 08:30:30 +08:00
begin = end - len ;
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 ( begin = = end & & * end )
end + + ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
len = end - begin ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( len )
{
reader_kill ( begin - buff , len , KILL_APPEND , last_char ! = R_KILL_WHOLE_LINE ) ;
}
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
/* yank*/
case R_YANK :
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
yank_str = kill_yank ( ) ;
2012-11-19 08:30:30 +08:00
insert_string ( yank_str ) ;
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 ( ) ;
insert_string ( yank_str ) ;
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 ( ) ;
reader_set_buffer ( data - > search_buff . c_str ( ) , data - > search_buff . size ( ) ) ;
}
else
{
reader_replace_current_token ( data - > search_buff . c_str ( ) ) ;
}
data - > search_buff . clear ( ) ;
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
reader_repaint ( ) ;
2012-11-19 08:30:30 +08:00
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 .
*/
if ( data - > buff_pos < data - > command_length ( ) )
{
data - > buff_pos + + ;
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
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) */
2013-02-03 06:50:22 +08:00
if ( is_backslashed ( data - > command_line , data - > buff_pos ) )
2012-11-19 08:30:30 +08:00
{
2013-03-13 04:00:37 +08:00
if ( data - > buff_pos > = data - > command_length ( ) | | iswspace ( data - > command_line . at ( data - > buff_pos ) ) )
{
insert_char ( ' \n ' ) ;
break ;
}
2012-11-19 16:31:03 +08:00
}
2013-07-17 15:38:04 +08:00
/* See if this command is valid */
int command_test_result = data - > test_func ( data - > command_line . 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. */
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
2013-07-17 15:38:04 +08:00
command_test_result = data - > test_func ( data - > command_line . c_str ( ) ) ;
}
}
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. */
2013-03-11 05:25:43 +08:00
if ( ! data - > command_line . empty ( ) & & data - > command_line . at ( 0 ) ! = L ' ' )
2012-11-19 16:31:03 +08:00
{
2013-03-11 05:25:43 +08:00
if ( data - > history ! = NULL )
2012-11-19 16:31:03 +08:00
{
data - > history - > add_with_file_detection ( data - > command_line ) ;
}
}
finished = 1 ;
data - > buff_pos = data - > command_length ( ) ;
reader_repaint ( ) ;
break ;
}
/*
We are incomplete , continue editing
*/
case PARSER_TEST_INCOMPLETE :
{
insert_char ( ' \n ' ) ;
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 ) ;
2012-11-19 16:31:03 +08:00
reader_repaint ( ) ;
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 ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
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
2012-11-19 16:31:03 +08:00
data - > search_buff . append ( data - > command_line ) ;
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 ( ) ;
}
set_command_line_and_position ( 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 :
{
if ( data - > buff_pos > 0 )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
data - > buff_pos - - ;
reader_repaint ( ) ;
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
{
2012-11-19 16:31:03 +08:00
if ( data - > buff_pos < data - > command_length ( ) )
2012-11-19 08:30:30 +08:00
{
2012-11-19 16:31:03 +08:00
data - > buff_pos + + ;
reader_repaint ( ) ;
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 ) ;
move_word ( 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
{
2012-12-21 09:37:09 +08:00
move_word ( 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
{
2012-12-21 09:37:09 +08:00
move_word ( 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
{
2012-12-11 08:23:08 +08:00
if ( data - > buff_pos < data - > command_length ( ) )
{
2012-12-21 09:37:09 +08:00
move_word ( 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 :
{
data - > history_search = history_search_t ( * data - > history , data - > command_line , HISTORY_SEARCH_TYPE_PREFIX ) ;
data - > history_search . go_to_beginning ( ) ;
if ( ! data - > history_search . is_at_end ( ) )
{
wcstring new_text = data - > history_search . current_string ( ) ;
set_command_line_and_position ( 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 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 :
{
int line_old = parse_util_get_line_from_offset ( data - > command_line , data - > buff_pos ) ;
int line_new ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( c = = R_UP_LINE )
line_new = line_old - 1 ;
else
line_new = line_old + 1 ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
int line_count = parse_util_lineno ( data - > command_line . c_str ( ) , data - > command_length ( ) ) - 1 ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
if ( line_new > = 0 & & line_new < = line_count )
{
size_t base_pos_new ;
size_t base_pos_old ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
int indent_old ;
int indent_new ;
size_t line_offset_old ;
size_t total_offset_new ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
base_pos_new = parse_util_get_offset_from_line ( data - > command_line , line_new ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
base_pos_old = parse_util_get_offset_from_line ( data - > command_line , line_old ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31: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 ) ;
2012-11-19 08:30:30 +08:00
2012-11-19 16:31:03 +08:00
line_offset_old = data - > buff_pos - parse_util_get_offset_from_line ( data - > command_line , line_old ) ;
total_offset_new = parse_util_get_offset ( data - > command_line , line_new , line_offset_old - 4 * ( indent_new - indent_old ) ) ;
data - > buff_pos = total_offset_new ;
reader_repaint ( ) ;
}
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_SUPPRESS_AUTOSUGGESTION :
{
data - > suppress_autosuggestion = true ;
data - > autosuggestion . clear ( ) ;
2012-11-19 08:30:30 +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_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 :
{
2013-05-25 14:53:10 +08:00
if ( data - > command_length ( ) < 2 )
{
2013-05-21 03:42:34 +08:00
break ;
}
/* If the cursor is at the end, transpose the last two characters of the line */
if ( data - > buff_pos = = data - > command_length ( ) )
{
data - > buff_pos - - ;
}
/*
Drag the character before the cursor forward over the character at the cursor , moving
the cursor forward as well .
*/
if ( data - > buff_pos > 0 )
{
2013-05-25 14:53:10 +08:00
wcstring local_cmd = data - > command_line ;
std : : swap ( local_cmd . at ( data - > buff_pos ) , local_cmd . at ( data - > buff_pos - 1 ) ) ;
set_command_line_and_position ( local_cmd , data - > buff_pos + 1 ) ;
2013-05-21 03:42:34 +08:00
}
break ;
}
2013-05-28 05:25:55 +08:00
case R_TRANSPOSE_WORDS :
{
size_t len = data - > command_length ( ) ;
const wchar_t * buff = data - > command_line . c_str ( ) ;
const wchar_t * tok_begin , * tok_end , * prev_begin , * prev_end ;
/* If we are not in a token, look for one ahead */
while ( data - > buff_pos ! = len & & ! iswalnum ( buff [ data - > buff_pos ] ) )
data - > buff_pos + + ;
parse_util_token_extent ( buff , data - > buff_pos , & tok_begin , & tok_end , & prev_begin , & prev_end ) ;
/* 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 */
set_command_line_and_position ( new_buff , tok_end - buff ) ;
2013-05-28 05:25:55 +08:00
}
break ;
}
2013-09-10 00:46:16 +08:00
case R_UPCASE_WORD :
case R_DOWNCASE_WORD :
case R_CAPITALIZE_WORD :
{
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-09-11 11:46:46 +08:00
// We apply the operation from the current location to the end of the word
size_t pos = data - > buff_pos ;
2013-09-10 00:46:16 +08:00
move_word ( MOVE_DIR_RIGHT , false , move_word_style_punctuation , false ) ;
2013-09-11 11:46:46 +08:00
for ( ; pos < data - > buff_pos ; pos + + )
{
wchar_t chr = data - > command_line . at ( pos ) ;
// 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 ) ;
// Apply the operation and then record what we did
if ( make_uppercase )
chr = towupper ( chr ) ;
else
chr = towlower ( chr ) ;
data - > command_line . at ( pos ) = chr ;
capitalized_first = capitalized_first | | make_uppercase ;
2013-09-10 00:46:16 +08:00
}
2013-09-11 11:46:46 +08:00
data - > command_line_changed ( ) ;
reader_super_highlight_me_plenty ( data - > buff_pos ) ;
2013-09-10 00:46:16 +08:00
reader_repaint ( ) ;
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 ) ) )
{
2013-08-25 02:16:45 +08:00
/* Expand abbreviations after space */
bool should_expand_abbreviations = ( c = = L ' ' ) ;
2013-07-17 17:55:15 +08:00
2012-11-19 16:31:03 +08:00
/* Regular character */
2013-08-25 02:16:45 +08:00
insert_char ( c , should_expand_abbreviations ) ;
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 ;
}
writestr ( L " \n " ) ;
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 ( ) ) ;
}
return finished ? data - > command_line . c_str ( ) : 0 ;
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
}
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 ;
}
2012-12-20 05:31:06 +08:00
wcstring sb ;
if ( ! parser . test ( str . c_str ( ) , 0 , & sb , L " fish " ) )
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
{
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
}