dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

cider-selector.el (6738B)


      1 ;;; cider-selector.el --- Buffer selection command inspired by SLIME's selector -*- lexical-binding: t -*-
      2 
      3 ;; Copyright © 2012-2013 Tim King, Phil Hagelberg, Bozhidar Batsov
      4 ;; Copyright © 2013-2023 Bozhidar Batsov, Artur Malabarba and CIDER contributors
      5 ;;
      6 ;; Author: Tim King <kingtim@gmail.com>
      7 ;;         Phil Hagelberg <technomancy@gmail.com>
      8 ;;         Bozhidar Batsov <bozhidar@batsov.dev>
      9 ;;         Artur Malabarba <bruce.connor.am@gmail.com>
     10 ;;         Hugo Duncan <hugo@hugoduncan.org>
     11 ;;         Steve Purcell <steve@sanityinc.com>
     12 
     13 ;; This program is free software: you can redistribute it and/or modify
     14 ;; it under the terms of the GNU General Public License as published by
     15 ;; the Free Software Foundation, either version 3 of the License, or
     16 ;; (at your option) any later version.
     17 
     18 ;; This program is distributed in the hope that it will be useful,
     19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     21 ;; GNU General Public License for more details.
     22 
     23 ;; You should have received a copy of the GNU General Public License
     24 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
     25 
     26 ;; This file is not part of GNU Emacs.
     27 
     28 ;;; Commentary:
     29 
     30 ;; Buffer selection command inspired by SLIME's selector.
     31 
     32 ;;; Code:
     33 
     34 (require 'cider-client)
     35 (require 'cider-eval)
     36 (require 'cider-scratch)
     37 (require 'cider-profile)
     38 
     39 (defconst cider-selector-help-buffer "*CIDER Selector Help*"
     40   "The name of the selector's help buffer.")
     41 
     42 (defvar cider-selector-methods nil
     43   "List of buffer-selection methods for the `cider-selector' command.
     44 Each element is a list (KEY DESCRIPTION FUNCTION).
     45 DESCRIPTION is a one-line description of what the key selects.")
     46 
     47 (defvar cider-selector-other-window nil
     48   "If non-nil use `switch-to-buffer-other-window'.
     49 Not meant to be set by users.  It's used internally
     50 by `cider-selector'.")
     51 
     52 (defun cider-selector--recently-visited-buffer (mode &optional consider-visible-p)
     53   "Return the most recently visited buffer, deriving its `major-mode' from MODE.
     54 CONSIDER-VISIBLE-P will allow handling of visible windows as well.
     55 First pass only considers buffers that are not already visible.
     56 Second pass will attempt one of visible ones for scenarios where the window
     57 is visible, but not focused."
     58   (cl-loop for buffer in (buffer-list)
     59            when (and (with-current-buffer buffer
     60                        (derived-mode-p mode))
     61                      ;; names starting with space are considered hidden by Emacs
     62                      (not (string-match-p "^ " (buffer-name buffer)))
     63                      (or consider-visible-p
     64                          (null (get-buffer-window buffer 'visible))))
     65            return buffer
     66            finally (if consider-visible-p
     67                        (error "Can't find unshown buffer in %S" mode)
     68                      (cider-selector--recently-visited-buffer mode t))))
     69 
     70 ;;;###autoload
     71 (defun cider-selector (&optional other-window)
     72   "Select a new buffer by type, indicated by a single character.
     73 The user is prompted for a single character indicating the method by
     74 which to choose a new buffer.  The `?' character describes the
     75 available methods.  OTHER-WINDOW provides an optional target.
     76 See `def-cider-selector-method' for defining new methods."
     77   (interactive)
     78   (message "Select [%s]: "
     79            (apply #'string (mapcar #'car cider-selector-methods)))
     80   (let* ((cider-selector-other-window other-window)
     81          (ch (save-window-excursion
     82                (select-window (minibuffer-window))
     83                (read-char)))
     84          (method (cl-find ch cider-selector-methods :key #'car)))
     85     (cond (method
     86            (funcall (cl-caddr method)))
     87           (t
     88            (message "No method for character: ?\\%c" ch)
     89            (ding)
     90            (sleep-for 1)
     91            (discard-input)
     92            (cider-selector)))))
     93 
     94 (defmacro def-cider-selector-method (key description &rest body)
     95   "Define a new `cider-select' buffer selection method.
     96 KEY is the key the user will enter to choose this method.
     97 
     98 DESCRIPTION is a one-line sentence describing how the method
     99 selects a buffer.
    100 
    101 BODY is a series of forms which are evaluated when the selector
    102 is chosen.  The returned buffer is selected with
    103 `switch-to-buffer'."
    104   (let ((method `(lambda ()
    105                    (let ((buffer (progn ,@body)))
    106                      (cond ((not (and buffer (get-buffer buffer)))
    107                             (message "No such buffer: %S" buffer)
    108                             (ding))
    109                            ((get-buffer-window buffer)
    110                             (select-window (get-buffer-window buffer)))
    111                            (cider-selector-other-window
    112                             (switch-to-buffer-other-window buffer))
    113                            (t
    114                             (switch-to-buffer buffer)))))))
    115     `(setq cider-selector-methods
    116            (cl-sort (cons (list ,key ,description ,method)
    117                           (cl-remove ,key cider-selector-methods :key #'car))
    118                     #'< :key #'car))))
    119 
    120 (def-cider-selector-method ?? "Selector help buffer."
    121   (ignore-errors (kill-buffer cider-selector-help-buffer))
    122   (with-current-buffer (get-buffer-create cider-selector-help-buffer)
    123     (insert "CIDER Selector Methods:\n\n")
    124     (cl-loop for (key line nil) in cider-selector-methods
    125              do (insert (format "%c:\t%s\n" key line)))
    126     (goto-char (point-min))
    127     (help-mode)
    128     (display-buffer (current-buffer) t))
    129   (cider-selector)
    130   (current-buffer))
    131 
    132 (cl-pushnew (list ?4 "Select in other window" (lambda () (cider-selector t)))
    133             cider-selector-methods :key #'car)
    134 
    135 (def-cider-selector-method ?c
    136   "Most recently visited clojure-mode buffer."
    137   (cider-selector--recently-visited-buffer 'clojure-mode))
    138 
    139 (def-cider-selector-method ?e
    140   "Most recently visited emacs-lisp-mode buffer."
    141   (cider-selector--recently-visited-buffer 'emacs-lisp-mode))
    142 
    143 (def-cider-selector-method ?q "Abort."
    144   (top-level))
    145 
    146 (def-cider-selector-method ?r
    147   "Current REPL buffer or as a fallback, the most recently
    148 visited cider-repl-mode buffer."
    149   (or (cider-current-repl)
    150       (cider-selector--recently-visited-buffer 'cider-repl-mode)))
    151 
    152 (def-cider-selector-method ?m
    153   "Current connection's *nrepl-messages* buffer."
    154   (nrepl-messages-buffer (cider-current-repl)))
    155 
    156 (def-cider-selector-method ?x
    157   "*cider-error* buffer."
    158   cider-error-buffer)
    159 
    160 (def-cider-selector-method ?p
    161   "*cider-profile* buffer."
    162   cider-profile-buffer)
    163 
    164 (def-cider-selector-method ?d
    165   "*cider-doc* buffer."
    166   cider-doc-buffer)
    167 
    168 (def-cider-selector-method ?s
    169   "*cider-scratch* buffer."
    170   (cider-scratch-find-or-create-buffer))
    171 
    172 (provide 'cider-selector)
    173 
    174 ;;; cider-selector.el ends here