Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

793 lines
32 KiB

4 years ago
  1. ;; cider-util.el --- Common utility functions that don't belong anywhere else -*- 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. ;; This program is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; This program is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. ;; This file is not part of GNU Emacs.
  22. ;;; Commentary:
  23. ;; Common utility functions that don't belong anywhere else.
  24. ;;; Code:
  25. ;; Built-ins
  26. (require 'ansi-color)
  27. (require 'color)
  28. (require 'seq)
  29. (require 'subr-x)
  30. (require 'thingatpt)
  31. ;; clojure-mode and CIDER
  32. (require 'cider-compat)
  33. (require 'clojure-mode)
  34. (require 'nrepl-dict)
  35. (defalias 'cider-pop-back 'pop-tag-mark)
  36. (defcustom cider-font-lock-max-length 10000
  37. "The max length of strings to fontify in `cider-font-lock-as'.
  38. Setting this to nil removes the fontification restriction."
  39. :group 'cider
  40. :type 'boolean
  41. :package-version '(cider . "0.10.0"))
  42. (defun cider-util--hash-keys (hashtable)
  43. "Return a list of keys in HASHTABLE."
  44. (let ((keys '()))
  45. (maphash (lambda (k _v) (setq keys (cons k keys))) hashtable)
  46. keys))
  47. (defun cider-util--clojure-buffers ()
  48. "Return a list of all existing `clojure-mode' buffers."
  49. (seq-filter
  50. (lambda (buffer) (with-current-buffer buffer (derived-mode-p 'clojure-mode)))
  51. (buffer-list)))
  52. (defun cider-current-dir ()
  53. "Return the directory of the current buffer."
  54. (if buffer-file-name
  55. (file-name-directory buffer-file-name)
  56. default-directory))
  57. (defun cider-in-string-p ()
  58. "Return non-nil if point is in a string."
  59. (let ((beg (save-excursion (beginning-of-defun) (point))))
  60. (nth 3 (parse-partial-sexp beg (point)))))
  61. (defun cider-in-comment-p ()
  62. "Return non-nil if point is in a comment."
  63. (let ((beg (save-excursion (beginning-of-defun) (point))))
  64. (nth 4 (parse-partial-sexp beg (point)))))
  65. (defun cider--tooling-file-p (file-name)
  66. "Return t if FILE-NAME is not a 'real' source file.
  67. Currently, only check if the relative file name starts with 'form-init'
  68. which nREPL uses for temporary evaluation file names."
  69. (let ((fname (file-name-nondirectory file-name)))
  70. (string-match-p "^form-init" fname)))
  71. (defun cider--cljc-buffer-p (&optional buffer)
  72. "Return non-nil if the current buffer is visiting a cljc file.
  73. If BUFFER is provided act on that buffer instead."
  74. (with-current-buffer (or buffer (current-buffer))
  75. (or (derived-mode-p 'clojurec-mode))))
  76. ;;; Thing at point
  77. (defun cider--text-or-limits (bounds start end)
  78. "Returns the substring or the bounds of text.
  79. If BOUNDS is non-nil, returns the list (START END) of character
  80. positions. Else returns the substring from START to END."
  81. (funcall (if bounds #'list #'buffer-substring-no-properties)
  82. start end))
  83. (defun cider-defun-at-point (&optional bounds)
  84. "Return the text of the top level sexp at point.
  85. If BOUNDS is non-nil, return a list of its starting and ending position
  86. instead."
  87. (save-excursion
  88. (save-match-data
  89. (end-of-defun)
  90. (let ((end (point)))
  91. (clojure-backward-logical-sexp 1)
  92. (cider--text-or-limits bounds (point) end)))))
  93. (defun cider-ns-form ()
  94. "Retrieve the ns form."
  95. (when (clojure-find-ns)
  96. (save-excursion
  97. (goto-char (match-beginning 0))
  98. (cider-defun-at-point))))
  99. (defun cider-symbol-at-point (&optional look-back)
  100. "Return the name of the symbol at point, otherwise nil.
  101. Ignores the REPL prompt. If LOOK-BACK is non-nil, move backwards trying to
  102. find a symbol if there isn't one at point."
  103. (or (when-let* ((str (thing-at-point 'symbol)))
  104. (unless (text-property-any 0 (length str) 'field 'cider-repl-prompt str)
  105. (substring-no-properties str)))
  106. (when look-back
  107. (save-excursion
  108. (ignore-errors
  109. (while (not (looking-at "\\sw\\|\\s_\\|\\`"))
  110. (forward-sexp -1)))
  111. (cider-symbol-at-point)))))
  112. ;;; sexp navigation
  113. (defun cider-sexp-at-point (&optional bounds)
  114. "Return the sexp at point as a string, otherwise nil.
  115. If BOUNDS is non-nil, return a list of its starting and ending position
  116. instead."
  117. (when-let* ((b (or (and (equal (char-after) ?\()
  118. (member (char-before) '(?\' ?\, ?\@))
  119. ;; hide stuff before ( to avoid quirks with '( etc.
  120. (save-restriction
  121. (narrow-to-region (point) (point-max))
  122. (bounds-of-thing-at-point 'sexp)))
  123. (bounds-of-thing-at-point 'sexp))))
  124. (funcall (if bounds #'list #'buffer-substring-no-properties)
  125. (car b) (cdr b))))
  126. (defun cider-last-sexp (&optional bounds)
  127. "Return the sexp preceding the point.
  128. If BOUNDS is non-nil, return a list of its starting and ending position
  129. instead."
  130. (apply (if bounds #'list #'buffer-substring-no-properties)
  131. (save-excursion
  132. (clojure-backward-logical-sexp 1)
  133. (list (point)
  134. (progn (clojure-forward-logical-sexp 1)
  135. (skip-chars-forward "[:blank:]")
  136. (when (looking-at-p "\n") (forward-char 1))
  137. (point))))))
  138. (defun cider-start-of-next-sexp (&optional skip)
  139. "Move to the start of the next sexp.
  140. Skip any non-logical sexps like ^metadata or #reader macros.
  141. If SKIP is an integer, also skip that many logical sexps first.
  142. Can only error if SKIP is non-nil."
  143. (while (clojure--looking-at-non-logical-sexp)
  144. (forward-sexp 1))
  145. (when (and skip (> skip 0))
  146. (dotimes (_ skip)
  147. (forward-sexp 1)
  148. (cider-start-of-next-sexp))))
  149. (defun cider-second-sexp-in-list ()
  150. "Return the second sexp in the list at point."
  151. (condition-case nil
  152. (save-excursion
  153. (backward-up-list)
  154. (forward-char)
  155. (forward-sexp 2)
  156. (cider-sexp-at-point))
  157. (error nil)))
  158. ;;; Text properties
  159. (defun cider-maybe-intern (name)
  160. "If NAME is a symbol, return it; otherwise, intern it."
  161. (if (symbolp name) name (intern name)))
  162. (defun cider-intern-keys (plist)
  163. "Copy PLIST, with any non-symbol keys replaced with symbols."
  164. (when plist
  165. (cons (cider-maybe-intern (pop plist))
  166. (cons (pop plist) (cider-intern-keys plist)))))
  167. (defmacro cider-propertize-region (props &rest body)
  168. "Execute BODY and add PROPS to all the inserted text.
  169. More precisely, PROPS are added to the region between the point's
  170. positions before and after executing BODY."
  171. (declare (indent 1)
  172. (debug (sexp body)))
  173. (let ((start (make-symbol "start")))
  174. `(let ((,start (point)))
  175. (prog1 (progn ,@body)
  176. (add-text-properties ,start (point) ,props)))))
  177. (put 'cider-propertize-region 'lisp-indent-function 1)
  178. (defun cider-property-bounds (prop)
  179. "Return the the positions of the previous and next change to PROP.
  180. PROP is the name of a text property."
  181. (let ((end (next-single-char-property-change (point) prop)))
  182. (list (previous-single-char-property-change end prop) end)))
  183. (defun cider-insert (text &optional face break more-text)
  184. "Insert TEXT with FACE, optionally followed by a line BREAK and MORE-TEXT."
  185. (insert (if face (propertize text 'font-lock-face face) text))
  186. (when more-text (insert more-text))
  187. (when break (insert "\n")))
  188. ;;; Hooks
  189. (defun cider-run-chained-hook (hook arg)
  190. "Like `run-hook-with-args' but pass intermediate return values through.
  191. HOOK is a name of a hook (a symbol). You can use `add-hook' or
  192. `remove-hook' to add functions to this variable. ARG is passed to first
  193. function. Its return value is passed to the second function and so forth
  194. till all functions are called or one of them returns nil. Return the value
  195. return by the last called function."
  196. (let ((functions (copy-sequence (symbol-value hook))))
  197. (while (and functions arg)
  198. (if (eq (car functions) t)
  199. ;; global value of the hook
  200. (let ((functions (default-value hook)))
  201. (while (and functions arg)
  202. (setq arg (funcall (car functions) arg))
  203. (setq functions (cdr functions))))
  204. (setq arg (funcall (car functions) arg)))
  205. (setq functions (cdr functions)))
  206. arg))
  207. ;;; Font lock
  208. (defalias 'cider--font-lock-ensure
  209. (if (fboundp 'font-lock-ensure)
  210. #'font-lock-ensure
  211. (with-no-warnings
  212. (lambda (&optional _beg _end)
  213. (when font-lock-mode
  214. (font-lock-fontify-buffer))))))
  215. (defalias 'cider--font-lock-flush
  216. (if (fboundp 'font-lock-flush)
  217. #'font-lock-flush
  218. (with-no-warnings
  219. (lambda (&optional _beg _end)
  220. (when font-lock-mode
  221. (font-lock-fontify-buffer))))))
  222. (defvar cider--mode-buffers nil
  223. "A list of buffers for different major modes.")
  224. (defun cider--make-buffer-for-mode (mode)
  225. "Return a temp buffer using `major-mode' MODE.
  226. This buffer is not designed to display anything to the user. For that, use
  227. `cider-make-popup-buffer' instead."
  228. (setq cider--mode-buffers (seq-filter (lambda (x) (buffer-live-p (cdr x)))
  229. cider--mode-buffers))
  230. (or (cdr (assq mode cider--mode-buffers))
  231. (let ((b (generate-new-buffer (format " *cider-temp %s*" mode))))
  232. (push (cons mode b) cider--mode-buffers)
  233. (with-current-buffer b
  234. ;; suppress major mode hooks as we care only about their font-locking
  235. ;; otherwise modes like whitespace-mode and paredit might interfere
  236. (setq-local delay-mode-hooks t)
  237. (setq delayed-mode-hooks nil)
  238. (funcall mode))
  239. b)))
  240. (defun cider-ansi-color-string-p (string)
  241. "Return non-nil if STRING is an ANSI string."
  242. (string-match "^\\[" string))
  243. (defun cider-font-lock-as (mode string)
  244. "Use MODE to font-lock the STRING."
  245. (let ((string (if (cider-ansi-color-string-p string)
  246. (substring-no-properties (ansi-color-apply string))
  247. string)))
  248. (if (or (null cider-font-lock-max-length)
  249. (< (length string) cider-font-lock-max-length))
  250. (with-current-buffer (cider--make-buffer-for-mode mode)
  251. (erase-buffer)
  252. (insert string)
  253. (font-lock-fontify-region (point-min) (point-max))
  254. (buffer-string))
  255. string)))
  256. (defun cider-font-lock-region-as (mode beg end &optional buffer)
  257. "Use MODE to font-lock text between BEG and END.
  258. Unless you specify a BUFFER it will default to the current one."
  259. (with-current-buffer (or buffer (current-buffer))
  260. (let ((text (buffer-substring beg end)))
  261. (delete-region beg end)
  262. (goto-char beg)
  263. (insert (cider-font-lock-as mode text)))))
  264. (defun cider-font-lock-as-clojure (string)
  265. "Font-lock STRING as Clojure code."
  266. (cider-font-lock-as 'clojure-mode string))
  267. ;; Button allowing use of `font-lock-face', ignoring any inherited `face'
  268. (define-button-type 'cider-plain-button
  269. 'face nil)
  270. (defun cider-add-face (regexp face &optional foreground-only sub-expr object)
  271. "Propertize all occurrences of REGEXP with FACE.
  272. If FOREGROUND-ONLY is non-nil, change only the foreground of matched
  273. regions. SUB-EXPR is a sub-expression of REGEXP to be
  274. propertized (defaults to 0). OBJECT is an object to be
  275. propertized (defaults to current buffer)."
  276. (setq sub-expr (or sub-expr 0))
  277. (when (and regexp face)
  278. (let ((beg 0)
  279. (end 0))
  280. (with-current-buffer (or (and (bufferp object) object)
  281. (current-buffer))
  282. (while (if (stringp object)
  283. (string-match regexp object end)
  284. (re-search-forward regexp nil t))
  285. (setq beg (match-beginning sub-expr)
  286. end (match-end sub-expr))
  287. (if foreground-only
  288. (let ((face-spec (list (cons 'foreground-color
  289. (face-attribute face :foreground nil t)))))
  290. (font-lock-prepend-text-property beg end 'face face-spec object))
  291. (put-text-property beg end 'face face object)))))))
  292. ;;; Colors
  293. (defun cider-scale-background-color ()
  294. "Scale the current background color to get a slighted muted version."
  295. (let ((color (frame-parameter nil 'background-color))
  296. (darkp (eq (frame-parameter nil 'background-mode) 'dark)))
  297. (unless (equal "unspecified-bg" color)
  298. (color-lighten-name color (if darkp 5 -5)))))
  299. (autoload 'pkg-info-version-info "pkg-info.el")
  300. (defvar cider-version)
  301. (defvar cider-codename)
  302. (defun cider--version ()
  303. "Retrieve CIDER's version.
  304. A codename is added to stable versions."
  305. (let ((version (condition-case nil
  306. (pkg-info-version-info 'cider)
  307. (error cider-version))))
  308. (if (string-match-p "-snapshot" cider-version)
  309. version
  310. (format "%s (%s)" version cider-codename))))
  311. ;;; Strings
  312. (defun cider-join-into-alist (candidates &optional separator)
  313. "Make an alist from CANDIDATES.
  314. The keys are the elements joined with SEPARATOR and values are the original
  315. elements. Useful for `completing-read' when candidates are complex
  316. objects."
  317. (mapcar (lambda (el)
  318. (if (listp el)
  319. (cons (string-join el (or separator ":")) el)
  320. (cons el el)))
  321. candidates))
  322. (defun cider-add-to-alist (symbol car cadr)
  323. "Add '(CAR CADR) to the alist stored in SYMBOL.
  324. If CAR already corresponds to an entry in the alist, destructively replace
  325. the entry's second element with CADR.
  326. This can be used, for instance, to update the version of an injected
  327. plugin or dependency with:
  328. (cider-add-to-alist 'cider-jack-in-lein-plugins
  329. \"plugin/artifact-name\" \"THE-NEW-VERSION\")"
  330. (let ((alist (symbol-value symbol)))
  331. (if-let* ((cons (assoc car alist)))
  332. (setcdr cons (list cadr))
  333. (set symbol (cons (list car cadr) alist)))))
  334. (defun cider-namespace-qualified-p (sym)
  335. "Return t if SYM is namespace-qualified."
  336. (string-match-p "[^/]+/" sym))
  337. (defvar cider-version)
  338. (defconst cider-manual-url "https://docs.cider.mx/cider/%s"
  339. "The URL to CIDER's manual.")
  340. (defun cider--manual-version ()
  341. "Convert the version to a ReadTheDocs-friendly version."
  342. (if (string-match-p "-snapshot" cider-version)
  343. ""
  344. (concat cider-version "/")))
  345. (defun cider-manual-url ()
  346. "The CIDER manual's url."
  347. (format cider-manual-url (cider--manual-version)))
  348. ;;;###autoload
  349. (defun cider-view-manual ()
  350. "View the manual in your default browser."
  351. (interactive)
  352. (browse-url (cider-manual-url)))
  353. (defun cider--manual-button (label section-id)
  354. "Return a button string that links to the online manual.
  355. LABEL is the displayed string, and SECTION-ID is where it points
  356. to."
  357. (with-temp-buffer
  358. (insert-text-button
  359. label
  360. 'follow-link t
  361. 'action (lambda (&rest _) (interactive)
  362. (browse-url (concat (cider-manual-url)
  363. section-id))))
  364. (buffer-string)))
  365. (defconst cider-refcard-url "https://github.com/clojure-emacs/cider/raw/%s/doc/cider-refcard.pdf"
  366. "The URL to CIDER's refcard.")
  367. (defun cider--github-version ()
  368. "Convert the version to a GitHub-friendly version."
  369. (if (string-match-p "-snapshot" cider-version)
  370. "master"
  371. (concat "v" cider-version)))
  372. (defun cider-refcard-url ()
  373. "The CIDER manual's url."
  374. (format cider-refcard-url (cider--github-version)))
  375. (defun cider-view-refcard ()
  376. "View the refcard in your default browser."
  377. (interactive)
  378. (browse-url (cider-refcard-url)))
  379. (defconst cider-report-bug-url "https://github.com/clojure-emacs/cider/issues/new"
  380. "The URL to report a CIDER issue.")
  381. (defun cider-report-bug ()
  382. "Report a bug in your default browser."
  383. (interactive)
  384. (browse-url cider-report-bug-url))
  385. (defun cider--project-name (dir)
  386. "Extracts a project name from DIR, possibly nil.
  387. The project name is the final component of DIR if not nil."
  388. (when dir
  389. (file-name-nondirectory (directory-file-name dir))))
  390. ;;; Vectors
  391. (defun cider--deep-vector-to-list (x)
  392. "Convert vectors in X to lists.
  393. If X is a sequence, return a list of `cider--deep-vector-to-list' applied to
  394. each of its elements.
  395. Any other value is just returned."
  396. (if (sequencep x)
  397. (mapcar #'cider--deep-vector-to-list x)
  398. x))
  399. ;;; Help mode
  400. ;; Same as https://github.com/emacs-mirror/emacs/blob/86d083438dba60dc00e9e96414bf7e832720c05a/lisp/help-mode.el#L355
  401. ;; the original function uses some buffer local variables, but the buffer used
  402. ;; is not configurable. It defaults to (help-buffer)
  403. (defun cider--help-setup-xref (item interactive-p buffer)
  404. "Invoked from commands using the \"*Help*\" buffer to install some xref info.
  405. ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
  406. buffer after following a reference. INTERACTIVE-P is non-nil if the
  407. calling command was invoked interactively. In this case the stack of
  408. items for help buffer \"back\" buttons is cleared. Use BUFFER for the
  409. buffer local variables.
  410. This should be called very early, before the output buffer is cleared,
  411. because we want to record the \"previous\" position of point so we can
  412. restore it properly when going back."
  413. (with-current-buffer buffer
  414. (when help-xref-stack-item
  415. (push (cons (point) help-xref-stack-item) help-xref-stack)
  416. (setq help-xref-forward-stack nil))
  417. (when interactive-p
  418. (let ((tail (nthcdr 10 help-xref-stack)))
  419. ;; Truncate the stack.
  420. (if tail (setcdr tail nil))))
  421. (setq help-xref-stack-item item)))
  422. (defcustom cider-doc-xref-regexp "`\\(.*?\\)`"
  423. "The regexp used to search Clojure vars in doc buffers."
  424. :type 'regexp
  425. :safe #'stringp
  426. :group 'cider
  427. :package-version '(cider . "0.13.0"))
  428. (defun cider--find-symbol-xref ()
  429. "Parse and return the first clojure symbol in current buffer.
  430. Use `cider-doc-xref-regexp' for the search. Set match data and return a
  431. string of the Clojure symbol. Return nil if there are no more matches in
  432. the buffer."
  433. (when (re-search-forward cider-doc-xref-regexp nil t)
  434. (match-string 1)))
  435. (declare-function cider-doc-lookup "cider-doc")
  436. (declare-function cider--eldoc-remove-dot "cider-eldoc")
  437. ;; Taken from: https://github.com/emacs-mirror/emacs/blob/65c8c7cb96c14f9c6accd03cc8851b5a3459049e/lisp/help-mode.el#L551-L565
  438. (defun cider--make-back-forward-xrefs (&optional buffer)
  439. "Insert special references `back' and `forward', as in `help-make-xrefs'.
  440. Optional argument BUFFER is the buffer in which to insert references.
  441. Default is current buffer."
  442. (with-current-buffer (or buffer (current-buffer))
  443. (insert "\n")
  444. (when (or help-xref-stack help-xref-forward-stack)
  445. (insert "\n"))
  446. ;; Make a back-reference in this buffer if appropriate.
  447. (when help-xref-stack
  448. (help-insert-xref-button help-back-label 'help-back
  449. (current-buffer)))
  450. ;; Make a forward-reference in this buffer if appropriate.
  451. (when help-xref-forward-stack
  452. (when help-xref-stack
  453. (insert "\t"))
  454. (help-insert-xref-button help-forward-label 'help-forward
  455. (current-buffer)))
  456. (when (or help-xref-stack help-xref-forward-stack)
  457. (insert "\n"))))
  458. ;; Similar to https://github.com/emacs-mirror/emacs/blob/65c8c7cb96c14f9c6accd03cc8851b5a3459049e/lisp/help-mode.el#L404
  459. (defun cider--doc-make-xrefs ()
  460. "Parse and hyperlink documentation cross-references in current buffer.
  461. Find cross-reference information in a buffer and activate such cross
  462. references for selection with `help-xref'. Cross-references are parsed
  463. using `cider--find-symbol-xref'.
  464. Special references `back' and `forward' are made to go back and forth
  465. through a stack of help buffers. Variables `help-back-label' and
  466. `help-forward-label' specify the text for that."
  467. (interactive "b")
  468. ;; parse the docstring and create xrefs for symbols
  469. (save-excursion
  470. (goto-char (point-min))
  471. (let ((symbol))
  472. (while (setq symbol (cider--find-symbol-xref))
  473. (replace-match "")
  474. (insert-text-button symbol
  475. 'type 'help-xref
  476. 'help-function (apply-partially #'cider-doc-lookup
  477. (cider--eldoc-remove-dot symbol))))))
  478. (cider--make-back-forward-xrefs))
  479. ;;; Words of inspiration
  480. (defun cider-user-first-name ()
  481. "Find the current user's first name."
  482. (let ((name (if (string= (user-full-name) "")
  483. (user-login-name)
  484. (user-full-name))))
  485. (string-match "^[^ ]*" name)
  486. (capitalize (match-string 0 name))))
  487. (defvar cider-words-of-inspiration
  488. `("The best way to predict the future is to invent it. -Alan Kay"
  489. "A point of view is worth 80 IQ points. -Alan Kay"
  490. "Lisp isn't a language, it's a building material. -Alan Kay"
  491. "Simple things should be simple, complex things should be possible. -Alan Kay"
  492. "Everything should be as simple as possible, but not simpler. -Albert Einstein"
  493. "Measuring programming progress by lines of code is like measuring aircraft building progress by weight. -Bill Gates"
  494. "Controlling complexity is the essence of computer programming. -Brian Kernighan"
  495. "The unavoidable price of reliability is simplicity. -C.A.R. Hoare"
  496. "You're bound to be unhappy if you optimize everything. -Donald Knuth"
  497. "Simplicity is prerequisite for reliability. -Edsger W. Dijkstra"
  498. "Elegance is not a dispensable luxury but a quality that decides between success and failure. -Edsger W. Dijkstra"
  499. "Deleted code is debugged code. -Jeff Sickel"
  500. "The key to performance is elegance, not battalions of special cases. -Jon Bentley and Doug McIlroy"
  501. "First, solve the problem. Then, write the code. -John Johnson"
  502. "Simplicity is the ultimate sophistication. -Leonardo da Vinci"
  503. "Programming is not about typing... it's about thinking. -Rich Hickey"
  504. "Design is about pulling things apart. -Rich Hickey"
  505. "Programmers know the benefits of everything and the tradeoffs of nothing. -Rich Hickey"
  506. "Code never lies, comments sometimes do. -Ron Jeffries"
  507. "The true delight is in the finding out rather than in the knowing. -Isaac Asimov"
  508. "If paredit is not for you, then you need to become the sort of person that paredit is for. -Phil Hagelberg"
  509. "Express Yourself. -Madonna"
  510. "Put on your red shoes and dance the blues. -David Bowie"
  511. "Do. Or do not. There is no try. -Yoda"
  512. "The enjoyment of one's tools is an essential ingredient of successful work. -Donald E. Knuth"
  513. "Not all those who wander are lost. -J.R.R. Tolkien"
  514. "The best way to learn is to do. -P.R. Halmos"
  515. "If you wish to make an apple pie from scratch, you must first invent the universe. -Carl Sagan"
  516. "Learn the rules like a pro, so you can break them like an artist. -Pablo Picasso"
  517. "The only way of discovering the limits of the possible is to venture a little way past them into the impossible. -Arthur C. Clarke"
  518. "Don't wish it were easier. Wish you were better. -Jim Rohn"
  519. "One chord is fine. Two chords is pushing it. Three chords and you're into jazz. -Lou Reed"
  520. "We are all apprentices in a craft where no one ever becomes a master. -Ernest Hemingway"
  521. "A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away. -Antoine de Saint-Exupery"
  522. "Clojure isn't a language, it's a building material."
  523. "Think big!"
  524. "Think bold!"
  525. "Think fun!"
  526. "Code big!"
  527. "Code bold!"
  528. "Code fun!"
  529. "Take this REPL, fellow hacker, and may it serve you well."
  530. "Let the hacking commence!"
  531. "Hacks and glory await!"
  532. "Hack and be merry!"
  533. "Your hacking starts... NOW!"
  534. "May the Source be with you!"
  535. "May the Source shine upon thy REPL!"
  536. "Code long and prosper!"
  537. "Happy hacking!"
  538. "nREPL server is up, CIDER REPL is online!"
  539. "CIDER REPL operational!"
  540. "Your imagination is the only limit to what you can do with this REPL!"
  541. "This REPL is yours to command!"
  542. "Fame is but a hack away!"
  543. "The REPL is not enough, but it is such a perfect place to start..."
  544. "Keep on codin' in the free world!"
  545. "What we do in the REPL echoes in eternity!"
  546. "Evaluating is believing."
  547. "To infinity... and beyond."
  548. "Showtime!"
  549. "Unfortunately, no one can be told what CIDER is. You have to figure this out yourself."
  550. "Procure a bottle of cider to achieve optimum programming results."
  551. "In parentheses we trust!"
  552. "Write you some Clojure for Great Good!"
  553. "Oh, what a day... what a lovely day!"
  554. "What a day! What cannot be accomplished on such a splendid day!"
  555. "Home is where your REPL is."
  556. "The worst day programming is better than the best day working."
  557. "The only thing worse than a rebel without a cause is a REPL without a clause."
  558. "In the absence of parentheses, chaos prevails."
  559. "One REPL to rule them all, One REPL to find them, One REPL to bring them all, and in parentheses bind them!"
  560. "A blank REPL promotes creativity."
  561. "A blank REPL is infinitely better than a blank cheque."
  562. ,(format "%s, I've a feeling we're not in Kansas anymore."
  563. (cider-user-first-name))
  564. ,(format "%s, this could be the start of a beautiful program."
  565. (cider-user-first-name)))
  566. "Scientifically-proven optimal words of hackerish encouragement.")
  567. (defun cider-random-words-of-inspiration ()
  568. "Select a random entry from `cider-words-of-inspiration'."
  569. (eval (nth (random (length cider-words-of-inspiration))
  570. cider-words-of-inspiration)))
  571. (defvar cider-tips
  572. '("Press <\\[cider-connect]> to connect to a running nREPL server."
  573. "Press <\\[cider-quit]> to quit the current connection."
  574. "Press <\\[cider-view-manual]> to view CIDER's manual."
  575. "Press <\\[cider-view-refcard]> to view CIDER's refcard."
  576. "Press <\\[describe-mode]> to see a list of the keybindings available (this will work in every Emacs buffer)."
  577. "Press <\\[cider-repl-handle-shortcut]> to quickly invoke some REPL command."
  578. "Press <\\[cider-switch-to-last-clojure-buffer]> to switch between the REPL and a Clojure source buffer."
  579. "Press <\\[cider-doc]> to view the documentation for something (e.g. a var, a Java method)."
  580. "Press <\\[cider-find-resource]> to find a resource on the classpath."
  581. "Press <\\[cider-find-var]> to jump to the source of something (e.g. a var, a Java method)."
  582. "Press <\\[cider-selector]> to quickly select a CIDER buffer."
  583. "Press <\\[cider-test-run-ns-tests]> to run the tests for the current namespace."
  584. "Press <\\[cider-test-run-loaded-tests]> to run all loaded tests."
  585. "Press <\\[cider-test-run-project-tests]> to run all tests for the current project."
  586. "Press <\\[cider-apropos]> to look for a symbol by some search string."
  587. "Press <\\[cider-apropos-documentation]> to look for a symbol that has some string in its docstring."
  588. "Press <\\[cider-eval-defun-at-point]> to eval the top-level form at point."
  589. "Press <\\[cider-eval-defun-up-to-point]> to eval the top-level form up to the point."
  590. "Press <\\[cider-eval-sexp-up-to-point]> to eval the current form up to the point."
  591. "Press <\\[cider-eval-sexp-at-point]> to eval the current form around the point."
  592. "Press <\\[cider-eval-sexp-at-point-in-context]> to eval the current form around the point in a user-provided context."
  593. "Press <\\[cider-eval-buffer]> to eval the entire source buffer."
  594. "Press <\\[cider-scratch]> to create a Clojure scratchpad. Pretty handy for prototyping."
  595. "Press <\\[cider-read-and-eval]> to evaluate some Clojure expression directly in the minibuffer."
  596. "Press <\\[cider-drink-a-sip]> to get more CIDER tips."
  597. "Press <\\[cider-browse-ns-all]> to start CIDER's namespace browser."
  598. "Press <\\[cider-classpath]> to start CIDER's classpath browser."
  599. "Press <\\[cider-repl-history]> to start CIDER's REPL input history browser."
  600. "Press <\\[cider-macroexpand-1]> to expand the preceding macro."
  601. "Press <\\[cider-inspect]> to inspect the preceding expression's result."
  602. "Press <C-u \\[cider-inspect]> to inspect the defun at point's result."
  603. "Press <C-u C-u \\[cider-inspect]> to read Clojure code from the minibuffer and inspect its result."
  604. "Press <\\[cider-ns-refresh]> to reload modified and unloaded namespaces."
  605. "You can define Clojure functions to be called before and after `cider-ns-refresh' (see `cider-ns-refresh-before-fn' and `cider-ns-refresh-after-fn'."
  606. "Press <\\[cider-describe-connection]> to view information about the connection."
  607. "Press <\\[cider-undef]> to undefine a symbol in the current namespace."
  608. "Press <\\[cider-interrupt]> to interrupt an ongoing evaluation."
  609. "Use <M-x customize-group RET cider RET> to see every possible setting you can customize."
  610. "Use <M-x customize-group RET cider-repl RET> to see every possible REPL setting you can customize."
  611. "Enable `eldoc-mode' to display function & method signatures in the minibuffer."
  612. "Enable `cider-enlighten-mode' to display the locals of a function when it's executed."
  613. "Use <\\[cider-close-ancillary-buffers]> to close all ancillary buffers created by CIDER (e.g. *cider-doc*)."
  614. "Exploring CIDER's menu-bar entries is a great way to discover features."
  615. "Keep in mind that some commands don't have a keybinding by default. Explore CIDER!"
  616. "Tweak `cider-repl-prompt-function' to customize your REPL prompt."
  617. "Tweak `cider-eldoc-ns-function' to customize the way namespaces are displayed by eldoc."
  618. "For no middleware, low-tech and reliable namespace reloading use <\\[cider-ns-reload]>."
  619. "Press <\\[cider-load-buffer-and-switch-to-repl-buffer]> to load the current buffer and switch to the REPL buffer afterwards.")
  620. "Some handy CIDER tips."
  621. )
  622. (defun cider-random-tip ()
  623. "Select a random tip from `cider-tips'."
  624. (substitute-command-keys (nth (random (length cider-tips)) cider-tips)))
  625. (defun cider-drink-a-sip ()
  626. "Show a random tip."
  627. (interactive)
  628. (message (cider-random-tip)))
  629. (defun cider-column-number-at-pos (pos)
  630. "Analog to `line-number-at-pos'.
  631. Return buffer column number at position POS."
  632. (save-excursion
  633. (goto-char pos)
  634. ;; we have to adjust the column number by 1 to account for the fact
  635. ;; that Emacs starts counting columns from 0 and Clojure from 1
  636. (1+ (current-column))))
  637. (defun cider-propertize (text kind)
  638. "Propertize TEXT as KIND.
  639. KIND can be the symbols `ns', `var', `emph', `fn', or a face name."
  640. (propertize text 'face (pcase kind
  641. (`fn 'font-lock-function-name-face)
  642. (`var 'font-lock-variable-name-face)
  643. (`ns 'font-lock-type-face)
  644. (`emph 'font-lock-keyword-face)
  645. (face face))))
  646. (defun cider--menu-add-help-strings (menu-list)
  647. "Add a :help entries to items in MENU-LIST."
  648. (mapcar (lambda (x)
  649. (cond
  650. ((listp x) (cider--menu-add-help-strings x))
  651. ((and (vectorp x)
  652. (not (plist-get (append x nil) :help))
  653. (functionp (elt x 1)))
  654. (vconcat x `[:help ,(documentation (elt x 1))]))
  655. (t x)))
  656. menu-list))
  657. (defcustom cider-jdk-src-paths '("/usr/lib/jvm/openjdk-8/src.zip")
  658. "Used by `cider-stacktrace-navigate'.
  659. Zip/jar files work, but it's better to extract them and put the directory
  660. paths here. Clojure sources here:
  661. https://repo1.maven.org/maven2/org/clojure/clojure/1.8.0/."
  662. :group 'cider
  663. :package-version '(cider . "0.17.0")
  664. :type '(list string))
  665. (defun cider-resolve-java-class (class)
  666. "Return a path to a Java source file that corresponds to CLASS.
  667. This will be a zip/jar path for archived sources and a normal
  668. file path otherwise."
  669. (when class
  670. (let ((file-name (concat (replace-regexp-in-string "\\." "/" class) ".java")))
  671. (cl-find-if
  672. 'file-exists-p
  673. (mapcar
  674. (lambda (d)
  675. (cond ((file-directory-p d)
  676. (expand-file-name file-name d))
  677. ((and (file-exists-p d)
  678. (member (file-name-extension d) '("jar" "zip")))
  679. (format "zip:file:%s!/%s" d file-name))
  680. (t (error "Unexpected archive: %s" d))))
  681. cider-jdk-src-paths)))))
  682. (provide 'cider-util)
  683. ;;; cider-util.el ends here