Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
4.6 KiB

4 years ago
  1. ;;; inferior-slime.el --- Minor mode with Slime keys for comint buffers
  2. ;;
  3. ;; Author: Luke Gorrie <luke@synap.se>
  4. ;; License: GNU GPL (same license as Emacs)
  5. ;;
  6. ;;; Installation:
  7. ;;
  8. ;; Add something like this to your .emacs:
  9. ;;
  10. ;; (add-to-list 'load-path "<directory-of-this-file>")
  11. ;; (add-hook 'slime-load-hook (lambda () (require 'inferior-slime)))
  12. ;; (add-hook 'inferior-lisp-mode-hook (lambda () (inferior-slime-mode 1)))
  13. (require 'slime)
  14. (require 'cl-lib)
  15. (define-minor-mode inferior-slime-mode
  16. "\\<slime-mode-map>\
  17. Inferior SLIME mode: The Inferior Superior Lisp Mode for Emacs.
  18. This mode is intended for use with `inferior-lisp-mode'. It provides a
  19. subset of the bindings from `slime-mode'.
  20. \\{inferior-slime-mode-map}"
  21. :keymap
  22. ;; Fake binding to coax `define-minor-mode' to create the keymap
  23. '((" " 'undefined))
  24. (slime-setup-completion)
  25. (setq-local tab-always-indent 'complete))
  26. (defun inferior-slime-return ()
  27. "Handle the return key in the inferior-lisp buffer.
  28. The current input should only be sent if a whole expression has been
  29. entered, i.e. the parenthesis are matched.
  30. A prefix argument disables this behaviour."
  31. (interactive)
  32. (if (or current-prefix-arg (inferior-slime-input-complete-p))
  33. (comint-send-input)
  34. (insert "\n")
  35. (inferior-slime-indent-line)))
  36. (defun inferior-slime-indent-line ()
  37. "Indent the current line, ignoring everything before the prompt."
  38. (interactive)
  39. (save-restriction
  40. (let ((indent-start
  41. (save-excursion
  42. (goto-char (process-mark (get-buffer-process (current-buffer))))
  43. (let ((inhibit-field-text-motion t))
  44. (beginning-of-line 1))
  45. (point))))
  46. (narrow-to-region indent-start (point-max)))
  47. (lisp-indent-line)))
  48. (defun inferior-slime-input-complete-p ()
  49. "Return true if the input is complete in the inferior lisp buffer."
  50. (slime-input-complete-p (process-mark (get-buffer-process (current-buffer)))
  51. (point-max)))
  52. (defun inferior-slime-closing-return ()
  53. "Send the current expression to Lisp after closing any open lists."
  54. (interactive)
  55. (goto-char (point-max))
  56. (save-restriction
  57. (narrow-to-region (process-mark (get-buffer-process (current-buffer)))
  58. (point-max))
  59. (while (ignore-errors (save-excursion (backward-up-list 1) t))
  60. (insert ")")))
  61. (comint-send-input))
  62. (defun inferior-slime-change-directory (directory)
  63. "Set default-directory in the *inferior-lisp* buffer to DIRECTORY."
  64. (let* ((proc (slime-process))
  65. (buffer (and proc (process-buffer proc))))
  66. (when buffer
  67. (with-current-buffer buffer
  68. (cd-absolute directory)))))
  69. (defun inferior-slime-init-keymap ()
  70. (let ((map inferior-slime-mode-map))
  71. (set-keymap-parent map slime-parent-map)
  72. (slime-define-keys map
  73. ([return] 'inferior-slime-return)
  74. ([(control return)] 'inferior-slime-closing-return)
  75. ([(meta control ?m)] 'inferior-slime-closing-return)
  76. ;;("\t" 'slime-indent-and-complete-symbol)
  77. (" " 'slime-space))))
  78. (inferior-slime-init-keymap)
  79. (defun inferior-slime-hook-function ()
  80. (inferior-slime-mode 1))
  81. (defun inferior-slime-switch-to-repl-buffer ()
  82. (switch-to-buffer (process-buffer (slime-inferior-process))))
  83. (defun inferior-slime-show-transcript (string)
  84. (remove-hook 'comint-output-filter-functions
  85. 'inferior-slime-show-transcript t)
  86. (with-current-buffer (process-buffer (slime-inferior-process))
  87. (let ((window (display-buffer (current-buffer) t)))
  88. (set-window-point window (point-max)))))
  89. (defun inferior-slime-start-transcript ()
  90. (let ((proc (slime-inferior-process)))
  91. (when proc
  92. (with-current-buffer (process-buffer proc)
  93. (add-hook 'comint-output-filter-functions
  94. 'inferior-slime-show-transcript
  95. nil t)))))
  96. (defun inferior-slime-stop-transcript ()
  97. (let ((proc (slime-inferior-process)))
  98. (when proc
  99. (with-current-buffer (process-buffer (slime-inferior-process))
  100. (run-with-timer 0.2 nil
  101. (lambda (buffer)
  102. (with-current-buffer buffer
  103. (remove-hook 'comint-output-filter-functions
  104. 'inferior-slime-show-transcript t)))
  105. (current-buffer))))))
  106. (defun inferior-slime-init ()
  107. (add-hook 'slime-inferior-process-start-hook 'inferior-slime-hook-function)
  108. (add-hook 'slime-change-directory-hooks 'inferior-slime-change-directory)
  109. (add-hook 'slime-transcript-start-hook 'inferior-slime-start-transcript)
  110. (add-hook 'slime-transcript-stop-hook 'inferior-slime-stop-transcript)
  111. (def-slime-selector-method ?r
  112. "SLIME Read-Eval-Print-Loop."
  113. (process-buffer (slime-inferior-process))))
  114. (provide 'inferior-slime)