My dotfiles. Period.

lh-vim-lib-3.1.1.vmb 227KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779
  1. " Vimball Archiver by Charles E. Campbell, Jr., Ph.D.
  2. UseVimball
  3. finish
  4. autoload/lh/askvim.vim [[[1
  5. 150
  6. "=============================================================================
  7. " $Id: askvim.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  8. " File: autoload/lh/askvim.vim {{{1
  9. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  10. " <URL:http://code.google.com/p/lh-vim/>
  11. " License: GPLv3 with exceptions
  12. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  13. " Version: 3.0.0
  14. " Created: 17th Apr 2007
  15. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (17th Apr 2007)
  16. "------------------------------------------------------------------------
  17. " Description:
  18. " Defines functions that asks vim what it is relinquish to tell us
  19. " - menu
  20. "
  21. "------------------------------------------------------------------------
  22. " Installation:
  23. " Drop it into {rtp}/autoload/lh/
  24. " Vim 7+ required.
  25. " History:
  26. " v2.0.0:
  27. " v3.0.0: GPLv3
  28. " TODO: «missing features»
  29. " }}}1
  30. "=============================================================================
  31. "=============================================================================
  32. let s:cpo_save=&cpo
  33. set cpo&vim
  34. "------------------------------------------------------------------------
  35. " ## Functions {{{1
  36. " # Debug {{{2
  37. function! lh#askvim#verbose(level)
  38. let s:verbose = a:level
  39. endfunction
  40. function! s:Verbose(expr)
  41. if exists('s:verbose') && s:verbose
  42. echomsg a:expr
  43. endif
  44. endfunction
  45. function! lh#askvim#debug(expr)
  46. return eval(a:expr)
  47. endfunction
  48. "------------------------------------------------------------------------
  49. " # Public {{{2
  50. " Function: lh#askvim#exe(command) {{{3
  51. function! lh#askvim#Exe(command)
  52. echomsg 'lh#askvim#Exe() is deprecated, use lh#askvim#exe()'
  53. return lh#askvim#exe(a:command)
  54. endfunction
  55. function! lh#askvim#exe(command)
  56. let save_a = @a
  57. try
  58. silent! redir @a
  59. silent! exe a:command
  60. redir END
  61. finally
  62. " Always restore everything
  63. let res = @a
  64. let @a = save_a
  65. return res
  66. endtry
  67. endfunction
  68. " Function: lh#askvim#menu(menuid) {{{3
  69. function! s:AskOneMenu(menuact, res)
  70. let sKnown_menus = lh#askvim#exe(a:menuact)
  71. let lKnown_menus = split(sKnown_menus, '\n')
  72. " echo string(lKnown_menus)
  73. " 1- search for the menuid
  74. " todo: fix the next line to correctly interpret "stuff\.stuff" and
  75. " "stuff\\.stuff".
  76. let menuid_parts = split(a:menuact, '\.')
  77. let simplifiedKnown_menus = deepcopy(lKnown_menus)
  78. call map(simplifiedKnown_menus, 'substitute(v:val, "&", "", "g")')
  79. " let idx = lh#list#match(simplifiedKnown_menus, '^\d\+\s\+'.menuid_parts[-1])
  80. let idx = match(simplifiedKnown_menus, '^\d\+\s\+'.menuid_parts[-1])
  81. if idx == -1
  82. " echo "not found"
  83. return
  84. endif
  85. " echo "l[".idx."]=".lKnown_menus[idx]
  86. if empty(a:res)
  87. let a:res.priority = matchstr(lKnown_menus[idx], '\d\+\ze\s\+.*')
  88. let a:res.name = matchstr(lKnown_menus[idx], '\d\+\s\+\zs.*')
  89. let a:res.actions = {}
  90. " else
  91. " what if the priority isn't the same?
  92. endif
  93. " 2- search for the menu definition
  94. let idx += 1
  95. while idx != len(lKnown_menus)
  96. echo "l[".idx."]=".lKnown_menus[idx]
  97. " should not happen
  98. if lKnown_menus[idx] =~ '^\d\+' | break | endif
  99. " :h showing-menus
  100. " -> The format of the result of the call to Exe() seems to be:
  101. " ^ssssMns-sACTION$
  102. " s == 1 whitespace
  103. " M == mode (inrvcs)
  104. " n == noremap(*)/script(&)
  105. " - == disable(-)/of not
  106. let act = {}
  107. let menu_def = matchlist(lKnown_menus[idx],
  108. \ '^\s*\([invocs]\)\([&* ]\) \([- ]\) \(.*\)$')
  109. if len(menu_def) > 4
  110. let act.mode = menu_def[1]
  111. let act.nore_script = menu_def[2]
  112. let act.disabled = menu_def[3]
  113. let act.action = menu_def[4]
  114. else
  115. echomsg string(menu_def)
  116. echoerr "lh#askvim#menu(): Cannot decode ``".lKnown_menus[idx]."''"
  117. endif
  118. let a:res.actions["mode_" . act.mode] = act
  119. let idx += 1
  120. endwhile
  121. " n- Return the result
  122. return a:res
  123. endfunction
  124. function! lh#askvim#menu(menuid, modes)
  125. let res = {}
  126. let i = 0
  127. while i != strlen(a:modes)
  128. call s:AskOneMenu(a:modes[i].'menu '.a:menuid, res)
  129. let i += 1
  130. endwhile
  131. return res
  132. endfunction
  133. " Functions }}}1
  134. "------------------------------------------------------------------------
  135. let &cpo=s:cpo_save
  136. "=============================================================================
  137. " vim600: set fdm=marker:
  138. autoload/lh/buffer.vim [[[1
  139. 102
  140. "=============================================================================
  141. " $Id: buffer.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  142. " File: autoload/lh/buffer.vim {{{1
  143. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  144. " <URL:http://code.google.com/p/lh-vim/>
  145. " Licence: GPLv3
  146. " Version: 3.0.0
  147. " Created: 23rd Jan 2007
  148. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  149. "------------------------------------------------------------------------
  150. " Description:
  151. " Defines functions that help finding windows and handling buffers.
  152. "
  153. "------------------------------------------------------------------------
  154. " Installation:
  155. " Drop it into {rtp}/autoload/lh/
  156. " Vim 7+ required.
  157. " History:
  158. " v1.0.0 First Version
  159. " (*) Functions moved from searchInRuntimeTime
  160. " v2.2.0
  161. " (*) new function: lh#buffer#list()
  162. " v3.0.0 GPLv3
  163. " TODO:
  164. " }}}1
  165. "=============================================================================
  166. "=============================================================================
  167. let s:cpo_save=&cpo
  168. set cpo&vim
  169. " ## Functions {{{1
  170. "------------------------------------------------------------------------
  171. " # Debug {{{2
  172. function! lh#buffer#verbose(level)
  173. let s:verbose = a:level
  174. endfunction
  175. function! s:Verbose(expr)
  176. if exists('s:verbose') && s:verbose
  177. echomsg a:expr
  178. endif
  179. endfunction
  180. function! lh#buffer#debug(expr)
  181. return eval(a:expr)
  182. endfunction
  183. "------------------------------------------------------------------------
  184. " # Public {{{2
  185. " Function: lh#buffer#find({filename}) {{{3
  186. " If {filename} is opened in a window, jump to this window, otherwise return -1
  187. " Moved from searchInRuntimeTime.vim
  188. function! lh#buffer#find(filename)
  189. let b = bufwinnr(a:filename)
  190. if b == -1 | return b | endif
  191. exe b.'wincmd w'
  192. return b
  193. endfunction
  194. function! lh#buffer#Find(filename)
  195. return lh#buffer#find(a:filename)
  196. endfunction
  197. " Function: lh#buffer#jump({filename},{cmd}) {{{3
  198. function! lh#buffer#jump(filename, cmd)
  199. if lh#buffer#find(a:filename) != -1 | return | endif
  200. exe a:cmd . ' ' . a:filename
  201. endfunction
  202. function! lh#buffer#Jump(filename, cmd)
  203. return lh#buffer#jump(a:filename, a:cmd)
  204. endfunction
  205. " Function: lh#buffer#scratch({bname},{where}) {{{3
  206. function! lh#buffer#scratch(bname, where)
  207. try
  208. silent exe a:where.' sp '.a:bname
  209. catch /.*/
  210. throw "Can't open a buffer named '".a:bname."'!"
  211. endtry
  212. setlocal bt=nofile bh=wipe nobl noswf ro
  213. endfunction
  214. function! lh#buffer#Scratch(bname, where)
  215. return lh#buffer#scratch(a:bname, a:where)
  216. endfunction
  217. " Function: lh#buffer#list() {{{3
  218. function! lh#buffer#list()
  219. let all = range(0, bufnr('$'))
  220. " let res = lh#list#transform_if(all, [], 'v:1_', 'buflisted')
  221. let res = lh#list#copy_if(all, [], 'buflisted')
  222. return res
  223. endfunction
  224. " Ex: Names of the buffers listed
  225. " -> echo lh#list#transform(lh#buffer#list(), [], "bufname")
  226. " Ex: wipeout empty buffers listed
  227. " -> echo 'bw'.join(lh#list#copy_if(range(0, bufnr('$')), [], 'buflisted(v:1_) && empty(bufname(v:1_))'), ' ')
  228. "=============================================================================
  229. let &cpo=s:cpo_save
  230. "=============================================================================
  231. " vim600: set fdm=marker:
  232. autoload/lh/buffer/dialog.vim [[[1
  233. 271
  234. "=============================================================================
  235. " $Id: dialog.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  236. " File: autoload/lh/buffer/dialog.vim {{{1
  237. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  238. " <URL:http://code.google.com/p/lh-vim/>
  239. " License: GPLv3 with exceptions
  240. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  241. " Version: 3.0.0
  242. " Created: 21st Sep 2007
  243. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  244. "------------------------------------------------------------------------
  245. " Description: «description»
  246. "
  247. "------------------------------------------------------------------------
  248. " Installation:
  249. " Drop it into {rtp}/autoload/lh/
  250. " Vim 7+ required.
  251. " History:
  252. " v1.0.0 First Version
  253. " (*) Functions imported from Mail_mutt_alias.vim
  254. " v3.0.0 GPLv3
  255. " TODO:
  256. " (*) --abort-- line
  257. " (*) custom messages
  258. " (*) do not mess with search history
  259. " (*) support any &magic
  260. " (*) syntax
  261. " (*) add number/letters
  262. " (*) tag with '[x] ' instead of '* '
  263. " }}}1
  264. "=============================================================================
  265. "=============================================================================
  266. let s:cpo_save=&cpo
  267. set cpo&vim
  268. "=============================================================================
  269. " ## Globals {{{1
  270. let s:LHdialog = {}
  271. "=============================================================================
  272. " ## Functions {{{1
  273. " # Debug {{{2
  274. function! lh#buffer#dialog#verbose(level)
  275. let s:verbose = a:level
  276. endfunction
  277. function! s:Verbose(expr)
  278. if exists('s:verbose') && s:verbose
  279. echomsg a:expr
  280. endif
  281. endfunction
  282. function! lh#buffer#dialog#debug(expr)
  283. return eval(a:expr)
  284. endfunction
  285. "=============================================================================
  286. " # Dialog functions {{{2
  287. "------------------------------------------------------------------------
  288. function! s:Mappings(abuffer)
  289. " map <enter> to edit a file, also dbl-click
  290. exe "nnoremap <silent> <buffer> <esc> :silent call ".a:abuffer.action."(-1, ".a:abuffer.id.")<cr>"
  291. exe "nnoremap <silent> <buffer> q :call lh#buffer#dialog#select(-1, ".a:abuffer.id.")<cr>"
  292. exe "nnoremap <silent> <buffer> <cr> :call lh#buffer#dialog#select(line('.'), ".a:abuffer.id.")<cr>"
  293. " nnoremap <silent> <buffer> <2-LeftMouse> :silent call <sid>GrepEditFileLine(line("."))<cr>
  294. " nnoremap <silent> <buffer> Q :call <sid>Reformat()<cr>
  295. " nnoremap <silent> <buffer> <Left> :set tabstop-=1<cr>
  296. " nnoremap <silent> <buffer> <Right> :set tabstop+=1<cr>
  297. if a:abuffer.support_tagging
  298. nnoremap <silent> <buffer> t :silent call <sid>ToggleTag(line("."))<cr>
  299. nnoremap <silent> <buffer> <space> :silent call <sid>ToggleTag(line("."))<cr>
  300. endif
  301. nnoremap <silent> <buffer> <tab> :silent call <sid>NextChoice('')<cr>
  302. nnoremap <silent> <buffer> <S-tab> :silent call <sid>NextChoice('b')<cr>
  303. exe "nnoremap <silent> <buffer> h :silent call <sid>ToggleHelp(".a:abuffer.id.")<cr>"
  304. endfunction
  305. "----------------------------------------
  306. " Tag / untag the current choice {{{
  307. function! s:ToggleTag(lineNum)
  308. if a:lineNum > s:Help_NbL()
  309. " If tagged
  310. if (getline(a:lineNum)[0] == '*')
  311. let b:NbTags = b:NbTags - 1
  312. silent exe a:lineNum.'s/^\* / /e'
  313. else
  314. let b:NbTags = b:NbTags + 1
  315. silent exe a:lineNum.'s/^ /* /e'
  316. endif
  317. " Move after the tag ; there is something with the two previous :s. They
  318. " don't leave the cursor at the same position.
  319. silent! normal! 3|
  320. call s:NextChoice('') " move to the next choice
  321. endif
  322. endfunction
  323. " }}}
  324. function! s:Help_NbL()
  325. " return 1 + nb lines of BuildHelp
  326. return 2 + len(b:dialog['help_'.b:dialog.help_type])
  327. endfunction
  328. "----------------------------------------
  329. " Go to the Next (/previous) possible choice. {{{
  330. function! s:NextChoice(direction)
  331. " echomsg "next!"
  332. call search('^[ *]\s*\zs\S\+', a:direction)
  333. endfunction
  334. " }}}
  335. "------------------------------------------------------------------------
  336. function! s:RedisplayHelp(dialog)
  337. silent! 2,$g/^@/d_
  338. normal! gg
  339. for help in a:dialog['help_'.a:dialog.help_type]
  340. silent put=help
  341. endfor
  342. endfunction
  343. function! lh#buffer#dialog#update(dialog)
  344. set noro
  345. exe (s:Help_NbL()+1).',$d_'
  346. for choice in a:dialog.choices
  347. silent $put=' '.choice
  348. endfor
  349. set ro
  350. endfunction
  351. function! s:Display(dialog, atitle)
  352. set noro
  353. 0 put = a:atitle
  354. call s:RedisplayHelp(a:dialog)
  355. for choice in a:dialog.choices
  356. silent $put=' '.choice
  357. endfor
  358. set ro
  359. exe s:Help_NbL()+1
  360. endfunction
  361. function! s:ToggleHelp(bufferId)
  362. call lh#buffer#find(a:bufferId)
  363. call b:dialog.toggle_help()
  364. endfunction
  365. function! lh#buffer#dialog#toggle_help() dict
  366. let self.help_type
  367. \ = (self.help_type == 'short')
  368. \ ? 'long'
  369. \ : 'short'
  370. call s:RedisplayHelp(self)
  371. endfunction
  372. function! lh#buffer#dialog#new(bname, title, where, support_tagging, action, choices)
  373. " The ID will be the buffer id
  374. let res = {}
  375. let where_it_started = getpos('.')
  376. let where_it_started[0] = bufnr('%')
  377. let res.where_it_started = where_it_started
  378. try
  379. call lh#buffer#scratch(a:bname, a:where)
  380. catch /.*/
  381. echoerr v:exception
  382. return res
  383. endtry
  384. let res.id = bufnr('%')
  385. let b:NbTags = 0
  386. let b:dialog = res
  387. let s:LHdialog[res.id] = res
  388. let res.help_long = []
  389. let res.help_short = []
  390. let res.help_type = 'short'
  391. let res.support_tagging = a:support_tagging
  392. let res.action = a:action
  393. let res.choices = a:choices
  394. " Long help
  395. call lh#buffer#dialog#add_help(res, '@| <cr>, <double-click> : select this', 'long')
  396. call lh#buffer#dialog#add_help(res, '@| <esc>, q : Abort', 'long')
  397. if a:support_tagging
  398. call lh#buffer#dialog#add_help(res, '@| <t>, <space> : Tag/Untag the current item', 'long')
  399. endif
  400. call lh#buffer#dialog#add_help(res, '@| <up>/<down>, <tab>, +/- : Move between entries', 'long')
  401. call lh#buffer#dialog#add_help(res, '@|', 'long')
  402. " call lh#buffer#dialog#add_help(res, '@| h : Toggle help', 'long')
  403. call lh#buffer#dialog#add_help(res, '@+'.repeat('-', winwidth(bufwinnr(res.id))-3), 'long')
  404. " Short Help
  405. " call lh#buffer#dialog#add_help(res, '@| h : Toggle help', 'short')
  406. call lh#buffer#dialog#add_help(res, '@+'.repeat('-', winwidth(bufwinnr(res.id))-3), 'short')
  407. let res.toggle_help = function("lh#buffer#dialog#toggle_help")
  408. let title = '@ ' . a:title
  409. let helpstr = '| Toggle (h)elp'
  410. let title = title
  411. \ . repeat(' ', winwidth(bufwinnr(res.id))-lh#encoding#strlen(title)-lh#encoding#strlen(helpstr)-1)
  412. \ . helpstr
  413. call s:Display(res, title)
  414. call s:Mappings(res)
  415. return res
  416. endfunction
  417. function! lh#buffer#dialog#add_help(abuffer, text, help_type)
  418. call add(a:abuffer['help_'.a:help_type],a:text)
  419. endfunction
  420. "=============================================================================
  421. function! lh#buffer#dialog#quit()
  422. let bufferId = b:dialog.where_it_started[0]
  423. echohl WarningMsg
  424. echo "Abort"
  425. echohl None
  426. quit
  427. call lh#buffer#find(bufferId)
  428. endfunction
  429. " Function: lh#buffer#dialog#select(line, bufferId [,overriden-action])
  430. function! lh#buffer#dialog#select(line, bufferId, ...)
  431. if a:line == -1
  432. call lh#buffer#dialog#quit()
  433. return
  434. " elseif a:line <= s:Help_NbL() + 1
  435. elseif a:line <= s:Help_NbL()
  436. echoerr "Unselectable item"
  437. return
  438. else
  439. let dialog = s:LHdialog[a:bufferId]
  440. let results = { 'dialog' : dialog, 'selection' : [] }
  441. if b:NbTags == 0
  442. " -1 because first index is 0
  443. " let results = [ dialog.choices[a:line - s:Help_NbL() - 1] ]
  444. let results.selection = [ a:line - s:Help_NbL() - 1 ]
  445. else
  446. silent g/^* /call add(results.selection, line('.')-s:Help_NbL()-1)
  447. endif
  448. endif
  449. if a:0 > 0 " action overriden
  450. exe 'call '.dialog.action.'(results, a:000)'
  451. else
  452. exe 'call '.dialog.action.'(results)'
  453. endif
  454. endfunction
  455. function! lh#buffer#dialog#Select(line, bufferId, ...)
  456. echomsg "lh#buffer#dialog#Select() is deprecated, use lh#buffer#dialog#select() instead"
  457. if a:0 > 0 " action overriden
  458. exe 'call lh#buffer#dialog#select(a:line, a:bufferId, a:1)'
  459. else
  460. exe 'call lh#buffer#dialog#select(a:line, a:bufferId)'
  461. endif
  462. endfunction
  463. function! Action(results)
  464. let dialog = a:results.dialog
  465. let choices = dialog.choices
  466. for r in a:results.selection
  467. echomsg '-> '.choices[r]
  468. endfor
  469. endfunction
  470. "=============================================================================
  471. let &cpo=s:cpo_save
  472. "=============================================================================
  473. " vim600: set fdm=marker:
  474. autoload/lh/command.vim [[[1
  475. 226
  476. "=============================================================================
  477. " $Id: command.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  478. " File: autoload/lh/command.vim {{{1
  479. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  480. " <URL:http://code.google.com/p/lh-vim/>
  481. " License: GPLv3 with exceptions
  482. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  483. " Version: 3.0.0
  484. " Created: 08th Jan 2007
  485. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (08th Jan 2007)
  486. "------------------------------------------------------------------------
  487. " Description:
  488. " Helpers to define commands that:
  489. " - support subcommands
  490. " - support autocompletion
  491. "
  492. "------------------------------------------------------------------------
  493. " Installation:
  494. " Drop it into {rtp}/autoload/lh/
  495. " Vim 7+ required.
  496. " History:
  497. " v3.0.0: GPLv3
  498. " v2.0.0: Code moved from other plugins
  499. " TODO: «missing features»
  500. " }}}1
  501. "=============================================================================
  502. "=============================================================================
  503. let s:cpo_save=&cpo
  504. set cpo&vim
  505. " ## Debug {{{1
  506. function! lh#command#verbose(level)
  507. let s:verbose = a:level
  508. endfunction
  509. function! s:Verbose(expr)
  510. if exists('s:verbose') && s:verbose
  511. echomsg a:expr
  512. endif
  513. endfunction
  514. function! lh#command#debug(expr)
  515. return eval(a:expr)
  516. endfunction
  517. "------------------------------------------------------------------------
  518. " ## Functions {{{1
  519. " Tool functions {{{2
  520. " Function: lh#command#Fargs2String(aList) {{{3
  521. " @param[in,out] aList list of params from <f-args>
  522. " @see tests/lh/test-Fargs2String.vim
  523. function! lh#command#Fargs2String(aList)
  524. if empty(a:aList) | return '' | endif
  525. let quote_char = a:aList[0][0]
  526. let res = a:aList[0]
  527. call remove(a:aList, 0)
  528. if quote_char !~ '["'."']"
  529. return res
  530. endif
  531. " else
  532. let end_string = '[^\\]\%(\\\\\)*'.quote_char.'$'
  533. while !empty(a:aList) && res !~ end_string
  534. let res .= ' ' . a:aList[0]
  535. call remove(a:aList, 0)
  536. endwhile
  537. return res
  538. endfunction
  539. "------------------------------------------------------------------------
  540. " ## Experimental Functions {{{1
  541. " Internal functions {{{2
  542. " Function: s:SaveData({Data}) {{{3
  543. " @param Data Command definition
  544. " Saves {Data} as s:Data{s:data_id++}. The definition will be used by
  545. " automatically generated commands.
  546. " @return s:data_id
  547. let s:data_id = 0
  548. function! s:SaveData(Data)
  549. if has_key(a:Data, "command_id")
  550. " Avoid data duplication
  551. return a:Data.command_id
  552. else
  553. let s:Data{s:data_id} = a:Data
  554. let id = s:data_id
  555. let s:data_id += 1
  556. let a:Data.command_id = id
  557. return id
  558. endif
  559. endfunction
  560. " BTWComplete(ArgLead, CmdLine, CursorPos): Auto-complete {{{3
  561. function! lh#command#complete(ArgLead, CmdLine, CursorPos)
  562. let tmp = substitute(a:CmdLine, '\s*\S*', 'Z', 'g')
  563. let pos = strlen(tmp)
  564. if 0
  565. call confirm( "AL = ". a:ArgLead."\nCL = ". a:CmdLine."\nCP = ".a:CursorPos
  566. \ . "\ntmp = ".tmp."\npos = ".pos
  567. \, '&Ok', 1)
  568. endif
  569. if 2 == pos
  570. " First argument: a command
  571. return s:commands
  572. elseif 3 == pos
  573. " Second argument: first arg of the command
  574. if -1 != match(a:CmdLine, '^BTW\s\+echo')
  575. return s:functions . "\n" . s:variables
  576. elseif -1 != match(a:CmdLine, '^BTW\s\+\%(help\|?\)')
  577. elseif -1 != match(a:CmdLine, '^BTW\s\+\%(set\|add\)\%(local\)\=')
  578. " Adds a filter
  579. " let files = globpath(&rtp, 'compiler/BT-*')
  580. " let files = files . globpath(&rtp, 'compiler/BT_*')
  581. " let files = files . globpath(&rtp, 'compiler/BT/*')
  582. let files = s:FindFilter('*')
  583. let files = substitute(files,
  584. \ '\(^\|\n\).\{-}compiler[\\/]BTW[-_\\/]\(.\{-}\)\.vim\>\ze\%(\n\|$\)',
  585. \ '\1\2', 'g')
  586. return files
  587. elseif -1 != match(a:CmdLine, '^BTW\s\+remove\%(local\)\=')
  588. " Removes a filter
  589. return substitute(s:FiltersList(), ',', '\n', 'g')
  590. endif
  591. endif
  592. " finally: unknown
  593. echoerr 'BTW: unespected parameter ``'. a:ArgLead ."''"
  594. return ''
  595. endfunction
  596. function! s:BTW(command, ...)
  597. " todo: check a:0 > 1
  598. if 'set' == a:command | let g:BTW_build_tool = a:1
  599. if exists('b:BTW_build_tool')
  600. let b:BTW_build_tool = a:1
  601. endif
  602. elseif 'setlocal' == a:command | let b:BTW_build_tool = a:1
  603. elseif 'add' == a:command | call s:AddFilter('g', a:1)
  604. elseif 'addlocal' == a:command | call s:AddFilter('b', a:1)
  605. " if exists('b:BTW_filters_list') " ?????
  606. " call s:AddFilter('b', a:1)
  607. " endif
  608. elseif 'remove' == a:command | call s:RemoveFilter('g', a:1)
  609. elseif 'removelocal' == a:command | call s:RemoveFilter('b', a:1)
  610. elseif 'rebuild' == a:command " wait for s:ReconstructToolsChain()
  611. elseif 'echo' == a:command | exe "echo s:".a:1
  612. " echo s:{a:f1} ## don't support «echo s:f('foo')»
  613. elseif 'reloadPlugin' == a:command
  614. let g:force_reload_BuildToolsWrapper = 1
  615. let g:BTW_BTW_in_use = 1
  616. exe 'so '.s:sfile
  617. unlet g:force_reload_BuildToolsWrapper
  618. unlet g:BTW_BTW_in_use
  619. return
  620. elseif a:command =~ '\%(help\|?\)'
  621. call s:Usage()
  622. return
  623. endif
  624. call s:ReconstructToolsChain()
  625. endfunction
  626. " ##############################################################
  627. " Public functions {{{2
  628. function! s:FindSubcommand(definition, subcommand)
  629. for arg in a:definition.arguments
  630. if arg.name == a:subcommand
  631. return arg
  632. endif
  633. endfor
  634. throw "NF"
  635. endfunction
  636. function! s:execute_function(definition, params)
  637. if len(a:params) < 1
  638. throw "(lh#command) Not enough arguments"
  639. endif
  640. let l:Fn = a:definition.action
  641. echo "calling ".string(l:Fn)
  642. echo "with ".string(a:params)
  643. " call remove(a:params, 0)
  644. call l:Fn(a:params)
  645. endfunction
  646. function! s:execute_sub_commands(definition, params)
  647. try
  648. if len(a:params) < 1
  649. throw "(lh#command) Not enough arguments"
  650. endif
  651. let subcommand = s:FindSubcommand(a:definition, a:params[0])
  652. call remove(a:params, 0)
  653. call s:int_execute(subcommand, a:params)
  654. catch /NF.*/
  655. throw "(lh#command) Unexpected subcommand `".a:params[0]."'."
  656. endtry
  657. endfunction
  658. function! s:int_execute(definition, params)
  659. echo "params=".string(a:params)
  660. call s:execute_{a:definition.arg_type}(a:definition, a:params)
  661. endfunction
  662. function! s:execute(definition, ...)
  663. try
  664. let params = copy(a:000)
  665. call s:int_execute(a:definition, params)
  666. catch /(lh#command).*/
  667. echoerr v:exception . " in `".a:definition.name.' '.join(a:000, ' ')."'"
  668. endtry
  669. endfunction
  670. function! lh#command#new(definition)
  671. let cmd_name = a:definition.name
  672. " Save the definition as an internal script variable
  673. let id = s:SaveData(a:definition)
  674. exe "command! -nargs=* ".cmd_name." :call s:execute(s:Data".id.", <f-args>)"
  675. endfunction
  676. " Functions }}}1
  677. "------------------------------------------------------------------------
  678. let &cpo=s:cpo_save
  679. "=============================================================================
  680. " vim600: set fdm=marker:
  681. autoload/lh/common.vim [[[1
  682. 97
  683. "=============================================================================
  684. " $Id: common.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  685. " File: autoload/lh/common.vim {{{1
  686. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  687. " <URL:http://code.google.com/p/lh-vim/>
  688. " License: GPLv3 with exceptions
  689. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  690. " Version: 3.0.0
  691. " Created: 07th Oct 2006
  692. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (08th Feb 2008)
  693. "------------------------------------------------------------------------
  694. " Description:
  695. " Some common functions for:
  696. " - displaying error messages
  697. " - checking dependencies
  698. "
  699. "------------------------------------------------------------------------
  700. " Installation:
  701. " Drop it into {rtp}/autoload/lh/
  702. " Vim 7+ required.
  703. " History:
  704. " v3.0.0
  705. " - GPLv3
  706. " v2.1.1
  707. " - New function: lh#common#echomsg_multilines()
  708. " - lh#common#warning_msg() supports multilines messages
  709. "
  710. " v2.0.0:
  711. " - Code moved from other plugins
  712. " }}}1
  713. "=============================================================================
  714. "=============================================================================
  715. let s:cpo_save=&cpo
  716. set cpo&vim
  717. "------------------------------------------------------------------------
  718. " Functions {{{1
  719. " Function: lh#common#echomsg_multilines {{{2
  720. function! lh#common#echomsg_multilines(text)
  721. let lines = split(a:text, "[\n\r]")
  722. for line in lines
  723. echomsg line
  724. endfor
  725. endfunction
  726. function! lh#common#echomsgMultilines(text)
  727. return lh#common#echomsg_multilines(a:text)
  728. endfunction
  729. " Function: lh#common#error_msg {{{2
  730. function! lh#common#error_msg(text)
  731. if has('gui_running')
  732. call confirm(a:text, '&Ok', '1', 'Error')
  733. else
  734. " echohl ErrorMsg
  735. echoerr a:text
  736. " echohl None
  737. endif
  738. endfunction
  739. function! lh#common#ErrorMsg(text)
  740. return lh#common#error_msg(a:text)
  741. endfunction
  742. " Function: lh#common#warning_msg {{{2
  743. function! lh#common#warning_msg(text)
  744. echohl WarningMsg
  745. " echomsg a:text
  746. call lh#common#echomsg_multilines(a:text)
  747. echohl None
  748. endfunction
  749. function! lh#common#WarningMsg(text)
  750. return lh#common#warning_msg(a:text)
  751. endfunction
  752. " Dependencies {{{2
  753. function! lh#common#check_deps(Symbol, File, path, plugin) " {{{3
  754. if !exists(a:Symbol)
  755. exe "runtime ".a:path.a:File
  756. if !exists(a:Symbol)
  757. call lh#common#error_msg( a:plugin.': Requires <'.a:File.'>')
  758. return 0
  759. endif
  760. endif
  761. return 1
  762. endfunction
  763. function! lh#common#CheckDeps(Symbol, File, path, plugin) " {{{3
  764. echomsg "lh#common#CheckDeps() is deprecated, use lh#common#check_deps() instead."
  765. return lh#common#check_deps(a:Symbol, a:File, a:path, a:plugin)
  766. endfunction
  767. " Functions }}}1
  768. "------------------------------------------------------------------------
  769. let &cpo=s:cpo_save
  770. "=============================================================================
  771. " vim600: set fdm=marker:
  772. autoload/lh/encoding.vim [[[1
  773. 78
  774. "=============================================================================
  775. " $Id: encoding.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  776. " File: autoload/lh/encoding.vim {{{1
  777. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  778. " <URL:http://code.google.com/p/lh-vim/>
  779. " License: GPLv3 with exceptions
  780. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  781. " Version: 3.0.0
  782. " Created: 21st Feb 2008
  783. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  784. "------------------------------------------------------------------------
  785. " Description:
  786. " Defines functions that help managing various encodings
  787. "
  788. "------------------------------------------------------------------------
  789. " Installation:
  790. " Drop it into {rtp}/autoload/lh/
  791. " Vim 7+ required.
  792. " History:
  793. " v3.0.0:
  794. " (*) GPLv3
  795. " v2.2.2:
  796. " (*) new mb_strings functions: strlen, strpart, at
  797. " v2.0.7:
  798. " (*) lh#encoding#Iconv() copied from map-tools
  799. " TODO: «missing features»
  800. " }}}1
  801. "=============================================================================
  802. let s:cpo_save=&cpo
  803. set cpo&vim
  804. "------------------------------------------------------------------------
  805. " Exported functions {{{2
  806. " Function: lh#encoding#iconv(expr, from, to) " {{{3
  807. " Unlike |iconv()|, this wrapper returns {expr} when we know no convertion can
  808. " be acheived.
  809. function! lh#encoding#iconv(expr, from, to)
  810. " call Dfunc("s:ICONV(".a:expr.','.a:from.','.a:to.')')
  811. if has('multi_byte') &&
  812. \ ( has('iconv') || has('iconv/dyn') ||
  813. \ ((a:from=~'latin1\|utf-8') && (a:to=~'latin1\|utf-8')))
  814. " call confirm('encoding: '.&enc."\nto:".a:to, "&Ok", 1)
  815. " call Dret("s:ICONV convert=".iconv(a:expr, a:from, a:to))
  816. return iconv(a:expr,a:from,a:to)
  817. else
  818. " Cannot convert
  819. " call Dret("s:ICONV no convert=".a:expr)
  820. return a:expr
  821. endif
  822. endfunction
  823. " Function: lh#encoding#at(mb_string, i) " {{{3
  824. " @return i-th character in a mb_string
  825. " @parem mb_string multi-bytes string
  826. " @param i 0-indexed position
  827. function! lh#encoding#at(mb_string, i)
  828. return matchstr(a:mb_string, '.', 0, a:i+1)
  829. endfunction
  830. " Function: lh#encoding#strpart(mb_string, pos, length) " {{{3
  831. " @return {length} extracted characters from {position} in multi-bytes string.
  832. " @parem mb_string multi-bytes string
  833. " @param p 0-indexed position
  834. " @param l length of the string to extract
  835. function! lh#encoding#strpart(mb_string, p, l)
  836. return matchstr(a:mb_string, '.\{'.a:l.'}', 0, a:p+1)
  837. endfunction
  838. " Function: lh#encoding#strlen(mb_string) " {{{3
  839. " @return the length of the multi-bytes string.
  840. function! lh#encoding#strlen(mb_string)
  841. return strlen(substitute(a:mb_string, '.', 'a', 'g'))
  842. endfunction
  843. "------------------------------------------------------------------------
  844. let &cpo=s:cpo_save
  845. "=============================================================================
  846. " vim600: set fdm=marker:
  847. autoload/lh/env.vim [[[1
  848. 78
  849. "=============================================================================
  850. " $Id: env.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  851. " File: autoload/lh/env.vim {{{1
  852. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  853. " <URL:http://code.google.com/p/lh-vim/>
  854. " License: GPLv3 with exceptions
  855. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  856. " Version: 3.0.0
  857. " Created: 19th Jul 2010
  858. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  859. "------------------------------------------------------------------------
  860. " Description:
  861. " Functions related to environment (variables)
  862. "
  863. "------------------------------------------------------------------------
  864. " Installation:
  865. " Drop this file into {rtp}/autoload/lh
  866. " Requires Vim7+
  867. " History:
  868. " v2.2.1: First Version
  869. " v3.0.0: GPLv3
  870. " TODO: «missing features»
  871. " }}}1
  872. "=============================================================================
  873. let s:cpo_save=&cpo
  874. set cpo&vim
  875. "------------------------------------------------------------------------
  876. " ## Misc Functions {{{1
  877. " # Version {{{2
  878. let s:k_version = 300
  879. function! lh#env#version()
  880. return s:k_version
  881. endfunction
  882. " # Debug {{{2
  883. let s:verbose = 0
  884. function! lh#env#verbose(...)
  885. if a:0 > 0 | let s:verbose = a:1 | endif
  886. return s:verbose
  887. endfunction
  888. function! s:Verbose(expr)
  889. if s:verbose
  890. echomsg a:expr
  891. endif
  892. endfunction
  893. function! lh#env#debug(expr)
  894. return eval(a:expr)
  895. endfunction
  896. "------------------------------------------------------------------------
  897. " ## Exported functions {{{1
  898. function! lh#env#expand_all(string)
  899. let res = ''
  900. let tail = a:string
  901. while !empty(tail)
  902. let [ all, head, var, tail; dummy ] = matchlist(tail, '\(.\{-}\)\%(${\(.\{-}\)}\)\=\(.*\)')
  903. if empty(var)
  904. let res .= tail
  905. break
  906. else
  907. let res .= head
  908. let val = eval('$'.var)
  909. let res .= val
  910. endif
  911. endwhile
  912. return res
  913. endfunction
  914. "------------------------------------------------------------------------
  915. " ## Internal functions {{{1
  916. "------------------------------------------------------------------------
  917. let &cpo=s:cpo_save
  918. "=============================================================================
  919. " vim600: set fdm=marker:
  920. autoload/lh/event.vim [[[1
  921. 70
  922. "=============================================================================
  923. " $Id: event.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  924. " File: autoload/lh/event.vim {{{1
  925. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  926. " <URL:http://code.google.com/p/lh-vim/>
  927. " License: GPLv3 with exceptions
  928. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  929. " Version: 3.0.0
  930. " Created: 15th Feb 2008
  931. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  932. "------------------------------------------------------------------------
  933. " Description:
  934. " Function to help manage vim |autocommand-events|
  935. "
  936. "------------------------------------------------------------------------
  937. " Installation:
  938. " Drop it into {rtp}/autoload/lh/
  939. " Vim 7+ required.
  940. " History:
  941. " v2.0.6: Creation
  942. " v3.0.0: GPLv3
  943. " TODO:
  944. " }}}1
  945. "=============================================================================
  946. let s:cpo_save=&cpo
  947. set cpo&vim
  948. "------------------------------------------------------------------------
  949. " ## Functions {{{1
  950. " # Debug {{{2
  951. function! lh#event#verbose(level)
  952. let s:verbose = a:level
  953. endfunction
  954. function! s:Verbose(expr)
  955. if exists('s:verbose') && s:verbose
  956. echomsg a:expr
  957. endif
  958. endfunction
  959. function! lh#event#debug(expr)
  960. return eval(a:expr)
  961. endfunction
  962. "------------------------------------------------------------------------
  963. " # Event Registration {{{2
  964. function! s:RegisteredOnce(cmd, group)
  965. " We can't delete the current augroup autocommand => increment a counter
  966. if !exists('s:'.a:group) || s:{a:group} == 0
  967. let s:{a:group} = 1
  968. exe a:cmd
  969. endif
  970. endfunction
  971. function! lh#event#register_for_one_execution_at(event, cmd, group)
  972. let group = a:group.'_once'
  973. let s:{group} = 0
  974. exe 'augroup '.group
  975. au!
  976. exe 'au '.a:event.' '.expand('%:p').' call s:RegisteredOnce('.string(a:cmd).','.string(group).')'
  977. augroup END
  978. endfunction
  979. function! lh#event#RegisterForOneExecutionAt(event, cmd, group)
  980. return lh#event#register_for_one_execution_at(a:event, a:cmd, a:group)
  981. endfunction
  982. "------------------------------------------------------------------------
  983. let &cpo=s:cpo_save
  984. "=============================================================================
  985. " vim600: set fdm=marker:
  986. autoload/lh/float.vim [[[1
  987. 120
  988. "=============================================================================
  989. " $Id: float.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  990. " File: autoload/lh/float.vim {{{1
  991. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  992. " <URL:http://code.google.com/p/lh-vim/>
  993. " License: GPLv3 with exceptions
  994. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  995. " Version: 3.0.0
  996. " Created: 16th Nov 2010
  997. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  998. "------------------------------------------------------------------------
  999. " Description:
  1000. " Defines functions related to |expr-float| numbers
  1001. "
  1002. "------------------------------------------------------------------------
  1003. " Installation:
  1004. " Drop this file into {rtp}/autoload/lh
  1005. " Requires Vim7+
  1006. " History:
  1007. " v2.0.0: first version
  1008. " v3.0.0: GPLv3
  1009. " TODO:
  1010. " }}}1
  1011. "=============================================================================
  1012. let s:cpo_save=&cpo
  1013. set cpo&vim
  1014. "------------------------------------------------------------------------
  1015. " ## Misc Functions {{{1
  1016. " # Version {{{2
  1017. let s:k_version = 300
  1018. function! lh#float#version()
  1019. return s:k_version
  1020. endfunction
  1021. " # Debug {{{2
  1022. let s:verbose = 0
  1023. function! lh#float#verbose(...)
  1024. if a:0 > 0 | let s:verbose = a:1 | endif
  1025. return s:verbose
  1026. endfunction
  1027. function! s:Verbose(expr)
  1028. if s:verbose
  1029. echomsg a:expr
  1030. endif
  1031. endfunction
  1032. function! lh#float#debug(expr)
  1033. return eval(a:expr)
  1034. endfunction
  1035. "------------------------------------------------------------------------
  1036. " ## Exported functions {{{1
  1037. " # lh#float#min(list) {{{2
  1038. function! lh#float#min(list)
  1039. let am = lh#float#arg_min(a:list)
  1040. return a:list[am]
  1041. endfunction
  1042. function! lh#float#arg_min(list)
  1043. if empty(a:list) | return -1 | endif
  1044. let m = type(a:list[0]) == type(0.0) ? a:list[0] : str2float(a:list[0])
  1045. let p = 0
  1046. let i = 1
  1047. while i != len(a:list)
  1048. let e = a:list[i]
  1049. if type(e) != type(0.0) |
  1050. let v = str2float(e)
  1051. else
  1052. let v = e
  1053. endif
  1054. if v < m
  1055. let m = v
  1056. let p = i
  1057. endif
  1058. let i += 1
  1059. endwhile
  1060. return p
  1061. endfunction
  1062. " # lh#float#max(list) {{{2
  1063. function! lh#float#max(list)
  1064. let am = lh#float#arg_max(a:list)
  1065. return a:list[am]
  1066. endfunction
  1067. function! lh#float#arg_max(list)
  1068. if empty(a:list) | return -1 | endif
  1069. let m = type(a:list[0]) == type(0.0) ? a:list[0] : str2float(a:list[0])
  1070. let p = 0
  1071. let i = 1
  1072. while i != len(a:list)
  1073. let e = a:list[i]
  1074. if type(e) != type(0.0) |
  1075. let v = str2float(e)
  1076. else
  1077. let v = e
  1078. endif
  1079. if v > m
  1080. let m = v
  1081. let p = i
  1082. endif
  1083. let i += 1
  1084. endwhile
  1085. return p
  1086. endfunction
  1087. "------------------------------------------------------------------------
  1088. " ## Internal functions {{{1
  1089. "------------------------------------------------------------------------
  1090. let &cpo=s:cpo_save
  1091. "=============================================================================
  1092. " vim600: set fdm=marker:
  1093. autoload/lh/function.vim [[[1
  1094. 217
  1095. "=============================================================================
  1096. " $Id: function.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  1097. " File: autoload/lh/function.vim {{{1
  1098. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  1099. " <URL:http://code.google.com/p/lh-vim/>
  1100. " License: GPLv3 with exceptions
  1101. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  1102. " Version: 3.0.0
  1103. " Created: 03rd Nov 2008
  1104. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  1105. "------------------------------------------------------------------------
  1106. " Description:
  1107. " Implements:
  1108. " - lh#function#bind()
  1109. " - lh#function#execute()
  1110. " - lh#function#prepare()
  1111. " - a binded function type
  1112. "
  1113. "------------------------------------------------------------------------
  1114. " Installation:
  1115. " Drop it into {rtp}/autoload/lh/
  1116. " Vim 7+ required.
  1117. " History:
  1118. " v2.2.0: first implementation
  1119. " v3.0.0: GPLv3
  1120. " TODO: «missing features»
  1121. " }}}1
  1122. "=============================================================================
  1123. let s:cpo_save=&cpo
  1124. set cpo&vim
  1125. "------------------------------------------------------------------------
  1126. " ## Functions {{{1
  1127. " # Debug {{{2
  1128. function! lh#function#verbose(level)
  1129. let s:verbose = a:level
  1130. endfunction
  1131. function! s:Verbose(expr)
  1132. if exists('s:verbose') && s:verbose
  1133. echomsg a:expr
  1134. endif
  1135. endfunction
  1136. function! lh#function#debug(expr)
  1137. return eval(a:expr)
  1138. endfunction
  1139. " # Function: s:Join(arguments...) {{{2
  1140. function! s:Join(args)
  1141. let res = ''
  1142. if len(a:args) > 0
  1143. let res = string(a:args[0])
  1144. let i = 1
  1145. while i != len(a:args)
  1146. let res.=','.string(a:args[i])
  1147. let i += 1
  1148. endwhile
  1149. endif
  1150. return res
  1151. endfunction
  1152. " # Function: s:DoBindList(arguments...) {{{2
  1153. function! s:DoBindList(formal, real)
  1154. let args = []
  1155. for arg in a:formal
  1156. if type(arg)==type('string') && arg =~ '^v:\d\+_$'
  1157. let new = a:real[matchstr(arg, 'v:\zs\d\+\ze_')-1]
  1158. elseif type(arg)==type('string')
  1159. let new = eval(s:DoBindEvaluatedString(arg, a:real))
  1160. else
  1161. let new = arg
  1162. endif
  1163. call add(args, new)
  1164. unlet new
  1165. unlet arg
  1166. endfor
  1167. return args
  1168. endfunction
  1169. " # Function: s:DoBindString(arguments...) {{{2
  1170. function! s:DoBindString(expr, real)
  1171. let expr = substitute(a:expr, '\<v:\(\d\+\)_\>', a:real.'[\1-1]', 'g')
  1172. return expr
  1173. endfunction
  1174. function! s:ToString(expr)
  1175. return type(a:expr) != type('')
  1176. \ ? string(a:expr)
  1177. \ : (a:expr)
  1178. endfunction
  1179. function! s:DoBindEvaluatedString(expr, real)
  1180. let expr = a:expr
  1181. let p = 0
  1182. while 1
  1183. let p = match(expr, '\<v:\d\+_\>', p)
  1184. if -1 == p | break | endif
  1185. let e = matchend(expr, '\<v:\d\+_\>', p)
  1186. let n = eval(expr[p+2 : e-2])
  1187. " let new = (type(a:real[n-1])==type('') && a:real[n-1]=~ '\<v:\d\+_\>')
  1188. " \ ? a:real[n-1]
  1189. " \ : string(a:real[n-1])
  1190. let new = s:ToString(a:real[n-1])
  1191. " let new = string(a:real[n-1]) " -> bind_counpound vars
  1192. let expr = ((p>0) ? (expr[0:p-1]) : '') . new . expr[e : -1]
  1193. " echo expr
  1194. let p += len(new)
  1195. " silent! unlet new
  1196. endwhile
  1197. return expr
  1198. endfunction
  1199. " # Function: s:Execute(arguments...) {{{2
  1200. function! s:Execute(args) dict
  1201. if type(self.function) == type(function('exists'))
  1202. let args = s:DoBindList(self.args, a:args)
  1203. " echomsg '##'.string(self.function).'('.join(args, ',').')'
  1204. let res = eval(string(self.function).'('.s:Join(args).')')
  1205. elseif type(self.function) == type('string')
  1206. let expr = s:DoBindString(self.function, 'a:args')
  1207. let res = eval(expr)
  1208. elseif type(self.function) == type({})
  1209. return self.function.execute(a:args)
  1210. else
  1211. throw "lh#functor#execute: unpected function type: ".type(self.function)
  1212. endif
  1213. return res
  1214. endfunction
  1215. " # Function: lh#function#prepare(function, arguments_list) {{{2
  1216. function! lh#function#prepare(Fn, arguments_list)
  1217. if type(a:Fn) == type(function('exists'))
  1218. let expr = string(a:Fn).'('.s:Join(a:arguments_list).')'
  1219. return expr
  1220. elseif type(a:Fn) == type('string')
  1221. if a:Fn =~ '^[a-zA-Z0-9_#]\+$'
  1222. let expr = string(function(a:Fn)).'('.s:Join(a:arguments_list).')'
  1223. return expr
  1224. else
  1225. let expr = s:DoBindString(a:Fn, 'a:000')
  1226. return expr
  1227. endif
  1228. else
  1229. throw "lh#function#prepare(): {Fn} argument of type ".type(a:Fn). " is unsupported"
  1230. endif
  1231. endfunction
  1232. " # Function: lh#function#execute(function, arguments...) {{{2
  1233. function! lh#function#execute(Fn, ...)
  1234. if type(a:Fn) == type({}) && has_key(a:Fn, 'execute')
  1235. return a:Fn.execute(a:000)
  1236. else
  1237. let expr = lh#function#prepare(a:Fn, a:000)
  1238. return eval(expr)
  1239. endif
  1240. endfunction
  1241. " # Function: lh#function#bind(function, arguments...) {{{2
  1242. function! lh#function#bind(Fn, ...)
  1243. let args = copy(a:000)
  1244. if type(a:Fn) == type('string') && a:Fn =~ '^[a-zA-Z0-9_#]\+$'
  1245. \ && exists('*'.a:Fn)
  1246. let Fn = function(a:Fn)
  1247. elseif type(a:Fn) == type({})
  1248. " echo string(a:Fn).'('.string(a:000).')'
  1249. " Rebinding another binded function
  1250. " TASSERT has_key(a:Fn, 'function')
  1251. " TASSERT has_key(a:Fn, 'execute')
  1252. " TASSERT has_key(a:Fn, 'args')
  1253. let Fn = a:Fn.function
  1254. let N = len(a:Fn.args)
  1255. if N != 0 " args to rebind
  1256. let i = 0
  1257. let t_args = [] " necessary to avoid type changes
  1258. while i != N
  1259. silent! unlet arg
  1260. let arg = a:Fn.args[i]
  1261. if arg =~ 'v:\d\+_$'
  1262. let arg2 = eval(s:DoBindString(arg, string(args)))
  1263. " echo arg."-(".string(args).")->".string(arg2)
  1264. unlet arg
  1265. let arg = arg2
  1266. unlet arg2
  1267. endif
  1268. call add(t_args, arg)
  1269. let i += 1
  1270. endwhile
  1271. unlet a:Fn.args
  1272. let a:Fn.args = t_args
  1273. else " expression to fix
  1274. " echo Fn
  1275. " echo s:DoBindString(Fn, string(args))
  1276. " echo eval(string(s:DoBindString(Fn, string(args))))
  1277. let Fn = (s:DoBindEvaluatedString(Fn, args))
  1278. endif
  1279. let args = a:Fn.args
  1280. else
  1281. let Fn = a:Fn
  1282. endif
  1283. let binded_fn = {
  1284. \ 'function': Fn,
  1285. \ 'args': args,
  1286. \ 'execute': function('s:Execute')
  1287. \}
  1288. return binded_fn
  1289. endfunction
  1290. " }}1
  1291. "------------------------------------------------------------------------
  1292. let &cpo=s:cpo_save
  1293. "=============================================================================
  1294. " vim600: set fdm=marker:
  1295. " Vim: let g:UTfiles='tests/lh/function.vim'
  1296. autoload/lh/graph/tsort.vim [[[1
  1297. 179
  1298. "=============================================================================
  1299. " $Id: tsort.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  1300. " File: autoload/lh/tsort.vim {{{1
  1301. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  1302. " <URL:http://code.google.com/p/lh-vim/>
  1303. " License: GPLv3 with exceptions
  1304. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  1305. " Version: 3.0.0
  1306. " Created: 21st Apr 2008
  1307. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  1308. "------------------------------------------------------------------------
  1309. " Description: Library functions for Topological Sort
  1310. "
  1311. "------------------------------------------------------------------------
  1312. " Drop the file into {rtp}/autoload/lh/graph
  1313. " History: «history»
  1314. " TODO: «missing features»
  1315. " }}}1
  1316. "=============================================================================
  1317. let s:cpo_save=&cpo
  1318. set cpo&vim
  1319. "------------------------------------------------------------------------
  1320. " ## Debug {{{1
  1321. function! lh#graph#tsort#verbose(level)
  1322. let s:verbose = a:level
  1323. endfunction
  1324. function! s:Verbose(expr)
  1325. if exists('s:verbose') && s:verbose
  1326. echomsg a:expr
  1327. endif
  1328. endfunction
  1329. function! lh#graph#tsort#debug(expr)
  1330. return eval(a:expr)
  1331. endfunction
  1332. "------------------------------------------------------------------------
  1333. "## Helper functions {{{1
  1334. "# s:Successors_fully_defined(node) {{{2
  1335. function! s:Successors_fully_defined(node) dict
  1336. if has_key(self.table, a:node)
  1337. return self.table[a:node]
  1338. else
  1339. return []
  1340. endif
  1341. endfunction
  1342. "# s:Successors_lazy(node) {{{2
  1343. function! s:Successors_lazy(node) dict
  1344. if !has_key(self.table, a:node)
  1345. let nodes = self.fetch(a:node)
  1346. let self.table[a:node] = nodes
  1347. " if len(nodes) > 0
  1348. " let self.nb += 1
  1349. " endif
  1350. return nodes
  1351. else
  1352. return self.table[a:node]
  1353. endif
  1354. endfunction
  1355. "# s:PrepareDAG(dag) {{{2
  1356. function! s:PrepareDAG(dag)
  1357. if type(a:dag) == type(function('has_key'))
  1358. let dag = {
  1359. \ 'successors': function('s:Successors_lazy'),
  1360. \ 'fetch' : a:dag,
  1361. \ 'table' : {}
  1362. \}
  1363. else
  1364. let dag = {
  1365. \ 'successors': function('s:Successors_fully_defined'),
  1366. \ 'table' : deepcopy(a:dag)
  1367. \}
  1368. endif
  1369. return dag
  1370. endfunction
  1371. "## Depth-first search (recursive) {{{1
  1372. " Do not detect cyclic graphs
  1373. "# lh#graph#tsort#depth(dag, start_nodes) {{{2
  1374. function! lh#graph#tsort#depth(dag, start_nodes)
  1375. let dag = s:PrepareDAG(a:dag)
  1376. let results = []
  1377. let visited_nodes = { 'Visited':function('s:Visited')}
  1378. call s:RecursiveDTSort(dag, a:start_nodes, results, visited_nodes)
  1379. call reverse(results)
  1380. return results
  1381. endfunction
  1382. "# The real, recursive, T-Sort {{{2
  1383. "see boost.graph for a non recursive implementation
  1384. function! s:RecursiveDTSort(dag, start_nodes, results, visited_nodes)
  1385. for node in a:start_nodes
  1386. let visited = a:visited_nodes.Visited(node)
  1387. if visited == 1 | continue " done
  1388. elseif visited == 2 | throw "Tsort: cyclic graph detected: ".node
  1389. endif
  1390. let a:visited_nodes[node] = 2 " visiting
  1391. let succs = a:dag.successors(node)
  1392. try
  1393. call s:RecursiveDTSort(a:dag, succs, a:results, a:visited_nodes)
  1394. catch /Tsort:/
  1395. throw v:exception.'>'.node
  1396. endtry
  1397. let a:visited_nodes[node] = 1 " visited
  1398. call add(a:results, node)
  1399. endfor
  1400. endfunction
  1401. function! s:Visited(node) dict
  1402. return has_key(self, a:node) ? self[a:node] : 0
  1403. endfunction
  1404. "## Breadth-first search (non recursive) {{{1
  1405. "# lh#graph#tsort#breadth(dag, start_nodes) {{{2
  1406. " warning: This implementation does not work with lazy dag, but only with fully
  1407. " defined ones
  1408. function! lh#graph#tsort#breadth(dag, start_nodes)
  1409. let result = []
  1410. let dag = s:PrepareDAG(a:dag)
  1411. let queue = deepcopy(a:start_nodes)
  1412. while len(queue) > 0
  1413. let node = remove(queue, 0)
  1414. " echomsg "result <- ".node
  1415. call add(result, node)
  1416. let successors = dag.successors(node)
  1417. while len(successors) > 0
  1418. let m = s:RemoveEdgeFrom(dag, node)
  1419. " echomsg "graph loose ".node."->".m
  1420. if !s:HasIncomingEgde(dag, m)
  1421. " echomsg "queue <- ".m
  1422. call add(queue, m)
  1423. endif
  1424. endwhile
  1425. endwhile
  1426. if !s:Empty(dag)
  1427. throw "Tsort: cyclic graph detected: "
  1428. endif
  1429. return result
  1430. endfunction
  1431. function! s:HasIncomingEgde(dag, node)
  1432. for node in keys(a:dag.table)
  1433. if type(a:dag.table[node]) != type([])
  1434. continue
  1435. endif
  1436. if index(a:dag.table[node], a:node) != -1
  1437. return 1
  1438. endif
  1439. endfor
  1440. return 0
  1441. endfunction
  1442. function! s:RemoveEdgeFrom(dag, node)
  1443. let successors = a:dag.successors(a:node)
  1444. if len(successors) > 0
  1445. let successor = remove(successors, 0)
  1446. if len(successors) == 0
  1447. " echomsg "finished with ->".a:node
  1448. call remove(a:dag.table, a:node)
  1449. endif
  1450. return successor
  1451. endif
  1452. throw "No more edges from ".a:node
  1453. endfunction
  1454. function! s:Empty(dag)
  1455. " echomsg "len="len(a:dag.table)
  1456. return len(a:dag.table) == 0
  1457. endfunction
  1458. " }}}1
  1459. "------------------------------------------------------------------------
  1460. let &cpo=s:cpo_save
  1461. "=============================================================================
  1462. " vim600: set fdm=marker
  1463. autoload/lh/icomplete.vim [[[1
  1464. 95
  1465. "=============================================================================
  1466. " $Id$
  1467. " File: autoload/lh/icomplete.vim {{{1
  1468. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  1469. " <URL:http://code.google.com/p/lh-vim/>
  1470. " License: GPLv3 with exceptions
  1471. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  1472. " Version: 3.0.0
  1473. " Created: 03rd Jan 2011
  1474. " Last Update: $Date$
  1475. "------------------------------------------------------------------------
  1476. " Description:
  1477. " Helpers functions to build |ins-completion-menu|
  1478. "
  1479. "------------------------------------------------------------------------
  1480. " Installation:
  1481. " Drop this file into {rtp}/autoload/lh
  1482. " Requires Vim7+
  1483. " History:
  1484. " v3.0.0: GPLv3
  1485. " v2.2.4: first version
  1486. " TODO:
  1487. " - We are not able to detect the end of the completion mode. As a
  1488. " consequence we can't prevent c/for<space> to trigger an abbreviation
  1489. " instead of the right template file.
  1490. " In an ideal world, there would exist an event post |complete()|
  1491. " }}}1
  1492. "=============================================================================
  1493. let s:cpo_save=&cpo
  1494. set cpo&vim
  1495. "------------------------------------------------------------------------
  1496. " ## Misc Functions {{{1
  1497. " # Version {{{2
  1498. let s:k_version = 300
  1499. function! lh#icomplete#version()
  1500. return s:k_version
  1501. endfunction
  1502. " # Debug {{{2
  1503. let s:verbose = 0
  1504. function! lh#icomplete#verbose(...)
  1505. if a:0 > 0 | let s:verbose = a:1 | endif
  1506. return s:verbose
  1507. endfunction
  1508. function! s:Verbose(expr)
  1509. if s:verbose
  1510. echomsg a:expr
  1511. endif
  1512. endfunction
  1513. function! lh#icomplete#debug(expr)
  1514. return eval(a:expr)
  1515. endfunction
  1516. "------------------------------------------------------------------------
  1517. " ## Exported functions {{{1
  1518. " Function: lh#icomplete#run(startcol, matches, Hook) {{{2
  1519. function! lh#icomplete#run(startcol, matches, Hook)
  1520. call lh#icomplete#_register_hook(a:Hook)
  1521. call complete(a:startcol, a:matches)
  1522. return ''
  1523. endfunction
  1524. "------------------------------------------------------------------------
  1525. " ## Internal functions {{{1
  1526. " Function: lh#icomplete#_clear_key_bindings() {{{2
  1527. function! lh#icomplete#_clear_key_bindings()
  1528. iunmap <cr>
  1529. iunmap <c-y>
  1530. iunmap <esc>
  1531. " iunmap <space>
  1532. " iunmap <tab>
  1533. endfunction
  1534. " Function: lh#icomplete#_register_hook(Hook) {{{2
  1535. function! lh#icomplete#_register_hook(Hook)
  1536. exe 'inoremap <silent> <cr> <cr><c-\><c-n>:call' .a:Hook . '()<cr>'
  1537. exe 'inoremap <silent> <c-y> <c-y><c-\><c-n>:call' .a:Hook . '()<cr>'
  1538. " <c-o><Nop> doesn't work as expected...
  1539. " To stay in INSERT-mode:
  1540. " inoremap <silent> <esc> <c-e><c-o>:<cr>
  1541. " To return into NORMAL-mode:
  1542. inoremap <silent> <esc> <c-e><esc>
  1543. call lh#event#register_for_one_execution_at('InsertLeave',
  1544. \ ':call lh#icomplete#_clear_key_bindings()', 'CompleteGroup')
  1545. endfunction
  1546. "------------------------------------------------------------------------
  1547. let &cpo=s:cpo_save
  1548. "=============================================================================
  1549. " vim600: set fdm=marker:
  1550. autoload/lh/list.vim [[[1
  1551. 376
  1552. "=============================================================================
  1553. " $Id: list.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  1554. " File: autoload/lh/list.vim {{{1
  1555. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  1556. " <URL:http://code.google.com/p/lh-vim/>
  1557. " License: GPLv3 with exceptions
  1558. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  1559. " Version: 3.0.0
  1560. " Created: 17th Apr 2007
  1561. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (17th Apr 2007)
  1562. "------------------------------------------------------------------------
  1563. " Description:
  1564. " Defines functions related to |Lists|
  1565. "
  1566. "------------------------------------------------------------------------
  1567. " Installation:
  1568. " Drop it into {rtp}/autoload/lh/
  1569. " Vim 7+ required.
  1570. " History:
  1571. " v3.0.0:
  1572. " (*) GPLv3
  1573. " v2.2.2:
  1574. " (*) new functions: lh#list#remove(), lh#list#matches(),
  1575. " lh#list#not_found().
  1576. " v2.2.1:
  1577. " (*) use :unlet in :for loop to support heterogeneous lists
  1578. " (*) binary search algorithms (upper_bound, lower_bound, equal_range)
  1579. " v2.2.0:
  1580. " (*) new functions: lh#list#accumulate, lh#list#transform,
  1581. " lh#list#transform_if, lh#list#find_if, lh#list#copy_if,
  1582. " lh#list#subset, lh#list#intersect
  1583. " (*) the functions are compatible with lh#function functors
  1584. " v2.1.1:
  1585. " (*) unique_sort
  1586. " v2.0.7:
  1587. " (*) Bug fix: lh#list#Match()
  1588. " v2.0.6:
  1589. " (*) lh#list#Find_if() supports search predicate, and start index
  1590. " (*) lh#list#Match() supports start index
  1591. " v2.0.0:
  1592. " TODO: «missing features»
  1593. " }}}1
  1594. "=============================================================================
  1595. "=============================================================================
  1596. let s:cpo_save=&cpo
  1597. set cpo&vim
  1598. "------------------------------------------------------------------------
  1599. " ## Functions {{{1
  1600. " # Debug {{{2
  1601. function! lh#list#verbose(level)
  1602. let s:verbose = a:level
  1603. endfunction
  1604. function! s:Verbose(expr)
  1605. if exists('s:verbose') && s:verbose
  1606. echomsg a:expr
  1607. endif
  1608. endfunction
  1609. function! lh#list#debug(expr)
  1610. return eval(a:expr)
  1611. endfunction
  1612. "------------------------------------------------------------------------
  1613. " # Public {{{2
  1614. " Function: lh#list#Transform(input, output, action) {{{3
  1615. " deprecated version
  1616. function! lh#list#Transform(input, output, action)
  1617. let new = map(copy(a:input), a:action)
  1618. let res = extend(a:output,new)
  1619. return res
  1620. for element in a:input
  1621. let action = substitute(a:action, 'v:val','element', 'g')
  1622. let res = eval(action)
  1623. call add(a:output, res)
  1624. unlet element " for heterogeneous lists
  1625. endfor
  1626. return a:output
  1627. endfunction
  1628. function! lh#list#transform(input, output, action)
  1629. for element in a:input
  1630. let res = lh#function#execute(a:action, element)
  1631. call add(a:output, res)
  1632. unlet element " for heterogeneous lists
  1633. endfor
  1634. return a:output
  1635. endfunction
  1636. function! lh#list#transform_if(input, output, action, predicate)
  1637. for element in a:input
  1638. if lh#function#execute(a:predicate, element)
  1639. let res = lh#function#execute(a:action, element)
  1640. call add(a:output, res)
  1641. endif
  1642. unlet element " for heterogeneous lists
  1643. endfor
  1644. return a:output
  1645. endfunction
  1646. function! lh#list#copy_if(input, output, predicate)
  1647. for element in a:input
  1648. if lh#function#execute(a:predicate, element)
  1649. call add(a:output, element)
  1650. endif
  1651. unlet element " for heterogeneous lists
  1652. endfor
  1653. return a:output
  1654. endfunction
  1655. function! lh#list#accumulate(input, transformation, accumulator)
  1656. let transformed = lh#list#transform(a:input, [], a:transformation)
  1657. let res = lh#function#execute(a:accumulator, transformed)
  1658. return res
  1659. endfunction
  1660. " Function: lh#list#match(list, to_be_matched [, idx]) {{{3
  1661. function! lh#list#match(list, to_be_matched, ...)
  1662. let idx = (a:0>0) ? a:1 : 0
  1663. while idx < len(a:list)
  1664. if match(a:list[idx], a:to_be_matched) != -1
  1665. return idx
  1666. endif
  1667. let idx += 1
  1668. endwhile
  1669. return -1
  1670. endfunction
  1671. function! lh#list#Match(list, to_be_matched, ...)
  1672. let idx = (a:0>0) ? a:1 : 0
  1673. return lh#list#match(a:list, a:to_be_matched, idx)
  1674. endfunction
  1675. " Function: lh#list#matches(list, to_be_matched [,idx]) {{{3
  1676. " Return the list of indices that match {to_be_matched}
  1677. function! lh#list#matches(list, to_be_matched, ...)
  1678. let res = []
  1679. let idx = (a:0>0) ? a:1 : 0
  1680. while idx < len(a:list)
  1681. if match(a:list[idx], a:to_be_matched) != -1
  1682. let res += [idx]
  1683. endif
  1684. let idx += 1
  1685. endwhile
  1686. return res
  1687. endfunction
  1688. " Function: lh#list#Find_if(list, predicate [, predicate-arguments] [, start-pos]) {{{3
  1689. function! lh#list#Find_if(list, predicate, ...)
  1690. " Parameters
  1691. let idx = 0
  1692. let args = []
  1693. if a:0 == 2
  1694. let idx = a:2
  1695. let args = a:1
  1696. elseif a:0 == 1
  1697. if type(a:1) == type([])
  1698. let args = a:1
  1699. elseif type(a:1) == type(42)
  1700. let idx = a:1
  1701. else
  1702. throw "lh#list#Find_if: unexpected argument type"
  1703. endif
  1704. elseif a:0 != 0
  1705. throw "lh#list#Find_if: unexpected number of arguments: lh#list#Find_if(list, predicate [, predicate-arguments] [, start-pos])"
  1706. endif
  1707. " The search loop
  1708. while idx != len(a:list)
  1709. let predicate = substitute(a:predicate, 'v:val', 'a:list['.idx.']', 'g')
  1710. let predicate = substitute(predicate, 'v:\(\d\+\)_', 'args[\1-1]', 'g')
  1711. let res = eval(predicate)
  1712. " echomsg string(predicate) . " --> " . res
  1713. if res | return idx | endif
  1714. let idx += 1
  1715. endwhile
  1716. return -1
  1717. endfunction
  1718. " Function: lh#list#find_if(list, predicate [, predicate-arguments] [, start-pos]) {{{3
  1719. function! lh#list#find_if(list, predicate, ...)
  1720. " Parameters
  1721. let idx = 0
  1722. let args = []
  1723. if a:0 == 1
  1724. let idx = a:1
  1725. elseif a:0 != 0
  1726. throw "lh#list#find_if: unexpected number of arguments: lh#list#find_if(list, predicate [, start-pos])"
  1727. endif
  1728. " The search loop
  1729. while idx != len(a:list)
  1730. " let predicate = substitute(a:predicate, 'v:val', 'a:list['.idx.']', 'g')
  1731. let res = lh#function#execute(a:predicate, a:list[idx])
  1732. if res | return idx | endif
  1733. let idx += 1
  1734. endwhile
  1735. return -1
  1736. endfunction
  1737. " Function: lh#list#lower_bound(sorted_list, value [, first[, last]]) {{{3
  1738. function! lh#list#lower_bound(list, val, ...)
  1739. let first = 0
  1740. let last = len(a:list)
  1741. if a:0 >= 1 | let first = a:1
  1742. elseif a:0 >= 2 | let last = a:2
  1743. elseif a:0 > 2
  1744. throw "lh#list#lower_bound: unexpected number of arguments: lh#list#lower_bound(sorted_list, value [, first[, last]])"
  1745. endif
  1746. let len = last - first
  1747. while len > 0
  1748. let half = len / 2
  1749. let middle = first + half
  1750. if a:list[middle] < a:val
  1751. let first = middle + 1
  1752. let len -= half + 1
  1753. else
  1754. let len = half
  1755. endif
  1756. endwhile
  1757. return first
  1758. endfunction
  1759. " Function: lh#list#upper_bound(sorted_list, value [, first[, last]]) {{{3
  1760. function! lh#list#upper_bound(list, val, ...)
  1761. let first = 0
  1762. let last = len(a:list)
  1763. if a:0 >= 1 | let first = a:1
  1764. elseif a:0 >= 2 | let last = a:2
  1765. elseif a:0 > 2
  1766. throw "lh#list#upper_bound: unexpected number of arguments: lh#list#upper_bound(sorted_list, value [, first[, last]])"
  1767. endif
  1768. let len = last - first
  1769. while len > 0
  1770. let half = len / 2
  1771. let middle = first + half
  1772. if a:val < a:list[middle]
  1773. let len = half
  1774. else
  1775. let first = middle + 1
  1776. let len -= half + 1
  1777. endif
  1778. endwhile
  1779. return first
  1780. endfunction
  1781. " Function: lh#list#equal_range(sorted_list, value [, first[, last]]) {{{3
  1782. " @return [f, l], where
  1783. " f : First position where {value} could be inserted
  1784. " l : Last position where {value} could be inserted
  1785. function! lh#list#equal_range(list, val, ...)
  1786. let first = 0
  1787. let last = len(a:list)
  1788. " Parameters
  1789. if a:0 >= 1 | let first = a:1
  1790. elseif a:0 >= 2 | let last = a:2
  1791. elseif a:0 > 2
  1792. throw "lh#list#equal_range: unexpected number of arguments: lh#list#equal_range(sorted_list, value [, first[, last]])"
  1793. endif
  1794. " The search loop ( == STLPort's equal_range)
  1795. let len = last - first
  1796. while len > 0
  1797. let half = len / 2
  1798. let middle = first + half
  1799. if a:list[middle] < a:val
  1800. let first = middle + 1
  1801. let len -= half + 1
  1802. elseif a:val < a:list[middle]
  1803. let len = half
  1804. else
  1805. let left = lh#list#lower_bound(a:list, a:val, first, middle)
  1806. let right = lh#list#upper_bound(a:list, a:val, middle+1, first+len)
  1807. return [left, right]
  1808. endif
  1809. " let predicate = substitute(a:predicate, 'v:val', 'a:list['.idx.']', 'g')
  1810. " let res = lh#function#execute(a:predicate, a:list[idx])
  1811. endwhile
  1812. return [first, first]
  1813. endfunction
  1814. " Function: lh#list#not_found(range) {{{3
  1815. " @return whether the range returned from equal_range is empty (i.e. element not fount)
  1816. function! lh#list#not_found(range)
  1817. return a:range[0] == a:range[1]
  1818. endfunction
  1819. " Function: lh#list#unique_sort(list [, func]) {{{3
  1820. " See also http://vim.wikia.com/wiki/Unique_sorting
  1821. "
  1822. " Works like sort(), optionally taking in a comparator (just like the
  1823. " original), except that duplicate entries will be removed.
  1824. " todo: support another argument that act as an equality predicate
  1825. function! lh#list#unique_sort(list, ...)
  1826. let dictionary = {}
  1827. for i in a:list
  1828. let dictionary[string(i)] = i
  1829. endfor
  1830. let result = []
  1831. " echo join(values(dictionary),"\n")
  1832. if ( exists( 'a:1' ) )
  1833. let result = sort( values( dictionary ), a:1 )
  1834. else
  1835. let result = sort( values( dictionary ) )
  1836. endif
  1837. return result
  1838. endfunction
  1839. function! lh#list#unique_sort2(list, ...)
  1840. let list = copy(a:list)
  1841. if ( exists( 'a:1' ) )
  1842. call sort(list, a:1 )
  1843. else
  1844. call sort(list)
  1845. endif
  1846. if len(list) <= 1 | return list | endif
  1847. let result = [ list[0] ]
  1848. let last = list[0]
  1849. let i = 1
  1850. while i < len(list)
  1851. if last != list[i]
  1852. let last = list[i]
  1853. call add(result, last)
  1854. endif
  1855. let i += 1
  1856. endwhile
  1857. return result
  1858. endfunction
  1859. " Function: lh#list#subset(list, indices) {{{3
  1860. function! lh#list#subset(list, indices)
  1861. let result=[]
  1862. for e in a:indices
  1863. call add(result, a:list[e])
  1864. endfor
  1865. return result
  1866. endfunction
  1867. " Function: lh#list#remove(list, indices) {{{3
  1868. function! lh#list#remove(list, indices)
  1869. " assert(is_sorted(indices))
  1870. let idx = reverse(copy(a:indices))
  1871. for i in idx
  1872. call remove(a:list, i)
  1873. endfor
  1874. return a:list
  1875. endfunction
  1876. " Function: lh#list#intersect(list1, list2) {{{3
  1877. function! lh#list#intersect(list1, list2)
  1878. let result = copy(a:list1)
  1879. call filter(result, 'index(a:list2, v:val) >= 0')
  1880. return result
  1881. for e in a:list1
  1882. if index(a:list2, e) > 0
  1883. call result(result, e)
  1884. endif
  1885. endfor
  1886. endfunction
  1887. " Functions }}}1
  1888. "------------------------------------------------------------------------
  1889. let &cpo=s:cpo_save
  1890. "=============================================================================
  1891. " vim600: set fdm=marker:
  1892. autoload/lh/menu.vim [[[1
  1893. 477
  1894. "=============================================================================
  1895. " $Id: menu.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  1896. " File: autoload/lh/menu.vim {{{1
  1897. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  1898. " <URL:http://code.google.com/p/lh-vim/>
  1899. " License: GPLv3 with exceptions
  1900. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  1901. " Version: 3.0.0
  1902. " Created: 13th Oct 2006
  1903. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (07th Dec 2010)
  1904. "------------------------------------------------------------------------
  1905. " Description:
  1906. " Defines the global function lh#menu#def_menu
  1907. " Aimed at (ft)plugin writers.
  1908. "
  1909. "------------------------------------------------------------------------
  1910. " Installation:
  1911. " Drop this file into {rtp}/autoload/lh/
  1912. " Requires Vim 7+
  1913. " History:
  1914. " v2.0.0: Moving to vim7
  1915. " v2.0.1: :Toggle echoes the new value
  1916. " v2.2.0: Support environment variables
  1917. " Only one :Toggle command is defined.
  1918. " v2.2.3: :Toggle can directly set the final value
  1919. " (prefer this way of proceeding to update the menu to the new
  1920. " value)
  1921. " :Toggle suports auto-completion on possible values
  1922. " v2.2.6: Toggle menus are silent, but not the actions executed
  1923. " v3.0.0: GPLv3
  1924. " TODO:
  1925. " * Should the argument to :Toggle be simplified to use the variable name
  1926. " instead ? May be a banged :Toggle! could work on the real variable
  1927. " name, and on the real value.
  1928. " * show all possible values in a sub menu (on demand)
  1929. " }}}1
  1930. "=============================================================================
  1931. "=============================================================================
  1932. let s:cpo_save=&cpo
  1933. set cpo&vim
  1934. "------------------------------------------------------------------------
  1935. " ## Internal Variables {{{1
  1936. let s:k_Toggle_cmd = 'Toggle'
  1937. if !exists('s:toggle_commands')
  1938. let s:toggle_commands = {}
  1939. endif
  1940. "------------------------------------------------------------------------
  1941. " ## Functions {{{1
  1942. " # Version {{{2
  1943. let s:k_version = 300
  1944. function! lh#menu#version()
  1945. return s:k_version
  1946. endfunction
  1947. " # Debug {{{2
  1948. let s:verbose = 0
  1949. function! lh#menu#verbose(...)
  1950. if a:0 > 0 | let s:verbose = a:1 | endif
  1951. return s:verbose
  1952. endfunction
  1953. function! s:Verbose(expr)
  1954. if exists('s:verbose') && s:verbose
  1955. echomsg a:expr
  1956. endif
  1957. endfunction
  1958. function! lh#menu#debug(expr)
  1959. return eval(a:expr)
  1960. endfunction
  1961. "------------------------------------------------------------------------
  1962. " # Common stuff {{{2
  1963. " Function: lh#menu#text({text}) {{{3
  1964. " @return a text to be used in menus where "\" and spaces have been escaped.
  1965. function! lh#menu#text(text)
  1966. return escape(a:text, '\ ')
  1967. endfunction
  1968. " # Toggling menu item {{{2
  1969. " Function: s:Fetch({Data},{key}) {{{3
  1970. " @param[in] Data Menu-item definition
  1971. " @param[in] key Table table from which the result will be fetched
  1972. " @return the current value, or text, whose index is Data.idx_crt_value.
  1973. function! s:Fetch(Data, key)
  1974. let len = len(a:Data[a:key])
  1975. if a:Data.idx_crt_value >= len | let a:Data.idx_crt_value = 0 | endif
  1976. let value = a:Data[a:key][a:Data.idx_crt_value]
  1977. return value
  1978. endfunction
  1979. " Function: s:Search({Data},{value}) {{{3
  1980. " Searches for the index of {value} in {Data.values} list. Return 0 if not
  1981. " found.
  1982. function! s:Search(Data, value)
  1983. let idx = index(a:Data.values, a:value)
  1984. " echo a:Data.variable . "[".idx."] == " . a:value
  1985. return idx > 0 ? idx : 0 " default is first element
  1986. endfunction
  1987. " Function: s:SearchText({Data},{value}) {{{3
  1988. " Searches for the index of {value} in {Data.values/text} list.
  1989. " Returns -1 if not found.
  1990. function! s:SearchText(Data, value)
  1991. let labels_key = s:MenuKey(a:Data)
  1992. let list = a:Data[labels_key]
  1993. let idx = index(list, a:value)
  1994. return idx
  1995. endfunction
  1996. " Function: s:Set({Data}) {{{3
  1997. " @param[in,out] Data Menu item definition
  1998. "
  1999. " Sets the global variable associated to the menu item according to the item's
  2000. " current value.
  2001. function! s:Set(Data)
  2002. let value = a:Data.values[a:Data.idx_crt_value]
  2003. let variable = a:Data.variable
  2004. if variable[0] == '$' " environment variabmes
  2005. exe "let ".variable." = ".string(value)
  2006. else
  2007. let g:{variable} = value
  2008. endif
  2009. if has_key(a:Data, "actions")
  2010. let l:Action = a:Data.actions[a:Data.idx_crt_value]
  2011. if type(l:Action) == type(function('tr'))
  2012. call l:Action()
  2013. else
  2014. exe l:Action
  2015. endif
  2016. endif
  2017. if has_key(a:Data, "hook")
  2018. let l:Action = a:Data.hook
  2019. if type(l:Action) == type(function('tr'))
  2020. call a:Data.hook()
  2021. else
  2022. exe l:Action
  2023. endif
  2024. endif
  2025. return value
  2026. endfunction
  2027. " Function: s:MenuKey({Data}) {{{3
  2028. " @return the table name from which the current value name (to dsplay in the
  2029. " menu) must be fetched.
  2030. " Priority is given to the optional "texts" table over the madatory "values" table.
  2031. function! s:MenuKey(Data)
  2032. if has_key(a:Data, "texts")
  2033. let menu_id = "texts"
  2034. else
  2035. let menu_id = "values"
  2036. endif
  2037. return menu_id
  2038. endfunction
  2039. " Function: s:SetTextValue({Data},{TextValue}) {{{3
  2040. " Force the value of the variable to the one associated to the {TextValue}
  2041. " The menu, and the variable are updated in consequence.
  2042. function! s:SetTextValue(Data, text)
  2043. " Where the texts for values must be fetched
  2044. let labels_key = s:MenuKey(a:Data)
  2045. " Fetch the old current value
  2046. let old = s:Fetch(a:Data, labels_key)
  2047. let new_idx = s:SearchText(a:Data, a:text)
  2048. if -1 == new_idx
  2049. throw "toggle-menu: unsupported value for {".(a:Data.variable)."}"
  2050. endif
  2051. if a:Data.idx_crt_value == new_idx
  2052. " value unchanged => abort
  2053. return
  2054. endif
  2055. " Remove the entry from the menu
  2056. call s:ClearMenu(a:Data.menu, old)
  2057. " Cycle/increment the current value
  2058. let a:Data.idx_crt_value = new_idx
  2059. " Fetch it
  2060. let new = s:Fetch(a:Data,labels_key)
  2061. " Add the updated entry in the menu
  2062. call s:UpdateMenu(a:Data.menu, new, a:Data.command)
  2063. " Update the binded global variable
  2064. let value = s:Set(a:Data)
  2065. echo a:Data.variable.'='.value
  2066. endfunction
  2067. " Function: s:NextValue({Data}) {{{3
  2068. " Change the value of the variable to the next in the list of value.
  2069. " The menu, and the variable are updated in consequence.
  2070. function! s:NextValue(Data)
  2071. " Where the texts for values must be fetched
  2072. let labels_key = s:MenuKey(a:Data)
  2073. " Fetch the old current value
  2074. let old = s:Fetch(a:Data, labels_key)
  2075. " Remove the entry from the menu
  2076. call s:ClearMenu(a:Data.menu, old)
  2077. " Cycle/increment the current value
  2078. let a:Data.idx_crt_value += 1
  2079. " Fetch it
  2080. let new = s:Fetch(a:Data,labels_key)
  2081. " Add the updated entry in the menu
  2082. call s:UpdateMenu(a:Data.menu, new, a:Data.command)
  2083. " Update the binded global variable
  2084. let value = s:Set(a:Data)
  2085. echo a:Data.variable.'='.value
  2086. endfunction
  2087. " Function: s:ClearMenu({Menu}, {text}) {{{3
  2088. " Removes a menu item
  2089. "
  2090. " @param[in] Menu.priority Priority of the new menu-item
  2091. " @param[in] Menu.name Name of the new menu-item
  2092. " @param[in] text Text of the previous value of the variable binded
  2093. function! s:ClearMenu(Menu, text)
  2094. if has('gui_running')
  2095. let name = substitute(a:Menu.name, '&', '', 'g')
  2096. let cmd = 'unmenu '.lh#menu#text(name.'<tab>('.a:text.')')
  2097. silent! exe cmd
  2098. endif
  2099. endfunction
  2100. " Function: s:UpdateMenu({Menu}, {text}, {command}) {{{3
  2101. " Adds a new menu item, with the text associated to the current value in
  2102. " braces.
  2103. "
  2104. " @param[in] Menu.priority Priority of the new menu-item
  2105. " @param[in] Menu.name Name of the new menu-item
  2106. " @param[in] text Text of the current value of the variable binded to
  2107. " the menu-item
  2108. " @param[in] command Toggle command to execute when the menu-item is selected
  2109. function! s:UpdateMenu(Menu, text, command)
  2110. if has('gui_running')
  2111. let cmd = 'nnoremenu <silent> '.a:Menu.priority.' '.
  2112. \ lh#menu#text(a:Menu.name.'<tab>('.a:text.')').
  2113. \ ' :'.s:k_Toggle_cmd.' '.a:command."\<cr>"
  2114. silent! exe cmd
  2115. endif
  2116. endfunction
  2117. " Function: s:SaveData({Data}) {{{3
  2118. " @param Data Menu-item definition
  2119. " Saves {Data} as s:Data{s:data_id++}. The definition will be used by
  2120. " automatically generated commands.
  2121. " @return s:data_id
  2122. let s:data_id = 0
  2123. function! s:SaveData(Data)
  2124. let s:Data{s:data_id} = a:Data
  2125. let id = s:data_id
  2126. let s:data_id += 1
  2127. return id
  2128. endfunction
  2129. " Function: lh#menu#def_toggle_item({Data}) {{{3
  2130. " @param Data.idx_crt_value
  2131. " @param Data.definitions == [ {value:, menutext: } ]
  2132. " @param Data.menu == { name:, position: }
  2133. "
  2134. " Sets a toggle-able menu-item defined by {Data}.
  2135. "
  2136. function! lh#menu#def_toggle_item(Data)
  2137. " Save the menu data as an internal script variable
  2138. let id = s:SaveData(a:Data)
  2139. " If the index of the current value hasn't been set, fetch it from the
  2140. " associated variable
  2141. if !has_key(a:Data, "idx_crt_value")
  2142. " Fetch the value of the associated variable
  2143. let value = lh#option#get(a:Data.variable, 0, 'g')
  2144. " echo a:Data.variable . " <- " . value
  2145. " Update the index of the current value
  2146. let a:Data.idx_crt_value = s:Search(a:Data, value)
  2147. endif
  2148. " Name of the auto-matically generated toggle command
  2149. let cmdName = substitute(a:Data.menu.name, '[^a-zA-Z_]', '', 'g')
  2150. " Lazy definition of the command
  2151. if 2 != exists(':'.s:k_Toggle_cmd)
  2152. exe 'command! -nargs=+ -complete=custom,lh#menu#_toggle_complete '
  2153. \ . s:k_Toggle_cmd . ' :call s:Toggle(<f-args>)'
  2154. endif
  2155. " silent exe 'command! -nargs=0 '.cmdName.' :call s:NextValue(s:Data'.id.')'
  2156. let s:toggle_commands[cmdName] = eval('s:Data'.id)
  2157. let a:Data["command"] = cmdName
  2158. " Add the menu entry according to the current value
  2159. call s:UpdateMenu(a:Data.menu, s:Fetch(a:Data, s:MenuKey(a:Data)), cmdName)
  2160. " Update the associated global variable
  2161. call s:Set(a:Data)
  2162. endfunction
  2163. "------------------------------------------------------------------------
  2164. function! s:Toggle(cmdName, ...)
  2165. if !has_key(s:toggle_commands, a:cmdName)
  2166. throw "toggle-menu: unknown toggable variable ".a:cmdName
  2167. endif
  2168. let data = s:toggle_commands[a:cmdName]
  2169. if a:0 > 0
  2170. call s:SetTextValue(data, a:1)
  2171. else
  2172. call s:NextValue(data)
  2173. endif
  2174. endfunction
  2175. function! lh#menu#_toggle_complete(ArgLead, CmdLine, CursorPos)
  2176. let cmdline = split(a:CmdLine)
  2177. " echomsg "cmd line: " . string(cmdline)." # ". (a:CmdLine =~ ' $')
  2178. let nb_args = len(cmdline)
  2179. if (a:CmdLine !~ ' $')
  2180. let nb_args -= 1
  2181. endif
  2182. " echomsg "nb args: ". nb_args
  2183. if nb_args < 2
  2184. return join(keys(s:toggle_commands),"\n")
  2185. elseif nb_args == 2
  2186. let variable = cmdline[1]
  2187. if !has_key(s:toggle_commands, variable)
  2188. throw "toggle-menu: unknown toggable variable ".variable
  2189. endif
  2190. let data = s:toggle_commands[variable]
  2191. let labels_key = s:MenuKey(data)
  2192. " echomsg "keys: ".string(data[labels_key])
  2193. return join(data[labels_key], "\n")
  2194. else
  2195. return ''
  2196. endif
  2197. endfunction
  2198. "------------------------------------------------------------------------
  2199. " # IVN Menus {{{2
  2200. " Function: s:CTRL_O({cmd}) {{{3
  2201. " Build the command (sequence of ':ex commands') to be executed from
  2202. " INSERT-mode.
  2203. function! s:CTRL_O(cmd)
  2204. return substitute(a:cmd, '\(^\|<CR>\):', '\1\<C-O>:', 'g')
  2205. endfunction
  2206. " Function: lh#menu#is_in_visual_mode() {{{3
  2207. function! lh#menu#is_in_visual_mode()
  2208. return exists('s:is_in_visual_mode') && s:is_in_visual_mode
  2209. endfunction
  2210. " Function: lh#menu#_CMD_and_clear_v({cmd}) {{{3
  2211. " Internal function that executes the command and then clears the @v buffer
  2212. " todo: save and restore @v,
  2213. function! lh#menu#_CMD_and_clear_v(cmd)
  2214. try
  2215. let s:is_in_visual_mode = 1
  2216. exe a:cmd
  2217. finally
  2218. let @v=''
  2219. silent! unlet s:is_in_visual_mode
  2220. endtry
  2221. endfunction
  2222. " Function: s:Build_CMD({prefix},{cmd}) {{{3
  2223. " build the exact command to execute regarding the mode it is dedicated
  2224. function! s:Build_CMD(prefix, cmd)
  2225. if a:cmd[0] != ':' | return ' ' . a:cmd
  2226. endif
  2227. if a:prefix[0] == "i" | return ' ' . <SID>CTRL_O(a:cmd)
  2228. elseif a:prefix[0] == "n" | return ' ' . a:cmd
  2229. elseif a:prefix[0] == "v"
  2230. if match(a:cmd, ":VCall") == 0
  2231. return substitute(a:cmd, ':VCall', ' :call', ''). "\<cr>gV"
  2232. " gV exit select-mode if we where in it!
  2233. else
  2234. return
  2235. \ " \"vy\<C-C>:call lh#menu#_CMD_and_clear_v('" .
  2236. \ substitute(a:cmd, "<CR>$", '', '') ."')\<cr>"
  2237. endif
  2238. elseif a:prefix[0] == "c" | return " \<C-C>" . a:cmd
  2239. else | return ' ' . a:cmd
  2240. endif
  2241. endfunction
  2242. " Function: lh#menu#map_all({map_type}, [{map args}...) {{{3
  2243. " map the command to all the modes required
  2244. function! lh#menu#map_all(map_type,...)
  2245. let nore = (match(a:map_type, '[aincv]*noremap') != -1) ? "nore" : ""
  2246. let prefix = matchstr(substitute(a:map_type, nore, '', ''), '[aincv]*')
  2247. if a:1 == "<buffer>" | let i = 3 | let binding = a:1 . ' ' . a:2
  2248. else | let i = 2 | let binding = a:1
  2249. endif
  2250. let binding = '<silent> ' . binding
  2251. let cmd = a:{i}
  2252. let i += 1
  2253. while i <= a:0
  2254. let cmd .= ' ' . a:{i}
  2255. let i += 1
  2256. endwhile
  2257. let build_cmd = nore . 'map ' . binding
  2258. while strlen(prefix)
  2259. if prefix[0] == "a" | let prefix = "incv"
  2260. else
  2261. execute prefix[0] . build_cmd . <SID>Build_CMD(prefix[0],cmd)
  2262. let prefix = strpart(prefix, 1)
  2263. endif
  2264. endwhile
  2265. endfunction
  2266. " Function: lh#menu#make({prefix},{code},{text},{binding},...) {{{3
  2267. " Build the menu and map its associated binding to all the modes required
  2268. function! lh#menu#make(prefix, code, text, binding, ...)
  2269. let nore = (match(a:prefix, '[aincv]*nore') != -1) ? "nore" : ""
  2270. let prefix = matchstr(substitute(a:prefix, nore, '', ''), '[aincv]*')
  2271. let b = (a:1 == "<buffer>") ? 1 : 0
  2272. let i = b + 1
  2273. let cmd = a:{i}
  2274. let i += 1
  2275. while i <= a:0
  2276. let cmd .= ' ' . a:{i}
  2277. let i += 1
  2278. endwhile
  2279. let build_cmd = nore . "menu <silent> " . a:code . ' ' . lh#menu#text(a:text)
  2280. if strlen(a:binding) != 0
  2281. let build_cmd .= '<tab>' .
  2282. \ substitute(lh#menu#text(a:binding), '&', '\0\0', 'g')
  2283. if prefix == 'i' && exists('*IMAP')
  2284. if b != 0
  2285. call IMAP(a:binding, cmd, &ft)
  2286. else
  2287. call IMAP(a:binding, cmd)
  2288. endif
  2289. else
  2290. if b != 0
  2291. call lh#menu#map_all(prefix.nore."map", ' <buffer> '.a:binding, cmd)
  2292. else
  2293. call lh#menu#map_all(prefix.nore."map", a:binding, cmd)
  2294. endif
  2295. endif
  2296. endif
  2297. if has("gui_running")
  2298. while strlen(prefix)
  2299. execute <SID>BMenu(b).prefix[0].build_cmd.<SID>Build_CMD(prefix[0],cmd)
  2300. let prefix = strpart(prefix, 1)
  2301. endwhile
  2302. endif
  2303. endfunction
  2304. " Function: s:BMenu({b}) {{{3
  2305. " If <buffermenu.vim> is installed and the menu should be local, then the
  2306. " apropriate string is returned.
  2307. function! s:BMenu(b)
  2308. let res = (a:b && exists(':Bmenu')
  2309. \ && (1 == lh#option#get("want_buffermenu_or_global_disable", 1, "bg"))
  2310. \) ? 'B' : ''
  2311. " call confirm("BMenu(".a:b.")=".res, '&Ok', 1)
  2312. return res
  2313. endfunction
  2314. " Function: lh#menu#IVN_make(...) {{{3
  2315. function! lh#menu#IVN_make(code, text, binding, i_cmd, v_cmd, n_cmd, ...)
  2316. " nore options
  2317. let nore_i = (a:0 > 0) ? ((a:1 != 0) ? 'nore' : '') : ''
  2318. let nore_v = (a:0 > 1) ? ((a:2 != 0) ? 'nore' : '') : ''
  2319. let nore_n = (a:0 > 2) ? ((a:3 != 0) ? 'nore' : '') : ''
  2320. "
  2321. call lh#menu#make('i'.nore_i,a:code,a:text, a:binding, '<buffer>', a:i_cmd)
  2322. call lh#menu#make('v'.nore_v,a:code,a:text, a:binding, '<buffer>', a:v_cmd)
  2323. if strlen(a:n_cmd) != 0
  2324. call lh#menu#make('n'.nore_n,a:code,a:text, a:binding, '<buffer>', a:n_cmd)
  2325. endif
  2326. endfunction
  2327. "
  2328. " Functions }}}1
  2329. "------------------------------------------------------------------------
  2330. let &cpo=s:cpo_save
  2331. "=============================================================================
  2332. " vim600: set fdm=marker:
  2333. autoload/lh/option.vim [[[1
  2334. 127
  2335. "=============================================================================
  2336. " $Id: option.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  2337. " File: autoload/lh/option.vim {{{1
  2338. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  2339. " <URL:http://code.google.com/p/lh-vim/>
  2340. " License: GPLv3 with exceptions
  2341. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  2342. " Version: 3.0.0
  2343. " Created: 24th Jul 2004
  2344. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (07th Oct 2006)
  2345. "------------------------------------------------------------------------
  2346. " Description:
  2347. " Defines the global function lh#option#get().
  2348. " Aimed at (ft)plugin writers.
  2349. "
  2350. "------------------------------------------------------------------------
  2351. " Installation:
  2352. " Drop this file into {rtp}/autoload/lh/
  2353. " Requires Vim 7+
  2354. " History:
  2355. " v3.0.0
  2356. " (*) GPLv3
  2357. " v2.0.6
  2358. " (*) lh#option#add() add values to a vim list |option|
  2359. " v2.0.5
  2360. " (*) lh#option#get_non_empty() manages Lists and Dictionaries
  2361. " (*) lh#option#get() doesn't test emptyness anymore
  2362. " v2.0.0
  2363. " Code moved from {rtp}/macros/
  2364. " }}}1
  2365. "=============================================================================
  2366. "=============================================================================
  2367. let s:cpo_save=&cpo
  2368. set cpo&vim
  2369. "------------------------------------------------------------------------
  2370. " ## Functions {{{1
  2371. " # Debug {{{2
  2372. function! lh#option#verbose(level)
  2373. let s:verbose = a:level
  2374. endfunction
  2375. function! s:Verbose(expr)
  2376. if exists('s:verbose') && s:verbose
  2377. echomsg a:expr
  2378. endif
  2379. endfunction
  2380. function! lh#option#debug(expr)
  2381. return eval(a:expr)
  2382. endfunction
  2383. " # Public {{{2
  2384. " Function: lh#option#get(name, default [, scope]) {{{3
  2385. " @return b:{name} if it exists, or g:{name} if it exists, or {default}
  2386. " otherwise
  2387. " The order of the variables checked can be specified through the optional
  2388. " argument {scope}
  2389. function! lh#option#get(name,default,...)
  2390. let scope = (a:0 == 1) ? a:1 : 'bg'
  2391. let name = a:name
  2392. let i = 0
  2393. while i != strlen(scope)
  2394. if exists(scope[i].':'.name)
  2395. " \ && (0 != strlen({scope[i]}:{name}))
  2396. return {scope[i]}:{name}
  2397. endif
  2398. let i += 1
  2399. endwhile
  2400. return a:default
  2401. endfunction
  2402. function! lh#option#Get(name,default,...)
  2403. let scope = (a:0 == 1) ? a:1 : 'bg'
  2404. return lh#option#get(a:name, a:default, scope)
  2405. endfunction
  2406. function! s:IsEmpty(variable)
  2407. if type(a:variable) == type('string') | return 0 == strlen(a:variable)
  2408. elseif type(a:variable) == type(42) | return 0 == a:variable
  2409. elseif type(a:variable) == type([]) | return 0 == len(a:variable)
  2410. elseif type(a:variable) == type({}) | return 0 == len(a:variable)
  2411. else | return false
  2412. endif
  2413. endfunction
  2414. " Function: lh#option#get_non_empty(name, default [, scope]) {{{3
  2415. " @return of b:{name}, g:{name}, or {default} the first which exists and is not empty
  2416. " The order of the variables checked can be specified through the optional
  2417. " argument {scope}
  2418. function! lh#option#get_non_empty(name,default,...)
  2419. let scope = (a:0 == 1) ? a:1 : 'bg'
  2420. let name = a:name
  2421. let i = 0
  2422. while i != strlen(scope)
  2423. if exists(scope[i].':'.name) && !s:IsEmpty({scope[i]}:{name})
  2424. return {scope[i]}:{name}
  2425. endif
  2426. let i += 1
  2427. endwhile
  2428. return a:default
  2429. endfunction
  2430. function! lh#option#GetNonEmpty(name,default,...)
  2431. let scope = (a:0 == 1) ? a:1 : 'bg'
  2432. return lh#option#get_non_empty(a:name, a:default, scope)
  2433. endfunction
  2434. " Function: lh#option#add(name, values) {{{3
  2435. " Add fields to a vim option.
  2436. " @param values list of values to add
  2437. " @example let lh#option#add('l:tags', ['.tags'])
  2438. function! lh#option#add(name,values)
  2439. let values = type(a:values) == type([])
  2440. \ ? copy(a:values)
  2441. \ : [a:values]
  2442. let old = split(eval('&'.a:name), ',')
  2443. let new = filter(values, 'match(old, v:val) < 0')
  2444. let val = join(old+new, ',')
  2445. exe 'let &'.a:name.' = val'
  2446. endfunction
  2447. " Functions }}}1
  2448. "------------------------------------------------------------------------
  2449. let &cpo=s:cpo_save
  2450. "=============================================================================
  2451. " vim600: set fdm=marker:
  2452. autoload/lh/path.vim [[[1
  2453. 366
  2454. "=============================================================================
  2455. " $Id: path.vim 606 2012-05-31 17:09:46Z luc.hermitte@gmail.com $
  2456. " File: autoload/lh/path.vim {{{1
  2457. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  2458. " <URL:http://code.google.com/p/lh-vim/>
  2459. " License: GPLv3 with exceptions
  2460. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  2461. " Version: 3.1.1
  2462. " Created: 23rd Jan 2007
  2463. " Last Update: $Date
  2464. "------------------------------------------------------------------------
  2465. " Description:
  2466. " Functions related to the handling of pathnames
  2467. "
  2468. "------------------------------------------------------------------------
  2469. " Installation:
  2470. " Drop this file into {rtp}/autoload/lh
  2471. " Requires Vim7+
  2472. " History:
  2473. " v 1.0.0 First Version
  2474. " (*) Functions moved from searchInRuntimeTime
  2475. " v 2.0.1
  2476. " (*) lh#path#Simplify() becomes like |simplify()| except for trailing
  2477. " v 2.0.2
  2478. " (*) lh#path#SelectOne()
  2479. " (*) lh#path#ToRelative()
  2480. " v 2.0.3
  2481. " (*) lh#path#GlobAsList()
  2482. " v 2.0.4
  2483. " (*) lh#path#StripStart()
  2484. " v 2.0.5
  2485. " (*) lh#path#StripStart() interprets '.' as getcwd()
  2486. " v 2.2.0
  2487. " (*) new functions: lh#path#common(), lh#path#to_dirname(),
  2488. " lh#path#depth(), lh#path#relative_to(), lh#path#to_regex(),
  2489. " lh#path#find()
  2490. " (*) lh#path#simplify() fixed
  2491. " (*) lh#path#to_relative() use simplify()
  2492. " v 2.2.2
  2493. " (*) lh#path#strip_common() fixed
  2494. " (*) lh#path#simplify() new optional parameter: make_relative_to_pwd
  2495. " v 2.2.5
  2496. " (*) fix lh#path#to_dirname('') -> return ''
  2497. " v 2.2.6
  2498. " (*) fix lh#path#glob_as_list() does not return the same path several
  2499. " times
  2500. " v 2.2.7
  2501. " (*) fix lh#path#strip_start() to strip as much as possible.
  2502. " (*) lh#path#glob_as_list() changed to handle **
  2503. " v 3.0.0
  2504. " (*) GPLv3
  2505. " v 3.1.0
  2506. " (*) lh#path#glob_as_list accepts a new option: mustSort which value
  2507. " true by default.
  2508. " v 3.1.1
  2509. " (*) lh#path#strip_start() shall support very big lists of dirnames now.
  2510. " TODO:
  2511. " (*) Decide what #depth('../../bar') shall return
  2512. " (*) Fix #simplify('../../bar')
  2513. " }}}1
  2514. "=============================================================================
  2515. "=============================================================================
  2516. " Avoid global reinclusion {{{1
  2517. let s:cpo_save=&cpo
  2518. set cpo&vim
  2519. "=============================================================================
  2520. " ## Functions {{{1
  2521. " # Version {{{2
  2522. let s:k_version = 310
  2523. function! lh#path#version()
  2524. return s:k_version
  2525. endfunction
  2526. " # Debug {{{2
  2527. let s:verbose = 0
  2528. function! lh#path#verbose(...)
  2529. if a:0 > 0 | let s:verbose = a:1 | endif
  2530. return s:verbose
  2531. endfunction
  2532. function! s:Verbose(expr)
  2533. if s:verbose
  2534. echomsg a:expr
  2535. endif
  2536. endfunction
  2537. function! lh#path#debug(expr)
  2538. return eval(a:expr)
  2539. endfunction
  2540. "=============================================================================
  2541. " # Public {{{2
  2542. " Function: lh#path#simplify({pathname}, [make_relative_to_pwd=true]) {{{3
  2543. " Like |simplify()|, but also strip the leading './'
  2544. " It seems unable to simplify '..\' when compiled without +shellslash
  2545. function! lh#path#simplify(pathname, ...)
  2546. let make_relative_to_pwd = a:0 == 0 || a:1 == 1
  2547. let pathname = simplify(a:pathname)
  2548. let pathname = substitute(pathname, '^\%(\.[/\\]\)\+', '', '')
  2549. let pathname = substitute(pathname, '\([/\\]\)\%(\.[/\\]\)\+', '\1', 'g')
  2550. if make_relative_to_pwd
  2551. let pwd = getcwd().'/'
  2552. let pathname = substitute(pathname, '^'.lh#path#to_regex(pwd), '', 'g')
  2553. endif
  2554. return pathname
  2555. endfunction
  2556. function! lh#path#Simplify(pathname)
  2557. return lh#path#simplify(a:pathname)
  2558. endfunction
  2559. " Function: lh#path#common({pathnames}) {{{3
  2560. " Find the common leading path between all pathnames
  2561. function! lh#path#common(pathnames)
  2562. " assert(len(pathnames)) > 1
  2563. let common = a:pathnames[0]
  2564. let i = 1
  2565. while i < len(a:pathnames)
  2566. let fcrt = a:pathnames[i]
  2567. " pathnames should not contain @
  2568. " let common = matchstr(common.'@@'.fcrt, '^\zs\(.*[/\\]\)\ze.\{-}@@\1.*$')
  2569. let common = matchstr(common.'@@'.fcrt, '^\zs\(.*\>\)\ze.\{-}@@\1\>.*$')
  2570. if strlen(common) == 0
  2571. " No need to further checks
  2572. break
  2573. endif
  2574. let i += 1
  2575. endwhile
  2576. return common
  2577. endfunction
  2578. " Function: lh#path#strip_common({pathnames}) {{{3
  2579. " Find the common leading path between all pathnames, and strip it
  2580. function! lh#path#strip_common(pathnames)
  2581. " assert(len(pathnames)) > 1
  2582. let common = lh#path#common(a:pathnames)
  2583. let common = lh#path#to_dirname(common)
  2584. let l = strlen(common)
  2585. if l == 0
  2586. return a:pathnames
  2587. else
  2588. let pathnames = a:pathnames
  2589. call map(pathnames, 'strpart(v:val, '.l.')' )
  2590. return pathnames
  2591. endif
  2592. endfunction
  2593. function! lh#path#StripCommon(pathnames)
  2594. return lh#path#strip_common(a:pathnames)
  2595. endfunction
  2596. " Function: lh#path#is_absolute_path({path}) {{{3
  2597. function! lh#path#is_absolute_path(path)
  2598. return a:path =~ '^/'
  2599. \ . '\|^[a-zA-Z]:[/\\]'
  2600. \ . '\|^[/\\]\{2}'
  2601. " Unix absolute path
  2602. " or Windows absolute path
  2603. " or UNC path
  2604. endfunction
  2605. function! lh#path#IsAbsolutePath(path)
  2606. return lh#path#is_absolute_path(a:path)
  2607. endfunction
  2608. " Function: lh#path#is_url({path}) {{{3
  2609. function! lh#path#is_url(path)
  2610. " todo: support UNC paths and other urls
  2611. return a:path =~ '^\%(https\=\|s\=ftp\|dav\|fetch\|file\|rcp\|rsynch\|scp\)://'
  2612. endfunction
  2613. function! lh#path#IsURL(path)
  2614. return lh#path#is_url(a:path)
  2615. endfunction
  2616. " Function: lh#path#select_one({pathnames},{prompt}) {{{3
  2617. function! lh#path#select_one(pathnames, prompt)
  2618. if len(a:pathnames) > 1
  2619. let simpl_pathnames = deepcopy(a:pathnames)
  2620. let simpl_pathnames = lh#path#strip_common(simpl_pathnames)
  2621. let simpl_pathnames = [ '&Cancel' ] + simpl_pathnames
  2622. " Consider guioptions+=c is case of difficulties with the gui
  2623. let selection = confirm(a:prompt, join(simpl_pathnames,"\n"), 1, 'Question')
  2624. let file = (selection == 1) ? '' : a:pathnames[selection-2]
  2625. return file
  2626. elseif len(a:pathnames) == 0
  2627. return ''
  2628. else
  2629. return a:pathnames[0]
  2630. endif
  2631. endfunction
  2632. function! lh#path#SelectOne(pathnames, prompt)
  2633. return lh#path#select_one(a:pathnames, a:prompt)
  2634. endfunction
  2635. " Function: lh#path#to_relative({pathname}) {{{3
  2636. function! lh#path#to_relative(pathname)
  2637. let newpath = fnamemodify(a:pathname, ':p:.')
  2638. let newpath = simplify(newpath)
  2639. return newpath
  2640. endfunction
  2641. function! lh#path#ToRelative(pathname)
  2642. return lh#path#to_relative(a:pathname)
  2643. endfunction
  2644. " Function: lh#path#to_dirname({dirname}) {{{3
  2645. " todo: use &shellslash
  2646. function! lh#path#to_dirname(dirname)
  2647. let dirname = a:dirname . (empty(a:dirname) || a:dirname[-1:] =~ '[/\\]' ? '' : '/')
  2648. return dirname
  2649. endfunction
  2650. " Function: lh#path#depth({dirname}) {{{3
  2651. " todo: make a choice about "negative" paths like "../../foo"
  2652. function! lh#path#depth(dirname)
  2653. if empty(a:dirname) | return 0 | endif
  2654. let dirname = lh#path#to_dirname(a:dirname)
  2655. let dirname = lh#path#simplify(dirname)
  2656. if lh#path#is_absolute_path(dirname)
  2657. let dirname = matchstr(dirname, '.\{-}[/\\]\zs.*')
  2658. endif
  2659. let depth = len(substitute(dirname, '[^/\\]\+[/\\]', '#', 'g'))
  2660. return depth
  2661. endfunction
  2662. " Function: lh#path#relative_to({from}, {to}) {{{3
  2663. " @param two directories
  2664. " @return a directories delta that ends with a '/' (may depends on
  2665. " &shellslash)
  2666. function! lh#path#relative_to(from, to)
  2667. " let from = fnamemodify(a:from, ':p')
  2668. " let to = fnamemodify(a:to , ':p')
  2669. let from = lh#path#to_dirname(a:from)
  2670. let to = lh#path#to_dirname(a:to )
  2671. let [from, to] = lh#path#strip_common([from, to])
  2672. let nb_up = lh#path#depth(from)
  2673. return repeat('../', nb_up).to
  2674. " cannot rely on :cd (as it alters things, and doesn't work with
  2675. " non-existant paths)
  2676. let pwd = getcwd()
  2677. exe 'cd '.a:to
  2678. let res = lh#path#to_relative(a:from)
  2679. exe 'cd '.pwd
  2680. return res
  2681. endfunction
  2682. " Function: lh#path#glob_as_list({pathslist}, {expr} [, mustSort=1]) {{{3
  2683. function! s:GlobAsList(pathslist, expr, mustSort)
  2684. let pathslist = type(a:pathslist) == type([]) ? join(a:pathslist, ',') : a:pathslist
  2685. let sResult = globpath(pathslist, a:expr)
  2686. let lResult = split(sResult, '\n')
  2687. " workaround a non feature of wildignore: it does not ignore directories
  2688. for ignored_pattern in split(&wildignore,',')
  2689. if stridx(ignored_pattern,'/') != -1
  2690. call filter(lResult, 'v:val !~ '.string(ignored_pattern))
  2691. endif
  2692. endfor
  2693. return a:mustSort ? lh#list#unique_sort(lResult) : lResult
  2694. endfunction
  2695. function! lh#path#glob_as_list(pathslist, expr, ...)
  2696. let mustSort = (a:0 > 0) ? (a:1) : 0
  2697. if type(a:expr) == type('string')
  2698. return s:GlobAsList(a:pathslist, a:expr, mustSort)
  2699. elseif type(a:expr) == type([])
  2700. let res = []
  2701. for expr in a:expr
  2702. call extend(res, s:GlobAsList(a:pathslist, expr, mustSort))
  2703. endfor
  2704. return res
  2705. else
  2706. throw "Unexpected type for a:expression"
  2707. endif
  2708. endfunction
  2709. function! lh#path#GlobAsList(pathslist, expr)
  2710. return lh#path#glob_as_list(a:pathslist, a:expr)
  2711. endfunction
  2712. " Function: lh#path#strip_start({pathname}, {pathslist}) {{{3
  2713. " Strip occurrence of paths from {pathslist} in {pathname}
  2714. " @param[in] {pathname} name to simplify
  2715. " @param[in] {pathslist} list of pathname (can be a |string| of pathnames
  2716. " separated by ",", of a |List|).
  2717. function! lh#path#strip_start(pathname, pathslist)
  2718. if type(a:pathslist) == type('string')
  2719. " let strip_re = escape(a:pathslist, '\\.')
  2720. " let strip_re = '^' . substitute(strip_re, ',', '\\|^', 'g')
  2721. let pathslist = split(a:pathslist, ',')
  2722. elseif type(a:pathslist) == type([])
  2723. let pathslist = deepcopy(a:pathslist)
  2724. else
  2725. throw "Unexpected type for a:pathname"
  2726. endif
  2727. " apply a realpath like operation
  2728. let nb_paths = len(pathslist) " set before the loop
  2729. let i = 0
  2730. while i != nb_paths
  2731. if pathslist[i] =~ '^\.\%(/\|$\)'
  2732. let path2 = getcwd().pathslist[i][1:]
  2733. call add(pathslist, path2)
  2734. endif
  2735. let i += 1
  2736. endwhile
  2737. " replace path separators by a regex that can match them
  2738. call map(pathslist, 'substitute(v:val, "[\\\\/]", "[\\\\/]", "g")')
  2739. " echomsg string(pathslist)
  2740. " escape .
  2741. call map(pathslist, '"^".escape(v:val, ".")')
  2742. " handle "**" as anything
  2743. call map(pathslist, 'substitute(v:val, "\\*\\*", "\\\\%([^\\\\/]*[\\\\/]\\\\)*", "g")')
  2744. " reverse the list to use the real best match, which is "after"
  2745. call reverse(pathslist)
  2746. if 0
  2747. " build the strip regex
  2748. let strip_re = join(pathslist, '\|')
  2749. " echomsg strip_re
  2750. let best_match = substitute(a:pathname, '\%('.strip_re.'\)[/\\]\=', '', '')
  2751. else
  2752. let best_match = ''
  2753. for path in pathslist
  2754. let a_match = substitute(a:pathname, '\%('.path.'\)[/\\]\=', '', '')
  2755. if len(a_match) < len(best_match) || empty(best_match)
  2756. let best_match = a_match
  2757. endif
  2758. endfor
  2759. endif
  2760. return best_match
  2761. endfunction
  2762. function! lh#path#StripStart(pathname, pathslist)
  2763. return lh#path#strip_start(a:pathname, a:pathslist)
  2764. endfunction
  2765. " Function: lh#path#to_regex({pathname}) {{{3
  2766. function! lh#path#to_regex(path)
  2767. let regex = substitute(a:path, '[/\\]', '[/\\\\]', 'g')
  2768. return regex
  2769. endfunction
  2770. " Function: lh#path#find({pathname}, {regex}) {{{3
  2771. function! lh#path#find(paths, regex)
  2772. let paths = (type(a:paths) == type([]))
  2773. \ ? (a:paths)
  2774. \ : split(a:paths,',')
  2775. for path in paths
  2776. if match(path ,a:regex) != -1
  2777. return path
  2778. endif
  2779. endfor
  2780. return ''
  2781. endfunction
  2782. " Function: lh#path#vimfiles() {{{3
  2783. function! lh#path#vimfiles()
  2784. let expected_win = $HOME . '/vimfiles'
  2785. let expected_nix = $HOME . '/.vim'
  2786. let what = lh#path#to_regex($HOME.'/').'\(vimfiles\|.vim\)'
  2787. " Comment what
  2788. let z = lh#path#find(&rtp,what)
  2789. return z
  2790. endfunction
  2791. " }}}1
  2792. "=============================================================================
  2793. let &cpo=s:cpo_save
  2794. "=============================================================================
  2795. " vim600: set fdm=marker:
  2796. autoload/lh/position.vim [[[1
  2797. 96
  2798. "=============================================================================
  2799. " $Id: position.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  2800. " File: autoload/lh/position.vim {{{1
  2801. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  2802. " <URL:http://code.google.com/p/lh-vim/>
  2803. " License: GPLv3 with exceptions
  2804. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  2805. " Version: 3.0.0
  2806. " Created: 05th Sep 2007
  2807. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (05th Sep 2007)
  2808. "------------------------------------------------------------------------
  2809. " Description: «description»
  2810. "
  2811. "------------------------------------------------------------------------
  2812. " Installation:
  2813. " Drop it into {rtp}/autoload/lh/
  2814. " Vim 7+ required.
  2815. " History: «history»
  2816. " v1.0.0:
  2817. " Creation
  2818. " v3.0.0: GPLv3
  2819. " TODO:
  2820. " }}}1
  2821. "=============================================================================
  2822. "=============================================================================
  2823. let s:cpo_save=&cpo
  2824. set cpo&vim
  2825. "------------------------------------------------------------------------
  2826. " ## Functions {{{1
  2827. " # Debug {{{2
  2828. function! lh#position#verbose(level)
  2829. let s:verbose = a:level
  2830. endfunction
  2831. function! s:Verbose(expr)
  2832. if exists('s:verbose') && s:verbose
  2833. echomsg a:expr
  2834. endif
  2835. endfunction
  2836. function! lh#position#debug(expr)
  2837. return eval(a:expr)
  2838. endfunction
  2839. "------------------------------------------------------------------------
  2840. " # Public {{{2
  2841. " Function: lh#position#is_before {{{3
  2842. " @param[in] positions as those returned from |getpos()|
  2843. " @return whether lhs_pos is before rhs_pos
  2844. function! lh#position#is_before(lhs_pos, rhs_pos)
  2845. if a:lhs_pos[0] != a:rhs_pos[0]
  2846. throw "Positions from incompatible buffers can't be ordered"
  2847. endif
  2848. "1 test lines
  2849. "2 test cols
  2850. let before
  2851. \ = (a:lhs_pos[1] == a:rhs_pos[1])
  2852. \ ? (a:lhs_pos[2] < a:rhs_pos[2])
  2853. \ : (a:lhs_pos[1] < a:rhs_pos[1])
  2854. return before
  2855. endfunction
  2856. function! lh#position#IsBefore(lhs_pos, rhs_pos)
  2857. return lh#position#is_before(a:lhs_pos, a:rhs_pos)
  2858. endfunction
  2859. " Function: lh#position#char_at_mark {{{3
  2860. " @return the character at a given mark (|mark|)
  2861. function! lh#position#char_at_mark(mark)
  2862. let c = getline(a:mark)[col(a:mark)-1]
  2863. return c
  2864. endfunction
  2865. function! lh#position#CharAtMark(mark)
  2866. return lh#position#char_at_mark(a:mark)
  2867. endfunction
  2868. " Function: lh#position#char_at_pos {{{3
  2869. " @return the character at a given position (|getpos()|)
  2870. function! lh#position#char_at_pos(pos)
  2871. let c = getline(a:pos[1])[(a:pos[2])-1]
  2872. return c
  2873. endfunction
  2874. function! lh#position#CharAtPos(pos)
  2875. return lh#position#char_at_pos(a:pos)
  2876. endfunction
  2877. " Functions }}}1
  2878. "------------------------------------------------------------------------
  2879. let &cpo=s:cpo_save
  2880. "=============================================================================
  2881. " vim600: set fdm=marker:
  2882. autoload/lh/syntax.vim [[[1
  2883. 153
  2884. "=============================================================================
  2885. " $Id: syntax.vim 558 2012-04-10 08:00:00Z luc.hermitte $
  2886. " File: autoload/lh/syntax.vim {{{1
  2887. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  2888. " <URL:http://code.google.com/p/lh-vim/>
  2889. " License: GPLv3 with exceptions
  2890. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  2891. " Version: 3.1.0
  2892. " Created: 05th Sep 2007
  2893. " Last Update: $Date: 2012-04-10 10:00:00 +0200 (mar 10 avr 2012) $ (05th Sep 2007)
  2894. "------------------------------------------------------------------------
  2895. " Description: «description»
  2896. "
  2897. "------------------------------------------------------------------------
  2898. " Installation:
  2899. " Drop it into {rtp}/autoload/lh/
  2900. " Vim 7+ required.
  2901. " History: «history»
  2902. " v1.0.0:
  2903. " Creation ;
  2904. " Functions moved from lhVimSpell
  2905. " v3.0.0: GPLv3
  2906. " v3.1.0: lh#syntax#is_a_comment()
  2907. " TODO:
  2908. " function, to inject "contained", see lhVimSpell approach
  2909. " }}}1
  2910. "=============================================================================
  2911. "=============================================================================
  2912. let s:cpo_save=&cpo
  2913. set cpo&vim
  2914. "------------------------------------------------------------------------
  2915. " ## Functions {{{1
  2916. " # Debug {{{2
  2917. function! lh#syntax#verbose(level)
  2918. let s:verbose = a:level
  2919. endfunction
  2920. function! s:Verbose(expr)
  2921. if exists('s:verbose') && s:verbose
  2922. echomsg a:expr
  2923. endif
  2924. endfunction
  2925. function! lh#syntax#debug(expr)
  2926. return eval(a:expr)
  2927. endfunction
  2928. " # Public {{{2
  2929. " Functions: Show name of the syntax kind of a character {{{3
  2930. function! lh#syntax#name_at(l,c, ...)
  2931. let what = a:0 > 0 ? a:1 : 0
  2932. return synIDattr(synID(a:l, a:c, what),'name')
  2933. endfunction
  2934. function! lh#syntax#NameAt(l,c, ...)
  2935. let what = a:0 > 0 ? a:1 : 0
  2936. return lh#syntax#name_at(a:l, a:c, what)
  2937. endfunction
  2938. function! lh#syntax#name_at_mark(mark, ...)
  2939. let what = a:0 > 0 ? a:1 : 0
  2940. return lh#syntax#name_at(line(a:mark), col(a:mark), what)
  2941. endfunction
  2942. function! lh#syntax#NameAtMark(mark, ...)
  2943. let what = a:0 > 0 ? a:1 : 0
  2944. return lh#syntax#name_at_mark(a:mark, what)
  2945. endfunction
  2946. " Functions: skip string, comment, character, doxygen {{{3
  2947. func! lh#syntax#skip_at(l,c)
  2948. return lh#syntax#name_at(a:l,a:c) =~? 'string\|comment\|character\|doxygen'
  2949. endfun
  2950. func! lh#syntax#SkipAt(l,c)
  2951. return lh#syntax#skip_at(a:l,a:c)
  2952. endfun
  2953. func! lh#syntax#skip()
  2954. return lh#syntax#skip_at(line('.'), col('.'))
  2955. endfun
  2956. func! lh#syntax#Skip()
  2957. return lh#syntax#skip()
  2958. endfun
  2959. func! lh#syntax#skip_at_mark(mark)
  2960. return lh#syntax#skip_at(line(a:mark), col(a:mark))
  2961. endfun
  2962. func! lh#syntax#SkipAtMark(mark)
  2963. return lh#syntax#skip_at_mark(a:mark)
  2964. endfun
  2965. " Command: :SynShow Show current syntax kind {{{3
  2966. command! SynShow echo 'hi<'.lh#syntax#name_at_mark('.',1).'> trans<'
  2967. \ lh#syntax#name_at_mark('.',0).'> lo<'.
  2968. \ synIDattr(synIDtrans(synID(line('.'), col('.'), 1)), 'name').'> ## '
  2969. \ lh#list#transform(synstack(line("."), col(".")), [], 'synIDattr(v:1_, "name")')
  2970. " Function: lh#syntax#list_raw(name) : string {{{3
  2971. function! lh#syntax#list_raw(name)
  2972. let a_save = @a
  2973. try
  2974. redir @a
  2975. exe 'silent! syn list '.a:name
  2976. redir END
  2977. let res = @a
  2978. finally
  2979. let @a = a_save
  2980. endtry
  2981. return res
  2982. endfunction
  2983. " Function: lh#syntax#list(name) : List {{{3
  2984. function! lh#syntax#list(name)
  2985. let raw = lh#syntax#list_raw(a:name)
  2986. let res = []
  2987. let lines = split(raw, '\n')
  2988. let started = 0
  2989. for l in lines
  2990. if started
  2991. let li = (l =~ 'links to') ? '' : l
  2992. elseif l =~ 'xxx'
  2993. let li = matchstr(l, 'xxx\s*\zs.*')
  2994. let started = 1
  2995. else
  2996. let li = ''
  2997. endif
  2998. if strlen(li) != 0
  2999. let li = substitute(li, 'contained\S*\|transparent\|nextgroup\|skipwhite\|skipnl\|skipempty', '', 'g')
  3000. let kinds = split(li, '\s\+')
  3001. call extend(res, kinds)
  3002. endif
  3003. endfor
  3004. return res
  3005. endfunction
  3006. " Function: lh#syntax#is_a_comment(mark) : bool {{{3
  3007. function! lh#syntax#is_a_comment(mark)
  3008. let stack = synstack(line(a:mark), col(a:mark))
  3009. for syn in stack
  3010. if synIDattr(syn, 'name') =~? 'comment'
  3011. return 1
  3012. endif
  3013. endfor
  3014. return 0
  3015. endfunction
  3016. " Functions }}}1
  3017. "------------------------------------------------------------------------
  3018. let &cpo=s:cpo_save
  3019. "=============================================================================
  3020. " vim600: set fdm=marker:
  3021. autoload/lh/visual.vim [[[1
  3022. 58
  3023. "=============================================================================
  3024. " $Id$
  3025. " File: autoload/lh/visual.vim {{{1
  3026. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  3027. " <URL:http://code.google.com/p/lh-vim/>
  3028. " License: GPLv3 with exceptions
  3029. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  3030. " Version: 3.0.0
  3031. " Created: 08th Sep 2008
  3032. " Last Update: $Date$
  3033. "------------------------------------------------------------------------
  3034. " Helpers functions releated to the visual mode
  3035. "
  3036. "------------------------------------------------------------------------
  3037. " Drop it into {rtp}/autoload/lh/
  3038. " Vim 7+ required.
  3039. " History:
  3040. " v3.0.0: GPLv3
  3041. " v2.2.5: lh#visual#cut()
  3042. " v2.0.6: First appearance
  3043. " TODO: «missing features»
  3044. " }}}1
  3045. "=============================================================================
  3046. let s:cpo_save=&cpo
  3047. set cpo&vim
  3048. "------------------------------------------------------------------------
  3049. " Functions {{{1
  3050. " Function: lh#visual#selection() {{{3
  3051. " @return the text currently selected
  3052. function! lh#visual#selection()
  3053. try
  3054. let a_save = @a
  3055. normal! gv"ay
  3056. return @a
  3057. finally
  3058. let @a = a_save
  3059. endtry
  3060. endfunction
  3061. " Function: lh#visual#cut() {{{3
  3062. " @return and delete the text currently selected
  3063. function! lh#visual#cut()
  3064. try
  3065. let a_save = @a
  3066. normal! gv"ad
  3067. return @a
  3068. finally
  3069. let @a = a_save
  3070. endtry
  3071. endfunction
  3072. " Functions }}}1
  3073. "------------------------------------------------------------------------
  3074. let &cpo=s:cpo_save
  3075. "=============================================================================
  3076. " vim600: set fdm=marker:
  3077. doc/lh-vim-lib.txt [[[1
  3078. 1369
  3079. *lh-vim-lib.txt* Vim common libraries (v3.1.0)
  3080. For Vim version 7+ Last change: $Date: 2012-04-10 10:00:00 +0200 (mar 10 avr 2012) $
  3081. By Luc Hermitte
  3082. hermitte {at} free {dot} fr
  3083. ==============================================================================
  3084. CONTENTS *lhvl-contents* {{{1
  3085. |lhvl-presentation| Presentation
  3086. |lhvl-functions| Functions
  3087. |add-local-help| Instructions on installing this help file
  3088. ------------------------------------------------------------------------------
  3089. PRESENTATION *lhvl-presentation* {{{1
  3090. |lh-vim-lib| is a library that defines some common VimL functions I use in my
  3091. various plugins and ftplugins.
  3092. This library has been conceived as a suite of |autoload| plugins, and a few
  3093. |macros| plugins. As such, it requires Vim 7+.
  3094. ==============================================================================
  3095. FUNCTIONS *lhvl-functions* {{{1
  3096. {{{2Functions list~
  3097. Miscellanous functions: |lhvl#misc|
  3098. - |lh#askvim#exe()|
  3099. - |lh#common#check_deps()|
  3100. - |lh#common#error_msg()|
  3101. - |lh#common#warning_msg()|
  3102. - |lh#common#echomsg_multilines()|
  3103. - |lh#encoding#iconv()|
  3104. - |lh#encoding#strlen()|
  3105. - |lh#encoding#strpart()|
  3106. - |lh#event#register_for_one_execution_at()|
  3107. - |lh#option#get()|
  3108. - |lh#option#get_non_empty()|
  3109. - |lh#position#char_at_mark()|
  3110. - |lh#position#char_at_pos()|
  3111. - |lh#position#is_before()|
  3112. - |lh#visual#selection()|
  3113. - |lh#visual#cut()|
  3114. Functors related functions: |lhvl#function|
  3115. - |lh#function#bind()|
  3116. - |lh#function#execute()|
  3117. - |lh#function#prepare()|
  3118. Lists related functions: |lhvl#list|
  3119. - |lh#list#accumulate()|
  3120. - |lh#list#at()|
  3121. - |lh#list#copy_if()|
  3122. - |lh#list#equal_range()|,
  3123. - |lh#list#Find_if()| and |lh#list#find_if()|
  3124. - |lh#list#intersect()|
  3125. - |lh#list#lower_bound()| and |lh#list#upper_bound()|
  3126. - |lh#list#match()|
  3127. - |lh#list#matches()|
  3128. - |lh#list#not_found()|
  3129. - |lh#list#remove()|
  3130. - |lh#list#subset()|
  3131. - |lh#list#Transform()| and |lh#list#transform()|
  3132. - |lh#list#transform_if()|
  3133. - |lh#list#unique_sort()| and |lh#list#unique_sort2()|
  3134. Graphs related functions: |lhvl#graph|
  3135. - |lh#graph#tsort#depth()|
  3136. - |lh#graph#tsort#breadth()|
  3137. Paths related functions: |lhvl#path|
  3138. - |lh#path#common()|
  3139. - |lh#path#depth()|
  3140. - |lh#path#glob_as_list()|
  3141. - |lh#path#is_absolute_path()|
  3142. - |lh#path#is_url()|
  3143. - |lh#path#select_one()|
  3144. - |lh#path#simplify()|
  3145. - |lh#path#strip_common()|
  3146. - |lh#path#strip_start()|
  3147. - |lh#path#to_dirname()|
  3148. - |lh#path#to_relative()|
  3149. - |lh#path#relative_to()|
  3150. - |lh#path#to_regex()|
  3151. Commands related functions: |lhvl#command|
  3152. - |lh#command#new()| (alpha version)
  3153. - |lh#command#Fargs2String()| (alpha version)
  3154. - |lh#command#complete()| (alpha version)
  3155. Menus related functions: |lhvl#menu|
  3156. - |lh#menu#def_toggle_item()|
  3157. - |lh#menu#text()|
  3158. - |lh#menu#make()|
  3159. - |lh#menu#IVN_make()|
  3160. - |lh#menu#is_in_visual_mode()|
  3161. - |lh#menu#map_all()|
  3162. - |lh#askvim#menu()| (beta version)
  3163. Buffers related functions: |lhvl#buffer|
  3164. - |lh#buffer#list()|
  3165. - |lh#buffer#find()|
  3166. - |lh#buffer#jump()|
  3167. - |lh#buffer#scratch()|
  3168. - |lh#buffer#dialog#| functions for building interactive dialogs
  3169. - |lh#buffer#dialog#new()|
  3170. - |lh#buffer#dialog#add_help()|
  3171. - |lh#buffer#dialog#select()|
  3172. - |lh#buffer#dialog#quit()|
  3173. - |lh#buffer#dialog#update()|
  3174. Syntax related functions: |lhvl#syntax|
  3175. - |lh#syntax#name_at()|
  3176. - |lh#syntax#name_at_mark()|
  3177. - |lh#syntax#skip()|
  3178. - |lh#syntax#skip_at()|
  3179. - |lh#syntax#skip_at_mark()|
  3180. - |lh#syntax#list_raw()|
  3181. - |lh#syntax#list()|
  3182. Completion related functions |lhvl#completion|
  3183. }}}2
  3184. ------------------------------------------------------------------------------
  3185. MISCELLANOUS FUNCTIONS *lhvl#misc* {{{2
  3186. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3187. *lh#common#echomsgMultilines()* {{{3
  3188. lh#common#echomsgMultilines()({text}) (*deprecated*)~
  3189. *lh#common#echomsg_multilines()*
  3190. lh#common#echomsg_multilines()({text})~
  3191. @param {text} Message to display on several lines
  3192. @return Nothing
  3193. This function executes |:echomsg| as many times as required as there are lines
  3194. in the original {text}.
  3195. This is a workaround |:echomsg| that is unable to handle correctly multi-lines
  3196. messages.
  3197. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3198. *lh#common#ErrorMsg()* {{{3
  3199. lh#common#ErrorMsg({text}) (*deprecated*)~
  3200. *lh#common#error_msg()*
  3201. lh#common#error_msg({text})~
  3202. @param {text} Error message to display
  3203. @return Nothing
  3204. This function displays an error message in a |confirm()| box if gvim is being
  3205. used, or as a standard vim error message through |:echoerr| otherwise.
  3206. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3207. *lh#common#WarningMsg()* {{{3
  3208. lh#common#WarningMsg({text}) (*deprecated*)~
  3209. *lh#common#warning_msg()*
  3210. lh#common#warning_msg({text})~
  3211. @param {text} Error message to display
  3212. @return Nothing
  3213. This function displays a warning message highlighted with |WarningMsg| syntax.
  3214. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3215. *lh#common#CheckDeps()* {{{3
  3216. lh#common#CheckDeps({symbol},{file},{path},{requester}) (*deprecated*)~
  3217. *lh#common#check_deps()*
  3218. lh#common#check_deps({symbol},{file},{path},{requester})~
  3219. @param {symbol} Symbol required, see |exists()| for symbol format.
  3220. @param {file} File in which the symbol is expected to be defined
  3221. @param {path} Path where the file can be found
  3222. @param {requester} Name of the script in need of this symbol
  3223. @return 0/1 whether the {symbol} exists
  3224. Checks if {symbol} exists in vim. If not, this function first tries
  3225. to |:source| the {file} in which the {symbol} is expected to be defined. If the
  3226. {symbol} is still not defined, an error message is issued (with
  3227. |lh#common#error_msg()|, and 0 is returned.
  3228. Example: >
  3229. if
  3230. \ !lh#common#check_deps('*Cpp_CurrentScope',
  3231. \ 'cpp_FindContextClass.vim', 'ftplugin/cpp/',
  3232. \ 'cpp#GotoFunctionImpl.vim')
  3233. \ || !lh#common#check_deps(':CheckOptions',
  3234. \ 'cpp_options-commands.vim', 'ftplugin/cpp/',
  3235. \ 'cpp#GotoFunctionImpl.vim')
  3236. let &cpo=s:cpo_save
  3237. finish
  3238. endif
  3239. Note: Since the introduction of |autoload| plugins in Vim 7, this function has
  3240. lost most of its interrest.
  3241. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3242. *lh#option#Get()* {{{3
  3243. lh#option#Get({name},{default}[,{scopes}]) (*deprecated*)~
  3244. *lh#option#get()*
  3245. lh#option#get({name},{default}[,{scopes}])~
  3246. @param {name} Name of the option to fetch
  3247. @param {default} Default value in case the option is not defined
  3248. @param {scopes} Vim scopes in which the options must be searched,
  3249. default="bg".
  3250. @return b:{name} if it exists, or g:{name} if it exists, or
  3251. {default} otherwise.
  3252. @see For development oriented options, |lh-dev| provides a
  3253. dedicated function: |lh#dev#option#get()|.
  3254. This function fetches the value of an user defined option (not Vim |options|).
  3255. The option can be either a |global-variable|, a |buffer-variable|, or even
  3256. a|window-variable|.
  3257. The order of the variables checked can be specified through the optional
  3258. argument {scopes}. By default, buffer-local options have the priority over
  3259. global options.
  3260. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3261. *lh#option#GetNonEmpty()* {{{3
  3262. lh#option#GetNonEmpty({name},{default}[,{scopes}]) (*deprecated*)~
  3263. *lh#option#get_non_empty()*
  3264. lh#option#get_non_empty({name},{default}[,{scopes}])~
  3265. @param {name} Name of the option to fetch
  3266. @param {default} Default value in case the option is not defined, nor empty
  3267. @param {scopes} Vim scopes in which the options must be searched,
  3268. default="bg".
  3269. @return b:{name} If it exists, of g:{name} if it exists, or {default}
  3270. otherwise.
  3271. This function works exactly like |lh#option#get()| except that a defined
  3272. variable with an empty value will be ignored as well.
  3273. An |expr-string| will be considered empty if its |strlen()| is 0, an
  3274. |expr-number| when it values 0, |Lists| and |Dictionaries| when their |len()|
  3275. is 0.
  3276. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3277. *lh#askvim#Exe()* {{{3
  3278. lh#askvim#Exe({command}) (*deprecated*)~
  3279. *lh#askvim#exe()*
  3280. lh#askvim#exe({command})~
  3281. @param {command} Command to execute from vim.
  3282. @return What the command echoes while executed.
  3283. @note This function encapsultates |redir| without altering any
  3284. register.
  3285. Some information aren't directly accessible (yet) through vim API
  3286. (|functions|). However, they can be obtained by executing some commands, and
  3287. redirecting the result of these commands.
  3288. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3289. *lh#askvim#menu()* {{{3
  3290. lh#askvim#menu({menuid},{modes})~
  3291. @param {menuid} Menu identifier.
  3292. @param {modes} List of modes
  3293. @return Information related to the {menuid}
  3294. @todo Still bugged
  3295. This function provides a way to obtain information related to a menu entry in
  3296. Vim.
  3297. The format of the result being «to be stabilized»
  3298. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3299. *lh#position#IsBefore()* {{{3
  3300. lh#position#IsBefore({lhs_pos},{rhs_pos}) (*deprecated*)~
  3301. *lh#position#is_before()*
  3302. lh#position#is_before({lhs_pos},{rhs_pos})~
  3303. @param[in] Positions as those returned from |getpos()|
  3304. @return Whether {lhs_pos} is before {rhs_pos}
  3305. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3306. *lh#position#CharAtMark()* {{{3
  3307. lh#position#CharAtMark({mark}) (*deprecated*)~
  3308. *lh#position#char_at_mark()*
  3309. lh#position#char_at_mark({mark})~
  3310. @return The character at a given |mark|.
  3311. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3312. *lh#position#CharAtPos()* {{{3
  3313. lh#position#CharAtPos({pos}) (*deprecated*)~
  3314. *lh#position#char_at_pos()* {{{3
  3315. lh#position#char_at_pos({pos})~
  3316. @return The character at a position (see |getpos()|).
  3317. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3318. *lh#visual#selection()* {{{3
  3319. lh#visual#selection()~
  3320. @return The current visual selection
  3321. @post |registers| are not altered by this function
  3322. *lh#visual#cut()* {{{3
  3323. lh#visual#cut()~
  3324. @return The current visual selection
  3325. @post |registers| are not altered by this function ;
  3326. selection is deleted.
  3327. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3328. *lh#event#RegisterForOneExecutionAt()* {{{3
  3329. lh#event#RegisterForOneExecutionAt({event}, {cmd}, {group}) (*deprecated*)~
  3330. *lh#event#register_for_one_execution_at()*
  3331. lh#event#register_for_one_execution_at({event}, {cmd}, {group})~
  3332. Registers a command to be executed once (and only once) when {event} is
  3333. triggered on the current file.
  3334. @param {event} Event that will trigger the execution of {cmd}|autocmd-events|
  3335. @param {cmd} |expression-command| to execute
  3336. @param {group} |autocmd-groups| under which the internal autocommand will be
  3337. registered.
  3338. @todo possibility to specify the file pattern
  3339. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3340. *lh#encoding#iconv()* {{{3
  3341. lh#encoding#iconv({expr}, {from}, {to})~
  3342. This function just calls |iconv()| with the same arguments. The only
  3343. difference is that it return {expr} when we know that |iconv()| will return an
  3344. empty string.
  3345. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3346. *lh#encoding#strlen()* {{{3
  3347. lh#encoding#strlen({mb_string})~
  3348. This function returns the length of the multi-bytes string.
  3349. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3350. *lh#encoding#at()* {{{3
  3351. lh#encoding#at({mb_string}, {i})~
  3352. Returns the i-th character in a multi-bytes string.
  3353. @param {i} 0-indexed offset.
  3354. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3355. *lh#encoding#strpart()* {{{3
  3356. lh#encoding#strpart({mb_string}, {position}, {length})~
  3357. Returns {length} extracted characters from {position} in a multi-bytes string.
  3358. @param {position} 0-indexed offset.
  3359. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3360. *lh#float#max()* *lh#float#min()* {{{3
  3361. *lh#float#arg_max()* *lh#float#arg_min()*
  3362. lh#float#min({list})~
  3363. lh#float#arg_min({list})~
  3364. lh#float#min({list})~
  3365. lh#float#arg_min({list})~
  3366. Returns The minimum, /arg-minimum, /maximum, /arg-maximum of a |List| of
  3367. |expr-float|.
  3368. ------------------------------------------------------------------------------
  3369. FUNCTORS RELATED FUNCTIONS *lhvl#function* {{{2
  3370. This sub-library helps defining functors-like variables, and execute them.
  3371. NB: C++ developpers may be already familiar with boost.bind
  3372. (/std(::tr1)::bind) function that inspired by feature.
  3373. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3374. *lhvl#functor* {{{3
  3375. A functor is implemented as a |Dictionary| that has the following fields:
  3376. - {execute} is the |Funcref| that will be actually executed by
  3377. |lh#function#execute()|. Its only argument is a |List| of
  3378. arguments for {function}.
  3379. - {function} that identifies the function to execute,
  3380. internals: it could be either a |Funcref|or a |expr-string|, or
  3381. whatever is compatible with the {execute} |FuncRef| field.
  3382. - {args} will contain the binded arguments as defined by
  3383. |lh#function#bind()|. If you attach a {execute} function of your
  3384. own to a functor, you don't need to fill "args".
  3385. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3386. *lh#function#bind()* {{{3
  3387. lh#function#bind({fn} [, {arguments} ...])~
  3388. This function creates a new |lhvl#functor| based on a given function {fn}, and
  3389. where some arguments are binded to the ones from {arguments}.
  3390. The result is a new function-like data having for parameter v:1_, v:2_, ...
  3391. that were specified in |lh#function#bind()| {arguments} list.
  3392. Examples:~
  3393. See tests/lh/function.vim
  3394. Let's suppose Print(...) a VimL variadic function that echoes the arguments it
  3395. receives, i.e. >
  3396. call Print(1,2,"text", ['foo', 'bar'])
  3397. will echo: >
  3398. 1 ## 2 ## 'text' ## ['foo', 'bar']
  3399. * Binding a |FuncRef|:~
  3400. and reverse the arguments given to it when it will be executed >
  3401. >:let func = lh#function#bind(function('Print'), 'v:3_', 'v:2_', 'v:1_')
  3402. >:echo lh#function#execute(func, 1, 'two', [3])
  3403. [3] ## 'two' ## 1
  3404. * Binding a named function:~
  3405. the new function has 3 parameters and calls the named function with its 3rd
  3406. parameter, 42, its second and its first parameters as arguments. >
  3407. >:let func = lh#function#bind('Print', 'v:3_', 42, 'v:2_', 'v:1_')
  3408. >:echo lh#function#execute(func, 1, 'two', [3])
  3409. [3] ## 42 ## 'two' ## 1
  3410. < NB: if exists('*'.func_name) is false, then the string is considered to be
  3411. an expression that will be evaluated as specified in the next use case.
  3412. * Binding an expression:~
  3413. This time more complex on-the-fly computations on the |lhvl#functor|
  3414. parameters can be accomplished >
  3415. >:let func = lh#function#bind('Print(len(v:3_), 42, v:2_, v:1_)')
  3416. >:echo lh#function#execute(func, 1, 'two', [1,2,3])
  3417. 3 ## 42 ## 'two' ## 1
  3418. < NB: func["args"] is defined, but empty, and unused.
  3419. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3420. *lh#function#execute()* {{{3
  3421. lh#function#execute({functor} [, {arguments} ...])~
  3422. While |lh#function#bind()| defines a |lhvl#functor| that can be stored and
  3423. used later, |lh#function#execute()| directly executes the {functor} received.
  3424. Different kind of {functors} are accepted:
  3425. - |FuncRef|, and function names, where arguments are |lh#function#execute()|
  3426. ones ;
  3427. - |expr-string|, where "v:{pos}_" strings are binded on-the-fly to {arguments} ;
  3428. - |lhvl#functor|, that will be given {arguments} as arguments.
  3429. Examples:~
  3430. See tests/lh/function.vim
  3431. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3432. *lh#function#prepare()* {{{3
  3433. lh#function#prepare({function}, {arguments} ...)~
  3434. This function expands all the elements from the {arguments} |List|, and
  3435. prepares a |expr-string| that once evaluated will call the n-ary {function}
  3436. with the n-{arguments}.
  3437. The evaluation is meant to be done with |eval()|.
  3438. >
  3439. >:let call = lh#function#prepare('Print', [1,2,"foo"])
  3440. >:echo eval(call)
  3441. 1 ## 2 ## 'foo'
  3442. ------------------------------------------------------------------------------
  3443. LISTS RELATED FUNCTIONS *lhvl#list* {{{2
  3444. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3445. *lh#list#Match()* {{{3
  3446. lh#list#Match({list},{pattern}[, {start-pos}]) (*deprecated*)~
  3447. *lh#list#match()*
  3448. lh#list#match({list},{pattern}[, {start-pos}])~
  3449. @param {list} |List|
  3450. @param {pattern} |expr-string|
  3451. @param {start-pos} First index to check
  3452. @return The lowest index, >= {start-pos}, in |List| {list} where
  3453. the item matches {pattern}.
  3454. @return -1 if no item matches {pattern}.
  3455. @see |index()|, |match()|
  3456. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3457. *lh#list#matches()* {{{3
  3458. lh#list#match({list},{pattern}[, {start-pos}])~
  3459. @param {list} |List|
  3460. @param {pattern} |expr-string|
  3461. @param {start-pos} First index to check
  3462. @return The |List| of indices, >= {start-pos}, in |List| {list} where
  3463. the item matches {pattern}.
  3464. @return [] if no item matches {pattern}.
  3465. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3466. *lh#list#find_if()* *lh#list#Find_if()* {{{3
  3467. lh#list#Find_if({list},{string-predicate} [, {pred-parameters}][, {start-pos}])~
  3468. lh#list#find_if({list},{functor-predicate} [, {pred-parameters}][, {start-pos}])~
  3469. @param {list} |List|
  3470. @param {*-predicate} Predicate to evaluate
  3471. @param {pred-parameters}] |List| of Parameters to bind to special arguments in
  3472. the {predicate}.
  3473. @param {start-pos} First index to check
  3474. @return The lowest index, >= {start-pos}, in |List| {list}
  3475. where the {predicate} evals to true.
  3476. @return -1 if no item matches {pattern}.
  3477. @see |index()|, |eval()|
  3478. The {string-predicate} recognizes some special arguments:
  3479. - |v:val| is substituted with the current element being evaluated in the list
  3480. - *v:1_* *v:2_* , ..., are substituted with the i-th elements from
  3481. {pred-parameters}.
  3482. NB: the "v:\d\+_" are 1-indexed while {pred-parameters} is indeed seen as
  3483. 0-indexed by Vim.
  3484. This particular feature permits to pass any type of variable to the
  3485. predicate: a |expr-string|, a |List|, a |Dictionary|, ...
  3486. e.g.: >
  3487. :let b = { 'min': 12, 'max': 42 }
  3488. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  3489. :let i = lh#list#Find_if(l, 'v:val>v:1_.min && v:val<v:1_.max && v:val%v:2_==0', [b, 2] )
  3490. :echo l[i]
  3491. 28
  3492. The {functor-predicate} is a |lhvl#function|. The same example can be
  3493. rewritten as: >
  3494. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  3495. :let i = lh#list#find_if(l, 'v:1_>12 && v:1_<42 && v:1_%2==0')
  3496. :echo l[i]
  3497. 28
  3498. NB: Expect the Find_if() version to be replaced with the find_if() one.
  3499. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3500. *lh#list#lower_bound()* *lh#list#upper_bound()* {{{3
  3501. *lh#list#equal_range()*
  3502. lh#list#lower_bound({list}, {value} [, {first}][, {last}])~
  3503. lh#list#upper_bound({list}, {value} [, {first}][, {last}])~
  3504. lh#list#equal_range({list}, {value} [, {first}][, {last}])~
  3505. @param {list} Sorted |List|
  3506. @param {value} Value to search
  3507. @param {first} First index to check
  3508. @param {last} Last+1 index to check
  3509. @return The lowest index, >= {first} and < {last}, in |List| {list}
  3510. such as the index is <= first {value} occurrence, in lower_bound case
  3511. such as the index is > last {value} occurrence, in upper_bound case
  3512. @return -1 if no item matches {pattern}.
  3513. @return the pair [lower_bound(), upper_bound()] in equal_range() case
  3514. @see C++ STL eponym algorithms.
  3515. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3516. *lh#list#not_found()* {{{3
  3517. lh#list#not_found({range})~
  3518. Returns whether the {range} is empty. This function can be used to check
  3519. |lh#list#equal_range()| functions results
  3520. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3521. *lh#list#unique_sort()* *lh#list#unique_sort2()* {{{3
  3522. lh#list#unique_sort({list} [, {cmp}])~
  3523. lh#list#unique_sort2({list} [, {cmp}])~
  3524. @param[in] {list} |List| to sort
  3525. @param {cmp} |Funcref| or function name that acts as a compare predicate.
  3526. It seems to be required in order to not compare number with
  3527. a lexicographic order (with vim 7.1-156)
  3528. @return A new |List| sorted with no element repeated
  3529. @todo support an optional {equal} predicate to use in the /unique/ making
  3530. process.
  3531. The difference between the two functions is the following:
  3532. - unique_sort() stores all the elements in a |Dictionary|, then sort the values
  3533. stored in the dictionary ;
  3534. - unique_sort2() sorts all the elements from the initial |List|, and then
  3535. keeps only the elements that appear once.
  3536. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3537. *lh#list#Transform()* {{{3
  3538. lh#list#Transform({input},{output},{action})~
  3539. @param[in] {input} Input |List| to transform
  3540. @param[out] {output} Output |List| where the transformed elements will be
  3541. appended.
  3542. @param {action} Stringified action to apply on each element from {input}.
  3543. The string "v:val" will always be replaced with the
  3544. element currently transformed.
  3545. @return {output}
  3546. This function is actually returning >
  3547. extend(a:output, map(copy(a:input), a:action))
  3548. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3549. *lh#list#transform()* {{{3
  3550. lh#list#transform({input},{output},{action})~
  3551. @param[in] {input} Input |List| to transform
  3552. @param[out] {output} Output |List| where the transformed elements will be
  3553. appended.
  3554. @param {action}|lhvl#functor| action to apply on each element from
  3555. {input}.
  3556. @return {output}
  3557. This function is equivalent to (|lh#list#Transform()|) >
  3558. extend(a:output, map(copy(a:input), a:action))
  3559. except the action is not a string but a |lhvl#functor|.
  3560. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3561. *lh#list#transform_if()* {{{3
  3562. lh#list#transform_if({input},{output},{action},{predicate})~
  3563. @param[in] {input} Input |List| to transform
  3564. @param[out] {output} Output |List| where the transformed elements will be
  3565. appended.
  3566. @param {action}|lhvl#functor| action to apply on each element from
  3567. {input} that verifies the {predicate}.
  3568. @param {predicate} Boolean |lhvl#functor| tested on each element before
  3569. transforming it.
  3570. @return {output}
  3571. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3572. *lh#list#copy_if()* {{{3
  3573. lh#list#copy_if({input},{output},{predicate})~
  3574. Appends in {output} the elements from {input} that verifies the {predicate}.
  3575. @param[in] {input} Input |List| to transform
  3576. @param[out] {output} Output |List| where the elements that verify the
  3577. {predicate} will be appended.
  3578. @param {predicate} Boolean |lhvl#functor| tested on each element.
  3579. @return {output}
  3580. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3581. *lh#list#accumulate()* {{{3
  3582. lh#list#accumulate({input},{transformation},{accumulator})~
  3583. Accumulates the transformed elements from {input}.
  3584. @param[in] {input} Input |List| to transform
  3585. @param {transformation}|lhvl#functor| applied on each element from {input}.
  3586. @param {accumulator}|lhvl#functor| taking the list of tranformaed elements
  3587. as input
  3588. @return the result of {accumulator}
  3589. Examples: >
  3590. :let strings = [ 'foo', 'bar', 'toto' ]
  3591. :echo eval(lh#list#accumulate(strings, 'strlen', 'join(v:1_, "+")'))
  3592. 10
  3593. :let l = [ 1, 2, 'foo', ['bar']]
  3594. :echo lh#list#accumulate(l, 'string', 'join(v:1_, "##")')
  3595. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3596. *lh#list#subset()* {{{3
  3597. lh#list#subset({input},{indices})~
  3598. Returns a subset slice of the {input} list.
  3599. @param[in] {input} Input |List| from which element will be extracted
  3600. @param[in] {indices}|List| of indices to extract
  3601. @return a |List| of the elements from {input} indexed by the {indices}
  3602. Example: >
  3603. :let l = [ 1, 25, 5, 48, 25, 5, 28, 6]
  3604. :let indices = [ 0, 5, 7, 3 ]
  3605. :echo lh#list#subset(l, indices)
  3606. [ 1, 5, 6, 48 ]
  3607. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3608. *lh#list#remove()* {{{3
  3609. lh#list#remove({input},{indices})~
  3610. Returns a subset slice of the {input} list trimmed of elements.
  3611. @param[in,out] {input} Input |List| from which element will be removed
  3612. @param[in] {indices}|List| of indices to remove
  3613. @return a |List| of the elements from {input} not indexed by the {indices}
  3614. @pre {indices} MUST be sorted
  3615. Example: >
  3616. :let l = [ 1, 25, 5, 48, 25, 5, 28, 6]
  3617. :let indices = [ 0, 3, 5, 7 ]
  3618. :echo lh#list#remove(l, indices)
  3619. [ 25, 5, 25, 28 ]
  3620. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3621. *lh#list#intersect()* {{{3
  3622. lh#list#intersect({list1},{list2})~
  3623. Returns the elements present in both input lists.
  3624. @param[in] {list1}|List|
  3625. @param[in] {list2}|List|
  3626. @return a |List| of the elements in both {list1} and {list2}, the elements are
  3627. kepts in the same order as in {list1}
  3628. @note the algorithm is in O(len({list1})*len({list2}))
  3629. Example: >
  3630. :let l1 = [ 1, 25, 7, 48, 26, 5, 28, 6]
  3631. :let l2 = [ 3, 8, 7, 25, 6 ]
  3632. :echo lh#list#intersect(l1, l2)
  3633. [ 25, 7, 6 ]
  3634. ------------------------------------------------------------------------------
  3635. GRAPHS RELATED FUNCTIONS *lhvl#graph* {{{2
  3636. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3637. *lh#graph#tsort#depth()* {{{3
  3638. *lh#graph#tsort#breadth()* {{{3
  3639. lh#graph#tsort#depth({dag}, {start-nodes})~
  3640. lh#graph#tsort#breadth({dag}, {start-nodes})~
  3641. These two functions implement a topological sort on the Direct Acyclic Graph.
  3642. - depth() is a recursive implementation of a depth-first search.
  3643. - breadth() is a non recursive implementation of a breadth-first search.
  3644. @param {dag} is a direct acyclic graph defined either:
  3645. - as a |Dictionnary| that associates to each node, the |List| of
  3646. all its successors
  3647. - or as a /fetch/ |function()| that returns the |List| of the
  3648. successors of a given node -- works only with depth() which
  3649. takes care of not calling this function more than once for each
  3650. given node.
  3651. @param {start-nodes} is a |List| of start nodes with no incoming edge
  3652. @throw "Tsort: cyclic graph detected:" if {dag} is not a DAG.
  3653. @see http://en.wikipedia.org/wiki/Topological_sort
  3654. @since Version 2.1.0
  3655. @test tests/lh/topological-sort.vim
  3656. ------------------------------------------------------------------------------
  3657. PATHS RELATED FUNCTIONS *lhvl#path* {{{2
  3658. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3659. *lh#path#depth()* {{{3
  3660. lh#path#depth({dirname})~
  3661. Returns the depth of a directory name.
  3662. @param {dirname} Pathname to simplify
  3663. @return the depth of the simplified directory name, i.e.
  3664. lh#path#depth("bar/b2/../../foo/") returns 1
  3665. @todo However, it is not able to return depth of negative paths like
  3666. "../../foo/". I still need to decide whether the function should return
  3667. -1 or 3.
  3668. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3669. *lh#path#dirname()* {{{3
  3670. lh#path#dirname({dirname})~
  3671. Ensures the returned directory name ends with a '/' or a '\'.
  3672. @todo On windows, it should take 'shellslash' into account to decide the
  3673. character to append.
  3674. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3675. *lh#path#Simplify()* {{{3
  3676. lh#path#Simplify({pathname}) (*deprecated*)~
  3677. *lh#path#simplify()*
  3678. lh#path#simplify({pathname} [{make_relative_to_pwd])~
  3679. Simplifies a path by getting rid of useless '../' and './'.
  3680. @param {pathname} Pathname to simplify
  3681. @param {make_relative_to_pwd} The {pathname} is made relative to pwd when set
  3682. @return the simplified pathname
  3683. This function works like |simplify()|, except that it also strips the leading
  3684. "./".
  3685. Note: when vim is compiled for unix, it seems unable to |simplify()| paths
  3686. containing "..\". (It likelly works this way when vim is compiled without
  3687. 'shellslash' support)
  3688. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3689. *lh#path#common()* {{{3
  3690. lh#path#common({pathnames})~
  3691. @param[in] {pathnames} |List| of pathnames to analyse
  3692. @return the common leading path between all {pathnames}
  3693. e.g.: >
  3694. :echo lh#path#common(['foo/bar/file','foo/file', 'foo/foo/file'])
  3695. echoes >
  3696. foo/
  3697. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3698. *lh#path#StripCommon()* {{{3
  3699. lh#path#StripCommon({pathnames}) (*deprecated*)~
  3700. *lh#path#strip_common()*
  3701. lh#path#strip_common({pathnames})~
  3702. @param[in,out] {pathnames} |List| of pathnames to simplify
  3703. @return the simplified pathnames
  3704. This function strips all pathnames from their common leading part. The
  3705. compuation of the common leading part is ensured by |lh#path#common()|
  3706. thank.
  3707. e.g.: >
  3708. :echo lh#path#strip_common(['foo/bar/file','foo/file', 'foo/foo/file'])
  3709. echoes >
  3710. ['bar/file','file', 'foo/file']
  3711. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3712. *lh#path#StripStart()* {{{3
  3713. lh#path#StripStart({pathname}, {pathslist}) (*deprecated*)~
  3714. *lh#path#strip_start()*
  3715. lh#path#strip_start({pathname}, {pathslist})~
  3716. @param[in] {pathname} name to simplify
  3717. @param[in] {pathslist} list of pathname (can be a |string| of pathnames
  3718. separated by ",", of a |List|).
  3719. Strips {pathname} from any path from {pathslist}.
  3720. e.g.: >
  3721. :echo lh#path#strip_start($HOME.'/.vim/template/bar.template',
  3722. \ ['/home/foo/.vim', '/usr/local/share/vim/'])
  3723. :echo lh#path#strip_start($HOME.'/.vim/template/bar.template',&rtp)
  3724. echoes >
  3725. template/bar.template
  3726. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3727. *lh#path#IsAbsolutePath()* {{{3
  3728. lh#path#IsAbsolutePath({path}) (*deprecated*)~
  3729. *lh#path#is_absolute_path()*
  3730. lh#path#is_absolute_path({path})~
  3731. @return {path} Path to test
  3732. @return whether the path is an absolute path
  3733. @note Supports Unix absolute paths, Windows absolute paths, and UNC paths
  3734. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3735. *lh#path#IsURL()* {{{3
  3736. lh#path#IsURL({path}) (*deprecated*)~
  3737. *lh#path#is_url()*
  3738. lh#path#is_url({path})~
  3739. @return {path} Path to test
  3740. @return whether the path is an URL
  3741. @note Supports http(s)://, (s)ftp://, dav://, fetch://, file://, rcp://,
  3742. rsynch://, scp://
  3743. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3744. *lh#path#SelectOne()* {{{3
  3745. lh#path#SelectOne({pathnames},{prompt}) (*deprecated*)~
  3746. *lh#path#select_one()*
  3747. lh#path#select_one({pathnames},{prompt})~
  3748. @param[in] {pathnames} |List| of pathname
  3749. @param {prompt} Prompt for the dialog box
  3750. @return "" if len({pathnames}) == 0
  3751. @return {pathnames}[0] if len({pathnames}) == 1
  3752. @return the selected pathname otherwise
  3753. Asks the end-user to choose a pathname among a list of pathnames.
  3754. The pathnames displayed will be simplified thanks to |lh#path#strip_common()|
  3755. -- the pathname returned is the "full" original pathname matching the
  3756. simplified pathname selected.
  3757. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3758. *lh#path#ToRelative()* {{{3
  3759. lh#path#ToRelative({pathname}) (*deprecated*)~
  3760. *lh#path#to_relative()*
  3761. lh#path#to_relative({pathname})~
  3762. @param {pathname} Pathname to convert
  3763. @return the simplified {pathname} in its relative form as it would be seen
  3764. from the current directory.
  3765. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3766. *lh#path#relative_to()* {{{3
  3767. lh#path#relative_to({from}, {to})~
  3768. Returns the relative directory that indentifies {to} from {from} location.
  3769. @param {from} origin directory
  3770. @param {to} destination directory
  3771. @return the simplified pathname {to} in its relative form as it would be seen
  3772. from the {from} directory.
  3773. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3774. *lh#path#GlobAsList()* {{{3
  3775. lh#path#GlobAsList({pathslist}, {expr}) (*deprecated*)~
  3776. *lh#path#glob_as_list()*
  3777. lh#path#glob_as_list({pathslist}, {expr} [,{must_sort}])~
  3778. @param[in] {pathslist} list (|List|, or comma separated list) of paths where
  3779. to search.
  3780. @param[in] {expr} glob expression of the files to search.
  3781. @param[in] {mustSort} tells whether the results shall be sorted per
  3782. {pathslist}, default: true.
  3783. @return |globpath()|'s result, but formatted as a list of matching pathnames.
  3784. In case {expr} is a |List|, |globpath()| is applied on each expression in
  3785. {expr}.
  3786. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3787. *lh#path#find()* {{{3
  3788. lh#path#find({pathslist}, {regex})~
  3789. @param[in] {pathslist} List of paths which can be received as a |List| or as a
  3790. string made of coma separated paths.
  3791. @return the path that matches the given {regex}
  3792. e.g.: >
  3793. let expected_win = $HOME . '/vimfiles'
  3794. let expected_nix = $HOME . '/.vim'
  3795. let what = lh#path#to_regex($HOME.'/').'\(vimfiles\|.vim\)'
  3796. let z = lh#path#find(&rtp,what)
  3797. if has('win16')||has('win32')||has('win64')
  3798. Assert z == expected_win
  3799. else
  3800. Assert z == expected_nix
  3801. endif
  3802. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3803. *lh#path#to_regex()* {{{3
  3804. lh#path#to_regex({pathname})~
  3805. Transforms the {pathname} to separate each node-name by the string '[/\\]'
  3806. The rationale behind this function is to build system independant regex
  3807. pattern to use on pathnames as sometimes pathnames are built by appending
  3808. '/stuff/like/this' without taking 'shellslash' into account.
  3809. e.g.: >
  3810. echo lh#path#to_regex('/home/luc/').'\(vimfiles\|.vim\)'
  3811. echoes >
  3812. [/\\]home[/\\]luc[/\\]\(vimfiles\|.vim\)
  3813. ------------------------------------------------------------------------------
  3814. MENUS RELATED FUNCTIONS *lhvl#menu* {{{2
  3815. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3816. *lh#menu#def_toggle_item()* {{{3
  3817. lh#menu#def_toggle_item({Data})~
  3818. @param[in,out] {Data} Definition of a |menu| item.
  3819. This function defines a |menu| entry that will be associated to a
  3820. |global-variable| whose values can be cycled and explored from the menu. This
  3821. global variable can be seen as an enumerate whose value can be cyclically
  3822. updated through a menu.
  3823. {Data} is a |Dictionary| whose keys are:
  3824. - "variable": name of the |global-variable| to bind to the menu entry
  3825. Mandatory.
  3826. - "values": associated values of string or integers (|List|)
  3827. Mandatory.
  3828. - "menu": describes where the menu entry must be placed (|Dictionary|)
  3829. - "priority": complete priority of the entry (see |sub-menu-priority|)
  3830. - "name": complete name of the entry -- ampersand (&) can be used to define
  3831. shortcut keys
  3832. Mandatory.
  3833. - "idx_crt_value": index of the current value for the option (|expr-number|)
  3834. This is also an internal variable that will be automatically updated to
  3835. keep the index of the current value of the "variable" in "values".
  3836. Optional ; default value is 1, or the associated index of the initial value
  3837. of the variable (in "values") before the function call.
  3838. - "texts": texts to display according to the variable value (|List|)
  3839. Optional, "values" will be used by default. This option is to be used to
  3840. distinguish the short encoded value, from the long self explanatory name.
  3841. - "hook": |function| to call, or command to |:execute| when the value of the
  3842. variable is toggled through toggle-menu ; default: none.
  3843. - "actions": list of functions to call, or commands to execute when the value
  3844. of the variable is toggled through toggle-menu. There shall be one action
  3845. per possible value when defined ; default: empty list
  3846. Warning:
  3847. If the variable is changed by hand without using the menu, then the menu
  3848. and the variable will be out of synch. Unless the command |lhvl-:Toggle|
  3849. is used to change the value of the options (and keep the menu
  3850. synchronized).
  3851. Examples:
  3852. See tests/lh/test-toggle-menu.vim
  3853. *lhvl-:Toggle*
  3854. :Toggle {variable-name} [{text-value}]~
  3855. @param {variable-name}
  3856. must be a |global-variable| name used as "variable" in the
  3857. definition of a toggable menu item thanks to
  3858. |lh#menu#def_toggle_item()|.
  3859. @param {text-value}
  3860. when specified, :Toggle directly sets the variable to the value
  3861. associated to {text-value}.
  3862. This command supports autocompletion on the {variable-name}, and on
  3863. {text-value}.
  3864. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3865. *lh#menu#text()* {{{3
  3866. lh#menu#text({text})~
  3867. @param[in] {text} Text to send to |:menu| commands
  3868. @return a text to be used in menus where "\" and spaces have been escaped.
  3869. This helper function transforms a regular text into a text that can be
  3870. directly used with |:menu| commands.
  3871. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3872. *lh#menu#make()* {{{3
  3873. option: *[gb]:want_buffermenu_or_global_disable*
  3874. If Michael Geddes's |buffer-menu| plugin is installed, this option tells
  3875. whether we want to take advantage of it to define menus or to ignore it.
  3876. lh#menu#make({modes}, {menu-priority}, {menu-text}, {key-binding}, [<buffer>,] {action})~
  3877. Creates a menu entry and its associated mappings for several modes at once.
  3878. @param[in] {modes} Vim modes the menus and maps will be provided for
  3879. @param[in] {menu-priority} |sub-menu-priority| for the new menu entry
  3880. @param[in] {menu-text} Name of the new menu entry
  3881. @param[in] {key-binding} Sequence of keys to execute the associated action
  3882. @param[in] "<buffer>" If the string "<buffer>" is provided, then the
  3883. associated mapping will be a |map-<buffer>|, and
  3884. the menu will be available to the current buffer
  3885. only. See |[gb]:want_buffermenu_or_global_disable|
  3886. When "<buffer>" is set, the call to lh#menu#make()
  3887. must be done in the buffer-zone from a |ftplugin|,
  3888. or from a |local_vimrc|.
  3889. @param[in] {action} Action to execute when {key-binding} is typed, or
  3890. when the menu entry is selected.
  3891. @todo support select ('s') and visual-not-select ('x') modes
  3892. First example:~
  3893. The following call will add the menu "LaTeX.Run LaTeX once <C-L><C-O>", with
  3894. the priority (placement) 50.305, for the NORMAL, INSERT and COMMAND modes. The
  3895. action associated first saves all the changed buffers and then invokes LaTeX.
  3896. The same action is also binded to <C-L><C-O> for the same modes, with the
  3897. nuance that the maps will be local to the buffer.
  3898. >
  3899. call lh#menu#make("nic", '50.305', '&LaTeX.Run LaTeX &once', "<C-L><C-O>",
  3900. \ '<buffer>', ":wa<CR>:call TKMakeDVIfile(1)<CR>")
  3901. Second example:~
  3902. This example demonstrates an hidden, but useful, behavior: if the mode is the
  3903. visual one, then the register v is filled with the text of the visual area.
  3904. This text can then be used in the function called. Here, it will be proposed
  3905. as a default name for the section to insert:
  3906. >
  3907. function! TKinsertSec()
  3908. " ...
  3909. if (strlen(@v) != 0) && (visualmode() == 'v')
  3910. let SecName = input("name of ".SecType.": ", @v)
  3911. else
  3912. let SecName = input("name of ".SecType.": ")
  3913. endif
  3914. " ...
  3915. endfunction
  3916. call lh#menu#make("vnic", '50.360.100', '&LaTeX.&Insert.&Section',
  3917. \ "<C-L><C-S>", '<buffer>', ":call TKinsertSec()<CR>")
  3918. We have to be cautious to one little thing, there is a side effect: the visual
  3919. mode vanishes when we enter the function. If you don't want this to happen,
  3920. use the non-existant command: |:VCall|.
  3921. Third example:~
  3922. If it is known that a function will be called only under |VISUAL-mode|, and
  3923. that we don't want of the previous behavior, we can explicitly invoke the
  3924. function with |:VCall| -- command that doesn't actually exist. Check
  3925. lh-tex/ftplugin/tex/tex-set.vim |s:MapMenu4Env| for such an example.
  3926. Fourth thing: actually, lh#menu#make() is not restricted to commands. The
  3927. action can be anything that could come at the right hand side of any |:map| or
  3928. |:menu| action. But this time, you have to be cautious with the modes you
  3929. dedicate your map to. I won't give any related example ; this is the
  3930. underlying approach in |lh#menu#IVN_make()|.
  3931. *lh#menu#make()_modes*
  3932. Implementation details:~
  3933. The actual creation of the mappings is delegated to |lh#menu#map_all()|.
  3934. If the {action} to execute doesn't start with ':', it is left untransformed,
  3935. otherwise it is adapted depending on each {mode}:
  3936. - INSERT-mode: each recognized |:command| call is prepended with |i_CTRL-O|
  3937. - NORMAL-mode: the {action} is used as it is
  3938. - VISUAL-mode: ":Vcall" is replaced by "\<cr>gV", otherwise the selection is
  3939. recorded into @v register, the {action} command is executed after a
  3940. |v_CTRL-C|, and eventually @v is cleared.
  3941. The use is @v is deprecated, rely instead on |lh#menu#is_in_visual_mode()|
  3942. and on |lh#selection#visual()|.
  3943. - COMMAND-mode: the {action} is prepended with |c_CTRL-C|.
  3944. Examples:
  3945. See tests/lh/test-menu-map.vim
  3946. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3947. *lh#menu#IVN_make()* {{{3
  3948. Mappings & menus inserting text~
  3949. lh#menu#IVN_make(<priority>, {text}, {key}, {IM-action}, {VM-action}, {NM-action} [, {nore-IM}, {nore-VM}, {nore-NM}])~
  3950. lh#menu#IVN_MenuMake() accepts three different actions for the three modes:
  3951. INSERT, VISUAL and NORMAL. The mappings defined will be relative to the
  3952. current buffer -- this function is addressed to ftplugins writers. The last
  3953. arguments specify whether the inner mappings and abbreviations embedded within
  3954. the actions should be expanded or not ; i.e. are we defining
  3955. «noremaps/noremenus» ?
  3956. You will find very simple examples of what could be done at the end of
  3957. menu-map.vim. Instead, I'll show here an extract of my TeX ftplugin: it
  3958. defines complex functions that will help to define very simply the different
  3959. mappings I use. You could find another variation on this theme in
  3960. ftplugin/html/html_set.vim.
  3961. >
  3962. :MapMenu 50.370.300 &LaTeX.&Fonts.&Emphasize ]em emph
  3963. call <SID>MapMenu4Env("50.370.200", '&LaTeX.&Environments.&itemize',
  3964. \ ']ei', 'itemize', '\item ')
  3965. The first command binds ]em to \emph{} for the three different modes. In
  3966. INSERT mode, the cursor is positioned between the curly brackets, and a marker
  3967. is added after the closing bracket -- cf. my bracketing system. In VISUAL
  3968. mode, the curly brackets are added around the visual area. In NORMAL mode, the
  3969. area is considered to be the current word.
  3970. The second call binds for the three modes: ]ei to:
  3971. >
  3972. \begin{itemize}
  3973. \item
  3974. \end{itemize}
  3975. The definition of the different functions and commands involved just follows.
  3976. >
  3977. command -nargs=1 -buffer MapMenu :call <SID>MapMenu(<f-args>)
  3978. function! s:MapMenu(code,text,binding, tex_cmd, ...)
  3979. let _2visual = (a:0 > 0) ? a:1 : "viw"
  3980. " If the tex_cmd starts with an alphabetic character, then suppose the
  3981. " command must begin with a '\'.
  3982. let texc = ((a:tex_cmd[0] =~ '\a') ? '\' : "") . a:tex_cmd
  3983. call lh#menu#IVN_make(a:code, a:text.' -- ' . texc .'{}', a:binding,
  3984. \ texc.'{',
  3985. \ '<ESC>`>a}<ESC>`<i' . texc . '{<ESC>%l',
  3986. \ ( (_2visual=='0') ? "" : _2visual.a:binding),
  3987. \ 0, 1, 0)
  3988. endfunction
  3989. " a function and its map to close a "}", and that works whatever the
  3990. " activation states of the brackets and marking features are.
  3991. function! s:Close()
  3992. if strlen(maparg('{')) == 0 | exe "normal a} \<esc>"
  3993. elseif exists("b:usemarks") && (b:usemarks==1) | exe "normal ¡jump! "
  3994. else | exe "normal a "
  3995. endif
  3996. endfunction
  3997. imap <buffer> ¡close! <c-o>:call <SID>Close()<cr>
  3998. function! s:MapMenu4Env(code,text,binding, tex_env, middle, ...)
  3999. let _2visual = (a:0 > 0) ? a:1 : "vip"
  4000. let b = "'" . '\begin{' . a:tex_env . '}' . "'"
  4001. let e = "'" . '\end{' . a:tex_env . '}' . "'"
  4002. call IVN_MenuMake(a:code, a:text, a:binding,
  4003. \ '\begin{'.a:tex_env.'¡close!<CR>'.a:middle.' <CR>\end{'.a:tex_env.'}<C-F><esc>ks',
  4004. \ ':VCall MapAroundVisualLines('.b. ',' .e.',1,1)',
  4005. \ _2visual.a:binding,
  4006. \ 0, 1, 0)
  4007. endfunction
  4008. Examples:
  4009. See tests/lh/test-menu-map.vim
  4010. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4011. *lh#menu#is_in_visual_mode()* {{{3
  4012. lh#menu#is_in_visual_mode()~
  4013. @return a boolean that tells whether the {action} used in
  4014. |lh#menu#is_in_visual_mode()| has been invoked from the VISUAL-mode.
  4015. NB: this function deprecates the test on @v.
  4016. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4017. *lh#menu#map_all()* {{{3
  4018. lh#menu#map_all({map-type}[, {map-args...}])~
  4019. This function is a helper function that defines several mappings at once as
  4020. |:amenu| would do.
  4021. @param {map-type} String of the form "[aincv]*(nore)?map" that tells the
  4022. mode on which mappings should be defined, and whether
  4023. the mappings shall be |:noremap|.
  4024. @param {map-args...} Rest of the parameters that defines the mapping
  4025. The action to execute will be corrected depending on the current mode. See
  4026. |lh#menu#make()_modes| for more details.
  4027. ------------------------------------------------------------------------------
  4028. COMMANDS RELATED FUNCTIONS *lhvl#command* {{{2
  4029. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4030. *lh#command#new()* {{{3
  4031. Highly Experimental.
  4032. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4033. *lh#command#Fargs2String()* {{{3
  4034. lh#command#Fargs2String({aList})~
  4035. @param[in,out] aList list of params from <f-args>
  4036. @see tests/lh/test-Fargs2String.vim
  4037. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4038. *lh#command#complete()* {{{3
  4039. lh#command#complete({argLead}, {cmdLine}, {cursorPos})~
  4040. Under developpement
  4041. ------------------------------------------------------------------------------
  4042. BUFFERS RELATED FUNCTIONS *lhvl#buffer* {{{2
  4043. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4044. *lh#buffer#list()* {{{3
  4045. lh#buffer#list()~
  4046. @return The |List| of |buflisted| buffers.
  4047. e.g.: >
  4048. echo lh#list#transform(lh#buffer#list(), [], "bufname")
  4049. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4050. *lh#buffer#Find()* {{{3
  4051. lh#buffer#Find({filename}) (*deprecated*)~
  4052. *lh#buffer#find()*
  4053. lh#buffer#find({filename})~
  4054. Searchs for a window where the buffer is opened.
  4055. @param {filename}
  4056. @return The number of the first window found, in which {filename} is opened.
  4057. If {filename} is opened in a window, jump to this window. Otherwise, return
  4058. -1.
  4059. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4060. *lh#buffer#Jump()* {{{3
  4061. lh#buffer#Jump({filename}, {cmd}) (*deprecated*)~
  4062. *lh#buffer#jump()*
  4063. lh#buffer#jump({filename}, {cmd})~
  4064. Jumps to the window where the buffer is opened, or open the buffer in a new
  4065. windows if none match.
  4066. @param {filename}
  4067. @param {cmd}
  4068. @return Nothing.
  4069. If {filename} is opened in a window, jump to this window.
  4070. Otherwise, execute {cmd} with {filename} as a parameter. Typical values for
  4071. the command will be "sp" or "vsp". (see |:split|, |:vsplit|).
  4072. N.B.: While it is not the rationale behind this function, other commands that
  4073. does not open the buffer may be used in the {cmd} parameter.
  4074. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4075. *lh#buffer#Scratch()* {{{3
  4076. lh#buffer#Scratch({bname},{where}) (*deprecated*)~
  4077. *scratch* *lh#buffer#scratch()*
  4078. lh#buffer#scratch({bname},{where})~
  4079. Split-opens a new scratch buffer.
  4080. @param {bname} Name for the new scratch buffer
  4081. @param {where} Where the new scratch buffer will be opened ('', or 'v')
  4082. @post The buffer has the following properties set:
  4083. 'bt'=nofile, 'bh'=wipe, 'nobl', 'noswf', 'ro'
  4084. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4085. *lhvl-dialog* *lh#buffer#dialog#* {{{3
  4086. Functions for building interactive dialogs~
  4087. Unlike other |lh-vim-lib| functions which can be used independently from each
  4088. others, all the lh#buffer#dialog#*() functions constitute a coherent framework
  4089. to define interactive dialogs.
  4090. For the moment it only supports the selection of one or several items in a
  4091. list.
  4092. From a end-user point of view, a list of items is displayed in a (|scratch|)
  4093. buffer. If enabled, the user can select (/tag) one or several items, and then
  4094. validate its choice. He can always abort and quit the dialog. A few other
  4095. features are also supported: a help message can be shown, the list may be
  4096. colored, etc.
  4097. The items displayed can be of any kind (function signatures, email addresses,
  4098. suggested spellings, ...), as well as the validating action. The
  4099. help-header can be customized, as well as colours, other mappings, ...
  4100. However the list displaying + selection aspect is almost hardcoded.
  4101. How it works~
  4102. ------------
  4103. Scripts have to call the function *lh#buffer#dialog#new()*
  4104. lh#buffer#dialog#new(bname, title, where, support-tagging, action, choices)~
  4105. with:
  4106. - {bname} being the name the |scratch| buffer will receive.
  4107. - {title} the title that appears at the first line of the scratch buffer.
  4108. I usually use it to display the name of the "client" script, its version,
  4109. and its purpose/what to do.
  4110. - {where} are |:split| options (like "bot below") used to open the scratch
  4111. buffer.
  4112. - {support-tagging} is a boolean (0/1) option to enable the multi-selection.
  4113. - {action} is the name of the callback function (more
  4114. advanced calling mechanisms latter may be supported later with
  4115. |lhvl-functions|).
  4116. - {choices} is the |List| of exact strings to display.
  4117. The #new function builds and returns a |Dictionary|, it also opens and fills
  4118. the scratch buffer, and put us within its context -- i.e. any |:map-<buffer>|
  4119. or other buffer-related definitions will done in the new scratch buffer.
  4120. Thus, if we want to add other mappings, and set a syntax highlighting for the
  4121. new buffer, it is done at this point (see the *s:PostInit()* function in my
  4122. "client" scripts like |lh-tags|).
  4123. At this point, I also add all the high level information to the
  4124. dictionary (for instance, the list of function signatures is nice, but
  4125. it does not provides enough information (the corresponding file, the
  4126. command to jump to the definition/declaration, the scope, ...)
  4127. The dictionary returned is filled with the following information:
  4128. - buffer ids,
  4129. - where was the cursor at the time of the creation of the new scratch buffer,
  4130. - name of the callback function.
  4131. Regarding the callback function: *lhvl-dialog-select-callback*
  4132. - It ca not be a |script-local| function, only global and autoload functions
  4133. are supported.
  4134. - When called, we are still within the scratch buffer context.
  4135. - It must accept a |List| of numbers as its first parameter: the index (+1) of
  4136. the items selected.
  4137. - The number 0, when in the list, means "aborted". In that case, the
  4138. callback function is expected to call |lh#buffer#dialog#quit()| that will
  4139. terminate the scratch buffer (with |:quit|), and jump back to where we were
  4140. when #new was called, and display a little "Abort" message.
  4141. - We can terminate the dialog with just :quit if we don't need to jump
  4142. back anywhere. For instance, lh-tags callback function first
  4143. terminates the dialog, then jumps to the file where the selected tag
  4144. comes from.
  4145. - It's completely asynchronous: the callback function does not return anything
  4146. to anyone, but instead applies transformations in other places.
  4147. This aspect is very important. I don't see how this kind of feature can work
  4148. if not asynchronously in vim.
  4149. How to customize it:
  4150. - *lh#buffer#dialog#quit()* can be explicitly called, from a registered select
  4151. callback (|lhvl-dialog-select-callback|), in order to terminate the dialog.
  4152. - *lh#buffer#dialog#add_help()* can be used to complete the help/usage message
  4153. in both its short and long form.
  4154. - *lh#buffer#dialog#update()* can be called after the list of items has been
  4155. altered in order to refresh what is displayed. The rationale behind this
  4156. feature is to support sorting, filtering, items expansion, etc. See
  4157. |lh-tags| implementation for an example.
  4158. - *lh#buffer#dialog#select()* can be used in new mappings in order to handle
  4159. differently the selected items.
  4160. |lh-tags| uses this function to map 'o' to the split-opening of the selected
  4161. items.
  4162. NB: the way this feature is supported may change in future releases.
  4163. Limitations:
  4164. This script is a little bit experimental (even if it the result of almost 10
  4165. years of evolution), and it is a little bit cumbersome.
  4166. - it is defined to support only one callback -- see the hacks in |lh-tags| to
  4167. workaround this limitation.
  4168. - it is defined to display list of items, and to select one or several items
  4169. in the end.
  4170. - and of course, it requires many other functions from |lh-vim-lib|, but
  4171. nothing else.
  4172. ------------------------------------------------------------------------------
  4173. SYNTAX RELATED FUNCTIONS *lhvl#syntax* {{{2
  4174. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4175. *lh#syntax#NameAt()* {{{3
  4176. lh#syntax#NameAt({lnum},{col}[,{trans}]) (*deprecated*)~
  4177. *lh#syntax#name_at()*
  4178. lh#syntax#name_at({lnum},{col}[,{trans}])~
  4179. @param {lnum} line of the character
  4180. @param {col} column of the character
  4181. @param {trans} see |synID()|, default=0
  4182. @return the syntax kind of the given character at {lnum}, {col}
  4183. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4184. *lh#syntax#NameAtMark()* {{{3
  4185. lh#syntax#NameAtMark({mark}[,{trans}]) (*deprecated*)~
  4186. *lh#syntax#is_a_comment()* {{{3
  4187. lh#syntax#is_a_comment({mark})~
  4188. @param {mark} position of the character
  4189. @return whether the character is within a Comment syntax element.
  4190. *lh#syntax#name_at_mark()* {{{3
  4191. lh#syntax#name_at_mark({mark}[,{trans}])~
  4192. @param {mark} position of the character
  4193. @param {trans} see |synID()|, default=0
  4194. @return the syntax kind of the character at the given |mark|.
  4195. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4196. *lh#syntax#Skip()* *lh#syntax#SkipAt()* *lh#syntax#SkipAtMark()* {{{3
  4197. lh#syntax#Skip() (*deprecated*)~
  4198. lh#syntax#SkipAt({lnum},{col}) (*deprecated*)~
  4199. lh#syntax#SkipAtMark({mark}) (*deprecated*)~
  4200. *lh#syntax#skip()* *lh#syntax#skip_at()* *lh#syntax#skip_at_mark()*
  4201. lh#syntax#skip()~
  4202. lh#syntax#skip_at({lnum},{col})~
  4203. lh#syntax#skip_at_mark({mark})~
  4204. Functions to be used with |searchpair()| functions in order to search for a
  4205. pair of elements, without taking comments, strings, characters and doxygen
  4206. (syntax) contexts into account while searching.
  4207. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4208. *lh#syntax#list_raw()* {{{3
  4209. lh#syntax#list_raw({name})~
  4210. @param {group-name}
  4211. @return the result of "syn list {group-name}" as a string
  4212. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  4213. *lh#syntax#list()* {{{3
  4214. lh#syntax#list()~
  4215. @param {group-name}
  4216. @return the result of "syn list {group-name}" as a list.
  4217. This function tries to interpret the result of the raw list of syntax
  4218. elements.
  4219. ------------------------------------------------------------------------------
  4220. COMPLETION RELATED FUNCTIONS *lhvl#completion* {{{2
  4221. *lh#icomplete#run()* {{{3
  4222. lh#icomplete#run(startcol, matches, Hook)~
  4223. Runs |complete()| and registers the {Hook} to be executed when the user
  4224. selects one entry in the menu.
  4225. ------------------------------------------------------------------------------
  4226. }}}1
  4227. ==============================================================================
  4228. © Luc Hermitte, 2001-2012 <http://code.google.com/p/lh-vim/>, CC by SA 3.0 {{{1
  4229. $Id: lh-vim-lib.txt 558 2012-04-10 08:00:00Z luc.hermitte $
  4230. VIM: let b:VS_language = 'american'
  4231. vim:ts=8:sw=4:tw=80:fo=tcq2:ft=help:
  4232. vim600:fdm=marker:
  4233. lh-vim-lib-addon-info.txt [[[1
  4234. 10
  4235. {
  4236. "name" : "lh-vim-lib",
  4237. "version" : "dev",
  4238. "author" : "Luc Hermitte <http://code.google.com/p/lh-vim/wiki/contact>",
  4239. "maintainer" : "Luc Hermitte <http://code.google.com/p/lh-vim/wiki/contact>",
  4240. "repository" : {"type": "svn", "url": "http://lh-vim.googlecode.com/svn/vim-lib/trunk"},
  4241. "dependencies" : {},
  4242. "description" : "Library of VimL functions",
  4243. "homepage" : "http://code.google.com/p/lh-vim/wiki/lhVimLib"
  4244. }
  4245. lh-vim-lib.README [[[1
  4246. 20
  4247. ------------------
  4248. lh-vim-lib 3.1.0
  4249. ------------------
  4250. lh-vim-lib is a library plugin for vim v7+. It is just a collection of
  4251. functions that are meant to be used by script writers.
  4252. Audience : Vim script writers
  4253. Requirements : Vim 7.1, Vim 7.3 for some functions
  4254. Required by : Just a few other plugins for the moment
  4255. Author : Luc Hermitte
  4256. License : GPLv3 with exceptions
  4257. http://code.google.com/p/lh-vim/wiki/License
  4258. More Help At : http://code.google.com/p/lh-vim/wiki/lhVimLib
  4259. Vim script#214: http://www.vim.org/scripts/script.php?script_id=214
  4260. Repository :
  4261. svn checkout http://lh-vim.googlecode.com/svn/vim-lib/trunk lh-vim-lib
  4262. VAM :
  4263. http://code.google.com/p/lh-vim/source/browse/vim-lib/trunk/lh-vim-lib-addon-info.txt
  4264. macros/menu-map.vim [[[1
  4265. 87
  4266. "===========================================================================
  4267. " $Id: menu-map.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  4268. " File: macros/menu-map.vim
  4269. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  4270. " <URL:http://code.google.com/p/lh-vim/>
  4271. "
  4272. " Purpose: Define functions to build mappings and menus at the same time
  4273. "
  4274. " License: GPLv3 with exceptions
  4275. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  4276. " Version: 3.0.0
  4277. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (02nd Dec 2006)
  4278. "
  4279. " Last Changes: {{{
  4280. " Version 3.0.0:
  4281. " GPLv3
  4282. " Version 2.0.0:
  4283. " Moved to vim7,
  4284. " Functions moved to {rtp}/autoload/
  4285. " Version 1.6.2:
  4286. " (*) Silent mappings and menus
  4287. " Version 1.6. :
  4288. " (*) Uses has('gui_running') instead of has('gui') to check if
  4289. " we can generate the menu.
  4290. " Version 1.5. :
  4291. " (*) visual mappings launched from select-mode don't end with
  4292. " text still selected -- applied to :VCalls
  4293. " Version 1.4. :
  4294. " (*) address obfuscated for spammers
  4295. " (*) support the local option
  4296. " b:want_buffermenu_or_global_disable if we don't want
  4297. " buffermenu to be used systematically.
  4298. " 0 -> buffer menu not used
  4299. " 1 -> buffer menu used
  4300. " 2 -> the VimL developper will use a global disable.
  4301. " cf.: tex-maps.vim:: s:SimpleMenu()
  4302. " and texmenus.vim
  4303. " Version 1.3. :
  4304. " (*) add continuation lines support ; cf 'cpoptions'
  4305. " Version 1.2. :
  4306. " (*) Code folded.
  4307. " (*) Take advantage of buffermenu.vim if present for local
  4308. " menus.
  4309. " (*) If non gui is available, the menus won't be defined
  4310. " Version 1.1. :
  4311. " (*) Bug corrected :
  4312. " vnore(map\|menu) does not imply v+n(map\|menu) any more
  4313. " }}}
  4314. "
  4315. " Inspired By: A function from Benji Fisher
  4316. "
  4317. " TODO: (*) no menu if no gui.
  4318. "
  4319. "===========================================================================
  4320. if exists("g:loaded_menu_map") | finish | endif
  4321. let g:loaded_menu_map = 1
  4322. "" line continuation used here ??
  4323. let s:cpo_save = &cpo
  4324. set cpo&vim
  4325. "=========================================================================
  4326. " Commands {{{
  4327. command! -nargs=+ -bang MAP map<bang> <args>
  4328. command! -nargs=+ IMAP imap <args>
  4329. command! -nargs=+ NMAP nmap <args>
  4330. command! -nargs=+ CMAP cmap <args>
  4331. command! -nargs=+ VMAP vmap <args>
  4332. command! -nargs=+ AMAP
  4333. \ call lh#menu#map_all('amap', <f-args>)
  4334. command! -nargs=+ -bang NOREMAP noremap<bang> <args>
  4335. command! -nargs=+ INOREMAP inoremap <args>
  4336. command! -nargs=+ NNOREMAP nnoremap <args>
  4337. command! -nargs=+ CNOREMAP cnoremap <args>
  4338. command! -nargs=+ VNOREMAP vnoremap <args>
  4339. command! -nargs=+ ANOREMAP
  4340. \ call lh#menu#map_all('anoremap', <f-args>)
  4341. " }}}
  4342. " End !
  4343. let &cpo = s:cpo_save
  4344. finish
  4345. "=========================================================================
  4346. " vim600: set fdm=marker:
  4347. mkVba/mk-lh-vim-lib.vim [[[1
  4348. 60
  4349. "=============================================================================
  4350. " $Id: mk-lh-vim-lib.vim 606 2012-05-31 17:09:46Z luc.hermitte@gmail.com $
  4351. " File: mkVba/mk-lh-lib.vim
  4352. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  4353. " <URL:http://code.google.com/p/lh-vim/>
  4354. " License: GPLv3 with exceptions
  4355. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  4356. " Version: 3.1.1
  4357. let s:version = '3.1.1'
  4358. " Created: 06th Nov 2007
  4359. " Last Update: $Date: 2012-05-31 19:09:46 +0200 (jeu 31 mai 2012) $
  4360. "------------------------------------------------------------------------
  4361. cd <sfile>:p:h
  4362. try
  4363. let save_rtp = &rtp
  4364. let &rtp = expand('<sfile>:p:h:h').','.&rtp
  4365. exe '24,$MkVimball! lh-vim-lib-'.s:version
  4366. set modifiable
  4367. set buftype=
  4368. finally
  4369. let &rtp = save_rtp
  4370. endtry
  4371. finish
  4372. autoload/lh/askvim.vim
  4373. autoload/lh/buffer.vim
  4374. autoload/lh/buffer/dialog.vim
  4375. autoload/lh/command.vim
  4376. autoload/lh/common.vim
  4377. autoload/lh/encoding.vim
  4378. autoload/lh/env.vim
  4379. autoload/lh/event.vim
  4380. autoload/lh/float.vim
  4381. autoload/lh/function.vim
  4382. autoload/lh/graph/tsort.vim
  4383. autoload/lh/icomplete.vim
  4384. autoload/lh/list.vim
  4385. autoload/lh/menu.vim
  4386. autoload/lh/option.vim
  4387. autoload/lh/path.vim
  4388. autoload/lh/position.vim
  4389. autoload/lh/syntax.vim
  4390. autoload/lh/visual.vim
  4391. doc/lh-vim-lib.txt
  4392. lh-vim-lib-addon-info.txt
  4393. lh-vim-lib.README
  4394. macros/menu-map.vim
  4395. mkVba/mk-lh-vim-lib.vim
  4396. plugin/let.vim
  4397. plugin/lhvl.vim
  4398. plugin/ui-functions.vim
  4399. plugin/words_tools.vim
  4400. tests/lh/function.vim
  4401. tests/lh/list.vim
  4402. tests/lh/path.vim
  4403. tests/lh/test-Fargs2String.vim
  4404. tests/lh/test-askmenu.vim
  4405. tests/lh/test-command.vim
  4406. tests/lh/test-menu-map.vim
  4407. tests/lh/test-toggle-menu.vim
  4408. tests/lh/topological-sort.vim
  4409. plugin/let.vim [[[1
  4410. 57
  4411. "=============================================================================
  4412. " $Id: let.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  4413. " File: plugin/let.vim {{{1
  4414. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  4415. " <URL:http://code.google.com/p/lh-vim/>
  4416. " License: GPLv3 with exceptions
  4417. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  4418. " Version: 3.0.0
  4419. " Created: 31st May 2010
  4420. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  4421. "------------------------------------------------------------------------
  4422. " Description:
  4423. " Defines a command :LetIfUndef that sets a variable if undefined
  4424. "
  4425. "------------------------------------------------------------------------
  4426. " Installation:
  4427. " Drop this file into {rtp}/plugin
  4428. " Requires Vim7+
  4429. " History:
  4430. " v2.2.1: first version of this command into lh-vim-lib
  4431. " v3.0.0: GPLv3
  4432. " TODO:
  4433. " }}}1
  4434. "=============================================================================
  4435. " Avoid global reinclusion {{{1
  4436. let s:k_version = 300
  4437. if &cp || (exists("g:loaded_let")
  4438. \ && (g:loaded_let >= s:k_version)
  4439. \ && !exists('g:force_reload_let'))
  4440. finish
  4441. endif
  4442. let g:loaded_let = s:k_version
  4443. let s:cpo_save=&cpo
  4444. set cpo&vim
  4445. " Avoid global reinclusion }}}1
  4446. "------------------------------------------------------------------------
  4447. " Commands and Mappings {{{1
  4448. command! -nargs=+ LetIfUndef call s:LetIfUndef(<f-args>)
  4449. " Commands and Mappings }}}1
  4450. "------------------------------------------------------------------------
  4451. " Functions {{{1
  4452. " Note: most functions are best placed into
  4453. " autoload/«your-initials»/«let».vim
  4454. " Keep here only the functions are are required when the plugin is loaded,
  4455. " like functions that help building a vim-menu for this plugin.
  4456. function! s:LetIfUndef(var, value)
  4457. if !exists(a:var)
  4458. let {a:var} = eval(a:value)
  4459. endif
  4460. endfunction
  4461. " Functions }}}1
  4462. "------------------------------------------------------------------------
  4463. let &cpo=s:cpo_save
  4464. "=============================================================================
  4465. " vim600: set fdm=marker:
  4466. plugin/lhvl.vim [[[1
  4467. 48
  4468. "=============================================================================
  4469. " $Id: lhvl.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  4470. " File: plugin/lhvl.vim {{{1
  4471. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  4472. " <URL:http://code.google.com/p/lh-vim/>
  4473. " License: GPLv3 with exceptions
  4474. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  4475. " Version: 3.0.0
  4476. " Created: 27th Apr 2010
  4477. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  4478. "------------------------------------------------------------------------
  4479. " Description:
  4480. " Non-function resources from lh-vim-lib
  4481. "
  4482. "------------------------------------------------------------------------
  4483. " Installation:
  4484. " Drop the file into {rtp}/plugin
  4485. " History:
  4486. " v2.2.1 first version
  4487. " v3.0.0 GPLv3
  4488. " TODO: «missing features»
  4489. " }}}1
  4490. "=============================================================================
  4491. " Avoid global reinclusion {{{1
  4492. let s:k_version = 300
  4493. if &cp || (exists("g:loaded_lhvl")
  4494. \ && (g:loaded_lhvl >= s:k_version)
  4495. \ && !exists('g:force_reload_lhvl'))
  4496. finish
  4497. endif
  4498. let g:loaded_lhvl = s:k_version
  4499. let s:cpo_save=&cpo
  4500. set cpo&vim
  4501. " Avoid global reinclusion }}}1
  4502. "------------------------------------------------------------------------
  4503. " Commands and Mappings {{{1
  4504. " Moved from lh-cpp
  4505. command! PopSearch :call histdel('search', -1)| let @/=histget('search',-1)
  4506. " Commands and Mappings }}}1
  4507. "------------------------------------------------------------------------
  4508. " Functions {{{1
  4509. " Functions }}}1
  4510. "------------------------------------------------------------------------
  4511. let &cpo=s:cpo_save
  4512. "=============================================================================
  4513. " vim600: set fdm=marker:
  4514. plugin/ui-functions.vim [[[1
  4515. 490
  4516. "=============================================================================
  4517. " File: plugin/ui-functions.vim {{{1
  4518. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  4519. " <URL:http://code.google.com/p/lh-vim/>
  4520. " License: GPLv3 with exceptions
  4521. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  4522. " Version: 3.0.0
  4523. " Created: 18th nov 2002
  4524. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (19th Mar 2012)
  4525. "------------------------------------------------------------------------
  4526. " Description: Functions for the interaction with a User Interface.
  4527. " The UI can be graphical or textual.
  4528. " At first, this was designed to ease the syntax of
  4529. " mu-template's templates.
  4530. "
  4531. " Option: {{{2
  4532. " {[bg]:ui_type}
  4533. " = "g\%[ui]",
  4534. " = "t\%[ext]" ; the call must not be |:silent|
  4535. " = "f\%[te]"
  4536. " }}}2
  4537. "------------------------------------------------------------------------
  4538. " Installation: Drop this into one of your {rtp}/plugin/ directories.
  4539. " History: {{{2
  4540. " v0.01 Initial Version
  4541. " v0.02
  4542. " (*) Code "factorisations"
  4543. " (*) Help on <F1> enhanced.
  4544. " (*) Small changes regarding the parameter accepted
  4545. " (*) Function SWITCH
  4546. " v0.03
  4547. " (*) Small bug fix with INPUT()
  4548. " v0.04
  4549. " (*) New function: WHICH()
  4550. " v0.05
  4551. " (*) In vim7e, inputdialog() returns a trailing '\n'. INPUT() strips the
  4552. " NL character.
  4553. " v0.06
  4554. " (*) :s/echoerr/throw/ => vim7 only
  4555. " v2.2.0
  4556. " (*) menu to switch the ui_type
  4557. " v2.2.6
  4558. " (*) CONFIRM() and WHICH() accept lists of {choices}
  4559. " v3.0.0 GPLv3
  4560. "
  4561. " TODO: {{{2
  4562. " (*) Save the hl-User1..9 before using them
  4563. " (*) Possibility other than &statusline:
  4564. " echohl User1 |echon "bla"|echohl User2|echon "bli"|echohl None
  4565. " (*) Wraps too long choices-line (length > term-width)
  4566. " (*) Add to the documentation: "don't use CTRL-C to abort !!"
  4567. " (*) Look if I need to support 'wildmode'
  4568. " (*) 3rd mode: return string for FTE
  4569. " (*) 4th mode: interaction in a scratch buffer
  4570. "
  4571. " }}}1
  4572. "=============================================================================
  4573. " Avoid reinclusion {{{1
  4574. "
  4575. if exists("g:loaded_ui_functions") && !exists('g:force_reload_ui_functions')
  4576. finish
  4577. endif
  4578. let g:loaded_ui_functions = 1
  4579. let s:cpo_save=&cpo
  4580. set cpo&vim
  4581. " }}}1
  4582. "------------------------------------------------------------------------
  4583. " External functions {{{1
  4584. " Function: IF(var, then, else) {{{2
  4585. function! IF(var,then, else)
  4586. let o = s:Opt_type() " {{{3
  4587. if o =~ 'g\%[ui]\|t\%[ext]' " {{{4
  4588. return a:var ? a:then : a:else
  4589. elseif o =~ 'f\%[te]' " {{{4
  4590. return s:if_fte(a:var, a:then, a:else)
  4591. else " {{{4
  4592. throw "UI-Fns::IF(): Unkonwn user-interface style (".o.")"
  4593. endif
  4594. " }}}3
  4595. endfunction
  4596. " Function: SWITCH(var, case, action [, case, action] [default_action]) {{{2
  4597. function! SWITCH(var, ...)
  4598. let o = s:Opt_type() " {{{3
  4599. if o =~ 'g\%[ui]\|t\%[ext]' " {{{4
  4600. let explicit_def = ((a:0 % 2) == 1)
  4601. let default = explicit_def ? a:{a:0} : ''
  4602. let i = a:0 - 1 - explicit_def
  4603. while i > 0
  4604. if a:var == a:{i}
  4605. return a:{i+1}
  4606. endif
  4607. let i -= 2
  4608. endwhile
  4609. return default
  4610. elseif o =~ 'f\%[te]' " {{{4
  4611. return s:if_fte(a:var, a:then, a:else)
  4612. else " {{{4
  4613. throw "UI-Fns::SWITCH(): Unkonwn user-interface style (".o.")"
  4614. endif
  4615. " }}}3
  4616. endfunction
  4617. " Function: CONFIRM(text [, choices [, default [, type]]]) {{{2
  4618. function! CONFIRM(text, ...)
  4619. " 1- Check parameters {{{3
  4620. if a:0 > 4 " {{{4
  4621. throw "UI-Fns::CONFIRM(): too many parameters"
  4622. return 0
  4623. endif
  4624. " build the parameters string {{{4
  4625. let i = 1
  4626. while i <= a:0
  4627. if i == 1
  4628. if type(a:1) == type([])
  4629. let params = string(join(a:1, "\n"))
  4630. else
  4631. let params = 'a:{1}'
  4632. endif
  4633. else | let params .= ',a:{'.i.'}'
  4634. endif
  4635. let i += 1
  4636. endwhile
  4637. " 2- Choose the correct way to execute according to the option {{{3
  4638. let o = s:Opt_type()
  4639. if o =~ 'g\%[ui]' " {{{4
  4640. exe 'return confirm(a:text,'.params.')'
  4641. elseif o =~ 't\%[ext]' " {{{4
  4642. if !has('gui_running') && has('dialog_con')
  4643. exe 'return confirm(a:text,'.params.')'
  4644. else
  4645. exe 'return s:confirm_text("none", a:text,'.params.')'
  4646. endif
  4647. elseif o =~ 'f\%[te]' " {{{4
  4648. exe 'return s:confirm_fte(a:text,'.params.')'
  4649. else " {{{4
  4650. throw "UI-Fns::CONFIRM(): Unkonwn user-interface style (".o.")"
  4651. endif
  4652. " }}}3
  4653. endfunction
  4654. " Function: INPUT(prompt [, default ]) {{{2
  4655. function! INPUT(prompt, ...)
  4656. " 1- Check parameters {{{3
  4657. if a:0 > 4 " {{{4
  4658. throw "UI-Fns::INPUT(): too many parameters"
  4659. return 0
  4660. endif
  4661. " build the parameters string {{{4
  4662. let i = 1 | let params = ''
  4663. while i <= a:0
  4664. if i == 1 | let params = 'a:{1}'
  4665. else | let params .= ',a:{'.i.'}'
  4666. endif
  4667. let i += 1
  4668. endwhile
  4669. " 2- Choose the correct way to execute according to the option {{{3
  4670. let o = s:Opt_type()
  4671. if o =~ 'g\%[ui]' " {{{4
  4672. exe 'return matchstr(inputdialog(a:prompt,'.params.'), ".\\{-}\\ze\\n\\=$")'
  4673. elseif o =~ 't\%[ext]' " {{{4
  4674. exe 'return input(a:prompt,'.params.')'
  4675. elseif o =~ 'f\%[te]' " {{{4
  4676. exe 'return s:input_fte(a:prompt,'.params.')'
  4677. else " {{{4
  4678. throw "UI-Fns::INPUT(): Unkonwn user-interface style (".o.")"
  4679. endif
  4680. " }}}3
  4681. endfunction
  4682. " Function: COMBO(prompt, choice [, ... ]) {{{2
  4683. function! COMBO(prompt, ...)
  4684. " 1- Check parameters {{{3
  4685. if a:0 > 4 " {{{4
  4686. throw "UI-Fns::COMBO(): too many parameters"
  4687. return 0
  4688. endif
  4689. " build the parameters string {{{4
  4690. let i = 1
  4691. while i <= a:0
  4692. if i == 1 | let params = 'a:{1}'
  4693. else | let params .= ',a:{'.i.'}'
  4694. endif
  4695. let i += 1
  4696. endwhile
  4697. " 2- Choose the correct way to execute according to the option {{{3
  4698. let o = s:Opt_type()
  4699. if o =~ 'g\%[ui]' " {{{4
  4700. exe 'return confirm(a:prompt,'.params.')'
  4701. elseif o =~ 't\%[ext]' " {{{4
  4702. exe 'return s:confirm_text("combo", a:prompt,'.params.')'
  4703. elseif o =~ 'f\%[te]' " {{{4
  4704. exe 'return s:combo_fte(a:prompt,'.params.')'
  4705. else " {{{4
  4706. throw "UI-Fns::COMBO(): Unkonwn user-interface style (".o.")"
  4707. endif
  4708. " }}}3
  4709. endfunction
  4710. " Function: WHICH(function, prompt, choice [, ... ]) {{{2
  4711. function! WHICH(fn, prompt, ...)
  4712. " 1- Check parameters {{{3
  4713. " build the parameters string {{{4
  4714. let i = 1
  4715. while i <= a:0
  4716. if i == 1
  4717. if type(a:1) == type([])
  4718. let choices = a:1
  4719. else
  4720. let choices = split(a:1, "\n")
  4721. endif
  4722. let params = 'a:{1}'
  4723. else | let params .= ',a:{'.i.'}'
  4724. endif
  4725. let i += 1
  4726. endwhile
  4727. " 2- Execute the function {{{3
  4728. exe 'let which = '.a:fn.'(a:prompt,'.params.')'
  4729. if 0 >= which | return ''
  4730. else
  4731. return substitute(choices[which-1], '&', '', '')
  4732. endif
  4733. " }}}3
  4734. endfunction
  4735. " Function: CHECK(prompt, choice [, ... ]) {{{2
  4736. function! CHECK(prompt, ...)
  4737. " 1- Check parameters {{{3
  4738. if a:0 > 4 " {{{4
  4739. throw "UI-Fns::CHECK(): too many parameters"
  4740. return 0
  4741. endif
  4742. " build the parameters string {{{4
  4743. let i = 1
  4744. while i <= a:0
  4745. if i == 1 | let params = 'a:{1}'
  4746. else | let params .= ',a:{'.i.'}'
  4747. endif
  4748. let i += 1
  4749. endwhile
  4750. " 2- Choose the correct way to execute according to the option {{{3
  4751. let o = s:Opt_type()
  4752. if o =~ 'g\%[ui]' " {{{4
  4753. exe 'return s:confirm_text("check", a:prompt,'.params.')'
  4754. elseif o =~ 't\%[ext]' " {{{4
  4755. exe 'return s:confirm_text("check", a:prompt,'.params.')'
  4756. elseif o =~ 'f\%[te]' " {{{4
  4757. exe 'return s:check_fte(a:prompt,'.params.')'
  4758. else " {{{4
  4759. throw "UI-Fns::CHECK(): Unkonwn user-interface style (".o.")"
  4760. endif
  4761. " }}}3
  4762. endfunction
  4763. " }}}1
  4764. "------------------------------------------------------------------------
  4765. " Options setting {{{1
  4766. let s:OptionData = {
  4767. \ "variable": "ui_type",
  4768. \ "idx_crt_value": 1,
  4769. \ "values": ['gui', 'text', 'fte'],
  4770. \ "menu": { "priority": '500.2700', "name": '&Plugin.&LH.&UI type'}
  4771. \}
  4772. call lh#menu#def_toggle_item(s:OptionData)
  4773. " }}}1
  4774. "------------------------------------------------------------------------
  4775. " Internal functions {{{1
  4776. function! s:Option(name, default) " {{{2
  4777. if exists('b:ui_'.a:name) | return b:ui_{a:name}
  4778. elseif exists('g:ui_'.a:name) | return g:ui_{a:name}
  4779. else | return a:default
  4780. endif
  4781. endfunction
  4782. function! s:Opt_type() " {{{2
  4783. return s:Option('type', 'gui')
  4784. endfunction
  4785. "
  4786. " Function: s:status_line(current, hl [, choices] ) {{{2
  4787. " a:current: current item
  4788. " a:hl : Generic, Warning, Error
  4789. function! s:status_line(current, hl, ...)
  4790. " Highlightning {{{3
  4791. if a:hl == "Generic" | let hl = '%1*'
  4792. elseif a:hl == "Warning" | let hl = '%2*'
  4793. elseif a:hl == "Error" | let hl = '%3*'
  4794. elseif a:hl == "Info" | let hl = '%4*'
  4795. elseif a:hl == "Question" | let hl = '%5*'
  4796. else | let hl = '%1*'
  4797. endif
  4798. " Build the string {{{3
  4799. let sl_choices = '' | let i = 1
  4800. while i <= a:0
  4801. if i == a:current
  4802. let sl_choices .= ' '. hl .
  4803. \ substitute(a:{i}, '&\(.\)', '%6*\1'.hl, '') . '%* '
  4804. else
  4805. let sl_choices .= ' ' .
  4806. \ substitute(a:{i}, '&\(.\)', '%6*\1%*', '') . ' '
  4807. endif
  4808. let i += 1
  4809. endwhile
  4810. " }}}3
  4811. return sl_choices
  4812. endfunction
  4813. " Function: s:confirm_text(box, text [, choices [, default [, type]]]) {{{2
  4814. function! s:confirm_text(box, text, ...)
  4815. let help = "/<esc>/<s-tab>/<tab>/<left>/<right>/<cr>/<F1>"
  4816. " 1- Retrieve the parameters {{{3
  4817. let choices = ((a:0>=1) ? a:1 : '&Ok')
  4818. let default = ((a:0>=2) ? a:2 : (('check' == a:box) ? 0 : 1))
  4819. let type = ((a:0>=3) ? a:3 : 'Generic')
  4820. if 'none' == a:box | let prefix = ''
  4821. elseif 'combo' == a:box | let prefix = '( )_'
  4822. elseif 'check' == a:box | let prefix = '[ ]_'
  4823. let help = '/ '.help
  4824. else | let prefix = ''
  4825. endif
  4826. " 2- Retrieve the proposed choices {{{3
  4827. " Prepare the hot keys
  4828. let i = 0
  4829. while i != 26
  4830. let hotkey_{nr2char(i+65)} = 0
  4831. let i += 1
  4832. endwhile
  4833. let hotkeys = '' | let help_k = '/'
  4834. " Parse the choices
  4835. let i = 0
  4836. while choices != ""
  4837. let i += 1
  4838. let item = matchstr(choices, "^.\\{-}\\ze\\(\n\\|$\\)")
  4839. let choices = matchstr(choices, "\n\\zs.*$")
  4840. " exe 'anoremenu ]'.a:text.'.'.item.' :let s:choice ='.i.'<cr>'
  4841. if ('check' == a:box) && (strlen(default)>=i) && (1 == default[i-1])
  4842. " let choice_{i} = '[X]' . substitute(item, '&', '', '')
  4843. let choice_{i} = '[X]_' . item
  4844. else
  4845. " let choice_{i} = prefix . substitute(item, '&', '', '')
  4846. let choice_{i} = prefix . item
  4847. endif
  4848. if i == 1
  4849. let list_choices = 'choice_{1}'
  4850. else
  4851. let list_choices .= ',choice_{'.i.'}'
  4852. endif
  4853. " Update the hotkey.
  4854. let key = toupper(matchstr(choice_{i}, '&\zs.\ze'))
  4855. let hotkey_{key} = i
  4856. let hotkeys .= tolower(key) . toupper(key)
  4857. let help_k .= tolower(key)
  4858. endwhile
  4859. let nb_choices = i
  4860. if default > nb_choices | let default = nb_choices | endif
  4861. " 3- Run an interactive text menu {{{3
  4862. " Note: emenu can not be used through ":exe" {{{4
  4863. " let wcm = &wcm
  4864. " set wcm=<tab>
  4865. " exe ':emenu ]'.a:text.'.'."<tab>"
  4866. " let &wcm = wcm
  4867. " 3.1- Preparations for the statusline {{{4
  4868. " save the statusline
  4869. let sl = &l:statusline
  4870. " Color schemes for selected item {{{5
  4871. :hi User1 term=inverse,bold cterm=inverse,bold ctermfg=Yellow
  4872. \ guifg=Black guibg=Yellow
  4873. :hi User2 term=inverse,bold cterm=inverse,bold ctermfg=LightRed
  4874. \ guifg=Black guibg=LightRed
  4875. :hi User3 term=inverse,bold cterm=inverse,bold ctermfg=Red
  4876. \ guifg=Black guibg=Red
  4877. :hi User4 term=inverse,bold cterm=inverse,bold ctermfg=Cyan
  4878. \ guifg=Black guibg=Cyan
  4879. :hi User5 term=inverse,bold cterm=inverse,bold ctermfg=LightYellow
  4880. \ guifg=Black guibg=LightYellow
  4881. :hi User6 term=inverse,bold cterm=inverse,bold ctermfg=LightGray
  4882. \ guifg=DarkRed guibg=LightGray
  4883. " }}}5
  4884. " 3.2- Interactive loop {{{4
  4885. let help = "\r-- Keys available (".help_k.help.")"
  4886. " item selected at the start
  4887. let i = ('check' != a:box) ? default : 1
  4888. let direction = 0 | let toggle = 0
  4889. while 1
  4890. if 'combo' == a:box
  4891. let choice_{i} = substitute(choice_{i}, '^( )', '(*)', '')
  4892. endif
  4893. " Colored statusline
  4894. " Note: unfortunately the 'statusline' is a global option, {{{
  4895. " not a local one. I the hope that may change, as it does not provokes any
  4896. " error, I use '&l:statusline'. }}}
  4897. exe 'let &l:statusline=s:status_line(i, type,'. list_choices .')'
  4898. if has(':redrawstatus')
  4899. redrawstatus!
  4900. else
  4901. redraw!
  4902. endif
  4903. " Echo the current selection
  4904. echo "\r". a:text.' '.substitute(choice_{i}, '&', '', '')
  4905. " Wait the user to hit a key
  4906. let key=getchar()
  4907. let complType=nr2char(key)
  4908. " If the key hit matched awaited keys ...
  4909. if -1 != stridx(" \<tab>\<esc>\<enter>".hotkeys,complType) ||
  4910. \ (key =~ "\<F1>\\|\<right>\\|\<left>\\|\<s-tab>")
  4911. if key == "\<F1>" " Help {{{5
  4912. redraw!
  4913. echohl StatusLineNC
  4914. echo help
  4915. echohl None
  4916. let key=getchar()
  4917. let complType=nr2char(key)
  4918. endif
  4919. " TODO: support CTRL-D
  4920. if complType == "\<enter>" " Validate {{{5
  4921. break
  4922. elseif complType == " " " check box {{{5
  4923. let toggle = 1
  4924. elseif complType == "\<esc>" " Abort {{{5
  4925. let i = -1 | break
  4926. elseif complType == "\<tab>" || key == "\<right>" " Next {{{5
  4927. let direction = 1
  4928. elseif key =~ "\<left>\\|\<s-tab>" " Previous {{{5
  4929. let direction = -1
  4930. elseif -1 != stridx(hotkeys, complType ) " Hotkeys {{{5
  4931. if '' == complType | continue | endif
  4932. let direction = hotkey_{toupper(complType)} - i
  4933. let toggle = 1
  4934. " else
  4935. endif
  4936. " }}}5
  4937. endif
  4938. if direction != 0 " {{{5
  4939. if 'combo' == a:box
  4940. let choice_{i} = substitute(choice_{i}, '^(\*)', '( )', '')
  4941. endif
  4942. let i += direction
  4943. if i > nb_choices | let i = 1
  4944. elseif i == 0 | let i = nb_choices
  4945. endif
  4946. let direction = 0
  4947. endif
  4948. if toggle == 1 " {{{5
  4949. if 'check' == a:box
  4950. let choice_{i} = ((choice_{i}[1] == ' ')? '[X]' : '[ ]')
  4951. \ . strpart(choice_{i}, 3)
  4952. endif
  4953. let toggle = 0
  4954. endif
  4955. endwhile " }}}4
  4956. " 4- Terminate {{{3
  4957. " Clear screen
  4958. redraw!
  4959. " Restore statusline
  4960. let &l:statusline=sl
  4961. " Return
  4962. if (i == -1) || ('check' != a:box)
  4963. return i
  4964. else
  4965. let r = '' | let i = 1
  4966. while i <= nb_choices
  4967. let r .= ((choice_{i}[1] == 'X') ? '1' : '0')
  4968. let i += 1
  4969. endwhile
  4970. return r
  4971. endif
  4972. endfunction
  4973. " }}}1
  4974. "------------------------------------------------------------------------
  4975. " Functions that insert fte statements {{{1
  4976. " Function: s:if_fte(var, then, else) {{{2
  4977. " Function: s:confirm_fte(text, [, choices [, default [, type]]]) {{{2
  4978. " Function: s:input_fte(prompt [, default]) {{{2
  4979. " Function: s:combo_fte(prompt, choice [, ...]) {{{2
  4980. " Function: s:check_fte(prompt, choice [, ...]) {{{2
  4981. " }}}1
  4982. "------------------------------------------------------------------------
  4983. let &cpo=s:cpo_save
  4984. "=============================================================================
  4985. " vim600: set fdm=marker:
  4986. plugin/words_tools.vim [[[1
  4987. 105
  4988. " File: plugin/words_tools.vim
  4989. " Author: Luc Hermitte <hermitte {at} free {dot} fr>
  4990. " <URL:http://code.google.com/p/lh-vim/>
  4991. " License: GPLv3 with exceptions
  4992. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  4993. " Version: 3.0.0
  4994. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (19th Mar 2012)
  4995. " Purpose: Define functions better than expand("<cword>")
  4996. "
  4997. " Note: They are expected to be used in insert mode (thanks to <c-r>
  4998. " or <c-o>)
  4999. "
  5000. "===========================================================================
  5001. " Return the current keyword, uses spaces to delimitate {{{1
  5002. function! GetNearestKeyword()
  5003. let c = col ('.')-1
  5004. let ll = getline('.')
  5005. let ll1 = strpart(ll,0,c)
  5006. let ll1 = matchstr(ll1,'\k*$')
  5007. let ll2 = strpart(ll,c,strlen(ll)-c+1)
  5008. let ll2 = matchstr(ll2,'^\k*')
  5009. " let ll2 = strpart(ll2,0,match(ll2,'$\|\s'))
  5010. return ll1.ll2
  5011. endfunction
  5012. " Return the current word, uses spaces to delimitate {{{1
  5013. function! GetNearestWord()
  5014. let c = col ('.')-1
  5015. let l = line('.')
  5016. let ll = getline(l)
  5017. let ll1 = strpart(ll,0,c)
  5018. let ll1 = matchstr(ll1,'\S*$')
  5019. let ll2 = strpart(ll,c,strlen(ll)-c+1)
  5020. let ll2 = strpart(ll2,0,match(ll2,'$\|\s'))
  5021. ""echo ll1.ll2
  5022. return ll1.ll2
  5023. endfunction
  5024. " Return the word before the cursor, uses spaces to delimitate {{{1
  5025. " Rem : <cword> is the word under or after the cursor
  5026. function! GetCurrentWord()
  5027. let c = col ('.')-1
  5028. let l = line('.')
  5029. let ll = getline(l)
  5030. let ll1 = strpart(ll,0,c)
  5031. let ll1 = matchstr(ll1,'\S*$')
  5032. if strlen(ll1) == 0
  5033. return ll1
  5034. else
  5035. let ll2 = strpart(ll,c,strlen(ll)-c+1)
  5036. let ll2 = strpart(ll2,0,match(ll2,'$\|\s'))
  5037. return ll1.ll2
  5038. endif
  5039. endfunction
  5040. " Return the keyword before the cursor, uses \k to delimitate {{{1
  5041. " Rem : <cword> is the word under or after the cursor
  5042. function! GetCurrentKeyword()
  5043. let c = col ('.')-1
  5044. let l = line('.')
  5045. let ll = getline(l)
  5046. let ll1 = strpart(ll,0,c)
  5047. let ll1 = matchstr(ll1,'\k*$')
  5048. if strlen(ll1) == 0
  5049. return ll1
  5050. else
  5051. let ll2 = strpart(ll,c,strlen(ll)-c+1)
  5052. let ll2 = matchstr(ll2,'^\k*')
  5053. " let ll2 = strpart(ll2,0,match(ll2,'$\|\s'))
  5054. return ll1.ll2
  5055. endif
  5056. endfunction
  5057. " Extract the word before the cursor, {{{1
  5058. " use keyword definitions, skip latter spaces (see "bla word_accepted ")
  5059. function! GetPreviousWord()
  5060. let lig = getline(line('.'))
  5061. let lig = strpart(lig,0,col('.')-1)
  5062. return matchstr(lig, '\<\k*\>\s*$')
  5063. endfunction
  5064. " GetLikeCTRL_W() retrieves the characters that i_CTRL-W deletes. {{{1
  5065. " Initial need by Hari Krishna Dara <hari_vim@yahoo.com>
  5066. " Last ver:
  5067. " Pb: "if strlen(w) == " --> ") == " instead of just "== ".
  5068. " There still exists a bug regarding the last char of a line. VIM bug ?
  5069. function! GetLikeCTRL_W()
  5070. let lig = getline(line('.'))
  5071. let lig = strpart(lig,0,col('.')-1)
  5072. " treat ending spaces apart.
  5073. let s = matchstr(lig, '\s*$')
  5074. let lig = strpart(lig, 0, strlen(lig)-strlen(s))
  5075. " First case : last characters belong to a "word"
  5076. let w = matchstr(lig, '\<\k\+\>$')
  5077. if strlen(w) == 0
  5078. " otherwise, they belong to a "non word" (without any space)
  5079. let w = substitute(lig, '.*\(\k\|\s\)', '', 'g')
  5080. endif
  5081. return w . s
  5082. endfunction
  5083. " }}}1
  5084. "========================================================================
  5085. " vim60: set fdm=marker:
  5086. tests/lh/function.vim [[[1
  5087. 288
  5088. "=============================================================================
  5089. " $Id: function.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5090. " File: tests/lh/function.vim {{{1
  5091. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5092. " <URL:http://code.google.com/p/lh-vim/>
  5093. " License: GPLv3 with exceptions
  5094. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5095. " Version: 3.0.0
  5096. " Created: 03rd Nov 2008
  5097. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5098. "------------------------------------------------------------------------
  5099. " Description:
  5100. " Tests for autoload/lh/function.vim
  5101. "
  5102. "------------------------------------------------------------------------
  5103. " Installation: «install details»
  5104. " History: «history»
  5105. " TODO: «missing features»
  5106. " }}}1
  5107. "=============================================================================
  5108. UTSuite [lh-vim-lib] Testing lh#function plugin
  5109. runtime autoload/lh/function.vim
  5110. let s:cpo_save=&cpo
  5111. set cpo&vim
  5112. "------------------------------------------------------------------------
  5113. function! Test(...)
  5114. let nb = len(a:000)
  5115. " echo "test(".nb.':' .join(a:000, ' -- ')')'
  5116. let i =0
  5117. while i!= len(a:000)
  5118. echo "Test: type(".i.")=".type(a:000[i]).' --> '. string(a:000[i])
  5119. let i += 1
  5120. endwhile
  5121. endfunction
  5122. function! Print(...)
  5123. let res = lh#list#accumulate([1,2,'foo'], 'string', 'join(v:1_, " ## ")')
  5124. return res
  5125. endfunction
  5126. function! Id(...)
  5127. return copy(a:000)
  5128. endfunction
  5129. function! s:TestId()
  5130. let r = Id(1, 'string', [0], [[1]], {'ffo':42}, function('exists'), 1.2)
  5131. Assert! len(r) == 7
  5132. Assert! should#be#number (r[0])
  5133. Assert! should#be#string (r[1])
  5134. Assert! should#be#list (r[2])
  5135. Assert! should#be#list (r[3])
  5136. Assert! should#be#dict (r[4])
  5137. Assert! should#be#funcref(r[5])
  5138. Assert! should#be#float (r[6])
  5139. Assert r[0] == 1
  5140. Assert r[1] == 'string'
  5141. Assert r[2] == [0]
  5142. Assert r[3] == [[1]]
  5143. Assert r[4].ffo == 42
  5144. Assert r[5] == function('exists')
  5145. Assert r[6] == 1.2
  5146. endfunction
  5147. function! s:Test_bind()
  5148. " lh#function#bind + lh#function#execute
  5149. let rev4 = lh#function#bind(function('Id'), 'v:4_', 42, 'v:3_', 'v:2_', 'v:1_')
  5150. let r = lh#function#execute(rev4, 1,'two','three', [4,5])
  5151. Assert! len(r) == 5
  5152. Assert! should#be#list (r[0])
  5153. Assert! should#be#number (r[1])
  5154. Assert! should#be#string (r[2])
  5155. Assert! should#be#string (r[3])
  5156. Assert! should#be#number (r[4])
  5157. Assert r[0] == [4,5]
  5158. Assert r[1] == 42
  5159. Assert r[2] == 'three'
  5160. Assert r[3] == 'two'
  5161. Assert r[4] == 1
  5162. endfunction
  5163. function! s:Test_bind_compound_vars()
  5164. " lh#function#bind + lh#function#execute
  5165. let rev4 = lh#function#bind(function('Id'), 'v:4_', 'v:1_ . v:2_', 'v:3_', 'v:2_', 'v:1_')
  5166. let r = lh#function#execute(rev4, 1,'two','three', [4,5])
  5167. Assert! len(r) == 5
  5168. Assert! should#be#list (r[0])
  5169. Assert! should#be#string (r[1])
  5170. Assert! should#be#string (r[2])
  5171. Assert! should#be#string (r[3])
  5172. Assert! should#be#number (r[4])
  5173. Assert r[0] == [4,5]
  5174. Assert r[1] == '1two'
  5175. Assert r[2] == 'three'
  5176. Assert r[3] == 'two'
  5177. Assert r[4] == 1
  5178. endfunction
  5179. function! s:Test_execute_func_string_name()
  5180. " function name as string
  5181. let r = lh#function#execute('Id', 1,'two',3)
  5182. Assert! len(r) == 3
  5183. Assert! should#be#number (r[0])
  5184. Assert! should#be#string (r[1])
  5185. Assert! should#be#number (r[2])
  5186. Assert r[0] == 1
  5187. Assert r[1] == 'two'
  5188. Assert r[2] == 3
  5189. endfunction
  5190. function! s:Test_execute_string_expr()
  5191. " exp as binded-string
  5192. let r = lh#function#execute('Id(12,len(v:2_).v:2_, 42, v:3_, v:1_)', 1,'two',3)
  5193. Assert! len(r) == 5
  5194. Assert! should#be#number (r[0])
  5195. Assert! should#be#string (r[1])
  5196. Assert! should#be#number (r[2])
  5197. Assert! should#be#number (r[3])
  5198. Assert! should#be#number (r[4])
  5199. Assert r[0] == 12
  5200. Assert r[1] == len('two').'two'
  5201. Assert r[2] == 42
  5202. Assert r[3] == 3
  5203. Assert r[4] == 1
  5204. endfunction
  5205. function! s:Test_execute_func()
  5206. " calling a function() + bind
  5207. let r = lh#function#execute(function('Id'), 1,'two','v:1_',['a',42])
  5208. Assert! len(r) == 4
  5209. Assert! should#be#number (r[0])
  5210. Assert! should#be#string (r[1])
  5211. Assert! should#be#string (r[2])
  5212. Assert! should#be#list (r[3])
  5213. Assert r[0] == 1
  5214. Assert r[1] == 'two'
  5215. Assert r[2] == 'v:1_'
  5216. Assert r[3] == ['a', 42]
  5217. endfunction
  5218. "------------------------------------------------------------------------
  5219. function! s:Test_bind_func_string_name_AND_execute()
  5220. " function name as string
  5221. let rev3 = lh#function#bind('Id', 'v:3_', 12, 'v:2_', 'v:1_')
  5222. let r = lh#function#execute(rev3, 1,'two',3)
  5223. Assert! len(r) == 4
  5224. Assert! should#be#number (r[0])
  5225. Assert! should#be#number (r[1])
  5226. Assert! should#be#string (r[2])
  5227. Assert! should#be#number (r[3])
  5228. Assert r[0] == 3
  5229. Assert r[1] == 12
  5230. Assert r[2] == 'two'
  5231. Assert r[3] == 1
  5232. endfunction
  5233. function! s:Test_bind_string_expr_AND_execute()
  5234. " expressions as string
  5235. let rev3 = lh#function#bind('Id(12,len(v:2_).v:2_, 42, v:3_, v:1_)')
  5236. let r = lh#function#execute(rev3, 1,'two',3)
  5237. Assert! len(r) == 5
  5238. Assert! should#be#number (r[0])
  5239. Assert! should#be#string (r[1])
  5240. Assert! should#be#number (r[2])
  5241. Assert! should#be#number (r[3])
  5242. Assert! should#be#number (r[4])
  5243. Assert r[0] == 12
  5244. Assert r[1] == len('two').'two'
  5245. Assert r[2] == 42
  5246. Assert r[3] == 3
  5247. Assert r[4] == 1
  5248. endfunction
  5249. function! s:Test_double_bind_func_name()
  5250. let f1 = lh#function#bind('Id', 1, 2, 'v:1_', 4, 'v:2_')
  5251. " Comment "f1=".string(f1)
  5252. let r = lh#function#execute(f1, 3, 5)
  5253. Assert! len(r) == 5
  5254. let i = 0
  5255. while i != len(r)
  5256. Assert! should#be#number (r[i])
  5257. Assert r[i] == i+1
  5258. let i += 1
  5259. endwhile
  5260. " f2
  5261. let f2 = lh#function#bind(f1, 'v:1_', 5)
  5262. " Comment "f2=f1(v:1_, 5)=".string(f2)
  5263. let r = lh#function#execute(f2, 3)
  5264. Assert! len(r) == 5
  5265. let i = 0
  5266. while i != len(r)
  5267. Assert! should#be#number (r[i])
  5268. " echo "?? ".(r[i])."==".(i+1)
  5269. Assert r[i] == i+1
  5270. let i += 1
  5271. endwhile
  5272. endfunction
  5273. function! s:Test_double_bind_func()
  5274. let f1 = lh#function#bind(function('Id'), 1, 2, 'v:1_', 4, 'v:2_')
  5275. " Comment "f1=".string(f1)
  5276. let r = lh#function#execute(f1, 3, 5)
  5277. Assert! len(r) == 5
  5278. let i = 0
  5279. while i != len(r)
  5280. Assert! should#be#number (r[i])
  5281. Assert r[i] == i+1
  5282. let i += 1
  5283. endwhile
  5284. " f2
  5285. let f2 = lh#function#bind(f1, 'v:1_', 5)
  5286. " Comment "f2=f1(v:1_, 5)=".string(f2)
  5287. let r = lh#function#execute(f2, 3)
  5288. Assert! len(r) == 5
  5289. let i = 0
  5290. while i != len(r)
  5291. Assert! should#be#number (r[i])
  5292. Assert r[i] == i+1
  5293. let i += 1
  5294. endwhile
  5295. endfunction
  5296. function! s:Test_double_bind_func_cplx()
  5297. let g:bar = "bar"
  5298. let f1 = lh#function#bind(function('Id'), 1, 2, 'v:1_', 4, 'v:2_', 'v:3_', 'v:4_', 'v:5_', 'v:6_', 'v:7_')
  5299. " Comment "2bcpl# f1=".string(f1)
  5300. let f2 = lh#function#bind(f1, 'len(g:bar.v:1_)+v:1_', [1,2], '[v:1_, v:2_]', 4,5,6,7)
  5301. " let f2 = lh#function#bind(f1, 'v:1_', 5, 'foo', g:bar, 'len(g:bar.v:1_)+v:1_', [1,2], '[v:1_, v:2_]')
  5302. " Comment "2bcpl# f2=f1(v:1_, 5)=".string(f2)
  5303. let r = lh#function#execute(f2, 42, "foo")
  5304. Assert! 0 && "not ready"
  5305. Comment "2bcpl# ".string(r)
  5306. endfunction
  5307. function! s:Test_double_bind_expr()
  5308. let f1 = lh#function#bind('Id(1, 2, v:1_, v:3_, v:2_)')
  5309. Comment "2be# f1=".string(f1)
  5310. let r = lh#function#execute(f1, 3, 5, 4)
  5311. Comment "2be# ".string(r)
  5312. Assert! len(r) == 5
  5313. let i = 0
  5314. while i != len(r)
  5315. Assert! should#be#number (r[i])
  5316. Assert r[i] == i+1
  5317. let i += 1
  5318. endwhile
  5319. " f2
  5320. let f2 = lh#function#bind(f1, 'v:1_', '"foo"', [])
  5321. Comment "2be# f2=f1(v:1_, 5)=".string(f2)
  5322. let r = lh#function#execute(f2, 3)
  5323. Comment "2be# ".string(r)
  5324. Assert! len(r) == 5
  5325. let i = 0
  5326. while i != len(r)-2
  5327. Assert! should#be#number (r[i])
  5328. Assert r[i] == i+1
  5329. let i += 1
  5330. endwhile
  5331. Assert! should#be#list (r[-2])
  5332. Assert r[-2] == []
  5333. Assert! should#be#string (r[-1])
  5334. Assert r[-1] == 'foo'
  5335. endfunction
  5336. "todo: write double-binded tests for all kind of binded parameters:
  5337. " 'len(g:bar)'
  5338. " 42
  5339. " []
  5340. " v:1_ + len(v:2_.v:3_)
  5341. " '"foo"'
  5342. " v:1_
  5343. "------------------------------------------------------------------------
  5344. let &cpo=s:cpo_save
  5345. "=============================================================================
  5346. " vim600: set fdm=marker:
  5347. tests/lh/list.vim [[[1
  5348. 167
  5349. "=============================================================================
  5350. " $Id: list.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5351. " File: tests/lh/list.vim {{{1
  5352. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5353. " <URL:http://code.google.com/p/lh-vim/>
  5354. " License: GPLv3 with exceptions
  5355. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5356. " Version: 3.0.0
  5357. " Created: 19th Nov 2008
  5358. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5359. "------------------------------------------------------------------------
  5360. " Description:
  5361. " Tests for autoload/lh/list.vim
  5362. "
  5363. "------------------------------------------------------------------------
  5364. " Installation: «install details»
  5365. " History: «history»
  5366. " TODO: «missing features»
  5367. " }}}1
  5368. "=============================================================================
  5369. UTSuite [lh-vim-lib] Testing lh#list functions
  5370. runtime autoload/lh/function.vim
  5371. runtime autoload/lh/list.vim
  5372. let s:cpo_save=&cpo
  5373. set cpo&vim
  5374. "------------------------------------------------------------------------
  5375. " Find_if
  5376. function! s:Test_Find_If_string_predicate()
  5377. :let b = { 'min': 12, 'max': 42 }
  5378. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  5379. :let i = lh#list#Find_if(l, 'v:val>v:1_.min && v:val<v:1_.max && v:val%v:2_==0', [b, 2] )
  5380. " echo i . '/' . len(l)
  5381. Assert i == 5
  5382. Assert l[i] == 28
  5383. " :echo l[i]
  5384. endfunction
  5385. function! s:Test_Find_If_functor_predicate()
  5386. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  5387. :let i = lh#list#find_if(l, 'v:1_>12 && v:1_<42 && v:1_%2==0')
  5388. " echo i . '/' . len(l)
  5389. Assert i == 5
  5390. Assert l[i] == 28
  5391. " :echo l[i]
  5392. endfunction
  5393. function! s:Test_find_if_double_bind()
  5394. :let b = { 'min': 12, 'max': 42 }
  5395. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  5396. :let f = lh#function#bind( 'v:3_>v:1_.min && v:3_<v:1_.max && v:3_%v:2_==0')
  5397. :let p = lh#function#bind(f, b,2,'v:1_')
  5398. :let i = lh#list#find_if(l, p)
  5399. :echo l[i]
  5400. endfunction
  5401. " double bind is not yet operational
  5402. UTIgnore Test_find_if_double_bind
  5403. "------------------------------------------------------------------------
  5404. " Unique Sorting
  5405. function! CmpNumbers(lhs, rhs)
  5406. if a:lhs < a:rhs | return -1
  5407. elseif a:lhs == a:rhs | return 0
  5408. else | return +1
  5409. endif
  5410. endfunction
  5411. function! s:Test_sort()
  5412. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  5413. :let expected = [ 1, 5, 6, 25, 28, 48]
  5414. :let s = lh#list#unique_sort(l, "CmpNumbers")
  5415. " Comment string(s)
  5416. Assert s == expected
  5417. endfunction
  5418. function! s:Test_sort2()
  5419. :let l = [ 1, 5, 48, 25, 5, 28, 6]
  5420. :let expected = [ 1, 5, 6, 25, 28, 48]
  5421. :let s = lh#list#unique_sort2(l, "CmpNumbers")
  5422. " Comment string(s)
  5423. Assert s == expected
  5424. endfunction
  5425. "------------------------------------------------------------------------
  5426. " Searchs
  5427. function! s:TestBinarySearches()
  5428. let v1 = [ -3, -2, -1, -1, 0, 0, 1, 2, 3, 4, 6 ]
  5429. let i = lh#list#lower_bound(v1, 3)
  5430. Assert v1[i] == 3
  5431. let i = lh#list#upper_bound(v1, 3)
  5432. Assert v1[i] == 4
  5433. let r = lh#list#equal_range(v1, 3)
  5434. Assert v1[r[0]:r[1]-1] == [3]
  5435. let i = lh#list#lower_bound(v1, -1)
  5436. Assert v1[i] == -1
  5437. let i = lh#list#upper_bound(v1, -1)
  5438. Assert v1[i] == 0
  5439. let r = lh#list#equal_range(v1, -1)
  5440. Assert v1[r[0]:r[1]-1] == [-1, -1]
  5441. let i = lh#list#lower_bound(v1, 5)
  5442. Assert v1[i] == 6
  5443. let i = lh#list#upper_bound(v1, 5)
  5444. Assert v1[i] == 6
  5445. let r = lh#list#equal_range(v1, 5)
  5446. Assert v1[r[0]:r[1]-1] == []
  5447. Assert len(v1) == lh#list#lower_bound(v1, 10)
  5448. Assert len(v1) == lh#list#upper_bound(v1, 10)
  5449. Assert [len(v1), len(v1)] == lh#list#equal_range(v1, 10)
  5450. endfunction
  5451. "------------------------------------------------------------------------
  5452. " accumulate
  5453. function! s:Test_accumulate_len_strings()
  5454. let strings = [ 'foo', 'bar', 'toto' ]
  5455. let len = eval(lh#list#accumulate(strings, 'strlen', 'join(v:1_, "+")'))
  5456. Assert len == 3+3+4
  5457. endfunction
  5458. function! s:Test_accumulate_join()
  5459. let ll = [ 1, 2, 'foo', ['bar'] ]
  5460. let res = lh#list#accumulate(ll, 'string', 'join(v:1_, " ## ")')
  5461. Assert res == "1 ## 2 ## 'foo' ## ['bar']"
  5462. " This test will fail because it seems :for each loop cannot iterate on
  5463. " heterogeneous containers
  5464. endfunction
  5465. "------------------------------------------------------------------------
  5466. " Copy_if
  5467. function! s:Test_copy_if()
  5468. :let l = [ 1, 25, 5, 48, 25, 5, 28, 6]
  5469. :let expected = [ 25, 48, 25, 28, 6]
  5470. :let s = lh#list#copy_if(l, [], "v:1_ > 5")
  5471. " Comment string(s)
  5472. Assert s == expected
  5473. endfunction
  5474. "------------------------------------------------------------------------
  5475. " subset
  5476. function! s:Test_subset()
  5477. :let l = [ 1, 25, 5, 48, 25, 5, 28, 6]
  5478. :let indices = [ 0, 5, 7, 3 ]
  5479. :let expected = [ 1, 5, 6, 48 ]
  5480. :let s = lh#list#subset(l, indices)
  5481. " Comment string(s)
  5482. Assert s == expected
  5483. endfunction
  5484. "------------------------------------------------------------------------
  5485. " intersect
  5486. function! s:Test_intersect()
  5487. :let l1 = [ 1, 25, 7, 48, 26, 5, 28, 6]
  5488. :let l2 = [ 3, 8, 7, 25, 6 ]
  5489. :let expected = [ 25, 7, 6 ]
  5490. :let s = lh#list#intersect(l1, l2)
  5491. " Comment string(s)
  5492. Assert s == expected
  5493. endfunction
  5494. "------------------------------------------------------------------------
  5495. let &cpo=s:cpo_save
  5496. "=============================================================================
  5497. " vim600: set fdm=marker:
  5498. tests/lh/path.vim [[[1
  5499. 180
  5500. "=============================================================================
  5501. " $Id: path.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5502. " File: tests/lh/path.vim {{{1
  5503. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5504. " <URL:http://code.google.com/p/lh-vim/>
  5505. " License: GPLv3 with exceptions
  5506. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5507. " Version: 3.0.0
  5508. " Created: 28th May 2009
  5509. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5510. "------------------------------------------------------------------------
  5511. " Description:
  5512. " Tests for autoload/lh/path.vim
  5513. "
  5514. "------------------------------------------------------------------------
  5515. " Installation: «install details»
  5516. " History: «history»
  5517. " TODO: «missing features»
  5518. " }}}1
  5519. "=============================================================================
  5520. UTSuite [lh-vim-lib] Testing lh#path functions
  5521. runtime autoload/lh/path.vim
  5522. let s:cpo_save=&cpo
  5523. set cpo&vim
  5524. "------------------------------------------------------------------------
  5525. function! s:Test_simplify()
  5526. Assert lh#path#simplify('a/b/c') == 'a/b/c'
  5527. Assert lh#path#simplify('a/b/./c') == 'a/b/c'
  5528. Assert lh#path#simplify('./a/b/./c') == 'a/b/c'
  5529. Assert lh#path#simplify('./a/../b/./c') == 'b/c'
  5530. Assert lh#path#simplify('../a/../b/./c') == '../b/c'
  5531. Assert lh#path#simplify('a\b\c') == 'a\b\c'
  5532. Assert lh#path#simplify('a\b\.\c') == 'a\b\c'
  5533. Assert lh#path#simplify('.\a\b\.\c') == 'a\b\c'
  5534. if exists('+shellslash')
  5535. Assert lh#path#simplify('.\a\..\b\.\c') == 'b\c'
  5536. Assert lh#path#simplify('..\a\..\b\.\c') == '..\b\c'
  5537. endif
  5538. endfunction
  5539. function! s:Test_strip_common()
  5540. let paths = ['foo/bar/file', 'foo/file', 'foo/foo/file']
  5541. let expected = [ 'bar/file', 'file', 'foo/file']
  5542. Assert lh#path#strip_common(paths) == expected
  5543. endfunction
  5544. function! s:Test_common()
  5545. " Pick one ...
  5546. Assert 'foo/' == lh#path#common(['foo/bar/dir', 'foo'])
  5547. Assert 'foo/bar/' == lh#path#common(['foo/bar/dir', 'foo/bar'])
  5548. Assert 'foo/' == lh#path#common(['foo/bar/dir', 'foo/bar2'])
  5549. Assert 'foo' == lh#path#common(['foo/bar/dir', 'foo'])
  5550. Assert 'foo/bar' == lh#path#common(['foo/bar/dir', 'foo/bar'])
  5551. Assert 'foo' == lh#path#common(['foo/bar/dir', 'foo/bar2'])
  5552. endfunction
  5553. function! s:Test_strip_start()
  5554. let expected = 'template/bar.template'
  5555. Assert lh#path#strip_start($HOME.'/.vim/template/bar.template',
  5556. \ [ $HOME.'/.vim', $HOME.'/vimfiles', '/usr/local/share/vim' ])
  5557. \ == expected
  5558. Assert lh#path#strip_start($HOME.'/vimfiles/template/bar.template',
  5559. \ [ $HOME.'/.vim', $HOME.'/vimfiles', '/usr/local/share/vim' ])
  5560. \ == expected
  5561. Assert lh#path#strip_start('/usr/local/share/vim/template/bar.template',
  5562. \ [ $HOME.'/.vim', $HOME.'/vimfiles', '/usr/local/share/vim' ])
  5563. \ == expected
  5564. endfunction
  5565. function! s:Test_IsAbsolutePath()
  5566. " nix paths
  5567. Assert lh#path#is_absolute_path('/usr/local')
  5568. Assert lh#path#is_absolute_path($HOME)
  5569. Assert ! lh#path#is_absolute_path('./usr/local')
  5570. Assert ! lh#path#is_absolute_path('.usr/local')
  5571. " windows paths
  5572. Assert lh#path#is_absolute_path('e:\usr\local')
  5573. Assert ! lh#path#is_absolute_path('.\usr\local')
  5574. Assert ! lh#path#is_absolute_path('.usr\local')
  5575. " UNC paths
  5576. Assert lh#path#is_absolute_path('\\usr\local')
  5577. Assert lh#path#is_absolute_path('//usr/local')
  5578. endfunction
  5579. function! s:Test_IsURL()
  5580. " nix paths
  5581. Assert ! lh#path#is_url('/usr/local')
  5582. Assert ! lh#path#is_url($HOME)
  5583. Assert ! lh#path#is_url('./usr/local')
  5584. Assert ! lh#path#is_url('.usr/local')
  5585. " windows paths
  5586. Assert ! lh#path#is_url('e:\usr\local')
  5587. Assert ! lh#path#is_url('.\usr\local')
  5588. Assert ! lh#path#is_url('.usr\local')
  5589. " UNC paths
  5590. Assert ! lh#path#is_url('\\usr\local')
  5591. Assert ! lh#path#is_url('//usr/local')
  5592. " URLs
  5593. Assert lh#path#is_url('http://www.usr/local')
  5594. Assert lh#path#is_url('https://www.usr/local')
  5595. Assert lh#path#is_url('ftp://www.usr/local')
  5596. Assert lh#path#is_url('sftp://www.usr/local')
  5597. Assert lh#path#is_url('dav://www.usr/local')
  5598. Assert lh#path#is_url('fetch://www.usr/local')
  5599. Assert lh#path#is_url('file://www.usr/local')
  5600. Assert lh#path#is_url('rcp://www.usr/local')
  5601. Assert lh#path#is_url('rsynch://www.usr/local')
  5602. Assert lh#path#is_url('scp://www.usr/local')
  5603. endfunction
  5604. function! s:Test_ToRelative()
  5605. let pwd = getcwd()
  5606. Assert lh#path#to_relative(pwd.'/foo/bar') == 'foo/bar'
  5607. Assert lh#path#to_relative(pwd.'/./foo') == 'foo'
  5608. Assert lh#path#to_relative(pwd.'/foo/../bar') == 'bar'
  5609. " Does not work yet as it returns an absolute path it that case
  5610. Assert lh#path#to_relative(pwd.'/../bar') == '../bar'
  5611. endfunction
  5612. function! s:Test_relative_path()
  5613. Assert lh#path#relative_to('foo/bar/dir', 'foo') == '../../'
  5614. Assert lh#path#relative_to('foo', 'foo/bar/dir') == 'bar/dir/'
  5615. Assert lh#path#relative_to('foo/bar', 'foo/bar2/dir') == '../bar2/dir/'
  5616. let pwd = getcwd()
  5617. Assert lh#path#relative_to(pwd ,pwd.'/../bar') == '../bar/'
  5618. endfunction
  5619. function! s:Test_search_vimfiles()
  5620. let expected_win = $HOME . '/vimfiles'
  5621. let expected_nix = $HOME . '/.vim'
  5622. let what = lh#path#to_regex($HOME.'/').'\(vimfiles\|.vim\)'
  5623. " Comment what
  5624. let z = lh#path#find(&rtp,what)
  5625. if has('win16')||has('win32')||has('win64')
  5626. Assert z == expected_win
  5627. else
  5628. Assert z == expected_nix
  5629. endif
  5630. endfunction
  5631. function! s:Test_path_depth()
  5632. Assert 0 == lh#path#depth('.')
  5633. Assert 0 == lh#path#depth('./')
  5634. Assert 0 == lh#path#depth('.\')
  5635. Assert 1 == lh#path#depth('toto')
  5636. Assert 1 == lh#path#depth('toto/')
  5637. Assert 1 == lh#path#depth('toto\')
  5638. Assert 1 == lh#path#depth('toto/.')
  5639. Assert 1 == lh#path#depth('toto\.')
  5640. Assert 1 == lh#path#depth('toto/./.')
  5641. Assert 1 == lh#path#depth('toto\.\.')
  5642. Assert 0 == lh#path#depth('toto/..')
  5643. if exists('+shellslash')
  5644. Assert 0 == lh#path#depth('toto\..')
  5645. endif
  5646. Assert 2 == lh#path#depth('toto/titi/')
  5647. Assert 2 == lh#path#depth('toto\titi\')
  5648. Assert 2 == lh#path#depth('/toto/titi/')
  5649. Assert 2 == lh#path#depth('c:/toto/titi/')
  5650. Assert 2 == lh#path#depth('c:\toto/titi/')
  5651. " todo: make a choice about "negative" paths like "../../foo"
  5652. Assert -1 == lh#path#depth('../../foo')
  5653. endfunction
  5654. "------------------------------------------------------------------------
  5655. let &cpo=s:cpo_save
  5656. "=============================================================================
  5657. " vim600: set fdm=marker:
  5658. tests/lh/test-Fargs2String.vim [[[1
  5659. 85
  5660. "=============================================================================
  5661. " $Id: test-Fargs2String.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5662. " File: tests/lh/test-Fargs2String.vim {{{1
  5663. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5664. " <URL:http://code.google.com/p/lh-vim/>
  5665. " License: GPLv3 with exceptions
  5666. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5667. " Version: 3.0.0
  5668. " Created: 16th Apr 2007
  5669. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5670. "------------------------------------------------------------------------
  5671. " Description: Tests for lh-vim-lib . lh#command#Fargs2String
  5672. "
  5673. "------------------------------------------------------------------------
  5674. " Installation:
  5675. " Relies on the version «patched by myself|1?» of vim_units
  5676. " History: «history»
  5677. " TODO: «missing features»
  5678. " }}}1
  5679. "=============================================================================
  5680. function! s:TestEmpty()
  5681. let empty = []
  5682. let res = lh#command#Fargs2String(empty)
  5683. call VUAssertEquals(len(empty), 0, 'Expected empty', 22)
  5684. call VUAssertEquals(res, '', 'Expected empty result', 23)
  5685. endfunction
  5686. function! s:TestSimpleText1()
  5687. let expected = 'text'
  5688. let one = [ expected ]
  5689. let res = lh#command#Fargs2String(one)
  5690. call VUAssertEquals(len(one), 0, 'Expected empty', 27)
  5691. call VUAssertEquals(res, expected, 'Expected a simple result', 28)
  5692. endfunction
  5693. function! s:TestSimpleTextN()
  5694. let expected = 'text'
  5695. let list = [ expected , 'stuff1', 'stuff2']
  5696. let res = lh#command#Fargs2String(list)
  5697. call VUAssertEquals(len(list), 2, 'Expected not empty', 38)
  5698. call VUAssertEquals(res, expected, 'Expected a simple result', 39)
  5699. endfunction
  5700. function! s:TestComposedN()
  5701. let expected = '"a several tokens string"'
  5702. let list = [ '"a', 'several', 'tokens', 'string"', 'stuff1', 'stuff2']
  5703. let res = lh#command#Fargs2String(list)
  5704. call VUAssertEquals(len(list), 2, 'Expected not empty', 46)
  5705. call VUAssertEquals(res, expected, 'Expected a composed string', 47)
  5706. call VUAssertEquals(list, ['stuff1', 'stuff2'], 'Expected a list', 48)
  5707. call VUAssertNotSame(list, ['stuff1', 'stuff2'], 'Expected different lists', 49)
  5708. endfunction
  5709. function! s:TestComposed1()
  5710. let expected = '"string"'
  5711. let list = [ '"string"', 'stuff1', 'stuff2']
  5712. let res = lh#command#Fargs2String(list)
  5713. call VUAssertEquals(len(list), 2, 'Expected not empty', 56)
  5714. call VUAssertEquals(res, expected, 'Expected a string', 57)
  5715. call VUAssertEquals(list, ['stuff1', 'stuff2'], 'Expected a list', 58)
  5716. call VUAssertNotSame(list, ['stuff1', 'stuff2'], 'Expected different lists', 59)
  5717. endfunction
  5718. function! s:TestInvalidString()
  5719. let expected = '"a string'
  5720. let list = [ '"a', 'string']
  5721. let res = lh#command#Fargs2String(list)
  5722. call VUAssertEquals(len(list), 0, 'Expected empty', 66)
  5723. call VUAssertEquals(res, expected, 'Expected an invalid string', 67)
  5724. endfunction
  5725. function! AllTests()
  5726. call s:TestEmpty()
  5727. call s:TestSimpleText1()
  5728. call s:TestSimpleTextN()
  5729. call s:TestComposed1()
  5730. call s:TestComposedN()
  5731. endfunction
  5732. " call VURunnerRunTest('AllTests')
  5733. VURun % AllTests
  5734. "=============================================================================
  5735. " vim600: set fdm=marker:
  5736. tests/lh/test-askmenu.vim [[[1
  5737. 67
  5738. "=============================================================================
  5739. " $Id: test-askmenu.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5740. " File: tests/test-buffer-menu.vim {{{1
  5741. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5742. " <URL:http://code.google.com/p/lh-vim/>
  5743. " License: GPLv3 with exceptions
  5744. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5745. " Version: 3.0.0
  5746. " Created: 18th Apr 2007
  5747. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5748. "------------------------------------------------------------------------
  5749. " Description:
  5750. " Test units for buffermenu.vim
  5751. "
  5752. "------------------------------------------------------------------------
  5753. " Installation: Requires:
  5754. " (*) Vim 7.0+
  5755. " (*) vim_units.vim v0.2/1.0?
  5756. " Vimscript # «???»
  5757. " (*) lh-vim-lib (lh#ask#menu)
  5758. "
  5759. " User Manual:
  5760. " Source this file.
  5761. "
  5762. " History:
  5763. " (*) 17th Apr 2007: First version
  5764. " TODO: «missing features»
  5765. " }}}1
  5766. "=============================================================================
  5767. "=============================================================================
  5768. let s:cpo_save=&cpo
  5769. "------------------------------------------------------------------------
  5770. " Functions {{{1
  5771. function! TestAskMenu()
  5772. imenu 42.40.10 &LH-Tests.&Menu.&ask.i iask
  5773. inoremenu 42.40.10 &LH-Tests.&Menu.&ask.inore inoreask
  5774. nmenu 42.40.10 &LH-Tests.&Menu.&ask.n nask
  5775. nnoremenu 42.40.10 &LH-Tests.&Menu.&ask.nnore nnoreask
  5776. nmenu <script> 42.40.10 &LH-Tests.&Menu.&ask.nscript nscriptask
  5777. nnoremenu <script> 42.40.10 &LH-Tests.&Menu.&ask.nnnscript nnscriptask
  5778. vmenu 42.40.10 &LH-Tests.&Menu.&ask.v vask
  5779. vnoremenu 42.40.10 &LH-Tests.&Menu.&ask.vnore vnoreask
  5780. call s:CheckInMode('i', 'i')
  5781. endfunction
  5782. function! s:CheckInMode(mode, name)
  5783. let g:menu = lh#askvim#menu('LH-Tests.Menu.ask.'.a:name, a:mode)
  5784. let g:name = a:name
  5785. " VUAssert 55 Equals g:menu.name g:name "Name mismatch"
  5786. " VUAssert 56 Equals g:menu.priority '42.40.10' "Priority mismatch"
  5787. " VUAssert 57 Fail "parce qu'il le faut bien"
  5788. echomsg "name= ".g:menu.name
  5789. echomsg "prio= ".g:menu.priority
  5790. endfunction
  5791. " Functions }}}1
  5792. "------------------------------------------------------------------------
  5793. let &cpo=s:cpo_save
  5794. "=============================================================================
  5795. " vim600: set fdm=marker:
  5796. tests/lh/test-command.vim [[[1
  5797. 69
  5798. " $Id: test-command.vim 156 2010-05-07 00:54:36Z luc.hermitte $
  5799. " Tests for lh-vim-lib . lh#command
  5800. " FindFilter(filter): Helper {{{3
  5801. function! s:FindFilter(filter)
  5802. let filter = a:filter . '.vim'
  5803. let result =globpath(&rtp, "compiler/BTW-".filter) . "\n" .
  5804. \ globpath(&rtp, "compiler/BTW_".filter). "\n" .
  5805. \ globpath(&rtp, "compiler/BTW/".filter)
  5806. let result = substitute(result, '\n\n', '\n', 'g')
  5807. let result = substitute(result, '^\n', '', 'g')
  5808. return result
  5809. endfunction
  5810. function! s:ComplFilter(filter)
  5811. let files = s:FindFilter('*')
  5812. let files = substitute(files,
  5813. \ '\(^\|\n\).\{-}compiler[\\/]BTW[-_\\/]\(.\{-}\)\.vim\>\ze\%(\n\|$\)',
  5814. \ '\1\2', 'g')
  5815. return files
  5816. endfunction
  5817. function! s:Add()
  5818. endfunction
  5819. let s:v1 = 'v1'
  5820. let s:v2 = 2
  5821. function! s:Foo(i)
  5822. return a:i*a:i
  5823. endfunction
  5824. function! s:echo(params)
  5825. echo s:{join(a:params, '')}
  5826. endfunction
  5827. function! Echo(params)
  5828. " echo "Echo(".string(a:params).')'
  5829. let expr = 's:'.join(a:params, '')
  5830. " echo expr
  5831. exe 'echo '.expr
  5832. endfunction
  5833. let TBTWcommand = {
  5834. \ "name" : "TBT",
  5835. \ "arg_type" : "sub_commands",
  5836. \ "arguments" :
  5837. \ [
  5838. \ { "name" : "echo",
  5839. \ "arg_type" : "function",
  5840. \ "arguments" : "v1,v2",
  5841. \ "action": function("\<sid>echo") },
  5842. \ { "name" : "Echo",
  5843. \ "arg_type" : "function",
  5844. \ "arguments" : "v1,v2",
  5845. \ "action": function("Echo") },
  5846. \ { "name" : "help" },
  5847. \ { "name" : "add",
  5848. \ "arguments": function("s:ComplFilter"),
  5849. \ "action" : function("s:Add") }
  5850. \ ]
  5851. \ }
  5852. call lh#command#new(TBTWcommand)
  5853. nnoremap µ :call lh#command#new(TBTWcommand)<cr>
  5854. "=============================================================================
  5855. " vim600: set fdm=marker:
  5856. tests/lh/test-menu-map.vim [[[1
  5857. 56
  5858. "=============================================================================
  5859. " $Id: test-menu-map.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5860. " File: tests/lh/test-menu-map.vim {{{1
  5861. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5862. " <URL:http://code.google.com/p/lh-vim/>
  5863. " License: GPLv3 with exceptions
  5864. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5865. " Version: 3.0.0
  5866. " Created: 05th Dec 2006
  5867. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5868. "------------------------------------------------------------------------
  5869. " Description: Tests for lh-vim-lib . lh#menu#
  5870. "
  5871. "------------------------------------------------------------------------
  5872. " Installation: «install details»
  5873. " History: «history»
  5874. " TODO: «missing features»
  5875. " }}}1
  5876. "=============================================================================
  5877. " let g:want_buffermenu_or_global_disable = 1
  5878. " let b:want_buffermenu_or_global_disable = 1
  5879. " echo lh#option#get("want_buffermenu_or_global_disable", 1, "bg")
  5880. " Call a command (':Command')
  5881. call lh#menu#make("nic", '42.50.340',
  5882. \ '&LH-Tests.&Menu-Make.Build Ta&gs', "<C-L>g",
  5883. \ '<buffer>',
  5884. \ ":echo 'TeXtags'<CR>")
  5885. " With '{' expanding to '{}××', or '{}' regarding the mode
  5886. call lh#menu#IVN_make('42.50.360.200',
  5887. \ '&LH-Tests.&Menu-Make.&Insert.\toto{}', ']toto',
  5888. \ '\\toto{',
  5889. \ '{%i\\toto<ESC>%l',
  5890. \ "viw]toto")
  5891. " Noremap for the visual maps
  5892. call lh#menu#IVN_make('42.50.360.200',
  5893. \ '&LH-Tests.&Menu-Make.&Insert.\titi{}', ']titi',
  5894. \ '\\titi{',
  5895. \ '<ESC>`>a}<ESC>`<i\\titi{<ESC>%l',
  5896. \ "viw]titi",
  5897. \ 0, 1, 0)
  5898. " Noremap for the insert and visual maps
  5899. call lh#menu#IVN_make('42.50.360.200',
  5900. \ '&LH-Tests.&Menu-Make.&Insert.<tata></tata>', ']tata',
  5901. \ '<tata></tata><esc>?<<CR>i',
  5902. \ '<ESC>`>a</tata><ESC>`<i<tata><ESC>/<\\/tata>/e1<CR>',
  5903. \ "viw]tata",
  5904. \ 1, 1, 0)
  5905. "=============================================================================
  5906. " vim600: set fdm=marker:
  5907. tests/lh/test-toggle-menu.vim [[[1
  5908. 86
  5909. "=============================================================================
  5910. " $Id: test-toggle-menu.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5911. " File: tests/lh/test-toggle-menu.vim {{{1
  5912. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5913. " <URL:http://code.google.com/p/lh-vim/>
  5914. " License: GPLv3 with exceptions
  5915. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5916. " Version: 3.0.0
  5917. " Created: 17th Apr 2007
  5918. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5919. "------------------------------------------------------------------------
  5920. " Description:
  5921. " Tests for lh-vim-lib . lh#menu#def_toggle_item()
  5922. "
  5923. "------------------------------------------------------------------------
  5924. " Installation: «install details»
  5925. " History: «history»
  5926. " TODO: «missing features»
  5927. " }}}1
  5928. "=============================================================================
  5929. runtime autoload/lh/menu.vim
  5930. let Data = {
  5931. \ "variable": "bar",
  5932. \ "idx_crt_value": 1,
  5933. \ "values": [ 'a', 'b', 'c', 'd' ],
  5934. \ "menu": { "priority": '42.50.10', "name": '&LH-Tests.&TogMenu.&bar'}
  5935. \}
  5936. call lh#menu#def_toggle_item(Data)
  5937. let Data2 = {
  5938. \ "variable": "foo",
  5939. \ "idx_crt_value": 3,
  5940. \ "texts": [ 'un', 'deux', 'trois', 'quatre' ],
  5941. \ "values": [ 1, 2, 3, 4 ],
  5942. \ "menu": { "priority": '42.50.11', "name": '&LH-Tests.&TogMenu.&foo'}
  5943. \}
  5944. call lh#menu#def_toggle_item(Data2)
  5945. " No default
  5946. let Data3 = {
  5947. \ "variable": "nodef",
  5948. \ "texts": [ 'one', 'two', 'three', 'four' ],
  5949. \ "values": [ 1, 2, 3, 4 ],
  5950. \ "menu": { "priority": '42.50.12', "name": '&LH-Tests.&TogMenu.&nodef'}
  5951. \}
  5952. call lh#menu#def_toggle_item(Data3)
  5953. " No default
  5954. let g:def = 2
  5955. let Data4 = {
  5956. \ "variable": "def",
  5957. \ "values": [ 1, 2, 3, 4 ],
  5958. \ "menu": { "priority": '42.50.13', "name": '&LH-Tests.&TogMenu.&def'}
  5959. \}
  5960. call lh#menu#def_toggle_item(Data4)
  5961. " What follows does not work because we can't build an exportable FuncRef on top
  5962. " of a script local function
  5963. " finish
  5964. function! s:getSNR()
  5965. if !exists("s:SNR")
  5966. let s:SNR=matchstr(expand("<sfile>"), "<SNR>\\d\\+_\\zegetSNR$")
  5967. endif
  5968. return s:SNR
  5969. endfunction
  5970. function! s:Yes()
  5971. echomsg "Yes"
  5972. endfunction
  5973. function! s:No()
  5974. echomsg "No"
  5975. endfunction
  5976. let Data4 = {
  5977. \ "variable": "yesno",
  5978. \ "values": [ 1, 2 ],
  5979. \ "text": [ "No", "Yes" ],
  5980. \ "actions": [ function(s:getSNR()."No"), function(s:getSNR()."Yes") ],
  5981. \ "menu": { "priority": '42.50.20', "name": '&LH-Tests.&TogMenu.&yesno'}
  5982. \}
  5983. call lh#menu#def_toggle_item(Data4)
  5984. tests/lh/topological-sort.vim [[[1
  5985. 122
  5986. "=============================================================================
  5987. " $Id: topological-sort.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  5988. " File: tests/lh/topological-sort.vim {{{1
  5989. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5990. " <URL:http://code.google.com/p/lh-vim/>
  5991. " License: GPLv3 with exceptions
  5992. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  5993. " Version: 3.0.0
  5994. " Created: 17th Apr 2008
  5995. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  5996. "------------------------------------------------------------------------
  5997. " Description: «description»
  5998. "
  5999. "------------------------------------------------------------------------
  6000. " Installation: «install details»
  6001. " History: «history»
  6002. " TODO: «missing features»
  6003. " }}}1
  6004. "=============================================================================
  6005. let s:cpo_save=&cpo
  6006. set cpo&vim
  6007. "------------------------------------------------------------------------
  6008. UTSuite [lh-vim-lib] topological sort
  6009. " Fully defineds DAGs {{{1
  6010. " A Direct Acyclic Graph {{{2
  6011. let s:dag1 = {}
  6012. let s:dag1[7] = [11, 8]
  6013. let s:dag1[5] = [11]
  6014. let s:dag1[3] = [8, 10]
  6015. let s:dag1[11] = [2, 9, 10]
  6016. let s:dag1[8] = [9]
  6017. " A Direct Cyclic Graph {{{2
  6018. let s:dcg1 = deepcopy(s:dag1)
  6019. let s:dcg1[9] = [11]
  6020. " Check routine: are the elements correctly sorted? {{{2
  6021. function! s:DoTestOrder(elements)
  6022. Assert! len(a:elements) == 8
  6023. Assert index(a:elements, 7) < index(a:elements, 11)
  6024. Assert index(a:elements, 7) < index(a:elements, 8)
  6025. Assert index(a:elements, 5) < index(a:elements, 11)
  6026. Assert index(a:elements, 3) < index(a:elements, 8)
  6027. Assert index(a:elements, 3) < index(a:elements, 10)
  6028. Assert index(a:elements, 11) < index(a:elements, 2)
  6029. Assert index(a:elements, 11) < index(a:elements, 9)
  6030. Assert index(a:elements, 11) < index(a:elements, 10)
  6031. Assert index(a:elements, 8) < index(a:elements, 9)
  6032. endfunction
  6033. " Test DAG1 {{{2
  6034. function! s:TestDAG_depth()
  6035. let res = lh#graph#tsort#depth(s:dag1, [3, 5,7])
  6036. call s:DoTestOrder(res)
  6037. echo "D(s:dag1)=".string(res)
  6038. endfunction
  6039. function! s:TestDAG_breadth()
  6040. let res = lh#graph#tsort#breadth(s:dag1, [3, 5,7])
  6041. call s:DoTestOrder(res)
  6042. echo "B(s:dag1)=".string(res)
  6043. endfunction
  6044. " Test DCG1 {{{2
  6045. function! s:TestDCG_depth()
  6046. let expr = 'lh#graph#tsort#depth('.string(s:dcg1).', [3, 5,7])'
  6047. Assert should#throw(expr, 'Tsort: cyclic graph detected')
  6048. endfunction
  6049. function! s:TestDCG_breadth()
  6050. let expr = 'lh#graph#tsort#breadth('.string(s:dcg1).', [3, 5,7])'
  6051. Assert should#throw(expr, 'Tsort: cyclic graph detected')
  6052. endfunction
  6053. " Lazzy Evaluated DAGs {{{1
  6054. " Emulated lazzyness {{{2
  6055. " The time-consumings evaluation function
  6056. let s:called = 0
  6057. function! Fetch(node)
  6058. let s:called += 1
  6059. return has_key(s:dag1, a:node) ? (s:dag1[a:node]) : []
  6060. endfunction
  6061. " Test Fetch on a DAG {{{2
  6062. function! s:TestDAG_fetch()
  6063. let s:called = 0
  6064. let res = lh#graph#tsort#depth(function('Fetch'), [3,5,7])
  6065. call s:DoTestOrder(res)
  6066. echo "D(fetch)=".string(res)
  6067. echo "Fetch has been evaluated ".s:called." times / ".len(res)
  6068. Assert s:called == len(res)
  6069. endfunction
  6070. " Setup/Teardown functions {{{1
  6071. " display the test name before each assertion
  6072. function! s:Setup()
  6073. if exists('g:UT_print_test')
  6074. let s:old_print_test = g:UT_print_test
  6075. endif
  6076. let g:UT_print_test = 1
  6077. endfunction
  6078. function! s:Teardown()
  6079. if exists('s:old_print_test')
  6080. let g:UT_print_test = s:old_print_test
  6081. unlet s:old_print_test
  6082. else
  6083. unlet g:UT_print_test
  6084. endif
  6085. endfunction
  6086. " }}}1
  6087. let &cpo=s:cpo_save
  6088. "=============================================================================
  6089. " vim600: set fdm=marker: