]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
update no.po
[lyx.git] / src / paragraph_funcs.C
1 /**
2  * \file paragraph_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "paragraph_funcs.h"
14 #include "buffer.h"
15 #include "ParagraphParameters.h"
16 #include "lyxtextclasslist.h"
17 #include "debug.h"
18
19 using lyx::pos_type;
20 //using lyx::layout_type;
21 using std::endl;
22
23
24 void breakParagraph(BufferParams const & bparams,
25                     Paragraph * par,
26                     pos_type pos,
27                     int flag)
28 {
29         // create a new paragraph
30         Paragraph * tmp = new Paragraph(par);
31         // without doing that we get a crash when typing <Return> at the
32         // end of a paragraph
33         tmp->layout(bparams.getLyXTextClass().defaultLayout());
34         // remember to set the inset_owner
35         tmp->setInsetOwner(par->inInset());
36
37         // this is an idea for a more userfriendly layout handling, I will
38         // see what the users say
39
40         // layout stays the same with latex-environments
41         if (flag) {
42                 tmp->layout(par->layout());
43                 tmp->setLabelWidthString(par->params().labelWidthString());
44         }
45
46         bool const isempty = (par->layout()->keepempty && par->empty());
47
48         if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
49                 tmp->layout(par->layout());
50                 tmp->params().align(par->params().align());
51                 tmp->setLabelWidthString(par->params().labelWidthString());
52
53                 tmp->params().lineBottom(par->params().lineBottom());
54                 par->params().lineBottom(false);
55                 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
56                 par->params().pagebreakBottom(false);
57                 tmp->params().spaceBottom(par->params().spaceBottom());
58                 par->params().spaceBottom(VSpace(VSpace::NONE));
59
60                 tmp->params().depth(par->params().depth());
61                 tmp->params().noindent(par->params().noindent());
62
63                 // copy everything behind the break-position
64                 // to the new paragraph
65
66 #ifdef WITH_WARNINGS
67 #warning this seems wrong
68 #endif
69                 /* FIXME: if !keepempty, empty() == true, then we reach
70                  * here with size() == 0. So pos_end becomes - 1. Why
71                  * doesn't this cause problems ???
72                  */
73                 pos_type pos_end = par->size() - 1;
74                 pos_type i = pos;
75                 pos_type j = pos;
76                 for (; i <= pos_end; ++i) {
77                         par->cutIntoMinibuffer(bparams, i);
78                         if (tmp->insertFromMinibuffer(j - pos))
79                                 ++j;
80                 }
81                 for (i = pos_end; i >= pos; --i) {
82                         par->erase(i);
83                 }
84         }
85
86         if (pos)
87                 return;
88  
89         tmp->params().lineTop(par->params().lineTop());
90         tmp->params().pagebreakTop(par->params().pagebreakTop());
91         tmp->params().spaceTop(par->params().spaceTop());
92         tmp->bibkey = par->bibkey;
93
94         par->bibkey = 0;
95         par->params().clear();
96
97         par->layout(bparams.getLyXTextClass().defaultLayout());
98
99         // layout stays the same with latex-environments
100         if (flag) {
101                 par->layout(tmp->layout());
102                 par->setLabelWidthString(tmp->params().labelWidthString());
103                 par->params().depth(tmp->params().depth());
104         }
105 }
106
107
108 void breakParagraphConservative(BufferParams const & bparams,
109                                 Paragraph * par,
110                                 pos_type pos)
111 {
112         // create a new paragraph
113         Paragraph * tmp = new Paragraph(par);
114         tmp->makeSameLayout(par);
115
116         // When can pos > Last()?
117         // I guess pos == Last() is possible.
118         if (par->size() > pos) {
119                 // copy everything behind the break-position to the new
120                 // paragraph
121                 pos_type pos_end = par->size() - 1;
122
123                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
124                         par->cutIntoMinibuffer(bparams, i);
125                         if (tmp->insertFromMinibuffer(j - pos))
126                                 ++j;
127                 }
128
129                 for (pos_type k = pos_end; k >= pos; --k) {
130                         par->erase(k);
131                 }
132         }
133 }
134
135
136 void mergeParagraph(BufferParams const & bparams, Paragraph * par)
137 {
138         Paragraph * the_next = par->next();
139
140         // first the DTP-stuff
141         par->params().lineBottom(the_next->params().lineBottom());
142         par->params().spaceBottom(the_next->params().spaceBottom());
143         par->params().pagebreakBottom(the_next->params().pagebreakBottom());
144
145         pos_type pos_end = the_next->size() - 1;
146         pos_type pos_insert = par->size();
147
148         // ok, now copy the paragraph
149         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
150                 the_next->cutIntoMinibuffer(bparams, i);
151                 if (par->insertFromMinibuffer(pos_insert + j))
152                         ++j;
153         }
154
155         // delete the next paragraph
156         Paragraph * ppar = the_next->previous();
157         Paragraph * npar = the_next->next();
158         delete the_next;
159         ppar->next(npar);
160 }
161
162
163 #if 0
164 Paragraph * depthHook(Paragraph * par, Paragraph::depth_type depth)
165 {
166         Paragraph * newpar = par;
167
168         do {
169                 newpar = newpar->previous();
170         } while (newpar && newpar->getDepth() > depth);
171
172         if (!newpar) {
173                 if (par->previous() || par->getDepth())
174                         lyxerr << "Error (depthHook): "
175                                << "no hook." << endl;
176                 newpar = par;
177         }
178         return newpar;
179 }
180
181
182 Paragraph * outerHook(Paragraph * par)
183 {
184         if (!par->getDepth())
185                 return 0;
186         return depthHook(par, Paragraph::depth_type(par->getDepth() - 1));
187 }
188
189
190 bool isFirstInSequence(Paragraph * par)
191 {
192         Paragraph const * dhook = depthHook(par, par->getDepth());
193         return (dhook == par
194                 || dhook->getLayout() != par->getLayout()
195                 || dhook->getDepth() != par->getDepth());
196 }
197
198
199 int getEndLabel(Paragraph * para, BufferParams const & bparams)
200 {
201         Paragraph * par = para;
202         while (par) {
203                 Paragraph::depth_type par_depth = par->getDepth();
204                 layout_type layout = par->getLayout();
205                 int const endlabeltype =
206                         textclasslist.Style(bparams.textclass,
207                                             layout).endlabeltype;
208                 if (endlabeltype != END_LABEL_NO_LABEL) {
209                         if (!para->next())
210                                 return endlabeltype;
211
212                         Paragraph::depth_type const next_depth =
213                                 para->next()->getDepth();
214                         if (par_depth > next_depth ||
215                             (par_depth == next_depth
216                              && layout != para->next()->getLayout()))
217                                 return endlabeltype;
218                         break;
219                 }
220                 if (par_depth == 0)
221                         break;
222                 par = outerHook(par);
223         }
224         return END_LABEL_NO_LABEL;
225 }
226 #endif