Skip to content

Commit

Permalink
Add :NoteFromSelectedText command (suggested by Lukas Muehlethaler)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Jun 7, 2011
1 parent 677cd70 commit 26ab4f4
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -3,6 +3,7 @@
The notes.vim plug-in for the [Vim text editor] [vim] makes it easy to manage your notes in Vim:

* **Starting a new note:** Execute the `:Note` command to create a new buffer and load the appropriate file type and syntax
* You can also start a new note with the selected text as title using the `:NoteFromSelectedText` command
* **Saving notes:** Just use Vim's [:write] [write] and [:update] [update] commands, you don't need to provide a filename because it will be set based on the title (first line) of your note (you also don't need to worry about special characters, they'll be escaped)
* **Editing existing notes:** Execute `:Note anything` to edit a note containing `anything` in its title (if no notes are found a new one is created with its title set to `anything`)
* **Deleting notes:** The `:DeleteNote` command enables you to delete the current note
Expand Down
21 changes: 20 additions & 1 deletion autoload/xolox/notes.vim
@@ -1,6 +1,6 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: June 4, 2011
" Last Change: June 7, 2011
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
Expand Down Expand Up @@ -51,6 +51,25 @@ function! xolox#notes#edit(bang, title) abort " {{{1
call xolox#misc#timer#stop('%s: Started new note in %s.', s:script, starttime)
endfunction

function! xolox#notes#from_selection(bang) " {{{1
" TODO This will always open a new buffer in the current window which I
" don't consider very friendly (because the user loses his/her context),
" but choosing to always split the window doesn't seem right either...
call xolox#notes#edit(a:bang, s:get_visual_selection())
endfunction

function! s:get_visual_selection()
" Why is this not a built-in Vim script function?! See also the question at
" http://stackoverflow.com/questions/1533565 but note that none of the code
" posted there worked for me so I wrote this function.
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][: col2 - 2]
let lines[0] = lines[0][col1 - 1:]
return join(lines, ' ')
endfunction

function! xolox#notes#edit_shadow() " {{{1
" People using latin1 don't like the UTF-8 curly quotes and bullets used in
" the predefined notes because there are no equivalent characters in latin1,
Expand Down
3 changes: 3 additions & 0 deletions doc/notes.txt
Expand Up @@ -6,6 +6,9 @@ notes in Vim:
- Starting a new note: Execute the ':Note' command to create a new buffer and
load the appropriate file type and syntax

- You can also start a new note with the selected text as title using the
':NoteFromSelectedText' command

- Saving notes: Just use Vim's |:write| and |:update| commands, you don't need to
provide a filename because it will be set based on the title (first line)
of your note (you also don't need to worry about special characters,
Expand Down
8 changes: 8 additions & 0 deletions misc/notes/shadow/Note taking commands
Expand Up @@ -18,6 +18,14 @@ created with its title set to the text you passed to :Note. This command will
fail when changes have been made to the current buffer, unless you use :Note!
which discards any changes.

To start a new note and use the currently selected text as the title for the
note you can use the :NoteFromSelectedText command. The name of this command
isn’t very well suited to daily use, however the idea is that users will define
their own mapping to invoke this command. For example:
{{{vim
" Map \ns in visual mode to start new note with selected text as title.
vmap <Leader>ns :NoteFromSelectedText<CR>
}}}
# :DeleteNote deletes the current note

The :DeleteNote command deletes the current note, destroys the buffer and
Expand Down
5 changes: 3 additions & 2 deletions plugin/notes.vim
@@ -1,9 +1,9 @@
" Vim plug-in
" Author: Peter Odding <peter@peterodding.com>
" Last Change: June 4, 2011
" Last Change: June 7, 2011
" URL: http://peterodding.com/code/vim/notes/
" License: MIT
" Version: 0.8.8
" Version: 0.8.9

" Support for automatic update using the GLVS plug-in.
" GetLatestVimScripts: 3375 1 :AutoInstall: notes.zip
Expand Down Expand Up @@ -38,6 +38,7 @@ endif

" User commands to create, delete and search notes.
command! -bar -bang -nargs=? -complete=customlist,xolox#notes#complete Note call xolox#notes#edit(<q-bang>, <q-args>)
command! -bar -bang -range NoteFromSelectedText call xolox#notes#from_selection(<q-bang>)
command! -bar -bang DeleteNote call xolox#notes#delete(<q-bang>)
command! -bar -bang -nargs=? SearchNotes call xolox#notes#search(<q-bang>, <q-args>)
command! -bar -bang RelatedNotes call xolox#notes#related(<q-bang>)
Expand Down

0 comments on commit 26ab4f4

Please sign in to comment.