2013-12-08 23 views
1

我想在Vim中實現一個鬆散版本的Niklas Luhmann's Zettelkasten 。他的方法的核心是註釋片段,它繼續當前的 筆記或布拉赫從它,引入一個稍微不同的話題或 的概念。在筆記名稱中,字母表示分支和數字 表示延續。像這樣:Vim:爲當前文件名添加自動字母和/或數字索引?

note100 
note101 
    note101a  # branches off from note100 (related topic) 
    note101b  # also branches off from note100 (related topic) 
     note101b01 # continues note101b (same topic) 
     note101b02 # also continues note101b (same topic) 
    note101c 
note102 

爲了實現這一點的Vim的,我需要的是無論是作爲一個「繼續」或 一個在當前緩衝區的說明「分支」自動列舉新文件 名。作爲一個非編碼器製造Vimscript中的第一個「真正」的步驟,這是我在哪裏有分支提示功能:(!)

function! ZettelkastenNewBranchingNote() 
    let b:current_note_name = expand('%:t:r') 
    let b:new_branching_note = call(BranchingFunctionThatReturnsNewNoteName) 
    silent execute 'edit' b:new_branching_note 
    echomsg 'New branching note ' b:new_branching_note 'created.' 
endfunction 

BranchingFunctionThatReturnsNewNoteName()應該採取 b:current_note_name並具有自動字母擴展它 指數(按字母順序向上計數)。我怎麼能做到這一點?

另外,對於我的新的連續註釋功能:我怎麼能從最後的數字部分數字 向上計數當前文件名? (例如100a01 > 100a02

感謝您的任何建議!

(有點與此相關,here Nexus的插件建議,但我寧願讓我的腳本 自足。)

回答

2

您提供上下文的大量(這是偉大的),但點亮所需的算法。對我來說,它看起來像這樣:如果當前文件以字母結尾,增加它,否則(它是一個數字),追加a開始按字母順序排列。

支票在Vim中完成正則表達式; \a[A-Za-z]縮寫形式(你也可以寫[[:alpha:]];是它是靈活的),和$錨它在名稱末尾:

if b:current_note_name =~ '\a$' 
    ... 

matchstr()提取的最後一個字符。

let lastAlpha = matchstr(b:current_note_name, '\a$') 
    if lastAlpha ==? 'z' 
     " TODO: Handle overflow 
    endif 

「增加」字母字符,首先將其轉換爲數字,增加,再回到:

let newAlpha = nr2char(char2nr(lastAlpha) + 1) 

要更換,使用substitute(),同樣具有相同的正則表達式。

let b:new_branching_note = substitute(b:current_note_name, '\a$', newAlpha, '') 

追加很簡單:

else 
    let b:new_branching_note = b:current_note_name . 'a' 
endif 
+0

非常感謝您,也爲解釋;這些對我的學習過程非常有幫助。 (我特別喜歡nr2char內建 - 非常好!)這個解決方案確實創建了這個文件,但我也得到了「E121:Undefined variable:b:new_branching_note」。爲什麼? – marttt

+0

該變量尚未定義(在該緩衝區中)。您可能需要在某個地方進行檢查:「如果!存在('b:new_branching_note')...' –

+0

謝謝!用'let b:current_note_name = exists('b:current_note_name')解決的問題? b:current_note_name:expand('%:t:r')'。我也必須修改一些變量類型,從'b:'到's:'。 – marttt

相關問題