Klimi's new dotfiles with stow.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

510 rindas
19 KiB

pirms 4 gadiem
  1. ;;; haskell-process.el --- Communicating with the inferior Haskell process -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2011 Chris Done
  3. ;; Author: Chris Done <chrisdone@gmail.com>
  4. ;; This file is not part of GNU Emacs.
  5. ;; This file is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation; either version 3, or (at your option)
  8. ;; any later version.
  9. ;; This file is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs; see the file COPYING. If not, write to
  15. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. ;; Boston, MA 02110-1301, USA.
  17. ;;; Code:
  18. (require 'cl-lib)
  19. (require 'json)
  20. (require 'url-util)
  21. (require 'haskell-compat)
  22. (require 'haskell-session)
  23. (require 'haskell-customize)
  24. (require 'haskell-string)
  25. (defconst haskell-process-prompt-regex "\4"
  26. "Used for delimiting command replies. 4 is End of Transmission.")
  27. (defvar haskell-reload-p nil
  28. "Used internally for `haskell-process-loadish'.")
  29. (defconst haskell-process-greetings
  30. (list "Hello, Haskell!"
  31. "The lambdas must flow."
  32. "Hours of hacking await!"
  33. "The next big Haskell project is about to start!"
  34. "Your wish is my IO ().")
  35. "Greetings for when the Haskell process starts up.")
  36. (defconst haskell-process-logo
  37. (expand-file-name "logo.svg" haskell-mode-pkg-base-dir)
  38. "Haskell logo for notifications.")
  39. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  40. ;; Accessing commands -- using cl 'defstruct'
  41. (cl-defstruct haskell-command
  42. "Data structure representing a command to be executed when with
  43. a custom state and three callback."
  44. ;; hold the custom command state
  45. ;; state :: a
  46. state
  47. ;; called when to execute a command
  48. ;; go :: a -> ()
  49. go
  50. ;; called whenever output was collected from the haskell process
  51. ;; live :: a -> Response -> Bool
  52. live
  53. ;; called when the output from the haskell process indicates that the command
  54. ;; is complete
  55. ;; complete :: a -> Response -> ()
  56. complete)
  57. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  58. ;; Building the process
  59. (defun haskell-process-compute-process-log-and-command (session hptype)
  60. "Compute the log and process to start command for the SESSION from the HPTYPE.
  61. Do not actually start any process.
  62. HPTYPE is the result of calling `'haskell-process-type`' function."
  63. (let ((session-name (haskell-session-name session)))
  64. (cl-ecase hptype
  65. ('ghci
  66. (append (list (format "Starting inferior GHCi process %s ..."
  67. haskell-process-path-ghci)
  68. session-name
  69. nil)
  70. (apply haskell-process-wrapper-function
  71. (list
  72. (append (haskell-process-path-to-list haskell-process-path-ghci)
  73. haskell-process-args-ghci)))))
  74. ('cabal-new-repl
  75. (append (list (format "Starting inferior `cabal new-repl' process using %s ..."
  76. haskell-process-path-cabal)
  77. session-name
  78. nil)
  79. (apply haskell-process-wrapper-function
  80. (list
  81. (append
  82. (haskell-process-path-to-list haskell-process-path-cabal)
  83. (list "new-repl")
  84. haskell-process-args-cabal-new-repl
  85. (let ((target (haskell-session-target session)))
  86. (if target (list target) nil)))))))
  87. ('cabal-repl
  88. (append (list (format "Starting inferior `cabal repl' process using %s ..."
  89. haskell-process-path-cabal)
  90. session-name
  91. nil)
  92. (apply haskell-process-wrapper-function
  93. (list
  94. (append
  95. (haskell-process-path-to-list haskell-process-path-cabal)
  96. (list "repl")
  97. haskell-process-args-cabal-repl
  98. (let ((target (haskell-session-target session)))
  99. (if target (list target) nil)))))))
  100. ('stack-ghci
  101. (append (list (format "Starting inferior stack GHCi process using %s" haskell-process-path-stack)
  102. session-name
  103. nil)
  104. (apply haskell-process-wrapper-function
  105. (list
  106. (append
  107. (haskell-process-path-to-list haskell-process-path-stack)
  108. (list "ghci")
  109. (let ((target (haskell-session-target session)))
  110. (if target (list target) nil))
  111. haskell-process-args-stack-ghci))))))))
  112. (defun haskell-process-path-to-list (path)
  113. "Convert a path (which may be a string or a list) to a list."
  114. (if (stringp path)
  115. (list path)
  116. path))
  117. (defun haskell-process-make (name)
  118. "Make an inferior Haskell process."
  119. (list (cons 'name name)))
  120. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  121. ;; Process communication
  122. (defun haskell-process-sentinel (proc event)
  123. "The sentinel for the process pipe."
  124. (let ((session (haskell-process-project-by-proc proc)))
  125. (when session
  126. (let* ((process (haskell-session-process session)))
  127. (unless (haskell-process-restarting process)
  128. (haskell-process-log
  129. (propertize (format "Event: %S\n" event)
  130. 'face '((:weight bold))))
  131. (haskell-process-log
  132. (propertize "Process reset.\n"
  133. 'face 'font-lock-comment-face))
  134. (run-hook-with-args 'haskell-process-ended-functions process))))))
  135. (defun haskell-process-filter (proc response)
  136. "The filter for the process pipe."
  137. (let ((i 0))
  138. (cl-loop for line in (split-string response "\n")
  139. do (haskell-process-log
  140. (concat (if (= i 0)
  141. (propertize "<- " 'face 'font-lock-comment-face)
  142. " ")
  143. (propertize line 'face 'haskell-interactive-face-compile-warning)))
  144. do (setq i (1+ i))))
  145. (let ((session (haskell-process-project-by-proc proc)))
  146. (when session
  147. (if (haskell-process-cmd (haskell-session-process session))
  148. (haskell-process-collect session
  149. response
  150. (haskell-session-process session))))))
  151. (defun haskell-process-log (msg)
  152. "Effective append MSG to the process log (if enabled)."
  153. (when haskell-process-log
  154. (let* ((append-to (get-buffer-create "*haskell-process-log*")))
  155. (with-current-buffer append-to
  156. ;; point should follow insertion so that it stays at the end
  157. ;; of the buffer
  158. (setq-local window-point-insertion-type t)
  159. (let ((buffer-read-only nil))
  160. (insert msg "\n"))))))
  161. (defun haskell-process-project-by-proc (proc)
  162. "Find project by process."
  163. (cl-find-if (lambda (project)
  164. (string= (haskell-session-name project)
  165. (process-name proc)))
  166. haskell-sessions))
  167. (defun haskell-process-collect (_session response process)
  168. "Collect input for the response until receives a prompt."
  169. (haskell-process-set-response process
  170. (concat (haskell-process-response process) response))
  171. (while (haskell-process-live-updates process))
  172. (when (string-match haskell-process-prompt-regex
  173. (haskell-process-response process))
  174. (haskell-command-exec-complete
  175. (haskell-process-cmd process)
  176. (replace-regexp-in-string
  177. haskell-process-prompt-regex
  178. ""
  179. (haskell-process-response process)))
  180. (haskell-process-reset process)
  181. (haskell-process-trigger-queue process)))
  182. (defun haskell-process-reset (process)
  183. "Reset the process's state, ready for the next send/reply."
  184. (progn (haskell-process-set-response-cursor process 0)
  185. (haskell-process-set-response process "")
  186. (haskell-process-set-cmd process nil)))
  187. (defun haskell-process-consume (process regex)
  188. "Consume a regex from the response and move the cursor along if succeed."
  189. (when (string-match regex
  190. (haskell-process-response process)
  191. (haskell-process-response-cursor process))
  192. (haskell-process-set-response-cursor process (match-end 0))
  193. t))
  194. (defun haskell-process-send-string (process string)
  195. "Try to send a string to the process's process. Ask to restart if it's not running."
  196. (let ((child (haskell-process-process process)))
  197. (if (equal 'run (process-status child))
  198. (let ((out (concat string "\n")))
  199. (let ((i 0))
  200. (cl-loop for line in (split-string out "\n")
  201. do (unless (string-equal "" line)
  202. (haskell-process-log
  203. (concat (if (= i 0)
  204. (propertize "-> " 'face 'font-lock-comment-face)
  205. " ")
  206. (propertize line 'face 'font-lock-string-face))))
  207. do (setq i (1+ i))))
  208. (process-send-string child out))
  209. (unless (haskell-process-restarting process)
  210. (run-hook-with-args 'haskell-process-ended-functions process)))))
  211. (defun haskell-process-live-updates (process)
  212. "Process live updates."
  213. (haskell-command-exec-live (haskell-process-cmd process)
  214. (haskell-process-response process)))
  215. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  216. ;; Making commands
  217. (defun haskell-process-queue-without-filters (process line)
  218. "Queue LINE to be sent to PROCESS without bothering to look at
  219. the response."
  220. (haskell-process-queue-command
  221. process
  222. (make-haskell-command
  223. :state (cons process line)
  224. :go (lambda (state)
  225. (haskell-process-send-string (car state)
  226. (cdr state))))))
  227. (defun haskell-process-queue-command (process command)
  228. "Add a command to the process command queue."
  229. (haskell-process-cmd-queue-add process command)
  230. (haskell-process-trigger-queue process))
  231. (defun haskell-process-trigger-queue (process)
  232. "Trigger the next command in the queue to be ran if there is no current command."
  233. (if (and (haskell-process-process process)
  234. (process-live-p (haskell-process-process process)))
  235. (unless (haskell-process-cmd process)
  236. (let ((cmd (haskell-process-cmd-queue-pop process)))
  237. (when cmd
  238. (haskell-process-set-cmd process cmd)
  239. (haskell-command-exec-go cmd))))
  240. (progn (haskell-process-reset process)
  241. (haskell-process-set process 'command-queue nil)
  242. (run-hook-with-args 'haskell-process-ended-functions process))))
  243. (defun haskell-process-queue-flushed-p (process)
  244. "Return t if command queue has been completely processed."
  245. (not (or (haskell-process-cmd-queue process)
  246. (haskell-process-cmd process))))
  247. (defun haskell-process-queue-flush (process)
  248. "Block till PROCESS' command queue has been completely processed.
  249. This uses `accept-process-output' internally."
  250. (while (not (haskell-process-queue-flushed-p process))
  251. (haskell-process-trigger-queue process)
  252. (accept-process-output (haskell-process-process process) 1)))
  253. (defun haskell-process-queue-sync-request (process reqstr)
  254. "Queue submitting REQSTR to PROCESS and return response blockingly."
  255. (let ((cmd (make-haskell-command
  256. :state (cons nil process)
  257. :go `(lambda (s) (haskell-process-send-string (cdr s) ,reqstr))
  258. :complete 'setcar)))
  259. (haskell-process-queue-command process cmd)
  260. (haskell-process-queue-flush process)
  261. (car-safe (haskell-command-state cmd))))
  262. (defun haskell-process-get-repl-completions (process inputstr &optional limit)
  263. "Query PROCESS with `:complete repl ...' for INPUTSTR.
  264. Give optional LIMIT arg to limit completion candidates count,
  265. zero, negative values, and nil means all possible completions.
  266. Returns NIL when no completions found."
  267. (let* ((mlimit (if (and limit (> limit 0))
  268. (concat " " (number-to-string limit) " ")
  269. " "))
  270. (reqstr (concat ":complete repl"
  271. mlimit
  272. (haskell-string-literal-encode inputstr)))
  273. (rawstr (haskell-process-queue-sync-request process reqstr))
  274. (response-status (haskell-utils-repl-response-error-status rawstr)))
  275. (if (eq 'unknown-command response-status)
  276. (error
  277. "GHCi lacks `:complete' support (try installing GHC 7.8+ or ghci-ng)")
  278. (when rawstr
  279. ;; parse REPL response if any
  280. (let* ((s1 (split-string rawstr "\r?\n" t))
  281. (cs (mapcar #'haskell-string-literal-decode (cdr s1)))
  282. (h0 (car s1))) ;; "<limit count> <all count> <unused string>"
  283. (unless (string-match
  284. "\\`\\([0-9]+\\) \\([0-9]+\\) \\(\".*\"\\)\\'"
  285. h0)
  286. (error "Invalid `:complete' response"))
  287. (let ((cnt1 (match-string 1 h0))
  288. (h1 (haskell-string-literal-decode (match-string 3 h0))))
  289. (unless (= (string-to-number cnt1) (length cs))
  290. (error "Lengths inconsistent in `:complete' reponse"))
  291. (cons h1 cs)))))))
  292. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  293. ;; Accessing the process
  294. (defun haskell-process-get (process key)
  295. "Get the PROCESS's KEY value.
  296. Returns nil if KEY not set."
  297. (cdr (assq key process)))
  298. (defun haskell-process-set (process key value)
  299. "Set the PROCESS's KEY to VALUE.
  300. Returns newly set VALUE."
  301. (if process
  302. (let ((cell (assq key process)))
  303. (if cell
  304. (setcdr cell value) ; modify cell in-place
  305. (setcdr process (cons (cons key value) (cdr process))) ; new cell
  306. value))
  307. (display-warning 'haskell-interactive
  308. "`haskell-process-set' called with nil process")))
  309. ;; Wrappers using haskell-process-{get,set}
  310. (defun haskell-process-set-sent-stdin (p v)
  311. "We've sent stdin, so let's not clear the output at the end."
  312. (haskell-process-set p 'sent-stdin v))
  313. (defun haskell-process-sent-stdin-p (p)
  314. "Did we send any stdin to the process during evaluation?"
  315. (haskell-process-get p 'sent-stdin))
  316. (defun haskell-process-set-suggested-imports (p v)
  317. "Remember what imports have been suggested, to avoid
  318. re-asking about the same imports."
  319. (haskell-process-set p 'suggested-imported v))
  320. (defun haskell-process-suggested-imports (p)
  321. "Get what modules have already been suggested and accepted."
  322. (haskell-process-get p 'suggested-imported))
  323. (defun haskell-process-set-evaluating (p v)
  324. "Set status of evaluating to be on/off."
  325. (haskell-process-set p 'evaluating v))
  326. (defun haskell-process-evaluating-p (p)
  327. "Get status of evaluating (on/off)."
  328. (haskell-process-get p 'evaluating))
  329. (defun haskell-process-set-process (p v)
  330. "Set the process's inferior process."
  331. (haskell-process-set p 'inferior-process v))
  332. (defun haskell-process-process (p)
  333. "Get the process child."
  334. (haskell-process-get p 'inferior-process))
  335. (defun haskell-process-name (p)
  336. "Get the process name."
  337. (haskell-process-get p 'name))
  338. (defun haskell-process-cmd (p)
  339. "Get the process's current command.
  340. Return nil if no current command."
  341. (haskell-process-get p 'current-command))
  342. (defun haskell-process-set-cmd (p v)
  343. "Set the process's current command."
  344. (haskell-process-set-evaluating p nil)
  345. (haskell-process-set-sent-stdin p nil)
  346. (haskell-process-set-suggested-imports p nil)
  347. (haskell-process-set p 'current-command v))
  348. (defun haskell-process-response (p)
  349. "Get the process's current response."
  350. (haskell-process-get p 'current-response))
  351. (defun haskell-process-session (p)
  352. "Get the process's current session."
  353. (haskell-process-get p 'session))
  354. (defun haskell-process-set-response (p v)
  355. "Set the process's current response."
  356. (haskell-process-set p 'current-response v))
  357. (defun haskell-process-set-session (p v)
  358. "Set the process's current session."
  359. (haskell-process-set p 'session v))
  360. (defun haskell-process-response-cursor (p)
  361. "Get the process's current response cursor."
  362. (haskell-process-get p 'current-response-cursor))
  363. (defun haskell-process-set-response-cursor (p v)
  364. "Set the process's response cursor."
  365. (haskell-process-set p 'current-response-cursor v))
  366. ;; low-level command queue operations
  367. (defun haskell-process-restarting (process)
  368. "Is the PROCESS restarting?"
  369. (haskell-process-get process 'is-restarting))
  370. (defun haskell-process-cmd-queue (process)
  371. "Get the PROCESS' command queue.
  372. New entries get added to the end of the list. Use
  373. `haskell-process-cmd-queue-add' and
  374. `haskell-process-cmd-queue-pop' to modify the command queue."
  375. (haskell-process-get process 'command-queue))
  376. (defun haskell-process-cmd-queue-add (process cmd)
  377. "Add CMD to end of PROCESS's command queue."
  378. (cl-check-type cmd haskell-command)
  379. (haskell-process-set process
  380. 'command-queue
  381. (append (haskell-process-cmd-queue process)
  382. (list cmd))))
  383. (defun haskell-process-cmd-queue-pop (process)
  384. "Pop the PROCESS' next entry from command queue.
  385. Returns nil if queue is empty."
  386. (let ((queue (haskell-process-cmd-queue process)))
  387. (when queue
  388. (haskell-process-set process 'command-queue (cdr queue))
  389. (car queue))))
  390. (defun haskell-process-unignore-file (session file)
  391. "
  392. Note to Windows Emacs hackers:
  393. chmod is how to change the mode of files in POSIX
  394. systems. This will not work on your operating
  395. system.
  396. There is a command a bit like chmod called \"Calcs\"
  397. that you can try using here:
  398. http://technet.microsoft.com/en-us/library/bb490872.aspx
  399. If it works, you can submit a patch to this
  400. function and remove this comment.
  401. "
  402. (shell-command (read-from-minibuffer "Permissions command: "
  403. (concat "chmod 700 "
  404. file)))
  405. (haskell-session-modify
  406. session
  407. 'ignored-files
  408. (lambda (files)
  409. (cl-remove-if (lambda (path)
  410. (string= path file))
  411. files))))
  412. (defun haskell-command-exec-go (command)
  413. "Call the command's go function."
  414. (let ((go-func (haskell-command-go command)))
  415. (when go-func
  416. (funcall go-func (haskell-command-state command)))))
  417. (defun haskell-command-exec-complete (command response)
  418. "Call the command's complete function."
  419. (let ((comp-func (haskell-command-complete command)))
  420. (when comp-func
  421. (condition-case-unless-debug e
  422. (funcall comp-func
  423. (haskell-command-state command)
  424. response)
  425. (quit (message "Quit"))
  426. (error (message "Haskell process command errored with: %S" e))))))
  427. (defun haskell-command-exec-live (command response)
  428. "Trigger the command's live updates callback."
  429. (let ((live-func (haskell-command-live command)))
  430. (when live-func
  431. (funcall live-func
  432. (haskell-command-state command)
  433. response))))
  434. (provide 'haskell-process)
  435. ;;; haskell-process.el ends here