Skip to content
This repository
Browse code

Support for non-Unicode encodings (requested by SteP)

When the encoding option is set to something other than UTF-8 the
plug-in will no longer try to insert curly quotes, dashes or bullets in
the user's notes and special characters in shadow notes will be
transcoded on the fly to their ASCII equivalents.
  • Loading branch information...
commit 6b7566e56d993abd0977e7271e63244bf9a9616e 1 parent 28514cb
Peter Odding authored May 22, 2011
48  autoload/xolox/notes.vim
@@ -22,6 +22,9 @@ function! xolox#notes#edit(bang, title) abort " {{{1
22 22
     let fname = xolox#notes#select(title)
23 23
     if fname != ''
24 24
       execute 'edit' . a:bang fnameescape(fname)
  25
+      if !xolox#notes#unicode_enabled() && xolox#misc#path#equals(fnamemodify(fname, ':h'), g:notes_shadowdir)
  26
+        call s:transcode_utf8_latin1()
  27
+      endif
25 28
       setlocal filetype=notes
26 29
       call xolox#misc#timer#stop('%s: Opened note in %s.', s:script, starttime)
27 30
       return
@@ -36,6 +39,9 @@ function! xolox#notes#edit(bang, title) abort " {{{1
36 39
     let fname = xolox#misc#path#merge(g:notes_shadowdir, 'New note')
37 40
     execute 'silent read' fnameescape(fname)
38 41
     1delete
  42
+    if !xolox#notes#unicode_enabled()
  43
+      call s:transcode_utf8_latin1()
  44
+    endif
39 45
     setlocal nomodified
40 46
   endif
41 47
   if title != 'New note'
@@ -45,6 +51,31 @@ function! xolox#notes#edit(bang, title) abort " {{{1
45 51
   call xolox#misc#timer#stop('%s: Started new note in %s.', s:script, starttime)
46 52
 endfunction
47 53
 
  54
+function! xolox#notes#edit_shadow() " {{{1
  55
+  " People using latin1 don't like the UTF-8 curly quotes and bullets used in
  56
+  " the predefined notes because there are no equivalent characters in latin1,
  57
+  " resulting in the characters being shown as garbage or a question mark.
  58
+  execute 'edit' fnameescape(expand('<amatch>'))
  59
+  if !xolox#notes#unicode_enabled()
  60
+    call s:transcode_utf8_latin1()
  61
+  endif
  62
+  setlocal filetype=notes
  63
+endfunction
  64
+
  65
+function! xolox#notes#unicode_enabled()
  66
+  return &encoding == 'utf-8'
  67
+endfunction
  68
+
  69
+function! s:transcode_utf8_latin1()
  70
+  let view = winsaveview()
  71
+  silent %s/\%xe2\%x80\%x98/`/eg
  72
+  silent %s/\%xe2\%x80\%x99/'/eg
  73
+  silent %s/\%xe2\%x80[\x9c\x9d]/"/eg
  74
+  silent %s/\%xe2\%x80\%xa2/\*/eg
  75
+  setlocal nomodified
  76
+  call winrestview(view)
  77
+endfunction
  78
+
48 79
 function! xolox#notes#select(filter) " {{{1
49 80
   " Interactively select an existing note whose title contains {filter}.
50 81
   let notes = {}
@@ -425,13 +456,22 @@ function! xolox#notes#insert_quote(style) " {{{3
425 456
   " XXX When I pass the below string constants as arguments from the file type
426 457
   " plug-in the resulting strings contain mojibake (UTF-8 interpreted as
427 458
   " latin1?) even if both scripts contain a UTF-8 BOM! Maybe a bug in Vim?!
428  
-  let [open_quote, close_quote] = a:style == 1 ? ['‘', '’'] : ['“', '”']
  459
+  if xolox#notes#unicode_enabled()
  460
+    let [open_quote, close_quote] = a:style == 1 ? ['‘', '’'] : ['“', '”']
  461
+  else
  462
+    let [open_quote, close_quote] = a:style == 1 ? ['`', "'"] : ['"', '"']
  463
+  endif
429 464
   return getline('.')[col('.')-2] =~ '\S$' ? close_quote : open_quote
430 465
 endfunction
431 466
 
432  
-function! xolox#notes#insert_bullet(c) " {{{3
  467
+function! xolox#notes#insert_bullet(chr) " {{{3
433 468
   " Insert a UTF-8 list bullet when the user types "*".
434  
-  return getline('.')[0 : max([0, col('.') - 2])] =~ '^\s*$' ? '•' : a:c
  469
