Commit Graph

94 Commits

Author SHA1 Message Date
James McCoy
7d0a149169
Remove GPGCleanup function
If 'autowrite' is set, an encrypted buffer is modified, and ":q!"/"ZQ"
is executed, GPGCleanup's "only!" would cause the buffer to be
abandoned.  Due to 'autowrite' and the VimLeave autocmd not using
"nested", abandoning the buffer causes Vim to write the buffer, which is
currently unencrypted, overwriting the encrypted file with plain text.

Since the user can more reliably clear their own console/terminal scroll
back than we can, simply remove this functionality.  It's safer than
trying to unwind the twisty passages of Vim's autocmds, especially those
of the *WriteCmd variety.

Closes jamessan/vim-gnupg#77
Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-02-14 22:11:52 -05:00
James McCoy
fa18ce0c9e
Use non-fnameescape()d filenames in logs
Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-12-18 01:14:28 -05:00
James McCoy
d279115bcd
GPGDecrypt: Handle (un)encrypted files consistently
In the refactor to use the *Cmd events, an unencrypted file was simply
:read into the buffer.  This incorrectly caused an extra blank line to
be added to the top of the buffer.

The handling of encrypted files properly handles this, as well as
triggering BufRead(Pre,Post) events.

Instead of returning early for unencrypted files, go through the same
steps as for encrypted files (minus the encryption specific steps), thus
preserving the content of the original file.
2016-12-17 22:05:17 -05:00
James McCoy
be052843ef
Add s:unencrypted function to consolidate "unencrypted buffer" check
Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-12-17 21:14:35 -05:00
James McCoy
45fdaf52ed
GPGCleanup: Use "only!" to force hiding modified buffers
In dda14abb3c, GPGCleanup added 'set
buffhidden=hide' to try avoiding error messages when modified buffers
are being abandoned.  However, that only works if the buffer is the last
visible buffer.

If there are windows displaying multiple buffers, then errors can still
be emitted.  Using "only!" ensures all the other windows can be closed.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-12-17 20:51:26 -05:00
James McCoy
dda14abb3c
GPGCleanup: Allow the buffer to be hidden on exit
When Vim has decided it can exit, GPGCleanup forces a blank buffer to
take over the screen so contents aren't visible in scrollback.  However,
if the buffer is modified and 'hidden' isn't set then the command will
fail.

Force the buffer to be hidden, so this is avoided.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-12-14 23:05:40 -05:00
James McCoy
e7be112873
GPGEdit{Recipients,Options}: Check for presence of b:GPGCorrespondingTo
Since the user can control which file patterns are handled by the plugin
(via g:GPGFilePattern), the regular expression checking the buffer name
for GPGEdit{Recipients,Options} may not match.  A simpler and more
definitive check is to see whether b:GPGCorrespondingTo is set to
determine if the buffer is an existing recipients/options buffer.

Closes jamessan/vim-gnupg#72
Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-12-04 14:07:36 -05:00
James McCoy
454090fe40
GPGEncrypt: Remove (file)encoding dance around file encryption
Changing &encoding when &fileencoding != &encoding causes two problems.

First, it's completely unsafe to change &encoding after vim is running,
since there may be characters that can't be translated from one encoding
to another, which can make vim crash.

Second, this actually loses information about the buffer's encoding.
When the encrypted file is opened later, it won't be in the same
encoding as before it was saved.  Changing user's data on them is
unacceptable.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-09-18 15:22:06 -04:00
James McCoy
22cca0c821
Use “:read ++edit !...” so file encoding/format detection triggers
Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-09-17 22:45:31 -04:00
James McCoy
28e0be4cd2
Maintain 'readonly' if it's already set
If the user is using "vim -R"/":view", then the opened buffer should
have 'readonly' set, even if permissions would otherwise allow editing.

Closes jamessan/vim-gnupg#67

Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-08-25 21:38:56 -04:00
James McCoy
541e333cfb
Stop setting g:GPGPreferArmor in GPGInit
When the user hasn't explicity let g:GPGPreferArmor, the value is
supposed to be determined by the filename.  Instead of letting
g:GPGPreferArmor, lazily determine what should be done when actually
doing the encryption.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2016-04-24 23:27:34 -04:00
James McCoy
2dfb82f13e
Handle rename() failures
Renaming the temporary, encrypted file to the intended filename (per the
buffer) may fail (e.g., typoed directory name).  In this case, remove
the temporary file and then give the typical error message for this
situation.

