scheme.el 30 KB

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