]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-markup.el
Merge branch 'master' into experimental
[factor.git] / misc / fuel / fuel-markup.el
1 ;;; fuel-markup.el -- printing factor help markup
2
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
5
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Thu Jan 01, 2009 21:43
9
10 ;;; Comentary:
11
12 ;; Utilities for printing Factor's help markup.
13
14 ;;; Code:
15
16 (require 'fuel-eval)
17 (require 'fuel-font-lock)
18 (require 'fuel-base)
19 (require 'fuel-table)
20
21 (require 'button)
22
23 \f
24 ;;; Customization:
25
26 (fuel-font-lock--defface fuel-font-lock-markup-title
27   'bold fuel-help "article titles in help buffers")
28
29 (fuel-font-lock--defface fuel-font-lock-markup-heading
30   'bold fuel-help "headlines in help buffers")
31
32 (fuel-font-lock--defface fuel-font-lock-markup-link
33   'link fuel-help "links to topics in help buffers")
34
35 (fuel-font-lock--defface fuel-font-lock-markup-emphasis
36   'italic fuel-help "emphasized words in help buffers")
37
38 (fuel-font-lock--defface fuel-font-lock-markup-strong
39   'link fuel-help "bold words in help buffers")
40
41 \f
42 ;;; Links:
43
44 (make-variable-buffer-local
45  (defvar fuel-markup--follow-link-function 'fuel-markup--echo-link))
46
47 (define-button-type 'fuel-markup--button
48   'action 'fuel-markup--follow-link
49   'face 'fuel-font-lock-markup-link
50   'follow-link t)
51
52 (defun fuel-markup--follow-link (button)
53   (when fuel-markup--follow-link-function
54     (funcall fuel-markup--follow-link-function
55              (button-get button 'markup-link)
56              (button-get button 'markup-label)
57              (button-get button 'markup-link-type))))
58
59 (defun fuel-markup--echo-link (link label type)
60   (message "Link %s pointing to %s named %s" label type link))
61
62 (defun fuel-markup--insert-button (label link type)
63   (let ((label (format "%s" label))
64         (link (if (listp link) link (format "%s" link))))
65     (insert-text-button label
66                         :type 'fuel-markup--button
67                         'markup-link link
68                         'markup-label label
69                         'markup-link-type type
70                         'help-echo (format "%s (%s)" label type))))
71
72 (defun fuel-markup--article-title (name)
73   (let ((name (if (listp name) (cons :seq name) name)))
74     (fuel-eval--retort-result
75      (fuel-eval--send/wait `(:fuel* ((,name fuel-get-article-title)) "fuel")))))
76
77 (defun fuel-markup--link-at-point ()
78   (let ((button (condition-case nil (forward-button 0) (error nil))))
79     (when button
80       (list (button-get button 'markup-link)
81             (button-get button 'markup-label)
82             (button-get button 'markup-link-type)))))
83
84 \f
85 ;;; Markup printers:
86
87 (defconst fuel-markup--printers
88   '(($all-tags . fuel-markup--all-tags)
89     ($all-authors . fuel-markup--all-authors)
90     ($author . fuel-markup--author)
91     ($authors . fuel-markup--authors)
92     ($class-description . fuel-markup--class-description)
93     ($code . fuel-markup--code)
94     ($command . fuel-markup--command)
95     ($command-map . fuel-markup--null)
96     ($contract . fuel-markup--contract)
97     ($curious . fuel-markup--curious)
98     ($definition . fuel-markup--definition)
99     ($describe-vocab . fuel-markup--describe-vocab)
100     ($description . fuel-markup--description)
101     ($doc-path . fuel-markup--doc-path)
102     ($emphasis . fuel-markup--emphasis)
103     ($error-description . fuel-markup--error-description)
104     ($errors . fuel-markup--errors)
105     ($example . fuel-markup--example)
106     ($examples . fuel-markup--examples)
107     ($heading . fuel-markup--heading)
108     ($index . fuel-markup--index)
109     ($instance . fuel-markup--instance)
110     ($io-error . fuel-markup--io-error)
111     ($link . fuel-markup--link)
112     ($links . fuel-markup--links)
113     ($list . fuel-markup--list)
114     ($low-level-note . fuel-markup--low-level-note)
115     ($markup-example . fuel-markup--markup-example)
116     ($maybe . fuel-markup--maybe)
117     ($methods . fuel-markup--methods)
118     ($nl . fuel-markup--newline)
119     ($notes . fuel-markup--notes)
120     ($operation . fuel-markup--link)
121     ($or . fuel-markup--or)
122     ($parsing-note . fuel-markup--parsing-note)
123     ($predicate . fuel-markup--predicate)
124     ($prettyprinting-note . fuel-markup--prettyprinting-note)
125     ($quotation . fuel-markup--quotation)
126     ($references . fuel-markup--references)
127     ($related . fuel-markup--related)
128     ($see . fuel-markup--see)
129     ($see-also . fuel-markup--see-also)
130     ($shuffle . fuel-markup--shuffle)
131     ($side-effects . fuel-markup--side-effects)
132     ($slot . fuel-markup--snippet)
133     ($snippet . fuel-markup--snippet)
134     ($strong . fuel-markup--strong)
135     ($subheading . fuel-markup--subheading)
136     ($subsection . fuel-markup--subsection)
137     ($synopsis . fuel-markup--synopsis)
138     ($syntax . fuel-markup--syntax)
139     ($table . fuel-markup--table)
140     ($tag . fuel-markup--tag)
141     ($tags . fuel-markup--tags)
142     ($unchecked-example . fuel-markup--example)
143     ($value . fuel-markup--value)
144     ($values . fuel-markup--values)
145     ($values-x/y . fuel-markup--values-x/y)
146     ($var-description . fuel-markup--var-description)
147     ($vocab-link . fuel-markup--vocab-link)
148     ($vocab-links . fuel-markup--vocab-links)
149     ($vocab-subsection . fuel-markup--vocab-subsection)
150     ($vocabulary . fuel-markup--vocabulary)
151     ($warning . fuel-markup--warning)
152     (article . fuel-markup--article)
153     (describe-words . fuel-markup--describe-words)
154     (vocab-list . fuel-markup--vocab-list)))
155
156 (make-variable-buffer-local
157  (defvar fuel-markup--maybe-nl nil))
158
159 (defun fuel-markup--print (e)
160   (cond ((null e) (insert "f"))
161         ((stringp e) (fuel-markup--insert-string e))
162         ((and (listp e) (symbolp (car e))
163               (assoc (car e) fuel-markup--printers))
164          (funcall (cdr (assoc (car e) fuel-markup--printers)) e))
165         ((and (symbolp e)
166               (assoc e fuel-markup--printers))
167          (funcall (cdr (assoc e fuel-markup--printers)) e))
168         ((listp e) (mapc 'fuel-markup--print e))
169         ((symbolp e) (fuel-markup--print (list '$link e)))
170         (t (insert (format "\n%S\n" e)))))
171
172 (defun fuel-markup--print-str (e)
173   (with-temp-buffer
174     (fuel-markup--print e)
175     (buffer-string)))
176
177 (defun fuel-markup--maybe-nl ()
178   (setq fuel-markup--maybe-nl (point)))
179
180 (defun fuel-markup--insert-newline (&optional justification nosqueeze)
181   (fill-region (save-excursion (beginning-of-line) (point))
182                (point)
183                (or justification 'left)
184                nosqueeze)
185   (newline))
186
187 (defsubst fuel-markup--insert-nl-if-nb (&optional no-fill)
188   (unless (eq (save-excursion (beginning-of-line) (point)) (point))
189     (if no-fill (newline) (fuel-markup--insert-newline))))
190
191 (defsubst fuel-markup--put-face (txt face)
192   (put-text-property 0 (length txt) 'font-lock-face face txt)
193   txt)
194
195 (defun fuel-markup--insert-heading (txt &optional no-nl)
196   (fuel-markup--insert-nl-if-nb)
197   (delete-blank-lines)
198   (unless (bobp) (newline))
199   (fuel-markup--put-face txt 'fuel-font-lock-markup-heading)
200   (fuel-markup--insert-string txt)
201   (unless no-nl (newline)))
202
203 (defun fuel-markup--insert-string (str)
204   (when fuel-markup--maybe-nl
205     (newline 2)
206     (setq fuel-markup--maybe-nl nil))
207   (insert str))
208
209 (defun fuel-markup--article (e)
210   (setq fuel-markup--maybe-nl nil)
211   (insert (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-title))
212   (newline 2)
213   (fuel-markup--print (car (cddr e))))
214
215 (defun fuel-markup--heading (e)
216   (fuel-markup--insert-heading (cadr e)))
217
218 (defun fuel-markup--subheading (e)
219   (fuel-markup--insert-heading (cadr e)))
220
221 (defun fuel-markup--subsection (e)
222   (fuel-markup--insert-nl-if-nb)
223   (insert "  - ")
224   (fuel-markup--link (cons '$link (cdr e)))
225   (fuel-markup--maybe-nl))
226
227 (defun fuel-markup--vocab-subsection (e)
228   (fuel-markup--insert-nl-if-nb)
229   (insert "  - ")
230   (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
231   (fuel-markup--maybe-nl))
232
233 (defun fuel-markup--newline (e)
234   (fuel-markup--insert-newline)
235   (newline))
236
237 (defun fuel-markup--doc-path (e)
238   (fuel-markup--insert-heading "Related topics")
239   (insert "  ")
240   (dolist (art (cdr e))
241     (fuel-markup--insert-button (car art) (cadr art) 'article)
242     (insert ", "))
243   (delete-backward-char 2)
244   (fuel-markup--insert-newline 'left))
245
246 (defun fuel-markup--emphasis (e)
247   (when (stringp (cadr e))
248     (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-emphasis)
249     (insert (cadr e))))
250
251 (defun fuel-markup--strong (e)
252   (when (stringp (cadr e))
253     (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-strong)
254     (insert (cadr e))))
255
256 (defun fuel-markup--snippet (e)
257   (insert (mapconcat '(lambda (s)
258                         (if (stringp s)
259                             (fuel-font-lock--factor-str s)
260                           (fuel-markup--print-str s)))
261                      (cdr e)
262                      " ")))
263
264 (defun fuel-markup--code (e)
265   (fuel-markup--insert-nl-if-nb)
266   (newline)
267   (dolist (snip (cdr e))
268     (if (stringp snip)
269         (insert (fuel-font-lock--factor-str snip))
270       (fuel-markup--print snip))
271     (newline))
272   (newline))
273
274 (defun fuel-markup--command (e)
275   (fuel-markup--snippet (list '$snippet (nth 3 e))))
276
277 (defun fuel-markup--syntax (e)
278   (fuel-markup--insert-heading "Syntax")
279   (fuel-markup--print (cons '$code (cdr e)))
280   (newline))
281
282 (defun fuel-markup--example (e)
283   (fuel-markup--insert-newline)
284   (dolist (s (cdr e))
285     (fuel-markup--snippet (list '$snippet s))
286     (newline))
287   (newline))
288
289 (defun fuel-markup--markup-example (e)
290   (fuel-markup--insert-newline)
291   (fuel-markup--snippet (cons '$snippet (cdr e))))
292
293 (defun fuel-markup--link (e)
294   (let* ((link (or (nth 1 e) 'f))
295          (type (or (nth 3 e) (if (symbolp link) 'word 'article)))
296          (label (or (nth 2 e)
297                     (and (eq type 'article)
298                          (fuel-markup--article-title link))
299                     link)))
300     (fuel-markup--insert-button label link type)))
301
302 (defun fuel-markup--links (e)
303   (dolist (link (cdr e))
304     (fuel-markup--link (list '$link link))
305     (insert ", "))
306   (delete-backward-char 2))
307
308 (defun fuel-markup--index-quotation (q)
309   (cond ((null q) null)
310         ((listp q) (vconcat (mapcar 'fuel-markup--index-quotation q)))
311         (t q)))
312
313 (defun fuel-markup--index (e)
314   (let* ((q (fuel-markup--index-quotation (cadr e)))
315          (cmd `(:fuel* ((,q fuel-index)) "fuel"
316                        ("builtins" "help" "help.topics" "classes"
317                         "classes.builtin" "classes.tuple"
318                         "classes.singleton" "classes.union"
319                         "classes.intersection" "classes.predicate")))
320          (subs (fuel-eval--retort-result (fuel-eval--send/wait cmd 200))))
321     (when subs
322       (let ((start (point))
323             (sort-fold-case nil))
324         (fuel-markup--print subs)
325         (sort-lines nil start (point))))))
326
327 (defun fuel-markup--vocab-link (e)
328   (fuel-markup--insert-button (cadr e) (or (car (cddr e)) (cadr e)) 'vocab))
329
330 (defun fuel-markup--vocab-links (e)
331   (dolist (link (cdr e))
332     (insert " ")
333     (fuel-markup--vocab-link (list '$vocab-link link))
334     (insert " ")))
335
336 (defun fuel-markup--vocab-list (e)
337   (let ((rows (mapcar '(lambda (elem)
338                          (list (car elem)
339                                (list '$vocab-link (cadr elem))
340                                (caddr elem)))
341                       (cdr e))))
342     (fuel-markup--table (cons '$table rows))))
343
344 (defun fuel-markup--describe-vocab (e)
345   (fuel-markup--insert-nl-if-nb)
346   (let* ((cmd `(:fuel* ((,(cadr e) fuel-vocab-help)) "fuel" t))
347          (res (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
348     (when res (fuel-markup--print res))))
349
350 (defun fuel-markup--vocabulary (e)
351   (fuel-markup--insert-heading "Vocabulary: " t)
352   (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
353   (newline))
354
355 (defun fuel-markup--parse-classes ()
356   (let ((elems))
357     (while (looking-at ".+ classes$")
358       (let ((heading `($heading ,(match-string-no-properties 0)))
359             (rows))
360         (forward-line)
361         (when (looking-at "Class *.+$")
362           (push (split-string (match-string-no-properties 0) nil t) rows)
363           (forward-line))
364         (while (not (looking-at "$"))
365           (let* ((objs (split-string (thing-at-point 'line) nil t))
366                  (class (list '$link (car objs) (car objs) 'word))
367                  (super (and (cadr objs)
368                              (list (list '$link (cadr objs) (cadr objs) 'word))))
369                  (slots (when (cddr objs)
370                           (list (mapcar '(lambda (s) (list s " ")) (cddr objs))))))
371             (push `(,class ,@super ,@slots) rows))
372           (forward-line))
373         (push `(,heading ($table ,@(reverse rows))) elems))
374       (forward-line))
375     (reverse elems)))
376
377 (defun fuel-markup--parse-words ()
378   (let ((elems))
379     (while (looking-at ".+ words\\|Primitives$")
380       (let ((heading `($heading ,(match-string-no-properties 0)))
381             (rows))
382         (forward-line)
383         (when (looking-at "Word *\\(Stack effect\\|Syntax\\)$")
384           (push (list "Word" (match-string-no-properties 1)) rows)
385           (forward-line))
386         (while (looking-at "\\(.+?\\)\\( +\\(.+\\)\\)?$")
387           (let ((word `($link ,(match-string-no-properties 1)
388                               ,(match-string-no-properties 1)
389                               word))
390                 (se (and (match-string-no-properties 3)
391                          `(($snippet ,(match-string-no-properties 3))))))
392             (push `(,word ,@se) rows))
393           (forward-line))
394         (push `(,heading ($table ,@(reverse rows))) elems))
395       (forward-line))
396     (reverse elems)))
397
398 (defun fuel-markup--parse-words-desc (desc)
399   (with-temp-buffer
400     (insert desc)
401     (goto-char (point-min))
402     (when (re-search-forward "^Words$" nil t)
403       (forward-line 2)
404       (let ((elems '(($heading "Words"))))
405         (push (fuel-markup--parse-classes) elems)
406         (push (fuel-markup--parse-words) elems)
407         (reverse elems)))))
408
409 (defun fuel-markup--describe-words (e)
410   (when (cadr e)
411     (fuel-markup--print (fuel-markup--parse-words-desc (cadr e)))))
412
413 (defun fuel-markup--tag (e)
414   (fuel-markup--link (list '$link (cadr e) (cadr e) 'tag)))
415
416 (defun fuel-markup--tags (e)
417   (when (cdr e)
418     (fuel-markup--insert-heading "Tags: " t)
419     (dolist (tag (cdr e))
420       (fuel-markup--tag (list '$tag tag))
421       (insert ", "))
422     (delete-backward-char 2)
423     (fuel-markup--insert-newline)))
424
425 (defun fuel-markup--all-tags (e)
426   (let* ((cmd `(:fuel* (all-tags :get) "fuel" t))
427          (tags (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
428     (fuel-markup--list
429      (cons '$list (mapcar (lambda (tag) (list '$link tag tag 'tag)) tags)))))
430
431 (defun fuel-markup--author (e)
432   (fuel-markup--link (list '$link (cadr e) (cadr e) 'author)))
433
434 (defun fuel-markup--authors (e)
435   (when (cdr e)
436     (fuel-markup--insert-heading "Authors: " t)
437     (dolist (a (cdr e))
438       (fuel-markup--author (list '$author a))
439       (insert ", "))
440     (delete-backward-char 2)
441     (fuel-markup--insert-newline)))
442
443 (defun fuel-markup--all-authors (e)
444   (let* ((cmd `(:fuel* (all-authors :get) "fuel" t))
445          (authors (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
446     (fuel-markup--list
447      (cons '$list (mapcar (lambda (a) (list '$link a a 'author)) authors)))))
448
449 (defun fuel-markup--list (e)
450   (fuel-markup--insert-nl-if-nb)
451   (dolist (elt (cdr e))
452     (insert " - ")
453     (fuel-markup--print elt)
454     (fuel-markup--insert-newline)))
455
456 (defun fuel-markup--table (e)
457   (fuel-markup--insert-newline)
458   (delete-blank-lines)
459   (newline)
460   (fuel-table--insert
461    (mapcar '(lambda (row) (mapcar 'fuel-markup--print-str row)) (cdr e)))
462   (newline))
463
464 (defun fuel-markup--instance (e)
465   (insert " an instance of ")
466   (fuel-markup--print (cadr e)))
467
468 (defun fuel-markup--maybe (e)
469   (fuel-markup--instance (cons '$instance (cdr e)))
470   (insert " or f "))
471
472 (defun fuel-markup--or (e)
473   (let ((fst (car (cdr e)))
474         (mid (butlast (cddr e)))
475         (lst (car (last (cdr e)))))
476     (insert (format "%s" fst))
477     (dolist (m mid) (insert (format ", %s" m)))
478     (insert (format " or %s" lst))))
479
480 (defun fuel-markup--values (e)
481   (fuel-markup--insert-heading "Inputs and outputs")
482   (dolist (val (cdr e))
483     (insert " " (car val) " - ")
484     (fuel-markup--print (cdr val))
485     (newline)))
486
487 (defun fuel-markup--predicate (e)
488   (fuel-markup--values '($values ("object" object) ("?" "a boolean")))
489   (let ((word (make-symbol (substring (format "%s" (cadr e)) 0 -1))))
490   (fuel-markup--description
491    `($description "Tests if the object is an instance of the "
492                   ($link ,word) " class."))))
493
494 (defun fuel-markup--side-effects (e)
495   (fuel-markup--insert-heading "Side effects")
496   (insert "Modifies ")
497   (fuel-markup--print (cdr e))
498   (fuel-markup--insert-newline))
499
500 (defun fuel-markup--definition (e)
501   (fuel-markup--insert-heading "Definition")
502   (fuel-markup--code (cons '$code (cdr e))))
503
504 (defun fuel-markup--methods (e)
505   (fuel-markup--insert-heading "Methods")
506   (fuel-markup--code (cons '$code (cdr e))))
507
508 (defun fuel-markup--value (e)
509   (fuel-markup--insert-heading "Variable value")
510   (insert "Current value in global namespace: ")
511   (fuel-markup--snippet (cons '$snippet (cdr e)))
512   (newline))
513
514 (defun fuel-markup--values-x/y (e)
515   (fuel-markup--values '($values ("x" "number") ("y" "number"))))
516
517 (defun fuel-markup--curious (e)
518   (fuel-markup--insert-heading "For the curious...")
519   (fuel-markup--print (cdr e)))
520
521 (defun fuel-markup--references (e)
522   (fuel-markup--insert-heading "References")
523   (dolist (ref (cdr e))
524     (if (listp ref)
525         (fuel-markup--print ref)
526       (fuel-markup--subsection (list '$subsection ref)))))
527
528 (defun fuel-markup--see-also (e)
529   (fuel-markup--insert-heading "See also")
530   (fuel-markup--links (cons '$links (cdr e))))
531
532 (defun fuel-markup--related (e)
533   (fuel-markup--insert-heading "See also")
534   (fuel-markup--links (cons '$links (cadr e))))
535
536 (defun fuel-markup--shuffle (e)
537   (insert "\nShuffle word. Re-arranges the stack "
538           "according to the stack effect pattern.")
539   (fuel-markup--insert-newline))
540
541 (defun fuel-markup--low-level-note (e)
542   (fuel-markup--print '($notes "Calling this word directly is not necessary "
543                                "in most cases. "
544                                "Higher-level words call it automatically.")))
545
546 (defun fuel-markup--parsing-note (e)
547   (fuel-markup--insert-nl-if-nb)
548   (insert "This word should only be called from parsing words.")
549   (fuel-markup--insert-newline))
550
551 (defun fuel-markup--io-error (e)
552   (fuel-markup--errors '($errors "Throws an error if the I/O operation fails.")))
553
554 (defun fuel-markup--prettyprinting-note (e)
555   (fuel-markup--print '($notes ("This word should only be called within the "
556                                 ($link with-pprint) " combinator."))))
557
558 (defun fuel-markup--elem-with-heading (elem heading)
559   (fuel-markup--insert-heading heading)
560   (fuel-markup--print (cdr elem))
561   (fuel-markup--insert-newline))
562
563 (defun fuel-markup--quotation (e)
564   (insert "a ")
565   (fuel-markup--link (list '$link 'quotation 'quotation 'word))
566   (insert " with stack effect ")
567   (fuel-markup--snippet (list '$snippet (nth 1 e))))
568
569 (defun fuel-markup--warning (e)
570   (fuel-markup--elem-with-heading e "Warning"))
571
572 (defun fuel-markup--description (e)
573   (fuel-markup--elem-with-heading e "Word description"))
574
575 (defun fuel-markup--class-description (e)
576   (fuel-markup--elem-with-heading e "Class description"))
577
578 (defun fuel-markup--error-description (e)
579   (fuel-markup--elem-with-heading e "Error description"))
580
581 (defun fuel-markup--var-description (e)
582   (fuel-markup--elem-with-heading e "Variable description"))
583
584 (defun fuel-markup--contract (e)
585   (fuel-markup--elem-with-heading e "Generic word contract"))
586
587 (defun fuel-markup--errors (e)
588   (fuel-markup--elem-with-heading e "Errors"))
589
590 (defun fuel-markup--examples (e)
591   (fuel-markup--elem-with-heading e "Examples"))
592
593 (defun fuel-markup--notes (e)
594   (fuel-markup--elem-with-heading e "Notes"))
595
596 (defun fuel-markup--word-info (e s)
597   (let* ((word (nth 1 e))
598          (cmd (and word `(:fuel* ((:quote ,(format "%s" word)) ,s) "fuel")))
599          (ret (and cmd (fuel-eval--send/wait cmd)))
600          (res (and (not (fuel-eval--retort-error ret))
601                    (fuel-eval--retort-output ret))))
602     (if res
603         (fuel-markup--code (list '$code res))
604       (fuel-markup--snippet (list '$snippet " " word)))))
605
606 (defun fuel-markup--see (e)
607   (fuel-markup--word-info e 'see))
608
609 (defun fuel-markup--synopsis (e)
610   (fuel-markup--word-info e 'synopsis))
611
612 (defun fuel-markup--null (e))
613
614 \f
615 (provide 'fuel-markup)
616 ;;; fuel-markup.el ends here