Source

hyperbole / kotl / kfile.el

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
;;!emacs
;;
;; FILE:         kfile.el
;; SUMMARY:      Save and restore kotls from files.
;; USAGE:        GNU Emacs V19 Lisp Library
;; KEYWORDS:     outlines, wp
;;
;; AUTHOR:       Bob Weiner
;; ORG:          BeOpen.com
;;
;; ORIG-DATE:    10/31/93
;; LAST-MOD:     13-Jun-99 at 02:13:07 by Bob Weiner
;;
;; Copyright (C) 1993-1999  BeOpen.com
;; See the "../HY-COPY" file for license information.
;;
;; This file is part of Hyperbole.

;;; ************************************************************************
;;; Other required Elisp libraries
;;; ************************************************************************

(mapcar 'require '(kproperty kotl-mode))

;; Loads menus under non-tty InfoDock, XEmacs or Emacs19; does nothing
;; otherwise.
(and (not (featurep 'kmenu)) hyperb:window-system
     (or hyperb:xemacs-p hyperb:emacs19-p) (require 'kmenu))

;;; ************************************************************************
;;; Public variables
;;; ************************************************************************

(defconst kfile:version "Kotl-4.0"
  "Version number of persistent data format used for saving koutlines.")

;;; ************************************************************************
;;; Entry Points
;;; ************************************************************************

;;;###autoload
(defun kfile:find (file-name)
  "Find a file FILE-NAME containing a kotl or create one if none exists.
Return the new kview."
  (interactive
   (list (kfile:read-name
	  "Find koutline file: " nil)))
  (let ((existing-file (file-exists-p file-name))
	buffer)
    (and existing-file
	 (not (file-readable-p file-name))
	 (error
	  "(kfile:find): \"%s\" is not readable.  Check permissions."
	  file-name))
    (setq buffer (find-file file-name))
    ;; Finding the file may have already done a kfile:read as invoked through
    ;; kotl-mode via a file local variable setting.  If so, don't read it
    ;; again.
    (if (kview:is-p kview)
	nil
      (kfile:read buffer existing-file))
    (or (eq major-mode 'kotl-mode) (kotl-mode))
    kview))

;;;###autoload
(defun kfile:view (file-name)
  "View an existing kotl version-2 file FILE-NAME in a read-only mode."
  (interactive
   (list (kfile:read-name
	  "View koutline file: " t)))
  (let ((existing-file (file-exists-p file-name)))
    (if existing-file
	(if (not (file-readable-p file-name))
	    (error
	     "(kfile:view): \"%s\" is not readable.  Check permissions."
	     file-name))
      (error "(kfile:view): \"%s\" does not exist."))
    (view-file file-name))
    (kfile:narrow-to-kcells)
    (goto-char (point-min)))

;;; ************************************************************************
;;; Public functions
;;; ************************************************************************

(defun kfile:create (buffer)
  "Create a new koutline file attached to BUFFER, with a single empty level 1 kotl cell.
Return file's kview."
  (or buffer (setq buffer (current-buffer)))
  (if (not (bufferp buffer))
      (error "(kfile:create): Invalid buffer argument, %s" buffer))
  (set-buffer buffer)
  (if buffer-read-only
      (error "(kfile:create): %s is read-only" buffer))
  (widen)

  (let ((empty-p (zerop (buffer-size)))
	import-from view standard-output)

    (if (not empty-p)
	;; This is a foreign file whose elements must be converted into
	;; koutline cells.
	(progn (setq import-from (kimport:copy-and-set-buffer buffer))
	       (set-buffer buffer)
	       (erase-buffer))) ;; We copied the contents to `import-from'.

    (setq view (kview:create (buffer-name buffer))
	  standard-output (current-buffer))
    (goto-char (point-min))
    (princ ";; -*- Mode: kotl -*- \n")
    (prin1 kfile:version)
    (princ " ;; file-format\n\^_\n")
    ;; Ensure that last cell has two newlines after it so that
    ;; kfile:insert-attributes finds it.
    (goto-char (point-max))
    (princ "\n\n\^_\n")
    (princ "\^_\n;; depth-first kcell attributes\n")
    ;; Ensure that display is narrowed to cell region only.
    (kfile:narrow-to-kcells)
    (goto-char (point-min))
    (if empty-p
	;; This is a new koutline file.  Always need at least one visible
	;; cell within a view. Insert initial empty cell.
	(progn (kview:add-cell "1" 1)
	       ;; Mark view unmodified, so if kill right away, there is no
	       ;; prompt.
	       (set-buffer-modified-p nil)
	       ;; Move to first cell.
	       (goto-char (point-min))
	       (goto-char (kcell-view:start)))
      ;; Import buffer.  Next line is necessary or the importation will fail.
      (delete-region (point-min) (point-max)) 
      ;; Import foreign buffer as koutline cells.
      (kimport:file import-from (current-buffer))
      ;; If import buffer name starts with a space, kill it, as it is no
      ;; longer needed.
      (if (eq ?\ (aref (buffer-name import-from) 0))
	  (kill-buffer import-from)))

    view))

;;;###autoload
(defun kfile:is-p ()
  "Iff current buffer contains an unformatted or formatted koutline, return file format version string, else nil."
  (let (ver-string)
    (save-excursion
      (save-restriction
	(widen)
	(goto-char (point-min))
	(condition-case ()
	    (progn
	      (setq ver-string (read (current-buffer)))
	      (and (stringp ver-string) (string-match "^Kotl-" ver-string)
		   ver-string))
	  (error nil))))))

(defun kfile:read (buffer existing-file-p &optional ver-string)
  "Create a new kotl view by reading BUFFER or create an empty view when EXISTING-FILE-P is nil.
Optional VER-STRING is the outline format version number for the BUFFER that
was previously read by calling `kfile:is-p'.

Return the new view."
  (cond ((not (bufferp buffer))
	 (error "(kfile:read): Argument must be a buffer, `%s'." buffer))
	((and (zerop (buffer-size)) (not existing-file-p))
	 (kfile:create buffer))
	((progn
	   (set-buffer buffer)
	   (not (or (stringp ver-string) (setq ver-string (kfile:is-p)))))
	 (error "(kfile:read): `%s' is not a koutline file." buffer))
	((equal ver-string "Kotl-4.0")
	 (kfile:read-v4-or-v3 buffer nil))
	((equal ver-string "Kotl-3.0")
	 (kfile:read-v4-or-v3 buffer t))
	((equal ver-string "Kotl-2.0")
	 (kfile:read-v2 buffer))
	((equal ver-string "Kotl-1.0")
	 (error "(kfile:read): V1 koutlines are no longer supported"))
	(t (error "(kfile:read): `%s' has unknown kotl version, %s."
		  buffer ver-string))))

(defun kfile:read-v2 (buffer)
  "Create a kotl view by reading kotl version-2 BUFFER.  Return the new view."
  (let ((standard-input buffer)
	cell-count label-type label-min-width label-separator
	level-indent cell-data kotl-structure view kcell-list)
    (widen)
    (goto-char (point-min))
    ;; Skip past cell contents here.
    (search-forward "\n\^_" nil t 2)
    ;; Read rest of file data.
    (setq cell-count (read)
	  label-type (read)
	  label-min-width (read)
	  label-separator (read)
	  level-indent (read)
	  cell-data (read)
	  kotl-structure (read))
    ;;
    ;; kcell-list is a depth-first list of kcells to be attached to the cell
    ;; contents within the kview down below.
    (setq kcell-list (kfile:build-structure-v2 kotl-structure cell-data)
	  view (kview:create (buffer-name buffer) cell-count label-type
				 level-indent label-separator label-min-width))
    ;;
    (kfile:narrow-to-kcells)
    (goto-char (point-min))
    ;;
    ;; Add attributes to cells.
    (kfile:insert-attributes-v2 view kcell-list)
    ;;
    ;; Mark view unmodified and move to first cell.
    (set-buffer-modified-p nil)
    (goto-char (point-min))
    (goto-char (kcell-view:start))
    view))

(defun kfile:read-v4-or-v3 (buffer v3-flag)
  "Create a koutline view by reading version-4 BUFFER.  Return the new view.
If V3-FLAG is true, read as a version-3 buffer."
  (let ((standard-input buffer)
	cell-count label-type label-min-width label-separator
	level-indent cell-data view)
    (widen)
    (goto-char (point-min))
    ;; Skip past cell contents here.
    (search-forward "\n\^_" nil t 2)
    ;; Read rest of file data.
    (if v3-flag
	nil ;; V3 files did not store viewspecs.
      (kvspec:initialize)
      (setq kvspec:current (read)))
    (setq cell-count (read)
	  label-type (read)
	  label-min-width (read)
	  label-separator (read)
	  level-indent (read)
	  cell-data (read))
    ;;
    (setq view (kview:create (buffer-name buffer) cell-count label-type
			     level-indent label-separator label-min-width))
    ;;
    (kfile:narrow-to-kcells)
    (goto-char (point-min))
    ;;
    ;; Add attributes to cells.
    (kfile:insert-attributes-v3 view cell-data)
    ;;
    ;; Mark view unmodified and move to first cell.
    (set-buffer-modified-p nil)
    (goto-char (point-min))
    (goto-char (kcell-view:start))
    view))

(defun kfile:update (&optional visible-only-p)
  "Update kfile internal structure so that view is ready for saving to a file.
Leave outline file expanded with structure data showing unless optional
VISIBLE-ONLY-P is non-nil.  Signal an error if kotl is not attached to a file."
  (let* ((top (kview:top-cell kview))
	 (file (kcell:get-attr top 'file))
	 (label-type (kview:label-type kview))
	 (label-min-width (kview:label-min-width kview))
	 (label-separator (kview:label-separator kview))
	 (level-indent (kview:level-indent kview))
	 ;; If this happens to be non-nil, it is virtually impossible to save
	 ;; a file, so ensure it is nil.
	 (debug-on-error))
    (cond ((null file)
	   (error "(kfile:update): Current outline is not attached to a file."))
	  ((not (file-writable-p file))
	   (error "(kfile:update): File \"%s\" is not writable." file)))
    (let* ((buffer-read-only)
	   (id-counter (kcell:get-attr top 'id-counter))
	   (kotl-data (make-vector (1+ id-counter) nil))
	   (standard-output (current-buffer))
	   (opoint (set-marker (make-marker) (point)))
	   (kcell-num 1)
	   cell)
      ;;
      ;; Prepare cell data for saving.
      (kfile:narrow-to-kcells)
      (kview:map-tree
	(function
	  (lambda (view)
	    (setq cell (kcell-view:cell))
	    (aset kotl-data
		  kcell-num
		  (kotl-data:create cell))
	    (setq kcell-num (1+ kcell-num))))
	kview t)
      ;; Save top cell, 0, last since above loop may increment the total
      ;; number of cells counter stored in it, if any invalid cells are
      ;; encountered. 
      (aset kotl-data 0 (kotl-data:create top))
      (setq id-counter (kcell:get-attr top 'id-counter))
      ;;
      (widen)
      (goto-char (point-min))
      (if (search-forward "\n\^_\n" nil t)
	  (delete-region (point-min) (match-end 0)))
      (princ ";; -*- Mode: kotl -*- \n")
      (prin1 kfile:version)
      (princ " ;; file-format\n\^_\n")
      ;; Skip past cells.
      (if (search-forward "\n\^_\n" nil t)
	  ;; Get rid of excess blank lines after last cell.
	  (progn (goto-char (match-beginning 0))
		 (skip-chars-backward "\n")
		 (delete-region (point) (point-max)))
	(goto-char (point-max)))
      ;; Ensure that last cell has two newlines after it so that
      ;; kfile:insert-attributes finds it.
      (princ "\n\n\^_\n")
      (princ (format (concat
		      "%S ;; kvspec:current\n%d ;; id-counter\n"
		      "%S ;; label-type\n%d ;; label-min-width\n"
		      "%S ;; label-separator\n%d ;; level-indent\n")
		     kvspec:current id-counter label-type label-min-width
		     label-separator level-indent))
      (princ "\^_\n;; depth-first kcell attributes\n")
      (kfile:pretty-print kotl-data)
      ;;
      ;; Don't re-narrow buffer by default since this is used in
      ;; write-contents-hooks after save-buffer has widened buffer.  If
      ;; buffer is narrowed here, only the narrowed portion will be saved to
      ;; the file.  Narrow as an option since saving only the portion of the
      ;; file visible in a view may be useful in some situations.
      (if visible-only-p (kfile:narrow-to-kcells))
      ;;
      ;; Return point to its original position as given by the opoint marker.
      (goto-char opoint)
      (set-marker opoint nil)
      nil)))

;;; Next function is adapted from `file-write' of GNU Emacs 19, copyright FSF,
;;; under the GPL.
(defun kfile:write (file)
  "Write current outline to FILE."
  (interactive "FWrite outline file: ")
  (if (or (null file) (string-equal file ""))
      (error "(kfile:write): Invalid file name, \"%s\"" file))
  ;; If arg is just a directory, use same file name, but in that directory.
  (if (and (file-directory-p file) buffer-file-name)
      (setq file (concat (file-name-as-directory file)
			 (file-name-nondirectory buffer-file-name))))
  (kcell:set-attr (kview:top-cell kview) 'file file)
  (set-visited-file-name file)
  ;; Set-visited-file-name clears local-write-file-hooks that we use to save
  ;; koutlines properly, so reinitialize local variables.
  (kotl-mode)
  (set-buffer-modified-p t)
  ;; This next line must come before the save-buffer since write-file-hooks
  ;; can make use of it.
  (kview:set-buffer-name kview (buffer-name))
  (save-buffer))

;;; ************************************************************************
;;; Private functions
;;; ************************************************************************

(defun kfile:build-structure-v2 (kotl-structure cell-data)
  "Build cell list from the KOTL-STRUCTURE and its CELL-DATA.
Assumes all arguments are valid.  CELL-DATA is a vector of cell fields read
from a koutline file.

Return list of outline cells in depth first order.  Invisible top cell is not
included in the list."
  (let ((stack) (sibling-p) (cell-list) func cell)
    (mapcar
     (function
      (lambda (item)
	(setq func (cdr (assoc item
			       (list
				(cons "\("
				      (function
				       (lambda ()
					 (setq stack (cons sibling-p stack)
					       sibling-p nil))))
				(cons "\)" 
				      (function
				       (lambda ()
					 (setq sibling-p (car stack)
					       stack (cdr stack)))))))))
	(cond (func (funcall func))
	      ;; 0th cell was created with kview:create.
	      ((equal item 0) nil)
	      (t (setq cell (kotl-data:to-kcell-v2 (aref cell-data item))
		       cell-list (cons cell cell-list)
		       sibling-p t)
		 ))))
     kotl-structure)
    (nreverse cell-list)))

(defun kfile:insert-attributes-v2 (kview kcell-list)
  "Set cell attributes within kview for each element in KCELL-LIST.
Assumes all cell contents are already in kview and that no cells are
hidden."
  (let (buffer-read-only)
    (while
	(progn
	  (skip-chars-forward "\n")
	  ;; !!! Won't work if label-type is 'no.
	  ;; Here we search past the cell identifier
	  ;; for the location at which to place cell properties.
	  ;; Be sure not to skip past a period which may terminate the label.
	  (if (re-search-forward "[A-Za-z0-9]\\(\\.?[A-Za-z0-9]\\)*" nil t)
	      (progn
		(kproperty:set 'kcell (car kcell-list))
		(setq kcell-list (cdr kcell-list))))
	  (search-forward "\n\n" nil t)))))

(defun kfile:insert-attributes-v3 (kview kcell-vector)
  "Set cell attributes within kview for each element in KCELL-VECTOR.
Assumes all cell contents are already in kview and that no cells are
hidden."
  (let ((kcell-num 1)
	(buffer-read-only))
    (while
	(progn
	  (skip-chars-forward "\n")
	  ;; !!! Won't work if label-type is 'no.
	  ;; Here we search past the cell identifier
	  ;; for the location at which to place cell properties.
	  ;; Be sure not to skip past a period which may terminate the label.
	  (if (re-search-forward "[A-Za-z0-9]\\(\\.?[A-Za-z0-9]\\)*" nil t)
	      (progn
		(kproperty:set 'kcell
			       (kotl-data:to-kcell-v3
				(aref kcell-vector kcell-num)))
		(setq kcell-num (1+ kcell-num))))
	  (search-forward "\n\n" nil t)))))

(defun kfile:narrow-to-kcells ()
  "Narrow kotl file to kcell section only."
  (interactive)
  (if (kview:is-p kview)
      (let ((start-text) (end-text))
	(save-excursion
	  (widen)
	  (goto-char (point-min))
	  ;; Skip to start of kcells.
	  (if (search-forward "\n\^_" nil t)
	      (setq start-text (1+ (match-end 0))))
	  ;; Skip past end of kcells.
	  (if (and start-text (search-forward "\n\^_" nil t))
	      (setq end-text (1+ (match-beginning 0))))
	  (if (and start-text end-text)
	      (progn (narrow-to-region start-text end-text)
		     (goto-char (point-min)))
	    (error
	     "(kfile:narrow-to-kcells): Cannot find start or end of kcells"))
	  ))))

(defun kfile:print-to-string (object)
  "Return a string containing OBJECT, any Lisp object, in pretty-printed form.
Quoting characters are used when needed to make output that `read' can
handle, whenever this is possible."
  (save-excursion
    (set-buffer (get-buffer-create " kfile:print-to-string"))
    (let ((emacs-lisp-mode-hook)
	  (buffer-read-only))
      (erase-buffer)
      (unwind-protect
	  (progn
	    (emacs-lisp-mode)
	    (let ((print-escape-newlines kfile:escape-newlines))
	      (prin1 object (current-buffer)))
	    (goto-char (point-min))
	    (while (not (eobp))
	      ;; (message "%06d" (- (point-max) (point)))
	      (cond
	       ((looking-at "\\s\(")
		(while (looking-at "\\s(")
		  (forward-char 1)))
	       ((and (looking-at "\\(quote[ \t]+\\)\\([^.)]\\)")
		     (> (match-beginning 1) 1)
		     (eq ?\( (char-after (1- (match-beginning 1))))
		     ;; Make sure this is a two-element list.
		     (save-excursion
		       (goto-char (match-beginning 2))
		       (forward-sexp)
		       ;; (looking-at "[ \t]*\)")
		       ;; Avoid mucking with match-data
		       (eq ?\) (char-after (point)))))
		;; -1 gets the paren preceding the quote as well.
		(delete-region (1- (match-beginning 1)) (match-end 1))
		(insert "'")
		(forward-sexp 1)
		(if (looking-at "[ \t]*\)")
		    (delete-region (match-beginning 0) (match-end 0))
		  (error "Malformed quote"))
		(backward-sexp 1))	      
	       ((condition-case ()
		    (prog1 t (down-list 1))
		  (error nil))
		(backward-char 1)
		(skip-chars-backward " \t")
		(delete-region
		 (point)
		 (progn (skip-chars-forward " \t") (point)))
		(if (not (eq ?' (char-after (1- (point)))))
		    (insert ?\n)))
	       ((condition-case ()
		    (prog1 t (up-list 1))
		  (error nil))
		(while (looking-at "\\s)")
		  (forward-char 1))
		(skip-chars-backward " \t")
		(delete-region
		 (point)
		 (progn (skip-chars-forward " \t") (point)))
		(if (not (eq ?' (char-after (1- (point)))))
		    (insert ?\n)))
	       (t (goto-char (point-max)))))
	    (goto-char (point-min))
	    (indent-sexp)
	    (buffer-string))
	(kill-buffer (current-buffer))))))

(defun kfile:pretty-print (object &optional stream)
  "Output the pretty-printed representation of OBJECT, any Lisp object.
Quoting characters are printed when needed to make output that `read'
can handle, whenever this is possible.
Output stream is STREAM, or value of `standard-output' (which see)."
  (princ (kfile:print-to-string object) (or stream standard-output)))

(defun kfile:read-name (prompt existing-p)
  "PROMPT for and read a koutline file name.  EXISTING-P means must exist."
  (let ((filename))
    (while (not filename)
      (setq filename (read-file-name prompt nil nil existing-p))
      (if (or (null filename) (equal filename ""))
	  (progn (ding) (setq filename nil))))
    filename))

;;; ************************************************************************
;;; Private variables
;;; ************************************************************************

(defvar kfile:escape-newlines t 
  "Value of print-escape-newlines used by `kfile:print-to-string' function.")

(provide 'kfile)