Commits

ZyX_I committed 74e9fb7

Finished :AuRecord

Comments (0)

Files changed (2)

             \           'please retry.',
             \'renfail': 'Failed to move file %s to %s',
             \'delfail': 'Failed to remove file %s',
+            \ 'bkpmis': 'Backup file %s not found',
         \}
 "▶1 Вторая загрузка — функции
 "▶2 hg
     execute s:_r.py.cmd 'aurum.get_status(vim.eval("a:repo.path"), '.revargs.')'
     return r
 endfunction
+"▶3 hg.commit :: repo, message[, files[, user[, date[, force]]]]
+function s:F.hg.commit(repo, message, ...)
+    let match = get(a:000, 0, 0)
+    let user  = get(a:000, 1, 0)
+    let date  = get(a:000, 2, 0)
+    let force = get(a:000, 3, 0)
+    let args  =  'text=vim.eval("a:message"), '.
+                \'force='.((force)?('True'):('False')).', '.
+                \'user='.((user is 0)?('None'):('vim.eval("user")')).', '.
+                \'date='.((date is 0)?('None'):('vim.eval("date")')).', '.
+                \'files='.((empty(match))?('None'):('vim.eval("match")'))
+    execute s:_r.py.cmd 'aurum.commit(vim.eval("a:repo.path"), '.args.')'
+endfunction
 "▶2 rec
 "▶3 recfunc
 function s:recfunc.function(repo, opts, ...)
 endfunction
 "▶3 rec.unload
 function s:F.rec.unload(bvar)
+    if exists('t:aurecid') && t:aurecid is# 'AuRecordTab'
+        unlet t:aurecid
+        tabclose!
+    else
+        return
+    endif
     for [backupfile, file] in items(a:bvar.backupfiles)
+        if !filereadable(backupfile)
+            call s:_f.warn('bkpmis', backupfile)
+            continue
+        endif
         if delete(file)
             call s:_f.warn('delfail', file)
         endif
             call s:_f.warn('renfail', backupfile, file)
         endif
     endfor
-    if exists('t:aurecid') && t:aurecid is# 'AuRecordTab'
-        tabclose!
-    endif
 endfunction
 "▶3 rec.getwnrs
 function s:F.rec.getwnrs()
         endif
         if type is# 'modified' || type is# 'added' || type is# 'unknown'
             if !modified
+                execute swnr.'wincmd w'
                 let status=3
                 let bvar.statuses[line('.')-1]=status
                 call setline('.', s:statchars[status].bvar.lines[line('.')-1])
                 let bvar.prevct=b:changedtick
+                setlocal readonly nomodifiable
+                execute lwnr.'wincmd w'
             else
                 execute 'edit' fnameescape(fullpath)
                 setlocal bufhidden=wipe
                 setlocal noreadonly modifiable bufhidden=wipe
             endif
             if !modified
-                setlocal buftype=acwrite
                 let isexe=executable(fullpath)
                 if rename(fullpath, backupfile)
                     call s:_f.warn('renfail', fullpath, backupfile)
                     return
                 endif
                 augroup AuRecordLeft
-                    execute 'autocmd BufWriteCmd <buffer> w! '.
-                                \fnameescape(fullpath)
+                    execute 'autocmd BufWriteCmd <buffer> write! '.
+                                \fnameescape(fullpath).' | setlocal nomodified'
                 augroup END
+                setlocal buftype=acwrite
                 let bvar.backupfiles[backupfile]=fullpath
                 write
                 if isexe && s:_r.os.name is# 'posix'
             endif
         endif
     elseif a:action is# 'commit'
