Imported from ../bash-2.05a.tar.gz.
[platform/upstream/bash.git] / lib / readline / kill.c
1 /* kill.c -- kill ring management. */
2
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
4
5    This file is part of the GNU Readline Library, a library for
6    reading lines of text with interactive input and history editing.
7
8    The GNU Readline Library is free software; you can redistribute it
9    and/or modify it under the terms of the GNU General Public License
10    as published by the Free Software Foundation; either version 2, or
11    (at your option) any later version.
12
13    The GNU Readline Library is distributed in the hope that it will be
14    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    The GNU General Public License is often shipped with GNU software, and
19    is generally kept in a file called COPYING or LICENSE.  If you do not
20    have a copy of the license, write to the Free Software Foundation,
21    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
22 #define READLINE_LIBRARY
23
24 #if defined (HAVE_CONFIG_H)
25 #  include <config.h>
26 #endif
27
28 #include <sys/types.h>
29
30 #if defined (HAVE_UNISTD_H)
31 #  include <unistd.h>           /* for _POSIX_VERSION */
32 #endif /* HAVE_UNISTD_H */
33
34 #if defined (HAVE_STDLIB_H)
35 #  include <stdlib.h>
36 #else
37 #  include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
39
40 #include <stdio.h>
41
42 /* System-specific feature definitions and include files. */
43 #include "rldefs.h"
44
45 /* Some standard library routines. */
46 #include "readline.h"
47 #include "history.h"
48
49 #include "rlprivate.h"
50 #include "xmalloc.h"
51
52 /* **************************************************************** */
53 /*                                                                  */
54 /*                      Killing Mechanism                           */
55 /*                                                                  */
56 /* **************************************************************** */
57
58 /* What we assume for a max number of kills. */
59 #define DEFAULT_MAX_KILLS 10
60
61 /* The real variable to look at to find out when to flush kills. */
62 static int rl_max_kills =  DEFAULT_MAX_KILLS;
63
64 /* Where to store killed text. */
65 static char **rl_kill_ring = (char **)NULL;
66
67 /* Where we are in the kill ring. */
68 static int rl_kill_index;
69
70 /* How many slots we have in the kill ring. */
71 static int rl_kill_ring_length;
72
73 static int _rl_copy_to_kill_ring PARAMS((char *, int));
74 static int region_kill_internal PARAMS((int));
75 static int _rl_copy_word_as_kill PARAMS((int, int));
76 static int rl_yank_nth_arg_internal PARAMS((int, int, int));
77
78 /* How to say that you only want to save a certain amount
79    of kill material. */
80 int
81 rl_set_retained_kills (num)
82      int num;
83 {
84   return 0;
85 }
86
87 /* Add TEXT to the kill ring, allocating a new kill ring slot as necessary.
88    This uses TEXT directly, so the caller must not free it.  If APPEND is
89    non-zero, and the last command was a kill, the text is appended to the
90    current kill ring slot, otherwise prepended. */
91 static int
92 _rl_copy_to_kill_ring (text, append)
93      char *text;
94      int append;
95 {
96   char *old, *new;
97   int slot;
98
99   /* First, find the slot to work with. */
100   if (_rl_last_command_was_kill == 0)
101     {
102       /* Get a new slot.  */
103       if (rl_kill_ring == 0)
104         {
105           /* If we don't have any defined, then make one. */
106           rl_kill_ring = (char **)
107             xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
108           rl_kill_ring[slot = 0] = (char *)NULL;
109         }
110       else
111         {
112           /* We have to add a new slot on the end, unless we have
113              exceeded the max limit for remembering kills. */
114           slot = rl_kill_ring_length;
115           if (slot == rl_max_kills)
116             {
117               register int i;
118               free (rl_kill_ring[0]);
119               for (i = 0; i < slot; i++)
120                 rl_kill_ring[i] = rl_kill_ring[i + 1];
121             }
122           else
123             {
124               slot = rl_kill_ring_length += 1;
125               rl_kill_ring = (char **)xrealloc (rl_kill_ring, slot * sizeof (char *));
126             }
127           rl_kill_ring[--slot] = (char *)NULL;
128         }
129     }
130   else
131     slot = rl_kill_ring_length - 1;
132
133   /* If the last command was a kill, prepend or append. */
134   if (_rl_last_command_was_kill && rl_editing_mode != vi_mode)
135     {
136       old = rl_kill_ring[slot];
137       new = (char *)xmalloc (1 + strlen (old) + strlen (text));
138
139       if (append)
140         {
141           strcpy (new, old);
142           strcat (new, text);
143         }
144       else
145         {
146           strcpy (new, text);
147           strcat (new, old);
148         }
149       free (old);
150       free (text);
151       rl_kill_ring[slot] = new;
152     }
153   else
154     rl_kill_ring[slot] = text;
155
156   rl_kill_index = slot;
157   return 0;
158 }
159
160 /* The way to kill something.  This appends or prepends to the last
161    kill, if the last command was a kill command.  if FROM is less
162    than TO, then the text is appended, otherwise prepended.  If the
163    last command was not a kill command, then a new slot is made for
164    this kill. */
165 int
166 rl_kill_text (from, to)
167      int from, to;
168 {
169   char *text;
170
171   /* Is there anything to kill? */
172   if (from == to)
173     {
174       _rl_last_command_was_kill++;
175       return 0;
176     }
177
178   text = rl_copy_text (from, to);
179
180   /* Delete the copied text from the line. */
181   rl_delete_text (from, to);
182
183   _rl_copy_to_kill_ring (text, from < to);
184
185   _rl_last_command_was_kill++;
186   return 0;
187 }
188
189 /* Now REMEMBER!  In order to do prepending or appending correctly, kill
190    commands always make rl_point's original position be the FROM argument,
191    and rl_point's extent be the TO argument. */
192
193 /* **************************************************************** */
194 /*                                                                  */
195 /*                      Killing Commands                            */
196 /*                                                                  */
197 /* **************************************************************** */
198
199 /* Delete the word at point, saving the text in the kill ring. */
200 int
201 rl_kill_word (count, key)
202      int count, key;
203 {
204   int orig_point = rl_point;
205
206   if (count < 0)
207     return (rl_backward_kill_word (-count, key));
208   else
209     {
210       rl_forward_word (count, key);
211
212       if (rl_point != orig_point)
213         rl_kill_text (orig_point, rl_point);
214
215       rl_point = orig_point;
216     }
217   return 0;
218 }
219
220 /* Rubout the word before point, placing it on the kill ring. */
221 int
222 rl_backward_kill_word (count, ignore)
223      int count, ignore;
224 {
225   int orig_point = rl_point;
226
227   if (count < 0)
228     return (rl_kill_word (-count, ignore));
229   else
230     {
231       rl_backward_word (count, ignore);
232
233       if (rl_point != orig_point)
234         rl_kill_text (orig_point, rl_point);
235     }
236   return 0;
237 }
238
239 /* Kill from here to the end of the line.  If DIRECTION is negative, kill
240    back to the line start instead. */
241 int
242 rl_kill_line (direction, ignore)
243      int direction, ignore;
244 {
245   int orig_point = rl_point;
246
247   if (direction < 0)
248     return (rl_backward_kill_line (1, ignore));
249   else
250     {
251       rl_end_of_line (1, ignore);
252       if (orig_point != rl_point)
253         rl_kill_text (orig_point, rl_point);
254       rl_point = orig_point;
255     }
256   return 0;
257 }
258
259 /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
260    forwards to the line end instead. */
261 int
262 rl_backward_kill_line (direction, ignore)
263      int direction, ignore;
264 {
265   int orig_point = rl_point;
266
267   if (direction < 0)
268     return (rl_kill_line (1, ignore));
269   else
270     {
271       if (!rl_point)
272         rl_ding ();
273       else
274         {
275           rl_beg_of_line (1, ignore);
276           rl_kill_text (orig_point, rl_point);
277         }
278     }
279   return 0;
280 }
281
282 /* Kill the whole line, no matter where point is. */
283 int
284 rl_kill_full_line (count, ignore)
285      int count, ignore;
286 {
287   rl_begin_undo_group ();
288   rl_point = 0;
289   rl_kill_text (rl_point, rl_end);
290   rl_end_undo_group ();
291   return 0;
292 }
293
294 /* The next two functions mimic unix line editing behaviour, except they
295    save the deleted text on the kill ring.  This is safer than not saving
296    it, and since we have a ring, nobody should get screwed. */
297
298 /* This does what C-w does in Unix.  We can't prevent people from
299    using behaviour that they expect. */
300 int
301 rl_unix_word_rubout (count, key)
302      int count, key;
303 {
304   int orig_point;
305
306   if (rl_point == 0)
307     rl_ding ();
308   else
309     {
310       orig_point = rl_point;
311       if (count <= 0)
312         count = 1;
313
314       while (count--)
315         {
316           while (rl_point && whitespace (rl_line_buffer[rl_point - 1]))
317             rl_point--;
318
319           while (rl_point && (whitespace (rl_line_buffer[rl_point - 1]) == 0))
320             rl_point--;
321         }
322
323       rl_kill_text (orig_point, rl_point);
324     }
325   return 0;
326 }
327
328 /* Here is C-u doing what Unix does.  You don't *have* to use these
329    key-bindings.  We have a choice of killing the entire line, or
330    killing from where we are to the start of the line.  We choose the
331    latter, because if you are a Unix weenie, then you haven't backspaced
332    into the line at all, and if you aren't, then you know what you are
333    doing. */
334 int
335 rl_unix_line_discard (count, key)
336      int count, key;
337 {
338   if (rl_point == 0)
339     rl_ding ();
340   else
341     {
342       rl_kill_text (rl_point, 0);
343       rl_point = 0;
344     }
345   return 0;
346 }
347
348 /* Copy the text in the `region' to the kill ring.  If DELETE is non-zero,
349    delete the text from the line as well. */
350 static int
351 region_kill_internal (delete)
352      int delete;
353 {
354   char *text;
355
356   if (rl_mark == rl_point)
357     {
358       _rl_last_command_was_kill++;
359       return 0;
360     }
361
362   text = rl_copy_text (rl_point, rl_mark);
363   if (delete)
364     rl_delete_text (rl_point, rl_mark);
365   _rl_copy_to_kill_ring (text, rl_point < rl_mark);
366
367   _rl_last_command_was_kill++;
368   return 0;
369 }
370
371 /* Copy the text in the region to the kill ring. */
372 int
373 rl_copy_region_to_kill (count, ignore)
374      int count, ignore;
375 {
376   return (region_kill_internal (0));
377 }
378
379 /* Kill the text between the point and mark. */
380 int
381 rl_kill_region (count, ignore)
382      int count, ignore;
383 {
384   int r, npoint;
385
386   npoint = (rl_point < rl_mark) ? rl_point : rl_mark;
387   r = region_kill_internal (1);
388   _rl_fix_point (1);
389   rl_point = npoint;
390   return r;
391 }
392
393 /* Copy COUNT words to the kill ring.  DIR says which direction we look
394    to find the words. */
395 static int
396 _rl_copy_word_as_kill (count, dir)
397      int count, dir;
398 {
399   int om, op, r;
400
401   om = rl_mark;
402   op = rl_point;
403
404   if (dir > 0)
405     rl_forward_word (count, 0);
406   else
407     rl_backward_word (count, 0);
408
409   rl_mark = rl_point;
410
411   if (dir > 0)
412     rl_backward_word (count, 0);
413   else
414     rl_forward_word (count, 0);
415
416   r = region_kill_internal (0);
417
418   rl_mark = om;
419   rl_point = op;
420
421   return r;
422 }
423
424 int
425 rl_copy_forward_word (count, key)
426      int count, key;
427 {
428   if (count < 0)
429     return (rl_copy_backward_word (-count, key));
430
431   return (_rl_copy_word_as_kill (count, 1));
432 }
433
434 int
435 rl_copy_backward_word (count, key)
436      int count, key;
437 {
438   if (count < 0)
439     return (rl_copy_forward_word (-count, key));
440
441   return (_rl_copy_word_as_kill (count, -1));
442 }
443   
444 /* Yank back the last killed text.  This ignores arguments. */
445 int
446 rl_yank (count, ignore)
447      int count, ignore;
448 {
449   if (rl_kill_ring == 0)
450     {
451       _rl_abort_internal ();
452       return -1;
453     }
454
455   _rl_set_mark_at_pos (rl_point);
456   rl_insert_text (rl_kill_ring[rl_kill_index]);
457   return 0;
458 }
459
460 /* If the last command was yank, or yank_pop, and the text just
461    before point is identical to the current kill item, then
462    delete that text from the line, rotate the index down, and
463    yank back some other text. */
464 int
465 rl_yank_pop (count, key)
466      int count, key;
467 {
468   int l, n;
469
470   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
471       !rl_kill_ring)
472     {
473       _rl_abort_internal ();
474       return -1;
475     }
476
477   l = strlen (rl_kill_ring[rl_kill_index]);
478   n = rl_point - l;
479   if (n >= 0 && STREQN (rl_line_buffer + n, rl_kill_ring[rl_kill_index], l))
480     {
481       rl_delete_text (n, rl_point);
482       rl_point = n;
483       rl_kill_index--;
484       if (rl_kill_index < 0)
485         rl_kill_index = rl_kill_ring_length - 1;
486       rl_yank (1, 0);
487       return 0;
488     }
489   else
490     {
491       _rl_abort_internal ();
492       return -1;
493     }
494 }
495
496 /* Yank the COUNTh argument from the previous history line, skipping
497    HISTORY_SKIP lines before looking for the `previous line'. */
498 static int
499 rl_yank_nth_arg_internal (count, ignore, history_skip)
500      int count, ignore, history_skip;
501 {
502   register HIST_ENTRY *entry;
503   char *arg;
504   int i, pos;
505
506   pos = where_history ();
507
508   if (history_skip)
509     {
510       for (i = 0; i < history_skip; i++)
511         entry = previous_history ();
512     }
513
514   entry = previous_history ();
515
516   history_set_pos (pos);
517
518   if (entry == 0)
519     {
520       rl_ding ();
521       return -1;
522     }
523
524   arg = history_arg_extract (count, count, entry->line);
525   if (!arg || !*arg)
526     {
527       rl_ding ();
528       return -1;
529     }
530
531   rl_begin_undo_group ();
532
533 #if defined (VI_MODE)
534   /* Vi mode always inserts a space before yanking the argument, and it
535      inserts it right *after* rl_point. */
536   if (rl_editing_mode == vi_mode)
537     {
538       rl_vi_append_mode (1, ignore);
539       rl_insert_text (" ");
540     }
541 #endif /* VI_MODE */
542
543   rl_insert_text (arg);
544   free (arg);
545
546   rl_end_undo_group ();
547   return 0;
548 }
549
550 /* Yank the COUNTth argument from the previous history line. */
551 int
552 rl_yank_nth_arg (count, ignore)
553      int count, ignore;
554 {
555   return (rl_yank_nth_arg_internal (count, ignore, 0));
556 }
557
558 /* Yank the last argument from the previous history line.  This `knows'
559    how rl_yank_nth_arg treats a count of `$'.  With an argument, this
560    behaves the same as rl_yank_nth_arg. */
561 int
562 rl_yank_last_arg (count, key)
563      int count, key;
564 {
565   static int history_skip = 0;
566   static int explicit_arg_p = 0;
567   static int count_passed = 1;
568   static int direction = 1;
569   static int undo_needed = 0;
570   int retval;
571
572   if (rl_last_func != rl_yank_last_arg)
573     {
574       history_skip = 0;
575       explicit_arg_p = rl_explicit_arg;
576       count_passed = count;
577       direction = 1;
578     }
579   else
580     {
581       if (undo_needed)
582         rl_do_undo ();
583       if (count < 1)
584         direction = -direction;
585       history_skip += direction;
586       if (history_skip < 0)
587         history_skip = 0;
588     }
589  
590   if (explicit_arg_p)
591     retval = rl_yank_nth_arg_internal (count_passed, key, history_skip);
592   else
593     retval = rl_yank_nth_arg_internal ('$', key, history_skip);
594
595   undo_needed = retval == 0;
596   return retval;
597 }
598
599 /* A special paste command for users of Cygnus's cygwin32. */
600 #if defined (__CYGWIN__)
601 #include <windows.h>
602
603 int
604 rl_paste_from_clipboard (count, key)
605      int count, key;
606 {
607   char *data, *ptr;
608   int len;
609
610   if (OpenClipboard (NULL) == 0)
611     return (0);
612
613   data = (char *)GetClipboardData (CF_TEXT);
614   if (data)
615     {
616       ptr = strchr (data, '\r');
617       if (ptr)
618         {
619           len = ptr - data;
620           ptr = (char *)xmalloc (len + 1);
621           ptr[len] = '\0';
622           strncpy (ptr, data, len);
623         }
624       else
625         ptr = data;
626       rl_insert_text (ptr);
627       if (ptr != data)
628         free (ptr);
629       CloseClipboard ();
630     }
631   return (0);
632 }
633 #endif /* __CYGWIN__ */