Klimi's new dotfiles with stow.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1491 Zeilen
64 KiB

vor 5 Jahren
  1. ;;; cider.el --- Clojure Interactive Development Environment that Rocks -*- lexical-binding: t -*-
  2. ;; Copyright © 2012-2013 Tim King, Phil Hagelberg, Bozhidar Batsov
  3. ;; Copyright © 2013-2019 Bozhidar Batsov, Artur Malabarba and CIDER contributors
  4. ;;
  5. ;; Author: Tim King <kingtim@gmail.com>
  6. ;; Phil Hagelberg <technomancy@gmail.com>
  7. ;; Bozhidar Batsov <bozhidar@batsov.com>
  8. ;; Artur Malabarba <bruce.connor.am@gmail.com>
  9. ;; Hugo Duncan <hugo@hugoduncan.org>
  10. ;; Steve Purcell <steve@sanityinc.com>
  11. ;; Maintainer: Bozhidar Batsov <bozhidar@batsov.com>
  12. ;; URL: http://www.github.com/clojure-emacs/cider
  13. ;; Version: 0.22.0-snapshot
  14. ;; Package-Requires: ((emacs "25") (clojure-mode "5.9") (parseedn "0.1") (pkg-info "0.4") (queue "0.2") (spinner "1.7") (seq "2.16") (sesman "0.3.2"))
  15. ;; Keywords: languages, clojure, cider
  16. ;; This program is free software: you can redistribute it and/or modify
  17. ;; it under the terms of the GNU General Public License as published by
  18. ;; the Free Software Foundation, either version 3 of the License, or
  19. ;; (at your option) any later version.
  20. ;; This program is distributed in the hope that it will be useful,
  21. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. ;; GNU General Public License for more details.
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. ;; This file is not part of GNU Emacs.
  27. ;;; Commentary:
  28. ;; Provides a Clojure interactive development environment for Emacs, built on
  29. ;; top of nREPL.
  30. ;;; Installation:
  31. ;; Available as a package in melpa.org and stable.melpa.org
  32. ;; (add-to-list 'package-archives
  33. ;; '("melpa" . "https://melpa.org/packages/"))
  34. ;;
  35. ;; or
  36. ;;
  37. ;; (add-to-list 'package-archives
  38. ;; '("melpa-stable" . "https://stable.melpa.org/packages/") t)
  39. ;;
  40. ;; M-x package-install cider
  41. ;;; Usage:
  42. ;; M-x cider-jack-in-clj
  43. ;; M-x cider-jack-in-cljs
  44. ;;
  45. ;; M-x cider-connect-sibling-clj
  46. ;; M-x cider-connect-sibling-cljs
  47. ;;
  48. ;; M-x cider-connect-clj
  49. ;; M-x cider-connect-cljs
  50. ;;; Code:
  51. (defgroup cider nil
  52. "Clojure Interactive Development Environment that Rocks."
  53. :prefix "cider-"
  54. :group 'applications
  55. :link '(url-link :tag "GitHub" "https://github.com/clojure-emacs/cider")
  56. :link '(url-link :tag "Homepage" "https://cider.mx")
  57. :link '(url-link :tag "Documentation" "https://docs.cider.mx")
  58. :link '(emacs-commentary-link :tag "Commentary" "cider"))
  59. (require 'cider-client)
  60. (require 'cider-eldoc)
  61. (require 'cider-repl)
  62. (require 'cider-repl-history)
  63. (require 'cider-connection)
  64. (require 'cider-mode)
  65. (require 'cider-common)
  66. (require 'cider-compat)
  67. (require 'cider-debug)
  68. (require 'cider-util)
  69. (require 'tramp-sh)
  70. (require 'subr-x)
  71. (require 'seq)
  72. (require 'sesman)
  73. (defconst cider-version "0.22.0-snapshot"
  74. "Fallback version used when it cannot be extracted automatically.
  75. Normally it won't be used, unless `pkg-info' fails to extract the
  76. version from the CIDER package or library.")
  77. (defconst cider-codename "Lima"
  78. "Codename used to denote stable releases.")
  79. (defcustom cider-lein-command
  80. "lein"
  81. "The command used to execute Leiningen."
  82. :type 'string
  83. :group 'cider)
  84. (defcustom cider-lein-global-options
  85. nil
  86. "Command global options used to execute Leiningen (e.g.: -o for offline)."
  87. :type 'string
  88. :group 'cider
  89. :safe #'stringp)
  90. (defcustom cider-lein-parameters
  91. "repl :headless :host localhost"
  92. "Params passed to Leiningen to start an nREPL server via `cider-jack-in'."
  93. :type 'string
  94. :group 'cider
  95. :safe #'stringp)
  96. (defcustom cider-boot-command
  97. "boot"
  98. "The command used to execute Boot."
  99. :type 'string
  100. :group 'cider
  101. :package-version '(cider . "0.9.0"))
  102. (defcustom cider-boot-global-options
  103. nil
  104. "Command global options used to execute Boot (e.g.: -c for checkouts)."
  105. :type 'string
  106. :group 'cider
  107. :safe #'stringp
  108. :package-version '(cider . "0.14.0"))
  109. (defcustom cider-boot-parameters
  110. "repl -s -b localhost wait"
  111. "Params passed to boot to start an nREPL server via `cider-jack-in'."
  112. :type 'string
  113. :group 'cider
  114. :safe #'stringp
  115. :package-version '(cider . "0.9.0"))
  116. (defcustom cider-clojure-cli-command
  117. "clojure"
  118. "The command used to execute clojure with tools.deps (requires Clojure 1.9+).
  119. Don't use clj here, as it doesn't work when spawned from Emacs due to
  120. it using rlwrap."
  121. :type 'string
  122. :group 'cider
  123. :safe #'stringp
  124. :package-version '(cider . "0.17.0"))
  125. (defcustom cider-clojure-cli-global-options
  126. nil
  127. "Command line options used to execute clojure with tools.deps."
  128. :type 'string
  129. :group 'cider
  130. :safe #'stringp
  131. :package-version '(cider . "0.17.0"))
  132. (defcustom cider-clojure-cli-parameters
  133. "-m nrepl.cmdline --middleware '%s'"
  134. "Params passed to clojure to start an nREPL server via `cider-jack-in'.
  135. This is evaluated using `format', with the first argument being the Clojure
  136. vector of middleware variables as a string."
  137. :type 'string
  138. :group 'cider
  139. :safe #'stringp
  140. :package-version '(cider . "0.17.0"))
  141. (defcustom cider-shadow-cljs-command
  142. "npx shadow-cljs"
  143. "The command used to execute shadow-cljs.
  144. By default we favor the project-specific shadow-cljs over the system-wide."
  145. :type 'string
  146. :group 'cider
  147. :safe #'stringp
  148. :package-version '(cider . "0.17.0"))
  149. (defcustom cider-shadow-cljs-global-options
  150. ""
  151. "Command line options used to execute shadow-cljs (e.g.: -v for verbose mode)."
  152. :type 'string
  153. :group 'cider
  154. :safe #'stringp
  155. :package-version '(cider . "0.17.0"))
  156. (defcustom cider-shadow-cljs-parameters
  157. "server"
  158. "Params passed to shadow-cljs to start an nREPL server via `cider-jack-in'."
  159. :type 'string
  160. :group 'cider
  161. :safe #'stringp
  162. :package-version '(cider . "0.17.0"))
  163. (defcustom cider-gradle-command
  164. "gradle"
  165. "The command used to execute Gradle."
  166. :type 'string
  167. :group 'cider
  168. :safe #'stringp
  169. :package-version '(cider . "0.10.0"))
  170. (defcustom cider-gradle-global-options
  171. "--no-daemon"
  172. "Command line options used to execute Gradle (e.g.: -m for dry run)."
  173. :type 'string
  174. :group 'cider
  175. :safe #'stringp
  176. :package-version '(cider . "0.14.0"))
  177. (defcustom cider-gradle-parameters
  178. "clojureRepl"
  179. "Params passed to gradle to start an nREPL server via `cider-jack-in'."
  180. :type 'string
  181. :group 'cider
  182. :safe #'stringp
  183. :package-version '(cider . "0.10.0"))
  184. (define-obsolete-variable-alias 'cider-default-repl-command 'cider-jack-in-default)
  185. (defcustom cider-jack-in-default (if (executable-find "clojure") 'clojure-cli 'lein)
  186. "The default tool to use when doing `cider-jack-in' outside a project.
  187. This value will only be consulted when no identifying file types, i.e.
  188. project.clj for leiningen or build.boot for boot, could be found.
  189. As the Clojure CLI is bundled with Clojure itself, it's the default.
  190. In the absence of the Clojure CLI (e.g. on Windows), we fallback
  191. to Leiningen."
  192. :type '(choice (const lein)
  193. (const boot)
  194. (const clojure-cli)
  195. (const shadow-cljs)
  196. (const gradle))
  197. :group 'cider
  198. :safe #'symbolp
  199. :package-version '(cider . "0.9.0"))
  200. (defcustom cider-preferred-build-tool
  201. nil
  202. "Allow choosing a build system when there are many.
  203. When there are project markers from multiple build systems (e.g. lein and
  204. boot) the user is prompted to select one of them. When non-nil, this
  205. variable will suppress this behavior and will select whatever build system
  206. is indicated by the variable if present. Note, this is only when CIDER
  207. cannot decide which of many build systems to use and will never override a
  208. command when there is no ambiguity."
  209. :type '(choice (const lein)
  210. (const boot)
  211. (const clojure-cli)
  212. (const shadow-cljs)
  213. (const gradle)
  214. (const :tag "Always ask" nil))
  215. :group 'cider
  216. :safe #'symbolp
  217. :package-version '(cider . "0.13.0"))
  218. (defcustom cider-allow-jack-in-without-project 'warn
  219. "Controls what happens when doing `cider-jack-in' outside a project.
  220. When set to 'warn you'd prompted to confirm the command.
  221. When set to t `cider-jack-in' will quietly continue.
  222. When set to nil `cider-jack-in' will fail."
  223. :type '(choice (const :tag "always" t)
  224. (const warn)
  225. (const :tag "never" nil))
  226. :group 'cider
  227. :safe #'symbolp
  228. :package-version '(cider . "0.15.0"))
  229. (defcustom cider-known-endpoints nil
  230. "A list of connection endpoints where each endpoint is a list.
  231. For example: \\='((\"label\" \"host\" \"port\")).
  232. The label is optional so that \\='(\"host\" \"port\") will suffice.
  233. This variable is used by `cider-connect'."
  234. :type '(repeat (list (string :tag "label")
  235. (string :tag "host")
  236. (string :tag "port")))
  237. :group 'cider)
  238. (defcustom cider-connected-hook nil
  239. "List of functions to call when connected to Clojure nREPL server."
  240. :type 'hook
  241. :group 'cider
  242. :package-version '(cider . "0.9.0"))
  243. (defcustom cider-disconnected-hook nil
  244. "List of functions to call when disconnected from the Clojure nREPL server."
  245. :type 'hook
  246. :group 'cider
  247. :package-version '(cider . "0.9.0"))
  248. (defcustom cider-inject-dependencies-at-jack-in t
  249. "When nil, do not inject repl dependencies (most likely nREPL middlewares) at `cider-jack-in' time."
  250. :type 'boolean
  251. :safe #'booleanp
  252. :version '(cider . "0.11.0"))
  253. (defcustom cider-offer-to-open-cljs-app-in-browser t
  254. "When nil, do not offer to open ClojureScript apps in a browser on connect."
  255. :type 'boolean
  256. :safe #'booleanp
  257. :version '(cider . "0.15.0"))
  258. (defvar cider-ps-running-nrepls-command "ps u | grep leiningen"
  259. "Process snapshot command used in `cider-locate-running-nrepl-ports'.")
  260. (defvar cider-ps-running-nrepl-path-regexp-list
  261. '("\\(?:leiningen.original.pwd=\\)\\(.+?\\) -D"
  262. "\\(?:-classpath +:?\\(.+?\\)/self-installs\\)")
  263. "Regexp list to get project paths.
  264. Extract project paths from output of `cider-ps-running-nrepls-command'.
  265. Sub-match 1 must be the project path.")
  266. (defvar cider-host-history nil
  267. "Completion history for connection hosts.")
  268. ;;;###autoload
  269. (defun cider-version ()
  270. "Display CIDER's version."
  271. (interactive)
  272. (message "CIDER %s" (cider--version)))
  273. (defun cider-jack-in-command (project-type)
  274. "Determine the command `cider-jack-in' needs to invoke for the PROJECT-TYPE."
  275. (pcase project-type
  276. ('lein cider-lein-command)
  277. ('boot cider-boot-command)
  278. ('clojure-cli cider-clojure-cli-command)
  279. ('shadow-cljs cider-shadow-cljs-command)
  280. ('gradle cider-gradle-command)
  281. (_ (user-error "Unsupported project type `%S'" project-type))))
  282. (defun cider-jack-in-resolve-command (project-type)
  283. "Determine the resolved file path to `cider-jack-in-command'.
  284. Throws an error if PROJECT-TYPE is unknown."
  285. (pcase project-type
  286. ('lein (cider--resolve-command cider-lein-command))
  287. ('boot (cider--resolve-command cider-boot-command))
  288. ('clojure-cli (cider--resolve-command cider-clojure-cli-command))
  289. ;; here we have to account for the possibility that the command is either
  290. ;; "npx shadow-cljs" or just "shadow-cljs"
  291. ('shadow-cljs (let ((parts (split-string cider-shadow-cljs-command)))
  292. (when-let* ((command (cider--resolve-command (car parts))))
  293. (mapconcat #'identity (cons command (cdr parts)) " "))))
  294. ('gradle (cider--resolve-command cider-gradle-command))
  295. (_ (user-error "Unsupported project type `%S'" project-type))))
  296. (defun cider-jack-in-global-options (project-type)
  297. "Determine the command line options for `cider-jack-in' for the PROJECT-TYPE."
  298. (pcase project-type
  299. ('lein cider-lein-global-options)
  300. ('boot cider-boot-global-options)
  301. ('clojure-cli cider-clojure-cli-global-options)
  302. ('shadow-cljs cider-shadow-cljs-global-options)
  303. ('gradle cider-gradle-global-options)
  304. (_ (user-error "Unsupported project type `%S'" project-type))))
  305. (defun cider-jack-in-params (project-type)
  306. "Determine the commands params for `cider-jack-in' for the PROJECT-TYPE."
  307. (pcase project-type
  308. ('lein cider-lein-parameters)
  309. ('boot cider-boot-parameters)
  310. ('clojure-cli (format cider-clojure-cli-parameters
  311. (concat
  312. "["
  313. (mapconcat
  314. (apply-partially #'format "\"%s\"")
  315. (cider-jack-in-normalized-nrepl-middlewares)
  316. ", ")
  317. "]")))
  318. ('shadow-cljs cider-shadow-cljs-parameters)
  319. ('gradle cider-gradle-parameters)
  320. (_ (user-error "Unsupported project type `%S'" project-type))))
  321. ;;; Jack-in dependencies injection
  322. (defvar cider-jack-in-dependencies nil
  323. "List of dependencies where elements are lists of artifact name and version.")
  324. (put 'cider-jack-in-dependencies 'risky-local-variable t)
  325. ;; We inject the newest known version of nREPL just in case
  326. ;; your version of Boot or Leiningen is bundling an older one.
  327. (cider-add-to-alist 'cider-jack-in-dependencies
  328. "nrepl" "0.6.0")
  329. (defvar cider-jack-in-cljs-dependencies nil
  330. "List of dependencies where elements are lists of artifact name and version.
  331. Added to `cider-jack-in-dependencies' when doing `cider-jack-in-cljs'.")
  332. (put 'cider-jack-in-cljs-dependencies 'risky-local-variable t)
  333. (cider-add-to-alist 'cider-jack-in-cljs-dependencies "cider/piggieback" "0.4.1")
  334. (defvar cider-jack-in-dependencies-exclusions nil
  335. "List of exclusions for jack in dependencies.
  336. Elements of the list are artifact name and list of exclusions to apply for the artifact.")
  337. (put 'cider-jack-in-dependencies-exclusions 'risky-local-variable t)
  338. (defconst cider-clojure-artifact-id "org.clojure/clojure"
  339. "Artifact identifier for Clojure.")
  340. (defconst cider-minimum-clojure-version "1.8.0"
  341. "Minimum supported version of Clojure.")
  342. (defconst cider-latest-clojure-version "1.10.0"
  343. "Latest supported version of Clojure.")
  344. (defconst cider-required-middleware-version "0.22.0-beta8"
  345. "The CIDER nREPL version that's known to work properly with CIDER.")
  346. (defcustom cider-jack-in-auto-inject-clojure nil
  347. "Version of clojure to auto-inject into REPL.
  348. If nil, do not inject Clojure into the REPL. If `latest', inject
  349. `cider-latest-clojure-version', which should approximate to the most recent
  350. version of Clojure. If `minimal', inject `cider-minimum-clojure-version',
  351. which will be the lowest version CIDER supports. If a string, use this as
  352. the version number. If it is a list, the first element should be a string,
  353. specifying the artifact ID, and the second element the version number."
  354. :type '(choice (const :tag "None" nil)
  355. (const :tag "Latest" 'latest)
  356. (const :tag "Minimal" 'minimal)
  357. (string :tag "Specific Version")
  358. (list :tag "Artifact ID and Version"
  359. (string :tag "Artifact ID")
  360. (string :tag "Version"))))
  361. (defvar cider-jack-in-lein-plugins nil
  362. "List of Leiningen plugins to be injected at jack-in.
  363. Each element is a list of artifact name and version, followed optionally by
  364. keyword arguments. The only keyword argument currently accepted is
  365. `:predicate', which should be given a function that takes the list (name,
  366. version, and keyword arguments) and returns non-nil to indicate that the
  367. plugin should actually be injected. (This is useful primarily for packages
  368. that extend CIDER, not for users. For example, a refactoring package might
  369. want to inject some middleware only when within a project context.)")
  370. (put 'cider-jack-in-lein-plugins 'risky-local-variable t)
  371. (cider-add-to-alist 'cider-jack-in-lein-plugins
  372. "cider/cider-nrepl" cider-required-middleware-version)
  373. (defvar cider-jack-in-cljs-lein-plugins nil
  374. "List of Leiningen plugins to be injected at jack-in.
  375. Added to `cider-jack-in-lein-plugins' (which see) when doing
  376. `cider-jack-in-cljs'.")
  377. (put 'cider-jack-in-cljs-lein-plugins 'risky-local-variable t)
  378. (defun cider-jack-in-normalized-lein-plugins ()
  379. "Return a normalized list of Leiningen plugins to be injected.
  380. See `cider-jack-in-lein-plugins' for the format, except that the list
  381. returned by this function does not include keyword arguments."
  382. (thread-last cider-jack-in-lein-plugins
  383. (seq-filter
  384. (lambda (spec)
  385. (if-let* ((pred (plist-get (seq-drop spec 2) :predicate)))
  386. (funcall pred spec)
  387. t)))
  388. (mapcar
  389. (lambda (spec)
  390. (seq-take spec 2)))))
  391. (defvar cider-jack-in-nrepl-middlewares nil
  392. "List of Clojure variable names.
  393. Each of these Clojure variables should hold a vector of nREPL middlewares.
  394. Instead of a string, an element can be a list containing a string followed
  395. by optional keyword arguments. The only keyword argument currently
  396. accepted is `:predicate', which should be given a function that takes the
  397. list (string and keyword arguments) and returns non-nil to indicate that
  398. the middlewares should actually be injected.")
  399. (put 'cider-jack-in-nrepl-middlewares 'risky-local-variable t)
  400. (add-to-list 'cider-jack-in-nrepl-middlewares "cider.nrepl/cider-middleware")
  401. (defvar cider-jack-in-cljs-nrepl-middlewares nil
  402. "List of Clojure variable names.
  403. Added to `cider-jack-in-nrepl-middlewares' (which see) when doing
  404. `cider-jack-in-cljs'.")
  405. (put 'cider-jack-in-cljs-nrepl-middlewares 'risky-local-variable t)
  406. (add-to-list 'cider-jack-in-cljs-nrepl-middlewares "cider.piggieback/wrap-cljs-repl")
  407. (defun cider-jack-in-normalized-nrepl-middlewares ()
  408. "Return a normalized list of middleware variable names.
  409. See `cider-jack-in-nrepl-middlewares' for the format, except that the list
  410. returned by this function only contains strings."
  411. (thread-last cider-jack-in-nrepl-middlewares
  412. (seq-filter
  413. (lambda (spec)
  414. (or (not (listp spec))
  415. (if-let* ((pred (plist-get (cdr spec) :predicate)))
  416. (funcall pred spec)
  417. t))))
  418. (mapcar
  419. (lambda (spec)
  420. (if (listp spec)
  421. (car spec)
  422. spec)))))
  423. (defun cider--list-as-boot-artifact (list)
  424. "Return a boot artifact string described by the elements of LIST.
  425. LIST should have the form (ARTIFACT-NAME ARTIFACT-VERSION). The returned
  426. string is quoted for passing as argument to an inferior shell."
  427. (concat "-d " (shell-quote-argument (format "%s:%s" (car list) (cadr list)))))
  428. (defun cider-boot-dependencies (dependencies)
  429. "Return a list of boot artifact strings created from DEPENDENCIES."
  430. (concat (mapconcat #'cider--list-as-boot-artifact dependencies " ")
  431. (unless (seq-empty-p dependencies) " ")))
  432. (defun cider-boot-middleware-task (params middlewares)
  433. "Create a command to add MIDDLEWARES with corresponding PARAMS."
  434. (concat "cider.tasks/add-middleware "
  435. (mapconcat (lambda (middleware)
  436. (format "-m %s" (shell-quote-argument middleware)))
  437. middlewares
  438. " ")
  439. " " params))
  440. (defun cider-boot-jack-in-dependencies (global-opts params dependencies plugins middlewares)
  441. "Create boot jack-in dependencies.
  442. Does so by concatenating GLOBAL-OPTS, DEPENDENCIES,
  443. PLUGINS and MIDDLEWARES. PARAMS and MIDDLEWARES are passed on to
  444. `cider-boot-middleware-task` before concatenating and DEPENDENCIES and PLUGINS
  445. are passed on to `cider-boot-dependencies`."
  446. (concat global-opts
  447. (unless (seq-empty-p global-opts) " ")
  448. "-i \"(require 'cider.tasks)\" " ;; Note the space at the end here
  449. (cider-boot-dependencies (append dependencies plugins))
  450. (cider-boot-middleware-task params middlewares)))
  451. (defun cider--lein-artifact-exclusions (exclusions)
  452. "Return an exclusions vector described by the elements of EXCLUSIONS."
  453. (if exclusions
  454. (format " :exclusions [%s]" (mapconcat #'identity exclusions " "))
  455. ""))
  456. (defun cider--list-as-lein-artifact (list &optional exclusions)
  457. "Return an artifact string described by the elements of LIST.
  458. LIST should have the form (ARTIFACT-NAME ARTIFACT-VERSION). Optionally a list
  459. of EXCLUSIONS can be provided as well. The returned
  460. string is quoted for passing as argument to an inferior shell."
  461. (shell-quote-argument (format "[%s %S%s]" (car list) (cadr list) (cider--lein-artifact-exclusions exclusions))))
  462. (defun cider-lein-jack-in-dependencies (global-opts params dependencies dependencies-exclusions lein-plugins)
  463. "Create lein jack-in dependencies.
  464. Does so by concatenating GLOBAL-OPTS, DEPENDENCIES, with DEPENDENCIES-EXCLUSIONS
  465. removed, LEIN-PLUGINS, and finally PARAMS."
  466. (concat
  467. global-opts
  468. (unless (seq-empty-p global-opts) " ")
  469. (mapconcat #'identity
  470. (append (seq-map (lambda (dep)
  471. (let ((exclusions (cadr (assoc (car dep) dependencies-exclusions))))
  472. (concat "update-in :dependencies conj "
  473. (cider--list-as-lein-artifact dep exclusions))))
  474. dependencies)
  475. (seq-map (lambda (plugin)
  476. (concat "update-in :plugins conj "
  477. (cider--list-as-lein-artifact plugin)))
  478. lein-plugins))
  479. " -- ")
  480. " -- "
  481. params))
  482. (defun cider-clojure-cli-jack-in-dependencies (global-opts params dependencies)
  483. "Create Clojure tools.deps jack-in dependencies.
  484. Does so by concatenating GLOBAL-OPTS, DEPENDENCIES finally PARAMS."
  485. (let ((dependencies (append dependencies cider-jack-in-lein-plugins)))
  486. (concat
  487. global-opts
  488. (unless (seq-empty-p global-opts) " ")
  489. "-Sdeps '{:deps {"
  490. (mapconcat #'identity
  491. (seq-map (lambda (dep) (format "%s {:mvn/version \"%s\"}" (car dep) (cadr dep))) dependencies)
  492. " ")
  493. "}}' "
  494. params)))
  495. (defun cider-shadow-cljs-jack-in-dependencies (global-opts params dependencies)
  496. "Create shadow-cljs jack-in deps.
  497. Does so by concatenating GLOBAL-OPTS, DEPENDENCIES finally PARAMS."
  498. (let ((dependencies (append dependencies cider-jack-in-lein-plugins)))
  499. (concat
  500. global-opts
  501. (unless (seq-empty-p global-opts) " ")
  502. (mapconcat #'identity
  503. (seq-map (lambda (dep) (format "-d %s:%s" (car dep) (cadr dep))) dependencies)
  504. " ")
  505. " "
  506. params)))
  507. (defun cider-add-clojure-dependencies-maybe (dependencies)
  508. "Return DEPENDENCIES with an added Clojure dependency if requested.
  509. See also `cider-jack-in-auto-inject-clojure'."
  510. (if cider-jack-in-auto-inject-clojure
  511. (if (consp cider-jack-in-auto-inject-clojure)
  512. (cons cider-jack-in-auto-inject-clojure dependencies)
  513. (cons (list cider-clojure-artifact-id
  514. (cond
  515. ((stringp cider-jack-in-auto-inject-clojure)
  516. cider-jack-in-auto-inject-clojure)
  517. ((eq cider-jack-in-auto-inject-clojure 'minimal)
  518. cider-minimum-clojure-version)
  519. ((eq cider-jack-in-auto-inject-clojure 'latest)
  520. cider-latest-clojure-version)))
  521. dependencies))
  522. dependencies))
  523. (defun cider-inject-jack-in-dependencies (global-opts params project-type)
  524. "Return GLOBAL-OPTS and PARAMS with injected REPL dependencies.
  525. These are set in `cider-jack-in-dependencies', `cider-jack-in-lein-plugins' and
  526. `cider-jack-in-nrepl-middlewares' are injected from the CLI according to
  527. the used PROJECT-TYPE. Eliminates the need for hacking profiles.clj or the
  528. boot script for supporting CIDER with its nREPL middleware and
  529. dependencies."
  530. (pcase project-type
  531. ('lein (cider-lein-jack-in-dependencies
  532. global-opts
  533. params
  534. (cider-add-clojure-dependencies-maybe
  535. cider-jack-in-dependencies)
  536. cider-jack-in-dependencies-exclusions
  537. (cider-jack-in-normalized-lein-plugins)))
  538. ('boot (cider-boot-jack-in-dependencies
  539. global-opts
  540. params
  541. (cider-add-clojure-dependencies-maybe
  542. cider-jack-in-dependencies)
  543. (cider-jack-in-normalized-lein-plugins)
  544. (cider-jack-in-normalized-nrepl-middlewares)))
  545. ('clojure-cli (cider-clojure-cli-jack-in-dependencies
  546. global-opts
  547. params
  548. (cider-add-clojure-dependencies-maybe
  549. cider-jack-in-dependencies)))
  550. ('shadow-cljs (cider-shadow-cljs-jack-in-dependencies
  551. global-opts
  552. params
  553. (cider-add-clojure-dependencies-maybe
  554. cider-jack-in-dependencies)))
  555. ('gradle (concat
  556. global-opts
  557. (unless (seq-empty-p global-opts) " ")
  558. params))
  559. (_ (error "Unsupported project type `%S'" project-type))))
  560. ;;; ClojureScript REPL creation
  561. (defcustom cider-check-cljs-repl-requirements t
  562. "When non-nil will run the requirement checks for the different cljs repls.
  563. Generally you should not disable this unless you run into some faulty check."
  564. :type 'boolean
  565. :safe #'booleanp
  566. :package-version '(cider . "0.17.0"))
  567. (defun cider-verify-clojurescript-is-present ()
  568. "Check whether ClojureScript is present."
  569. (unless (cider-library-present-p "cljs.core")
  570. (user-error "ClojureScript is not available. See https://docs.cider.mx/cider/basics/clojurescript for details")))
  571. (defun cider-verify-piggieback-is-present ()
  572. "Check whether the piggieback middleware is present."
  573. (unless (cider-library-present-p "cider.piggieback")
  574. (user-error "Piggieback 0.4.x (aka cider/piggieback) is not available. See https://docs.cider.mx/cider/basics/clojurescript for details")))
  575. (defun cider-check-nashorn-requirements ()
  576. "Check whether we can start a Nashorn ClojureScript REPL."
  577. (cider-verify-piggieback-is-present))
  578. (defun cider-check-node-requirements ()
  579. "Check whether we can start a Node ClojureScript REPL."
  580. (cider-verify-piggieback-is-present)
  581. (unless (executable-find "node")
  582. (user-error "Node.js is not present on the exec-path. Make sure you've installed it and your exec-path is properly set")))
  583. (defun cider-check-figwheel-requirements ()
  584. "Check whether we can start a Figwheel ClojureScript REPL."
  585. (cider-verify-piggieback-is-present)
  586. (unless (cider-library-present-p "figwheel-sidecar.repl")
  587. (user-error "Figwheel-sidecar is not available. Please check https://docs.cider.mx/cider/basics/clojurescript")))
  588. (defun cider-check-figwheel-main-requirements ()
  589. "Check whether we can start a Figwheel ClojureScript REPL."
  590. (cider-verify-piggieback-is-present)
  591. (unless (cider-library-present-p "figwheel.main")
  592. (user-error "Figwheel-main is not available. Please check https://docs.cider.mx/cider/basics/clojurescript")))
  593. (defun cider-check-weasel-requirements ()
  594. "Check whether we can start a Weasel ClojureScript REPL."
  595. (cider-verify-piggieback-is-present)
  596. (unless (cider-library-present-p "weasel.repl.server")
  597. (user-error "Weasel in not available. Please check https://docs.cider.mx/cider/basics/clojurescript/#browser-connected-clojurescript-repl")))
  598. (defun cider-check-boot-requirements ()
  599. "Check whether we can start a Boot ClojureScript REPL."
  600. (cider-verify-piggieback-is-present)
  601. (unless (cider-library-present-p "adzerk.boot-cljs-repl")
  602. (user-error "The Boot ClojureScript REPL is not available. Please check https://github.com/adzerk-oss/boot-cljs-repl/blob/master/README.md")))
  603. (defun cider-check-shadow-cljs-requirements ()
  604. "Check whether we can start a shadow-cljs REPL."
  605. (unless (cider-library-present-p "shadow.repl")
  606. (user-error "The shadow-cljs ClojureScript REPL is not available")))
  607. (defun cider-normalize-cljs-init-options (options)
  608. "Normalize the OPTIONS string used for initializing a ClojureScript REPL."
  609. (if (or (string-prefix-p "{" options)
  610. (string-prefix-p "(" options)
  611. (string-prefix-p "[" options)
  612. (string-prefix-p ":" options)
  613. (string-prefix-p "\"" options))
  614. options
  615. (concat ":" options)))
  616. (defcustom cider-shadow-default-options nil
  617. "Defines default `shadow-cljs' options."
  618. :type 'string
  619. :safe (lambda (s) (or (null s) (stringp s)))
  620. :package-version '(cider . "0.18.0"))
  621. (defun cider--shadow-parse-builds (hash)
  622. "Parses the build names of a shadow-cljs.edn HASH map.
  623. The default options of `browser-repl' and `node-repl' are also included."
  624. (let* ((builds (when (hash-table-p hash)
  625. (gethash :builds hash)))
  626. (build-keys (when (hash-table-p builds)
  627. (hash-table-keys builds))))
  628. (append build-keys '(browser-repl node-repl))))
  629. (defun cider--shadow-get-builds ()
  630. "Extract build names from the shadow-cljs.edn config file in the project root."
  631. (let ((shadow-edn (concat (clojure-project-dir) "shadow-cljs.edn")))
  632. (when (file-exists-p shadow-edn)
  633. (with-temp-buffer
  634. (insert-file-contents shadow-edn)
  635. (let ((hash (car (parseedn-read '((shadow/env . identity))))))
  636. (cider--shadow-parse-builds hash))))))
  637. (defun cider-shadow-select-cljs-init-form ()
  638. "Generate the init form for a shadow-cljs select-only REPL.
  639. We have to prompt the user to select a build, that's why this is a command,
  640. not just a string."
  641. (let ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/nrepl-select %s))")
  642. (options (or cider-shadow-default-options
  643. (completing-read "Select shadow-cljs build: "
  644. (cider--shadow-get-builds)))))
  645. (format form (cider-normalize-cljs-init-options options))))
  646. (defun cider-shadow-cljs-init-form ()
  647. "Generate the init form for a shadow-cljs REPL.
  648. We have to prompt the user to select a build, that's why
  649. this is a command, not just a string."
  650. (let* ((form "(do (require '[shadow.cljs.devtools.api :as shadow]) (shadow/watch %s) (shadow/nrepl-select %s))")
  651. (options (or cider-shadow-default-options
  652. (completing-read "Select shadow-cljs build: "
  653. (cider--shadow-get-builds))))
  654. (build (cider-normalize-cljs-init-options options)))
  655. (format form build build)))
  656. (defcustom cider-figwheel-main-default-options nil
  657. "Defines the `figwheel.main/start' options.
  658. Note that figwheel-main/start can also accept a map of options, refer to
  659. Figwheel for details."
  660. :type 'string
  661. :safe (lambda (s) (or (null s) (stringp s)))
  662. :package-version '(cider . "0.18.0"))
  663. (defun cider-figwheel-main-init-form ()
  664. "Produce the figwheel-main ClojureScript init form."
  665. (let ((form "(do (require 'figwheel.main) (figwheel.main/start %s))")
  666. (options (string-trim
  667. (or cider-figwheel-main-default-options
  668. (read-from-minibuffer "Select figwheel-main build (e.g. :dev): ")))))
  669. (format form (cider-normalize-cljs-init-options options))))
  670. (defun cider-custom-cljs-repl-init-form ()
  671. "Prompt for a form that would start a ClojureScript REPL.
  672. The supplied string will be wrapped in a do form if needed."
  673. (let ((form (read-from-minibuffer "Please, provide a form to start a ClojureScript REPL: ")))
  674. ;; TODO: We should probably make this more robust (e.g. by using a regexp or
  675. ;; parsing the form).
  676. (if (string-prefix-p "(do" form)
  677. form
  678. (format "(do %s)" form))))
  679. (defvar cider-cljs-repl-types
  680. '((nashorn "(do (require 'cljs.repl.nashorn) (cider.piggieback/cljs-repl (cljs.repl.nashorn/repl-env)))"
  681. cider-check-nashorn-requirements)
  682. (figwheel "(do (require 'figwheel-sidecar.repl-api) (figwheel-sidecar.repl-api/start-figwheel!) (figwheel-sidecar.repl-api/cljs-repl))"
  683. cider-check-figwheel-requirements)
  684. (figwheel-main cider-figwheel-main-init-form cider-check-figwheel-main-requirements)
  685. (figwheel-connected "(figwheel-sidecar.repl-api/cljs-repl)"
  686. cider-check-figwheel-requirements)
  687. (node "(do (require 'cljs.repl.node) (cider.piggieback/cljs-repl (cljs.repl.node/repl-env)))"
  688. cider-check-node-requirements)
  689. (weasel "(do (require 'weasel.repl.websocket) (cider.piggieback/cljs-repl (weasel.repl.websocket/repl-env :ip \"127.0.0.1\" :port 9001)))"
  690. cider-check-weasel-requirements)
  691. (boot "(do (require 'adzerk.boot-cljs-repl) (adzerk.boot-cljs-repl/start-repl))"
  692. cider-check-boot-requirements)
  693. (shadow cider-shadow-cljs-init-form cider-check-shadow-cljs-requirements)
  694. (shadow-select cider-shadow-select-cljs-init-form cider-check-shadow-cljs-requirements)
  695. (custom cider-custom-cljs-repl-init-form nil))
  696. "A list of supported ClojureScript REPLs.
  697. For each one we have its name, the form we need to evaluate in a Clojure
  698. REPL to start the ClojureScript REPL and functions to very their requirements.
  699. The form should be either a string or a function producing a string.")
  700. (defun cider-register-cljs-repl-type (type init-form &optional requirements-fn)
  701. "Register a new ClojureScript REPL type.
  702. Types are defined by the following:
  703. - TYPE - symbol identifier that will be used to refer to the REPL type
  704. - INIT-FORM - string or function (symbol) producing string
  705. - REQUIREMENTS-FN - function to check whether the REPL can be started.
  706. This param is optional.
  707. All this function does is modifying `cider-cljs-repl-types'.
  708. It's intended to be used in your Emacs config."
  709. (unless (symbolp type)
  710. (user-error "The REPL type must be a symbol"))
  711. (unless (or (stringp init-form) (symbolp init-form))
  712. (user-error "The init form must be a string or a symbol referring to a function"))
  713. (unless (or (null requirements-fn) (symbolp requirements-fn))
  714. (user-error "The requirements-fn must be a symbol referring to a function"))
  715. (add-to-list 'cider-cljs-repl-types (list type init-form requirements-fn)))
  716. (defcustom cider-default-cljs-repl nil
  717. "The default ClojureScript REPL to start.
  718. This affects commands like `cider-jack-in-cljs'. Generally it's
  719. intended to be set via .dir-locals.el for individual projects, as its
  720. relatively unlikely you'd like to use the same type of REPL in each project
  721. you're working on."
  722. :type '(choice (const :tag "Nashorn" nashorn)
  723. (const :tag "Figwheel" figwheel)
  724. (const :tag "Figwheel Main" figwheel-main)
  725. (const :tag "Node" node)
  726. (const :tag "Weasel" weasel)
  727. (const :tag "Boot" boot)
  728. (const :tag "Shadow" shadow)
  729. (const :tag "Shadow w/o Server" shadow-select)
  730. (const :tag "Custom" custom))
  731. :group 'cider
  732. :safe #'symbolp
  733. :package-version '(cider . "0.17.0"))
  734. (make-obsolete-variable 'cider-cljs-lein-repl 'cider-default-cljs-repl "0.17")
  735. (make-obsolete-variable 'cider-cljs-boot-repl 'cider-default-cljs-repl "0.17")
  736. (make-obsolete-variable 'cider-cljs-gradle-repl 'cider-default-cljs-repl "0.17")
  737. (defvar cider--select-cljs-repl-history nil)
  738. (defun cider-select-cljs-repl (&optional default)
  739. "Select the ClojureScript REPL to use with `cider-jack-in-cljs'.
  740. DEFAULT is the default ClojureScript REPL to offer in completion."
  741. (let ((repl-types (mapcar #'car cider-cljs-repl-types)))
  742. (intern (completing-read "Select ClojureScript REPL type: " repl-types
  743. nil nil nil 'cider--select-cljs-repl-history
  744. (or default (car cider--select-cljs-repl-history))))))
  745. (defun cider-cljs-repl-form (repl-type)
  746. "Get the cljs REPL form for REPL-TYPE."
  747. (if-let* ((repl-form (cadr (seq-find
  748. (lambda (entry)
  749. (eq (car entry) repl-type))
  750. cider-cljs-repl-types))))
  751. ;; repl-form can be either a string or a function producing a string
  752. (if (symbolp repl-form)
  753. (funcall repl-form)
  754. repl-form)
  755. (user-error "No ClojureScript REPL type %s found. Please make sure that `cider-cljs-repl-types' has an entry for it" repl-type)))
  756. (defun cider-verify-cljs-repl-requirements (&optional repl-type)
  757. "Verify that the requirements for REPL-TYPE are met.
  758. Return REPL-TYPE if requirements are met."
  759. (let ((repl-type (or repl-type
  760. cider-default-cljs-repl
  761. (cider-select-cljs-repl))))
  762. (when cider-check-cljs-repl-requirements
  763. (when-let* ((fun (nth 2 (seq-find
  764. (lambda (entry)
  765. (eq (car entry) repl-type))
  766. cider-cljs-repl-types))))
  767. (funcall fun)))
  768. repl-type))
  769. (defun cider--check-cljs (&optional cljs-type no-error)
  770. "Verify that all cljs requirements are met for CLJS-TYPE connection.
  771. Return REPL-TYPE of requirement are met, and throw an user-error otherwise.
  772. When NO-ERROR is non-nil, don't throw an error, issue a message and return
  773. nil."
  774. (if no-error
  775. (condition-case ex
  776. (progn
  777. (cider-verify-clojurescript-is-present)
  778. (cider-verify-cljs-repl-requirements cljs-type))
  779. (error
  780. (message "Invalid ClojureScript dependency: %S" ex)
  781. nil))
  782. (cider-verify-clojurescript-is-present)
  783. (cider-verify-cljs-repl-requirements cljs-type)))
  784. (defun cider--offer-to-open-app-in-browser (server-buffer)
  785. "Look for a server address in SERVER-BUFFER and offer to open it."
  786. (when (buffer-live-p server-buffer)
  787. (with-current-buffer server-buffer
  788. (save-excursion
  789. (goto-char (point-min))
  790. (when-let* ((url (and (search-forward-regexp "http://localhost:[0-9]+" nil 'noerror)
  791. (match-string 0))))
  792. (when (y-or-n-p (format "Visit ‘%s’ in a browser? " url))
  793. (browse-url url)))))))
  794. ;;; User Level Connectors
  795. ;;;###autoload (autoload 'cider-start-map "cider" "CIDER jack-in and connect keymap." t 'keymap)
  796. (defvar cider-start-map
  797. (let ((map (define-prefix-command 'cider-start-map)))
  798. (define-key map (kbd "x") #'cider)
  799. (define-key map (kbd "C-x") #'cider)
  800. (define-key map (kbd "j j") #'cider-jack-in-clj)
  801. (define-key map (kbd "j s") #'cider-jack-in-cljs)
  802. (define-key map (kbd "j m") #'cider-jack-in-clj&cljs)
  803. (define-key map (kbd "C-j j") #'cider-jack-in-clj)
  804. (define-key map (kbd "C-j s") #'cider-jack-in-cljs)
  805. (define-key map (kbd "C-j m") #'cider-jack-in-clj&cljs)
  806. (define-key map (kbd "C-j C-j") #'cider-jack-in-clj)
  807. (define-key map (kbd "C-j C-s") #'cider-jack-in-cljs)
  808. (define-key map (kbd "C-j C-m") #'cider-jack-in-clj&cljs)
  809. (define-key map (kbd "c j") #'cider-connect-clj)
  810. (define-key map (kbd "c s") #'cider-connect-cljs)
  811. (define-key map (kbd "c m") #'cider-connect-clj&cljs)
  812. (define-key map (kbd "C-c j") #'cider-connect-clj)
  813. (define-key map (kbd "C-c s") #'cider-connect-cljs)
  814. (define-key map (kbd "C-c m") #'cider-connect-clj&cljs)
  815. (define-key map (kbd "C-c C-j") #'cider-connect-clj)
  816. (define-key map (kbd "C-c C-s") #'cider-connect-cljs)
  817. (define-key map (kbd "C-c C-m") #'cider-connect-clj&cljs)
  818. (define-key map (kbd "s j") #'cider-connect-sibling-clj)
  819. (define-key map (kbd "s s") #'cider-connect-sibling-cljs)
  820. (define-key map (kbd "C-s j") #'cider-connect-sibling-clj)
  821. (define-key map (kbd "C-s s") #'cider-connect-sibling-cljs)
  822. (define-key map (kbd "C-s C-j") #'cider-connect-sibling-clj)
  823. (define-key map (kbd "C-s C-s") #'cider-connect-sibling-cljs)
  824. map)
  825. "CIDER jack-in and connect keymap.")
  826. ;;;###autoload
  827. (defun cider-jack-in-clj (params)
  828. "Start an nREPL server for the current project and connect to it.
  829. PARAMS is a plist optionally containing :project-dir and :jack-in-cmd.
  830. With the prefix argument, allow editing of the jack in command; with a
  831. double prefix prompt for all these parameters."
  832. (interactive "P")
  833. (let ((params (thread-first params
  834. (cider--update-project-dir)
  835. (cider--check-existing-session)
  836. (cider--update-jack-in-cmd))))
  837. (nrepl-start-server-process
  838. (plist-get params :project-dir)
  839. (plist-get params :jack-in-cmd)
  840. (lambda (server-buffer)
  841. (cider-connect-sibling-clj params server-buffer)))))
  842. ;;;###autoload
  843. (defun cider-jack-in-cljs (params)
  844. "Start an nREPL server for the current project and connect to it.
  845. PARAMS is a plist optionally containing :project-dir, :jack-in-cmd and
  846. :cljs-repl-type (e.g. Node, Figwheel, etc). With the prefix argument,
  847. allow editing of the jack in command; with a double prefix prompt for all
  848. these parameters."
  849. (interactive "P")
  850. (let ((cider-jack-in-dependencies (append cider-jack-in-dependencies cider-jack-in-cljs-dependencies))
  851. (cider-jack-in-lein-plugins (append cider-jack-in-lein-plugins cider-jack-in-cljs-lein-plugins))
  852. (cider-jack-in-nrepl-middlewares (append cider-jack-in-nrepl-middlewares cider-jack-in-cljs-nrepl-middlewares))
  853. (orig-buffer (current-buffer)))
  854. ;; cider--update-jack-in-cmd relies indirectly on the above dynamic vars
  855. (let ((params (thread-first params
  856. (cider--update-project-dir)
  857. (cider--check-existing-session)
  858. (cider--update-jack-in-cmd))))
  859. (nrepl-start-server-process
  860. (plist-get params :project-dir)
  861. (plist-get params :jack-in-cmd)
  862. (lambda (server-buffer)
  863. (with-current-buffer orig-buffer
  864. (cider-connect-sibling-cljs params server-buffer)))))))
  865. ;;;###autoload
  866. (defun cider-jack-in-clj&cljs (&optional params soft-cljs-start)
  867. "Start an nREPL server and connect with clj and cljs REPLs.
  868. PARAMS is a plist optionally containing :project-dir, :jack-in-cmd and
  869. :cljs-repl-type (e.g. Node, Figwheel, etc). With the prefix argument,
  870. allow for editing of the jack in command; with a double prefix prompt for
  871. all these parameters. When SOFT-CLJS-START is non-nil, start cljs REPL
  872. only when the ClojureScript dependencies are met."
  873. (interactive "P")
  874. (let ((cider-jack-in-dependencies (append cider-jack-in-dependencies cider-jack-in-cljs-dependencies))
  875. (cider-jack-in-lein-plugins (append cider-jack-in-lein-plugins cider-jack-in-cljs-lein-plugins))
  876. (cider-jack-in-nrepl-middlewares (append cider-jack-in-nrepl-middlewares cider-jack-in-cljs-nrepl-middlewares))
  877. (orig-buffer (current-buffer)))
  878. ;; cider--update-jack-in-cmd relies indirectly on the above dynamic vars
  879. (let ((params (thread-first params
  880. (cider--update-project-dir)
  881. (cider--check-existing-session)
  882. (cider--update-jack-in-cmd)
  883. (cider--update-cljs-type)
  884. ;; already asked, don't ask on sibling connect
  885. (plist-put :do-prompt nil))))
  886. (nrepl-start-server-process
  887. (plist-get params :project-dir)
  888. (plist-get params :jack-in-cmd)
  889. (lambda (server-buffer)
  890. (with-current-buffer orig-buffer
  891. (let ((clj-repl (cider-connect-sibling-clj params server-buffer)))
  892. (if soft-cljs-start
  893. (when (cider--check-cljs (plist-get params :cljs-repl-type) 'no-error)
  894. (cider-connect-sibling-cljs params clj-repl))
  895. (cider-connect-sibling-cljs params clj-repl)))))))))
  896. ;;;###autoload
  897. (defun cider-connect-sibling-clj (params &optional other-repl)
  898. "Create a Clojure REPL with the same server as OTHER-REPL.
  899. PARAMS is for consistency with other connection commands and is currently
  900. ignored. OTHER-REPL defaults to `cider-current-repl' and in programs can
  901. also be a server buffer, in which case a new session with a REPL for that
  902. server is created."
  903. (interactive "P")
  904. (cider-nrepl-connect
  905. (let* ((other-repl (or other-repl (cider-current-repl nil 'ensure)))
  906. (other-params (cider--gather-connect-params nil other-repl))
  907. (ses-name (unless (nrepl-server-p other-repl)
  908. (sesman-session-name-for-object 'CIDER other-repl))))
  909. (thread-first params
  910. (cider--update-do-prompt)
  911. (append other-params)
  912. (plist-put :repl-init-function nil)
  913. (plist-put :repl-type 'clj)
  914. (plist-put :session-name ses-name)))))
  915. ;;;###autoload
  916. (defun cider-connect-sibling-cljs (params &optional other-repl)
  917. "Create a ClojureScript REPL with the same server as OTHER-REPL.
  918. PARAMS is a plist optionally containing :cljs-repl-type (e.g. Node,
  919. Figwheel, etc). All other parameters are inferred from the OTHER-REPL.
  920. OTHER-REPL defaults to `cider-current-repl' but in programs can also be a
  921. server buffer, in which case a new session for that server is created."
  922. (interactive "P")
  923. (let* ((other-repl (or other-repl (cider-current-repl nil 'ensure)))
  924. (other-params (cider--gather-connect-params nil other-repl))
  925. (ses-name (unless (nrepl-server-p other-repl)
  926. (sesman-session-name-for-object 'CIDER other-repl))))
  927. (cider-nrepl-connect
  928. (thread-first params
  929. (cider--update-do-prompt)
  930. (append other-params)
  931. (cider--update-cljs-type)
  932. (cider--update-cljs-init-function)
  933. (plist-put :session-name ses-name)
  934. (plist-put :repl-type 'pending-cljs)))))
  935. ;;;###autoload
  936. (defun cider-connect-clj (&optional params)
  937. "Initialize a Clojure connection to an nREPL server.
  938. PARAMS is a plist optionally containing :host, :port and :project-dir. On
  939. prefix argument, prompt for all the parameters."
  940. (interactive "P")
  941. (cider-nrepl-connect
  942. (thread-first params
  943. (cider--update-project-dir)
  944. (cider--update-host-port)
  945. (cider--check-existing-session)
  946. (plist-put :repl-init-function nil)
  947. (plist-put :session-name nil)
  948. (plist-put :repl-type 'clj))))
  949. ;;;###autoload
  950. (defun cider-connect-cljs (&optional params)
  951. "Initialize a ClojureScript connection to an nREPL server.
  952. PARAMS is a plist optionally containing :host, :port, :project-dir and
  953. :cljs-repl-type (e.g. Node, Figwheel, etc). On prefix, prompt for all the
  954. parameters regardless of their supplied or default values."
  955. (interactive "P")
  956. (cider-nrepl-connect
  957. (thread-first params
  958. (cider--update-project-dir)
  959. (cider--update-host-port)
  960. (cider--check-existing-session)
  961. (cider--update-cljs-type)
  962. (cider--update-cljs-init-function)
  963. (plist-put :session-name nil)
  964. (plist-put :repl-type 'pending-cljs))))
  965. ;;;###autoload
  966. (defun cider-connect-clj&cljs (params &optional soft-cljs-start)
  967. "Initialize a Clojure and ClojureScript connection to an nREPL server.
  968. PARAMS is a plist optionally containing :host, :port, :project-dir and
  969. :cljs-repl-type (e.g. Node, Figwheel, etc). When SOFT-CLJS-START is
  970. non-nil, don't start if ClojureScript requirements are not met."
  971. (interactive "P")
  972. (let* ((params (thread-first params
  973. (cider--update-project-dir)
  974. (cider--update-host-port)
  975. (cider--check-existing-session)
  976. (cider--update-cljs-type)))
  977. (clj-repl (cider-connect-clj params)))
  978. (if soft-cljs-start
  979. (when (cider--check-cljs (plist-get params :cljs-repl-type) 'no-error)
  980. (cider-connect-sibling-cljs params clj-repl))
  981. (cider-connect-sibling-cljs params clj-repl))))
  982. (defvar cider-connection-init-commands
  983. '(cider-jack-in-clj
  984. cider-jack-in-cljs
  985. cider-jack-in-clj&cljs
  986. cider-connect-clj
  987. cider-connect-cljs
  988. cider-connect-clj&cljs
  989. cider-connect-sibling-clj
  990. cider-connect-sibling-cljs)
  991. "A list of all user-level connection init commands in CIDER.")
  992. ;;;###autoload
  993. (defun cider ()
  994. "Start a connection of any type interactively."
  995. (interactive)
  996. (when-let* ((command (intern (completing-read "Select command: " cider-connection-init-commands))))
  997. (call-interactively command)))
  998. ;;; PARAMS updating
  999. (defun cider--update-do-prompt (params)
  1000. "Update :do-prompt in PARAMS."
  1001. (cond ((equal params '(4)) (list :edit-jack-in-command t))
  1002. ((equal params '(16)) (list :do-prompt t))
  1003. (t params)))
  1004. (defun cider--update-project-dir (params)
  1005. "Update :project-dir in PARAMS."
  1006. (let* ((params (cider--update-do-prompt params))
  1007. (proj-dir (if (plist-get params :do-prompt)
  1008. (read-directory-name "Project: "
  1009. (clojure-project-dir (cider-current-dir)))
  1010. (plist-get params :project-dir)))
  1011. (orig-buffer (current-buffer)))
  1012. (if (or (null proj-dir)
  1013. (file-in-directory-p default-directory proj-dir))
  1014. (plist-put params :project-dir
  1015. (or proj-dir
  1016. (clojure-project-dir (cider-current-dir))))
  1017. ;; If proj-dir is not a parent of default-directory, transfer all local
  1018. ;; variables and hack dir-local variables into a temporary buffer and keep
  1019. ;; that buffer within `params` for the later use by other --update-
  1020. ;; functions. The context buffer should not be used outside of the param
  1021. ;; initialization pipeline. Therefore, we don't bother with making it
  1022. ;; unique or killing it anywhere.
  1023. (let ((context-buf-name " *cider-context-buffer*"))
  1024. (when (get-buffer context-buf-name)
  1025. (kill-buffer context-buf-name))
  1026. (with-current-buffer (get-buffer-create context-buf-name)
  1027. (dolist (pair (buffer-local-variables orig-buffer))
  1028. (pcase pair
  1029. (`(,name . ,value) ;ignore unbound variables
  1030. (ignore-errors (set (make-local-variable name) value))))
  1031. (setq-local buffer-file-name nil))
  1032. (let ((default-directory proj-dir))
  1033. (hack-dir-local-variables-non-file-buffer)
  1034. (thread-first params
  1035. (plist-put :project-dir proj-dir)
  1036. (plist-put :--context-buffer (current-buffer)))))))))
  1037. (defun cider--update-cljs-type (params)
  1038. "Update :cljs-repl-type in PARAMS."
  1039. (with-current-buffer (or (plist-get params :--context-buffer)
  1040. (current-buffer))
  1041. (let ((params (cider--update-do-prompt params))
  1042. (inferred-type (or (plist-get params :cljs-repl-type)
  1043. cider-default-cljs-repl)))
  1044. (plist-put params :cljs-repl-type
  1045. (if (plist-get params :do-prompt)
  1046. (cider-select-cljs-repl inferred-type)
  1047. (or inferred-type
  1048. (cider-select-cljs-repl)))))))
  1049. (defcustom cider-edit-jack-in-command nil
  1050. "When truthy allow the user to edit the command."
  1051. :type 'boolean
  1052. :safe #'booleanp
  1053. :version '(cider . "0.22.0"))
  1054. (defun cider--update-jack-in-cmd (params)
  1055. "Update :jack-in-cmd key in PARAMS."
  1056. (let* ((params (cider--update-do-prompt params))
  1057. (project-dir (plist-get params :project-dir))
  1058. (project-type (cider-project-type project-dir))
  1059. (command (cider-jack-in-command project-type))
  1060. (command-resolved (cider-jack-in-resolve-command project-type))
  1061. (command-global-opts (cider-jack-in-global-options project-type))
  1062. (command-params (cider-jack-in-params project-type)))
  1063. (if command-resolved
  1064. (with-current-buffer (or (plist-get params :--context-buffer)
  1065. (current-buffer))
  1066. (let* ((command-params (if (plist-get params :do-prompt)
  1067. (read-string (format "nREPL server command: %s " command-params)
  1068. command-params)
  1069. command-params))
  1070. (cmd-params (if cider-inject-dependencies-at-jack-in
  1071. (cider-inject-jack-in-dependencies command-global-opts command-params project-type)
  1072. command-params)))
  1073. (if (or project-dir cider-allow-jack-in-without-project)
  1074. (when (or project-dir
  1075. (eq cider-allow-jack-in-without-project t)
  1076. (and (null project-dir)
  1077. (eq cider-allow-jack-in-without-project 'warn)
  1078. (y-or-n-p "Are you sure you want to run `cider-jack-in' without a Clojure project? ")))
  1079. (let ((cmd (format "%s %s" command-resolved cmd-params)))
  1080. (plist-put params :jack-in-cmd (if (or cider-edit-jack-in-command
  1081. (plist-get params :edit-jack-in-command))
  1082. (read-string "jack-in command: " cmd t)
  1083. cmd))))
  1084. (user-error "`cider-jack-in' is not allowed without a Clojure project"))))
  1085. (user-error "The %s executable isn't on your `exec-path'" command))))
  1086. (defun cider--update-host-port (params)
  1087. "Update :host and :port in PARAMS."
  1088. (with-current-buffer (or (plist-get params :--context-buffer)
  1089. (current-buffer))
  1090. (let* ((params (cider--update-do-prompt params))
  1091. (host (plist-get params :host))
  1092. (port (plist-get params :port))
  1093. (endpoint (if (plist-get params :do-prompt)
  1094. (cider-select-endpoint)
  1095. (if (and host port)
  1096. (cons host port)
  1097. (cider-select-endpoint)))))
  1098. (thread-first params
  1099. (plist-put :host (car endpoint))
  1100. (plist-put :port (cdr endpoint))))))
  1101. (defun cider--update-cljs-init-function (params)
  1102. "Update PARAMS :repl-init-function for cljs connections."
  1103. (with-current-buffer (or (plist-get params :--context-buffer)
  1104. (current-buffer))
  1105. (let ((cljs-type (plist-get params :cljs-repl-type)))
  1106. (plist-put params :repl-init-function
  1107. (lambda ()
  1108. (cider--check-cljs cljs-type)
  1109. ;; FIXME: ideally this should be done in the state handler
  1110. (setq-local cider-cljs-repl-type cljs-type)
  1111. (cider-nrepl-send-request
  1112. (list "op" "eval"
  1113. "ns" (cider-current-ns)
  1114. "code" (cider-cljs-repl-form cljs-type))
  1115. (cider-repl-handler (current-buffer)))
  1116. (when (and (buffer-live-p nrepl-server-buffer)
  1117. cider-offer-to-open-cljs-app-in-browser)
  1118. (cider--offer-to-open-app-in-browser nrepl-server-buffer)))))))
  1119. (defun cider--check-existing-session (params)
  1120. "Ask for confirmation if a session with similar PARAMS already exists.
  1121. If no session exists or user chose to proceed, return PARAMS. If the user
  1122. canceled the action, signal quit."
  1123. (let* ((proj-dir (plist-get params :project-dir))
  1124. (host (plist-get params :host))
  1125. (port (plist-get params :port))
  1126. (session (seq-find (lambda (ses)
  1127. (let ((ses-params (cider--gather-session-params ses)))
  1128. (and (equal proj-dir (plist-get ses-params :project-dir))
  1129. (or (null port)
  1130. (equal port (plist-get ses-params :port)))
  1131. (or (null host)
  1132. (equal host (plist-get ses-params :host))))))
  1133. (sesman-current-sessions 'CIDER '(project)))))
  1134. (when session
  1135. (unless (y-or-n-p
  1136. (concat
  1137. "A session with the same parameters exists (" (car session) "). "
  1138. "You can connect a sibling instead. Proceed? "))
  1139. (let ((debug-on-quit nil))
  1140. (signal 'quit nil)))))
  1141. params)
  1142. ;;; Aliases
  1143. ;;;###autoload
  1144. (defalias 'cider-jack-in #'cider-jack-in-clj)
  1145. ;;;###autoload
  1146. (define-obsolete-function-alias 'cider-jack-in-clojure 'cider-jack-in-clj "0.22")
  1147. ;;;###autoload
  1148. (define-obsolete-function-alias 'cider-jack-in-clojurescript 'cider-jack-in-cljs "0.22")
  1149. ;;;###autoload
  1150. (defalias 'cider-connect #'cider-connect-clj)
  1151. ;;;###autoload
  1152. (define-obsolete-function-alias 'cider-connect-clojure 'cider-connect-clj "0.22")
  1153. ;;;###autoload
  1154. (define-obsolete-function-alias 'cider-connect-clojurescript 'cider-connect-cljs "0.22")
  1155. ;;;###autoload
  1156. (define-obsolete-function-alias 'cider-connect-sibling-clojure 'cider-connect-sibling-clj "0.22")
  1157. ;;;###autoload
  1158. (define-obsolete-function-alias 'cider-connect-sibling-clojurescript 'cider-connect-sibling-cljs "0.22")
  1159. ;;; Helpers
  1160. (defun cider-current-host ()
  1161. "Retrieve the current host."
  1162. (or (when (stringp buffer-file-name)
  1163. (file-remote-p buffer-file-name 'host))
  1164. "localhost"))
  1165. (defun cider-select-endpoint ()
  1166. "Interactively select the host and port to connect to."
  1167. (dolist (endpoint cider-known-endpoints)
  1168. (unless (stringp (or (nth 2 endpoint)
  1169. (nth 1 endpoint)))
  1170. (user-error "The port for %s in `cider-known-endpoints' should be a string"
  1171. (nth 0 endpoint))))
  1172. (let* ((ssh-hosts (cider--ssh-hosts))
  1173. (hosts (seq-uniq (append (when cider-host-history
  1174. ;; history elements are strings of the form "host:port"
  1175. (list (split-string (car cider-host-history) ":")))
  1176. (list (list (cider-current-host)))
  1177. cider-known-endpoints
  1178. ssh-hosts
  1179. ;; always add localhost
  1180. '(("localhost")))))
  1181. (sel-host (cider--completing-read-host hosts))
  1182. (host (car sel-host))
  1183. (port (or (cadr sel-host)
  1184. (cider--completing-read-port host (cider--infer-ports host ssh-hosts)))))
  1185. (cons host port)))
  1186. (defun cider--ssh-hosts ()
  1187. "Retrieve all ssh host from local configuration files."
  1188. (seq-map (lambda (s) (list (replace-regexp-in-string ":$" "" s)))
  1189. ;; `tramp-completion-mode' is obsoleted in 26
  1190. (cl-progv (if (version< emacs-version "26")
  1191. '(tramp-completion-mode)
  1192. '(non-essential)) '(t)
  1193. (tramp-completion-handle-file-name-all-completions "" "/ssh:"))))
  1194. (defun cider--completing-read-host (hosts)
  1195. "Interactively select host from HOSTS.
  1196. Each element in HOSTS is one of: (host), (host port) or (label host port).
  1197. Return a list of the form (HOST PORT), where PORT can be nil."
  1198. (let* ((hosts (cider-join-into-alist hosts))
  1199. (sel-host (completing-read "Host: " hosts nil nil nil
  1200. 'cider-host-history (caar hosts)))
  1201. (host (or (cdr (assoc sel-host hosts)) (list sel-host))))
  1202. ;; remove the label
  1203. (if (= 3 (length host)) (cdr host) host)))
  1204. (defun cider--tramp-file-name (vec)
  1205. "A simple compatibility wrapper around `make-tramp-file-name'.
  1206. Tramp version starting 26.1 is using a `cl-defstruct' rather than vanilla VEC."
  1207. (if (version< emacs-version "26.1")
  1208. vec
  1209. (with-no-warnings
  1210. (make-tramp-file-name :method (elt vec 0)
  1211. :host (elt vec 2)))))
  1212. (defcustom cider-infer-remote-nrepl-ports nil
  1213. "When true, cider will use ssh to try to infer nREPL ports on remote hosts."
  1214. :type 'boolean
  1215. :safe #'booleanp
  1216. :package-version '(cider . "0.19.0"))
  1217. (defun cider--infer-ports (host ssh-hosts)
  1218. "Infer nREPL ports on HOST.
  1219. Return a list of elements of the form (directory port). SSH-HOSTS is a list
  1220. of remote SSH hosts."
  1221. (let ((localp (or (nrepl-local-host-p host)
  1222. (not (assoc-string host ssh-hosts)))))
  1223. (if localp
  1224. ;; change dir: current file might be remote
  1225. (let* ((change-dir-p (file-remote-p default-directory))
  1226. (default-directory (if change-dir-p "~/" default-directory)))
  1227. (cider-locate-running-nrepl-ports (unless change-dir-p default-directory)))
  1228. (when cider-infer-remote-nrepl-ports
  1229. (let ((vec (vector "sshx" nil host "" nil))
  1230. ;; change dir: user might want to connect to a different remote
  1231. (dir (when (file-remote-p default-directory)
  1232. (with-parsed-tramp-file-name default-directory cur
  1233. (when (string= cur-host host) default-directory)))))
  1234. (tramp-maybe-open-connection (cider--tramp-file-name vec))
  1235. (with-current-buffer (tramp-get-connection-buffer (cider--tramp-file-name vec))
  1236. (cider-locate-running-nrepl-ports dir)))))))
  1237. (defun cider--completing-read-port (host ports)
  1238. "Interactively select port for HOST from PORTS."
  1239. (let* ((ports (cider-join-into-alist ports))
  1240. (sel-port (completing-read (format "Port for %s: " host) ports
  1241. nil nil nil nil (caar ports)))
  1242. (port (or (cdr (assoc sel-port ports)) sel-port))
  1243. (port (if (listp port) (cadr port) port)))
  1244. (if (stringp port) (string-to-number port) port)))
  1245. (defun cider-locate-running-nrepl-ports (&optional dir)
  1246. "Locate ports of running nREPL servers.
  1247. When DIR is non-nil also look for nREPL port files in DIR. Return a list
  1248. of list of the form (project-dir port)."
  1249. (let* ((paths (cider--running-nrepl-paths))
  1250. (proj-ports (mapcar (lambda (d)
  1251. (when-let* ((port (and d (nrepl-extract-port (cider--file-path d)))))
  1252. (list (file-name-nondirectory (directory-file-name d)) port)))
  1253. (cons (clojure-project-dir dir) paths))))
  1254. (seq-uniq (delq nil proj-ports))))
  1255. (defun cider--running-nrepl-paths ()
  1256. "Retrieve project paths of running nREPL servers.
  1257. Use `cider-ps-running-nrepls-command' and `cider-ps-running-nrepl-path-regexp-list'."
  1258. (let (paths)
  1259. (with-temp-buffer
  1260. (insert (shell-command-to-string cider-ps-running-nrepls-command))
  1261. (dolist (regexp cider-ps-running-nrepl-path-regexp-list)
  1262. (goto-char 1)
  1263. (while (re-search-forward regexp nil t)
  1264. (setq paths (cons (match-string 1) paths)))))
  1265. (seq-uniq paths)))
  1266. (defun cider--identify-buildtools-present (&optional project-dir)
  1267. "Identify build systems present by their build files in PROJECT-DIR.
  1268. PROJECT-DIR defaults to current project."
  1269. (let* ((default-directory (or project-dir (clojure-project-dir (cider-current-dir))))
  1270. (build-files '((lein . "project.clj")
  1271. (boot . "build.boot")
  1272. (clojure-cli . "deps.edn")
  1273. (shadow-cljs . "shadow-cljs.edn")
  1274. (gradle . "build.gradle")
  1275. (gradle . "build.gradle.kts"))))
  1276. (delq nil
  1277. (mapcar (lambda (candidate)
  1278. (when (file-exists-p (cdr candidate))
  1279. (car candidate)))
  1280. build-files))))
  1281. (defun cider-project-type (&optional project-dir)
  1282. "Determine the type of the project in PROJECT-DIR.
  1283. When multiple project file markers are present, check for a preferred build
  1284. tool in `cider-preferred-build-tool', otherwise prompt the user to choose.
  1285. PROJECT-DIR defaults to the current project."
  1286. (let* ((choices (cider--identify-buildtools-present project-dir))
  1287. (multiple-project-choices (> (length choices) 1))
  1288. ;; this needs to be a string to be used in `completing-read'
  1289. (default (symbol-name (car choices)))
  1290. ;; `cider-preferred-build-tool' used to be a string prior to CIDER
  1291. ;; 0.18, therefore the need for `cider-maybe-intern'
  1292. (preferred-build-tool (cider-maybe-intern cider-preferred-build-tool)))
  1293. (cond ((and multiple-project-choices
  1294. (member preferred-build-tool choices))
  1295. preferred-build-tool)
  1296. (multiple-project-choices
  1297. (intern
  1298. (completing-read
  1299. (format "Which command should be used (default %s): " default)
  1300. choices nil t nil nil default)))
  1301. (choices
  1302. (car choices))
  1303. ;; TODO: Move this fallback outside the project-type check
  1304. ;; if we're outside a project we fallback to whatever tool
  1305. ;; is specified in `cider-jack-in-default' (normally clojure-cli)
  1306. ;; `cider-jack-in-default' used to be a string prior to CIDER
  1307. ;; 0.18, therefore the need for `cider-maybe-intern'
  1308. (t (cider-maybe-intern cider-jack-in-default)))))
  1309. ;; TODO: Implement a check for command presence over tramp
  1310. (defun cider--resolve-command (command)
  1311. "Find COMMAND in exec path (see variable `exec-path').
  1312. Return nil if not found. In case `default-directory' is non-local we
  1313. assume the command is available."
  1314. (when-let* ((command (or (and (file-remote-p default-directory) command)
  1315. (executable-find command)
  1316. (executable-find (concat command ".bat")))))
  1317. (shell-quote-argument command)))
  1318. ;;;###autoload
  1319. (with-eval-after-load 'clojure-mode
  1320. (define-key clojure-mode-map (kbd "C-c M-x") #'cider)
  1321. (define-key clojure-mode-map (kbd "C-c M-j") #'cider-jack-in-clj)
  1322. (define-key clojure-mode-map (kbd "C-c M-J") #'cider-jack-in-cljs)
  1323. (define-key clojure-mode-map (kbd "C-c M-c") #'cider-connect-clj)
  1324. (define-key clojure-mode-map (kbd "C-c M-C") #'cider-connect-cljs)
  1325. (define-key clojure-mode-map (kbd "C-c C-x") 'cider-start-map)
  1326. (define-key clojure-mode-map (kbd "C-c C-s") 'sesman-map)
  1327. (require 'sesman)
  1328. (sesman-install-menu clojure-mode-map)
  1329. (add-hook 'clojure-mode-hook (lambda () (setq-local sesman-system 'CIDER))))
  1330. (provide 'cider)
  1331. ;;; cider.el ends here