-        " TODO
-        call s:F.rec.unload(bvar)
+        let epath=escape(bvar.repo.path, ':\')
+        let files=filter(copy(bvar.files), 'bvar.statuses[v:key]>1')
+        aboveleft execute 'new' fnameescape('aurum://noop:commit:'.epath.':'.
+                    \join(map(copy(files), 'escape(v:val, "\\;")'), ';'))
+        let cbvar=s:bufvars[bufnr('%')]
+        let cbvar.repo=bvar.repo
+        let cbvar.files=files
+        let cbvar.sbvar=bvar
+        call s:_f.mapgroup.map('AuRecordCommit', bufnr('%'))
+        set modifiable noreadonly
+        startinsert
         return
     endif
-    if bufnr('%')==buf && b:changedtick!=bvar.prevct
-        let bvar.prevct=b:changedtick
-        if bvar.reset
-            let &l:undolevels=bvar.undolevels
-            unlet bvar.undolevels
-            let bvar.reset=0
+    if bufnr('%')==buf
+        if b:changedtick!=bvar.prevct
+            let bvar.prevct=b:changedtick
+            if bvar.reset
+                let &l:undolevels=bvar.undolevels
+                unlet bvar.undolevels
+                let bvar.reset=0
+            endif
         endif
+        setlocal readonly nomodifiable
     endif
-    setlocal readonly nomodifiable
 endfunction
 let s:_augroups+=['AuRecordLeft']
 "▶3 rec.runleftmap
     let [lwnr, rwnr, swnr]=s:F.rec.getwnrs()
     execute swnr.'wincmd w'
 endfunction
+"▶3 rec.runcommmap
+function s:F.rec.runcommmap(action)
+    let bvar=s:bufvars[bufnr('%')]
+    if a:action is# 'commit'
+        let message=join(getline(1, '$'), "\n")
+        call bvar.repo.functions.commit(bvar.repo, message, bvar.files)
+        call s:F.rec.unload(bvar.sbvar)
+    elseif a:action is# 'discard'
+        call s:F.rec.unload(bvar.sbvar)
+    endif
+endfunction
 "▶3 rec mappings
 function s:F.rec.gm(...)
     return ':<C-u>call call(<SID>Eval("s:F.rec.runstatmap"),'.string(a:000).','.
     return ':<C-u>call call(<SID>Eval("s:F.rec.runleftmap"),'.string(a:000).','.
                 \          '{})<CR>'
 endfunction
+function s:F.rec.gmc(...)
+    return ':<C-u>call call(<SID>Eval("s:F.rec.runcommmap"),'.string(a:000).','.
+                \          '{})<CR>'
+endfunction
 call s:_f.mapgroup.add('AuRecord', {
             \    'add': {'lhs': 'A', 'rhs': s:F.rec.gm('add')    },
             \ 'ignore': {'lhs': 'I', 'rhs': s:F.rec.gm('ignore') },
 call s:_f.mapgroup.add('AuRecordLeft', {
             \   'save': {'lhs': ',A', 'rhs': s:F.rec.gml('save')},
         \}, {'mode': 'n', 'silent': 1, 'dontmap': 1, 'leader': ','})
+call s:_f.mapgroup.add('AuRecordCommit', {
+            \ 'commit': {'lhs': 'C', 'rhs': s:F.rec.gmc('commit') },
+            \'discard': {'lhs': 'X', 'rhs': s:F.rec.gmc('discard')},
+        \}, {'mode': 'n', 'silent': 1, 'dontmap': 1, 'leader': ','})
 "▶2 comm
 "▶3 comm.difftobuffer
 function s:F.comm.difftobuffer(repo, buf, ...)
     " XXX If more metacharacters will be supported, they must be added to 
     " escape() calls in s:F.filehistory and s:F.runmap in ftplugin/aurumannotate
     return '\V\^'.substitute(substitute(substitute(substitute(substitute(
-                \substitute(substitute(substitute(a:glob,
+                \substitute(substitute(substitute(substitute(substitute(a:glob,
                 \'\v\\(.)', '\="\\{".char2nr(submatch(1))."}"', 'g'),
+                \'\V//\+',  '/',              'g'),
+                \'\V/\$',   '',                ''),
                 \'\V**/',   '\\(**/\\)\\=',   'g'),
                 \'\V**',    '\\.\\\\{42}',    'g'),
                 \'\V*',     '\\\\{91}^/]\\*', 'g'),
                 \'\V?',     '\\.',            'g'),
                 \'\V[',     '\\[',            'g'),
                 \'\V\\{'.char2nr('\').'}', '\\\\', 'g'),
-                \'\V\\{\(\d\+\)}', '\=nr2char(submatch(1))', 'g').'\$'
+                \'\V\\{\(\d\+\)}', '\=nr2char(submatch(1))', 'g').'\v($|\/)'
 endfunction
 "▶3 auefunc
 let s:bufvars={}
         call setline('.', fcontents)
         let s:bufvars[buf]={'file': tail}
     "▶4 noop command (do nothing)
+    " This command is needed to generate buffer names that will explain what is 
+    " this buffer for (it may be used if I implement session support)
     elseif command is# 'noop'
         if !has_key(s:bufvars, buf)
             let s:bufvars[buf]={}
                                          'ignored': status[5],
                                            'clean': status[6],
                                        })+')')
+def commit(path, text, force=False, user=None, date=None, files=None):
+    repo=get_repo(path)
+    args=[ui.ui(), repo]
+    if files:
+        args.extend(files)
+    kwargs={'addremove': True, 'close_branch': False, 'message': text}
+    if date:
+        kwargs['date']=date
+    if user:
+        kwargs['user']=user
+    if force:
+        kwargs['force']=True
+    workdir=os.path.abspath('.')
+    try:
+        os.chdir(repo.root)
+    except AttributeError:
+        pass
+    except OSError:
+        pass
+    commands.commit(*args, **kwargs)
+    os.chdir(workdir)