Skip to content

Commit 2873198

Browse files
committed
Add mouse highlighting
1 parent d5e6a25 commit 2873198

File tree

6 files changed

+61
-26
lines changed

6 files changed

+61
-26
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ reports ~6400 source code lines (excluding whitespace, comments, language keywor
2525

2626
# Features
2727

28+
- [ ] Built-in copy-pasting support
2829
- [x] All saved files use CRLF line-ending format by default, LF and CR are also supported
2930
* *Editing text files is sublime*
3031
- [x] The femto editor utilizes the whole command prompt window, window is as big as your console currently is
@@ -64,6 +65,9 @@ reports ~6400 source code lines (excluding whitespace, comments, language keywor
6465
| <kbd>Ctrl+Q</kbd> | Same as above |
6566
| <kbd>Shift+ESC</kbd> | Force exit editor |
6667
| <kbd>Ctrl+Shift+Q</kbd> | Same as above |
68+
| <kbd>Ctrl+X</kbd> | Cut |
69+
| <kbd>Ctrl+C</kbd> | Copy |
70+
| <kbd>Ctrl+V</kbd> | Paste |
6771
| <kbd>Ctrl+N</kbd> | Create new blank file |
6872
| <kbd>Ctrl+O</kbd> | Open file |
6973
| <kbd>Ctrl+S</kbd> | Save file |
@@ -200,8 +204,8 @@ Have a look at the provided [settings file](./settings.json) to get a better und
200204
# Changelog
201205

202206
* 2.1 (planned)
207+
* [x] Text highlighting using keyboard & mouse
203208
* [ ] Proper text copying/pasting support
204-
* *Copying is already implicitly supported by the console itself, although limited to only 1 screenbuffer at a time*
205209
* [x] Port codebase to use universal integer sizes `size_t` & `ssize_t` (signed)
206210
* [x] Properly fixed search functionality
207211

images/sloc.png

-49 Bytes
Loading

src/fData.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void fData_cancelHighlight(fData_t * restrict self)
316316

317317
pfile->data.hl.beg = NULL;
318318
pfile->data.bUpdateAll = true;
319-
fData_refreshEdit(self);
319+
fData_refreshEditAsync(self);
320320
}
321321

322322
bool fData_openTab(fData_t * restrict self, const wchar * restrict fileName)

src/fFile.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,6 @@ bool fFile_addNormalCh(fFile_t * restrict self, wchar ch, u8 tabWidth)
402402
return true;
403403
}
404404

405-
#if SSIZE_MAX == INT64_MAX
406-
#define USIZE_BIT_MASK 0x7fffffffffffffff
407-
#else
408-
#define USIZE_BIT_MASK 0x7fffffff
409-
#endif
410405

411406
bool fFile_startHighlighting(fFile_t * restrict self, wchar ch, bool shift)
412407
{
@@ -432,7 +427,7 @@ bool fFile_startHighlighting(fFile_t * restrict self, wchar ch, bool shift)
432427
if (hl->beg == NULL)
433428
{
434429
hl->beg = node;
435-
hl->begx = node->curx & USIZE_BIT_MASK;
430+
hl->begx = node->curx & USIZE_BIT_1_MASK;
436431
}
437432

438433
hl->backwards = ((hl->beg == node) && ((ch == VK_UP) || ((hl->begx > node->curx) || ((hl->begx == node->curx) && (ch == VK_LEFT)))) ) ||
@@ -442,7 +437,6 @@ bool fFile_startHighlighting(fFile_t * restrict self, wchar ch, bool shift)
442437

443438
return hl->beg != NULL;
444439
}
445-
#undef USIZE_BIT_MASK
446440

447441

448442
bool fFile_addSpecialCh(

src/fFile.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ typedef enum eolSequence
2020
} eolSequence_e, eolSeq_e;
2121

