]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-markup.el
FUEL: Even better $see handling in help browser.
[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     ($parsing-note . fuel-markup--parsing-note)
122     ($predicate . fuel-markup--predicate)
123     ($prettyprinting-note . fuel-markup--prettyprinting-note)
124     ($quotation . fuel-markup--quotation)
125     ($references . fuel-markup--references)
126     ($related . fuel-markup--related)
127     ($see . fuel-markup--see)
128     ($see-also . fuel-markup--see-also)
129     ($shuffle . fuel-markup--shuffle)
130     ($side-effects . fuel-markup--side-effects)
131     ($slot . fuel-markup--snippet)
132     ($snippet . fuel-markup--snippet)
133     ($strong . fuel-markup--strong)
134     ($subheading . fuel-markup--subheading)
135     ($subsection . fuel-markup--subsection)
136     ($synopsis . fuel-markup--synopsis)
137     ($syntax . fuel-markup--syntax)
138     ($table . fuel-markup--table)
139     ($tag . fuel-markup--tag)
140     ($tags . fuel-markup--tags)
141     ($unchecked-example . fuel-markup--example)
142     ($value . fuel-markup--value)
143     ($values . fuel-markup--values)
144     ($values-x/y . fuel-markup--values-x/y)
145     ($var-description . fuel-markup--var-description)
146     ($vocab-link . fuel-markup--vocab-link)
147     ($vocab-links . fuel-markup--vocab-links)
148     ($vocab-subsection . fuel-markup--vocab-subsection)
149     ($vocabulary . fuel-markup--vocabulary)
150     ($warning . fuel-markup--warning)
151     (article . fuel-markup--article)
152     (describe-words . fuel-markup--describe-words)
153     (vocab-list . fuel-markup--vocab-list)))
154
155 (make-variable-buffer-local
156  (defvar fuel-markup--maybe-nl nil))
157
158 (defun fuel-markup--print (e)
159   (cond ((null e) (insert "f"))
160         ((stringp e) (fuel-markup--insert-string e))
161         ((and (listp e) (symbolp (car e))
162               (assoc (car e) fuel-markup--printers))
163          (funcall (cdr (assoc (car e) fuel-markup--printers)) e))
164         ((and (symbolp e)
165               (assoc e fuel-markup--printers))
166          (funcall (cdr (assoc e fuel-markup--printers)) e))
167         ((listp e) (mapc 'fuel-markup--print e))
168         ((symbolp e) (fuel-markup--print (list '$link e)))
169         (t (insert (format "\n%S\n" e)))))
170
171 (defun fuel-markup--print-str (e)
172   (with-temp-buffer
173     (fuel-markup--print e)
174     (buffer-string)))
175
176 (defun fuel-markup--maybe-nl ()
177   (setq fuel-markup--maybe-nl (point)))
178
179 (defun fuel-markup--insert-newline (&optional justification nosqueeze)
180   (fill-region (save-excursion (beginning-of-line) (point))
181                (point)
182                (or justification 'left)
183                nosqueeze)
184   (newline))
185
186 (defsubst fuel-markup--insert-nl-if-nb (&optional no-fill)
187   (unless (eq (save-excursion (beginning-of-line) (point)) (point))
188     (if no-fill (newline) (fuel-markup--insert-newline))))
189
190 (defsubst fuel-markup--put-face (txt face)
191   (put-text-property 0 (length txt) 'font-lock-face face txt)
192   txt)
193
194 (defun fuel-markup--insert-heading (txt &optional no-nl)
195   (fuel-markup--insert-nl-if-nb)
196   (delete-blank-lines)
197   (unless (bobp) (newline))
198   (fuel-markup--put-face txt 'fuel-font-lock-markup-heading)
199   (fuel-markup--insert-string txt)
200   (unless no-nl (newline)))
201
202 (defun fuel-markup--insert-string (str)
203   (when fuel-markup--maybe-nl
204     (newline 2)
205     (setq fuel-markup--maybe-nl nil))
206   (insert str))
207
208 (defun fuel-markup--article (e)
209   (setq fuel-markup--maybe-nl nil)
210   (insert (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-title))
211   (newline 2)
212   (fuel-markup--print (car (cddr e))))
213
214 (defun fuel-markup--heading (e)
215   (fuel-markup--insert-heading (cadr e)))
216
217 (defun fuel-markup--subheading (e)
218   (fuel-markup--insert-heading (cadr e)))
219
220 (defun fuel-markup--subsection (e)
221   (fuel-markup--insert-nl-if-nb)
222   (insert "  - ")
223   (fuel-markup--link (cons '$link (cdr e)))
224   (fuel-markup--maybe-nl))
225
226 (defun fuel-markup--vocab-subsection (e)
227   (fuel-markup--insert-nl-if-nb)
228   (insert "  - ")
229   (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
230   (fuel-markup--maybe-nl))
231
232 (defun fuel-markup--newline (e)
233   (fuel-markup--insert-newline)
234   (newline))
235
236 (defun fuel-markup--doc-path (e)
237   (fuel-markup--insert-heading "Related topics")
238   (insert "  ")
239   (dolist (art (cdr e))
240     (fuel-markup--insert-button (car art) (cadr art) 'article)
241     (insert ", "))
242   (delete-backward-char 2)
243   (fuel-markup--insert-newline 'left))
244
245 (defun fuel-markup--emphasis (e)
246   (when (stringp (cadr e))
247     (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-emphasis)
248     (insert (cadr e))))
249
250 (defun fuel-markup--strong (e)
251   (when (stringp (cadr e))
252     (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-strong)
253     (insert (cadr e))))
254
255 (defun fuel-markup--snippet (e)
256   (insert (mapconcat '(lambda (s)
257                         (if (stringp s)
258                             (fuel-font-lock--factor-str s)
259                           (fuel-markup--print-str s)))
260                      (cdr e)
261                      " ")))
262
263 (defun fuel-markup--code (e)
264   (fuel-markup--insert-nl-if-nb)
265   (newline)
266   (dolist (snip (cdr e))
267     (if (stringp snip)
268         (insert (fuel-font-lock--factor-str snip))
269       (fuel-markup--print snip))
270     (newline))
271   (newline))
272
273 (defun fuel-markup--command (e)
274   (fuel-markup--snippet (list '$snippet (nth 3 e))))
275
276 (defun fuel-markup--syntax (e)
277   (fuel-markup--insert-heading "Syntax")
278   (fuel-markup--print (cons '$code (cdr e)))
279   (newline))
280
281 (defun fuel-markup--example (e)
282   (fuel-markup--insert-newline)
283   (dolist (s (cdr e))
284     (fuel-markup--snippet (list '$snippet s))
285     (newline)))
286
287 (defun fuel-markup--markup-example (e)
288   (fuel-markup--insert-newline)
289   (fuel-markup--snippet (cons '$snippet (cdr e))))
290
291 (defun fuel-markup--link (e)
292   (let* ((link (or (nth 1 e) 'f))
293          (type (or (nth 3 e) (if (symbolp link) 'word 'article)))
294          (label (or (nth 2 e)
295                     (and (eq type 'article)
296                          (fuel-markup--article-title link))
297                     link)))
298     (fuel-markup--insert-button label link type)))
299
300 (defun fuel-markup--links (e)
301   (dolist (link (cdr e))
302     (fuel-markup--link (list '$link link))
303     (insert ", "))
304   (delete-backward-char 2))
305
306 (defun fuel-markup--index-quotation (q)
307   (cond ((null q) null)
308         ((listp q) (vconcat (mapcar 'fuel-markup--index-quotation q)))
309         (t q)))
310
311 (defun fuel-markup--index (e)
312   (let* ((q (fuel-markup--index-quotation (cadr e)))
313          (cmd `(:fuel* ((,q fuel-index)) "fuel"
314                        ("builtins" "help" "help.topics" "classes"
315                         "classes.builtin" "classes.tuple"
316                         "classes.singleton" "classes.union"
317                         "classes.intersection" "classes.predicate")))
318          (subs (fuel-eval--retort-result (fuel-eval--send/wait cmd 200))))
319     (when subs
320       (let ((start (point))
321             (sort-fold-case nil))
322         (fuel-markup--print subs)
323         (sort-lines nil start (point))))))
324
325 (defun fuel-markup--vocab-link (e)
326   (fuel-markup--insert-button (cadr e) (cadr e) 'vocab))
327
328 (defun fuel-markup--vocab-links (e)
329   (dolist (link (cdr e))
330     (insert " ")
331     (fuel-markup--vocab-link (list '$vocab-link link))
332     (insert " ")))
333
334 (defun fuel-markup--vocab-list (e)
335   (let ((rows (mapcar '(lambda (elem)
336                          (list (car elem)
337                                (list '$vocab-link (cadr elem))
338                                (caddr elem)))
339                       (cdr e))))
340     (fuel-markup--table (cons '$table rows))))
341
342 (defun fuel-markup--describe-vocab (e)
343   (fuel-markup--insert-nl-if-nb)
344   (let* ((cmd `(:fuel* ((,(cadr e) fuel-vocab-help)) "fuel" t))
345          (res (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
346     (when res (fuel-markup--print res))))
347
348 (defun fuel-markup--vocabulary (e)
349   (fuel-markup--insert-heading "Vocabulary: " t)
350   (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
351   (newline))
352
353 (defun fuel-markup--parse-classes ()
354   (let ((elems))
355     (while (looking-at ".+ classes$")
356       (let ((heading `($heading ,(match-string-no-properties 0)))
357             (rows))
358         (forward-line)
359         (when (looking-at "Class *.+$")
360           (push (split-string (match-string-no-properties 0) nil t) rows)
361           (forward-line))
362         (while (not (looking-at "$"))
363           (let* ((objs (split-string (thing-at-point 'line) nil t))
364                  (class (list '$link (car objs) (car objs) 'word))
365                  (super (and (cadr objs)
366                              (list (list '$link (cadr objs) (cadr objs) 'word))))
367                  (slots (when (cddr objs)
368                           (list (mapcar '(lambda (s) (list s " ")) (cddr objs))))))
369             (push `(,class ,@super ,@slots) rows))
370           (forward-line))
371         (push `(,heading ($table ,@(reverse rows))) elems))
372       (forward-line))
373     (reverse elems)))
374
375 (defun fuel-markup--parse-words ()
376   (let ((elems))
377     (while (looking-at ".+ words\\|Primitives$")
378       (let ((heading `($heading ,(match-string-no-properties 0)))
379             (rows))
380         (forward-line)
381         (when (looking-at "Word *\\(Stack effect\\|Syntax\\)$")
382           (push (list "Word" (match-string-no-properties 1)) rows)
383           (forward-line))
384         (while (looking-at "\\(.+?\\)\\( +\\(.+\\)\\)?$")
385           (let ((word `($link ,(match-string-no-properties 1)
386                               ,(match-string-no-properties 1)
387                               word))
388                 (se (and (match-string-no-properties 3)
389                          `(($snippet ,(match-string-no-properties 3))))))
390             (push `(,word ,@se) rows))
391           (forward-line))
392         (push `(,heading ($table ,@(reverse rows))) elems))
393       (forward-line))
394     (reverse elems)))
395
396 (defun fuel-markup--parse-words-desc (desc)
397   (with-temp-buffer
398     (insert desc)
399     (goto-char (point-min))
400     (when (re-search-forward "^Words$" nil t)
401       (forward-line 2)
402       (let ((elems '(($heading "Words"))))
403         (push (fuel-markup--parse-classes) elems)
404         (push (fuel-markup--parse-words) elems)
405         (reverse elems)))))
406
407 (defun fuel-markup--describe-words (e)
408   (when (cadr e)
409     (fuel-markup--print (fuel-markup--parse-words-desc (cadr e)))))
410
411 (defun fuel-markup--tag (e)
412   (fuel-markup--link (list '$link (cadr e) (cadr e) 'tag)))
413
414 (defun fuel-markup--tags (e)
415   (when (cdr e)
416     (fuel-markup--insert-heading "Tags: " t)
417     (dolist (tag (cdr e))
418       (fuel-markup--tag (list '$tag tag))
419       (insert ", "))
420     (delete-backward-char 2)
421     (fuel-markup--insert-newline)))
422
423 (defun fuel-markup--all-tags (e)
424   (let* ((cmd `(:fuel* (all-tags :get) "fuel" t))
425          (tags (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
426     (fuel-markup--list
427      (cons '$list (mapcar (lambda (tag) (list '$link tag tag 'tag)) tags)))))
428
429 (defun fuel-markup--author (e)
430   (fuel-markup--link (list '$link (cadr e) (cadr e) 'author)))
431
432 (defun fuel-markup--authors (e)
433   (when (cdr e)
434     (fuel-markup--insert-heading "Authors: " t)
435     (dolist (a (cdr e))
436       (fuel-markup--author (list '$author a))
437       (insert ", "))
438     (delete-backward-char 2)
439     (fuel-markup--insert-newline)))
440
441 (defun fuel-markup--all-authors (e)
442   (let* ((cmd `(:fuel* (all-authors :get) "fuel" t))
443          (authors (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
444     (fuel-markup--list
445      (cons '$list (mapcar (lambda (a) (list '$link a a 'author)) authors)))))
446
447 (defun fuel-markup--list (e)
448   (fuel-markup--insert-nl-if-nb)
449   (dolist (elt (cdr e))
450     (insert " - ")
451     (fuel-markup--print elt)
452     (fuel-markup--insert-newline)))
453
454 (defun fuel-markup--table (e)
455   (fuel-markup--insert-newline)
456   (delete-blank-lines)
457   (newline)
458   (fuel-table--insert
459    (mapcar '(lambda (row) (mapcar 'fuel-markup--print-str row)) (cdr e)))
460   (newline))
461
462 (defun fuel-markup--instance (e)
463   (insert " an instance of ")
464   (fuel-markup--print (cadr e)))
465
466 (defun fuel-markup--maybe (e)
467   (fuel-markup--instance (cons '$instance (cdr e)))
468   (insert " or f "))
469
470 (defun fuel-markup--values (e)
471   (fuel-markup--insert-heading "Inputs and outputs")
472   (dolist (val (cdr e))
473     (insert " " (car val) " - ")
474     (fuel-markup--print (cdr val))
475     (newline)))
476
477 (defun fuel-markup--predicate (e)
478   (fuel-markup--values '($values ("object" object) ("?" "a boolean")))
479   (let ((word (make-symbol (substring (format "%s" (cadr e)) 0 -1))))
480   (fuel-markup--description
481    `($description "Tests if the object is an instance of the "
482                   ($link ,word) " class."))))
483
484 (defun fuel-markup--side-effects (e)
485   (fuel-markup--insert-heading "Side effects")
486   (insert "Modifies ")
487   (fuel-markup--print (cdr e))
488   (fuel-markup--insert-newline))
489
490 (defun fuel-markup--definition (e)
491   (fuel-markup--insert-heading "Definition")
492   (fuel-markup--code (cons '$code (cdr e))))
493
494 (defun fuel-markup--methods (e)
495   (fuel-markup--insert-heading "Methods")
496   (fuel-markup--code (cons '$code (cdr e))))
497
498 (defun fuel-markup--value (e)
499   (fuel-markup--insert-heading "Variable value")
500   (insert "Current value in global namespace: ")
501   (fuel-markup--snippet (cons '$snippet (cdr e)))
502   (newline))
503
504 (defun fuel-markup--values-x/y (e)
505   (fuel-markup--values '($values ("x" "number") ("y" "number"))))
506
507 (defun fuel-markup--curious (e)
508   (fuel-markup--insert-heading "For the curious...")
509   (fuel-markup--print (cdr e)))
510
511 (defun fuel-markup--references (e)
512   (fuel-markup--insert-heading "References")
513   (dolist (ref (cdr e))
514     (if (listp ref)
515         (fuel-markup--print ref)
516       (fuel-markup--subsection (list '$subsection ref)))))
517
518 (defun fuel-markup--see-also (e)
519   (fuel-markup--insert-heading "See also")
520   (fuel-markup--links (cons '$links (cdr e))))
521
522 (defun fuel-markup--related (e)
523   (fuel-markup--insert-heading "See also")
524   (fuel-markup--links (cons '$links (cadr e))))
525
526 (defun fuel-markup--shuffle (e)
527   (insert "\nShuffle word. Re-arranges the stack "
528           "according to the stack effect pattern.")
529   (fuel-markup--insert-newline))
530
531 (defun fuel-markup--low-level-note (e)
532   (fuel-markup--print '($notes "Calling this word directly is not necessary "
533                                "in most cases. "
534                                "Higher-level words call it automatically.")))
535
536 (defun fuel-markup--parsing-note (e)
537   (fuel-markup--insert-nl-if-nb)
538   (insert "This word should only be called from parsing words.")
539   (fuel-markup--insert-newline))
540
541 (defun fuel-markup--io-error (e)
542   (fuel-markup--errors '($errors "Throws an error if the I/O operation fails.")))
543
544 (defun fuel-markup--prettyprinting-note (e)
545   (fuel-markup--print '($notes ("This word should only be called within the "
546                                 ($link with-pprint) " combinator."))))
547
548 (defun fuel-markup--elem-with-heading (elem heading)
549   (fuel-markup--insert-heading heading)
550   (fuel-markup--print (cdr elem))
551   (fuel-markup--insert-newline))
552
553 (defun fuel-markup--quotation (e)
554   (insert "a ")
555   (fuel-markup--link (list '$link 'quotation 'quotation 'word))
556   (insert " with stack effect ")
557   (fuel-markup--snippet (list '$snippet (nth 1 e))))
558
559 (defun fuel-markup--warning (e)
560   (fuel-markup--elem-with-heading e "Warning"))
561
562 (defun fuel-markup--description (e)
563   (fuel-markup--elem-with-heading e "Word description"))
564
565 (defun fuel-markup--class-description (e)
566   (fuel-markup--elem-with-heading e "Class description"))
567
568 (defun fuel-markup--error-description (e)
569   (fuel-markup--elem-with-heading e "Error description"))
570
571 (defun fuel-markup--var-description (e)
572   (fuel-markup--elem-with-heading e "Variable description"))
573
574 (defun fuel-markup--contract (e)
575   (fuel-markup--elem-with-heading e "Generic word contract"))
576
577 (defun fuel-markup--errors (e)
578   (fuel-markup--elem-with-heading e "Errors"))
579
580 (defun fuel-markup--examples (e)
581   (fuel-markup--elem-with-heading e "Examples"))
582
583 (defun fuel-markup--notes (e)
584   (fuel-markup--elem-with-heading e "Notes"))
585
586 (defun fuel-markup--see (e)
587   (let* ((word (nth 1 e))
588          (cmd (and word `(:fuel* ((:quote ,(format "%s" word)) see) "fuel")))
589          (ret (and cmd (fuel-eval--send/wait cmd)))
590          (res (and (not (fuel-eval--retort-error ret))
591                    (fuel-eval--retort-output ret))))
592     (if res
593         (fuel-markup--code (list '$code res))
594       (fuel-markup--snippet (list '$snippet " " word)))))
595
596 (defun fuel-markup--null (e))
597
598 (defun fuel-markup--synopsis (e)
599   (insert (format " %S " e)))
600
601 \f
602 (provide 'fuel-markup)
603 ;;; fuel-markup.el ends here