Klimi's new dotfiles with stow.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

375 строки
16 KiB

4 лет назад
  1. ;;; cider-common.el --- Common use functions -*- lexical-binding: t; -*-
  2. ;; Copyright © 2015-2019 Artur Malabarba
  3. ;; Author: Artur Malabarba <bruce.connor.am@gmail.com>
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;; Common functions that are useful in both Clojure buffers and REPL
  16. ;; buffers.
  17. ;;; Code:
  18. (require 'subr-x)
  19. (require 'cider-compat)
  20. (require 'nrepl-dict)
  21. (require 'cider-util)
  22. (require 'etags) ; for find-tags-marker-ring
  23. (require 'tramp)
  24. (defcustom cider-prompt-for-symbol t
  25. "Controls when to prompt for symbol when a command requires one.
  26. When non-nil, always prompt, and use the symbol at point as the default
  27. value at the prompt.
  28. When nil, attempt to use the symbol at point for the command, and only
  29. prompt if that throws an error."
  30. :type '(choice (const :tag "always" t)
  31. (const :tag "dwim" nil))
  32. :group 'cider
  33. :package-version '(cider . "0.9.0"))
  34. (defcustom cider-special-mode-truncate-lines t
  35. "If non-nil, contents of CIDER's special buffers will be line-truncated.
  36. Should be set before loading CIDER."
  37. :type 'boolean
  38. :group 'cider
  39. :package-version '(cider . "0.15.0"))
  40. (defun cider--should-prompt-for-symbol (&optional invert)
  41. "Return the value of the variable `cider-prompt-for-symbol'.
  42. Optionally invert the value, if INVERT is truthy."
  43. (if invert (not cider-prompt-for-symbol) cider-prompt-for-symbol))
  44. (defun cider-prompt-for-symbol-function (&optional invert)
  45. "Prompt for symbol if funcall `cider--should-prompt-for-symbol' is truthy.
  46. Otherwise attempt to use the symbol at point for the command, and only
  47. prompt if that throws an error.
  48. INVERT is used to invert the semantics of the function `cider--should-prompt-for-symbol'."
  49. (if (cider--should-prompt-for-symbol invert)
  50. #'cider-read-symbol-name
  51. #'cider-try-symbol-at-point))
  52. (defun cider--kw-to-symbol (kw)
  53. "Convert the keyword KW to a symbol."
  54. (when kw
  55. (replace-regexp-in-string "\\`:+" "" kw)))
  56. ;;; Minibuffer
  57. (defvar cider-minibuffer-history '()
  58. "History list of expressions read from the minibuffer.")
  59. (defvar cider-minibuffer-map
  60. (let ((map (make-sparse-keymap)))
  61. (set-keymap-parent map minibuffer-local-map)
  62. (define-key map (kbd "TAB") #'complete-symbol)
  63. (define-key map (kbd "M-TAB") #'complete-symbol)
  64. map)
  65. "Minibuffer keymap used for reading Clojure expressions.")
  66. (declare-function cider-complete-at-point "cider-completion")
  67. (declare-function cider-eldoc "cider-eldoc")
  68. (defun cider-read-from-minibuffer (prompt &optional value)
  69. "Read a string from the minibuffer, prompting with PROMPT.
  70. If VALUE is non-nil, it is inserted into the minibuffer as initial-input.
  71. PROMPT need not end with \": \". If it doesn't, VALUE is displayed on the
  72. prompt as a default value (used if the user doesn't type anything) and is
  73. not used as initial input (input is left empty)."
  74. (minibuffer-with-setup-hook
  75. (lambda ()
  76. (set-syntax-table clojure-mode-syntax-table)
  77. (add-hook 'completion-at-point-functions
  78. #'cider-complete-at-point nil t)
  79. (setq-local eldoc-documentation-function #'cider-eldoc)
  80. (run-hooks 'eval-expression-minibuffer-setup-hook))
  81. (let* ((has-colon (string-match ": \\'" prompt))
  82. (input (read-from-minibuffer (cond
  83. (has-colon prompt)
  84. (value (format "%s (default %s): " prompt value))
  85. (t (format "%s: " prompt)))
  86. (when has-colon value) ; initial-input
  87. cider-minibuffer-map nil
  88. 'cider-minibuffer-history
  89. (unless has-colon value)))) ; default-value
  90. (if (and (equal input "") value (not has-colon))
  91. value
  92. input))))
  93. (defun cider-read-symbol-name (prompt callback)
  94. "Read a symbol name using PROMPT with a default of the one at point.
  95. Use CALLBACK as the completing read var callback."
  96. (funcall callback (cider-read-from-minibuffer
  97. prompt
  98. ;; if the thing at point is a keyword we treat it as symbol
  99. (cider--kw-to-symbol (cider-symbol-at-point 'look-back)))))
  100. (defun cider-try-symbol-at-point (prompt callback)
  101. "Call CALLBACK with symbol at point.
  102. On failure, read a symbol name using PROMPT and call CALLBACK with that."
  103. (condition-case nil (funcall callback (cider--kw-to-symbol (cider-symbol-at-point 'look-back)))
  104. ('error (funcall callback (cider-read-from-minibuffer prompt)))))
  105. (declare-function cider-mode "cider-mode")
  106. (defun cider-jump-to (buffer &optional pos other-window)
  107. "Push current point onto marker ring, and jump to BUFFER and POS.
  108. POS can be either a number, a cons, or a symbol.
  109. If a number, it is the character position (the point).
  110. If a cons, it specifies the position as (LINE . COLUMN). COLUMN can be nil.
  111. If a symbol, `cider-jump-to' searches for something that looks like the
  112. symbol's definition in the file.
  113. If OTHER-WINDOW is non-nil don't reuse current window."
  114. (with-no-warnings
  115. (ring-insert find-tag-marker-ring (point-marker)))
  116. (if other-window
  117. (pop-to-buffer buffer 'display-buffer-pop-up-window)
  118. ;; like switch-to-buffer, but reuse existing window if BUFFER is visible
  119. (pop-to-buffer buffer '((display-buffer-reuse-window display-buffer-same-window))))
  120. (with-current-buffer buffer
  121. (widen)
  122. (goto-char (point-min))
  123. (cider-mode +1)
  124. (cond
  125. ;; Line-column specification.
  126. ((consp pos)
  127. (forward-line (1- (or (car pos) 1)))
  128. (if (cdr pos)
  129. (move-to-column (cdr pos))
  130. (back-to-indentation)))
  131. ;; Point specification.
  132. ((numberp pos)
  133. (goto-char pos))
  134. ;; Symbol or string.
  135. (pos
  136. ;; Try to find (def full-name ...).
  137. (if (or (save-excursion
  138. (search-forward-regexp (format "(def.*\\s-\\(%s\\)" (regexp-quote pos))
  139. nil 'noerror))
  140. (let ((name (replace-regexp-in-string ".*/" "" pos)))
  141. ;; Try to find (def name ...).
  142. (or (save-excursion
  143. (search-forward-regexp (format "(def.*\\s-\\(%s\\)" (regexp-quote name))
  144. nil 'noerror))
  145. ;; Last resort, just find the first occurrence of `name'.
  146. (save-excursion
  147. (search-forward name nil 'noerror)))))
  148. (goto-char (match-beginning 0))
  149. (message "Can't find %s in %s" pos (buffer-file-name))))
  150. (t nil))))
  151. (defun cider--find-buffer-for-file (file)
  152. "Return a buffer visiting FILE.
  153. If FILE is a temp buffer name, return that buffer."
  154. (if (string-prefix-p "*" file)
  155. file
  156. (and file
  157. (not (cider--tooling-file-p file))
  158. (cider-find-file file))))
  159. (defun cider--jump-to-loc-from-info (info &optional other-window)
  160. "Jump to location give by INFO.
  161. INFO object is returned by `cider-var-info' or `cider-member-info'.
  162. OTHER-WINDOW is passed to `cider-jump-to'."
  163. (let* ((line (nrepl-dict-get info "line"))
  164. (file (nrepl-dict-get info "file"))
  165. (name (nrepl-dict-get info "name"))
  166. ;; the filename might actually be a REPL buffer name
  167. (buffer (cider--find-buffer-for-file file)))
  168. (if buffer
  169. (cider-jump-to buffer (if line (cons line nil) name) other-window)
  170. (error "No source location"))))
  171. (declare-function url-filename "url-parse" (cl-x) t)
  172. (defun cider--url-to-file (url)
  173. "Return the filename from the resource URL.
  174. Uses `url-generic-parse-url' to parse the url. The filename is extracted and
  175. then url decoded. If the decoded filename has a Windows device letter followed
  176. by a colon immediately after the leading '/' then the leading '/' is dropped to
  177. create a valid path."
  178. (let ((filename (url-unhex-string (url-filename (url-generic-parse-url url)))))
  179. (if (string-match "^/\\([a-zA-Z]:/.*\\)" filename)
  180. (match-string 1 filename)
  181. filename)))
  182. (defun cider-make-tramp-prefix (method user host)
  183. "Constructs a Tramp file prefix from METHOD, USER, HOST.
  184. It originated from Tramp's `tramp-make-tramp-file-name'. The original be
  185. forced to make full file name with `with-parsed-tramp-file-name', not providing
  186. prefix only option."
  187. (concat tramp-prefix-format
  188. (unless (zerop (length method))
  189. (concat method tramp-postfix-method-format))
  190. (unless (zerop (length user))
  191. (concat user tramp-postfix-user-format))
  192. (when host
  193. (if (string-match tramp-ipv6-regexp host)
  194. (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
  195. host))
  196. tramp-postfix-host-format))
  197. (defun cider-tramp-prefix (&optional buffer)
  198. "Use the filename for BUFFER to determine a tramp prefix.
  199. Defaults to the current buffer. Return the tramp prefix, or nil
  200. if BUFFER is local."
  201. (let* ((buffer (or buffer (current-buffer)))
  202. (name (or (buffer-file-name buffer)
  203. (with-current-buffer buffer
  204. default-directory))))
  205. (when (tramp-tramp-file-p name)
  206. (with-parsed-tramp-file-name name v
  207. (with-no-warnings
  208. (cider-make-tramp-prefix v-method v-user v-host))))))
  209. (defun cider--client-tramp-filename (name &optional buffer)
  210. "Return the tramp filename for path NAME relative to BUFFER.
  211. If BUFFER has a tramp prefix, it will be added as a prefix to NAME.
  212. If the resulting path is an existing tramp file, it returns the path,
  213. otherwise, nil."
  214. (let* ((buffer (or buffer (current-buffer)))
  215. (name (replace-regexp-in-string "^file:" "" name))
  216. (name (concat (cider-tramp-prefix buffer) name)))
  217. (if (tramp-handle-file-exists-p name)
  218. name)))
  219. (defun cider--server-filename (name)
  220. "Return the nREPL server-relative filename for NAME."
  221. (if (tramp-tramp-file-p name)
  222. (with-parsed-tramp-file-name name nil
  223. localname)
  224. name))
  225. (defvar cider-from-nrepl-filename-function
  226. (with-no-warnings
  227. (if (eq system-type 'cygwin)
  228. #'cygwin-convert-file-name-from-windows
  229. #'identity))
  230. "Function to translate nREPL namestrings to Emacs filenames.")
  231. (defcustom cider-prefer-local-resources nil
  232. "Prefer local resources to remote (tramp) ones when both are available."
  233. :type 'boolean
  234. :group 'cider)
  235. (defun cider--file-path (path)
  236. "Return PATH's local or tramp path using `cider-prefer-local-resources'.
  237. If no local or remote file exists, return nil."
  238. (let* ((local-path (funcall cider-from-nrepl-filename-function path))
  239. (tramp-path (and local-path (cider--client-tramp-filename local-path))))
  240. (cond ((equal local-path "") "")
  241. ((and cider-prefer-local-resources (file-exists-p local-path))
  242. local-path)
  243. ((and tramp-path (file-exists-p tramp-path))
  244. tramp-path)
  245. ((and local-path (file-exists-p local-path))
  246. local-path))))
  247. (declare-function archive-extract "arc-mode")
  248. (declare-function archive-zip-extract "arc-mode")
  249. (defun cider-find-file (url)
  250. "Return a buffer visiting the file URL if it exists, or nil otherwise.
  251. If URL has a scheme prefix, it must represent a fully-qualified file path
  252. or an entry within a zip/jar archive. If AVFS (archive virtual file
  253. system; see online docs) is mounted the archive entry is opened inside the
  254. AVFS directory, otherwise the entry is archived into a temporary read-only
  255. buffer. If URL doesn't contain a scheme prefix and is an absolute path, it
  256. is treated as such. Finally, if URL is relative, it is expanded within each
  257. of the open Clojure buffers till an existing file ending with URL has been
  258. found."
  259. (require 'arc-mode)
  260. (cond ((string-match "^file:\\(.+\\)" url)
  261. (when-let* ((file (cider--url-to-file (match-string 1 url)))
  262. (path (cider--file-path file)))
  263. (find-file-noselect path)))
  264. ((string-match "^\\(jar\\|zip\\):\\(file:.+\\)!/\\(.+\\)" url)
  265. (when-let* ((entry (match-string 3 url))
  266. (file (cider--url-to-file (match-string 2 url)))
  267. (path (cider--file-path file))
  268. (name (format "%s:%s" path entry))
  269. (avfs (format "%s%s#uzip/%s"
  270. (expand-file-name (or (getenv "AVFSBASE") "~/.avfs/"))
  271. path entry)))
  272. (cond
  273. ;; 1) use avfs
  274. ((file-exists-p avfs)
  275. (find-file-noselect avfs))
  276. ;; 2) already uncompressed
  277. ((find-buffer-visiting name))
  278. ;; 3) on remotes use Emacs built-in archiving
  279. ((tramp-tramp-file-p path)
  280. (find-file path)
  281. (goto-char (point-min))
  282. ;; anchor to eol to prevent eg. clj matching cljs.
  283. (re-search-forward (concat entry "$"))
  284. (let ((archive-buffer (current-buffer)))
  285. (archive-extract)
  286. (kill-buffer archive-buffer))
  287. (current-buffer))
  288. ;; 4) Use external zip program to extract a single file
  289. (t
  290. (with-current-buffer (generate-new-buffer
  291. (file-name-nondirectory entry))
  292. (archive-zip-extract path entry)
  293. (set-visited-file-name name)
  294. (setq-local default-directory (file-name-directory path))
  295. (setq-local buffer-read-only t)
  296. (set-buffer-modified-p nil)
  297. (set-auto-mode)
  298. (current-buffer))))))
  299. (t (if-let* ((path (cider--file-path url)))
  300. (find-file-noselect path)
  301. (unless (file-name-absolute-p url)
  302. (let ((cider-buffers (cider-util--clojure-buffers))
  303. (url (file-name-nondirectory url)))
  304. (or (cl-loop for bf in cider-buffers
  305. for path = (with-current-buffer bf
  306. (expand-file-name url))
  307. if (and path (file-exists-p path))
  308. return (find-file-noselect path))
  309. (cl-loop for bf in cider-buffers
  310. if (string= (buffer-name bf) url)
  311. return bf))))))))
  312. (defun cider--open-other-window-p (arg)
  313. "Test prefix value ARG to see if it indicates displaying results in other window."
  314. (let ((narg (prefix-numeric-value arg)))
  315. (pcase narg
  316. (-1 t) ; -
  317. (16 t) ; empty empty
  318. (_ nil))))
  319. (defun cider-abbreviate-ns (namespace)
  320. "Return a string that abbreviates NAMESPACE."
  321. (when namespace
  322. (let* ((names (reverse (split-string namespace "\\.")))
  323. (lastname (car names)))
  324. (concat (mapconcat (lambda (s) (concat (substring s 0 1) "."))
  325. (reverse (cdr names))
  326. "")
  327. lastname))))
  328. (defun cider-last-ns-segment (namespace)
  329. "Return the last segment of NAMESPACE."
  330. (when namespace
  331. (car (reverse (split-string namespace "\\.")))))
  332. (provide 'cider-common)
  333. ;;; cider-common.el ends here