This file is indexed.

/usr/share/emacs/site-lisp/ocaml-mode/caml-help.el is in ocaml-mode 4.01.0-5.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
;;; caml-help.el --- Contextual completion and help to caml-mode
;(***********************************************************************)
;(*                                                                     *)
;(*                                OCaml                                *)
;(*                                                                     *)
;(*            Didier Remy, projet Cristal, INRIA Rocquencourt          *)
;(*                                                                     *)
;(*  Copyright 2001 Institut National de Recherche en Informatique et   *)
;(*  en Automatique.  All rights reserved.  This file is distributed    *)
;(*  under the terms of the GNU General Public License.                 *)
;(*                                                                     *)
;(***********************************************************************)

;; Author: Didier Remy, November 2001.

;;; Commentary:

;; This provides two functions: completion and help.
;; Look for caml-complete and caml-help.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  This is a preliminary version.
;;
;;  Possible improvements?
;;   - dump some databaes: Info, Lib, ...
;;   - accept a search path for local libraries instead of current dir
;;     (then distinguish between different modules lying in different
;;     directories)
;;   - improve the construction for info files.
;;
;;  Abstract over
;;   - the viewing method and the database, so that the documentation for
;;     and identifier could be search in
;;       * info / html / man / mli's sources
;;       * viewed in Emacs or using an external previewer.
;;
;;  Take all identifiers (labels, Constructors, exceptions, etc.)
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; Code:

