Index: wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js
===================================================================
--- wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js	(revision 21949)
+++ wp-includes/js/tinymce/plugins/wordpress/editor_plugin_src.js	(working copy)
@@ -7,13 +7,18 @@
 
 	tinymce.create('tinymce.plugins.WordPress', {
 		mceTout : 0,
+		previews : {i: 0},
+		reg_previews : [],
 
 		init : function(ed, url) {
 			var t = this, tbId = ed.getParam('wordpress_adv_toolbar', 'toolbar2'), last = 0, moreHTML, nextpageHTML, closeOnClick, mod_key;
+
 			moreHTML = '<img src="' + url + '/img/trans.gif" class="mceWPmore mceItemNoResize" title="'+ed.getLang('wordpress.wp_more_alt')+'" />';
 			nextpageHTML = '<img src="' + url + '/img/trans.gif" class="mceWPnextpage mceItemNoResize" title="'+ed.getLang('wordpress.wp_page_alt')+'" />';
 
-			if ( getUserSetting('hidetb', '0') == '1' )
+			t.ed = ed;
+
+			if ( typeof(getUserSetting) == 'function' && getUserSetting('hidetb', '0') == '1' )
 				ed.settings.wordpress_adv_hidden = 0;
 
 			// Hides the specified toolbar and resizes the iframe
@@ -251,9 +256,6 @@
 			// Add custom shortcuts
 			mod_key = 'alt+shift';
 
-		//	if ( tinymce.isGecko ) // disable for mow, too many shortcuts conflicts
-		//		mod_key = 'ctrl+alt';
-
 			ed.addShortcut(mod_key + '+c', 'justifycenter_desc', 'JustifyCenter');
 			ed.addShortcut(mod_key + '+r', 'justifyright_desc', 'JustifyRight');
 			ed.addShortcut(mod_key + '+l', 'justifyleft_desc', 'JustifyLeft');
@@ -313,6 +315,95 @@
 			// close popups when clicking on the background
 			tinymce.dom.Event.remove(document.body, 'click', closeOnClick);
 			tinymce.dom.Event.add(document.body, 'click', closeOnClick);
+
+
+			// previews
+			ed.onPreInit.add( function(ed){
+				ed.schema.addValidElements('div[*]'); // so we can set contenteditable=false
+
+				// add custom jQuery event so plugins can do:
+				// jQuery(document).on( 'wp_add_mce_previews', function(e, ed){ ed.wpPreviewRegister(args) } );
+				if ( typeof(jQuery) != 'undefined' )
+					jQuery(document).trigger( 'wp_add_mce_previews', ed );
+			});
+
+			// scan on paste and on inserting nodes/html (on send_to_editor)
+			ed.onInit.add(function(ed) {
+				ed.selection.onSetContent.add(function(sel, o) {
+					var node;
+
+					if ( o.context && t.reg_previews.length ) {
+						node = sel.getNode();
+
+						if ( node.innerHTML ) {
+							node.innerHTML = t.previews_step_1(node.innerHTML);
+							t.previews_step_2( ed.dom.select('div.wp-preview-wrap', node) );
+						}
+					}
+				});
+			});
+			
+			// scan the last paragraph on enter
+			ed.onKeyDown.add(function(ed, e){
+				var node, html;
+
+				if ( e.keyCode == 13 ) {
+					node = ed.selection.getNode();
+
+					if ( node.innerHTML ) {
+						html = t.previews_step_1(node.innerHTML);
+
+						setTimeout(function(){ // delay so all other keydown events run
+							node.innerHTML = html;
+
+							if ( ed.dom.is(node.lastChild, 'br') )
+								ed.dom.remove(node.lastChild);
+
+							t.previews_step_2( ed.dom.select('div.wp-preview-wrap', node) );
+						}, 50);	
+					}
+				}
+			});
+
+			// runs every time content (html string) is loaded in the editor
+			ed.onBeforeSetContent.add(function(ed, o) {
+				t.previews = {i: 0};
+
+				if ( o.content )
+					o.content = t.previews_step_1(o.content);
+			});
+			
+			// runs when the DOM is ready every time content is loaded in the editor
+			ed.onLoadContent.add(function(ed, o){
+				t.previews_step_2( ed.dom.select('div.wp-preview-wrap') );
+			});
+
+			ed.onPostProcess.add(function(ed, o) {
+				if ( ( o.get || o.save ) && o.content.indexOf('data-wp-preview="') > -1 ) {
+					o.content = o.content.replace(/(<div[^>]+data-wp-preview="([^"]+)"[^>]*>)[\s\S]+?<div data-wp-preview-blocker="1">.*?<\/div><\/div>/g, function(a, b, c){
+						var previews = t.previews, str = '<p>'+ decodeURIComponent(c) +'</p>', div_id = b.replace(/.+id="([^"]+)".+/, '$1');
+
+						if ( previews[div_id] && previews[div_id].cleanup_callback )
+							str = previews[div_id].cleanup_callback.call( t, { ed: ed, original: str } );
+
+						return str;
+					});
+				}
+			});
+
+			/*
+			store all preview args
+			args = {
+				ref_name: 'my_preview',
+				preview_regexp: /\[gallery[^\]]*\]/,
+				str_callback: function(obj){ console.log('str_callback'); console.log(obj); return obj.original+' 123 ' },
+				node_callback: function(obj){ console.log('node_callback'); console.log(obj) },
+				cleanup_callback: function(obj){ console.log('cleanup_callback'); console.log(obj); return obj.original }
+			}
+			*/
+			ed.wpPreviewRegister = function(args) {
+				t.reg_previews.push(args);
+			};
 		},
 
 		getInfo : function() {
@@ -418,17 +509,18 @@
 
 			// Replace images with morebreak
 			ed.onPostProcess.add(function(ed, o) {
-				if (o.get)
+				if ( o.get ) {
 					o.content = o.content.replace(/<img[^>]+>/g, function(im) {
-						if (im.indexOf('class="mceWPmore') !== -1) {
-							var m, moretext = (m = im.match(/alt="(.*?)"/)) ? m[1] : '';
+						if ( im.indexOf('class="mceWPmore') !== -1 ) {
+							var m, moretext = ( m = im.match(/alt="(.*?)"/) ) ? m[1] : '';
 							im = '<!--more'+moretext+'-->';
 						}
-						if (im.indexOf('class="mceWPnextpage') !== -1)
+						if ( im.indexOf('class="mceWPnextpage') !== -1 )
 							im = '<!--nextpage-->';
 
 						return im;
 					});
+				}
 			});
 
 			// Set active buttons if user selected pagebreak or more break
@@ -436,6 +528,55 @@
 				cm.setActive('wp_page', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPnextpage'));
 				cm.setActive('wp_more', n.nodeName === 'IMG' && ed.dom.hasClass(n, 'mceWPmore'));
 			});
+		},
+		
+		previews_step_1: function(content) {
+			var self = this, previews = self.previews;
+
+			if ( self.reg_previews.length ) {
+				tinymce.each( self.reg_previews, function(args){
+					if ( !args.preview_regexp )
+						return;
+
+					var regex = new RegExp( '(?:<p>\\s*)?(' + args.preview_regexp.source + ')(?:\\s*</p>)?', 'g' );
+
+					content = content.replace(regex, function(a, orig_str){
+						var html, div_id, new_str;
+
+						previews.i++;
+						div_id = 'wp_mce_preview_' + previews.i;
+						// ed, self or this?
+						new_str = args.str_callback.call( self, { ed: self.ed, original: orig_str, div_id: div_id } );
+						previews[div_id] = args;
+
+						html = '<div id="'+ div_id +'" data-wp-preview="'+ encodeURIComponent(orig_str) +'" class="wp-preview-wrap" contenteditable="false">' +
+							'<div class="wp-preview">'+ new_str +'</div>' + // add inner wrapper so the preview node(s) can be appended there without disturbing the blocker and end divs?
+							'<div data-wp-preview-blocker="1">\uFEFF</div></div>';
+
+						return html;
+					});
+				});
+			}
+
+			return content;
+		},
+
+		// go through the previews added in step_1 and fire node callback
+		previews_step_2: function(nodes) {
+			var self = this;
+
+			tinymce.each( nodes, function(node){
+				var div_id = node.id, args;
+
+				if ( !div_id || !self.previews.hasOwnProperty(div_id) )
+					return;
+
+				args = self.previews[div_id];
+
+				if ( args.node_callback ) { // use the inner wrapper as node in the callback
+					args.node_callback.call( self, { ed: self.ed, node: node.firstChild, div_id: div_id } );
+				}
+			});
 		}
 	});
 
