1#!/bin/sh 2# 3# $OpenBSD: vi.sh,v 1.6 2017/08/22 20:14:57 anton Exp $ 4# 5# Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org> 6# Copyright (c) 2017 Anton Lindqvist <anton@openbsd.org> 7# 8# Permission to use, copy, modify, and distribute this software for any 9# purpose with or without fee is hereby granted, provided that the above 10# copyright notice and this permission notice appear in all copies. 11# 12# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 20. "${1:-.}/subr.sh" 21 22KSH=$2 23 24EDITOR= 25ENV= 26HISTFILE= 27MAIL= 28MALLOC_OPTIONS=S 29PS1=' # ' 30VISUAL=vi 31export EDITOR ENV HISTFILE MAIL MALLOC_OPTIONS PS1 VISUAL 32 33# ^H, ^?: Erase. 34testseq "ab\bc" " # ab\b \bc" 35testseq "ab\0177c" " # ab\b \bc" 36 37# ^J, ^M: End of line. 38testseq "echo a\nb" " # echo a\r\r\na\r\n # b" 39testseq "echo a\rb" " # echo a\r\r\na\r\n # b" 40testseq "echo a\0033\nb" " # echo a\b\r\r\na\r\n # b" 41testseq "echo a\0033\rb" " # echo a\b\r\r\na\r\n # b" 42 43# ^U: Kill. 44testseq "ab\0033ic\0025d" " # ab\bcb\b\b\bb \b\b\bdb\b" 45 46# ^V: Literal next. 47testseq "a\0026\0033b" " # a^\b^[b" 48 49# ^W: Word erase. 50testseq "one two\0027rep" " # one two\b\b\b \b\b\brep" 51 52# A: Append at end of line. 53# 0: Move to column 0. 54testseq "one\00330A two" " # one\b\b\bone two" 55testseq "one\003302A two\0033" " # one\b\b\bone two two\b" 56 57# a: Append. 58# .: Redo. 59testseq "ab\00330axy" " # ab\b\baxb\byb\b" 60testseq "ab\003302axy\0033" " # ab\b\baxb\byb\bxyb\b\b" 61testseq "ab\00330axy\0033." " # ab\b\baxb\byb\b\byxyb\b\b" 62 63# B: Move back big word. 64testseq "one 2.0\0033BD" " # one 2.0\b\b\b \b\b\b\b" 65 66# b: Move back word. 67# C: Change to end of line. 68# D: Delete to end of line. 69testseq "one ab.cd\0033bDa.\00332bD" \ 70 " # one ab.cd\b\b \b\b\b..\b\b\b\b \b\b\b\b\b" 71testseq "one two\0033bCrep" " # one two\b\b\b \b\b\brep" 72 73# c: Change region. 74testseq "one two\0033cbrep" " # one two\b\b\bo \b\b\bro\beo\bpo\b" 75testseq "one two\00332chx" " # one two\b\b\bo \b\b\bxo\b" 76 77# d: Delete region. 78testseq "one two\0033db" " # one two\b\b\bo \b\b\b" 79testseq "one two xy\00332db" " # one two xy\b\b\b\b\b\by \b\b\b\b\b\b" 80 81# E: Move to end of big word. 82testseq "1.00 two\00330ED" " # 1.00 two\b\r # 1.0 \b\b\b\b\b\b" 83 84# e: Move to end of word. 85testseq "onex two\00330eD" " # onex two\b\r # one \b\b\b\b\b\b" 86 87# F: Find character backward. 88# ;: Repeat last search. 89# ,: Repeat last search in opposite direction. 90testseq "hello\00332FlD" " # hello\b\b\b \b\b\b\b" 91testseq "hello\0033Flix\0033;ix" " # hello\b\bxlo\b\b\b\bxlxlo\b\b\b\b" 92testseq "hello\00332Flix\00332,ix" " # hello\b\b\bxllo\b\b\b\bxlxlo\b\b" 93 94# f: Find character forward. 95testseq "hello\003302flD" " # hello\b\b\b\b\bhel \b\b\b" 96 97# h, ^H: Move left. 98# i: Insert. 99testseq "hello\00332hix" " # hello\b\b\bxllo\b\b\b" 100testseq "hello\00332\b2ix\0033" " # hello\b\b\bxllo\b\b\bxllo\b\b\b\b" 101 102# I: Insert before first non-blank. 103# ^: Move to the first non-whitespace character. 104testseq " ab\0033Ixy" " # ab\b\bxab\b\byab\b\b" 105testseq " ab\00332Ixy\0033" " # ab\b\bxab\b\byab\b\bxyab\b\b\b" 106testseq " ab\0033^ixy" " # ab\b\bxab\b\byab\b\b" 107 108# L: Undefined command (beep). 109testseq "ab\0033Lx" " # ab\b\a \b\b" 110 111# l, space: Move right. 112# ~: Change case. 113testseq "abc\003302l~" " # abc\b\b\babC\b" 114testseq "abc\00330 rx" " # abc\b\b\bax\b" 115 116# P: Paste at current position. 117testseq "abcde\0033hDhP" " # abcde\b\b \b\b\b\bdebc\b\b" 118testseq "abcde\0033hDh2P" " # abcde\b\b \b\b\b\bdedebc\b\b\b" 119 120# p: Paste after current position. 121testseq "abcd\0033hDhp" " # abcd\b\b \b\b\b\bacdb\b\b" 122testseq "abcd\0033hDh2p" " # abcd\b\b \b\b\b\bacdcdb\b\b" 123 124# R: Replace. 125testseq "abcd\00332h2Rx\0033" " # abcd\b\b\bxx\b" 126testseq "abcdef\00334h2Rxy\0033" " # abcdef\b\b\b\b\bxyxy\b" 127 128# r: Replace character. 129testseq "abcd\00332h2rxiy" " # abcd\b\b\bxx\byxd\b\b" 130testseq "\0303\0266\0033ro" " # \0303\0266\bo \b\b" 131testseq "\0342\0202\0254\0033ro" " # \0342\0202\0254\bo \b\b\b" 132testseq "\0303\0266\00332ro" " # \0303\0266\b\a" 133 134# S: Substitute whole line. 135testseq "oldst\0033Snew" " # oldst\b\b\b\b\b \r # new" 136testseq "oldstr\033Snew" " # oldstr\b\r # \r # new" 137 138# s: Substitute. 139testseq "abcd\00332h2sx" " # abcd\b\b\bd \b\b\bxd\b" 140testseq "\0303\0266\0033s" " # \0303\0266\b \b\b" 141 142# T: Move backward after character. 143testseq "helloo\0033TlD" " # helloo\b\b \b\b\b" 144testseq "hello\00332TlD" " # hello\b\b \b\b\b" 145 146# t: Move forward before character. 147testseq "abc\00330tcD" " # abc\b\b\ba \b\b\b" 148testseq "hello\003302tlD" " # hello\b\b\b\b\bhe \b\b\b\b" 149 150# U: Undo all changes. 151testseq "test\0033U" " # test\b\b\b\b \b\b\b\b" 152 153# u: Undo. 154testseq "test\0033hxu" " # test\b\bt \b\bst\b\b" 155 156# W: Move forward big word. 157testseq "1.0 two\00330WD" " # 1.0 two\b\r # 1.0 \b\b\b\b" 158 159# w: Move forward word. 160testseq "ab cd ef\003302wD" " # ab cd ef\b\r # ab cd \b\b\b" 161 162# X: Delete previous character. 163testseq "abcd\00332X" " # abcd\b\b\bd \b\b\b" 164 165# x: Delete character. 166# |: Move to column. 167testseq "abcd\00332|2x" " # abcd\b\b\bd \b\b\b" 168testseq "\0303\0266a\0033xx" " # \0303\0266a\b \b\b \b\b" 169 170# Y: Yank to end of line. 171testseq "abcd\0033hYp" " # abcd\b\bccdd\b\b" 172 173# y: Yank region. 174# $: Move to the last character. 175testseq "abcd\00332h2ylp" " # abcd\b\b\bbbccd\b\b\b" 176testseq "abcd\00332h2yl\$p" " # abcd\b\b\bbcdbc\b" 177 178# %: Find match. 179testseq "(x)\0033%lrc" " # (x)\b\b\b(c\b" 180testseq "(x)\00330%hrc" " # (x)\b\b\b(x\bc\b" 181 182# ^L, ^R: Redraw. 183testseq "test\0033\0014" " # test\b\r\r\n # test\b" 184testseq "test\0033h\0022" " # test\b\b\r\r\n # test\b\b" 185