+  if xolox#notes#unicode_enabled()
  470
+    if getline('.')[0 : max([0, col('.') - 2])] =~ '^\s*$'
  471
+      return '•'
  472
+    endif
  473
+  endif
  474
+  return a:chr
435 475
 endfunction
436 476
 
437 477
 function! xolox#notes#indent_list(command, line1, line2) " {{{3
@@ -440,7 +480,7 @@ function! xolox#notes#indent_list(command, line1, line2) " {{{3
440 480
     call setline(a:line1, repeat(' ', &tabstop))
441 481
   else
442 482
     execute a:line1 . ',' . a:line2 . 'normal' a:command
443  
-    if getline('.') =~ '$'
  483
+    if getline('.') =~ '\(•\|\*\)$'
444 484
       call setline('.', getline('.') . ' ')
445 485
     endif
446 486
   endif
26  ftplugin/notes.vim
... ...
@@ -1,6 +1,6 @@
1 1
 " Vim file type plug-in
2 2
 " Author: Peter Odding <peter@peterodding.com>
3  
-" Last Change: January 7, 2011
  3
+" Last Change: May 22, 2011
4 4
 " URL: http://peterodding.com/code/vim/notes/
5 5
 
6 6
 if exists('b:did_ftplugin')
@@ -22,7 +22,11 @@ setlocal tabstop=3 shiftwidth=3 expandtab
22 22
 let b:undo_ftplugin .= ' tabstop< shiftwidth< expandtab<'
23 23
 
24 24
 " Automatic formatting for bulleted lists. {{{1
25  
-let &l:comments = ': • ,:> '
  25
+if xolox#notes#unicode_enabled()
  26
+  let &l:comments = ': • ,: * ,:> '
  27
+else
  28
+  let &l:comments = ': * ,:> '
  29
+endif
26 30
 let &l:commentstring = '> %s'
27 31
 setlocal formatoptions=tcron
28 32
 let b:undo_ftplugin .= ' commentstring< comments< formatoptions<'
@@ -44,8 +48,10 @@ setlocal includeexpr=xolox#notes#include_expr(v:fname)
44 48
 let b:undo_ftplugin .= ' includeexpr<'
45 49
 
46 50
 " Change double-dash to em-dash as it is typed. {{{1
47  
-imap <buffer> -- —
48  
-let b:undo_ftplugin .= ' | execute "iunmap <buffer> --"'
  51
+if xolox#notes#unicode_enabled()
  52
+  imap <buffer> -- —
  53
+  let b:undo_ftplugin .= ' | execute "iunmap <buffer> --"'
  54
+endif
49 55
 
50 56
 " Change plain quotes to curly quotes as they're typed. {{{1
51 57
 imap <buffer> <expr> ' xolox#notes#insert_quote(1)
@@ -54,10 +60,12 @@ let b:undo_ftplugin .= ' | execute "iunmap <buffer> ''"'
54 60
 let b:undo_ftplugin .= ' | execute ''iunmap <buffer> "'''
55 61
 
56 62
 " Change ASCII style arrows to Unicode arrows. {{{1
57  
-imap <buffer> -> →
58  
-imap <buffer> <- ←
59  
-let b:undo_ftplugin .= ' | execute "iunmap <buffer> ->"'
60  
-let b:undo_ftplugin .= ' | execute "iunmap <buffer> <-"'
  63
+if xolox#notes#unicode_enabled()
  64
+  imap <buffer> -> →
  65
+  imap <buffer> <- ←
  66
+  let b:undo_ftplugin .= ' | execute "iunmap <buffer> ->"'
  67
+  let b:undo_ftplugin .= ' | execute "iunmap <buffer> <-"'
  68
+endif
61 69
 
62 70
 " Convert ASCII list bullets to Unicode bullets. {{{1
63 71
 imap <buffer> <expr> - xolox#notes#insert_bullet('-')
@@ -68,12 +76,10 @@ let b:undo_ftplugin .= ' | execute "iunmap <buffer> +"'
68 76
 let b:undo_ftplugin .= ' | execute "iunmap <buffer> *"'
69 77
 
70 78
 " Indent list items using <Tab>. {{{1
71  
-
72 79
 imap <buffer> <silent> <Tab> <C-o>:call xolox#notes#indent_list('>>', line('.'), line('.'))<CR>
73 80
 smap <buffer> <silent> <Tab> <C-o>:<C-u>call xolox#notes#indent_list('>>', line("'<"), line("'>"))<CR><C-o>gv
74 81
 let b:undo_ftplugin .= ' | execute "iunmap <buffer> <Tab>"'
