2016-05-01 11:31:02 +08:00
|
|
|
// Prototypes for functions for storing and retrieving function information. These functions also
|
|
|
|
// take care of autoloading functions in the $fish_function_path. Actual function evaluation is
|
|
|
|
// taken care of by the parser and to some degree the builtin handling library.
|
2005-10-04 23:11:39 +08:00
|
|
|
#ifndef FISH_FUNCTION_H
|
|
|
|
#define FISH_FUNCTION_H
|
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2012-01-14 15:44:18 +08:00
|
|
|
#include "common.h"
|
2014-10-03 06:59:24 +08:00
|
|
|
#include "env.h"
|
2016-05-01 11:31:02 +08:00
|
|
|
#include "event.h"
|
2018-02-10 13:53:06 +08:00
|
|
|
#include "parse_tree.h"
|
|
|
|
#include "tnode.h"
|
2005-10-04 23:11:39 +08:00
|
|
|
|
2012-01-17 04:10:08 +08:00
|
|
|
class parser_t;
|
|
|
|
|
2018-02-12 08:37:38 +08:00
|
|
|
/// A function's constant properties. These do not change once initialized.
|
|
|
|
struct function_properties_t {
|
|
|
|
/// Parsed source containing the function.
|
|
|
|
parsed_source_ref_t parsed_source;
|
|
|
|
|
|
|
|
/// Node containing the function body, pointing into parsed_source.
|
|
|
|
tnode_t<grammar::job_list> body_node;
|
|
|
|
|
|
|
|
/// List of all named arguments for this function.
|
|
|
|
wcstring_list_t named_arguments;
|
|
|
|
|
|
|
|
/// Set to true if invoking this function shadows the variables of the underlying function.
|
|
|
|
bool shadow_scope;
|
|
|
|
};
|
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Structure describing a function. This is used by the parser to store data on a function while
|
2018-02-11 09:38:57 +08:00
|
|
|
/// parsing it. It is not used internally to store functions, the function_info_t structure
|
|
|
|
/// is used for that purpose. Parhaps this should be merged with function_info_t.
|
2016-05-01 11:31:02 +08:00
|
|
|
struct function_data_t {
|
|
|
|
/// Name of function.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring name;
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Description of function.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring description;
|
2016-05-01 11:31:02 +08:00
|
|
|
/// List of all variables that are inherited from the function definition scope. The variable
|
|
|
|
/// values are snapshotted when function_add() is called.
|
2014-10-03 06:59:24 +08:00
|
|
|
wcstring_list_t inherit_vars;
|
2018-02-12 08:37:38 +08:00
|
|
|
/// Function's metadata fields
|
|
|
|
function_properties_t props;
|
|
|
|
/// List of all event handlers for this function.
|
|
|
|
std::vector<event_t> events;
|
2012-02-08 18:34:31 +08:00
|
|
|
};
|
2012-01-24 12:32:36 +08:00
|
|
|
|
2018-02-10 13:53:06 +08:00
|
|
|
/// Add a function.
|
|
|
|
void function_add(const function_data_t &data, const parser_t &parser);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Remove the function with the specified name.
|
2012-11-19 08:30:30 +08:00
|
|
|
void function_remove(const wcstring &name);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2018-02-12 09:21:24 +08:00
|
|
|
/// Returns the properties for a function, or nullptr if none. This does not trigger autoloading.
|
|
|
|
std::shared_ptr<const function_properties_t> function_get_properties(const wcstring &name);
|
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns by reference the definition of the function with the name \c name. Returns true if
|
|
|
|
/// successful, false if no function with the given name exists.
|
2018-02-12 09:21:24 +08:00
|
|
|
/// This does not trigger autoloading.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool function_get_definition(const wcstring &name, wcstring *out_definition);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns by reference the description of the function with the name \c name. Returns true if the
|
|
|
|
/// function exists and has a nonempty description, false if it does not.
|
2018-02-12 09:21:24 +08:00
|
|
|
/// This does not trigger autoloading.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool function_get_desc(const wcstring &name, wcstring *out_desc);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Sets the description of the function with the name \c name.
|
2012-11-19 08:30:30 +08:00
|
|
|
void function_set_desc(const wcstring &name, const wcstring &desc);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns true if the function with the name name exists.
|
2018-02-12 09:21:24 +08:00
|
|
|
/// This may autoload.
|
2012-11-19 08:30:30 +08:00
|
|
|
int function_exists(const wcstring &name);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Attempts to load a function if not yet loaded. This is used by the completion machinery.
|
2018-03-15 07:22:10 +08:00
|
|
|
bool function_load(const wcstring &name);
|
2015-10-08 09:59:41 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns true if the function with the name name exists, without triggering autoload.
|
2012-11-19 08:30:30 +08:00
|
|
|
int function_exists_no_autoload(const wcstring &name, const env_vars_snapshot_t &vars);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns all function names.
|
|
|
|
///
|
|
|
|
/// \param get_hidden whether to include hidden functions, i.e. ones starting with an underscore.
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring_list_t function_get_names(int get_hidden);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2017-01-10 14:49:33 +08:00
|
|
|
/// Returns true if the function was autoloaded.
|
|
|
|
bool function_is_autoloaded(const wcstring &name);
|
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns tha absolute path of the file where the specified function was defined. Returns 0 if the
|
|
|
|
/// file was defined on the commandline.
|
|
|
|
///
|
|
|
|
/// This function does not autoload functions, it will only work on functions that have already been
|
|
|
|
/// defined.
|
|
|
|
///
|
|
|
|
/// This returns an intern'd string.
|
2012-11-19 08:30:30 +08:00
|
|
|
const wchar_t *function_get_definition_file(const wcstring &name);
|
2006-01-26 22:48:10 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns the linenumber where the definition of the specified function started.
|
2018-02-12 09:21:24 +08:00
|
|
|
/// This does not trigger autoloading.
|
2018-02-10 13:53:06 +08:00
|
|
|
int function_get_definition_lineno(const wcstring &name);
|
2006-01-26 22:48:10 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Returns a mapping of all variables of the specified function that were inherited from the scope
|
|
|
|
/// of the function definition to their values.
|
|
|
|
std::map<wcstring, env_var_t> function_get_inherit_vars(const wcstring &name);
|
2014-10-03 06:59:24 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Creates a new function using the same definition as the specified function. Returns true if copy
|
|
|
|
/// is successful.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool function_copy(const wcstring &name, const wcstring &new_name);
|
2010-09-08 01:31:05 +08:00
|
|
|
|
2016-05-01 11:31:02 +08:00
|
|
|
/// Prepares the environment for executing a function.
|
|
|
|
void function_prepare_environment(const wcstring &name, const wchar_t *const *argv,
|
|
|
|
const std::map<wcstring, env_var_t> &inherited_vars);
|
2015-08-16 04:37:17 +08:00
|
|
|
|
2018-02-16 13:58:02 +08:00
|
|
|
/// Observes that fish_function_path has changed.
|
|
|
|
void function_invalidate_path();
|
|
|
|
|
2005-10-04 23:11:39 +08:00
|
|
|
#endif
|