riched20: Implement ECO/EM SELECTIONBAR.
[wine] / dlls / riched20 / caret.c
1 /*
2  * RichEdit - Caret and selection functions.
3  *
4  * Copyright 2004 by Krzysztof Foltman
5  * Copyright 2005 by Phil Krylov
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22
23 #include "editor.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(richedit);
26
27 static BOOL
28 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs);
29
30 void ME_GetSelection(ME_TextEditor *editor, int *from, int *to)
31 {
32   *from = ME_GetCursorOfs(editor, 0);
33   *to =   ME_GetCursorOfs(editor, 1);
34   
35   if (*from > *to)
36   {
37     int tmp = *from;
38     *from = *to;
39     *to = tmp;    
40   }
41 }
42
43 int ME_GetTextLength(ME_TextEditor *editor)
44 {
45   return ME_CharOfsFromRunOfs(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun), 0);   
46 }
47
48
49 int ME_GetTextLengthEx(ME_TextEditor *editor, const GETTEXTLENGTHEX *how)
50 {
51   int length;
52
53   if (how->flags & GTL_PRECISE && how->flags & GTL_CLOSE)
54     return E_INVALIDARG;
55   if (how->flags & GTL_NUMCHARS && how->flags & GTL_NUMBYTES)
56     return E_INVALIDARG;
57   
58   length = ME_GetTextLength(editor);
59
60   if ((GetWindowLongW(editor->hWnd, GWL_STYLE) & ES_MULTILINE) && (how->flags & GTL_USECRLF))
61     length += editor->nParagraphs - 1;
62   
63   if (how->flags & GTL_NUMBYTES)
64   {
65     CPINFO cpinfo;
66     
67     if (how->codepage == 1200)
68       return length * 2;
69     if (how->flags & GTL_PRECISE)
70       FIXME("GTL_PRECISE flag unsupported. Using GTL_CLOSE\n");
71     if (GetCPInfo(how->codepage, &cpinfo))
72       return length * cpinfo.MaxCharSize;
73     ERR("Invalid codepage %u\n", how->codepage);
74     return E_INVALIDARG;
75   }
76   return length; 
77 }
78
79
80 int ME_SetSelection(ME_TextEditor *editor, int from, int to)
81 {
82   int selectionEnd = 0;
83   const int len = ME_GetTextLength(editor);
84
85   /* all negative values are effectively the same */
86   if (from < 0)
87     from = -1;
88   if (to < 0)
89     to = -1;
90
91   /* select all */
92   if (from == 0 && to == -1)
93   {
94     editor->pCursors[1].pRun = ME_FindItemFwd(editor->pBuffer->pFirst, diRun);
95     editor->pCursors[1].nOffset = 0; 
96     editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun); 
97     editor->pCursors[0].nOffset = 0;
98     ME_InvalidateSelection(editor);
99     ME_ClearTempStyle(editor);
100     return len + 1;
101   }
102
103   /* if both values are equal and also out of bound, that means to */
104   /* put the selection at the end of the text */
105   if ((from == to) && (to < 0 || to > len))
106   {
107     selectionEnd = 1;
108   }
109   else
110   {
111     /* if from is negative and to is positive then selection is */
112     /* deselected and caret moved to end of the current selection */
113     if (from < 0)
114     {
115       int start, end;
116       ME_GetSelection(editor, &start, &end);
117       editor->pCursors[1] = editor->pCursors[0];
118       ME_Repaint(editor);
119       ME_ClearTempStyle(editor);
120       return end;
121     }
122
123     /* adjust to if it's a negative value */
124     if (to < 0)
125       to = len + 1;
126
127     /* flip from and to if they are reversed */
128     if (from>to)
129     {
130       int tmp = from;
131       from = to;
132       to = tmp;
133     }
134
135     /* after fiddling with the values, we find from > len && to > len */
136     if (from > len)
137       selectionEnd = 1;
138     /* special case with to too big */
139     else if (to > len)
140       to = len + 1;
141   }
142
143   if (selectionEnd)
144   {
145     editor->pCursors[1].pRun = editor->pCursors[0].pRun = ME_FindItemBack(editor->pBuffer->pLast, diRun);
146     editor->pCursors[1].nOffset = editor->pCursors[0].nOffset = 0;
147     ME_InvalidateSelection(editor);
148     ME_ClearTempStyle(editor);
149     return len;
150   }
151
152   ME_RunOfsFromCharOfs(editor, from, &editor->pCursors[1].pRun, &editor->pCursors[1].nOffset);
153   ME_RunOfsFromCharOfs(editor, to, &editor->pCursors[0].pRun, &editor->pCursors[0].nOffset);
154   return to;
155 }
156
157
158 void
159 ME_GetCursorCoordinates(ME_TextEditor *editor, ME_Cursor *pCursor,
160                         int *x, int *y, int *height)
161 {
162   ME_DisplayItem *pCursorRun = pCursor->pRun;
163   ME_DisplayItem *pSizeRun = pCursor->pRun;
164
165   assert(!pCursor->nOffset || !editor->bCaretAtEnd);
166   assert(height && x && y);
167   assert(!(ME_GetParagraph(pCursorRun)->member.para.nFlags & MEPF_REWRAP));
168   assert(pCursor->pRun);
169   assert(pCursor->pRun->type == diRun);
170   
171   if (pCursorRun->type == diRun) {
172     ME_DisplayItem *row = ME_FindItemBack(pCursorRun, diStartRowOrParagraph);
173
174     if (row) {
175       HDC hDC = GetDC(editor->hWnd);
176       ME_Context c;
177       ME_DisplayItem *run = pCursorRun;
178       ME_DisplayItem *para = NULL;
179       SIZE sz = {0, 0};
180     
181       ME_InitContext(&c, editor, hDC);
182       
183       if (!pCursor->nOffset && !editor->bCaretAtEnd)
184       {
185         ME_DisplayItem *prev = ME_FindItemBack(pCursorRun, diRunOrStartRow);
186         assert(prev);
187         if (prev->type == diRun)
188           pSizeRun = prev;
189       }
190       assert(row->type == diStartRow); /* paragraph -> run without start row ?*/
191       para = ME_FindItemBack(row, diParagraph);
192       assert(para);
193       assert(para->type == diParagraph);
194       if (editor->bCaretAtEnd && !pCursor->nOffset && 
195           run == ME_FindItemFwd(row, diRun))
196       {
197         ME_DisplayItem *tmp = ME_FindItemBack(row, diRunOrParagraph);
198         assert(tmp);
199         if (tmp->type == diRun)
200         {
201           row = ME_FindItemBack(tmp, diStartRow);
202           pSizeRun = run = tmp;
203           assert(run);
204           assert(run->type == diRun);
205           sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, ME_StrLen(run->member.run.strText));
206         }
207       }
208       if (pCursor->nOffset && !(run->member.run.nFlags & MERF_SKIPPED)) {
209         sz = ME_GetRunSize(&c, &para->member.para, &run->member.run, pCursor->nOffset);
210       }
211
212       *height = pSizeRun->member.run.nAscent + pSizeRun->member.run.nDescent;
213       *x = run->member.run.pt.x + sz.cx;
214       *y = para->member.para.nYPos + row->member.row.nBaseline + pSizeRun->member.run.pt.y - pSizeRun->member.run.nAscent - ME_GetYScrollPos(editor);
215       
216       ME_DestroyContext(&c);
217       ReleaseDC(editor->hWnd, hDC);
218       return;
219     }
220   }
221   *height = 10; /* FIXME use global font */
222   *x = 0;
223   *y = 0;
224 }
225
226
227 void
228 ME_MoveCaret(ME_TextEditor *editor)
229 {
230   int x, y, height;
231
232   if (ME_WrapMarkedParagraphs(editor))
233     ME_UpdateScrollBar(editor);
234   ME_GetCursorCoordinates(editor, &editor->pCursors[0], &x, &y, &height);
235   if(editor->bHaveFocus)
236   {
237     CreateCaret(editor->hWnd, NULL, 0, height);
238     SetCaretPos(x, y);
239   }
240 }
241
242
243 void ME_ShowCaret(ME_TextEditor *ed)
244 {
245   ME_MoveCaret(ed);
246   if(ed->bHaveFocus)
247     ShowCaret(ed->hWnd);
248 }
249
250 void ME_HideCaret(ME_TextEditor *ed)
251 {
252   if(ed->bHaveFocus)
253   {
254     HideCaret(ed->hWnd);
255     DestroyCaret();
256   }
257 }
258
259 void ME_InternalDeleteText(ME_TextEditor *editor, int nOfs, 
260   int nChars)
261 {
262   ME_Cursor c;
263   int shift = 0;
264   
265   while(nChars > 0)
266   {
267     ME_Run *run;
268     ME_CursorFromCharOfs(editor, nOfs, &c);
269     run = &c.pRun->member.run;
270     if (run->nFlags & MERF_ENDPARA) {
271       if (!ME_FindItemFwd(c.pRun, diParagraph))
272       {
273         return;
274       }
275       ME_JoinParagraphs(editor, ME_GetParagraph(c.pRun));
276       /* ME_SkipAndPropagateCharOffset(p->pRun, shift); */
277       ME_CheckCharOffsets(editor);
278       nChars--;
279       if (editor->bEmulateVersion10 && nChars)
280         nChars--;
281       continue;
282     }
283     else
284     {
285       ME_Cursor cursor;
286       int nIntendedChars = nChars;
287       int nCharsToDelete = nChars;
288       int i;
289       int loc = c.nOffset;
290       
291       ME_FindItemBack(c.pRun, diParagraph)->member.para.nFlags |= MEPF_REWRAP;
292       
293       cursor = c;
294       ME_StrRelPos(run->strText, loc, &nChars);
295       /* nChars is the number of characters that should be deleted from the
296          FOLLOWING runs (these AFTER cursor.pRun)
297          nCharsToDelete is a number of chars to delete from THIS run */
298       nCharsToDelete -= nChars;
299       shift -= nCharsToDelete;
300       TRACE("Deleting %d (intended %d-remaning %d) chars at %d in '%s' (%d)\n", 
301         nCharsToDelete, nIntendedChars, nChars, c.nOffset, 
302         debugstr_w(run->strText->szData), run->strText->nLen);
303
304       if (!c.nOffset && ME_StrVLen(run->strText) == nCharsToDelete)
305       {
306         /* undo = reinsert whole run */
307         /* nOfs is a character offset (from the start of the document
308            to the current (deleted) run */
309         ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
310         if (pUndo)
311           pUndo->di.member.run.nCharOfs = nOfs;
312       }
313       else
314       {
315         /* undo = reinsert partial run */
316         ME_UndoItem *pUndo = ME_AddUndoItem(editor, diUndoInsertRun, c.pRun);
317         if (pUndo) {
318           ME_DestroyString(pUndo->di.member.run.strText);
319           pUndo->di.member.run.nCharOfs = nOfs;
320           pUndo->di.member.run.strText = ME_MakeStringN(run->strText->szData+c.nOffset, nCharsToDelete);
321         }
322       }
323       TRACE("Post deletion string: %s (%d)\n", debugstr_w(run->strText->szData), run->strText->nLen);
324       TRACE("Shift value: %d\n", shift);
325       ME_StrDeleteV(run->strText, c.nOffset, nCharsToDelete);
326       
327       /* update cursors (including c) */
328       for (i=-1; i<editor->nCursors; i++) {
329         ME_Cursor *pThisCur = editor->pCursors + i; 
330         if (i == -1) pThisCur = &c;
331         if (pThisCur->pRun == cursor.pRun) {
332           if (pThisCur->nOffset > cursor.nOffset) {
333             if (pThisCur->nOffset-cursor.nOffset < nCharsToDelete)
334               pThisCur->nOffset = cursor.nOffset;
335             else
336               pThisCur->nOffset -= nCharsToDelete;
337             assert(pThisCur->nOffset >= 0);
338             assert(pThisCur->nOffset <= ME_StrVLen(run->strText));
339           }
340           if (pThisCur->nOffset == ME_StrVLen(run->strText))
341           {
342             pThisCur->pRun = ME_FindItemFwd(pThisCur->pRun, diRunOrParagraphOrEnd);
343             assert(pThisCur->pRun->type == diRun);
344             pThisCur->nOffset = 0;
345           }
346         }
347       }
348       
349       /* c = updated data now */
350       
351       if (c.pRun == cursor.pRun)
352         ME_SkipAndPropagateCharOffset(c.pRun, shift);
353       else
354         ME_PropagateCharOffset(c.pRun, shift);
355
356       if (!ME_StrVLen(cursor.pRun->member.run.strText))
357       {
358         TRACE("Removing useless run\n");
359         ME_Remove(cursor.pRun);
360         ME_DestroyDisplayItem(cursor.pRun);
361       }
362       
363       shift = 0;
364       /*
365       ME_CheckCharOffsets(editor);
366       */
367       continue;
368     }
369   }
370 }
371
372 void ME_DeleteTextAtCursor(ME_TextEditor *editor, int nCursor, 
373   int nChars)
374 {  
375   assert(nCursor>=0 && nCursor<editor->nCursors);
376   /* text operations set modified state */
377   editor->nModifyStep = 1;
378   ME_InternalDeleteText(editor, ME_GetCursorOfs(editor, nCursor), nChars);
379 }
380
381 static ME_DisplayItem *
382 ME_InternalInsertTextFromCursor(ME_TextEditor *editor, int nCursor,
383                                 const WCHAR *str, int len, ME_Style *style,
384                                 int flags)
385 {
386   ME_Cursor *p = &editor->pCursors[nCursor];
387
388   editor->bCaretAtEnd = FALSE;
389   
390   assert(p->pRun->type == diRun);
391   
392   return ME_InsertRunAtCursor(editor, p, style, str, len, flags);
393 }
394
395
396 /* FIXME this is temporary, just to have something to test how bad graphics handler is */
397 void ME_InsertGraphicsFromCursor(ME_TextEditor *editor, int nCursor)
398 {
399   ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
400   WCHAR space = ' ';
401   
402   /* FIXME no no no */
403   if (ME_IsSelection(editor))
404     ME_DeleteSelection(editor);
405
406   ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, pStyle,
407                                   MERF_GRAPHICS);
408   ME_SendSelChange(editor);
409 }
410
411
412 void
413 ME_InsertTableCellFromCursor(ME_TextEditor *editor, int nCursor)
414 {
415   WCHAR tab = '\t';
416   ME_DisplayItem *p, *run;
417   ME_Style *pStyle = ME_GetInsertStyle(editor, nCursor);
418   
419   p = ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, pStyle,
420                                       MERF_CELL);
421   run = p;
422   while ((run = ME_FindItemBack(run, diRunOrParagraph))->type == diRun)
423   {
424     if (run->member.run.nFlags & MERF_CELL)
425     {
426       assert(run->member.run.pCell->next);
427       p->member.run.pCell = run->member.run.pCell->next;
428       return;
429     }
430   }
431   assert(run->type == diParagraph);
432   assert(run->member.para.bTable);
433   assert(run->member.para.pCells);
434   p->member.run.pCell = run->member.para.pCells;
435 }
436
437
438 void ME_InsertTextFromCursor(ME_TextEditor *editor, int nCursor, 
439   const WCHAR *str, int len, ME_Style *style)
440 {
441   const WCHAR *pos;
442   ME_Cursor *p = NULL;
443   int oldLen;
444
445   /* FIXME really HERE ? */
446   if (ME_IsSelection(editor))
447     ME_DeleteSelection(editor);
448
449   /* FIXME: is this too slow? */
450   /* Didn't affect performance for WM_SETTEXT (around 50sec/30K) */
451   oldLen = ME_GetTextLength(editor);
452
453   /* text operations set modified state */
454   editor->nModifyStep = 1;
455
456   assert(style);
457
458   assert(nCursor>=0 && nCursor<editor->nCursors);
459   if (len == -1)
460     len = lstrlenW(str);
461
462   /* grow the text limit to fit our text */
463   if(editor->nTextLimit < oldLen +len)
464     editor->nTextLimit = oldLen + len;
465
466   while (len)
467   {
468     pos = str;
469     /* FIXME this sucks - no respect for unicode (what else can be a line separator in unicode?) */
470     while(pos-str < len && *pos != '\r' && *pos != '\n' && *pos != '\t')
471       pos++;
472     if (pos-str < len && *pos == '\t') { /* handle tabs */
473       WCHAR tab = '\t';
474
475       if (pos!=str)
476         ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
477     
478       ME_InternalInsertTextFromCursor(editor, nCursor, &tab, 1, style, MERF_TAB);
479  
480       pos++;
481       if(pos-str <= len) {
482         len -= pos - str;
483         str = pos;
484         continue;
485       }
486     }
487     /* handle special \r\r\n sequence (richedit 2.x and higher only) */
488     if (!editor->bEmulateVersion10 && pos-str < len-2 && pos[0] == '\r' && pos[1] == '\r' && pos[2] == '\n') {
489       WCHAR space = ' ';
490
491       if (pos!=str)
492         ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
493
494       ME_InternalInsertTextFromCursor(editor, nCursor, &space, 1, style, 0);
495
496       pos+=3;
497       if(pos-str <= len) {
498         len -= pos - str;
499         str = pos;
500         continue;
501       }
502     }
503     if (pos-str < len) {   /* handle EOLs */
504       ME_DisplayItem *tp, *end_run;
505       ME_Style *tmp_style;
506       if (pos!=str)
507         ME_InternalInsertTextFromCursor(editor, nCursor, str, pos-str, style, 0);
508       p = &editor->pCursors[nCursor];
509       if (p->nOffset) {
510         ME_SplitRunSimple(editor, p->pRun, p->nOffset);
511         p = &editor->pCursors[nCursor];
512       }
513       tmp_style = ME_GetInsertStyle(editor, nCursor);
514       /* ME_SplitParagraph increases style refcount */
515       tp = ME_SplitParagraph(editor, p->pRun, p->pRun->member.run.style);
516       p->pRun = ME_FindItemFwd(tp, diRun);
517       end_run = ME_FindItemBack(tp, diRun);
518       ME_ReleaseStyle(end_run->member.run.style);
519       end_run->member.run.style = tmp_style;
520       p->nOffset = 0;
521       if(pos-str < len && *pos =='\r')
522         pos++;
523       if(pos-str < len && *pos =='\n')
524         pos++;
525       if(pos-str <= len) {
526         len -= pos - str;
527         str = pos;
528         continue;
529       }
530     }
531     ME_InternalInsertTextFromCursor(editor, nCursor, str, len, style, 0);
532     len = 0;
533   }
534 }
535
536
537 static BOOL
538 ME_MoveCursorChars(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
539 {
540   ME_DisplayItem *pRun = pCursor->pRun;
541   
542   if (nRelOfs == -1)
543   {
544     if (!pCursor->nOffset)
545     {
546       do {
547         pRun = ME_FindItemBack(pRun, diRunOrParagraph);
548         assert(pRun);
549         switch (pRun->type)
550         {
551           case diRun:
552             break;
553           case diParagraph:
554             if (pRun->member.para.prev_para->type == diTextStart)
555               return FALSE;
556             pRun = ME_FindItemBack(pRun, diRunOrParagraph);
557             /* every paragraph ought to have at least one run */
558             assert(pRun && pRun->type == diRun);
559             assert(pRun->member.run.nFlags & MERF_ENDPARA);
560             break;
561           default:
562             assert(pRun->type != diRun && pRun->type != diParagraph);
563             return FALSE;
564         }
565       } while (RUN_IS_HIDDEN(&pRun->member.run));
566       pCursor->pRun = pRun;
567       if (pRun->member.run.nFlags & MERF_ENDPARA)
568         pCursor->nOffset = 0;
569       else
570         pCursor->nOffset = pRun->member.run.strText->nLen;
571     }
572     
573     if (pCursor->nOffset)
574       pCursor->nOffset = ME_StrRelPos2(pCursor->pRun->member.run.strText, pCursor->nOffset, nRelOfs);
575     return TRUE;
576   }
577   else
578   {
579     if (!(pRun->member.run.nFlags & MERF_ENDPARA))
580     {
581       int new_ofs = ME_StrRelPos2(pRun->member.run.strText, pCursor->nOffset, nRelOfs);
582     
583       if (new_ofs < pRun->member.run.strText->nLen)
584       {
585         pCursor->nOffset = new_ofs;
586         return TRUE;
587       }
588     }
589     do {
590       pRun = ME_FindItemFwd(pRun, diRun);
591     } while (pRun && RUN_IS_HIDDEN(&pRun->member.run));
592     if (pRun)
593     {
594       pCursor->pRun = pRun;
595       pCursor->nOffset = 0;
596       return TRUE;
597     }
598   }
599   return FALSE;
600 }
601
602
603 static BOOL
604 ME_MoveCursorWords(ME_TextEditor *editor, ME_Cursor *cursor, int nRelOfs)
605 {
606   ME_DisplayItem *pRun = cursor->pRun, *pOtherRun;
607   int nOffset = cursor->nOffset;
608   
609   if (nRelOfs == -1)
610   {
611     /* Backward movement */
612     while (TRUE)
613     {
614       nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
615                                      nOffset, WB_MOVEWORDLEFT);
616        if (nOffset)
617         break;
618       pOtherRun = ME_FindItemBack(pRun, diRunOrParagraph);
619       if (pOtherRun->type == diRun)
620       {
621         if (ME_CallWordBreakProc(editor, pOtherRun->member.run.strText,
622                                  pOtherRun->member.run.strText->nLen - 1,
623                                  WB_ISDELIMITER)
624             && !(pRun->member.run.nFlags & MERF_ENDPARA)
625             && !(cursor->pRun == pRun && cursor->nOffset == 0)
626             && !ME_CallWordBreakProc(editor, pRun->member.run.strText, 0,
627                                      WB_ISDELIMITER))
628           break;
629         pRun = pOtherRun;
630         nOffset = pOtherRun->member.run.strText->nLen;
631       }
632       else if (pOtherRun->type == diParagraph)
633       {
634         if (cursor->pRun == pRun && cursor->nOffset == 0)
635         {
636           /* Paragraph breaks are treated as separate words */
637           if (pOtherRun->member.para.prev_para->type == diTextStart)
638             return FALSE;
639           pRun = ME_FindItemBack(pOtherRun, diRunOrParagraph);
640         }
641         break;
642       }
643     }
644   }
645   else
646   {
647     /* Forward movement */
648     BOOL last_delim = FALSE;
649     
650     while (TRUE)
651     {
652       if (last_delim && !ME_CallWordBreakProc(editor, pRun->member.run.strText,
653                                               nOffset, WB_ISDELIMITER))
654         break;
655       nOffset = ME_CallWordBreakProc(editor, pRun->member.run.strText,
656                                      nOffset, WB_MOVEWORDRIGHT);
657       if (nOffset < pRun->member.run.strText->nLen)
658         break;
659       pOtherRun = ME_FindItemFwd(pRun, diRunOrParagraphOrEnd);
660       if (pOtherRun->type == diRun)
661       {
662         last_delim = ME_CallWordBreakProc(editor, pRun->member.run.strText,
663                                           nOffset - 1, WB_ISDELIMITER);
664         pRun = pOtherRun;
665         nOffset = 0;
666       }
667       else if (pOtherRun->type == diParagraph)
668       {
669         if (cursor->pRun == pRun)
670           pRun = ME_FindItemFwd(pOtherRun, diRun);
671         nOffset = 0;
672         break;
673       }
674       else /* diTextEnd */
675       {
676         if (cursor->pRun == pRun)
677           return FALSE;
678         nOffset = 0;
679         break;
680       }
681     }
682   }
683   cursor->pRun = pRun;
684   cursor->nOffset = nOffset;
685   return TRUE;
686 }
687
688
689 void
690 ME_SelectWord(ME_TextEditor *editor)
691 {
692   if (!(editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA))
693     ME_MoveCursorWords(editor, &editor->pCursors[0], -1);
694   ME_MoveCursorWords(editor, &editor->pCursors[1], +1);
695   ME_InvalidateSelection(editor);
696   ME_SendSelChange(editor);
697 }
698
699
700 int ME_GetCursorOfs(ME_TextEditor *editor, int nCursor)
701 {
702   ME_Cursor *pCursor = &editor->pCursors[nCursor];
703   return ME_GetParagraph(pCursor->pRun)->member.para.nCharOfs
704     + pCursor->pRun->member.run.nCharOfs + pCursor->nOffset;
705 }
706
707 int ME_FindPixelPos(ME_TextEditor *editor, int x, int y, ME_Cursor *result, BOOL *is_eol)
708 {
709   ME_DisplayItem *p = editor->pBuffer->pFirst->member.para.next_para;
710   int rx = 0;
711   
712   if (is_eol)
713     *is_eol = 0;
714
715   while(p != editor->pBuffer->pLast)
716   {
717     if (p->type == diParagraph)
718     {
719       int ry = y - p->member.para.nYPos;
720       if (ry < 0)
721       {
722         result->pRun = ME_FindItemFwd(p, diRun);
723         result->nOffset = 0;
724         return 0;
725       }
726       if (ry >= p->member.para.nHeight)
727       {
728         p = p->member.para.next_para;
729         continue;
730       }
731       p = ME_FindItemFwd(p, diStartRow);
732       y = ry;
733       continue;
734     }
735     if (p->type == diStartRow)
736     {
737       int ry = y - p->member.row.nYPos;
738       if (ry < 0)
739         return 0;
740       if (ry >= p->member.row.nHeight)
741       {
742         p = ME_FindItemFwd(p, diStartRowOrParagraphOrEnd);
743         if (p->type != diStartRow)
744           return 0;
745         continue;
746       }
747       p = ME_FindItemFwd(p, diRun);
748       continue;
749     }
750     if (p->type == diRun)
751     {
752       ME_DisplayItem *pp;
753       rx = x - p->member.run.pt.x;
754       if (rx < 0)
755         rx = 0;
756       if (rx >= p->member.run.nWidth) /* not this run yet... find next item */
757       {
758         pp = p;
759         do {
760           p = p->next;
761           if (p->type == diRun)
762           {
763             rx = x - p->member.run.pt.x;
764             goto continue_search;
765           }
766           if (p->type == diStartRow)
767           {
768             p = ME_FindItemFwd(p, diRun);
769             if (is_eol)
770               *is_eol = 1;
771             rx = 0; /* FIXME not sure */
772             goto found_here;
773           }
774           if (p->type == diParagraph || p->type == diTextEnd)
775           {
776             rx = 0; /* FIXME not sure */
777             p = pp;
778             goto found_here;
779           }
780         } while(1);
781         continue;
782       }
783     found_here:
784       if (p->member.run.nFlags & MERF_ENDPARA)
785         rx = 0;
786       result->pRun = p;
787       result->nOffset = ME_CharFromPointCursor(editor, rx, &p->member.run);
788       if (editor->pCursors[0].nOffset == p->member.run.strText->nLen && rx)
789       {
790         result->pRun = ME_FindItemFwd(editor->pCursors[0].pRun, diRun);
791         result->nOffset = 0;
792       }
793       return 1;
794     }
795     assert(0);
796   continue_search:
797     ;
798   }
799   result->pRun = ME_FindItemBack(p, diRun);
800   result->nOffset = 0;
801   assert(result->pRun->member.run.nFlags & MERF_ENDPARA);
802   return 0;
803 }
804
805
806 int
807 ME_CharFromPos(ME_TextEditor *editor, int x, int y)
808 {
809   ME_Cursor cursor;
810   RECT rc;
811
812   GetClientRect(editor->hWnd, &rc);
813   if (x < 0 || y < 0 || x >= rc.right || y >= rc.bottom)
814     return -1;
815   y += ME_GetYScrollPos(editor);
816   ME_FindPixelPos(editor, x, y, &cursor, NULL);
817   return (ME_GetParagraph(cursor.pRun)->member.para.nCharOfs
818           + cursor.pRun->member.run.nCharOfs + cursor.nOffset);
819 }
820
821
822 void ME_LButtonDown(ME_TextEditor *editor, int x, int y)
823 {
824   ME_Cursor tmp_cursor;
825   int is_selection = 0;
826   
827   editor->nUDArrowX = -1;
828   
829   y += ME_GetYScrollPos(editor);
830
831   tmp_cursor = editor->pCursors[0];
832   is_selection = ME_IsSelection(editor);
833
834   if (x >= editor->selofs)
835   {
836     ME_FindPixelPos(editor, x, y, &editor->pCursors[0], &editor->bCaretAtEnd);
837     if (GetKeyState(VK_SHIFT)>=0)
838     {
839       editor->pCursors[1] = editor->pCursors[0];
840     }
841     else if (!is_selection) {
842       editor->pCursors[1] = tmp_cursor;
843       is_selection = 1;
844     }
845
846     ME_InvalidateSelection(editor);
847     HideCaret(editor->hWnd);
848     ME_MoveCaret(editor);
849     ShowCaret(editor->hWnd);
850     ME_ClearTempStyle(editor);
851     ME_SendSelChange(editor);
852   }
853   else
854   {
855     ME_DisplayItem *pRow;
856
857     editor->linesel = 1;
858     editor->sely = y;
859     /* Set pCursors[0] to beginning of line */
860     ME_FindPixelPos(editor, x, y, &editor->pCursors[1], &editor->bCaretAtEnd);
861     /* Set pCursors[1] to end of line */
862     pRow = ME_FindItemFwd(editor->pCursors[1].pRun, diStartRowOrParagraphOrEnd);
863     assert(pRow);
864     /* pCursor[0] is the position where the cursor will be drawn,
865      * pCursor[1] is the other end of the selection range
866      * pCursor[2] and [3] are backups of [0] and [1] so I
867      * don't have to look them up again
868      */
869
870     if (pRow->type == diStartRow) {
871       /* FIXME WTF was I thinking about here ? */
872       ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
873       assert(pRun);
874       editor->pCursors[0].pRun = pRun;
875       editor->pCursors[0].nOffset = 0;
876       editor->bCaretAtEnd = 1;
877     } else {
878       editor->pCursors[0].pRun = ME_FindItemBack(pRow, diRun);
879       assert(editor->pCursors[0].pRun && editor->pCursors[0].pRun->member.run.nFlags & MERF_ENDPARA);
880       editor->pCursors[0].nOffset = 0;
881       editor->bCaretAtEnd = 0;
882     }
883     editor->pCursors[2] = editor->pCursors[0];
884     editor->pCursors[3] = editor->pCursors[1];
885     ME_InvalidateSelection(editor);
886     HideCaret(editor->hWnd);
887     ME_MoveCaret(editor);
888     ShowCaret(editor->hWnd);
889     ME_ClearTempStyle(editor);
890     ME_SendSelChange(editor);
891   }
892 }
893
894 void ME_MouseMove(ME_TextEditor *editor, int x, int y)
895 {
896   ME_Cursor tmp_cursor;
897   
898   y += ME_GetYScrollPos(editor);
899
900   tmp_cursor = editor->pCursors[0];
901   /* FIXME: do something with the return value of ME_FindPixelPos */
902   if (!editor->linesel)
903     ME_FindPixelPos(editor, x, y, &tmp_cursor, &editor->bCaretAtEnd);
904   else ME_FindPixelPos(editor, (y > editor->sely) * editor->rcFormat.right, y, &tmp_cursor, &editor->bCaretAtEnd);
905
906   if (!memcmp(&tmp_cursor, editor->pCursors, sizeof(tmp_cursor)))
907     return;
908
909   ME_InvalidateSelection(editor);
910   if (!editor->linesel)
911     editor->pCursors[0] = tmp_cursor;
912   else if (!memcmp(&tmp_cursor, editor->pCursors+2, sizeof(tmp_cursor)) ||
913            !memcmp(&tmp_cursor, editor->pCursors+3, sizeof(tmp_cursor)))
914   {
915     editor->pCursors[0] = editor->pCursors[2];
916     editor->pCursors[1] = editor->pCursors[3];
917   }
918   else if (y < editor->sely)
919   {
920     editor->pCursors[0] = tmp_cursor;
921     editor->pCursors[1] = editor->pCursors[2];
922   }
923   else
924   {
925     editor->pCursors[0] = tmp_cursor;
926     editor->pCursors[1] = editor->pCursors[3];
927   }
928
929   HideCaret(editor->hWnd);
930   ME_MoveCaret(editor);
931   ME_InvalidateSelection(editor);
932   ShowCaret(editor->hWnd);
933   ME_SendSelChange(editor);
934 }
935
936 static ME_DisplayItem *ME_FindRunInRow(ME_TextEditor *editor, ME_DisplayItem *pRow, 
937                                 int x, int *pOffset, int *pbCaretAtEnd)
938 {
939   ME_DisplayItem *pNext, *pLastRun;
940   pNext = ME_FindItemFwd(pRow, diRunOrStartRow);
941   assert(pNext->type == diRun);
942   pLastRun = pNext;
943   *pbCaretAtEnd = FALSE;
944   do {
945     int run_x = pNext->member.run.pt.x;
946     int width = pNext->member.run.nWidth;
947     if (x < run_x)
948     {
949       if (pOffset) *pOffset = 0;
950       return pNext;
951     }
952     if (x >= run_x && x < run_x+width)
953     {
954       int ch = ME_CharFromPointCursor(editor, x-run_x, &pNext->member.run);
955       ME_String *s = pNext->member.run.strText;
956       if (ch < s->nLen) {
957         if (pOffset)
958           *pOffset = ch;
959         return pNext;          
960       }
961     }
962     pLastRun = pNext;
963     pNext = ME_FindItemFwd(pNext, diRunOrStartRow);
964   } while(pNext && pNext->type == diRun);
965   
966   if ((pLastRun->member.run.nFlags & MERF_ENDPARA) == 0)
967   {
968     pNext = ME_FindItemFwd(pNext, diRun);
969     if (pbCaretAtEnd) *pbCaretAtEnd = 1;
970     if (pOffset) *pOffset = 0;
971     return pNext;
972   } else {
973     if (pbCaretAtEnd) *pbCaretAtEnd = 0;
974     if (pOffset) *pOffset = 0;
975     return pLastRun;
976   }
977 }
978
979 static int ME_GetXForArrow(ME_TextEditor *editor, ME_Cursor *pCursor)
980 {
981   ME_DisplayItem *pRun = pCursor->pRun;
982   int x;
983
984   if (editor->nUDArrowX != -1)
985     x = editor->nUDArrowX;
986   else {
987     if (editor->bCaretAtEnd)
988     {
989       pRun = ME_FindItemBack(pRun, diRun);
990       assert(pRun);
991       x = pRun->member.run.pt.x + pRun->member.run.nWidth;
992     }
993     else {
994       x = pRun->member.run.pt.x;
995       x += ME_PointFromChar(editor, &pRun->member.run, pCursor->nOffset);
996     }
997     editor->nUDArrowX = x;
998   }
999   return x;
1000 }
1001
1002
1003 static void
1004 ME_MoveCursorLines(ME_TextEditor *editor, ME_Cursor *pCursor, int nRelOfs)
1005 {
1006   ME_DisplayItem *pRun = pCursor->pRun;
1007   ME_DisplayItem *pItem;
1008   int x = ME_GetXForArrow(editor, pCursor);
1009
1010   if (editor->bCaretAtEnd && !pCursor->nOffset)
1011     pRun = ME_FindItemBack(pRun, diRun);
1012   if (!pRun)
1013     return;
1014   if (nRelOfs == -1)
1015   {
1016     /* start of this row */
1017     pItem = ME_FindItemBack(pRun, diStartRow);
1018     assert(pItem);
1019     /* start of the previous row */
1020     pItem = ME_FindItemBack(pItem, diStartRow);
1021   }
1022   else
1023   {
1024     /* start of the next row */
1025     pItem = ME_FindItemFwd(pRun, diStartRow);
1026     /* FIXME If diParagraph is before diStartRow, wrap the next paragraph?
1027     */
1028   }
1029   if (!pItem)
1030   {
1031     /* row not found - ignore */
1032     return;
1033   }
1034   pCursor->pRun = ME_FindRunInRow(editor, pItem, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1035   assert(pCursor->pRun);
1036   assert(pCursor->pRun->type == diRun);
1037 }
1038
1039
1040 static void ME_ArrowPageUp(ME_TextEditor *editor, ME_Cursor *pCursor)
1041 {
1042   ME_DisplayItem *pRun = pCursor->pRun;
1043   ME_DisplayItem *pLast, *p;
1044   int x, y, ys, yd, yp, yprev;
1045   ME_Cursor tmp_curs = *pCursor;
1046   
1047   x = ME_GetXForArrow(editor, pCursor);
1048   if (!pCursor->nOffset && editor->bCaretAtEnd)
1049     pRun = ME_FindItemBack(pRun, diRun);
1050   
1051   p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1052   assert(p->type == diStartRow);
1053   yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1054   yprev = ys = y = yp + p->member.row.nYPos;
1055   yd = y - editor->sizeWindow.cy;
1056   pLast = p;
1057   
1058   do {
1059     p = ME_FindItemBack(p, diStartRowOrParagraph);
1060     if (!p)
1061       break;
1062     if (p->type == diParagraph) { /* crossing paragraphs */
1063       if (p->member.para.prev_para == NULL)
1064         break;
1065       yp = p->member.para.prev_para->member.para.nYPos;
1066       continue;
1067     }
1068     y = yp + p->member.row.nYPos;
1069     if (y < yd)
1070       break;
1071     pLast = p;
1072     yprev = y;
1073   } while(1);
1074   
1075   pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1076   ME_UpdateSelection(editor, &tmp_curs);
1077   if (yprev < editor->sizeWindow.cy)
1078   {
1079     ME_EnsureVisible(editor, ME_FindItemFwd(editor->pBuffer->pFirst, diRun));
1080     ME_Repaint(editor);
1081   }
1082   else 
1083   {
1084     ME_ScrollUp(editor, ys-yprev);
1085   }
1086   assert(pCursor->pRun);
1087   assert(pCursor->pRun->type == diRun);
1088 }
1089
1090 /* FIXME: in the original RICHEDIT, PageDown always scrolls by the same amount 
1091    of pixels, even if it makes the scroll bar position exceed its normal maximum.
1092    In such a situation, clicking the scrollbar restores its position back to the
1093    normal range (ie. sets it to (doclength-screenheight)). */
1094
1095 static void ME_ArrowPageDown(ME_TextEditor *editor, ME_Cursor *pCursor)
1096 {
1097   ME_DisplayItem *pRun = pCursor->pRun;
1098   ME_DisplayItem *pLast, *p;
1099   int x, y, ys, yd, yp, yprev;
1100   ME_Cursor tmp_curs = *pCursor;
1101   
1102   x = ME_GetXForArrow(editor, pCursor);
1103   if (!pCursor->nOffset && editor->bCaretAtEnd)
1104     pRun = ME_FindItemBack(pRun, diRun);
1105   
1106   p = ME_FindItemBack(pRun, diStartRowOrParagraph);
1107   assert(p->type == diStartRow);
1108   yp = ME_FindItemBack(p, diParagraph)->member.para.nYPos;
1109   yprev = ys = y = yp + p->member.row.nYPos;
1110   yd = y + editor->sizeWindow.cy;
1111   pLast = p;
1112   
1113   do {
1114     p = ME_FindItemFwd(p, diStartRowOrParagraph);
1115     if (!p)
1116       break;
1117     if (p->type == diParagraph) {
1118       yp = p->member.para.nYPos;
1119       continue;
1120     }
1121     y = yp + p->member.row.nYPos;
1122     if (y >= yd)
1123       break;
1124     pLast = p;
1125     yprev = y;
1126   } while(1);
1127   
1128   pCursor->pRun = ME_FindRunInRow(editor, pLast, x, &pCursor->nOffset, &editor->bCaretAtEnd);
1129   ME_UpdateSelection(editor, &tmp_curs);
1130   if (yprev >= editor->nTotalLength-editor->sizeWindow.cy)
1131   {
1132     ME_EnsureVisible(editor, ME_FindItemBack(editor->pBuffer->pLast, diRun));
1133     ME_Repaint(editor);
1134   }
1135   else 
1136   {
1137     ME_ScrollUp(editor,ys-yprev);
1138   }
1139   assert(pCursor->pRun);
1140   assert(pCursor->pRun->type == diRun);
1141 }
1142
1143 static void ME_ArrowHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1144 {
1145   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diStartRow);
1146   /* bCaretAtEnd doesn't make sense if the cursor isn't set at the
1147   first character of the next row */
1148   assert(!editor->bCaretAtEnd || !pCursor->nOffset);
1149   ME_WrapMarkedParagraphs(editor);
1150   if (pRow) {
1151     ME_DisplayItem *pRun;
1152     if (editor->bCaretAtEnd && !pCursor->nOffset) {
1153       pRow = ME_FindItemBack(pRow, diStartRow);
1154       if (!pRow)
1155         return;
1156     }
1157     pRun = ME_FindItemFwd(pRow, diRun);
1158     if (pRun) {
1159       pCursor->pRun = pRun;
1160       pCursor->nOffset = 0;
1161     }
1162   }
1163   editor->bCaretAtEnd = FALSE;
1164 }
1165
1166 static void ME_ArrowCtrlHome(ME_TextEditor *editor, ME_Cursor *pCursor)
1167 {
1168   ME_DisplayItem *pRow = ME_FindItemBack(pCursor->pRun, diTextStart);
1169   if (pRow) {
1170     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1171     if (pRun) {
1172       pCursor->pRun = pRun;
1173       pCursor->nOffset = 0;
1174     }
1175   }
1176 }
1177
1178 static void ME_ArrowEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1179 {
1180   ME_DisplayItem *pRow;
1181   
1182   if (editor->bCaretAtEnd && !pCursor->nOffset)
1183     return;
1184   
1185   pRow = ME_FindItemFwd(pCursor->pRun, diStartRowOrParagraphOrEnd);
1186   assert(pRow);
1187   if (pRow->type == diStartRow) {
1188     /* FIXME WTF was I thinking about here ? */
1189     ME_DisplayItem *pRun = ME_FindItemFwd(pRow, diRun);
1190     assert(pRun);
1191     pCursor->pRun = pRun;
1192     pCursor->nOffset = 0;
1193     editor->bCaretAtEnd = 1;
1194     return;
1195   }
1196   pCursor->pRun = ME_FindItemBack(pRow, diRun);
1197   assert(pCursor->pRun && pCursor->pRun->member.run.nFlags & MERF_ENDPARA);
1198   pCursor->nOffset = 0;
1199   editor->bCaretAtEnd = FALSE;
1200 }
1201       
1202 static void ME_ArrowCtrlEnd(ME_TextEditor *editor, ME_Cursor *pCursor)
1203 {
1204   ME_DisplayItem *p = ME_FindItemFwd(pCursor->pRun, diTextEnd);
1205   assert(p);
1206   p = ME_FindItemBack(p, diRun);
1207   assert(p);
1208   assert(p->member.run.nFlags & MERF_ENDPARA);
1209   pCursor->pRun = p;
1210   pCursor->nOffset = 0;
1211   editor->bCaretAtEnd = FALSE;
1212 }
1213
1214 BOOL ME_IsSelection(ME_TextEditor *editor)
1215 {
1216   return memcmp(&editor->pCursors[0], &editor->pCursors[1], sizeof(ME_Cursor))!=0;
1217 }
1218
1219 static int ME_GetSelCursor(ME_TextEditor *editor, int dir)
1220 {
1221   int cdir = ME_GetCursorOfs(editor, 0) - ME_GetCursorOfs(editor, 1);
1222   
1223   if (cdir*dir>0)
1224     return 0;
1225   else
1226     return 1;
1227 }
1228
1229 BOOL ME_UpdateSelection(ME_TextEditor *editor, const ME_Cursor *pTempCursor)
1230 {
1231   ME_Cursor old_anchor = editor->pCursors[1];
1232   
1233   if (GetKeyState(VK_SHIFT)>=0) /* cancelling selection */
1234   {
1235     /* any selection was present ? if so, it's no more, repaint ! */
1236     editor->pCursors[1] = editor->pCursors[0];
1237     if (memcmp(pTempCursor, &old_anchor, sizeof(ME_Cursor))) {
1238       return TRUE;
1239     }
1240     return FALSE;
1241   }
1242   else
1243   {
1244     if (!memcmp(pTempCursor, &editor->pCursors[1], sizeof(ME_Cursor))) /* starting selection */
1245     {
1246       editor->pCursors[1] = *pTempCursor;
1247       return TRUE;
1248     }
1249   }
1250
1251   ME_Repaint(editor);
1252   return TRUE;
1253 }
1254
1255 void ME_DeleteSelection(ME_TextEditor *editor)
1256 {
1257   int from, to;
1258   ME_GetSelection(editor, &from, &to);
1259   ME_DeleteTextAtCursor(editor, ME_GetSelCursor(editor,-1), to-from);
1260 }
1261
1262 ME_Style *ME_GetSelectionInsertStyle(ME_TextEditor *editor)
1263 {
1264   ME_Style *style;
1265   int from, to;
1266   ME_Cursor c;
1267   
1268   ME_GetSelection(editor, &from, &to);
1269   ME_CursorFromCharOfs(editor, from, &c);
1270   if (from != to) {
1271     style = c.pRun->member.run.style;
1272     ME_AddRefStyle(style); /* ME_GetInsertStyle has already done that */
1273   }
1274   else
1275     style = ME_GetInsertStyle(editor, 0);
1276   return style;
1277 }
1278
1279 void ME_SendSelChange(ME_TextEditor *editor)
1280 {
1281   SELCHANGE sc;
1282
1283   ME_ClearTempStyle(editor);
1284   
1285   if (!(editor->nEventMask & ENM_SELCHANGE))
1286     return;
1287   
1288   sc.nmhdr.hwndFrom = editor->hWnd;
1289   sc.nmhdr.idFrom = GetWindowLongW(editor->hWnd, GWLP_ID);
1290   sc.nmhdr.code = EN_SELCHANGE;
1291   SendMessageW(editor->hWnd, EM_EXGETSEL, 0, (LPARAM)&sc.chrg);
1292   sc.seltyp = SEL_EMPTY;
1293   if (sc.chrg.cpMin != sc.chrg.cpMax)
1294     sc.seltyp |= SEL_TEXT;
1295   if (sc.chrg.cpMin < sc.chrg.cpMax+1) /* wth were RICHEDIT authors thinking ? */
1296     sc.seltyp |= SEL_MULTICHAR;
1297   SendMessageW(GetParent(editor->hWnd), WM_NOTIFY, sc.nmhdr.idFrom, (LPARAM)&sc);
1298 }
1299
1300 BOOL
1301 ME_ArrowKey(ME_TextEditor *editor, int nVKey, BOOL extend, BOOL ctrl)
1302 {
1303   int nCursor = 0;
1304   ME_Cursor *p = &editor->pCursors[nCursor];
1305   ME_Cursor tmp_curs = *p;
1306   BOOL success = FALSE;
1307   
1308   ME_CheckCharOffsets(editor);
1309   editor->nUDArrowX = -1;
1310   switch(nVKey) {
1311     case VK_LEFT:
1312       editor->bCaretAtEnd = 0;
1313       if (ctrl)
1314         success = ME_MoveCursorWords(editor, &tmp_curs, -1);
1315       else
1316         success = ME_MoveCursorChars(editor, &tmp_curs, -1);
1317       break;
1318     case VK_RIGHT:
1319       editor->bCaretAtEnd = 0;
1320       if (ctrl)
1321         success = ME_MoveCursorWords(editor, &tmp_curs, +1);
1322       else
1323         success = ME_MoveCursorChars(editor, &tmp_curs, +1);
1324       break;
1325     case VK_UP:
1326       ME_MoveCursorLines(editor, &tmp_curs, -1);
1327       break;
1328     case VK_DOWN:
1329       ME_MoveCursorLines(editor, &tmp_curs, +1);
1330       break;
1331     case VK_PRIOR:
1332       ME_ArrowPageUp(editor, &tmp_curs);
1333       break;
1334     case VK_NEXT:
1335       ME_ArrowPageDown(editor, &tmp_curs);
1336       break;
1337     case VK_HOME: {
1338       if (ctrl)
1339         ME_ArrowCtrlHome(editor, &tmp_curs);
1340       else
1341         ME_ArrowHome(editor, &tmp_curs);
1342       editor->bCaretAtEnd = 0;
1343       break;
1344     }
1345     case VK_END: 
1346       if (ctrl)
1347         ME_ArrowCtrlEnd(editor, &tmp_curs);
1348       else
1349         ME_ArrowEnd(editor, &tmp_curs);
1350       break;
1351   }
1352   
1353   if (!extend)
1354     editor->pCursors[1] = tmp_curs;
1355   *p = tmp_curs;
1356   
1357   ME_InvalidateSelection(editor);
1358   ME_Repaint(editor);
1359   HideCaret(editor->hWnd);
1360   ME_EnsureVisible(editor, tmp_curs.pRun); 
1361   ME_ShowCaret(editor);
1362   ME_SendSelChange(editor);
1363   return success;
1364 }