1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-04-14 15:19:12 +08:00

Improve pastebin plugin (#3797)

This commit is contained in:
Wang Shidong 2020-09-10 21:03:26 +08:00 committed by GitHub
parent dc1c6694e2
commit d9bc5a3dae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 107 additions and 52 deletions

View File

@ -177,7 +177,7 @@ function! SpaceVim#default#keyBindings() abort
xnoremap <Leader>P "*P
endif
xnoremap <silent><Leader>Y :call SpaceVim#plugins#pastebin#paste()<CR>
xnoremap <silent><Leader>Y :<C-u>call SpaceVim#plugins#pastebin#paste()<CR>
" call SpaceVim#mapping#guide#register_displayname(':call SpaceVim#plugins#pastebin#paste()<CR>', 'copy to pastebin')
" quickfix list movement
@ -252,11 +252,6 @@ function! SpaceVim#default#keyBindings() abort
nnoremap <silent><Down> gj
nnoremap <silent><Up> gk
" Navigate window
nnoremap <silent><C-q> <C-w>
" Fast saving
nnoremap <C-s> :<C-u>w<CR>
vnoremap <C-s> :<C-u>w<CR>
@ -353,7 +348,7 @@ endfunction
function! s:switch_tabs() abort
let previous_tab = s:TAB.previous_tabpagenr()
if previous_tab > 0
exe "tabnext " . previous_tab
exe 'tabnext ' . previous_tab
endif
endfunction

View File

@ -8,49 +8,63 @@
let s:JOB = SpaceVim#api#import('job')
let s:LOGGER =SpaceVim#logger#derive('pastebin')
let s:job_id = -1
function! SpaceVim#plugins#pastebin#paste()
function! SpaceVim#plugins#pastebin#paste() abort
let s:url = ''
let context = s:get_visual_selection()
if empty(context)
call s:LOGGER.info('no selection text, skipped.')
return
endif
" let ft = &filetype
if s:job_id != -1
call s:LOGGER.info('previous job has not been finished, killed!')
call s:JOB.stop(s:job_id)
endif
let cmd = 'curl -s -F "content=<-" http://dpaste.com/api/v2/'
let s:job_id = s:JOB.start(cmd,{
\ 'on_stdout' : function('s:on_stdout'),
\ 'on_stderr' : function('s:on_stderr'),
\ 'on_exit' : function('s:on_exit'),
\ })
call s:LOGGER.info('job id: '. s:job_id)
call s:JOB.send(s:job_id, split(context, "\n"))
call s:JOB.chanclose(s:job_id, 'stdin')
endfunction
function! s:on_stdout(job_id, data, event) abort
" echom 'stdout:' . string(a:data)
for url in filter(a:data, '!empty(v:val)')
call s:LOGGER.info('stdout: '. url)
let s:url = url
endfor
endfunction
function! s:on_stderr(job_id, data, event) abort
" echom 'stderr:' . string(a:data)
call s:LOGGER.warn('stderr:' . string(a:data))
endfunction
function! s:on_exit(job_id, data, event) abort
let s:job_id = -1
if a:data ==# 0 && !empty(s:url)
let @+ = s:url . '.txt'
echo 'Pastbin: ' . s:url . '.txt'
else
call s:LOGGER.warn('exit code: ' . string(a:data))
call s:LOGGER.warn('url: ' . s:url)
endif
endfunction
" ref: https://stackoverflow.com/a/6271254
function! s:get_visual_selection()
" Why is this not a built-in Vim script function?!
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
let lines = getline(line_start, line_end)
if len(lines) == 0
return ''
endif
let lines[-1] = lines[-1][: column_end - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][column_start - 1:]
return join(lines, "\n")
function! s:get_visual_selection() abort
" Why is this not a built-in Vim script function?!
let [line_start, column_start] = getpos("'<")[1:2]
let [line_end, column_end] = getpos("'>")[1:2]
let lines = getline(line_start, line_end)
if len(lines) == 0
return ''
endif
let lines[-1] = lines[-1][: column_end - (&selection ==# 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][column_start - 1:]
return join(lines, "\n")
endfunction

View File

@ -34,13 +34,14 @@ lang: zh
- [文件树中打开文件](#文件树中打开文件)
- [基本操作](#基本操作)
- [原生功能](#原生功能)
- [可视模式快捷键](#可视模式快捷键)
- [命令行模式快捷键](#命令行模式快捷键)
- [快捷键导航](#快捷键导航)
- [基本编辑操作](#基本编辑操作)
- [移动文本块](#移动文本块)
- [文本操作命令](#文本操作命令)
- [文本插入命令](#文本插入命令)
- [增加或减小数字](#增加或减小数字)
- [复制粘贴](#复制粘贴)
- [增删注释](#增删注释)
- [文本编码格式](#文本编码格式)
- [窗口管理](#窗口管理)
@ -791,20 +792,6 @@ SpaceVim 的文件树提供了版本控制信息的接口,但是这一特性
| `<leader> q r?` | 原生 `q ?` 快捷键,打开命令行窗口 |
| `<leader> q r:` | 原生 `q :` 快捷键,打开命令行窗口 |
### 可视模式快捷键
| 快捷键 | 功能描述 |
| ----------------- | ---------------------------- |
| `<Leader> y` | 复制选中文本至系统剪切板 |
| `<Leader> p` | 粘贴系统剪切板内容至当前位置 |
| `<` | 向左移动文本 |
| `>` | 向右移动文本 |
| `<Tab>` | 向左移动文本 |
| `Shift-<Tab>` | 向右移动文本 |
| `Ctrl-Shift-Up` | 向上移动选中行 |
| `Ctrl-Shift-Down` | 向下移动选中行 |
| `Ctrl-q` | `Ctrl-w` |
### 命令行模式快捷键
常规模式下按下 `:` 键后,可进入命令行模式,再次可以是下可以编辑 Vim 的命令并执行,
@ -837,7 +824,6 @@ SpaceVim 的文件树提供了版本控制信息的接口,但是这一特性
| `[Window]` | `windows_leader` / `s` | SpaceVim 默认窗口前缀键 |
| `<leader>` | 默认的 Vim leader 键 | Vim/Neovim 默认前缀键 |
默认的 `<Leader>` 键是 `\`, 如果需要修改 `<Leader>`
键则需要使用启动函数修改 `g:mapleader` 的值,
比如使用逗号 `,` 作为 `<Leader>` 按键。
@ -882,6 +868,15 @@ call SpaceVim#custom#SPC('nnoremap', ['f', 't'], 'echom "hello world"', 'test cu
### 基本编辑操作
#### 移动文本块
| 快捷键 | 功能描述 |
| ----------------- | -------------- |
| `<` / `Shift-Tab` | 向左移动文本 |
| `>` / `Tab` | 向右移动文本 |
| `Ctrl-Shift-Up` | 向上移动选中行 |
| `Ctrl-Shift-Down` | 向下移动选中行 |
#### 文本操作命令
文本相关的命令 (以 `x` 开头)
@ -987,6 +982,33 @@ call SpaceVim#custom#SPC('nnoremap', ['f', 't'], 'echom "hello world"', 'test cu
**提示:** 如果你想为光标下的数字所增加的值大于 `1`,你可以使用前缀参数。例如:`10 SPC n +` 将为光标下的数字加 `10`
#### 复制粘贴
如果 `has('unnamedplus')` 返回 `1`,那么快捷键 `<Leader> y` 使用的寄存器是 `+`
否则,这个快捷键使用的寄存器是 `*`
可以阅读 `:h registers` 获取更多关于寄存器相关的内容。
| 快捷键 | 功能描述 |
| ------------ | ---------------------------- |
| `<Leader> y` | 复制文本至系统剪切板 |
| `<Leader> p` | 粘贴系统剪切板文字至当前位置 |
| `<Leader> Y` | 复制文本至 pastebin |
快捷键 `<Leader< Y` 将把选中的文本复制到 pastebin 服务器,并且将返回的链接复制到系统剪切板。
使用该功能,需要系统里有 `curl` 可执行程序Windows 系统下Neovim 自带 `curl`)。
按下快捷键 `<Leader> Y` 后,实际执行的命令为:
```
curl -s -F "content=<-" http://dpaste.com/api/v2/
```
该命令将读取标准输入(`stdin`),并且把输入的内容复制到 dpaste 服务器,等同于:
```
echo "selected text" | curl -s -F "content=<-" http://dpaste.com/api/v2/
```
#### 增删注释
注释的增删是通过插件 [nerdcommenter](https://github.com/preservim/nerdcommenter) 来实现的,

View File

@ -32,13 +32,14 @@ description: "General documentation about how to using SpaceVim, including the q
- [Open file with file tree.](#open-file-with-file-tree)
- [General usage](#general-usage)
- [Native functions](#native-functions)
- [Visual mode key bindings](#visual-mode-key-bindings)
- [Command line mode key bindings](#command-line-mode-key-bindings)
- [Mappings guide](#mappings-guide)
- [Editing](#editing)
- [Moving text](#moving-text)
- [Text manipulation commands](#text-manipulation-commands)
- [Text insertion commands](#text-insertion-commands)
- [Increase/Decrease numbers](#increasedecrease-numbers)
- [Copy and paste](#copy-and-paste)
- [Commenting](#commenting)
- [Multi-Encodings](#multi-encodings)
- [Window manager](#window-manager)
@ -832,6 +833,10 @@ The following key bindings are the general key bindings for moving cursor.
### Native functions
When vimcompatible is not enabled, some native key bindings of vim
has been overrided. To use these key bindings, SpaceVim provides
alternate key bindings:
| Key bindings | Mode | Action |
| ---------------- | ------ | --------------------------------- |
| `<Leader> q r` | Normal | Same as native `q` |
@ -839,20 +844,6 @@ The following key bindings are the general key bindings for moving cursor.
| `<Leader> q r ?` | Normal | Same as native `q ?`, open cmdwin |
| `<Leader> q r :` | Normal | Same as native `q :`, open cmdwin |
### Visual mode key bindings
| Key | Action |
| ----------------- | ---------------------------------------- |
| `<Leader> y` | Copy selection to X11 clipboard ("+y) |
| `<Leader> p` | Paste selection from X11 clipboard ("+p) |
| `<` | Indent to left and re-select |
| `>` | Indent to right and re-select |
| `<Tab>` | Indent to right and re-select |
| `Shift-<Tab>` | Indent to left and re-select |
| `Ctrl-q` | `Ctrl-w` |
| `Ctrl-Shift-Up` | move lines up |
| `Ctrl-Shift-Down` | move lines down |
### Command line mode key bindings
After pressing `:`, you can switch to command line mode, here is a list of key bindings
@ -932,6 +923,15 @@ Then use `<Tab>` or `<Up>` and `<Down>` to select the mapping, press `<Enter>` t
### Editing
#### Moving text
| Key | Action |
| ----------------- | ----------------------------- |
| `>` / `Tab` | Indent to right and re-select |
| `<` / `Shift-Tab` | Indent to left and re-select |
| `Ctrl-Shift-Up` | move lines up |
| `Ctrl-Shift-Down` | move lines down |
#### Text manipulation commands
Text related commands (start with `x`):
@ -1037,6 +1037,30 @@ In transient state:
**Tips:** You can increase or decrease a number by more than once by using a prefix argument (i.e. `10 SPC n +` will add 10 to the number under cursor).
#### Copy and paste
If `has('unnamedplus')`, the register used by `<Leader> y` is `+`, otherwise it is `*`.
Read `:h registers` for more info about other registers.
| Key | Action |
| ------------ | -------------------------------- |
| `<Leader> y` | Copy text to system clipboard |
| `<Leader> p` | Paste text from system clipboard |
| `<Leader> Y` | Copy text to pastebin |
The `<Leader< Y` key binding will copy selected text to a pastebin server. It requires `curl` in your `$PATH`.
And the default command is:
```
curl -s -F "content=<-" http://dpaste.com/api/v2/
```
This command will read stdin and copy the stdin to dpaste server. It is same as:
```
echo "selected text" | curl -s -F "content=<-" http://dpaste.com/api/v2/
```
#### Commenting
Comments are handled by [nerdcommenter](https://github.com/scrooloose/nerdcommenter), its bound to the following keys.