 (setq max-specpdl-size 10000)
 
 ;; lists of different outline items
-(defvar-local module-list nil)
 (defvar-local value-list nil)
 (defvar-local type-list nil)
-(defvar-local class-list nil)
 (defvar-local exception-list nil)
-(defvar-local constructor-list nil)
-(defvar-local label-list nil)
-(defvar-local misc-list nil)
 
-(defun compute-pos (line col)
+(defun merlin-imenu-compute-position (line col)
   "Get location of the item."
   (save-excursion
     (condition-case nil

;;; merlin-imenu.el --- Merlin and imenu integration.   -*- coding: utf-8 -*-
;; Licensed under the MIT license.

;; Author: tddsg
;; Created: 10 July 2016
;; Updated: 27 April 2017
;; Version: 0.2
;; Keywords: ocaml, imenu, merlin
;; URL:


          (point))
      (error -1))))

(defun merlin-imenu-create-entry (prefix name type kind line col)
  (let* ((name (concat prefix name))
         (type (cond ((not (string= kind "Value")) "null")
                     ((not (string= type "null")) type)
                     (t (let* ((types (merlin/call
                                       "type-enclosing"
                                       "-position" (format "%d:%d" line col)
                                       "-expression" name)))
                          (cdr (nth 3 (car types)))))))
         (type (replace-regexp-in-string "\n" " " type))
         (type (propertize type 'face 'font-lock-doc-face)))
    (if (string= type "null") name (concat name " : " type))))

(defun merlin-imenu-parse-outline-item (prefix item)
  "Parse one item of the outline tree."
  (let* ((line (cdr (assoc 'line (assoc 'start item))))
         (col (cdr (assoc 'col (assoc 'start item))))
         (name (cdr (assoc 'name item)))
         (kind (cdr (assoc 'kind item)))
         (type (cdr (assoc 'type item)))
         (sub-trees (cdr (assoc 'children item)))
         (entry (merlin-imenu-create-entry prefix name type kind line col))
         (position (merlin-imenu-compute-position line col))
         (marker (cons entry (set-marker (make-marker) position))))
    (cond ((string= kind "Value")
           (setq value-list (cons marker value-list)))
          ((string= kind "Type")
           (setq type-list (cons marker type-list)))
          ((string= kind "Exn")
           (setq exception-list (cons marker exception-list))))
    (if (and (listp sub-trees) (not (null sub-trees)))
        (merlin-imenu-parse-outline-tree (concat prefix entry ".") sub-trees))))

(defun merlin-imenu-parse-outline-tree (prefix outline)
  "Parse outline tree."
  (when (not (null outline))
    (merlin-imenu-parse-outline-item prefix (car outline))
    (merlin-imenu-parse-outline-tree prefix (cdr outline))))

(defun merlin-imenu-create-index ()
  "Create data for imenu using the merlin outline feature."
  (interactive)
  ;; Reset local vars
  (setq value-list nil
        type-list nil
        exception-list nil)
  ;; Read outline tree
  (merlin-imenu-parse-outline-tree "" (merlin/call "outline"))
  (let ((index ()))
    (when value-list (push (cons "Value" value-list) index))
    (when exception-list (push (cons "Exception" exception-list) index))
    (when type-list (push (cons "Type" type-list) index))
    index))

;; enable Merlin to use the merlin-imenu module
