Initial Commit
[packages] / xemacs-packages / hyperbole / kotl / kotl-mode.el
1 ;;; kotl-mode.el --- Major mode for editing koutlines and associated commands.
2
3 ;; Copyright (C) 1993, 1994, 1995, 2007  Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
5
6 ;; Author: Bob Weiner, Brown U.
7 ;;         Kellie Clark
8 ;; Maintainer: Mats Lidell <matsl@contactor.se>
9 ;; Keywords: data, hypermedia, outlines, wp
10
11 ;; This file is part of GNU Hyperbole.
12
13 ;; GNU Hyperbole is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 3, or (at
16 ;; your option) any later version.
17
18 ;; GNU Hyperbole is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 ;;;
33 ;;; Other required Lisp Libraries
34 ;;;
35
36 (mapcar 'require '(hsite hmail kview kimport kvspec kotl kmenu))
37
38 ;;;
39 ;;; Public variables
40 ;;;
41
42 (defvar kotl-mode:refill-flag nil
43   "*Automatically refill cells during move, copy, promotion and demotion operations when non-nil.
44 Default value is nil.  Cells with a `no-fill' attribute are never refilled
45 during such operations, regardless of the value of this flag.")
46
47 ;;;
48 ;;; Public functions
49 ;;;
50
51 ;;;###autoload
52 (defun kotl-mode ()
53   "The major mode used to edit and view koutlines.
54 It provides the following keys:
55 \\{kotl-mode-map}"
56   (interactive)
57   (use-local-map kotl-mode-map)
58   (set-syntax-table text-mode-syntax-table)
59   ;; Turn off filladapt minor mode if on, so that it does not interfere with
60   ;; the filling code in "kfill.el".
61   (and (boundp 'filladapt-mode) filladapt-mode (filladapt-mode -1))
62   (if (/= 3 (length (action:params (symbol-function 'fill-paragraph))))
63       ;; Some package such as filladapt has overwritten the primitives
64       ;; defined in kfill.el, so reload it.
65       (load "kfill"))
66   ;; Ensure that outline structure data is saved when save-buffer is called
67   ;; from save-some-buffers, {C-x s}.
68   (add-hook 'local-write-file-hooks 'kotl-mode:update-buffer)
69   (mapcar 'make-local-variable
70           '(kotl-previous-mode indent-line-function indent-region-function
71             minor-mode-alist selective-display-ellipses))
72   ;; Used by kimport.el functions.
73   (if (and (boundp 'kotl-previous-mode) kotl-previous-mode)
74       nil
75     (setq kotl-previous-mode major-mode
76           ;; Remove outline indication due to selective-display.
77           minor-mode-alist (copy-sequence minor-mode-alist)
78           minor-mode-alist (set:remove '(selective-display " Outline")
79                                        minor-mode-alist)
80           minor-mode-alist (set:remove '(selective-display " Otl")
81                                        minor-mode-alist)
82         ;; Remove indication that buffer is ;; narrowed.
83         mode-line-format (copy-sequence mode-line-format)
84         mode-line-format (set:remove "%n" mode-line-format)))
85   ;;
86   (setq indent-line-function 'kotl-mode:indent-line
87         indent-region-function 'kotl-mode:indent-region
88         local-abbrev-table text-mode-abbrev-table
89         selective-display t
90         selective-display-ellipses t
91         paragraph-start "^[ \t]*$\\|^\^L"
92         paragraph-separate "^[ \t]*$\\|^\^L")
93   ;;
94   ;; This major-mode setting must come after the local variable settings but
95   ;; before the koutline is formatted.
96   (setq major-mode 'kotl-mode
97         mode-name "Kotl"
98         indent-tabs-mode nil)
99   ;; If buffer has not yet been formatted for editing, format it.
100   (cond
101    ;; Koutline file that has been loaded and formatted for editing.
102    ((kview:is-p kview)
103     ;; The buffer might have been widened for inspection, so narrow to cells
104     ;; only.
105     (kfile:narrow-to-kcells))
106    ;; Koutline file that has been loaded but not yet formatted for editing.
107    ((kfile:is-p)
108     (kfile:read
109      (current-buffer)
110      (and buffer-file-name (file-exists-p buffer-file-name)))
111     (kvspec:activate))
112    ;; New koutline buffer or a foreign text buffer that must be converted to
113    ;; koutline format.
114    (t
115     (kfile:create (current-buffer))
116     (kvspec:activate)))
117   ;; We have been converting a buffer from a foreign format to a koutline.
118   ;; Now that it is converted, ensure that kotl-previous-mode is set to
119   ;; koutline now.
120   (setq kotl-previous-mode 'kotl-mode)
121   (easy-menu-add kotl-mode-menu kotl-mode-map)
122   (run-hooks 'kotl-mode-hook))
123
124 (defun kotl-mode:find-file-hook ()
125   (if (kview:is-p kview)
126       (kotl-mode:to-valid-position))
127   nil)
128
129 ;;; Ensure that point ends up at a valid position whenever a find-file
130 ;;; is done on a kotl-file.
131 (add-hook 'find-file-hooks 'kotl-mode:find-file-hook)
132
133 ;;; Ensure that outline structure data is hidden from view after a file save.
134 (add-hook 'after-save-hook 'kfile:narrow-to-kcells)
135
136 ;;;
137 ;;; Editing within a single kotl
138 ;;;
139
140 (fset 'kotl-mode:backward-delete-char-untabify
141       'kotl-mode:delete-backward-char)
142 (fset 'kotl-mode:backward-delete-char
143       'kotl-mode:delete-backward-char)
144
145 (defun kotl-mode:backward-kill-word (arg)
146   "Kill up to prefix ARG words preceding point within a single cell."
147   (interactive "*p")
148   (or arg (setq arg 1))
149   (cond ((< arg 0)
150          (if (kotl-mode:eocp)
151              (error "(kotl-mode:backward-kill-word): End of cell")))
152         ((> arg 0)
153          (if (kotl-mode:bocp)
154              (error "(kotl-mode:backward-kill-word): Beginning of cell"))))
155   (if (= arg 0)
156       nil
157     (save-restriction
158       (narrow-to-region (kcell-view:start) (kcell-view:end-contents))
159       (backward-kill-word arg))))
160
161 (defun kotl-mode:center-line ()
162   "Center the line point is on, within the width specified by `fill-column'.
163 This means adjusting the indentation so that it equals the distance between
164 the end of the text and `fill-column'."
165   (interactive "*")
166   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
167   (let ((indent (kcell-view:indent))
168         (opoint (point-marker))
169         (bocp)
170         start)
171     (setq start (kotl-mode:beginning-of-line))
172     (if (setq bocp (kotl-mode:bocp))
173         (progn
174           ;; Add a temporary fill-prefix since this is the 1st line of the cell
175           ;; where label could interfere with centering.
176           (insert "\n\n") (insert-char ?\  indent)))
177     (center-line)
178     (if bocp
179         ;; Delete temporary fill prefix.
180         (delete-region start (+ start indent 2)))
181     (goto-char opoint)
182     ;; Move to editable point if need be.
183     (kotl-mode:to-valid-position)))
184
185 (defun kotl-mode:center-paragraph ()
186   "Center each nonblank line in the paragraph at or after point.
187 See `center-line' for more info."
188   (interactive "*")
189   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
190   (let ((indent (kcell-view:indent))
191         (opoint (point-marker))
192         start)
193     (backward-paragraph)
194     (kotl-mode:to-valid-position)
195     (setq start (point))
196     ;; Add a temporary fill-prefix for 1st line in cell which contains a
197     ;; label, so is centered properly.
198     (insert "\n\n") (insert-char ?\  indent)
199     (kcell-view:operate 'center-paragraph)
200     ;; Delete temporary fill prefix.
201     (delete-region start (+ start indent 2))
202     ;; Return to original point.
203     (goto-char (min opoint (kcell-view:end-contents)))
204     ;; Move to editable point if need be.
205     (kotl-mode:to-valid-position)))
206
207 (defun kotl-mode:copy-region-as-kill (start end)
208   "Copy region between START and END within a single kcell to kill ring."
209   (interactive "r")
210   (kotl-mode:kill-region start end t))
211
212 (defun kotl-mode:copy-to-register (register start end &optional delete-flag)
213   "Copy into REGISTER the region START to END.
214 With optional prefix arg DELETE-FLAG, delete region."
215   (interactive "cCopy to register: \nr\nP")
216   (let ((indent (kcell-view:indent)))
217     (set-register register
218                   (hypb:replace-match-string
219                    (concat "^" (make-string indent ?\ ))
220                    (buffer-substring start end)
221                    "" t)))
222   (if delete-flag (delete-region start end)))
223
224 (defun kotl-mode:delete-backward-char (arg &optional kill-flag)
225   "Delete up to the preceding prefix ARG characters.
226 Return number of characters deleted.
227 Optional KILL-FLAG non-nil means save in kill ring instead of deleting.
228 Does not delete across cell boundaries."
229   (interactive "*P")
230   (if (interactive-p)
231       (if current-prefix-arg
232           (setq kill-flag t
233                 arg (prefix-numeric-value current-prefix-arg))))
234   (or arg (setq arg 1))
235   (kotl-mode:delete-char (- arg) kill-flag))
236
237 (defun kotl-mode:delete-blank-lines ()
238   "On blank line within a cell, delete all surrounding blank lines, leaving just one.
239 On isolated blank line, delete that one.
240 On nonblank line, delete all blank lines that follow it.
241
242 If nothing but whitespace follows point until the end of a cell, delete all
243 whitespace at the end of the cell."
244   (interactive "*")
245   ;; If nothing but whitespace from point until the end of cell, remove all
246   ;; cell trailing whitespace.
247   (let ((end (kcell-view:end-contents))
248         start)
249     (if (save-excursion
250           (skip-chars-forward " \t\n\r" end)
251           (not (kotl-mode:eocp)))
252         (kcell-view:operate (function (lambda () (delete-blank-lines))))
253       (setq start (kcell-view:start))
254       (goto-char end)
255       ;; delete any preceding whitespace
256       (skip-chars-backward " \t\n\r" start)
257       (delete-region (max start (point)) end)))
258   (kotl-mode:to-valid-position))
259
260 (defun kotl-mode:delete-char (arg &optional kill-flag)
261   "Delete up to prefix ARG characters following point.
262 Return number of characters deleted.
263 Optional KILL-FLAG non-nil means save in kill ring instead of deleting.
264 Does not delete across cell boundaries."
265   (interactive "*P")
266   (if (interactive-p)
267       (if current-prefix-arg
268           (setq kill-flag t
269                 arg (prefix-numeric-value current-prefix-arg))))
270   (or arg (setq arg 1))
271   (let ((del-count 0)
272         (indent (kcell-view:indent))
273         count start end)
274     (cond ((> arg 0)
275            (if (kotl-mode:eocp)
276                (error "(kotl-mode:delete-char): End of cell")
277              (setq end (kcell-view:end)
278                    arg (min arg (- end (point))))
279              (while (> arg 0)
280                (if (kotl-mode:eolp)
281                    (if (/= ?\ (char-syntax (following-char)))
282                        (setq arg 0
283                              del-count (1- del-count))
284                      (delete-char 1 kill-flag)
285                      ;; There may be non-whitespace characters in the
286                      ;; indent area.  Don't delete them.
287                      (setq count indent)
288                      (while (and (> count 0)
289                                  (= ?\ (char-syntax (following-char))))
290                        (delete-char 1)
291                        (setq count (1- count))))
292                  (delete-char 1 kill-flag))
293                (setq arg (1- arg)
294                      del-count (1+ del-count)))
295              ))
296           ((< arg 0)
297            (if (kotl-mode:bocp)
298                (error "(kotl-mode:delete-char): Beginning of cell")
299              (setq start (kcell-view:start)
300                    arg (max arg (- start (point))))
301              (while (< arg 0)
302                (if (kotl-mode:bolp)
303                    (if (/= ?\ (char-syntax (preceding-char)))
304                        (setq arg 0
305                              del-count (1- del-count))
306                      ;; There may be non-whitespace characters in the
307                      ;; indent area.  Don't delete them.
308                      (setq count indent)
309                      (while (and (> count 0)
310                                  (= ?\ (char-syntax (preceding-char))))
311                        (delete-char -1)
312                        (setq count (1- count)))
313                      (if (zerop count)
314                          (delete-char -1 kill-flag)))
315                  (delete-char -1 kill-flag))
316                (setq arg (1+ arg)
317                      del-count (1+ del-count))))))
318     del-count))
319
320 (defun kotl-mode:delete-horizontal-space ()
321   "Delete all spaces and tabs around point."
322   (interactive "*")
323   (save-restriction
324     (narrow-to-region
325      (save-excursion
326        (kotl-mode:start-of-line))
327      (save-excursion
328        (kotl-mode:finish-of-line)))
329     (delete-horizontal-space)))
330
331 (defun kotl-mode:delete-indentation (&optional arg)
332   "Join this line to previous and fix up whitespace at join.
333 If there is a fill prefix, delete it from the beginning of this line.
334 With argument, join this line to following line."
335   (interactive "*P")
336   (kcell-view:operate
337    (function
338     (lambda ()
339       (let ((opoint (point)))
340         (beginning-of-line)
341         (if arg (forward-line 1))
342         (if (eq (preceding-char) ?\n)
343             (progn
344               (delete-region (point) (1- (point)))
345               ;; If the second line started with the fill prefix,
346               ;; delete the prefix.
347               (if (and fill-prefix
348                        (<= (+ (point) (length fill-prefix)) (point-max))
349                        (string= fill-prefix
350                                 (buffer-substring
351                                  (point) (+ (point) (length fill-prefix)))))
352                   (delete-region (point) (+ (point) (length fill-prefix))))
353               (fixup-whitespace))
354           (goto-char opoint)))))))
355
356 (defun kotl-mode:fill-cell (&optional justify ignore-collapsed-p)
357   "Fill current cell within current view if it does not have a non-nil `no-fill' attribute.
358 With optional JUSTIFY, justify cell as well.
359 IGNORE-COLLAPSED-P is used when caller has already expanded cell, indicating
360 it is not collapsed."
361   (interactive "*P")
362   (cond ((kcell-view:get-attr 'no-fill)
363          (if (interactive-p)
364              (progn (beep)
365                     (message "Current cell has a `do not fill' attribute.")
366                     nil)))
367         ((string-match "\\`[ \t\n\r]*\\'" (kcell-view:contents))
368           ;; Cell content is all whitespace.
369          nil)
370         (t (let* ((indent (kcell-view:indent))
371                   (opoint (set-marker (make-marker) (point)))
372                   (start  (kcell-view:start))
373                   (collapsed-p)
374                   (end (kcell-view:end-contents))
375                   temp-prefix prev-point)
376              (goto-char start)
377              ;; Expand cell if collapsed so that filling is done properly.
378              (if (and (not ignore-collapsed-p)
379                       (setq collapsed-p (search-forward "\r" end t)))
380                  (subst-char-in-region start end ?\r ?\n t))
381              (goto-char start)
382              ;; Add a temporary fill-prefix for first labeled line, so is
383              ;; filled properly.
384              (insert (setq temp-prefix
385                            (concat "\n\n" (make-string indent ?\ ))))
386              (while (progn (fill-paragraph justify)
387                            (setq prev-point (point))
388                            (forward-paragraph)
389                            (and (/= (point) prev-point)
390                                 (< (point) (kcell-view:end-contents))
391                                 (if (memq (preceding-char) '(?\n ?\r))
392                                     (not (looking-at "[\n\r]"))
393                                   t))))
394              ;; Delete temporary fill prefix.
395              (goto-char start)
396              (if (looking-at temp-prefix)
397                  (replace-match "" t t))
398              ;; Return to original point.
399              (setq end (kcell-view:end-contents))
400              (goto-char (min opoint end))
401              ;;
402              ;; If cell was collapsed before filling, collapse it again.
403              (if collapsed-p
404                  (subst-char-in-region start end ?\n ?\r t))
405              ;;
406              ;; Remove markers
407              (set-marker opoint nil))
408            ;; Move to editable point if need be.
409            (kotl-mode:to-valid-position))))
410
411 (defun kotl-mode:fill-paragraph (&optional justify)
412   "Fill the current paragraph within the cell.
413 With optional JUSTIFY, justify the paragraph as well.
414 Ignore any non-nil no-fill attribute attached to the cell."
415   (interactive "*P")
416   (let ((indent (kcell-view:indent))
417         (opoint (point-marker))
418         start end)
419     (backward-paragraph)
420     (kotl-mode:to-valid-position)
421     (setq start (point-marker))
422     ;; Add a temporary fill-prefix for 1st line in cell which contains a
423     ;; label, so is filled properly.
424     (insert "\n\n") (insert-char ?\  indent)
425     (setq end (point-marker))
426     ;; Return to original paragraph point.  This is the correct formula,
427     ;; considering the fill prefix that was just added.
428     (goto-char (min (max opoint (point)) (kcell-view:end-contents)))
429     (fill-paragraph justify)
430     ;; Delete temporary fill prefix.
431     (delete-region start end)
432     ;; Return to original point.
433     (goto-char (min opoint (kcell-view:end-contents)))
434     ;; Move to editable point if need be.
435     (kotl-mode:to-valid-position)
436     ;; Remove markers
437     (set-marker opoint nil)
438     (set-marker start nil)
439     (set-marker end nil)))
440
441 ;; XEmacs binds this to {M-q}.
442 (fset 'kotl-mode:fill-paragraph-or-region 'kotl-mode:fill-paragraph)
443
444 (defun kotl-mode:fill-tree (&optional top-p)
445   "Refill each cell within the tree whose root is at point.
446 Skip cells with a non-nil no-fill attribute.
447 With optional prefix argument TOP-P non-nil, refill all cells in the outline."
448   (interactive "P")
449   ;; Store list of which cells are presently collapsed.
450   (let ((collapsed-cells
451          (kview:map-tree
452           (function (lambda (view)
453                       ;; Use free variable label-sep-len bound in
454                       ;; kview:map-tree for speed.
455                       (kcell-view:collapsed-p nil label-sep-len)))
456           kview top-p)))
457     ;;
458     ;; Expand all cells in tree.
459     (if top-p
460         (subst-char-in-region (point-min) (point-max) ?\r ?\n t)
461       (save-excursion
462         (kotl-mode:end-of-tree)
463         (subst-char-in-region
464          (point) (kcell-view:end-contents) ?\r ?\n t)))
465     ;;
466     ;; Refill cells without no-fill property.
467     (kview:map-tree (function (lambda (view) (kotl-mode:fill-cell)))
468                     kview top-p)
469     ;;
470     ;; Collapse temporarily expanded cells.
471     (if (delq nil collapsed-cells)
472         (kview:map-tree
473          (function
474           (lambda (view)
475             (if (car collapsed-cells)
476                 ;; Use free variable label-sep-len bound in
477                 ;; kview:map-tree for speed.
478                 (kcell-view:collapse nil label-sep-len))
479             (setq collapsed-cells (cdr collapsed-cells))))
480          kview top-p))))
481
482 (defun kotl-mode:insert-buffer (buffer)
483   "Insert after point the contents of BUFFER.
484 Puts mark after the inserted text.
485 BUFFER may be a buffer or a buffer name."
486   (interactive "*bInsert buffer: ")
487   (insert-buffer buffer)
488   (kotl-mode:add-indent-to-region))
489
490 (defun kotl-mode:insert-file (import-from children-p)
491   "Insert each element in IMPORT-FROM as a separate cell in the current view.
492 Insert as sibling cells following the current cell unless prefix arg,
493 CHILDREN-P is non-nil, then insert as the initial children of the current
494 cell.
495
496 IMPORT-FROM may be a buffer name or file name (file name completion is
497 provided).
498
499 See documentation for `kimport:file' for information on how the type of
500 importation is determined."
501   (interactive
502    (list (read-file-name
503           (if current-prefix-arg
504               "Buffer or file to insert as children of current cell: "
505             "Buffer or file to insert as siblings of current cell: "))
506          current-prefix-arg))
507   (kimport:file import-from (current-buffer) children-p))
508
509 (defun kotl-mode:insert-file-contents (filename)
510   "Insert contents of file FILENAME into current cell after point.
511 Set mark after the inserted text."
512   (interactive "*fInsert file: ")
513   (let ((tem (insert-file-contents filename)))
514     (push-mark (+ (point) (car (cdr tem)))))
515   (kotl-mode:add-indent-to-region))
516
517 (defun kotl-mode:insert-register (register &optional arg)
518   "Insert contents of register REGISTER at point in current cell.
519 REGISTER is a character naming the register to insert.
520 Normally puts point before and mark after the inserted text.
521 If optional second arg is non-nil, puts mark before and point after.
522 Interactively, second arg is non-nil if prefix arg is supplied."
523   (interactive "*cInsert register: \nP")
524   (push-mark)
525   (let ((val (get-register register)))
526     (cond ((consp val)
527            (insert-rectangle val))
528           ((stringp val)
529            (insert val)
530            (kotl-mode:add-indent-to-region))
531           ((integerp val)
532            (princ val (current-buffer)))
533           ((and (markerp val) (marker-position val))
534            (princ (marker-position val) (current-buffer)))
535           (t
536            (error "Register '%c' does not contain text" register))))
537   (if (not arg) (exchange-point-and-mark)))
538
539 (defun kotl-mode:just-one-space ()
540   "Delete all spaces and tabs around point and leave one space."
541   (interactive "*")
542   (save-restriction
543     (narrow-to-region
544      (save-excursion
545        (kotl-mode:start-of-line))
546      (save-excursion
547        (kotl-mode:finish-of-line)))
548     (just-one-space)))
549
550 (defun kotl-mode:kill-line (&optional arg)
551   "Kill ARG lines from point."
552   (interactive "*P")
553   (if (and (null arg)
554            (kotl-mode:bolp)
555            (boundp 'kill-whole-line) kill-whole-line)
556       (let ((indent (kcell-view:indent)))
557         ;; Kill whole line including newline, if any.
558         (kcell-view:operate
559          (function
560           (lambda ()
561             (let ((no-newline))
562               (kill-region (point)
563                            (progn (setq no-newline
564                                         (not (search-forward "\n" nil 'stay)))
565                                   (point)))
566               (or no-newline (delete-char indent)))))))
567     ;; Kill part of a line or multiple lines.
568     (let ((num-arg (prefix-numeric-value arg)))
569       (cond
570        ((and (null arg) (not (kotl-mode:eolp)))
571         ;; kill to eol but not newline
572         (kill-region (point) (setq arg (kotl-mode:finish-of-line))))
573        ((= num-arg 0)
574         ;; kill to bol
575         (kill-region (point) (setq arg (kotl-mode:start-of-line))))
576        (t;; (/= num-arg 0)
577         ;; Find start and end of region to kill
578         (let ((start (point))
579               (end (min (kcell-view:end-contents)
580                         (save-excursion (forward-line num-arg) (point)))))
581           (kotl-mode:kill-region start end))))))
582   (setq last-command 'kill-region))
583
584 (defun kotl-mode:kill-region (start end &optional copy-p)
585   "Kill region between START and END within a single kcell.
586 With optional COPY-P equal to 't, copy region to kill ring but does not
587 kill it.  With COPY-P any other non-nil value, return region as a
588 string without affecting kill ring.
589
590 If the buffer is read-only and COPY-P is nil, the region will not be deleted
591 but it will be copied to the kill ring and then an error will be signaled."
592   (interactive "*r")
593   (let ((read-only (and (not copy-p) buffer-read-only)))
594     (if read-only (setq copy-p t))
595     (if (and (number-or-marker-p start)
596              (number-or-marker-p end)
597              (eq (kcell-view:cell start)
598                  (kcell-view:cell end)))
599         (save-excursion
600           (goto-char start)
601           (let ((indent (kcell-view:indent))
602                 killed subst-str)
603             ;; Convert region to string
604             ;; Convert all occurrences of newline + indent
605             ;; to just newline, eliminating indent.
606             ;; Then save to kill ring.
607             (setq subst-str (concat "\\([\n\r]\\)" (make-string indent ?\ ))
608                   killed
609                   (hypb:replace-match-string
610                    subst-str (buffer-substring start end) "\\1"))
611             (if copy-p
612                 nil
613               ;; If last char of region is a newline, then delete indent in
614               ;; following line.
615               (delete-region
616                start (+ end (if (memq (char-after (1- (max start end)))
617                                       '(?\n ?\r))
618                                 indent
619                               0))))
620             (if (and copy-p (not (eq copy-p t)))
621                 ;; Return killed region as a string.
622                 killed
623               (if (eq last-command 'kill-region)
624                   (kill-append killed (< end start))
625                 (kill-new killed))
626               (setq this-command 'kill-region)
627               (if read-only (barf-if-buffer-read-only))
628               )))
629       (error
630        "(kotl-mode:kill-region): Bad region or not within a single kcell."))))
631
632 (fset 'kotl-mode:kill-ring-save 'kotl-mode:copy-region-as-kill)
633
634 (defun kotl-mode:kill-sentence (&optional arg)
635   "Kill up to prefix ARG (or 1) sentences following point within a single cell."
636   (interactive "*p")
637   (or arg (setq arg 1))
638   (cond ((> arg 0)
639          (if (kotl-mode:eocp)
640              (error "(kotl-mode:kill-sentence): End of cell")))
641         ((< arg 0)
642          (if (kotl-mode:bocp)
643              (error "(kotl-mode:kill-sentence): Beginning of cell"))))
644   (if (= arg 0)
645       nil
646     (kotl-mode:kill-region (point)
647                            (save-excursion
648                              (kotl-mode:forward-sentence arg)))))
649
650 (defun kotl-mode:kill-word (arg)
651   "Kill up to prefix ARG words following point within a single cell."
652   (interactive "*p")
653   (or arg (setq arg 1))
654   (cond ((> arg 0)
655          (if (kotl-mode:eocp)
656              (error "(kotl-mode:kill-word): End of cell")))
657         ((< arg 0)
658          (if (kotl-mode:bocp)
659              (error "(kotl-mode:kill-word): Beginning of cell"))))
660   (if (= arg 0)
661       nil
662     (save-restriction
663       (narrow-to-region (kcell-view:start) (kcell-view:end-contents))
664       (kill-word arg))))
665
666 (defun kotl-mode:newline (arg)
667   "Insert a newline.  With ARG, insert ARG newlines.
668 In Auto Fill mode, if no numeric arg, break the preceding line if it is
669 too long."
670   (interactive "*p")
671   (let ((indent (kcell-view:indent)))
672     (if (equal arg 1)
673         (progn
674           (save-excursion
675             (insert ?\n)
676             (insert-char ?\  indent))
677           (do-auto-fill)
678           (forward-line 1)
679           (kotl-mode:start-of-line)
680           )
681       (while (> arg 0)
682         (insert ?\n)
683         (insert-char ?\  indent)
684         (setq arg (1- arg))))))
685
686 (fset 'kotl-mode:newline-and-indent 'kotl-mode:newline)
687
688 (defun kotl-mode:open-line (arg)
689   "Insert a newline and leave point before it.
690 With arg N, insert N newlines."
691   (interactive "*p")
692   (let* ((bolp (and (kotl-mode:bolp) (not (kotl-mode:bocp))))
693          (indent (kcell-view:indent)))
694     (while (> arg 0)
695       (save-excursion
696         (insert ?\n)
697         (if (and (not bolp) fill-prefix)
698             (insert fill-prefix)
699           (insert-char ?\  indent)))
700       (setq arg (1- arg)))
701     (if (and bolp fill-prefix)
702         (progn (delete-horizontal-space)
703                (insert fill-prefix)))
704     ))
705
706 (defun kotl-mode:set-fill-prefix (turn-off)
707   "Sets fill prefix to line up to point.
708 With prefix arg TURN-OFF or at begin of line, turns fill prefix off."
709   (interactive "P")
710   (set-fill-prefix (or turn-off (kotl-mode:bolp))))
711
712 (defun kotl-mode:transpose-chars (arg)
713   "Interchange characters around point, moving forward one character.
714 With prefix ARG, take character before point and drag it forward past ARG
715 other characters (backward if ARG negative).
716 If no prefix ARG and at end of line, the previous two characters are
717 exchanged."
718   (interactive "*P")
719   (and (null arg) (kotl-mode:eolp) (kotl-mode:forward-char -1))
720   (transpose-subr 'kotl-mode:forward-char (prefix-numeric-value arg)))
721
722 (defun kotl-mode:transpose-lines (arg)
723   "Exchange current line and previous line, leaving point after both.
724 If no previous line, exchange current with next line.
725 With prefix ARG, take previous line and move it past ARG lines.
726 With prefix ARG = 0, interchange the line that contains point with the line
727 that contains mark."
728   (interactive "*p")
729   (cond
730    ((and (kotl-mode:first-line-p) (kotl-mode:last-line-p))
731     (error "(kotl-mode:transpose-lines): Only one line in outline"))
732    ;;
733    ;; Transpose current and previous lines or current and next lines, if no
734    ;; previous line.  Leave point after both exchanged lines.
735    ((= arg 1)
736     (let* ((point (point-marker))
737            (mark (set-marker (make-marker)
738                              (if (kotl-mode:first-line-p)
739                                  (kotl-mode:next-line 1)
740                                (kotl-mode:previous-line 1)))))
741       (kotl-mode:transpose-lines-internal point mark)
742       (goto-char (max point mark))
743       (kotl-mode:next-line 1)
744       (set-marker mark nil)))
745    ;;
746    ;; Transpose point and mark lines, leaving point on the line of text that
747    ;; originally contained point.
748    ((= arg 0)
749     (kotl-mode:transpose-lines-internal (point-marker) (hypb:mark-marker t))
750     ;; This is like exchange-point-and-mark, but doesn't activate the
751     ;; mark.
752     (goto-char (prog1 (hypb:mark t)
753                  (set-marker (hypb:mark-marker t) (point)))))
754    ;;
755    ;; Move previous line past ARG next lines and leave point after previous
756    ;; line text.
757    (t
758     (if (kotl-mode:first-line-p)
759         (error "(kotl-mode:transpose-lines): No previous line to transpose"))
760     (kotl-mode:previous-line 1)
761     (let* ((mark (set-marker (make-marker)
762                              (save-excursion (kotl-mode:next-line arg))))
763            (line-to-move (kotl-mode:delete-line)))
764       (condition-case ()
765           ;; Delete trailing newline if any, ignoring error.
766           (kotl-mode:delete-char 1)
767         (error nil))
768       (goto-char mark)
769       (set-marker mark nil)
770       (kotl-mode:finish-of-line)
771       (insert "\n")
772       (insert-char ?\  (kcell-view:indent))
773       (insert line-to-move)
774       (kotl-mode:start-of-line)))))
775
776 (defun kotl-mode:transpose-paragraphs (arg)
777   "Interchange this (or next) paragraph with previous one."
778   (interactive "*p")
779   (transpose-subr 'kotl-mode:forward-paragraph (prefix-numeric-value arg)))
780
781 (defun kotl-mode:transpose-sentences (arg)
782   "Interchange this (next) and previous sentence."
783   (interactive "*p")
784   (transpose-subr 'kotl-mode:forward-sentence (prefix-numeric-value arg)))
785
786 (defun kotl-mode:transpose-words (arg)
787   "Interchange words around point, leaving point after both words.
788 With prefix ARG, take word before or around point and drag it forward past
789 ARG other words (backward if ARG negative).  If ARG is zero, the words around
790 or after point and around or after mark are interchanged."
791   (interactive "*p")
792   (transpose-subr 'kotl-mode:forward-word (prefix-numeric-value arg)))
793
794 (defun kotl-mode:zap-to-char (arg char)
795   "Kill up to and including prefix ARG'th occurrence of CHAR.
796 Goes backward if ARG is negative; error if CHAR not found."
797   (interactive "*p\ncZap to char within current cell: ")
798   (kcell-view:operate 
799    (function (lambda () (zap-to-char arg char)))))
800
801 ;;;
802 ;;; Editing across kotls
803 ;;;
804
805 (defun kotl-mode:append-cell (contents-cell append-to-cell)
806   "Append CONTENTS-CELL to APPEND-TO-CELL.
807 APPEND-TO-CELL is refilled if neither cell has a no-fill property and
808 kotl-mode:refill-flag is enabled."
809   (interactive
810    (let* ((label (kcell-view:label))
811           (hargs:defaults (list label label)))
812      (hargs:iform-read
813       '(interactive
814         "*+KAppend contents of cell: \n+KAppend contents of cell <%s> to cell: "))))
815   (save-excursion
816     (kotl-mode:goto-cell contents-cell)
817     (let ((contents (kcell-view:contents))
818           (no-fill (kcell-view:get-attr 'no-fill)))
819       (kotl-mode:goto-cell append-to-cell)
820       (if no-fill nil (setq no-fill (kcell-view:get-attr 'no-fill)))
821       (goto-char (kcell-view:end-contents))
822       (let ((fill-prefix (make-string (kcell-view:indent) ?\ )))
823         (if (kotl-mode:bolp)
824             nil
825           ;; Append contents of cell beginning on its own line.
826           (insert "\n" fill-prefix))
827         (kview:insert-contents (kcell-view:cell) contents
828                                (or no-fill (null kotl-mode:refill-flag))
829                                fill-prefix)))))
830
831 (defun kotl-mode:copy-after (from-cell-ref to-cell-ref child-p)
832   "Copy tree rooted at FROM-CELL-REF to follow tree rooted at TO-CELL-REF.
833 If prefix arg CHILD-P is non-nil, make FROM-CELL-REF the first child of
834 TO-CELL-REF, otherwise make it the sibling following TO-CELL-REF.
835
836 Leave point at the start of the root cell of the new tree."
837   (interactive
838    (let* ((label (kcell-view:label))
839           (hargs:defaults (list label label)))
840      (append
841       (hargs:iform-read
842        (list
843         'interactive
844         (format "*+KCopy tree: \n+KCopy tree <%%s> to follow as %s of cell: "
845                 (if current-prefix-arg "child" "sibling"))))
846       (list current-prefix-arg))))
847   ;;
848   ;; Copy tree in current view and leave point at the start of the copy.
849   (goto-char (kotl-mode:move-after from-cell-ref to-cell-ref child-p t))
850   ;; Alter the copied tree so each cell appears to be newly created.
851   (kview:map-tree
852    (function
853     (lambda (view)
854       (kcell-view:set-cell
855        (kcell:create nil (kview:id-increment view)))))
856    kview))
857
858 (defun kotl-mode:copy-before (from-cell-ref to-cell-ref parent-p)
859   "Copy tree rooted at FROM-CELL-REF to precede tree rooted at TO-CELL-REF.
860 If prefix arg PARENT-P is non-nil, make FROM-CELL-REF the first child of
861 TO-CELL-REF's parent, otherwise make it the preceding sibling of TO-CELL-REF.
862
863 Leave point at the start of the root cell of the new tree."
864   (interactive
865    (let* ((label (kcell-view:label))
866           (hargs:defaults (list label label)))
867      (append
868       (hargs:iform-read
869        (list 'interactive
870              (format "*+KCopy tree: \n+KCopy tree <%%s> to be %s of cell: "
871                      (if current-prefix-arg "first child of parent"
872                        "preceding sibling"))))
873       (list current-prefix-arg))))
874   ;;
875   ;; Copy tree in current view and leave point at the start of the copy.
876   (goto-char (kotl-mode:move-before from-cell-ref to-cell-ref parent-p t))
877   ;; Alter the copied tree so each cell appears to be newly created.
878   (kview:map-tree
879    (function
880     (lambda (view)
881       (kcell-view:set-cell
882        (kcell:create nil (kview:id-increment view)))))
883    kview))
884
885 (defun kotl-mode:copy-to-buffer (cell-ref buffer invisible-flag)
886   "Copy outline tree rooted at CELL-REF to a non-koutline BUFFER.
887 Invisible text is expanded and included in the copy only if INVISIBLE-FLAG is
888 non-nil.  The tree is inserted before point in BUFFER.  Use \"0\" to copy the
889 whole outline buffer."
890   (interactive
891    (let ((label-default (kcell-view:label)))
892      (hargs:iform-read
893       '(interactive
894         (list
895          (hargs:read "Copy tree without attributes: (0 for whole outline) "
896                      nil label-default nil 'kcell)
897          (read-buffer "To buffer: "
898                       (save-window-excursion
899                         (if (one-window-p)
900                             (select-frame (next-frame))
901                           (other-window 1))
902                         (buffer-name))
903                       t)
904          (y-or-n-p "Copy invisible text? "))))))
905   (message "") ;; Erase last interactive prompt, if any.
906   (setq buffer (get-buffer-create buffer))
907   (if (equal cell-ref "0")
908       (hypb:insert-region buffer (point-min) (point-max) invisible-flag)
909     (let (start end)
910       (save-excursion
911         (kotl-mode:goto-cell cell-ref t)
912         (save-excursion (beginning-of-line) (setq start (point)))
913         (setq end (kotl-mode:tree-end)))
914       (hypb:insert-region buffer start end invisible-flag))))
915
916 (defun kotl-mode:move-after (from-cell-ref to-cell-ref child-p
917                              &optional copy-p fill-p)
918   "Move tree rooted at FROM-CELL-REF to follow tree rooted at TO-CELL-REF.
919 If prefix arg CHILD-P is non-nil, make FROM-CELL-REF the first child of
920 TO-CELL-REF, otherwise make it the sibling following TO-CELL-REF.
921 With optional COPY-P, copies tree rather than moving it.
922
923 Leave point at original location but return the tree's new start point."
924   (interactive
925    (let* ((label (kcell-view:label))
926           (hargs:defaults (list label label)))
927      (append
928       (hargs:iform-read
929        (list
930         'interactive
931         (format "*+KMove tree: \n+KMove tree <%%s> to follow as %s of cell: "
932                 (if current-prefix-arg "child" "sibling"))))
933       (list current-prefix-arg))))
934   (if (and (not copy-p) (equal from-cell-ref to-cell-ref))
935       (error "(kotl-mode:move-after): Can't move tree after itself"))
936   (let* ((orig (set-marker (make-marker) (point)))
937          (label-sep-len (kview:label-separator-length kview))
938          (move-to-point (set-marker
939                          (make-marker)
940                          (kotl-mode:goto-cell to-cell-ref t)))
941          (to-label (kcell-view:label))
942          (to-indent (kcell-view:indent nil label-sep-len))
943          (from-label (progn (kotl-mode:goto-cell from-cell-ref t)
944                             (kcell-view:label)))
945          (from-indent (kcell-view:indent nil label-sep-len))
946          (start (kotl-mode:tree-start))
947          (end   (kotl-mode:tree-end))
948          (sib-id (if (= 0 (kotl-mode:forward-cell 1))
949                      (kcell-view:idstamp)))
950          new-tree-start)
951     ;;
952     ;; We can't move a tree to a point within itself, so if that is the case
953     ;; and this is not a copy operation, signal an error.
954     (if (and (not copy-p) (>= move-to-point start) (<= move-to-point end))
955         (error "(kotl-mode:move-after): Can't move tree <%s> to within itself"
956                from-label))
957     ;;
958     ;; If tree to move has a sibling, point is now at the start of the
959     ;; sibling cell.  Mark its label with a property which will be deleted
960     ;; whenever the cell label is renumbered.  This tells us whether or not
961     ;; to renumber the sibling separately from the tree to move.
962     (if sib-id
963         ;; Move to middle of label and insert klabel-original temp property.
964         (progn (goto-char (- (point) label-sep-len 3))
965                (kproperty:set 'klabel-original t)))
966     ;;
967     ;; Position for insertion before deletion of tree-to-move from old
968     ;; position, in case old position precedes new one.
969     ;; Skip past either cell or tree at move-to-point.
970     (goto-char move-to-point)
971     (if child-p
972         ;; Move to insert position for first child of to-cell-ref.
973         (progn (goto-char (kcell-view:end))
974                (setq to-label (klabel:child to-label)
975                      to-indent (+ to-indent (kview:level-indent kview))))
976       ;; Move to after to-cell-ref's tree for insertion as following sibling.
977       (goto-char (kotl-mode:tree-end))
978       (setq to-label (klabel:increment to-label)))
979     ;;
980     ;; Insert tree-to-move at new location
981     ;;
982     (kview:move start end (point) from-indent to-indent copy-p
983                 (or fill-p kotl-mode:refill-flag))
984     ;;
985     ;; Ensure that point is within editable region of cell with to-label.
986     (kotl-mode:to-valid-position)
987     (setq new-tree-start (point))
988     ;;
989     ;; Update current cell and new siblings' labels within view.
990     (klabel-type:update-labels to-label)
991     ;;
992     (if copy-p
993         nil
994       ;;
995       ;; Move to sibling of tree-to-move within view and update labels within
996       ;; view of tree-to-move's original siblings.
997       (if sib-id
998           (progn (kotl-mode:goto-cell sib-id t)
999                  ;; Sibling labels may have already been updated if tree was
1000                  ;; moved somewhere preceding its siblings.
1001                  (let ((label-middle (- (point) label-sep-len 2)))
1002                    (if (kproperty:get label-middle 'klabel-original)
1003                        (klabel-type:update-labels from-label))))))
1004     ;;
1005     (goto-char orig)
1006     ;;
1007     ;; Ensure that point is within editable region of a cell.
1008     (kotl-mode:to-valid-position)
1009     ;;
1010     (set-marker orig nil)
1011     (set-marker move-to-point nil)
1012     new-tree-start))
1013
1014 (defun kotl-mode:move-before (from-cell-ref to-cell-ref parent-p
1015                               &optional copy-p fill-p)
1016   "Move tree rooted at FROM-CELL-REF to precede tree rooted at TO-CELL-REF.
1017 If prefix arg PARENT-P is non-nil, make FROM-CELL-REF the first child of
1018 TO-CELL-REF's parent, otherwise make it the preceding sibling of TO-CELL-REF.
1019 With optional COPY-P, copies tree rather than moving it.
1020
1021 Leave point at original location but return the tree's new start point."
1022   (interactive
1023    (let* ((label (kcell-view:label))
1024           (hargs:defaults (list label label)))
1025      (append
1026       (hargs:iform-read
1027        (list 'interactive
1028              (format "*+KMove tree: \n+KMove tree <%%s> to be %s of cell: "
1029                      (if current-prefix-arg "first child of parent"
1030                        "preceding sibling"))))
1031       (list current-prefix-arg))))
1032   (if (and (not copy-p) (equal from-cell-ref to-cell-ref))
1033       (error "(kotl-mode:move-before): Can't move tree before itself"))
1034   (let* ((orig (set-marker (make-marker) (point)))
1035          (label-sep-len (kview:label-separator-length kview))
1036          (move-to-point (set-marker
1037                          (make-marker)
1038                          (kotl-mode:goto-cell to-cell-ref t)))
1039          (to-label (kcell-view:label))
1040          (to-indent (kcell-view:indent nil label-sep-len))
1041          (from-label (progn (kotl-mode:goto-cell from-cell-ref t)
1042                             (kcell-view:label)))
1043          (from-indent (kcell-view:indent nil label-sep-len))
1044          (start (kotl-mode:tree-start))
1045          (end   (kotl-mode:tree-end))
1046          (sib-id (if (= 0 (kotl-mode:forward-cell 1))
1047                      (kcell-view:idstamp)))
1048          new-tree-start)
1049     ;;
1050     ;; We can't move a tree to a point within itself, so if that is the case
1051     ;; and this is not a copy operation, signal an error.
1052     (if (and (not copy-p) (>= move-to-point start) (<= move-to-point end))
1053         (error "(kotl-mode:move-before): Can't move tree <%s> to within itself"
1054                from-label))
1055     ;;
1056     ;; If tree to move has a sibling, point is now at the start of the
1057     ;; sibling cell.  Mark its label with a property which will be deleted
1058     ;; whenever the cell label is renumbered.  This tells us whether or not
1059     ;; to renumber the sibling separately from the tree to move.
1060     (if sib-id
1061         ;; Move to middle of label and insert klabel-original temp property.
1062         (progn (goto-char (- (point) label-sep-len 3))
1063                (kproperty:set 'klabel-original t)))
1064     ;;
1065     ;; Position for insertion at succeeding-tree, before deletion of
1066     ;; tree-to-move from old position, in case old position precedes new one.
1067     (goto-char move-to-point)
1068     (if parent-p
1069         ;; Move to insert position for first child of to-cell-ref's parent.
1070         (if (kcell-view:parent nil label-sep-len)
1071             (progn (setq to-label (klabel:child (kcell-view:label)))
1072                    (goto-char (kcell-view:end)))
1073           (error "(kotl-mode:move-before): to-cell-ref's parent not in current view"))
1074       ;; Move to before to-cell-ref for insertion as preceding sibling.
1075       (goto-char (kotl-mode:tree-start)))
1076     ;;
1077     ;; Insert tree-to-move at new location
1078     ;;
1079     (kview:move start end (point) from-indent to-indent copy-p
1080                 (or fill-p kotl-mode:refill-flag))
1081     ;;
1082     ;; Ensure that point is within editable region of root of tree just moved.
1083     (kotl-mode:to-valid-position)
1084     (setq new-tree-start (point))
1085     ;;
1086     ;; Update current cell and new siblings' labels within view.
1087     (klabel-type:update-labels to-label)
1088     ;;
1089     (if copy-p
1090         nil
1091       ;;
1092       ;; Move to sibling of tree-to-move within view and update labels within
1093       ;; view of tree-to-move's original siblings.
1094       (if sib-id
1095           (progn
1096             (kotl-mode:goto-cell sib-id t)
1097             ;; Sibling labels may have already been updated if tree was
1098             ;; moved somewhere preceding its siblings.
1099             (let ((label-middle (- (point) label-sep-len 2)))
1100               (if (kproperty:get label-middle 'klabel-original)
1101                   (klabel-type:update-labels from-label))))))
1102     ;;
1103     (goto-char orig)
1104     ;;
1105     ;; Ensure that point is within editable region of a cell.
1106     (kotl-mode:to-valid-position)
1107     ;;
1108     (set-marker orig nil)
1109     (set-marker move-to-point nil)
1110     new-tree-start))
1111
1112 (defun kotl-mode:yank (&optional arg)
1113   "Reinsert the last stretch of killed text.
1114 More precisely, reinsert the stretch of killed text most recently
1115 killed OR yanked.  Put point at end, and set mark at beginning.
1116 With just C-u as argument, same but put point at beginning (and mark at end).
1117 With argument N, reinsert the Nth most recently killed stretch of killed
1118 text.
1119 See also the command \\[kotl-mode:yank-pop]."
1120   (interactive "*P")
1121   (push-mark (point))
1122   (let* ((yank-text (current-kill (cond
1123                                    ((listp arg) 0)
1124                                    ((eq arg '-) -1)
1125                                    (t (1- arg)))))
1126          (indent (kcell-view:indent))
1127          (indent-str (make-string indent ?\ )))
1128     ;; Convert all occurrences of newline to newline + cell indent.
1129     ;; Then insert into buffer.
1130     (insert (hypb:replace-match-string
1131              "[\n\r]" yank-text (concat "\\0" indent-str))))
1132   (if (consp arg)
1133       ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1134       ;; It is cleaner to avoid activation, even though the command
1135       ;; loop would deactivate the mark because we inserted text.
1136       (goto-char (prog1 (mark t)
1137                    (set-marker (hypb:mark-marker t) (point)))))
1138   nil)
1139
1140 (defun kotl-mode:yank-pop (arg)
1141   "Replace just-yanked stretch of killed text with a different stretch.
1142 This command is allowed only immediately after a `yank' or a `yank-pop'.
1143 At such a time, the region contains a stretch of reinserted
1144 previously-killed text.  `yank-pop' deletes that text and inserts in its
1145 place a different stretch of killed text.
1146
1147 With no argument, the previous kill is inserted.
1148 With argument N, insert the Nth previous kill.
1149 If N is negative, this is a more recent kill.
1150
1151 The sequence of kills wraps around, so that after the oldest one
1152 comes the newest one."
1153   (interactive "*p")
1154   (if (not (eq last-command 'kotl-mode:yank))
1155       (error "Previous command was not a yank"))
1156   (setq this-command 'kotl-mode:yank)
1157   (let ((before (< (point) (mark t))))
1158     (delete-region (point) (mark t))
1159     (set-marker (hypb:mark-marker t) (point) (current-buffer))
1160     (let* ((yank-text (current-kill arg))
1161            (indent (kcell-view:indent))
1162            (indent-str (make-string indent ?\ )))
1163       ;; Convert all occurrences of newline to newline + cell indent.
1164       ;; Then insert into buffer.
1165       (insert (hypb:replace-match-string
1166                "[\n\r]" yank-text (concat "\\0" indent-str))))
1167     (if before
1168         ;; This is like exchange-point-and-mark, but doesn't activate the mark.
1169         ;; It is cleaner to avoid activation, even though the command
1170         ;; loop would deactivate the mark because we inserted text.
1171         (goto-char (prog1 (mark t)
1172                      (set-marker (hypb:mark-marker t) (point) (current-buffer))))))
1173   nil)
1174
1175 ;;;
1176 ;;; Movement
1177 ;;;
1178
1179 ;;; Cursor and keypad key functions aliases for XEmacs.
1180 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1181     nil
1182   (fset 'kotl-mode:fkey-backward-char 'kotl-mode:backward-char)
1183   (fset 'kotl-mode:fkey-forward-char  'kotl-mode:forward-char)
1184   (fset 'kotl-mode:fkey-next-line     'kotl-mode:next-line)
1185   (fset 'kotl-mode:fkey-previous-line 'kotl-mode:previous-line)
1186   (fset 'kotl-mode:deprecated-scroll-down 'kotl-mode:scroll-down)
1187   (fset 'kotl-mode:deprecated-scroll-up 'kotl-mode:scroll-up)
1188   (fset 'kotl-mode:deprecated-bob     'kotl-mode:beginning-of-buffer)
1189   (fset 'kotl-mode:deprecated-eob     'kotl-mode:end-of-buffer))
1190
1191 (defun kotl-mode:back-to-indentation ()
1192   "Move point to the first non-read-only non-whitespace character on this line."
1193   (interactive)
1194   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1195   (back-to-indentation)
1196   (kotl-mode:to-valid-position))
1197
1198 (defun kotl-mode:backward-cell (arg)
1199   "Move to prefix ARGth prior visible cell (same level) within current view.
1200 Return number of cells left to move."
1201   (interactive "p")
1202   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1203   (if (< arg 0)
1204       (kotl-mode:forward-cell (- arg))
1205     (let ((prior (= arg 0))
1206           (label-sep-len (kview:label-separator-length kview)))
1207       (while (and (> arg 0) (setq prior (kcell-view:backward t label-sep-len)))
1208         (setq arg (1- arg)))
1209       (if (or prior (not (interactive-p)))
1210           arg
1211         (error "(kotl-mode:backward-cell): No prior cell at same level")))))
1212
1213 (defun kotl-mode:backward-char (&optional arg)
1214   "Move point backward ARG (or 1) characters and return point."
1215   (interactive "p")
1216   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1217   (or arg (setq arg 1))
1218   (if (>= arg 0)
1219       (while (> arg 0)
1220         (cond ((kotl-mode:bobp)
1221                (error "(kotl-mode:backward-char): Beginning of buffer"))
1222               ((kotl-mode:bocp)
1223                (if (kcell-view:previous)
1224                    (kotl-mode:end-of-cell)))
1225               ((kotl-mode:bolp)
1226                (if (re-search-backward "[\n\r]" nil t)
1227                    (kotl-mode:to-valid-position t)))
1228               (t (backward-char)))
1229         (setq arg (1- arg)))
1230     (kotl-mode:forward-char (- arg)))
1231   (point))
1232
1233 (defun kotl-mode:backward-paragraph (&optional arg)
1234   "Move backward to start of paragraph.
1235 With arg N, do it N times; negative arg -N means move forward N paragraphs.
1236 Return point.
1237
1238 A paragraph start is the beginning of a line which is a
1239 `first-line-of-paragraph' or which is ordinary text and follows a
1240 paragraph-separating line.
1241
1242 See `forward-paragraph' for more information."
1243   (interactive "p")
1244   (setq arg (prefix-numeric-value arg)
1245         zmacs-region-stays t);; Maintain region highlight for XEmacs.
1246   (kotl-mode:forward-paragraph (- arg)))
1247
1248 (fset 'kotl-mode:backward-para 'kotl-mode:backward-paragraph)
1249
1250 (defun kotl-mode:backward-sentence (&optional arg)
1251   "Move point backward ARG (or 1) sentences and return point."
1252   (interactive "p")
1253   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1254   (let* ((label-sep-len (kview:label-separator-length kview))
1255          ;; Setting fill prefix makes sentence commands properly recognize
1256          ;; indented paragraphs.
1257          (fill-prefix (make-string (kcell-view:indent nil label-sep-len) ?\ )))
1258     (if (kotl-mode:bobp)
1259         (error "(kotl-mode:backward-sentence): First sentence")
1260       (if (and (kotl-mode:bocp) (kcell-view:previous nil label-sep-len))
1261           (goto-char (kcell-view:end-contents)))
1262       (or arg (setq arg 1))
1263       (save-restriction
1264         (if (= arg 1)
1265             (narrow-to-region
1266              (- (kcell-view:start nil label-sep-len)
1267                 (kcell-view:indent nil label-sep-len))
1268              (kcell-view:end-contents)))
1269         (unwind-protect
1270             (let ((opoint (point)))
1271               (backward-sentence arg)
1272               (if (= opoint (point))
1273                   (progn (kcell-view:previous nil label-sep-len)
1274                          (backward-sentence arg))))
1275           (kotl-mode:to-valid-position t)))))
1276   (point))
1277
1278 (defun kotl-mode:backward-word (&optional arg)
1279   "Move point backward ARG (or 1) words and return point."
1280   (interactive "p")
1281   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1282   (or arg (setq arg 1))
1283   (if (>= arg 0)
1284       (while (> arg 0)
1285         (cond ((kotl-mode:bobp) (setq arg 0))
1286               ((kotl-mode:bocp)
1287                (beginning-of-line)
1288                (kotl-mode:to-valid-position t)))
1289         (unwind-protect
1290             (backward-word 1)
1291           (kotl-mode:to-valid-position t))
1292         (setq arg (1- arg)))
1293     (kotl-mode:forward-word (- arg)))
1294   (point))
1295
1296 (defun kotl-mode:beginning-of-buffer ()
1297   "Move point to beginning of buffer and return point."
1298   (interactive)
1299   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1300   (goto-char (point-min))
1301   ;; To move to cell start.
1302   (goto-char (kcell-view:start)))
1303
1304 (defun kotl-mode:beginning-of-cell (&optional arg)
1305   "Move point to beginning of current or ARGth - 1 prior cell and return point."
1306   (interactive "p")
1307   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1308   (or arg (setq arg 1))
1309   (or (integer-or-marker-p arg)
1310       (error "(kotl-mode:beginning-of-cell): Wrong type arg, integer-or-marker, '%s'" arg))
1311   (if (= arg 1)
1312       (goto-char (kcell-view:start))
1313     (kotl-mode:backward-cell (1- arg)))
1314   (point))
1315
1316 ;;; Avoid XEmacs byte-compiler bug which inserts nil for calls to this
1317 ;;; function if named kotl-mode:beginning-of-line.
1318 ;;;
1319 (defun kotl-mode:start-of-line (&optional arg)
1320   "Move point to beginning of current or ARGth - 1 line and return point."
1321   (interactive "p")
1322   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1323   (or arg (setq arg 1))
1324   (or (integer-or-marker-p arg)
1325       (error "(kotl-mode:start-of-line): Wrong type arg, integer-or-marker, '%s'" arg))
1326   (forward-line (1- arg))
1327   (if (eolp)
1328       nil
1329     (forward-char (prog1 (kcell-view:indent)
1330                     (beginning-of-line))))
1331   (point))
1332
1333 (defalias 'kotl-mode:beginning-of-line 'kotl-mode:start-of-line)
1334
1335 (defun kotl-mode:beginning-of-tree ()
1336   "Move point to the level 1 root of the current cell's tree.
1337 Leave point at the start of the cell."
1338   (interactive)
1339   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1340   (let ((label-sep-len (kview:label-separator-length kview)))
1341     (if (/= (kcell-view:level nil label-sep-len) 1)
1342         ;; Enable user to return to this previous position if desired.
1343         (push-mark nil 'no-msg))
1344     (while (and (/= (kcell-view:level nil label-sep-len) 1)
1345                 (kcell-view:parent nil label-sep-len)))
1346     (kotl-mode:beginning-of-cell)))
1347
1348 (defun kotl-mode:down-level (arg)
1349   "Move down prefix ARG levels lower within current tree."
1350   (interactive "p")
1351   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1352   (if (< arg 0)
1353       (kotl-mode:up-level (- arg))
1354     ;; Enable user to return to this previous position if desired.
1355     (push-mark nil 'no-msg)
1356     (let ((child))
1357       (while (and (> arg 0) (kcell-view:child))
1358         (or child (setq child t))
1359         (setq arg (1- arg)))
1360       ;; Signal an error if couldn't move down at least 1 child level.
1361       (or child
1362           (progn
1363             (goto-char (hypb:mark t))
1364             (pop-mark)
1365             (error "(kotl-mode:down-level): No child level to which to move")
1366             )))))
1367
1368 (defun kotl-mode:end-of-buffer ()
1369   "Move point to end of buffer and return point."
1370   (interactive)
1371   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1372   (goto-char (point-max))
1373   ;; To move to cell end.
1374   (kotl-mode:to-valid-position t)
1375   (point))
1376
1377 (defun kotl-mode:end-of-cell (&optional arg)
1378   "Move point to end of current or ARGth - 1 succeeding cell and return point."
1379   (interactive "p")
1380   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1381   (or arg (setq arg 1))
1382   (or (integer-or-marker-p arg)
1383       (error "(kotl-mode:end-of-cell): Wrong type arg, integer-or-marker, '%s'" arg))
1384   (if (= arg 1)
1385       (goto-char (kcell-view:end-contents))
1386     (kotl-mode:forward-cell (1- arg)))
1387   (point))
1388
1389 ;;; Avoid XEmacs byte-compiler bug which inserts nil for calls to this
1390 ;;; function if named kotl-mode:end-of-line.
1391 ;;;
1392 (defun kotl-mode:finish-of-line (&optional arg)
1393   "Move point to end of current or ARGth - 1 line and return point."
1394   (interactive "p")
1395   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1396   (or arg (setq arg 1))
1397   (or (integer-or-marker-p arg)
1398       (error "(kotl-mode:finish-of-line): Wrong type arg, integer-or-marker, '%s'" arg))
1399   (forward-line (1- arg))
1400   (end-of-line)
1401   ;; May have to move backwards to before label if support labels
1402   ;; at end of cells.
1403   (point))
1404
1405 (defalias 'kotl-mode:end-of-line 'kotl-mode:finish-of-line)
1406
1407 (defun kotl-mode:end-of-tree ()
1408   "Move point to the last cell in tree rooted at the current cell.
1409 Leave point at the start of the cell."
1410   (interactive)
1411   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1412   ;; Enable user to return to this previous position if desired.
1413   (push-mark nil 'no-msg)
1414   (let ((label-sep-len (kview:label-separator-length kview)))
1415     (if (kcell-view:forward nil label-sep-len)
1416         ;; Move to cell preceding start of next tree.
1417         (kcell-view:previous nil label-sep-len)
1418       ;; Otherwise, no next tree, so move until find last cell in tree.
1419       (let ((cell-indent (kcell-view:indent nil label-sep-len))
1420             (end-point (point)))
1421         ;; Terminate when no further cells or when reach a cell at an equal
1422         ;; or higher level in the outline than the first cell that we
1423         ;; processed.
1424         (while (and (kcell-view:next nil label-sep-len)
1425                     (> (kcell-view:indent nil label-sep-len) cell-indent))
1426           (setq end-point (point)))
1427         (goto-char end-point)))
1428     (kotl-mode:beginning-of-cell)))
1429
1430 (defun kotl-mode:first-sibling ()
1431   "Move point to the first sibling of the present cell.
1432 Leave point at the start of the cell or at its present position if it is
1433 already within the first sibling cell."
1434   (interactive)
1435   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1436   (let ((label-sep-len (kview:label-separator-length kview)))
1437     (if (save-excursion (kcell-view:backward nil label-sep-len))
1438         ;; Enable user to return to this previous position if desired.
1439         (push-mark nil 'no-msg))
1440     (while (kcell-view:backward nil label-sep-len))))
1441
1442 (defun kotl-mode:forward-cell (arg)
1443   "Move to prefix ARGth following cell (same level) within current view.
1444 Return number of cells left to move."
1445   (interactive "p")
1446   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1447   (if (< arg 0)
1448       (kotl-mode:backward-cell (- arg))
1449     (let ((next (= arg 0))
1450           (label-sep-len (kview:label-separator-length kview)))
1451       (while (and (> arg 0) (setq next (kcell-view:forward t label-sep-len)))
1452         (setq arg (1- arg)))
1453       (if (or next (not (interactive-p)))
1454           arg
1455         (error "(kotl-mode:forward-cell): No following cell at same level")))))
1456
1457 (defun kotl-mode:forward-char (&optional arg)
1458   "Move point forward ARG (or 1) characters and return point."
1459   (interactive "p")
1460   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1461   (or arg (setq arg 1))
1462   (if (>= arg 0)
1463       (while (> arg 0)
1464         (cond ((and (kotl-mode:eolp) (kotl-mode:last-line-p))
1465                (error "(kotl-mode:forward-char): End of buffer"))
1466               ((kotl-mode:eocp)
1467                (skip-chars-forward "\n\r")
1468                (kotl-mode:start-of-line))
1469               ((kotl-mode:eolp)
1470                (forward-char)
1471                (kotl-mode:start-of-line))
1472               (t (forward-char)))
1473         (setq arg (1- arg)))
1474     (kotl-mode:backward-char (- arg)))
1475   (point))
1476
1477 (defun kotl-mode:forward-paragraph (&optional arg)
1478   "Move point forward until after the last character of the current paragraph.
1479 With arg N, do it N times; negative arg -N means move backward N paragraphs.
1480 Return point.
1481
1482 A line which `paragraph-start' matches either separates paragraphs
1483 \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
1484 A paragraph end is one character before the beginning of a line which is not
1485 part of the paragraph, or the end of the buffer."
1486   (interactive "p")
1487   (setq arg (prefix-numeric-value arg)
1488         zmacs-region-stays t);; Maintain region highlight for XEmacs.
1489   (if (< arg 0)
1490       (progn
1491         (if (kotl-mode:bocp) (setq arg (1- arg)))
1492         (while (< arg 0)
1493           (start-of-paragraph-text)
1494           (setq arg (1+ arg))))
1495     (while (> arg 0) 
1496       (end-of-paragraph-text)
1497       (setq arg (1- arg))))
1498   (kotl-mode:to-valid-position)
1499   (point))
1500
1501 (fset 'kotl-mode:forward-para 'kotl-mode:forward-paragraph)
1502
1503 (defun kotl-mode:forward-sentence (&optional arg)
1504   "Move point forward ARG (or 1) sentences and return point."
1505   (interactive "P")
1506   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1507   (let* ((label-sep-len (kview:label-separator-length kview))
1508          ;; Setting fill prefix makes sentence commands properly recognize
1509          ;; indented paragraphs.
1510          (fill-prefix (make-string (kcell-view:indent nil label-sep-len) ?\ )))
1511     (if (kotl-mode:eobp)
1512         (error "(kotl-mode:forward-sentence): Last sentence")
1513       (if (kotl-mode:eocp) (kcell-view:next nil label-sep-len))
1514       (or arg (setq arg 1))
1515       (save-restriction
1516         (if (= arg 1)
1517             (narrow-to-region
1518              (- (kcell-view:start nil label-sep-len)
1519                 (kcell-view:indent nil label-sep-len))
1520              (kcell-view:end-contents)))
1521         (unwind-protect
1522             (let ((opoint (point)))
1523               (forward-sentence arg)
1524               (if (= opoint (point))
1525                   (progn (kcell-view:next nil label-sep-len)
1526                          (forward-sentence arg))))
1527           (kotl-mode:to-valid-position)))))
1528   (point))
1529
1530 (defun kotl-mode:forward-word (&optional arg)
1531   "Move point forward ARG (or 1) words and return point."
1532   (interactive "p")
1533   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1534   (or arg (setq arg 1))
1535   (if (>= arg 0)
1536       (while (> arg 0)
1537         (cond ((kotl-mode:eobp) (setq arg 0))
1538               ((kotl-mode:eocp)
1539                (skip-chars-forward "\n\r")
1540                (kotl-mode:start-of-line)))
1541         (unwind-protect
1542             (forward-word 1)
1543           (kotl-mode:to-valid-position))
1544         ;; If point is at beginning of a cell after moving forward a word,
1545         ;; then we moved over something other than a word (some
1546         ;; punctuation or an outline autonumber); therefore, leave counter as
1547         ;; is in order to move forward over next word.
1548         (or (kotl-mode:bocp)
1549             (setq arg (1- arg))))
1550     (kotl-mode:backward-word (- arg)))
1551   (point))
1552
1553 (defun kotl-mode:goto-cell (cell-ref &optional error-p)
1554   "Move point to start of cell given by CELL-REF.  (See 'kcell:ref-to-id'.)
1555 Return point iff CELL-REF is found within current view.
1556 With a prefix argument, CELL-REF is assigned the argument value for use
1557 as an idstamp.
1558
1559 Optional second arg, ERROR-P, non-nil means signal an error if CELL-REF is
1560 not found within current view.  Will signal same error if called
1561 interactively when CELL-REF is not found."
1562   (interactive
1563    (list (if current-prefix-arg
1564              (format "0%d" (prefix-numeric-value current-prefix-arg))
1565            (read-string "Goto cell label or id: "))))
1566   (setq cell-ref
1567         (or (kcell:ref-to-id cell-ref)
1568             (error "(kotl-mode:goto-cell): Invalid cell reference, '%s'" cell-ref)))
1569   (let* ((opoint (point))
1570          (found)
1571          cell-id kvspec)
1572     (if (= ?| (aref cell-ref 0))
1573         ;; This is a standalone view spec, not a cell reference.
1574         (progn (kvspec:activate cell-ref) (setq found (point)))
1575
1576       ;; !! Remove any relative specs and view specs from
1577       ;; cell-ref to form cell-id.  Really should account for relative
1578       ;; specs here, but we don't yet support them.
1579       (if (string-match "\\(\\.[a-zA-Z]+\\)?\\([|:].*\\)\\|\\.[a-zA-Z]+"
1580                         cell-ref)
1581           (setq cell-id (substring cell-ref 0 (match-beginning 0))
1582                 kvspec  (if (match-beginning 2)
1583                             (substring
1584                              cell-ref (match-beginning 2) (match-end 2))))
1585         (setq cell-id cell-ref kvspec nil))
1586
1587       (goto-char (point-min))
1588       (cond ((= ?0 (aref cell-id 0))
1589              ;; is an idstamp
1590              (if (kview:goto-cell-id cell-id)
1591                  (setq found (point))))
1592             ;; is a label
1593             ((re-search-forward
1594               (format "\\([\n\r][\n\r]\\|\\`\\)[ ]*%s%s"
1595                       (regexp-quote cell-id)
1596                       (regexp-quote (kview:label-separator kview)))
1597               nil t)
1598              (setq found (point)))
1599             ;; no match
1600             (t (goto-char opoint)
1601                nil))
1602       (if (and (not found) (or error-p (interactive-p)))
1603           (error "(kotl-mode:goto-cell): No '%s' cell in this view" cell-ref)
1604         ;; Activate any viewspec associated with cell-ref.
1605         (if kvspec (kvspec:activate kvspec))))
1606     found))
1607
1608 (defun kotl-mode:head-cell ()
1609   "Move point to the start of the first visible cell at the same level as current cell.
1610 If at head cell already, do nothing and return nil."
1611   (interactive "p")
1612   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1613   (let ((moved)
1614         (label-sep-len (kview:label-separator-length kview)))
1615     (while (kcell-view:backward t label-sep-len)
1616       (setq moved t))
1617     moved))
1618
1619 (defun kotl-mode:last-sibling ()
1620   "Move point to the last sibling of the present cell.
1621 Leave point at the start of the cell or at its present position if it is
1622 already within the last sibling cell."
1623   (interactive)
1624   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1625   (let ((label-sep-len (kview:label-separator-length kview)))
1626     (if (save-excursion (kcell-view:forward nil label-sep-len))
1627         ;; Enable user to return to this previous position if desired.
1628         (push-mark nil 'no-msg))
1629     (while (kcell-view:forward nil label-sep-len))))
1630
1631 (defun kotl-mode:mark-paragraph ()
1632   "Put point at beginning of this paragraph, mark at end.
1633 The paragraph marked is the one that contains point or follows point."
1634   (interactive)
1635   (forward-paragraph 1)
1636   (kotl-mode:to-valid-position t)
1637   (hypb:push-mark nil t t)
1638   (backward-paragraph 1)
1639   (kotl-mode:to-valid-position))
1640
1641 (defun kotl-mode:mark-whole-buffer ()
1642   "Put point at first editable character in buffer and mark at the last such character."
1643   (interactive)
1644   (hypb:push-mark (point))
1645   (kotl-mode:end-of-buffer)
1646   (hypb:push-mark (point) nil t)
1647   (kotl-mode:beginning-of-buffer))
1648
1649 (defun kotl-mode:next-cell (arg)
1650   "Move to prefix ARGth next cell (any level) within current view."
1651   (interactive "p")
1652   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1653   (if (< arg 0)
1654       (kotl-mode:previous-cell (- arg))
1655     (let ((next (= arg 0))
1656           (label-sep-len (kview:label-separator-length kview)))
1657       (while (and (> arg 0) (setq next (kcell-view:next t label-sep-len)))
1658         (setq arg (1- arg)))
1659       (if next
1660           arg
1661         (error "(kotl-mode:next-cell): Last cell")))))
1662
1663 (defun kotl-mode:next-line (arg)
1664   "Move point to ARGth next line and return point."
1665   (interactive "p")
1666   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1667   (kotl-mode:set-temp-goal-column)
1668   (let ((orig-arg arg))
1669     (cond ((> arg 0)
1670            (while (and (> arg 0) (= 0 (forward-line 1)))
1671              (cond ((kotl-mode:eobp)
1672                     (forward-line -1)
1673                     (goto-char (kcell-view:end-contents))
1674                     (and (interactive-p) (= orig-arg arg)
1675                          (message "(kotl-mode:next-line): Last line") (beep))
1676                     (setq arg 0)
1677                     )
1678                    ((looking-at "^$");; blank line between cells
1679                     nil);; Don't count this line.
1680                    (t (setq arg (1- arg)))))
1681            (kotl-mode:line-move 0)
1682            (kotl-mode:to-valid-position)
1683            )
1684           ((< arg 0)
1685            (kotl-mode:previous-line (- arg)))
1686           (t)))
1687   (setq this-command 'next-line)
1688   (point))
1689
1690 (defun kotl-mode:next-tree ()
1691   "Move past current tree to next tree, or to last cell in tree if no next tree.
1692 Return non-nil iff there is a next tree within this koutline."
1693   (let ((start-indent (kcell-view:indent))
1694         (label-sep-len (kview:label-separator-length kview))
1695         (same-tree t))
1696       (while (and (kcell-view:next nil label-sep-len)
1697                   (setq same-tree (< start-indent
1698                                      (kcell-view:indent nil label-sep-len)))))
1699       (not same-tree)))
1700
1701 (defun kotl-mode:previous-line (arg)
1702   "Move point to ARGth previous line and return point."
1703   (interactive "p")
1704   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1705   (kotl-mode:set-temp-goal-column)
1706   (cond ((> arg 0)
1707          (while (and (> arg 0) (= 0 (forward-line -1)))
1708            (cond ((kotl-mode:bobp)
1709                   (kotl-mode:beginning-of-cell)
1710                   (setq arg 0))
1711                  ((looking-at "^$") ;; blank line between cells
1712                   nil) ;; Don't count this line.
1713                  (t (setq arg (1- arg)))))
1714          (kotl-mode:line-move 0)
1715          (kotl-mode:to-valid-position)
1716          )
1717         ((< arg 0)
1718          (kotl-mode:next-line (- arg)))
1719         (t))
1720   (setq this-command 'previous-line)
1721   (point))
1722
1723 (defun kotl-mode:previous-cell (arg)
1724   "Move to prefix ARGth previous cell (any level) within current view."
1725   (interactive "p")
1726   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1727   (if (< arg 0)
1728       (kotl-mode:next-cell (- arg))
1729     (let ((previous (= arg 0))
1730           (label-sep-len (kview:label-separator-length kview)))
1731       (while (and (> arg 0) (setq previous
1732                                   (kcell-view:previous t label-sep-len)))
1733         (setq arg (1- arg)))
1734       (if previous
1735           arg
1736         (error "(kotl-mode:previous-cell): First cell")))))
1737
1738 (defun kotl-mode:scroll-down (arg)
1739   "Scroll text of current window downward ARG lines; or a windowful if no ARG."
1740   (interactive "P")
1741   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1742   (scroll-down arg)
1743   (kotl-mode:to-valid-position t))
1744
1745 (defun kotl-mode:scroll-up (arg)
1746   "Scroll text of current window upward ARG lines; or a windowful if no ARG."
1747   (interactive "P")
1748   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1749   (scroll-up arg)
1750   (kotl-mode:to-valid-position))
1751
1752 (defun kotl-mode:tail-cell ()
1753   "Move point to the start of the last visible cell at the same level as current cell and return t.
1754 If at tail cell already, do nothing and return nil."
1755   (interactive "p")
1756   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1757   (let ((moved)
1758         (label-sep-len (kview:label-separator-length kview)))
1759     (while (kcell-view:forward t label-sep-len)
1760       (setq moved t))
1761     moved))
1762
1763 (defun kotl-mode:up-level (arg)
1764   "Move up prefix ARG levels higher in current outline view."
1765   (interactive "p")
1766   (setq zmacs-region-stays t) ;; Maintain region highlight for XEmacs.
1767   (if (< arg 0)
1768       (kotl-mode:down-level (- arg))
1769     ;; Enable user to return to this previous position if desired.
1770     (push-mark nil 'no-msg)
1771     (let ((parent)
1772           (label-sep-len (kview:label-separator-length kview))
1773           result)
1774       (while (and (> arg 0) (setq result (kcell-view:parent t label-sep-len)))
1775         (or parent (setq parent result))
1776         (setq arg (if (eq result 0) 0 (1- arg))))
1777       ;; Signal an error if couldn't move up at least 1 parent level.
1778       (or (and parent (not (eq parent 0)))
1779           (progn
1780             (goto-char (hypb:mark t))
1781             (pop-mark)
1782             (error "(kotl-mode:up-level): No parent level to which to move")
1783             )))))
1784
1785 ;;;
1786 ;;; Predicates
1787 ;;;
1788
1789 (defun kotl-mode:bobp ()
1790   "Return point if at the start of the first cell in kview, else nil."
1791   (interactive)
1792   (or (bobp)
1793       (and (not (save-excursion (re-search-backward "[\n\r]" nil t)))
1794            (kotl-mode:bolp))))
1795
1796 (defun kotl-mode:bocp ()
1797   "Return point if at beginning of a kcell, else nil."
1798   (and (kotl-mode:bolp)
1799        (let ((begin-point (kcell-view:plist-point))
1800              (bol))
1801          (and begin-point
1802               (save-excursion
1803                 ;; If first line-begin is less than cell begin point,
1804                 ;; then we know we are on the first line of the cell.
1805                 (if (setq bol (re-search-backward "^" nil t))
1806                     (<= bol begin-point)))))
1807        (point)))
1808
1809 (defun kotl-mode:bolp ()
1810   "Return point if at beginning of a kview line, else nil."
1811   (if (= (current-column) (kcell-view:indent))
1812       (point)))
1813
1814 (defun kotl-mode:buffer-empty-p ()
1815   "Return non-nil iff there are no outline cells within current buffer."
1816   (save-excursion
1817     (goto-char (point-min))
1818     (looking-at "[\n\r]*\\'")))
1819
1820 (defun kotl-mode:eobp ()
1821   "Return point if after the end of the last cell in kview, else nil."
1822   (interactive)
1823   (if (looking-at "^[\n\r]*\\'") (point)))
1824
1825 (defun kotl-mode:eocp ()
1826   "Return point if at the end of a kview cell, else nil."
1827   (or (eobp)
1828       (looking-at "[\n\r]+\\'")
1829       (and (eolp)
1830            (save-excursion
1831              (skip-chars-forward "\n\r")
1832              (kotl-mode:start-of-line)
1833              (kotl-mode:bocp)))))
1834
1835 (fset 'kotl-mode:eolp 'eolp)
1836
1837 (defun kotl-mode:first-cell-p ()
1838   "Return t iff point is on the first cell of the outline."
1839   (save-excursion (not (kcell-view:previous))))
1840
1841 (fset 'kotl-mode:first-line-p 'first-line-p)
1842
1843 (defun kotl-mode:last-cell-p ()
1844   "Return t iff point is on the last cell of the outline."
1845   (save-excursion (not (kcell-view:next))))
1846
1847 (defun kotl-mode:last-line-p ()
1848   "Return t iff point is on the last line of the outline."
1849   (save-excursion
1850     (kotl-mode:finish-of-line)
1851     (looking-at "\n*\\'")))
1852
1853 ;;;
1854 ;;; Smart Key Support
1855 ;;;
1856
1857
1858 (defun kotl-mode:action-key ()
1859   "Collapses, expands, links to, and scrolls through koutline cells.
1860 Invoked via a key press when in kotl-mode.  It assumes that its caller has
1861 already checked that the key was pressed in an appropriate buffer and has
1862 moved the cursor to the selected buffer.
1863
1864 If key is pressed:
1865  (1) at the end of buffer, uncollapse and unhide all cells in view;
1866  (2) within a cell, if its subtree is hidden then show it,
1867      otherwise hide it;
1868  (3) between cells or within the read-only indentation region to the left of
1869      a cell, then move point to prior location and begin creation of a
1870      klink to some other outline cell; hit the Action Key twice to select the
1871      link referent cell;
1872  (4) anywhere else, scroll up a windowful."
1873   (interactive)
1874   (cond ((kotl-mode:eobp) (kotl-mode:show-all))
1875         ((kotl-mode:eolp) (smart-scroll-up))
1876         ((not (kview:valid-position-p))
1877          (if (markerp action-key-depress-prev-point)
1878              (progn (select-window
1879                      (get-buffer-window
1880                       (marker-buffer action-key-depress-prev-point)))
1881                     (goto-char (marker-position action-key-depress-prev-point))
1882                     (call-interactively 'klink:create))
1883            (kotl-mode:to-valid-position)
1884            (error "(kotl-mode:action-key): Action Key released at invalid position")))
1885         (t ;; On a cell line (not at the end of line).
1886          (if (smart-outline-subtree-hidden-p)
1887              (kotl-mode:show-tree (kcell-view:label))
1888            (kotl-mode:hide-tree (kcell-view:label)))))
1889   (kotl-mode:to-valid-position))
1890
1891 (defun kotl-mode:help-key ()
1892   "Displays properties of koutline cells, collapses all cells, and scrolls back.
1893 Invoked via an assist-key press when in kotl-mode.  It assumes that its caller
1894 has already checked that the assist-key was pressed in an appropriate buffer
1895 and has moved the cursor to the selected buffer.
1896
1897 If assist-key is pressed:
1898  (1) at the end of buffer, collapse all cells and hide all non-level-one
1899      cells;
1900  (2) on a header line but not at the beginning or end, display properties of
1901      each cell in tree beginning at point;
1902  (3) between cells or within the read-only indentation region to the left of
1903      a cell, then move point to prior location and prompt to move one tree to
1904      a new location in the outline; hit the Action Key twice to select the
1905      tree to move and where to move it;
1906  (4) anywhere else, scroll down a windowful."
1907   (interactive)
1908   (cond ((kotl-mode:eobp) (kotl-mode:overview))
1909         ((kotl-mode:eolp) (smart-scroll-down))
1910         ((not (kview:valid-position-p))
1911          (if (markerp assist-key-depress-prev-point)
1912              (progn (select-window
1913                      (get-buffer-window
1914                       (marker-buffer assist-key-depress-prev-point)))
1915                     (goto-char (marker-position
1916                                 assist-key-depress-prev-point))
1917                     (call-interactively 'kotl-mode:move-after))
1918            (kotl-mode:to-valid-position)
1919            (error "(kotl-mode:help-key): Help Key released at invalid position")))
1920         ((not (bolp))
1921          ;; On an outline header line but not at the start/end of line,
1922          ;; show attributes for tree at point.
1923          (kotl-mode:cell-help (kcell-view:label) (or current-prefix-arg 2)))
1924         ((smart-scroll-down)))
1925   (kotl-mode:to-valid-position))
1926
1927 ;;;
1928 ;;; Structure Editing
1929 ;;;
1930
1931 (defun kotl-mode:add-child ()
1932   "Add a new cell to current kview as first child of current cell."
1933   (interactive "*")
1934   (kotl-mode:add-cell '(4)))
1935
1936 (defun kotl-mode:add-parent ()
1937   "Add a new cell to current kview as sibling of current cell's parent."
1938   (interactive "*")
1939   (kotl-mode:add-cell -1))
1940
1941 (defun kotl-mode:add-cell (&optional relative-level contents plist no-fill)
1942   "Add a cell following current cell at optional RELATIVE-LEVEL with CONTENTS string, attributes in PLIST, a property list, and NO-FILL flag to prevent any filling of CONTENTS. 
1943
1944 Optional prefix arg RELATIVE-LEVEL means add as sibling if nil or >= 0, as
1945 child if equal to universal argument, {C-u}, and as sibling of current cell's
1946 parent, otherwise.  If added as sibling of current level, RELATIVE-LEVEL is
1947 used as a repeat count for the number of cells to add.
1948
1949 Return last newly added cell."
1950   (interactive "*P")
1951   (or (stringp contents) (setq contents nil))
1952   (let ((klabel (kcell-view:label))
1953         (label-sep-len (kview:label-separator-length kview))
1954         cell-level new-cell sibling-p child-p start parent
1955         cells-to-add)
1956     (setq cell-level (kcell-view:level nil label-sep-len)
1957           child-p (equal relative-level '(4))
1958           sibling-p (and (not child-p)
1959                          (cond ((not relative-level) 1)
1960                                ((>= (prefix-numeric-value relative-level) 0)
1961                                 (prefix-numeric-value relative-level))))
1962           cells-to-add (or sibling-p 1))
1963     (if child-p
1964         (setq cell-level (1+ cell-level))
1965       (if sibling-p
1966           nil
1967         ;; Add as following sibling of current cell's parent.
1968         ;; Move to parent.
1969         (setq cell-level (1- cell-level)
1970               start (point)
1971               parent (kcell-view:parent nil label-sep-len))
1972         (if (not (eq parent t))
1973             (progn
1974               (goto-char start)
1975               (error
1976                "(kotl-mode:add-cell): No higher level at which to add cell.")
1977               )))
1978       ;; Skip from point past any children to next cell.
1979       (if (kotl-mode:next-tree)
1980           ;; If found a new tree, then move back to prior cell so can add
1981           ;; new cell after it.
1982           (kcell-view:previous nil label-sep-len)))
1983     (goto-char (kcell-view:end))
1984     ;;
1985     ;; Insert new cells into view.
1986     (if (= cells-to-add 1)
1987         (setq klabel
1988               (cond (sibling-p
1989                      (klabel:increment klabel))
1990                     (child-p (klabel:child klabel))
1991                     ;; add as sibling of parent of current cell
1992                     (t (klabel:increment (klabel:parent klabel))))
1993               new-cell (kview:add-cell klabel cell-level contents plist
1994                                        (or no-fill sibling-p
1995                                            (not kotl-mode:refill-flag))))
1996       ;;
1997       ;; sibling-p must be true if we are looping here so there is no need to
1998       ;; conditionalize how to increment the labels.
1999       (while (>= (setq cells-to-add (1- cells-to-add)) 0)
2000         (setq klabel (klabel:increment klabel)
2001               ;; Since new cells are at the same level as old one, don't fill
2002               ;; any of their intial contents.
2003               new-cell (kview:add-cell klabel cell-level contents plist t))))
2004     ;;
2005     ;; Move back to last inserted cell and then move to its following
2006     ;; sibling if any.
2007     (kotl-mode:to-valid-position t)
2008     (save-excursion
2009       (if (kcell-view:forward t label-sep-len)
2010           ;; Update the labels of these siblings and their subtrees.
2011           (klabel-type:update-labels (klabel:increment klabel))))
2012     ;;
2013     ;; Leave point within last newly added cell and return this cell.
2014     (kotl-mode:beginning-of-cell)
2015     new-cell))
2016
2017 (defun kotl-mode:demote-tree (arg)
2018   "Move current tree a maximum of prefix ARG levels lower in current view.
2019 Each cell is refilled iff its `no-fill' attribute is nil and
2020 kotl-mode:refill-flag is non-nil.  With prefix ARG = 0, cells are demoted up
2021 to one level and kotl-mode:refill-flag is treated as true."
2022   (interactive "*p")
2023   (if (< arg 0)
2024       (kotl-mode:promote-tree (- arg))
2025     (let* ((label-sep-len (kview:label-separator-length kview))
2026            (orig-level (kcell-view:level nil label-sep-len))
2027            (orig-point (point))
2028            (orig-id (kcell-view:idstamp))
2029            (fill-p (= arg 0))
2030            (orig-pos-in-cell
2031             (- (point) (kcell-view:start nil label-sep-len)))
2032            prev prev-level)
2033       (if fill-p (setq arg 1))
2034       (unwind-protect
2035           (progn
2036             (backward-char 1)
2037             (while (and (> arg 0)
2038                         (setq prev
2039                               (kcell-view:previous nil label-sep-len)))
2040               (if prev
2041                   (progn (setq prev-level
2042                                (kcell-view:level nil label-sep-len))
2043                          (cond ((> prev-level (+ orig-level arg))
2044                                 ;; Don't want to demote this far
2045                                 ;; so keep looking at prior nodes.
2046                                 nil)
2047                                ((= arg (- prev-level orig-level))
2048                                 ;; Demote to be sibling of this kcell.
2049                                 (setq arg -1))
2050                                ((< prev-level orig-level)
2051                                 ;; prev is at higher level then
2052                                 ;; orig, so can't demote
2053                                 (setq prev nil
2054                                       arg 0))
2055                                (t
2056                                 ;; Demote below this kcell.  This is
2057                                 ;; as far we can demote, though it may
2058                                 ;; not be the full amount of arg.
2059                                 (setq arg 0))))))
2060             (if prev
2061                 (kotl-mode:move-after
2062                  (kcell-view:label orig-point)
2063                  (kcell-view:label) (= arg 0)
2064                  nil fill-p)))
2065         ;; Move to start of original cell
2066         (kotl-mode:goto-cell orig-id)
2067         ;; Move to original pos within cell
2068         (forward-char orig-pos-in-cell)
2069         (kotl-mode:to-valid-position))
2070       (if (not prev)
2071           (error "(kotl-mode:demote-tree): Cannot demote any further")))))
2072
2073 (defun kotl-mode:exchange-cells (cell-ref-1 cell-ref-2)
2074   "Exchange CELL-REF-1 with CELL-REF-2 in current view.  Don't move point."
2075   (interactive
2076    (let ((hargs:defaults
2077           (save-excursion
2078             (list (kcell-view:label)
2079                   (cond
2080                    ((kcell-view:previous t)
2081                     (kcell-view:label))
2082                    ((kcell-view:next t)
2083                     (kcell-view:label))
2084                    (t (error
2085                        "(kotl-mode:exchange-cells): No 2 visible cells")))))))
2086      (hargs:iform-read
2087       '(interactive "*+KExchange cell: \n+KExchange cell <%s> with cell: "))))
2088   (save-excursion
2089     (let (kcell-1 contents-1
2090           kcell-2 contents-2)
2091       ;;
2092       ;; Save cell-1 attributes
2093       (kotl-mode:goto-cell cell-ref-1 t)
2094       (setq kcell-1 (kcell-view:cell)
2095             contents-1 (kcell-view:contents))
2096       ;;
2097       ;; Save cell-2 attributes
2098       (kotl-mode:goto-cell cell-ref-2 t)
2099       (setq kcell-2 (kcell-view:cell)
2100             contents-2 (kcell-view:contents))
2101       ;;
2102       ;; Substitute cell-1 attributes into cell-2 location.
2103       ;;
2104       ;; Set kcell properties.
2105       (kcell-view:set-cell kcell-1)
2106       ;; If idstamp labels are on, then must exchange labels in view.
2107       (if (eq (kview:label-type kview) 'id)
2108           (klabel:set (kcell-view:idstamp)))
2109       ;; Exchange cell contents.
2110       (delete-region (kcell-view:start) (kcell-view:end-contents))
2111       (insert
2112        (hypb:replace-match-string
2113         "\\([\n\r]\\)"
2114         contents-1 (concat "\\1" (make-string (kcell-view:indent) ?\ ))))
2115       (if kotl-mode:refill-flag (kotl-mode:fill-cell))
2116       ;;
2117       ;; Substitute cell-2 attributes into cell-1 location.
2118       ;;
2119       ;; Set kcell properties.
2120       (kotl-mode:goto-cell cell-ref-1 t)
2121       (kcell-view:set-cell kcell-2)
2122       ;; If idstamp labels are on, then must exchange labels in view.
2123       (if (eq (kview:label-type kview) 'id)
2124           (klabel:set (kcell-view:idstamp)))
2125       ;; Exchange cell contents.
2126       (delete-region (kcell-view:start) (kcell-view:end-contents))
2127       ;; Add indentation to all but first line.
2128       (insert
2129        (hypb:replace-match-string
2130         "\\([\n\r]\\)"
2131         contents-2 (concat "\\1" (make-string (kcell-view:indent) ?\ ))))
2132       (if kotl-mode:refill-flag (kotl-mode:fill-cell)))))
2133
2134 (defun kotl-mode:kill-contents (arg)
2135   "Kill contents of cell from point to cell end.
2136 With prefix ARG, kill entire cell contents."
2137   (interactive "*P")
2138   (kotl-mode:kill-region
2139    (if arg (kcell-view:start) (point))
2140    (kcell-view:end-contents)))
2141
2142 (defun kotl-mode:kill-tree (&optional arg)
2143   "Kill ARG following trees starting with tree rooted at point.
2144 If ARG is not a non-positive number, nothing is done."
2145   (interactive "*p")
2146   (or (integerp arg) (setq arg 1))
2147   (let ((killed) (label (kcell-view:label))
2148         (label-sep-len (kview:label-separator-length kview))
2149         start end sib)
2150     (while (> arg 0)
2151       (setq start (kotl-mode:tree-start)
2152             end   (kotl-mode:tree-end)
2153             sib   (kcell-view:sibling-p nil nil label-sep-len)
2154             arg (1- arg)
2155             killed t)
2156       ;; Don't want to delete any prior cells, so if on last cell, ensure
2157       ;; this is the last one killed.
2158       (if (kotl-mode:last-cell-p)
2159           (progn (setq arg 0)
2160                  (kview:delete-region start end))
2161         (kview:delete-region start end)
2162         (kotl-mode:to-valid-position)))
2163     (if killed
2164         (progn
2165           (cond (sib (klabel-type:update-labels label))
2166                 ((kotl-mode:buffer-empty-p)
2167                  ;; Always leave at least 1 visible cell within a view.
2168                  (kview:add-cell "1" 1)))
2169           (kotl-mode:to-valid-position)))))
2170
2171 (defun kotl-mode:mail-tree (cell-ref invisible-flag)
2172   "Mail outline tree rooted at CELL-REF.  Use \"0\" for whole outline buffer.
2173 Invisible text is expanded and included in the mail only if INVISIBLE-FLAG is
2174 non-nil."
2175   (interactive
2176    (let ((label-default (kcell-view:label)))
2177      (hargs:iform-read
2178       '(interactive
2179         (list 
2180          (hargs:read "Mail tree: (0 for whole outline) "
2181                      nil label-default nil 'kcell)
2182          (y-or-n-p "Include invisible text? "))))))
2183   (if (equal cell-ref "0")
2184       (hmail:buffer nil invisible-flag)
2185     (let (start end)
2186       (save-excursion
2187         (kotl-mode:goto-cell cell-ref t)
2188         (beginning-of-line)
2189         (setq start (point))
2190         (or (= (kotl-mode:forward-cell 1) 0) (goto-char (point-max)))
2191         (forward-line -1)
2192         (setq end (point)))
2193       (hmail:region start end nil invisible-flag))))
2194
2195 (defun kotl-mode:promote-tree (arg)
2196   "Move current tree a maximum of prefix ARG levels higher in current view.
2197 Each cell is refilled iff its `no-fill' attribute is nil and
2198 kotl-mode:refill-flag is non-nil.  With prefix ARG = 0, cells are promoted up
2199 to one level and kotl-mode:refill-flag is treated as true."
2200   (interactive "*p")
2201   (if (< arg 0)
2202       (kotl-mode:demote-tree (- arg))
2203     (let* ((parent) (result)
2204            (label-sep-len (kview:label-separator-length kview))
2205            (orig-point (point))
2206            (orig-id (kcell-view:idstamp))
2207            (fill-p (= arg 0))
2208            (orig-pos-in-cell
2209             (- (point) (kcell-view:start nil label-sep-len))))
2210       (if fill-p (setq arg 1))
2211       (unwind-protect
2212           (progn
2213             (backward-char 1)
2214             (while (and (> arg 0)
2215                         (setq result (kcell-view:parent
2216                                       nil label-sep-len))
2217                         (not (eq result 0)))
2218               (setq parent result
2219                     arg (1- arg)))
2220             (if parent
2221                 (kotl-mode:move-after
2222                  (kcell-view:label orig-point)
2223                  (kcell-view:label) nil
2224                  nil fill-p)))
2225         ;; Move to start of original cell
2226         (kotl-mode:goto-cell orig-id)
2227         ;; Move to original pos within cell
2228         (forward-char orig-pos-in-cell)
2229         (kotl-mode:to-valid-position))
2230       (if (not parent)
2231           (error "(kotl-mode:promote-tree): Cannot promote any further")))))
2232
2233 (defun kotl-mode:set-cell-attribute (attribute value &optional pos)
2234   "Include ATTRIBUTE VALUE with the current cell or the cell at optional POS.
2235 Replaces any existing value that ATTRIBUTE has.
2236 When called interactively, it displays the setting in the minibuffer as
2237 confirmation."
2238   (interactive
2239    (let* ((plist (copy-sequence (kcell-view:plist)))
2240           (existing-attributes plist)
2241           attribute value)
2242      (barf-if-buffer-read-only)
2243      ;; Remove attribute values
2244      (while plist
2245        (setcdr plist (cdr (cdr plist)))
2246        (setq plist (cdr plist)))
2247      ;; Remove read-only attributes
2248      (setq existing-attributes (set:create existing-attributes)
2249            existing-attributes (set:difference
2250                                 existing-attributes
2251                                 kcell:read-only-attributes))
2252
2253      (while (zerop (length (setq attribute
2254                                  (completing-read
2255                                   "Current cell attribute to set: "
2256                                   (mapcar 'list
2257                                           (mapcar 'symbol-name
2258                                                   existing-attributes))))))
2259        (beep))
2260      (setq attribute (intern attribute)
2261            value (kcell-view:get-attr attribute))
2262      (if value
2263          (setq value (read-expression
2264                       (format "Change value of \"%s\" to: " attribute)
2265                       (prin1-to-string value)))
2266        (setq value (read-expression
2267                     (format "Set value of \"%s\" to: " attribute))))
2268      (list attribute value nil)))
2269   (kcell-view:set-attr attribute value pos)
2270   ;; Note that buffer needs to be saved to store new attribute value.
2271   (set-buffer-modified-p t)
2272   (if (interactive-p)
2273       (message "Attribute \"%s\" set to `%s' in cell <%s>."
2274                attribute value (kcell-view:label pos))))
2275
2276 (defun kotl-mode:split-cell (&optional arg)
2277   "Split the current cell into two cells and move to the new cell.
2278 The cell contents after point become part of the newly created cell.
2279 The default is to create the new cell as a sibling of the current cell.
2280 With optional universal ARG, {C-u}, the new cell is added as the child of
2281 the current cell."
2282   (interactive "*P")
2283   (let ((new-cell-contents (kotl-mode:kill-region
2284                             (point) (kcell-view:end-contents) 'string))
2285         (start (kcell-view:start)))
2286     ;; delete any preceding whitespace
2287     (skip-chars-backward " \t\n\r" start)
2288     (delete-region (max start (point)) (kcell-view:end-contents))
2289     (kotl-mode:add-cell arg new-cell-contents (kcell-view:plist))))
2290
2291 (defun kotl-mode:transpose-cells (arg)
2292   "Exchange current and previous visible cells, leaving point after both.
2293 If no previous cell, exchange current with next cell.
2294 With prefix ARG, take current tree and move it past ARG visible cells.
2295 With prefix ARG = 0, interchange the cell that contains point with the cell
2296 that contains mark."
2297   (interactive "*p")
2298   (let ((label-sep-len (kview:label-separator-length kview)))
2299     (cond
2300      ((save-excursion (not (or (kcell-view:next t label-sep-len)
2301                                (kcell-view:previous t label-sep-len))))
2302       (error "(kotl-mode:transpose-cells): Only one visible cell in outline"))
2303      ;;
2304      ;; Transpose current and previous cells or current and next cells, if no
2305      ;; previous cell.  Leave point after both exchanged cells or within last
2306      ;; visible cell.
2307      ((= arg 1)
2308       (let ((label-1 (kcell-view:label))
2309             (prev (kcell-view:previous t label-sep-len))
2310             label-2)
2311         (or prev (kcell-view:next t label-sep-len))
2312         (setq label-2 (kcell-view:label))
2313         (kotl-mode:exchange-cells label-1 label-2)
2314         (kcell-view:next t label-sep-len)
2315         (if prev (kcell-view:next t label-sep-len))))
2316      ;;
2317      ;; Transpose point and mark cells, moving point to the new location of the
2318      ;; cell which originally contained point.
2319      ((= arg 0)
2320       (let ((label-1 (kcell-view:label))
2321             label-2)
2322         ;; This is like exchange-point-and-mark, but doesn't activate the
2323         ;; mark.
2324         (goto-char (prog1 (hypb:mark t)
2325                      (set-marker (hypb:mark-marker t) (point))))
2326         (setq label-2 (kcell-view:label))
2327         (kotl-mode:exchange-cells label-1 label-2)))
2328      ;;
2329      ;; Move current tree past ARG next visible cells and leave point after
2330      ;; original cell text.
2331      (t
2332       (let ((mark (set-marker (make-marker)
2333                               (save-excursion (kotl-mode:next-line arg)))))
2334         (kotl-mode:move-after
2335          (kcell-view:label)
2336          (progn (while (and (> arg 0) (kcell-view:next t label-sep-len))
2337                   (setq arg (1- arg)))
2338                 (kcell-view:label))
2339          nil)
2340         (goto-char mark)
2341         (set-marker mark nil))))))
2342
2343 ;;;
2344 ;;; Structure Viewing
2345 ;;;
2346
2347 (defun kotl-mode:collapse-tree (&optional all-flag)
2348   "Collapse to one line each visible cell of tree rooted at point.
2349 With optional ALL-FLAG non-nil, collapse all cells visible within the current
2350 view."
2351   (interactive "P")
2352   (kotl-mode:is-p)
2353   (let (buffer-read-only)
2354     (if all-flag
2355         (progn (kvspec:show-lines-per-cell 1)
2356                (kvspec:update t))
2357       (kview:map-tree
2358        (function
2359         (lambda (kview)
2360           ;; Use free variable label-sep-len bound in kview:map-tree for speed.
2361           (goto-char (kcell-view:start nil label-sep-len))
2362           (subst-char-in-region (point) (kcell-view:end-contents) ?\n ?\r t)))
2363        kview nil t))))
2364
2365 (defun kotl-mode:expand-tree (&optional all-flag)
2366   "Expand each visible cell of tree rooted at point.
2367 With optional ALL-FLAG non-nil, expand all cells visible within the current
2368 view."
2369   (interactive "P")
2370   (kotl-mode:is-p)
2371   (let (buffer-read-only)
2372     (if all-flag
2373         (progn (kvspec:show-lines-per-cell 0)
2374                (kvspec:update t))
2375       (kview:map-tree
2376        (function
2377         (lambda (kview)
2378           ;; Use free variable label-sep-len bound in kview:map-tree for speed.
2379           (goto-char (kcell-view:start nil label-sep-len))
2380           (subst-char-in-region (point) (kcell-view:end-contents) ?\r ?\n t)))
2381        kview nil t))))
2382
2383 (defun kotl-mode:toggle-tree-expansion (&optional all-flag)
2384   "Collapse or expand each cell of tree rooted at point or all visible cells if optional prefix arg ALL-FLAG is given.
2385 If current cell is collapsed, cells will be expanded, otherwise they will be
2386 collapsed."
2387   (interactive "P")
2388   (if (kcell-view:collapsed-p)
2389        ;; expand cells
2390       (kotl-mode:expand-tree all-flag)
2391     (kotl-mode:collapse-tree all-flag)))
2392
2393 ;;; 
2394 (defun kotl-mode:overview ()
2395   "Show the first line of each cell."
2396   (interactive)
2397   (kotl-mode:show-all)
2398   (kotl-mode:collapse-tree t))
2399
2400 (defun kotl-mode:show-all ()
2401   "Show (expand) all cells in current view."
2402   (interactive)
2403   (if (kotl-mode:is-p)
2404       (progn (kview:set-attr kview 'levels-to-show 0)
2405              (kview:set-attr kview 'lines-to-show 0)
2406              (show-all)
2407              (kvspec:update t))))
2408
2409 (defun kotl-mode:top-cells ()
2410   "Collapse all level 1 cells in view and hide any deeper sublevels."
2411   (interactive)
2412   (kotl-mode:is-p)
2413   (let ((modified-p (buffer-modified-p))
2414         (buffer-read-only))
2415     (kvspec:levels-to-show 1)
2416     (kvspec:show-lines-per-cell 1)
2417     (kvspec:update t)
2418     ;; Restore buffer modification status
2419     (set-buffer-modified-p modified-p)))
2420
2421 ;;; 
2422 (defun kotl-mode:hide-sublevels (levels-to-keep)
2423   "Hide all cells in outline at levels deeper than LEVELS-TO-KEEP (a number).
2424 Shows any hidden cells within LEVELS-TO-KEEP.  1 is the first level.  0 means
2425 display all levels of cells."
2426   (interactive "P")
2427   (kvspec:levels-to-show levels-to-keep)
2428   ;; The prior call might have shown more lines per cell than the current
2429   ;; viewspec supports, so reset lines per cell.
2430   (kvspec:lines-to-show)
2431   (kvspec:update t))
2432
2433 (defun kotl-mode:hide-subtree (&optional cell-ref show-flag)
2434   "Hide subtree, ignoring root, at optional CELL-REF (defaults to cell at point)."
2435   (interactive)
2436   (kotl-mode:is-p)
2437   (save-excursion
2438     (if cell-ref
2439         (kotl-mode:goto-cell cell-ref t)
2440       (kotl-mode:beginning-of-cell))
2441     (let ((start (kcell-view:end-contents))
2442           (end (kotl-mode:tree-end t))
2443           (buffer-read-only))
2444       (if show-flag
2445           (subst-char-in-region start end ?\r ?\n t)
2446         (subst-char-in-region start end ?\n ?\r t)))))
2447
2448 (defun kotl-mode:show-subtree (&optional cell-ref)
2449   "Show subtree, ignoring root, at optional CELL-REF (defaults to cell at point)."
2450   (interactive)
2451   (kotl-mode:hide-subtree cell-ref t))
2452
2453 (defun kotl-mode:hide-tree (&optional cell-ref show-flag)
2454   "Collapse tree rooted at optional CELL-REF (defaults to cell at point)."
2455   (interactive)
2456   (kotl-mode:is-p)
2457   (save-excursion
2458     (let ((start (if cell-ref
2459                      (kotl-mode:goto-cell cell-ref t)
2460                    (kotl-mode:beginning-of-cell)))
2461           (end (kotl-mode:tree-end t))
2462           (buffer-read-only))
2463       (if show-flag
2464           (subst-char-in-region start end ?\r ?\n t)
2465         (subst-char-in-region start end ?\n ?\r t)))))
2466
2467 (defun kotl-mode:show-tree (&optional cell-ref)
2468   "Display fully expanded tree rooted at CELL-REF."
2469   (interactive)
2470   (kotl-mode:hide-tree cell-ref t))
2471
2472 ;;; 
2473 (defun kotl-mode:cell-attributes (all-flag)
2474   "Display a temporary buffer with the attributes of the current kcell.
2475 With prefix arg ALL-FLAG non-nil, display the attributes of all visible
2476 kcells in the current buffer.
2477
2478 See also the documentation for `kotl-mode:cell-help'."
2479   (interactive "P")
2480   (with-output-to-temp-buffer
2481       (hypb:help-buf-name "Kotl")
2482     (save-excursion
2483       (if (not all-flag)
2484           (kotl-mode:print-attributes kview)
2485         (let ((label-sep-len (kview:label-separator-length kview)))
2486           (kotl-mode:beginning-of-buffer)
2487           (while (progn (kotl-mode:print-attributes kview)
2488                         (kcell-view:next t label-sep-len))))))))
2489
2490 (defun kotl-mode:cell-help (&optional cell-ref cells-flag)
2491   "Display a temporary buffer with CELL-REF's attributes.
2492 CELL-REF defaults to current cell.
2493 Optional prefix arg CELLS-FLAG selects the cells to print:
2494   If = 1, print CELL-REF's cell only;
2495   If > 1, print CELL-REF's visible tree (the tree rooted at CELL-REF);
2496   If < 1, print all visible cells in current view (CELL-REF is not used).
2497
2498 See also the documentation for `kotl-mode:cell-attributes'."
2499   (interactive
2500    (let* ((label (kcell-view:label))
2501           (hargs:defaults (list label label)))
2502      (append
2503       (let ((arg (prefix-numeric-value current-prefix-arg)))
2504         (if (< arg 1)
2505             0
2506           (hargs:iform-read
2507            (list 'interactive
2508                  (format "+KDisplay properties of koutline %s: "
2509                          (if (= arg 1) "cell" "tree"))))))
2510       (list current-prefix-arg))))
2511   (or (integerp cells-flag)
2512       (setq cells-flag (prefix-numeric-value cells-flag)))
2513   (or (stringp cell-ref) (setq cell-ref (kcell-view:label)))
2514   (with-output-to-temp-buffer
2515       (hypb:help-buf-name "Koutline")
2516     (save-excursion
2517       (if (equal cell-ref "0")
2518           (progn
2519             (hattr:report (kcell:plist (kview:top-cell kview)))
2520             (terpri)
2521             (cond ((= cells-flag 1) nil)
2522                   ((> cells-flag 1)
2523                    (kview:map-tree 'kotl-mode:print-attributes kview t t))
2524                   ;; (< cells-flag 1)
2525                   (t (kotl-mode:cell-attributes t))))
2526         (cond ((= cells-flag 1)
2527                (kotl-mode:goto-cell cell-ref)
2528                (kotl-mode:print-attributes kview))
2529               ((> cells-flag 1)
2530                (kotl-mode:goto-cell cell-ref)
2531                (kview:map-tree 'kotl-mode:print-attributes kview nil t))
2532               ;; (< cells-flag 1)
2533               (t (kotl-mode:cell-attributes t)))))))
2534
2535 (defun kotl-mode:get-cell-attribute (attribute &optional pos)
2536   "Return ATTRIBUTE's value for the current cell or the cell at optional POS.
2537 When called interactively, it displays the value in the minibuffer."
2538   (interactive "SCurrent cell attribute to get: ")
2539   (let ((value (kcell-view:get-attr attribute pos)))
2540     (if (interactive-p)
2541         (message "Attribute \"%s\" = `%s' in cell <%s>."
2542                  attribute value (kcell-view:label pos)))
2543     value))
2544
2545 ;;;
2546 ;;; Private functions
2547 ;;;
2548
2549 (defun kotl-mode:add-indent-to-region (&optional indent start end)
2550   "Add current cell's indent to current region.
2551 Optionally, INDENT and region START and END may be given."
2552   (or (integerp indent) (setq indent (kcell-view:indent)))
2553   (save-excursion
2554     (save-restriction
2555       (narrow-to-region (or start (point)) (or end (hypb:mark t)))
2556       (goto-char (point-min))
2557       (replace-regexp "\n" (concat "\n" (make-string indent ?\ ))))))
2558
2559 (defun kotl-mode:delete-line (&optional pos)
2560   "Delete and return contents of cell line at point or optional POS as a string.
2561 Does not delete newline at end of line."
2562   (save-excursion
2563     (if pos (goto-char pos))
2564     (if (kview:valid-position-p)
2565         (let ((bol (kotl-mode:start-of-line))
2566               (eol (kotl-mode:finish-of-line)))
2567           (prog1
2568               (buffer-substring bol eol)
2569             (delete-region bol eol)))
2570       (error "(kotl-mode:delete-line): Invalid position, '%d'" (point)))))
2571
2572 (defun kotl-mode:indent-line (arg)
2573   ;; Disallow the indent-line command.
2574   (error "(kotl-mode:indent-line): Insert spaces to indent each line."))
2575
2576 (defun kotl-mode:indent-region (start end)
2577   ;; User might try to indent across cells.  This would be bad, so disallow
2578   ;; the indent-region command.
2579   (error "(kotl-mode:indent-region): Insert spaces to indent each line."))
2580
2581 (defun kotl-mode:is-p ()
2582   "Signal an error if current buffer is not a Hyperbole outline, else return t."
2583   (if (kview:is-p kview)
2584       t
2585     (hypb:error
2586      "(kotl-mode:is-p): Command requires a current Hyperbole outline.")))
2587
2588 (defun kotl-mode:tree-end (&optional omit-end-newlines)
2589   "Return end point of current cell's tree within this view.
2590 If optional OMIT-END-NEWLINES is non-nil, point returned precedes any
2591 newlines at end of tree."
2592   (let* ((label-sep-len (kview:label-separator-length kview))
2593          (start-indent (kcell-view:indent nil label-sep-len))
2594          (next))
2595     (save-excursion
2596       (while (and (setq next (kcell-view:next nil label-sep-len))
2597                   (< start-indent (kcell-view:indent nil label-sep-len))))
2598       (cond (next
2599              (goto-char (progn (kcell-view:previous nil label-sep-len)
2600                                (kcell-view:end))))
2601             ;; Avoid skipping too far at end of file.
2602             ((re-search-forward "[\n\r][\n\r]" nil t))
2603             (t (goto-char (point-max))))
2604       (if omit-end-newlines (skip-chars-backward "\n\r"))
2605       (point))))
2606
2607 (defun kotl-mode:tree-start ()
2608   "Return beginning of line position preceding current cell's start point."
2609   (save-excursion (goto-char (kcell-view:start)) (beginning-of-line)
2610                   (point)))
2611
2612 (defun kotl-mode:line-move (arg)
2613   "Move point ARG visible lines forward within an outline."
2614   (if (not (integerp selective-display))
2615       (forward-line arg)
2616     ;; Move by arg lines, but ignore invisible ones.
2617     (while (> arg 0)
2618       (vertical-motion 1)
2619       (forward-char -1)
2620       (forward-line 1)
2621       (setq arg (1- arg)))
2622     (while (< arg 0)
2623       (vertical-motion -1)
2624       (beginning-of-line)
2625       (setq arg (1+ arg))))
2626   (move-to-column (or goal-column temporary-goal-column))
2627   nil)
2628
2629 (defun kotl-mode:print-attributes (kview)
2630   "Print to the `standard-output' stream the attributes of the current visible kcell. 
2631 Takes argument KVIEW (so it can be used with 'kview:map-tree' and so that
2632 KVIEW is bound correctly) but always operates upon the current view."
2633   ;; Move to start of visible cell to avoid printing attributes for an
2634   ;; invisible kcell which point may be over.
2635   ;; Print first line of cell for reference.
2636   (save-excursion
2637     (princ
2638      (buffer-substring (progn (beginning-of-line) (point))
2639                        (progn (kview:end-of-actual-line)
2640                               (point)))))
2641   (terpri)
2642   (hattr:report (kcell:plist (kcell-view:cell)))
2643   (terpri))
2644
2645 (defun kotl-mode:set-temp-goal-column ()
2646   (if (not (or (eq last-command 'next-line)
2647                (eq last-command 'previous-line)))
2648       (setq temporary-goal-column
2649             (if (and track-eol (eolp)
2650                      ;; Don't count beg of empty line as end of line
2651                      ;; unless we just did explicit end-of-line.
2652                      (or (not (bolp)) (eq last-command 'end-of-line)))
2653                 9999
2654               (current-column)))))
2655
2656 (defun kotl-mode:to-valid-position (&optional backward-p)
2657   "Move point to the nearest non-read-only position within current koutline view.
2658 With optional BACKWARD-P, move backward if possible to get to valid position."
2659   (if (kview:valid-position-p)
2660       nil
2661     (let ((label-sep-len (kview:label-separator-length kview)))
2662       (cond ((kotl-mode:bobp)
2663              (goto-char (kcell-view:start nil label-sep-len)))
2664             ((kotl-mode:eobp)
2665              (skip-chars-backward "\n\r"))
2666             (t (let ((indent (kcell-view:indent nil label-sep-len)))
2667                  (if (bolp)
2668                      (if backward-p
2669                          (skip-chars-backward "\n\r")
2670                        (skip-chars-forward "\n\r")))
2671                  (setq indent (kcell-view:indent nil label-sep-len))
2672                  (if (< (current-column) indent)
2673                      (move-to-column indent))))))))
2674
2675 (defun kotl-mode:transpose-lines-internal (start end)
2676   "Transpose lines at START and END markers within an outline.
2677 Leave point at end of line now residing at START."
2678   (if (and start end
2679            (kview:valid-position-p start)
2680            (kview:valid-position-p end))
2681       (let* ((pline (kotl-mode:delete-line start))
2682              mline)
2683         (goto-char end)
2684         (setq mline (kotl-mode:delete-line))
2685         (insert pline)
2686         (goto-char start)
2687         (insert mline))
2688     ;; Set non-point and non-mark markers to point nowhere before signalling
2689     ;; an error.
2690     (or (eq start (point-marker))
2691         (eq start (hypb:mark-marker t))
2692         (set-marker start nil))
2693     (or (eq end (point-marker))
2694         (eq end (hypb:mark-marker t))
2695         (set-marker start nil))
2696     (error "(kotl-mode:transpose-lines): Point or mark is at an invalid position")))
2697
2698 (defun kotl-mode:update-buffer ()
2699   "Update current view buffer in preparation for saving."
2700   (if (kview:is-p kview)
2701       (let ((mod-p (buffer-modified-p))
2702             (start (window-start)))
2703         (save-excursion
2704           (kfile:update)
2705           (set-buffer-modified-p mod-p))
2706         (set-window-start nil (max (point-min) start) t)
2707         nil)))
2708
2709 ;;; ----
2710
2711 (defvar kotl-mode-map nil
2712   "Keymap containing koutliner editing and viewing commands.")
2713 (if kotl-mode-map
2714     nil
2715   (setq kotl-mode-map
2716         (if (string-match "XEmacs\\|Lucid" emacs-version)
2717             (make-keymap)
2718           (copy-keymap text-mode-map)))
2719   ;; Overload edit keys to deal with structure and labels.
2720   (let (local-cmd)
2721     (mapcar 
2722      (if (string-match "XEmacs\\|Lucid" emacs-version)
2723          ;; XEmacs
2724          (function
2725           (lambda (cmd)
2726             (setq local-cmd (intern-soft
2727                              (concat "kotl-mode:" (symbol-name cmd))))
2728             ;; Only bind key locally if kotl-mode local-cmd has already
2729             ;; been defined and cmd is a valid function.
2730             (if (and local-cmd (fboundp cmd))
2731                 (progn
2732                   ;; Make local-cmd have the same property list as cmd,
2733                   ;; e.g. so pending-delete property is the same.
2734                   (setplist local-cmd (symbol-plist cmd)) 
2735                   (mapcar
2736                    (function
2737                     (lambda (key) (define-key kotl-mode-map key local-cmd)))
2738                    (where-is-internal cmd))))))
2739        ;; GNU Emacs 19
2740        (function
2741         (lambda (cmd)
2742           (setq local-cmd (intern-soft
2743                            (concat "kotl-mode:" (symbol-name cmd))))
2744           ;; Only bind key locally if kotl-mode local-cmd has already
2745           ;; been defined and cmd is a valid function.
2746           (if (and local-cmd (fboundp cmd))
2747               (progn
2748                 ;; Make local-cmd have the same property list as cmd,
2749                 ;; e.g. so pending-delete property is the same.
2750                 (setplist local-cmd (symbol-plist cmd)) 
2751                 (substitute-key-definition
2752                  cmd local-cmd kotl-mode-map global-map))))))
2753      '(
2754        back-to-indentation
2755        backward-char
2756        backward-delete-char
2757        backward-delete-char-untabify
2758        backward-kill-word
2759        backward-para
2760        backward-paragraph
2761        backward-sentence
2762        backward-word
2763        beginning-of-buffer
2764        beginning-of-line
2765        copy-region-as-kill
2766        copy-to-register
2767        delete-blank-lines
2768        delete-backward-char
2769        delete-char
2770        delete-horizontal-space
2771        delete-indentation
2772        end-of-buffer
2773        end-of-line
2774        fill-paragraph
2775        fill-paragraph-or-region
2776        ;; cursor keys
2777        fkey-backward-char
2778        fkey-forward-char
2779        fkey-next-line
2780        fkey-previous-line
2781        ;;
2782        forward-char
2783        forward-word
2784        forward-para
2785        forward-paragraph
2786        forward-sentence
2787        insert-buffer
2788        insert-file
2789        insert-register
2790        just-one-space
2791        kill-word
2792        kill-line
2793        kill-region
2794        kill-ring-save
2795        kill-sentence
2796        mark-paragraph
2797        mark-whole-buffer
2798        newline
2799        newline-and-indent
2800        next-line
2801        open-line
2802        previous-line
2803        scroll-down
2804        scroll-up
2805        set-fill-prefix
2806        transpose-chars
2807        transpose-lines
2808        transpose-paragraphs
2809        transpose-sentences
2810        transpose-words
2811        yank
2812        yank-pop
2813        zap-to-char
2814        )))
2815
2816 \f
2817   ;; kotl-mode keys
2818   (define-key kotl-mode-map "\C-c@"     'kotl-mode:mail-tree)
2819   (define-key kotl-mode-map "\C-c+"     'kotl-mode:append-cell)
2820   (define-key kotl-mode-map "\C-c,"     'kotl-mode:beginning-of-cell)
2821   (define-key kotl-mode-map "\C-c."     'kotl-mode:end-of-cell)
2822   (define-key kotl-mode-map "\C-c<"     'kotl-mode:first-sibling)
2823   (define-key kotl-mode-map "\C-c>"     'kotl-mode:last-sibling)
2824   (define-key kotl-mode-map "\C-c^"     'kotl-mode:beginning-of-tree)
2825   (define-key kotl-mode-map "\C-c$"     'kotl-mode:end-of-tree)
2826   (define-key kotl-mode-map "\C-ca"     'kotl-mode:add-child)
2827   (define-key kotl-mode-map "\C-c\C-a"  'kotl-mode:show-all)
2828   (define-key kotl-mode-map "\C-cb"     'kvspec:toggle-blank-lines)
2829   (define-key kotl-mode-map "\C-c\C-b"  'kotl-mode:backward-cell)
2830   (define-key kotl-mode-map "\C-cc"     'kotl-mode:copy-after)
2831   (define-key kotl-mode-map "\C-c\C-c"  'kotl-mode:copy-before)
2832   (define-key kotl-mode-map "\C-c\M-c"  'kotl-mode:copy-to-buffer)
2833   (define-key kotl-mode-map "\C-cd"     'kotl-mode:down-level)
2834   (define-key kotl-mode-map "\C-c\C-d"  'kotl-mode:down-level)
2835   (define-key kotl-mode-map "\C-ce"     'kotl-mode:exchange-cells)
2836   (define-key kotl-mode-map "\C-c\C-f"  'kotl-mode:forward-cell)
2837   (define-key kotl-mode-map "\C-cg"     'kotl-mode:goto-cell)
2838   (define-key kotl-mode-map "\C-ch"     'kotl-mode:cell-help)
2839   (define-key kotl-mode-map "\C-c\C-h"  'kotl-mode:hide-tree)
2840   (define-key kotl-mode-map "\M-\C-h"   'kotl-mode:hide-subtree)
2841   ;; Override this global binding for set-selective-display with a similar
2842   ;; function appropriate for kotl-mode.
2843   (define-key kotl-mode-map "\C-x$"     'kotl-mode:hide-sublevels)
2844   (define-key kotl-mode-map "\C-i"      'kotl-mode:demote-tree)
2845   (define-key kotl-mode-map "\M-\C-i"   'kotl-mode:promote-tree)
2846   (define-key kotl-mode-map "\C-j"      'kotl-mode:add-cell)
2847   (define-key kotl-mode-map "\M-j"      'kotl-mode:fill-paragraph)
2848   (define-key kotl-mode-map "\C-c\M-j"  'kotl-mode:fill-cell)
2849   (define-key kotl-mode-map "\M-\C-j"   'kotl-mode:fill-tree)
2850   (define-key kotl-mode-map "\C-c\C-k"  'kotl-mode:kill-tree)
2851   (define-key kotl-mode-map "\C-ck"     'kotl-mode:kill-contents)
2852   (define-key kotl-mode-map "\C-c\C-i"  'kotl-mode:set-cell-attribute)
2853   (define-key kotl-mode-map "\C-cl"     'klink:create)
2854   (define-key kotl-mode-map "\C-c\C-l"  'kview:set-label-type)
2855   (define-key kotl-mode-map "\C-c\M-l"  'kview:set-label-separator)
2856   (define-key kotl-mode-map "\C-m"      'kotl-mode:newline)
2857   (define-key kotl-mode-map "\C-cm"     'kotl-mode:move-after)
2858   (define-key kotl-mode-map "\C-c\C-m"  'kotl-mode:move-before)
2859   (define-key kotl-mode-map "\C-c\C-n"  'kotl-mode:next-cell)
2860   (define-key kotl-mode-map "\C-c\C-o"  'kotl-mode:overview)
2861   (define-key kotl-mode-map "\C-c\C-p"  'kotl-mode:previous-cell)
2862   (define-key kotl-mode-map "\C-cp"     'kotl-mode:add-parent)
2863   (if (memq (global-key-binding "\M-q") '(fill-paragraph
2864                                           fill-paragraph-or-region))
2865       (progn
2866         (define-key kotl-mode-map "\C-c\M-q" 'kotl-mode:fill-cell)
2867         (define-key kotl-mode-map "\M-\C-q"  'kotl-mode:fill-tree)))
2868   (define-key kotl-mode-map "\C-cs"     'kotl-mode:split-cell)
2869   (define-key kotl-mode-map "\C-c\C-s"  'kotl-mode:show-tree)
2870   (define-key kotl-mode-map "\C-c\C-\\" 'kotl-mode:show-tree)
2871   (define-key kotl-mode-map "\M-s"      'kotl-mode:center-line)
2872   (define-key kotl-mode-map "\M-S"      'kotl-mode:center-paragraph)
2873   (define-key kotl-mode-map "\C-ct"     'kotl-mode:transpose-cells)
2874   (define-key kotl-mode-map "\C-c\C-t"  'kotl-mode:top-cells)
2875   (define-key kotl-mode-map "\C-cu"     'kotl-mode:up-level)
2876   (define-key kotl-mode-map "\C-c\C-u"  'kotl-mode:up-level)
2877   (define-key kotl-mode-map "\C-c\C-v"  'kvspec:activate)
2878   (define-key kotl-mode-map "\C-x\C-w"  'kfile:write))
2879
2880 (easy-menu-define kotl-mode-menu kotl-mode-map "Kotl Mode Menu." kmenu:menu)
2881
2882 (provide 'kotl-mode)
2883
2884 ;;; kotl-mode.el ends here