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 (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)))
104 (if fuel-stack-mode-show-sexp-p
105 (concat (fuel--shorten-str r 30) ": " e)
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.
114 When Stack mode is enabled, inferred stack effects for current
115 sexp are automatically displayed in the echo area."
117 :lighter fuel-stack-mode-string
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"))))
130 (provide 'fuel-stack)
131 ;;; fuel-stack.el ends here