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