2222
#if SSIZE_MAX == INT64_MAX
23-
#define MAX_USIZE_BITS_1 63
23+
#define USIZE_BITS_1 63
2424
#else
25-
#define MAX_USIZE_BITS_1 31
25+
#define USIZE_BITS_1 31
26+
#endif
27+
28+
#if SSIZE_MAX == INT64_MAX
29+
#define USIZE_BIT_1_MASK 0x7fffffffffffffff
30+
#else
31+
#define USIZE_BIT_1_MASK 0x7fffffff
2632
#endif
2733

2834
typedef struct fFile
@@ -48,7 +54,7 @@ typedef struct fFile
4854
const fLine_t * beg;
4955
// Also store the beginning cursor of beginning line, if the index
5056
// is after beg->curx, beg->freeSpaceLen will be subtracted
51-
usize begx:MAX_USIZE_BITS_1;
57+
usize begx:USIZE_BITS_1;
5258
// Indicates whether the highlighting is backwards or forwards
5359
usize backwards:1;
5460
} hl;
@@ -66,7 +72,7 @@ typedef struct fFile
6672

6773
} fFile_t;
6874

69-
#undef MAX_USIZE_BITS_1
75+
#undef USIZE_BITS_1
7076

7177
/**
7278
* @brief Resets fFile_t structure memory layout, zeroes all members

src/femto.c

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ static inline bool s_femto_inner_kbdHandle(
906906
break;
907907
case VK_TAB:
908908
{
909+
fData_cancelHighlight(peditor);
909910
// Shuffle between tabs
910911
if ((GetAsyncKeyState(VK_LCONTROL) & 0x8000) || (GetAsyncKeyState(VK_RCONTROL) & 0x8000))
911912
{
@@ -944,12 +945,14 @@ static inline bool s_femto_inner_kbdHandle(
944945
}
945946
case VK_F2:
946947
case VK_F3:
948+
fData_cancelHighlight(peditor);
947949
send = false;
948950
peditor->bDirBack = (wVirtKey == VK_F2);
949951
s_femto_inner_searchTerm(peditor, tempstr, false);
950952
break;
951953
case VK_DELETE:
952954
{
955+
fData_cancelHighlight(peditor);
953956
// Check for shift to alt key
954957
if (shift ^ ((GetAsyncKeyState(VK_LMENU) & 0x8000) || (GetAsyncKeyState(VK_RMENU) & 0x8000)))
955958
{
@@ -966,6 +969,7 @@ static inline bool s_femto_inner_kbdHandle(
966969
// Check for alt key
967970
if ((GetAsyncKeyState(VK_LMENU) & 0x8000) || (GetAsyncKeyState(VK_RMENU) & 0x8000))
968971
{
972+
fData_cancelHighlight(peditor);
969973
swprintf_s(tempstr, MAX_STATUS, L"'ALT' + \u2191 #%u", keyCount);
970974
wVirtKey = FEMTO_MOVELINE_UP;
971975
}
@@ -978,6 +982,7 @@ static inline bool s_femto_inner_kbdHandle(
978982
// Check for alt key
979983
if ((GetAsyncKeyState(VK_LMENU) & 0x8000) || (GetAsyncKeyState(VK_RMENU) & 0x8000))
980984
{
985+
fData_cancelHighlight(peditor);
981986
swprintf_s(tempstr, MAX_STATUS, L"'ALT' + \u2193 #%u", keyCount);
982987
wVirtKey = FEMTO_MOVELINE_DOWN;
983988
}
@@ -988,12 +993,14 @@ static inline bool s_femto_inner_kbdHandle(
988993
break;
989994
case VK_RETURN: // Enter key
990995
case VK_BACK: // Backspace
991-
case VK_LEFT: // Left arrow
992-
case VK_RIGHT: // Right arrow
993996
case VK_PRIOR: // Page up
994997
case VK_NEXT: // Page down
995998
case VK_END:
996999
case VK_HOME:
1000+
fData_cancelHighlight(peditor);
1001+
/* fall through */
1002+
case VK_LEFT: // Left arrow
1003+
case VK_RIGHT: // Right arrow
9971004
{
9981005
static const wchar * buf[] = {
9991006
[VK_RETURN] = L"'RET'",
@@ -1161,11 +1168,17 @@ static inline bool s_femto_inner_mouseHandle(
11611168
// If not, don't draw anything on the statusbar
11621169
draw = s_femto_inner_calcMousePos(peditor, ir, &pos);
11631170

1171+
struct fFileHighLight * restrict hl = &pfile->data.hl;
1172+
assert(hl != NULL);
1173+
1174+
bool moved = false;
1175+
11641176
// Check if mouse is moving
11651177
if (ir->dwEventFlags & MOUSE_MOVED)
11661178
{
11671179
if (draw)
11681180
{
1181+
moved = true;
11691182
swprintf_s(tempstr, MAX_STATUS, L"'LCLICK' + MOVE @%hd, %hd", pos.X, pos.Y);
11701183
}
11711184
}
@@ -1175,19 +1188,37 @@ static inline bool s_femto_inner_mouseHandle(
11751188
{
11761189
fProf_write("Mouse click @%hd, %hd", pos.X, pos.Y);
11771190

1178-
if (pfile->data.pcury != NULL)
1191+
fData_cancelHighlight(peditor);
1192+
1193+
swprintf_s(tempstr, MAX_STATUS, L"'LCLICK' @%hd, %hd", pos.X, pos.Y);
1194+
}
1195+
}
1196+
1197+
if (draw)
1198+
{
1199+
if (pfile->data.pcury != NULL)
1200+
{
1201+
pfile->data.currentNode = pfile->data.pcury;
1202+
const fLine_t * restrict lastcurnode = pfile->data.currentNode;
1203+
fLine_moveCursorVert(&pfile->data.currentNode, (isize)pos.Y);
1204+
fLine_t * restrict curNode = pfile->data.currentNode;
1205+
pfile->data.bUpdateAll |= (curNode != lastcurnode) & peditor->settings.bRelLineNums;
1206+
// Now move the cursor to correct X position
1207+
fLine_moveCursorAbs(curNode, fLine_calcCursor(curNode, (usize)pos.X + pfile->data.curx, peditor->settings.tabWidth));
1208+
fLine_calcVirtCursor(curNode, peditor->settings.tabWidth);
1209+
pfile->data.lastx = curNode->virtcurx;
1210+
1211+
if (moved && (hl->beg == NULL))
11791212
{
1180-
pfile->data.currentNode = pfile->data.pcury;
1181-
const fLine_t * restrict lastcurnode = pfile->data.currentNode;
1182-
fLine_moveCursorVert(&pfile->data.currentNode, (isize)pos.Y);
1183-
pfile->data.bUpdateAll |= (pfile->data.currentNode != lastcurnode) & peditor->settings.bRelLineNums;
1184-
// Now move the cursor to correct X position
1185-
fLine_moveCursorAbs(pfile->data.currentNode, fLine_calcCursor(pfile->data.currentNode, (usize)pos.X + pfile->data.curx, peditor->settings.tabWidth));
1186-
fLine_calcVirtCursor(pfile->data.currentNode, peditor->settings.tabWidth);
1187-
pfile->data.lastx = pfile->data.currentNode->virtcurx;
1188-
fData_refreshEditAsync(peditor);
1213+
hl->beg = pfile->data.currentNode;
1214+
hl->begx = pfile->data.currentNode->curx & USIZE_BIT_1_MASK;
11891215
}
1190-
swprintf_s(tempstr, MAX_STATUS, L"'LCLICK' @%hd, %hd", pos.X, pos.Y);
1216+
if (hl->beg != NULL)
1217+
{
1218+
hl->backwards = (hl->beg->lineNumber > curNode->lineNumber) ||
1219+
((hl->beg == curNode) && (hl->begx > curNode->curx));
1220+
}
1221+
fData_refreshEditAsync(peditor);
11911222
}
11921223
}
11931224
}

0 commit comments

Comments
 (0)