From 960cb2c18e20efe428653f8832e488aac93a6603 Mon Sep 17 00:00:00 2001 From: H.G.Muller Date: Wed, 9 Mar 2016 23:15:22 +0100 Subject: [PATCH] Implement rough help popup A routine is added that searches a given text header in the man file, and then displays the following text until the next header (after skipping additional headers that come before any text) in a message box. The text uses a simple fill and wrap to line length > 80, and ignores all control characters, except empty lines. Both .B and .SS directives are recognized as headers. --- dialogs.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ gtk/xoptions.c | 4 +--- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/dialogs.c b/dialogs.c index 5eb3687..3279a5f 100644 --- a/dialogs.c +++ b/dialogs.c @@ -2426,6 +2426,59 @@ DisplayTitle (char *text) SetWindowTitle(text, title, icon); } +static char * +ReadLine (FILE *f) +{ + static char buf[MSG_SIZ]; + int i = 0, c; + while((c = fgetc(f)) != '\n') { if(c == EOF) return NULL; buf[i++] = c; } + buf[i] = NULLCHAR; + return buf; +} + +void +GetHelpText (FILE *f, char *name) +{ + char *line, buf[MSG_SIZ], text[10000], *p = text, *q = text; + int len, cnt = 0; + snprintf(buf, MSG_SIZ, ".B %s", name); + len = strlen(buf); + for(len=1; buf[len] == ' ' || isalpha(buf[len]) || isdigit(buf[len]); len++); + buf[len] = NULLCHAR; + while(buf[--len] == ' ') buf[len] = NULLCHAR; + while((line = ReadLine(f))) { + if(!strncmp(line, buf, len) && (strncmp(line, ".SS ", 4) || strncmp(line+4, buf+3, len-3))) { + while((line = ReadLine(f)) && (cnt == 0 || strncmp(line, ".B ", 3))) { + if(!*line) { *p++ = '\n'; *p++ = '\n'; q = p; continue; } + if(*line == '.') continue; + *p++ = ' '; cnt++; + while(*line) { + if(*line < ' ') { line++; continue;} + if(*line == ' ' && p - q > 80) *line = '\n', q = p; + *p++ = *line++; + } + if(p - text > 9900) break; + } + *p = NULLCHAR; + DisplayNote(text); + return; + } + } + snprintf(text, MSG_SIZ, "No help on '%s'\n", buf+3); + DisplayNote(text); +} + +void +DisplayHelp (char *name) +{ + char *manFile = DATADIR "/../../man/man6/xboard.6"; + FILE *f = fopen(manFile, "r"); + if(f) { + GetHelpText(f, name); + fclose(f); + } +} + #define PAUSE_BUTTON "P" #define PIECE_MENU_SIZE 18 static String pieceMenuStrings[2][PIECE_MENU_SIZE+1] = { diff --git a/gtk/xoptions.c b/gtk/xoptions.c index e7fb43c..a3a54ea 100644 --- a/gtk/xoptions.c +++ b/gtk/xoptions.c @@ -375,10 +375,8 @@ HelpEvent(GtkWidget *widget, GdkEventButton *event, gpointer gdata) { // intercept button3 clicks to pop up help // Option *opt = (Option *) gdata; char *msg = (char *) gdata; - char buf[MSG_SIZ]; if(event->button != 3) return FALSE; - snprintf(buf, MSG_SIZ, "Help on:\n%s", msg); - DisplayNote(buf); + DisplayHelp(msg); return TRUE; } -- 1.7.0.4