]> code.delx.au - gnu-emacs-elpa/blob - packages/quarter-plane/quarter-plane.el
* packages/quarter-plane/quarter-plane.el
[gnu-emacs-elpa] / packages / quarter-plane / quarter-plane.el
1 ;;; quarter-plane.el --- editing using quarter-plane screen model
2
3 ;; Copyright (C) 2011 Free Software Foundation, Inc.
4
5 ;; Author: Peter J. Weisberg
6 ;; Version: 0.1
7 ;; Keywords: convenience wp
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; This code provides a minor mode to enable the some of picture-mode
25 ;; commands documented in the Emacs manual in order to treat the
26 ;; screen as a semi-infinite quarter-plane, without changing the
27 ;; buffer's major mode.
28
29 ;; Known issues:
30
31 ;; Quarter-Plane mode doesn't work in read-only buffers, where it
32 ;; can't insert spaces.
33
34 ;; The user doesn't really care about the "modifications" of adding
35 ;; whitespace that's going to be trimmed when he exits quarter-plane
36 ;; mode or saves, but it's still part of the undo history.
37
38 ;; Both of these are due to the disconnect between what the user
39 ;; really wants--movement of the cursor within the window, regardless
40 ;; of where the text is--and what the mode can actually do--add dummy
41 ;; text to give the cursor a place to move to.
42
43 ;;; Code:
44
45 (require 'picture)
46
47 (defvar quarter-plane-mode-map
48 (let ((map (make-sparse-keymap)))
49 (define-key map [remap right-char] 'picture-forward-column)
50 (define-key map [remap forward-char] 'picture-forward-column)
51 (define-key map [remap previous-line] 'picture-move-up)
52 (define-key map [remap next-line] 'picture-move-down)
53 (define-key map [remap mouse-set-point] 'picture-mouse-set-point)
54 map))
55
56 (defvar quarter-plane-saved-values nil)
57 (make-variable-buffer-local 'quarter-plane-saved-values)
58
59 ;;;###autoload
60 (define-minor-mode quarter-plane-mode
61 "Toggle Quarter-Plane mode on or off.
62 Interactively, with no prefix argument, toggle the mode.
63 With universal prefix ARG turn mode on.
64 With zero or negative ARG turn mode off.
65
66 Use point movement commands that act as if the text extended
67 infinitely down and to the right, inserting spaces as necessary.
68 Excess whitespace is trimmed when saving or exiting Quarter-Plane mode.
69
70 Because it works by inserting spaces, Quarter-Plane mode won't work in
71 read-only buffers.
72
73 \\{quarter-plane-mode-map}"
74 :lighter " Plane"
75 :group 'picture
76 :keymap quarter-plane-mode-map
77 (remove-hook 'before-save-hook 'quarter-plane-delete-whitespace t)
78 (dolist (symval (prog1 quarter-plane-saved-values
79 (setq quarter-plane-saved-values nil)))
80 (set (car symval) (cdr symval)))
81 (when quarter-plane-mode
82 (add-hook 'before-save-hook 'quarter-plane-delete-whitespace nil t)
83 ;; Since quarter-plane-mode is not permanent-local, it should turn itself
84 ;; off cleanly.
85 (add-hook 'change-major-mode-hook (lambda () (quarter-plane-mode -1)) nil t)
86 (dolist (symval '((truncate-lines . t)
87 (show-trailing-whitespace . nil)))
88 (push (cons (car symval) (symbol-value (car symval)))
89 quarter-plane-saved-values)
90 (set (car symval) (cdr symval)))))
91
92 ;;;###autoload
93 (define-global-minor-mode global-quarter-plane-mode quarter-plane-mode
94 quarter-plane-mode
95 :group 'picture)
96
97 (defun quarter-plane-delete-whitespace ()
98 "Call `delete-trailing-whitespace' if the buffer is not read-only."
99 (if (not buffer-read-only)
100 (delete-trailing-whitespace)))
101
102 (add-hook 'quarter-plane-mode-off-hook 'quarter-plane-delete-whitespace)
103
104 (provide 'quarter-plane)
105
106 ;;; quarter-plane.el ends here