1 ;;; kview.el --- Display handling of koutlines.
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4 ;; Developed with support from Motorola Inc.
6 ;; Author: Bob Weiner, Brown U.
8 ;; Maintainer: Mats Lidell <matsl@contactor.se>
11 ;; This file is part of GNU Hyperbole.
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.
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.
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.
33 ;;; Other required Lisp Libraries
35 (mapcar 'require '(klabel kfill hypb))
41 (set-default 'kview nil)
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.")
47 (defvar kview:default-levels-to-show 0
48 "*Default number of cell levels to show. 0 means all levels.")
50 (defvar kview:default-lines-to-show 0
51 "*Default number of lines per cell to show. 0 means all lines.")
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.")
58 (defvar kview:default-label-separator " "
59 "*Default string of characters to insert between label and contents of a koutline cell.")
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:
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. '***'.")
71 (defvar kview:default-level-indent 3
72 "*Default number of spaces to indent each succeeding level in koutlines.")
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))
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))
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))
101 ((< curr-indent start-indent)
102 ;; Went past start of this tree without a match.
105 ;; else go to prior node
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)
114 (defun kcell-view:cell (&optional pos)
115 "Return kcell at optional POS or point."
116 (kproperty:get (kcell-view:plist-point pos) 'kcell))
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))
133 ;; Move back to previous point and return nil.
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."
143 (if pos (goto-char pos))
144 (kcell-view:child visible-p label-sep-len)))
146 (defun kcell-view:collapse (&optional pos label-sep-len)
147 "Collapse cell at optional POS or point within the current view."
149 (goto-char (kcell-view:start pos label-sep-len))
150 (subst-char-in-region (point) (kcell-view:end-contents) ?\n ?\r t)))
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."
155 (goto-char (kcell-view:start pos label-sep-len))
156 (if (search-forward "\r" (kcell-view:end-contents) t)
159 (defun kcell-view:contents (&optional pos)
160 "Return contents of cell at optional POS or point."
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"))))
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
175 (if (= (kcell:idstamp cell) 0)
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)))
187 (fill-prefix (make-string thru-label ?\ ))
190 (if no-fill (kcell:set-attr cell 'no-fill t))
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 ""))
196 (if (kview:get-attr kview 'blank-lines)
198 ;; Make blank lines invisible.
199 (kproperty:put (1- (point)) (min (point) (point-max))
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)
208 (format "%%%ds" (- thru-label (length label-separator)))
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))))
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)))
222 (or (re-search-forward "[\n\r][\n\r]" nil t)
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."
229 (if pos (goto-char pos))
230 (goto-char (kcell-view:end))
231 (skip-chars-backward "\n\r")
234 (defun kcell-view:expand (&optional pos label-sep-len)
235 "Expand cell at optional POS or point within the current view."
237 (goto-char (kcell-view:start pos label-sep-len))
238 (subst-char-in-region (point) (kcell-view:end-contents) ?\r ?\n t)))
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))
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))
256 ((< curr-indent start-indent)
257 ;; Went past end of this tree without a match.
260 ;; else go to following node
262 ;; If didn't find a match, return to original point.
263 (or found (goto-char opoint))
266 (defun kcell-view:get-attr (attribute &optional pos)
267 "Return ATTRIBUTE's value for current cell or cell at optional POS."
269 (if pos (goto-char pos))
270 (kcell:get-attr (kcell-view:cell) attribute)))
272 (defun kcell-view:idstamp (&optional pos)
273 "Return idstamp string of cell at optional POS or point."
275 (if pos (goto-char pos))
276 (format "0%s" (or (kcell:idstamp (kcell-view:cell)) ""))))
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."
283 (kcell-view:to-label-end pos)
285 (or label-sep-len (kview:label-separator-length kview))))
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."
291 (if pos (goto-char pos))
292 (let ((label-type (kview:label-type kview)))
293 (if (eq label-type 'no)
295 (kcell-view:to-label-end)
296 (buffer-substring (point) (progn (skip-chars-backward "^ \t\n\r")
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)))
307 (if pos (goto-char pos))
308 (/ (- (or indent (kcell-view:indent nil label-sep-len)) label-sep-len)
309 (kview:level-indent kview))))
311 (defun kcell-view:line (&optional pos)
312 "Return contents of cell line at point or optional POS as a string."
314 (if pos (goto-char pos))
315 (if (kview:valid-position-p)
317 (kotl-mode:beginning-of-line)
318 (kotl-mode:end-of-line))
319 (error "(kcell-view:line): Invalid position, '%d'" (point)))))
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))
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.
331 (progn (goto-char (kcell-view:end-contents)) (end-of-line)))
332 (setq pos (kproperty:next-single-change (point) 'kcell))
334 (if (goto-char pos) (kotl-mode:eobp)))
335 (progn (goto-char opoint)
337 (goto-char (kcell-view:start nil label-sep-len))
338 (not (eq opoint (point))))))
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."
344 (narrow-to-region (or start (kcell-view:start))
345 (or end (kcell-view:end-contents)))
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))
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)
365 ;; Move back to previous point and return nil.
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))
375 (goto-char (kcell-view:start nil label-sep-len))
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.
381 (if (setq pos (kproperty:previous-single-change (point) 'kcell))
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
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)))
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))))
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)
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)
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))
420 (t (error "(kcell-view:to-label-end): Can't find end of current cell's label"))))
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)))
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: ")
434 (if pos (goto-char pos))
435 (let ((kcell (kcell:remove-attr (kcell-view:cell) attribute)))
437 (message "Cell <%s> now has no %s attribute."
438 (kcell-view:label) attribute))
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."
444 (if pos (goto-char pos))
446 (kcell:set-attr (kcell-view:cell) attribute value)))
448 (defun kcell-view:set-cell (kcell)
449 "Attach KCELL property to cell at point."
451 (kcell-view:to-label-end)
452 (kproperty:set 'kcell kcell)))
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."
458 (if pos (goto-char pos))
459 (kcell-view:forward visible-p label-sep-len)))
461 (defun kcell-view:start (&optional pos label-sep-len)
462 "Return start position of cell contents from optional POS or point."
464 (+ (kcell-view:to-label-end pos)
465 (or label-sep-len (kview:label-separator-length kview)))))
468 ;;; kview - one view per buffer, multiple views per kotl
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)
478 (kcell-view:create kview new-cell level klabel no-fill)
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))))
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.
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."
504 (let ((buf (get-buffer buffer-name)))
506 (error "(kview:create): No such buffer, '%s'." buffer-name))
507 ((or (null id-counter) (= id-counter 0))
509 ((not (integerp id-counter))
510 (error "(kview:create): 2nd arg, '%s', must be an integer." id-counter)))
512 (if (and (boundp 'kview) (eq (kview:buffer kview) buf))
513 ;; Don't recreate view if it exists.
515 (make-local-variable 'kview)
518 (list 'view-buffer-name buffer-name
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)
532 (or blank-lines kview:default-blank-lines)
534 (or levels-to-show kview:default-levels-to-show)
536 (or lines-to-show kview:default-lines-to-show)
538 (kview:set-functions (or label-type kview:default-label-type)))
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
547 (defun kview:delete-region (start end)
548 "Delete cells between START and END points from current view."
549 (delete-region start end))
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)
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))
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))
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.
579 (set-marker opoint nil)))
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))
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))
596 (goto-char label-end)
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))
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))))))
611 (goto-char label-end)
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))
619 (goto-char (point-min))
620 (while (and (setq pos
621 (kproperty:next-single-change (point) 'kcell))
623 (or (null (setq kcell (kproperty:get pos 'kcell)))
624 (/= (kcell:idstamp kcell) cell-id))))
627 (forward-char (kview:label-separator-length kview))
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)
640 (defun kview:idstamp-to-label (permanent-id)
641 "Return relative label for cell with PERMANENT-ID within current kview."
643 (if (kotl-mode:goto-cell permanent-id)
644 (kcell-view:label))))
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))
653 (setq contents (or contents (kcell:contents kcell) ""))
656 ;; Delete any extra newlines at end of cell contents.
658 (skip-chars-backward "\n\r")
659 (delete-region (point) end)
664 ;; Insert proper indent in all but the first line which has
665 ;; already been indented.
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)))
673 ;; Filling cell will insert proper indent on all lines.
674 (if (equal contents "")
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))
689 (goto-char (point-max))))))
692 (defun kview:is-p (object)
694 (if (listp object) (eq (car object) 'kview)))
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)))
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))
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))
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)))
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)))
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))
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)))
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)))
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
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.
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.
754 See also 'kview:map-siblings' and 'kview:map-tree'."
756 (set-buffer (kview:buffer kview))
758 (label-sep-len (kview:label-separator-length kview)))
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))))
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
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.
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.
783 See also 'kview:map-branch' and 'kview:map-tree'."
785 (set-buffer (kview:buffer kview))
787 (label-sep-len (kview:label-separator-length kview)))
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))))
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
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.
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.
810 See also 'kview:map-branch' and 'kview:map-siblings'."
812 (label-sep-len (kview:label-separator-length kview)))
814 (set-buffer (kview:buffer kview))
816 (progn (goto-char (point-min))
817 (kview:end-of-actual-line)
818 ;; Terminate when no further cells to process.
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)
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)
840 ;; Move or copy tree region to new location.
841 (or copy-p (delete-region from-start from-end))
842 (goto-char new-start)
844 (setq new-end (point))
846 ;; Change indentation of tree cells.
847 (if (/= from-indent to-indent)
849 (narrow-to-region new-start new-end)
850 ;; Store list of which cells are presently collapsed.
851 (setq collapsed-cells
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)))
859 (subst-char-in-region new-start new-end ?\r ?\n t)
861 (goto-char (point-min))
862 (if (< from-indent to-indent)
865 (setq expr (make-string (1+ (- to-indent from-indent)) ?\ ))
866 (while (re-search-forward "^ " nil t)
867 (replace-match expr t t)
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)
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)
882 (setq expr (concat "^" (make-string
883 (- from-indent to-indent
884 (kview:label-separator-length kview)
888 (function (lambda (view)
891 (if (looking-at expr)
892 (replace-match "" t t)))))
896 ;; Refill cells without no-fill attribute.
897 (kview:map-tree (function (lambda (view)
898 (kotl-mode:fill-cell nil t)))
901 ;; Collapse temporarily expanded cells.
902 (if (delq nil collapsed-cells)
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))))
913 (goto-char new-start)
915 ;; Delete temporary markers.
916 (set-marker new-start nil)))
918 (defun kview:set-buffer-name (kview new-name)
919 "Set kview's buffer name to NEW-NAME."
920 (if (kview:is-p kview)
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")))
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))
939 (format "View label type (current = %s): "
941 '(("alpha") ("legal") ("id") ("no")
942 ("partial-alpha") ("star"))
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)
954 (klabel-type:set-labels new-type)
955 (kview:set-attr kview 'label-type new-type)
956 (kview:set-functions new-type)
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)))
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."
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'"
977 (>= (current-column) (kcell-view:indent))))))
980 ;;; Private functions
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)))))))
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)))
993 (setcar (cdr attr) value)
995 (nconc (list attribute value) plist)))
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)))
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."
1011 (progn (barf-if-buffer-read-only)
1012 (list (if (kview:is-p kview)
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)))
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\""
1026 ((< (length label-separator) 2)
1027 (error "(kview:set-label-separator): Separator must be two or more characters, \"%s\""
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))
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)
1046 ;; Decrease indent in each cell line.
1047 (function (lambda ()
1048 (goto-char (point-min))
1050 (concat "[^\n\r][\n\r]"
1052 (- sep-len-increase) ?\ )))
1053 (while (re-search-forward indent nil t)
1055 (+ (match-beginning 0) 2) (match-end 0))))))))
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
1062 (funcall reindent-function))
1063 (kview:set-attr kview 'label-separator label-separator)
1064 (kview:set-attr kview 'label-separator-length sep-len)
1066 (setq kview:default-label-separator label-separator))))
1070 ;;; kview.el ends here