Index: src/wp-admin/admin-ajax.php
===================================================================
--- src/wp-admin/admin-ajax.php	(revision 25335)
+++ src/wp-admin/admin-ajax.php	(working copy)
@@ -49,7 +49,7 @@
 	'oembed-cache', 'image-editor', 'delete-comment', 'delete-tag', 'delete-link',
 	'delete-meta', 'delete-post', 'trash-post', 'untrash-post', 'delete-page', 'dim-comment',
 	'add-link-category', 'add-tag', 'get-tagcloud', 'get-comments', 'replyto-comment',
-	'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'autosave', 'closed-postboxes',
+	'edit-comment', 'add-menu-item', 'add-meta', 'add-user', 'closed-postboxes',
 	'hidden-columns', 'update-welcome-panel', 'menu-get-metabox', 'wp-link-ajax',
 	'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
 	'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
Index: src/wp-admin/edit-form-advanced.php
===================================================================
--- src/wp-admin/edit-form-advanced.php	(revision 25335)
+++ src/wp-admin/edit-form-advanced.php	(working copy)
@@ -347,7 +347,6 @@
 
 echo $form_extra;
 
-wp_nonce_field( 'autosave', 'autosavenonce', false );
 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
 ?>
Index: src/wp-admin/includes/ajax-actions.php
===================================================================
--- src/wp-admin/includes/ajax-actions.php	(revision 25335)
+++ src/wp-admin/includes/ajax-actions.php	(working copy)
@@ -1042,69 +1042,6 @@
 	$x->send();
 }
 