75 82
 let b:undo_ftplugin .= ' | execute "sunmap <buffer> <Tab>"'
76  
-
77 83
 imap <buffer> <silent> <S-Tab> <C-o>:call xolox#notes#indent_list('<<', line('.'), line('.'))<CR>
78 84
 smap <buffer> <silent> <S-Tab> <C-o>:<C-u>call xolox#notes#indent_list('<<', line("'<"), line("'>"))<CR><C-o>gv
79 85
 let b:undo_ftplugin .= ' | execute "iunmap <buffer> <S-Tab>"'
6  misc/notes/shadow/Note taking syntax
@@ -7,7 +7,7 @@ notes plug-in.
7 7
 
8 8
 Lines prefixed with one or more ‘#’ symbols are headings which can be used for
9 9
 automatic text folding. There’s also an alternative heading format which isn’t
10  
-folded, it consists of a line shorten than 60 letters that starts with an
  10
+folded, it consists of a line shorter than 60 letters that starts with an
11 11
 uppercase letter and ends in a colon (the hard wrapping in this paragraph
12 12
 illustrates why the “starts with uppercase” rule is needed):
13 13
 
@@ -26,7 +26,7 @@ Hyper links and such:
26 26
 
27 27
 # Lists
28 28
 
29  
-Bulleted lists can be used for to do lists:
  29
+Bulleted lists can be used for to-do lists:
30 30
  • DONE Publish my notes.vim plug-in
31 31
  • TODO Write an indent script for atx headings
32 32
  • XXX This one is really important
@@ -39,7 +39,7 @@ Numbered lists are also supported:
39 39
 # Block quotes
40 40
 
41 41
 > Quotes are written using
42  
-> the convention from e-mail 
  42
+> the convention from e-mail
43 43
 
44 44
 # Embedded syntax highlighting
45 45
 
3  plugin/notes.vim
@@ -3,7 +3,7 @@
3 3
 " Last Change: May 22, 2011
4 4
 " URL: http://peterodding.com/code/vim/notes/
5 5
 " License: MIT
6  
-" Version: 0.8.6
  6
+" Version: 0.8.7
7 7
 
8 8
 " Support for automatic update using the GLVS plug-in.
9 9
 " GetLatestVimScripts: 3375 1 :AutoInstall: notes.zip
@@ -65,6 +65,7 @@ augroup PluginNotes
65 65
   " NB: "nested" is used here so that SwapExists automatic commands apply
66 66
   " to notes (which is IMHO better than always showing the E325 prompt).
67 67
   au BufReadCmd note:* nested call xolox#notes#shortcut()
  68
+  call s:DAC('BufReadCmd', g:notes_shadowdir, 'call xolox#notes#edit_shadow()')
68 69
   call s:DAC('BufWriteCmd', g:notes_directory, 'call xolox#notes#save()')
69 70
   au SwapExists * call xolox#notes#swaphack()
70 71
   au WinEnter * if &ft == 'notes' | call xolox#notes#highlight_names(0) | endif
10  syntax/notes.vim
... ...
@@ -1,6 +1,6 @@
1 1
 " Vim syntax script
2 2
 " Author: Peter Odding <peter@peterodding.com>
3  
-" Last Change: March 25, 2011
  3
+" Last Change: May 22, 2011
4 4
 " URL: http://peterodding.com/code/vim/notes/
5 5
 
6 6
 " Note: This file is encoded in UTF-8 including a byte order mark so
@@ -28,13 +28,17 @@ syntax cluster notesInline add=notesName
28 28
 highlight def link notesName Underlined
29 29
 
30 30
 " Highlight list bullets and numbers. {{{2
31  
-syntax match notesListBullet /^\s*\zs/
  31
+syntax match notesListBullet /^\s*\zs\(•\|\*\)/
32 32
 highlight def link notesListBullet Comment
33 33
 syntax match notesListNumber /^\s*\zs\d\+[[:punct:]]\?\ze\s/
34 34
 highlight def link notesListNumber Comment
35 35
 
36 36
 " Highlight quoted fragments (inside single quotes). {{{2
37  
-syntax match notesQuotedFragment /‘.\{-}’/
  37
+if xolox#notes#unicode_enabled()
  38
+  syntax match notesQuotedFragment /‘.\{-}’/
  39
+else
  40
+  syntax match notesQuotedFragment /`.\{-}'/
  41
+endif
38 42
 highlight def link notesQuotedFragment Special
39 43
 
40 44
 " Highlight text emphasized in italic font. {{{2

0 notes on commit 6b7566e

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