In order to be safe, the message is manually being generated instead of
trying to be clever and do something like “noautocmd write” expecting it
to hit the same error that rename() did.

Closes jamessan/vim-gnupg#56
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-12-17 21:52:12 -05:00
Scott Stevenson
c2ddc05bc2
Fall back to gpg2 if gpg is not available
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-12-11 20:47:17 -05:00
James McCoy
bdf632d370
Explain X selection problem in Known Issues
Closes jamessan/vim-gnupg#36

Signed-off-by: James McCoy <vega.james@gmail.com>
2015-12-04 19:20:48 -05:00
James McCoy
ccdf455426
After writing the buffer to disk, update 'readonly'
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-11-04 22:03:35 -05:00
James McCoy
1c61d3f9ed
Ignore hidden recipients when decrypting
When --throw-keyids, --hidden-recipient, or --hidden-encrypt-to were
used for an encrypted file, the encryption metadata uses all zeroes as
the key ID.  Ignore these key IDs to avoid generating extraneous
messages during decryption or adding invalid IDs to the recipients list.

Closes jamessan/vim-gnupg#50
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-11-04 21:48:25 -05:00
James McCoy
a3e4dc94d4
Set 'readonly' per the permissions of the encrypted file.
Closes jamessan/vim-gnupg#51
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-11-04 21:02:31 -05:00
Simon Ruderich
97436d1158
remove unnecessary exists check for g:GPGPreferSymmetric
The variable is always set in GPGInit().

Signed-off-by: James McCoy <vega.james@gmail.com>
2015-10-05 21:08:59 -04:00
Simon Ruderich
80fd6e4861
remove superfluous | in BufReadCmd autocmd
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-10-05 21:08:56 -04:00
Simon Ruderich
842622cbc0
use variable for fnameescape(expand('<afile>:r'))
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-10-05 21:08:52 -04:00
Simon Ruderich
39bc3f96c1
use .= where possible
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-10-05 21:08:47 -04:00
Simon Ruderich
30db0eec65
update documentation to match code, add missing defaults
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-10-05 21:08:36 -04:00
James McCoy
f12d007414
Account for 'shellslash' being set on Windows
If a user has 'shellslash' set in Windows, then filename related
functionality uses forward slashes instead of backslashes.  This breaks
running of external commands because some external commands in Windows
treat any forward slash as a command line switch.

This change adds s:shellescape(), which wraps Vim's shellescape(), but
ensures 'noshellslash' is set first in Windows command prompt (not
Cygwin or msys) environments.  Having 'noshellslash' set when
shellescape() is called causes the string to be quoted, regardless of
whether there is whitespace or typical special characters, thus avoiding
the interpretation of the string as being command line switches.

Closes jamessan/vim-gnupg#41
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-09-29 19:48:18 -04:00
James McCoy
89253464f1
Use :language command to force C locale
In non-Windows environments, the gpg command is prepended with “LANG=C
LC_ALL=C” to ensure gpg's output is consistent to allow for parsing the
information.  However, that doesn't work in Windows and won't
necessarily work if we ever remove the code forcing use of /bin/sh.

Since Vim already knows how to influence the environment, use its
“:language message …” command to set/restore the locale before/after
running the gpg command.

Closes jamessan/vim-gnupg#45
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-09-29 19:32:19 -04:00
James McCoy
8394f2e1b5
Remove “--batch” from command used to detect encryption
If the “log-file” option is set in ~/.gnupg/gpg.conf, then using “--batch”
means that the CLI's “--logger-fd” is ignored.  Since this command
shouldn't need any of “--batch”'s functionality and “log-file” hides all
the output we're expecting, dropping “--batch” is the sane thing to do.

Closes #40

Signed-off-by: James McCoy <vega.james@gmail.com>

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Your branch is up-to-date with 'origin/master'.
#
# Changes to be committed:
#	modified:   plugin/gnupg.vim
#
# Untracked files:
#	foo.asc
#	foo.gpg
#	foo.sig
#
# ------------------------ >8 ------------------------
# Do not touch the line above.
# Everything below will be removed.
diff --git c/plugin/gnupg.vim i/plugin/gnupg.vim
index 80f2061..b0bcd5c 100644
--- c/plugin/gnupg.vim
+++ i/plugin/gnupg.vim
@@ -1,5 +1,5 @@
 " Name:    gnupg.vim
