]> gitweb.factorcode.org Git - factor.git/blob - misc/fuel/fuel-stack.el
FUEL: Improved internal font definition handling.
[factor.git] / misc / fuel / fuel-stack.el
1 ;;; fuel-stack.el -- stack inference help
2
3 ;; Copyright (C) 2008 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: Sat Dec 20, 2008 01:08
9
10 ;;; Comentary:
11
12 ;; Utilities and a minor mode to show inferred stack effects in the
13 ;; echo area.
14
15 ;;; Code:
16
17 (require 'fuel-autodoc)
18 (require 'fuel-syntax)
19 (require 'fuel-eval)
20 (require 'fuel-font-lock)
21 (require 'fuel-base)
22
23 \f
24 ;;; Customization
25
26 (defgroup fuel-stack nil
27   "Customization for FUEL's stack inference engine"
28   :group 'fuel)
29
30 (fuel-font-lock--defface fuel-font-lock-stack-region
31   'highlight fuel-stack "highlighting the stack effect region")
32
33 (defcustom fuel-stack-highlight-period 2.0
34   "Time, in seconds, the region is highlighted when showing its
35 stack effect.
36
37 Set it to 0 to disable highlighting."
38   :group 'fuel-stack
39   :type 'float)
40
41 (defcustom fuel-stack-mode-show-sexp-p t
42   "Whether to show in the echo area the sexp together with its stack effect."
43   :group 'fuel-stack
44   :type 'boolean)
45
46 \f
47 ;;; Querying for stack effects
48
49 (defun fuel-stack--infer-effect (str)
50   (let ((cmd `(:fuel*
51                ((:using stack-checker effects)
52                 ([ (:factor ,str) ] infer effect>string :get)))))
53     (fuel-eval--retort-result (fuel-eval--send/wait cmd 500))))
54
55 (defsubst fuel-stack--infer-effect/prop (str)
56   (let ((e (fuel-stack--infer-effect str)))
57     (when e
58       (put-text-property 0 (length e) 'face 'factor-font-lock-stack-effect e))
59     e))
60
61 (defvar fuel-stack--overlay
62   (let ((overlay (make-overlay 0 0)))
63     (overlay-put overlay 'face 'fuel-font-lock-stack-region)
64     (delete-overlay overlay)
65     overlay))
66
67 (defun fuel-stack-effect-region (begin end)
68   "Displays the inferred stack effect of the code in current region."
69   (interactive "r")
70   (when (> fuel-stack-highlight-period 0)
71     (move-overlay fuel-stack--overlay begin end))
72   (condition-case nil
73       (let* ((str (fuel--region-to-string begin end))
74              (effect (fuel-stack--infer-effect/prop str)))
75         (if effect (message "%s" effect)
76           (message "Couldn't infer effect for '%s'"
77                    (fuel--shorten-region begin end 60)))
78         (sit-for fuel-stack-highlight-period))
79     (error))
80   (delete-overlay fuel-stack--overlay))
81
82 (defun fuel-stack-effect-sexp (&optional arg)
83   "Displays the inferred stack effect for the current sexp.
84 With prefix argument, use current region instead"
85   (interactive "P")
86   (if arg
87       (call-interactively 'fuel-stack-effect-region)
88     (fuel-stack-effect-region (1+ (fuel-syntax--beginning-of-sexp-pos))
89                               (if (looking-at-p ";") (point)
90                                 (fuel-syntax--end-of-symbol-pos)))))
91
92 \f
93 ;;; Stack mode:
94
95 (make-variable-buffer-local
96  (defvar fuel-stack-mode-string " S"
97    "Modeline indicator for fuel-stack-mode"))
98
99 (defun fuel-stack--eldoc ()
100   (when (looking-at-p " \\|$")
101     (let* ((r (fuel--region-to-string (1+ (fuel-syntax--beginning-of-sexp-pos))))
102            (e (fuel-stack--infer-effect/prop r)))
103       (when e
104         (if fuel-stack-mode-show-sexp-p
105             (concat (fuel--shorten-str r 30) ": " e)
106           e)))))
107
108 (define-minor-mode fuel-stack-mode
109   "Toggle Fuel's Stack mode.
110 With no argument, this command toggles the mode.
111 Non-null prefix argument turns on the mode.
112 Null prefix argument turns off the mode.
113
114 When Stack mode is enabled, inferred stack effects for current
115 sexp are automatically displayed in the echo area."
116   :init-value nil
117   :lighter fuel-stack-mode-string
118   :group 'fuel-stack
119
120   (setq fuel-autodoc--fallback-function
121         (when fuel-stack-mode 'fuel-stack--eldoc))
122   (set (make-local-variable 'eldoc-minor-mode-string) nil)
123   (unless fuel-autodoc-mode
124     (set (make-local-variable 'eldoc-documentation-function)
125          (when fuel-stack-mode 'fuel-stack--eldoc))
126     (eldoc-mode fuel-stack-mode)
127     (message "Fuel Stack Autodoc %s" (if fuel-stack-mode "enabled" "disabled"))))
128
129 \f
130 (provide 'fuel-stack)
131 ;;; fuel-stack.el ends here