 ;; chosen to keep it since it provides a very useful functionality
 ;; which is not possible to achieve using a clean approach.
 ;;   / AndersL
+;;
+;; The previous hack seems to have broken in Emacs 25. This is fixed
+;; by trying to find the function in the man buffer a few times with a
+;; delay in between (see function
+;; erlang-man-repeated-search-for-function). This fix is also a hack
+;; but it should be quite robust and will work even if the function
+;; that erlang-man-function-display-man-page tries to hook into
+;; disappears or changes.
 
 (defvar erlang-man-function-name nil
   "Name of function for last `erlang-man-function' call.

    (when (or (null modname) (string= modname ""))
      (error "No Erlang module name given"))
    (cond ((fboundp 'Man-notify-when-ready)
           ;; Emacs 19:  The man command could possibly start an
           ;; asynchronous process, i.e. we must hook ourselves into
           ;; the system to be activated when the man-process
           ;; terminates.

    "Set point at the documentation of the function name in
`erlang-man-function-name' when the man page is displayed."
    (if erlang-man-function-name
        (erlang-man-repeated-search-for-function (ad-get-arg 0)
                                                 erlang-man-function-name)
      (setq erlang-man-function-name nil))))




(defun erlang-man-find-function (buf func &optional module-name)
  "Find manual page for function `erlang-man-function-name' in buffer BUF.
The function will automatically try to find the correct buffer among the
opened buffers if BUF is nil. The optional parameter MODULE-NAME will make
the search for the buffer more accurate."
  (let ((buffer (or buf
                    (progn
                      ; find buffer containing man page
                      (require 'cl-lib)
                      (car (cl-remove-if-not (lambda (buf)
                                               (string-match
                                                (or module-name "")
                                                (format "%s" buf)))
                                             (cl-remove-if-not
                                              (lambda (buf)
                                                (string-match
                                                 "[Mm][Aa][Nn]"
                                                 (format "%s" buf)))
                                              (buffer-list))))))))
    (if (and func buffer)
        (let ((win (get-buffer-window buffer)))
          (if win
              (progn
                (set-buffer buffer)
                (goto-char (point-min))
                (if (re-search-forward
                     (concat "^[ \t]*\\([a-z0-9_]*[ \t]*:\\)?[ \t]*" func "[ \t]*([A-Za-z0-9 \t:,_()]*)[ \t]*->")
                     (point-max) t)
                    (progn
                      (forward-word -1)
                      (set-window-point win (point))
                      (message "Found documentation for function `%s'" func)
                      t)
                  (if (re-search-forward
                       (concat "^[ \t]*\\([a-z0-9_]*[ \t]*:\\)?[ \t]*" func "[ \t]*\(")
                       (point-max) t)
                      (progn
                        (forward-word -1)
                        (set-window-point win (point))
                        (message "Found documentation for function `%s'" func)
                        t)
                    (progn
                      (message "Could not find function `%s'" func)
                      nil)))))))))

(defvar erlang-man-file-regexp
  "\\(.*\\)/man[^/]*/\\([^.]+\\)\\.\\([124-9]\\|3\\(erl\\)?\\)\\(\\.gz\\)?$")
