Commits

ZyX_I committed cff27ee

Move tab closing code and uniqueness checks to tabutils

Comments (0)

Files changed (2)

autoload/aurum/record.vim

             \               'filter': 'bool'},
         \}
 let s:_messages={
-            \  'recex': 'There is already one AuRecord tab active '.
-            \           '(found tab with t:aurum_tabid set to "AuRecordTab")',
             \ 'bkpmis': 'Backup file %s not found',
             \'delfail': 'Failed to remove file %s',
             \'renfail': 'Failed to move file %s to %s',
     endif
     return height
 endfunction
+"▶1 run
+function s:F.run(cmd, opts, files, container)
+    let files=copy(a:files)
+    if !empty(files) && a:opts.repo is# ':'
+        let repo=s:_r.cmdutils.checkedgetrepo(s:_r.os.path.dirname(files[0]))
+    else
+        let repo=s:_r.cmdutils.checkedgetrepo(a:opts.repo)
+    endif
+    if get(a:opts, 'amend', 0)
+        let state=s:F.genstate(repo)
+        call repo.functions.strip(repo)
+    else
+        let state=0
+    endif
+    call map(files, 'repo.functions.reltorepo(repo, v:val)')
+    let sopts={'record': 1}
+    if !empty(files)
+        let sopts.files=files
+    endif
+    call s:_r.run(a:cmd, 'status', repo, sopts)
+    setlocal nomodifiable
+    call s:_f.mapgroup.map('AuRecord', bufnr('%'))
+    call extend(a:container, [state, repo])
+endfunction
 "▶1 recfunc
 " TODO investigate why closing record tab is causing next character consumption
 "      under wine
 let s:_aufunctions.comp=s:_r.cmdutils.gencompfunc(s:_aufunctions.cmd['@FWC'][0],
             \                                     [], s:_f.fwc.compile)
 function s:_aufunctions.cmd.function(opts, ...)
-    if !empty(filter(range(1, tabpagenr('$')),
-                \    'gettabvar(v:val, "aurum_tabid") is# "AuRecordTab"'))
-        call s:_f.throw('recex')
-    endif
-    let files=copy(a:000)
-    if !empty(files) && a:opts.repo is# ':'
-        let repo=s:_r.cmdutils.checkedgetrepo(s:_r.os.path.dirname(files[0]))
-    else
-        let repo=s:_r.cmdutils.checkedgetrepo(a:opts.repo)
-    endif
-    if get(a:opts, 'amend', 0)
-        let state=s:F.genstate(repo)
-        call repo.functions.strip(repo)
-    endif
-    call map(files, 'repo.functions.reltorepo(repo, v:val)')
-    let sopts={'record': 1}
-    if !empty(files)
-        let sopts.files=files
-    endif
-    call s:_f.tab.create(s:layoutname, s:_r.run, ['status', repo, sopts])
-    setlocal nomodifiable
-    call s:_f.mapgroup.map('AuRecord', bufnr('%'))
+    let container=[]
+    call s:_f.tab.create(s:layoutname, s:F.run, [a:opts, a:000, container])
+    let [state, repo]=container
     let bvar=s:_r.bufvars[bufnr('%')]
     " 0: not included, unmodified
     " 1: not included,   modified
     if empty(bvar.chars)
         bwipeout!
     endif
-    if get(a:opts, 'amend', 0)
+    if !empty(state)
         call s:F.setstate(repo, bvar, state)
     endif
 endfunction
     if bufexists(sbvar.bufnr)
         call setbufvar(sbvar.bufnr, '&modified', 0)
     endif
-    if exists('t:aurum_tabid') && t:aurum_tabid is# 'AuRecordTab'
-        unlet t:aurum_tabid
-        if tabpagenr('$')>1
-            tabclose!
-        else
-            let wlist=range(1, winnr('$'))
-            while !empty(wlist)
-                for wnr in wlist
-                    call remove(wlist, 0)
-                    if getwinvar(wnr, 'aurum_winid')[:7] is# 'AuRecord'
-                        execute wnr.'wincmd w'
-                        close!
-                        let wlist=range(1, winnr('$'))
-                        break
-                    endif
-                endfor
-            endwhile
+    "▶2 Close tab
+    try
+        silent call s:_f.tab.close()
+    catch /\V\^Frawor:\[^:]\+:tidukn:/
+        if s:_f.tab.find(s:layoutname)
+            try
+                call s:_f.tab.close()
+            catch /\V\^Frawor:\[^:]\+:tidukn:/
+            endtry
         endif
-    else
-        return
-    endif
+    endtry
+    "▲2
     let backupfiles=copy(sbvar.backupfiles)
     let newfiles=copy(sbvar.newfiles)
     call filter(backupfiles, 'filereadable(v:key)')
 endfunction
 "▶1 getwnrs
 function s:F.getwnrs()
-    return s:_f.tab.getwnrs(s:layoutname)
+    return s:_f.tab.getwnrs()
 endfunction
-"▶1 register layout
+"▶1 register tab
 let s:layoutname='AuRecordTab'
 call s:_f.tab.new(s:layoutname, {
             \   'top': ['AuRecordLeft', 'AuRecordRight'],

autoload/aurum/tabutils.vim

             \'tidukn' : 'Unknown tabid or tabid is not a string',
             \'botncal': 'botfun argument is not callable',
             \'barnlst': 'botargs argument is not a list',
+            \  'tabex': 'There already is tab with id “%s”',
         \}
 let s:f={'cons': {}}
 "▶1 getids
                 \'extend(curids, {v:val[0]: v:val[1]})')
     return map(copy(a:ids), 'get(curids, v:val, 0)')
 endfunction
-"▶1 f.cons.getwnrs :: {f}, tabid → ([topid] + bottomids)
-function s:f.cons.getwnrs(plugdict, fdict, tabid)
-    "▶2 Check argument
-    if type(a:tabid)!=type('') || !has_key(a:fdict, a:tabid)
+"▶1 f.cons.getwnrs :: {f} → ([topid] + bottomids)
+function s:f.cons.getwnrs(plugdict, fdict)
+    let tabid=gettabvar(tabpagenr(), 'aurum_tabid')
+    "▶2 Check tabid
+    if type(tabid)!=type('') || !has_key(a:fdict, tabid)
         call s:_f.throw('tidukn')
     endif
     "▲2
-    let tabspec=a:fdict[a:tabid]
+    let tabspec=a:fdict[tabid]
     let ids=tabspec.ids
     let winnrs=s:F.getwinnrs(ids)
     if winnrs[0] is 0 || index(winnrs, 0)==-1
         call s:_f.throw('barnlst')
     endif
     "▲2
+    if !empty(filter(range(1, tabpagenr('$')),
+                \    'gettabvar(v:val, "aurum_tabid") is# '.string(a:tabid)))
+        call s:_f.throw('tabex', a:tabid)
+    endif
     let tabspec=a:fdict[a:tabid]
     tabnew
     setlocal bufhidden=wipe
     setlocal bufhidden=wipe
     let w:aurum_winid=tabspec.layout.bottom
 endfunction
+"▶1 f.cons.find :: {f}, tabid
+function s:f.cons.find(plugdict, fdict, tabid)
+    "▶2 Check argument
+    if type(a:tabid)!=type('') || !has_key(a:fdict, a:tabid)
+        call s:_f.throw('tidukn')
+    endif
+    "▲2
+    for tabnr in range(1, tabpagenr('$'))
+        if gettabvar(tabnr, 'aurum_tabid') is# a:tabid
+            execute 'tabnext' tabnr
+            return tabnr
+        endif
+    endfor
+    return 0
+endfunction
+"▶1 f.cons.close :: {f}
+function s:f.cons.close(plugdict, fdict)
+    let tabid=gettabvar(tabpagenr(), 'aurum_tabid')
+    "▶2 Check tabid
+    if type(tabid)!=type('') || !has_key(a:fdict, tabid)
+        call s:_f.throw('tidukn')
+    endif
+    "▲2
+    let tabspec=a:fdict[tabid]
+    unlet t:aurum_tabid
+    if tabpagenr('$')>1
+        tabclose!
+    else
+        let wlist=range(1, winnr('$'))
+        while !empty(wlist)
+            for wnr in wlist
+                call remove(wlist, 0)
+                if index(tabspec.ids, getwinvar(wnr, 'aurum_winid'))
+                    execute wnr.'wincmd w'
+                    if winnr('$')==1
+                        unlet w:aurum_winid
+                        enew
+                    else
+                        close!
+                    endif
+                    let wlist=range(1, winnr('$'))
+                    break
+                endif
+            endfor
+        endwhile
+    endif
+    return 1
+endfunction
 "▶1 f.unloadpre :: {f}
 function s:f.unloadpre(plugdict, fdict)
     for tabnr in range(1, tabpagenr('$'))
         let tabid=gettabvar(tabnr, 'aurum_tabid')
         if type(tabid)==type('') && has_key(a:fdict, tabid)
-            call call(a:fdict[tabid].unload, [], {})
+            execute 'tabnext' tabnr
+            let tabspec=a:fdict[tabid]
+            call call(tabspec.unload, [], {})
         endif
     endfor
     call map(keys(a:fdict), 'remove(s:tabs, v:val)')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.