-function wp_ajax_autosave() {
-	define( 'DOING_AUTOSAVE', true );
-
-	check_ajax_referer( 'autosave', 'autosavenonce' );
-
-	$_POST['post_category'] = explode(",", $_POST['catslist']);
-	if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
-		unset($_POST['post_category']);
-
-	$data = '';
-	$supplemental = array();
-	$id = $revision_id = 0;
-
-	$post_id = (int) $_POST['post_id'];
-	$_POST['ID'] = $_POST['post_ID'] = $post_id;
-	$post = get_post($post_id);
-	if ( 'auto-draft' == $post->post_status )
-		$_POST['post_status'] = 'draft';
-
-	if ( 'page' == $post->post_type ) {
-		if ( !current_user_can('edit_page', $post->ID) )
-			wp_die( __( 'You are not allowed to edit this page.' ) );
-	} else {
-		if ( !current_user_can('edit_post', $post->ID) )
-			wp_die( __( 'You are not allowed to edit this post.' ) );
-	}
-
-	if ( ! empty( $_POST['autosave'] ) ) {
-		if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
-			// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
-			$id = edit_post();
-		} else {
-			// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
-			$revision_id = wp_create_post_autosave( $post->ID );
-			if ( is_wp_error($revision_id) )
-				$id = $revision_id;
-			else
-				$id = $post->ID;
-		}
-
-		if ( ! is_wp_error($id) ) {
-			/* translators: draft saved date format, see http://php.net/date */
-			$draft_saved_date_format = __('g:i:s a');
-			/* translators: %s: date and time */
-			$data = sprintf( __('Draft saved at %s.'), date_i18n( $draft_saved_date_format ) );
-		}
-	} else {
-		if ( ! empty( $_POST['auto_draft'] ) )
-			$id = 0; // This tells us it didn't actually save
-		else
-			$id = $post->ID;
-	}
-
-	// @todo Consider exposing any errors, rather than having 'Saving draft...'
-	$x = new WP_Ajax_Response( array(
-		'what' => 'autosave',
-		'id' => $id,
-		'data' => $data,
-		'supplemental' => $supplemental
-	) );
-	$x->send();
-}
-
 function wp_ajax_closed_postboxes() {
 	check_ajax_referer( 'closedpostboxes', 'closedpostboxesnonce' );
 	$closed = isset( $_POST['closed'] ) ? explode( ',', $_POST['closed']) : array();
Index: src/wp-admin/includes/misc.php
===================================================================
--- src/wp-admin/includes/misc.php	(revision 25335)
+++ src/wp-admin/includes/misc.php	(working copy)
@@ -650,7 +650,6 @@
 		if ( 2 === wp_verify_nonce( $received['post_nonce'], 'update-post_' . $post_id ) ) {
 			$response['wp-refresh-post-nonces'] = array(
 				'replace' => array(
-					'autosavenonce' => wp_create_nonce('autosave'),
 					'getpermalinknonce' => wp_create_nonce('getpermalink'),
 					'samplepermalinknonce' => wp_create_nonce('samplepermalink'),
 					'closedpostboxesnonce' => wp_create_nonce('closedpostboxes'),
@@ -665,3 +664,29 @@
 	return $response;
 }
 add_filter( 'heartbeat_received', 'wp_refresh_post_nonces', 10, 3 );
+
+/**
+ * Autosave with heartbeat
+ *
+ * @since 3.7
+ */
+function wp_autosave( $response, $data ) {
+	if ( ! empty( $data['autosave'] ) ) {
+		$saved = autosave( $data['autosave'] );
+
+		if ( is_wp_error( $saved ) ) {
+			$response['autosave'] = array( 'success' => false, 'message' => $saved->get_error_message() );
+		} elseif ( empty( $saved ) ) {
+			$response['autosave'] = array( 'success' => false, 'message' => __( 'Error while saving the draft.' ) );
+		} else {
+			/* translators: draft saved date format, see http://php.net/date */
+			$draft_saved_date_format = __( 'g:i:s a' );
+			/* translators: %s: date and time */
+			$response['autosave'] = array( 'success' => true, 'message' => sprintf( __( 'Draft saved at %s.' ), date_i18n( $draft_saved_date_format ) ) );
+		}
+	}
+
+	return $response;
+}
+// Run later as we have to set DOING_AUTOSAVE for back-compat
+add_filter( 'heartbeat_received', 'wp_autosave', 50, 2 );
Index: src/wp-admin/includes/post.php
===================================================================
--- src/wp-admin/includes/post.php	(revision 25335)
+++ src/wp-admin/includes/post.php	(working copy)
@@ -79,9 +79,14 @@
 		}
 	}
 
-	if ( ! empty( $post_data['post_status'] ) )
+	if ( ! empty( $post_data['post_status'] ) ) {
 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
 
+		// No longer an auto-draft
+		if ( 'auto-draft' == $post_data['post_status'] )
+			$post_data['post_status'] = 'draft';
+	}
+
 	// What to do based on which button they pressed
 	if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
 		$post_data['post_status'] = 'draft';
@@ -181,9 +186,6 @@
 	$post_data = _wp_translate_postdata( true, $post_data );
 	if ( is_wp_error($post_data) )
 		wp_die( $post_data->get_error_message() );
-	if ( ( empty( $post_data['action'] ) || 'autosave' != $post_data['action'] ) && 'auto-draft' == $post_data['post_status'] ) {
-		$post_data['post_status'] = 'draft';
-	}
 
 	if ( isset($post_data['visibility']) ) {
 		switch ( $post_data['visibility'] ) {
@@ -1312,22 +1314,30 @@
  * @uses _wp_translate_postdata()
  * @uses _wp_post_revision_fields()
  *
- * @return unknown
+ * @param mixed $post_data Associative array containing the post data. Suppotrs passing the post ID for backwards compatibility.
+ * @return mixed The autosave revision ID. WP_Error or 0 on error.
  */
-function wp_create_post_autosave( $post_id ) {
-	$translated = _wp_translate_postdata( true );
-	if ( is_wp_error( $translated ) )
-		return $translated;
+function wp_create_post_autosave( $post_data ) {
+	if ( is_numeric( $post_data ) ) {
+		$post_id = $post_data;
+		$post_data = &$_POST;
+	} else {
+		$post_id = (int) $post_data['post_ID'];
+	}
 
+	$post_data = _wp_translate_postdata( true, $post_data );
+	if ( is_wp_error( $post_data ) )
+		return $post_data;
+
 	$post_author = get_current_user_id();
 
 	// Store one autosave per author. If there is already an autosave, overwrite it.
 	if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
-		$new_autosave = _wp_post_revision_fields( $_POST, true );
+		$new_autosave = _wp_post_revision_fields( $post_data, true );
 		$new_autosave['ID'] = $old_autosave->ID;
 		$new_autosave['post_author'] = $post_author;
 
-		// If the new autosave is the same content as the post, delete the old autosave.
+		// If the new autosave has the same content as the post, delete the autosave.
 		$post = get_post( $post_id );
 		$autosave_is_different = false;
 		foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
@@ -1339,7 +1349,7 @@
 
 		if ( ! $autosave_is_different ) {
 			wp_delete_post_revision( $old_autosave->ID );
-			return;
+			return 0;
 		}
 
 		return wp_update_post( $new_autosave );
@@ -1346,7 +1356,7 @@
 	}
 
 	// _wp_put_post_revision() expects unescaped.
-	$post_data = wp_unslash( $_POST );
+	$post_data = wp_unslash( $post_data );
 
 	// Otherwise create the new autosave as a special post revision
 	return _wp_put_post_revision( $post_data, true );
@@ -1372,58 +1382,82 @@
 function post_preview() {
 
 	$post_ID = (int) $_POST['post_ID'];
-	$status = get_post_status( $post_ID );
-	if ( 'auto-draft' == $status )
-		wp_die( __('Preview not available. Please save as a draft first.') );
+	$_POST['ID'] = $post_ID;
 
-	if ( isset($_POST['catslist']) )
-		$_POST['post_category'] = explode(",", $_POST['catslist']);
+	if ( ! $post = get_post( $post_ID ) )
+		wp_die( __('You attempted to preview a non existing item.') );
 
-	if ( isset($_POST['tags_input']) )
-		$_POST['tags_input'] = explode(",", $_POST['tags_input']);
+	if ( ! current_user_can( 'edit_post', $post->ID ) )
+		wp_die( __('You are not allowed to preview this item.') );
 
-	if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
-		unset($_POST['post_category']);
+	$is_autosave = false;
 
-	$_POST['ID'] = $post_ID;
-	$post = get_post($post_ID);
-
-	if ( 'page' == $post->post_type ) {
-		if ( ! current_user_can('edit_page', $post_ID) )
-			wp_die( __('You are not allowed to edit this page.') );
+	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
+		$saved_post_id = edit_post();
 	} else {
-		if ( ! current_user_can('edit_post', $post_ID) )
-			wp_die( __('You are not allowed to edit this post.') );
-	}
+		$is_autosave = true;
 
-	$user_id = get_current_user_id();
-	$locked = wp_check_post_lock( $post->ID );
-	if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) {
-		$id = edit_post();
-	} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
-		$id = wp_create_post_autosave( $post->ID );
-		if ( ! is_wp_error($id) )
-			$id = $post->ID;
+		if ( 'auto-draft' == $_POST['post_status'] )
+			$_POST['post_status'] = 'draft';
+
+		$saved_post_id = wp_create_post_autosave();
 	}
 
-	if ( is_wp_error($id) )
-		wp_die( $id->get_error_message() );
+	if ( is_wp_error( $saved_post_id ) )
+		wp_die( $saved_post_id->get_error_message() );
 
-	if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
-		$url = add_query_arg( 'preview', 'true', get_permalink($id) );
-	} else {
-		$nonce = wp_create_nonce('post_preview_' . $id);
-		$args = array(
-			'preview' => 'true',
-			'preview_id' => $id,
-			'preview_nonce' => $nonce,
-		);
+	$query_args = array( 'preview' => 'true' );
 
+	if ( $is_autosave && $saved_post_id ) {
+		$query_args['preview_id'] = $post->ID;
+		$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
+
 		if ( isset( $_POST['post_format'] ) )
-			$args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
-
-		$url = add_query_arg( $args, get_permalink($id) );
+			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
 	}
 
+	$url = add_query_arg( $query_args, get_permalink( $post->ID ) );
 	return apply_filters( 'preview_post_link', $url );
 }
+
+/**
+ * Save a post submitted with XHR
+ *
+ * Intended for use with heartbeat and autosave.js
+ *
+ * @since 3.7
+ *
+ * @param $post_data Associative array of the submitted post data.
+ * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
+ *               Te ID can be either the draft post_id or the autosave revision post_id.
+ */
+function autosave( $post_data ) {
+	// Back-compat
+	if ( ! defined( 'DOING_AUTOSAVE' ) )
+		define( 'DOING_AUTOSAVE', true );
+
+	$post_id = (int) $post_data['post_id'];
+	$post_data['ID'] = $post_data['post_ID'] = $post_id;
+
+	if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) )
+		return new WP_Error( 'invalid_nonce', __('ERROR: invalid post data.') );
+
+	$post = get_post( $post_id );
+
+	if ( ! current_user_can( 'edit_post', $post->ID ) )
+		return new WP_Error( 'edit_post', __('You are not allowed to edit this item.') );
+
+	if ( 'auto-draft' == $post->post_status )
+		$post_data['post_status'] = 'draft';
+
+	if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
+		$post_data['post_category'] = explode( ',', $post_data['catslist'] );
+
+	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
+		// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
+		return edit_post( $post_data );
+	} else {
+		// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
+		return wp_create_post_autosave( $post_data );
+	}
+}
Index: src/wp-admin/js/post.js
===================================================================
--- src/wp-admin/js/post.js	(revision 25335)
+++ src/wp-admin/js/post.js	(working copy)
@@ -1,4 +1,6 @@
-var tagBox, commentsBox, editPermalink, makeSlugeditClickable, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
+var tagBox, commentsBox, WPSetThumbnailHTML, WPSetThumbnailID, WPRemoveThumbnail, wptitlehint;
+// Prevent fatal errors (back-compat)
+makeSlugeditClickable = editPermalink = function(){};
 
 // return an array with any duplicate, whitespace or values removed
 function array_unique_noempty(a) {
@@ -265,10 +267,9 @@
 		send['lock'] = lock;
 
 	data['wp-refresh-post-lock'] = send;
-});
 
