From: Richard Russon Date: Sun, 1 Apr 2018 02:33:43 +0000 (+0100) Subject: split out terminal setting X-Git-Tag: neomutt-20180512~68 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c406eefcf4618d65b6d872e846aa640fdfdf6153;p=neomutt split out terminal setting --- diff --git a/Makefile.autosetup b/Makefile.autosetup index 4b2787ece..a49de85af 100644 --- a/Makefile.autosetup +++ b/Makefile.autosetup @@ -70,7 +70,7 @@ NEOMUTTOBJS= addrbook.o alias.o attach.o bcache.o body.o browser.o buffy.o \ postpone.o query.o recvattach.o recvcmd.o resize.o rfc1524.o \ rfc2047.o rfc2231.o rfc3676.o safe_asprintf.o score.o send.o \ sendlib.o sidebar.o smtp.o sort.o state.o status.o system.o \ - tags.o thread.o url.o version.o + tags.o terminal.o thread.o url.o version.o @if !HAVE_WCSCASECMP NEOMUTTOBJS+= wcscasecmp.o diff --git a/curs_main.c b/curs_main.c index 56a64238a..77f115c1a 100644 --- a/curs_main.c +++ b/curs_main.c @@ -52,14 +52,8 @@ #include "protos.h" #include "sort.h" #include "tags.h" +#include "terminal.h" #include "thread.h" -#ifdef HAVE_NCURSESW_NCURSES_H -#include -#elif defined(HAVE_NCURSES_NCURSES_H) -#include -#elif !defined(USE_SLANG_CURSES) -#include -#endif #ifdef USE_SIDEBAR #include "sidebar.h" #endif @@ -145,10 +139,6 @@ static const char *NoVisible = N_("No visible messages."); #define CAN_COLLAPSE(header) \ ((CollapseUnread || !UNREAD(header)) && (CollapseFlagged || !FLAGGED(header))) -/* de facto standard escapes for tsl/fsl */ -static char *tsl = "\033]0;"; -static char *fsl = "\007"; - /** * collapse/uncollapse all threads * @param menu current menu @@ -518,77 +508,6 @@ static int main_change_folder(struct Menu *menu, int op, char *buf, return 0; } -/** - * mutt_ts_capability - Check terminal capabilities - * - * terminal status capability check. terminfo must have been initialized. - */ -bool mutt_ts_capability(void) -{ - const char *term = mutt_str_getenv("TERM"); - char *tcaps = NULL; -#ifdef HAVE_USE_EXTENDED_NAMES - int tcapi; -#endif - char **termp = NULL; - char *known[] = { - "color-xterm", "cygwin", "eterm", "kterm", "nxterm", - "putty", "rxvt", "screen", "xterm", NULL, - }; - - /* If tsl is set, then terminfo says that status lines work. */ - tcaps = tigetstr("tsl"); - if (tcaps && tcaps != (char *) -1 && *tcaps) - { - /* update the static defns of tsl/fsl from terminfo */ - tsl = tcaps; - - tcaps = tigetstr("fsl"); - if (tcaps && tcaps != (char *) -1 && *tcaps) - fsl = tcaps; - - return true; - } - -/* If XT (boolean) is set, then this terminal supports the standard escape. */ -/* Beware: tigetflag returns -1 if XT is invalid or not a boolean. */ -#ifdef HAVE_USE_EXTENDED_NAMES - use_extended_names(true); - tcapi = tigetflag("XT"); - if (tcapi == 1) - return true; -#endif /* HAVE_USE_EXTENDED_NAMES */ - - /* Check term types that are known to support the standard escape without - * necessarily asserting it in terminfo. */ - for (termp = known; termp; termp++) - { - if (term && *termp && (mutt_str_strncasecmp(term, *termp, strlen(*termp)) != 0)) - return true; - } - - /* not supported */ - return false; -} - -void mutt_ts_status(char *str) -{ - /* If empty, do not set. To clear, use a single space. */ - if (str == NULL || *str == '\0') - return; - fprintf(stderr, "%s%s%s", tsl, str, fsl); -} - -void mutt_ts_icon(char *str) -{ - /* If empty, do not set. To clear, use a single space. */ - if (str == NULL || *str == '\0') - return; - - /* icon setting is not supported in terminfo, so hardcode the escape - yuck */ - fprintf(stderr, "\033]1;%s\007", str); -} - /** * index_make_entry - Format a menu item for the index list * @param[out] buf Buffer in which to save string @@ -883,7 +802,7 @@ static void index_menu_redraw(struct Menu *menu) mutt_draw_statusline(MuttStatusWindow->cols, buf, sizeof(buf)); NORMAL_COLOR; menu->redraw &= ~REDRAW_STATUS; - if (TsEnabled && TSSupported) + if (TsEnabled && TsSupported) { menu_status_line(buf, sizeof(buf), menu, NONULL(TSStatusFormat)); mutt_ts_status(buf); diff --git a/globals.h b/globals.h index 293bc8764..941d8f4d4 100644 --- a/globals.h +++ b/globals.h @@ -46,7 +46,6 @@ WHERE char *ShortHostname; WHERE struct ListHead Muttrc INITVAL(STAILQ_HEAD_INITIALIZER(Muttrc)); -WHERE short TSSupported; WHERE char *Username; WHERE char *CurrentFolder; diff --git a/main.c b/main.c index c33a86796..83b0cab0a 100644 --- a/main.c +++ b/main.c @@ -53,6 +53,7 @@ #include "ncrypt/ncrypt.h" #include "options.h" #include "protos.h" +#include "terminal.h" #include "url.h" #include "version.h" #ifdef ENABLE_NLS @@ -524,7 +525,7 @@ int main(int argc, char *argv[], char *envp[]) goto main_curses; // TEST08: can't test -- fake term? /* check whether terminal status is supported (must follow curses init) */ - TSSupported = mutt_ts_capability(); + TsSupported = mutt_ts_capability(); } /* set defaults and read init files */ diff --git a/mutt_menu.h b/mutt_menu.h index a83a1bd83..fa72d49bb 100644 --- a/mutt_menu.h +++ b/mutt_menu.h @@ -119,9 +119,6 @@ void menu_redraw_sidebar(struct Menu *menu); #endif void menu_redraw_status(struct Menu *menu); void menu_status_line(char *buf, size_t buflen, struct Menu *menu, const char *p); -bool mutt_ts_capability(void); -void mutt_ts_status(char *str); -void mutt_ts_icon(char *str); void menu_top_page(struct Menu *menu); void mutt_menu_current_redraw(void); diff --git a/pager.c b/pager.c index 531fae58a..e819dec97 100644 --- a/pager.c +++ b/pager.c @@ -53,6 +53,7 @@ #include "options.h" #include "protos.h" #include "sort.h" +#include "terminal.h" #ifdef USE_SIDEBAR #include "sidebar.h" #endif @@ -2017,7 +2018,7 @@ static void pager_menu_redraw(struct Menu *pager_menu) mutt_draw_statusline(rd->pager_status_window->cols, bn, sizeof(bn)); } NORMAL_COLOR; - if (TsEnabled && TSSupported && rd->index) + if (TsEnabled && TsSupported && rd->index) { menu_status_line(buffer, sizeof(buffer), rd->index, NONULL(TSStatusFormat)); mutt_ts_status(buffer); diff --git a/terminal.c b/terminal.c new file mode 100644 index 000000000..7e18d51b8 --- /dev/null +++ b/terminal.c @@ -0,0 +1,119 @@ +/** + * @file + * Set the terminal title/icon + * + * @authors + * Copyright (C) 2018 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#include "config.h" +#include +#include +#include +#include "mutt/mutt.h" +#include "mutt_curses.h" +#ifdef HAVE_NCURSESW_NCURSES_H +#include +#elif defined(HAVE_NCURSES_NCURSES_H) +#include +#elif !defined(USE_SLANG_CURSES) +#include +#endif + +bool TsSupported; /**< Terminal Setting is supported */ + +/* de facto standard escapes for tsl/fsl */ +static char *tsl = "\033]0;"; +static char *fsl = "\007"; + +/** + * mutt_ts_capability - Check terminal capabilities + * @retval true Terminal is capable of having its title/icon set + * + * @note This must happen after the terminfo has been initialised. + */ +bool mutt_ts_capability(void) +{ + char *known[] = { + "color-xterm", "cygwin", "eterm", "kterm", "nxterm", + "putty", "rxvt", "screen", "xterm", NULL, + }; + + /* If tsl is set, then terminfo says that status lines work. */ + char *tcaps = tigetstr("tsl"); + if (tcaps && tcaps != (char *) -1 && *tcaps) + { + /* update the static definitiions of tsl/fsl from terminfo */ + tsl = tcaps; + + tcaps = tigetstr("fsl"); + if (tcaps && (tcaps != (char *) -1) && *tcaps) + fsl = tcaps; + + return true; + } + +#ifdef HAVE_USE_EXTENDED_NAMES + /* If XT (boolean) is set, then this terminal supports the standard escape. */ + /* Beware: tigetflag returns -1 if XT is invalid or not a boolean. */ + use_extended_names(true); + int tcapi = tigetflag("XT"); + if (tcapi == 1) + return true; +#endif + + /* Check term types that are known to support the standard escape without + * necessarily asserting it in terminfo. */ + const char *term = mutt_str_getenv("TERM"); + for (char **termp = known; termp; termp++) + { + if (term && *termp && (mutt_str_strncasecmp(term, *termp, strlen(*termp)) != 0)) + return true; + } + + /* not supported */ + return false; +} + +/** + * mutt_ts_status - Set the text of the terminal title bar + * @param str Text to set + * + * @note To clear the text, set the title to a single space. + */ +void mutt_ts_status(char *str) +{ + if (!str || (*str == '\0')) + return; + + fprintf(stderr, "%s%s%s", tsl, str, fsl); +} + +/** + * mutt_ts_icon - Set the icon in the terminal title bar + * @param str Icon string + * + * @note To clear the icon, set it to a single space. + */ +void mutt_ts_icon(char *str) +{ + if (!str || (*str == '\0')) + return; + + /* icon setting is not supported in terminfo, so hardcode the escape */ + fprintf(stderr, "\033]1;%s\007", str); +} diff --git a/terminal.h b/terminal.h new file mode 100644 index 000000000..7b9b58381 --- /dev/null +++ b/terminal.h @@ -0,0 +1,34 @@ +/** + * @file + * Set the terminal title/icon + * + * @authors + * Copyright (C) 2018 Richard Russon + * + * @copyright + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 2 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + */ + +#ifndef _MUTT_TERMINAL_H +#define _MUTT_TERMINAL_H + +#include + +extern bool TsSupported; + +bool mutt_ts_capability(void); +void mutt_ts_status(char *str); +void mutt_ts_icon(char *str); + +#endif /* _MUTT_TERMINAL_H */