Skip to content

Commit 6b7566e

Browse files
committed
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.
1 parent 28514cb commit 6b7566e

File tree

5 files changed

+72
-21
lines changed

5 files changed

+72
-21
lines changed

autoload/xolox/notes.vim

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ function! xolox#notes#edit(bang, title) abort " {{{1
2222
let fname = xolox#notes#select(title)
2323
if fname != ''
2424
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
2528
setlocal filetype=notes
2629
call xolox#misc#timer#stop('%s: Opened note in %s.', s:script, starttime)
2730
return
@@ -36,6 +39,9 @@ function! xolox#notes#edit(bang, title) abort " {{{1
3639
let fname = xolox#misc#path#merge(g:notes_shadowdir, 'New note')
3740
execute 'silent read' fnameescape(fname)
3841
1delete
42+
if !xolox#notes#unicode_enabled()
43+
call s:transcode_utf8_latin1()
44+
endif
3945
setlocal nomodified
4046
endif
4147
if title != 'New note'
@@ -45,6 +51,31 @@ function! xolox#notes#edit(bang, title) abort " {{{1
4551
call xolox#misc#timer#stop('%s: Started new note in %s.', s:script, starttime)
4652
endfunction
4753

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+
4879
function! xolox#notes#select(filter) " {{{1
4980
" Interactively select an existing note whose title contains {filter}.
5081
let notes = {}
@@ -425,13 +456,22 @@ function! xolox#notes#insert_quote(style) " {{{3
425456
" XXX When I pass the below string constants as arguments from the file type
426457
" plug-in the resulting strings contain mojibake (UTF-8 interpreted as
427458
" 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
429464
return getline('.')[col('.')-2] =~ '\S$' ? close_quote : open_quote
430465
endfunction
431466

432-
function! xolox#notes#insert_bullet(c) " {{{3
467+
function! xolox#notes#insert_bullet(chr) " {{{3
433468
" 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
435475
endfunction
436476

437477
function! xolox#notes#indent_list(command, line1, line2) " {{{3
@@ -440,7 +480,7 @@ function! xolox#notes#indent_list(command, line1, line2) " {{{3
440480
call setline(a:line1, repeat(' ', &tabstop))
441481
else
442482
execute a:line1 . ',' . a:line2 . 'normal' a:command
443-
if getline('.') =~ '$'
483+
if getline('.') =~ '\(•\|\*\)$'
444484
call setline('.', getline('.') . ' ')
445485
endif
446486
endif

ftplugin/notes.vim

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim file type plug-in
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: January 7, 2011
3+
" Last Change: May 22, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

66
if exists('b:did_ftplugin')
@@ -22,7 +22,11 @@ setlocal tabstop=3 shiftwidth=3 expandtab
2222
let b:undo_ftplugin .= ' tabstop< shiftwidth< expandtab<'
2323

2424
" 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
2630
let &l:commentstring = '> %s'
2731
setlocal formatoptions=tcron
2832
let b:undo_ftplugin .= ' commentstring< comments< formatoptions<'
@@ -44,8 +48,10 @@ setlocal includeexpr=xolox#notes#include_expr(v:fname)
4448
let b:undo_ftplugin .= ' includeexpr<'
4549

4650
" 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
4955

5056
" Change plain quotes to curly quotes as they're typed. {{{1
5157
imap <buffer> <expr> ' xolox#notes#insert_quote(1)
@@ -54,10 +60,12 @@ let b:undo_ftplugin .= ' | execute "iunmap <buffer> ''"'
5460
let b:undo_ftplugin .= ' | execute ''iunmap <buffer> "'''
5561

5662
" 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
6169

6270
" Convert ASCII list bullets to Unicode bullets. {{{1
6371
imap <buffer> <expr> - xolox#notes#insert_bullet('-')
@@ -68,12 +76,10 @@ let b:undo_ftplugin .= ' | execute "iunmap <buffer> +"'
6876
let b:undo_ftplugin .= ' | execute "iunmap <buffer> *"'
6977

