├── LICENSE ├── README.md ├── doc └── copy-as-rtf.txt └── plugin └── copy-as-rtf.vim /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2011 Nathan Witmer 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vim-copy-as-rtf 2 | 3 | This plugin provides a `CopyRTF` command for OS X systems that copies the 4 | current buffer or selected text to the clipboard as syntax-highlighted RTF text. 5 | 6 | This plugin was inspired by the 7 | [rtf-highlight](https://github.com/dharanasoft/rtf-highlight) plugin, but only 8 | uses commands available by default on OS X and an HTML conversion plugin that 9 | ships with vim. 10 | 11 | ## Requirements 12 | 13 | * OS X 14 | * The `:TOhtml` plugin, which ships with vim and is enabled by default 15 | 16 | ## Installation 17 | 18 | Use [pathogen](https://github.com/tpope/vim-pathogen/) and clone this repo to 19 | `~/.vim/bundle`. 20 | 21 | ## Usage 22 | 23 | When the plugin is loaded, the following command is available: 24 | 25 | :CopyRTF 26 | 27 | It operates on either the current buffer or the currently selected text. After 28 | the command executes, the RTF text will be available on the system clipboard. 29 | 30 | For customization of how the text is generated, see the vim documentation for 31 | the `:TOhtml` command. 32 | -------------------------------------------------------------------------------- /doc/copy-as-rtf.txt: -------------------------------------------------------------------------------- 1 | *copy-as-rtf.txt* Plugin for copying syntax highlighted text as RTF 2 | 3 | This plugin lets you select text or a buffer and copy it to the OS X 4 | clipboard as RTF text. 5 | 6 | Requirements~ 7 | 8 | * OS X 9 | * The |:TOhtml| plugin, which comes with vim 10 | 11 | Commands~ 12 | 13 | *:CopyRTF* 14 | 15 | Copies the current buffer or the selected block as RTF to the clipboard. 16 | 17 | 18 | *copy-as-rtf-settings* 19 | 20 | *g:copy_as_rtf_preserve_indent* (default 0) 21 | 22 | This setting determines whether or not |:CopyRTF| will preserve the 23 | indentation for the text it's converting. If 0, code will be outdented as much 24 | as possible (ignoring non-code lines) before conversion. 25 | 26 | *g:copy_as_rtf_using_local_buffer* (default 0) 27 | 28 | This setting determines whether or not temporary indentation changes (see 29 | previous setting) are applied in the current buffer. If set to 0, the selected 30 | code is copied to a scratch buffer before modifying it. 31 | 32 | The |:TOhtml| plugin can be also be configured to change how text is converted 33 | to html before being converted to RTF. To set the font to match your MacVim or 34 | terminal settings, use this (undocumented) setting: 35 | 36 | let g:html_font="Your Favorite Console Font" 37 | 38 | 39 | vim:tw=78:ts=8:ft=help:norl: 40 | -------------------------------------------------------------------------------- /plugin/copy-as-rtf.vim: -------------------------------------------------------------------------------- 1 | " Vim plugin for copying syntax highlighted code as RTF on OS X systems 2 | " Last Change: 2012-07-14 3 | " Maintainer: Nathan Witmer 4 | " License: WTFPL 5 | 6 | if exists('g:loaded_copy_as_rtf') 7 | finish 8 | endif 9 | let g:loaded_copy_as_rtf = 1 10 | 11 | " Set this to 1 to tell copy_as_rtf to use the local buffer instead of a scratch 12 | " buffer with the selected code. Use this if the syntax highlighting isn't 13 | " correctly handling your code when removed from its context in its original 14 | " file. 15 | if !exists('g:copy_as_rtf_using_local_buffer') 16 | let g:copy_as_rtf_using_local_buffer = 0 17 | endif 18 | 19 | " Set this to 1 to preserve the indentation as-is when converting to RTF. 20 | " Otherwise, the selected lines are outdented as far as possible before 21 | " conversion. 22 | if !exists('g:copy_as_rtf_preserve_indent') 23 | let g:copy_as_rtf_preserve_indent = 0 24 | endif 25 | 26 | if !executable('pbcopy') || !executable('textutil') 27 | echomsg 'cannot load copy-as-rtf plugin, not on a mac?' 28 | finish 29 | endif 30 | 31 | " copy the current buffer or selected text as RTF 32 | " 33 | " bufnr - the buffer number of the current buffer 34 | " line1 - the start line of the selection 35 | " line2 - the ending line of the selection 36 | function! s:CopyRTF(bufnr, line1, line2) 37 | 38 | " check at runtime since this plugin may not load before this one 39 | if !exists(':TOhtml') 40 | echoerr 'cannot load copy-as-rtf plugin, TOhtml command not found.' 41 | finish 42 | endif 43 | 44 | " save the alternate file and restore it at the end 45 | let l:alternate=bufnr(@#) 46 | 47 | if g:copy_as_rtf_using_local_buffer 48 | let lines = getline(a:line1, a:line2) 49 | 50 | if !g:copy_as_rtf_preserve_indent 51 | call s:RemoveCommonIndentation(a:line1, a:line2) 52 | endif 53 | call tohtml#Convert2HTML(a:line1, a:line2) 54 | silent exe "%!textutil -convert rtf -stdin -stdout | pbcopy" 55 | 56 | silent bd! 57 | silent call setline(a:line1, lines) 58 | else 59 | 60 | " open a new scratch buffer 61 | let orig_ft = &ft 62 | let l:orig_bg = &background 63 | let l:orig_nu = &number 64 | let l:orig_nuw = &numberwidth 65 | if exists("b:is_bash") 66 | let l:is_bash = b:is_bash 67 | endif 68 | new __copy_as_rtf__ 69 | " enable the same syntax highlighting 70 | if exists("l:is_bash") 71 | let b:is_bash=l:is_bash 72 | endif 73 | let &ft=orig_ft 74 | let &background=l:orig_bg 75 | let &number=l:orig_nu 76 | let &numberwidth=l:orig_nuw 77 | set buftype=nofile 78 | set bufhidden=hide 79 | setlocal noswapfile 80 | 81 | " copy the selection into the scratch buffer 82 | call setline(1, getbufline(a:bufnr, a:line1, a:line2)) 83 | 84 | if !g:copy_as_rtf_preserve_indent 85 | call s:RemoveCommonIndentation(1, line('$')) 86 | endif 87 | 88 | call tohtml#Convert2HTML(1, line('$')) 89 | silent exe "%!textutil -convert rtf -stdin -stdout | pbcopy" 90 | silent bd! 91 | silent bd! 92 | endif 93 | 94 | let @# = l:alternate 95 | echomsg "RTF copied to clipboard" 96 | endfunction 97 | 98 | " outdent selection to the least indented level 99 | function! s:RemoveCommonIndentation(line1, line2) 100 | " normalize indentation 101 | silent exe a:line1 . ',' . a:line2 . 'retab' 102 | 103 | let lines_with_code = filter(range(a:line1, a:line2), 'match(getline(v:val), ''\S'') >= 0') 104 | let minimum_indent = min(map(lines_with_code, 'indent(v:val)')) 105 | let pattern = '^\s\{' . minimum_indent . '}' 106 | call setline(a:line1, map(getline(a:line1, a:line2), 'substitute(v:val, pattern, "", "")')) 107 | endfunction 108 | 109 | command! -range=% CopyRTF :call s:CopyRTF(bufnr('%'),,) 110 | --------------------------------------------------------------------------------