 	(line-no           (current-line-no))
         (column-no         (current-column-no))
 	(buffer (current-buffer)))
-    (if (buffer-modified-p buffer) (message-box "Buffer has been changed")
+    (if (buffer-modified-p buffer) (message-box "The current buffer has been changed, please save it first.")
       	(if (yes-or-no-p "Only check the current buffer?")
 	  (erl-spawn
 	    (erl-send-rpc wrangler-erl-node 'wrangler_code_inspector 'long_functions_in_file(list current-file-name lines wrangler-search-paths tab-width))



(defun fold_expr_by_name(buffer current-file-name module-name function-name arity clause-index)
    (erl-spawn
    (erl-send-rpc wrangler-erl-node 'wrangler_distel 'fold_expr_by_name(list current-file-name module-name function-name arity clause-index wrangler-search-paths tab-width))
    (erl-receive (buffer current-file-name)
	((['rex ['badrpc rsn]]
	  (message "Refactoring failed: %S" rsn))
	 (['rex ['error rsn]]
	  (message "Refactoring failed: %s" rsn))
	 (['rex ['ok candidates]]
	  (with-current-buffer buffer
	    (progn (while (not (equal candidates nil))
		     (setq reg (car candidates))
		     (setq line1 (elt reg 0))
		     (setq col1  (elt  reg 1))
		     (setq line2 (elt reg 2))
		     (setq col2  (elt  reg 3))
		     (setq funcall (elt reg 4))
		     (setq fundef (elt reg 5))
		     (highlight-region line1 col1 line2  col2 buffer)
		     (if (yes-or-no-p "Would you like to fold this expression? ")
			 (progn (erl-spawn (erl-send-rpc wrangler-erl-node 'refac_fold_expression
							 'fold_expression_1(list current-file-name line1 col1 line2 col2 funcall fundef wrangler-search-paths tab-width))
				  (erl-receive (buffer highlight-region-overlay current-file-name)
				      ((['rex ['badrpc rsn]]
					(delete-overlay highlight-region-overlay)
					(message "Refactoring failed: %s" rsn))
				       (['rex ['error rsn]]
					(delete-overlay highlight-region-overlay)
					(message "Refactoring failed: %s" rsn))			     
				       (['rex ['ok candidates1]]
					(with-current-buffer buffer (revert-buffer nil t t)
							     (if (not (equal candidates1 nil))
								 (progn (highlight-folding-candidates current-file-name candidates1 buffer highlight-region-overlay)
									(delete-overlay highlight-region-overlay))
							       (delete-overlay highlight-region-overlay))))
				       )))
				(setq candidates nil))
		       (setq candidates (cdr candidates))))
		   (revert-buffer nil t t)
		   (delete-overlay highlight-region-overlay)
		   (message "Refactoring succeeded."))))))))


  
(defun fold_expr_by_loc(buffer current-file-name line-no column-no)
  (erl-spawn
    (erl-send-rpc wrangler-erl-node 'wrangler_distel 'fold_expr_by_loc(list current-file-name line-no column-no wrangler-search-paths tab-width))
    (erl-receive (buffer current-file-name)
	((['rex ['badrpc rsn]]
	  (message "Refactoring failed: %S" rsn))
	 (['rex ['error rsn]]
	  (message "Refactoring failed: %s" rsn))
	 (['rex ['ok candidates]]
	  (with-current-buffer buffer
	    (progn (while (not (equal candidates nil))
		     (setq reg (car candidates))
		     (setq line1 (elt reg 0))
		     (setq col1  (elt  reg 1))
		     (setq line2 (elt reg 2))
		     (setq col2  (elt  reg 3))
		     (setq funcall (elt reg 4))
		     (setq fundef (elt reg 5))
		     (highlight-region line1 col1 line2  col2 buffer)
		     (if (yes-or-no-p "Would you like to fold this expression? ")
			 (progn (erl-spawn (erl-send-rpc wrangler-erl-node 'refac_fold_expression
							 'fold_expression_1(list current-file-name line1 col1 line2 col2 funcall fundef wrangler-search-paths tab-width))
				  (erl-receive (buffer highlight-region-overlay current-file-name)
				      ((['rex ['badrpc rsn]]
					(delete-overlay highlight-region-overlay)
					(message "Refactoring failed: %s" rsn))
				       (['rex ['error rsn]]
					(delete-overlay highlight-region-overlay)
					(message "Refactoring failed: %s" rsn))			     
				       (['rex ['ok candidates1]]
					(with-current-buffer buffer (revert-buffer nil t t)
							     (if (not (equal candidates1 nil))
								 (progn (highlight-folding-candidates current-file-name candidates1 buffer highlight-region-overlay)
									(delete-overlay highlight-region-overlay))
							       (delete-overlay highlight-region-overlay))))
				       )))
				(setq candidates nil))
		       (setq candidates (cdr candidates))))
		   (revert-buffer nil t t)
		   (delete-overlay highlight-region-overlay)
		   (message "Refactoring succeeded."))))))))



(defun highlight-folding-candidates(current-file-name candidates buffer highlight-region-overlay)
  "highlight the found candidate expressions one by one"
  (while (not (equal candidates nil))
    (setq reg (car candidates))
    (setq line1 (elt reg 0))
    (setq col1  (elt  reg 1))
    (setq line2 (elt reg 2))
    (setq col2  (elt  reg 3))
    (setq funcall (elt reg 4))
    (setq fundef (elt reg 5))
    (highlight-region line1 col1 line2  col2 buffer)
    (if (yes-or-no-p "Would you like to fold this expression? ")
	(progn (erl-spawn (erl-send-rpc wrangler-erl-node 'refac_fold_expression 'fold_expression_1(list current-file-name line1 col1
										 line2 col2 funcall fundef wrangler-search-paths tab-width))
		 (erl-receive (buffer highlight-region-overlay current-file-name)
		     ((['rex ['badrpc rsn]]
		       (delete-overlay highlight-region-overlay)
		       (message "Refactoring failed: %s" rsn))
		      (['rex ['error rsn]]
		       (delete-overlay highlight-region-overlay)
		       (message "Refactoring failed: %s" rsn))
		      (['rex ['ok candidates1]]
		       (with-current-buffer buffer (revert-buffer nil t t)
					    (if (not (equal candidates1 nil))
						(progn (highlight-folding-candidates current-file-name candidates1 buffer highlight-region-overlay)
						       (delete-overlay highlight-region-overlay))
					      (delete-overlay highlight-region-overlay)))))))
	       (setq candidates nil))    
      (setq candidates (cdr candidates)) 
    (with-current-buffer buffer (revert-buffer nil t t)
			 (delete-overlay highlight-region-overlay))
    ;;(message "Refactoring finished.")
    ))
    (message "Refactoring succeeded.")
    )
      

(defun erl-refactor-tuple-to-record(start end)
  "From tuple to record representation."

	(line-no           (current-line-no))
        (column-no         (current-column-no))
	(buffer (current-buffer)))
    (if (buffer-modified-p buffer) (message-box "The current buffer has been changed, please save it first.")
      (if (yes-or-no-p "Only check the current buffer?")
	  (erl-spawn
	    (erl-send-rpc wrangler-erl-node 'wrangler_code_inspector 'nested_case_exprs_in_file(list current-file-name level wrangler-search-paths tab-width))
