1 ;;; ws-mode.el --- WordStar emulation mode for GNU Emacs
3 ;; Copyright (C) 1991, 2001-2013 Free Software Foundation, Inc.
5 ;; Author: Juergen Nickelsen <nickel@cs.tu-berlin.de>
7 ;; Keywords: emulations
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This emulates WordStar, with a major mode.
29 (defvar wordstar-C-k-map
30 (let ((map (make-keymap)))
31 (define-key map
" " ())
32 (define-key map
"0" 'ws-set-marker-0
)
33 (define-key map
"1" 'ws-set-marker-1
)
34 (define-key map
"2" 'ws-set-marker-2
)
35 (define-key map
"3" 'ws-set-marker-3
)
36 (define-key map
"4" 'ws-set-marker-4
)
37 (define-key map
"5" 'ws-set-marker-5
)
38 (define-key map
"6" 'ws-set-marker-6
)
39 (define-key map
"7" 'ws-set-marker-7
)
40 (define-key map
"8" 'ws-set-marker-8
)
41 (define-key map
"9" 'ws-set-marker-9
)
42 (define-key map
"b" 'ws-begin-block
)
43 (define-key map
"\C-b" 'ws-begin-block
)
44 (define-key map
"c" 'ws-copy-block
)
45 (define-key map
"\C-c" 'ws-copy-block
)
46 (define-key map
"d" 'save-buffers-kill-emacs
)
47 (define-key map
"\C-d" 'save-buffers-kill-emacs
)
48 (define-key map
"f" 'find-file
)
49 (define-key map
"\C-f" 'find-file
)
50 (define-key map
"h" 'ws-show-markers
)
51 (define-key map
"\C-h" 'ws-show-markers
)
52 (define-key map
"i" 'ws-indent-block
)
53 (define-key map
"\C-i" 'ws-indent-block
)
54 (define-key map
"k" 'ws-end-block
)
55 (define-key map
"\C-k" 'ws-end-block
)
56 (define-key map
"p" 'ws-print-block
)
57 (define-key map
"\C-p" 'ws-print-block
)
58 (define-key map
"q" 'kill-emacs
)
59 (define-key map
"\C-q" 'kill-emacs
)
60 (define-key map
"r" 'insert-file
)
61 (define-key map
"\C-r" 'insert-file
)
62 (define-key map
"s" 'save-some-buffers
)
63 (define-key map
"\C-s" 'save-some-buffers
)
64 (define-key map
"t" 'ws-mark-word
)
65 (define-key map
"\C-t" 'ws-mark-word
)
66 (define-key map
"u" 'ws-exdent-block
)
67 (define-key map
"\C-u" 'keyboard-quit
)
68 (define-key map
"v" 'ws-move-block
)
69 (define-key map
"\C-v" 'ws-move-block
)
70 (define-key map
"w" 'ws-write-block
)
71 (define-key map
"\C-w" 'ws-write-block
)
72 (define-key map
"x" 'save-buffers-kill-emacs
)
73 (define-key map
"\C-x" 'save-buffers-kill-emacs
)
74 (define-key map
"y" 'ws-delete-block
)
75 (define-key map
"\C-y" 'ws-delete-block
)
79 (defvar wordstar-C-o-map
80 (let ((map (make-keymap)))
81 (define-key map
" " ())
82 (define-key map
"c" 'wordstar-center-line
)
83 (define-key map
"\C-c" 'wordstar-center-line
)
84 (define-key map
"b" 'switch-to-buffer
)
85 (define-key map
"\C-b" 'switch-to-buffer
)
86 (define-key map
"j" 'justify-current-line
)
87 (define-key map
"\C-j" 'justify-current-line
)
88 (define-key map
"k" 'kill-buffer
)
89 (define-key map
"\C-k" 'kill-buffer
)
90 (define-key map
"l" 'list-buffers
)
91 (define-key map
"\C-l" 'list-buffers
)
92 (define-key map
"m" 'auto-fill-mode
)
93 (define-key map
"\C-m" 'auto-fill-mode
)
94 (define-key map
"r" 'set-fill-column
)
95 (define-key map
"\C-r" 'set-fill-column
)
96 (define-key map
"\C-u" 'keyboard-quit
)
97 (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
)
104 (defvar wordstar-C-q-map
105 (let ((map (make-keymap)))
106 (define-key map
" " ())
107 (define-key map
"0" 'ws-find-marker-0
)
108 (define-key map
"1" 'ws-find-marker-1
)
109 (define-key map
"2" 'ws-find-marker-2
)
110 (define-key map
"3" 'ws-find-marker-3
)
111 (define-key map
"4" 'ws-find-marker-4
)
112 (define-key map
"5" 'ws-find-marker-5
)
113 (define-key map
"6" 'ws-find-marker-6
)
114 (define-key map
"7" 'ws-find-marker-7
)
115 (define-key map
"8" 'ws-find-marker-8
)
116 (define-key map
"9" 'ws-find-marker-9
)
117 (define-key map
"a" 'ws-query-replace
)
118 (define-key map
"\C-a" 'ws-query-replace
)
119 (define-key map
"b" 'ws-goto-block-begin
)
120 (define-key map
"\C-b" 'ws-goto-block-begin
)
121 (define-key map
"c" 'end-of-buffer
)
122 (define-key map
"\C-c" 'end-of-buffer
)
123 (define-key map
"d" 'end-of-line
)
124 (define-key map
"\C-d" 'end-of-line
)
125 (define-key map
"f" 'ws-search
)
126 (define-key map
"\C-f" 'ws-search
)
127 (define-key map
"k" 'ws-goto-block-end
)
128 (define-key map
"\C-k" 'ws-goto-block-end
)
129 (define-key map
"l" 'ws-undo
)
130 (define-key map
"\C-l" 'ws-undo
)
131 (define-key map
"p" 'ws-last-cursorp
)
132 (define-key map
"\C-p" 'ws-last-cursorp
)
133 (define-key map
"r" 'beginning-of-buffer
)
134 (define-key map
"\C-r" 'beginning-of-buffer
)
135 (define-key map
"s" 'beginning-of-line
)
136 (define-key map
"\C-s" 'beginning-of-line
)
137 (define-key map
"\C-u" 'keyboard-quit
)
138 (define-key map
"w" 'ws-last-error
)
139 (define-key map
"\C-w" 'ws-last-error
)
140 (define-key map
"y" 'ws-kill-eol
)
141 (define-key map
"\C-y" 'ws-kill-eol
)
142 (define-key map
"\177" 'ws-kill-bol
)
146 (defvar wordstar-mode-map
147 (let ((map (make-keymap)))
148 (define-key map
"\C-a" 'backward-word
)
149 (define-key map
"\C-b" 'fill-paragraph
)
150 (define-key map
"\C-c" 'scroll-up-command
)
151 (define-key map
"\C-d" 'forward-char
)
152 (define-key map
"\C-e" 'previous-line
)
153 (define-key map
"\C-f" 'forward-word
)
154 (define-key map
"\C-g" 'delete-char
)
155 (define-key map
"\C-h" 'backward-char
)
156 (define-key map
"\C-i" 'indent-for-tab-command
)
157 (define-key map
"\C-j" 'help-for-help
)
158 (define-key map
"\C-k" wordstar-C-k-map
)
159 (define-key map
"\C-l" 'ws-repeat-search
)
160 (define-key map
"\C-n" 'open-line
)
161 (define-key map
"\C-o" wordstar-C-o-map
)
162 (define-key map
"\C-p" 'quoted-insert
)
163 (define-key map
"\C-q" wordstar-C-q-map
)
164 (define-key map
"\C-r" 'scroll-down-command
)
165 (define-key map
"\C-s" 'backward-char
)
166 (define-key map
"\C-t" 'kill-word
)
167 (define-key map
"\C-u" 'keyboard-quit
)
168 (define-key map
"\C-v" 'overwrite-mode
)
169 (define-key map
"\C-w" 'scroll-down-line
)
170 (define-key map
"\C-x" 'next-line
)
171 (define-key map
"\C-y" 'kill-complete-line
)
172 (define-key map
"\C-z" 'scroll-up-line
)
176 ;; wordstar-C-j-map not yet implemented
177 (defvar wordstar-C-j-map nil
"")
180 (put 'wordstar-mode
'mode-class
'special
)
183 (defun wordstar-mode ()
184 "Major mode with WordStar-like key bindings.
187 - Help menus with WordStar commands (C-j just calls help-for-help)
189 - Options for search and replace
190 - Show markers (C-k h) is somewhat strange
191 - Search and replace (C-q a) is only available in forward direction
193 No key bindings beginning with ESC are installed, they will work
196 The key bindings are:
206 C-i indent-for-tab-command
219 C-y kill-complete-line
222 C-k 0 ws-set-marker-0
223 C-k 1 ws-set-marker-1
224 C-k 2 ws-set-marker-2
225 C-k 3 ws-set-marker-3
226 C-k 4 ws-set-marker-4
227 C-k 5 ws-set-marker-5
228 C-k 6 ws-set-marker-6
229 C-k 7 ws-set-marker-7
230 C-k 8 ws-set-marker-8
231 C-k 9 ws-set-marker-9
234 C-k d save-buffers-kill-emacs
236 C-k h ws-show-markers
237 C-k i ws-indent-block
242 C-k s save-some-buffers
244 C-k u ws-exdent-block
245 C-k C-u keyboard-quit
249 C-k y ws-delete-block
251 C-o c wordstar-center-line
252 C-o b switch-to-buffer
253 C-o j justify-current-line
257 C-o r set-fill-column
258 C-o C-u keyboard-quit
259 C-o wd delete-other-windows
260 C-o wh split-window-right
262 C-o wv split-window-below
264 C-q 0 ws-find-marker-0
265 C-q 1 ws-find-marker-1
266 C-q 2 ws-find-marker-2
267 C-q 3 ws-find-marker-3
268 C-q 4 ws-find-marker-4
269 C-q 5 ws-find-marker-5
270 C-q 6 ws-find-marker-6
271 C-q 7 ws-find-marker-7
272 C-q 8 ws-find-marker-8
273 C-q 9 ws-find-marker-9
274 C-q a ws-query-replace
275 C-q b ws-to-block-begin
279 C-q k ws-to-block-end
281 C-q p ws-last-cursorp
282 C-q r beginning-of-buffer
283 C-q C-u keyboard-quit
289 (kill-all-local-variables)
290 (use-local-map wordstar-mode-map
)
291 (setq mode-name
"WordStar")
292 (setq major-mode
'wordstar-mode
)
293 (run-mode-hooks 'wordstar-mode-hook
))
296 (defun wordstar-center-paragraph ()
297 "Center each line in the paragraph at or after point.
298 See `wordstar-center-line' for more info."
302 (or (bolp) (newline 1))
305 (wordstar-center-region (point) end
))))
307 (defun wordstar-center-region (from to
)
308 "Center each line starting in the region.
309 See `wordstar-center-line' for more info."
313 (setq to from from tem
)))
316 (narrow-to-region from to
)
319 (wordstar-center-line)
322 (defun wordstar-center-line ()
323 "Center the line point is on, within the width specified by `fill-column'.
324 This means adjusting the indentation to match
325 the distance between the end of the text and `fill-column'."
330 (delete-horizontal-space)
332 (delete-horizontal-space)
333 (setq line-length
(current-column))
337 (/ (- fill-column left-margin line-length
) 2))))))
340 ;; wordstar special variables:
342 (defvar ws-marker-0 nil
"Position marker 0 in WordStar mode.")
343 (defvar ws-marker-1 nil
"Position marker 1 in WordStar mode.")
344 (defvar ws-marker-2 nil
"Position marker 2 in WordStar mode.")
345 (defvar ws-marker-3 nil
"Position marker 3 in WordStar mode.")
346 (defvar ws-marker-4 nil
"Position marker 4 in WordStar mode.")
347 (defvar ws-marker-5 nil
"Position marker 5 in WordStar mode.")
348 (defvar ws-marker-6 nil
"Position marker 6 in WordStar mode.")
349 (defvar ws-marker-7 nil
"Position marker 7 in WordStar mode.")
350 (defvar ws-marker-8 nil
"Position marker 8 in WordStar mode.")
351 (defvar ws-marker-9 nil
"Position marker 9 in WordStar mode.")
353 (defvar ws-block-begin-marker nil
"Beginning of \"Block\" in WordStar mode.")
354 (defvar ws-block-end-marker nil
"End of \"Block\" in WordStar mode.")
356 (defvar ws-search-string nil
"String of last search in WordStar mode.")
357 (defvar ws-search-direction t
358 "Direction of last search in WordStar mode. t if forward, nil if backward.")
360 (defvar ws-last-cursorposition nil
361 "Position before last search etc. in WordStar mode.")
363 (defvar ws-last-errormessage nil
364 "Last error message issued by a WordStar mode function.")
367 ;; wordstar special functions:
369 (defun ws-error (string)
370 "Report error of a WordStar special function. Error message is saved
371 in ws-last-errormessage for recovery with C-q w."
372 (setq ws-last-errormessage string
)
375 (defun ws-set-marker-0 ()
376 "In WordStar mode: Set marker 0 to current cursor position."
378 (setq ws-marker-0
(point-marker))
379 (message "Marker 0 set"))
381 (defun ws-set-marker-1 ()
382 "In WordStar mode: Set marker 1 to current cursor position."
384 (setq ws-marker-1
(point-marker))
385 (message "Marker 1 set"))
387 (defun ws-set-marker-2 ()
388 "In WordStar mode: Set marker 2 to current cursor position."
390 (setq ws-marker-2
(point-marker))
391 (message "Marker 2 set"))
393 (defun ws-set-marker-3 ()
394 "In WordStar mode: Set marker 3 to current cursor position."
396 (setq ws-marker-3
(point-marker))
397 (message "Marker 3 set"))
399 (defun ws-set-marker-4 ()
400 "In WordStar mode: Set marker 4 to current cursor position."
402 (setq ws-marker-4
(point-marker))
403 (message "Marker 4 set"))
405 (defun ws-set-marker-5 ()
406 "In WordStar mode: Set marker 5 to current cursor position."
408 (setq ws-marker-5
(point-marker))
409 (message "Marker 5 set"))
411 (defun ws-set-marker-6 ()
412 "In WordStar mode: Set marker 6 to current cursor position."
414 (setq ws-marker-6
(point-marker))
415 (message "Marker 6 set"))
417 (defun ws-set-marker-7 ()
418 "In WordStar mode: Set marker 7 to current cursor position."
420 (setq ws-marker-7
(point-marker))
421 (message "Marker 7 set"))
423 (defun ws-set-marker-8 ()
424 "In WordStar mode: Set marker 8 to current cursor position."
426 (setq ws-marker-8
(point-marker))
427 (message "Marker 8 set"))
429 (defun ws-set-marker-9 ()
430 "In WordStar mode: Set marker 9 to current cursor position."
432 (setq ws-marker-9
(point-marker))
433 (message "Marker 9 set"))
435 (defun ws-begin-block ()
436 "In WordStar mode: Set block begin marker to current cursor position."
438 (setq ws-block-begin-marker
(point-marker))
439 (message "Block begin marker set"))
441 (defun ws-show-markers ()
442 "In WordStar mode: Show block markers."
444 (if (or ws-block-begin-marker ws-block-end-marker
)
446 (if ws-block-begin-marker
448 (goto-char ws-block-begin-marker
)
449 (message "Block begin marker")
451 (message "Block begin marker not set")
453 (if ws-block-end-marker
455 (goto-char ws-block-end-marker
)
456 (message "Block end marker")
458 (message "Block end marker not set"))
460 (message "Block markers not set")))
463 (defun ws-indent-block ()
464 "In WordStar mode: Indent block (not yet implemented)."
466 (ws-error "Indent block not yet implemented"))
468 (defun ws-end-block ()
469 "In WordStar mode: Set block end marker to current cursor position."
471 (setq ws-block-end-marker
(point-marker))
472 (message "Block end marker set"))
474 (defun ws-print-block ()
475 "In WordStar mode: Print block."
477 (message "Don't do this. Write block to a file (C-k w) and print this file."))
479 (defun ws-mark-word ()
480 "In WordStar mode: Mark current word as block."
490 (defun ws-exdent-block ()
491 "I don't know what this (C-k u) should do."
493 (ws-error "This won't be done -- not yet implemented."))
495 (defun ws-move-block ()
496 "In WordStar mode: Move block to current cursor position."
498 (if (and ws-block-begin-marker ws-block-end-marker
)
500 (kill-region ws-block-begin-marker ws-block-end-marker
)
503 (goto-char (region-beginning))
504 (setq ws-block-begin-marker
(point-marker))
505 (goto-char (region-end))
506 (setq ws-block-end-marker
(point-marker))))
507 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
508 (ws-block-end-marker "Block begin marker not set")
509 (t "Block markers not set")))))
511 (defun ws-write-block ()
512 "In WordStar mode: Write block to file."
514 (if (and ws-block-begin-marker ws-block-end-marker
)
515 (let ((filename (read-file-name "Write block to file: ")))
516 (write-region ws-block-begin-marker ws-block-end-marker filename
))
517 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
518 (ws-block-end-marker "Block begin marker not set")
519 (t "Block markers not set")))))
522 (defun ws-delete-block ()
523 "In WordStar mode: Delete block."
525 (if (and ws-block-begin-marker ws-block-end-marker
)
527 (kill-region ws-block-begin-marker ws-block-end-marker
)
528 (setq ws-block-end-marker nil
)
529 (setq ws-block-begin-marker nil
))
530 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
531 (ws-block-end-marker "Block begin marker not set")
532 (t "Block markers not set")))))
534 (defun ws-find-marker-0 ()
535 "In WordStar mode: Go to marker 0."
539 (setq ws-last-cursorposition
(point-marker))
540 (goto-char ws-marker-0
))
541 (ws-error "Marker 0 not set")))
543 (defun ws-find-marker-1 ()
544 "In WordStar mode: Go to marker 1."
548 (setq ws-last-cursorposition
(point-marker))
549 (goto-char ws-marker-1
))
550 (ws-error "Marker 1 not set")))
552 (defun ws-find-marker-2 ()
553 "In WordStar mode: Go to marker 2."
557 (setq ws-last-cursorposition
(point-marker))
558 (goto-char ws-marker-2
))
559 (ws-error "Marker 2 not set")))
561 (defun ws-find-marker-3 ()
562 "In WordStar mode: Go to marker 3."
566 (setq ws-last-cursorposition
(point-marker))
567 (goto-char ws-marker-3
))
568 (ws-error "Marker 3 not set")))
570 (defun ws-find-marker-4 ()
571 "In WordStar mode: Go to marker 4."
575 (setq ws-last-cursorposition
(point-marker))
576 (goto-char ws-marker-4
))
577 (ws-error "Marker 4 not set")))
579 (defun ws-find-marker-5 ()
580 "In WordStar mode: Go to marker 5."
584 (setq ws-last-cursorposition
(point-marker))
585 (goto-char ws-marker-5
))
586 (ws-error "Marker 5 not set")))
588 (defun ws-find-marker-6 ()
589 "In WordStar mode: Go to marker 6."
593 (setq ws-last-cursorposition
(point-marker))
594 (goto-char ws-marker-6
))
595 (ws-error "Marker 6 not set")))
597 (defun ws-find-marker-7 ()
598 "In WordStar mode: Go to marker 7."
602 (setq ws-last-cursorposition
(point-marker))
603 (goto-char ws-marker-7
))
604 (ws-error "Marker 7 not set")))
606 (defun ws-find-marker-8 ()
607 "In WordStar mode: Go to marker 8."
611 (setq ws-last-cursorposition
(point-marker))
612 (goto-char ws-marker-8
))
613 (ws-error "Marker 8 not set")))
615 (defun ws-find-marker-9 ()
616 "In WordStar mode: Go to marker 9."
620 (setq ws-last-cursorposition
(point-marker))
621 (goto-char ws-marker-9
))
622 (ws-error "Marker 9 not set")))
624 (defun ws-goto-block-begin ()
625 "In WordStar mode: Go to block begin marker."
627 (if ws-block-begin-marker
629 (setq ws-last-cursorposition
(point-marker))
630 (goto-char ws-block-begin-marker
))
631 (ws-error "Block begin marker not set")))
633 (defun ws-search (string)
634 "In WordStar mode: Search string, remember string for repetition."
635 (interactive "sSearch for: ")
636 (message "Forward (f) or backward (b)")
639 (cond ((equal (upcase direction
) ?F
)
640 (setq ws-search-string string
)
641 (setq ws-search-direction t
)
642 (setq ws-last-cursorposition
(point-marker))
643 (search-forward string
))
644 ((equal (upcase direction
) ?B
)
645 (setq ws-search-string string
)
646 (setq ws-search-direction nil
)
647 (setq ws-last-cursorposition
(point-marker))
648 (search-backward string
))
649 (t (keyboard-quit)))))
651 (defun ws-goto-block-end ()
652 "In WordStar mode: Go to block end marker."
654 (if ws-block-end-marker
656 (setq ws-last-cursorposition
(point-marker))
657 (goto-char ws-block-end-marker
))
658 (ws-error "Block end marker not set")))
661 "In WordStar mode: Undo and give message about undoing more changes."
664 (message "Repeat C-q l to undo more changes."))
666 (defun ws-goto-last-cursorposition ()
669 (if ws-last-cursorposition
671 (setq ws-last-cursorposition
(point-marker))
672 (goto-char ws-last-cursorposition
))
673 (ws-error "No last cursor position available.")))
675 (defun ws-last-error ()
676 "In WordStar mode: repeat last error message.
677 This will only work for errors raised by WordStar mode functions."
679 (if ws-last-errormessage
680 (message "%s" ws-last-errormessage
)
681 (message "No WordStar error yet.")))
683 (defun ws-kill-eol ()
684 "In WordStar mode: Kill to end of line (like WordStar, not like Emacs)."
688 (kill-region p
(point))))
690 (defun ws-kill-bol ()
691 "In WordStar mode: Kill to beginning of line
692 \(like WordStar, not like Emacs)."
696 (kill-region (point) p
)))
698 (defun kill-complete-line ()
699 "Kill the complete line."
702 (if (eobp) (error "End of buffer"))
705 (kill-region beg
(point))))
707 (defun ws-repeat-search ()
708 "In WordStar mode: Repeat last search."
710 (setq ws-last-cursorposition
(point-marker))
712 (if ws-search-direction
713 (search-forward ws-search-string
)
714 (search-backward ws-search-string
))
715 (ws-error "No search to repeat")))
717 (defun ws-query-replace (from to
)
718 "In WordStar mode: Search string, remember string for repetition."
719 (interactive "sReplace: \n\
721 (setq ws-search-string from
)
722 (setq ws-search-direction t
)
723 (setq ws-last-cursorposition
(point-marker))
724 (query-replace from to
))
726 (defun ws-copy-block ()
727 "In WordStar mode: Copy block to current cursor position."
729 (if (and ws-block-begin-marker ws-block-end-marker
)
731 (copy-region-as-kill ws-block-begin-marker ws-block-end-marker
)
734 (goto-char (region-beginning))
735 (setq ws-block-begin-marker
(point-marker))
736 (goto-char (region-end))
737 (setq ws-block-end-marker
(point-marker))))
738 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
739 (ws-block-end-marker "Block begin marker not set")
740 (t "Block markers not set")))))
744 ;;; ws-mode.el ends here