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