scheme.el 26 KB

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