Index: wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js
===================================================================
--- wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js	(revision 21949)
+++ wp-includes/js/tinymce/plugins/wpgallery/editor_plugin_src.js	(working copy)
@@ -26,7 +26,7 @@
 				if ( e.target.nodeName == 'IMG' && ed.dom.hasClass(e.target, 'wpGallery') )
 					ed.plugins.wordpress._showButtons(e.target, 'wp_gallerybtns');
 			});
-
+/*
 			ed.onBeforeSetContent.add(function(ed, o) {
 				o.content = t._do_gallery(o.content);
 			});
@@ -35,6 +35,7 @@
 				if (o.get)
 					o.content = t._get_gallery(o.content);
 			});
+*/
 		},
 
 		_do_gallery : function(co) {
Index: wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css
===================================================================
--- wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css	(revision 21949)
+++ wp-includes/js/tinymce/themes/advanced/skins/wp_theme/content.css	(working copy)
@@ -141,3 +141,108 @@
 	height: 250px;
 }
 
+/* Previews */
+
+div[data-wp-preview-blocker] {
+	height: 100%;
+	width: 100%;
+	position: absolute;
+	background: #fff;
+	opacity: 0.01;
+	filter: alpha(opacity=1);
+	top: 0;
+	left: 0;
+	bottom: 0;
+	right: 0;
+	z-index: 5;
+}
+
+div[data-wp-preview-blocker].focused {
+	background-color: #000;
+	filter: alpha(opacity=25);
+	opacity: 0.25;
+}
+
+div.wp-preview-wrap {
+	margin: 10px auto;
+	position: relative;
+}
+
+div.wp-preview-wrap,
+div[data-wp-preview-blocker] {
+	-ms-user-select: none;
+	-moz-user-select: none; /* doesn't work */
+	-webkit-user-select: none;
+	user-select: none;
+}
+
+div.wp-preview-wrap .gallery-icon {
+	margin: 0;
+}
+
+div.wp-preview-wrap .gallery-icon img {
+	display: block;
+	margin: 0 auto;
+	padding: 0;
+	max-width: 90%;
+	height: auto;
+	border: 0;
+}
+
+div.wp-preview-wrap dd.gallery-caption {
+	margin: 0 6%;
+	font-size: 12px;
+}
+
+div.wp-preview-wrap div.gallery {
+	clear: both;
+}
+
+div.wp-preview-wrap div.gallery dl.gallery-item {
+	float: left;
+	margin: 10px 0;
+	padding: 0;
+}
+
+div.wp-preview-wrap div.gallery-columns-1 dl.gallery-item {
+	width: 99%;
+}
+
+div.wp-preview-wrap div.gallery-columns-2 dl.gallery-item {
+	width: 49.5%;
+}
+
+div.wp-preview-wrap div.gallery-columns-3 dl.gallery-item {
+	width: 33%;
+}
+
+div.wp-preview-wrap div.gallery-columns-4 dl.gallery-item {
+	width: 24.75%;
+}
+
+div.wp-preview-wrap div.gallery-columns-5 dl.gallery-item {
+	width: 19.8%;
+}
+
+div.wp-preview-wrap div.gallery-columns-6 dl.gallery-item {
+	width: 16.5%;
+}
+
+div.wp-preview-wrap div.gallery-columns-7 dl.gallery-item {
+	width: 14.2%;
+}
+
+div.wp-preview-wrap div.gallery-columns-8 dl.gallery-item {
+	width: 12.45%;
+}
+
+div.wp-preview-wrap div.gallery-columns-9 dl.gallery-item {
+	width: 11.1%;
+}
+
+div.wp-preview-wrap div.gallery-columns-10 dl.gallery-item {
+	width: 9.99%;
+}
+
+
+
