This patch addresses 2714, for which there was an earlier patch, but this patch proceeds more generally, in accord with some comments by JMarc. The original request was to be able to set paragraph alignment via an LFUN. This was not possible using paragraph-params-apply, because that overrides all other settings. This patch introduces paragraph-params, which does the same thing but DOESN'T override existing settings.
The basic idea is pretty simple, but the code needed some cleanup before it would work at all elegantly.
I've tested the patch, and it seems to work. Comments welcome. Seeking to commit to trunk and, I suppose, also to branch.
Richard -- ================================================================== Richard G Heck, Jr Professor of Philosophy Brown University http://frege.brown.edu/heck/ ================================================================== Get my public key from http://sks.keyserver.penguin.de Hash: 0x1DE91F1E66FFBDEC Learn how to sign your email using Thunderbird and GnuPG at: http://dudu.dyn.2-h.org/nist/gpg-enigmail-howto
Index: src/lfuns.h =================================================================== --- src/lfuns.h (revision 19362) +++ src/lfuns.h (working copy) @@ -379,6 +379,8 @@ LFUN_LISTING_INSERT, // Herbert 20011110, bpeng 20070502 LFUN_TOOLBAR_TOGGLE, // Edwin 20070521 LFUN_BUFFER_WRITE_ALL, // rgh, gpothier 200707XX + //290 + LFUN_PARAGRAPH_PARAMS, // rgh, 200708XX LFUN_LASTACTION // end of the table }; Index: src/LyXAction.cpp =================================================================== --- src/LyXAction.cpp (revision 19362) +++ src/LyXAction.cpp (working copy) @@ -336,6 +336,7 @@ { LFUN_INSET_MODIFY, "", Noop }, { LFUN_INSET_DIALOG_UPDATE, "", Noop }, { LFUN_INSET_SETTINGS, "inset-settings", ReadOnly }, + { LFUN_PARAGRAPH_PARAMS, "paragraph-params", Noop }, { LFUN_PARAGRAPH_PARAMS_APPLY, "paragraph-params-apply", Noop }, { LFUN_PARAGRAPH_UPDATE, "", Noop }, { LFUN_EXTERNAL_EDIT, "external-edit", Noop }, Index: src/Text.h =================================================================== --- src/Text.h (revision 19362) +++ src/Text.h (working copy) @@ -269,12 +269,13 @@ always in the first physical paragraph, the bottom settings in the last. When a paragraph is broken, the top settings rest, the bottom settings are given to the new one. + This function will handle a multi-paragraph selection. */ - void setParagraph(Cursor & cur, - Spacing const & spacing, - LyXAlignment align, - docstring const & labelwidthstring, - bool noindent); + void setParagraphs(Cursor & cur, docstring arg, bool modify = false); + /// Sets parameters for current or selected paragraphs + void setParagraphs(Cursor & cur, ParagraphParameters const & p); + /// Similar, but handles only one paragraph. + void setParagraph(Paragraph & par, ParagraphParameters const & p); /* these things are for search and replace */ Index: src/Text2.cpp =================================================================== --- src/Text2.cpp (revision 19362) +++ src/Text2.cpp (working copy) @@ -29,6 +29,7 @@ #include "BufferView.h" #include "bufferview_funcs.h" #include "Bullet.h" +#include "Color.h" #include "CoordCache.h" #include "Cursor.h" #include "CutAndPaste.h" @@ -38,7 +39,7 @@ #include "FuncRequest.h" #include "gettext.h" #include "Language.h" -#include "Color.h" +#include "Lexer.h" #include "LyXFunc.h" #include "LyXRC.h" #include "Row.h" @@ -72,8 +73,8 @@ using std::string; using std::max; using std::min; +using std::istringstream; - Text::Text() : current_font(Font::ALL_INHERIT), background_color_(Color::background), @@ -633,27 +634,65 @@ } -void Text::setParagraph(Cursor & cur, - Spacing const & spacing, LyXAlignment align, - docstring const & labelwidthstring, bool noindent) +void Text::setParagraphs(Cursor & cur, docstring arg, bool modify) { BOOST_ASSERT(cur.text()); // make sure that the depth behind the selection are restored, too pit_type undopit = undoSpan(cur.selEnd().pit()); recUndo(cur, cur.selBegin().pit(), undopit - 1); + //lyxerr << to_utf8(arg) << std::endl; + ParagraphParameters params; + if (!modify) { + //move this outside the loop if we're doing the same + //thing every time + istringstream is(to_utf8(arg)); + Lexer lex(0, 0); + lex.setStream(is); + params.read(lex); + } for (pit_type pit = cur.selBegin().pit(), end = cur.selEnd().pit(); pit <= end; ++pit) { + if (modify) { + istringstream is(to_utf8(arg)); + Lexer lex(0, 0); + lex.setStream(is); + params = pars_[pit].params(); + params.read(lex); + } + setParagraph(pars_[pit], params); + } +} + + +//FIXME This is a little redundant now, but it's probably worth keeping, +//especially if we're going to go away from using serialization internally +//quite so much. +void Text::setParagraphs(Cursor & cur, ParagraphParameters const & p) +{ + BOOST_ASSERT(cur.text()); + // make sure that the depth behind the selection are restored, too + pit_type undopit = undoSpan(cur.selEnd().pit()); + recUndo(cur, cur.selBegin().pit(), undopit - 1); + + for (pit_type pit = cur.selBegin().pit(), end = cur.selEnd().pit(); + pit <= end; ++pit) { Paragraph & par = pars_[pit]; - ParagraphParameters & params = par.params(); - params.spacing(spacing); + setParagraph(par, p); + } +} - // does the layout allow the new alignment? - if (align & par.layout()->alignpossible) - params.align(align); - par.setLabelWidthString(labelwidthstring); - params.noindent(noindent); - } + +void Text::setParagraph(Paragraph & par, ParagraphParameters const & p) +{ + ParagraphParameters & params = par.params(); + params.spacing(p.spacing()); + + // does the layout allow the new alignment? + if (p.align() & par.layout()->alignpossible) + params.align(p.align()); + par.setLabelWidthString(p.labelWidthString()); + params.noindent(p.noindent()); } Index: src/Text3.cpp =================================================================== --- src/Text3.cpp (revision 19362) +++ src/Text3.cpp (working copy) @@ -690,6 +690,9 @@ break; } + // TODO + // With the creation of LFUN_PARAGRAPH_PARAMS, this is now redundant, + // as its duties can be performed there. Should it be removed?? case LFUN_PARAGRAPH_SPACING: { Paragraph & par = cur.paragraph(); Spacing::Space cur_spacing = par.params().spacing().getSpace(); @@ -1531,7 +1534,8 @@ } setLayout(cur, tclass.defaultLayoutName()); - setParagraph(cur, Spacing(), LYX_ALIGN_LAYOUT, docstring(), 0); + ParagraphParameters p; + setParagraphs(cur, p); insertInset(cur, new InsetFloatList(to_utf8(cmd.argument()))); cur.posRight(); } else { @@ -1569,20 +1573,24 @@ case LFUN_PARAGRAPH_PARAMS_APPLY: { // Given data, an encoding of the ParagraphParameters // generated in the Paragraph dialog, this function sets - // the current paragraph appropriately. - istringstream is(to_utf8(cmd.argument())); - Lexer lex(0, 0); - lex.setStream(is); - ParagraphParameters params; - params.read(lex); - setParagraph(cur, - params.spacing(), - params.align(), - params.labelWidthString(), - params.noindent()); + // the current paragraph, or currently selected paragraphs, + // appropriately. + // NOTE: This function overrides all existing settings. + setParagraphs(cur, cmd.argument()); cur.message(_("Paragraph layout set")); break; } + + case LFUN_PARAGRAPH_PARAMS: { + // Given data, an encoding of the ParagraphParameters as we'd + // find them in a LyX file, this function modifies the current paragraph, + // or currently selected paragraphs. + // NOTE: This function only modifies, and does not override, existing + // settings. + setParagraphs(cur, cmd.argument(), true); + cur.message(_("Paragraph layout set")); + break; + } case LFUN_ESCAPE: if (cur.selection()) { @@ -1998,6 +2006,7 @@ case LFUN_ACCENT_OGONEK: case LFUN_THESAURUS_ENTRY: case LFUN_PARAGRAPH_PARAMS_APPLY: + case LFUN_PARAGRAPH_PARAMS: case LFUN_ESCAPE: case LFUN_BUFFER_END: case LFUN_BUFFER_BEGIN: Index: src/ParagraphParameters.cpp =================================================================== --- src/ParagraphParameters.cpp (revision 19362) +++ src/ParagraphParameters.cpp (working copy) @@ -37,19 +37,12 @@ namespace lyx { +//NOTE The order of these MUST be the same as in Layout.h. static char const * const string_align[] = { - "block", "left", "right", "center", "" + "block", "left", "right", "center", "default", "" }; -static int findToken(char const * const str[], string const & search_token) -{ - return search_token == "default" ? - 0 : - support::findToken(str, search_token); -} - - ParagraphParameters::ParagraphParameters() : noindent_(false), start_of_appendix_(false), appendix_(false), @@ -196,6 +189,9 @@ if (token == "\\noindent") { noindent(true); + } else if (token == "\\indent") { + //not found in LyX files but can be used with lfuns + noindent(false); } else if (token == "\\leftindent") { lex.next(); Length value(lex.getString()); @@ -205,7 +201,10 @@ } else if (token == "\\paragraph_spacing") { lex.next(); string const tmp = rtrim(lex.getString()); - if (tmp == "single") { + if (tmp == "default") { + //not found in LyX files but can be used with lfuns + spacing(Spacing(Spacing::Default)); + } else if (tmp == "single") { spacing(Spacing(Spacing::Single)); } else if (tmp == "onehalf") { spacing(Spacing(Spacing::Onehalf)); @@ -220,7 +219,7 @@ } } else if (token == "\\align") { lex.next(); - int tmpret = findToken(string_align, lex.getString()); + int tmpret = support::findToken(string_align, lex.getString()); if (tmpret == -1) ++tmpret; align(LyXAlignment(1 << tmpret)); Index: src/insets/InsetERT.cpp =================================================================== --- src/insets/InsetERT.cpp (revision 19362) +++ src/insets/InsetERT.cpp (working copy) @@ -349,6 +349,7 @@ case LFUN_URL_INSERT: case LFUN_FLOAT_LIST: case LFUN_INSET_INSERT: + case LFUN_PARAGRAPH_PARAMS: case LFUN_PARAGRAPH_PARAMS_APPLY: case LFUN_PARAGRAPH_UPDATE: case LFUN_NOMENCL_INSERT: