64 calendar.vim (3-3) calendar_action.vim の更新

問題とその修正

一日ずれたところをハイライトしてほしいだけなのに、いちいち検索される。ひどく時間がかかる。これは嫌だ。

いちいち検索しなおすのではなく、既に howm の検索窓が出ている場合には、カーソル移動とハイライトのみを行うようにした。

新しい問題

検索窓が予定一覧ではなく、TODO一覧や、検索結果一覧だった場合にうまく動かない。これはまた次回に対応したい。howm の設定に依存しすぎている。%Y-%m-%d 以外でちゃんと動くか確認していない。MatchParen などを勝手に行っている。

思うこと

HowmDate2Int() って便利な関数だなと思う。日付をあらわす文字列から数値に変換する関数を自作しようかな。

--- calendar_action.vim.orig	2008-03-15 20:33:27.510782400 +0900
+++ calendar_action.vim	2008-03-15 20:22:19.079625600 +0900
@@ -1,3 +1,4 @@
+" calendar.vim と howm.vim を連携するための設定
 scriptencoding utf-8
 
 let g:calendar_action = 'CalendarActionHowm'
@@ -9,9 +10,54 @@
   let query = substitute(query, '%Y', printf('%04d', a:year), 'g')
   let query = substitute(query, '%m', printf('%02d', a:month), 'g')
   let query = substitute(query, '%d', printf('%02d', a:day), 'g')
-  " echo query
-  only | enew
-  execute ':HowmSchedule ' query
+  let bufname = 'howm\ Search\ result'
+  let winnr = bufwinnr(bufname)
+  if winnr == -1
+    " echo query
+    only | enew
+    execute ':HowmSchedule ' query
+  else
+    NoMatchParen
+    let bufnr = bufnr(bufname)
+    execute winnr . 'wincmd w'
+    let today_i = HowmDate2Int(query, g:howm_date_pattern)
+    let lnum = -1
+    let i = 1
+    let n = getbufvar(bufnr, 'searchResultNum')
+    while i <= n
+      let content = getbufvar(bufnr, 'content' . i)
+      if content =~ '^\[' . query . '\][@!.]\d*'
+        let lnum = i
+        break
+      endif
+      let date_pat = '^\[\(\d\{4}-\d\{2}-\d\{2}\)\][@!.]\d*.*$'
+      if content =~ date_pat
+        let date = substitute(content, date_pat, '\1', '')
+        if today_i < HowmDate2Int(date, g:howm_date_pattern)
+          let lnum = i
+          break
+        endif
+      endif
+      let i += 1
+    endwhile
+    if lnum != -1
+      " ハイライト
+      syntax clear
+      setlocal ft=howm_importantdate
+      let today_pat = (today_i - 719527) * 86400
+      let tomorrow_pat = (today_i + 1 - 719527) * 86400
+      let today_pat = strftime(g:howm_date_pattern, today_pat)
+      let tomorrow_pat = strftime(g:howm_date_pattern, tomorrow_pat)
+      let today_pat = '\%(\[' . today_pat . '\][-!@+.]\d*\)\@<=.*$'
+      let tomorrow_pat = '\%(\[' . tomorrow_pat . '\][-!@+.]\d*\)\@<=.*$'
+      execute 'syntax match howmToday display "' . today_pat . '"'
+      execute 'syntax match howmTomorrow display "' . tomorrow_pat . '"'
+      " カーソル移動
+      call cursor(lnum, 1)
+      redraw
+    endif
+    DoMatchParen
+  endif
   let cmd = ':Calendar'
   if a:dir != 'V' | let cmd .= 'H' | endif
   execute cmd a:year a:month
@@ -39,3 +85,4 @@
   endif
 endfunction
 
+" vim: set et ts=2 sw=2 fdm=marker: