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