|
| 1 | +;;; cider-browse-ns.el --- CIDER namespace browser |
| 2 | + |
| 3 | +;; Copyright © 2014 John Andrews |
| 4 | + |
| 5 | +;; Author: John Andrews <[email protected]> |
| 6 | + |
| 7 | +;; This program is free software: you can redistribute it and/or modify |
| 8 | +;; it under the terms of the GNU General Public License as published by |
| 9 | +;; the Free Software Foundation, either version 3 of the License, or |
| 10 | +;; (at your option) any later version. |
| 11 | + |
| 12 | +;; This program is distributed in the hope that it will be useful, |
| 13 | +;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +;; GNU General Public License for more details. |
| 16 | + |
| 17 | +;; You should have received a copy of the GNU General Public License |
| 18 | +;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;; This file is not part of GNU Emacs. |
| 21 | + |
| 22 | +;;; Commentary: |
| 23 | + |
| 24 | +;; (cider-browse-ns) |
| 25 | +;; Display a list of all vars in a namespace. |
| 26 | +;; Pressing <enter> will take you to the cider-doc buffer for that var. |
| 27 | +;; Pressing ^ will take you to a list of all namespaces (akin to dired mode) |
| 28 | + |
| 29 | +;; (cider-browse-ns-all) |
| 30 | +;; Explore clojure namespaces by browsing a list of all namespaces. |
| 31 | +;; Pressing enter expands into a list of that namespace's vars as if by |
| 32 | +;; executing the command (cider-browse-ns "my.ns") |
| 33 | + |
| 34 | +;;; Code: |
| 35 | + |
| 36 | +(require 'cider-repl) |
| 37 | +(require 'cider-client) |
| 38 | +(require 'cider-interaction) |
| 39 | + |
| 40 | +(defvar cider-browse-ns-buffer "*Browse NS*") |
| 41 | +(defvar cider-browse-ns-current-ns nil) |
| 42 | + |
| 43 | +(make-variable-buffer-local 'cider-browse-ns-current-ns) |
| 44 | + |
| 45 | + |
| 46 | +;; Utility Functions |
| 47 | + |
| 48 | +(defun cider-browse-ns-properties (text) |
| 49 | + "Decorate TEXT with a clickable keymap and function face." |
| 50 | + (propertize text |
| 51 | + 'font-lock-face 'font-lock-function-name-face |
| 52 | + 'mouse-face 'highlight |
| 53 | + 'keymap cider-browse-ns-mouse-map)) |
| 54 | + |
| 55 | + |
| 56 | +;; Mode Definition |
| 57 | + |
| 58 | +(defvar cider-browse-ns-mode-map |
| 59 | + (let ((map (make-sparse-keymap))) |
| 60 | + (set-keymap-parent map cider-popup-buffer-mode-map) |
| 61 | + (define-key map [return] 'cider-browse-ns-operate-on-point) |
| 62 | + (define-key map "^" 'cider-browse-ns-all) |
| 63 | + (define-key map "n" 'next-line) |
| 64 | + (define-key map "p" 'previous-line) |
| 65 | + map)) |
| 66 | + |
| 67 | +(define-derived-mode cider-browse-ns-mode fundamental-mode "browse-ns" |
| 68 | + "Major mode for browsing Clojure namespaces. |
| 69 | +
|
| 70 | +\\{cider-browse-ns-mode-map}" |
| 71 | + (set-syntax-table clojure-mode-syntax-table) |
| 72 | + (setq buffer-read-only t) |
| 73 | + (setq-local electric-indent-chars nil) |
| 74 | + (setq-local truncate-lines t) |
| 75 | + (setq-local cider-browse-ns-current-ns nil)) |
| 76 | + |
| 77 | +(defun cider-browse-ns-list (buffer title items) |
| 78 | + "Reset contents of BUFFER. Then display TITLE at the top and ITEMS are indented underneath." |
| 79 | + (with-current-buffer buffer |
| 80 | + (cider-browse-ns-mode) |
| 81 | + (let ((inhibit-read-only t)) |
| 82 | + (erase-buffer) |
| 83 | + (insert (propertize title 'font-lock-face 'cider-doc-strong-face)) |
| 84 | + (newline) |
| 85 | + (dolist (item items) |
| 86 | + (insert " " item) |
| 87 | + (newline)) |
| 88 | + (goto-char (point-min))))) |
| 89 | + |
| 90 | +(defvar cider-browse-ns-mouse-map (make-sparse-keymap)) |
| 91 | +(define-key cider-browse-ns-mouse-map [mouse-1] 'cider-browse-ns-handle-mouse) |
| 92 | + |
| 93 | + |
| 94 | +;; Interactive Functions |
| 95 | + |
| 96 | +;;;###autoload |
| 97 | +(defun cider-browse-ns (namespace) |
| 98 | + "List all NAMESPACE's vars in BUFFER." |
| 99 | + (interactive (list (completing-read "Browse namespace: " (cider--all-ns)))) |
| 100 | + (with-current-buffer (cider-popup-buffer cider-browse-ns-buffer t) |
| 101 | + (let* ((form "(sort (map name (keys (ns-publics (quote %s)))))") |
| 102 | + (vars (cider-eval-and-get-value (format form namespace)))) |
| 103 | + (cider-browse-ns-list (current-buffer) |
| 104 | + namespace |
| 105 | + (mapcar (lambda (var) |
| 106 | + (format "/%s" |
| 107 | + (cider-browse-ns-properties var))) |
| 108 | + vars)) |
| 109 | + (setq-local cider-browse-ns-current-ns namespace)))) |
| 110 | + |
| 111 | +;;;###autoload |
| 112 | +(defun cider-browse-ns-all () |
| 113 | + "List all loaded namespaces in BUFFER." |
| 114 | + (interactive) |
| 115 | + (with-current-buffer (cider-popup-buffer cider-browse-ns-buffer t) |
| 116 | + (let ((names (cider-eval-and-get-value |
| 117 | + "(->> (all-ns) |
| 118 | + (map ns-name) |
| 119 | + (map name) |
| 120 | + (sort))"))) |
| 121 | + (cider-browse-ns-list (current-buffer) |
| 122 | + "All loaded namespaces" |
| 123 | + (mapcar (lambda (name) |
| 124 | + (cider-browse-ns-properties name)) |
| 125 | + names)) |
| 126 | + (setq-local cider-browse-ns-current-ns nil)))) |
| 127 | + |
| 128 | +(defun cider-browse-ns-operate-on-point () |
| 129 | + "Expand browser according to thing at current point." |
| 130 | + (interactive) |
| 131 | + (let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position)))) |
| 132 | + (cond |
| 133 | + ((= 1 (line-number-at-pos)) |
| 134 | + 'nothing-to-do) |
| 135 | + ((string-match " +/\\(.+\\)" line) |
| 136 | + (cider-doc-lookup (format "%s/%s" cider-browse-ns-current-ns (match-string 1 line)))) |
| 137 | + ('else |
| 138 | + (cider-browse-ns (replace-regexp-in-string " " "" line)))))) |
| 139 | + |
| 140 | +(defun cider-browse-ns-handle-mouse (event) |
| 141 | + "Handle mouse click EVENT." |
| 142 | + (interactive "e") |
| 143 | + (cider-browse-ns-operate-on-point)) |
| 144 | + |
| 145 | + |
| 146 | +(provide 'cider-browse-ns) |
| 147 | + |
| 148 | +;;; cider-browse-ns.el ends here |
0 commit comments