On Mon, Mar 13, 2006 at 08:36:33PM +0200, Martin Vermeer wrote:
> On Mon, Mar 13, 2006 at 06:06:10PM +0000, Angus Leeming wrote:
> > Rainer Dorsch <[EMAIL PROTECTED]> writes:
  
 ...
  
> > Interesting questions:
> > Presumably there will be char styles associated with the layout. Could there
> > also be char styles specified by the user on a per-document basis?
> 
> Yes, I have something like this in my local tree. The per-document
> charstyles are defined in the minibuffer and take the font at the cursor
> location as the inset's font definition. (A GUI for defining them rather
> than the minibuffer would be nice, but I'm sure this already will make
> many people happy.)
> 
> These per-document charstyles output the same font to the screen and
> to the LaTeX file. They have initially no displayed label (Jean-Marc
> objected to their real-estate demand when I proposed to use them in
> LaTeX to replace emph and noun.) They are added to the menu below the
> layout-specific charstyles.
> 
> > If so, where would these per-document char styles be saved? Presumably in 
> > the
> > lyx documnent. 
> 
> Yes... haven't done that yet. Otherwise seems to work.

Attached the current state of my work. Now saving and loading works too.
You define a buffer (document) specific charstyle by the following
minibuffer command:

        M-x charstyle-insert _mystyle

where the underscore is necessary for now (I may be able to get rid of this).

The charstyle takes the font at the cursor location as its own: family,
series, shape, and colour. For now. Both on-screen and in LaTeX output.

Textclass switching preserves these user defined charstyles. They remain
separate from the textclass defined ones.

This is a preliminary design and could do with some review. (But no,
I don't promise to implement all your crazy ideas :-)

- Martin

Index: insets/insetcharstyle.C
===================================================================
--- insets/insetcharstyle.C     (revision 13327)
+++ insets/insetcharstyle.C     (working copy)
@@ -48,7 +49,7 @@
        setInsetName("CharStyle");
        setInlined();
        setDrawFrame(false);
-       has_label_ = true;
+       has_label_ = params_.latextype != "fontstyle";
 }
 
 
