 	    (message "Refactoring failed: %s" rsn))
 	   (['rex ['ok refac-rename]]
 	    (with-current-buffer buffer (revert-buffer nil t t))
-            (message "Refactoring succeeded!")))))))+            (message "Refactoring succeeded!")))))))

	       (message "Refactoring succeeded!")))))))


(defun erl-refactor-rename-mod (node name)
  "Rename the current module name."
  (interactive (list (erl-target-node)

  (delete-overlay highlight-region-overlay)
  )

(defun erl-refactor-instrument-prog (node)
  "Instrument an Erlang program to trace process communication."
  (interactive (list (erl-target-node)))
  (let ((current-file-name (buffer-file-name))
	(buffer (current-buffer)))
    (erl-spawn
      (erl-send-rpc node 'wrangler_distel 'instrument_prog(list current-file-name erlang-refac-search-paths))
      (erl-receive (buffer)
	  ((['rex ['badrpc rsn]]
	    (message "Refactoring failed: %S" rsn))
	   (['rex ['error rsn]]
	    (message "Refactoring failed: %s" rsn))
	   (['rex ['ok modified]]
	    (with-current-buffer buffer
	       (dolist (f modified)
		 (let ((buffer (get-file-buffer f)))
		   (if buffer (with-current-buffer buffer (revert-buffer nil t t))
		     ;;(message-box (format "modified unopened file: %s" f))))))
		     nil))))
	       (message "Refactoring succeeded!")))))))

(defun erl-refactor-uninstrument-prog (node)
  "Uninstrument an Erlang program to remove the code added by Wrangler to trace process communication."
  (interactive (list (erl-target-node)))
  (let ((current-file-name (buffer-file-name))
	(buffer (current-buffer)))
    (erl-spawn
      (erl-send-rpc node 'wrangler_distel 'uninstrument_prog(list current-file-name erlang-refac-search-paths))
      (erl-receive (buffer)
	  ((['rex ['badrpc rsn]]
	    (message "Refactoring failed: %S" rsn))
	   (['rex ['error rsn]]
	    (message "Refactoring failed: %s" rsn))
	   (['rex ['ok modified]]
	    (with-current-buffer buffer
	       (dolist (f modified)
		 (let ((buffer (get-file-buffer f)))
		   (if buffer (with-current-buffer buffer (revert-buffer nil t t))
		     ;;(message-box (format "modified unopened file: %s" f))))))
		     nil))))
	       (message "Refactoring succeeded!")))))))


(defun erl-refactor-add-a-tag (node name)
  "Add a tag to the messages received by a process."
  (interactive (list (erl-target-node)
		     (read-string "Tag to add: ")
		     ))
  (let ((current-file-name (buffer-file-name))
	(line-no           (current-line-no))
        (column-no         (current-column-no))
	(buffer (current-buffer)))       
    (erl-spawn
      (erl-send-rpc node 'wrangler_distel 'add_a_tag(list current-file-name line-no column-no name erlang-refac-search-paths))
      (erl-receive (node buffer name 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 (revert-buffer nil t t) 
	      (while (not (equal candidates nil))
		(setq send (car candidates))
		(setq mod (elt send 0))
		(setq fun (elt send 1))
		(setq arity (elt send 2))
		(setq index (elt send 3))
		(erl-spawn
		  (erl-send-rpc node 'refac_add_a_tag 'send_expr_to_region(list current-file-name mod fun arity index))
		  (erl-receive (node buffer current-file-name name)
		      ((['rex ['badrpc rsn]]
			;;  (setq candidates nil)
			(message "Refactoring failed: %s" rsn))					  
		       (['rex ['error rsn]]
			;;  (setq candidates nil)
			(message "Refactoring failed: %s" rsn))
		       (['rex ['ok region]]
			(with-current-buffer buffer 
			(progn (setq line1 (elt region 0))
			       (setq col1 (elt region 1))
			       (setq line2 (elt region 2))
			       (setq col2 (elt region 3))
			       (highlight-region line1 (- col1 1) line2  col2 buffer)
			       (if (yes-or-no-p "Should a tag be added to this expression? ")
				   (erl-spawn (erl-send-rpc node 'refac_add_a_tag 'add_a_tag(list current-file-name name line1 col1 line2 col2))
				     (erl-receive (buffer)
					 ((['rex ['badrpc rsn]]
					   (message "Refactoring failed: %s" rsn))
					  (['rex ['error rsn]]
					   (message "Refactoring failed: %s" rsn))
					  (['rex ['ok res]]
					   (with-current-buffer buffer (revert-buffer nil t t)
						(delete-overlay highlight-region-overlay))
					  ))))
				(delete-overlay highlight-region-overlay)
			       )))))))
		(setq candidates (cdr candidates)))
	      (with-current-buffer buffer (revert-buffer nil t t))
	      ;; (delete-overlay highlight-region-overlay)
	      (message "Refactoring succeeded!"))))))))
  

 
 ;; End of modification by H.Li
;;---------------------------------------------------------------------
