Klimi's new dotfiles with stow.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

577 lignes
21 KiB

il y a 5 ans
  1. ;;; cider-cheatsheet.el --- Quick reference for Clojure -*- lexical-binding: t -*-
  2. ;; Copyright © 2019 Kris Jenkins, Bozhidar Batsov and CIDER contributors
  3. ;;
  4. ;; Author: Kris Jenkins <krisajenkins@gmail.com>
  5. ;; This program is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;; This file is not part of GNU Emacs.
  16. ;;; Commentary:
  17. ;; A quick reference system for Clojure. Fast, searchable & available offline.
  18. ;; Mostly taken from Kris Jenkins' `clojure-cheatsheet'
  19. ;; See: https://github.com/clojure-emacs/clojure-cheatsheet
  20. ;;; Code:
  21. (require 'cider-doc)
  22. (require 'seq)
  23. (defconst cider-cheatsheet-hierarchy
  24. '(("Primitives"
  25. ("Numbers"
  26. ("Arithmetic"
  27. (clojure.core + - * / quot rem mod dec inc max min))
  28. ("Compare"
  29. (clojure.core = == not= < > <= >= compare))
  30. ("Bitwise"
  31. (clojure.core bit-and bit-and-not bit-clear bit-flip bit-not bit-or bit-set bit-shift-left bit-shift-right bit-test bit-xor unsigned-bit-shift-right))
  32. ("Cast"
  33. (clojure.core byte short long int float double bigdec bigint biginteger num rationalize))
  34. ("Test"
  35. (clojure.core nil? some? identical? zero? pos? neg? even? odd?))
  36. ("Random"
  37. (clojure.core rand rand-int))
  38. ("BigDecimal"
  39. (clojure.core with-precision))
  40. ("Ratios"
  41. (clojure.core numerator denominator ratio?))
  42. ("Arbitrary Precision Arithmetic"
  43. (clojure.core +\' -\' *\' inc\' dec\'))
  44. ("Unchecked"
  45. (clojure.core *unchecked-math*
  46. unchecked-add
  47. unchecked-add-int
  48. unchecked-byte
  49. unchecked-char
  50. unchecked-dec
  51. unchecked-dec-int
  52. unchecked-divide-int
  53. unchecked-double
  54. unchecked-float
  55. unchecked-inc
  56. unchecked-inc-int
  57. unchecked-int
  58. unchecked-long
  59. unchecked-multiply
  60. unchecked-multiply-int
  61. unchecked-negate
  62. unchecked-negate-int
  63. unchecked-remainder-int
  64. unchecked-short
  65. unchecked-subtract
  66. unchecked-subtract-int)))
  67. ("Strings"
  68. ("Create"
  69. (clojure.core str format))
  70. ("Use"
  71. (clojure.core count get subs compare)
  72. (clojure.string join escape split split-lines replace replace-first reverse re-quote-replacement index-of last-index-of starts-with? ends-with? includes?))
  73. ("Regex"
  74. (clojure.core re-find re-seq re-matches re-pattern re-matcher re-groups)
  75. (clojure.string replace replace-first re-quote-replacement))
  76. ("Letters"
  77. (clojure.string capitalize lower-case upper-case))
  78. ("Trim"
  79. (clojure.string trim trim-newline triml trimr))
  80. ("Test"
  81. (clojure.core char char? string?)
  82. (clojure.string blank?)))
  83. ("Other"
  84. ("Characters"
  85. (clojure.core char char-name-string char-escape-string))
  86. ("Keywords"
  87. (clojure.core keyword keyword? find-keyword))
  88. ("Symbols"
  89. (clojure.core symbol symbol? gensym))
  90. ("Data Readers"
  91. (clojure.core *data-readers* default-data-readers *default-data-reader-fn*))))
  92. ("Collections"
  93. ("Generic Ops"
  94. (clojure.core count bounded-count empty not-empty into conj))
  95. ("Tree Walking"
  96. (clojure.walk walk prewalk prewalk-demo prewalk-replace postwalk postwalk-demo postwalk-replace keywordize-keys stringify-keys))
  97. ("Content tests"
  98. (clojure.core distinct? empty? every? not-every? some not-any?))
  99. ("Capabilities"
  100. (clojure.core sequential? associative? sorted? counted? reversible?))
  101. ("Type tests"
  102. (clojure.core type class coll? list? vector? set? map? seq?
  103. number? integer? float? decimal? class? rational? ratio?
  104. chunked-seq? reduced? special-symbol? record?))
  105. ("Lists"
  106. ("Create"
  107. (clojure.core list list*))
  108. ("Examine"
  109. (clojure.core first nth peek))
  110. ("Change"
  111. (clojure.core cons conj rest pop)))
  112. ("Vectors"
  113. ("Create"
  114. (clojure.core vec vector vector-of))
  115. ("Examine"
  116. (clojure.core get peek))
  117. ("Change"
  118. (clojure.core assoc pop subvec replace conj rseq))
  119. ("Ops"
  120. (clojure.core mapv filterv reduce-kv)))
  121. ("Sets"
  122. ("Create"
  123. (clojure.core set hash-set sorted-set sorted-set-by))
  124. ("Examine"
  125. (clojure.core get contains?))
  126. ("Change"
  127. (clojure.core conj disj))
  128. ("Relational Algebra"
  129. (clojure.set join select project union difference intersection))
  130. ("Get map"
  131. (clojure.set index rename-keys rename map-invert))
  132. ("Test"
  133. (clojure.set subset? superset?))
  134. ("Sorted Sets"
  135. (clojure.core rseq subseq rsubseq)))
  136. ("Maps"
  137. ("Create"
  138. (clojure.core hash-map array-map zipmap sorted-map sorted-map-by bean frequencies group-by))
  139. ("Examine"
  140. (clojure.core get get-in contains? find keys vals map-entry?))
  141. ("Change"
  142. (clojure.core assoc assoc-in dissoc merge merge-with select-keys update update-in))
  143. ("Entry"
  144. (clojure.core key val))
  145. ("Sorted Maps"
  146. (clojure.core rseq subseq rsubseq)))
  147. ("Hashes"
  148. (clojure.core hash hash-ordered-coll hash-unordered-coll mix-collection-hash))
  149. ("Volatiles"
  150. (clojure.core volatile! volatile? vreset! vswap!)))
  151. ("Functions"
  152. ("Create"
  153. (clojure.core fn defn defn- definline identity constantly comp complement partial juxt memfn memoize fnil every-pred some-fn trampoline))
  154. ("Call"
  155. (clojure.core -> ->> some-> some->> as-> cond-> cond->>))
  156. ("Test"
  157. (clojure.core fn? ifn?)))
  158. ("Transducers"
  159. ("Create"
  160. (clojure.core cat dedupe distinct drop drop-while filter halt-when interpose keep keep-indexed map map-indexed mapcat partition-all partition-by random-sample remove replace take take-nth take-while))
  161. ("Call"
  162. (clojure.core ->Eduction eduction into sequence transduce completing run!))
  163. ("Early Termination"
  164. (clojure.core deref reduced reduced? ensure-reduced unreduced)))
  165. ("Spec"
  166. ("Operations"
  167. (clojure.spec.alpha valid? conform unform explain explain-data explain-str explain-out form describe assert check-asserts check-asserts?))
  168. ("Generator Ops"
  169. (clojure.spec.alpha gen exercise exercise-fn))
  170. ("Defn & Registry"
  171. (clojure.spec.alpha def fdef registry get-spec spec? spec with-gen))
  172. ("Logical"
  173. (clojure.spec.alpha and or))
  174. ("Collection"
  175. (clojure.spec.alpha coll-of map-of every every-kv keys merge))
  176. ("Regex "
  177. (clojure.spec.alpha cat alt * + \? & keys*))
  178. ("Range"
  179. (clojure.spec.alpha int-in inst-in double-in int-in-range? inst-in-range?))
  180. ("Custom Explain"
  181. (clojure.spec.alpha explain-printer *explain-out*))
  182. ("Other"
  183. (clojure.spec.alpha nilable multi-spec fspec conformer))
  184. ("Predicates with test.check generators"
  185. ("Numbers"
  186. (clojure.core number? rational? integer? ratio? decimal? float? zero? double? int? nat-int? neg-int? pos-int?))
  187. ("Symbols & Keywords"
  188. (clojure.core keyword? symbol? ident? qualified-ident? qualified-keyword? qualified-symbol? simple-ident? simple-keyword? simple-symbol?))
  189. ("Scalars"
  190. (clojure.core string? true? false? nil? some? boolean? bytes? inst? uri? uuid?))
  191. ("Collections"
  192. (clojure.core list? map? set? vector? associative? coll? sequential? seq? empty? indexed? seqable?))
  193. ("Other"
  194. (clojure.core any?))))
  195. ("Other"
  196. ("XML"
  197. (clojure.core xml-seq)
  198. (clojure.xml parse))
  199. ("REPL"
  200. (clojure.core *1 *2 *3 *e *print-dup* *print-length* *print-level* *print-meta* *print-readably*))
  201. ("EDN"
  202. (clojure.edn read read-string))
  203. ("Compiling Code & Class Generation"
  204. (clojure.core *compile-files* *compile-path* *file* *warn-on-reflection* compile gen-class gen-interface loaded-libs test))
  205. ("Misc"
  206. (clojure.core eval force name *clojure-version* clojure-version *command-line-args*))
  207. ("Pretty Printing"
  208. (clojure.pprint pprint print-table pp *print-right-margin*))
  209. ("Browser / Shell"
  210. (clojure.java.browse browse-url)
  211. (clojure.java.shell sh with-sh-dir with-sh-env)))
  212. ("Vars & Global Environment"
  213. ("Def Variants"
  214. (:special def)
  215. (clojure.core defn defn- definline defmacro defmethod defmulti defonce defrecord))
  216. ("Interned Vars"
  217. (:special var)
  218. (clojure.core declare intern binding find-var))
  219. ("Var Objects"
  220. (clojure.core with-local-vars var-get var-set alter-var-root var?))
  221. ("Var Validators"
  222. (clojure.core set-validator! get-validator)))
  223. ("Reader Conditionals"
  224. (clojure.core reader-conditional reader-conditional? tagged-literal tagged-literal?))
  225. ("Abstractions"
  226. ("Protocols"
  227. (clojure.core defprotocol extend extend-type extend-protocol reify extends? satisfies? extenders))
  228. ("Records & Types"
  229. (clojure.core defrecord deftype))
  230. ("Multimethods"
  231. ("Define"
  232. (clojure.core defmulti defmethod))
  233. ("Dispatch"
  234. (clojure.core get-method methods))
  235. ("Remove"
  236. (clojure.core remove-method remove-all-methods))
  237. ("Prefer"
  238. (clojure.core prefer-method prefers))
  239. ("Relation"
  240. (clojure.core derive isa? parents ancestors descendants make-hierarchy))))
  241. ("Macros"
  242. ("Create"
  243. (clojure.core defmacro definline))
  244. ("Debug"
  245. (clojure.core macroexpand-1 macroexpand)
  246. (clojure.walk macroexpand-all))
  247. ("Branch"
  248. (clojure.core and or when when-not when-let when-first if-not if-let cond condp case))
  249. ("Loop"
  250. (clojure.core for doseq dotimes while))
  251. ("Arrange"
  252. (clojure.core .. doto ->))
  253. ("Scope"
  254. (clojure.core binding locking time)
  255. (clojure.core with-in-str with-local-vars with-open with-out-str with-precision with-redefs with-redefs-fn))
  256. ("Lazy"
  257. (clojure.core lazy-cat lazy-seq delay delay?))
  258. ("Doc"
  259. (clojure.core assert comment)
  260. (clojure.repl doc dir dir-fn source-fn)))
  261. ("Java Interop"
  262. ("General"
  263. (:special new set!)
  264. (clojure.core .. doto bean comparator enumeration-seq import iterator-seq memfn definterface supers bases))
  265. ("Cast"
  266. (clojure.core boolean byte short char int long float double bigdec bigint num cast biginteger))
  267. ("Exceptions"
  268. (:special throw try catch finally)
  269. (clojure.core ex-info ex-data Throwable->map StackTraceElement->vec)
  270. (clojure.repl pst))
  271. ("Arrays"
  272. ("Create"
  273. (clojure.core boolean-array byte-array double-array char-array float-array int-array long-array make-array object-array short-array to-array))
  274. ("Manipulate"
  275. (clojure.core aclone aget aset alength amap areduce aset-int aset-long aset-short aset-boolean aset-byte aset-char aset-double aset-float))
  276. ("Cast"
  277. (clojure.core booleans bytes chars doubles floats ints longs shorts)))
  278. ("Proxy"
  279. ("Create"
  280. (clojure.core proxy get-proxy-class construct-proxy init-proxy))
  281. ("Misc"
  282. (clojure.core proxy-mappings proxy-super update-proxy))))
  283. ("Namespaces"
  284. ("Current"
  285. (clojure.core *ns*))
  286. ("Create Switch"
  287. (clojure.core ns in-ns create-ns))
  288. ("Add"
  289. (clojure.core alias import intern refer refer-clojure))
  290. ("Find"
  291. (clojure.core all-ns find-ns))
  292. ("Examine"
  293. (clojure.core ns-aliases ns-imports ns-interns ns-map ns-name ns-publics ns-refers))
  294. ("From symbol"
  295. (clojure.core resolve namespace ns-resolve the-ns))
  296. ("Remove"
  297. (clojure.core ns-unalias ns-unmap remove-ns)))
  298. ("Loading"
  299. ("Load libs"
  300. (clojure.core require use import refer))
  301. ("List Loaded"
  302. (clojure.core loaded-libs))
  303. ("Load Misc"
  304. (clojure.core load load-file load-reader load-string)))
  305. ("Concurrency"
  306. ("Atoms"
  307. (clojure.core atom swap! swap-vals! reset! reset-vals! compare-and-set!))
  308. ("Futures"
  309. (clojure.core future future-call future-cancel future-cancelled? future-done? future?))
  310. ("Threads"
  311. (clojure.core bound-fn bound-fn* get-thread-bindings pop-thread-bindings push-thread-bindings))
  312. ("Misc"
  313. (clojure.core locking pcalls pvalues pmap seque promise deliver))
  314. ("Refs & Transactions"
  315. ("Create"
  316. (clojure.core ref))
  317. ("Examine"
  318. (clojure.core deref))
  319. ("Transaction"
  320. (clojure.core sync dosync io!))
  321. ("In Transaction"
  322. (clojure.core ensure ref-set alter commute))
  323. ("Validators"
  324. (clojure.core get-validator set-validator!))
  325. ("History"
  326. (clojure.core ref-history-count ref-max-history ref-min-history)))
  327. ("Agents & Asynchronous Actions"
  328. ("Create"
  329. (clojure.core agent))
  330. ("Examine"
  331. (clojure.core agent-error))
  332. ("Change State"
  333. (clojure.core send send-off restart-agent send-via set-agent-send-executor! set-agent-send-off-executor!))
  334. ("Block Waiting"
  335. (clojure.core await await-for))
  336. ("Ref Validators"
  337. (clojure.core get-validator set-validator!))
  338. ("Watchers"
  339. (clojure.core add-watch remove-watch))
  340. ("Thread Handling"
  341. (clojure.core shutdown-agents))
  342. ("Error"
  343. (clojure.core error-handler set-error-handler! error-mode set-error-mode!))
  344. ("Misc"
  345. (clojure.core *agent* release-pending-sends))))
  346. ("Sequences"
  347. ("Creating a Lazy Seq"
  348. ("From Collection"
  349. (clojure.core seq sequence keys vals rseq subseq rsubseq))
  350. ("From Producer Fn"
  351. (clojure.core lazy-seq repeatedly iterate))
  352. ("From Constant"
  353. (clojure.core repeat range))
  354. ("From Other"
  355. (clojure.core file-seq line-seq resultset-seq re-seq tree-seq xml-seq iterator-seq enumeration-seq))
  356. ("From Seq"
  357. (clojure.core keep keep-indexed)))
  358. ("Seq in, Seq out"
  359. ("Get shorter"
  360. (clojure.core distinct dedupe filter remove for))
  361. ("Get longer"
  362. (clojure.core cons conj concat lazy-cat mapcat cycle interleave interpose)))
  363. ("Tail-items"
  364. (clojure.core rest nthrest fnext nnext drop drop-while take-last for))
  365. ("Head-items"
  366. (clojure.core take take-nth take-while butlast drop-last for))
  367. ("Change"
  368. (clojure.core conj concat distinct flatten group-by partition partition-all partition-by split-at split-with filter remove replace shuffle random-sample))
  369. ("Rearrange"
  370. (clojure.core reverse sort sort-by compare))
  371. ("Process items"
  372. (clojure.core map pmap map-indexed mapcat for replace seque))
  373. ("Using a Seq"
  374. ("Extract item"
  375. (clojure.core first second last rest next ffirst nfirst fnext nnext nth nthnext rand-nth when-first max-key min-key))
  376. ("Construct coll"
  377. (clojure.core zipmap into reduce reductions set vec into-array to-array-2d))
  378. ("Pass to fn"
  379. (clojure.core apply))
  380. ("Search"
  381. (clojure.core some filter))
  382. ("Force evaluation"
  383. (clojure.core doseq dorun doall))
  384. ("Check for forced"
  385. (clojure.core realized?))))
  386. ("Zippers"
  387. ("Create"
  388. (clojure.zip zipper seq-zip vector-zip xml-zip))
  389. ("Get loc"
  390. (clojure.zip up down left right leftmost rightmost))
  391. ("Get seq"
  392. (clojure.zip lefts rights path children))
  393. ("Change"
  394. (clojure.zip make-node replace edit insert-child insert-left insert-right append-child remove))
  395. ("Move"
  396. (clojure.zip next prev))
  397. ("XML"
  398. (clojure.data.zip.xml attr attr= seq-test tag= text text= xml-> xml1->))
  399. ("Misc"
  400. (clojure.zip root node branch? end?)))
  401. ("Documentation"
  402. ("REPL"
  403. (clojure.repl doc find-doc apropos source pst)
  404. (clojure.java.javadoc javadoc)))
  405. ("Transients"
  406. ("Create"
  407. (clojure.core transient persistent!))
  408. ("Change"
  409. (clojure.core conj! pop! assoc! dissoc! disj!)))
  410. ("Misc"
  411. ("Compare"
  412. (clojure.core = == identical? not= not compare)
  413. (clojure.data diff))
  414. ("Test"
  415. (clojure.core true? false? nil? instance?)))
  416. ("IO"
  417. ("To/from ..."
  418. (clojure.core spit slurp))
  419. ("To *out*"
  420. (clojure.core pr prn print printf println newline)
  421. (clojure.pprint print-table))
  422. ("To writer"
  423. (clojure.pprint pprint cl-format))
  424. ("To string"
  425. (clojure.core format with-out-str pr-str prn-str print-str println-str))
  426. ("From *in*"
  427. (clojure.core read-line read))
  428. ("From reader"
  429. (clojure.core line-seq read))
  430. ("From string"
  431. (clojure.core read-string with-in-str))
  432. ("Open"
  433. (clojure.core with-open)
  434. (clojure.java.io reader writer input-stream output-stream))
  435. ("Interop"
  436. (clojure.java.io make-writer make-reader make-output-stream make-input-stream))
  437. ("Misc"
  438. (clojure.core flush file-seq *in* *out* *err*)
  439. (clojure.java.io file copy delete-file resource as-file as-url as-relative-path make-parents)))
  440. ("Metadata"
  441. (clojure.core meta with-meta alter-meta! reset-meta! vary-meta))
  442. ("Special Forms"
  443. (:special def if do quote var recur throw try monitor-enter monitor-exit)
  444. (clojure.core fn loop)
  445. ("Binding / Destructuring"
  446. (clojure.core let fn letfn defn defmacro loop for doseq if-let if-some when-let when-some)))
  447. ("Async"
  448. ("Main"
  449. (clojure.core.async go go-loop <! <!! >! >!! chan put! take take! close! timeout offer! poll! promise-chan))
  450. ("Choice"
  451. (clojure.core.async alt! alt!! alts! alts!! do-alts))
  452. ("Buffering"
  453. (clojure.core.async buffer dropping-buffer sliding-buffer unblocking-buffer?))
  454. ("Pipelines"
  455. (clojure.core.async pipeline pipeline-async pipeline-blocking))
  456. ("Threading"
  457. (clojure.core.async thread thread-call))
  458. ("Mixing"
  459. (clojure.core.async admix solo-mode mix unmix unmix-all toggle merge pipe unique))
  460. ("Multiples"
  461. (clojure.core.async mult tap untap untap-all))
  462. ("Publish/Subscribe"
  463. (clojure.core.async pub sub unsub unsub-all))
  464. ("Higher Order"
  465. (clojure.core.async filter< filter> map map< map> mapcat< mapcat> partition partition-by reduce remove< remove> split))
  466. ("Pre-Populate"
  467. (clojure.core.async into onto-chan to-chan)))
  468. ("Unit Tests"
  469. ("Defining"
  470. (clojure.test deftest deftest- testing is are))
  471. ("Running"
  472. (clojure.test run-tests run-all-tests test-vars))
  473. ("Fixtures"
  474. (clojure.test use-fixtures join-fixtures compose-fixtures))))
  475. "A data structure for Clojure cheatsheet information.
  476. It's a tree, where the head of each list determines the context of the rest
  477. of the list. The head may be:
  478. - A string, in which case it's a (sub)heading for the rest of the items.
  479. - A symbol, in which case it's the Clojure namespace of the symbols that
  480. follow it.
  481. - The keyword :special, in which case it's a Clojure special form
  482. - Any other keyword, in which case it's a typed item that will be passed
  483. through.
  484. Note that some Clojure symbols appear in more than once. This is entirely
  485. intentional. For instance, `map` belongs in the sections on collections
  486. and transducers.")
  487. (defun cider-cheatsheet--expand-vars (list)
  488. "Expand the symbols in LIST to fully-qualified var names.
  489. This list is supposed to have the following format:
  490. (my-ns var1 var2 var3)"
  491. (let ((ns (car list))
  492. (vars (cdr list)))
  493. (if (eq ns :special)
  494. (mapcar #'symbol-name vars)
  495. (mapcar (lambda (var) (format "%s/%s" ns var)) vars))))
  496. (defun cider-cheatsheet--select-var (var-list)
  497. "Expand the symbols in VAR-LIST to fully-qualified var names.
  498. The list can hold one or more lists inside - one per each namespace."
  499. (let ((namespaced-vars (seq-mapcat #'cider-cheatsheet--expand-vars
  500. (seq-remove (lambda (list)
  501. (eq (car list) :url))
  502. var-list))))
  503. (cider-doc-lookup (completing-read "Select var: " namespaced-vars))))
  504. ;;;###autoload
  505. (defun cider-cheatsheet ()
  506. "Navigate `cider-cheatsheet-hierarchy' with `completing-read'.
  507. When you make it to a Clojure var its doc buffer gets displayed."
  508. (interactive)
  509. (let ((cheatsheet-data cider-cheatsheet-hierarchy))
  510. (while (stringp (caar cheatsheet-data))
  511. (let* ((sections (mapcar #'car cheatsheet-data))
  512. (sel-section (completing-read "Select cheatsheet section: " sections))
  513. (section-data (seq-find (lambda (elem) (equal (car elem) sel-section)) cheatsheet-data)))
  514. (setq cheatsheet-data (cdr section-data))))
  515. (cider-cheatsheet--select-var cheatsheet-data)))
  516. (provide 'cider-cheatsheet)
  517. ;;; cider-cheatsheet.el ends here