Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm using latex as a diary with sections for each subject. I manually add date of creation and modification to each element of text (section, subsection, subsubsection, ...). Is there a way to make it automatic?

I'm willing to learn lua or python and code it (I use modern C++) but i'm not sure what tools are available and how to make it fast.

My plan is to make it look starting from previous text element declaration and up to the next element declaration. may be look in undo tree. see if there are any changes. if there are than change the date after declaration of the current section. it would also be nice to hide these dates and show them back.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

This is possible, but it will require that you have some knowledge of Vim's scripting language (VimL). You will need to write a Vim function that can find the location of the date within the current section without "escaping" that section, then change that line. Once the function is written you will need to attach it to an autocmd.

For example, if your function was called TexAutoDate() you could call it automatically every time you write a file ending in .tex this way:

:autocmd BufWritePre *.tex :call TexAutoDate()

Unfortunately the actual body of the function will be fairly complex and without knowing more about your file layout I cannot help you much. I do have a function I wrote for a similar purpose, but it does not constrain itself to sections. The following function updates the "Last change:" line of a Vim help file automatically on write:

function HelpModifiedUpdate()
    if &filetype != "help"
        return
    endif

    let today=strftime('%Y %b %d')

    " Save current cursor position:
    let lnum = line('.')
    let col  = col('.')

    if search('Last change: ', 'w')
        let line1 = getline('.')
        if line1 =~ today
            " Restore cursor position:
            call cursor(lnum, col)
            return
        endif
        let line2 = substitute(
            \line1,
            \'Last change: .*',
            \'Last change: ' . today,
            \''
        \)
        call setline('.', line2)
    endif

    " Restore cursor position:
    call cursor(lnum, col)
endfunction

See:

:help :autocmd
:help :function
:help eval.txt

The last is extensive and you will not need to read through the entire file, but it will contain much of what you need if you do not already know VimL.

share|improve this answer

Probably the easiest way to do this is to install a snippet plugin. I prefer ultisnips because it's much more powerful, but snipmate is also very popular.

With ultisnips, I would define a snippet like this in my tex.snippets file;

snippet sec "section" b
\section{`!v strftime("%Y-%m-%d")`: ${1:section label}}

$0
endsnippet

If you now type sec at the beginning of a line and hit the <tab> key, the snippet will expand, inserting the current date (using the strftime function) and asking you to fill in the section label.

share|improve this answer
    
I meant that if you edit the section, the date would get update. not when you create a section. basically a plug-in would have to be checking text within the section for updates. i ended up using git for now –  kirill_igum Aug 18 '13 at 23:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.