├── .gitignore ├── LICENSE ├── README.md ├── doc └── marked.txt └── plugin └── marked.vim /.gitignore: -------------------------------------------------------------------------------- 1 | doc/tags 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Joshua Priddle 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # marked.vim 2 | 3 | Open the current Markdown buffer in [Marked 2](https://marked2app.com/). 4 | 5 | Adds `:MarkedOpen`, `:MarkedQuit`, `:MarkedToggle`, and `:MarkedPreview` 6 | commands to Markdown buffers, allowing you to quickly open, close, and preview 7 | content in Marked. 8 | 9 | **Note**: Since Marked is available only for macOS, this plugin will not be 10 | loaded on other operating systems. 11 | 12 | ## Usage 13 | 14 | This plugin adds the following commands to Markdown buffers (see 15 | `g:marked_filetypes` below to add more FileTypes): 16 | 17 | #### :MarkedOpen[!] 18 | 19 | Open the current Markdown file in Marked. Call with a bang to prevent Marked 20 | from stealing focus from Vim. Documents opened in Marked are tracked and 21 | closed automatically when you quit Vim. 22 | 23 | #### :MarkedQuit[!] 24 | 25 | Close the current Markdown buffer in Marked. Quits Marked if there are no 26 | other documents open. Call with a bang to quit Marked completely. 27 | 28 | #### :MarkedToggle[!] 29 | 30 | If the current Markdown file is already open in Marked, same as 31 | `:MarkedQuit[!]`. If not, same as `:MarkedOpen[!]`. 32 | 33 | #### :[range]MarkedPreview[!] 34 | 35 | Send the current range (defaults to the entire buffer) to Marked as a preview. 36 | Call with a bang to prevent Marked from stealing focus from Vim. 37 | 38 | ## Configuration 39 | 40 | **`g:marked_filetypes`** 41 | 42 | Vim FileTypes that can be opened by Marked and will load this plugin. The 43 | default is as follows and can be customized in your `vimrc` if necessary: 44 | 45 | ```vim 46 | let g:marked_filetypes = ["markdown", "mkd", "ghmarkdown", "vimwiki"] 47 | ``` 48 | 49 | **`g:marked_auto_quit`** 50 | 51 | If true, quit Marked when Vim exits. Default is true. To disable, add the 52 | following to your `vimrc`: 53 | 54 | ```vim 55 | let g:marked_auto_quit = 0 56 | ``` 57 | 58 | **`g:marked_app`** 59 | 60 | The Marked application name. By default this is "Marked 2". If your version of 61 | Marked 2 doesn't open with `open -a "Marked 2"`, you can specify a different 62 | name or path to the application. 63 | 64 | ```vim 65 | let g:marked_app = "/Applications/Setapp/Marked 2.app" 66 | ``` 67 | 68 | Note that this plugin requires Marked 2. If you are still using Marked 1, you 69 | can use the 1.0.0 release of this plugin (see [below](#marked-1)). 70 | 71 | ## Installation 72 | 73 | Use your favorite plugin manager to install this plugin. For example, using 74 | [vim-plug](https://github.com/junegunn/vim-plug): 75 | 76 | ```vim 77 | Plug 'itspriddle/vim-marked' 78 | ``` 79 | 80 | Or using vim's built-in package manager: 81 | 82 | ```sh 83 | mkdir -p ~/.vim/pack/itspriddle/start 84 | cd ~/.vim/pack/itspriddle/start 85 | git clone https://github.com/itspriddle/vim-marked.git 86 | vim -u NONE -c "helptags vim-marked/doc" -c q 87 | ``` 88 | 89 | ### Marked 1 90 | 91 | If you are using the older first version of Marked, you can use v1.0.0 of this 92 | plugin from . 93 | 94 | For vim-plug: 95 | 96 | ```vim 97 | Plug 'itspriddle/vim-marked', { 'tag': 'v1.0.0' } 98 | ``` 99 | 100 | Or using vim's built-in package manager: 101 | 102 | ```sh 103 | mkdir -p ~/.vim/pack/itspriddle/start 104 | cd ~/.vim/pack/itspriddle/start 105 | git clone --branch v1.0.0 https://github.com/itspriddle/vim-marked.git 106 | vim -u NONE -c "helptags vim-marked/doc" -c q 107 | ``` 108 | 109 | ## License 110 | 111 | MIT License - see [`LICENSE`](./LICENSE) in this repo. 112 | -------------------------------------------------------------------------------- /doc/marked.txt: -------------------------------------------------------------------------------- 1 | *marked.txt* Open Markdown in Marked. 2 | Author: Joshua Priddle 3 | License: MIT (see |marked-license|) 4 | Version: 2.0.0-beta 5 | 6 | This plugin is only available on macOS and if 'compatible' is not set. 7 | 8 | ============================================================================== 9 | INTRODUCTION *marked* 10 | 11 | Vim plugin for Marked 2 (https://marked2app.com), a previewer for Markdown 12 | files. 13 | 14 | Adds |:MarkedOpen|, |:MarkedQuit|, |:MarkedToggle|, and |:MarkedPreview| 15 | commands to Markdown buffers, allowing you to quickly open, close, and preview 16 | content in Marked. 17 | 18 | ============================================================================== 19 | COMMANDS *marked-commands* 20 | 21 | This plugin adds the following commands to Markdown buffers (see 22 | |g:marked_filetypes| to add more |FileTypes|): 23 | 24 | :MarkedOpen[!] *marked-:MarkedOpen* 25 | 26 | Open the current Markdown file in Marked. Call with a bang to prevent Marked 27 | from stealing focus from Vim. Documents opened in Marked are tracked and 28 | closed automatically when you quit Vim. 29 | 30 | :MarkedQuit[!] *marked-:MarkedQuit* 31 | 32 | Close the current Markdown buffer in Marked. Quits Marked if there are no 33 | other documents open. Call with a bang to quit Marked completely. 34 | 35 | :MarkedToggle[!] *marked-:MarkedToggle* 36 | 37 | If the current Markdown file is already open in Marked, same as 38 | |:MarkedQuit|. If not, same as |:MarkedOpen|. 39 | 40 | :[range]MarkedPreview[!] *marked-:MarkedPreview* 41 | 42 | Send the current range (defaults to the enture buffer) to Marked as a preview. 43 | Call with a bang to prevent Marked from stealing focus from Vim. 44 | 45 | ============================================================================== 46 | CONFIGURATION *marked-configuration* 47 | 48 | *g:marked_filetypes* 49 | 50 | Vim FileTypes that can be opened by Marked and will load this plugin. The 51 | default is as follows and can be customized in your |vimrc| if necessary: > 52 | let g:marked_filetypes = ["markdown", "mkd", "ghmarkdown", "vimwiki"] 53 | < 54 | *g:marked_auto_quit* 55 | 56 | If true, quit Marked when Vim exits. Default is true. To disable, add the 57 | following to your |vimrc|: > 58 | let g:marked_auto_quit = 0 59 | < 60 | *g:marked_app* 61 | 62 | The Marked application name. By default this is "Marked 2". If your version of 63 | Marked 2 doesn't open with `open -a "Marked 2"`, you can specify a different 64 | name or path to the application in your |vimrc|: > 65 | let g:marked_app = "/Applications/Setapp/Marked 2.app" 66 | < 67 | Note that this plugin requires Marked 2. If you are still using Marked 1, you 68 | can use the 1.0.0 release of this plugin (see |marked-about|). If you still 69 | have *g:marked_app* set to "Marked", you should update it to "Marked 2" or 70 | remove it from your |vimrc|. 71 | 72 | ============================================================================== 73 | ABOUT *marked-about* 74 | 75 | Grab the latest version or report a bug on Github: 76 | 77 | https://github.com/itspriddle/vim-marked 78 | 79 | Version 2.0.0 of this plugin removes support for Marked 1. If you are still 80 | using that version, you can use the 1.0.0 release of this plugin: 81 | 82 | https://github.com/itspriddle/vim-marked/releases/tag/v1.0.0 83 | 84 | ============================================================================== 85 | LICENSE *marked-license* 86 | 87 | MIT License 88 | 89 | https://github.com/itspriddle/vim-marked/blob/master/LICENSE 90 | 91 | ============================================================================== 92 | vim:tw=78:et:ft=help:norl: 93 | -------------------------------------------------------------------------------- /plugin/marked.vim: -------------------------------------------------------------------------------- 1 | " marked.vim 2 | " Author: Joshua Priddle 3 | " URL: https://github.com/itspriddle/vim-marked 4 | " Version: 2.0.0-beta 5 | " License: MIT 6 | 7 | " Don't do anything if we're not on macOS. 8 | if &cp || (exists("g:marked_loaded") && g:marked_loaded) || !executable("osascript") 9 | finish 10 | endif 11 | 12 | let g:marked_loaded = 1 13 | let s:save_cpo = &cpo 14 | set cpo&vim 15 | 16 | let g:marked_app = get(g:, "marked_app", "Marked 2") 17 | let g:marked_filetypes = get(g:, "marked_filetypes", ["markdown", "mkd", "ghmarkdown", "vimwiki"]) 18 | 19 | let s:open_documents = [] 20 | 21 | function! s:AddDocument(path) abort 22 | if index(s:open_documents, a:path) < 0 23 | call add(s:open_documents, a:path) 24 | endif 25 | endfunction 26 | 27 | function! s:RemoveDocument(path) abort 28 | unlet! s:open_documents[index(s:open_documents, a:path)] 29 | endfunction 30 | 31 | function! s:MarkedOpenURI(background, command, args) abort 32 | let uri = "x-marked://" . a:command 33 | 34 | if !empty(a:args) 35 | let uri .= "?" . join(map(items(a:args), 'printf("%s=%s", v:val[0], s:url_encode(v:val[1]))'), "&") 36 | endif 37 | 38 | execute printf("silent !open %s %s", (a:background ? "-g" : ""), shellescape(uri, 1)) 39 | endfunction 40 | 41 | function! s:MarkedOpen(background, path) abort 42 | if s:MarkedVersionCheck() == 0 43 | return 44 | endif 45 | 46 | call s:AddDocument(a:path) 47 | call s:MarkedOpenURI(a:background, "open", { "file": a:path }) 48 | 49 | redraw! 50 | endfunction 51 | 52 | function! s:MarkedQuit(force, path) abort 53 | if s:MarkedVersionCheck() == 0 54 | return 55 | endif 56 | 57 | if a:force 58 | let s:open_documents = [] 59 | call s:RunApplescript("quit") 60 | else 61 | call s:RemoveDocument(a:path) 62 | call s:RunApplescript("closeDocument", a:path) 63 | endif 64 | 65 | redraw! 66 | endfunction 67 | 68 | function! s:MarkedPreview(background, line1, line2) abort 69 | if s:MarkedVersionCheck() == 0 70 | return 71 | endif 72 | 73 | let lines = getline(a:line1, a:line2) 74 | 75 | call s:MarkedOpenURI(a:background, "preview", { "text": join(lines, "\n") }) 76 | endfunction 77 | 78 | let s:js =<< trim JS 79 | function run(argv) { 80 | let appId = argv[0], action = argv[1], path = argv[2]; 81 | 82 | try { 83 | var app = Application(appId); 84 | } catch (e) { 85 | return `error:Couldn't find Marked 2 application.`; 86 | } 87 | 88 | if (action == "version") return app.version(); 89 | 90 | if (!app.running()) return; 91 | 92 | if (action == "quit") { 93 | app.quit(); 94 | } else if (action == "closeDocument") { 95 | let doc = app.documents().find((d) => d.path() == path) 96 | 97 | if (doc) { 98 | doc.close(); 99 | if (app.documents().length == 0) app.quit(); 100 | } 101 | } 102 | } 103 | JS 104 | 105 | function! s:RunApplescript(...) abort 106 | let app = get(g:, "marked_app", "Marked 2") 107 | 108 | let args = join(map([app] + copy(a:000), "shellescape(v:val, 1)"), " ") 109 | 110 | silent return trim(system("osascript -l JavaScript - " . args, s:js)) 111 | endfunction 112 | 113 | " From the legend 114 | " https://github.com/tpope/vim-unimpaired/blob/5694455/plugin/unimpaired.vim#L369-L372 115 | function! s:url_encode(str) abort 116 | return substitute(iconv(a:str, "latin1", "utf-8"), "[^A-Za-z0-9_.~-]", '\="%".printf("%02X", char2nr(submatch(0)))', "g") 117 | endfunction 118 | 119 | function! s:MarkedToggle(background_or_force_quit, path) abort 120 | if index(s:open_documents, a:path) < 0 121 | call s:MarkedOpen(a:background_or_force_quit, a:path) 122 | else 123 | call s:MarkedQuit(a:background_or_force_quit, a:path) 124 | endif 125 | endfunction 126 | 127 | function! s:MarkedQuitVimLeave() abort 128 | if get(g:, "marked_auto_quit", 1) 129 | for document in s:open_documents 130 | call s:MarkedQuit(0, document) 131 | endfor 132 | endif 133 | endfunction 134 | 135 | function! s:MarkedVersionCheck() abort 136 | if !exists("s:marked_version") 137 | let s:marked_version = s:RunApplescript("version") 138 | endif 139 | 140 | if s:marked_version =~ "^1" 141 | echohl WarningMsg 142 | echomsg "marked.vim requires Marked 2 but you've configured it to use Marked 1." 143 | echomsg "See `:help g:marked_app` for more information." 144 | echohl None 145 | elseif s:marked_version !~ "^2" 146 | echohl WarningMsg 147 | echomsg "This plugin requires Marked 2." 148 | echomsg "Visit https://marked2app.com to download it." 149 | echohl None 150 | end 151 | 152 | return s:marked_version =~ "^2" 153 | endfunction 154 | 155 | function! s:RegisterCommands(filetype) abort 156 | if index(g:marked_filetypes, a:filetype) >= 0 157 | command! -buffer -bang MarkedOpen call s:MarkedOpen(0, expand('%:p')) 158 | command! -buffer -bang MarkedQuit call s:MarkedQuit(0, expand('%:p')) 159 | command! -buffer -bang MarkedToggle call s:MarkedToggle(0, expand('%:p')) 160 | command! -buffer -bang -range=% MarkedPreview call s:MarkedPreview(0, , ) 161 | 162 | let b:undo_ftplugin = get(b:, "undo_ftplugin", "exe") . 163 | \ "| delc MarkedOpen | delc MarkedQuit | delc MarkedToggle | delc MarkedPreview" 164 | endif 165 | endfunction 166 | 167 | augroup marked_commands 168 | autocmd! 169 | autocmd VimLeavePre * call s:MarkedQuitVimLeave() 170 | autocmd FileType * call s:RegisterCommands(expand("")) 171 | augroup END 172 | 173 | let &cpo = s:save_cpo 174 | unlet s:save_cpo 175 | 176 | " vim:ft=vim:fdm=marker:ts=2:sw=2:sts=2:et 177 | --------------------------------------------------------------------------------