diff --git a/scripts/emacs/refactoring-browser/refactoring.el b/scripts/emacs/refactoring-browser/refactoring.el
index 735fe7e7c54f20491b69b634dc4bf93df2a023f3..7795272a67a58d0342194f4cc510461d7651e98d 100644
--- a/scripts/emacs/refactoring-browser/refactoring.el
+++ b/scripts/emacs/refactoring-browser/refactoring.el
@@ -6,6 +6,7 @@
   (let (
 				(current-dir (file-name-directory (symbol-file 'refactoring-extract-method)))
 				(file-name (buffer-file-name (current-buffer)))
+				(point (region-beginning))
 				(start-line (line-number-at-pos (region-beginning)))
 				(end-line (line-number-at-pos (region-end)))
 				(refactoring-command)
@@ -19,16 +20,30 @@
 
 		(shell-command 
 		 refactoring-command
-		 (switch-to-buffer refactoring-buffer)
+		 refactoring-buffer
 		 )
 
 		(if (y-or-n-p "Apply refactoring ?")
-				(progn 
-					(shell-command
-					 (concat refactoring-command "|patch -p1")))
-		)
+			(progn 
+				(switch-to-buffer patched-file-buffer)
+				(erase-buffer)
+				(shell-command
+				 (concat refactoring-command "|patch -p1 -o -")	 patched-file-buffer refactoring-buffer)
+				(goto-char point)
+				)
+			)
 
-		(kill-buffer)
-		(revert-buffer)
+		(kill-buffer refactoring-buffer)
+		(switch-to-buffer patched-file-buffer)
+		)
 	)
-)
+
+
+(defun refactoring-revert-buffer-keep-undo(buffer)
+	"Revert buffer but keep undo history."   
+	(switch-to-buffer buffer)
+	(let ((inhibit-read-only t))
+		(erase-buffer)
+		(insert-file-contents (buffer-file-name))
+		(set-visited-file-modtime (visited-file-modtime))
+		(set-buffer-modified-p nil)))