-// Post locks: update the lock string or show the dialog if somebody has taken over editing
-$(document).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
+}).on( 'heartbeat-tick.refresh-lock', function( e, data ) {
+	// Post locks: update the lock string or show the dialog if somebody has taken over editing
 	var received, wrap, avatar;
 
 	if ( data['wp-refresh-post-lock'] ) {
@@ -279,19 +280,16 @@
 			wrap = $('#post-lock-dialog');
 
 			if ( wrap.length && ! wrap.is(':visible') ) {
-				if ( typeof autosave == 'function' ) {
-					$(document).on('autosave-disable-buttons.post-lock', function() {
-						wrap.addClass('saving');
-					}).on('autosave-enable-buttons.post-lock', function() {
+				if ( typeof wp != 'undefined' && wp.autosave ) {
+					// Save the latest changes and disable
+					$(document).one( 'heartbeat-tick', function() {
+						wp.autosave.remote.disable();
 						wrap.removeClass('saving').addClass('saved');
-						window.onbeforeunload = null;
+						$(window).off( 'beforeunload.autosave' );
 					});
 
-					// Save the latest changes and disable
-					if ( ! autosave() )
-						window.onbeforeunload = null;
-
-					autosave = function(){};
+					wrap.addClass('saving');
+					wp.autosave.remote.triggerSave();
 				}
 
 				if ( received.lock_error.avatar_src ) {
@@ -306,6 +304,22 @@
 			$('#active_post_lock').val( received.new_lock );
 		}
 	}
+}).on( 'after-autosave.update-post-slug', function() {
+	// create slug area only if not already there
+	if ( ! $('#edit-slug-box > *').length ) {
+		$.post( ajaxurl, {
+				action: 'sample-permalink',
+				post_id: $('#post_ID').val(),
+				new_title: typeof fullscreen != 'undefined' && fullscreen.settings.visible ? $('#wp-fullscreen-title').val() : $('#title').val(),
+				samplepermalinknonce: $('#samplepermalinknonce').val()
+			},
+			function( data ) {
+				if ( data != '-1' ) {
+					$('#edit-slug-box').html(data);
+				}
+			}
+		);
+	}
 });
 
 }(jQuery));
@@ -351,7 +365,8 @@
 }(jQuery));
 
 jQuery(document).ready( function($) {
-	var stamp, visibility, sticky = '', last = 0, co = $('#content');
+	var stamp, visibility, sticky = '', last = 0, co = $('#content'), $editSlugWrap = $('#edit-slug-box'),
+		postId = $('#post_ID').val() || 0;
 
 	postboxes.add_postbox_toggles(pagenow);
 
@@ -680,9 +695,11 @@
 	} // end submitdiv
 
 	// permalink
-	if ( $('#edit-slug-box').length ) {
-		editPermalink = function(post_id) {
-			var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'), revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(), full = $('#editable-post-name-full').html();
+	if ( $editSlugWrap.length ) {
+		function editPermalink() {
+			var i, c = 0, e = $('#editable-post-name'), revert_e = e.html(), real_slug = $('#post_name'),
+				revert_slug = real_slug.val(), b = $('#edit-slug-buttons'), revert_b = b.html(),
+				full = $('#editable-post-name-full').html();
 
 			$('#view-post-btn').hide();
 			b.html('<a href="#" class="save button button-small">'+postL10n.ok+'</a> <a class="cancel" href="#">'+postL10n.cancel+'</a>');
@@ -689,11 +706,11 @@
 			b.children('.save').click(function() {
 				var new_slug = e.children('input').val();
 				if ( new_slug == $('#editable-post-name-full').text() ) {
-					return $('.cancel', '#edit-slug-buttons').click();
+					return $('#edit-slug-buttons .cancel').click();
 				}
 				$.post(ajaxurl, {
 					action: 'sample-permalink',
-					post_id: post_id,
+					post_id: postId,
 					new_slug: new_slug,
 					new_title: $('#title').val(),
 					samplepermalinknonce: $('#samplepermalinknonce').val()
@@ -707,13 +724,12 @@
 					}
 					b.html(revert_b);
 					real_slug.val(new_slug);
-					makeSlugeditClickable();
 					$('#view-post-btn').show();
 				});
 				return false;
 			});
 
-			$('.cancel', '#edit-slug-buttons').click(function() {
+			$('#edit-slug-buttons .cancel').click(function() {
 				$('#view-post-btn').show();
 				e.html(revert_e);
 				b.html(revert_b);
@@ -743,12 +759,13 @@
 			}).focus();
 		}
 
-		makeSlugeditClickable = function() {
-			$('#editable-post-name').click(function() {
-				$('#edit-slug-buttons').children('.edit-slug').click();
-			});
-		}
-		makeSlugeditClickable();
+		$editSlugWrap.on( 'click', function( event ) {
+			var $target = $( event.target );
+
+			if ( $target.is('#editable-post-name') || $target.hasClass('edit-slug') ) {
+				editPermalink();
+			}
+		});
 	}
 
 	// word count
Index: src/wp-admin/post.php
===================================================================
--- src/wp-admin/post.php	(revision 25335)
+++ src/wp-admin/post.php	(working copy)
@@ -296,7 +296,7 @@
 	break;
 
 case 'preview':
-	check_admin_referer( 'autosave', 'autosavenonce' );
+	check_admin_referer( 'update-post_' . $post_id );
 
 	$url = post_preview();
 
Index: src/wp-includes/js/autosave.js
===================================================================
--- src/wp-includes/js/autosave.js	(revision 25335)
+++ src/wp-includes/js/autosave.js	(working copy)
@@ -1,400 +1,102 @@
-var autosave, autosaveLast = '', autosavePeriodical, autosaveDelayPreview = false, notSaved = true, blockSave = false, fullscreen, autosaveLockRelease = true;
+window.wp = window.wp || {};
 
-jQuery(document).ready( function($) {
+( function( $, window ) {
+	var $document = $(document), _disabled, _blockSave, _blockSaveTimeout;
 
-	if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) {
-		autosaveLast = wp.autosave.getCompareString({
-			post_title : $('#title').val() || '',
-			content : switchEditors.pre_wpautop( $('#content').val() ) || '',
-			excerpt : $('#excerpt').val() || ''
-		});
-	} else {
-		autosaveLast = wp.autosave.getCompareString();
-	}
+/**
+ * Returns the data saved in both local and remote autosave
+ *
+ * @return mixed Object containing the post data or false
+ */
+wp.autosave = {
+	getPostData: function( type ) {
+		var post_name, parent_id, cats = [], data,
+			editor = typeof tinymce != 'undefined' && tinymce.get('content');
 
-	autosavePeriodical = $.schedule({time: autosaveL10n.autosaveInterval * 1000, func: function() { autosave(); }, repeat: true, protect: true});
-
-	//Disable autosave after the form has been submitted
-	$("#post").submit(function() {
-		$.cancel(autosavePeriodical);
-		autosaveLockRelease = false;
-	});
-
-	$('input[type="submit"], a.submitdelete', '#submitpost').click(function(){
-		blockSave = true;
-		window.onbeforeunload = null;
-		$(':button, :submit', '#submitpost').each(function(){
-			var t = $(this);
-			if ( t.hasClass('button-primary') )
-				t.addClass('button-primary-disabled');
-			else
-				t.addClass('button-disabled');
-		});
-		if ( $(this).attr('id') == 'publish' )
-			$('#major-publishing-actions .spinner').show();
-		else
-			$('#minor-publishing .spinner').show();
-	});
-
-	window.onbeforeunload = function(){
-		var editor = typeof(tinymce) != 'undefined' ? tinymce.activeEditor : false, compareString;
-
 		if ( editor && ! editor.isHidden() ) {
-			if ( editor.isDirty() )
-				return autosaveL10n.saveAlert;
-		} else {
-			if ( fullscreen && fullscreen.settings.visible ) {
-				compareString = wp.autosave.getCompareString({
-					post_title: $('#wp-fullscreen-title').val() || '',
-					content: $('#wp_mce_fullscreen').val() || '',
-					excerpt: $('#excerpt').val() || ''
-				});
+			// Don't run while the tinymce spellcheck is on. It resets all found words.
+			if ( editor.plugins.spellchecker && editor.plugins.spellchecker.active ) {
+				return false;
 			} else {
-				compareString = wp.autosave.getCompareString();
+				tinymce.triggerSave();
 			}
-
-			if ( compareString != autosaveLast )
-				return autosaveL10n.saveAlert;
 		}
-	};
 
-	$(window).unload( function(e) {
-		if ( ! autosaveLockRelease )
-			return;
+		data = {
+			post_id: $('#post_ID').val() || 0,
+			post_type: $('#post_type').val() || '',
+			post_author: $('#post_author').val() || '',
+			excerpt: $('#excerpt').val() || ''
+		};
 
-		// unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload.
-		if ( e.target && e.target.nodeName != '#document' )
-			return;
-
-		$.ajax({
-			type: 'POST',
-			url: ajaxurl,
-			async: false,
-			data: {
-				action: 'wp-remove-post-lock',
-				_wpnonce: $('#_wpnonce').val(),
-				post_ID: $('#post_ID').val(),
-				active_post_lock: $('#active_post_lock').val()
-			}
-		});
-	} );
-
-	// preview
-	$('#post-preview').click(function(){
-		if ( $('#auto_draft').val() == '1' && notSaved ) {
-			autosaveDelayPreview = true;
-			autosave();
-			return false;
+		if ( typeof fullscreen != 'undefined' && fullscreen.settings.visible ) {
+			data['post_title'] = $('#wp-fullscreen-title').val() || '';
+			data['content'] = $('#wp_mce_fullscreen').val() || '';
+		} else {
+			data['post_title'] = $('#title').val() || '';
+			data['content'] = $('#content').val() || '';
 		}
-		doPreview();
-		return false;
-	});
 
-	doPreview = function() {
-		$('input#wp-preview').val('dopreview');
-		$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
+		if ( type == 'local' )
+			return data;
 
-		/*
-		 * Workaround for WebKit bug preventing a form submitting twice to the same action.
-		 * https://bugs.webkit.org/show_bug.cgi?id=28633
-		 */
-		var ua = navigator.userAgent.toLowerCase();
-		if ( ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1 ) {
-			$('form#post').attr('action', function(index, value) {
-				return value + '?t=' + new Date().getTime();
-			});
-		}
-
-		$('input#wp-preview').val('');
-	}
-
-	// This code is meant to allow tabbing from Title to Post content.
-	$('#title').on('keydown.editor-focus', function(e) {
-		var ed;
-
-		if ( e.which != 9 )
-			return;
-
-		if ( !e.ctrlKey && !e.altKey && !e.shiftKey ) {
-			if ( typeof(tinymce) != 'undefined' )
-				ed = tinymce.get('content');
-
-			if ( ed && !ed.isHidden() ) {
-				$(this).one('keyup', function(e){
-					$('#content_tbl td.mceToolbar > a').focus();
-				});
-			} else {
-				$('#content').focus();
-			}
-
-			e.preventDefault();
-		}
-	});
-
-	// autosave new posts after a title is typed but not if Publish or Save Draft is clicked
-	if ( '1' == $('#auto_draft').val() ) {
-		$('#title').blur( function() {
-			if ( !this.value || $('#auto_draft').val() != '1' )
-				return;
-			delayed_autosave();
+		$('input[id^="in-category-"]:checked').each( function() {
+			cats.push( this.value );
 		});
-	}
+		data['catslist'] = cats.join(',');
 
-	// When connection is lost, keep user from submitting changes.
-	$(document).on('heartbeat-connection-lost.autosave', function( e, error ) {
-		if ( 'timeout' === error ) {
-			var notice = $('#lost-connection-notice');
-			if ( ! wp.autosave.local.hasStorage ) {
-				notice.find('.hide-if-no-sessionstorage').hide();
-			}
-			notice.show();
-			autosave_disable_buttons();
-		}
-	}).on('heartbeat-connection-restored.autosave', function() {
-		$('#lost-connection-notice').hide();
-		autosave_enable_buttons();
-	});
-});
+		if ( post_name = $('#post_name').val() )
+			data['post_name'] = post_name;
 
-function autosave_parse_response( response ) {
-	var res = wpAjax.parseAjaxResponse(response, 'autosave'), post_id, sup;
+		if ( parent_id = $('#parent_id').val() )
+			data['parent_id'] = parent_id;
 
-	if ( res && res.responses && res.responses.length ) {
-		if ( res.responses[0].supplemental ) {
-			sup = res.responses[0].supplemental;
+		if ( $('#comment_status').prop('checked') )
+			data['comment_status'] = 'open';
 
-			jQuery.each( sup, function( selector, value ) {
-				if ( selector.match(/^replace-/) )
-					jQuery( '#' + selector.replace('replace-', '') ).val( value );
-			});
-		}
+		if ( $('#ping_status').prop('checked') )
+			data['ping_status'] = 'open';
 
-		// if no errors: add slug UI and update autosave-message
-		if ( !res.errors ) {
-			if ( post_id = parseInt( res.responses[0].id, 10 ) )
-				autosave_update_slug( post_id );
+		if ( $('#auto_draft').val() == '1' )
+			data['auto_draft'] = '1';
 
-			if ( res.responses[0].data ) // update autosave message
-				jQuery('.autosave-message').text( res.responses[0].data );
-		}
-	}
+		return data;
+	},
 
-	return res;
-}
-
-// called when autosaving pre-existing post
-function autosave_saved(response) {
-	blockSave = false;
-	autosave_parse_response(response); // parse the ajax response
-	autosave_enable_buttons(); // re-enable disabled form buttons
-}
-
-// called when autosaving new post
-function autosave_saved_new(response) {
-	blockSave = false;
-	var res = autosave_parse_response(response), post_id;
-
-	if ( res && res.responses.length && !res.errors ) {
-		// An ID is sent only for real auto-saves, not for autosave=0 "keepalive" saves
-		post_id = parseInt( res.responses[0].id, 10 );
-
-		if ( post_id ) {
-			notSaved = false;
-			jQuery('#auto_draft').val('0'); // No longer an auto-draft
+	// Concatenate title, content and excerpt. Used to track changes when auto-saving.
+	getCompareString: function( post_data ) {
+		if ( typeof post_data === 'object' ) {
+			return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' );
 		}
 
-		autosave_enable_buttons();
+		return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
+	},
 
-		if ( autosaveDelayPreview ) {
-			autosaveDelayPreview = false;
-			doPreview();
-		}
-	} else {
-		autosave_enable_buttons(); // re-enable disabled form buttons
-	}
-}
+	enableButtons: function() {
+		$document.trigger('autosave-enable-buttons');
 
-function autosave_update_slug(post_id) {
-	// create slug area only if not already there
-	if ( 'undefined' != makeSlugeditClickable && jQuery.isFunction(makeSlugeditClickable) && !jQuery('#edit-slug-box > *').size() ) {
-		jQuery.post( ajaxurl, {
-				action: 'sample-permalink',
-				post_id: post_id,
-				new_title: fullscreen && fullscreen.settings.visible ? jQuery('#wp-fullscreen-title').val() : jQuery('#title').val(),
-				samplepermalinknonce: jQuery('#samplepermalinknonce').val()
-			},
-			function(data) {
-				if ( data !== '-1' ) {
-					var box = jQuery('#edit-slug-box');
-					box.html(data);
-					if (box.hasClass('hidden')) {
-						box.fadeIn('fast', function () {
-							box.removeClass('hidden');
-						});
-					}
-					makeSlugeditClickable();
-				}
-			}
-		);
-	}
-}
-
-function autosave_loading() {
-	jQuery('.autosave-message').html(autosaveL10n.savingText);
-}
-
-function autosave_enable_buttons() {
-	jQuery(document).trigger('autosave-enable-buttons');
-	if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
-		// delay that a bit to avoid some rare collisions while the DOM is being updated.
-		setTimeout(function(){
-			var parent = jQuery('#submitpost');
-			parent.find(':button, :submit').removeAttr('disabled');
+		if ( ! wp.heartbeat || ! wp.heartbeat.hasConnectionError() ) {
+			var parent = $('#submitpost');
+			parent.find(':button, :submit, a.submitdelete, #post-preview').prop('disabled', false).removeClass('button-disabled');
 			parent.find('.spinner').hide();
-		}, 500);
-	}
-}
+		}
+	},
 
-function autosave_disable_buttons() {
-	jQuery(document).trigger('autosave-disable-buttons');
-	jQuery('#submitpost').find(':button, :submit').prop('disabled', true);
-	// Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
-	setTimeout( autosave_enable_buttons, 5000 );
-}
+	disableButtons: function() {
+		$document.trigger('autosave-disable-buttons');
 
-function delayed_autosave() {
-	setTimeout(function(){
-		if ( blockSave )
-			return;
-		autosave();
-	}, 200);
-}
+		var parent = $('#submitpost');
+		parent.find(':button, :submit, a.submitdelete, #post-preview').prop('disabled', true).addClass('button-disabled');
+		parent.find('#save-action .spinner').show();
 
-autosave = function() {
-	var post_data = wp.autosave.getPostData(),
-		compareString,
-		successCallback;
-
-	blockSave = true;
-
-	// post_data.content cannot be retrieved at the moment
-	if ( ! post_data.autosave )
-		return false;
-
-	// No autosave while thickbox is open (media buttons)
-	if ( jQuery("#TB_window").css('display') == 'block' )
-		return false;
-
-	compareString = wp.autosave.getCompareString( post_data );
-
-	// Nothing to save or no change.
-	if ( compareString == autosaveLast )
-		return false;
-
-	autosaveLast = compareString;
-	jQuery(document).triggerHandler('wpcountwords', [ post_data["content"] ]);
-
-	// Disable buttons until we know the save completed.
-	autosave_disable_buttons();
-
-	if ( post_data["auto_draft"] == '1' ) {
-		successCallback = autosave_saved_new; // new post
-	} else {
-		successCallback = autosave_saved; // pre-existing post
+		// Re-enable 5 sec later. Just gives autosave a head start to avoid collisions.
+		setTimeout( wp.autosave.enableButtons, 5000 );
 	}
+};
 
-	jQuery.ajax({
-		data: post_data,
-		beforeSend: autosave_loading,
-		type: "POST",
-		url: ajaxurl,
-		success: successCallback
-	});
-
-	return true;
-}
-
 // Autosave in localStorage
 // set as simple object/mixin for now
-window.wp = window.wp || {};
-wp.autosave = wp.autosave || {};
-
-(function($){
-// Returns the data for saving in both localStorage and autosaves to the server
-wp.autosave.getPostData = function() {
-	var ed = typeof tinymce != 'undefined' ? tinymce.activeEditor : null, post_name, parent_id, cats = [],
-		data = {
-			action: 'autosave',
-			autosave: true,
-			post_id: $('#post_ID').val() || 0,
-			autosavenonce: $('#autosavenonce').val() || '',
-			post_type: $('#post_type').val() || '',
-			post_author: $('#post_author').val() || '',
-			excerpt: $('#excerpt').val() || ''
-		};
-
-	if ( ed && !ed.isHidden() ) {
-		// Don't run while the tinymce spellcheck is on. It resets all found words.
-		if ( ed.plugins.spellchecker && ed.plugins.spellchecker.active ) {
-			data.autosave = false;
-			return data;
-		} else {
-			if ( 'mce_fullscreen' == ed.id )
-				tinymce.get('content').setContent(ed.getContent({format : 'raw'}), {format : 'raw'});
-
-			tinymce.triggerSave();
-		}
-	}
-
-	if ( typeof fullscreen != 'undefined' && fullscreen.settings.visible ) {
-		data['post_title'] = $('#wp-fullscreen-title').val() || '';
-		data['content'] = $('#wp_mce_fullscreen').val() || '';
-	} else {
-		data['post_title'] = $('#title').val() || '';
-		data['content'] = $('#content').val() || '';
-	}
-
-	/*
-	// We haven't been saving tags with autosave since 2.8... Start again?
-	$('.the-tags').each( function() {
-		data[this.name] = this.value;
-	});
-	*/
-
-	$('input[id^="in-category-"]:checked').each( function() {
-		cats.push(this.value);
-	});
-	data['catslist'] = cats.join(',');
-
-	if ( post_name = $('#post_name').val() )
-		data['post_name'] = post_name;
-
-	if ( parent_id = $('#parent_id').val() )
-		data['parent_id'] = parent_id;
-
-	if ( $('#comment_status').prop('checked') )
-		data['comment_status'] = 'open';
-
-	if ( $('#ping_status').prop('checked') )
-		data['ping_status'] = 'open';
-
-	if ( $('#auto_draft').val() == '1' )
-		data['auto_draft'] = '1';
-
-	return data;
-};
-
-// Concatenate title, content and excerpt. Used to track changes when auto-saving.
-wp.autosave.getCompareString = function( post_data ) {
-	if ( typeof post_data === 'object' ) {
-		return ( post_data.post_title || '' ) + '::' + ( post_data.content || '' ) + '::' + ( post_data.excerpt || '' );
-	}
-
-	return ( $('#title').val() || '' ) + '::' + ( $('#content').val() || '' ) + '::' + ( $('#excerpt').val() || '' );
-};
-
 wp.autosave.local = {
-
 	lastSavedData: '',
 	blog_id: 0,
 	hasStorage: false,
@@ -502,18 +204,16 @@
 	save: function( data ) {
 		var result = false, post_data, compareString;
 
-		if ( ! data ) {
-			post_data = wp.autosave.getPostData();
-		} else {
+		if ( data ) {
 			post_data = this.getData() || {};
 			$.extend( post_data, data );
-			post_data.autosave = true;
+		} else {
+			post_data = wp.autosave.getPostData('local');
+			// Cannot get the post data at the moment
+			if ( ! post_data )
+				return false;
 		}
 
-		// Cannot get the post data at the moment
-		if ( ! post_data.autosave )
-			return false;
-
 		compareString = wp.autosave.getCompareString( post_data );
 
 		// If the content, title and excerpt did not change since the last save, don't save again
@@ -697,6 +397,246 @@
 	}
 };
 
+// Autosave on the server
+wp.autosave.remote = {
+	releaseLock: true,
+	autosaveLast: '',
+	nextTime: 0,
+
+	init: function() {
+		var self = this, $submitpost = $('#submitpost');
+
+		$document.on( 'heartbeat-send.autosave', function( event, data ) {
+			var autosaveData = self.getData();
+
+			if ( autosaveData ) {
+				data.autosave = autosaveData;
+			}
+		}).on( 'heartbeat-tick.autosave', function( event, data ) {
+			if ( data.autosave ) {
+				self.response( data.autosave );
+			}
+		}).on('heartbeat-connection-lost.autosave', function( event, error ) {
+			// When connection is lost, keep user from submitting changes.
+			if ( 'timeout' === error ) {
+				var notice = $('#lost-connection-notice');
+				if ( ! wp.autosave.local.hasStorage ) {
+					notice.find('.hide-if-no-sessionstorage').hide();
+				}
+				notice.show();
+				wp.autosave.disableButtons();
+			}
+		}).on('heartbeat-connection-restored.autosave', function() {
+			$('#lost-connection-notice').hide();
+			wp.autosave.enableButtons();
+		});
+
+		$(window).on( 'beforeunload.autosave', function() {
+			var editor = typeof(tinymce) != 'undefined' && tinymce.activeEditor, compareString;
+
+			if ( editor && ! editor.isHidden() ) {
+				if ( editor.isDirty() )
+					return autosaveL10n.saveAlert;
+			} else {
+				if ( fullscreen && fullscreen.settings.visible ) {
+					compareString = wp.autosave.getCompareString({
+						post_title: $('#wp-fullscreen-title').val() || '',
+						content: $('#wp_mce_fullscreen').val() || '',
+						excerpt: $('#excerpt').val() || ''
+					});
+				} else {
+					compareString = wp.autosave.getCompareString();
+				}
+
+				if ( compareString != self.autosaveLast )
+					return autosaveL10n.saveAlert;
+			}
+		}).on( 'unload.autosave', function( event ) {
+			if ( ! self.releaseLock )
+				return;
+
+			// unload fires (twice) on removing the Thickbox iframe. Make sure we process only the main document unload.
+			if ( event.target && event.target.nodeName != '#document' )
+				return;
+
+			$.ajax({
+				type: 'POST',
+				url: ajaxurl,
+				async: false,
+				data: {
+					action: 'wp-remove-post-lock',
+					_wpnonce: $('#_wpnonce').val(),
+					post_ID: $('#post_ID').val(),
+					active_post_lock: $('#active_post_lock').val()
+				}
+			});
+		});
+
+		$document.ready( function() {
+			self._schedule();
+
+			if ( $('#wp-content-wrap').hasClass('tmce-active') && typeof switchEditors != 'undefined' ) {
+				self.autosaveLast = wp.autosave.getCompareString({
+					post_title : $('#title').val() || '',
+					content : switchEditors.pre_wpautop( $('#content').val() ) || '',
+					excerpt : $('#excerpt').val() || ''
+				});
+			} else {
+				self.autosaveLast = wp.autosave.getCompareString();
+			}
+
+			// The form is being submitted by the user
+			$submitpost.find( 'input[type="submit"], a.submitdelete' ).on( 'click.autosave', function( event ) {
+				var $this = $(this);
+
+				if ( $this.prop('disabled') ) {
+					event.preventDefault(); // For the "Move to Trash" link
+					return;
+				}
+
+				self.blockSave();
+				self.releaseLock = false;
+				$(window).off( 'beforeunload.autosave' );
+
+				$submitpost.find('a.submitdelete, #post-preview').addClass('button-disabled');
+
+				if ( $this.attr('id') == 'publish' )
+					$submitpost.find('#major-publishing-actions .spinner').show();
+				else
+					$submitpost.find('#minor-publishing .spinner').show();
+			});
+
+			$('#post-preview').on( 'click.post-preview', function( event ) {
+				if ( ! $(this).prop('disabled') ) {
+					self.doPreview();
+				}
+				event.preventDefault();
+			});
+
+			// This code is meant to allow tabbing from Title to Post content.
+			$('#title').on( 'keydown.editor-focus', function( event ) {
+				var editor;
+
+				if ( event.which == 9 && ! event.ctrlKey && ! event.altKey && ! event.shiftKey ) {
+					if ( typeof(tinymce) != 'undefined' )
+						editor = tinymce.get('content');
+
+					if ( editor && ! editor.isHidden() ) {
+						$(this).one( 'keyup', function() {
+							$('#content_tbl td.mceToolbar > a').focus();
+						});
+					} else {
+						$('#content').focus();
+					}
+
+					event.preventDefault();
+				}
+			});
+
+			// Autosave new posts after a title is typed
+			if ( $('#auto_draft').val() ) {
+				$('#title').blur( function() {
+					if ( ! this.value || $('#auto_draft').val() != '1' )
+						return;
+
+					self.triggerSave();
+				});
+			}
+		});
+	},
+
+	// Block saving for the next 15 sec.
+	blockSave: function() {
+		_blockSave = true;
+		window.clearTimeout( _blockSaveTimeout );
+		_blockSaveTimeout = window.setTimeout( function() {
+			_blockSave = false;
+		}, 15000 );
+	},
+
+	// Submit the form saving a draft or an autosave, and show a preview in a new tab
+	doPreview: function() {
+		this.blockSave();
+		$('input#wp-preview').val('dopreview');
+		$('form#post').attr('target', 'wp-preview').submit().attr('target', '');
+
+		// Workaround for WebKit bug preventing a form submitting twice to the same action.
+		// https://bugs.webkit.org/show_bug.cgi?id=28633
+		var ua = navigator.userAgent.toLowerCase();
+		if ( ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1 ) {
+			$('form#post').attr( 'action', function( index, value ) {
+				return value + '?t=' + new Date().getTime();
+			});
+		}
+
+		$('input#wp-preview').val('');
+	},
+
+	// Runs on heartbeat-response
+	response: function( data ) {
+		this._schedule();
+		_blockSave = false;
+		this.autosaveLast = this.lastCompareString;
+		this.lastCompareString = '';
+
+		$(document).trigger( 'after-autosave', [data] );
+		$('#auto_draft').val(''); // No longer an auto-draft
+		$('.autosave-message').text( data.message );
+
+		wp.autosave.enableButtons(); // re-enable disabled form buttons
+	},
+
+	disable: function() {
+		_disabled = true;
+	},
+
+	_schedule: function() {
+		this.nextTime = ( new Date() ).getTime() + ( autosaveL10n.autosaveInterval * 1000 ) || 60000;
+	},
+
+	// Set heartbeat to connect fast (once) and save on the next tick.
+	triggerSave: function() {
+		this.nextTime = 0;
+		wp.heartbeat.interval( 'fast', 1 );
+	},
+
+	// Runs on 'heartbeat-send'
+	getData: function() {
+		var compareString, postData;
+
+		if ( _disabled || _blockSave )
+			return false;
+
+		if ( ( new Date() ).getTime() < this.nextTime )
+			return false;
+
+		postData = wp.autosave.getPostData();
+
+		// post_data.content cannot be retrieved at the moment
+		if ( ! postData )
+			return false;
+
+		compareString = wp.autosave.getCompareString( postData );
+
+		// Nothing to save or no change.
+		if ( compareString == this.autosaveLast )
+			return false;
+
+		this.blockSave();
+		wp.autosave.disableButtons();
+		this.lastCompareString = compareString;
+
+		$(document).trigger( 'wpcountwords', [ postData['content'] ] );
+		$(document).trigger( 'before-autosave', [ postData ] );
+
+		$('.autosave-message').text( autosaveL10n.savingText );
+		postData._wpnonce = $('#_wpnonce').val() || '';
+
+		return postData;
+	}
+};
+
 wp.autosave.local.init();
+wp.autosave.remote.init();
 
-}(jQuery));
+}( jQuery, window ));
Index: src/wp-includes/js/heartbeat.js
===================================================================
--- src/wp-includes/js/heartbeat.js	(revision 25335)
+++ src/wp-includes/js/heartbeat.js	(working copy)
@@ -183,6 +183,9 @@
 				timeout: 30000, // throw an error if not completed after 30 sec.
 				data: send,
 				dataType: 'json'
+			}).always( function() {
+				connecting = false;
+				next();
 			}).done( function( response, textStatus, jqXHR ) {
 				var new_interval;
 
@@ -209,9 +212,6 @@
 				// do this last, can trigger the next XHR if connection time > 5 sec. and new_interval == 'fast'
 				if ( new_interval )
 					self.interval.call( self, new_interval );
-			}).always( function() {
-				connecting = false;
-				next();
 			}).fail( function( jqXHR, textStatus, error ) {
 				errorstate( textStatus || 'unknown' );
 				self.error( jqXHR, textStatus, error );
Index: src/wp-includes/revision.php
===================================================================
--- src/wp-includes/revision.php	(revision 25335)
+++ src/wp-includes/revision.php	(working copy)
@@ -215,7 +215,7 @@
  *
  * @param int|object|array $post Post ID, post object OR post array.
  * @param bool $autosave Optional. Is the revision an autosave?
- * @return mixed Null or 0 if error, new revision ID if success.
+ * @return mixed WP_Error or 0 if error, new revision ID if success.
  */
 function _wp_put_post_revision( $post = null, $autosave = false ) {
 	if ( is_object($post) )
@@ -223,8 +223,8 @@
 	elseif ( !is_array($post) )
 		$post = get_post($post, ARRAY_A);
 
-	if ( !$post || empty($post['ID']) )
-		return;
+	if ( ! $post || empty($post['ID']) )
+		return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
 
 	if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
 		return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
