ws-mode.el 20.1 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;;; ws-mode.el --- WordStar emulation mode for GNU Emacs

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1991, 2001-2015 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5 6
;; Author: Juergen Nickelsen <nickel@cs.tu-berlin.de>
;; Version: 0.7
Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Keywords: emulations
8
;; Obsolete-since: 24.5
Jim Blandy's avatar
Jim Blandy committed
9

10 11
;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
16 17 18 19 20 21 22

;; 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
23
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
Jim Blandy's avatar
Jim Blandy committed
24

Eric S. Raymond's avatar
Eric S. Raymond committed
25
;;; Commentary:
Jim Blandy's avatar
Jim Blandy committed
26

27
;; This emulates WordStar, with a major mode.
Jim Blandy's avatar
Jim Blandy committed
28

Eric S. Raymond's avatar
Eric S. Raymond committed
29
;;; Code:
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
(defvar wordstar-C-k-map
  (let ((map (make-keymap)))
    (define-key map " " ())
    (define-key map "0" 'ws-set-marker-0)
    (define-key map "1" 'ws-set-marker-1)
    (define-key map "2" 'ws-set-marker-2)
    (define-key map "3" 'ws-set-marker-3)
    (define-key map "4" 'ws-set-marker-4)
    (define-key map "5" 'ws-set-marker-5)
    (define-key map "6" 'ws-set-marker-6)
    (define-key map "7" 'ws-set-marker-7)
    (define-key map "8" 'ws-set-marker-8)
    (define-key map "9" 'ws-set-marker-9)
    (define-key map "b" 'ws-begin-block)
    (define-key map "\C-b" 'ws-begin-block)
    (define-key map "c" 'ws-copy-block)
    (define-key map "\C-c" 'ws-copy-block)
    (define-key map "d" 'save-buffers-kill-emacs)
    (define-key map "\C-d" 'save-buffers-kill-emacs)
    (define-key map "f" 'find-file)
    (define-key map "\C-f" 'find-file)
    (define-key map "h" 'ws-show-markers)
    (define-key map "\C-h" 'ws-show-markers)
    (define-key map "i" 'ws-indent-block)
    (define-key map "\C-i" 'ws-indent-block)
    (define-key map "k" 'ws-end-block)
    (define-key map "\C-k" 'ws-end-block)
    (define-key map "p" 'ws-print-block)
    (define-key map "\C-p" 'ws-print-block)
    (define-key map "q" 'kill-emacs)
    (define-key map "\C-q" 'kill-emacs)
    (define-key map "r" 'insert-file)
    (define-key map "\C-r" 'insert-file)
    (define-key map "s" 'save-some-buffers)
    (define-key map "\C-s" 'save-some-buffers)
    (define-key map "t" 'ws-mark-word)
    (define-key map "\C-t" 'ws-mark-word)
    (define-key map "u" 'ws-exdent-block)
    (define-key map "\C-u" 'keyboard-quit)
    (define-key map "v" 'ws-move-block)
    (define-key map "\C-v" 'ws-move-block)
    (define-key map "w" 'ws-write-block)
    (define-key map "\C-w" 'ws-write-block)
    (define-key map "x" 'save-buffers-kill-emacs)
    (define-key map "\C-x" 'save-buffers-kill-emacs)
    (define-key map "y" 'ws-delete-block)
    (define-key map "\C-y" 'ws-delete-block)
77
    map))
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

(defvar wordstar-C-o-map
  (let ((map (make-keymap)))
    (define-key map " " ())
    (define-key map "c" 'wordstar-center-line)
    (define-key map "\C-c" 'wordstar-center-line)
    (define-key map "b" 'switch-to-buffer)
    (define-key map "\C-b" 'switch-to-buffer)
    (define-key map "j" 'justify-current-line)
    (define-key map "\C-j" 'justify-current-line)
    (define-key map "k" 'kill-buffer)
    (define-key map "\C-k" 'kill-buffer)
    (define-key map "l" 'list-buffers)
    (define-key map "\C-l" 'list-buffers)
    (define-key map "m" 'auto-fill-mode)
    (define-key map "\C-m" 'auto-fill-mode)
    (define-key map "r" 'set-fill-column)
    (define-key map "\C-r" 'set-fill-column)
    (define-key map "\C-u" 'keyboard-quit)
    (define-key map "wd" 'delete-other-windows)
98
    (define-key map "wh" 'split-window-right)
99
    (define-key map "wo" 'other-window)
100
    (define-key map "wv" 'split-window-below)
101 102 103 104 105 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
    map)
  "")

(defvar wordstar-C-q-map
  (let ((map (make-keymap)))
    (define-key map " " ())
    (define-key map "0" 'ws-find-marker-0)
    (define-key map "1" 'ws-find-marker-1)
    (define-key map "2" 'ws-find-marker-2)
    (define-key map "3" 'ws-find-marker-3)
    (define-key map "4" 'ws-find-marker-4)
    (define-key map "5" 'ws-find-marker-5)
    (define-key map "6" 'ws-find-marker-6)
    (define-key map "7" 'ws-find-marker-7)
    (define-key map "8" 'ws-find-marker-8)
    (define-key map "9" 'ws-find-marker-9)
    (define-key map "a" 'ws-query-replace)
    (define-key map "\C-a" 'ws-query-replace)
    (define-key map "b" 'ws-goto-block-begin)
    (define-key map "\C-b" 'ws-goto-block-begin)
    (define-key map "c" 'end-of-buffer)
    (define-key map "\C-c" 'end-of-buffer)
    (define-key map "d" 'end-of-line)
    (define-key map "\C-d" 'end-of-line)
    (define-key map "f" 'ws-search)
    (define-key map "\C-f" 'ws-search)
    (define-key map "k" 'ws-goto-block-end)
    (define-key map "\C-k" 'ws-goto-block-end)
    (define-key map "l" 'ws-undo)
    (define-key map "\C-l" 'ws-undo)
    (define-key map "p" 'ws-last-cursorp)
    (define-key map "\C-p" 'ws-last-cursorp)
    (define-key map "r" 'beginning-of-buffer)
    (define-key map "\C-r" 'beginning-of-buffer)
    (define-key map "s" 'beginning-of-line)
    (define-key map "\C-s" 'beginning-of-line)
    (define-key map "\C-u" 'keyboard-quit)
    (define-key map "w" 'ws-last-error)
    (define-key map "\C-w" 'ws-last-error)
    (define-key map "y" 'ws-kill-eol)
    (define-key map "\C-y" 'ws-kill-eol)
    (define-key map "\177" 'ws-kill-bol)
143
    map))
144

145
(defvar wordstar-mode-map
146 147 148
  (let ((map (make-keymap)))
    (define-key map "\C-a" 'backward-word)
    (define-key map "\C-b" 'fill-paragraph)
149
    (define-key map "\C-c" 'scroll-up-command)
150 151 152 153 154 155 156 157 158 159 160 161 162
    (define-key map "\C-d" 'forward-char)
    (define-key map "\C-e" 'previous-line)
    (define-key map "\C-f" 'forward-word)
    (define-key map "\C-g" 'delete-char)
    (define-key map "\C-h" 'backward-char)
    (define-key map "\C-i" 'indent-for-tab-command)
    (define-key map "\C-j" 'help-for-help)
    (define-key map "\C-k" wordstar-C-k-map)
    (define-key map "\C-l" 'ws-repeat-search)
    (define-key map "\C-n" 'open-line)
    (define-key map "\C-o" wordstar-C-o-map)
    (define-key map "\C-p" 'quoted-insert)
    (define-key map "\C-q" wordstar-C-q-map)
163
    (define-key map "\C-r" 'scroll-down-command)
164 165 166 167 168 169 170 171
    (define-key map "\C-s" 'backward-char)
    (define-key map "\C-t" 'kill-word)
    (define-key map "\C-u" 'keyboard-quit)
    (define-key map "\C-v" 'overwrite-mode)
    (define-key map "\C-w" 'scroll-down-line)
    (define-key map "\C-x" 'next-line)
    (define-key map "\C-y" 'kill-complete-line)
    (define-key map "\C-z" 'scroll-up-line)
172
    map))
173 174

;; wordstar-C-j-map not yet implemented
175
(defvar wordstar-C-j-map nil)
176

Jim Blandy's avatar
Jim Blandy committed
177

178 179
(put 'wordstar-mode 'mode-class 'special)

180
;;;###autoload
181
(define-derived-mode wordstar-mode fundamental-mode "WordStar"
182 183 184 185 186 187 188 189 190 191
  "Major mode with WordStar-like key bindings.

BUGS:
 - Help menus with WordStar commands (C-j just calls help-for-help)
   are not implemented
 - Options for search and replace
 - Show markers (C-k h) is somewhat strange
 - Search and replace (C-q a) is only available in forward direction

No key bindings beginning with ESC are installed, they will work
192
Emacs-like.")
193

Jim Blandy's avatar
Jim Blandy committed
194

195
(defun wordstar-center-paragraph ()
Jim Blandy's avatar
Jim Blandy committed
196
  "Center each line in the paragraph at or after point.
197
See `wordstar-center-line' for more info."
Jim Blandy's avatar
Jim Blandy committed
198 199 200 201 202 203
  (interactive)
  (save-excursion
    (forward-paragraph)
    (or (bolp) (newline 1))
    (let ((end (point)))
      (backward-paragraph)
204
      (wordstar-center-region (point) end))))
Jim Blandy's avatar
Jim Blandy committed
205

206
(defun wordstar-center-region (from to)
Jim Blandy's avatar
Jim Blandy committed
207
  "Center each line starting in the region.
208
See `wordstar-center-line' for more info."
Jim Blandy's avatar
Jim Blandy committed
209 210 211 212 213 214 215 216 217
  (interactive "r")
  (if (> from to)
      (let ((tem to))
	(setq to from from tem)))
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char from)
      (while (not (eobp))
218
	(wordstar-center-line)
Jim Blandy's avatar
Jim Blandy committed
219 220
	(forward-line 1)))))

221
(defun wordstar-center-line ()
Jim Blandy's avatar
Jim Blandy committed
222 223 224 225 226 227 228 229 230 231 232 233
  "Center the line point is on, within the width specified by `fill-column'.
This means adjusting the indentation to match
the distance between the end of the text and `fill-column'."
  (interactive)
  (save-excursion
    (let (line-length)
      (beginning-of-line)
      (delete-horizontal-space)
      (end-of-line)
      (delete-horizontal-space)
      (setq line-length (current-column))
      (beginning-of-line)
234 235
      (indent-to
       (+ left-margin
Jim Blandy's avatar
Jim Blandy committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	  (/ (- fill-column left-margin line-length) 2))))))

;;;;;;;;;;;
;; wordstar special variables:

(defvar ws-marker-0 nil "Position marker 0 in WordStar mode.")
(defvar ws-marker-1 nil "Position marker 1 in WordStar mode.")
(defvar ws-marker-2 nil "Position marker 2 in WordStar mode.")
(defvar ws-marker-3 nil "Position marker 3 in WordStar mode.")
(defvar ws-marker-4 nil "Position marker 4 in WordStar mode.")
(defvar ws-marker-5 nil "Position marker 5 in WordStar mode.")
(defvar ws-marker-6 nil "Position marker 6 in WordStar mode.")
(defvar ws-marker-7 nil "Position marker 7 in WordStar mode.")
(defvar ws-marker-8 nil "Position marker 8 in WordStar mode.")
(defvar ws-marker-9 nil "Position marker 9 in WordStar mode.")

(defvar ws-block-begin-marker nil "Beginning of \"Block\" in WordStar mode.")
(defvar ws-block-end-marker nil "End of \"Block\" in WordStar mode.")

(defvar ws-search-string nil "String of last search in WordStar mode.")
256
(defvar ws-search-direction t
Pavel Janík's avatar
Pavel Janík committed
257
  "Direction of last search in WordStar mode. t if forward, nil if backward.")
Jim Blandy's avatar
Jim Blandy committed
258

259
(defvar ws-last-cursorposition nil
Jim Blandy's avatar
Jim Blandy committed
260 261
  "Position before last search etc. in WordStar mode.")

262
(defvar ws-last-errormessage nil
Jim Blandy's avatar
Jim Blandy committed
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
  "Last error message issued by a WordStar mode function.")

;;;;;;;;;;;
;; wordstar special functions:

(defun ws-error (string)
  "Report error of a WordStar special function. Error message is saved
in ws-last-errormessage for recovery with C-q w."
  (setq ws-last-errormessage string)
  (error string))

(defun ws-set-marker-0 ()
  "In WordStar mode: Set marker 0 to current cursor position."
  (interactive)
  (setq ws-marker-0 (point-marker))
  (message "Marker 0 set"))

(defun ws-set-marker-1 ()
  "In WordStar mode: Set marker 1 to current cursor position."
  (interactive)
  (setq ws-marker-1 (point-marker))
  (message "Marker 1 set"))

(defun ws-set-marker-2 ()
  "In WordStar mode: Set marker 2 to current cursor position."
  (interactive)
  (setq ws-marker-2 (point-marker))
  (message "Marker 2 set"))

(defun ws-set-marker-3 ()
  "In WordStar mode: Set marker 3 to current cursor position."
  (interactive)
  (setq ws-marker-3 (point-marker))
  (message "Marker 3 set"))

(defun ws-set-marker-4 ()
  "In WordStar mode: Set marker 4 to current cursor position."
  (interactive)
  (setq ws-marker-4 (point-marker))
  (message "Marker 4 set"))

(defun ws-set-marker-5 ()
  "In WordStar mode: Set marker 5 to current cursor position."
  (interactive)
  (setq ws-marker-5 (point-marker))
  (message "Marker 5 set"))

(defun ws-set-marker-6 ()
  "In WordStar mode: Set marker 6 to current cursor position."
  (interactive)
  (setq ws-marker-6 (point-marker))
  (message "Marker 6 set"))

(defun ws-set-marker-7 ()
  "In WordStar mode: Set marker 7 to current cursor position."
  (interactive)
  (setq ws-marker-7 (point-marker))
  (message "Marker 7 set"))

(defun ws-set-marker-8 ()
  "In WordStar mode: Set marker 8 to current cursor position."
  (interactive)
  (setq ws-marker-8 (point-marker))
  (message "Marker 8 set"))

(defun ws-set-marker-9 ()
  "In WordStar mode: Set marker 9 to current cursor position."
  (interactive)
  (setq ws-marker-9 (point-marker))
  (message "Marker 9 set"))

(defun ws-begin-block ()
  "In WordStar mode: Set block begin marker to current cursor position."
  (interactive)
  (setq ws-block-begin-marker (point-marker))
  (message "Block begin marker set"))

(defun ws-show-markers ()
  "In WordStar mode: Show block markers."
  (interactive)
  (if (or ws-block-begin-marker ws-block-end-marker)
      (save-excursion
	(if ws-block-begin-marker
346
	    (progn
Jim Blandy's avatar
Jim Blandy committed
347 348 349 350 351 352
	      (goto-char ws-block-begin-marker)
	      (message "Block begin marker")
	      (sit-for 2))
	  (message "Block begin marker not set")
	  (sit-for 2))
	(if ws-block-end-marker
353
	    (progn
Jim Blandy's avatar
Jim Blandy committed
354 355 356 357 358 359 360 361 362
	      (goto-char ws-block-end-marker)
	      (message "Block end marker")
	      (sit-for 2))
	  (message "Block end marker not set"))
	(message ""))
    (message "Block markers not set")))


(defun ws-indent-block ()
363
  "In WordStar mode: Indent block (not yet implemented)."
Jim Blandy's avatar
Jim Blandy committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
  (interactive)
  (ws-error "Indent block not yet implemented"))

(defun ws-end-block ()
  "In WordStar mode: Set block end marker to current cursor position."
  (interactive)
  (setq ws-block-end-marker (point-marker))
  (message "Block end marker set"))

(defun ws-print-block ()
  "In WordStar mode: Print block."
  (interactive)
  (message "Don't do this. Write block to a file (C-k w) and print this file."))

(defun ws-mark-word ()
  "In WordStar mode: Mark current word as block."
  (interactive)
  (save-excursion
382
    (forward-word 1)
Jim Blandy's avatar
Jim Blandy committed
383 384
    (sit-for 1)
    (ws-end-block)
385
    (forward-word -1)
Jim Blandy's avatar
Jim Blandy committed
386 387 388 389 390 391 392 393 394 395 396 397
    (sit-for 1)
    (ws-begin-block)))

(defun ws-exdent-block ()
  "I don't know what this (C-k u) should do."
  (interactive)
  (ws-error "This won't be done -- not yet implemented."))

(defun ws-move-block ()
  "In WordStar mode: Move block to current cursor position."
  (interactive)
  (if (and ws-block-begin-marker ws-block-end-marker)
398
      (progn
399
	(kill-region ws-block-begin-marker ws-block-end-marker)
Jim Blandy's avatar
Jim Blandy committed
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	(yank)
	(save-excursion
	  (goto-char (region-beginning))
	  (setq ws-block-begin-marker (point-marker))
	  (goto-char (region-end))
	  (setq ws-block-end-marker (point-marker))))
    (ws-error (cond (ws-block-begin-marker "Block end marker not set")
		    (ws-block-end-marker "Block begin marker not set")
		    (t "Block markers not set")))))

(defun ws-write-block ()
  "In WordStar mode: Write block to file."
  (interactive)
  (if (and ws-block-begin-marker ws-block-end-marker)
      (let ((filename (read-file-name "Write block to file: ")))
	(write-region ws-block-begin-marker ws-block-end-marker filename))
    (ws-error (cond (ws-block-begin-marker "Block end marker not set")
		    (ws-block-end-marker "Block begin marker not set")
		    (t "Block markers not set")))))


(defun ws-delete-block ()
  "In WordStar mode: Delete block."
  (interactive)
  (if (and ws-block-begin-marker ws-block-end-marker)
425
      (progn
Jim Blandy's avatar
Jim Blandy committed
426 427 428 429 430 431 432 433 434 435 436
	(kill-region ws-block-begin-marker ws-block-end-marker)
	(setq ws-block-end-marker nil)
	(setq ws-block-begin-marker nil))
    (ws-error (cond (ws-block-begin-marker "Block end marker not set")
		    (ws-block-end-marker "Block begin marker not set")
		    (t "Block markers not set")))))

(defun ws-find-marker-0 ()
  "In WordStar mode: Go to marker 0."
  (interactive)
  (if ws-marker-0
437
      (progn
Jim Blandy's avatar
Jim Blandy committed
438 439 440 441 442 443 444 445
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-0))
    (ws-error "Marker 0 not set")))

(defun ws-find-marker-1 ()
  "In WordStar mode: Go to marker 1."
  (interactive)
  (if ws-marker-1
446
      (progn
Jim Blandy's avatar
Jim Blandy committed
447 448 449 450 451 452 453 454
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-1))
    (ws-error "Marker 1 not set")))

(defun ws-find-marker-2 ()
  "In WordStar mode: Go to marker 2."
  (interactive)
  (if ws-marker-2
455
      (progn
Jim Blandy's avatar
Jim Blandy committed
456 457 458 459 460 461 462 463
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-2))
    (ws-error "Marker 2 not set")))

(defun ws-find-marker-3 ()
  "In WordStar mode: Go to marker 3."
  (interactive)
  (if ws-marker-3
464
      (progn
Jim Blandy's avatar
Jim Blandy committed
465 466 467 468 469 470 471 472
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-3))
    (ws-error "Marker 3 not set")))

(defun ws-find-marker-4 ()
  "In WordStar mode: Go to marker 4."
  (interactive)
  (if ws-marker-4
473
      (progn
Jim Blandy's avatar
Jim Blandy committed
474 475 476 477 478 479 480 481
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-4))
    (ws-error "Marker 4 not set")))

(defun ws-find-marker-5 ()
  "In WordStar mode: Go to marker 5."
  (interactive)
  (if ws-marker-5
482
      (progn
Jim Blandy's avatar
Jim Blandy committed
483 484 485 486 487 488 489 490
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-5))
    (ws-error "Marker 5 not set")))

(defun ws-find-marker-6 ()
  "In WordStar mode: Go to marker 6."
  (interactive)
  (if ws-marker-6
491
      (progn
Jim Blandy's avatar
Jim Blandy committed
492 493 494 495 496 497 498 499
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-6))
    (ws-error "Marker 6 not set")))

(defun ws-find-marker-7 ()
  "In WordStar mode: Go to marker 7."
  (interactive)
  (if ws-marker-7
500
      (progn
Jim Blandy's avatar
Jim Blandy committed
501 502 503 504 505 506 507 508
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-7))
    (ws-error "Marker 7 not set")))

(defun ws-find-marker-8 ()
  "In WordStar mode: Go to marker 8."
  (interactive)
  (if ws-marker-8
509
      (progn
Jim Blandy's avatar
Jim Blandy committed
510 511 512 513 514 515 516 517
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-8))
    (ws-error "Marker 8 not set")))

(defun ws-find-marker-9 ()
  "In WordStar mode: Go to marker 9."
  (interactive)
  (if ws-marker-9
518
      (progn
Jim Blandy's avatar
Jim Blandy committed
519 520 521 522 523 524 525 526
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-marker-9))
    (ws-error "Marker 9 not set")))

(defun ws-goto-block-begin ()
  "In WordStar mode: Go to block begin marker."
  (interactive)
  (if ws-block-begin-marker
527
      (progn
Jim Blandy's avatar
Jim Blandy committed
528 529 530 531 532 533 534 535 536 537
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-block-begin-marker))
    (ws-error "Block begin marker not set")))

(defun ws-search (string)
  "In WordStar mode: Search string, remember string for repetition."
  (interactive "sSearch for: ")
  (message "Forward (f) or backward (b)")
  (let ((direction
	 (read-char)))
538
    (cond ((equal (upcase direction) ?F)
Jim Blandy's avatar
Jim Blandy committed
539 540 541 542
	   (setq ws-search-string string)
	   (setq ws-search-direction t)
	   (setq ws-last-cursorposition (point-marker))
	   (search-forward string))
543
	  ((equal (upcase direction) ?B)
Jim Blandy's avatar
Jim Blandy committed
544 545 546 547 548 549 550 551 552 553
	   (setq ws-search-string string)
	   (setq ws-search-direction nil)
	   (setq ws-last-cursorposition (point-marker))
	   (search-backward string))
	  (t (keyboard-quit)))))

(defun ws-goto-block-end ()
  "In WordStar mode: Go to block end marker."
  (interactive)
  (if ws-block-end-marker
554
      (progn
Jim Blandy's avatar
Jim Blandy committed
555 556 557 558 559 560 561 562 563 564 565 566 567 568
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-block-end-marker))
    (ws-error "Block end marker not set")))

(defun ws-undo ()
  "In WordStar mode: Undo and give message about undoing more changes."
  (interactive)
  (undo)
  (message "Repeat C-q l to undo more changes."))

(defun ws-goto-last-cursorposition ()
  "In WordStar mode: "
  (interactive)
  (if ws-last-cursorposition
569
      (progn
Jim Blandy's avatar
Jim Blandy committed
570 571 572 573 574 575 576 577 578
	(setq ws-last-cursorposition (point-marker))
	(goto-char ws-last-cursorposition))
    (ws-error "No last cursor position available.")))

(defun ws-last-error ()
  "In WordStar mode: repeat last error message.
This will only work for errors raised by WordStar mode functions."
  (interactive)
  (if ws-last-errormessage
579
      (message "%s" ws-last-errormessage)
Jim Blandy's avatar
Jim Blandy committed
580 581 582 583 584 585 586 587 588 589
    (message "No WordStar error yet.")))

(defun ws-kill-eol ()
  "In WordStar mode: Kill to end of line (like WordStar, not like Emacs)."
  (interactive)
  (let ((p (point)))
    (end-of-line)
    (kill-region p (point))))

(defun ws-kill-bol ()
590
  "In WordStar mode: Kill to beginning of line
Richard M. Stallman's avatar
Richard M. Stallman committed
591
\(like WordStar, not like Emacs)."
Jim Blandy's avatar
Jim Blandy committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
  (interactive)
  (let ((p (point)))
    (beginning-of-line)
    (kill-region (point) p)))

(defun kill-complete-line ()
  "Kill the complete line."
  (interactive)
  (beginning-of-line)
  (if (eobp) (error "End of buffer"))
  (let ((beg (point)))
    (forward-line 1)
    (kill-region beg (point))))

(defun ws-repeat-search ()
  "In WordStar mode: Repeat last search."
  (interactive)
  (setq ws-last-cursorposition (point-marker))
  (if ws-search-string
      (if ws-search-direction
	  (search-forward ws-search-string)
	(search-backward ws-search-string))
    (ws-error "No search to repeat")))

(defun ws-query-replace (from to)
  "In WordStar mode: Search string, remember string for repetition."
618
  (interactive "sReplace: \n\
Jim Blandy's avatar
Jim Blandy committed
619 620 621 622 623 624 625 626 627 628
sWith: " )
  (setq ws-search-string from)
  (setq ws-search-direction t)
  (setq ws-last-cursorposition (point-marker))
  (query-replace from to))

(defun ws-copy-block ()
  "In WordStar mode: Copy block to current cursor position."
  (interactive)
  (if (and ws-block-begin-marker ws-block-end-marker)
629
      (progn
Jim Blandy's avatar
Jim Blandy committed
630 631 632 633 634 635 636 637 638 639
	(copy-region-as-kill ws-block-begin-marker ws-block-end-marker)
	(yank)
	(save-excursion
	  (goto-char (region-beginning))
	  (setq ws-block-begin-marker (point-marker))
	  (goto-char (region-end))
	  (setq ws-block-end-marker (point-marker))))
    (ws-error (cond (ws-block-begin-marker "Block end marker not set")
		    (ws-block-end-marker "Block begin marker not set")
		    (t "Block markers not set")))))
Eric S. Raymond's avatar
Eric S. Raymond committed
640

Richard M. Stallman's avatar
Richard M. Stallman committed
641 642
(provide 'ws-mode)

Eric S. Raymond's avatar
Eric S. Raymond committed
643
;;; ws-mode.el ends here