| 1 | ;; Basic editing commands for Emacs |
| 2 | ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
| 3 | |
| 4 | ;; This file is part of GNU Emacs. |
| 5 | |
| 6 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
| 7 | ;; it under the terms of the GNU General Public License as published by |
| 8 | ;; the Free Software Foundation; either version 1, or (at your option) |
| 9 | ;; any later version. |
| 10 | |
| 11 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 12 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | ;; GNU General Public License for more details. |
| 15 | |
| 16 | ;; You should have received a copy of the GNU General Public License |
| 17 | ;; along with GNU Emacs; see the file COPYING. If not, write to |
| 18 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | |
| 20 | |
| 21 | (defun open-line (arg) |
| 22 | "Insert a newline and leave point before it. If there is a fill |
| 23 | prefix, inserts the fill prefix after the newline that it inserts. |
| 24 | With arg, inserts that many newlines." |
| 25 | (interactive "*p") |
| 26 | (let ((flag (and (bolp) (not (bobp))))) |
| 27 | (if flag (forward-char -1)) |
| 28 | (while (> arg 0) |
| 29 | (save-excursion |
| 30 | (insert ?\n) |
| 31 | (if fill-prefix (insert fill-prefix))) |
| 32 | (setq arg (1- arg))) |
| 33 | (if flag (forward-char 1)))) |
| 34 | |
| 35 | (defun split-line () |
| 36 | "Split current line, moving portion beyond point vertically down." |
| 37 | (interactive "*") |
| 38 | (skip-chars-forward " \t") |
| 39 | (let ((col (current-column)) |
| 40 | (pos (point))) |
| 41 | (insert ?\n) |
| 42 | (indent-to col 0) |
| 43 | (goto-char pos))) |
| 44 | |
| 45 | (defun quoted-insert (arg) |
| 46 | "Read next input character and insert it. |
| 47 | Useful for inserting control characters. |
| 48 | You may also type up to 3 octal digits, to insert a character with that code" |
| 49 | (interactive "*p") |
| 50 | (let ((char (read-quoted-char))) |
| 51 | (while (> arg 0) |
| 52 | (insert char) |
| 53 | (setq arg (1- arg))))) |
| 54 | |
| 55 | (defun delete-indentation (&optional arg) |
| 56 | "Join this line to previous and fix up whitespace at join. |
| 57 | With argument, join this line to following line." |
| 58 | (interactive "*P") |
| 59 | (beginning-of-line) |
| 60 | (if arg (forward-line 1)) |
| 61 | (if (eq (preceding-char) ?\n) |
| 62 | (progn |
| 63 | (delete-region (point) (1- (point))) |
| 64 | (fixup-whitespace)))) |
| 65 | |
| 66 | (defun fixup-whitespace () |
| 67 | "Fixup white space between objects around point. |
| 68 | Leave one space or none, according to the context." |
| 69 | (interactive "*") |
| 70 | (save-excursion |
| 71 | (delete-horizontal-space) |
| 72 | (if (or (looking-at "^\\|\\s)") |
| 73 | (save-excursion (forward-char -1) |
| 74 | (looking-at "$\\|\\s(\\|\\s'"))) |
| 75 | nil |
| 76 | (insert ?\ )))) |
| 77 | |
| 78 | (defun delete-horizontal-space () |
| 79 | "Delete all spaces and tabs around point." |
| 80 | (interactive "*") |
| 81 | (skip-chars-backward " \t") |
| 82 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) |
| 83 | |
| 84 | (defun just-one-space () |
| 85 | "Delete all spaces and tabs around point, leaving one space." |
| 86 | (interactive "*") |
| 87 | (skip-chars-backward " \t") |
| 88 | (if (= (following-char) ? ) |
| 89 | (forward-char 1) |
| 90 | (insert ? )) |
| 91 | (delete-region (point) (progn (skip-chars-forward " \t") (point)))) |
| 92 | |
| 93 | (defun delete-blank-lines () |
| 94 | "On blank line, delete all surrounding blank lines, leaving just one. |
| 95 | On isolated blank line, delete that one. |
| 96 | On nonblank line, delete all blank lines that follow it." |
| 97 | (interactive "*") |
| 98 | (let (thisblank singleblank) |
| 99 | (save-excursion |
| 100 | (beginning-of-line) |
| 101 | (setq thisblank (looking-at "[ \t]*$")) |
| 102 | (setq singleblank |
| 103 | (and thisblank |
| 104 | (not (looking-at "[ \t]*\n[ \t]*$")) |
| 105 | (or (bobp) |
| 106 | (progn (forward-line -1) |
| 107 | (not (looking-at "[ \t]*$"))))))) |
| 108 | (if thisblank |
| 109 | (progn |
| 110 | (beginning-of-line) |
| 111 | (if singleblank (forward-line 1)) |
| 112 | (delete-region (point) |
| 113 | (if (re-search-backward "[^ \t\n]" nil t) |
| 114 | (progn (forward-line 1) (point)) |
| 115 | (point-min))))) |
| 116 | (if (not (and thisblank singleblank)) |
| 117 | (save-excursion |
| 118 | (end-of-line) |
| 119 | (forward-line 1) |
| 120 | (delete-region (point) |
| 121 | (if (re-search-forward "[^ \t\n]" nil t) |
| 122 | (progn (beginning-of-line) (point)) |
| 123 | (point-max))))))) |
| 124 | |
| 125 | (defun back-to-indentation () |
| 126 | "Move point to the first non-whitespace character on this line." |
| 127 | (interactive) |
| 128 | (beginning-of-line 1) |
| 129 | (skip-chars-forward " \t")) |
| 130 | |
| 131 | (defun newline-and-indent () |
| 132 | "Insert a newline, then indent according to major mode. |
| 133 | Indentation is done using the current indent-line-function. |
| 134 | In programming language modes, this is the same as TAB. |
| 135 | In some text modes, where TAB inserts a tab, this indents to the |
| 136 | specified left-margin column." |
| 137 | (interactive "*") |
| 138 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) |
| 139 | (insert ?\n) |
| 140 | (indent-according-to-mode)) |
| 141 | |
| 142 | (defun reindent-then-newline-and-indent () |
| 143 | "Reindent current line, insert newline, then indent the new line. |
| 144 | Indentation of both lines is done according to the current major mode, |
| 145 | which means that the current value of indent-line-function is called. |
| 146 | In programming language modes, this is the same as TAB. |
| 147 | In some text modes, where TAB inserts a tab, this indents to the |
| 148 | specified left-margin column." |
| 149 | (interactive "*") |
| 150 | (save-excursion |
| 151 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) |
| 152 | (indent-according-to-mode)) |
| 153 | (insert ?\n) |
| 154 | (indent-according-to-mode)) |
| 155 | |
| 156 | ;; Internal subroutine of delete-char |
| 157 | (defun kill-forward-chars (arg) |
| 158 | (if (listp arg) (setq arg (car arg))) |
| 159 | (if (eq arg '-) (setq arg -1)) |
| 160 | (kill-region (point) (+ (point) arg))) |
| 161 | |
| 162 | ;; Internal subroutine of backward-delete-char |
| 163 | (defun kill-backward-chars (arg) |
| 164 | (if (listp arg) (setq arg (car arg))) |
| 165 | (if (eq arg '-) (setq arg -1)) |
| 166 | (kill-region (point) (- (point) arg))) |
| 167 | |
| 168 | (defun backward-delete-char-untabify (arg &optional killp) |
| 169 | "Delete characters backward, changing tabs into spaces. |
| 170 | Delete ARG chars, and kill (save in kill ring) if KILLP is non-nil. |
| 171 | Interactively, ARG is the prefix arg (default 1) |
| 172 | and KILLP is t if prefix arg is was specified." |
| 173 | (interactive "*p\nP") |
| 174 | (let ((count arg)) |
| 175 | (save-excursion |
| 176 | (while (and (> count 0) (not (bobp))) |
| 177 | (if (= (preceding-char) ?\t) |
| 178 | (let ((col (current-column))) |
| 179 | (forward-char -1) |
| 180 | (setq col (- col (current-column))) |
| 181 | (insert-char ?\ col) |
| 182 | (delete-char 1))) |
| 183 | (forward-char -1) |
| 184 | (setq count (1- count))))) |
| 185 | (delete-backward-char arg killp) |
| 186 | ;; In overwrite mode, back over columns while clearing them out, |
| 187 | ;; unless at end of line. |
| 188 | (and overwrite-mode (not (eolp)) |
| 189 | (save-excursion (insert-char ?\ arg)))) |
| 190 | |
| 191 | (defun zap-to-char (arg char) |
| 192 | "Kill up to and including ARG'th occurrence of CHAR. |
| 193 | Goes backward if ARG is negative; error if CHAR not found." |
| 194 | (interactive "p\ncZap to char: ") |
| 195 | (kill-region (point) (progn |
| 196 | (search-forward (char-to-string char) nil nil arg) |
| 197 | ; (goto-char (if (> arg 0) (1- (point)) (1+ (point)))) |
| 198 | (point)))) |
| 199 | |
| 200 | (defun beginning-of-buffer (&optional arg) |
| 201 | "Move point to the beginning of the buffer; leave mark at previous position. |
| 202 | With arg N, put point N/10 of the way from the true beginning. |
| 203 | Don't use this in Lisp programs! |
| 204 | \(goto-char (point-min)) is faster and avoids clobbering the mark." |
| 205 | (interactive "P") |
| 206 | (push-mark) |
| 207 | (goto-char (if arg |
| 208 | (if (> (buffer-size) 10000) |
| 209 | ;; Avoid overflow for large buffer sizes! |
| 210 | (* (prefix-numeric-value arg) |
| 211 | (/ (buffer-size) 10)) |
| 212 | (/ (+ 10 (* (buffer-size) (prefix-numeric-value arg))) 10)) |
| 213 | (point-min))) |
| 214 | (if arg (forward-line 1))) |
| 215 | |
| 216 | (defun end-of-buffer (&optional arg) |
| 217 | "Move point to the end of the buffer; leave mark at previous position. |
| 218 | With arg N, put point N/10 of the way from the true end. |
| 219 | Don't use this in Lisp programs! |
| 220 | \(goto-char (point-max)) is faster and avoids clobbering the mark." |
| 221 | (interactive "P") |
| 222 | (push-mark) |
| 223 | (goto-char (if arg |
| 224 | (- (1+ (buffer-size)) |
| 225 | (if (> (buffer-size) 10000) |
| 226 | ;; Avoid overflow for large buffer sizes! |
| 227 | (* (prefix-numeric-value arg) |
| 228 | (/ (buffer-size) 10)) |
| 229 | (/ (* (buffer-size) (prefix-numeric-value arg)) 10))) |
| 230 | (point-max))) |
| 231 | (if arg (forward-line 1) |
| 232 | ;; Scroll to put point near bottom--show nearly maximum amount of text, |
| 233 | ;; but leave room to add something. |
| 234 | (recenter -3))) |
| 235 | |
| 236 | (defun mark-whole-buffer () |
| 237 | "Put point at beginning and mark at end of buffer." |
| 238 | (interactive) |
| 239 | (push-mark (point)) |
| 240 | (push-mark (point-max)) |
| 241 | (goto-char (point-min))) |
| 242 | |
| 243 | (defun count-lines-region (start end) |
| 244 | "Print number of lines and charcters in the region." |
| 245 | (interactive "r") |
| 246 | (message "Region has %d lines, %d characters" |
| 247 | (count-lines start end) (- end start))) |
| 248 | |
| 249 | (defun what-line () |
| 250 | "Print the current line number (in the buffer) of point." |
| 251 | (interactive) |
| 252 | (save-restriction |
| 253 | (widen) |
| 254 | (save-excursion |
| 255 | (beginning-of-line) |
| 256 | (message "Line %d" |
| 257 | (1+ (count-lines 1 (point))))))) |
| 258 | |
| 259 | (defun count-lines (start end) |
| 260 | "Return number of lines between START and END. |
| 261 | This is usually the number of newlines between them, |
| 262 | but will be one more if START is not equal to END |
| 263 | and the greater of them is not at the start of a line." |
| 264 | (save-excursion |
| 265 | (save-restriction |
| 266 | (narrow-to-region start end) |
| 267 | (goto-char (point-min)) |
| 268 | (if (eq selective-display t) |
| 269 | (let ((done 0)) |
| 270 | (while (re-search-forward "[\n\C-m]" nil t 40) |
| 271 | (setq done (+ 40 done))) |
| 272 | (while (re-search-forward "[\n\C-m]" nil t 1) |
| 273 | (setq done (+ 1 done))) |
| 274 | done) |
| 275 | (- (buffer-size) (forward-line (buffer-size))))))) |
| 276 | |
| 277 | (defun what-cursor-position () |
| 278 | "Print info on cursor position (on screen and within buffer)." |
| 279 | (interactive) |
| 280 | (let* ((char (following-char)) |
| 281 | (beg (point-min)) |
| 282 | (end (point-max)) |
| 283 | (pos (point)) |
| 284 | (total (buffer-size)) |
| 285 | (percent (if (> total 50000) |
| 286 | ;; Avoid overflow from multiplying by 100! |
| 287 | (/ (+ (/ total 200) (1- pos)) (max (/ total 100) 1)) |
| 288 | (/ (+ (/ total 2) (* 100 (1- pos))) (max total 1)))) |
| 289 | (hscroll (if (= (window-hscroll) 0) |
| 290 | "" |
| 291 | (format " Hscroll=%d" (window-hscroll)))) |
| 292 | (col (current-column))) |
| 293 | (if (= pos end) |
| 294 | (if (or (/= beg 1) (/= end (1+ total))) |
| 295 | (message "point=%d of %d(%d%%) <%d - %d> column %d %s" |
| 296 | pos total percent beg end col hscroll) |
| 297 | (message "point=%d of %d(%d%%) column %d %s" |
| 298 | pos total percent col hscroll)) |
| 299 | (if (or (/= beg 1) (/= end (1+ total))) |
| 300 | (message "Char: %s (0%o) point=%d of %d(%d%%) <%d - %d> column %d %s" |
| 301 | (single-key-description char) char pos total percent beg end col hscroll) |
| 302 | (message "Char: %s (0%o) point=%d of %d(%d%%) column %d %s" |
| 303 | (single-key-description char) char pos total percent col hscroll))))) |
| 304 | |
| 305 | (defun fundamental-mode () |
| 306 | "Major mode not specialized for anything in particular. |
| 307 | Other major modes are defined by comparison with this one." |
| 308 | (interactive) |
| 309 | (kill-all-local-variables)) |
| 310 | |
| 311 | (put 'eval-expression 'disabled t) |
| 312 | |
| 313 | ;; We define this, rather than making eval interactive, |
| 314 | ;; for the sake of completion of names like eval-region, eval-current-buffer. |
| 315 | (defun eval-expression (expression) |
| 316 | "Evaluate EXPRESSION and print value in minibuffer. |
| 317 | Value is also consed on to front of variable values 's value." |
| 318 | (interactive "xEval: ") |
| 319 | (setq values (cons (eval expression) values)) |
| 320 | (prin1 (car values) t)) |
| 321 | |
| 322 | (defun edit-and-eval-command (prompt command) |
| 323 | "Prompting with PROMPT, let user edit COMMAND and eval result. |
| 324 | COMMAND is a Lisp expression. Let user edit that expression in |
| 325 | the minibuffer, then read and evaluate the result." |
| 326 | (let ((command (read-minibuffer prompt |
| 327 | (prin1-to-string command)))) |
| 328 | ;; Add edited command to command history, unless redundant. |
| 329 | (or (equal command (car command-history)) |
| 330 | (setq command-history (cons command command-history))) |
| 331 | (eval command))) |
| 332 | |
| 333 | ;; (defvar repeat-complex-command nil) |
| 334 | |
| 335 | (defvar repeat-complex-command-map (copy-keymap minibuffer-local-map)) |
| 336 | (define-key repeat-complex-command-map "\ep" 'previous-complex-command) |
| 337 | (define-key repeat-complex-command-map "\en" 'next-complex-command) |
| 338 | (defun repeat-complex-command (repeat-complex-command-arg) |
| 339 | "Edit and re-evaluate last complex command, or ARGth from last. |
| 340 | A complex command is one which used the minibuffer. |
| 341 | The command is placed in the minibuffer as a Lisp form for editing. |
| 342 | The result is executed, repeating the command as changed. |
| 343 | If the command has been changed or is not the most recent previous command |
| 344 | it is added to the front of the command history. |
| 345 | Whilst editing the command, the following commands are available: |
| 346 | \\{repeat-complex-command-map}" |
| 347 | (interactive "p") |
| 348 | (let ((elt (nth (1- repeat-complex-command-arg) command-history)) |
| 349 | (repeat-complex-command-flag t) |
| 350 | newcmd) |
| 351 | (if elt |
| 352 | (progn |
| 353 | (setq newcmd (read-from-minibuffer "Redo: " |
| 354 | (prin1-to-string elt) |
| 355 | repeat-complex-command-map |
| 356 | t)) |
| 357 | ;; If command to be redone does not match front of history, |
| 358 | ;; add it to the history. |
| 359 | (or (equal newcmd (car command-history)) |
| 360 | (setq command-history (cons newcmd command-history))) |
| 361 | (eval newcmd)) |
| 362 | (ding)))) |
| 363 | |
| 364 | (defun next-complex-command (n) |
| 365 | "Inserts the next element of `command-history' into the minibuffer." |
| 366 | (interactive "p") |
| 367 | (let ((narg (min (max 1 (- repeat-complex-command-arg n)) |
| 368 | (length command-history)))) |
| 369 | (if (= repeat-complex-command-arg narg) |
| 370 | (error (if (= repeat-complex-command-arg 1) |
| 371 | "No following item in command history" |
| 372 | "No preceding item in command history")) |
| 373 | (erase-buffer) |
| 374 | (setq repeat-complex-command-arg narg) |
| 375 | (insert (prin1-to-string (nth (1- repeat-complex-command-arg) |
| 376 | command-history))) |
| 377 | (goto-char (point-min))))) |
| 378 | |
| 379 | (defun previous-complex-command (n) |
| 380 | "Inserts the previous element of `command-history' into the minibuffer." |
| 381 | (interactive "p") |
| 382 | (if repeat-complex-command-flag |
| 383 | (next-complex-command (- n)) |
| 384 | (repeat-complex-command 1))) |
| 385 | |
| 386 | (defun goto-line (arg) |
| 387 | "Goto line ARG, counting from line 1 at beginning of buffer." |
| 388 | (interactive "NGoto line: ") |
| 389 | (save-restriction |
| 390 | (widen) |
| 391 | (goto-char 1) |
| 392 | (if (eq selective-display t) |
| 393 | (re-search-forward "[\n\C-m]" nil 'end (1- arg)) |
| 394 | (forward-line (1- arg))))) |
| 395 | |
| 396 | ;Put this on C-x u, so we can force that rather than C-_ into startup msg |
| 397 | (fset 'advertised-undo 'undo) |
| 398 | |
| 399 | (defun undo (&optional arg) |
| 400 | "Undo some previous changes. |
| 401 | Repeat this command to undo more changes. |
| 402 | A numeric argument serves as a repeat count." |
| 403 | (interactive "*p") |
| 404 | (let ((modified (buffer-modified-p))) |
| 405 | (or (eq (selected-window) (minibuffer-window)) |
| 406 | (message "Undo!")) |
| 407 | (or (eq last-command 'undo) |
| 408 | (progn (undo-start) |
| 409 | (undo-more 1))) |
| 410 | (setq this-command 'undo) |
| 411 | (undo-more (or arg 1)) |
| 412 | (and modified (not (buffer-modified-p)) |
| 413 | (delete-auto-save-file-if-necessary)))) |
| 414 | |
| 415 | (defun undo-start () |
| 416 | "Move pending-undo-list to front of undo records. |
| 417 | The next call to undo-more will undo the most recently made change." |
| 418 | (if (eq buffer-undo-list t) |
| 419 | (error "No undo information in this buffer")) |
| 420 | (setq pending-undo-list buffer-undo-list)) |
| 421 | |
| 422 | (defun undo-more (count) |
| 423 | "Undo back N undo-boundaries beyond what was already undone recently. |
| 424 | Call undo-start to get ready to undo recent changes, |
| 425 | then call undo-more one or more times to undo them." |
| 426 | (or pending-undo-list |
| 427 | (error "No further undo information")) |
| 428 | (setq pending-undo-list (primitive-undo count pending-undo-list))) |
| 429 | |
| 430 | (defvar last-shell-command "") |
| 431 | (defvar last-shell-command-on-region "") |
| 432 | |
| 433 | (defun shell-command (command &optional flag) |
| 434 | "Execute string COMMAND in inferior shell; display output, if any. |
| 435 | If COMMAND ends in ampersand, execute it asynchronously. |
| 436 | |
| 437 | Optional second arg non-nil (prefix arg, if interactive) |
| 438 | means insert output in current buffer after point (leave mark after it). |
| 439 | This cannot be done asynchronously." |
| 440 | (interactive (list (read-string "Shell command: " last-shell-command) |
| 441 | current-prefix-arg)) |
| 442 | (if flag |
| 443 | (progn (barf-if-buffer-read-only) |
| 444 | (push-mark) |
| 445 | ;; We do not use -f for csh; we will not support broken use of |
| 446 | ;; .cshrcs. Even the BSD csh manual says to use |
| 447 | ;; "if ($?prompt) exit" before things which are not useful |
| 448 | ;; non-interactively. Besides, if someone wants their other |
| 449 | ;; aliases for shell commands then they can still have them. |
| 450 | (call-process shell-file-name nil t nil |
| 451 | "-c" command) |
| 452 | (exchange-point-and-mark)) |
| 453 | ;; Preserve the match data in case called from a program. |
| 454 | (let ((data (match-data))) |
| 455 | (unwind-protect |
| 456 | (if (string-match "[ \t]*&[ \t]*$" command) |
| 457 | ;; Command ending with ampersand means asynchronous. |
| 458 | (let ((buffer (get-buffer-create "*shell-command*")) |
| 459 | (directory default-directory) |
| 460 | proc) |
| 461 | ;; Remove the ampersand. |
| 462 | (setq command (substring command 0 (match-beginning 0))) |
| 463 | ;; If will kill a process, query first. |
| 464 | (setq proc (get-buffer-process buffer)) |
| 465 | (if proc |
| 466 | (if (yes-or-no-p "A command is running. Kill it? ") |
| 467 | (kill-process proc) |
| 468 | (error "Shell command in progress"))) |
| 469 | (save-excursion |
| 470 | (set-buffer buffer) |
| 471 | (erase-buffer) |
| 472 | (display-buffer buffer) |
| 473 | (setq default-directory directory) |
| 474 | (setq proc (start-process "Shell" buffer |
| 475 | shell-file-name "-c" command)) |
| 476 | (setq mode-line-process '(": %s")) |
| 477 | (set-process-sentinel proc 'shell-command-sentinel) |
| 478 | (set-process-filter proc 'shell-command-filter) |
| 479 | )) |
| 480 | (shell-command-on-region (point) (point) command nil)) |
| 481 | (store-match-data data))))) |
| 482 | |
| 483 | ;; We have a sentinel to prevent insertion of a termination message |
| 484 | ;; in the buffer itself. |
| 485 | (defun shell-command-sentinel (process signal) |
| 486 | (if (memq (process-status process) '(exit signal)) |
| 487 | (progn |
| 488 | (message "%s: %s." |
| 489 | (car (cdr (cdr (process-command process)))) |
| 490 | (substring signal 0 -1)) |
| 491 | (save-excursion |
| 492 | (set-buffer (process-buffer process)) |
| 493 | (setq mode-line-process nil)) |
| 494 | (delete-process process)))) |
| 495 | |
| 496 | (defun shell-command-filter (proc string) |
| 497 | ;; Do save-excursion by hand so that we can leave point numerically unchanged |
| 498 | ;; despite an insertion immediately after it. |
| 499 | (let* ((obuf (current-buffer)) |
| 500 | (buffer (process-buffer proc)) |
| 501 | opoint |
| 502 | (window (get-buffer-window buffer)) |
| 503 | (pos (window-start window))) |
| 504 | (unwind-protect |
| 505 | (progn |
| 506 | (set-buffer buffer) |
| 507 | (setq opoint (point)) |
| 508 | (goto-char (point-max)) |
| 509 | (insert-before-markers string)) |
| 510 | ;; insert-before-markers moved this marker: set it back. |
| 511 | (set-window-start window pos) |
| 512 | ;; Finish our save-excursion. |
| 513 | (goto-char opoint) |
| 514 | (set-buffer obuf)))) |
| 515 | |
| 516 | (defun shell-command-on-region (start end command &optional flag interactive) |
| 517 | "Execute string COMMAND in inferior shell with region as input. |
| 518 | Normally display output (if any) in temp buffer `*Shell Command Output*'; |
| 519 | Prefix arg means replace the region with it. |
| 520 | Noninteractive args are START, END, COMMAND, FLAG. |
| 521 | Noninteractively FLAG means insert output in place of text from START to END, |
| 522 | and put point at the end, but don't alter the mark. |
| 523 | |
| 524 | If the output is one line, it is displayed in the echo area, |
| 525 | but it is nonetheless available in buffer `*Shell Command Output*' |
| 526 | even though that buffer is not automatically displayed. If there is no output |
| 527 | or output is inserted in the current buffer then `*Shell Command Output*' is |
| 528 | deleted." |
| 529 | (interactive (list (min (point) (mark)) (max (point) (mark)) |
| 530 | (read-string "Shell command on region: " |
| 531 | last-shell-command-on-region) |
| 532 | current-prefix-arg |
| 533 | (prefix-numeric-value current-prefix-arg))) |
| 534 | (if flag |
| 535 | ;; Replace specified region with output from command. |
| 536 | (let ((swap (and interactive (< (point) (mark))))) |
| 537 | ;; Don't muck with mark |
| 538 | ;; unless called interactively. |
| 539 | (and interactive (push-mark)) |
| 540 | (call-process-region start end shell-file-name t t nil |
| 541 | "-c" command) |
| 542 | (if (get-buffer "*Shell Command Output*") |
| 543 | (kill-buffer "*Shell Command Output*")) |
| 544 | (and interactive swap (exchange-point-and-mark))) |
| 545 | ;; No prefix argument: put the output in a temp buffer, |
| 546 | ;; replacing its entire contents. |
| 547 | (let ((buffer (get-buffer-create "*Shell Command Output*"))) |
| 548 | (if (eq buffer (current-buffer)) |
| 549 | ;; If the input is the same buffer as the output, |
| 550 | ;; delete everything but the specified region, |
| 551 | ;; then replace that region with the output. |
| 552 | (progn (delete-region end (point-max)) |
| 553 | (delete-region (point-min) start) |
| 554 | (call-process-region (point-min) (point-max) |
| 555 | shell-file-name t t nil |
| 556 | "-c" command)) |
| 557 | ;; Clear the output buffer, then run the command with output there. |
| 558 | (save-excursion |
| 559 | (set-buffer buffer) |
| 560 | (erase-buffer)) |
| 561 | (call-process-region start end shell-file-name |
| 562 | nil buffer nil |
| 563 | "-c" command)) |
| 564 | ;; Report the amount of output. |
| 565 | (let ((lines (save-excursion |
| 566 | (set-buffer buffer) |
| 567 | (if (= (buffer-size) 0) |
| 568 | 0 |
| 569 | (count-lines (point-min) (point-max)))))) |
| 570 | (cond ((= lines 0) |
| 571 | (message "(Shell command completed with no output)") |
| 572 | (kill-buffer "*Shell Command Output*")) |
| 573 | ((= lines 1) |
| 574 | (message "%s" |
| 575 | (save-excursion |
| 576 | (set-buffer buffer) |
| 577 | (goto-char (point-min)) |
| 578 | (buffer-substring (point) |
| 579 | (progn (end-of-line) (point)))))) |
| 580 | (t |
| 581 | (set-window-start (display-buffer buffer) 1))))))) |
| 582 | \f |
| 583 | (defun universal-argument () |
| 584 | "Begin a numeric argument for the following command. |
| 585 | Digits or minus sign following \\[universal-argument] make up the numeric argument. |
| 586 | \\[universal-argument] following the digits or minus sign ends the argument. |
| 587 | \\[universal-argument] without digits or minus sign provides 4 as argument. |
| 588 | Repeating \\[universal-argument] without digits or minus sign |
| 589 | multiplies the argument by 4 each time." |
| 590 | (interactive nil) |
| 591 | (let ((factor 4) |
| 592 | key) |
| 593 | (describe-arg (list factor) 1) |
| 594 | (setq key (read-key-sequence nil)) |
| 595 | (while (equal (key-binding key) 'universal-argument) |
| 596 | (setq factor (* 4 factor)) |
| 597 | (describe-arg (list factor) 1) |
| 598 | (setq key (read-key-sequence nil))) |
| 599 | (prefix-arg-internal key factor nil))) |
| 600 | |
| 601 | (defun prefix-arg-internal (key factor value) |
| 602 | (let ((sign 1)) |
| 603 | (if (and (numberp value) (< value 0)) |
| 604 | (setq sign -1 value (- value))) |
| 605 | (if (eq value '-) |
| 606 | (setq sign -1 value nil)) |
| 607 | (describe-arg value sign) |
| 608 | (while (equal key "-") |
| 609 | (setq sign (- sign) factor nil) |
| 610 | (describe-arg value sign) |
| 611 | (setq key (read-key-sequence nil))) |
| 612 | (while (and (= (length key) 1) |
| 613 | (not (string< key "0")) |
| 614 | (not (string< "9" key))) |
| 615 | (setq value (+ (* (if (numberp value) value 0) 10) |
| 616 | (- (aref key 0) ?0)) |
| 617 | factor nil) |
| 618 | (describe-arg value sign) |
| 619 | (setq key (read-key-sequence nil))) |
| 620 | (setq prefix-arg |
| 621 | (cond (factor (list factor)) |
| 622 | ((numberp value) (* value sign)) |
| 623 | ((= sign -1) '-))) |
| 624 | ;; Calling universal-argument after digits |
| 625 | ;; terminates the argument but is ignored. |
| 626 | (if (eq (key-binding key) 'universal-argument) |
| 627 | (progn |
| 628 | (describe-arg value sign) |
| 629 | (setq key (read-key-sequence nil)))) |
| 630 | (if (= (length key) 1) |
| 631 | ;; Make sure self-insert-command finds the proper character; |
| 632 | ;; unread the character and let the command loop process it. |
| 633 | (setq unread-command-char (string-to-char key)) |
| 634 | ;; We can't push back a longer string, so we'll emulate the |
| 635 | ;; command loop ourselves. |
| 636 | (command-execute (key-binding key))))) |
| 637 | |
| 638 | (defun describe-arg (value sign) |
| 639 | (cond ((numberp value) |
| 640 | (message "Arg: %d" (* value sign))) |
| 641 | ((consp value) |
| 642 | (message "Arg: [%d]" (car value))) |
| 643 | ((< sign 0) |
| 644 | (message "Arg: -")))) |
| 645 | |
| 646 | (defun digit-argument (arg) |
| 647 | "Part of the numeric argument for the next command. |
| 648 | \\[universal-argument] following digits or minus sign ends the argument." |
| 649 | (interactive "P") |
| 650 | (prefix-arg-internal (char-to-string (logand last-command-char ?\177)) |
| 651 | nil arg)) |
| 652 | |
| 653 | (defun negative-argument (arg) |
| 654 | "Begin a negative numeric argument for the next command. |
| 655 | \\[universal-argument] following digits or minus sign ends the argument." |
| 656 | (interactive "P") |
| 657 | (prefix-arg-internal "-" nil arg)) |
| 658 | \f |
| 659 | (defun forward-to-indentation (arg) |
| 660 | "Move forward ARG lines and position at first nonblank character." |
| 661 | (interactive "p") |
| 662 | (forward-line arg) |
| 663 | (skip-chars-forward " \t")) |
| 664 | |
| 665 | (defun backward-to-indentation (arg) |
| 666 | "Move backward ARG lines and position at first nonblank character." |
| 667 | (interactive "p") |
| 668 | (forward-line (- arg)) |
| 669 | (skip-chars-forward " \t")) |
| 670 | |
| 671 | (defun kill-line (&optional arg) |
| 672 | "Kill the rest of the current line; if no nonblanks there, kill thru newline. |
| 673 | With prefix argument, kill that many lines from point. |
| 674 | Negative arguments kill lines backward. |
| 675 | |
| 676 | When calling from a program, nil means \"no arg\", |
| 677 | a number counts as a prefix arg." |
| 678 | (interactive "P") |
| 679 | (kill-region (point) |
| 680 | (progn |
| 681 | (if arg |
| 682 | (forward-line (prefix-numeric-value arg)) |
| 683 | (if (eobp) |
| 684 | (signal 'end-of-buffer nil)) |
| 685 | (if (looking-at "[ \t]*$") |
| 686 | (forward-line 1) |
| 687 | (end-of-line))) |
| 688 | (point)))) |
| 689 | \f |
| 690 | ;;;; The kill ring |
| 691 | |
| 692 | (defvar kill-ring nil |
| 693 | "List of killed text sequences.") |
| 694 | |
| 695 | (defconst kill-ring-max 30 |
| 696 | "*Maximum length of kill ring before oldest elements are thrown away.") |
| 697 | |
| 698 | (defvar kill-ring-yank-pointer nil |
| 699 | "The tail of the kill ring whose car is the last thing yanked.") |
| 700 | |
| 701 | (defun kill-append (string before-p) |
| 702 | (setcar kill-ring |
| 703 | (if before-p |
| 704 | (concat string (car kill-ring)) |
| 705 | (concat (car kill-ring) string)))) |
| 706 | |
| 707 | (defun kill-region (beg end) |
| 708 | "Kill between point and mark. |
| 709 | The text is deleted but saved in the kill ring. |
| 710 | The command \\[yank] can retrieve it from there. |
| 711 | \(If you want to kill and then yank immediately, use \\[copy-region-as-kill].) |
| 712 | |
| 713 | This is the primitive for programs to kill text (as opposed to deleting it). |
| 714 | Supply two arguments, character numbers indicating the stretch of text |
| 715 | to be killed. |
| 716 | Any command that calls this function is a \"kill command\". |
| 717 | If the previous command was also a kill command, |
| 718 | the text killed this time appends to the text killed last time |
| 719 | to make one entry in the kill ring." |
| 720 | (interactive "r") |
| 721 | (if (and (not (eq buffer-undo-list t)) |
| 722 | (not (eq last-command 'kill-region)) |
| 723 | (not (eq beg end)) |
| 724 | (not buffer-read-only)) |
| 725 | ;; Don't let the undo list be truncated before we can even access it. |
| 726 | (let ((undo-high-threshold (+ (- (max beg end) (min beg end)) 100))) |
| 727 | (delete-region beg end) |
| 728 | ;; Take the same string recorded for undo |
| 729 | ;; and put it in the kill-ring. |
| 730 | (setq kill-ring (cons (car (car buffer-undo-list)) kill-ring)) |
| 731 | (if (> (length kill-ring) kill-ring-max) |
| 732 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)) |
| 733 | (setq this-command 'kill-region) |
| 734 | (setq kill-ring-yank-pointer kill-ring)) |
| 735 | (copy-region-as-kill beg end) |
| 736 | (or buffer-read-only (delete-region beg end)))) |
| 737 | |
| 738 | (defvar interprogram-cut-function nil |
| 739 | "Function to call to make a killed region available to other programs. |
| 740 | |
| 741 | Most window systems provide some sort of facility for cutting and |
| 742 | pasting text between the windows of different programs. On startup, |
| 743 | this variable is set to a function which emacs will call to make the |
| 744 | most recently killed text available to other programs. |
| 745 | |
| 746 | The function takes one argument, TEXT, which is a string containing |
| 747 | the text which should be made available.") |
| 748 | |
| 749 | (defun copy-region-as-kill (beg end) |
| 750 | "Save the region as if killed, but don't kill it. |
| 751 | If `x-select-kill' is non-nil, also save the text for X cut and paste." |
| 752 | (interactive "r") |
| 753 | (if (eq last-command 'kill-region) |
| 754 | (kill-append (buffer-substring beg end) (< end beg)) |
| 755 | (setq kill-ring (cons (buffer-substring beg end) kill-ring)) |
| 756 | (if (> (length kill-ring) kill-ring-max) |
| 757 | (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))) |
| 758 | (if interprogram-cut-function |
| 759 | (funcall interprogram-cut-function (car kill-ring))) |
| 760 | (setq this-command 'kill-region |
| 761 | kill-ring-yank-pointer kill-ring) |
| 762 | nil) |
| 763 | |
| 764 | (defun kill-ring-save (beg end) |
| 765 | "Save the region as if killed, but don't kill it." |
| 766 | (interactive "r") |
| 767 | (copy-region-as-kill beg end) |
| 768 | (message "%d characters copied to kill ring" |
| 769 | (- (max beg end) (min beg end)))) |
| 770 | |
| 771 | (defun append-next-kill () |
| 772 | "Cause following command, if kill, to append to previous kill." |
| 773 | (interactive) |
| 774 | (if (interactive-p) |
| 775 | (progn |
| 776 | (setq this-command 'kill-region) |
| 777 | (message "If the next command is a kill, it will append")) |
| 778 | (setq last-command 'kill-region))) |
| 779 | |
| 780 | (defun rotate-yank-pointer (arg) |
| 781 | "Rotate the yanking point in the kill ring." |
| 782 | (interactive "p") |
| 783 | (let ((length (length kill-ring))) |
| 784 | (if (zerop length) |
| 785 | (error "Kill ring is empty") |
| 786 | (setq kill-ring-yank-pointer |
| 787 | (nthcdr (% (+ arg (- length (length kill-ring-yank-pointer))) |
| 788 | length) |
| 789 | kill-ring))))) |
| 790 | |
| 791 | (defun yank-pop (arg) |
| 792 | "Replace just-yanked stretch of killed-text with a different stretch. |
| 793 | This command is allowed only immediately after a yank or a yank-pop. |
| 794 | At such a time, the region contains a stretch of reinserted |
| 795 | previously-killed text. yank-pop deletes that text and inserts in its |
| 796 | place a different stretch of killed text. |
| 797 | |
| 798 | With no argument, the previous kill is inserted. |
| 799 | With argument n, the n'th previous kill is inserted. |
| 800 | If n is negative, this is a more recent kill. |
| 801 | |
| 802 | The sequence of kills wraps around, so that after the oldest one |
| 803 | comes the newest one." |
| 804 | (interactive "*p") |
| 805 | (if (not (eq last-command 'yank)) |
| 806 | (error "Previous command was not a yank")) |
| 807 | (setq this-command 'yank) |
| 808 | (let ((before (< (point) (mark)))) |
| 809 | (delete-region (point) (mark)) |
| 810 | (rotate-yank-pointer arg) |
| 811 | (set-mark (point)) |
| 812 | (insert (car kill-ring-yank-pointer)) |
| 813 | (if before (exchange-point-and-mark)))) |
| 814 | |
| 815 | (defun yank (&optional arg) |
| 816 | "Reinsert the last stretch of killed text. |
| 817 | More precisely, reinsert the stretch of killed text most recently |
| 818 | killed OR yanked. |
| 819 | With just C-U as argument, same but put point in front (and mark at end). |
| 820 | With argument n, reinsert the nth most recently killed stretch of killed |
| 821 | text. |
| 822 | See also the command \\[yank-pop]." |
| 823 | (interactive "*P") |
| 824 | (rotate-yank-pointer (if (listp arg) 0 |
| 825 | (if (eq arg '-) -1 |
| 826 | (1- arg)))) |
| 827 | (push-mark (point)) |
| 828 | (insert (car kill-ring-yank-pointer)) |
| 829 | (if (consp arg) |
| 830 | (exchange-point-and-mark))) |
| 831 | \f |
| 832 | (defun insert-buffer (buffer) |
| 833 | "Insert after point the contents of BUFFER. |
| 834 | Puts mark after the inserted text. |
| 835 | BUFFER may be a buffer or a buffer name." |
| 836 | (interactive (list (read-buffer "Insert buffer: " (other-buffer) t))) |
| 837 | (or (bufferp buffer) |
| 838 | (setq buffer (get-buffer buffer))) |
| 839 | (let (start end newmark) |
| 840 | (save-excursion |
| 841 | (save-excursion |
| 842 | (set-buffer buffer) |
| 843 | (setq start (point-min) end (point-max))) |
| 844 | (insert-buffer-substring buffer start end) |
| 845 | (setq newmark (point))) |
| 846 | (push-mark newmark))) |
| 847 | |
| 848 | (defun append-to-buffer (buffer start end) |
| 849 | "Append to specified buffer the text of the region. |
| 850 | It is inserted into that buffer before its point. |
| 851 | |
| 852 | When calling from a program, give three arguments: |
| 853 | BUFFER (or buffer name), START and END. |
| 854 | START and END specify the portion of the current buffer to be copied." |
| 855 | (interactive "BAppend to buffer: \nr") |
| 856 | (let ((oldbuf (current-buffer))) |
| 857 | (save-excursion |
| 858 | (set-buffer (get-buffer-create buffer)) |
| 859 | (insert-buffer-substring oldbuf start end)))) |
| 860 | |
| 861 | (defun prepend-to-buffer (buffer start end) |
| 862 | "Prepend to specified buffer the text of the region. |
| 863 | It is inserted into that buffer after its point. |
| 864 | |
| 865 | When calling from a program, give three arguments: |
| 866 | BUFFER (or buffer name), START and END. |
| 867 | START and END specify the portion of the current buffer to be copied." |
| 868 | (interactive "BPrepend to buffer: \nr") |
| 869 | (let ((oldbuf (current-buffer))) |
| 870 | (save-excursion |
| 871 | (set-buffer (get-buffer-create buffer)) |
| 872 | (save-excursion |
| 873 | (insert-buffer-substring oldbuf start end))))) |
| 874 | |
| 875 | (defun copy-to-buffer (buffer start end) |
| 876 | "Copy to specified buffer the text of the region. |
| 877 | It is inserted into that buffer, replacing existing text there. |
| 878 | |
| 879 | When calling from a program, give three arguments: |
| 880 | BUFFER (or buffer name), START and END. |
| 881 | START and END specify the portion of the current buffer to be copied." |
| 882 | (interactive "BCopy to buffer: \nr") |
| 883 | (let ((oldbuf (current-buffer))) |
| 884 | (save-excursion |
| 885 | (set-buffer (get-buffer-create buffer)) |
| 886 | (erase-buffer) |
| 887 | (save-excursion |
| 888 | (insert-buffer-substring oldbuf start end))))) |
| 889 | \f |
| 890 | (defun mark () |
| 891 | "Return this buffer's mark value as integer, or nil if no mark. |
| 892 | If you are using this in an editing command, you are most likely making |
| 893 | a mistake; see the documentation of `set-mark'." |
| 894 | (marker-position (mark-marker))) |
| 895 | |
| 896 | (defun set-mark (pos) |
| 897 | "Set this buffer's mark to POS. Don't use this function! |
| 898 | That is to say, don't use this function unless you want |
| 899 | the user to see that the mark has moved, and you want the previous |
| 900 | mark position to be lost. |
| 901 | |
| 902 | Normally, when a new mark is set, the old one should go on the stack. |
| 903 | This is why most applications should use push-mark, not set-mark. |
| 904 | |
| 905 | Novice emacs-lisp programmers often try to use the mark for the wrong |
| 906 | purposes. The mark saves a location for the user's convenience. |
| 907 | Most editing commands should not alter the mark. |
| 908 | To remember a location for internal use in the Lisp program, |
| 909 | store it in a Lisp variable. Example: |
| 910 | |
| 911 | (let ((beg (point))) (forward-line 1) (delete-region beg (point)))." |
| 912 | |
| 913 | (set-marker (mark-marker) pos (current-buffer))) |
| 914 | |
| 915 | (defvar mark-ring nil |
| 916 | "The list of saved former marks of the current buffer, |
| 917 | most recent first.") |
| 918 | (make-variable-buffer-local 'mark-ring) |
| 919 | |
| 920 | (defconst mark-ring-max 16 |
| 921 | "*Maximum size of mark ring. Start discarding off end if gets this big.") |
| 922 | |
| 923 | (defun set-mark-command (arg) |
| 924 | "Set mark at where point is, or jump to mark. |
| 925 | With no prefix argument, set mark, and push previous mark on mark ring. |
| 926 | With argument, jump to mark, and pop into mark off the mark ring. |
| 927 | |
| 928 | Novice emacs-lisp programmers often try to use the mark for the wrong |
| 929 | purposes. See the documentation of `set-mark' for more information." |
| 930 | (interactive "P") |
| 931 | (if (null arg) |
| 932 | (push-mark) |
| 933 | (if (null (mark)) |
| 934 | (error "No mark set in this buffer") |
| 935 | (goto-char (mark)) |
| 936 | (pop-mark)))) |
| 937 | |
| 938 | (defun push-mark (&optional location nomsg) |
| 939 | "Set mark at LOCATION (point, by default) and push old mark on mark ring. |
| 940 | Displays \"Mark set\" unless the optional second arg NOMSG is non-nil. |
| 941 | |
| 942 | Novice emacs-lisp programmers often try to use the mark for the wrong |
| 943 | purposes. See the documentation of `set-mark' for more information." |
| 944 | (if (null (mark)) |
| 945 | nil |
| 946 | (setq mark-ring (cons (copy-marker (mark-marker)) mark-ring)) |
| 947 | (if (> (length mark-ring) mark-ring-max) |
| 948 | (progn |
| 949 | (move-marker (car (nthcdr mark-ring-max mark-ring)) nil) |
| 950 | (setcdr (nthcdr (1- mark-ring-max) mark-ring) nil)))) |
| 951 | (set-mark (or location (point))) |
| 952 | (or nomsg executing-macro (> (minibuffer-depth) 0) |
| 953 | (message "Mark set")) |
| 954 | nil) |
| 955 | |
| 956 | (defun pop-mark () |
| 957 | "Pop off mark ring into the buffer's actual mark. |
| 958 | Does not set point. Does nothing if mark ring is empty." |
| 959 | (if mark-ring |
| 960 | (progn |
| 961 | (setq mark-ring (nconc mark-ring (list (copy-marker (mark-marker))))) |
| 962 | (set-mark (+ 0 (car mark-ring))) |
| 963 | (move-marker (car mark-ring) nil) |
| 964 | (if (null (mark)) (ding)) |
| 965 | (setq mark-ring (cdr mark-ring))))) |
| 966 | |
| 967 | (fset 'exchange-dot-and-mark 'exchange-point-and-mark) |
| 968 | (defun exchange-point-and-mark () |
| 969 | "Put the mark where point is now, and point where the mark is now." |
| 970 | (interactive nil) |
| 971 | (let ((omark (mark))) |
| 972 | (if (null omark) |
| 973 | (error "No mark set in this buffer")) |
| 974 | (set-mark (point)) |
| 975 | (goto-char omark) |
| 976 | nil)) |
| 977 | \f |
| 978 | (defun next-line (arg) |
| 979 | "Move cursor vertically down ARG lines. |
| 980 | If there is no character in the target line exactly under the current column, |
| 981 | the cursor is positioned after the character in that line which spans this |
| 982 | column, or at the end of the line if it is not long enough. |
| 983 | If there is no line in the buffer after this one, |
| 984 | a newline character is inserted to create a line |
| 985 | and the cursor moves to that line. |
| 986 | |
| 987 | The command \\[set-goal-column] can be used to create |
| 988 | a semipermanent goal column to which this command always moves. |
| 989 | Then it does not try to move vertically. This goal column is stored |
| 990 | in `goal-column', which is nil when there is none. |
| 991 | |
| 992 | If you are thinking of using this in a Lisp program, consider |
| 993 | using `forward-line' instead. It is usually easier to use |
| 994 | and more reliable (no dependence on goal column, etc.)." |
| 995 | (interactive "p") |
| 996 | (if (= arg 1) |
| 997 | (let ((opoint (point))) |
| 998 | (forward-line 1) |
| 999 | (if (or (= opoint (point)) |
| 1000 | (not (eq (preceding-char) ?\n))) |
| 1001 | (insert ?\n) |
| 1002 | (goto-char opoint) |
| 1003 | (line-move arg))) |
| 1004 | (line-move arg)) |
| 1005 | nil) |
| 1006 | |
| 1007 | (defun previous-line (arg) |
| 1008 | "Move cursor vertically up ARG lines. |
| 1009 | If there is no character in the target line exactly over the current column, |
| 1010 | the cursor is positioned after the character in that line which spans this |
| 1011 | column, or at the end of the line if it is not long enough. |
| 1012 | |
| 1013 | The command \\[set-goal-column] can be used to create |
| 1014 | a semipermanent goal column to which this command always moves. |
| 1015 | Then it does not try to move vertically. |
| 1016 | |
| 1017 | If you are thinking of using this in a Lisp program, consider using |
| 1018 | `forward-line' with negative argument instead.. It is usually easier |
| 1019 | to use and more reliable (no dependence on goal column, etc.)." |
| 1020 | (interactive "p") |
| 1021 | (line-move (- arg)) |
| 1022 | nil) |
| 1023 | |
| 1024 | (defconst track-eol nil |
| 1025 | "*Non-nil means vertical motion starting at end of line keeps to ends of lines. |
| 1026 | This means moving to the end of each line moved onto. |
| 1027 | The beginning of a blank line does not count as the end of a line.") |
| 1028 | |
| 1029 | (make-variable-buffer-local |
| 1030 | (defvar goal-column nil |
| 1031 | "*Semipermanent goal column for vertical motion, as set by \\[set-goal-column], or nil.")) |
| 1032 | |
| 1033 | (defvar temporary-goal-column 0 |
| 1034 | "Current goal column for vertical motion. |
| 1035 | It is the column where point was |
| 1036 | at the start of current run of vertical motion commands. |
| 1037 | When the `track-eol' feature is doing its job, the value is 9999.") |
| 1038 | |
| 1039 | (defun line-move (arg) |
| 1040 | (if (not (or (eq last-command 'next-line) |
| 1041 | (eq last-command 'previous-line))) |
| 1042 | (setq temporary-goal-column |
| 1043 | (if (and track-eol (eolp) |
| 1044 | ;; Don't count beg of empty line as end of line |
| 1045 | ;; unless we just did explicit end-of-line. |
| 1046 | (or (not (bolp)) (eq last-command 'end-of-line))) |
| 1047 | 9999 |
| 1048 | (current-column)))) |
| 1049 | (if (not (integerp selective-display)) |
| 1050 | (forward-line arg) |
| 1051 | ;; Move by arg lines, but ignore invisible ones. |
| 1052 | (while (> arg 0) |
| 1053 | (vertical-motion 1) |
| 1054 | (forward-char -1) |
| 1055 | (forward-line 1) |
| 1056 | (setq arg (1- arg))) |
| 1057 | (while (< arg 0) |
| 1058 | (vertical-motion -1) |
| 1059 | (beginning-of-line) |
| 1060 | (setq arg (1+ arg)))) |
| 1061 | (move-to-column (or goal-column temporary-goal-column)) |
| 1062 | nil) |
| 1063 | |
| 1064 | |
| 1065 | (defun set-goal-column (arg) |
| 1066 | "Set the current horizontal position as a goal for \\[next-line] and \\[previous-line]. |
| 1067 | Those commands will move to this position in the line moved to |
| 1068 | rather than trying to keep the same horizontal position. |
| 1069 | With a non-nil argument, clears out the goal column |
| 1070 | so that \\[next-line] and \\[previous-line] resume vertical motion." |
| 1071 | (interactive "P") |
| 1072 | (if arg |
| 1073 | (progn |
| 1074 | (setq goal-column nil) |
| 1075 | (message "No goal column")) |
| 1076 | (setq goal-column (current-column)) |
| 1077 | (message (substitute-command-keys |
| 1078 | "Goal column %d (use \\[set-goal-column] with an arg to unset it)") |
| 1079 | goal-column)) |
| 1080 | nil) |
| 1081 | \f |
| 1082 | (defun transpose-chars (arg) |
| 1083 | "Interchange characters around point, moving forward one character. |
| 1084 | With prefix arg ARG, effect is to take character before point |
| 1085 | and drag it forward past ARG other characters (backward if ARG negative). |
| 1086 | If no argument and at end of line, the previous two chars are exchanged." |
| 1087 | (interactive "*P") |
| 1088 | (and (null arg) (eolp) (forward-char -1)) |
| 1089 | (transpose-subr 'forward-char (prefix-numeric-value arg))) |
| 1090 | |
| 1091 | (defun transpose-words (arg) |
| 1092 | "Interchange words around point, leaving point at end of them. |
| 1093 | With prefix arg ARG, effect is to take word before or around point |
| 1094 | and drag it forward past ARG other words (backward if ARG negative). |
| 1095 | If ARG is zero, the words around or after point and around or after mark |
| 1096 | are interchanged." |
| 1097 | (interactive "*p") |
| 1098 | (transpose-subr 'forward-word arg)) |
| 1099 | |
| 1100 | (defun transpose-sexps (arg) |
| 1101 | "Like \\[transpose-words] but applies to sexps. |
| 1102 | Does not work on a sexp that point is in the middle of |
| 1103 | if it is a list or string." |
| 1104 | (interactive "*p") |
| 1105 | (transpose-subr 'forward-sexp arg)) |
| 1106 | |
| 1107 | (defun transpose-lines (arg) |
| 1108 | "Exchange current line and previous line, leaving point after both. |
| 1109 | With argument ARG, takes previous line and moves it past ARG lines. |
| 1110 | With argument 0, interchanges line point is in with line mark is in." |
| 1111 | (interactive "*p") |
| 1112 | (transpose-subr (function |
| 1113 | (lambda (arg) |
| 1114 | (if (= arg 1) |
| 1115 | (progn |
| 1116 | ;; Move forward over a line, |
| 1117 | ;; but create a newline if none exists yet. |
| 1118 | (end-of-line) |
| 1119 | (if (eobp) |
| 1120 | (newline) |
| 1121 | (forward-char 1))) |
| 1122 | (forward-line arg)))) |
| 1123 | arg)) |
| 1124 | |
| 1125 | (defun transpose-subr (mover arg) |
| 1126 | (let (start1 end1 start2 end2) |
| 1127 | (if (= arg 0) |
| 1128 | (progn |
| 1129 | (save-excursion |
| 1130 | (funcall mover 1) |
| 1131 | (setq end2 (point)) |
| 1132 | (funcall mover -1) |
| 1133 | (setq start2 (point)) |
| 1134 | (goto-char (mark)) |
| 1135 | (funcall mover 1) |
| 1136 | (setq end1 (point)) |
| 1137 | (funcall mover -1) |
| 1138 | (setq start1 (point)) |
| 1139 | (transpose-subr-1)) |
| 1140 | (exchange-point-and-mark))) |
| 1141 | (while (> arg 0) |
| 1142 | (funcall mover -1) |
| 1143 | (setq start1 (point)) |
| 1144 | (funcall mover 1) |
| 1145 | (setq end1 (point)) |
| 1146 | (funcall mover 1) |
| 1147 | (setq end2 (point)) |
| 1148 | (funcall mover -1) |
| 1149 | (setq start2 (point)) |
| 1150 | (transpose-subr-1) |
| 1151 | (goto-char end2) |
| 1152 | (setq arg (1- arg))) |
| 1153 | (while (< arg 0) |
| 1154 | (funcall mover -1) |
| 1155 | (setq start2 (point)) |
| 1156 | (funcall mover -1) |
| 1157 | (setq start1 (point)) |
| 1158 | (funcall mover 1) |
| 1159 | (setq end1 (point)) |
| 1160 | (funcall mover 1) |
| 1161 | (setq end2 (point)) |
| 1162 | (transpose-subr-1) |
| 1163 | (setq arg (1+ arg))))) |
| 1164 | |
| 1165 | (defun transpose-subr-1 () |
| 1166 | (if (> (min end1 end2) (max start1 start2)) |
| 1167 | (error "Don't have two things to transpose")) |
| 1168 | (let ((word1 (buffer-substring start1 end1)) |
| 1169 | (word2 (buffer-substring start2 end2))) |
| 1170 | (delete-region start2 end2) |
| 1171 | (goto-char start2) |
| 1172 | (insert word1) |
| 1173 | (goto-char (if (< start1 start2) start1 |
| 1174 | (+ start1 (- (length word1) (length word2))))) |
| 1175 | (delete-char (length word1)) |
| 1176 | (insert word2))) |
| 1177 | \f |
| 1178 | (defconst comment-column 32 |
| 1179 | "*Column to indent right-margin comments to. |
| 1180 | Setting this variable automatically makes it local to the current buffer.") |
| 1181 | (make-variable-buffer-local 'comment-column) |
| 1182 | |
| 1183 | (defconst comment-start nil |
| 1184 | "*String to insert to start a new comment, or nil if no comment syntax defined.") |
| 1185 | |
| 1186 | (defconst comment-start-skip nil |
| 1187 | "*Regexp to match the start of a comment plus everything up to its body. |
| 1188 | If there are any \\(...\\) pairs, the comment delimiter text is held to begin |
| 1189 | at the place matched by the close of the first pair.") |
| 1190 | |
| 1191 | (defconst comment-end "" |
| 1192 | "*String to insert to end a new comment. |
| 1193 | Should be an empty string if comments are terminated by end-of-line.") |
| 1194 | |
| 1195 | (defconst comment-indent-hook |
| 1196 | '(lambda () comment-column) |
| 1197 | "Function to compute desired indentation for a comment. |
| 1198 | This function is called with no args with point at the beginning of |
| 1199 | the comment's starting delimiter.") |
| 1200 | |
| 1201 | (defun indent-for-comment () |
| 1202 | "Indent this line's comment to comment column, or insert an empty comment." |
| 1203 | (interactive "*") |
| 1204 | (beginning-of-line 1) |
| 1205 | (if (null comment-start) |
| 1206 | (error "No comment syntax defined") |
| 1207 | (let* ((eolpos (save-excursion (end-of-line) (point))) |
| 1208 | cpos indent begpos) |
| 1209 | (if (re-search-forward comment-start-skip eolpos 'move) |
| 1210 | (progn (setq cpos (point-marker)) |
| 1211 | ;; Find the start of the comment delimiter. |
| 1212 | ;; If there were paren-pairs in comment-start-skip, |
| 1213 | ;; position at the end of the first pair. |
| 1214 | (if (match-end 1) |
| 1215 | (goto-char (match-end 1)) |
| 1216 | ;; If comment-start-skip matched a string with internal |
| 1217 | ;; whitespace (not final whitespace) then the delimiter |
| 1218 | ;; start at the end of that whitespace. |
| 1219 | ;; Otherwise, it starts at the beginning of what was matched. |
| 1220 | (skip-chars-backward " \t" (match-beginning 0)) |
| 1221 | (skip-chars-backward "^ \t" (match-beginning 0))))) |
| 1222 | (setq begpos (point)) |
| 1223 | ;; Compute desired indent. |
| 1224 | (if (= (current-column) |
| 1225 | (setq indent (funcall comment-indent-hook))) |
| 1226 | (goto-char begpos) |
| 1227 | ;; If that's different from current, change it. |
| 1228 | (skip-chars-backward " \t") |
| 1229 | (delete-region (point) begpos) |
| 1230 | (indent-to indent)) |
| 1231 | ;; An existing comment? |
| 1232 | (if cpos |
| 1233 | (progn (goto-char cpos) |
| 1234 | (set-marker cpos nil)) |
| 1235 | ;; No, insert one. |
| 1236 | (insert comment-start) |
| 1237 | (save-excursion |
| 1238 | (insert comment-end)))))) |
| 1239 | |
| 1240 | (defun set-comment-column (arg) |
| 1241 | "Set the comment column based on point. |
| 1242 | With no arg, set the comment column to the current column. |
| 1243 | With just minus as arg, kill any comment on this line. |
| 1244 | With any other arg, set comment column to indentation of the previous comment |
| 1245 | and then align or create a comment on this line at that column." |
| 1246 | (interactive "P") |
| 1247 | (if (eq arg '-) |
| 1248 | (kill-comment nil) |
| 1249 | (if arg |
| 1250 | (progn |
| 1251 | (save-excursion |
| 1252 | (beginning-of-line) |
| 1253 | (re-search-backward comment-start-skip) |
| 1254 | (beginning-of-line) |
| 1255 | (re-search-forward comment-start-skip) |
| 1256 | (goto-char (match-beginning 0)) |
| 1257 | (setq comment-column (current-column)) |
| 1258 | (message "Comment column set to %d" comment-column)) |
| 1259 | (indent-for-comment)) |
| 1260 | (setq comment-column (current-column)) |
| 1261 | (message "Comment column set to %d" comment-column)))) |
| 1262 | |
| 1263 | (defun kill-comment (arg) |
| 1264 | "Kill the comment on this line, if any. |
| 1265 | With argument, kill comments on that many lines starting with this one." |
| 1266 | ;; this function loses in a lot of situations. it incorrectly recognises |
| 1267 | ;; comment delimiters sometimes (ergo, inside a string), doesn't work |
| 1268 | ;; with multi-line comments, can kill extra whitespace if comment wasn't |
| 1269 | ;; through end-of-line, et cetera. |
| 1270 | (interactive "P") |
| 1271 | (or comment-start-skip (error "No comment syntax defined")) |
| 1272 | (let ((count (prefix-numeric-value arg)) endc) |
| 1273 | (while (> count 0) |
| 1274 | (save-excursion |
| 1275 | (end-of-line) |
| 1276 | (setq endc (point)) |
| 1277 | (beginning-of-line) |
| 1278 | (and (string< "" comment-end) |
| 1279 | (setq endc |
| 1280 | (progn |
| 1281 | (re-search-forward (regexp-quote comment-end) endc 'move) |
| 1282 | (skip-chars-forward " \t") |
| 1283 | (point)))) |
| 1284 | (beginning-of-line) |
| 1285 | (if (re-search-forward comment-start-skip endc t) |
| 1286 | (progn |
| 1287 | (goto-char (match-beginning 0)) |
| 1288 | (skip-chars-backward " \t") |
| 1289 | (kill-region (point) endc) |
| 1290 | ;; to catch comments a line beginnings |
| 1291 | (indent-according-to-mode)))) |
| 1292 | (if arg (forward-line 1)) |
| 1293 | (setq count (1- count))))) |
| 1294 | |
| 1295 | (defun comment-region (beg end &optional arg) |
| 1296 | "Comment the region; third arg numeric means use ARG comment characters. |
| 1297 | If ARG is negative, delete that many comment characters instead. |
| 1298 | Comments are terminated on each line, even for syntax in which newline does |
| 1299 | not end the comment. Blank lines do not get comments." |
| 1300 | ;; if someone wants it to only put a comment-start at the beginning and |
| 1301 | ;; comment-end at the end then typing it, C-x C-x, closing it, C-x C-x |
| 1302 | ;; is easy enough. No option is made here for other than commenting |
| 1303 | ;; every line. |
| 1304 | (interactive "r\np") |
| 1305 | (or comment-start (error "No comment syntax is defined")) |
| 1306 | (if (> beg end) (let (mid) (setq mid beg beg end end mid))) |
| 1307 | (save-excursion |
| 1308 | (save-restriction |
| 1309 | (let ((cs comment-start) (ce comment-end)) |
| 1310 | (cond ((not arg) (setq arg 1)) |
| 1311 | ((> arg 1) |
| 1312 | (while (> (setq arg (1- arg)) 0) |
| 1313 | (setq cs (concat cs comment-start) |
| 1314 | ce (concat ce comment-end))))) |
| 1315 | (narrow-to-region beg end) |
| 1316 | (goto-char beg) |
| 1317 | (while (not (eobp)) |
| 1318 | (if (< arg 0) |
| 1319 | (let ((count arg)) |
| 1320 | (while (and (> 1 (setq count (1+ count))) |
| 1321 | (looking-at (regexp-quote cs))) |
| 1322 | (delete-char (length cs))) |
| 1323 | (if (string= "" ce) () |
| 1324 | (setq count arg) |
| 1325 | (while (> 1 (setq count (1+ count))) |
| 1326 | (end-of-line) |
| 1327 | ;; this is questionable if comment-end ends in whitespace |
| 1328 | ;; that is pretty brain-damaged though |
| 1329 | (skip-chars-backward " \t") |
| 1330 | (backward-char (length ce)) |
| 1331 | (if (looking-at (regexp-quote ce)) |
| 1332 | (delete-char (length ce)))))) |
| 1333 | (if (looking-at "[ \t]*$") () |
| 1334 | (insert cs) |
| 1335 | (if (string= "" ce) () |
| 1336 | (end-of-line) |
| 1337 | (insert ce))) |
| 1338 | (search-forward "\n" nil 'move))))))) |
| 1339 | \f |
| 1340 | (defun backward-word (arg) |
| 1341 | "Move backward until encountering the end of a word. |
| 1342 | With argument, do this that many times. |
| 1343 | In programs, it is faster to call forward-word with negative arg." |
| 1344 | (interactive "p") |
| 1345 | (forward-word (- arg))) |
| 1346 | |
| 1347 | (defun mark-word (arg) |
| 1348 | "Set mark arg words away from point." |
| 1349 | (interactive "p") |
| 1350 | (push-mark |
| 1351 | (save-excursion |
| 1352 | (forward-word arg) |
| 1353 | (point)))) |
| 1354 | |
| 1355 | (defun kill-word (arg) |
| 1356 | "Kill characters forward until encountering the end of a word. |
| 1357 | With argument, do this that many times." |
| 1358 | (interactive "p") |
| 1359 | (kill-region (point) (progn (forward-word arg) (point)))) |
| 1360 | |
| 1361 | (defun backward-kill-word (arg) |
| 1362 | "Kill characters backward until encountering the end of a word. |
| 1363 | With argument, do this that many times." |
| 1364 | (interactive "p") |
| 1365 | (kill-word (- arg))) |
| 1366 | \f |
| 1367 | (defconst fill-prefix nil |
| 1368 | "*String for filling to insert at front of new line, or nil for none. |
| 1369 | Setting this variable automatically makes it local to the current buffer.") |
| 1370 | (make-variable-buffer-local 'fill-prefix) |
| 1371 | |
| 1372 | (defconst auto-fill-inhibit-regexp nil |
| 1373 | "*Regexp to match lines which should not be auto-filled.") |
| 1374 | |
| 1375 | (defun do-auto-fill () |
| 1376 | (let (give-up) |
| 1377 | (or (and auto-fill-inhibit-regexp |
| 1378 | (save-excursion (beginning-of-line) |
| 1379 | (looking-at auto-fill-inhibit-regexp))) |
| 1380 | (while (and (not give-up) (> (current-column) fill-column)) |
| 1381 | (let ((fill-point |
| 1382 | (let ((opoint (point))) |
| 1383 | (save-excursion |
| 1384 | (move-to-column (1+ fill-column)) |
| 1385 | (skip-chars-backward "^ \t\n") |
| 1386 | (if (bolp) |
| 1387 | (re-search-forward "[ \t]" opoint t)) |
| 1388 | (skip-chars-backward " \t") |
| 1389 | (point))))) |
| 1390 | ;; If there is a space on the line before fill-point, |
| 1391 | ;; and nonspaces precede it, break the line there. |
| 1392 | (if (save-excursion |
| 1393 | (goto-char fill-point) |
| 1394 | (not (bolp))) |
| 1395 | ;; If point is at the fill-point, do not `save-excursion'. |
| 1396 | ;; Otherwise, if a comment prefix or fill-prefix is inserted, |
| 1397 | ;; point will end up before it rather than after it. |
| 1398 | (if (save-excursion |
| 1399 | (skip-chars-backward " \t") |
| 1400 | (= (point) fill-point)) |
| 1401 | (indent-new-comment-line) |
| 1402 | (save-excursion |
| 1403 | (goto-char fill-point) |
| 1404 | (indent-new-comment-line))) |
| 1405 | ;; No place to break => stop trying. |
| 1406 | (setq give-up t))))))) |
| 1407 | |
| 1408 | (defconst comment-multi-line nil |
| 1409 | "*Non-nil means \\[indent-new-comment-line] should continue same comment |
| 1410 | on new line, with no new terminator or starter.") |
| 1411 | |
| 1412 | (defun indent-new-comment-line () |
| 1413 | "Break line at point and indent, continuing comment if presently within one. |
| 1414 | The body of the continued comment is indented under the previous comment line." |
| 1415 | (interactive "*") |
| 1416 | (let (comcol comstart) |
| 1417 | (skip-chars-backward " \t") |
| 1418 | (delete-region (point) |
| 1419 | (progn (skip-chars-forward " \t") |
| 1420 | (point))) |
| 1421 | (insert ?\n) |
| 1422 | (save-excursion |
| 1423 | (if (and comment-start-skip |
| 1424 | (let ((opoint (point))) |
| 1425 | (forward-line -1) |
| 1426 | (re-search-forward comment-start-skip opoint t))) |
| 1427 | ;; The old line is a comment. |
| 1428 | ;; Set WIN to the pos of the comment-start. |
| 1429 | ;; But if the comment is empty, look at preceding lines |
| 1430 | ;; to find one that has a nonempty comment. |
| 1431 | (let ((win (match-beginning 0))) |
| 1432 | (while (and (eolp) (not (bobp)) |
| 1433 | (let (opoint) |
| 1434 | (beginning-of-line) |
| 1435 | (setq opoint (point)) |
| 1436 | (forward-line -1) |
| 1437 | (re-search-forward comment-start-skip opoint t))) |
| 1438 | (setq win (match-beginning 0))) |
| 1439 | ;; Indent this line like what we found. |
| 1440 | (goto-char win) |
| 1441 | (setq comcol (current-column)) |
| 1442 | (setq comstart (buffer-substring (point) (match-end 0)))))) |
| 1443 | (if comcol |
| 1444 | (let ((comment-column comcol) |
| 1445 | (comment-start comstart) |
| 1446 | (comment-end comment-end)) |
| 1447 | (and comment-end (not (equal comment-end "")) |
| 1448 | (if (not comment-multi-line) |
| 1449 | (progn |
| 1450 | (forward-char -1) |
| 1451 | (insert comment-end) |
| 1452 | (forward-char 1)) |
| 1453 | (setq comment-column (+ comment-column (length comment-start)) |
| 1454 | comment-start ""))) |
| 1455 | (if (not (eolp)) |
| 1456 | (setq comment-end "")) |
| 1457 | (insert ?\n) |
| 1458 | (forward-char -1) |
| 1459 | (indent-for-comment) |
| 1460 | (save-excursion |
| 1461 | ;; Make sure we delete the newline inserted above. |
| 1462 | (end-of-line) |
| 1463 | (delete-char 1))) |
| 1464 | (if fill-prefix |
| 1465 | (insert fill-prefix) |
| 1466 | (indent-according-to-mode))))) |
| 1467 | |
| 1468 | (defun auto-fill-mode (&optional arg) |
| 1469 | "Toggle auto-fill mode. |
| 1470 | With arg, turn auto-fill mode on if and only if arg is positive. |
| 1471 | In auto-fill mode, inserting a space at a column beyond fill-column |
| 1472 | automatically breaks the line at a previous space." |
| 1473 | (interactive "P") |
| 1474 | (prog1 (setq auto-fill-function |
| 1475 | (if (if (null arg) |
| 1476 | (not auto-fill-function) |
| 1477 | (> (prefix-numeric-value arg) 0)) |
| 1478 | 'do-auto-fill |
| 1479 | nil)) |
| 1480 | ;; update mode-line |
| 1481 | (set-buffer-modified-p (buffer-modified-p)))) |
| 1482 | |
| 1483 | (defun turn-on-auto-fill () |
| 1484 | "Unconditionally turn on Auto Fill mode." |
| 1485 | (auto-fill-mode 1)) |
| 1486 | |
| 1487 | (defun set-fill-column (arg) |
| 1488 | "Set fill-column to current column, or to argument if given. |
| 1489 | fill-column's value is separate for each buffer." |
| 1490 | (interactive "P") |
| 1491 | (setq fill-column (if (integerp arg) arg (current-column))) |
| 1492 | (message "fill-column set to %d" fill-column)) |
| 1493 | \f |
| 1494 | (defun set-selective-display (arg) |
| 1495 | "Set selective-display to ARG; clear it if no arg. |
| 1496 | When selective-display is a number > 0, |
| 1497 | lines whose indentation is >= selective-display are not displayed. |
| 1498 | selective-display's value is separate for each buffer." |
| 1499 | (interactive "P") |
| 1500 | (if (eq selective-display t) |
| 1501 | (error "selective-display already in use for marked lines")) |
| 1502 | (setq selective-display |
| 1503 | (and arg (prefix-numeric-value arg))) |
| 1504 | (set-window-start (selected-window) (window-start (selected-window))) |
| 1505 | (princ "selective-display set to " t) |
| 1506 | (prin1 selective-display t) |
| 1507 | (princ "." t)) |
| 1508 | |
| 1509 | (defun overwrite-mode (arg) |
| 1510 | "Toggle overwrite mode. |
| 1511 | With arg, turn overwrite mode on iff arg is positive. |
| 1512 | In overwrite mode, printing characters typed in replace existing text |
| 1513 | on a one-for-one basis, rather than pushing it to the right." |
| 1514 | (interactive "P") |
| 1515 | (setq overwrite-mode |
| 1516 | (if (null arg) (not overwrite-mode) |
| 1517 | (> (prefix-numeric-value arg) 0))) |
| 1518 | (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line. |
| 1519 | \f |
| 1520 | (defvar blink-matching-paren t |
| 1521 | "*Non-nil means show matching open-paren when close-paren is inserted.") |
| 1522 | |
| 1523 | (defconst blink-matching-paren-distance 4000 |
| 1524 | "*If non-nil, is maximum distance to search for matching open-paren |
| 1525 | when close-paren is inserted.") |
| 1526 | |
| 1527 | (defun blink-matching-open () |
| 1528 | "Move cursor momentarily to the beginning of the sexp before point." |
| 1529 | (interactive) |
| 1530 | (and (> (point) (1+ (point-min))) |
| 1531 | (/= (char-syntax (char-after (- (point) 2))) ?\\ ) |
| 1532 | blink-matching-paren |
| 1533 | (let* ((oldpos (point)) |
| 1534 | (blinkpos) |
| 1535 | (mismatch)) |
| 1536 | (save-excursion |
| 1537 | (save-restriction |
| 1538 | (if blink-matching-paren-distance |
| 1539 | (narrow-to-region (max (point-min) |
| 1540 | (- (point) blink-matching-paren-distance)) |
| 1541 | oldpos)) |
| 1542 | (condition-case () |
| 1543 | (setq blinkpos (scan-sexps oldpos -1)) |
| 1544 | (error nil))) |
| 1545 | (and blinkpos (/= (char-syntax (char-after blinkpos)) |
| 1546 | ?\$) |
| 1547 | (setq mismatch |
| 1548 | (/= (char-after (1- oldpos)) |
| 1549 | (logand (lsh (aref (syntax-table) |
| 1550 | (char-after blinkpos)) |
| 1551 | -8) |
| 1552 | 255)))) |
| 1553 | (if mismatch (setq blinkpos nil)) |
| 1554 | (if blinkpos |
| 1555 | (progn |
| 1556 | (goto-char blinkpos) |
| 1557 | (if (pos-visible-in-window-p) |
| 1558 | (sit-for 1) |
| 1559 | (goto-char blinkpos) |
| 1560 | (message |
| 1561 | "Matches %s" |
| 1562 | (if (save-excursion |
| 1563 | (skip-chars-backward " \t") |
| 1564 | (not (bolp))) |
| 1565 | (buffer-substring (progn (beginning-of-line) (point)) |
| 1566 | (1+ blinkpos)) |
| 1567 | (buffer-substring blinkpos |
| 1568 | (progn |
| 1569 | (forward-char 1) |
| 1570 | (skip-chars-forward "\n \t") |
| 1571 | (end-of-line) |
| 1572 | (point))))))) |
| 1573 | (cond (mismatch |
| 1574 | (message "Mismatched parentheses")) |
| 1575 | ((not blink-matching-paren-distance) |
| 1576 | (message "Unmatched parenthesis")))))))) |
| 1577 | |
| 1578 | ;Turned off because it makes dbx bomb out. |
| 1579 | (setq blink-paren-function 'blink-matching-open) |
| 1580 | |
| 1581 | ; this is just something for the luser to see in a keymap -- this is not |
| 1582 | ; how quitting works normally! |
| 1583 | (defun keyboard-quit () |
| 1584 | "Signal a quit condition." |
| 1585 | (interactive) |
| 1586 | (signal 'quit nil)) |
| 1587 | |
| 1588 | (define-key global-map "\C-g" 'keyboard-quit) |
| 1589 | \f |
| 1590 | (defun set-variable (var val) |
| 1591 | "Set VARIABLE to VALUE. VALUE is a Lisp object. |
| 1592 | When using this interactively, supply a Lisp expression for VALUE. |
| 1593 | If you want VALUE to be a string, you must surround it with doublequotes." |
| 1594 | (interactive |
| 1595 | (let* ((var (read-variable "Set variable: ")) |
| 1596 | (minibuffer-help-form |
| 1597 | '(funcall myhelp)) |
| 1598 | (myhelp |
| 1599 | (function |
| 1600 | (lambda () |
| 1601 | (with-output-to-temp-buffer "*Help*" |
| 1602 | (prin1 var) |
| 1603 | (princ "\nDocumentation:\n") |
| 1604 | (princ (substring (documentation-property var 'variable-documentation) |
| 1605 | 1)) |
| 1606 | (if (boundp var) |
| 1607 | (let ((print-length 20)) |
| 1608 | (princ "\n\nCurrent value: ") |
| 1609 | (prin1 (symbol-value var)))) |
| 1610 | nil))))) |
| 1611 | (list var |
| 1612 | (eval-minibuffer (format "Set %s to value: " var))))) |
| 1613 | (set var val)) |
| 1614 | \f |
| 1615 | ;These commands are defined in editfns.c |
| 1616 | ;but they are not assigned to keys there. |
| 1617 | (put 'narrow-to-region 'disabled t) |
| 1618 | (define-key ctl-x-map "n" 'narrow-to-region) |
| 1619 | (define-key ctl-x-map "w" 'widen) |
| 1620 | |
| 1621 | (define-key global-map "\C-j" 'newline-and-indent) |
| 1622 | (define-key global-map "\C-m" 'newline) |
| 1623 | (define-key global-map "\C-o" 'open-line) |
| 1624 | (define-key esc-map "\C-o" 'split-line) |
| 1625 | (define-key global-map "\C-q" 'quoted-insert) |
| 1626 | (define-key esc-map "^" 'delete-indentation) |
| 1627 | (define-key esc-map "\\" 'delete-horizontal-space) |
| 1628 | (define-key esc-map "m" 'back-to-indentation) |
| 1629 | (define-key ctl-x-map "\C-o" 'delete-blank-lines) |
| 1630 | (define-key esc-map " " 'just-one-space) |
| 1631 | (define-key esc-map "z" 'zap-to-char) |
| 1632 | (define-key esc-map "=" 'count-lines-region) |
| 1633 | (define-key ctl-x-map "=" 'what-cursor-position) |
| 1634 | (define-key esc-map "\e" 'eval-expression) |
| 1635 | (define-key ctl-x-map "\e" 'repeat-complex-command) |
| 1636 | (define-key ctl-x-map "u" 'advertised-undo) |
| 1637 | (define-key global-map "\C-_" 'undo) |
| 1638 | (define-key esc-map "!" 'shell-command) |
| 1639 | (define-key esc-map "|" 'shell-command-on-region) |
| 1640 | |
| 1641 | (define-key global-map "\C-u" 'universal-argument) |
| 1642 | (let ((i ?0)) |
| 1643 | (while (<= i ?9) |
| 1644 | (define-key esc-map (char-to-string i) 'digit-argument) |
| 1645 | (setq i (1+ i)))) |
| 1646 | (define-key esc-map "-" 'negative-argument) |
| 1647 | |
| 1648 | (define-key global-map "\C-k" 'kill-line) |
| 1649 | (define-key global-map "\C-w" 'kill-region) |
| 1650 | (define-key esc-map "w" 'kill-ring-save) |
| 1651 | (define-key esc-map "\C-w" 'append-next-kill) |
| 1652 | (define-key global-map "\C-y" 'yank) |
| 1653 | (define-key esc-map "y" 'yank-pop) |
| 1654 | |
| 1655 | (define-key ctl-x-map "a" 'append-to-buffer) |
| 1656 | |
| 1657 | (define-key global-map "\C-@" 'set-mark-command) |
| 1658 | (define-key ctl-x-map "\C-x" 'exchange-point-and-mark) |
| 1659 | |
| 1660 | (define-key global-map "\C-n" 'next-line) |
| 1661 | (define-key global-map "\C-p" 'previous-line) |
| 1662 | (define-key ctl-x-map "\C-n" 'set-goal-column) |
| 1663 | |
| 1664 | (define-key global-map [up] 'previous-line) |
| 1665 | (define-key global-map [down] 'next-line) |
| 1666 | (define-key global-map [left] 'backward-char) |
| 1667 | (define-key global-map [right] 'forward-char) |
| 1668 | |
| 1669 | (define-key global-map "\C-t" 'transpose-chars) |
| 1670 | (define-key esc-map "t" 'transpose-words) |
| 1671 | (define-key esc-map "\C-t" 'transpose-sexps) |
| 1672 | (define-key ctl-x-map "\C-t" 'transpose-lines) |
| 1673 | |
| 1674 | (define-key esc-map ";" 'indent-for-comment) |
| 1675 | (define-key esc-map "j" 'indent-new-comment-line) |
| 1676 | (define-key esc-map "\C-j" 'indent-new-comment-line) |
| 1677 | (define-key ctl-x-map ";" 'set-comment-column) |
| 1678 | (define-key ctl-x-map "f" 'set-fill-column) |
| 1679 | (define-key ctl-x-map "$" 'set-selective-display) |
| 1680 | |
| 1681 | (define-key esc-map "@" 'mark-word) |
| 1682 | (define-key esc-map "f" 'forward-word) |
| 1683 | (define-key esc-map "b" 'backward-word) |
| 1684 | (define-key esc-map "d" 'kill-word) |
| 1685 | (define-key esc-map "\177" 'backward-kill-word) |
| 1686 | |
| 1687 | (define-key esc-map "<" 'beginning-of-buffer) |
| 1688 | (define-key esc-map ">" 'end-of-buffer) |
| 1689 | (define-key ctl-x-map "h" 'mark-whole-buffer) |
| 1690 | (define-key esc-map "\\" 'delete-horizontal-space) |
| 1691 | |
| 1692 | (fset 'mode-specific-command-prefix (make-sparse-keymap)) |
| 1693 | (defconst mode-specific-map (symbol-function 'mode-specific-command-prefix) |
| 1694 | "Keymap for characters following C-c.") |
| 1695 | (define-key global-map "\C-c" 'mode-specific-command-prefix) |