Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Differentiate between full and partial writing mode. Add write_auto.

  • Loading branch information...
commit ef7d23db1129d45872913c233416b26497b5d7fa 1 parent 831d12b
Nate authored October 04, 2012
34  autoload/write.vim
@@ -5,9 +5,16 @@ let g:write#autoloaded = 1
5 5
 
6 6
 
7 7
 " Sets the buffer as writer-friendly.
8  
-function! write#On()
9  
-	let b:writing=1
10  
-	setlocal spell wrap linebreak display+=lastline nonu nornu nolist
  8
+" Args:
  9
+"  {string} bang Whether or not to force full writing mode.
  10
+function! write#On(bang)
  11
+	setlocal spell wrap display+=lastline nolist linebreak
  12
+	if a:bang != '' || &tw == 0
  13
+		setlocal tw=0 nonu nornu
  14
+		let b:writing=2
  15
+	else
  16
+		let b:writing=1
  17
+	endif
11 18
 	noremap  <buffer> <silent> k gk
12 19
 	noremap  <buffer> <silent> j gj
13 20
 endfunction
@@ -16,19 +23,34 @@ endfunction
16 23
 " Unsets the buffer as writer-friendly.
17 24
 " Will only undo changes that write.vim made.
18 25
 function! write#Off()
  26
+	if b:writing == 2
  27
+		setlocal tw< nonu< nornu<
  28
+	endif
  29
+	setlocal spell< wrap< display< list< linebreak<
19 30
 	let b:writing=0
20  
-	setlocal spell< wrap< linebreak< display< nu< rnu< list<
21 31
 	silent! unmap <buffer> k
22 32
 	silent! unmap <buffer> j
23 33
 endfunction
24 34
 
25 35
 
26 36
 " Toggles write mode.
27  
-function! write#Toggle()
  37
+" Args:
  38
+"   {string} bang Whether or not to force full writing mode.
  39
+function! write#Toggle(bang)
28 40
 	if exists('b:writing') && b:writing > 0
29 41
 		call write#Off()
30 42
 	else
31  
-		call write#On()
  43
+		call write#On(bang)
  44
+	endif
  45
+endfunction
  46
+
  47
+
  48
+" Toggles writing mode only if writing has never been set for this buffer
  49
+" Args:
  50
+"   {string} bang Whether or not to force full writing mode.
  51
+function! write#Reenter(bang)
  52
+	if !exists('b:writing')
  53
+		call write#On(a:bang)
32 54
 	endif
33 55
 endfunction
34 56
 
70  doc/write.txt
... ...
@@ -1,45 +1,87 @@
1 1
 *write.txt*  Get your author on.
  2
+*write* *write.vim*
2 3
 
3 4
 Author:  Nate Soares <http://so8r.es/>
4 5
 License: Same terms as Vim itself (see |license|)
5 6
 
  7
+=============================================================================
  8
+CONTENTS                                        *write-contents*
  9
+	    1. Introduction................................|write-intro|
  10
+	    2. Configuration...............................|write-config|
  11
+	    3. Commands....................................|write-commands|
  12
+	    4. Extras......................................|write-extras|
  13
+	    5. About.......................................|write-about|
  14
+
6 15
 ==============================================================================
7 16
 INTRODUCTION                                    *write*
8 17
 
9 18
 Vim writing mode, for writing non-code text (books, novels, etc.).
10 19
 
  20
+==============================================================================
  21
+CONFIGURATION                                   *write-commands*
  22
+
  23
+						*g:write_auto*
  24
+A list of filetypes that should automatically go into write mode. If the
  25
+filetype string ends with a '!', that filetype will automatically go into full
  26
+write mode. For example: >
  27
+	let g:write_auto=['markdown', 'help', 'text!']
  28
+<
  29
+Will set up autocommands on markdown, help, and text files to put them into
  30
+write mode, and text files will be forced into full write mode regardless of
  31
+|textwidth| settings.
  32
+
11 33
 
12 34
 ==============================================================================
13 35
 COMMANDS                                        *write-commands*
14 36
 
15  
-						*:WriteOn*
16  
-Turn writing mode on. This does the following:
17  
-- soft wrap lines
18  
-- make j & k navigate screenwise, not linewise
19  
-- turn spelling on
20  
-- turn line numbers off
21  
-As well as a few other tweaks that make writing easier.
  37
