On Mon, Mar 24, 2008 at 12:31:15PM +0000, [EMAIL PROTECTED] wrote:
> Author: rgheck
> Date: Mon Mar 24 13:31:07 2008
> New Revision: 23913
> 
> URL: http://www.lyx.org/trac/changeset/23913
> Log:
> Add support for document-local layout information. GUI is to follow.

That's a tall order to do generally and safely.

For charstyles, I suggest a mechanism whereby the cursor points
to an existing lyxtext position with a set of character
attributes, and say "put these into a local layout". I had such 
a thing working at one point.

- Martin


> Modified:
>     lyx-devel/trunk/development/FORMAT
>     lyx-devel/trunk/lib/lyx2lyx/LyX.py
>     lyx-devel/trunk/lib/lyx2lyx/lyx_1_6.py
>     lyx-devel/trunk/src/Buffer.cpp
>     lyx-devel/trunk/src/BufferParams.cpp
>     lyx-devel/trunk/src/BufferParams.h
>     lyx-devel/trunk/src/TextClass.cpp
>     lyx-devel/trunk/src/TextClass.h
>     lyx-devel/trunk/src/frontends/qt4/GuiDocument.cpp
> 
> Modified: lyx-devel/trunk/development/FORMAT
> URL: http://www.lyx.org/trac/file/lyx-devel/trunk/development/FORMAT?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/development/FORMAT (original)
> +++ lyx-devel/trunk/development/FORMAT Mon Mar 24 13:31:07 2008
> @@ -1,5 +1,8 @@
>  LyX file-format changes
>  -----------------------
> +
> +2008-03-24 Richard Heck <[EMAIL PROTECTED]>
> +     * Format incremented to 322: local layout
>  
>  2008-03-18 Edwin Leuven <[EMAIL PROTECTED]>
>       * Format incremented to 321: drop row/col lines and ensure
> 
> Modified: lyx-devel/trunk/lib/lyx2lyx/LyX.py
> URL: http://www.lyx.org/trac/file/lyx-devel/trunk/lib/lyx2lyx/LyX.py?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/lib/lyx2lyx/LyX.py (original)
> +++ lyx-devel/trunk/lib/lyx2lyx/LyX.py Mon Mar 24 13:31:07 2008
> @@ -80,7 +80,7 @@
>                     ("1_3",     [221], minor_versions("1.3" , 7)),
>                     ("1_4", range(222,246), minor_versions("1.4" , 5)),
>                     ("1_5", range(246,277), minor_versions("1.5" , 2)),
> -                   ("1_6", range(277,322), minor_versions("1.6" , 0))]
> +                   ("1_6", range(277,323), minor_versions("1.6" , 0))] # 
> rgh: local layout
>  
>  
>  def formats_list():
> 
> Modified: lyx-devel/trunk/lib/lyx2lyx/lyx_1_6.py
> URL: 
> http://www.lyx.org/trac/file/lyx-devel/trunk/lib/lyx2lyx/lyx_1_6.py?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/lib/lyx2lyx/lyx_1_6.py (original)
> +++ lyx-devel/trunk/lib/lyx2lyx/lyx_1_6.py Mon Mar 24 13:31:07 2008
> @@ -1631,6 +1631,21 @@
>          'hspace*{\n\\backslash\nfill}\n\\end_layout\n\n\\end_inset\n\n')
>  
>  
> +def revert_local_layout(document):
> +    ' Revert local layout headers.'
> +    i = 0
> +    while True:
> +        i = find_token(document.header, "\\begin_local_layout", i)
> +        if i == -1:
> +            return
> +        j = find_end_of(document.header, i, "\\begin_local_layout", 
> "\\end_local_layout")
> +        if j == -1:
> +            # this should not happen
> +            break
> +        document.header[i : j + 1] = []
> +
> +
> +
>  ##
>  # Conversion hub
>  #
> @@ -1680,10 +1695,12 @@
>             [318, []],
>             [319, [convert_spaceinset, convert_hfill]],
>             [320, []],
> -           [321, [convert_tablines]]
> +           [321, [convert_tablines]],
> +           [322, []]
>            ]
>  
> -revert =  [[320, [revert_tablines]],
> +revert =  [[321, [revert_local_layout]],
> +           [320, [revert_tablines]],
>             [319, [revert_protected_hfill]],
>             [318, [revert_spaceinset, revert_hfills, revert_hspace]],
>             [317, [remove_extra_embedded_files]],
> 
> Modified: lyx-devel/trunk/src/Buffer.cpp
> URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/Buffer.cpp?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/src/Buffer.cpp (original)
> +++ lyx-devel/trunk/src/Buffer.cpp Mon Mar 24 13:31:07 2008
> @@ -116,7 +116,7 @@
>  
>  namespace {
>  
> -int const LYX_FORMAT = 321;
> +int const LYX_FORMAT = 322; // rgh: local layout
>  
>  typedef map<string, bool> DepClean;
>  typedef map<docstring, pair<InsetLabel const *, Buffer::References> > 
> RefCache;
> 
> Modified: lyx-devel/trunk/src/BufferParams.cpp
> URL: 
> http://www.lyx.org/trac/file/lyx-devel/trunk/src/BufferParams.cpp?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/src/BufferParams.cpp (original)
> +++ lyx-devel/trunk/src/BufferParams.cpp Mon Mar 24 13:31:07 2008
> @@ -506,6 +506,8 @@
>               
>       } else if (token == "\\begin_preamble") {
>               readPreamble(lex);
> +     } else if (token == "\\begin_local_layout") {
> +             readLocalLayout(lex);
>       } else if (token == "\\begin_modules") {
>               readModules(lex);
>       } else if (token == "\\options") {
> @@ -722,6 +724,15 @@
>               os << "\\end_modules" << '\n';
>       }
>  
> +     // local layout information
> +     if (!local_layout.empty()) {
> +             // remove '\n' from the end 
> +             string const tmplocal = rtrim(local_layout, "\n");
> +             os << "\\begin_local_layout\n"
> +                << tmplocal
> +                << "\n\\end_local_layout\n";
> +     }
> +
>       // then the text parameters
>       if (language != ignore_language)
>               os << "\\language " << language->lang() << '\n';
> @@ -1501,6 +1512,12 @@
>                       frontend::Alert::warning(_("Read Error"), msg);
>               }
>       }
> +     if (!local_layout.empty()) {
> +             if (!doc_class_->read(local_layout, TextClass::MODULE)) {
> +                     docstring const msg = _("Error reading internal layout 
> information");
> +                     frontend::Alert::warning(_("Read Error"), msg);
> +             }
> +     }
>  }
>  
>  
> @@ -1552,6 +1569,16 @@
>                       "consistency check failed." << endl;
>  
>       preamble = lex.getLongString("\\end_preamble");
> +}
> +
> +
> +void BufferParams::readLocalLayout(Lexer & lex)
> +{
> +     if (lex.getString() != "\\begin_local_layout")
> +             lyxerr << "Error (BufferParams::readLocalLayout):"
> +                     "consistency check failed." << endl;
> +
> +     local_layout = lex.getLongString("\\end_local_layout");
>  }
>  
>  
> 
> Modified: lyx-devel/trunk/src/BufferParams.h
> URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/BufferParams.h?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/src/BufferParams.h (original)
> +++ lyx-devel/trunk/src/BufferParams.h Mon Mar 24 13:31:07 2008
> @@ -220,6 +220,8 @@
>       ///
>       std::string preamble;
>       ///
> +     std::string local_layout;
> +     ///
>       std::string options;
>       ///
>       std::string float_placement;
> @@ -316,6 +318,8 @@
>       ///
>       void readPreamble(Lexer &);
>       ///
> +     void readLocalLayout(Lexer &);
> +     ///
>       void readLanguage(Lexer &);
>       ///
>       void readGraphicsDriver(Lexer &);
> 
> Modified: lyx-devel/trunk/src/TextClass.cpp
> URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/TextClass.cpp?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/src/TextClass.cpp (original)
> +++ lyx-devel/trunk/src/TextClass.cpp Mon Mar 24 13:31:07 2008
> @@ -36,6 +36,7 @@
>  #include "support/os.h"
>  
>  #include <algorithm>
> +#include <fstream>
>  #include <sstream>
>  
>  #include "boost/assert.hpp"
> @@ -101,6 +102,8 @@
>               return "input file";
>       case TextClass::MODULE:
>               return "module file";
> +     case TextClass::VALIDATION:
> +             return "validation";
>       }
>       // shutup warning
>       return string();
> @@ -180,14 +183,7 @@
>  };
>  
>  
> -// Reads a textclass structure from file.
> -bool TextClass::read(FileName const & filename, ReadType rt)
> -{
> -     if (!filename.isReadableFile()) {
> -             lyxerr << "Cannot read layout file `" << filename << "'."
> -                    << endl;
> -             return false;
> -     }
> +namespace {
>  
>       keyword_item textClassTags[] = {
>               { "classoptions",    TC_CLASSOPTIONS },
> @@ -216,6 +212,28 @@
>               { "titlelatextype",  TC_TITLELATEXTYPE },
>               { "tocdepth",        TC_TOCDEPTH }
>       };
> +     
> +} //namespace anon
> +
> +
> +bool TextClass::convertLayoutFormat(support::FileName const & filename, 
> ReadType rt)
> +{
> +     LYXERR(Debug::TCLASS, "Converting layout file to " << FORMAT);
> +             FileName const tempfile = FileName::tempName();
> +             bool success = layout2layout(filename, tempfile);
> +             if (success)
> +                     success = read(tempfile, rt);
> +             tempfile.removeFile();
> +             return success;
> +}
> +
> +bool TextClass::read(FileName const & filename, ReadType rt)
> +{
> +     if (!filename.isReadableFile()) {
> +             lyxerr << "Cannot read layout file `" << filename << "'."
> +                    << endl;
> +             return false;
> +     }
>  
>       LYXERR(Debug::TCLASS, "Reading " + translateRT(rt) + ": " +
>               to_utf8(makeDisplayPath(filename.absFilename())));
> @@ -223,7 +241,7 @@
>       // Define the `empty' layout used in table cells, ert, etc. Note that 
>       // we do this before loading any layout file, so that classes can 
>       // override features of this layout if they should choose to do so.
> -     if (rt == BASECLASS) {
> +     if (rt == BASECLASS && !hasLayout(emptylayout_)) {
>               static char const * s = "Margin Static\n"
>                       "LatexType Paragraph\n"
>                       "LatexName dummy\n"
> @@ -243,11 +261,71 @@
>               };
>               layoutlist_.push_back(lay);
>       }
> -
>       Lexer lexrc(textClassTags,
>               sizeof(textClassTags) / sizeof(textClassTags[0]));
>  
>       lexrc.setFile(filename);
> +     ReturnValues retval = read(lexrc, rt);
> +     
> +     LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
> +                     to_utf8(makeDisplayPath(filename.absFilename())));
> +     
> +     if (retval != FORMAT_MISMATCH) 
> +             return retval == OK;
> +     
> +     bool const worx = convertLayoutFormat(filename, rt);
> +     if (!worx) {
> +             lyxerr << "Unable to convert " << filename << 
> +                     " to format " << FORMAT << std::endl;
> +             return false;
> +     }
> +     return true;
> +}
> +
> +
> +bool TextClass::validate(std::string const & str)
> +{
> +     TextClass tc;
> +     return tc.read(str, VALIDATION);
> +}
> +
> +
> +bool TextClass::read(std::string const & str, ReadType rt) 
> +{
> +     Lexer lexrc(textClassTags,
> +             sizeof(textClassTags) / sizeof(textClassTags[0]));
> +     istringstream is(str);
> +     lexrc.setStream(is);
> +     ReturnValues retval = read(lexrc, rt);
> +
> +     if (retval != FORMAT_MISMATCH) 
> +             return retval == OK;
> +
> +     // write the layout string to a temporary file
> +     FileName const tempfile = FileName::tempName();
> +     ofstream os(tempfile.toFilesystemEncoding().c_str());
> +     if (!os) {
> +             lyxerr << "Unable to create tempoary file in TextClass::read!!" 
> +                     << std::endl;
> +             return false;
> +     }
> +     os << str;
> +     os.close();
> +
> +     // now try to convert it
> +     bool const worx = convertLayoutFormat(tempfile, rt);
> +     if (!worx) {
> +             lyxerr << "Unable to convert internal layout information to 
> format " 
> +                     << FORMAT << std::endl;
> +     }
> +     tempfile.removeFile();
> +     return worx;
> +}
> +
> +
> +// Reads a textclass structure from file.
> +TextClass::ReturnValues TextClass::read(Lexer & lexrc, ReadType rt) 
> +{
>       bool error = !lexrc.isOK();
>  
>       // Format of files before the 'Format' tag was introduced
> @@ -316,9 +394,8 @@
>                                               + lexrc.getString() + " is 
> probably not valid UTF-8!";
>                                       lexrc.printError(s.c_str());
>                                       Layout lay;
> -                                     //FIXME If we're just dropping this 
> layout, do we really
> -                                     //care whether there's an error?? Or 
> should we just set
> -                                     //error to true, since we couldn't even 
> read the name?
> +                                     // Since we couldn't read the name, we 
> just scan the rest
> +                                     // of the style and discard it.
>                                       error = !readStyle(lexrc, lay);
>                               } else if (hasLayout(name)) {
>                                       Layout & lay = operator[](name);
> @@ -482,22 +559,11 @@
>                       break;
>       }
>  
> -     if (format != FORMAT) {
> -             LYXERR(Debug::TCLASS, "Converting layout file from format "
> -                                   << format << " to " << FORMAT);
> -             FileName const tempfile = FileName::tempName();
> -             bool success = layout2layout(filename, tempfile);
> -             if (success)
> -                      read(tempfile, rt);
> -             tempfile.removeFile();
> -             return success;
> -     }
> -
> -     LYXERR(Debug::TCLASS, "Finished reading " + translateRT(rt) + ": " +
> -                     to_utf8(makeDisplayPath(filename.absFilename())));
> +     if (format != FORMAT)
> +             return FORMAT_MISMATCH;
>  
>       if (rt != BASECLASS) 
> -             return !error;
> +             return (error ? ERROR : OK);
>  
>       if (defaultlayout_.empty()) {
>               lyxerr << "Error: Textclass '" << name_
> @@ -547,7 +613,7 @@
>       LYXERR(Debug::TCLASS, "Minimum TocLevel is " << min_toclevel_
>               << ", maximum is " << max_toclevel_);
>  
> -     return !error;
> +     return (error ? ERROR : OK);
>  }
>  
>  
> 
> Modified: lyx-devel/trunk/src/TextClass.h
> URL: http://www.lyx.org/trac/file/lyx-devel/trunk/src/TextClass.h?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/src/TextClass.h (original)
> +++ lyx-devel/trunk/src/TextClass.h Mon Mar 24 13:31:07 2008
> @@ -127,11 +127,25 @@
>       enum ReadType { 
>               BASECLASS, //>This is a base class, i.e., top-level layout file
>               MERGE, //>This is a file included in a layout file
> -             MODULE //>This is a layout module
> +             MODULE, //>This is a layout module
> +             VALIDATION //>We're just validating
>       };
> +     /// return values for read()
> +     enum ReturnValues {
> +             OK,
> +             ERROR,
> +             FORMAT_MISMATCH
> +     };
> +
>       /// Performs the read of the layout file.
>       /// \return true on success.
>       bool read(support::FileName const & filename, ReadType rt = BASECLASS);
> +     ///
> +     bool read(std::string const & str, ReadType rt = BASECLASS);
> +     ///
> +     ReturnValues read(Lexer & lex, ReadType rt = BASECLASS);
> +     /// validates the layout information passed in str
> +     static bool validate(std::string const & str);
>  
>       ///////////////////////////////////////////////////////////////////
>       // loading
> @@ -242,6 +256,8 @@
>       ///////////////////////////////////////////////////////////////////
>       ///
>       bool deleteLayout(docstring const &);
> +     ///
> +     bool convertLayoutFormat(support::FileName const &, ReadType);
>       /// \return true for success.
>       bool readStyle(Lexer &, Layout &);
>       ///
> 
> Modified: lyx-devel/trunk/src/frontends/qt4/GuiDocument.cpp
> URL: 
> http://www.lyx.org/trac/file/lyx-devel/trunk/src/frontends/qt4/GuiDocument.cpp?rev=23913
> ==============================================================================
> --- lyx-devel/trunk/src/frontends/qt4/GuiDocument.cpp (original)
> +++ lyx-devel/trunk/src/frontends/qt4/GuiDocument.cpp Mon Mar 24 13:31:07 2008
> @@ -1296,6 +1296,10 @@
>                       return;
>               }
>       }
> +     // FIXME Note that by doing things this way, we load the TextClass
> +     // as soon as it is selected. So, if you use the scroll wheel when
> +     // sitting on the combo box, we'll load a lot of TextClass objects
> +     // very quickly. This could be changed.
>       if (!bp_.setBaseClass(classname)) {
>               Alert::error(_("Error"), _("Unable to set document class."));
>               return;
> 
> 
> _______________________________________________
> Cvslog mailing list
> [EMAIL PROTECTED]
> http://www.lyx.org/mailman/listinfo/cvslog
> 

Reply via email to