(eval-and-compile
  (if (featurep 'xemacs)
      (require 'caml-xemacs)
    (require 'caml-emacs)))

;; Loading or building databases.
;;

;; variables to be customized

(defvar ocaml-lib-path 'lazy
  "Path list for ocaml lib sources (mli files).

`lazy' means ask ocaml to find it for your at first use.")
(defun ocaml-lib-path ()
  "Compute if necessary and return the path for ocaml libs."
  (if (listp ocaml-lib-path) nil
    (setq ocaml-lib-path
          (split-string
           (shell-command-to-string
            (or
             (and (boundp 'inferior-caml-program)
                      (string-match "\\([^ ]*/ocaml\\)\\( \\|$\\)"
                       inferior-caml-program)
                      (let ((file
                             (concat (match-string 1 inferior-caml-program)
                                     "c")))
                        (and (file-executable-p file)
                             (concat file " -where"))))
             "ocamlc -where")))))
    ocaml-lib-path)



;; General purpose auxiliary functions

(defun ocaml-capitalize (s)
  (concat (capitalize (substring s 0 1)) (substring s 1)))

(defun ocaml-uncapitalize (s)
  (if (> (length s) 0)
      (concat (downcase (substring s 0 1)) (substring s 1))
    s))

(defun ocaml-find-files (path filter &optional depth split)
  (let* ((path-string
          (if (stringp path)
              (if (file-directory-p path) path nil)
            (mapconcat (lambda (d) (if (file-directory-p d) d))
                       path " ")))
         (command
          (and path-string
               (concat "find " path-string
                       " '(' " filter " ')' "
                       (if depth (concat " -maxdepth " (int-to-string depth)))
                       (if split nil " -printf '%\p '")
                       )))
          (files
           (and command (shell-command-to-string command))))
         (if (and split (stringp files)) (split-string files "\n") files)
         ))

;; Specialized auxiliary functions


;; Global table of modules contents of modules loaded lazily.

(defvar ocaml-module-alist 'lazy
  "A-list of modules with how and where to find help information.
`delay' means non computed yet.")

(defun ocaml-add-mli-modules (modules tag &optional path)
  (let ((files
         (ocaml-find-files (or path (ocaml-lib-path))
                           "-type f -name '*.mli'" 1 t)))
    (while (consp files)
      (if (string-match "\\([^/]*\\).mli" (car files))
          (let* ((module (ocaml-capitalize (match-string 1 (car files))))
                 (dir (file-name-directory (car files)))
                 (dirp (member dir (ocaml-lib-path))))
            (if (and (consp dirp) (string-equal dir (car dirp)))
                (setq dir (car dirp)))
            (if (assoc module modules) nil
              (setq modules
                    (cons (cons module (cons (cons tag dir) 'lazy)) modules))
              )))
      (setq files (cdr files)))
    modules))

(defun ocaml-add-path (dir &optional path)
  "Extend  `ocaml-module-alist' with modules of DIR relative to PATH."
  (interactive "D")
  (let* ((old (ocaml-lib-path))
         (new
          (if (file-name-absolute-p dir) dir
            (concat
             (or (find-if (lambda (p) (file-directory-p (concat p  "/" dir)))
                      (cons default-directory old))
                 (error "Directory not found"))
             "/" dir))))
    (setq ocaml-lib-path (cons (car old) (cons new (cdr old))))
    (setq ocaml-module-alist
          (ocaml-add-mli-modules (ocaml-module-alist) 'lib new))))

(defun ocaml-module-alist ()
  "Call by need value of variable `ocaml-module-alist'."
  (if (listp ocaml-module-alist)
      nil
    ;; build list of mli files
    (setq ocaml-module-alist (ocaml-add-mli-modules nil 'lib))
    ;; dumping information ? TODO
    )
  ocaml-module-alist)

(defun ocaml-get-or-make-module (module &optional tag)
  (let ((info (assoc module (ocaml-module-alist))))
    (if info nil
      (setq info (cons module (cons (cons 'local default-directory) 'lazy)))
      (setq ocaml-module-alist (cons info ocaml-module-alist))
      )
    info))

;; Symbols of module are lazily computed

(defun ocaml-module-filename (module)
  (let ((module (ocaml-uncapitalize module)) (name))
    (if (file-exists-p (setq name (concat module ".mli"))) nil
      (let ((tmp (ocaml-lib-path)))
        (while (consp tmp)
          (setq name (concat (car tmp) "/" module ".mli"))
          (if (file-exists-p name) (setq tmp nil)
            (setq name nil)))))
    name))

(defun ocaml-module-symbols (module-info)
  (let* ((module (car module-info))
         (tail (and module-info (cdr module-info)))
         (tag (caar tail))
         (dir (cdar tail))
         (file)
         (alist))
    (if (listp (cdr tail))
        (cdr tail)
      (if (equal tag 'info)
          (setq dir (car ocaml-lib-path)) ; XXX to be fixed
        )
      (setq file (concat dir "/" (ocaml-uncapitalize module) ".mli"))
      (message file)
      (save-window-excursion
        (set-buffer (get-buffer-create "*caml-help*"))
        (if (and file (file-exists-p file))
            (progn
              (message "Scanning module %s" file)
              (insert-file-contents file))
          (message "Module %s not found" module))
        (while (re-search-forward
                "\\([ \t]*val\\|let\\|exception\\|external\\|  [|]\\) \\([a-zA-Z_0-9'][a-zA-Z_0-9']*\\)\\|^  *[{]* \\([a-z_][A-Za-z_0-9]*\\) : [^;\n][^;\n]*;"
                (point-max) 'move)
          (pop-to-buffer (current-buffer))
          (setq alist (cons (or (match-string 2) (match-string 3)) alist)))
        (erase-buffer)
        )
      (setcdr tail alist)
      alist)
      ))

;; Local list of visible modules.

(defvar ocaml-visible-modules 'lazy
  "A-list of open modules, local to every file.")
(make-variable-buffer-local 'ocaml-visible-modules)
(defun ocaml-visible-modules ()
  (if (listp ocaml-visible-modules) nil
    (progn
      (setq ocaml-visible-modules
            (list (ocaml-get-or-make-module "Pervasives")))
      (save-excursion
        (goto-char (point-min))
        (while (re-search-forward "^ *open  *\\([A-Z][a-zA-Z'_0-9]*\\)"
                                  (point-max) t)
          (let ((module (match-string 1)))
            (if (assoc module ocaml-visible-modules) nil
              (setq ocaml-visible-modules
                    (cons (ocaml-get-or-make-module module)
                          ocaml-visible-modules)))))
        )))
  ocaml-visible-modules)

(defun ocaml-open-module (arg)
  "*Make module of name ARG visible whe ARG is a string.
When call interactively, make completion over known modules."
  (interactive "P")
  (if (not (stringp arg))
      (let ((modules (ocaml-module-alist)))
        (setq arg
              (completing-read "Open module: " modules))))
  (if (and (stringp arg) (not (equal arg "")))
      (progn
        (if (assoc arg (ocaml-visible-modules))
            (ocaml-close-module arg))
        (setq ocaml-visible-modules
              (cons (ocaml-get-or-make-module arg) (ocaml-visible-modules)))
        ))
  (message "%S" (mapcar 'car (ocaml-visible-modules))))

(defun ocaml-close-module (arg)
  "*Close module of name ARG when ARG is a string.
When call interactively, make completion over visible modules.
Otherwise if ARG is true, close all modules and reset to default."
  (interactive "P")
  (if (= (prefix-numeric-value arg) 4)
      (setq ocaml-visible-modules 'lazy)
    (let* ((modules (ocaml-visible-modules)))
      (if (null modules) (error "No visible module to close"))
      (unless (stringp arg)
        (setq arg
              (completing-read
               (concat "Close module ["  (caar modules) "] : ")
               modules))
        (if (equal arg "") (setq arg (caar modules))))
      (setq ocaml-visible-modules
            (remove-if (lambda (m) (equal (car m) arg))
                       ocaml-visible-modules))
      ))
  (message "%S" (mapcar 'car (ocaml-visible-modules))))


;; Look for identifiers around point

(defun ocaml-qualified-identifier (&optional show)
  "Search for a qualified identifier (Path. entry) around point.

Entry may be nil.
Currently, the path may only be nil or a single Module.
For paths is of the form Module.Path', it returns Module
and always nil for entry.

If defined Module and Entry are represented by a region in the buffer,
and are nil otherwise.

For debugging purposes, it returns the string Module.entry if called
with an optional non-nil argument."
  (save-excursion
    (let ((module) (entry))
      (if (looking-at "[ \n]") (skip-chars-backward " "))
      (if (re-search-backward
           "\\([^A-Za-z0-9_.']\\|\\`\\)\\([A-Za-z0-9_']*[.]\\)*[A-Za-z0-9_']*\\="
           (- (point) 100) t)
          (progn
            (or (looking-at "\\`[A-Za-z)-9_.]") (forward-char 1))
            (if (looking-at "\\<\\([A-Za-z_][A-Za-z0-9_']*\\)[.]")
                (progn
                  (setq module (cons (match-beginning 1) (match-end 1)))
                  (goto-char (match-end 0))))
            (if (looking-at "\\<\\([A-Za-z_][A-Za-z0-9_']*\\)\\>")
                (setq entry (cons (match-beginning 1) (match-end 1))))))
      (if show
          (concat
           (and module (buffer-substring (car module) (cdr module)))
           "."
           (and entry (buffer-substring (car entry) (cdr entry))))
      (cons module entry))
    )))

;; completion around point

(defun ocaml-completion (pattern module)
  (let ((list
         (or
          (and module
               (list
                (or (assoc module (ocaml-module-alist))
                    (error "Unknown module %s" module))))
          (ocaml-visible-modules))))
    (message "Completion from %s" (mapconcat 'car list " "))
    (if (null pattern)
        (apply 'append (mapcar 'ocaml-module-symbols list))
      (let ((pat (concat "^" (regexp-quote pattern))) (res))
        (mapc
         (lambda (l)
           (mapc (lambda (x)
                   (if (string-match pat (car l))
                       (if (member x res) nil (setq res (cons x res)))))
                 (ocaml-module-symbols l)))
         list)
        res)
      )))

(defun caml-complete (arg)
  "Does completion for OCaml identifiers qualified.

It attemps to recognize an qualified identifier Module . entry
around point using function \\[ocaml-qualified-identifier].

If Module is defined, it does completion for identifier in Module.

If Module is undefined, it does completion in visible modules.
Then, if completion fails, it does completion among  all modules
where identifier is defined."
  (interactive "p")
  (let* ((module-entry (ocaml-qualified-identifier)) (entry)
         (module)
         (beg) (end) (pattern))
    (if (car module-entry)
        (progn
          (setq module
                (buffer-substring (caar module-entry) (cdar module-entry)))
          (or (assoc module (ocaml-module-alist))
              (and (setq module
                         (completing-read "Module: " (ocaml-module-alist)
                                          nil nil module))
                   (save-excursion
                     (goto-char (caar module-entry))
                     (delete-region (caar module-entry) (cdar module-entry))
                     (insert module) t)
                   (setq module-entry (ocaml-qualified-identifier))
                   (car module-entry)
                   (progn (setq entry (cdr module-entry)) t))
              (error "Unknown module %s" module))))
    (if (consp (cdr module-entry))
        (progn
          (setq beg (cadr module-entry))
          (setq end (cddr module-entry)))
      (if (and module
           (save-excursion
            (goto-char (cdar module-entry))
            (looking-at " *[.]")))
          (progn
            (setq beg (match-end 0))
            (setq end beg))))
    (if (not (and beg end))
        (error "Did not find anything to complete around point")

      (setq pattern (buffer-substring beg end))
      (let* ((all-completions (ocaml-completion pattern module))
             (completion
              (try-completion pattern (mapcar 'list all-completions))))
        (cond ((eq completion t))

              ((null completion)
               (let*
                   ((modules (ocaml-find-module pattern))
                    (visible (intersection modules (ocaml-visible-modules)))
                    (hist)
                    (module
                     (cond
                      ((null modules)
                       nil)
                      ((equal (length modules) 1)
                       (caar modules))
                      ((equal (length visible) 1)
                       (caar visible))
                      (t
                       (setq hist (mapcar 'car modules))
                       (completing-read "Module: " modules nil t
                                        "" (cons hist 0)))
                      )))
                 (if (null module)
                     (error "Can't find completion for \"%s\"" pattern)
                   (message "Completion found in module %s" module)
                   (if (and (consp module-entry) (consp (cdr module-entry)))
                       (delete-region (caar module-entry) end)
                     (delete-region beg end))
                   (insert module "." pattern))))

              ((not (string-equal pattern completion))
               (delete-region beg end)
               (goto-char beg)
               (insert completion))

              (t
                (with-output-to-temp-buffer "*Completions*"
                  (display-completion-list all-completions))
                ))
               ))))


;; Info files (only in ocamldoc style)


(defvar ocaml-info-prefix "ocaml-lib"
  "Prefix of ocaml info files describing library modules.
Suffix .info will be added to info files.
Additional suffix .gz may be added if info files are compressed.")
;;

(defun ocaml-hevea-info-add-entries (entries dir name)
  (let*
      ((filter
        (concat "-type f -regex '.*/" name
                "\\(.info\\|\\)\\(-[0-9]*\\|\\)\\([.]gz\\|\\)'"
                ))
       (section-regexp
        "\\* \\(Section [1-9][0-9--]*\\)::[ \t][ \t]*Module *\\([A-Z][A-Za-z_0-9]*\\)")
       (files (ocaml-find-files dir filter))
       (command))
    ;; scanning info files
    (if (or (null files)
            (not (stringp files))
            (string-match files "^ *$"))
        (message "No info file found: %s." (mapconcat 'identity files " "))
      (message "Scanning info files %s." files)
      (save-window-excursion
        (set-buffer (get-buffer-create "*caml-help*"))
        (setq command
              (concat "zcat -f " files
                      " | grep -e '" section-regexp "'"))
        (message "Scanning files with: %s" command)
        (or (shell-command command (current-buffer))
            (error "Error while scanning"))
        (goto-char (point-min))
        (while (re-search-forward section-regexp (point-max) t)
          (let* ((module (match-string 2))
                 (section (match-string 1)))
            ;; (message "%s %s" module section)
            (if (assoc module entries) nil
              (setq entries
                    (cons (cons module (concat "(" name ")" section))
                          entries))
              )))
        (let ((buf (get-buffer "*caml-help*")))
          (if buf (kill-buffer buf)))))
    entries))

(defun ocaml-hevea-info ()
  "The default way to create an info data base from the value
of \\[Info-default-directory-list] and the base name \\[ocaml-info-name]
of files to look for.

This uses info files produced by HeVeA."
  (let ((collect) (seen))
    (mapc (lambda (d)
            (if (member d seen) nil
              (setq collect
                    (ocaml-hevea-info-add-entries
                     collect d ocaml-info-prefix))
              (setq seen (cons d seen))))
          Info-directory-list)
    collect))

(defun ocaml-ocamldoc-info-add-entries (entries dir name)
  (let*
      ((module-regexp "^Node: \\([A-Z][A-Za-z_0-9]*\\)[^ ]")
       (command
        (concat
         "find " dir " -type f -regex '.*/" name
         "\\(.info\\|\\)\\([.]gz\\|\\)' -print0"
         " | xargs -0 zcat -f | grep '" module-regexp "'")))
    (message "Scanning info files in %s" dir)
    (save-window-excursion
      (set-buffer (get-buffer-create "*caml-help*"))
      (or (shell-command command (current-buffer))
          (error "Could not run:%s" command))
      (goto-char (point-min))
      (while (re-search-forward module-regexp (point-max) t)
        (if (equal (char-after (match-end 1)) 127)
            (let* ((module (match-string 1)))
              (if (assoc module entries) nil
                (setq entries
                      (cons (cons module (concat "(" name ")" module))
                            entries))
                ))))
      ; (kill-buffer (current-buffer))
      )
    entries))

(defun ocaml-ocamldoc-info ()
  "The default way to create an info data base from the value
of \\[Info-default-directory-list] and the base name \\[ocaml-info-name]
of files to look for.

This uses info files produced by ocamldoc."
  (require 'info)
  (let ((collect) (seen))
    (mapc (lambda (d)
            (if (member d seen) nil
              (setq collect
                    (ocaml-ocamldoc-info-add-entries collect d
                                                     ocaml-info-prefix))
              (setq seen (cons d seen))))
          Info-directory-list)
    collect))

;; Continuing

(defvar ocaml-info-alist 'ocaml-ocamldoc-info
  "A-list binding module names to info entries:

  nil means do not use info.

  A function to build the list lazily (at the first call).  The result of
the function call will be assign permanently to this variable for future
uses.  We provide two default functions `ocaml-hevea-info'
\(info produced by HeVeA is the default) and `ocaml-ocamldoc-info'
\(info produced by ocamldoc).

  Otherwise, this value should be an alist binding module names to info
entries of the form to \"(entry)section\" be taken by the \\[info]
command. An entry may be an info module or a complete file name."
)

(defun ocaml-info-alist ()
  "Call by need value of variable `ocaml-info-alist'."
  (cond
   ((listp ocaml-info-alist))
   ((functionp ocaml-info-alist)
    (setq ocaml-info-alist (apply ocaml-info-alist nil)))
   (t
    (error "wrong type for ocaml-info-alist")))
  ocaml-info-alist)

;; help around point

(defun ocaml-find-module (symbol &optional module-list)
  (let ((list (or module-list (ocaml-module-alist)))
        (collect))
    (while (consp list)
      (if (member symbol (ocaml-module-symbols (car list)))
          (setq collect (cons (car list) collect)))
      (setq list (cdr list)))
    (nreverse collect)
    ))

(defun ocaml-buffer-substring (region)
  (and region (buffer-substring-no-properties (car region) (cdr region))))

;; Help function.

(defvar view-return-to-alist)
(defvar view-exit-action)

(defun ocaml-goto-help (&optional module entry same-window)
  "Search info manual for MODULE and ENTRY in MODULE.
If unspecified, MODULE and ENTRY are inferred from the position in the
current buffer using \\[ocaml-qualified-identifier]."
  (interactive)
  (let ((window (selected-window))
        (info-section (assoc module (ocaml-info-alist))))
    (if info-section
        (caml-info-other-window (cdr info-section))
      (ocaml-visible-modules)
      (let* ((module-info
              (or (assoc module (ocaml-module-alist))
                  (and (file-exists-p
                        (concat (ocaml-uncapitalize module) ".mli"))
                       (ocaml-get-or-make-module module))))
             (location (cdr (cadr module-info))))
        (cond
         (location
          (let ((file (concat location (ocaml-uncapitalize module) ".mli")))
            (if (window-live-p same-window)
                (progn (select-window same-window)
                       (view-mode-exit view-return-to-alist view-exit-action))
              ;; (view-buffer (find-file-noselect file) 'view))
              )
            (view-file-other-window file)
            (bury-buffer (current-buffer))))
         (info-section (error "Aborted"))
         (t (error "No help for module %s" module))))
      )
    (if (stringp entry)
        (let ((here (point))
              (regex (regexp-quote entry))
              (case-fold-search nil))
          (goto-char (point-min))
          (if (or (re-search-forward
                   (concat "\\(val\\|exception\\|type\\|external\\|[|{;]\\) +"
                           regex)
                   ;; (concat "\\(val\\|exception\\|external\\) +\\("
                   ;;         regex "\\|( *" regex " *)\\)")
                   (point-max) t)
                  (re-search-forward
                   (concat "type [^{]*{[^}]*" regex " :")
                   ;; (concat "\\(type\\|[|{;]\\) +" regex)
                   (point-max) t)
                  (progn
                    (if (window-live-p window) (select-window window))
                    (error "Entry %s not found in module %s"
                           entry module))
                  ;; (search-forward entry (point-max) t)
                  )
              (ocaml-help-show -1)
            (progn
              (message "Help for entry %s not found in module %s"
                       entry module)
              (goto-char here)))
          ))
    (ocaml-link-activate (cdr info-section))
    (if (window-live-p window) (select-window window))
    ))

(defface ocaml-help-face
  '((t :background "#88FF44"))
  "Face to highlight expressions and types.")

(defvar ocaml-help-ovl
  (let ((ovl (make-overlay 1 1)))
    (overlay-put ovl 'face 'ocaml-help-face)
    ovl))

(defun caml-help (arg)
  "Find documentation for OCaml qualified identifiers.

It attemps to recognize an qualified identifier of the form
``Module . entry'' around point using function `ocaml-qualified-identifier'.

If Module is undetermined it is temptatively guessed from the identifier name
and according to visible modules.  If this is still unsucessful,  the user is
then prompted for a Module name.

The documentation for Module is first seach in the info manual if available,
then in the ``module.mli'' source file.  The entry is then searched in the
documentation.

Visible modules are computed only once, at the first call.
Modules can be made visible explicitly with `ocaml-open-module' and
hidden with `ocaml-close-module'.

Prefix arg 0 forces recompilation of visible modules (and their content)
from the file content.

Prefix arg 4 prompts for Module and identifier instead of guessing values
from the possition of point in the current buffer."
  (interactive "p")
  (delete-overlay ocaml-help-ovl)
  (let ((module) (entry) (module-entry))
    (cond
     ((= arg 4)
      (or (and
           (setq module
                (completing-read "Module: " (ocaml-module-alist)
                                 nil t "" (cons 'hist 0)))
           (not (string-equal module "")))
          (error "Quit"))
      (let ((symbols
             (mapcar 'list
                     (ocaml-module-symbols
                      (assoc module (ocaml-module-alist))))))
        (setq entry
              (completing-read (format "Value: %s." module) symbols nil t)))
      (if (string-equal entry "") (setq entry nil))
      )
     (t
      (if (= arg 0) (setq ocaml-visible-modules 'lazy))
      (setq module-entry (ocaml-qualified-identifier))
      (setq entry (ocaml-buffer-substring (cdr module-entry)))
      (setq module
            (or (ocaml-buffer-substring (car module-entry))
                (let ((modules
                       (or (ocaml-find-module entry (ocaml-visible-modules))
                           (ocaml-find-module entry)))
                      (hist) (default))
                  (cond
                   ((null modules)
                    (error "No module found for entry %s" entry))
                   ((equal (length modules) 1)
                    (caar modules))
                   (t
                    (setq hist (mapcar 'car modules))
                    (setq default (car hist))
                    (setq module
                          (completing-read
                           (concat "Module: "
                                   (and default (concat "[" default "] ")))
                           modules nil t "" (cons 'hist 0)))
                    (if (string-equal module "") default module))
                   ))))
      ))
     (message "Help for %s%s%s" module (if entry "." "") (or entry ""))
     (ocaml-goto-help module entry)
     ))

;; auto-links

(defconst ocaml-link-regexp
  "\\(type\\|and\\) \\('[a-z] +\\|(\\('[a-z], *\\)*'[a-z])\\|\\) *\\([a-zA-Z0-9_]*\\)\\( *$\\| =\\)")
(defconst ocaml-longident-regexp
  "\\([A-Z][a-zA-Z_0]*\\)[.]\\([a-zA-Z][A-Za-z0-9_]*\\)")

(defvar ocaml-links nil
  "Local links in the current of last info node or interface file.

The car of the list is a key that indentifies the module to prevent
recompilation when next help command is relative to the same module.
The cdr is a list of elments, each of which is an string and a pair of
buffer positions."
)
(make-variable-buffer-local 'ocaml-links)

(defun ocaml-info-links (section)
  (cdr
   (if (and ocaml-links section (equal (car ocaml-links) section))
       ocaml-links
     (save-excursion
       (goto-char (point-min))
       (let ((regexp (concat (if (equal major-mode 'Info-mode) "^ - " "^")
                             ocaml-link-regexp))
             (all))
         (while (re-search-forward regexp (point-max) t)
           (setq all
                 (cons (cons (match-string 4)
                             (cons (match-beginning 4)
                                   (match-end 4)))
                       all)))
         (setq ocaml-links (cons section all))
         )))))

(defvar ocaml-link-map
  (let ((map (make-sparse-keymap)))
    (define-key map [mouse-2] 'ocaml-link-goto)
    map))

(defun ocaml-help-show (arg)
  (let ((right (point))
        (left (progn (forward-word arg) (point))))
    (goto-char right)
    (move-overlay ocaml-help-ovl left right (current-buffer))
    (recenter 1)
    ))

(defun ocaml-link-goto (click)
  "Follow link at point."
  (interactive "e")
  (let* ((pos (caml-event-point-start click))
         (win (caml-event-window click))
         (buf (window-buffer win))
         (window (selected-window))
         (link))
    (setq link
          (with-current-buffer buf
            (buffer-substring
             (previous-single-property-change (+ pos 1) 'local-map
                                                   buf (- pos 100))
             (next-single-property-change pos  'local-map
                                        buf (+ pos 100)))))
    (if (string-match (concat "^" ocaml-longident-regexp "$") link)
        (ocaml-goto-help (match-string 1 link) (match-string 2 link) win)
      (if (not (equal (window-buffer window) buf))
          (switch-to-buffer-other-window buf))
      (if (setq link (assoc link (cdr ocaml-links)))
          (progn
            (goto-char (cadr link))
            (ocaml-help-show 1)))
      (if (window-live-p window) (select-window window))
      )))


(defface ocaml-link-face
  '((((class color)) :foreground "Purple"))
  "Face to highlight hyperlinks.")

(defun ocaml-link-activate (section)
  (let ((links (ocaml-info-links section)))
    (if links
        (let ((regexp (concat "[^A-Za-z0-9'_]\\("
                              ocaml-longident-regexp "\\|"
                              (mapconcat 'car links "\\|")
                              "\\)[^A-Za-z0-9'_]"))
              (case-fold-search nil))
          (save-excursion
            (goto-char (point-min))
            (let ((buffer-read-only nil)
                  ;; use of dynamic scoping, need not be restored!
                  (modified-p (buffer-modified-p)))
              (unwind-protect
                  (save-excursion
                    (goto-char (point-min))
                    (while (re-search-forward regexp (point-max) t)
                      (put-text-property (match-beginning 1) (match-end 1)
                                         'mouse-face 'highlight)
                      (put-text-property (match-beginning 1) (match-end 1)
                                         'local-map ocaml-link-map)
                      (if (x-display-color-p)
                          (put-text-property (match-beginning 1) (match-end 1)
                                             'face 'ocaml-link-face)))
                    )
                ;; need to restore flag if buffer was unmodified.
                (unless modified-p (set-buffer-modified-p nil))
                ))
            )))))



;; bindings ---now in caml.el

; (and
;  (boundp 'caml-mode-map)
;  (keymapp caml-mode-map)
;  (progn
;    (define-key caml-mode-map [?\C-c?i] 'ocaml-add-path)
;    (define-key caml-mode-map [?\C-c?]] 'ocaml-close-module)
;    (define-key caml-mode-map [?\C-c?[] 'ocaml-open-module)
;    (define-key caml-mode-map [?\C-c?\C-h] 'caml-help)
;    (define-key caml-mode-map [?\C-c?\t] 'caml-complete)
;    (let ((map (lookup-key caml-mode-map [menu-bar caml])))
;      (and
;       (keymapp map)
;       (progn
;         (define-key map [separator-help] '("---"))
;         (define-key map [open] '("Open add path" . ocaml-add-path ))
;         (define-key map [close]
;           '("Close module for help" . ocaml-close-module))
;         (define-key map [open] '("Open module for help" . ocaml-open-module))
;         (define-key map [help] '("Help for identifier" . caml-help))
;         (define-key map [complete] '("Complete identifier" . caml-complete))
;         )
;    ))))


(provide 'caml-help)
;;; caml-help.el ends here