scheme.el 23 KB

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