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