44
55; ; Author: Antoine R. Dumont <eniotna.t AT gmail.com>
66; ; Maintainer: Antoine R. Dumont <eniotna.t AT gmail.com>
7- ; ; Version: 0.0.4
7+ ; ; Version: 0.0.5
88; ; Package-Requires: ((purescript-mode "13.10") (dash "2.9.0") (s "1.9.0") (f "0.17.1") (deferred "0.3.2"))
99; ; Keywords: purescript psci repl major mode
1010; ; URL: https://github.com/ardumont/emacs-psci
3838; ; to purescript-mode:
3939; ; (add-hook 'purescript-mode-hook 'inferior-psci-mode)
4040
41- ; ; To activate psci directly from a purescript-mode buffer, you
42- ; ; could use repl-toggle (available on melpa):
41+ ; ; To come back and forth between a purescript-mode buffer and
42+ ; ; repl, you could use repl-toggle (available on melpa):
4343; ; (require 'repl-toggle)
4444; ; (add-to-list 'rtog/mode-repl-alist '(purescript-mode . psci))
4545
5555(require 'f )
5656(require 'deferred )
5757
58+ ; ; constants or variables
59+
5860(defvar psci/buffer-name " psci"
5961 " Buffer name of the psci buffer." )
6062
61- (defun psci/process-name (buffer-name )
62- " Compute the buffer's process name based on BUFFER-NAME."
63- (format " *%s * " buffer-name))
64-
6563(defvar psci/file-path " psci"
6664 " Path to the program used by `psci' function." )
6765
6866(defvar psci/arguments '()
6967 " Commandline arguments to pass to `psci' function." )
7068
71- (defvar psci-mode-map
72- (let ((map (nconc (make-sparse-keymap ) comint-mode-map)))
73- (define-key map " \t " 'completion-at-point )
74- map)
75- " Basic mode map for `psci' ." )
76-
7769(defvar psci/prompt " > "
7870 " The psci prompt." )
7971
72+ (defvar psci/project-module-file " .psci"
73+ " The default file referencing the purescript modules to load at psci startup." )
74+
75+ (defvar psci/--modules-folder " .psci_modules"
76+ " The modules folder psci uses as cache." )
77+
78+ ; ; private functions
79+
80+ (defun psci/--project-root! ()
81+ " Determine the project's root folder."
82+ (->> psci/project-module-file
83+ (locate-dominating-file default-directory)
84+ expand-file-name))
85+
86+ (defun psci/--process-name (buffer-name )
87+ " Compute the buffer's process name based on BUFFER-NAME."
88+ (format " *%s * " buffer-name))
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+
142+ ;;;### autoload
80143(defun psci ()
81144 " Run an inferior instance of `psci' inside Emacs."
82145 (interactive )
87150 (pop-to-buffer-same-window
88151 (if (or buffer (not (derived-mode-p 'psci-mode ))
89152 (comint-check-proc (current-buffer )))
90- (get-buffer-create (or buffer (psci/process-name psci/buffer-name)))
153+ (get-buffer-create (or buffer (psci/-- process-name psci/buffer-name)))
91154 (current-buffer )))
92155 ; ; create the comint process if there is no buffer.
93156 (unless buffer
96159 psci-program psci/arguments)
97160 (psci-mode))))
98161
162+ (defvar psci-mode-map
163+ (let ((map (nconc (make-sparse-keymap ) comint-mode-map)))
164+ (define-key map " \t " 'completion-at-point )
165+ map)
166+ " Basic mode map for `psci' ." )
167+
99168;;;### autoload
100169(define-derived-mode psci-mode comint-mode " psci"
101170 " Major mode for `run-psci' .
115184 (set (make-local-variable 'comment-start ) " -- " )
116185 (set (make-local-variable 'comment-use-syntax ) t ))
117186
118- (defun psci/--run-psci-command! (command )
119- " Run psci COMMAND as string."
120- (-when-let (process (get-buffer-process (psci/process-name psci/buffer-name)))
121- (comint-simple-send process command)
122- (process-send-eof process)))
123-
124- ; ; (defun psci/load-region! (region-start region-end)
125- ; ; "Run purescript code between REGION-START and REGION-END."
126- ; ; (interactive "r")
127- ; ; (-when-let (process (get-buffer-process (psci/process-name psci/buffer-name)))
128- ; ; (comint-send-region process region-start region-end)
129- ; ; (process-send-eof process)))
130-
131- (defun psci/--load-file! (filename )
132- " Load the purescript FILENAME inside the current running session."
133- (psci/--run-psci-command! (format " :m %s " filename)))
134-
135187;;;### autoload
136188(defun psci/load-current-file! ()
137189 " Load the current file in the psci repl."
145197 (lambda ()
146198 (call-interactively 'psci/reset! )))))))
147199
148- (defun psci/--compute-module-name! ()
149- " Compute the current file's module name."
150- (save-excursion
151- (goto-char (point-min ))
152- (let ((regexp " ^module \\ \( [a-zA-Z0-9\\ \. ]+\\ \) " ))
153- (search-forward-regexp regexp)
154- (match-string 1 ))))
155-
156200;;;### autoload
157201(defun psci/load-module! ()
158202 " Load the module inside the repl session."
159203 (interactive )
160204 (-when-let (module-name (psci/--compute-module-name!))
161205 (psci/--run-psci-command! (format " :i %s " module-name))))
162206
163- (defvar psci/project-module-file " .psci"
164- " The default file referencing the purescript modules to load at psci startup." )
165-
166- (defun psci/--file-content (filename )
167- " Load the FILENAME's content as a string.
168- When FILENAME is nil or not a real file, returns nil."
169- (when (and filename (file-exists-p filename))
170- (with-temp-buffer
171- (insert-file-contents filename)
172- (buffer-substring-no-properties (point-min ) (point-max )))))
173-
174- (defun psci/--symbol (sym n )
175- " Compute the repetition of a symbol SYM N times as a string."
176- (--> n
177- (-repeat it sym)
178- (s-join " " it)))
179-
180- (defun psci/--project-psci-file (project-root-folder )
181- " Compute the project's psci file from the PROJECT-ROOT-FOLDER.
182- Returns nil if no .psci file is found."
183- (let ((psci-module-file (expand-file-name psci/project-module-file project-root-folder)))
184- (when (file-exists-p psci-module-file)
185- psci-module-file)))
186-
187- (defun psci/--project-module-files! ()
188- " Compulse the list of modules for the current project.
189- Assumes the location of the modules is the project root folder."
190- (let* ((parent-root-folder (psci/--project-root!))
191- (psci-module-file (psci/--project-psci-file parent-root-folder)))
192- (when psci-module-file
193- (->> psci-module-file
194- psci/--file-content
195- (s-split " \n " )
196- (--map (s-concat " ./" (cadr (s-split " :m " it))))
197- (-filter 'file-exists-p )
198- nreverse ))))
199-
200- (defvar psci/--modules-folder " .psci_modules"
201- " The modules folder psci uses as cache." )
202-
203- (defun psci/--compute-modules-folder (project-root-folder )
204- " Compute the psci modules folder from PROJECT-ROOT-FOLDER."
205- (concat project-root-folder psci/--modules-folder))
206-
207207;;;### autoload
208208(defun psci/load-project-modules! ()
209209 " Load the modules needed for the repl session.
@@ -224,11 +224,11 @@ We chose to load the .psci file's content (the purescript doc proposes its use).
224224 (interactive )
225225 (psci/--run-psci-command! " :r" ))
226226
227- ( defun psci/--project-root! ()
228- " Determine the project's root folder. "
229- (-> > psci/project-module-file
230- ( locate-dominating-file default-directory )
231- expand-file-name ))
227+ ;;;### autoload
228+ ( defun psci/quit! ()
229+ " Quit the psci session. "
230+ ( interactive )
231+ (psci/--run-psci-command! " :q " ))
232232
233233(defvar inferior-psci-mode-map
234234 (let ((map (make-sparse-keymap )))
@@ -245,7 +245,7 @@ We chose to load the .psci file's content (the purescript doc proposes its use).
245245 :prefix " psci/" )
246246
247247;;;### autoload
248- (define-minor-mode inferior-psci-mode " Extend the bindings ."
248+ (define-minor-mode inferior-psci-mode " psci minor mode to define default bindings ."
249249 :lighter " ip"
250250 :keymap inferior-psci-mode-map
251251 :group 'psci )
0 commit comments