entered into RCS
[bpt/emacs.git] / lisp / simple.el
CommitLineData
c88ab9ce
ER
1;;; simple.el --- basic editing commands for Emacs
2
71e40adf 3;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc.
2076c87c
JB
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software; you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation; either version 1, or (at your option)
10;; any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs; see the file COPYING. If not, write to
19;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22(defun open-line (arg)
23 "Insert a newline and leave point before it. If there is a fill
24prefix, inserts the fill prefix after the newline that it inserts.
25With arg, inserts that many newlines."
26 (interactive "*p")
27 (let ((flag (and (bolp) (not (bobp)))))
28 (if flag (forward-char -1))
29 (while (> arg 0)
30 (save-excursion
31 (insert ?\n)
32 (if fill-prefix (insert fill-prefix)))
33 (setq arg (1- arg)))
34 (if flag (forward-char 1))))
35
36(defun split-line ()
37 "Split current line, moving portion beyond point vertically down."
38 (interactive "*")
39 (skip-chars-forward " \t")
40 (let ((col (current-column))
41 (pos (point)))
42 (insert ?\n)
43 (indent-to col 0)
44 (goto-char pos)))
45
46(defun quoted-insert (arg)
47 "Read next input character and insert it.
48Useful for inserting control characters.
49You may also type up to 3 octal digits, to insert a character with that code"
50 (interactive "*p")
51 (let ((char (read-quoted-char)))
52 (while (> arg 0)
53 (insert char)
54 (setq arg (1- arg)))))
55
56(defun delete-indentation (&optional arg)
57 "Join this line to previous and fix up whitespace at join.
58With argument, join this line to following line."
59 (interactive "*P")
60 (beginning-of-line)
61 (if arg (forward-line 1))
62 (if (eq (preceding-char) ?\n)
63 (progn
64 (delete-region (point) (1- (point)))
65 (fixup-whitespace))))
66
67(defun fixup-whitespace ()
68 "Fixup white space between objects around point.
69Leave one space or none, according to the context."
70 (interactive "*")
71 (save-excursion
72 (delete-horizontal-space)
73 (if (or (looking-at "^\\|\\s)")
74 (save-excursion (forward-char -1)
75 (looking-at "$\\|\\s(\\|\\s'")))
76 nil
77 (insert ?\ ))))
78
79(defun delete-horizontal-space ()
80 "Delete all spaces and tabs around point."
81 (interactive "*")
82 (skip-chars-backward " \t")
83 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
84
85(defun just-one-space ()
86 "Delete all spaces and tabs around point, leaving one space."
87 (interactive "*")
88 (skip-chars-backward " \t")
89 (if (= (following-char) ? )
90 (forward-char 1)
91 (insert ? ))
92 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
93
94(defun delete-blank-lines ()
95 "On blank line, delete all surrounding blank lines, leaving just one.
96On isolated blank line, delete that one.
97On nonblank line, delete all blank lines that follow it."
98 (interactive "*")
99 (let (thisblank singleblank)
100 (save-excursion
101 (beginning-of-line)
102 (setq thisblank (looking-at "[ \t]*$"))
70e14c01 103 ;; Set singleblank if there is just one blank line here.
2076c87c
JB
104 (setq singleblank
105 (and thisblank
106 (not (looking-at "[ \t]*\n[ \t]*$"))
107 (or (bobp)
108 (progn (forward-line -1)
109 (not (looking-at "[ \t]*$")))))))
70e14c01 110 ;; Delete preceding blank lines, and this one too if it's the only one.
2076c87c
JB
111 (if thisblank
112 (progn
113 (beginning-of-line)
114 (if singleblank (forward-line 1))
115 (delete-region (point)
116 (if (re-search-backward "[^ \t\n]" nil t)
117 (progn (forward-line 1) (point))
118 (point-min)))))
70e14c01
JB
119 ;; Delete following blank lines, unless the current line is blank
120 ;; and there are no following blank lines.
2076c87c
JB
121 (if (not (and thisblank singleblank))
122 (save-excursion
123 (end-of-line)
124 (forward-line 1)
125 (delete-region (point)
126 (if (re-search-forward "[^ \t\n]" nil t)
127 (progn (beginning-of-line) (point))
70e14c01
JB
128 (point-max)))))
129 ;; Handle the special case where point is followed by newline and eob.
130 ;; Delete the line, leaving point at eob.
131 (if (looking-at "^[ \t]*\n\\'")
132 (delete-region (point) (point-max)))))
2076c87c
JB
133
134(defun back-to-indentation ()
135 "Move point to the first non-whitespace character on this line."
136 (interactive)
137 (beginning-of-line 1)
138 (skip-chars-forward " \t"))
139
140(defun newline-and-indent ()
141 "Insert a newline, then indent according to major mode.
142Indentation is done using the current indent-line-function.
143In programming language modes, this is the same as TAB.
144In some text modes, where TAB inserts a tab, this indents to the
145specified left-margin column."
146 (interactive "*")
147 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
46947372 148 (newline)
2076c87c
JB
149 (indent-according-to-mode))
150
151(defun reindent-then-newline-and-indent ()
152 "Reindent current line, insert newline, then indent the new line.
153Indentation of both lines is done according to the current major mode,
154which means that the current value of indent-line-function is called.
155In programming language modes, this is the same as TAB.
156In some text modes, where TAB inserts a tab, this indents to the
157specified left-margin column."
158 (interactive "*")
159 (save-excursion
160 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
161 (indent-according-to-mode))
46947372 162 (newline)
2076c87c
JB
163 (indent-according-to-mode))
164
165;; Internal subroutine of delete-char
166(defun kill-forward-chars (arg)
167 (if (listp arg) (setq arg (car arg)))
168 (if (eq arg '-) (setq arg -1))
169 (kill-region (point) (+ (point) arg)))
170
171;; Internal subroutine of backward-delete-char
172(defun kill-backward-chars (arg)
173 (if (listp arg) (setq arg (car arg)))
174 (if (eq arg '-) (setq arg -1))
175 (kill-region (point) (- (point) arg)))
176
177(defun backward-delete-char-untabify (arg &optional killp)
178 "Delete characters backward, changing tabs into spaces.
179Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
180Interactively, ARG is the prefix arg (default 1)
181and KILLP is t if prefix arg is was specified."
182 (interactive "*p\nP")
183 (let ((count arg))
184 (save-excursion
185 (while (and (> count 0) (not (bobp)))
186 (if (= (preceding-char) ?\t)
187 (let ((col (current-column)))
188 (forward-char -1)
189 (setq col (- col (current-column)))
190 (insert-char ?\ col)
191 (delete-char 1)))
192 (forward-char -1)
193 (setq count (1- count)))))
194 (delete-backward-char arg killp)
195 ;; In overwrite mode, back over columns while clearing them out,
196 ;; unless at end of line.
197 (and overwrite-mode (not (eolp))
198 (save-excursion (insert-char ?\ arg))))
199
200(defun zap-to-char (arg char)
201 "Kill up to and including ARG'th occurrence of CHAR.
202Goes backward if ARG is negative; error if CHAR not found."
203 (interactive "p\ncZap to char: ")
204 (kill-region (point) (progn
205 (search-forward (char-to-string char) nil nil arg)
206; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
207 (point))))
208
209(defun beginning-of-buffer (&optional arg)
210 "Move point to the beginning of the buffer; leave mark at previous position.
211With arg N, put point N/10 of the way from the true beginning.
212Don't use this in Lisp programs!
213\(goto-char (point-min)) is faster and avoids clobbering the mark."
214 (interactive "P")
215 (push-mark)
216 (goto-char (if arg
217 (if (> (buffer-size) 10000)
218 ;; Avoid overflow for large buffer sizes!
219 (* (prefix-numeric-value arg)
220 (/ (buffer-size) 10))
221 (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
222 (point-min)))
223 (if arg (forward-line 1)))
224
225(defun end-of-buffer (&optional arg)
226 "Move point to the end of the buffer; leave mark at previous position.
227With arg N, put point N/10 of the way from the true end.
228Don't use this in Lisp programs!
229\(goto-char (point-max)) is faster and avoids clobbering the mark."
230 (interactive "P")
231 (push-mark)
232 (goto-char (if arg
233 (- (1+ (buffer-size))
234 (if (> (buffer-size) 10000)
235 ;; Avoid overflow for large buffer sizes!
236 (* (prefix-numeric-value arg)
237 (/ (buffer-size) 10))
238 (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
239 (point-max)))
240 (if arg (forward-line 1)
241 ;; Scroll to put point near bottom--show nearly maximum amount of text,
242 ;; but leave room to add something.
243 (recenter -3)))
244
245(defun mark-whole-buffer ()
70e14c01
JB
246 "Put point at beginning and mark at end of buffer.
247You probably should not use this function in Lisp programs;
248it is usually a mistake for a Lisp function to use any subroutine
249that uses or sets the mark."
2076c87c
JB
250 (interactive)
251 (push-mark (point))
252 (push-mark (point-max))
253 (goto-char (point-min)))
254
255(defun count-lines-region (start end)
256 "Print number of lines and charcters in the region."
257 (interactive "r")
258 (message "Region has %d lines, %d characters"
259 (count-lines start end) (- end start)))
260
261(defun what-line ()
262 "Print the current line number (in the buffer) of point."
263 (interactive)
264 (save-restriction
265 (widen)
266 (save-excursion
267 (beginning-of-line)
268 (message "Line %d"
269 (1+ (count-lines 1 (point)))))))
270
271(defun count-lines (start end)
272 "Return number of lines between START and END.
273This is usually the number of newlines between them,
274but will be one more if START is not equal to END
275and the greater of them is not at the start of a line."
276 (save-excursion
277 (save-restriction
278 (narrow-to-region start end)
279 (goto-char (point-min))
280 (if (eq selective-display t)
281 (let ((done 0))
282 (while (re-search-forward "[\n\C-m]" nil t 40)
283 (setq done (+ 40 done)))
284 (while (re-search-forward "[\n\C-m]" nil t 1)
285 (setq done (+ 1 done)))
286 done)
287 (- (buffer-size) (forward-line (buffer-size)))))))
288
289(defun what-cursor-position ()
290 "Print info on cursor position (on screen and within buffer)."
291 (interactive)
292 (let* ((char (following-char))
293 (beg (point-min))
294 (end (point-max))
295 (pos (point))
296 (total (buffer-size))
297 (percent (if (> total 50000)
298 ;; Avoid overflow from multiplying by 100!
299 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
300 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
301 (hscroll (if (= (window-hscroll) 0)
302 ""
303 (format " Hscroll=%d" (window-hscroll))))
304 (col (current-column)))
305 (if (= pos end)
306 (if (or (/= beg 1) (/= end (1+ total)))
307 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
308 pos total percent beg end col hscroll)
309 (message "point=%d of %d(%d%%) column %d %s"
310 pos total percent col hscroll))
311 (if (or (/= beg 1) (/= end (1+ total)))
312 (message "Char: %s (0%o) point=%d of %d(%d%%) <%d - %d> column %d %s"
313 (single-key-description char) char pos total percent beg end col hscroll)
314 (message "Char: %s (0%o) point=%d of %d(%d%%) column %d %s"
315 (single-key-description char) char pos total percent col hscroll)))))
316
317(defun fundamental-mode ()
318 "Major mode not specialized for anything in particular.
319Other major modes are defined by comparison with this one."
320 (interactive)
321 (kill-all-local-variables))
322
323(put 'eval-expression 'disabled t)
324
325;; We define this, rather than making eval interactive,
326;; for the sake of completion of names like eval-region, eval-current-buffer.
327(defun eval-expression (expression)
328 "Evaluate EXPRESSION and print value in minibuffer.
329Value is also consed on to front of variable values 's value."
330 (interactive "xEval: ")
331 (setq values (cons (eval expression) values))
332 (prin1 (car values) t))
333
334(defun edit-and-eval-command (prompt command)
335 "Prompting with PROMPT, let user edit COMMAND and eval result.
336COMMAND is a Lisp expression. Let user edit that expression in
337the minibuffer, then read and evaluate the result."
338 (let ((command (read-minibuffer prompt
339 (prin1-to-string command))))
340 ;; Add edited command to command history, unless redundant.
341 (or (equal command (car command-history))
342 (setq command-history (cons command command-history)))
343 (eval command)))
344
345;; (defvar repeat-complex-command nil)
346
347(defvar repeat-complex-command-map (copy-keymap minibuffer-local-map))
348(define-key repeat-complex-command-map "\ep" 'previous-complex-command)
349(define-key repeat-complex-command-map "\en" 'next-complex-command)
350(defun repeat-complex-command (repeat-complex-command-arg)
351 "Edit and re-evaluate last complex command, or ARGth from last.
352A complex command is one which used the minibuffer.
353The command is placed in the minibuffer as a Lisp form for editing.
354The result is executed, repeating the command as changed.
355If the command has been changed or is not the most recent previous command
356it is added to the front of the command history.
357Whilst editing the command, the following commands are available:
358\\{repeat-complex-command-map}"
359 (interactive "p")
360 (let ((elt (nth (1- repeat-complex-command-arg) command-history))
361 (repeat-complex-command-flag t)
362 newcmd)
363 (if elt
364 (progn
365 (setq newcmd (read-from-minibuffer "Redo: "
366 (prin1-to-string elt)
367 repeat-complex-command-map
368 t))
369 ;; If command to be redone does not match front of history,
370 ;; add it to the history.
371 (or (equal newcmd (car command-history))
372 (setq command-history (cons newcmd command-history)))
373 (eval newcmd))
374 (ding))))
375
376(defun next-complex-command (n)
377 "Inserts the next element of `command-history' into the minibuffer."
378 (interactive "p")
379 (let ((narg (min (max 1 (- repeat-complex-command-arg n))
380 (length command-history))))
381 (if (= repeat-complex-command-arg narg)
382 (error (if (= repeat-complex-command-arg 1)
383 "No following item in command history"
384 "No preceding item in command history"))
385 (erase-buffer)
386 (setq repeat-complex-command-arg narg)
387 (insert (prin1-to-string (nth (1- repeat-complex-command-arg)
388 command-history)))
389 (goto-char (point-min)))))
390
391(defun previous-complex-command (n)
392 "Inserts the previous element of `command-history' into the minibuffer."
393 (interactive "p")
394 (if repeat-complex-command-flag
395 (next-complex-command (- n))
396 (repeat-complex-command 1)))
397
398(defun goto-line (arg)
399 "Goto line ARG, counting from line 1 at beginning of buffer."
400 (interactive "NGoto line: ")
401 (save-restriction
402 (widen)
403 (goto-char 1)
404 (if (eq selective-display t)
405 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
406 (forward-line (1- arg)))))
407
408;Put this on C-x u, so we can force that rather than C-_ into startup msg
409(fset 'advertised-undo 'undo)
410
411(defun undo (&optional arg)
412 "Undo some previous changes.
413Repeat this command to undo more changes.
414A numeric argument serves as a repeat count."
415 (interactive "*p")
416 (let ((modified (buffer-modified-p)))
71e40adf
JB
417 (or (eq (selected-window) (minibuffer-window))
418 (message "Undo!"))
2076c87c
JB
419 (or (eq last-command 'undo)
420 (progn (undo-start)
421 (undo-more 1)))
422 (setq this-command 'undo)
423 (undo-more (or arg 1))
424 (and modified (not (buffer-modified-p))
425 (delete-auto-save-file-if-necessary))))
426
427(defun undo-start ()
428 "Move pending-undo-list to front of undo records.
429The next call to undo-more will undo the most recently made change."
430 (if (eq buffer-undo-list t)
431 (error "No undo information in this buffer"))
432 (setq pending-undo-list buffer-undo-list))
433
434(defun undo-more (count)
435 "Undo back N undo-boundaries beyond what was already undone recently.
436Call undo-start to get ready to undo recent changes,
437then call undo-more one or more times to undo them."
438 (or pending-undo-list
439 (error "No further undo information"))
440 (setq pending-undo-list (primitive-undo count pending-undo-list)))
441
442(defvar last-shell-command "")
443(defvar last-shell-command-on-region "")
444
445(defun shell-command (command &optional flag)
446 "Execute string COMMAND in inferior shell; display output, if any.
447If COMMAND ends in ampersand, execute it asynchronously.
448
449Optional second arg non-nil (prefix arg, if interactive)
450means insert output in current buffer after point (leave mark after it).
451This cannot be done asynchronously."
452 (interactive (list (read-string "Shell command: " last-shell-command)
453 current-prefix-arg))
454 (if flag
455 (progn (barf-if-buffer-read-only)
456 (push-mark)
457 ;; We do not use -f for csh; we will not support broken use of
458 ;; .cshrcs. Even the BSD csh manual says to use
459 ;; "if ($?prompt) exit" before things which are not useful
460 ;; non-interactively. Besides, if someone wants their other
461 ;; aliases for shell commands then they can still have them.
462 (call-process shell-file-name nil t nil
463 "-c" command)
464 (exchange-point-and-mark))
465 ;; Preserve the match data in case called from a program.
466 (let ((data (match-data)))
467 (unwind-protect
468 (if (string-match "[ \t]*&[ \t]*$" command)
469 ;; Command ending with ampersand means asynchronous.
470 (let ((buffer (get-buffer-create "*shell-command*"))
471 (directory default-directory)
472 proc)
473 ;; Remove the ampersand.
474 (setq command (substring command 0 (match-beginning 0)))
475 ;; If will kill a process, query first.
476 (setq proc (get-buffer-process buffer))
477 (if proc
478 (if (yes-or-no-p "A command is running. Kill it? ")
479 (kill-process proc)
480 (error "Shell command in progress")))
481 (save-excursion
482 (set-buffer buffer)
483 (erase-buffer)
484 (display-buffer buffer)
485 (setq default-directory directory)
486 (setq proc (start-process "Shell" buffer
487 shell-file-name "-c" command))
488 (setq mode-line-process '(": %s"))
489 (set-process-sentinel proc 'shell-command-sentinel)
490 (set-process-filter proc 'shell-command-filter)
491 ))
492 (shell-command-on-region (point) (point) command nil))
493 (store-match-data data)))))
494
495;; We have a sentinel to prevent insertion of a termination message
496;; in the buffer itself.
497(defun shell-command-sentinel (process signal)
498 (if (memq (process-status process) '(exit signal))
499 (progn
500 (message "%s: %s."
501 (car (cdr (cdr (process-command process))))
502 (substring signal 0 -1))
503 (save-excursion
504 (set-buffer (process-buffer process))
505 (setq mode-line-process nil))
506 (delete-process process))))
507
508(defun shell-command-filter (proc string)
509 ;; Do save-excursion by hand so that we can leave point numerically unchanged
510 ;; despite an insertion immediately after it.
511 (let* ((obuf (current-buffer))
512 (buffer (process-buffer proc))
513 opoint
514 (window (get-buffer-window buffer))
515 (pos (window-start window)))
516 (unwind-protect
517 (progn
518 (set-buffer buffer)
519 (setq opoint (point))
520 (goto-char (point-max))
521 (insert-before-markers string))
522 ;; insert-before-markers moved this marker: set it back.
523 (set-window-start window pos)
524 ;; Finish our save-excursion.
525 (goto-char opoint)
526 (set-buffer obuf))))
527
528(defun shell-command-on-region (start end command &optional flag interactive)
529 "Execute string COMMAND in inferior shell with region as input.
530Normally display output (if any) in temp buffer `*Shell Command Output*';
531Prefix arg means replace the region with it.
532Noninteractive args are START, END, COMMAND, FLAG.
533Noninteractively FLAG means insert output in place of text from START to END,
534and put point at the end, but don't alter the mark.
535
536If the output is one line, it is displayed in the echo area,
537but it is nonetheless available in buffer `*Shell Command Output*'
538even though that buffer is not automatically displayed. If there is no output
539or output is inserted in the current buffer then `*Shell Command Output*' is
540deleted."
541 (interactive (list (min (point) (mark)) (max (point) (mark))
542 (read-string "Shell command on region: "
543 last-shell-command-on-region)
544 current-prefix-arg
545 (prefix-numeric-value current-prefix-arg)))
546 (if flag
547 ;; Replace specified region with output from command.
548 (let ((swap (and interactive (< (point) (mark)))))
549 ;; Don't muck with mark
550 ;; unless called interactively.
551 (and interactive (push-mark))
552 (call-process-region start end shell-file-name t t nil
553 "-c" command)
554 (if (get-buffer "*Shell Command Output*")
555 (kill-buffer "*Shell Command Output*"))
556 (and interactive swap (exchange-point-and-mark)))
557 ;; No prefix argument: put the output in a temp buffer,
558 ;; replacing its entire contents.
559 (let ((buffer (get-buffer-create "*Shell Command Output*")))
560 (if (eq buffer (current-buffer))
561 ;; If the input is the same buffer as the output,
562 ;; delete everything but the specified region,
563 ;; then replace that region with the output.
564 (progn (delete-region end (point-max))
565 (delete-region (point-min) start)
566 (call-process-region (point-min) (point-max)
567 shell-file-name t t nil
568 "-c" command))
569 ;; Clear the output buffer, then run the command with output there.
570 (save-excursion
571 (set-buffer buffer)
572 (erase-buffer))
573 (call-process-region start end shell-file-name
574 nil buffer nil
575 "-c" command))
576 ;; Report the amount of output.
577 (let ((lines (save-excursion
578 (set-buffer buffer)
579 (if (= (buffer-size) 0)
580 0
581 (count-lines (point-min) (point-max))))))
582 (cond ((= lines 0)
583 (message "(Shell command completed with no output)")
584 (kill-buffer "*Shell Command Output*"))
585 ((= lines 1)
586 (message "%s"
587 (save-excursion
588 (set-buffer buffer)
589 (goto-char (point-min))
590 (buffer-substring (point)
591 (progn (end-of-line) (point))))))
592 (t
593 (set-window-start (display-buffer buffer) 1)))))))
594\f
595(defun universal-argument ()
596 "Begin a numeric argument for the following command.
597Digits or minus sign following \\[universal-argument] make up the numeric argument.
598\\[universal-argument] following the digits or minus sign ends the argument.
599\\[universal-argument] without digits or minus sign provides 4 as argument.
600Repeating \\[universal-argument] without digits or minus sign
601 multiplies the argument by 4 each time."
602 (interactive nil)
c637ae6f
JB
603 (let ((factor 4)
604 key)
70e14c01
JB
605;; (describe-arg (list factor) 1)
606 (setq key (read-key-sequence nil t))
c637ae6f
JB
607 (while (equal (key-binding key) 'universal-argument)
608 (setq factor (* 4 factor))
70e14c01
JB
609;; (describe-arg (list factor) 1)
610 (setq key (read-key-sequence nil t)))
c637ae6f
JB
611 (prefix-arg-internal key factor nil)))
612
613(defun prefix-arg-internal (key factor value)
2076c87c
JB
614 (let ((sign 1))
615 (if (and (numberp value) (< value 0))
616 (setq sign -1 value (- value)))
617 (if (eq value '-)
618 (setq sign -1 value nil))
70e14c01 619;; (describe-arg value sign)
c637ae6f
JB
620 (while (equal key "-")
621 (setq sign (- sign) factor nil)
70e14c01
JB
622;; (describe-arg value sign)
623 (setq key (read-key-sequence nil t)))
c637ae6f
JB
624 (while (and (= (length key) 1)
625 (not (string< key "0"))
626 (not (string< "9" key)))
627 (setq value (+ (* (if (numberp value) value 0) 10)
628 (- (aref key 0) ?0))
629 factor nil)
70e14c01
JB
630;; (describe-arg value sign)
631 (setq key (read-key-sequence nil t)))
2076c87c 632 (setq prefix-arg
c637ae6f 633 (cond (factor (list factor))
2076c87c
JB
634 ((numberp value) (* value sign))
635 ((= sign -1) '-)))
c637ae6f
JB
636 ;; Calling universal-argument after digits
637 ;; terminates the argument but is ignored.
638 (if (eq (key-binding key) 'universal-argument)
639 (progn
640 (describe-arg value sign)
70e14c01 641 (setq key (read-key-sequence nil t))))
c637ae6f
JB
642 (if (= (length key) 1)
643 ;; Make sure self-insert-command finds the proper character;
644 ;; unread the character and let the command loop process it.
645 (setq unread-command-char (string-to-char key))
646 ;; We can't push back a longer string, so we'll emulate the
647 ;; command loop ourselves.
648 (command-execute (key-binding key)))))
649
650(defun describe-arg (value sign)
651 (cond ((numberp value)
652 (message "Arg: %d" (* value sign)))
653 ((consp value)
654 (message "Arg: [%d]" (car value)))
655 ((< sign 0)
656 (message "Arg: -"))))
2076c87c
JB
657
658(defun digit-argument (arg)
659 "Part of the numeric argument for the next command.
660\\[universal-argument] following digits or minus sign ends the argument."
661 (interactive "P")
c637ae6f
JB
662 (prefix-arg-internal (char-to-string (logand last-command-char ?\177))
663 nil arg))
2076c87c
JB
664
665(defun negative-argument (arg)
666 "Begin a negative numeric argument for the next command.
667\\[universal-argument] following digits or minus sign ends the argument."
668 (interactive "P")
c637ae6f 669 (prefix-arg-internal "-" nil arg))
2076c87c
JB
670\f
671(defun forward-to-indentation (arg)
672 "Move forward ARG lines and position at first nonblank character."
673 (interactive "p")
674 (forward-line arg)
675 (skip-chars-forward " \t"))
676
677(defun backward-to-indentation (arg)
678 "Move backward ARG lines and position at first nonblank character."
679 (interactive "p")
680 (forward-line (- arg))
681 (skip-chars-forward " \t"))
682
683(defun kill-line (&optional arg)
684 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
685With prefix argument, kill that many lines from point.
686Negative arguments kill lines backward.
687
688When calling from a program, nil means \"no arg\",
689a number counts as a prefix arg."
690 (interactive "P")
691 (kill-region (point)
692 (progn
693 (if arg
694 (forward-line (prefix-numeric-value arg))
695 (if (eobp)
696 (signal 'end-of-buffer nil))
697 (if (looking-at "[ \t]*$")
698 (forward-line 1)
699 (end-of-line)))
700 (point))))
701\f
70e14c01
JB
702;;;; Window system cut and paste hooks.
703
704(defvar interprogram-cut-function nil
705 "Function to call to make a killed region available to other programs.
706
707Most window systems provide some sort of facility for cutting and
708pasting text between the windows of different programs. On startup,
709this variable is set to a function which emacs will call whenever text
710is put in the kill ring to make the new kill available to other
711programs.
712
713The function takes one argument, TEXT, which is a string containing
714the text which should be made available.")
715
716(defvar interprogram-paste-function nil
717 "Function to call to get text cut from other programs.
718
719Most window systems provide some sort of facility for cutting and
720pasting text between the windows of different programs. On startup,
721this variable is set to a function which emacs will call to obtain
722text that other programs have provided for pasting.
723
724The function should be called with no arguments. If the function
725returns nil, then no other program has provided such text, and the top
726of the Emacs kill ring should be used. If the function returns a
727string, that string should be put in the kill ring as the latest kill.")
728
729
730\f
731;;;; The kill ring data structure.
2076c87c
JB
732
733(defvar kill-ring nil
70e14c01
JB
734 "List of killed text sequences.
735Since the kill ring is supposed to interact nicely with cut-and-paste
736facilities offered by window systems, use of this variable should
737interact nicely with `interprogram-cut-function' and
738`interprogram-paste-function'. The functions `kill-new',
739`kill-append', and `current-kill' are supposed to implement this
740interaction; you may want to use them instead of manipulating the kill
741ring directly.")
2076c87c
JB
742
743(defconst kill-ring-max 30
744 "*Maximum length of kill ring before oldest elements are thrown away.")
745
746(defvar kill-ring-yank-pointer nil
747 "The tail of the kill ring whose car is the last thing yanked.")
748
70e14c01
JB
749(defun kill-new (string)
750 "Make STRING the latest kill in the kill ring.
751Set the kill-ring-yank pointer to point to it.
752If `interprogram-cut-function' is non-nil, apply it to STRING."
753 (setq kill-ring (cons string kill-ring))
754 (if (> (length kill-ring) kill-ring-max)
755 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
756 (setq kill-ring-yank-pointer kill-ring)
757 (if interprogram-cut-function
758 (funcall interprogram-cut-function string)))
759
2076c87c 760(defun kill-append (string before-p)
70e14c01
JB
761 "Append STRING to the end of the latest kill in the kill ring.
762If BEFORE-P is non-nil, prepend STRING to the kill.
763If 'interprogram-cut-function' is set, pass the resulting kill to
764it."
2076c87c
JB
765 (setcar kill-ring
766 (if before-p
767 (concat string (car kill-ring))
70e14c01
JB
768 (concat (car kill-ring) string)))
769 (if interprogram-cut-function
770 (funcall interprogram-cut-function (car kill-ring))))
771
772(defun current-kill (n &optional do-not-move)
773 "Rotate the yanking point by N places, and then return that kill.
774If N is zero, `interprogram-paste-function' is set, and calling it
775returns a string, then that string is added to the front of the
776kill ring and returned as the latest kill.
777If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
778yanking point; just return the Nth kill forward."
779 (let ((interprogram-paste (and (= n 0)
780 interprogram-paste-function
781 (funcall interprogram-paste-function))))
782 (if interprogram-paste
783 (progn
784 ;; Disable the interprogram cut function when we add the new
785 ;; text to the kill ring, so Emacs doesn't try to own the
786 ;; selection, with identical text.
787 (let ((interprogram-cut-function nil))
788 (kill-new interprogram-paste))
789 interprogram-paste)
790 (or kill-ring (error "Kill ring is empty"))
791 (let* ((length (length kill-ring))
792 (ARGth-kill-element
793 (nthcdr (% (+ n (- length (length kill-ring-yank-pointer)))
794 length)
795 kill-ring)))
796 (or do-not-move
797 (setq kill-ring-yank-pointer ARGth-kill-element))
798 (car ARGth-kill-element)))))
c88ab9ce 799
c88ab9ce 800
70e14c01
JB
801\f
802;;;; Commands for manipulating the kill ring.
c88ab9ce 803
2076c87c
JB
804(defun kill-region (beg end)
805 "Kill between point and mark.
806The text is deleted but saved in the kill ring.
807The command \\[yank] can retrieve it from there.
808\(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
809
810This is the primitive for programs to kill text (as opposed to deleting it).
811Supply two arguments, character numbers indicating the stretch of text
812 to be killed.
813Any command that calls this function is a \"kill command\".
814If the previous command was also a kill command,
815the text killed this time appends to the text killed last time
816to make one entry in the kill ring."
817 (interactive "r")
70e14c01
JB
818 (cond
819 (buffer-read-only
820 (copy-region-as-kill beg end))
821 ((not (or (eq buffer-undo-list t)
822 (eq last-command 'kill-region)
823 (eq beg end)))
824 ;; Don't let the undo list be truncated before we can even access it.
825 (let ((undo-high-threshold (+ (- (max beg end) (min beg end)) 100)))
826 (delete-region beg end)
827 ;; Take the same string recorded for undo
828 ;; and put it in the kill-ring.
829 (kill-new (car (car buffer-undo-list)))
830 (setq this-command 'kill-region)))
831 (t
2076c87c 832 (copy-region-as-kill beg end)
70e14c01 833 (delete-region beg end))))
2076c87c 834
2076c87c
JB
835(defun copy-region-as-kill (beg end)
836 "Save the region as if killed, but don't kill it.
46947372
JB
837If `interprogram-cut-function' is non-nil, also save the text for a window
838system cut and paste."
2076c87c
JB
839 (interactive "r")
840 (if (eq last-command 'kill-region)
841 (kill-append (buffer-substring beg end) (< end beg))
70e14c01
JB
842 (kill-new (buffer-substring beg end)))
843 (setq this-command 'kill-region)
2076c87c
JB
844 nil)
845
846(defun kill-ring-save (beg end)
847 "Save the region as if killed, but don't kill it."
848 (interactive "r")
849 (copy-region-as-kill beg end)
70e14c01
JB
850 (save-excursion
851 (let ((other-end (if (= (point) beg) end beg)))
852 (if (pos-visible-in-window-p other-end (selected-window))
853 (progn
854 (goto-char other-end)
855 (sit-for 1))
856 (let* ((killed-text (current-kill 0))
857 (message-len (min (length killed-text) 40)))
858 (message
859 (if (= (point) beg)
860 (format "Killed until \"%s\""
861 (substring killed-text (- message-len)))
862 (format "Killed from \"%s\""
863 (substring killed-text 0 message-len)))))))))
2076c87c
JB
864
865(defun append-next-kill ()
866 "Cause following command, if kill, to append to previous kill."
867 (interactive)
868 (if (interactive-p)
869 (progn
870 (setq this-command 'kill-region)
871 (message "If the next command is a kill, it will append"))
872 (setq last-command 'kill-region)))
873
2076c87c
JB
874(defun yank-pop (arg)
875 "Replace just-yanked stretch of killed-text with a different stretch.
876This command is allowed only immediately after a yank or a yank-pop.
877At such a time, the region contains a stretch of reinserted
878previously-killed text. yank-pop deletes that text and inserts in its
879place a different stretch of killed text.
880
881With no argument, the previous kill is inserted.
882With argument n, the n'th previous kill is inserted.
883If n is negative, this is a more recent kill.
884
885The sequence of kills wraps around, so that after the oldest one
886comes the newest one."
887 (interactive "*p")
888 (if (not (eq last-command 'yank))
889 (error "Previous command was not a yank"))
890 (setq this-command 'yank)
891 (let ((before (< (point) (mark))))
892 (delete-region (point) (mark))
2076c87c 893 (set-mark (point))
70e14c01 894 (insert (current-kill arg))
2076c87c
JB
895 (if before (exchange-point-and-mark))))
896
897(defun yank (&optional arg)
898 "Reinsert the last stretch of killed text.
899More precisely, reinsert the stretch of killed text most recently
900killed OR yanked.
901With just C-U as argument, same but put point in front (and mark at end).
902With argument n, reinsert the nth most recently killed stretch of killed
903text.
904See also the command \\[yank-pop]."
905 (interactive "*P")
2076c87c 906 (push-mark (point))
70e14c01
JB
907 (insert (current-kill (cond
908 ((listp arg) 0)
909 ((eq arg '-) -1)
910 (t (1- arg)))))
2076c87c
JB
911 (if (consp arg)
912 (exchange-point-and-mark)))
70e14c01
JB
913
914(defun rotate-yank-pointer (arg)
915 "Rotate the yanking point in the kill ring.
916With argument, rotate that many kills forward (or backward, if negative)."
917 (interactive "p")
918 (current-kill arg))
919
2076c87c
JB
920\f
921(defun insert-buffer (buffer)
922 "Insert after point the contents of BUFFER.
923Puts mark after the inserted text.
924BUFFER may be a buffer or a buffer name."
925 (interactive (list (read-buffer "Insert buffer: " (other-buffer) t)))
926 (or (bufferp buffer)
927 (setq buffer (get-buffer buffer)))
928 (let (start end newmark)
929 (save-excursion
930 (save-excursion
931 (set-buffer buffer)
932 (setq start (point-min) end (point-max)))
933 (insert-buffer-substring buffer start end)
934 (setq newmark (point)))
935 (push-mark newmark)))
936
937(defun append-to-buffer (buffer start end)
938 "Append to specified buffer the text of the region.
939It is inserted into that buffer before its point.
940
941When calling from a program, give three arguments:
942BUFFER (or buffer name), START and END.
943START and END specify the portion of the current buffer to be copied."
70e14c01
JB
944 (interactive
945 (list (read-buffer "Append to buffer: " (other-buffer nil t) t)))
2076c87c
JB
946 (let ((oldbuf (current-buffer)))
947 (save-excursion
948 (set-buffer (get-buffer-create buffer))
949 (insert-buffer-substring oldbuf start end))))
950
951(defun prepend-to-buffer (buffer start end)
952 "Prepend to specified buffer the text of the region.
953It is inserted into that buffer after its point.
954
955When calling from a program, give three arguments:
956BUFFER (or buffer name), START and END.
957START and END specify the portion of the current buffer to be copied."
958 (interactive "BPrepend to buffer: \nr")
959 (let ((oldbuf (current-buffer)))
960 (save-excursion
961 (set-buffer (get-buffer-create buffer))
962 (save-excursion
963 (insert-buffer-substring oldbuf start end)))))
964
965(defun copy-to-buffer (buffer start end)
966 "Copy to specified buffer the text of the region.
967It is inserted into that buffer, replacing existing text there.
968
969When calling from a program, give three arguments:
970BUFFER (or buffer name), START and END.
971START and END specify the portion of the current buffer to be copied."
972 (interactive "BCopy to buffer: \nr")
973 (let ((oldbuf (current-buffer)))
974 (save-excursion
975 (set-buffer (get-buffer-create buffer))
976 (erase-buffer)
977 (save-excursion
978 (insert-buffer-substring oldbuf start end)))))
979\f
980(defun mark ()
981 "Return this buffer's mark value as integer, or nil if no mark.
982If you are using this in an editing command, you are most likely making
983a mistake; see the documentation of `set-mark'."
984 (marker-position (mark-marker)))
985
986(defun set-mark (pos)
987 "Set this buffer's mark to POS. Don't use this function!
988That is to say, don't use this function unless you want
989the user to see that the mark has moved, and you want the previous
990mark position to be lost.
991
992Normally, when a new mark is set, the old one should go on the stack.
993This is why most applications should use push-mark, not set-mark.
994
995Novice emacs-lisp programmers often try to use the mark for the wrong
996purposes. The mark saves a location for the user's convenience.
997Most editing commands should not alter the mark.
998To remember a location for internal use in the Lisp program,
999store it in a Lisp variable. Example:
1000
1001 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1002
1003 (set-marker (mark-marker) pos (current-buffer)))
1004
1005(defvar mark-ring nil
1006 "The list of saved former marks of the current buffer,
1007most recent first.")
1008(make-variable-buffer-local 'mark-ring)
1009
1010(defconst mark-ring-max 16
1011 "*Maximum size of mark ring. Start discarding off end if gets this big.")
1012
1013(defun set-mark-command (arg)
1014 "Set mark at where point is, or jump to mark.
1015With no prefix argument, set mark, and push previous mark on mark ring.
1016With argument, jump to mark, and pop into mark off the mark ring.
1017
1018Novice emacs-lisp programmers often try to use the mark for the wrong
1019purposes. See the documentation of `set-mark' for more information."
1020 (interactive "P")
1021 (if (null arg)
1022 (push-mark)
1023 (if (null (mark))
1024 (error "No mark set in this buffer")
1025 (goto-char (mark))
1026 (pop-mark))))
1027
1028(defun push-mark (&optional location nomsg)
1029 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1030Displays \"Mark set\" unless the optional second arg NOMSG is non-nil.
1031
1032Novice emacs-lisp programmers often try to use the mark for the wrong
1033purposes. See the documentation of `set-mark' for more information."
1034 (if (null (mark))
1035 nil
1036 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1037 (if (> (length mark-ring) mark-ring-max)
1038 (progn
1039 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1040 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
1041 (set-mark (or location (point)))
1042 (or nomsg executing-macro (> (minibuffer-depth) 0)
1043 (message "Mark set"))
1044 nil)
1045
1046(defun pop-mark ()
1047 "Pop off mark ring into the buffer's actual mark.
1048Does not set point. Does nothing if mark ring is empty."
1049 (if mark-ring
1050 (progn
1051 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
1052 (set-mark (+ 0 (car mark-ring)))
1053 (move-marker (car mark-ring) nil)
1054 (if (null (mark)) (ding))
1055 (setq mark-ring (cdr mark-ring)))))
1056
1057(fset 'exchange-dot-and-mark 'exchange-point-and-mark)
1058(defun exchange-point-and-mark ()
1059 "Put the mark where point is now, and point where the mark is now."
1060 (interactive nil)
1061 (let ((omark (mark)))
1062 (if (null omark)
1063 (error "No mark set in this buffer"))
1064 (set-mark (point))
1065 (goto-char omark)
1066 nil))
1067\f
1068(defun next-line (arg)
1069 "Move cursor vertically down ARG lines.
1070If there is no character in the target line exactly under the current column,
1071the cursor is positioned after the character in that line which spans this
1072column, or at the end of the line if it is not long enough.
1073If there is no line in the buffer after this one,
1074a newline character is inserted to create a line
1075and the cursor moves to that line.
1076
1077The command \\[set-goal-column] can be used to create
1078a semipermanent goal column to which this command always moves.
1079Then it does not try to move vertically. This goal column is stored
1080in `goal-column', which is nil when there is none.
1081
1082If you are thinking of using this in a Lisp program, consider
1083using `forward-line' instead. It is usually easier to use
1084and more reliable (no dependence on goal column, etc.)."
1085 (interactive "p")
1086 (if (= arg 1)
1087 (let ((opoint (point)))
1088 (forward-line 1)
1089 (if (or (= opoint (point))
1090 (not (eq (preceding-char) ?\n)))
1091 (insert ?\n)
1092 (goto-char opoint)
1093 (line-move arg)))
1094 (line-move arg))
1095 nil)
1096
1097(defun previous-line (arg)
1098 "Move cursor vertically up ARG lines.
1099If there is no character in the target line exactly over the current column,
1100the cursor is positioned after the character in that line which spans this
1101column, or at the end of the line if it is not long enough.
1102
1103The command \\[set-goal-column] can be used to create
1104a semipermanent goal column to which this command always moves.
1105Then it does not try to move vertically.
1106
1107If you are thinking of using this in a Lisp program, consider using
1108`forward-line' with negative argument instead.. It is usually easier
1109to use and more reliable (no dependence on goal column, etc.)."
1110 (interactive "p")
1111 (line-move (- arg))
1112 nil)
1113
1114(defconst track-eol nil
1115 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1116This means moving to the end of each line moved onto.
1117The beginning of a blank line does not count as the end of a line.")
1118
1119(make-variable-buffer-local
1120 (defvar goal-column nil
1121 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."))
1122
1123(defvar temporary-goal-column 0
1124 "Current goal column for vertical motion.
1125It is the column where point was
1126at the start of current run of vertical motion commands.
c637ae6f 1127When the `track-eol' feature is doing its job, the value is 9999.")
2076c87c
JB
1128
1129(defun line-move (arg)
1130 (if (not (or (eq last-command 'next-line)
1131 (eq last-command 'previous-line)))
1132 (setq temporary-goal-column
1133 (if (and track-eol (eolp)
1134 ;; Don't count beg of empty line as end of line
1135 ;; unless we just did explicit end-of-line.
1136 (or (not (bolp)) (eq last-command 'end-of-line)))
1137 9999
1138 (current-column))))
1139 (if (not (integerp selective-display))
1140 (forward-line arg)
1141 ;; Move by arg lines, but ignore invisible ones.
1142 (while (> arg 0)
1143 (vertical-motion 1)
1144 (forward-char -1)
1145 (forward-line 1)
1146 (setq arg (1- arg)))
1147 (while (< arg 0)
1148 (vertical-motion -1)
1149 (beginning-of-line)
1150 (setq arg (1+ arg))))
1151 (move-to-column (or goal-column temporary-goal-column))
1152 nil)
1153
1154
1155(defun set-goal-column (arg)
1156 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1157Those commands will move to this position in the line moved to
1158rather than trying to keep the same horizontal position.
1159With a non-nil argument, clears out the goal column
1160so that \\[next-line] and \\[previous-line] resume vertical motion."
1161 (interactive "P")
1162 (if arg
1163 (progn
1164 (setq goal-column nil)
1165 (message "No goal column"))
1166 (setq goal-column (current-column))
1167 (message (substitute-command-keys
1168 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
1169 goal-column))
1170 nil)
1171\f
1172(defun transpose-chars (arg)
1173 "Interchange characters around point, moving forward one character.
1174With prefix arg ARG, effect is to take character before point
1175and drag it forward past ARG other characters (backward if ARG negative).
1176If no argument and at end of line, the previous two chars are exchanged."
1177 (interactive "*P")
1178 (and (null arg) (eolp) (forward-char -1))
1179 (transpose-subr 'forward-char (prefix-numeric-value arg)))
1180
1181(defun transpose-words (arg)
1182 "Interchange words around point, leaving point at end of them.
1183With prefix arg ARG, effect is to take word before or around point
1184and drag it forward past ARG other words (backward if ARG negative).
1185If ARG is zero, the words around or after point and around or after mark
1186are interchanged."
1187 (interactive "*p")
1188 (transpose-subr 'forward-word arg))
1189
1190(defun transpose-sexps (arg)
1191 "Like \\[transpose-words] but applies to sexps.
1192Does not work on a sexp that point is in the middle of
1193if it is a list or string."
1194 (interactive "*p")
1195 (transpose-subr 'forward-sexp arg))
1196
1197(defun transpose-lines (arg)
1198 "Exchange current line and previous line, leaving point after both.
1199With argument ARG, takes previous line and moves it past ARG lines.
1200With argument 0, interchanges line point is in with line mark is in."
1201 (interactive "*p")
1202 (transpose-subr (function
1203 (lambda (arg)
1204 (if (= arg 1)
1205 (progn
1206 ;; Move forward over a line,
1207 ;; but create a newline if none exists yet.
1208 (end-of-line)
1209 (if (eobp)
1210 (newline)
1211 (forward-char 1)))
1212 (forward-line arg))))
1213 arg))
1214
1215(defun transpose-subr (mover arg)
1216 (let (start1 end1 start2 end2)
1217 (if (= arg 0)
1218 (progn
1219 (save-excursion
1220 (funcall mover 1)
1221 (setq end2 (point))
1222 (funcall mover -1)
1223 (setq start2 (point))
1224 (goto-char (mark))
1225 (funcall mover 1)
1226 (setq end1 (point))
1227 (funcall mover -1)
1228 (setq start1 (point))
1229 (transpose-subr-1))
1230 (exchange-point-and-mark)))
1231 (while (> arg 0)
1232 (funcall mover -1)
1233 (setq start1 (point))
1234 (funcall mover 1)
1235 (setq end1 (point))
1236 (funcall mover 1)
1237 (setq end2 (point))
1238 (funcall mover -1)
1239 (setq start2 (point))
1240 (transpose-subr-1)
1241 (goto-char end2)
1242 (setq arg (1- arg)))
1243 (while (< arg 0)
1244 (funcall mover -1)
1245 (setq start2 (point))
1246 (funcall mover -1)
1247 (setq start1 (point))
1248 (funcall mover 1)
1249 (setq end1 (point))
1250 (funcall mover 1)
1251 (setq end2 (point))
1252 (transpose-subr-1)
1253 (setq arg (1+ arg)))))
1254
1255(defun transpose-subr-1 ()
1256 (if (> (min end1 end2) (max start1 start2))
1257 (error "Don't have two things to transpose"))
1258 (let ((word1 (buffer-substring start1 end1))
1259 (word2 (buffer-substring start2 end2)))
1260 (delete-region start2 end2)
1261 (goto-char start2)
1262 (insert word1)
1263 (goto-char (if (< start1 start2) start1
1264 (+ start1 (- (length word1) (length word2)))))
1265 (delete-char (length word1))
1266 (insert word2)))
1267\f
1268(defconst comment-column 32
1269 "*Column to indent right-margin comments to.
1270Setting this variable automatically makes it local to the current buffer.")
1271(make-variable-buffer-local 'comment-column)
1272
1273(defconst comment-start nil
1274 "*String to insert to start a new comment, or nil if no comment syntax defined.")
1275
1276(defconst comment-start-skip nil
1277 "*Regexp to match the start of a comment plus everything up to its body.
1278If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1279at the place matched by the close of the first pair.")
1280
1281(defconst comment-end ""
1282 "*String to insert to end a new comment.
1283Should be an empty string if comments are terminated by end-of-line.")
1284
1285(defconst comment-indent-hook
1286 '(lambda () comment-column)
1287 "Function to compute desired indentation for a comment.
1288This function is called with no args with point at the beginning of
1289the comment's starting delimiter.")
1290
1291(defun indent-for-comment ()
1292 "Indent this line's comment to comment column, or insert an empty comment."
1293 (interactive "*")
1294 (beginning-of-line 1)
1295 (if (null comment-start)
1296 (error "No comment syntax defined")
1297 (let* ((eolpos (save-excursion (end-of-line) (point)))
1298 cpos indent begpos)
1299 (if (re-search-forward comment-start-skip eolpos 'move)
1300 (progn (setq cpos (point-marker))
1301 ;; Find the start of the comment delimiter.
1302 ;; If there were paren-pairs in comment-start-skip,
1303 ;; position at the end of the first pair.
1304 (if (match-end 1)
1305 (goto-char (match-end 1))
1306 ;; If comment-start-skip matched a string with internal
1307 ;; whitespace (not final whitespace) then the delimiter
1308 ;; start at the end of that whitespace.
1309 ;; Otherwise, it starts at the beginning of what was matched.
1310 (skip-chars-backward " \t" (match-beginning 0))
1311 (skip-chars-backward "^ \t" (match-beginning 0)))))
1312 (setq begpos (point))
1313 ;; Compute desired indent.
1314 (if (= (current-column)
1315 (setq indent (funcall comment-indent-hook)))
1316 (goto-char begpos)
1317 ;; If that's different from current, change it.
1318 (skip-chars-backward " \t")
1319 (delete-region (point) begpos)
1320 (indent-to indent))
1321 ;; An existing comment?
1322 (if cpos
1323 (progn (goto-char cpos)
1324 (set-marker cpos nil))
1325 ;; No, insert one.
1326 (insert comment-start)
1327 (save-excursion
1328 (insert comment-end))))))
1329
1330(defun set-comment-column (arg)
1331 "Set the comment column based on point.
1332With no arg, set the comment column to the current column.
1333With just minus as arg, kill any comment on this line.
1334With any other arg, set comment column to indentation of the previous comment
1335 and then align or create a comment on this line at that column."
1336 (interactive "P")
1337 (if (eq arg '-)
1338 (kill-comment nil)
1339 (if arg
1340 (progn
1341 (save-excursion
1342 (beginning-of-line)
1343 (re-search-backward comment-start-skip)
1344 (beginning-of-line)
1345 (re-search-forward comment-start-skip)
1346 (goto-char (match-beginning 0))
1347 (setq comment-column (current-column))
1348 (message "Comment column set to %d" comment-column))
1349 (indent-for-comment))
1350 (setq comment-column (current-column))
1351 (message "Comment column set to %d" comment-column))))
1352
1353(defun kill-comment (arg)
1354 "Kill the comment on this line, if any.
1355With argument, kill comments on that many lines starting with this one."
1356 ;; this function loses in a lot of situations. it incorrectly recognises
1357 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
1358 ;; with multi-line comments, can kill extra whitespace if comment wasn't
1359 ;; through end-of-line, et cetera.
1360 (interactive "P")
1361 (or comment-start-skip (error "No comment syntax defined"))
1362 (let ((count (prefix-numeric-value arg)) endc)
1363 (while (> count 0)
1364 (save-excursion
1365 (end-of-line)
1366 (setq endc (point))
1367 (beginning-of-line)
1368 (and (string< "" comment-end)
1369 (setq endc
1370 (progn
1371 (re-search-forward (regexp-quote comment-end) endc 'move)
1372 (skip-chars-forward " \t")
1373 (point))))
1374 (beginning-of-line)
1375 (if (re-search-forward comment-start-skip endc t)
1376 (progn
1377 (goto-char (match-beginning 0))
1378 (skip-chars-backward " \t")
1379 (kill-region (point) endc)
1380 ;; to catch comments a line beginnings
1381 (indent-according-to-mode))))
1382 (if arg (forward-line 1))
1383 (setq count (1- count)))))
1384
1385(defun comment-region (beg end &optional arg)
1386 "Comment the region; third arg numeric means use ARG comment characters.
1387If ARG is negative, delete that many comment characters instead.
1388Comments are terminated on each line, even for syntax in which newline does
1389not end the comment. Blank lines do not get comments."
1390 ;; if someone wants it to only put a comment-start at the beginning and
1391 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
1392 ;; is easy enough. No option is made here for other than commenting
1393 ;; every line.
1394 (interactive "r\np")
1395 (or comment-start (error "No comment syntax is defined"))
1396 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
1397 (save-excursion
1398 (save-restriction
1399 (let ((cs comment-start) (ce comment-end))
1400 (cond ((not arg) (setq arg 1))
1401 ((> arg 1)
1402 (while (> (setq arg (1- arg)) 0)
1403 (setq cs (concat cs comment-start)
1404 ce (concat ce comment-end)))))
1405 (narrow-to-region beg end)
1406 (goto-char beg)
1407 (while (not (eobp))
1408 (if (< arg 0)
1409 (let ((count arg))
1410 (while (and (> 1 (setq count (1+ count)))
1411 (looking-at (regexp-quote cs)))
1412 (delete-char (length cs)))
1413 (if (string= "" ce) ()
1414 (setq count arg)
1415 (while (> 1 (setq count (1+ count)))
1416 (end-of-line)
1417 ;; this is questionable if comment-end ends in whitespace
1418 ;; that is pretty brain-damaged though
1419 (skip-chars-backward " \t")
1420 (backward-char (length ce))
1421 (if (looking-at (regexp-quote ce))
1422 (delete-char (length ce))))))
1423 (if (looking-at "[ \t]*$") ()
1424 (insert cs)
1425 (if (string= "" ce) ()
1426 (end-of-line)
1427 (insert ce)))
1428 (search-forward "\n" nil 'move)))))))
1429\f
1430(defun backward-word (arg)
1431 "Move backward until encountering the end of a word.
1432With argument, do this that many times.
1433In programs, it is faster to call forward-word with negative arg."
1434 (interactive "p")
1435 (forward-word (- arg)))
1436
1437(defun mark-word (arg)
1438 "Set mark arg words away from point."
1439 (interactive "p")
1440 (push-mark
1441 (save-excursion
1442 (forward-word arg)
1443 (point))))
1444
1445(defun kill-word (arg)
1446 "Kill characters forward until encountering the end of a word.
1447With argument, do this that many times."
1448 (interactive "p")
1449 (kill-region (point) (progn (forward-word arg) (point))))
1450
1451(defun backward-kill-word (arg)
1452 "Kill characters backward until encountering the end of a word.
1453With argument, do this that many times."
1454 (interactive "p")
1455 (kill-word (- arg)))
1456\f
1457(defconst fill-prefix nil
1458 "*String for filling to insert at front of new line, or nil for none.
1459Setting this variable automatically makes it local to the current buffer.")
1460(make-variable-buffer-local 'fill-prefix)
1461
1462(defconst auto-fill-inhibit-regexp nil
1463 "*Regexp to match lines which should not be auto-filled.")
1464
1465(defun do-auto-fill ()
1466 (let (give-up)
1467 (or (and auto-fill-inhibit-regexp
1468 (save-excursion (beginning-of-line)
1469 (looking-at auto-fill-inhibit-regexp)))
1470 (while (and (not give-up) (> (current-column) fill-column))
1471 (let ((fill-point
1472 (let ((opoint (point)))
1473 (save-excursion
1474 (move-to-column (1+ fill-column))
1475 (skip-chars-backward "^ \t\n")
1476 (if (bolp)
1477 (re-search-forward "[ \t]" opoint t))
1478 (skip-chars-backward " \t")
1479 (point)))))
1480 ;; If there is a space on the line before fill-point,
1481 ;; and nonspaces precede it, break the line there.
1482 (if (save-excursion
1483 (goto-char fill-point)
1484 (not (bolp)))
1485 ;; If point is at the fill-point, do not `save-excursion'.
1486 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
1487 ;; point will end up before it rather than after it.
1488 (if (save-excursion
1489 (skip-chars-backward " \t")
1490 (= (point) fill-point))
1491 (indent-new-comment-line)
1492 (save-excursion
1493 (goto-char fill-point)
1494 (indent-new-comment-line)))
1495 ;; No place to break => stop trying.
1496 (setq give-up t)))))))
1497
1498(defconst comment-multi-line nil
1499 "*Non-nil means \\[indent-new-comment-line] should continue same comment
c88ab9ce
ER
1500on new line, with no new terminator or starter.
1501This is obsolete because you might as well use \\[newline-and-indent].")
2076c87c
JB
1502
1503(defun indent-new-comment-line ()
1504 "Break line at point and indent, continuing comment if presently within one.
c88ab9ce
ER
1505The body of the continued comment is indented under the previous comment line.
1506
1507This command is intended for styles where you write a comment per line,
1508starting a new comment (and terminating it if necessary) on each line.
1509If you want to continue one comment across several lines, use \\[newline-and-indent]."
2076c87c
JB
1510 (interactive "*")
1511 (let (comcol comstart)
1512 (skip-chars-backward " \t")
1513 (delete-region (point)
1514 (progn (skip-chars-forward " \t")
1515 (point)))
1516 (insert ?\n)
c88ab9ce
ER
1517 (if (not comment-multi-line)
1518 (save-excursion
1519 (if (and comment-start-skip
1520 (let ((opoint (point)))
1521 (forward-line -1)
1522 (re-search-forward comment-start-skip opoint t)))
1523 ;; The old line is a comment.
1524 ;; Set WIN to the pos of the comment-start.
1525 ;; But if the comment is empty, look at preceding lines
1526 ;; to find one that has a nonempty comment.
1527 (let ((win (match-beginning 0)))
1528 (while (and (eolp) (not (bobp))
1529 (let (opoint)
1530 (beginning-of-line)
1531 (setq opoint (point))
1532 (forward-line -1)
1533 (re-search-forward comment-start-skip opoint t)))
1534 (setq win (match-beginning 0)))
1535 ;; Indent this line like what we found.
1536 (goto-char win)
1537 (setq comcol (current-column))
1538 (setq comstart (buffer-substring (point) (match-end 0)))))))
2076c87c
JB
1539 (if comcol
1540 (let ((comment-column comcol)
1541 (comment-start comstart)
1542 (comment-end comment-end))
1543 (and comment-end (not (equal comment-end ""))
c88ab9ce 1544; (if (not comment-multi-line)
2076c87c
JB
1545 (progn
1546 (forward-char -1)
1547 (insert comment-end)
1548 (forward-char 1))
c88ab9ce
ER
1549; (setq comment-column (+ comment-column (length comment-start))
1550; comment-start "")
1551; )
1552 )
2076c87c
JB
1553 (if (not (eolp))
1554 (setq comment-end ""))
1555 (insert ?\n)
1556 (forward-char -1)
1557 (indent-for-comment)
1558 (save-excursion
1559 ;; Make sure we delete the newline inserted above.
1560 (end-of-line)
1561 (delete-char 1)))
1562 (if fill-prefix
1563 (insert fill-prefix)
1564 (indent-according-to-mode)))))
1565
1566(defun auto-fill-mode (&optional arg)
1567 "Toggle auto-fill mode.
1568With arg, turn auto-fill mode on if and only if arg is positive.
1569In auto-fill mode, inserting a space at a column beyond fill-column
1570automatically breaks the line at a previous space."
1571 (interactive "P")
1572 (prog1 (setq auto-fill-function
1573 (if (if (null arg)
1574 (not auto-fill-function)
1575 (> (prefix-numeric-value arg) 0))
1576 'do-auto-fill
1577 nil))
1578 ;; update mode-line
1579 (set-buffer-modified-p (buffer-modified-p))))
1580
1581(defun turn-on-auto-fill ()
1582 "Unconditionally turn on Auto Fill mode."
1583 (auto-fill-mode 1))
1584
1585(defun set-fill-column (arg)
1586 "Set fill-column to current column, or to argument if given.
1587fill-column's value is separate for each buffer."
1588 (interactive "P")
1589 (setq fill-column (if (integerp arg) arg (current-column)))
1590 (message "fill-column set to %d" fill-column))
1591\f
1592(defun set-selective-display (arg)
1593 "Set selective-display to ARG; clear it if no arg.
1594When selective-display is a number > 0,
1595lines whose indentation is >= selective-display are not displayed.
1596selective-display's value is separate for each buffer."
1597 (interactive "P")
1598 (if (eq selective-display t)
1599 (error "selective-display already in use for marked lines"))
c88ab9ce
ER
1600 (let ((current-vpos
1601 (save-restriction
1602 (narrow-to-region (point-min) (point))
1603 (goto-char (window-start))
1604 (vertical-motion (window-height)))))
1605 (setq selective-display
1606 (and arg (prefix-numeric-value arg)))
1607 (recenter current-vpos))
2076c87c
JB
1608 (set-window-start (selected-window) (window-start (selected-window)))
1609 (princ "selective-display set to " t)
1610 (prin1 selective-display t)
1611 (princ "." t))
1612
1613(defun overwrite-mode (arg)
1614 "Toggle overwrite mode.
1615With arg, turn overwrite mode on iff arg is positive.
1616In overwrite mode, printing characters typed in replace existing text
1617on a one-for-one basis, rather than pushing it to the right."
1618 (interactive "P")
1619 (setq overwrite-mode
1620 (if (null arg) (not overwrite-mode)
1621 (> (prefix-numeric-value arg) 0)))
1622 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
1623\f
1624(defvar blink-matching-paren t
1625 "*Non-nil means show matching open-paren when close-paren is inserted.")
1626
1627(defconst blink-matching-paren-distance 4000
1628 "*If non-nil, is maximum distance to search for matching open-paren
1629when close-paren is inserted.")
1630
1631(defun blink-matching-open ()
1632 "Move cursor momentarily to the beginning of the sexp before point."
1633 (interactive)
1634 (and (> (point) (1+ (point-min)))
1635 (/= (char-syntax (char-after (- (point) 2))) ?\\ )
1636 blink-matching-paren
1637 (let* ((oldpos (point))
1638 (blinkpos)
1639 (mismatch))
1640 (save-excursion
1641 (save-restriction
1642 (if blink-matching-paren-distance
1643 (narrow-to-region (max (point-min)
1644 (- (point) blink-matching-paren-distance))
1645 oldpos))
1646 (condition-case ()
1647 (setq blinkpos (scan-sexps oldpos -1))
1648 (error nil)))
1649 (and blinkpos (/= (char-syntax (char-after blinkpos))
1650 ?\$)
1651 (setq mismatch
1652 (/= (char-after (1- oldpos))
1653 (logand (lsh (aref (syntax-table)
1654 (char-after blinkpos))
1655 -8)
1656 255))))
1657 (if mismatch (setq blinkpos nil))
1658 (if blinkpos
1659 (progn
1660 (goto-char blinkpos)
1661 (if (pos-visible-in-window-p)
1662 (sit-for 1)
1663 (goto-char blinkpos)
1664 (message
1665 "Matches %s"
1666 (if (save-excursion
1667 (skip-chars-backward " \t")
1668 (not (bolp)))
1669 (buffer-substring (progn (beginning-of-line) (point))
1670 (1+ blinkpos))
1671 (buffer-substring blinkpos
1672 (progn
1673 (forward-char 1)
1674 (skip-chars-forward "\n \t")
1675 (end-of-line)
1676 (point)))))))
1677 (cond (mismatch
1678 (message "Mismatched parentheses"))
1679 ((not blink-matching-paren-distance)
1680 (message "Unmatched parenthesis"))))))))
1681
1682;Turned off because it makes dbx bomb out.
1683(setq blink-paren-function 'blink-matching-open)
1684
1685; this is just something for the luser to see in a keymap -- this is not
1686; how quitting works normally!
1687(defun keyboard-quit ()
1688 "Signal a quit condition."
1689 (interactive)
1690 (signal 'quit nil))
1691
1692(define-key global-map "\C-g" 'keyboard-quit)
1693\f
1694(defun set-variable (var val)
1695 "Set VARIABLE to VALUE. VALUE is a Lisp object.
1696When using this interactively, supply a Lisp expression for VALUE.
1697If you want VALUE to be a string, you must surround it with doublequotes."
1698 (interactive
1699 (let* ((var (read-variable "Set variable: "))
1700 (minibuffer-help-form
1701 '(funcall myhelp))
1702 (myhelp
1703 (function
1704 (lambda ()
1705 (with-output-to-temp-buffer "*Help*"
1706 (prin1 var)
1707 (princ "\nDocumentation:\n")
1708 (princ (substring (documentation-property var 'variable-documentation)
1709 1))
1710 (if (boundp var)
1711 (let ((print-length 20))
1712 (princ "\n\nCurrent value: ")
1713 (prin1 (symbol-value var))))
1714 nil)))))
1715 (list var
1716 (eval-minibuffer (format "Set %s to value: " var)))))
1717 (set var val))
1718\f
1719;These commands are defined in editfns.c
1720;but they are not assigned to keys there.
1721(put 'narrow-to-region 'disabled t)
1722(define-key ctl-x-map "n" 'narrow-to-region)
1723(define-key ctl-x-map "w" 'widen)
1724
1725(define-key global-map "\C-j" 'newline-and-indent)
1726(define-key global-map "\C-m" 'newline)
1727(define-key global-map "\C-o" 'open-line)
1728(define-key esc-map "\C-o" 'split-line)
1729(define-key global-map "\C-q" 'quoted-insert)
1730(define-key esc-map "^" 'delete-indentation)
1731(define-key esc-map "\\" 'delete-horizontal-space)
1732(define-key esc-map "m" 'back-to-indentation)
1733(define-key ctl-x-map "\C-o" 'delete-blank-lines)
1734(define-key esc-map " " 'just-one-space)
1735(define-key esc-map "z" 'zap-to-char)
1736(define-key esc-map "=" 'count-lines-region)
1737(define-key ctl-x-map "=" 'what-cursor-position)
1738(define-key esc-map "\e" 'eval-expression)
1739(define-key ctl-x-map "\e" 'repeat-complex-command)
1740(define-key ctl-x-map "u" 'advertised-undo)
1741(define-key global-map "\C-_" 'undo)
1742(define-key esc-map "!" 'shell-command)
1743(define-key esc-map "|" 'shell-command-on-region)
1744
1745(define-key global-map "\C-u" 'universal-argument)
1746(let ((i ?0))
1747 (while (<= i ?9)
1748 (define-key esc-map (char-to-string i) 'digit-argument)
1749 (setq i (1+ i))))
1750(define-key esc-map "-" 'negative-argument)
1751
1752(define-key global-map "\C-k" 'kill-line)
1753(define-key global-map "\C-w" 'kill-region)
1754(define-key esc-map "w" 'kill-ring-save)
1755(define-key esc-map "\C-w" 'append-next-kill)
1756(define-key global-map "\C-y" 'yank)
1757(define-key esc-map "y" 'yank-pop)
1758
1759(define-key ctl-x-map "a" 'append-to-buffer)
1760
1761(define-key global-map "\C-@" 'set-mark-command)
1762(define-key ctl-x-map "\C-x" 'exchange-point-and-mark)
1763
1764(define-key global-map "\C-n" 'next-line)
1765(define-key global-map "\C-p" 'previous-line)
1766(define-key ctl-x-map "\C-n" 'set-goal-column)
1767
c637ae6f
JB
1768(define-key global-map [up] 'previous-line)
1769(define-key global-map [down] 'next-line)
1770(define-key global-map [left] 'backward-char)
1771(define-key global-map [right] 'forward-char)
1772
2076c87c
JB
1773(define-key global-map "\C-t" 'transpose-chars)
1774(define-key esc-map "t" 'transpose-words)
1775(define-key esc-map "\C-t" 'transpose-sexps)
1776(define-key ctl-x-map "\C-t" 'transpose-lines)
1777
1778(define-key esc-map ";" 'indent-for-comment)
1779(define-key esc-map "j" 'indent-new-comment-line)
1780(define-key esc-map "\C-j" 'indent-new-comment-line)
1781(define-key ctl-x-map ";" 'set-comment-column)
1782(define-key ctl-x-map "f" 'set-fill-column)
1783(define-key ctl-x-map "$" 'set-selective-display)
1784
1785(define-key esc-map "@" 'mark-word)
1786(define-key esc-map "f" 'forward-word)
1787(define-key esc-map "b" 'backward-word)
1788(define-key esc-map "d" 'kill-word)
1789(define-key esc-map "\177" 'backward-kill-word)
1790
1791(define-key esc-map "<" 'beginning-of-buffer)
1792(define-key esc-map ">" 'end-of-buffer)
1793(define-key ctl-x-map "h" 'mark-whole-buffer)
1794(define-key esc-map "\\" 'delete-horizontal-space)
1795
1796(fset 'mode-specific-command-prefix (make-sparse-keymap))
1797(defconst mode-specific-map (symbol-function 'mode-specific-command-prefix)
1798 "Keymap for characters following C-c.")
1799(define-key global-map "\C-c" 'mode-specific-command-prefix)
c88ab9ce
ER
1800
1801;;; simple.el ends here