@@ -104,6 +105,7 @@
 void InsetCharStyle::setDefined(CharStyles::iterator cs)
 {
        params_.latextype = cs->latextype;
+       has_label_ = params_.latextype != "fontstyle";
        params_.latexname = cs->latexname;
        params_.latexparam = cs->latexparam;
        params_.font = cs->font;
@@ -261,15 +263,43 @@
 int InsetCharStyle::latex(Buffer const & buf, ostream & os,
                     OutputParams const & runparams) const
 {
-       if (!undefined()) {
-               os << "\\" << params_.latexname;
-               if (!params_.latexparam.empty())
-                       os << params_.latexparam;
-               os << "{";
+       int i;
+       if (params_.latextype == "fontstyle") {
+               // Output the inset's defined font params_.font:
+               pos_type brackets = 0;
+               LyXFont f = params_.font;
+               LyXFont ah = LyXFont(LyXFont::ALL_SANE);
+               string const ff[] =   
{"rm","sf","tt","md","bf","up","it","sl","sc"};
+               if (f.family() != ah.family()) {
+                       os << "\\text" << ff[f.family()] << '{';
+                       ++brackets;
+               }
+               if (f.series() != ah.series()) {
+                       os << "\\text" << ff[f.series() + 3] << '{';
+                       ++brackets;
+               }
+               if (f.shape() != ah.shape()) {
+                       os << "\\text" << ff[f.shape() + 5] << '{';
+                       ++brackets;
+               }
+               if (f.color() != ah.color()) {
+                       os << "\\textcolor{" << lcolor.getLaTeXName(f.color()) 
<< "}{";
+                       ++brackets;
+               }
+               i = InsetText::latex(buf, os, runparams);
+               os << string(brackets, '}');
+
+       } else if (params_.latextype == "command") {
+               if (!undefined()) {
+                       os << "\\" << params_.latexname;
+                       if (!params_.latexparam.empty())
+                               os << params_.latexparam;
+                       os << "{";
+               }
+               i = InsetText::latex(buf, os, runparams);
+               if (!undefined())
+                       os << "}";
        }
-       int i = InsetText::latex(buf, os, runparams);
-       if (!undefined())
-               os << "}";
        return i;
 }
 
Index: factory.C
===================================================================
--- factory.C   (revision 13312)
+++ factory.C   (working copy)
@@ -90,8 +90,28 @@
                CharStyles::iterator found_cs = tclass.charstyle(s);
                if (found_cs != tclass.charstyles().end())
                        return new InsetCharStyle(params, found_cs);
-               else
-                       return new InsetCharStyle(params, s);
+               else {
+                       found_cs = params.charstyle(s);
+                       if (found_cs != params.charstyles().end())
+                               return new InsetCharStyle(params, found_cs);
+                       else if (s[0] == '_') {
+                               // Insert here new buffer style into list...
+                               InsetCharStyle * inset = 
+                                       new InsetCharStyle(params, s);
+                               CharStyle cs;
+                               cs.name = s;
+                               cs.latexname = s;
+                               cs.latextype = "fontstyle";
+                               cs.font = bv->cursor().getFont();
+                               CharStyles & css = const_cast<CharStyles 
&>(params.charstyles());
+                               if (params.charstyle(cs.name) == css.end())
+                                       css.push_back(cs);
+                               //inset->setDefined(css.end() - 1);
+                               inset->setDefined(params.charstyle(cs.name));
+                               return inset;
+                       } else
+                               return new InsetCharStyle(params, s);
+               }
        }
 
        case LFUN_INSERT_NOTE: {
@@ -410,8 +430,15 @@
                        if (found_cs != tclass.charstyles().end())
                                inset.reset(new InsetCharStyle(buf.params(), 
found_cs));
                        else {
+                               CharStyles::iterator found_cs 
+                                       = buf.params().charstyle(s);
+                               if (found_cs != buf.params().charstyles().end())
+                                       inset.reset(new 
InsetCharStyle(buf.params(), found_cs));
+                               else {
+                               
                                // "Undefined" inset
                                inset.reset(new InsetCharStyle(buf.params(), 
s));
+                               }
                        }
                } else if (tmptok == "Branch") {
                        inset.reset(new InsetBranch(buf.params(),
Index: CutAndPaste.C
===================================================================
--- CutAndPaste.C       (revision 13339)
+++ CutAndPaste.C       (working copy)
@@ -388,26 +389,28 @@
                if (it->lyxCode() == InsetBase::CHARSTYLE_CODE) {
                        InsetCharStyle & inset =
                                static_cast<InsetCharStyle &>(*it);
-                       string const name = inset.params().type;
-                       CharStyles::iterator const found_cs =
-                               tclass2.charstyle(name);
-                       if (found_cs == tclass2.charstyles().end()) {
-                               // The character style is undefined in tclass2
-                               inset.setUndefined();
-                               string const s = bformat(_(
-                                       "Character style %1$s is "
-                                       "undefined because of class "
-                                       "conversion from\n%2$s to %3$s"),
-                                        name, tclass1.name(), tclass2.name());
-                               // To warn the user that something had to be 
done.
-                               errorlist.push_back(ErrorItem(
-                                               _("Undefined character style"),
-                                               s, it.paragraph().id(),
-                                               it.pos(), it.pos() + 1));
-                       } else if (inset.undefined()) {
-                               // The character style is undefined in
-                               // tclass1 and is defined in tclass2
-                               inset.setDefined(found_cs);
+                       // Ignore per-document charstyles:
+                       if (inset.params().latextype != "fontstyle") {
+                               string const name = inset.params().type;
+                               CharStyles::iterator const found_cs = 
tclass2.charstyle(name);
+                               if (found_cs == tclass2.charstyles().end()) {
+                                       // The character style is undefined in 
tclass2
+                                       inset.setUndefined();
+                                       string const s = bformat(_(
+                                               "Character style %1$s is "
+                                               "undefined because of class "
+                                               "conversion from\n%2$s to 
%3$s"),
+                                                name, tclass1.name(), 
tclass2.name());
+                                       // To warn the user that something had 
to be done.
+                                       errorlist.push_back(ErrorItem(
+                                                       _("Undefined character 
style"),
+                                                       s, it.paragraph().id(),
+                                                       it.pos(), it.pos() + 
1));
+                               } else if (inset.undefined()) {
+                                       // The character style is undefined in
+                                       // tclass1 and is defined in tclass2
+                                       inset.setDefined(found_cs);
+                               }
                        }
                }
        }
Index: bufferparams.C
===================================================================
--- bufferparams.C      (revision 13312)
+++ bufferparams.C      (working copy)
@@ -245,6 +245,9 @@
 
        AuthorList authorlist;
        BranchList branchlist;
+       /// CharStyles defined in this buffer
+       CharStyles charstyles;
+
        boost::array<Bullet, 4> temp_bullets;
        boost::array<Bullet, 4> user_defined_bullets;
        Spacing spacing;
@@ -344,6 +347,32 @@
 }
 
 
+CharStyles & BufferParams::charstyles()
+{
+       return pimpl_->charstyles;
+}
+
+
+CharStyles const & BufferParams::charstyles() const
+{
+       return pimpl_->charstyles;
+}
+
+
+CharStyles::iterator BufferParams::charstyle(string const & s) const
+{
+       CharStyles::iterator cs = 
+               const_cast<CharStyles &>(charstyles()).begin(); 
+       CharStyles::iterator csend = 
+               const_cast<CharStyles &>(charstyles()).end(); 
+       for (; cs != csend; ++cs) { 
+               if (cs->name == s) 
+                       return cs; 
+       } 
+       return csend; 
+}
+
+
 Bullet & BufferParams::temp_bullet(lyx::size_type const index)
 {
        BOOST_ASSERT(index < 4);
@@ -488,6 +517,15 @@
 
                        }
                }
+       } else if (token == "\\charstyle") {
+               lex.next();
+               CharStyle cs;
+               cs.name = lex.getString();
+               cs.latextype = "fontstyle";
+               cs.latexname = cs.name;
+               cs.font.lyxRead(lex);
+               if (charstyle(cs.name) == charstyles().end())
+                       charstyles().push_back(cs);
        } else if (token == "\\author") {
                lex.eatLine();
                istringstream ss(lex.getString());
@@ -605,7 +643,15 @@
                   << "\n\\end_branch"
                   << "\n";
        }
-
+       CharStyles::const_iterator cit = charstyles().begin();
+       CharStyles::const_iterator cend = charstyles().end();
+       for (; cit != cend; ++cit) {
+               os << "\\charstyle " << cit->name;
+               // Output font info here
+               cit->font.lyxWriteChanges(LyXFont(LyXFont::ALL_SANE), os);
+               os << "\\endfont"
+                  << "\n";
+       }       
        if (!paperwidth.empty())
                os << "\\paperwidth "
                   << VSpace(paperwidth).asLyXCommand() << '\n';
Index: text3.C
===================================================================
--- text3.C     (revision 13328)
+++ text3.C     (working copy)
@@ -1665,8 +1665,7 @@
                break;
        case LFUN_INSERT_CHARSTYLE:
                code = InsetBase::CHARSTYLE_CODE;
-               if 
(cur.buffer().params().getLyXTextClass().charstyles().empty())
-                       enable = false;
+               enable = true;
                break;
        case LFUN_INSERT_BOX:
                code = InsetBase::BOX_CODE;
Index: bufferparams.h
===================================================================
--- bufferparams.h      (revision 13312)
+++ bufferparams.h      (working copy)
@@ -161,6 +161,11 @@
        /// BranchList:
        BranchList & branchlist();
        BranchList const & branchlist() const;
+       /// CharStyles:
+       CharStyles & charstyles();
+       CharStyles const & charstyles() const;
+       /// Retrieve element of name s:
+       CharStyles::iterator charstyle(std::string const & s) const;
        ///
        std::string inputenc;
        ///
Index: lyxfont.C
===================================================================
--- lyxfont.C   (revision 13312)
+++ lyxfont.C   (working copy)
@@ -628,7 +628,9 @@
        bool finished = false;
        while (!finished && lex.isOK() && !error) {
                lex.next();
-               string const tok = ascii_lowercase(lex.getString());
+               string tok = ascii_lowercase(lex.getString());
+               if (tok[0] == '\\')
+                       tok = tok.substr(1);
 
                if (tok.empty()) {
                        continue;
Index: MenuBackend.C
===================================================================
--- MenuBackend.C       (revision 13312)
+++ MenuBackend.C       (working copy)
@@ -601,6 +601,17 @@
                                    FuncRequest(LFUN_INSERT_CHARSTYLE,
                                                cit->name)), view);
        }
+       tomenu.add(MenuItem(MenuItem::Separator));
+       CharStyles & charstyles2 = view->buffer()->params().charstyles();
+       cit = charstyles2.begin();
+       end = charstyles2.end();
+       for (; cit != end; ++cit) {
+               string const label = cit->name;
+               tomenu.add(MenuItem(MenuItem::Command, label,
+                                   FuncRequest(LFUN_INSERT_CHARSTYLE,
+                                               cit->name)), view);
+       }
+
 }
 
 

Attachment: pgpfHrq4mBpfP.pgp
Description: PGP signature

Reply via email to