Commits

ZyX_I committed 9323cf1

Convert tabutils to use feature in place of resource

Comments (0)

Files changed (2)

autoload/aurum/record.vim

         call repo.functions.strip(repo)
     endif
     call map(files, 'repo.functions.reltorepo(repo, v:val)')
-    tabnew
-    setlocal bufhidden=wipe
-    let t:aurum_tabid='AuRecordTab'
-    call s:_r.tabutils.settoplayout(s:layout)
     let sopts={'record': 1}
     if !empty(files)
         let sopts.files=files
     endif
-    call s:_r.run('silent botright '.s:F.getswheight().'split', 'status',
-                \ repo, sopts)
-    setlocal bufhidden=wipe
-    let w:aurum_winid='AuRecordStatus'
+    call s:_f.tab.create(s:layoutname, s:_r.run, ['status', repo, sopts])
     setlocal nomodifiable
     call s:_f.mapgroup.map('AuRecord', bufnr('%'))
     let bvar=s:_r.bufvars[bufnr('%')]
         endif
     endif
 endfunction
+"▶1 tabunload
+function s:F.tabunload()
+    let [lwnr, rwnr, swnr]=s:F.getwnrs()
+    execute swnr.'wincmd w'
+    let bvar=s:_r.bufvars[bufnr('%')]
+    return s:F.unload(bvar)
+endfunction
 "▶1 unload
 function s:F.unload(bvar)
     let sbvar=get(a:bvar, 'sbvar', a:bvar)
     augroup END
 endfunction
 "▶1 getwnrs
-let s:layout={
+function s:F.getwnrs()
+    return s:_f.tab.getwnrs(s:layoutname)
+endfunction
+"▶1 register layout
+let s:layoutname='AuRecordTab'
+call s:_f.tab.new(s:layoutname, {
             \   'top': ['AuRecordLeft', 'AuRecordRight'],
             \'bottom': 'AuRecordStatus',
-            \'bottomheightfun': s:F.getswheight,
-        \}
-function s:F.getwnrs()
-    return s:_r.tabutils.getwnrs(s:layout)
-endfunction
+        \}, s:F.getswheight, s:F.tabunload)
 "▶1 edit
 let s:savedopts=['readonly', 'modifiable', 'scrollbind', 'cursorbind',
             \    'scrollopt', 'wrap', 'foldmethod', 'foldcolumn']

autoload/aurum/tabutils.vim

 "▶1 
 scriptencoding utf-8
-execute frawor#Setup('0.0', {'@/resources': '0.0',})
-let s:r={}
+execute frawor#Setup('0.0', {})
+let s:_messages={
+            \'tidnstr': 'Tab identifier must be a non-empty string',
+            \'tidreg' : 'Tab identifier “%s” was already registered '.
+            \           'by plugin %s',
+            \'layinv' : "Invalid layout: it must\n".
+            \           " - be a dictionary\n".
+            \           " - that has only “top” and “bottom” keys\n".
+            \           " - with “top” key value being a non-empty ".
+            \              "list of unique strings\n".
+            \           " - and “bottom” key value being a string not present ".
+            \              "in “top” list",
+            \'bhfncal': 'botheightfun argument is not callable',
+            \'unlncal': 'unload argument is not callable',
+            \'tidukn' : 'Unknown tabid or tabid is not a string',
+            \'botncal': 'botfun argument is not callable',
+            \'barnlst': 'botargs argument is not a list',
+        \}
+let s:f={'cons': {}}
 "▶1 getids
 function s:F.getids(layout)
     return a:layout.top + [a:layout.bottom]
 endfunction
