edt.el 65.9 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; edt.el --- Enhanced EDT Keypad Mode Emulation for GNU Emacs 19
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1986, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

5 6
;; Author: Kevin Gallagher <kgallagh@spd.dsccc.com>
;; Maintainer: Kevin Gallagher <kgallagh@spd.dsccc.com>
Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Keywords: emulations
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Richard M. Stallman's avatar
Richard M. Stallman committed
9 10 11 12
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Eric S. Raymond's avatar
Eric S. Raymond committed
13
;; the Free Software Foundation; either version 2, or (at your option)
Richard M. Stallman's avatar
Richard M. Stallman committed
14 15 16 17 18 19 20 21 22 23 24
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

25 26
;;; Usage:

Karl Heuer's avatar
Karl Heuer committed
27
;;  See edt-user.doc in the Emacs etc directory.
28 29 30 31 32 33 34 35 36 37 38 39

;; ====================================================================

;;;  Electric Help functions are used for keypad help displays.  A few
;;;  picture functions are used in rectangular cut and paste commands.
(require 'ehelp)
(require 'picture)

;;;;
;;;; VARIABLES and CONSTANTS
;;;;

Richard M. Stallman's avatar
Richard M. Stallman committed
40
(defvar edt-last-deleted-lines ""
41 42
  "Last text deleted by an EDT emulation line delete command.")

Richard M. Stallman's avatar
Richard M. Stallman committed
43
(defvar edt-last-deleted-words ""
44 45
  "Last text deleted by an EDT emulation word delete command.")

Richard M. Stallman's avatar
Richard M. Stallman committed
46
(defvar edt-last-deleted-chars ""
47 48 49 50 51 52
  "Last text deleted by an EDT emulation character delete command.")

(defvar edt-last-replaced-key-definition ""
  "Key definition replaced with edt-define-key or edt-learn command.")

(defvar edt-direction-string ""
Karl Heuer's avatar
Karl Heuer committed
53
  "String indicating current direction of movement.")
54 55

(defvar edt-select-mode nil
Karl Heuer's avatar
Karl Heuer committed
56
  "Non-nil means select mode is active.")
57 58

(defvar edt-select-mode-text ""
Karl Heuer's avatar
Karl Heuer committed
59
  "Text displayed in mode line when select mode is active.")
60 61

(defconst edt-select-mode-string " Select"
Karl Heuer's avatar
Karl Heuer committed
62
  "String to indicate select mode is active.")
63 64

(defconst edt-forward-string " ADVANCE"
Karl Heuer's avatar
Karl Heuer committed
65
  "Direction string in mode line to indicate forward movement.")
66 67

(defconst edt-backward-string "  BACKUP"
Karl Heuer's avatar
Karl Heuer committed
68
  "Direction string in mode line to indicate backward movement.")
69 70

(defvar edt-default-map-active nil
Karl Heuer's avatar
Karl Heuer committed
71 72
  "Non-nil indicates that default EDT emulation key bindings are active.
Nil means user-defined custom bindings are active.")
73 74

(defvar edt-user-map-configured nil
Karl Heuer's avatar
Karl Heuer committed
75 76
  "Non-nil indicates that user custom EDT key bindings are configured.
This means that an edt-user.el file was found in the user's load-path.")
77 78

(defvar edt-keep-current-page-delimiter nil
Karl Heuer's avatar
Karl Heuer committed
79 80 81 82
  "Non-nil leaves current value of page-delimiter unchanged.
Nil causes the page-delimiter variable to be set to to \"\\f\"
when edt-emulation-on is first invoked.  Original value is restored
when edt-emulation-off is called.")
83 84

(defvar edt-use-EDT-control-key-bindings nil
Karl Heuer's avatar
Karl Heuer committed
85 86 87
  "Non-nil causes the control key bindings to be replaced with EDT bindings.
Nil (the default) means EDT control key bindings are not used and the current
control key bindings are retained for use in the EDT emulation.")
88 89

(defvar edt-word-entities '(?\t)
Karl Heuer's avatar
Karl Heuer committed
90
  "*Specifies the list of EDT word entity characters.")
Richard M. Stallman's avatar
Richard M. Stallman committed
91

92 93 94 95 96 97
;;;
;;;  Emacs version identifiers - currently referenced by
;;;
;;;     o edt-emulation-on      o edt-load-xkeys
;;;
(defconst edt-emacs19-p (not (string-lessp emacs-version "19"))
Karl Heuer's avatar
Karl Heuer committed
98
  "Non-nil if we are running Lucid or GNU Emacs version 19.")
99 100 101

(defconst edt-lucid-emacs19-p
  (and edt-emacs19-p (string-match "Lucid" emacs-version))
Karl Heuer's avatar
Karl Heuer committed
102
  "Non-nil if we are running Lucid Emacs version 19.")
103 104

(defconst edt-gnu-emacs19-p (and edt-emacs19-p (not edt-lucid-emacs19-p))
Karl Heuer's avatar
Karl Heuer committed
105
  "Non-nil if we are running GNU Emacs version 19.")
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

(defvar edt-xkeys-file nil
  "File mapping X function keys to LK-201 keyboard function and keypad keys.")

;;;;
;;;; EDT Emulation Commands
;;;;

;;; Almost all of EDT's keypad mode commands have equivalent
;;; counterparts in Emacs.  Some behave the same way in Emacs as they
;;; do in EDT, but most do not.
;;;
;;; The following Emacs functions emulate, where practical, the exact
;;; behavior of the corresponding EDT keypad mode commands.  In a few
;;; cases, the emulation is not exact, but it is close enough for most
;;; EDT die-hards.
;;;
;;; In a very few cases, we chose to use the superior Emacs way of
;;; handling things.  For example, we do not emulate the EDT SUBS
;;; command.  Instead, we chose to use the superior Emacs
;;; query-replace function.
;;;

;;;
;;; PAGE
;;;
;;; Emacs uses the regexp assigned to page-delimiter to determine what
;;; marks a page break.  This is normally "^\f", which causes the
;;; edt-page command to ignore form feeds not located at the beginning
;;; of a line.  To emulate the EDT PAGE command exactly,
;;; page-delimiter is set to "\f" when EDT emulation is turned on, and
;;; restored to its original value when EDT emulation is turned off.
;;; But this can be overridden if the EDT definition is not desired by
;;; placing
;;;
;;;         (setq edt-keep-current-page-delimiter t)
;;;
;;; in your .emacs file.

(defun edt-page-forward (num)
  "Move forward to just after next page delimiter.
Accepts a positive prefix argument for the number of page delimiters to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (eobp)
      (error "End of buffer")
      (progn
        (forward-page num)
	(if (eobp)
	    (edt-line-to-bottom-of-window)
	    (edt-line-to-top-of-window)))))

(defun edt-page-backward (num)
  "Move backward to just after previous page delimiter.
Accepts a positive prefix argument for the number of page delimiters to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (bobp)
      (error "Beginning of buffer")
      (progn
        (backward-page num)
        (edt-line-to-top-of-window))))

(defun edt-page (num)
  "Move in current direction to next page delimiter.
Accepts a positive prefix argument for the number of page delimiters to move."
Richard M. Stallman's avatar
Richard M. Stallman committed
172
  (interactive "p")
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
  (if (equal edt-direction-string edt-forward-string)
      (edt-page-forward num)
      (edt-page-backward num)))

;;;
;;; SECT
;;;
;;; EDT defaults a section size to be 16 lines of its one and only
;;; 24-line window.  That's two-thirds of the window at a time.  The
;;; EDT SECT commands moves the cursor, not the window.  
;;; 
;;; This emulation of EDT's SECT moves the cursor approximately two-thirds
;;; of the current window at a time.

(defun edt-sect-forward (num)
  "Move cursor forward two-thirds of a window.
Accepts a positive prefix argument for the number of sections to move."
  (interactive "p")
  (edt-check-prefix num)
  (edt-line-forward (* (* (/ (- (window-height) 1) 3) 2) num)))

(defun edt-sect-backward (num)
  "Move cursor backward two-thirds of a window.
Accepts a positive prefix argument for the number of sections to move."
  (interactive "p")
  (edt-check-prefix num)
  (edt-line-backward (* (* (/ (- (window-height) 1) 3) 2) num)))

(defun edt-sect (num)
  "Move in current direction a full window.
Accepts a positive prefix argument for the number windows to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-sect-forward num)
      (edt-sect-backward num)))

;;;
;;; BEGINNING OF LINE
;;;
;;; EDT's beginning-of-line command is not affected by current
;;; direction, for some unknown reason.

(defun edt-beginning-of-line (num)
  "Move backward to next beginning of line mark.
Accepts a positive prefix argument for the number of BOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (bolp)
      (forward-line (* -1 num))
      (progn
        (setq num (1- num))
        (forward-line (* -1 num)))))

;;;
;;; EOL (End of Line)
;;;

(defun edt-end-of-line-forward (num)
  "Move forward to next end of line mark.
Accepts a positive prefix argument for the number of EOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (forward-char)
  (end-of-line num))

(defun edt-end-of-line-backward (num)
  "Move backward to next end of line mark.
Accepts a positive prefix argument for the number of EOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (end-of-line (1- num)))

(defun edt-end-of-line (num)
  "Move in current direction to next end of line mark.
Accepts a positive prefix argument for the number of EOL marks to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-end-of-line-forward num)
      (edt-end-of-line-backward num)))

