Commit | Line | Data |
---|---|---|
c88ab9ce ER |
1 | ;;; simple.el --- basic editing commands for Emacs |
2 | ||
da7d231b KS |
3 | ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 99, |
4 | ;; 2000, 2001, 2002, 2003 | |
69c1dd37 | 5 | ;; Free Software Foundation, Inc. |
2076c87c | 6 | |
30764597 PJ |
7 | ;; Maintainer: FSF |
8 | ;; Keywords: internal | |
9 | ||
2076c87c JB |
10 | ;; This file is part of GNU Emacs. |
11 | ||
12 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
3a801d0c | 14 | ;; the Free Software Foundation; either version 2, or (at your option) |
2076c87c JB |
15 | ;; any later version. |
16 | ||
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
2076c87c | 26 | |
d9ecc911 ER |
27 | ;;; Commentary: |
28 | ||
29 | ;; A grab-bag of basic Emacs commands not specifically related to some | |
30 | ;; major mode or to file-handling. | |
31 | ||
3a801d0c | 32 | ;;; Code: |
2076c87c | 33 | |
d01a33cf | 34 | (eval-when-compile |
a339f909 | 35 | (autoload 'widget-convert "wid-edit") |
cc76a58c | 36 | (autoload 'shell-mode "shell")) |
d01a33cf GM |
37 | |
38 | ||
69c1dd37 RS |
39 | (defgroup killing nil |
40 | "Killing and yanking commands" | |
41 | :group 'editing) | |
42 | ||
69c1dd37 RS |
43 | (defgroup paren-matching nil |
44 | "Highlight (un)matching of parens and expressions." | |
69c1dd37 RS |
45 | :group 'matching) |
46 | ||
f54b0d85 RS |
47 | (define-key global-map [?\C-x right] 'next-buffer) |
48 | (define-key global-map [?\C-x left] 'prev-buffer) | |
49 | (defun next-buffer () | |
50 | "Switch to the next buffer in cyclic order." | |
51 | (interactive) | |
52 | (let ((buffer (current-buffer))) | |
53 | (switch-to-buffer (other-buffer buffer)) | |
54 | (bury-buffer buffer))) | |
55 | ||
56 | (defun prev-buffer () | |
57 | "Switch to the previous buffer in cyclic order." | |
58 | (interactive) | |
59 | (let ((list (nreverse (buffer-list))) | |
60 | found) | |
61 | (while (and (not found) list) | |
62 | (let ((buffer (car list))) | |
63 | (if (and (not (get-buffer-window buffer)) | |
64 | (not (string-match "\\` " (buffer-name buffer)))) | |
65 | (setq found buffer))) | |
66 | (setq list (cdr list))) | |
67 | (switch-to-buffer found))) | |
69c1dd37 | 68 | |
93be67de KH |
69 | (defun fundamental-mode () |
70 | "Major mode not specialized for anything in particular. | |
71 | Other major modes are defined by comparison with this one." | |
72 | (interactive) | |
73 | (kill-all-local-variables)) | |
eaae8106 | 74 | |
93be67de KH |
75 | ;; Making and deleting lines. |
76 | ||
30bb9754 | 77 | (defun newline (&optional arg) |
d133d835 | 78 | "Insert a newline, and move to left margin of the new line if it's blank. |
058d4999 DL |
79 | If `use-hard-newlines' is non-nil, the newline is marked with the |
80 | text-property `hard'. | |
76c64e24 | 81 | With ARG, insert that many newlines. |
058d4999 DL |
82 | Call `auto-fill-function' if the current column number is greater |
83 | than the value of `fill-column' and ARG is `nil'." | |
30bb9754 | 84 | (interactive "*P") |
4c4cbf11 | 85 | (barf-if-buffer-read-only) |
30bb9754 BG |
86 | ;; Inserting a newline at the end of a line produces better redisplay in |
87 | ;; try_window_id than inserting at the beginning of a line, and the textual | |
88 | ;; result is the same. So, if we're at beginning of line, pretend to be at | |
89 | ;; the end of the previous line. | |
1e722f9f | 90 | (let ((flag (and (not (bobp)) |
30bb9754 | 91 | (bolp) |
1cd24721 RS |
92 | ;; Make sure no functions want to be told about |
93 | ;; the range of the changes. | |
1cd24721 RS |
94 | (not after-change-functions) |
95 | (not before-change-functions) | |
fd977703 RS |
96 | ;; Make sure there are no markers here. |
97 | (not (buffer-has-markers-at (1- (point)))) | |
2f047f6c | 98 | (not (buffer-has-markers-at (point))) |
1cd24721 RS |
99 | ;; Make sure no text properties want to know |
100 | ;; where the change was. | |
101 | (not (get-char-property (1- (point)) 'modification-hooks)) | |
102 | (not (get-char-property (1- (point)) 'insert-behind-hooks)) | |
103 | (or (eobp) | |
104 | (not (get-char-property (point) 'insert-in-front-hooks))) | |
31a5333f MB |
105 | ;; Make sure the newline before point isn't intangible. |
106 | (not (get-char-property (1- (point)) 'intangible)) | |
107 | ;; Make sure the newline before point isn't read-only. | |
108 | (not (get-char-property (1- (point)) 'read-only)) | |
109 | ;; Make sure the newline before point isn't invisible. | |
110 | (not (get-char-property (1- (point)) 'invisible)) | |
111 | ;; Make sure the newline before point has the same | |
112 | ;; properties as the char before it (if any). | |
1e722f9f | 113 | (< (or (previous-property-change (point)) -2) |
d133d835 RS |
114 | (- (point) 2)))) |
115 | (was-page-start (and (bolp) | |
116 | (looking-at page-delimiter))) | |
117 | (beforepos (point))) | |
30bb9754 BG |
118 | (if flag (backward-char 1)) |
119 | ;; Call self-insert so that auto-fill, abbrev expansion etc. happens. | |
120 | ;; Set last-command-char to tell self-insert what to insert. | |
121 | (let ((last-command-char ?\n) | |
122 | ;; Don't auto-fill if we have a numeric argument. | |
3954fff9 RS |
123 | ;; Also not if flag is true (it would fill wrong line); |
124 | ;; there is no need to since we're at BOL. | |
125 | (auto-fill-function (if (or arg flag) nil auto-fill-function))) | |
4cc9d0dc RS |
126 | (unwind-protect |
127 | (self-insert-command (prefix-numeric-value arg)) | |
128 | ;; If we get an error in self-insert-command, put point at right place. | |
129 | (if flag (forward-char 1)))) | |
2f047f6c KH |
130 | ;; Even if we did *not* get an error, keep that forward-char; |
131 | ;; all further processing should apply to the newline that the user | |
132 | ;; thinks he inserted. | |
133 | ||
30bb9754 BG |
134 | ;; Mark the newline(s) `hard'. |
135 | (if use-hard-newlines | |
2f047f6c | 136 | (set-hard-newline-properties |
55741b46 | 137 | (- (point) (if arg (prefix-numeric-value arg) 1)) (point))) |
d133d835 RS |
138 | ;; If the newline leaves the previous line blank, |
139 | ;; and we have a left margin, delete that from the blank line. | |
140 | (or flag | |
141 | (save-excursion | |
142 | (goto-char beforepos) | |
143 | (beginning-of-line) | |
144 | (and (looking-at "[ \t]$") | |
145 | (> (current-left-margin) 0) | |
146 | (delete-region (point) (progn (end-of-line) (point)))))) | |
d133d835 RS |
147 | ;; Indent the line after the newline, except in one case: |
148 | ;; when we added the newline at the beginning of a line | |
149 | ;; which starts a page. | |
150 | (or was-page-start | |
151 | (move-to-left-margin nil t))) | |
30bb9754 BG |
152 | nil) |
153 | ||
55741b46 RS |
154 | (defun set-hard-newline-properties (from to) |
155 | (let ((sticky (get-text-property from 'rear-nonsticky))) | |
156 | (put-text-property from to 'hard 't) | |
157 | ;; If rear-nonsticky is not "t", add 'hard to rear-nonsticky list | |
158 | (if (and (listp sticky) (not (memq 'hard sticky))) | |
159 | (put-text-property from (point) 'rear-nonsticky | |
160 | (cons 'hard sticky))))) | |
eaae8106 | 161 | |
2076c87c | 162 | (defun open-line (arg) |
ff1fbe3e | 163 | "Insert a newline and leave point before it. |
3db1e3b5 | 164 | If there is a fill prefix and/or a left-margin, insert them on the new line |
d133d835 | 165 | if the line would have been blank. |
616ed245 | 166 | With arg N, insert N newlines." |
2076c87c | 167 | (interactive "*p") |
616ed245 | 168 | (let* ((do-fill-prefix (and fill-prefix (bolp))) |
3db1e3b5 | 169 | (do-left-margin (and (bolp) (> (current-left-margin) 0))) |
207d7545 GM |
170 | (loc (point)) |
171 | ;; Don't expand an abbrev before point. | |
172 | (abbrev-mode nil)) | |
d133d835 RS |
173 | (newline arg) |
174 | (goto-char loc) | |
28191e20 | 175 | (while (> arg 0) |
d133d835 RS |
176 | (cond ((bolp) |
177 | (if do-left-margin (indent-to (current-left-margin))) | |
178 | (if do-fill-prefix (insert-and-inherit fill-prefix)))) | |
179 | (forward-line 1) | |
28191e20 | 180 | (setq arg (1- arg))) |
d133d835 RS |
181 | (goto-char loc) |
182 | (end-of-line))) | |
2076c87c | 183 | |
da7d231b KS |
184 | |
185 | (defun split-line (&optional arg) | |
186 | "Split current line, moving portion beyond point vertically down. | |
187 | If the current line starts with `fill-prefix', insert it on the new | |
188 | line as well. With prefix arg, don't insert fill-prefix on new line. | |
189 | ||
190 | When called from Lisp code, the arg may be a prefix string to copy." | |
191 | (interactive "*P") | |
2076c87c JB |
192 | (skip-chars-forward " \t") |
193 | (let ((col (current-column)) | |
da7d231b KS |
194 | (pos (point)) |
195 | (beg (line-beginning-position)) | |
196 | (prefix (cond ((stringp arg) arg) | |
197 | (arg nil) | |
198 | (t fill-prefix)))) | |
28191e20 | 199 | (newline 1) |
da7d231b KS |
200 | (if (and (stringp prefix) |
201 | (string-equal prefix | |
202 | (buffer-substring beg (+ beg (length prefix))))) | |
203 | (insert-and-inherit prefix)) | |
2076c87c JB |
204 | (indent-to col 0) |
205 | (goto-char pos))) | |
206 | ||
2076c87c JB |
207 | (defun delete-indentation (&optional arg) |
208 | "Join this line to previous and fix up whitespace at join. | |
ccc58657 | 209 | If there is a fill prefix, delete it from the beginning of this line. |
2076c87c JB |
210 | With argument, join this line to following line." |
211 | (interactive "*P") | |
212 | (beginning-of-line) | |
213 | (if arg (forward-line 1)) | |
214 | (if (eq (preceding-char) ?\n) | |
215 | (progn | |
216 | (delete-region (point) (1- (point))) | |
ccc58657 RS |
217 | ;; If the second line started with the fill prefix, |
218 | ;; delete the prefix. | |
219 | (if (and fill-prefix | |
01b8e020 | 220 | (<= (+ (point) (length fill-prefix)) (point-max)) |
ccc58657 RS |
221 | (string= fill-prefix |
222 | (buffer-substring (point) | |
223 | (+ (point) (length fill-prefix))))) | |
224 | (delete-region (point) (+ (point) (length fill-prefix)))) | |
2076c87c JB |
225 | (fixup-whitespace)))) |
226 | ||
fc025090 | 227 | (defalias 'join-line #'delete-indentation) ; easier to find |
eaae8106 | 228 | |
2076c87c JB |
229 | (defun delete-blank-lines () |
230 | "On blank line, delete all surrounding blank lines, leaving just one. | |
231 | On isolated blank line, delete that one. | |
6d30d416 | 232 | On nonblank line, delete any immediately following blank lines." |
2076c87c JB |
233 | (interactive "*") |
234 | (let (thisblank singleblank) | |
235 | (save-excursion | |
236 | (beginning-of-line) | |
237 | (setq thisblank (looking-at "[ \t]*$")) | |
70e14c01 | 238 | ;; Set singleblank if there is just one blank line here. |
2076c87c JB |
239 | (setq singleblank |
240 | (and thisblank | |
241 | (not (looking-at "[ \t]*\n[ \t]*$")) | |
242 | (or (bobp) | |
243 | (progn (forward-line -1) | |
244 | (not (looking-at "[ \t]*$"))))))) | |
70e14c01 | 245 | ;; Delete preceding blank lines, and this one too if it's the only one. |
2076c87c JB |
246 | (if thisblank |
247 | (progn | |
248 | (beginning-of-line) | |
249 | (if singleblank (forward-line 1)) | |
250 | (delete-region (point) | |
251 | (if (re-search-backward "[^ \t\n]" nil t) | |
252 | (progn (forward-line 1) (point)) | |
253 | (point-min))))) | |
70e14c01 JB |
254 | ;; Delete following blank lines, unless the current line is blank |
255 | ;; and there are no following blank lines. | |
2076c87c JB |
256 | (if (not (and thisblank singleblank)) |
257 | (save-excursion | |
258 | (end-of-line) | |
259 | (forward-line 1) | |
260 | (delete-region (point) | |
261 | (if (re-search-forward "[^ \t\n]" nil t) | |
262 | (progn (beginning-of-line) (point)) | |
70e14c01 JB |
263 | (point-max))))) |
264 | ;; Handle the special case where point is followed by newline and eob. | |
265 | ;; Delete the line, leaving point at eob. | |
266 | (if (looking-at "^[ \t]*\n\\'") | |
267 | (delete-region (point) (point-max))))) | |
2076c87c | 268 | |
eaae8106 SS |
269 | (defun delete-trailing-whitespace () |
270 | "Delete all the trailing whitespace across the current buffer. | |
271 | All whitespace after the last non-whitespace character in a line is deleted. | |
103db06c RS |
272 | This respects narrowing, created by \\[narrow-to-region] and friends. |
273 | A formfeed is not considered whitespace by this function." | |
eaae8106 SS |
274 | (interactive "*") |
275 | (save-match-data | |
276 | (save-excursion | |
277 | (goto-char (point-min)) | |
5c9b3fac MB |
278 | (while (re-search-forward "\\s-$" nil t) |
279 | (skip-syntax-backward "-" (save-excursion (forward-line 0) (point))) | |
3a768251 | 280 | ;; Don't delete formfeeds, even if they are considered whitespace. |
661aa5c7 GM |
281 | (save-match-data |
282 | (if (looking-at ".*\f") | |
283 | (goto-char (match-end 0)))) | |
7981d89f | 284 | (delete-region (point) (match-end 0)))))) |
eaae8106 | 285 | |
2076c87c JB |
286 | (defun newline-and-indent () |
287 | "Insert a newline, then indent according to major mode. | |
ff1fbe3e | 288 | Indentation is done using the value of `indent-line-function'. |
2076c87c | 289 | In programming language modes, this is the same as TAB. |
ff1fbe3e | 290 | In some text modes, where TAB inserts a tab, this command indents to the |
eed5698b | 291 | column specified by the function `current-left-margin'." |
2076c87c | 292 | (interactive "*") |
5ff4ba3d | 293 | (delete-horizontal-space t) |
46947372 | 294 | (newline) |
2076c87c JB |
295 | (indent-according-to-mode)) |
296 | ||
297 | (defun reindent-then-newline-and-indent () | |
298 | "Reindent current line, insert newline, then indent the new line. | |
299 | Indentation of both lines is done according to the current major mode, | |
ff1fbe3e | 300 | which means calling the current value of `indent-line-function'. |
2076c87c JB |
301 | In programming language modes, this is the same as TAB. |
302 | In some text modes, where TAB inserts a tab, this indents to the | |
eed5698b | 303 | column specified by the function `current-left-margin'." |
2076c87c | 304 | (interactive "*") |
e1e04350 SM |
305 | (delete-horizontal-space t) |
306 | (let ((pos (point))) | |
307 | ;; Be careful to insert the newline before indenting the line. | |
308 | ;; Otherwise, the indentation might be wrong. | |
309 | (newline) | |
310 | (save-excursion | |
311 | (goto-char pos) | |
312 | (indent-according-to-mode)) | |
313 | (indent-according-to-mode))) | |
eaae8106 | 314 | |
93be67de KH |
315 | (defun quoted-insert (arg) |
316 | "Read next input character and insert it. | |
317 | This is useful for inserting control characters. | |
2076c87c | 318 | |
93be67de KH |
319 | If the first character you type after this command is an octal digit, |
320 | you should type a sequence of octal digits which specify a character code. | |
321 | Any nondigit terminates the sequence. If the terminator is a RET, | |
322 | it is discarded; any other terminator is used itself as input. | |
323 | The variable `read-quoted-char-radix' specifies the radix for this feature; | |
324 | set it to 10 or 16 to use decimal or hex instead of octal. | |
dff7d67f | 325 | |
93be67de KH |
326 | In overwrite mode, this function inserts the character anyway, and |
327 | does not handle octal digits specially. This means that if you use | |
328 | overwrite as your normal editing mode, you can use this function to | |
329 | insert characters when necessary. | |
dff7d67f | 330 | |
93be67de KH |
331 | In binary overwrite mode, this function does overwrite, and octal |
332 | digits are interpreted as a character code. This is intended to be | |
333 | useful for editing binary files." | |
334 | (interactive "*p") | |
c2f51635 | 335 | (let* ((char (let (translation-table-for-input) |
940fe486 DL |
336 | (if (or (not overwrite-mode) |
337 | (eq overwrite-mode 'overwrite-mode-binary)) | |
338 | (read-quoted-char) | |
339 | (read-char))))) | |
93be67de KH |
340 | ;; Assume character codes 0240 - 0377 stand for characters in some |
341 | ;; single-byte character set, and convert them to Emacs | |
342 | ;; characters. | |
343 | (if (and enable-multibyte-characters | |
344 | (>= char ?\240) | |
345 | (<= char ?\377)) | |
346 | (setq char (unibyte-char-to-multibyte char))) | |
347 | (if (> arg 0) | |
348 | (if (eq overwrite-mode 'overwrite-mode-binary) | |
349 | (delete-char arg))) | |
350 | (while (> arg 0) | |
351 | (insert-and-inherit char) | |
352 | (setq arg (1- arg))))) | |
eaae8106 | 353 | |
93be67de KH |
354 | (defun forward-to-indentation (arg) |
355 | "Move forward ARG lines and position at first nonblank character." | |
356 | (interactive "p") | |
357 | (forward-line arg) | |
358 | (skip-chars-forward " \t")) | |
cc2b2b6c | 359 | |
93be67de KH |
360 | (defun backward-to-indentation (arg) |
361 | "Move backward ARG lines and position at first nonblank character." | |
362 | (interactive "p") | |
363 | (forward-line (- arg)) | |
364 | (skip-chars-forward " \t")) | |
2076c87c | 365 | |
93be67de KH |
366 | (defun back-to-indentation () |
367 | "Move point to the first non-whitespace character on this line." | |
368 | (interactive) | |
369 | (beginning-of-line 1) | |
370 | (skip-chars-forward " \t")) | |
371 | ||
372 | (defun fixup-whitespace () | |
373 | "Fixup white space between objects around point. | |
374 | Leave one space or none, according to the context." | |
375 | (interactive "*") | |
376 | (save-excursion | |
377 | (delete-horizontal-space) | |
378 | (if (or (looking-at "^\\|\\s)") | |
379 | (save-excursion (forward-char -1) | |
380 | (looking-at "$\\|\\s(\\|\\s'"))) | |
381 | nil | |
382 | (insert ?\ )))) | |
383 | ||
5ff4ba3d MB |
384 | (defun delete-horizontal-space (&optional backward-only) |
385 | "Delete all spaces and tabs around point. | |
386 | If BACKWARD-ONLY is non-nil, only delete spaces before point." | |
93be67de | 387 | (interactive "*") |
9ab59a1a MB |
388 | (let ((orig-pos (point))) |
389 | (delete-region | |
390 | (if backward-only | |
391 | orig-pos | |
392 | (progn | |
393 | (skip-chars-forward " \t") | |
394 | (constrain-to-field nil orig-pos t))) | |
5ff4ba3d | 395 | (progn |
9ab59a1a MB |
396 | (skip-chars-backward " \t") |
397 | (constrain-to-field nil orig-pos))))) | |
93be67de KH |
398 | |
399 | (defun just-one-space () | |
400 | "Delete all spaces and tabs around point, leaving one space." | |
401 | (interactive "*") | |
9ab59a1a MB |
402 | (let ((orig-pos (point))) |
403 | (skip-chars-backward " \t") | |
404 | (constrain-to-field nil orig-pos) | |
405 | (if (= (following-char) ? ) | |
406 | (forward-char 1) | |
407 | (insert ? )) | |
408 | (delete-region | |
409 | (point) | |
410 | (progn | |
411 | (skip-chars-forward " \t") | |
412 | (constrain-to-field nil orig-pos t))))) | |
2d88b556 | 413 | \f |
2076c87c JB |
414 | (defun beginning-of-buffer (&optional arg) |
415 | "Move point to the beginning of the buffer; leave mark at previous position. | |
c66587fe RS |
416 | With arg N, put point N/10 of the way from the beginning. |
417 | ||
418 | If the buffer is narrowed, this command uses the beginning and size | |
419 | of the accessible part of the buffer. | |
ff1fbe3e RS |
420 | |
421 | Don't use this command in Lisp programs! | |
2076c87c JB |
422 | \(goto-char (point-min)) is faster and avoids clobbering the mark." |
423 | (interactive "P") | |
424 | (push-mark) | |
c66587fe RS |
425 | (let ((size (- (point-max) (point-min)))) |
426 | (goto-char (if arg | |
427 | (+ (point-min) | |
428 | (if (> size 10000) | |
429 | ;; Avoid overflow for large buffer sizes! | |
430 | (* (prefix-numeric-value arg) | |
431 | (/ size 10)) | |
432 | (/ (+ 10 (* size (prefix-numeric-value arg))) 10))) | |
433 | (point-min)))) | |
2076c87c JB |
434 | (if arg (forward-line 1))) |
435 | ||
436 | (defun end-of-buffer (&optional arg) | |
437 | "Move point to the end of the buffer; leave mark at previous position. | |
c66587fe RS |
438 | With arg N, put point N/10 of the way from the end. |
439 | ||
440 | If the buffer is narrowed, this command uses the beginning and size | |
441 | of the accessible part of the buffer. | |
ff1fbe3e RS |
442 | |
443 | Don't use this command in Lisp programs! | |
2076c87c JB |
444 | \(goto-char (point-max)) is faster and avoids clobbering the mark." |
445 | (interactive "P") | |
446 | (push-mark) | |
c66587fe RS |
447 | (let ((size (- (point-max) (point-min)))) |
448 | (goto-char (if arg | |
449 | (- (point-max) | |
450 | (if (> size 10000) | |
451 | ;; Avoid overflow for large buffer sizes! | |
452 | (* (prefix-numeric-value arg) | |
453 | (/ size 10)) | |
454 | (/ (* size (prefix-numeric-value arg)) 10))) | |
455 | (point-max)))) | |
3a801d0c ER |
456 | ;; If we went to a place in the middle of the buffer, |
457 | ;; adjust it to the beginning of a line. | |
314808dc | 458 | (cond (arg (forward-line 1)) |
919f2812 | 459 | ((> (point) (window-end nil t)) |
314808dc GM |
460 | ;; If the end of the buffer is not already on the screen, |
461 | ;; then scroll specially to put it near, but not at, the bottom. | |
462 | (overlay-recenter (point)) | |
463 | (recenter -3)))) | |
2076c87c JB |
464 | |
465 | (defun mark-whole-buffer () | |
70e14c01 JB |
466 | "Put point at beginning and mark at end of buffer. |
467 | You probably should not use this function in Lisp programs; | |
468 | it is usually a mistake for a Lisp function to use any subroutine | |
469 | that uses or sets the mark." | |
2076c87c JB |
470 | (interactive) |
471 | (push-mark (point)) | |
fd0f4056 | 472 | (push-mark (point-max) nil t) |
2076c87c | 473 | (goto-char (point-min))) |
2d88b556 | 474 | \f |
eaae8106 | 475 | |
93be67de KH |
476 | ;; Counting lines, one way or another. |
477 | ||
478 | (defun goto-line (arg) | |
479 | "Goto line ARG, counting from line 1 at beginning of buffer." | |
480 | (interactive "NGoto line: ") | |
481 | (setq arg (prefix-numeric-value arg)) | |
482 | (save-restriction | |
483 | (widen) | |
484 | (goto-char 1) | |
485 | (if (eq selective-display t) | |
486 | (re-search-forward "[\n\C-m]" nil 'end (1- arg)) | |
487 | (forward-line (1- arg))))) | |
2076c87c JB |
488 | |
489 | (defun count-lines-region (start end) | |
eb8c3be9 | 490 | "Print number of lines and characters in the region." |
2076c87c JB |
491 | (interactive "r") |
492 | (message "Region has %d lines, %d characters" | |
493 | (count-lines start end) (- end start))) | |
494 | ||
495 | (defun what-line () | |
2578be76 | 496 | "Print the current buffer line number and narrowed line number of point." |
2076c87c | 497 | (interactive) |
2578be76 | 498 | (let ((opoint (point)) start) |
2076c87c | 499 | (save-excursion |
2578be76 RS |
500 | (save-restriction |
501 | (goto-char (point-min)) | |
502 | (widen) | |
4d251ae7 | 503 | (forward-line 0) |
2578be76 RS |
504 | (setq start (point)) |
505 | (goto-char opoint) | |
4d251ae7 | 506 | (forward-line 0) |
d99f8496 | 507 | (if (/= start (point-min)) |
2578be76 | 508 | (message "line %d (narrowed line %d)" |
d99f8496 | 509 | (1+ (count-lines (point-min) (point))) |
2578be76 | 510 | (1+ (count-lines start (point)))) |
d99f8496 | 511 | (message "Line %d" (1+ (count-lines (point-min) (point))))))))) |
2578be76 | 512 | |
2076c87c JB |
513 | (defun count-lines (start end) |
514 | "Return number of lines between START and END. | |
515 | This is usually the number of newlines between them, | |
ff1fbe3e | 516 | but can be one more if START is not equal to END |
2076c87c | 517 | and the greater of them is not at the start of a line." |
e406700d RS |
518 | (save-excursion |
519 | (save-restriction | |
520 | (narrow-to-region start end) | |
521 | (goto-char (point-min)) | |
522 | (if (eq selective-display t) | |
523 | (save-match-data | |
dde92ca6 RS |
524 | (let ((done 0)) |
525 | (while (re-search-forward "[\n\C-m]" nil t 40) | |
526 | (setq done (+ 40 done))) | |
527 | (while (re-search-forward "[\n\C-m]" nil t 1) | |
528 | (setq done (+ 1 done))) | |
043efc41 RS |
529 | (goto-char (point-max)) |
530 | (if (and (/= start end) | |
531 | (not (bolp))) | |
532 | (1+ done) | |
e406700d RS |
533 | done))) |
534 | (- (buffer-size) (forward-line (buffer-size))))))) | |
eaae8106 | 535 | |
d5d99b80 KH |
536 | (defun what-cursor-position (&optional detail) |
537 | "Print info on cursor position (on screen and within buffer). | |
e38dff0c | 538 | Also describe the character after point, and give its character code |
c6fcc518 KH |
539 | in octal, decimal and hex. |
540 | ||
541 | For a non-ASCII multibyte character, also give its encoding in the | |
542 | buffer's selected coding system if the coding system encodes the | |
543 | character safely. If the character is encoded into one byte, that | |
544 | code is shown in hex. If the character is encoded into more than one | |
545 | byte, just \"...\" is shown. | |
e5a902cf | 546 | |
24dad5d5 | 547 | In addition, with prefix argument, show details about that character |
0b69eec5 | 548 | in *Help* buffer. See also the command `describe-char'." |
d5d99b80 | 549 | (interactive "P") |
2076c87c JB |
550 | (let* ((char (following-char)) |
551 | (beg (point-min)) | |
552 | (end (point-max)) | |
553 | (pos (point)) | |
554 | (total (buffer-size)) | |
555 | (percent (if (> total 50000) | |
556 | ;; Avoid overflow from multiplying by 100! | |
557 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) | |
558 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) | |
559 | (hscroll (if (= (window-hscroll) 0) | |
560 | "" | |
561 | (format " Hscroll=%d" (window-hscroll)))) | |
562 | (col (current-column))) | |
563 | (if (= pos end) | |
564 | (if (or (/= beg 1) (/= end (1+ total))) | |
74fd2380 | 565 | (message "point=%d of %d (%d%%) <%d - %d> column %d %s" |
2076c87c | 566 | pos total percent beg end col hscroll) |
74fd2380 | 567 | (message "point=%d of %d (%d%%) column %d %s" |
2076c87c | 568 | pos total percent col hscroll)) |
c6fcc518 KH |
569 | (let ((coding buffer-file-coding-system) |
570 | encoded encoding-msg) | |
571 | (if (or (not coding) | |
572 | (eq (coding-system-type coding) t)) | |
573 | (setq coding default-buffer-file-coding-system)) | |
28fd4883 KH |
574 | (if (not (char-valid-p char)) |
575 | (setq encoding-msg | |
576 | (format "(0%o, %d, 0x%x, invalid)" char char char)) | |
577 | (setq encoded (and (>= char 128) (encode-coding-char char coding))) | |
578 | (setq encoding-msg | |
579 | (if encoded | |
24dad5d5 | 580 | (format "(0%o, %d, 0x%x, file %s)" |
28fd4883 | 581 | char char char |
24dad5d5 | 582 | (if (> (length encoded) 1) |
28fd4883 | 583 | "..." |
24dad5d5 | 584 | (encoded-string-description encoded coding))) |
28fd4883 | 585 | (format "(0%o, %d, 0x%x)" char char char)))) |
e5e89e48 | 586 | (if detail |
24dad5d5 | 587 | ;; We show the detailed information about CHAR. |
0b69eec5 | 588 | (describe-char (point))) |
24dad5d5 KH |
589 | (if (or (/= beg 1) (/= end (1+ total))) |
590 | (message "Char: %s %s point=%d of %d (%d%%) <%d - %d> column %d %s" | |
e5a902cf KH |
591 | (if (< char 256) |
592 | (single-key-description char) | |
f0d16a7f | 593 | (buffer-substring-no-properties (point) (1+ (point)))) |
24dad5d5 KH |
594 | encoding-msg pos total percent beg end col hscroll) |
595 | (message "Char: %s %s point=%d of %d (%d%%) column %d %s" | |
596 | (if (< char 256) | |
597 | (single-key-description char) | |
598 | (buffer-substring-no-properties (point) (1+ (point)))) | |
599 | encoding-msg pos total percent col hscroll)))))) | |
2d88b556 | 600 | \f |
76c64e24 SM |
601 | (defvar read-expression-map |
602 | (let ((m (make-sparse-keymap))) | |
603 | (define-key m "\M-\t" 'lisp-complete-symbol) | |
604 | (set-keymap-parent m minibuffer-local-map) | |
605 | m) | |
854c16c5 | 606 | "Minibuffer keymap used for reading Lisp expressions.") |
854c16c5 | 607 | |
8570b0ca RM |
608 | (defvar read-expression-history nil) |
609 | ||
b49df39d | 610 | (defcustom eval-expression-print-level 4 |
058d4999 | 611 | "*Value to use for `print-level' when printing value in `eval-expression'. |
d26b26dc | 612 | A value of nil means no limit." |
b49df39d | 613 | :group 'lisp |
058d4999 | 614 | :type '(choice (const :tag "No Limit" nil) integer) |
b49df39d RS |
615 | :version "21.1") |
616 | ||
617 | (defcustom eval-expression-print-length 12 | |
058d4999 | 618 | "*Value to use for `print-length' when printing value in `eval-expression'. |
d26b26dc | 619 | A value of nil means no limit." |
b49df39d | 620 | :group 'lisp |
058d4999 | 621 | :type '(choice (const :tag "No Limit" nil) integer) |
b49df39d RS |
622 | :version "21.1") |
623 | ||
624 | (defcustom eval-expression-debug-on-error t | |
ed8bcabe GM |
625 | "*Non-nil means set `debug-on-error' when evaluating in `eval-expression'. |
626 | If nil, don't change the value of `debug-on-error'." | |
b49df39d RS |
627 | :group 'lisp |
628 | :type 'boolean | |
629 | :version "21.1") | |
630 | ||
8570b0ca | 631 | ;; We define this, rather than making `eval' interactive, |
2076c87c | 632 | ;; for the sake of completion of names like eval-region, eval-current-buffer. |
ecb7ad00 RS |
633 | (defun eval-expression (eval-expression-arg |
634 | &optional eval-expression-insert-value) | |
a6a1ee53 EZ |
635 | "Evaluate EVAL-EXPRESSION-ARG and print value in the echo area. |
636 | Value is also consed on to front of the variable `values'. | |
637 | Optional argument EVAL-EXPRESSION-INSERT-VALUE, if non-nil, means | |
638 | insert the result into the current buffer instead of printing it in | |
639 | the echo area." | |
adca5fa6 | 640 | (interactive |
b387ef9a RS |
641 | (list (read-from-minibuffer "Eval: " |
642 | nil read-expression-map t | |
ecb7ad00 RS |
643 | 'read-expression-history) |
644 | current-prefix-arg)) | |
eaae8106 | 645 | |
ed8bcabe GM |
646 | (if (null eval-expression-debug-on-error) |
647 | (setq values (cons (eval eval-expression-arg) values)) | |
648 | (let ((old-value (make-symbol "t")) new-value) | |
649 | ;; Bind debug-on-error to something unique so that we can | |
650 | ;; detect when evaled code changes it. | |
651 | (let ((debug-on-error old-value)) | |
652 | (setq values (cons (eval eval-expression-arg) values)) | |
653 | (setq new-value debug-on-error)) | |
654 | ;; If evaled code has changed the value of debug-on-error, | |
655 | ;; propagate that change to the global binding. | |
656 | (unless (eq old-value new-value) | |
657 | (setq debug-on-error new-value)))) | |
eaae8106 | 658 | |
b49df39d RS |
659 | (let ((print-length eval-expression-print-length) |
660 | (print-level eval-expression-print-level)) | |
661 | (prin1 (car values) | |
662 | (if eval-expression-insert-value (current-buffer) t)))) | |
2076c87c JB |
663 | |
664 | (defun edit-and-eval-command (prompt command) | |
665 | "Prompting with PROMPT, let user edit COMMAND and eval result. | |
666 | COMMAND is a Lisp expression. Let user edit that expression in | |
667 | the minibuffer, then read and evaluate the result." | |
9f4b6084 MR |
668 | (let ((command |
669 | (unwind-protect | |
670 | (read-from-minibuffer prompt | |
671 | (prin1-to-string command) | |
672 | read-expression-map t | |
673 | '(command-history . 1)) | |
674 | ;; If command was added to command-history as a string, | |
675 | ;; get rid of that. We want only evaluable expressions there. | |
676 | (if (stringp (car command-history)) | |
677 | (setq command-history (cdr command-history)))))) | |
5d6c83ae KH |
678 | |
679 | ;; If command to be redone does not match front of history, | |
680 | ;; add it to the history. | |
681 | (or (equal command (car command-history)) | |
682 | (setq command-history (cons command command-history))) | |
2076c87c JB |
683 | (eval command))) |
684 | ||
ebb61177 | 685 | (defun repeat-complex-command (arg) |
2076c87c JB |
686 | "Edit and re-evaluate last complex command, or ARGth from last. |
687 | A complex command is one which used the minibuffer. | |
688 | The command is placed in the minibuffer as a Lisp form for editing. | |
689 | The result is executed, repeating the command as changed. | |
690 | If the command has been changed or is not the most recent previous command | |
691 | it is added to the front of the command history. | |
eb6e9899 RS |
692 | You can use the minibuffer history commands \\<minibuffer-local-map>\\[next-history-element] and \\[previous-history-element] |
693 | to get different commands to edit and resubmit." | |
2076c87c | 694 | (interactive "p") |
ba343182 | 695 | (let ((elt (nth (1- arg) command-history)) |
2076c87c JB |
696 | newcmd) |
697 | (if elt | |
854c16c5 | 698 | (progn |
eab22e27 | 699 | (setq newcmd |
74ae5fab RS |
700 | (let ((print-level nil) |
701 | (minibuffer-history-position arg) | |
99ea24de | 702 | (minibuffer-history-sexp-flag (1+ (minibuffer-depth)))) |
9f4b6084 MR |
703 | (unwind-protect |
704 | (read-from-minibuffer | |
705 | "Redo: " (prin1-to-string elt) read-expression-map t | |
706 | (cons 'command-history arg)) | |
707 | ||
708 | ;; If command was added to command-history as a | |
709 | ;; string, get rid of that. We want only | |
710 | ;; evaluable expressions there. | |
711 | (if (stringp (car command-history)) | |
712 | (setq command-history (cdr command-history)))))) | |
db16f109 RS |
713 | |
714 | ;; If command to be redone does not match front of history, | |
715 | ;; add it to the history. | |
716 | (or (equal newcmd (car command-history)) | |
717 | (setq command-history (cons newcmd command-history))) | |
2076c87c | 718 | (eval newcmd)) |
536b728a RS |
719 | (if command-history |
720 | (error "Argument %d is beyond length of command history" arg) | |
721 | (error "There are no previous complex commands to repeat"))))) | |
2d88b556 | 722 | \f |
854c16c5 RS |
723 | (defvar minibuffer-history nil |
724 | "Default minibuffer history list. | |
725 | This is used for all minibuffer input | |
726 | except when an alternate history list is specified.") | |
727 | (defvar minibuffer-history-sexp-flag nil | |
e02e6f75 | 728 | "Non-nil when doing history operations on the variable `command-history'. |
854c16c5 | 729 | More generally, indicates that the history list being acted on |
99ea24de KH |
730 | contains expressions rather than strings. |
731 | It is only valid if its value equals the current minibuffer depth, | |
732 | to handle recursive uses of the minibuffer.") | |
e91f80c4 RS |
733 | (setq minibuffer-history-variable 'minibuffer-history) |
734 | (setq minibuffer-history-position nil) | |
854c16c5 | 735 | (defvar minibuffer-history-search-history nil) |
e91f80c4 | 736 | |
93cee14b RS |
737 | (defvar minibuffer-text-before-history nil |
738 | "Text that was in this minibuffer before any history commands. | |
739 | This is nil if there have not yet been any history commands | |
740 | in this use of the minibuffer.") | |
741 | ||
742 | (add-hook 'minibuffer-setup-hook 'minibuffer-history-initialize) | |
743 | ||
744 | (defun minibuffer-history-initialize () | |
745 | (setq minibuffer-text-before-history nil)) | |
746 | ||
6e7d0ff7 MB |
747 | (defun minibuffer-avoid-prompt (new old) |
748 | "A point-motion hook for the minibuffer, that moves point out of the prompt." | |
749 | (constrain-to-field nil (point-max))) | |
750 | ||
6e30a99a RS |
751 | (defcustom minibuffer-history-case-insensitive-variables nil |
752 | "*Minibuffer history variables for which matching should ignore case. | |
753 | If a history variable is a member of this list, then the | |
754 | \\[previous-matching-history-element] and \\[next-matching-history-element]\ | |
755 | commands ignore case when searching it, regardless of `case-fold-search'." | |
756 | :type '(repeat variable) | |
757 | :group 'minibuffer) | |
758 | ||
e91f80c4 | 759 | (defun previous-matching-history-element (regexp n) |
854c16c5 RS |
760 | "Find the previous history element that matches REGEXP. |
761 | \(Previous history elements refer to earlier actions.) | |
762 | With prefix argument N, search for Nth previous match. | |
5c2010f0 | 763 | If N is negative, find the next or Nth next match. |
9889af08 EZ |
764 | Normally, history elements are matched case-insensitively if |
765 | `case-fold-search' is non-nil, but an uppercase letter in REGEXP | |
766 | makes the search case-sensitive. | |
6e30a99a | 767 | See also `minibuffer-history-case-insensitive-variables'." |
854c16c5 | 768 | (interactive |
c1172a19 | 769 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
770 | (regexp (read-from-minibuffer "Previous element matching (regexp): " |
771 | nil | |
772 | minibuffer-local-map | |
773 | nil | |
774 | 'minibuffer-history-search-history))) | |
775 | ;; Use the last regexp specified, by default, if input is empty. | |
776 | (list (if (string= regexp "") | |
a8e96cea KH |
777 | (if minibuffer-history-search-history |
778 | (car minibuffer-history-search-history) | |
779 | (error "No previous history search regexp")) | |
c1172a19 | 780 | regexp) |
854c16c5 | 781 | (prefix-numeric-value current-prefix-arg)))) |
e276a14a MB |
782 | (unless (zerop n) |
783 | (if (and (zerop minibuffer-history-position) | |
784 | (null minibuffer-text-before-history)) | |
efaac2e6 | 785 | (setq minibuffer-text-before-history |
6d74d713 | 786 | (minibuffer-contents-no-properties))) |
e276a14a MB |
787 | (let ((history (symbol-value minibuffer-history-variable)) |
788 | (case-fold-search | |
789 | (if (isearch-no-upper-case-p regexp t) ; assume isearch.el is dumped | |
790 | ;; On some systems, ignore case for file names. | |
791 | (if (memq minibuffer-history-variable | |
792 | minibuffer-history-case-insensitive-variables) | |
793 | t | |
794 | ;; Respect the user's setting for case-fold-search: | |
795 | case-fold-search) | |
796 | nil)) | |
797 | prevpos | |
798 | match-string | |
799 | match-offset | |
800 | (pos minibuffer-history-position)) | |
801 | (while (/= n 0) | |
802 | (setq prevpos pos) | |
803 | (setq pos (min (max 1 (+ pos (if (< n 0) -1 1))) (length history))) | |
804 | (when (= pos prevpos) | |
e91f80c4 | 805 | (error (if (= pos 1) |
ccc58657 RS |
806 | "No later matching history item" |
807 | "No earlier matching history item"))) | |
e276a14a MB |
808 | (setq match-string |
809 | (if (eq minibuffer-history-sexp-flag (minibuffer-depth)) | |
7908d27c | 810 | (let ((print-level nil)) |
e276a14a MB |
811 | (prin1-to-string (nth (1- pos) history))) |
812 | (nth (1- pos) history))) | |
813 | (setq match-offset | |
814 | (if (< n 0) | |
815 | (and (string-match regexp match-string) | |
816 | (match-end 0)) | |
817 | (and (string-match (concat ".*\\(" regexp "\\)") match-string) | |
818 | (match-beginning 1)))) | |
819 | (when match-offset | |
820 | (setq n (+ n (if (< n 0) 1 -1))))) | |
821 | (setq minibuffer-history-position pos) | |
822 | (goto-char (point-max)) | |
efaac2e6 | 823 | (delete-minibuffer-contents) |
e276a14a | 824 | (insert match-string) |
6d74d713 | 825 | (goto-char (+ (minibuffer-prompt-end) match-offset)))) |
e1e04350 SM |
826 | (if (memq (car (car command-history)) '(previous-matching-history-element |
827 | next-matching-history-element)) | |
854c16c5 | 828 | (setq command-history (cdr command-history)))) |
e91f80c4 | 829 | |
e91f80c4 | 830 | (defun next-matching-history-element (regexp n) |
854c16c5 RS |
831 | "Find the next history element that matches REGEXP. |
832 | \(The next history element refers to a more recent action.) | |
833 | With prefix argument N, search for Nth next match. | |
5c2010f0 | 834 | If N is negative, find the previous or Nth previous match. |
9889af08 EZ |
835 | Normally, history elements are matched case-insensitively if |
836 | `case-fold-search' is non-nil, but an uppercase letter in REGEXP | |
837 | makes the search case-sensitive." | |
854c16c5 | 838 | (interactive |
c1172a19 | 839 | (let* ((enable-recursive-minibuffers t) |
c1172a19 RS |
840 | (regexp (read-from-minibuffer "Next element matching (regexp): " |
841 | nil | |
842 | minibuffer-local-map | |
843 | nil | |
844 | 'minibuffer-history-search-history))) | |
845 | ;; Use the last regexp specified, by default, if input is empty. | |
846 | (list (if (string= regexp "") | |
847 | (setcar minibuffer-history-search-history | |
848 | (nth 1 minibuffer-history-search-history)) | |
849 | regexp) | |
854c16c5 | 850 | (prefix-numeric-value current-prefix-arg)))) |
e91f80c4 | 851 | (previous-matching-history-element regexp (- n))) |
2076c87c | 852 | |
8dc3ba7d MB |
853 | (defvar minibuffer-temporary-goal-position nil) |
854 | ||
ebb61177 RS |
855 | (defun next-history-element (n) |
856 | "Insert the next element of the minibuffer history into the minibuffer." | |
2076c87c | 857 | (interactive "p") |
0818b15e | 858 | (or (zerop n) |
93cee14b RS |
859 | (let ((narg (- minibuffer-history-position n)) |
860 | (minimum (if minibuffer-default -1 0)) | |
068c712c | 861 | elt minibuffer-returned-to-present) |
93cee14b RS |
862 | (if (and (zerop minibuffer-history-position) |
863 | (null minibuffer-text-before-history)) | |
efaac2e6 MB |
864 | (setq minibuffer-text-before-history |
865 | (minibuffer-contents-no-properties))) | |
93cee14b | 866 | (if (< narg minimum) |
81091543 RS |
867 | (if minibuffer-default |
868 | (error "End of history; no next item") | |
869 | (error "End of history; no default available"))) | |
93cee14b RS |
870 | (if (> narg (length (symbol-value minibuffer-history-variable))) |
871 | (error "Beginning of history; no preceding item")) | |
e1e04350 SM |
872 | (unless (memq last-command '(next-history-element |
873 | previous-history-element)) | |
efaac2e6 | 874 | (let ((prompt-end (minibuffer-prompt-end))) |
8dc3ba7d MB |
875 | (set (make-local-variable 'minibuffer-temporary-goal-position) |
876 | (cond ((<= (point) prompt-end) prompt-end) | |
877 | ((eobp) nil) | |
878 | (t (point)))))) | |
a4d1159b | 879 | (goto-char (point-max)) |
efaac2e6 | 880 | (delete-minibuffer-contents) |
93cee14b RS |
881 | (setq minibuffer-history-position narg) |
882 | (cond ((= narg -1) | |
883 | (setq elt minibuffer-default)) | |
884 | ((= narg 0) | |
54c548db | 885 | (setq elt (or minibuffer-text-before-history "")) |
068c712c | 886 | (setq minibuffer-returned-to-present t) |
93cee14b RS |
887 | (setq minibuffer-text-before-history nil)) |
888 | (t (setq elt (nth (1- minibuffer-history-position) | |
889 | (symbol-value minibuffer-history-variable))))) | |
890 | (insert | |
068c712c KH |
891 | (if (and (eq minibuffer-history-sexp-flag (minibuffer-depth)) |
892 | (not minibuffer-returned-to-present)) | |
93cee14b RS |
893 | (let ((print-level nil)) |
894 | (prin1-to-string elt)) | |
895 | elt)) | |
8dc3ba7d | 896 | (goto-char (or minibuffer-temporary-goal-position (point-max)))))) |
2076c87c | 897 | |
ebb61177 | 898 | (defun previous-history-element (n) |
3ee3a076 | 899 | "Inserts the previous element of the minibuffer history into the minibuffer." |
2076c87c | 900 | (interactive "p") |
2c5e21c1 | 901 | (next-history-element (- n))) |
d0678801 RM |
902 | |
903 | (defun next-complete-history-element (n) | |
a4d1159b GM |
904 | "Get next history element which completes the minibuffer before the point. |
905 | The contents of the minibuffer after the point are deleted, and replaced | |
906 | by the new completion." | |
d0678801 | 907 | (interactive "p") |
b5e6f936 RM |
908 | (let ((point-at-start (point))) |
909 | (next-matching-history-element | |
a4d1159b | 910 | (concat |
efaac2e6 | 911 | "^" (regexp-quote (buffer-substring (minibuffer-prompt-end) (point)))) |
a4d1159b | 912 | n) |
b5e6f936 RM |
913 | ;; next-matching-history-element always puts us at (point-min). |
914 | ;; Move to the position we were at before changing the buffer contents. | |
915 | ;; This is still sensical, because the text before point has not changed. | |
916 | (goto-char point-at-start))) | |
d0678801 RM |
917 | |
918 | (defun previous-complete-history-element (n) | |
1f6fcec3 | 919 | "\ |
a4d1159b GM |
920 | Get previous history element which completes the minibuffer before the point. |
921 | The contents of the minibuffer after the point are deleted, and replaced | |
922 | by the new completion." | |
d0678801 RM |
923 | (interactive "p") |
924 | (next-complete-history-element (- n))) | |
a4d1159b | 925 | |
efaac2e6 | 926 | ;; For compatibility with the old subr of the same name. |
a4d1159b GM |
927 | (defun minibuffer-prompt-width () |
928 | "Return the display width of the minibuffer prompt. | |
929 | Return 0 if current buffer is not a mini-buffer." | |
930 | ;; Return the width of everything before the field at the end of | |
931 | ;; the buffer; this should be 0 for normal buffers. | |
efaac2e6 | 932 | (1- (minibuffer-prompt-end))) |
2d88b556 | 933 | \f |
2076c87c | 934 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg |
e462e42f | 935 | (defalias 'advertised-undo 'undo) |
2076c87c JB |
936 | |
937 | (defun undo (&optional arg) | |
938 | "Undo some previous changes. | |
939 | Repeat this command to undo more changes. | |
65627aad RS |
940 | A numeric argument serves as a repeat count. |
941 | ||
3c1b77ca MB |
942 | In Transient Mark mode when the mark is active, only undo changes within |
943 | the current region. Similarly, when not in Transient Mark mode, just C-u | |
944 | as an argument limits undo to changes within the current region." | |
65627aad | 945 | (interactive "*P") |
2e033693 RS |
946 | ;; Make last-command indicate for the next command that this was an undo. |
947 | ;; That way, another undo will undo more. | |
948 | ;; If we get to the end of the undo history and get an error, | |
949 | ;; another undo command will find the undo history empty | |
950 | ;; and will get another error. To begin undoing the undos, | |
951 | ;; you must type some other command. | |
952 | (setq this-command 'undo) | |
b553cffa RS |
953 | (let ((modified (buffer-modified-p)) |
954 | (recent-save (recent-auto-save-p))) | |
71e40adf | 955 | (or (eq (selected-window) (minibuffer-window)) |
fa1735d7 RS |
956 | (message (if (and transient-mark-mode mark-active) |
957 | "Undo in region!" | |
958 | "Undo!"))) | |
3c1b77ca MB |
959 | (unless (eq last-command 'undo) |
960 | (if (if transient-mark-mode mark-active (and arg (not (numberp arg)))) | |
961 | (undo-start (region-beginning) (region-end)) | |
962 | (undo-start)) | |
963 | ;; get rid of initial undo boundary | |
964 | (undo-more 1)) | |
965 | (undo-more | |
966 | (if (or transient-mark-mode (numberp arg)) | |
967 | (prefix-numeric-value arg) | |
968 | 1)) | |
2512c9f0 RS |
969 | ;; Don't specify a position in the undo record for the undo command. |
970 | ;; Instead, undoing this should move point to where the change is. | |
971 | (let ((tail buffer-undo-list) | |
003550c5 GM |
972 | (prev nil)) |
973 | (while (car tail) | |
974 | (when (integerp (car tail)) | |
975 | (let ((pos (car tail))) | |
976 | (if (null prev) | |
977 | (setq buffer-undo-list (cdr tail)) | |
978 | (setcdr prev (cdr tail))) | |
979 | (setq tail (cdr tail)) | |
980 | (while (car tail) | |
981 | (if (eq pos (car tail)) | |
982 | (if prev | |
983 | (setcdr prev (cdr tail)) | |
984 | (setq buffer-undo-list (cdr tail))) | |
985 | (setq prev tail)) | |
986 | (setq tail (cdr tail))) | |
987 | (setq tail nil))) | |
988 | (setq prev tail tail (cdr tail)))) | |
989 | ||
2076c87c | 990 | (and modified (not (buffer-modified-p)) |
2e033693 | 991 | (delete-auto-save-file-if-necessary recent-save)))) |
2076c87c | 992 | |
278b0a58 RS |
993 | (defvar pending-undo-list nil |
994 | "Within a run of consecutive undo commands, list remaining to be undone.") | |
995 | ||
52d1110d RS |
996 | (defvar undo-in-progress nil |
997 | "Non-nil while performing an undo. | |
998 | Some change-hooks test this variable to do something different.") | |
999 | ||
2076c87c JB |
1000 | (defun undo-more (count) |
1001 | "Undo back N undo-boundaries beyond what was already undone recently. | |
ff1fbe3e RS |
1002 | Call `undo-start' to get ready to undo recent changes, |
1003 | then call `undo-more' one or more times to undo them." | |
2076c87c | 1004 | (or pending-undo-list |
fa1735d7 RS |
1005 | (error (format "No further undo information%s" |
1006 | (if (and transient-mark-mode mark-active) | |
1007 | " for region" "")))) | |
52d1110d RS |
1008 | (let ((undo-in-progress t)) |
1009 | (setq pending-undo-list (primitive-undo count pending-undo-list)))) | |
2076c87c | 1010 | |
65627aad RS |
1011 | ;; Deep copy of a list |
1012 | (defun undo-copy-list (list) | |
1013 | "Make a copy of undo list LIST." | |
1014 | (mapcar 'undo-copy-list-1 list)) | |
1015 | ||
1016 | (defun undo-copy-list-1 (elt) | |
1017 | (if (consp elt) | |
1018 | (cons (car elt) (undo-copy-list-1 (cdr elt))) | |
1019 | elt)) | |
1020 | ||
1021 | (defun undo-start (&optional beg end) | |
1022 | "Set `pending-undo-list' to the front of the undo list. | |
1023 | The next call to `undo-more' will undo the most recently made change. | |
1024 | If BEG and END are specified, then only undo elements | |
1025 | that apply to text between BEG and END are used; other undo elements | |
1026 | are ignored. If BEG and END are nil, all undo elements are used." | |
1027 | (if (eq buffer-undo-list t) | |
1028 | (error "No undo information in this buffer")) | |
1e722f9f | 1029 | (setq pending-undo-list |
65627aad RS |
1030 | (if (and beg end (not (= beg end))) |
1031 | (undo-make-selective-list (min beg end) (max beg end)) | |
1032 | buffer-undo-list))) | |
1033 | ||
1034 | (defvar undo-adjusted-markers) | |
1035 | ||
1036 | (defun undo-make-selective-list (start end) | |
1037 | "Return a list of undo elements for the region START to END. | |
1038 | The elements come from `buffer-undo-list', but we keep only | |
1039 | the elements inside this region, and discard those outside this region. | |
1040 | If we find an element that crosses an edge of this region, | |
1041 | we stop and ignore all further elements." | |
1042 | (let ((undo-list-copy (undo-copy-list buffer-undo-list)) | |
1043 | (undo-list (list nil)) | |
1044 | undo-adjusted-markers | |
1045 | some-rejected | |
1046 | undo-elt undo-elt temp-undo-list delta) | |
1047 | (while undo-list-copy | |
1048 | (setq undo-elt (car undo-list-copy)) | |
1049 | (let ((keep-this | |
1050 | (cond ((and (consp undo-elt) (eq (car undo-elt) t)) | |
1051 | ;; This is a "was unmodified" element. | |
1052 | ;; Keep it if we have kept everything thus far. | |
1053 | (not some-rejected)) | |
1054 | (t | |
1055 | (undo-elt-in-region undo-elt start end))))) | |
1056 | (if keep-this | |
1057 | (progn | |
1058 | (setq end (+ end (cdr (undo-delta undo-elt)))) | |
1059 | ;; Don't put two nils together in the list | |
1060 | (if (not (and (eq (car undo-list) nil) | |
1061 | (eq undo-elt nil))) | |
1062 | (setq undo-list (cons undo-elt undo-list)))) | |
1063 | (if (undo-elt-crosses-region undo-elt start end) | |
1064 | (setq undo-list-copy nil) | |
1065 | (setq some-rejected t) | |
1066 | (setq temp-undo-list (cdr undo-list-copy)) | |
1067 | (setq delta (undo-delta undo-elt)) | |
1068 | ||
1069 | (when (/= (cdr delta) 0) | |
1070 | (let ((position (car delta)) | |
1071 | (offset (cdr delta))) | |
1072 | ||
e1e04350 SM |
1073 | ;; Loop down the earlier events adjusting their buffer |
1074 | ;; positions to reflect the fact that a change to the buffer | |
1075 | ;; isn't being undone. We only need to process those element | |
1076 | ;; types which undo-elt-in-region will return as being in | |
1077 | ;; the region since only those types can ever get into the | |
1078 | ;; output | |
65627aad RS |
1079 | |
1080 | (while temp-undo-list | |
1081 | (setq undo-elt (car temp-undo-list)) | |
1082 | (cond ((integerp undo-elt) | |
1083 | (if (>= undo-elt position) | |
1084 | (setcar temp-undo-list (- undo-elt offset)))) | |
1085 | ((atom undo-elt) nil) | |
1086 | ((stringp (car undo-elt)) | |
1087 | ;; (TEXT . POSITION) | |
1088 | (let ((text-pos (abs (cdr undo-elt))) | |
1089 | (point-at-end (< (cdr undo-elt) 0 ))) | |
1090 | (if (>= text-pos position) | |
1e722f9f | 1091 | (setcdr undo-elt (* (if point-at-end -1 1) |
65627aad RS |
1092 | (- text-pos offset)))))) |
1093 | ((integerp (car undo-elt)) | |
1094 | ;; (BEGIN . END) | |
1095 | (when (>= (car undo-elt) position) | |
1096 | (setcar undo-elt (- (car undo-elt) offset)) | |
1097 | (setcdr undo-elt (- (cdr undo-elt) offset)))) | |
1098 | ((null (car undo-elt)) | |
1099 | ;; (nil PROPERTY VALUE BEG . END) | |
1100 | (let ((tail (nthcdr 3 undo-elt))) | |
1101 | (when (>= (car tail) position) | |
1102 | (setcar tail (- (car tail) offset)) | |
1103 | (setcdr tail (- (cdr tail) offset)))))) | |
1104 | (setq temp-undo-list (cdr temp-undo-list)))))))) | |
1105 | (setq undo-list-copy (cdr undo-list-copy))) | |
1106 | (nreverse undo-list))) | |
1107 | ||
1108 | (defun undo-elt-in-region (undo-elt start end) | |
1109 | "Determine whether UNDO-ELT falls inside the region START ... END. | |
1110 | If it crosses the edge, we return nil." | |
1111 | (cond ((integerp undo-elt) | |
1112 | (and (>= undo-elt start) | |
12a93712 | 1113 | (<= undo-elt end))) |
65627aad RS |
1114 | ((eq undo-elt nil) |
1115 | t) | |
1116 | ((atom undo-elt) | |
1117 | nil) | |
1118 | ((stringp (car undo-elt)) | |
1119 | ;; (TEXT . POSITION) | |
1120 | (and (>= (abs (cdr undo-elt)) start) | |
1121 | (< (abs (cdr undo-elt)) end))) | |
1122 | ((and (consp undo-elt) (markerp (car undo-elt))) | |
1123 | ;; This is a marker-adjustment element (MARKER . ADJUSTMENT). | |
1124 | ;; See if MARKER is inside the region. | |
1125 | (let ((alist-elt (assq (car undo-elt) undo-adjusted-markers))) | |
1126 | (unless alist-elt | |
1127 | (setq alist-elt (cons (car undo-elt) | |
1128 | (marker-position (car undo-elt)))) | |
1129 | (setq undo-adjusted-markers | |
1130 | (cons alist-elt undo-adjusted-markers))) | |
1131 | (and (cdr alist-elt) | |
1132 | (>= (cdr alist-elt) start) | |
12a93712 | 1133 | (<= (cdr alist-elt) end)))) |
65627aad RS |
1134 | ((null (car undo-elt)) |
1135 | ;; (nil PROPERTY VALUE BEG . END) | |
1136 | (let ((tail (nthcdr 3 undo-elt))) | |
1137 | (and (>= (car tail) start) | |
12a93712 | 1138 | (<= (cdr tail) end)))) |
65627aad RS |
1139 | ((integerp (car undo-elt)) |
1140 | ;; (BEGIN . END) | |
1141 | (and (>= (car undo-elt) start) | |
12a93712 | 1142 | (<= (cdr undo-elt) end))))) |
65627aad RS |
1143 | |
1144 | (defun undo-elt-crosses-region (undo-elt start end) | |
1145 | "Test whether UNDO-ELT crosses one edge of that region START ... END. | |
1146 | This assumes we have already decided that UNDO-ELT | |
1147 | is not *inside* the region START...END." | |
1148 | (cond ((atom undo-elt) nil) | |
1149 | ((null (car undo-elt)) | |
1150 | ;; (nil PROPERTY VALUE BEG . END) | |
1151 | (let ((tail (nthcdr 3 undo-elt))) | |
1152 | (not (or (< (car tail) end) | |
1153 | (> (cdr tail) start))))) | |
1154 | ((integerp (car undo-elt)) | |
1155 | ;; (BEGIN . END) | |
1156 | (not (or (< (car undo-elt) end) | |
1157 | (> (cdr undo-elt) start)))))) | |
1158 | ||
1159 | ;; Return the first affected buffer position and the delta for an undo element | |
1160 | ;; delta is defined as the change in subsequent buffer positions if we *did* | |
1161 | ;; the undo. | |
1162 | (defun undo-delta (undo-elt) | |
1163 | (if (consp undo-elt) | |
1164 | (cond ((stringp (car undo-elt)) | |
1165 | ;; (TEXT . POSITION) | |
1166 | (cons (abs (cdr undo-elt)) (length (car undo-elt)))) | |
1167 | ((integerp (car undo-elt)) | |
1168 | ;; (BEGIN . END) | |
1169 | (cons (car undo-elt) (- (car undo-elt) (cdr undo-elt)))) | |
1170 | (t | |
1171 | '(0 . 0))) | |
1172 | '(0 . 0))) | |
e1e04350 | 1173 | \f |
009ef402 RS |
1174 | (defvar shell-command-history nil |
1175 | "History list for some commands that read shell commands.") | |
1176 | ||
59fc41e5 RS |
1177 | (defvar shell-command-switch "-c" |
1178 | "Switch used to have the shell execute its command line argument.") | |
1179 | ||
cc039f78 KH |
1180 | (defvar shell-command-default-error-buffer nil |
1181 | "*Buffer name for `shell-command' and `shell-command-on-region' error output. | |
637fff82 | 1182 | This buffer is used when `shell-command' or `shell-command-on-region' |
cc039f78 KH |
1183 | is run interactively. A value of nil means that output to stderr and |
1184 | stdout will be intermixed in the output stream.") | |
1185 | ||
1186 | (defun shell-command (command &optional output-buffer error-buffer) | |
2076c87c | 1187 | "Execute string COMMAND in inferior shell; display output, if any. |
0b3f96d4 | 1188 | With prefix argument, insert the COMMAND's output at point. |
d382f610 | 1189 | |
2076c87c | 1190 | If COMMAND ends in ampersand, execute it asynchronously. |
d382f610 | 1191 | The output appears in the buffer `*Async Shell Command*'. |
bcad4985 | 1192 | That buffer is in shell mode. |
d382f610 | 1193 | |
939ac10c GM |
1194 | Otherwise, COMMAND is executed synchronously. The output appears in |
1195 | the buffer `*Shell Command Output*'. If the output is short enough to | |
1196 | display in the echo area (which is determined by the variables | |
1197 | `resize-mini-windows' and `max-mini-window-height'), it is shown | |
1198 | there, but it is nonetheless available in buffer `*Shell Command | |
e1e04350 | 1199 | Output*' even though that buffer is not automatically displayed. |
d0d74413 | 1200 | |
07f458c1 RS |
1201 | To specify a coding system for converting non-ASCII characters |
1202 | in the shell command output, use \\[universal-coding-system-argument] | |
1203 | before this command. | |
1204 | ||
1205 | Noninteractive callers can specify coding systems by binding | |
1206 | `coding-system-for-read' and `coding-system-for-write'. | |
1207 | ||
d0d74413 RS |
1208 | The optional second argument OUTPUT-BUFFER, if non-nil, |
1209 | says to put the output in some other buffer. | |
1210 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. | |
1211 | If OUTPUT-BUFFER is not a buffer and not nil, | |
1212 | insert output in current buffer. (This cannot be done asynchronously.) | |
cc039f78 KH |
1213 | In either case, the output is inserted after point (leaving mark after it). |
1214 | ||
2e033693 RS |
1215 | If the command terminates without error, but generates output, |
1216 | and you did not specify \"insert it in the current buffer\", | |
1217 | the output can be displayed in the echo area or in its buffer. | |
1218 | If the output is short enough to display in the echo area | |
1219 | \(determined by the variable `max-mini-window-height' if | |
1220 | `resize-mini-windows' is non-nil), it is shown there. Otherwise, | |
1221 | the buffer containing the output is displayed. | |
1222 | ||
1223 | If there is output and an error, and you did not specify \"insert it | |
1224 | in the current buffer\", a message about the error goes at the end | |
1225 | of the output. | |
1226 | ||
1227 | If there is no output, or if output is inserted in the current buffer, | |
1228 | then `*Shell Command Output*' is deleted. | |
1229 | ||
cc039f78 KH |
1230 | If the optional third argument ERROR-BUFFER is non-nil, it is a buffer |
1231 | or buffer name to which to direct the command's standard error output. | |
1232 | If it is nil, error output is mingled with regular output. | |
1233 | In an interactive call, the variable `shell-command-default-error-buffer' | |
1234 | specifies the value of ERROR-BUFFER." | |
1235 | ||
aa00b92d RS |
1236 | (interactive (list (read-from-minibuffer "Shell command: " |
1237 | nil nil nil 'shell-command-history) | |
cc039f78 KH |
1238 | current-prefix-arg |
1239 | shell-command-default-error-buffer)) | |
c7edd03c KH |
1240 | ;; Look for a handler in case default-directory is a remote file name. |
1241 | (let ((handler | |
1242 | (find-file-name-handler (directory-file-name default-directory) | |
1243 | 'shell-command))) | |
1244 | (if handler | |
cc039f78 | 1245 | (funcall handler 'shell-command command output-buffer error-buffer) |
c7edd03c KH |
1246 | (if (and output-buffer |
1247 | (not (or (bufferp output-buffer) (stringp output-buffer)))) | |
2e033693 | 1248 | ;; Output goes in current buffer. |
cc039f78 | 1249 | (let ((error-file |
1e722f9f | 1250 | (if error-buffer |
b005abd5 | 1251 | (make-temp-file |
171a45d9 EZ |
1252 | (expand-file-name "scor" |
1253 | (or small-temporary-file-directory | |
1254 | temporary-file-directory))) | |
cc039f78 KH |
1255 | nil))) |
1256 | (barf-if-buffer-read-only) | |
63437623 | 1257 | (push-mark nil t) |
cc039f78 KH |
1258 | ;; We do not use -f for csh; we will not support broken use of |
1259 | ;; .cshrcs. Even the BSD csh manual says to use | |
1260 | ;; "if ($?prompt) exit" before things which are not useful | |
1261 | ;; non-interactively. Besides, if someone wants their other | |
1262 | ;; aliases for shell commands then they can still have them. | |
1e722f9f | 1263 | (call-process shell-file-name nil |
cc039f78 KH |
1264 | (if error-file |
1265 | (list t error-file) | |
1266 | t) | |
1267 | nil shell-command-switch command) | |
1268 | (when (and error-file (file-exists-p error-file)) | |
1269 | (if (< 0 (nth 7 (file-attributes error-file))) | |
1270 | (with-current-buffer (get-buffer-create error-buffer) | |
1271 | (let ((pos-from-end (- (point-max) (point)))) | |
1272 | (or (bobp) | |
1273 | (insert "\f\n")) | |
1274 | ;; Do no formatting while reading error file, | |
1275 | ;; because that can run a shell command, and we | |
1276 | ;; don't want that to cause an infinite recursion. | |
1277 | (format-insert-file error-file nil) | |
1278 | ;; Put point after the inserted errors. | |
1279 | (goto-char (- (point-max) pos-from-end))) | |
1280 | (display-buffer (current-buffer)))) | |
1281 | (delete-file error-file)) | |
1282 | ;; This is like exchange-point-and-mark, but doesn't | |
1283 | ;; activate the mark. It is cleaner to avoid activation, | |
1284 | ;; even though the command loop would deactivate the mark | |
1285 | ;; because we inserted text. | |
1286 | (goto-char (prog1 (mark t) | |
1287 | (set-marker (mark-marker) (point) | |
1288 | (current-buffer))))) | |
2e033693 | 1289 | ;; Output goes in a separate buffer. |
c7edd03c KH |
1290 | ;; Preserve the match data in case called from a program. |
1291 | (save-match-data | |
aab5d2c5 | 1292 | (if (string-match "[ \t]*&[ \t]*\\'" command) |
c7edd03c KH |
1293 | ;; Command ending with ampersand means asynchronous. |
1294 | (let ((buffer (get-buffer-create | |
1295 | (or output-buffer "*Async Shell Command*"))) | |
1296 | (directory default-directory) | |
1297 | proc) | |
1298 | ;; Remove the ampersand. | |
1299 | (setq command (substring command 0 (match-beginning 0))) | |
1300 | ;; If will kill a process, query first. | |
1301 | (setq proc (get-buffer-process buffer)) | |
1302 | (if proc | |
1303 | (if (yes-or-no-p "A command is running. Kill it? ") | |
1304 | (kill-process proc) | |
1305 | (error "Shell command in progress"))) | |
1306 | (save-excursion | |
1307 | (set-buffer buffer) | |
1308 | (setq buffer-read-only nil) | |
1309 | (erase-buffer) | |
1310 | (display-buffer buffer) | |
1311 | (setq default-directory directory) | |
1e722f9f | 1312 | (setq proc (start-process "Shell" buffer shell-file-name |
c7edd03c KH |
1313 | shell-command-switch command)) |
1314 | (setq mode-line-process '(":%s")) | |
1315 | (require 'shell) (shell-mode) | |
1316 | (set-process-sentinel proc 'shell-command-sentinel) | |
1317 | )) | |
cc039f78 KH |
1318 | (shell-command-on-region (point) (point) command |
1319 | output-buffer nil error-buffer))))))) | |
eaae8106 | 1320 | |
f69aad2b MB |
1321 | (defun display-message-or-buffer (message |
1322 | &optional buffer-name not-this-window frame) | |
1323 | "Display MESSAGE in the echo area if possible, otherwise in a pop-up buffer. | |
1324 | MESSAGE may be either a string or a buffer. | |
1325 | ||
1326 | A buffer is displayed using `display-buffer' if MESSAGE is too long for | |
939ac10c GM |
1327 | the maximum height of the echo area, as defined by `max-mini-window-height' |
1328 | if `resize-mini-windows' is non-nil. | |
f69aad2b | 1329 | |
2a3f00bf MB |
1330 | Returns either the string shown in the echo area, or when a pop-up |
1331 | buffer is used, the window used to display it. | |
1332 | ||
f69aad2b MB |
1333 | If MESSAGE is a string, then the optional argument BUFFER-NAME is the |
1334 | name of the buffer used to display it in the case where a pop-up buffer | |
1335 | is used, defaulting to `*Message*'. In the case where MESSAGE is a | |
1336 | string and it is displayed in the echo area, it is not specified whether | |
1337 | the contents are inserted into the buffer anyway. | |
1338 | ||
1339 | Optional arguments NOT-THIS-WINDOW and FRAME are as for `display-buffer', | |
1340 | and only used if a buffer is displayed." | |
1341 | (cond ((and (stringp message) (not (string-match "\n" message))) | |
1342 | ;; Trivial case where we can use the echo area | |
1343 | (message "%s" message)) | |
1344 | ((and (stringp message) | |
1345 | (= (string-match "\n" message) (1- (length message)))) | |
1346 | ;; Trivial case where we can just remove single trailing newline | |
1347 | (message "%s" (substring message 0 (1- (length message))))) | |
1348 | (t | |
1349 | ;; General case | |
1350 | (with-current-buffer | |
1351 | (if (bufferp message) | |
1352 | message | |
1353 | (get-buffer-create (or buffer-name "*Message*"))) | |
1354 | ||
1355 | (unless (bufferp message) | |
1356 | (erase-buffer) | |
1357 | (insert message)) | |
1358 | ||
1359 | (let ((lines | |
1360 | (if (= (buffer-size) 0) | |
1361 | 0 | |
1362 | (count-lines (point-min) (point-max))))) | |
4f017185 RS |
1363 | (cond ((= lines 0)) |
1364 | ((and (or (<= lines 1) | |
aab5d2c5 RS |
1365 | (<= lines |
1366 | (if resize-mini-windows | |
1367 | (cond ((floatp max-mini-window-height) | |
1368 | (* (frame-height) | |
1369 | max-mini-window-height)) | |
1370 | ((integerp max-mini-window-height) | |
1371 | max-mini-window-height) | |
1372 | (t | |
1373 | 1)) | |
1374 | 1))) | |
1375 | ;; Don't use the echo area if the output buffer is | |
1376 | ;; already dispayed in the selected frame. | |
61b80ebf | 1377 | (not (get-buffer-window (current-buffer)))) |
f69aad2b MB |
1378 | ;; Echo area |
1379 | (goto-char (point-max)) | |
1380 | (when (bolp) | |
1381 | (backward-char 1)) | |
1382 | (message "%s" (buffer-substring (point-min) (point)))) | |
1383 | (t | |
1384 | ;; Buffer | |
1385 | (goto-char (point-min)) | |
31252c00 MB |
1386 | (display-buffer (current-buffer) |
1387 | not-this-window frame)))))))) | |
f69aad2b MB |
1388 | |
1389 | ||
2076c87c JB |
1390 | ;; We have a sentinel to prevent insertion of a termination message |
1391 | ;; in the buffer itself. | |
1392 | (defun shell-command-sentinel (process signal) | |
bcad4985 | 1393 | (if (memq (process-status process) '(exit signal)) |
1e722f9f | 1394 | (message "%s: %s." |
bcad4985 KH |
1395 | (car (cdr (cdr (process-command process)))) |
1396 | (substring signal 0 -1)))) | |
2076c87c | 1397 | |
d0d74413 | 1398 | (defun shell-command-on-region (start end command |
cce1c318 RS |
1399 | &optional output-buffer replace |
1400 | error-buffer) | |
2076c87c JB |
1401 | "Execute string COMMAND in inferior shell with region as input. |
1402 | Normally display output (if any) in temp buffer `*Shell Command Output*'; | |
a0184aeb DL |
1403 | Prefix arg means replace the region with it. Return the exit code of |
1404 | COMMAND. | |
56c0450e | 1405 | |
07f458c1 RS |
1406 | To specify a coding system for converting non-ASCII characters |
1407 | in the input and output to the shell command, use \\[universal-coding-system-argument] | |
1408 | before this command. By default, the input (from the current buffer) | |
1409 | is encoded in the same coding system that will be used to save the file, | |
1410 | `buffer-file-coding-system'. If the output is going to replace the region, | |
1411 | then it is decoded from that same coding system. | |
1412 | ||
8923a211 RS |
1413 | The noninteractive arguments are START, END, COMMAND, OUTPUT-BUFFER, |
1414 | REPLACE, ERROR-BUFFER. Noninteractive callers can specify coding | |
1415 | systems by binding `coding-system-for-read' and | |
1416 | `coding-system-for-write'. | |
2076c87c | 1417 | |
2e033693 RS |
1418 | If the command generates output, the output may be displayed |
1419 | in the echo area or in a buffer. | |
1420 | If the output is short enough to display in the echo area | |
1421 | \(determined by the variable `max-mini-window-height' if | |
1422 | `resize-mini-windows' is non-nil), it is shown there. Otherwise | |
1423 | it is displayed in the buffer `*Shell Command Output*'. The output | |
1424 | is available in that buffer in both cases. | |
1425 | ||
1426 | If there is output and an error, a message about the error | |
1427 | appears at the end of the output. | |
1428 | ||
1429 | If there is no output, or if output is inserted in the current buffer, | |
1430 | then `*Shell Command Output*' is deleted. | |
d0d74413 | 1431 | |
56c0450e RS |
1432 | If the optional fourth argument OUTPUT-BUFFER is non-nil, |
1433 | that says to put the output in some other buffer. | |
d0d74413 RS |
1434 | If OUTPUT-BUFFER is a buffer or buffer name, put the output there. |
1435 | If OUTPUT-BUFFER is not a buffer and not nil, | |
1436 | insert output in the current buffer. | |
cce1c318 RS |
1437 | In either case, the output is inserted after point (leaving mark after it). |
1438 | ||
8923a211 RS |
1439 | If REPLACE, the optional fifth argument, is non-nil, that means insert |
1440 | the output in place of text from START to END, putting point and mark | |
1441 | around it. | |
1442 | ||
b735c991 | 1443 | If optional sixth argument ERROR-BUFFER is non-nil, it is a buffer |
cce1c318 | 1444 | or buffer name to which to direct the command's standard error output. |
7fd47839 | 1445 | If it is nil, error output is mingled with regular output. |
cc039f78 KH |
1446 | In an interactive call, the variable `shell-command-default-error-buffer' |
1447 | specifies the value of ERROR-BUFFER." | |
195ce311 RS |
1448 | (interactive (let (string) |
1449 | (unless (mark) | |
1450 | (error "The mark is not set now, so there is no region")) | |
1451 | ;; Do this before calling region-beginning | |
1452 | ;; and region-end, in case subprocess output | |
1453 | ;; relocates them while we are in the minibuffer. | |
1454 | (setq string (read-from-minibuffer "Shell command on region: " | |
1455 | nil nil nil | |
1456 | 'shell-command-history)) | |
2b03c506 RS |
1457 | ;; call-interactively recognizes region-beginning and |
1458 | ;; region-end specially, leaving them in the history. | |
1459 | (list (region-beginning) (region-end) | |
cae49185 RS |
1460 | string |
1461 | current-prefix-arg | |
7fd47839 | 1462 | current-prefix-arg |
cc039f78 | 1463 | shell-command-default-error-buffer))) |
cce1c318 | 1464 | (let ((error-file |
171a45d9 | 1465 | (if error-buffer |
b005abd5 | 1466 | (make-temp-file |
171a45d9 EZ |
1467 | (expand-file-name "scor" |
1468 | (or small-temporary-file-directory | |
1469 | temporary-file-directory))) | |
a0184aeb DL |
1470 | nil)) |
1471 | exit-status) | |
7fd47839 RS |
1472 | (if (or replace |
1473 | (and output-buffer | |
748d6ca4 | 1474 | (not (or (bufferp output-buffer) (stringp output-buffer))))) |
7fd47839 RS |
1475 | ;; Replace specified region with output from command. |
1476 | (let ((swap (and replace (< start end)))) | |
1477 | ;; Don't muck with mark unless REPLACE says we should. | |
1478 | (goto-char start) | |
30883773 | 1479 | (and replace (push-mark (point) 'nomsg)) |
a0184aeb DL |
1480 | (setq exit-status |
1481 | (call-process-region start end shell-file-name t | |
1482 | (if error-file | |
1483 | (list t error-file) | |
1484 | t) | |
1485 | nil shell-command-switch command)) | |
e1e04350 SM |
1486 | ;; It is rude to delete a buffer which the command is not using. |
1487 | ;; (let ((shell-buffer (get-buffer "*Shell Command Output*"))) | |
1488 | ;; (and shell-buffer (not (eq shell-buffer (current-buffer))) | |
1489 | ;; (kill-buffer shell-buffer))) | |
7fd47839 RS |
1490 | ;; Don't muck with mark unless REPLACE says we should. |
1491 | (and replace swap (exchange-point-and-mark))) | |
1492 | ;; No prefix argument: put the output in a temp buffer, | |
1493 | ;; replacing its entire contents. | |
1494 | (let ((buffer (get-buffer-create | |
d4bbcbb4 | 1495 | (or output-buffer "*Shell Command Output*")))) |
7fd47839 RS |
1496 | (unwind-protect |
1497 | (if (eq buffer (current-buffer)) | |
1498 | ;; If the input is the same buffer as the output, | |
1499 | ;; delete everything but the specified region, | |
1500 | ;; then replace that region with the output. | |
1501 | (progn (setq buffer-read-only nil) | |
1502 | (delete-region (max start end) (point-max)) | |
1503 | (delete-region (point-min) (min start end)) | |
1504 | (setq exit-status | |
1505 | (call-process-region (point-min) (point-max) | |
1e722f9f | 1506 | shell-file-name t |
7fd47839 RS |
1507 | (if error-file |
1508 | (list t error-file) | |
1509 | t) | |
a0184aeb DL |
1510 | nil shell-command-switch |
1511 | command))) | |
1512 | ;; Clear the output buffer, then run the command with | |
1513 | ;; output there. | |
c2e303c8 GM |
1514 | (let ((directory default-directory)) |
1515 | (save-excursion | |
1516 | (set-buffer buffer) | |
1517 | (setq buffer-read-only nil) | |
1518 | (if (not output-buffer) | |
1519 | (setq default-directory directory)) | |
1520 | (erase-buffer))) | |
7fd47839 RS |
1521 | (setq exit-status |
1522 | (call-process-region start end shell-file-name nil | |
1523 | (if error-file | |
1524 | (list buffer error-file) | |
1525 | buffer) | |
a0184aeb | 1526 | nil shell-command-switch command))) |
2e033693 | 1527 | ;; Report the output. |
9a98fa64 KS |
1528 | (with-current-buffer buffer |
1529 | (setq mode-line-process | |
d4bbcbb4 AS |
1530 | (cond ((null exit-status) |
1531 | " - Error") | |
1532 | ((stringp exit-status) | |
1533 | (format " - Signal [%s]" exit-status)) | |
1534 | ((not (equal 0 exit-status)) | |
1535 | (format " - Exit [%d]" exit-status))))) | |
f69aad2b MB |
1536 | (if (with-current-buffer buffer (> (point-max) (point-min))) |
1537 | ;; There's some output, display it | |
9a98fa64 | 1538 | (display-message-or-buffer buffer) |
f69aad2b | 1539 | ;; No output; error? |
94ddbe6d RS |
1540 | (let ((output |
1541 | (if (and error-file | |
1542 | (< 0 (nth 7 (file-attributes error-file)))) | |
1543 | "some error output" | |
1544 | "no output"))) | |
d4bbcbb4 AS |
1545 | (cond ((null exit-status) |
1546 | (message "(Shell command failed with error)")) | |
1547 | ((equal 0 exit-status) | |
1548 | (message "(Shell command succeeded with %s)" | |
1549 | output)) | |
1550 | ((stringp exit-status) | |
1551 | (message "(Shell command killed by signal %s)" | |
1552 | exit-status)) | |
1553 | (t | |
1554 | (message "(Shell command failed with code %d and %s)" | |
1555 | exit-status output)))) | |
e1e04350 SM |
1556 | ;; Don't kill: there might be useful info in the undo-log. |
1557 | ;; (kill-buffer buffer) | |
1558 | )))) | |
f69aad2b | 1559 | |
cc039f78 KH |
1560 | (when (and error-file (file-exists-p error-file)) |
1561 | (if (< 0 (nth 7 (file-attributes error-file))) | |
1562 | (with-current-buffer (get-buffer-create error-buffer) | |
1563 | (let ((pos-from-end (- (point-max) (point)))) | |
1564 | (or (bobp) | |
1565 | (insert "\f\n")) | |
1566 | ;; Do no formatting while reading error file, | |
1567 | ;; because that can run a shell command, and we | |
1568 | ;; don't want that to cause an infinite recursion. | |
1569 | (format-insert-file error-file nil) | |
1570 | ;; Put point after the inserted errors. | |
1571 | (goto-char (- (point-max) pos-from-end))) | |
1572 | (display-buffer (current-buffer)))) | |
1573 | (delete-file error-file)) | |
a0184aeb | 1574 | exit-status)) |
1e722f9f | 1575 | |
d589bd99 RS |
1576 | (defun shell-command-to-string (command) |
1577 | "Execute shell command COMMAND and return its output as a string." | |
1578 | (with-output-to-string | |
17cc9013 RS |
1579 | (with-current-buffer |
1580 | standard-output | |
1581 | (call-process shell-file-name nil t nil shell-command-switch command)))) | |
2d88b556 | 1582 | \f |
1b43f83f | 1583 | (defvar universal-argument-map |
69d4c3c4 KH |
1584 | (let ((map (make-sparse-keymap))) |
1585 | (define-key map [t] 'universal-argument-other-key) | |
b9ff190d | 1586 | (define-key map (vector meta-prefix-char t) 'universal-argument-other-key) |
69d4c3c4 KH |
1587 | (define-key map [switch-frame] nil) |
1588 | (define-key map [?\C-u] 'universal-argument-more) | |
1589 | (define-key map [?-] 'universal-argument-minus) | |
1590 | (define-key map [?0] 'digit-argument) | |
1591 | (define-key map [?1] 'digit-argument) | |
1592 | (define-key map [?2] 'digit-argument) | |
1593 | (define-key map [?3] 'digit-argument) | |
1594 | (define-key map [?4] 'digit-argument) | |
1595 | (define-key map [?5] 'digit-argument) | |
1596 | (define-key map [?6] 'digit-argument) | |
1597 | (define-key map [?7] 'digit-argument) | |
1598 | (define-key map [?8] 'digit-argument) | |
1599 | (define-key map [?9] 'digit-argument) | |
bd7acc8d GM |
1600 | (define-key map [kp-0] 'digit-argument) |
1601 | (define-key map [kp-1] 'digit-argument) | |
1602 | (define-key map [kp-2] 'digit-argument) | |
1603 | (define-key map [kp-3] 'digit-argument) | |
1604 | (define-key map [kp-4] 'digit-argument) | |
1605 | (define-key map [kp-5] 'digit-argument) | |
1606 | (define-key map [kp-6] 'digit-argument) | |
1607 | (define-key map [kp-7] 'digit-argument) | |
1608 | (define-key map [kp-8] 'digit-argument) | |
1609 | (define-key map [kp-9] 'digit-argument) | |
1610 | (define-key map [kp-subtract] 'universal-argument-minus) | |
69d4c3c4 KH |
1611 | map) |
1612 | "Keymap used while processing \\[universal-argument].") | |
1613 | ||
0de84e16 RS |
1614 | (defvar universal-argument-num-events nil |
1615 | "Number of argument-specifying events read by `universal-argument'. | |
1616 | `universal-argument-other-key' uses this to discard those events | |
1617 | from (this-command-keys), and reread only the final command.") | |
1618 | ||
e8d1a377 KH |
1619 | (defun universal-argument () |
1620 | "Begin a numeric argument for the following command. | |
1621 | Digits or minus sign following \\[universal-argument] make up the numeric argument. | |
1622 | \\[universal-argument] following the digits or minus sign ends the argument. | |
1623 | \\[universal-argument] without digits or minus sign provides 4 as argument. | |
1624 | Repeating \\[universal-argument] without digits or minus sign | |
0565d307 RS |
1625 | multiplies the argument by 4 each time. |
1626 | For some commands, just \\[universal-argument] by itself serves as a flag | |
a697fc62 RS |
1627 | which is different in effect from any particular numeric argument. |
1628 | These commands include \\[set-mark-command] and \\[start-kbd-macro]." | |
69d4c3c4 KH |
1629 | (interactive) |
1630 | (setq prefix-arg (list 4)) | |
0de84e16 | 1631 | (setq universal-argument-num-events (length (this-command-keys))) |
69d4c3c4 | 1632 | (setq overriding-terminal-local-map universal-argument-map)) |
e8d1a377 | 1633 | |
69d4c3c4 KH |
1634 | ;; A subsequent C-u means to multiply the factor by 4 if we've typed |
1635 | ;; nothing but C-u's; otherwise it means to terminate the prefix arg. | |
1636 | (defun universal-argument-more (arg) | |
e8d1a377 | 1637 | (interactive "P") |
69d4c3c4 KH |
1638 | (if (consp arg) |
1639 | (setq prefix-arg (list (* 4 (car arg)))) | |
1cd24721 RS |
1640 | (if (eq arg '-) |
1641 | (setq prefix-arg (list -4)) | |
1642 | (setq prefix-arg arg) | |
1643 | (setq overriding-terminal-local-map nil))) | |
0de84e16 | 1644 | (setq universal-argument-num-events (length (this-command-keys)))) |
e8d1a377 KH |
1645 | |
1646 | (defun negative-argument (arg) | |
1647 | "Begin a negative numeric argument for the next command. | |
1648 | \\[universal-argument] following digits or minus sign ends the argument." | |
1649 | (interactive "P") | |
69d4c3c4 KH |
1650 | (cond ((integerp arg) |
1651 | (setq prefix-arg (- arg))) | |
1652 | ((eq arg '-) | |
1653 | (setq prefix-arg nil)) | |
1654 | (t | |
b9ff190d | 1655 | (setq prefix-arg '-))) |
0de84e16 | 1656 | (setq universal-argument-num-events (length (this-command-keys))) |
b9ff190d | 1657 | (setq overriding-terminal-local-map universal-argument-map)) |
69d4c3c4 KH |
1658 | |
1659 | (defun digit-argument (arg) | |
1660 | "Part of the numeric argument for the next command. | |
1661 | \\[universal-argument] following digits or minus sign ends the argument." | |
1662 | (interactive "P") | |
bd7acc8d GM |
1663 | (let* ((char (if (integerp last-command-char) |
1664 | last-command-char | |
1665 | (get last-command-char 'ascii-character))) | |
1666 | (digit (- (logand char ?\177) ?0))) | |
69d4c3c4 KH |
1667 | (cond ((integerp arg) |
1668 | (setq prefix-arg (+ (* arg 10) | |
1669 | (if (< arg 0) (- digit) digit)))) | |
1670 | ((eq arg '-) | |
1671 | ;; Treat -0 as just -, so that -01 will work. | |
1672 | (setq prefix-arg (if (zerop digit) '- (- digit)))) | |
1673 | (t | |
b9ff190d | 1674 | (setq prefix-arg digit)))) |
0de84e16 | 1675 | (setq universal-argument-num-events (length (this-command-keys))) |
b9ff190d | 1676 | (setq overriding-terminal-local-map universal-argument-map)) |
69d4c3c4 KH |
1677 | |
1678 | ;; For backward compatibility, minus with no modifiers is an ordinary | |
1679 | ;; command if digits have already been entered. | |
1680 | (defun universal-argument-minus (arg) | |
1681 | (interactive "P") | |
1682 | (if (integerp arg) | |
1683 | (universal-argument-other-key arg) | |
1684 | (negative-argument arg))) | |
1685 | ||
1686 | ;; Anything else terminates the argument and is left in the queue to be | |
1687 | ;; executed as a command. | |
1688 | (defun universal-argument-other-key (arg) | |
1689 | (interactive "P") | |
1690 | (setq prefix-arg arg) | |
0de84e16 RS |
1691 | (let* ((key (this-command-keys)) |
1692 | (keylist (listify-key-sequence key))) | |
1693 | (setq unread-command-events | |
06697cdb RS |
1694 | (append (nthcdr universal-argument-num-events keylist) |
1695 | unread-command-events))) | |
f0ef2555 | 1696 | (reset-this-command-lengths) |
69d4c3c4 | 1697 | (setq overriding-terminal-local-map nil)) |
2d88b556 | 1698 | \f |
93be67de | 1699 | ;;;; Window system cut and paste hooks. |
70e14c01 JB |
1700 | |
1701 | (defvar interprogram-cut-function nil | |
1702 | "Function to call to make a killed region available to other programs. | |
1703 | ||
1704 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
1705 | pasting text between the windows of different programs. |
1706 | This variable holds a function that Emacs calls whenever text | |
1707 | is put in the kill ring, to make the new kill available to other | |
70e14c01 JB |
1708 | programs. |
1709 | ||
9f112a3d RS |
1710 | The function takes one or two arguments. |
1711 | The first argument, TEXT, is a string containing | |
1712 | the text which should be made available. | |
1713 | The second, PUSH, if non-nil means this is a \"new\" kill; | |
1714 | nil means appending to an \"old\" kill.") | |
70e14c01 JB |
1715 | |
1716 | (defvar interprogram-paste-function nil | |
1717 | "Function to call to get text cut from other programs. | |
1718 | ||
1719 | Most window systems provide some sort of facility for cutting and | |
9f112a3d RS |
1720 | pasting text between the windows of different programs. |
1721 | This variable holds a function that Emacs calls to obtain | |
70e14c01 JB |
1722 | text that other programs have provided for pasting. |
1723 | ||
1724 | The function should be called with no arguments. If the function | |
1725 | returns nil, then no other program has provided such text, and the top | |
1726 | of the Emacs kill ring should be used. If the function returns a | |
daa37602 JB |
1727 | string, that string should be put in the kill ring as the latest kill. |
1728 | ||
1729 | Note that the function should return a string only if a program other | |
1730 | than Emacs has provided a string for pasting; if Emacs provided the | |
1731 | most recent string, the function should return nil. If it is | |
1732 | difficult to tell whether Emacs or some other program provided the | |
1733 | current string, it is probably good enough to return nil if the string | |
1734 | is equal (according to `string=') to the last text Emacs provided.") | |
2d88b556 | 1735 | \f |
70e14c01 | 1736 | |
eaae8106 | 1737 | |
70e14c01 | 1738 | ;;;; The kill ring data structure. |
2076c87c JB |
1739 | |
1740 | (defvar kill-ring nil | |
70e14c01 JB |
1741 | "List of killed text sequences. |
1742 | Since the kill ring is supposed to interact nicely with cut-and-paste | |
1743 | facilities offered by window systems, use of this variable should | |
1744 | interact nicely with `interprogram-cut-function' and | |
1745 | `interprogram-paste-function'. The functions `kill-new', | |
1746 | `kill-append', and `current-kill' are supposed to implement this | |
1747 | interaction; you may want to use them instead of manipulating the kill | |
1748 | ring directly.") | |
2076c87c | 1749 | |
bffa4d92 | 1750 | (defcustom kill-ring-max 60 |
69c1dd37 RS |
1751 | "*Maximum length of kill ring before oldest elements are thrown away." |
1752 | :type 'integer | |
1753 | :group 'killing) | |
2076c87c JB |
1754 | |
1755 | (defvar kill-ring-yank-pointer nil | |
1756 | "The tail of the kill ring whose car is the last thing yanked.") | |
1757 | ||
f914dc91 | 1758 | (defun kill-new (string &optional replace) |
70e14c01 | 1759 | "Make STRING the latest kill in the kill ring. |
3e505153 | 1760 | Set `kill-ring-yank-pointer' to point to it. |
f914dc91 KH |
1761 | If `interprogram-cut-function' is non-nil, apply it to STRING. |
1762 | Optional second argument REPLACE non-nil means that STRING will replace | |
1763 | the front of the kill ring, rather than being added to the list." | |
f1d01ba2 KH |
1764 | (and (fboundp 'menu-bar-update-yank-menu) |
1765 | (menu-bar-update-yank-menu string (and replace (car kill-ring)))) | |
ab7e20d5 | 1766 | (if (and replace kill-ring) |
f914dc91 KH |
1767 | (setcar kill-ring string) |
1768 | (setq kill-ring (cons string kill-ring)) | |
1769 | (if (> (length kill-ring) kill-ring-max) | |
1770 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) | |
70e14c01 JB |
1771 | (setq kill-ring-yank-pointer kill-ring) |
1772 | (if interprogram-cut-function | |
657a33ab | 1773 | (funcall interprogram-cut-function string (not replace)))) |
70e14c01 | 1774 | |
2076c87c | 1775 | (defun kill-append (string before-p) |
70e14c01 JB |
1776 | "Append STRING to the end of the latest kill in the kill ring. |
1777 | If BEFORE-P is non-nil, prepend STRING to the kill. | |
88c1aa79 | 1778 | If `interprogram-cut-function' is set, pass the resulting kill to |
70e14c01 | 1779 | it." |
f914dc91 KH |
1780 | (kill-new (if before-p |
1781 | (concat string (car kill-ring)) | |
2192bd73 RS |
1782 | (concat (car kill-ring) string)) |
1783 | t)) | |
70e14c01 JB |
1784 | |
1785 | (defun current-kill (n &optional do-not-move) | |
1786 | "Rotate the yanking point by N places, and then return that kill. | |
1787 | If N is zero, `interprogram-paste-function' is set, and calling it | |
1788 | returns a string, then that string is added to the front of the | |
1789 | kill ring and returned as the latest kill. | |
1e722f9f | 1790 | If optional arg DO-NOT-MOVE is non-nil, then don't actually move the |
70e14c01 JB |
1791 | yanking point; just return the Nth kill forward." |
1792 | (let ((interprogram-paste (and (= n 0) | |
1793 | interprogram-paste-function | |
1794 | (funcall interprogram-paste-function)))) | |
1795 | (if interprogram-paste | |
1796 | (progn | |
1797 | ;; Disable the interprogram cut function when we add the new | |
1798 | ;; text to the kill ring, so Emacs doesn't try to own the | |
1799 | ;; selection, with identical text. | |
1800 | (let ((interprogram-cut-function nil)) | |
1801 | (kill-new interprogram-paste)) | |
1802 | interprogram-paste) | |
1803 | (or kill-ring (error "Kill ring is empty")) | |
47096a67 PE |
1804 | (let ((ARGth-kill-element |
1805 | (nthcdr (mod (- n (length kill-ring-yank-pointer)) | |
1806 | (length kill-ring)) | |
1807 | kill-ring))) | |
70e14c01 JB |
1808 | (or do-not-move |
1809 | (setq kill-ring-yank-pointer ARGth-kill-element)) | |
1810 | (car ARGth-kill-element))))) | |
c88ab9ce | 1811 | |
c88ab9ce | 1812 | |
eaae8106 | 1813 | |
70e14c01 | 1814 | ;;;; Commands for manipulating the kill ring. |
c88ab9ce | 1815 | |
69c1dd37 RS |
1816 | (defcustom kill-read-only-ok nil |
1817 | "*Non-nil means don't signal an error for killing read-only text." | |
1818 | :type 'boolean | |
1819 | :group 'killing) | |
e6291fe1 | 1820 | |
3a5da8a8 RS |
1821 | (put 'text-read-only 'error-conditions |
1822 | '(text-read-only buffer-read-only error)) | |
1823 | (put 'text-read-only 'error-message "Text is read-only") | |
1824 | ||
2076c87c JB |
1825 | (defun kill-region (beg end) |
1826 | "Kill between point and mark. | |
1827 | The text is deleted but saved in the kill ring. | |
1828 | The command \\[yank] can retrieve it from there. | |
81558867 EZ |
1829 | \(If you want to kill and then yank immediately, use \\[kill-ring-save].) |
1830 | ||
1831 | If you want to append the killed region to the last killed text, | |
1832 | use \\[append-next-kill] before \\[kill-region]. | |
1833 | ||
2aa7a8bf JB |
1834 | If the buffer is read-only, Emacs will beep and refrain from deleting |
1835 | the text, but put the text in the kill ring anyway. This means that | |
1836 | you can use the killing commands to copy text from a read-only buffer. | |
2076c87c JB |
1837 | |
1838 | This is the primitive for programs to kill text (as opposed to deleting it). | |
1839 | Supply two arguments, character numbers indicating the stretch of text | |
1840 | to be killed. | |
1841 | Any command that calls this function is a \"kill command\". | |
1842 | If the previous command was also a kill command, | |
1843 | the text killed this time appends to the text killed last time | |
1844 | to make one entry in the kill ring." | |
e761e42c | 1845 | (interactive "r") |
ccd19b9f | 1846 | (condition-case nil |
a1eb02bd SM |
1847 | (let ((string (delete-and-extract-region beg end))) |
1848 | (when string ;STRING is nil if BEG = END | |
1849 | ;; Add that string to the kill ring, one way or another. | |
1850 | (if (eq last-command 'kill-region) | |
1851 | (kill-append string (< end beg)) | |
1852 | (kill-new string))) | |
ccd19b9f KH |
1853 | (setq this-command 'kill-region)) |
1854 | ((buffer-read-only text-read-only) | |
1855 | ;; The code above failed because the buffer, or some of the characters | |
1856 | ;; in the region, are read-only. | |
1857 | ;; We should beep, in case the user just isn't aware of this. | |
1858 | ;; However, there's no harm in putting | |
1859 | ;; the region's text in the kill ring, anyway. | |
1860 | (copy-region-as-kill beg end) | |
cb3e1b4c RS |
1861 | ;; Set this-command now, so it will be set even if we get an error. |
1862 | (setq this-command 'kill-region) | |
1863 | ;; This should barf, if appropriate, and give us the correct error. | |
ccd19b9f KH |
1864 | (if kill-read-only-ok |
1865 | (message "Read only text copied to kill ring") | |
ccd19b9f KH |
1866 | ;; Signal an error if the buffer is read-only. |
1867 | (barf-if-buffer-read-only) | |
1868 | ;; If the buffer isn't read-only, the text is. | |
1869 | (signal 'text-read-only (list (current-buffer))))))) | |
2076c87c | 1870 | |
a382890a KH |
1871 | ;; copy-region-as-kill no longer sets this-command, because it's confusing |
1872 | ;; to get two copies of the text when the user accidentally types M-w and | |
1873 | ;; then corrects it with the intended C-w. | |
2076c87c JB |
1874 | (defun copy-region-as-kill (beg end) |
1875 | "Save the region as if killed, but don't kill it. | |
0e264847 | 1876 | In Transient Mark mode, deactivate the mark. |
46947372 JB |
1877 | If `interprogram-cut-function' is non-nil, also save the text for a window |
1878 | system cut and paste." | |
2076c87c JB |
1879 | (interactive "r") |
1880 | (if (eq last-command 'kill-region) | |
1881 | (kill-append (buffer-substring beg end) (< end beg)) | |
70e14c01 | 1882 | (kill-new (buffer-substring beg end))) |
1e722f9f | 1883 | (if transient-mark-mode |
5c7319b6 | 1884 | (setq deactivate-mark t)) |
2076c87c JB |
1885 | nil) |
1886 | ||
1887 | (defun kill-ring-save (beg end) | |
0964e562 | 1888 | "Save the region as if killed, but don't kill it. |
0e264847 | 1889 | In Transient Mark mode, deactivate the mark. |
0964e562 | 1890 | If `interprogram-cut-function' is non-nil, also save the text for a window |
0e264847 RS |
1891 | system cut and paste. |
1892 | ||
81558867 EZ |
1893 | If you want to append the killed line to the last killed text, |
1894 | use \\[append-next-kill] before \\[kill-ring-save]. | |
1895 | ||
0e264847 RS |
1896 | This command is similar to `copy-region-as-kill', except that it gives |
1897 | visual feedback indicating the extent of the region being copied." | |
2076c87c JB |
1898 | (interactive "r") |
1899 | (copy-region-as-kill beg end) | |
3a801d0c | 1900 | (if (interactive-p) |
66050f10 RS |
1901 | (let ((other-end (if (= (point) beg) end beg)) |
1902 | (opoint (point)) | |
1903 | ;; Inhibit quitting so we can make a quit here | |
1904 | ;; look like a C-g typed as a command. | |
1905 | (inhibit-quit t)) | |
1906 | (if (pos-visible-in-window-p other-end (selected-window)) | |
db6f8ff1 | 1907 | (unless transient-mark-mode |
66050f10 RS |
1908 | ;; Swap point and mark. |
1909 | (set-marker (mark-marker) (point) (current-buffer)) | |
1910 | (goto-char other-end) | |
1911 | (sit-for 1) | |
1912 | ;; Swap back. | |
1913 | (set-marker (mark-marker) other-end (current-buffer)) | |
1914 | (goto-char opoint) | |
1915 | ;; If user quit, deactivate the mark | |
1916 | ;; as C-g would as a command. | |
e4e593ae | 1917 | (and quit-flag mark-active |
fcadf1c7 | 1918 | (deactivate-mark))) |
66050f10 RS |
1919 | (let* ((killed-text (current-kill 0)) |
1920 | (message-len (min (length killed-text) 40))) | |
1921 | (if (= (point) beg) | |
1922 | ;; Don't say "killed"; that is misleading. | |
1923 | (message "Saved text until \"%s\"" | |
1924 | (substring killed-text (- message-len))) | |
1925 | (message "Saved text from \"%s\"" | |
1926 | (substring killed-text 0 message-len)))))))) | |
2076c87c | 1927 | |
c75d4986 KH |
1928 | (defun append-next-kill (&optional interactive) |
1929 | "Cause following command, if it kills, to append to previous kill. | |
1930 | The argument is used for internal purposes; do not supply one." | |
1931 | (interactive "p") | |
1932 | ;; We don't use (interactive-p), since that breaks kbd macros. | |
1933 | (if interactive | |
2076c87c JB |
1934 | (progn |
1935 | (setq this-command 'kill-region) | |
1936 | (message "If the next command is a kill, it will append")) | |
1937 | (setq last-command 'kill-region))) | |
cfb4f123 | 1938 | \f |
93be67de | 1939 | ;; Yanking. |
2076c87c | 1940 | |
cfb4f123 RS |
1941 | ;; This is actually used in subr.el but defcustom does not work there. |
1942 | (defcustom yank-excluded-properties | |
82093109 | 1943 | '(read-only invisible intangible field mouse-face help-echo local-map keymap) |
cfb4f123 RS |
1944 | "*Text properties to discard when yanking." |
1945 | :type '(choice (const :tag "All" t) (repeat symbol)) | |
1946 | :group 'editing | |
a2d10c40 | 1947 | :version "21.4") |
cfb4f123 | 1948 | |
120de5bd RS |
1949 | (defvar yank-window-start nil) |
1950 | ||
2076c87c | 1951 | (defun yank-pop (arg) |
ff1fbe3e RS |
1952 | "Replace just-yanked stretch of killed text with a different stretch. |
1953 | This command is allowed only immediately after a `yank' or a `yank-pop'. | |
2076c87c | 1954 | At such a time, the region contains a stretch of reinserted |
ff1fbe3e | 1955 | previously-killed text. `yank-pop' deletes that text and inserts in its |
2076c87c JB |
1956 | place a different stretch of killed text. |
1957 | ||
1958 | With no argument, the previous kill is inserted. | |
ff1fbe3e RS |
1959 | With argument N, insert the Nth previous kill. |
1960 | If N is negative, this is a more recent kill. | |
2076c87c JB |
1961 | |
1962 | The sequence of kills wraps around, so that after the oldest one | |
1963 | comes the newest one." | |
1964 | (interactive "*p") | |
1965 | (if (not (eq last-command 'yank)) | |
1966 | (error "Previous command was not a yank")) | |
1967 | (setq this-command 'yank) | |
3a5da8a8 RS |
1968 | (let ((inhibit-read-only t) |
1969 | (before (< (point) (mark t)))) | |
9a1277dd | 1970 | (delete-region (point) (mark t)) |
fd0f4056 | 1971 | (set-marker (mark-marker) (point) (current-buffer)) |
cfb4f123 | 1972 | (insert-for-yank (current-kill arg)) |
120de5bd RS |
1973 | ;; Set the window start back where it was in the yank command, |
1974 | ;; if possible. | |
1975 | (set-window-start (selected-window) yank-window-start t) | |
fd0f4056 RS |
1976 | (if before |
1977 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. | |
1978 | ;; It is cleaner to avoid activation, even though the command | |
1979 | ;; loop would deactivate the mark because we inserted text. | |
1980 | (goto-char (prog1 (mark t) | |
1981 | (set-marker (mark-marker) (point) (current-buffer)))))) | |
0964e562 | 1982 | nil) |
2076c87c JB |
1983 | |
1984 | (defun yank (&optional arg) | |
1985 | "Reinsert the last stretch of killed text. | |
1986 | More precisely, reinsert the stretch of killed text most recently | |
ff1fbe3e | 1987 | killed OR yanked. Put point at end, and set mark at beginning. |
d99f8496 | 1988 | With just \\[universal-argument] as argument, same but put point at beginning (and mark at end). |
ff1fbe3e | 1989 | With argument N, reinsert the Nth most recently killed stretch of killed |
2076c87c JB |
1990 | text. |
1991 | See also the command \\[yank-pop]." | |
1992 | (interactive "*P") | |
120de5bd | 1993 | (setq yank-window-start (window-start)) |
456c617c RS |
1994 | ;; If we don't get all the way thru, make last-command indicate that |
1995 | ;; for the following command. | |
1996 | (setq this-command t) | |
2076c87c | 1997 | (push-mark (point)) |
cfb4f123 RS |
1998 | (insert-for-yank (current-kill (cond |
1999 | ((listp arg) 0) | |
2000 | ((eq arg '-) -1) | |
2001 | (t (1- arg))))) | |
2076c87c | 2002 | (if (consp arg) |
fd0f4056 RS |
2003 | ;; This is like exchange-point-and-mark, but doesn't activate the mark. |
2004 | ;; It is cleaner to avoid activation, even though the command | |
2005 | ;; loop would deactivate the mark because we inserted text. | |
2006 | (goto-char (prog1 (mark t) | |
2007 | (set-marker (mark-marker) (point) (current-buffer))))) | |
456c617c RS |
2008 | ;; If we do get all the way thru, make this-command indicate that. |
2009 | (setq this-command 'yank) | |
0964e562 | 2010 | nil) |
70e14c01 JB |
2011 | |
2012 | (defun rotate-yank-pointer (arg) | |
2013 | "Rotate the yanking point in the kill ring. | |
2014 | With argument, rotate that many kills forward (or backward, if negative)." | |
2015 | (interactive "p") | |
2016 | (current-kill arg)) | |
2d88b556 | 2017 | \f |
93be67de KH |
2018 | ;; Some kill commands. |
2019 | ||
2020 | ;; Internal subroutine of delete-char | |
2021 | (defun kill-forward-chars (arg) | |
2022 | (if (listp arg) (setq arg (car arg))) | |
2023 | (if (eq arg '-) (setq arg -1)) | |
2024 | (kill-region (point) (forward-point arg))) | |
2025 | ||
2026 | ;; Internal subroutine of backward-delete-char | |
2027 | (defun kill-backward-chars (arg) | |
2028 | (if (listp arg) (setq arg (car arg))) | |
2029 | (if (eq arg '-) (setq arg -1)) | |
2030 | (kill-region (point) (forward-point (- arg)))) | |
2031 | ||
2032 | (defcustom backward-delete-char-untabify-method 'untabify | |
2033 | "*The method for untabifying when deleting backward. | |
1e722f9f SS |
2034 | Can be `untabify' -- turn a tab to many spaces, then delete one space; |
2035 | `hungry' -- delete all whitespace, both tabs and spaces; | |
2036 | `all' -- delete all whitespace, including tabs, spaces and newlines; | |
93be67de | 2037 | nil -- just delete one character." |
1e722f9f | 2038 | :type '(choice (const untabify) (const hungry) (const all) (const nil)) |
03167a34 | 2039 | :version "20.3" |
93be67de KH |
2040 | :group 'killing) |
2041 | ||
2042 | (defun backward-delete-char-untabify (arg &optional killp) | |
2043 | "Delete characters backward, changing tabs into spaces. | |
2044 | The exact behavior depends on `backward-delete-char-untabify-method'. | |
2045 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. | |
2046 | Interactively, ARG is the prefix arg (default 1) | |
2047 | and KILLP is t if a prefix arg was specified." | |
2048 | (interactive "*p\nP") | |
2049 | (when (eq backward-delete-char-untabify-method 'untabify) | |
2050 | (let ((count arg)) | |
2051 | (save-excursion | |
2052 | (while (and (> count 0) (not (bobp))) | |
2053 | (if (= (preceding-char) ?\t) | |
2054 | (let ((col (current-column))) | |
2055 | (forward-char -1) | |
2056 | (setq col (- col (current-column))) | |
2057 | (insert-char ?\ col) | |
2058 | (delete-char 1))) | |
2059 | (forward-char -1) | |
2060 | (setq count (1- count)))))) | |
2061 | (delete-backward-char | |
1e722f9f SS |
2062 | (let ((skip (cond ((eq backward-delete-char-untabify-method 'hungry) " \t") |
2063 | ((eq backward-delete-char-untabify-method 'all) | |
2064 | " \t\n\r")))) | |
2065 | (if skip | |
2066 | (let ((wh (- (point) (save-excursion (skip-chars-backward skip) | |
93be67de KH |
2067 | (point))))) |
2068 | (+ arg (if (zerop wh) 0 (1- wh)))) | |
1e722f9f | 2069 | arg)) |
93be67de KH |
2070 | killp)) |
2071 | ||
2072 | (defun zap-to-char (arg char) | |
2073 | "Kill up to and including ARG'th occurrence of CHAR. | |
2074 | Case is ignored if `case-fold-search' is non-nil in the current buffer. | |
2075 | Goes backward if ARG is negative; error if CHAR not found." | |
e761e42c | 2076 | (interactive "p\ncZap to char: ") |
93be67de KH |
2077 | (kill-region (point) (progn |
2078 | (search-forward (char-to-string char) nil nil arg) | |
2079 | ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) | |
2080 | (point)))) | |
eaae8106 | 2081 | |
93be67de KH |
2082 | ;; kill-line and its subroutines. |
2083 | ||
2084 | (defcustom kill-whole-line nil | |
2085 | "*If non-nil, `kill-line' with no arg at beg of line kills the whole line." | |
2086 | :type 'boolean | |
2087 | :group 'killing) | |
2088 | ||
2089 | (defun kill-line (&optional arg) | |
2090 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. | |
2091 | With prefix argument, kill that many lines from point. | |
2092 | Negative arguments kill lines backward. | |
8be7408c | 2093 | With zero argument, kills the text before point on the current line. |
93be67de KH |
2094 | |
2095 | When calling from a program, nil means \"no arg\", | |
2096 | a number counts as a prefix arg. | |
2097 | ||
2098 | To kill a whole line, when point is not at the beginning, type \ | |
2099 | \\[beginning-of-line] \\[kill-line] \\[kill-line]. | |
2100 | ||
2101 | If `kill-whole-line' is non-nil, then this command kills the whole line | |
2102 | including its terminating newline, when used at the beginning of a line | |
2103 | with no argument. As a consequence, you can always kill a whole line | |
d3f22784 EZ |
2104 | by typing \\[beginning-of-line] \\[kill-line]. |
2105 | ||
81558867 EZ |
2106 | If you want to append the killed line to the last killed text, |
2107 | use \\[append-next-kill] before \\[kill-line]. | |
2108 | ||
d3f22784 EZ |
2109 | If the buffer is read-only, Emacs will beep and refrain from deleting |
2110 | the line, but put the line in the kill ring anyway. This means that | |
2111 | you can use this command to copy text from a read-only buffer." | |
e761e42c | 2112 | (interactive "P") |
93be67de KH |
2113 | (kill-region (point) |
2114 | ;; It is better to move point to the other end of the kill | |
2115 | ;; before killing. That way, in a read-only buffer, point | |
2116 | ;; moves across the text that is copied to the kill ring. | |
2117 | ;; The choice has no effect on undo now that undo records | |
2118 | ;; the value of point from before the command was run. | |
2119 | (progn | |
2120 | (if arg | |
2121 | (forward-visible-line (prefix-numeric-value arg)) | |
2122 | (if (eobp) | |
2123 | (signal 'end-of-buffer nil)) | |
5560dc5d RS |
2124 | (let ((end |
2125 | (save-excursion | |
2126 | (end-of-visible-line) (point)))) | |
2127 | (if (or (save-excursion | |
2128 | (skip-chars-forward " \t" end) | |
2129 | (= (point) end)) | |
2130 | (and kill-whole-line (bolp))) | |
2131 | (forward-visible-line 1) | |
2132 | (goto-char end)))) | |
93be67de KH |
2133 | (point)))) |
2134 | ||
12a93712 | 2135 | |
93be67de KH |
2136 | (defun forward-visible-line (arg) |
2137 | "Move forward by ARG lines, ignoring currently invisible newlines only. | |
2138 | If ARG is negative, move backward -ARG lines. | |
2139 | If ARG is zero, move to the beginning of the current line." | |
2140 | (condition-case nil | |
2141 | (if (> arg 0) | |
12a93712 RS |
2142 | (progn |
2143 | (while (> arg 0) | |
93be67de | 2144 | (or (zerop (forward-line 1)) |
12a93712 RS |
2145 | (signal 'end-of-buffer nil)) |
2146 | ;; If the newline we just skipped is invisible, | |
2147 | ;; don't count it. | |
2148 | (let ((prop | |
2149 | (get-char-property (1- (point)) 'invisible))) | |
2150 | (if (if (eq buffer-invisibility-spec t) | |
2151 | prop | |
2152 | (or (memq prop buffer-invisibility-spec) | |
2153 | (assq prop buffer-invisibility-spec))) | |
2154 | (setq arg (1+ arg)))) | |
2155 | (setq arg (1- arg))) | |
2156 | ;; If invisible text follows, and it is a number of complete lines, | |
2157 | ;; skip it. | |
2158 | (let ((opoint (point))) | |
2159 | (while (and (not (eobp)) | |
2160 | (let ((prop | |
2161 | (get-char-property (point) 'invisible))) | |
2162 | (if (eq buffer-invisibility-spec t) | |
2163 | prop | |
2164 | (or (memq prop buffer-invisibility-spec) | |
2165 | (assq prop buffer-invisibility-spec))))) | |
2166 | (goto-char | |
2167 | (if (get-text-property (point) 'invisible) | |
2168 | (or (next-single-property-change (point) 'invisible) | |
2169 | (point-max)) | |
2170 | (next-overlay-change (point))))) | |
2171 | (unless (bolp) | |
2172 | (goto-char opoint)))) | |
93be67de KH |
2173 | (let ((first t)) |
2174 | (while (or first (< arg 0)) | |
2175 | (if (zerop arg) | |
2176 | (beginning-of-line) | |
2177 | (or (zerop (forward-line -1)) | |
2178 | (signal 'beginning-of-buffer nil))) | |
12a93712 RS |
2179 | ;; If the newline we just moved to is invisible, |
2180 | ;; don't count it. | |
2181 | (unless (bobp) | |
2182 | (let ((prop | |
2183 | (get-char-property (1- (point)) 'invisible))) | |
2184 | (if (if (eq buffer-invisibility-spec t) | |
2185 | prop | |
2186 | (or (memq prop buffer-invisibility-spec) | |
2187 | (assq prop buffer-invisibility-spec))) | |
2188 | (setq arg (1+ arg))))) | |
2189 | (setq first nil) | |
2190 | (setq arg (1+ arg))) | |
2191 | ;; If invisible text follows, and it is a number of complete lines, | |
2192 | ;; skip it. | |
2193 | (let ((opoint (point))) | |
93be67de KH |
2194 | (while (and (not (bobp)) |
2195 | (let ((prop | |
2196 | (get-char-property (1- (point)) 'invisible))) | |
2197 | (if (eq buffer-invisibility-spec t) | |
2198 | prop | |
2199 | (or (memq prop buffer-invisibility-spec) | |
2200 | (assq prop buffer-invisibility-spec))))) | |
2201 | (goto-char | |
2202 | (if (get-text-property (1- (point)) 'invisible) | |
2203 | (or (previous-single-property-change (point) 'invisible) | |
2204 | (point-min)) | |
12a93712 RS |
2205 | (previous-overlay-change (point))))) |
2206 | (unless (bolp) | |
2207 | (goto-char opoint))))) | |
93be67de KH |
2208 | ((beginning-of-buffer end-of-buffer) |
2209 | nil))) | |
70e14c01 | 2210 | |
93be67de KH |
2211 | (defun end-of-visible-line () |
2212 | "Move to end of current visible line." | |
2213 | (end-of-line) | |
2214 | ;; If the following character is currently invisible, | |
2215 | ;; skip all characters with that same `invisible' property value, | |
2216 | ;; then find the next newline. | |
2217 | (while (and (not (eobp)) | |
5560dc5d RS |
2218 | (save-excursion |
2219 | (skip-chars-forward "^\n") | |
2220 | (let ((prop | |
2221 | (get-char-property (point) 'invisible))) | |
2222 | (if (eq buffer-invisibility-spec t) | |
2223 | prop | |
2224 | (or (memq prop buffer-invisibility-spec) | |
2225 | (assq prop buffer-invisibility-spec)))))) | |
2226 | (skip-chars-forward "^\n") | |
93be67de KH |
2227 | (if (get-text-property (point) 'invisible) |
2228 | (goto-char (next-single-property-change (point) 'invisible)) | |
2229 | (goto-char (next-overlay-change (point)))) | |
2230 | (end-of-line))) | |
2d88b556 | 2231 | \f |
2076c87c JB |
2232 | (defun insert-buffer (buffer) |
2233 | "Insert after point the contents of BUFFER. | |
2234 | Puts mark after the inserted text. | |
6cb6e7a2 GM |
2235 | BUFFER may be a buffer or a buffer name. |
2236 | ||
2237 | This function is meant for the user to run interactively. | |
2238 | Don't call it from programs!" | |
c3d4f949 | 2239 | (interactive |
a3e7c391 FP |
2240 | (list |
2241 | (progn | |
2242 | (barf-if-buffer-read-only) | |
2243 | (read-buffer "Insert buffer: " | |
2244 | (if (eq (selected-window) (next-window (selected-window))) | |
2245 | (other-buffer (current-buffer)) | |
2246 | (window-buffer (next-window (selected-window)))) | |
2247 | t)))) | |
2076c87c JB |
2248 | (or (bufferp buffer) |
2249 | (setq buffer (get-buffer buffer))) | |
2250 | (let (start end newmark) | |
2251 | (save-excursion | |
2252 | (save-excursion | |
2253 | (set-buffer buffer) | |
2254 | (setq start (point-min) end (point-max))) | |
2255 | (insert-buffer-substring buffer start end) | |
2256 | (setq newmark (point))) | |
1537a263 JB |
2257 | (push-mark newmark)) |
2258 | nil) | |
2076c87c JB |
2259 | |
2260 | (defun append-to-buffer (buffer start end) | |
2261 | "Append to specified buffer the text of the region. | |
2262 | It is inserted into that buffer before its point. | |
2263 | ||
2264 | When calling from a program, give three arguments: | |
2265 | BUFFER (or buffer name), START and END. | |
2266 | START and END specify the portion of the current buffer to be copied." | |
70e14c01 | 2267 | (interactive |
5d771766 | 2268 | (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t)) |
23efee2c | 2269 | (region-beginning) (region-end))) |
2076c87c JB |
2270 | (let ((oldbuf (current-buffer))) |
2271 | (save-excursion | |
c069a480 GM |
2272 | (let* ((append-to (get-buffer-create buffer)) |
2273 | (windows (get-buffer-window-list append-to t t)) | |
2274 | point) | |
2275 | (set-buffer append-to) | |
2276 | (setq point (point)) | |
2277 | (barf-if-buffer-read-only) | |
2278 | (insert-buffer-substring oldbuf start end) | |
2279 | (dolist (window windows) | |
2280 | (when (= (window-point window) point) | |
2281 | (set-window-point window (point)))))))) | |
2076c87c JB |
2282 | |
2283 | (defun prepend-to-buffer (buffer start end) | |
2284 | "Prepend to specified buffer the text of the region. | |
2285 | It is inserted into that buffer after its point. | |
2286 | ||
2287 | When calling from a program, give three arguments: | |
2288 | BUFFER (or buffer name), START and END. | |
2289 | START and END specify the portion of the current buffer to be copied." | |
2290 | (interactive "BPrepend to buffer: \nr") | |
2291 | (let ((oldbuf (current-buffer))) | |
2292 | (save-excursion | |
2293 | (set-buffer (get-buffer-create buffer)) | |
74399eac | 2294 | (barf-if-buffer-read-only) |
2076c87c JB |
2295 | (save-excursion |
2296 | (insert-buffer-substring oldbuf start end))))) | |
2297 | ||
2298 | (defun copy-to-buffer (buffer start end) | |
2299 | "Copy to specified buffer the text of the region. | |
2300 | It is inserted into that buffer, replacing existing text there. | |
2301 | ||
2302 | When calling from a program, give three arguments: | |
2303 | BUFFER (or buffer name), START and END. | |
2304 | START and END specify the portion of the current buffer to be copied." | |
2305 | (interactive "BCopy to buffer: \nr") | |
2306 | (let ((oldbuf (current-buffer))) | |
2307 | (save-excursion | |
2308 | (set-buffer (get-buffer-create buffer)) | |
74399eac | 2309 | (barf-if-buffer-read-only) |
2076c87c JB |
2310 | (erase-buffer) |
2311 | (save-excursion | |
2312 | (insert-buffer-substring oldbuf start end))))) | |
2d88b556 | 2313 | \f |
62d1c1fc RM |
2314 | (put 'mark-inactive 'error-conditions '(mark-inactive error)) |
2315 | (put 'mark-inactive 'error-message "The mark is not active now") | |
2316 | ||
af39530e | 2317 | (defun mark (&optional force) |
c7c8b31e | 2318 | "Return this buffer's mark value as integer; error if mark inactive. |
af39530e | 2319 | If optional argument FORCE is non-nil, access the mark value |
c7c8b31e RS |
2320 | even if the mark is not currently active, and return nil |
2321 | if there is no mark at all. | |
af39530e | 2322 | |
2076c87c JB |
2323 | If you are using this in an editing command, you are most likely making |
2324 | a mistake; see the documentation of `set-mark'." | |
0e3a7b14 | 2325 | (if (or force (not transient-mark-mode) mark-active mark-even-if-inactive) |
af39530e | 2326 | (marker-position (mark-marker)) |
62d1c1fc | 2327 | (signal 'mark-inactive nil))) |
2076c87c | 2328 | |
19d35374 RM |
2329 | ;; Many places set mark-active directly, and several of them failed to also |
2330 | ;; run deactivate-mark-hook. This shorthand should simplify. | |
2331 | (defsubst deactivate-mark () | |
2332 | "Deactivate the mark by setting `mark-active' to nil. | |
fcadf1c7 | 2333 | \(That makes a difference only in Transient Mark mode.) |
19d35374 | 2334 | Also runs the hook `deactivate-mark-hook'." |
868c2f49 KS |
2335 | (cond |
2336 | ((eq transient-mark-mode 'lambda) | |
2337 | (setq transient-mark-mode nil)) | |
2338 | (transient-mark-mode | |
2339 | (setq mark-active nil) | |
2340 | (run-hooks 'deactivate-mark-hook)))) | |
19d35374 | 2341 | |
2076c87c JB |
2342 | (defun set-mark (pos) |
2343 | "Set this buffer's mark to POS. Don't use this function! | |
2344 | That is to say, don't use this function unless you want | |
2345 | the user to see that the mark has moved, and you want the previous | |
2346 | mark position to be lost. | |
2347 | ||
2348 | Normally, when a new mark is set, the old one should go on the stack. | |
2349 | This is why most applications should use push-mark, not set-mark. | |
2350 | ||
ff1fbe3e | 2351 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
2352 | purposes. The mark saves a location for the user's convenience. |
2353 | Most editing commands should not alter the mark. | |
2354 | To remember a location for internal use in the Lisp program, | |
2355 | store it in a Lisp variable. Example: | |
2356 | ||
2357 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." | |
2358 | ||
fcadf1c7 RS |
2359 | (if pos |
2360 | (progn | |
2361 | (setq mark-active t) | |
2362 | (run-hooks 'activate-mark-hook) | |
2363 | (set-marker (mark-marker) pos (current-buffer))) | |
24c22852 RS |
2364 | ;; Normally we never clear mark-active except in Transient Mark mode. |
2365 | ;; But when we actually clear out the mark value too, | |
2366 | ;; we must clear mark-active in any mode. | |
2367 | (setq mark-active nil) | |
2368 | (run-hooks 'deactivate-mark-hook) | |
2369 | (set-marker (mark-marker) nil))) | |
2076c87c JB |
2370 | |
2371 | (defvar mark-ring nil | |
e55e2267 | 2372 | "The list of former marks of the current buffer, most recent first.") |
2076c87c | 2373 | (make-variable-buffer-local 'mark-ring) |
e55e2267 | 2374 | (put 'mark-ring 'permanent-local t) |
2076c87c | 2375 | |
69c1dd37 RS |
2376 | (defcustom mark-ring-max 16 |
2377 | "*Maximum size of mark ring. Start discarding off end if gets this big." | |
2378 | :type 'integer | |
2379 | :group 'editing-basics) | |
2076c87c | 2380 | |
dc029f0b RM |
2381 | (defvar global-mark-ring nil |
2382 | "The list of saved global marks, most recent first.") | |
2383 | ||
69c1dd37 | 2384 | (defcustom global-mark-ring-max 16 |
dc029f0b | 2385 | "*Maximum size of global mark ring. \ |
69c1dd37 RS |
2386 | Start discarding off end if gets this big." |
2387 | :type 'integer | |
2388 | :group 'editing-basics) | |
dc029f0b | 2389 | |
868c2f49 KS |
2390 | (defun pop-to-mark-command () |
2391 | "Jump to mark, and pop a new position for mark off the ring | |
2392 | \(does not affect global mark ring\)." | |
2393 | (interactive) | |
2394 | (if (null (mark t)) | |
2395 | (error "No mark set in this buffer") | |
868c2f49 KS |
2396 | (goto-char (mark t)) |
2397 | (pop-mark))) | |
2398 | ||
d00ffe21 | 2399 | (defun push-mark-command (arg &optional nomsg) |
868c2f49 | 2400 | "Set mark at where point is. |
d00ffe21 KS |
2401 | If no prefix arg and mark is already set there, just activate it. |
2402 | Display `Mark set' unless the optional second arg NOMSG is non-nil." | |
868c2f49 KS |
2403 | (interactive "P") |
2404 | (let ((mark (marker-position (mark-marker)))) | |
2405 | (if (or arg (null mark) (/= mark (point))) | |
d00ffe21 | 2406 | (push-mark nil nomsg t) |
868c2f49 | 2407 | (setq mark-active t) |
d00ffe21 KS |
2408 | (unless nomsg |
2409 | (message "Mark activated"))))) | |
868c2f49 | 2410 | |
2076c87c JB |
2411 | (defun set-mark-command (arg) |
2412 | "Set mark at where point is, or jump to mark. | |
dc029f0b | 2413 | With no prefix argument, set mark, push old mark position on local mark |
868c2f49 KS |
2414 | ring, and push mark on global mark ring. Immediately repeating the |
2415 | command activates `transient-mark-mode' temporarily. | |
2416 | ||
dc029f0b | 2417 | With argument, jump to mark, and pop a new position for mark off the ring |
868c2f49 KS |
2418 | \(does not affect global mark ring\). Repeating the command without |
2419 | an argument jumps to the next position off the mark ring. | |
2076c87c | 2420 | |
ff1fbe3e | 2421 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
2076c87c JB |
2422 | purposes. See the documentation of `set-mark' for more information." |
2423 | (interactive "P") | |
868c2f49 KS |
2424 | (if (eq transient-mark-mode 'lambda) |
2425 | (setq transient-mark-mode nil)) | |
2426 | (cond | |
2427 | ((not (eq this-command 'set-mark-command)) | |
1841f9e3 KS |
2428 | (if arg |
2429 | (pop-to-mark-command) | |
2430 | (push-mark-command t))) | |
868c2f49 KS |
2431 | ((eq last-command 'pop-to-mark-command) |
2432 | (if (and (consp arg) (> (prefix-numeric-value arg) 4)) | |
2433 | (push-mark-command nil) | |
1841f9e3 | 2434 | (setq this-command 'pop-to-mark-command) |
868c2f49 KS |
2435 | (pop-to-mark-command))) |
2436 | (arg | |
1841f9e3 | 2437 | (setq this-command 'pop-to-mark-command) |
868c2f49 KS |
2438 | (pop-to-mark-command)) |
2439 | ((and (eq last-command 'set-mark-command) | |
2440 | mark-active (null transient-mark-mode)) | |
2441 | (setq transient-mark-mode 'lambda) | |
2442 | (message "Transient-mark-mode temporarily enabled")) | |
2443 | (t | |
2444 | (push-mark-command nil)))) | |
2076c87c | 2445 | |
fd0f4056 | 2446 | (defun push-mark (&optional location nomsg activate) |
2076c87c | 2447 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. |
f1382a3d RM |
2448 | If the last global mark pushed was not in the current buffer, |
2449 | also push LOCATION on the global mark ring. | |
fd0f4056 | 2450 | Display `Mark set' unless the optional second arg NOMSG is non-nil. |
8cdc660f | 2451 | In Transient Mark mode, activate mark if optional third arg ACTIVATE non-nil. |
2076c87c | 2452 | |
ff1fbe3e | 2453 | Novice Emacs Lisp programmers often try to use the mark for the wrong |
9a1277dd RS |
2454 | purposes. See the documentation of `set-mark' for more information. |
2455 | ||
2456 | In Transient Mark mode, this does not activate the mark." | |
af39530e | 2457 | (if (null (mark t)) |
2076c87c JB |
2458 | nil |
2459 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) | |
2460 | (if (> (length mark-ring) mark-ring-max) | |
2461 | (progn | |
2462 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) | |
2463 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) | |
9a1277dd | 2464 | (set-marker (mark-marker) (or location (point)) (current-buffer)) |
dc029f0b | 2465 | ;; Now push the mark on the global mark ring. |
f1382a3d | 2466 | (if (and global-mark-ring |
e08d3f7c | 2467 | (eq (marker-buffer (car global-mark-ring)) (current-buffer))) |
f1382a3d RM |
2468 | ;; The last global mark pushed was in this same buffer. |
2469 | ;; Don't push another one. | |
2470 | nil | |
2471 | (setq global-mark-ring (cons (copy-marker (mark-marker)) global-mark-ring)) | |
dc029f0b RM |
2472 | (if (> (length global-mark-ring) global-mark-ring-max) |
2473 | (progn | |
2474 | (move-marker (car (nthcdr global-mark-ring-max global-mark-ring)) | |
2475 | nil) | |
f1382a3d | 2476 | (setcdr (nthcdr (1- global-mark-ring-max) global-mark-ring) nil)))) |
efcf38c7 | 2477 | (or nomsg executing-kbd-macro (> (minibuffer-depth) 0) |
2076c87c | 2478 | (message "Mark set")) |
8cdc660f RS |
2479 | (if (or activate (not transient-mark-mode)) |
2480 | (set-mark (mark t))) | |
2076c87c JB |
2481 | nil) |
2482 | ||
2483 | (defun pop-mark () | |
2484 | "Pop off mark ring into the buffer's actual mark. | |
2485 | Does not set point. Does nothing if mark ring is empty." | |
2486 | (if mark-ring | |
2487 | (progn | |
2488 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) | |
9a1277dd | 2489 | (set-marker (mark-marker) (+ 0 (car mark-ring)) (current-buffer)) |
19d35374 | 2490 | (deactivate-mark) |
2076c87c | 2491 | (move-marker (car mark-ring) nil) |
9a1277dd | 2492 | (if (null (mark t)) (ding)) |
2076c87c JB |
2493 | (setq mark-ring (cdr mark-ring))))) |
2494 | ||
e462e42f | 2495 | (defalias 'exchange-dot-and-mark 'exchange-point-and-mark) |
868c2f49 | 2496 | (defun exchange-point-and-mark (&optional arg) |
af39530e RS |
2497 | "Put the mark where point is now, and point where the mark is now. |
2498 | This command works even when the mark is not active, | |
868c2f49 KS |
2499 | and it reactivates the mark. |
2500 | With prefix arg, `transient-mark-mode' is enabled temporarily." | |
2501 | (interactive "P") | |
2502 | (if arg | |
2503 | (if mark-active | |
2504 | (if (null transient-mark-mode) | |
2505 | (setq transient-mark-mode 'lambda)) | |
2506 | (setq arg nil))) | |
2507 | (unless arg | |
2508 | (let ((omark (mark t))) | |
2509 | (if (null omark) | |
2510 | (error "No mark set in this buffer")) | |
2511 | (set-mark (point)) | |
2512 | (goto-char omark) | |
2513 | nil))) | |
e23c2c21 | 2514 | |
6710df48 | 2515 | (define-minor-mode transient-mark-mode |
e23c2c21 | 2516 | "Toggle Transient Mark mode. |
b411b5fa | 2517 | With arg, turn Transient Mark mode on if arg is positive, off otherwise. |
e23c2c21 | 2518 | |
5dd1220d RS |
2519 | In Transient Mark mode, when the mark is active, the region is highlighted. |
2520 | Changing the buffer \"deactivates\" the mark. | |
2521 | So do certain other operations that set the mark | |
2522 | but whose main purpose is something else--for example, | |
cfa70244 EZ |
2523 | incremental search, \\[beginning-of-buffer], and \\[end-of-buffer]. |
2524 | ||
8e843bc4 EZ |
2525 | You can also deactivate the mark by typing \\[keyboard-quit] or |
2526 | \\[keyboard-escape-quit]. | |
1465c66b | 2527 | |
cfa70244 EZ |
2528 | Many commands change their behavior when Transient Mark mode is in effect |
2529 | and the mark is active, by acting on the region instead of their usual | |
4c5f7215 | 2530 | default part of the buffer's text. Examples of such commands include |
cfa70244 EZ |
2531 | \\[comment-dwim], \\[flush-lines], \\[ispell], \\[keep-lines], |
2532 | \\[query-replace], \\[query-replace-regexp], and \\[undo]. Invoke | |
c4e39bdd EZ |
2533 | \\[apropos-documentation] and type \"transient\" or \"mark.*active\" at |
2534 | the prompt, to see the documentation of commands which are sensitive to | |
2535 | the Transient Mark mode." | |
82dc968c | 2536 | :global t :group 'editing-basics :require nil) |
dc029f0b RM |
2537 | |
2538 | (defun pop-global-mark () | |
2539 | "Pop off global mark ring and jump to the top location." | |
2540 | (interactive) | |
52b6d445 RS |
2541 | ;; Pop entries which refer to non-existent buffers. |
2542 | (while (and global-mark-ring (not (marker-buffer (car global-mark-ring)))) | |
2543 | (setq global-mark-ring (cdr global-mark-ring))) | |
dc029f0b RM |
2544 | (or global-mark-ring |
2545 | (error "No global mark set")) | |
2546 | (let* ((marker (car global-mark-ring)) | |
2547 | (buffer (marker-buffer marker)) | |
2548 | (position (marker-position marker))) | |
34c31301 RS |
2549 | (setq global-mark-ring (nconc (cdr global-mark-ring) |
2550 | (list (car global-mark-ring)))) | |
dc029f0b RM |
2551 | (set-buffer buffer) |
2552 | (or (and (>= position (point-min)) | |
2553 | (<= position (point-max))) | |
2554 | (widen)) | |
2555 | (goto-char position) | |
2556 | (switch-to-buffer buffer))) | |
2d88b556 | 2557 | \f |
95791033 | 2558 | (defcustom next-line-add-newlines nil |
69c1dd37 RS |
2559 | "*If non-nil, `next-line' inserts newline to avoid `end of buffer' error." |
2560 | :type 'boolean | |
e1d6e383 | 2561 | :version "21.1" |
69c1dd37 | 2562 | :group 'editing-basics) |
38ebcf29 | 2563 | |
058d4999 | 2564 | (defun next-line (&optional arg) |
2076c87c JB |
2565 | "Move cursor vertically down ARG lines. |
2566 | If there is no character in the target line exactly under the current column, | |
2567 | the cursor is positioned after the character in that line which spans this | |
2568 | column, or at the end of the line if it is not long enough. | |
38ebcf29 | 2569 | If there is no line in the buffer after this one, behavior depends on the |
1a2c3941 RS |
2570 | value of `next-line-add-newlines'. If non-nil, it inserts a newline character |
2571 | to create a line, and moves the cursor to that line. Otherwise it moves the | |
e47d38f6 | 2572 | cursor to the end of the buffer. |
2076c87c JB |
2573 | |
2574 | The command \\[set-goal-column] can be used to create | |
85969cb1 RS |
2575 | a semipermanent goal column for this command. |
2576 | Then instead of trying to move exactly vertically (or as close as possible), | |
2577 | this command moves to the specified goal column (or as close as possible). | |
2578 | The goal column is stored in the variable `goal-column', which is nil | |
2579 | when there is no goal column. | |
2076c87c JB |
2580 | |
2581 | If you are thinking of using this in a Lisp program, consider | |
2582 | using `forward-line' instead. It is usually easier to use | |
2583 | and more reliable (no dependence on goal column, etc.)." | |
2584 | (interactive "p") | |
058d4999 | 2585 | (unless arg (setq arg 1)) |
028922cf | 2586 | (if (and next-line-add-newlines (= arg 1)) |
207d7545 GM |
2587 | (if (save-excursion (end-of-line) (eobp)) |
2588 | ;; When adding a newline, don't expand an abbrev. | |
2589 | (let ((abbrev-mode nil)) | |
24886813 GM |
2590 | (end-of-line) |
2591 | (insert "\n")) | |
207d7545 | 2592 | (line-move arg)) |
1a2c3941 RS |
2593 | (if (interactive-p) |
2594 | (condition-case nil | |
2595 | (line-move arg) | |
2596 | ((beginning-of-buffer end-of-buffer) (ding))) | |
2597 | (line-move arg))) | |
2076c87c JB |
2598 | nil) |
2599 | ||
058d4999 | 2600 | (defun previous-line (&optional arg) |
2076c87c JB |
2601 | "Move cursor vertically up ARG lines. |
2602 | If there is no character in the target line exactly over the current column, | |
2603 | the cursor is positioned after the character in that line which spans this | |
2604 | column, or at the end of the line if it is not long enough. | |
2605 | ||
2606 | The command \\[set-goal-column] can be used to create | |
85969cb1 RS |
2607 | a semipermanent goal column for this command. |
2608 | Then instead of trying to move exactly vertically (or as close as possible), | |
2609 | this command moves to the specified goal column (or as close as possible). | |
2610 | The goal column is stored in the variable `goal-column', which is nil | |
2611 | when there is no goal column. | |
2076c87c JB |
2612 | |
2613 | If you are thinking of using this in a Lisp program, consider using | |
c2e8a012 | 2614 | `forward-line' with a negative argument instead. It is usually easier |
2076c87c JB |
2615 | to use and more reliable (no dependence on goal column, etc.)." |
2616 | (interactive "p") | |
058d4999 | 2617 | (unless arg (setq arg 1)) |
1a2c3941 RS |
2618 | (if (interactive-p) |
2619 | (condition-case nil | |
2620 | (line-move (- arg)) | |
2621 | ((beginning-of-buffer end-of-buffer) (ding))) | |
2622 | (line-move (- arg))) | |
2076c87c | 2623 | nil) |
eaae8106 | 2624 | |
69c1dd37 | 2625 | (defcustom track-eol nil |
2076c87c JB |
2626 | "*Non-nil means vertical motion starting at end of line keeps to ends of lines. |
2627 | This means moving to the end of each line moved onto. | |
69c1dd37 RS |
2628 | The beginning of a blank line does not count as the end of a line." |
2629 | :type 'boolean | |
2630 | :group 'editing-basics) | |
2631 | ||
2632 | (defcustom goal-column nil | |
2633 | "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil." | |
2634 | :type '(choice integer | |
2635 | (const :tag "None" nil)) | |
2636 | :group 'editing-basics) | |
912c6728 | 2637 | (make-variable-buffer-local 'goal-column) |
2076c87c JB |
2638 | |
2639 | (defvar temporary-goal-column 0 | |
2640 | "Current goal column for vertical motion. | |
2641 | It is the column where point was | |
2642 | at the start of current run of vertical motion commands. | |
c637ae6f | 2643 | When the `track-eol' feature is doing its job, the value is 9999.") |
2076c87c | 2644 | |
69c1dd37 | 2645 | (defcustom line-move-ignore-invisible nil |
098fc1fb | 2646 | "*Non-nil means \\[next-line] and \\[previous-line] ignore invisible lines. |
69c1dd37 RS |
2647 | Outline mode sets this." |
2648 | :type 'boolean | |
2649 | :group 'editing-basics) | |
098fc1fb | 2650 | |
af894fc9 RS |
2651 | (defun line-move-invisible (pos) |
2652 | "Return non-nil if the character after POS is currently invisible." | |
2653 | (let ((prop | |
2654 | (get-char-property pos 'invisible))) | |
2655 | (if (eq buffer-invisibility-spec t) | |
2656 | prop | |
2657 | (or (memq prop buffer-invisibility-spec) | |
2658 | (assq prop buffer-invisibility-spec))))) | |
2659 | ||
8c745744 RS |
2660 | ;; This is the guts of next-line and previous-line. |
2661 | ;; Arg says how many lines to move. | |
2076c87c | 2662 | (defun line-move (arg) |
2596511d RS |
2663 | ;; Don't run any point-motion hooks, and disregard intangibility, |
2664 | ;; for intermediate positions. | |
2665 | (let ((inhibit-point-motion-hooks t) | |
2666 | (opoint (point)) | |
6c8499b9 | 2667 | new line-end line-beg) |
2596511d RS |
2668 | (unwind-protect |
2669 | (progn | |
41d22ee0 | 2670 | (if (not (memq last-command '(next-line previous-line))) |
2596511d RS |
2671 | (setq temporary-goal-column |
2672 | (if (and track-eol (eolp) | |
2673 | ;; Don't count beg of empty line as end of line | |
2674 | ;; unless we just did explicit end-of-line. | |
2675 | (or (not (bolp)) (eq last-command 'end-of-line))) | |
2676 | 9999 | |
2677 | (current-column)))) | |
2678 | (if (and (not (integerp selective-display)) | |
2679 | (not line-move-ignore-invisible)) | |
2680 | ;; Use just newline characters. | |
e9cd25fe | 2681 | ;; Set ARG to 0 if we move as many lines as requested. |
2596511d RS |
2682 | (or (if (> arg 0) |
2683 | (progn (if (> arg 1) (forward-line (1- arg))) | |
2684 | ;; This way of moving forward ARG lines | |
2685 | ;; verifies that we have a newline after the last one. | |
2686 | ;; It doesn't get confused by intangible text. | |
2687 | (end-of-line) | |
e9cd25fe RS |
2688 | (if (zerop (forward-line 1)) |
2689 | (setq arg 0))) | |
2596511d | 2690 | (and (zerop (forward-line arg)) |
e9cd25fe RS |
2691 | (bolp) |
2692 | (setq arg 0))) | |
2596511d RS |
2693 | (signal (if (< arg 0) |
2694 | 'beginning-of-buffer | |
e9cd25fe | 2695 | 'end-of-buffer) |
2596511d RS |
2696 | nil)) |
2697 | ;; Move by arg lines, but ignore invisible ones. | |
2698 | (while (> arg 0) | |
af894fc9 RS |
2699 | ;; If the following character is currently invisible, |
2700 | ;; skip all characters with that same `invisible' property value. | |
2701 | (while (and (not (eobp)) (line-move-invisible (point))) | |
2702 | (goto-char (next-char-property-change (point)))) | |
2703 | ;; Now move a line. | |
2596511d RS |
2704 | (end-of-line) |
2705 | (and (zerop (vertical-motion 1)) | |
2706 | (signal 'end-of-buffer nil)) | |
2596511d RS |
2707 | (setq arg (1- arg))) |
2708 | (while (< arg 0) | |
2709 | (beginning-of-line) | |
2710 | (and (zerop (vertical-motion -1)) | |
2711 | (signal 'beginning-of-buffer nil)) | |
af894fc9 RS |
2712 | (setq arg (1+ arg)) |
2713 | (while (and (not (bobp)) (line-move-invisible (1- (point)))) | |
2714 | (goto-char (previous-char-property-change (point))))))) | |
2715 | ||
e9cd25fe RS |
2716 | (cond ((> arg 0) |
2717 | ;; If we did not move down as far as desired, | |
2718 | ;; at least go to end of line. | |
2719 | (end-of-line)) | |
2720 | ((< arg 0) | |
2721 | ;; If we did not move down as far as desired, | |
2722 | ;; at least go to end of line. | |
2723 | (beginning-of-line)) | |
2724 | (t | |
2725 | (line-move-finish (or goal-column temporary-goal-column) opoint))))) | |
2596511d | 2726 | nil) |
2076c87c | 2727 | |
af894fc9 RS |
2728 | (defun line-move-finish (column opoint) |
2729 | (let ((repeat t)) | |
2730 | (while repeat | |
2731 | ;; Set REPEAT to t to repeat the whole thing. | |
2732 | (setq repeat nil) | |
2733 | ||
1f980920 | 2734 | (let (new |
af894fc9 | 2735 | (line-beg (save-excursion (beginning-of-line) (point))) |
1f980920 RS |
2736 | (line-end |
2737 | ;; Compute the end of the line | |
2738 | ;; ignoring effectively intangible newlines. | |
85fd1cfa MB |
2739 | (let ((inhibit-point-motion-hooks nil) |
2740 | (inhibit-field-text-motion t)) | |
1f980920 RS |
2741 | (save-excursion (end-of-line) (point))))) |
2742 | ||
2743 | ;; Move to the desired column. | |
2744 | (line-move-to-column column) | |
2745 | (setq new (point)) | |
af894fc9 RS |
2746 | |
2747 | ;; Process intangibility within a line. | |
2748 | ;; Move to the chosen destination position from above, | |
2749 | ;; with intangibility processing enabled. | |
2750 | ||
2751 | (goto-char (point-min)) | |
2752 | (let ((inhibit-point-motion-hooks nil)) | |
2753 | (goto-char new) | |
2754 | ||
2755 | ;; If intangibility moves us to a different (later) place | |
2756 | ;; in the same line, use that as the destination. | |
2757 | (if (<= (point) line-end) | |
1f980920 RS |
2758 | (setq new (point)) |
2759 | ;; If that position is "too late", | |
2760 | ;; try the previous allowable position. | |
2761 | ;; See if it is ok. | |
2762 | (backward-char) | |
2763 | (if (<= (point) line-end) | |
2764 | (setq new (point)) | |
2765 | ;; As a last resort, use the end of the line. | |
2766 | (setq new line-end)))) | |
af894fc9 RS |
2767 | |
2768 | ;; Now move to the updated destination, processing fields | |
2769 | ;; as well as intangibility. | |
2770 | (goto-char opoint) | |
2771 | (let ((inhibit-point-motion-hooks nil)) | |
2772 | (goto-char | |
2773 | (constrain-to-field new opoint nil t | |
2774 | 'inhibit-line-move-field-capture))) | |
2775 | ||
1f980920 | 2776 | ;; If all this moved us to a different line, |
af894fc9 RS |
2777 | ;; retry everything within that new line. |
2778 | (when (or (< (point) line-beg) (> (point) line-end)) | |
2779 | ;; Repeat the intangibility and field processing. | |
2780 | (setq repeat t)))))) | |
2781 | ||
2782 | (defun line-move-to-column (col) | |
2783 | "Try to find column COL, considering invisibility. | |
2784 | This function works only in certain cases, | |
2785 | because what we really need is for `move-to-column' | |
2786 | and `current-column' to be able to ignore invisible text." | |
a615252b RS |
2787 | (if (zerop col) |
2788 | (beginning-of-line) | |
2789 | (move-to-column col)) | |
af894fc9 RS |
2790 | |
2791 | (when (and line-move-ignore-invisible | |
2792 | (not (bolp)) (line-move-invisible (1- (point)))) | |
2793 | (let ((normal-location (point)) | |
2794 | (normal-column (current-column))) | |
2795 | ;; If the following character is currently invisible, | |
2796 | ;; skip all characters with that same `invisible' property value. | |
2797 | (while (and (not (eobp)) | |
2798 | (line-move-invisible (point))) | |
2799 | (goto-char (next-char-property-change (point)))) | |
2800 | ;; Have we advanced to a larger column position? | |
2801 | (if (> (current-column) normal-column) | |
2802 | ;; We have made some progress towards the desired column. | |
2803 | ;; See if we can make any further progress. | |
2804 | (line-move-to-column (+ (current-column) (- col normal-column))) | |
2805 | ;; Otherwise, go to the place we originally found | |
2806 | ;; and move back over invisible text. | |
2807 | ;; that will get us to the same place on the screen | |
2808 | ;; but with a more reasonable buffer position. | |
2809 | (goto-char normal-location) | |
2810 | (let ((line-beg (save-excursion (beginning-of-line) (point)))) | |
2811 | (while (and (not (bolp)) (line-move-invisible (1- (point)))) | |
2812 | (goto-char (previous-char-property-change (point) line-beg)))))))) | |
2813 | ||
d5ab2033 JB |
2814 | ;;; Many people have said they rarely use this feature, and often type |
2815 | ;;; it by accident. Maybe it shouldn't even be on a key. | |
2816 | (put 'set-goal-column 'disabled t) | |
2076c87c JB |
2817 | |
2818 | (defun set-goal-column (arg) | |
2819 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. | |
2820 | Those commands will move to this position in the line moved to | |
2821 | rather than trying to keep the same horizontal position. | |
2822 | With a non-nil argument, clears out the goal column | |
912c6728 RS |
2823 | so that \\[next-line] and \\[previous-line] resume vertical motion. |
2824 | The goal column is stored in the variable `goal-column'." | |
2076c87c JB |
2825 | (interactive "P") |
2826 | (if arg | |
2827 | (progn | |
2828 | (setq goal-column nil) | |
2829 | (message "No goal column")) | |
2830 | (setq goal-column (current-column)) | |
2831 | (message (substitute-command-keys | |
2832 | "Goal column %d (use \\[set-goal-column] with an arg to unset it)") | |
2833 | goal-column)) | |
2834 | nil) | |
2d88b556 | 2835 | \f |
7492f5a6 RS |
2836 | |
2837 | (defun scroll-other-window-down (lines) | |
e47d38f6 RS |
2838 | "Scroll the \"other window\" down. |
2839 | For more details, see the documentation for `scroll-other-window'." | |
7492f5a6 RS |
2840 | (interactive "P") |
2841 | (scroll-other-window | |
2842 | ;; Just invert the argument's meaning. | |
2843 | ;; We can do that without knowing which window it will be. | |
2844 | (if (eq lines '-) nil | |
2845 | (if (null lines) '- | |
2846 | (- (prefix-numeric-value lines)))))) | |
e47d38f6 | 2847 | (define-key esc-map [?\C-\S-v] 'scroll-other-window-down) |
3aef9604 RS |
2848 | |
2849 | (defun beginning-of-buffer-other-window (arg) | |
2850 | "Move point to the beginning of the buffer in the other window. | |
2851 | Leave mark at previous position. | |
2852 | With arg N, put point N/10 of the way from the true beginning." | |
2853 | (interactive "P") | |
2854 | (let ((orig-window (selected-window)) | |
2855 | (window (other-window-for-scrolling))) | |
2856 | ;; We use unwind-protect rather than save-window-excursion | |
2857 | ;; because the latter would preserve the things we want to change. | |
2858 | (unwind-protect | |
2859 | (progn | |
2860 | (select-window window) | |
2861 | ;; Set point and mark in that window's buffer. | |
2862 | (beginning-of-buffer arg) | |
2863 | ;; Set point accordingly. | |
2864 | (recenter '(t))) | |
2865 | (select-window orig-window)))) | |
2866 | ||
2867 | (defun end-of-buffer-other-window (arg) | |
2868 | "Move point to the end of the buffer in the other window. | |
2869 | Leave mark at previous position. | |
2870 | With arg N, put point N/10 of the way from the true end." | |
2871 | (interactive "P") | |
2872 | ;; See beginning-of-buffer-other-window for comments. | |
2873 | (let ((orig-window (selected-window)) | |
2874 | (window (other-window-for-scrolling))) | |
2875 | (unwind-protect | |
2876 | (progn | |
2877 | (select-window window) | |
4500ff36 | 2878 | (end-of-buffer arg) |
3aef9604 RS |
2879 | (recenter '(t))) |
2880 | (select-window orig-window)))) | |
2d88b556 | 2881 | \f |
2076c87c JB |
2882 | (defun transpose-chars (arg) |
2883 | "Interchange characters around point, moving forward one character. | |
2884 | With prefix arg ARG, effect is to take character before point | |
2885 | and drag it forward past ARG other characters (backward if ARG negative). | |
2886 | If no argument and at end of line, the previous two chars are exchanged." | |
2887 | (interactive "*P") | |
2888 | (and (null arg) (eolp) (forward-char -1)) | |
2889 | (transpose-subr 'forward-char (prefix-numeric-value arg))) | |
2890 | ||
2891 | (defun transpose-words (arg) | |
2892 | "Interchange words around point, leaving point at end of them. | |
2893 | With prefix arg ARG, effect is to take word before or around point | |
2894 | and drag it forward past ARG other words (backward if ARG negative). | |
2895 | If ARG is zero, the words around or after point and around or after mark | |
2896 | are interchanged." | |
41d22ee0 | 2897 | ;; FIXME: `foo a!nd bar' should transpose into `bar and foo'. |
2076c87c JB |
2898 | (interactive "*p") |
2899 | (transpose-subr 'forward-word arg)) | |
2900 | ||
2901 | (defun transpose-sexps (arg) | |
2902 | "Like \\[transpose-words] but applies to sexps. | |
2903 | Does not work on a sexp that point is in the middle of | |
2904 | if it is a list or string." | |
2905 | (interactive "*p") | |
41d22ee0 SM |
2906 | (transpose-subr |
2907 | (lambda (arg) | |
2908 | ;; Here we should try to simulate the behavior of | |
2909 | ;; (cons (progn (forward-sexp x) (point)) | |
2910 | ;; (progn (forward-sexp (- x)) (point))) | |
2911 | ;; Except that we don't want to rely on the second forward-sexp | |
2912 | ;; putting us back to where we want to be, since forward-sexp-function | |
2913 | ;; might do funny things like infix-precedence. | |
2914 | (if (if (> arg 0) | |
2915 | (looking-at "\\sw\\|\\s_") | |
2916 | (and (not (bobp)) | |
2917 | (save-excursion (forward-char -1) (looking-at "\\sw\\|\\s_")))) | |
2918 | ;; Jumping over a symbol. We might be inside it, mind you. | |
2919 | (progn (funcall (if (> arg 0) | |
2920 | 'skip-syntax-backward 'skip-syntax-forward) | |
2921 | "w_") | |
2922 | (cons (save-excursion (forward-sexp arg) (point)) (point))) | |
2923 | ;; Otherwise, we're between sexps. Take a step back before jumping | |
2924 | ;; to make sure we'll obey the same precedence no matter which direction | |
2925 | ;; we're going. | |
2926 | (funcall (if (> arg 0) 'skip-syntax-backward 'skip-syntax-forward) " .") | |
2927 | (cons (save-excursion (forward-sexp arg) (point)) | |
2928 | (progn (while (or (forward-comment (if (> arg 0) 1 -1)) | |
2929 | (not (zerop (funcall (if (> arg 0) | |
2930 | 'skip-syntax-forward | |
2931 | 'skip-syntax-backward) | |
2932 | "."))))) | |
2933 | (point))))) | |
2934 | arg 'special)) | |
2076c87c JB |
2935 | |
2936 | (defun transpose-lines (arg) | |
2937 | "Exchange current line and previous line, leaving point after both. | |
2938 | With argument ARG, takes previous line and moves it past ARG lines. | |
2939 | With argument 0, interchanges line point is in with line mark is in." | |
2940 | (interactive "*p") | |
2941 | (transpose-subr (function | |
2942 | (lambda (arg) | |
d3f4ef3f | 2943 | (if (> arg 0) |
2076c87c | 2944 | (progn |
d3f4ef3f AS |
2945 | ;; Move forward over ARG lines, |
2946 | ;; but create newlines if necessary. | |
2947 | (setq arg (forward-line arg)) | |
2948 | (if (/= (preceding-char) ?\n) | |
2949 | (setq arg (1+ arg))) | |
2950 | (if (> arg 0) | |
2951 | (newline arg))) | |
2076c87c JB |
2952 | (forward-line arg)))) |
2953 | arg)) | |
2954 | ||
e1e04350 SM |
2955 | (defun transpose-subr (mover arg &optional special) |
2956 | (let ((aux (if special mover | |
2957 | (lambda (x) | |
2958 | (cons (progn (funcall mover x) (point)) | |
2959 | (progn (funcall mover (- x)) (point)))))) | |
2960 | pos1 pos2) | |
2961 | (cond | |
2962 | ((= arg 0) | |
2963 | (save-excursion | |
2964 | (setq pos1 (funcall aux 1)) | |
2965 | (goto-char (mark)) | |
2966 | (setq pos2 (funcall aux 1)) | |
2967 | (transpose-subr-1 pos1 pos2)) | |
2968 | (exchange-point-and-mark)) | |
2969 | ((> arg 0) | |
2970 | (setq pos1 (funcall aux -1)) | |
2971 | (setq pos2 (funcall aux arg)) | |
2972 | (transpose-subr-1 pos1 pos2) | |
2973 | (goto-char (car pos2))) | |
2974 | (t | |
2975 | (setq pos1 (funcall aux -1)) | |
2976 | (goto-char (car pos1)) | |
2977 | (setq pos2 (funcall aux arg)) | |
2978 | (transpose-subr-1 pos1 pos2))))) | |
2979 | ||
2980 | (defun transpose-subr-1 (pos1 pos2) | |
2981 | (when (> (car pos1) (cdr pos1)) (setq pos1 (cons (cdr pos1) (car pos1)))) | |
2982 | (when (> (car pos2) (cdr pos2)) (setq pos2 (cons (cdr pos2) (car pos2)))) | |
2983 | (when (> (car pos1) (car pos2)) | |
2984 | (let ((swap pos1)) | |
2985 | (setq pos1 pos2 pos2 swap))) | |
2986 | (if (> (cdr pos1) (car pos2)) (error "Don't have two things to transpose")) | |
dc7d7552 RS |
2987 | (atomic-change-group |
2988 | (let (word2) | |
2989 | (setq word2 (delete-and-extract-region (car pos2) (cdr pos2))) | |
2990 | (goto-char (car pos2)) | |
2991 | (insert (delete-and-extract-region (car pos1) (cdr pos1))) | |
2992 | (goto-char (car pos1)) | |
2993 | (insert word2)))) | |
2d88b556 | 2994 | \f |
2076c87c | 2995 | (defun backward-word (arg) |
b7e91b0c | 2996 | "Move backward until encountering the beginning of a word. |
20ecc110 | 2997 | With argument, do this that many times." |
9e50756b | 2998 | (interactive "p") |
2076c87c JB |
2999 | (forward-word (- arg))) |
3000 | ||
3001 | (defun mark-word (arg) | |
cad113ae KG |
3002 | "Set mark arg words away from point. |
3003 | If this command is repeated, it marks the next ARG words after the ones | |
3004 | already marked." | |
2076c87c | 3005 | (interactive "p") |
cad113ae KG |
3006 | (cond ((and (eq last-command this-command) (mark t)) |
3007 | (set-mark | |
3008 | (save-excursion | |
3009 | (goto-char (mark)) | |
3010 | (forward-word arg) | |
3011 | (point)))) | |
3012 | (t | |
3013 | (push-mark | |
3014 | (save-excursion | |
3015 | (forward-word arg) | |
3016 | (point)) | |
3017 | nil t)))) | |
2076c87c JB |
3018 | |
3019 | (defun kill-word (arg) | |
3020 | "Kill characters forward until encountering the end of a word. | |
3021 | With argument, do this that many times." | |
e761e42c | 3022 | (interactive "p") |
89ee2bf6 | 3023 | (kill-region (point) (progn (forward-word arg) (point)))) |
2076c87c JB |
3024 | |
3025 | (defun backward-kill-word (arg) | |
3026 | "Kill characters backward until encountering the end of a word. | |
3027 | With argument, do this that many times." | |
e761e42c | 3028 | (interactive "p") |
2076c87c | 3029 | (kill-word (- arg))) |
d7c64071 | 3030 | |
1e8c5ac4 RS |
3031 | (defun current-word (&optional strict) |
3032 | "Return the word point is on (or a nearby word) as a string. | |
3033 | If optional arg STRICT is non-nil, return nil unless point is within | |
3034 | or adjacent to a word." | |
d7c64071 ER |
3035 | (save-excursion |
3036 | (let ((oldpoint (point)) (start (point)) (end (point))) | |
3037 | (skip-syntax-backward "w_") (setq start (point)) | |
3038 | (goto-char oldpoint) | |
3039 | (skip-syntax-forward "w_") (setq end (point)) | |
3040 | (if (and (eq start oldpoint) (eq end oldpoint)) | |
1e8c5ac4 RS |
3041 | ;; Point is neither within nor adjacent to a word. |
3042 | (and (not strict) | |
3043 | (progn | |
3044 | ;; Look for preceding word in same line. | |
3045 | (skip-syntax-backward "^w_" | |
3046 | (save-excursion (beginning-of-line) | |
3047 | (point))) | |
3048 | (if (bolp) | |
3049 | ;; No preceding word in same line. | |
3050 | ;; Look for following word in same line. | |
3051 | (progn | |
3052 | (skip-syntax-forward "^w_" | |
3053 | (save-excursion (end-of-line) | |
3054 | (point))) | |
3055 | (setq start (point)) | |
3056 | (skip-syntax-forward "w_") | |
3057 | (setq end (point))) | |
3058 | (setq end (point)) | |
3059 | (skip-syntax-backward "w_") | |
3060 | (setq start (point))) | |
020db25f RS |
3061 | (buffer-substring-no-properties start end))) |
3062 | (buffer-substring-no-properties start end))))) | |
2d88b556 | 3063 | \f |
69c1dd37 | 3064 | (defcustom fill-prefix nil |
e1e04350 | 3065 | "*String for filling to insert at front of new line, or nil for none." |
69c1dd37 RS |
3066 | :type '(choice (const :tag "None" nil) |
3067 | string) | |
3068 | :group 'fill) | |
2076c87c JB |
3069 | (make-variable-buffer-local 'fill-prefix) |
3070 | ||
69c1dd37 RS |
3071 | (defcustom auto-fill-inhibit-regexp nil |
3072 | "*Regexp to match lines which should not be auto-filled." | |
3073 | :type '(choice (const :tag "None" nil) | |
3074 | regexp) | |
3075 | :group 'fill) | |
2076c87c | 3076 | |
58dd38f1 | 3077 | (defvar comment-line-break-function 'comment-indent-new-line |
b3ac9fa9 RS |
3078 | "*Mode-specific function which line breaks and continues a comment. |
3079 | ||
3080 | This function is only called during auto-filling of a comment section. | |
3081 | The function should take a single optional argument, which is a flag | |
3082 | indicating whether it should use soft newlines. | |
3083 | ||
3084 | Setting this variable automatically makes it local to the current buffer.") | |
3085 | ||
dbe524b6 | 3086 | ;; This function is used as the auto-fill-function of a buffer |
e2504204 KH |
3087 | ;; when Auto-Fill mode is enabled. |
3088 | ;; It returns t if it really did any work. | |
dbe524b6 RS |
3089 | ;; (Actually some major modes use a different auto-fill function, |
3090 | ;; but this one is the default one.) | |
2076c87c | 3091 | (defun do-auto-fill () |
a0170800 RS |
3092 | (let (fc justify bol give-up |
3093 | (fill-prefix fill-prefix)) | |
c18465c4 | 3094 | (if (or (not (setq justify (current-justification))) |
8f066a20 RS |
3095 | (null (setq fc (current-fill-column))) |
3096 | (and (eq justify 'left) | |
3097 | (<= (current-column) fc)) | |
1e722f9f | 3098 | (save-excursion (beginning-of-line) |
eed5698b RS |
3099 | (setq bol (point)) |
3100 | (and auto-fill-inhibit-regexp | |
3101 | (looking-at auto-fill-inhibit-regexp)))) | |
3102 | nil ;; Auto-filling not required | |
3db1e3b5 BG |
3103 | (if (memq justify '(full center right)) |
3104 | (save-excursion (unjustify-current-line))) | |
a0170800 RS |
3105 | |
3106 | ;; Choose a fill-prefix automatically. | |
e1e04350 SM |
3107 | (when (and adaptive-fill-mode |
3108 | (or (null fill-prefix) (string= fill-prefix ""))) | |
3109 | (let ((prefix | |
3110 | (fill-context-prefix | |
3111 | (save-excursion (backward-paragraph 1) (point)) | |
3112 | (save-excursion (forward-paragraph 1) (point))))) | |
3113 | (and prefix (not (equal prefix "")) | |
3114 | ;; Use auto-indentation rather than a guessed empty prefix. | |
0e53a373 | 3115 | (not (and fill-indent-according-to-mode |
d99f8496 | 3116 | (string-match "\\`[ \t]*\\'" prefix))) |
e1e04350 SM |
3117 | (setq fill-prefix prefix)))) |
3118 | ||
eed5698b | 3119 | (while (and (not give-up) (> (current-column) fc)) |
e47d38f6 | 3120 | ;; Determine where to split the line. |
db893d00 RS |
3121 | (let* (after-prefix |
3122 | (fill-point | |
41d22ee0 | 3123 | (let ((opoint (point))) |
db893d00 RS |
3124 | (save-excursion |
3125 | (beginning-of-line) | |
3126 | (setq after-prefix (point)) | |
3127 | (and fill-prefix | |
3128 | (looking-at (regexp-quote fill-prefix)) | |
3129 | (setq after-prefix (match-end 0))) | |
3130 | (move-to-column (1+ fc)) | |
41d22ee0 | 3131 | (fill-move-to-break-point after-prefix) |
db893d00 RS |
3132 | (point))))) |
3133 | ||
3134 | ;; See whether the place we found is any good. | |
e47d38f6 RS |
3135 | (if (save-excursion |
3136 | (goto-char fill-point) | |
41d22ee0 SM |
3137 | (or (bolp) |
3138 | ;; There is no use breaking at end of line. | |
3139 | (save-excursion (skip-chars-forward " ") (eolp)) | |
3140 | ;; It is futile to split at the end of the prefix | |
3141 | ;; since we would just insert the prefix again. | |
3142 | (and after-prefix (<= (point) after-prefix)) | |
3143 | ;; Don't split right after a comment starter | |
3144 | ;; since we would just make another comment starter. | |
3145 | (and comment-start-skip | |
3146 | (let ((limit (point))) | |
3147 | (beginning-of-line) | |
3148 | (and (re-search-forward comment-start-skip | |
3149 | limit t) | |
3150 | (eq (point) limit)))))) | |
3151 | ;; No good place to break => stop trying. | |
3152 | (setq give-up t) | |
3153 | ;; Ok, we have a useful place to break the line. Do it. | |
3154 | (let ((prev-column (current-column))) | |
3155 | ;; If point is at the fill-point, do not `save-excursion'. | |
3156 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, | |
3157 | ;; point will end up before it rather than after it. | |
3158 | (if (save-excursion | |
3159 | (skip-chars-backward " \t") | |
3160 | (= (point) fill-point)) | |
3161 | (funcall comment-line-break-function t) | |
3162 | (save-excursion | |
3163 | (goto-char fill-point) | |
3164 | (funcall comment-line-break-function t))) | |
3165 | ;; Now do justification, if required | |
3166 | (if (not (eq justify 'left)) | |
e47d38f6 | 3167 | (save-excursion |
e1e04350 SM |
3168 | (end-of-line 0) |
3169 | (justify-current-line justify nil t))) | |
41d22ee0 SM |
3170 | ;; If making the new line didn't reduce the hpos of |
3171 | ;; the end of the line, then give up now; | |
3172 | ;; trying again will not help. | |
3173 | (if (>= (current-column) prev-column) | |
3174 | (setq give-up t)))))) | |
24ebf92e | 3175 | ;; Justify last line. |
e2504204 | 3176 | (justify-current-line justify t t) |
1e722f9f | 3177 | t))) |
2076c87c | 3178 | |
24ebf92e RS |
3179 | (defvar normal-auto-fill-function 'do-auto-fill |
3180 | "The function to use for `auto-fill-function' if Auto Fill mode is turned on. | |
3181 | Some major modes set this.") | |
3182 | ||
d99f8496 SM |
3183 | ;; FIXME: turn into a proper minor mode. |
3184 | ;; Add a global minor mode version of it. | |
d7465b15 | 3185 | (defun auto-fill-mode (&optional arg) |
24ebf92e RS |
3186 | "Toggle Auto Fill mode. |
3187 | With arg, turn Auto Fill mode on if and only if arg is positive. | |
3188 | In Auto Fill mode, inserting a space at a column beyond `current-fill-column' | |
3189 | automatically breaks the line at a previous space. | |
3190 | ||
3191 | The value of `normal-auto-fill-function' specifies the function to use | |
3192 | for `auto-fill-function' when turning Auto Fill mode on." | |
d7465b15 RS |
3193 | (interactive "P") |
3194 | (prog1 (setq auto-fill-function | |
3195 | (if (if (null arg) | |
3196 | (not auto-fill-function) | |
3197 | (> (prefix-numeric-value arg) 0)) | |
24ebf92e | 3198 | normal-auto-fill-function |
d7465b15 | 3199 | nil)) |
7911ecc8 | 3200 | (force-mode-line-update))) |
d7465b15 RS |
3201 | |
3202 | ;; This holds a document string used to document auto-fill-mode. | |
3203 | (defun auto-fill-function () | |
3204 | "Automatically break line at a previous space, in insertion of text." | |
3205 | nil) | |
3206 | ||
3207 | (defun turn-on-auto-fill () | |
3208 | "Unconditionally turn on Auto Fill mode." | |
3209 | (auto-fill-mode 1)) | |
3a99c819 GM |
3210 | |
3211 | (defun turn-off-auto-fill () | |
3212 | "Unconditionally turn off Auto Fill mode." | |
3213 | (auto-fill-mode -1)) | |
3214 | ||
7cbf1dc1 | 3215 | (custom-add-option 'text-mode-hook 'turn-on-auto-fill) |
d7465b15 RS |
3216 | |
3217 | (defun set-fill-column (arg) | |
4cc0ea11 | 3218 | "Set `fill-column' to specified argument. |
923efb99 | 3219 | Use \\[universal-argument] followed by a number to specify a column. |
4cc0ea11 | 3220 | Just \\[universal-argument] as argument means to use the current column." |
d7465b15 | 3221 | (interactive "P") |
f4520363 RS |
3222 | (if (consp arg) |
3223 | (setq arg (current-column))) | |
3224 | (if (not (integerp arg)) | |
3225 | ;; Disallow missing argument; it's probably a typo for C-x C-f. | |
3226 | (error "set-fill-column requires an explicit argument") | |
3227 | (message "Fill column set to %d (was %d)" arg fill-column) | |
3228 | (setq fill-column arg))) | |
2d88b556 | 3229 | \f |
2076c87c | 3230 | (defun set-selective-display (arg) |
ff1fbe3e RS |
3231 | "Set `selective-display' to ARG; clear it if no arg. |
3232 | When the value of `selective-display' is a number > 0, | |
3233 | lines whose indentation is >= that value are not displayed. | |
3234 | The variable `selective-display' has a separate value for each buffer." | |
2076c87c JB |
3235 | (interactive "P") |
3236 | (if (eq selective-display t) | |
3237 | (error "selective-display already in use for marked lines")) | |
c88ab9ce ER |
3238 | (let ((current-vpos |
3239 | (save-restriction | |
3240 | (narrow-to-region (point-min) (point)) | |
3241 | (goto-char (window-start)) | |
3242 | (vertical-motion (window-height))))) | |
3243 | (setq selective-display | |
3244 | (and arg (prefix-numeric-value arg))) | |
3245 | (recenter current-vpos)) | |
2076c87c JB |
3246 | (set-window-start (selected-window) (window-start (selected-window))) |
3247 | (princ "selective-display set to " t) | |
3248 | (prin1 selective-display t) | |
3249 | (princ "." t)) | |
3250 | ||
40a64816 RS |
3251 | (defvaralias 'indicate-unused-lines 'indicate-empty-lines) |
3252 | (defvaralias 'default-indicate-unused-lines 'default-indicate-empty-lines) | |
3253 | ||
0bb64d76 PA |
3254 | (defun toggle-truncate-lines (arg) |
3255 | "Toggle whether to fold or truncate long lines on the screen. | |
46cdfe8f RS |
3256 | With arg, truncate long lines iff arg is positive. |
3257 | Note that in side-by-side windows, truncation is always enabled." | |
0bb64d76 PA |
3258 | (interactive "P") |
3259 | (setq truncate-lines | |
3260 | (if (null arg) | |
3261 | (not truncate-lines) | |
46cdfe8f RS |
3262 | (> (prefix-numeric-value arg) 0))) |
3263 | (force-mode-line-update) | |
4f017185 RS |
3264 | (unless truncate-lines |
3265 | (let ((buffer (current-buffer))) | |
3266 | (walk-windows (lambda (window) | |
3267 | (if (eq buffer (window-buffer window)) | |
3268 | (set-window-hscroll window 0))) | |
3269 | nil t))) | |
46cdfe8f RS |
3270 | (message "Truncate long lines %s" |
3271 | (if truncate-lines "enabled" "disabled"))) | |
0bb64d76 | 3272 | |
4f8f7f9f | 3273 | (defvar overwrite-mode-textual " Ovwrt" |
b6a22db0 | 3274 | "The string displayed in the mode line when in overwrite mode.") |
4f8f7f9f | 3275 | (defvar overwrite-mode-binary " Bin Ovwrt" |
b6a22db0 JB |
3276 | "The string displayed in the mode line when in binary overwrite mode.") |
3277 | ||
2076c87c JB |
3278 | (defun overwrite-mode (arg) |
3279 | "Toggle overwrite mode. | |
3280 | With arg, turn overwrite mode on iff arg is positive. | |
3281 | In overwrite mode, printing characters typed in replace existing text | |
b6a22db0 JB |
3282 | on a one-for-one basis, rather than pushing it to the right. At the |
3283 | end of a line, such characters extend the line. Before a tab, | |
3284 | such characters insert until the tab is filled in. | |
3285 | \\[quoted-insert] still inserts characters in overwrite mode; this | |
3286 | is supposed to make it easier to insert characters when necessary." | |
3287 | (interactive "P") | |
3288 | (setq overwrite-mode | |
3289 | (if (if (null arg) (not overwrite-mode) | |
3290 | (> (prefix-numeric-value arg) 0)) | |
3291 | 'overwrite-mode-textual)) | |
3292 | (force-mode-line-update)) | |
3293 | ||
3294 | (defun binary-overwrite-mode (arg) | |
3295 | "Toggle binary overwrite mode. | |
3296 | With arg, turn binary overwrite mode on iff arg is positive. | |
3297 | In binary overwrite mode, printing characters typed in replace | |
3298 | existing text. Newlines are not treated specially, so typing at the | |
3299 | end of a line joins the line to the next, with the typed character | |
3300 | between them. Typing before a tab character simply replaces the tab | |
3301 | with the character typed. | |
3302 | \\[quoted-insert] replaces the text at the cursor, just as ordinary | |
3303 | typing characters do. | |
3304 | ||
3305 | Note that binary overwrite mode is not its own minor mode; it is a | |
3306 | specialization of overwrite-mode, entered by setting the | |
3307 | `overwrite-mode' variable to `overwrite-mode-binary'." | |
2076c87c JB |
3308 | (interactive "P") |
3309 | (setq overwrite-mode | |
b6a22db0 | 3310 | (if (if (null arg) |
a61099dd | 3311 | (not (eq overwrite-mode 'overwrite-mode-binary)) |
b6a22db0 JB |
3312 | (> (prefix-numeric-value arg) 0)) |
3313 | 'overwrite-mode-binary)) | |
3314 | (force-mode-line-update)) | |
eaae8106 | 3315 | |
6710df48 | 3316 | (define-minor-mode line-number-mode |
a61099dd RS |
3317 | "Toggle Line Number mode. |
3318 | With arg, turn Line Number mode on iff arg is positive. | |
3319 | When Line Number mode is enabled, the line number appears | |
8dc9e2ef KH |
3320 | in the mode line. |
3321 | ||
32f2f98e EZ |
3322 | Line numbers do not appear for very large buffers and buffers |
3323 | with very long lines; see variables `line-number-display-limit' | |
3324 | and `line-number-display-limit-width'." | |
82dc968c | 3325 | :init-value t :global t :group 'editing-basics :require nil) |
bcad4985 | 3326 | |
6710df48 | 3327 | (define-minor-mode column-number-mode |
bcad4985 KH |
3328 | "Toggle Column Number mode. |
3329 | With arg, turn Column Number mode on iff arg is positive. | |
3330 | When Column Number mode is enabled, the column number appears | |
3331 | in the mode line." | |
82dc968c | 3332 | :global t :group 'editing-basics :require nil) |
2d88b556 | 3333 | \f |
4b384a8f | 3334 | (defgroup paren-blinking nil |
020db25f | 3335 | "Blinking matching of parens and expressions." |
4b384a8f SM |
3336 | :prefix "blink-matching-" |
3337 | :group 'paren-matching) | |
3338 | ||
69c1dd37 RS |
3339 | (defcustom blink-matching-paren t |
3340 | "*Non-nil means show matching open-paren when close-paren is inserted." | |
3341 | :type 'boolean | |
4b384a8f | 3342 | :group 'paren-blinking) |
2076c87c | 3343 | |
69c1dd37 | 3344 | (defcustom blink-matching-paren-on-screen t |
29fc44dd | 3345 | "*Non-nil means show matching open-paren when it is on screen. |
4b384a8f SM |
3346 | If nil, means don't show it (but the open-paren can still be shown |
3347 | when it is off screen)." | |
69c1dd37 | 3348 | :type 'boolean |
4b384a8f | 3349 | :group 'paren-blinking) |
29fc44dd | 3350 | |
4b384a8f | 3351 | (defcustom blink-matching-paren-distance (* 25 1024) |
69c1dd37 RS |
3352 | "*If non-nil, is maximum distance to search for matching open-paren." |
3353 | :type 'integer | |
4b384a8f | 3354 | :group 'paren-blinking) |
2076c87c | 3355 | |
69c1dd37 | 3356 | (defcustom blink-matching-delay 1 |
4b384a8f SM |
3357 | "*Time in seconds to delay after showing a matching paren." |
3358 | :type 'number | |
3359 | :group 'paren-blinking) | |
72dddf8b | 3360 | |
69c1dd37 | 3361 | (defcustom blink-matching-paren-dont-ignore-comments nil |
4b384a8f | 3362 | "*Non-nil means `blink-matching-paren' will not ignore comments." |
69c1dd37 | 3363 | :type 'boolean |
4b384a8f | 3364 | :group 'paren-blinking) |
903b7f65 | 3365 | |
2076c87c JB |
3366 | (defun blink-matching-open () |
3367 | "Move cursor momentarily to the beginning of the sexp before point." | |
3368 | (interactive) | |
3369 | (and (> (point) (1+ (point-min))) | |
2076c87c | 3370 | blink-matching-paren |
7e1ddd45 RS |
3371 | ;; Verify an even number of quoting characters precede the close. |
3372 | (= 1 (logand 1 (- (point) | |
3373 | (save-excursion | |
3374 | (forward-char -1) | |
3375 | (skip-syntax-backward "/\\") | |
3376 | (point))))) | |
2076c87c JB |
3377 | (let* ((oldpos (point)) |
3378 | (blinkpos) | |
3379 | (mismatch)) | |
3380 | (save-excursion | |
3381 | (save-restriction | |
3382 | (if blink-matching-paren-distance | |
3383 | (narrow-to-region (max (point-min) | |
3384 | (- (point) blink-matching-paren-distance)) | |
3385 | oldpos)) | |
3386 | (condition-case () | |
903b7f65 RS |
3387 | (let ((parse-sexp-ignore-comments |
3388 | (and parse-sexp-ignore-comments | |
3389 | (not blink-matching-paren-dont-ignore-comments)))) | |
3390 | (setq blinkpos (scan-sexps oldpos -1))) | |
2076c87c | 3391 | (error nil))) |
903b7f65 RS |
3392 | (and blinkpos |
3393 | (/= (char-syntax (char-after blinkpos)) | |
3394 | ?\$) | |
2076c87c | 3395 | (setq mismatch |
903b7f65 RS |
3396 | (or (null (matching-paren (char-after blinkpos))) |
3397 | (/= (char-after (1- oldpos)) | |
3398 | (matching-paren (char-after blinkpos)))))) | |
2076c87c JB |
3399 | (if mismatch (setq blinkpos nil)) |
3400 | (if blinkpos | |
a117eaee KH |
3401 | ;; Don't log messages about paren matching. |
3402 | (let (message-log-max) | |
2076c87c JB |
3403 | (goto-char blinkpos) |
3404 | (if (pos-visible-in-window-p) | |
29fc44dd KH |
3405 | (and blink-matching-paren-on-screen |
3406 | (sit-for blink-matching-delay)) | |
2076c87c JB |
3407 | (goto-char blinkpos) |
3408 | (message | |
3409 | "Matches %s" | |
e9f1d66d | 3410 | ;; Show what precedes the open in its line, if anything. |
2076c87c JB |
3411 | (if (save-excursion |
3412 | (skip-chars-backward " \t") | |
3413 | (not (bolp))) | |
3414 | (buffer-substring (progn (beginning-of-line) (point)) | |
3415 | (1+ blinkpos)) | |
e9f1d66d RS |
3416 | ;; Show what follows the open in its line, if anything. |
3417 | (if (save-excursion | |
3418 | (forward-char 1) | |
3419 | (skip-chars-forward " \t") | |
3420 | (not (eolp))) | |
3421 | (buffer-substring blinkpos | |
3422 | (progn (end-of-line) (point))) | |
267935b9 RS |
3423 | ;; Otherwise show the previous nonblank line, |
3424 | ;; if there is one. | |
3425 | (if (save-excursion | |
3426 | (skip-chars-backward "\n \t") | |
3427 | (not (bobp))) | |
3428 | (concat | |
3429 | (buffer-substring (progn | |
3430 | (skip-chars-backward "\n \t") | |
3431 | (beginning-of-line) | |
3432 | (point)) | |
3433 | (progn (end-of-line) | |
3434 | (skip-chars-backward " \t") | |
3435 | (point))) | |
3436 | ;; Replace the newline and other whitespace with `...'. | |
3437 | "..." | |
3438 | (buffer-substring blinkpos (1+ blinkpos))) | |
3439 | ;; There is nothing to show except the char itself. | |
3440 | (buffer-substring blinkpos (1+ blinkpos)))))))) | |
2076c87c JB |
3441 | (cond (mismatch |
3442 | (message "Mismatched parentheses")) | |
3443 | ((not blink-matching-paren-distance) | |
3444 | (message "Unmatched parenthesis")))))))) | |
3445 | ||
3446 | ;Turned off because it makes dbx bomb out. | |
3447 | (setq blink-paren-function 'blink-matching-open) | |
2d88b556 | 3448 | \f |
9a1277dd RS |
3449 | ;; This executes C-g typed while Emacs is waiting for a command. |
3450 | ;; Quitting out of a program does not go through here; | |
3451 | ;; that happens in the QUIT macro at the C code level. | |
2076c87c | 3452 | (defun keyboard-quit () |
d5dae4e1 | 3453 | "Signal a `quit' condition. |
af39530e RS |
3454 | During execution of Lisp code, this character causes a quit directly. |
3455 | At top-level, as an editor command, this simply beeps." | |
2076c87c | 3456 | (interactive) |
19d35374 | 3457 | (deactivate-mark) |
f5e13057 | 3458 | (setq defining-kbd-macro nil) |
2076c87c JB |
3459 | (signal 'quit nil)) |
3460 | ||
3461 | (define-key global-map "\C-g" 'keyboard-quit) | |
c66587fe | 3462 | |
1c6c6fde RS |
3463 | (defvar buffer-quit-function nil |
3464 | "Function to call to \"quit\" the current buffer, or nil if none. | |
3465 | \\[keyboard-escape-quit] calls this function when its more local actions | |
3466 | \(such as cancelling a prefix argument, minibuffer or region) do not apply.") | |
3467 | ||
c66587fe RS |
3468 | (defun keyboard-escape-quit () |
3469 | "Exit the current \"mode\" (in a generalized sense of the word). | |
3470 | This command can exit an interactive command such as `query-replace', | |
3471 | can clear out a prefix argument or a region, | |
3472 | can get out of the minibuffer or other recursive edit, | |
1c6c6fde RS |
3473 | cancel the use of the current buffer (for special-purpose buffers), |
3474 | or go back to just one window (by deleting all but the selected window)." | |
c66587fe RS |
3475 | (interactive) |
3476 | (cond ((eq last-command 'mode-exited) nil) | |
3477 | ((> (minibuffer-depth) 0) | |
3478 | (abort-recursive-edit)) | |
3479 | (current-prefix-arg | |
3480 | nil) | |
3481 | ((and transient-mark-mode | |
3482 | mark-active) | |
3483 | (deactivate-mark)) | |
1b657835 RS |
3484 | ((> (recursion-depth) 0) |
3485 | (exit-recursive-edit)) | |
1c6c6fde RS |
3486 | (buffer-quit-function |
3487 | (funcall buffer-quit-function)) | |
c66587fe | 3488 | ((not (one-window-p t)) |
1b657835 RS |
3489 | (delete-other-windows)) |
3490 | ((string-match "^ \\*" (buffer-name (current-buffer))) | |
3491 | (bury-buffer)))) | |
c66587fe | 3492 | |
2d88b556 RS |
3493 | (defun play-sound-file (file &optional volume device) |
3494 | "Play sound stored in FILE. | |
3495 | VOLUME and DEVICE correspond to the keywords of the sound | |
3496 | specification for `play-sound'." | |
3497 | (interactive "fPlay sound file: ") | |
3498 | (let ((sound (list :file file))) | |
3499 | (if volume | |
3500 | (plist-put sound :volume volume)) | |
3501 | (if device | |
3502 | (plist-put sound :device device)) | |
3503 | (push 'sound sound) | |
3504 | (play-sound sound))) | |
3505 | ||
1c6c6fde | 3506 | (define-key global-map "\e\e\e" 'keyboard-escape-quit) |
22e4ec98 | 3507 | |
7683b5c2 DL |
3508 | (defcustom read-mail-command 'rmail |
3509 | "*Your preference for a mail reading package. | |
9023837e DL |
3510 | This is used by some keybindings which support reading mail. |
3511 | See also `mail-user-agent' concerning sending mail." | |
7683b5c2 DL |
3512 | :type '(choice (function-item rmail) |
3513 | (function-item gnus) | |
3514 | (function-item mh-rmail) | |
3515 | (function :tag "Other")) | |
3516 | :version "21.1" | |
3517 | :group 'mail) | |
3518 | ||
69c1dd37 | 3519 | (defcustom mail-user-agent 'sendmail-user-agent |
a31ca314 | 3520 | "*Your preference for a mail composition package. |
9023837e | 3521 | Various Emacs Lisp packages (e.g. Reporter) require you to compose an |
a31ca314 RS |
3522 | outgoing email message. This variable lets you specify which |
3523 | mail-sending package you prefer. | |
3524 | ||
3525 | Valid values include: | |
3526 | ||
9023837e DL |
3527 | `sendmail-user-agent' -- use the default Emacs Mail package. |
3528 | See Info node `(emacs)Sending Mail'. | |
3529 | `mh-e-user-agent' -- use the Emacs interface to the MH mail system. | |
3530 | See Info node `(mh-e)'. | |
3531 | `message-user-agent' -- use the Gnus Message package. | |
3532 | See Info node `(message)'. | |
3533 | `gnus-user-agent' -- like `message-user-agent', but with Gnus | |
3534 | paraphernalia, particularly the Gcc: header for | |
3535 | archiving. | |
a31ca314 RS |
3536 | |
3537 | Additional valid symbols may be available; check with the author of | |
15d0c9b1 DL |
3538 | your package for details. The function should return non-nil if it |
3539 | succeeds. | |
9023837e DL |
3540 | |
3541 | See also `read-mail-command' concerning reading mail." | |
69c1dd37 RS |
3542 | :type '(radio (function-item :tag "Default Emacs mail" |
3543 | :format "%t\n" | |
3544 | sendmail-user-agent) | |
3545 | (function-item :tag "Emacs interface to MH" | |
3546 | :format "%t\n" | |
3547 | mh-e-user-agent) | |
9023837e | 3548 | (function-item :tag "Gnus Message package" |
69c1dd37 RS |
3549 | :format "%t\n" |
3550 | message-user-agent) | |
9023837e DL |
3551 | (function-item :tag "Gnus Message with full Gnus features" |
3552 | :format "%t\n" | |
3553 | gnus-user-agent) | |
69c1dd37 RS |
3554 | (function :tag "Other")) |
3555 | :group 'mail) | |
a31ca314 | 3556 | |
a31ca314 | 3557 | (define-mail-user-agent 'sendmail-user-agent |
34fbcdf3 | 3558 | 'sendmail-user-agent-compose |
a31ca314 RS |
3559 | 'mail-send-and-exit) |
3560 | ||
360b5483 RS |
3561 | (defun rfc822-goto-eoh () |
3562 | ;; Go to header delimiter line in a mail message, following RFC822 rules | |
3563 | (goto-char (point-min)) | |
e1e04350 SM |
3564 | (when (re-search-forward |
3565 | "^\\([:\n]\\|[^: \t\n]+[ \t\n]\\)" nil 'move) | |
3566 | (goto-char (match-beginning 0)))) | |
360b5483 | 3567 | |
34fbcdf3 RS |
3568 | (defun sendmail-user-agent-compose (&optional to subject other-headers continue |
3569 | switch-function yank-action | |
3570 | send-actions) | |
3571 | (if switch-function | |
3572 | (let ((special-display-buffer-names nil) | |
3573 | (special-display-regexps nil) | |
3574 | (same-window-buffer-names nil) | |
3575 | (same-window-regexps nil)) | |
3576 | (funcall switch-function "*mail*"))) | |
3577 | (let ((cc (cdr (assoc-ignore-case "cc" other-headers))) | |
0740c738 GM |
3578 | (in-reply-to (cdr (assoc-ignore-case "in-reply-to" other-headers))) |
3579 | (body (cdr (assoc-ignore-case "body" other-headers)))) | |
34fbcdf3 RS |
3580 | (or (mail continue to subject in-reply-to cc yank-action send-actions) |
3581 | continue | |
3582 | (error "Message aborted")) | |
3583 | (save-excursion | |
360b5483 | 3584 | (rfc822-goto-eoh) |
34fbcdf3 | 3585 | (while other-headers |
0740c738 GM |
3586 | (unless (member-ignore-case (car (car other-headers)) |
3587 | '("in-reply-to" "cc" "body")) | |
34fbcdf3 RS |
3588 | (insert (car (car other-headers)) ": " |
3589 | (cdr (car other-headers)) "\n")) | |
3590 | (setq other-headers (cdr other-headers))) | |
0740c738 GM |
3591 | (when body |
3592 | (forward-line 1) | |
3593 | (insert body)) | |
34fbcdf3 RS |
3594 | t))) |
3595 | ||
a31ca314 RS |
3596 | (define-mail-user-agent 'mh-e-user-agent |
3597 | 'mh-smail-batch 'mh-send-letter 'mh-fully-kill-draft | |
3598 | 'mh-before-send-letter-hook) | |
d0008a00 RS |
3599 | |
3600 | (defun compose-mail (&optional to subject other-headers continue | |
3601 | switch-function yank-action send-actions) | |
3602 | "Start composing a mail message to send. | |
3603 | This uses the user's chosen mail composition package | |
3604 | as selected with the variable `mail-user-agent'. | |
3605 | The optional arguments TO and SUBJECT specify recipients | |
3606 | and the initial Subject field, respectively. | |
3607 | ||
3608 | OTHER-HEADERS is an alist specifying additional | |
3609 | header fields. Elements look like (HEADER . VALUE) where both | |
3610 | HEADER and VALUE are strings. | |
3611 | ||
3612 | CONTINUE, if non-nil, says to continue editing a message already | |
3613 | being composed. | |
3614 | ||
3615 | SWITCH-FUNCTION, if non-nil, is a function to use to | |
3616 | switch to and display the buffer used for mail composition. | |
3617 | ||
3618 | YANK-ACTION, if non-nil, is an action to perform, if and when necessary, | |
06720de2 RS |
3619 | to insert the raw text of the message being replied to. |
3620 | It has the form (FUNCTION . ARGS). The user agent will apply | |
3621 | FUNCTION to ARGS, to insert the raw text of the original message. | |
3622 | \(The user agent will also run `mail-citation-hook', *after* the | |
3623 | original text has been inserted in this way.) | |
d0008a00 RS |
3624 | |
3625 | SEND-ACTIONS is a list of actions to call when the message is sent. | |
3626 | Each action has the form (FUNCTION . ARGS)." | |
b5f019be RS |
3627 | (interactive |
3628 | (list nil nil nil current-prefix-arg)) | |
d0008a00 RS |
3629 | (let ((function (get mail-user-agent 'composefunc))) |
3630 | (funcall function to subject other-headers continue | |
3631 | switch-function yank-action send-actions))) | |
b5f019be RS |
3632 | |
3633 | (defun compose-mail-other-window (&optional to subject other-headers continue | |
3634 | yank-action send-actions) | |
3635 | "Like \\[compose-mail], but edit the outgoing message in another window." | |
3636 | (interactive | |
3637 | (list nil nil nil current-prefix-arg)) | |
3638 | (compose-mail to subject other-headers continue | |
3639 | 'switch-to-buffer-other-window yank-action send-actions)) | |
3640 | ||
3641 | ||
3642 | (defun compose-mail-other-frame (&optional to subject other-headers continue | |
3643 | yank-action send-actions) | |
3644 | "Like \\[compose-mail], but edit the outgoing message in another frame." | |
3645 | (interactive | |
3646 | (list nil nil nil current-prefix-arg)) | |
3647 | (compose-mail to subject other-headers continue | |
3648 | 'switch-to-buffer-other-frame yank-action send-actions)) | |
eaae8106 | 3649 | |
610c1c68 RS |
3650 | (defvar set-variable-value-history nil |
3651 | "History of values entered with `set-variable'.") | |
3652 | ||
16236388 | 3653 | (defun set-variable (var val &optional make-local) |
610c1c68 RS |
3654 | "Set VARIABLE to VALUE. VALUE is a Lisp object. |
3655 | When using this interactively, enter a Lisp object for VALUE. | |
3656 | If you want VALUE to be a string, you must surround it with doublequotes. | |
3657 | VALUE is used literally, not evaluated. | |
3658 | ||
3659 | If VARIABLE has a `variable-interactive' property, that is used as if | |
3660 | it were the arg to `interactive' (which see) to interactively read VALUE. | |
3661 | ||
3662 | If VARIABLE has been defined with `defcustom', then the type information | |
16236388 RS |
3663 | in the definition is used to check that VALUE is valid. |
3664 | ||
3665 | With a prefix argument, set VARIABLE to VALUE buffer-locally." | |
e9dfb72e RS |
3666 | (interactive |
3667 | (let* ((default-var (variable-at-point)) | |
3668 | (var (if (symbolp default-var) | |
3669 | (read-variable (format "Set variable (default %s): " default-var) | |
3670 | default-var) | |
3671 | (read-variable "Set variable: "))) | |
610c1c68 RS |
3672 | (minibuffer-help-form '(describe-variable var)) |
3673 | (prop (get var 'variable-interactive)) | |
16236388 RS |
3674 | (prompt (format "Set %s%s to value: " var |
3675 | (cond ((local-variable-p var) | |
3676 | " (buffer-local)") | |
3677 | ((or current-prefix-arg | |
3678 | (local-variable-if-set-p var)) | |
3679 | " buffer-locally") | |
3680 | (t " globally")))) | |
610c1c68 RS |
3681 | (val (if prop |
3682 | ;; Use VAR's `variable-interactive' property | |
3683 | ;; as an interactive spec for prompting. | |
3684 | (call-interactively `(lambda (arg) | |
3685 | (interactive ,prop) | |
3686 | arg)) | |
3687 | (read | |
3688 | (read-string prompt nil | |
3689 | 'set-variable-value-history))))) | |
16236388 | 3690 | (list var val current-prefix-arg))) |
610c1c68 | 3691 | |
f8496faa | 3692 | (let ((type (get var 'custom-type))) |
610c1c68 RS |
3693 | (when type |
3694 | ;; Match with custom type. | |
36755dd9 | 3695 | (require 'cus-edit) |
610c1c68 RS |
3696 | (setq type (widget-convert type)) |
3697 | (unless (widget-apply type :match val) | |
1e722f9f | 3698 | (error "Value `%S' does not match type %S of %S" |
610c1c68 | 3699 | val (car type) var)))) |
16236388 RS |
3700 | |
3701 | (if make-local | |
3702 | (make-local-variable var)) | |
3703 | ||
a2aef080 GM |
3704 | (set var val) |
3705 | ||
3706 | ;; Force a thorough redisplay for the case that the variable | |
3707 | ;; has an effect on the display, like `tab-width' has. | |
3708 | (force-mode-line-update)) | |
eaae8106 | 3709 | |
e8a700bf RS |
3710 | ;; Define the major mode for lists of completions. |
3711 | ||
98b45886 RS |
3712 | (defvar completion-list-mode-map nil |
3713 | "Local map for completion list buffers.") | |
ac29eb79 | 3714 | (or completion-list-mode-map |
e8a700bf RS |
3715 | (let ((map (make-sparse-keymap))) |
3716 | (define-key map [mouse-2] 'mouse-choose-completion) | |
eaf76065 | 3717 | (define-key map [down-mouse-2] nil) |
80298193 | 3718 | (define-key map "\C-m" 'choose-completion) |
1c6c6fde | 3719 | (define-key map "\e\e\e" 'delete-completion-window) |
dde69dbe RS |
3720 | (define-key map [left] 'previous-completion) |
3721 | (define-key map [right] 'next-completion) | |
ac29eb79 | 3722 | (setq completion-list-mode-map map))) |
e8a700bf RS |
3723 | |
3724 | ;; Completion mode is suitable only for specially formatted data. | |
ac29eb79 | 3725 | (put 'completion-list-mode 'mode-class 'special) |
e8a700bf | 3726 | |
98b45886 RS |
3727 | (defvar completion-reference-buffer nil |
3728 | "Record the buffer that was current when the completion list was requested. | |
3729 | This is a local variable in the completion list buffer. | |
ec39964e | 3730 | Initial value is nil to avoid some compiler warnings.") |
3819736b | 3731 | |
83434bda RS |
3732 | (defvar completion-no-auto-exit nil |
3733 | "Non-nil means `choose-completion-string' should never exit the minibuffer. | |
3734 | This also applies to other functions such as `choose-completion' | |
3735 | and `mouse-choose-completion'.") | |
3736 | ||
98b45886 RS |
3737 | (defvar completion-base-size nil |
3738 | "Number of chars at beginning of minibuffer not involved in completion. | |
3739 | This is a local variable in the completion list buffer | |
3740 | but it talks about the buffer in `completion-reference-buffer'. | |
3741 | If this is nil, it means to compare text to determine which part | |
3742 | of the tail end of the buffer's text is involved in completion.") | |
f6b293e3 | 3743 | |
1c6c6fde RS |
3744 | (defun delete-completion-window () |
3745 | "Delete the completion list window. | |
3746 | Go to the window from which completion was requested." | |
3747 | (interactive) | |
3748 | (let ((buf completion-reference-buffer)) | |
ddb2b181 RS |
3749 | (if (one-window-p t) |
3750 | (if (window-dedicated-p (selected-window)) | |
3751 | (delete-frame (selected-frame))) | |
3752 | (delete-window (selected-window)) | |
3753 | (if (get-buffer-window buf) | |
3754 | (select-window (get-buffer-window buf)))))) | |
1c6c6fde | 3755 | |
dde69dbe RS |
3756 | (defun previous-completion (n) |
3757 | "Move to the previous item in the completion list." | |
3758 | (interactive "p") | |
3759 | (next-completion (- n))) | |
3760 | ||
3761 | (defun next-completion (n) | |
3762 | "Move to the next item in the completion list. | |
1f238ac2 | 3763 | With prefix argument N, move N items (negative N means move backward)." |
dde69dbe | 3764 | (interactive "p") |
58dd38f1 SM |
3765 | (let ((beg (point-min)) (end (point-max))) |
3766 | (while (and (> n 0) (not (eobp))) | |
dde69dbe | 3767 | ;; If in a completion, move to the end of it. |
58dd38f1 SM |
3768 | (when (get-text-property (point) 'mouse-face) |
3769 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) | |
dde69dbe | 3770 | ;; Move to start of next one. |
58dd38f1 SM |
3771 | (unless (get-text-property (point) 'mouse-face) |
3772 | (goto-char (next-single-property-change (point) 'mouse-face nil end))) | |
3773 | (setq n (1- n))) | |
3774 | (while (and (< n 0) (not (bobp))) | |
3775 | (let ((prop (get-text-property (1- (point)) 'mouse-face))) | |
3776 | ;; If in a completion, move to the start of it. | |
3777 | (when (and prop (eq prop (get-text-property (point) 'mouse-face))) | |
b61a81c2 | 3778 | (goto-char (previous-single-property-change |
58dd38f1 SM |
3779 | (point) 'mouse-face nil beg))) |
3780 | ;; Move to end of the previous completion. | |
3781 | (unless (or (bobp) (get-text-property (1- (point)) 'mouse-face)) | |
3782 | (goto-char (previous-single-property-change | |
3783 | (point) 'mouse-face nil beg))) | |
3784 | ;; Move to the start of that one. | |
3785 | (goto-char (previous-single-property-change | |
3786 | (point) 'mouse-face nil beg)) | |
3787 | (setq n (1+ n)))))) | |
dde69dbe | 3788 | |
80298193 RS |
3789 | (defun choose-completion () |
3790 | "Choose the completion that point is in or next to." | |
3791 | (interactive) | |
f6b293e3 RS |
3792 | (let (beg end completion (buffer completion-reference-buffer) |
3793 | (base-size completion-base-size)) | |
6096f362 RS |
3794 | (if (and (not (eobp)) (get-text-property (point) 'mouse-face)) |
3795 | (setq end (point) beg (1+ (point)))) | |
3796 | (if (and (not (bobp)) (get-text-property (1- (point)) 'mouse-face)) | |
3f299281 | 3797 | (setq end (1- (point)) beg (point))) |
6096f362 RS |
3798 | (if (null beg) |
3799 | (error "No completion here")) | |
3800 | (setq beg (previous-single-property-change beg 'mouse-face)) | |
88dd3c24 | 3801 | (setq end (or (next-single-property-change end 'mouse-face) (point-max))) |
ab63960f RS |
3802 | (setq completion (buffer-substring beg end)) |
3803 | (let ((owindow (selected-window))) | |
3804 | (if (and (one-window-p t 'selected-frame) | |
3805 | (window-dedicated-p (selected-window))) | |
3806 | ;; This is a special buffer's frame | |
3807 | (iconify-frame (selected-frame)) | |
3808 | (or (window-dedicated-p (selected-window)) | |
3809 | (bury-buffer))) | |
3810 | (select-window owindow)) | |
f6b293e3 | 3811 | (choose-completion-string completion buffer base-size))) |
80298193 RS |
3812 | |
3813 | ;; Delete the longest partial match for STRING | |
3814 | ;; that can be found before POINT. | |
3815 | (defun choose-completion-delete-max-match (string) | |
3816 | (let ((opoint (point)) | |
f0bfada7 RS |
3817 | len) |
3818 | ;; Try moving back by the length of the string. | |
3819 | (goto-char (max (- (point) (length string)) | |
3820 | (minibuffer-prompt-end))) | |
3821 | ;; See how far back we were actually able to move. That is the | |
3822 | ;; upper bound on how much we can match and delete. | |
3823 | (setq len (- opoint (point))) | |
61bbf6fe RS |
3824 | (if completion-ignore-case |
3825 | (setq string (downcase string))) | |
80298193 | 3826 | (while (and (> len 0) |
f0bfada7 | 3827 | (let ((tail (buffer-substring (point) opoint))) |
61bbf6fe RS |
3828 | (if completion-ignore-case |
3829 | (setq tail (downcase tail))) | |
80298193 RS |
3830 | (not (string= tail (substring string 0 len))))) |
3831 | (setq len (1- len)) | |
3832 | (forward-char 1)) | |
3833 | (delete-char len))) | |
3834 | ||
ba36181b | 3835 | (defvar choose-completion-string-functions nil |
bbbbb15b KS |
3836 | "Functions that may override the normal insertion of a completion choice. |
3837 | These functions are called in order with four arguments: | |
3838 | CHOICE - the string to insert in the buffer, | |
3839 | BUFFER - the buffer in which the choice should be inserted, | |
89a6cfe3 | 3840 | MINI-P - non-nil iff BUFFER is a minibuffer, and |
12829a07 RS |
3841 | BASE-SIZE - the number of characters in BUFFER before |
3842 | the string being completed. | |
3843 | ||
bbbbb15b KS |
3844 | If a function in the list returns non-nil, that function is supposed |
3845 | to have inserted the CHOICE in the BUFFER, and possibly exited | |
12829a07 | 3846 | the minibuffer; no further functions will be called. |
ba36181b | 3847 | |
12829a07 RS |
3848 | If all functions in the list return nil, that means to use |
3849 | the default method of inserting the completion in BUFFER.") | |
74d0290b | 3850 | |
f6b293e3 | 3851 | (defun choose-completion-string (choice &optional buffer base-size) |
12829a07 RS |
3852 | "Switch to BUFFER and insert the completion choice CHOICE. |
3853 | BASE-SIZE, if non-nil, says how many characters of BUFFER's text | |
e36aeef9 RS |
3854 | to keep. If it is nil, we call `choose-completion-delete-max-match' |
3855 | to decide what to delete." | |
12829a07 RS |
3856 | |
3857 | ;; If BUFFER is the minibuffer, exit the minibuffer | |
3858 | ;; unless it is reading a file name and CHOICE is a directory, | |
3859 | ;; or completion-no-auto-exit is non-nil. | |
3860 | ||
f436a90a | 3861 | (let ((buffer (or buffer completion-reference-buffer)) |
d99f8496 SM |
3862 | (mini-p (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" |
3863 | (buffer-name buffer)))) | |
cf52ad58 RS |
3864 | ;; If BUFFER is a minibuffer, barf unless it's the currently |
3865 | ;; active minibuffer. | |
f436a90a | 3866 | (if (and mini-p |
45486731 RS |
3867 | (or (not (active-minibuffer-window)) |
3868 | (not (equal buffer | |
3869 | (window-buffer (active-minibuffer-window)))))) | |
cf52ad58 | 3870 | (error "Minibuffer is not active for completion") |
bbbbb15b | 3871 | (unless (run-hook-with-args-until-success |
d99f8496 SM |
3872 | 'choose-completion-string-functions |
3873 | choice buffer mini-p base-size) | |
3874 | ;; Insert the completion into the buffer where it was requested. | |
bbbbb15b KS |
3875 | (set-buffer buffer) |
3876 | (if base-size | |
3877 | (delete-region (+ base-size (if mini-p | |
3878 | (minibuffer-prompt-end) | |
3879 | (point-min))) | |
3880 | (point)) | |
3881 | (choose-completion-delete-max-match choice)) | |
3882 | (insert choice) | |
3883 | (remove-text-properties (- (point) (length choice)) (point) | |
3884 | '(mouse-face nil)) | |
3885 | ;; Update point in the window that BUFFER is showing in. | |
3886 | (let ((window (get-buffer-window buffer t))) | |
3887 | (set-window-point window (point))) | |
3888 | ;; If completing for the minibuffer, exit it with this choice. | |
3889 | (and (not completion-no-auto-exit) | |
3890 | (equal buffer (window-buffer (minibuffer-window))) | |
3891 | minibuffer-completion-table | |
3892 | ;; If this is reading a file name, and the file name chosen | |
3893 | ;; is a directory, don't exit the minibuffer. | |
3894 | (if (and (eq minibuffer-completion-table 'read-file-name-internal) | |
3895 | (file-directory-p (field-string (point-max)))) | |
3896 | (let ((mini (active-minibuffer-window))) | |
3897 | (select-window mini) | |
3898 | (when minibuffer-auto-raise | |
3899 | (raise-frame (window-frame mini)))) | |
3900 | (exit-minibuffer))))))) | |
80298193 | 3901 | |
ac29eb79 | 3902 | (defun completion-list-mode () |
e8a700bf | 3903 | "Major mode for buffers showing lists of possible completions. |
80298193 RS |
3904 | Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\ |
3905 | to select the completion near point. | |
3906 | Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\ | |
3907 | with the mouse." | |
e8a700bf RS |
3908 | (interactive) |
3909 | (kill-all-local-variables) | |
ac29eb79 RS |
3910 | (use-local-map completion-list-mode-map) |
3911 | (setq mode-name "Completion List") | |
3912 | (setq major-mode 'completion-list-mode) | |
f6b293e3 RS |
3913 | (make-local-variable 'completion-base-size) |
3914 | (setq completion-base-size nil) | |
ac29eb79 | 3915 | (run-hooks 'completion-list-mode-hook)) |
e8a700bf | 3916 | |
c8d6d636 GM |
3917 | (defun completion-list-mode-finish () |
3918 | "Finish setup of the completions buffer. | |
3919 | Called from `temp-buffer-show-hook'." | |
3920 | (when (eq major-mode 'completion-list-mode) | |
3921 | (toggle-read-only 1))) | |
3922 | ||
3923 | (add-hook 'temp-buffer-show-hook 'completion-list-mode-finish) | |
3924 | ||
747a0e2f RS |
3925 | (defvar completion-setup-hook nil |
3926 | "Normal hook run at the end of setting up a completion list buffer. | |
3927 | When this hook is run, the current buffer is the one in which the | |
3928 | command to display the completion list buffer was run. | |
3929 | The completion list buffer is available as the value of `standard-output'.") | |
3930 | ||
98b45886 RS |
3931 | ;; This function goes in completion-setup-hook, so that it is called |
3932 | ;; after the text of the completion list buffer is written. | |
6096f362 | 3933 | |
e8a700bf RS |
3934 | (defun completion-setup-function () |
3935 | (save-excursion | |
98b45886 | 3936 | (let ((mainbuf (current-buffer))) |
3819736b RS |
3937 | (set-buffer standard-output) |
3938 | (completion-list-mode) | |
3939 | (make-local-variable 'completion-reference-buffer) | |
3940 | (setq completion-reference-buffer mainbuf) | |
2d64b6f6 | 3941 | (if minibuffer-completing-file-name |
50be475d RS |
3942 | ;; For file name completion, |
3943 | ;; use the number of chars before the start of the | |
3944 | ;; last file name component. | |
3945 | (setq completion-base-size | |
3946 | (save-excursion | |
3947 | (set-buffer mainbuf) | |
3948 | (goto-char (point-max)) | |
5560dc5d | 3949 | (skip-chars-backward "^/") |
0aad4805 | 3950 | (- (point) (minibuffer-prompt-end)))) |
19183a29 RS |
3951 | ;; Otherwise, in minibuffer, the whole input is being completed. |
3952 | (save-match-data | |
3953 | (if (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" | |
3954 | (buffer-name mainbuf)) | |
3955 | (setq completion-base-size 0)))) | |
3819736b | 3956 | (goto-char (point-min)) |
0d6e23cf | 3957 | (if (display-mouse-p) |
3819736b | 3958 | (insert (substitute-command-keys |
80298193 RS |
3959 | "Click \\[mouse-choose-completion] on a completion to select it.\n"))) |
3960 | (insert (substitute-command-keys | |
3961 | "In this buffer, type \\[choose-completion] to \ | |
7d22ed15 | 3962 | select the completion near point.\n\n"))))) |
c88ab9ce | 3963 | |
e8a700bf | 3964 | (add-hook 'completion-setup-hook 'completion-setup-function) |
dde69dbe RS |
3965 | |
3966 | (define-key minibuffer-local-completion-map [prior] | |
3967 | 'switch-to-completions) | |
3968 | (define-key minibuffer-local-must-match-map [prior] | |
3969 | 'switch-to-completions) | |
3970 | (define-key minibuffer-local-completion-map "\M-v" | |
3971 | 'switch-to-completions) | |
3972 | (define-key minibuffer-local-must-match-map "\M-v" | |
3973 | 'switch-to-completions) | |
3974 | ||
3975 | (defun switch-to-completions () | |
3976 | "Select the completion list window." | |
3977 | (interactive) | |
9595fbdb RS |
3978 | ;; Make sure we have a completions window. |
3979 | (or (get-buffer-window "*Completions*") | |
3980 | (minibuffer-completion-help)) | |
fdbd7c4d KH |
3981 | (let ((window (get-buffer-window "*Completions*"))) |
3982 | (when window | |
3983 | (select-window window) | |
3984 | (goto-char (point-min)) | |
3985 | (search-forward "\n\n") | |
3986 | (forward-line 1)))) | |
eaae8106 | 3987 | |
82072f33 RS |
3988 | ;; Support keyboard commands to turn on various modifiers. |
3989 | ||
3990 | ;; These functions -- which are not commands -- each add one modifier | |
3991 | ;; to the following event. | |
3992 | ||
3993 | (defun event-apply-alt-modifier (ignore-prompt) | |
70cf9f08 KH |
3994 | "Add the Alt modifier to the following event. |
3995 | For example, type \\[event-apply-alt-modifier] & to enter Alt-&." | |
82072f33 RS |
3996 | (vector (event-apply-modifier (read-event) 'alt 22 "A-"))) |
3997 | (defun event-apply-super-modifier (ignore-prompt) | |
70cf9f08 KH |
3998 | "Add the Super modifier to the following event. |
3999 | For example, type \\[event-apply-super-modifier] & to enter Super-&." | |
82072f33 RS |
4000 | (vector (event-apply-modifier (read-event) 'super 23 "s-"))) |
4001 | (defun event-apply-hyper-modifier (ignore-prompt) | |
70cf9f08 KH |
4002 | "Add the Hyper modifier to the following event. |
4003 | For example, type \\[event-apply-hyper-modifier] & to enter Hyper-&." | |
82072f33 RS |
4004 | (vector (event-apply-modifier (read-event) 'hyper 24 "H-"))) |
4005 | (defun event-apply-shift-modifier (ignore-prompt) | |
70cf9f08 KH |
4006 | "Add the Shift modifier to the following event. |
4007 | For example, type \\[event-apply-shift-modifier] & to enter Shift-&." | |
82072f33 RS |
4008 | (vector (event-apply-modifier (read-event) 'shift 25 "S-"))) |
4009 | (defun event-apply-control-modifier (ignore-prompt) | |
70cf9f08 KH |
4010 | "Add the Ctrl modifier to the following event. |
4011 | For example, type \\[event-apply-control-modifier] & to enter Ctrl-&." | |
82072f33 RS |
4012 | (vector (event-apply-modifier (read-event) 'control 26 "C-"))) |
4013 | (defun event-apply-meta-modifier (ignore-prompt) | |
70cf9f08 KH |
4014 | "Add the Meta modifier to the following event. |
4015 | For example, type \\[event-apply-meta-modifier] & to enter Meta-&." | |
82072f33 RS |
4016 | (vector (event-apply-modifier (read-event) 'meta 27 "M-"))) |
4017 | ||
4018 | (defun event-apply-modifier (event symbol lshiftby prefix) | |
4019 | "Apply a modifier flag to event EVENT. | |
4020 | SYMBOL is the name of this modifier, as a symbol. | |
4021 | LSHIFTBY is the numeric value of this modifier, in keyboard events. | |
4022 | PREFIX is the string that represents this modifier in an event type symbol." | |
4023 | (if (numberp event) | |
4024 | (cond ((eq symbol 'control) | |
90bebcb0 KH |
4025 | (if (and (<= (downcase event) ?z) |
4026 | (>= (downcase event) ?a)) | |
82072f33 | 4027 | (- (downcase event) ?a -1) |
90bebcb0 KH |
4028 | (if (and (<= (downcase event) ?Z) |
4029 | (>= (downcase event) ?A)) | |
82072f33 RS |
4030 | (- (downcase event) ?A -1) |
4031 | (logior (lsh 1 lshiftby) event)))) | |
4032 | ((eq symbol 'shift) | |
4033 | (if (and (<= (downcase event) ?z) | |
4034 | (>= (downcase event) ?a)) | |
4035 | (upcase event) | |
4036 | (logior (lsh 1 lshiftby) event))) | |
4037 | (t | |
4038 | (logior (lsh 1 lshiftby) event))) | |
4039 | (if (memq symbol (event-modifiers event)) | |
4040 | event | |
4041 | (let ((event-type (if (symbolp event) event (car event)))) | |
4042 | (setq event-type (intern (concat prefix (symbol-name event-type)))) | |
4043 | (if (symbolp event) | |
4044 | event-type | |
4045 | (cons event-type (cdr event))))))) | |
4046 | ||
e5fff738 KH |
4047 | (define-key function-key-map [?\C-x ?@ ?h] 'event-apply-hyper-modifier) |
4048 | (define-key function-key-map [?\C-x ?@ ?s] 'event-apply-super-modifier) | |
4049 | (define-key function-key-map [?\C-x ?@ ?m] 'event-apply-meta-modifier) | |
4050 | (define-key function-key-map [?\C-x ?@ ?a] 'event-apply-alt-modifier) | |
4051 | (define-key function-key-map [?\C-x ?@ ?S] 'event-apply-shift-modifier) | |
4052 | (define-key function-key-map [?\C-x ?@ ?c] 'event-apply-control-modifier) | |
eaae8106 | 4053 | |
a3d1480b JB |
4054 | ;;;; Keypad support. |
4055 | ||
4056 | ;;; Make the keypad keys act like ordinary typing keys. If people add | |
4057 | ;;; bindings for the function key symbols, then those bindings will | |
4058 | ;;; override these, so this shouldn't interfere with any existing | |
4059 | ;;; bindings. | |
4060 | ||
0d173134 | 4061 | ;; Also tell read-char how to handle these keys. |
e1e04350 | 4062 | (mapc |
a3d1480b JB |
4063 | (lambda (keypad-normal) |
4064 | (let ((keypad (nth 0 keypad-normal)) | |
4065 | (normal (nth 1 keypad-normal))) | |
0d173134 | 4066 | (put keypad 'ascii-character normal) |
a3d1480b JB |
4067 | (define-key function-key-map (vector keypad) (vector normal)))) |
4068 | '((kp-0 ?0) (kp-1 ?1) (kp-2 ?2) (kp-3 ?3) (kp-4 ?4) | |
4069 | (kp-5 ?5) (kp-6 ?6) (kp-7 ?7) (kp-8 ?8) (kp-9 ?9) | |
4070 | (kp-space ?\ ) | |
4071 | (kp-tab ?\t) | |
4072 | (kp-enter ?\r) | |
4073 | (kp-multiply ?*) | |
4074 | (kp-add ?+) | |
4075 | (kp-separator ?,) | |
4076 | (kp-subtract ?-) | |
4077 | (kp-decimal ?.) | |
4078 | (kp-divide ?/) | |
4079 | (kp-equal ?=))) | |
f54b0d85 | 4080 | \f |
1e722f9f | 4081 | ;;;; |
b005abd5 | 4082 | ;;;; forking a twin copy of a buffer. |
1e722f9f | 4083 | ;;;; |
b005abd5 SM |
4084 | |
4085 | (defvar clone-buffer-hook nil | |
4086 | "Normal hook to run in the new buffer at the end of `clone-buffer'.") | |
4087 | ||
4088 | (defun clone-process (process &optional newname) | |
4089 | "Create a twin copy of PROCESS. | |
4090 | If NEWNAME is nil, it defaults to PROCESS' name; | |
4091 | NEWNAME is modified by adding or incrementing <N> at the end as necessary. | |
4092 | If PROCESS is associated with a buffer, the new process will be associated | |
4093 | with the current buffer instead. | |
4094 | Returns nil if PROCESS has already terminated." | |
4095 | (setq newname (or newname (process-name process))) | |
4096 | (if (string-match "<[0-9]+>\\'" newname) | |
4097 | (setq newname (substring newname 0 (match-beginning 0)))) | |
4098 | (when (memq (process-status process) '(run stop open)) | |
4099 | (let* ((process-connection-type (process-tty-name process)) | |
b005abd5 SM |
4100 | (new-process |
4101 | (if (memq (process-status process) '(open)) | |
ed7069af KS |
4102 | (let ((args (process-contact process t))) |
4103 | (setq args (plist-put args :name newname)) | |
4104 | (setq args (plist-put args :buffer | |
4105 | (if (process-buffer process) (current-buffer)))) | |
4106 | (apply 'make-network-process args)) | |
b005abd5 SM |
4107 | (apply 'start-process newname |
4108 | (if (process-buffer process) (current-buffer)) | |
4109 | (process-command process))))) | |
ed7069af KS |
4110 | (set-process-query-on-exit-flag |
4111 | new-process (process-query-on-exit-flag process)) | |
b005abd5 SM |
4112 | (set-process-inherit-coding-system-flag |
4113 | new-process (process-inherit-coding-system-flag process)) | |
4114 | (set-process-filter new-process (process-filter process)) | |
4115 | (set-process-sentinel new-process (process-sentinel process)) | |
4116 | new-process))) | |
4117 | ||
b75b82ab | 4118 | ;; things to maybe add (currently partly covered by `funcall mode'): |
b005abd5 SM |
4119 | ;; - syntax-table |
4120 | ;; - overlays | |
4121 | (defun clone-buffer (&optional newname display-flag) | |
4122 | "Create a twin copy of the current buffer. | |
4123 | If NEWNAME is nil, it defaults to the current buffer's name; | |
4124 | NEWNAME is modified by adding or incrementing <N> at the end as necessary. | |
4125 | ||
4126 | If DISPLAY-FLAG is non-nil, the new buffer is shown with `pop-to-buffer'. | |
4127 | This runs the normal hook `clone-buffer-hook' in the new buffer | |
4128 | after it has been set up properly in other respects." | |
61acfe7f RS |
4129 | (interactive |
4130 | (progn | |
4131 | (if buffer-file-name | |
4132 | (error "Cannot clone a file-visiting buffer")) | |
4133 | (if (get major-mode 'no-clone) | |
4134 | (error "Cannot clone a buffer in %s mode" mode-name)) | |
4135 | (list (if current-prefix-arg (read-string "Name: ")) | |
4136 | t))) | |
b005abd5 SM |
4137 | (if buffer-file-name |
4138 | (error "Cannot clone a file-visiting buffer")) | |
4139 | (if (get major-mode 'no-clone) | |
4140 | (error "Cannot clone a buffer in %s mode" mode-name)) | |
4141 | (setq newname (or newname (buffer-name))) | |
4142 | (if (string-match "<[0-9]+>\\'" newname) | |
4143 | (setq newname (substring newname 0 (match-beginning 0)))) | |
4144 | (let ((buf (current-buffer)) | |
4145 | (ptmin (point-min)) | |
4146 | (ptmax (point-max)) | |
4147 | (pt (point)) | |
4148 | (mk (if mark-active (mark t))) | |
4149 | (modified (buffer-modified-p)) | |
4150 | (mode major-mode) | |
4151 | (lvars (buffer-local-variables)) | |
4152 | (process (get-buffer-process (current-buffer))) | |
4153 | (new (generate-new-buffer (or newname (buffer-name))))) | |
4154 | (save-restriction | |
4155 | (widen) | |
4156 | (with-current-buffer new | |
4157 | (insert-buffer-substring buf))) | |
4158 | (with-current-buffer new | |
4159 | (narrow-to-region ptmin ptmax) | |
4160 | (goto-char pt) | |
4161 | (if mk (set-mark mk)) | |
4162 | (set-buffer-modified-p modified) | |
4163 | ||
4164 | ;; Clone the old buffer's process, if any. | |
4165 | (when process (clone-process process)) | |
4166 | ||
4167 | ;; Now set up the major mode. | |
4168 | (funcall mode) | |
4169 | ||
4170 | ;; Set up other local variables. | |
4171 | (mapcar (lambda (v) | |
4172 | (condition-case () ;in case var is read-only | |
4173 | (if (symbolp v) | |
4174 | (makunbound v) | |
4175 | (set (make-local-variable (car v)) (cdr v))) | |
4176 | (error nil))) | |
4177 | lvars) | |
4178 | ||
4179 | ;; Run any hooks (typically set up by the major mode | |
4180 | ;; for cloning to work properly). | |
4181 | (run-hooks 'clone-buffer-hook)) | |
4182 | (if display-flag (pop-to-buffer new)) | |
4183 | new)) | |
4184 | ||
fa65f20b | 4185 | |
7e3afb04 | 4186 | (defun clone-indirect-buffer (newname display-flag &optional norecord) |
fa65f20b GM |
4187 | "Create an indirect buffer that is a twin copy of the current buffer. |
4188 | ||
4189 | Give the indirect buffer name NEWNAME. Interactively, read NEW-NAME | |
4190 | from the minibuffer when invoked with a prefix arg. If NEWNAME is nil | |
4191 | or if not called with a prefix arg, NEWNAME defaults to the current | |
4192 | buffer's name. The name is modified by adding a `<N>' suffix to it | |
4193 | or by incrementing the N in an existing suffix. | |
4194 | ||
4195 | DISPLAY-FLAG non-nil means show the new buffer with `pop-to-buffer'. | |
7e3afb04 GM |
4196 | This is always done when called interactively. |
4197 | ||
4198 | Optional last arg NORECORD non-nil means do not put this buffer at the | |
4199 | front of the list of recently selected ones." | |
61acfe7f RS |
4200 | (interactive |
4201 | (progn | |
4202 | (if (get major-mode 'no-clone-indirect) | |
4203 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
4204 | (list (if current-prefix-arg | |
4205 | (read-string "BName of indirect buffer: ")) | |
4206 | t))) | |
4207 | (if (get major-mode 'no-clone-indirect) | |
4208 | (error "Cannot indirectly clone a buffer in %s mode" mode-name)) | |
fa65f20b GM |
4209 | (setq newname (or newname (buffer-name))) |
4210 | (if (string-match "<[0-9]+>\\'" newname) | |
4211 | (setq newname (substring newname 0 (match-beginning 0)))) | |
4212 | (let* ((name (generate-new-buffer-name newname)) | |
4213 | (buffer (make-indirect-buffer (current-buffer) name t))) | |
4214 | (when display-flag | |
58dd38f1 | 4215 | (pop-to-buffer buffer norecord)) |
fa65f20b GM |
4216 | buffer)) |
4217 | ||
4218 | ||
7e3afb04 GM |
4219 | (defun clone-indirect-buffer-other-window (buffer &optional norecord) |
4220 | "Create an indirect buffer that is a twin copy of BUFFER. | |
4221 | Select the new buffer in another window. | |
4222 | Optional second arg NORECORD non-nil means do not put this buffer at | |
4223 | the front of the list of recently selected ones." | |
4224 | (interactive "bClone buffer in other window: ") | |
acd39eb6 | 4225 | (let ((pop-up-windows t)) |
7e3afb04 GM |
4226 | (set-buffer buffer) |
4227 | (clone-indirect-buffer nil t norecord))) | |
4228 | ||
14583cb1 | 4229 | (define-key ctl-x-4-map "c" 'clone-indirect-buffer-other-window) |
f54b0d85 | 4230 | \f |
1d4b11bf GM |
4231 | ;;; Handling of Backspace and Delete keys. |
4232 | ||
7f62656b EZ |
4233 | (defcustom normal-erase-is-backspace nil |
4234 | "If non-nil, Delete key deletes forward and Backspace key deletes backward. | |
1d4b11bf GM |
4235 | |
4236 | On window systems, the default value of this option is chosen | |
4237 | according to the keyboard used. If the keyboard has both a Backspace | |
4238 | key and a Delete key, and both are mapped to their usual meanings, the | |
4239 | option's default value is set to t, so that Backspace can be used to | |
7f62656b | 4240 | delete backward, and Delete can be used to delete forward. |
1d4b11bf | 4241 | |
7f62656b | 4242 | If not running under a window system, customizing this option accomplishes |
1d4b11bf GM |
4243 | a similar effect by mapping C-h, which is usually generated by the |
4244 | Backspace key, to DEL, and by mapping DEL to C-d via | |
4245 | `keyboard-translate'. The former functionality of C-h is available on | |
4246 | the F1 key. You should probably not use this setting if you don't | |
f060b834 GM |
4247 | have both Backspace, Delete and F1 keys. |
4248 | ||
4249 | Setting this variable with setq doesn't take effect. Programmatically, | |
7f62656b | 4250 | call `normal-erase-is-backspace-mode' (which see) instead." |
1d4b11bf GM |
4251 | :type 'boolean |
4252 | :group 'editing-basics | |
4253 | :version "21.1" | |
4254 | :set (lambda (symbol value) | |
4255 | ;; The fboundp is because of a problem with :set when | |
4256 | ;; dumping Emacs. It doesn't really matter. | |
7f62656b EZ |
4257 | (if (fboundp 'normal-erase-is-backspace-mode) |
4258 | (normal-erase-is-backspace-mode (or value 0)) | |
1d4b11bf GM |
4259 | (set-default symbol value)))) |
4260 | ||
4261 | ||
7f62656b EZ |
4262 | (defun normal-erase-is-backspace-mode (&optional arg) |
4263 | "Toggle the Erase and Delete mode of the Backspace and Delete keys. | |
4264 | ||
e02160a3 | 4265 | With numeric arg, turn the mode on if and only if ARG is positive. |
7f62656b EZ |
4266 | |
4267 | On window systems, when this mode is on, Delete is mapped to C-d and | |
4268 | Backspace is mapped to DEL; when this mode is off, both Delete and | |
4269 | Backspace are mapped to DEL. (The remapping goes via | |
4270 | `function-key-map', so binding Delete or Backspace in the global or | |
4271 | local keymap will override that.) | |
4272 | ||
4273 | In addition, on window systems, the bindings of C-Delete, M-Delete, | |
4274 | C-M-Delete, C-Backspace, M-Backspace, and C-M-Backspace are changed in | |
4275 | the global keymap in accordance with the functionality of Delete and | |
4276 | Backspace. For example, if Delete is remapped to C-d, which deletes | |
4277 | forward, C-Delete is bound to `kill-word', but if Delete is remapped | |
4278 | to DEL, which deletes backward, C-Delete is bound to | |
4279 | `backward-kill-word'. | |
4280 | ||
4281 | If not running on a window system, a similar effect is accomplished by | |
4282 | remapping C-h (normally produced by the Backspace key) and DEL via | |
4283 | `keyboard-translate': if this mode is on, C-h is mapped to DEL and DEL | |
4284 | to C-d; if it's off, the keys are not remapped. | |
4285 | ||
4286 | When not running on a window system, and this mode is turned on, the | |
4287 | former functionality of C-h is available on the F1 key. You should | |
4288 | probably not turn on this mode on a text-only terminal if you don't | |
4289 | have both Backspace, Delete and F1 keys. | |
4290 | ||
4291 | See also `normal-erase-is-backspace'." | |
1d4b11bf | 4292 | (interactive "P") |
7f62656b | 4293 | (setq normal-erase-is-backspace |
1d4b11bf GM |
4294 | (if arg |
4295 | (> (prefix-numeric-value arg) 0) | |
7f62656b | 4296 | (not normal-erase-is-backspace))) |
1d4b11bf | 4297 | |
13a9eed7 EZ |
4298 | (cond ((or (memq window-system '(x w32 mac pc)) |
4299 | (memq system-type '(ms-dos windows-nt))) | |
7f62656b | 4300 | (let ((bindings |
103db06c RS |
4301 | `(([C-delete] [C-backspace]) |
4302 | ([M-delete] [M-backspace]) | |
4303 | ([C-M-delete] [C-M-backspace]) | |
ec9f4754 | 4304 | (,esc-map |
103db06c RS |
4305 | [C-delete] [C-backspace]))) |
4306 | (old-state (lookup-key function-key-map [delete]))) | |
ec9f4754 | 4307 | |
7f62656b | 4308 | (if normal-erase-is-backspace |
ec9f4754 GM |
4309 | (progn |
4310 | (define-key function-key-map [delete] [?\C-d]) | |
be4f1e41 | 4311 | (define-key function-key-map [kp-delete] [?\C-d]) |
ec9f4754 GM |
4312 | (define-key function-key-map [backspace] [?\C-?])) |
4313 | (define-key function-key-map [delete] [?\C-?]) | |
be4f1e41 | 4314 | (define-key function-key-map [kp-delete] [?\C-?]) |
ec9f4754 GM |
4315 | (define-key function-key-map [backspace] [?\C-?])) |
4316 | ||
103db06c RS |
4317 | ;; Maybe swap bindings of C-delete and C-backspace, etc. |
4318 | (unless (equal old-state (lookup-key function-key-map [delete])) | |
4319 | (dolist (binding bindings) | |
4320 | (let ((map global-map)) | |
4321 | (when (keymapp (car binding)) | |
4322 | (setq map (car binding) binding (cdr binding))) | |
4323 | (let* ((key1 (nth 0 binding)) | |
4324 | (key2 (nth 1 binding)) | |
4325 | (binding1 (lookup-key map key1)) | |
4326 | (binding2 (lookup-key map key2))) | |
4327 | (define-key map key1 binding2) | |
4328 | (define-key map key2 binding1))))))) | |
1d4b11bf | 4329 | (t |
7f62656b | 4330 | (if normal-erase-is-backspace |
1d4b11bf GM |
4331 | (progn |
4332 | (keyboard-translate ?\C-h ?\C-?) | |
4333 | (keyboard-translate ?\C-? ?\C-d)) | |
4334 | (keyboard-translate ?\C-h ?\C-h) | |
4335 | (keyboard-translate ?\C-? ?\C-?)))) | |
4336 | ||
7f62656b | 4337 | (run-hooks 'normal-erase-is-backspace-hook) |
1d4b11bf | 4338 | (if (interactive-p) |
7f62656b EZ |
4339 | (message "Delete key deletes %s" |
4340 | (if normal-erase-is-backspace "forward" "backward")))) | |
4341 | ||
1d4b11bf | 4342 | |
e1e04350 | 4343 | ;; Minibuffer prompt stuff. |
9b350152 | 4344 | |
49c14a05 GM |
4345 | ;(defun minibuffer-prompt-modification (start end) |
4346 | ; (error "You cannot modify the prompt")) | |
4347 | ; | |
4348 | ; | |
4349 | ;(defun minibuffer-prompt-insertion (start end) | |
4350 | ; (let ((inhibit-modification-hooks t)) | |
4351 | ; (delete-region start end) | |
4352 | ; ;; Discard undo information for the text insertion itself | |
4353 | ; ;; and for the text deletion.above. | |
4354 | ; (when (consp buffer-undo-list) | |
4355 | ; (setq buffer-undo-list (cddr buffer-undo-list))) | |
4356 | ; (message "You cannot modify the prompt"))) | |
4357 | ; | |
4358 | ; | |
4359 | ;(setq minibuffer-prompt-properties | |
4360 | ; (list 'modification-hooks '(minibuffer-prompt-modification) | |
4361 | ; 'insert-in-front-hooks '(minibuffer-prompt-insertion))) | |
4362 | ; | |
9b350152 | 4363 | |
00398e3b | 4364 | (provide 'simple) |
c88ab9ce | 4365 | ;;; simple.el ends here |