Contents of release-21-2 at 1999-07-05-18.
[chise/xemacs-chise.git.1] / lisp / simple.el
1 ;;; simple.el --- basic editing commands for XEmacs
2
3 ;; Copyright (C) 1985-7, 1993-5, 1997 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Tinker Systems and INS Engineering Corp.
5
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: lisp, extensions, internal, dumped
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 ;; 02111-1307, USA.
25
26 ;;; Synched up with: FSF 19.34 [But not very closely].
27
28 ;;; Commentary:
29
30 ;; This file is dumped with XEmacs.
31
32 ;; A grab-bag of basic XEmacs commands not specifically related to some
33 ;; major mode or to file-handling.
34
35 ;; Changes for zmacs-style active-regions:
36 ;;
37 ;; beginning-of-buffer, end-of-buffer, count-lines-region,
38 ;; count-lines-buffer, what-line, what-cursor-position, set-goal-column,
39 ;; set-fill-column, prefix-arg-internal, and line-move (which is used by
40 ;; next-line and previous-line) set zmacs-region-stays to t, so that they
41 ;; don't affect the current region-hilighting state.
42 ;;
43 ;; mark-whole-buffer, mark-word, exchange-point-and-mark, and
44 ;; set-mark-command (without an argument) call zmacs-activate-region.
45 ;;
46 ;; mark takes an optional arg like the new Fmark_marker() does.  When
47 ;; the region is not active, mark returns nil unless the optional arg is true.
48 ;;
49 ;; push-mark, pop-mark, exchange-point-and-mark, and set-marker, and
50 ;; set-mark-command use (mark t) so that they can access the mark whether
51 ;; the region is active or not.
52 ;;
53 ;; shell-command, shell-command-on-region, yank, and yank-pop (which all
54 ;; push a mark) have been altered to call exchange-point-and-mark with an
55 ;; argument, meaning "don't activate the region".  These commands  only use
56 ;; exchange-point-and-mark to position the newly-pushed mark correctly, so
57 ;; this isn't a user-visible change.  These functions have also been altered
58 ;; to use (mark t) for the same reason.
59
60 ;; 97/3/14 Jareth Hein (jhod@po.iijnet.or.jp) added kinsoku processing (support
61 ;; for filling of Asian text) into the fill code. This was ripped bleeding from
62 ;; Mule-2.3, and could probably use some feature additions (like additional wrap
63 ;; styles, etc)
64
65 ;; 97/06/11 Steve Baur (steve@altair.xemacs.org) Convert use of
66 ;;  (preceding|following)-char to char-(after|before).
67
68 ;;; Code:
69
70 (defgroup editing-basics nil
71   "Most basic editing variables."
72   :group 'editing)
73
74 (defgroup killing nil
75   "Killing and yanking commands."
76   :group 'editing)
77
78 (defgroup fill-comments nil
79   "Indenting and filling of comments."
80   :prefix "comment-"
81   :group 'fill)
82
83 (defgroup paren-matching nil
84   "Highlight (un)matching of parens and expressions."
85   :prefix "paren-"
86   :group 'matching)
87
88 (defgroup log-message nil
89   "Messages logging and display customizations."
90   :group 'minibuffer)
91
92 (defgroup warnings nil
93   "Warnings customizations."
94   :group 'minibuffer)
95
96
97 (defcustom search-caps-disable-folding t
98   "*If non-nil, upper case chars disable case fold searching.
99 This does not apply to \"yanked\" strings."
100   :type 'boolean
101   :group 'editing-basics)
102
103 ;; This is stolen (and slightly modified) from FSF emacs's
104 ;; `isearch-no-upper-case-p'.
105 (defun no-upper-case-p (string &optional regexp-flag)
106   "Return t if there are no upper case chars in STRING.
107 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
108 since they have special meaning in a regexp."
109   (let ((case-fold-search nil))
110     (not (string-match (if regexp-flag 
111                            "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]"
112                          "[A-Z]")
113                        string))
114     ))
115
116 (defmacro with-search-caps-disable-folding (string regexp-flag &rest body) "\
117 Eval BODY with `case-fold-search' let to nil if `search-caps-disable-folding' 
118 is non-nil, and if STRING (either a string or a regular expression according
119 to REGEXP-FLAG) contains uppercase letters."
120   `(let ((case-fold-search
121           (if (and case-fold-search search-caps-disable-folding)
122               (no-upper-case-p ,string ,regexp-flag)
123             case-fold-search)))
124      ,@body))
125 (put 'with-search-caps-disable-folding 'lisp-indent-function 2)
126 (put 'with-search-caps-disable-folding 'edebug-form-spec 
127      '(sexp sexp &rest form))
128
129 (defmacro with-interactive-search-caps-disable-folding (string regexp-flag 
130                                                                &rest body)
131   "Same as `with-search-caps-disable-folding', but only in the case of a
132 function called interactively."
133   `(let ((case-fold-search
134           (if (and (interactive-p) 
135                    case-fold-search search-caps-disable-folding)
136               (no-upper-case-p ,string ,regexp-flag)
137             case-fold-search)))
138      ,@body))
139 (put 'with-interactive-search-caps-disable-folding 'lisp-indent-function 2)
140 (put 'with-interactive-search-caps-disable-folding 'edebug-form-spec 
141      '(sexp sexp &rest form))
142
143 (defun newline (&optional arg)
144   "Insert a newline, and move to left margin of the new line if it's blank.
145 The newline is marked with the text-property `hard'.
146 With arg, insert that many newlines.
147 In Auto Fill mode, if no numeric arg, break the preceding line if it's long."
148   (interactive "*P")
149   (barf-if-buffer-read-only nil (point))
150   ;; Inserting a newline at the end of a line produces better redisplay in
151   ;; try_window_id than inserting at the beginning of a line, and the textual
152   ;; result is the same.  So, if we're at beginning of line, pretend to be at
153   ;; the end of the previous line.
154   ;; #### Does this have any relevance in XEmacs?
155   (let ((flag (and (not (bobp))
156                    (bolp)
157                    ;; Make sure the newline before point isn't intangible.
158                    (not (get-char-property (1- (point)) 'intangible))
159                    ;; Make sure the newline before point isn't read-only.
160                    (not (get-char-property (1- (point)) 'read-only))
161                    ;; Make sure the newline before point isn't invisible.
162                    (not (get-char-property (1- (point)) 'invisible))
163                    ;; This should probably also test for the previous char
164                    ;;  being the *last* character too.
165                    (not (get-char-property (1- (point)) 'end-open))
166                    ;; Make sure the newline before point has the same
167                    ;; properties as the char before it (if any).
168                    (< (or (previous-extent-change (point)) -2)
169                       (- (point) 2))))
170         (was-page-start (and (bolp)
171                              (looking-at page-delimiter)))
172         (beforepos (point)))
173     (if flag (backward-char 1))
174     ;; Call self-insert so that auto-fill, abbrev expansion etc. happens.
175     ;; Set last-command-char to tell self-insert what to insert.
176     (let ((last-command-char ?\n)
177           ;; Don't auto-fill if we have a numeric argument.
178           ;; Also not if flag is true (it would fill wrong line);
179           ;; there is no need to since we're at BOL.
180           (auto-fill-function (if (or arg flag) nil auto-fill-function)))
181       (unwind-protect
182           (self-insert-command (prefix-numeric-value arg))
183         ;; If we get an error in self-insert-command, put point at right place.
184         (if flag (forward-char 1))))
185     ;; If we did *not* get an error, cancel that forward-char.
186     (if flag (backward-char 1))
187     ;; Mark the newline(s) `hard'.
188     (if use-hard-newlines
189         (let* ((from (- (point) (if arg (prefix-numeric-value arg) 1)))
190                (sticky (get-text-property from 'end-open))) ; XEmacs
191           (put-text-property from (point) 'hard 't)
192           ;; If end-open is not "t", add 'hard to end-open list
193           (if (and (listp sticky) (not (memq 'hard sticky)))
194               (put-text-property from (point) 'end-open ; XEmacs
195                                  (cons 'hard sticky)))))
196     ;; If the newline leaves the previous line blank,
197     ;; and we have a left margin, delete that from the blank line.
198     (or flag
199         (save-excursion
200           (goto-char beforepos)
201           (beginning-of-line)
202           (and (looking-at "[ \t]$")
203                (> (current-left-margin) 0)
204                (delete-region (point) (progn (end-of-line) (point))))))
205     (if flag (forward-char 1))
206     ;; Indent the line after the newline, except in one case:
207     ;; when we added the newline at the beginning of a line
208     ;; which starts a page.
209     (or was-page-start
210         (move-to-left-margin nil t)))
211   nil)
212
213 (defun set-hard-newline-properties (from to)
214   (let ((sticky (get-text-property from 'rear-nonsticky)))
215     (put-text-property from to 'hard 't)
216     ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list
217     (if (and (listp sticky) (not (memq 'hard sticky)))
218         (put-text-property from (point) 'rear-nonsticky
219                            (cons 'hard sticky)))))
220
221 (defun open-line (arg)
222   "Insert a newline and leave point before it.
223 If there is a fill prefix and/or a left-margin, insert them on the new line
224 if the line would have been blank.
225 With arg N, insert N newlines."
226   (interactive "*p")
227   (let* ((do-fill-prefix (and fill-prefix (bolp)))
228          (do-left-margin (and (bolp) (> (current-left-margin) 0)))
229          (loc (point)))
230     (newline arg)
231     (goto-char loc)
232     (while (> arg 0)
233       (cond ((bolp)
234              (if do-left-margin (indent-to (current-left-margin)))
235              (if do-fill-prefix (insert fill-prefix))))
236       (forward-line 1)
237       (setq arg (1- arg)))
238     (goto-char loc)
239     (end-of-line)))
240
241 (defun split-line ()
242   "Split current line, moving portion beyond point vertically down."
243   (interactive "*")
244   (skip-chars-forward " \t")
245   (let ((col (current-column))
246         (pos (point)))
247     (newline 1)
248     (indent-to col 0)
249     (goto-char pos)))
250
251 (defun quoted-insert (arg)
252   "Read next input character and insert it.
253 This is useful for inserting control characters.
254 You may also type up to 3 octal digits, to insert a character with that code.
255
256 In overwrite mode, this function inserts the character anyway, and
257 does not handle octal digits specially.  This means that if you use
258 overwrite as your normal editing mode, you can use this function to
259 insert characters when necessary.
260
261 In binary overwrite mode, this function does overwrite, and octal
262 digits are interpreted as a character code.  This is supposed to make
263 this function useful in editing binary files."
264   (interactive "*p")
265   (let ((char (if (or (not overwrite-mode)
266                       (eq overwrite-mode 'overwrite-mode-binary))
267                   (read-quoted-char)
268                 ;; read-char obeys C-g, so we should protect.  FSF
269                 ;; doesn't have the protection here, but it's a bug in
270                 ;; FSF.
271                 (let ((inhibit-quit t))
272                   (read-char)))))
273     (if (> arg 0)
274         (if (eq overwrite-mode 'overwrite-mode-binary)
275             (delete-char arg)))
276     (while (> arg 0)
277       (insert char)
278       (setq arg (1- arg)))))
279
280 (defun delete-indentation (&optional arg)
281   "Join this line to previous and fix up whitespace at join.
282 If there is a fill prefix, delete it from the beginning of this line.
283 With argument, join this line to following line."
284   (interactive "*P")
285   (beginning-of-line)
286   (if arg (forward-line 1))
287   (if (eq (char-before (point)) ?\n)
288       (progn
289         (delete-region (point) (1- (point)))
290         ;; If the second line started with the fill prefix,
291         ;; delete the prefix.
292         (if (and fill-prefix
293                  (<= (+ (point) (length fill-prefix)) (point-max))
294                  (string= fill-prefix
295                           (buffer-substring (point)
296                                             (+ (point) (length fill-prefix)))))
297             (delete-region (point) (+ (point) (length fill-prefix))))
298         (fixup-whitespace))))
299
300 (defun fixup-whitespace ()
301   "Fixup white space between objects around point.
302 Leave one space or none, according to the context."
303   (interactive "*")
304   (save-excursion
305     (delete-horizontal-space)
306     (if (or (looking-at "^\\|\\s)")
307             (save-excursion (forward-char -1)
308                             (looking-at "$\\|\\s(\\|\\s'")))
309         nil
310       (insert ?\ ))))
311
312 (defun delete-horizontal-space ()
313   "Delete all spaces and tabs around point."
314   (interactive "*")
315   (skip-chars-backward " \t")
316   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
317
318 (defun just-one-space ()
319   "Delete all spaces and tabs around point, leaving one space."
320   (interactive "*")
321   (if abbrev-mode ; XEmacs
322       (expand-abbrev))
323   (skip-chars-backward " \t")
324   (if (eq (char-after (point)) ? ) ; XEmacs
325       (forward-char 1)
326     (insert ? ))
327   (delete-region (point) (progn (skip-chars-forward " \t") (point))))
328
329 (defun delete-blank-lines ()
330   "On blank line, delete all surrounding blank lines, leaving just one.
331 On isolated blank line, delete that one.
332 On nonblank line, delete any immediately following blank lines."
333   (interactive "*")
334   (let (thisblank singleblank)
335     (save-excursion
336       (beginning-of-line)
337       (setq thisblank (looking-at "[ \t]*$"))
338       ;; Set singleblank if there is just one blank line here.
339       (setq singleblank
340             (and thisblank
341                  (not (looking-at "[ \t]*\n[ \t]*$"))
342                  (or (bobp)
343                      (progn (forward-line -1)
344                             (not (looking-at "[ \t]*$")))))))
345     ;; Delete preceding blank lines, and this one too if it's the only one.
346     (if thisblank
347         (progn
348           (beginning-of-line)
349           (if singleblank (forward-line 1))
350           (delete-region (point)
351                          (if (re-search-backward "[^ \t\n]" nil t)
352                              (progn (forward-line 1) (point))
353                            (point-min)))))
354     ;; Delete following blank lines, unless the current line is blank
355     ;; and there are no following blank lines.
356     (if (not (and thisblank singleblank))
357         (save-excursion
358           (end-of-line)
359           (forward-line 1)
360           (delete-region (point)
361                          (if (re-search-forward "[^ \t\n]" nil t)
362                              (progn (beginning-of-line) (point))
363                            (point-max)))))
364     ;; Handle the special case where point is followed by newline and eob.
365     ;; Delete the line, leaving point at eob.
366     (if (looking-at "^[ \t]*\n\\'")
367         (delete-region (point) (point-max)))))
368
369 (defun back-to-indentation ()
370   "Move point to the first non-whitespace character on this line."
371   ;; XEmacs change
372   (interactive "_")
373   (beginning-of-line 1)
374   (skip-chars-forward " \t"))
375
376 (defun newline-and-indent ()
377   "Insert a newline, then indent according to major mode.
378 Indentation is done using the value of `indent-line-function'.
379 In programming language modes, this is the same as TAB.
380 In some text modes, where TAB inserts a tab, this command indents to the
381 column specified by the function `current-left-margin'."
382   (interactive "*")
383   (delete-region (point) (progn (skip-chars-backward " \t") (point)))
384   (newline)
385   (indent-according-to-mode))
386
387 (defun reindent-then-newline-and-indent ()
388   "Reindent current line, insert newline, then indent the new line.
389 Indentation of both lines is done according to the current major mode,
390 which means calling the current value of `indent-line-function'.
391 In programming language modes, this is the same as TAB.
392 In some text modes, where TAB inserts a tab, this indents to the
393 column specified by the function `current-left-margin'."
394   (interactive "*")
395   (save-excursion
396     (delete-region (point) (progn (skip-chars-backward " \t") (point)))
397     (indent-according-to-mode))
398   (newline)
399   (indent-according-to-mode))
400
401 ;; Internal subroutine of delete-char
402 (defun kill-forward-chars (arg)
403   (if (listp arg) (setq arg (car arg)))
404   (if (eq arg '-) (setq arg -1))
405   (kill-region (point) (+ (point) arg)))
406
407 ;; Internal subroutine of backward-delete-char
408 (defun kill-backward-chars (arg)
409   (if (listp arg) (setq arg (car arg)))
410   (if (eq arg '-) (setq arg -1))
411   (kill-region (point) (- (point) arg)))
412
413 (defun backward-delete-char-untabify (arg &optional killp)
414   "Delete characters backward, changing tabs into spaces.
415 Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil.
416 Interactively, ARG is the prefix arg (default 1)
417 and KILLP is t if a prefix arg was specified."
418   (interactive "*p\nP")
419   (let ((count arg))
420     (save-excursion
421       (while (and (> count 0) (not (bobp)))
422         (if (eq (char-before (point)) ?\t) ; XEmacs
423             (let ((col (current-column)))
424               (forward-char -1)
425               (setq col (- col (current-column)))
426               (insert-char ?\ col)
427               (delete-char 1)))
428         (forward-char -1)
429         (setq count (1- count)))))
430   (delete-backward-char arg killp)
431   ;; XEmacs: In overwrite mode, back over columns while clearing them out,
432   ;; unless at end of line.
433   (and overwrite-mode (not (eolp))
434        (save-excursion (insert-char ?\  arg))))
435
436 (defcustom delete-key-deletes-forward t
437   "*If non-nil, the DEL key will erase one character forwards.
438 If nil, the DEL key will erase one character backwards."
439   :type 'boolean
440   :group 'editing-basics)
441
442 (defcustom backward-delete-function 'backward-delete-char
443   "*Function called to delete backwards on a delete keypress.
444 If `delete-key-deletes-forward' is nil, `backward-or-forward-delete-char'
445 calls this function to erase one character backwards.  Default value
446 is 'backward-delete-char, with 'backward-delete-char-untabify being a
447 popular alternate setting."
448   :type 'function
449   :group 'editing-basics)
450
451 ;; Trash me, baby.
452 (defsubst delete-forward-p ()
453   (and delete-key-deletes-forward
454        (or (not (eq (device-type) 'x))
455            (x-keysym-on-keyboard-sans-modifiers-p 'backspace))))
456
457 (defun backward-or-forward-delete-char (arg)
458   "Delete either one character backwards or one character forwards.
459 Controlled by the state of `delete-key-deletes-forward' and whether the
460 BackSpace keysym even exists on your keyboard.  If you don't have a
461 BackSpace keysym, the delete key should always delete one character
462 backwards."
463   (interactive "*p")
464   (if (delete-forward-p)
465       (delete-char arg)
466     (funcall backward-delete-function arg)))
467
468 (defun backward-or-forward-kill-word (arg)
469   "Delete either one word backwards or one word forwards.
470 Controlled by the state of `delete-key-deletes-forward' and whether the
471 BackSpace keysym even exists on your keyboard.  If you don't have a
472 BackSpace keysym, the delete key should always delete one character
473 backwards."
474   (interactive "*p")
475   (if (delete-forward-p)
476       (kill-word arg)
477     (backward-kill-word arg)))
478
479 (defun backward-or-forward-kill-sentence (arg)
480     "Delete either one sentence backwards or one sentence forwards.
481 Controlled by the state of `delete-key-deletes-forward' and whether the
482 BackSpace keysym even exists on your keyboard.  If you don't have a
483 BackSpace keysym, the delete key should always delete one character
484 backwards."
485   (interactive "*P")
486   (if (delete-forward-p)
487       (kill-sentence arg)
488     (backward-kill-sentence (prefix-numeric-value arg))))
489
490 (defun backward-or-forward-kill-sexp (arg)
491     "Delete either one sexpr backwards or one sexpr forwards.
492 Controlled by the state of `delete-key-deletes-forward' and whether the
493 BackSpace keysym even exists on your keyboard.  If you don't have a
494 BackSpace keysym, the delete key should always delete one character
495 backwards."
496   (interactive "*p")
497   (if (delete-forward-p)
498       (kill-sexp arg)
499     (backward-kill-sexp arg)))
500
501 (defun zap-to-char (arg char)
502   "Kill up to and including ARG'th occurrence of CHAR.
503 Goes backward if ARG is negative; error if CHAR not found."
504   (interactive "*p\ncZap to char: ")
505   (kill-region (point) (with-interactive-search-caps-disable-folding
506                            (char-to-string char) nil
507                          (search-forward (char-to-string char) nil nil arg)
508                          (point))))
509
510 (defun zap-up-to-char (arg char)
511   "Kill up to ARG'th occurrence of CHAR.
512 Goes backward if ARG is negative; error if CHAR not found."
513   (interactive "*p\ncZap up to char: ")
514   (kill-region (point) (with-interactive-search-caps-disable-folding
515                            (char-to-string char) nil
516                          (search-forward (char-to-string char) nil nil arg)
517                          (goto-char (if (> arg 0) (1- (point)) (1+ (point))))
518                          (point))))
519
520 (defun beginning-of-buffer (&optional arg)
521   "Move point to the beginning of the buffer; leave mark at previous position.
522 With arg N, put point N/10 of the way from the beginning.
523
524 If the buffer is narrowed, this command uses the beginning and size
525 of the accessible part of the buffer.
526
527 Don't use this command in Lisp programs!
528 \(goto-char (point-min)) is faster and avoids clobbering the mark."
529   ;; XEmacs change
530   (interactive "_P")
531   (push-mark)
532   (let ((size (- (point-max) (point-min))))
533     (goto-char (if arg
534                    (+ (point-min)
535                       (if (> size 10000)
536                           ;; Avoid overflow for large buffer sizes!
537                           (* (prefix-numeric-value arg)
538                              (/ size 10))
539                         (/ (+ 10 (* size (prefix-numeric-value arg))) 10)))
540                  (point-min))))
541   (if arg (forward-line 1)))
542
543 (defun end-of-buffer (&optional arg)
544   "Move point to the end of the buffer; leave mark at previous position.
545 With arg N, put point N/10 of the way from the end.
546
547 If the buffer is narrowed, this command uses the beginning and size
548 of the accessible part of the buffer.
549
550 Don't use this command in Lisp programs!
551 \(goto-char (point-max)) is faster and avoids clobbering the mark."
552   ;; XEmacs change
553   (interactive "_P")
554   (push-mark)
555   ;; XEmacs changes here.
556   (let ((scroll-to-end (not (pos-visible-in-window-p (point-max))))
557         (size (- (point-max) (point-min))))
558     (goto-char (if arg
559                    (- (point-max)
560                       (if (> size 10000)
561                           ;; Avoid overflow for large buffer sizes!
562                           (* (prefix-numeric-value arg)
563                              (/ size 10))
564                         (/ (* size (prefix-numeric-value arg)) 10)))
565                  (point-max)))
566     (cond (arg
567            ;; If we went to a place in the middle of the buffer,
568            ;; adjust it to the beginning of a line.
569            (forward-line 1))
570           ;; XEmacs change
571           (scroll-to-end
572            ;; If the end of the buffer is not already on the screen,
573            ;; then scroll specially to put it near, but not at, the bottom.
574            (recenter -3)))))
575
576 ;; XEmacs (not in FSF)
577 (defun mark-beginning-of-buffer (&optional arg)
578   "Push a mark at the beginning of the buffer; leave point where it is.
579 With arg N, push mark N/10 of the way from the true beginning."
580   (interactive "P")
581   (push-mark (if arg
582                  (if (> (buffer-size) 10000)
583                      ;; Avoid overflow for large buffer sizes!
584                      (* (prefix-numeric-value arg)
585                         (/ (buffer-size) 10))
586                    (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10))
587                (point-min))
588              nil
589              t))
590 (define-function 'mark-bob 'mark-beginning-of-buffer)
591
592 ;; XEmacs (not in FSF)
593 (defun mark-end-of-buffer (&optional arg)
594   "Push a mark at the end of the buffer; leave point where it is.
595 With arg N, push mark N/10 of the way from the true end."
596   (interactive "P")
597   (push-mark (if arg
598                  (- (1+ (buffer-size))
599                     (if (> (buffer-size) 10000)
600                         ;; Avoid overflow for large buffer sizes!
601                         (* (prefix-numeric-value arg)
602                            (/ (buffer-size) 10))
603                       (/ (* (buffer-size) (prefix-numeric-value arg)) 10)))
604                  (point-max))
605              nil
606              t))
607 (define-function 'mark-eob 'mark-end-of-buffer)
608
609 (defun mark-whole-buffer ()
610   "Put point at beginning and mark at end of buffer.
611 You probably should not use this function in Lisp programs;
612 it is usually a mistake for a Lisp function to use any subroutine
613 that uses or sets the mark."
614   (interactive)
615   (push-mark (point))
616   (push-mark (point-max) nil t)
617   (goto-char (point-min)))
618
619 ;; XEmacs
620 (defun eval-current-buffer (&optional printflag)
621   "Evaluate the current buffer as Lisp code.
622 Programs can pass argument PRINTFLAG which controls printing of output:
623 nil means discard it; anything else is stream for print."
624   (interactive)
625   (eval-buffer (current-buffer) printflag))
626
627 ;; XEmacs
628 (defun count-words-buffer (&optional buffer)
629   "Print the number of words in BUFFER.
630 If called noninteractively, the value is returned rather than printed.
631 BUFFER defaults to the current buffer."
632   (interactive)
633   (let ((words (count-words-region (point-min) (point-max) buffer)))
634     (when (interactive-p)
635       (message "Buffer has %d words" words))
636     words))
637
638 ;; XEmacs
639 (defun count-words-region (start end &optional buffer)
640   "Print the number of words in region between START and END in BUFFER.
641 If called noninteractively, the value is returned rather than printed.
642 BUFFER defaults to the current buffer."
643   (interactive "_r")
644   (save-excursion
645     (set-buffer (or buffer (current-buffer)))
646     (let ((words 0))
647       (goto-char start)
648       (while (< (point) end)
649         (when (forward-word 1)
650           (incf words)))
651       (when  (interactive-p)
652         (message "Region has %d words" words))
653       words)))
654
655 (defun count-lines-region (start end)
656   "Print number of lines and characters in the region."
657   ;; XEmacs change
658   (interactive "_r")
659   (message "Region has %d lines, %d characters"
660            (count-lines start end) (- end start)))
661
662 ;; XEmacs
663 (defun count-lines-buffer (&optional buffer)
664   "Print number of lines and characters in BUFFER."
665   (interactive)
666   (with-current-buffer (or buffer (current-buffer))
667     (let ((cnt (count-lines (point-min) (point-max))))
668       (message "Buffer has %d lines, %d characters"
669                cnt (- (point-max) (point-min)))
670       cnt)))
671
672 ;;; Modified by Bob Weiner, 8/24/95, to print narrowed line number also.
673 ;;; Expanded by Bob Weiner, Altrasoft, on 02/12/1997
674 (defun what-line ()
675   "Print the following variants of the line number of point:
676      Region line     - displayed line within the active region
677      Collapsed line  - includes only selectively displayed lines;
678      Buffer line     - physical line in the buffer;
679      Narrowed line   - line number from the start of the buffer narrowing."
680   ;; XEmacs change
681   (interactive "_")
682   (let ((opoint (point)) start)
683     (save-excursion
684       (save-restriction
685         (if (region-active-p)
686             (goto-char (region-beginning))
687           (goto-char (point-min)))
688         (widen)
689         (beginning-of-line)
690         (setq start (point))
691         (goto-char opoint)
692         (beginning-of-line)
693         (let* ((buffer-line (1+ (count-lines 1 (point))))
694                (narrowed-p (or (/= start 1)
695                                (/= (point-max) (1+ (buffer-size)))))
696                (narrowed-line (if narrowed-p (1+ (count-lines start (point)))))
697                (selective-line (if selective-display
698                                    (1+ (count-lines start (point) t))))
699                (region-line (if (region-active-p)
700                                 (1+ (count-lines start (point) selective-display)))))
701           (cond (region-line
702                  (message "Region line %d; Buffer line %d"
703                           region-line buffer-line))
704                 ((and narrowed-p selective-line (/= selective-line narrowed-line))
705                  ;; buffer narrowed and some lines selectively displayed
706                  (message "Collapsed line %d; Buffer line %d; Narrowed line %d"
707                           selective-line buffer-line narrowed-line))
708                 (narrowed-p
709                  ;; buffer narrowed
710                  (message "Buffer line %d; Narrowed line %d"
711                           buffer-line narrowed-line))
712                 ((and selective-line (/= selective-line buffer-line))
713                  ;; some lines selectively displayed
714                  (message "Collapsed line %d; Buffer line %d"
715                           selective-line buffer-line))
716                 (t
717                  ;; give a basic line count
718                  (message "Line %d" buffer-line)))))))
719   (setq zmacs-region-stays t))
720
721 ;;; Bob Weiner, Altrasoft, 02/12/1998
722 ;;; Added the 3rd arg in `count-lines' to conditionalize the counting of
723 ;;; collapsed lines.
724 (defun count-lines (start end &optional ignore-invisible-lines-flag)
725   "Return number of lines between START and END.
726 This is usually the number of newlines between them,
727 but can be one more if START is not equal to END
728 and the greater of them is not at the start of a line.
729
730 With optional IGNORE-INVISIBLE-LINES-FLAG non-nil, lines collapsed with
731 selective-display are excluded from the line count."
732   (save-excursion
733     (save-restriction
734       (narrow-to-region start end)
735       (goto-char (point-min))
736       (if (and (not ignore-invisible-lines-flag) (eq selective-display t))
737           (save-match-data
738             (let ((done 0))
739               (while (re-search-forward "[\n\C-m]" nil t 40)
740                 (setq done (+ 40 done)))
741               (while (re-search-forward "[\n\C-m]" nil t 1)
742                 (setq done (+ 1 done)))
743               (goto-char (point-max))
744               (if (and (/= start end)
745                        (not (bolp)))
746                   (1+ done)
747                 done)))
748         (- (buffer-size) (forward-line (buffer-size)))))))
749
750 (defun what-cursor-position ()
751   "Print info on cursor position (on screen and within buffer)."
752   ;; XEmacs change
753   (interactive "_")
754   (let* ((char (char-after (point))) ; XEmacs
755          (beg (point-min))
756          (end (point-max))
757          (pos (point))
758          (total (buffer-size))
759          (percent (if (> total 50000)
760                       ;; Avoid overflow from multiplying by 100!
761                       (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1))
762                     (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1))))
763          (hscroll (if (= (window-hscroll) 0)
764                       ""
765                     (format " Hscroll=%d" (window-hscroll))))
766          (col (+ (current-column) (if column-number-start-at-one 1 0))))
767     (if (= pos end)
768         (if (or (/= beg 1) (/= end (1+ total)))
769             (message "point=%d of %d(%d%%) <%d - %d>  column %d %s"
770                      pos total percent beg end col hscroll)
771           (message "point=%d of %d(%d%%)  column %d %s"
772                    pos total percent col hscroll))
773       ;; XEmacs: don't use single-key-description
774       (if (or (/= beg 1) (/= end (1+ total)))
775           (message "Char: %s (0%o, %d, 0x%x)  point=%d of %d(%d%%) <%d - %d>  column %d %s"
776                    (text-char-description char) char char char pos total
777                    percent beg end col hscroll)
778         (message "Char: %s (0%o, %d, 0x%x)  point=%d of %d(%d%%)  column %d %s"
779                  (text-char-description char) char char char pos total
780                  percent col hscroll)))))
781
782 (defun fundamental-mode ()
783   "Major mode not specialized for anything in particular.
784 Other major modes are defined by comparison with this one."
785   (interactive)
786   (kill-all-local-variables))
787
788 ;; XEmacs the following are declared elsewhere
789 ;(defvar read-expression-map (cons 'keymap minibuffer-local-map)
790 ;  "Minibuffer keymap used for reading Lisp expressions.")
791 ;(define-key read-expression-map "\M-\t" 'lisp-complete-symbol)
792
793 ;(put 'eval-expression 'disabled t)
794
795 ;(defvar read-expression-history nil)
796
797 ;; We define this, rather than making `eval' interactive,
798 ;; for the sake of completion of names like eval-region, eval-current-buffer.
799 (defun eval-expression (expression &optional eval-expression-insert-value)
800   "Evaluate EXPRESSION and print value in minibuffer.
801 Value is also consed on to front of the variable `values'.
802 With prefix argument, insert the result to the current buffer."
803   ;(interactive "xEval: ")
804   (interactive
805    (list (read-from-minibuffer "Eval: "
806                                nil read-expression-map t
807                                'read-expression-history)
808          current-prefix-arg))
809   (setq values (cons (eval expression) values))
810   (prin1 (car values)
811          (if eval-expression-insert-value (current-buffer) t)))
812
813 ;; XEmacs -- extra parameter (variant, but equivalent logic)
814 (defun edit-and-eval-command (prompt command &optional history)
815   "Prompting with PROMPT, let user edit COMMAND and eval result.
816 COMMAND is a Lisp expression.  Let user edit that expression in
817 the minibuffer, then read and evaluate the result."
818   (let ((command (read-expression prompt
819                                   ;; first try to format the thing readably;
820                                   ;; and if that fails, print it normally.
821                                   (condition-case ()
822                                       (let ((print-readably t))
823                                         (prin1-to-string command))
824                                     (error (prin1-to-string command)))
825                                   (or history '(command-history . 1)))))
826     (or history (setq history 'command-history))
827     (if (consp history)
828         (setq history (car history)))
829     (if (eq history t)
830         nil
831       ;; If command was added to the history as a string,
832       ;; get rid of that.  We want only evallable expressions there.
833       (if (stringp (car (symbol-value history)))
834           (set history (cdr (symbol-value history))))
835
836       ;; If command to be redone does not match front of history,
837       ;; add it to the history.
838       (or (equal command (car (symbol-value history)))
839           (set history (cons command (symbol-value history)))))
840     (eval command)))
841
842 (defun repeat-complex-command (arg)
843   "Edit and re-evaluate last complex command, or ARGth from last.
844 A complex command is one which used the minibuffer.
845 The command is placed in the minibuffer as a Lisp form for editing.
846 The result is executed, repeating the command as changed.
847 If the command has been changed or is not the most recent previous command
848 it is added to the front of the command history.
849 You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element]
850 to get different commands to edit and resubmit."
851   (interactive "p")
852   ;; XEmacs: It looks like our version is better -sb
853   (let ((print-level nil))
854     (edit-and-eval-command "Redo: "
855                            (or (nth (1- arg) command-history)
856                                (error ""))
857                            (cons 'command-history arg))))
858
859 ;; XEmacs: Functions moved to minibuf.el
860 ;; previous-matching-history-element
861 ;; next-matching-history-element
862 ;; next-history-element
863 ;; previous-history-element
864 ;; next-complete-history-element
865 ;; previous-complete-history-element
866 \f
867 (defun goto-line (arg)
868   "Goto line ARG, counting from line 1 at beginning of buffer."
869   (interactive "NGoto line: ")
870   (setq arg (prefix-numeric-value arg))
871   (save-restriction
872     (widen)
873     (goto-char 1)
874     (if (eq selective-display t)
875         (re-search-forward "[\n\C-m]" nil 'end (1- arg))
876       (forward-line (1- arg)))))
877
878 ;Put this on C-x u, so we can force that rather than C-_ into startup msg
879 (define-function 'advertised-undo 'undo)
880
881 (defun undo (&optional arg)
882   "Undo some previous changes.
883 Repeat this command to undo more changes.
884 A numeric argument serves as a repeat count."
885   (interactive "*p")
886   ;; If we don't get all the way through, make last-command indicate that
887   ;; for the following command.
888   (setq this-command t)
889   (let ((modified (buffer-modified-p))
890         (recent-save (recent-auto-save-p)))
891     (or (eq (selected-window) (minibuffer-window))
892         (display-message 'command "Undo!"))
893     (or (and (eq last-command 'undo)
894              (eq (current-buffer) last-undo-buffer)) ; XEmacs
895         (progn (undo-start)
896                (undo-more 1)))
897     (undo-more (or arg 1))
898     ;; Don't specify a position in the undo record for the undo command.
899     ;; Instead, undoing this should move point to where the change is.
900     (let ((tail buffer-undo-list)
901           done)
902       (while (and tail (not done) (not (null (car tail))))
903         (if (integerp (car tail))
904             (progn
905               (setq done t)
906               (setq buffer-undo-list (delq (car tail) buffer-undo-list))))
907         (setq tail (cdr tail))))
908     (and modified (not (buffer-modified-p))
909          (delete-auto-save-file-if-necessary recent-save)))
910   ;; If we do get all the way through, make this-command indicate that.
911   (setq this-command 'undo))
912
913 (defvar pending-undo-list nil
914   "Within a run of consecutive undo commands, list remaining to be undone.")
915
916 (defvar last-undo-buffer nil)   ; XEmacs
917
918 (defun undo-start ()
919   "Set `pending-undo-list' to the front of the undo list.
920 The next call to `undo-more' will undo the most recently made change."
921   (if (eq buffer-undo-list t)
922       (error "No undo information in this buffer"))
923   (setq pending-undo-list buffer-undo-list))
924
925 (defun undo-more (count)
926   "Undo back N undo-boundaries beyond what was already undone recently.
927 Call `undo-start' to get ready to undo recent changes,
928 then call `undo-more' one or more times to undo them."
929   (or pending-undo-list
930       (error "No further undo information"))
931   (setq pending-undo-list (primitive-undo count pending-undo-list)
932         last-undo-buffer (current-buffer)))     ; XEmacs
933
934 ;; XEmacs
935 (defun call-with-transparent-undo (fn &rest args)
936   "Apply FN to ARGS, and then undo all changes made by FN to the current
937 buffer.  The undo records are processed even if FN returns non-locally.
938 There is no trace of the changes made by FN in the buffer's undo history.
939
940 You can use this in a write-file-hooks function with continue-save-buffer
941 to make the contents of a disk file differ from its in-memory buffer."
942   (let ((buffer-undo-list nil)
943         ;; Kludge to prevent undo list truncation:
944         (undo-high-threshold -1)
945         (undo-threshold -1)
946         (obuffer (current-buffer)))
947     (unwind-protect
948         (apply fn args)
949       ;; Go to the buffer we will restore and make it writable:
950       (set-buffer obuffer)
951       (save-excursion
952         (let ((buffer-read-only nil))
953           (save-restriction
954             (widen)
955             ;; Perform all undos, with further undo logging disabled:
956             (let ((tail buffer-undo-list))
957               (setq buffer-undo-list t)
958               (while tail
959                 (setq tail (primitive-undo (length tail) tail))))))))))
960
961 ;; XEmacs: The following are in other files
962 ;; shell-command-history
963 ;; shell-command-switch
964 ;; shell-command
965 ;; shell-command-sentinel
966
967 \f
968 (defconst universal-argument-map
969   (let ((map (make-sparse-keymap)))
970     (set-keymap-default-binding map 'universal-argument-other-key)
971     ;FSFmacs (define-key map [switch-frame] nil)
972     (define-key map [(t)] 'universal-argument-other-key)
973     (define-key map [(meta t)] 'universal-argument-other-key)
974     (define-key map [(control u)] 'universal-argument-more)
975     (define-key map [?-] 'universal-argument-minus)
976     (define-key map [?0] 'digit-argument)
977     (define-key map [?1] 'digit-argument)
978     (define-key map [?2] 'digit-argument)
979     (define-key map [?3] 'digit-argument)
980     (define-key map [?4] 'digit-argument)
981     (define-key map [?5] 'digit-argument)
982     (define-key map [?6] 'digit-argument)
983     (define-key map [?7] 'digit-argument)
984     (define-key map [?8] 'digit-argument)
985     (define-key map [?9] 'digit-argument)
986     map)
987   "Keymap used while processing \\[universal-argument].")
988
989 (defvar universal-argument-num-events nil
990   "Number of argument-specifying events read by `universal-argument'.
991 `universal-argument-other-key' uses this to discard those events
992 from (this-command-keys), and reread only the final command.")
993
994 (defun universal-argument ()
995   "Begin a numeric argument for the following command.
996 Digits or minus sign following \\[universal-argument] make up the numeric argument.
997 \\[universal-argument] following the digits or minus sign ends the argument.
998 \\[universal-argument] without digits or minus sign provides 4 as argument.
999 Repeating \\[universal-argument] without digits or minus sign
1000  multiplies the argument by 4 each time."
1001   (interactive)
1002   (setq prefix-arg (list 4))
1003   (setq zmacs-region-stays t)   ; XEmacs
1004   (setq universal-argument-num-events (length (this-command-keys)))
1005   (setq overriding-terminal-local-map universal-argument-map))
1006
1007 ;; A subsequent C-u means to multiply the factor by 4 if we've typed
1008 ;; nothing but C-u's; otherwise it means to terminate the prefix arg.
1009 (defun universal-argument-more (arg)
1010   (interactive "_P")                    ; XEmacs
1011   (if (consp arg)
1012       (setq prefix-arg (list (* 4 (car arg))))
1013     (setq prefix-arg arg)
1014     (setq overriding-terminal-local-map nil))
1015   (setq universal-argument-num-events (length (this-command-keys))))
1016
1017 (defun negative-argument (arg)
1018   "Begin a negative numeric argument for the next command.
1019 \\[universal-argument] following digits or minus sign ends the argument."
1020   (interactive "_P")                    ; XEmacs
1021   (cond ((integerp arg)
1022           (setq prefix-arg (- arg)))
1023          ((eq arg '-)
1024           (setq prefix-arg nil))
1025          (t
1026           (setq prefix-arg '-)))
1027   (setq universal-argument-num-events (length (this-command-keys)))
1028   (setq overriding-terminal-local-map universal-argument-map))
1029
1030 ;; XEmacs:  This function not synched with FSF
1031 (defun digit-argument (arg)
1032   "Part of the numeric argument for the next command.
1033 \\[universal-argument] following digits or minus sign ends the argument."
1034   (interactive "_P")                    ; XEmacs
1035   (let* ((event last-command-event)
1036          (key (and (key-press-event-p event)
1037                    (event-key event)))
1038          (digit (and key (characterp key) (>= key ?0) (<= key ?9)
1039                      (- key ?0))))
1040     (if (null digit)
1041         (universal-argument-other-key arg)
1042       (cond ((integerp arg)
1043              (setq prefix-arg (+ (* arg 10)
1044                                  (if (< arg 0) (- digit) digit))))
1045             ((eq arg '-)
1046              ;; Treat -0 as just -, so that -01 will work.
1047              (setq prefix-arg (if (zerop digit) '- (- digit))))
1048             (t
1049              (setq prefix-arg digit)))
1050       (setq universal-argument-num-events (length (this-command-keys)))
1051       (setq overriding-terminal-local-map universal-argument-map))))
1052
1053 ;; For backward compatibility, minus with no modifiers is an ordinary
1054 ;; command if digits have already been entered.
1055 (defun universal-argument-minus (arg)
1056   (interactive "_P") ; XEmacs
1057   (if (integerp arg)
1058       (universal-argument-other-key arg)
1059     (negative-argument arg)))
1060
1061 ;; Anything else terminates the argument and is left in the queue to be
1062 ;; executed as a command.
1063 (defun universal-argument-other-key (arg)
1064   (interactive "_P")                    ; XEmacs
1065   (setq prefix-arg arg)
1066   (let* ((key (this-command-keys))
1067          ;; FSF calls silly function `listify-key-sequence' here.
1068           (keylist (append key nil)))
1069     (setq unread-command-events
1070            (append (nthcdr universal-argument-num-events keylist)
1071                    unread-command-events)))
1072   (reset-this-command-lengths)
1073   (setq overriding-terminal-local-map nil))
1074
1075 \f
1076 ;; XEmacs -- keep zmacs-region active.
1077 (defun forward-to-indentation (arg)
1078   "Move forward ARG lines and position at first nonblank character."
1079   (interactive "_p")
1080   (forward-line arg)
1081   (skip-chars-forward " \t"))
1082
1083 (defun backward-to-indentation (arg)
1084   "Move backward ARG lines and position at first nonblank character."
1085   (interactive "_p")
1086   (forward-line (- arg))
1087   (skip-chars-forward " \t"))
1088
1089 (defcustom kill-whole-line nil
1090   "*If non-nil, `kill-line' with no arg at beg of line kills the whole line."
1091   :type 'boolean
1092   :group 'killing)
1093
1094 (defun kill-line (&optional arg)
1095   "Kill the rest of the current line; if no nonblanks there, kill thru newline.
1096 With prefix argument, kill that many lines from point.
1097 Negative arguments kill lines backward.
1098
1099 When calling from a program, nil means \"no arg\",
1100 a number counts as a prefix arg.
1101
1102 If `kill-whole-line' is non-nil, then kill the whole line
1103 when given no argument at the beginning of a line."
1104   (interactive "*P")
1105   (kill-region (point)
1106                ;; Don't shift point before doing the delete; that way,
1107                ;; undo will record the right position of point.
1108 ;; FSF
1109 ;              ;; It is better to move point to the other end of the kill
1110 ;              ;; before killing.  That way, in a read-only buffer, point
1111 ;              ;; moves across the text that is copied to the kill ring.
1112 ;              ;; The choice has no effect on undo now that undo records
1113 ;              ;; the value of point from before the command was run.
1114 ;              (progn
1115                (save-excursion
1116                  (if arg
1117                      (forward-line (prefix-numeric-value arg))
1118                    (if (eobp)
1119                        (signal 'end-of-buffer nil))
1120                    (if (or (looking-at "[ \t]*$") (and kill-whole-line (bolp)))
1121                        (forward-line 1)
1122                      (end-of-line)))
1123                  (point))))
1124
1125 ;; XEmacs
1126 (defun backward-kill-line nil
1127   "Kill back to the beginning of the line."
1128   (interactive)
1129   (let ((point (point)))
1130     (beginning-of-line nil)
1131     (kill-region (point) point)))
1132
1133 \f
1134 ;;;; Window system cut and paste hooks.
1135 ;;;
1136 ;;; I think that kill-hooks is a better name and more general mechanism
1137 ;;; than interprogram-cut-function (from FSFmacs).  I don't like the behavior
1138 ;;; of interprogram-paste-function: ^Y should always come from the kill ring,
1139 ;;; not the X selection.  But if that were provided, it should be called (and
1140 ;;; behave as) yank-hooks instead.  -- jwz
1141
1142 ;; [... code snipped ...]
1143
1144 (defcustom kill-hooks nil
1145   "*Functions run when something is added to the XEmacs kill ring.
1146 These functions are called with one argument, the string most recently
1147 cut or copied.  You can use this to, for example, make the most recent
1148 kill become the X Clipboard selection."
1149   :type 'hook
1150   :group 'killing)
1151
1152 ;;; `kill-hooks' seems not sufficient because
1153 ;;; `interprogram-cut-function' requires more variable about to rotate
1154 ;;; the cut buffers.  I'm afraid to change interface of `kill-hooks',
1155 ;;; so I add it. (1997-11-03 by MORIOKA Tomohiko)
1156
1157 (defvar interprogram-cut-function nil
1158   "Function to call to make a killed region available to other programs.
1159
1160 Most window systems provide some sort of facility for cutting and
1161 pasting text between the windows of different programs.
1162 This variable holds a function that Emacs calls whenever text
1163 is put in the kill ring, to make the new kill available to other
1164 programs.
1165
1166 The function takes one or two arguments.
1167 The first argument, TEXT, is a string containing
1168 the text which should be made available.
1169 The second, PUSH, if non-nil means this is a \"new\" kill;
1170 nil means appending to an \"old\" kill.")
1171
1172 (defvar interprogram-paste-function nil
1173   "Function to call to get text cut from other programs.
1174
1175 Most window systems provide some sort of facility for cutting and
1176 pasting text between the windows of different programs.
1177 This variable holds a function that Emacs calls to obtain
1178 text that other programs have provided for pasting.
1179
1180 The function should be called with no arguments.  If the function
1181 returns nil, then no other program has provided such text, and the top
1182 of the Emacs kill ring should be used.  If the function returns a
1183 string, that string should be put in the kill ring as the latest kill.
1184
1185 Note that the function should return a string only if a program other
1186 than Emacs has provided a string for pasting; if Emacs provided the
1187 most recent string, the function should return nil.  If it is
1188 difficult to tell whether Emacs or some other program provided the
1189 current string, it is probably good enough to return nil if the string
1190 is equal (according to `string=') to the last text Emacs provided.")
1191
1192 \f
1193 ;;;; The kill ring data structure.
1194
1195 (defvar kill-ring nil
1196   "List of killed text sequences.
1197 Since the kill ring is supposed to interact nicely with cut-and-paste
1198 facilities offered by window systems, use of this variable should
1199 interact nicely with `interprogram-cut-function' and
1200 `interprogram-paste-function'.  The functions `kill-new',
1201 `kill-append', and `current-kill' are supposed to implement this
1202 interaction; you may want to use them instead of manipulating the kill
1203 ring directly.")
1204
1205 (defcustom kill-ring-max 30
1206   "*Maximum length of kill ring before oldest elements are thrown away."
1207   :type 'integer
1208   :group 'killing)
1209
1210 (defvar kill-ring-yank-pointer nil
1211   "The tail of the kill ring whose car is the last thing yanked.")
1212
1213 (defun kill-new (string &optional replace)
1214   "Make STRING the latest kill in the kill ring.
1215 Set the kill-ring-yank pointer to point to it.
1216 Run `kill-hooks'.
1217 Optional second argument REPLACE non-nil means that STRING will replace
1218 the front of the kill ring, rather than being added to the list."
1219 ;  (and (fboundp 'menu-bar-update-yank-menu)
1220 ;       (menu-bar-update-yank-menu string (and replace (car kill-ring))))
1221   (if replace
1222       (setcar kill-ring string)
1223     (setq kill-ring (cons string kill-ring))
1224     (if (> (length kill-ring) kill-ring-max)
1225         (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
1226   (setq kill-ring-yank-pointer kill-ring)
1227   (if interprogram-cut-function
1228       (funcall interprogram-cut-function string (not replace)))
1229   (run-hook-with-args 'kill-hooks string))
1230
1231 (defun kill-append (string before-p)
1232   "Append STRING to the end of the latest kill in the kill ring.
1233 If BEFORE-P is non-nil, prepend STRING to the kill.
1234 Run `kill-hooks'."
1235   (kill-new (if before-p
1236                 (concat string (car kill-ring))
1237               (concat (car kill-ring) string)) t))
1238
1239 (defun current-kill (n &optional do-not-move)
1240   "Rotate the yanking point by N places, and then return that kill.
1241 If N is zero, `interprogram-paste-function' is set, and calling it
1242 returns a string, then that string is added to the front of the
1243 kill ring and returned as the latest kill.
1244 If optional arg DO-NOT-MOVE is non-nil, then don't actually move the
1245 yanking point\; just return the Nth kill forward."
1246   (let ((interprogram-paste (and (= n 0)
1247                                  interprogram-paste-function
1248                                  (funcall interprogram-paste-function))))
1249     (if interprogram-paste
1250         (progn
1251           ;; Disable the interprogram cut function when we add the new
1252           ;; text to the kill ring, so Emacs doesn't try to own the
1253           ;; selection, with identical text.
1254           (let ((interprogram-cut-function nil))
1255             (kill-new interprogram-paste))
1256           interprogram-paste)
1257       (or kill-ring (error "Kill ring is empty"))
1258       (let* ((tem (nthcdr (mod (- n (length kill-ring-yank-pointer))
1259                                (length kill-ring))
1260                           kill-ring)))
1261         (or do-not-move
1262             (setq kill-ring-yank-pointer tem))
1263         (car tem)))))
1264
1265
1266 \f
1267 ;;;; Commands for manipulating the kill ring.
1268
1269 ;; In FSF killing read-only text just pastes it into kill-ring.  Which
1270 ;; is a very bad idea -- see Jamie's comment below.
1271
1272 ;(defvar kill-read-only-ok nil
1273 ;  "*Non-nil means don't signal an error for killing read-only text.")
1274
1275 (defun kill-region (beg end &optional verbose) ; verbose is XEmacs addition
1276   "Kill between point and mark.
1277 The text is deleted but saved in the kill ring.
1278 The command \\[yank] can retrieve it from there.
1279 \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].)
1280
1281 This is the primitive for programs to kill text (as opposed to deleting it).
1282 Supply two arguments, character numbers indicating the stretch of text
1283  to be killed.
1284 Any command that calls this function is a \"kill command\".
1285 If the previous command was also a kill command,
1286 the text killed this time appends to the text killed last time
1287 to make one entry in the kill ring."
1288   (interactive "*r\np")
1289 ;  (interactive
1290 ;   (let ((region-hack (and zmacs-regions (eq last-command 'yank))))
1291 ;     ;; This lets "^Y^W" work.  I think this is dumb, but zwei did it.
1292 ;     (if region-hack (zmacs-activate-region))
1293 ;     (prog1
1294 ;        (list (point) (mark) current-prefix-arg)
1295 ;       (if region-hack (zmacs-deactivate-region)))))
1296   ;; beg and end can be markers but the rest of this function is
1297   ;; written as if they are only integers
1298   (if (markerp beg) (setq beg (marker-position beg)))
1299   (if (markerp end) (setq end (marker-position end)))
1300   (or (and beg end) (if zmacs-regions ;; rewritten for I18N3 snarfing
1301                         (error "The region is not active now")
1302                       (error "The mark is not set now")))
1303   (if verbose (if buffer-read-only
1304                   (lmessage 'command "Copying %d characters"
1305                             (- (max beg end) (min beg end)))
1306                 (lmessage 'command "Killing %d characters"
1307                           (- (max beg end) (min beg end)))))
1308   (cond
1309
1310    ;; I don't like this large change in behavior -- jwz
1311    ;; Read-Only text means it shouldn't be deleted, so I'm restoring
1312    ;; this code, but only for text-properties and not full extents. -sb
1313    ;; If the buffer is read-only, we should beep, in case the person
1314    ;; just isn't aware of this.  However, there's no harm in putting
1315    ;; the region's text in the kill ring, anyway.
1316    ((or (and buffer-read-only (not inhibit-read-only))
1317         (text-property-not-all (min beg end) (max beg end) 'read-only nil))
1318    ;; This is redundant.
1319    ;; (if verbose (message "Copying %d characters"
1320    ;;                    (- (max beg end) (min beg end))))
1321     (copy-region-as-kill beg end)
1322    ;; ;; This should always barf, and give us the correct error.
1323    ;; (if kill-read-only-ok
1324    ;;     (message "Read only text copied to kill ring")
1325     (setq this-command 'kill-region)
1326     (barf-if-buffer-read-only)
1327     (signal 'buffer-read-only (list (current-buffer))))
1328
1329    ;; In certain cases, we can arrange for the undo list and the kill
1330    ;; ring to share the same string object.  This code does that.
1331    ((not (or (eq buffer-undo-list t)
1332              (eq last-command 'kill-region)
1333              ;; Use = since positions may be numbers or markers.
1334              (= beg end)))
1335     ;; Don't let the undo list be truncated before we can even access it.
1336     ;; FSF calls this `undo-strong-limit'
1337     (let ((undo-high-threshold (+ (- end beg) 100))
1338           ;(old-list buffer-undo-list)
1339           tail)
1340       (delete-region beg end)
1341       ;; Search back in buffer-undo-list for this string,
1342       ;; in case a change hook made property changes.
1343       (setq tail buffer-undo-list)
1344       (while (and tail
1345                   (not (stringp (car-safe (car-safe tail))))) ; XEmacs
1346         (pop tail))
1347       ;; Take the same string recorded for undo
1348       ;; and put it in the kill-ring.
1349       (and tail
1350            (kill-new (car (car tail))))))
1351
1352    (t
1353     ;; if undo is not kept, grab the string then delete it (which won't
1354     ;; add another string to the undo list).
1355     (copy-region-as-kill beg end)
1356     (delete-region beg end)))
1357   (setq this-command 'kill-region))
1358
1359 ;; copy-region-as-kill no longer sets this-command, because it's confusing
1360 ;; to get two copies of the text when the user accidentally types M-w and
1361 ;; then corrects it with the intended C-w.
1362 (defun copy-region-as-kill (beg end)
1363   "Save the region as if killed, but don't kill it.
1364 Run `kill-hooks'."
1365   (interactive "r")
1366   (if (eq last-command 'kill-region)
1367       (kill-append (buffer-substring beg end) (< end beg))
1368     (kill-new (buffer-substring beg end)))
1369   nil)
1370
1371 (defun kill-ring-save (beg end)
1372   "Save the region as if killed, but don't kill it.
1373 This command is similar to `copy-region-as-kill', except that it gives
1374 visual feedback indicating the extent of the region being copied."
1375   (interactive "r")
1376   (copy-region-as-kill beg end)
1377   ;; copy before delay, for xclipboard's benefit
1378   (if (interactive-p)
1379       (let ((other-end (if (= (point) beg) end beg))
1380             (opoint (point))
1381             ;; Inhibit quitting so we can make a quit here
1382             ;; look like a C-g typed as a command.
1383             (inhibit-quit t))
1384         (if (pos-visible-in-window-p other-end (selected-window))
1385             (progn
1386               ;; FSF (I'm not sure what this does -sb)
1387 ;             ;; Swap point and mark.
1388 ;             (set-marker (mark-marker) (point) (current-buffer))
1389               (goto-char other-end)
1390               (sit-for 1)
1391 ;             ;; Swap back.
1392 ;             (set-marker (mark-marker) other-end (current-buffer))
1393               (goto-char opoint)
1394               ;; If user quit, deactivate the mark
1395               ;; as C-g would as a command.
1396               (and quit-flag (mark)
1397                    (zmacs-deactivate-region)))
1398           ;; too noisy. -- jwz
1399 ;         (let* ((killed-text (current-kill 0))
1400 ;                (message-len (min (length killed-text) 40)))
1401 ;           (if (= (point) beg)
1402 ;               ;; Don't say "killed"; that is misleading.
1403 ;               (message "Saved text until \"%s\""
1404 ;                       (substring killed-text (- message-len)))
1405 ;             (message "Saved text from \"%s\""
1406 ;                     (substring killed-text 0 message-len))))
1407           ))))
1408
1409 (defun append-next-kill ()
1410   "Cause following command, if it kills, to append to previous kill."
1411   ;; XEmacs
1412   (interactive "_")
1413   (if (interactive-p)
1414       (progn
1415         (setq this-command 'kill-region)
1416         (display-message 'command
1417           "If the next command is a kill, it will append"))
1418     (setq last-command 'kill-region)))
1419
1420 (defun yank-pop (arg)
1421   "Replace just-yanked stretch of killed text with a different stretch.
1422 This command is allowed only immediately after a `yank' or a `yank-pop'.
1423 At such a time, the region contains a stretch of reinserted
1424 previously-killed text.  `yank-pop' deletes that text and inserts in its
1425 place a different stretch of killed text.
1426
1427 With no argument, the previous kill is inserted.
1428 With argument N, insert the Nth previous kill.
1429 If N is negative, this is a more recent kill.
1430
1431 The sequence of kills wraps around, so that after the oldest one
1432 comes the newest one."
1433   (interactive "*p")
1434   (if (not (eq last-command 'yank))
1435       (error "Previous command was not a yank"))
1436   (setq this-command 'yank)
1437   (let ((inhibit-read-only t)
1438         (before (< (point) (mark t))))
1439     (delete-region (point) (mark t))
1440     ;;(set-marker (mark-marker) (point) (current-buffer))
1441     (set-mark (point))
1442     (insert (current-kill arg))
1443     (if before
1444         ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1445         ;; It is cleaner to avoid activation, even though the command
1446         ;; loop would deactivate the mark because we inserted text.
1447         (goto-char (prog1 (mark t)
1448                      (set-marker (mark-marker t) (point) (current-buffer))))))
1449   nil)
1450
1451
1452 (defun yank (&optional arg)
1453   "Reinsert the last stretch of killed text.
1454 More precisely, reinsert the stretch of killed text most recently
1455 killed OR yanked.  Put point at end, and set mark at beginning.
1456 With just C-u as argument, same but put point at beginning (and mark at end).
1457 With argument N, reinsert the Nth most recently killed stretch of killed
1458 text.
1459 See also the command \\[yank-pop]."
1460   (interactive "*P")
1461   ;; If we don't get all the way through, make last-command indicate that
1462   ;; for the following command.
1463   (setq this-command t)
1464   (push-mark (point))
1465   (insert (current-kill (cond
1466                          ((listp arg) 0)
1467                          ((eq arg '-) -1)
1468                          (t (1- arg)))))
1469   (if (consp arg)
1470       ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1471       ;; It is cleaner to avoid activation, even though the command
1472       ;; loop would deactivate the mark because we inserted text.
1473       ;; (But it's an unnecessary kludge in XEmacs.)
1474       ;(goto-char (prog1 (mark t)
1475                    ;(set-marker (mark-marker) (point) (current-buffer)))))
1476       (exchange-point-and-mark t))
1477   ;; If we do get all the way thru, make this-command indicate that.
1478   (setq this-command 'yank)
1479   nil)
1480
1481 (defun rotate-yank-pointer (arg)
1482   "Rotate the yanking point in the kill ring.
1483 With argument, rotate that many kills forward (or backward, if negative)."
1484   (interactive "p")
1485   (current-kill arg))
1486
1487 \f
1488 (defun insert-buffer (buffer)
1489   "Insert after point the contents of BUFFER.
1490 Puts mark after the inserted text.
1491 BUFFER may be a buffer or a buffer name."
1492   (interactive
1493    (list
1494     (progn
1495       (barf-if-buffer-read-only)
1496       (read-buffer "Insert buffer: "
1497                    ;; XEmacs: we have different args
1498                    (other-buffer (current-buffer) nil t)
1499                    t))))
1500   (or (bufferp buffer)
1501       (setq buffer (get-buffer buffer)))
1502   (let (start end newmark)
1503     (save-excursion
1504       (save-excursion
1505         (set-buffer buffer)
1506         (setq start (point-min) end (point-max)))
1507       (insert-buffer-substring buffer start end)
1508       (setq newmark (point)))
1509     (push-mark newmark))
1510   nil)
1511
1512 (defun append-to-buffer (buffer start end)
1513   "Append to specified buffer the text of the region.
1514 It is inserted into that buffer before its point.
1515
1516 When calling from a program, give three arguments:
1517 BUFFER (or buffer name), START and END.
1518 START and END specify the portion of the current buffer to be copied."
1519   (interactive
1520    ;; XEmacs: we have different args to other-buffer
1521    (list (read-buffer "Append to buffer: " (other-buffer (current-buffer)
1522                                                          nil t))
1523          (region-beginning) (region-end)))
1524   (let ((oldbuf (current-buffer)))
1525     (save-excursion
1526       (set-buffer (get-buffer-create buffer))
1527       (insert-buffer-substring oldbuf start end))))
1528
1529 (defun prepend-to-buffer (buffer start end)
1530   "Prepend to specified buffer the text of the region.
1531 It is inserted into that buffer after its point.
1532
1533 When calling from a program, give three arguments:
1534 BUFFER (or buffer name), START and END.
1535 START and END specify the portion of the current buffer to be copied."
1536   (interactive "BPrepend to buffer: \nr")
1537   (let ((oldbuf (current-buffer)))
1538     (save-excursion
1539       (set-buffer (get-buffer-create buffer))
1540       (save-excursion
1541         (insert-buffer-substring oldbuf start end)))))
1542
1543 (defun copy-to-buffer (buffer start end)
1544   "Copy to specified buffer the text of the region.
1545 It is inserted into that buffer, replacing existing text there.
1546
1547 When calling from a program, give three arguments:
1548 BUFFER (or buffer name), START and END.
1549 START and END specify the portion of the current buffer to be copied."
1550   (interactive "BCopy to buffer: \nr")
1551   (let ((oldbuf (current-buffer)))
1552     (save-excursion
1553       (set-buffer (get-buffer-create buffer))
1554       (erase-buffer)
1555       (save-excursion
1556         (insert-buffer-substring oldbuf start end)))))
1557 \f
1558 ;FSFmacs
1559 ;(put 'mark-inactive 'error-conditions '(mark-inactive error))
1560 ;(put 'mark-inactive 'error-message "The mark is not active now")
1561
1562 (defun mark (&optional force buffer)
1563   "Return this buffer's mark value as integer, or nil if no mark.
1564
1565 If `zmacs-regions' is true, then this returns nil unless the region is
1566 currently in the active (highlighted) state.  With an argument of t, this
1567 returns the mark (if there is one) regardless of the active-region state.
1568 You should *generally* not use the mark unless the region is active, if
1569 the user has expressed a preference for the active-region model.
1570
1571 If you are using this in an editing command, you are most likely making
1572 a mistake; see the documentation of `set-mark'."
1573   (setq buffer (decode-buffer buffer))
1574 ;FSFmacs version:
1575 ;  (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive)
1576 ;      (marker-position (mark-marker))
1577 ;    (signal 'mark-inactive nil)))
1578   (let ((m (mark-marker force buffer)))
1579     (and m (marker-position m))))
1580
1581 ;;;#### FSFmacs
1582 ;;; Many places set mark-active directly, and several of them failed to also
1583 ;;; run deactivate-mark-hook.  This shorthand should simplify.
1584 ;(defsubst deactivate-mark ()
1585 ;  "Deactivate the mark by setting `mark-active' to nil.
1586 ;\(That makes a difference only in Transient Mark mode.)
1587 ;Also runs the hook `deactivate-mark-hook'."
1588 ;  (if transient-mark-mode
1589 ;      (progn
1590 ;       (setq mark-active nil)
1591 ;       (run-hooks 'deactivate-mark-hook))))
1592
1593 (defun set-mark (pos &optional buffer)
1594   "Set this buffer's mark to POS.  Don't use this function!
1595 That is to say, don't use this function unless you want
1596 the user to see that the mark has moved, and you want the previous
1597 mark position to be lost.
1598
1599 Normally, when a new mark is set, the old one should go on the stack.
1600 This is why most applications should use push-mark, not set-mark.
1601
1602 Novice Emacs Lisp programmers often try to use the mark for the wrong
1603 purposes.  The mark saves a location for the user's convenience.
1604 Most editing commands should not alter the mark.
1605 To remember a location for internal use in the Lisp program,
1606 store it in a Lisp variable.  Example:
1607
1608    (let ((beg (point))) (forward-line 1) (delete-region beg (point)))."
1609
1610   (setq buffer (decode-buffer buffer))
1611   (set-marker (mark-marker t buffer) pos buffer))
1612 ;; FSF
1613 ;  (if pos
1614 ;     (progn
1615 ;       (setq mark-active t)
1616 ;       (run-hooks 'activate-mark-hook)
1617 ;       (set-marker (mark-marker) pos (current-buffer)))
1618 ;    ;; Normally we never clear mark-active except in Transient Mark mode.
1619 ;    ;; But when we actually clear out the mark value too,
1620 ;    ;; we must clear mark-active in any mode.
1621 ;    (setq mark-active nil)
1622 ;    (run-hooks 'deactivate-mark-hook)
1623 ;    (set-marker (mark-marker) nil)))
1624
1625 (defvar mark-ring nil
1626   "The list of former marks of the current buffer, most recent first.")
1627 (make-variable-buffer-local 'mark-ring)
1628 (put 'mark-ring 'permanent-local t)
1629
1630 (defcustom mark-ring-max 16
1631   "*Maximum size of mark ring.  Start discarding off end if gets this big."
1632   :type 'integer
1633   :group 'killing)
1634
1635 (defvar global-mark-ring nil
1636   "The list of saved global marks, most recent first.")
1637
1638 (defcustom global-mark-ring-max 16
1639   "*Maximum size of global mark ring.  \
1640 Start discarding off end if gets this big."
1641   :type 'integer
1642   :group 'killing)
1643
1644 (defun set-mark-command (arg)
1645   "Set mark at where point is, or jump to mark.
1646 With no prefix argument, set mark, push old mark position on local mark
1647 ring, and push mark on global mark ring.
1648 With argument, jump to mark, and pop a new position for mark off the ring
1649 \(does not affect global mark ring\).
1650
1651 Novice Emacs Lisp programmers often try to use the mark for the wrong
1652 purposes.  See the documentation of `set-mark' for more information."
1653   (interactive "P")
1654   (if (null arg)
1655       (push-mark nil nil t)
1656     (if (null (mark t))
1657         (error "No mark set in this buffer")
1658       (goto-char (mark t))
1659       (pop-mark))))
1660
1661 ;; XEmacs: Extra parameter
1662 (defun push-mark (&optional location nomsg activate-region buffer)
1663   "Set mark at LOCATION (point, by default) and push old mark on mark ring.
1664 If the last global mark pushed was not in the current buffer,
1665 also push LOCATION on the global mark ring.
1666 Display `Mark set' unless the optional second arg NOMSG is non-nil.
1667 Activate mark if optional third arg ACTIVATE-REGION non-nil.
1668
1669 Novice Emacs Lisp programmers often try to use the mark for the wrong
1670 purposes.  See the documentation of `set-mark' for more information."
1671   (setq buffer (decode-buffer buffer)) ; XEmacs
1672   (if (null (mark t buffer)) ; XEmacs
1673       nil
1674     ;; The save-excursion / set-buffer is necessary because mark-ring
1675     ;; is a buffer local variable
1676     (save-excursion
1677       (set-buffer buffer)
1678       (setq mark-ring (cons (copy-marker (mark-marker t buffer)) mark-ring))
1679       (if (> (length mark-ring) mark-ring-max)
1680           (progn
1681             (move-marker (car (nthcdr mark-ring-max mark-ring)) nil buffer)
1682             (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))))
1683   (set-mark (or location (point buffer)) buffer)
1684 ; (set-marker (mark-marker) (or location (point)) (current-buffer)) ; FSF
1685   ;; Now push the mark on the global mark ring.
1686   (if (or (null global-mark-ring)
1687           (not (eq (marker-buffer (car global-mark-ring)) buffer)))
1688       ;; The last global mark pushed wasn't in this same buffer.
1689       (progn
1690         (setq global-mark-ring (cons (copy-marker (mark-marker t buffer))
1691                                      global-mark-ring))
1692         (if (> (length global-mark-ring) global-mark-ring-max)
1693             (progn
1694               (move-marker (car (nthcdr global-mark-ring-max global-mark-ring))
1695                            nil buffer)
1696               (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))))
1697   (or nomsg executing-kbd-macro (> (minibuffer-depth) 0)
1698       (display-message 'command "Mark set"))
1699   (if activate-region
1700       (progn
1701         (setq zmacs-region-stays t)
1702         (zmacs-activate-region)))
1703 ; (if (or activate (not transient-mark-mode)) ; FSF
1704 ;     (set-mark (mark t))) ; FSF
1705   nil)
1706
1707 (defun pop-mark ()
1708   "Pop off mark ring into the buffer's actual mark.
1709 Does not set point.  Does nothing if mark ring is empty."
1710   (if mark-ring
1711       (progn
1712         (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker t)))))
1713         (set-mark (car mark-ring))
1714         (move-marker (car mark-ring) nil)
1715         (if (null (mark t)) (ding))
1716         (setq mark-ring (cdr mark-ring)))))
1717
1718 (define-function 'exchange-dot-and-mark 'exchange-point-and-mark)
1719 (defun exchange-point-and-mark (&optional dont-activate-region)
1720   "Put the mark where point is now, and point where the mark is now.
1721 The mark is activated unless DONT-ACTIVATE-REGION is non-nil."
1722   (interactive nil)
1723   (let ((omark (mark t)))
1724     (if (null omark)
1725         (error "No mark set in this buffer"))
1726     (set-mark (point))
1727     (goto-char omark)
1728     (or dont-activate-region (zmacs-activate-region)) ; XEmacs
1729     nil))
1730
1731 ;; XEmacs
1732 (defun mark-something (mark-fn movement-fn arg)
1733   "internal function used by mark-sexp, mark-word, etc."
1734   (let (newmark (pushp t))
1735     (save-excursion
1736       (if (and (eq last-command mark-fn) (mark))
1737           ;; Extend the previous state in the same direction:
1738           (progn
1739             (if (< (mark) (point)) (setq arg (- arg)))
1740             (goto-char (mark))
1741             (setq pushp nil)))
1742       (funcall movement-fn arg)
1743       (setq newmark (point)))
1744     (if pushp
1745         (push-mark newmark nil t)
1746       ;; Do not mess with the mark stack, but merely adjust the previous state:
1747       (set-mark newmark)
1748       (activate-region))))
1749
1750 ;(defun transient-mark-mode (arg)
1751 ;  "Toggle Transient Mark mode.
1752 ;With arg, turn Transient Mark mode on if arg is positive, off otherwise.
1753 ;
1754 ;In Transient Mark mode, when the mark is active, the region is highlighted.
1755 ;Changing the buffer \"deactivates\" the mark.
1756 ;So do certain other operations that set the mark
1757 ;but whose main purpose is something else--for example,
1758 ;incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]."
1759 ;  (interactive "P")
1760 ;  (setq transient-mark-mode
1761 ;       (if (null arg)
1762 ;           (not transient-mark-mode)
1763 ;         (> (prefix-numeric-value arg) 0))))
1764
1765 (defun pop-global-mark ()
1766   "Pop off global mark ring and jump to the top location."
1767   (interactive)
1768   ;; Pop entries which refer to non-existent buffers.
1769   (while (and global-mark-ring (not (marker-buffer (car global-mark-ring))))
1770     (setq global-mark-ring (cdr global-mark-ring)))
1771   (or global-mark-ring
1772       (error "No global mark set"))
1773   (let* ((marker (car global-mark-ring))
1774          (buffer (marker-buffer marker))
1775          (position (marker-position marker)))
1776     (setq global-mark-ring (nconc (cdr global-mark-ring)
1777                                   (list (car global-mark-ring))))
1778     (set-buffer buffer)
1779     (or (and (>= position (point-min))
1780              (<= position (point-max)))
1781         (widen))
1782     (goto-char position)
1783     (switch-to-buffer buffer)))
1784
1785 \f
1786 (defcustom signal-error-on-buffer-boundary t
1787   "*Non-nil value causes XEmacs to beep or signal an error when certain interactive commands would move point past (point-min) or (point-max).
1788 The commands that honor this variable are
1789
1790 forward-char-command
1791 backward-char-command
1792 next-line
1793 previous-line
1794 scroll-up-command
1795 scroll-down-command"
1796   :type 'boolean
1797   :group 'editing-basics)
1798
1799 ;;; After 8 years of waiting ... -sb
1800 (defcustom next-line-add-newlines nil  ; XEmacs
1801   "*If non-nil, `next-line' inserts newline when the point is at end of buffer.
1802 This behavior used to be the default, and is still default in FSF Emacs.
1803 We think it is an unnecessary and unwanted side-effect."
1804   :type 'boolean
1805   :group 'editing-basics)
1806
1807 (defun forward-char-command (&optional arg buffer)
1808   "Move point right ARG characters (left if ARG negative) in BUFFER.
1809 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
1810 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
1811 Error signaling is suppressed if `signal-error-on-buffer-boundary'
1812 is nil.  If BUFFER is nil, the current buffer is assumed."
1813   (interactive "_p")
1814   (if signal-error-on-buffer-boundary
1815       (forward-char arg buffer)
1816     (condition-case nil
1817         (forward-char arg buffer)
1818       (beginning-of-buffer nil)
1819       (end-of-buffer nil))))
1820
1821 (defun backward-char-command (&optional arg buffer)
1822   "Move point left ARG characters (right if ARG negative) in BUFFER.
1823 On attempt to pass end of buffer, stop and signal `end-of-buffer'.
1824 On attempt to pass beginning of buffer, stop and signal `beginning-of-buffer'.
1825 Error signaling is suppressed if `signal-error-on-buffer-boundary'
1826 is nil.  If BUFFER is nil, the current buffer is assumed."
1827   (interactive "_p")
1828   (if signal-error-on-buffer-boundary
1829       (backward-char arg buffer)
1830     (condition-case nil
1831         (backward-char arg buffer)
1832       (beginning-of-buffer nil)
1833       (end-of-buffer nil))))
1834
1835 (defun scroll-up-command (&optional n)
1836   "Scroll text of current window upward ARG lines; or near full screen if no ARG.
1837 A near full screen is `next-screen-context-lines' less than a full screen.
1838 Negative ARG means scroll downward.
1839 When calling from a program, supply a number as argument or nil.
1840 On attempt to scroll past end of buffer, `end-of-buffer' is signaled.
1841 On attempt to scroll past beginning of buffer, `beginning-of-buffer' is
1842 signaled.
1843
1844 If `signal-error-on-buffer-boundary' is nil, attempts to scroll past buffer
1845 boundaries do not cause an error to be signaled."
1846   (interactive "_P")
1847   (if signal-error-on-buffer-boundary
1848       (scroll-up n)
1849     (condition-case nil
1850         (scroll-up n)
1851       (beginning-of-buffer nil)
1852       (end-of-buffer nil))))
1853
1854 (defun scroll-down-command (&optional n)
1855   "Scroll text of current window downward ARG lines; or near full screen if no ARG.
1856 A near full screen is `next-screen-context-lines' less than a full screen.
1857 Negative ARG means scroll upward.
1858 When calling from a program, supply a number as argument or nil.
1859 On attempt to scroll past end of buffer, `end-of-buffer' is signaled.
1860 On attempt to scroll past beginning of buffer, `beginning-of-buffer' is
1861 signaled.
1862
1863 If `signal-error-on-buffer-boundary' is nil, attempts to scroll past buffer
1864 boundaries do not cause an error to be signaled."
1865   (interactive "_P")
1866   (if signal-error-on-buffer-boundary
1867       (scroll-down n)
1868     (condition-case nil
1869         (scroll-down n)
1870       (beginning-of-buffer nil)
1871       (end-of-buffer nil))))
1872
1873 (defun next-line (arg)
1874   "Move cursor vertically down ARG lines.
1875 If there is no character in the target line exactly under the current column,
1876 the cursor is positioned after the character in that line which spans this
1877 column, or at the end of the line if it is not long enough.
1878
1879 If there is no line in the buffer after this one, behavior depends on the
1880 value of `next-line-add-newlines'.  If non-nil, it inserts a newline character
1881 to create a line, and moves the cursor to that line.  Otherwise it moves the
1882 cursor to the end of the buffer.
1883
1884 The command \\[set-goal-column] can be used to create
1885 a semipermanent goal column to which this command always moves.
1886 Then it does not try to move vertically.  This goal column is stored
1887 in `goal-column', which is nil when there is none.
1888
1889 If you are thinking of using this in a Lisp program, consider
1890 using `forward-line' instead.  It is usually easier to use
1891 and more reliable (no dependence on goal column, etc.)."
1892   (interactive "_p") ; XEmacs
1893   (if (and next-line-add-newlines (= arg 1))
1894       (let ((opoint (point)))
1895         (end-of-line)
1896         (if (eobp)
1897             (newline 1)
1898           (goto-char opoint)
1899           (line-move arg)))
1900     (if (interactive-p)
1901         ;; XEmacs:  Not sure what to do about this.  It's inconsistent. -sb
1902         (condition-case nil
1903             (line-move arg)
1904           ((beginning-of-buffer end-of-buffer)
1905            (when signal-error-on-buffer-boundary
1906              (ding nil 'buffer-bound))))
1907       (line-move arg)))
1908   nil)
1909
1910 (defun previous-line (arg)
1911   "Move cursor vertically up ARG lines.
1912 If there is no character in the target line exactly over the current column,
1913 the cursor is positioned after the character in that line which spans this
1914 column, or at the end of the line if it is not long enough.
1915
1916 The command \\[set-goal-column] can be used to create
1917 a semipermanent goal column to which this command always moves.
1918 Then it does not try to move vertically.
1919
1920 If you are thinking of using this in a Lisp program, consider using
1921 `forward-line' with a negative argument instead.  It is usually easier
1922 to use and more reliable (no dependence on goal column, etc.)."
1923   (interactive "_p") ; XEmacs
1924   (if (interactive-p)
1925       (condition-case nil
1926           (line-move (- arg))
1927         ((beginning-of-buffer end-of-buffer)
1928          (when signal-error-on-buffer-boundary ; XEmacs
1929            (ding nil 'buffer-bound))))
1930     (line-move (- arg)))
1931   nil)
1932
1933 (defcustom track-eol nil
1934   "*Non-nil means vertical motion starting at end of line keeps to ends of lines.
1935 This means moving to the end of each line moved onto.
1936 The beginning of a blank line does not count as the end of a line."
1937   :type 'boolean
1938   :group 'editing-basics)
1939
1940 (defcustom goal-column nil
1941   "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil."
1942   :type '(choice integer (const :tag "None" nil))
1943   :group 'editing-basics)
1944 (make-variable-buffer-local 'goal-column)
1945
1946 (defvar temporary-goal-column 0
1947   "Current goal column for vertical motion.
1948 It is the column where point was
1949 at the start of current run of vertical motion commands.
1950 When the `track-eol' feature is doing its job, the value is 9999.")
1951 (make-variable-buffer-local 'temporary-goal-column)
1952
1953 ;XEmacs: not yet ported, so avoid compiler warnings
1954 (eval-when-compile
1955   (defvar inhibit-point-motion-hooks))
1956
1957 (defcustom line-move-ignore-invisible nil
1958   "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines.
1959 Use with care, as it slows down movement significantly.  Outline mode sets this."
1960   :type 'boolean
1961   :group 'editing-basics)
1962
1963 ;; This is the guts of next-line and previous-line.
1964 ;; Arg says how many lines to move.
1965 (defun line-move (arg)
1966   ;; Don't run any point-motion hooks, and disregard intangibility,
1967   ;; for intermediate positions.
1968   (let ((inhibit-point-motion-hooks t)
1969         (opoint (point))
1970         new)
1971     (unwind-protect
1972         (progn
1973           (if (not (or (eq last-command 'next-line)
1974                        (eq last-command 'previous-line)))
1975               (setq temporary-goal-column
1976                     (if (and track-eol (eolp)
1977                              ;; Don't count beg of empty line as end of line
1978                              ;; unless we just did explicit end-of-line.
1979                              (or (not (bolp)) (eq last-command 'end-of-line)))
1980                         9999
1981                       (current-column))))
1982           (if (and (not (integerp selective-display))
1983                    (not line-move-ignore-invisible))
1984               ;; Use just newline characters.
1985               (or (if (> arg 0)
1986                       (progn (if (> arg 1) (forward-line (1- arg)))
1987                              ;; This way of moving forward ARG lines
1988                              ;; verifies that we have a newline after the last one.
1989                              ;; It doesn't get confused by intangible text.
1990                              (end-of-line)
1991                              (zerop (forward-line 1)))
1992                     (and (zerop (forward-line arg))
1993                          (bolp)))
1994                   (signal (if (< arg 0)
1995                               'beginning-of-buffer
1996                             'end-of-buffer)
1997                           nil))
1998             ;; Move by arg lines, but ignore invisible ones.
1999             (while (> arg 0)
2000               (end-of-line)
2001               (and (zerop (vertical-motion 1))
2002                    (signal 'end-of-buffer nil))
2003               ;; If the following character is currently invisible,
2004               ;; skip all characters with that same `invisible' property value.
2005               (while (and (not (eobp))
2006                           (let ((prop
2007                                  (get-char-property (point) 'invisible)))
2008                             (if (eq buffer-invisibility-spec t)
2009                                 prop
2010                               (or (memq prop buffer-invisibility-spec)
2011                                   (assq prop buffer-invisibility-spec)))))
2012                 (if (get-text-property (point) 'invisible)
2013                     (goto-char (next-single-property-change (point) 'invisible))
2014                   (goto-char (next-extent-change (point))))) ; XEmacs
2015               (setq arg (1- arg)))
2016             (while (< arg 0)
2017               (beginning-of-line)
2018               (and (zerop (vertical-motion -1))
2019                    (signal 'beginning-of-buffer nil))
2020               (while (and (not (bobp))
2021                           (let ((prop
2022                                  (get-char-property (1- (point)) 'invisible)))
2023                             (if (eq buffer-invisibility-spec t)
2024                                 prop
2025                               (or (memq prop buffer-invisibility-spec)
2026                                   (assq prop buffer-invisibility-spec)))))
2027                 (if (get-text-property (1- (point)) 'invisible)
2028                     (goto-char (previous-single-property-change (point) 'invisible))
2029                   (goto-char (previous-extent-change (point))))) ; XEmacs
2030               (setq arg (1+ arg))))
2031           (move-to-column (or goal-column temporary-goal-column)))
2032       ;; Remember where we moved to, go back home,
2033       ;; then do the motion over again
2034       ;; in just one step, with intangibility and point-motion hooks
2035       ;; enabled this time.
2036       (setq new (point))
2037       (goto-char opoint)
2038       (setq inhibit-point-motion-hooks nil)
2039       (goto-char new)))
2040   nil)
2041
2042 ;;; Many people have said they rarely use this feature, and often type
2043 ;;; it by accident.  Maybe it shouldn't even be on a key.
2044 ;; It's not on a key, as of 20.2.  So no need for this.
2045 ;(put 'set-goal-column 'disabled t)
2046
2047 (defun set-goal-column (arg)
2048   "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line].
2049 Those commands will move to this position in the line moved to
2050 rather than trying to keep the same horizontal position.
2051 With a non-nil argument, clears out the goal column
2052 so that \\[next-line] and \\[previous-line] resume vertical motion.
2053 The goal column is stored in the variable `goal-column'."
2054   (interactive "_P") ; XEmacs
2055   (if arg
2056       (progn
2057         (setq goal-column nil)
2058         (display-message 'command "No goal column"))
2059     (setq goal-column (current-column))
2060     (lmessage 'command
2061         "Goal column %d (use %s with an arg to unset it)"
2062       goal-column
2063       (substitute-command-keys "\\[set-goal-column]")))
2064   nil)
2065 \f
2066 ;; deleted FSFmacs terminal randomness hscroll-point-visible stuff.
2067 ;; hscroll-step
2068 ;; hscroll-point-visible
2069 ;; hscroll-window-column
2070 ;; right-arrow
2071 ;; left-arrow
2072
2073 (defun scroll-other-window-down (lines)
2074   "Scroll the \"other window\" down.
2075 For more details, see the documentation for `scroll-other-window'."
2076   (interactive "P")
2077   (scroll-other-window
2078    ;; Just invert the argument's meaning.
2079    ;; We can do that without knowing which window it will be.
2080    (if (eq lines '-) nil
2081      (if (null lines) '-
2082        (- (prefix-numeric-value lines))))))
2083 ;(define-key esc-map [?\C-\S-v] 'scroll-other-window-down)
2084
2085 (defun beginning-of-buffer-other-window (arg)
2086   "Move point to the beginning of the buffer in the other window.
2087 Leave mark at previous position.
2088 With arg N, put point N/10 of the way from the true beginning."
2089   (interactive "P")
2090   (let ((orig-window (selected-window))
2091         (window (other-window-for-scrolling)))
2092     ;; We use unwind-protect rather than save-window-excursion
2093     ;; because the latter would preserve the things we want to change.
2094     (unwind-protect
2095         (progn
2096           (select-window window)
2097           ;; Set point and mark in that window's buffer.
2098           (beginning-of-buffer arg)
2099           ;; Set point accordingly.
2100           (recenter '(t)))
2101       (select-window orig-window))))
2102
2103 (defun end-of-buffer-other-window (arg)
2104   "Move point to the end of the buffer in the other window.
2105 Leave mark at previous position.
2106 With arg N, put point N/10 of the way from the true end."
2107   (interactive "P")
2108   ;; See beginning-of-buffer-other-window for comments.
2109   (let ((orig-window (selected-window))
2110         (window (other-window-for-scrolling)))
2111     (unwind-protect
2112         (progn
2113           (select-window window)
2114           (end-of-buffer arg)
2115           (recenter '(t)))
2116       (select-window orig-window))))
2117 \f
2118 (defun transpose-chars (arg)
2119   "Interchange characters around point, moving forward one character.
2120 With prefix arg ARG, effect is to take character before point
2121 and drag it forward past ARG other characters (backward if ARG negative).
2122 If no argument and at end of line, the previous two chars are exchanged."
2123   (interactive "*P")
2124   (and (null arg) (eolp) (forward-char -1))
2125   (transpose-subr 'forward-char (prefix-numeric-value arg)))
2126
2127 ;;; A very old implementation of transpose-chars from the old days ...
2128 (defun transpose-preceding-chars (arg)
2129   "Interchange characters before point.
2130 With prefix arg ARG, effect is to take character before point
2131 and drag it forward past ARG other characters (backward if ARG negative).
2132 If no argument and not at start of line, the previous two chars are exchanged."
2133   (interactive "*P")
2134   (and (null arg) (not (bolp)) (forward-char -1))
2135   (transpose-subr 'forward-char (prefix-numeric-value arg)))
2136
2137
2138 (defun transpose-words (arg)
2139   "Interchange words around point, leaving point at end of them.
2140 With prefix arg ARG, effect is to take word before or around point
2141 and drag it forward past ARG other words (backward if ARG negative).
2142 If ARG is zero, the words around or after point and around or after mark
2143 are interchanged."
2144   (interactive "*p")
2145   (transpose-subr 'forward-word arg))
2146
2147 (defun transpose-sexps (arg)
2148   "Like \\[transpose-words] but applies to sexps.
2149 Does not work on a sexp that point is in the middle of
2150 if it is a list or string."
2151   (interactive "*p")
2152   (transpose-subr 'forward-sexp arg))
2153
2154 (defun transpose-lines (arg)
2155   "Exchange current line and previous line, leaving point after both.
2156 With argument ARG, takes previous line and moves it past ARG lines.
2157 With argument 0, interchanges line point is in with line mark is in."
2158   (interactive "*p")
2159   (transpose-subr #'(lambda (arg)
2160                      (if (= arg 1)
2161                          (progn
2162                            ;; Move forward over a line,
2163                            ;; but create a newline if none exists yet.
2164                            (end-of-line)
2165                            (if (eobp)
2166                                (newline)
2167                              (forward-char 1)))
2168                        (forward-line arg)))
2169                   arg))
2170
2171 (eval-when-compile
2172   ;; avoid byte-compiler warnings...
2173   (defvar start1)
2174   (defvar start2)
2175   (defvar end1)
2176   (defvar end2))
2177
2178 ; start[12] and end[12] used in transpose-subr-1 below
2179 (defun transpose-subr (mover arg)
2180   (let (start1 end1 start2 end2)
2181     (if (= arg 0)
2182         (progn
2183           (save-excursion
2184             (funcall mover 1)
2185             (setq end2 (point))
2186             (funcall mover -1)
2187             (setq start2 (point))
2188             (goto-char (mark t)) ; XEmacs
2189             (funcall mover 1)
2190             (setq end1 (point))
2191             (funcall mover -1)
2192             (setq start1 (point))
2193             (transpose-subr-1))
2194           (exchange-point-and-mark t))) ; XEmacs
2195     (while (> arg 0)
2196       (funcall mover -1)
2197       (setq start1 (point))
2198       (funcall mover 1)
2199       (setq end1 (point))
2200       (funcall mover 1)
2201       (setq end2 (point))
2202       (funcall mover -1)
2203       (setq start2 (point))
2204       (transpose-subr-1)
2205       (goto-char end2)
2206       (setq arg (1- arg)))
2207     (while (< arg 0)
2208       (funcall mover -1)
2209       (setq start2 (point))
2210       (funcall mover -1)
2211       (setq start1 (point))
2212       (funcall mover 1)
2213       (setq end1 (point))
2214       (funcall mover 1)
2215       (setq end2 (point))
2216       (transpose-subr-1)
2217       (setq arg (1+ arg)))))
2218
2219 ; start[12] and end[12] used free
2220 (defun transpose-subr-1 ()
2221   (if (> (min end1 end2) (max start1 start2))
2222       (error "Don't have two things to transpose"))
2223   (let ((word1 (buffer-substring start1 end1))
2224         (word2 (buffer-substring start2 end2)))
2225     (delete-region start2 end2)
2226     (goto-char start2)
2227     (insert word1)
2228     (goto-char (if (< start1 start2) start1
2229                  (+ start1 (- (length word1) (length word2)))))
2230     (delete-char (length word1))
2231     (insert word2)))
2232 \f
2233 (defcustom comment-column 32
2234   "*Column to indent right-margin comments to.
2235 Setting this variable automatically makes it local to the current buffer.
2236 Each mode establishes a different default value for this variable; you
2237 can set the value for a particular mode using that mode's hook."
2238   :type 'integer
2239   :group 'fill-comments)
2240 (make-variable-buffer-local 'comment-column)
2241
2242 (defcustom comment-start nil
2243   "*String to insert to start a new comment, or nil if no comment syntax."
2244   :type '(choice (const :tag "None" nil)
2245                  string)
2246   :group 'fill-comments)
2247
2248 (defcustom comment-start-skip nil
2249   "*Regexp to match the start of a comment plus everything up to its body.
2250 If there are any \\(...\\) pairs, the comment delimiter text is held to begin
2251 at the place matched by the close of the first pair."
2252   :type '(choice (const :tag "None" nil)
2253                  regexp)
2254   :group 'fill-comments)
2255
2256 (defcustom comment-end ""
2257   "*String to insert to end a new comment.
2258 Should be an empty string if comments are terminated by end-of-line."
2259   :type 'string
2260   :group 'fill-comments)
2261
2262 (defconst comment-indent-hook nil
2263   "Obsolete variable for function to compute desired indentation for a comment.
2264 Use `comment-indent-function' instead.
2265 This function is called with no args with point at the beginning of
2266 the comment's starting delimiter.")
2267
2268 (defconst comment-indent-function
2269   ;; XEmacs - add at least one space after the end of the text on the
2270   ;; current line...
2271   (lambda ()
2272     (save-excursion
2273       (beginning-of-line)
2274       (let ((eol (save-excursion (end-of-line) (point))))
2275         (and comment-start-skip
2276              (re-search-forward comment-start-skip eol t)
2277              (setq eol (match-beginning 0)))
2278         (goto-char eol)
2279         (skip-chars-backward " \t")
2280         (max comment-column (1+ (current-column))))))
2281   "Function to compute desired indentation for a comment.
2282 This function is called with no args with point at the beginning of
2283 the comment's starting delimiter.")
2284
2285 (defcustom block-comment-start nil
2286   "*String to insert to start a new comment on a line by itself.
2287 If nil, use `comment-start' instead.
2288 Note that the regular expression `comment-start-skip' should skip this string
2289 as well as the `comment-start' string."
2290   :type '(choice (const :tag "Use `comment-start'" nil)
2291                  string)
2292   :group 'fill-comments)
2293
2294 (defcustom block-comment-end nil
2295   "*String to insert to end a new comment on a line by itself.
2296 Should be an empty string if comments are terminated by end-of-line.
2297 If nil, use `comment-end' instead."
2298   :type '(choice (const :tag "Use `comment-end'" nil)
2299                  string)
2300   :group 'fill-comments)
2301
2302 (defun indent-for-comment ()
2303   "Indent this line's comment to comment column, or insert an empty comment."
2304   (interactive "*")
2305   (let* ((empty (save-excursion (beginning-of-line)
2306                                 (looking-at "[ \t]*$")))
2307          (starter (or (and empty block-comment-start) comment-start))
2308          (ender (or (and empty block-comment-end) comment-end)))
2309     (if (null starter)
2310         (error "No comment syntax defined")
2311       (let* ((eolpos (save-excursion (end-of-line) (point)))
2312              cpos indent begpos)
2313         (beginning-of-line)
2314         (if (re-search-forward comment-start-skip eolpos 'move)
2315             (progn (setq cpos (point-marker))
2316                    ;; Find the start of the comment delimiter.
2317                    ;; If there were paren-pairs in comment-start-skip,
2318                    ;; position at the end of the first pair.
2319                    (if (match-end 1)
2320                        (goto-char (match-end 1))
2321                      ;; If comment-start-skip matched a string with
2322                      ;; internal whitespace (not final whitespace) then
2323                      ;; the delimiter start at the end of that
2324                      ;; whitespace.  Otherwise, it starts at the
2325                      ;; beginning of what was matched.
2326                      (skip-syntax-backward " " (match-beginning 0))
2327                      (skip-syntax-backward "^ " (match-beginning 0)))))
2328         (setq begpos (point))
2329         ;; Compute desired indent.
2330         (if (= (current-column)
2331                (setq indent (funcall comment-indent-function)))
2332             (goto-char begpos)
2333           ;; If that's different from current, change it.
2334           (skip-chars-backward " \t")
2335           (delete-region (point) begpos)
2336           (indent-to indent))
2337         ;; An existing comment?
2338         (if cpos
2339             (progn (goto-char cpos)
2340                    (set-marker cpos nil))
2341           ;; No, insert one.
2342           (insert starter)
2343           (save-excursion
2344             (insert ender)))))))
2345
2346 (defun set-comment-column (arg)
2347   "Set the comment column based on point.
2348 With no arg, set the comment column to the current column.
2349 With just minus as arg, kill any comment on this line.
2350 With any other arg, set comment column to indentation of the previous comment
2351  and then align or create a comment on this line at that column."
2352   (interactive "P")
2353   (if (eq arg '-)
2354       (kill-comment nil)
2355     (if arg
2356         (progn
2357           (save-excursion
2358             (beginning-of-line)
2359             (re-search-backward comment-start-skip)
2360             (beginning-of-line)
2361             (re-search-forward comment-start-skip)
2362             (goto-char (match-beginning 0))
2363             (setq comment-column (current-column))
2364             (lmessage 'command "Comment column set to %d" comment-column))
2365           (indent-for-comment))
2366       (setq comment-column (current-column))
2367       (lmessage 'command "Comment column set to %d" comment-column))))
2368
2369 (defun kill-comment (arg)
2370   "Kill the comment on this line, if any.
2371 With argument, kill comments on that many lines starting with this one."
2372   ;; this function loses in a lot of situations.  it incorrectly recognizes
2373   ;; comment delimiters sometimes (ergo, inside a string), doesn't work
2374   ;; with multi-line comments, can kill extra whitespace if comment wasn't
2375   ;; through end-of-line, et cetera.
2376   (interactive "*P")
2377   (or comment-start-skip (error "No comment syntax defined"))
2378   (let ((count (prefix-numeric-value arg)) endc)
2379     (while (> count 0)
2380       (save-excursion
2381         (end-of-line)
2382         (setq endc (point))
2383         (beginning-of-line)
2384         (and (string< "" comment-end)
2385              (setq endc
2386                    (progn
2387                      (re-search-forward (regexp-quote comment-end) endc 'move)
2388                      (skip-chars-forward " \t")
2389                      (point))))
2390         (beginning-of-line)
2391         (if (re-search-forward comment-start-skip endc t)
2392             (progn
2393               (goto-char (match-beginning 0))
2394               (skip-chars-backward " \t")
2395               (kill-region (point) endc)
2396               ;; to catch comments a line beginnings
2397               (indent-according-to-mode))))
2398       (if arg (forward-line 1))
2399       (setq count (1- count)))))
2400
2401 (defun comment-region (beg end &optional arg)
2402   "Comment or uncomment each line in the region.
2403 With just C-u prefix arg, uncomment each line in region.
2404 Numeric prefix arg ARG means use ARG comment characters.
2405 If ARG is negative, delete that many comment characters instead.
2406 Comments are terminated on each line, even for syntax in which newline does
2407 not end the comment.  Blank lines do not get comments."
2408   ;; if someone wants it to only put a comment-start at the beginning and
2409   ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x
2410   ;; is easy enough.  No option is made here for other than commenting
2411   ;; every line.
2412   (interactive "r\nP")
2413   (or comment-start (error "No comment syntax is defined"))
2414   (if (> beg end) (let (mid) (setq mid beg beg end end mid)))
2415   (save-excursion
2416     (save-restriction
2417       (let ((cs comment-start) (ce comment-end)
2418             numarg)
2419         (if (consp arg) (setq numarg t)
2420           (setq numarg (prefix-numeric-value arg))
2421           ;; For positive arg > 1, replicate the comment delims now,
2422           ;; then insert the replicated strings just once.
2423           (while (> numarg 1)
2424             (setq cs (concat cs comment-start)
2425                   ce (concat ce comment-end))
2426             (setq numarg (1- numarg))))
2427         ;; Loop over all lines from BEG to END.
2428         (narrow-to-region beg end)
2429         (goto-char beg)
2430         (while (not (eobp))
2431           (if (or (eq numarg t) (< numarg 0))
2432               (progn
2433                 ;; Delete comment start from beginning of line.
2434                 (if (eq numarg t)
2435                     (while (looking-at (regexp-quote cs))
2436                       (delete-char (length cs)))
2437                   (let ((count numarg))
2438                     (while (and (> 1 (setq count (1+ count)))
2439                                 (looking-at (regexp-quote cs)))
2440                       (delete-char (length cs)))))
2441                 ;; Delete comment end from end of line.
2442                 (if (string= "" ce)
2443                     nil
2444                   (if (eq numarg t)
2445                       (progn
2446                         (end-of-line)
2447                         ;; This is questionable if comment-end ends in
2448                         ;; whitespace.  That is pretty brain-damaged,
2449                         ;; though.
2450                         (skip-chars-backward " \t")
2451                         (if (and (>= (- (point) (point-min)) (length ce))
2452                                  (save-excursion
2453                                    (backward-char (length ce))
2454                                    (looking-at (regexp-quote ce))))
2455                             (delete-char (- (length ce)))))
2456                     (let ((count numarg))
2457                       (while (> 1 (setq count (1+ count)))
2458                         (end-of-line)
2459                         ;; This is questionable if comment-end ends in
2460                         ;; whitespace.  That is pretty brain-damaged though
2461                         (skip-chars-backward " \t")
2462                         (save-excursion
2463                           (backward-char (length ce))
2464                           (if (looking-at (regexp-quote ce))
2465                               (delete-char (length ce))))))))
2466                 (forward-line 1))
2467             ;; Insert at beginning and at end.
2468             (if (looking-at "[ \t]*$") ()
2469               (insert cs)
2470               (if (string= "" ce) ()
2471                 (end-of-line)
2472                 (insert ce)))
2473             (search-forward "\n" nil 'move)))))))
2474
2475 ;; XEmacs
2476 (defun prefix-region (prefix)
2477   "Add a prefix string to each line between mark and point."
2478   (interactive "sPrefix string: ")
2479   (if prefix
2480       (let ((count (count-lines (mark) (point))))
2481         (goto-char (min (mark) (point)))
2482         (while (> count 0)
2483           (setq count (1- count))
2484           (beginning-of-line 1)
2485           (insert prefix)
2486           (end-of-line 1)
2487           (forward-char 1)))))
2488
2489 \f
2490 ;; XEmacs - extra parameter
2491 (defun backward-word (arg &optional buffer)
2492   "Move backward until encountering the end of a word.
2493 With argument, do this that many times.
2494 In programs, it is faster to call `forward-word' with negative arg."
2495   (interactive "_p") ; XEmacs
2496   (forward-word (- arg) buffer))
2497
2498 (defun mark-word (arg)
2499   "Set mark arg words away from point."
2500   (interactive "p")
2501   (mark-something 'mark-word 'forward-word arg))
2502
2503 ;; XEmacs modified
2504 (defun kill-word (arg)
2505   "Kill characters forward until encountering the end of a word.
2506 With argument, do this that many times."
2507   (interactive "*p")
2508   (kill-region (point) (save-excursion (forward-word arg) (point))))
2509
2510 (defun backward-kill-word (arg)
2511   "Kill characters backward until encountering the end of a word.
2512 With argument, do this that many times."
2513   (interactive "*p") ; XEmacs
2514   (kill-word (- arg)))
2515
2516 (defun current-word (&optional strict)
2517   "Return the word point is on (or a nearby word) as a string.
2518 If optional arg STRICT is non-nil, return nil unless point is within
2519 or adjacent to a word.
2520 If point is not between two word-constituent characters, but immediately
2521 follows one, move back first.
2522 Otherwise, if point precedes a word constituent, move forward first.
2523 Otherwise, move backwards until a word constituent is found and get that word;
2524 if you a newlines is reached first, move forward instead."
2525   (save-excursion
2526     (let ((oldpoint (point)) (start (point)) (end (point)))
2527       (skip-syntax-backward "w_") (setq start (point))
2528       (goto-char oldpoint)
2529       (skip-syntax-forward "w_") (setq end (point))
2530       (if (and (eq start oldpoint) (eq end oldpoint))
2531           ;; Point is neither within nor adjacent to a word.
2532           (and (not strict)
2533                (progn
2534                  ;; Look for preceding word in same line.
2535                  (skip-syntax-backward "^w_"
2536                                        (save-excursion
2537                                          (beginning-of-line) (point)))
2538                  (if (bolp)
2539                      ;; No preceding word in same line.
2540                      ;; Look for following word in same line.
2541                      (progn
2542                        (skip-syntax-forward "^w_"
2543                                             (save-excursion
2544                                               (end-of-line) (point)))
2545                        (setq start (point))
2546                        (skip-syntax-forward "w_")
2547                        (setq end (point)))
2548                      (setq end (point))
2549                      (skip-syntax-backward "w_")
2550                      (setq start (point)))
2551                  (buffer-substring start end)))
2552           (buffer-substring start end)))))
2553 \f
2554 (defcustom fill-prefix nil
2555   "*String for filling to insert at front of new line, or nil for none.
2556 Setting this variable automatically makes it local to the current buffer."
2557   :type '(choice (const :tag "None" nil)
2558                  string)
2559   :group 'fill)
2560 (make-variable-buffer-local 'fill-prefix)
2561
2562 (defcustom auto-fill-inhibit-regexp nil
2563   "*Regexp to match lines which should not be auto-filled."
2564   :type '(choice (const :tag "None" nil)
2565                  regexp)
2566   :group 'fill)
2567
2568 (defvar comment-line-break-function 'indent-new-comment-line
2569   "*Mode-specific function which line breaks and continues a comment.
2570
2571 This function is only called during auto-filling of a comment section.
2572 The function should take a single optional argument which is a flag
2573 indicating whether soft newlines should be inserted.")
2574
2575 ;; defined in mule-base/mule-category.el
2576 (defvar word-across-newline)
2577
2578 ;; This function is the auto-fill-function of a buffer
2579 ;; when Auto-Fill mode is enabled.
2580 ;; It returns t if it really did any work.
2581 ;; XEmacs:  This function is totally different.
2582 (defun do-auto-fill ()
2583   (let (give-up)
2584     (or (and auto-fill-inhibit-regexp
2585              (save-excursion (beginning-of-line)
2586                              (looking-at auto-fill-inhibit-regexp)))
2587         (while (and (not give-up) (> (current-column) fill-column))
2588           ;; Determine where to split the line.
2589           (let ((fill-prefix fill-prefix)
2590                 (fill-point
2591                  (let ((opoint (point))
2592                        bounce
2593                        ;; 97/3/14 jhod: Kinsoku
2594                        (re-break-point (if (featurep 'mule)
2595                                             (concat "[ \t\n]\\|" word-across-newline)
2596                                         "[ \t\n]"))
2597                        ;; end patch
2598                        (first t))
2599                    (save-excursion
2600                      (move-to-column (1+ fill-column))
2601                      ;; Move back to a word boundary.
2602                      (while (or first
2603                                 ;; If this is after period and a single space,
2604                                 ;; move back once more--we don't want to break
2605                                 ;; the line there and make it look like a
2606                                 ;; sentence end.
2607                                 (and (not (bobp))
2608                                      (not bounce)
2609                                      sentence-end-double-space
2610                                      (save-excursion (forward-char -1)
2611                                                      (and (looking-at "\\. ")
2612                                                           (not (looking-at "\\.  "))))))
2613                        (setq first nil)
2614                        ;; 97/3/14 jhod: Kinsoku
2615                        ; (skip-chars-backward "^ \t\n"))
2616                        (fill-move-backward-to-break-point re-break-point)
2617                        ;; end patch
2618                        ;; If we find nowhere on the line to break it,
2619                        ;; break after one word.  Set bounce to t
2620                        ;; so we will not keep going in this while loop.
2621                        (if (bolp)
2622                            (progn
2623                              ;; 97/3/14 jhod: Kinsoku
2624                              ; (re-search-forward "[ \t]" opoint t)
2625                              (fill-move-forward-to-break-point re-break-point
2626                                                                opoint)
2627                              ;; end patch
2628                              (setq bounce t)))
2629                        (skip-chars-backward " \t"))
2630                      (if (and (featurep 'mule)
2631                               (or bounce (bolp))) (kinsoku-process)) ;; 97/3/14 jhod: Kinsoku
2632                      ;; Let fill-point be set to the place where we end up.
2633                      (point)))))
2634
2635             ;; I'm not sure why Stig made this change but it breaks
2636             ;; auto filling in at least C mode so I'm taking it back
2637             ;; out.  --cet
2638             ;; XEmacs - adaptive fill.
2639             ;;(maybe-adapt-fill-prefix
2640             ;; (or from (setq from (save-excursion (beginning-of-line)
2641             ;;                                   (point))))
2642             ;; (or to   (setq to (save-excursion (beginning-of-line 2)
2643             ;;                                 (point))))
2644             ;; t)
2645
2646             ;; If that place is not the beginning of the line,
2647             ;; break the line there.
2648             (if (save-excursion
2649                   (goto-char fill-point)
2650                   (not (or (bolp) (eolp)))) ; 97/3/14 jhod: during kinsoku processing it is possible to move beyond
2651                 (let ((prev-column (current-column)))
2652                   ;; If point is at the fill-point, do not `save-excursion'.
2653                   ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2654                   ;; point will end up before it rather than after it.
2655                   (if (save-excursion
2656                         (skip-chars-backward " \t")
2657                         (= (point) fill-point))
2658                       ;; 97/3/14 jhod: Kinsoku processing
2659                       ;(indent-new-comment-line)
2660                       (let ((spacep (memq (char-before (point)) '(?\  ?\t))))
2661                         (funcall comment-line-break-function)
2662                         ;; if user type space explicitly, leave SPC
2663                         ;; even if there is no WAN.
2664                         (if spacep
2665                             (save-excursion
2666                               (goto-char fill-point)
2667                               ;; put SPC except that there is SPC
2668                               ;; already or there is sentence end.
2669                               (or (memq (char-after (point)) '(?\  ?\t))
2670                                   (fill-end-of-sentence-p)
2671                                   (insert ?\ )))))
2672                     (save-excursion
2673                       (goto-char fill-point)
2674                       (funcall comment-line-break-function)))
2675                   ;; If making the new line didn't reduce the hpos of
2676                   ;; the end of the line, then give up now;
2677                   ;; trying again will not help.
2678                   (if (>= (current-column) prev-column)
2679                       (setq give-up t)))
2680               ;; No place to break => stop trying.
2681               (setq give-up t)))))))
2682
2683 ;; Put FSF one in until I can one or the other working properly, then the
2684 ;; other one is history.
2685 ;(defun fsf:do-auto-fill ()
2686 ;  (let (fc justify
2687 ;          ;; bol
2688 ;          give-up
2689 ;          (fill-prefix fill-prefix))
2690 ;    (if (or (not (setq justify (current-justification)))
2691 ;           (null (setq fc (current-fill-column)))
2692 ;           (and (eq justify 'left)
2693 ;                (<= (current-column) fc))
2694 ;           (save-excursion (beginning-of-line)
2695 ;                           ;; (setq bol (point))
2696 ;                           (and auto-fill-inhibit-regexp
2697 ;                                (looking-at auto-fill-inhibit-regexp))))
2698 ;       nil ;; Auto-filling not required
2699 ;      (if (memq justify '(full center right))
2700 ;         (save-excursion (unjustify-current-line)))
2701
2702 ;      ;; Choose a fill-prefix automatically.
2703 ;      (if (and adaptive-fill-mode
2704 ;              (or (null fill-prefix) (string= fill-prefix "")))
2705 ;         (let ((prefix
2706 ;                (fill-context-prefix
2707 ;                 (save-excursion (backward-paragraph 1) (point))
2708 ;                 (save-excursion (forward-paragraph 1) (point))
2709 ;                 ;; Don't accept a non-whitespace fill prefix
2710 ;                 ;; from the first line of a paragraph.
2711 ;                 "^[ \t]*$")))
2712 ;           (and prefix (not (equal prefix ""))
2713 ;                (setq fill-prefix prefix))))
2714
2715 ;      (while (and (not give-up) (> (current-column) fc))
2716 ;       ;; Determine where to split the line.
2717 ;       (let ((fill-point
2718 ;              (let ((opoint (point))
2719 ;                    bounce
2720 ;                    (first t))
2721 ;                (save-excursion
2722 ;                  (move-to-column (1+ fc))
2723 ;                  ;; Move back to a word boundary.
2724 ;                  (while (or first
2725 ;                             ;; If this is after period and a single space,
2726 ;                             ;; move back once more--we don't want to break
2727 ;                             ;; the line there and make it look like a
2728 ;                             ;; sentence end.
2729 ;                             (and (not (bobp))
2730 ;                                  (not bounce)
2731 ;                                  sentence-end-double-space
2732 ;                                  (save-excursion (forward-char -1)
2733 ;                                                  (and (looking-at "\\. ")
2734 ;                                                       (not (looking-at "\\.  "))))))
2735 ;                    (setq first nil)
2736 ;                    (skip-chars-backward "^ \t\n")
2737 ;                    ;; If we find nowhere on the line to break it,
2738 ;                    ;; break after one word.  Set bounce to t
2739 ;                    ;; so we will not keep going in this while loop.
2740 ;                    (if (bolp)
2741 ;                        (progn
2742 ;                          (re-search-forward "[ \t]" opoint t)
2743 ;                          (setq bounce t)))
2744 ;                    (skip-chars-backward " \t"))
2745 ;                  ;; Let fill-point be set to the place where we end up.
2746 ;                  (point)))))
2747 ;         ;; If that place is not the beginning of the line,
2748 ;         ;; break the line there.
2749 ;         (if (save-excursion
2750 ;               (goto-char fill-point)
2751 ;               (not (bolp)))
2752 ;             (let ((prev-column (current-column)))
2753 ;               ;; If point is at the fill-point, do not `save-excursion'.
2754 ;               ;; Otherwise, if a comment prefix or fill-prefix is inserted,
2755 ;               ;; point will end up before it rather than after it.
2756 ;               (if (save-excursion
2757 ;                     (skip-chars-backward " \t")
2758 ;                     (= (point) fill-point))
2759 ;                   (funcall comment-line-break-function t)
2760 ;                 (save-excursion
2761 ;                   (goto-char fill-point)
2762 ;                   (funcall comment-line-break-function t)))
2763 ;               ;; Now do justification, if required
2764 ;               (if (not (eq justify 'left))
2765 ;                   (save-excursion
2766 ;                     (end-of-line 0)
2767 ;                     (justify-current-line justify nil t)))
2768 ;               ;; If making the new line didn't reduce the hpos of
2769 ;               ;; the end of the line, then give up now;
2770 ;               ;; trying again will not help.
2771 ;               (if (>= (current-column) prev-column)
2772 ;                   (setq give-up t)))
2773 ;           ;; No place to break => stop trying.
2774 ;           (setq give-up t))))
2775 ;      ;; Justify last line.
2776 ;      (justify-current-line justify t t)
2777 ;      t)))
2778
2779 (defvar normal-auto-fill-function 'do-auto-fill
2780   "The function to use for `auto-fill-function' if Auto Fill mode is turned on.
2781 Some major modes set this.")
2782
2783 (defun auto-fill-mode (&optional arg)
2784   "Toggle auto-fill mode.
2785 With arg, turn auto-fill mode on if and only if arg is positive.
2786 In Auto-Fill mode, inserting a space at a column beyond `current-fill-column'
2787 automatically breaks the line at a previous space.
2788
2789 The value of `normal-auto-fill-function' specifies the function to use
2790 for `auto-fill-function' when turning Auto Fill mode on."
2791   (interactive "P")
2792   (prog1 (setq auto-fill-function
2793                (if (if (null arg)
2794                        (not auto-fill-function)
2795                        (> (prefix-numeric-value arg) 0))
2796                    normal-auto-fill-function
2797                    nil))
2798     (redraw-modeline)))
2799
2800 ;; This holds a document string used to document auto-fill-mode.
2801 (defun auto-fill-function ()
2802   "Automatically break line at a previous space, in insertion of text."
2803   nil)
2804
2805 (defun turn-on-auto-fill ()
2806   "Unconditionally turn on Auto Fill mode."
2807   (auto-fill-mode 1))
2808
2809 (defun set-fill-column (arg)
2810   "Set `fill-column' to specified argument.
2811 Just \\[universal-argument] as argument means to use the current column
2812 The variable `fill-column' has a separate value for each buffer."
2813   (interactive "_P") ; XEmacs
2814   (cond ((integerp arg)
2815          (setq fill-column arg))
2816         ((consp arg)
2817          (setq fill-column (current-column)))
2818         ;; Disallow missing argument; it's probably a typo for C-x C-f.
2819         (t
2820          (error "set-fill-column requires an explicit argument")))
2821   (lmessage 'command "fill-column set to %d" fill-column))
2822 \f
2823 (defcustom comment-multi-line t ; XEmacs - this works well with adaptive fill
2824   "*Non-nil means \\[indent-new-comment-line] should continue same comment
2825 on new line, with no new terminator or starter.
2826 This is obsolete because you might as well use \\[newline-and-indent]."
2827   :type 'boolean
2828   :group 'fill-comments)
2829
2830 (defun indent-new-comment-line (&optional soft)
2831   "Break line at point and indent, continuing comment if within one.
2832 This indents the body of the continued comment
2833 under the previous comment line.
2834
2835 This command is intended for styles where you write a comment per line,
2836 starting a new comment (and terminating it if necessary) on each line.
2837 If you want to continue one comment across several lines, use \\[newline-and-indent].
2838
2839 If a fill column is specified, it overrides the use of the comment column
2840 or comment indentation.
2841
2842 The inserted newline is marked hard if `use-hard-newlines' is true,
2843 unless optional argument SOFT is non-nil."
2844   (interactive)
2845   (let (comcol comstart)
2846     (skip-chars-backward " \t")
2847     ;; 97/3/14 jhod: Kinsoku processing
2848     (if (featurep 'mule)
2849         (kinsoku-process))
2850     (delete-region (point)
2851                    (progn (skip-chars-forward " \t")
2852                           (point)))
2853     (if soft (insert ?\n) (newline 1))
2854     (if fill-prefix
2855         (progn
2856           (indent-to-left-margin)
2857           (insert fill-prefix))
2858     ;; #### - Eric Eide reverts to v18 semantics for this function in
2859     ;; fa-extras, which I'm not gonna do.  His changes are to (1) execute
2860     ;; the save-excursion below unconditionally, and (2) uncomment the check
2861     ;; for (not comment-multi-line) further below.  --Stig
2862       ;;#### jhod: probably need to fix this for kinsoku processing
2863       (if (not comment-multi-line)
2864           (save-excursion
2865             (if (and comment-start-skip
2866                      (let ((opoint (point)))
2867                        (forward-line -1)
2868                        (re-search-forward comment-start-skip opoint t)))
2869                 ;; The old line is a comment.
2870                 ;; Set WIN to the pos of the comment-start.
2871                 ;; But if the comment is empty, look at preceding lines
2872                 ;; to find one that has a nonempty comment.
2873
2874                 ;; If comment-start-skip contains a \(...\) pair,
2875                 ;; the real comment delimiter starts at the end of that pair.
2876                 (let ((win (or (match-end 1) (match-beginning 0))))
2877                   (while (and (eolp) (not (bobp))
2878                               (let (opoint)
2879                                 (beginning-of-line)
2880                                 (setq opoint (point))
2881                                 (forward-line -1)
2882                                 (re-search-forward comment-start-skip opoint t)))
2883                     (setq win (or (match-end 1) (match-beginning 0))))
2884                   ;; Indent this line like what we found.
2885                   (goto-char win)
2886                   (setq comcol (current-column))
2887                   (setq comstart
2888                         (buffer-substring (point) (match-end 0)))))))
2889       (if (and comcol (not fill-prefix))  ; XEmacs - (ENE) from fa-extras.
2890           (let ((comment-column comcol)
2891                 (comment-start comstart)
2892                 (block-comment-start comstart)
2893                 (comment-end comment-end))
2894             (and comment-end (not (equal comment-end ""))
2895   ;            (if (not comment-multi-line)
2896                      (progn
2897                        (forward-char -1)
2898                        (insert comment-end)
2899                        (forward-char 1))
2900   ;              (setq comment-column (+ comment-column (length comment-start))
2901   ;                    comment-start "")
2902   ;                )
2903                  )
2904             (if (not (eolp))
2905                 (setq comment-end ""))
2906             (insert ?\n)
2907             (forward-char -1)
2908             (indent-for-comment)
2909             (save-excursion
2910               ;; Make sure we delete the newline inserted above.
2911               (end-of-line)
2912               (delete-char 1)))
2913         (indent-according-to-mode)))))
2914
2915 \f
2916 (defun set-selective-display (arg)
2917   "Set `selective-display' to ARG; clear it if no arg.
2918 When the value of `selective-display' is a number > 0,
2919 lines whose indentation is >= that value are not displayed.
2920 The variable `selective-display' has a separate value for each buffer."
2921   (interactive "P")
2922   (if (eq selective-display t)
2923       (error "selective-display already in use for marked lines"))
2924   (let ((current-vpos
2925          (save-restriction
2926            (narrow-to-region (point-min) (point))
2927            (goto-char (window-start))
2928            (vertical-motion (window-height)))))
2929     (setq selective-display
2930           (and arg (prefix-numeric-value arg)))
2931     (recenter current-vpos))
2932   (set-window-start (selected-window) (window-start (selected-window)))
2933   ;; #### doesn't localize properly:
2934   (princ "selective-display set to " t)
2935   (prin1 selective-display t)
2936   (princ "." t))
2937
2938 ;; XEmacs
2939 (defun nuke-selective-display ()
2940   "Ensure that the buffer is not in selective-display mode.
2941 If `selective-display' is t, then restore the buffer text to its original
2942 state before disabling selective display."
2943   ;; by Stig@hackvan.com
2944   (interactive)
2945   (and (eq t selective-display)
2946        (save-excursion
2947          (save-restriction
2948            (widen)
2949            (goto-char (point-min))
2950            (let ((mod-p (buffer-modified-p))
2951                  (buffer-read-only nil))
2952              (while (search-forward "\r" nil t)
2953                (delete-char -1)
2954                (insert "\n"))
2955              (set-buffer-modified-p mod-p)
2956              ))))
2957   (setq selective-display nil))
2958
2959 (add-hook 'change-major-mode-hook 'nuke-selective-display)
2960
2961 (defconst overwrite-mode-textual (purecopy " Ovwrt")
2962   "The string displayed in the mode line when in overwrite mode.")
2963 (defconst overwrite-mode-binary (purecopy " Bin Ovwrt")
2964   "The string displayed in the mode line when in binary overwrite mode.")
2965
2966 (defun overwrite-mode (arg)
2967   "Toggle overwrite mode.
2968 With arg, turn overwrite mode on iff arg is positive.
2969 In overwrite mode, printing characters typed in replace existing text
2970 on a one-for-one basis, rather than pushing it to the right.  At the
2971 end of a line, such characters extend the line.  Before a tab,
2972 such characters insert until the tab is filled in.
2973 \\[quoted-insert] still inserts characters in overwrite mode; this
2974 is supposed to make it easier to insert characters when necessary."
2975   (interactive "P")
2976   (setq overwrite-mode
2977         (if (if (null arg) (not overwrite-mode)
2978               (> (prefix-numeric-value arg) 0))
2979             'overwrite-mode-textual))
2980   (redraw-modeline))
2981
2982 (defun binary-overwrite-mode (arg)
2983   "Toggle binary overwrite mode.
2984 With arg, turn binary overwrite mode on iff arg is positive.
2985 In binary overwrite mode, printing characters typed in replace
2986 existing text.  Newlines are not treated specially, so typing at the
2987 end of a line joins the line to the next, with the typed character
2988 between them.  Typing before a tab character simply replaces the tab
2989 with the character typed.
2990 \\[quoted-insert] replaces the text at the cursor, just as ordinary
2991 typing characters do.
2992
2993 Note that binary overwrite mode is not its own minor mode; it is a
2994 specialization of overwrite-mode, entered by setting the
2995 `overwrite-mode' variable to `overwrite-mode-binary'."
2996   (interactive "P")
2997   (setq overwrite-mode
2998         (if (if (null arg)
2999                 (not (eq overwrite-mode 'overwrite-mode-binary))
3000               (> (prefix-numeric-value arg) 0))
3001             'overwrite-mode-binary))
3002   (redraw-modeline))
3003 \f
3004 (defcustom line-number-mode nil
3005   "*Non-nil means display line number in modeline."
3006   :type 'boolean
3007   :group 'editing-basics)
3008
3009 (defun line-number-mode (arg)
3010   "Toggle Line Number mode.
3011 With arg, turn Line Number mode on iff arg is positive.
3012 When Line Number mode is enabled, the line number appears
3013 in the mode line."
3014   (interactive "P")
3015   (setq line-number-mode
3016         (if (null arg) (not line-number-mode)
3017           (> (prefix-numeric-value arg) 0)))
3018   (redraw-modeline))
3019
3020 (defcustom column-number-mode nil
3021   "*Non-nil means display column number in mode line."
3022   :type 'boolean
3023   :group 'editing-basics)
3024
3025 (defun column-number-mode (arg)
3026   "Toggle Column Number mode.
3027 With arg, turn Column Number mode on iff arg is positive.
3028 When Column Number mode is enabled, the column number appears
3029 in the mode line."
3030   (interactive "P")
3031   (setq column-number-mode
3032         (if (null arg) (not column-number-mode)
3033           (> (prefix-numeric-value arg) 0)))
3034   (redraw-modeline))
3035
3036 \f
3037 (defcustom blink-matching-paren t
3038   "*Non-nil means show matching open-paren when close-paren is inserted."
3039   :type 'boolean
3040   :group 'paren-blinking)
3041
3042 (defcustom blink-matching-paren-on-screen t
3043   "*Non-nil means show matching open-paren when it is on screen.
3044 nil means don't show it (but the open-paren can still be shown
3045 when it is off screen."
3046   :type 'boolean
3047   :group 'paren-blinking)
3048
3049 (defcustom blink-matching-paren-distance 12000
3050   "*If non-nil, is maximum distance to search for matching open-paren."
3051   :type '(choice integer (const nil))
3052   :group 'paren-blinking)
3053
3054 (defcustom blink-matching-delay 1
3055   "*The number of seconds that `blink-matching-open' will delay at a match."
3056   :type 'number
3057   :group 'paren-blinking)
3058
3059 (defcustom blink-matching-paren-dont-ignore-comments nil
3060   "*Non-nil means `blink-matching-paren' should not ignore comments."
3061   :type 'boolean
3062   :group 'paren-blinking)
3063
3064 (defun blink-matching-open ()
3065   "Move cursor momentarily to the beginning of the sexp before point."
3066   (interactive "_") ; XEmacs
3067   (and (> (point) (1+ (point-min)))
3068        blink-matching-paren
3069        ;; Verify an even number of quoting characters precede the close.
3070        (= 1 (logand 1 (- (point)
3071                          (save-excursion
3072                            (forward-char -1)
3073                            (skip-syntax-backward "/\\")
3074                            (point)))))
3075        (let* ((oldpos (point))
3076               (blinkpos)
3077               (mismatch))
3078          (save-excursion
3079            (save-restriction
3080              (if blink-matching-paren-distance
3081                  (narrow-to-region (max (point-min)
3082                                         (- (point) blink-matching-paren-distance))
3083                                    oldpos))
3084              (condition-case ()
3085                  (let ((parse-sexp-ignore-comments
3086                         (and parse-sexp-ignore-comments
3087                              (not blink-matching-paren-dont-ignore-comments))))
3088                    (setq blinkpos (scan-sexps oldpos -1)))
3089                (error nil)))
3090            (and blinkpos
3091                 (/= (char-syntax (char-after blinkpos))
3092                     ?\$)
3093                 (setq mismatch
3094                       (or (null (matching-paren (char-after blinkpos)))
3095                           (/= (char-after (1- oldpos))
3096                               (matching-paren (char-after blinkpos))))))
3097            (if mismatch (setq blinkpos nil))
3098            (if blinkpos
3099                (progn
3100                 (goto-char blinkpos)
3101                 (if (pos-visible-in-window-p)
3102                     (and blink-matching-paren-on-screen
3103                          (progn
3104                            (auto-show-make-point-visible)
3105                            (sit-for blink-matching-delay)))
3106                   (goto-char blinkpos)
3107                   (lmessage 'command "Matches %s"
3108                     ;; Show what precedes the open in its line, if anything.
3109                     (if (save-excursion
3110                           (skip-chars-backward " \t")
3111                           (not (bolp)))
3112                         (buffer-substring (progn (beginning-of-line) (point))
3113                                           (1+ blinkpos))
3114                       ;; Show what follows the open in its line, if anything.
3115                       (if (save-excursion
3116                             (forward-char 1)
3117                             (skip-chars-forward " \t")
3118                             (not (eolp)))
3119                           (buffer-substring blinkpos
3120                                             (progn (end-of-line) (point)))
3121                         ;; Otherwise show the previous nonblank line,
3122                         ;; if there is one.
3123                         (if (save-excursion
3124                               (skip-chars-backward "\n \t")
3125                               (not (bobp)))
3126                             (concat
3127                              (buffer-substring (progn
3128                                                  (skip-chars-backward "\n \t")
3129                                                  (beginning-of-line)
3130                                                  (point))
3131                                                (progn (end-of-line)
3132                                                       (skip-chars-backward " \t")
3133                                                       (point)))
3134                              ;; Replace the newline and other whitespace with `...'.
3135                              "..."
3136                              (buffer-substring blinkpos (1+ blinkpos)))
3137                           ;; There is nothing to show except the char itself.
3138                           (buffer-substring blinkpos (1+ blinkpos))))))))
3139              (cond (mismatch
3140                     (display-message 'no-log "Mismatched parentheses"))
3141                    ((not blink-matching-paren-distance)
3142                     (display-message 'no-log "Unmatched parenthesis"))))))))
3143
3144 ;Turned off because it makes dbx bomb out.
3145 (setq blink-paren-function 'blink-matching-open)
3146 \f
3147 (eval-when-compile (defvar myhelp))     ; suppress compiler warning
3148
3149 ;; XEmacs: Some functions moved to cmdloop.el:
3150 ;; keyboard-quit
3151 ;; buffer-quit-function
3152 ;; keyboard-escape-quit
3153
3154 (defun assoc-ignore-case (key alist)
3155   "Like `assoc', but assumes KEY is a string and ignores case when comparing."
3156   (setq key (downcase key))
3157   (let (element)
3158     (while (and alist (not element))
3159       (if (equal key (downcase (car (car alist))))
3160           (setq element (car alist)))
3161       (setq alist (cdr alist)))
3162     element))
3163
3164 \f
3165 (defcustom mail-user-agent 'sendmail-user-agent
3166   "*Your preference for a mail composition package.
3167 Various Emacs Lisp packages (e.g. reporter) require you to compose an
3168 outgoing email message.  This variable lets you specify which
3169 mail-sending package you prefer.
3170
3171 Valid values include:
3172
3173     sendmail-user-agent -- use the default Emacs Mail package
3174     mh-e-user-agent     -- use the Emacs interface to the MH mail system
3175     message-user-agent  -- use the GNUS mail sending package
3176
3177 Additional valid symbols may be available; check with the author of
3178 your package for details."
3179   :type '(radio (function-item :tag "Default Emacs mail"
3180                                :format "%t\n"
3181                                sendmail-user-agent)
3182                 (function-item :tag "Gnus mail sending package"
3183                                :format "%t\n"
3184                                message-user-agent)
3185                 (function :tag "Other"))
3186   :group 'mail)
3187
3188 (defun define-mail-user-agent (symbol composefunc sendfunc
3189                                       &optional abortfunc hookvar)
3190   "Define a symbol to identify a mail-sending package for `mail-user-agent'.
3191
3192 SYMBOL can be any Lisp symbol.  Its function definition and/or
3193 value as a variable do not matter for this usage; we use only certain
3194 properties on its property list, to encode the rest of the arguments.
3195
3196 COMPOSEFUNC is program callable function that composes an outgoing
3197 mail message buffer.  This function should set up the basics of the
3198 buffer without requiring user interaction.  It should populate the
3199 standard mail headers, leaving the `to:' and `subject:' headers blank
3200 by default.
3201
3202 COMPOSEFUNC should accept several optional arguments--the same
3203 arguments that `compose-mail' takes.  See that function's documentation.
3204
3205 SENDFUNC is the command a user would run to send the message.
3206
3207 Optional ABORTFUNC is the command a user would run to abort the
3208 message.  For mail packages that don't have a separate abort function,
3209 this can be `kill-buffer' (the equivalent of omitting this argument).
3210
3211 Optional HOOKVAR is a hook variable that gets run before the message
3212 is actually sent.  Callers that use the `mail-user-agent' may
3213 install a hook function temporarily on this hook variable.
3214 If HOOKVAR is nil, `mail-send-hook' is used.
3215
3216 The properties used on SYMBOL are `composefunc', `sendfunc',
3217 `abortfunc', and `hookvar'."
3218   (put symbol 'composefunc composefunc)
3219   (put symbol 'sendfunc sendfunc)
3220   (put symbol 'abortfunc (or abortfunc 'kill-buffer))
3221   (put symbol 'hookvar (or hookvar 'mail-send-hook)))
3222
3223 (define-mail-user-agent 'sendmail-user-agent
3224   'sendmail-user-agent-compose 'mail-send-and-exit)
3225
3226 (define-mail-user-agent 'message-user-agent
3227   'message-mail 'message-send-and-exit
3228   'message-kill-buffer 'message-send-hook)
3229
3230 (defun sendmail-user-agent-compose (&optional to subject other-headers continue
3231                                               switch-function yank-action
3232                                               send-actions)
3233   (if switch-function
3234       (let ((special-display-buffer-names nil)
3235             (special-display-regexps nil)
3236             (same-window-buffer-names nil)
3237             (same-window-regexps nil))
3238         (funcall switch-function "*mail*")))
3239   (let ((cc (cdr (assoc-ignore-case "cc" other-headers)))
3240         (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))))
3241     (or (mail continue to subject in-reply-to cc yank-action send-actions)
3242         continue
3243         (error "Message aborted"))
3244     (save-excursion
3245       (goto-char (point-min))
3246       (search-forward mail-header-separator)
3247       (beginning-of-line)
3248       (while other-headers
3249         (if (not (member (car (car other-headers)) '("in-reply-to" "cc")))
3250             (insert (car (car other-headers)) ": "
3251                     (cdr (car other-headers)) "\n"))
3252         (setq other-headers (cdr other-headers)))
3253       t)))
3254
3255 (define-mail-user-agent 'mh-e-user-agent
3256   'mh-user-agent-compose 'mh-send-letter 'mh-fully-kill-draft
3257   'mh-before-send-letter-hook)
3258
3259 (defun compose-mail (&optional to subject other-headers continue
3260                                switch-function yank-action send-actions)
3261   "Start composing a mail message to send.
3262 This uses the user's chosen mail composition package
3263 as selected with the variable `mail-user-agent'.
3264 The optional arguments TO and SUBJECT specify recipients
3265 and the initial Subject field, respectively.
3266
3267 OTHER-HEADERS is an alist specifying additional
3268 header fields.  Elements look like (HEADER . VALUE) where both
3269 HEADER and VALUE are strings.
3270
3271 CONTINUE, if non-nil, says to continue editing a message already
3272 being composed.
3273
3274 SWITCH-FUNCTION, if non-nil, is a function to use to
3275 switch to and display the buffer used for mail composition.
3276
3277 YANK-ACTION, if non-nil, is an action to perform, if and when necessary,
3278 to insert the raw text of the message being replied to.
3279 It has the form (FUNCTION . ARGS).  The user agent will apply
3280 FUNCTION to ARGS, to insert the raw text of the original message.
3281 \(The user agent will also run `mail-citation-hook', *after* the
3282 original text has been inserted in this way.)
3283
3284 SEND-ACTIONS is a list of actions to call when the message is sent.
3285 Each action has the form (FUNCTION . ARGS)."
3286   (interactive
3287    (list nil nil nil current-prefix-arg))
3288   (let ((function (get mail-user-agent 'composefunc)))
3289     (funcall function to subject other-headers continue
3290              switch-function yank-action send-actions)))
3291
3292 (defun compose-mail-other-window (&optional to subject other-headers continue
3293                                             yank-action send-actions)
3294   "Like \\[compose-mail], but edit the outgoing message in another window."
3295   (interactive
3296    (list nil nil nil current-prefix-arg))
3297   (compose-mail to subject other-headers continue
3298                 'switch-to-buffer-other-window yank-action send-actions))
3299
3300
3301 (defun compose-mail-other-frame (&optional to subject other-headers continue
3302                                             yank-action send-actions)
3303   "Like \\[compose-mail], but edit the outgoing message in another frame."
3304   (interactive
3305    (list nil nil nil current-prefix-arg))
3306   (compose-mail to subject other-headers continue
3307                 'switch-to-buffer-other-frame yank-action send-actions))
3308
3309 \f
3310 (defun set-variable (var val)
3311   "Set VARIABLE to VALUE.  VALUE is a Lisp object.
3312 When using this interactively, supply a Lisp expression for VALUE.
3313 If you want VALUE to be a string, you must surround it with doublequotes.
3314 If VARIABLE is a specifier, VALUE is added to it as an instantiator in
3315 the 'global locale with nil tag set (see `set-specifier').
3316
3317 If VARIABLE has a `variable-interactive' property, that is used as if
3318 it were the arg to `interactive' (which see) to interactively read the value."
3319   (interactive
3320    (let* ((var (read-variable "Set variable: "))
3321           ;; #### - yucky code replication here.  This should use something
3322           ;; from help.el or hyper-apropos.el
3323           (minibuffer-help-form
3324            '(funcall myhelp))
3325           (myhelp
3326            #'(lambda ()
3327               (with-output-to-temp-buffer "*Help*"
3328                 (prin1 var)
3329                 (princ "\nDocumentation:\n")
3330                 (princ (substring (documentation-property var 'variable-documentation)
3331                                   1))
3332                 (if (boundp var)
3333                     (let ((print-length 20))
3334                       (princ "\n\nCurrent value: ")
3335                       (prin1 (symbol-value var))))
3336                 (save-excursion
3337                   (set-buffer standard-output)
3338                   (help-mode))
3339                 nil))))
3340      (list var
3341            (let ((prop (get var 'variable-interactive)))
3342              (if prop
3343                  ;; Use VAR's `variable-interactive' property
3344                  ;; as an interactive spec for prompting.
3345                  (call-interactively (list 'lambda '(arg)
3346                                            (list 'interactive prop)
3347                                            'arg))
3348                (eval-minibuffer (format "Set %s to value: " var)))))))
3349   (if (and (boundp var) (specifierp (symbol-value var)))
3350       (set-specifier (symbol-value var) val)
3351     (set var val)))
3352 \f
3353 ;; XEmacs
3354 (defun activate-region ()
3355   "Activate the region, if `zmacs-regions' is true.
3356 Setting `zmacs-regions' to true causes LISPM-style active regions to be used.
3357 This function has no effect if `zmacs-regions' is false."
3358   (interactive)
3359   (and zmacs-regions (zmacs-activate-region)))
3360
3361 ;; XEmacs
3362 (defsubst region-exists-p ()
3363   "Return t if the region exists.
3364 If active regions are in use (i.e. `zmacs-regions' is true), this means that
3365  the region is active.  Otherwise, this means that the user has pushed
3366  a mark in this buffer at some point in the past.
3367 The functions `region-beginning' and `region-end' can be used to find the
3368  limits of the region."
3369   (not (null (mark))))
3370
3371 ;; XEmacs
3372 (defun region-active-p ()
3373   "Return non-nil if the region is active.
3374 If `zmacs-regions' is true, this is equivalent to `region-exists-p'.
3375 Otherwise, this function always returns false."
3376   (and zmacs-regions zmacs-region-extent))
3377
3378 ;; A bunch of stuff was moved elsewhere:
3379 ;; completion-list-mode-map
3380 ;; completion-reference-buffer
3381 ;; completion-base-size
3382 ;; delete-completion-window
3383 ;; previous-completion
3384 ;; next-completion
3385 ;; choose-completion
3386 ;; choose-completion-delete-max-match
3387 ;; choose-completion-string
3388 ;; completion-list-mode
3389 ;; completion-fixup-function
3390 ;; completion-setup-function
3391 ;; switch-to-completions
3392 ;; event stuffs
3393 ;; keypad stuffs
3394
3395 ;; The rest of this file is not in Lisp in FSF
3396 (defun capitalize-region-or-word (arg)
3397   "Capitalize the selected region or the following word (or ARG words)."
3398   (interactive "p")
3399   (if (region-active-p)
3400       (capitalize-region (region-beginning) (region-end))
3401     (capitalize-word arg)))
3402
3403 (defun upcase-region-or-word (arg)
3404   "Upcase the selected region or the following word (or ARG words)."
3405   (interactive "p")
3406   (if (region-active-p)
3407       (upcase-region (region-beginning) (region-end))
3408     (upcase-word arg)))
3409
3410 (defun downcase-region-or-word (arg)
3411   "Downcase the selected region or the following word (or ARG words)."
3412   (interactive "p")
3413   (if (region-active-p)
3414       (downcase-region (region-beginning) (region-end))
3415     (downcase-word arg)))
3416
3417 ;; Most of the zmacs code is now in elisp.  The only thing left in C
3418 ;; are the variables zmacs-regions, zmacs-region-active-p and
3419 ;; zmacs-region-stays plus the function zmacs_update_region which
3420 ;; simply calls the lisp level zmacs-update-region.  It must remain
3421 ;; for convenience, since it is called by core C code.
3422
3423 (defvar zmacs-activate-region-hook nil
3424   "Function or functions called when the region becomes active;
3425 see the variable `zmacs-regions'.")
3426
3427 (defvar zmacs-deactivate-region-hook nil
3428   "Function or functions called when the region becomes inactive;
3429 see the variable `zmacs-regions'.")
3430
3431 (defvar zmacs-update-region-hook nil
3432   "Function or functions called when the active region changes.
3433 This is called after each command that sets `zmacs-region-stays' to t.
3434 See the variable `zmacs-regions'.")
3435
3436 (defvar zmacs-region-extent nil
3437   "The extent of the zmacs region; don't use this.")
3438
3439 (defvar zmacs-region-rectangular-p nil
3440   "Whether the zmacs region is a rectangle; don't use this.")
3441
3442 (defun zmacs-make-extent-for-region (region)
3443   ;; Given a region, this makes an extent in the buffer which holds that
3444   ;; region, for highlighting purposes.  If the region isn't associated
3445   ;; with a buffer, this does nothing.
3446   (let ((buffer nil)
3447         (valid (and (extentp zmacs-region-extent)
3448                     (extent-object zmacs-region-extent)
3449                     (buffer-live-p (extent-object zmacs-region-extent))))
3450         start end)
3451     (cond ((consp region)
3452            (setq start (min (car region) (cdr region))
3453                  end (max (car region) (cdr region))
3454                  valid (and valid
3455                             (eq (marker-buffer (car region))
3456                                 (extent-object zmacs-region-extent)))
3457                  buffer (marker-buffer (car region))))
3458           (t
3459            (signal 'error (list "Invalid region" region))))
3460
3461     (if valid
3462         nil
3463       ;; The condition case is in case any of the extents are dead or
3464       ;; otherwise incapacitated.
3465       (condition-case ()
3466           (if (listp zmacs-region-extent)
3467               (mapc 'delete-extent zmacs-region-extent)
3468             (delete-extent zmacs-region-extent))
3469         (error nil)))
3470
3471     (if valid
3472         (set-extent-endpoints zmacs-region-extent start end)
3473       (setq zmacs-region-extent (make-extent start end buffer))
3474
3475       ;; Make the extent be closed on the right, which means that if
3476       ;; characters are inserted exactly at the end of the extent, the
3477       ;; extent will grow to cover them.  This is important for shell
3478       ;; buffers - suppose one makes a region, and one end is at point-max.
3479       ;; If the shell produces output, that marker will remain at point-max
3480       ;; (its position will increase).  So it's important that the extent
3481       ;; exhibit the same behavior, lest the region covered by the extent
3482       ;; (the visual indication), and the region between point and mark
3483       ;; (the actual region value) become different!
3484       (set-extent-property zmacs-region-extent 'end-open nil)
3485
3486       ;; use same priority as mouse-highlighting so that conflicts between
3487       ;; the region extent and a mouse-highlighted extent are resolved by
3488       ;; the usual size-and-endpoint-comparison method.
3489       (set-extent-priority zmacs-region-extent mouse-highlight-priority)
3490       (set-extent-face zmacs-region-extent 'zmacs-region)
3491
3492       ;; #### It might be better to actually break
3493       ;; default-mouse-track-next-move-rect out of mouse.el so that we
3494       ;; can use its logic here.
3495       (cond
3496        (zmacs-region-rectangular-p
3497         (setq zmacs-region-extent (list zmacs-region-extent))
3498         (default-mouse-track-next-move-rect start end zmacs-region-extent)
3499         ))
3500
3501       zmacs-region-extent)))
3502
3503 (defun zmacs-region-buffer ()
3504   "Return the buffer containing the zmacs region, or nil."
3505   ;; #### this is horrible and kludgy!  This stuff needs to be rethought.
3506   (and zmacs-regions zmacs-region-active-p
3507        (or (marker-buffer (mark-marker t))
3508            (and (extent-live-p zmacs-region-extent)
3509                 (buffer-live-p (extent-object zmacs-region-extent))
3510                 (extent-object zmacs-region-extent)))))
3511
3512 (defun zmacs-activate-region ()
3513   "Make the region between `point' and `mark' be active (highlighted),
3514 if `zmacs-regions' is true.  Only a very small number of commands
3515 should ever do this.  Calling this function will call the hook
3516 `zmacs-activate-region-hook', if the region was previously inactive.
3517 Calling this function ensures that the region stays active after the
3518 current command terminates, even if `zmacs-region-stays' is not set.
3519 Returns t if the region was activated (i.e. if `zmacs-regions' if t)."
3520   (if (not zmacs-regions)
3521       nil
3522     (setq zmacs-region-active-p t
3523           zmacs-region-stays t
3524           zmacs-region-rectangular-p (and (boundp 'mouse-track-rectangle-p)
3525                                           mouse-track-rectangle-p))
3526     (if (marker-buffer (mark-marker t))
3527         (zmacs-make-extent-for-region (cons (point-marker t) (mark-marker t))))
3528     (run-hooks 'zmacs-activate-region-hook)
3529     t))
3530
3531 (defun zmacs-deactivate-region ()
3532   "Make the region between `point' and `mark' no longer be active,
3533 if `zmacs-regions' is true.  You shouldn't need to call this; the
3534 command loop calls it when appropriate.  Calling this function will
3535 call the hook `zmacs-deactivate-region-hook', if the region was
3536 previously active.  Returns t if the region had been active, nil
3537 otherwise."
3538   (if (not zmacs-region-active-p)
3539       nil
3540     (setq zmacs-region-active-p nil
3541           zmacs-region-stays nil
3542           zmacs-region-rectangular-p nil)
3543     (if zmacs-region-extent
3544         (let ((inhibit-quit t))
3545           (if (listp zmacs-region-extent)
3546               (mapc 'delete-extent zmacs-region-extent)
3547             (delete-extent zmacs-region-extent))
3548           (setq zmacs-region-extent nil)))
3549     (run-hooks 'zmacs-deactivate-region-hook)
3550     t))
3551
3552 (defun zmacs-update-region ()
3553   "Update the highlighted region between `point' and `mark'.
3554 You shouldn't need to call this; the command loop calls it
3555 when appropriate.  Calling this function will call the hook
3556 `zmacs-update-region-hook', if the region is active."
3557   (when zmacs-region-active-p
3558     (when (marker-buffer (mark-marker t))
3559       (zmacs-make-extent-for-region (cons (point-marker t)
3560                                           (mark-marker t))))
3561     (run-hooks 'zmacs-update-region-hook)))
3562
3563 ;;;;;;
3564 ;;;;;; echo area stuff
3565 ;;;;;;
3566
3567 ;;; #### Should this be moved to a separate file, for clarity?
3568 ;;; -hniksic
3569
3570 ;;; The `message-stack' is an alist of labels with messages; the first
3571 ;;; message in this list is always in the echo area.  A call to
3572 ;;; `display-message' inserts a label/message pair at the head of the
3573 ;;; list, and removes any other pairs with that label.  Calling
3574 ;;; `clear-message' causes any pair with matching label to be removed,
3575 ;;; and this may cause the displayed message to change or vanish.  If
3576 ;;; the label arg is nil, the entire message stack is cleared.
3577 ;;;
3578 ;;; Message/error filtering will be a little tricker to implement than
3579 ;;; logging, since messages can be built up incrementally
3580 ;;; using clear-message followed by repeated calls to append-message
3581 ;;; (this happens with error messages).  For messages which aren't
3582 ;;; created this way, filtering could be implemented at display-message
3583 ;;; very easily.
3584 ;;;
3585 ;;; Bits of the logging code are borrowed from log-messages.el by
3586 ;;; Robert Potter (rpotter@grip.cis.upenn.edu).
3587
3588 ;; need this to terminate the currently-displayed message
3589 ;; ("Loading simple ...")
3590 (when (and
3591        (not (fboundp 'display-message))
3592        (not (featurep 'debug)))
3593   (send-string-to-terminal "\n"))
3594
3595 (defvar message-stack nil
3596   "An alist of label/string pairs representing active echo-area messages.
3597 The first element in the list is currently displayed in the echo area.
3598 Do not modify this directly--use the `message' or
3599 `display-message'/`clear-message' functions.")
3600
3601 (defvar remove-message-hook 'log-message
3602   "A function or list of functions to be called when a message is removed
3603 from the echo area at the bottom of the frame.  The label of the removed
3604 message is passed as the first argument, and the text of the message
3605 as the second argument.")
3606
3607 (defcustom log-message-max-size 50000
3608   "Maximum size of the \" *Message-Log*\" buffer.  See `log-message'."
3609   :type 'integer
3610   :group 'log-message)
3611 (make-compatible-variable 'message-log-max 'log-message-max-size)
3612
3613 ;; We used to reject quite a lot of stuff here, but it was a bad idea,
3614 ;; for two reasons:
3615 ;;
3616 ;; a) In most circumstances, you *want* to see the message in the log.
3617 ;;    The explicitly non-loggable messages should be marked as such by
3618 ;;    the issuer.  Gratuitous non-displaying of random regexps made
3619 ;;    debugging harder, too (because various reasonable debugging
3620 ;;    messages would get eaten).
3621 ;;
3622 ;; b) It slowed things down.  Yes, visibly.
3623 ;;
3624 ;; So, I left only a few of the really useless ones on this kill-list.
3625 ;;
3626 ;;                                            --hniksic
3627 (defcustom log-message-ignore-regexps
3628   '(;; Note: adding entries to this list slows down messaging
3629     ;; significantly.  Wherever possible, use message lables.
3630
3631     ;; Often-seen messages
3632     "\\`\\'"                            ; empty message
3633     "\\`\\(Beginning\\|End\\) of buffer\\'"
3634     ;;"^Quit$"
3635     ;; completions
3636     ;; Many packages print this -- impossible to categorize
3637     ;;"^Making completion list"
3638     ;; Gnus
3639     ;; "^No news is no news$"
3640     ;; "^No more\\( unread\\)? newsgroups$"
3641     ;; "^Opening [^ ]+ server\\.\\.\\."
3642     ;; "^[^:]+: Reading incoming mail"
3643     ;; "^Getting mail from "
3644     ;; "^\\(Generating Summary\\|Sorting threads\\|Making sparse threads\\|Scoring\\|Checking new news\\|Expiring articles\\|Sending\\)\\.\\.\\."
3645     ;; "^\\(Fetching headers for\\|Retrieving newsgroup\\|Reading active file\\)"
3646     ;; "^No more\\( unread\\)? articles"
3647     ;; "^Deleting article "
3648     ;; W3
3649     ;; "^Parsed [0-9]+ of [0-9]+ ([0-9]+%)"
3650     )
3651   "List of regular expressions matching messages which shouldn't be logged.
3652 See `log-message'.
3653
3654 Ideally, packages which generate messages which might need to be ignored
3655 should label them with 'progress, 'prompt, or 'no-log, so they can be
3656 filtered by the log-message-ignore-labels."
3657   :type '(repeat regexp)
3658   :group 'log-message)
3659
3660 (defcustom log-message-ignore-labels
3661   '(help-echo command progress prompt no-log garbage-collecting auto-saving)
3662   "List of symbols indicating labels of messages which shouldn't be logged.
3663 See `display-message' for some common labels.  See also `log-message'."
3664   :type '(repeat (symbol :tag "Label"))
3665   :group 'log-message)
3666
3667 ;;Subsumed by view-lossage
3668 ;; Not really, I'm adding it back by popular demand. -slb
3669 (defun show-message-log ()
3670   "Show the \" *Message-Log*\" buffer, which contains old messages and errors."
3671   (interactive)
3672   (pop-to-buffer (get-buffer-create " *Message-Log*")))
3673
3674 (defvar log-message-filter-function 'log-message-filter
3675   "Value must be a function of two arguments: a symbol (label) and
3676 a string (message).  It should return non-nil to indicate a message
3677 should be logged.  Possible values include 'log-message-filter and
3678 'log-message-filter-errors-only.")
3679
3680 (defun log-message-filter (label message)
3681   "Default value of `log-message-filter-function'.
3682 Messages whose text matches one of the `log-message-ignore-regexps'
3683 or whose label appears in `log-message-ignore-labels' are not saved."
3684   (let ((r  log-message-ignore-regexps)
3685         (ok (not (memq label log-message-ignore-labels))))
3686     (save-match-data
3687       (while (and r ok)
3688         (when (string-match (car r) message)
3689           (setq ok nil))
3690         (setq r (cdr r))))
3691     ok))
3692
3693 (defun log-message-filter-errors-only (label message)
3694   "For use as the `log-message-filter-function'.  Only logs error messages."
3695   (eq label 'error))
3696
3697 (defun log-message (label message)
3698   "Stuff a copy of the message into the \" *Message-Log*\" buffer,
3699 if it satisfies the `log-message-filter-function'.
3700
3701 For use on `remove-message-hook'."
3702   (when (and (not noninteractive)
3703              (funcall log-message-filter-function label message))
3704     ;; Use save-excursion rather than save-current-buffer because we
3705     ;; change the value of point.
3706     (save-excursion
3707       (set-buffer (get-buffer-create " *Message-Log*"))
3708       (goto-char (point-max))
3709       ;(insert (concat (upcase (symbol-name label)) ": "  message "\n"))
3710       (let (extent)
3711         ;; Mark multiline message with an extent, which `view-lossage'
3712         ;; will recognize.
3713         (when (string-match "\n" message)
3714           (setq extent (make-extent (point) (point)))
3715           (set-extent-properties extent '(end-open nil message-multiline t)))
3716         (insert message "\n")
3717         (when extent
3718           (set-extent-property extent 'end-open t)))
3719       (when (> (point-max) (max log-message-max-size (point-min)))
3720         ;; Trim log to ~90% of max size.
3721         (goto-char (max (- (point-max)
3722                            (truncate (* 0.9 log-message-max-size)))
3723                         (point-min)))
3724         (forward-line 1)
3725         (delete-region (point-min) (point))))))
3726
3727 (defun message-displayed-p (&optional return-string frame)
3728   "Return a non-nil value if a message is presently displayed in the\n\
3729 minibuffer's echo area.  If optional argument RETURN-STRING is non-nil,\n\
3730 return a string containing the message, otherwise just return t."
3731   ;; by definition, a message is displayed if the echo area buffer is
3732   ;; non-empty (see also echo_area_active()).  It had better also
3733   ;; be the case that message-stack is nil exactly when the echo area
3734   ;; is non-empty.
3735   (let ((buffer (get-buffer " *Echo Area*")))
3736     (and (< (point-min buffer) (point-max buffer))
3737          (if return-string
3738              (buffer-substring nil nil buffer)
3739            t))))
3740
3741 ;;; Returns the string which remains in the echo area, or nil if none.
3742 ;;; If label is nil, the whole message stack is cleared.
3743 (defun clear-message (&optional label frame stdout-p no-restore)
3744   "Remove any message with the given LABEL from the message-stack,
3745 erasing it from the echo area if it's currently displayed there.
3746 If a message remains at the head of the message-stack and NO-RESTORE
3747 is nil, it will be displayed.  The string which remains in the echo
3748 area will be returned, or nil if the message-stack is now empty.
3749 If LABEL is nil, the entire message-stack is cleared.
3750
3751 Unless you need the return value or you need to specify a label,
3752 you should just use (message nil)."
3753   (or frame (setq frame (selected-frame)))
3754   (let ((clear-stream (and message-stack (eq 'stream (frame-type frame)))))
3755     (remove-message label frame)
3756     (let ((inhibit-read-only t)
3757           (zmacs-region-stays zmacs-region-stays)) ; preserve from change
3758       (erase-buffer " *Echo Area*"))
3759     (if clear-stream
3760         (send-string-to-terminal ?\n stdout-p))
3761     (if no-restore
3762         nil                     ; just preparing to put another msg up
3763       (if message-stack
3764           (let ((oldmsg (cdr (car message-stack))))
3765             (raw-append-message oldmsg frame stdout-p)
3766             oldmsg)
3767         ;; #### Should we (redisplay-echo-area) here?  Messes some
3768         ;; things up.
3769         nil))))
3770
3771 (defun remove-message (&optional label frame)
3772   ;; If label is nil, we want to remove all matching messages.
3773   ;; Must reverse the stack first to log them in the right order.
3774   (let ((log nil))
3775     (while (and message-stack
3776                 (or (null label)        ; null label means clear whole stack
3777                     (eq label (car (car message-stack)))))
3778       (push (car message-stack) log)
3779       (setq message-stack (cdr message-stack)))
3780     (let ((s  message-stack))
3781       (while (cdr s)
3782         (let ((msg (car (cdr s))))
3783           (if (eq label (car msg))
3784               (progn
3785                 (push msg log)
3786                 (setcdr s (cdr (cdr s))))
3787             (setq s (cdr s))))))
3788     ;; (possibly) log each removed message
3789     (while log
3790       (condition-case e
3791           (run-hook-with-args 'remove-message-hook
3792                               (car (car log)) (cdr (car log)))
3793         (error (setq remove-message-hook nil)
3794                (lwarn 'message-log 'warning
3795                  "Error caught in `remove-message-hook': %s"
3796                  (error-message-string e))
3797                (let ((inhibit-read-only t))
3798                  (erase-buffer " *Echo Area*"))
3799                (signal (car e) (cdr e))))
3800       (setq log (cdr log)))))
3801
3802 (defun append-message (label message &optional frame stdout-p)
3803   (or frame (setq frame (selected-frame)))
3804   ;; Add a new entry to the message-stack, or modify an existing one
3805   (let ((top (car message-stack)))
3806     (if (eq label (car top))
3807         (setcdr top (concat (cdr top) message))
3808       (push (cons label message) message-stack)))
3809   (raw-append-message message frame stdout-p))
3810
3811 ;; Really append the message to the echo area.  no fiddling with
3812 ;; message-stack.
3813 (defun raw-append-message (message &optional frame stdout-p)
3814   (unless (equal message "")
3815     (let ((inhibit-read-only t)
3816           (zmacs-region-stays zmacs-region-stays)) ; preserve from change
3817       (insert-string message " *Echo Area*")
3818       ;; Conditionalizing on the device type in this way is not that clean,
3819       ;; but neither is having a device method, as I originally implemented
3820       ;; it: all non-stream devices behave in the same way.  Perhaps
3821       ;; the cleanest way is to make the concept of a "redisplayable"
3822       ;; device, which stream devices are not.  Look into this more if
3823       ;; we ever create another non-redisplayable device type (e.g.
3824       ;; processes?  printers?).
3825
3826       ;; Don't redisplay the echo area if we are executing a macro.
3827       (if (not executing-kbd-macro)
3828           (if (eq 'stream (frame-type frame))
3829               (send-string-to-terminal message stdout-p (frame-device frame))
3830             (redisplay-echo-area))))))
3831
3832 (defun display-message (label message &optional frame stdout-p)
3833   "Print a one-line message at the bottom of the frame.  First argument
3834 LABEL is an identifier for this message.  MESSAGE is the string to display.
3835 Use `clear-message' to remove a labelled message.
3836
3837 Here are some standard labels (those marked with `*' are not logged
3838 by default--see the `log-message-ignore-labels' variable):
3839     message       default label used by the `message' function
3840     error         default label used for reporting errors
3841   * progress      progress indicators like \"Converting... 45%\"
3842   * prompt        prompt-like messages like \"I-search: foo\"
3843   * command       helper command messages like \"Mark set\"
3844   * no-log        messages that should never be logged"
3845   (clear-message label frame stdout-p t)
3846   (append-message label message frame stdout-p))
3847
3848 (defun current-message (&optional frame)
3849   "Return the current message in the echo area, or nil.
3850 The FRAME argument is currently unused."
3851   (cdr (car message-stack)))
3852
3853 ;;; may eventually be frame-dependent
3854 (defun current-message-label (&optional frame)
3855   (car (car message-stack)))
3856
3857 (defun message (fmt &rest args)
3858   "Print a one-line message at the bottom of the frame.
3859 The arguments are the same as to `format'.
3860
3861 If the only argument is nil, clear any existing message; let the
3862 minibuffer contents show."
3863   ;; questionable junk in the C code
3864   ;; (if (framep default-minibuffer-frame)
3865   ;;     (make-frame-visible default-minibuffer-frame))
3866   (if (and (null fmt) (null args))
3867       (prog1 nil
3868         (clear-message nil))
3869     (let ((str (apply 'format fmt args)))
3870       (display-message 'message str)
3871       str)))
3872
3873 (defun lmessage (label fmt &rest args)
3874   "Print a one-line message at the bottom of the frame.
3875 First argument LABEL is an identifier for this message.  The rest of the
3876 arguments are the same as to `format'.
3877
3878 See `display-message' for a list of standard labels."
3879   (if (and (null fmt) (null args))
3880       (prog1 nil
3881         (clear-message label nil))
3882     (let ((str (apply 'format fmt args)))
3883       (display-message label str)
3884       str)))
3885
3886
3887 ;;;;;;
3888 ;;;;;; warning stuff
3889 ;;;;;;
3890
3891 (defcustom log-warning-minimum-level 'info
3892   "Minimum level of warnings that should be logged.
3893 The warnings in levels below this are completely ignored, as if they never
3894 happened.
3895
3896 The recognized warning levels, in decreasing order of priority, are
3897 'emergency, 'alert, 'critical, 'error, 'warning, 'notice, 'info, and
3898 'debug.
3899
3900 See also `display-warning-minimum-level'.
3901
3902 You can also control which warnings are displayed on a class-by-class
3903 basis.  See `display-warning-suppressed-classes' and
3904 `log-warning-suppressed-classes'."
3905   :type '(choice (const emergency) (const alert) (const critical)
3906                  (const error) (const warning) (const notice)
3907                  (const info) (const debug))
3908   :group 'warnings)
3909
3910 (defcustom display-warning-minimum-level 'info
3911   "Minimum level of warnings that should be displayed.
3912 The warnings in levels below this will be generated, but not
3913 displayed.
3914
3915 The recognized warning levels, in decreasing order of priority, are
3916 'emergency, 'alert, 'critical, 'error, 'warning, 'notice, 'info, and
3917 'debug.
3918
3919 See also `log-warning-minimum-level'.
3920
3921 You can also control which warnings are displayed on a class-by-class
3922 basis.  See `display-warning-suppressed-classes' and
3923 `log-warning-suppressed-classes'."
3924   :type '(choice (const emergency) (const alert) (const critical)
3925                  (const error) (const warning) (const notice)
3926                  (const info) (const debug))
3927   :group 'warnings)
3928
3929 (defvar log-warning-suppressed-classes nil
3930   "List of classes of warnings that shouldn't be logged or displayed.
3931 If any of the CLASS symbols associated with a warning is the same as
3932 any of the symbols listed here, the warning will be completely ignored,
3933 as it they never happened.
3934
3935 NOTE: In most circumstances, you should *not* set this variable.
3936 Set `display-warning-suppressed-classes' instead.  That way the suppressed
3937 warnings are not displayed but are still unobtrusively logged.
3938
3939 See also `log-warning-minimum-level' and `display-warning-minimum-level'.")
3940
3941 (defcustom display-warning-suppressed-classes nil
3942   "List of classes of warnings that shouldn't be displayed.
3943 If any of the CLASS symbols associated with a warning is the same as
3944 any of the symbols listed here, the warning will not be displayed.
3945 The warning will still logged in the *Warnings* buffer (unless also
3946 contained in `log-warning-suppressed-classes'), but the buffer will
3947 not be automatically popped up.
3948
3949 See also `log-warning-minimum-level' and `display-warning-minimum-level'."
3950   :type '(repeat symbol)
3951   :group 'warnings)
3952
3953 (defvar warning-count 0
3954   "Count of the number of warning messages displayed so far.")
3955
3956 (defconst warning-level-alist '((emergency . 8)
3957                                 (alert . 7)
3958                                 (critical . 6)
3959                                 (error . 5)
3960                                 (warning . 4)
3961                                 (notice . 3)
3962                                 (info . 2)
3963                                 (debug . 1)))
3964
3965 (defun warning-level-p (level)
3966   "Non-nil if LEVEL specifies a warning level."
3967   (and (symbolp level) (assq level warning-level-alist)))
3968
3969 ;; If you're interested in rewriting this function, be aware that it
3970 ;; could be called at arbitrary points in a Lisp program (when a
3971 ;; built-in function wants to issue a warning, it will call out to
3972 ;; this function the next time some Lisp code is evaluated).  Therefore,
3973 ;; this function *must* not permanently modify any global variables
3974 ;; (e.g. the current buffer) except those that specifically apply
3975 ;; to the warning system.
3976
3977 (defvar before-init-deferred-warnings nil)
3978
3979 (defun after-init-display-warnings ()
3980   "Display warnings deferred till after the init file is run.
3981 Warnings that occur before then are deferred so that warning
3982 suppression in the .emacs file will be honored."
3983   (while before-init-deferred-warnings
3984     (apply 'display-warning (car before-init-deferred-warnings))
3985     (setq before-init-deferred-warnings
3986           (cdr before-init-deferred-warnings))))
3987
3988 (add-hook 'after-init-hook 'after-init-display-warnings)
3989
3990 (defun display-warning (class message &optional level)
3991   "Display a warning message.
3992 CLASS should be a symbol describing what sort of warning this is, such
3993 as `resource' or `key-mapping'.  A list of such symbols is also
3994 accepted. (Individual classes can be suppressed; see
3995 `display-warning-suppressed-classes'.)  Optional argument LEVEL can
3996 be used to specify a priority for the warning, other than default priority
3997 `warning'. (See `display-warning-minimum-level').  The message is
3998 inserted into the *Warnings* buffer, which is made visible at appropriate
3999 times."
4000   (or level (setq level 'warning))
4001   (or (listp class) (setq class (list class)))
4002   (check-argument-type 'warning-level-p level)
4003   (if (and (not (featurep 'infodock))
4004            (not init-file-loaded))
4005       (push (list class message level) before-init-deferred-warnings)
4006     (catch 'ignored
4007       (let ((display-p t)
4008             (level-num (cdr (assq level warning-level-alist))))
4009         (if (< level-num (cdr (assq log-warning-minimum-level
4010                                     warning-level-alist)))
4011             (throw 'ignored nil))
4012         (if (intersection class log-warning-suppressed-classes)
4013             (throw 'ignored nil))
4014
4015         (if (< level-num (cdr (assq display-warning-minimum-level
4016                                     warning-level-alist)))
4017             (setq display-p nil))
4018         (if (and display-p
4019                  (intersection class display-warning-suppressed-classes))
4020             (setq display-p nil))
4021         (let ((buffer (get-buffer-create "*Warnings*")))
4022           (when display-p
4023             ;; The C code looks at display-warning-tick to determine
4024             ;; when it should call `display-warning-buffer'.  Change it
4025             ;; to get the C code's attention.
4026             (incf display-warning-tick))
4027           (with-current-buffer buffer
4028             (goto-char (point-max))
4029             (incf warning-count)
4030             (princ (format "(%d) (%s/%s) "
4031                            warning-count
4032                            (mapconcat 'symbol-name class ",")
4033                            level)
4034                    buffer)
4035             (princ message buffer)
4036             (terpri buffer)
4037             (terpri buffer)))))))
4038
4039 (defun warn (&rest args)
4040   "Display a warning message.
4041 The message is constructed by passing all args to `format'.  The message
4042 is placed in the *Warnings* buffer, which will be popped up at the next
4043 redisplay.  The class of the warning is `warning'.  See also
4044 `display-warning'."
4045   (display-warning 'warning (apply 'format args)))
4046
4047 (defun lwarn (class level &rest args)
4048   "Display a labeled warning message.
4049 CLASS should be a symbol describing what sort of warning this is, such
4050 as `resource' or `key-mapping'.  A list of such symbols is also
4051 accepted. (Individual classes can be suppressed; see
4052 `display-warning-suppressed-classes'.)  If non-nil, LEVEL can be used
4053 to specify a priority for the warning, other than default priority
4054 `warning'. (See `display-warning-minimum-level').  The message is
4055 inserted into the *Warnings* buffer, which is made visible at appropriate
4056 times.
4057
4058 The rest of the arguments are passed to `format'."
4059   (display-warning class (apply 'format args)
4060                    (or level 'warning)))
4061
4062 (defvar warning-marker nil)
4063
4064 ;; When this function is called by the C code, all non-local exits are
4065 ;; trapped and C-g is inhibited; therefore, it would be a very, very
4066 ;; bad idea for this function to get into an infinite loop.
4067
4068 (defun display-warning-buffer ()
4069   "Make the buffer that contains the warnings be visible.
4070 The C code calls this periodically, right before redisplay."
4071   (let ((buffer (get-buffer-create "*Warnings*")))
4072     (when (or (not warning-marker)
4073               (not (eq (marker-buffer warning-marker) buffer)))
4074       (setq warning-marker (make-marker))
4075       (set-marker warning-marker 1 buffer))
4076     (if temp-buffer-show-function
4077         (let ((show-buffer (get-buffer-create "*Warnings-Show*")))
4078           (save-excursion
4079             (set-buffer show-buffer)
4080             (setq buffer-read-only nil)
4081             (erase-buffer))
4082           (save-excursion
4083             (set-buffer buffer)
4084             (copy-to-buffer show-buffer
4085                             (marker-position warning-marker)
4086                             (point-max)))
4087           (funcall temp-buffer-show-function show-buffer))
4088       (set-window-start (display-buffer buffer) warning-marker))
4089     (set-marker warning-marker (point-max buffer) buffer)))
4090
4091 (defun emacs-name ()
4092   "Return the printable name of this instance of Emacs."
4093   (cond ((featurep 'infodock) "InfoDock")
4094         ((featurep 'xemacs) "XEmacs")
4095         (t "Emacs")))
4096
4097 ;;; simple.el ends here