Skip to content

Commit

Permalink
Add new commands, improve line wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rdipardo committed Jul 25, 2020
1 parent f2d7b62 commit 90b30a2
Show file tree
Hide file tree
Showing 16 changed files with 377 additions and 245 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
blank_issues_enabled: true
Binary file added .github/img/nvim_043_ver_011.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### 0.1.1

- convenience commands for getting/setting global options:
- `:CPYwriteDefaultLicense` -- supports `<tab>` completion
- `:CPYwriteToggleMode` -- switches `g:cpywrite_verbatim_mode` on/off

- relaxed file naming rules to accept full paths

- recognize *CMakeLists* files with the `.txt` extension

- apply line wrapping to keep standard headers within 80 chars (still in progress; expect mixed results)

- brief notices are now fully capitalized

- better-looking standard headers for the older GPL and GFDL licenses families

### 0.1.0

- initial release
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Generate copyright headers for any open source license

<img src=".github/img/nvim_043_scheme.gif" alt="nvim-043-linux-demo" width="800"/>
<img src=".github/img/nvim_043_ver_011.gif" alt="nvim-043-linux-demo" width="800"/>

## Description

Expand All @@ -18,17 +18,30 @@ This plugin learns your name and email by invoking `git`. If that fails, the cop
## Highlights

