Skip to content

Commit

Permalink
Move between multiple search results on the same line
Browse files Browse the repository at this point in the history
  • Loading branch information
makuke1234 committed Jun 6, 2023
1 parent e94d93c commit 317f1ab
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Have a look at the provided [settings file](./settings.json) to get a better und
* 2.3 (planned)
* [x] More advanced program help, supports keywords
* [x] Version info
* [x] Lets move between multiple search results on the same line
* [ ] Advanced theming support, load themes from separate files
* [ ] Advanced undo/redo, choose which way to redo

Expand Down
2 changes: 1 addition & 1 deletion src/fCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ typedef unsigned char uchar;

#define FEMTO_UNTITLED_NAME L"untitled"

#define MAX_STATUS 256
#define MAX_STATUS 512

#define FEMTO_SHIFT_DEL 0xE000
#define FEMTO_MOVELINE_UP 0xE001
Expand Down
1 change: 1 addition & 0 deletions src/fData.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bool fData_reset(fData_t * restrict self)
.fileIdx = -1,
.searchBuf = { [0] = L'\0' },
.psearchTerm = NULL,
.searchOpts = fsrchFIRST,
.bDirBack = false
};

Expand Down
1 change: 1 addition & 0 deletions src/fData.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct fData

wchar searchBuf[MAX_STATUS];
const wchar * psearchTerm;
fSearch_e searchOpts;
bool bDirBack;

} fData_t;
Expand Down
80 changes: 76 additions & 4 deletions src/fLine.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,58 @@ usize fLine_find(const fLine_t * restrict node, usize startIdx, const wchar * re

return UINT32_MAX;
}
usize fLine_rfind(const fLine_t * restrict node, usize startIdx, const wchar * restrict string, usize maxString)
{
assert(node != NULL);
assert(string != NULL);
assert(maxString > 0);

// Clamp startIdx
const usize cur2 = node->curx + node->freeSpaceLen;
startIdx = ((startIdx >= node->curx) && (startIdx < cur2)) ? cur2 : startIdx;

for (usize i = startIdx; 1;)
{
if ((i == (cur2 - 1)) && (node->freeSpaceLen > 0))
{
i -= node->freeSpaceLen;
continue;
}
else if (node->line[i] == string[0])
{
const wchar * str = string;
usize k = 0;
for (usize j = i; (j < node->lineEndx) && (k < maxString);)
{
if ((j == node->curx) && (node->freeSpaceLen > 0))
{
j = cur2;
continue;
}
else if ((*str == L'\0') || (node->line[j] != *str))
{
break;
}
++str;
++k;
++j;
}

if ((k == maxString) || (*str == L'\0'))
{
return i;
}
}
if (i == 0)
{
break;
}

--i;
}

return UINT32_MAX;
}

