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