|
55 | 55 | (require 'f)
|
56 | 56 | (require 'deferred)
|
57 | 57 |
|
| 58 | +;; constants or variables |
| 59 | + |
58 | 60 | (defvar psci/buffer-name "psci"
|
59 | 61 | "Buffer name of the psci buffer.")
|
60 | 62 |
|
|
73 | 75 | (defvar psci/--modules-folder ".psci_modules"
|
74 | 76 | "The modules folder psci uses as cache.")
|
75 | 77 |
|
| 78 | +;; private functions |
| 79 | + |
76 | 80 | (defun psci/--project-root! ()
|
77 | 81 | "Determine the project's root folder."
|
78 | 82 | (->> psci/project-module-file
|
|
83 | 87 | "Compute the buffer's process name based on BUFFER-NAME."
|
84 | 88 | (format "*%s*" buffer-name))
|
85 | 89 |
|
| 90 | +(defun psci/--file-content (filename) |
| 91 | + "Load the FILENAME's content as a string. |
| 92 | +When FILENAME is nil or not a real file, returns nil." |
| 93 | + (when (and filename (file-exists-p filename)) |
| 94 | + (with-temp-buffer |
| 95 | + (insert-file-contents filename) |
| 96 | + (buffer-substring-no-properties (point-min) (point-max))))) |
| 97 | + |
| 98 | +(defun psci/--project-psci-file (project-root-folder) |
| 99 | + "Compute the project's psci file from the PROJECT-ROOT-FOLDER. |
| 100 | +Returns nil if no .psci file is found." |
| 101 | + (let ((psci-module-file (expand-file-name psci/project-module-file project-root-folder))) |
| 102 | + (when (file-exists-p psci-module-file) |
| 103 | + psci-module-file))) |
| 104 | + |
| 105 | +(defun psci/--project-module-files! () |
| 106 | + "Compulse the list of modules for the current project. |
| 107 | +Assumes the location of the modules is the project root folder." |
| 108 | + (let* ((parent-root-folder (psci/--project-root!)) |
| 109 | + (psci-module-file (psci/--project-psci-file parent-root-folder))) |
| 110 | + (when psci-module-file |
| 111 | + (->> psci-module-file |
| 112 | + psci/--file-content |
| 113 | + (s-split "\n") |
| 114 | + (--map (s-concat "./" (cadr (s-split ":m " it)))) |
| 115 | + (-filter 'file-exists-p) |
| 116 | + nreverse)))) |
| 117 | + |
| 118 | +(defun psci/--compute-modules-folder (project-root-folder) |
| 119 | + "Compute the psci modules folder from PROJECT-ROOT-FOLDER." |
| 120 | + (concat project-root-folder psci/--modules-folder)) |
| 121 | + |
| 122 | +(defun psci/--run-psci-command! (command) |
| 123 | + "Run psci COMMAND as string." |
| 124 | + (-when-let (process (get-buffer-process (psci/--process-name psci/buffer-name))) |
| 125 | + (comint-simple-send process command) |
| 126 | + (process-send-eof process))) |
| 127 | + |
| 128 | +(defun psci/--load-file! (filename) |
| 129 | + "Load the purescript FILENAME inside the current running session." |
| 130 | + (psci/--run-psci-command! (format ":m %s" filename))) |
| 131 | + |
| 132 | +(defun psci/--compute-module-name! () |
| 133 | + "Compute the current file's module name." |
| 134 | + (save-excursion |
| 135 | + (goto-char (point-min)) |
| 136 | + (let ((regexp "^module \\\([a-zA-Z0-9\\\.]+\\\) ")) |
| 137 | + (search-forward-regexp regexp) |
| 138 | + (match-string 1)))) |
| 139 | + |
| 140 | +;; public functions |
| 141 | + |
86 | 142 | ;;;###autoload
|
87 | 143 | (defun psci ()
|
88 | 144 | "Run an inferior instance of `psci' inside Emacs."
|
|
128 | 184 | (set (make-local-variable 'comment-start) "-- ")
|
129 | 185 | (set (make-local-variable 'comment-use-syntax) t))
|
130 | 186 |
|
131 |
| -(defun psci/--run-psci-command! (command) |
132 |
| - "Run psci COMMAND as string." |
133 |
| - (-when-let (process (get-buffer-process (psci/--process-name psci/buffer-name))) |
134 |
| - (comint-simple-send process command) |
135 |
| - (process-send-eof process))) |
136 |
| - |
137 |
| -(defun psci/--load-file! (filename) |
138 |
| - "Load the purescript FILENAME inside the current running session." |
139 |
| - (psci/--run-psci-command! (format ":m %s" filename))) |
140 |
| - |
141 | 187 | ;;;###autoload
|
142 | 188 | (defun psci/load-current-file! ()
|
143 | 189 | "Load the current file in the psci repl."
|
|
151 | 197 | (lambda ()
|
152 | 198 | (call-interactively 'psci/reset!)))))))
|
153 | 199 |
|
154 |
| -(defun psci/--compute-module-name! () |
155 |
| - "Compute the current file's module name." |
156 |
| - (save-excursion |
157 |
| - (goto-char (point-min)) |
158 |
| - (let ((regexp "^module \\\([a-zA-Z0-9\\\.]+\\\) ")) |
159 |
| - (search-forward-regexp regexp) |
160 |
| - (match-string 1)))) |
161 |
| - |
162 | 200 | ;;;###autoload
|
163 | 201 | (defun psci/load-module! ()
|
164 | 202 | "Load the module inside the repl session."
|
165 | 203 | (interactive)
|
166 | 204 | (-when-let (module-name (psci/--compute-module-name!))
|
167 | 205 | (psci/--run-psci-command! (format ":i %s" module-name))))
|
168 | 206 |
|
169 |
| -(defun psci/--file-content (filename) |
170 |
| - "Load the FILENAME's content as a string. |
171 |
| -When FILENAME is nil or not a real file, returns nil." |
172 |
| - (when (and filename (file-exists-p filename)) |
173 |
| - (with-temp-buffer |
174 |
| - (insert-file-contents filename) |
175 |
| - (buffer-substring-no-properties (point-min) (point-max))))) |
176 |
| - |
177 |
| -(defun psci/--project-psci-file (project-root-folder) |
178 |
| - "Compute the project's psci file from the PROJECT-ROOT-FOLDER. |
179 |
| -Returns nil if no .psci file is found." |
180 |
| - (let ((psci-module-file (expand-file-name psci/project-module-file project-root-folder))) |
181 |
| - (when (file-exists-p psci-module-file) |
182 |
| - psci-module-file))) |
183 |
| - |
184 |
| -(defun psci/--project-module-files! () |
185 |
| - "Compulse the list of modules for the current project. |
186 |
| -Assumes the location of the modules is the project root folder." |
187 |
| - (let* ((parent-root-folder (psci/--project-root!)) |
188 |
| - (psci-module-file (psci/--project-psci-file parent-root-folder))) |
189 |
| - (when psci-module-file |
190 |
| - (->> psci-module-file |
191 |
| - psci/--file-content |
192 |
| - (s-split "\n") |
193 |
| - (--map (s-concat "./" (cadr (s-split ":m " it)))) |
194 |
| - (-filter 'file-exists-p) |
195 |
| - nreverse)))) |
196 |
| - |
197 |
| -(defun psci/--compute-modules-folder (project-root-folder) |
198 |
| - "Compute the psci modules folder from PROJECT-ROOT-FOLDER." |
199 |
| - (concat project-root-folder psci/--modules-folder)) |
200 |
| - |
201 | 207 | ;;;###autoload
|
202 | 208 | (defun psci/load-project-modules! ()
|
203 | 209 | "Load the modules needed for the repl session.
|
|
0 commit comments