Initial import from FreeBSD RELENG_4:
[dragonfly.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: src/lib/libedit/editline.3,v 1.8.2.7 2001/12/17 10:08:30 ru Exp $
37 .\"
38 .Dd January 11, 1997
39 .Os
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 LIBRARY
63 .Lb libedit
64 .Sh SYNOPSIS
65 .In histedit.h
66 .Ft EditLine *
67 .Fn el_init "const char *prog" "FILE *fin" "FILE *fout"
68 .Ft void
69 .Fn el_end "EditLine *e"
70 .Ft void
71 .Fn el_reset "EditLine *e"
72 .Ft const char *
73 .Fn el_gets "EditLine *e" "int *count"
74 .Ft int
75 .Fn el_getc "EditLine *e" "char *ch"
76 .Ft void
77 .Fn el_push "EditLine *e" "const char *str"
78 .Ft int
79 .Fn el_parse "EditLine *e" "int argc" "char *argv[]"
80 .Ft int
81 .Fn el_set "EditLine *e" "int op" "..."
82 .Ft int
83 .Fn el_source "EditLine *e" "const char *file"
84 .Ft void
85 .Fn el_resize "EditLine *e"
86 .Ft const LineInfo *
87 .Fn el_line "EditLine *e"
88 .Ft int
89 .Fn el_insertstr "EditLine *e" "char *str"
90 .Ft void
91 .Fn el_deletestr "EditLine *e" "int count"
92 .Ft void
93 .Fn el_data_set "EditLine *e" "void *data"
94 .Ft void *
95 .Fn el_data_get "EditLine *e"
96 .Ft History *
97 .Fn history_init
98 .Ft void
99 .Fn history_end "History *h"
100 .Ft const HistEvent *
101 .Fn history "History *h" "int op" "..."
102 .Sh DESCRIPTION
103 The
104 .Nm
105 library provides generic line editing and history functions,
106 similar to those found in
107 .Xr sh 1 .
108 .Pp
109 These functions are available in the
110 .Nm libedit
111 library (which needs the
112 .Nm libtermcap
113 library).
114 Programs should be linked with
115 .Fl ledit ltermcap .
116 .Sh LINE EDITING FUNCTIONS
117 The line editing functions use a common data structure,
118 .Fa EditLine ,
119 which is created by
120 .Fn el_init
121 and freed by
122 .Fn el_end .
123 .Pp
124 The following functions are available:
125 .Bl -tag -width 4n
126 .It Fn el_init
127 Initialise the line editor, and return a data structure
128 to be used by all other line editing functions.
129 .Fa prog
130 is the name of the invoking program, used when reading the
131 .Xr editrc 5
132 file to determine which settings to use.
133 .Fa fin
134 and
135 .Fa fout
136 are the input and output streams (respectively) to use.
137 In this documentation, references to
138 .Dq the tty
139 are actually to this input/output stream combination.
140 .It Fn el_end
141 Clean up and finish with
142 .Fa e ,
143 assumed to have been created with
144 .Fn el_init .
145 .It Fn el_reset
146 Reset the tty and the parser.
147 This should be called after an error which may have upset the tty's
148 state.
149 .It Fn el_gets
150 Read a line from the tty.
151 .Fa count
152 is modified to contain the number of characters read.
153 Returns the line read if successful, or
154 .Dv NULL
155 if no characters were read or if an error occurred.
156 .It Fn el_getc
157 Read a character from the tty.
158 .Fa ch
159 is modified to contain the character read.
160 Returns the number of characters read if successful, -1 otherwise.
161 .It Fn el_push
162 Pushes
163 .Fa str
164 back onto the input stream.
165 This is used by the macro expansion mechanism.
166 Refer to the description of
167 .Ic bind
168 .Fl s
169 in
170 .Xr editrc 5
171 for more information.
172 .It Fn el_parse
173 Parses the
174 .Fa argv
175 array (which is
176 .Fa argc
177 elements in size)
178 to execute builtin
179 .Nm
180 commands.
181 If the command is prefixed with
182 .Dq prog:
183 then
184 .Fn el_parse
185 will only execute the command if
186 .Dq prog
187 matches the
188 .Fa prog
189 argument supplied to
190 .Fn el_init .
191 The return value is
192 -1 if the command is unknown,
193 0 if there was no error or
194 .Dq prog
195 didn't match, or
196 1 if the command returned an error.
197 Refer to
198 .Xr editrc 5
199 for more information.
200 .Pp
201 .Em NOTE :
202 .Va argv[0]
203 may be modified by
204 .Fn el_parse .
205 The colon between
206 .Dq prog
207 and the command,
208 .Ar command ,
209 will be replaced with a NUL
210 .Pq Dq \e0 .
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 $HOME/.editrc .
383 Refer to
384 .Xr editrc 5
385 for details on the format of
386 .Fa file .
387 .It Fn el_resize
388 Must be called if the terminal size changes.
389 If
390 .Dv EL_SIGNAL
391 has been set with
392 .Fn el_set ,
393 then this is done automatically.
394 Otherwise, it's the responsibility of the application to call
395 .Fn el_resize
396 on the appropriate occasions.
397 .It Fn el_line
398 Return the editing information for the current line in a
399 .Fa LineInfo
400 structure, which is defined as follows:
401 .Bd -literal
402 typedef struct lineinfo {
403     const char *buffer;    /* address of buffer */
404     const char *cursor;    /* address of cursor */
405     const char *lastchar;  /* address of last character */
406 } LineInfo;
407 .Ed
408 .It Fn el_insertstr
409 Insert
410 .Fa str
411 into the line at the cursor.
412 Returns -1 if
413 .Fa str
414 is empty or won't fit, and 0 otherwise.
415 .It Fn el_deletestr
416 Delete
417 .Fa num
418 characters before the cursor.
419 .It Fn el_data_set
420 Set the user data to
421 .Fa data .
422 .It Fn el_data_get
423 Get the user data.
424 .El
425 .Sh HISTORY LIST FUNCTIONS
426 The history functions use a common data structure,
427 .Fa History ,
428 which is created by
429 .Fn history_init
430 and freed by
431 .Fn history_end .
432 .Pp
433 The following functions are available:
434 .Bl -tag -width 4n
435 .It Fn history_init
436 Initialise the history list, and return a data structure
437 to be used by all other history list functions.
438 .It Fn history_end
439 Clean up and finish with
440 .Fa h ,
441 assumed to have been created with
442 .Fn history_init .
443 .It Fn history
444 Perform operation
445 .Fa op
446 on the history list, with optional arguments as needed by the
447 operation.
448 The following values for
449 .Fa op
450 are supported, along with the required argument list:
451 .Bl -tag -width 4n
452 .It Dv H_EVENT , Fa "int size"
453 Set size of history to
454 .Fa size
455 elements.
456 .It Dv H_END
457 Cleans up and finishes with
458 .Fa h ,
459 assumed to be created with
460 .Fn history_init .
461 .It Dv H_CLEAR
462 Clear the history.
463 .It Dv H_FUNC , Xo
464 .Fa "void *ptr" ,
465 .Fa "history_gfun_t first" ,
466 .Fa "history_gfun_t next" ,
467 .Fa "history_gfun_t last" ,
468 .Fa "history_gfun_t prev" ,
469 .Fa "history_gfun_t curr" ,
470 .Fa "history_vfun_t clear" ,
471 .Fa "history_efun_t enter" ,
472 .Fa "history_efun_t add"
473 .Xc
474 Define functions to perform various history operations.
475 .Fa ptr
476 is the argument given to a function when it's invoked.
477 .It Dv H_FIRST
478 Return the first element in the history.
479 .It Dv H_LAST
480 Return the last element in the history.
481 .It Dv H_PREV
482 Return the previous element in the history.
483 .It Dv H_NEXT
484 Return the next element in the history.
485 .It Dv H_CURR
486 Return the current element in the history.
487 .It Dv H_ADD , Fa "const char *str"
488 Append
489 .Fa str
490 to the current element of the history, or create an element with
491 .Dv H_ENTER
492 if there isn't one.
493 .It Dv H_ENTER , Fa "const char *str"
494 Add
495 .Fa str
496 as a new element to the history, and, if necessary,
497 removing the oldest entry to keep the list to the created size.
498 .It Dv H_PREV_STR , Fa "const char *str"
499 Return the closest previous event that starts with
500 .Fa str .
501 .It Dv H_NEXT_STR , Fa "const char *str"
502 Return the closest next event that starts with
503 .Fa str .
504 .It Dv H_PREV_EVENT , Fa "int e"
505 Return the previous event numbered
506 .Fa e .
507 .It Dv H_NEXT_EVENT , Fa "int e"
508 Return the next event numbered
509 .Fa e .
510 .It Dv H_LOAD , Fa "const char *file"
511 Load the history list stored in
512 .Fa file .
513 .It Dv H_SAVE , Fa "const char *file"
514 Save the history list to
515 .Fa file .
516 .El
517 .El
518 .\"XXX.Sh EXAMPLES
519 .\"XXX: provide some examples
520 .Sh SEE ALSO
521 .Xr sh 1 ,
522 .Xr signal 3 ,
523 .Xr termcap 3 ,
524 .Xr editrc 5
525 .Sh HISTORY
526 The
527 .Nm
528 library first appeared in
529 .Bx 4.4 .
530 .Sh AUTHORS
531 .An -nosplit
532 The
533 .Nm
534 library was written by
535 .An Christos Zoulas ,
536 and this manual was written by
537 .An Luke Mewburn .
538 .Sh BUGS
539 This documentation is probably incomplete.
540 .Pp
541 .Fn el_parse
542 should not modify the supplied
543 .Va argv[0] .
544 .Pp
545 The tokenization functions are not publicly defined in
546 .Li <histedit.h> .