Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 18th, 2012  |  syntax: None  |  size: 4.01 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. " autoload/memolist.vim
  2. " Author:  Akira Maeda <glidenote@gmail.com>
  3. " Version: 0.0.5
  4. " Install this file as autoload/memolist.vim.  This file is sourced manually by
  5. " plugin/memolist.vim.  It is in autoload directory to allow for future usage of
  6. " Vim 7's autoload feature.
  7.  
  8. " Exit quickly when:
  9. " - this plugin was already loaded (or disabled)
  10. " - when 'compatible' is set
  11.  
  12. if &cp || exists("g:autoloaded_memolist")
  13.   finish
  14. endif
  15. let g:autoloaded_memolist= '1'
  16.  
  17. let s:cpo_save = &cpo
  18. set cpo&vim
  19.  
  20. " Utility Functions {{{1
  21. function! s:error(str)
  22.   echohl ErrorMsg
  23.   echomsg a:str
  24.   echohl None
  25.   let v:errmsg = a:str
  26. endfunction
  27. " }}}1
  28.  
  29. "------------------------
  30. " setting
  31. "------------------------
  32. if !exists('g:memolist_memo_suffix')
  33.   let g:memolist_memo_suffix = "markdown"
  34. endif
  35.  
  36. if !exists('g:memolist_memo_date')
  37.   let g:memolist_memo_date = "%Y-%m-%d %H:%M"
  38. endif
  39.  
  40. if !exists('g:memolist_title_pattern')
  41.   let g:memolist_title_pattern = "[ '\"]"
  42. endif
  43.  
  44. if !exists('g:memolist_qfixgrep')
  45.   let g:memolist_qfixgrep = ""
  46. endif
  47.  
  48. if !exists('g:memolist_vimfiler')
  49.   let g:memolist_vimfiler = ""
  50. endif
  51.  
  52. if !exists('g:memolist_template_path')
  53.   let g:memolist_template_path = ""
  54. endif
  55.  
  56. function! s:esctitle(str)
  57.   let str = a:str
  58.   let str = tolower(str)
  59.   let str = substitute(str, g:memolist_title_pattern, '-', 'g')
  60.   let str = substitute(str, '\(--\)\+', '-', 'g')
  61.   let str = substitute(str, '\(^-\|-$\)', '', 'g')
  62.   return str
  63. endfunction
  64.  
  65. function! s:escarg(s)
  66.   return escape(a:s, ' ')
  67. endfunction
  68.  
  69. let g:memolist_path = expand(g:memolist_path, ':p')
  70. if !isdirectory(g:memolist_path)
  71.   call mkdir(g:memolist_path, 'p')
  72. endif
  73.  
  74. "------------------------
  75. " function
  76. "------------------------
  77. function! memolist#list()
  78.   let vimfiler = g:memolist_vimfiler
  79.   if vimfiler == 'true'
  80.     exe "VimFiler" s:escarg(g:memolist_path)
  81.   else
  82.     exe "e" s:escarg(g:memolist_path)
  83.   endif
  84. endfunction
  85.  
  86. function! memolist#grep(word)
  87.   let word = a:word
  88.   if word == ''
  89.     let word = input("MemoGrep word: ")
  90.   endif
  91.   if word == ''
  92.     return
  93.   endif
  94.   let qfixgrep = g:memolist_qfixgrep
  95.   try
  96.     if qfixgrep == 'true'
  97.       exe "Vimgrep" s:escarg(word) s:escarg(g:memolist_path . "/*")
  98.     else
  99.       exe "vimgrep" s:escarg(word) s:escarg(g:memolist_path . "/*")
  100.     endif
  101.   catch
  102.     redraw | echohl ErrorMsg | echo v:exception | echohl None
  103.   endtry
  104. endfunction
  105.  
  106. function! memolist#_complete_ymdhms(...)
  107.   return [strftime("%Y%m%d%H%M")]
  108. endfunction
  109.  
  110. function! memolist#new(title)
  111.   let items = {
  112.   \ 'title': a:title,
  113.   \ 'date':  localtime(),
  114.   \}
  115.  
  116.   if g:memolist_memo_date != 'epoch'
  117.     let items['date'] = strftime(g:memolist_memo_date)
  118.   endif
  119.   if items['title'] == ''
  120.     let items['title']= input("Memo title: ", "", "customlist,memolist#_complete_ymdhms")
  121.   endif
  122.   if items['title'] == ''
  123.     return
  124.   endif
  125.   if get(g:, 'memolist_prompt_tags', 0) != 0
  126.     let items['tags'] = input("Memo tags: ")
  127.   endif
  128.   if get(g:, 'memolist_prompt_categories', 0) != 0
  129.     let items['categories'] = input("Memo categories: ")
  130.   endif
  131.  
  132.   let file_name = strftime("%Y-%m-%d-") . s:esctitle(items['title']) . "." . g:memolist_memo_suffix
  133.  
  134.   echo "Making that memo " . file_name
  135.   exe (&l:modified ? "sp" : "e") s:escarg(g:memolist_path . "/" . file_name)
  136.  
  137.   " memo template
  138.   let template = s:default_template
  139.   if g:memolist_template_path != ""
  140.     let path = expand(g:memolist_template_path, ":p")
  141.     let path = path . "/" . g:memolist_memo_suffix . ".txt"
  142.     if filereadable(path)
  143.       let template = readfile(path, 'b')
  144.     endif
  145.   endif
  146.   " apply template
  147.   let err = append(0, s:apply_template(template, items))
  148.  
  149. endfunction
  150.  
  151. let s:default_template = [
  152. \ 'title: {{_title_}}',
  153. \ '==========',
  154. \ 'date: {{_date_}}',
  155. \ 'tags: {{_tags_}}',
  156. \ 'categories: {{_categories_}}',
  157. \ '- - -',
  158. \]
  159.  
  160. function! s:apply_template(template, items)
  161.   let mx = '{{_\(\w\+\)_}}'
  162.   return filter(map(copy(a:template), "
  163.   \  substitute(v:val, mx,
  164.   \   '\\=has_key(a:items, submatch(1)) ? a:items[submatch(1)] : submatch(0)', 'g')
  165.   \"), 'v:val !~ mx')
  166. endfunction
  167.  
  168. let &cpo = s:cpo_save
  169.  
  170. " vim:set ft=vim ts=2 sw=2 sts=2: