New inset for \nopagebreak

Pavel Sanda sanda at lyx.org
Sat Oct 10 22:24:52 UTC 2020


On Fri, Oct 09, 2020 at 06:12:01PM -0400, Richard Kimberly Heck wrote:
> On 10/9/20 5:34 PM, Pavel Sanda wrote:
> > On Thu, Oct 08, 2020 at 09:57:00PM -0400, Richard Kimberly Heck wrote:
> >>> Implementation-wise it could be just variant of page-break inset when you look
> >>> at the semantics, painting-wise it would belong to the newline inset. Or it
> >>> could be completely standalone inset. The semantic version seem most appropriate
> >>> to me. Opinions?
> >> No objection. I'd add it to InsetPageBreak.
> > That would be like the attached. First iteration without format changes yet.
> 
> Looks mostly fine. Two things. First, there's a lot of indentation
> weirdness. Second, I get the words "No page break" below the arrow
> symbols. Did you mean to return after the specialized drawing routine?

Indeed, return was removed while I was polishing the patch.
Whitespace issues hopefully fixed and format update included as well.

Pavel
-------------- next part --------------
diff --git a/development/FORMAT b/development/FORMAT
index 438ef08a1f..c8a237abf7 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 74f17cf11d..fafa8dde9d 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 fe6f269973..45bb578de0 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 e958178a7e..7a21069e20 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 f255ef2540..0cd3a4ca0a 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 6f89eac03b..7e91e689e0 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 e7f1126a8a..f02048804d 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 bfa9bcd908..e431f3665a 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 770c308362..f13766180b 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-devel mailing list