-
Notifications
You must be signed in to change notification settings - Fork 0
/
pt.vim
144 lines (122 loc) · 5.01 KB
/
pt.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
augroup PurenTonbo_encrypted
autocmd!
if $PTCIPHER_EXE == ""
let $PTCIPHER_EXE = "ptcipher"
endif
function! s:PurenTonboReadPre()
set cmdheight=3
set viminfo=
set noswapfile
set shell=/bin/sh
set bin
endfunction
function! s:WorksErrorsToBufferPurenTonboReadPost()
" using filename <afile> rather than stdin to tool due to unbuffered issues with python (bins and __main__) and Windows
" 0 and 1 both work so far under Linux
" experiment hard coded password
"silent 1,$!ptcipher -d -p password '<afile>'
"silent 0,$!ptcipher -d -p password '<afile>'
" ptcipher prompts for password
" on error bugfer fills with error text
"silent 1,$!ptcipher -d '<afile>'
" vim prompts for password, put into OS env PT_PASSWORD which ptcipher picks up automatically
if $PT_PASSWORD == ""
let $PT_PASSWORD = inputsecret("ptcipher Password: ")
endif
silent 1,$!$PTCIPHER_EXE -d '<afile>'
set nobin
set cmdheight&
set shell&
execute ":doautocmd BufReadPost ".expand("%:r")
redraw!
endfunction
function! s:PurenTonboReadPost()
" using filename <afile> rather than stdin to tool due to unbuffered issues with python (bins and __main__) and Windows
" 0 and 1 both work so far under Linux
" vim prompts for password, put into OS env PT_PASSWORD which ptcipher picks up automatically
" error message that requires dismisal displayed, then raw file loaded/displayed in buffer
if $PT_PASSWORD == ""
" TODO refactor into a function for password prompt
let $PT_PASSWORD = inputsecret("ptcipher Password: ")
endif
" let ptcipher determine cipher type from filename
let l:expr = "1,$!$PTCIPHER_EXE --silent --no-prompt --decrypt '<afile>'"
silent! execute l:expr
if v:shell_error
silent! 0,$y
silent! undo
echo "COULD NOT DECRYPT USING EXPRESSION: " . expr
echo "ERROR FROM Puren Tonbo:"
echo @"
echo "COULD NOT DECRYPT"
echo "Unsetting local OS env PT_PASSWORD"
let $PT_PASSWORD = ""
return
endif
set nobin
set cmdheight&
set shell&
execute ":doautocmd BufReadPost ".expand("%:r")
redraw!
endfunction
function! s:PurenTonboWritePre()
" Likely Linux/Unix only due to Windows buffered IO issues (and use of /bin/sh)
" did experiment with using <afile> and having ptcipher write directly
" that write/save works, but then get prompt from vim:
" WARNING: The file has been changed since reading it!!!
" Do you really want to write to it (y/n)?
" Say no, ptcipher had already written to file
" Saying yes, outputs the stdout/stderror from ptcipher into file, e.g.:
" Read in from stdin...DEBUG tmp_out_filename '/..../puren_tonbo/write.aeszip20230311_170505e0grbho9'
set cmdheight=3
set viminfo=
set noswapfile
set shell=/bin/sh
set bin
set nofixendofline
" noeol has no effect, BUT vim 7.4+ directive nofixendofline does on vim 8.1.2269
"set noeol
" 0 and 1 both work so far under Linux
" vim prompts for password, put into OS env PT_PASSWORD which ptcipher picks up automatically
" error message that requires dismisal displayed, then raw file loaded/displayed in buffer
" if using stdout for encryption, ptcipher needs to be told which scheme/file type to use
" use the file extension as the format/encryption cipher
let l:file_extension = expand("<afile>:e")
if $PT_PASSWORD == ""
let $PT_PASSWORD = inputsecret("ptcipher Password: ")
endif
" TODO prompt twice to avoid incorrect passwords? and/or only prompt if PT_PASSWORD is not set
" WARNING! end up with "Read in from stdin..." as prefix in file without --silent flag, even though that was sent to stderr
"let l:expr = "1,$!ptcipher --cipher " . l:file_extension . " -e -o -"
"let l:expr = "1,$!ptcipher --silent --cipher " . l:file_extension . " -e -o -"
"let l:expr = "1,$!ptcipher --silent --cipher " . l:file_extension . " --encrypt -o -"
let l:expr = "1,$!$PTCIPHER_EXE --silent --no-prompt --cipher " . l:file_extension . " --encrypt -o -"
silent! execute l:expr
if v:shell_error
silent! 0,$y
silent! undo
echo "COULD NOT ENCRYPT USING EXPRESSION: " . expr
echo "ERROR FROM Puren Tonbo:"
echo @"
echo "COULD NOT ENCRYPT"
return
endif
set nobin
set cmdheight&
set shell&
execute ":doautocmd BufReadPost ".expand("%:r")
redraw!
endfunction
function! s:PurenTonboWritePost()
silent! undo
set nobin
set shell&
set cmdheight&
redraw!
endfunction
" potentially filter aes zip to *aeszip
autocmd BufReadPre,FileReadPre *.chi,*.chs,*.age,*.asc,*.gpg,*zip call s:PurenTonboReadPre()
autocmd BufReadPost,FileReadPost *.chi,*.chs,*.age,*.asc,*.gpg,*zip call s:PurenTonboReadPost()
autocmd BufWritePre,FileWritePre *.chi,*.chs,*.age,*.asc,*.gpg,*zip call s:PurenTonboWritePre()
autocmd BufWritePost,FileWritePost *.chi,*.chs,*.age,*.asc,*.gpg,*zip call s:PurenTonboWritePost()
augroup END