[LyX/master] Add inset for \nopagebreak.

Pavel Sanda sanda at lyx.org
Tue Oct 13 16:50:14 UTC 2020


commit 0ea9df7467a9b77bface87cbddb9731574b017e4
Author: Pavel Sanda <sanda at lyx.org>
Date:   Tue Oct 13 19:13:59 2020 +0200

    Add inset for \nopagebreak.
    
    https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg213388.html
---
 development/FORMAT          |    3 ++
 lib/lyx2lyx/lyx_2_4.py      |   19 +++++++++-
 lib/ui/stdcontext.inc       |    1 +
 lib/ui/stdmenus.inc         |    1 +
 src/factory.cpp             |    2 +
 src/insets/InsetNewpage.cpp |   76 +++++++++++++++++++++++++++++++++++++++++-
 src/insets/InsetNewpage.h   |    6 ++-
 src/tex2lyx/text.cpp        |    3 +-
 src/version.h               |    4 +-
 9 files changed, 106 insertions(+), 9 deletions(-)

diff --git a/development/FORMAT b/development/FORMAT
index 438ef08..c8a237a 100644
--- a/development/FORMAT
+++ b/development/FORMAT
@@ -7,6 +7,9 @@ changes happened in particular if possible. A good example would be
 
 -----------------------
 
+2020-10-10 Pavel Sanda <sanda at lyx.org>
+	* Format incremented to 599: Add inset for \nopagebreak macro (part of InseNewPage now).
+
 2020-07-14 Thibaut Cuvelier <tcuvelier at lyx.org>
  	* Format incremented to 598: DocBook can export to HTML and CALS tables, with the parameter
           \docbook_table_output (0: HTML, only choice previously; CALS: 1).
diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py
index 74f17cf..fafa8dd 100644
--- a/lib/lyx2lyx/lyx_2_4.py
+++ b/lib/lyx2lyx/lyx_2_4.py
@@ -3958,6 +3958,19 @@ def revert_docbook_table_output(document):
         del document.header[i]
 
 
+def revert_nopagebreak(document):
+    while True:
+        i = find_token(document.body, "\\begin_inset Newpage nopagebreak")
+        if i == -1:
+            return
+        end = find_end_of_inset(document.body, i)
+        if end == 1:
+            document.warning("Malformed LyX document: Could not find end of Newpage inset.")
+            continue
+        subst = put_cmd_in_ert("\\nopagebreak{}")
+        document.body[i : end + 1] = subst
+
+
 ##
 # Conversion hub
 #
@@ -4017,10 +4030,12 @@ convert = [
            [595, []],
            [596, [convert_parskip]],
            [597, [convert_libertinus_rm_fonts]],
-           [598, []]
+           [598, []],
+           [599, []]
           ]
 
-revert =  [[597, [revert_docbook_table_output]],
+revert =  [[598, [revert_nopagebreak]],
+           [597, [revert_docbook_table_output]],
            [596, [revert_libertinus_rm_fonts,revert_libertinus_sftt_fonts]],
            [595, [revert_parskip,revert_line_vspaces]],
            [594, [revert_ams_spaces]],
diff --git a/lib/ui/stdcontext.inc b/lib/ui/stdcontext.inc
index fe6f269..45bb578 100644
--- a/lib/ui/stdcontext.inc
+++ b/lib/ui/stdcontext.inc
@@ -322,6 +322,7 @@ Menuset
 	Menu "context-newpage"
 		Item "New Page|N" "inset-modify newpage newpage"
 		Item "Page Break|a" "inset-modify newpage pagebreak"
+		Item "No Page Break|g" "inset-modify newpage nopagebreak"
 		Item "Clear Page|C" "inset-modify newpage clearpage"
 		Item "Clear Double Page|D" "inset-modify newpage cleardoublepage"
 	End
diff --git a/lib/ui/stdmenus.inc b/lib/ui/stdmenus.inc
index e958178..7a21069 100644
--- a/lib/ui/stdmenus.inc
+++ b/lib/ui/stdmenus.inc
@@ -467,6 +467,7 @@ Menuset
 		Separator
 		Item "New Page|N" "newpage-insert newpage"
 		Item "Page Break|a" "newpage-insert pagebreak"
+		Item "No Page Break|g" "newpage-insert nopagebreak"
 		Item "Clear Page|C" "newpage-insert clearpage"
 		Item "Clear Double Page|D" "newpage-insert cleardoublepage"
 	End
diff --git a/src/factory.cpp b/src/factory.cpp
index f255ef2..0cd3a4c 100644
--- a/src/factory.cpp
+++ b/src/factory.cpp
@@ -99,6 +99,8 @@ Inset * createInsetHelper(Buffer * buf, FuncRequest const & cmd)
 				inp.kind = InsetNewpageParams::CLEARPAGE;
 			else if (name == "cleardoublepage")
 				inp.kind = InsetNewpageParams::CLEARDOUBLEPAGE;
+			else if (name == "nopagebreak")
+				inp.kind = InsetNewpageParams::NOPAGEBREAK;
 			return new InsetNewpage(inp);
 		}
 
diff --git a/src/insets/InsetNewpage.cpp b/src/insets/InsetNewpage.cpp
index 6f89eac..7e91e68 100644
--- a/src/insets/InsetNewpage.cpp
+++ b/src/insets/InsetNewpage.cpp
@@ -61,6 +61,9 @@ void InsetNewpageParams::write(ostream & os) const
 	case InsetNewpageParams::CLEARDOUBLEPAGE:
 		os <<  "cleardoublepage";
 		break;
+	case InsetNewpageParams::NOPAGEBREAK:
+		os <<  "nopagebreak";
+		break;
 	}
 }
 
