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