Index: lyx-devel/src/tex2lyx/text.cpp
===================================================================
--- lyx-devel/src/tex2lyx/text.cpp	(revision 18756)
+++ lyx-devel/src/tex2lyx/text.cpp	(working copy)
@@ -69,11 +69,12 @@
 
 /// parses a paragraph snippet, useful for example for \\emph{...}
 void parse_text_snippet(Parser & p, ostream & os, unsigned flags, bool outer,
-		Context & context)
+		Context & context,bool inheritExtra=false)
 {
 	Context newcontext(context);
 	// Don't inherit the extra stuff
-	newcontext.extra_stuff.clear();
+	if (!inheritExtra)
+		newcontext.extra_stuff.clear();
 	parse_text(p, os, flags, outer, newcontext);
 	// Make sure that we don't create invalid .lyx files
 	context.need_layout = newcontext.need_layout;
@@ -169,7 +170,7 @@
 char const * const known_coded_font_series[] = { "bold", "medium", 0};
 
 /// LaTeX 2.09 names for font shapes
-char const * const known_old_font_shapes[] = { "it", "sl", "sc", 0};
+char const * const known_old_font_shapes[] = { "it", "sl", "sc", "em", 0};
 
 /// LaTeX names for font shapes
 char const * const known_font_shapes[] = { "itshape", "slshape", "scshape",
@@ -177,7 +178,7 @@
 
 /// the same as known_old_font_shapes and known_font_shapes with .lyx names
 char const * const known_coded_font_shapes[] = { "italic", "slanted",
-"smallcaps", "up", 0};
+"smallcaps", "italic", "up", 0};
 
 /*!
  * Graphics file extensions known by the dvips driver of the graphics package.
@@ -793,19 +794,27 @@
 
 	// Alignment settings
 	else if (name == "center" || name == "flushleft" || name == "flushright" ||
-		 name == "centering" || name == "raggedright" || name == "raggedleft") {
-		eat_whitespace(p, os, parent_context, false);
+		 name == "centering" || name == "raggedright" || name == "raggedleft"  ||
+		 name == "spacing") {
+		p.skip_spaces();
 		// We must begin a new paragraph if not already done
 		if (! parent_context.atParagraphStart()) {
 			parent_context.check_end_layout(os);
 			parent_context.new_paragraph(os);
 		}
-		if (name == "flushleft" || name == "raggedright")
-			parent_context.add_extra_stuff("\\align left\n");
-		else if (name == "flushright" || name == "raggedleft")
-			parent_context.add_extra_stuff("\\align right\n");
-		else
-			parent_context.add_extra_stuff("\\align center\n");
+		if(name == "spacing") {
+			std::string extra = "\\paragraph_spacing other ";
+			extra += p.getArg('{', '}')+'\n';
+			parent_context.add_extra_stuff(extra);
+			p.skip_spaces();
+		} else {
+			if (name == "flushleft" || name == "raggedright")
+				parent_context.add_extra_stuff("\\align left\n");
+			else if (name == "flushright" || name == "raggedleft")
+				parent_context.add_extra_stuff("\\align right\n");
+			else
+				parent_context.add_extra_stuff("\\align center\n");
+		}
 		parse_text(p, os, FLAG_END, outer, parent_context);
 		// Just in case the environment is empty ..
 		parent_context.extra_stuff.erase();
@@ -1514,14 +1523,21 @@
 			p.skip_spaces();
 		}
 
-		else if (t.cs() == "includegraphics") {
-			bool const clip = p.next_token().asInput() == "*";
-			if (clip)
-				p.get_token();
-			map<string, string> opts = split_map(p.getArg('[', ']'));
-			if (clip)
-				opts["clip"] = string();
-			string name = normalize_filename(p.verbatim_item());
+		else if ( (t.cs() == "includegraphics") || (t.cs() == "epsfig") || (t.cs() == "psfig") ) {
+			map<string, string> opts;
+			string name;
+			if( (t.cs() == "epsfig") || (t.cs() == "psfig") ) {
+				opts = split_map(p.getArg('{', '}'));
+				name = normalize_filename(opts["figure"]);
+			} else {
+				bool const clip = p.next_token().asInput() == "*";
+				if (clip)
+					p.get_token();
+				opts = split_map(p.getArg('[', ']'));
+				if (clip)
+					opts["clip"] = string();
+				name = normalize_filename(p.verbatim_item());
+			}
 
 			string const path = getMasterFilePath();
 			// We want to preserve relative / absolute filenames,
@@ -1822,6 +1838,39 @@
 			os << "\n\\" << t.cs() << " default\n";
 		}
 
+		else if (t.cs() == "caption") {
+			context.new_paragraph(os);
+			context.check_layout(os);
+			context.need_end_layout = false;
+
+			begin_inset(os, "Caption\n");
+			context.new_paragraph(os);
+			parse_text_snippet(p, os, FLAG_ITEM, outer, context);
+			context.check_end_layout(os);
+			end_inset(os);
+			
+			context.need_end_layout = true;
+		}
+
+		// FIXME: We now just ignore the size parameter.
+		// How to resolve it to real lyx size parameters?
+		else if (t.cs() == "lyxline") {
+			os << "\n\\lyxline\n";
+			p.getArg('{','}');
+		}
+
+		else if (t.cs() == "centerline") {
+			eat_whitespace(p, os, context, false);
+
+			context.add_extra_stuff("\\align center\n");
+			parse_text_snippet(p, os, FLAG_ITEM, outer, context, true);
+			context.extra_stuff.erase();
+			
+			// We must begin a new paragraph to reset the alignment
+			context.new_paragraph(os);
+			p.skip_spaces();
+		}
+
 		else if (use_natbib &&
 			 is_known(t.cs(), known_natbib_commands) &&
 			 ((t.cs() != "citefullauthor" &&
Index: lyx-devel/src/tex2lyx/preamble.cpp
===================================================================
--- lyx-devel/src/tex2lyx/preamble.cpp	(revision 18756)
+++ lyx-devel/src/tex2lyx/preamble.cpp	(working copy)
@@ -279,6 +279,8 @@
 	// initialize fixed types
 	special_columns['D'] = 3;
 	bool is_full_document = false;
+	
+	string lyxLayout;
 
 	// determine whether this is a full document or a fragment for inclusion
 	while (p.good()) {
@@ -288,9 +290,14 @@
 			is_full_document = true;
 			break;
 		}
+		
+		if (t.cat() == catComment) {
+			if (t.cs().substr(0,24) == "%LyXComment%% LyXLayout=") 
+				lyxLayout = t.cs().substr(24);
+		}
 	}
 	p.reset();
-
+	
 	while (is_full_document && p.good()) {
 		Token const & t = p.get_token();
 
@@ -298,6 +305,27 @@
 		cerr << "t: " << t << "\n";
 #endif
 
+		// ignore LyX written comments and LyX controlled preamble
+		if (t.cat() == catComment) {
+			if (t.cs().substr(0,13) == "%LyXComment%%") 
+				continue;
+			
+			if (t.cs().substr(0,28) == "% BeginLyXControlledPreamble") {
+				while (p.good()) {
+					Token const & t2 = p.get_token();
+					if (t2.cat() == catComment) {
+						if (t2.cs().substr(0,26) == "% EndLyXControlledPreamble") 
+							break;
+					} else if (t2.cs() == "makeatletter")
+						p.setCatCode('@', catLetter);
+					else if (t2.cs() == "makeatother")
+						p.setCatCode('@', catOther);
+				}
+				
+				continue;
+			}
+		}
+
 		//
 		// cat codes
 		//
@@ -490,6 +518,9 @@
 	// Force textclass if the user wanted it
 	if (!forceclass.empty())
 		h_textclass = forceclass;
+	// Use textclass layout name written in comment if we find one
+	if (!lyxLayout.empty())
+		h_textclass = lyxLayout;
 	if (noweb_mode && !lyx::support::prefixIs(h_textclass, "literate-"))
 		h_textclass.insert(0, "literate-");
 	FileName layoutfilename = libFileSearch("layouts", h_textclass, "layout");
Index: lyx-devel/src/Buffer.cpp
===================================================================
--- lyx-devel/src/Buffer.cpp	(revision 18756)
+++ lyx-devel/src/Buffer.cpp	(working copy)
@@ -946,10 +946,14 @@
 	texrow().start(paragraphs().begin()->id(), 0);
 
 	if (output_preamble && runparams.nice) {
-		os << "%% LyX " << lyx_version << " created this file.  "
+		os << "%%LyXComment%% LyX " << lyx_version << " created this file.  "
 			"For more info, see http://www.lyx.org/.\n"
-			"%% Do not edit unless you really know what "
+			"%%LyXComment%% Do not edit unless you really know what "
 			"you are doing.\n";
+			
+		docstring const layoutName = from_utf8(params().getTextClass().name());
+			
+		os << "%%LyXComment%% LyXLayout=" << layoutName << "\n";
 		texrow().newline();
 		texrow().newline();
 	}
Index: lyx-devel/src/BufferParams.cpp
===================================================================
--- lyx-devel/src/BufferParams.cpp	(revision 18756)
+++ lyx-devel/src/BufferParams.cpp	(working copy)
@@ -1072,6 +1072,10 @@
 	// The optional packages;
 	docstring lyxpreamble(from_ascii(features.getPackages()));
 
+	// we do not want tex2lyx read these contents back again
+	// so we mark them out
+	lyxpreamble += "%% BeginLyXControlledPreamble ** \\/\\/ DO NOT EDIT BELOW \\/\\/ **\n";
+
 	// this might be useful...
 	lyxpreamble += "\n\\makeatletter\n";
 
@@ -1079,7 +1083,7 @@
 	docstring tmppreamble(from_ascii(features.getMacros()));
 
 	if (!tmppreamble.empty()) {
-		lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
+		lyxpreamble += "\n%%LyXComment%% %%%%%%%%%%%%%%%%%%%% "
 			"LyX specific LaTeX commands.\n"
 			+ tmppreamble + '\n';
 	}
@@ -1087,15 +1091,19 @@
 	// the text class specific preamble
 	tmppreamble = features.getTClassPreamble();
 	if (!tmppreamble.empty()) {
-		lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
+		lyxpreamble += "%%LyXComment%% %%%%%%%%%%%%%%%%%%%%% "
 			"Textclass specific LaTeX commands.\n"
 			+ tmppreamble + '\n';
 	}
 
+	// we do not want tex2lyx read these contents back again
+	// so we mark them out. Here we are at the end
+	lyxpreamble += "%% EndLyXControlledPreamble ** /\\/\\ DO NOT EDIT ABOVE /\\/\\ **\n";
+
 	/* the user-defined preamble */
 	if (!preamble.empty()) {
 		// FIXME UNICODE
-		lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
+		lyxpreamble += "%%LyXComment%% %%%%%%%%%%%%%%%%%%%%% "
 			"User specified LaTeX commands.\n"
 			+ from_utf8(preamble) + '\n';
 	}
@@ -1144,7 +1152,11 @@
 		lyxpreamble += from_utf8(features.getBabelOptions());
 	}
 
+	// we do not want tex2lyx read these contents back again
+	// so we mark them out
+	lyxpreamble += "%% BeginLyXControlledPreamble ** \\/\\/ DO NOT EDIT BELOW \\/\\/ **\n";
 	lyxpreamble += "\\makeatother\n";
+	lyxpreamble += "%% EndLyXControlledPreamble ** /\\/\\ DO NOT EDIT ABOVE /\\/\\ **\n";
 
 	int const nlines =
 		int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
