* Makefile.in (install-arch-indep): Avoid readdir race.
[emacs.git] / lisp / emulation / ws-mode.el
blobfb140e6ebd7a530472971be298aae748481cbef5
1 ;;; ws-mode.el --- WordStar emulation mode for GNU Emacs
3 ;; Copyright (C) 1991, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Juergen Nickelsen <nickel@cs.tu-berlin.de>
6 ;; Version: 0.7
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/>.
24 ;;; Commentary:
26 ;; This emulates WordStar, with a major mode.
28 ;;; Code:
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)
76 map))
78 (defvar wordstar-C-o-map
79 (let ((map (make-keymap)))
80 (define-key map " " ())
81 (define-key map "c" 'wordstar-center-line)
82 (define-key map "\C-c" 'wordstar-center-line)
83 (define-key map "b" 'switch-to-buffer)
84 (define-key map "\C-b" 'switch-to-buffer)
85 (define-key map "j" 'justify-current-line)
86 (define-key map "\C-j" 'justify-current-line)
87 (define-key map "k" 'kill-buffer)
88 (define-key map "\C-k" 'kill-buffer)
89 (define-key map "l" 'list-buffers)
90 (define-key map "\C-l" 'list-buffers)
91 (define-key map "m" 'auto-fill-mode)
92 (define-key map "\C-m" 'auto-fill-mode)
93 (define-key map "r" 'set-fill-column)
94 (define-key map "\C-r" 'set-fill-column)
95 (define-key map "\C-u" 'keyboard-quit)
96 (define-key map "wd" 'delete-other-windows)
97 (define-key map "wh" 'split-window-right)
98 (define-key map "wo" 'other-window)
99 (define-key map "wv" 'split-window-below)
100 map)
103 (defvar wordstar-C-q-map
104 (let ((map (make-keymap)))
105 (define-key map " " ())
106 (define-key map "0" 'ws-find-marker-0)
107 (define-key map "1" 'ws-find-marker-1)
108 (define-key map "2" 'ws-find-marker-2)
109 (define-key map "3" 'ws-find-marker-3)
110 (define-key map "4" 'ws-find-marker-4)
111 (define-key map "5" 'ws-find-marker-5)
112 (define-key map "6" 'ws-find-marker-6)
113 (define-key map "7" 'ws-find-marker-7)
114 (define-key map "8" 'ws-find-marker-8)
115 (define-key map "9" 'ws-find-marker-9)
116 (define-key map "a" 'ws-query-replace)
117 (define-key map "\C-a" 'ws-query-replace)
118 (define-key map "b" 'ws-goto-block-begin)
119 (define-key map "\C-b" 'ws-goto-block-begin)
120 (define-key map "c" 'end-of-buffer)
121 (define-key map "\C-c" 'end-of-buffer)
122 (define-key map "d" 'end-of-line)
123 (define-key map "\C-d" 'end-of-line)
124 (define-key map "f" 'ws-search)
125 (define-key map "\C-f" 'ws-search)
126 (define-key map "k" 'ws-goto-block-end)
127 (define-key map "\C-k" 'ws-goto-block-end)
128 (define-key map "l" 'ws-undo)
129 (define-key map "\C-l" 'ws-undo)
130 (define-key map "p" 'ws-last-cursorp)
131 (define-key map "\C-p" 'ws-last-cursorp)
132 (define-key map "r" 'beginning-of-buffer)
133 (define-key map "\C-r" 'beginning-of-buffer)
134 (define-key map "s" 'beginning-of-line)
135 (define-key map "\C-s" 'beginning-of-line)
136 (define-key map "\C-u" 'keyboard-quit)
137 (define-key map "w" 'ws-last-error)
138 (define-key map "\C-w" 'ws-last-error)
139 (define-key map "y" 'ws-kill-eol)
140 (define-key map "\C-y" 'ws-kill-eol)
141 (define-key map "\177" 'ws-kill-bol)
142 map))
144 (defvar wordstar-mode-map
145 (let ((map (make-keymap)))
146 (define-key map "\C-a" 'backward-word)
147 (define-key map "\C-b" 'fill-paragraph)
148 (define-key map "\C-c" 'scroll-up-command)
149 (define-key map "\C-d" 'forward-char)
150 (define-key map "\C-e" 'previous-line)
151 (define-key map "\C-f" 'forward-word)
152 (define-key map "\C-g" 'delete-char)
153 (define-key map "\C-h" 'backward-char)
154 (define-key map "\C-i" 'indent-for-tab-command)
155 (define-key map "\C-j" 'help-for-help)
156 (define-key map "\C-k" wordstar-C-k-map)
157 (define-key map "\C-l" 'ws-repeat-search)
158 (define-key map "\C-n" 'open-line)
159 (define-key map "\C-o" wordstar-C-o-map)
160 (define-key map "\C-p" 'quoted-insert)
161 (define-key map "\C-q" wordstar-C-q-map)
162 (define-key map "\C-r" 'scroll-down-command)
163 (define-key map "\C-s" 'backward-char)
164 (define-key map "\C-t" 'kill-word)
165 (define-key map "\C-u" 'keyboard-quit)
166 (define-key map "\C-v" 'overwrite-mode)
167 (define-key map "\C-w" 'scroll-down-line)
168 (define-key map "\C-x" 'next-line)
169 (define-key map "\C-y" 'kill-complete-line)
170 (define-key map "\C-z" 'scroll-up-line)
171 map))
173 ;; wordstar-C-j-map not yet implemented
174 (defvar wordstar-C-j-map nil)
177 (put 'wordstar-mode 'mode-class 'special)
179 ;;;###autoload
180 (define-derived-mode wordstar-mode fundamental-mode "WordStar"
181 "Major mode with WordStar-like key bindings.
183 BUGS:
184 - Help menus with WordStar commands (C-j just calls help-for-help)
185 are not implemented
186 - Options for search and replace
187 - Show markers (C-k h) is somewhat strange
188 - Search and replace (C-q a) is only available in forward direction
190 No key bindings beginning with ESC are installed, they will work
191 Emacs-like.")
194 (defun wordstar-center-paragraph ()
195 "Center each line in the paragraph at or after point.
196 See `wordstar-center-line' for more info."
197 (interactive)
198 (save-excursion
199 (forward-paragraph)
200 (or (bolp) (newline 1))
201 (let ((end (point)))
202 (backward-paragraph)
203 (wordstar-center-region (point) end))))
205 (defun wordstar-center-region (from to)
206 "Center each line starting in the region.
207 See `wordstar-center-line' for more info."
208 (interactive "r")
209 (if (> from to)
210 (let ((tem to))
211 (setq to from from tem)))
212 (save-excursion
213 (save-restriction
214 (narrow-to-region from to)
215 (goto-char from)
216 (while (not (eobp))
217 (wordstar-center-line)
218 (forward-line 1)))))
220 (defun wordstar-center-line ()
221 "Center the line point is on, within the width specified by `fill-column'.
222 This means adjusting the indentation to match
223 the distance between the end of the text and `fill-column'."
224 (interactive)
225 (save-excursion
226 (let (line-length)
227 (beginning-of-line)
228 (delete-horizontal-space)
229 (end-of-line)
230 (delete-horizontal-space)
231 (setq line-length (current-column))
232 (beginning-of-line)
233 (indent-to
234 (+ left-margin
235 (/ (- fill-column left-margin line-length) 2))))))
237 ;;;;;;;;;;;
238 ;; wordstar special variables:
240 (defvar ws-marker-0 nil "Position marker 0 in WordStar mode.")
241 (defvar ws-marker-1 nil "Position marker 1 in WordStar mode.")
242 (defvar ws-marker-2 nil "Position marker 2 in WordStar mode.")
243 (defvar ws-marker-3 nil "Position marker 3 in WordStar mode.")
244 (defvar ws-marker-4 nil "Position marker 4 in WordStar mode.")
245 (defvar ws-marker-5 nil "Position marker 5 in WordStar mode.")
246 (defvar ws-marker-6 nil "Position marker 6 in WordStar mode.")
247 (defvar ws-marker-7 nil "Position marker 7 in WordStar mode.")
248 (defvar ws-marker-8 nil "Position marker 8 in WordStar mode.")
249 (defvar ws-marker-9 nil "Position marker 9 in WordStar mode.")
251 (defvar ws-block-begin-marker nil "Beginning of \"Block\" in WordStar mode.")
252 (defvar ws-block-end-marker nil "End of \"Block\" in WordStar mode.")
254 (defvar ws-search-string nil "String of last search in WordStar mode.")
255 (defvar ws-search-direction t
256 "Direction of last search in WordStar mode. t if forward, nil if backward.")
258 (defvar ws-last-cursorposition nil
259 "Position before last search etc. in WordStar mode.")
261 (defvar ws-last-errormessage nil
262 "Last error message issued by a WordStar mode function.")
264 ;;;;;;;;;;;
265 ;; wordstar special functions:
267 (defun ws-error (string)
268 "Report error of a WordStar special function. Error message is saved
269 in ws-last-errormessage for recovery with C-q w."
270 (setq ws-last-errormessage string)
271 (error string))
273 (defun ws-set-marker-0 ()
274 "In WordStar mode: Set marker 0 to current cursor position."
275 (interactive)
276 (setq ws-marker-0 (point-marker))
277 (message "Marker 0 set"))
279 (defun ws-set-marker-1 ()
280 "In WordStar mode: Set marker 1 to current cursor position."
281 (interactive)
282 (setq ws-marker-1 (point-marker))
283 (message "Marker 1 set"))
285 (defun ws-set-marker-2 ()
286 "In WordStar mode: Set marker 2 to current cursor position."
287 (interactive)
288 (setq ws-marker-2 (point-marker))
289 (message "Marker 2 set"))
291 (defun ws-set-marker-3 ()
292 "In WordStar mode: Set marker 3 to current cursor position."
293 (interactive)
294 (setq ws-marker-3 (point-marker))
295 (message "Marker 3 set"))
297 (defun ws-set-marker-4 ()
298 "In WordStar mode: Set marker 4 to current cursor position."
299 (interactive)
300 (setq ws-marker-4 (point-marker))
301 (message "Marker 4 set"))
303 (defun ws-set-marker-5 ()
304 "In WordStar mode: Set marker 5 to current cursor position."
305 (interactive)
306 (setq ws-marker-5 (point-marker))
307 (message "Marker 5 set"))
309 (defun ws-set-marker-6 ()
310 "In WordStar mode: Set marker 6 to current cursor position."
311 (interactive)
312 (setq ws-marker-6 (point-marker))
313 (message "Marker 6 set"))
315 (defun ws-set-marker-7 ()
316 "In WordStar mode: Set marker 7 to current cursor position."
317 (interactive)
318 (setq ws-marker-7 (point-marker))
319 (message "Marker 7 set"))
321 (defun ws-set-marker-8 ()
322 "In WordStar mode: Set marker 8 to current cursor position."
323 (interactive)
324 (setq ws-marker-8 (point-marker))
325 (message "Marker 8 set"))
327 (defun ws-set-marker-9 ()
328 "In WordStar mode: Set marker 9 to current cursor position."
329 (interactive)
330 (setq ws-marker-9 (point-marker))
331 (message "Marker 9 set"))
333 (defun ws-begin-block ()
334 "In WordStar mode: Set block begin marker to current cursor position."
335 (interactive)
336 (setq ws-block-begin-marker (point-marker))
337 (message "Block begin marker set"))
339 (defun ws-show-markers ()
340 "In WordStar mode: Show block markers."
341 (interactive)
342 (if (or ws-block-begin-marker ws-block-end-marker)
343 (save-excursion
344 (if ws-block-begin-marker
345 (progn
346 (goto-char ws-block-begin-marker)
347 (message "Block begin marker")
348 (sit-for 2))
349 (message "Block begin marker not set")
350 (sit-for 2))
351 (if ws-block-end-marker
352 (progn
353 (goto-char ws-block-end-marker)
354 (message "Block end marker")
355 (sit-for 2))
356 (message "Block end marker not set"))
357 (message ""))
358 (message "Block markers not set")))
361 (defun ws-indent-block ()
362 "In WordStar mode: Indent block (not yet implemented)."
363 (interactive)
364 (ws-error "Indent block not yet implemented"))
366 (defun ws-end-block ()
367 "In WordStar mode: Set block end marker to current cursor position."
368 (interactive)
369 (setq ws-block-end-marker (point-marker))
370 (message "Block end marker set"))
372 (defun ws-print-block ()
373 "In WordStar mode: Print block."
374 (interactive)
375 (message "Don't do this. Write block to a file (C-k w) and print this file."))
377 (defun ws-mark-word ()
378 "In WordStar mode: Mark current word as block."
379 (interactive)
380 (save-excursion
381 (forward-word 1)
382 (sit-for 1)
383 (ws-end-block)
384 (forward-word -1)
385 (sit-for 1)
386 (ws-begin-block)))
388 (defun ws-exdent-block ()
389 "I don't know what this (C-k u) should do."
390 (interactive)
391 (ws-error "This won't be done -- not yet implemented."))
393 (defun ws-move-block ()
394 "In WordStar mode: Move block to current cursor position."
395 (interactive)
396 (if (and ws-block-begin-marker ws-block-end-marker)
397 (progn
398 (kill-region ws-block-begin-marker ws-block-end-marker)
399 (yank)
400 (save-excursion
401 (goto-char (region-beginning))
402 (setq ws-block-begin-marker (point-marker))
403 (goto-char (region-end))
404 (setq ws-block-end-marker (point-marker))))
405 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
406 (ws-block-end-marker "Block begin marker not set")
407 (t "Block markers not set")))))
409 (defun ws-write-block ()
410 "In WordStar mode: Write block to file."
411 (interactive)
412 (if (and ws-block-begin-marker ws-block-end-marker)
413 (let ((filename (read-file-name "Write block to file: ")))
414 (write-region ws-block-begin-marker ws-block-end-marker filename))
415 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
416 (ws-block-end-marker "Block begin marker not set")
417 (t "Block markers not set")))))
420 (defun ws-delete-block ()
421 "In WordStar mode: Delete block."
422 (interactive)
423 (if (and ws-block-begin-marker ws-block-end-marker)
424 (progn
425 (kill-region ws-block-begin-marker ws-block-end-marker)
426 (setq ws-block-end-marker nil)
427 (setq ws-block-begin-marker nil))
428 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
429 (ws-block-end-marker "Block begin marker not set")
430 (t "Block markers not set")))))
432 (defun ws-find-marker-0 ()
433 "In WordStar mode: Go to marker 0."
434 (interactive)
435 (if ws-marker-0
436 (progn
437 (setq ws-last-cursorposition (point-marker))
438 (goto-char ws-marker-0))
439 (ws-error "Marker 0 not set")))
441 (defun ws-find-marker-1 ()
442 "In WordStar mode: Go to marker 1."
443 (interactive)
444 (if ws-marker-1
445 (progn
446 (setq ws-last-cursorposition (point-marker))
447 (goto-char ws-marker-1))
448 (ws-error "Marker 1 not set")))
450 (defun ws-find-marker-2 ()
451 "In WordStar mode: Go to marker 2."
452 (interactive)
453 (if ws-marker-2
454 (progn
455 (setq ws-last-cursorposition (point-marker))
456 (goto-char ws-marker-2))
457 (ws-error "Marker 2 not set")))
459 (defun ws-find-marker-3 ()
460 "In WordStar mode: Go to marker 3."
461 (interactive)
462 (if ws-marker-3
463 (progn
464 (setq ws-last-cursorposition (point-marker))
465 (goto-char ws-marker-3))
466 (ws-error "Marker 3 not set")))
468 (defun ws-find-marker-4 ()
469 "In WordStar mode: Go to marker 4."
470 (interactive)
471 (if ws-marker-4
472 (progn
473 (setq ws-last-cursorposition (point-marker))
474 (goto-char ws-marker-4))
475 (ws-error "Marker 4 not set")))
477 (defun ws-find-marker-5 ()
478 "In WordStar mode: Go to marker 5."
479 (interactive)
480 (if ws-marker-5
481 (progn
482 (setq ws-last-cursorposition (point-marker))
483 (goto-char ws-marker-5))
484 (ws-error "Marker 5 not set")))
486 (defun ws-find-marker-6 ()
487 "In WordStar mode: Go to marker 6."
488 (interactive)
489 (if ws-marker-6
490 (progn
491 (setq ws-last-cursorposition (point-marker))
492 (goto-char ws-marker-6))
493 (ws-error "Marker 6 not set")))
495 (defun ws-find-marker-7 ()
496 "In WordStar mode: Go to marker 7."
497 (interactive)
498 (if ws-marker-7
499 (progn
500 (setq ws-last-cursorposition (point-marker))
501 (goto-char ws-marker-7))
502 (ws-error "Marker 7 not set")))
504 (defun ws-find-marker-8 ()
505 "In WordStar mode: Go to marker 8."
506 (interactive)
507 (if ws-marker-8
508 (progn
509 (setq ws-last-cursorposition (point-marker))
510 (goto-char ws-marker-8))
511 (ws-error "Marker 8 not set")))
513 (defun ws-find-marker-9 ()
514 "In WordStar mode: Go to marker 9."
515 (interactive)
516 (if ws-marker-9
517 (progn
518 (setq ws-last-cursorposition (point-marker))
519 (goto-char ws-marker-9))
520 (ws-error "Marker 9 not set")))
522 (defun ws-goto-block-begin ()
523 "In WordStar mode: Go to block begin marker."
524 (interactive)
525 (if ws-block-begin-marker
526 (progn
527 (setq ws-last-cursorposition (point-marker))
528 (goto-char ws-block-begin-marker))
529 (ws-error "Block begin marker not set")))
531 (defun ws-search (string)
532 "In WordStar mode: Search string, remember string for repetition."
533 (interactive "sSearch for: ")
534 (message "Forward (f) or backward (b)")
535 (let ((direction
536 (read-char)))
537 (cond ((equal (upcase direction) ?F)
538 (setq ws-search-string string)
539 (setq ws-search-direction t)
540 (setq ws-last-cursorposition (point-marker))
541 (search-forward string))
542 ((equal (upcase direction) ?B)
543 (setq ws-search-string string)
544 (setq ws-search-direction nil)
545 (setq ws-last-cursorposition (point-marker))
546 (search-backward string))
547 (t (keyboard-quit)))))
549 (defun ws-goto-block-end ()
550 "In WordStar mode: Go to block end marker."
551 (interactive)
552 (if ws-block-end-marker
553 (progn
554 (setq ws-last-cursorposition (point-marker))
555 (goto-char ws-block-end-marker))
556 (ws-error "Block end marker not set")))
558 (defun ws-undo ()
559 "In WordStar mode: Undo and give message about undoing more changes."
560 (interactive)
561 (undo)
562 (message "Repeat C-q l to undo more changes."))
564 (defun ws-goto-last-cursorposition ()
565 "In WordStar mode: "
566 (interactive)
567 (if ws-last-cursorposition
568 (progn
569 (setq ws-last-cursorposition (point-marker))
570 (goto-char ws-last-cursorposition))
571 (ws-error "No last cursor position available.")))
573 (defun ws-last-error ()
574 "In WordStar mode: repeat last error message.
575 This will only work for errors raised by WordStar mode functions."
576 (interactive)
577 (if ws-last-errormessage
578 (message "%s" ws-last-errormessage)
579 (message "No WordStar error yet.")))
581 (defun ws-kill-eol ()
582 "In WordStar mode: Kill to end of line (like WordStar, not like Emacs)."
583 (interactive)
584 (let ((p (point)))
585 (end-of-line)
586 (kill-region p (point))))
588 (defun ws-kill-bol ()
589 "In WordStar mode: Kill to beginning of line
590 \(like WordStar, not like Emacs)."
591 (interactive)
592 (let ((p (point)))
593 (beginning-of-line)
594 (kill-region (point) p)))
596 (defun kill-complete-line ()
597 "Kill the complete line."
598 (interactive)
599 (beginning-of-line)
600 (if (eobp) (error "End of buffer"))
601 (let ((beg (point)))
602 (forward-line 1)
603 (kill-region beg (point))))
605 (defun ws-repeat-search ()
606 "In WordStar mode: Repeat last search."
607 (interactive)
608 (setq ws-last-cursorposition (point-marker))
609 (if ws-search-string
610 (if ws-search-direction
611 (search-forward ws-search-string)
612 (search-backward ws-search-string))
613 (ws-error "No search to repeat")))
615 (defun ws-query-replace (from to)
616 "In WordStar mode: Search string, remember string for repetition."
617 (interactive "sReplace: \n\
618 sWith: " )
619 (setq ws-search-string from)
620 (setq ws-search-direction t)
621 (setq ws-last-cursorposition (point-marker))
622 (query-replace from to))
624 (defun ws-copy-block ()
625 "In WordStar mode: Copy block to current cursor position."
626 (interactive)
627 (if (and ws-block-begin-marker ws-block-end-marker)
628 (progn
629 (copy-region-as-kill ws-block-begin-marker ws-block-end-marker)
630 (yank)
631 (save-excursion
632 (goto-char (region-beginning))
633 (setq ws-block-begin-marker (point-marker))
634 (goto-char (region-end))
635 (setq ws-block-end-marker (point-marker))))
636 (ws-error (cond (ws-block-begin-marker "Block end marker not set")
637 (ws-block-end-marker "Block begin marker not set")
638 (t "Block markers not set")))))
640 (provide 'ws-mode)
642 ;;; ws-mode.el ends here