-"▶1 r.settoplayout
-function s:r.settoplayout(layout)
+"▶1 f.cons.new :: {f}, tabid, layout, botheightfun, unload → + s:tabs, fdict
+let s:tabs={}
+function s:f.cons.new(plugdict, fdict, tabid, layout, botheightfun, unload)
+    "▶2 Check arguments
+    if type(a:tabid)!=type('') || empty(a:tabid)
+        call s:_f.throw('tidnstr')
+    elseif has_key(s:tabs, a:tabid)
+        call s:_f.throw('tidreg', a:tabid, s:tabs[a:tabid])
+    elseif          type(a:layout)!=type({})
+                \|| sort(keys(a:layout)) !=# ['bottom', 'top']
+                \|| type(a:layout.bottom)!=type('')
+                \|| type(a:layout.top)!=type([])
+                \|| empty(a:layout.top)
+                \|| !empty(filter(copy(a:layout.top), 'type(v:val)!='.type('')))
+                \|| !empty(filter(a:layout.top[1:],
+                \          'index(a:layout.top[:(v:key)], v:val)!=-1'))
+                \|| index(a:layout.top, a:layout.bottom)!=-1
+        call s:_f.throw('layinv')
+    elseif !exists('*a:botheightfun')
+        call s:_f.throw('gshncal')
+    elseif !exists('*a:unload')
+        call s:_f.throw('unlncal')
+    endif
+    "▶2 tabdesc
+    let a:fdict[a:tabid]={
+                \'layout': deepcopy(a:layout),
+                \'botheightfun': a:botheightfun,
+                \'unload': a:unload,
+                \'ids': s:F.getids(a:layout),
+                \'id': a:tabid,
+            \}
+    let s:tabs[a:tabid]=a:plugdict.id
+endfunction
+"▶1 settoplayout
+function s:F.settoplayout(layout)
     let w:aurum_winid=a:layout.top[0]
     for id in a:layout.top[1:]
         rightbelow vnew
                 \'extend(curids, {v:val[0]: v:val[1]})')
     return map(copy(a:ids), 'get(curids, v:val, 0)')
 endfunction
-"▶1 r.getwnrs
-" layout: {
-"   "top": [id],
-"   "bottom": id,
-"   "bottomheightfun": () → N,
-" }
-" r: [topid] + [bottomid]
-function s:r.getwnrs(layout)
-    let r=[]
-    let ids=s:F.getids(a:layout)
+"▶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)
+        call s:_f.throw('tidukn')
+    endif
+    "▲2
+    let tabspec=a:fdict[a:tabid]
+    let ids=tabspec.ids
     let winnrs=s:F.getwinnrs(ids)
     if winnrs[0] is 0 || index(winnrs, 0)==-1
         return winnrs
         execute winnrs[-1].'wincmd w'
         silent only!
         topleft new
-        call s:r.settoplayout(a:layout)
+        call s:F.settoplayout(tabspec.layout)
         wincmd j
-        execute 'resize' call(a:layout.bottomheightfun, [], {})
+        execute 'resize' call(tabspec.botheightfun, [], {})
         return s:F.getwinnrs(ids)
     endif
 endfunction
+"▶1 f.cons.create :: {f}, tabid, botfun, botargs → + vim
+function s:f.cons.create(plugdict, fdict, tabid, botfun, botargs)
+    "▶2 Check arguments
+    if type(a:tabid)!=type('') || !has_key(a:fdict, a:tabid)
+        call s:_f.throw('tidukn')
+    elseif !exists('*a:botfun')
+        call s:_f.throw('botncal')
+    elseif type(a:botargs)!=type([])
+        call s:_f.throw('barnlst')
+    endif
+    "▲2
+    let tabspec=a:fdict[a:tabid]
+    tabnew
+    setlocal bufhidden=wipe
+    let t:aurum_tabid=tabspec.id
+    call s:F.settoplayout(tabspec.layout)
+    let height=call(tabspec.botheightfun, [], {})
+    call call(a:botfun, ['silent botright '.height.'split'] + a:botargs,
+                \{})
+    setlocal bufhidden=wipe
+    let w:aurum_winid=tabspec.layout.bottom
+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, [], {})
+        endif
+    endfor
+    call map(keys(a:fdict), 'remove(s:tabs, v:val)')
+endfunction
 "▶1 Post resource
-call s:_f.postresource('tabutils', s:r)
+call s:_f.newfeature('tab', s:f)
 "▶1
-call frawor#Lockvar(s:, '_r,_pluginloaded')
+call frawor#Lockvar(s:, 'tabs')
 " vim: ft=vim ts=4 sts=4 et fmr=▶,▲