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)))))
97 (defun fuel-markup--nav-crumbs (e)
98 (fuel-markup--links e " > ")
103 (defconst fuel-markup--printers
104 '(($all-tags . fuel-markup--all-tags)
105 ($all-authors . fuel-markup--all-authors)
106 ($author . fuel-markup--author)
107 ($authors . fuel-markup--authors)
108 ($class-description . fuel-markup--class-description)
109 ($code . fuel-markup--code)
110 ($command . fuel-markup--command)
111 ($command-map . fuel-markup--null)
112 ($complex-shuffle . fuel-markup--complex-shuffle)
113 ($contract . fuel-markup--contract)
114 ($curious . fuel-markup--curious)
115 ($definition . fuel-markup--definition)
116 ($description . fuel-markup--description)
117 ($doc-path . fuel-markup--doc-path)
118 ($emphasis . fuel-markup--emphasis)
119 ($error-description . fuel-markup--error-description)
120 ($errors . fuel-markup--errors)
121 ($example . fuel-markup--example)
122 ($examples . fuel-markup--examples)
123 ($fuel-nav-crumbs . fuel-markup--nav-crumbs)
124 ($heading . fuel-markup--heading)
125 ($index . fuel-markup--index)
126 ($instance . fuel-markup--instance)
127 ($io-error . fuel-markup--io-error)
128 ($link . fuel-markup--link)
129 ($links . (lambda (e) (fuel-markup--links e ", ")))
130 ($list . fuel-markup--list)
131 ($low-level-note . fuel-markup--low-level-note)
132 ($markup-example . fuel-markup--markup-example)
133 ($maybe . fuel-markup--maybe)
134 ($sequence . fuel-markup--sequence)
135 ($methods . fuel-markup--methods)
136 ($next-link . (lambda (e) (fuel-markup--prefixed-link "Next:" e)))
137 ($nl . fuel-markup--newline)
138 ($notes . fuel-markup--notes)
139 ($operation . fuel-markup--link)
140 ($or . fuel-markup--or)
141 ($parsing-note . fuel-markup--parsing-note)
142 ($predicate . fuel-markup--predicate)
143 ($prettyprinting-note . fuel-markup--prettyprinting-note)
144 ($prev-link . (lambda (e) (fuel-markup--prefixed-link "Prev:" e)))
145 ($quotation . fuel-markup--quotation)
146 ($references . fuel-markup--references)
147 ($related . fuel-markup--related)
148 ($see . fuel-markup--see)
149 ($see-also . fuel-markup--see-also)
150 ($shuffle . fuel-markup--shuffle)
151 ($side-effects . fuel-markup--side-effects)
152 ($slot . fuel-markup--snippet)
153 ($snippet . fuel-markup--snippet)
154 ($strong . fuel-markup--strong)
155 ($subheading . fuel-markup--subheading)
156 ($subsection . fuel-markup--subsection)
157 ($subsections . fuel-markup--subsections)
158 ($synopsis . fuel-markup--synopsis)
159 ($syntax . fuel-markup--syntax)
160 ($table . fuel-markup--table)
161 ($tag . fuel-markup--tag)
162 ($tags . fuel-markup--tags)
163 ($unchecked-example . fuel-markup--example)
164 ($url . fuel-markup--url)
165 ($value . fuel-markup--value)
166 ($values . fuel-markup--values)
167 ($values-x/y . fuel-markup--values-x/y)
168 ($var-description . fuel-markup--var-description)
169 ($vocab . fuel-markup--vocab)
170 ($vocab-link . fuel-markup--vocab-link)
171 ($vocab-links . fuel-markup--vocab-links)
172 ($vocab-subsection . fuel-markup--vocab-subsection)
173 ($warning . fuel-markup--warning)
174 (article . fuel-markup--article)
175 (describe-words . fuel-markup--describe-words)
176 (vocab-list . fuel-markup--vocab-list)))
178 (defvar-local fuel-markup--maybe-nl nil)
180 (defun fuel-markup--print (e)
181 (cond ((null e) (insert "f"))
182 ((stringp e) (fuel-markup--insert-string e))
183 ((and (listp e) (symbolp (car e))
184 (assoc (car e) fuel-markup--printers))
185 (funcall (cdr (assoc (car e) fuel-markup--printers)) e))
187 (assoc e fuel-markup--printers))
188 (funcall (cdr (assoc e fuel-markup--printers)) e))
189 ((listp e) (mapc 'fuel-markup--print e))
190 ((symbolp e) (fuel-markup--print (list '$link e)))
191 (t (insert (format "\n%S\n" e)))))
193 (defun fuel-markup--print-str (e)
195 (fuel-markup--print e)
198 (defun fuel-markup--maybe-nl ()
199 (setq fuel-markup--maybe-nl (point)))
201 (defun fuel-markup--insert-newline (&optional justification nosqueeze)
202 (fill-region (save-excursion (beginning-of-line) (point))
204 (or justification 'left)
208 (defsubst fuel-markup--insert-nl-if-nb (&optional no-fill)
209 (unless (eq (save-excursion (beginning-of-line) (point)) (point))
210 (if no-fill (newline) (fuel-markup--insert-newline))))
212 (defsubst fuel-markup--put-face (txt face)
213 (put-text-property 0 (length txt) 'font-lock-face face txt)
216 (defun fuel-markup--insert-heading (txt &optional no-nl)
217 (fuel-markup--insert-nl-if-nb)
219 (unless (bobp) (newline))
220 (fuel-markup--put-face txt 'fuel-font-lock-markup-heading)
221 (fuel-markup--insert-string txt)
222 (unless no-nl (newline)))
224 (defun fuel-markup--insert-string (str)
225 (when fuel-markup--maybe-nl
227 (setq fuel-markup--maybe-nl nil))
230 (defun fuel-markup--article (e)
231 (setq fuel-markup--maybe-nl nil)
232 (insert (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-title))
234 (fuel-markup--print (car (cddr e))))
236 (defun fuel-markup--heading (e)
237 (fuel-markup--insert-heading (cadr e)))
239 (defun fuel-markup--subheading (e)
240 (fuel-markup--insert-heading (cadr e)))
242 (defun fuel-markup--subsection (e)
243 (fuel-markup--insert-nl-if-nb)
245 (fuel-markup--link (cons '$link (cdr e)))
246 (fuel-markup--maybe-nl))
248 (defun fuel-markup--subsections (e)
249 (dolist (link (cdr e))
250 (fuel-markup--insert-nl-if-nb)
252 (fuel-markup--link (list '$link link))
253 (fuel-markup--maybe-nl)))
255 (defun fuel-markup--vocab-subsection (e)
256 (fuel-markup--insert-nl-if-nb)
258 (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
259 (fuel-markup--maybe-nl))
261 (defun fuel-markup--newline (e)
262 (fuel-markup--insert-newline)
265 (defun fuel-markup--doc-path (e)
266 (fuel-markup--insert-heading "Related topics")
268 (dolist (art (cdr e))
269 (fuel-markup--insert-button (car art) (cadr art) 'article)
272 (fuel-markup--insert-newline 'left))
274 (defun fuel-markup--emphasis (e)
275 (when (stringp (cadr e))
276 (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-emphasis)
279 (defun fuel-markup--strong (e)
280 (when (stringp (cadr e))
281 (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-strong)
284 (define-button-type 'fuel-markup--url
285 'action 'fuel-markup--follow-url
286 'face 'fuel-font-lock-markup-link
289 (defun fuel-markup--follow-url (button)
290 (browse-url (button-get button 'markup-link)))
292 (defun fuel-markup--url (e)
293 (let ((url (cadr e)))
294 (insert-text-button url
295 :type 'fuel-markup--url
298 (defun fuel-markup--snippet (e)
299 (insert (mapconcat #'(lambda (s)
301 (factor-font-lock-string s)
302 (fuel-markup--print-str s)))
306 (defun fuel-markup--code (e)
307 (fuel-markup--insert-nl-if-nb)
309 (dolist (snip (cdr e))
311 (insert (factor-font-lock-string snip))
312 (fuel-markup--print snip))
316 (defun fuel-markup--command (e)
317 (fuel-markup--snippet (list '$snippet (nth 3 e))))
319 (defun fuel-markup--syntax (e)
320 (fuel-markup--insert-heading "Syntax")
321 (fuel-markup--print (cons '$code (cdr e)))
324 (defun fuel-markup--example (e)
325 (fuel-markup--insert-newline)
327 (fuel-markup--snippet (list '$snippet s))
331 (defun fuel-markup--markup-example (e)
332 (fuel-markup--insert-newline)
333 (fuel-markup--snippet (cons '$snippet (cdr e))))
335 (defun fuel-markup--link (e)
336 (let* ((link (or (nth 1 e) 'f))
337 (type (or (nth 3 e) (if (symbolp link) 'word 'article)))
339 (and (eq type 'article)
340 (fuel-markup--article-title link))
342 (fuel-markup--insert-button label link type)))
344 (defun fuel-markup--links (e sep)
345 "Inserts a sequence of links. Used for rendering see also lists
346 and breadcrumb navigation. The items in e can either be strings
348 (let ((links (cdr e)))
351 (message (format "link %s" link))
357 (delete-char (- (length sep))))))
359 (defun fuel-markup--index-quotation (q)
361 ((listp q) (vconcat (mapcar 'fuel-markup--index-quotation q)))
364 (defun fuel-markup--index (e)
365 (let* ((q (fuel-markup--index-quotation (cadr e)))
366 (cmd `(:fuel* ((,q fuel-index)) "fuel"
367 ("assocs" "builtins" "classes" "classes.builtin"
368 "classes.intersection" "classes.predicate"
369 "classes.singleton" "classes.tuple" "classes.union"
370 "help" "help.topics" "namespaces" "sequences"
372 (subs (fuel-eval--retort-result (fuel-eval--send/wait cmd 200))))
374 (let ((start (point))
375 (sort-fold-case nil))
376 (fuel-markup--print subs)
377 (sort-lines nil start (point))))))
379 (defun fuel-markup--vocab-link (e)
380 (fuel-markup--insert-button (cadr e) (or (car (cddr e)) (cadr e)) 'vocab))
382 (defun fuel-markup--vocab-links (e)
383 (dolist (link (cdr e))
385 (fuel-markup--vocab-link (list '$vocab-link link))
388 (defun fuel-markup--vocab-list (e)
389 (let ((rows (mapcar #'(lambda (elem)
390 (list (list '$vocab-link (car elem))
393 (fuel-markup--table (cons '$table rows))))
395 (defun fuel-markup--vocab (e)
396 (fuel-markup--insert-nl-if-nb)
397 (let* ((cmd `(:fuel* ((,(cadr e) fuel-vocab-help)) "fuel" t))
398 (res (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
399 (when res (fuel-markup--print res))))
401 (defun fuel-markup--parse-classes ()
403 (while (looking-at ".+ classes$")
404 (let ((heading `($heading ,(match-string-no-properties 0)))
407 (when (looking-at "Class *.+$")
408 (push (split-string (match-string-no-properties 0) nil t) rows)
410 (while (not (looking-at "$"))
411 (let* ((objs (split-string (thing-at-point 'line) nil t))
412 (class (list '$link (car objs) (car objs) 'word))
413 (super (and (cadr objs)
414 (list (list '$link (cadr objs) (cadr objs) 'word))))
415 (slots (when (cddr objs)
416 (list (mapcar #'(lambda (s) (list s " ")) (cddr objs))))))
417 (push `(,class ,@super ,@slots) rows))
419 (push `(,heading ($table ,@(reverse rows))) elems))
423 (defun fuel-markup--parse-words ()
425 (while (looking-at ".+ words\\|Primitives$")
426 (let ((heading `($heading ,(match-string-no-properties 0)))
429 (when (looking-at "Word *\\(Stack effect\\|Syntax\\)$")
430 (push (list "Word" (match-string-no-properties 1)) rows)
432 (while (looking-at " ?\\(.+?\\)\\( +\\(.+\\)\\)?$")
433 (let ((word `($link ,(match-string-no-properties 1)
434 ,(match-string-no-properties 1)
436 (se (and (match-string-no-properties 3)
437 `(($snippet ,(match-string-no-properties 3))))))
438 (push `(,word ,@se) rows))
440 (push `(,heading ($table ,@(reverse rows))) elems))
444 (defun fuel-markup--parse-words-desc (desc)
445 "This function parses the text descripiton of the vocab that
446 the 'words.' word emits."
449 (goto-char (point-min))
450 (when (re-search-forward "^Words$" nil t)
452 (let ((elems '(($heading "Words"))))
453 (push (fuel-markup--parse-classes) elems)
454 (push (fuel-markup--parse-words) elems)
455 (reverse (remove nil elems))))))
457 (defun fuel-markup--describe-words (e)
459 (fuel-markup--print (fuel-markup--parse-words-desc (cadr e)))))
461 (defun fuel-markup--tag (e)
462 (fuel-markup--link (list '$link (cadr e) (cadr e) 'tag)))
464 (defun fuel-markup--tags (e)
466 (fuel-markup--insert-heading "Tags: " t)
467 (dolist (tag (cdr e))
468 (fuel-markup--tag (list '$tag tag))
471 (fuel-markup--insert-newline)))
473 (defun fuel-markup--all-tags (e)
474 (let* ((cmd `(:fuel* (all-tags :get) "fuel" t))
475 (tags (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
477 (cons '$list (mapcar (lambda (tag) (list '$link tag tag 'tag)) tags)))))
479 (defun fuel-markup--author (e)
480 (fuel-markup--link (list '$link (cadr e) (cadr e) 'author)))
482 (defun fuel-markup--authors (e)
484 (fuel-markup--insert-heading "Authors: " t)
486 (fuel-markup--author (list '$author a))
489 (fuel-markup--insert-newline)))
491 (defun fuel-markup--all-authors (e)
492 (let* ((cmd `(:fuel* (all-authors :get) "fuel" t))
493 (authors (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
495 (cons '$list (mapcar (lambda (a) (list '$link a a 'author)) authors)))))
497 (defun fuel-markup--complex-shuffle (e)
498 (fuel-markup--description
499 `($description "Shuffle word. Rearranges the top of the datastack as "
500 "indicated in the stack effect pattern."))
501 (fuel-markup--elem-with-heading
502 `(nil "The data flow represented by this shuffle word can be more clearly "
503 "expressed using " ($vocab-link "Lexical variables" "locals") ".")
504 "This word is deprecated"))
506 (defun fuel-markup--list (e)
507 (fuel-markup--insert-nl-if-nb)
508 (dolist (elt (cdr e))
510 (fuel-markup--print elt)
511 (fuel-markup--insert-newline)))
513 (defun fuel-markup--table (e)
514 (fuel-markup--insert-newline)
518 (mapcar #'(lambda (row) (mapcar 'fuel-markup--print-str row)) (cdr e)))
521 (defun fuel-markup--instance (e)
522 (insert " an instance of ")
523 (fuel-markup--print (cadr e)))
525 (defun fuel-markup--maybe (e)
526 (fuel-markup--instance (cons '$instance (cdr e)))
529 (defun fuel-markup--sequence (e)
531 (fuel-markup--link (list '$link 'sequence 'sequence 'word))
533 (fuel-markup--print (cadr e))
536 (defun fuel-markup--or (e)
537 (let ((fst (car (cdr e)))
538 (mid (butlast (cddr e)))
539 (lst (car (last (cdr e)))))
540 (insert (format "%s" fst))
541 (dolist (m mid) (insert (format ", %s" m)))
542 (insert (format " or %s" lst))))
544 (defun fuel-markup--values (e)
545 (fuel-markup--insert-heading "Inputs and outputs")
546 (dolist (val (cdr e))
547 (insert " " (car val) " - ")
548 (fuel-markup--print (cdr val))
551 (defun fuel-markup--predicate (e)
552 (fuel-markup--values '($values ("object" object) ("?" "a boolean")))
553 (let ((word (make-symbol (substring (format "%s" (cadr e)) 0 -1))))
554 (fuel-markup--description
555 `($description "Tests if the object is an instance of the "
556 ($link ,word) " class."))))
558 (defun fuel-markup--side-effects (e)
559 (fuel-markup--insert-heading "Side effects")
561 (fuel-markup--print (cdr e))
562 (fuel-markup--insert-newline))
564 (defun fuel-markup--definition (e)
565 (fuel-markup--insert-heading "Definition")
566 (fuel-markup--code (cons '$code (cdr e))))
568 (defun fuel-markup--methods (e)
569 (fuel-markup--insert-heading "Methods")
570 (fuel-markup--code (cons '$code (cdr e))))
572 (defun fuel-markup--value (e)
573 (fuel-markup--insert-heading "Variable value")
574 (insert "Current value in global namespace: ")
575 (fuel-markup--snippet (cons '$snippet (cdr e)))
578 (defun fuel-markup--values-x/y (e)
579 (fuel-markup--values '($values ("x" "number") ("y" "number"))))
581 (defun fuel-markup--curious (e)
582 (fuel-markup--insert-heading "For the curious...")
583 (fuel-markup--print (cdr e)))
585 (defun fuel-markup--references (e)
586 (fuel-markup--insert-heading "References")
587 (dolist (ref (cdr e))
589 (fuel-markup--print ref)
590 (fuel-markup--subsection (list '$subsection ref)))))
592 (defun fuel-markup--see-also (e)
593 (fuel-markup--insert-heading "See also")
594 (fuel-markup--links (cons '$links (cdr e)) ", "))
596 (defun fuel-markup--related (e)
597 (fuel-markup--insert-heading "See also")
598 (fuel-markup--links (cons '$links (cadr e)) ", "))
600 (defun fuel-markup--shuffle (e)
601 (insert "\nShuffle word. Re-arranges the stack "
602 "according to the stack effect pattern.")
603 (fuel-markup--insert-newline))
605 (defun fuel-markup--low-level-note (e)
606 (fuel-markup--print '($notes "Calling this word directly is not necessary "
608 "Higher-level words call it automatically.")))
610 (defun fuel-markup--parsing-note (e)
611 (fuel-markup--insert-nl-if-nb)
612 (insert "This word should only be called from parsing words.")
613 (fuel-markup--insert-newline))
615 (defun fuel-markup--io-error (e)
616 (fuel-markup--errors '($errors "Throws an error if the I/O operation fails.")))
618 (defun fuel-markup--prettyprinting-note (e)
619 (fuel-markup--print '($notes ("This word should only be called within the "
620 ($link with-pprint) " combinator."))))
622 (defun fuel-markup--prefixed-link (prefix e)
623 (insert (format " %s " prefix))
624 (fuel-markup--link e)
627 (defun fuel-markup--elem-with-heading (elem heading)
628 (fuel-markup--insert-heading heading)
629 (fuel-markup--print (cdr elem))
630 (fuel-markup--insert-newline))
632 (defun fuel-markup--stack-effect (e)
633 (let* ((in (mapconcat 'identity (nth 1 e) " "))
634 (out (mapconcat 'identity (nth 2 e) " "))
635 (str (format "( %s -- %s )" in out)))
636 (fuel-markup--snippet (list '$snippet str))))
638 (defun fuel-markup--quotation (e)
640 (fuel-markup--link (list '$link 'quotation 'quotation 'word))
641 (insert " with stack effect ")
642 (fuel-markup--stack-effect (nth 1 e)))
644 (defun fuel-markup--warning (e)
645 (fuel-markup--elem-with-heading e "Warning"))
647 (defun fuel-markup--description (e)
648 (fuel-markup--elem-with-heading e "Word description"))
650 (defun fuel-markup--class-description (e)
651 (fuel-markup--elem-with-heading e "Class description"))
653 (defun fuel-markup--error-description (e)
654 (fuel-markup--elem-with-heading e "Error description"))
656 (defun fuel-markup--var-description (e)
657 (fuel-markup--elem-with-heading e "Variable description"))
659 (defun fuel-markup--contract (e)
660 (fuel-markup--elem-with-heading e "Generic word contract"))
662 (defun fuel-markup--errors (e)
663 (fuel-markup--elem-with-heading e "Errors"))
665 (defun fuel-markup--examples (e)
666 (fuel-markup--elem-with-heading e "Examples"))
668 (defun fuel-markup--notes (e)
669 (fuel-markup--elem-with-heading e "Notes"))
671 (defun fuel-markup--word-info (e s)
672 (let* ((word (nth 1 e))
673 (cmd (and word `(:fuel* ((:quote ,(format "%s" word)) ,s) "fuel")))
674 (ret (and cmd (fuel-eval--send/wait cmd)))
675 (res (and (not (fuel-eval--retort-error ret))
676 (fuel-eval--retort-output ret))))
678 (fuel-markup--code (list '$code res))
679 (fuel-markup--snippet (list '$snippet " " word)))))
681 (defun fuel-markup--see (e)
682 (fuel-markup--word-info e 'see))
684 (defun fuel-markup--synopsis (e)
685 (fuel-markup--word-info e 'synopsis))
687 (defun fuel-markup--null (e))
690 (provide 'fuel-markup)
692 ;;; fuel-markup.el ends here