@@ -79,6 +82,8 @@ void InsetNewpageParams::read(Lexer & lex)
 		kind = InsetNewpageParams::CLEARPAGE;
 	else if (token == "cleardoublepage")
 		kind = InsetNewpageParams::CLEARDOUBLEPAGE;
+	else if (token == "nopagebreak")
+		kind = InsetNewpageParams::NOPAGEBREAK;
 	else
 		lex.printError("Unknown kind");
 }
@@ -100,6 +105,14 @@ void InsetNewpage::read(Lexer & lex)
 
 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
 {
+	if (params_.kind == InsetNewpageParams::NOPAGEBREAK) {
+		frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
+		dim.asc = fm.maxAscent();
+	        dim.des = fm.maxDescent();
+	        dim.wid = 3 * fm.width('n');
+		return;
+	}
+
 	dim.asc = defaultRowHeight();
 	dim.des = defaultRowHeight();
 	dim.wid = mi.base.textwidth;
@@ -108,6 +121,54 @@ void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
 
 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
 {
+	if (params_.kind == InsetNewpageParams::NOPAGEBREAK) {
+
+	        FontInfo font;
+	        font.setColor(ColorName());
+
+	        frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
+	        int const wid = 3 * fm.width('n');
+	        int const asc = fm.maxAscent();
+
+		int xp[3];
+	        int yp[3];
+
+		//left side arrow
+		yp[0] = int(y - 0.875 * asc * 0.75);
+		yp[1] = int(y - 0.500 * asc * 0.75);
+		yp[2] = int(y - 0.125 * asc * 0.75);
+		xp[0] = int(x + wid * 0.25);
+		xp[1] = int(x + wid * 0.4); 
+		xp[2] = int(x + wid * 0.25);
+		pi.pain.lines(xp, yp, 3, ColorName());
+
+		yp[0] = yp[1] = int(y - 0.500 * asc * 0.75);
+		xp[0] = int(x + wid * 0.03);
+		xp[1] = int(x + wid * 0.4); 
+		pi.pain.lines(xp, yp, 2, ColorName());
+
+		//right side arrow
+		yp[0] = int(y - 0.875 * asc * 0.75);
+		yp[1] = int(y - 0.500 * asc * 0.75);
+		yp[2] = int(y - 0.125 * asc * 0.75);
+		xp[0] = int(x + wid * 0.75);
+		xp[1] = int(x + wid * 0.6); 
+		xp[2] = int(x + wid * 0.75);
+		pi.pain.lines(xp, yp, 3, ColorName());
+
+		yp[0] = yp[1] = int(y - 0.500 * asc * 0.75);
+		xp[0] = int(x + wid * 0.97);
+		xp[1] = int(x + wid * 0.6); 
+		pi.pain.lines(xp, yp, 2, ColorName());
+
+		//mid-rule
+		xp[0] = xp[1] = int(x + wid * 0.5);;
+		yp[0] = int(y - 0.875 * asc * 0.75);
+		yp[1] = int(y - 0.125 * asc * 0.75);
+		pi.pain.lines(xp, yp, 2, ColorName());
+		return;
+	}
+
 	using frontend::Painter;
 
 	FontInfo font;
@@ -187,6 +248,9 @@ docstring InsetNewpage::insetLabel() const
 		case InsetNewpageParams::CLEARDOUBLEPAGE:
 			return _("Clear Double Page");
 			break;
+		case InsetNewpageParams::NOPAGEBREAK:
+			return _("No Page Break");
+			break;
 		default:
 			return _("New Page");
 			break;
@@ -198,6 +262,7 @@ ColorCode InsetNewpage::ColorName() const
 {
 	switch (params_.kind) {
 		case InsetNewpageParams::PAGEBREAK:
+		case InsetNewpageParams::NOPAGEBREAK:
 			return Color_pagebreak;
 			break;
 		case InsetNewpageParams::NEWPAGE:
@@ -232,6 +297,9 @@ void InsetNewpage::latex(otexstream & os, OutputParams const & runparams) const
 		case InsetNewpageParams::CLEARDOUBLEPAGE:
 			os << "\\cleardoublepage" << termcmd;
 			break;
+		case InsetNewpageParams::NOPAGEBREAK:
+			os << "\\nopagebreak" << termcmd;
+			break;
 		default:
 			os << "\\newpage" << termcmd;
 			break;
@@ -243,6 +311,8 @@ void InsetNewpage::latex(otexstream & os, OutputParams const & runparams) const
 int InsetNewpage::plaintext(odocstringstream & os,
         OutputParams const &, size_t) const
 {
+	if (params_.kind ==  InsetNewpageParams::NOPAGEBREAK)
+		return 0;
 	os << '\n';
 	return PLAINTEXT_NEWLINE;
 }
@@ -250,13 +320,15 @@ int InsetNewpage::plaintext(odocstringstream & os,
 
 void InsetNewpage::docbook(XMLStream & os, OutputParams const &) const
 {
-	os << xml::CR();
+	if (params_.kind !=  InsetNewpageParams::NOPAGEBREAK)
+		os << xml::CR();
 }
 
 
 docstring InsetNewpage::xhtml(XMLStream & xs, OutputParams const &) const
 {
-	xs << xml::CompTag("br");
+	if (params_.kind !=  InsetNewpageParams::NOPAGEBREAK)
+		xs << xml::CompTag("br");
 	return docstring();
 }
 
diff --git a/src/insets/InsetNewpage.h b/src/insets/InsetNewpage.h
index e7f1126..f020488 100644
--- a/src/insets/InsetNewpage.h
+++ b/src/insets/InsetNewpage.h
@@ -29,7 +29,9 @@ public:
 		///
 		CLEARPAGE,
 		///
-		CLEARDOUBLEPAGE
+		CLEARDOUBLEPAGE,
+		///
+		NOPAGEBREAK
 	};
 	///
 	InsetNewpageParams() : kind(NEWPAGE) {}
@@ -74,7 +76,7 @@ private:
 	///
 	void write(std::ostream & os) const override;
 	///
-	RowFlags rowFlags() const override { return Display; }
+	RowFlags rowFlags() const override { return (params_.kind == InsetNewpageParams::NOPAGEBREAK) ? Inline : Display; }
 	///
 	docstring insetLabel() const;
 	///
diff --git a/src/tex2lyx/text.cpp b/src/tex2lyx/text.cpp
index bfa9bcd..e431f36 100644
--- a/src/tex2lyx/text.cpp
+++ b/src/tex2lyx/text.cpp
@@ -5593,7 +5593,8 @@ void parse_text(Parser & p, ostream & os, unsigned flags, bool outer,
 		if (t.cs() == "newpage" ||
 		    (t.cs() == "pagebreak" && !p.hasOpt()) ||
 		    t.cs() == "clearpage" ||
-		    t.cs() == "cleardoublepage") {
+		    t.cs() == "cleardoublepage" ||
+		    t.cs() == "nopagebreak") {
 			context.check_layout(os);
 			begin_inset(os, "Newpage ");
 			os << t.cs();
diff --git a/src/version.h b/src/version.h
index 770c308..f137661 100644
--- a/src/version.h
+++ b/src/version.h
@@ -32,8 +32,8 @@ extern char const * const lyx_version_info;
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-#define LYX_FORMAT_LYX 598 // tcuvelier: DocBook tables
-#define LYX_FORMAT_TEX2LYX 598
+#define LYX_FORMAT_LYX 599 // sanda: nobreakpage
+#define LYX_FORMAT_TEX2LYX 599
 
 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
 #ifndef _MSC_VER


More information about the lyx-cvs mailing list