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
daa37602
JB
727string, that string should be put in the kill ring as the latest kill.
728
729Note that the function should return a string only if a program other
730than Emacs has provided a string for pasting; if Emacs provided the
731most recent string, the function should return nil. If it is
732difficult to tell whether Emacs or some other program provided the
733current string, it is probably good enough to return nil if the string
734is equal (according to `string=') to the last text Emacs provided.")
70e14c01
JB
735
736
737\f
738;;;; The kill ring data structure.
2076c87c
JB
739
740(defvar kill-ring nil
70e14c01
JB
741 "List of killed text sequences.
742Since the kill ring is supposed to interact nicely with cut-and-paste
743facilities offered by window systems, use of this variable should
744interact nicely with `interprogram-cut-function' and
745`interprogram-paste-function'. The functions `kill-new',
746`kill-append', and `current-kill' are supposed to implement this
747interaction; you may want to use them instead of manipulating the kill
748ring directly.")
2076c87c
JB
749
750(defconst kill-ring-max 30
751 "*Maximum length of kill ring before oldest elements are thrown away.")
752
753(defvar kill-ring-yank-pointer nil
754 "The tail of the kill ring whose car is the last thing yanked.")
755
70e14c01
JB
756(defun kill-new (string)
757 "Make STRING the latest kill in the kill ring.
758Set the kill-ring-yank pointer to point to it.
759If `interprogram-cut-function' is non-nil, apply it to STRING."
760 (setq kill-ring (cons string kill-ring))
761 (if (> (length kill-ring) kill-ring-max)
762 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
763 (setq kill-ring-yank-pointer kill-ring)
764 (if interprogram-cut-function
765 (funcall interprogram-cut-function string)))
766
2076c87c 767(defun kill-append (string before-p)
70e14c01
JB
768 "Append STRING to the end of the latest kill in the kill ring.
769If BEFORE-P is non-nil, prepend STRING to the kill.
770If 'interprogram-cut-function' is set, pass the resulting kill to
771it."
2076c87c
JB
772 (setcar kill-ring
773 (if before-p
774 (concat string (car kill-ring))
70e14c01
JB
775 (concat (car kill-ring) string)))
776 (if interprogram-cut-function
777 (funcall interprogram-cut-function (car kill-ring))))
778
779(defun current-kill (n &optional do-not-move)
780 "Rotate the yanking point by N places, and then return that kill.
781If N is zero, `interprogram-paste-function' is set, and calling it
782returns a string, then that string is added to the front of the
783kill ring and returned as the latest kill.
784If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
785yanking point; just return the Nth kill forward."
786 (let ((interprogram-paste (and (= n 0)
787 interprogram-paste-function
788 (funcall interprogram-paste-function))))
789 (if interprogram-paste
790 (progn
791 ;; Disable the interprogram cut function when we add the new
792 ;; text to the kill ring, so Emacs doesn't try to own the
793 ;; selection, with identical text.
794 (let ((interprogram-cut-function nil))
795 (kill-new interprogram-paste))
796 interprogram-paste)
797 (or kill-ring (error "Kill ring is empty"))
798 (let* ((length (length kill-ring))
799 (ARGth-kill-element
800 (nthcdr (% (+ n (- length (length kill-ring-yank-pointer)))
801 length)
802 kill-ring)))
803 (or do-not-move
804 (setq kill-ring-yank-pointer ARGth-kill-element))
805 (car ARGth-kill-element)))))
c88ab9ce 806
c88ab9ce 807
70e14c01
JB
808\f
809;;;; Commands for manipulating the kill ring.
c88ab9ce 810
2076c87c
JB
811(defun kill-region (beg end)
812 "Kill between point and mark.
813The text is deleted but saved in the kill ring.
814The command \\[yank] can retrieve it from there.
815\(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
816
817This is the primitive for programs to kill text (as opposed to deleting it).
818Supply two arguments, character numbers indicating the stretch of text
819 to be killed.
820Any command that calls this function is a \"kill command\".
821If the previous command was also a kill command,
822the text killed this time appends to the text killed last time
823to make one entry in the kill ring."
824 (interactive "r")
70e14c01
JB
825 (cond
826 (buffer-read-only
827 (copy-region-as-kill beg end))
828 ((not (or (eq buffer-undo-list t)
829 (eq last-command 'kill-region)
830 (eq beg end)))
831 ;; Don't let the undo list be truncated before we can even access it.
109d300c 832 (let ((undo-strong-limit (+ (- (max beg end) (min beg end)) 100)))
70e14c01
JB
833 (delete-region beg end)
834 ;; Take the same string recorded for undo
835 ;; and put it in the kill-ring.
836 (kill-new (car (car buffer-undo-list)))
837 (setq this-command 'kill-region)))
838 (t
2076c87c 839 (copy-region-as-kill beg end)
70e14c01 840 (delete-region beg end))))
2076c87c 841
2076c87c
JB
842(defun copy-region-as-kill (beg end)
843 "Save the region as if killed, but don't kill it.
46947372
JB
844If `interprogram-cut-function' is non-nil, also save the text for a window
845system cut and paste."
2076c87c
JB
846 (interactive "r")
847 (if (eq last-command 'kill-region)
848 (kill-append (buffer-substring beg end) (< end beg))
70e14c01
JB
849 (kill-new (buffer-substring beg end)))
850 (setq this-command 'kill-region)
2076c87c
JB
851 nil)
852
853(defun kill-ring-save (beg end)
854 "Save the region as if killed, but don't kill it."
855 (interactive "r")
856 (copy-region-as-kill beg end)
70e14c01
JB
857 (save-excursion
858 (let ((other-end (if (= (point) beg) end beg)))
859 (if (pos-visible-in-window-p other-end (selected-window))
860 (progn
861 (goto-char other-end)
862 (sit-for 1))
863 (let* ((killed-text (current-kill 0))
864 (message-len (min (length killed-text) 40)))
865 (message
866 (if (= (point) beg)
867 (format "Killed until \"%s\""
868 (substring killed-text (- message-len)))
869 (format "Killed from \"%s\""
870 (substring killed-text 0 message-len)))))))))
2076c87c
JB
871
872(defun append-next-kill ()
873 "Cause following command, if kill, to append to previous kill."
874 (interactive)
875 (if (interactive-p)
876 (progn
877 (setq this-command 'kill-region)
878 (message "If the next command is a kill, it will append"))
879 (setq last-command 'kill-region)))
880
2076c87c
JB
881(defun yank-pop (arg)
882 "Replace just-yanked stretch of killed-text with a different stretch.
883This command is allowed only immediately after a yank or a yank-pop.
884At such a time, the region contains a stretch of reinserted
885previously-killed text. yank-pop deletes that text and inserts in its
886place a different stretch of killed text.
887
888With no argument, the previous kill is inserted.
889With argument n, the n'th previous kill is inserted.
890If n is negative, this is a more recent kill.
891
892The sequence of kills wraps around, so that after the oldest one
893comes the newest one."
894 (interactive "*p")
895 (if (not (eq last-command 'yank))
896 (error "Previous command was not a yank"))
897 (setq this-command 'yank)
898 (let ((before (< (point) (mark))))
899 (delete-region (point) (mark))
2076c87c 900 (set-mark (point))
70e14c01 901 (insert (current-kill arg))
2076c87c
JB
902 (if before (exchange-point-and-mark))))
903
904(defun yank (&optional arg)
905 "Reinsert the last stretch of killed text.
906More precisely, reinsert the stretch of killed text most recently
907killed OR yanked.
908With just C-U as argument, same but put point in front (and mark at end).
909With argument n, reinsert the nth most recently killed stretch of killed
910text.
911See also the command \\[yank-pop]."
912 (interactive "*P")
2076c87c 913 (push-mark (point))
70e14c01
JB
914 (insert (current-kill (cond
915 ((listp arg) 0)
916 ((eq arg '-) -1)
917 (t (1- arg)))))
2076c87c
JB
918 (if (consp arg)
919 (exchange-point-and-mark)))
70e14c01
JB
920
921(defun rotate-yank-pointer (arg)
922 "Rotate the yanking point in the kill ring.
923With argument, rotate that many kills forward (or backward, if negative)."
924 (interactive "p")
925 (current-kill arg))
926
2076c87c
JB
927\f
928(defun insert-buffer (buffer)
929 "Insert after point the contents of BUFFER.
930Puts mark after the inserted text.
931BUFFER may be a buffer or a buffer name."
932 (interactive (list (read-buffer "Insert buffer: " (other-buffer) t)))
933 (or (bufferp buffer)
934 (setq buffer (get-buffer buffer)))
935 (let (start end newmark)
936 (save-excursion
937 (save-excursion
938 (set-buffer buffer)
939 (setq start (point-min) end (point-max)))
940 (insert-buffer-substring buffer start end)
941 (setq newmark (point)))
942 (push-mark newmark)))
943
944(defun append-to-buffer (buffer start end)
945 "Append to specified buffer the text of the region.
946It is inserted into that buffer before its point.
947
948When calling from a program, give three arguments:
949BUFFER (or buffer name), START and END.
950START and END specify the portion of the current buffer to be copied."
70e14c01
JB
951 (interactive
952 (list (read-buffer "Append to buffer: " (other-buffer nil t) t)))
2076c87c
JB
953 (let ((oldbuf (current-buffer)))
954 (save-excursion
955 (set-buffer (get-buffer-create buffer))
956 (insert-buffer-substring oldbuf start end))))
957
958(defun prepend-to-buffer (buffer start end)
959 "Prepend to specified buffer the text of the region.
960It is inserted into that buffer after its point.
961
962When calling from a program, give three arguments:
963BUFFER (or buffer name), START and END.
964START and END specify the portion of the current buffer to be copied."
965 (interactive "BPrepend to buffer: \nr")
966 (let ((oldbuf (current-buffer)))
967 (save-excursion
968 (set-buffer (get-buffer-create buffer))
969 (save-excursion
970 (insert-buffer-substring oldbuf start end)))))
971
972(defun copy-to-buffer (buffer start end)
973 "Copy to specified buffer the text of the region.
974It is inserted into that buffer, replacing existing text there.
975
976When calling from a program, give three arguments:
977BUFFER (or buffer name), START and END.
978START and END specify the portion of the current buffer to be copied."
979 (interactive "BCopy to buffer: \nr")
980 (let ((oldbuf (current-buffer)))
981 (save-excursion
982 (set-buffer (get-buffer-create buffer))
983 (erase-buffer)
984 (save-excursion
985 (insert-buffer-substring oldbuf start end)))))
986\f
987(defun mark ()
988 "Return this buffer's mark value as integer, or nil if no mark.
989If you are using this in an editing command, you are most likely making
990a mistake; see the documentation of `set-mark'."
991 (marker-position (mark-marker)))
992
993(defun set-mark (pos)
994 "Set this buffer's mark to POS. Don't use this function!
995That is to say, don't use this function unless you want
996the user to see that the mark has moved, and you want the previous
997mark position to be lost.
998
999Normally, when a new mark is set, the old one should go on the stack.
1000This is why most applications should use push-mark, not set-mark.
1001
1002Novice emacs-lisp programmers often try to use the mark for the wrong
1003purposes. The mark saves a location for the user's convenience.
1004Most editing commands should not alter the mark.
1005To remember a location for internal use in the Lisp program,
1006store it in a Lisp variable. Example:
1007
1008 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1009
1010 (set-marker (mark-marker) pos (current-buffer)))
1011
1012(defvar mark-ring nil
1013 "The list of saved former marks of the current buffer,
1014most recent first.")
1015(make-variable-buffer-local 'mark-ring)
1016
1017(defconst mark-ring-max 16
1018 "*Maximum size of mark ring. Start discarding off end if gets this big.")
1019
1020(defun set-mark-command (arg)
1021 "Set mark at where point is, or jump to mark.
1022With no prefix argument, set mark, and push previous mark on mark ring.
1023With argument, jump to mark, and pop into mark off the mark ring.
1024
1025Novice emacs-lisp programmers often try to use the mark for the wrong
1026purposes. See the documentation of `set-mark' for more information."
1027 (interactive "P")
1028 (if (null arg)
1029 (push-mark)
1030 (if (null (mark))
1031 (error "No mark set in this buffer")
1032 (goto-char (mark))
1033 (pop-mark))))
1034
1035(defun push-mark (&optional location nomsg)
1036 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1037Displays \"Mark set\" unless the optional second arg NOMSG is non-nil.
1038
1039Novice emacs-lisp programmers often try to use the mark for the wrong
1040purposes. See the documentation of `set-mark' for more information."
1041 (if (null (mark))
1042 nil
1043 (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring))
1044 (if (> (length mark-ring) mark-ring-max)
1045 (progn
1046 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil)
1047 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil))))
1048 (set-mark (or location (point)))
1049 (or nomsg executing-macro (> (minibuffer-depth) 0)
1050 (message "Mark set"))
1051 nil)
1052
1053(defun pop-mark ()
1054 "Pop off mark ring into the buffer's actual mark.
1055Does not set point. Does nothing if mark ring is empty."
1056 (if mark-ring
1057 (progn
1058 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker)))))
1059 (set-mark (+ 0 (car mark-ring)))
1060 (move-marker (car mark-ring) nil)
1061 (if (null (mark)) (ding))
1062 (setq mark-ring (cdr mark-ring)))))
1063
1064(fset 'exchange-dot-and-mark 'exchange-point-and-mark)
1065(defun exchange-point-and-mark ()
1066 "Put the mark where point is now, and point where the mark is now."
1067 (interactive nil)
1068 (let ((omark (mark)))
1069 (if (null omark)
1070 (error "No mark set in this buffer"))
1071 (set-mark (point))
1072 (goto-char omark)
1073 nil))
1074\f
1075(defun next-line (arg)
1076 "Move cursor vertically down ARG lines.
1077If there is no character in the target line exactly under the current column,
1078the cursor is positioned after the character in that line which spans this
1079column, or at the end of the line if it is not long enough.
1080If there is no line in the buffer after this one,
1081a newline character is inserted to create a line
1082and the cursor moves to that line.
1083
1084The command \\[set-goal-column] can be used to create
1085a semipermanent goal column to which this command always moves.
1086Then it does not try to move vertically. This goal column is stored
1087in `goal-column', which is nil when there is none.
1088
1089If you are thinking of using this in a Lisp program, consider
1090using `forward-line' instead. It is usually easier to use
1091and more reliable (no dependence on goal column, etc.)."
1092 (interactive "p")
1093 (if (= arg 1)
1094 (let ((opoint (point)))
1095 (forward-line 1)
1096 (if (or (= opoint (point))
1097 (not (eq (preceding-char) ?\n)))
1098 (insert ?\n)
1099 (goto-char opoint)
1100 (line-move arg)))
1101 (line-move arg))
1102 nil)
1103
1104(defun previous-line (arg)
1105 "Move cursor vertically up ARG lines.
1106If there is no character in the target line exactly over the current column,
1107the cursor is positioned after the character in that line which spans this
1108column, or at the end of the line if it is not long enough.
1109
1110The command \\[set-goal-column] can be used to create
1111a semipermanent goal column to which this command always moves.
1112Then it does not try to move vertically.
1113
1114If you are thinking of using this in a Lisp program, consider using
1115`forward-line' with negative argument instead.. It is usually easier
1116to use and more reliable (no dependence on goal column, etc.)."
1117 (interactive "p")
1118 (line-move (- arg))
1119 nil)
1120
1121(defconst track-eol nil
1122 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1123This means moving to the end of each line moved onto.
1124The beginning of a blank line does not count as the end of a line.")
1125
1126(make-variable-buffer-local
1127 (defvar goal-column nil
1128 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."))
1129
1130(defvar temporary-goal-column 0
1131 "Current goal column for vertical motion.
1132It is the column where point was
1133at the start of current run of vertical motion commands.
c637ae6f 1134When the `track-eol' feature is doing its job, the value is 9999.")
2076c87c
JB
1135
1136(defun line-move (arg)
1137 (if (not (or (eq last-command 'next-line)
1138 (eq last-command 'previous-line)))
1139 (setq temporary-goal-column
1140 (if (and track-eol (eolp)
1141 ;; Don't count beg of empty line as end of line
1142 ;; unless we just did explicit end-of-line.
1143 (or (not (bolp)) (eq last-command 'end-of-line)))
1144 9999
1145 (current-column))))
1146 (if (not (integerp selective-display))
1147 (forward-line arg)
1148 ;; Move by arg lines, but ignore invisible ones.
1149 (while (> arg 0)
1150 (vertical-motion 1)
1151 (forward-char -1)
1152 (forward-line 1)
1153 (setq arg (1- arg)))
1154 (while (< arg 0)
1155 (vertical-motion -1)
1156 (beginning-of-line)
1157 (setq arg (1+ arg))))
1158 (move-to-column (or goal-column temporary-goal-column))
1159 nil)
1160
1161
1162(defun set-goal-column (arg)
1163 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1164Those commands will move to this position in the line moved to
1165rather than trying to keep the same horizontal position.
1166With a non-nil argument, clears out the goal column
1167so that \\[next-line] and \\[previous-line] resume vertical motion."
1168 (interactive "P")
1169 (if arg
1170 (progn
1171 (setq goal-column nil)
1172 (message "No goal column"))
1173 (setq goal-column (current-column))
1174 (message (substitute-command-keys
1175 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
1176 goal-column))
1177 nil)
1178\f
1179(defun transpose-chars (arg)
1180 "Interchange characters around point, moving forward one character.
1181With prefix arg ARG, effect is to take character before point
1182and drag it forward past ARG other characters (backward if ARG negative).
1183If no argument and at end of line, the previous two chars are exchanged."
1184 (interactive "*P")
1185 (and (null arg) (eolp) (forward-char -1))
1186 (transpose-subr 'forward-char (prefix-numeric-value arg)))
1187
1188(defun transpose-words (arg)
1189 "Interchange words around point, leaving point at end of them.
1190With prefix arg ARG, effect is to take word before or around point
1191and drag it forward past ARG other words (backward if ARG negative).
1192If ARG is zero, the words around or after point and around or after mark
1193are interchanged."
1194 (interactive "*p")
1195 (transpose-subr 'forward-word arg))
1196
1197(defun transpose-sexps (arg)
1198 "Like \\[transpose-words] but applies to sexps.
1199Does not work on a sexp that point is in the middle of
1200if it is a list or string."
1201 (interactive "*p")
1202 (transpose-subr 'forward-sexp arg))
1203
1204(defun transpose-lines (arg)
1205 "Exchange current line and previous line, leaving point after both.
1206With argument ARG, takes previous line and moves it past ARG lines.
1207With argument 0, interchanges line point is in with line mark is in."
1208 (interactive "*p")
1209 (transpose-subr (function
1210 (lambda (arg)
1211 (if (= arg 1)
1212 (progn
1213 ;; Move forward over a line,
1214 ;; but create a newline if none exists yet.
1215 (end-of-line)
1216 (if (eobp)
1217 (newline)
1218 (forward-char 1)))
1219 (forward-line arg))))
1220 arg))
1221
1222(defun transpose-subr (mover arg)
1223 (let (start1 end1 start2 end2)
1224 (if (= arg 0)
1225 (progn
1226 (save-excursion
1227 (funcall mover 1)
1228 (setq end2 (point))
1229 (funcall mover -1)
1230 (setq start2 (point))
1231 (goto-char (mark))
1232 (funcall mover 1)
1233 (setq end1 (point))
1234 (funcall mover -1)
1235 (setq start1 (point))
1236 (transpose-subr-1))
1237 (exchange-point-and-mark)))
1238 (while (> arg 0)
1239 (funcall mover -1)
1240 (setq start1 (point))
1241 (funcall mover 1)
1242 (setq end1 (point))
1243 (funcall mover 1)
1244 (setq end2 (point))
1245 (funcall mover -1)
1246 (setq start2 (point))
1247 (transpose-subr-1)
1248 (goto-char end2)
1249 (setq arg (1- arg)))
1250 (while (< arg 0)
1251 (funcall mover -1)
1252 (setq start2 (point))
1253 (funcall mover -1)
1254 (setq start1 (point))
1255 (funcall mover 1)
1256 (setq end1 (point))
1257 (funcall mover 1)
1258 (setq end2 (point))
1259 (transpose-subr-1)
1260 (setq arg (1+ arg)))))
1261
1262(defun transpose-subr-1 ()
1263 (if (> (min end1 end2) (max start1 start2))
1264 (error "Don't have two things to transpose"))
1265 (let ((word1 (buffer-substring start1 end1))
1266 (word2 (buffer-substring start2 end2)))
1267 (delete-region start2 end2)
1268 (goto-char start2)
1269 (insert word1)
1270 (goto-char (if (< start1 start2) start1
1271 (+ start1 (- (length word1) (length word2)))))
1272 (delete-char (length word1))
1273 (insert word2)))
1274\f
1275(defconst comment-column 32
1276 "*Column to indent right-margin comments to.
1277Setting this variable automatically makes it local to the current buffer.")
1278(make-variable-buffer-local 'comment-column)
1279
1280(defconst comment-start nil
1281 "*String to insert to start a new comment, or nil if no comment syntax defined.")
1282
1283(defconst comment-start-skip nil
1284 "*Regexp to match the start of a comment plus everything up to its body.
1285If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1286at the place matched by the close of the first pair.")
1287
1288(defconst comment-end ""
1289 "*String to insert to end a new comment.
1290Should be an empty string if comments are terminated by end-of-line.")
1291
1292(defconst comment-indent-hook
1293 '(lambda () comment-column)
1294 "Function to compute desired indentation for a comment.
1295This function is called with no args with point at the beginning of
1296the comment's starting delimiter.")
1297
1298(defun indent-for-comment ()
1299 "Indent this line's comment to comment column, or insert an empty comment."
1300 (interactive "*")
1301 (beginning-of-line 1)
1302 (if (null comment-start)
1303 (error "No comment syntax defined")
1304 (let* ((eolpos (save-excursion (end-of-line) (point)))
1305 cpos indent begpos)
1306 (if (re-search-forward comment-start-skip eolpos 'move)
1307 (progn (setq cpos (point-marker))
1308 ;; Find the start of the comment delimiter.
1309 ;; If there were paren-pairs in comment-start-skip,
1310 ;; position at the end of the first pair.
1311 (if (match-end 1)
1312 (goto-char (match-end 1))
1313 ;; If comment-start-skip matched a string with internal
1314 ;; whitespace (not final whitespace) then the delimiter
1315 ;; start at the end of that whitespace.
1316 ;; Otherwise, it starts at the beginning of what was matched.
1317 (skip-chars-backward " \t" (match-beginning 0))
1318 (skip-chars-backward "^ \t" (match-beginning 0)))))
1319 (setq begpos (point))
1320 ;; Compute desired indent.
1321 (if (= (current-column)
1322 (setq indent (funcall comment-indent-hook)))
1323 (goto-char begpos)
1324 ;; If that's different from current, change it.
1325 (skip-chars-backward " \t")
1326 (delete-region (point) begpos)
1327 (indent-to indent))
1328 ;; An existing comment?
1329 (if cpos
1330 (progn (goto-char cpos)
1331 (set-marker cpos nil))
1332 ;; No, insert one.
1333 (insert comment-start)
1334 (save-excursion
1335 (insert comment-end))))))
1336
1337(defun set-comment-column (arg)
1338 "Set the comment column based on point.
1339With no arg, set the comment column to the current column.
1340With just minus as arg, kill any comment on this line.
1341With any other arg, set comment column to indentation of the previous comment
1342 and then align or create a comment on this line at that column."
1343 (interactive "P")
1344 (if (eq arg '-)
1345 (kill-comment nil)
1346 (if arg
1347 (progn
1348 (save-excursion
1349 (beginning-of-line)
1350 (re-search-backward comment-start-skip)
1351 (beginning-of-line)
1352 (re-search-forward comment-start-skip)
1353 (goto-char (match-beginning 0))
1354 (setq comment-column (current-column))
1355 (message "Comment column set to %d" comment-column))
1356 (indent-for-comment))
1357 (setq comment-column (current-column))
1358 (message "Comment column set to %d" comment-column))))
1359
1360(defun kill-comment (arg)
1361 "Kill the comment on this line, if any.
1362With argument, kill comments on that many lines starting with this one."
1363 ;; this function loses in a lot of situations. it incorrectly recognises
1364 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
1365 ;; with multi-line comments, can kill extra whitespace if comment wasn't
1366 ;; through end-of-line, et cetera.
1367 (interactive "P")
1368 (or comment-start-skip (error "No comment syntax defined"))
1369 (let ((count (prefix-numeric-value arg)) endc)
1370 (while (> count 0)
1371 (save-excursion
1372 (end-of-line)
1373 (setq endc (point))
1374 (beginning-of-line)
1375 (and (string< "" comment-end)
1376 (setq endc
1377 (progn
1378 (re-search-forward (regexp-quote comment-end) endc 'move)
1379 (skip-chars-forward " \t")
1380 (point))))
1381 (beginning-of-line)
1382 (if (re-search-forward comment-start-skip endc t)
1383 (progn
1384 (goto-char (match-beginning 0))
1385 (skip-chars-backward " \t")
1386 (kill-region (point) endc)
1387 ;; to catch comments a line beginnings
1388 (indent-according-to-mode))))
1389 (if arg (forward-line 1))
1390 (setq count (1- count)))))
1391
1392(defun comment-region (beg end &optional arg)
1393 "Comment the region; third arg numeric means use ARG comment characters.
1394If ARG is negative, delete that many comment characters instead.
1395Comments are terminated on each line, even for syntax in which newline does
1396not end the comment. Blank lines do not get comments."
1397 ;; if someone wants it to only put a comment-start at the beginning and
1398 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
1399 ;; is easy enough. No option is made here for other than commenting
1400 ;; every line.
1401 (interactive "r\np")
1402 (or comment-start (error "No comment syntax is defined"))
1403 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
1404 (save-excursion
1405 (save-restriction
1406 (let ((cs comment-start) (ce comment-end))
1407 (cond ((not arg) (setq arg 1))
1408 ((> arg 1)
1409 (while (> (setq arg (1- arg)) 0)
1410 (setq cs (concat cs comment-start)
1411 ce (concat ce comment-end)))))
1412 (narrow-to-region beg end)
1413 (goto-char beg)
1414 (while (not (eobp))
1415 (if (< arg 0)
1416 (let ((count arg))
1417 (while (and (> 1 (setq count (1+ count)))
1418 (looking-at (regexp-quote cs)))
1419 (delete-char (length cs)))
1420 (if (string= "" ce) ()
1421 (setq count arg)
1422 (while (> 1 (setq count (1+ count)))
1423 (end-of-line)
1424 ;; this is questionable if comment-end ends in whitespace
1425 ;; that is pretty brain-damaged though
1426 (skip-chars-backward " \t")
1427 (backward-char (length ce))
1428 (if (looking-at (regexp-quote ce))
1429 (delete-char (length ce))))))
1430 (if (looking-at "[ \t]*$") ()
1431 (insert cs)
1432 (if (string= "" ce) ()
1433 (end-of-line)
1434 (insert ce)))
1435 (search-forward "\n" nil 'move)))))))
1436\f
1437(defun backward-word (arg)
1438 "Move backward until encountering the end of a word.
1439With argument, do this that many times.
1440In programs, it is faster to call forward-word with negative arg."
1441 (interactive "p")
1442 (forward-word (- arg)))
1443
1444(defun mark-word (arg)
1445 "Set mark arg words away from point."
1446 (interactive "p")
1447 (push-mark
1448 (save-excursion
1449 (forward-word arg)
1450 (point))))
1451
1452(defun kill-word (arg)
1453 "Kill characters forward until encountering the end of a word.
1454With argument, do this that many times."
1455 (interactive "p")
1456 (kill-region (point) (progn (forward-word arg) (point))))
1457
1458(defun backward-kill-word (arg)
1459 "Kill characters backward until encountering the end of a word.
1460With argument, do this that many times."
1461 (interactive "p")
1462 (kill-word (- arg)))
1463\f
1464(defconst fill-prefix nil
1465 "*String for filling to insert at front of new line, or nil for none.
1466Setting this variable automatically makes it local to the current buffer.")
1467(make-variable-buffer-local 'fill-prefix)
1468
1469(defconst auto-fill-inhibit-regexp nil
1470 "*Regexp to match lines which should not be auto-filled.")
1471
1472(defun do-auto-fill ()
1473 (let (give-up)
1474 (or (and auto-fill-inhibit-regexp
1475 (save-excursion (beginning-of-line)
1476 (looking-at auto-fill-inhibit-regexp)))
1477 (while (and (not give-up) (> (current-column) fill-column))
1478 (let ((fill-point
1479 (let ((opoint (point)))
1480 (save-excursion
1481 (move-to-column (1+ fill-column))
1482 (skip-chars-backward "^ \t\n")
1483 (if (bolp)
1484 (re-search-forward "[ \t]" opoint t))
1485 (skip-chars-backward " \t")
1486 (point)))))
1487 ;; If there is a space on the line before fill-point,
1488 ;; and nonspaces precede it, break the line there.
1489 (if (save-excursion
1490 (goto-char fill-point)
1491 (not (bolp)))
1492 ;; If point is at the fill-point, do not `save-excursion'.
1493 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
1494 ;; point will end up before it rather than after it.
1495 (if (save-excursion
1496 (skip-chars-backward " \t")
1497 (= (point) fill-point))
1498 (indent-new-comment-line)
1499 (save-excursion
1500 (goto-char fill-point)
1501 (indent-new-comment-line)))
1502 ;; No place to break => stop trying.
1503 (setq give-up t)))))))
1504
1505(defconst comment-multi-line nil
1506 "*Non-nil means \\[indent-new-comment-line] should continue same comment
c88ab9ce
ER
1507on new line, with no new terminator or starter.
1508This is obsolete because you might as well use \\[newline-and-indent].")
2076c87c
JB
1509
1510(defun indent-new-comment-line ()
1511 "Break line at point and indent, continuing comment if presently within one.
c88ab9ce
ER
1512The body of the continued comment is indented under the previous comment line.
1513
1514This command is intended for styles where you write a comment per line,
1515starting a new comment (and terminating it if necessary) on each line.
1516If you want to continue one comment across several lines, use \\[newline-and-indent]."
2076c87c
JB
1517 (interactive "*")
1518 (let (comcol comstart)
1519 (skip-chars-backward " \t")
1520 (delete-region (point)
1521 (progn (skip-chars-forward " \t")
1522 (point)))
1523 (insert ?\n)
c88ab9ce
ER
1524 (if (not comment-multi-line)
1525 (save-excursion
1526 (if (and comment-start-skip
1527 (let ((opoint (point)))
1528 (forward-line -1)
1529 (re-search-forward comment-start-skip opoint t)))
1530 ;; The old line is a comment.
1531 ;; Set WIN to the pos of the comment-start.
1532 ;; But if the comment is empty, look at preceding lines
1533 ;; to find one that has a nonempty comment.
1534 (let ((win (match-beginning 0)))
1535 (while (and (eolp) (not (bobp))
1536 (let (opoint)
1537 (beginning-of-line)
1538 (setq opoint (point))
1539 (forward-line -1)
1540 (re-search-forward comment-start-skip opoint t)))
1541 (setq win (match-beginning 0)))
1542 ;; Indent this line like what we found.
1543 (goto-char win)
1544 (setq comcol (current-column))
1545 (setq comstart (buffer-substring (point) (match-end 0)))))))
2076c87c
JB
1546 (if comcol
1547 (let ((comment-column comcol)
1548 (comment-start comstart)
1549 (comment-end comment-end))
1550 (and comment-end (not (equal comment-end ""))
c88ab9ce 1551; (if (not comment-multi-line)
2076c87c
JB
1552 (progn
1553 (forward-char -1)
1554 (insert comment-end)
1555 (forward-char 1))
c88ab9ce
ER
1556; (setq comment-column (+ comment-column (length comment-start))
1557; comment-start "")
1558; )
1559 )
2076c87c
JB
1560 (if (not (eolp))
1561 (setq comment-end ""))
1562 (insert ?\n)
1563 (forward-char -1)
1564 (indent-for-comment)
1565 (save-excursion
1566 ;; Make sure we delete the newline inserted above.
1567 (end-of-line)
1568 (delete-char 1)))
1569 (if fill-prefix
1570 (insert fill-prefix)
1571 (indent-according-to-mode)))))
1572
1573(defun auto-fill-mode (&optional arg)
1574 "Toggle auto-fill mode.
1575With arg, turn auto-fill mode on if and only if arg is positive.
1576In auto-fill mode, inserting a space at a column beyond fill-column
1577automatically breaks the line at a previous space."
1578 (interactive "P")
1579 (prog1 (setq auto-fill-function
1580 (if (if (null arg)
1581 (not auto-fill-function)
1582 (> (prefix-numeric-value arg) 0))
1583 'do-auto-fill
1584 nil))
1585 ;; update mode-line
1586 (set-buffer-modified-p (buffer-modified-p))))
1587
1588(defun turn-on-auto-fill ()
1589 "Unconditionally turn on Auto Fill mode."
1590 (auto-fill-mode 1))
1591
1592(defun set-fill-column (arg)
1593 "Set fill-column to current column, or to argument if given.
1594fill-column's value is separate for each buffer."
1595 (interactive "P")
1596 (setq fill-column (if (integerp arg) arg (current-column)))
1597 (message "fill-column set to %d" fill-column))
1598\f
1599(defun set-selective-display (arg)
1600 "Set selective-display to ARG; clear it if no arg.
1601When selective-display is a number > 0,
1602lines whose indentation is >= selective-display are not displayed.
1603selective-display's value is separate for each buffer."
1604 (interactive "P")
1605 (if (eq selective-display t)
1606 (error "selective-display already in use for marked lines"))
c88ab9ce
ER
1607 (let ((current-vpos
1608 (save-restriction
1609 (narrow-to-region (point-min) (point))
1610 (goto-char (window-start))
1611 (vertical-motion (window-height)))))
1612 (setq selective-display
1613 (and arg (prefix-numeric-value arg)))
1614 (recenter current-vpos))
2076c87c
JB
1615 (set-window-start (selected-window) (window-start (selected-window)))
1616 (princ "selective-display set to " t)
1617 (prin1 selective-display t)
1618 (princ "." t))
1619
1620(defun overwrite-mode (arg)
1621 "Toggle overwrite mode.
1622With arg, turn overwrite mode on iff arg is positive.
1623In overwrite mode, printing characters typed in replace existing text
1624on a one-for-one basis, rather than pushing it to the right."
1625 (interactive "P")
1626 (setq overwrite-mode
1627 (if (null arg) (not overwrite-mode)
1628 (> (prefix-numeric-value arg) 0)))
1629 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
1630\f
1631(defvar blink-matching-paren t
1632 "*Non-nil means show matching open-paren when close-paren is inserted.")
1633
1634(defconst blink-matching-paren-distance 4000
1635 "*If non-nil, is maximum distance to search for matching open-paren
1636when close-paren is inserted.")
1637
1638(defun blink-matching-open ()
1639 "Move cursor momentarily to the beginning of the sexp before point."
1640 (interactive)
1641 (and (> (point) (1+ (point-min)))
1642 (/= (char-syntax (char-after (- (point) 2))) ?\\ )
1643 blink-matching-paren
1644 (let* ((oldpos (point))
1645 (blinkpos)
1646 (mismatch))
1647 (save-excursion
1648 (save-restriction
1649 (if blink-matching-paren-distance
1650 (narrow-to-region (max (point-min)
1651 (- (point) blink-matching-paren-distance))
1652 oldpos))
1653 (condition-case ()
1654 (setq blinkpos (scan-sexps oldpos -1))
1655 (error nil)))
1656 (and blinkpos (/= (char-syntax (char-after blinkpos))
1657 ?\$)
1658 (setq mismatch
1659 (/= (char-after (1- oldpos))
1660 (logand (lsh (aref (syntax-table)
1661 (char-after blinkpos))
1662 -8)
1663 255))))
1664 (if mismatch (setq blinkpos nil))
1665 (if blinkpos
1666 (progn
1667 (goto-char blinkpos)
1668 (if (pos-visible-in-window-p)
1669 (sit-for 1)
1670 (goto-char blinkpos)
1671 (message
1672 "Matches %s"
1673 (if (save-excursion
1674 (skip-chars-backward " \t")
1675 (not (bolp)))
1676 (buffer-substring (progn (beginning-of-line) (point))
1677 (1+ blinkpos))
1678 (buffer-substring blinkpos
1679 (progn
1680 (forward-char 1)
1681 (skip-chars-forward "\n \t")
1682 (end-of-line)
1683 (point)))))))
1684 (cond (mismatch
1685 (message "Mismatched parentheses"))
1686 ((not blink-matching-paren-distance)
1687 (message "Unmatched parenthesis"))))))))
1688
1689;Turned off because it makes dbx bomb out.
1690(setq blink-paren-function 'blink-matching-open)
1691
1692; this is just something for the luser to see in a keymap -- this is not
1693; how quitting works normally!
1694(defun keyboard-quit ()
1695 "Signal a quit condition."
1696 (interactive)
1697 (signal 'quit nil))
1698
1699(define-key global-map "\C-g" 'keyboard-quit)
1700\f
1701(defun set-variable (var val)
1702 "Set VARIABLE to VALUE. VALUE is a Lisp object.
1703When using this interactively, supply a Lisp expression for VALUE.
1704If you want VALUE to be a string, you must surround it with doublequotes."
1705 (interactive
1706 (let* ((var (read-variable "Set variable: "))
1707 (minibuffer-help-form
1708 '(funcall myhelp))
1709 (myhelp
1710 (function
1711 (lambda ()
1712 (with-output-to-temp-buffer "*Help*"
1713 (prin1 var)
1714 (princ "\nDocumentation:\n")
1715 (princ (substring (documentation-property var 'variable-documentation)
1716 1))
1717 (if (boundp var)
1718 (let ((print-length 20))
1719 (princ "\n\nCurrent value: ")
1720 (prin1 (symbol-value var))))
1721 nil)))))
1722 (list var
1723 (eval-minibuffer (format "Set %s to value: " var)))))
1724 (set var val))
1725\f
1726;These commands are defined in editfns.c
1727;but they are not assigned to keys there.
1728(put 'narrow-to-region 'disabled t)
1729(define-key ctl-x-map "n" 'narrow-to-region)
1730(define-key ctl-x-map "w" 'widen)
1731
1732(define-key global-map "\C-j" 'newline-and-indent)
1733(define-key global-map "\C-m" 'newline)
1734(define-key global-map "\C-o" 'open-line)
1735(define-key esc-map "\C-o" 'split-line)
1736(define-key global-map "\C-q" 'quoted-insert)
1737(define-key esc-map "^" 'delete-indentation)
1738(define-key esc-map "\\" 'delete-horizontal-space)
1739(define-key esc-map "m" 'back-to-indentation)
1740(define-key ctl-x-map "\C-o" 'delete-blank-lines)
1741(define-key esc-map " " 'just-one-space)
1742(define-key esc-map "z" 'zap-to-char)
1743(define-key esc-map "=" 'count-lines-region)
1744(define-key ctl-x-map "=" 'what-cursor-position)
1745(define-key esc-map "\e" 'eval-expression)
1746(define-key ctl-x-map "\e" 'repeat-complex-command)
1747(define-key ctl-x-map "u" 'advertised-undo)
1748(define-key global-map "\C-_" 'undo)
1749(define-key esc-map "!" 'shell-command)
1750(define-key esc-map "|" 'shell-command-on-region)
1751
1752(define-key global-map "\C-u" 'universal-argument)
1753(let ((i ?0))
1754 (while (<= i ?9)
1755 (define-key esc-map (char-to-string i) 'digit-argument)
1756 (setq i (1+ i))))
1757(define-key esc-map "-" 'negative-argument)
1758
1759(define-key global-map "\C-k" 'kill-line)
1760(define-key global-map "\C-w" 'kill-region)
1761(define-key esc-map "w" 'kill-ring-save)
1762(define-key esc-map "\C-w" 'append-next-kill)
1763(define-key global-map "\C-y" 'yank)
1764(define-key esc-map "y" 'yank-pop)
1765
1766(define-key ctl-x-map "a" 'append-to-buffer)
1767
1768(define-key global-map "\C-@" 'set-mark-command)
1769(define-key ctl-x-map "\C-x" 'exchange-point-and-mark)
1770
1771(define-key global-map "\C-n" 'next-line)
1772(define-key global-map "\C-p" 'previous-line)
1773(define-key ctl-x-map "\C-n" 'set-goal-column)
1774
c637ae6f
JB
1775(define-key global-map [up] 'previous-line)
1776(define-key global-map [down] 'next-line)
1777(define-key global-map [left] 'backward-char)
1778(define-key global-map [right] 'forward-char)
1779
2076c87c
JB
1780(define-key global-map "\C-t" 'transpose-chars)
1781(define-key esc-map "t" 'transpose-words)
1782(define-key esc-map "\C-t" 'transpose-sexps)
1783(define-key ctl-x-map "\C-t" 'transpose-lines)
1784
1785(define-key esc-map ";" 'indent-for-comment)
1786(define-key esc-map "j" 'indent-new-comment-line)
1787(define-key esc-map "\C-j" 'indent-new-comment-line)
1788(define-key ctl-x-map ";" 'set-comment-column)
1789(define-key ctl-x-map "f" 'set-fill-column)
1790(define-key ctl-x-map "$" 'set-selective-display)
1791
1792(define-key esc-map "@" 'mark-word)
1793(define-key esc-map "f" 'forward-word)
1794(define-key esc-map "b" 'backward-word)
1795(define-key esc-map "d" 'kill-word)
1796(define-key esc-map "\177" 'backward-kill-word)
1797
1798(define-key esc-map "<" 'beginning-of-buffer)
1799(define-key esc-map ">" 'end-of-buffer)
1800(define-key ctl-x-map "h" 'mark-whole-buffer)
1801(define-key esc-map "\\" 'delete-horizontal-space)
1802
1803(fset 'mode-specific-command-prefix (make-sparse-keymap))
1804(defconst mode-specific-map (symbol-function 'mode-specific-command-prefix)
1805 "Keymap for characters following C-c.")
1806(define-key global-map "\C-c" 'mode-specific-command-prefix)
c88ab9ce
ER
1807
1808;;; simple.el ends here