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.
17 (require 'fuel-font-lock)
26 (fuel-font-lock--defface fuel-font-lock-markup-title
27 'bold fuel-help "article titles in help buffers")
29 (fuel-font-lock--defface fuel-font-lock-markup-heading
30 'bold fuel-help "headlines in help buffers")
32 (fuel-font-lock--defface fuel-font-lock-markup-link
33 'link fuel-help "links to topics in help buffers")
35 (fuel-font-lock--defface fuel-font-lock-markup-emphasis
36 'italic fuel-help "emphasized words in help buffers")
38 (fuel-font-lock--defface fuel-font-lock-markup-strong
39 'link fuel-help "bold words in help buffers")
44 (make-variable-buffer-local
45 (defvar fuel-markup--follow-link-function 'fuel-markup--echo-link))
47 (define-button-type 'fuel-markup--button
48 'action 'fuel-markup--follow-link
49 'face 'fuel-font-lock-markup-link
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))))
59 (defun fuel-markup--echo-link (link label type)
60 (message "Link %s pointing to %s named %s" label type link))
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
69 'markup-link-type type
70 'help-echo (format "%s (%s)" label type))))
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")))))
77 (defun fuel-markup--link-at-point ()
78 (let ((button (condition-case nil (forward-button 0) (error nil))))
80 (list (button-get button 'markup-link)
81 (button-get button 'markup-label)
82 (button-get button 'markup-link-type)))))
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 ($subsections . fuel-markup--subsections)
138 ($synopsis . fuel-markup--synopsis)
139 ($syntax . fuel-markup--syntax)
140 ($table . fuel-markup--table)
141 ($tag . fuel-markup--tag)
142 ($tags . fuel-markup--tags)
143 ($unchecked-example . fuel-markup--example)
144 ($value . fuel-markup--value)
145 ($values . fuel-markup--values)
146 ($values-x/y . fuel-markup--values-x/y)
147 ($var-description . fuel-markup--var-description)
148 ($vocab-link . fuel-markup--vocab-link)
149 ($vocab-links . fuel-markup--vocab-links)
150 ($vocab-subsection . fuel-markup--vocab-subsection)
151 ($vocabulary . fuel-markup--vocabulary)
152 ($warning . fuel-markup--warning)
153 (article . fuel-markup--article)
154 (describe-words . fuel-markup--describe-words)
155 (vocab-list . fuel-markup--vocab-list)))
157 (make-variable-buffer-local
158 (defvar fuel-markup--maybe-nl nil))
160 (defun fuel-markup--print (e)
161 (cond ((null e) (insert "f"))
162 ((stringp e) (fuel-markup--insert-string e))
163 ((and (listp e) (symbolp (car e))
164 (assoc (car e) fuel-markup--printers))
165 (funcall (cdr (assoc (car e) fuel-markup--printers)) e))
167 (assoc e fuel-markup--printers))
168 (funcall (cdr (assoc e fuel-markup--printers)) e))
169 ((listp e) (mapc 'fuel-markup--print e))
170 ((symbolp e) (fuel-markup--print (list '$link e)))
171 (t (insert (format "\n%S\n" e)))))
173 (defun fuel-markup--print-str (e)
175 (fuel-markup--print e)
178 (defun fuel-markup--maybe-nl ()
179 (setq fuel-markup--maybe-nl (point)))
181 (defun fuel-markup--insert-newline (&optional justification nosqueeze)
182 (fill-region (save-excursion (beginning-of-line) (point))
184 (or justification 'left)
188 (defsubst fuel-markup--insert-nl-if-nb (&optional no-fill)
189 (unless (eq (save-excursion (beginning-of-line) (point)) (point))
190 (if no-fill (newline) (fuel-markup--insert-newline))))
192 (defsubst fuel-markup--put-face (txt face)
193 (put-text-property 0 (length txt) 'font-lock-face face txt)
196 (defun fuel-markup--insert-heading (txt &optional no-nl)
197 (fuel-markup--insert-nl-if-nb)
199 (unless (bobp) (newline))
200 (fuel-markup--put-face txt 'fuel-font-lock-markup-heading)
201 (fuel-markup--insert-string txt)
202 (unless no-nl (newline)))
204 (defun fuel-markup--insert-string (str)
205 (when fuel-markup--maybe-nl
207 (setq fuel-markup--maybe-nl nil))
210 (defun fuel-markup--article (e)
211 (setq fuel-markup--maybe-nl nil)
212 (insert (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-title))
214 (fuel-markup--print (car (cddr e))))
216 (defun fuel-markup--heading (e)
217 (fuel-markup--insert-heading (cadr e)))
219 (defun fuel-markup--subheading (e)
220 (fuel-markup--insert-heading (cadr e)))
222 (defun fuel-markup--subsection (e)
223 (fuel-markup--insert-nl-if-nb)
225 (fuel-markup--link (cons '$link (cdr e)))
226 (fuel-markup--maybe-nl))
228 (defun fuel-markup--subsections (e)
229 (dolist (link (cdr e))
230 (fuel-markup--insert-nl-if-nb)
232 (fuel-markup--link (list '$link link))
233 (fuel-markup--maybe-nl)))
235 (defun fuel-markup--vocab-subsection (e)
236 (fuel-markup--insert-nl-if-nb)
238 (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
239 (fuel-markup--maybe-nl))
241 (defun fuel-markup--newline (e)
242 (fuel-markup--insert-newline)
245 (defun fuel-markup--doc-path (e)
246 (fuel-markup--insert-heading "Related topics")
248 (dolist (art (cdr e))
249 (fuel-markup--insert-button (car art) (cadr art) 'article)
251 (delete-backward-char 2)
252 (fuel-markup--insert-newline 'left))
254 (defun fuel-markup--emphasis (e)
255 (when (stringp (cadr e))
256 (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-emphasis)
259 (defun fuel-markup--strong (e)
260 (when (stringp (cadr e))
261 (fuel-markup--put-face (cadr e) 'fuel-font-lock-markup-strong)
264 (defun fuel-markup--snippet (e)
265 (insert (mapconcat '(lambda (s)
267 (fuel-font-lock--factor-str s)
268 (fuel-markup--print-str s)))
272 (defun fuel-markup--code (e)
273 (fuel-markup--insert-nl-if-nb)
275 (dolist (snip (cdr e))
277 (insert (fuel-font-lock--factor-str snip))
278 (fuel-markup--print snip))
282 (defun fuel-markup--command (e)
283 (fuel-markup--snippet (list '$snippet (nth 3 e))))
285 (defun fuel-markup--syntax (e)
286 (fuel-markup--insert-heading "Syntax")
287 (fuel-markup--print (cons '$code (cdr e)))
290 (defun fuel-markup--example (e)
291 (fuel-markup--insert-newline)
293 (fuel-markup--snippet (list '$snippet s))
297 (defun fuel-markup--markup-example (e)
298 (fuel-markup--insert-newline)
299 (fuel-markup--snippet (cons '$snippet (cdr e))))
301 (defun fuel-markup--link (e)
302 (let* ((link (or (nth 1 e) 'f))
303 (type (or (nth 3 e) (if (symbolp link) 'word 'article)))
305 (and (eq type 'article)
306 (fuel-markup--article-title link))
308 (fuel-markup--insert-button label link type)))
310 (defun fuel-markup--links (e)
311 (dolist (link (cdr e))
312 (fuel-markup--link (list '$link link))
314 (delete-backward-char 2))
316 (defun fuel-markup--index-quotation (q)
317 (cond ((null q) null)
318 ((listp q) (vconcat (mapcar 'fuel-markup--index-quotation q)))
321 (defun fuel-markup--index (e)
322 (let* ((q (fuel-markup--index-quotation (cadr e)))
323 (cmd `(:fuel* ((,q fuel-index)) "fuel"
324 ("builtins" "help" "help.topics" "classes"
325 "classes.builtin" "classes.tuple"
326 "classes.singleton" "classes.union"
327 "classes.intersection" "classes.predicate")))
328 (subs (fuel-eval--retort-result (fuel-eval--send/wait cmd 200))))
330 (let ((start (point))
331 (sort-fold-case nil))
332 (fuel-markup--print subs)
333 (sort-lines nil start (point))))))
335 (defun fuel-markup--vocab-link (e)
336 (fuel-markup--insert-button (cadr e) (or (car (cddr e)) (cadr e)) 'vocab))
338 (defun fuel-markup--vocab-links (e)
339 (dolist (link (cdr e))
341 (fuel-markup--vocab-link (list '$vocab-link link))
344 (defun fuel-markup--vocab-list (e)
345 (let ((rows (mapcar '(lambda (elem)
346 (list (list '$vocab-link (car elem))
349 (fuel-markup--table (cons '$table rows))))
351 (defun fuel-markup--describe-vocab (e)
352 (fuel-markup--insert-nl-if-nb)
353 (let* ((cmd `(:fuel* ((,(cadr e) fuel-vocab-help)) "fuel" t))
354 (res (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
355 (when res (fuel-markup--print res))))
357 (defun fuel-markup--vocabulary (e)
358 (fuel-markup--insert-heading "Vocabulary: " t)
359 (fuel-markup--vocab-link (cons '$vocab-link (cdr e)))
362 (defun fuel-markup--parse-classes ()
364 (while (looking-at ".+ classes$")
365 (let ((heading `($heading ,(match-string-no-properties 0)))
368 (when (looking-at "Class *.+$")
369 (push (split-string (match-string-no-properties 0) nil t) rows)
371 (while (not (looking-at "$"))
372 (let* ((objs (split-string (thing-at-point 'line) nil t))
373 (class (list '$link (car objs) (car objs) 'word))
374 (super (and (cadr objs)
375 (list (list '$link (cadr objs) (cadr objs) 'word))))
376 (slots (when (cddr objs)
377 (list (mapcar '(lambda (s) (list s " ")) (cddr objs))))))
378 (push `(,class ,@super ,@slots) rows))
380 (push `(,heading ($table ,@(reverse rows))) elems))
384 (defun fuel-markup--parse-words ()
386 (while (looking-at ".+ words\\|Primitives$")
387 (let ((heading `($heading ,(match-string-no-properties 0)))
390 (when (looking-at "Word *\\(Stack effect\\|Syntax\\)$")
391 (push (list "Word" (match-string-no-properties 1)) rows)
393 (while (looking-at " ?\\(.+?\\)\\( +\\(.+\\)\\)?$")
394 (let ((word `($link ,(match-string-no-properties 1)
395 ,(match-string-no-properties 1)
397 (se (and (match-string-no-properties 3)
398 `(($snippet ,(match-string-no-properties 3))))))
399 (push `(,word ,@se) rows))
401 (push `(,heading ($table ,@(reverse rows))) elems))
405 (defun fuel-markup--parse-words-desc (desc)
408 (goto-char (point-min))
409 (when (re-search-forward "^Words$" nil t)
411 (let ((elems '(($heading "Words"))))
412 (push (fuel-markup--parse-classes) elems)
413 (push (fuel-markup--parse-words) elems)
416 (defun fuel-markup--describe-words (e)
418 (fuel-markup--print (fuel-markup--parse-words-desc (cadr e)))))
420 (defun fuel-markup--tag (e)
421 (fuel-markup--link (list '$link (cadr e) (cadr e) 'tag)))
423 (defun fuel-markup--tags (e)
425 (fuel-markup--insert-heading "Tags: " t)
426 (dolist (tag (cdr e))
427 (fuel-markup--tag (list '$tag tag))
429 (delete-backward-char 2)
430 (fuel-markup--insert-newline)))
432 (defun fuel-markup--all-tags (e)
433 (let* ((cmd `(:fuel* (all-tags :get) "fuel" t))
434 (tags (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
436 (cons '$list (mapcar (lambda (tag) (list '$link tag tag 'tag)) tags)))))
438 (defun fuel-markup--author (e)
439 (fuel-markup--link (list '$link (cadr e) (cadr e) 'author)))
441 (defun fuel-markup--authors (e)
443 (fuel-markup--insert-heading "Authors: " t)
445 (fuel-markup--author (list '$author a))
447 (delete-backward-char 2)
448 (fuel-markup--insert-newline)))
450 (defun fuel-markup--all-authors (e)
451 (let* ((cmd `(:fuel* (all-authors :get) "fuel" t))
452 (authors (fuel-eval--retort-result (fuel-eval--send/wait cmd))))
454 (cons '$list (mapcar (lambda (a) (list '$link a a 'author)) authors)))))
456 (defun fuel-markup--list (e)
457 (fuel-markup--insert-nl-if-nb)
458 (dolist (elt (cdr e))
460 (fuel-markup--print elt)
461 (fuel-markup--insert-newline)))
463 (defun fuel-markup--table (e)
464 (fuel-markup--insert-newline)
468 (mapcar '(lambda (row) (mapcar 'fuel-markup--print-str row)) (cdr e)))
471 (defun fuel-markup--instance (e)
472 (insert " an instance of ")
473 (fuel-markup--print (cadr e)))
475 (defun fuel-markup--maybe (e)
476 (fuel-markup--instance (cons '$instance (cdr e)))
479 (defun fuel-markup--or (e)
480 (let ((fst (car (cdr e)))
481 (mid (butlast (cddr e)))
482 (lst (car (last (cdr e)))))
483 (insert (format "%s" fst))
484 (dolist (m mid) (insert (format ", %s" m)))
485 (insert (format " or %s" lst))))
487 (defun fuel-markup--values (e)
488 (fuel-markup--insert-heading "Inputs and outputs")
489 (dolist (val (cdr e))
490 (insert " " (car val) " - ")
491 (fuel-markup--print (cdr val))
494 (defun fuel-markup--predicate (e)
495 (fuel-markup--values '($values ("object" object) ("?" "a boolean")))
496 (let ((word (make-symbol (substring (format "%s" (cadr e)) 0 -1))))
497 (fuel-markup--description
498 `($description "Tests if the object is an instance of the "
499 ($link ,word) " class."))))
501 (defun fuel-markup--side-effects (e)
502 (fuel-markup--insert-heading "Side effects")
504 (fuel-markup--print (cdr e))
505 (fuel-markup--insert-newline))
507 (defun fuel-markup--definition (e)
508 (fuel-markup--insert-heading "Definition")
509 (fuel-markup--code (cons '$code (cdr e))))
511 (defun fuel-markup--methods (e)
512 (fuel-markup--insert-heading "Methods")
513 (fuel-markup--code (cons '$code (cdr e))))
515 (defun fuel-markup--value (e)
516 (fuel-markup--insert-heading "Variable value")
517 (insert "Current value in global namespace: ")
518 (fuel-markup--snippet (cons '$snippet (cdr e)))
521 (defun fuel-markup--values-x/y (e)
522 (fuel-markup--values '($values ("x" "number") ("y" "number"))))
524 (defun fuel-markup--curious (e)
525 (fuel-markup--insert-heading "For the curious...")
526 (fuel-markup--print (cdr e)))
528 (defun fuel-markup--references (e)
529 (fuel-markup--insert-heading "References")
530 (dolist (ref (cdr e))
532 (fuel-markup--print ref)
533 (fuel-markup--subsection (list '$subsection ref)))))
535 (defun fuel-markup--see-also (e)
536 (fuel-markup--insert-heading "See also")
537 (fuel-markup--links (cons '$links (cdr e))))
539 (defun fuel-markup--related (e)
540 (fuel-markup--insert-heading "See also")
541 (fuel-markup--links (cons '$links (cadr e))))
543 (defun fuel-markup--shuffle (e)
544 (insert "\nShuffle word. Re-arranges the stack "
545 "according to the stack effect pattern.")
546 (fuel-markup--insert-newline))
548 (defun fuel-markup--low-level-note (e)
549 (fuel-markup--print '($notes "Calling this word directly is not necessary "
551 "Higher-level words call it automatically.")))
553 (defun fuel-markup--parsing-note (e)
554 (fuel-markup--insert-nl-if-nb)
555 (insert "This word should only be called from parsing words.")
556 (fuel-markup--insert-newline))
558 (defun fuel-markup--io-error (e)
559 (fuel-markup--errors '($errors "Throws an error if the I/O operation fails.")))
561 (defun fuel-markup--prettyprinting-note (e)
562 (fuel-markup--print '($notes ("This word should only be called within the "
563 ($link with-pprint) " combinator."))))
565 (defun fuel-markup--elem-with-heading (elem heading)
566 (fuel-markup--insert-heading heading)
567 (fuel-markup--print (cdr elem))
568 (fuel-markup--insert-newline))
570 (defun fuel-markup--quotation (e)
572 (fuel-markup--link (list '$link 'quotation 'quotation 'word))
573 (insert " with stack effect ")
574 (fuel-markup--snippet (list '$snippet (nth 1 e))))
576 (defun fuel-markup--warning (e)
577 (fuel-markup--elem-with-heading e "Warning"))
579 (defun fuel-markup--description (e)
580 (fuel-markup--elem-with-heading e "Word description"))
582 (defun fuel-markup--class-description (e)
583 (fuel-markup--elem-with-heading e "Class description"))
585 (defun fuel-markup--error-description (e)
586 (fuel-markup--elem-with-heading e "Error description"))
588 (defun fuel-markup--var-description (e)
589 (fuel-markup--elem-with-heading e "Variable description"))
591 (defun fuel-markup--contract (e)
592 (fuel-markup--elem-with-heading e "Generic word contract"))
594 (defun fuel-markup--errors (e)
595 (fuel-markup--elem-with-heading e "Errors"))
597 (defun fuel-markup--examples (e)
598 (fuel-markup--elem-with-heading e "Examples"))
600 (defun fuel-markup--notes (e)
601 (fuel-markup--elem-with-heading e "Notes"))
603 (defun fuel-markup--word-info (e s)
604 (let* ((word (nth 1 e))
605 (cmd (and word `(:fuel* ((:quote ,(format "%s" word)) ,s) "fuel")))
606 (ret (and cmd (fuel-eval--send/wait cmd)))
607 (res (and (not (fuel-eval--retort-error ret))
608 (fuel-eval--retort-output ret))))
610 (fuel-markup--code (list '$code res))
611 (fuel-markup--snippet (list '$snippet " " word)))))
613 (defun fuel-markup--see (e)
614 (fuel-markup--word-info e 'see))
616 (defun fuel-markup--synopsis (e)
617 (fuel-markup--word-info e 'synopsis))
619 (defun fuel-markup--null (e))
622 (provide 'fuel-markup)
623 ;;; fuel-markup.el ends here