1 ;;; fuel-stack.el -- stack inference help
3 ;; Copyright (C) 2008 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: Sat Dec 20, 2008 01:08
12 ;; Utilities and a minor mode to show inferred stack effects in the
17 (require 'fuel-autodoc
)
18 (require 'fuel-syntax
)
20 (require 'fuel-font-lock
)
26 (defgroup fuel-stack nil
27 "Customization for FUEL's stack inference engine."
30 (fuel-font-lock--defface fuel-font-lock-stack-region
31 'highlight fuel-stack
"highlighting the stack effect region")
33 (defcustom fuel-stack-highlight-period
2.0
34 "Time, in seconds, the region is highlighted when showing its
37 Set it to 0 to disable highlighting."
41 (defcustom fuel-stack-mode-show-sexp-p t
42 "Whether to show in the echo area the sexp together with its stack effect."
47 ;;; Querying for stack effects
49 (defun fuel-stack--infer-effect (str)
51 ((:using stack-checker effects
)
52 ([ (:factor
,str
) ] infer effect
>string
:get
)))))
53 (fuel-eval--retort-result (fuel-eval--send/wait cmd
500))))
55 (defsubst fuel-stack--infer-effect
/prop
(str)
56 (let ((e (fuel-stack--infer-effect str
)))
58 (put-text-property 0 (length e
) 'face
'factor-font-lock-stack-effect e
))
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
)
67 (defun fuel-stack-effect-region (begin end
)
68 "Displays the inferred stack effect of the code in current region."
70 (when (> fuel-stack-highlight-period
0)
71 (move-overlay fuel-stack--overlay begin end
))
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
))
80 (delete-overlay fuel-stack--overlay
))
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"
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)))))
95 (make-variable-buffer-local
96 (defvar fuel-stack-mode-string
" S"
97 "Modeline indicator for fuel-stack-mode"))
99 (make-variable-buffer-local
100 (defvar fuel-stack--region-function
102 (fuel--region-to-string (1+ (fuel-syntax--beginning-of-sexp-pos))))))
104 (defun fuel-stack--eldoc ()
105 (when (looking-at-p " \\|$")
106 (let* ((r (funcall fuel-stack--region-function
))
108 (not (string-match "^ *$" r
))
109 (fuel-stack--infer-effect/prop r
))))
111 (if fuel-stack-mode-show-sexp-p
112 (concat (fuel--shorten-str r
30) " -> " e
)
115 (define-minor-mode fuel-stack-mode
116 "Toggle Fuel's Stack mode.
117 With no argument, this command toggles the mode.
118 Non-null prefix argument turns on the mode.
119 Null prefix argument turns off the mode.
121 When Stack mode is enabled, inferred stack effects for current
122 sexp are automatically displayed in the echo area."
124 :lighter fuel-stack-mode-string
127 (setq fuel-autodoc--fallback-function
128 (when fuel-stack-mode
'fuel-stack--eldoc
))
129 (set (make-local-variable 'eldoc-minor-mode-string
) nil
)
130 (unless fuel-autodoc-mode
131 (set (make-local-variable 'eldoc-documentation-function
)
132 (when fuel-stack-mode
'fuel-stack--eldoc
))
133 (eldoc-mode fuel-stack-mode
)
134 (message "Fuel Stack Autodoc %s" (if fuel-stack-mode
"enabled" "disabled"))))
137 (provide 'fuel-stack
)
138 ;;; fuel-stack.el ends here