Conversion from self implemented "list" to vim builtin lists.

This commit is contained in:
Markus Braun
2008-07-28 21:01:41 +00:00
parent 921d99fd88
commit 96f3901aa0

View File

@@ -66,9 +66,15 @@
" - Giel van Schijndel for patch to get GPG_TTY dynamically. " - Giel van Schijndel for patch to get GPG_TTY dynamically.
" "
" Section: Plugin header {{{1 " Section: Plugin header {{{1
if v:version < 700
echohl ErrorMsg | echo 'plugin gnupg.vim requires Vim version >= 7' | echohl None
finish
endif
if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)")) if (exists("g:loaded_gnupg") || &cp || exists("#BufReadPre#*.\(gpg\|asc\|pgp\)"))
finish finish
endi endi
let g:loaded_gnupg = "$Revision$" let g:loaded_gnupg = "$Revision$"
" Section: Autocmd setup {{{1 " Section: Autocmd setup {{{1
@@ -202,9 +208,9 @@ fun s:GPGDecrypt()
" clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions " clear GPGEncrypted, GPGRecipients, GPGUnknownRecipients and GPGOptions
let b:GPGEncrypted=0 let b:GPGEncrypted=0
let b:GPGRecipients="" let b:GPGRecipients=[]
let b:GPGUnknownRecipients="" let b:GPGUnknownRecipients=[]
let b:GPGOptions="" let b:GPGOptions=[]
" find the recipients of the file " find the recipients of the file
let &shellredir=s:shellredir let &shellredir=s:shellredir
@@ -221,11 +227,11 @@ fun s:GPGDecrypt()
let b:GPGEncrypted=1 let b:GPGEncrypted=1
call s:GPGDebug(1, "this file is symmetric encrypted") call s:GPGDebug(1, "this file is symmetric encrypted")
let b:GPGOptions=b:GPGOptions . "symmetric:" let b:GPGOptions+=["symmetric"]
let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "") let cipher=substitute(output, ".*gpg: \\([^ ]\\+\\) encrypted data.*", "\\1", "")
if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0) if (match(s:GPGCipher, "\\<" . cipher . "\\>") >= 0)
let b:GPGOptions=b:GPGOptions . "cipher-algo " . cipher . ":" let b:GPGOptions+=["cipher-algo " . cipher]
call s:GPGDebug(1, "cipher-algo is " . cipher) call s:GPGDebug(1, "cipher-algo is " . cipher)
else else
echohl GPGWarning echohl GPGWarning
@@ -238,7 +244,7 @@ fun s:GPGDecrypt()
let b:GPGEncrypted=1 let b:GPGEncrypted=1
call s:GPGDebug(1, "this file is asymmetric encrypted") call s:GPGDebug(1, "this file is asymmetric encrypted")
let b:GPGOptions=b:GPGOptions . "encrypt:" let b:GPGOptions+=["encrypt"]
let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}") let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}")
while (start >= 0) while (start >= 0)
@@ -247,10 +253,10 @@ fun s:GPGDecrypt()
call s:GPGDebug(1, "recipient is " . recipient) call s:GPGDebug(1, "recipient is " . recipient)
let name=s:GPGNameToID(recipient) let name=s:GPGNameToID(recipient)
if (strlen(name) > 0) if (strlen(name) > 0)
let b:GPGRecipients=b:GPGRecipients . name . ":" let b:GPGRecipients+=[name]
call s:GPGDebug(1, "name of recipient is " . name) call s:GPGDebug(1, "name of recipient is " . name)
else else
let b:GPGUnknownRecipients=b:GPGUnknownRecipients . recipient . ":" let b:GPGUnknownRecipients+=[recipient]
echohl GPGWarning echohl GPGWarning
echom "The recipient " . recipient . " is not in your public keyring!" echom "The recipient " . recipient . " is not in your public keyring!"
echohl None echohl None
@@ -271,7 +277,7 @@ fun s:GPGDecrypt()
" check if the message is armored " check if the message is armored
if (match(output, "gpg: armor header") >= 0) if (match(output, "gpg: armor header") >= 0)
call s:GPGDebug(1, "this file is armored") call s:GPGDebug(1, "this file is armored")
let b:GPGOptions=b:GPGOptions . "armor:" let b:GPGOptions+=["armor"]
endi endi
" finally decrypt the buffer content " finally decrypt the buffer content
@@ -309,10 +315,8 @@ endf
" "
fun s:GPGEncrypt() fun s:GPGEncrypt()
" save window view " save window view
if v:version >= 700 let s:GPGWindowView = winsaveview()
let s:GPGWindowView = winsaveview() call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView))
call s:GPGDebug(2, "saved window view " . string(s:GPGWindowView))
endi
" store encoding and switch to a safe one " store encoding and switch to a safe one
if &fileencoding != &encoding if &fileencoding != &encoding
@@ -340,89 +344,71 @@ fun s:GPGEncrypt()
let field=0 let field=0
" built list of options " built list of options
if (!exists("b:GPGOptions") || strlen(b:GPGOptions) == 0) if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0)
let b:GPGOptions=[]
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
let b:GPGOptions="symmetric:" let b:GPGOptions+=["symmetric"]
else else
let b:GPGOptions="encrypt:" let b:GPGOptions+=["encrypt"]
endi endi
if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1) if (exists("g:GPGPreferArmor") && g:GPGPreferArmor == 1)
let b:GPGOptions=b:GPGOptions . "armor:" let b:GPGOptions+=["armor"]
endi endi
call s:GPGDebug(1, "no options set, so using default options: " . b:GPGOptions) call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions))
endi endi
let field=0 for option in b:GPGOptions
let option=s:GetField(b:GPGOptions, ":", field)
while (strlen(option))
let options=options . " --" . option . " " let options=options . " --" . option . " "
let field=field+1 endfor
let option=s:GetField(b:GPGOptions, ":", field)
endw
let GPGUnknownRecipients="" let GPGUnknownRecipients=[]
let field=0
let cur_recipient="-"
" Check recipientslist for unknown recipients again " Check recipientslist for unknown recipients again
while(strlen(cur_recipient)) for cur_recipient in b:GPGRecipients
let cur_recipient=s:GetField(b:GPGRecipients, ":", field)
let field=field+1
" only do this if the line is not empty " only do this if the line is not empty
if (strlen(cur_recipient) > 0) if (strlen(cur_recipient) > 0)
let gpgid=s:GPGNameToID(cur_recipient) let gpgid=s:GPGNameToID(cur_recipient)
if (strlen(gpgid) <= 0) if (strlen(gpgid) <= 0)
let GPGUnknownRecipients=GPGUnknownRecipients . cur_recipient . ":" let GPGUnknownRecipients+=[cur_recipient]
echohl GPGWarning echohl GPGWarning
echom "The recipient " . cur_recipient . " is not in your public keyring!" echom "The recipient " . cur_recipient . " is not in your public keyring!"
echohl None echohl None
endi endi
endi endi
endw endfor
" check if there are unknown recipients and warn " check if there are unknown recipients and warn
if(strlen(GPGUnknownRecipients) > 0) if(len(GPGUnknownRecipients) > 0)
echohl GPGWarning echohl GPGWarning
echom "There are unknown recipients!!" echom "There are unknown recipients!!"
echom "Please use GPGEditRecipients to correct!!" echom "Please use GPGEditRecipients to correct!!"
echo echo
echohl None echohl None
call s:GPGDebug(1, "unknown recipients are: " . GPGUnknownRecipients) call s:GPGDebug(1, "unknown recipients are: " . join(GPGUnknownRecipients, " "))
" Remove unknown recipients from recipientslist " Remove unknown recipients from recipientslist
let unknown_recipients_field=0 let unknown_recipients=join(GPGUnknownRecipients, " ")
let cur_unknown_recipient="-" let index=0
let known_recipients=b:GPGRecipients while index < len(b:GPGRecipients)
while(strlen(cur_unknown_recipient)) if match(unknown_recipients, b:GPGRecipients[index])
let cur_unknown_recipient=s:GetField(GPGUnknownRecipients, ":", unknown_recipients_field) echohl GPGWarning
echom "Removing ". b:GPGRecipients[index] ." from recipientlist!\n"
let match_result=match(known_recipients, cur_unknown_recipient.":") echohl None
if(match_result > 0 && strlen(cur_unknown_recipient) > 0) call remove(b:GPGRecipients, index)
echohl GPGWarning endi
echom "Removing ". cur_unknown_recipient ." from recipientlist!\n"
echohl None
let Known_Recipients=substitute(known_recipients, cur_unknown_recipient .":", "", "g")
endi
let unknown_recipients_field=unknown_recipients_field+1
endw endw
" Let user know whats happend and copy known_recipients back to buffer
let dummy=input("Press ENTER to quit") " Let user know whats happend and copy known_recipients back to buffer
let b:GPGRecipients=known_recipients let dummy=input("Press ENTER to quit")
endi endi
" built list of recipients " built list of recipients
if (exists("b:GPGRecipients") && strlen(b:GPGRecipients) > 0) if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0)
call s:GPGDebug(1, "recipients are: " . b:GPGRecipients) call s:GPGDebug(1, "recipients are: " . join(b:GPGRecipients, " "))
let field=0 for gpgid in b:GPGRecipients
let gpgid=s:GetField(b:GPGRecipients, ":", field)
while (strlen(gpgid))
let recipients=recipients . " -r " . gpgid let recipients=recipients . " -r " . gpgid
let field=field+1 endfor
let gpgid=s:GetField(b:GPGRecipients, ":", field)
endw
else else
if (match(b:GPGOptions, "encrypt:") >= 0) if (match(join(b:GPGOptions, " "), "encrypt") >= 0)
echohl GPGError echohl GPGError
echom "There are no recipients!!" echom "There are no recipients!!"
echom "Please use GPGEditRecipients to correct!!" echom "Please use GPGEditRecipients to correct!!"
@@ -472,10 +458,8 @@ fun s:GPGEncryptPost()
endi endi
" restore window view " restore window view
if v:version >= 700 call winrestview(s:GPGWindowView)
call winrestview(s:GPGWindowView) call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView))
call s:GPGDebug(2, "restored window view" . string(s:GPGWindowView))
endi
" refresh screen " refresh screen
redraw! redraw!
@@ -497,31 +481,21 @@ fun s:GPGViewRecipients()
if (exists("b:GPGRecipients")) if (exists("b:GPGRecipients"))
echo 'This file has following recipients (Unknown recipients have a prepended "!"):' echo 'This file has following recipients (Unknown recipients have a prepended "!"):'
" echo the recipients " echo the recipients
let field=0 for name in b:GPGRecipients
let name=s:GetField(b:GPGRecipients, ":", field)
while (strlen(name) > 0)
let name=s:GPGIDToName(name) let name=s:GPGIDToName(name)
echo name echo name
endfor
let field=field+1
let name=s:GetField(b:GPGRecipients, ":", field)
endw
" put the unknown recipients in the scratch buffer " put the unknown recipients in the scratch buffer
let field=0
echohl GPGWarning echohl GPGWarning
let name=s:GetField(b:GPGUnknownRecipients, ":", field) for name in b:GPGUnknownRecipients
while (strlen(name) > 0)
let name="!" . name let name="!" . name
echo name echo name
endfor
let field=field+1
let name=s:GetField(b:GPGUnknownRecipients, ":", field)
endw
echohl None echohl None
" check if there is any known recipient " check if there is any known recipient
if (strlen(s:GetField(b:GPGRecipients, ":", 0)) == 0) if (len(b:GPGRecipients) == 0)
echohl GPGError echohl GPGError
echom 'There are no known recipients!' echom 'There are no known recipients!'
echohl None echohl None
@@ -593,31 +567,20 @@ fun s:GPGEditRecipients()
" put the recipients in the scratch buffer " put the recipients in the scratch buffer
let recipients=getbufvar(b:corresponding_to, "GPGRecipients") let recipients=getbufvar(b:corresponding_to, "GPGRecipients")
let field=0
let name=s:GetField(recipients, ":", field) for name in recipients
while (strlen(name) > 0)
let name=s:GPGIDToName(name) let name=s:GPGIDToName(name)
silent put =name silent put =name
endfor
let field=field+1
let name=s:GetField(recipients, ":", field)
endw
" put the unknown recipients in the scratch buffer " put the unknown recipients in the scratch buffer
let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients") let unknownRecipients=getbufvar(b:corresponding_to, "GPGUnknownRecipients")
let field=0
let syntaxPattern="\\(nonexistingwordinthisbuffer" let syntaxPattern="\\(nonexistingwordinthisbuffer"
for name in unknownRecipients
let name=s:GetField(unknownRecipients, ":", field)
while (strlen(name) > 0)
let name="!" . name let name="!" . name
let syntaxPattern=syntaxPattern . "\\|" . name let syntaxPattern=syntaxPattern . "\\|" . name
silent put =name silent put =name
endfor
let field=field+1
let name=s:GetField(unknownRecipients, ":", field)
endw
let syntaxPattern=syntaxPattern . "\\)" let syntaxPattern=syntaxPattern . "\\)"
@@ -660,8 +623,8 @@ fun s:GPGFinishRecipientsBuffer()
endi endi
" clear GPGRecipients and GPGUnknownRecipients " clear GPGRecipients and GPGUnknownRecipients
let GPGRecipients="" let GPGRecipients=[]
let GPGUnknownRecipients="" let GPGUnknownRecipients=[]
" delete the autocommand " delete the autocommand
autocmd! * <buffer> autocmd! * <buffer>
@@ -681,9 +644,9 @@ fun s:GPGFinishRecipientsBuffer()
if (strlen(recipient) > 0) if (strlen(recipient) > 0)
let gpgid=s:GPGNameToID(recipient) let gpgid=s:GPGNameToID(recipient)
if (strlen(gpgid) > 0) if (strlen(gpgid) > 0)
let GPGRecipients=GPGRecipients . gpgid . ":" let GPGRecipients+=[gpgid]
else else
let GPGUnknownRecipients=GPGUnknownRecipients . recipient . ":" let GPGUnknownRecipients+=[recipient]
echohl GPGWarning echohl GPGWarning
echom "The recipient " . recipient . " is not in your public keyring!" echom "The recipient " . recipient . " is not in your public keyring!"
echohl None echohl None
@@ -702,7 +665,7 @@ fun s:GPGFinishRecipientsBuffer()
call setbufvar(b:corresponding_to, "GPGEncrypted", 1) call setbufvar(b:corresponding_to, "GPGEncrypted", 1)
" check if there is any known recipient " check if there is any known recipient
if (strlen(s:GetField(GPGRecipients, ":", 0)) == 0) if (len(GPGRecipients) == 0)
echohl GPGError echohl GPGError
echom 'There are no known recipients!' echom 'There are no known recipients!'
echohl None echohl None
@@ -728,14 +691,9 @@ fun s:GPGViewOptions()
if (exists("b:GPGOptions")) if (exists("b:GPGOptions"))
echo 'This file has following options:' echo 'This file has following options:'
" echo the options " echo the options
let field=0 for option in b:GPGOptions
let option=s:GetField(b:GPGOptions, ":", field)
while (strlen(option) > 0)
echo option echo option
endfor
let field=field+1
let option=s:GetField(b:GPGOptions, ":", field)
endw
endi endi
endf endf
@@ -805,15 +763,10 @@ fun s:GPGEditOptions()
" put the options in the scratch buffer " put the options in the scratch buffer
let options=getbufvar(b:corresponding_to, "GPGOptions") let options=getbufvar(b:corresponding_to, "GPGOptions")
let field=0
let option=s:GetField(options, ":", field) for option in options
while (strlen(option) > 0)
silent put =option silent put =option
endfor
let field=field+1
let option=s:GetField(options, ":", field)
endw
" delete the empty first line " delete the empty first line
silent normal! 1Gdd silent normal! 1Gdd
@@ -849,8 +802,8 @@ fun s:GPGFinishOptionsBuffer()
endi endi
" clear GPGOptions and GPGUnknownOptions " clear GPGOptions and GPGUnknownOptions
let GPGOptions="" let GPGOptions=[]
let GPGUnknownOptions="" let GPGUnknownOptions=[]
" delete the autocommand " delete the autocommand
autocmd! * <buffer> autocmd! * <buffer>
@@ -868,7 +821,7 @@ fun s:GPGFinishOptionsBuffer()
" only do this if the line is not empty " only do this if the line is not empty
if (strlen(option) > 0) if (strlen(option) > 0)
let GPGOptions=GPGOptions . option . ":" let GPGOptions+=[option]
endi endi
let currentline=currentline+1 let currentline=currentline+1
@@ -901,25 +854,25 @@ fun s:GPGNameToID(name)
if &encoding != "utf-8" if &encoding != "utf-8"
let output=iconv(output, "utf-8", &encoding) let output=iconv(output, "utf-8", &encoding)
endi endi
let lines=split(output, "\n")
" parse the output of gpg " parse the output of gpg
let pub_seen=0 let pub_seen=0
let uid_seen=0 let uid_seen=0
let line=0
let counter=0 let counter=0
let gpgids="" let gpgids=[]
let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n" let choices="The name \"" . a:name . "\" is ambiguous. Please select the correct key:\n"
let linecontent=s:GetField(output, "\n", line) for line in lines
while (strlen(linecontent)) let fields=split(line, ":")
" search for the next uid " search for the next uid
if (pub_seen == 1) if (pub_seen == 1)
if (s:GetField(linecontent, ":", 0) == "uid") if (fields[0] == "uid")
if (uid_seen == 0) if (uid_seen == 0)
let choices=choices . counter . ": " . s:GetField(linecontent, ":", 9) . "\n" let choices=choices . counter . ": " . fields[9] . "\n"
let counter=counter+1 let counter=counter+1
let uid_seen=1 let uid_seen=1
else else
let choices=choices . " " . s:GetField(linecontent, ":", 9) . "\n" let choices=choices . " " . fields[9] . "\n"
endi endi
else else
let uid_seen=0 let uid_seen=0
@@ -929,15 +882,13 @@ fun s:GPGNameToID(name)
" search for the next pub " search for the next pub
if (pub_seen == 0) if (pub_seen == 0)
if (s:GetField(linecontent, ":", 0) == "pub") if (fields[0] == "pub")
let gpgids=gpgids . s:GetField(linecontent, ":", 4) . ":" let gpgids+=[fields[4]]
let pub_seen=1 let pub_seen=1
endi endi
endi endi
let line=line+1 endfor
let linecontent=s:GetField(output, "\n", line)
endw
" counter > 1 means we have more than one results " counter > 1 means we have more than one results
let answer=0 let answer=0
@@ -949,7 +900,7 @@ fun s:GPGNameToID(name)
endw endw
endi endi
return s:GetField(gpgids, ":", answer) return get(gpgids, answer, "")
endf endf
" Function: s:GPGIDToName(identity) {{{2 " Function: s:GPGIDToName(identity) {{{2
@@ -971,63 +922,29 @@ fun s:GPGIDToName(identity)
if &encoding != "utf-8" if &encoding != "utf-8"
let output=iconv(output, "utf-8", &encoding) let output=iconv(output, "utf-8", &encoding)
endi endi
let lines=split(output, "\n")
" parse the output of gpg " parse the output of gpg
let pub_seen=0 let pub_seen=0
let finish=0 let uid=""
let line=0 for line in lines
let linecontent=s:GetField(output, "\n", line) let fields=split(line, ":")
while (strlen(linecontent) && !finish)
if (pub_seen == 0) " search for the next pub if (pub_seen == 0) " search for the next pub
if (s:GetField(linecontent, ":", 0) == "pub") if (fields[0] == "pub")
let pub_seen=1 let pub_seen=1
endi endi
else " search for the next uid else " search for the next uid
if (s:GetField(linecontent, ":", 0) == "uid") if (fields[0] == "uid")
let pub_seen=0 let pub_seen=0
let finish=1 let uid=fields[9]
let uid=s:GetField(linecontent, ":", 9) break
endi endi
endi endi
endfor
let line=line+1
let linecontent=s:GetField(output, "\n", line)
endw
return uid return uid
endf endf
" Function: s:GetField(line, separator, field) {{{2
"
" find field of 'separator' separated string, counting starts with 0
" Returns: content of the field, if field doesn't exist it returns an empty
" string
fun s:GetField(line, separator, field)
let counter=a:field
let separatorLength=strlen(a:separator)
let start=0
let end=match(a:line, a:separator)
if (end < 0)
let end=strlen(a:line)
endi
" search for requested field
while (start < strlen(a:line) && counter > 0)
let counter=counter-separatorLength
let start=end+separatorLength
let end=match(a:line, a:separator, start)
if (end < 0)
let end=strlen(a:line)
endi
endw
if (start < strlen(a:line))
return strpart(a:line, start, end-start)
else
return ""
endi
endf
" Function: s:GPGDebug(level, text) {{{2 " Function: s:GPGDebug(level, text) {{{2
" "
" output debug message, if this message has high enough importance " output debug message, if this message has high enough importance