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