adamc@350
|
1 ;;; urweb-mode.el --- Major mode for editing (Standard) ML
|
adamc@350
|
2
|
adamc@350
|
3 ;; Based on sml-mode:
|
adamc@350
|
4 ;; Copyright (C) 1999,2000,2004 Stefan Monnier
|
adamc@350
|
5 ;; Copyright (C) 1994-1997 Matthew J. Morley
|
adamc@350
|
6 ;; Copyright (C) 1989 Lars Bo Nielsen
|
adamc@350
|
7 ;;
|
adamc@350
|
8 ;; Modified for urweb-mode:
|
adamc@350
|
9 ;; Copyright (C) 2008 Adam Chlipala <adamc@hcoop.net>
|
adamc@350
|
10
|
adamc@350
|
11 ;; Author: Lars Bo Nielsen
|
adamc@350
|
12 ;; Olin Shivers
|
adamc@350
|
13 ;; Fritz Knabe (?)
|
adamc@350
|
14 ;; Steven Gilmore (?)
|
adamc@350
|
15 ;; Matthew Morley <mjm@scs.leeds.ac.uk> (aka <matthew@verisity.com>)
|
adamc@350
|
16 ;; Matthias Blume <blume@cs.princeton.edu> (aka <blume@kurims.kyoto-u.ac.jp>)
|
adamc@350
|
17 ;; (Stefan Monnier) monnier@cs.yale.edu
|
adamc@350
|
18 ;; Adam Chlipala
|
adamc@350
|
19
|
adamc@350
|
20 ;; This file is not part of GNU Emacs, but it is distributed under the
|
adamc@350
|
21 ;; same conditions.
|
adamc@350
|
22
|
adamc@350
|
23 ;; This program is free software; you can redistribute it and/or
|
adamc@350
|
24 ;; modify it under the terms of the GNU General Public License as
|
adamc@350
|
25 ;; published by the Free Software Foundation; either version 2, or (at
|
adamc@350
|
26 ;; your option) any later version.
|
adamc@350
|
27
|
adamc@350
|
28 ;; This program is distributed in the hope that it will be useful, but
|
adamc@350
|
29 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
adamc@350
|
30 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
adamc@350
|
31 ;; General Public License for more details.
|
adamc@350
|
32
|
adamc@350
|
33 ;; You should have received a copy of the GNU General Public License
|
adamc@350
|
34 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
adamc@350
|
35 ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
adamc@350
|
36
|
adamc@350
|
37 ;;; Commentary:
|
adamc@350
|
38
|
adamc@350
|
39 ;;; HISTORY
|
adamc@350
|
40
|
adamc@350
|
41 ;; Still under construction: History obscure, needs a biographer as
|
adamc@350
|
42 ;; well as a M-x doctor. Change Log on request.
|
adamc@350
|
43
|
adamc@350
|
44 ;; Hacked by Olin Shivers for comint from Lars Bo Nielsen's urweb.el.
|
adamc@350
|
45
|
adamc@350
|
46 ;; Hacked by Matthew Morley to incorporate Fritz Knabe's hilite and
|
adamc@350
|
47 ;; font-lock patterns, some of Steven Gilmore's (reduced) easy-menus,
|
adamc@350
|
48 ;; and numerous bugs and bug-fixes.
|
adamc@350
|
49
|
adamc@350
|
50 ;;; DESCRIPTION
|
adamc@350
|
51
|
adamc@350
|
52 ;; See accompanying info file: urweb-mode.info
|
adamc@350
|
53
|
adamc@350
|
54 ;;; FOR YOUR .EMACS FILE
|
adamc@350
|
55
|
adamc@350
|
56 ;; If urweb-mode.el lives in some non-standard directory, you must tell
|
adamc@350
|
57 ;; emacs where to get it. This may or may not be necessary:
|
adamc@350
|
58
|
adamc@350
|
59 ;; (add-to-list 'load-path "~jones/lib/emacs/")
|
adamc@350
|
60
|
adamc@350
|
61 ;; Then to access the commands autoload urweb-mode with that command:
|
adamc@350
|
62
|
adamc@350
|
63 ;; (load "urweb-mode-startup")
|
adamc@350
|
64
|
adamc@350
|
65 ;; urweb-mode-hook is run whenever a new urweb-mode buffer is created.
|
adamc@350
|
66
|
adamc@350
|
67 ;;; Code:
|
adamc@350
|
68
|
adamc@350
|
69 (eval-when-compile (require 'cl))
|
adamc@350
|
70 (require 'urweb-util)
|
adamc@350
|
71 (require 'urweb-move)
|
adamc@350
|
72 (require 'urweb-defs)
|
adamc@350
|
73 (condition-case nil (require 'skeleton) (error nil))
|
adamc@350
|
74
|
adamc@350
|
75 ;;; VARIABLES CONTROLLING INDENTATION
|
adamc@350
|
76
|
adamc@350
|
77 (defcustom urweb-indent-level 4
|
adamc@350
|
78 "*Indentation of blocks in Ur/Web (see also `urweb-structure-indent')."
|
adamc@350
|
79 :group 'urweb
|
adamc@350
|
80 :type '(integer))
|
adamc@350
|
81
|
adamc@350
|
82 (defcustom urweb-indent-args urweb-indent-level
|
adamc@350
|
83 "*Indentation of args placed on a separate line."
|
adamc@350
|
84 :group 'urweb
|
adamc@350
|
85 :type '(integer))
|
adamc@350
|
86
|
adamc@350
|
87 (defcustom urweb-electric-semi-mode nil
|
adamc@350
|
88 "*If non-nil, `\;' will self insert, reindent the line, and do a newline.
|
adamc@350
|
89 If nil, just insert a `\;'. (To insert while t, do: \\[quoted-insert] \;)."
|
adamc@350
|
90 :group 'urweb
|
adamc@350
|
91 :type 'boolean)
|
adamc@350
|
92
|
adamc@350
|
93 (defcustom urweb-rightalign-and t
|
adamc@350
|
94 "If non-nil, right-align `and' with its leader.
|
adamc@350
|
95 If nil: If t:
|
adamc@350
|
96 datatype a = A datatype a = A
|
adamc@350
|
97 and b = B and b = B"
|
adamc@350
|
98 :group 'urweb
|
adamc@350
|
99 :type 'boolean)
|
adamc@350
|
100
|
adamc@350
|
101 ;;; OTHER GENERIC MODE VARIABLES
|
adamc@350
|
102
|
adamc@350
|
103 (defvar urweb-mode-info "urweb-mode"
|
adamc@350
|
104 "*Where to find Info file for `urweb-mode'.
|
adamc@350
|
105 The default assumes the info file \"urweb-mode.info\" is on Emacs' info
|
adamc@350
|
106 directory path. If it is not, either put the file on the standard path
|
adamc@350
|
107 or set the variable `urweb-mode-info' to the exact location of this file
|
adamc@350
|
108
|
adamc@350
|
109 (setq urweb-mode-info \"/usr/me/lib/info/urweb-mode\")
|
adamc@350
|
110
|
adamc@350
|
111 in your .emacs file. You can always set it interactively with the
|
adamc@350
|
112 set-variable command.")
|
adamc@350
|
113
|
adamc@350
|
114 (defvar urweb-mode-hook nil
|
adamc@350
|
115 "*Run upon entering `urweb-mode'.
|
adamc@350
|
116 This is a good place to put your preferred key bindings.")
|
adamc@350
|
117
|
adamc@350
|
118 ;;; CODE FOR Ur/Web-MODE
|
adamc@350
|
119
|
adamc@350
|
120 (defun urweb-mode-info ()
|
adamc@350
|
121 "Command to access the TeXinfo documentation for `urweb-mode'.
|
adamc@350
|
122 See doc for the variable `urweb-mode-info'."
|
adamc@350
|
123 (interactive)
|
adamc@350
|
124 (require 'info)
|
adamc@350
|
125 (condition-case nil
|
adamc@350
|
126 (info urweb-mode-info)
|
adamc@350
|
127 (error (progn
|
adamc@350
|
128 (describe-variable 'urweb-mode-info)
|
adamc@350
|
129 (message "Can't find it... set this variable first!")))))
|
adamc@350
|
130
|
adamc@350
|
131
|
adamc@350
|
132 ;; font-lock setup
|
adamc@350
|
133
|
adamc@350
|
134 (defconst urweb-keywords-regexp
|
adamc@350
|
135 (urweb-syms-re "and" "case" "class" "con" "constraint" "constraints"
|
adamc@350
|
136 "datatype" "else" "end" "extern" "fn" "fold"
|
adamc@350
|
137 "fun" "functor" "if" "include"
|
adamc@350
|
138 "of" "open"
|
adamc@350
|
139 "rec" "sequence" "sig" "signature"
|
adamc@350
|
140 "struct" "structure" "table" "then" "type" "val" "where"
|
adamc@350
|
141 "with"
|
adamc@350
|
142
|
adamc@350
|
143 "Name" "Type" "Unit")
|
adamc@350
|
144 "A regexp that matches any non-SQL keywords of Ur/Web.")
|
adamc@350
|
145
|
adamc@350
|
146 (defconst urweb-sql-keywords-regexp
|
adamc@350
|
147 (urweb-syms-re "SELECT" "FROM" "AS" "WHERE" "SQL" "GROUP" "ORDER" "BY"
|
adamc@350
|
148 "HAVING" "LIMIT" "OFFSET" "ALL" "UNION" "INTERSECT" "EXCEPT"
|
adamc@350
|
149 "TRUE" "FALSE" "AND" "OR" "NOT" "COUNT" "AVG" "SUM" "MIN" "MAX"
|
adamc@350
|
150 "ASC" "DESC" "INSERT" "INTO" "VALUES" "UPDATE" "SET" "DELETE")
|
adamc@350
|
151 "A regexp that matches SQL keywords.")
|
adamc@350
|
152
|
adamc@351
|
153 (defconst urweb-lident-regexp "\\<[a-z_][A-Za-z0-9_']*\\>"
|
adamc@351
|
154 "A regexp that matches lowercase Ur/Web identifiers.")
|
adamc@351
|
155
|
adamc@351
|
156 (defconst urweb-cident-regexp "\\<[A-Z][A-Za-z0-9_']*\\>"
|
adamc@351
|
157 "A regexp that matches uppercase Ur/Web identifiers.")
|
adamc@351
|
158
|
adamc@350
|
159 ;;; Font-lock settings ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
adamc@350
|
160
|
adamc@350
|
161 ;; The font lock regular expressions.
|
adamc@350
|
162
|
adamc@350
|
163 (defconst urweb-font-lock-keywords
|
adamc@350
|
164 `(;;(urweb-font-comments-and-strings)
|
adamc@350
|
165 (,(concat "\\<\\(fun\\|and\\)\\s-+\\(\\sw+\\)\\s-+[^ \t\n=]")
|
adamc@350
|
166 (1 font-lock-keyword-face)
|
adamc@353
|
167 (2 font-lock-function-name-face))
|
adamc@353
|
168 (,(concat "\\<\\(\\(data\\)?type\\|con\\|class\\)\\s-+\\(\\sw+\\)")
|
adamc@350
|
169 (1 font-lock-keyword-face)
|
adamc@352
|
170 (3 font-lock-type-def-face))
|
adamc@353
|
171 ("\\<\\(val\\|table\\|sequence\\)\\s-+\\(\\sw+\\>\\s-*\\)?\\(\\sw+\\)\\s-*[=:]"
|
adamc@350
|
172 (1 font-lock-keyword-face)
|
adamc@350
|
173 (3 font-lock-variable-name-face))
|
adamc@350
|
174 ("\\<\\(structure\\|functor\\)\\s-+\\(\\sw+\\)"
|
adamc@350
|
175 (1 font-lock-keyword-face)
|
adamc@350
|
176 (2 font-lock-module-def-face))
|
adamc@350
|
177 ("\\<\\(signature\\)\\s-+\\(\\sw+\\)"
|
adamc@350
|
178 (1 font-lock-keyword-face)
|
adamc@350
|
179 (2 font-lock-interface-def-face))
|
adamc@350
|
180
|
adamc@350
|
181 (,urweb-keywords-regexp . font-lock-keyword-face)
|
adamc@351
|
182 (,urweb-sql-keywords-regexp . font-lock-sql-face)
|
adamc@351
|
183 ; (,urweb-lident-regexp . font-lock-variable-face)
|
adamc@351
|
184 (,urweb-cident-regexp . font-lock-cvariable-face))
|
adamc@350
|
185 "Regexps matching standard Ur/Web keywords.")
|
adamc@350
|
186
|
adamc@350
|
187 (defface font-lock-type-def-face
|
adamc@350
|
188 '((t (:bold t)))
|
adamc@350
|
189 "Font Lock mode face used to highlight type definitions."
|
adamc@350
|
190 :group 'font-lock-highlighting-faces)
|
adamc@350
|
191 (defvar font-lock-type-def-face 'font-lock-type-def-face
|
adamc@350
|
192 "Face name to use for type definitions.")
|
adamc@350
|
193
|
adamc@350
|
194 (defface font-lock-module-def-face
|
adamc@350
|
195 '((t (:bold t)))
|
adamc@350
|
196 "Font Lock mode face used to highlight module definitions."
|
adamc@350
|
197 :group 'font-lock-highlighting-faces)
|
adamc@350
|
198 (defvar font-lock-module-def-face 'font-lock-module-def-face
|
adamc@350
|
199 "Face name to use for module definitions.")
|
adamc@350
|
200
|
adamc@350
|
201 (defface font-lock-interface-def-face
|
adamc@350
|
202 '((t (:bold t)))
|
adamc@350
|
203 "Font Lock mode face used to highlight interface definitions."
|
adamc@350
|
204 :group 'font-lock-highlighting-faces)
|
adamc@350
|
205 (defvar font-lock-interface-def-face 'font-lock-interface-def-face
|
adamc@350
|
206 "Face name to use for interface definitions.")
|
adamc@350
|
207
|
adamc@350
|
208 (defface font-lock-sql-face
|
adamc@350
|
209 '((t (:bold t)))
|
adamc@350
|
210 "Font Lock mode face used to highlight SQL keywords."
|
adamc@350
|
211 :group 'font-lock-highlighting-faces)
|
adamc@350
|
212 (defvar font-lock-sql-face 'font-lock-sql-face
|
adamc@350
|
213 "Face name to use for SQL keywords.")
|
adamc@350
|
214
|
adamc@351
|
215 ;(defface font-lock-variable-face
|
adamc@351
|
216 ; '((t (:foreground "green")))
|
adamc@351
|
217 ; "Font Lock mode face used to highlight lowercase identifiers."
|
adamc@351
|
218 ; :group 'font-lock-highlighting-faces)
|
adamc@351
|
219 ;(defvar font-lock-variable-face 'font-lock-variable-face
|
adamc@351
|
220 ; "Face name to use for lowercase identifiers.")
|
adamc@351
|
221
|
adamc@351
|
222 (defface font-lock-cvariable-face
|
adamc@351
|
223 '((t (:foreground "dark blue")))
|
adamc@351
|
224 "Font Lock mode face used to highlight capitalized identifiers."
|
adamc@351
|
225 :group 'font-lock-highlighting-faces)
|
adamc@351
|
226 (defvar font-lock-cvariable-face 'font-lock-cvariable-face
|
adamc@351
|
227 "Face name to use for capitalized identifiers.")
|
adamc@351
|
228
|
adamc@350
|
229 ;;
|
adamc@350
|
230 ;; Code to handle nested comments and unusual string escape sequences
|
adamc@350
|
231 ;;
|
adamc@350
|
232
|
adamc@350
|
233 (defsyntax urweb-syntax-prop-table
|
adamc@350
|
234 '((?\\ . ".") (?* . "."))
|
adamc@350
|
235 "Syntax table for text-properties")
|
adamc@350
|
236
|
adamc@350
|
237 ;; For Emacsen that have no built-in support for nested comments
|
adamc@350
|
238 (defun urweb-get-depth-st ()
|
adamc@350
|
239 (save-excursion
|
adamc@350
|
240 (let* ((disp (if (eq (char-before) ?\)) (progn (backward-char) -1) nil))
|
adamc@350
|
241 (_ (backward-char))
|
adamc@350
|
242 (disp (if (eq (char-before) ?\() (progn (backward-char) 0) disp))
|
adamc@350
|
243 (pt (point)))
|
adamc@350
|
244 (when disp
|
adamc@350
|
245 (let* ((depth
|
adamc@350
|
246 (save-match-data
|
adamc@350
|
247 (if (re-search-backward "\\*)\\|(\\*" nil t)
|
adamc@350
|
248 (+ (or (get-char-property (point) 'comment-depth) 0)
|
adamc@350
|
249 (case (char-after) (?\( 1) (?* 0))
|
adamc@350
|
250 disp)
|
adamc@350
|
251 0)))
|
adamc@350
|
252 (depth (if (> depth 0) depth)))
|
adamc@350
|
253 (put-text-property pt (1+ pt) 'comment-depth depth)
|
adamc@350
|
254 (when depth urweb-syntax-prop-table))))))
|
adamc@350
|
255
|
adamc@350
|
256 (defconst urweb-font-lock-syntactic-keywords
|
adamc@350
|
257 `(("^\\s-*\\(\\\\\\)" (1 ',urweb-syntax-prop-table))
|
adamc@350
|
258 ,@(unless urweb-builtin-nested-comments-flag
|
adamc@350
|
259 '(("(?\\(\\*\\))?" (1 (urweb-get-depth-st)))))))
|
adamc@350
|
260
|
adamc@350
|
261 (defconst urweb-font-lock-defaults
|
adamc@350
|
262 '(urweb-font-lock-keywords nil nil ((?_ . "w") (?' . "w")) nil
|
adamc@350
|
263 (font-lock-syntactic-keywords . urweb-font-lock-syntactic-keywords)))
|
adamc@350
|
264
|
adamc@350
|
265 ;;;;
|
adamc@350
|
266 ;;;; Imenu support
|
adamc@350
|
267 ;;;;
|
adamc@350
|
268
|
adamc@350
|
269 (defvar urweb-imenu-regexp
|
adamc@350
|
270 (concat "^[ \t]*\\(let[ \t]+\\)?"
|
adamc@350
|
271 (regexp-opt (append urweb-module-head-syms
|
adamc@350
|
272 '("and" "fun" "datatype" "type")) t)
|
adamc@350
|
273 "\\>"))
|
adamc@350
|
274
|
adamc@350
|
275 (defun urweb-imenu-create-index ()
|
adamc@350
|
276 (let (alist)
|
adamc@350
|
277 (goto-char (point-max))
|
adamc@350
|
278 (while (re-search-backward urweb-imenu-regexp nil t)
|
adamc@350
|
279 (save-excursion
|
adamc@350
|
280 (let ((kind (match-string 2))
|
adamc@350
|
281 (column (progn (goto-char (match-beginning 2)) (current-column)))
|
adamc@350
|
282 (location
|
adamc@350
|
283 (progn (goto-char (match-end 0))
|
adamc@350
|
284 (urweb-forward-spaces)
|
adamc@350
|
285 (when (looking-at urweb-tyvarseq-re)
|
adamc@350
|
286 (goto-char (match-end 0)))
|
adamc@350
|
287 (point)))
|
adamc@350
|
288 (name (urweb-forward-sym)))
|
adamc@350
|
289 ;; Eliminate trivial renamings.
|
adamc@350
|
290 (when (or (not (member kind '("structure" "signature")))
|
adamc@350
|
291 (progn (search-forward "=")
|
adamc@350
|
292 (urweb-forward-spaces)
|
adamc@350
|
293 (looking-at "sig\\|struct")))
|
adamc@350
|
294 (push (cons (concat (make-string (/ column 2) ?\ ) name) location)
|
adamc@350
|
295 alist)))))
|
adamc@350
|
296 alist))
|
adamc@350
|
297
|
adamc@350
|
298 ;;; MORE CODE FOR URWEB-MODE
|
adamc@350
|
299
|
adamc@350
|
300 ;;;###autoload (add-to-list 'load-path (file-name-directory load-file-name))
|
adamc@350
|
301 ;;;###autoload
|
adamc@350
|
302 (add-to-list 'auto-mode-alist '("\\.s\\(ml\\|ig\\)\\'" . urweb-mode))
|
adamc@350
|
303
|
adamc@350
|
304 ;;;###autoload
|
adamc@350
|
305 (define-derived-mode urweb-mode fundamental-mode "Ur/Web"
|
adamc@350
|
306 "\\<urweb-mode-map>Major mode for editing Ur/Web code.
|
adamc@350
|
307 This mode runs `urweb-mode-hook' just before exiting.
|
adamc@350
|
308 \\{urweb-mode-map}"
|
adamc@350
|
309 (set (make-local-variable 'font-lock-defaults) urweb-font-lock-defaults)
|
adamc@350
|
310 (set (make-local-variable 'outline-regexp) urweb-outline-regexp)
|
adamc@350
|
311 (set (make-local-variable 'imenu-create-index-function)
|
adamc@350
|
312 'urweb-imenu-create-index)
|
adamc@350
|
313 (set (make-local-variable 'add-log-current-defun-function)
|
adamc@350
|
314 'urweb-current-fun-name)
|
adamc@350
|
315 ;; Treat paragraph-separators in comments as paragraph-separators.
|
adamc@350
|
316 (set (make-local-variable 'paragraph-separate)
|
adamc@350
|
317 (concat "\\([ \t]*\\*)?\\)?\\(" paragraph-separate "\\)"))
|
adamc@350
|
318 (set (make-local-variable 'require-final-newline) t)
|
adamc@350
|
319 ;; forward-sexp-function is an experimental variable in my hacked Emacs.
|
adamc@350
|
320 (set (make-local-variable 'forward-sexp-function) 'urweb-user-forward-sexp)
|
adamc@350
|
321 ;; For XEmacs
|
adamc@350
|
322 (easy-menu-add urweb-mode-menu)
|
adamc@350
|
323 ;; Compatibility. FIXME: we should use `-' in Emacs-CVS.
|
adamc@350
|
324 (unless (boundp 'skeleton-positions) (set (make-local-variable '@) nil))
|
adamc@350
|
325 (urweb-mode-variables))
|
adamc@350
|
326
|
adamc@350
|
327 (defun urweb-mode-variables ()
|
adamc@350
|
328 (set-syntax-table urweb-mode-syntax-table)
|
adamc@350
|
329 (setq local-abbrev-table urweb-mode-abbrev-table)
|
adamc@350
|
330 ;; A paragraph is separated by blank lines or ^L only.
|
adamc@350
|
331
|
adamc@350
|
332 (set (make-local-variable 'indent-line-function) 'urweb-indent-line)
|
adamc@350
|
333 (set (make-local-variable 'comment-start) "(* ")
|
adamc@350
|
334 (set (make-local-variable 'comment-end) " *)")
|
adamc@350
|
335 (set (make-local-variable 'comment-nested) t)
|
adamc@350
|
336 ;;(set (make-local-variable 'block-comment-start) "* ")
|
adamc@350
|
337 ;;(set (make-local-variable 'block-comment-end) "")
|
adamc@350
|
338 ;; (set (make-local-variable 'comment-column) 40)
|
adamc@350
|
339 (set (make-local-variable 'comment-start-skip) "(\\*+\\s-*"))
|
adamc@350
|
340
|
adamc@350
|
341 (defun urweb-funname-of-and ()
|
adamc@350
|
342 "Name of the function this `and' defines, or nil if not a function.
|
adamc@350
|
343 Point has to be right after the `and' symbol and is not preserved."
|
adamc@350
|
344 (urweb-forward-spaces)
|
adamc@350
|
345 (if (looking-at urweb-tyvarseq-re) (goto-char (match-end 0)))
|
adamc@350
|
346 (let ((sym (urweb-forward-sym)))
|
adamc@350
|
347 (urweb-forward-spaces)
|
adamc@350
|
348 (unless (or (member sym '(nil "d="))
|
adamc@350
|
349 (member (urweb-forward-sym) '("d=")))
|
adamc@350
|
350 sym)))
|
adamc@350
|
351
|
adamc@350
|
352 ;;; INDENTATION !!!
|
adamc@350
|
353
|
adamc@350
|
354 (defun urweb-mark-function ()
|
adamc@350
|
355 "Synonym for `mark-paragraph' -- sorry.
|
adamc@350
|
356 If anyone has a good algorithm for this..."
|
adamc@350
|
357 (interactive)
|
adamc@350
|
358 (mark-paragraph))
|
adamc@350
|
359
|
adamc@350
|
360 (defun urweb-indent-line ()
|
adamc@350
|
361 "Indent current line of Ur/Web code."
|
adamc@350
|
362 (interactive)
|
adamc@350
|
363 (let ((savep (> (current-column) (current-indentation)))
|
adamc@350
|
364 (indent (max (or (ignore-errors (urweb-calculate-indentation)) 0) 0)))
|
adamc@350
|
365 (if savep
|
adamc@350
|
366 (save-excursion (indent-line-to indent))
|
adamc@350
|
367 (indent-line-to indent))))
|
adamc@350
|
368
|
adamc@350
|
369 (defun urweb-back-to-outer-indent ()
|
adamc@350
|
370 "Unindents to the next outer level of indentation."
|
adamc@350
|
371 (interactive)
|
adamc@350
|
372 (save-excursion
|
adamc@350
|
373 (beginning-of-line)
|
adamc@350
|
374 (skip-chars-forward "\t ")
|
adamc@350
|
375 (let ((start-column (current-column))
|
adamc@350
|
376 (indent (current-column)))
|
adamc@350
|
377 (if (> start-column 0)
|
adamc@350
|
378 (progn
|
adamc@350
|
379 (save-excursion
|
adamc@350
|
380 (while (>= indent start-column)
|
adamc@350
|
381 (if (re-search-backward "^[^\n]" nil t)
|
adamc@350
|
382 (setq indent (current-indentation))
|
adamc@350
|
383 (setq indent 0))))
|
adamc@350
|
384 (backward-delete-char-untabify (- start-column indent)))))))
|
adamc@350
|
385
|
adamc@350
|
386 (defun urweb-find-comment-indent ()
|
adamc@350
|
387 (save-excursion
|
adamc@350
|
388 (let ((depth 1))
|
adamc@350
|
389 (while (> depth 0)
|
adamc@350
|
390 (if (re-search-backward "(\\*\\|\\*)" nil t)
|
adamc@350
|
391 (cond
|
adamc@350
|
392 ;; FIXME: That's just a stop-gap.
|
adamc@350
|
393 ((eq (get-text-property (point) 'face) 'font-lock-string-face))
|
adamc@350
|
394 ((looking-at "*)") (incf depth))
|
adamc@350
|
395 ((looking-at comment-start-skip) (decf depth)))
|
adamc@350
|
396 (setq depth -1)))
|
adamc@350
|
397 (if (= depth 0)
|
adamc@350
|
398 (1+ (current-column))
|
adamc@350
|
399 nil))))
|
adamc@350
|
400
|
adamc@350
|
401 (defun urweb-calculate-indentation ()
|
adamc@350
|
402 (save-excursion
|
adamc@350
|
403 (beginning-of-line) (skip-chars-forward "\t ")
|
adamc@350
|
404 (urweb-with-ist
|
adamc@350
|
405 ;; Indentation for comments alone on a line, matches the
|
adamc@350
|
406 ;; proper indentation of the next line.
|
adamc@350
|
407 (when (looking-at "(\\*") (urweb-forward-spaces))
|
adamc@350
|
408 (let (data
|
adamc@350
|
409 (sym (save-excursion (urweb-forward-sym))))
|
adamc@350
|
410 (or
|
adamc@350
|
411 ;; Allow the user to override the indentation.
|
adamc@350
|
412 (when (looking-at (concat ".*" (regexp-quote comment-start)
|
adamc@350
|
413 "[ \t]*fixindent[ \t]*"
|
adamc@350
|
414 (regexp-quote comment-end)))
|
adamc@350
|
415 (current-indentation))
|
adamc@350
|
416
|
adamc@350
|
417 ;; Continued comment.
|
adamc@350
|
418 (and (looking-at "\\*") (urweb-find-comment-indent))
|
adamc@350
|
419
|
adamc@350
|
420 ;; Continued string ? (Added 890113 lbn)
|
adamc@350
|
421 (and (looking-at "\\\\")
|
adamc@350
|
422 (save-excursion
|
adamc@350
|
423 (if (save-excursion (previous-line 1)
|
adamc@350
|
424 (beginning-of-line)
|
adamc@350
|
425 (looking-at "[\t ]*\\\\"))
|
adamc@350
|
426 (progn (previous-line 1) (current-indentation))
|
adamc@350
|
427 (if (re-search-backward "[^\\\\]\"" nil t)
|
adamc@350
|
428 (1+ (current-column))
|
adamc@350
|
429 0))))
|
adamc@350
|
430
|
adamc@350
|
431 ;; Closing parens. Could be handled below with `urweb-indent-relative'?
|
adamc@350
|
432 (and (looking-at "\\s)")
|
adamc@350
|
433 (save-excursion
|
adamc@350
|
434 (skip-syntax-forward ")")
|
adamc@350
|
435 (backward-sexp 1)
|
adamc@350
|
436 (if (urweb-dangling-sym)
|
adamc@350
|
437 (urweb-indent-default 'noindent)
|
adamc@350
|
438 (current-column))))
|
adamc@350
|
439
|
adamc@350
|
440 (and (setq data (assoc sym urweb-close-paren))
|
adamc@350
|
441 (urweb-indent-relative sym data))
|
adamc@350
|
442
|
adamc@350
|
443 (and (member sym urweb-starters-syms)
|
adamc@350
|
444 (urweb-indent-starter sym))
|
adamc@350
|
445
|
adamc@350
|
446 (and (string= sym "|") (urweb-indent-pipe))
|
adamc@350
|
447
|
adamc@350
|
448 (urweb-indent-arg)
|
adamc@350
|
449 (urweb-indent-default))))))
|
adamc@350
|
450
|
adamc@350
|
451 (defsubst urweb-bolp ()
|
adamc@350
|
452 (save-excursion (skip-chars-backward " \t|") (bolp)))
|
adamc@350
|
453
|
adamc@350
|
454 (defun urweb-indent-starter (orig-sym)
|
adamc@350
|
455 "Return the indentation to use for a symbol in `urweb-starters-syms'.
|
adamc@350
|
456 Point should be just before the symbol ORIG-SYM and is not preserved."
|
adamc@350
|
457 (let ((sym (unless (save-excursion (urweb-backward-arg))
|
adamc@350
|
458 (urweb-backward-spaces)
|
adamc@350
|
459 (urweb-backward-sym))))
|
adamc@350
|
460 (if (member sym '(";" "d=")) (setq sym nil))
|
adamc@350
|
461 (if sym (urweb-get-sym-indent sym)
|
adamc@350
|
462 ;; FIXME: this can take a *long* time !!
|
adamc@350
|
463 (setq sym (urweb-find-matching-starter urweb-starters-syms))
|
adamc@350
|
464 ;; Don't align with `and' because it might be specially indented.
|
adamc@350
|
465 (if (and (or (equal orig-sym "and") (not (equal sym "and")))
|
adamc@350
|
466 (urweb-bolp))
|
adamc@350
|
467 (+ (current-column)
|
adamc@350
|
468 (if (and urweb-rightalign-and (equal orig-sym "and"))
|
adamc@350
|
469 (- (length sym) 3) 0))
|
adamc@350
|
470 (urweb-indent-starter orig-sym)))))
|
adamc@350
|
471
|
adamc@350
|
472 (defun urweb-indent-relative (sym data)
|
adamc@350
|
473 (save-excursion
|
adamc@350
|
474 (urweb-forward-sym) (urweb-backward-sexp nil)
|
adamc@350
|
475 (unless (second data) (urweb-backward-spaces) (urweb-backward-sym))
|
adamc@350
|
476 (+ (or (cdr (assoc sym urweb-symbol-indent)) 0)
|
adamc@350
|
477 (urweb-delegated-indent))))
|
adamc@350
|
478
|
adamc@350
|
479 (defun urweb-indent-pipe ()
|
adamc@350
|
480 (let ((sym (urweb-find-matching-starter urweb-pipeheads
|
adamc@350
|
481 (urweb-op-prec "|" 'back))))
|
adamc@350
|
482 (when sym
|
adamc@350
|
483 (if (string= sym "|")
|
adamc@350
|
484 (if (urweb-bolp) (current-column) (urweb-indent-pipe))
|
adamc@350
|
485 (let ((pipe-indent (or (cdr (assoc "|" urweb-symbol-indent)) -2)))
|
adamc@350
|
486 (when (or (member sym '("datatype"))
|
adamc@350
|
487 (and (equal sym "and")
|
adamc@350
|
488 (save-excursion
|
adamc@350
|
489 (forward-word 1)
|
adamc@350
|
490 (not (urweb-funname-of-and)))))
|
adamc@350
|
491 (re-search-forward "="))
|
adamc@350
|
492 (urweb-forward-sym)
|
adamc@350
|
493 (urweb-forward-spaces)
|
adamc@350
|
494 (+ pipe-indent (current-column)))))))
|
adamc@350
|
495
|
adamc@350
|
496 (defun urweb-find-forward (re)
|
adamc@350
|
497 (urweb-forward-spaces)
|
adamc@350
|
498 (while (and (not (looking-at re))
|
adamc@350
|
499 (progn
|
adamc@350
|
500 (or (ignore-errors (forward-sexp 1) t) (forward-char 1))
|
adamc@350
|
501 (urweb-forward-spaces)
|
adamc@350
|
502 (not (looking-at re))))))
|
adamc@350
|
503
|
adamc@350
|
504 (defun urweb-indent-arg ()
|
adamc@350
|
505 (and (save-excursion (ignore-errors (urweb-forward-arg)))
|
adamc@350
|
506 ;;(not (looking-at urweb-not-arg-re))
|
adamc@350
|
507 ;; looks like a function or an argument
|
adamc@350
|
508 (urweb-move-if (urweb-backward-arg))
|
adamc@350
|
509 ;; an argument
|
adamc@350
|
510 (if (save-excursion (not (urweb-backward-arg)))
|
adamc@350
|
511 ;; a first argument
|
adamc@350
|
512 (+ (current-column) urweb-indent-args)
|
adamc@350
|
513 ;; not a first arg
|
adamc@350
|
514 (while (and (/= (current-column) (current-indentation))
|
adamc@350
|
515 (urweb-move-if (urweb-backward-arg))))
|
adamc@350
|
516 (unless (save-excursion (urweb-backward-arg))
|
adamc@350
|
517 ;; all earlier args are on the same line
|
adamc@350
|
518 (urweb-forward-arg) (urweb-forward-spaces))
|
adamc@350
|
519 (current-column))))
|
adamc@350
|
520
|
adamc@350
|
521 (defun urweb-get-indent (data sym)
|
adamc@350
|
522 (let (d)
|
adamc@350
|
523 (cond
|
adamc@350
|
524 ((not (listp data)) data)
|
adamc@350
|
525 ((setq d (member sym data)) (cadr d))
|
adamc@350
|
526 ((and (consp data) (not (stringp (car data)))) (car data))
|
adamc@350
|
527 (t urweb-indent-level))))
|
adamc@350
|
528
|
adamc@350
|
529 (defun urweb-dangling-sym ()
|
adamc@350
|
530 "Non-nil if the symbol after point is dangling.
|
adamc@350
|
531 The symbol can be an Ur/Web symbol or an open-paren. \"Dangling\" means that
|
adamc@350
|
532 it is not on its own line but is the last element on that line."
|
adamc@350
|
533 (save-excursion
|
adamc@350
|
534 (and (not (urweb-bolp))
|
adamc@350
|
535 (< (urweb-point-after (end-of-line))
|
adamc@350
|
536 (urweb-point-after (or (urweb-forward-sym) (skip-syntax-forward "("))
|
adamc@350
|
537 (urweb-forward-spaces))))))
|
adamc@350
|
538
|
adamc@350
|
539 (defun urweb-delegated-indent ()
|
adamc@350
|
540 (if (urweb-dangling-sym)
|
adamc@350
|
541 (urweb-indent-default 'noindent)
|
adamc@350
|
542 (urweb-move-if (backward-word 1)
|
adamc@350
|
543 (looking-at urweb-agglomerate-re))
|
adamc@350
|
544 (current-column)))
|
adamc@350
|
545
|
adamc@350
|
546 (defun urweb-get-sym-indent (sym &optional style)
|
adamc@350
|
547 "Find the indentation for the SYM we're `looking-at'.
|
adamc@350
|
548 If indentation is delegated, point will move to the start of the parent.
|
adamc@350
|
549 Optional argument STYLE is currently ignored."
|
adamc@350
|
550 (assert (equal sym (save-excursion (urweb-forward-sym))))
|
adamc@350
|
551 (save-excursion
|
adamc@350
|
552 (let ((delegate (and (not (equal sym "end")) (assoc sym urweb-close-paren)))
|
adamc@350
|
553 (head-sym sym))
|
adamc@350
|
554 (when (and delegate (not (eval (third delegate))))
|
adamc@350
|
555 ;;(urweb-find-match-backward sym delegate)
|
adamc@350
|
556 (urweb-forward-sym) (urweb-backward-sexp nil)
|
adamc@350
|
557 (setq head-sym
|
adamc@350
|
558 (if (second delegate)
|
adamc@350
|
559 (save-excursion (urweb-forward-sym))
|
adamc@350
|
560 (urweb-backward-spaces) (urweb-backward-sym))))
|
adamc@350
|
561
|
adamc@350
|
562 (let ((idata (assoc head-sym urweb-indent-rule)))
|
adamc@350
|
563 (when idata
|
adamc@350
|
564 ;;(if (or style (not delegate))
|
adamc@350
|
565 ;; normal indentation
|
adamc@350
|
566 (let ((indent (urweb-get-indent (cdr idata) sym)))
|
adamc@350
|
567 (when indent (+ (urweb-delegated-indent) indent)))
|
adamc@350
|
568 ;; delgate indentation to the parent
|
adamc@350
|
569 ;;(urweb-forward-sym) (urweb-backward-sexp nil)
|
adamc@350
|
570 ;;(let* ((parent-sym (save-excursion (urweb-forward-sym)))
|
adamc@350
|
571 ;; (parent-indent (cdr (assoc parent-sym urweb-indent-starters))))
|
adamc@350
|
572 ;; check the special rules
|
adamc@350
|
573 ;;(+ (urweb-delegated-indent)
|
adamc@350
|
574 ;; (or (urweb-get-indent (cdr indent-data) 1 'strict)
|
adamc@350
|
575 ;; (urweb-get-indent (cdr parent-indent) 1 'strict)
|
adamc@350
|
576 ;; (urweb-get-indent (cdr indent-data) 0)
|
adamc@350
|
577 ;; (urweb-get-indent (cdr parent-indent) 0))))))))
|
adamc@350
|
578 )))))
|
adamc@350
|
579
|
adamc@350
|
580 (defun urweb-indent-default (&optional noindent)
|
adamc@350
|
581 (let* ((sym-after (save-excursion (urweb-forward-sym)))
|
adamc@350
|
582 (_ (urweb-backward-spaces))
|
adamc@350
|
583 (sym-before (urweb-backward-sym))
|
adamc@350
|
584 (sym-indent (and sym-before (urweb-get-sym-indent sym-before)))
|
adamc@350
|
585 (indent-after (or (cdr (assoc sym-after urweb-symbol-indent)) 0)))
|
adamc@350
|
586 (when (equal sym-before "end")
|
adamc@350
|
587 ;; I don't understand what's really happening here, but when
|
adamc@350
|
588 ;; it's `end' clearly, we need to do something special.
|
adamc@350
|
589 (forward-word 1)
|
adamc@350
|
590 (setq sym-before nil sym-indent nil))
|
adamc@350
|
591 (cond
|
adamc@350
|
592 (sym-indent
|
adamc@350
|
593 ;; the previous sym is an indentation introducer: follow the rule
|
adamc@350
|
594 (if noindent
|
adamc@350
|
595 ;;(current-column)
|
adamc@350
|
596 sym-indent
|
adamc@350
|
597 (+ sym-indent indent-after)))
|
adamc@350
|
598 ;; If we're just after a hanging open paren.
|
adamc@350
|
599 ((and (eq (char-syntax (preceding-char)) ?\()
|
adamc@350
|
600 (save-excursion (backward-char) (urweb-dangling-sym)))
|
adamc@350
|
601 (backward-char)
|
adamc@350
|
602 (urweb-indent-default))
|
adamc@350
|
603 (t
|
adamc@350
|
604 ;; default-default
|
adamc@350
|
605 (let* ((prec-after (urweb-op-prec sym-after 'back))
|
adamc@350
|
606 (prec (or (urweb-op-prec sym-before 'back) prec-after 100)))
|
adamc@350
|
607 ;; go back until you hit a symbol that has a lower prec than the
|
adamc@350
|
608 ;; "current one", or until you backed over a sym that has the same prec
|
adamc@350
|
609 ;; but is at the beginning of a line.
|
adamc@350
|
610 (while (and (not (urweb-bolp))
|
adamc@350
|
611 (while (urweb-move-if (urweb-backward-sexp (1- prec))))
|
adamc@350
|
612 (not (urweb-bolp)))
|
adamc@350
|
613 (while (urweb-move-if (urweb-backward-sexp prec))))
|
adamc@350
|
614 (if noindent
|
adamc@350
|
615 ;; the `noindent' case does back over an introductory symbol
|
adamc@350
|
616 ;; such as `fun', ...
|
adamc@350
|
617 (progn
|
adamc@350
|
618 (urweb-move-if
|
adamc@350
|
619 (urweb-backward-spaces)
|
adamc@350
|
620 (member (urweb-backward-sym) urweb-starters-syms))
|
adamc@350
|
621 (current-column))
|
adamc@350
|
622 ;; Use `indent-after' for cases such as when , or ; should be
|
adamc@350
|
623 ;; outdented so that their following terms are aligned.
|
adamc@350
|
624 (+ (if (progn
|
adamc@350
|
625 (if (equal sym-after ";")
|
adamc@350
|
626 (urweb-move-if
|
adamc@350
|
627 (urweb-backward-spaces)
|
adamc@350
|
628 (member (urweb-backward-sym) urweb-starters-syms)))
|
adamc@350
|
629 (and sym-after (not (looking-at sym-after))))
|
adamc@350
|
630 indent-after 0)
|
adamc@350
|
631 (current-column))))))))
|
adamc@350
|
632
|
adamc@350
|
633
|
adamc@350
|
634 ;; maybe `|' should be set to word-syntax in our temp syntax table ?
|
adamc@350
|
635 (defun urweb-current-indentation ()
|
adamc@350
|
636 (save-excursion
|
adamc@350
|
637 (beginning-of-line)
|
adamc@350
|
638 (skip-chars-forward " \t|")
|
adamc@350
|
639 (current-column)))
|
adamc@350
|
640
|
adamc@350
|
641
|
adamc@350
|
642 (defun urweb-find-matching-starter (syms &optional prec)
|
adamc@350
|
643 (let (sym)
|
adamc@350
|
644 (ignore-errors
|
adamc@350
|
645 (while
|
adamc@350
|
646 (progn (urweb-backward-sexp prec)
|
adamc@350
|
647 (setq sym (save-excursion (urweb-forward-sym)))
|
adamc@350
|
648 (not (or (member sym syms) (bobp)))))
|
adamc@350
|
649 (if (member sym syms) sym))))
|
adamc@350
|
650
|
adamc@350
|
651 (defun urweb-skip-siblings ()
|
adamc@350
|
652 (while (and (not (bobp)) (urweb-backward-arg))
|
adamc@350
|
653 (urweb-find-matching-starter urweb-starters-syms)))
|
adamc@350
|
654
|
adamc@350
|
655 (defun urweb-beginning-of-defun ()
|
adamc@350
|
656 (let ((sym (urweb-find-matching-starter urweb-starters-syms)))
|
adamc@350
|
657 (if (member sym '("fun" "and" "functor" "signature" "structure"
|
adamc@350
|
658 "datatype"))
|
adamc@350
|
659 (save-excursion (urweb-forward-sym) (urweb-forward-spaces)
|
adamc@350
|
660 (urweb-forward-sym))
|
adamc@350
|
661 ;; We're inside a "non function declaration": let's skip all other
|
adamc@350
|
662 ;; declarations that we find at the same level and try again.
|
adamc@350
|
663 (urweb-skip-siblings)
|
adamc@350
|
664 ;; Obviously, let's not try again if we're at bobp.
|
adamc@350
|
665 (unless (bobp) (urweb-beginning-of-defun)))))
|
adamc@350
|
666
|
adamc@350
|
667 (defcustom urweb-max-name-components 3
|
adamc@350
|
668 "Maximum number of components to use for the current function name."
|
adamc@350
|
669 :group 'urweb
|
adamc@350
|
670 :type 'integer)
|
adamc@350
|
671
|
adamc@350
|
672 (defun urweb-current-fun-name ()
|
adamc@350
|
673 (save-excursion
|
adamc@350
|
674 (let ((count urweb-max-name-components)
|
adamc@350
|
675 fullname name)
|
adamc@350
|
676 (end-of-line)
|
adamc@350
|
677 (while (and (> count 0)
|
adamc@350
|
678 (setq name (urweb-beginning-of-defun)))
|
adamc@350
|
679 (decf count)
|
adamc@350
|
680 (setq fullname (if fullname (concat name "." fullname) name))
|
adamc@350
|
681 ;; Skip all other declarations that we find at the same level.
|
adamc@350
|
682 (urweb-skip-siblings))
|
adamc@350
|
683 fullname)))
|
adamc@350
|
684
|
adamc@350
|
685 (provide 'urweb-mode)
|
adamc@350
|
686
|
adamc@350
|
687 ;;; urweb-mode.el ends here
|