guile-scheme.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. ;;; guile-scheme.el --- Guile Scheme editing mode
  2. ;; Copyright (C) 2001, 2006 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free
  15. ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. ;;;; 02111-1307 USA
  17. ;;; Commentary:
  18. ;; Put the following lines in your ~/.emacs:
  19. ;;
  20. ;; (require 'guile-scheme)
  21. ;; (setq initial-major-mode 'scheme-interaction-mode)
  22. ;;; Code:
  23. (require 'guile)
  24. (require 'scheme)
  25. (defgroup guile-scheme nil
  26. "Editing Guile-Scheme code"
  27. :group 'lisp)
  28. (defvar guile-scheme-syntax-keywords
  29. '((begin 0) (if 1) (cond 0) (case 1) (do 2)
  30. quote syntax lambda and or else delay receive use-modules
  31. (match 1) (match-lambda 0) (match-lambda* 0)
  32. (let scheme-let-indent) (let* 1) (letrec 1) (and-let* 1)
  33. (let-syntax 1) (letrec-syntax 1) (syntax-rules 1) (syntax-case 2)))
  34. (defvar guile-scheme-special-procedures
  35. '((catch 1) (lazy-catch 1) (stack-catch 1)
  36. map for-each (dynamic-wind 3)))
  37. ;; set indent functions
  38. (dolist (x (append guile-scheme-syntax-keywords
  39. guile-scheme-special-procedures))
  40. (when (consp x)
  41. (put (car x) 'scheme-indent-function (cadr x))))
  42. (defconst guile-scheme-font-lock-keywords
  43. (eval-when-compile
  44. (list
  45. (list (concat "(\\(define\\*?\\("
  46. ;; Function names.
  47. "\\(\\|-public\\|-method\\|-generic\\)\\|"
  48. ;; Macro names, as variable names.
  49. "\\(-syntax\\|-macro\\)\\|"
  50. ;; Others
  51. "-\\sw+\\)\\)\\>"
  52. ;; Any whitespace and declared object.
  53. "\\s *(?\\(\\sw+\\)?")
  54. '(1 font-lock-keyword-face)
  55. '(5 (cond ((match-beginning 3) font-lock-function-name-face)
  56. ((match-beginning 4) font-lock-variable-name-face)
  57. (t font-lock-type-face)) nil t))
  58. (list (concat
  59. "(" (regexp-opt
  60. (mapcar (lambda (e)
  61. (prin1-to-string (if (consp e) (car e) e)))
  62. (append guile-scheme-syntax-keywords
  63. guile-scheme-special-procedures)) 'words))
  64. '(1 font-lock-keyword-face))
  65. '("<\\sw+>" . font-lock-type-face)
  66. '("\\<:\\sw+\\>" . font-lock-builtin-face)
  67. ))
  68. "Expressions to highlight in Guile Scheme mode.")
  69. ;;;
  70. ;;; Guile Scheme mode
  71. ;;;
  72. (defvar guile-scheme-mode-map nil
  73. "Keymap for Guile Scheme mode.
  74. All commands in `lisp-mode-shared-map' are inherited by this map.")
  75. (unless guile-scheme-mode-map
  76. (let ((map (make-sparse-keymap "Guile-Scheme")))
  77. (setq guile-scheme-mode-map map)
  78. (cond ((boundp 'lisp-mode-shared-map)
  79. (set-keymap-parent map lisp-mode-shared-map))
  80. ((boundp 'shared-lisp-mode-map)
  81. (set-keymap-parent map shared-lisp-mode-map)))
  82. (define-key map [menu-bar] (make-sparse-keymap))
  83. (define-key map [menu-bar guile-scheme] (cons "Guile-Scheme" map))
  84. (define-key map [uncomment-region]
  85. '("Uncomment Out Region" . (lambda (beg end)
  86. (interactive "r")
  87. (comment-region beg end '(4)))))
  88. (define-key map [comment-region] '("Comment Out Region" . comment-region))
  89. (define-key map [indent-region] '("Indent Region" . indent-region))
  90. (define-key map [indent-line] '("Indent Line" . lisp-indent-line))
  91. (define-key map "\e\C-i" 'guile-scheme-complete-symbol)
  92. (define-key map "\e\C-x" 'guile-scheme-eval-define)
  93. (define-key map "\C-x\C-e" 'guile-scheme-eval-last-sexp)
  94. (define-key map "\C-c\C-b" 'guile-scheme-eval-buffer)
  95. (define-key map "\C-c\C-r" 'guile-scheme-eval-region)
  96. (define-key map "\C-c:" 'guile-scheme-eval-expression)
  97. (define-key map "\C-c\C-a" 'guile-scheme-apropos)
  98. (define-key map "\C-c\C-d" 'guile-scheme-describe)
  99. (define-key map "\C-c\C-k" 'guile-scheme-kill-process)
  100. (put 'comment-region 'menu-enable 'mark-active)
  101. (put 'uncomment-region 'menu-enable 'mark-active)
  102. (put 'indent-region 'menu-enable 'mark-active)))
  103. (defcustom guile-scheme-mode-hook nil
  104. "Normal hook run when entering `guile-scheme-mode'."
  105. :type 'hook
  106. :group 'guile-scheme)
  107. ;;;###autoload
  108. (defun guile-scheme-mode ()
  109. "Major mode for editing Guile Scheme code.
  110. Editing commands are similar to those of `scheme-mode'.
  111. \\{scheme-mode-map}
  112. Entry to this mode calls the value of `scheme-mode-hook'
  113. if that value is non-nil."
  114. (interactive)
  115. (kill-all-local-variables)
  116. (setq mode-name "Guile Scheme")
  117. (setq major-mode 'guile-scheme-mode)
  118. (use-local-map guile-scheme-mode-map)
  119. (scheme-mode-variables)
  120. (setq mode-line-process
  121. '(:eval (if (processp guile-scheme-adapter)
  122. (format " [%s]" guile-scheme-command)
  123. "")))
  124. (setq font-lock-defaults
  125. '((guile-scheme-font-lock-keywords)
  126. nil t (("+-*/.<>=!?$%_&~^:@" . "w")) beginning-of-defun
  127. (font-lock-mark-block-function . mark-defun)))
  128. (run-hooks 'guile-scheme-mode-hook))
  129. ;;;
  130. ;;; Scheme interaction mode
  131. ;;;
  132. (defvar scheme-interaction-mode-map ()
  133. "Keymap for Scheme Interaction mode.
  134. All commands in `guile-scheme-mode-map' are inherited by this map.")
  135. (unless scheme-interaction-mode-map
  136. (let ((map (make-sparse-keymap)))
  137. (setq scheme-interaction-mode-map map)
  138. (set-keymap-parent map guile-scheme-mode-map)
  139. (define-key map "\C-j" 'guile-scheme-eval-print-last-sexp)
  140. ))
  141. (defvar scheme-interaction-mode-hook nil
  142. "Normal hook run when entering `scheme-interaction-mode'.")
  143. (defun scheme-interaction-mode ()
  144. "Major mode for evaluating Scheme expressions with Guile.
  145. \\{scheme-interaction-mode-map}"
  146. (interactive)
  147. (guile-scheme-mode)
  148. (use-local-map scheme-interaction-mode-map)
  149. (setq major-mode 'scheme-interaction-mode)
  150. (setq mode-name "Scheme Interaction")
  151. (run-hooks 'scheme-interaction-mode-hook))
  152. ;;;
  153. ;;; Guile Scheme adapter
  154. ;;;
  155. (defvar guile-scheme-command "guile")
  156. (defvar guile-scheme-adapter nil)
  157. (defvar guile-scheme-module nil)
  158. (defun guile-scheme-adapter ()
  159. (if (and (processp guile-scheme-adapter)
  160. (eq (process-status guile-scheme-adapter) 'run))
  161. guile-scheme-adapter
  162. (setq guile-scheme-module nil)
  163. (setq guile-scheme-adapter
  164. (guile:make-adapter guile-scheme-command 'emacs-scheme-channel))))
  165. (defun guile-scheme-set-module ()
  166. "Set the current module based on buffer contents.
  167. If there is a (define-module ...) form, evaluate it.
  168. Otherwise, choose module (guile-user)."
  169. (save-excursion
  170. (let ((module (if (re-search-backward "^(define-module " nil t)
  171. (let ((start (match-beginning 0)))
  172. (goto-char start)
  173. (forward-sexp)
  174. (buffer-substring-no-properties start (point)))
  175. "(define-module (emacs-user))")))
  176. (unless (string= guile-scheme-module module)
  177. (prog1 (guile:eval module (guile-scheme-adapter))
  178. (setq guile-scheme-module module))))))
  179. (defun guile-scheme-eval-string (string)
  180. (guile-scheme-set-module)
  181. (guile:eval string (guile-scheme-adapter)))
  182. (defun guile-scheme-display-result (value flag)
  183. (if (string= value "#<unspecified>")
  184. (setq value "done"))
  185. (if flag
  186. (insert value)
  187. (message "%s" value)))
  188. ;;;
  189. ;;; Interactive commands
  190. ;;;
  191. (defun guile-scheme-eval-expression (string)
  192. "Evaluate the expression in STRING and show value in echo area."
  193. (interactive "SGuile Scheme Eval: ")
  194. (guile-scheme-display-result (guile-scheme-eval-string string) nil))
  195. (defun guile-scheme-eval-region (start end)
  196. "Evaluate the region as Guile Scheme code."
  197. (interactive "r")
  198. (guile-scheme-eval-expression (buffer-substring-no-properties start end)))
  199. (defun guile-scheme-eval-buffer ()
  200. "Evaluate the current buffer as Guile Scheme code."
  201. (interactive)
  202. (guile-scheme-eval-expression (buffer-string)))
  203. (defun guile-scheme-eval-last-sexp (arg)
  204. "Evaluate sexp before point; show value in echo area.
  205. With argument, print output into current buffer."
  206. (interactive "P")
  207. (guile-scheme-display-result
  208. (guile-scheme-eval-string
  209. (buffer-substring-no-properties
  210. (point) (save-excursion (backward-sexp) (point)))) arg))
  211. (defun guile-scheme-eval-print-last-sexp ()
  212. "Evaluate sexp before point; print value into current buffer."
  213. (interactive)
  214. (let ((start (point)))
  215. (guile-scheme-eval-last-sexp t)
  216. (insert "\n")
  217. (save-excursion (goto-char start) (insert "\n"))))
  218. (defun guile-scheme-eval-define ()
  219. (interactive)
  220. (guile-scheme-eval-region (save-excursion (end-of-defun) (point))
  221. (save-excursion (beginning-of-defun) (point))))
  222. (defun guile-scheme-load-file (file)
  223. "Load a Guile Scheme file."
  224. (interactive "fGuile Scheme load file: ")
  225. (guile-scheme-eval-string (format "(load %s)" (expand-file-name file)))
  226. (message "done"))
  227. (guile-import guile-emacs-complete-alist)
  228. (defun guile-scheme-complete-symbol ()
  229. (interactive)
  230. (let* ((end (point))
  231. (start (save-excursion (skip-syntax-backward "w_") (point)))
  232. (pattern (buffer-substring-no-properties start end))
  233. (alist (guile-emacs-complete-alist pattern)))
  234. (goto-char end)
  235. (let ((completion (try-completion pattern alist)))
  236. (cond ((eq completion t))
  237. ((not completion)
  238. (message "Can't find completion for \"%s\"" pattern)
  239. (ding))
  240. ((not (string= pattern completion))
  241. (delete-region start end)
  242. (insert completion))
  243. (t
  244. (message "Making completion list...")
  245. (with-output-to-temp-buffer "*Completions*"
  246. (display-completion-list alist))
  247. (message "Making completion list...done"))))))
  248. (guile-import guile-emacs-apropos)
  249. (defun guile-scheme-apropos (regexp)
  250. (interactive "sGuile Scheme apropos (regexp): ")
  251. (guile-scheme-set-module)
  252. (with-output-to-temp-buffer "*Help*"
  253. (princ (guile-emacs-apropos regexp))))
  254. (guile-import guile-emacs-describe)
  255. (defun guile-scheme-describe (symbol)
  256. (interactive (list (guile-scheme-input-symbol "Describe Guile variable")))
  257. (guile-scheme-set-module)
  258. (with-output-to-temp-buffer "*Help*"
  259. (princ (guile-emacs-describe symbol))))
  260. (defun guile-scheme-kill-process ()
  261. (interactive)
  262. (if guile-scheme-adapter
  263. (guile-process-kill guile-scheme-adapter))
  264. (setq guile-scheme-adapter nil))
  265. ;;;
  266. ;;; Internal functions
  267. ;;;
  268. (guile-import apropos-internal guile-apropos-internal)
  269. (defvar guile-scheme-complete-table (make-vector 151 nil))
  270. (defun guile-scheme-input-symbol (prompt)
  271. (mapc (lambda (sym)
  272. (if (symbolp sym)
  273. (intern (symbol-name sym) guile-scheme-complete-table)))
  274. (guile-apropos-internal ""))
  275. (let* ((str (thing-at-point 'symbol))
  276. (default (if (intern-soft str guile-scheme-complete-table)
  277. (concat " (default " str ")")
  278. "")))
  279. (intern (completing-read (concat prompt default ": ")
  280. guile-scheme-complete-table nil t nil nil str))))
  281. ;;;
  282. ;;; Turn on guile-scheme-mode for .scm files by default.
  283. ;;;
  284. (setq auto-mode-alist
  285. (cons '("\\.scm\\'" . guile-scheme-mode) auto-mode-alist))
  286. (provide 'guile-scheme)
  287. ;;; guile-scheme.el ends here