1 /* $OpenBSD: ex_visual.c,v 1.10 2016/01/06 22:28:52 millert Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1992, 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #include <sys/types.h> 15 #include <sys/queue.h> 16 #include <sys/time.h> 17 18 #include <bitstring.h> 19 #include <limits.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 #include <unistd.h> 24 25 #include "../common/common.h" 26 #include "../vi/vi.h" 27 28 /* 29 * ex_visual -- :[line] vi[sual] [^-.+] [window_size] [flags] 30 * Switch to visual mode. 31 * 32 * PUBLIC: int ex_visual(SCR *, EXCMD *); 33 */ 34 int 35 ex_visual(SCR *sp, EXCMD *cmdp) 36 { 37 SCR *tsp; 38 size_t len; 39 int pos; 40 char buf[256]; 41 42 /* If open option off, disallow visual command. */ 43 if (!O_ISSET(sp, O_OPEN)) { 44 msgq(sp, M_ERR, 45 "The visual command requires that the open option be set"); 46 return (1); 47 } 48 49 /* Move to the address. */ 50 sp->lno = cmdp->addr1.lno == 0 ? 1 : cmdp->addr1.lno; 51 52 /* 53 * Push a command based on the line position flags. If no 54 * flag specified, the line goes at the top of the screen. 55 */ 56 switch (FL_ISSET(cmdp->iflags, 57 E_C_CARAT | E_C_DASH | E_C_DOT | E_C_PLUS)) { 58 case E_C_CARAT: 59 pos = '^'; 60 break; 61 case E_C_DASH: 62 pos = '-'; 63 break; 64 case E_C_DOT: 65 pos = '.'; 66 break; 67 case E_C_PLUS: 68 pos = '+'; 69 break; 70 default: 71 sp->frp->lno = sp->lno; 72 sp->frp->cno = 0; 73 (void)nonblank(sp, sp->lno, &sp->cno); 74 F_SET(sp->frp, FR_CURSORSET); 75 goto nopush; 76 } 77 78 if (FL_ISSET(cmdp->iflags, E_C_COUNT)) 79 len = snprintf(buf, sizeof(buf), 80 "%luz%c%lu", (ulong)sp->lno, pos, cmdp->count); 81 else 82 len = snprintf(buf, sizeof(buf), "%luz%c", (ulong)sp->lno, pos); 83 if (len >= sizeof(buf)) 84 len = sizeof(buf) - 1; 85 (void)v_event_push(sp, NULL, buf, len, CH_NOMAP | CH_QUOTED); 86 87 /* 88 * !!! 89 * Historically, if no line address was specified, the [p#l] flags 90 * caused the cursor to be moved to the last line of the file, which 91 * was then positioned as described above. This seems useless, so 92 * I haven't implemented it. 93 */ 94 switch (FL_ISSET(cmdp->iflags, E_C_HASH | E_C_LIST | E_C_PRINT)) { 95 case E_C_HASH: 96 O_SET(sp, O_NUMBER); 97 break; 98 case E_C_LIST: 99 O_SET(sp, O_LIST); 100 break; 101 case E_C_PRINT: 102 break; 103 } 104 105 nopush: /* 106 * !!! 107 * You can call the visual part of the editor from within an ex 108 * global command. 109 * 110 * XXX 111 * Historically, undoing a visual session was a single undo command, 112 * i.e. you could undo all of the changes you made in visual mode. 113 * We don't get this right; I'm waiting for the new logging code to 114 * be available. 115 * 116 * It's explicit, don't have to wait for the user, unless there's 117 * already a reason to wait. 118 */ 119 if (!F_ISSET(sp, SC_SCR_EXWROTE)) 120 F_SET(sp, SC_EX_WAIT_NO); 121 122 if (F_ISSET(sp, SC_EX_GLOBAL)) { 123 /* 124 * When the vi screen(s) exit, we don't want to lose our hold 125 * on this screen or this file, otherwise we're going to fail 126 * fairly spectacularly. 127 */ 128 ++sp->refcnt; 129 ++sp->ep->refcnt; 130 131 /* 132 * Fake up a screen pointer -- vi doesn't get to change our 133 * underlying file, regardless. 134 */ 135 tsp = sp; 136 if (vi(&tsp)) 137 return (1); 138 139 /* 140 * !!! 141 * Historically, if the user exited the vi screen(s) using an 142 * ex quit command (e.g. :wq, :q) ex/vi exited, it was only if 143 * they exited vi using the Q command that ex continued. Some 144 * early versions of nvi continued in ex regardless, but users 145 * didn't like the semantic. 146 * 147 * Reset the screen. 148 */ 149 if (ex_init(sp)) 150 return (1); 151 152 /* Move out of the vi screen. */ 153 (void)ex_puts(sp, "\n"); 154 } else { 155 F_CLR(sp, SC_EX | SC_SCR_EX); 156 F_SET(sp, SC_VI); 157 } 158 return (0); 159 } 160