annotate lisp/hyperbole/kotl/kfile.el @ 36:c53a95d3c46d r19-15b101

Import from CVS: tag r19-15b101
author cvs
date Mon, 13 Aug 2007 08:53:38 +0200
parents 376386a54a3c
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;!emacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; FILE: kfile.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; SUMMARY: Save and restore kotls from files.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; USAGE: GNU Emacs V19 Lisp Library
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; KEYWORDS: outlines, wp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; AUTHOR: Bob Weiner & Kellie Clark
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; ORIG-DATE: 10/31/93
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
11 ;; LAST-MOD: 6-Mar-97 at 01:17:51 by Bob Weiner
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
12
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; Other required Elisp libraries
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 (mapcar 'require '(kproperty kotl-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; Loads menus under non-tty InfoDock, XEmacs or Emacs19; does nothing
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; otherwise.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 (and (not (featurep 'kmenu)) hyperb:window-system
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 (or hyperb:lemacs-p hyperb:emacs19-p) (require 'kmenu))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; Public variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 (defconst kfile:version "Kotl-4.0"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 "Version number of persistent data format used for saving koutlines.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; Entry Points
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 (defun kfile:find (file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 "Find a file FILE-NAME containing a kotl or create one if none exists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 Return the new kview."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (list (kfile:read-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 "Find koutline file: " nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (let ((existing-file (file-exists-p file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (and existing-file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (not (file-readable-p file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 "(kfile:find): \"%s\" is not readable. Check permissions."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (setq buffer (find-file file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; Finding the file may have already done a kfile:read as invoked through
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; kotl-mode via a file local variable setting. If so, don't read it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; again.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (if (kview:is-p kview)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (kfile:read buffer existing-file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (or (eq major-mode 'kotl-mode) (kotl-mode))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 kview))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (defun kfile:view (file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 "View an existing kotl version-2 file FILE-NAME in a read-only mode."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (interactive
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (list (kfile:read-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 "View koutline file: " t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (let ((existing-file (file-exists-p file-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (if existing-file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (if (not (file-readable-p file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 "(kfile:view): \"%s\" is not readable. Check permissions."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (error "(kfile:view): \"%s\" does not exist."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (view-file file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (kfile:narrow-to-kcells)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (goto-char (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;;; Public functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (defun kfile:create (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 "Create a new koutline file attached to BUFFER, with a single empty level 1 kotl cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 Return file's kview."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (or buffer (setq buffer (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (if (not (bufferp buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (error "(kfile:create): Invalid buffer argument, %s" buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (set-buffer buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (if buffer-read-only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (error "(kfile:create): %s is read-only" buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (let ((empty-p (zerop (buffer-size)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 import-from view standard-output)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (if (not empty-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 ;; This is a foreign file whose elements must be converted into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 ;; koutline cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (progn (setq import-from (kimport:copy-and-set-buffer buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (set-buffer buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (erase-buffer))) ;; We copied the contents to `import-from'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (setq view (kview:create (buffer-name buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 standard-output (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (princ ";; -*- Mode: kotl -*- \n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (prin1 kfile:version)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (princ " ;; file-format\n\^_\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 ;; Ensure that last cell has two newlines after it so that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 ;; kfile:insert-attributes finds it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (princ "\n\n\^_\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (princ "\^_\n;; depth-first kcell attributes\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 ;; Ensure that display is narrowed to cell region only.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (kfile:narrow-to-kcells)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (if empty-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ;; This is a new koutline file. Always need at least one visible
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; cell within a view. Insert initial empty cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (progn (kview:add-cell "1" 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 ;; Mark view unmodified, so if kill right away, there is no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 ;; prompt.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 ;; Move to first cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (goto-char (kcell-view:start)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 ;; Import buffer. Next line is necessary or the importation will fail.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (delete-region (point-min) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 ;; Import foreign buffer as koutline cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (kimport:file import-from (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;; If import buffer name starts with a space, kill it, as it is no
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ;; longer needed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (if (= ?\ (aref (buffer-name import-from) 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (kill-buffer import-from)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 view))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (defun kfile:is-p ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 "Iff current buffer contains an unformatted or formatted koutline, return file format version string, else nil."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (let (ver-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (setq ver-string (read (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (and (stringp ver-string) (string-match "^Kotl-" ver-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 ver-string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (error nil))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (defun kfile:read (buffer existing-file-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 "Create a new kotl view by reading BUFFER or create an empty view when EXISTING-FILE-P is nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 Return the new view."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (let (ver-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (cond ((not (bufferp buffer))
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
156 (error "(kfile:read): Argument must be a buffer, `%s'." buffer))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 ((not existing-file-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (kfile:create buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 ((progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (set-buffer buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (not (setq ver-string (kfile:is-p))))
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
162 (error "(kfile:read): `%s' is not a koutline file." buffer))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 ((equal ver-string "Kotl-4.0")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (kfile:read-v4-or-v3 buffer nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 ((equal ver-string "Kotl-3.0")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (kfile:read-v4-or-v3 buffer t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 ((equal ver-string "Kotl-2.0")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (kfile:read-v2 buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 ((equal ver-string "Kotl-1.0")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (error "(kfile:read): V1 koutlines are no longer supported"))
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
171 (t (error "(kfile:read): `%s' has unknown kotl version, %s."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 buffer ver-string)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (defun kfile:read-v2 (buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 "Create a kotl view by reading kotl version-2 BUFFER. Return the new view."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (let ((standard-input buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 cell-count label-type label-min-width label-separator
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 level-indent cell-data kotl-structure view kcell-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 ;; Skip past cell contents here.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (search-forward "\n\^_" nil t 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ;; Read rest of file data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (setq cell-count (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 label-type (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 label-min-width (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 label-separator (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 level-indent (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 cell-data (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 kotl-structure (read))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 ;; kcell-list is a depth-first list of kcells to be attached to the cell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 ;; contents within the kview down below.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (setq kcell-list (kfile:build-structure-v2 kotl-structure cell-data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 view (kview:create (buffer-name buffer) cell-count label-type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 level-indent label-separator label-min-width))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (kfile:narrow-to-kcells)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 ;; Add attributes to cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (kfile:insert-attributes-v2 view kcell-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 ;; Mark view unmodified and move to first cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (goto-char (kcell-view:start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 view))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (defun kfile:read-v4-or-v3 (buffer v3-flag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 "Create a koutline view by reading version-4 BUFFER. Return the new view.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 If V3-FLAG is true, read as a version-3 buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (let ((standard-input buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 cell-count label-type label-min-width label-separator
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 level-indent cell-data view)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 ;; Skip past cell contents here.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (search-forward "\n\^_" nil t 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 ;; Read rest of file data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (if v3-flag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 nil ;; V3 files did not store viewspecs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (kvspec:initialize)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 (setq kvspec:current (read)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (setq cell-count (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 label-type (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 label-min-width (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 label-separator (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 level-indent (read)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 cell-data (read))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 (setq view (kview:create (buffer-name buffer) cell-count label-type
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 level-indent label-separator label-min-width))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 (kfile:narrow-to-kcells)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 ;; Add attributes to cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 (kfile:insert-attributes-v3 view cell-data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 ;; Mark view unmodified and move to first cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (goto-char (kcell-view:start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 view))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 (defun kfile:update (&optional visible-only-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 "Update kfile internal structure so that view is ready for saving to a file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 Leave outline file expanded with structure data showing unless optional
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 VISIBLE-ONLY-P is non-nil. Signal an error if kotl is not attached to a file."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 (let* ((top (kview:top-cell kview))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (file (kcell:get-attr top 'file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 (label-type (kview:label-type kview))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 (label-min-width (kview:label-min-width kview))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (label-separator (kview:label-separator kview))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (level-indent (kview:level-indent kview))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 ;; If this happens to be non-nil, it is virtually impossible to save
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 ;; a file, so ensure it is nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (debug-on-error))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (cond ((null file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 (error "(kfile:update): Current outline is not attached to a file."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 ((not (file-writable-p file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (error "(kfile:update): File \"%s\" is not writable." file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (let* ((buffer-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (id-counter (kcell:get-attr top 'id-counter))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (kotl-data (make-vector (1+ id-counter) nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (standard-output (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (opoint (set-marker (make-marker) (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 (kcell-num 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 cell)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 ;; Prepare cell data for saving.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (kfile:narrow-to-kcells)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 (kview:map-tree
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 (lambda (view)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 (setq cell (kcell-view:cell))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 (aset kotl-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 kcell-num
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 (kotl-data:create cell))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (setq kcell-num (1+ kcell-num))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 kview t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 ;; Save top cell, 0, last since above loop may increment the total
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 ;; number of cells counter stored in it, if any invalid cells are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 ;; encountered.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 (aset kotl-data 0 (kotl-data:create top))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (setq id-counter (kcell:get-attr top 'id-counter))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (if (search-forward "\n\^_\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (delete-region (point-min) (match-end 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (princ ";; -*- Mode: kotl -*- \n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (prin1 kfile:version)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (princ " ;; file-format\n\^_\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 ;; Skip past cells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (if (search-forward "\n\^_\n" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 ;; Get rid of excess blank lines after last cell.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (progn (goto-char (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (skip-chars-backward "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (delete-region (point) (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (goto-char (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 ;; Ensure that last cell has two newlines after it so that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 ;; kfile:insert-attributes finds it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (princ "\n\n\^_\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (princ (format (concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 "%S ;; kvspec:current\n%d ;; id-counter\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 "%S ;; label-type\n%d ;; label-min-width\n"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 "%S ;; label-separator\n%d ;; level-indent\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 kvspec:current id-counter label-type label-min-width
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 label-separator level-indent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (princ "\^_\n;; depth-first kcell attributes\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (kfile:pretty-print kotl-data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 ;; Don't re-narrow buffer by default since this is used in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 ;; write-contents-hooks after save-buffer has widened buffer. If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 ;; buffer is narrowed here, only the narrowed portion will be saved to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 ;; the file. Narrow as an option since saving only the portion of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 ;; file visible in a view may be useful in some situations.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (if visible-only-p (kfile:narrow-to-kcells))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 ;; Return point to its original position as given by the opoint marker.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 (goto-char opoint)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (set-marker opoint nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
327 ;;; Next function is adapted from `file-write' of GNU Emacs 19, copyright FSF,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 ;;; under the GPL.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (defun kfile:write (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 "Write current outline to FILE."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (interactive "FWrite outline file: ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (if (or (null file) (string-equal file ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (error "(kfile:write): Invalid file name, \"%s\"" file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 ;; If arg is just a directory, use same file name, but in that directory.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (if (and (file-directory-p file) buffer-file-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (setq file (concat (file-name-as-directory file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (file-name-nondirectory buffer-file-name))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (kcell:set-attr (kview:top-cell kview) 'file file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (set-visited-file-name file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 ;; Set-visited-file-name clears local-write-file-hooks that we use to save
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 ;; koutlines properly, so reinitialize local variables.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (kotl-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (set-buffer-modified-p t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 ;; This next line must come before the save-buffer since write-file-hooks
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 ;; can make use of it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (kview:set-buffer-name kview (buffer-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (save-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 ;;; Private functions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (defun kfile:build-structure-v2 (kotl-structure cell-data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 "Build cell list from the KOTL-STRUCTURE and its CELL-DATA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 Assumes all arguments are valid. CELL-DATA is a vector of cell fields read
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 from a koutline file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 Return list of outline cells in depth first order. Invisible top cell is not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 included in the list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (let ((stack) (sibling-p) (cell-list) func cell)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (mapcar
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 (lambda (item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (setq func (cdr (assoc item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 (cons "\("
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 (setq stack (cons sibling-p stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 sibling-p nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 (cons "\)"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 (lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (setq sibling-p (car stack)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 stack (cdr stack)))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (cond (func (funcall func))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 ;; 0th cell was created with kview:create.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 ((equal item 0) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (t (setq cell (kotl-data:to-kcell-v2 (aref cell-data item))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 cell-list (cons cell cell-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 sibling-p t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 ))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 kotl-structure)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (nreverse cell-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 (defun kfile:insert-attributes-v2 (kview kcell-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 "Set cell attributes within kview for each element in KCELL-LIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 Assumes all cell contents are already in kview and that no cells are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 hidden."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 (let (buffer-read-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 (while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 (skip-chars-forward "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 ;; !!! Won't work if label-type is 'no.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 ;; Here we search past the cell identifier
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 ;; for the location at which to place cell properties.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 ;; Be sure not to skip past a period which may terminate the label.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 (if (re-search-forward "[A-Za-z0-9]\\(\\.?[A-Za-z0-9]\\)*" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 (kproperty:set 'kcell (car kcell-list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 (setq kcell-list (cdr kcell-list))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 (search-forward "\n\n" nil t)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 (defun kfile:insert-attributes-v3 (kview kcell-vector)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 "Set cell attributes within kview for each element in KCELL-VECTOR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 Assumes all cell contents are already in kview and that no cells are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 hidden."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 (let ((kcell-num 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 (buffer-read-only))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 (while
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 (skip-chars-forward "\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 ;; !!! Won't work if label-type is 'no.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 ;; Here we search past the cell identifier
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 ;; for the location at which to place cell properties.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 ;; Be sure not to skip past a period which may terminate the label.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 (if (re-search-forward "[A-Za-z0-9]\\(\\.?[A-Za-z0-9]\\)*" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 (kproperty:set 'kcell
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 (kotl-data:to-kcell-v3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (aref kcell-vector kcell-num)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 (setq kcell-num (1+ kcell-num))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 (search-forward "\n\n" nil t)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (defun kfile:narrow-to-kcells ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 "Narrow kotl file to kcell section only."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (if (kview:is-p kview)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 (let ((start-text) (end-text))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 (widen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 ;; Skip to start of kcells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 (if (search-forward "\n\^_" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 (setq start-text (1+ (match-end 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 ;; Skip past end of kcells.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 (if (and start-text (search-forward "\n\^_" nil t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (setq end-text (1+ (match-beginning 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 (if (and start-text end-text)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 (progn (narrow-to-region start-text end-text)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 (goto-char (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 "(kfile:narrow-to-kcells): Cannot find start or end of kcells"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 ))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (defun kfile:print-to-string (object)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 "Return a string containing OBJECT, any Lisp object, in pretty-printed form.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 Quoting characters are used when needed to make output that `read' can
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 handle, whenever this is possible."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 (set-buffer (get-buffer-create " kfile:print-to-string"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 (let ((emacs-lisp-mode-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453 (buffer-read-only))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 (emacs-lisp-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 (let ((print-escape-newlines kfile:escape-newlines))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 (prin1 object (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 (while (not (eobp))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 ;; (message "%06d" (- (point-max) (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 (cond
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 ((looking-at "\\s\(")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (while (looking-at "\\s(")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (forward-char 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (> (match-beginning 1) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 (= ?\( (char-after (1- (match-beginning 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 ;; Make sure this is a two-element list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 (goto-char (match-beginning 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (forward-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 ;; (looking-at "[ \t]*\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475 ;; Avoid mucking with match-data; does this test work?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 (char-equal ?\) (char-after (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 ;; -1 gets the paren preceding the quote as well.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (delete-region (1- (match-beginning 1)) (match-end 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 (insert "'")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 (forward-sexp 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (if (looking-at "[ \t]*\)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 (delete-region (match-beginning 0) (match-end 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 (error "Malformed quote"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 (backward-sexp 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 ((condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (prog1 t (down-list 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 (error nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (backward-char 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (delete-region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 (progn (skip-chars-forward " \t") (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 (if (not (char-equal ?' (char-after (1- (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 (insert ?\n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 ((condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 (prog1 t (up-list 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 (error nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 (while (looking-at "\\s)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 (forward-char 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 (skip-chars-backward " \t")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 (delete-region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (progn (skip-chars-forward " \t") (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 (if (not (char-equal ?' (char-after (1- (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (insert ?\n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (t (goto-char (point-max)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (indent-sexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 (buffer-string))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (kill-buffer (current-buffer))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (defun kfile:pretty-print (object &optional stream)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 "Output the pretty-printed representation of OBJECT, any Lisp object.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 Quoting characters are printed when needed to make output that `read'
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 can handle, whenever this is possible.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 Output stream is STREAM, or value of `standard-output' (which see)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 (princ (kfile:print-to-string object) (or stream standard-output)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 (defun kfile:read-name (prompt existing-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 "PROMPT for and read a koutline file name. EXISTING-P means must exist."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (let ((filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 (while (not filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 (setq filename (read-file-name prompt nil nil existing-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 (if (or (null filename) (equal filename ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 (progn (ding) (setq filename nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 ;;; Private variables
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 ;;; ************************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (defvar kfile:escape-newlines t
36
c53a95d3c46d Import from CVS: tag r19-15b101
cvs
parents: 0
diff changeset
533 "Value of print-escape-newlines used by `kfile:print-to-string' function.")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (provide 'kfile)