;;;
;;; WORD
;;;
;;; This one is a tad messy.  To emulate EDT's behavior everywhere in
;;; the file (beginning of file, end of file, beginning of line, end
;;; of line, etc.) it takes a bit of special handling.  
;;; 
;;; The variable edt-word-entities contains a list of characters which
;;; are to be viewed as distinct words where ever they appear in the
;;; buffer.  This emulates the EDT line mode command SET ENTITY WORD. 


(defun edt-one-word-forward ()
  "Move forward to first character of next word."
  (interactive)
  (if (eobp)
      (error "End of buffer"))
  (if (eolp)
      (forward-char)
      (progn
        (if (memq (following-char) edt-word-entities)
            (forward-char)
            (while (and 
                     (not (eolp))
                     (not (eobp))
                     (not (eq ?\  (char-syntax (following-char))))
                     (not (memq (following-char) edt-word-entities)))
              (forward-char)))
        (while (and 
                 (not (eolp))
                 (not (eobp))
                 (eq ?\  (char-syntax (following-char)))
                 (not (memq (following-char) edt-word-entities)))
          (forward-char)))))

(defun edt-one-word-backward ()
  "Move backward to first character of previous word."
  (interactive)
  (if (bobp)
      (error "Beginning of buffer"))
  (if (bolp)
      (backward-char)
      (progn
        (backward-char)
        (while (and 
                 (not (bolp))
                 (not (bobp))
                 (eq ?\  (char-syntax (following-char)))
                 (not (memq (following-char) edt-word-entities)))
          (backward-char))
        (if (not (memq (following-char) edt-word-entities))
            (while (and 
                     (not (bolp))
                     (not (bobp))
                     (not (eq ?\  (char-syntax (preceding-char))))
                     (not (memq (preceding-char) edt-word-entities)))
              (backward-char))))))

(defun edt-word-forward (num)
  "Move forward to first character of next word.
Accepts a positive prefix argument for the number of words to move."
  (interactive "p")
  (edt-check-prefix num)
  (while (> num 0)
    (edt-one-word-forward)
    (setq num (1- num))))

(defun edt-word-backward (num)
  "Move backward to first character of previous word.
Accepts a positive prefix argument for the number of words to move."
  (interactive "p")
  (edt-check-prefix num)
  (while (> num 0)
    (edt-one-word-backward)
    (setq num (1- num))))

(defun edt-word (num)
  "Move in current direction to first character of next word.
Accepts a positive prefix argument for the number of words to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-word-forward num)
      (edt-word-backward num)))

;;;
;;; CHAR
;;;

(defun edt-character (num)
  "Move in current direction to next character.
Accepts a positive prefix argument for the number of characters to move."
  (interactive "p")
  (edt-check-prefix num)
  (if (equal edt-direction-string edt-forward-string)
      (forward-char num)
      (backward-char num)))

;;;
;;; LINE
;;;
;;; When direction is set to BACKUP, LINE behaves just like BEGINNING
;;; OF LINE in EDT.  So edt-line-backward is not really needed as a
;;; separate function.

(defun edt-line-backward (num)
  "Move backward to next beginning of line mark.
Accepts a positive prefix argument for the number of BOL marks to move."
  (interactive "p")
  (edt-beginning-of-line num))

(defun edt-line-forward (num)
  "Move forward to next beginning of line mark.
Accepts a positive prefix argument for the number of BOL marks to move."
  (interactive "p")
  (edt-check-prefix num)
  (forward-line num))

(defun edt-line (num)
  "Move in current direction to next beginning of line mark.
Accepts a positive prefix argument for the number of BOL marks to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-line-forward num)
      (edt-line-backward num)))

;;;
;;; TOP
;;;

(defun edt-top ()
  "Move cursor to the beginning of buffer."
  (interactive)
  (goto-char (point-min)))

;;;
;;; BOTTOM
;;;

(defun edt-bottom ()
  "Move cursor to the end of buffer."
  (interactive)
  (goto-char (point-max))
  (edt-line-to-bottom-of-window))

;;;
;;; FIND
;;;

(defun edt-find-forward (&optional find)
402
  "Find first occurrence of a string in forward direction and save it."
403 404 405 406 407 408 409
  (interactive)
  (if (not find)
      (set 'search-last-string (read-string "Search forward: ")))
  (if (search-forward search-last-string)
      (search-backward search-last-string)))

(defun edt-find-backward (&optional find)
410
  "Find first occurrence of a string in the backward direction and save it."
411 412 413 414 415 416
  (interactive)
  (if (not find)
      (set 'search-last-string (read-string "Search backward: ")))
  (search-backward search-last-string))

(defun edt-find ()
417
  "Find first occurrence of string in current direction and save it."
418 419 420 421 422 423 424 425 426 427 428 429
  (interactive)
  (set 'search-last-string (read-string "Search: "))
  (if (equal edt-direction-string edt-forward-string)
      (edt-find-forward t)
      (edt-find-backward t)))
  

;;;
;;; FNDNXT
;;;

(defun edt-find-next-forward ()
430
  "Find next occurrence of a string in forward direction."
431 432 433 434 435 436 437 438 439
  (interactive)
  (forward-char 1)
  (if (search-forward search-last-string nil t)
      (search-backward search-last-string)
      (progn
        (backward-char 1)
        (error "Search failed: \"%s\"." search-last-string))))

(defun edt-find-next-backward ()
440
  "Find next occurrence of a string in backward direction."
441 442 443 444 445 446
  (interactive)
  (if (eq (search-backward search-last-string nil t) nil)
      (progn
        (error "Search failed: \"%s\"." search-last-string))))

(defun edt-find-next ()
447
  "Find next occurrence of a string in current direction."
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
  (interactive)
  (if (equal edt-direction-string edt-forward-string)
      (edt-find-next-forward)
      (edt-find-next-backward)))
  
;;;
;;; APPEND
;;;

(defun edt-append ()
  "Append this kill region to last killed region."
  (interactive "*")
  (edt-check-selection)
  (append-next-kill)
  (kill-region (mark) (point))
  (message "Selected text APPENDED to kill ring"))

;;;
;;; DEL L
;;;

(defun edt-delete-line (num)
  "Delete from cursor up to and including the end of line mark.
Accepts a positive prefix argument for the number of lines to delete."
  (interactive "*p")
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
474 475 476
  (let ((beg (point)))
    (forward-line num)
    (if (not (eq (preceding-char) ?\n))
477
        (insert "\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
478
    (setq edt-last-deleted-lines
479
          (buffer-substring beg (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
480 481
    (delete-region beg (point))))

482 483 484 485 486 487 488 489 490
;;;
;;; DEL EOL
;;;

(defun edt-delete-to-end-of-line (num)
  "Delete from cursor up to but excluding the end of line mark.
Accepts a positive prefix argument for the number of lines to delete."
  (interactive "*p")
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
491 492 493 494
  (let ((beg (point)))
    (forward-char 1)
    (end-of-line num)
    (setq edt-last-deleted-lines
495
          (buffer-substring beg (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
496 497
    (delete-region beg (point))))

498 499 500 501 502
;;;
;;; SELECT
;;;

(defun edt-select-mode (arg)
Karl Heuer's avatar
Karl Heuer committed
503
  "Turn EDT select mode off if ARG is nil; otherwise, turn EDT select mode on.
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
In select mode, selected text is highlighted."
  (if arg
      (progn
	(make-local-variable 'edt-select-mode)
	(setq edt-select-mode 'edt-select-mode-text)
	(setq rect-start-point (window-point)))
    (progn
      (kill-local-variable 'edt-select-mode)))
  (force-mode-line-update))

(defun edt-select ()
  "Set mark at cursor and start text selection."
  (interactive)   
  (set-mark-command nil)) 

(defun edt-reset ()
  "Cancel text selection."
  (interactive)
  (deactivate-mark))

;;;
;;; CUT
;;;

(defun edt-cut ()
  "Deletes selected text but copies to kill ring."
  (interactive "*")
  (edt-check-selection)
  (kill-region (mark) (point))
  (message "Selected text CUT to kill ring"))

;;;
;;; DELETE TO BEGINNING OF LINE
;;;

(defun edt-delete-to-beginning-of-line (num)
Karl Heuer's avatar
Karl Heuer committed
540
  "Delete from cursor to beginning of line.
541 542 543
Accepts a positive prefix argument for the number of lines to delete."
  (interactive "*p")
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
544
  (let ((beg (point)))
545 546 547
    (edt-beginning-of-line num)
    (setq edt-last-deleted-lines
          (buffer-substring (point) beg))
Richard M. Stallman's avatar
Richard M. Stallman committed
548 549
    (delete-region beg (point))))

550 551 552 553 554 555 556 557 558
;;;
;;; DEL W
;;;

(defun edt-delete-word (num)
  "Delete from cursor up to but excluding first character of next word.
Accepts a positive prefix argument for the number of words to delete."
  (interactive "*p")
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
559
  (let ((beg (point)))
560 561
    (edt-word-forward num)
    (setq edt-last-deleted-words (buffer-substring beg (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
562 563
    (delete-region beg (point))))

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
;;;
;;; DELETE TO BEGINNING OF WORD
;;;

(defun edt-delete-to-beginning-of-word (num)
  "Delete from cursor to beginning of word.
Accepts a positive prefix argument for the number of words to delete."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((beg (point)))
    (edt-word-backward num)
    (setq edt-last-deleted-words (buffer-substring (point) beg))
    (delete-region beg (point))))

;;;
;;; DEL C
;;;

(defun edt-delete-character (num)
  "Delete character under cursor.
Accepts a positive prefix argument for the number of characters to delete."
  (interactive "*p")
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
587
  (setq edt-last-deleted-chars
588
        (buffer-substring (point) (min (point-max) (+ (point) num))))
Richard M. Stallman's avatar
Richard M. Stallman committed
589 590
  (delete-region (point) (min (point-max) (+ (point) num))))

591 592 593 594 595 596 597 598 599
;;;
;;; DELETE CHAR
;;;

(defun edt-delete-previous-character (num)
  "Delete character in front of cursor.
Accepts a positive prefix argument for the number of characters to delete."
  (interactive "*p")
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
600
  (setq edt-last-deleted-chars
601
        (buffer-substring (max (point-min) (- (point) num)) (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
602 603
  (delete-region (max (point-min) (- (point) num)) (point)))

604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
;;;
;;; UND L
;;;

(defun edt-undelete-line ()
  "Undelete previous deleted line(s)."
  (interactive "*")
  (point-to-register 1)
  (insert edt-last-deleted-lines)
  (register-to-point 1))

;;;
;;; UND W
;;;

(defun edt-undelete-word ()
Karl Heuer's avatar
Karl Heuer committed
620
  "Undelete previous deleted word(s)."
621 622 623 624 625 626 627 628 629 630
  (interactive "*")
  (point-to-register 1)
  (insert edt-last-deleted-words)
  (register-to-point 1))

;;;
;;; UND C
;;;

(defun edt-undelete-character ()
Karl Heuer's avatar
Karl Heuer committed
631
  "Undelete previous deleted character(s)."
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
  (interactive "*")
  (point-to-register 1)
  (insert edt-last-deleted-chars)
  (register-to-point 1))

;;;
;;; REPLACE
;;;

(defun edt-replace ()
  "Replace marked section with last CUT (killed) text."
  (interactive "*")
  (exchange-point-and-mark)
  (let ((beg (point)))
    (exchange-point-and-mark)
    (delete-region beg (point)))
  (yank))

;;;
;;; ADVANCE
;;;

(defun edt-advance ()
Karl Heuer's avatar
Karl Heuer committed
655 656
  "Set movement direction forward.
Also, execute command specified if in Minibuffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
657
  (interactive)
658
  (setq edt-direction-string edt-forward-string)
659
  (force-mode-line-update)
660 661 662 663 664 665 666
  (if (string-equal " *Minibuf" 
                    (substring (buffer-name) 0 (min (length (buffer-name)) 9)))
      (exit-minibuffer)))
  
;;;
;;; BACKUP
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
667

668
(defun edt-backup ()
Karl Heuer's avatar
Karl Heuer committed
669 670
  "Set movement direction backward.
Also, execute command specified if in Minibuffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
671
  (interactive)
672
  (setq edt-direction-string edt-backward-string)
673
  (force-mode-line-update)
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
  (if (string-equal " *Minibuf" 
                    (substring (buffer-name) 0 (min (length (buffer-name)) 9)))
      (exit-minibuffer)))

;;;
;;; CHNGCASE
;;;
;; This function is based upon Jeff Kowalski's case-flip function in his 
;; tpu.el.

(defun edt-change-case (num)
  "Change the case of specified characters.
If text selection IS active, then characters between the cursor and mark are
changed.  If text selection is NOT active, there are two cases.  First, if the
current direction is ADVANCE, then the prefix number of character(s) under and
following cursor are changed.  Second, if the current direction is BACKUP, then
the prefix number of character(s) before the cursor are changed.  Accepts a
positive prefix for the number of characters to change, but the prefix is
ignored if text selection is active."
  (interactive "*p")
  (edt-check-prefix num)
  (if edt-select-mode
      (let ((end (max (mark) (point)))
            (point-save (point)))
        (goto-char (min (point) (mark)))
        (while (not (eq (point) end))
          (funcall (if (<= ?a (following-char))
                       'upcase-region 'downcase-region)
                   (point) (1+ (point)))
          (forward-char 1))
        (goto-char point-save))
      (progn
        (if (string= edt-direction-string edt-backward-string)
            (backward-char num))
        (while (> num 0)
          (funcall (if (<= ?a (following-char))
                       'upcase-region 'downcase-region)
                   (point) (1+ (point)))
          (forward-char 1)
          (setq num (1- num))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
714

715 716 717 718 719 720 721 722
;;;
;;; DEFINE KEY
;;;

(defun edt-define-key ()
  "Assign an interactively-callable function to a specified key sequence.
The current key definition is saved in edt-last-replaced-key-definition.
Use edt-restore-key to restore last replaced key definition."
Richard M. Stallman's avatar
Richard M. Stallman committed
723
  (interactive)
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
  (let (edt-function
    edt-key-definition-string)
    (setq edt-key-definition-string
         (read-key-sequence "Press the key to be defined: "))
    (if (string-equal "\C-m" edt-key-definition-string) 
        (message "Key not defined") 
        (progn
          (setq edt-function (read-command "Enter command name: "))
          (if (string-equal "" edt-function)
              (message "Key not defined") 
              (progn
                (setq edt-last-replaced-key-definition
                   (lookup-key (current-global-map) edt-key-definition-string))
                (define-key (current-global-map) 
                    edt-key-definition-string edt-function)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
739

740 741 742
;;;
;;; FORM FEED INSERT
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
743

744 745 746 747 748 749 750 751
(defun edt-form-feed-insert (num)
  "Insert form feed character at cursor position.
Accepts a positive prefix argument for the number of form feeds to insert."
  (interactive "*p")
  (edt-check-prefix num)
  (while (> num 0)
    (insert ?\f)
    (setq num (1- num))))
Richard M. Stallman's avatar
Richard M. Stallman committed
752

753 754 755
;;;
;;; TAB INSERT
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
756

757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
(defun edt-tab-insert (num)
  "Insert tab character at cursor position.
Accepts a positive prefix argument for the number of tabs to insert."
  (interactive "*p")
  (edt-check-prefix num)
  (while (> num 0)
    (insert ?\t)
    (setq num (1- num))))

;;;
;;; Check Prefix
;;;

(defun edt-check-prefix (num)
  "Indicate error if prefix is not positive."
  (if (<= num 0)
      (error "Prefix must be positive")))
      
;;;
;;; Check Selection
;;;

(defun edt-check-selection ()
  "Indicate error if EDT selection is not active."
  (if (not edt-select-mode)
      (error "Selection NOT active")))

;;;;
;;;; ENHANCEMENTS AND ADDITIONS FOR EDT KEYPAD MODE
;;;;

;;; 
;;; Several enhancements and additions to EDT keypad mode commands are
;;; provided here.  Some of these have been motivated by similar
;;; TPU/EVE and EVE-Plus commands.  Others are new.

;;;
;;; CHANGE DIRECTION
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
796

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
(defun edt-change-direction ()
  "Toggle movement direction."
  (interactive)
  (if (equal edt-direction-string edt-forward-string) 
      (edt-backup)
      (edt-advance)))

;;;
;;; TOGGLE SELECT
;;;

(defun edt-toggle-select ()
  "Toggle to start (or cancel) text selection."
  (interactive)
  (if edt-select-mode
      (edt-reset)
    (edt-select)))

;;;
;;; SENTENCE
;;;

(defun edt-sentence-forward (num)
  "Move forward to start of next sentence.
Accepts a positive prefix argument for the number of sentences to move."
Richard M. Stallman's avatar
Richard M. Stallman committed
822
  (interactive "p")
823 824 825 826 827 828 829
  (edt-check-prefix num)
  (if (eobp)
      (progn
        (error "End of buffer"))
      (progn
        (forward-sentence num)
        (edt-one-word-forward))))
Richard M. Stallman's avatar
Richard M. Stallman committed
830

831 832 833
(defun edt-sentence-backward (num)
  "Move backward to next sentence beginning.
Accepts a positive prefix argument for the number of sentences to move."
Richard M. Stallman's avatar
Richard M. Stallman committed
834
  (interactive "p")
835 836 837 838 839
  (edt-check-prefix num)
  (if (eobp)
      (progn
        (error "End of buffer"))
      (backward-sentence num)))
Richard M. Stallman's avatar
Richard M. Stallman committed
840

841 842 843
(defun edt-sentence (num)
  "Move in current direction to next sentence.
Accepts a positive prefix argument for the number of sentences to move."
Richard M. Stallman's avatar
Richard M. Stallman committed
844
  (interactive "p")
845 846 847 848 849 850 851
  (if (equal edt-direction-string edt-forward-string)
      (edt-sentence-forward num)
      (edt-sentence-backward num)))

;;;
;;; PARAGRAPH
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
852

853 854 855
(defun edt-paragraph-forward (num)
  "Move forward to beginning of paragraph.
Accepts a positive prefix argument for the number of paragraphs to move."
Richard M. Stallman's avatar
Richard M. Stallman committed
856
  (interactive "p")
857
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
858 859 860 861
  (while (> num 0)
    (next-line 1)
    (forward-paragraph)
    (previous-line 1)
862 863
    (if (eolp)
        (next-line 1))
Richard M. Stallman's avatar
Richard M. Stallman committed
864 865
    (setq num (1- num))))

866 867 868
(defun edt-paragraph-backward (num)
  "Move backward to beginning of paragraph.
Accepts a positive prefix argument for the number of paragraphs to move."
Richard M. Stallman's avatar
Richard M. Stallman committed
869
  (interactive "p")
870
  (edt-check-prefix num)
Richard M. Stallman's avatar
Richard M. Stallman committed
871 872 873 874 875 876
  (while (> num 0)
    (backward-paragraph)
    (previous-line 1)
    (if (eolp) (next-line 1))
    (setq num (1- num))))

877 878 879 880 881 882 883 884 885 886 887 888 889
(defun edt-paragraph (num)
  "Move in current direction to next paragraph.
Accepts a positive prefix argument for the number of paragraph to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-paragraph-forward num)
      (edt-paragraph-backward num)))

;;;
;;; RESTORE KEY
;;;

(defun edt-restore-key ()
Karl Heuer's avatar
Karl Heuer committed
890 891
  "Restore last replaced key definition.
Definition is stored in edt-last-replaced-key-definition."
Richard M. Stallman's avatar
Richard M. Stallman committed
892
  (interactive)
893 894 895 896 897 898 899 900 901 902 903 904 905 906
  (if edt-last-replaced-key-definition
      (progn
        (let (edt-key-definition-string)
          (set 'edt-key-definition-string
               (read-key-sequence "Press the key to be restored: "))
          (if (string-equal "\C-m" edt-key-definition-string) 
              (message "Key not restored") 
              (define-key (current-global-map) 
                 edt-key-definition-string edt-last-replaced-key-definition))))
      (error "No replaced key definition to restore!")))

;;;
;;; WINDOW TOP
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
907

908 909
(defun edt-window-top ()
  "Move the cursor to the top of the window."
Richard M. Stallman's avatar
Richard M. Stallman committed
910
  (interactive)
911 912 913
  (let ((start-column (current-column)))
    (move-to-window-line 0)
    (move-to-column start-column)))
Richard M. Stallman's avatar
Richard M. Stallman committed
914

915 916 917
;;;
;;; WINDOW BOTTOM
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
918

919 920 921 922 923 924 925 926 927 928 929 930
(defun edt-window-bottom ()
  "Move the cursor to the bottom of the window."
  (interactive)
  (let ((start-column (current-column)))
    (move-to-window-line (- (window-height) 2))
    (move-to-column start-column)))

;;;
;;; SCROLL WINDOW LINE
;;;

(defun edt-scroll-window-forward-line ()
Karl Heuer's avatar
Karl Heuer committed
931
  "Move window forward one line leaving cursor at position in window."
932 933 934 935
  (interactive)
  (scroll-up 1))

(defun edt-scroll-window-backward-line ()
Karl Heuer's avatar
Karl Heuer committed
936
  "Move window backward one line leaving cursor at position in window."
937 938 939 940 941 942 943 944 945
  (interactive)
  (scroll-down 1))

(defun edt-scroll-line ()
  "Move window one line in current direction."
  (interactive)
  (if (equal edt-direction-string edt-forward-string)
      (edt-scroll-window-forward-line)
      (edt-scroll-window-backward-line)))
Richard M. Stallman's avatar
Richard M. Stallman committed
946

947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
;;;
;;; SCROLL WINDOW
;;;
;;; Scroll a window (less one line) at a time.  Leave cursor in center of 
;;; window. 

(defun edt-scroll-window-forward (num)
  "Scroll forward one window in buffer, less one line.
Accepts a positive prefix argument for the number of windows to move."
  (interactive "p")
  (edt-check-prefix num)
  (scroll-up (- (* (window-height) num) 2))
  (edt-line-forward (/ (- (window-height) 1) 2)))

(defun edt-scroll-window-backward (num)
  "Scroll backward one window in buffer, less one line.
Accepts a positive prefix argument for the number of windows to move."
  (interactive "p")
  (edt-check-prefix num)
  (scroll-down (- (* (window-height) num) 2))
  (edt-line-backward (/ (- (window-height) 1) 2)))

(defun edt-scroll-window (num)
  "Scroll one window in buffer, less one line, in current direction.
Accepts a positive prefix argument for the number windows to move."
  (interactive "p")
  (if (equal edt-direction-string edt-forward-string)
      (edt-scroll-window-forward num)
      (edt-scroll-window-backward num)))

;;;
;;; LINE TO BOTTOM OF WINDOW
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
980 981

(defun edt-line-to-bottom-of-window ()
982
  "Move the current line to the bottom of the window."
Richard M. Stallman's avatar
Richard M. Stallman committed
983 984 985
  (interactive)
  (recenter -1))

986 987 988 989
;;;
;;; LINE TO TOP OF WINDOW
;;;

Richard M. Stallman's avatar
Richard M. Stallman committed
990 991 992 993 994
(defun edt-line-to-top-of-window ()
  "Move the current line to the top of the window."
  (interactive)
  (recenter 0))

995 996 997
;;;
;;; LINE TO MIDDLE OF WINDOW
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
998

999 1000
(defun edt-line-to-middle-of-window ()
  "Move window so line with cursor is in the middle of the window."
Richard M. Stallman's avatar
Richard M. Stallman committed
1001
  (interactive)
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
  (recenter '(4)))

;;;
;;; GOTO PERCENTAGE
;;;

(defun edt-goto-percentage (num)
  "Move to specified percentage in buffer from top of buffer."
  (interactive "NGoto-percentage: ")
  (if (or (> num 100) (< num 0))
      (error "Percentage %d out of range 0 < percent < 100" num)
      (goto-char (/ (* (point-max) num) 100))))

;;;
;;; FILL REGION
;;;

(defun edt-fill-region ()
  "Fill selected text."
  (interactive "*")         
  (edt-check-selection)
  (fill-region (point) (mark)))

;;;
;;; INDENT OR FILL REGION
;;;

(defun edt-indent-or-fill-region ()
  "Fill region in text modes, indent region in programming language modes."
  (interactive "*")         
1032
  (if (string= paragraph-start "$\\|\f")
Richard M. Stallman's avatar
Richard M. Stallman committed
1033
      (indent-region (point) (mark) nil)
1034
      (fill-region (point) (mark))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1035

1036 1037 1038 1039 1040
;;;
;;; MARK SECTION WISELY
;;;

(defun edt-mark-section-wisely ()
Richard M. Stallman's avatar
Richard M. Stallman committed
1041
  "Mark the section in a manner consistent with the major-mode.
1042
Uses mark-defun for emacs-lisp and lisp,
Richard M. Stallman's avatar
Richard M. Stallman committed
1043
mark-c-function for C,
1044
mark-fortran-subsystem for fortran,
Richard M. Stallman's avatar
Richard M. Stallman committed
1045 1046
and mark-paragraph for other modes."
  (interactive)
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  (if edt-select-mode
      (progn
        (edt-reset))
      (progn
	(cond  ((or (eq major-mode 'emacs-lisp-mode)
		    (eq major-mode 'lisp-mode))
		(mark-defun)
		(message "Lisp defun selected"))
	       ((eq major-mode 'c-mode)
		(mark-c-function)
		(message "C function selected"))
	       ((eq major-mode 'fortran-mode)
		(mark-fortran-subprogram)
		(message "Fortran subprogram selected"))
	       (t (mark-paragraph)
		  (message "Paragraph selected"))))))

;;;
;;; COPY
;;;

(defun edt-copy ()
  "Copy selected region to kill ring, but don't delete it!"
  (interactive)
  (edt-check-selection)
  (copy-region-as-kill (mark) (point))
  (edt-reset)
  (message "Selected text COPIED to kill ring"))

;;;
;;; CUT or COPY
;;;

(defun edt-cut-or-copy ()
  "Cuts (or copies) selected text to kill ring.
Cuts selected text if buffer-read-only is nil.
Copies selected text if buffer-read-only is t."
  (interactive)
  (if buffer-read-only
      (edt-copy)
      (edt-cut)))

;;;
;;; DELETE ENTIRE LINE
;;;

(defun edt-delete-entire-line ()
  "Delete entire line regardless of cursor position in the line."
  (interactive "*")
  (beginning-of-line)
  (edt-delete-line 1))

;;;
;;; DUPLICATE LINE
;;;

(defun edt-duplicate-line (num)
  "Duplicate a line of text.
Accepts a positive prefix argument for the number times to duplicate the line."
  (interactive "*p")
  (edt-check-prefix num)
  (let ((old-column (current-column))
        (count num))
    (edt-delete-entire-line)
    (edt-undelete-line)
    (while (> count 0)
      (edt-undelete-line)
      (setq count (1- count)))
    (edt-line-forward num)
    (move-to-column old-column)))

;;;
;;; DUPLICATE WORD
;;;

(defun edt-duplicate-word()
  "Duplicate word (or rest of word) found directly above cursor, if any."
  (interactive "*")
  (let ((start (point))
        (start-column (current-column)))
    (forward-line -1)
    (move-to-column start-column)
    (if (and (not (equal start (point)))
             (not (eolp)))
        (progn
          (if (and (equal ?\t (preceding-char))
                   (< start-column (current-column)))
              (backward-char))
          (let ((beg (point)))
            (edt-one-word-forward)
            (setq edt-last-copied-word (buffer-substring beg (point))))
          (forward-line)
          (move-to-column start-column)
          (insert edt-last-copied-word))
        (progn
	  (if (not (equal start (point)))
	      (forward-line))
          (move-to-column start-column)
          (error "Nothing to duplicate!")))))

;;;
;;; KEY NOT ASSIGNED
;;;

(defun edt-key-not-assigned ()
  "Displays message that key has not been assigned to a function."
  (interactive)
  (error "Key not assigned"))

;;;
;;; TOGGLE CAPITALIZATION OF WORD
;;;

(defun edt-toggle-capitalization-of-word ()
  "Toggle the capitalization of the current word and move forward to next."
  (interactive "*")
  (edt-one-word-forward)
  (edt-one-word-backward)
  (edt-change-case 1)
  (edt-one-word-backward)
  (edt-one-word-forward))

;;;
;;; ELIMINATE ALL TABS
;;;

(defun edt-eliminate-all-tabs ()
  "Convert all tabs to spaces in the entire buffer."
  (interactive "*")
  (untabify (point-min) (point-max))
  (message "TABS converted to SPACES"))

;;;
;;; DISPLAY THE TIME
;;;

(defun edt-display-the-time ()
  "Display the current time."
  (interactive)
  (set 'time-string (current-time-string))
  (message time-string))

;;;
;;; LEARN
;;;

(defun edt-learn ()
  "Learn a sequence of key strokes to bind to a key."
  (interactive)
  (if (eq defining-kbd-macro t)
      (edt-remember)
      (start-kbd-macro nil)))

;;;
;;; REMEMBER
;;;

(defun edt-remember ()
  "Store the sequence of key strokes started by edt-learn to a key."
  (interactive)
  (if (eq defining-kbd-macro nil)
      (error "Nothing to remember!")
      (progn
        (end-kbd-macro nil)
        (let (edt-key-definition-string)
          (set 'edt-key-definition-string
               (read-key-sequence "Enter key for binding: "))
          (if (string-equal "\C-m" edt-key-definition-string) 
              (message "Key sequence not remembered") 
              (progn
                (set 'edt-learn-macro-count (+ edt-learn-macro-count 1))
                (setq edt-last-replaced-key-definition
                      (lookup-key (current-global-map)
                                  edt-key-definition-string))
                (define-key (current-global-map) edt-key-definition-string
                  (name-last-kbd-macro
                    (intern (concat "last-learned-sequence-"
                                  (int-to-string edt-learn-macro-count)))))))))))

;;;
;;; EXIT
;;;

(defun edt-exit ()
  "Save current buffer, ask to save other buffers, and then exit Emacs."
  (interactive)
  (save-buffer)
  (save-buffers-kill-emacs))

;;; 
;;; QUIT
;;;

(defun edt-quit ()
  "Quit Emacs without saving changes."
  (interactive)
  (kill-emacs))

;;; 
;;; SPLIT WINDOW
;;;

(defun edt-split-window ()
  "Split current window and place cursor in the new window."
  (interactive)
  (split-window)
  (other-window 1))

;;;
;;; COPY RECTANGLE
;;;

(defun edt-copy-rectangle ()
  "Copy a rectangle of text between mark and cursor to register."
  (interactive)
  (edt-check-selection)
  (copy-rectangle-to-register 3 (region-beginning) (region-end) nil)
  (edt-reset)
  (message "Selected rectangle COPIED to register"))

;;;
;;; CUT RECTANGLE
;;;

(defun edt-cut-rectangle-overstrike-mode ()
Karl Heuer's avatar
Karl Heuer committed
1272 1273 1274
  "Cut a rectangle of text between mark and cursor to register.
Replace cut characters with spaces and moving cursor back to
upper left corner."
1275 1276 1277 1278 1279 1280 1281 1282
  (interactive "*")
  (edt-check-selection)
  (setq edt-rect-start-point (region-beginning))
  (picture-clear-rectangle-to-register (region-beginning) (region-end) 3)
  (set-window-point (get-buffer-window (window-buffer)) edt-rect-start-point)
  (message "Selected rectangle CUT to register"))

(defun edt-cut-rectangle-insert-mode ()
Karl Heuer's avatar
Karl Heuer committed
1283 1284
  "Cut a rectangle of text between mark and cursor to register.
Move cursor back to upper left corner."
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
  (interactive "*")         
  (edt-check-selection)
  (setq edt-rect-start-point (region-beginning))
  (picture-clear-rectangle-to-register (region-beginning) (region-end) 3 t)
  (fixup-whitespace)
  (set-window-point (get-buffer-window (window-buffer)) edt-rect-start-point)
  (message "Selected rectangle CUT to register"))

(defun edt-cut-rectangle ()
  "Cut a rectangular region of text to register.
Karl Heuer's avatar
Karl Heuer committed
1295
If overwrite mode is active, cut text is replaced with whitespace."
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
  (interactive "*")
  (if overwrite-mode
      (edt-cut-rectangle-overstrike-mode)
      (edt-cut-rectangle-insert-mode)))

;;;
;;; PASTE RECTANGLE
;;;

(defun edt-paste-rectangle-overstrike-mode ()
  "Paste a rectangular region of text from register, replacing text at cursor."
  (interactive "*")    
  (picture-yank-rectangle-from-register 3))

(defun edt-paste-rectangle-insert-mode ()
  "Paste previously deleted rectangular region, inserting text at cursor."
  (interactive "*")
  (picture-yank-rectangle-from-register 3 t))

(defun edt-paste-rectangle ()
  "Paste a rectangular region of text.
If overwrite mode is active, existing text is replace with text from register."
  (interactive)
  (if overwrite-mode
      (edt-paste-rectangle-overstrike-mode)
      (edt-paste-rectangle-insert-mode)))

;;;
;;; DOWNCASE REGION
;;;

(defun edt-lowercase ()
  "Change specified characters to lower case.
If text selection IS active, then characters between the cursor and
mark are changed.  If text selection is NOT active, there are two
situations.  If the current direction is ADVANCE, then the word under
the cursor is changed to lower case and the cursor is moved to rest at
the beginning of the next word.  If the current direction is BACKUP,
the word prior to the word under the cursor is changed to lower case
and the cursor is left to rest at the beginning of that word."
  (interactive "*")
  (if edt-select-mode
      (progn
	(downcase-region (mark) (point)))
      (progn
	;; Move to beginning of current word.
	(if (and
	      (not (bobp))
	      (not (eobp))
	      (not (bolp))
	      (not (eolp))
	      (not (eq ?\  (char-syntax (preceding-char))))
	      (not (memq (preceding-char) edt-word-entities))
	      (not (memq (following-char) edt-word-entities)))
	    (edt-one-word-backward))
	(if (equal edt-direction-string edt-backward-string)
	    (edt-one-word-backward))
	(let ((beg (point)))
	  (edt-one-word-forward)
	  (downcase-region beg (point)))
	(if (equal edt-direction-string edt-backward-string)
	    (edt-one-word-backward)))))

;;;
;;; UPCASE REGION
;;;

(defun edt-uppercase ()
  "Change specified characters to upper case.
If text selection IS active, then characters between the cursor and
mark are changed.  If text selection is NOT active, there are two
situations.  If the current direction is ADVANCE, then the word under
the cursor is changed to upper case and the cursor is moved to rest at
the beginning of the next word.  If the current direction is BACKUP,
the word prior to the word under the cursor is changed to upper case
and the cursor is left to rest at the beginning of that word."
  (interactive "*")
  (if edt-select-mode
      (progn
	(upcase-region (mark) (point)))
      (progn
	;; Move to beginning of current word.
	(if (and
	      (not (bobp))
	      (not (eobp))
	      (not (bolp))
	      (not (eolp))
	      (not (eq ?\  (char-syntax (preceding-char))))
	      (not (memq (preceding-char) edt-word-entities))
	      (not (memq (following-char) edt-word-entities)))
	    (edt-one-word-backward))
	(if (equal edt-direction-string edt-backward-string)
	    (edt-one-word-backward))
	(let ((beg (point)))
	  (edt-one-word-forward)
	  (upcase-region beg (point)))
	(if (equal edt-direction-string edt-backward-string)
	    (edt-one-word-backward)))))


;;;
;;; INITIALIZATION COMMANDS.
;;;

;;;
;;;  Emacs version 19 X-windows key definition support
;;;
Karl Heuer's avatar
Karl Heuer committed
1403 1404
(defvar edt-last-answer nil 
  "Most recent response to edt-y-or-n-p.")
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478

(defun edt-y-or-n-p (prompt &optional not-yes)
  "Prompt for a y or n answer with positive default.
Optional second argument NOT-YES changes default to negative.
Like emacs y-or-n-p, also accepts space as y and DEL as n."
  (message (format "%s[%s]" prompt (if not-yes "n" "y")))
  (let ((doit t))
    (while doit
      (setq doit nil)
      (let ((ans (read-char)))
	(cond ((or (= ans ?y) (= ans ?Y) (= ans ?\ ))
	       (setq edt-last-answer t))
	      ((or (= ans ?n) (= ans ?N) (= ans ?\C-?))
	       (setq edt-last-answer nil))
	      ((= ans ?\r) (setq edt-last-answer (not not-yes)))
	      (t
	       (setq doit t) (beep)
	       (message (format "Please answer y or n.  %s[%s]"
				prompt (if not-yes "n" "y"))))))))
  edt-last-answer)

(defun edt-load-xkeys (file)
  "Load the EDT X-windows key definitions FILE.
If FILE is nil, try to load a default file.  The default file names are
~/.edt-lucid-keys for Lucid emacs, and ~/.edt-gnu-keys for GNU emacs."
  (interactive "fX key definition file: ")
  (cond (file
	 (setq file (expand-file-name file)))
	(edt-xkeys-file
	 (setq file (expand-file-name edt-xkeys-file)))
	(edt-gnu-emacs19-p
	 (setq file (expand-file-name "~/.edt-gnu-keys")))
	(edt-lucid-emacs19-p
	 (setq file (expand-file-name "~/.edt-lucid-keys"))))
  (cond ((file-readable-p file)
	 (load-file file))
	(t
	 (switch-to-buffer "*scratch*")
	 (erase-buffer)
	 (insert "

     Ack!!  You're running the Enhanced EDT Emulation under X-windows
     without loading an EDT X key definition file.  To create an EDT X
     key definition file, run the edt-mapper.el program.  But ONLY run
     it from an Emacs loaded without any of your own customizations
     found in your .emacs file, etc.  Some user customization confuse
     the edt-mapper function.  To do this, you need to invoke Emacs
     as follows:

          emacs -q -l edt-mapper.el
     
     The file edt-mapper.el includes these same directions on how to
     use it!  Perhaps it's laying around here someplace. \n     ")
	 (let ((file "edt-mapper.el")
	       (found nil)
	       (path nil)
	       (search-list (append (list (expand-file-name ".")) load-path)))
	   (while (and (not found) search-list)
	     (setq path (concat (car search-list)
				(if (string-match "/$" (car search-list)) "" "/")
				file))
	     (if (and (file-exists-p path) (not (file-directory-p path)))
		 (setq found t))
	     (setq search-list (cdr search-list)))
	   (cond (found
		  (insert (format
			   "Ah yes, there it is, in \n\n       %s \n\n" path))
		  (if (edt-y-or-n-p "Do you want to run it now? ")
		      (load-file path)
		    (error "EDT Emulation not configured.")))
		 (t
		  (insert "Nope, I can't seem to find it.  :-(\n\n")
		  (sit-for 20)
		  (error "EDT Emulation not configured.")))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1479

Jim Blandy's avatar
Jim Blandy committed
1480
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
1481
(defun edt-emulation-on ()
1482 1483 1484
  "Turn on EDT Emulation."
  (interactive)
  ;; If using MS-DOS, need to load edt-pc.el
1485
  (if (eq system-type 'ms-dos)
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
      (setq edt-term "pc")
    (setq edt-term (getenv "TERM")))
  ;; All DEC VT series terminals are supported by loading edt-vt100.el
  (if (string-equal "vt" (substring edt-term 0 (min (length edt-term) 2)))
      (setq edt-term "vt100"))
  ;; Load EDT terminal specific configuration file.
  (let ((term edt-term)
        hyphend)
    (while (and term
                (not (load (concat "edt-" term) t t)))
      ;; Strip off last hyphen and what follows, then try again
      (if (setq hyphend (string-match "[-_][^-_]+$" term))
          (setq term (substring term 0 hyphend))
          (setq term nil)))
    ;; Override terminal-specific file if running X Windows.  X Windows support
    ;; is handled differently in edt-load-xkeys
1502
    (if (eq window-system 'x)
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617