aboutsummaryrefslogtreecommitdiffhomepage
path: root/function.h
blob: 2db437949804672e4c23297b37b7f1295ee4bfc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** \file function.h 

    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.
*/

#ifndef FISH_FUNCTION_H
#define FISH_FUNCTION_H

#include <wchar.h>

#include "util.h"
#include "common.h"

#include <tr1/memory>
using std::tr1::shared_ptr;

class parser_t;

/**
   Structure describing a function. This is used by the parser to
   store data on a function while parsing it. It is not used
   internally to store functions, the function_internal_data_t
   structure is used for that purpose. Parhaps these two should be
   merged.
  */
typedef struct function_data
{
	/**
	   Name of function
	 */
	wchar_t *name;
	/**
	   Description of function
	 */
	wchar_t *description;
	/**
	   Function definition
	 */
	wchar_t *definition;
	/**
	   List of all event handlers for this function
	 */
	array_list_t *events;
	/**
	   List of all named arguments for this function
	 */
	array_list_t *named_arguments;
	/**
	   Set to non-zero if invoking this function shadows the variables
	   of the underlying function.
	 */
	int shadows;
} function_data_t;

class function_info_t {
public:
    /** Function definition */
    wcstring definition;
    
    /** Function description */
    wcstring description;
    
	/**
	   File where this function was defined (intern'd string)
	*/
    const wchar_t *definition_file;
    
	/**
	   Line where definition started
	*/
	int definition_offset;
    
	/**
	   List of all named arguments for this function
	 */
    wcstring_list_t named_arguments;
    
	/**
	   Flag for specifying that this function was automatically loaded
	*/
    bool is_autoload;
    
	/**
	   Set to non-zero if invoking this function shadows the variables
	   of the underlying function.
	 */
	bool shadows;
};


/**
   Initialize function data   
*/
void function_init();

/**
   Add a function. The parameters values are copied and should be
   freed by the caller.
*/
void function_add( function_data_t *data, const parser_t &parser );

/**
   Remove the function with the specified name.
*/
void function_remove( const wchar_t *name );

/**
    Gets a function by name.
*/
shared_ptr<function_info_t> function_get(const wcstring &name);

/**
   Returns the definition of the function with the name \c name.
*/
const wchar_t *function_get_definition( const wchar_t *name );

/**
   Returns the description of the function with the name \c name.
*/
const wchar_t *function_get_desc( const wchar_t *name );

/**
   Sets the description of the function with the name \c name.
*/
void function_set_desc( const wchar_t *name, const wchar_t *desc );

/**
   Returns true if the function with the name name exists.
*/
int function_exists( const wchar_t *name );

/**
   Returns true if the function with the name name exists, without triggering autoload.
*/
int function_exists_no_autoload( const wchar_t *name );

/**
   Returns all function names.
   
   \param get_hidden whether to include hidden functions, i.e. ones starting with an underscore
*/
wcstring_list_t function_get_names( int get_hidden );

/**
   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.
*/
const wchar_t *function_get_definition_file( const wchar_t *name );

/**
   Returns the linenumber where the definition of the specified
   function started.

   This function does not autoload functions, it will only work on
   functions that have already been defined.
*/
int function_get_definition_offset( const wchar_t *name );

/**
   Returns a list of all named arguments of the specified function.
*/
wcstring_list_t function_get_named_arguments( const wchar_t *name );

/**
   Creates a new function using the same definition as the specified function.
   Returns non-zero if copy is successful.
*/
int function_copy( const wchar_t *name, const wchar_t *new_name );


/**
   Returns whether this function shadows variables of the underlying function
*/
int function_get_shadows( const wchar_t *name );

#endif