-" Last Change: 2015 Jul 16
+" Last Change: 2015 Jul 26
 " Maintainer:  James McCoy <vega.james@gmail.com>
 " Original Author:  Markus Braun <markus.braun@krawel.de>
 " Summary: Vim plugin for transparent editing of gpg encrypted files.
@@ -448,7 +448,7 @@ function s:GPGDecrypt(bufread)

   " find the recipients of the file
   let cmd = { 'level': 3 }
-  let cmd.args = '--verbose --decrypt --list-only --dry-run --batch --no-use-agent --logger-fd 1 ' . shellescape(filename)
+  let cmd.args = '--verbose --decrypt --list-only --dry-run --no-use-agent --logger-fd 1 ' . shellescape(filename)
   let output = s:GPGSystem(cmd)

   " Suppress the "N more lines" message when editing a file, not when reading
2015-07-26 16:07:49 -04:00
James McCoy
ac5d94519e
Setup agent handling after determining gnupg's version
Starting with release 2.1, gnupg will automatically run gpg-agent rather
than requiring it to be run as a daemon.  The “--no-use-agent” switch
was also marked deprecated, since it no longer does anything.

This commit moves the --(no-)use-agent handling until after it is known
what version of gpg is being used.

Closes #37

Signed-off-by: James McCoy <vega.james@gmail.com>
2015-07-16 23:13:12 -04:00
James McCoy
dd8e4adb4d
Silence "No matching autocommands" for TermChanged event
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-03-21 21:46:00 -04:00
James McCoy
553f9197d3 Document that GPGSystem always uses temp files
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-03-02 23:18:13 -05:00
James McCoy
671b84626e Emit TermChanged when resetting &term
Resetting &term can affect various settings which are terminal
dependent (e.g., escape sequences described at “:help :set-termcap”).
Emitting TermChanged allows people to re-set these options so they don't
lose functionality (c.f. jamessan/vim-gnupg#28).

Signed-off-by: James McCoy <vega.james@gmail.com>
2015-02-06 09:53:09 -05:00
James McCoy
380f05d2e9 Trigger FileReadPost event after decrypting for :r
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-01-05 22:53:47 -05:00
James McCoy
cdf98c6af9 Manually set [ and ] marks after decrypting
:lockmarks doesn't appear to preserve these marks.  They would need
to be adjusted anyway due to the line numbers changing, so just set the
marks to the start/end of the buffer.

Signed-off-by: James McCoy <vega.james@gmail.com>
2015-01-05 22:47:04 -05:00
James McCoy
30a49872ca Emit BufReadPre/FileReadPre when decrypting the file
Signed-off-by: James McCoy <vega.james@gmail.com>
2015-01-05 22:36:31 -05:00
James McCoy
3c01375c3b Don't change modified state of buffer for FileWriteCmd
Signed-off-by: James McCoy <vega.james@gmail.com>
2014-12-25 15:13:00 -05:00
James McCoy
9de82fa38a Emit FileWritePre/Post events when handling FileWriteCmd
Signed-off-by: James McCoy <vega.james@gmail.com>
2014-12-25 15:13:00 -05:00
Daniel Hahler
28c6e91593 Trigger BufWritePost event via BufWriteCmd/FileWriteCmd
Closes jamessan/vim-gnupg#23
Signed-off-by: James McCoy <vega.james@gmail.com>
2014-12-25 15:12:47 -05:00
James McCoy
20999789f8 Merge pull request #21 from tyll/menu
Add g:GPGRecipientsMenu
2014-11-20 23:52:31 -05:00
James McCoy
bd3ebdff6c Force Vim to re-setup the terminal after running a command
After Vim runs certain commands (e.g., pinentry-curses), the terminal is
not returned to its previous state.  This causes some escape sequences,
like arrow keys, to stop being recognized properly by Vim.

Re-setting &term to itself causes Vim to go through its normal terminal
initialization steps which gets things back in working order.

Closes jamessan/vim-gnupg#17
Signed-off-by: James McCoy <vega.james@gmail.com>
2014-11-20 23:18:59 -05:00
Sebastian Neumann
d316155866 Refactor g:GPGRecipientsMenu to use list and rename to g:GPGPossibleRecipients
let g:GPGPossibleRecipients=[
    \"Example User <example@example.com>",
    \"Other User <otherexample@example.com>"
  \]
2014-10-29 16:53:59 +01:00
Till Maas
22f68e70cc Add g:GPGRecipientsMenu
Allow to set initial contents for recipients menu with
g:GPGRecipientsMenu. Example usage:

let g:GPGRecipientsMenu=""
    \."GPG: Example User <example@example.com>\<NL>"
    \."GPG: Other User <otherexample@example.com>\<NL>"
2014-10-09 16:55:42 +02:00
James McCoy
40cec4652c Put --homedir at the start of the command line
Closes jamessan/vim-gnupg#20
Signed-off-by: James McCoy <vega.james@gmail.com>
2014-08-10 18:48:00 -04:00
James McCoy
a1bc9f1ca0 Ensure b:GPGRecipients is initialized to a list value
Closes #18
Signed-off-by: James McCoy <vega.james@gmail.com>
2014-07-27 22:26:48 -04:00
nachoalonso
6e9f52a160 Use filereadable() instead of glob() to detect whether a file exists.
glob() reports false positives if the filename has metacharacters in it
(e.g., "[...]"), which causes Vim to incorrectly open a new, blank
buffer instead of the specified file.

Closes jamessan/vim-gnupg#12

Signed-off-by: James McCoy <vega.james@gmail.com>
2013-09-09 21:05:25 -04:00
James McCoy
15fd394fc5 Restore the :silent command when encrypting the buffer
This was lost in the refactor of external command handling, commit
7d102c843, and was causing an unnecessary "Press ENTER …" prompt to be
emitted whenever the buffer was written out.

Signed-off-by: James McCoy <vega.james@gmail.com>
2013-06-14 23:03:58 -04:00
James McCoy
27ea958f5b Allow definition of actions specific to GnuPG-managed buffers
If there are specific actions that should take place when editing a
GnuPG-managed buffer, an autocmd for the User event and GnuPG pattern
can be defined.  For example, the following will set 'textwidth' to 72
for all GnuPG-encrypted buffers:

    autocmd User GnuPG setl textwidth=72

This will be triggered before any BufRead or BufNewFile autocmds, and
therefore will not take precedence over settings specific to any
filetype that may get set.

Closes jamessan/vim-gnupg#10

Signed-off-by: James McCoy <vega.james@gmail.com>
2013-04-23 22:32:13 -04:00
James McCoy
5ec1093bd7 Merge pull request #8 from Jonty/edit-recipients-on-new-file
Edit recipients when opening a new file
2013-02-22 12:42:21 -08:00
Jonty Wareing
3e060c5ea0 Edit recipients when opening a new file
The previous behaviour of this plugin was to open the recipient pane
immediately after opening a new file with the correct extension. The
documentation states that this should still occur, so this commit
implements the behaviour in a slightly different manner than the old
plugin.
2013-02-22 19:59:12 +00:00
Jonty Wareing
2fb7599083 Do not trigger encrypt on BufWrite etc in settings
If a user :w's when in the options or recipient panes the global
bufwrite handlers will be called, triggering an attempt to encrypt the
file. The user will be dropped into the command line recipient adding
process, and the recipients listed in the buffer will be ignored.

This commit checks to see if a buffer is a child of another by looking
for the buffer local var containing the parent buffer name, and only
runs the encryption if it is not present.
2013-02-22 19:58:36 +00:00
James McCoy
57695361bb Document g:GPGFilePattern 2013-02-10 11:59:44 -05:00
James McCoy
6c758903e6 Merge pull request #4 from vaz/master 2013-02-10 11:57:54 -05:00
matt-garman
faa3d652e3 Update plugin/gnupg.vim
Need to use copy() when setting b:GPGRecipients in GPGDecrypt(), otherwise the local variable will act as a reference to the global one.  The result will be that the global default recipient list will become a super-set of recipients as multiple gpg files are opened.
2013-02-05 11:52:40 -06:00