Move initialization of variables to the places where they are used.

This commit is contained in:
Markus Braun
2011-07-05 12:46:39 +02:00
parent 7afa048df7
commit c3f898827a

View File

@@ -229,6 +229,7 @@ function s:GPGDecrypt()
let b:GPGOptions+=["symmetric"] let b:GPGOptions+=["symmetric"]
" find the used cipher algorithm
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+=["cipher-algo " . cipher] let b:GPGOptions+=["cipher-algo " . cipher]
@@ -246,6 +247,7 @@ function s:GPGDecrypt()
let b:GPGOptions+=["encrypt"] let b:GPGOptions+=["encrypt"]
" find the used public keys
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)
let start=start + strlen("gpg: public key is ") let start=start + strlen("gpg: public key is ")
@@ -258,7 +260,7 @@ function s:GPGDecrypt()
else else
let 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
end end
let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start) let start=match(output, "gpg: public key is [[:xdigit:]]\\{8}", start)
@@ -291,7 +293,7 @@ function s:GPGDecrypt()
if (v:shell_error) " message could not be decrypted if (v:shell_error) " message could not be decrypted
silent u silent u
echohl GPGError echohl GPGError
let asd=input("Message could not be decrypted! (Press ENTER)") let blackhole=input("Message could not be decrypted! (Press ENTER)")
echohl None echohl None
bwipeout bwipeout
set nobin set nobin
@@ -339,11 +341,7 @@ function s:GPGEncrypt()
return return
endif endif
let options="" " initialize GPGOptions if not happened before
let recipients=""
let field=0
" built list of options
if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0) if (!exists("b:GPGOptions") || len(b:GPGOptions) == 0)
let b:GPGOptions=[] let b:GPGOptions=[]
if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1) if (exists("g:GPGPreferSymmetric") && g:GPGPreferSymmetric == 1)
@@ -356,52 +354,35 @@ function s:GPGEncrypt()
endif endif
call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions)) call s:GPGDebug(1, "no options set, so using default options: " . string(b:GPGOptions))
endif endif
" built list of options
let options=""
for option in b:GPGOptions for option in b:GPGOptions
let options=options . " --" . option . " " let options=options . " --" . option . " "
endfor endfor
let GPGUnknownRecipients=[] " check recipientslist for unknown recipients again
for recipient in b:GPGUnknownRecipients
" Check recipientslist for unknown recipients again echohl GPGWarning
for cur_recipient in b:GPGRecipients echom "The recipient \"" . recipient . "\" is not in your public keyring!"
" only do this if the line is not empty echohl None
if (strlen(cur_recipient) > 0)
let gpgid=s:GPGNameToID(cur_recipient)
if (strlen(gpgid) <= 0)
let GPGUnknownRecipients+=[cur_recipient]
echohl GPGWarning
echom "The recipient " . cur_recipient . " is not in your public keyring!"
echohl None
endif
endif
endfor endfor
" check if there are unknown recipients and warn " check if there are unknown recipients and warn
if(len(GPGUnknownRecipients) > 0) if(len(b: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: " . join(GPGUnknownRecipients, " ")) call s:GPGDebug(1, "unknown recipients are: " . join(b:GPGUnknownRecipients, " "))
" Remove unknown recipients from recipientslist
let unknown_recipients=join(GPGUnknownRecipients, " ")
let index=0
while index < len(b:GPGRecipients)
if match(unknown_recipients, b:GPGRecipients[index])
echohl GPGWarning
echom "Removing ". b:GPGRecipients[index] ." from recipientlist!\n"
echohl None
call remove(b:GPGRecipients, index)
endif
endwhile
" Let user know whats happend and copy known_recipients back to buffer " Let user know whats happend and copy known_recipients back to buffer
let dummy=input("Press ENTER to quit") let dummy=input("Press ENTER to quit")
endif endif
" built list of recipients " built list of recipients
let recipients=""
if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0) if (exists("b:GPGRecipients") && len(b:GPGRecipients) > 0)
call s:GPGDebug(1, "recipients are: " . join(b:GPGRecipients, " ")) call s:GPGDebug(1, "recipients are: " . join(b:GPGRecipients, " "))
for gpgid in b:GPGRecipients for gpgid in b:GPGRecipients
@@ -427,7 +408,7 @@ function s:GPGEncrypt()
if (v:shell_error) " message could not be encrypted if (v:shell_error) " message could not be encrypted
silent u silent u
echohl GPGError echohl GPGError
let asd=input("Message could not be encrypted! File might be empty! (Press ENTER)") let blackhole=input("Message could not be encrypted! File might be empty! (Press ENTER)")
echohl None echohl None
bwipeout bwipeout
return return
@@ -440,7 +421,7 @@ endfunction
" undo changes don by encrypt, after writing " undo changes don by encrypt, after writing
" "
function s:GPGEncryptPost() function s:GPGEncryptPost()
" guard for unencrypted files
if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0) if (exists("b:GPGEncrypted") && b:GPGEncrypted == 0)
return return
endif endif
@@ -486,7 +467,7 @@ function s:GPGViewRecipients()
echo name echo name
endfor endfor
" put the unknown recipients in the scratch buffer " echo the unknown recipients
echohl GPGWarning echohl GPGWarning
for name in b:GPGUnknownRecipients for name in b:GPGUnknownRecipients
let name="!" . name let name="!" . name
@@ -555,7 +536,7 @@ function s:GPGEditRecipients()
setlocal nonumber setlocal nonumber
" so we know for which other buffer this edit buffer is " so we know for which other buffer this edit buffer is
let b:corresponding_to=buffername let b:GPGCorrespondingTo=buffername
" put some comments to the scratch buffer " put some comments to the scratch buffer
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
@@ -566,30 +547,28 @@ function s:GPGEditRecipients()
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
" 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:GPGCorrespondingTo, "GPGRecipients")
if (type(recipients) != type([])) if (type(recipients) != type([]))
unlet recipients unlet recipients
let recipients=[] let recipients=[]
endif endif
for name in recipients for name in recipients
let name=s:GPGIDToName(name) let name=s:GPGIDToName(name)
silent put =name silent put =name
endfor endfor
" 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:GPGCorrespondingTo, "GPGUnknownRecipients")
if (type(unknownRecipients) != type([])) if (type(unknownrecipients) != type([]))
unlet unknownRecipients unlet unknownrecipients
let unknownRecipients=[] let unknownrecipients=[]
endif endif
let syntaxPattern="\\(nonexistingwordinthisbuffer" let syntaxPattern="\\(nonexxistinwordinthisbuffer"
for name in unknownRecipients for name in unknownrecipients
let name="!" . name let name="!" . name
let syntaxPattern=syntaxPattern . "\\|" . name let syntaxPattern=syntaxPattern . "\\|" . name
silent put =name silent put =name
endfor endfor
let syntaxPattern=syntaxPattern . "\\)" let syntaxPattern=syntaxPattern . "\\)"
" define highlight " define highlight
@@ -630,10 +609,6 @@ function s:GPGFinishRecipientsBuffer()
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w" execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
endif endif
" clear GPGRecipients and GPGUnknownRecipients
let GPGRecipients=[]
let GPGUnknownRecipients=[]
" delete the autocommand " delete the autocommand
autocmd! * <buffer> autocmd! * <buffer>
@@ -641,6 +616,8 @@ function s:GPGFinishRecipientsBuffer()
let recipient=getline(currentline) let recipient=getline(currentline)
" get the recipients from the scratch buffer " get the recipients from the scratch buffer
let recipients=[]
let unknownrecipients=[]
while (currentline <= line("$")) while (currentline <= line("$"))
" delete all spaces at beginning and end of the line " delete all spaces at beginning and end of the line
" also delete a '!' at the beginning of the line " also delete a '!' at the beginning of the line
@@ -652,14 +629,14 @@ function 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)
if (match(GPGRecipients, gpgid) < 0) if (match(recipients, gpgid) < 0)
let GPGRecipients+=[gpgid] let recipients+=[gpgid]
endif endif
else else
if (match(GPGUnknownRecipients, recipient) < 0) if (match(unknownrecipients, recipient) < 0)
let GPGUnknownRecipients+=[recipient] let unknownrecipients+=[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
endif endif
end end
@@ -671,13 +648,13 @@ function s:GPGFinishRecipientsBuffer()
" write back the new recipient list to the corresponding buffer and mark it " write back the new recipient list to the corresponding buffer and mark it
" as modified. Buffer is now for sure a encrypted buffer. " as modified. Buffer is now for sure a encrypted buffer.
call setbufvar(b:corresponding_to, "GPGRecipients", GPGRecipients) call setbufvar(b:GPGCorrespondingTo, "GPGRecipients", recipients)
call setbufvar(b:corresponding_to, "GPGUnknownRecipients", GPGUnknownRecipients) call setbufvar(b:GPGCorrespondingTo, "GPGUnknownRecipients", unknownrecipients)
call setbufvar(b:corresponding_to, "&mod", 1) call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
call setbufvar(b:corresponding_to, "GPGEncrypted", 1) call setbufvar(b:GPGCorrespondingTo, "GPGEncrypted", 1)
" check if there is any known recipient " check if there is any known recipient
if (len(GPGRecipients) == 0) if (len(recipients) == 0)
echohl GPGError echohl GPGError
echom 'There are no known recipients!' echom 'There are no known recipients!'
echohl None echohl None
@@ -760,7 +737,7 @@ function s:GPGEditOptions()
setlocal nonumber setlocal nonumber
" so we know for which other buffer this edit buffer is " so we know for which other buffer this edit buffer is
let b:corresponding_to=buffername let b:GPGCorrespondingTo=buffername
" put some comments to the scratch buffer " put some comments to the scratch buffer
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
@@ -774,7 +751,7 @@ function s:GPGEditOptions()
silent put ='GPG: ----------------------------------------------------------------------' silent put ='GPG: ----------------------------------------------------------------------'
" 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:GPGCorrespondingTo, "GPGOptions")
for option in options for option in options
silent put =option silent put =option
@@ -813,9 +790,9 @@ function s:GPGFinishOptionsBuffer()
execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w" execute 'silent! ' . bufwinnr(expand("<afile>")) . "wincmd w"
endif endif
" clear GPGOptions and GPGUnknownOptions " clear options and unknownOptions
let GPGOptions=[] let options=[]
let GPGUnknownOptions=[] let unknownOptions=[]
" delete the autocommand " delete the autocommand
autocmd! * <buffer> autocmd! * <buffer>
@@ -832,8 +809,8 @@ function s:GPGFinishOptionsBuffer()
let option=substitute(option, "^GPG:.*$", "", "") let option=substitute(option, "^GPG:.*$", "", "")
" only do this if the line is not empty " only do this if the line is not empty
if (strlen(option) > 0 && match(GPGOptions, option) < 0) if (strlen(option) > 0 && match(options, option) < 0)
let GPGOptions+=[option] let options+=[option]
endif endif
let currentline=currentline+1 let currentline=currentline+1
@@ -842,8 +819,8 @@ function s:GPGFinishOptionsBuffer()
" write back the new option list to the corresponding buffer and mark it " write back the new option list to the corresponding buffer and mark it
" as modified " as modified
call setbufvar(b:corresponding_to, "GPGOptions", GPGOptions) call setbufvar(b:GPGCorrespondingTo, "GPGOptions", options)
call setbufvar(b:corresponding_to, "&mod", 1) call setbufvar(b:GPGCorrespondingTo, "&mod", 1)
" reset modified flag " reset modified flag
set nomodified set nomodified
@@ -869,34 +846,34 @@ function s:GPGNameToID(name)
let lines=split(output, "\n") let lines=split(output, "\n")
" parse the output of gpg " parse the output of gpg
let pub_seen=0 let pubseen=0
let uid_seen=0 let uidseen=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"
for line in lines for line in lines
let fields=split(line, ":") let fields=split(line, ":")
" search for the next uid " search for the next uid
if (pub_seen == 1) if (pubseen == 1)
if (fields[0] == "uid") if (fields[0] == "uid")
if (uid_seen == 0) if (uidseen == 0)
let choices=choices . counter . ": " . fields[9] . "\n" let choices=choices . counter . ": " . fields[9] . "\n"
let counter=counter+1 let counter=counter+1
let uid_seen=1 let uidseen=1
else else
let choices=choices . " " . fields[9] . "\n" let choices=choices . " " . fields[9] . "\n"
endif endif
else else
let uid_seen=0 let uidseen=0
let pub_seen=0 let pubseen=0
endif endif
endif endif
" search for the next pub " search for the next pub
if (pub_seen == 0) if (pubseen == 0)
if (fields[0] == "pub") if (fields[0] == "pub")
let gpgids+=[fields[4]] let gpgids+=[fields[4]]
let pub_seen=1 let pubseen=1
endif endif
endif endif
@@ -937,17 +914,17 @@ function s:GPGIDToName(identity)
let lines=split(output, "\n") let lines=split(output, "\n")
" parse the output of gpg " parse the output of gpg
let pub_seen=0 let pubseen=0
let uid="" let uid=""
for line in lines for line in lines
let fields=split(line, ":") let fields=split(line, ":")
if (pub_seen == 0) " search for the next pub if (pubseen == 0) " search for the next pub
if (fields[0] == "pub") if (fields[0] == "pub")
let pub_seen=1 let pubseen=1
endif endif
else " search for the next uid else " search for the next uid
if (fields[0] == "uid") if (fields[0] == "uid")
let pub_seen=0 let pubseen=0
let uid=fields[9] let uid=fields[9]
break break
endif endif