Skip to content

Commit

Permalink
help: Automatically create links for code items
Browse files Browse the repository at this point in the history
When writing `STO` in the help file, automatically add a link to it.

Signed-off-by: Christophe de Dinechin <[email protected]>
  • Loading branch information
c3d committed Oct 28, 2024
1 parent 470a9f0 commit dd2fe3f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 26 deletions.
Binary file modified images/help-help.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/help-page6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/help-page8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 43 additions & 4 deletions src/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ unicode file::get()
}


uint file::find(unicode cp)
uint file::find(unicode cp)
// ----------------------------------------------------------------------------
// Find a given code point in file looking forward
// ----------------------------------------------------------------------------
Expand All @@ -357,13 +357,31 @@ uint file::find(unicode cp)
uint off;
do
{
off = ftell(data);
c = get();
off = ftell(data);
c = get();
} while (c && c != cp);
return off;
}


uint file::find(unicode cp1, unicode cp2)
// ----------------------------------------------------------------------------
// Find a given code point in file looking forward
// ----------------------------------------------------------------------------
// Return position right before code point, position file right after it
{
unicode c;
uint off;
bool in = false;
do
{
off = ftell(data);
c = get();
} while (c && c != cp1 && (c != cp2 || (in = !in)));
return off;
}


uint file::rfind(unicode cp)
// ----------------------------------------------------------------------------
// Find a given code point in file looking backward
Expand All @@ -377,13 +395,34 @@ uint file::rfind(unicode cp)
if (off == 0)
break;
fseek(data, --off, SEEK_SET);
c = get();
c = get();
}
while (c != cp);
return off;
}


uint file::rfind(unicode cp1, unicode cp2)
// ----------------------------------------------------------------------------
// Find a given code point in file looking backward
// ----------------------------------------------------------------------------
// Return position right before code point, position file right after it
{
uint off = ftell(data);
unicode c;
bool in = false;
do
{
if (off == 0)
break;
fseek(data, --off, SEEK_SET);
c = get();
}
while (c != cp1 && (c != cp2 || (in = !in)));
return off;
}


cstring file::error(int err) const
// ----------------------------------------------------------------------------
// Return error from error code
Expand Down
2 changes: 2 additions & 0 deletions src/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ struct file
unicode peek();
uint position();
uint find(unicode cp);
uint find(unicode cp1, unicode cp2);
uint rfind(unicode cp);
uint rfind(unicode cp1, unicode cp2);
cstring error(int err) const;
cstring error() const;
cstring filename() const { return name; }
Expand Down
100 changes: 78 additions & 22 deletions src/user_interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3146,6 +3146,7 @@ enum style_name
KEY,
TOPIC,
HIGHLIGHTED_TOPIC,
HIGHLIGHTED_CODE,
NUM_STYLES
};

Expand Down Expand Up @@ -3179,6 +3180,7 @@ bool user_interface::draw_help()
{ HelpFont, p::white, p::black, false, false, false, false },
{ HelpFont, p::black, p::gray50, false, false, true, false },
{ HelpFont, p::white, p::gray10, false, false, false, false },
{ HelpCodeFont, p::white, p::gray10, false, false, true, true },
};


Expand Down Expand Up @@ -3222,6 +3224,7 @@ bool user_interface::draw_help()
uint codeStart = 0;
uint shown = 0;
bool hadTitle = false;
static char link[60];

// Pun not indented
helpfile.seek(help);
Expand Down Expand Up @@ -3439,9 +3442,50 @@ bool user_interface::draw_help()
case '`':
if (last != '`' && helpfile.peek() != '`')
{
restyle = style == CODE ? NORMAL : CODE;
bool wascode = style == CODE || style == HIGHLIGHTED_CODE;
restyle = wascode ? NORMAL : CODE;
skip = true;
emit = true;


if (!wascode)
{
uint pos = helpfile.position();
unicode n = helpfile.get();
char *p = link;
while (n != '`')
{
if (p < link + sizeof(link) - 1)
*p++ = n;
n = helpfile.get();
}
*p = 0;
size_t cmdlen = p - link;
object::id cmd = command::lookup(utf8(link), cmdlen);
wascode = cmd && cmdlen == size_t(p - link);
helpfile.seek(pos);

if (wascode)
{
lastTopic = pos;
if (topic < shown)
topic = lastTopic;
if (lastTopic == topic)
{
restyle = HIGHLIGHTED_CODE;
codeStart = 0;
}

if (follow && restyle == HIGHLIGHTED_CODE)
{
if (topics_history)
topics[topics_history-1] = shown;
load_help(utf8(link));
Screen.clip(clip);
goto restart;
}
}
}
}
else
{
Expand Down Expand Up @@ -3517,11 +3561,10 @@ bool user_interface::draw_help()
break;
}

static char link[60];
char *p = link;
char *p = link;
while (n != ')')
{
n = helpfile.get();
n = helpfile.get();
if (n != '#')
if (p < link + sizeof(link))
*p++ = n;
Expand Down Expand Up @@ -3564,7 +3607,7 @@ bool user_interface::draw_help()
height = font->height();

// If we are rendering a command name, follow user preferences
if (style == SUBTITLE || style == CODE)
if (style == SUBTITLE || style == CODE || style == HIGHLIGHTED_CODE)
{
size_t cmdlen = widx;
if (object::id cmd = command::lookup(buffer, cmdlen))
Expand Down Expand Up @@ -3667,31 +3710,38 @@ bool user_interface::draw_help()
if (style == VERBATIM)
Screen.fill(xleft, y - 2, xright, y + height * 5 / 4 - 3, bg);

if (underline)
if (box)
{
if (draw)
{
xl -= 2;
xr += 2;
Screen.fill(xl, yf, xr, yf, bg);
xl += 2;
xr -= 2;
xl += 1;
xr += 8;
if (underline)
{
Screen.fill(xl, y, xr, yf, bg);
}
else
{
Screen.fill(xl, yf, xr, yf, bg);
Screen.fill(xl, y, xl, yf, bg);
Screen.fill(xr, y, xr, yf, bg);
Screen.fill(xl, y, xr, y, bg);
}
xl -= 1;
xr -= 8;
}
kwidth += 4;
}
else if (box)
else if (underline)
{
if (draw)
{
xl += 1;
xr += 8;
xl -= 2;
xr += 2;
Screen.fill(xl, yf, xr, yf, bg);
Screen.fill(xl, y, xl, yf, bg);
Screen.fill(xr, y, xr, yf, bg);
Screen.fill(xl, y, xr, y, bg);
xl -= 1;
xr -= 8;
xl += 2;
xr -= 2;
}
kwidth += 4;
}
else if (bg.bits != pattern::white.bits)
{
Expand Down Expand Up @@ -3992,6 +4042,12 @@ bool user_interface::handle_help(int &key)
{
helpfile.seek(help);
help = helpfile.rfind('\n');
unicode next = helpfile.peek();
if (next != '#')
{
count++;
line += height;
}
if (!help)
break;
}
Expand Down Expand Up @@ -4020,7 +4076,7 @@ bool user_interface::handle_help(int &key)
while(count--)
{
helpfile.seek(topic);
topic = helpfile.rfind('[');
topic = helpfile.rfind('[', '`');
}
topic = helpfile.position();
repeat = true;
Expand All @@ -4031,7 +4087,7 @@ bool user_interface::handle_help(int &key)
case KEY_MUL:
helpfile.seek(topic);
while (count--)
helpfile.find('[');
helpfile.find('[', '`');
topic = helpfile.position();
repeat = true;
dirtyHelp = true;
Expand Down

0 comments on commit dd2fe3f

Please sign in to comment.