scheme.el 29 KB

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