My apologies for all those mistakes. Now, I've read the entire README and the 
files in the development/Code_rules directory ;-)
I've send that patch mostly for the idea of inserting the X selection into a 
table, because it could be useful for inserting columns of numbers for 
example. But I think the real need is to insert data coming from a 
spreadsheet into LyX, something like inserting a csv file into a LyX table.

Thank you for the trick for inserting tab-separated fields !

Here is something less bad.

-- 
Bruno

Le Lundi 6 Janvier 2003 11:57, vous avez écrit :
> On Mon, Jan 06, 2003 at 11:35:37AM +0100, Bruno Mathieu wrote:
> > Oups, sorry !
> > Here is the dirty patch.
>
> Just to comment cosmetics: It would be nice if new code followed optically
> current practice, i.e:
>
> +     int row=1;
>
>   int row = 1;
>
> +       if (col>colmax) {
>
>   if (col > colmax) {
>
> +     InsetTabular *it = new InsetTabular(*this,row,colmax);
>
>   InsetTabular *it = new InsetTabular(*this, row, colmax);
>
> +     row=0;
> +     col=0;
>
> [This almost looks like the function could be broken into smaller parts..]
>
> +     //        LyXTabular *lt = it->tabular;
> +     InsetText *inset=NULL;
>
> This should be moved down to the place where it is actually needed:
>
> +         inset = it->tabular->GetCellInset(c);
> +         inset->setText(texte,font);
>
>    InsetText * inset = it->tabular->GetCellInset(c);
>    inset->setText(texte, font);
>
> [And NULL is rarely used in LyX's code, so you should use '0' in cases when
> you want a 'pointer to nothing']
>
> + char texte[1000];
>
> Why this magic constant 1000?

a dirty constant ;-( 

> What happens if I paste a single word with 1001 chars?

>=1000 chars => probably a segfault 

>       ///
> +     void pasteClipboardAsTablular(void);
>
>   void pasteClipboardAsTablular();
>
> And filling in the comment line would be nice as well (contrary to the
> advice "do what the others do" above ;-})
>
> Andre'

diff -Naur lyx-1.2.2-orig/src/buffer.C lyx-1.2.2/src/buffer.C
--- lyx-1.2.2-orig/src/buffer.C	2002-08-19 12:37:08.000000000 +0200
+++ lyx-1.2.2/src/buffer.C	2003-01-06 18:51:36.000000000 +0100
@@ -1487,6 +1487,91 @@
 	return the_end_read;
 }
 
+// set the contents of a cell, used by insertStringAsTabular
+static void setTexte(const InsetTabular *it,  int row, 
+		     int col, const string &texte, const LyXFont &font)
+{
+	int c = it->tabular->GetCellNumber(row, col);
+	InsetText *inset = it->tabular->GetCellInset(c);
+	inset->setText(texte, font);	
+}
+
+// insert the X selection into a table. Example :
+/* 
+    X      Y
+   123    123
+    33     65 
+*/
+// produces a table of 2 colums, 3 rows.
+void Buffer::insertStringAsTabular(Paragraph *& par, pos_type & pos,
+				 LyXFont const & fn,string const & str) const
+{
+	LyXFont font = fn;
+
+	int colmax = 0;
+	int row;
+	int col;
+	bool update = false;
+	InsetTabular * it = 0;
+	string texte;
+	string::const_iterator beg = str.begin();
+	string::const_iterator end = str.end();
+
+	while (beg != end && (*beg == ' '  || *beg == '\t')) {
+		++beg;
+	}
+	if (beg == end) {
+	  return;
+	}	
+	do {
+		row = 0;
+		col = 0;
+		for(string::const_iterator cit = beg;
+		    cit != end; ++cit) {
+	  		switch (*cit) {
+  			case '\n': {
+  				if (update) {
+					setTexte(it, row, col, texte, font);
+					texte.clear();
+	  			}
+				++row;
+				col = 0;
+				while (++cit < end && (*cit == ' '  || *cit == '\t'));
+				--cit;
+				continue;
+  			}
+	  		case ' ':
+  			case '\t': {
+  				if (update) {
+  					setTexte(it, row, col, texte, font);
+					texte.clear();
+	  			}
+				++col;
+				if (col > colmax) {
+					colmax = col;
+	  	  		}
+				while (++cit < end && (*cit == ' '  || *cit == '\t'));
+				--cit;
+				continue;
+			}
+			default: {
+  				if (update) {
+  					texte.push_back(*cit);
+  				}
+			}
+  			}
+		}
+	  	if (!update) {
+  			it = new InsetTabular(*this, row+1, colmax+1);
+  			par->insertInset(pos, it);
+	  		update = true;
+  		} else {
+  			update = false;
+	  	}
+	} while (update);
+	setTexte(it, row, col, texte, font);
+}
+
 // needed to insert the selection
 void Buffer::insertStringAsLines(Paragraph *& par, pos_type & pos,
 				 LyXFont const & fn,string const & str) const
@@ -1494,7 +1579,7 @@
 	LyXLayout const & layout =
 		textclasslist[params.textclass][par->layout()];
 	LyXFont font = fn;
-
+	
 	par->checkInsertChar(font);
 	// insert the string, don't insert doublespace
 	bool space_inserted = true;
diff -Naur lyx-1.2.2-orig/src/buffer.h lyx-1.2.2/src/buffer.h
--- lyx-1.2.2-orig/src/buffer.h	2002-05-31 11:51:40.000000000 +0200
+++ lyx-1.2.2/src/buffer.h	2003-01-06 17:47:13.000000000 +0100
@@ -125,6 +125,9 @@
 	///
 	void insertStringAsLines(Paragraph *&, lyx::pos_type &,
 				 LyXFont const &, string const &) const;
+	/// This inserts the X selection into a tabular.
+	void insertStringAsTabular(Paragraph *&, lyx::pos_type &,
+				  LyXFont const &, string const &) const;
 #ifndef NO_COMPABILITY
 	///
 	Inset * isErtInset(Paragraph * par, int pos) const;
diff -Naur lyx-1.2.2-orig/src/BufferView_pimpl.C lyx-1.2.2/src/BufferView_pimpl.C
--- lyx-1.2.2-orig/src/BufferView_pimpl.C	2002-10-15 12:40:31.000000000 +0200
+++ lyx-1.2.2/src/BufferView_pimpl.C	2003-01-06 19:25:06.000000000 +0100
@@ -1549,6 +1549,25 @@
 	update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
 }
 
