Debian Codegraph @

ide/emacs/esml-mlb-mode.el

raw
1 ;;; esml-mlb-mode.el --- esml-mlb-mode.el -*- lexical-binding: t; -*-
2 ;; Copyright (C) 2005-2007 Vesa Karvonen
3 ;;
4 ;; MLton is released under a HPND-style license.
5 ;; See the file MLton-LICENSE for details.
6
7 (require 'esml-util)
8
9 ;; Emacs mode for editing ML Basis files
10 ;;
11 ;; Installation
12 ;; ============
13 ;;
14 ;; - Push the path to this file (and `esml-util.el') to `load-path' and
15 ;; either
16 ;; (require 'esml-mlb-mode)
17 ;; or
18 ;; (autoload 'esml-mlb-mode "esml-mlb-mode")
19 ;; (add-to-list 'auto-mode-alist '("\\.mlb\\'" . esml-mlb-mode))
20 ;; in your Emacs initialization file.
21 ;;
22 ;; Alternatively you could use `load-file'.
23 ;;
24 ;; Beware that (at least) Tuareg mode may already be associated to .mlb
25 ;; files. You need to add ML Basis mode to `auto-mode-alist' after
26 ;; Tuareg mode.
27 ;;
28 ;; - Check the `esml-mlb' customization group.
29 ;;
30 ;; Ideas for future development
31 ;; ============================
32 ;;
33 ;; - customisable indentation
34 ;; - movement
35 ;; - type-check / compile / compile-and-run
36 ;; - find-structure / find-signature / find-functor
37 ;; - highlight only binding occurances of basids
38 ;; - find-binding-occurance (of a basid)
39 ;; - support doc strings in mlb files
40
41 ;; TBD:
42 ;; - fix indentation bugs
43 ;; - use something more robust than `shell-command' to run shell commands
44
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; Prelude
47
48 (defvar esml-mlb-load-time t)
49
50 (defun esml-mlb-set-custom-and-update (sym val)
51 (custom-set-default sym val)
52 (unless esml-mlb-load-time
53 (esml-mlb-update)))
54
55 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
56 ;; Customization
57
58 (defgroup esml-mlb nil
59 "Major mode for editing ML Basis files."
60 :group 'sml)
61
62 (defcustom esml-mlb-additional-annotations
63 '()
64 "Additional annotations accepted by your compiler(s). Note that ML
65 Basis mode runs the `esml-mlb-show-annotations-command' to query available
66 annotations automatically."
67 :type '(repeat (cons :tag "Annotation"
68 (string :tag "Name")
69 (repeat :tag "Values starting with the default"
70 string)))
71 :set 'esml-mlb-set-custom-and-update
72 :group 'esml-mlb)
73
74 (defcustom esml-mlb-additional-path-variables
75 '()
76 "Additional path variables that can not be found in the path map files
77 specified by `esml-mlb-mlb-path-map-files' or by running the command
78 `esml-mlb-show-path-map-command'."
79 :type '(repeat (cons (string :tag "Name") (string :tag "Value")))
80 :set 'esml-mlb-set-custom-and-update
81 :group 'esml-mlb)
82
83 (defcustom esml-mlb-completion-ignored-files-regexp "\\.[^.].*\\|CVS/\\|.*~"
84 "Completion ignores files (and directories) whose names match this
85 regexp."
86 :type 'regexp
87 :group 'esml-mlb)
88
89 (defcustom esml-mlb-indentation-offset 3
90 "Basic offset for indentation."
91 :type 'integer
92 :group 'esml-mlb)
93
94 (defcustom esml-mlb-key-bindings
95 '(("[tab]"
96 . esml-mlb-indent-line-or-complete)
97 ("[(control c) (control f)]"
98 . esml-mlb-find-file-at-point)
99 ("[(control c) (control s)]"
100 . esml-mlb-show-basis))
101 "Key bindings for the ML Basis mode. The key specifications must be in a
102 format accepted by the function `define-key'. Hint: You might want to type
103 `M-x describe-function esml-mlb <TAB>' to see the available commands."
104 :type '(repeat (cons :tag "Key Binding"
105 (string :tag "Key")
106 (function :tag "Command")))
107 :group 'esml-mlb)
108
109 (defcustom esml-mlb-mlb-path-map-files
110 '("~/.mlton/mlb-path-map"
111 "/usr/lib/mlton/mlb-path-map")
112 "Files to search for definitions of path variables."
113 :type '(repeat file)
114 :set 'esml-mlb-set-custom-and-update
115 :group 'esml-mlb)
116
117 (defcustom esml-mlb-path-suffix-regexp "fun\\|mlb\\|sig\\|sml"
118 "Regexp for matching valid path name suffices. Completion only considers
119 files whose extension matches this regexp."
120 :type 'regexp
121 :set 'esml-mlb-set-custom-and-update
122 :group 'esml-mlb)
123
124 (defcustom esml-mlb-show-annotations-command
125 "mlton -expert true -show anns"
126 "Shell command used to query the available annotations."
127 :type 'string
128 :set 'esml-mlb-set-custom-and-update
129 :group 'esml-mlb)
130
131 (defcustom esml-mlb-show-basis-command
132 "mlton -stop tc -show-basis %t %f"
133 "Shell command used to pretty print the basis defined by an MLB file.
134 `%t' is replaced by the name of a temporary file and `%f' is replaced by
135 the name of the MLB file."
136 :type 'string
137 :group 'esml-mlb)
138
139 (defcustom esml-mlb-show-path-map-command
140 "mlton -expert true -show path-map"
141 "Shell command used to query the available path variables."
142 :type 'string
143 :set 'esml-mlb-set-custom-and-update
144 :group 'esml-mlb)
145
146 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
147 ;; Faces
148
149 (defface font-lock-interface-def-face
150 '((t (:bold t)))
151 "Font Lock mode face used to highlight interface definitions."
152 :group 'font-lock-highlighting-faces)
153
154 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
155 ;; Annotations
156
157 (defvar esml-mlb-annotations nil
158 "An association list of known annotations. This variable is updated by
159 `esml-mlb-update'.")
160
161 (defun esml-mlb-parse-annotations ()
162 (setq esml-mlb-annotations
163 (cl-remove-duplicates
164 (sort (append
165 esml-mlb-additional-annotations
166 (when (not (string= "" esml-mlb-show-annotations-command))
167 (mapcar (function
168 (lambda (s)
169 (esml-split-string s "[ \t]*[{}|][ \t]*")))
170 (esml-split-string
171 (with-temp-buffer
172 (save-window-excursion
173 (shell-command
174 esml-mlb-show-annotations-command
175 (current-buffer))
176 (buffer-string)))
177 "[ \t]*\n+[ \t]*"))))
178 (function
179 (lambda (a b)
180 (string-lessp (car a) (car b)))))
181 :test (function
182 (lambda (a b)
183 (string= (car a) (car b)))))))
184
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
186 ;; Path variables
187
188 (defvar esml-mlb-path-variables nil
189 "An association list of known path variables. This variable is updated
190 by `esml-mlb-update'.")
191
192 (defun esml-mlb-parse-path-variables-from-string (path-map-string)
193 (mapcar (function
194 (lambda (s) (apply (function cons) (esml-split-string s "[ \t]+"))))
195 (esml-split-string path-map-string "[ \t]*\n+[ \t]*")))
196
197 (defun esml-mlb-parse-path-variables ()
198 (setq esml-mlb-path-variables
199 (cl-remove-duplicates
200 (sort (append
201 esml-mlb-additional-path-variables
202 (esml-mlb-parse-path-variables-from-string
203 (with-temp-buffer
204 (save-window-excursion
205 (shell-command
206 esml-mlb-show-path-map-command
207 (current-buffer))
208 (buffer-string))))
209 (cl-loop for file in esml-mlb-mlb-path-map-files
210 append (when (file-readable-p file)
211 (esml-mlb-parse-path-variables-from-string
212 (with-temp-buffer
213 (insert-file-contents file)
214 (buffer-string))))))
215 (function
216 (lambda (a b)
217 (string-lessp (car a) (car b)))))
218 :test (function
219 (lambda (a b)
220 (string= (car a) (car b)))))))
221
222 (defun esml-mlb-expand-path (path)
223 "Expands path variable references in the given path."
224 (let ((parts nil))
225 (with-temp-buffer
226 (insert path)
227 (goto-char 0)
228 (while (not (eobp))
229 (if (looking-at "\\$(\\([^)]+\\))")
230 (let* ((name (match-string 1))
231 (name-value (assoc name esml-mlb-path-variables)))
232 (unless name-value
233 (error "Unknown path variable: %s" name))
234 (delete-char (length (match-string 0)))
235 (insert (cdr name-value)))
236 (forward-char 1)
237 (skip-chars-forward "^$")
238 (push (buffer-substring 1 (point))
239 parts)
240 (delete-char (- 1 (point))))
241 (goto-char 0)))
242 (apply (function concat) (reverse parts))))
243
244 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
245 ;; Syntax and highlighting
246
247 (defconst esml-mlb-string-continue-regexp "\\(?:\\\\[ \t\n]+\\\\\\)")
248 (defconst esml-mlb-string-char-regexp
249 (concat "\\(?:" esml-mlb-string-continue-regexp
250 "*\\(?:[^\n\"\\]\\|\\\\[^ \t\n]\\)\\)"))
251 (defconst esml-mlb-inside-string-regexp
252 (concat "\"" esml-mlb-string-char-regexp "*"
253 esml-mlb-string-continue-regexp "*"))
254 (defconst esml-mlb-string-regexp (concat esml-mlb-inside-string-regexp "\""))
255 (defconst esml-mlb-inside-comment-regexp "(\\*\\(?:[^*]\\|\\*[^)]\\)*")
256 (defconst esml-mlb-comment-regexp
257 (concat esml-mlb-inside-comment-regexp "\\*)"))
258 (defconst esml-mlb-path-var-chars "A-Za-z0-9_")
259 (defconst esml-mlb-unquoted-path-chars "-A-Za-z0-9_/.")
260 (defconst esml-mlb-unquoted-path-or-ref-chars
261 (concat esml-mlb-unquoted-path-chars "()$"))
262 (defconst esml-mlb-compiler-ann-prefix
263 (concat "\\(?:" esml-mlb-string-char-regexp "*:[ \t]*\\)"))
264
265 (defun esml-mlb-<token>-to-regexp (<token>)
266 (let* ((<token>-to-regexp
267 '(("<longstrid>" . "[A-Za-z0-9_]*")))
268 (<token>-regexp (assoc <token> <token>-to-regexp)))
269 (if <token>-regexp
270 (cdr <token>-regexp)
271 <token>)))
272
273 (defconst esml-mlb-keywords
274 '("and" "ann" "bas" "basis" "end" "functor" "in" "let" "local" "open"
275 "signature" "structure")
276 "Keywords of ML Basis syntax.")
277
278 (defconst esml-mlb-keywords-usually-followed-by-space
279 '("and" "functor" "open" "signature" "structure")
280 "Keywords of ML Basis syntax that are under most circumstances followed
281 by a space.")
282
283 (defconst esml-mlb-mode-syntax-table
284 (let ((table (make-syntax-table)))
285 (mapc (function
286 (lambda (char-flags)
287 (modify-syntax-entry (car char-flags) (cdr char-flags)
288 table)))
289 '((?\( . "()1")
290 (?\* . ". 23")
291 (?\) . ")(4")
292 (?\" . "$") ;; not '"' to allow custom highlighting of ann
293 (?\\ . "/") ;; not '\' due to class of '"'
294 (?/ . "_")
295 (?- . "_")
296 (?_ . "w") ;; not "_" due to variables regexp
297 (?. . "_")
298 (?$ . "_")
299 (?\; . ".")
300 (?= . ".")))
301 table)
302 "Syntax table for ML Basis mode.")
303
304 (defvar esml-mlb-font-lock-table nil)
305
306 (defun esml-mlb-build-font-lock-table ()
307 "Builds the font-lock table for ML Basis mode."
308 (setq esml-mlb-font-lock-table
309 `(;; quoted path names
310 (,(concat esml-mlb-inside-string-regexp
311 "\\.\\(" esml-mlb-path-suffix-regexp "\\)\"")
312 . font-lock-constant-face)
313 ;; annotations
314 (,(apply
315 (function concat)
316 "\"[ \t]*" esml-mlb-compiler-ann-prefix "?\\("
317 (cl-reduce
318 (function
319 (lambda (regexps name-values)
320 (if (cdr regexps)
321 (push "\\|" regexps))
322 (cons (if (cdr name-values)
323 (concat
324 (car name-values) "[ \t]+\\("
325 (cl-reduce
326 (function
327 (lambda (r s)
328 (concat r "\\|\\("
329 (esml-mlb-<token>-to-regexp s)
330 "\\)")))
331 (cddr name-values)
332 :initial-value (concat "\\("
333 (esml-mlb-<token>-to-regexp
334 (cadr name-values))
335 "\\)"))
336 "\\)")
337 (car name-values))
338 regexps)))
339 esml-mlb-annotations
340 :initial-value '("\\)[ \t]*\"")))
341 . font-lock-string-face)
342 (,esml-mlb-string-regexp
343 . font-lock-warning-face)
344 ;; path variables
345 (,(concat "\\$(\\("
346 (regexp-opt (mapcar 'car esml-mlb-path-variables))
347 "\\))")
348 . font-lock-reference-face)
349 ("\\$([^)]*?)"
350 . font-lock-warning-face)
351 ;; unquoted path names
352 (,(concat "[-A-Za-z0-9_/.]*[.]\\("
353 esml-mlb-path-suffix-regexp
354 "\\)[ \t\n]")
355 . font-lock-constant-face)
356 ("[.][-A-Za-z0-9_]+[ \t\n]"
357 . font-lock-warning-face)
358 ("[A-Za-z0-9_]*[-/.][-A-Za-z0-9_/]*"
359 . font-lock-constant-face)
360 ;; keywords
361 (,(concat "\\<\\(" (regexp-opt esml-mlb-keywords) "\\)\\>")
362 . font-lock-keyword-face)
363 ;; basids
364 ("[A-Za-z][A-Za-z0-9_']*"
365 . font-lock-interface-def-face))))
366
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
368 ;; Indentation
369
370 (defconst esml-mlb-indent-sync-keywords-regexp
371 (concat "\\("
372 (regexp-opt '("ann" "bas" "basis" "end" "functor" "in" "let"
373 "local" "open" "signature" "structure"))
374 "\\)[ \t\n]")
375 "Regexp for synchronizing indentation.")
376
377 (defun esml-mlb-previous-indentation ()
378 "Finds the previous indentation level and evidence."
379 (let ((result nil))
380 (save-excursion
381 (beginning-of-line)
382 (while (not (or (consp result) (bobp)))
383 (forward-line -1)
384 (beginning-of-line)
385 (skip-chars-forward " \t;")
386 (cond ((looking-at esml-mlb-indent-sync-keywords-regexp)
387 (setq result (let ((start (point))
388 (indentation (current-column)))
389 (forward-word 1)
390 (cons indentation
391 (intern (buffer-substring
392 start
393 (point)))))))
394 ((looking-at "(\\*")
395 (setq result (cons (current-column) '*)))
396 (t
397 (setq result (if result
398 (min result (current-indentation))
399 (current-indentation)))))))
400 (cond ((consp result)
401 result)
402 ((numberp result)
403 (cons result 'min))
404 (t
405 '(0 . min)))))
406
407 (defun esml-mlb-indent-line ()
408 "Indent current line as ML Basis code."
409 (interactive)
410 (let* ((indent-evidence (esml-mlb-previous-indentation))
411 (indent (car indent-evidence))
412 (evidence (cdr indent-evidence)))
413 (save-excursion
414 (beginning-of-line)
415 (skip-chars-forward " \t")
416 (cond ((looking-at ";")
417 (cl-case evidence
418 ((in bas)
419 (indent-line-to
420 (max 0 (+ indent -2 esml-mlb-indentation-offset))))
421 (t
422 (indent-line-to (max 0 (- indent 2))))))
423 ((looking-at "end[ \t\n]")
424 (cl-case evidence
425 ((ann bas in let local)
426 (indent-line-to indent))
427 (t
428 (indent-line-to
429 (max 0 (- indent esml-mlb-indentation-offset))))))
430 ((looking-at "in[ \t\n]")
431 (cl-case evidence
432 ((ann let local)
433 (indent-line-to indent))
434 (t
435 (indent-line-to (- indent esml-mlb-indentation-offset)))))
436 ((looking-at "and[ \t\n]")
437 (cl-case evidence
438 ((basis functor signature structure)
439 (indent-line-to (+ indent -3 (length (symbol-name evidence)))))
440 (t
441 (indent-line-to indent))))
442 ((looking-at "\\*")
443 (cl-case evidence
444 ((*)
445 (indent-line-to (+ indent 1)))
446 (t
447 (indent-line-to indent))))
448 (t
449 (cl-case evidence
450 ((ann bas in let local)
451 (indent-line-to (+ indent esml-mlb-indentation-offset)))
452 (t
453 (indent-line-to indent))))))
454 (if (< (current-column) (current-indentation))
455 (forward-char (- (current-indentation) (current-column))))))
456
457 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
458 ;; Completion
459
460 (defun esml-mlb-filter-file-completions (completions &optional allow-dots)
461 "Removes the directories `./' and `../' as well as files whose suffix
462 does not appear in `esml-mlb-path-suffix-regexp' from the list of file
463 name completions."
464 (let ((ignored-files-regexp
465 (concat "^\\(" esml-mlb-completion-ignored-files-regexp "\\)$"))
466 (valid-suffices-regexp
467 (concat "^\\(" esml-mlb-path-suffix-regexp "\\)$")))
468 (cl-remove
469 nil
470 completions
471 :test (function
472 (lambda (_ x)
473 (or (and (not allow-dots)
474 (member x '("./" "../")))
475 (string-match ignored-files-regexp x)
476 (not (or (file-name-directory x)
477 (let ((ext (file-name-extension x)))
478 (when ext
479 (string-match
480 valid-suffices-regexp ext)))))))))))
481
482 (defun esml-mlb-complete ()
483 "Performs context sensitive completion at point."
484 (interactive)
485 (cond
486 ;; no completion inside comments
487 ((esml-point-preceded-by esml-mlb-inside-comment-regexp))
488
489 ;; annotation values
490 ((esml-point-preceded-by
491 (concat "\"[ \t\n]*" esml-mlb-compiler-ann-prefix "?\\("
492 (regexp-opt (mapcar 'car esml-mlb-annotations))
493 "\\)[ \t\n]+\\(" esml-mlb-string-char-regexp "*\\)"))
494 (let* ((annot (assoc (match-string 1) esml-mlb-annotations))
495 (all-values (cdr annot))
496 (values (cl-remove nil all-values
497 :test (function
498 (lambda (_ s)
499 (and (< 0 (length s))
500 (= ?< (aref s 0)))))))
501 (value-prefix (match-string 2))
502 (value-completion
503 (try-completion value-prefix (mapcar 'list values)))
504 (value (if (eq t value-completion) value-prefix value-completion)))
505 (message "Annotation: %s %s" (car annot) (if all-values all-values ""))
506 (when (stringp value-completion)
507 (esml-insert-or-skip-if-looking-at
508 (substring value (length value-prefix))))
509 (when (and value
510 (eq t (try-completion value (mapcar 'list values))))
511 (esml-insert-or-skip-if-looking-at "\""))))
512
513 ;; annotation names
514 ((and (esml-point-preceded-by
515 (concat "\\<ann[ \t\n]+\\([ \t\n]+\\|" esml-mlb-string-regexp
516 "\\|" esml-mlb-comment-regexp "\\)*\"[^\"]*"))
517 (esml-point-preceded-by
518 (concat "\"[ \t\n]*" esml-mlb-compiler-ann-prefix "?\\("
519 esml-mlb-string-char-regexp "*\\)")))
520 (let* ((name-prefix (match-string 1))
521 (name-completion (try-completion name-prefix esml-mlb-annotations))
522 (name (if (eq t name-completion) name-prefix name-completion)))
523 (if (not name-completion)
524 (message "Annotations: %s" (mapcar 'car esml-mlb-annotations))
525 (when (stringp name-completion)
526 (esml-insert-or-skip-if-looking-at
527 (substring name (length name-prefix))))
528 (if (and name
529 (eq t (try-completion name esml-mlb-annotations)))
530 (let ((values (cdr (assoc name esml-mlb-annotations))))
531 (esml-insert-or-skip-if-looking-at (if values " " "\""))
532 (message "Annotation: %s %s" name (if values values "")))
533 (message "Annotations: %s"
534 (all-completions name-prefix esml-mlb-annotations))))))
535
536 ;; path variables
537 ((esml-point-preceded-by (concat "\\$(\\([" esml-mlb-path-var-chars "]*\\)"))
538 (let* ((name-prefix (match-string 1))
539 (name-completion
540 (try-completion name-prefix esml-mlb-path-variables))
541 (name (if (eq t name-completion) name-prefix name-completion)))
542 (if (not name-completion)
543 (message "Path variables: %s" (mapcar 'car esml-mlb-path-variables))
544 (when (stringp name-completion)
545 (esml-insert-or-skip-if-looking-at
546 (substring name (length name-prefix))))
547 (if (and name
548 (eq t (try-completion name esml-mlb-path-variables)))
549 (let* ((value (cdr (assoc name esml-mlb-path-variables)))
550 (expanded (esml-mlb-expand-path value)))
551 (esml-insert-or-skip-if-looking-at ")")
552 (if (string= value expanded)
553 (message "Path variable: %s [%s]" name value)
554 (message "Path variable: %s [%s ==> %s]" name value expanded)))
555 (message "Path variables: %s"
556 (all-completions name-prefix esml-mlb-path-variables))))))
557
558 ;; filenames and keywords
559 ((or (esml-point-preceded-by
560 (concat "\\(\"\\)\\(" esml-mlb-string-char-regexp "+\\)"))
561 (esml-point-preceded-by
562 (concat "\\([ \t\n]\\|^\\)\\(["
563 esml-mlb-unquoted-path-or-ref-chars
564 "]+\\)")))
565 ;; TBD: escape sequences in quoted pathnames
566 (let* ((quoted (string= "\"" (match-string 1)))
567 (path-prefix (match-string 2))
568 (path-expanded (esml-mlb-expand-path path-prefix))
569 (dir (if (file-name-directory path-expanded)
570 (file-name-directory path-expanded)
571 ""))
572 (nondir-prefix (file-name-nondirectory path-expanded))
573 (nondir-completions
574 (mapcar 'list
575 (let ((files (esml-mlb-filter-file-completions
576 (file-name-all-completions nondir-prefix dir)
577 t)))
578 (if (string= "" dir)
579 (if quoted
580 files
581 (append (all-completions
582 nondir-prefix
583 (mapcar 'list esml-mlb-keywords))
584 files))
585 (esml-mlb-filter-file-completions
586 files
587 (esml-string-matches-p "\\(\.\./\\)+" dir))))))
588 (nondir-completion (try-completion nondir-prefix nondir-completions))
589 (nondir (if (eq t nondir-completion)
590 nondir-prefix
591 nondir-completion)))
592 (if (not nondir-completion)
593 (if (string= path-prefix path-expanded)
594 (message "No completions for %s" path-prefix)
595 (message "No completions for %s ==> %s" path-prefix path-expanded))
596 (when (stringp nondir-completion)
597 (esml-insert-or-skip-if-looking-at
598 (substring nondir (length nondir-prefix))))
599 (if (eq t (try-completion nondir nondir-completions))
600 (cond
601 ((file-name-directory nondir)
602 (message "Completions: %s"
603 (sort (let ((dir (concat dir nondir)))
604 (esml-mlb-filter-file-completions
605 (file-name-all-completions "" dir)
606 (esml-string-matches-p "\\(\.\./\\)+" dir)))
607 'string-lessp)))
608 ((member nondir esml-mlb-keywords)
609 (esml-mlb-indent-line)
610 (message "Keyword: %s" nondir)
611 (when (member nondir esml-mlb-keywords-usually-followed-by-space)
612 (esml-insert-or-skip-if-looking-at " ")))
613 (t
614 (message "Expanded path: %s%s" dir nondir)))
615 (message "Completions: %s"
616 (sort (mapcar 'car nondir-completions)
617 'string-lessp))))))))
618
619 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
620 ;; Commands
621
622 (defun esml-mlb-indent-line-or-complete ()
623 "Indents the current line. If indentation does not change, attempts to
624 perform context sensitive completion. This command is not idempotent."
625 (interactive)
626 (let ((old-indentation (current-indentation)))
627 (esml-mlb-indent-line)
628 (when (= old-indentation (current-indentation))
629 (esml-mlb-complete))))
630
631 (defun esml-mlb-find-file-at-point ()
632 "Grabs the path surrounding point and attempts to find the file."
633 (interactive)
634 (let ((file (esml-mlb-expand-path
635 (save-excursion
636 (if (and (not (bobp))
637 (= ?\" (char-before)))
638 (let ((end (point)))
639 (backward-sexp)
640 (buffer-substring (+ (point) 1) (- end 1)))
641 (skip-chars-backward esml-mlb-unquoted-path-or-ref-chars)
642 (let ((start (point)))
643 (skip-chars-forward esml-mlb-unquoted-path-or-ref-chars)
644 (buffer-substring start (point))))))))
645 (if (file-readable-p file)
646 (find-file file)
647 (message (if (file-exists-p file)
648 "Not readable: %s"
649 "Does not exists: %s")
650 file))))
651
652 (defconst esml-mlb-show-basis-process-name "*mlb-show-basis*")
653
654 (defun esml-mlb-show-basis ()
655 "Shows the basis defined by the MLB file in the current buffer."
656 (interactive)
657 ;; TBD: find-error / error output mode
658 (unless (eq major-mode 'esml-mlb-mode)
659 (error "show-basis is only meaningful on MLB files"))
660 (when (get-process esml-mlb-show-basis-process-name)
661 (error "show-basis already running"))
662 (save-some-buffers)
663 (let ((tmp-file (concat
664 (file-name-directory (buffer-file-name))
665 "." (file-name-nondirectory (buffer-file-name))
666 ".basis"))
667 (buffer (get-buffer-create esml-mlb-show-basis-process-name)))
668 (when (file-exists-p tmp-file)
669 (error "Temporary basis file already exists: %s" tmp-file))
670 (with-current-buffer buffer
671 (delete-region (point-min) (point-max)))
672 (let ((process (start-process-shell-command
673 esml-mlb-show-basis-process-name
674 buffer
675 (replace-regexp-in-string
676 "%f"
677 (buffer-file-name)
678 (replace-regexp-in-string
679 "%t"
680 tmp-file
681 esml-mlb-show-basis-command
682 t t)
683 t t))))
684 (set-process-sentinel
685 process
686 (function
687 (lambda (process event)
688 (if (and (esml-string-matches-p "finished\n" event)
689 (file-readable-p tmp-file))
690 (with-current-buffer (find-file-other-window tmp-file)
691 (read-only-mode 'toggle)
692 (delete-file tmp-file))
693 (switch-to-buffer buffer))
694 (message "%S" event)))))
695 (message "%s" "show-basis running...")))
696
697 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
698 ;; Key Map
699
700 (defvar esml-mlb-mode-map (make-sparse-keymap)
701 "Keymap for ML Basis mode. This variable is updated by
702 `esml-mlb-update'.")
703
704 (defun esml-mlb-build-mode-map ()
705 "Builds the key map for ML Basis mode."
706 (let ((result (make-sparse-keymap)))
707 (mapc (function
708 (lambda (key-command)
709 (define-key result
710 (read (car key-command))
711 (cdr key-command))))
712 esml-mlb-key-bindings)
713 (setq esml-mlb-mode-map result)))
714
715 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
716 ;; Define mode
717
718 (defvar esml-mlb-mode-hook nil
719 "Hook run when entering ML Basis mode.")
720
721 (define-derived-mode esml-mlb-mode fundamental-mode "MLB"
722 "Major mode for editing ML Basis files. Provides syntax highlighting,
723 indentation, and context sensitive completion.
724
725 See the customization group `esml-mlb'."
726 :group 'esml-mlb
727 (set (make-local-variable 'font-lock-defaults)
728 '(esml-mlb-font-lock-table))
729 (set (make-local-variable 'indent-line-function)
730 'esml-mlb-indent-line))
731
732 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
733 ;; Finalization
734
735 (setq esml-mlb-load-time nil)
736
737 (defun esml-mlb-update ()
738 "Updates data based on customization variables."
739 (interactive)
740 ;; Warning: order dependencies
741 (esml-mlb-parse-path-variables)
742 (esml-mlb-parse-annotations)
743 (esml-mlb-build-font-lock-table)
744 (esml-mlb-build-mode-map))
745
746 ;; We are finally ready to update everything the first time.
747 (esml-mlb-update)
748
749 (add-to-list 'auto-mode-alist '("\\.mlb\\'" . esml-mlb-mode))
750 (add-to-list 'auto-mode-alist '("\\.basis\\'" . sml-mode))
751
752 (provide 'esml-mlb-mode)