use cooper theme -- end of git, I am trying livemesh
[srid.dotfiles.git] / emacs / external / ml / caml.el
blobfd1abda7c0bb6ba332ff832104091ee1bb504d96
1 ;(***********************************************************************)
2 ;(* *)
3 ;(* Objective Caml *)
4 ;(* *)
5 ;(* Jacques Garrigue and Ian T Zimmerman *)
6 ;(* *)
7 ;(* Copyright 1997 Institut National de Recherche en Informatique et *)
8 ;(* en Automatique. All rights reserved. This file is distributed *)
9 ;(* under the terms of the GNU General Public License. *)
10 ;(* *)
11 ;(***********************************************************************)
13 ;(* $Id: caml.el,v 1.36.4.1 2004/08/09 16:09:33 doligez Exp $ *)
15 ;;; caml.el --- O'Caml code editing commands for Emacs
17 ;; Xavier Leroy, july 1993.
19 ;;indentation code is Copyright (C) 1996 by Ian T Zimmerman <itz@rahul.net>
20 ;;copying: covered by the current FSF General Public License.
22 ;; indentation code adapted for Objective Caml by Jacques Garrigue,
23 ;; july 1997. <garrigue@kurims.kyoto-u.ac.jp>
25 ;;user customizable variables
26 (defvar caml-quote-char "'"
27 "*Quote for character constants. \"'\" for Objective Caml, \"`\" for Caml-Light.")
29 (defvar caml-imenu-enable nil
30 "*Enable Imenu support.")
32 (defvar caml-mode-indentation 2
33 "*Used for \\[caml-unindent-command].")
35 (defvar caml-lookback-limit 5000
36 "*How far to look back for syntax things in caml mode.")
38 (defvar caml-max-indent-priority 8
39 "*Bounds priority of operators permitted to affect caml indentation.
41 Priorities are assigned to `interesting' caml operators as follows:
43 all keywords 0 to 7 8
44 type, val, ... + 0 7
45 :: ^ 6
46 @ 5
47 := <- 4
48 if 3
49 fun, let, match ... 2
50 module 1
51 opening keywords 0.")
53 (defvar caml-apply-extra-indent 2
54 "*How many spaces to add to indentation for an application in caml mode.")
55 (make-variable-buffer-local 'caml-apply-extra-indent)
57 (defvar caml-begin-indent 2
58 "*How many spaces to indent from a begin keyword in caml mode.")
59 (make-variable-buffer-local 'caml-begin-indent)
61 (defvar caml-class-indent 2
62 "*How many spaces to indent from a class keyword in caml mode.")
63 (make-variable-buffer-local 'caml-class-indent)
65 (defvar caml-exception-indent 2
66 "*How many spaces to indent from a exception keyword in caml mode.")
67 (make-variable-buffer-local 'caml-exception-indent)
69 (defvar caml-for-indent 2
70 "*How many spaces to indent from a for keyword in caml mode.")
71 (make-variable-buffer-local 'caml-for-indent)
73 (defvar caml-fun-indent 2
74 "*How many spaces to indent from a fun keyword in caml mode.")
75 (make-variable-buffer-local 'caml-fun-indent)
77 (defvar caml-function-indent 4
78 "*How many spaces to indent from a function keyword in caml mode.")
79 (make-variable-buffer-local 'caml-function-indent)
81 (defvar caml-if-indent 2
82 "*How many spaces to indent from a if keyword in caml mode.")
83 (make-variable-buffer-local 'caml-if-indent)
85 (defvar caml-if-else-indent 0
86 "*How many spaces to indent from an if .. else line in caml mode.")
87 (make-variable-buffer-local 'caml-if-else-indent)
89 (defvar caml-inherit-indent 2
90 "*How many spaces to indent from a inherit keyword in caml mode.")
91 (make-variable-buffer-local 'caml-inherit-indent)
93 (defvar caml-initializer-indent 2
94 "*How many spaces to indent from a initializer keyword in caml mode.")
95 (make-variable-buffer-local 'caml-initializer-indent)
97 (defvar caml-include-indent 2
98 "*How many spaces to indent from a include keyword in caml mode.")
99 (make-variable-buffer-local 'caml-include-indent)
101 (defvar caml-let-indent 2
102 "*How many spaces to indent from a let keyword in caml mode.")
103 (make-variable-buffer-local 'caml-let-indent)
105 (defvar caml-let-in-indent 0
106 "*How many spaces to indent from a let .. in keyword in caml mode.")
107 (make-variable-buffer-local 'caml-let-in-indent)
109 (defvar caml-match-indent 2
110 "*How many spaces to indent from a match keyword in caml mode.")
111 (make-variable-buffer-local 'caml-match-indent)
113 (defvar caml-method-indent 2
114 "*How many spaces to indent from a method keyword in caml mode.")
115 (make-variable-buffer-local 'caml-method-indent)
117 (defvar caml-module-indent 2
118 "*How many spaces to indent from a module keyword in caml mode.")
119 (make-variable-buffer-local 'caml-module-indent)
121 (defvar caml-object-indent 2
122 "*How many spaces to indent from a object keyword in caml mode.")
123 (make-variable-buffer-local 'caml-object-indent)
125 (defvar caml-of-indent 2
126 "*How many spaces to indent from a of keyword in caml mode.")
127 (make-variable-buffer-local 'caml-of-indent)
129 (defvar caml-parser-indent 4
130 "*How many spaces to indent from a parser keyword in caml mode.")
131 (make-variable-buffer-local 'caml-parser-indent)
133 (defvar caml-sig-indent 2
134 "*How many spaces to indent from a sig keyword in caml mode.")
135 (make-variable-buffer-local 'caml-sig-indent)
137 (defvar caml-struct-indent 2
138 "*How many spaces to indent from a struct keyword in caml mode.")
139 (make-variable-buffer-local 'caml-struct-indent)
141 (defvar caml-try-indent 2
142 "*How many spaces to indent from a try keyword in caml mode.")
143 (make-variable-buffer-local 'caml-try-indent)
145 (defvar caml-type-indent 4
146 "*How many spaces to indent from a type keyword in caml mode.")
147 (make-variable-buffer-local 'caml-type-indent)
149 (defvar caml-val-indent 2
150 "*How many spaces to indent from a val keyword in caml mode.")
151 (make-variable-buffer-local 'caml-val-indent)
153 (defvar caml-while-indent 2
154 "*How many spaces to indent from a while keyword in caml mode.")
155 (make-variable-buffer-local 'caml-while-indent)
157 (defvar caml-::-indent 2
158 "*How many spaces to indent from a :: operator in caml mode.")
159 (make-variable-buffer-local 'caml-::-indent)
161 (defvar caml-@-indent 2
162 "*How many spaces to indent from a @ operator in caml mode.")
163 (make-variable-buffer-local 'caml-@-indent)
165 (defvar caml-:=-indent 2
166 "*How many spaces to indent from a := operator in caml mode.")
167 (make-variable-buffer-local 'caml-:=-indent)
169 (defvar caml-<--indent 2
170 "*How many spaces to indent from a <- operator in caml mode.")
171 (make-variable-buffer-local 'caml-<--indent)
173 (defvar caml-->-indent 2
174 "*How many spaces to indent from a -> operator in caml mode.")
175 (make-variable-buffer-local 'caml-->-indent)
177 (defvar caml-lb-indent 2
178 "*How many spaces to indent from a \[ operator in caml mode.")
179 (make-variable-buffer-local 'caml-lb-indent)
181 (defvar caml-lc-indent 2
182 "*How many spaces to indent from a \{ operator in caml mode.")
183 (make-variable-buffer-local 'caml-lc-indent)
185 (defvar caml-lp-indent 1
186 "*How many spaces to indent from a \( operator in caml mode.")
187 (make-variable-buffer-local 'caml-lp-indent)
189 (defvar caml-and-extra-indent nil
190 "*Extra indent for caml lines starting with the and keyword.
191 Usually negative. nil is align on master.")
192 (make-variable-buffer-local 'caml-and-extra-indent)
194 (defvar caml-do-extra-indent nil
195 "*Extra indent for caml lines starting with the do keyword.
196 Usually negative. nil is align on master.")
197 (make-variable-buffer-local 'caml-do-extra-indent)
199 (defvar caml-done-extra-indent nil
200 "*Extra indent for caml lines starting with the done keyword.
201 Usually negative. nil is align on master.")
202 (make-variable-buffer-local 'caml-done-extra-indent)
204 (defvar caml-else-extra-indent nil
205 "*Extra indent for caml lines starting with the else keyword.
206 Usually negative. nil is align on master.")
207 (make-variable-buffer-local 'caml-else-extra-indent)
209 (defvar caml-end-extra-indent nil
210 "*Extra indent for caml lines starting with the end keyword.
211 Usually negative. nil is align on master.")
212 (make-variable-buffer-local 'caml-end-extra-indent)
214 (defvar caml-in-extra-indent nil
215 "*Extra indent for caml lines starting with the in keyword.
216 Usually negative. nil is align on master.")
217 (make-variable-buffer-local 'caml-in-extra-indent)
219 (defvar caml-then-extra-indent nil
220 "*Extra indent for caml lines starting with the then keyword.
221 Usually negative. nil is align on master.")
222 (make-variable-buffer-local 'caml-then-extra-indent)
224 (defvar caml-to-extra-indent -1
225 "*Extra indent for caml lines starting with the to keyword.
226 Usually negative. nil is align on master.")
227 (make-variable-buffer-local 'caml-to-extra-indent)
229 (defvar caml-with-extra-indent nil
230 "*Extra indent for caml lines starting with the with keyword.
231 Usually negative. nil is align on master.")
232 (make-variable-buffer-local 'caml-with-extra-indent)
234 (defvar caml-comment-indent 3
235 "*Indent inside comments.")
236 (make-variable-buffer-local 'caml-comment-indent)
238 (defvar caml-|-extra-indent -2
239 "*Extra indent for caml lines starting with the | operator.
240 Usually negative. nil is align on master.")
241 (make-variable-buffer-local 'caml-|-extra-indent)
243 (defvar caml-rb-extra-indent -2
244 "*Extra indent for caml lines statring with ].
245 Usually negative. nil is align on master.")
247 (defvar caml-rc-extra-indent -2
248 "*Extra indent for caml lines starting with }.
249 Usually negative. nil is align on master.")
251 (defvar caml-rp-extra-indent -1
252 "*Extra indent for caml lines starting with ).
253 Usually negative. nil is align on master.")
255 (defvar caml-electric-indent t
256 "*Non-nil means electrically indent lines starting with |, ] or }.
258 Many people find eletric keys irritating, so you can disable them if
259 you are one.")
261 (defvar caml-electric-close-vector t
262 "*Non-nil means electrically insert a | before a vector-closing ].
264 Many people find eletric keys irritating, so you can disable them if
265 you are one. You should probably have this on, though, if you also
266 have caml-electric-indent on, which see.")
268 ;;code
269 (if (or (not (fboundp 'indent-line-to))
270 (not (fboundp 'buffer-substring-no-properties)))
271 (require 'caml-compat))
273 (defvar caml-shell-active nil
274 "Non nil when a subshell is running.")
276 (defvar running-xemacs (string-match "XEmacs" emacs-version)
277 "Non-nil if we are running in the XEmacs environment.")
279 (defvar caml-mode-map nil
280 "Keymap used in Caml mode.")
281 (if caml-mode-map
283 (setq caml-mode-map (make-sparse-keymap))
284 (define-key caml-mode-map "|" 'caml-electric-pipe)
285 (define-key caml-mode-map "}" 'caml-electric-pipe)
286 (define-key caml-mode-map "]" 'caml-electric-rb)
287 (define-key caml-mode-map "\t" 'caml-indent-command)
288 (define-key caml-mode-map [backtab] 'caml-unindent-command)
290 ;itz 04-21-96 instead of defining a new function, use defadvice
291 ;that way we get out effect even when we do \C-x` in compilation buffer
292 ; (define-key caml-mode-map "\C-x`" 'caml-next-error)
294 (if running-xemacs
295 (define-key caml-mode-map 'backspace 'backward-delete-char-untabify)
296 (define-key caml-mode-map "\177" 'backward-delete-char-untabify))
298 ;; caml-types
299 (define-key caml-mode-map [?\C-c?\C-t] 'caml-types-show-type)
300 ;; must be a mouse-down event. Can be any button and any prefix
301 (define-key caml-mode-map [?\C-c down-mouse-1] 'caml-types-explore)
302 ;; caml-help
303 (define-key caml-mode-map [?\C-c?i] 'ocaml-add-path)
304 (define-key caml-mode-map [?\C-c?]] 'ocaml-close-module)
305 (define-key caml-mode-map [?\C-c?[] 'ocaml-open-module)
306 (define-key caml-mode-map [?\C-c?\C-h] 'caml-help)
307 (define-key caml-mode-map [?\C-c?\t] 'caml-complete)
308 ;; others
309 (define-key caml-mode-map "\C-cb" 'caml-insert-begin-form)
310 (define-key caml-mode-map "\C-cf" 'caml-insert-for-form)
311 (define-key caml-mode-map "\C-ci" 'caml-insert-if-form)
312 (define-key caml-mode-map "\C-cl" 'caml-insert-let-form)
313 (define-key caml-mode-map "\C-cm" 'caml-insert-match-form)
314 (define-key caml-mode-map "\C-ct" 'caml-insert-try-form)
315 (define-key caml-mode-map "\C-cw" 'caml-insert-while-form)
316 (define-key caml-mode-map "\C-c`" 'caml-goto-phrase-error)
317 (define-key caml-mode-map "\C-c\C-a" 'caml-find-alternate-file)
318 (define-key caml-mode-map "\C-c\C-c" 'compile)
319 (define-key caml-mode-map "\C-c\C-e" 'caml-eval-phrase)
320 (define-key caml-mode-map "\C-c\C-\[" 'caml-backward-to-less-indent)
321 (define-key caml-mode-map "\C-c\C-\]" 'caml-forward-to-less-indent)
322 (define-key caml-mode-map "\C-c\C-q" 'caml-indent-phrase)
323 (define-key caml-mode-map "\C-c\C-r" 'caml-eval-region)
324 (define-key caml-mode-map "\C-c\C-s" 'caml-show-subshell)
325 (define-key caml-mode-map "\M-\C-h" 'caml-mark-phrase)
326 (define-key caml-mode-map "\M-\C-q" 'caml-indent-phrase)
327 (define-key caml-mode-map "\M-\C-x" 'caml-eval-phrase)
329 (if running-xemacs nil ; if not running xemacs
330 (let ((map (make-sparse-keymap "Caml"))
331 (forms (make-sparse-keymap "Forms")))
332 (define-key caml-mode-map "\C-c\C-d" 'caml-show-imenu)
333 (define-key caml-mode-map [menu-bar] (make-sparse-keymap))
334 (define-key caml-mode-map [menu-bar caml] (cons "Caml" map))
335 ;; caml-help
337 (define-key map [open] '("Open add path" . ocaml-add-path ))
338 (define-key map [close]
339 '("Close module for help" . ocaml-close-module))
340 (define-key map [open] '("Open module for help" . ocaml-open-module))
341 (define-key map [help] '("Help for identifier" . caml-help))
342 (define-key map [complete] '("Complete identifier" . caml-complete))
343 (define-key map [separator-help] '("---"))
345 ;; caml-types
346 (define-key map [show-type]
347 '("Show type at point" . caml-types-show-type ))
348 (define-key map [separator-types] '("---"))
350 ;; others
351 (define-key map [run-caml] '("Start subshell..." . run-caml))
352 (define-key map [compile] '("Compile..." . compile))
353 (define-key map [switch-view]
354 '("Switch view" . caml-find-alternate-file))
355 (define-key map [separator-format] '("--"))
356 (define-key map [forms] (cons "Forms" forms))
357 (define-key map [show-imenu] '("Show index" . caml-show-imenu))
358 (put 'caml-show-imenu 'menu-enable '(not caml-imenu-shown))
359 (define-key map [show-subshell] '("Show subshell" . caml-show-subshell))
360 (put 'caml-show-subshell 'menu-enable 'caml-shell-active)
361 (define-key map [eval-phrase] '("Eval phrase" . caml-eval-phrase))
362 (put 'caml-eval-phrase 'menu-enable 'caml-shell-active)
363 (define-key map [indent-phrase] '("Indent phrase" . caml-indent-phrase))
364 (define-key forms [while]
365 '("while .. do .. done" . caml-insert-while-form))
366 (define-key forms [try] '("try .. with .." . caml-insert-try-form))
367 (define-key forms [match] '("match .. with .." . caml-insert-match-form))
368 (define-key forms [let] '("let .. in .." . caml-insert-let-form))
369 (define-key forms [if] '("if .. then .. else .." . caml-insert-if-form))
370 (define-key forms [begin] '("for .. do .. done" . caml-insert-for-form))
371 (define-key forms [begin] '("begin .. end" . caml-insert-begin-form)))))
373 (defvar caml-mode-xemacs-menu
374 (if running-xemacs
375 '("Caml"
376 [ "Indent phrase" caml-indent-phrase :keys "C-M-q" ]
377 [ "Eval phrase" caml-eval-phrase
378 :active caml-shell-active :keys "C-M-x" ]
379 [ "Show subshell" caml-show-subshell caml-shell-active ]
380 ("Forms"
381 [ "while .. do .. done" caml-insert-while-form t]
382 [ "try .. with .." caml-insert-try-form t ]
383 [ "match .. with .." caml-insert-match-form t ]
384 [ "let .. in .." caml-insert-let-form t ]
385 [ "if .. then .. else .." caml-insert-if-form t ]
386 [ "for .. do .. done" caml-insert-for-form t ]
387 [ "begin .. end" caml-insert-begin-form t ])
388 "---"
389 [ "Switch view" caml-find-alternate-file t ]
390 [ "Compile..." compile t ]
391 [ "Start subshell..." run-caml t ]
392 "---"
393 [ "Show type at point" caml-types-show-type t ]
394 "---"
395 [ "Complete identifier" caml-complete t ]
396 [ "Help for identifier" caml-help t ]
397 [ "Add path for documentation" ocaml-add-path t ]
398 [ "Open module for documentation" ocaml-open t ]
399 [ "Close module for documentation" ocaml-close t ]
401 "Menu to add to the menubar when running Xemacs")
403 (defvar caml-mode-syntax-table nil
404 "Syntax table in use in Caml mode buffers.")
405 (if caml-mode-syntax-table
407 (setq caml-mode-syntax-table (make-syntax-table))
408 ; backslash is an escape sequence
409 (modify-syntax-entry ?\\ "\\" caml-mode-syntax-table)
410 ; ( is first character of comment start
411 (modify-syntax-entry ?\( "()1" caml-mode-syntax-table)
412 ; * is second character of comment start,
413 ; and first character of comment end
414 (modify-syntax-entry ?* ". 23" caml-mode-syntax-table)
415 ; ) is last character of comment end
416 (modify-syntax-entry ?\) ")(4" caml-mode-syntax-table)
417 ; backquote was a string-like delimiter (for character literals)
418 ; (modify-syntax-entry ?` "\"" caml-mode-syntax-table)
419 ; quote and underscore are part of words
420 (modify-syntax-entry ?' "w" caml-mode-syntax-table)
421 (modify-syntax-entry ?_ "w" caml-mode-syntax-table)
422 ; ISO-latin accented letters and EUC kanjis are part of words
423 (let ((i 160))
424 (while (< i 256)
425 (modify-syntax-entry i "w" caml-mode-syntax-table)
426 (setq i (1+ i)))))
428 (defvar caml-mode-abbrev-table nil
429 "Abbrev table used for Caml mode buffers.")
430 (if caml-mode-abbrev-table nil
431 (setq caml-mode-abbrev-table (make-abbrev-table))
432 (define-abbrev caml-mode-abbrev-table "and" "and" 'caml-abbrev-hook)
433 (define-abbrev caml-mode-abbrev-table "do" "do" 'caml-abbrev-hook)
434 (define-abbrev caml-mode-abbrev-table "done" "done" 'caml-abbrev-hook)
435 (define-abbrev caml-mode-abbrev-table "else" "else" 'caml-abbrev-hook)
436 (define-abbrev caml-mode-abbrev-table "end" "end" 'caml-abbrev-hook)
437 (define-abbrev caml-mode-abbrev-table "in" "in" 'caml-abbrev-hook)
438 (define-abbrev caml-mode-abbrev-table "then" "then" 'caml-abbrev-hook)
439 (define-abbrev caml-mode-abbrev-table "with" "with" 'caml-abbrev-hook))
441 ;; Other internal variables
443 (defvar caml-last-noncomment-pos nil
444 "Caches last buffer position determined not inside a caml comment.")
445 (make-variable-buffer-local 'caml-last-noncomment-pos)
447 ;;last-noncomment-pos can be a simple position, because we nil it
448 ;;anyway whenever buffer changes upstream. last-comment-start and -end
449 ;;have to be markers, because we preserve them when the changes' end
450 ;;doesn't overlap with the comment's start.
452 (defvar caml-last-comment-start nil
453 "A marker caching last determined caml comment start.")
454 (make-variable-buffer-local 'caml-last-comment-start)
456 (defvar caml-last-comment-end nil
457 "A marker caching last determined caml comment end.")
458 (make-variable-buffer-local 'caml-last-comment-end)
460 (make-variable-buffer-local 'before-change-function)
462 (defvar caml-imenu-shown nil
463 "True if we have computed definition list.")
464 (make-variable-buffer-local 'caml-imenu-shown)
466 (defconst caml-imenu-search-regexp
467 (concat "\\<in\\>\\|"
468 "^[ \t]*\\(let\\|class\\|type\\|m\\(odule\\|ethod\\)"
469 "\\|functor\\|and\\|val\\)[ \t]+"
470 "\\(\\('[a-zA-Z0-9]+\\|([^)]+)"
471 "\\|mutable\\|private\\|rec\\|type\\)[ \t]+\\)?"
472 "\\([a-zA-Z][a-zA-Z0-9_']*\\)"))
474 ;;; The major mode
475 (eval-when-compile
476 (if (and (boundp 'running-xemacs) running-xemacs) nil
477 (require 'imenu)))
480 (defvar caml-mode-hook nil
481 "Hook for caml-mode")
483 (defun caml-mode ()
484 "Major mode for editing Caml code.
486 \\{caml-mode-map}"
488 (interactive)
489 (kill-all-local-variables)
490 (setq major-mode 'caml-mode)
491 (setq mode-name "caml")
492 (use-local-map caml-mode-map)
493 (set-syntax-table caml-mode-syntax-table)
494 (setq local-abbrev-table caml-mode-abbrev-table)
495 (make-local-variable 'paragraph-start)
496 (setq paragraph-start (concat "^$\\|" page-delimiter))
497 (make-local-variable 'paragraph-separate)
498 (setq paragraph-separate paragraph-start)
499 (make-local-variable 'paragraph-ignore-fill-prefix)
500 (setq paragraph-ignore-fill-prefix t)
501 (make-local-variable 'require-final-newline)
502 (setq require-final-newline t)
503 (make-local-variable 'comment-start)
504 (setq comment-start "(*")
505 (make-local-variable 'comment-end)
506 (setq comment-end "*)")
507 (make-local-variable 'comment-column)
508 (setq comment-column 40)
509 (make-local-variable 'comment-start-skip)
510 (setq comment-start-skip "(\\*+ *")
511 (make-local-variable 'parse-sexp-ignore-comments)
512 (setq parse-sexp-ignore-comments nil)
513 (make-local-variable 'indent-line-function)
514 (setq indent-line-function 'caml-indent-command)
515 ;itz Fri Sep 25 13:23:49 PDT 1998
516 (make-local-variable 'add-log-current-defun-function)
517 (setq add-log-current-defun-function 'caml-current-defun)
518 ;itz 03-25-96
519 (setq before-change-function 'caml-before-change-function)
520 (setq caml-last-noncomment-pos nil)
521 (setq caml-last-comment-start (make-marker))
522 (setq caml-last-comment-end (make-marker))
523 ;garrigue 27-11-96
524 (setq case-fold-search nil)
525 ;garrigue july 97
526 (if running-xemacs ; from Xemacs lisp mode
527 (if (and (featurep 'menubar)
528 current-menubar)
529 (progn
530 ;; make a local copy of the menubar, so our modes don't
531 ;; change the global menubar
532 (set-buffer-menubar current-menubar)
533 (add-submenu nil caml-mode-xemacs-menu)))
534 ;imenu support (not for Xemacs)
535 (make-local-variable 'imenu-create-index-function)
536 (setq imenu-create-index-function 'caml-create-index-function)
537 (make-local-variable 'imenu-generic-expression)
538 (setq imenu-generic-expression caml-imenu-search-regexp)
539 (if (and caml-imenu-enable (< (buffer-size) 10000))
540 (caml-show-imenu)))
541 (run-hooks 'caml-mode-hook))
543 (defun caml-set-compile-command ()
544 "Hook to set compile-command locally, unless there is a Makefile in the
545 current directory."
546 (interactive)
547 (unless (or (null buffer-file-name)
548 (file-exists-p "makefile")
549 (file-exists-p "Makefile"))
550 (let* ((filename (file-name-nondirectory buffer-file-name))
551 (basename (file-name-sans-extension filename))
552 (command nil))
553 (cond
554 ((string-match ".*\\.mli\$" filename)
555 (setq command "ocamlc -c"))
556 ((string-match ".*\\.ml\$" filename)
557 (setq command "ocamlc -c") ; (concat "ocamlc -o " basename)
559 ((string-match ".*\\.mll\$" filename)
560 (setq command "ocamllex"))
561 ((string-match ".*\\.mll\$" filename)
562 (setq command "ocamlyacc"))
564 (if command
565 (progn
566 (make-local-variable 'compile-command)
567 (setq compile-command (concat command " " filename))))
570 (add-hook 'caml-mode-hook 'caml-set-compile-command)
572 ;;; Auxiliary function. Garrigue 96-11-01.
574 (defun caml-find-alternate-file ()
575 (interactive)
576 (let ((name (buffer-file-name)))
577 (if (string-match "^\\(.*\\)\\.\\(ml\\|mli\\)$" name)
578 (find-file
579 (concat
580 (caml-match-string 1 name)
581 (if (string= "ml" (caml-match-string 2 name)) ".mli" ".ml"))))))
583 ;;; subshell support
585 (defun caml-eval-region (start end)
586 "Send the current region to the inferior Caml process."
587 (interactive"r")
588 (require 'inf-caml)
589 (inferior-caml-eval-region start end))
591 ;; old version ---to be deleted later
593 ; (defun caml-eval-phrase ()
594 ; "Send the current Caml phrase to the inferior Caml process."
595 ; (interactive)
596 ; (save-excursion
597 ; (let ((bounds (caml-mark-phrase)))
598 ; (inferior-caml-eval-region (car bounds) (cdr bounds)))))
600 (defun caml-eval-phrase (arg &optional min max)
601 "Send the phrase containing the point to the CAML process.
602 With prefix-arg send as many phrases as its numeric value,
603 If an error occurs during evalutaion, stop at this phrase and
604 repport the error.
606 Return nil if noerror and position of error if any.
608 If arg's numeric value is zero or negative, evaluate the current phrase
609 or as many as prefix arg, ignoring evaluation errors.
610 This allows to jump other erroneous phrases.
612 Optional arguments min max defines a region within which the phrase
613 should lies."
614 (interactive "p")
615 (require 'inf-caml)
616 (inferior-caml-eval-phrase arg min max))
618 (defun caml-eval-buffer (arg)
619 "Evaluate the buffer from the beginning to the phrase under the point.
620 With prefix arg, evaluate past the whole buffer, no stopping at
621 the current point."
622 (interactive "p")
623 (let ((here (point)) err)
624 (goto-char (point-min))
625 (setq err
626 (caml-eval-phrase 500 (point-min) (if arg (point-max) here)))
627 (if err (set-mark err))
628 (goto-char here)))
630 (defun caml-show-subshell ()
631 (interactive)
632 (require 'inf-caml)
633 (inferior-caml-show-subshell))
636 ;;; Imenu support
637 (defun caml-show-imenu ()
638 (interactive)
639 (require 'imenu)
640 (switch-to-buffer (current-buffer))
641 (imenu-add-to-menubar "Defs")
642 (setq caml-imenu-shown t))
644 (defun caml-prev-index-position-function ()
645 (let (found data)
646 (while (and (setq found
647 (re-search-backward caml-imenu-search-regexp nil 'move))
648 (progn (setq data (match-data)) t)
649 (or (caml-in-literal-p)
650 (caml-in-comment-p)
651 (if (looking-at "in") (caml-find-in-match)))))
652 (set-match-data data)
653 found))
654 (defun caml-create-index-function ()
655 (let (value-alist
656 type-alist
657 class-alist
658 method-alist
659 module-alist
660 and-alist
661 all-alist
662 menu-alist
663 (prev-pos (point-max))
664 index)
665 (goto-char prev-pos)
666 (imenu-progress-message prev-pos 0 t)
667 ;; collect definitions
668 (while (caml-prev-index-position-function)
669 (setq index (cons (caml-match-string 5) (point)))
670 (imenu-progress-message prev-pos nil t)
671 (setq all-alist (cons index all-alist))
672 (cond
673 ((looking-at "[ \t]*and")
674 (setq and-alist (cons index and-alist)))
675 ((looking-at "[ \t]*let")
676 (setq value-alist (cons index (append and-alist value-alist)))
677 (setq and-alist nil))
678 ((looking-at "[ \t]*type")
679 (setq type-alist (cons index (append and-alist type-alist)))
680 (setq and-alist nil))
681 ((looking-at "[ \t]*class")
682 (setq class-alist (cons index (append and-alist class-alist)))
683 (setq and-alist nil))
684 ((looking-at "[ \t]*val")
685 (setq value-alist (cons index value-alist)))
686 ((looking-at "[ \t]*\\(module\\|functor\\)")
687 (setq module-alist (cons index module-alist)))
688 ((looking-at "[ \t]*method")
689 (setq method-alist (cons index method-alist)))))
690 ;; build menu
691 (mapcar
692 '(lambda (pair)
693 (if (symbol-value (cdr pair))
694 (setq menu-alist
695 (cons
696 (cons (car pair)
697 (sort (symbol-value (cdr pair)) 'imenu--sort-by-name))
698 menu-alist))))
699 '(("Values" . value-alist)
700 ("Types" . type-alist)
701 ("Modules" . module-alist)
702 ("Methods" . method-alist)
703 ("Classes" . class-alist)))
704 (if all-alist (setq menu-alist (cons (cons "Index" all-alist) menu-alist)))
705 (imenu-progress-message prev-pos 100 t)
706 menu-alist))
708 ;;; Indentation stuff
710 (defun caml-in-indentation ()
711 "Tests whether all characters between beginning of line and point
712 are blanks."
713 (save-excursion
714 (skip-chars-backward " \t")
715 (bolp)))
717 ;;; The command
718 ;;; Sorry, I didn't like the previous behaviour... Garrigue 96/11/01
720 (defun caml-indent-command (&optional p)
721 "Indent the current line in Caml mode.
723 Compute new indentation based on caml syntax. If prefixed, indent
724 the line all the way to where point is."
726 (interactive "*p")
727 (cond
728 ((and p (> p 1)) (indent-line-to (current-column)))
729 ((caml-in-indentation) (indent-line-to (caml-compute-final-indent)))
730 (t (save-excursion
731 (indent-line-to
732 (caml-compute-final-indent))))))
734 (defun caml-unindent-command ()
736 "Decrease indentation by one level in Caml mode.
738 Works only if the point is at the beginning of an indented line
739 \(i.e. all characters between beginning of line and point are
740 blanks\). Does nothing otherwise. The unindent size is given by the
741 variable caml-mode-indentation."
743 (interactive "*")
744 (let* ((begline
745 (save-excursion
746 (beginning-of-line)
747 (point)))
748 (current-offset
749 (- (point) begline)))
750 (if (and (>= current-offset caml-mode-indentation)
751 (caml-in-indentation))
752 (backward-delete-char-untabify caml-mode-indentation))))
755 ;;; Error processing
758 ;; Error positions are given in bytes, not in characters
759 ;; This function switches to monobyte mode
761 (if (not (fboundp 'char-bytes))
762 (defalias 'forward-byte 'forward-char)
763 (defun caml-char-bytes (ch)
764 (let ((l (char-bytes ch)))
765 (if (> l 1) (- l 1) l)))
766 (defun forward-byte (count)
767 (if (> count 0)
768 (while (> count 0)
769 (let ((char (char-after)))
770 (if (null char)
771 (setq count 0)
772 (setq count (- count (caml-char-bytes (char-after))))
773 (forward-char))))
774 (while (< count 0)
775 (let ((char (char-after)))
776 (if (null char)
777 (setq count 0)
778 (setq count (+ count (caml-char-bytes (char-before))))
779 (backward-char))))
782 (require 'compile)
784 ;; In Emacs 19, the regexps in compilation-error-regexp-alist do not
785 ;; match the error messages when the language is not English.
786 ;; Hence we add a regexp.
788 (defconst caml-error-regexp
789 "^[A-\377]+ \"\\([^\"\n]+\\)\", [A-\377]+ \\([0-9]+\\)[-,:]"
790 "Regular expression matching the error messages produced by camlc.")
792 (if (boundp 'compilation-error-regexp-alist)
793 (or (assoc caml-error-regexp
794 compilation-error-regexp-alist)
795 (setq compilation-error-regexp-alist
796 (cons (list caml-error-regexp 1 2)
797 compilation-error-regexp-alist))))
799 ;; A regexp to extract the range info
801 (defconst caml-error-chars-regexp
802 ".*, .*, [A-\377]+ \\([0-9]+\\)-\\([0-9]+\\):"
803 "Regular expression extracting the character numbers
804 from an error message produced by camlc.")
806 ;; Wrapper around next-error.
808 (defvar caml-error-overlay nil)
810 ;;itz 04-21-96 somebody didn't get the documetation for next-error
811 ;;right. When the optional argument is a number n, it should move
812 ;;forward n errors, not reparse.
814 ;itz 04-21-96 instead of defining a new function, use defadvice
815 ;that way we get our effect even when we do \C-x` in compilation buffer
817 (defadvice next-error (after caml-next-error activate)
818 "Reads the extra positional information provided by the Caml compiler.
820 Puts the point and the mark exactly around the erroneous program
821 fragment. The erroneous fragment is also temporarily highlighted if
822 possible."
824 (if (eq major-mode 'caml-mode)
825 (let (bol beg end)
826 (save-excursion
827 (set-buffer
828 (if (boundp 'compilation-last-buffer)
829 compilation-last-buffer ;Emacs 19
830 "*compilation*")) ;Emacs 18
831 (save-excursion
832 (goto-char (window-point (get-buffer-window (current-buffer))))
833 (if (looking-at caml-error-chars-regexp)
834 (setq beg
835 (string-to-int
836 (buffer-substring (match-beginning 1) (match-end 1)))
838 (string-to-int
839 (buffer-substring (match-beginning 2) (match-end 2)))))))
840 (cond (beg
841 (setq end (- end beg))
842 (beginning-of-line)
843 (forward-byte beg)
844 (setq beg (point))
845 (forward-byte end)
846 (setq end (point))
847 (goto-char beg)
848 (push-mark end t)
849 (cond ((fboundp 'make-overlay)
850 (if caml-error-overlay ()
851 (setq caml-error-overlay (make-overlay 1 1))
852 (overlay-put caml-error-overlay 'face 'region))
853 (unwind-protect
854 (progn
855 (move-overlay caml-error-overlay
856 beg end (current-buffer))
857 (sit-for 60))
858 (delete-overlay caml-error-overlay)))))))))
860 ;; Usual match-string doesn't work properly with font-lock-mode
861 ;; on some emacs.
863 (defun caml-match-string (num &optional string)
865 "Return string of text matched by last search, without properties.
867 NUM specifies which parenthesized expression in the last regexp.
868 Value is nil if NUMth pair didn't match, or there were less than NUM
869 pairs. Zero means the entire text matched by the whole regexp or
870 whole string."
872 (let* ((data (match-data))
873 (begin (nth (* 2 num) data))
874 (end (nth (1+ (* 2 num)) data)))
875 (if string (substring string begin end)
876 (buffer-substring-no-properties begin end))))
878 ;; itz Thu Sep 24 19:02:42 PDT 1998 this is to have some level of
879 ;; comfort when sending phrases to the toplevel and getting errors.
880 (defun caml-goto-phrase-error ()
881 "Find the error location in current Caml phrase."
882 (interactive)
883 (require 'inf-caml)
884 (let ((bounds (save-excursion (caml-mark-phrase))))
885 (inferior-caml-goto-error (car bounds) (cdr bounds))))
887 ;;; Phrases
889 ;itz the heuristics used to see if we're `between two phrases'
890 ;didn't seem right to me.
892 (defconst caml-phrase-start-keywords
893 (concat "\\<\\(class\\|ex\\(ternal\\|ception\\)\\|functor"
894 "\\|let\\|module\\|open\\|type\\|val\\)\\>")
895 "Keywords starting phrases in files")
897 ;; a phrase starts when a toplevel keyword is at the beginning of a line
898 (defun caml-at-phrase-start-p ()
899 (and (bolp)
900 (or (looking-at "#")
901 (looking-at caml-phrase-start-keywords))))
903 (defun caml-skip-comments-forward ()
904 (skip-chars-forward " \n\t")
905 (while (or (looking-at comment-start-skip) (caml-in-comment-p))
906 (if (= (following-char) ?\)) (forward-char)
907 (search-forward comment-end))
908 (skip-chars-forward " \n\t")))
910 (defun caml-skip-comments-backward ()
911 (skip-chars-backward " \n\t")
912 (while (and (eq (preceding-char) ?\)) (eq (char-after (- (point) 2)) ?*))
913 (backward-char)
914 (while (caml-in-comment-p) (search-backward comment-start))
915 (skip-chars-backward " \n\t")))
917 (defconst caml-phrase-sep-keywords (concat ";;\\|" caml-phrase-start-keywords))
919 (defun caml-find-phrase (&optional min-pos max-pos)
920 "Find the CAML phrase containing the point.
921 Return the position of the beginning of the phrase, and move point
922 to the end.
924 (interactive)
925 (if (not min-pos) (setq min-pos (point-min)))
926 (if (not max-pos) (setq max-pos (point-max)))
927 (let (beg end use-semi kwop)
928 ;(caml-skip-comments-backward)
929 (cond
930 ; shall we have special processing for semicolons?
931 ;((and (eq (char-before (- (point) 1)) ?\;) (eq (char-before) ?\;))
932 ; (forward-char)
933 ; (caml-skip-comments-forward)
934 ; (setq beg (point))
935 ; (while (and (search-forward ";;" max-pos 'move)
936 ; (or (caml-in-comment-p) (caml-in-literal-p)))))
938 (caml-skip-comments-forward)
939 (if (caml-at-phrase-start-p) (forward-char))
940 (while (and (cond
941 ((re-search-forward caml-phrase-sep-keywords max-pos 'move)
942 (goto-char (match-beginning 0)) t))
943 (or (not (or (bolp) (looking-at ";;")))
944 (caml-in-comment-p)
945 (caml-in-literal-p)))
946 (forward-char))
947 (setq end (+ (point) (if (looking-at ";;") 2 0)))
948 (while (and
949 (setq kwop (caml-find-kwop caml-phrase-sep-keywords min-pos))
950 (not (string= kwop ";;"))
951 (not (bolp))))
952 (if (string= kwop ";;") (forward-char 2))
953 (if (not kwop) (goto-char min-pos))
954 (caml-skip-comments-forward)
955 (setq beg (point))
956 (if (>= beg end) (error "no phrase before point"))
957 (goto-char end)))
958 (caml-skip-comments-forward)
959 beg))
961 (defun caml-mark-phrase (&optional min-pos max-pos)
962 "Put mark at end of this Caml phrase, point at beginning.
964 (interactive)
965 (let* ((beg (caml-find-phrase min-pos max-pos)) (end (point)))
966 (push-mark)
967 (goto-char beg)
968 (cons beg end)))
970 ;;itz Fri Sep 25 12:58:13 PDT 1998 support for adding change-log entries
971 (defun caml-current-defun ()
972 (save-excursion
973 (caml-mark-phrase)
974 (if (not (looking-at caml-phrase-start-keywords)) nil
975 (re-search-forward caml-phrase-start-keywords)
976 (let ((done nil))
977 (while (not done)
978 (cond
979 ((looking-at "\\s ")
980 (skip-syntax-forward " "))
981 ((char-equal (following-char) ?\( )
982 (forward-sexp 1))
983 ((char-equal (following-char) ?')
984 (skip-syntax-forward "w_"))
985 (t (setq done t)))))
986 (re-search-forward "\\(\\sw\\|\\s_\\)+")
987 (match-string 0))))
989 (defun caml-overlap (b1 e1 b2 e2)
990 (<= (max b1 b2) (min e1 e2)))
992 ;this clears the last comment cache if necessary
993 (defun caml-before-change-function (begin end)
994 (if (and caml-last-noncomment-pos
995 (> caml-last-noncomment-pos begin))
996 (setq caml-last-noncomment-pos nil))
997 (if (and (marker-position caml-last-comment-start)
998 (marker-position caml-last-comment-end)
999 (caml-overlap begin end
1000 caml-last-comment-start
1001 caml-last-comment-end))
1002 (prog2
1003 (set-marker caml-last-comment-start nil)
1004 (set-marker caml-last-comment-end nil)))
1005 (let ((orig-function (default-value 'before-change-function)))
1006 (if orig-function (funcall orig-function begin end))))
1008 (defun caml-in-literal-p ()
1009 "Returns non-nil if point is inside a caml literal."
1010 (let* ((start-literal (concat "[\"" caml-quote-char "]"))
1011 (char-literal
1012 (concat "\\([^\\]\\|\\\\\\.\\|\\\\[0-9][0-9][0-9]\\)"
1013 caml-quote-char))
1014 (pos (point))
1015 (eol (progn (end-of-line 1) (point)))
1016 state in-str)
1017 (beginning-of-line 1)
1018 (while (and (not state)
1019 (re-search-forward start-literal eol t)
1020 (<= (point) pos))
1021 (cond
1022 ((string= (caml-match-string 0) "\"")
1023 (setq in-str t)
1024 (while (and in-str (not state)
1025 (re-search-forward "\"\\|\\\\\"" eol t))
1026 (if (> (point) pos) (setq state t))
1027 (if (string= (caml-match-string 0) "\"") (setq in-str nil)))
1028 (if in-str (setq state t)))
1029 ((looking-at char-literal)
1030 (if (and (>= pos (match-beginning 0)) (< pos (match-end 0)))
1031 (setq state t)
1032 (goto-char (match-end 0))))))
1033 (goto-char pos)
1034 state))
1036 (defun caml-forward-comment ()
1037 "Skip one (eventually nested) comment."
1038 (let ((count 1) match)
1039 (while (> count 0)
1040 (if (not (re-search-forward "(\\*\\|\\*)" nil 'move))
1041 (setq count -1)
1042 (setq match (caml-match-string 0))
1043 (cond
1044 ((caml-in-literal-p)
1045 nil)
1046 ((string= match comment-start)
1047 (setq count (1+ count)))
1049 (setq count (1- count))))))
1050 (= count 0)))
1052 (defun caml-backward-comment ()
1053 "Skip one (eventually nested) comment."
1054 (let ((count 1) match)
1055 (while (> count 0)
1056 (if (not (re-search-backward "(\\*\\|\\*)" nil 'move))
1057 (setq count -1)
1058 (setq match (caml-match-string 0))
1059 (cond
1060 ((caml-in-literal-p)
1061 nil)
1062 ((string= match comment-start)
1063 (setq count (1- count)))
1065 (setq count (1+ count))))))
1066 (= count 0)))
1068 (defun caml-in-comment-p ()
1069 "Returns non-nil if point is inside a caml comment.
1070 Returns nil for the parenthesis openning a comment."
1071 ;;we look for comments differently than literals. there are two
1072 ;;reasons for this. first, caml has nested comments and it is not so
1073 ;;clear that parse-partial-sexp supports them; second, if proper
1074 ;;style is used, literals are never split across lines, so we don't
1075 ;;have to worry about bogus phrase breaks inside literals, while we
1076 ;;have to account for that possibility in comments.
1077 (if caml-last-comment-start
1078 (save-excursion
1079 (let* ((cached-pos caml-last-noncomment-pos)
1080 (cached-begin (marker-position caml-last-comment-start))
1081 (cached-end (marker-position caml-last-comment-end)))
1082 (cond
1083 ((and cached-begin cached-end
1084 (< cached-begin (point)) (< (point) cached-end)) t)
1085 ((and cached-pos (= cached-pos (point))) nil)
1086 ((and cached-pos (> cached-pos (point))
1087 (< (abs (- cached-pos (point))) caml-lookback-limit))
1088 (let (end found (here (point)))
1089 ; go back to somewhere sure
1090 (goto-char cached-pos)
1091 (while (> (point) here)
1092 ; look for the end of a comment
1093 (while (and (if (search-backward comment-end (1- here) 'move)
1094 (setq end (match-end 0))
1095 (setq end nil))
1096 (caml-in-literal-p)))
1097 (if end (setq found (caml-backward-comment))))
1098 (if (and found (= (point) here)) (setq end nil))
1099 (if (not end)
1100 (setq caml-last-noncomment-pos here)
1101 (set-marker caml-last-comment-start (point))
1102 (set-marker caml-last-comment-end end))
1103 end))
1105 (let (begin found (here (point)))
1106 ;; go back to somewhere sure (or far enough)
1107 (goto-char
1108 (if cached-pos cached-pos (- (point) caml-lookback-limit)))
1109 (while (< (point) here)
1110 ;; look for the beginning of a comment
1111 (while (and (if (search-forward comment-start (1+ here) 'move)
1112 (setq begin (match-beginning 0))
1113 (setq begin nil))
1114 (caml-in-literal-p)))
1115 (if begin (setq found (caml-forward-comment))))
1116 (if (and found (= (point) here)) (setq begin nil))
1117 (if (not begin)
1118 (setq caml-last-noncomment-pos here)
1119 (set-marker caml-last-comment-start begin)
1120 (set-marker caml-last-comment-end (point)))
1121 begin)))))))
1123 ;; Various constants and regexps
1125 (defconst caml-before-expr-prefix
1126 (concat "\\<\\(asr\\|begin\\|class\\|do\\(wnto\\)?\\|else"
1127 "\\|i\\(f\\|n\\(herit\\|itializer\\)?\\)"
1128 "\\|f\\(or\\|un\\(ct\\(ion\\|or\\)\\)?\\)"
1129 "\\|l\\(and\\|or\\|s[lr]\\|xor\\)\\|m\\(atch\\|od\\)"
1130 "\\|o[fr]\\|parser\\|s\\(ig\\|truct\\)\\|t\\(hen\\|o\\|ry\\)"
1131 "\\|w\\(h\\(en\\|ile\\)\\|ith\\)\\)\\>\\|:begin\\>"
1132 "\\|[=<>@^|&+-*/$%][!$%*+-./:<=>?@^|~]*\\|:[:=]\\|[[({,;]")
1134 "Keywords that may appear immediately before an expression.
1135 Used to distinguish it from toplevel let construct.")
1137 (defconst caml-matching-kw-regexp
1138 (concat
1139 "\\<\\(and\\|do\\(ne\\)?\\|e\\(lse\\|nd\\)\\|in\\|t\\(hen\\|o\\)"
1140 "\\|with\\)\\>\\|[^[|]|")
1141 "Regexp used in caml mode for skipping back over nested blocks.")
1143 (defconst caml-matching-kw-alist
1144 '(("|" . caml-find-pipe-match)
1145 (";" . caml-find-semi-match)
1146 ("," . caml-find-comma-match)
1147 ("end" . caml-find-end-match)
1148 ("done" . caml-find-done-match)
1149 ("in" . caml-find-in-match)
1150 ("with" . caml-find-with-match)
1151 ("else" . caml-find-else-match)
1152 ("then" . caml-find-then-match)
1153 ("to" . caml-find-done-match)
1154 ("do" . caml-find-done-match)
1155 ("and" . caml-find-and-match))
1157 "Association list used in caml mode for skipping back over nested blocks.")
1159 (defconst caml-kwop-regexps (make-vector 9 nil)
1160 "Array of regexps representing caml keywords of different priorities.")
1162 (defun caml-in-expr-p ()
1163 (let ((pos (point)) (in-expr t))
1164 (caml-find-kwop
1165 (concat caml-before-expr-prefix "\\|"
1166 caml-matching-kw-regexp "\\|"
1167 (aref caml-kwop-regexps caml-max-indent-priority)))
1168 (cond
1169 ; special case for ;;
1170 ((and (> (point) 1) (= (preceding-char) ?\;) (= (following-char) ?\;))
1171 (setq in-expr nil))
1172 ((looking-at caml-before-expr-prefix)
1173 (if (not (looking-at "(\\*")) (goto-char (match-end 0)))
1174 (skip-chars-forward " \t\n")
1175 (while (looking-at "(\\*")
1176 (forward-char)
1177 (caml-forward-comment)
1178 (skip-chars-forward " \t\n"))
1179 (if (<= pos (point)) (setq in-expr nil))))
1180 (goto-char pos)
1181 in-expr))
1183 (defun caml-at-sexp-close-p ()
1184 (or (char-equal ?\) (following-char))
1185 (char-equal ?\] (following-char))
1186 (char-equal ?} (following-char))))
1188 (defun caml-find-kwop (kwop-regexp &optional min-pos)
1189 "Look back for a caml keyword or operator matching KWOP-REGEXP.
1190 Second optional argument MIN-POS bounds the search.
1192 Ignore occurences inside literals. If found, return a list of two
1193 values: the actual text of the keyword or operator, and a boolean
1194 indicating whether the keyword was one we looked for explicitly
1195 {non-nil}, or on the other hand one of the block-terminating
1196 keywords."
1198 (let ((start-literal (concat "[\"" caml-quote-char "]"))
1199 found kwop)
1200 (while (and (> (point) 1) (not found)
1201 (re-search-backward kwop-regexp min-pos 'move))
1202 (setq kwop (caml-match-string 0))
1203 (cond
1204 ((looking-at "(\\*")
1205 (if (> (point) 1) (backward-char)))
1206 ((caml-in-comment-p)
1207 (search-backward "(" min-pos 'move))
1208 ((looking-at start-literal))
1209 ((caml-in-literal-p)
1210 (re-search-backward start-literal min-pos 'move)) ;ugly hack
1211 ((setq found t))))
1212 (if found
1213 (if (not (string-match "\\`[^|[]|[^]|]?\\'" kwop)) ;arrrrgh!!
1214 kwop
1215 (forward-char 1) "|") nil)))
1217 ; Association list of indentation values based on governing keywords.
1219 ;Each element is of the form (KEYWORD OP-TYPE PRIO INDENT). OP-TYPE is
1220 ;non-nil for operator-type nodes, which affect indentation in a
1221 ;different way from keywords: subsequent lines are indented to the
1222 ;actual occurrence of an operator, but relative to the indentation of
1223 ;the line where the governing keyword occurs.
1225 (defconst caml-no-indent 0)
1227 (defconst caml-kwop-alist
1228 '(("begin" nil 6 caml-begin-indent)
1229 (":begin" nil 6 caml-begin-indent) ; hack
1230 ("class" nil 0 caml-class-indent)
1231 ("constraint" nil 0 caml-val-indent)
1232 ("sig" nil 1 caml-sig-indent)
1233 ("struct" nil 1 caml-struct-indent)
1234 ("exception" nil 0 caml-exception-indent)
1235 ("for" nil 6 caml-for-indent)
1236 ("fun" nil 3 caml-fun-indent)
1237 ("function" nil 3 caml-function-indent)
1238 ("if" nil 6 caml-if-indent)
1239 ("if-else" nil 6 caml-if-else-indent)
1240 ("include" nil 0 caml-include-indent)
1241 ("inherit" nil 0 caml-inherit-indent)
1242 ("initializer" nil 0 caml-initializer-indent)
1243 ("let" nil 6 caml-let-indent)
1244 ("let-in" nil 6 caml-let-in-indent)
1245 ("match" nil 6 caml-match-indent)
1246 ("method" nil 0 caml-method-indent)
1247 ("module" nil 0 caml-module-indent)
1248 ("object" nil 6 caml-object-indent)
1249 ("of" nil 7 caml-of-indent)
1250 ("open" nil 0 caml-no-indent)
1251 ("parser" nil 3 caml-parser-indent)
1252 ("try" nil 6 caml-try-indent)
1253 ("type" nil 0 caml-type-indent)
1254 ("val" nil 0 caml-val-indent)
1255 ("when" nil 2 caml-if-indent)
1256 ("while" nil 6 caml-while-indent)
1257 ("::" t 5 caml-::-indent)
1258 ("@" t 4 caml-@-indent)
1259 ("^" t 4 caml-@-indent)
1260 (":=" nil 3 caml-:=-indent)
1261 ("<-" nil 3 caml-<--indent)
1262 ("->" nil 2 caml-->-indent)
1263 ("\[" t 8 caml-lb-indent)
1264 ("{" t 8 caml-lc-indent)
1265 ("\(" t 8 caml-lp-indent)
1266 ("|" nil 2 caml-no-indent)
1267 (";;" nil 0 caml-no-indent))
1268 ; if-else and let-in are not keywords but idioms
1269 ; "|" is not in the regexps
1270 ; all these 3 values correspond to hard-coded names
1272 "Association list of indentation values based on governing keywords.
1274 Each element is of the form (KEYWORD OP-TYPE PRIO INDENT). OP-TYPE is
1275 non-nil for operator-type nodes, which affect indentation in a
1276 different way from keywords: subsequent lines are indented to the
1277 actual occurrence of an operator, but relative to the indentation of
1278 the line where the governing keyword occurs.")
1280 ;;Originally, we had caml-kwop-regexp create these at runtime, from an
1281 ;;additional field in caml-kwop-alist. That proved way too slow,
1282 ;;although I still can't understand why. itz
1284 (aset caml-kwop-regexps 0
1285 (concat
1286 "\\<\\(begin\\|object\\|for\\|s\\(ig\\|truct\\)\\|while\\)\\>"
1287 "\\|:begin\\>\\|[[({]\\|;;"))
1288 (aset caml-kwop-regexps 1
1289 (concat (aref caml-kwop-regexps 0) "\\|\\<\\(class\\|module\\)\\>"))
1290 (aset caml-kwop-regexps 2
1291 (concat
1292 (aref caml-kwop-regexps 1)
1293 "\\|\\<\\(fun\\(ction\\)?\\|initializer\\|let\\|m\\(atch\\|ethod\\)"
1294 "\\|parser\\|try\\|val\\)\\>\\|->"))
1295 (aset caml-kwop-regexps 3
1296 (concat (aref caml-kwop-regexps 2) "\\|\\<if\\|when\\>"))
1297 (aset caml-kwop-regexps 4
1298 (concat (aref caml-kwop-regexps 3) "\\|:=\\|<-"))
1299 (aset caml-kwop-regexps 5
1300 (concat (aref caml-kwop-regexps 4) "\\|@"))
1301 (aset caml-kwop-regexps 6
1302 (concat (aref caml-kwop-regexps 5) "\\|::\\|\\^"))
1303 (aset caml-kwop-regexps 7
1304 (concat
1305 (aref caml-kwop-regexps 0)
1306 "\\|\\<\\(constraint\\|exception\\|in\\(herit\\|clude\\)"
1307 "\\|o\\(f\\|pen\\)\\|type\\|val\\)\\>"))
1308 (aset caml-kwop-regexps 8
1309 (concat (aref caml-kwop-regexps 6)
1310 "\\|\\<\\(constraint\\|exception\\|in\\(herit\\|clude\\)"
1311 "\\|o\\(f\\|pen\\)\\|type\\)\\>"))
1313 (defun caml-find-done-match ()
1314 (let ((unbalanced 1) (kwop t))
1315 (while (and (not (= 0 unbalanced)) kwop)
1316 (setq kwop (caml-find-kwop "\\<\\(done\\|for\\|while\\)\\>"))
1317 (cond
1318 ((not kwop))
1319 ((string= kwop "done") (setq unbalanced (1+ unbalanced)))
1320 (t (setq unbalanced (1- unbalanced)))))
1321 kwop))
1323 (defun caml-find-end-match ()
1324 (let ((unbalanced 1) (kwop t))
1325 (while (and (not (= 0 unbalanced)) kwop)
1326 (setq kwop
1327 (caml-find-kwop
1328 "\\<\\(end\\|begin\\|object\\|s\\(ig\\|truct\\)\\)\\>\\|:begin\\>\\|;;"))
1329 (cond
1330 ((not kwop))
1331 ((string= kwop ";;") (setq kwop nil) (forward-line 1))
1332 ((string= kwop "end") (setq unbalanced (1+ unbalanced)))
1333 ( t (setq unbalanced (1- unbalanced)))))
1334 (if (string= kwop ":begin") "begin"
1335 kwop)))
1337 (defun caml-find-in-match ()
1338 (let ((unbalanced 1) (kwop t))
1339 (while (and (not (= 0 unbalanced)) kwop)
1340 (setq kwop (caml-find-kwop "\\<\\(in\\|let\\|end\\)\\>"))
1341 (cond
1342 ((not kwop))
1343 ((string= kwop "end") (caml-find-end-match))
1344 ((string= kwop "in") (setq unbalanced (1+ unbalanced)))
1345 (t (setq unbalanced (1- unbalanced)))))
1346 kwop))
1348 (defun caml-find-with-match ()
1349 (let ((unbalanced 1) (kwop t))
1350 (while (and (not (= 0 unbalanced)) kwop)
1351 (setq kwop
1352 (caml-find-kwop
1353 "\\<\\(with\\|try\\|m\\(atch\\|odule\\)\\|functor\\)\\>\\|[{}()]"))
1354 (cond
1355 ((not kwop))
1356 ((caml-at-sexp-close-p)
1357 (caml-find-paren-match (following-char)))
1358 ((string= kwop "with")
1359 (setq unbalanced (1+ unbalanced)))
1360 ((or (string= kwop "module")
1361 (string= kwop "functor")
1362 (string= kwop "{")
1363 (string= kwop "("))
1364 (setq unbalanced 0))
1365 (t (setq unbalanced (1- unbalanced)))))
1366 kwop))
1368 (defun caml-find-paren-match (close)
1369 (let ((unbalanced 1)
1370 (regexp (cond ((= close ?\)) "[()]")
1371 ((= close ?\]) "[][]")
1372 ((= close ?\}) "[{}]"))))
1373 (while (and (> unbalanced 0)
1374 (caml-find-kwop regexp))
1375 (if (= close (following-char))
1376 (setq unbalanced (1+ unbalanced))
1377 (setq unbalanced (1- unbalanced))))))
1379 (defun caml-find-then-match (&optional from-else)
1380 (let ((bol (if from-else
1381 (save-excursion
1382 (progn (beginning-of-line) (point)))))
1383 kwop done matching-fun)
1384 (while (not done)
1385 (setq kwop
1386 (caml-find-kwop
1387 "\\<\\(e\\(nd\\|lse\\)\\|done\\|then\\|if\\|with\\)\\>\\|[])};]"))
1388 (cond
1389 ((not kwop) (setq done t))
1390 ((caml-at-sexp-close-p)
1391 (caml-find-paren-match (following-char)))
1392 ((string= kwop "if") (setq done t))
1393 ((string= kwop "then")
1394 (if (not from-else) (setq kwop (caml-find-then-match))))
1395 ((setq matching-fun (cdr-safe (assoc kwop caml-matching-kw-alist)))
1396 (setq kwop (funcall matching-fun)))))
1397 (if (and bol (>= (point) bol))
1398 "if-else"
1399 kwop)))
1401 (defun caml-find-pipe-match ()
1402 (let ((done nil) (kwop)
1403 (re (concat
1404 "\\<\\(try\\|match\\|with\\|function\\|parser\\|type"
1405 "\\|e\\(nd\\|lse\\)\\|done\\|then\\|in\\)\\>"
1406 "\\|[^[|]|\\|[])}]")))
1407 (while (not done)
1408 (setq kwop (caml-find-kwop re))
1409 (cond
1410 ((not kwop) (setq done t))
1411 ((looking-at "[^[|]\\(|\\)")
1412 (goto-char (match-beginning 1))
1413 (setq kwop "|")
1414 (setq done t))
1415 ((caml-at-sexp-close-p)
1416 (caml-find-paren-match (following-char)))
1417 ((string= kwop "with")
1418 (setq kwop (caml-find-with-match))
1419 (setq done t))
1420 ((string= kwop "parser")
1421 (if (re-search-backward "\\<with\\>" (- (point) 5) t)
1422 (setq kwop (caml-find-with-match)))
1423 (setq done t))
1424 ((string= kwop "done") (caml-find-done-match))
1425 ((string= kwop "end") (caml-find-end-match))
1426 ((string= kwop "then") (caml-find-then-match))
1427 ((string= kwop "else") (caml-find-else-match))
1428 ((string= kwop "in") (caml-find-in-match))
1429 (t (setq done t))))
1430 kwop))
1432 (defun caml-find-and-match ()
1433 (let ((done nil) (kwop))
1434 (while (not done)
1435 (setq kwop (caml-find-kwop
1436 "\\<\\(object\\|exception\\|let\\|type\\|end\\|in\\)\\>"))
1437 (cond
1438 ((not kwop) (setq done t))
1439 ((string= kwop "end") (caml-find-end-match))
1440 ((string= kwop "in") (caml-find-in-match))
1441 (t (setq done t))))
1442 kwop))
1444 (defun caml-find-else-match ()
1445 (caml-find-then-match t))
1447 (defun caml-find-semi-match ()
1448 (caml-find-kwop-skipping-blocks 2))
1450 (defun caml-find-comma-match ()
1451 (caml-find-kwop-skipping-blocks 3))
1453 (defun caml-find-kwop-skipping-blocks (prio)
1454 "Look back for a caml keyword matching caml-kwop-regexps [PRIO].
1456 Skip nested blocks."
1458 (let ((done nil) (kwop nil) (matching-fun)
1459 (kwop-list (aref caml-kwop-regexps prio)))
1460 (while (not done)
1461 (setq kwop (caml-find-kwop
1462 (concat caml-matching-kw-regexp
1463 (cond ((> prio 3) "\\|[])},;]\\|")
1464 ((> prio 2) "\\|[])};]\\|")
1465 (t "\\|[])}]\\|"))
1466 kwop-list)))
1467 (cond
1468 ((not kwop) (setq done t))
1469 ((caml-at-sexp-close-p)
1470 (caml-find-paren-match (following-char)))
1471 ((or (string= kwop ";;")
1472 (and (string= kwop ";") (= (preceding-char) ?\;)))
1473 (forward-line 1)
1474 (setq kwop ";;")
1475 (setq done t))
1476 ((and (>= prio 2) (string= kwop "|")) (setq done t))
1477 ((string= kwop "end") (caml-find-end-match))
1478 ((string= kwop "done") (caml-find-done-match))
1479 ((string= kwop "in")
1480 (cond ((and (caml-find-in-match) (>= prio 2))
1481 (setq kwop "let-in")
1482 (setq done t))))
1483 ((and (string= kwop "parser") (>= prio 2)
1484 (re-search-backward "\\<with\\>" (- (point) 5) t))
1485 (setq kwop (caml-find-with-match))
1486 (setq done t))
1487 ((setq matching-fun (cdr-safe (assoc kwop caml-matching-kw-alist)))
1488 (setq kwop (funcall matching-fun))
1489 (if (looking-at kwop-list) (setq done t)))
1490 (t (let* ((kwop-info (assoc kwop caml-kwop-alist))
1491 (is-op (and (nth 1 kwop-info)
1492 ; check that we are not at beginning of line
1493 (let ((pos (point)) bti)
1494 (back-to-indentation)
1495 (setq bti (point))
1496 (goto-char pos)
1497 (< bti pos)))))
1498 (if (and is-op (looking-at
1499 (concat (regexp-quote kwop)
1500 "|?[ \t]*\\(\n\\|(\\*\\)")))
1501 (setq kwop-list
1502 (aref caml-kwop-regexps (nth 2 kwop-info)))
1503 (setq done t))))))
1504 kwop))
1506 (defun caml-compute-basic-indent (prio)
1507 "Compute indent of current caml line, ignoring leading keywords.
1509 Find the `governing node' for current line. Compute desired
1510 indentation based on the node and the indentation alists.
1511 Assumes point is exactly at line indentation.
1512 Does not preserve point."
1514 (let* (in-expr
1515 (kwop (cond
1516 ((looking-at ";;")
1517 (beginning-of-line 1))
1518 ((looking-at "|\\([^]|]\\|\\'\\)")
1519 (caml-find-pipe-match))
1520 ((and (looking-at caml-phrase-start-keywords)
1521 (caml-in-expr-p))
1522 (caml-find-end-match))
1523 ((and (looking-at caml-matching-kw-regexp)
1524 (assoc (caml-match-string 0) caml-matching-kw-alist))
1525 (funcall (cdr-safe (assoc (caml-match-string 0)
1526 caml-matching-kw-alist))))
1527 ((looking-at
1528 (aref caml-kwop-regexps caml-max-indent-priority))
1529 (let* ((kwop (caml-match-string 0))
1530 (kwop-info (assoc kwop caml-kwop-alist))
1531 (prio (if kwop-info (nth 2 kwop-info)
1532 caml-max-indent-priority)))
1533 (if (and (looking-at (aref caml-kwop-regexps 0))
1534 (not (looking-at "object"))
1535 (caml-in-expr-p))
1536 (setq in-expr t))
1537 (caml-find-kwop-skipping-blocks prio)))
1539 (if (and (= prio caml-max-indent-priority) (caml-in-expr-p))
1540 (setq in-expr t))
1541 (caml-find-kwop-skipping-blocks prio))))
1542 (kwop-info (assoc kwop caml-kwop-alist))
1543 (indent-diff
1544 (cond
1545 ((not kwop-info) (beginning-of-line 1) 0)
1546 ((looking-at "[[({][|<]?[ \t]*")
1547 (length (caml-match-string 0)))
1548 ((nth 1 kwop-info) (symbol-value (nth 3 kwop-info)))
1550 (let ((pos (point)))
1551 (back-to-indentation)
1552 ; (if (looking-at "\\<let\\>") (goto-char pos))
1553 (- (symbol-value (nth 3 kwop-info))
1554 (if (looking-at "|") caml-|-extra-indent 0))))))
1555 (extra (if in-expr caml-apply-extra-indent 0)))
1556 (+ indent-diff extra (current-column))))
1558 (defconst caml-leading-kwops-regexp
1559 (concat
1560 "\\<\\(and\\|do\\(ne\\)?\\|e\\(lse\\|nd\\)\\|in"
1561 "\\|t\\(hen\\|o\\)\\|with\\)\\>\\|[]|})]")
1563 "Regexp matching caml keywords which need special indentation.")
1565 (defconst caml-leading-kwops-alist
1566 '(("and" caml-and-extra-indent 2)
1567 ("do" caml-do-extra-indent 0)
1568 ("done" caml-done-extra-indent 0)
1569 ("else" caml-else-extra-indent 3)
1570 ("end" caml-end-extra-indent 0)
1571 ("in" caml-in-extra-indent 2)
1572 ("then" caml-then-extra-indent 3)
1573 ("to" caml-to-extra-indent 0)
1574 ("with" caml-with-extra-indent 2)
1575 ("|" caml-|-extra-indent 2)
1576 ("]" caml-rb-extra-indent 0)
1577 ("}" caml-rc-extra-indent 0)
1578 (")" caml-rp-extra-indent 0))
1580 "Association list of special caml keyword indent values.
1582 Each member is of the form (KEYWORD EXTRA-INDENT PRIO) where
1583 EXTRA-INDENT is the variable holding extra indentation amount for
1584 KEYWORD (usually negative) and PRIO is upper bound on priority of
1585 matching nodes to determine KEYWORD's final indentation.")
1587 (defun caml-compute-final-indent ()
1588 (save-excursion
1589 (back-to-indentation)
1590 (cond
1591 ((and (bolp) (looking-at comment-start-skip)) (current-column))
1592 ((caml-in-comment-p)
1593 (let ((closing (looking-at "\\*)"))
1594 (comment-mark (looking-at "\\*")))
1595 (caml-backward-comment)
1596 (looking-at comment-start-skip)
1597 (+ (current-column)
1598 (cond
1599 (closing 1)
1600 (comment-mark 1)
1601 (t caml-comment-indent)))))
1602 (t (let* ((leading (looking-at caml-leading-kwops-regexp))
1603 (assoc-val (if leading (assoc (caml-match-string 0)
1604 caml-leading-kwops-alist)))
1605 (extra (if leading (symbol-value (nth 1 assoc-val)) 0))
1606 (prio (if leading (nth 2 assoc-val)
1607 caml-max-indent-priority))
1608 (basic (caml-compute-basic-indent prio)))
1609 (max 0 (if extra (+ extra basic) (current-column))))))))
1613 (defun caml-split-string ()
1614 "Called whenever a line is broken inside a caml string literal."
1615 (insert-before-markers "\"^\"")
1616 (backward-char 1))
1618 (defadvice indent-new-comment-line (around
1619 caml-indent-new-comment-line
1620 activate)
1622 "Handle multi-line strings in caml mode."
1624 ;this advice doesn't make sense in other modes. I wish there were a
1625 ;cleaner way to do this: I haven't found one.
1627 (let ((hooked (and (eq major-mode 'caml-mode) (caml-in-literal-p)))
1628 (split-mark))
1629 (if (not hooked) nil
1630 (setq split-mark (set-marker (make-marker) (point)))
1631 (caml-split-string))
1632 ad-do-it
1633 (if (not hooked) nil
1634 (goto-char split-mark)
1635 (set-marker split-mark nil))))
1637 (defadvice newline-and-indent (around
1638 caml-newline-and-indent
1639 activate)
1641 "Handle multi-line strings in caml mode."
1643 (let ((hooked (and (eq major-mode 'caml-mode) (caml-in-literal-p)))
1644 (split-mark))
1645 (if (not hooked) nil
1646 (setq split-mark (set-marker (make-marker) (point)))
1647 (caml-split-string))
1648 ad-do-it
1649 (if (not hooked) nil
1650 (goto-char split-mark)
1651 (set-marker split-mark nil))))
1653 (defun caml-electric-pipe ()
1654 "If inserting a | or } operator at beginning of line, reindent the line.
1656 Unfortunately there is a situation where this mechanism gets
1657 confused. It's when | is the first character of a |] sequence. This is
1658 a misfeature of caml syntax and cannot be fixed, however, as a
1659 workaround, the electric ] inserts | itself if the matching [ is
1660 followed by |."
1662 (interactive "*")
1663 (let ((electric (and caml-electric-indent
1664 (caml-in-indentation)
1665 (not (caml-in-comment-p)))))
1666 (self-insert-command 1)
1667 (if electric (save-excursion (caml-indent-command)))))
1669 (defun caml-electric-rb ()
1670 "If inserting a ] operator at beginning of line, reindent the line.
1672 Also, if the matching [ is followed by a | and this ] is not preceded
1673 by |, insert one."
1675 (interactive "*")
1676 (let* ((prec (preceding-char))
1677 (use-pipe (and caml-electric-close-vector
1678 (not (caml-in-comment-p))
1679 (not (caml-in-literal-p))
1680 (or (not (numberp prec))
1681 (not (char-equal ?| prec)))))
1682 (electric (and caml-electric-indent
1683 (caml-in-indentation)
1684 (not (caml-in-comment-p)))))
1685 (self-insert-command 1)
1686 (if electric (save-excursion (caml-indent-command)))
1687 (if (and use-pipe
1688 (save-excursion
1689 (condition-case nil
1690 (prog2
1691 (backward-list 1)
1692 (looking-at "\\[|"))
1693 (error ""))))
1694 (save-excursion
1695 (backward-char 1)
1696 (insert "|")))))
1698 (defun caml-abbrev-hook ()
1699 "If inserting a leading keyword at beginning of line, reindent the line."
1700 ;itz unfortunately we need a special case
1701 (if (and (not (caml-in-comment-p)) (not (= last-command-char ?_)))
1702 (let* ((bol (save-excursion (beginning-of-line) (point)))
1703 (kw (save-excursion
1704 (and (re-search-backward "^[ \t]*\\(\\sw+\\)\\=" bol t)
1705 (caml-match-string 1)))))
1706 (if kw
1707 (let ((indent (save-excursion
1708 (goto-char (match-beginning 1))
1709 (caml-indent-command)
1710 (current-column)))
1711 (abbrev-correct (if (= last-command-char ?\ ) 1 0)))
1712 (indent-to (- indent
1714 (symbol-value
1715 (nth 1
1716 (assoc kw caml-leading-kwops-alist)))
1718 abbrev-correct)))))))
1720 ; (defun caml-indent-phrase ()
1721 ; (interactive "*")
1722 ; (let ((bounds (caml-mark-phrase)))
1723 ; (indent-region (car bounds) (cdr bounds) nil)))
1725 ;;; Additional commands by Didier to report errors in toplevel mode
1727 (defun caml-skip-blank-forward ()
1728 (if (looking-at "[ \t\n]*\\((\\*\\([^*]\\|[^(]\\*[^)]\\)*\\*)[ \t\n]*\\)*")
1729 (goto-char (match-end 0))))
1731 ;; to mark phrases, so that repeated calls will take several of them
1732 ;; knows little about Ocaml appart literals and comments, so it should work
1733 ;; with other dialects as long as ;; marks the end of phrase.
1735 (defun caml-indent-phrase (arg)
1736 "Indent current phrase
1737 with prefix arg, indent that many phrases starting with the current phrase."
1738 (interactive "p")
1739 (save-excursion
1740 (let ((beg (caml-find-phrase)))
1741 (while (progn (setq arg (- arg 1)) (> arg 0)) (caml-find-phrase))
1742 (indent-region beg (point) nil))))
1744 (defun caml-indent-buffer ()
1745 (interactive)
1746 (indent-region (point-min) (point-max) nil))
1748 (defun caml-backward-to-less-indent (&optional n)
1749 "Move cursor back N lines with less or same indentation."
1750 (interactive "p")
1751 (beginning-of-line 1)
1752 (if (< n 0) (caml-forward-to-less-indent (- n))
1753 (while (> n 0)
1754 (let ((i (current-indentation)))
1755 (forward-line -1)
1756 (while (or (> (current-indentation) i)
1757 (caml-in-comment-p)
1758 (looking-at
1759 (concat "[ \t]*\\(\n\\|" comment-start-skip "\\)")))
1760 (forward-line -1)))
1761 (setq n (1- n))))
1762 (back-to-indentation))
1764 (defun caml-forward-to-less-indent (&optional n)
1765 "Move cursor back N lines with less or same indentation."
1766 (interactive "p")
1767 (beginning-of-line 1)
1768 (if (< n 0) (caml-backward-to-less-indent (- n))
1769 (while (> n 0)
1770 (let ((i (current-indentation)))
1771 (forward-line 1)
1772 (while (or (> (current-indentation) i)
1773 (caml-in-comment-p)
1774 (looking-at
1775 (concat "[ \t]*\\(\n\\|" comment-start-skip "\\)")))
1776 (forward-line 1)))
1777 (setq n (1- n))))
1778 (back-to-indentation))
1780 (defun caml-insert-begin-form ()
1781 "Inserts a nicely formatted begin-end form, leaving a mark after end."
1782 (interactive "*")
1783 (let ((prec (preceding-char)))
1784 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1785 (insert " ")))
1786 (let* ((c (current-indentation)) (i (+ caml-begin-indent c)))
1787 (insert "begin\n\nend")
1788 (push-mark)
1789 (indent-line-to c)
1790 (forward-line -1)
1791 (indent-line-to i)))
1793 (defun caml-insert-for-form ()
1794 "Inserts a nicely formatted for-do-done form, leaving a mark after do(ne)."
1795 (interactive "*")
1796 (let ((prec (preceding-char)))
1797 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1798 (insert " ")))
1799 (let* ((c (current-indentation)) (i (+ caml-for-indent c)))
1800 (insert "for do\n\ndone")
1801 (push-mark)
1802 (indent-line-to c)
1803 (forward-line -1)
1804 (indent-line-to i)
1805 (push-mark)
1806 (beginning-of-line 1)
1807 (backward-char 4)))
1809 (defun caml-insert-if-form ()
1810 "Insert nicely formatted if-then-else form leaving mark after then, else."
1811 (interactive "*")
1812 (let ((prec (preceding-char)))
1813 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1814 (insert " ")))
1815 (let* ((c (current-indentation)) (i (+ caml-if-indent c)))
1816 (insert "if\n\nthen\n\nelse\n")
1817 (indent-line-to i)
1818 (push-mark)
1819 (forward-line -1)
1820 (indent-line-to c)
1821 (forward-line -1)
1822 (indent-line-to i)
1823 (push-mark)
1824 (forward-line -1)
1825 (indent-line-to c)
1826 (forward-line -1)
1827 (indent-line-to i)))
1829 (defun caml-insert-match-form ()
1830 "Insert nicely formatted match-with form leaving mark after with."
1831 (interactive "*")
1832 (let ((prec (preceding-char)))
1833 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1834 (insert " ")))
1835 (let* ((c (current-indentation)) (i (+ caml-match-indent c)))
1836 (insert "match\n\nwith\n")
1837 (indent-line-to i)
1838 (push-mark)
1839 (forward-line -1)
1840 (indent-line-to c)
1841 (forward-line -1)
1842 (indent-line-to i)))
1844 (defun caml-insert-let-form ()
1845 "Insert nicely formatted let-in form leaving mark after in."
1846 (interactive "*")
1847 (let ((prec (preceding-char)))
1848 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1849 (insert " ")))
1850 (let* ((c (current-indentation)))
1851 (insert "let in\n")
1852 (indent-line-to c)
1853 (push-mark)
1854 (forward-line -1)
1855 (forward-char (+ c 4))))
1857 (defun caml-insert-try-form ()
1858 "Insert nicely formatted try-with form leaving mark after with."
1859 (interactive "*")
1860 (let ((prec (preceding-char)))
1861 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1862 (insert " ")))
1863 (let* ((c (current-indentation)) (i (+ caml-try-indent c)))
1864 (insert "try\n\nwith\n")
1865 (indent-line-to i)
1866 (push-mark)
1867 (forward-line -1)
1868 (indent-line-to c)
1869 (forward-line -1)
1870 (indent-line-to i)))
1872 (defun caml-insert-while-form ()
1873 "Insert nicely formatted while-do-done form leaving mark after do, done."
1874 (interactive "*")
1875 (let ((prec (preceding-char)))
1876 (if (and (numberp prec) (not (char-equal ?\ (char-syntax prec))))
1877 (insert " ")))
1878 (let* ((c (current-indentation)) (i (+ caml-if-indent c)))
1879 (insert "while do\n\ndone")
1880 (push-mark)
1881 (indent-line-to c)
1882 (forward-line -1)
1883 (indent-line-to i)
1884 (push-mark)
1885 (beginning-of-line 1)
1886 (backward-char 4)))
1888 (autoload 'run-caml "inf-caml" "Run an inferior Caml process." t)
1890 (autoload 'caml-types-show-type "caml-types"
1891 "Show the type of expression or pattern at point." t)
1892 (autoload 'caml-types-explore "caml-types"
1893 "Explore type annotations by mouse dragging." t)
1895 (autoload 'caml-help "caml-help"
1896 "Show documentation for qualilifed OCaml identifier." t)
1897 (autoload 'caml-complete "caml-help"
1898 "Does completion for documented qualified OCaml identifier." t)
1899 (autoload 'ocaml-open-module "caml-help"
1900 "Add module in documentation search path." t)
1901 (autoload 'ocaml-close-module "caml-help"
1902 "Remove module from documentation search path." t)
1903 (autoload 'ocaml-add-path "caml-help"
1904 "Add search path for documentation." t)
1906 ;;; caml.el ends here
1908 (provide 'caml)