Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend to other markup languages #56

Merged
merged 3 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,32 @@ autocmd FileType markdown nmap <buffer><silent> <leader>p :call mdip#MarkdownCli
" let g:mdip_imgname = 'image'
```

### Extend to other markup languages ###
Simply add a custom paste function that accepts the relative path to the image as an argument, and set `g:PasteImageFunction` to the name of your function. E.g.
```
function! g:LatexPasteImage(relpath)
execute "normal! i\\includegraphics{" . a:relpath . "}\r\\caption{I"
let ipos = getcurpos()
execute "normal! a" . "mage}"
call setpos('.', ipos)
execute "normal! ve\<C-g>"
endfunction
```
Then in your .vimrc:
```
autocmd FileType markdown let g:PasteImageFunction = 'g:MarkdownPasteImage'
autocmd FileType tex let g:PasteImageFunction = 'g:LatexPasteImage'
```
The former sets the (default) markdown paste function for markdown files, while the latter sets the new latex paste function to be used in latex/tex files. The above LatesPasteImage has already been added to the plugin, see `plugin/mdip.vim`. Existing paste functions:

| Filetype | Function name | Content |
|----------|---------------|---------|
| Markdown | MarkdownPasteImate | `![Image](path)` |
| Latex | LatexPasteImate | `\includegraphics{path} \caption{Image}` |
| N/A | EmptyPasteImate | `path` |

PRs welcome

### For linux user
This plugin gets clipboard content by running the `xclip` command.

Expand Down
30 changes: 25 additions & 5 deletions plugin/mdip.vim
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,28 @@ function! s:InputName()
return name
endfunction

function! g:MarkdownPasteImage(relpath)
execute "normal! i![" . g:mdip_tmpname[0:0]
let ipos = getcurpos()
execute "normal! a" . g:mdip_tmpname[1:] . "](" . a:relpath . ")"
call setpos('.', ipos)
execute "normal! vt]\<C-g>"
endfunction

function! g:LatexPasteImage(relpath)
execute "normal! i\\includegraphics{" . a:relpath . "}\r\\caption{I"
let ipos = getcurpos()
execute "normal! a" . "mage}"
call setpos('.', ipos)
execute "normal! ve\<C-g>"
endfunction

function! g:EmptyPasteImage(relpath)
execute "normal! i" . a:relpath
endfunction

let g:PasteImageFunction = 'g:MarkdownPasteImage'

function! mdip#MarkdownClipboardImage()
" detect os: https://vi.stackexchange.com/questions/2572/detect-os-in-vimscript
let s:os = "Windows"
Expand All @@ -198,11 +220,9 @@ function! mdip#MarkdownClipboardImage()
" let relpath = s:SaveNewFile(g:mdip_imgdir, tmpfile)
let extension = split(tmpfile, '\.')[-1]
let relpath = g:mdip_imgdir_intext . '/' . g:mdip_tmpname . '.' . extension
execute "normal! i![" . g:mdip_tmpname[0:0]
let ipos = getcurpos()
execute "normal! a" . g:mdip_tmpname[1:] . "](" . relpath . ")"
call setpos('.', ipos)
execute "normal! vt]\<C-g>"
if call(get(g:, 'PasteImageFunction'), [relpath])
return
endif
endif
endfunction

Expand Down