The previous diff had issues with duplicate colums. e.g 4 drwx------ 3 root wheel 512 Aug 9 02:36 root
using strstr() would give location of the first instance of `root'. The following is simpler, and calculates the offset of the 9th column, and also avoids using strtok_r() which requires a lot of care to get done right. Now, I use strspn(). Au usual complaints welcomed ;-) Index: dired.c =================================================================== RCS file: /cvs/src/usr.bin/mg/dired.c,v retrieving revision 1.48 diff -u -p -r1.48 dired.c --- dired.c 23 Jan 2011 00:45:03 -0000 1.48 +++ dired.c 13 Aug 2011 16:33:11 -0000 @@ -36,6 +36,10 @@ static int d_rename(int, int); static int d_shell_command(int, int); static int d_create_directory(int, int); static int d_makename(struct line *, char *, size_t); +static int d_forwpage(int, int); +static int d_backpage(int, int); +static int d_forwline(int, int); +static int d_backline(int, int); static void reaper(int); extern struct keymap_s helpmap, cXmap, metamap; @@ -57,15 +61,15 @@ static PF dirednul[] = { static PF diredcl[] = { reposition, /* ^L */ d_findfile, /* ^M */ - forwline, /* ^N */ + d_forwline, /* ^N */ rescan, /* ^O */ - backline, /* ^P */ + d_backline, /* ^P */ rescan, /* ^Q */ backisearch, /* ^R */ forwisearch, /* ^S */ rescan, /* ^T */ universal_argument, /* ^U */ - forwpage, /* ^V */ + d_forwpage, /* ^V */ rescan, /* ^W */ NULL /* ^X */ }; @@ -77,7 +81,7 @@ static PF diredcz[] = { rescan, /* ^] */ rescan, /* ^^ */ rescan, /* ^_ */ - forwline, /* SP */ + d_forwline, /* SP */ d_shell_command, /* ! */ rescan, /* " */ rescan, /* # */ @@ -99,9 +103,9 @@ static PF diredc[] = { }; static PF diredn[] = { - forwline, /* n */ + d_forwline, /* n */ d_ffotherwindow, /* o */ - backline, /* p */ + d_backline, /* p */ rescan, /* q */ d_rename, /* r */ rescan, /* s */ @@ -116,13 +120,32 @@ static PF direddl[] = { d_undelbak /* del */ }; +static PF diredbp[] = { + d_backpage /* v */ +}; + +static PF dirednull[] = { + NULL +}; + #ifndef DIRED_XMAPS #define NDIRED_XMAPS 0 /* number of extra map sections */ #endif /* DIRED_XMAPS */ -static struct KEYMAPE (6 + NDIRED_XMAPS + IMAPEXT) diredmap = { - 6 + NDIRED_XMAPS, - 6 + NDIRED_XMAPS + IMAPEXT, +static struct KEYMAPE (1 + IMAPEXT) d_backpagemap = { + 1, + 1 + IMAPEXT, + rescan, + { + { + 'v', 'v', diredbp, NULL + } + } +}; + +static struct KEYMAPE (7 + NDIRED_XMAPS + IMAPEXT) diredmap = { + 7 + NDIRED_XMAPS, + 7 + NDIRED_XMAPS + IMAPEXT, rescan, { #ifndef NO_HELP @@ -138,6 +161,10 @@ static struct KEYMAPE (6 + NDIRED_XMAPS CCHR('L'), CCHR('X'), diredcl, (KEYMAP *) & cXmap }, { + CCHR('['), CCHR('['), dirednull, (KEYMAP *) & + d_backpagemap + }, + { CCHR('Z'), '+', diredcz, (KEYMAP *) & metamap }, { @@ -592,6 +619,118 @@ d_makename(struct line *lp, char *fn, si return ((lgetc(lp, 2) == 'd') ? TRUE : FALSE); } +static int +d_forwpage(int f, int n) +{ + char *track, *anchor = NULL; + int col = 0; + + forwpage(f | FFRAND, n); + track = curwp->w_dotp->l_text; + while (track != NULL && track - curwp->w_dotp->l_text <= + strlen(curwp->w_dotp->l_text)) { + if (strspn(track, " ") > 0) { + track += strspn(track, " "); + col++; + if (col == 9) { + anchor = track; + break; + } + } + else + track++; + } + if (anchor == NULL) + curwp->w_doto = 0; + else + curwp->w_doto = anchor - curwp->w_dotp->l_text; + return TRUE; +} + +static int +d_backpage (int f, int n) +{ + char *track, *anchor = NULL; + int col = 0; + + backpage(f | FFRAND, n); + track = curwp->w_dotp->l_text; + while (track != NULL && track - curwp->w_dotp->l_text <= + strlen(curwp->w_dotp->l_text)) { + if (strspn(track, " ") > 0) { + track += strspn(track, " "); + col++; + if (col == 9) { + anchor = track; + break; + } + } + else + track++; + } + if (anchor == NULL) + curwp->w_doto = 0; + else + curwp->w_doto = anchor - curwp->w_dotp->l_text; + return TRUE; +} + +static int +d_forwline (int f, int n) +{ + char *track, *anchor = NULL; + int col = 0; + + forwline(f | FFRAND, n); + track = curwp->w_dotp->l_text; + while (track != NULL && track - curwp->w_dotp->l_text <= + strlen(curwp->w_dotp->l_text)) { + if (strspn(track, " ") > 0) { + track += strspn(track, " "); + col++; + if (col == 9) { + anchor = track; + break; + } + } + else + track++; + } + if (anchor == NULL) + curwp->w_doto = 0; + else + curwp->w_doto = anchor - curwp->w_dotp->l_text; + return TRUE; +} + +static int +d_backline (int f, int n) +{ + char *track, *anchor = NULL; + int col = 0; + + backline(f | FFRAND, n); + track = curwp->w_dotp->l_text; + while (track != NULL && track - curwp->w_dotp->l_text <= + strlen(curwp->w_dotp->l_text)) { + if (strspn(track, " ") > 0) { + track += strspn(track, " "); + col++; + if (col == 9) { + anchor = track; + break; + } + } + else + track++; + } + if (anchor == NULL) + curwp->w_doto = 0; + else + curwp->w_doto = anchor - curwp->w_dotp->l_text; + return TRUE; +} + /* * XXX dname needs to have enough place to store an additional '/'. */ @@ -600,10 +739,12 @@ dired_(char *dname) { struct buffer *bp; FILE *dirpipe; - char line[256]; - int len, ret, counter, warp; + char line[256], *anchor, *track; + int len, ret, counter, warp, col; counter = 0; warp = 0; + col = 0; + anchor = NULL; if ((fopen(dname,"r")) == NULL) { if (errno == EACCES) @@ -649,9 +790,24 @@ dired_(char *dname) if ((strrchr(line,' ')) != NULL) { if (strcmp((strrchr(line,' '))," ..") == 0) warp = counter - 1; - } - if ((strrchr(line,' ')) != NULL) - bp->b_doto = strrchr(line,' ') - line + 1; + } + track = line; + while (track != NULL && track - line <= strlen(line)) { + if (strspn(track, " ") > 0) { + track += strspn(track, " "); + col++; + if (col == 9) { + anchor = track; + break; + } + } + else + track++; + } + if (anchor == NULL) + bp->b_doto = 0; + else + bp->b_doto = anchor - line; if (pclose(dirpipe) == -1) { ewprintf("Problem closing pipe to ls : %s", strerror(errno));