Commit Graph

102 Commits

Author SHA1 Message Date
James McCoy
6be6f96aa5
Do not set buftype=acwrite for FileReadCmd
FileReadCmd reads the contents of another file into the current buffer.
Therefore, it should honor the settings ('buftype' and :file) of the
current buffer.

Closes jamessan/vim-gnupg#86
Signed-off-by: James McCoy <jamessan@jamessan.com>
2018-01-23 22:06:27 -05:00
James McCoy
599a039254
fnameescape() paths for constructed commands
Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-12-15 14:44:04 -05:00
James McCoy
c411e61aeb
On save, only unset 'modified' for the current filename
If the buffer is being saved to a different filename, then this buffer
should still be modified as its backing file is still different than the
buffer.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-10-22 23:24:22 -04:00
James McCoy
d3453145c5
Set the buffer name to the resolved, absolute path
If buftype=acwrite, Vim doesn't adjust relative buffer names when
changing directory.  Since 5103285d, encrypted buffers have set
buftype=acwrite and therefore also risked having incorrect buffer names
(e.g., if 'autochdir' is set).

Now that the buffer name is always fully resolved, this should no longer
be a problem.

Closes jamessan/vim-gnupg#81
Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-10-22 23:24:09 -04:00
James McCoy
ae1b31965e
Ensure buftype=acwrite is always set for the buffer
The user may be a) editing an existing encrypted file, b) creating a new
encrypted file, c) editing an unnamed buffer and saving it as an
encrypted file.  In all cases, 'buftype' should be set correctly once we
know the buffer is intended to be encrypted.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-10-22 23:16:28 -04:00
James McCoy
e7b6648ab2
Merge branch 'gpg4win' 2017-05-31 22:24:26 -04:00
James McCoy
9d600d73fa
Support using vim-gnupg from cygwin with Gpg4win
Gpg4win expects native Windows paths even when it's being run from a
cygwin environment.  In order to satisfy that, update `s:shellescape` to
know whether it should convert the paths to native Windows paths using
`cygpath -am`.

In order to detect whether cygpath should be invoked, look for the
combination of `has('win32unix')` being true and `gpg --version`
reporting a Windows style path for gpg's home directory.

Closes jamessan/vim-gnupg#62

Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-05-31 00:37:06 -04:00
James McCoy
5103285d4d
Set "buftype=acwrite" for encrypted buffers
This ensures any write of the buffer requires a configured BufWriteCmd
autocmd, providing more safe guards against problems like
jamessan/vim-gnupg#77.

Signed-off-by: James McCoy <jamessan@jamessan.com>
2017-02-14 22:12:05 -05:00
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