+void BufferView::Pimpl::pasteClipboardAsTabular()
+{
+	if (!buffer_)
+		return;
+
+	screen_->hideCursor();
+	beforeChange(bv_->text);
+
+	string const clip(workarea_.getClipboard());
+
+	if (clip.empty())
+		return;
+
+	bv_->getLyXText()->insertStringAsTabular(bv_, clip);
+	
+	bv_->getLyXText()->clearSelection();
+	update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
+}
+
 
 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
 {
@@ -1737,6 +1756,12 @@
 	}
 	break;
 
+	case LFUN_PASTESELECTION_TABULAR:
+	{
+		pasteClipboardAsTablular();
+	}
+	break;
+
 	case LFUN_CUT:
 		bv_->cut();
 		break;
diff -Naur lyx-1.2.2-orig/src/BufferView_pimpl.h lyx-1.2.2/src/BufferView_pimpl.h
--- lyx-1.2.2-orig/src/BufferView_pimpl.h	2002-04-23 15:35:43.000000000 +0200
+++ lyx-1.2.2/src/BufferView_pimpl.h	2003-01-06 19:28:55.000000000 +0100
@@ -179,6 +179,8 @@
 	Timeout cursor_timeout;
 	///
 	WorkArea workarea_;
+	/// cf. Buffer::insertStringAsTabular
+	void pasteClipboardAsTabular();
 	///
 	void pasteClipboard(bool asPara);
 	///
diff -Naur lyx-1.2.2-orig/src/commandtags.h lyx-1.2.2/src/commandtags.h
--- lyx-1.2.2-orig/src/commandtags.h	2002-11-28 15:21:04.000000000 +0100
+++ lyx-1.2.2/src/commandtags.h	2003-01-06 17:47:13.000000000 +0100
@@ -296,6 +296,7 @@
 	LFUN_FORKS_SHOW,                // Angus 16 Feb 2002
 	LFUN_FORKS_KILL,                // Angus 16 Feb 2002
 	LFUN_TOOLTIPS_TOGGLE,           // Angus 8 Mar 2002
+	LFUN_PASTESELECTION_TABULAR,    // Bruno 23 Dec 2002
 	LFUN_LASTACTION  /* this marks the end of the table */
 };
 
diff -Naur lyx-1.2.2-orig/src/LyXAction.C lyx-1.2.2/src/LyXAction.C
--- lyx-1.2.2-orig/src/LyXAction.C	2002-11-28 15:21:03.000000000 +0100
+++ lyx-1.2.2/src/LyXAction.C	2003-01-06 17:47:13.000000000 +0100
@@ -432,6 +432,7 @@
 		{ LFUN_FORKS_KILL, "kill-forks",
 		  N_("Kill the forked process with this PID"), NoBuffer },
 		{ LFUN_TOOLTIPS_TOGGLE, "toggle-tooltips", "", NoBuffer },
+		{ LFUN_PASTESELECTION_TABULAR, "primary-selection-paste-tab", "", Noop },
 		{ LFUN_NOACTION, "", "", Noop }
 	};
 
diff -Naur lyx-1.2.2-orig/src/lyxtext.h lyx-1.2.2/src/lyxtext.h
--- lyx-1.2.2-orig/src/lyxtext.h	2002-05-03 11:22:06.000000000 +0200
+++ lyx-1.2.2/src/lyxtext.h	2003-01-06 17:47:13.000000000 +0100
@@ -447,6 +447,8 @@
 	void replaceSelectionWithString(BufferView *, string const & str);
 
 	/// needed to insert the selection
+	void LyXText::insertStringAsTabular(BufferView *, string const &);
+	/// needed to insert the selection
 	void insertStringAsLines(BufferView *, string const & str);
 	/// needed to insert the selection
 	void insertStringAsParagraphs(BufferView *, string const & str);
diff -Naur lyx-1.2.2-orig/src/text2.C lyx-1.2.2/src/text2.C
--- lyx-1.2.2-orig/src/text2.C	2002-07-23 12:05:03.000000000 +0200
+++ lyx-1.2.2/src/text2.C	2003-01-06 17:47:13.000000000 +0100
@@ -1854,6 +1854,27 @@
 	unFreezeUndo();
 }
 
+// inserts strings into a tabular.
+void LyXText::insertStringAsTabular(BufferView * bview, string const & str)
+{
+	Paragraph * par = cursor.par();
+	pos_type pos = cursor.pos();
+	Paragraph * endpar = cursor.par()->next();
+
+	setCursorParUndo(bview);
+
+	// only to be sure, should not be neccessary
+	clearSelection();
+
+	bview->buffer()->insertStringAsTabular(par, pos, current_font, str);
+
+	redoParagraphs(bview, cursor, endpar);
+	setCursor(bview, cursor.par(), cursor.pos());
+	selection.cursor = cursor;
+	setCursor(bview, par, pos);
+	setSelection(bview);
+}
+
 
 // needed to insert the selection
 void LyXText::insertStringAsLines(BufferView * bview, string const & str)

Reply via email to