1 /* $NetBSD: ex_join.c,v 1.5 2017/11/21 07:48:07 rin Exp $ */ 2 /*- 3 * Copyright (c) 1992, 1993, 1994 4 * The Regents of the University of California. All rights reserved. 5 * Copyright (c) 1992, 1993, 1994, 1995, 1996 6 * Keith Bostic. All rights reserved. 7 * 8 * See the LICENSE file for redistribution information. 9 */ 10 11 #include "config.h" 12 13 #include <sys/cdefs.h> 14 #if 0 15 #ifndef lint 16 static const char sccsid[] = "Id: ex_join.c,v 10.17 2004/03/16 14:14:04 skimo Exp (Berkeley) Date: 2004/03/16 14:14:04 "; 17 #endif /* not lint */ 18 #else 19 __RCSID("$NetBSD: ex_join.c,v 1.5 2017/11/21 07:48:07 rin Exp $"); 20 #endif 21 22 #include <sys/types.h> 23 #include <sys/queue.h> 24 25 #include <bitstring.h> 26 #include <ctype.h> 27 #include <limits.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include <string.h> 31 32 #include "../common/common.h" 33 34 /* 35 * ex_join -- :[line [,line]] j[oin][!] [count] [flags] 36 * Join lines. 37 * 38 * PUBLIC: int ex_join __P((SCR *, EXCMD *)); 39 */ 40 int 41 ex_join(SCR *sp, EXCMD *cmdp) 42 { 43 db_recno_t from, to; 44 size_t blen, clen, len, tlen; 45 int extra, first; 46 ARG_CHAR_T echar = 0; 47 CHAR_T *p, *bp, *tbp = NULL; 48 49 NEEDFILE(sp, cmdp); 50 51 from = cmdp->addr1.lno; 52 to = cmdp->addr2.lno; 53 54 /* Check for no lines to join. */ 55 if (!db_exist(sp, from + 1)) { 56 msgq(sp, M_ERR, "131|No following lines to join"); 57 return (1); 58 } 59 60 GET_SPACE_RETW(sp, bp, blen, 256); 61 62 /* 63 * The count for the join command was off-by-one, 64 * historically, to other counts for other commands. 65 */ 66 if (F_ISSET(cmdp, E_ADDR_DEF) || cmdp->addrcnt == 1) 67 ++cmdp->addr2.lno; 68 69 clen = tlen = 0; 70 for (first = 1, 71 from = cmdp->addr1.lno, to = cmdp->addr2.lno; from <= to; ++from) { 72 /* 73 * Get next line. Historic versions of vi allowed "10J" while 74 * less than 10 lines from the end-of-file, so we do too. 75 */ 76 if (db_get(sp, from, 0, &p, &len)) { 77 cmdp->addr2.lno = from - 1; 78 break; 79 } 80 81 /* Empty lines just go away. */ 82 if (len == 0) 83 continue; 84 85 /* 86 * Get more space if necessary. Note, tlen isn't the length 87 * of the new line, it's roughly the amount of space needed. 88 * tbp - bp is the length of the new line. 89 */ 90 tlen += len + 2; 91 ADD_SPACE_RETW(sp, bp, blen, tlen); 92 tbp = bp + clen; 93 94 /* 95 * Historic practice: 96 * 97 * If force specified, join without modification. 98 * If the current line ends with whitespace, strip leading 99 * whitespace from the joined line. 100 * If the next line starts with a ), do nothing. 101 * If the current line ends with ., insert two spaces. 102 * Else, insert one space. 103 * 104 * One change -- add ? and ! to the list of characters for 105 * which we insert two spaces. I expect that POSIX 1003.2 106 * will require this as well. 107 * 108 * Echar is the last character in the last line joined. 109 */ 110 extra = 0; 111 if (!first && !FL_ISSET(cmdp->iflags, E_C_FORCE)) { 112 /* 113 * Here we implement behavior just based on nvi-m17n. 114 * last char first char behavior 115 * --- --- --- 116 * multi-width multi-width nothing ins'ed 117 * multi-width single-width 1 spc ins'ed 118 * single-width multi-width 1 spc ins'ed 119 * single-width single-width original 120 */ 121 if (ISMULTIWIDTH(sp, echar)) { 122 if (ISMULTIWIDTH(sp, p[0])) { 123 ; /* nothing */ 124 } else { 125 *tbp++ = ' '; 126 ++clen; 127 for (; len && ISBLANK((UCHAR_T)*p); 128 --len, ++p); 129 } 130 } else if (ISMULTIWIDTH(sp, p[0])) { 131 *tbp++ = ' '; 132 ++clen; 133 } else if (ISBLANK(echar)) 134 for (; len && ISBLANK((UCHAR_T)*p); --len, ++p); 135 else if (p[0] != ')') { 136 if (STRCHR(L(".?!"), echar)) { 137 *tbp++ = ' '; 138 ++clen; 139 extra = 1; 140 } 141 *tbp++ = ' '; 142 ++clen; 143 for (; len && ISBLANK((UCHAR_T)*p); --len, ++p); 144 } 145 } 146 147 if (len != 0) { 148 MEMCPYW(tbp, p, len); 149 tbp += len; 150 clen += len; 151 echar = p[len - 1]; 152 } else 153 echar = ' '; 154 155 /* 156 * Historic practice for vi was to put the cursor at the first 157 * inserted whitespace character, if there was one, or the 158 * first character of the joined line, if there wasn't, or the 159 * last character of the line if joined to an empty line. If 160 * a count was specified, the cursor was moved as described 161 * for the first line joined, ignoring subsequent lines. If 162 * the join was a ':' command, the cursor was placed at the 163 * first non-blank character of the line unless the cursor was 164 * "attracted" to the end of line when the command was executed 165 * in which case it moved to the new end of line. There are 166 * probably several more special cases, but frankly, my dear, 167 * I don't give a damn. This implementation puts the cursor 168 * on the first inserted whitespace character, the first 169 * character of the joined line, or the last character of the 170 * line regardless. Note, if the cursor isn't on the joined 171 * line (possible with : commands), it is reset to the starting 172 * line. 173 */ 174 if (first) { 175 sp->cno = (tbp - bp) - (1 + extra); 176 first = 0; 177 } else 178 sp->cno = (tbp - bp) - len - (1 + extra); 179 } 180 sp->lno = cmdp->addr1.lno; 181 182 /* Delete the joined lines. */ 183 for (from = cmdp->addr1.lno, to = cmdp->addr2.lno; to > from; --to) 184 if (db_delete(sp, to)) 185 goto err; 186 187 /* If the original line changed, reset it. */ 188 if (!first && db_set(sp, from, bp, tbp - bp)) { 189 err: FREE_SPACEW(sp, bp, blen); 190 return (1); 191 } 192 FREE_SPACEW(sp, bp, blen); 193 194 sp->rptlines[L_JOINED] += (cmdp->addr2.lno - cmdp->addr1.lno) + 1; 195 return (0); 196 } 197