1 /* kill.c -- kill ring management. */
3 /* Copyright (C) 1994-2017 Free Software Foundation, Inc.
5 This file is part of the GNU Readline Library (Readline), a library
6 for reading lines of text with interactive input and history editing.
8 Readline is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 Readline is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Readline. If not, see <http://www.gnu.org/licenses/>.
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 (int num
)
86 /* Add TEXT to the kill ring, allocating a new kill ring slot as necessary.
87 This uses TEXT directly, so the caller must not free it. If APPEND is
88 non-zero, and the last command was a kill, the text is appended to the
89 current kill ring slot, otherwise prepended. */
91 _rl_copy_to_kill_ring (char *text
, int append
)
96 /* First, find the slot to work with. */
97 if (_rl_last_command_was_kill
== 0 || rl_kill_ring
== 0)
100 if (rl_kill_ring
== 0)
102 /* If we don't have any defined, then make one. */
103 rl_kill_ring
= (char **)
104 xmalloc (((rl_kill_ring_length
= 1) + 1) * sizeof (char *));
105 rl_kill_ring
[slot
= 0] = (char *)NULL
;
109 /* We have to add a new slot on the end, unless we have
110 exceeded the max limit for remembering kills. */
111 slot
= rl_kill_ring_length
;
112 if (slot
== rl_max_kills
)
115 xfree (rl_kill_ring
[0]);
116 for (i
= 0; i
< slot
; i
++)
117 rl_kill_ring
[i
] = rl_kill_ring
[i
+ 1];
121 slot
= rl_kill_ring_length
+= 1;
122 rl_kill_ring
= (char **)xrealloc (rl_kill_ring
, (slot
+ 1) * sizeof (char *));
124 rl_kill_ring
[--slot
] = (char *)NULL
;
128 slot
= rl_kill_ring_length
- 1;
130 /* If the last command was a kill, prepend or append. */
131 if (_rl_last_command_was_kill
&& rl_kill_ring
[slot
] && rl_editing_mode
!= vi_mode
)
133 old
= rl_kill_ring
[slot
];
134 new = (char *)xmalloc (1 + strlen (old
) + strlen (text
));
148 rl_kill_ring
[slot
] = new;
151 rl_kill_ring
[slot
] = text
;
153 rl_kill_index
= slot
;
157 /* The way to kill something. This appends or prepends to the last
158 kill, if the last command was a kill command. if FROM is less
159 than TO, then the text is appended, otherwise prepended. If the
160 last command was not a kill command, then a new slot is made for
163 rl_kill_text (int from
, int to
)
167 /* Is there anything to kill? */
170 _rl_last_command_was_kill
++;
174 text
= rl_copy_text (from
, to
);
176 /* Delete the copied text from the line. */
177 rl_delete_text (from
, to
);
179 _rl_copy_to_kill_ring (text
, from
< to
);
181 _rl_last_command_was_kill
++;
185 /* Now REMEMBER! In order to do prepending or appending correctly, kill
186 commands always make rl_point's original position be the FROM argument,
187 and rl_point's extent be the TO argument. */
189 /* **************************************************************** */
191 /* Killing Commands */
193 /* **************************************************************** */
195 /* Delete the word at point, saving the text in the kill ring. */
197 rl_kill_word (int count
, int key
)
202 return (rl_backward_kill_word (-count
, key
));
205 orig_point
= rl_point
;
206 rl_forward_word (count
, key
);
208 if (rl_point
!= orig_point
)
209 rl_kill_text (orig_point
, rl_point
);
211 rl_point
= orig_point
;
212 if (rl_editing_mode
== emacs_mode
)
218 /* Rubout the word before point, placing it on the kill ring. */
220 rl_backward_kill_word (int count
, int key
)
225 return (rl_kill_word (-count
, key
));
228 orig_point
= rl_point
;
229 rl_backward_word (count
, key
);
231 if (rl_point
!= orig_point
)
232 rl_kill_text (orig_point
, rl_point
);
234 if (rl_editing_mode
== emacs_mode
)
240 /* Kill from here to the end of the line. If DIRECTION is negative, kill
241 back to the line start instead. */
243 rl_kill_line (int direction
, int key
)
248 return (rl_backward_kill_line (1, key
));
251 orig_point
= rl_point
;
252 rl_end_of_line (1, key
);
253 if (orig_point
!= rl_point
)
254 rl_kill_text (orig_point
, rl_point
);
255 rl_point
= orig_point
;
256 if (rl_editing_mode
== emacs_mode
)
262 /* Kill backwards to the start of the line. If DIRECTION is negative, kill
263 forwards to the line end instead. */
265 rl_backward_kill_line (int direction
, int key
)
270 return (rl_kill_line (1, key
));
277 orig_point
= rl_point
;
278 rl_beg_of_line (1, key
);
279 if (rl_point
!= orig_point
)
280 rl_kill_text (orig_point
, rl_point
);
281 if (rl_editing_mode
== emacs_mode
)
288 /* Kill the whole line, no matter where point is. */
290 rl_kill_full_line (int count
, int key
)
292 rl_begin_undo_group ();
294 rl_kill_text (rl_point
, rl_end
);
296 rl_end_undo_group ();
300 /* The next two functions mimic unix line editing behaviour, except they
301 save the deleted text on the kill ring. This is safer than not saving
302 it, and since we have a ring, nobody should get screwed. */
304 /* This does what C-w does in Unix. We can't prevent people from
305 using behaviour that they expect. */
307 rl_unix_word_rubout (int count
, int key
)
315 orig_point
= rl_point
;
321 while (rl_point
&& whitespace (rl_line_buffer
[rl_point
- 1]))
324 while (rl_point
&& (whitespace (rl_line_buffer
[rl_point
- 1]) == 0))
325 rl_point
--; /* XXX - multibyte? */
328 rl_kill_text (orig_point
, rl_point
);
329 if (rl_editing_mode
== emacs_mode
)
336 /* This deletes one filename component in a Unix pathname. That is, it
337 deletes backward to directory separator (`/') or whitespace. */
339 rl_unix_filename_rubout (int count
, int key
)
347 orig_point
= rl_point
;
353 c
= rl_line_buffer
[rl_point
- 1];
354 while (rl_point
&& (whitespace (c
) || c
== '/'))
357 c
= rl_line_buffer
[rl_point
- 1];
360 while (rl_point
&& (whitespace (c
) == 0) && c
!= '/')
362 rl_point
--; /* XXX - multibyte? */
363 c
= rl_line_buffer
[rl_point
- 1];
367 rl_kill_text (orig_point
, rl_point
);
368 if (rl_editing_mode
== emacs_mode
)
375 /* Here is C-u doing what Unix does. You don't *have* to use these
376 key-bindings. We have a choice of killing the entire line, or
377 killing from where we are to the start of the line. We choose the
378 latter, because if you are a Unix weenie, then you haven't backspaced
379 into the line at all, and if you aren't, then you know what you are
382 rl_unix_line_discard (int count
, int key
)
388 rl_kill_text (rl_point
, 0);
390 if (rl_editing_mode
== emacs_mode
)
396 /* Copy the text in the `region' to the kill ring. If DELETE is non-zero,
397 delete the text from the line as well. */
399 region_kill_internal (int delete)
403 if (rl_mark
!= rl_point
)
405 text
= rl_copy_text (rl_point
, rl_mark
);
407 rl_delete_text (rl_point
, rl_mark
);
408 _rl_copy_to_kill_ring (text
, rl_point
< rl_mark
);
411 _rl_last_command_was_kill
++;
415 /* Copy the text in the region to the kill ring. */
417 rl_copy_region_to_kill (int count
, int key
)
419 return (region_kill_internal (0));
422 /* Kill the text between the point and mark. */
424 rl_kill_region (int count
, int key
)
428 npoint
= (rl_point
< rl_mark
) ? rl_point
: rl_mark
;
429 r
= region_kill_internal (1);
435 /* Copy COUNT words to the kill ring. DIR says which direction we look
436 to find the words. */
438 _rl_copy_word_as_kill (int count
, int dir
)
446 rl_forward_word (count
, 0);
448 rl_backward_word (count
, 0);
453 rl_backward_word (count
, 0);
455 rl_forward_word (count
, 0);
457 r
= region_kill_internal (0);
466 rl_copy_forward_word (int count
, int key
)
469 return (rl_copy_backward_word (-count
, key
));
471 return (_rl_copy_word_as_kill (count
, 1));
475 rl_copy_backward_word (int count
, int key
)
478 return (rl_copy_forward_word (-count
, key
));
480 return (_rl_copy_word_as_kill (count
, -1));
483 /* Yank back the last killed text. This ignores arguments. */
485 rl_yank (int count
, int key
)
487 if (rl_kill_ring
== 0)
489 _rl_abort_internal ();
493 _rl_set_mark_at_pos (rl_point
);
494 rl_insert_text (rl_kill_ring
[rl_kill_index
]);
498 /* If the last command was yank, or yank_pop, and the text just
499 before point is identical to the current kill item, then
500 delete that text from the line, rotate the index down, and
501 yank back some other text. */
503 rl_yank_pop (int count
, int key
)
507 if (((rl_last_func
!= rl_yank_pop
) && (rl_last_func
!= rl_yank
)) ||
510 _rl_abort_internal ();
514 l
= strlen (rl_kill_ring
[rl_kill_index
]);
516 if (n
>= 0 && STREQN (rl_line_buffer
+ n
, rl_kill_ring
[rl_kill_index
], l
))
518 rl_delete_text (n
, rl_point
);
521 if (rl_kill_index
< 0)
522 rl_kill_index
= rl_kill_ring_length
- 1;
528 _rl_abort_internal ();
533 #if defined (VI_MODE)
535 rl_vi_yank_pop (int count
, int key
)
539 if (((rl_last_func
!= rl_vi_yank_pop
) && (rl_last_func
!= rl_vi_put
)) ||
542 _rl_abort_internal ();
546 l
= strlen (rl_kill_ring
[rl_kill_index
]);
548 if (n
>= 0 && STREQN (rl_line_buffer
+ n
, rl_kill_ring
[rl_kill_index
], l
))
550 rl_delete_text (n
, rl_point
);
553 if (rl_kill_index
< 0)
554 rl_kill_index
= rl_kill_ring_length
- 1;
560 _rl_abort_internal ();
566 /* Yank the COUNTh argument from the previous history line, skipping
567 HISTORY_SKIP lines before looking for the `previous line'. */
569 rl_yank_nth_arg_internal (int count
, int key
, int history_skip
)
571 register HIST_ENTRY
*entry
;
575 pos
= where_history ();
579 for (i
= 0; i
< history_skip
; i
++)
580 entry
= previous_history ();
583 entry
= previous_history ();
585 history_set_pos (pos
);
593 arg
= history_arg_extract (count
, count
, entry
->line
);
601 rl_begin_undo_group ();
603 _rl_set_mark_at_pos (rl_point
);
605 #if defined (VI_MODE)
606 /* Vi mode always inserts a space before yanking the argument, and it
607 inserts it right *after* rl_point. */
608 if (rl_editing_mode
== vi_mode
)
610 rl_vi_append_mode (1, key
);
611 rl_insert_text (" ");
615 rl_insert_text (arg
);
618 rl_end_undo_group ();
622 /* Yank the COUNTth argument from the previous history line. */
624 rl_yank_nth_arg (int count
, int key
)
626 return (rl_yank_nth_arg_internal (count
, key
, 0));
629 /* Yank the last argument from the previous history line. This `knows'
630 how rl_yank_nth_arg treats a count of `$'. With an argument, this
631 behaves the same as rl_yank_nth_arg. */
633 rl_yank_last_arg (int count
, int key
)
635 static int history_skip
= 0;
636 static int explicit_arg_p
= 0;
637 static int count_passed
= 1;
638 static int direction
= 1;
639 static int undo_needed
= 0;
642 if (rl_last_func
!= rl_yank_last_arg
)
645 explicit_arg_p
= rl_explicit_arg
;
646 count_passed
= count
;
653 if (count
< 0) /* XXX - was < 1 */
654 direction
= -direction
;
655 history_skip
+= direction
;
656 if (history_skip
< 0)
661 retval
= rl_yank_nth_arg_internal (count_passed
, key
, history_skip
);
663 retval
= rl_yank_nth_arg_internal ('$', key
, history_skip
);
665 undo_needed
= retval
== 0;
669 /* Having read the special escape sequence denoting the beginning of a
670 `bracketed paste' sequence, read the rest of the pasted input until the
671 closing sequence and insert the pasted text as a single unit without
674 _rl_bracketed_text (size_t *lenp
)
681 buf
= xmalloc (cap
= 64);
684 RL_SETSTATE (RL_STATE_MOREINPUT
);
685 while ((c
= rl_read_key ()) >= 0)
687 if (RL_ISSTATE (RL_STATE_MACRODEF
))
688 _rl_add_macro_char (c
);
690 if (c
== '\r') /* XXX */
694 buf
= xrealloc (buf
, cap
*= 2);
697 if (len
>= BRACK_PASTE_SLEN
&& c
== BRACK_PASTE_LAST
&&
698 STREQN (buf
+ len
- BRACK_PASTE_SLEN
, BRACK_PASTE_SUFF
, BRACK_PASTE_SLEN
))
700 len
-= BRACK_PASTE_SLEN
;
704 RL_UNSETSTATE (RL_STATE_MOREINPUT
);
709 buf
= xrealloc (buf
, cap
+ 1);
719 rl_bracketed_paste_begin (int count
, int key
)
725 buf
= _rl_bracketed_text (&len
);
726 retval
= rl_insert_text (buf
) == len
? 0 : 1;
732 /* A special paste command for Windows users. */
737 rl_paste_from_clipboard (int count
, int key
)
742 if (OpenClipboard (NULL
) == 0)
745 data
= (char *)GetClipboardData (CF_TEXT
);
748 ptr
= strchr (data
, '\r');
752 ptr
= (char *)xmalloc (len
+ 1);
754 strncpy (ptr
, data
, len
);
758 _rl_set_mark_at_pos (rl_point
);
759 rl_insert_text (ptr
);