├── LICENSE ├── README.md ├── carp-flycheck.el ├── carp-mode.el └── inf-carp-mode.el /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | 3 | Version 2.0, January 2004 4 | 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 16 | 17 | "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 18 | 19 | "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 20 | 21 | "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 22 | 23 | "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 24 | 25 | "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 26 | 27 | "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 28 | 29 | "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 30 | 31 | 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 32 | 33 | 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 34 | 35 | 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 36 | 37 | You must give any other recipients of the Work or Derivative Works a copy of this License; and 38 | You must cause any modified files to carry prominent notices stating that You changed the files; and 39 | You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 | If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 41 | 42 | You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 43 | 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 44 | 45 | 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 46 | 47 | 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 48 | 49 | 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 50 | 51 | 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 52 | 53 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repo contains a hacked-together Emacs package that depends on Clojure mode being installed. 2 | It will hopefully be rewritten and put into one of the package managers eventually. 3 | 4 | Any contributions are most welcome! 5 | 6 | ## Configuration 7 | 8 | Add this to your configuration file: 9 | 10 | ```emacs-lisp 11 | (add-to-list 'load-path "~/your/path/to/carp-emacs") 12 | 13 | (require 'carp-mode) 14 | (require 'inf-carp-mode) 15 | 16 | ;; Use carp-mode for .carp files 17 | (add-to-list 'auto-mode-alist '("\\.carp\\'" . carp-mode)) 18 | ``` 19 | 20 | To start an interactive session, make sure `carp` is in your path (inside Emacs) and execute `M-x run-carp`. 21 | 22 | ## Flycheck 23 | 24 | This minor mode is highly recommended -- Carp's type errors are much easier to understand when they can be seen visually in the source code. 25 | 26 | ``` 27 | (require 'carp-flycheck) 28 | 29 | (add-hook 'carp-mode-hook 30 | (lambda () 31 | (flycheck-mode 1))) 32 | ``` 33 | 34 | 35 | 36 | ## License 37 | 38 | Copyright 2016 - 2020 Erik Svedäng 39 | 40 | Licensed under the Apache License, Version 2.0 (the "License"); 41 | you may not use this file except in compliance with the License. 42 | You may obtain a copy of the License at 43 | 44 | http://www.apache.org/licenses/LICENSE-2.0 45 | 46 | Unless required by applicable law or agreed to in writing, software 47 | distributed under the License is distributed on an "AS IS" BASIS, 48 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 49 | See the License for the specific language governing permissions and 50 | limitations under the License. 51 | 52 | The regular expression implementation as found in src/carp_regex.h are 53 | Copyright (C) 1994-2017 Lua.org, PUC-Rio under the terms of the MIT license. 54 | Details can be found in the License file LUA_LICENSE. 55 | -------------------------------------------------------------------------------- /carp-flycheck.el: -------------------------------------------------------------------------------- 1 | (require 'flycheck) 2 | 3 | (flycheck-define-checker carp-checker 4 | "A Carp syntax checker. 5 | 6 | See URL `http://github.com/carp-lang/Carp/'." 7 | :command ("carp" "--check" source) 8 | :error-patterns ((error line-start space (message) "\n At line " line ", column " column " in '" (file-name) "'" line-end) 9 | (error line-start (file-name) ":" line ":" column " " (message) line-end)) 10 | :modes (carp-mode)) 11 | 12 | (eval-after-load 'flycheck 13 | '(add-to-list 'flycheck-checkers 'carp-checker)) 14 | 15 | ;; (flycheck-parse-error-with-patterns 16 | ;; " At line 11, column 3 in 'foo.carp'" 17 | ;; (flycheck-checker-get 'carp-checker 'error-patterns) 18 | ;; 'carp-checker) 19 | 20 | (provide 'carp-flycheck) 21 | -------------------------------------------------------------------------------- /carp-mode.el: -------------------------------------------------------------------------------- 1 | 2 | (define-derived-mode carp-mode clojure-mode "Carp" 3 | "Major mode for the Carp programming language.") 4 | 5 | (add-to-list 'auto-mode-alist '("\\.carp\\'" . carp-mode)) 6 | 7 | (provide 'carp-mode) 8 | -------------------------------------------------------------------------------- /inf-carp-mode.el: -------------------------------------------------------------------------------- 1 | (require 'comint) 2 | (require 'thingatpt) 3 | 4 | ;; This code is blatantly stolen from clojure-inf by Bozhidar Batsov 5 | 6 | (defgroup inf-carp nil 7 | "Run an external Carp process (REPL) in an Emacs buffer." 8 | :group 'carp) 9 | 10 | (defcustom inf-carp-prompt-read-only t 11 | "If non-nil, the prompt will be read-only. 12 | 13 | Also see the description of `ielm-prompt-read-only'." 14 | :type 'boolean 15 | :group 'inf-carp) 16 | 17 | (defcustom inf-carp-filter-regexp 18 | "\\`\\s *\\(:\\(\\w\\|\\s_\\)\\)?\\s *\\'" 19 | "What not to save on inferior Carp's input history. 20 | Input matching this regexp is not saved on the input history in Inferior Carp 21 | mode. Default is whitespace followed by 0 or 1 single-letter colon-keyword 22 | \(as in :a, :c, etc.)" 23 | :type 'regexp 24 | :group 'inf-carp) 25 | 26 | (defvar inf-carp-mode-map 27 | (let ((map (copy-keymap comint-mode-map))) 28 | (define-key map "\C-x\C-e" #'inf-carp-eval-last-sexp) 29 | (define-key map "\C-c\C-l" #'inf-carp-load-file) 30 | (define-key map "\C-c\M-o" #'inf-carp-clear-repl-buffer) 31 | (easy-menu-define inf-carp-mode-menu map 32 | "Inferior Carp REPL Menu" 33 | '("Inf-Carp REPL" 34 | ["Eval last sexp" inf-carp-eval-last-sexp t] 35 | "--" 36 | ["Load file" inf-carp-load-file t] 37 | "--" 38 | ["Show arglist" inf-carp-show-arglist t] 39 | ;;["Show documentation for var" inf-carp-show-var-documentation t] 40 | ;;["Show info for var" inf-carp-show-var-info t] 41 | "--" 42 | ["Clear REPL" inf-carp-clear-repl-buffer])) 43 | map)) 44 | 45 | (defvar inf-carp-minor-mode-map 46 | (let ((map (make-sparse-keymap))) 47 | (define-key map "\M-\C-x" #'inf-carp-eval-defun) 48 | (define-key map "\C-x\C-e" #'inf-carp-eval-last-sexp) 49 | (define-key map "\C-c\C-e" #'inf-carp-eval-last-sexp) 50 | (define-key map "\C-c\C-c" #'inf-carp-build-and-run) 51 | (define-key map (kbd "") #'inf-carp-eval-defun) 52 | (define-key map "\C-c\C-k" #'inf-carp-build) 53 | (define-key map "\C-c\C-r" #'inf-carp-eval-region) 54 | (define-key map "\C-c\C-n" #'inf-carp-eval-form-and-next) 55 | (define-key map "\C-c\C-z" #'inf-carp-switch-to-repl) 56 | (define-key map "\C-c\C-m" #'inf-carp-macroexpand) 57 | (define-key map "\C-c\C-l" #'inf-carp-eval-buffer) 58 | ;; TODO: 59 | ;; (define-key map "\C-c\C-v" #'inf-carp-show-var-documentation) 60 | ;; (define-key map "\C-c\C-t" #'inf-carp-show-var-type) 61 | 62 | (easy-menu-define inf-carp-minor-mode-menu map 63 | "Inferior Carp Minor Mode Menu" 64 | '("Inf-Carp" 65 | ["Eval top-level sexp at point" inf-carp-eval-defun t] 66 | ["Eval last sexp" inf-carp-eval-last-sexp t] 67 | ["Eval region" inf-carp-eval-region t] 68 | ["Eval buffer" inf-carp-eval-buffer t] 69 | "--" 70 | ["Load file..." inf-carp-load-file t] 71 | "--" 72 | ["Switch to REPL" inf-carp-switch-to-repl t] 73 | ["Set REPL ns" inf-carp-set-ns t] 74 | "--" 75 | ;;["Show documentation for var" inf-carp-show-var-documentation t] 76 | ["Macroexpand" inf-carp-macroexpand t])) 77 | map)) 78 | 79 | ;;;###autoload 80 | (define-minor-mode inf-carp-minor-mode 81 | "Minor mode for interacting with the inferior Carp process buffer. 82 | 83 | The following commands are available: 84 | 85 | \\{inf-carp-minor-mode-map}" 86 | :lighter "" :keymap inf-carp-minor-mode-map) 87 | 88 | (defcustom inf-carp-program "carp" 89 | "Program name for invoking an inferior Carp in Inferior Carp mode." 90 | :type 'string 91 | :group 'inf-carp) 92 | 93 | (defcustom inf-carp-load-command "(load \"%s\")\n" 94 | "Format-string for building a Carp expression to load a file. 95 | This format string should use `%s' to substitute a file name 96 | and should result in a Carp expression that will command the inferior Carp 97 | to load that file." 98 | :type 'string 99 | :group 'inf-carp) 100 | 101 | (defcustom inf-carp-prompt "^[^鲮 \n] *" ;; "^[^λ> \n]+λ> *" 102 | "Regexp to recognize prompts in the Inferior Carp mode." 103 | :type 'regexp 104 | :group 'inf-carp) 105 | 106 | (defcustom inf-carp-subprompt " *_> *" 107 | "Regexp to recognize subprompts in the Inferior Carp mode." 108 | :type 'regexp 109 | :group 'inf-carp) 110 | 111 | (defvar inf-carp-buffer nil 112 | "The current inf-carp process buffer. 113 | 114 | MULTIPLE PROCESS SUPPORT 115 | =========================================================================== 116 | To run multiple Carp processes, you start the first up 117 | with \\[inf-carp]. It will be in a buffer named `*inf-carp*'. 118 | Rename this buffer with \\[rename-buffer]. You may now start up a new 119 | process with another \\[inf-carp]. It will be in a new buffer, 120 | named `*inf-carp*'. You can switch between the different process 121 | buffers with \\[switch-to-buffer]. 122 | 123 | Commands that send text from source buffers to Carp processes -- 124 | like `inf-carp-eval-defun' or `inf-carp-show-arglist' -- have to choose a 125 | process to send to, when you have more than one Carp process around. This 126 | is determined by the global variable `inf-carp-buffer'. Suppose you 127 | have three inferior Carps running: 128 | Buffer Process 129 | foo inf-carp 130 | bar inf-carp<2> 131 | *inf-carp* inf-carp<3> 132 | If you do a \\[inf-carp-eval-defun] command on some Carp source code, 133 | what process do you send it to? 134 | 135 | - If you're in a process buffer (foo, bar, or *inf-carp*), 136 | you send it to that process. 137 | - If you're in some other buffer (e.g., a source file), you 138 | send it to the process attached to buffer `inf-carp-buffer'. 139 | This process selection is performed by function `inf-carp-proc'. 140 | 141 | Whenever \\[inf-carp] fires up a new process, it resets 142 | `inf-carp-buffer' to be the new process's buffer. If you only run 143 | one process, this does the right thing. If you run multiple 144 | processes, you might need to change `inf-carp-buffer' to 145 | whichever process buffer you want to use.") 146 | 147 | (defvar inf-carp-mode-hook '() 148 | "Hook for customizing Inferior Carp mode.") 149 | 150 | (put 'inf-carp-mode 'mode-class 'special) 151 | 152 | (define-derived-mode inf-carp-mode comint-mode "Inferior Carp" 153 | "Major mode for interacting with an inferior Carp process. 154 | Runs a Carp interpreter as a subprocess of Emacs, with Carp I/O through an 155 | Emacs buffer. Variable `inf-carp-program' controls which Carp interpreter 156 | is run. Variables `inf-carp-prompt', `inf-carp-filter-regexp' and 157 | `inf-carp-load-command' can customize this mode for different Carp 158 | interpreters. 159 | 160 | For information on running multiple processes in multiple buffers, see 161 | documentation for variable `inf-carp-buffer'. 162 | 163 | \\{inf-carp-mode-map} 164 | 165 | Customization: Entry to this mode runs the hooks on `comint-mode-hook' and 166 | `inf-carp-mode-hook' (in that order). 167 | 168 | You can send text to the inferior Carp process from other buffers containing 169 | Carp source. 170 | `inf-carp-switch-to-repl' switches the current buffer to the Carp process buffer. 171 | `inf-carp-eval-defun' sends the current defun to the Carp process. 172 | `inf-carp-eval-region' sends the current region to the Carp process. 173 | 174 | Prefixing the inf-carp-eval/defun/region commands with 175 | a \\[universal-argument] causes a switch to the Carp process buffer after sending 176 | the text. 177 | 178 | Commands:\\ 179 | \\[comint-send-input] after the end of the process' output sends the text from the 180 | end of process to point. 181 | \\[comint-send-input] before the end of the process' output copies the sexp ending at point 182 | to the end of the process' output, and sends it. 183 | \\[comint-copy-old-input] copies the sexp ending at point to the end of the process' output, 184 | allowing you to edit it before sending it. 185 | If `comint-use-prompt-regexp' is nil (the default), \\[comint-insert-input] on old input 186 | copies the entire old input to the end of the process' output, allowing 187 | you to edit it before sending it. When not used on old input, or if 188 | `comint-use-prompt-regexp' is non-nil, \\[comint-insert-input] behaves according to 189 | its global binding. 190 | \\[backward-delete-char-untabify] converts tabs to spaces as it moves back. 191 | \\[carp-indent-line] indents for Carp; with argument, shifts rest 192 | of expression rigidly with the current line. 193 | \\[indent-sexp] does \\[carp-indent-line] on each line starting within following expression. 194 | Paragraphs are separated only by blank lines. Semicolons start comments. 195 | If you accidentally suspend your process, use \\[comint-continue-subjob] 196 | to continue it." 197 | (setq comint-prompt-regexp inf-carp-prompt) 198 | (setq mode-line-process '(":%s")) 199 | ;;(carp-mode-variables) ;; ??? 200 | ;;(inf-carp-eldoc-setup) 201 | (setq comint-get-old-input #'inf-carp-get-old-input) 202 | (setq comint-input-filter #'inf-carp-input-filter) 203 | (set (make-local-variable 'comint-prompt-read-only) inf-carp-prompt-read-only) 204 | (add-hook 'comint-preoutput-filter-functions #'inf-carp-preoutput-filter nil t) 205 | (add-hook 'completion-at-point-functions #'inf-carp-completion-at-point nil t)) 206 | 207 | (add-hook 'carp-mode-hook (lambda () (inf-carp-minor-mode 1))) 208 | 209 | (defun inf-carp-get-old-input () 210 | "Return a string containing the sexp ending at point." 211 | (save-excursion 212 | (let ((end (point))) 213 | (backward-sexp) 214 | (buffer-substring (point) end)))) 215 | 216 | (defun inf-carp-input-filter (str) 217 | "Return t if STR does not match `inf-carp-filter-regexp'." 218 | (not (string-match inf-carp-filter-regexp str))) 219 | 220 | (defun inf-carp-chomp (string) 221 | "Remove final newline from STRING." 222 | (if (string-match "[\n]\\'" string) 223 | (replace-match "" t t string) 224 | string)) 225 | 226 | (defun inf-carp-remove-subprompts (string) 227 | "Remove subprompts from STRING." 228 | (replace-regexp-in-string inf-carp-subprompt "" string)) 229 | 230 | (defun inf-carp-preoutput-filter (str) 231 | "Preprocess the output STR from interactive commands." 232 | (cond 233 | ((string-prefix-p "inf-carp-" (symbol-name (or this-command last-command))) 234 | ;; Remove subprompts and prepend a newline to the output string 235 | (inf-carp-chomp (concat "\n" (inf-carp-remove-subprompts str)))) 236 | (t str))) 237 | 238 | (defvar inf-carp-project-root-files 239 | '("project.carp") 240 | "A list of files that can be considered project markers.") 241 | 242 | (defun inf-carp-project-root () 243 | "Retrieve the root directory of a project if available. 244 | 245 | Fallback to `default-directory.' if not within a project." 246 | (or (car (remove nil 247 | (mapcar (lambda 248 | (file) 249 | (locate-dominating-file default-directory file)) 250 | inf-carp-project-root-files))) 251 | default-directory)) 252 | 253 | (defun inf-carp-clear-repl-buffer () 254 | "Clear the REPL buffer." 255 | (interactive) 256 | (let ((comint-buffer-maximum-size 0)) 257 | (comint-truncate-buffer))) 258 | 259 | ;;;###autoload 260 | (defun inf-carp (cmd) 261 | "Run an inferior Carp process, input and output via buffer `*inf-carp*'. 262 | If there is a process already running in `*inf-carp*', just switch 263 | to that buffer. 264 | With argument, allows you to edit the command line (default is value 265 | of `inf-carp-program'). Runs the hooks from 266 | `inf-carp-mode-hook' (after the `comint-mode-hook' is run). 267 | \(Type \\[describe-mode] in the process buffer for a list of commands.)" 268 | (interactive (list (if current-prefix-arg 269 | (read-string "Run Carp: " inf-carp-program) 270 | inf-carp-program))) 271 | (if (not (comint-check-proc "*inf-carp*")) 272 | ;; run the new process in the project's root when in a project folder 273 | (let ((default-directory (inf-carp-project-root)) 274 | (cmdlist (split-string cmd))) 275 | (set-buffer (apply #'make-comint 276 | "inf-carp" (car cmdlist) nil (cdr cmdlist))) 277 | (inf-carp-mode))) 278 | (setq inf-carp-buffer "*inf-carp*") 279 | ;;(split-window-below) 280 | ;;(pop-to-buffer-same-window "*inf-carp*") 281 | (pop-to-buffer "*inf-carp*") 282 | ) 283 | 284 | ;;;###autoload 285 | (defalias 'run-carp 'inf-carp) 286 | 287 | (defun inf-carp-eval-region (start end &optional and-go) 288 | "Send the current region to the inferior Carp process. 289 | Prefix argument AND-GO means switch to the Carp buffer afterwards." 290 | (interactive "r\nP") 291 | ;; replace multiple newlines at the end of the region by a single one 292 | ;; or add one if there was no newline 293 | (let ((str (replace-regexp-in-string 294 | "[\n]*\\'" "\n" 295 | (buffer-substring-no-properties start end)))) 296 | (comint-send-string (inf-carp-proc) str)) 297 | (if and-go (inf-carp-switch-to-repl t))) 298 | 299 | (defun inf-carp-eval-string (code) 300 | "Send the string CODE to the inferior Carp process to be executed." 301 | (comint-send-string (inf-carp-proc) (concat code "\n"))) 302 | 303 | (defun inf-carp-eval-defun (&optional and-go) 304 | "Send the current defun to the inferior Carp process. 305 | Prefix argument AND-GO means switch to the Carp buffer afterwards." 306 | (interactive "P") 307 | (save-excursion 308 | (end-of-defun) 309 | (let ((end (point)) (case-fold-search t)) 310 | (beginning-of-defun) 311 | (inf-carp-eval-region (point) end and-go)))) 312 | 313 | (defun inf-carp-eval-buffer (&optional and-go) 314 | "Send the current buffer to the inferior Carp process. 315 | Prefix argument AND-GO means switch to the Carp buffer afterwards." 316 | (interactive "P") 317 | (save-excursion 318 | (widen) 319 | (let ((case-fold-search t)) 320 | (inf-carp-eval-region (point-min) (point-max) and-go)))) 321 | 322 | (defun inf-carp-eval-last-sexp (&optional and-go) 323 | "Send the previous sexp to the inferior Carp process. 324 | Prefix argument AND-GO means switch to the Carp buffer afterwards." 325 | (interactive "P") 326 | (inf-carp-eval-region (save-excursion (backward-sexp) (point)) (point) and-go)) 327 | 328 | (defun inf-carp-eval-form-and-next () 329 | "Send the previous sexp to the inferior Carp process and move to the next one." 330 | (interactive "") 331 | (while (not (zerop (car (syntax-ppss)))) 332 | (up-list)) 333 | (inf-carp-eval-last-sexp) 334 | (forward-sexp)) 335 | 336 | (defun inf-carp-switch-to-repl (eob-p) 337 | "Switch to the inferior Carp process buffer. 338 | With prefix argument EOB-P, positions cursor at end of buffer." 339 | (interactive "P") 340 | (if (get-buffer-process inf-carp-buffer) 341 | (let ((pop-up-frames 342 | ;; Be willing to use another frame 343 | ;; that already has the window in it. 344 | (or pop-up-frames 345 | (get-buffer-window inf-carp-buffer t)))) 346 | (pop-to-buffer inf-carp-buffer)) 347 | (run-carp inf-carp-program)) 348 | (when eob-p 349 | (push-mark) 350 | (goto-char (point-max)))) 351 | 352 | 353 | ;;; Now that inf-carp-eval-/defun/region takes an optional prefix arg, 354 | ;;; these commands are redundant. But they are kept around for the user 355 | ;;; to bind if he wishes, for backwards functionality, and because it's 356 | ;;; easier to type C-c e than C-u C-c C-e. 357 | 358 | (defun inf-carp-eval-region-and-go (start end) 359 | "Send the current region to the inferior Carp, and switch to its buffer." 360 | (interactive "r") 361 | (inf-carp-eval-region start end t)) 362 | 363 | (defun inf-carp-eval-defun-and-go () 364 | "Send the current defun to the inferior Carp, and switch to its buffer." 365 | (interactive) 366 | (inf-carp-eval-defun t)) 367 | 368 | (defvar inf-carp-prev-l/c-dir/file nil 369 | "Record last directory and file used in loading or compiling. 370 | This holds a cons cell of the form `(DIRECTORY . FILE)' 371 | describing the last `inf-carp-load-file' command.") 372 | 373 | (defcustom inf-carp-source-modes '(carp-mode) 374 | "Used to determine if a buffer contains Carp source code. 375 | If it's loaded into a buffer that is in one of these major modes, it's 376 | considered a Carp source file by `inf-carp-load-file'. 377 | Used by this command to determine defaults." 378 | :type '(repeat symbol) 379 | :group 'inf-carp) 380 | 381 | (defun inf-carp-load-file (file-name) 382 | "Load a Carp file FILE-NAME into the inferior Carp process." 383 | (interactive (comint-get-source "Load Carp file: " inf-carp-prev-l/c-dir/file 384 | inf-carp-source-modes nil)) ; nil because LOAD 385 | ; doesn't need an exact name 386 | (comint-check-source file-name) ; Check to see if buffer needs saved. 387 | (setq inf-carp-prev-l/c-dir/file (cons (file-name-directory file-name) 388 | (file-name-nondirectory file-name))) 389 | (comint-send-string (inf-carp-proc) 390 | (format inf-carp-load-command file-name)) 391 | (inf-carp-switch-to-repl t)) 392 | 393 | (defun inf-carp-connected-p () 394 | "Return t if inferior Carp is currently connected, nil otherwise." 395 | (not (null inf-carp-buffer))) 396 | 397 | 398 | ;;; Documentation functions: function doc, var doc, arglist, and 399 | ;;; describe symbol. 400 | ;;; =========================================================================== 401 | 402 | ;;; Command strings 403 | ;;; =============== 404 | 405 | (defcustom inf-carp-build-command 406 | "(do (reload) (build))\n" 407 | "Command to build the project." 408 | :type 'string 409 | :group 'inf-carp) 410 | 411 | (defcustom inf-carp-build-and-run-command 412 | "(do (reload) (build) (run))\n" 413 | "Command to build and run the project." 414 | :type 'string 415 | :group 'inf-carp) 416 | 417 | (defcustom inf-carp-var-type-command 418 | "(type %s)\n" 419 | "Command to query inferior Carp for a var's type." 420 | :type 'string 421 | :group 'inf-carp) 422 | 423 | (defcustom inf-carp-var-info-command 424 | "(info %s)\n" 425 | "Command to query inferior Carp for a var's source." 426 | :type 'string 427 | :group 'inf-carp) 428 | 429 | (defcustom inf-carp-arglist-command 430 | "(try 431 | (:arglists 432 | (carp.core/meta 433 | (carp.core/resolve 434 | (carp.core/read-string \"%s\")))) 435 | (catch Throwable t nil))\n" 436 | "Command to query inferior Carp for a function's arglist." 437 | :type 'string 438 | :group 'inf-carp) 439 | 440 | (defcustom inf-carp-completion-command 441 | "(complete.core/completions \"%s\")\n" 442 | "Command to query inferior Carp for completion candidates." 443 | :type 'string 444 | :group 'inf-carp) 445 | 446 | (defcustom inf-carp-macroexpand-command 447 | "(expand '%s)\n" 448 | "Command to invoke macroexpand." 449 | :type 'string 450 | :group 'inf-carp) 451 | 452 | ;;; Ancillary functions 453 | ;;; =================== 454 | 455 | ;;; Reads a string from the user. 456 | (defun inf-carp-symprompt (prompt default) 457 | (list (let* ((prompt (if default 458 | (format "%s (default %s): " prompt default) 459 | (concat prompt ": "))) 460 | (ans (read-string prompt))) 461 | (if (zerop (length ans)) default ans)))) 462 | 463 | 464 | ;;; Adapted from function-called-at-point in help.el. 465 | (defun inf-carp-fn-called-at-pt () 466 | "Return the name of the function called in the current call. 467 | The value is nil if it can't find one." 468 | (condition-case nil 469 | (save-excursion 470 | (save-restriction 471 | (narrow-to-region (max (point-min) (- (point) 1000)) (point-max)) 472 | (backward-up-list 1) 473 | (forward-char 1) 474 | (let ((obj (read (current-buffer)))) 475 | (and (symbolp obj) obj)))) 476 | (error nil))) 477 | 478 | 479 | ;;; Adapted from variable-at-point in help.el. 480 | (defun inf-carp-var-at-pt () 481 | (condition-case () 482 | (save-excursion 483 | (forward-sexp -1) 484 | (skip-chars-forward "'") 485 | (let ((obj (read (current-buffer)))) 486 | (and (symbolp obj) obj))) 487 | (error nil))) 488 | 489 | (defun inf-carp-symbol-at-point () 490 | "Return the name of the symbol at point, otherwise nil." 491 | (or (thing-at-point 'symbol) "")) 492 | 493 | (defun inf-carp-build () 494 | "Send a command to the inferior Carp to build a form. 495 | See variable `inf-carp-var-build-command'." 496 | (interactive) 497 | (comint-proc-query (inf-carp-proc) (format inf-carp-build-command))) 498 | 499 | (defun inf-carp-build-and-run () 500 | "Send a command to the inferior Carp to build a form. 501 | See variable `inf-carp-var-build-command'." 502 | (interactive) 503 | (inf-carp-eval-buffer) 504 | (comint-proc-query (inf-carp-proc) (format inf-carp-build-and-run-command))) 505 | 506 | (defun inf-carp-bake (var) 507 | "Send a command to the inferior Carp to bake a form. 508 | See variable `inf-carp-var-bake-command'." 509 | (interactive (inf-carp-symprompt "Var bake" (inf-carp-var-at-pt))) 510 | (comint-proc-query (inf-carp-proc) (format inf-carp-var-bake-command var))) 511 | 512 | ;;; Documentation functions: var doc and arglist. 513 | ;;; ====================================================================== 514 | 515 | (defun inf-carp-show-var-type (var) 516 | "Send a command to the inferior Carp to give type for VAR. 517 | See variable `inf-carp-var-type-command'." 518 | (interactive (inf-carp-symprompt "Var type" (inf-carp-var-at-pt))) 519 | (comint-proc-query (inf-carp-proc) (format inf-carp-var-type-command var))) 520 | 521 | (defun inf-carp-arglist (fn) 522 | "Send a query to the inferior Carp for the arglist for function FN. 523 | See variable `inf-carp-arglist-command'." 524 | (interactive (inf-carp-symprompt "Arglist" (inf-carp-fn-called-at-pt))) 525 | (let* ((proc (inf-carp-proc)) 526 | (comint-filt (process-filter proc)) 527 | (kept "") 528 | eldoc) 529 | (set-process-filter proc (lambda (_proc string) (setq kept (concat kept string)))) 530 | (unwind-protect 531 | (let ((eldoc-snippet (format inf-carp-arglist-command fn))) 532 | (process-send-string proc eldoc-snippet) 533 | (while (and (not (string-match inf-carp-prompt kept)) 534 | (accept-process-output proc 2))) 535 | (setq eldoc (and (string-match "(.+)" kept) (match-string 0 kept))) 536 | ) 537 | (set-process-filter proc comint-filt)) 538 | eldoc)) 539 | 540 | (defun inf-carp-show-arglist (fn) 541 | "Show the arglist for function FN in the mini-buffer." 542 | (interactive (inf-carp-symprompt "Arglist" (inf-carp-fn-called-at-pt))) 543 | (let ((eldoc (inf-carp-arglist fn))) 544 | (when eldoc 545 | (message "%s: %s" fn eldoc)))) 546 | 547 | (defun inf-carp-show-ns-vars (ns) 548 | "Send a query to the inferior Carp for the public vars in NS. 549 | See variable `inf-carp-ns-vars-command'." 550 | (interactive (inf-carp-symprompt "Ns vars" (carp-find-ns))) 551 | (comint-proc-query (inf-carp-proc) (format inf-carp-ns-vars-command ns))) 552 | 553 | (defun inf-carp-set-ns (ns) 554 | "Set the ns of the inferior Carp process to NS. 555 | Defaults to the ns of the current buffer." 556 | (interactive (inf-carp-symprompt "Set ns to" (carp-find-ns))) 557 | (comint-proc-query (inf-carp-proc) (format inf-carp-set-ns-command ns))) 558 | 559 | (defun inf-carp-apropos (var) 560 | "Send a command to the inferior Carp to give apropos for VAR. 561 | See variable `inf-carp-apropos-command'." 562 | (interactive (inf-carp-symprompt "Var apropos" (inf-carp-var-at-pt))) 563 | (comint-proc-query (inf-carp-proc) (format inf-carp-apropos-command var))) 564 | 565 | (defun inf-carp-macroexpand (&optional macro-1) 566 | "Send a command to the inferior Carp to give apropos for VAR. 567 | See variable `inf-carp-macroexpand-command'. 568 | With a prefix arg MACRO-1 uses `inf-carp-macroexpand-1-command'." 569 | (interactive "P") 570 | (let ((last-sexp (buffer-substring-no-properties (save-excursion (backward-sexp) (point)) (point)))) 571 | (comint-send-string 572 | (inf-carp-proc) 573 | (format (if macro-1 574 | inf-carp-macroexpand-1-command 575 | inf-carp-macroexpand-command) 576 | last-sexp)))) 577 | 578 | 579 | (defun inf-carp-proc () 580 | "Return the current inferior Carp process. 581 | See variable `inf-carp-buffer'." 582 | (let ((proc (get-buffer-process (if (derived-mode-p 'inf-carp-mode) 583 | (current-buffer) 584 | inf-carp-buffer)))) 585 | (or proc 586 | (error "No Carp subprocess; see variable `inf-carp-buffer'")))) 587 | 588 | (defun inf-carp-completions (expr) 589 | "Return a list of completions for the Carp expression starting with EXPR." 590 | (let* ((proc (inf-carp-proc)) 591 | (comint-filt (process-filter proc)) 592 | (kept "") 593 | completions) 594 | (set-process-filter proc (lambda (_proc string) (setq kept (concat kept string)))) 595 | (unwind-protect 596 | (let ((completion-snippet 597 | (format 598 | inf-carp-completion-command (substring-no-properties expr)))) 599 | (process-send-string proc completion-snippet) 600 | (while (and (not (string-match inf-carp-prompt kept)) 601 | (accept-process-output proc 2))) 602 | (setq completions (read kept)) 603 | ;; Subprocess echoes output on Windows and OS X. 604 | (when (and completions (string= (concat (car completions) "\n") completion-snippet)) 605 | (setq completions (cdr completions)))) 606 | (set-process-filter proc comint-filt)) 607 | completions)) 608 | 609 | (defconst inf-carp-carp-expr-break-chars " \t\n\"\'`><,;|&{(") 610 | 611 | (defun inf-carp-completion-bounds-of-expr-at-point () 612 | "Return bounds of expression at point to complete." 613 | (when (not (memq (char-syntax (following-char)) '(?w ?_))) 614 | (save-excursion 615 | (let ((end (point))) 616 | (skip-chars-backward (concat "^" inf-carp-carp-expr-break-chars)) 617 | (cons (point) end))))) 618 | 619 | (defun inf-carp-completion-expr-at-point () 620 | "Return expression at point to complete." 621 | (let ((bounds (inf-carp-completion-bounds-of-expr-at-point))) 622 | (and bounds 623 | (buffer-substring (car bounds) (cdr bounds))))) 624 | 625 | (defun inf-carp-completion-at-point () 626 | "Retrieve the list of completions and prompt the user. 627 | Returns the selected completion or nil." 628 | (let ((bounds (inf-carp-completion-bounds-of-expr-at-point))) 629 | (when bounds 630 | (list (car bounds) (cdr bounds) 631 | (if (fboundp 'completion-table-with-cache) 632 | (completion-table-with-cache #'inf-carp-completions) 633 | (completion-table-dynamic #'inf-carp-completions)))))) 634 | 635 | (provide 'inf-carp-mode) 636 | --------------------------------------------------------------------------------