]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libedit/editline.3
This commit was generated by cvs2svn to compensate for changes in r58653,
[FreeBSD/FreeBSD.git] / lib / libedit / editline.3
1 .\"     $NetBSD: editline.3,v 1.4 1997/01/14 04:17:23 lukem Exp $
2 .\"
3 .\" Copyright (c) 1997 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
5 .\"
6 .\" This file was contributed to The NetBSD Foundation by Luke Mewburn.
7 .\"
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
10 .\" are met:
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\"    notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\"    notice, this list of conditions and the following disclaimer in the
15 .\"    documentation and/or other materials provided with the distribution.
16 .\" 3. All advertising materials mentioning features or use of this software
17 .\"    must display the following acknowledgement:
18 .\"        This product includes software developed by the NetBSD
19 .\"        Foundation, Inc. and its contributors.
20 .\" 4. Neither the name of The NetBSD Foundation nor the names of its
21 .\"    contributors may be used to endorse or promote products derived
22 .\"    from this software without specific prior written permission.
23 .\"
24 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
25 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 .\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
28 .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 .\" POSSIBILITY OF SUCH DAMAGE.
35 .\"
36 .\" $FreeBSD$
37 .\"
38 .Dd January 11, 1997
39 .Os BSD 4.4
40 .Dt EDITLINE 3
41 .Sh NAME
42 .Nm editline ,
43 .Nm el_init ,
44 .Nm el_end ,
45 .Nm el_reset ,
46 .Nm el_gets ,
47 .Nm el_getc ,
48 .Nm el_push ,
49 .Nm el_parse ,
50 .Nm el_set ,
51 .Nm el_source ,
52 .Nm el_resize ,
53 .Nm el_line ,
54 .Nm el_insertstr ,
55 .Nm el_deletestr ,
56 .Nm el_data_set ,
57 .Nm el_data_get ,
58 .Nm history_init ,
59 .Nm history_end ,
60 .Nm history
61 .Nd line editor and history functions
62 .Sh SYNOPSIS
63 .Fd #include <histedit.h>
64 .Ft EditLine *
65 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout"
66 .Ft void
67 .Fn el_end "EditLine *e"
68 .Ft void
69 .Fn el_reset "EditLine *e"
70 .Ft const char *
71 .Fn el_gets "EditLine *e" "int *count"
72 .Ft int
73 .Fn el_getc "EditLine *e" "char *ch"
74 .Ft void
75 .Fn el_push "EditLine *e" "const char *str"
76 .Ft int
77 .Fn el_parse "EditLine *e" "int argc" "char *argv[]"
78 .Ft int
79 .Fn el_set "EditLine *e" "int op" "..."
80 .Ft int
81 .Fn el_source "EditLine *e" "const char *file"
82 .Ft void
83 .Fn el_resize "EditLine *e"
84 .Ft const LineInfo *
85 .Fn el_line "EditLine *e"
86 .Ft int
87 .Fn el_insertstr "EditLine *e" "char *str"
88 .Ft void
89 .Fn el_deletestr "EditLine *e" "int count"
90 .Ft void
91 .Fn el_data_set "EditLine *e" "void *data"
92 .Ft void *
93 .Fn el_data_get "EditLine *e"
94 .Ft History *
95 .Fn history_init
96 .Ft void
97 .Fn history_end "History *h"
98 .Ft const HistEvent *
99 .Fn history "History *h" "int op" "..."
100 .Sh DESCRIPTION
101 The
102 .Nm
103 library provides generic line editing and history functions,
104 similar to those found in
105 .Xr sh 1 .
106 .Pp
107 These functions are available in the
108 .Nm libedit
109 library (which needs the
110 .Nm libtermcap
111 library).
112 Programs should be linked with
113 .Fl ledit ltermcap .
114 .Sh LINE EDITING FUNCTIONS
115 The line editing functions use a common data structure,
116 .Fa EditLine ,
117 which is created by
118 .Fn el_init
119 and freed by
120 .Fn el_end .
121 .Pp
122 The following functions are available:
123 .Bl -tag -width 4n
124 .It Fn el_init
125 Initialise the line editor, and return a data structure
126 to be used by all other line editing functions.
127 .Fa prog
128 is the name of the invoking program, used when reading the
129 .Xr editrc 5
130 file to determine which settings to use.
131 .Fa fin
132 and
133 .Fa fout
134 are the input and output streams (respectively) to use.
135 In this documentation, references to
136 .Dq the tty
137 are actually to this input/output stream combination.
138 .It Fn el_end
139 Clean up and finish with
140 .Fa e ,
141 assumed to have been created with
142 .Fn el_init .
143 .It Fn el_reset
144 Reset the tty and the parser.
145 This should be called after an error which may have upset the tty's
146 state.
147 .It Fn el_gets
148 Read a line from the tty.
149 .Fa count
150 is modified to contain the number of characters read.
151 Returns the line read if successful, or
152 .Dv NULL
153 if no characters were read or if an error occurred.
154 .It Fn el_getc
155 Read a character from the tty.
156 .Fa ch
157 is modified to contain the character read.
158 Returns the number of characters read if successful, -1 otherwise.
159 .It Fn el_push
160 Pushes
161 .Fa str
162 back onto the input stream.
163 This is used by the macro expansion mechanism.
164 Refer to the description of
165 .Ic bind
166 .Fl s
167 in
168 .Xr editrc 5
169 for more information.
170 .It Fn el_parse
171 Parses the
172 .Fa argv
173 array (which is
174 .Fa argc
175 elements in size)
176 to execute builtin
177 .Nm
178 commands.
179 If the command is prefixed with
180 .Dq prog:
181 then
182 .Fn el_parse
183 will only execute the command if
184 .Dq prog
185 matches the
186 .Fa prog
187 argument supplied to
188 .Fn el_init .
189 The return value is
190 -1 if the command is unknown,
191 0 if there was no error or
192 .Dq prog
193 didn't match, or
194 1 if the command returned an error.
195 Refer to
196 .Xr editrc 5
197 for more information.
198 .Pp
199 .Em NOTE:
200 .Va argv[0]
201 may be modified by
202 .Fn el_parse .
203 The colon between
204 .Dq prog
205 and the command, 
206 .Ar command ,
207 will be replaced with a NUL
208 .Po
209 .Dq \e0
210 .Pc .
211 .It Fn el_set
212 Set
213 .Nm
214 parameters.
215 .Fa op
216 determines which parameter to set, and each operation has its
217 own parameter list.
218 .Pp
219 The following values for
220 .Fa op
221 are supported, along with the required argument list:
222 .Bl -tag -width 4n
223 .It Dv EL_PROMPT , Fa "char *(*f)(EditLine *)"
224 Define prompt printing function as
225 .Fa f ,
226 which is to return a string that contains the prompt.
227 .It Dv EL_TERMINAL , Fa "const char *type"
228 Define terminal type of the tty to be
229 .Fa type ,
230 or to
231 .Ev TERM
232 if
233 .Fa type
234 is
235 .Dv NULL .
236 .It Dv EL_EDITOR , Fa "const char *mode"
237 Set editing mode to
238 .Fa mode ,
239 which must be one of
240 .Dq emacs
241 or
242 .Dq vi .
243 .It Dv EL_SIGNAL , Fa "int flag"
244 If
245 .Fa flag
246 is non-zero,
247 .Nm
248 will install its own signal handler for the following signals when
249 reading command input:
250 .Dv SIGCONT ,
251 .Dv SIGHUP ,
252 .Dv SIGINT ,
253 .Dv SIGQUIT ,
254 .Dv SIGSTOP ,
255 .Dv SIGTERM ,
256 .Dv SIGTSTP ,
257 and
258 .Dv SIGWINCH .
259 Otherwise, the current signal handlers will be used.
260 .It Dv EL_BIND , Xo
261 .Fa "const char *" ,
262 .Fa "..." ,
263 .Dv NULL
264 .Xc
265 Perform the
266 .Ic bind
267 builtin command.
268 Refer to 
269 .Xr editrc 5
270 for more information.
271 .It Dv EL_ECHOTC , Xo
272 .Fa "const char *" ,
273 .Fa "..." ,
274 .Dv NULL
275 .Xc
276 Perform the
277 .Ic echotc
278 builtin command.
279 Refer to 
280 .Xr editrc 5
281 for more information.
282 .It Dv EL_SETTC , Xo
283 .Fa "const char *" ,
284 .Fa "..." ,
285 .Dv NULL
286 .Xc
287 Perform the
288 .Ic settc
289 builtin command.
290 Refer to 
291 .Xr editrc 5
292 for more information.
293 .It Dv EL_SETTY , Xo
294 .Fa "const char *" ,
295 .Fa "..." ,
296 .Dv NULL
297 .Xc
298 Perform the
299 .Ic setty
300 builtin command.
301 Refer to 
302 .Xr editrc 5
303 for more information.
304 .It Dv EL_TELLTC , Xo
305 .Fa "const char *" ,
306 .Fa "..." ,
307 .Dv NULL
308 .Xc
309 Perform the
310 .Ic telltc
311 builtin command.
312 Refer to 
313 .Xr editrc 5
314 for more information.
315 .It Dv EL_ADDFN , Xo
316 .Fa "const char *name" ,
317 .Fa "const char *help" ,
318 .Fa "unsigned char (*func)(EditLine *e, int ch)
319 .Xc
320 Add a user defined function,
321 .Fn func ,
322 referred to as
323 .Fa name
324 which is invoked when a key which is bound to
325 .Fa name
326 is entered.
327 .Fa help
328 is a description of
329 .Fa name .
330 At invocation time,
331 .Fa ch
332 is the key which caused the invocation.
333 The return value of
334 .Fn func
335 should be one of:
336 .Bl -tag -width "CC_REDISPLAY"
337 .It Dv CC_NORM
338 Add a normal character.
339 .It Dv CC_NEWLINE
340 End of line was entered.
341 .It Dv CC_EOF
342 EOF was entered.
343 .It Dv CC_ARGHACK
344 Expecting further command input as arguments, do nothing visually.
345 .It Dv CC_REFRESH
346 Refresh display.
347 .It Dv CC_CURSOR
348 Cursor moved, so update and perform
349 .Dv CC_REFRESH.
350 .It Dv CC_REDISPLAY
351 Redisplay entire input line.
352 This is useful if a key binding outputs extra information.
353 .It Dv CC_ERROR
354 An error occurred.
355 Beep, and flush tty.
356 .It Dv CC_FATAL
357 Fatal error, reset tty to known state.
358 .El
359 .It Dv EL_HIST , Xo
360 .Fa "History *(*func)(History *, int op, ...)" ,
361 .Fa "const char *ptr"
362 .Xc
363 Defines which history function to use, which is usually
364 .Fn history .
365 .Fa ptr
366 should be the value returned by
367 .Fn history_init .
368 .El
369 .It Fn el_source
370 Initialise
371 .Nm
372 by reading the contents of 
373 .Fa file .
374 .Fn el_parse
375 is called for each line in
376 .Fa file .
377 If
378 .Fa file
379 is
380 .Dv NULL ,
381 try
382 .Pa $PWD/.editrc
383 then
384 .Pa $HOME/.editrc .
385 Refer to
386 .Xr editrc 5
387 for details on the format of
388 .Fa file .
389 .It Fn el_resize
390 Must be called if the terminal size changes.
391 If
392 .Dv EL_SIGNAL
393 has been set with
394 .Fn el_set ,
395 then this is done automatically.
396 Otherwise, it's the responsibility of the application to call
397 .Fn el_resize
398 on the appropriate occasions.
399 .It Fn el_line
400 Return the editing information for the current line in a
401 .Fa LineInfo
402 structure, which is defined as follows:
403 .Bd -literal
404 typedef struct lineinfo {
405     const char *buffer;    /* address of buffer */
406     const char *cursor;    /* address of cursor */
407     const char *lastchar;  /* address of last character */
408 } LineInfo;
409 .Ed
410 .It Fn el_insertstr
411 Insert
412 .Fa str
413 into the line at the cursor.
414 Returns -1 if
415 .Fa str
416 is empty or won't fit, and 0 otherwise.
417 .It Fn el_deletestr
418 Delete
419 .Fa num
420 characters before the cursor.
421 .It Fn el_data_set
422 Set the user data to
423 .Fa data
424 .
425 .It Fn el_data_get
426 Get the user data.
427 .El
428 .Sh HISTORY LIST FUNCTIONS
429 The history functions use a common data structure,
430 .Fa History ,
431 which is created by
432 .Fn history_init
433 and freed by
434 .Fn history_end .
435 .Pp
436 The following functions are available:
437 .Bl -tag -width 4n
438 .It Fn history_init
439 Initialise the history list, and return a data structure
440 to be used by all other history list functions.
441 .It Fn history_end
442 Clean up and finish with
443 .Fa h ,
444 assumed to have been created with
445 .Fn history_init .
446 .It Fn history
447 Perform operation
448 .Fa op
449 on the history list, with optional arguments as needed by the
450 operation.
451 The following values for
452 .Fa op
453 are supported, along with the required argument list:
454 .Bl -tag -width 4n
455 .It Dv H_EVENT , Fa "int size"
456 Set size of history to
457 .Fa size
458 elements.
459 .It Dv H_END
460 Cleans up and finishes with
461 .Fa h ,
462 assumed to be created with
463 .Fn history_init .
464 .It Dv H_CLEAR
465 Clear the history.
466 .It Dv H_FUNC , Xo
467 .Fa "void *ptr" ,
468 .Fa "history_gfun_t first" ,
469 .Fa "history_gfun_t next" ,
470 .Fa "history_gfun_t last" ,
471 .Fa "history_gfun_t prev" ,
472 .Fa "history_gfun_t curr" ,
473 .Fa "history_vfun_t clear" ,
474 .Fa "history_efun_t enter" ,
475 .Fa "history_efun_t add"
476 .Xc
477 Define functions to perform various history operations.
478 .Fa ptr
479 is the argument given to a function when it's invoked.
480 .It Dv H_FIRST
481 Return the first element in the history.
482 .It Dv H_LAST
483 Return the last element in the history.
484 .It Dv H_PREV
485 Return the previous element in the history.
486 .It Dv H_NEXT
487 Return the next element in the history.
488 .It Dv H_CURR
489 Return the current element in the history.
490 .It Dv H_ADD , Fa "const char *str"
491 Append
492 .Fa str
493 to the current element of the history, or create an element with
494 .Dv H_ENTER
495 if there isn't one.
496 .It Dv H_ENTER , Fa "const char *str"
497 Add
498 .Fa str
499 as a new element to the history, and, if necessary, 
500 removing the oldest entry to keep the list to the created size.
501 .It Dv H_PREV_STR , Fa "const char *str"
502 Return the closest previous event that starts with
503 .Fa str .
504 .It Dv H_NEXT_STR , Fa "const char *str"
505 Return the closest next event that starts with
506 .Fa str .
507 .It Dv H_PREV_EVENT , Fa "int e"
508 Return the previous event numbered
509 .Fa e .
510 .It Dv H_NEXT_EVENT , Fa "int e"
511 Return the next event numbered
512 .Fa e .
513 .It Dv H_LOAD , Fa "const char *file"
514 Load the history list stored in
515 .Fa file .
516 .It Dv H_SAVE , Fa "const char *file"
517 Save the history list to
518 .Fa file .
519 .El
520 .El
521 .\"XXX.Sh EXAMPLES
522 .\"XXX: provide some examples
523 .Sh SEE ALSO
524 .Xr sh 1 ,
525 .Xr signal 3 ,
526 .Xr termcap 3 ,
527 .Xr editrc 5
528 .Sh HISTORY
529 The
530 .Nm
531 library first appeared in
532 .Bx 4.4 .
533 .Sh AUTHORS
534 The
535 .Nm
536 library was written by
537 .An Christos Zoulas ,
538 and this manual was written by
539 .An Luke Mewburn .
540 .Sh BUGS
541 This documentation is probably incomplete.
542 .Pp
543 .Fn el_parse
544 should not modify the supplied
545 .Va argv[0] .
546 .Pp
547 The tokenization functions are not publicly defined in
548 .Li <histedit.h> .