7078
" Indent list items using <Tab>. {{{1
71-
7279
imap <buffer> <silent> <Tab> <C-o>:call xolox#notes#indent_list('>>', line('.'), line('.'))<CR>
7380
smap <buffer> <silent> <Tab> <C-o>:<C-u>call xolox#notes#indent_list('>>', line("'<"), line("'>"))<CR><C-o>gv
7481
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <Tab>"'
7582
let b:undo_ftplugin .= ' | execute "sunmap <buffer> <Tab>"'
76-
7783
imap <buffer> <silent> <S-Tab> <C-o>:call xolox#notes#indent_list('<<', line('.'), line('.'))<CR>
7884
smap <buffer> <silent> <S-Tab> <C-o>:<C-u>call xolox#notes#indent_list('<<', line("'<"), line("'>"))<CR><C-o>gv
7985
let b:undo_ftplugin .= ' | execute "iunmap <buffer> <S-Tab>"'

misc/notes/shadow/Note taking syntax

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ notes plug-in.
77

88
Lines prefixed with one or more ‘#’ symbols are headings which can be used for
99
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
1111
uppercase letter and ends in a colon (the hard wrapping in this paragraph
1212
illustrates why the “starts with uppercase” rule is needed):
1313

@@ -26,7 +26,7 @@ Hyper links and such:
2626

2727
# Lists
2828

29-
Bulleted lists can be used for to do lists:
29+
Bulleted lists can be used for to-do lists:
3030
• DONE Publish my notes.vim plug-in
3131
• TODO Write an indent script for atx headings
3232
• XXX This one is really important
@@ -39,7 +39,7 @@ Numbered lists are also supported:
3939
# Block quotes
4040

4141
> Quotes are written using
42-
> the convention from e-mail
42+
> the convention from e-mail
4343

4444
# Embedded syntax highlighting
4545

plugin/notes.vim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Last Change: May 22, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55
" License: MIT
6-
" Version: 0.8.6
6+
" Version: 0.8.7
77

88
" Support for automatic update using the GLVS plug-in.
99
" GetLatestVimScripts: 3375 1 :AutoInstall: notes.zip
@@ -65,6 +65,7 @@ augroup PluginNotes
6565
" NB: "nested" is used here so that SwapExists automatic commands apply
6666
" to notes (which is IMHO better than always showing the E325 prompt).
6767
au BufReadCmd note:* nested call xolox#notes#shortcut()
68+
call s:DAC('BufReadCmd', g:notes_shadowdir, 'call xolox#notes#edit_shadow()')
6869
call s:DAC('BufWriteCmd', g:notes_directory, 'call xolox#notes#save()')
6970
au SwapExists * call xolox#notes#swaphack()
7071
au WinEnter * if &ft == 'notes' | call xolox#notes#highlight_names(0) | endif

syntax/notes.vim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
" Vim syntax script
22
" Author: Peter Odding <peter@peterodding.com>
3-
" Last Change: March 25, 2011
3+
" Last Change: May 22, 2011
44
" URL: http://peterodding.com/code/vim/notes/
55

66
" Note: This file is encoded in UTF-8 including a byte order mark so
@@ -28,13 +28,17 @@ syntax cluster notesInline add=notesName
2828
highlight def link notesName Underlined
2929

3030
" Highlight list bullets and numbers. {{{2
31-
syntax match notesListBullet /^\s*\zs/
31+
syntax match notesListBullet /^\s*\zs\(\|\*\)/
3232
highlight def link notesListBullet Comment
3333
syntax match notesListNumber /^\s*\zs\d\+[[:punct:]]\?\ze\s/
3434
highlight def link notesListNumber Comment
3535

3636
" 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
3842
highlight def link notesQuotedFragment Special
3943

4044
" Highlight text emphasized in italic font. {{{2

0 commit comments

Comments
 (0)