- Python bindings compatible with python 2.7.x or 3.4+, depending on your platform and (neo)vim version, of course
- Choose from 380 licenses (press `<tab>` after the `:CPYwrite` command for suggestions)
- Choose from 380 licenses (press `<tab>` after the `CPYwriteDefaultLicense` or `:CPYwrite` command for suggestions)
- No dependency on other plugins. That said, neovim users will be grateful to have the [completeopt](https://neovim.io/doc/user/options.html#'completeopt') feature when tabbing through all the available licenses!


## Quick Reference

### Commands
| | |
|:--|:--|
|`:CPYwrite [{spdx_short_name}]`|Fetches the license identified by `spdx_short_name` (without quotes) -- uses the current value of `g:cpywrite_default_license` when no argument is given -- supports `<tab>` completion |
|`:CPYwriteDefaultLicense [{spdx_short_name}]`|Sets `g:cpywrite_default_license` to the option license identified by `spdx_short_name` (without quotes) -- prints the default licence id when called with no argument -- supports `<tab>` completion|
|`:CPYwriteToggleMode`|Switches `g:cpywrite_verbatim_mode` on or off|
|`<Plug>(cpywrite)`|Does the same as calling `:CPYwrite` with no argument|

### Default mappings
| | |
|:--|:--|
|`{Normal}LH`|Maps to `<Plug>(cpywrite)`|
|`g:cpywrite_verbatim_mode`|When set to a "truthy" value, the full license text will be requested -- only choose this when the license is no longer than 3-4 paragraphs (e.g. Unlicense, MIT, BSD 1- 2- 3-Clause, etc.)|

### Options
| | |
|:--|:--|
|`g:cpywrite_default_license`|The SPDX identifier of the license to be fetched by the `:CPYwrite` command.|
|`g:cpywrite_verbatim_mode`|When set to 1, the full license text will be requested -- only choose this when the license is no longer than 3-4 paragraphs (e.g. Unlicense, MIT, BSD 1- 2- 3-Clause, etc.)|


## Requirements
Expand Down Expand Up @@ -130,7 +143,7 @@ Distributed under the terms of the MIT license
[licl]: https://www.vim.org/scripts/script.php?script_id=4064
[vim-8-pkg-docs]: https://github.com/vim/vim/blob/03c3bd9fd094c1aede2e8fe3ad8fd25b9f033053/runtime/doc/repeat.txt#L515
[vim-pack-man]: https://shapeshed.com/vim-packages/#how-it-works
[lang-list]: https://github.com/rdipardo/vim-cpywrite/blob/6efd4e6c20be32a3c8876e77a1dd7b2736f30d3d/rplugin/pythonx/cpywrite/generator.py#L250
[lang-list]: rplugin/pythonx/cpywrite/generator.py#L255
[travis-badge]: https://travis-ci.com/rdipardo/vim-cpywrite.svg?token=yCqYFpeQtymaztY4Spav&branch=master
[travis-builds]: https://travis-ci.com/rdipardo/vim-cpywrite

Expand Down
33 changes: 25 additions & 8 deletions doc/cpywrite.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Pass a license identifier to the |:CPYwrite| command and it will insert the
standard header, if available, at the top of the current buffer.

When |g:cpywrite_verbatim_mode| is off, licenses with no standard header will
be identified below your author line. Turn on |g:cpywrite_verbatim_mode| to
be acknowledged below your author line. Turn on |g:cpywrite_verbatim_mode| to
use the full license text instead.

This plugin learns your name and email by invoking `git`; it falls back to
Expand Down Expand Up @@ -40,15 +40,14 @@ OPTIONS *cpywrite-options*
`string` (default: 'GPL-3.0-or-later')

Set this to the SPDX identifier of your favourite license. Press <tab> after
the |:CPYwrite| command to see the list.
the |:CPYwriteDefaultLicense| or |:CPYwrite| command for suggestions.

*g:cpywrite_verbatim_mode*
`number` (default: 0)

When set to a "truthy" value, the full license text will be requested (whether
or not a standard header exists). You should only choose this when the license
is no longer than 3-4 paragraphs (e.g. Unlicense, MIT, BSD 1- 2- 3-Clause,
etc.)
When set to 1, the full license text will be requested, whether or not a
standard header exists. You should only choose this when the license is no
longer than 3-4 paragraphs (e.g. Unlicense, MIT, BSD 1- 2- 3-Clause, etc.)

When set to 0, only standard headers are requested. Licenses with no standard
header will be briefly mentioned below your copyright line.
Expand All @@ -67,10 +66,28 @@ COMMANDS *cpywrite-commands*
Optionally takes the name of a SPDX license identifier (without quotes). Use
<tab> for name completion.

When called with no argument, this commmand behaves exactly like
When called with no argument, this command behaves exactly like
|<Plug>(cpywrite)|.

*<Plug>(cpywrite)*
*:CPYwriteDefaultLicense*
:CPYwriteDefaultLicense [{spdx_short_name}]

Sets |g:cpywrite_default_license| to the given SPDX license identifier (without
quotes) and prints the updated value. Use <tab> for name completion.

Simply prints |g:cpywrite_default_license| when called with no argument.

*:CPYwriteToggleMode*
:CPYwriteToggleMode

Toggles the state of the |g:cpywrite_verbatim_mode| option and prints a
description of the updated state:
>
0 == "standard header/brief"
1 == "verbatim"
<

*<Plug>(cpywrite)*
Quick action command for fetching the default license assigned to
|g:cpywrite_default_license|.

Expand Down
2 changes: 2 additions & 0 deletions doc/tags
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
:CPYwrite cpywrite.txt /*:CPYwrite*
:CPYwriteDefaultLicense cpywrite.txt /*:CPYwriteDefaultLicense*
:CPYwriteToggleMode cpywrite.txt /*:CPYwriteToggleMode*
<Plug>(cpywrite) cpywrite.txt /*<Plug>(cpywrite)*
LH cpywrite.txt /*LH*
cpywrite-commands cpywrite.txt /*cpywrite-commands*
Expand Down
39 changes: 31 additions & 8 deletions plugin/cpywrite.vim
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ if !has('python3')
\ ['Enter :help provider-python for more information.'])
else
echohl WarningMsg |
echom 'Error loading cpywrite: this plugin requires one of the
\ following features: +python, +python3, +python/dyn, or
echom 'Error loading vim-cpywrite: this plugin requires one of
\ the following features: +python, +python3, +python/dyn, or
\ +python3/dyn. Enter :help python for more information.'
\ | echohl None
endif
Expand All @@ -29,40 +29,63 @@ let s:cpywrite_python_cmd = (has('python3') ? 'py3' : 'py') . 'file'

if !exists('g:cpywrite_version')
exe s:cpywrite_python_cmd
\ fnamemodify(globpath(&rtp, 'plugin/cpywrite/version.py', 0, 1)[0],
\ ':p')
\ fnamemodify(
\ globpath(&rtp, 'plugin/cpywrite/version.py', 0, 1)[0],
\ ':p')
endif

if !exists('g:cpywrite_verbatim_mode')
let g:cpywrite_verbatim_mode = 0
endif

if !get(g:, 'cpywrite_default_license', 0)
if empty(get(g:, 'cpywrite_default_license', ''))
let g:cpywrite_default_license = 'GPL-3.0-or-later'
endif

func! s:prepend_header(...) abort
let l:license_name = get(a:, 1, g:cpywrite_default_license)
exe s:cpywrite_python_cmd
\ fnamemodify(globpath(&rtp, 'plugin/cpywrite/main.py', 0, 1)[0],
\ ':p')
\ fnamemodify(
\ globpath(&rtp, 'plugin/cpywrite/main.py', 0, 1)[0],
\ ':p')
endfunc

func! s:get_license_list(a,l,p) abort
let s:license_list=[]
let l:subs = get(a:, 'a', '')
exe s:cpywrite_python_cmd
\ fnamemodify(globpath(&rtp, 'plugin/cpywrite/tabs.py', 0, 1)[0],
\ fnamemodify(
\ globpath(&rtp, 'plugin/cpywrite/tabs.py', 0, 1)[0],
\ ':p')

return s:license_list
endfunc

func! s:set_default_license(...)
if !empty(get(a:, 1, ''))
exe 'let g:cpywrite_default_license = ' . string(a:1)
endif

exe 'echo g:cpywrite_default_license'
endfunc

if !exists(':CPYwrite')
com! -nargs=* -complete=customlist,s:get_license_list
\ CPYwrite :call s:prepend_header(<f-args>)
endif

if !exists(':CPYwriteDefaultLicense')
com! -nargs=* -complete=customlist,s:get_license_list
\ CPYwriteDefaultLicense :call s:set_default_license(<f-args>)
endif

if !exists(':CPYwriteToggleMode')
com! CPYwriteToggleMode
\ exe 'if(g:cpywrite_verbatim_mode) | let g:cpywrite_verbatim_mode = 0 |
\ else | let g:cpywrite_verbatim_mode = 1 | endif |
\ echo (g:cpywrite_verbatim_mode) ? "verbatim" : "standard header/brief"'
endif

nnoremap <silent> <Plug>(cpywrite)
\ :exe 'CPYwrite ' . g:cpywrite_default_license . ' '<CR>
Expand Down
9 changes: 4 additions & 5 deletions plugin/cpywrite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
def prepend():
"""Prepend license header to the open buffer"""
if vim.current.buffer:
vim.command("let b:fn=expand('%:t')")
filename = vim.eval('b:fn')
license_name = vim.eval('l:license_name')

try:
generator = Generator(rights=license_name)
generator = Generator(filename, license_name)
_write_header(generator, vim.current.buffer)
except (ValueError, vim.error) as exc:
print(str(exc))
Expand All @@ -28,19 +30,16 @@ def _write_header(writer, curr_buffer):
"""Write the license header"""
try:
old_row, old_col = vim.current.window.cursor
vim.command("let b:fn=expand('%:t')")
filename = vim.eval('b:fn')
use_text_as_header = vim.eval('g:cpywrite_verbatim_mode')

try:
use_text_as_header = bool(int(use_text_as_header, 10))
except ValueError:
print("'g:cpywrite_verbatim_mode' should be set to a number!",
print("'g:cpywrite_verbatim_mode' should be set to 1 or 0!",
file=sys.stderr)
vim.command('let g:cpywrite_verbatim_mode=0')
use_text_as_header = False

writer.set_file_props(filename)
header = writer.fetch_license_header(use_text_as_header)

if header:
Expand Down
2 changes: 1 addition & 1 deletion rplugin/pythonx/cpywrite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
~~~~~~~~~
"""

__version__ = '0.1.0'
__version__ = '0.1.1'
__authors__ = ['Robert Di Pardo']
__url__ = 'https://github.com/rdipardo/vim-cpywrite'
__license__ = 'MIT'
Expand Down
Loading

0 comments on commit 90b30a2

Please sign in to comment.