+						*:WriteOn[!]*
  38
+Turn writing mode on. There are two writing mode styles: full and partial.
  39
+
  40
+Partial writing mode turns on spell checking and a number of enhancements that
  41
+make writing mode more comfortable (line wrapping, intuitive cursor movement
  42
+over long lines, some display tweaks for long paragraphs, etc.).
  43
+
  44
+Full writing mode sets all of the same enhancements as partial mode and also
  45
+turns off line numbers and hard text wrapping.
  46
+
  47
+Write.vim tries to autodetect your writing style by checking the |textwidth|
  48
+setting. If you |textwidth| is nonzero then Write.vim assumes that you want to
  49
+hard wrap your lines and goes into partial writing mode. To force full writing
  50
+mode, use >
  51
+	:WriteOn!
  52
+<
  53
+If you want |textwidth| automatically managed by filetype, check out the
  54
+|longlines| plugin at <http://github.com/Soares/longlines.vim>.
22 55
 
23 56
 						*:WriteOff*
24  
-Turn writing mode off. Resets all values changed by :WriteOn (which are
25  
-changed locally) to the global values.
  57
+Turn writing mode off. Resets all values changed by :WriteOn.
26 58
 
27  
-						*:Write*
28  
-Toggles writing mode.
  59
+NOTE: Writing mode makes all setting changes locally. When turning writing
  60
+mode off, settings will be reset to their global values. If you have local
  61
+settings that interfere with writer (i.e. you have ':set nospell' and
  62
+':setlocal spell') then write mode will clobber those local settings.
29 63
 
  64
+						*:Write[!]*
  65
+Toggles writing mode. The bang, if given, will be passed to |:WriteOn|
30 66
 
31 67
 ==============================================================================
32 68
 EXTRAS                                          *write-extras*
33 69
 
34  
-                                                *write#Writing*
35  
-True if writing mode is on. Useful for statusline flags. We recommend adding
36  
-something like >
  70
+Status Line                                     *write#Writing*
  71
+						*write-statusline*
  72
+Returns non-zero if writing mode is on. Useful for statusline flags. We
  73
+recommend adding something like >
37 74
 	set statusline+=%#ModeMsg#
38 75
 	set statusline+=%{write#Writing()?'[W]':''}
39 76
 	set statusline+=%*
40 77
 <
41 78
 to your statusline.
42 79
 
  80
+Mappings                                        *write-mappings*
  81
+Write.vim does not provide mappings for you. You'll have to set them yourself.
  82
+We suggest something along the lines of: >
  83
+	noremap <leader>W :Write<CR>
  84
+<
43 85
 
44 86
 ==============================================================================
45 87
 ABOUT						*write-about*
23  plugin/write.vim
@@ -5,11 +5,28 @@
5 5
 " License:      The same as vim itself. (See |license|)
6 6
 " GetLatestVimScripts: 4249 1 :AutoInstall: write.zip
7 7
 
8  
-if exists("g:loaded_write") || &cp || v:version < 700
  8
+if exists('g:loaded_write') || &cp || v:version < 700
9 9
 	finish
10 10
 endif
11 11
 let g:loaded_write = 1
12 12
 
13  
-command! WriteOn call write#On()
  13
+
  14
+command! -bang WriteOn call write#On('<bang>')
14 15
 command! WriteOff call write#Off()
15  
-command! Write call write#Toggle()
  16
+command! -bang Write call write#Toggle('<bang>')
  17
+
  18
+
  19
+if !exists('g:write_auto')
  20
+	let g:write_auto = []
  21
+endif
  22
+
  23
+if len(g:write_auto) > 0
  24
+	augroup write
  25
+		autocmd!
  26
+		for s:ft in g:write_auto
  27
+			let s:bang = s:ft =~# '!$' ? "'!'" : "''"
  28
+			let s:ft = substitute(s:ft, '!$', '', '')
  29
+			exe 'autocmd FileType '.s:ft.' call write#Reenter('.s:bang.')'
  30
+		endfor
  31
+	augroup end
  32
+endif

0 notes on commit ef7d23d

Please sign in to comment.
Something went wrong with that request. Please try again.