PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/.emacs.d/lib/clojure-mode/clojure-mode.el

https://github.com/technomancy/dotfiles
Emacs Lisp | 2357 lines | 1892 code | 231 blank | 234 comment | 47 complexity | 0337fbff3e1039ed402e7c604228c70d MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. ;;; clojure-mode.el --- Major mode for Clojure code -*- lexical-binding: t; -*-
  2. ;; Copyright © 2007-2016 Jeffrey Chu, Lennart Staflin, Phil Hagelberg
  3. ;; Copyright © 2013-2016 Bozhidar Batsov, Artur Malabarba
  4. ;;
  5. ;; Authors: Jeffrey Chu <jochu0@gmail.com>
  6. ;; Lennart Staflin <lenst@lysator.liu.se>
  7. ;; Phil Hagelberg <technomancy@gmail.com>
  8. ;; Bozhidar Batsov <bozhidar@batsov.com>
  9. ;; Artur Malabarba <bruce.connor.am@gmail.com>
  10. ;; URL: http://github.com/clojure-emacs/clojure-mode
  11. ;; Keywords: languages clojure clojurescript lisp
  12. ;; Version: 5.6.0
  13. ;; Package-Requires: ((emacs "24.4"))
  14. ;; This file is not part of GNU Emacs.
  15. ;;; Commentary:
  16. ;; Provides font-lock, indentation, navigation and basic refactoring for the
  17. ;; Clojure programming language (http://clojure.org).
  18. ;; Using clojure-mode with paredit or smartparens is highly recommended.
  19. ;; Here are some example configurations:
  20. ;; ;; require or autoload paredit-mode
  21. ;; (add-hook 'clojure-mode-hook #'paredit-mode)
  22. ;; ;; require or autoload smartparens
  23. ;; (add-hook 'clojure-mode-hook #'smartparens-strict-mode)
  24. ;; See inf-clojure (http://github.com/clojure-emacs/inf-clojure) for
  25. ;; basic interaction with Clojure subprocesses.
  26. ;; See CIDER (http://github.com/clojure-emacs/cider) for
  27. ;; better interaction with subprocesses via nREPL.
  28. ;;; License:
  29. ;; This program is free software; you can redistribute it and/or
  30. ;; modify it under the terms of the GNU General Public License
  31. ;; as published by the Free Software Foundation; either version 3
  32. ;; of the License, or (at your option) any later version.
  33. ;;
  34. ;; This program is distributed in the hope that it will be useful,
  35. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. ;; GNU General Public License for more details.
  38. ;;
  39. ;; You should have received a copy of the GNU General Public License
  40. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  41. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  42. ;; Boston, MA 02110-1301, USA.
  43. ;;; Code:
  44. (eval-when-compile
  45. (defvar calculate-lisp-indent-last-sexp)
  46. (defvar font-lock-beg)
  47. (defvar font-lock-end)
  48. (defvar paredit-space-for-delimiter-predicates)
  49. (defvar paredit-version)
  50. (defvar paredit-mode))
  51. (require 'cl-lib)
  52. (require 'imenu)
  53. (require 'newcomment)
  54. (require 'align)
  55. (require 'subr-x)
  56. (declare-function lisp-fill-paragraph "lisp-mode" (&optional justify))
  57. (defgroup clojure nil
  58. "Major mode for editing Clojure code."
  59. :prefix "clojure-"
  60. :group 'languages
  61. :link '(url-link :tag "Github" "https://github.com/clojure-emacs/clojure-mode")
  62. :link '(emacs-commentary-link :tag "Commentary" "clojure-mode"))
  63. (defconst clojure-mode-version "5.5.2"
  64. "The current version of `clojure-mode'.")
  65. (defface clojure-keyword-face
  66. '((t (:inherit font-lock-constant-face)))
  67. "Face used to font-lock Clojure keywords (:something)."
  68. :package-version '(clojure-mode . "3.0.0"))
  69. (defface clojure-character-face
  70. '((t (:inherit font-lock-string-face)))
  71. "Face used to font-lock Clojure character literals."
  72. :package-version '(clojure-mode . "3.0.0"))
  73. (defface clojure-interop-method-face
  74. '((t (:inherit font-lock-preprocessor-face)))
  75. "Face used to font-lock interop method names (camelCase)."
  76. :package-version '(clojure-mode . "3.0.0"))
  77. (defcustom clojure-indent-style :always-align
  78. "Indentation style to use for function forms and macro forms.
  79. There are two cases of interest configured by this variable.
  80. - Case (A) is when at least one function argument is on the same
  81. line as the function name.
  82. - Case (B) is the opposite (no arguments are on the same line as
  83. the function name). Note that the body of macros is not
  84. affected by this variable, it is always indented by
  85. `lisp-body-indent' (default 2) spaces.
  86. Note that this variable configures the indentation of function
  87. forms (and function-like macros), it does not affect macros that
  88. already use special indentation rules.
  89. The possible values for this variable are keywords indicating how
  90. to indent function forms.
  91. `:always-align' - Follow the same rules as `lisp-mode'. All
  92. args are vertically aligned with the first arg in case (A),
  93. and vertically aligned with the function name in case (B).
  94. For instance:
  95. (reduce merge
  96. some-coll)
  97. (reduce
  98. merge
  99. some-coll)
  100. `:always-indent' - All args are indented like a macro body.
  101. (reduce merge
  102. some-coll)
  103. (reduce
  104. merge
  105. some-coll)
  106. `:align-arguments' - Case (A) is indented like `lisp', and
  107. case (B) is indented like a macro body.
  108. (reduce merge
  109. some-coll)
  110. (reduce
  111. merge
  112. some-coll)"
  113. :safe #'keywordp
  114. :type '(choice (const :tag "Same as `lisp-mode'" :always-align)
  115. (const :tag "Indent like a macro body" :always-indent)
  116. (const :tag "Indent like a macro body unless first arg is on the same line"
  117. :align-arguments))
  118. :package-version '(clojure-mode . "5.2.0"))
  119. (define-obsolete-variable-alias 'clojure-defun-style-default-indent
  120. 'clojure-indent-style "5.2.0")
  121. (defcustom clojure-use-backtracking-indent t
  122. "When non-nil, enable context sensitive indentation."
  123. :type 'boolean
  124. :safe 'booleanp)
  125. (defcustom clojure-max-backtracking 3
  126. "Maximum amount to backtrack up a list to check for context."
  127. :type 'integer
  128. :safe 'integerp)
  129. (defcustom clojure-docstring-fill-column fill-column
  130. "Value of `fill-column' to use when filling a docstring."
  131. :type 'integer
  132. :safe 'integerp)
  133. (defcustom clojure-docstring-fill-prefix-width 2
  134. "Width of `fill-prefix' when filling a docstring.
  135. The default value conforms with the de facto convention for
  136. Clojure docstrings, aligning the second line with the opening
  137. double quotes on the third column."
  138. :type 'integer
  139. :safe 'integerp)
  140. (defcustom clojure-omit-space-between-tag-and-delimiters '(?\[ ?\{)
  141. "Allowed opening delimiter characters after a reader literal tag.
  142. For example, \[ is allowed in :db/id[:db.part/user]."
  143. :type '(set (const :tag "[" ?\[)
  144. (const :tag "{" ?\{)
  145. (const :tag "(" ?\()
  146. (const :tag "\"" ?\"))
  147. :safe (lambda (value)
  148. (and (listp value)
  149. (cl-every 'characterp value))))
  150. (defcustom clojure-build-tool-files '("project.clj" "build.boot" "build.gradle")
  151. "A list of files, which identify a Clojure project's root.
  152. Out-of-the box clojure-mode understands lein, boot and gradle."
  153. :type '(repeat string)
  154. :package-version '(clojure-mode . "5.0.0")
  155. :safe (lambda (value)
  156. (and (listp value)
  157. (cl-every 'stringp value))))
  158. (defcustom clojure-refactor-map-prefix (kbd "C-c C-r")
  159. "Clojure refactor keymap prefix."
  160. :group 'clojure
  161. :type 'string
  162. :package-version '(clojure-mode . "5.6.0"))
  163. (defvar clojure-refactor-map
  164. (let ((map (make-sparse-keymap)))
  165. (define-key map (kbd "C-t") #'clojure-thread)
  166. (define-key map (kbd "t") #'clojure-thread)
  167. (define-key map (kbd "C-u") #'clojure-unwind)
  168. (define-key map (kbd "u") #'clojure-unwind)
  169. (define-key map (kbd "C-f") #'clojure-thread-first-all)
  170. (define-key map (kbd "f") #'clojure-thread-first-all)
  171. (define-key map (kbd "C-l") #'clojure-thread-last-all)
  172. (define-key map (kbd "l") #'clojure-thread-last-all)
  173. (define-key map (kbd "C-a") #'clojure-unwind-all)
  174. (define-key map (kbd "a") #'clojure-unwind-all)
  175. (define-key map (kbd "C-p") #'clojure-cycle-privacy)
  176. (define-key map (kbd "p") #'clojure-cycle-privacy)
  177. (define-key map (kbd "C-(") #'clojure-convert-collection-to-list)
  178. (define-key map (kbd "(") #'clojure-convert-collection-to-list)
  179. (define-key map (kbd "C-'") #'clojure-convert-collection-to-quoted-list)
  180. (define-key map (kbd "'") #'clojure-convert-collection-to-quoted-list)
  181. (define-key map (kbd "C-{") #'clojure-convert-collection-to-map)
  182. (define-key map (kbd "{") #'clojure-convert-collection-to-map)
  183. (define-key map (kbd "C-[") #'clojure-convert-collection-to-vector)
  184. (define-key map (kbd "[") #'clojure-convert-collection-to-vector)
  185. (define-key map (kbd "C-#") #'clojure-convert-collection-to-set)
  186. (define-key map (kbd "#") #'clojure-convert-collection-to-set)
  187. (define-key map (kbd "C-i") #'clojure-cycle-if)
  188. (define-key map (kbd "i") #'clojure-cycle-if)
  189. (define-key map (kbd "n i") #'clojure-insert-ns-form)
  190. (define-key map (kbd "n h") #'clojure-insert-ns-form-at-point)
  191. (define-key map (kbd "n u") #'clojure-update-ns)
  192. (define-key map (kbd "n s") #'clojure-sort-ns)
  193. (define-key map (kbd "s i") #'clojure-introduce-let)
  194. (define-key map (kbd "s m") #'clojure-move-to-let)
  195. (define-key map (kbd "s f") #'clojure-let-forward-slurp-sexp)
  196. (define-key map (kbd "s b") #'clojure-let-backward-slurp-sexp)
  197. map)
  198. "Keymap for Clojure refactoring commands.")
  199. (fset 'clojure-refactor-map clojure-refactor-map)
  200. (defvar clojure-mode-map
  201. (let ((map (make-sparse-keymap)))
  202. (define-key map (kbd "C-:") #'clojure-toggle-keyword-string)
  203. (define-key map (kbd "C-c SPC") #'clojure-align)
  204. (define-key map clojure-refactor-map-prefix 'clojure-refactor-map)
  205. (easy-menu-define clojure-mode-menu map "Clojure Mode Menu"
  206. '("Clojure"
  207. ["Toggle between string & keyword" clojure-toggle-keyword-string]
  208. ["Align expression" clojure-align]
  209. ["Cycle privacy" clojure-cycle-privacy]
  210. ["Cycle if, if-not" clojure-cycle-if]
  211. ("ns forms"
  212. ["Insert ns form at the top" clojure-insert-ns-form]
  213. ["Insert ns form here" clojure-insert-ns-form-at-point]
  214. ["Update ns form" clojure-update-ns]
  215. ["Sort ns form" clojure-sort-ns])
  216. ("Convert collection"
  217. ["Convert to list" clojure-convert-collection-to-list]
  218. ["Convert to quoted list" clojure-convert-collection-to-quoted-list]
  219. ["Convert to map" clojure-convert-collection-to-map]
  220. ["Convert to vector" clojure-convert-collection-to-vector]
  221. ["Convert to set" clojure-convert-collection-to-set])
  222. ("Refactor -> and ->>"
  223. ["Thread once more" clojure-thread]
  224. ["Fully thread a form with ->" clojure-thread-first-all]
  225. ["Fully thread a form with ->>" clojure-thread-last-all]
  226. "--"
  227. ["Unwind once" clojure-unwind]
  228. ["Fully unwind a threading macro" clojure-unwind-all])
  229. ("Let expression"
  230. ["Introduce let" clojure-introduce-let]
  231. ["Move to let" clojure-move-to-let]
  232. ["Forward slurp form into let" clojure-let-forward-slurp-sexp]
  233. ["Backward slurp form into let" clojure-let-backward-slurp-sexp])
  234. ("Documentation"
  235. ["View a Clojure guide" clojure-view-guide]
  236. ["View a Clojure reference section" clojure-view-reference-section]
  237. ["View the Clojure cheatsheet" clojure-view-cheatsheet]
  238. ["View the Clojure Grimoire" clojure-view-grimoire]
  239. ["View the Clojure style guide" clojure-view-style-guide])
  240. "--"
  241. ["Report a clojure-mode bug" clojure-mode-report-bug]
  242. ["Clojure-mode version" clojure-mode-display-version]))
  243. map)
  244. "Keymap for Clojure mode.")
  245. (defvar clojure-mode-syntax-table
  246. (let ((table (copy-syntax-table emacs-lisp-mode-syntax-table)))
  247. (modify-syntax-entry ?\{ "(}" table)
  248. (modify-syntax-entry ?\} "){" table)
  249. (modify-syntax-entry ?\[ "(]" table)
  250. (modify-syntax-entry ?\] ")[" table)
  251. (modify-syntax-entry ?? "_ p" table) ; ? is a prefix outside symbols
  252. (modify-syntax-entry ?# "_ p" table) ; # is allowed inside keywords (#399)
  253. (modify-syntax-entry ?~ "'" table)
  254. (modify-syntax-entry ?^ "'" table)
  255. (modify-syntax-entry ?@ "'" table)
  256. table)
  257. "Syntax table for Clojure mode.
  258. Inherits from `emacs-lisp-mode-syntax-table'.")
  259. (defconst clojure--prettify-symbols-alist
  260. '(("fn" . ?λ)))
  261. (defvar-local clojure-expected-ns-function nil
  262. "The function used to determine the expected namespace of a file.
  263. `clojure-mode' ships a basic function named `clojure-expected-ns'
  264. that does basic heuristics to figure this out.
  265. CIDER provides a more complex version which does classpath analysis.")
  266. (defun clojure-mode-display-version ()
  267. "Display the current `clojure-mode-version' in the minibuffer."
  268. (interactive)
  269. (message "clojure-mode (version %s)" clojure-mode-version))
  270. (defconst clojure-mode-report-bug-url "https://github.com/clojure-emacs/clojure-mode/issues/new"
  271. "The URL to report a clojure-mode issue.")
  272. (defun clojure-mode-report-bug ()
  273. "Report a bug in your default browser."
  274. (interactive)
  275. (browse-url clojure-mode-report-bug-url))
  276. (defconst clojure-guides-base-url "https://clojure.org/guides/"
  277. "The base URL for official Clojure guides.")
  278. (defconst clojure-guides '(("Getting Started" . "getting_started")
  279. ("FAQ" . "faq")
  280. ("spec" . "spec")
  281. ("Destructuring" . "destructuring")
  282. ("Threading Macros" . "threading_macros")
  283. ("Comparators" . "comparators")
  284. ("Reader Conditionals" . "reader_conditionals"))
  285. "A list of all official Clojure guides.")
  286. (defun clojure-view-guide ()
  287. "Open a Clojure guide in your default browser.
  288. The command will prompt you to select one of the available guides."
  289. (interactive)
  290. (let ((guide (completing-read "Select a guide: " (mapcar #'car clojure-guides))))
  291. (when guide
  292. (let ((guide-url (concat clojure-guides-base-url (cdr (assoc guide clojure-guides)))))
  293. (browse-url guide-url)))))
  294. (defconst clojure-reference-base-url "https://clojure.org/reference/"
  295. "The base URL for the official Clojure reference.")
  296. (defconst clojure-reference-sections '(("The Reader" . "reader")
  297. ("The REPL and main" . "repl_and_main")
  298. ("Evaluation" . "evaluation")
  299. ("Special Forms" . "special_forms")
  300. ("Macros" . "macros")
  301. ("Other Functions" . "other_functions")
  302. ("Data Structures" . "data_structures")
  303. ("Datatypes" . "datatypes")
  304. ("Sequences" . "sequences")
  305. ("Transients" . "transients")
  306. ("Transducers" . "transducers")
  307. ("Multimethods and Hierarchies" . "multimethods")
  308. ("Protocols" . "protocols")
  309. ("Metadata" . "metadata")
  310. ("Namespaces" . "namespaces")
  311. ("Libs" . "libs")
  312. ("Vars and Environments" . "vars")
  313. ("Refs and Transactions" . "refs")
  314. ("Agents" . "agents")
  315. ("Atoms" . "atoms")
  316. ("Reducers" . "reducers")
  317. ("Java Interop" . "java_interop")
  318. ("Compilation and Class Generation" . "compilation")
  319. ("Other Libraries" . "other_libraries")
  320. ("Differences with Lisps" . "lisps")))
  321. (defun clojure-view-reference-section ()
  322. "Open a Clojure reference section in your default browser.
  323. The command will prompt you to select one of the available sections."
  324. (interactive)
  325. (let ((section (completing-read "Select a reference section: " (mapcar #'car clojure-reference-sections))))
  326. (when section
  327. (let ((section-url (concat clojure-reference-base-url (cdr (assoc section clojure-reference-sections)))))
  328. (browse-url section-url)))))
  329. (defconst clojure-cheatsheet-url "http://clojure.org/api/cheatsheet"
  330. "The URL of the official Clojure cheatsheet.")
  331. (defun clojure-view-cheatsheet ()
  332. "Open the Clojure cheatsheet in your default browser."
  333. (interactive)
  334. (browse-url clojure-cheatsheet-url))
  335. (defconst clojure-grimoire-url "https://www.conj.io/"
  336. "The URL of the Grimoire community documentation site.")
  337. (defun clojure-view-grimoire ()
  338. "Open the Clojure Grimoire in your default browser."
  339. (interactive)
  340. (browse-url clojure-grimoire-url))
  341. (defconst clojure-style-guide-url "https://github.com/bbatsov/clojure-style-guide"
  342. "The URL of the Clojure style guide.")
  343. (defun clojure-view-style-guide ()
  344. "Open the Clojure style guide in your default browser."
  345. (interactive)
  346. (browse-url clojure-style-guide-url))
  347. (defun clojure-space-for-delimiter-p (endp delim)
  348. "Prevent paredit from inserting useless spaces.
  349. See `paredit-space-for-delimiter-predicates' for the meaning of
  350. ENDP and DELIM."
  351. (or endp
  352. (not (memq delim '(?\" ?{ ?\( )))
  353. (not (or (derived-mode-p 'clojure-mode)
  354. (derived-mode-p 'cider-repl-mode)))
  355. (save-excursion
  356. (backward-char)
  357. (cond ((eq (char-after) ?#)
  358. (and (not (bobp))
  359. (or (char-equal ?w (char-syntax (char-before)))
  360. (char-equal ?_ (char-syntax (char-before))))))
  361. ((and (eq delim ?\()
  362. (eq (char-after) ??)
  363. (eq (char-before) ?#))
  364. nil)
  365. (t)))))
  366. (defun clojure-no-space-after-tag (endp delimiter)
  367. "Prevent inserting a space after a reader-literal tag?
  368. When a reader-literal tag is followed be an opening delimiter
  369. listed in `clojure-omit-space-between-tag-and-delimiters', this
  370. function returns t.
  371. This allows you to write things like #db/id[:db.part/user]
  372. without inserting a space between the tag and the opening
  373. bracket.
  374. See `paredit-space-for-delimiter-predicates' for the meaning of
  375. ENDP and DELIMITER."
  376. (if endp
  377. t
  378. (or (not (member delimiter clojure-omit-space-between-tag-and-delimiters))
  379. (save-excursion
  380. (let ((orig-point (point)))
  381. (not (and (re-search-backward
  382. "#\\([a-zA-Z0-9._-]+/\\)?[a-zA-Z0-9._-]+"
  383. (line-beginning-position)
  384. t)
  385. (= orig-point (match-end 0)))))))))
  386. (declare-function paredit-open-curly "ext:paredit")
  387. (declare-function paredit-close-curly "ext:paredit")
  388. (declare-function paredit-convolute-sexp "ext:paredit")
  389. (defun clojure--replace-let-bindings-and-indent (orig-fun &rest args)
  390. "Advise `paredit-convolute-sexp' to replace s-expressions with their bound name if a let form was convoluted."
  391. (save-excursion
  392. (backward-sexp)
  393. (when (looking-back clojure--let-regexp)
  394. (clojure--replace-sexps-with-bindings-and-indent))))
  395. (defun clojure-paredit-setup (&optional keymap)
  396. "Make \"paredit-mode\" play nice with `clojure-mode'.
  397. If an optional KEYMAP is passed the changes are applied to it,
  398. instead of to `clojure-mode-map'.
  399. Also advice `paredit-convolute-sexp' when used on a let form as drop in
  400. replacement for `cljr-expand-let`."
  401. (when (>= paredit-version 21)
  402. (let ((keymap (or keymap clojure-mode-map)))
  403. (define-key keymap "{" #'paredit-open-curly)
  404. (define-key keymap "}" #'paredit-close-curly))
  405. (add-to-list 'paredit-space-for-delimiter-predicates
  406. #'clojure-space-for-delimiter-p)
  407. (add-to-list 'paredit-space-for-delimiter-predicates
  408. #'clojure-no-space-after-tag)
  409. (advice-add 'paredit-convolute-sexp :after #'clojure--replace-let-bindings-and-indent)))
  410. (defun clojure-mode-variables ()
  411. "Set up initial buffer-local variables for Clojure mode."
  412. (add-to-list 'imenu-generic-expression '(nil clojure-match-next-def 0))
  413. (setq-local indent-tabs-mode nil)
  414. (setq-local paragraph-ignore-fill-prefix t)
  415. (setq-local outline-regexp ";;;\\(;* [^ \t\n]\\)\\|(")
  416. (setq-local outline-level 'lisp-outline-level)
  417. (setq-local comment-start ";")
  418. (setq-local comment-start-skip ";+ *")
  419. (setq-local comment-add 1) ; default to `;;' in comment-region
  420. (setq-local comment-column 40)
  421. (setq-local comment-use-syntax t)
  422. (setq-local multibyte-syntax-as-symbol t)
  423. (setq-local electric-pair-skip-whitespace 'chomp)
  424. (setq-local electric-pair-open-newline-between-pairs nil)
  425. (setq-local fill-paragraph-function #'clojure-fill-paragraph)
  426. (setq-local adaptive-fill-function #'clojure-adaptive-fill-function)
  427. (setq-local normal-auto-fill-function #'clojure-auto-fill-function)
  428. (setq-local comment-start-skip
  429. "\\(\\(^\\|[^\\\\\n]\\)\\(\\\\\\\\\\)*\\)\\(;+\\|#|\\) *")
  430. (setq-local indent-line-function #'clojure-indent-line)
  431. (setq-local indent-region-function #'clojure-indent-region)
  432. (setq-local lisp-indent-function #'clojure-indent-function)
  433. (setq-local lisp-doc-string-elt-property 'clojure-doc-string-elt)
  434. (setq-local clojure-expected-ns-function #'clojure-expected-ns)
  435. (setq-local parse-sexp-ignore-comments t)
  436. (setq-local prettify-symbols-alist clojure--prettify-symbols-alist)
  437. (setq-local open-paren-in-column-0-is-defun-start nil))
  438. ;;;###autoload
  439. (define-derived-mode clojure-mode prog-mode "Clojure"
  440. "Major mode for editing Clojure code.
  441. \\{clojure-mode-map}"
  442. (clojure-mode-variables)
  443. (clojure-font-lock-setup)
  444. (add-hook 'paredit-mode-hook #'clojure-paredit-setup))
  445. (defcustom clojure-verify-major-mode t
  446. "If non-nil, warn when activating the wrong major-mode."
  447. :type 'boolean
  448. :safe #'booleanp
  449. :package-version '(clojure-mode "5.3.0"))
  450. (defun clojure--check-wrong-major-mode ()
  451. "Check if the current major-mode matches the file extension.
  452. If it doesn't, issue a warning if `clojure-verify-major-mode' is
  453. non-nil."
  454. (when (and clojure-verify-major-mode
  455. (stringp (buffer-file-name)))
  456. (let* ((case-fold-search t)
  457. (problem (cond ((and (string-match "\\.clj\\'" (buffer-file-name))
  458. (not (eq major-mode 'clojure-mode)))
  459. 'clojure-mode)
  460. ((and (string-match "\\.cljs\\'" (buffer-file-name))
  461. (not (eq major-mode 'clojurescript-mode)))
  462. 'clojurescript-mode)
  463. ((and (string-match "\\.cljc\\'" (buffer-file-name))
  464. (not (eq major-mode 'clojurec-mode)))
  465. 'clojurec-mode)
  466. ((and (string-match "\\.cljx\\'" (buffer-file-name))
  467. (not (eq major-mode 'clojurex-mode)))
  468. 'clojurex-mode))))
  469. (when problem
  470. (message "[WARNING] %s activated `%s' instead of `%s' in this buffer.
  471. This could cause problems.
  472. \(See `clojure-verify-major-mode' to disable this message.)"
  473. (if (eq major-mode real-this-command)
  474. "You have"
  475. "Something in your configuration")
  476. major-mode
  477. problem)))))
  478. (add-hook 'clojure-mode-hook #'clojure--check-wrong-major-mode)
  479. (defsubst clojure-in-docstring-p ()
  480. "Check whether point is in a docstring."
  481. (eq (get-text-property (point) 'face) 'font-lock-doc-face))
  482. (defsubst clojure-docstring-fill-prefix ()
  483. "The prefix string used by `clojure-fill-paragraph'.
  484. It is simply `clojure-docstring-fill-prefix-width' number of spaces."
  485. (make-string clojure-docstring-fill-prefix-width ? ))
  486. (defun clojure-adaptive-fill-function ()
  487. "Clojure adaptive fill function.
  488. This only takes care of filling docstring correctly."
  489. (when (clojure-in-docstring-p)
  490. (clojure-docstring-fill-prefix)))
  491. (defun clojure-fill-paragraph (&optional justify)
  492. "Like `fill-paragraph', but can handle Clojure docstrings.
  493. If JUSTIFY is non-nil, justify as well as fill the paragraph."
  494. (if (clojure-in-docstring-p)
  495. (let ((paragraph-start
  496. (concat paragraph-start
  497. "\\|\\s-*\\([(;:\"[]\\|~@\\|`(\\|#'(\\)"))
  498. (paragraph-separate
  499. (concat paragraph-separate "\\|\\s-*\".*[,\\.]$"))
  500. (fill-column (or clojure-docstring-fill-column fill-column))
  501. (fill-prefix (clojure-docstring-fill-prefix)))
  502. (fill-paragraph justify))
  503. (let ((paragraph-start (concat paragraph-start
  504. "\\|\\s-*\\([(;:\"[]\\|`(\\|#'(\\)"))
  505. (paragraph-separate
  506. (concat paragraph-separate "\\|\\s-*\".*[,\\.[]$")))
  507. (or (fill-comment-paragraph justify)
  508. (fill-paragraph justify))
  509. ;; Always return `t'
  510. t)))
  511. (defun clojure-auto-fill-function ()
  512. "Clojure auto-fill function."
  513. ;; Check if auto-filling is meaningful.
  514. (let ((fc (current-fill-column)))
  515. (when (and fc (> (current-column) fc))
  516. (let ((fill-column (if (clojure-in-docstring-p)
  517. clojure-docstring-fill-column
  518. fill-column))
  519. (fill-prefix (clojure-adaptive-fill-function)))
  520. (do-auto-fill)))))
  521. ;;; #_ comments font-locking
  522. ;; Code heavily borrowed from Slime.
  523. ;; https://github.com/slime/slime/blob/master/contrib/slime-fontifying-fu.el#L186
  524. (defvar clojure--comment-macro-regexp
  525. (rx "#_" (* " ") (group-n 1 (not (any " "))))
  526. "Regexp matching the start of a comment sexp.
  527. The beginning of match-group 1 should be before the sexp to be
  528. marked as a comment. The end of sexp is found with
  529. `clojure-forward-logical-sexp'.
  530. By default, this only applies to code after the `#_' reader
  531. macro. In order to also font-lock the `(comment ...)' macro as a
  532. comment, you can set the value to:
  533. \"#_ *\\\\(?1:[^ ]\\\\)\\\\|\\\\(?1:(comment\\\\_>\\\\)\"")
  534. (defun clojure--search-comment-macro-internal (limit)
  535. (when (search-forward-regexp clojure--comment-macro-regexp limit t)
  536. (let* ((md (match-data))
  537. (start (match-beginning 1))
  538. (state (syntax-ppss start)))
  539. ;; inside string or comment?
  540. (if (or (nth 3 state)
  541. (nth 4 state))
  542. (clojure--search-comment-macro-internal limit)
  543. (goto-char start)
  544. (clojure-forward-logical-sexp 1)
  545. ;; Data for (match-end 1).
  546. (setf (elt md 3) (point))
  547. (set-match-data md)
  548. t))))
  549. (defun clojure--search-comment-macro (limit)
  550. "Find comment macros and set the match data.
  551. Search from point up to LIMIT. The region that should be
  552. considered a comment is between `(match-beginning 1)'
  553. and `(match-end 1)'."
  554. (let ((result 'retry))
  555. (while (and (eq result 'retry) (<= (point) limit))
  556. (condition-case nil
  557. (setq result (clojure--search-comment-macro-internal limit))
  558. (end-of-file (setq result nil))
  559. (scan-error (setq result 'retry))))
  560. result))
  561. ;;; General font-locking
  562. (defun clojure-match-next-def ()
  563. "Scans the buffer backwards for the next \"top-level\" definition.
  564. Called by `imenu--generic-function'."
  565. ;; we have to take into account namespace-definition forms
  566. ;; e.g. s/defn
  567. (when (re-search-backward "^(\\([a-z0-9.-]+/\\)?def\\sw*" nil t)
  568. (save-excursion
  569. (let (found?
  570. (start (point)))
  571. (down-list)
  572. (forward-sexp)
  573. (while (not found?)
  574. (forward-sexp)
  575. (or (if (char-equal ?[ (char-after (point)))
  576. (backward-sexp))
  577. (if (char-equal ?) (char-after (point)))
  578. (backward-sexp)))
  579. (cl-destructuring-bind (def-beg . def-end) (bounds-of-thing-at-point 'sexp)
  580. (if (char-equal ?^ (char-after def-beg))
  581. (progn (forward-sexp) (backward-sexp))
  582. (setq found? t)
  583. (set-match-data (list def-beg def-end)))))
  584. (goto-char start)))))
  585. (eval-and-compile
  586. (defconst clojure--sym-forbidden-rest-chars "][\";\'@\\^`~\(\)\{\}\\,\s\t\n\r"
  587. "A list of chars that a Clojure symbol cannot contain.
  588. See definition of 'macros': URL `http://git.io/vRGLD'.")
  589. (defconst clojure--sym-forbidden-1st-chars (concat clojure--sym-forbidden-rest-chars "0-9:")
  590. "A list of chars that a Clojure symbol cannot start with.
  591. See the for-loop: URL `http://git.io/vRGTj' lines: URL
  592. `http://git.io/vRGIh', URL `http://git.io/vRGLE' and value
  593. definition of 'macros': URL `http://git.io/vRGLD'.")
  594. (defconst clojure--sym-regexp
  595. (concat "[^" clojure--sym-forbidden-1st-chars "][^" clojure--sym-forbidden-rest-chars "]*")
  596. "A regexp matching a Clojure symbol or namespace alias.
  597. Matches the rule `clojure--sym-forbidden-1st-chars' followed by
  598. any number of matches of `clojure--sym-forbidden-rest-chars'."))
  599. (defconst clojure-font-lock-keywords
  600. (eval-when-compile
  601. `( ;; Top-level variable definition
  602. (,(concat "(\\(?:clojure.core/\\)?\\("
  603. (regexp-opt '("def" "defonce"))
  604. ;; variable declarations
  605. "\\)\\>"
  606. ;; Any whitespace
  607. "[ \r\n\t]*"
  608. ;; Possibly type or metadata
  609. "\\(?:#?^\\(?:{[^}]*}\\|\\sw+\\)[ \r\n\t]*\\)*"
  610. "\\(\\sw+\\)?")
  611. (1 font-lock-keyword-face)
  612. (2 font-lock-variable-name-face nil t))
  613. ;; Type definition
  614. (,(concat "(\\(?:clojure.core/\\)?\\("
  615. (regexp-opt '("defstruct" "deftype" "defprotocol"
  616. "defrecord"))
  617. ;; type declarations
  618. "\\)\\>"
  619. ;; Any whitespace
  620. "[ \r\n\t]*"
  621. ;; Possibly type or metadata
  622. "\\(?:#?^\\(?:{[^}]*}\\|\\sw+\\)[ \r\n\t]*\\)*"
  623. "\\(\\sw+\\)?")
  624. (1 font-lock-keyword-face)
  625. (2 font-lock-type-face nil t))
  626. ;; Function definition (anything that starts with def and is not
  627. ;; listed above)
  628. (,(concat "(\\(?:" clojure--sym-regexp "/\\)?"
  629. "\\(def[^ \r\n\t]*\\)"
  630. ;; Function declarations
  631. "\\>"
  632. ;; Any whitespace
  633. "[ \r\n\t]*"
  634. ;; Possibly type or metadata
  635. "\\(?:#?^\\(?:{[^}]*}\\|\\sw+\\)[ \r\n\t]*\\)*"
  636. "\\(\\sw+\\)?")
  637. (1 font-lock-keyword-face)
  638. (2 font-lock-function-name-face nil t))
  639. ;; (fn name? args ...)
  640. (,(concat "(\\(?:clojure.core/\\)?\\(fn\\)[ \t]+"
  641. ;; Possibly type
  642. "\\(?:#?^\\sw+[ \t]*\\)?"
  643. ;; Possibly name
  644. "\\(\\sw+\\)?" )
  645. (1 font-lock-keyword-face)
  646. (2 font-lock-function-name-face nil t))
  647. ;; lambda arguments - %, %&, %1, %2, etc
  648. ("\\<%[&1-9]?" (0 font-lock-variable-name-face))
  649. ;; Special forms
  650. (,(concat
  651. "("
  652. (regexp-opt
  653. '("def" "do" "if" "let" "let*" "var" "fn" "fn*" "loop" "loop*"
  654. "recur" "throw" "try" "catch" "finally"
  655. "set!" "new" "."
  656. "monitor-enter" "monitor-exit" "quote") t)
  657. "\\>")
  658. 1 font-lock-keyword-face)
  659. ;; Built-in binding and flow of control forms
  660. (,(concat
  661. "(\\(?:clojure.core/\\)?"
  662. (regexp-opt
  663. '("letfn" "case" "cond" "cond->" "cond->>" "condp"
  664. "for" "when" "when-not" "when-let" "when-first" "when-some"
  665. "if-let" "if-not" "if-some"
  666. ".." "->" "->>" "as->" "doto" "and" "or"
  667. "dosync" "doseq" "dotimes" "dorun" "doall"
  668. "ns" "in-ns"
  669. "with-open" "with-local-vars" "binding"
  670. "with-redefs" "with-redefs-fn"
  671. "declare") t)
  672. "\\>")
  673. 1 font-lock-keyword-face)
  674. ;; Macros similar to let, when, and while
  675. (,(rx symbol-start
  676. (or "let" "when" "while") "-"
  677. (1+ (or (syntax word) (syntax symbol)))
  678. symbol-end)
  679. 0 font-lock-keyword-face)
  680. (,(concat
  681. "\\<"
  682. (regexp-opt
  683. '("*1" "*2" "*3" "*agent*"
  684. "*allow-unresolved-vars*" "*assert*" "*clojure-version*"
  685. "*command-line-args*" "*compile-files*"
  686. "*compile-path*" "*data-readers*" "*default-data-reader-fn*"
  687. "*e" "*err*" "*file*" "*flush-on-newline*"
  688. "*in*" "*macro-meta*" "*math-context*" "*ns*" "*out*"
  689. "*print-dup*" "*print-length*" "*print-level*"
  690. "*print-meta*" "*print-readably*"
  691. "*read-eval*" "*source-path*"
  692. "*unchecked-math*"
  693. "*use-context-classloader*" "*warn-on-reflection*")
  694. t)
  695. "\\>")
  696. 0 font-lock-builtin-face)
  697. ;; Dynamic variables - *something* or @*something*
  698. ("\\(?:\\<\\|/\\)@?\\(\\*[a-z-]*\\*\\)\\>" 1 font-lock-variable-name-face)
  699. ;; Global constants - nil, true, false
  700. (,(concat
  701. "\\<"
  702. (regexp-opt
  703. '("true" "false" "nil") t)
  704. "\\>")
  705. 0 font-lock-constant-face)
  706. ;; Character literals - \1, \a, \newline, \u0000
  707. ("\\\\\\([[:punct:]]\\|[a-z0-9]+\\>\\)" 0 'clojure-character-face)
  708. ;; foo/ Foo/ @Foo/ /FooBar
  709. (,(concat "\\(?:\\<:?\\|\\.\\)@?\\(" clojure--sym-regexp "\\)\\(/\\)")
  710. (1 font-lock-type-face) (2 'default))
  711. ;; Constant values (keywords), including as metadata e.g. ^:static
  712. ("\\<^?\\(:\\(\\sw\\|\\s_\\)+\\(\\>\\|\\_>\\)\\)" 1 'clojure-keyword-face append)
  713. ;; Java interop highlighting
  714. ;; CONST SOME_CONST (optionally prefixed by /)
  715. ("\\(?:\\<\\|/\\)\\([A-Z]+\\|\\([A-Z]+_[A-Z1-9_]+\\)\\)\\>" 1 font-lock-constant-face)
  716. ;; .foo .barBaz .qux01 .-flibble .-flibbleWobble
  717. ("\\<\\.-?[a-z][a-zA-Z0-9]*\\>" 0 'clojure-interop-method-face)
  718. ;; Foo Bar$Baz Qux_ World_OpenUDP Foo. Babylon15.
  719. ("\\(?:\\<\\|\\.\\|/\\|#?^\\)\\([A-Z][a-zA-Z0-9_]*[a-zA-Z0-9$_]+\\.?\\>\\)" 1 font-lock-type-face)
  720. ;; foo.bar.baz
  721. ("\\<^?\\([a-z][a-z0-9_-]+\\.\\([a-z][a-z0-9_-]*\\.?\\)+\\)" 1 font-lock-type-face)
  722. ;; (ns namespace) - special handling for single segment namespaces
  723. (,(concat "(\\<ns\\>[ \r\n\t]*"
  724. ;; Possibly metadata
  725. "\\(?:\\^?{[^}]+}[ \r\n\t]*\\)*"
  726. ;; namespace
  727. "\\([a-z0-9-]+\\)")
  728. (1 font-lock-type-face nil t))
  729. ;; fooBar
  730. ("\\(?:\\<\\|/\\)\\([a-z]+[A-Z]+[a-zA-Z0-9$]*\\>\\)" 1 'clojure-interop-method-face)
  731. ;; #_ and (comment ...) macros.
  732. (clojure--search-comment-macro 1 font-lock-comment-face t)
  733. ;; Highlight `code` marks, just like `elisp'.
  734. (,(rx "`" (group-n 1 (optional "#'")
  735. (+ (or (syntax symbol) (syntax word)))) "`")
  736. (1 'font-lock-constant-face prepend))
  737. ;; Highlight escaped characters in strings.
  738. (clojure-font-lock-escaped-chars 0 'bold prepend)
  739. ;; Highlight grouping constructs in regular expressions
  740. (clojure-font-lock-regexp-groups
  741. (1 'font-lock-regexp-grouping-construct prepend))))
  742. "Default expressions to highlight in Clojure mode.")
  743. (defun clojure-font-lock-syntactic-face-function (state)
  744. "Find and highlight text with a Clojure-friendly syntax table.
  745. This function is passed to `font-lock-syntactic-face-function',
  746. which is called with a single parameter, STATE (which is, in
  747. turn, returned by `parse-partial-sexp' at the beginning of the
  748. highlighted region)."
  749. (if (nth 3 state)
  750. ;; This might be a (doc)string or a |...| symbol.
  751. (let ((startpos (nth 8 state)))
  752. (if (eq (char-after startpos) ?|)
  753. ;; This is not a string, but a |...| symbol.
  754. nil
  755. (let* ((listbeg (nth 1 state))
  756. (firstsym (and listbeg
  757. (save-excursion
  758. (goto-char listbeg)
  759. (and (looking-at "([ \t\n]*\\(\\(\\sw\\|\\s_\\)+\\)")
  760. (match-string 1)))))
  761. (docelt (and firstsym
  762. (function-get (intern-soft firstsym)
  763. lisp-doc-string-elt-property))))
  764. (if (and docelt
  765. ;; It's a string in a form that can have a docstring.
  766. ;; Check whether it's in docstring position.
  767. (save-excursion
  768. (when (functionp docelt)
  769. (goto-char (match-end 1))
  770. (setq docelt (funcall docelt)))
  771. (goto-char listbeg)
  772. (forward-char 1)
  773. (condition-case nil
  774. (while (and (> docelt 0) (< (point) startpos)
  775. (progn (forward-sexp 1) t))
  776. ;; ignore metadata and type hints
  777. (unless (looking-at "[ \n\t]*\\(\\^[A-Z:].+\\|\\^?{.+\\)")
  778. (setq docelt (1- docelt))))
  779. (error nil))
  780. (and (zerop docelt) (<= (point) startpos)
  781. (progn (forward-comment (point-max)) t)
  782. (= (point) (nth 8 state)))))
  783. font-lock-doc-face
  784. font-lock-string-face))))
  785. font-lock-comment-face))
  786. (defun clojure-font-lock-setup ()
  787. "Configures font-lock for editing Clojure code."
  788. (setq-local font-lock-multiline t)
  789. (add-to-list 'font-lock-extend-region-functions
  790. #'clojure-font-lock-extend-region-def t)
  791. (setq font-lock-defaults
  792. '(clojure-font-lock-keywords ; keywords
  793. nil nil
  794. (("+-*/.<>=!?$%_&:" . "w")) ; syntax alist
  795. nil
  796. (font-lock-mark-block-function . mark-defun)
  797. (font-lock-syntactic-face-function
  798. . clojure-font-lock-syntactic-face-function))))
  799. (defun clojure-font-lock-def-at-point (point)
  800. "Range between the top-most def* and the fourth element after POINT.
  801. Note that this means that there is no guarantee of proper font
  802. locking in def* forms that are not at top level."
  803. (goto-char point)
  804. (condition-case nil
  805. (beginning-of-defun)
  806. (error nil))
  807. (let ((beg-def (point)))
  808. (when (and (not (= point beg-def))
  809. (looking-at "(def"))
  810. (condition-case nil
  811. (progn
  812. ;; move forward as much as possible until failure (or success)
  813. (forward-char)
  814. (dotimes (_ 4)
  815. (forward-sexp)))
  816. (error nil))
  817. (cons beg-def (point)))))
  818. (defun clojure-font-lock-extend-region-def ()
  819. "Set region boundaries to include the first four elements of def* forms."
  820. (let ((changed nil))
  821. (let ((def (clojure-font-lock-def-at-point font-lock-beg)))
  822. (when def
  823. (cl-destructuring-bind (def-beg . def-end) def
  824. (when (and (< def-beg font-lock-beg)
  825. (< font-lock-beg def-end))
  826. (setq font-lock-beg def-beg
  827. changed t)))))
  828. (let ((def (clojure-font-lock-def-at-point font-lock-end)))
  829. (when def
  830. (cl-destructuring-bind (def-beg . def-end) def
  831. (when (and (< def-beg font-lock-end)
  832. (< font-lock-end def-end))
  833. (setq font-lock-end def-end
  834. changed t)))))
  835. changed))
  836. (defun clojure--font-locked-as-string-p (&optional regexp)
  837. "Non-nil if the char before point is font-locked as a string.
  838. If REGEXP is non-nil, also check whether current string is
  839. preceeded by a #."
  840. (let ((face (get-text-property (1- (point)) 'face)))
  841. (and (or (and (listp face)
  842. (memq 'font-lock-string-face face))
  843. (eq 'font-lock-string-face face))
  844. (or (clojure-string-start t)
  845. (unless regexp
  846. (clojure-string-start nil))))))
  847. (defun clojure-font-lock-escaped-chars (bound)
  848. "Highlight \escaped chars in strings.
  849. BOUND denotes a buffer position to limit the search."
  850. (let ((found nil))
  851. (while (and (not found)
  852. (re-search-forward "\\\\." bound t))
  853. (setq found (clojure--font-locked-as-string-p)))
  854. found))
  855. (defun clojure-font-lock-regexp-groups (bound)
  856. "Highlight grouping constructs in regular expression.
  857. BOUND denotes the maximum number of characters (relative to the
  858. point) to check."
  859. (let ((found nil))
  860. (while (and (not found)
  861. (re-search-forward (eval-when-compile
  862. (concat
  863. ;; A group may start using several alternatives:
  864. "\\(\\(?:"
  865. ;; 1. (? special groups
  866. "(\\?\\(?:"
  867. ;; a) non-capturing group (?:X)
  868. ;; b) independent non-capturing group (?>X)
  869. ;; c) zero-width positive lookahead (?=X)
  870. ;; d) zero-width negative lookahead (?!X)
  871. "[:=!>]\\|"
  872. ;; e) zero-width positive lookbehind (?<=X)
  873. ;; f) zero-width negative lookbehind (?<!X)
  874. "<[=!]\\|"
  875. ;; g) named capturing group (?<name>X)
  876. "<[[:alnum:]]+>"
  877. "\\)\\|" ;; end of special groups
  878. ;; 2. normal capturing groups (
  879. ;; 3. we also highlight alternative
  880. ;; separarators |, and closing parens )
  881. "[|()]"
  882. "\\)\\)"))
  883. bound t))
  884. (setq found (clojure--font-locked-as-string-p 'regexp)))
  885. found))
  886. ;; Docstring positions
  887. (put 'ns 'clojure-doc-string-elt 2)
  888. (put 'def 'clojure-doc-string-elt 2)
  889. (put 'defn 'clojure-doc-string-elt 2)
  890. (put 'defn- 'clojure-doc-string-elt 2)
  891. (put 'defmulti 'clojure-doc-string-elt 2)
  892. (put 'defmacro 'clojure-doc-string-elt 2)
  893. (put 'definline 'clojure-doc-string-elt 2)
  894. (put 'defprotocol 'clojure-doc-string-elt 2)
  895. ;;; Vertical alignment
  896. (defcustom clojure-align-forms-automatically nil
  897. "If non-nil, vertically align some forms automatically.
  898. Automatically means it is done as part of indenting code. This
  899. applies to binding forms (`clojure-align-binding-forms'), to cond
  900. forms (`clojure-align-cond-forms') and to map literals. For
  901. instance, selecting a map a hitting \\<clojure-mode-map>`\\[indent-for-tab-command]'
  902. will align the values like this:
  903. {:some-key 10
  904. :key2 20}"
  905. :package-version '(clojure-mode . "5.1")
  906. :safe #'booleanp
  907. :type 'boolean)
  908. (defcustom clojure-align-binding-forms
  909. '("let" "when-let" "when-some" "if-let" "if-some" "binding" "loop"
  910. "doseq" "for" "with-open" "with-local-vars" "with-redefs")
  911. "List of strings matching forms that have binding forms."
  912. :package-version '(clojure-mode . "5.1")
  913. :safe #'listp
  914. :type '(repeat string))
  915. (defcustom clojure-align-cond-forms '("condp" "cond" "cond->" "cond->>" "case" "are")
  916. "List of strings identifying cond-like forms."
  917. :package-version '(clojure-mode . "5.1")
  918. :safe #'listp
  919. :type '(repeat string))
  920. (defun clojure--position-for-alignment ()
  921. "Non-nil if the sexp around point should be automatically aligned.
  922. This function expects to be called immediately after an
  923. open-brace or after the function symbol in a function call.
  924. First check if the sexp around point is a map literal, or is a
  925. call to one of the vars listed in `clojure-align-cond-forms'. If
  926. it isn't, return nil. If it is, return non-nil and place point
  927. immediately before the forms that should be aligned.
  928. For instance, in a map literal point is left immediately before
  929. the first key; while, in a let-binding, point is left inside the
  930. binding vector and immediately before the first binding
  931. construct."
  932. ;; Are we in a map?
  933. (or (and (eq (char-before) ?{)
  934. (not (eq (char-before (1- (point))) ?\#)))
  935. ;; Are we in a cond form?
  936. (let* ((fun (car (member (thing-at-point 'symbol) clojure-align-cond-forms)))
  937. (method (and fun (clojure--get-indent-method fun)))
  938. ;; The number of special arguments in the cond form is
  939. ;; the number of sexps we skip before aligning.
  940. (skip (cond ((numberp method) method)
  941. ((null method) 0)
  942. ((sequencep method) (elt method 0)))))
  943. (when (and fun (numberp skip))
  944. (clojure-forward-logical-sexp skip)
  945. (comment-forward (point-max))
  946. fun)) ; Return non-nil (the var name).
  947. ;; Are we in a let-like form?
  948. (when (member (thing-at-point 'symbol)
  949. clojure-align-binding-forms)
  950. ;; Position inside the binding vector.
  951. (clojure-forward-logical-sexp)
  952. (backward-sexp)
  953. (when (eq (char-after) ?\[)
  954. (forward-char 1)
  955. (comment-forward (point-max))
  956. ;; Return non-nil.
  957. t))))
  958. (defun clojure--find-sexp-to-align (end)
  959. "Non-nil if there's a sexp ahead to be aligned before END.
  960. Place point as in `clojure--position-for-alignment'."
  961. ;; Look for a relevant sexp.
  962. (let ((found))
  963. (while (and (not found)
  964. (search-forward-regexp
  965. (concat "{\\|(" (regexp-opt
  966. (append clojure-align-binding-forms
  967. clojure-align-cond-forms)
  968. 'symbols))
  969. end 'noerror))
  970. (let ((ppss (syntax-ppss)))
  971. ;; If we're in a string or comment.
  972. (unless (or (elt ppss 3)
  973. (elt ppss 4))
  974. ;; Only stop looking if we successfully position
  975. ;; the point.
  976. (setq found (clojure--position-for-alignment)))))
  977. found))
  978. (defun clojure--search-whitespace-after-next-sexp (&optional bound _noerror)
  979. "Move point after all whitespace after the next sexp.
  980. Set the match data group 1 to be this region of whitespace and
  981. return point."
  982. (unwind-protect
  983. (ignore-errors
  984. (clojure-forward-logical-sexp 1)
  985. (search-forward-regexp "\\([,\s\t]*\\)" bound)
  986. (pcase (syntax-after (point))
  987. ;; End-of-line, try again on next line.
  988. (`(12) (clojure--search-whitespace-after-next-sexp bound))
  989. ;; Closing paren, stop here.
  990. (`(5 . ,_) nil)
  991. ;; Anything else is something to align.
  992. (_ (point))))
  993. (when (and bound (> (point) bound))
  994. (goto-char bound))))
  995. (defun clojure-align (beg end)
  996. "Vertically align the contents of the sexp around point.
  997. If region is active, align it. Otherwise, align everything in the
  998. current \"top-level\" sexp.
  999. When called from lisp code align everything between BEG and END."
  1000. (interactive (if (use-region-p)
  1001. (list (region-beginning) (region-end))
  1002. (save-excursion
  1003. (let ((end (progn (end-of-defun)
  1004. (point))))
  1005. (clojure-backward-logical-sexp)
  1006. (list (point) end)))))
  1007. (setq end (copy-marker end))
  1008. (save-excursion
  1009. (goto-char beg)
  1010. (while (clojure--find-sexp-to-align end)
  1011. (let ((sexp-end (save-excursion
  1012. (backward-up-list)
  1013. (forward-sexp 1)
  1014. (point-marker)))
  1015. (clojure-align-forms-automatically nil)
  1016. (count 1))
  1017. ;; For some bizarre reason, we need to `align-region' once for each
  1018. ;; group.
  1019. (save-excursion
  1020. (while (search-forward-regexp "^ *\n" sexp-end 'noerror)
  1021. (cl-incf count)))
  1022. (dotimes (_ count)
  1023. (align-region (point) sexp-end nil
  1024. '((clojure-align (regexp . clojure--search-whitespace-after-next-sexp)
  1025. (group . 1)
  1026. (separate . "^ *$")
  1027. (repeat . t)))
  1028. nil))
  1029. ;; Reindent after aligning because of #360.
  1030. (indent-region (point) sexp-end)))))
  1031. ;;; Indentation
  1032. (defun clojure-indent-region (beg end)
  1033. "Like `indent-region', but also maybe align forms.
  1034. Forms between BEG and END are aligned according to
  1035. `clojure-align-forms-automatically'."
  1036. (prog1 (let ((indent-region-function nil))
  1037. (indent-region beg end))
  1038. (when clojure-align-forms-automatically
  1039. (condition-case nil
  1040. (clojure-align beg end)
  1041. (scan-error nil)))))
  1042. (defun clojure-indent-line ()
  1043. "Indent current line as Clojure code."
  1044. (if (clojure-in-docstring-p)
  1045. (save-excursion
  1046. (beginning-of-line)
  1047. (when (and (looking-at "^\\s-*")
  1048. (<= (string-width (match-string-no-properties 0))
  1049. (string-width (clojure-docstring-fill-prefix))))
  1050. (replace-match (clojure-docstring-fill-prefix))))
  1051. (lisp-indent-line)))
  1052. (defvar clojure-get-indent-function nil
  1053. "Function to get the indent spec of a symbol.
  1054. This function should take one argument, the name of the symbol as
  1055. a string. This name will be exactly as it appears in the buffer,
  1056. so it might start with a namespace alias.
  1057. This function is analogous to the `clojure-indent-function'
  1058. symbol property, and its return value should match one of the
  1059. allowed values of this property. See `clojure-indent-function'
  1060. for more information.")
  1061. (defun clojure--get-indent-method (function-name)
  1062. "Return the indent spec for the symbol named FUNCTION-NAME.
  1063. FUNCTION-NAME is a string. If it contains a `/', also try only
  1064. the part after the `/'.
  1065. Look for a spec using `clojure-get-indent-function', then try the
  1066. `clojure-indent-function' and `clojure-backtracking-indent'
  1067. symbol properties."
  1068. (or (when (functionp clojure-get-indent-function)

Large files files are truncated, but you can click here to view the full file