0
|
1 ;;!emacs
|
|
2 ;;
|
|
3 ;; FILE: kimport.el
|
|
4 ;; SUMMARY: Convert and insert other outline file formats into koutlines.
|
|
5 ;; USAGE: GNU Emacs V19 Lisp Library
|
|
6 ;; KEYWORDS: data, outlines, wp
|
|
7 ;;
|
|
8 ;; AUTHOR: Bob Weiner & Kellie Clark
|
|
9 ;;
|
|
10 ;; ORIG-DATE: 15-Nov-93 at 11:57:05
|
114
|
11 ;; LAST-MOD: 6-Mar-97 at 01:18:33 by Bob Weiner
|
|
12
|
0
|
13 ;;; ************************************************************************
|
|
14 ;;; Other required Elisp libraries
|
|
15 ;;; ************************************************************************
|
|
16
|
|
17 ;; kfile.el requires kotl-mode.el which requires kimport.el.
|
|
18 (require 'wrolo)
|
|
19
|
|
20 ;;; ************************************************************************
|
|
21 ;;; Public variables
|
|
22 ;;; ************************************************************************
|
|
23
|
|
24 ;; kimport:mode-alist and kimport:suffix-alist are defined in
|
|
25 ;; "../hyperbole.el".
|
|
26
|
|
27 ;;; ************************************************************************
|
|
28 ;;; Public functions
|
|
29 ;;; ************************************************************************
|
|
30
|
|
31 ;;;###autoload
|
|
32 (defun kimport:file (import-from output-to &optional children-p)
|
|
33 "Import a buffer or file IMPORT-FROM into the koutline in buffer or file OUTPUT-TO.
|
|
34
|
|
35 Any suffix in IMPORT-FROM's buffer name is used to determine the type of
|
|
36 importation. All others are imported as text, one paragraph per cell.
|
|
37
|
|
38 See the documentation for the variable, `kimport:suffix-alist' for
|
|
39 information on specific importation formats."
|
|
40 (interactive "FImport from buffer/file: \nFInsert into koutline buffer/file: \nP")
|
|
41 (let ((import-buf-name
|
|
42 (cond ((or (bufferp import-from)
|
|
43 (get-buffer import-from))
|
|
44 (buffer-name (get-buffer import-from)))
|
|
45 ((get-file-buffer import-from)
|
|
46 (buffer-name (get-file-buffer import-from)))
|
|
47 ((stringp import-from)
|
|
48 (file-name-nondirectory import-from))
|
|
49 (t (error "(kimport:buffer): `%s' is an invalid `import-from' argument"))))
|
|
50 (function))
|
|
51
|
|
52 (set-buffer import-buf-name)
|
|
53 (if (setq function (cdr (assq major-mode kimport:mode-alist)))
|
|
54 nil
|
|
55 (let ((import-suffix (if (string-match "\\..+\\'" import-buf-name)
|
|
56 (match-string 0 import-buf-name)))
|
|
57 (suffix-alist kimport:suffix-alist)
|
|
58 suffix-regexp)
|
|
59 (while (and import-suffix suffix-alist)
|
|
60 (setq suffix-regexp (car (car suffix-alist))
|
|
61 function (cdr (car suffix-alist))
|
|
62 suffix-alist (cdr suffix-alist))
|
|
63 (if (string-match suffix-regexp import-suffix)
|
|
64 nil
|
|
65 (setq function nil)))
|
|
66 (if function nil (setq function (cdr (assq t kimport:mode-alist))))))
|
|
67 (funcall function import-from output-to children-p)))
|
|
68
|
|
69 ;;; Augment right-side numbered files, blank line between cells
|
|
70 ;;;
|
|
71
|
|
72 ;;;###autoload
|
|
73 (defun kimport:aug-post-outline (import-from output-to &optional children-p)
|
|
74 "Insert Augment outline statements from IMPORT-FROM into koutline OUTPUT-TO.
|
|
75 Displays and leaves point in OUTPUT-TO. See documentation for
|
|
76 `kimport:initialize' for valid values of IMPORT-FROM and OUTPUT-TO and for
|
|
77 an explanation of where imported cells are placed.
|
|
78
|
|
79 If OUTPUT-TO is a new koutline, the first statement inserted will be the
|
|
80 first cell. Otherwise, it will be the successor of the current cell.
|
|
81
|
|
82 Each statement to be imported is delimited by an Augment relative id at the
|
|
83 end of the statement. \"1\" = level 1, \"1a\" = level 2 in outline and so
|
|
84 on."
|
|
85 (interactive "FImport from Augment post-numbered buffer/file: \nFBuffer/file to insert cells into: \nP")
|
|
86 (let ((output-level 1) (klabel "1")
|
|
87 initially-empty-output no-renumber orig-point count total)
|
|
88 ;; Don't change the order of import-from and output-to inits here.
|
|
89 (setq import-from (kimport:copy-and-set-buffer import-from)
|
|
90 output-to (kimport:initialize output-to)
|
|
91 orig-point (point)
|
|
92 initially-empty-output (zerop (- (point-max) (point-min)))
|
|
93 no-renumber (or initially-empty-output
|
|
94 (not (if children-p
|
|
95 (kcell-view:child-p)
|
|
96 (kcell-view:sibling-p)))))
|
|
97
|
|
98 (if (eq import-from output-to)
|
|
99 (error "(kimport:aug-post-outline): Import and output buffers may not be the same."))
|
|
100
|
|
101 (set-buffer import-from)
|
|
102 (show-all)
|
|
103 (save-excursion
|
|
104 (goto-char (point-min))
|
|
105 ;; Total number of Augement statements.
|
|
106 (setq total (read (count-matches
|
|
107 " +\\([0-9][0-9a-z]*\\)\n\\(\n\\|\\'\\)")))
|
|
108 (if initially-empty-output
|
|
109 nil
|
|
110 ;; Insert first cell as sibling of current cell.
|
|
111 (set-buffer output-to)
|
|
112 (if children-p
|
|
113 ;; Insert as children.
|
|
114 (progn (setq klabel (klabel:child (kcell-view:label))
|
|
115 output-level (klabel:level klabel))
|
|
116 ;; Move to end of this cell since cell insertion will
|
|
117 ;; occur at point.
|
|
118 (goto-char (kcell-view:end)))
|
|
119 ;; Insert as successors.
|
|
120 (setq klabel (klabel:increment (kcell-view:label))
|
|
121 output-level (klabel:level klabel))
|
|
122 ;; Move to start of line of next tree since cell insertion will occur
|
|
123 ;; at point.
|
|
124 (goto-char (kotl-mode:tree-end))))
|
|
125 (setq count (kimport:aug-post-statements
|
|
126 import-from output-to klabel output-level 1 0 total)))
|
|
127 (pop-to-buffer output-to)
|
|
128 (kfile:narrow-to-kcells)
|
|
129 (if no-renumber nil (klabel-type:update-labels klabel))
|
|
130 (goto-char orig-point)
|
|
131 (if (kotl-mode:buffer-empty-p)
|
|
132 nil
|
|
133 (kotl-mode:to-valid-position))
|
|
134 (message "Imported %d of %d Augment statements." count total)))
|
|
135
|
|
136 ;;;
|
114
|
137 ;;; Emacs outliner style files, leading `*' cell delimiters
|
0
|
138 ;;;
|
|
139
|
|
140 ;;;###autoload
|
|
141 (defun kimport:star-outline (import-from output-to &optional children-p)
|
|
142 "Insert star outline nodes from IMPORT-FROM into koutline OUTPUT-TO.
|
|
143 Displays and leaves point in OUTPUT-TO. See documentation for
|
|
144 `kimport:initialize' for valid values of IMPORT-FROM and OUTPUT-TO and for
|
|
145 an explanation of where imported cells are placed.
|
|
146
|
|
147 \"* \" = level 1, \"** \" = level 2 in outline and so on."
|
|
148 (interactive "FImport from star delimited cells buffer/file: \nFBuffer/file to insert cells into: \nP")
|
|
149 (let ((output-level 1) (klabel "1")
|
|
150 initially-empty-output no-renumber orig-point count total)
|
|
151 ;; Don't change the order of import-from and output-to inits here.
|
|
152 (setq import-from (kimport:copy-and-set-buffer import-from)
|
|
153 output-to (kimport:initialize output-to)
|
|
154 orig-point (point)
|
|
155 initially-empty-output (zerop (- (point-max) (point-min)))
|
|
156 no-renumber (or initially-empty-output
|
|
157 (not (if children-p
|
|
158 (kcell-view:child-p)
|
|
159 (kcell-view:sibling-p)))))
|
|
160
|
|
161 (if (eq import-from output-to)
|
|
162 (error "(kimport:star-outline): Import and output buffers may not be the same."))
|
|
163
|
|
164 (set-buffer import-from)
|
|
165 (show-all)
|
|
166 (save-excursion
|
|
167 (goto-char (point-min))
|
|
168 ;; If initial text in buffer is not an star outline node, add a star to
|
|
169 ;; make it one, so it is not deleted from the import.
|
|
170 (if (not (looking-at "[ \t]*\\*"))
|
|
171 (insert "* "))
|
|
172 (goto-char (point-min))
|
|
173 ;; Total number of top-level cells.
|
|
174 (setq total (read (count-matches "^[ \t]*\\*[ \t\n]")))
|
|
175 (if initially-empty-output
|
|
176 nil
|
|
177 ;; Insert first cell as sibling of current cell.
|
|
178 (set-buffer output-to)
|
|
179 (if children-p
|
|
180 ;; Insert as children.
|
|
181 (progn (setq klabel (klabel:child (kcell-view:label))
|
|
182 output-level (klabel:level klabel))
|
|
183 ;; Move to end of this cell since cell insertion will
|
|
184 ;; occur at point.
|
|
185 (goto-char (kcell-view:end)))
|
|
186 ;; Insert as successors.
|
|
187 (setq klabel (klabel:increment (kcell-view:label))
|
|
188 output-level (klabel:level klabel))
|
|
189 ;; Move to start of line of next tree since cell insertion will occur
|
|
190 ;; at point.
|
|
191 (goto-char (kotl-mode:tree-end))))
|
|
192 (setq count (kimport:star-entries
|
|
193 import-from output-to klabel output-level 1 0 total)))
|
|
194 (pop-to-buffer output-to)
|
|
195 (kfile:narrow-to-kcells)
|
|
196 (if no-renumber nil (klabel-type:update-labels klabel))
|
|
197 (goto-char orig-point)
|
|
198 (if (kotl-mode:buffer-empty-p)
|
|
199 nil
|
|
200 (kotl-mode:to-valid-position))
|
|
201 (message "Imported %d of %d star outline trees." count total)))
|
|
202
|
|
203 ;;;
|
|
204 ;;; Generic text file import or koutline insertion.
|
|
205 ;;;
|
|
206
|
|
207 ;;;###autoload
|
|
208 (defun kimport:text (import-from output-to &optional children-p)
|
|
209 "Insert text paragraphs from IMPORT-FROM into koutline OUTPUT-TO.
|
|
210 Displays and leaves point in OUTPUT-TO. See documentation for
|
|
211 `kimport:initialize' for valid values of IMPORT-FROM and OUTPUT-TO and for
|
|
212 an explanation of where imported cells are placed.
|
|
213
|
|
214 Text paragraphs are imported as a sequence of same level cells. Koutlines
|
|
215 are imported with their structure intact.
|
|
216
|
114
|
217 The variable, `paragraph-start,' is used to determine paragraphs."
|
0
|
218 (interactive "FImport from text/koutline buffer/file: \nFInsert cells into koutline buffer/file: \nP")
|
|
219 (let ((klabel "1") (output-level 1) (count 0) initially-empty-output
|
|
220 no-renumber orig-point total)
|
|
221 ;; Don't change the order of import-from and output-to inits here.
|
|
222 (setq import-from (kimport:copy-and-set-buffer import-from)
|
|
223 output-to (kimport:initialize output-to)
|
|
224 orig-point (point)
|
|
225 initially-empty-output (zerop (- (point-max) (point-min)))
|
|
226 no-renumber (or initially-empty-output
|
|
227 (not (if children-p
|
|
228 (kcell-view:child-p)
|
|
229 (kcell-view:sibling-p)))))
|
|
230
|
|
231 (if (eq import-from output-to)
|
|
232 (error "(kimport:text): Import and output buffers may not be the same."))
|
|
233
|
|
234 (set-buffer import-from)
|
|
235 (let ((kotl-import (eq major-mode 'kotl-mode))
|
|
236 visible-cells)
|
|
237 (save-excursion
|
|
238 (if initially-empty-output
|
|
239 nil
|
|
240 ;; Insert first cell as sibling of current cell.
|
|
241 (set-buffer output-to)
|
|
242 (if children-p
|
|
243 ;; Insert as children.
|
|
244 (progn (setq klabel (klabel:child (kcell-view:label))
|
|
245 output-level (klabel:level klabel))
|
|
246 ;; Move to end of this cell since cell insertion will
|
|
247 ;; occur at point.
|
|
248 (goto-char (kcell-view:end)))
|
|
249 ;; Insert as successors.
|
|
250 (setq klabel (klabel:increment (kcell-view:label))
|
|
251 output-level (klabel:level klabel))
|
|
252 ;; Move to start of line of next tree since cell insertion will occur
|
|
253 ;; at point.
|
|
254 (goto-char (kotl-mode:tree-end)))
|
|
255 (set-buffer import-from))
|
|
256
|
|
257 (if kotl-import
|
|
258 ;; Importing from a koutline, so handle specially.
|
|
259 (progn (kotl-mode:beginning-of-buffer)
|
|
260 ;; Total number of cells.
|
|
261 (setq total (read (count-matches "[\n\r][\n\r]"))
|
|
262 visible-cells (read (count-matches "\n\n"))
|
|
263 count (save-excursion
|
|
264 ;; Incredible non-local exit to ensure that
|
|
265 ;; recursion ends at the right time.
|
|
266 (catch 'end
|
|
267 (kimport:kcells import-from output-to klabel
|
|
268 output-level 1
|
|
269 count total)))))
|
|
270
|
|
271 (show-all)
|
|
272 (goto-char (point-min))
|
|
273 ;; Total number of paragraphs.
|
|
274 (setq total (read (count-matches paragraph-start))
|
|
275 count (kimport:text-paragraphs import-from output-to klabel
|
|
276 output-level count total))))
|
|
277 (pop-to-buffer output-to)
|
|
278 (kfile:narrow-to-kcells)
|
|
279 (if no-renumber nil (klabel-type:update-labels klabel))
|
|
280 (goto-char orig-point)
|
|
281 (if (kotl-mode:buffer-empty-p)
|
|
282 nil
|
|
283 (kotl-mode:to-valid-position))
|
|
284 (if kotl-import
|
|
285 (message "Imported %d of %d visible cells from a %d cell outline."
|
|
286 count visible-cells total)
|
|
287 (message "Imported %d of %d paragraphs." count total)))))
|
|
288
|
|
289 ;;; ************************************************************************
|
|
290 ;;; Private functions - Don't call these functions from outside of this
|
|
291 ;;; module or you may misuse them and cause data corruption.
|
|
292 ;;; ************************************************************************
|
|
293
|
|
294 (defun kimport:aug-label-lessp (label1 label2)
|
|
295 "Return non-nil iff Augment-style LABEL1 is less than LABEL2."
|
|
296 (let ((lev1 (klabel:level-alpha label1))
|
|
297 (lev2 (klabel:level-alpha label2)))
|
|
298 (cond ((< lev1 lev2))
|
|
299 ((= lev1 lev2) (string-lessp label1 label2))
|
|
300 (t nil))))
|
|
301
|
|
302 (defun kimport:aug-post-statements (import-from output-to klabel output-level
|
|
303 import-level count total)
|
|
304 "Insert post-numbered Augment statements (contents only) from IMPORT-FROM into existing OUTPUT-TO.
|
|
305
|
|
306 KLABEL is the label to use for the first imported statement.
|
|
307 OUTPUT-LEVEL is the level at which to insert the first statement.
|
|
308 IMPORT-LEVEL is the depth of the current statement in the import file,
|
|
309 \(initially 1).
|
|
310
|
|
311 COUNT of inserted cells starts at 0. TOTAL is the total number of statements
|
|
312 in IMPORT-FROM, used to show a running tally of the imported statements."
|
|
313 (set-buffer import-from)
|
|
314 (let ((cell-end-regexp " +\\([0-9][0-9a-z]*\\)\n\\(\n+\\|\\'\\)")
|
|
315 contents start subtree-p end end-contents statement-level
|
|
316 child-label)
|
|
317 ;; While find cells at import-level or deeper ...
|
|
318 (while (and (setq start (point))
|
|
319 (re-search-forward cell-end-regexp nil t)
|
|
320 (<= import-level
|
|
321 (setq statement-level
|
|
322 (klabel:level-alpha
|
|
323 (buffer-substring
|
|
324 (match-beginning 1) (match-end 1))))))
|
|
325 (setq end-contents (match-beginning 0)
|
|
326 end (match-end 0))
|
|
327 (goto-char start)
|
|
328 (skip-chars-forward " ")
|
|
329 (setq contents (kimport:unindent-region (point) end-contents))
|
|
330 (goto-char end)
|
|
331 (setq subtree-p (save-excursion
|
|
332 (if (re-search-forward cell-end-regexp nil t)
|
|
333 (< statement-level
|
|
334 (klabel:level-alpha
|
|
335 (buffer-substring
|
|
336 (match-beginning 1) (match-end 1)))))))
|
|
337 (save-excursion
|
|
338 (set-buffer output-to)
|
|
339 ;; Add the cell starting at point.
|
|
340 (kview:add-cell klabel output-level contents nil t)
|
|
341 (if subtree-p (setq child-label (klabel:child klabel)))
|
|
342 (message "%d of %d statements converted..."
|
|
343 (setq count (1+ count)) total)
|
|
344 (setq klabel (klabel:increment klabel)))
|
|
345 ;;
|
|
346 ;; Current buffer returns to `import-from' here.
|
|
347 ;; Handle each sub-level through recursion.
|
|
348 (if subtree-p
|
|
349 ;; Subtree exists so insert its cells.
|
|
350 (setq count
|
|
351 (kimport:aug-post-statements
|
|
352 import-from output-to child-label (1+ output-level)
|
|
353 (1+ import-level) count total))))
|
|
354 (goto-char start))
|
|
355 count)
|
|
356
|
|
357 (defun kimport:copy-and-set-buffer (source)
|
|
358 "Copy and untabify SOURCE, set copy buffer as current buffer for this command and return the copy buffer.
|
|
359 SOURCE may be a buffer name, a buffer or a file name.
|
|
360 If SOURCE buffer name begins with a space, it is not copied under the
|
|
361 assumption that it already has been. If SOURCE is a koutline, it is not
|
|
362 copied since there is no need to copy it to import it."
|
|
363 ;; This buffer name format is used so that we can easily
|
|
364 ;; extract any file name suffix from the buffer name.
|
|
365 (setq source (set-buffer (or (get-buffer source)
|
|
366 (find-file-noselect source))))
|
|
367 (let ((mode (or (if (boundp 'kotl-previous-mode) kotl-previous-mode)
|
|
368 major-mode))
|
|
369 copy)
|
|
370 (if (or (eq mode 'kotl-mode)
|
|
371 (= ?\ (aref (buffer-name source) 0)))
|
|
372 source
|
|
373 (setq copy (get-buffer-create
|
|
374 (concat " " (if (string-match ".+[|<]" (buffer-name))
|
|
375 (substring (buffer-name)
|
|
376 0 (1- (match-end 0)))
|
|
377 (buffer-name)))))
|
|
378 (set-buffer copy)
|
|
379 (setq buffer-read-only nil
|
|
380 major-mode mode)
|
|
381 (erase-buffer)
|
|
382 (insert-buffer source)
|
|
383 (untabify (point-min) (point-max))
|
|
384 ;; Ensure buffer ends with a newline so that we don't miss the last
|
|
385 ;; element during the import.
|
|
386 (goto-char (point-max))
|
|
387 (if (/= (preceding-char) ?\n) (insert "\n"))
|
|
388 (set-buffer-modified-p nil)
|
|
389 copy)))
|
|
390
|
|
391 (defun kimport:initialize (output-to)
|
|
392 "Setup to import elements into koutline OUTPUT-TO.
|
|
393 Return OUTPUT-TO buffer and set current buffer for the current command
|
|
394 to OUTPUT-TO.
|
|
395
|
|
396 OUTPUT-TO may be a buffer, buffer-name or file name. If OUTPUT-TO exists
|
|
397 already, it must be a koutline or an error will be signaled. For an existing
|
|
398 OUTPUT-TO, the text cells are inserted after the cell at point or after the
|
|
399 first cell for a newly loaded koutline. If OUTPUT-TO is nil, the current
|
|
400 buffer is used.
|
|
401
|
|
402 If OUTPUT-TO is an existing koutline, the first cell imported will be added
|
|
403 as the successor of the current cell. If an existing file is read in as
|
|
404 OUTPUT-TO within this function, point is left at the end of this buffer so
|
|
405 that imported cells will be appended to the buffer. For a new file, this
|
|
406 means the first cell imported will become the first outline cell.
|
|
407
|
|
408 If a non-nil third argument, CHILDREN-P, is given to the caller of this
|
|
409 function and OUTPUT-TO contains at least one cell, then the imported cells
|
|
410 will be added as children of the cell where this function leaves point
|
|
411 \(either the current cell or for a newly read in outline, the last cell)."
|
|
412 (let* ((output-existing-buffer-p
|
|
413 (if output-to
|
|
414 (or (get-buffer output-to) (get-file-buffer output-to))))
|
|
415 (output-exists-p
|
|
416 (if output-to
|
|
417 (or output-existing-buffer-p (file-exists-p output-to))
|
|
418 ;; current buffer will be used for output and it exists.
|
|
419 t)))
|
|
420 (setq output-to (if output-to
|
|
421 (or (get-buffer output-to)
|
|
422 (find-file-noselect output-to))
|
|
423 (current-buffer)))
|
|
424 (set-buffer output-to)
|
|
425 (if output-exists-p
|
|
426 (if (eq major-mode 'kotl-mode)
|
|
427 (if (kotl-mode:buffer-empty-p)
|
|
428 nil
|
|
429 ;; Make imported cells be appended if the output buffer was
|
|
430 ;; just read in.
|
|
431 (if output-existing-buffer-p nil (goto-char (point-max)))
|
|
432 (kotl-mode:to-valid-position))
|
|
433 (error
|
|
434 "(kimport:initialize): Second arg, %s, must be a koutline file."
|
|
435 (buffer-name output-to)))
|
|
436 (if (eq major-mode 'kotl-mode)
|
|
437 nil
|
|
438 (setq kview nil)
|
|
439 (kotl-mode))
|
|
440 (delete-region (point-min) (point-max))))
|
|
441 output-to)
|
|
442
|
|
443 (defun kimport:kcells (import-from output-to klabel output-level
|
|
444 import-level count total)
|
|
445 "Insert visible koutline cells (contents and attributes) from IMPORT-FROM into existing OUTPUT-TO.
|
|
446
|
|
447 KLABEL is the label to use for the first imported cell.
|
|
448 OUTPUT-LEVEL is the level at which to insert the first cell.
|
|
449 IMPORT-LEVEL is the depth of the current cell in the import file,
|
|
450 \(initially 1).
|
|
451
|
|
452 COUNT of inserted cells starts at 0. TOTAL is the total number of cells
|
|
453 in IMPORT-FROM, used to show a running tally of the imported cells."
|
|
454 (set-buffer import-from)
|
|
455 (goto-char (kcell-view:start))
|
|
456 (let ((again t) contents subtree-p child-label)
|
|
457 ;; While find cells at import-level or deeper ...
|
|
458 (while (<= import-level (kcell-view:level))
|
|
459 (setq subtree-p (kcell-view:child-p nil t)
|
|
460 contents (kcell-view:contents))
|
|
461 (goto-char (kcell-view:end-contents))
|
|
462 (save-excursion
|
|
463 (set-buffer output-to)
|
|
464 ;; Add the cell starting at point.
|
|
465 (kview:add-cell klabel output-level contents nil t)
|
|
466 (if subtree-p (setq child-label (klabel:child klabel)))
|
|
467 (message "%d of %d cells inserted..."
|
|
468 (setq count (1+ count)) total)
|
|
469 (setq klabel (klabel:increment klabel)))
|
|
470 ;;
|
|
471 ;; Current buffer returns to `import-from' here.
|
|
472 ;; Handle each sub-level through recursion.
|
|
473 (if (and (setq again (kcell-view:next t)) subtree-p)
|
|
474 ;; Subtree exists so insert its cells.
|
|
475 (setq count
|
|
476 (kimport:kcells
|
|
477 import-from output-to child-label (1+ output-level)
|
|
478 (1+ import-level) count total)))
|
|
479 (if again nil (throw 'end count))))
|
|
480 count)
|
|
481
|
|
482 (defun kimport:star-entries (import-from output-to klabel output-level
|
|
483 import-level count total)
|
|
484 "Insert visible star outline entries from IMPORT-FROM into existing OUTPUT-TO.
|
|
485
|
|
486 KLABEL is the label to use for the first imported entry.
|
|
487 OUTPUT-LEVEL is the level at which to insert the first entry.
|
|
488 IMPORT-LEVEL is the depth of the current entry in the import file,
|
|
489 \(initially 1).
|
|
490
|
|
491 COUNT of inserted entries starts at 0. TOTAL is the total number of entries
|
|
492 in IMPORT-FROM, used to show a running tally of the imported entries."
|
|
493 (set-buffer import-from)
|
|
494 (let ((start (point))
|
|
495 (rolo-entry-regexp "^[ \t]*\\(\\*+\\)")
|
|
496 subtree-p end contents node-level child-label)
|
|
497 ;; While find cells at import-level or deeper ...
|
|
498 (while (and (re-search-forward rolo-entry-regexp nil t)
|
|
499 (<= import-level
|
|
500 (setq node-level
|
|
501 (length
|
|
502 (buffer-substring
|
|
503 (match-beginning 1) (match-end 1))))))
|
|
504 (skip-chars-forward " \t")
|
|
505 (setq start (point)
|
|
506 end (rolo-to-entry-end)
|
|
507 subtree-p (if (looking-at rolo-entry-regexp)
|
|
508 (< node-level
|
|
509 (length (buffer-substring
|
|
510 (match-beginning 1) (match-end 1))))))
|
|
511 (skip-chars-backward "\n\r")
|
|
512 (setq contents (kimport:unindent-region start (point)))
|
|
513 (save-excursion
|
|
514 (set-buffer output-to)
|
|
515 ;; Add the cell starting at point.
|
|
516 (kview:add-cell klabel output-level contents nil t)
|
|
517 (if subtree-p (setq child-label (klabel:child klabel)))
|
|
518 (message "%d of %d trees converted..."
|
|
519 (if (= node-level 1) (setq count (1+ count)) count)
|
|
520 total)
|
|
521 (setq klabel (klabel:increment klabel)))
|
|
522 ;;
|
|
523 ;; Current buffer returns to `import-from' here.
|
|
524 (goto-char end)
|
|
525 ;;
|
|
526 ;; Handle each sub-level through recursion.
|
|
527 (if subtree-p
|
|
528 ;; Subtree exists so insert its cells.
|
|
529 (setq count
|
|
530 (kimport:star-entries import-from output-to child-label
|
|
531 (1+ output-level) (1+ import-level)
|
|
532 count total))))
|
|
533 (goto-char start))
|
|
534 count)
|
|
535
|
|
536 (defun kimport:text-paragraphs (import-from output-to klabel
|
|
537 output-level count total)
|
|
538 "Insert text paragraphs from IMPORT-FROM into existing OUTPUT-TO.
|
|
539 First cell is inserted with KLABEL at OUTPUT-LEVEL, as the sibling of the
|
|
540 previous cell, with the COUNT of inserted paragraphs starting at 0. TOTAL is
|
|
541 the total number of paragraphs in IMPORT-FROM, used to show a running tally
|
|
542 of the imported paragraphs.
|
|
543
|
114
|
544 The variable, `paragraph-start' is used to determine paragraphs."
|
0
|
545 (set-buffer import-from)
|
|
546 (let* ((count 0) start end contents)
|
|
547 ;; Next line is needed when importing into an existing kview.
|
|
548 (goto-char (point-min))
|
|
549 ;; Move past blank lines at point.
|
|
550 (skip-chars-forward " \t\n\r")
|
|
551 (beginning-of-line)
|
|
552 (while (and (setq start (point)
|
|
553 end (re-search-forward paragraph-start nil t))
|
|
554 (/= start end))
|
|
555 (setq contents (kimport:unindent-region start end))
|
|
556 (set-buffer output-to)
|
|
557 ;; Add the cell starting at point.
|
|
558 (kview:add-cell klabel output-level contents nil t)
|
|
559 (setq count (1+ count))
|
|
560 (message "%d of %d paragraphs converted..."
|
|
561 count total)
|
|
562 (setq klabel (klabel:increment klabel))
|
|
563 (set-buffer import-from)
|
|
564 (goto-char end)
|
|
565 ;; Move past blank lines separating paragraphs.
|
|
566 (skip-chars-forward " \t\n\r")
|
|
567 (beginning-of-line))
|
|
568 (message "%d of %d paragraphs converted" count total)
|
|
569 count))
|
|
570
|
|
571 (defun kimport:unindent-region (start end)
|
|
572 "Calculate indent based upon the second line within the region START to END.
|
|
573 Remove the indent and return the remaining region as a string."
|
|
574 (save-excursion
|
|
575 (let (indent-regexp)
|
|
576 (goto-char start)
|
|
577 ;; Remove leading indent from lines in paragraph. Base paragraph
|
|
578 ;; indent on the 2nd paragraph line since the first line might be
|
|
579 ;; further indented or outdented.
|
|
580 (setq indent-regexp
|
|
581 (if (re-search-forward "[\n\r][ \t]+" end t)
|
|
582 (concat "^" (make-string (current-column) ?\ ))))
|
|
583 (if indent-regexp
|
|
584 (hypb:replace-match-string
|
|
585 indent-regexp (buffer-substring start end) "" t)
|
|
586 (buffer-substring start end)))))
|
|
587
|
|
588 (provide 'kimport)
|
|
589
|