 			(message "Refactoring failed: %s" rsn))
 		       (['rex ['ok modified]]
 			(preview-commit-cancel current-file-name modified nil)
-			(with-current-buffer (get-file-buffer current-file-name)
+			(with-current-buffer (get-file-buffer-1 current-file-name)
 			  (goto-line line-no)
 			  (goto-column column-no))))))))
 	    ))))))

                                    (?n n "Answer n not to change this candidate;")
                                    (?Y Y "Answer Y to fold all the remaining candidates;")
                                    (?N N "Answer N to fold none of remaining candidates")))))
      (cond ((eq answer 'y)
             (setq candidates (cdr candidates)))
            ((eq answer 'n)
             (setq candidates-not-to-change  (cons new-cand candidates-not-to-change))
             (setq candidates (cdr candidates)))
            ((eq answer 'Y)
             (setq candidates nil))
            ((eq answer 'N)
             (setq candidates-not-to-change  (append candidates candidates-not-to-change))
             (setq candidates nil)))))
  (org-delete-overlay highlight-region-overlay)

	       (['rex ['ok modified]]
		 (progn
		   (preview-commit-cancel current-file-name modified nil)
		   (with-current-buffer (get-file-buffer-1 current-file-name)
		     (goto-line start-line-no)
		     (goto-column start-column-no))))
	       )))
       (message "Refactoring aborted."))))

(defun erl-refactor-move-fun-composite(args)
  "Move a function specified by mfa to another module."
  (interactive)
  (erl-spawn
    (erl-send-rpc wrangler-erl-node 'wrangler_refacs 'try_refactoring
                  (list 'refac_move_fun 'move_fun_by_name args))
    (erl-receive (args)
        ((['rex ['ok current-file-name line-no col-no target-file-name search-paths]]
          (erl-refactor-move-fun-1 current-file-name line-no col-no target-file-name 
                                   search-paths tab-width 'composite_emacs))
         (['rex result]
          (process-result (car args) result 0 0 t))
         ))))
        
(defun erl-refactor-move-fun (name)
  "Move a function definition from one module to another."
  (interactive (list (read-string "Target Module name: ")))
  (let ((current-file-name (buffer-file-name))
	(line-no           (current-line-no))
        (column-no         (current-column-no))
	(buffer (current-buffer)))
    (if (buffers-saved)
        (erl-refactor-move-fun-1 current-file-name line-no column-no name wrangler-search-paths tab-width 'emacs)
      (message "Refactoring aborted."))))

(defun erl-refactor-move-fun-1 (current-file-name line-no column-no name search-path tab-width editor)
  "Move a function definition from one module to another."
  (setq composite-refac-p (equal editor 'composite_emacs))
  (erl-spawn
    (erl-send-rpc wrangler-erl-node 'emacs_refac 'move_fun
                  (list current-file-name line-no column-no name search-paths tab-width editor))
    (erl-receive (line-no column-no  name current-file-name editor)
        ((['rex ['question msg]]
          (progn 
            (if (y-or-n-p msg)
                (erl-spawn
                  (erl-send-rpc wrangler-erl-node 'refac_move_fun 'move_fun_1 
                                (list current-file-name line-no column-no name 'true 
                                      search-paths tab-width editor))
                  (erl-receive (name line-no column-no current-file-name editor)
                      ((['rex ['warning msg]]
                        (progn 
                          (if (y-or-n-p msg)
                              (erl-spawn
                                (erl-send-rpc wrangler-erl-node 'refac_move_fun 'move_fun_1 
                                              (list current-file-name line-no column-no name 'false 
                                                    search-paths tab-width editor))
                                (erl-receive (line-no column-no current-file-name)
                                    ((['rex result]
                                      (process-result current-file-name result line-no column-no composite-refac-p))
                                     ))))
                          (process-result current-file-name (list 'abort) line-no column-no composite-refac-p)
                         ))
                       (['rex result]
                        (process-result current-file-name result line-no column-no composite-refac-p)
                        ))))
               (process-result current-file-name (list 'abort) line-no column-no composite-refac-p)
               )))
         (['rex ['warning msg]]
          (if (y-or-n-p msg)
              (erl-spawn
                (erl-send-rpc wrangler-erl-node 'refac_move_fun 'move_fun_1 
                              (list current-file-name line-no column-no name 'false 
                                    search-paths tab-width editor))
                (erl-receive (line-no column-no current-file-name)
                    ((['rex result]
                        (process-result current-file-name result line-no column-no composite-refac-p))
                     )))
            (process-result current-file-name (list 'abort) line-no column-no composite-refac-p)
            ))
         (['rex result]
          (process-result current-file-name result line-no column-no composite-refac-p))
         ))))
    
(defun process-result(current-file-name result line-no column-no composite-refac-p)
  "process the result return by refactoring"
  (if composite-refac-p 
      (cond ((equal (elt result 0) 'badrpc)
             (setq rsn (elt result 1))
             (message "Refactoring failed: %S" rsn)
             (apply-refac-cmds current-file-name (list 'error rsn)))
            ((equal (elt result 0) 'error)        
             (setq rsn (elt result 1))
             (message "Refactoring failed: %S" rsn)
             (apply-refac-cmds current-file-name (list 'error rsn)))
            ((equal (elt result 0) 'ok)
             (setq modified (elt result 1))
             (setq renamed (if (> (length result) 3)
                               (elt result 2)
                             nil))
             (setq warning (if (> (length result) 3)
                               (elt result 3)
                             (if (> (length result) 2)
                                 (elt result 2)
                               nil)))
             (if warning
                 (setq has-warning warning)
               nil)
             (message "Refactoring succeeded %s" modified)
             ;;(update-buffers modified)
             (revert-all-buffers)
             (apply-refac-cmds current-file-name (list 'ok modified)))
            ((and (sequencep (elt result 0)) (equal (elt (elt result 0) 0) 'ok))
             (setq modified (elt (elt result 0) 1))
             (setq name_change (elt result 1))
             (message "Refactoring succeeded %s" modified)
             ;;(update-buffers modified)
             (revert-all-buffers)
             (apply-refac-cmds current-file-name (list (list 'ok modified) name_change)))
            (t
             (revert-all-buffers)
             (message "Unexpected result: %s" result))
            )
    (cond ((equal (elt result 0) 'ok)
           (setq modified (if (> (length result) 1)
                              (elt result 1)
                            nil))
             (setq renamed (if (> (length result) 3)
                               (elt result 2)
                             nil))
             (setq warning (if (> (length result) 3)
                               (elt result 3)
                             (if (> (length result) 2)
                                 (elt result 2)
                               nil)))
             (if warning
               (setq has-warning warning)
             nil)
           (if (equal modified nil)
               (message "Refactoring finished, and no file has been changed.")
             (preview-commit-cancel current-file-name modified renamed)
             (if (not (eq line-no 0))
               (with-current-buffer (get-file-buffer-1 current-file-name)
                 (goto-line line-no)
                 (goto-column column-no))
               nil)))
          ((equal (elt result 0) 'error)       
           (setq rsn (elt result 1))
           (message "Refactoring failed: %S" rsn))
          ((equal (elt result 0) 'badrpc)
           (setq rsn (elt result 1))
           (message "Refactoring failed: %S" rsn))
          ((equal result ['abort])
           (message "Refactoring aborted.")))))
          
                               

;; redefined get-file-buffer to handle the difference between
;; unix and windows filepath seperator.
