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