From patchwork Mon Jan 26 07:29:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eli Zaretskii X-Patchwork-Id: 4817 Received: (qmail 22108 invoked by alias); 26 Jan 2015 07:29:50 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 22082 invoked by uid 89); 26 Jan 2015 07:29:48 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.6 required=5.0 tests=AWL, RCVD_IN_DNSWL_NONE, SPF_SOFTFAIL autolearn=no version=3.3.2 X-HELO: mtaout21.012.net.il Received: from mtaout21.012.net.il (HELO mtaout21.012.net.il) (80.179.55.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Jan 2015 07:29:45 +0000 Received: from conversion-daemon.a-mtaout21.012.net.il by a-mtaout21.012.net.il (HyperSendmail v2007.08) id <0NIR00000WR35R00@a-mtaout21.012.net.il> for gdb-patches@sourceware.org; Mon, 26 Jan 2015 09:29:42 +0200 (IST) Received: from HOME-C4E4A596F7 ([87.69.4.28]) by a-mtaout21.012.net.il (HyperSendmail v2007.08) with ESMTPA id <0NIR00NDOWTH19B0@a-mtaout21.012.net.il>; Mon, 26 Jan 2015 09:29:42 +0200 (IST) Date: Mon, 26 Jan 2015 09:29:28 +0200 From: Eli Zaretskii Subject: Re: [PATCH] TUI: Expand TABs into spaces In-reply-to: To: Doug Evans Cc: gdb-patches@sourceware.org Reply-to: Eli Zaretskii Message-id: <834mreq9t3.fsf@gnu.org> References: <83k3149k5b.fsf@gnu.org> <837fwn2cvc.fsf@gnu.org> <83a918s6k5.fsf@gnu.org> X-IsSubscribed: yes > From: Doug Evans > Cc: gdb-patches@sourceware.org > Date: Sat, 24 Jan 2015 13:26:47 -0800 > > For tui_register_format it used to be such a straightforward function, > it's a shame to grow it by 2x for bitfiddly tab handling. > > Let's add a helper routine that takes one string and returns > another with tabs expanded, and put this helper routine in, say tui-io.c > (this routine isn't tui-regs specific), and then call that routine from > tui_register_format. I did that below, but since tui_register_format is its only user, keeping that function in tui-regs.c would have allowed us to make it static. Wouldn't that be slightly better? Thanks for the review; updated patch attached. 2015-01-26 Eli Zaretskii * tui/tui-io.c (tui_expand_tabs): New function. (tui_puts, tui_redisplay_readline): Expand TABs into the appropriate number of spaces. * tui/tui-regs.c: Include tui-io.h. (tui_register_format): Call tui_expand_tabs to expand TABs into the appropriate number of spaces. * tui/tui-io.h: Add prototype for tui_expand_tabs. --- tui/tui-io.c~0 Wed Oct 29 21:45:50 2014 +++ tui/tui-io.c Mon Jan 26 09:10:34 2015 @@ -179,7 +179,20 @@ tui_puts (const char *string) else if (tui_skip_line != 1) { tui_skip_line = -1; - waddch (w, c); + /* Expand TABs, since ncurses on MS-Windows doesn't. */ + if (c == '\t') + { + int line, col; + + getyx (w, line, col); + do + { + waddch (w, ' '); + col++; + } while ((col % 8) != 0); + } + else + waddch (w, c); } else if (c == '\n') tui_skip_line = -1; @@ -254,6 +269,16 @@ tui_redisplay_readline (void) waddch (w, '^'); waddch (w, CTRL_CHAR (c) ? UNCTRL (c) : '?'); } + else if (c == '\t') + { + /* Expand TABs, since ncurses on MS-Windows doesn't. */ + getyx (w, line, col); + do + { + waddch (w, ' '); + col++; + } while ((col % 8) != 0); + } else { waddch (w, c); @@ -700,6 +725,58 @@ tui_getc (FILE *fp) return ch; } +/* Utility function to expand TABs in a STRING into spaces. STRING + will be displayed starting at column COL. The returned expanded + string is malloc'ed. */ +char * +tui_expand_tabs (const char *string, int col) +{ + int n_adjust; + const char *s; + char *ret, *q; + + /* 1. How many additional characters do we need? */ + for (n_adjust = 0, s = string; s; ) + { + s = strpbrk (s, "\t"); + if (s) + { + col += (s - string) + n_adjust; + /* Adjustment for the next tab stop, minus one for the TAB + we replace with spaces. */ + n_adjust += 8 - (col % 8) - 1; + s++; + } + } + + /* Allocate the copy. */ + ret = q = xmalloc (strlen (string) + n_adjust + 1); + + /* 2. Copy the original string while replacing TABs with spaces. */ + for (s = string; s; ) + { + char *s1 = strpbrk (s, "\t"); + if (s1) + { + if (s1 > s) + { + strncpy (q, s, s1 - s); + q += s1 - s; + col += s1 - s; + } + do { + *q++ = ' '; + col++; + } while ((col % 8) != 0); + s1++; + } + else + strcpy (q, s); + s = s1; + } + + return ret; +} /* Cleanup when a resize has occured. Returns the character that must be processed. */ --- tui/tui-regs.c~0 Wed Oct 29 21:45:50 2014 +++ tui/tui-regs.c Mon Jan 26 09:24:23 2015 @@ -37,6 +37,7 @@ #include "tui/tui-wingeneral.h" #include "tui/tui-file.h" #include "tui/tui-regs.h" +#include "tui/tui-io.h" #include "reggroups.h" #include "valprint.h" @@ -694,7 +695,9 @@ tui_register_format (struct frame_info * if (s && s[1] == 0) *s = 0; - ret = xstrdup (p); + /* Expand tabs into spaces, since ncurses on MS-Windows doesn't. */ + ret = tui_expand_tabs (p, 0); + do_cleanups (cleanups); return ret; --- tui/tui-io.h~0 Wed Jun 11 19:34:41 2014 +++ tui/tui-io.h Mon Jan 26 09:11:42 2015 @@ -41,6 +41,9 @@ changed the edited text. */ extern void tui_redisplay_readline (void); +/* Expand TABs into spaces. */ +extern char *tui_expand_tabs (const char *, int); + extern struct ui_out *tui_out; extern struct ui_out *tui_old_uiout;