bool fLine_mergeNext(fLine_t * restrict self, fLine_t ** restrict ppcury, u8 * restrict noLen)
{
Expand Down Expand Up @@ -589,7 +641,7 @@ void fLine_updateLineNumbers(fLine_t * restrict startnode, usize startLno, u8 *

bool fLine_updateSyntax(
fLine_t * restrict node, fStx_e fs, const WORD * colors,
const wchar * restrict searchTerm, const struct fFileHighLight * restrict hl,
const wchar * restrict searchTerm, fSearch_e searchOpts, const struct fFileHighLight * restrict hl,
usize curLineNum, u8 tabWidth
)
{
Expand Down Expand Up @@ -661,9 +713,29 @@ bool fLine_updateSyntax(
{
break;
}

firstidx = first ? idx : firstidx;
first = false;

switch (searchOpts)
{
case fsrchFIRST:
firstidx = first ? idx : firstidx;
first = false;
break;
case fsrchLAST:
firstidx = idx;
first = false;
break;
case fsrchPREV:
firstidx = (idx < node->curx) ? idx : firstidx;
first = false;
break;
case fsrchNEXT:
if (first && (idx > (node->curx + node->freeSpaceLen)))
{
firstidx = idx;
first = false;
}
break;
}
// Found
node->userValue.bits.b8 = true;

Expand Down
24 changes: 22 additions & 2 deletions src/fLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ typedef struct fLine

} fLine_t;

typedef enum fSearch
{
fsrchFIRST,
fsrchLAST,
fsrchPREV,
fsrchNEXT
} fSearch_e;


/**
* @brief Initialises already pre-allocated memory of fLine_t structure
Expand Down Expand Up @@ -138,7 +146,7 @@ bool fLine_addChar(fLine_t * restrict self, wchar ch, u8 tabWidth);
*/
bool fLine_checkAt(const fLine_t * restrict node, isize maxdelta, const wchar * restrict string, usize maxString);
/**
* @brief Finds the string location on current line
* @brief Finds the string location on current line iterating forwards
*
* @param node Pointer to line node
* @param startIdx Starting index, value is clamped
Expand All @@ -147,6 +155,17 @@ bool fLine_checkAt(const fLine_t * restrict node, isize maxdelta, const wchar *
* @return usize Index location of string, UINT32_MAX on failure
*/
usize fLine_find(const fLine_t * restrict node, usize startIdx, const wchar * restrict string, usize maxString);
/**
* @brief Finds the string location on current line iterating backwards from the starting point
*
* @param node Pointer to line node
* @param startIdx Starting index, value is clamped
* @param string Matchable string
* @param maxString Absolute maximum number of character to check, stops anyway on null-terminator
* @return usize Index location of string, UINT32_MAX on failure
*/
usize fLine_rfind(const fLine_t * restrict node, usize startIdx, const wchar * restrict string, usize maxString);


/**
* @brief Merges current line node with next line node, adjusts current
Expand Down Expand Up @@ -223,6 +242,7 @@ void fLine_updateLineNumbers(fLine_t * restrict startnode, usize startLno, u8 *
* @param fs Syntax identifier
* @param colors Syntax token coloring palette
* @param searchTerm Phrase to be searched, can be NULL
* @param searchOpts Search options
* @param hl Highlighting data
* @param curLineNum Current active line number
* @param tabWidth Tab width in characters
Expand All @@ -231,7 +251,7 @@ void fLine_updateLineNumbers(fLine_t * restrict startnode, usize startLno, u8 *
*/
bool fLine_updateSyntax(
fLine_t * restrict node, fStx_e fs, const WORD * colors,
const wchar * restrict searchTerm, const struct fFileHighLight * restrict hl,
const wchar * restrict searchTerm, fSearch_e searchOpts, const struct fFileHighLight * restrict hl,
usize curLineNum, u8 tabWidth
);

Expand Down
34 changes: 29 additions & 5 deletions src/femto.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,33 @@ static inline void s_femto_inner_searchTerm(fData_t * restrict peditor, wchar *
wcscpy_s(tempstr, MAX_STATUS, L"No lines to be searched");
return;
}
//
node = first ? node : ((peditor->bDirBack) ? node->prevNode : node->nextNode);
isize deltaLines = first ? 0 : delta;

// try finding another term on the same line
bool foundOnSameLine = false;
if (peditor->bDirBack)
{
// search backwards
foundOnSameLine = (node->curx > 0) && (fLine_rfind(node, node->curx - 1, peditor->psearchTerm, wcslen(peditor->psearchTerm)) != UINT32_MAX);
}
else
{
// search forwards
foundOnSameLine = fLine_find(node, node->curx + node->freeSpaceLen + 1, peditor->psearchTerm, wcslen(peditor->psearchTerm)) != UINT32_MAX;
}


// that didn't work, multiple search terms weren't found
isize deltaLines = 0;
if (!foundOnSameLine)
{
node = first ? node : ((peditor->bDirBack) ? node->prevNode : node->nextNode);
deltaLines = first ? 0 : delta;
peditor->searchOpts = (peditor->bDirBack) ? fsrchLAST : fsrchFIRST;
}
else
{
peditor->searchOpts = (peditor->bDirBack) ? fsrchPREV : fsrchNEXT;
}

if (first)
{
Expand Down Expand Up @@ -1568,7 +1592,7 @@ bool femto_updateScrbuf(fData_t * restrict peditor, u32 * restrict curline)
{
fLine_updateSyntax(
node, pfile->syntax, peditor->settings.syntaxColors,
peditor->psearchTerm, &pfile->data.hl,
peditor->psearchTerm, peditor->searchOpts, &pfile->data.hl,
pfile->data.currentNode->lineNumber, peditor->settings.tabWidth
);

Expand Down Expand Up @@ -1708,7 +1732,7 @@ bool femto_updateScrbufLine(fData_t * restrict peditor, fLine_t * restrict node,

if (!fLine_updateSyntax(
node, pfile->syntax, peditor->settings.syntaxColors,
peditor->psearchTerm, &pfile->data.hl,
peditor->psearchTerm, peditor->searchOpts, &pfile->data.hl,
pfile->data.currentNode->lineNumber, peditor->settings.tabWidth
))
{
Expand Down

0 comments on commit 317f1ab

Please sign in to comment.