1 /* kill.c -- kill ring management. */
3 /* Copyright (C) 1994 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library, a library for
6 reading lines of text with interactive input and history editing.
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.
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.
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
24 #if defined (HAVE_CONFIG_H)
28 #include <sys/types.h>
30 #if defined (HAVE_UNISTD_H)
31 # include <unistd.h> /* for _POSIX_VERSION */
32 #endif /* HAVE_UNISTD_H */
34 #if defined (HAVE_STDLIB_H)
37 # include "ansi_stdlib.h"
38 #endif /* HAVE_STDLIB_H */
42 /* System-specific feature definitions and include files. */
45 /* Some standard library routines. */
49 #include "rlprivate.h"
52 /* **************************************************************** */
54 /* Killing Mechanism */
56 /* **************************************************************** */
58 /* What we assume for a max number of kills. */
59 #define DEFAULT_MAX_KILLS 10
61 /* The real variable to look at to find out when to flush kills. */
62 static int rl_max_kills
= DEFAULT_MAX_KILLS
;
64 /* Where to store killed text. */
65 static char **rl_kill_ring
= (char **)NULL
;
67 /* Where we are in the kill ring. */
68 static int rl_kill_index
;
70 /* How many slots we have in the kill ring. */
71 static int rl_kill_ring_length
;
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));
78 /* How to say that you only want to save a certain amount
81 rl_set_retained_kills (num
)
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. */
92 _rl_copy_to_kill_ring (text
, append
)
99 /* First, find the slot to work with. */
100 if (_rl_last_command_was_kill
== 0)
102 /* Get a new slot. */
103 if (rl_kill_ring
== 0)
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
;
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
)
118 free (rl_kill_ring
[0]);
119 for (i
= 0; i
< slot
; i
++)
120 rl_kill_ring
[i
] = rl_kill_ring
[i
+ 1];
124 slot
= rl_kill_ring_length
+= 1;
125 rl_kill_ring
= (char **)xrealloc (rl_kill_ring
, slot
* sizeof (char *));
127 rl_kill_ring
[--slot
] = (char *)NULL
;
131 slot
= rl_kill_ring_length
- 1;
133 /* If the last command was a kill, prepend or append. */
134 if (_rl_last_command_was_kill
&& rl_editing_mode
!= vi_mode
)
136 old
= rl_kill_ring
[slot
];
137 new = (char *)xmalloc (1 + strlen (old
) + strlen (text
));
151 rl_kill_ring
[slot
] = new;
154 rl_kill_ring
[slot
] = text
;
156 rl_kill_index
= slot
;
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
166 rl_kill_text (from
, to
)
171 /* Is there anything to kill? */
174 _rl_last_command_was_kill
++;
178 text
= rl_copy_text (from
, to
);
180 /* Delete the copied text from the line. */
181 rl_delete_text (from
, to
);
183 _rl_copy_to_kill_ring (text
, from
< to
);
185 _rl_last_command_was_kill
++;
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. */
193 /* **************************************************************** */
195 /* Killing Commands */
197 /* **************************************************************** */
199 /* Delete the word at point, saving the text in the kill ring. */
201 rl_kill_word (count
, key
)
207 return (rl_backward_kill_word (-count
, key
));
210 orig_point
= rl_point
;
211 rl_forward_word (count
, key
);
213 if (rl_point
!= orig_point
)
214 rl_kill_text (orig_point
, rl_point
);
216 rl_point
= orig_point
;
217 if (rl_editing_mode
== emacs_mode
)
223 /* Rubout the word before point, placing it on the kill ring. */
225 rl_backward_kill_word (count
, ignore
)
231 return (rl_kill_word (-count
, ignore
));
234 orig_point
= rl_point
;
235 rl_backward_word (count
, ignore
);
237 if (rl_point
!= orig_point
)
238 rl_kill_text (orig_point
, rl_point
);
240 if (rl_editing_mode
== emacs_mode
)
246 /* Kill from here to the end of the line. If DIRECTION is negative, kill
247 back to the line start instead. */
249 rl_kill_line (direction
, ignore
)
250 int direction
, ignore
;
255 return (rl_backward_kill_line (1, ignore
));
258 orig_point
= rl_point
;
259 rl_end_of_line (1, ignore
);
260 if (orig_point
!= rl_point
)
261 rl_kill_text (orig_point
, rl_point
);
262 rl_point
= orig_point
;
263 if (rl_editing_mode
== emacs_mode
)
269 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
270 forwards to the line end instead. */
272 rl_backward_kill_line (direction
, ignore
)
273 int direction
, ignore
;
278 return (rl_kill_line (1, ignore
));
285 orig_point
= rl_point
;
286 rl_beg_of_line (1, ignore
);
287 if (rl_point
!= orig_point
)
288 rl_kill_text (orig_point
, rl_point
);
289 if (rl_editing_mode
== emacs_mode
)
296 /* Kill the whole line, no matter where point is. */
298 rl_kill_full_line (count
, ignore
)
301 rl_begin_undo_group ();
303 rl_kill_text (rl_point
, rl_end
);
305 rl_end_undo_group ();
309 /* The next two functions mimic unix line editing behaviour, except they
310 save the deleted text on the kill ring. This is safer than not saving
311 it, and since we have a ring, nobody should get screwed. */
313 /* This does what C-w does in Unix. We can't prevent people from
314 using behaviour that they expect. */
316 rl_unix_word_rubout (count
, key
)
325 orig_point
= rl_point
;
331 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
334 while (rl_point
&& (whitespace (rl_line_buffer
[rl_point
- 1]) == 0))
338 rl_kill_text (orig_point
, rl_point
);
339 if (rl_editing_mode
== emacs_mode
)
345 /* Here is C-u doing what Unix does. You don't *have* to use these
346 key-bindings. We have a choice of killing the entire line, or
347 killing from where we are to the start of the line. We choose the
348 latter, because if you are a Unix weenie, then you haven't backspaced
349 into the line at all, and if you aren't, then you know what you are
352 rl_unix_line_discard (count
, key
)
359 rl_kill_text (rl_point
, 0);
361 if (rl_editing_mode
== emacs_mode
)
367 /* Copy the text in the `region' to the kill ring. If DELETE is non-zero,
368 delete the text from the line as well. */
370 region_kill_internal (delete)
375 if (rl_mark
!= rl_point
)
377 text
= rl_copy_text (rl_point
, rl_mark
);
379 rl_delete_text (rl_point
, rl_mark
);
380 _rl_copy_to_kill_ring (text
, rl_point
< rl_mark
);
383 _rl_last_command_was_kill
++;
387 /* Copy the text in the region to the kill ring. */
389 rl_copy_region_to_kill (count
, ignore
)
392 return (region_kill_internal (0));
395 /* Kill the text between the point and mark. */
397 rl_kill_region (count
, ignore
)
402 npoint
= (rl_point
< rl_mark
) ? rl_point
: rl_mark
;
403 r
= region_kill_internal (1);
409 /* Copy COUNT words to the kill ring. DIR says which direction we look
410 to find the words. */
412 _rl_copy_word_as_kill (count
, dir
)
421 rl_forward_word (count
, 0);
423 rl_backward_word (count
, 0);
428 rl_backward_word (count
, 0);
430 rl_forward_word (count
, 0);
432 r
= region_kill_internal (0);
441 rl_copy_forward_word (count
, key
)
445 return (rl_copy_backward_word (-count
, key
));
447 return (_rl_copy_word_as_kill (count
, 1));
451 rl_copy_backward_word (count
, key
)
455 return (rl_copy_forward_word (-count
, key
));
457 return (_rl_copy_word_as_kill (count
, -1));
460 /* Yank back the last killed text. This ignores arguments. */
462 rl_yank (count
, ignore
)
465 if (rl_kill_ring
== 0)
467 _rl_abort_internal ();
471 _rl_set_mark_at_pos (rl_point
);
472 rl_insert_text (rl_kill_ring
[rl_kill_index
]);
476 /* If the last command was yank, or yank_pop, and the text just
477 before point is identical to the current kill item, then
478 delete that text from the line, rotate the index down, and
479 yank back some other text. */
481 rl_yank_pop (count
, key
)
486 if (((rl_last_func
!= rl_yank_pop
) && (rl_last_func
!= rl_yank
)) ||
489 _rl_abort_internal ();
493 l
= strlen (rl_kill_ring
[rl_kill_index
]);
495 if (n
>= 0 && STREQN (rl_line_buffer
+ n
, rl_kill_ring
[rl_kill_index
], l
))
497 rl_delete_text (n
, rl_point
);
500 if (rl_kill_index
< 0)
501 rl_kill_index
= rl_kill_ring_length
- 1;
507 _rl_abort_internal ();
512 /* Yank the COUNTh argument from the previous history line, skipping
513 HISTORY_SKIP lines before looking for the `previous line'. */
515 rl_yank_nth_arg_internal (count
, ignore
, history_skip
)
516 int count
, ignore
, history_skip
;
518 register HIST_ENTRY
*entry
;
522 pos
= where_history ();
526 for (i
= 0; i
< history_skip
; i
++)
527 entry
= previous_history ();
530 entry
= previous_history ();
532 history_set_pos (pos
);
540 arg
= history_arg_extract (count
, count
, entry
->line
);
547 rl_begin_undo_group ();
549 _rl_set_mark_at_pos (rl_point
);
551 #if defined (VI_MODE)
552 /* Vi mode always inserts a space before yanking the argument, and it
553 inserts it right *after* rl_point. */
554 if (rl_editing_mode
== vi_mode
)
556 rl_vi_append_mode (1, ignore
);
557 rl_insert_text (" ");
561 rl_insert_text (arg
);
564 rl_end_undo_group ();
568 /* Yank the COUNTth argument from the previous history line. */
570 rl_yank_nth_arg (count
, ignore
)
573 return (rl_yank_nth_arg_internal (count
, ignore
, 0));
576 /* Yank the last argument from the previous history line. This `knows'
577 how rl_yank_nth_arg treats a count of `$'. With an argument, this
578 behaves the same as rl_yank_nth_arg. */
580 rl_yank_last_arg (count
, key
)
583 static int history_skip
= 0;
584 static int explicit_arg_p
= 0;
585 static int count_passed
= 1;
586 static int direction
= 1;
587 static int undo_needed
= 0;
590 if (rl_last_func
!= rl_yank_last_arg
)
593 explicit_arg_p
= rl_explicit_arg
;
594 count_passed
= count
;
602 direction
= -direction
;
603 history_skip
+= direction
;
604 if (history_skip
< 0)
609 retval
= rl_yank_nth_arg_internal (count_passed
, key
, history_skip
);
611 retval
= rl_yank_nth_arg_internal ('$', key
, history_skip
);
613 undo_needed
= retval
== 0;
617 /* A special paste command for users of Cygnus's cygwin32. */
618 #if defined (__CYGWIN__)
622 rl_paste_from_clipboard (count
, key
)
628 if (OpenClipboard (NULL
) == 0)
631 data
= (char *)GetClipboardData (CF_TEXT
);
634 ptr
= strchr (data
, '\r');
638 ptr
= (char *)xmalloc (len
+ 1);
640 strncpy (ptr
, data
, len
);
644 _rl_set_mark_at_pos (rl_point
);
645 rl_insert_text (ptr
);
652 #endif /* __CYGWIN__ */