comparison lisp/prim/simple.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children ac2d302a0011
comparison
equal deleted inserted replaced
-1:000000000000 0:376386a54a3c
1 ;;; simple.el --- basic editing commands for XEmacs
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993-1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5
6 ;; This file is part of XEmacs.
7
8 ;; XEmacs is free software; you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; XEmacs is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;; General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with XEmacs; see the file COPYING. If not, write to the Free
20 ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 ;;; Synched up with: FSF 19.30.
23
24 ;;; Commentary:
25
26 ;; A grab-bag of basic XEmacs commands not specifically related to some
27 ;; major mode or to file-handling.
28
29 ;;; Changes for zmacs-style active-regions:
30 ;;;
31 ;;; beginning-of-buffer, end-of-buffer, count-lines-region,
32 ;;; count-lines-buffer, what-line, what-cursor-position, set-goal-column,
33 ;;; set-fill-column, prefix-arg-internal, and line-move (which is used by
34 ;;; next-line and previous-line) set zmacs-region-stays to t, so that they
35 ;;; don't affect the current region-hilighting state.
36 ;;;
37 ;;; mark-whole-buffer, mark-word, exchange-point-and-mark, and
38 ;;; set-mark-command (without an argument) call zmacs-activate-region.
39 ;;;
40 ;;; mark takes an optional arg like the new Fmark_marker() does. When
41 ;;; the region is not active, mark returns nil unless the optional arg is true.
42 ;;;
43 ;;; push-mark, pop-mark, exchange-point-and-mark, and set-marker, and
44 ;;; set-mark-command use (mark t) so that they can access the mark whether
45 ;;; the region is active or not.
46 ;;;
47 ;;; shell-command, shell-command-on-region, yank, and yank-pop (which all
48 ;;; push a mark) have been altered to call exchange-point-and-mark with an
49 ;;; argument, meaning "don't activate the region". These commands only use
50 ;;; exchange-point-and-mark to position the newly-pushed mark correctly, so
51 ;;; this isn't a user-visible change. These functions have also been altered
52 ;;; to use (mark t) for the same reason.
53
54 ;;; Code:
55
56 (defun newline (&optional arg)
57 "Insert a newline, and move to left margin of the new line if it's blank.
58 The newline is marked with the text-property `hard'.
59 With arg, insert that many newlines.
60 In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
61 (interactive "*P")
62 (barf-if-buffer-read-only nil (point))
63 ;; Inserting a newline at the end of a line produces better redisplay in
64 ;; try_window_id than inserting at the beginning of a line, and the textual
65 ;; result is the same. So, if we're at beginning of line, pretend to be at
66 ;; the end of the previous line.
67 (let ((flag (and (not (bobp))
68 (bolp)
69 (< (or (previous-extent-change (point)) -2)
70 (- (point) 2))))
71 (was-page-start (and (bolp)
72 (looking-at page-delimiter)))
73 (beforepos (point)))
74 (if flag (backward-char 1))
75 ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
76 ;; Set last-command-char to tell self-insert what to insert.
77 (let ((last-command-char ?\n)
78 ;; Don't auto-fill if we have a numeric argument.
79 ;; Also not if flag is true (it would fill wrong line);
80 ;; there is no need to since we're at BOL.
81 (auto-fill-function (if (or arg flag) nil auto-fill-function)))
82 (unwind-protect
83 (self-insert-command (prefix-numeric-value arg))
84 ;; If we get an error in self-insert-command, put point at right place.
85 (if flag (forward-char 1))))
86 ;; If we did *not* get an error, cancel that forward-char.
87 (if flag (backward-char 1))
88 ;; Mark the newline(s) `hard'.
89 (if use-hard-newlines
90 (let* ((from (- (point) (if arg (prefix-numeric-value arg) 1)))
91 (sticky (get-text-property from 'end-open)))
92 (put-text-property from (point) 'hard 't)
93 ;; If end-open is not "t", add 'hard to end-open list
94 (if (and (listp sticky) (not (memq 'hard sticky)))
95 (put-text-property from (point) 'end-open
96 (cons 'hard sticky)))))
97 ;; If the newline leaves the previous line blank,
98 ;; and we have a left margin, delete that from the blank line.
99 (or flag
100 (save-excursion
101 (goto-char beforepos)
102 (beginning-of-line)
103 (and (looking-at "[ \t]$")
104 (> (current-left-margin) 0)
105 (delete-region (point) (progn (end-of-line) (point))))))
106 (if flag (forward-char 1))
107 ;; Indent the line after the newline, except in one case:
108 ;; when we added the newline at the beginning of a line
109 ;; which starts a page.
110 (or was-page-start
111 (move-to-left-margin nil t)))
112 nil)
113
114 (defun open-line (arg)
115 "Insert a newline and leave point before it.
116 If there is a fill prefix and/or a left-margin, insert them on the new line
117 if the line would have been blank.
118 With arg N, insert N newlines."
119 ;; "Insert a newline and leave point before it.
120 ;; If there is a fill prefix, insert the fill prefix on the new line
121 ;; if the line would have been empty.
122 ;; With arg N, insert N newlines."
123 (interactive "*p")
124 (let* ((do-fill-prefix (and fill-prefix (bolp)))
125 ;well, I'm going to re-enable this. --ben
126 ;(do-fill-prefix nil) ;; screw this -- says JWZ
127 (do-left-margin (and (bolp) (> (current-left-margin) 0)))
128 (loc (point)))
129 (newline arg)
130 (goto-char loc)
131 (while (> arg 0)
132 (cond ((bolp)
133 (if do-left-margin (indent-to (current-left-margin)))
134 (if do-fill-prefix (insert fill-prefix))))
135 (forward-line 1)
136 (setq arg (1- arg)))
137 (goto-char loc)
138 (end-of-line)))
139
140 (defun split-line ()
141 "Split current line, moving portion beyond point vertically down."
142 (interactive "*")
143 (skip-chars-forward " \t")
144 (let ((col (current-column))
145 (pos (point)))
146 (newline 1)
147 (indent-to col 0)
148 (goto-char pos)))
149
150 (defun quoted-insert (arg)
151 "Read next input character and insert it.
152 This is useful for inserting control characters.
153 You may also type up to 3 octal digits, to insert a character with that code.
154
155 In overwrite mode, this function inserts the character anyway, and
156 does not handle octal digits specially. This means that if you use
157 overwrite as your normal editing mode, you can use this function to
158 insert characters when necessary.
159
160 In binary overwrite mode, this function does overwrite, and octal
161 digits are interpreted as a character code. This is supposed to make
162 this function useful in editing binary files."
163 (interactive "*p")
164 (let ((char (if (or (not overwrite-mode)
165 (eq overwrite-mode 'overwrite-mode-binary))
166 (read-quoted-char)
167 (read-char))))
168 (if (> arg 0)
169 (if (eq overwrite-mode 'overwrite-mode-binary)
170 (delete-char arg)))
171 (while (> arg 0)
172 (insert char)
173 (setq arg (1- arg)))))
174
175 (defun delete-indentation (&optional arg)
176 "Join this line to previous and fix up whitespace at join.
177 If there is a fill prefix, delete it from the beginning of this line.
178 With argument, join this line to following line."
179 (interactive "*P")
180 (beginning-of-line)
181 (if arg (forward-line 1))
182 (if (eq (preceding-char) ?\n)
183 (progn
184 (delete-region (point) (1- (point)))
185 ;; If the second line started with the fill prefix,
186 ;; delete the prefix.
187 (if (and fill-prefix
188 (<= (+ (point) (length fill-prefix)) (point-max))
189 (string= fill-prefix
190 (buffer-substring (point)
191 (+ (point) (length fill-prefix)))))
192 (delete-region (point) (+ (point) (length fill-prefix))))
193 (fixup-whitespace))))
194
195 (defun fixup-whitespace ()
196 "Fixup white space between objects around point.
197 Leave one space or none, according to the context."
198 (interactive "*")
199 (save-excursion
200 (delete-horizontal-space)
201 (if (or (looking-at "^\\|\\s)")
202 (save-excursion (forward-char -1)
203 (looking-at "$\\|\\s(\\|\\s'")))
204 nil
205 (insert ?\ ))))
206
207 (defun delete-horizontal-space ()
208 "Delete all spaces and tabs around point."
209 (interactive "*")
210 (skip-chars-backward " \t")
211 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
212
213 (defun just-one-space ()
214 "Delete all spaces and tabs around point, leaving one space."
215 (interactive "*")
216 (expand-abbrev)
217 (skip-chars-backward " \t")
218 (if (= (following-char) ? )
219 (forward-char 1)
220 (insert ? ))
221 (delete-region (point) (progn (skip-chars-forward " \t") (point))))
222
223 (defun delete-blank-lines ()
224 "On blank line, delete all surrounding blank lines, leaving just one.
225 On isolated blank line, delete that one.
226 On nonblank line, delete any immediately following blank lines."
227 (interactive "*")
228 (let (thisblank singleblank)
229 (save-excursion
230 (beginning-of-line)
231 (setq thisblank (looking-at "[ \t]*$"))
232 ;; Set singleblank if there is just one blank line here.
233 (setq singleblank
234 (and thisblank
235 (not (looking-at "[ \t]*\n[ \t]*$"))
236 (or (bobp)
237 (progn (forward-line -1)
238 (not (looking-at "[ \t]*$")))))))
239 ;; Delete preceding blank lines, and this one too if it's the only one.
240 (if thisblank
241 (progn
242 (beginning-of-line)
243 (if singleblank (forward-line 1))
244 (delete-region (point)
245 (if (re-search-backward "[^ \t\n]" nil t)
246 (progn (forward-line 1) (point))
247 (point-min)))))
248 ;; Delete following blank lines, unless the current line is blank
249 ;; and there are no following blank lines.
250 (if (not (and thisblank singleblank))
251 (save-excursion
252 (end-of-line)
253 (forward-line 1)
254 (delete-region (point)
255 (if (re-search-forward "[^ \t\n]" nil t)
256 (progn (beginning-of-line) (point))
257 (point-max)))))
258 ;; Handle the special case where point is followed by newline and eob.
259 ;; Delete the line, leaving point at eob.
260 (if (looking-at "^[ \t]*\n\\'")
261 (delete-region (point) (point-max)))))
262
263 (defun back-to-indentation ()
264 "Move point to the first non-whitespace character on this line."
265 (interactive "_")
266 (beginning-of-line 1)
267 (skip-chars-forward " \t"))
268
269 (defun newline-and-indent ()
270 "Insert a newline, then indent according to major mode.
271 Indentation is done using the value of `indent-line-function'.
272 In programming language modes, this is the same as TAB.
273 In some text modes, where TAB inserts a tab, this command indents to the
274 column specified by the function `current-left-margin'."
275 (interactive "*")
276 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
277 (newline)
278 (indent-according-to-mode))
279
280 (defun reindent-then-newline-and-indent ()
281 "Reindent current line, insert newline, then indent the new line.
282 Indentation of both lines is done according to the current major mode,
283 which means calling the current value of `indent-line-function'.
284 In programming language modes, this is the same as TAB.
285 In some text modes, where TAB inserts a tab, this indents to the
286 column specified by the function `current-left-margin'."
287 (interactive "*")
288 (save-excursion
289 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
290 (indent-according-to-mode))
291 (newline)
292 (indent-according-to-mode))
293
294 ;; Internal subroutine of delete-char
295 (defun kill-forward-chars (arg)
296 (if (listp arg) (setq arg (car arg)))
297 (if (eq arg '-) (setq arg -1))
298 (kill-region (point) (+ (point) arg)))
299
300 ;; Internal subroutine of backward-delete-char
301 (defun kill-backward-chars (arg)
302 (if (listp arg) (setq arg (car arg)))
303 (if (eq arg '-) (setq arg -1))
304 (kill-region (point) (- (point) arg)))
305
306 (defun backward-delete-char-untabify (arg &optional killp)
307 "Delete characters backward, changing tabs into spaces.
308 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
309 Interactively, ARG is the prefix arg (default 1)
310 and KILLP is t if a prefix arg was specified."
311 (interactive "*p\nP")
312 (let ((count arg))
313 (save-excursion
314 (while (and (> count 0) (not (bobp)))
315 (if (= (preceding-char) ?\t)
316 (let ((col (current-column)))
317 (forward-char -1)
318 (setq col (- col (current-column)))
319 (insert-char ?\ col)
320 (delete-char 1)))
321 (forward-char -1)
322 (setq count (1- count)))))
323 (delete-backward-char arg killp)
324 ;; In overwrite mode, back over columns while clearing them out,
325 ;; unless at end of line.
326 (and overwrite-mode (not (eolp))
327 (save-excursion (insert-char ?\ arg))))
328
329 (defun zap-to-char (arg char)
330 "Kill up to and including ARG'th occurrence of CHAR.
331 Goes backward if ARG is negative; error if CHAR not found."
332 (interactive "*p\ncZap to char: ")
333 (kill-region (point) (progn
334 (search-forward (char-to-string char) nil nil arg)
335 ; (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
336 (point))))
337
338 (defun beginning-of-buffer (&optional arg)
339 "Move point to the beginning of the buffer; leave mark at previous position.
340 With arg N, put point N/10 of the way from the beginning.
341
342 If the buffer is narrowed, this command uses the beginning and size
343 of the accessible part of the buffer.
344
345 Don't use this command in Lisp programs!
346 \(goto-char (point-min)) is faster and avoids clobbering the mark."
347 (interactive "_P")
348 (push-mark)
349 (let ((size (- (point-max) (point-min))))
350 (goto-char (if arg
351 (+ (point-min)
352 (if (> size 10000)
353 ;; Avoid overflow for large buffer sizes!
354 (* (prefix-numeric-value arg)
355 (/ size 10))
356 (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
357 (point-min))))
358 (if arg (forward-line 1)))
359
360 (defun end-of-buffer (&optional arg)
361 "Move point to the end of the buffer; leave mark at previous position.
362 With arg N, put point N/10 of the way from the end.
363
364 If the buffer is narrowed, this command uses the beginning and size
365 of the accessible part of the buffer.
366
367 Don't use this command in Lisp programs!
368 \(goto-char (point-max)) is faster and avoids clobbering the mark."
369 (interactive "_P")
370 (push-mark)
371 ;; XEmacs changes here.
372 (let ((scroll-to-end (not (pos-visible-in-window-p (point-max))))
373 (size (- (point-max) (point-min))))
374 (goto-char (if arg
375 (- (point-max)
376 (if (> size 10000)
377 ;; Avoid overflow for large buffer sizes!
378 (* (prefix-numeric-value arg)
379 (/ size 10))
380 (/ (* size (prefix-numeric-value arg)) 10)))
381 (point-max)))
382 (cond (arg
383 ;; If we went to a place in the middle of the buffer,
384 ;; adjust it to the beginning of a line.
385 (forward-line 1))
386 (scroll-to-end
387 ;; If the end of the buffer is not already on the screen,
388 ;; then scroll specially to put it near, but not at, the bottom.
389 (recenter -3)))))
390
391 (defun mark-beginning-of-buffer (&optional arg)
392 "Push a mark at the beginning of the buffer; leave point where it is.
393 With arg N, push mark N/10 of the way from the true beginning."
394 (interactive "P")
395 (push-mark (if arg
396 (if (> (buffer-size) 10000)
397 ;; Avoid overflow for large buffer sizes!
398 (* (prefix-numeric-value arg)
399 (/ (buffer-size) 10))
400 (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
401 (point-min))
402 nil
403 t))
404 (define-function 'mark-bob 'mark-beginning-of-buffer)
405
406 (defun mark-end-of-buffer (&optional arg)
407 "Push a mark at the end of the buffer; leave point where it is.
408 With arg N, push mark N/10 of the way from the true end."
409 (interactive "P")
410 (push-mark (if arg
411 (- (1+ (buffer-size))
412 (if (> (buffer-size) 10000)
413 ;; Avoid overflow for large buffer sizes!
414 (* (prefix-numeric-value arg)
415 (/ (buffer-size) 10))
416 (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
417 (point-max))
418 nil
419 t))
420 (define-function 'mark-eob 'mark-end-of-buffer)
421
422 (defun mark-whole-buffer ()
423 "Put point at beginning and mark at end of buffer.
424 You probably should not use this function in Lisp programs;
425 it is usually a mistake for a Lisp function to use any subroutine
426 that uses or sets the mark."
427 (interactive)
428 (push-mark (point))
429 (push-mark (point-max) nil t)
430 (goto-char (point-min)))
431
432 (defun eval-current-buffer (&optional printflag)
433 "Evaluate the current buffer as Lisp code.
434 Programs can pass argument PRINTFLAG which controls printing of output:
435 nil means discard it; anything else is stream for print."
436 (interactive)
437 (eval-buffer (current-buffer) printflag))
438
439 (defun count-words-buffer (b)
440 (interactive "b")
441 (save-excursion
442 (let ((buf (or b (current-buffer))))
443 (set-buffer buf)
444 (message "Buffer has %d words"
445 (count-words-region (point-min) (point-max))))))
446
447 (defun count-words-region (start end)
448 (interactive "r")
449 (save-excursion
450 (let ((n 0))
451 (goto-char start)
452 (while (< (point) end)
453 (if (forward-word 1)
454 (setq n (1+ n))))
455 (message "Region has %d words" n)
456 n)))
457
458 (defun count-lines-region (start end)
459 "Print number of lines and characters in the region."
460 (interactive "_r")
461 (let ((n (count-lines start end)))
462 (message "Region has %d lines, %d characters"
463 n (- end start))
464 n))
465
466 (defun count-lines-buffer (b)
467 "Print number of lines and charcters in the specified buffer."
468 (interactive "_b")
469 (save-excursion
470 (let ((buf (or b (current-buffer)))
471 cnt)
472 (set-buffer buf)
473 (setq cnt (count-lines (point-min) (point-max)))
474 (message "Region has %d lines, %d characters"
475 cnt (- (point-max) (point-min)))
476 cnt)))
477
478 (defun what-line ()
479 "Print the current buffer line number and narrowed line number of point."
480 (interactive "_")
481 (let ((opoint (point)) start)
482 (save-excursion
483 (save-restriction
484 (goto-char (point-min))
485 (widen)
486 (beginning-of-line)
487 (setq start (point))
488 (goto-char opoint)
489 (beginning-of-line)
490 (if (/= start 1)
491 (message "line %d (narrowed line %d)"
492 (1+ (count-lines 1 (point)))
493 (1+ (count-lines start (point))))
494 (message "Line %d" (1+ (count-lines 1 (point)))))))))
495
496
497 (defun count-lines (start end)
498 "Return number of lines between START and END.
499 This is usually the number of newlines between them,
500 but can be one more if START is not equal to END
501 and the greater of them is not at the start of a line."
502 (save-excursion
503 (save-restriction
504 (narrow-to-region start end)
505 (goto-char (point-min))
506 (if (eq selective-display t)
507 (save-match-data
508 (let ((done 0))
509 (while (re-search-forward "[\n\C-m]" nil t 40)
510 (setq done (+ 40 done)))
511 (while (re-search-forward "[\n\C-m]" nil t 1)
512 (setq done (+ 1 done)))
513 (goto-char (point-max))
514 (if (and (/= start end)
515 (not (bolp)))
516 (1+ done)
517 done)))
518 (- (buffer-size) (forward-line (buffer-size)))))))
519
520 (defun what-cursor-position ()
521 "Print info on cursor position (on screen and within buffer)."
522 (interactive "_")
523 (let* ((char (following-char))
524 (beg (point-min))
525 (end (point-max))
526 (pos (point))
527 (total (buffer-size))
528 (percent (if (> total 50000)
529 ;; Avoid overflow from multiplying by 100!
530 (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
531 (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
532 (hscroll (if (= (window-hscroll) 0)
533 ""
534 (format " Hscroll=%d" (window-hscroll))))
535 (col (current-column)))
536 (if (= pos end)
537 (if (or (/= beg 1) (/= end (1+ total)))
538 (message "point=%d of %d(%d%%) <%d - %d> column %d %s"
539 pos total percent beg end col hscroll)
540 (message "point=%d of %d(%d%%) column %d %s"
541 pos total percent col hscroll))
542 (if (or (/= beg 1) (/= end (1+ total)))
543 (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) <%d - %d> column %d %s"
544 (text-char-description char) char char char pos total
545 percent beg end col hscroll)
546 (message "Char: %s (0%o, %d, 0x%x) point=%d of %d(%d%%) column %d %s"
547 (text-char-description char) char char char pos total
548 percent col hscroll)))))
549
550 (defun fundamental-mode ()
551 "Major mode not specialized for anything in particular.
552 Other major modes are defined by comparison with this one."
553 (interactive)
554 (kill-all-local-variables))
555
556
557 ;; We define this, rather than making `eval' interactive,
558 ;; for the sake of completion of names like eval-region, eval-current-buffer.
559 (defun eval-expression (expression)
560 "Evaluate EXPRESSION and print value in minibuffer.
561 Value is also consed on to front of the variable `values'."
562 (interactive "xEval: ")
563 (setq values (cons (eval expression) values))
564 (prin1 (car values) t))
565
566 (defun edit-and-eval-command (prompt command &optional history)
567 "Prompting with PROMPT, let user edit COMMAND and eval result.
568 COMMAND is a Lisp expression. Let user edit that expression in
569 the minibuffer, then read and evaluate the result."
570 (let ((command (read-expression prompt
571 ;; first try to format the thing readably;
572 ;; and if that fails, print it normally.
573 (condition-case ()
574 (let ((print-readably t))
575 (prin1-to-string command))
576 (error (prin1-to-string command)))
577 (or history '(command-history . 1)))))
578 (or history (setq history 'command-history))
579 (if (consp history)
580 (setq history (car history)))
581 (if (eq history t)
582 nil
583 ;; If command was added to the history as a string,
584 ;; get rid of that. We want only evallable expressions there.
585 (if (stringp (car (symbol-value history)))
586 (set history (cdr (symbol-value history))))
587
588 ;; If command to be redone does not match front of history,
589 ;; add it to the history.
590 (or (equal command (car (symbol-value history)))
591 (set history (cons command (symbol-value history)))))
592 (eval command)))
593
594 (defun repeat-complex-command (arg)
595 "Edit and re-evaluate last complex command, or ARGth from last.
596 A complex command is one which used the minibuffer.
597 The command is placed in the minibuffer as a Lisp form for editing.
598 The result is executed, repeating the command as changed.
599 If the command has been changed or is not the most recent previous command
600 it is added to the front of the command history.
601 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
602 to get different commands to edit and resubmit."
603 (interactive "p")
604 (let ((print-level nil))
605 (edit-and-eval-command "Redo: "
606 (or (nth (1- arg) command-history)
607 (error ""))
608 (cons 'command-history arg))))
609
610 (defun goto-line (arg)
611 "Goto line ARG, counting from line 1 at beginning of buffer."
612 (interactive "NGoto line: ")
613 (setq arg (prefix-numeric-value arg))
614 (save-restriction
615 (widen)
616 (goto-char 1)
617 (if (eq selective-display t)
618 (re-search-forward "[\n\C-m]" nil 'end (1- arg))
619 (forward-line (1- arg)))))
620
621 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
622 (define-function 'advertised-undo 'undo)
623
624 (defun undo (&optional arg)
625 "Undo some previous changes.
626 Repeat this command to undo more changes.
627 A numeric argument serves as a repeat count."
628 (interactive "*p")
629 ;; If we don't get all the way through, make last-command indicate that
630 ;; for the following command.
631 (setq this-command t)
632 (let ((modified (buffer-modified-p))
633 (recent-save (recent-auto-save-p)))
634 (or (eq (selected-window) (minibuffer-window))
635 (message "Undo!"))
636 (or (and (eq last-command 'undo)
637 (eq (current-buffer) last-undo-buffer))
638 (progn (undo-start)
639 (undo-more 1)))
640 (undo-more (or arg 1))
641 ;; Don't specify a position in the undo record for the undo command.
642 ;; Instead, undoing this should move point to where the change is.
643 (let ((tail buffer-undo-list)
644 done)
645 (while (and tail (not done) (not (null (car tail))))
646 (if (integerp (car tail))
647 (progn
648 (setq done t)
649 (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
650 (setq tail (cdr tail))))
651 (and modified (not (buffer-modified-p))
652 (delete-auto-save-file-if-necessary recent-save)))
653 ;; If we do get all the way through, make this-command indicate that.
654 (setq this-command 'undo))
655
656 (defvar pending-undo-list nil
657 "Within a run of consecutive undo commands, list remaining to be undone.")
658
659 (defvar last-undo-buffer nil)
660
661 (defun undo-start ()
662 "Set `pending-undo-list' to the front of the undo list.
663 The next call to `undo-more' will undo the most recently made change."
664 (if (eq buffer-undo-list t)
665 (error "No undo information in this buffer"))
666 (setq pending-undo-list buffer-undo-list))
667
668 (defun undo-more (count)
669 "Undo back N undo-boundaries beyond what was already undone recently.
670 Call `undo-start' to get ready to undo recent changes,
671 then call `undo-more' one or more times to undo them."
672 (or pending-undo-list
673 (error "No further undo information"))
674 (setq pending-undo-list (primitive-undo count pending-undo-list)
675 last-undo-buffer (current-buffer)))
676
677 (defun call-with-transparent-undo (fn &rest args)
678 "Apply FN to ARGS, and then undo all changes made by FN to the current
679 buffer. The undo records are processed even if FN returns non-locally.
680 There is no trace of the changes made by FN in the buffer's undo history.
681
682 You can use this in a write-file-hooks function with continue-save-buffer
683 to make the contents of a disk file differ from its in-memory buffer."
684 (let ((buffer-undo-list nil)
685 ;; Kludge to prevent undo list truncation:
686 (undo-high-threshold -1)
687 (undo-threshold -1)
688 (obuffer (current-buffer)))
689 (unwind-protect
690 (apply fn args)
691 ;; Go to the buffer we will restore and make it writable:
692 (set-buffer obuffer)
693 (save-excursion
694 (let ((buffer-read-only nil))
695 (save-restriction
696 (widen)
697 ;; Perform all undos, with further undo logging disabled:
698 (let ((tail buffer-undo-list))
699 (setq buffer-undo-list t)
700 (while tail
701 (setq tail (primitive-undo (length tail) tail))))))))))
702
703
704 (defconst universal-argument-map
705 (let ((map (make-sparse-keymap)))
706 (set-keymap-default-binding map 'universal-argument-other-key)
707 ;FSFmacs (define-key map [switch-frame] nil)
708 (define-key map [(control u)] 'universal-argument-more)
709 (define-key map ?- 'universal-argument-minus)
710 (define-key map ?0 'digit-argument)
711 (define-key map ?1 'digit-argument)
712 (define-key map ?2 'digit-argument)
713 (define-key map ?3 'digit-argument)
714 (define-key map ?4 'digit-argument)
715 (define-key map ?5 'digit-argument)
716 (define-key map ?6 'digit-argument)
717 (define-key map ?7 'digit-argument)
718 (define-key map ?8 'digit-argument)
719 (define-key map ?9 'digit-argument)
720 map)
721 "Keymap used while processing \\[universal-argument].")
722
723 (defvar universal-argument-num-events nil
724 "Number of argument-specifying events read by `universal-argument'.
725 `universal-argument-other-key' uses this to discard those events
726 from (this-command-keys), and reread only the final command.")
727
728 (defun universal-argument ()
729 "Begin a numeric argument for the following command.
730 Digits or minus sign following \\[universal-argument] make up the numeric argument.
731 \\[universal-argument] following the digits or minus sign ends the argument.
732 \\[universal-argument] without digits or minus sign provides 4 as argument.
733 Repeating \\[universal-argument] without digits or minus sign
734 multiplies the argument by 4 each time."
735 (interactive)
736 (setq prefix-arg (list 4))
737 (setq zmacs-region-stays t)
738 (setq universal-argument-num-events (length (this-command-keys)))
739 (setq overriding-terminal-local-map universal-argument-map))
740
741 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
742 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
743 (defun universal-argument-more (arg)
744 (interactive "P")
745 (if (consp arg)
746 (setq prefix-arg (list (* 4 (car arg))))
747 (setq prefix-arg arg)
748 (setq overriding-terminal-local-map nil))
749 (setq zmacs-region-stays t)
750 (setq universal-argument-num-events (length (this-command-keys))))
751
752 (defun negative-argument (arg)
753 "Begin a negative numeric argument for the next command.
754 \\[universal-argument] following digits or minus sign ends the argument."
755 (interactive "P")
756 (cond ((integerp arg)
757 (setq prefix-arg (- arg)))
758 ((eq arg '-)
759 (setq prefix-arg nil))
760 (t
761 (setq prefix-arg '-)))
762 (setq zmacs-region-stays t)
763 (setq universal-argument-num-events (length (this-command-keys)))
764 (setq overriding-terminal-local-map universal-argument-map))
765
766 (defun digit-argument (arg)
767 "Part of the numeric argument for the next command.
768 \\[universal-argument] following digits or minus sign ends the argument."
769 (interactive "P")
770 (let* ((event last-command-event)
771 (key (and (key-press-event-p event)
772 (event-key event)))
773 (digit (and key (characterp key) (>= key ?0) (<= key ?9)
774 (- key ?0))))
775 (if (null digit)
776 (universal-argument-other-key arg)
777 (cond ((integerp arg)
778 (setq prefix-arg (+ (* arg 10)
779 (if (< arg 0) (- digit) digit))))
780 ((eq arg '-)
781 ;; Treat -0 as just -, so that -01 will work.
782 (setq prefix-arg (if (zerop digit) '- (- digit))))
783 (t
784 (setq prefix-arg digit)))
785 (setq zmacs-region-stays t)
786 (setq universal-argument-num-events (length (this-command-keys)))
787 (setq overriding-terminal-local-map universal-argument-map))))
788
789 ;; For backward compatibility, minus with no modifiers is an ordinary
790 ;; command if digits have already been entered.
791 (defun universal-argument-minus (arg)
792 (interactive "P")
793 (if (integerp arg)
794 (universal-argument-other-key arg)
795 (negative-argument arg)))
796
797 ;; Anything else terminates the argument and is left in the queue to be
798 ;; executed as a command.
799 (defun universal-argument-other-key (arg)
800 (interactive "P")
801 (setq prefix-arg arg)
802 (setq zmacs-region-stays t)
803 (let* ((key (this-command-keys))
804 ;; FSF calls silly function `listify-key-sequence' here.
805 (keylist (append key nil)))
806 (setq unread-command-events
807 (append (nthcdr universal-argument-num-events keylist)
808 unread-command-events)))
809 (reset-this-command-lengths)
810 (setq overriding-terminal-local-map nil))
811
812
813 (defun forward-to-indentation (arg)
814 "Move forward ARG lines and position at first nonblank character."
815 (interactive "p")
816 (forward-line arg)
817 (skip-chars-forward " \t"))
818
819 (defun backward-to-indentation (arg)
820 "Move backward ARG lines and position at first nonblank character."
821 (interactive "p")
822 (forward-line (- arg))
823 (skip-chars-forward " \t"))
824
825 (defvar kill-whole-line nil
826 "*If non-nil, `kill-line' with no arg at beg of line kills the whole line.")
827
828 (defun kill-line (&optional arg)
829 "Kill the rest of the current line; if no nonblanks there, kill thru newline.
830 With prefix argument, kill that many lines from point.
831 Negative arguments kill lines backward.
832
833 When calling from a program, nil means \"no arg\",
834 a number counts as a prefix arg.
835
836 If `kill-whole-line' is non-nil, then kill the whole line
837 when given no argument at the beginning of a line."
838 (interactive "*P")
839 (kill-region (point)
840 ;; Don't shift point before doing the delete; that way,
841 ;; undo will record the right position of point.
842 (save-excursion
843 (if arg
844 (forward-line (prefix-numeric-value arg))
845 (if (eobp)
846 (signal 'end-of-buffer nil))
847 (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
848 (forward-line 1)
849 (end-of-line)))
850 (point))))
851
852 (defun backward-kill-line nil
853 "Kill back to the beginning of the line."
854 (interactive)
855 (let ((point (point)))
856 (beginning-of-line nil)
857 (kill-region (point) point)))
858
859
860 ;;;; Window system cut and paste hooks.
861 ;;;
862 ;;; I think that kill-hooks is a better name and more general mechanism
863 ;;; than interprogram-cut-function (from FSFmacs). I don't like the behavior
864 ;;; of interprogram-paste-function: ^Y should always come from the kill ring,
865 ;;; not the X selection. But if that were provided, it should be called (and
866 ;;; behave as) yank-hooks instead. -- jwz
867
868 ;(defvar interprogram-cut-function nil
869 ; "Function to call to make a killed region available to other programs.
870 ;
871 ;Most window systems provide some sort of facility for cutting and
872 ;pasting text between the windows of different programs.
873 ;This variable holds a function that XEmacs calls whenever text
874 ;is put in the kill ring, to make the new kill available to other
875 ;programs.
876 ;
877 ;The function takes one or two arguments.
878 ;The first argument, TEXT, is a string containing
879 ;the text which should be made available.
880 ;The second, PUSH, if non-nil means this is a \"new\" kill;
881 ;nil means appending to an \"old\" kill.")
882 ;
883 ;(defvar interprogram-paste-function nil
884 ; "Function to call to get text cut from other programs.
885 ;
886 ;Most window systems provide some sort of facility for cutting and
887 ;pasting text between the windows of different programs.
888 ;This variable holds a function that Emacs calls to obtain
889 ;text that other programs have provided for pasting.
890 ;
891 ;The function should be called with no arguments. If the function
892 ;returns nil, then no other program has provided such text, and the top
893 ;of the Emacs kill ring should be used. If the function returns a
894 ;string, that string should be put in the kill ring as the latest kill.
895 ;
896 ;Note that the function should return a string only if a program other
897 ;than Emacs has provided a string for pasting; if Emacs provided the
898 ;most recent string, the function should return nil. If it is
899 ;difficult to tell whether Emacs or some other program provided the
900 ;current string, it is probably good enough to return nil if the string
901 ;is equal (according to `string=') to the last text Emacs provided.")
902
903 (defvar kill-hooks nil
904 "Functions run when something is added to the XEmacs kill ring.
905 These functions are called with one argument, the string most recently
906 cut or copied. You can use this to, for example, make the most recent
907 kill become the X Clipboard selection.")
908
909
910 ;;;; The kill ring data structure.
911
912 (defvar kill-ring nil
913 "List of killed text sequences.
914 In order to maintain correct interaction with cut-and-paste facilities
915 offered by window systems, the functions `kill-new', `kill-append', and
916 `current-kill' should be used to access the kill ring, instead of using
917 this variable directly.")
918
919 (defvar kill-ring-max 30
920 "*Maximum length of kill ring before oldest elements are thrown away.")
921
922 (defvar kill-ring-yank-pointer nil
923 "The tail of the kill ring whose car is the last thing yanked.")
924
925 (defun kill-new (string &optional replace)
926 "Make STRING the latest kill in the kill ring.
927 Set the kill-ring-yank pointer to point to it.
928 Run `kill-hooks'.
929 Optional second argument REPLACE non-nil means that STRING will replace
930 the front of the kill ring, rather than being added to the list."
931 ; (and (fboundp 'menu-bar-update-yank-menu)
932 ; (menu-bar-update-yank-menu string (and replace (car kill-ring))))
933 (if replace
934 (setcar kill-ring string)
935 (setq kill-ring (cons string kill-ring))
936 (if (> (length kill-ring) kill-ring-max)
937 (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
938 (setq kill-ring-yank-pointer kill-ring)
939 ; (if interprogram-cut-function
940 ; (funcall interprogram-cut-function string (not replace)))
941 (run-hook-with-args 'kill-hooks string))
942
943 (defun kill-append (string before-p)
944 "Append STRING to the end of the latest kill in the kill ring.
945 If BEFORE-P is non-nil, prepend STRING to the kill.
946 Run `kill-hooks'."
947 (kill-new (if before-p
948 (concat string (car kill-ring))
949 (concat (car kill-ring) string)) t))
950
951 (defun current-kill (n &optional do-not-move)
952 "Rotate the yanking point by N places, and then return that kill.
953 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
954 yanking point\; just return the Nth kill forward."
955 (or kill-ring (error "Kill ring is empty"))
956 (let* ((tem (nthcdr (mod (- n (length kill-ring-yank-pointer))
957 (length kill-ring))
958 kill-ring)))
959 (or do-not-move
960 (setq kill-ring-yank-pointer tem))
961 (car tem)))
962
963
964
965 ;;;; Commands for manipulating the kill ring.
966
967 ;;FSFmacs
968 ;(defvar kill-read-only-ok nil
969 ; "*Non-nil means don't signal an error for killing read-only text.")
970
971 (defun kill-region (beg end &optional verbose) ; verbose is XEmacs addition
972 "Kill between point and mark.
973 The text is deleted but saved in the kill ring.
974 The command \\[yank] can retrieve it from there.
975 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
976
977 This is the primitive for programs to kill text (as opposed to deleting it).
978 Supply two arguments, character numbers indicating the stretch of text
979 to be killed.
980 Any command that calls this function is a \"kill command\".
981 If the previous command was also a kill command,
982 the text killed this time appends to the text killed last time
983 to make one entry in the kill ring."
984 (interactive "*r\np")
985 ; (interactive
986 ; (let ((region-hack (and zmacs-regions (eq last-command 'yank))))
987 ; ;; This lets "^Y^W" work. I think this is dumb, but zwei did it.
988 ; (if region-hack (zmacs-activate-region))
989 ; (prog1
990 ; (list (point) (mark) current-prefix-arg)
991 ; (if region-hack (zmacs-deactivate-region)))))
992 ;; beg and end can be markers but the rest of this function is
993 ;; written as if they are only integers
994 (if (markerp beg) (setq beg (marker-position beg)))
995 (if (markerp end) (setq end (marker-position end)))
996 (or (and beg end) (if zmacs-regions ;; rewritten for I18N3 snarfing
997 (error "The region is not active now")
998 (error "The mark is not set now")))
999 (if verbose (if buffer-read-only
1000 (message "Copying %d characters"
1001 (- (max beg end) (min beg end)))
1002 (message "Killing %d characters"
1003 (- (max beg end) (min beg end)))))
1004 (cond
1005
1006 ;; I don't like this large change in behavior -- jwz
1007 ;; If the buffer is read-only, we should beep, in case the person
1008 ;; just isn't aware of this. However, there's no harm in putting
1009 ;; the region's text in the kill ring, anyway.
1010 ;;((or (and buffer-read-only (not inhibit-read-only))
1011 ;; (text-property-not-all beg end 'read-only nil))
1012 ;; (if verbose (message "Copying %d characters"
1013 ;; (- (max beg end) (min beg end))))
1014 ;; (copy-region-as-kill beg end)
1015 ;; ;; This should always barf, and give us the correct error.
1016 ;; (if kill-read-only-ok
1017 ;; (message "Read only text copied to kill ring")
1018 ;; (setq this-command 'kill-region)
1019 ;; (barf-if-buffer-read-only)))
1020
1021 ;; In certain cases, we can arrange for the undo list and the kill
1022 ;; ring to share the same string object. This code does that.
1023 ((not (or (eq buffer-undo-list t)
1024 (eq last-command 'kill-region)
1025 ;; Use = since positions may be numbers or markers.
1026 (= beg end)))
1027 ;; Don't let the undo list be truncated before we can even access it.
1028 (let ((undo-high-threshold (+ (- (max beg end) (min beg end)) 100))
1029 ;(old-list buffer-undo-list)
1030 tail)
1031 (delete-region beg end)
1032 ;; Search back in buffer-undo-list for this string,
1033 ;; in case a change hook made property changes.
1034 (setq tail buffer-undo-list)
1035 (while (not (stringp (car-safe (car-safe tail))))
1036 (setq tail (cdr tail)))
1037 ;; Take the same string recorded for undo
1038 ;; and put it in the kill-ring.
1039 (kill-new (car (car tail)))))
1040
1041 (t
1042 ;; if undo is not kept, grab the string then delete it (which won't
1043 ;; add another string to the undo list).
1044 (copy-region-as-kill beg end)
1045 (delete-region beg end)))
1046 (setq this-command 'kill-region))
1047
1048 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1049 ;; to get two copies of the text when the user accidentally types M-w and
1050 ;; then corrects it with the intended C-w.
1051 (defun copy-region-as-kill (beg end)
1052 "Save the region as if killed, but don't kill it.
1053 Run `kill-hooks'."
1054 (interactive "r")
1055 (if (eq last-command 'kill-region)
1056 (kill-append (buffer-substring beg end) (< end beg))
1057 (kill-new (buffer-substring beg end)))
1058 nil)
1059
1060 (defun kill-ring-save (beg end)
1061 "Save the region as if killed, but don't kill it.
1062 This command is similar to `copy-region-as-kill', except that it gives
1063 visual feedback indicating the extent of the region being copied."
1064 (interactive "r")
1065 (copy-region-as-kill beg end)
1066 ;; copy before delay, for xclipboard's benefit
1067 (if (interactive-p)
1068 (let ((other-end (if (= (point) beg) end beg))
1069 (opoint (point))
1070 ;; Inhibit quitting so we can make a quit here
1071 ;; look like a C-g typed as a command.
1072 (inhibit-quit t))
1073 (if (pos-visible-in-window-p other-end (selected-window))
1074 (progn
1075 (goto-char other-end)
1076 (sit-for 1)
1077 (goto-char opoint)
1078 ;; If user quit, deactivate the mark
1079 ;; as C-g would as a command.
1080 (and quit-flag (mark)
1081 (zmacs-deactivate-region)))
1082 ;; too noisy. -- jwz
1083 ; (let* ((killed-text (current-kill 0))
1084 ; (message-len (min (length killed-text) 40)))
1085 ; (if (= (point) beg)
1086 ; ;; Don't say "killed"; that is misleading.
1087 ; (message "Saved text until \"%s\""
1088 ; (substring killed-text (- message-len)))
1089 ; (message "Saved text from \"%s\""
1090 ; (substring killed-text 0 message-len))))
1091 ))))
1092
1093 (defun append-next-kill ()
1094 "Cause following command, if it kills, to append to previous kill."
1095 (interactive "_")
1096 (if (interactive-p)
1097 (progn
1098 (setq this-command 'kill-region)
1099 (message "If the next command is a kill, it will append"))
1100 (setq last-command 'kill-region)))
1101
1102 (defun yank-pop (arg)
1103 "Replace just-yanked stretch of killed text with a different stretch.
1104 This command is allowed only immediately after a `yank' or a `yank-pop'.
1105 At such a time, the region contains a stretch of reinserted
1106 previously-killed text. `yank-pop' deletes that text and inserts in its
1107 place a different stretch of killed text.
1108
1109 With no argument, the previous kill is inserted.
1110 With argument N, insert the Nth previous kill.
1111 If N is negative, this is a more recent kill.
1112
1113 The sequence of kills wraps around, so that after the oldest one
1114 comes the newest one."
1115 (interactive "*p")
1116 (if (not (eq last-command 'yank))
1117 (error "Previous command was not a yank"))
1118 (setq this-command 'yank)
1119 (let ((before (< (point) (mark t))))
1120 (delete-region (point) (mark t))
1121 (set-mark (point))
1122 (insert (current-kill arg))
1123 (if before (exchange-point-and-mark t))))
1124
1125 (defun yank (&optional arg)
1126 "Reinsert the last stretch of killed text.
1127 More precisely, reinsert the stretch of killed text most recently
1128 killed OR yanked. Put point at end, and set mark at beginning.
1129 With just C-u as argument, same but put point at beginning (and mark at end).
1130 With argument N, reinsert the Nth most recently killed stretch of killed text.
1131 See also the command \\[yank-pop]."
1132 (interactive "*P")
1133 ;; If we don't get all the way through, make last-command indicate that
1134 ;; for the following command.
1135 (setq this-command t)
1136 (push-mark (point))
1137 (insert (current-kill (cond
1138 ((listp arg) 0)
1139 ((eq arg '-) -1)
1140 (t (1- arg)))))
1141 (if (consp arg)
1142 (exchange-point-and-mark t))
1143 ;; If we do get all the way through, make this-command indicate that.
1144 (setq this-command 'yank))
1145
1146 (defun rotate-yank-pointer (arg)
1147 "Rotate the yanking point in the kill ring.
1148 With argument, rotate that many kills forward (or backward, if negative)."
1149 (interactive "p")
1150 (current-kill arg))
1151
1152
1153 (defun insert-buffer (buffer)
1154 "Insert after point the contents of BUFFER.
1155 Puts mark after the inserted text.
1156 BUFFER may be a buffer or a buffer name."
1157 (interactive (list (progn (barf-if-buffer-read-only)
1158 (read-buffer "Insert buffer: "
1159 ;; XEmacs: we have different args
1160 (other-buffer (current-buffer) nil t)
1161 t))))
1162 (or (bufferp buffer)
1163 (setq buffer (get-buffer buffer)))
1164 (let (start end newmark)
1165 (save-excursion
1166 (save-excursion
1167 (set-buffer buffer)
1168 (setq start (point-min) end (point-max)))
1169 (insert-buffer-substring buffer start end)
1170 (setq newmark (point)))
1171 (push-mark newmark))
1172 nil)
1173
1174 (defun append-to-buffer (buffer start end)
1175 "Append to specified buffer the text of the region.
1176 It is inserted into that buffer before its point.
1177
1178 When calling from a program, give three arguments:
1179 BUFFER (or buffer name), START and END.
1180 START and END specify the portion of the current buffer to be copied."
1181 (interactive
1182 ;; XEmacs: we have different args to other-buffer
1183 (list (read-buffer "Append to buffer: " (other-buffer (current-buffer)
1184 nil t))
1185 (region-beginning) (region-end)))
1186 (let ((oldbuf (current-buffer)))
1187 (save-excursion
1188 (set-buffer (get-buffer-create buffer))
1189 (insert-buffer-substring oldbuf start end))))
1190
1191 (defun prepend-to-buffer (buffer start end)
1192 "Prepend to specified buffer the text of the region.
1193 It is inserted into that buffer after its point.
1194
1195 When calling from a program, give three arguments:
1196 BUFFER (or buffer name), START and END.
1197 START and END specify the portion of the current buffer to be copied."
1198 (interactive "BPrepend to buffer: \nr")
1199 (let ((oldbuf (current-buffer)))
1200 (save-excursion
1201 (set-buffer (get-buffer-create buffer))
1202 (save-excursion
1203 (insert-buffer-substring oldbuf start end)))))
1204
1205 (defun copy-to-buffer (buffer start end)
1206 "Copy to specified buffer the text of the region.
1207 It is inserted into that buffer, replacing existing text there.
1208
1209 When calling from a program, give three arguments:
1210 BUFFER (or buffer name), START and END.
1211 START and END specify the portion of the current buffer to be copied."
1212 (interactive "BCopy to buffer: \nr")
1213 (let ((oldbuf (current-buffer)))
1214 (save-excursion
1215 (set-buffer (get-buffer-create buffer))
1216 (erase-buffer)
1217 (save-excursion
1218 (insert-buffer-substring oldbuf start end)))))
1219
1220 ;FSFmacs
1221 ;(define-error 'mark-inactive "The mark is not active now")
1222
1223 (defun mark (&optional force buffer)
1224 "Return this buffer's mark value as integer, or nil if no mark.
1225
1226 If `zmacs-regions' is true, then this returns nil unless the region is
1227 currently in the active (highlighted) state. With an argument of t, this
1228 returns the mark (if there is one) regardless of the active-region state.
1229 You should *generally* not use the mark unless the region is active, if
1230 the user has expressed a preference for the active-region model.
1231
1232 If you are using this in an editing command, you are most likely making
1233 a mistake; see the documentation of `set-mark'."
1234 (setq buffer (decode-buffer buffer))
1235 ;FSFmacs version:
1236 ; (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
1237 ; (marker-position (mark-marker))
1238 ; (signal 'mark-inactive nil)))
1239 (let ((m (mark-marker force buffer)))
1240 (and m (marker-position m))))
1241
1242 ;;;#### FSFmacs
1243 ;;; Many places set mark-active directly, and several of them failed to also
1244 ;;; run deactivate-mark-hook. This shorthand should simplify.
1245 ;(defsubst deactivate-mark ()
1246 ; "Deactivate the mark by setting `mark-active' to nil.
1247 ;\(That makes a difference only in Transient Mark mode.)
1248 ;Also runs the hook `deactivate-mark-hook'."
1249 ; (if transient-mark-mode
1250 ; (progn
1251 ; (setq mark-active nil)
1252 ; (run-hooks 'deactivate-mark-hook))))
1253
1254 (defun set-mark (pos &optional buffer)
1255 "Set this buffer's mark to POS. Don't use this function!
1256 That is to say, don't use this function unless you want
1257 the user to see that the mark has moved, and you want the previous
1258 mark position to be lost.
1259
1260 Normally, when a new mark is set, the old one should go on the stack.
1261 This is why most applications should use push-mark, not set-mark.
1262
1263 Novice Emacs Lisp programmers often try to use the mark for the wrong
1264 purposes. The mark saves a location for the user's convenience.
1265 Most editing commands should not alter the mark.
1266 To remember a location for internal use in the Lisp program,
1267 store it in a Lisp variable. Example:
1268
1269 (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1270
1271 (setq buffer (decode-buffer buffer))
1272 (set-marker (mark-marker t buffer) pos buffer))
1273
1274 (defvar mark-ring nil
1275 "The list of former marks of the current buffer, most recent first.")
1276 (make-variable-buffer-local 'mark-ring)
1277 (put 'mark-ring 'permanent-local t)
1278
1279 (defvar mark-ring-max 16
1280 "*Maximum size of mark ring. Start discarding off end if gets this big.")
1281
1282 (defvar global-mark-ring nil
1283 "The list of saved global marks, most recent first.")
1284
1285 (defconst global-mark-ring-max 16
1286 "*Maximum size of global mark ring. \
1287 Start discarding off end if gets this big.")
1288
1289 (defun set-mark-command (arg)
1290 "Set mark at where point is, or jump to mark.
1291 With no prefix argument, set mark, push old mark position on local mark
1292 ring, and push mark on global mark ring.
1293 With argument, jump to mark, and pop a new position for mark off the ring
1294 \(does not affect global mark ring\).
1295
1296 Novice Emacs Lisp programmers often try to use the mark for the wrong
1297 purposes. See the documentation of `set-mark' for more information."
1298 (interactive "P")
1299 (if (null arg)
1300 (push-mark nil nil t)
1301 (if (null (mark t))
1302 (error "No mark set in this buffer")
1303 (goto-char (mark t))
1304 (pop-mark))))
1305
1306 (defun push-mark (&optional location nomsg activate-region buffer)
1307 "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1308 If the last global mark pushed was not in the current buffer,
1309 also push LOCATION on the global mark ring.
1310 Display `Mark set' unless the optional second arg NOMSG is non-nil.
1311 Activate mark if optional third arg ACTIVATE-REGION non-nil.
1312
1313 Novice Emacs Lisp programmers often try to use the mark for the wrong
1314 purposes. See the documentation of `set-mark' for more information."
1315 (setq buffer (decode-buffer buffer))
1316 (if (null (mark t buffer))
1317 nil
1318 ;; The save-excursion / set-buffer is necessary because mark-ring
1319 ;; is a buffer local variable
1320 (save-excursion
1321 (set-buffer buffer)
1322 (setq mark-ring (cons (copy-marker (mark-marker t buffer)) mark-ring))
1323 (if (> (length mark-ring) mark-ring-max)
1324 (progn
1325 (move-marker (car (nthcdr mark-ring-max mark-ring)) nil buffer)
1326 (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))))
1327 (set-mark (or location (point buffer)) buffer)
1328 ;; Now push the mark on the global mark ring.
1329 (if (or (null global-mark-ring)
1330 (not (eq (marker-buffer (car global-mark-ring)) buffer)))
1331 ;; The last global mark pushed wasn't in this same buffer.
1332 (progn
1333 (setq global-mark-ring (cons (copy-marker (mark-marker t buffer))
1334 global-mark-ring))
1335 (if (> (length global-mark-ring) global-mark-ring-max)
1336 (progn
1337 (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
1338 nil buffer)
1339 (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))))
1340 (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
1341 (message "Mark set"))
1342 (if activate-region
1343 (progn
1344 (setq zmacs-region-stays t)
1345 (zmacs-activate-region)))
1346 nil)
1347
1348 (defun pop-mark ()
1349 "Pop off mark ring into the buffer's actual mark.
1350 Does not set point. Does nothing if mark ring is empty."
1351 (if mark-ring
1352 (progn
1353 (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker t)))))
1354 (set-mark (car mark-ring))
1355 (move-marker (car mark-ring) nil)
1356 (if (null (mark t)) (ding))
1357 (setq mark-ring (cdr mark-ring)))))
1358
1359 (define-function 'exchange-dot-and-mark 'exchange-point-and-mark)
1360 (defun exchange-point-and-mark (&optional dont-activate-region)
1361 "Put the mark where point is now, and point where the mark is now.
1362 The mark is activated unless DONT-ACTIVATE-REGION is non-nil."
1363 (interactive nil)
1364 (let ((omark (mark t)))
1365 (if (null omark)
1366 (error "No mark set in this buffer"))
1367 (set-mark (point))
1368 (goto-char omark)
1369 (or dont-activate-region (zmacs-activate-region))
1370 nil))
1371
1372 (defun mark-something (mark-fn movement-fn arg)
1373 "internal function used by mark-sexp, mark-word, etc."
1374 (let (newmark (pushp t))
1375 (save-excursion
1376 (if (and (eq last-command mark-fn) (mark))
1377 ;; Extend the previous state in the same direction:
1378 (progn
1379 (if (< (mark) (point)) (setq arg (- arg)))
1380 (goto-char (mark))
1381 (setq pushp nil)))
1382 (funcall movement-fn arg)
1383 (setq newmark (point)))
1384 (if pushp
1385 (push-mark newmark nil t)
1386 ;; Do not mess with the mark stack, but merely adjust the previous state:
1387 (set-mark newmark)
1388 (activate-region))))
1389
1390 ;(defun transient-mark-mode (arg)
1391 ; "Toggle Transient Mark mode.
1392 ;With arg, turn Transient Mark mode on if arg is positive, off otherwise.
1393 ;
1394 ;In Transient Mark mode, when the mark is active, the region is highlighted.
1395 ;Changing the buffer \"deactivates\" the mark.
1396 ;So do certain other operations that set the mark
1397 ;but whose main purpose is something else--for example,
1398 ;incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
1399 ; (interactive "P")
1400 ; (setq transient-mark-mode
1401 ; (if (null arg)
1402 ; (not transient-mark-mode)
1403 ; (> (prefix-numeric-value arg) 0))))
1404
1405 (defun pop-global-mark ()
1406 "Pop off global mark ring and jump to the top location."
1407 (interactive)
1408 ;; Pop entries which refer to non-existent buffers.
1409 (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
1410 (setq global-mark-ring (cdr global-mark-ring)))
1411 (or global-mark-ring
1412 (error "No global mark set"))
1413 (let* ((marker (car global-mark-ring))
1414 (buffer (marker-buffer marker))
1415 (position (marker-position marker)))
1416 (setq global-mark-ring (nconc (cdr global-mark-ring)
1417 (list (car global-mark-ring))))
1418 (set-buffer buffer)
1419 (or (and (>= position (point-min))
1420 (<= position (point-max)))
1421 (widen))
1422 (goto-char position)
1423 (switch-to-buffer buffer)))
1424
1425
1426 (defvar next-line-add-newlines t
1427 "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error.")
1428
1429 (defun next-line (arg)
1430 "Move cursor vertically down ARG lines.
1431 If there is no character in the target line exactly under the current column,
1432 the cursor is positioned after the character in that line which spans this
1433 column, or at the end of the line if it is not long enough.
1434
1435 If there is no line in the buffer after this one, behavior depends on the
1436 value of `next-line-add-newlines'. If non-nil, it inserts a newline character
1437 to create a line, and moves the cursor to that line. Otherwise it moves the
1438 cursor to the end of the buffer.
1439
1440 The command \\[set-goal-column] can be used to create
1441 a semipermanent goal column to which this command always moves.
1442 Then it does not try to move vertically. This goal column is stored
1443 in `goal-column', which is nil when there is none.
1444
1445 If you are thinking of using this in a Lisp program, consider
1446 using `forward-line' instead. It is usually easier to use
1447 and more reliable (no dependence on goal column, etc.)."
1448 (interactive "_p")
1449 (if (and next-line-add-newlines (= arg 1))
1450 (let ((opoint (point)))
1451 (end-of-line)
1452 (if (eobp)
1453 (newline 1)
1454 (goto-char opoint)
1455 (line-move arg)))
1456 (if (interactive-p)
1457 (condition-case nil
1458 (line-move arg)
1459 ((beginning-of-buffer end-of-buffer) (ding nil 'buffer-bound)))
1460 (line-move arg)))
1461 nil)
1462
1463 (defun previous-line (arg)
1464 "Move cursor vertically up ARG lines.
1465 If there is no character in the target line exactly over the current column,
1466 the cursor is positioned after the character in that line which spans this
1467 column, or at the end of the line if it is not long enough.
1468
1469 The command \\[set-goal-column] can be used to create
1470 a semipermanent goal column to which this command always moves.
1471 Then it does not try to move vertically.
1472
1473 If you are thinking of using this in a Lisp program, consider using
1474 `forward-line' with a negative argument instead. It is usually easier
1475 to use and more reliable (no dependence on goal column, etc.)."
1476 (interactive "_p")
1477 (if (interactive-p)
1478 (condition-case nil
1479 (line-move (- arg))
1480 ((beginning-of-buffer end-of-buffer) (ding nil 'buffer-bound)))
1481 (line-move (- arg)))
1482 nil)
1483
1484 (defvar track-eol nil
1485 "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1486 This means moving to the end of each line moved onto.
1487 The beginning of a blank line does not count as the end of a line.")
1488
1489 (defvar goal-column nil
1490 "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.")
1491 (make-variable-buffer-local 'goal-column)
1492
1493 (defvar temporary-goal-column 0
1494 "Current goal column for vertical motion.
1495 It is the column where point was
1496 at the start of current run of vertical motion commands.
1497 When the `track-eol' feature is doing its job, the value is 9999.")
1498
1499 ;XEmacs: not yet ported, so avoid compiler warnings
1500 (eval-when-compile
1501 (defvar inhibit-point-motion-hooks))
1502
1503 (defvar line-move-ignore-invisible nil
1504 "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
1505 Outline mode sets this.")
1506
1507 ;; This is the guts of next-line and previous-line.
1508 ;; Arg says how many lines to move.
1509 (defun line-move (arg)
1510 ;; Don't run any point-motion hooks, and disregard intangibility,
1511 ;; for intermediate positions.
1512 (let ((inhibit-point-motion-hooks t)
1513 (opoint (point))
1514 new)
1515 (unwind-protect
1516 (progn
1517 (if (not (or (eq last-command 'next-line)
1518 (eq last-command 'previous-line)))
1519 (setq temporary-goal-column
1520 (if (and track-eol (eolp)
1521 ;; Don't count beg of empty line as end of line
1522 ;; unless we just did explicit end-of-line.
1523 (or (not (bolp)) (eq last-command 'end-of-line)))
1524 9999
1525 (current-column))))
1526 (if (and (not (integerp selective-display))
1527 (not line-move-ignore-invisible))
1528 ;; Use just newline characters.
1529 (or (if (> arg 0)
1530 (progn (if (> arg 1) (forward-line (1- arg)))
1531 ;; This way of moving forward ARG lines
1532 ;; verifies that we have a newline after the last one.
1533 ;; It doesn't get confused by intangible text.
1534 (end-of-line)
1535 (zerop (forward-line 1)))
1536 (and (zerop (forward-line arg))
1537 (bolp)))
1538 (signal (if (< arg 0)
1539 'beginning-of-buffer
1540 'end-of-buffer)
1541 nil))
1542 ;; Move by arg lines, but ignore invisible ones.
1543 (while (> arg 0)
1544 (end-of-line)
1545 (and (zerop (vertical-motion 1))
1546 (signal 'end-of-buffer nil))
1547 ;; If the following character is currently invisible,
1548 ;; skip all characters with that same `invisible' property value.
1549 (while (and (not (eobp))
1550 (let ((prop
1551 (get-char-property (point) 'invisible)))
1552 (if (eq buffer-invisibility-spec t)
1553 prop
1554 (or (memq prop buffer-invisibility-spec)
1555 (assq prop buffer-invisibility-spec)))))
1556 (if (get-text-property (point) 'invisible)
1557 (goto-char (next-single-property-change (point) 'invisible))
1558 (goto-char (next-extent-change (point)))))
1559 (setq arg (1- arg)))
1560 (while (< arg 0)
1561 (beginning-of-line)
1562 (and (zerop (vertical-motion -1))
1563 (signal 'beginning-of-buffer nil))
1564 (while (and (not (bobp))
1565 (let ((prop
1566 (get-char-property (1- (point)) 'invisible)))
1567 (if (eq buffer-invisibility-spec t)
1568 prop
1569 (or (memq prop buffer-invisibility-spec)
1570 (assq prop buffer-invisibility-spec)))))
1571 (if (get-text-property (1- (point)) 'invisible)
1572 (goto-char (previous-single-property-change (point) 'invisible))
1573 (goto-char (previous-extent-change (point)))))
1574 (setq arg (1+ arg))))
1575 (move-to-column (or goal-column temporary-goal-column)))
1576 ;; Remember where we moved to, go back home,
1577 ;; then do the motion over again
1578 ;; in just one step, with intangibility and point-motion hooks
1579 ;; enabled this time.
1580 (setq new (point))
1581 (goto-char opoint)
1582 (setq inhibit-point-motion-hooks nil)
1583 (goto-char new)))
1584 nil)
1585
1586 ;;; Many people have said they rarely use this feature, and often type
1587 ;;; it by accident. Maybe it shouldn't even be on a key.
1588 (put 'set-goal-column 'disabled t)
1589
1590 (defun set-goal-column (arg)
1591 "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
1592 Those commands will move to this position in the line moved to
1593 rather than trying to keep the same horizontal position.
1594 With a non-nil argument, clears out the goal column
1595 so that \\[next-line] and \\[previous-line] resume vertical motion.
1596 The goal column is stored in the variable `goal-column'."
1597 (interactive "_P")
1598 (if arg
1599 (progn
1600 (setq goal-column nil)
1601 (message "No goal column"))
1602 (setq goal-column (current-column))
1603 (message (substitute-command-keys
1604 "Goal column %d (use \\[set-goal-column] with an arg to unset it)")
1605 goal-column))
1606 nil)
1607
1608
1609 ;;; deleted FSFmacs terminal randomness hscroll-point-visible stuff.
1610
1611 (defun scroll-other-window-down (lines)
1612 "Scroll the \"other window\" down.
1613 For more details, see the documentation for `scroll-other-window'."
1614 (interactive "P")
1615 (scroll-other-window
1616 ;; Just invert the argument's meaning.
1617 ;; We can do that without knowing which window it will be.
1618 (if (eq lines '-) nil
1619 (if (null lines) '-
1620 (- (prefix-numeric-value lines))))))
1621
1622 (defun beginning-of-buffer-other-window (arg)
1623 "Move point to the beginning of the buffer in the other window.
1624 Leave mark at previous position.
1625 With arg N, put point N/10 of the way from the true beginning."
1626 (interactive "P")
1627 (let ((orig-window (selected-window))
1628 (window (other-window-for-scrolling)))
1629 ;; We use unwind-protect rather than save-window-excursion
1630 ;; because the latter would preserve the things we want to change.
1631 (unwind-protect
1632 (progn
1633 (select-window window)
1634 ;; Set point and mark in that window's buffer.
1635 (beginning-of-buffer arg)
1636 ;; Set point accordingly.
1637 (recenter '(t)))
1638 (select-window orig-window))))
1639
1640 (defun end-of-buffer-other-window (arg)
1641 "Move point to the end of the buffer in the other window.
1642 Leave mark at previous position.
1643 With arg N, put point N/10 of the way from the true end."
1644 (interactive "P")
1645 ;; See beginning-of-buffer-other-window for comments.
1646 (let ((orig-window (selected-window))
1647 (window (other-window-for-scrolling)))
1648 (unwind-protect
1649 (progn
1650 (select-window window)
1651 (end-of-buffer arg)
1652 (recenter '(t)))
1653 (select-window orig-window))))
1654
1655 (defun transpose-chars (arg)
1656 "Interchange characters around point, moving forward one character.
1657 With prefix arg ARG, effect is to take character before point
1658 and drag it forward past ARG other characters (backward if ARG negative).
1659 If no argument and at end of line, the previous two chars are exchanged."
1660 (interactive "*P")
1661 (and (null arg) (eolp) (forward-char -1))
1662 (transpose-subr 'forward-char (prefix-numeric-value arg)))
1663
1664 (defun transpose-words (arg)
1665 "Interchange words around point, leaving point at end of them.
1666 With prefix arg ARG, effect is to take word before or around point
1667 and drag it forward past ARG other words (backward if ARG negative).
1668 If ARG is zero, the words around or after point and around or after mark
1669 are interchanged."
1670 (interactive "*p")
1671 (transpose-subr 'forward-word arg))
1672
1673 (defun transpose-sexps (arg)
1674 "Like \\[transpose-words] but applies to sexps.
1675 Does not work on a sexp that point is in the middle of
1676 if it is a list or string."
1677 (interactive "*p")
1678 (transpose-subr 'forward-sexp arg))
1679
1680 (defun transpose-lines (arg)
1681 "Exchange current line and previous line, leaving point after both.
1682 With argument ARG, takes previous line and moves it past ARG lines.
1683 With argument 0, interchanges line point is in with line mark is in."
1684 (interactive "*p")
1685 (transpose-subr #'(lambda (arg)
1686 (if (= arg 1)
1687 (progn
1688 ;; Move forward over a line,
1689 ;; but create a newline if none exists yet.
1690 (end-of-line)
1691 (if (eobp)
1692 (newline)
1693 (forward-char 1)))
1694 (forward-line arg)))
1695 arg))
1696
1697 (eval-when-compile
1698 ;; avoid byte-compiler warnings...
1699 (defvar start1)
1700 (defvar start2)
1701 (defvar end1)
1702 (defvar end2))
1703
1704 ; start[12] and end[12] used in transpose-subr-1 below
1705 (defun transpose-subr (mover arg)
1706 (let (start1 end1 start2 end2)
1707 (if (= arg 0)
1708 (progn
1709 (save-excursion
1710 (funcall mover 1)
1711 (setq end2 (point))
1712 (funcall mover -1)
1713 (setq start2 (point))
1714 (goto-char (mark t))
1715 (funcall mover 1)
1716 (setq end1 (point))
1717 (funcall mover -1)
1718 (setq start1 (point))
1719 (transpose-subr-1))
1720 (exchange-point-and-mark t)))
1721 (while (> arg 0)
1722 (funcall mover -1)
1723 (setq start1 (point))
1724 (funcall mover 1)
1725 (setq end1 (point))
1726 (funcall mover 1)
1727 (setq end2 (point))
1728 (funcall mover -1)
1729 (setq start2 (point))
1730 (transpose-subr-1)
1731 (goto-char end2)
1732 (setq arg (1- arg)))
1733 (while (< arg 0)
1734 (funcall mover -1)
1735 (setq start2 (point))
1736 (funcall mover -1)
1737 (setq start1 (point))
1738 (funcall mover 1)
1739 (setq end1 (point))
1740 (funcall mover 1)
1741 (setq end2 (point))
1742 (transpose-subr-1)
1743 (setq arg (1+ arg)))))
1744
1745 ; start[12] and end[12] used free
1746 (defun transpose-subr-1 ()
1747 (if (> (min end1 end2) (max start1 start2))
1748 (error "Don't have two things to transpose"))
1749 (let ((word1 (buffer-substring start1 end1))
1750 (word2 (buffer-substring start2 end2)))
1751 (delete-region start2 end2)
1752 (goto-char start2)
1753 (insert word1)
1754 (goto-char (if (< start1 start2) start1
1755 (+ start1 (- (length word1) (length word2)))))
1756 (delete-char (length word1))
1757 (insert word2)))
1758
1759 (defvar comment-column 32
1760 "*Column to indent right-margin comments to.
1761 Setting this variable automatically makes it local to the current buffer.
1762 Each mode establishes a different default value for this variable; you
1763 can set the value for a particular mode using that mode's hook.")
1764 (make-variable-buffer-local 'comment-column)
1765
1766 (defvar comment-start nil
1767 "*String to insert to start a new comment, or nil if no comment syntax.")
1768
1769 (defvar comment-start-skip nil
1770 "*Regexp to match the start of a comment plus everything up to its body.
1771 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
1772 at the place matched by the close of the first pair.")
1773
1774 (defvar comment-end ""
1775 "*String to insert to end a new comment.
1776 Should be an empty string if comments are terminated by end-of-line.")
1777
1778 (defconst comment-indent-hook nil
1779 "Obsolete variable for function to compute desired indentation for a comment.
1780 Use `comment-indent-function' instead.
1781 This function is called with no args with point at the beginning of
1782 the comment's starting delimiter.")
1783
1784 (defvar comment-indent-function
1785 ;; XEmacs - add at least one space after the end of the text on the
1786 ;; current line...
1787 #'(lambda ()
1788 (save-excursion
1789 (beginning-of-line)
1790 (let ((eol (save-excursion (end-of-line) (point))))
1791 (and comment-start-skip
1792 (re-search-forward comment-start-skip eol t)
1793 (setq eol (match-beginning 0)))
1794 (goto-char eol)
1795 (skip-chars-backward " \t")
1796 (max comment-column (1+ (current-column))))))
1797 "Function to compute desired indentation for a comment.
1798 This function is called with no args with point at the beginning of
1799 the comment's starting delimiter.")
1800
1801 (defconst block-comment-start nil
1802 "*String to insert to start a new comment on a line by itself.
1803 If nil, use `comment-start' instead.
1804 Note that the regular expression `comment-start-skip' should skip this string
1805 as well as the `comment-start' string.")
1806
1807 (defconst block-comment-end nil
1808 "*String to insert to end a new comment on a line by itself.
1809 Should be an empty string if comments are terminated by end-of-line.
1810 If nil, use `comment-end' instead.")
1811
1812 (defun indent-for-comment ()
1813 "Indent this line's comment to comment column, or insert an empty comment."
1814 (interactive "*")
1815 (let* ((empty (save-excursion (beginning-of-line)
1816 (looking-at "[ \t]*$")))
1817 (starter (or (and empty block-comment-start) comment-start))
1818 (ender (or (and empty block-comment-end) comment-end)))
1819 (if (null starter)
1820 (error "No comment syntax defined")
1821 (let* ((eolpos (save-excursion (end-of-line) (point)))
1822 cpos indent begpos)
1823 (beginning-of-line)
1824 (if (re-search-forward comment-start-skip eolpos 'move)
1825 (progn (setq cpos (point-marker))
1826 ;; Find the start of the comment delimiter.
1827 ;; If there were paren-pairs in comment-start-skip,
1828 ;; position at the end of the first pair.
1829 (if (match-end 1)
1830 (goto-char (match-end 1))
1831 ;; If comment-start-skip matched a string with
1832 ;; internal whitespace (not final whitespace) then
1833 ;; the delimiter start at the end of that
1834 ;; whitespace. Otherwise, it starts at the
1835 ;; beginning of what was matched.
1836 (skip-syntax-backward " " (match-beginning 0))
1837 (skip-syntax-backward "^ " (match-beginning 0)))))
1838 (setq begpos (point))
1839 ;; Compute desired indent.
1840 (if (= (current-column)
1841 (setq indent (if comment-indent-hook
1842 (funcall comment-indent-hook)
1843 (funcall comment-indent-function))))
1844 (goto-char begpos)
1845 ;; If that's different from current, change it.
1846 (skip-chars-backward " \t")
1847 (delete-region (point) begpos)
1848 (indent-to indent))
1849 ;; An existing comment?
1850 (if cpos
1851 (progn (goto-char cpos)
1852 (set-marker cpos nil))
1853 ;; No, insert one.
1854 (insert starter)
1855 (save-excursion
1856 (insert ender)))))))
1857
1858 (defun set-comment-column (arg)
1859 "Set the comment column based on point.
1860 With no arg, set the comment column to the current column.
1861 With just minus as arg, kill any comment on this line.
1862 With any other arg, set comment column to indentation of the previous comment
1863 and then align or create a comment on this line at that column."
1864 (interactive "P")
1865 (if (eq arg '-)
1866 (kill-comment nil)
1867 (if arg
1868 (progn
1869 (save-excursion
1870 (beginning-of-line)
1871 (re-search-backward comment-start-skip)
1872 (beginning-of-line)
1873 (re-search-forward comment-start-skip)
1874 (goto-char (match-beginning 0))
1875 (setq comment-column (current-column))
1876 (message "Comment column set to %d" comment-column))
1877 (indent-for-comment))
1878 (setq comment-column (current-column))
1879 (message "Comment column set to %d" comment-column))))
1880
1881 (defun kill-comment (arg)
1882 "Kill the comment on this line, if any.
1883 With argument, kill comments on that many lines starting with this one."
1884 ;; this function loses in a lot of situations. it incorrectly recognises
1885 ;; comment delimiters sometimes (ergo, inside a string), doesn't work
1886 ;; with multi-line comments, can kill extra whitespace if comment wasn't
1887 ;; through end-of-line, et cetera.
1888 (interactive "*P")
1889 (or comment-start-skip (error "No comment syntax defined"))
1890 (let ((count (prefix-numeric-value arg)) endc)
1891 (while (> count 0)
1892 (save-excursion
1893 (end-of-line)
1894 (setq endc (point))
1895 (beginning-of-line)
1896 (and (string< "" comment-end)
1897 (setq endc
1898 (progn
1899 (re-search-forward (regexp-quote comment-end) endc 'move)
1900 (skip-chars-forward " \t")
1901 (point))))
1902 (beginning-of-line)
1903 (if (re-search-forward comment-start-skip endc t)
1904 (progn
1905 (goto-char (match-beginning 0))
1906 (skip-chars-backward " \t")
1907 (kill-region (point) endc)
1908 ;; to catch comments a line beginnings
1909 (indent-according-to-mode))))
1910 (if arg (forward-line 1))
1911 (setq count (1- count)))))
1912
1913 (defun comment-region (beg end &optional arg)
1914 "Comment or uncomment each line in the region.
1915 With just C-u prefix arg, uncomment each line in region.
1916 Numeric prefix arg ARG means use ARG comment characters.
1917 If ARG is negative, delete that many comment characters instead.
1918 Comments are terminated on each line, even for syntax in which newline does
1919 not end the comment. Blank lines do not get comments."
1920 ;; if someone wants it to only put a comment-start at the beginning and
1921 ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
1922 ;; is easy enough. No option is made here for other than commenting
1923 ;; every line.
1924 (interactive "r\nP")
1925 (or comment-start (error "No comment syntax is defined"))
1926 (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
1927 (save-excursion
1928 (save-restriction
1929 (let ((cs comment-start) (ce comment-end)
1930 numarg)
1931 (if (consp arg) (setq numarg t)
1932 (setq numarg (prefix-numeric-value arg))
1933 ;; For positive arg > 1, replicate the comment delims now,
1934 ;; then insert the replicated strings just once.
1935 (while (> numarg 1)
1936 (setq cs (concat cs comment-start)
1937 ce (concat ce comment-end))
1938 (setq numarg (1- numarg))))
1939 ;; Loop over all lines from BEG to END.
1940 (narrow-to-region beg end)
1941 (goto-char beg)
1942 (while (not (eobp))
1943 (if (or (eq numarg t) (< numarg 0))
1944 (progn
1945 ;; Delete comment start from beginning of line.
1946 (if (eq numarg t)
1947 (while (looking-at (regexp-quote cs))
1948 (delete-char (length cs)))
1949 (let ((count numarg))
1950 (while (and (> 1 (setq count (1+ count)))
1951 (looking-at (regexp-quote cs)))
1952 (delete-char (length cs)))))
1953 ;; Delete comment end from end of line.
1954 (if (string= "" ce)
1955 nil
1956 (if (eq numarg t)
1957 (progn
1958 (end-of-line)
1959 ;; This is questionable if comment-end ends in
1960 ;; whitespace. That is pretty brain-damaged,
1961 ;; though.
1962 (skip-chars-backward " \t")
1963 (if (and (>= (- (point) (point-min)) (length ce))
1964 (save-excursion
1965 (backward-char (length ce))
1966 (looking-at (regexp-quote ce))))
1967 (delete-char (- (length ce)))))
1968 (let ((count numarg))
1969 (while (> 1 (setq count (1+ count)))
1970 (end-of-line)
1971 ;; This is questionable if comment-end ends in
1972 ;; whitespace. That is pretty brain-damaged though
1973 (skip-chars-backward " \t")
1974 (save-excursion
1975 (backward-char (length ce))
1976 (if (looking-at (regexp-quote ce))
1977 (delete-char (length ce))))))))
1978 (forward-line 1))
1979 ;; Insert at beginning and at end.
1980 (if (looking-at "[ \t]*$") ()
1981 (insert cs)
1982 (if (string= "" ce) ()
1983 (end-of-line)
1984 (insert ce)))
1985 (search-forward "\n" nil 'move)))))))
1986
1987 (defun prefix-region (prefix)
1988 "Add a prefix string to each line between mark and point."
1989 (interactive "sPrefix string: ")
1990 (if prefix
1991 (let ((count (count-lines (mark) (point))))
1992 (goto-char (min (mark) (point)))
1993 (while (> count 0)
1994 (setq count (1- count))
1995 (beginning-of-line 1)
1996 (insert prefix)
1997 (end-of-line 1)
1998 (forward-char 1)))))
1999
2000
2001 (defun backward-word (arg &optional buffer)
2002 "Move backward until encountering the end of a word.
2003 With argument, do this that many times.
2004 In programs, it is faster to call `forward-word' with negative arg."
2005 (interactive "_p")
2006 (forward-word (- arg) buffer))
2007
2008 (defun mark-word (arg)
2009 "Set mark arg words away from point."
2010 (interactive "p")
2011 (mark-something 'mark-word 'forward-word arg))
2012
2013 (defun kill-word (arg)
2014 "Kill characters forward until encountering the end of a word.
2015 With argument, do this that many times."
2016 (interactive "*p")
2017 (kill-region (point) (save-excursion (forward-word arg) (point))))
2018
2019 (defun backward-kill-word (arg)
2020 "Kill characters backward until encountering the end of a word.
2021 With argument, do this that many times."
2022 (interactive "*p")
2023 (kill-word (- arg)))
2024
2025 (defun current-word (&optional strict)
2026 "Return the word point is on (or a nearby word) as a string.
2027 If optional arg STRICT is non-nil, return nil unless point is within
2028 or adjacent to a word.
2029 If point is not between two word-constituent characters, but immediately
2030 follows one, move back first.
2031 Otherwise, if point precedes a word constituent, move forward first.
2032 Otherwise, move backwards until a word constituent is found and get that word;
2033 if you a newlines is reached first, move forward instead."
2034 (save-excursion
2035 (let ((oldpoint (point)) (start (point)) (end (point)))
2036 (skip-syntax-backward "w_") (setq start (point))
2037 (goto-char oldpoint)
2038 (skip-syntax-forward "w_") (setq end (point))
2039 (if (and (eq start oldpoint) (eq end oldpoint))
2040 ;; Point is neither within nor adjacent to a word.
2041 (and (not strict)
2042 (progn
2043 ;; Look for preceding word in same line.
2044 (skip-syntax-backward "^w_"
2045 (save-excursion
2046 (beginning-of-line) (point)))
2047 (if (bolp)
2048 ;; No preceding word in same line.
2049 ;; Look for following word in same line.
2050 (progn
2051 (skip-syntax-forward "^w_"
2052 (save-excursion
2053 (end-of-line) (point)))
2054 (setq start (point))
2055 (skip-syntax-forward "w_")
2056 (setq end (point)))
2057 (setq end (point))
2058 (skip-syntax-backward "w_")
2059 (setq start (point)))
2060 (buffer-substring start end)))
2061 (buffer-substring start end)))))
2062
2063 (defvar fill-prefix nil
2064 "*String for filling to insert at front of new line, or nil for none.
2065 Setting this variable automatically makes it local to the current buffer.")
2066 (make-variable-buffer-local 'fill-prefix)
2067
2068 (defvar auto-fill-inhibit-regexp nil
2069 "*Regexp to match lines which should not be auto-filled.")
2070
2071 (defun do-auto-fill ()
2072 (let (give-up)
2073 (or (and auto-fill-inhibit-regexp
2074 (save-excursion (beginning-of-line)
2075 (looking-at auto-fill-inhibit-regexp)))
2076 (while (and (not give-up) (> (current-column) fill-column))
2077 ;; Determine where to split the line.
2078 (let ((fill-prefix fill-prefix)
2079 (fill-point
2080 (let ((opoint (point))
2081 bounce
2082 (first t))
2083 (save-excursion
2084 (move-to-column (1+ fill-column))
2085 ;; Move back to a word boundary.
2086 (while (or first
2087 ;; If this is after period and a single space,
2088 ;; move back once more--we don't want to break
2089 ;; the line there and make it look like a
2090 ;; sentence end.
2091 (and (not (bobp))
2092 (not bounce)
2093 sentence-end-double-space
2094 (save-excursion (forward-char -1)
2095 (and (looking-at "\\. ")
2096 (not (looking-at "\\. "))))))
2097 (setq first nil)
2098 (skip-chars-backward "^ \t\n")
2099 ;; If we find nowhere on the line to break it,
2100 ;; break after one word. Set bounce to t
2101 ;; so we will not keep going in this while loop.
2102 (if (bolp)
2103 (progn
2104 (re-search-forward "[ \t]" opoint t)
2105 (setq bounce t)))
2106 (skip-chars-backward " \t"))
2107 ;; Let fill-point be set to the place where we end up.
2108 (point)))))
2109
2110 ;; I'm not sure why Stig made this change but it breaks
2111 ;; auto filling in at least C mode so I'm taking it back
2112 ;; out. --cet
2113 ;; XEmacs - adaptive fill.
2114 ;;(maybe-adapt-fill-prefix
2115 ;; (or from (setq from (save-excursion (beginning-of-line)
2116 ;; (point))))
2117 ;; (or to (setq to (save-excursion (beginning-of-line 2)
2118 ;; (point))))
2119 ;; t)
2120
2121 ;; If that place is not the beginning of the line,
2122 ;; break the line there.
2123 (if (save-excursion
2124 (goto-char fill-point)
2125 (not (bolp)))
2126 (let ((prev-column (current-column)))
2127 ;; If point is at the fill-point, do not `save-excursion'.
2128 ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2129 ;; point will end up before it rather than after it.
2130 (if (save-excursion
2131 (skip-chars-backward " \t")
2132 (= (point) fill-point))
2133 (indent-new-comment-line)
2134 (save-excursion
2135 (goto-char fill-point)
2136 (indent-new-comment-line)))
2137 ;; If making the new line didn't reduce the hpos of
2138 ;; the end of the line, then give up now;
2139 ;; trying again will not help.
2140 (if (>= (current-column) prev-column)
2141 (setq give-up t)))
2142 ;; No place to break => stop trying.
2143 (setq give-up t)))))))
2144
2145 (defvar comment-multi-line t ; XEmacs - this works well with adaptive fill
2146 "*Non-nil means \\[indent-new-comment-line] should continue same comment
2147 on new line, with no new terminator or starter.
2148 This is obsolete because you might as well use \\[newline-and-indent].")
2149
2150 (defun indent-new-comment-line (&optional soft)
2151 "Break line at point and indent, continuing comment if within one.
2152 This indents the body of the continued comment
2153 under the previous comment line.
2154
2155 This command is intended for styles where you write a comment per line,
2156 starting a new comment (and terminating it if necessary) on each line.
2157 If you want to continue one comment across several lines, use \\[newline-and-indent].
2158
2159 If a fill column is specified, it overrides the use of the comment column
2160 or comment indentation.
2161
2162 The inserted newline is marked hard if `use-hard-newlines' is true,
2163 unless optional argument SOFT is non-nil."
2164 (interactive)
2165 (let (comcol comstart)
2166 (skip-chars-backward " \t")
2167 (delete-region (point)
2168 (progn (skip-chars-forward " \t")
2169 (point)))
2170 (if soft (insert ?\n) (newline 1))
2171 (if fill-prefix
2172 (progn
2173 (indent-to-left-margin)
2174 (insert fill-prefix))
2175 ;; #### - Eric Eide reverts to v18 semantics for this function in
2176 ;; fa-extras, which I'm not gonna do. His changes are to (1) execute
2177 ;; the save-excursion below unconditionally, and (2) uncomment the check
2178 ;; for (not comment-multi-line) further below. --Stig
2179 (if (not comment-multi-line)
2180 (save-excursion
2181 (if (and comment-start-skip
2182 (let ((opoint (point)))
2183 (forward-line -1)
2184 (re-search-forward comment-start-skip opoint t)))
2185 ;; The old line is a comment.
2186 ;; Set WIN to the pos of the comment-start.
2187 ;; But if the comment is empty, look at preceding lines
2188 ;; to find one that has a nonempty comment.
2189
2190 ;; If comment-start-skip contains a \(...\) pair,
2191 ;; the real comment delimiter starts at the end of that pair.
2192 (let ((win (or (match-end 1) (match-beginning 0))))
2193 (while (and (eolp) (not (bobp))
2194 (let (opoint)
2195 (beginning-of-line)
2196 (setq opoint (point))
2197 (forward-line -1)
2198 (re-search-forward comment-start-skip opoint t)))
2199 (setq win (or (match-end 1) (match-beginning 0))))
2200 ;; Indent this line like what we found.
2201 (goto-char win)
2202 (setq comcol (current-column))
2203 (setq comstart
2204 (buffer-substring (point) (match-end 0)))))))
2205 (if (and comcol (not fill-prefix)) ; XEmacs - (ENE) from fa-extras.
2206 (let ((comment-column comcol)
2207 (comment-start comstart)
2208 (comment-end comment-end))
2209 (and comment-end (not (equal comment-end ""))
2210 ; (if (not comment-multi-line)
2211 (progn
2212 (forward-char -1)
2213 (insert comment-end)
2214 (forward-char 1))
2215 ; (setq comment-column (+ comment-column (length comment-start))
2216 ; comment-start "")
2217 ; )
2218 )
2219 (if (not (eolp))
2220 (setq comment-end ""))
2221 (insert ?\n)
2222 (forward-char -1)
2223 (indent-for-comment)
2224 (save-excursion
2225 ;; Make sure we delete the newline inserted above.
2226 (end-of-line)
2227 (delete-char 1)))
2228 (indent-according-to-mode)))))
2229
2230 (defun auto-fill-mode (&optional arg)
2231 "Toggle auto-fill mode.
2232 With arg, turn auto-fill mode on if and only if arg is positive.
2233 In Auto-Fill mode, inserting a space at a column beyond `current-fill-column'
2234 automatically breaks the line at a previous space."
2235 (interactive "P")
2236 (prog1 (setq auto-fill-function
2237 (if (if (null arg)
2238 (not auto-fill-function)
2239 (> (prefix-numeric-value arg) 0))
2240 'do-auto-fill
2241 nil))
2242 (redraw-modeline)))
2243
2244 ;; This holds a document string used to document auto-fill-mode.
2245 (defun auto-fill-function ()
2246 "Automatically break line at a previous space, in insertion of text."
2247 nil)
2248
2249 (defun turn-on-auto-fill ()
2250 "Unconditionally turn on Auto Fill mode."
2251 (auto-fill-mode 1))
2252
2253 (defun set-fill-column (arg)
2254 "Set `fill-column' to current column, or to argument if given.
2255 The variable `fill-column' has a separate value for each buffer."
2256 (interactive "_P")
2257 (setq fill-column (if (integerp arg) arg (current-column)))
2258 (message "fill-column set to %d" fill-column))
2259
2260 (defun set-selective-display (arg)
2261 "Set `selective-display' to ARG; clear it if no arg.
2262 When the value of `selective-display' is a number > 0,
2263 lines whose indentation is >= that value are not displayed.
2264 The variable `selective-display' has a separate value for each buffer."
2265 (interactive "P")
2266 (if (eq selective-display t)
2267 (error "selective-display already in use for marked lines"))
2268 (let ((current-vpos
2269 (save-restriction
2270 (narrow-to-region (point-min) (point))
2271 (goto-char (window-start))
2272 (vertical-motion (window-height)))))
2273 (setq selective-display
2274 (and arg (prefix-numeric-value arg)))
2275 (recenter current-vpos))
2276 (set-window-start (selected-window) (window-start (selected-window)))
2277 ;; #### doesn't localize properly:
2278 (princ "selective-display set to " t)
2279 (prin1 selective-display t)
2280 (princ "." t))
2281
2282 (defun nuke-selective-display ()
2283 "Ensure that the buffer is not in selective-display mode.
2284 If `selective-display' is t, then restore the buffer text to it's original
2285 state before disabling selective display."
2286 ;; by Stig@hackvan.com
2287 (interactive)
2288 (and (eq t selective-display)
2289 (save-excursion
2290 (save-restriction
2291 (widen)
2292 (goto-char (point-min))
2293 (let ((mod-p (buffer-modified-p))
2294 (buffer-read-only nil))
2295 (while (search-forward "\r" nil t)
2296 (delete-char -1)
2297 (insert "\n"))
2298 (set-buffer-modified-p mod-p)
2299 ))))
2300 (setq selective-display nil))
2301
2302 (add-hook 'change-major-mode-hook 'nuke-selective-display)
2303
2304 (defvar overwrite-mode-textual (purecopy " Ovwrt")
2305 "The string displayed in the modeline when in overwrite mode.")
2306 (defvar overwrite-mode-binary (purecopy " Bin Ovwrt")
2307 "The string displayed in the modeline when in binary overwrite mode.")
2308
2309 (defun overwrite-mode (arg)
2310 "Toggle overwrite mode.
2311 With arg, turn overwrite mode on iff arg is positive.
2312 In overwrite mode, printing characters typed in replace existing text
2313 on a one-for-one basis, rather than pushing it to the right. At the
2314 end of a line, such characters extend the line. Before a tab,
2315 such characters insert until the tab is filled in.
2316 \\[quoted-insert] still inserts characters in overwrite mode; this
2317 is supposed to make it easier to insert characters when necessary."
2318 (interactive "P")
2319 (setq overwrite-mode
2320 (if (if (null arg) (not overwrite-mode)
2321 (> (prefix-numeric-value arg) 0))
2322 'overwrite-mode-textual))
2323 (redraw-modeline))
2324
2325 (defun binary-overwrite-mode (arg)
2326 "Toggle binary overwrite mode.
2327 With arg, turn binary overwrite mode on iff arg is positive.
2328 In binary overwrite mode, printing characters typed in replace
2329 existing text. Newlines are not treated specially, so typing at the
2330 end of a line joins the line to the next, with the typed character
2331 between them. Typing before a tab character simply replaces the tab
2332 with the character typed.
2333 \\[quoted-insert] replaces the text at the cursor, just as ordinary
2334 typing characters do.
2335
2336 Note that binary overwrite mode is not its own minor mode; it is a
2337 specialization of overwrite-mode, entered by setting the
2338 `overwrite-mode' variable to `overwrite-mode-binary'."
2339 (interactive "P")
2340 (setq overwrite-mode
2341 (if (if (null arg)
2342 (not (eq overwrite-mode 'overwrite-mode-binary))
2343 (> (prefix-numeric-value arg) 0))
2344 'overwrite-mode-binary))
2345 (redraw-modeline))
2346
2347 (defvar line-number-mode nil
2348 "*Non-nil means display line number in modeline.")
2349
2350 (defun line-number-mode (arg)
2351 "Toggle Line Number mode.
2352 With arg, turn Line Number mode on iff arg is positive.
2353 When Line Number mode is enabled, the line number appears
2354 in the modeline."
2355 (interactive "P")
2356 (setq line-number-mode
2357 (if (null arg) (not line-number-mode)
2358 (> (prefix-numeric-value arg) 0)))
2359 (redraw-modeline))
2360
2361 (defvar column-number-mode nil
2362 "*Non-nil means display column number in modeline.")
2363
2364 (defun column-number-mode (arg)
2365 "Toggle Column Number mode.
2366 With arg, turn Column Number mode on iff arg is positive.
2367 When Column Number mode is enabled, the column number appears
2368 in the modeline."
2369 (interactive "P")
2370 (setq column-number-mode
2371 (if (null arg) (not column-number-mode)
2372 (> (prefix-numeric-value arg) 0)))
2373 (redraw-modeline))
2374
2375
2376 (defvar blink-matching-paren t
2377 "*Non-nil means show matching open-paren when close-paren is inserted.")
2378
2379 (defvar blink-matching-paren-distance 12000
2380 "*If non-nil, is maximum distance to search for matching open-paren.")
2381
2382 (defconst blink-matching-delay 1
2383 "*The number of seconds that `blink-matching-open' will delay at a match.")
2384
2385 (defconst blink-matching-paren-dont-ignore-comments nil
2386 "*Non-nil means `blink-matching-paren' should not ignore comments.")
2387
2388 (defun blink-matching-open ()
2389 "Move cursor momentarily to the beginning of the sexp before point."
2390 (interactive "_")
2391 (and (> (point) (1+ (point-min)))
2392 blink-matching-paren
2393 ;; Verify an even number of quoting characters precede the close.
2394 (= 1 (logand 1 (- (point)
2395 (save-excursion
2396 (forward-char -1)
2397 (skip-syntax-backward "/\\")
2398 (point)))))
2399 (let* ((oldpos (point))
2400 (parse-sexp-ignore-comments t) ; to avoid C++ lossage
2401 (blinkpos)
2402 (mismatch))
2403 (save-excursion
2404 (save-restriction
2405 (if blink-matching-paren-distance
2406 (narrow-to-region (max (point-min)
2407 (- (point) blink-matching-paren-distance))
2408 oldpos))
2409 (condition-case ()
2410 (let ((parse-sexp-ignore-comments
2411 (and parse-sexp-ignore-comments
2412 (not blink-matching-paren-dont-ignore-comments))))
2413 (setq blinkpos (scan-sexps oldpos -1)))
2414 (error nil)))
2415 (and blinkpos
2416 (/= (char-syntax (char-after blinkpos))
2417 ?\$)
2418 (setq mismatch
2419 (or (null (matching-paren (char-after blinkpos)))
2420 (/= (char-after (1- oldpos))
2421 (matching-paren (char-after blinkpos))))))
2422 (if mismatch (setq blinkpos nil))
2423 (if blinkpos
2424 (progn
2425 (goto-char blinkpos)
2426 (if (pos-visible-in-window-p)
2427 (sit-for blink-matching-delay)
2428 (goto-char blinkpos)
2429 (message
2430 "Matches %s"
2431 ;; Show what precedes the open in its line, if anything.
2432 (if (save-excursion
2433 (skip-chars-backward " \t")
2434 (not (bolp)))
2435 (buffer-substring (progn (beginning-of-line) (point))
2436 (1+ blinkpos))
2437 ;; Show what follows the open in its line, if anything.
2438 (if (save-excursion
2439 (forward-char 1)
2440 (skip-chars-forward " \t")
2441 (not (eolp)))
2442 (buffer-substring blinkpos
2443 (progn (end-of-line) (point)))
2444 ;; Otherwise show the previous nonblank line,
2445 ;; if there is one.
2446 (if (save-excursion
2447 (skip-chars-backward "\n \t")
2448 (not (bobp)))
2449 (concat
2450 (buffer-substring (progn
2451 (skip-chars-backward "\n \t")
2452 (beginning-of-line)
2453 (point))
2454 (progn (end-of-line)
2455 (skip-chars-backward " \t")
2456 (point)))
2457 ;; Replace the newline and other whitespace with `...'.
2458 "..."
2459 (buffer-substring blinkpos (1+ blinkpos)))
2460 ;; There is nothing to show except the char itself.
2461 (buffer-substring blinkpos (1+ blinkpos))))))))
2462 (cond (mismatch
2463 (message "Mismatched parentheses"))
2464 ((not blink-matching-paren-distance)
2465 (message "Unmatched parenthesis"))))))))
2466
2467 ;Turned off because it makes dbx bomb out.
2468 (setq blink-paren-function 'blink-matching-open)
2469
2470 (eval-when-compile (defvar myhelp)) ; suppress compiler warning
2471
2472 (defun set-variable (var val)
2473 "Set VARIABLE to VALUE. VALUE is a Lisp object.
2474 When using this interactively, supply a Lisp expression for VALUE.
2475 If you want VALUE to be a string, you must surround it with doublequotes.
2476
2477 If VARIABLE has a `variable-interactive' property, that is used as if
2478 it were the arg to `interactive' (which see) to interactively read the value."
2479 (interactive
2480 (let* ((var (read-variable "Set variable: "))
2481 ;; #### - yucky code replication here. This should use something
2482 ;; from help.el or hyper-apropos.el
2483 (minibuffer-help-form
2484 '(funcall myhelp))
2485 (myhelp
2486 #'(lambda ()
2487 (with-output-to-temp-buffer "*Help*"
2488 (prin1 var)
2489 (princ "\nDocumentation:\n")
2490 (princ (substring (documentation-property var 'variable-documentation)
2491 1))
2492 (if (boundp var)
2493 (let ((print-length 20))
2494 (princ "\n\nCurrent value: ")
2495 (prin1 (symbol-value var))))
2496 (save-excursion
2497 (set-buffer standard-output)
2498 (help-mode))
2499 nil))))
2500 (list var
2501 (let ((prop (get var 'variable-interactive)))
2502 (if prop
2503 ;; Use VAR's `variable-interactive' property
2504 ;; as an interactive spec for prompting.
2505 (call-interactively (list 'lambda '(arg)
2506 (list 'interactive prop)
2507 'arg))
2508 (eval-minibuffer (format "Set %s to value: " var)))))))
2509 (set var val))
2510
2511 (defun activate-region ()
2512 "Activate the region, if `zmacs-regions' is true.
2513 Setting `zmacs-regions' to true causes LISPM-style active regions to be used.
2514 This function has no effect if `zmacs-regions' is false."
2515 (interactive)
2516 (and zmacs-regions (zmacs-activate-region)))
2517
2518 (defsubst region-exists-p ()
2519 "Non-nil iff the region exists.
2520 If active regions are in use (i.e. `zmacs-regions' is true), this means that
2521 the region is active. Otherwise, this means that the user has pushed
2522 a mark in this buffer at some point in the past.
2523 The functions `region-beginning' and `region-end' can be used to find the
2524 limits of the region."
2525 (not (null (mark))))
2526
2527 (defun region-active-p ()
2528 "Non-nil iff the region is active.
2529 If `zmacs-regions' is true, this is equivalent to `region-exists-p'.
2530 Otherwise, this function always returns false."
2531 (and zmacs-regions zmacs-region-extent))
2532
2533 (defun capitalize-region-or-word (arg)
2534 "Capitalize the selected region or the following word (or ARG words)."
2535 (interactive "p")
2536 (if (region-active-p) (capitalize-region (region-beginning) (region-end))
2537 (capitalize-word arg)))
2538
2539 (defun upcase-region-or-word (arg)
2540 "Upcase the selected region or the following word (or ARG words)."
2541 (interactive "p")
2542 (if (region-active-p) (upcase-region (region-beginning) (region-end))
2543 (upcase-word arg)))
2544
2545 (defun downcase-region-or-word (arg)
2546 "Downcase the selected region or the following word (or ARG words)."
2547 (interactive "p")
2548 (if (region-active-p) (downcase-region (region-beginning) (region-end))
2549 (downcase-word arg)))
2550
2551 ;;;
2552 ;;; Most of the zmacs code is now in elisp. The only thing left in C
2553 ;;; are the variables zmacs-regions, zmacs-region-active-p and
2554 ;;; zmacs-region-stays plus the function zmacs_update_region which
2555 ;;; calls the lisp level zmacs-update-region. It must remain since it
2556 ;;; must be called by core C code.
2557 ;;;
2558
2559 (defvar zmacs-activate-region-hook nil
2560 "Function or functions called when the region becomes active;
2561 see the variable `zmacs-regions'.")
2562
2563 (defvar zmacs-deactivate-region-hook nil
2564 "Function or functions called when the region becomes inactive;
2565 see the variable `zmacs-regions'.")
2566
2567 (defvar zmacs-update-region-hook nil
2568 "Function or functions called when the active region changes.
2569 This is called after each command that sets `zmacs-region-stays' to t.
2570 See the variable `zmacs-regions'.")
2571
2572 (defvar zmacs-region-extent nil
2573 "The extent of the zmacs region; don't use this.")
2574
2575 (defvar zmacs-region-rectangular-p nil
2576 "Whether the zmacs region is a rectangle; don't use this.")
2577
2578 (defun zmacs-make-extent-for-region (region)
2579 ;; Given a region, this makes an extent in the buffer which holds that
2580 ;; region, for highlighting purposes. If the region isn't associated
2581 ;; with a buffer, this does nothing.
2582 (let ((buffer nil)
2583 (valid (and (extentp zmacs-region-extent)
2584 (extent-object zmacs-region-extent)
2585 (buffer-live-p (extent-object zmacs-region-extent))))
2586 start end)
2587 (cond ((consp region)
2588 (setq start (min (car region) (cdr region))
2589 end (max (car region) (cdr region))
2590 valid (and valid
2591 (eq (marker-buffer (car region))
2592 (extent-object zmacs-region-extent)))
2593 buffer (marker-buffer (car region))))
2594 (t
2595 (signal 'error (list "invalid region" region))))
2596
2597 (if valid
2598 nil
2599 ;; The condition case is in case any of the extents are dead or
2600 ;; otherwise incapacitated.
2601 (condition-case ()
2602 (if (listp zmacs-region-extent)
2603 (mapcar 'delete-extent zmacs-region-extent)
2604 (delete-extent zmacs-region-extent))
2605 (error nil)))
2606
2607 (if valid
2608 (set-extent-endpoints zmacs-region-extent start end)
2609 (setq zmacs-region-extent (make-extent start end buffer))
2610
2611 ;; Make the extent be closed on the right, which means that if
2612 ;; characters are inserted exactly at the end of the extent, the
2613 ;; extent will grow to cover them. This is important for shell
2614 ;; buffers - suppose one makes a region, and one end is at point-max.
2615 ;; If the shell produces output, that marker will remain at point-max
2616 ;; (its position will increase). So it's important that the extent
2617 ;; exhibit the same behavior, lest the region covered by the extent
2618 ;; (the visual indication), and the region between point and mark
2619 ;; (the actual region value) become different!
2620 (set-extent-property zmacs-region-extent 'end-open nil)
2621
2622 ;; use same priority as mouse-highlighting so that conflicts between
2623 ;; the region extent and a mouse-highlighted extent are resolved by
2624 ;; the usual size-and-endpoint-comparison method.
2625 (set-extent-priority zmacs-region-extent mouse-highlight-priority)
2626 (set-extent-face zmacs-region-extent 'zmacs-region)
2627
2628 ;; #### It might be better to actually break
2629 ;; default-mouse-track-next-move-rect out of mouse.el so that we
2630 ;; can use its logic here.
2631 (cond
2632 (zmacs-region-rectangular-p
2633 (setq zmacs-region-extent (list zmacs-region-extent))
2634 (default-mouse-track-next-move-rect start end zmacs-region-extent)
2635 ))
2636
2637 zmacs-region-extent)))
2638
2639 (defun zmacs-region-buffer ()
2640 "Return the buffer containing the zmacs region, or nil."
2641 ;; #### this is horrible and kludgy! This stuff needs to be rethought.
2642 (and zmacs-regions zmacs-region-active-p
2643 (or (marker-buffer (mark-marker t))
2644 (and (extent-live-p zmacs-region-extent)
2645 (buffer-live-p (extent-object zmacs-region-extent))
2646 (extent-object zmacs-region-extent)))))
2647
2648 (defun zmacs-activate-region ()
2649 "Make the region between `point' and `mark' be active (highlighted),
2650 if `zmacs-regions' is true. Only a very small number of commands
2651 should ever do this. Calling this function will call the hook
2652 `zmacs-activate-region-hook', if the region was previously inactive.
2653 Calling this function ensures that the region stays active after the
2654 current command terminates, even if `zmacs-region-stays' is not set.
2655 Returns t if the region was activated (i.e. if `zmacs-regions' if t)."
2656 (if (not zmacs-regions)
2657 nil
2658 (setq zmacs-region-active-p t
2659 zmacs-region-stays t
2660 zmacs-region-rectangular-p (and (boundp 'mouse-track-rectangle-p)
2661 mouse-track-rectangle-p))
2662 (if (marker-buffer (mark-marker t))
2663 (zmacs-make-extent-for-region (cons (point-marker t) (mark-marker t))))
2664 (run-hooks 'zmacs-activate-region-hook)
2665 t))
2666
2667 (defun zmacs-deactivate-region ()
2668 "Make the region between `point' and `mark' no longer be active,
2669 if `zmacs-regions' is true. You shouldn't need to call this; the
2670 command loop calls it when appropriate. Calling this function will
2671 call the hook `zmacs-deactivate-region-hook', if the region was
2672 previously active. Returns t if the region had been active, nil
2673 otherwise."
2674 (if (not zmacs-region-active-p)
2675 nil
2676 (setq zmacs-region-active-p nil
2677 zmacs-region-stays nil
2678 zmacs-region-rectangular-p nil)
2679 (if zmacs-region-extent
2680 (let ((inhibit-quit t))
2681 (if (listp zmacs-region-extent)
2682 (mapcar 'delete-extent zmacs-region-extent)
2683 (delete-extent zmacs-region-extent))
2684 (setq zmacs-region-extent nil)))
2685 (run-hooks 'zmacs-deactivate-region-hook)
2686 t))
2687
2688 (defun zmacs-update-region ()
2689 "Update the highlighted region between `point' and `mark'.
2690 You shouldn't need to call this; the command loop calls it
2691 when appropriate. Calling this function will call the hook
2692 `zmacs-update-region-hook', if the region is active."
2693 (if zmacs-region-active-p
2694 (progn
2695 (if (marker-buffer (mark-marker t))
2696 (zmacs-make-extent-for-region (cons (point-marker t)
2697 (mark-marker t))))
2698 (run-hooks 'zmacs-update-region-hook))))
2699
2700 ;;;;;;
2701 ;;;;;; echo area stuff
2702 ;;;;;;
2703
2704 ;;; The `message-stack' is an alist of labels with messages; the first
2705 ;;; message in this list is always in the echo area. A call to
2706 ;;; `display-message' inserts a label/message pair at the head of the
2707 ;;; list, and removes any other pairs with that label. Calling
2708 ;;; `clear-message' causes any pair with matching label to be removed,
2709 ;;; and this may cause the displayed message to change or vanish. If
2710 ;;; the label arg is nil, the entire message stack is cleared.
2711 ;;;
2712 ;;; Message/error filtering will be a little tricker to implement than
2713 ;;; logging, since messages can be built up incrementally
2714 ;;; using clear-message followed by repeated calls to append-message
2715 ;;; (this happens with error messages). For messages which aren't
2716 ;;; created this way, filtering could be implemented at display-message
2717 ;;; very easily.
2718 ;;;
2719 ;;; Bits of the logging code are borrowed from log-messages.el by
2720 ;;; Robert Potter (rpotter@grip.cis.upenn.edu).
2721
2722 ;; need this to terminate the currently-displayed message
2723 ;; ("Loading simple ...")
2724 (or (fboundp 'display-message) (send-string-to-terminal "\n"))
2725
2726 (defvar message-stack nil
2727 "An alist of label/string pairs representing active echo-area messages.
2728 The first element in the list is currently displayed in the echo area.
2729 Do not modify this directly--use the `message' or
2730 `display-message'/`clear-message' functions.")
2731
2732 (defvar remove-message-hook 'log-message
2733 "A function or list of functions to be called when a message is removed
2734 from the echo area at the bottom of the frame. The label of the removed
2735 message is passed as the first argument, and the text of the message
2736 as the second argument.")
2737
2738 (defvar log-message-max-size 50000
2739 "Maximum size of the \" *Message-Log*\" buffer. See `log-message'.")
2740
2741 (defvar log-message-ignore-regexps
2742 '("^Mark set$"
2743 "^Undo!$"
2744 "^Quit$"
2745 "^\\(Beginning\\|End\\) of buffer$"
2746 "^Fontifying"
2747 "^\\(Failing \\)?\\([Ww]rapped \\)?\\([Rr]egexp \\)?I-search\\( backward\\)?:"
2748 "^Mark saved where search started$"
2749 "^Making completion list"
2750 "^Matches " ; paren-matching message
2751 "^Type .* to \\(remove help\\|restore the other\\) window."
2752 "^M-x .* (bound to key" ; teach-extended-commands
2753 "^(No changes need to be saved)$"
2754 "^(No files need saving)$"
2755 "^\\(Parsing messages\\|Reading attributes\\|Generating summary\\|Building threads\\|Converting\\)\\.\\.\\. [0-9]+$" ; vm
2756 "^End of message \d+" ; vm
2757 "^Parsing error messages\\.\\.\\.[0-9]+" ; compile
2758 "^Parsed [0-9]+ of [0-9]+ ([0-9]+%)$" ; w3
2759 "^\\(Formatting Summary\\|Reading active file\\|Checking new news\\|Looking for crossposts\\|Marking crossposts\\|MHSPOOL:\\|NNSPOOL:\\|NNTP:\\|\\(Uns\\|S\\)ubscribing new newsgroups\\)\\.\\.\\. *[0-9]+%$" ; gnus
2760 "^Adding glyphs\\.\\.\\. ([0-9]+%)\\( done\\)?$" ; outl-mouse
2761 "^->" ; bbdb prompt
2762 )
2763 "List of regular expressions matching messages which shouldn't be logged.
2764 See `log-message'.
2765
2766 Ideally, packages which generate messages which might need to be ignored
2767 should label them with 'progress, 'prompt, or 'no-log, so they can be
2768 filtered by the log-message-ignore-labels.")
2769
2770 (defvar log-message-ignore-labels
2771 '(help-echo command progress prompt no-log garbage-collecting auto-saving)
2772 "List of symbols indicating labels of messages which shouldn't be logged.
2773 See `display-message' for some common labels. See also `log-message'.")
2774
2775 ;Subsumed by view-lossage
2776 ;(defun show-message-log ()
2777 ; "Show the \" *Message-Log*\" buffer, which contains old messages and errors."
2778 ; (interactive)
2779 ; (pop-to-buffer " *Message-Log*"))
2780
2781 (defvar log-message-filter-function 'log-message-filter
2782 "Value must be a function of two arguments: a symbol (label) and
2783 a string (messsage). It should return non-nil to indicate a message
2784 should be logged. Possible values include 'log-message-filter and
2785 'log-message-filter-errors-only.")
2786
2787 (defun log-message-filter (label message)
2788 "Default value of log-message-filter-function.
2789 Mesages whose text matches one of the log-message-ignore-regexps
2790 or whose label appears in log-message-ignore-labels are not saved."
2791 (let ((r log-message-ignore-regexps)
2792 (ok (not (memq label log-message-ignore-labels))))
2793 (while (and r ok)
2794 (if (save-match-data (string-match (car r) message))
2795 (setq ok nil))
2796 (setq r (cdr r)))
2797 ok))
2798
2799 (defun log-message-filter-errors-only (label message)
2800 "For use as the log-message-filter-function. Only logs error messages."
2801 (eq label 'error))
2802
2803 (defun log-message (label message)
2804 "Stuff a copy of the message into the \" *Message-Log*\" buffer,
2805 if it satisfies the log-message-filter-function.
2806
2807 For use on remove-message-hook."
2808 (if (and (not noninteractive)
2809 (funcall log-message-filter-function label message))
2810 (save-excursion
2811 (set-buffer (get-buffer-create " *Message-Log*"))
2812 (goto-char (point-max))
2813 ;; (insert (concat (upcase (symbol-name label)) ": " message "\n"))
2814 (insert message "\n")
2815 (if (> (point-max) (max log-message-max-size (point-min)))
2816 (progn
2817 ;; trim log to ~90% of max size
2818 (goto-char (max (- (point-max)
2819 (truncate (* 0.9 log-message-max-size)))
2820 (point-min)))
2821 (forward-line 1)
2822 (delete-region (point-min) (point)))))))
2823
2824 (defun message-displayed-p (&optional return-string frame)
2825 "Return a non-nil value if a message is presently displayed in the\n\
2826 minibuffer's echo area. If optional argument RETURN-STRING is non-nil,\n\
2827 return a string containing the message, otherwise just return t."
2828 ;; by definition, a message is displayed if the echo area buffer is
2829 ;; non-empty (see also echo_area_active()). It had better also
2830 ;; be the case that message-stack is nil exactly when the echo area
2831 ;; is non-empty.
2832 (let ((buffer (get-buffer " *Echo Area*")))
2833 (and (< (point-min buffer) (point-max buffer))
2834 (if return-string
2835 (buffer-substring nil nil buffer)
2836 t))))
2837
2838 ;;; Returns the string which remains in the echo area, or nil if none.
2839 ;;; If label is nil, the whole message stack is cleared.
2840 (defun clear-message (&optional label frame stdout-p no-restore)
2841 "Remove any message with the given LABEL from the message-stack,
2842 erasing it from the echo area if it's currently displayed there.
2843 If a message remains at the head of the message-stack and NO-RESTORE
2844 is nil, it will be displayed. The string which remains in the echo
2845 area will be returned, or nil if the message-stack is now empty.
2846 If LABEL is nil, the entire message-stack is cleared.
2847
2848 Unless you need the return value or you need to specify a lable,
2849 you should just use (message nil)."
2850 (or frame (setq frame (selected-frame)))
2851 (let ((clear-stream (and message-stack (eq 'stream (frame-type frame)))))
2852 (remove-message label frame)
2853 (let ((buffer (get-buffer " *Echo Area*"))
2854 (zmacs-region-stays zmacs-region-stays)) ; preserve from change
2855 (erase-buffer buffer))
2856 (if clear-stream
2857 (send-string-to-terminal ?\n stdout-p))
2858 (if no-restore
2859 nil ; just preparing to put another msg up
2860 (if message-stack
2861 (let ((oldmsg (cdr (car message-stack))))
2862 (raw-append-message oldmsg frame stdout-p)
2863 oldmsg)
2864 ;; ### should we (redisplay-echo-area) here? messes some things up.
2865 nil))))
2866
2867 (defun remove-message (&optional label frame)
2868 ;; If label is nil, we want to remove all matching messages.
2869 ;; Must reverse the stack first to log them in the right order.
2870 (let ((log nil))
2871 (while (and message-stack
2872 (or (null label) ; null label means clear whole stack
2873 (eq label (car (car message-stack)))))
2874 (setq log (cons (car message-stack) log))
2875 (setq message-stack (cdr message-stack)))
2876 (let ((s message-stack))
2877 (while (cdr s)
2878 (let ((msg (car (cdr s))))
2879 (if (eq label (car msg))
2880 (progn
2881 (setq log (cons msg log))
2882 (setcdr s (cdr (cdr s))))
2883 (setq s (cdr s))))))
2884 ;; (possibly) log each removed message
2885 (while log
2886 (condition-case e
2887 (run-hook-with-args 'remove-message-hook
2888 (car (car log)) (cdr (car log)))
2889 (error (setq remove-message-hook nil)
2890 (message "remove-message-hook error: %s" e)
2891 (sit-for 2)
2892 (erase-buffer (get-buffer " *Echo Area*"))
2893 (signal (car e) (cdr e))))
2894 (setq log (cdr log)))))
2895
2896 (defun append-message (label message &optional frame stdout-p)
2897 (or frame (setq frame (selected-frame)))
2898 ;; add a new entry to the message-stack, or modify an existing one
2899 (let ((top (car message-stack)))
2900 (if (eq label (car top))
2901 (setcdr top (concat (cdr top) message))
2902 (setq message-stack (cons (cons label message) message-stack))))
2903 (raw-append-message message frame stdout-p))
2904
2905 ;; really append the message to the echo area. no fiddling with message-stack.
2906 (defun raw-append-message (message &optional frame stdout-p)
2907 (if (eq message "") nil
2908 (let ((buffer (get-buffer " *Echo Area*"))
2909 (zmacs-region-stays zmacs-region-stays)) ; preserve from change
2910 (save-excursion
2911 (set-buffer buffer)
2912 (insert message))
2913 ;; Conditionalizing on the device type in this way is not that clean,
2914 ;; but neither is having a device method, as I originally implemented
2915 ;; it: all non-stream devices behave in the same way. Perhaps
2916 ;; the cleanest way is to make the concept of a "redisplayable"
2917 ;; device, which stream devices are not. Look into this more if
2918 ;; we ever create another non-redisplayable device type (e.g.
2919 ;; processes? printers?).
2920
2921 ;; Don't redisplay the echo area if we are executing a macro.
2922 (if (not executing-kbd-macro)
2923 (if (eq 'stream (frame-type frame))
2924 (send-string-to-terminal message stdout-p)
2925 (redisplay-echo-area))))))
2926
2927 (defun display-message (label message &optional frame stdout-p)
2928 "Print a one-line message at the bottom of the frame. First argument
2929 LABEL is an identifier for this message. MESSAGE is the string to display.
2930 Use `clear-message' to remove a labelled message.
2931
2932 Here are some standard labels (those marked with `*' are not logged
2933 by default--see the `log-message-ignore-labels' variable):
2934 message default label used by the `message' function
2935 error default label used for reporting errors
2936 * progress progress indicators like \"Converting... 45%\"
2937 * prompt prompt-like messages like \"I-search: foo\"
2938 * no-log messages that should never be logged"
2939 (clear-message label frame stdout-p t)
2940 (append-message label message frame stdout-p))
2941
2942 ;;; may eventually be frame-dependent
2943 (defun current-message-label (frame)
2944 (if message-stack
2945 (car (car message-stack))
2946 nil))
2947
2948 (defun message (fmt &rest args)
2949 "Print a one-line message at the bottom of the frame.
2950 The arguments are the same as to `format'.
2951
2952 If the only argument is nil, clear any existing message; let the
2953 minibuffer contents show."
2954 ;; questionable junk in the C code
2955 ;; (if (framep default-minibuffer-frame)
2956 ;; (make-frame-visible default-minibuffer-frame))
2957 (if (and (null fmt) (null args))
2958 (progn
2959 (clear-message nil)
2960 nil)
2961 (let ((str (apply 'format fmt args)))
2962 (display-message 'message str)
2963 str)))
2964
2965 ;;;;;;
2966 ;;;;;; warning stuff
2967 ;;;;;;
2968
2969 (defvar log-warning-minimum-level 'info
2970 "Minimum level of warnings that should be logged.
2971 The warnings in levels below this are completely ignored, as if they never
2972 happened.
2973
2974 The recognized warning levels, in decreasing order of priority, are
2975 'emergency, 'alert, 'critical, 'error, 'warning, 'notice, 'info, and
2976 'debug.
2977
2978 See also `display-warning-minimum-level'.
2979
2980 You can also control which warnings are displayed on a class-by-class
2981 basis. See `display-warning-suppressed-classes' and
2982 `log-warning-suppressed-classes'.")
2983
2984 (defvar display-warning-minimum-level 'info
2985 "Minimum level of warnings that should be displayed.
2986 The warnings in levels below this are completely ignored, as if they never
2987 happened.
2988
2989 The recognized warning levels, in decreasing order of priority, are
2990 'emergency, 'alert, 'critical, 'error, 'warning, 'notice, 'info, and
2991 'debug.
2992
2993 See also `log-warning-minimum-level'.
2994
2995 You can also control which warnings are displayed on a class-by-class
2996 basis. See `display-warning-suppressed-classes' and
2997 `log-warning-suppressed-classes'.")
2998
2999 (defvar log-warning-suppressed-classes nil
3000 "List of classes of warnings that shouldn't be logged or displayed.
3001 If any of the CLASS symbols associated with a warning is the same as
3002 any of the symbols listed here, the warning will be completely ignored,
3003 as it they never happened.
3004
3005 NOTE: In most circumstances, you should *not* set this variable.
3006 Set `display-warning-suppressed-classes' instead. That way the suppressed
3007 warnings are not displayed but are still unobtrusively logged.
3008
3009 See also `log-warning-minimum-level' and `display-warning-minimum-level'.")
3010
3011 (defvar display-warning-suppressed-classes nil
3012 "List of classes of warnings that shouldn't be displayed.
3013 If any of the CLASS symbols associated with a warning is the same as
3014 any of the symbols listed here, the warning will not be displayed.
3015 The warning will still logged in the *Warnings* buffer (unless also
3016 contained in `log-warning-suppressed-classes'), but the buffer will
3017 not be automatically popped up.
3018
3019 See also `log-warning-minimum-level' and `display-warning-minimum-level'.")
3020
3021 (defvar warning-count 0
3022 "Count of the number of warning messages displayed so far.")
3023
3024 (defconst warning-level-alist '((emergency . 8)
3025 (alert . 7)
3026 (critical . 6)
3027 (error . 5)
3028 (warning . 4)
3029 (notice . 3)
3030 (info . 2)
3031 (debug . 1)))
3032
3033 (defun warning-level-p (level)
3034 "Non-nil if LEVEL specifies a warning level."
3035 (and (symbolp level) (assq level warning-level-alist)))
3036
3037 ;; If you're interested in rewriting this function, be aware that it
3038 ;; could be called at arbitrary points in a Lisp program (when a
3039 ;; built-in function wants to issue a warning, it will call out to
3040 ;; this function the next time some Lisp code is evaluated). Therefore,
3041 ;; this function *must* not permanently modify any global variables
3042 ;; (e.g. the current buffer) except those that specifically apply
3043 ;; to the warning system.
3044
3045 (defvar before-init-deferred-warnings nil)
3046
3047 (defun after-init-display-warnings ()
3048 "Display warnings deferred till after the init file is run.
3049 Warnings that occur before then are deferred so that warning
3050 suppression in the .emacs file will be honored."
3051 (while before-init-deferred-warnings
3052 (apply 'display-warning (car before-init-deferred-warnings))
3053 (setq before-init-deferred-warnings
3054 (cdr before-init-deferred-warnings))))
3055
3056 (add-hook 'after-init-hook 'after-init-display-warnings)
3057
3058 (defun display-warning (class message &optional level)
3059 "Display a warning message.
3060 CLASS should be a symbol describing what sort of warning this is, such
3061 as `resource' or `key-mapping'. A list of such symbols is also
3062 accepted. (Individual classes can be suppressed; see
3063 `display-warning-suppressed-classes'.) Optional argument LEVEL can
3064 be used to specify a priority for the warning, other than default priority
3065 `warning'. (See `display-warning-minimum-level'). The message is
3066 inserted into the *Warnings* buffer, which is made visible at appropriate
3067 times."
3068 (or level (setq level 'warning))
3069 (or (listp class) (setq class (list class)))
3070 (check-argument-type 'warning-level-p level)
3071 (if (not init-file-loaded)
3072 (setq before-init-deferred-warnings
3073 (cons (list class message level) before-init-deferred-warnings))
3074 (catch 'ignored
3075 (let ((display-p t)
3076 (level-num (cdr (assq level warning-level-alist))))
3077 (if (< level-num (cdr (assq log-warning-minimum-level
3078 warning-level-alist)))
3079 (throw 'ignored nil))
3080 (if (intersection class log-warning-suppressed-classes)
3081 (throw 'ignored nil))
3082
3083 (if (< level-num (cdr (assq display-warning-minimum-level
3084 warning-level-alist)))
3085 (setq display-p nil))
3086 (if (and display-p
3087 (intersection class display-warning-suppressed-classes))
3088 (setq display-p nil))
3089 (save-excursion
3090 (let ((buffer (get-buffer-create "*Warnings*")))
3091 (if display-p
3092 ;; The C code looks at display-warning-tick to determine
3093 ;; when it should call `display-warning-buffer'. Change it
3094 ;; to get the C code's attention.
3095 (setq display-warning-tick (1+ display-warning-tick)))
3096 (set-buffer buffer)
3097 (goto-char (point-max))
3098 (setq warning-count (1+ warning-count))
3099 (princ (format "(%d) (%s/%s) "
3100 warning-count
3101 (mapconcat 'symbol-name class ",")
3102 level) buffer)
3103 (princ message buffer)
3104 (terpri buffer)
3105 (terpri buffer)))))))
3106
3107 (defun warn (&rest args)
3108 "Display a warning message.
3109 The message is constructed by passing all args to `format'. The message
3110 is placed in the *Warnings* buffer, which will be popped up at the next
3111 redisplay. The class of the warning is `warning'. See also
3112 `display-warning'."
3113 (display-warning 'warning (apply 'format args)))
3114
3115 (defvar warning-marker nil)
3116
3117 ;; When this function is called by the C code, all non-local exits are
3118 ;; trapped and C-g is inhibited; therefore, it would be a very, very
3119 ;; bad idea for this function to get into an infinite loop.
3120
3121 (defun display-warning-buffer ()
3122 "Make the buffer that contains the warnings be visible.
3123 The C code calls this periodically, right before redisplay."
3124 (let ((buffer (get-buffer-create "*Warnings*")))
3125 (if (or (not warning-marker) (not (eq (marker-buffer warning-marker)
3126 buffer)))
3127 (progn
3128 (setq warning-marker (make-marker))
3129 (set-marker warning-marker 1 buffer)))
3130 (set-window-start (display-buffer buffer) warning-marker)
3131 (set-marker warning-marker (point-max buffer) buffer)))