From c241f633682fac28cb8583ebdf661422ff1a73b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98ystein=20Walle?= Date: Tue, 20 Mar 2012 18:31:37 +0100 Subject: [PATCH] Initial commit --- README.md | 9 +++++ doc/double-tap.txt | 91 +++++++++++++++++++++++++++++++++++++++++++ plugin/double-tap.vim | 65 +++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 README.md create mode 100644 doc/double-tap.txt create mode 100644 plugin/double-tap.vim diff --git a/README.md b/README.md new file mode 100644 index 0000000..4b15399 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Double-tap +========== +Øystein Walle + +From time to time people ask on `#vim` on Freenode how they can disable +automatic insertion of comments when they open a new line. I think this is often +very handy when you're writing a longer, multi-line comment. How do we get the +best of both worlds? Double-tap! + diff --git a/doc/double-tap.txt b/doc/double-tap.txt new file mode 100644 index 0000000..8e3f43f --- /dev/null +++ b/doc/double-tap.txt @@ -0,0 +1,91 @@ +*doubletap.txt* For Vim version 7.3 Last change: 2012 Mar 20 + + 888 888 888 + 888 888 888 + 888 888 888 + 88888b. 8888b. 88888b. .d88b. 88888b. 8888b. 88888b. .d88b. 888 + 888 "88b "88b 888 "88b d88P"88b 888 "88b "88b 888 "88b d88P"88b 888 + 888 888 .d888888 888 888 888 888 888 888 .d888888 888 888 888 888 Y8P +888 d88P 888 888 888 888 Y88b 888 888 d88P 888 888 888 888 Y88b 888 " +88888P" "Y888888 888 888 "Y88888 88888P" "Y888888 888 888 "Y88888 888 + 888 888 + Y8b d88P Y8b d88P + Double-tap! "Y88P" "Y88P" + + *doubletap-toc* + +1 Introduction |doubletap-intro| +2 Requirements |doubletap-reqs| +3 Trivia |doubletap-trivia| +4 Contributing |doubletap-contrib| + +The functionality mentioned here is a plugin, see |add-plugin|. You can +avoid loading this plugin by setting the "loaded_doubletap" global +variable in your |vimrc| file: +> + :let g:loaded_doubletap = 1 +< +Since you are most likely using some plugin to manage your plugins there is +probably an even easier way. + +=============================================================================== +1 Introduction *doubletap-intro* + +From time to time people ask on #vim on FreeNode how they can disable automatic +insertion of comments when they open a new line. For example, in the following +situation, where | marks the cursor: +> + # This is a dummy comment for the + # sake of giving an example.| +< +pressing || will insert comments like this: +> + # This is a dummy comment for the + # sake of giving an example. + # | +< +This can be very handy, but the times it isn't it is extremely annoying. +Double-Tap fixes this once and for all. Continuing the example above, pressing +|Enter| one more time will yield the following: +> + # This is a dummy comment for the + # sake of giving an example. + | +< +So if you want to open a new line but don't what a new comment line, +double-tapping the |Enter| key will fix it for you! Pressing |Enter| after +opening a new line with |o| or |O| from normal mode has the same effect. + +=============================================================================== +2 Requirements *doubletap-reqs* + +Double-tap requires |+comments|. + +To find out whether it should clear the line or simply deliver the |Enter| +keystroke as usual, Double-tap checks the 'comments' setting. Specifically it +finds the item in the 'comments' setting where the {flags} portion is empty. If +it cannot find it, a regular |Enter| is delivered every time. + +If it is found, Double-tap has no way of knowing if it's actually correct. Thus +you are encouraged to either set the 'comments' setting appropriately for the +filetype of your choice if Double-tap behaves badly, or not use this plugin +(see |doubletap-trivia|). + +=============================================================================== +3 Trivia *doubletap-trivia* + +Did you know that pressing |i_CTRL-U| after pressing |Enter|, |o| or |O| in the +situation above would have literally the exact same effect? (If you don't +believe me, check the source!) Still, double-tapping is easier than some +weird Ctrl-combo. + +=============================================================================== +4 Contributing *doubletap-contrib* + +To be honest, I don't envision anything more for this plugin than what it +already does. Ideas that would be fitting are most welcome since I cannot +think of anything myself. + +Bugs can be reported on the Github page: http://github.com/osse/double-tap + + vim:tw=78:ts=8:ft=help:norl: diff --git a/plugin/double-tap.vim b/plugin/double-tap.vim new file mode 100644 index 0000000..6f7074e --- /dev/null +++ b/plugin/double-tap.vim @@ -0,0 +1,65 @@ +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +" Double-Tap makes annoying comments go away +" Maintainer: Øystein Walle +" Version: 0.1 +" Description: Opening a new line from a comment may insert an unwanted +" comment; (re)pressing Enter will clear the line for you. This works if in +" insert mode and pressing Enter as well when using o or O in normal mode +" Last Change: Tue Mar 20 16:40:28 CET 2012 +" License: Vim License (see :help license) +" Location: plugin/double-tab.vim +" Website: https://github.com/osse/double-tap +" +" See double-tap.txt for help. This can be accessed by doing: +" +" :helptags ~/.vim/doc +" :help doubletap.txt +"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" +let s:doubletap_version = '0.1' + +" Vimscript Setup: {{{1 +" Allow use of line continuation. +let s:save_cpo = &cpo +set cpo&vim + +" We need version 7 for mappings +" as well as the user wanting nocompatible +if exists("g:loaded_doubletap") + \ !has("comments") + \ || v:version < 700 + \ || &compatible + let &cpo = s:save_cpo + finish +endif +let g:loaded_doubletap = 1 +" }}} + +let s:pattern = '^\(.*,\|^\):\([^,]\+\).*$' " This patterns finds the wanted + " item in 'comments' + +let s:commStart = {} " dict to hold the comment starters using + " the current filetype as key + +" This function returns a regular if the current line +" is not simply an empty comment. Otherwise it clears the line +function! Doubletap_detect_empty_comment() + " Captures the comment starter if necessary; only once per filetype + if !has_key(s:commStart, &ft) + let s:commStart[&ft] = substitute(&comments, s:pattern, '\2', '') + endif + let line = getline('.') + if line =~ '^\s*'. s:commStart[&ft] . '\s*$' + return "\" + else + return "\" + endif +endfunction + +inoremap Doubletap_detect_empty_comment() + +" Teardown:{{{1 +"reset &cpo back to users setting +let &cpo = s:save_cpo +" }}} + +" vim: set sw=2 sts=2 et fdm=marker: