Build Fix -- compatibility issue with newer autoconf
[sxemacs] / info / sxemacs / killing.texi
1 @iftex
2 @chapter Killing and Moving Text
3
4   @dfn{Killing} means erasing text and copying it into the @dfn{kill ring},
5 from which it can be retrieved by @dfn{yanking} it.  Some other systems
6 that have recently become popular use the terms ``cutting'' and ``pasting''
7 for these operations.
8
9   The most common way of moving or copying text with Emacs is to kill it
10 and later yank it in one or more places.  This is safe because all the
11 text killed recently is stored in the kill ring, and it is versatile,
12 because you can use the same commands for killing syntactic units and
13 for moving those units.  There are other ways of copying text for
14 special purposes.
15
16   Emacs has only one kill ring, so you can kill text in one buffer and yank
17 it in another buffer. If you are using SXEmacs under X, you can
18 also use the X selection mechanism to copy text from one buffer to
19 another, or between applications. @xref{Using X Selections}.
20
21 @end iftex
22
23 @node Killing, Yanking, Additional Mouse Operations, Top
24 @section Deletion and Killing
25 @findex delete-char
26 @findex delete-backward-char
27
28 @cindex killing
29 @cindex cutting
30 @cindex deletion
31 @kindex C-d
32 @kindex DEL
33   Most commands that erase text from the buffer save it. You can get
34 the text back if you change your mind, or you can move or copy it to
35 other parts of the buffer.  Commands which erase text and save it in the
36 kill ring are known as @dfn{kill} commands.  Some other commands erase
37 text but do not save it; they are known as @dfn{delete} commands.  (This
38 distinction is made only for erasing text in the buffer.)
39
40 The commands' names and individual descriptions use the words
41 @samp{kill} and @samp{delete} to indicate what they do.  If you perform
42 a kill or delete command by mistake, use the @kbd{C-x u} (@code{undo})
43 command to undo it (@pxref{Undo}). The delete commands include @kbd{C-d}
44 (@code{delete-char}) and @key{DEL} (@code{delete-backward-char}), which
45 delete only one character at a time, and those commands that delete only
46 spaces or newlines.  Commands that can destroy significant amounts of
47 nontrivial data usually kill.@refill
48
49 @subsection Deletion
50
51 @table @kbd
52 @item C-d
53 Delete next character (@code{delete-char}).
54 @item @key{DEL}
55 Delete previous character (@code{delete-backward-char}).
56 @item M-\
57 Delete spaces and tabs around point (@code{delete-horizontal-space}).
58 @item M-@key{SPC}
59 Delete spaces and tabs around point, leaving one space
60 (@code{just-one-space}).
61 @item C-x C-o
62 Delete blank lines around the current line (@code{delete-blank-lines}).
63 @item M-^
64 Join two lines by deleting the intervening newline, and any indentation
65 following it (@code{delete-indentation}).
66 @end table
67
68   The most basic delete commands are @kbd{C-d} (@code{delete-char}) and
69 @key{DEL} (@code{delete-backward-char}).  @kbd{C-d} deletes the
70 character after point, the one the cursor is ``on top of''.  Point
71 doesn't move.  @key{DEL} deletes the character before the cursor, and
72 moves point back.  You can delete newlines like any other characters in
73 the buffer; deleting a newline joins two lines.  Actually, @kbd{C-d} and
74 @key{DEL} aren't always delete commands; if you give them an argument,
75 they kill instead, since they can erase more than one character this
76 way.
77
78 @kindex M-\
79 @findex delete-horizontal-space
80 @kindex M-SPC
81 @findex just-one-space
82 @kindex C-x C-o
83 @findex delete-blank-lines
84 @kindex M-^
85 @findex delete-indentation
86   The other delete commands delete only formatting characters: spaces,
87 tabs and newlines.  @kbd{M-\} (@code{delete-horizontal-space}) deletes
88 all spaces and tab characters before and after point.
89 @kbd{M-@key{SPC}} (@code{just-one-space}) does the same but leaves a
90 single space after point, regardless of the number of spaces that
91 existed previously (even zero).
92
93   @kbd{C-x C-o} (@code{delete-blank-lines}) deletes all blank lines after
94 the current line. If the current line is blank, it deletes all blank lines
95 preceding the current line as well as leaving one blank line, the current
96 line.  @kbd{M-^} (@code{delete-indentation}) joins the current line and
97 the previous line, or, if given an argument, joins the current line and
98 the next line by deleting a newline and all surrounding spaces, possibly
99 leaving a single space.  @xref{Indentation,M-^}.
100
101 @subsection Killing by Lines
102
103 @table @kbd
104 @item C-k
105 Kill rest of line or one or more lines (@code{kill-line}).
106 @end table
107
108 @kindex C-k
109 @findex kill-line
110   The simplest kill command is @kbd{C-k}.  If given at the beginning of
111 a line, it kills all the text on the line, leaving the line blank.  If
112 given on a blank line, the blank line disappears.  As a consequence, a
113 line disappears completely if you go to the front of a non-blank line
114 and type @kbd{C-k} twice.
115
116   More generally, @kbd{C-k} kills from point up to the end of the line,
117 unless it is at the end of a line.  In that case, it kills the newline
118 following the line, thus merging the next line into the current one.
119 Emacs ignores invisible spaces and tabs at the end of the line when deciding
120 which case applies: if point appears to be at the end of the line, you
121 can be sure the newline will be killed.
122
123   If you give @kbd{C-k} a positive argument, it kills that many lines
124 and the newlines that follow them (however, text on the current line
125 before point is not killed).  With a negative argument, @kbd{C-k} kills
126 back to a number of line beginnings.  An argument of @minus{}2 means
127 kill back to the second line beginning.  If point is at the beginning of
128 a line, that line beginning doesn't count, so @kbd{C-u - 2 C-k} with
129 point at the front of a line kills the two previous lines.
130
131   @kbd{C-k} with an argument of zero kills all the text before point on the
132 current line.
133
134 @subsection Other Kill Commands
135 @findex kill-region
136 @findex kill-word
137 @findex backward-kill-word
138 @findex kill-sexp
139 @findex kill-sentence
140 @findex backward-kill-sentence
141 @kindex M-d
142 @kindex M-DEL
143 @kindex C-M-k
144 @kindex C-x DEL
145 @kindex M-k
146 @kindex C-w
147
148 @c DoubleWideCommands
149 @table @kbd
150 @item C-w
151 Kill region (from point to the mark) (@code{kill-region}).
152 @xref{Words}.
153 @item M-d
154 Kill word (@code{kill-word}).
155 @item M-@key{DEL}
156 Kill word backwards (@code{backward-kill-word}).
157 @item C-x @key{DEL}
158 Kill back to beginning of sentence (@code{backward-kill-sentence}).
159 @xref{Sentences}.
160 @item M-k
161 Kill to end of sentence (@code{kill-sentence}).
162 @item C-M-k
163 Kill sexp (@code{kill-sexp}).  @xref{Lists}.
164 @item M-z @var{char}
165 Kill up to next occurrence of @var{char} (@code{zap-to-char}).
166 @end table
167
168    @kbd{C-w} (@code{kill-region}) is a very general kill command; it
169 kills everything between point and the mark. You can use this command to
170 kill any contiguous sequence of characters by first setting the mark at
171 one end of a sequence of characters, then going to the other end and
172 typing @kbd{C-w}.
173
174 @kindex M-z
175 @findex zap-to-char
176   A convenient way of killing is combined with searching: @kbd{M-z}
177 (@code{zap-to-char}) reads a character and kills from point up to (but not
178 including) the next occurrence of that character in the buffer.  If there
179 is no next occurrence, killing goes to the end of the buffer.  A numeric
180 argument acts as a repeat count.  A negative argument means to search
181 backward and kill text before point.
182
183   Other syntactic units can be killed: words, with @kbd{M-@key{DEL}} and
184 @kbd{M-d} (@pxref{Words}); sexps, with @kbd{C-M-k} (@pxref{Lists}); and
185 sentences, with @kbd{C-x @key{DEL}} and @kbd{M-k}
186 (@pxref{Sentences}).@refill
187
188 @node Yanking, Using X Selections, Killing, Top
189 @section Yanking
190 @cindex moving text
191 @cindex copying text
192 @cindex kill ring
193 @cindex yanking
194 @cindex pasting
195
196   @dfn{Yanking} means getting back text which was killed. Some systems
197 call this ``pasting''.  The usual way to move or copy text is to kill it
198 and then yank it one or more times.
199
200 @table @kbd
201 @item C-y
202 Yank last killed text (@code{yank}).
203 @item M-y
204 Replace re-inserted killed text with the previously killed text
205 (@code{yank-pop}).
206 @item M-w
207 Save region as last killed text without actually killing it
208 (@code{copy-region-as-kill}).
209 @item C-M-w
210 Append next kill to last batch of killed text (@code{append-next-kill}).
211 @end table
212
213 @menu
214 * Kill Ring::       Where killed text is stored.  Basic yanking.
215 * Appending Kills:: Several kills in a row all yank together.
216 * Earlier Kills::   Yanking something killed some time ago.
217 @end menu
218
219 @node Kill Ring, Appending Kills, Yanking, Yanking
220 @subsection The Kill Ring
221
222 @kindex C-y
223 @findex Yank
224   All killed text is recorded in the @dfn{kill ring}, a list of blocks of
225 text that have been killed.  There is only one kill ring, used in all
226 buffers, so you can kill text in one buffer and yank it in another buffer.
227 This is the usual way to move text from one file to another.
228 (@xref{Accumulating Text}, for some other ways.)
229
230   If you have two separate Emacs processes, you cannot use the kill ring
231 to move text. If you are using SXEmacs under X, however, you can
232 use the X selection mechanism to move text from one to another.
233
234 If you are using SXEmacs under X and have one Emacs process with
235 multiple frames, they do share the same kill ring.  You can kill or
236 copy text in one Emacs frame, then yank it in the other frame
237 belonging to the same process.
238
239   The command @kbd{C-y} (@code{yank}) reinserts the text of the most recent
240 kill.  It leaves the cursor at the end of the text and sets the mark at
241 the beginning of the text.  @xref{Mark}.
242
243   @kbd{C-u C-y} yanks the text, leaves the cursor in front of the text,
244 and sets the mark after it, if the argument is with just a @kbd{C-u}.
245 Any other argument, including @kbd{C-u} and digits, has different
246 results, described below, under ``Yanking Earlier Kills''.
247
248 @kindex M-w
249 @findex copy-region-as-kill
250  To copy a block of text, you can also use @kbd{M-w}
251 (@code{copy-region-as-kill}), which copies the region into the kill ring
252 without removing it from the buffer. @kbd{M-w} is similar to @kbd{C-w}
253 followed by @kbd{C-y} but does not mark the buffer as ``modified'' and
254 does not actually cut anything.
255
256 @node Appending Kills, Earlier Kills, Kill Ring, Yanking
257 @subsection Appending Kills
258
259 @cindex television
260   Normally, each kill command pushes a new block onto the kill ring.
261 However, two or more kill commands in a row combine their text into a
262 single entry, so that a single @kbd{C-y} yanks it all back. This means
263 you don't have to kill all the text you want to yank in one command; you
264 can kill line after line, or word after word, until you have killed what
265 you want, then get it all back at once using @kbd{C-y}. (Thus we join
266 television in leading people to kill thoughtlessly.)
267
268   Commands that kill forward from point add onto the end of the previous
269 killed text.  Commands that kill backward from point add onto the
270 beginning.  This way, any sequence of mixed forward and backward kill
271 commands puts all the killed text into one entry without rearrangement.
272 Numeric arguments do not break the sequence of appending kills.  For
273 example, suppose the buffer contains:
274
275 @example
276 This is the first
277 line of sample text
278 and here is the third.
279 @end example
280
281 @noindent
282 with point at the beginning of the second line.  If you type @kbd{C-k C-u 2
283 M-@key{DEL} C-k}, the first @kbd{C-k} kills the text @samp{line of sample
284 text}, @kbd{C-u 2 M-@key{DEL}} kills @samp{the first} with the newline that
285 followed it, and the second @kbd{C-k} kills the newline after the second
286 line.  The result is that the buffer contains @samp{This is and here is the
287 third.} and a single kill entry contains @samp{the first@key{RET}line of
288 sample text@key{RET}}---all the killed text, in its original order.
289
290 @kindex C-M-w
291 @findex append-next-kill
292   If a kill command is separated from the last kill command by other
293 commands (not just numeric arguments), it starts a new entry on the kill
294 ring.  To force a kill command to append, first type the command @kbd{C-M-w}
295 (@code{append-next-kill}). @kbd{C-M-w} tells the following command,
296 if it is a kill command, to append the text it kills to the last killed
297 text, instead of starting a new entry.  With @kbd{C-M-w}, you can kill
298 several separated pieces of text and accumulate them to be yanked back
299 in one place.@refill
300
301 @node Earlier Kills,, Appending Kills, Yanking
302 @subsection Yanking Earlier Kills
303
304 @kindex M-y
305 @findex yank-pop
306   To recover killed text that is no longer the most recent kill, you need
307 the @kbd{Meta-y} (@code{yank-pop}) command.  You can use @kbd{M-y} only
308 after a @kbd{C-y} or another @kbd{M-y}.  It takes the text previously
309 yanked and replaces it with the text from an earlier kill.  To recover
310 the text of the next-to-the-last kill, first use @kbd{C-y} to recover
311 the last kill, then @kbd{M-y} to replace it with the previous
312 kill.@refill
313
314   You can think in terms of a ``last yank'' pointer which points at an item
315 in the kill ring.  Each time you kill, the ``last yank'' pointer moves to
316 the new item at the front of the ring.  @kbd{C-y} yanks the item
317 which the ``last yank'' pointer points to.  @kbd{M-y} moves the ``last
318 yank'' pointer to a different item, and the text in the buffer changes to
319 match.  Enough @kbd{M-y} commands can move the pointer to any item in the
320 ring, so you can get any item into the buffer.  Eventually the pointer
321 reaches the end of the ring; the next @kbd{M-y} moves it to the first item
322 again.
323
324   Yanking moves the ``last yank'' pointer around the ring, but does not
325 change the order of the entries in the ring, which always runs from the
326 most recent kill at the front to the oldest one still remembered.
327
328   Use @kbd{M-y} with a numeric argument to advance the ``last
329 yank'' pointer by the specified number of items.  A negative argument
330 moves the pointer toward the front of the ring; from the front of the
331 ring, it moves to the last entry and starts moving forward from there.
332
333   Once the text you are looking for is brought into the buffer, you can
334 stop doing @kbd{M-y} commands and the text will stay there. Since the
335 text is just a copy of the kill ring item, editing it in the buffer does
336 not change what's in the ring.  As long you don't kill additional text,
337 the ``last yank'' pointer remains at the same place in the kill ring:
338 repeating @kbd{C-y} will yank another copy of the same old kill.
339
340   If you know how many @kbd{M-y} commands it would take to find the
341 text you want, you can yank that text in one step using @kbd{C-y} with
342 a numeric argument.  @kbd{C-y} with an argument greater than one
343 restores the text the specified number of entries back in the kill
344 ring.  Thus, @kbd{C-u 2 C-y} gets the next to the last block of killed
345 text.  It is equivalent to @kbd{C-y M-y}.  @kbd{C-y} with a numeric
346 argument starts counting from the ``last yank'' pointer, and sets the
347 ``last yank'' pointer to the entry that it yanks.
348
349 @vindex kill-ring-max
350   The variable @code{kill-ring-max} controls the length of the kill
351 ring; no more than that many blocks of killed text are saved.
352
353 @node Using X Selections, Accumulating Text, Yanking, Top
354 @section Using X Selections
355 @comment  node-name,  next,  previous,  up
356
357 In the X window system, mouse selections provide a simple mechanism for
358 text transfer between different applications.  In a typical X
359 application, you can select text by pressing the left mouse button and
360 dragging the cursor over the text you want to copy.  The text becomes the
361 primary X selection and is highlighted.  The highlighted region is also
362 the Emacs selected region.
363
364 @itemize @bullet
365 @item
366 Since the region is the primary X selection, you can go to a different X
367 application and click the middle mouse button: the text that you selected in
368 the previous application is pasted into the current application.
369 @item
370 Since the region is the Emacs selected region, you can use all region
371 commands (@kbd{C-w, M-w} etc.) as well as the options of the @b{Edit}
372 menu to manipulate the selected text.
373 @end itemize
374
375 @menu
376 * X Clipboard Selection::       Pasting to the X clipboard.
377 * X Selection Commands::        Other operations on the selection.
378 * X Cut Buffers::               X cut buffers are available for compatibility.
379 * Active Regions::              Using zmacs-style highlighting of the
380                                  selected region.
381 @end menu
382
383 @node X Clipboard Selection, X Selection Commands, Using X Selections, Using X Selections
384 @comment  node-name,  next,  previous,  up
385 @subsection The Clipboard Selection
386 @cindex clipboard selections
387
388 There are other kinds of X selections besides the @b{Primary} selection; one
389 common one is the @b{Clipboard} selection.  Some applications prefer to
390 transfer data using this selection in preference to the @b{Primary}.
391 One can transfer text from the @b{Primary} selection to the  @b{Clipboard}
392 selection with the @b{Copy} command under the @b{Edit} menu in the menubar.
393
394 Usually, the clipboard selection is not visible.  However, if you run the
395 @file{xclipboard} application, the text most recently copied to the clipboard
396 (with the @b{Copy} command) is displayed in a window.  Any time new text is
397 thus copied, the @file{xclipboard} application makes a copy of it and displays
398 it in its window.  The value of the clipboard can survive the lifetime of the
399 running Emacs process.  The @code{xclipboard} man page provides more details.
400
401 Warning: If you use the @file{xclipboard} application, remember that it
402 maintains a list of all things that have been pasted to the clipboard (that
403 is, copied with the @b{Copy} command).  If you don't manually delete elements
404 from this list by clicking on the @b{Delete} button in the @code{xclipboard}
405 window, the clipboard will eventually consume a lot of memory.
406
407 In summary, some X applications (such as @file{xterm}) allow one to paste
408 text in them from SXEmacs in the following way:
409
410 @itemize @bullet
411 @item
412 Drag out a region of text in Emacs with the left mouse button,
413 making that text be the @b{Primary} selection.
414
415 @item
416 Click the middle button in the other application, pasting the @b{Primary}
417 selection.
418 @end itemize
419
420 With some other applications (notably, the OpenWindows and Motif tools) you
421 must use this method instead:
422
423 @itemize @bullet
424 @item
425 Drag out a region of text in Emacs with the left mouse button,
426 making that text be the @b{Primary} selection.
427
428 @item
429 Copy the selected text to the @b{Clipboard} selection by selecting the
430 @b{Copy} menu item from the @b{Edit} menu, or by hitting the @b{Copy}
431 key on your keyboard.
432
433 @item
434 Paste the text in the other application by selecting @b{Paste} from its
435 menu, or by hitting the @b{Paste} key on your keyboard.
436 @end itemize
437
438
439 @node X Selection Commands, X Cut Buffers, X Clipboard Selection, Using X Selections
440 @subsection Miscellaneous X Selection Commands
441 @comment  node-name,  next,  previous,  up
442 @cindex cut buffers
443 @cindex primary selections
444
445 @findex x-copy-primary-selection
446 @findex x-delete-primary-selection
447 @findex x-insert-selection
448 @findex x-kill-primary-selection
449 @findex x-mouse-kill
450 @findex x-own-secondary-selection
451 @findex x-own-selection
452 @findex x-set-point-and-insert-selection
453 @table @kbd
454 @item M-x x-copy-primary-selection
455 Copy the primary selection to both the kill ring and the Clipboard.
456 @item M-x x-insert-selection
457 Insert the current selection into the buffer at point.
458 @item M-x x-delete-primary-selection
459 Deletes the text in the primary selection without copying it to the kill
460 ring or the Clipboard.
461 @item M-x x-kill-primary-selection
462 Deletes the text in the primary selection and copies it to
463 both the kill ring and the Clipboard.
464 @item M-x x-mouse-kill
465 Kill the text between point and the mouse and copy it to
466 the clipboard and to the cut buffer.
467 @item M-x x-own-secondary-selection
468 Make a secondary X selection of the given argument.
469 @item M-x x-own-selection
470 Make a primary X selection of the given argument.
471 @item M-x x-set-point-and-insert-selection
472 Set point where clicked and insert the primary selection or the
473 cut buffer.
474 @end table
475
476 @node X Cut Buffers, Active Regions, X Selection Commands, Using X Selections
477 @subsection X Cut Buffers
478 @comment  node-name,  next,  previous,  up
479
480 X cut buffers are a different, older way of transferring text between
481 applications.  SXEmacs supports cut buffers for compatibility
482 with older programs, even though selections are now the preferred way of
483 transferring text.
484
485 X has a concept of applications "owning" selections.  When you select
486 text by clicking and dragging inside an application, the application
487 tells the X server that it owns the selection.  When another
488 application asks the X server for the value of the selection, the X
489 server requests the information from the owner. When you use
490 selections, the selection data is not actually transferred unless
491 someone wants it; the act of making a selection doesn't transfer data.
492 Cut buffers are different: when you "own" a cut buffer, the data is
493 actually transferred to the X server immediately, and survives the
494 lifetime of the application.
495
496 Any time a region of text becomes the primary selection in Emacs,
497 Emacs also copies that text to the cut buffer.  This makes it possible
498 to copy text from an SXEmacs buffer and paste it into an older,
499 non-selection-based application (such as Emacs 18).
500
501 Note: Older versions of Emacs could not access the X selections, only
502 the X cut buffers.
503
504 @node Active Regions, , X Cut Buffers, Using X Selections
505 @subsection Active Regions
506 @comment  node-name,  next,  previous,  up
507 @cindex active regions
508
509   By default, both the text you select in an Emacs buffer using the
510 click-and-drag mechanism and text you select by setting point and the
511 mark is highlighted. You can use Emacs region commands as well as the
512 @b{Cut} and @b{Copy} commands on the highlighted region you selected
513 with the mouse.
514
515 If you prefer, you can make a distinction between text selected with the
516 mouse and text selected with point and the mark by setting the variable
517 @code{zmacs-regions} to @code{nil}.  In that case:
518
519 @itemize @bullet
520 @item
521 The text selected with the mouse becomes both the X selection and the
522 Emacs selected region. You can use menu-bar commands as well as Emacs
523 region commands on it.
524 @item
525 The text selected with point and the mark is not highlighted. You can
526 only use Emacs region commands on it, not the menu-bar items.
527 @end itemize
528
529   Active regions originally come from Zmacs, the Lisp Machine editor.
530 The idea behind them is that commands can only operate on a region when
531 the region is in an "active" state.  Put simply, you can only operate on
532 a region that is highlighted.
533
534 @vindex zmacs-regions
535 The variable @code{zmacs-regions} checks whether LISPM-style active
536 regions should be used.  This means that commands that operate on the
537 region (the area between point and the mark) only work while
538 the region is in the active state, which is indicated by highlighting.
539 Most commands causes the region to not be in the active state;
540 for example, @kbd{C-w} only works immediately after activating the
541 region.
542
543 More specifically:
544 @itemize @bullet
545 @item
546 Commands that operate on the region only work if the region is active.
547 @item
548 Only a very small set of commands causes the region to become active---
549 those commands whose semantics are to mark an area, such as @code{mark-defun}.
550 @item
551 The region is deactivated after each command that is executed, except that
552 motion commands do not change whether the region is active or not.
553 @end itemize
554
555 @code{set-mark-command} (@kbd{C-SPC}) pushes a mark and activates the
556 region.  Moving the cursor with normal motion commands (@kbd{C-n},
557 @kbd{C-p}, etc.) will cause the region between point and the
558 recently-pushed mark to be highlighted.  It will remain highlighted
559 until some non-motion command is executed.
560
561 @code{exchange-point-and-mark} (@kbd{C-x C-x}) activates the region.
562 So if you mark a region and execute a command that operates on it, you
563 can reactivate the same region with @kbd{C-x C-x} (or perhaps @kbd{C-x
564 C-x C-x C-x}) to operate on it again.
565
566 Generally, commands that push marks as a means of navigation, such as
567 @code{beginning-of-buffer} (@kbd{M-<}) and @code{end-of-buffer}
568 (@kbd{M->}), do not activate the region.  However, commands that push
569 marks as a means of marking an area of text, such as @code{mark-defun}
570 (@kbd{M-C-h}), @code{mark-word} (@kbd{M-@@}), and @code{mark-whole-buffer}
571 (@kbd{C-x h}), do activate the region.
572
573 When @code{zmacs-regions} is @code{t}, there is no distinction between
574 the primary X selection and the active region selected by point and the
575 mark.  To see this, set the mark (@key{C-SPC}) and move the cursor
576 with any cursor-motion command: the region between point and mark is
577 highlighted, and you can watch it grow and shrink as you move the
578 cursor.
579
580 Any other commands besides cursor-motion commands (such as inserting or
581 deleting text) will cause the region to no longer be active; it will no
582 longer be highlighted, and will no longer be the primary selection.
583 Region can be explicitly deactivated with @kbd{C-g}.
584
585 Commands that require a region (such as @kbd{C-w}) signal an error if
586 the region is not active.  Certain commands cause the region to be in
587 its active state.  The most common ones are @code{push-mark}
588 (@key{C-SPC}) and @code{exchange-point-and-mark} (@kbd{C-x C-x}).
589
590 @vindex zmacs-region-stays
591 When @code{zmacs-regions} is @code{t}, programs can be non-intrusive
592 on the state of the region by setting the variable @code{zmacs-region-stays}
593 to a non-@code{nil} value.  If you are writing a new Emacs command that
594 is conceptually a ``motion'' command and should not interfere with the
595 current highlightedness of the region, then you may set this variable.
596 It is reset to @code{nil} after each user command is executed.
597
598 @findex zmacs-activate-region
599 When @code{zmacs-regions} is @code{t}, programs can make the region between
600 point and mark go into the active (highlighted) state by using the
601 function @code{zmacs-activate-region}. Only a small number of commands
602 should ever do this.
603
604 @findex zmacs-deactivate-region
605 When @code{zmacs-regions} is @code{t}, programs can deactivate the region
606 between point and the mark by using @code{zmacs-deactivate-region}.
607 Note: you should not have to call this function; the command loop calls
608 it when appropriate.
609
610 @node Accumulating Text, Rectangles, Using X Selections, Top
611 @section Accumulating Text
612 @findex append-to-buffer
613 @findex prepend-to-buffer
614 @findex copy-to-buffer
615 @findex append-to-file
616 @cindex copying text
617 @cindex accumulating text
618
619   Usually you copy or move text by killing it and yanking it, but there are
620 other ways that are useful for copying one block of text in many places, or
621 for copying many scattered blocks of text into one place.
622
623   If you like, you can accumulate blocks of text from scattered
624 locations either into a buffer or into a file.  The relevant commands
625 are described here.  You can also use Emacs registers for storing and
626 accumulating text.  @xref{Registers}.
627
628 @table @kbd
629 @item M-x append-to-buffer
630 Append region to contents of specified buffer (@code{append-to-buffer}).
631 @item M-x prepend-to-buffer
632 Prepend region to contents of specified buffer.
633 @item M-x copy-to-buffer
634 Copy region into specified buffer, deleting that buffer's old contents.
635 @item M-x insert-buffer
636 Insert contents of specified buffer into current buffer at point.
637 @item M-x append-to-file
638 Append region to the end of the contents of specified file.
639 @end table
640
641   To accumulate text into a buffer, use the command @kbd{M-x
642 append-to-buffer}, which inserts a copy of the region into the buffer
643 @var{buffername}, at the location of point in that buffer.  If there is
644 no buffer with the given name, one is created.
645
646   If you append text to a buffer that has been used for editing, the
647 copied text goes to the place where point is.  Point in that buffer is
648 left at the end of the copied text, so successive uses of
649 @code{append-to-buffer} accumulate the text in the specified buffer in
650 the same order as they were copied.  Strictly speaking, this command does
651 not always append to the text already in the buffer; but if this command
652 is the only command used to alter a buffer, it does always append to the
653 existing text because point is always at the end.
654
655   @kbd{M-x prepend-to-buffer} is similar to @code{append-to-buffer}, but
656 point in the other buffer is left before the copied text, so successive
657 prependings add text in reverse order.  @kbd{M-x copy-to-buffer} is
658 similar, except that any existing text in the other buffer is deleted,
659 so the buffer is left containing just the text newly copied into it.
660
661   You can retrieve the accumulated text from that buffer with @kbd{M-x
662 insert-buffer}, which takes @var{buffername} as an argument.  It inserts
663 a copy of the text in buffer @var{buffername} into the selected buffer.
664 You could alternatively select the other buffer for editing, perhaps moving
665 text from it by killing or with @code{append-to-buffer}.  @xref{Buffers}, for
666 background information on buffers.
667
668   Instead of accumulating text within Emacs in a buffer, you can append
669 text directly into a file with @kbd{M-x append-to-file}, which takes
670 @var{file-name} as an argument.  It adds the text of the region to the
671 end of the specified file.  The file is changed immediately on disk.
672 This command is normally used with files that are @i{not} being visited
673 in Emacs.  Using it on a file that Emacs is visiting can produce
674 confusing results, because the file's text inside Emacs does not change
675 while the file itself changes.
676
677 @node Rectangles, Registers, Accumulating Text, Top
678 @section Rectangles
679 @cindex rectangles
680
681   The rectangle commands affect rectangular areas of text: all
682 characters between a certain pair of columns, in a certain range of lines.
683 Commands are provided to kill rectangles, yank killed rectangles, clear
684 them out, or delete them.  Rectangle commands are useful with text in
685 multicolumnar formats, like code with comments at the right,
686 or for changing text into or out of such formats.
687
688   To specify the rectangle a command should work on, put the mark at one
689 corner and point at the opposite corner.  The specified rectangle is
690 called the @dfn{region-rectangle} because it is controlled about the
691 same way the region is controlled.  Remember that a given
692 combination of point and mark values can be interpreted either as
693 specifying a region or as specifying a rectangle; it is up to the
694 command that uses them to choose the interpretation.
695
696 @table @kbd
697 @item M-x delete-rectangle
698 Delete the text of the region-rectangle, moving any following text on
699 each line leftward to the left edge of the region-rectangle.
700 @item M-x kill-rectangle
701 Similar, but also save the contents of the region-rectangle as the
702 ``last killed rectangle''.
703 @item M-x yank-rectangle
704 Yank the last killed rectangle with its upper left corner at point.
705 @item M-x open-rectangle
706 Insert blank space to fill the space of the region-rectangle.
707 The previous contents of the region-rectangle are pushed rightward.
708 @item M-x clear-rectangle
709 Clear the region-rectangle by replacing its contents with spaces.
710 @end table
711
712   The rectangle operations fall into two classes: commands deleting and
713 moving rectangles, and commands for blank rectangles.
714
715 @findex delete-rectangle
716 @findex kill-rectangle
717   There are two ways to get rid of the text in a rectangle: you can discard
718 the text (delete it) or save it as the ``last killed'' rectangle.  The
719 commands for these two ways are @kbd{M-x delete-rectangle} and @kbd{M-x
720 kill-rectangle}.  In either case, the portion of each line that falls inside
721 the rectangle's boundaries is deleted, causing following text (if any) on
722 the line to move left.
723
724   Note that ``killing'' a rectangle is not killing in the usual sense; the
725 rectangle is not stored in the kill ring, but in a special place that
726 only records the most recently killed rectangle (that is, does not
727 append to a killed rectangle).  Different yank commands
728 have to be used and only one rectangle is stored, because yanking
729 a rectangle is quite different from yanking linear text and yank-popping
730 commands are difficult to make sense of.
731
732   Inserting a rectangle is the opposite of deleting one.  You specify
733 where to put the upper left corner by putting point there.  The
734 rectangle's first line is inserted at point, the rectangle's second line
735 is inserted at a point one line vertically down, and so on.  The number
736 of lines affected is determined by the height of the saved rectangle.
737
738 @findex yank-rectangle
739   To insert the last killed rectangle, type @kbd{M-x yank-rectangle}.
740 This can be used to convert single-column lists into double-column
741 lists; kill the second half of the list as a rectangle and then
742 yank it beside the first line of the list.
743
744 @findex open-rectangle
745 @findex clear-rectangle
746   There are two commands for working with blank rectangles: @kbd{M-x
747 clear-rectangle} erases existing text, and @kbd{M-x open-rectangle}
748 inserts a blank rectangle.  Clearing a rectangle is equivalent to
749 deleting it and then inserting a blank rectangle of the same size.
750
751   Rectangles can also be copied into and out of registers.
752 @xref{RegRect,,Rectangle Registers}.