scheme.el 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. ;;; scheme.el --- Scheme (and DSSSL) editing mode -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 1986-2023 Free Software Foundation, Inc.
  3. ;; Author: Bill Rozas <jinx@martigny.ai.mit.edu>
  4. ;; Adapted-by: Dave Love <d.love@dl.ac.uk>
  5. ;; Keywords: languages, lisp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The major mode for editing Scheme-type Lisp code, very similar to
  19. ;; the Lisp mode documented in the Emacs manual. `dsssl-mode' is a
  20. ;; variant of scheme-mode for editing DSSSL specifications for SGML
  21. ;; documents. [As of Apr 1997, some pointers for DSSSL may be found,
  22. ;; for instance, at <URL:https://www.sil.org/sgml/related.html#dsssl>.]
  23. ;; All these Lisp-ish modes vary basically in details of the language
  24. ;; syntax they highlight/indent/index, but dsssl-mode uses "^;;;" as
  25. ;; the page-delimiter since ^L isn't normally a valid SGML character.
  26. ;;
  27. ;; For interacting with a Scheme interpreter See also `run-scheme' in
  28. ;; the `cmuscheme' package and also the implementation-specific
  29. ;; `xscheme' package.
  30. ;; Here's a recipe to generate a TAGS file for DSSSL, by the way:
  31. ;; etags --lang=scheme --regex='/[ \t]*(\(mode\|element\)[ \t
  32. ;; ]+\([^ \t(
  33. ;; ]+\)/\2/' --regex='/[ \t]*(element[ \t
  34. ;; ]*([^)]+[ \t
  35. ;; ]+\([^)]+\)[ \t
  36. ;; ]*)/\1/' --regex='/(declare[^ \t
  37. ;; ]*[ \t
  38. ;; ]+\([^ \t
  39. ;; ]+\)/\1/' "$@"
  40. ;;; Code:
  41. (require 'lisp-mode)
  42. (defvar scheme-mode-syntax-table
  43. (let ((st (make-syntax-table))
  44. (i 0))
  45. ;; Symbol constituents
  46. ;; We used to treat chars 128-256 as symbol-constituent, but they
  47. ;; should be valid word constituents (Bug#8843). Note that valid
  48. ;; identifier characters are Scheme-implementation dependent.
  49. (while (< i ?0)
  50. (modify-syntax-entry i "_ " st)
  51. (setq i (1+ i)))
  52. (setq i (1+ ?9))
  53. (while (< i ?A)
  54. (modify-syntax-entry i "_ " st)
  55. (setq i (1+ i)))
  56. (setq i (1+ ?Z))
  57. (while (< i ?a)
  58. (modify-syntax-entry i "_ " st)
  59. (setq i (1+ i)))
  60. (setq i (1+ ?z))
  61. (while (< i 128)
  62. (modify-syntax-entry i "_ " st)
  63. (setq i (1+ i)))
  64. ;; Whitespace
  65. (modify-syntax-entry ?\t " " st)
  66. (modify-syntax-entry ?\n "> " st)
  67. (modify-syntax-entry ?\f " " st)
  68. (modify-syntax-entry ?\r " " st)
  69. (modify-syntax-entry ?\s " " st)
  70. ;; These characters are delimiters but otherwise undefined.
  71. ;; Brackets and braces balance for editing convenience.
  72. (modify-syntax-entry ?\[ "(] " st)
  73. (modify-syntax-entry ?\] ")[ " st)
  74. (modify-syntax-entry ?{ "(} " st)
  75. (modify-syntax-entry ?} "){ " st)
  76. (modify-syntax-entry ?\| "\" 23bn" st)
  77. ;; Guile allows #! ... !# comments.
  78. ;; But SRFI-22 defines the comment as #!...\n instead.
  79. ;; Also Guile says that the !# should be on a line of its own.
  80. ;; It's too difficult to get it right, for too little benefit.
  81. ;; (modify-syntax-entry ?! "_ 2" st)
  82. ;; Other atom delimiters
  83. (modify-syntax-entry ?\( "() " st)
  84. (modify-syntax-entry ?\) ")( " st)
  85. ;; It's used for single-line comments as well as for #;(...) sexp-comments.
  86. (modify-syntax-entry ?\; "<" st)
  87. (modify-syntax-entry ?\" "\" " st)
  88. (modify-syntax-entry ?' "' " st)
  89. (modify-syntax-entry ?` "' " st)
  90. ;; Special characters
  91. (modify-syntax-entry ?, "' " st)
  92. (modify-syntax-entry ?@ "' " st)
  93. (modify-syntax-entry ?# "' 14" st)
  94. (modify-syntax-entry ?\\ "\\ " st)
  95. st))
  96. (defvar scheme-mode-abbrev-table nil)
  97. (define-abbrev-table 'scheme-mode-abbrev-table ())
  98. (defvar scheme-imenu-generic-expression
  99. `((nil
  100. ,(rx bol (zero-or-more space)
  101. "(define"
  102. (zero-or-one "*")
  103. (zero-or-one "-public")
  104. (one-or-more space)
  105. (zero-or-one "(")
  106. (group (one-or-more (or word (syntax symbol)))))
  107. 1)
  108. ("Methods"
  109. ,(rx bol (zero-or-more space)
  110. "(define-"
  111. (or "generic" "method" "accessor")
  112. (one-or-more space)
  113. (zero-or-one "(")
  114. (group (one-or-more (or word (syntax symbol)))))
  115. 1)
  116. ("Classes"
  117. ,(rx bol (zero-or-more space)
  118. "(define-class"
  119. (one-or-more space)
  120. (zero-or-one "(")
  121. (group (one-or-more (or word (syntax symbol)))))
  122. 1)
  123. ("Records"
  124. ,(rx bol (zero-or-more space)
  125. "(define-record-type"
  126. (zero-or-one "*")
  127. (one-or-more space)
  128. (group (one-or-more (or word (syntax symbol)))))
  129. 1)
  130. ("Conditions"
  131. ,(rx bol (zero-or-more space)
  132. "(define-condition-type"
  133. (one-or-more space)
  134. (group (one-or-more (or word (syntax symbol)))))
  135. 1)
  136. ("Modules"
  137. ,(rx bol (zero-or-more space)
  138. "(define-module"
  139. (one-or-more space)
  140. (group "(" (one-or-more any) ")"))
  141. 1)
  142. ("Macros"
  143. ,(rx bol (zero-or-more space) "("
  144. (or (and "defmacro"
  145. (zero-or-one "*")
  146. (zero-or-one "-public"))
  147. "define-macro" "define-syntax" "define-syntax-rule")
  148. (one-or-more space)
  149. (zero-or-one "(")
  150. (group (one-or-more (or word (syntax symbol)))))
  151. 1))
  152. "Imenu generic expression for Scheme mode. See `imenu-generic-expression'.")
  153. (defun scheme-mode-variables ()
  154. (set-syntax-table scheme-mode-syntax-table)
  155. (setq local-abbrev-table scheme-mode-abbrev-table)
  156. (setq-local paragraph-start (concat "$\\|" page-delimiter))
  157. (setq-local paragraph-separate paragraph-start)
  158. (setq-local paragraph-ignore-fill-prefix t)
  159. (setq-local fill-paragraph-function 'lisp-fill-paragraph)
  160. ;; Adaptive fill mode gets in the way of auto-fill,
  161. ;; and should make no difference for explicit fill
  162. ;; because lisp-fill-paragraph should do the job.
  163. (setq-local adaptive-fill-mode nil)
  164. (setq-local indent-line-function 'lisp-indent-line)
  165. (setq-local parse-sexp-ignore-comments t)
  166. (setq-local outline-regexp ";;; \\|(....")
  167. (setq-local add-log-current-defun-function #'lisp-current-defun-name)
  168. (setq-local comment-start ";")
  169. (setq-local comment-add 1)
  170. (setq-local comment-start-skip ";+[ \t]*")
  171. (setq-local comment-use-syntax t)
  172. (setq-local comment-column 40)
  173. (setq-local lisp-indent-function 'scheme-indent-function)
  174. (setq mode-line-process '("" scheme-mode-line-process))
  175. (setq-local imenu-case-fold-search t)
  176. (setq-local imenu-generic-expression scheme-imenu-generic-expression)
  177. (setq-local imenu-syntax-alist '(("+-*/.<>=?!$%_&~^:" . "w")))
  178. (setq-local syntax-propertize-function #'scheme-syntax-propertize)
  179. (setq font-lock-defaults
  180. '((scheme-font-lock-keywords
  181. scheme-font-lock-keywords-1 scheme-font-lock-keywords-2)
  182. nil t (("+-*/.<>=!?$%_&~^:" . "w") (?#. "w 14"))
  183. beginning-of-defun
  184. (font-lock-mark-block-function . mark-defun)))
  185. (setq-local prettify-symbols-alist lisp-prettify-symbols-alist)
  186. (setq-local lisp-doc-string-elt-property 'scheme-doc-string-elt))
  187. (defvar scheme-mode-line-process "")
  188. (defvar scheme-mode-map
  189. (let ((map (make-sparse-keymap)))
  190. (set-keymap-parent map lisp-mode-shared-map)
  191. map)
  192. "Keymap for Scheme mode.
  193. All commands in `lisp-mode-shared-map' are inherited by this map.")
  194. (easy-menu-define scheme-mode-menu scheme-mode-map
  195. "Menu for Scheme mode."
  196. '("Scheme"
  197. ["Indent Line" lisp-indent-line]
  198. ["Indent Region" indent-region
  199. :enable mark-active]
  200. ["Comment Out Region" comment-region
  201. :enable mark-active]
  202. ["Uncomment Out Region" (lambda (beg end)
  203. (interactive "r")
  204. (comment-region beg end '(4)))
  205. :enable mark-active]
  206. ["Run Inferior Scheme" run-scheme]))
  207. ;; Used by cmuscheme
  208. (defun scheme-mode-commands (map)
  209. ;;(define-key map "\t" 'indent-for-tab-command) ; default
  210. (define-key map "\177" 'backward-delete-char-untabify)
  211. (define-key map "\e\C-q" 'indent-sexp))
  212. ;;;###autoload
  213. (define-derived-mode scheme-mode prog-mode "Scheme"
  214. "Major mode for editing Scheme code.
  215. Editing commands are similar to those of `lisp-mode'.
  216. In addition, if an inferior Scheme process is running, some additional
  217. commands will be defined, for evaluating expressions and controlling
  218. the interpreter, and the state of the process will be displayed in the
  219. mode line of all Scheme buffers. The names of commands that interact
  220. with the Scheme process start with \"xscheme-\" if you use the MIT
  221. Scheme-specific `xscheme' package; for more information see the
  222. documentation for `xscheme-interaction-mode'. Use \\[run-scheme] to
  223. start an inferior Scheme using the more general `cmuscheme' package.
  224. Commands:
  225. Delete converts tabs to spaces as it moves back.
  226. Blank lines separate paragraphs. Semicolons start comments.
  227. \\{scheme-mode-map}"
  228. (scheme-mode-variables))
  229. (defgroup scheme nil
  230. "Editing Scheme code."
  231. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  232. :group 'lisp)
  233. (defcustom scheme-mit-dialect t
  234. "If non-nil, scheme mode is specialized for MIT Scheme.
  235. Set this to nil if you normally use another dialect."
  236. :type 'boolean)
  237. (defcustom dsssl-sgml-declaration
  238. "<!DOCTYPE style-sheet PUBLIC \"-//James Clark//DTD DSSSL Style Sheet//EN\">
  239. "
  240. "An SGML declaration for the DSSSL file.
  241. If it is defined as a string this will be inserted into an empty buffer
  242. which is in `dsssl-mode'. It is typically James Clark's style-sheet
  243. doctype, as required for Jade."
  244. :type '(choice (string :tag "Specified string")
  245. (const :tag "None" :value nil)))
  246. (defcustom scheme-mode-hook nil
  247. "Normal hook run when entering `scheme-mode'.
  248. See `run-hooks'."
  249. :type 'hook)
  250. (defcustom dsssl-mode-hook nil
  251. "Normal hook run when entering `dsssl-mode'.
  252. See `run-hooks'."
  253. :type 'hook)
  254. ;; This is shared by cmuscheme and xscheme.
  255. (defcustom scheme-program-name "scheme"
  256. "Program invoked by the `run-scheme' command."
  257. :type 'string)
  258. (defvar dsssl-imenu-generic-expression
  259. ;; Perhaps this should also look for the style-sheet DTD tags. I'm
  260. ;; not sure it's the best way to organize it; perhaps one type
  261. ;; should be at the first level, though you don't see this anyhow if
  262. ;; it gets split up.
  263. '(("Defines"
  264. "^(define\\s-+(?\\(\\sw+\\)" 1)
  265. ("Modes"
  266. "^\\s-*(mode\\s-+\\(\\(\\sw\\|\\s-\\)+\\)" 1)
  267. ("Elements"
  268. ;; (element foo ...) or (element (foo bar ...) ...)
  269. ;; Fixme: Perhaps it should do `root'.
  270. "^\\s-*(element\\s-+(?\\(\\(\\sw\\|\\s-\\)+\\))?" 1)
  271. ("Declarations"
  272. "^(declare\\(-\\sw+\\)+\\>\\s-+\\(\\sw+\\)" 2))
  273. "Imenu generic expression for DSSSL mode. See `imenu-generic-expression'.")
  274. (defconst scheme-font-lock-keywords-1
  275. (eval-when-compile
  276. (list
  277. ;;
  278. ;; Declarations. Hannes Haug <hannes.haug@student.uni-tuebingen.de> says
  279. ;; this works for SOS, STklos, SCOOPS, Meroon and Tiny CLOS.
  280. (list (concat "(\\(define\\*?\\("
  281. ;; Function names.
  282. "\\(\\|-public\\|-method\\|-generic\\(-procedure\\)?\\)\\|"
  283. ;; Macro names, as variable names. A bit dubious, this.
  284. "\\(-syntax\\|-macro\\)\\|"
  285. ;; Class names.
  286. "-class"
  287. ;; Guile modules.
  288. "\\|-module"
  289. "\\)\\)\\>"
  290. ;; Any whitespace and declared object.
  291. ;; The "(*" is for curried definitions, e.g.,
  292. ;; (define ((sum a) b) (+ a b))
  293. "[ \t]*(*"
  294. "\\(\\sw+\\)?")
  295. '(1 font-lock-keyword-face)
  296. '(6 (cond ((match-beginning 3) font-lock-function-name-face)
  297. ((match-beginning 5) font-lock-variable-name-face)
  298. (t font-lock-type-face))
  299. nil t))
  300. ))
  301. "Subdued expressions to highlight in Scheme modes.")
  302. (defconst scheme-font-lock-keywords-2
  303. (append scheme-font-lock-keywords-1
  304. (eval-when-compile
  305. (list
  306. ;;
  307. ;; Control structures.
  308. (cons
  309. (concat
  310. "(" (regexp-opt
  311. '("begin" "call-with-current-continuation" "call/cc"
  312. "call-with-input-file" "call-with-output-file"
  313. "call-with-port"
  314. "case" "cond"
  315. "do" "else" "for-each" "if" "lambda" "λ"
  316. "let" "let*" "let-syntax" "letrec" "letrec-syntax"
  317. ;; R6RS library subforms.
  318. "export" "import"
  319. ;; SRFI 11 usage comes up often enough.
  320. "let-values" "let*-values"
  321. ;; Hannes Haug <hannes.haug@student.uni-tuebingen.de> wants:
  322. "and" "or" "delay" "force"
  323. ;; Stefan Monnier <stefan.monnier@epfl.ch> says don't bother:
  324. ;;"quasiquote" "quote" "unquote" "unquote-splicing"
  325. "map" "syntax" "syntax-rules"
  326. ;; For R7RS
  327. "when" "unless" "letrec*" "include" "include-ci" "cond-expand"
  328. "delay-force" "parameterize" "guard" "case-lambda"
  329. "syntax-error" "only" "except" "prefix" "rename" "define-values"
  330. "define-record-type" "define-library"
  331. "include-library-declarations"
  332. ;; SRFI-8
  333. "receive"
  334. ) t)
  335. "\\>") 1)
  336. ;;
  337. ;; It wouldn't be Scheme without named-let.
  338. '("(let\\s-+\\(\\sw+\\)"
  339. (1 font-lock-function-name-face))
  340. ;;
  341. ;; David Fox <fox@graphics.cs.nyu.edu> for SOS/STklos class specifiers.
  342. '("\\<<\\sw+>\\>" . font-lock-type-face)
  343. ;;
  344. ;; Scheme `:' and `#:' keywords as builtins.
  345. '("\\<#?:\\sw+\\>" . font-lock-builtin-face)
  346. ;; R6RS library declarations.
  347. '("(\\(\\<library\\>\\)\\s-*(?\\(\\sw+\\)?"
  348. (1 font-lock-keyword-face)
  349. (2 font-lock-type-face))
  350. )))
  351. "Gaudy expressions to highlight in Scheme modes.")
  352. (defvar scheme-font-lock-keywords scheme-font-lock-keywords-1
  353. "Default expressions to highlight in Scheme modes.")
  354. (defconst scheme-sexp-comment-syntax-table
  355. (let ((st (make-syntax-table scheme-mode-syntax-table)))
  356. (modify-syntax-entry ?\; "." st)
  357. (modify-syntax-entry ?\n " " st)
  358. (modify-syntax-entry ?# "'" st)
  359. st))
  360. (put 'lambda 'scheme-doc-string-elt 2)
  361. (put 'lambda* 'scheme-doc-string-elt 2)
  362. ;; Docstring's pos in a `define' depends on whether it's a var or fun def.
  363. (put 'define 'scheme-doc-string-elt
  364. (lambda ()
  365. ;; The function is called with point right after "define".
  366. (forward-comment (point-max))
  367. (if (eq (char-after) ?\() 2 0)))
  368. (put 'define* 'scheme-doc-string-elt 2)
  369. (put 'case-lambda 'scheme-doc-string-elt 1)
  370. (put 'case-lambda* 'scheme-doc-string-elt 1)
  371. (put 'define-syntax-rule 'scheme-doc-string-elt 2)
  372. (put 'syntax-rules 'scheme-doc-string-elt 2)
  373. (defun scheme-syntax-propertize (beg end)
  374. (goto-char beg)
  375. (scheme-syntax-propertize-sexp-comment (point) end)
  376. (funcall
  377. (syntax-propertize-rules
  378. ("\\(#\\);" (1 (prog1 "< cn"
  379. (scheme-syntax-propertize-sexp-comment (point) end)))))
  380. (point) end))
  381. (defun scheme-syntax-propertize-sexp-comment (_ end)
  382. (let ((state (syntax-ppss)))
  383. (when (eq 2 (nth 7 state))
  384. ;; It's a sexp-comment. Tell parse-partial-sexp where it ends.
  385. (condition-case nil
  386. (progn
  387. (goto-char (+ 2 (nth 8 state)))
  388. ;; FIXME: this doesn't handle the case where the sexp
  389. ;; itself contains a #; comment.
  390. (forward-sexp 1)
  391. (put-text-property (1- (point)) (point)
  392. 'syntax-table (string-to-syntax "> cn")))
  393. (scan-error (goto-char end))))))
  394. ;;;###autoload
  395. (define-derived-mode dsssl-mode scheme-mode "DSSSL"
  396. "Major mode for editing DSSSL code.
  397. Editing commands are similar to those of `lisp-mode'.
  398. Commands:
  399. Delete converts tabs to spaces as it moves back.
  400. Blank lines separate paragraphs. Semicolons start comments.
  401. \\{scheme-mode-map}
  402. Entering this mode runs the hooks `scheme-mode-hook' and then
  403. `dsssl-mode-hook' and inserts the value of `dsssl-sgml-declaration' if
  404. that variable's value is a string."
  405. (setq-local page-delimiter "^;;;") ; ^L not valid SGML char
  406. ;; Insert a suitable SGML declaration into an empty buffer.
  407. ;; FIXME: This should use `auto-insert-alist' instead.
  408. (and (zerop (buffer-size))
  409. (stringp dsssl-sgml-declaration)
  410. (not buffer-read-only)
  411. (insert dsssl-sgml-declaration))
  412. (setq font-lock-defaults '(dsssl-font-lock-keywords
  413. nil t (("+-*/.<>=?$%_&~^:" . "w"))
  414. beginning-of-defun
  415. (font-lock-mark-block-function . mark-defun)))
  416. (setq-local add-log-current-defun-function #'lisp-current-defun-name)
  417. (setq-local imenu-case-fold-search nil)
  418. (setq imenu-generic-expression dsssl-imenu-generic-expression)
  419. (setq-local imenu-syntax-alist '(("+-*/.<>=?$%_&~^:" . "w"))))
  420. ;; Extra syntax for DSSSL. This isn't separated from Scheme, but
  421. ;; shouldn't cause much trouble in scheme-mode.
  422. (put 'element 'scheme-indent-function 1)
  423. (put 'mode 'scheme-indent-function 1)
  424. (put 'with-mode 'scheme-indent-function 1)
  425. (put 'make 'scheme-indent-function 1)
  426. (put 'style 'scheme-indent-function 1)
  427. (put 'root 'scheme-indent-function 1)
  428. (put 'λ 'scheme-indent-function 1)
  429. (defvar dsssl-font-lock-keywords
  430. (eval-when-compile
  431. (list
  432. ;; Similar to Scheme
  433. (list "(\\(define\\(-\\w+\\)?\\)\\>[ \t]*\\((?\\)\\(\\sw+\\)\\>"
  434. '(1 font-lock-keyword-face)
  435. '(4 font-lock-function-name-face))
  436. (cons
  437. (concat "(" (regexp-opt
  438. '("case" "cond" "else" "if" "lambda"
  439. "let" "let*" "letrec" "and" "or" "map" "with-mode")
  440. 'words))
  441. 1)
  442. ;; DSSSL syntax
  443. '("(\\(element\\|mode\\|declare-\\w+\\)\\>[ \t]*\\(\\sw+\\)"
  444. (1 font-lock-keyword-face)
  445. (2 font-lock-type-face))
  446. '("(\\(element\\)\\>[ \t]*(\\(\\S)+\\))"
  447. (1 font-lock-keyword-face)
  448. (2 font-lock-type-face))
  449. '("\\<\\sw+:\\>" . font-lock-constant-face) ; trailing `:' c.f. scheme
  450. ;; SGML markup (from sgml-mode) :
  451. '("<\\([!?][-a-z0-9]+\\)" 1 font-lock-keyword-face)
  452. '("<\\(/?[-a-z0-9]+\\)" 1 font-lock-function-name-face)))
  453. "Default expressions to highlight in DSSSL mode.")
  454. (defvar calculate-lisp-indent-last-sexp)
  455. ;; FIXME this duplicates almost all of lisp-indent-function.
  456. ;; Extract common code to a subroutine.
  457. (defun scheme-indent-function (indent-point state)
  458. "Scheme mode function for the value of the variable `lisp-indent-function'.
  459. This behaves like the function `lisp-indent-function', except that:
  460. i) it checks for a non-nil value of the property `scheme-indent-function'
  461. \(or the deprecated `scheme-indent-hook'), rather than `lisp-indent-function'.
  462. ii) if that property specifies a function, it is called with three
  463. arguments (not two), the third argument being the default (i.e., current)
  464. indentation."
  465. (let ((normal-indent (current-column)))
  466. (goto-char (1+ (elt state 1)))
  467. (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
  468. (if (and (elt state 2)
  469. (not (looking-at "\\sw\\|\\s_")))
  470. ;; car of form doesn't seem to be a symbol
  471. (progn
  472. (if (not (> (save-excursion (forward-line 1) (point))
  473. calculate-lisp-indent-last-sexp))
  474. (progn (goto-char calculate-lisp-indent-last-sexp)
  475. (beginning-of-line)
  476. (parse-partial-sexp (point)
  477. calculate-lisp-indent-last-sexp 0 t)))
  478. ;; Indent under the list or under the first sexp on the same
  479. ;; line as calculate-lisp-indent-last-sexp. Note that first
  480. ;; thing on that line has to be complete sexp since we are
  481. ;; inside the innermost containing sexp.
  482. (backward-prefix-chars)
  483. (current-column))
  484. (let ((function (buffer-substring (point)
  485. (progn (forward-sexp 1) (point))))
  486. method)
  487. (setq method (or (get (intern-soft function) 'scheme-indent-function)
  488. (get (intern-soft function) 'scheme-indent-hook)))
  489. (cond ((or (eq method 'defun)
  490. (and (null method)
  491. (> (length function) 3)
  492. (string-match "\\`def" function)))
  493. (lisp-indent-defform state indent-point))
  494. ((and (null method)
  495. (> (length function) 1)
  496. ;; The '#' in '#:' seems to get lost, not sure why
  497. (string-match "\\`:" function))
  498. (let ((lisp-body-indent 1))
  499. (lisp-indent-defform state indent-point)))
  500. ((integerp method)
  501. (lisp-indent-specform method state
  502. indent-point normal-indent))
  503. (method
  504. (funcall method state indent-point normal-indent)))))))
  505. ;;; Let is different in Scheme
  506. ;; (defun scheme-would-be-symbol (string)
  507. ;; (not (string-equal (substring string 0 1) "(")))
  508. ;; (defun scheme-next-sexp-as-string ()
  509. ;; ;; Assumes that it is protected by a save-excursion
  510. ;; (forward-sexp 1)
  511. ;; (let ((the-end (point)))
  512. ;; (backward-sexp 1)
  513. ;; (buffer-substring (point) the-end)))
  514. ;; This is correct but too slow.
  515. ;; The one below works almost always.
  516. ;;(defun scheme-let-indent (state indent-point)
  517. ;; (if (scheme-would-be-symbol (scheme-next-sexp-as-string))
  518. ;; (scheme-indent-specform 2 state indent-point)
  519. ;; (scheme-indent-specform 1 state indent-point)))
  520. (defun scheme-let-indent (state indent-point normal-indent)
  521. (skip-chars-forward " \t")
  522. (if (looking-at "[-a-zA-Z0-9+*/?!@$%^&_:~]")
  523. (lisp-indent-specform 2 state indent-point normal-indent)
  524. (lisp-indent-specform 1 state indent-point normal-indent)))
  525. ;; See `scheme-indent-function' (the function) for what these do.
  526. ;; In a nutshell:
  527. ;; . for forms with no `scheme-indent-function' property the 2nd
  528. ;; and subsequent lines will be indented with one space;
  529. ;; . if the value of the property is zero, then when the first form
  530. ;; is on a separate line, the next lines will be indented with 2
  531. ;; spaces instead of the default one space;
  532. ;; . if the value is a positive integer N, the first N lines after
  533. ;; the first one will be indented with 4 spaces, and the rest
  534. ;; will be indented with 2 spaces;
  535. ;; . if the value is `defun', the indentation is like for `defun';
  536. ;; . if the value is a function, it will be called to produce the
  537. ;; required indentation.
  538. ;; See also http://community.schemewiki.org/?emacs-indentation.
  539. (put 'begin 'scheme-indent-function 0)
  540. (put 'case 'scheme-indent-function 1)
  541. (put 'delay 'scheme-indent-function 0)
  542. (put 'do 'scheme-indent-function 2)
  543. (put 'lambda 'scheme-indent-function 1)
  544. (put 'let 'scheme-indent-function 'scheme-let-indent)
  545. (put 'let* 'scheme-indent-function 1)
  546. (put 'letrec 'scheme-indent-function 1)
  547. (put 'let-values 'scheme-indent-function 1) ; SRFI 11
  548. (put 'let*-values 'scheme-indent-function 1) ; SRFI 11
  549. (put 'and-let* 'scheme-indent-function 1) ; SRFI 2
  550. (put 'sequence 'scheme-indent-function 0) ; SICP, not r4rs
  551. (put 'let-syntax 'scheme-indent-function 1)
  552. (put 'letrec-syntax 'scheme-indent-function 1)
  553. (put 'syntax-rules 'scheme-indent-function 'defun)
  554. (put 'syntax-case 'scheme-indent-function 2) ; not r5rs
  555. (put 'with-syntax 'scheme-indent-function 1)
  556. (put 'library 'scheme-indent-function 1) ; R6RS
  557. ;; Part of at least Guile, Chez Scheme, Chicken
  558. (put 'eval-when 'scheme-indent-function 1)
  559. (put 'call-with-input-file 'scheme-indent-function 1)
  560. (put 'call-with-port 'scheme-indent-function 1)
  561. (put 'with-input-from-file 'scheme-indent-function 1)
  562. (put 'with-input-from-port 'scheme-indent-function 1)
  563. (put 'call-with-output-file 'scheme-indent-function 1)
  564. (put 'with-output-to-file 'scheme-indent-function 1)
  565. (put 'with-output-to-port 'scheme-indent-function 1)
  566. (put 'call-with-values 'scheme-indent-function 1) ; r5rs?
  567. (put 'dynamic-wind 'scheme-indent-function 3) ; r5rs?
  568. ;; R7RS
  569. (put 'when 'scheme-indent-function 1)
  570. (put 'unless 'scheme-indent-function 1)
  571. (put 'letrec* 'scheme-indent-function 1)
  572. (put 'parameterize 'scheme-indent-function 1)
  573. (put 'define-values 'scheme-indent-function 1)
  574. (put 'define-record-type 'scheme-indent-function 1) ;; is 1 correct?
  575. (put 'define-library 'scheme-indent-function 1)
  576. ;; SRFI-8
  577. (put 'receive 'scheme-indent-function 2)
  578. ;; SRFI-204 (withdrawn, but provided in many implementations, see the SRFI text)
  579. (put 'match 'scheme-indent-function 1)
  580. (put 'match-lambda 'scheme-indent-function 0)
  581. (put 'match-lambda* 'scheme-indent-function 0)
  582. (put 'match-let 'scheme-indent-function 'scheme-let-indent)
  583. (put 'match-let* 'scheme-indent-function 1)
  584. (put 'match-letrec 'scheme-indent-function 1)
  585. ;;;; MIT Scheme specific indentation.
  586. (if scheme-mit-dialect
  587. (progn
  588. (put 'fluid-let 'scheme-indent-function 1)
  589. (put 'in-package 'scheme-indent-function 1)
  590. (put 'local-declare 'scheme-indent-function 1)
  591. (put 'macro 'scheme-indent-function 1)
  592. (put 'make-environment 'scheme-indent-function 0)
  593. (put 'named-lambda 'scheme-indent-function 1)
  594. (put 'using-syntax 'scheme-indent-function 1)
  595. (put 'with-input-from-string 'scheme-indent-function 1)
  596. (put 'with-output-to-string 'scheme-indent-function 0)
  597. (put 'with-values 'scheme-indent-function 1)
  598. (put 'syntax-table-define 'scheme-indent-function 2)
  599. (put 'list-transform-positive 'scheme-indent-function 1)
  600. (put 'list-transform-negative 'scheme-indent-function 1)
  601. (put 'list-search-positive 'scheme-indent-function 1)
  602. (put 'list-search-negative 'scheme-indent-function 1)
  603. (put 'access-components 'scheme-indent-function 1)
  604. (put 'assignment-components 'scheme-indent-function 1)
  605. (put 'combination-components 'scheme-indent-function 1)
  606. (put 'comment-components 'scheme-indent-function 1)
  607. (put 'conditional-components 'scheme-indent-function 1)
  608. (put 'disjunction-components 'scheme-indent-function 1)
  609. (put 'declaration-components 'scheme-indent-function 1)
  610. (put 'definition-components 'scheme-indent-function 1)
  611. (put 'delay-components 'scheme-indent-function 1)
  612. (put 'in-package-components 'scheme-indent-function 1)
  613. (put 'lambda-components 'scheme-indent-function 1)
  614. (put 'lambda-components* 'scheme-indent-function 1)
  615. (put 'lambda-components** 'scheme-indent-function 1)
  616. (put 'open-block-components 'scheme-indent-function 1)
  617. (put 'pathname-components 'scheme-indent-function 1)
  618. (put 'procedure-components 'scheme-indent-function 1)
  619. (put 'sequence-components 'scheme-indent-function 1)
  620. (put 'unassigned\?-components 'scheme-indent-function 1)
  621. (put 'unbound\?-components 'scheme-indent-function 1)
  622. (put 'variable-components 'scheme-indent-function 1)))
  623. ;;;###autoload
  624. (add-to-list 'auto-mode-alist '("\\.\\(scm\\|sls\\|sld\\|stk\\|ss\\|sch\\)\\'" . scheme-mode))
  625. (provide 'scheme)
  626. ;;; scheme.el ends here