1 ;;; fuel-markup.el -- printing factor help markup
3 ;; Copyright (C) 2009 Jose Antonio Ortega Ruiz
4 ;; See http://factorcode.org/license.txt for BSD license.
6 ;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
7 ;; Keywords: languages, fuel, factor
8 ;; Start date: Thu Jan 01, 2009 21:43
12 ;; Utilities for printing Factor's help markup.
25 (defface fuel-font-lock-markup-title '((t (:inherit bold)))
26 "article titles in help buffers"
31 (defface fuel-font-lock-markup-heading '((t (:inherit bold)))
32 "headlines in help buffers"
37 (defface fuel-font-lock-markup-link '((t (:inherit link)))
38 "links to topics in help buffers"
43 (defface fuel-font-lock-markup-emphasis '((t (:inherit italic)))
44 "emphasized words in help buffers"
49 (defface fuel-font-lock-markup-strong '((t (:inherit link)))
50 "bold words in help buffers"
58 (defvar-local fuel-markup--follow-link-function 'fuel-markup--echo-link)
60 (define-button-type 'fuel-markup--button
61 'action 'fuel-markup--follow-link
62 'face 'fuel-font-lock-markup-link
65 (defun fuel-markup--follow-link (button)
66 (when fuel-markup--follow-link-function
67 (funcall fuel-markup--follow-link-function
68 (button-get button 'markup-link)
69 (button-get button 'markup-label)
70 (button-get button 'markup-link-type))))
72 (defun fuel-markup--echo-link (link label type)
73 (message "Link %s pointing to %s named %s" label type link))
75 (defun fuel-markup--insert-button (label link type)
76 (let ((label (format "%s" label))
77 (link (if (listp link) link (format "%s" link))))
78 (insert-text-button label
79 :type 'fuel-markup--button
82 'markup-link-type type
83 'help-echo (format "%s (%s)" label type))))
85 (defun fuel-markup--article-title (name)
86 (let ((name (if (listp name) (cons :seq name) name)))
87 (fuel-eval--retort-result
88 (fuel-eval--send/wait `(:fuel* ((,name fuel-get-article-title)) "fuel")))))
90 (defun fuel-markup--link-at-point ()
91 (let ((button (condition-case nil (forward-button 0) (error nil))))
93 (list (button-get button 'markup-link)
94 (button-get button 'markup-label)
95 (button-get button 'markup-link-type)))))
100 (defconst fuel-markup--printers
101 '(($all-tags . fuel-markup--all-tags)
102 ($all-authors . fuel-markup--all-authors)
103 ($author . fuel-markup--author)
104 ($authors . fuel-markup--authors)
105 ($class-description . fuel-markup--class-description)
106 ($code . fuel-markup--code)
107 ($command . fuel-markup--command)
108 ($command-map . fuel-markup--null)
109 ($contract . fuel-markup--contract)
110 ($curious . fuel-markup--curious)
111 ($definition . fuel-markup--definition)
112 ($describe-vocab . fuel-markup--describe-vocab)
113 ($description . fuel-markup--description)
114 ($doc-path . fuel-markup--doc-path)
115 ($emphasis . fuel-markup--emphasis)
116 ($error-description . fuel-markup--error-description)
117 ($errors . fuel-markup--errors)
118 ($example . fuel-markup--example)
119 ($examples . fuel-markup--examples)
120 ($heading . fuel-markup--heading)
121 ($index . fuel-markup--index)
122 ($instance . fuel-markup--instance)
123 ($io-error . fuel-markup--io-error)
124 ($link . fuel-markup--link)
125 ($links . fuel-markup--links)
126 ($list . fuel-markup--list)
127 ($low-level-note . fuel-markup--low-level-note)
128 ($markup-example . fuel-markup--markup-example)
129 ($maybe . fuel-markup--maybe)
130 ($methods . fuel-markup--methods)
131 ($nl . fuel-markup--newline)
132 ($notes . fuel-markup--notes)
133 ($operation . fuel-markup--link)
134 ($or . fuel-markup--or)
135 ($parsing-note . fuel-markup--parsing-note)
136 ($predicate . fuel-markup--predicate)
137 ($prettyprinting-note . fuel-markup--prettyprinting-note)
138 ($quotation . fuel-markup--quotation)
139 ($references . fuel-markup--references)
140 ($related . fuel-markup--related)
141 ($see . fuel-markup--see)
142 ($see-also . fuel-markup--see-also)
143 ($shuffle . fuel-markup--shuffle)
144 ($side-effects . fuel-markup--side-effects)
145 ($slot . fuel-markup--snippet)
146 ($snippet . fuel-markup--snippet)
147 ($strong . fuel-markup--strong)
148 ($subheading . fuel-markup--subheading)
149 ($subsection . fuel-markup--subsection)
150 ($subsections . fuel-markup--subsections)
151 ($synopsis . fuel-markup--synopsis)
152 ($syntax . fuel-markup--syntax)
153 ($table . fuel-markup--table)
154 ($tag . fuel-markup--tag)
155 ($tags . fuel-markup--tags)
156 ($unchecked-example . fuel-markup--example)
157 ($value . fuel-markup--value)
158 ($values . fuel-markup--values)
159 ($values-x/y . fuel-markup--values-x/y)
160 ($var-description . fuel-markup--var-description)
161 ($vocab-link . fuel-markup--vocab-link)
162 ($vocab-links . fuel-markup--vocab-links)
163 ($vocab-subsection . fuel-markup--vocab-subsection)
164 ($vocabulary . fuel-markup--vocabulary)
165 ($warning . fuel-markup--warning)
166 (article . fuel-markup--article)
167 (describe-words . fuel-markup--describe-words)
168 (vocab-list . fuel-markup--vocab-list)))
170 (defvar-local fuel-markup--maybe-nl nil)
172 (defun fuel-markup--print (e)
173 (cond ((null e) (insert "f"))
174 ((stringp e) (fuel-markup--insert-string e))
175 ((and (listp e) (symbolp (car e))
176 (assoc (car e) fuel-markup--printers))
177 (funcall (cdr (assoc (car e) fuel-markup--printers)) e))
179 (assoc e fuel-markup--printers))
180 (funcall (cdr (assoc e fuel-markup--printers)) e))
181 ((listp e) (mapc 'fuel-markup--print e))
182 ((symbolp e) (fuel-markup--print (list '$link e)))
183 (t (insert (format "\n%S\n" e)))))
185 (defun fuel-markup--print-str (e)
187 (fuel-markup--print e)
190 (defun fuel-markup--maybe-nl ()
191 (setq fuel-markup--maybe-nl (point)))
193 (defun fuel-markup--insert-newline (&optional justification nosqueeze)
194 (fill-region (save-excursion (beginning-of-line) (point))
196 (or justification 'left)
200 (defsubst fuel-markup--insert-nl-if-nb (&optional no-fill)
201 (unless (eq (save-excursion (beginning-of-line) (point)) (point))
202 (if no-fill (newline) (fuel-markup--insert-newline))))
204 (defsubst fuel-markup--put-face (txt face)
205 (put-text-property 0 (length txt) 'font-lock-face face txt)
208 (defun fuel-markup--insert-heading (txt &optional no-nl)
209 (fuel-markup--insert-nl-if-nb)
211 (unless (bobp) (newline))
212 (fuel-markup--put-face txt 'fuel-font-lock-markup-heading)
213 (fuel-markup--insert-string txt)
214 (unless no-nl (newline)))
216 (defun fuel-markup--insert-string (str)
217 (when fuel-markup--maybe-nl
219 (setq fuel-markup--maybe-nl nil))
222 (defun fuel-markup--article (e)
223 (setq fuel-markup--maybe-nl nil)
224 (insert (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-title))
226 (fuel-markup--print (car (cddr e))))
228 (defun fuel-markup--heading (e)
229 (fuel-markup--insert-heading (cadr e)))
231 (defun fuel-markup--subheading (e)
232 (fuel-markup--insert-heading (cadr e)))
234 (defun fuel-markup--subsection (e)
235 (fuel-markup--insert-nl-if-nb)
237 (fuel-markup--link (cons '$link (cdr e)))
238 (fuel-markup--maybe-nl))
240 (defun fuel-markup--subsections (e)
241 (dolist (link (cdr e))
242 (fuel-markup--insert-nl-if-nb)
244 (fuel-markup--link (list '$link link))
245 (fuel-markup--maybe-nl)))
247 (defun fuel-markup--vocab-subsection (e)
248 (fuel-markup--insert-nl-if-nb)
250 (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
251 (fuel-markup--maybe-nl))
253 (defun fuel-markup--newline (e)
254 (fuel-markup--insert-newline)
257 (defun fuel-markup--doc-path (e)
258 (fuel-markup--insert-heading "Related topics")
260 (dolist (art (cdr e))
261 (fuel-markup--insert-button (car art) (cadr art) 'article)
264 (fuel-markup--insert-newline 'left))
266 (defun fuel-markup--emphasis (e)
267 (when (stringp (cadr e))
268 (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-emphasis)
271 (defun fuel-markup--strong (e)
272 (when (stringp (cadr e))
273 (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-strong)
276 (defun fuel-markup--snippet (e)
277 (insert (mapconcat #'(lambda (s)
279 (factor-font-lock-string s)
280 (fuel-markup--print-str s)))
284 (defun fuel-markup--code (e)
285 (fuel-markup--insert-nl-if-nb)
287 (dolist (snip (cdr e))
289 (insert (factor-font-lock-string snip))
290 (fuel-markup--print snip))
294 (defun fuel-markup--command (e)
295 (fuel-markup--snippet (list '$snippet (nth 3 e))))
297 (defun fuel-markup--syntax (e)
298 (fuel-markup--insert-heading "Syntax")
299 (fuel-markup--print (cons '$code (cdr e)))
302 (defun fuel-markup--example (e)
303 (fuel-markup--insert-newline)
305 (fuel-markup--snippet (list '$snippet s))
309 (defun fuel-markup--markup-example (e)
310 (fuel-markup--insert-newline)
311 (fuel-markup--snippet (cons '$snippet (cdr e))))
313 (defun fuel-markup--link (e)
314 (let* ((link (or (nth 1 e) 'f))
315 (type (or (nth 3 e) (if (symbolp link) 'word 'article)))
317 (and (eq type 'article)
318 (fuel-markup--article-title link))
320 (fuel-markup--insert-button label link type)))
322 (defun fuel-markup--links (e)
323 (dolist (link (cdr e))
324 (fuel-markup--link (list '$link link))
328 (defun fuel-markup--index-quotation (q)
330 ((listp q) (vconcat (mapcar 'fuel-markup--index-quotation q)))
333 (defun fuel-markup--index (e)
334 (let* ((q (fuel-markup--index-quotation (cadr e)))
335 (cmd `(:fuel* ((,q fuel-index)) "fuel"
336 ("builtins" "help" "help.topics" "classes"
337 "classes.builtin" "classes.tuple"
338 "classes.singleton" "classes.union"
339 "classes.intersection" "classes.predicate")))
340 (subs (fuel-eval--retort-result (fuel-eval--send/wait cmd 200))))
342 (let ((start (point))
343 (sort-fold-case nil))
344 (fuel-markup--print subs)
345 (sort-lines nil start (point))))))
347 (defun fuel-markup--vocab-link (e)
348 (fuel-markup--insert-button (cadr e) (or (car (cddr e)) (cadr e)) 'vocab))
350 (defun fuel-markup--vocab-links (e)
351 (dolist (link (cdr e))
353 (fuel-markup--vocab-link (list '$vocab-link link))
356 (defun fuel-markup--vocab-list (e)
357 (let ((rows (mapcar #'(lambda (elem)
358 (list (list '$vocab-link (car elem))
361 (fuel-markup--table (cons '$table rows))))
363 (defun fuel-markup--describe-vocab (e)
364 (fuel-markup--insert-nl-if-nb)
365 (let* ((cmd `(:fuel* ((,(cadr e) fuel-vocab-help)) "fuel" t))
366 (res (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
367 (when res (fuel-markup--print res))))
369 (defun fuel-markup--vocabulary (e)
370 (fuel-markup--insert-heading "Vocabulary: " t)
371 (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
374 (defun fuel-markup--parse-classes ()
376 (while (looking-at ".+ classes$")
377 (let ((heading `($heading ,(match-string-no-properties 0)))
380 (when (looking-at "Class *.+$")
381 (push (split-string (match-string-no-properties 0) nil t) rows)
383 (while (not (looking-at "$"))
384 (let* ((objs (split-string (thing-at-point 'line) nil t))
385 (class (list '$link (car objs) (car objs) 'word))
386 (super (and (cadr objs)
387 (list (list '$link (cadr objs) (cadr objs) 'word))))
388 (slots (when (cddr objs)
389 (list (mapcar #'(lambda (s) (list s " ")) (cddr objs))))))
390 (push `(,class ,@super ,@slots) rows))
392 (push `(,heading ($table ,@(reverse rows))) elems))
396 (defun fuel-markup--parse-words ()
398 (while (looking-at ".+ words\\|Primitives$")
399 (let ((heading `($heading ,(match-string-no-properties 0)))
402 (when (looking-at "Word *\\(Stack effect\\|Syntax\\)$")
403 (push (list "Word" (match-string-no-properties 1)) rows)
405 (while (looking-at " ?\\(.+?\\)\\( +\\(.+\\)\\)?$")
406 (let ((word `($link ,(match-string-no-properties 1)
407 ,(match-string-no-properties 1)
409 (se (and (match-string-no-properties 3)
410 `(($snippet ,(match-string-no-properties 3))))))
411 (push `(,word ,@se) rows))
413 (push `(,heading ($table ,@(reverse rows))) elems))
417 (defun fuel-markup--parse-words-desc (desc)
420 (goto-char (point-min))
421 (when (re-search-forward "^Words$" nil t)
423 (let ((elems '(($heading "Words"))))
424 (push (fuel-markup--parse-classes) elems)
425 (push (fuel-markup--parse-words) elems)
426 (reverse (remove nil elems))))))
428 (defun fuel-markup--describe-words (e)
430 (fuel-markup--print (fuel-markup--parse-words-desc (cadr e)))))
432 (defun fuel-markup--tag (e)
433 (fuel-markup--link (list '$link (cadr e) (cadr e) 'tag)))
435 (defun fuel-markup--tags (e)
437 (fuel-markup--insert-heading "Tags: " t)
438 (dolist (tag (cdr e))
439 (fuel-markup--tag (list '$tag tag))
442 (fuel-markup--insert-newline)))
444 (defun fuel-markup--all-tags (e)
445 (let* ((cmd `(:fuel* (all-tags :get) "fuel" t))
446 (tags (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
448 (cons '$list (mapcar (lambda (tag) (list '$link tag tag 'tag)) tags)))))
450 (defun fuel-markup--author (e)
451 (fuel-markup--link (list '$link (cadr e) (cadr e) 'author)))
453 (defun fuel-markup--authors (e)
455 (fuel-markup--insert-heading "Authors: " t)
457 (fuel-markup--author (list '$author a))
460 (fuel-markup--insert-newline)))
462 (defun fuel-markup--all-authors (e)
463 (let* ((cmd `(:fuel* (all-authors :get) "fuel" t))
464 (authors (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
466 (cons '$list (mapcar (lambda (a) (list '$link a a 'author)) authors)))))
468 (defun fuel-markup--list (e)
469 (fuel-markup--insert-nl-if-nb)
470 (dolist (elt (cdr e))
472 (fuel-markup--print elt)
473 (fuel-markup--insert-newline)))
475 (defun fuel-markup--table (e)
476 (fuel-markup--insert-newline)
480 (mapcar #'(lambda (row) (mapcar 'fuel-markup--print-str row)) (cdr e)))
483 (defun fuel-markup--instance (e)
484 (insert " an instance of ")
485 (fuel-markup--print (cadr e)))
487 (defun fuel-markup--maybe (e)
488 (fuel-markup--instance (cons '$instance (cdr e)))
491 (defun fuel-markup--or (e)
492 (let ((fst (car (cdr e)))
493 (mid (butlast (cddr e)))
494 (lst (car (last (cdr e)))))
495 (insert (format "%s" fst))
496 (dolist (m mid) (insert (format ", %s" m)))
497 (insert (format " or %s" lst))))
499 (defun fuel-markup--values (e)
500 (fuel-markup--insert-heading "Inputs and outputs")
501 (dolist (val (cdr e))
502 (insert " " (car val) " - ")
503 (fuel-markup--print (cdr val))
506 (defun fuel-markup--predicate (e)
507 (fuel-markup--values '($values ("object" object) ("?" "a boolean")))
508 (let ((word (make-symbol (substring (format "%s" (cadr e)) 0 -1))))
509 (fuel-markup--description
510 `($description "Tests if the object is an instance of the "
511 ($link ,word) " class."))))
513 (defun fuel-markup--side-effects (e)
514 (fuel-markup--insert-heading "Side effects")
516 (fuel-markup--print (cdr e))
517 (fuel-markup--insert-newline))
519 (defun fuel-markup--definition (e)
520 (fuel-markup--insert-heading "Definition")
521 (fuel-markup--code (cons '$code (cdr e))))
523 (defun fuel-markup--methods (e)
524 (fuel-markup--insert-heading "Methods")
525 (fuel-markup--code (cons '$code (cdr e))))
527 (defun fuel-markup--value (e)
528 (fuel-markup--insert-heading "Variable value")
529 (insert "Current value in global namespace: ")
530 (fuel-markup--snippet (cons '$snippet (cdr e)))
533 (defun fuel-markup--values-x/y (e)
534 (fuel-markup--values '($values ("x" "number") ("y" "number"))))
536 (defun fuel-markup--curious (e)
537 (fuel-markup--insert-heading "For the curious...")
538 (fuel-markup--print (cdr e)))
540 (defun fuel-markup--references (e)
541 (fuel-markup--insert-heading "References")
542 (dolist (ref (cdr e))
544 (fuel-markup--print ref)
545 (fuel-markup--subsection (list '$subsection ref)))))
547 (defun fuel-markup--see-also (e)
548 (fuel-markup--insert-heading "See also")
549 (fuel-markup--links (cons '$links (cdr e))))
551 (defun fuel-markup--related (e)
552 (fuel-markup--insert-heading "See also")
553 (fuel-markup--links (cons '$links (cadr e))))
555 (defun fuel-markup--shuffle (e)
556 (insert "\nShuffle word. Re-arranges the stack "
557 "according to the stack effect pattern.")
558 (fuel-markup--insert-newline))
560 (defun fuel-markup--low-level-note (e)
561 (fuel-markup--print '($notes "Calling this word directly is not necessary "
563 "Higher-level words call it automatically.")))
565 (defun fuel-markup--parsing-note (e)
566 (fuel-markup--insert-nl-if-nb)
567 (insert "This word should only be called from parsing words.")
568 (fuel-markup--insert-newline))
570 (defun fuel-markup--io-error (e)
571 (fuel-markup--errors '($errors "Throws an error if the I/O operation fails.")))
573 (defun fuel-markup--prettyprinting-note (e)
574 (fuel-markup--print '($notes ("This word should only be called within the "
575 ($link with-pprint) " combinator."))))
577 (defun fuel-markup--elem-with-heading (elem heading)
578 (fuel-markup--insert-heading heading)
579 (fuel-markup--print (cdr elem))
580 (fuel-markup--insert-newline))
582 (defun fuel-markup--stack-effect (e)
583 (let* ((in (mapconcat 'identity (nth 1 e) " "))
584 (out (mapconcat 'identity (nth 2 e) " "))
585 (str (format "( %s -- %s )" in out)))
586 (fuel-markup--snippet (list '$snippet str))))
588 (defun fuel-markup--quotation (e)
590 (fuel-markup--link (list '$link 'quotation 'quotation 'word))
591 (insert " with stack effect ")
592 (fuel-markup--stack-effect (nth 1 e)))
594 (defun fuel-markup--warning (e)
595 (fuel-markup--elem-with-heading e "Warning"))
597 (defun fuel-markup--description (e)
598 (fuel-markup--elem-with-heading e "Word description"))
600 (defun fuel-markup--class-description (e)
601 (fuel-markup--elem-with-heading e "Class description"))
603 (defun fuel-markup--error-description (e)
604 (fuel-markup--elem-with-heading e "Error description"))
606 (defun fuel-markup--var-description (e)
607 (fuel-markup--elem-with-heading e "Variable description"))
609 (defun fuel-markup--contract (e)
610 (fuel-markup--elem-with-heading e "Generic word contract"))
612 (defun fuel-markup--errors (e)
613 (fuel-markup--elem-with-heading e "Errors"))
615 (defun fuel-markup--examples (e)
616 (fuel-markup--elem-with-heading e "Examples"))
618 (defun fuel-markup--notes (e)
619 (fuel-markup--elem-with-heading e "Notes"))
621 (defun fuel-markup--word-info (e s)
622 (let* ((word (nth 1 e))
623 (cmd (and word `(:fuel* ((:quote ,(format "%s" word)) ,s) "fuel")))
624 (ret (and cmd (fuel-eval--send/wait cmd)))
625 (res (and (not (fuel-eval--retort-error ret))
626 (fuel-eval--retort-output ret))))
628 (fuel-markup--code (list '$code res))
629 (fuel-markup--snippet (list '$snippet " " word)))))
631 (defun fuel-markup--see (e)
632 (fuel-markup--word-info e 'see))
634 (defun fuel-markup--synopsis (e)
635 (fuel-markup--word-info e 'synopsis))
637 (defun fuel-markup--null (e))
640 (provide 'fuel-markup)
642 ;;; fuel-markup.el ends here