Initial Commit
[packages] / xemacs-packages / hyperbole / kotl / kview.el
1 ;;; kview.el --- Display handling of koutlines.
2
3 ;; Copyright (C) 2008 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: 
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 (mapcar 'require '(klabel kfill hypb))
36
37 ;;;
38 ;;; Public variables
39 ;;;
40
41 (set-default 'kview nil)
42
43 (defvar kview:default-blank-lines t
44   "*Default setting of whether to show blank lines between koutline cells.
45 T means show them, nil means don't show them.")
46
47 (defvar kview:default-levels-to-show 0
48   "*Default number of cell levels to show.  0 means all levels.")
49
50 (defvar kview:default-lines-to-show 0
51   "*Default number of lines per cell to show.  0 means all lines.")
52
53
54 (defvar kview:default-label-min-width 4
55   "*Minimum width to which to pad labels in a kotl view.
56 Labels are padded with spaces on the left.")
57
58 (defvar kview:default-label-separator "  "
59   "*Default string of characters to insert between label and contents of a koutline cell.")
60
61 (defvar kview:default-label-type 'alpha
62   "*Default label-type to use for new koutlines.
63 It must be one of the following symbols:
64   no              for no labels
65   id              for permanent idstamp labels, e.g. 001, 002, etc.
66   alpha           for '1a2' full alphanumeric labels
67   legal           for '1.1.2' labels
68   partial-alpha   for partial alphanumeric labels, e.g. '2' for node '1a2'
69   star            for multi-star labeling, e.g. '***'.")
70
71 (defvar kview:default-level-indent 3
72   "*Default number of spaces to indent each succeeding level in koutlines.")
73
74 ;;;
75 ;;; Public functions
76 ;;;
77
78 ;;;
79 ;;; kcell-view
80 ;;;
81
82 (defun kcell-view:backward (&optional visible-p label-sep-len)
83   "Move to start of the prior cell at the same level as the current cell.
84 With optional VISIBLE-P, consider only visible cells.
85 Return t unless no such cell."
86   (or label-sep-len (setq label-sep-len
87                           (kview:label-separator-length kview)))
88   (let ((opoint (point))
89         (found) (done)
90         (curr-indent 0)
91         (start-indent (kcell-view:indent nil label-sep-len)))
92     (while (and (not (or found done))
93                 (kcell-view:previous visible-p label-sep-len))
94       (if (bobp)
95           (progn (setq done t)
96                  (goto-char opoint))
97         (setq curr-indent (kcell-view:indent nil label-sep-len))
98         (cond ((= curr-indent start-indent)
99                (goto-char (kcell-view:start nil label-sep-len))
100                (setq found t))
101               ((< curr-indent start-indent)
102                ;; Went past start of this tree without a match.
103                (setq done t)
104                (goto-char opoint))
105               ;; else go to prior node
106               )))
107     found))
108
109 (defun kview:beginning-of-actual-line ()
110   "Go to the beginning of the current line whether collapsed or not."
111   (if (re-search-backward "[\n\r]" nil 'move)
112       (forward-char 1)))
113
114 (defun kcell-view:cell (&optional pos)
115   "Return kcell at optional POS or point."
116   (kproperty:get (kcell-view:plist-point pos) 'kcell))
117
118 (defun kcell-view:child (&optional visible-p label-sep-len)
119   "Move to start of current cell's child.
120 With optional VISIBLE-P, consider only visible children.
121 Return t unless cell has no matching child.
122 Optional LABEL-SEP-LEN is the length of the separation between
123 a cell's label and the start of its contents."
124   (let* ((opoint (point))
125          (prev-indent (kcell-view:indent nil label-sep-len))
126          (next (kcell-view:next visible-p label-sep-len)))
127     (or label-sep-len (setq label-sep-len
128                             (kview:label-separator-length kview)))
129     ;; Since kcell-view:next leaves point at the start of a cell, the cell's
130     ;; indent is just the current-column of point.
131     (if (and next (> (current-column) prev-indent))
132         t
133       ;; Move back to previous point and return nil.
134       (goto-char opoint)
135       nil)))
136
137 (defun kcell-view:child-p (&optional pos visible-p label-sep-len)
138   "Return t if cell at optional POS or point has a child.
139 With optional VISIBLE-P, consider only visible children.
140 Optional LABEL-SEP-LEN is the length of the separation between
141 a cell's label and the start of its contents."
142   (save-excursion
143     (if pos (goto-char pos))
144     (kcell-view:child visible-p label-sep-len)))
145
146 (defun kcell-view:collapse (&optional pos label-sep-len)
147   "Collapse cell at optional POS or point within the current view."
148   (save-excursion
149     (goto-char (kcell-view:start pos label-sep-len))
150     (subst-char-in-region (point) (kcell-view:end-contents) ?\n ?\r t)))
151
152 (defun kcell-view:collapsed-p (&optional pos label-sep-len)
153   "Return t if cell at optional POS or point is collapsed within the current view."
154   (save-excursion
155     (goto-char (kcell-view:start pos label-sep-len))
156     (if (search-forward "\r" (kcell-view:end-contents) t)
157         t)))
158
159 (defun kcell-view:contents (&optional pos)
160   "Return contents of cell at optional POS or point."
161   (save-excursion
162     (if pos (goto-char pos))
163     (let ((indent (kcell-view:indent))
164           (start (kcell-view:start))
165           (end (kcell-view:end-contents)))
166       ;; Remove indentation from all but first line.
167       (hypb:replace-match-string
168        (concat "\\([\n\r]\\)" (make-string indent ?\ ))
169        (buffer-substring start end) "\\1"))))
170
171 (defun kcell-view:create (kview cell level klabel &optional no-fill)
172   "Insert into KVIEW at point, CELL at LEVEL (1 = first level) with KLABEL.
173 Optional NO-FILL non-nil suppresses filling of cell's contents upon insertion
174 or movement."
175   (if (= (kcell:idstamp cell) 0)
176       nil
177     (or no-fill (setq no-fill (kcell:get-attr cell 'no-fill)))
178     (let* ((label-min-width (kview:label-min-width kview))
179            (label-fmt (format "%%%ds" label-min-width))
180            (label (if (string= klabel "") "" (format label-fmt klabel)))
181            (label-separator (if (string= klabel "") " "
182                               (kview:label-separator kview)))
183            (mult-line-indent (* (1- level) (kview:level-indent kview)))
184            (thru-label (+ mult-line-indent label-min-width
185                           (length label-separator)))
186            (old-point (point))
187            (fill-prefix (make-string thru-label ?\ ))
188            contents
189            new-point)
190       (if no-fill (kcell:set-attr cell 'no-fill t))
191       (insert fill-prefix)
192       (setq contents (kview:insert-contents cell nil no-fill fill-prefix))
193       ;; Insert lines to separate cell from next.
194       (insert (if (or no-fill (equal contents ""))
195                   "\n\n" "\n"))
196       (if (kview:get-attr kview 'blank-lines)
197           nil
198         ;; Make blank lines invisible.
199         (kproperty:put (1- (point)) (min (point) (point-max))
200                        '(invisible t)))
201       (kfile:narrow-to-kcells)
202       (setq new-point (point))
203       (goto-char old-point)
204       ;; Delete leading spaces used to get fill right in first cell
205       ;; line.  Replace it with label.
206       (delete-char thru-label)
207       (insert (format
208                (format "%%%ds" (- thru-label (length label-separator)))
209                label))
210       (setq old-point (point))
211       (insert label-separator)
212       (goto-char old-point)
213       ;; Add cell's attributes to the text property list at point.
214       (kproperty:set 'kcell cell)
215       (goto-char new-point))))
216
217 (defun kcell-view:end (&optional pos)
218   "Return end position of cell from optional POS or point.
219 Includes blank lines following cell contents."
220   (or pos (setq pos (point)))
221   (save-excursion
222     (or (re-search-forward "[\n\r][\n\r]" nil t)
223         (point-max))))
224
225 (defun kcell-view:end-contents (&optional pos)
226   "Return end position of cell contents from optional POS or point.
227 Excludes blank lines following cell contents."
228   (save-excursion
229     (if pos (goto-char pos))
230     (goto-char (kcell-view:end))
231     (skip-chars-backward "\n\r")
232     (point)))
233
234 (defun kcell-view:expand (&optional pos label-sep-len)
235   "Expand cell at optional POS or point within the current view."
236   (save-excursion
237     (goto-char (kcell-view:start pos label-sep-len))
238     (subst-char-in-region (point) (kcell-view:end-contents) ?\r ?\n t)))
239
240 (defun kcell-view:forward (&optional visible-p label-sep-len)
241   "Move to start of the following cell at the same level as the current cell.
242 With optional VISIBLE-P, consider only visible cells.
243 Return t unless no such cell."
244   (or label-sep-len (setq label-sep-len
245                           (kview:label-separator-length kview)))
246   (let ((opoint (point))
247         (found) (done)
248         (curr-indent 0)
249         (start-indent (kcell-view:indent nil label-sep-len)))
250     (while (and (not (or found done))
251                 (kcell-view:next visible-p label-sep-len))
252       (setq curr-indent (kcell-view:indent nil label-sep-len))
253       (cond ((= curr-indent start-indent)
254              (goto-char (kcell-view:start nil label-sep-len))
255              (setq found t))
256             ((< curr-indent start-indent)
257              ;; Went past end of this tree without a match.
258              (setq done t)
259              (goto-char opoint))
260             ;; else go to following node
261             ))
262     ;; If didn't find a match, return to original point.
263     (or found (goto-char opoint))
264     found))
265
266 (defun kcell-view:get-attr (attribute &optional pos)
267   "Return ATTRIBUTE's value for current cell or cell at optional POS."
268   (save-excursion
269     (if pos (goto-char pos))
270     (kcell:get-attr (kcell-view:cell) attribute)))
271
272 (defun kcell-view:idstamp (&optional pos)
273   "Return idstamp string of cell at optional POS or point."
274   (save-excursion
275     (if pos (goto-char pos))
276     (format "0%s" (or (kcell:idstamp (kcell-view:cell)) ""))))
277
278 (defun kcell-view:indent (&optional pos label-sep-len)
279   "Return indentation of cell at optional POS or point.
280 Optional LABEL-SEP-LEN is the view-specific length of the separator between a
281 cell's label and the start of its contents."
282   (+ (save-excursion
283        (kcell-view:to-label-end pos)
284        (current-column))
285      (or label-sep-len (kview:label-separator-length kview))))
286
287 (defun kcell-view:label (&optional pos)
288   "Return displayed label string of cell at optional POS or point.
289 If labels are off, return cell's idstamp as a string."
290   (save-excursion
291     (if pos (goto-char pos))
292     (let ((label-type (kview:label-type kview)))
293       (if (eq label-type 'no)
294           (kcell-view:idstamp)
295         (kcell-view:to-label-end)
296         (buffer-substring (point) (progn (skip-chars-backward "^ \t\n\r")
297                                          (point)))))))
298
299 (defun kcell-view:level (&optional pos label-sep-len indent)
300   "Return cell level relative to top cell of the outline for current cell or one at optional POS.
301 0 = top cell level, 1 = 1st level in outline.
302 Optional LABEL-SEP-LEN is length of spaces between a cell label and its the
303 start of its body in the current view.  Optional INDENT is the indentation in
304 characters of the cell whose level is desired."
305   (or label-sep-len (setq label-sep-len (kview:label-separator-length kview)))
306   (save-excursion
307     (if pos (goto-char pos))
308     (/ (- (or indent (kcell-view:indent nil label-sep-len)) label-sep-len)
309        (kview:level-indent kview))))
310
311 (defun kcell-view:line (&optional pos)
312   "Return contents of cell line at point or optional POS as a string."
313   (save-excursion
314     (if pos (goto-char pos))
315     (if (kview:valid-position-p)
316         (buffer-substring
317          (kotl-mode:beginning-of-line)
318          (kotl-mode:end-of-line))
319       (error "(kcell-view:line): Invalid position, '%d'" (point)))))
320
321 (defun kcell-view:next (&optional visible-p label-sep-len)
322   "Move to start of next cell within current view.
323 With optional VISIBLE-P, consider only visible cells.
324 Return t unless no next cell."
325   (let ((opoint (point))
326         pos)
327     ;;
328     ;; If a subtree is collapsed, be sure we end up at the start of a visible
329     ;; cell rather than within an invisible one.
330     (if visible-p
331         (progn (goto-char (kcell-view:end-contents)) (end-of-line)))
332     (setq pos (kproperty:next-single-change (point) 'kcell))
333     (if (or (null pos)
334             (if (goto-char pos) (kotl-mode:eobp)))
335         (progn (goto-char opoint)
336                nil)
337       (goto-char (kcell-view:start nil label-sep-len))
338       (not (eq opoint (point))))))
339
340 (defun kcell-view:operate (function &optional start end)
341   "Invoke FUNCTION with view restricted to current cell contents.
342 Optional START and END are start and endpoints of cell to use."
343   (save-restriction
344     (narrow-to-region (or start (kcell-view:start))
345                       (or end (kcell-view:end-contents)))
346     (funcall function)))
347
348 (defun kcell-view:parent (&optional visible-p label-sep-len)
349   "Move to start of current cell's parent within current view.
350 If parent is top cell, move to first cell within view and return 0.
351 Otherwise, return t unless optional VISIBLE-P is non-nil and the parent cell
352 is not part of the current view."
353   (or label-sep-len (setq label-sep-len (kview:label-separator-length kview)))
354   (let ((opoint (point))
355         (parent-level (1- (kcell-view:level nil label-sep-len))))
356     (if (= parent-level 0) ;; top cell
357         (progn (goto-char (point-min))
358                (goto-char (kcell-view:start nil label-sep-len))
359                0)
360       ;; Skip from point back past any siblings
361       (while (kcell-view:backward visible-p label-sep-len))
362       ;; Move back to parent.
363       (if (kcell-view:previous visible-p label-sep-len)
364           t
365         ;; Move back to previous point and return nil.
366         (goto-char opoint)
367         nil))))
368
369 (defun kcell-view:previous (&optional visible-p label-sep-len)
370   "Move to start of previous cell within current view.
371 With optional VISIBLE-P, consider only visible cells.
372 Return t unless no previous cell."
373   (let ((opoint (point))
374         (pos (point)))
375     (goto-char (kcell-view:start nil label-sep-len))
376     ;;
377     ;; If a subtree is collapsed, be sure we end up at the start of a visible
378     ;; cell rather than within an invisible one.
379     (if visible-p
380         (beginning-of-line)
381       (if (setq pos (kproperty:previous-single-change (point) 'kcell))
382           (goto-char pos)))
383     (if (and pos (not (kotl-mode:bobp))
384              (setq pos (kproperty:previous-single-change (point) 'kcell)))
385         (progn (goto-char pos)
386                (skip-chars-backward "\n\r")
387                (if visible-p (beginning-of-line))
388                (goto-char (kcell-view:start nil label-sep-len))
389                (not (eq opoint (point))))
390       ;; No previous cell exists
391       (goto-char opoint)
392       nil)))
393
394 (defun kcell-view:plist (&optional pos)
395   "Return attributes associated with cell at optional POS or point."
396   (kcell:plist (kcell-view:cell pos)))
397
398 (defun kcell-view:plist-point (&optional pos)
399   "Return buffer position of attributes associated with cell at optional POS or point."
400   (save-excursion (1+ (kcell-view:to-label-end pos))))
401
402 (defun kcell-view:to-label-end (&optional pos)
403   "Move point after end of current cell's label and return point."
404   (if pos (goto-char pos))
405   (kview:end-of-actual-line)
406   (cond ((null kview)
407          (error "(kcell-view:to-label-end): Invalid kview; try {M-x kotl-mode RET} to fix it."))
408         (klabel-type:changing-flag
409          ;; When changing from one label type to another, e.g. alpha to
410          ;; legal, we can't depend on the label being of the type given by
411          ;; the kview, so use kcell properties to find label end.
412          (if (kproperty:get (1- (point)) 'kcell)
413              nil
414            ;; If not at beginning of cell contents, move there.
415            (goto-char (kproperty:previous-single-change (point) 'kcell)))
416          ;; Then move to end of label via embedded kcell property.
417          (goto-char (kproperty:previous-single-change (point) 'kcell)))
418         ((funcall (kview:get-attr kview 'to-label-end))
419          (point))
420         (t (error "(kcell-view:to-label-end): Can't find end of current cell's label"))))
421
422 (defun kcell-view:reference (&optional pos relative-dir)
423   "Return a reference to the kcell at optional POS or point for use in a link.
424 The reference is a string of the form, \"<kcell-file, cell-ref>\" where
425 cell-ref is as described in the documentation for 'kcell:ref-to-id'.
426 Kcell-file is made relative to optional RELATIVE-DIR before it is returned."
427   (format "<%s, %s=%s>" (hpath:relative-to buffer-file-name relative-dir)
428           (kcell-view:label pos) (kcell-view:idstamp pos)))
429
430 (defun kcell-view:remove-attr (attribute &optional pos)
431   "Remove ATTRIBUTE, if any, for current cell or cell at optional POS."
432   (interactive "*SAttribute to remove: ")
433   (save-excursion
434     (if pos (goto-char pos))
435     (let ((kcell (kcell:remove-attr (kcell-view:cell) attribute)))
436       (if (interactive-p)
437           (message "Cell <%s> now has no %s attribute."
438                    (kcell-view:label) attribute))
439       kcell)))
440
441 (defun kcell-view:set-attr (attribute value &optional pos)
442   "Set ATTRIBUTE's VALUE for current cell or cell at optional POS and return the cell."
443   (save-excursion
444     (if pos (goto-char pos))
445     ;; Returns kcell.
446     (kcell:set-attr (kcell-view:cell) attribute value)))
447
448 (defun kcell-view:set-cell (kcell)
449   "Attach KCELL property to cell at point."
450   (save-excursion
451     (kcell-view:to-label-end)
452     (kproperty:set 'kcell kcell)))
453
454 (defun kcell-view:sibling-p (&optional pos visible-p label-sep-len)
455   "Return t if cell at optional POS or point has a successor.
456 With optional VISIBLE-P, consider only visible siblings."
457   (save-excursion
458     (if pos (goto-char pos))
459     (kcell-view:forward visible-p label-sep-len)))
460
461 (defun kcell-view:start (&optional pos label-sep-len)
462   "Return start position of cell contents from optional POS or point."
463   (save-excursion
464     (+ (kcell-view:to-label-end pos)
465        (or label-sep-len (kview:label-separator-length kview)))))
466
467 ;;;
468 ;;; kview - one view per buffer, multiple views per kotl
469 ;;;
470
471 (defun kview:add-cell (klabel level &optional contents prop-list no-fill)
472   "Create a new cell with full KLABEL and add it at point at LEVEL within outline.
473 1 = first level.  Optional cell CONTENTS and PROP-LIST may also be given, as
474 well as NO-FILL which skips filling of any CONTENTS.
475 Return new cell.  This function does not renumber any other cells."
476   (let ((new-cell (kcell:create contents (kview:id-increment kview)
477                                 prop-list)))
478     (kcell-view:create kview new-cell level klabel no-fill)
479     new-cell))
480
481 (defun kview:buffer (kview)
482   "Return kview's buffer or nil if argument is not a kview."
483   (if (kview:is-p kview)
484       (get-buffer (kview:get-attr kview 'view-buffer-name))))
485
486 (defun kview:create (buffer-name
487                          &optional id-counter label-type level-indent
488                          label-separator label-min-width blank-lines
489                          levels-to-show lines-to-show)
490   "Return a new kview for BUFFER-NAME.
491 Optional ID-COUNTER is the maximum permanent id previously given out in this
492 outline.  Optional LABEL-TYPE, LEVEL-INDENT, LABEL-SEPARATOR, LABEL-MIN-WIDTH,
493 BLANK-LINES, LEVELS-TO-SHOW, and LINES-TO-SHOW may also be given, otherwise default values are used.
494
495   See documentation of:
496  'kview:default-label-type' for LABEL-TYPE,
497  'kview:default-level-indent' for LEVEL-INDENT,
498  'kview:default-label-separator' for LABEL-SEPARATOR,
499  'kview:default-label-min-width' for LABEL-MIN-WIDTH,
500  'kview:default-blank-lines' for BLANK-LINES,
501  'kview:default-levels-to-show' for LEVELS-TO-SHOW,
502  'kview:default-lines-to-show' for LINES-TO-SHOW."
503
504   (let ((buf (get-buffer buffer-name)))
505     (cond ((null buf)
506            (error "(kview:create): No such buffer, '%s'." buffer-name))
507           ((or (null id-counter) (= id-counter 0))
508            (setq id-counter 0))
509           ((not (integerp id-counter))
510            (error "(kview:create): 2nd arg, '%s', must be an integer." id-counter)))
511     (set-buffer buf)
512     (if (and (boundp 'kview) (eq (kview:buffer kview) buf))
513         ;; Don't recreate view if it exists.
514         nil
515       (make-local-variable 'kview)
516       (setq kview
517             (list 'kview 'plist
518                   (list 'view-buffer-name buffer-name
519                         'top-cell
520                         (kcell:create-top buffer-file-name id-counter)
521                         'label-type (or label-type kview:default-label-type)
522                         'label-min-width (or label-min-width
523                                              kview:default-label-min-width)
524                         'label-separator (or label-separator
525                                              kview:default-label-separator)
526                         'label-separator-length
527                         (length (or label-separator
528                                     kview:default-label-separator))
529                         'level-indent (or level-indent
530                                           kview:default-level-indent)
531                         'blank-lines
532                         (or blank-lines kview:default-blank-lines)
533                         'levels-to-show
534                         (or levels-to-show kview:default-levels-to-show)
535                         'lines-to-show
536                         (or lines-to-show kview:default-lines-to-show)
537 )))
538       (kview:set-functions (or label-type kview:default-label-type)))
539     kview))
540
541 ;;; Using this stimulates an GNU Emacs V19.19 bug in text-property handling,
542 ;;  visible when one deletes a sibling cell and then deletes the prior cell,
543 ;;  the following cell is left with a different idstamp and its label
544 ;;  displays as "0".  Using delete-char here would solve the problem but we
545 ;;  suggest you upgrade to a newer version of GNU Emacs in which the bug is
546 ;;  fixed.
547 (defun kview:delete-region (start end)
548   "Delete cells between START and END points from current view."
549   (delete-region start end))
550
551 (defun kview:end-of-actual-line ()
552   "Go to the end of the current line whether collapsed or not."
553   (if (re-search-forward "[\n\r]" nil 'move)
554       (backward-char 1)))
555
556 (defun kview:fill-region (start end &optional kcell justify)
557   "Fill region between START and END within current view.
558 With optional KCELL, assume START and END delimit that cell's contents.
559 With optional JUSTIFY, justify region as well.
560 Fill-prefix must be a string of spaces the length of this cell's indent, when
561 this function is called."
562   (let ((opoint (set-marker (make-marker) (point)))
563         (label-sep-len (kview:label-separator-length kview))
564         (continue t)
565         prev-point)
566     (goto-char start)
567     (while continue
568       (if (kcell:get-attr (or kcell (kcell-view:cell)) 'no-fill)
569           (setq continue (kcell-view:next nil label-sep-len))
570         (fill-paragraph justify t)
571         (setq prev-point (point))
572         (forward-paragraph)
573         (re-search-forward "[^ \t\n\r]" nil t))
574       (setq continue (and continue
575                           (/= (point) prev-point)
576                           (< (point) (min end (point-max))))))
577     ;; Return to original point.
578     (goto-char opoint)
579     (set-marker opoint nil)))
580
581 (cond ((and hyperb:xemacs-p (or (>= emacs-minor-version 12)
582                                 (> emacs-major-version 19)))
583        (defun kview:goto-cell-id (id-string)
584          "Move point to start of cell with idstamp ID-STRING and return t, else nil."
585          (let ((cell-id (string-to-number id-string))
586                label-end kcell)
587            (setq label-end
588                  (map-extents
589                   (function (lambda (extent unused)
590                               (setq kcell (extent-property extent 'kcell))
591                               (if (= (kcell:idstamp kcell) cell-id)
592                                   (extent-end-position extent))))
593                   nil nil nil nil nil 'kcell))
594            (if (null label-end)
595                nil
596              (goto-char label-end)
597              t))))
598       (hyperb:xemacs-p
599        (defun kview:goto-cell-id (id-string)
600          "Move point to start of cell with idstamp ID-STRING and return t, else nil."
601          (let ((cell-id (string-to-number id-string))
602                label-end kcell)
603            (setq label-end
604                  (map-extents
605                   (function (lambda (extent unused)
606                               (setq kcell (extent-property extent 'kcell))
607                               (and kcell (= (kcell:idstamp kcell) cell-id)
608                                    (extent-end-position extent))))))
609            (if (null label-end)
610                nil
611              (goto-char label-end)
612              t))))
613       ;; Emacs 19
614       (t (defun kview:goto-cell-id (id-string)
615            "Move point to start of cell with idstamp ID-STRING and return t, else nil."
616            (let ((cell-id (string-to-number id-string))
617                  (opoint (point))
618                  pos kcell)
619              (goto-char (point-min))
620              (while (and (setq pos
621                                (kproperty:next-single-change (point) 'kcell))
622                          (goto-char pos)
623                          (or (null (setq kcell (kproperty:get pos 'kcell)))
624                              (/= (kcell:idstamp kcell) cell-id))))
625              (if pos
626                  (progn
627                    (forward-char (kview:label-separator-length kview))
628                    t)
629                (goto-char opoint)
630                nil))))
631 )
632
633 (defun kview:id-increment (kview)
634   "Return next idstamp (an integer) for KVIEW."
635   (let* ((top-cell (kview:get-attr kview 'top-cell))
636          (counter (1+ (kcell:get-attr top-cell 'id-counter))))
637     (kcell:set-attr top-cell 'id-counter counter)
638     counter))
639
640 (defun kview:idstamp-to-label (permanent-id)
641   "Return relative label for cell with PERMANENT-ID within current kview."
642   (save-excursion
643     (if (kotl-mode:goto-cell permanent-id)
644         (kcell-view:label))))
645
646 (defun kview:insert-contents (kcell contents no-fill fill-prefix)
647   "Insert KCELL's CONTENTS into view at point and fill resulting paragraphs, unless NO-FILL is non-nil.
648 FILL-PREFIX is the indentation string for the current cell.
649 If CONTENTS is nil, get contents from KCELL.  Return contents inserted (this
650 value may differ from the value passed in.)"
651   (let ((start (point))
652         end)
653     (setq contents (or contents (kcell:contents kcell) ""))
654     (insert contents)
655     ;;
656     ;; Delete any extra newlines at end of cell contents.
657     (setq end (point))
658     (skip-chars-backward "\n\r")
659     (delete-region (point) end)
660     (setq end (point))
661     ;;
662     (save-restriction
663       (if no-fill
664           ;; Insert proper indent in all but the first line which has
665           ;; already been indented.
666           (progn
667             (narrow-to-region start end)
668             (goto-char (point-min))
669             (while (re-search-forward "[\n\r]" nil t)
670               (insert fill-prefix))
671             (goto-char (point-max)))
672         ;;
673         ;; Filling cell will insert proper indent on all lines.
674         (if (equal contents "")
675             nil
676           (goto-char start)
677           (beginning-of-line)
678           (narrow-to-region (point) end)
679           ;; Add fill-prefix to all but paragraph separator lines, so
680           ;; filling is done properly.
681           (while (re-search-forward "[\n\r][^\n\r]" nil t)
682             (forward-char -1) (insert fill-prefix))
683           (kview:fill-region start end kcell)
684           (goto-char (point-min))
685           ;; Now add fill-prefix to paragraph separator lines.
686           (while (re-search-forward "[\n\r][\n\r]" nil t)
687             (forward-char -1) (insert fill-prefix))
688           ;;
689           (goto-char (point-max))))))
690   contents)
691
692 (defun kview:is-p (object)
693   "Is OBJECT a kview?"
694   (if (listp object) (eq (car object) 'kview)))
695
696 (defun kview:kotl (kview)
697   "Return kview's kotl object or nil if argument is not a kview."
698   (if (kview:is-p kview)
699       (kview:get-attr kview 'kotl)))
700
701 (defun kview:label (klabel-function prev-label child-p)
702   "Return label string to display for current cell computed from KLABEL-FUNCTION, PREV-LABEL and CHILD-P."
703   (funcall klabel-function prev-label child-p))
704
705 (defun kview:label-function (kview)
706   "Return function which will return display label for current cell in KVIEW.
707 Function signature is: (func prev-label &optional child-p), where prev-label
708 is the display label of the cell preceding the current one and child-p is
709 non-nil if cell is to be the child of the preceding cell."
710   (kview:get-attr kview 'label-function))
711
712 (defun kview:label-min-width (kview)
713   "Return kview's label-min-width setting or nil if argument is not a kview.
714 See documentation for kview:default-label-min-width."
715   (if (kview:is-p kview)
716       (kview:get-attr kview 'label-min-width)))
717
718 (defun kview:label-separator (kview)
719   "Return kview's label-separator setting or nil if argument is not a kview.
720 See documentation for kview:default-label-separator."
721   (if (kview:is-p kview)
722       (kview:get-attr kview 'label-separator)))
723
724 (defun kview:label-separator-length (kview)
725   "Return kview's label-separator length or nil if argument is not a kview.
726 See documentation for kview:default-label-separator."
727   (kview:get-attr kview 'label-separator-length))
728
729 (defun kview:label-type (kview)
730   "Return kview's label-type setting or nil if argument is not a kview.
731 See documentation for kview:default-label-type."
732   (if (kview:is-p kview)
733       (kview:get-attr kview 'label-type)))
734
735 (defun kview:level-indent (kview)
736   "Return kview's level-indent setting or nil if argument is not a kview.
737 See documentation for kview:default-level-indent."
738   (if (kview:is-p kview)
739       (kview:get-attr kview 'level-indent)))
740
741 (defun kview:map-branch (func kview &optional first-p visible-p)
742   "Applies FUNC to the sibling trees from point forward within KVIEW and returns results as a list.
743 With optional FIRST-P non-nil, begins with first sibling in current branch.
744 With optional VISIBLE-P, considers only those sibling cells that are visible
745 in the view.
746
747 FUNC should take one argument, the kview local variable of the current
748 buffer or some other kview, and should operate upon the cell at point.
749
750 `Cell-indent' contains the indentation value of the first cell mapped when
751 FUNC is called so that it may test against this value.  `Label-sep-len'
752 contains the label separator length.
753
754 See also 'kview:map-siblings' and 'kview:map-tree'."
755     (save-excursion
756       (set-buffer (kview:buffer kview))
757       (let ((results)
758             (label-sep-len (kview:label-separator-length kview)))
759         (if first-p
760             ;; Move back to first predecessor at same level.
761             (while (kcell-view:backward t label-sep-len)))
762         (let ((cell-indent (kcell-view:indent nil label-sep-len)))
763           ;; Terminate when no further cells or when reach a cell at an equal
764           ;; or higher level in the kotl than the first cell that we processed.
765           (while (and (progn (setq results (cons (funcall func kview) results))
766                              (kcell-view:next visible-p label-sep-len))
767                       (> (kcell-view:indent nil label-sep-len) cell-indent))))
768         (nreverse results))))
769
770 (defun kview:map-siblings (func kview &optional first-p visible-p)
771   "Applies FUNC to the sibling cells from point forward within KVIEW and returns results as a list.
772 With optional FIRST-P non-nil, begins with first sibling in current branch.
773 With optional VISIBLE-P, considers only those sibling cells that are visible
774 in the view.
775
776 FUNC should take one argument, the kview local variable of the current
777 buffer or some other kview, and should operate upon the cell at point.
778
779 `Cell-indent' contains the indentation value of the first cell mapped when
780 FUNC is called so that it may test against this value.  `Label-sep-len'
781 contains the label separator length.
782
783 See also 'kview:map-branch' and 'kview:map-tree'."
784     (save-excursion
785       (set-buffer (kview:buffer kview))
786       (let ((results)
787             (label-sep-len (kview:label-separator-length kview)))
788         (if first-p
789             ;; Move back to first predecessor at same level.
790             (while (kcell-view:backward t label-sep-len)))
791         (let ((cell-indent (kcell-view:indent nil label-sep-len)))
792           ;; Terminate when no further cells at same level.
793           (while (progn (setq results (cons (funcall func kview) results))
794                         (kcell-view:forward visible-p label-sep-len))))
795         (nreverse results))))
796
797 (defun kview:map-tree (func kview &optional top-p visible-p)
798   "Applies FUNC to the tree starting at point within KVIEW and returns results as a list.
799 With optional TOP-P non-nil, maps over all of kview's cells.
800 With optional VISIBLE-P, considers only those cells that are visible in the
801 view.
802
803 FUNC should take one argument, the kview local variable of the current
804 buffer or some other kview, and should operate upon the cell at point.
805
806 `Cell-indent' contains the indentation value of the first cell mapped when
807 FUNC is called so that it may test against this value.  `Label-sep-len'
808 contains the label separator length.
809
810 See also 'kview:map-branch' and 'kview:map-siblings'."
811   (let ((results)
812         (label-sep-len (kview:label-separator-length kview)))
813     (save-excursion
814       (set-buffer (kview:buffer kview))
815       (if top-p
816           (progn (goto-char (point-min))
817                  (kview:end-of-actual-line)
818                  ;; Terminate when no further cells to process.
819                  (while (progn 
820                           (setq results (cons (funcall func kview) results))
821                           (kcell-view:next visible-p label-sep-len))))
822         (let ((cell-indent (kcell-view:indent nil label-sep-len)))
823           ;; Terminate when no further cells or when reach a cell at an equal
824           ;; or higher level in the kotl than the first cell that we processed.
825           (while (and (progn (setq results (cons (funcall func kview) results))
826                              (kcell-view:next visible-p label-sep-len))
827                       (> (kcell-view:indent nil label-sep-len)
828                          cell-indent))))))
829     (nreverse results)))
830
831 (defun kview:move (from-start from-end to-start from-indent to-indent
832                    &optional copy-p fill-p)
833   "Move tree between FROM-START and FROM-END to TO-START, changing FROM-INDENT to TO-INDENT.
834 Copy tree if optional COPY-P is non-nil.  Refill cells if optional
835 FILL-P is non-nil.  Leave point at TO-START."
836   (let ((region (buffer-substring from-start from-end))
837         (new-start (set-marker (make-marker) to-start))
838         collapsed-cells expr new-end space)
839     ;;
840     ;; Move or copy tree region to new location.
841     (or copy-p (delete-region from-start from-end))
842     (goto-char new-start)
843     (insert region)
844     (setq new-end (point))
845     ;;
846     ;; Change indentation of tree cells.
847     (if (/= from-indent to-indent)
848         (save-restriction
849           (narrow-to-region new-start new-end)
850           ;; Store list of which cells are presently collapsed.
851           (setq collapsed-cells
852                 (kview:map-tree
853                  (function (lambda (view)
854                              ;; Use free variable label-sep-len bound in
855                              ;; kview:map-tree for speed.
856                              (kcell-view:collapsed-p nil label-sep-len)))
857                  kview t))
858           ;; Expand all cells.
859           (subst-char-in-region new-start new-end ?\r ?\n t)
860           ;;
861           (goto-char (point-min))
862           (if (< from-indent to-indent)
863               ;; Add indent
864               (progn
865                 (setq expr (make-string (1+ (- to-indent from-indent)) ?\ ))
866                 (while (re-search-forward "^ " nil t)
867                   (replace-match expr t t)
868                   (forward-line 1)))
869             ;; Reduce indent in all but first cell lines.
870             (setq expr (concat "^" (make-string
871                                     (- from-indent to-indent) ?\ )))
872             (while (re-search-forward expr nil t)
873               (replace-match "" t t)
874               (forward-line 1))
875             ;; Reduce indent in first cell lines which may have an
876             ;; autonumber or other cell delimiter.
877             (setq space (- from-indent to-indent
878                            (kview:label-separator-length kview)
879                            1))
880             (if (zerop space)
881                 nil
882               (setq expr (concat "^" (make-string
883                                       (- from-indent to-indent
884                                          (kview:label-separator-length kview)
885                                          1)
886                                       ?\ )))
887               (kview:map-tree
888                (function (lambda (view)
889                            (save-excursion
890                              (beginning-of-line)
891                              (if (looking-at expr)
892                                  (replace-match "" t t)))))
893                kview t)))
894           ;;
895           (if fill-p
896               ;; Refill cells without no-fill attribute.
897               (kview:map-tree (function (lambda (view)
898                                           (kotl-mode:fill-cell nil t)))
899                               kview t))
900           ;;
901           ;; Collapse temporarily expanded cells.
902           (if (delq nil collapsed-cells)
903               (kview:map-tree
904                (function
905                 (lambda (view)
906                   (if (car collapsed-cells)
907                       ;; Use free variable label-sep-len bound in
908                       ;; kview:map-tree for speed.
909                       (kcell-view:collapse nil label-sep-len))
910                   (setq collapsed-cells (cdr collapsed-cells))))
911                kview t))))
912     ;;
913     (goto-char new-start)
914     ;;
915     ;; Delete temporary markers.
916     (set-marker new-start nil)))
917
918 (defun kview:set-buffer-name (kview new-name)
919   "Set kview's buffer name to NEW-NAME."
920   (if (kview:is-p kview)
921       (save-excursion
922         (let ((buf (kview:buffer kview)))
923           (if buf (set-buffer buf)))
924         (kview:set-attr kview 'view-buffer-name new-name))
925     (error "(kview:set-buffer-name): Invalid kview argument")))
926
927 (defun kview:set-label-type (kview new-type)
928   "Change kview's label display type to NEW-TYPE, updating all displayed labels.
929 See documentation for variable, kview:default-label-type, for
930 valid values of NEW-TYPE."
931   (interactive (list kview
932                      (let ((completion-ignore-case)
933                            (label-type (kview:label-type kview))
934                            new-type-str)
935                        (if (string=
936                             ""
937                             (setq new-type-str
938                                   (completing-read
939                                    (format "View label type (current = %s): "
940                                            label-type)
941                                    '(("alpha") ("legal") ("id") ("no")
942                                      ("partial-alpha") ("star"))
943                                    nil t)))
944                            label-type
945                          (intern new-type-str)))))
946   (if (not (memq new-type '(alpha legal id no partial-alpha star)))
947       (error "(kview:set-label-type): Invalid label type, '%s'." new-type))
948   ;; Disable use of partial-alpha for now since it is broken.
949   (if (eq new-type 'partial-alpha)
950       (error "(kview:set-label-type): Partial-alpha labels don't work, choose another type"))
951   (let ((old-label-type (kview:label-type kview)))
952     (if (eq old-label-type new-type)
953         nil
954       (klabel-type:set-labels new-type)
955       (kview:set-attr kview 'label-type new-type)
956       (kview:set-functions new-type)
957       (kvspec:update t))))
958
959 (defun kview:top-cell (kview)
960   "Return kview's invisible top cell with idstamp 0 or nil if argument is not a kview."
961   (if (kview:is-p kview)
962       (kview:get-attr kview 'top-cell)))
963
964 (defun kview:valid-position-p (&optional pos)
965   "Return non-nil iff point or optional POS is at a position where editing may occur.
966 The read-only positions between cells and within cell indentations are invalid."
967   (cond ((null pos)
968          (>= (current-column) (kcell-view:indent)))
969         ((not (integer-or-marker-p pos))
970          (error "(kview:valid-position-p): Argument POS not an integer
971 or marker, '%s'" pos))
972         ((or (< pos (point-min)) (> pos (point-max)))
973          (error "(kview:valid-position-p): Invalid POS argument, '%d'"
974                 pos))
975         (t (save-excursion
976              (goto-char pos)
977              (>= (current-column) (kcell-view:indent))))))
978
979 ;;;
980 ;;; Private functions
981 ;;;
982
983 (defun kview:get-attr (obj attribute)
984   "Return the value of OBJECT's ATTRIBUTE."
985   (car (cdr (memq attribute (car (cdr (memq 'plist obj)))))))
986
987 (defun kview:set-attr (obj attribute value)
988   "Set OBJECT's ATTRIBUTE to VALUE and return VALUE."
989   (let* ((plist-ptr (cdr (memq 'plist obj)))
990          (plist (car plist-ptr))
991          (attr (memq attribute plist)))
992     (if attr
993         (setcar (cdr attr) value)
994       (setcar plist-ptr
995               (nconc (list attribute value) plist)))
996     value))
997
998 (defun kview:set-functions (label-type)
999   "Setup functions which handle labels of LABEL-TYPE for current view."
1000   (kview:set-attr kview 'label-function (klabel-type:function label-type))
1001   (kview:set-attr kview 'label-child (klabel-type:child label-type))
1002   (kview:set-attr kview 'label-increment (klabel-type:increment label-type))
1003   (kview:set-attr kview 'label-parent (klabel-type:parent label-type))
1004   (kview:set-attr kview 'to-label-end (klabel-type:to-label-end label-type)))
1005
1006 (defun kview:set-label-separator (label-separator &optional set-default-p)
1007   "Set the LABEL-SEPARATOR (a string) between labels and cell contents for the current kview.
1008 With optional prefix arg SET-DEFAULT-P, the default separator value used for
1009 new outlines is also set to this new value."
1010   (interactive
1011    (progn (barf-if-buffer-read-only)
1012           (list (if (kview:is-p kview)
1013                     (read-string
1014                      (format
1015                       "Change current%s label separator from \"%s\" to: "
1016                       (if current-prefix-arg " and default" "")
1017                       (kview:label-separator kview))))
1018                 current-prefix-arg)))
1019
1020   (barf-if-buffer-read-only)
1021   (cond ((not (kview:is-p kview))
1022          (error "(kview:set-label-separator): This is not a koutline"))
1023         ((not (stringp label-separator))
1024          (error "(kview:set-label-separator): Invalid separator, \"%s\""
1025                 label-separator))
1026         ((< (length label-separator) 2)
1027          (error "(kview:set-label-separator): Separator must be two or more characters, \"%s\""
1028                 label-separator)))
1029
1030   (let* ((old-sep-len (kview:label-separator-length kview))
1031          (sep-len (length label-separator))
1032          (sep-len-increase (- sep-len old-sep-len))
1033          (indent)
1034          (reindent-function
1035           (cond ((zerop sep-len-increase)
1036                  (function (lambda ())))
1037                 ((> sep-len-increase 0)
1038                  ;; Increase indent in each cell line.
1039                  (function (lambda ()
1040                              (goto-char (point-min))
1041                              (setq indent (make-string
1042                                            sep-len-increase ?\ ))
1043                              (while (re-search-forward "[^\n\r][\n\r] " nil t)
1044                                (insert indent)))))
1045                 (t
1046                  ;; Decrease indent in each cell line.
1047                  (function (lambda ()
1048                              (goto-char (point-min))
1049                              (setq indent
1050                                    (concat "[^\n\r][\n\r]"
1051                                            (make-string
1052                                             (- sep-len-increase) ?\ )))
1053                              (while (re-search-forward indent nil t)
1054                                (delete-region
1055                                 (+ (match-beginning 0) 2) (match-end 0))))))))
1056          pos)
1057     (save-excursion
1058       (goto-char (point-min))
1059       (kproperty:replace-separator pos label-separator old-sep-len)
1060       ;; Reindent all lines in cells except the first line which has already
1061       ;; been done.
1062       (funcall reindent-function))
1063     (kview:set-attr kview 'label-separator label-separator)
1064     (kview:set-attr kview 'label-separator-length sep-len)
1065     (if set-default-p
1066         (setq kview:default-label-separator label-separator))))
1067
1068 (provide 'kview)
1069
1070 ;;; kview.el ends here