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