dotemacs

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

cider-scratch.el (3533B)


      1 ;;; cider-scratch.el --- *scratch* buffer for Clojure -*- lexical-binding: t -*-
      2 
      3 ;; Copyright © 2014-2023 Bozhidar Batsov and CIDER contributors
      4 ;;
      5 ;; Author: Tim King <kingtim@gmail.com>
      6 ;;         Phil Hagelberg <technomancy@gmail.com>
      7 ;;         Bozhidar Batsov <bozhidar@batsov.dev>
      8 ;;         Artur Malabarba <bruce.connor.am@gmail.com>
      9 ;;         Hugo Duncan <hugo@hugoduncan.org>
     10 ;;         Steve Purcell <steve@sanityinc.com>
     11 
     12 ;; This program is free software: you can redistribute it and/or modify
     13 ;; it under the terms of the GNU General Public License as published by
     14 ;; the Free Software Foundation, either version 3 of the License, or
     15 ;; (at your option) any later version.
     16 
     17 ;; This program is distributed in the hope that it will be useful,
     18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 ;; GNU General Public License for more details.
     21 
     22 ;; You should have received a copy of the GNU General Public License
     23 ;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
     24 
     25 ;; This file is not part of GNU Emacs.
     26 
     27 ;;; Commentary:
     28 
     29 ;; Imitate Emacs's *scratch* buffer.
     30 
     31 ;;; Code:
     32 
     33 (require 'cider-eval)
     34 (require 'clojure-mode)
     35 (require 'easymenu)
     36 
     37 (defcustom cider-scratch-initial-message
     38   ";; This buffer is for Clojure experiments and evaluation.\n
     39 ;; Press C-j to evaluate the last expression.\n
     40 ;; You can also press C-u C-j to evaluate the expression and pretty-print its result.\n\n"
     41   "The initial message displayed in new scratch buffers."
     42   :type 'string
     43   :group 'cider
     44   :package-version '(cider . "0.18.0"))
     45 
     46 (defvar cider-clojure-interaction-mode-map
     47   (let ((map (make-sparse-keymap)))
     48     (set-keymap-parent map clojure-mode-map)
     49     (define-key map (kbd "C-j") #'cider-eval-print-last-sexp)
     50     (define-key map [remap paredit-newline] #'cider-eval-print-last-sexp)
     51     (easy-menu-define cider-clojure-interaction-mode-menu map
     52       "Menu for Clojure Interaction mode"
     53       '("Clojure Interaction"
     54         (["Eval and print last sexp" #'cider-eval-print-last-sexp]
     55          "--"
     56          ["Reset" #'cider-scratch-reset])))
     57     map))
     58 
     59 (defconst cider-scratch-buffer-name "*cider-scratch*")
     60 
     61 ;;;###autoload
     62 (defun cider-scratch ()
     63   "Go to the scratch buffer named `cider-scratch-buffer-name'."
     64   (interactive)
     65   (pop-to-buffer (cider-scratch-find-or-create-buffer)))
     66 
     67 (defun cider-scratch-find-or-create-buffer ()
     68   "Find or create the scratch buffer."
     69   (or (get-buffer cider-scratch-buffer-name)
     70       (cider-scratch--create-buffer)))
     71 
     72 (define-derived-mode cider-clojure-interaction-mode clojure-mode "Clojure Interaction"
     73   "Major mode for typing and evaluating Clojure forms.
     74 Like `clojure-mode' except that \\[cider-eval-print-last-sexp] evals the Lisp expression
     75 before point, and prints its value into the buffer, advancing point.
     76 
     77 \\{cider-clojure-interaction-mode-map}"
     78   (setq-local sesman-system 'CIDER))
     79 
     80 (defun cider-scratch--insert-welcome-message ()
     81   "Insert the welcome message for the scratch buffer."
     82   (insert cider-scratch-initial-message))
     83 
     84 (defun cider-scratch--create-buffer ()
     85   "Create a new scratch buffer."
     86   (with-current-buffer (get-buffer-create cider-scratch-buffer-name)
     87     (cider-clojure-interaction-mode)
     88     (cider-scratch--insert-welcome-message)
     89     (current-buffer)))
     90 
     91 (defun cider-scratch-reset ()
     92   "Reset the current scratch buffer."
     93   (interactive)
     94   (erase-buffer)
     95   (cider-scratch--insert-welcome-message))
     96 
     97 (provide 'cider-scratch)
     98 
     99 ;;; cider-scratch.el ends here