2016-05-03 17:36:22 +00:00
|
|
|
// Prototypes for 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
|
|
|
|
// features.
|
2005-10-04 15:11:39 +00:00
|
|
|
#ifndef FISH_READER_H
|
|
|
|
#define FISH_READER_H
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
#include <stddef.h>
|
2017-02-14 04:37:27 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2012-02-11 01:54:21 +00:00
|
|
|
#include "common.h"
|
2012-02-24 20:13:35 +00:00
|
|
|
#include "complete.h"
|
2014-01-15 09:01:25 +00:00
|
|
|
#include "highlight.h"
|
2015-05-03 00:49:38 +00:00
|
|
|
#include "parse_constants.h"
|
2005-10-04 15:11:39 +00:00
|
|
|
|
2012-02-06 00:42:24 +00:00
|
|
|
class history_t;
|
2016-04-21 06:00:54 +00:00
|
|
|
class env_vars_snapshot_t;
|
|
|
|
class io_chain_t;
|
2012-01-23 05:40:08 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Helper class for storing a command line.
|
|
|
|
class editable_line_t {
|
|
|
|
public:
|
|
|
|
/// The command line.
|
2014-01-26 08:41:30 +00:00
|
|
|
wcstring text;
|
2016-05-03 17:36:22 +00:00
|
|
|
/// The current position of the cursor in the command line.
|
2014-01-26 08:41:30 +00:00
|
|
|
size_t position;
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
const wcstring &get_text() const { return text; }
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
// Gets the length of the text.
|
|
|
|
size_t size() const { return text.size(); }
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
bool empty() const { return text.empty(); }
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
void clear() {
|
2014-01-27 08:56:13 +00:00
|
|
|
text.clear();
|
|
|
|
position = 0;
|
|
|
|
}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
wchar_t at(size_t idx) { return text.at(idx); }
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
editable_line_t() : text(), position(0) {}
|
2014-03-31 17:01:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Inserts a substring of str given by start, len at the cursor position.
|
2014-07-25 19:23:42 +00:00
|
|
|
void insert_string(const wcstring &str, size_t start = 0, size_t len = wcstring::npos);
|
2014-01-26 08:41:30 +00:00
|
|
|
};
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Read commands from \c fd until encountering EOF.
|
2012-11-19 00:30:30 +00:00
|
|
|
int reader_read(int fd, const io_chain_t &io);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Tell the shell that it should exit after the currently running command finishes.
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_exit(int do_exit, int force);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Check that the reader is in a sane state.
|
2005-09-20 13:26:39 +00:00
|
|
|
void reader_sanity_check();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Initialize the reader.
|
2005-09-20 13:26:39 +00:00
|
|
|
void reader_init();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Destroy and free resources used by the reader.
|
2005-09-20 13:26:39 +00:00
|
|
|
void reader_destroy();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Restore the term mode at startup.
|
2013-10-26 22:22:20 +00:00
|
|
|
void restore_term_mode();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Returns the filename of the file currently read.
|
2012-02-02 23:05:08 +00:00
|
|
|
const wchar_t *reader_current_filename();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Push a new filename on the stack of read files.
|
|
|
|
///
|
|
|
|
/// \param fn The fileanme to push
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_push_current_filename(const wchar_t *fn);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2017-07-28 04:32:49 +00:00
|
|
|
/// Change the history file for the current command reading context.
|
|
|
|
void reader_change_history(const wchar_t *fn);
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Pop the current filename from the stack of read files.
|
|
|
|
void reader_pop_current_filename();
|
2014-07-14 15:34:15 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Write the title to the titlebar. This function is called just before a new application starts
|
|
|
|
/// executing and just after it finishes.
|
|
|
|
///
|
|
|
|
/// \param cmd Command line string passed to \c fish_title if is defined.
|
|
|
|
/// \param reset_cursor_position If set, issue a \r so the line driver knows where we are
|
2016-04-29 19:14:10 +00:00
|
|
|
void reader_write_title(const wcstring &cmd, bool reset_cursor_position = true);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Call this function to tell the reader that a repaint is needed, and should be performed when
|
|
|
|
/// possible.
|
2007-10-05 14:59:19 +00:00
|
|
|
void reader_repaint_needed();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Call this function to tell the reader that some color has changed.
|
2012-03-25 10:00:38 +00:00
|
|
|
void reader_react_to_color_change();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Repaint immediately if needed.
|
2012-03-25 10:00:38 +00:00
|
|
|
void reader_repaint_if_needed();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Run the specified command with the correct terminal modes, and while taking care to perform job
|
|
|
|
/// notification, set the title, etc.
|
2013-04-07 19:40:08 +00:00
|
|
|
void reader_run_command(const wcstring &buff);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Get the string of character currently entered into the command buffer, or 0 if interactive mode
|
|
|
|
/// is uninitialized.
|
2012-02-06 09:45:16 +00:00
|
|
|
const wchar_t *reader_get_buffer();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Returns the current reader's history.
|
2012-02-06 00:42:24 +00:00
|
|
|
history_t *reader_get_history(void);
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Set the string of characters in the command buffer, as well as the cursor position.
|
|
|
|
///
|
|
|
|
/// \param b the new buffer value
|
|
|
|
/// \param p the cursor position. If \c p is larger than the length of the command line, the cursor
|
|
|
|
/// is placed on the last character.
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_set_buffer(const wcstring &b, size_t p);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Get the current cursor position in the command line. If interactive mode is uninitialized,
|
2016-05-04 22:19:47 +00:00
|
|
|
/// return (size_t)-1.
|
2012-08-04 20:47:56 +00:00
|
|
|
size_t reader_get_cursor_pos();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Get the current selection range in the command line. Returns false if there is no active
|
|
|
|
/// selection, true otherwise.
|
2014-03-29 21:19:45 +00:00
|
|
|
bool reader_get_selection(size_t *start, size_t *len);
|
2014-01-15 14:07:22 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Return the value of the interrupted flag, which is set by the sigint handler, and clear it if it
|
|
|
|
/// was set.
|
2006-10-04 21:42:04 +00:00
|
|
|
int reader_interrupted();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Clear the interrupted flag unconditionally without handling anything. The flag could have been
|
|
|
|
/// set e.g. when an interrupt arrived just as we were ending an earlier \c reader_readline
|
|
|
|
/// invocation but before the \c is_interactive_read flag was cleared.
|
2013-01-20 21:23:27 +00:00
|
|
|
void reader_reset_interrupted();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Return the value of the interrupted flag, which is set by the sigint handler, and clear it if it
|
|
|
|
/// was set. If the current reader is interruptible, call \c reader_exit().
|
2013-01-22 10:00:02 +00:00
|
|
|
int reader_reading_interrupted();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Returns true if the current reader generation count does not equal the generation count the
|
|
|
|
/// current thread was started with. Note 1: currently only valid for autocompletion threads! Other
|
|
|
|
/// threads don't set the threadlocal generation count when they start up.
|
2013-02-05 20:18:22 +00:00
|
|
|
bool reader_thread_job_is_stale();
|
2013-01-22 23:19:29 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Read one line of input. Before calling this function, reader_push() must have been called in
|
|
|
|
/// order to set up a valid reader environment. If nchars > 0, return after reading that many
|
|
|
|
/// characters even if a full line has not yet been read. Note: the returned value may be longer
|
|
|
|
/// than nchars if a single keypress resulted in multiple characters being inserted into the
|
|
|
|
/// commandline.
|
2014-08-19 12:28:08 +00:00
|
|
|
const wchar_t *reader_readline(int nchars);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Push a new reader environment.
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_push(const wchar_t *name);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Return to previous reader environment.
|
2005-09-20 13:26:39 +00:00
|
|
|
void reader_pop();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Specify function to use for finding possible tab completions. The function must take these
|
|
|
|
/// arguments:
|
|
|
|
///
|
|
|
|
/// - The command to be completed as a null terminated array of wchar_t
|
|
|
|
/// - An array_list_t in which completions will be inserted.
|
|
|
|
typedef void (*complete_function_t)(const wcstring &, std::vector<completion_t> *,
|
|
|
|
completion_request_flags_t, const env_vars_snapshot_t &);
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_set_complete_function(complete_function_t);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// The type of a highlight function.
|
|
|
|
typedef void (*highlight_function_t)(const wcstring &, std::vector<highlight_spec_t> &, size_t,
|
|
|
|
wcstring_list_t *, const env_vars_snapshot_t &vars);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Specify function for syntax highlighting. The function must take these arguments:
|
|
|
|
///
|
|
|
|
/// - The command to be highlighted as a null terminated array of wchar_t
|
|
|
|
/// - The color code of each character as an array of ints
|
|
|
|
/// - The cursor position
|
|
|
|
/// - An array_list_t used for storing error messages
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_set_highlight_function(highlight_function_t);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Specify function for testing if the command buffer contains syntax errors that must be corrected
|
|
|
|
/// before returning.
|
2015-05-03 00:49:38 +00:00
|
|
|
void reader_set_test_function(parser_test_error_bits_t (*f)(const wchar_t *));
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Specify string of shell commands to be run in order to generate the prompt.
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_set_left_prompt(const wcstring &prompt);
|
2012-11-08 03:59:20 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Specify string of shell commands to be run in order to generate the right prompt.
|
2012-11-19 00:30:30 +00:00
|
|
|
void reader_set_right_prompt(const wcstring &prompt);
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Sets whether autosuggesting is allowed.
|
2012-11-18 10:43:35 +00:00
|
|
|
void reader_set_allow_autosuggesting(bool flag);
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Sets whether abbreviation expansion is performed.
|
2013-07-17 07:38:04 +00:00
|
|
|
void reader_set_expand_abbreviations(bool flag);
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Sets whether the reader should exit on ^C.
|
2013-01-22 10:25:17 +00:00
|
|
|
void reader_set_exit_on_interrupt(bool flag);
|
2013-01-20 21:23:27 +00:00
|
|
|
|
2017-04-09 18:48:21 +00:00
|
|
|
void reader_set_silent_status(bool f);
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Returns true if the shell is exiting, 0 otherwise.
|
2013-12-30 00:23:26 +00:00
|
|
|
bool shell_is_exiting();
|
2005-09-20 13:26:39 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// The readers interrupt signal handler. Cancels all currently running blocks.
|
2016-10-09 21:38:26 +00:00
|
|
|
void reader_handle_sigint();
|
2005-10-05 22:37:08 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// This function returns true if fish is exiting by force, i.e. because stdin died.
|
2017-01-24 23:15:38 +00:00
|
|
|
bool reader_exit_forced();
|
2006-05-14 10:16:23 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Test if the given shell command contains errors. Uses parser_test for testing. Suitable for
|
|
|
|
/// reader_set_test_function().
|
2015-05-03 00:49:38 +00:00
|
|
|
parser_test_error_bits_t reader_shell_test(const wchar_t *b);
|
2007-01-29 16:26:24 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Test whether the interactive reader is in search mode.
|
|
|
|
///
|
|
|
|
/// \return 0 if not in search mode, 1 if in search mode and -1 if not in interactive mode
|
2007-09-21 14:05:49 +00:00
|
|
|
int reader_search_mode();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Test whether the interactive reader has visible pager contents.
|
|
|
|
///
|
|
|
|
/// \return 0 if it has pager contents, 1 if it does not have pager contents, and -1 if not in
|
|
|
|
/// interactive mode
|
2014-01-17 20:53:01 +00:00
|
|
|
int reader_has_pager_contents();
|
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Given a command line and an autosuggestion, return the string that gets shown to the user.
|
|
|
|
/// Exposed for testing purposes only.
|
|
|
|
wcstring combine_command_and_autosuggestion(const wcstring &cmdline,
|
|
|
|
const wcstring &autosuggestion);
|
2014-01-17 20:53:01 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Expand abbreviations at the given cursor position. Exposed for testing purposes only.
|
|
|
|
bool reader_expand_abbreviation_in_command(const wcstring &cmdline, size_t cursor_pos,
|
|
|
|
wcstring *output);
|
2013-07-17 07:38:04 +00:00
|
|
|
|
2016-05-03 17:36:22 +00:00
|
|
|
/// Apply a completion string. Exposed for testing only.
|
|
|
|
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);
|
2013-02-02 22:50:22 +00:00
|
|
|
|
2005-10-04 15:11:39 +00:00
|
|
|
#endif
|