test-org.el 253 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;; Template test file for Org tests
  16. ;;; Code:
  17. (eval-and-compile (require 'cl-lib))
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (call-interactively #'org-comment-dwim)
  84. (buffer-string))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (call-interactively #'org-comment-dwim)
  91. (buffer-string))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# <point>Comment"
  96. (let ((org-adapt-indentation t))
  97. (call-interactively #'org-comment-dwim))
  98. (buffer-string))))
  99. ;; Also recognize single # at column 0 as comments.
  100. (should
  101. (equal "# Comment"
  102. (org-test-with-temp-text "# Comment"
  103. (call-interactively #'org-comment-dwim)
  104. (buffer-string))))
  105. ;; Region selected and only comments and blank lines within it:
  106. ;; un-comment all commented lines.
  107. (should
  108. (equal "Comment 1\n\nComment 2"
  109. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  110. (transient-mark-mode 1)
  111. (push-mark (point) t t)
  112. (goto-char (point-max))
  113. (call-interactively #'org-comment-dwim)
  114. (buffer-string))))
  115. ;; Region selected without comments: comment all lines if
  116. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  117. (should
  118. (equal "# Comment 1\n\n# Comment 2"
  119. (org-test-with-temp-text "Comment 1\n\nComment 2"
  120. (transient-mark-mode 1)
  121. (push-mark (point) t t)
  122. (goto-char (point-max))
  123. (let ((comment-empty-lines nil))
  124. (call-interactively #'org-comment-dwim))
  125. (buffer-string))))
  126. (should
  127. (equal "# Comment 1\n# \n# Comment 2"
  128. (org-test-with-temp-text "Comment 1\n\nComment 2"
  129. (transient-mark-mode 1)
  130. (push-mark (point) t t)
  131. (goto-char (point-max))
  132. (let ((comment-empty-lines t))
  133. (call-interactively #'org-comment-dwim))
  134. (buffer-string))))
  135. ;; In front of a keyword without region, insert a new comment.
  136. (should
  137. (equal "# \n#+KEYWORD: value"
  138. (org-test-with-temp-text "#+KEYWORD: value"
  139. (call-interactively #'org-comment-dwim)
  140. (buffer-string))))
  141. ;; Comment a heading
  142. (should
  143. (equal "* COMMENT Test"
  144. (org-test-with-temp-text "* Test"
  145. (call-interactively #'org-comment-dwim)
  146. (buffer-string))))
  147. ;; In a source block, use appropriate syntax.
  148. (should
  149. (equal " ;; "
  150. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively #'org-comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position)
  154. (point)))))
  155. (should
  156. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  157. (org-test-with-temp-text
  158. "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
  159. (transient-mark-mode 1)
  160. (push-mark (point) t t)
  161. (forward-line 2)
  162. (let ((org-edit-src-content-indentation 2))
  163. (call-interactively #'org-comment-dwim))
  164. (buffer-string)))))
  165. ;;; Date and time analysis
  166. (ert-deftest test-org/org-read-date ()
  167. "Test `org-read-date' specifications."
  168. ;; Parse ISO date with abbreviated year and month.
  169. (should (equal "2012-03-29 16:40"
  170. (let ((org-time-was-given t))
  171. (org-read-date t nil "12-3-29 16:40"))))
  172. ;; Parse Europeans dates.
  173. (should (equal "2012-03-29 16:40"
  174. (let ((org-time-was-given t))
  175. (org-read-date t nil "29.03.2012 16:40"))))
  176. ;; Parse Europeans dates without year.
  177. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  178. (let ((org-time-was-given t))
  179. (org-read-date t nil "29.03. 16:40"))))
  180. ;; Relative date applied to current time if there is single
  181. ;; plus/minus, or to default date when there are two of them.
  182. (should
  183. (equal
  184. "2015-03-04"
  185. (org-test-at-time "2014-03-04"
  186. (org-read-date
  187. t nil "+1y" nil
  188. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  189. (should
  190. (equal
  191. "2013-03-29"
  192. (org-test-at-time "2014-03-04"
  193. (org-read-date
  194. t nil "++1y" nil
  195. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  196. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  197. ;; dates (relatively to now) when incomplete. Otherwise, use
  198. ;; default date.
  199. (should
  200. (equal
  201. "2014-04-01"
  202. (org-test-at-time "2014-03-04"
  203. (let ((org-read-date-prefer-future t))
  204. (org-read-date t nil "1")))))
  205. (should
  206. (equal
  207. "2013-03-04"
  208. (org-test-at-time "2012-03-29"
  209. (let ((org-read-date-prefer-future t))
  210. (org-read-date t nil "3-4")))))
  211. (should
  212. (equal
  213. "2012-03-04"
  214. (org-test-at-time "2012-03-29"
  215. (let ((org-read-date-prefer-future nil))
  216. (org-read-date t nil "3-4")))))
  217. ;; When set to `org-read-date-prefer-future' is set to `time', read
  218. ;; day is moved to tomorrow if specified hour is before current
  219. ;; time. However, it only happens in no other part of the date is
  220. ;; specified.
  221. (should
  222. (equal
  223. "2012-03-30"
  224. (org-test-at-time "2012-03-29 16:40"
  225. (let ((org-read-date-prefer-future 'time))
  226. (org-read-date t nil "00:40" nil)))))
  227. (should-not
  228. (equal
  229. "2012-03-30"
  230. (org-test-at-time "2012-03-29 16:40"
  231. (let ((org-read-date-prefer-future 'time))
  232. (org-read-date t nil "29 00:40" nil)))))
  233. ;; Caveat: `org-read-date-prefer-future' always refers to current
  234. ;; time, not default time, when they differ.
  235. (should
  236. (equal
  237. "2014-04-01"
  238. (org-test-at-time "2014-03-04"
  239. (let ((org-read-date-prefer-future t))
  240. (org-read-date
  241. t nil "1" nil
  242. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  243. (should
  244. (equal
  245. "2014-03-25"
  246. (org-test-at-time "2014-03-04"
  247. (let ((org-read-date-prefer-future t))
  248. (org-read-date
  249. t nil "25" nil
  250. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  251. (ert-deftest test-org/org-parse-time-string ()
  252. "Test `org-parse-time-string'."
  253. (should (equal (org-parse-time-string "2012-03-29 16:40")
  254. '(0 40 16 29 3 2012 nil nil nil)))
  255. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  256. '(0 40 16 29 3 2012 nil nil nil)))
  257. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  258. '(0 40 16 29 3 2012 nil nil nil)))
  259. (should (equal (org-parse-time-string "<2012-03-29>")
  260. '(0 0 0 29 3 2012 nil nil nil)))
  261. (should (equal (org-parse-time-string "<2012-03-29>" t)
  262. '(0 nil nil 29 3 2012 nil nil nil))))
  263. (ert-deftest test-org/closest-date ()
  264. "Test `org-closest-date' specifications."
  265. (require 'calendar)
  266. ;; Time stamps without a repeater are returned unchanged.
  267. (should
  268. (equal
  269. '(3 29 2012)
  270. (calendar-gregorian-from-absolute
  271. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  272. ;; Time stamps with a null repeater are returned unchanged.
  273. (should
  274. (equal
  275. '(3 29 2012)
  276. (calendar-gregorian-from-absolute
  277. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  278. ;; if PREFER is set to `past' always return a date before, or equal
  279. ;; to CURRENT.
  280. (should
  281. (equal
  282. '(3 1 2014)
  283. (calendar-gregorian-from-absolute
  284. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  285. (should
  286. (equal
  287. '(3 4 2014)
  288. (calendar-gregorian-from-absolute
  289. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  290. ;; if PREFER is set to `future' always return a date before, or equal
  291. ;; to CURRENT.
  292. (should
  293. (equal
  294. '(3 29 2014)
  295. (calendar-gregorian-from-absolute
  296. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  297. (should
  298. (equal
  299. '(3 4 2014)
  300. (calendar-gregorian-from-absolute
  301. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  302. ;; If PREFER is neither `past' nor `future', select closest date.
  303. (should
  304. (equal
  305. '(3 1 2014)
  306. (calendar-gregorian-from-absolute
  307. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  308. (should
  309. (equal
  310. '(5 4 2014)
  311. (calendar-gregorian-from-absolute
  312. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  313. ;; Test "day" repeater.
  314. (should
  315. (equal '(3 8 2014)
  316. (calendar-gregorian-from-absolute
  317. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  318. (should
  319. (equal '(3 10 2014)
  320. (calendar-gregorian-from-absolute
  321. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  322. ;; Test "month" repeater.
  323. (should
  324. (equal '(1 5 2015)
  325. (calendar-gregorian-from-absolute
  326. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  327. (should
  328. (equal '(3 29 2014)
  329. (calendar-gregorian-from-absolute
  330. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  331. ;; Test "year" repeater.
  332. (should
  333. (equal '(3 5 2014)
  334. (calendar-gregorian-from-absolute
  335. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  336. (should
  337. (equal '(3 29 2014)
  338. (calendar-gregorian-from-absolute
  339. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  340. (ert-deftest test-org/deadline-close-p ()
  341. "Test `org-deadline-close-p' specifications."
  342. (org-test-at-time "2016-06-03 Fri 01:43"
  343. ;; Timestamps are close if they are within `ndays' of lead time.
  344. (org-test-with-temp-text "* Heading"
  345. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  346. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  347. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  348. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  349. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  350. ;; Read `ndays' from timestamp if argument not given.
  351. (org-test-with-temp-text "* H"
  352. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  353. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  354. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  355. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  356. ;; Prefer `ndays' argument over lead time in timestamp.
  357. (org-test-with-temp-text "* H"
  358. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  359. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  360. ;; Completed tasks are never close.
  361. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  362. (org-test-with-temp-text "* TODO Heading"
  363. (should (org-deadline-close-p "2016-06-03")))
  364. (org-test-with-temp-text "* DONE Heading"
  365. (should-not (org-deadline-close-p "2016-06-03"))))))
  366. ;;; Drawers
  367. (ert-deftest test-org/at-property-p ()
  368. "Test `org-at-property-p' specifications."
  369. (should
  370. (equal 't
  371. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:PROP: t\n:END:\n"
  372. (org-at-property-p))))
  373. (should
  374. (equal 't
  375. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n:END:\n"
  376. (org-at-property-p)))))
  377. (ert-deftest test-org/at-property-drawer-p ()
  378. "Test `org-at-property-drawer-p' specifications."
  379. (should
  380. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:PROP: t\n:END:\n"
  381. (org-at-property-drawer-p)))
  382. (should
  383. (org-test-with-temp-text ":PROPERTIES:\n:PROP: t\n:END:\n"
  384. (org-at-property-drawer-p)))
  385. ;; The function only returns t if point is at the first line of
  386. ;; a property block.
  387. (should-not
  388. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n:END:\n"
  389. (org-at-property-drawer-p)))
  390. ;; The function ignores incomplete drawers.
  391. (should-not
  392. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n"
  393. (org-at-property-drawer-p))))
  394. (ert-deftest test-org/get-property-block ()
  395. "Test `org-get-property-block' specifications."
  396. (should
  397. (equal '(14 . 14)
  398. (org-test-with-temp-text ":PROPERTIES:\n:END:\n* H\n"
  399. (org-get-property-block))))
  400. (should
  401. (equal '(14 . 14)
  402. (org-test-with-temp-text ":PROPERTIES:\n:END:\n"
  403. (org-get-property-block))))
  404. ;; Comments above a document property block is ok.
  405. (should
  406. (equal '(18 . 18)
  407. (org-test-with-temp-text "# C\n:PROPERTIES:\n:END:\n"
  408. (org-get-property-block))))
  409. ;; Keywords above a document property block is ok.
  410. (should
  411. (equal '(22 . 22)
  412. (org-test-with-temp-text "# C\n# C\n:PROPERTIES:\n:END:\n"
  413. (org-get-property-block))))
  414. ;; Comments and keywords are allowed before a document property block.
  415. (should
  416. (equal '(18 . 27)
  417. (org-test-with-temp-text "# C\n:PROPERTIES:\n:KEY: V:\n:END:\n"
  418. (org-get-property-block))))
  419. ;; A document property block will not be valid if there are lines
  420. ;; with whitespace above it
  421. (should-not
  422. (org-test-with-temp-text "\n:PROPERTIES:\n:END:\n"
  423. (org-get-property-block)))
  424. (should
  425. (equal '(18 . 18)
  426. (org-test-with-temp-text "* H\n:PROPERTIES:\n:END:\n<point>"
  427. (org-get-property-block))))
  428. (should
  429. (equal "* H\n:PROPERTIES:\n:END:\n"
  430. (org-test-with-temp-text "* H"
  431. (let ((org-adapt-indentation nil))
  432. (org-get-property-block nil 'force))
  433. (buffer-string))))
  434. (should
  435. (equal ":PROPERTIES:\n:END:\n"
  436. (org-test-with-temp-text ""
  437. (org-get-property-block nil 'force)
  438. (buffer-string))))
  439. (should
  440. (equal "* H1\n :PROPERTIES:\n :END:\n* H2"
  441. (org-test-with-temp-text "* H1\n* H2"
  442. (let ((org-adapt-indentation t))
  443. (org-get-property-block nil 'force))
  444. (buffer-string)))))
  445. (ert-deftest test-org/insert-property-drawer ()
  446. "Test `org-insert-property-drawer' specifications."
  447. ;; Insert drawer in empty buffer
  448. (should
  449. (equal ":PROPERTIES:\n:END:\n"
  450. (org-test-with-temp-text ""
  451. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  452. (buffer-string))))
  453. ;; Insert drawer in document header with existing comment and
  454. ;; keyword.
  455. (should
  456. (equal "# C\n:PROPERTIES:\n:END:\n#+TITLE: T"
  457. (org-test-with-temp-text "# C\n#+TITLE: T"
  458. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  459. (buffer-string))))
  460. ;; Insert drawer in document header with existing keyword.
  461. (should
  462. (equal ":PROPERTIES:\n:END:\n#+TITLE: T"
  463. (org-test-with-temp-text "#+TITLE: T"
  464. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  465. (buffer-string))))
  466. (should
  467. (equal ":PROPERTIES:\n:END:"
  468. (org-test-with-temp-text ":PROPERTIES:\n:END:"
  469. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  470. (buffer-string))))
  471. ;; Insert drawer in document header with one existing heading in buffer.
  472. (should
  473. (equal ":PROPERTIES:\n:END:\n\n* T\n"
  474. (org-test-with-temp-text "\n* T\n"
  475. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  476. (buffer-string))))
  477. ;; Insert drawer right after headline if there is no planning line,
  478. ;; or after it otherwise.
  479. (should
  480. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  481. (org-test-with-temp-text "* H\nParagraph<point>"
  482. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  483. (buffer-string))))
  484. (should
  485. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  486. (org-test-with-temp-text
  487. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  488. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  489. (buffer-string))))
  490. ;; Indent inserted drawer.
  491. (should
  492. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  493. (org-test-with-temp-text "* H\nParagraph<point>"
  494. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  495. (buffer-string))))
  496. ;; Handle insertion at eob.
  497. (should
  498. (equal "* H\n:PROPERTIES:\n:END:\n"
  499. (org-test-with-temp-text "* H"
  500. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  501. (buffer-string))))
  502. ;; Skip inlinetasks before point.
  503. (when (featurep 'org-inlinetask)
  504. (should
  505. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  506. (org-test-with-temp-text
  507. "* H\n*************** I\n*************** END\nP<point>"
  508. (let ((org-adapt-indentation nil)
  509. (org-inlinetask-min-level 15))
  510. (org-insert-property-drawer))
  511. (buffer-string)))))
  512. ;; Correctly set drawer in an inlinetask.
  513. (when (featurep 'org-inlinetask)
  514. (should
  515. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  516. (org-test-with-temp-text
  517. "* H\n*************** I\nP<point>\n*************** END"
  518. (let ((org-adapt-indentation nil)
  519. (org-inlinetask-min-level 15))
  520. (org-insert-property-drawer))
  521. (buffer-string))))))
  522. ;;; Filling
  523. (ert-deftest test-org/fill-element ()
  524. "Test `org-fill-element' specifications."
  525. ;; At an Org table, align it.
  526. (should
  527. (equal "| a |\n"
  528. (org-test-with-temp-text "|a|"
  529. (org-fill-element)
  530. (buffer-string))))
  531. (should
  532. (equal "#+name: table\n| a |\n"
  533. (org-test-with-temp-text "#+name: table\n| a |\n"
  534. (org-fill-element)
  535. (buffer-string))))
  536. ;; At a paragraph, preserve line breaks.
  537. (org-test-with-temp-text "some \\\\\nlong\ntext"
  538. (let ((fill-column 20))
  539. (org-fill-element)
  540. (should (equal (buffer-string) "some \\\\\nlong text"))))
  541. ;; Correctly fill a paragraph when point is at its very end.
  542. (should
  543. (equal "A B"
  544. (org-test-with-temp-text "A\nB"
  545. (let ((fill-column 20))
  546. (goto-char (point-max))
  547. (org-fill-element)
  548. (buffer-string)))))
  549. ;; Correctly fill the last paragraph of a greater element.
  550. (should
  551. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  552. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  553. (let ((fill-column 8))
  554. (forward-line)
  555. (end-of-line)
  556. (org-fill-element)
  557. (buffer-string)))))
  558. ;; Correctly fill an element in a narrowed buffer.
  559. (should
  560. (equal "01234\n6"
  561. (org-test-with-temp-text "01234 6789"
  562. (let ((fill-column 5))
  563. (narrow-to-region 1 8)
  564. (org-fill-element)
  565. (buffer-string)))))
  566. ;; Handle `adaptive-fill-regexp' in paragraphs.
  567. (should
  568. (equal "> a b"
  569. (org-test-with-temp-text "> a\n> b"
  570. (let ((fill-column 5)
  571. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  572. (org-fill-element)
  573. (buffer-string)))))
  574. ;; Special case: Fill first paragraph when point is at an item or
  575. ;; a plain-list or a footnote reference.
  576. (should
  577. (equal "- A B"
  578. (org-test-with-temp-text "- A\n B"
  579. (let ((fill-column 20))
  580. (org-fill-element)
  581. (buffer-string)))))
  582. (should
  583. (equal "[fn:1] A B"
  584. (org-test-with-temp-text "[fn:1] A\nB"
  585. (let ((fill-column 20))
  586. (org-fill-element)
  587. (buffer-string)))))
  588. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  589. (let ((fill-column 20))
  590. (org-fill-element)
  591. (should (equal (buffer-string)
  592. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  593. ;; Fill contents of `comment-block' elements.
  594. (should
  595. (equal
  596. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  597. (let ((fill-column 20))
  598. (forward-line)
  599. (org-fill-element)
  600. (buffer-string)))
  601. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  602. ;; Fill `comment' elements.
  603. (should
  604. (equal " # A B"
  605. (org-test-with-temp-text " # A\n # B"
  606. (let ((fill-column 20))
  607. (org-fill-element)
  608. (buffer-string)))))
  609. ;; Do not mix consecutive comments when filling one of them.
  610. (should
  611. (equal "# A B\n\n# C"
  612. (org-test-with-temp-text "# A\n# B\n\n# C"
  613. (let ((fill-column 20))
  614. (org-fill-element)
  615. (buffer-string)))))
  616. ;; Use commented empty lines as separators when filling comments.
  617. (should
  618. (equal "# A B\n#\n# C"
  619. (org-test-with-temp-text "# A\n# B\n#\n# C"
  620. (let ((fill-column 20))
  621. (org-fill-element)
  622. (buffer-string)))))
  623. ;; Handle `adaptive-fill-regexp' in comments.
  624. (should
  625. (equal "# > a b"
  626. (org-test-with-temp-text "# > a\n# > b"
  627. (let ((fill-column 20)
  628. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  629. (org-fill-element)
  630. (buffer-string)))))
  631. ;; Do nothing at affiliated keywords.
  632. (should
  633. (equal "#+NAME: para\nSome\ntext."
  634. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  635. (let ((fill-column 20))
  636. (org-fill-element)
  637. (buffer-string)))))
  638. ;; Do not move point after table when filling a table.
  639. (should-not
  640. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  641. (forward-char)
  642. (org-fill-element)
  643. (eobp)))
  644. ;; Do not fill "n" macro, with or without arguments, followed by
  645. ;; a dot or a closing parenthesis since it could be confused with
  646. ;; a numbered bullet.
  647. (should-not
  648. (equal "123456789\n{{{n}}}."
  649. (org-test-with-temp-text "123456789 {{{n}}}."
  650. (let ((fill-column 10))
  651. (org-fill-element)
  652. (buffer-string)))))
  653. (should-not
  654. (equal "123456789\n{{{n}}}\)"
  655. (org-test-with-temp-text "123456789 {{{n}}}\)"
  656. (let ((fill-column 10))
  657. (org-fill-element)
  658. (buffer-string)))))
  659. (should-not
  660. (equal "123456789\n{{{n()}}}."
  661. (org-test-with-temp-text "123456789 {{{n()}}}."
  662. (let ((fill-column 10))
  663. (org-fill-element)
  664. (buffer-string)))))
  665. (should-not
  666. (equal "123456789\n{{{n(counter)}}}."
  667. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  668. (let ((fill-column 10))
  669. (org-fill-element)
  670. (buffer-string))))))
  671. (ert-deftest test-org/auto-fill-function ()
  672. "Test auto-filling features."
  673. ;; Auto fill paragraph.
  674. (should
  675. (equal "12345\n7890"
  676. (org-test-with-temp-text "12345 7890"
  677. (let ((fill-column 5))
  678. (end-of-line)
  679. (org-auto-fill-function)
  680. (buffer-string)))))
  681. ;; Auto fill first paragraph in an item.
  682. (should
  683. (equal "- 12345\n 7890"
  684. (org-test-with-temp-text "- 12345 7890"
  685. (let ((fill-column 7))
  686. (end-of-line)
  687. (org-auto-fill-function)
  688. (buffer-string)))))
  689. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  690. (should
  691. (equal "> 12345\n 7890"
  692. (org-test-with-temp-text "> 12345 7890"
  693. (let ((fill-column 10)
  694. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  695. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  696. (end-of-line)
  697. (org-auto-fill-function)
  698. (buffer-string)))))
  699. (should
  700. (equal "> 12345\n> 12345\n> 7890"
  701. (org-test-with-temp-text "> 12345\n> 12345 7890"
  702. (let ((fill-column 10)
  703. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  704. (goto-char (point-max))
  705. (org-auto-fill-function)
  706. (buffer-string)))))
  707. (should-not
  708. (equal " 12345\n *12345\n *12345"
  709. (org-test-with-temp-text " 12345\n *12345 12345"
  710. (let ((fill-column 10)
  711. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  712. (goto-char (point-max))
  713. (org-auto-fill-function)
  714. (buffer-string)))))
  715. ;; Auto fill comments.
  716. (should
  717. (equal " # 12345\n # 7890"
  718. (org-test-with-temp-text " # 12345 7890"
  719. (let ((fill-column 10))
  720. (end-of-line)
  721. (org-auto-fill-function)
  722. (buffer-string)))))
  723. ;; A hash within a line isn't a comment.
  724. (should-not
  725. (equal "12345 # 7890\n# 1"
  726. (org-test-with-temp-text "12345 # 7890 1"
  727. (let ((fill-column 12))
  728. (end-of-line)
  729. (org-auto-fill-function)
  730. (buffer-string)))))
  731. ;; Correctly interpret empty prefix.
  732. (should-not
  733. (equal "# a\n# b\nRegular\n# paragraph"
  734. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  735. (let ((fill-column 12))
  736. (end-of-line 3)
  737. (org-auto-fill-function)
  738. (buffer-string)))))
  739. ;; Comment block: auto fill contents.
  740. (should
  741. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  742. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  743. (let ((fill-column 5))
  744. (forward-line)
  745. (end-of-line)
  746. (org-auto-fill-function)
  747. (buffer-string)))))
  748. (should
  749. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  750. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  751. (let ((fill-column 5))
  752. (forward-line)
  753. (end-of-line)
  754. (org-auto-fill-function)
  755. (buffer-string)))))
  756. ;; Do not fill if a new item could be created.
  757. (should-not
  758. (equal "12345\n- 90"
  759. (org-test-with-temp-text "12345 - 90"
  760. (let ((fill-column 5))
  761. (end-of-line)
  762. (org-auto-fill-function)
  763. (buffer-string)))))
  764. ;; Do not fill if a line break could be introduced.
  765. (should-not
  766. (equal "123\\\\\n7890"
  767. (org-test-with-temp-text "123\\\\ 7890"
  768. (let ((fill-column 6))
  769. (end-of-line)
  770. (org-auto-fill-function)
  771. (buffer-string)))))
  772. ;; Do not fill affiliated keywords.
  773. (should-not
  774. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  775. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  776. (let ((fill-column 20))
  777. (end-of-line)
  778. (org-auto-fill-function)
  779. (buffer-string))))))
  780. ;;; Indentation
  781. (ert-deftest test-org/indent-line ()
  782. "Test `org-indent-line' specifications."
  783. ;; Do not indent diary sexps, footnote definitions or headlines.
  784. (should
  785. (zerop
  786. (org-test-with-temp-text "%%(org-calendar-holiday)"
  787. (org-indent-line)
  788. (org-get-indentation))))
  789. (should
  790. (zerop
  791. (org-test-with-temp-text "[fn:1] fn"
  792. (let ((org-adapt-indentation t)) (org-indent-line))
  793. (org-get-indentation))))
  794. (should
  795. (zerop
  796. (org-test-with-temp-text "* H"
  797. (org-indent-line)
  798. (org-get-indentation))))
  799. ;; Do not indent before first headline.
  800. (should
  801. (zerop
  802. (org-test-with-temp-text ""
  803. (org-indent-line)
  804. (org-get-indentation))))
  805. ;; Indent according to headline level otherwise, unless
  806. ;; `org-adapt-indentation' is nil.
  807. (should
  808. (= 2
  809. (org-test-with-temp-text "* H\n<point>A"
  810. (let ((org-adapt-indentation t)) (org-indent-line))
  811. (org-get-indentation))))
  812. (should
  813. (= 2
  814. (org-test-with-temp-text "* H\n<point>\nA"
  815. (let ((org-adapt-indentation t)) (org-indent-line))
  816. (org-get-indentation))))
  817. (should
  818. (zerop
  819. (org-test-with-temp-text "* H\n<point>A"
  820. (let ((org-adapt-indentation nil)) (org-indent-line))
  821. (org-get-indentation))))
  822. ;; Indenting preserves point position.
  823. (should
  824. (org-test-with-temp-text "* H\nA<point>B"
  825. (let ((org-adapt-indentation t)) (org-indent-line))
  826. (looking-at "B")))
  827. ;; Do not change indentation at an item or a LaTeX environment.
  828. (should
  829. (= 1
  830. (org-test-with-temp-text "* H\n<point> - A"
  831. (let ((org-adapt-indentation t)) (org-indent-line))
  832. (org-get-indentation))))
  833. (should
  834. (= 1
  835. (org-test-with-temp-text
  836. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  837. (org-indent-line)
  838. (org-get-indentation))))
  839. ;; On blank lines at the end of a list, indent like last element
  840. ;; within it if the line is still in the list. If the last element
  841. ;; is an item, indent like its contents. Otherwise, indent like the
  842. ;; whole list.
  843. (should
  844. (= 4
  845. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  846. (let ((org-adapt-indentation t)) (org-indent-line))
  847. (org-get-indentation))))
  848. (should
  849. (= 4
  850. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  851. (let ((org-adapt-indentation t)) (org-indent-line))
  852. (org-get-indentation))))
  853. (should
  854. (zerop
  855. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  856. (let ((org-adapt-indentation t)) (org-indent-line))
  857. (org-get-indentation))))
  858. (should
  859. (= 4
  860. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  861. (let ((org-adapt-indentation t)) (org-indent-line))
  862. (org-get-indentation))))
  863. (should
  864. (= 4
  865. (org-test-with-temp-text
  866. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  867. (let ((org-adapt-indentation t)) (org-indent-line))
  868. (org-get-indentation))))
  869. (should
  870. (= 2
  871. (org-test-with-temp-text "- A\n B\n\n<point>"
  872. (let ((org-adapt-indentation nil)) (org-indent-line))
  873. (org-get-indentation))))
  874. (should
  875. (= 2
  876. (org-test-with-temp-text
  877. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  878. (let ((org-adapt-indentation nil)) (org-indent-line))
  879. (org-get-indentation))))
  880. ;; Likewise, on a blank line at the end of a footnote definition,
  881. ;; indent at column 0 if line belongs to the definition. Otherwise,
  882. ;; indent like the definition itself.
  883. (should
  884. (zerop
  885. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  886. (let ((org-adapt-indentation t)) (org-indent-line))
  887. (org-get-indentation))))
  888. (should
  889. (zerop
  890. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  891. (let ((org-adapt-indentation t)) (org-indent-line))
  892. (org-get-indentation))))
  893. ;; After the end of the contents of a greater element, indent like
  894. ;; the beginning of the element.
  895. (should
  896. (= 1
  897. (org-test-with-temp-text
  898. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  899. (org-indent-line)
  900. (org-get-indentation))))
  901. ;; On blank lines after a paragraph, indent like its last non-empty
  902. ;; line.
  903. (should
  904. (= 1
  905. (org-test-with-temp-text " Paragraph\n\n<point>"
  906. (org-indent-line)
  907. (org-get-indentation))))
  908. ;; At the first line of an element, indent like previous element's
  909. ;; first line, ignoring footnotes definitions and inline tasks, or
  910. ;; according to parent.
  911. (should
  912. (= 2
  913. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  914. (org-indent-line)
  915. (org-get-indentation))))
  916. (should
  917. (= 1
  918. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  919. (org-indent-line)
  920. (org-get-indentation))))
  921. (should
  922. (= 1
  923. (org-test-with-temp-text
  924. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  925. (org-indent-line)
  926. (org-get-indentation))))
  927. ;; Within code part of a source block, use language major mode if
  928. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  929. ;; according to line above.
  930. (should
  931. (= 6
  932. (org-test-with-temp-text
  933. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  934. (let ((org-src-tab-acts-natively t)
  935. (org-edit-src-content-indentation 0))
  936. (org-indent-line))
  937. (org-get-indentation))))
  938. (should
  939. (= 1
  940. (org-test-with-temp-text
  941. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  942. (let ((org-src-tab-acts-natively nil)
  943. (org-edit-src-content-indentation 0))
  944. (org-indent-line))
  945. (org-get-indentation))))
  946. ;; Otherwise, indent like the first non-blank line above.
  947. (should
  948. (zerop
  949. (org-test-with-temp-text
  950. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  951. (org-indent-line)
  952. (org-get-indentation))))
  953. ;; Align node properties according to `org-property-format'. Handle
  954. ;; nicely empty values.
  955. (should
  956. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  957. (org-test-with-temp-text
  958. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  959. (let ((org-property-format "%-10s %s")) (org-indent-line))
  960. (buffer-string))))
  961. (should
  962. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  963. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  964. (let ((org-property-format "%-10s %s")) (org-indent-line))
  965. (buffer-string)))))
  966. (ert-deftest test-org/indent-region ()
  967. "Test `org-indent-region' specifications."
  968. ;; Indent paragraph.
  969. (should
  970. (equal "A\nB\nC"
  971. (org-test-with-temp-text " A\nB\n C"
  972. (org-indent-region (point-min) (point-max))
  973. (buffer-string))))
  974. ;; Indent greater elements along with their contents.
  975. (should
  976. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  977. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  978. (org-indent-region (point-min) (point-max))
  979. (buffer-string))))
  980. ;; Ignore contents of verse blocks. Only indent block delimiters.
  981. (should
  982. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  983. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  984. (org-indent-region (point-min) (point-max))
  985. (buffer-string))))
  986. (should
  987. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  988. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  989. (org-indent-region (point-min) (point-max))
  990. (buffer-string))))
  991. ;; Indent example blocks as a single block, unless indentation
  992. ;; should be preserved. In this case only indent the block markers.
  993. (should
  994. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  995. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  996. (org-indent-region (point-min) (point-max))
  997. (buffer-string))))
  998. (should
  999. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1000. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  1001. (org-indent-region (point-min) (point-max))
  1002. (buffer-string))))
  1003. (should
  1004. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  1005. (org-test-with-temp-text
  1006. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  1007. (org-indent-region (point-min) (point-max))
  1008. (buffer-string))))
  1009. (should
  1010. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1011. (org-test-with-temp-text
  1012. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  1013. (let ((org-src-preserve-indentation t))
  1014. (org-indent-region (point-min) (point-max)))
  1015. (buffer-string))))
  1016. ;; Treat export blocks as a whole.
  1017. (should
  1018. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1019. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1020. (org-indent-region (point-min) (point-max))
  1021. (buffer-string))))
  1022. (should
  1023. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1024. (org-test-with-temp-text
  1025. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  1026. (org-indent-region (point-min) (point-max))
  1027. (buffer-string))))
  1028. ;; Indent according to mode if `org-src-tab-acts-natively' is
  1029. ;; non-nil. Otherwise, do not indent code at all.
  1030. (should
  1031. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  1032. (org-test-with-temp-text
  1033. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1034. (let ((org-src-tab-acts-natively t)
  1035. (org-edit-src-content-indentation 0))
  1036. (org-indent-region (point-min) (point-max)))
  1037. (buffer-string))))
  1038. (should
  1039. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1040. (org-test-with-temp-text
  1041. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1042. (let ((org-src-tab-acts-natively nil)
  1043. (org-edit-src-content-indentation 0))
  1044. (org-indent-region (point-min) (point-max)))
  1045. (buffer-string))))
  1046. ;; Align node properties according to `org-property-format'. Handle
  1047. ;; nicely empty values.
  1048. (should
  1049. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  1050. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  1051. (let ((org-property-format "%-10s %s")
  1052. (org-adapt-indentation nil))
  1053. (org-indent-region (point) (point-max)))
  1054. (buffer-string))))
  1055. (should
  1056. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  1057. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  1058. (let ((org-property-format "%-10s %s")
  1059. (org-adapt-indentation nil))
  1060. (org-indent-region (point) (point-max)))
  1061. (buffer-string))))
  1062. ;; Indent plain lists.
  1063. (should
  1064. (equal "- A\n B\n - C\n\n D"
  1065. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  1066. (org-indent-region (point-min) (point-max))
  1067. (buffer-string))))
  1068. (should
  1069. (equal "- A\n\n- B"
  1070. (org-test-with-temp-text " - A\n\n - B"
  1071. (org-indent-region (point-min) (point-max))
  1072. (buffer-string))))
  1073. ;; Indent footnote definitions.
  1074. (should
  1075. (equal "[fn:1] Definition\n\nDefinition"
  1076. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  1077. (org-indent-region (point-min) (point-max))
  1078. (buffer-string))))
  1079. ;; Special case: Start indenting on a blank line.
  1080. (should
  1081. (equal "\nParagraph"
  1082. (org-test-with-temp-text "\n Paragraph"
  1083. (org-indent-region (point-min) (point-max))
  1084. (buffer-string)))))
  1085. ;;; Editing
  1086. (ert-deftest test-org/delete-indentation ()
  1087. "Test `org-delete-indentation' specifications."
  1088. ;; Regular test.
  1089. (should (equal "foo bar"
  1090. (org-test-with-temp-text
  1091. "foo \n bar<point>"
  1092. (org-delete-indentation)
  1093. (buffer-string))))
  1094. ;; With optional argument.
  1095. (should (equal "foo bar"
  1096. (org-test-with-temp-text
  1097. "foo<point> \n bar"
  1098. (org-delete-indentation t)
  1099. (buffer-string))))
  1100. ;; At headline text should be appended to the headline text.
  1101. (should
  1102. (equal"* foo bar :tag:"
  1103. (let (org-auto-align-tags)
  1104. (org-test-with-temp-text
  1105. "* foo :tag:\n bar<point>"
  1106. (org-delete-indentation)
  1107. (buffer-string)))))
  1108. (should
  1109. (equal "* foo bar :tag:"
  1110. (let (org-auto-align-tags)
  1111. (org-test-with-temp-text
  1112. "* foo <point>:tag:\n bar"
  1113. (org-delete-indentation t)
  1114. (buffer-string))))))
  1115. (ert-deftest test-org/return ()
  1116. "Test `org-return' specifications."
  1117. ;; Regular test.
  1118. (should
  1119. (equal "Para\ngraph"
  1120. (org-test-with-temp-text "Para<point>graph"
  1121. (org-return)
  1122. (buffer-string))))
  1123. ;; With optional argument, indent line.
  1124. (should
  1125. (equal " Para\n graph"
  1126. (org-test-with-temp-text " Para<point>graph"
  1127. (org-return t)
  1128. (buffer-string))))
  1129. ;; On a table, call `org-table-next-row'.
  1130. (should
  1131. (org-test-with-temp-text "| <point>a |\n| b |"
  1132. (org-return)
  1133. (looking-at-p "b")))
  1134. ;; Open link or timestamp under point when `org-return-follows-link'
  1135. ;; is non-nil.
  1136. (should
  1137. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1138. (let ((org-return-follows-link t)
  1139. (org-link-search-must-match-exact-headline nil))
  1140. (org-return))
  1141. (looking-at-p "<<target>>")))
  1142. (should-not
  1143. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1144. (let ((org-return-follows-link nil)) (org-return))
  1145. (looking-at-p "<<target>>")))
  1146. (should
  1147. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1148. (let ((org-return-follows-link t)) (org-return))
  1149. (looking-at-p "* b")))
  1150. (should
  1151. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1152. (let ((org-return-follows-link t)
  1153. (org-link-search-must-match-exact-headline nil))
  1154. (org-return))
  1155. (looking-at-p "<<target>>")))
  1156. (should-not
  1157. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1158. (let ((org-return-follows-link t)
  1159. (org-link-search-must-match-exact-headline nil))
  1160. (org-return))
  1161. (looking-at-p "<<target>>")))
  1162. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1163. ;; timestamps in comments, node properties, etc.
  1164. (should
  1165. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1166. (let ((org-return-follows-link t)
  1167. (org-link-search-must-match-exact-headline nil))
  1168. (org-return))
  1169. (looking-at-p "<<target>>")))
  1170. (should-not
  1171. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1172. (let ((org-return-follows-link nil)) (org-return))
  1173. (looking-at-p "<<target>>")))
  1174. (should-not
  1175. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1176. (let ((org-return-follows-link t)
  1177. (org-link-search-must-match-exact-headline nil))
  1178. (org-return))
  1179. (looking-at-p "<<target>>")))
  1180. ;; Non-nil `org-return-follows-link' ignores read-only state of
  1181. ;; a buffer.
  1182. (should
  1183. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1184. (let ((org-return-follows-link t)
  1185. (org-link-search-must-match-exact-headline nil))
  1186. (setq buffer-read-only t)
  1187. (call-interactively #'org-return))
  1188. (looking-at-p "<<target>>")))
  1189. ;; `org-return-follows-link' handle multi-line lines.
  1190. (should
  1191. (org-test-with-temp-text
  1192. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1193. (let ((org-return-follows-link t)
  1194. (org-link-search-must-match-exact-headline nil))
  1195. (org-return))
  1196. (looking-at-p "<<target>>")))
  1197. (should-not
  1198. (org-test-with-temp-text
  1199. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1200. (let ((org-return-follows-link t)
  1201. (org-link-search-must-match-exact-headline nil))
  1202. (org-return))
  1203. (looking-at-p "<<target>>")))
  1204. ;; However, do not open link when point is in a table.
  1205. (should
  1206. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1207. (let ((org-return-follows-link t)) (org-return))
  1208. (looking-at-p "between")))
  1209. ;; Special case: in a list, when indenting, do not break structure.
  1210. (should
  1211. (equal "- A\n B"
  1212. (org-test-with-temp-text "- A <point>B"
  1213. (org-return t)
  1214. (buffer-string))))
  1215. (should
  1216. (equal "- A\n\n- B"
  1217. (org-test-with-temp-text "- A\n<point>- B"
  1218. (org-return t)
  1219. (buffer-string))))
  1220. ;; On tags part of a headline, add a newline below it instead of
  1221. ;; breaking it.
  1222. (should
  1223. (equal "* H :tag:\n"
  1224. (org-test-with-temp-text "* H :<point>tag:"
  1225. (org-return)
  1226. (buffer-string))))
  1227. ;; Before headline text, add a newline below it instead of breaking
  1228. ;; it.
  1229. (should
  1230. (equal "* TODO H :tag:\n"
  1231. (org-test-with-temp-text "* <point>TODO H :tag:"
  1232. (org-return)
  1233. (buffer-string))))
  1234. (should
  1235. (equal "* TODO [#B] H :tag:\n"
  1236. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1237. (org-return)
  1238. (buffer-string))))
  1239. (should ;TODO are case-sensitive
  1240. (equal "* \nTodo"
  1241. (org-test-with-temp-text "* <point>Todo"
  1242. (org-return)
  1243. (buffer-string))))
  1244. ;; At headline text, break headline text but preserve tags.
  1245. (should
  1246. (equal "* TODO [#B] foo :tag:\nbar"
  1247. (let (org-auto-align-tags)
  1248. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1249. (org-return)
  1250. (buffer-string)))))
  1251. ;; At bol of headline insert newline.
  1252. (should
  1253. (equal "\n* h"
  1254. (org-test-with-temp-text "<point>* h"
  1255. (org-return)
  1256. (buffer-string))))
  1257. ;; Refuse to leave invalid headline in buffer.
  1258. (should
  1259. (equal "* h\n"
  1260. (org-test-with-temp-text "*<point> h"
  1261. (org-return)
  1262. (buffer-string))))
  1263. ;; Before first column or after last one in a table, split the
  1264. ;; table.
  1265. (should
  1266. (equal "| a |\n\n| b |"
  1267. (org-test-with-temp-text "| a |\n<point>| b |"
  1268. (org-return)
  1269. (buffer-string))))
  1270. (should
  1271. (equal "| a |\n\n| b |"
  1272. (org-test-with-temp-text "| a |<point>\n| b |"
  1273. (org-return)
  1274. (buffer-string))))
  1275. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1276. (should
  1277. (equal "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.\n\n:END:"
  1278. (org-test-with-temp-text "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.<point>\n:END:"
  1279. (setq-local fill-column 10)
  1280. (auto-fill-mode 1)
  1281. (org-return)
  1282. (buffer-string)))))
  1283. (ert-deftest test-org/with-electric-indent ()
  1284. "Test RET and C-j specifications with `electric-indent-mode' on."
  1285. ;; Call commands interactively, since this is how `newline' knows it
  1286. ;; must run `post-self-insert-hook'.
  1287. ;;
  1288. ;; RET, like `newline', should indent.
  1289. (should
  1290. (equal " Para\n graph"
  1291. (org-test-with-temp-text " Para<point>graph"
  1292. (electric-indent-local-mode 1)
  1293. (call-interactively 'org-return)
  1294. (buffer-string))))
  1295. (should
  1296. (equal "- item1\n item2"
  1297. (org-test-with-temp-text "- item1<point>item2"
  1298. (electric-indent-local-mode 1)
  1299. (call-interactively 'org-return)
  1300. (buffer-string))))
  1301. (should
  1302. (equal "* heading\n body"
  1303. (org-test-with-temp-text "* heading<point>body"
  1304. (electric-indent-local-mode 1)
  1305. (call-interactively 'org-return)
  1306. (buffer-string))))
  1307. ;; C-j, like `electric-newline-and-maybe-indent', should not indent.
  1308. (should
  1309. (equal " Para\ngraph"
  1310. (org-test-with-temp-text " Para<point>graph"
  1311. (electric-indent-local-mode 1)
  1312. (call-interactively 'org-return-and-maybe-indent)
  1313. (buffer-string))))
  1314. (should
  1315. (equal "- item1\nitem2"
  1316. (org-test-with-temp-text "- item1<point>item2"
  1317. (electric-indent-local-mode 1)
  1318. (call-interactively 'org-return-and-maybe-indent)
  1319. (buffer-string))))
  1320. (should
  1321. (equal "* heading\nbody"
  1322. (org-test-with-temp-text "* heading<point>body"
  1323. (electric-indent-local-mode 1)
  1324. (call-interactively 'org-return-and-maybe-indent)
  1325. (buffer-string)))))
  1326. (ert-deftest test-org/without-electric-indent ()
  1327. "Test RET and C-j specifications with `electric-indent-mode' off."
  1328. ;; Call commands interactively, since this is how `newline' knows it
  1329. ;; must run `post-self-insert-hook'.
  1330. ;;
  1331. ;; RET, like `newline', should not indent.
  1332. (should
  1333. (equal " Para\ngraph"
  1334. (org-test-with-temp-text " Para<point>graph"
  1335. (electric-indent-local-mode 0)
  1336. (call-interactively 'org-return)
  1337. (buffer-string))))
  1338. (should
  1339. (equal "- item1\nitem2"
  1340. (org-test-with-temp-text "- item1<point>item2"
  1341. (electric-indent-local-mode 0)
  1342. (call-interactively 'org-return)
  1343. (buffer-string))))
  1344. (should
  1345. (equal "* heading\nbody"
  1346. (org-test-with-temp-text "* heading<point>body"
  1347. (electric-indent-local-mode 0)
  1348. (call-interactively 'org-return)
  1349. (buffer-string))))
  1350. ;; C-j, like `electric-newline-and-maybe-indent', should indent.
  1351. (should
  1352. (equal " Para\n graph"
  1353. (org-test-with-temp-text " Para<point>graph"
  1354. (electric-indent-local-mode 0)
  1355. (call-interactively 'org-return-and-maybe-indent)
  1356. (buffer-string))))
  1357. (should
  1358. (equal "- item1\n item2"
  1359. (org-test-with-temp-text "- item1<point>item2"
  1360. (electric-indent-local-mode 0)
  1361. (call-interactively 'org-return-and-maybe-indent)
  1362. (buffer-string))))
  1363. (should
  1364. (equal "* heading\n body"
  1365. (org-test-with-temp-text "* heading<point>body"
  1366. (electric-indent-local-mode 0)
  1367. (call-interactively 'org-return-and-maybe-indent)
  1368. (buffer-string)))))
  1369. (ert-deftest test-org/meta-return ()
  1370. "Test M-RET (`org-meta-return') specifications."
  1371. ;; In a table field insert a row above.
  1372. (should
  1373. (org-test-with-temp-text "| a |"
  1374. (forward-char)
  1375. (org-meta-return)
  1376. (forward-line -1)
  1377. (looking-at "| |$")))
  1378. ;; In a paragraph change current line into a header.
  1379. (should
  1380. (org-test-with-temp-text "a"
  1381. (org-meta-return)
  1382. (beginning-of-line)
  1383. (looking-at "\* a$")))
  1384. ;; In an item insert an item, in this case above.
  1385. (should
  1386. (org-test-with-temp-text "- a"
  1387. (org-meta-return)
  1388. (beginning-of-line)
  1389. (looking-at "- $")))
  1390. ;; In a drawer and item insert an item, in this case above.
  1391. (should
  1392. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1393. (forward-line)
  1394. (org-meta-return)
  1395. (beginning-of-line)
  1396. (looking-at "- $"))))
  1397. (ert-deftest test-org/insert-heading ()
  1398. "Test `org-insert-heading' specifications."
  1399. ;; In an empty buffer, insert a new headline.
  1400. (should
  1401. (equal "* "
  1402. (org-test-with-temp-text ""
  1403. (org-insert-heading)
  1404. (buffer-string))))
  1405. ;; At the beginning of a line, turn it into a headline.
  1406. (should
  1407. (equal "* P"
  1408. (org-test-with-temp-text "<point>P"
  1409. (org-insert-heading)
  1410. (buffer-string))))
  1411. ;; In the middle of a line, split the line if allowed, otherwise,
  1412. ;; insert the headline at its end.
  1413. (should
  1414. (equal "Para\n* graph"
  1415. (org-test-with-temp-text "Para<point>graph"
  1416. (let ((org-M-RET-may-split-line '((default . t))))
  1417. (org-insert-heading))
  1418. (buffer-string))))
  1419. (should
  1420. (equal "Paragraph\n* "
  1421. (org-test-with-temp-text "Para<point>graph"
  1422. (let ((org-M-RET-may-split-line '((default . nil))))
  1423. (org-insert-heading))
  1424. (buffer-string))))
  1425. ;; At the beginning of a headline, create one above.
  1426. (should
  1427. (equal "* \n* H"
  1428. (org-test-with-temp-text "* H"
  1429. (org-insert-heading)
  1430. (buffer-string))))
  1431. ;; In the middle of a headline, split it if allowed.
  1432. (should
  1433. (equal "* H\n* 1"
  1434. (org-test-with-temp-text "* H<point>1"
  1435. (let ((org-M-RET-may-split-line '((headline . t))))
  1436. (org-insert-heading))
  1437. (buffer-string))))
  1438. (should
  1439. (equal "* H1\n* "
  1440. (org-test-with-temp-text "* H<point>1"
  1441. (let ((org-M-RET-may-split-line '((headline . nil))))
  1442. (org-insert-heading))
  1443. (buffer-string))))
  1444. ;; However, splitting cannot happen on TODO keywords, priorities or
  1445. ;; tags.
  1446. (should
  1447. (equal "* TODO H1\n* "
  1448. (org-test-with-temp-text "* TO<point>DO H1"
  1449. (let ((org-M-RET-may-split-line '((headline . t))))
  1450. (org-insert-heading))
  1451. (buffer-string))))
  1452. (should
  1453. (equal "* [#A] H1\n* "
  1454. (org-test-with-temp-text "* [#<point>A] H1"
  1455. (let ((org-M-RET-may-split-line '((headline . t))))
  1456. (org-insert-heading))
  1457. (buffer-string))))
  1458. (should
  1459. (equal "* H1 :tag:\n* "
  1460. (org-test-with-temp-text "* H1 :ta<point>g:"
  1461. (let ((org-M-RET-may-split-line '((headline . t))))
  1462. (org-insert-heading))
  1463. (buffer-string))))
  1464. ;; New headline level depends on the level of the headline above.
  1465. (should
  1466. (equal "** H\n** P"
  1467. (org-test-with-temp-text "** H\n<point>P"
  1468. (org-insert-heading)
  1469. (buffer-string))))
  1470. (should
  1471. (equal "** H\nPara\n** graph"
  1472. (org-test-with-temp-text "** H\nPara<point>graph"
  1473. (let ((org-M-RET-may-split-line '((default . t))))
  1474. (org-insert-heading))
  1475. (buffer-string))))
  1476. (should
  1477. (equal "** \n** H"
  1478. (org-test-with-temp-text "** H"
  1479. (org-insert-heading)
  1480. (buffer-string))))
  1481. ;; When called with one universal argument, insert a new headline at
  1482. ;; the end of the current subtree, independently on the position of
  1483. ;; point.
  1484. (should
  1485. (equal
  1486. "* H1\n** H2\n* "
  1487. (org-test-with-temp-text "* H1\n** H2"
  1488. (let ((org-insert-heading-respect-content nil))
  1489. (org-insert-heading '(4)))
  1490. (buffer-string))))
  1491. (should
  1492. (equal
  1493. "* H1\n** H2\n* "
  1494. (org-test-with-temp-text "* H<point>1\n** H2"
  1495. (let ((org-insert-heading-respect-content nil))
  1496. (org-insert-heading '(4)))
  1497. (buffer-string))))
  1498. ;; When called with two universal arguments, insert a new headline
  1499. ;; at the end of the grandparent subtree.
  1500. (should
  1501. (equal "* H1\n** H3\n- item\n** H2\n** "
  1502. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1503. (let ((org-insert-heading-respect-content nil))
  1504. (org-insert-heading '(16)))
  1505. (buffer-string))))
  1506. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1507. ;; a level 1 heading.
  1508. (should
  1509. (equal "* H1\n** H2\n* "
  1510. (org-test-with-temp-text "* H1\n** H2<point>"
  1511. (org-insert-heading nil nil t)
  1512. (buffer-string))))
  1513. (should
  1514. (equal "* H1\n- item\n* "
  1515. (org-test-with-temp-text "* H1\n- item<point>"
  1516. (org-insert-heading nil nil t)
  1517. (buffer-string))))
  1518. ;; Obey `org-blank-before-new-entry'.
  1519. (should
  1520. (equal "* H1\n\n* "
  1521. (org-test-with-temp-text "* H1<point>"
  1522. (let ((org-blank-before-new-entry '((heading . t))))
  1523. (org-insert-heading))
  1524. (buffer-string))))
  1525. (should
  1526. (equal "* H1\n* "
  1527. (org-test-with-temp-text "* H1<point>"
  1528. (let ((org-blank-before-new-entry '((heading . nil))))
  1529. (org-insert-heading))
  1530. (buffer-string))))
  1531. (should
  1532. (equal "* H1\n* H2\n* "
  1533. (org-test-with-temp-text "* H1\n* H2<point>"
  1534. (let ((org-blank-before-new-entry '((heading . auto))))
  1535. (org-insert-heading))
  1536. (buffer-string))))
  1537. (should
  1538. (equal "* H1\n\n* H2\n\n* "
  1539. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1540. (let ((org-blank-before-new-entry '((heading . auto))))
  1541. (org-insert-heading))
  1542. (buffer-string))))
  1543. ;; Corner case: correctly insert a headline after an empty one.
  1544. (should
  1545. (equal "* \n* "
  1546. (org-test-with-temp-text "* <point>"
  1547. (org-insert-heading)
  1548. (buffer-string))))
  1549. (should
  1550. (org-test-with-temp-text "* <point>\n"
  1551. (org-insert-heading)
  1552. (looking-at-p "\n\\'")))
  1553. ;; Do not insert spurious headlines when inserting a new headline.
  1554. (should
  1555. (equal "* H1\n* H2\n* \n"
  1556. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1557. (org-insert-heading)
  1558. (buffer-string))))
  1559. ;; Preserve visibility at beginning of line. In particular, when
  1560. ;; removing spurious blank lines, do not visually merge heading with
  1561. ;; the line visible above.
  1562. (should-not
  1563. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1564. (org-overview)
  1565. (let ((org-blank-before-new-entry '((heading . nil))))
  1566. (org-insert-heading '(4)))
  1567. (invisible-p (line-end-position 0))))
  1568. ;; Properly handle empty lines when forcing a headline below current
  1569. ;; one.
  1570. (should
  1571. (equal "* H1\n\n* H\n\n* "
  1572. (org-test-with-temp-text "* H1\n\n* H<point>"
  1573. (let ((org-blank-before-new-entry '((heading . t))))
  1574. (org-insert-heading '(4))
  1575. (buffer-string))))))
  1576. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1577. "Test `org-insert-todo-heading-respect-content' specifications."
  1578. ;; Create a TODO heading.
  1579. (should
  1580. (org-test-with-temp-text "* H1\n Body"
  1581. (org-insert-todo-heading-respect-content)
  1582. (nth 2 (org-heading-components))))
  1583. ;; Add headline at the end of the first subtree
  1584. (should
  1585. (equal
  1586. "* TODO "
  1587. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1588. (org-insert-todo-heading-respect-content)
  1589. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1590. ;; In a list, do not create a new item.
  1591. (should
  1592. (equal
  1593. "* TODO "
  1594. (org-test-with-temp-text "* H\n- an item\n- another one"
  1595. (search-forward "an ")
  1596. (org-insert-todo-heading-respect-content)
  1597. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1598. ;; Use the same TODO keyword as current heading.
  1599. (should
  1600. (equal
  1601. "* TODO \n"
  1602. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1603. (org-insert-todo-heading-respect-content)
  1604. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1605. (ert-deftest test-org/clone-with-time-shift ()
  1606. "Test `org-clone-subtree-with-time-shift'."
  1607. ;; Raise an error before first heading.
  1608. (should-error
  1609. (org-test-with-temp-text ""
  1610. (org-clone-subtree-with-time-shift 1)))
  1611. ;; Raise an error on invalid number of clones.
  1612. (should-error
  1613. (org-test-with-temp-text "* Clone me"
  1614. (org-clone-subtree-with-time-shift -1)))
  1615. ;; Clone non-repeating once.
  1616. (should
  1617. (equal "\
  1618. * H1\n<2015-06-21>
  1619. * H1\n<2015-06-23>
  1620. "
  1621. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1622. (org-clone-subtree-with-time-shift 1 "+2d")
  1623. (replace-regexp-in-string
  1624. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1625. nil nil 1))))
  1626. ;; Clone repeating once.
  1627. (should
  1628. (equal "\
  1629. * H1\n<2015-06-21>
  1630. * H1\n<2015-06-23>
  1631. * H1\n<2015-06-25 +1w>
  1632. "
  1633. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1634. (org-clone-subtree-with-time-shift 1 "+2d")
  1635. (replace-regexp-in-string
  1636. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1637. nil nil 1))))
  1638. ;; Clone repeating once in backward.
  1639. (should
  1640. (equal "\
  1641. * H1\n<2015-06-21>
  1642. * H1\n<2015-06-19>
  1643. * H1\n<2015-06-17 +1w>
  1644. "
  1645. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1646. (org-clone-subtree-with-time-shift 1 "-2d")
  1647. (replace-regexp-in-string
  1648. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1649. nil nil 1))))
  1650. ;; Clone non-repeating zero times.
  1651. (should
  1652. (equal "\
  1653. * H1\n<2015-06-21>
  1654. "
  1655. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1656. (org-clone-subtree-with-time-shift 0 "+2d")
  1657. (replace-regexp-in-string
  1658. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1659. nil nil 1))))
  1660. ;; Clone repeating "zero" times.
  1661. (should
  1662. (equal "\
  1663. * H1\n<2015-06-21>
  1664. * H1\n<2015-06-23 +1w>
  1665. "
  1666. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1667. (org-clone-subtree-with-time-shift 0 "+2d")
  1668. (replace-regexp-in-string
  1669. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1670. nil nil 1))))
  1671. ;; Clone with blank SHIFT argument.
  1672. (should
  1673. (string-prefix-p
  1674. "* H <2012-03-29"
  1675. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1676. (org-clone-subtree-with-time-shift 1 "")
  1677. (buffer-substring-no-properties (line-beginning-position 2)
  1678. (line-end-position 2)))))
  1679. ;; Find time stamps before point. If SHIFT is not specified, ask
  1680. ;; for a time shift.
  1681. (should
  1682. (string-prefix-p
  1683. "* H <2012-03-30"
  1684. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1685. (org-clone-subtree-with-time-shift 1 "+1d")
  1686. (buffer-substring-no-properties (line-beginning-position 2)
  1687. (line-end-position 2)))))
  1688. (should
  1689. (string-prefix-p
  1690. "* H <2014-03-05"
  1691. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1692. (cl-letf (((symbol-function 'read-from-minibuffer)
  1693. (lambda (&rest args) "+1d")))
  1694. (org-clone-subtree-with-time-shift 1))
  1695. (buffer-substring-no-properties (line-beginning-position 2)
  1696. (line-end-position 2))))))
  1697. ;;; Fixed-Width Areas
  1698. (ert-deftest test-org/toggle-fixed-width ()
  1699. "Test `org-toggle-fixed-width' specifications."
  1700. ;; No region: Toggle on fixed-width marker in paragraphs.
  1701. (should
  1702. (equal ": A"
  1703. (org-test-with-temp-text "A"
  1704. (org-toggle-fixed-width)
  1705. (buffer-string))))
  1706. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1707. (should
  1708. (equal "A"
  1709. (org-test-with-temp-text ": A"
  1710. (org-toggle-fixed-width)
  1711. (buffer-string))))
  1712. ;; No region: Toggle on marker in blank lines after elements or just
  1713. ;; after a headline.
  1714. (should
  1715. (equal "* H\n: "
  1716. (org-test-with-temp-text "* H\n"
  1717. (forward-line)
  1718. (org-toggle-fixed-width)
  1719. (buffer-string))))
  1720. (should
  1721. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1722. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1723. (goto-char (point-max))
  1724. (org-toggle-fixed-width)
  1725. (buffer-string))))
  1726. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1727. ;; headlines, clocks)
  1728. (should
  1729. (equal ": * Headline"
  1730. (org-test-with-temp-text "* Headline"
  1731. (org-toggle-fixed-width)
  1732. (buffer-string))))
  1733. (should
  1734. (equal ": #+KEYWORD: value"
  1735. (org-test-with-temp-text "#+KEYWORD: value"
  1736. (org-toggle-fixed-width)
  1737. (buffer-string))))
  1738. ;; No region: error in other situations.
  1739. (should-error
  1740. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1741. (forward-line)
  1742. (org-toggle-fixed-width)
  1743. (buffer-string)))
  1744. ;; No region: Indentation is preserved.
  1745. (should
  1746. (equal "- A\n : B"
  1747. (org-test-with-temp-text "- A\n B"
  1748. (forward-line)
  1749. (org-toggle-fixed-width)
  1750. (buffer-string))))
  1751. ;; Region: If it contains only fixed-width elements and blank lines,
  1752. ;; toggle off fixed-width markup.
  1753. (should
  1754. (equal "A\n\nB"
  1755. (org-test-with-temp-text ": A\n\n: B"
  1756. (transient-mark-mode 1)
  1757. (push-mark (point) t t)
  1758. (goto-char (point-max))
  1759. (org-toggle-fixed-width)
  1760. (buffer-string))))
  1761. ;; Region: If it contains anything else, toggle on fixed-width but
  1762. ;; not on fixed-width areas.
  1763. (should
  1764. (equal ": A\n: \n: B\n: \n: C"
  1765. (org-test-with-temp-text "A\n\n: B\n\nC"
  1766. (transient-mark-mode 1)
  1767. (push-mark (point) t t)
  1768. (goto-char (point-max))
  1769. (org-toggle-fixed-width)
  1770. (buffer-string))))
  1771. ;; Region: Ignore blank lines at its end, unless it contains only
  1772. ;; such lines.
  1773. (should
  1774. (equal ": A\n\n"
  1775. (org-test-with-temp-text "A\n\n"
  1776. (transient-mark-mode 1)
  1777. (push-mark (point) t t)
  1778. (goto-char (point-max))
  1779. (org-toggle-fixed-width)
  1780. (buffer-string))))
  1781. (should
  1782. (equal ": \n: \n"
  1783. (org-test-with-temp-text "\n\n"
  1784. (transient-mark-mode 1)
  1785. (push-mark (point) t t)
  1786. (goto-char (point-max))
  1787. (org-toggle-fixed-width)
  1788. (buffer-string)))))
  1789. (ert-deftest test-org/kill-line ()
  1790. "Test `org-kill-line' specifications."
  1791. ;; At the beginning of a line, kill whole line.
  1792. (should
  1793. (equal ""
  1794. (org-test-with-temp-text "abc"
  1795. (org-kill-line)
  1796. (buffer-string))))
  1797. ;; In the middle of a line, kill line until its end.
  1798. (should
  1799. (equal "a"
  1800. (org-test-with-temp-text "a<point>bc"
  1801. (org-kill-line)
  1802. (buffer-string))))
  1803. ;; Do not kill newline character.
  1804. (should
  1805. (equal "\n123"
  1806. (org-test-with-temp-text "abc\n123"
  1807. (org-kill-line)
  1808. (buffer-string))))
  1809. (should
  1810. (equal "a\n123"
  1811. (org-test-with-temp-text "a<point>bc\n123"
  1812. (org-kill-line)
  1813. (buffer-string))))
  1814. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  1815. ;; kill until tags.
  1816. (should
  1817. (equal "* A :tag:"
  1818. (org-test-with-temp-text "* A<point>B :tag:"
  1819. (let ((org-special-ctrl-k t)
  1820. (org-tags-column 0))
  1821. (org-kill-line))
  1822. (buffer-string))))
  1823. ;; If point is on tags, only kill part left until the end of line.
  1824. (should
  1825. (equal "* A :tag:"
  1826. (org-test-with-temp-text "* A :tag:<point>tag2:"
  1827. (let ((org-special-ctrl-k t)
  1828. (org-tags-column 0))
  1829. (org-kill-line))
  1830. (buffer-string))))
  1831. ;; However, if point is at the beginning of the line, kill whole
  1832. ;; headline.
  1833. (should
  1834. (equal ""
  1835. (org-test-with-temp-text "* AB :tag:"
  1836. (let ((org-special-ctrl-k t)
  1837. (org-tags-column 0))
  1838. (org-kill-line))
  1839. (buffer-string))))
  1840. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  1841. ;; invisible text, ask before removing it. When set to `error',
  1842. ;; throw an error.
  1843. (should-error
  1844. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1845. (org-overview)
  1846. (let ((org-special-ctrl-k nil)
  1847. (org-ctrl-k-protect-subtree t))
  1848. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  1849. (org-kill-line)))))
  1850. (should-error
  1851. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1852. (org-overview)
  1853. (let ((org-special-ctrl-k nil)
  1854. (org-ctrl-k-protect-subtree 'error))
  1855. (org-kill-line)))))
  1856. ;;; Headline
  1857. (ert-deftest test-org/get-heading ()
  1858. "Test `org-get-heading' specifications."
  1859. ;; Return current heading, even if point is not on it.
  1860. (should
  1861. (equal "H"
  1862. (org-test-with-temp-text "* H"
  1863. (org-get-heading))))
  1864. (should
  1865. (equal "H"
  1866. (org-test-with-temp-text "* H\nText<point>"
  1867. (org-get-heading))))
  1868. ;; Without any optional argument, return TODO keyword, priority
  1869. ;; cookie, COMMENT keyword and tags.
  1870. (should
  1871. (equal "TODO H"
  1872. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1873. (org-get-heading))))
  1874. (should
  1875. (equal "[#A] H"
  1876. (org-test-with-temp-text "* [#A] H"
  1877. (org-get-heading))))
  1878. (should
  1879. (equal "COMMENT H"
  1880. (org-test-with-temp-text "* COMMENT H"
  1881. (org-get-heading))))
  1882. (should
  1883. (equal "H :tag:"
  1884. (org-test-with-temp-text "* H :tag:"
  1885. (org-get-heading))))
  1886. ;; With NO-TAGS argument, ignore tags.
  1887. (should
  1888. (equal "TODO H"
  1889. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1890. (org-get-heading t))))
  1891. (should
  1892. (equal "H"
  1893. (org-test-with-temp-text "* H :tag:"
  1894. (org-get-heading t))))
  1895. ;; With NO-TODO, ignore TODO keyword.
  1896. (should
  1897. (equal "H"
  1898. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1899. (org-get-heading nil t))))
  1900. (should
  1901. (equal "H :tag:"
  1902. (org-test-with-temp-text "* H :tag:"
  1903. (org-get-heading nil t))))
  1904. ;; TODO keywords are case-sensitive.
  1905. (should
  1906. (equal "Todo H"
  1907. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1908. (org-get-heading nil t))))
  1909. ;; With NO-PRIORITY, ignore priority.
  1910. (should
  1911. (equal "H"
  1912. (org-test-with-temp-text "* [#A] H"
  1913. (org-get-heading nil nil t))))
  1914. (should
  1915. (equal "H"
  1916. (org-test-with-temp-text "* H"
  1917. (org-get-heading nil nil t))))
  1918. (should
  1919. (equal "TODO H"
  1920. (org-test-with-temp-text "* TODO [#A] H"
  1921. (org-get-heading nil nil t))))
  1922. ;; With NO-COMMENT, ignore COMMENT keyword.
  1923. (should
  1924. (equal "H"
  1925. (org-test-with-temp-text "* COMMENT H"
  1926. (org-get-heading nil nil nil t))))
  1927. (should
  1928. (equal "H"
  1929. (org-test-with-temp-text "* H"
  1930. (org-get-heading nil nil nil t))))
  1931. (should
  1932. (equal "TODO [#A] H"
  1933. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1934. (org-get-heading nil nil nil t))))
  1935. ;; On an empty headline, return value is consistent.
  1936. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1937. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1938. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1939. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1940. (should
  1941. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1942. (ert-deftest test-org/in-commented-heading-p ()
  1943. "Test `org-in-commented-heading-p' specifications."
  1944. ;; Commented headline.
  1945. (should
  1946. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1947. (goto-char (point-max))
  1948. (org-in-commented-heading-p)))
  1949. ;; Commented ancestor.
  1950. (should
  1951. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1952. (goto-char (point-max))
  1953. (org-in-commented-heading-p)))
  1954. ;; Comment keyword is case-sensitive.
  1955. (should-not
  1956. (org-test-with-temp-text "* Comment Headline\nBody"
  1957. (goto-char (point-max))
  1958. (org-in-commented-heading-p)))
  1959. ;; Keyword is standalone.
  1960. (should-not
  1961. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1962. (goto-char (point-max))
  1963. (org-in-commented-heading-p)))
  1964. ;; Optional argument.
  1965. (should-not
  1966. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1967. (goto-char (point-max))
  1968. (org-in-commented-heading-p t))))
  1969. (ert-deftest test-org/entry-blocked-p ()
  1970. ;; Check other dependencies.
  1971. (should
  1972. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1973. (let ((org-enforce-todo-dependencies t)
  1974. (org-blocker-hook
  1975. '(org-block-todo-from-children-or-siblings-or-parent)))
  1976. (org-entry-blocked-p))))
  1977. (should-not
  1978. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1979. (let ((org-enforce-todo-dependencies t)
  1980. (org-blocker-hook
  1981. '(org-block-todo-from-children-or-siblings-or-parent)))
  1982. (org-entry-blocked-p))))
  1983. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1984. ;; blocked.
  1985. (should-not
  1986. (org-test-with-temp-text "* Blocked\n** TODO one"
  1987. (let ((org-enforce-todo-dependencies t)
  1988. (org-blocker-hook
  1989. '(org-block-todo-from-children-or-siblings-or-parent)))
  1990. (org-entry-blocked-p))))
  1991. (should-not
  1992. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1993. (let ((org-enforce-todo-dependencies t)
  1994. (org-blocker-hook
  1995. '(org-block-todo-from-children-or-siblings-or-parent)))
  1996. (org-entry-blocked-p))))
  1997. ;; Follow :ORDERED: specifications.
  1998. (should
  1999. (org-test-with-temp-text
  2000. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  2001. (let ((org-enforce-todo-dependencies t)
  2002. (org-blocker-hook
  2003. '(org-block-todo-from-children-or-siblings-or-parent)))
  2004. (org-entry-blocked-p))))
  2005. (should-not
  2006. (org-test-with-temp-text
  2007. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  2008. (let ((org-enforce-todo-dependencies t)
  2009. (org-blocker-hook
  2010. '(org-block-todo-from-children-or-siblings-or-parent)))
  2011. (org-entry-blocked-p)))))
  2012. (ert-deftest test-org/get-outline-path ()
  2013. "Test `org-get-outline-path' specifications."
  2014. ;; Top-level headlines have no outline path.
  2015. (should-not
  2016. (org-test-with-temp-text "* H"
  2017. (org-get-outline-path)))
  2018. ;; Otherwise, outline path is the path leading to the headline.
  2019. (should
  2020. (equal '("H")
  2021. (org-test-with-temp-text "* H\n** S<point>"
  2022. (org-get-outline-path))))
  2023. ;; Find path even when point is not on a headline.
  2024. (should
  2025. (equal '("H")
  2026. (org-test-with-temp-text "* H\n** S\nText<point>"
  2027. (org-get-outline-path))))
  2028. ;; TODO keywords, tags and statistics cookies are ignored.
  2029. (should
  2030. (equal '("H")
  2031. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  2032. (org-get-outline-path))))
  2033. ;; Links are replaced with their description or their path.
  2034. (should
  2035. (equal '("Org")
  2036. (org-test-with-temp-text
  2037. "* [[https://orgmode.org][Org]]\n** S<point>"
  2038. (org-get-outline-path))))
  2039. (should
  2040. (equal '("https://orgmode.org")
  2041. (org-test-with-temp-text
  2042. "* [[https://orgmode.org]]\n** S<point>"
  2043. (org-get-outline-path))))
  2044. ;; When WITH-SELF is non-nil, include current heading.
  2045. (should
  2046. (equal '("H")
  2047. (org-test-with-temp-text "* H"
  2048. (org-get-outline-path t))))
  2049. (should
  2050. (equal '("H" "S")
  2051. (org-test-with-temp-text "* H\n** S\nText<point>"
  2052. (org-get-outline-path t))))
  2053. ;; Using cache is transparent to the user.
  2054. (should
  2055. (equal '("H")
  2056. (org-test-with-temp-text "* H\n** S<point>"
  2057. (setq org-outline-path-cache nil)
  2058. (org-get-outline-path nil t))))
  2059. ;; Do not corrupt cache when finding outline path in distant part of
  2060. ;; the buffer.
  2061. (should
  2062. (equal '("H2")
  2063. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  2064. (setq org-outline-path-cache nil)
  2065. (org-get-outline-path nil t)
  2066. (search-forward "S2")
  2067. (org-get-outline-path nil t))))
  2068. ;; Do not choke on empty headlines.
  2069. (should
  2070. (org-test-with-temp-text "* H\n** <point>"
  2071. (org-get-outline-path)))
  2072. (should
  2073. (org-test-with-temp-text "* \n** H<point>"
  2074. (org-get-outline-path))))
  2075. (ert-deftest test-org/format-outline-path ()
  2076. "Test `org-format-outline-path' specifications."
  2077. (should
  2078. (string= (org-format-outline-path (list "one" "two" "three"))
  2079. "one/two/three"))
  2080. ;; Empty path.
  2081. (should
  2082. (string= (org-format-outline-path '())
  2083. ""))
  2084. (should
  2085. (string= (org-format-outline-path '(nil))
  2086. ""))
  2087. ;; Empty path and prefix.
  2088. (should
  2089. (string= (org-format-outline-path '() nil ">>")
  2090. ">>"))
  2091. ;; Trailing whitespace in headings.
  2092. (should
  2093. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  2094. "one/tw o/three"))
  2095. ;; Non-default prefix and separators.
  2096. (should
  2097. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  2098. ">>|one|two|three"))
  2099. ;; Truncate.
  2100. (should
  2101. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  2102. "one/two/.."))
  2103. ;; Give a very narrow width.
  2104. (should
  2105. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  2106. "on"))
  2107. ;; Give a prefix that extends beyond the width.
  2108. (should
  2109. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  2110. ">>>>>>>>>>")
  2111. ">>>>>>>>..")))
  2112. (ert-deftest test-org/map-entries ()
  2113. "Test `org-map-entries' specifications."
  2114. ;; Full match.
  2115. (should
  2116. (equal '(1 11)
  2117. (org-test-with-temp-text "* Level 1\n** Level 2"
  2118. (org-map-entries #'point))))
  2119. ;; Level match.
  2120. (should
  2121. (equal '(1)
  2122. (org-test-with-temp-text "* Level 1\n** Level 2"
  2123. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  2124. (should
  2125. (equal '(11)
  2126. (org-test-with-temp-text "* Level 1\n** Level 2"
  2127. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  2128. ;; Tag match.
  2129. (should
  2130. (equal '(11)
  2131. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  2132. (org-map-entries #'point "yes"))))
  2133. (should
  2134. (equal '(14)
  2135. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  2136. (org-map-entries #'point "+yes-a"))))
  2137. (should
  2138. (equal '(11 23)
  2139. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  2140. (org-map-entries #'point "{yes?}"))))
  2141. ;; Priority match.
  2142. (should
  2143. (equal '(1)
  2144. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  2145. (org-map-entries #'point "PRIORITY=\"A\""))))
  2146. ;; Date match.
  2147. (should
  2148. (equal '(36)
  2149. (org-test-with-temp-text "
  2150. * H1
  2151. SCHEDULED: <2012-03-29 thu.>
  2152. * H2
  2153. SCHEDULED: <2014-03-04 tue.>"
  2154. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  2155. (should
  2156. (equal '(2)
  2157. (org-test-with-temp-text "
  2158. * H1
  2159. SCHEDULED: <2012-03-29 thu.>
  2160. * H2
  2161. SCHEDULED: <2014-03-04 tue.>"
  2162. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  2163. ;; Regular property match.
  2164. (should
  2165. (equal '(2)
  2166. (org-test-with-temp-text "
  2167. * H1
  2168. :PROPERTIES:
  2169. :TEST: 1
  2170. :END:
  2171. * H2
  2172. :PROPERTIES:
  2173. :TEST: 2
  2174. :END:"
  2175. (org-map-entries #'point "TEST=1"))))
  2176. ;; Multiple criteria.
  2177. (should
  2178. (equal '(23)
  2179. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  2180. (let (org-odd-levels-only
  2181. (org-use-tag-inheritance nil))
  2182. (org-map-entries #'point "yes+LEVEL=1")))))
  2183. ;; "or" criteria.
  2184. (should
  2185. (equal '(12 24)
  2186. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  2187. (let (org-odd-levels-only)
  2188. (org-map-entries #'point "LEVEL=2|no")))))
  2189. (should
  2190. (equal '(1 12)
  2191. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  2192. (let (org-odd-levels-only)
  2193. (org-map-entries #'point "yes|no")))))
  2194. ;; "and" criteria.
  2195. (should
  2196. (equal '(22)
  2197. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  2198. (let (org-odd-levels-only)
  2199. (org-map-entries #'point "yes&no"))))))
  2200. (ert-deftest test-org/edit-headline ()
  2201. "Test `org-edit-headline' specifications."
  2202. (should
  2203. (equal "* B"
  2204. (org-test-with-temp-text "* A"
  2205. (org-edit-headline "B")
  2206. (buffer-string))))
  2207. ;; Handle empty headings.
  2208. (should
  2209. (equal "* "
  2210. (org-test-with-temp-text "* A"
  2211. (org-edit-headline "")
  2212. (buffer-string))))
  2213. (should
  2214. (equal "* A"
  2215. (org-test-with-temp-text "* "
  2216. (org-edit-headline "A")
  2217. (buffer-string))))
  2218. ;; Handle TODO keywords and priority cookies.
  2219. (should
  2220. (equal "* TODO B"
  2221. (org-test-with-temp-text "* TODO A"
  2222. (org-edit-headline "B")
  2223. (buffer-string))))
  2224. (should
  2225. (equal "* [#A] B"
  2226. (org-test-with-temp-text "* [#A] A"
  2227. (org-edit-headline "B")
  2228. (buffer-string))))
  2229. (should
  2230. (equal "* TODO [#A] B"
  2231. (org-test-with-temp-text "* TODO [#A] A"
  2232. (org-edit-headline "B")
  2233. (buffer-string))))
  2234. ;; Handle tags.
  2235. (equal "* B :tag:"
  2236. (org-test-with-temp-text "* A :tag:"
  2237. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2238. (buffer-string))))
  2239. ;;; Keywords
  2240. (ert-deftest test-org/set-regexps-and-options ()
  2241. "Test `org-set-regexps-and-options' specifications."
  2242. ;; TAGS keyword.
  2243. (should
  2244. (equal '(("A"))
  2245. (let ((org-tag-alist '(("A")))
  2246. (org-tag-persistent-alist nil))
  2247. (org-test-with-temp-text ""
  2248. (org-mode-restart)
  2249. org-current-tag-alist))))
  2250. (should
  2251. (equal '(("B"))
  2252. (let ((org-tag-alist '(("A")))
  2253. (org-tag-persistent-alist nil))
  2254. (org-test-with-temp-text "#+TAGS: B"
  2255. (org-mode-restart)
  2256. org-current-tag-alist))))
  2257. (should
  2258. (equal '(("C") ("B"))
  2259. (let ((org-tag-alist '(("A")))
  2260. (org-tag-persistent-alist '(("C"))))
  2261. (org-test-with-temp-text "#+TAGS: B"
  2262. (org-mode-restart)
  2263. org-current-tag-alist))))
  2264. (should
  2265. (equal '(("B"))
  2266. (let ((org-tag-alist '(("A")))
  2267. (org-tag-persistent-alist '(("C"))))
  2268. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2269. (org-mode-restart)
  2270. org-current-tag-alist))))
  2271. (should
  2272. (equal '(("A" . ?a) ("B") ("C"))
  2273. (let ((org-tag-persistant-alist nil))
  2274. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2275. (org-mode-restart)
  2276. org-current-tag-alist))))
  2277. (should
  2278. (equal '(("A") (:newline) ("B"))
  2279. (let ((org-tag-persistent-alist nil))
  2280. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2281. (org-mode-restart)
  2282. org-current-tag-alist))))
  2283. (should
  2284. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2285. (let ((org-tag-persistent-alist nil))
  2286. (org-test-with-temp-text "#+TAGS: { A B } C"
  2287. (org-mode-restart)
  2288. org-current-tag-alist))))
  2289. (should
  2290. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2291. (let ((org-tag-persistent-alist nil))
  2292. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2293. (org-mode-restart)
  2294. org-current-tag-alist))))
  2295. (should
  2296. (equal '(("A" "B" "C"))
  2297. (let ((org-tag-persistent-alist nil))
  2298. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2299. (org-mode-restart)
  2300. org-tag-groups-alist))))
  2301. (should
  2302. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2303. (let ((org-tag-persistent-alist nil))
  2304. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2305. (org-mode-restart)
  2306. org-current-tag-alist))))
  2307. (should
  2308. (equal '(("A" "B" "C"))
  2309. (let ((org-tag-persistent-alist nil))
  2310. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2311. (org-mode-restart)
  2312. org-tag-groups-alist))))
  2313. ;; FILETAGS keyword.
  2314. (should
  2315. (equal '("A" "B" "C")
  2316. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2317. (org-mode-restart)
  2318. org-file-tags)))
  2319. ;; PROPERTY keyword. Property names are case-insensitive.
  2320. (should
  2321. (equal "foo=1"
  2322. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2323. (org-mode-restart)
  2324. (cdr (assoc "var" org-keyword-properties)))))
  2325. (should
  2326. (equal
  2327. "foo=1 bar=2"
  2328. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2329. (org-mode-restart)
  2330. (cdr (assoc "var" org-keyword-properties)))))
  2331. (should
  2332. (equal
  2333. "foo=1 bar=2"
  2334. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2335. (org-mode-restart)
  2336. (cdr (assoc "var" org-keyword-properties)))))
  2337. ;; ARCHIVE keyword.
  2338. (should
  2339. (equal "%s_done::"
  2340. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2341. (org-mode-restart)
  2342. org-archive-location)))
  2343. ;; CATEGORY keyword.
  2344. (should
  2345. (eq 'test
  2346. (org-test-with-temp-text "#+CATEGORY: test"
  2347. (org-mode-restart)
  2348. org-category)))
  2349. (should
  2350. (equal "test"
  2351. (org-test-with-temp-text "#+CATEGORY: test"
  2352. (org-mode-restart)
  2353. (cdr (assoc "CATEGORY" org-keyword-properties)))))
  2354. ;; COLUMNS keyword.
  2355. (should
  2356. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2357. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2358. (org-mode-restart)
  2359. org-columns-default-format)))
  2360. ;; CONSTANTS keyword. Constants names are case sensitive.
  2361. (should
  2362. (equal '("299792458." "3.14")
  2363. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2364. (org-mode-restart)
  2365. (mapcar
  2366. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2367. '("c" "pi")))))
  2368. (should
  2369. (equal "3.14"
  2370. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2371. (org-mode-restart)
  2372. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2373. (should
  2374. (equal "22/7"
  2375. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2376. (org-mode-restart)
  2377. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2378. ;; LINK keyword.
  2379. (should
  2380. (equal
  2381. '("url1" "url2")
  2382. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2383. (org-mode-restart)
  2384. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2385. '("a" "b")))))
  2386. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2387. (should
  2388. (equal
  2389. '(?X ?Z ?Y)
  2390. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2391. (org-mode-restart)
  2392. (list org-priority-highest org-priority-lowest org-priority-default))))
  2393. (should
  2394. (equal
  2395. '(?A ?C ?B)
  2396. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2397. (org-mode-restart)
  2398. (list org-priority-highest org-priority-lowest org-priority-default))))
  2399. ;; STARTUP keyword.
  2400. (should
  2401. (equal '(t t)
  2402. (org-test-with-temp-text "#+STARTUP: fold odd"
  2403. (org-mode-restart)
  2404. (list org-startup-folded org-odd-levels-only))))
  2405. ;; TODO keywords.
  2406. (should
  2407. (equal '(("A" "B") ("C"))
  2408. (org-test-with-temp-text "#+TODO: A B | C"
  2409. (org-mode-restart)
  2410. (list org-not-done-keywords org-done-keywords))))
  2411. (should
  2412. (equal '(("A" "C") ("B" "D"))
  2413. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2414. (org-mode-restart)
  2415. (list org-not-done-keywords org-done-keywords))))
  2416. (should
  2417. (equal '(("A" "B") ("C"))
  2418. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2419. (org-mode-restart)
  2420. (list org-not-done-keywords org-done-keywords))))
  2421. (should
  2422. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2423. (org-test-with-temp-text "#+TODO: A(a)"
  2424. (org-mode-restart)
  2425. org-todo-key-alist)))
  2426. (should
  2427. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2428. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2429. (org-mode-restart)
  2430. org-todo-log-states)))
  2431. ;; Enter SETUPFILE keyword.
  2432. (should
  2433. (equal "1"
  2434. (org-test-with-temp-text
  2435. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2436. (org-mode-restart)
  2437. (cdr (assoc "a" org-keyword-properties))))))
  2438. ;;; Links
  2439. ;;;; Coderefs
  2440. (ert-deftest test-org/coderef ()
  2441. "Test coderef links specifications."
  2442. (should
  2443. (org-test-with-temp-text "
  2444. #+BEGIN_SRC emacs-lisp
  2445. \(+ 1 1) (ref:sc)
  2446. #+END_SRC
  2447. \[[(sc)<point>]]"
  2448. (org-open-at-point)
  2449. (looking-at "(ref:sc)")))
  2450. ;; Find coderef even with alternate label format.
  2451. (should
  2452. (org-test-with-temp-text "
  2453. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2454. \(+ 1 1) {ref:sc}
  2455. #+END_SRC
  2456. \[[(sc)<point>]]"
  2457. (org-open-at-point)
  2458. (looking-at "{ref:sc}"))))
  2459. ;;;; Custom ID
  2460. (ert-deftest test-org/custom-id ()
  2461. "Test custom ID links specifications."
  2462. (should
  2463. (org-test-with-temp-text
  2464. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2465. (org-open-at-point)
  2466. (looking-at-p "\\* H1")))
  2467. ;; Handle escape characters.
  2468. (should
  2469. (org-test-with-temp-text
  2470. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#\\[%\\]<point>]]"
  2471. (org-open-at-point)
  2472. (looking-at-p "\\* H1")))
  2473. ;; Throw an error on false positives.
  2474. (should-error
  2475. (org-test-with-temp-text
  2476. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2477. (org-open-at-point)
  2478. (looking-at-p "\\* H1"))))
  2479. ;;;; Fuzzy Links
  2480. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2481. ;; a named element (#+name: text) and to headlines (* Text).
  2482. (ert-deftest test-org/fuzzy-links ()
  2483. "Test fuzzy links specifications."
  2484. ;; Fuzzy link goes in priority to a matching target.
  2485. (should
  2486. (org-test-with-temp-text
  2487. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2488. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2489. (looking-at "<<Test>>")))
  2490. ;; Then fuzzy link points to an element with a given name.
  2491. (should
  2492. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2493. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2494. (looking-at "#\\+NAME: Test")))
  2495. ;; A target still lead to a matching headline otherwise.
  2496. (should
  2497. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2498. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2499. (looking-at "\\* Head2")))
  2500. ;; With a leading star in link, enforce heading match.
  2501. (should
  2502. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2503. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2504. (looking-at "\\* Test")))
  2505. ;; With a leading star in link, enforce exact heading match, even
  2506. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2507. (should-error
  2508. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2509. (let ((org-link-search-must-match-exact-headline nil))
  2510. (org-open-at-point))))
  2511. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2512. (should
  2513. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2514. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2515. (looking-at "\\* Test")))
  2516. (should
  2517. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2518. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2519. (looking-at "\\* Test")))
  2520. ;; Heading match should not care about spaces, cookies, TODO
  2521. ;; keywords, priorities, and tags. However, TODO keywords are
  2522. ;; case-sensitive.
  2523. (should
  2524. (let ((first-line
  2525. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2526. (org-test-with-temp-text
  2527. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2528. (let ((org-link-search-must-match-exact-headline nil)
  2529. (org-todo-regexp "TODO"))
  2530. (org-open-at-point))
  2531. (looking-at (regexp-quote first-line)))))
  2532. (should-error
  2533. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2534. (let ((org-link-search-must-match-exact-headline nil)
  2535. (org-todo-regexp "TODO"))
  2536. (org-open-at-point))))
  2537. ;; Heading match should still be exact.
  2538. (should-error
  2539. (org-test-with-temp-text "
  2540. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2541. Foo Bar
  2542. <point>[[*Test 1]]"
  2543. (let ((org-link-search-must-match-exact-headline nil)
  2544. (org-todo-regexp "TODO"))
  2545. (org-open-at-point))))
  2546. (should
  2547. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2548. (let ((org-link-search-must-match-exact-headline nil)
  2549. (org-todo-regexp "TODO"))
  2550. (org-open-at-point))
  2551. (looking-at-p (regexp-quote "** Test 1 2"))))
  2552. ;; Heading match ignores COMMENT keyword.
  2553. (should
  2554. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2555. (org-open-at-point)
  2556. (looking-at "\\* COMMENT Test")))
  2557. (should
  2558. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2559. (org-open-at-point)
  2560. (looking-at "\\* TODO COMMENT Test")))
  2561. ;; Correctly un-escape fuzzy links.
  2562. (should
  2563. (org-test-with-temp-text "* [foo]\n[[*\\[foo\\]][With escaped characters]]"
  2564. (org-open-at-point)
  2565. (bobp)))
  2566. ;; Match search strings containing newline characters, including
  2567. ;; blank lines.
  2568. (should
  2569. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2570. (let ((file (buffer-file-name)))
  2571. (goto-char (point-max))
  2572. (insert (format "[[file:%s::line1 line2]]" file))
  2573. (beginning-of-line)
  2574. (let ((org-link-search-must-match-exact-headline nil))
  2575. (org-open-at-point 0))
  2576. (looking-at-p "line1"))))
  2577. (should
  2578. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2579. (let ((file (buffer-file-name)))
  2580. (goto-char (point-max))
  2581. (insert (format "[[file:%s::line1 line2]]" file))
  2582. (beginning-of-line)
  2583. (let ((org-link-search-must-match-exact-headline nil))
  2584. (org-open-at-point 0))
  2585. (looking-at-p "line1")))))
  2586. ;;;; Open at point
  2587. (ert-deftest test-org/open-at-point/keyword ()
  2588. "Does `org-open-at-point' open link in a keyword line?"
  2589. (should
  2590. (org-test-with-temp-text
  2591. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2592. (org-open-at-point) t))
  2593. (should
  2594. (org-test-with-temp-text
  2595. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2596. (org-open-at-point) t)))
  2597. (ert-deftest test-org/open-at-point/property ()
  2598. "Does `org-open-at-point' open link in property drawer?"
  2599. (should
  2600. (org-test-with-temp-text
  2601. "* Headline
  2602. :PROPERTIES:
  2603. :URL: <point>[[*Headline]]
  2604. :END:"
  2605. (org-open-at-point) t)))
  2606. (ert-deftest test-org/open-at-point/comment ()
  2607. "Does `org-open-at-point' open link in a commented line?"
  2608. (should
  2609. (org-test-with-temp-text
  2610. "<<top>>\n# <point>[[top]]"
  2611. (org-open-at-point) t))
  2612. (should
  2613. (org-test-with-temp-text
  2614. "* H\n<<top>>\n# <point>[[top]]"
  2615. (org-open-at-point) t)))
  2616. (ert-deftest test-org/open-at-point/inline-image ()
  2617. "Test `org-open-at-point' on nested links."
  2618. (should
  2619. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2620. (org-open-at-point)
  2621. (bobp))))
  2622. (ert-deftest test-org/open-at-point/radio-target ()
  2623. "Test `org-open-at-point' on radio targets."
  2624. (should
  2625. (org-test-with-temp-text "<<<target>>> <point>target"
  2626. (org-update-radio-target-regexp)
  2627. (org-open-at-point)
  2628. (eq (org-element-type (org-element-context)) 'radio-target))))
  2629. (ert-deftest test-org/open-at-point/tag ()
  2630. "Test `org-open-at-point' on tags."
  2631. (should
  2632. (org-test-with-temp-text "* H :<point>tag:"
  2633. (catch :result
  2634. (cl-letf (((symbol-function 'org-tags-view)
  2635. (lambda (&rest args) (throw :result t))))
  2636. (org-open-at-point)
  2637. nil))))
  2638. (should-not
  2639. (org-test-with-temp-text-in-file "* H<point> :tag:"
  2640. (catch :result
  2641. (cl-letf (((symbol-function 'org-tags-view)
  2642. (lambda (&rest args) (throw :result t))))
  2643. ;; When point isn't on a tag it's going to try other things,
  2644. ;; possibly trying to open attachments which will return an
  2645. ;; error if there isn't an attachment. Suppress that error.
  2646. (ignore-errors
  2647. (org-open-at-point))
  2648. nil)))))
  2649. ;;; Node Properties
  2650. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2651. "Ensure properties accumulate in subtree drawers."
  2652. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2653. (org-babel-next-src-block)
  2654. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2655. (ert-deftest test-org/custom-properties ()
  2656. "Test custom properties specifications."
  2657. ;; Standard test.
  2658. (should
  2659. (let ((org-custom-properties '("FOO")))
  2660. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2661. (org-toggle-custom-properties-visibility)
  2662. (org-invisible-p2))))
  2663. ;; Properties are case-insensitive.
  2664. (should
  2665. (let ((org-custom-properties '("FOO")))
  2666. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2667. (org-toggle-custom-properties-visibility)
  2668. (org-invisible-p2))))
  2669. (should
  2670. (let ((org-custom-properties '("foo")))
  2671. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2672. (org-toggle-custom-properties-visibility)
  2673. (org-invisible-p2))))
  2674. ;; Multiple custom properties in the same drawer.
  2675. (should
  2676. (let ((org-custom-properties '("FOO" "BAR")))
  2677. (org-test-with-temp-text
  2678. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2679. (org-toggle-custom-properties-visibility)
  2680. (and (org-invisible-p2)
  2681. (not (progn (forward-line) (org-invisible-p2)))
  2682. (progn (forward-line) (org-invisible-p2))))))
  2683. ;; Hide custom properties with an empty value.
  2684. (should
  2685. (let ((org-custom-properties '("FOO")))
  2686. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2687. (org-toggle-custom-properties-visibility)
  2688. (org-invisible-p2))))
  2689. ;; Do not hide fake properties.
  2690. (should-not
  2691. (let ((org-custom-properties '("FOO")))
  2692. (org-test-with-temp-text ":FOO: val\n"
  2693. (org-toggle-custom-properties-visibility)
  2694. (org-invisible-p2))))
  2695. (should-not
  2696. (let ((org-custom-properties '("A")))
  2697. (org-test-with-temp-text
  2698. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2699. (org-toggle-custom-properties-visibility)
  2700. (org-invisible-p2)))))
  2701. ;;; Mark Region
  2702. (ert-deftest test-org/mark-subtree ()
  2703. "Test `org-mark-subtree' specifications."
  2704. ;; Error when point is before first headline.
  2705. (should-error
  2706. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2707. (progn (transient-mark-mode 1)
  2708. (org-mark-subtree))))
  2709. ;; Without argument, mark current subtree.
  2710. (should
  2711. (equal
  2712. '(12 32)
  2713. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2714. (progn (transient-mark-mode 1)
  2715. (forward-line 2)
  2716. (org-mark-subtree)
  2717. (list (region-beginning) (region-end))))))
  2718. ;; With an argument, move ARG up.
  2719. (should
  2720. (equal
  2721. '(1 32)
  2722. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2723. (progn (transient-mark-mode 1)
  2724. (forward-line 2)
  2725. (org-mark-subtree 1)
  2726. (list (region-beginning) (region-end))))))
  2727. ;; Do not get fooled by inlinetasks.
  2728. (when (featurep 'org-inlinetask)
  2729. (should
  2730. (= 1
  2731. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2732. (progn (transient-mark-mode 1)
  2733. (forward-line 1)
  2734. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2735. (region-beginning)))))))
  2736. ;;; Miscellaneous
  2737. (ert-deftest test-org/sort-entries ()
  2738. "Test `org-sort-entries'."
  2739. ;; Sort alphabetically.
  2740. (should
  2741. (equal "\n* abc\n* def\n* xyz\n"
  2742. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2743. (org-sort-entries nil ?a)
  2744. (buffer-string))))
  2745. (should
  2746. (equal "\n* xyz\n* def\n* abc\n"
  2747. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2748. (org-sort-entries nil ?A)
  2749. (buffer-string))))
  2750. (should
  2751. (equal "\n* \n* klm\n* xyz\n"
  2752. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  2753. (org-sort-entries nil ?a)
  2754. (buffer-string))))
  2755. ;; Sort numerically.
  2756. (should
  2757. (equal "\n* 1\n* 2\n* 10\n"
  2758. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2759. (org-sort-entries nil ?n)
  2760. (buffer-string))))
  2761. (should
  2762. (equal "\n* 10\n* 2\n* 1\n"
  2763. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2764. (org-sort-entries nil ?N)
  2765. (buffer-string))))
  2766. (should
  2767. (equal "\n* \n* 1\n* 2\n"
  2768. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  2769. (org-sort-entries nil ?n)
  2770. (buffer-string))))
  2771. ;; Sort by custom function.
  2772. (should
  2773. (equal "\n* b\n* aa\n* ccc\n"
  2774. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2775. (org-sort-entries nil ?f
  2776. (lambda ()
  2777. (length (buffer-substring (point-at-bol)
  2778. (point-at-eol))))
  2779. #'<)
  2780. (buffer-string))))
  2781. (should
  2782. (equal "\n* ccc\n* aa\n* b\n"
  2783. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2784. (org-sort-entries nil ?F
  2785. (lambda ()
  2786. (length (buffer-substring (point-at-bol)
  2787. (point-at-eol))))
  2788. #'<)
  2789. (buffer-string))))
  2790. ;; Sort by TODO keyword.
  2791. (should
  2792. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2793. (org-test-with-temp-text
  2794. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2795. (org-sort-entries nil ?o)
  2796. (buffer-string))))
  2797. (should
  2798. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2799. (org-test-with-temp-text
  2800. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2801. (org-sort-entries nil ?O)
  2802. (buffer-string))))
  2803. ;; Sort by priority.
  2804. (should
  2805. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2806. (org-test-with-temp-text
  2807. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2808. (org-sort-entries nil ?p)
  2809. (buffer-string))))
  2810. (should
  2811. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2812. (org-test-with-temp-text
  2813. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2814. (org-sort-entries nil ?P)
  2815. (buffer-string))))
  2816. ;; Sort by creation time.
  2817. (should
  2818. (equal "
  2819. * h3
  2820. [2017-05-08 Mon]
  2821. * h2
  2822. [2017-05-09 Tue]
  2823. * h1
  2824. [2018-05-09 Wed]
  2825. "
  2826. (org-test-with-temp-text
  2827. "
  2828. * h1
  2829. [2018-05-09 Wed]
  2830. * h2
  2831. [2017-05-09 Tue]
  2832. * h3
  2833. [2017-05-08 Mon]
  2834. "
  2835. (org-sort-entries nil ?c)
  2836. (buffer-string))))
  2837. ;; Sort by scheduled date.
  2838. (should
  2839. (equal "
  2840. * TODO h4
  2841. SCHEDULED: <2017-05-06 Sat>
  2842. * TODO h3
  2843. SCHEDULED: <2017-05-08 Mon>
  2844. * TODO h2
  2845. DEADLINE: <2017-05-09 Tue>
  2846. * TODO h1
  2847. DEADLINE: <2017-05-07 Sun>
  2848. "
  2849. (org-test-with-temp-text
  2850. "
  2851. * TODO h2
  2852. DEADLINE: <2017-05-09 Tue>
  2853. * TODO h1
  2854. DEADLINE: <2017-05-07 Sun>
  2855. * TODO h3
  2856. SCHEDULED: <2017-05-08 Mon>
  2857. * TODO h4
  2858. SCHEDULED: <2017-05-06 Sat>
  2859. "
  2860. (org-sort-entries nil ?s)
  2861. (buffer-string))))
  2862. ;; Sort by deadline date.
  2863. (should
  2864. (equal "
  2865. * TODO h1
  2866. DEADLINE: <2017-05-07 Sun>
  2867. * TODO h2
  2868. DEADLINE: <2017-05-09 Tue>
  2869. * TODO h3
  2870. SCHEDULED: <2017-05-08 Mon>
  2871. * TODO h4
  2872. SCHEDULED: <2017-05-06 Sat>
  2873. "
  2874. (org-test-with-temp-text
  2875. "
  2876. * TODO h2
  2877. DEADLINE: <2017-05-09 Tue>
  2878. * TODO h1
  2879. DEADLINE: <2017-05-07 Sun>
  2880. * TODO h3
  2881. SCHEDULED: <2017-05-08 Mon>
  2882. * TODO h4
  2883. SCHEDULED: <2017-05-06 Sat>
  2884. "
  2885. (org-sort-entries nil ?d)
  2886. (buffer-string))))
  2887. ;; Sort by any date/time
  2888. (should
  2889. (equal "
  2890. * TODO h4
  2891. SCHEDULED: <2017-05-06 Sat>
  2892. * TODO h1
  2893. DEADLINE: <2017-05-07 Sun>
  2894. * TODO h3
  2895. SCHEDULED: <2017-05-08 Mon>
  2896. * TODO h2
  2897. DEADLINE: <2017-05-09 Tue>
  2898. "
  2899. (org-test-with-temp-text
  2900. "
  2901. * TODO h2
  2902. DEADLINE: <2017-05-09 Tue>
  2903. * TODO h1
  2904. DEADLINE: <2017-05-07 Sun>
  2905. * TODO h3
  2906. SCHEDULED: <2017-05-08 Mon>
  2907. * TODO h4
  2908. SCHEDULED: <2017-05-06 Sat>
  2909. "
  2910. (org-sort-entries nil ?t)
  2911. (buffer-string))))
  2912. ;; Sort by clocking time.
  2913. (should
  2914. (equal "
  2915. * clocked h2
  2916. :LOGBOOK:
  2917. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2918. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2919. :END:
  2920. * clocked h1
  2921. :LOGBOOK:
  2922. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2923. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2924. :END:
  2925. "
  2926. (org-test-with-temp-text
  2927. "
  2928. * clocked h1
  2929. :LOGBOOK:
  2930. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2931. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2932. :END:
  2933. * clocked h2
  2934. :LOGBOOK:
  2935. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2936. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2937. :END:
  2938. "
  2939. (org-sort-entries nil ?k)
  2940. (buffer-string))))
  2941. ;; Preserve file local variables when sorting.
  2942. (should
  2943. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  2944. (org-test-with-temp-text
  2945. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  2946. (org-sort-entries nil ?a)
  2947. (buffer-string)))))
  2948. (ert-deftest test-org/string-collate-greaterp ()
  2949. "Test `org-string-collate-greaterp' specifications."
  2950. (should (org-string-collate-greaterp "def" "abc"))
  2951. (should-not (org-string-collate-greaterp "abc" "def")))
  2952. (ert-deftest test-org/file-contents ()
  2953. "Test `org-file-contents' specifications."
  2954. ;; Open files.
  2955. (should
  2956. (string= "#+BIND: variable value
  2957. #+DESCRIPTION: l2
  2958. #+LANGUAGE: en
  2959. #+SELECT_TAGS: b
  2960. #+TITLE: b
  2961. #+PROPERTY: a 1
  2962. " (org-file-contents (expand-file-name "setupfile3.org"
  2963. (concat org-test-dir "examples/")))))
  2964. ;; Throw error when trying to access an invalid file.
  2965. (should-error (org-file-contents "this-file-must-not-exist"))
  2966. ;; Try to access an invalid file, but do not throw an error.
  2967. (should
  2968. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  2969. ;; Open URL.
  2970. (should
  2971. (string= "foo"
  2972. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2973. (unwind-protect
  2974. ;; Simulate successful retrieval of a URL.
  2975. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2976. (lambda (&rest_)
  2977. (with-current-buffer buffer
  2978. (insert "HTTP/1.1 200 OK\n\nfoo"))
  2979. buffer)))
  2980. (org-file-contents "http://some-valid-url"))
  2981. (kill-buffer buffer)))))
  2982. ;; Throw error when trying to access an invalid URL.
  2983. (should-error
  2984. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2985. (unwind-protect
  2986. ;; Simulate unsuccessful retrieval of a URL.
  2987. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2988. (lambda (&rest_)
  2989. (with-current-buffer buffer
  2990. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2991. buffer)))
  2992. (org-file-contents "http://this-url-must-not-exist"))
  2993. (kill-buffer buffer))))
  2994. ;; Try to access an invalid URL, but do not throw an error.
  2995. (should-error
  2996. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2997. (unwind-protect
  2998. ;; Simulate unsuccessful retrieval of a URL.
  2999. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3000. (lambda (&rest_)
  3001. (with-current-buffer buffer
  3002. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3003. buffer)))
  3004. (org-file-contents "http://this-url-must-not-exist"))
  3005. (kill-buffer buffer))))
  3006. (should
  3007. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3008. (unwind-protect
  3009. ;; Simulate unsuccessful retrieval of a URL.
  3010. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3011. (lambda (&rest_)
  3012. (with-current-buffer buffer
  3013. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3014. buffer)))
  3015. (org-file-contents "http://this-url-must-not-exist" :noerror))
  3016. (kill-buffer buffer))
  3017. t)))
  3018. ;;; Navigation
  3019. (ert-deftest test-org/forward-heading-same-level ()
  3020. "Test `org-forward-heading-same-level' specifications."
  3021. ;; Test navigation at top level, forward and backward.
  3022. (should
  3023. (equal "* H2"
  3024. (org-test-with-temp-text "* H1\n* H2"
  3025. (org-forward-heading-same-level 1)
  3026. (buffer-substring-no-properties (point) (line-end-position)))))
  3027. (should
  3028. (equal "* H1"
  3029. (org-test-with-temp-text "* H1\n<point>* H2"
  3030. (org-forward-heading-same-level -1)
  3031. (buffer-substring-no-properties (point) (line-end-position)))))
  3032. ;; Test navigation in a sub-tree, forward and backward.
  3033. (should
  3034. (equal "* H2"
  3035. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3036. (org-forward-heading-same-level 1)
  3037. (buffer-substring-no-properties (point) (line-end-position)))))
  3038. (should
  3039. (equal "* H1"
  3040. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3041. (org-forward-heading-same-level -1)
  3042. (buffer-substring-no-properties (point) (line-end-position)))))
  3043. ;; Stop at first or last sub-heading.
  3044. (should-not
  3045. (equal "* H2"
  3046. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3047. (org-forward-heading-same-level 1)
  3048. (buffer-substring-no-properties (point) (line-end-position)))))
  3049. (should-not
  3050. (equal "* H2"
  3051. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3052. (org-forward-heading-same-level -1)
  3053. (buffer-substring-no-properties (point) (line-end-position)))))
  3054. ;; Allow multiple moves.
  3055. (should
  3056. (equal "* H3"
  3057. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3058. (org-forward-heading-same-level 2)
  3059. (buffer-substring-no-properties (point) (line-end-position)))))
  3060. (should
  3061. (equal "* H1"
  3062. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3063. (org-forward-heading-same-level -2)
  3064. (buffer-substring-no-properties (point) (line-end-position)))))
  3065. ;; Ignore spurious moves when first (or last) sibling is reached.
  3066. (should
  3067. (equal "** H3"
  3068. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3069. (org-forward-heading-same-level 100)
  3070. (buffer-substring-no-properties (point) (line-end-position)))))
  3071. (should
  3072. (equal "** H1"
  3073. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3074. (org-forward-heading-same-level -100)
  3075. (buffer-substring-no-properties (point) (line-end-position))))))
  3076. (ert-deftest test-org/end-of-meta-data ()
  3077. "Test `org-end-of-meta-data' specifications."
  3078. ;; Skip planning line.
  3079. (should
  3080. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3081. (org-end-of-meta-data)
  3082. (eobp)))
  3083. ;; Skip properties drawer.
  3084. (should
  3085. (org-test-with-temp-text
  3086. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3087. (org-end-of-meta-data)
  3088. (eobp)))
  3089. ;; Skip both.
  3090. (should
  3091. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3092. (org-end-of-meta-data)
  3093. (eobp)))
  3094. ;; Nothing to skip, go to first line.
  3095. (should
  3096. (org-test-with-temp-text "* Headline\nContents"
  3097. (org-end-of-meta-data)
  3098. (looking-at "Contents")))
  3099. ;; With option argument, skip empty lines, regular drawers and
  3100. ;; clocking lines.
  3101. (should
  3102. (org-test-with-temp-text "* Headline\n\nContents"
  3103. (org-end-of-meta-data t)
  3104. (looking-at "Contents")))
  3105. (should
  3106. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3107. (org-end-of-meta-data t)
  3108. (looking-at "Contents")))
  3109. (should
  3110. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3111. (org-end-of-meta-data t)
  3112. (looking-at "Contents")))
  3113. ;; Special case: do not skip incomplete drawers.
  3114. (should
  3115. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3116. (org-end-of-meta-data t)
  3117. (looking-at ":LOGBOOK:")))
  3118. ;; Special case: Be careful about consecutive headlines.
  3119. (should-not
  3120. (org-test-with-temp-text "* H1\n*H2\nContents"
  3121. (org-end-of-meta-data t)
  3122. (looking-at "Contents"))))
  3123. (ert-deftest test-org/beginning-of-line ()
  3124. "Test `org-beginning-of-line' specifications."
  3125. ;; Move to beginning of line. If current line in invisible, move to
  3126. ;; beginning of visible line instead.
  3127. (should
  3128. (org-test-with-temp-text "Some text\nSome other text<point>"
  3129. (org-beginning-of-line)
  3130. (bolp)))
  3131. (should
  3132. (org-test-with-temp-text "* H1\n** H2<point>"
  3133. (org-overview)
  3134. (org-beginning-of-line)
  3135. (= (line-beginning-position) 1)))
  3136. ;; With `visual-line-mode' active, move to beginning of visual line.
  3137. (should-not
  3138. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3139. (visual-line-mode)
  3140. (dotimes (i 1000) (insert "very "))
  3141. (org-beginning-of-line)
  3142. (bolp)))
  3143. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3144. ;; beginning of a visual line than to the logical beginning of line,
  3145. ;; even if special movement is active.
  3146. (should-not
  3147. (org-test-with-temp-text "* A <point>long headline"
  3148. (visual-line-mode)
  3149. (dotimes (i 1000) (insert "very "))
  3150. (goto-char (point-max))
  3151. (org-beginning-of-line)
  3152. (bobp)))
  3153. (should-not
  3154. (org-test-with-temp-text "* A <point>long headline"
  3155. (visual-line-mode)
  3156. (dotimes (i 1000) (insert "very "))
  3157. (goto-char (point-max))
  3158. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3159. (bobp)))
  3160. ;; At an headline with special movement, first move at beginning of
  3161. ;; title, then at the beginning of line, rinse, repeat.
  3162. (should
  3163. (org-test-with-temp-text "* TODO Headline<point>"
  3164. (let ((org-special-ctrl-a/e t))
  3165. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3166. (progn (org-beginning-of-line) (bolp))
  3167. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3168. (should
  3169. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3170. (let ((org-special-ctrl-a/e t))
  3171. (org-beginning-of-line)
  3172. (looking-at "Headline"))))
  3173. (should
  3174. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3175. (let ((org-special-ctrl-a/e '(t . nil)))
  3176. (org-beginning-of-line)
  3177. (looking-at "Headline"))))
  3178. (should-not
  3179. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3180. (let ((org-special-ctrl-a/e '(nil . nil)))
  3181. (org-beginning-of-line)
  3182. (looking-at "Headline"))))
  3183. ;; At an headline with reversed movement, first move to beginning of
  3184. ;; line, then to the beginning of title.
  3185. (should
  3186. (org-test-with-temp-text "* TODO Headline<point>"
  3187. (let ((org-special-ctrl-a/e 'reversed)
  3188. (this-command last-command))
  3189. (and (progn (org-beginning-of-line) (bolp))
  3190. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3191. (should
  3192. (org-test-with-temp-text "* TODO Headline<point>"
  3193. (let ((org-special-ctrl-a/e '(reversed . nil))
  3194. (this-command last-command))
  3195. (and (progn (org-beginning-of-line) (bolp))
  3196. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3197. (should-not
  3198. (org-test-with-temp-text "* TODO Headline<point>"
  3199. (let ((org-special-ctrl-a/e '(t . nil))
  3200. (this-command last-command))
  3201. (and (progn (org-beginning-of-line) (bolp))
  3202. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3203. ;; At an item with special movement, first move after to beginning
  3204. ;; of title, then to the beginning of line, rinse, repeat.
  3205. (should
  3206. (org-test-with-temp-text "- [ ] Item<point>"
  3207. (let ((org-special-ctrl-a/e t))
  3208. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3209. (progn (org-beginning-of-line) (bolp))
  3210. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3211. ;; At an item with reversed movement, first move to beginning of
  3212. ;; line, then to the beginning of title.
  3213. (should
  3214. (org-test-with-temp-text "- [X] Item<point>"
  3215. (let ((org-special-ctrl-a/e 'reversed)
  3216. (this-command last-command))
  3217. (and (progn (org-beginning-of-line) (bolp))
  3218. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3219. ;; Leave point before invisible characters at column 0.
  3220. (should
  3221. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3222. (let ((org-special-ctrl-a/e nil))
  3223. (org-beginning-of-line)
  3224. (bolp))))
  3225. (should
  3226. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3227. (let ((org-special-ctrl-a/e t))
  3228. (org-beginning-of-line)
  3229. (bolp))))
  3230. (should
  3231. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3232. (visual-line-mode)
  3233. (org-beginning-of-line)
  3234. (bolp)))
  3235. ;; Special case: Do not error when the buffer contains only a single
  3236. ;; asterisk.
  3237. (should
  3238. (org-test-with-temp-text "*<point>"
  3239. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3240. (should
  3241. (org-test-with-temp-text "*<point>"
  3242. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3243. (ert-deftest test-org/end-of-line ()
  3244. "Test `org-end-of-line' specifications."
  3245. ;; Standard test.
  3246. (should
  3247. (org-test-with-temp-text "Some text\nSome other text"
  3248. (org-end-of-line)
  3249. (eolp)))
  3250. ;; With `visual-line-mode' active, move to end of visible line.
  3251. ;; However, never go past ellipsis.
  3252. (should-not
  3253. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3254. (visual-line-mode)
  3255. (dotimes (i 1000) (insert "very "))
  3256. (goto-char (point-min))
  3257. (org-end-of-line)
  3258. (eolp)))
  3259. (should-not
  3260. (org-test-with-temp-text "* A short headline\nSome contents"
  3261. (visual-line-mode)
  3262. (org-overview)
  3263. (org-end-of-line)
  3264. (eobp)))
  3265. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3266. ;; of visible line if tags, or end of line, are farther.
  3267. (should-not
  3268. (org-test-with-temp-text "* A <point>long headline"
  3269. (visual-line-mode)
  3270. (dotimes (i 1000) (insert "very "))
  3271. (goto-char (point-min))
  3272. (org-end-of-line)
  3273. (eolp)))
  3274. (should-not
  3275. (org-test-with-temp-text "* A <point>long headline :tag:"
  3276. (visual-line-mode)
  3277. (dotimes (i 1000) (insert "very "))
  3278. (goto-char (point-min))
  3279. (org-end-of-line)
  3280. (eolp)))
  3281. ;; At an headline without special movement, go to end of line.
  3282. ;; However, never go past ellipsis.
  3283. (should
  3284. (org-test-with-temp-text "* Headline2b :tag:\n"
  3285. (let ((org-special-ctrl-a/e nil))
  3286. (and (progn (org-end-of-line) (eolp))
  3287. (progn (org-end-of-line) (eolp))))))
  3288. (should
  3289. (org-test-with-temp-text "* Headline2b :tag:\n"
  3290. (let ((org-special-ctrl-a/e '(t . nil)))
  3291. (and (progn (org-end-of-line) (eolp))
  3292. (progn (org-end-of-line) (eolp))))))
  3293. (should
  3294. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3295. (org-overview)
  3296. (let ((org-special-ctrl-a/e nil))
  3297. (org-end-of-line)
  3298. (= 1 (line-beginning-position)))))
  3299. ;; At an headline with special movement, first move before tags,
  3300. ;; then at the end of line, rinse, repeat. However, never go past
  3301. ;; ellipsis.
  3302. (should
  3303. (org-test-with-temp-text "* Headline1 :tag:\n"
  3304. (let ((org-special-ctrl-a/e t))
  3305. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3306. (progn (org-end-of-line) (eolp))
  3307. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3308. (should
  3309. (org-test-with-temp-text "* Headline1 :tag:\n"
  3310. (let ((org-special-ctrl-a/e '(nil . t)))
  3311. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3312. (progn (org-end-of-line) (eolp))
  3313. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3314. (should-not
  3315. (org-test-with-temp-text "* Headline1 :tag:\n"
  3316. (let ((org-special-ctrl-a/e '(nil . nil)))
  3317. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3318. (progn (org-end-of-line) (eolp))
  3319. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3320. (should
  3321. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3322. (org-overview)
  3323. (let ((org-special-ctrl-a/e t))
  3324. (org-end-of-line)
  3325. (org-end-of-line)
  3326. (= 1 (line-beginning-position)))))
  3327. ;; At an headline, with reversed movement, first go to end of line,
  3328. ;; then before tags. However, never go past ellipsis.
  3329. (should
  3330. (org-test-with-temp-text "* Headline3 :tag:\n"
  3331. (let ((org-special-ctrl-a/e 'reversed)
  3332. (this-command last-command))
  3333. (and (progn (org-end-of-line) (eolp))
  3334. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3335. (should
  3336. (org-test-with-temp-text "* Headline3 :tag:\n"
  3337. (let ((org-special-ctrl-a/e '(nil . reversed))
  3338. (this-command last-command))
  3339. (and (progn (org-end-of-line) (eolp))
  3340. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3341. (should-not
  3342. (org-test-with-temp-text "* Headline3 :tag:\n"
  3343. (let ((org-special-ctrl-a/e '(nil . t))
  3344. (this-command last-command))
  3345. (and (progn (org-end-of-line) (eolp))
  3346. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3347. (should
  3348. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3349. (org-overview)
  3350. (let ((org-special-ctrl-a/e 'reversed))
  3351. (org-end-of-line)
  3352. (= 1 (line-beginning-position)))))
  3353. ;; At a block without hidden contents.
  3354. (should
  3355. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3356. (progn (org-end-of-line) (eolp))))
  3357. ;; At a block with hidden contents.
  3358. (should-not
  3359. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3360. (let ((org-special-ctrl-a/e t))
  3361. (org-hide-block-toggle)
  3362. (org-end-of-line)
  3363. (eobp))))
  3364. ;; Get past invisible characters at the end of line.
  3365. (should
  3366. (org-test-with-temp-text "[[https://orgmode.org]]"
  3367. (org-end-of-line)
  3368. (eolp))))
  3369. (ert-deftest test-org/open-line ()
  3370. "Test `org-open-line' specifications."
  3371. ;; Call `open-line' outside of tables.
  3372. (should
  3373. (equal "\nText"
  3374. (org-test-with-temp-text "Text"
  3375. (org-open-line 1)
  3376. (buffer-string))))
  3377. ;; At a table, create a row above.
  3378. (should
  3379. (equal "\n| |\n| a |"
  3380. (org-test-with-temp-text "\n<point>| a |"
  3381. (org-open-line 1)
  3382. (buffer-string))))
  3383. ;; At the very first character of the buffer, also call `open-line'.
  3384. (should
  3385. (equal "\n| a |"
  3386. (org-test-with-temp-text "| a |"
  3387. (org-open-line 1)
  3388. (buffer-string))))
  3389. ;; Narrowing does not count.
  3390. (should
  3391. (equal "Text\n| |\n| a |"
  3392. (org-test-with-temp-text "Text\n<point>| a |"
  3393. (narrow-to-region (point) (point-max))
  3394. (org-open-line 1)
  3395. (widen)
  3396. (buffer-string)))))
  3397. (ert-deftest test-org/forward-sentence ()
  3398. "Test `org-forward-sentence' specifications."
  3399. ;; At the end of a table cell, move to the end of the next one.
  3400. (should
  3401. (org-test-with-temp-text "| a<point> | b |"
  3402. (org-forward-sentence)
  3403. (looking-at " |$")))
  3404. ;; Elsewhere in a cell, move to its end.
  3405. (should
  3406. (org-test-with-temp-text "| a<point>c | b |"
  3407. (org-forward-sentence)
  3408. (looking-at " | b |$")))
  3409. ;; Otherwise, simply call `forward-sentence'.
  3410. (should
  3411. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3412. (org-forward-sentence)
  3413. (looking-at " Sentence 2.")))
  3414. (should
  3415. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3416. (org-forward-sentence)
  3417. (org-forward-sentence)
  3418. (eobp)))
  3419. ;; At the end of an element, jump to the next one, without stopping
  3420. ;; on blank lines in-between.
  3421. (should
  3422. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3423. (org-forward-sentence)
  3424. (eobp)))
  3425. ;; Headlines are considered to be sentences by themselves, even if
  3426. ;; they do not end with a full stop.
  3427. (should
  3428. (equal
  3429. "* Headline"
  3430. (org-test-with-temp-text "* <point>Headline\nSentence."
  3431. (org-forward-sentence)
  3432. (buffer-substring-no-properties (line-beginning-position) (point)))))
  3433. (should
  3434. (org-test-with-temp-text "* Headline<point>\nSentence."
  3435. (org-forward-sentence)
  3436. (eobp)))
  3437. (should
  3438. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  3439. (org-forward-sentence)
  3440. (and (org-at-heading-p) (eolp)))))
  3441. (ert-deftest test-org/backward-sentence ()
  3442. "Test `org-backward-sentence' specifications."
  3443. ;; At the beginning of a table cell, move to the beginning of the
  3444. ;; previous one.
  3445. (should
  3446. (org-test-with-temp-text "| a | <point>b |"
  3447. (org-backward-sentence)
  3448. (looking-at "a | b |$")))
  3449. ;; Elsewhere in a cell, move to its beginning.
  3450. (should
  3451. (org-test-with-temp-text "| a | b<point>c |"
  3452. (org-backward-sentence)
  3453. (looking-at "bc |$")))
  3454. ;; Otherwise, simply call `backward-sentence'.
  3455. (should
  3456. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3457. (org-backward-sentence)
  3458. (looking-at "Sentence 2.")))
  3459. (should
  3460. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3461. (org-backward-sentence)
  3462. (org-backward-sentence)
  3463. (bobp)))
  3464. ;; Make sure to hit the beginning of a sentence on the same line as
  3465. ;; an item.
  3466. (should
  3467. (org-test-with-temp-text "- Line 1\n line <point>2."
  3468. (org-backward-sentence)
  3469. (looking-at "Line 1"))))
  3470. (ert-deftest test-org/forward-paragraph ()
  3471. "Test `org-forward-paragraph' specifications."
  3472. ;; At end of buffer, do not return an error.
  3473. (should
  3474. (org-test-with-temp-text "Paragraph"
  3475. (goto-char (point-max))
  3476. (org-forward-paragraph)
  3477. t))
  3478. ;; Standard test.
  3479. (should
  3480. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3481. (org-forward-paragraph)
  3482. (looking-at "P2")))
  3483. ;; Ignore depth.
  3484. (should
  3485. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3486. (org-forward-paragraph)
  3487. (looking-at "P1")))
  3488. ;; Do not enter elements with invisible contents.
  3489. (should
  3490. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3491. (org-hide-block-toggle)
  3492. (org-forward-paragraph)
  3493. (looking-at "P3")))
  3494. ;; On an affiliated keyword, jump to the beginning of the element.
  3495. (should
  3496. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3497. (org-forward-paragraph)
  3498. (looking-at "Para")))
  3499. ;; On an item or a footnote definition, move to the second element
  3500. ;; inside, if any.
  3501. (should
  3502. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3503. (org-forward-paragraph)
  3504. (looking-at " Paragraph")))
  3505. (should
  3506. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3507. (org-forward-paragraph)
  3508. (looking-at "Paragraph")))
  3509. ;; On an item, or a footnote definition, when the first line is
  3510. ;; empty, move to the first item.
  3511. (should
  3512. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3513. (org-forward-paragraph)
  3514. (looking-at " Paragraph")))
  3515. (should
  3516. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3517. (org-forward-paragraph)
  3518. (looking-at "Paragraph")))
  3519. ;; On a table (resp. a property drawer) do not move through table
  3520. ;; rows (resp. node properties).
  3521. (should
  3522. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3523. (org-forward-paragraph)
  3524. (looking-at "Paragraph")))
  3525. (should
  3526. (org-test-with-temp-text
  3527. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3528. (org-forward-paragraph)
  3529. (looking-at "Paragraph")))
  3530. ;; On a verse or source block, stop after blank lines.
  3531. (should
  3532. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3533. (org-forward-paragraph)
  3534. (looking-at "L2")))
  3535. (should
  3536. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3537. (org-forward-paragraph)
  3538. (looking-at "L2"))))
  3539. (ert-deftest test-org/backward-paragraph ()
  3540. "Test `org-backward-paragraph' specifications."
  3541. ;; Do not error at beginning of buffer.
  3542. (should
  3543. (org-test-with-temp-text "Paragraph"
  3544. (org-backward-paragraph)
  3545. t))
  3546. ;; Regular test.
  3547. (should
  3548. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  3549. (org-backward-paragraph)
  3550. (looking-at "P3")))
  3551. (should
  3552. (org-test-with-temp-text "P1\n\nP2\n\n<point>P3"
  3553. (org-backward-paragraph)
  3554. (looking-at-p "P2")))
  3555. ;; Ignore depth.
  3556. (should
  3557. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\n<point>P3"
  3558. (org-backward-paragraph)
  3559. (looking-at-p "P2")))
  3560. ;; Ignore invisible elements.
  3561. (should
  3562. (org-test-with-temp-text "* H1\n P1\n* H2"
  3563. (org-cycle)
  3564. (goto-char (point-max))
  3565. (beginning-of-line)
  3566. (org-backward-paragraph)
  3567. (bobp)))
  3568. ;; On an affiliated keyword, jump to the first one.
  3569. (should
  3570. (org-test-with-temp-text
  3571. "P1\n#+name: n\n#+caption: c1\n#+caption: <point>c2\nP2"
  3572. (org-backward-paragraph)
  3573. (looking-at-p "#\\+name")))
  3574. ;; On the second element in an item or a footnote definition, jump
  3575. ;; to item or the definition.
  3576. (should
  3577. (org-test-with-temp-text "- line1\n\n<point> line2"
  3578. (org-backward-paragraph)
  3579. (looking-at-p "- line1")))
  3580. (should
  3581. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  3582. (org-backward-paragraph)
  3583. (looking-at-p "\\[fn:1\\] line1")))
  3584. ;; On a table (resp. a property drawer), ignore table rows
  3585. ;; (resp. node properties).
  3586. (should
  3587. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  3588. (org-backward-paragraph)
  3589. (bobp)))
  3590. (should
  3591. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3592. (org-backward-paragraph)
  3593. (looking-at-p ":PROPERTIES:")))
  3594. ;; On a comment, example, src and verse blocks, stop before blank
  3595. ;; lines.
  3596. (should
  3597. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\n<point>L3\n#+END_VERSE"
  3598. (org-backward-paragraph)
  3599. (looking-at-p "L2")))
  3600. (should
  3601. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\n<point>L3#+END_SRC"
  3602. (org-backward-paragraph)
  3603. (looking-at-p "L2")))
  3604. ;; In comment, example, export, src and verse blocks, stop below
  3605. ;; opening line when called from within the block.
  3606. (should
  3607. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\nL2<point>\n#+END_VERSE"
  3608. (org-backward-paragraph)
  3609. (looking-at-p "L1")))
  3610. (should
  3611. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nL1\nL2<point>\n#+END_EXAMPLE"
  3612. (org-backward-paragraph)
  3613. (looking-at-p "L1")))
  3614. ;; When called from the opening line itself, however, move to
  3615. ;; beginning of block.
  3616. (should
  3617. (org-test-with-temp-text "#+BEGIN_<point>EXAMPLE\nL1\n#+END_EXAMPLE"
  3618. (org-backward-paragraph)
  3619. (bobp)))
  3620. ;; Pathological case: on an empty heading, move to its beginning.
  3621. (should
  3622. (org-test-with-temp-text "* <point>H"
  3623. (org-backward-paragraph)
  3624. (bobp))))
  3625. (ert-deftest test-org/forward-element ()
  3626. "Test `org-forward-element' specifications."
  3627. ;; 1. At EOB: should error.
  3628. (org-test-with-temp-text "Some text\n"
  3629. (goto-char (point-max))
  3630. (should-error (org-forward-element)))
  3631. ;; 2. Standard move: expected to ignore blank lines.
  3632. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3633. (org-forward-element)
  3634. (should (looking-at (regexp-quote "Second paragraph."))))
  3635. ;; 3. Headline tests.
  3636. (org-test-with-temp-text "
  3637. * Head 1
  3638. ** Head 1.1
  3639. *** Head 1.1.1
  3640. ** Head 1.2"
  3641. ;; 3.1. At an headline beginning: move to next headline at the
  3642. ;; same level.
  3643. (goto-line 3)
  3644. (org-forward-element)
  3645. (should (looking-at (regexp-quote "** Head 1.2")))
  3646. ;; 3.2. At an headline beginning: move to parent headline if no
  3647. ;; headline at the same level.
  3648. (goto-line 3)
  3649. (org-forward-element)
  3650. (should (looking-at (regexp-quote "** Head 1.2"))))
  3651. ;; 4. Greater element tests.
  3652. (org-test-with-temp-text
  3653. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3654. ;; 4.1. At a greater element: expected to skip contents.
  3655. (org-forward-element)
  3656. (should (looking-at (regexp-quote "Outside.")))
  3657. ;; 4.2. At the end of greater element contents: expected to skip
  3658. ;; to the end of the greater element.
  3659. (goto-line 2)
  3660. (org-forward-element)
  3661. (should (looking-at (regexp-quote "Outside."))))
  3662. ;; 5. List tests.
  3663. (org-test-with-temp-text "
  3664. - item1
  3665. - sub1
  3666. - sub2
  3667. - sub3
  3668. Inner paragraph.
  3669. - item2
  3670. Outside."
  3671. ;; 5.1. At list top point: expected to move to the element after
  3672. ;; the list.
  3673. (goto-line 2)
  3674. (org-forward-element)
  3675. (should (looking-at (regexp-quote "Outside.")))
  3676. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3677. ;; beginning of line, move to next item.
  3678. (goto-line 2)
  3679. (forward-char)
  3680. (org-forward-element)
  3681. (should (looking-at "- item2"))
  3682. (goto-line 4)
  3683. (forward-char)
  3684. (org-forward-element)
  3685. (should (looking-at " - sub2"))
  3686. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3687. (goto-line 4)
  3688. (org-forward-element)
  3689. (should (looking-at (regexp-quote " Inner paragraph.")))
  3690. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3691. (goto-line 8)
  3692. (org-forward-element)
  3693. (should (looking-at (regexp-quote " Inner paragraph.")))
  3694. ;; 5.5. At an item: expected to move to next item, if any.
  3695. (goto-line 6)
  3696. (org-forward-element)
  3697. (should (looking-at " - sub3"))))
  3698. (ert-deftest test-org/backward-element ()
  3699. "Test `org-backward-element' specifications."
  3700. ;; 1. Should error at BOB.
  3701. (org-test-with-temp-text " \nParagraph."
  3702. (should-error (org-backward-element)))
  3703. ;; 2. Should move at BOB when called on the first element in buffer.
  3704. (should
  3705. (org-test-with-temp-text "\n#+TITLE: test"
  3706. (progn (forward-line)
  3707. (org-backward-element)
  3708. (bobp))))
  3709. ;; 3. Not at the beginning of an element: move at its beginning.
  3710. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3711. (goto-line 3)
  3712. (end-of-line)
  3713. (org-backward-element)
  3714. (should (looking-at (regexp-quote "Paragraph2."))))
  3715. ;; 4. Headline tests.
  3716. (org-test-with-temp-text "
  3717. * Head 1
  3718. ** Head 1.1
  3719. *** Head 1.1.1
  3720. ** Head 1.2"
  3721. ;; 4.1. At an headline beginning: move to previous headline at the
  3722. ;; same level.
  3723. (goto-line 5)
  3724. (org-backward-element)
  3725. (should (looking-at (regexp-quote "** Head 1.1")))
  3726. ;; 4.2. At an headline beginning: move to parent headline if no
  3727. ;; headline at the same level.
  3728. (goto-line 3)
  3729. (org-backward-element)
  3730. (should (looking-at (regexp-quote "* Head 1")))
  3731. ;; 4.3. At the first top-level headline: should error.
  3732. (goto-line 2)
  3733. (should-error (org-backward-element)))
  3734. ;; 5. At beginning of first element inside a greater element:
  3735. ;; expected to move to greater element's beginning.
  3736. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3737. (goto-line 3)
  3738. (org-backward-element)
  3739. (should (looking-at "#\\+BEGIN_CENTER")))
  3740. ;; 6. At the beginning of the first element in a section: should
  3741. ;; move back to headline, if any.
  3742. (should
  3743. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3744. (progn (goto-char (point-max))
  3745. (beginning-of-line)
  3746. (org-backward-element)
  3747. (org-at-heading-p))))
  3748. ;; 7. List tests.
  3749. (org-test-with-temp-text "
  3750. - item1
  3751. - sub1
  3752. - sub2
  3753. - sub3
  3754. Inner paragraph.
  3755. - item2
  3756. Outside."
  3757. ;; 7.1. At beginning of sub-list: expected to move to the
  3758. ;; paragraph before it.
  3759. (goto-line 4)
  3760. (org-backward-element)
  3761. (should (looking-at "item1"))
  3762. ;; 7.2. At an item in a list: expected to move at previous item.
  3763. (goto-line 8)
  3764. (org-backward-element)
  3765. (should (looking-at " - sub2"))
  3766. (goto-line 12)
  3767. (org-backward-element)
  3768. (should (looking-at "- item1"))
  3769. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3770. ;; beginning.
  3771. (goto-line 10)
  3772. (org-backward-element)
  3773. (should (looking-at " - sub1"))
  3774. (goto-line 15)
  3775. (org-backward-element)
  3776. (should (looking-at "- item1"))
  3777. ;; 7.4. At blank-lines before list end: expected to move to top
  3778. ;; item.
  3779. (goto-line 14)
  3780. (org-backward-element)
  3781. (should (looking-at "- item1"))))
  3782. (ert-deftest test-org/up-element ()
  3783. "Test `org-up-element' specifications."
  3784. ;; 1. At BOB or with no surrounding element: should error.
  3785. (org-test-with-temp-text "Paragraph."
  3786. (should-error (org-up-element)))
  3787. (org-test-with-temp-text "* Head1\n* Head2"
  3788. (goto-line 2)
  3789. (should-error (org-up-element)))
  3790. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3791. (goto-line 3)
  3792. (should-error (org-up-element)))
  3793. ;; 2. At an headline: move to parent headline.
  3794. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3795. (goto-line 3)
  3796. (org-up-element)
  3797. (should (looking-at "\\* Head1")))
  3798. ;; 3. Inside a greater element: move to greater element beginning.
  3799. (org-test-with-temp-text
  3800. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3801. (goto-line 3)
  3802. (org-up-element)
  3803. (should (looking-at "#\\+BEGIN_CENTER")))
  3804. ;; 4. List tests.
  3805. (org-test-with-temp-text "* Top
  3806. - item1
  3807. - sub1
  3808. - sub2
  3809. Paragraph within sub2.
  3810. - item2"
  3811. ;; 4.1. Within an item: move to the item beginning.
  3812. (goto-line 8)
  3813. (org-up-element)
  3814. (should (looking-at " - sub2"))
  3815. ;; 4.2. At an item in a sub-list: move to parent item.
  3816. (goto-line 4)
  3817. (org-up-element)
  3818. (should (looking-at "- item1"))
  3819. ;; 4.3. At an item in top list: move to beginning of whole list.
  3820. (goto-line 10)
  3821. (org-up-element)
  3822. (should (looking-at "- item1"))
  3823. ;; 4.4. Special case. At very top point: should move to parent of
  3824. ;; list.
  3825. (goto-line 2)
  3826. (org-up-element)
  3827. (should (looking-at "\\* Top"))))
  3828. (ert-deftest test-org/down-element ()
  3829. "Test `org-down-element' specifications."
  3830. ;; Error when the element hasn't got a recursive type.
  3831. (org-test-with-temp-text "Paragraph."
  3832. (should-error (org-down-element)))
  3833. ;; Error when the element has no contents
  3834. (org-test-with-temp-text "* Headline"
  3835. (should-error (org-down-element)))
  3836. ;; When at a plain-list, move to first item.
  3837. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3838. (goto-line 2)
  3839. (org-down-element)
  3840. (should (looking-at " - Item 1.1")))
  3841. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3842. (org-down-element)
  3843. (should (looking-at " Item 1")))
  3844. ;; When at a table, move to first row
  3845. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3846. (org-down-element)
  3847. (should (looking-at " a | b |")))
  3848. ;; Otherwise, move inside the greater element.
  3849. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3850. (org-down-element)
  3851. (should (looking-at "Paragraph"))))
  3852. (ert-deftest test-org/drag-element-backward ()
  3853. "Test `org-drag-element-backward' specifications."
  3854. ;; Standard test.
  3855. (should
  3856. (equal
  3857. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3858. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3859. (org-drag-element-backward)
  3860. (buffer-string))))
  3861. (should
  3862. (equal
  3863. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3864. (org-test-with-temp-text
  3865. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3866. (org-drag-element-backward)
  3867. (buffer-string))))
  3868. ;; Preserve blank lines.
  3869. (should
  3870. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3871. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3872. (org-drag-element-backward)
  3873. (buffer-string))))
  3874. ;; Preserve column.
  3875. (should
  3876. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3877. (org-drag-element-backward)
  3878. (looking-at-p "2")))
  3879. ;; Error when trying to move first element of buffer.
  3880. (should-error
  3881. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3882. (org-drag-element-backward))
  3883. :type 'user-error)
  3884. ;; Error when trying to swap nested elements.
  3885. (should-error
  3886. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3887. (org-drag-element-backward))
  3888. :type 'user-error)
  3889. ;; Error when trying to swap an headline element and a non-headline
  3890. ;; element.
  3891. (should-error
  3892. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3893. (org-drag-element-backward))
  3894. :type 'error)
  3895. ;; Error when called before first element.
  3896. (should-error
  3897. (org-test-with-temp-text "\n<point>"
  3898. (org-drag-element-backward))
  3899. :type 'user-error)
  3900. ;; Preserve visibility of elements and their contents.
  3901. (should
  3902. (equal '((63 . 82) (26 . 48))
  3903. (org-test-with-temp-text "
  3904. #+BEGIN_CENTER
  3905. Text.
  3906. #+END_CENTER
  3907. - item 1
  3908. #+BEGIN_QUOTE
  3909. Text.
  3910. #+END_QUOTE"
  3911. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3912. (search-backward "- item 1")
  3913. (org-drag-element-backward)
  3914. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3915. (overlays-in (point-min) (point-max))))))
  3916. ;; Pathological case: handle call with point in blank lines right
  3917. ;; after a headline.
  3918. (should
  3919. (equal "* H2\n\n* H1\nText\n"
  3920. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3921. (org-drag-element-backward)
  3922. (buffer-string)))))
  3923. (ert-deftest test-org/drag-element-forward ()
  3924. "Test `org-drag-element-forward' specifications."
  3925. ;; 1. Error when trying to move first element of buffer.
  3926. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3927. (goto-line 3)
  3928. (should-error (org-drag-element-forward)))
  3929. ;; 2. Error when trying to swap nested elements.
  3930. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3931. (forward-line)
  3932. (should-error (org-drag-element-forward)))
  3933. ;; 3. Error when trying to swap a non-headline element and an
  3934. ;; headline.
  3935. (org-test-with-temp-text "Test.\n* Head 1"
  3936. (should-error (org-drag-element-forward)))
  3937. ;; 4. Error when called before first element.
  3938. (should-error
  3939. (org-test-with-temp-text "\n"
  3940. (forward-line)
  3941. (org-drag-element-backward))
  3942. :type 'user-error)
  3943. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3944. ;; between elements.
  3945. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3946. (search-forward "graph")
  3947. (org-drag-element-forward)
  3948. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3949. (should (looking-at " 1")))
  3950. ;; 5. Preserve visibility of elements and their contents.
  3951. (org-test-with-temp-text "
  3952. #+BEGIN_CENTER
  3953. Text.
  3954. #+END_CENTER
  3955. - item 1
  3956. #+BEGIN_QUOTE
  3957. Text.
  3958. #+END_QUOTE"
  3959. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3960. (search-backward "#+BEGIN_CENTER")
  3961. (org-drag-element-forward)
  3962. (should
  3963. (equal
  3964. '((63 . 82) (26 . 48))
  3965. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3966. (overlays-in (point-min) (point-max)))))))
  3967. (ert-deftest test-org/next-block ()
  3968. "Test `org-next-block' specifications."
  3969. ;; Regular test.
  3970. (should
  3971. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3972. (org-next-block 1)
  3973. (looking-at "#\\+BEGIN_CENTER")))
  3974. ;; Ignore case.
  3975. (should
  3976. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3977. (let ((case-fold-search nil))
  3978. (org-next-block 1)
  3979. (looking-at "#\\+begin_center"))))
  3980. ;; Ignore current line.
  3981. (should
  3982. (org-test-with-temp-text
  3983. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3984. (org-next-block 1)
  3985. (looking-at "#\\+BEGIN_CENTER")))
  3986. ;; Throw an error when no block is found.
  3987. (should-error
  3988. (org-test-with-temp-text "Paragraph"
  3989. (org-next-block 1)))
  3990. ;; With an argument, skip many blocks at once.
  3991. (should
  3992. (org-test-with-temp-text
  3993. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3994. (org-next-block 2)
  3995. (looking-at "#\\+BEGIN_QUOTE")))
  3996. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3997. (should
  3998. (org-test-with-temp-text
  3999. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  4000. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  4001. (looking-at "#\\+BEGIN_QUOTE")))
  4002. ;; Optional argument is also case-insensitive.
  4003. (should
  4004. (org-test-with-temp-text
  4005. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  4006. (let ((case-fold-search nil))
  4007. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  4008. (looking-at "#\\+begin_quote")))))
  4009. (ert-deftest test-org/insert-structure-template ()
  4010. "Test `org-insert-structure-template'."
  4011. ;; Test in empty buffer.
  4012. (should
  4013. (string= "#+begin_foo\n#+end_foo\n"
  4014. (org-test-with-temp-text ""
  4015. (org-insert-structure-template "foo")
  4016. (buffer-string))))
  4017. ;; Test with multiple lines in buffer.
  4018. (should
  4019. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4020. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4021. (transient-mark-mode 1)
  4022. (org-mark-element)
  4023. (org-insert-structure-template "foo")
  4024. (buffer-string))))
  4025. ;; Mark only the current line.
  4026. (should
  4027. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4028. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4029. (transient-mark-mode 1)
  4030. (set-mark (point-min))
  4031. (end-of-line)
  4032. (org-insert-structure-template "foo")
  4033. (buffer-string))))
  4034. ;; Middle of paragraph.
  4035. (should
  4036. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4037. (org-test-with-temp-text "p1\n<point>p2\np3"
  4038. (set-mark (line-beginning-position))
  4039. (end-of-line)
  4040. (activate-mark)
  4041. (org-insert-structure-template "foo")
  4042. (buffer-string))))
  4043. ;; Test with text in buffer, no region, no final newline.
  4044. (should
  4045. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4046. (org-test-with-temp-text "I'm a paragraph."
  4047. (org-mark-element)
  4048. (org-insert-structure-template "foo")
  4049. (buffer-string))))
  4050. ;; Test with text in buffer and region set.
  4051. (should
  4052. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4053. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4054. (set-mark (point))
  4055. (goto-char (point-max))
  4056. (org-insert-structure-template "foo")
  4057. (buffer-string))))
  4058. ;; Test with example escaping.
  4059. (should
  4060. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4061. (org-test-with-temp-text "* Heading"
  4062. (org-mark-element)
  4063. (org-insert-structure-template "example")
  4064. (buffer-string))))
  4065. ;; Test with indentation.
  4066. (should
  4067. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4068. (org-test-with-temp-text " This is a paragraph"
  4069. (org-mark-element)
  4070. (org-insert-structure-template "foo")
  4071. (buffer-string))))
  4072. (should
  4073. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4074. (org-test-with-temp-text " Line 1\n Line2"
  4075. (org-mark-element)
  4076. (org-insert-structure-template "foo")
  4077. (buffer-string))))
  4078. ;; Test point location.
  4079. (should
  4080. (string= "#+begin_foo\n"
  4081. (org-test-with-temp-text ""
  4082. (org-insert-structure-template "foo")
  4083. (buffer-substring (point-min) (point)))))
  4084. (should
  4085. (string= "#+begin_src "
  4086. (org-test-with-temp-text ""
  4087. (org-insert-structure-template "src")
  4088. (buffer-substring (point-min) (point))))))
  4089. (ert-deftest test-org/previous-block ()
  4090. "Test `org-previous-block' specifications."
  4091. ;; Regular test.
  4092. (should
  4093. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4094. (org-previous-block 1)
  4095. (looking-at "#\\+BEGIN_CENTER")))
  4096. ;; Ignore case.
  4097. (should
  4098. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4099. (let ((case-fold-search nil))
  4100. (org-previous-block 1)
  4101. (looking-at "#\\+begin_center"))))
  4102. ;; Ignore current line.
  4103. (should
  4104. (org-test-with-temp-text
  4105. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4106. (org-previous-block 1)
  4107. (looking-at "#\\+BEGIN_QUOTE")))
  4108. ;; Throw an error when no block is found.
  4109. (should-error
  4110. (org-test-with-temp-text "Paragraph<point>"
  4111. (org-previous-block 1)))
  4112. ;; With an argument, skip many blocks at once.
  4113. (should
  4114. (org-test-with-temp-text
  4115. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4116. (org-previous-block 2)
  4117. (looking-at "#\\+BEGIN_CENTER")))
  4118. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4119. (should
  4120. (org-test-with-temp-text
  4121. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4122. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4123. (looking-at "#\\+BEGIN_QUOTE")))
  4124. ;; Optional argument is also case-insensitive.
  4125. (should
  4126. (org-test-with-temp-text
  4127. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4128. (let ((case-fold-search nil))
  4129. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4130. (looking-at "#\\+begin_quote")))))
  4131. ;;; Outline structure
  4132. (ert-deftest test-org/demote ()
  4133. "Test `org-demote' specifications."
  4134. ;; Add correct number of stars according to `org-odd-levels-only'.
  4135. (should
  4136. (= 2
  4137. (org-test-with-temp-text "* H"
  4138. (let ((org-odd-levels-only nil)) (org-demote))
  4139. (org-current-level))))
  4140. (should
  4141. (= 3
  4142. (org-test-with-temp-text "* H"
  4143. (let ((org-odd-levels-only t)) (org-demote))
  4144. (org-current-level))))
  4145. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4146. (should
  4147. (org-test-with-temp-text "* H :tag:"
  4148. (let ((org-tags-column 10)
  4149. (org-auto-align-tags t)
  4150. (org-odd-levels-only nil))
  4151. (org-demote))
  4152. (org-move-to-column 10)
  4153. (looking-at-p ":tag:$")))
  4154. (should-not
  4155. (org-test-with-temp-text "* H :tag:"
  4156. (let ((org-tags-column 10)
  4157. (org-auto-align-tags nil)
  4158. (org-odd-levels-only nil))
  4159. (org-demote))
  4160. (org-move-to-column 10)
  4161. (looking-at-p ":tag:$")))
  4162. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4163. ;; info and property drawers accordingly.
  4164. (should
  4165. (= 3
  4166. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4167. (let ((org-odd-levels-only nil)
  4168. (org-adapt-indentation t))
  4169. (org-demote))
  4170. (forward-line)
  4171. (org-get-indentation))))
  4172. (should
  4173. (= 3
  4174. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4175. (let ((org-odd-levels-only nil)
  4176. (org-adapt-indentation t))
  4177. (org-demote))
  4178. (forward-line)
  4179. (org-get-indentation))))
  4180. (should-not
  4181. (= 3
  4182. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4183. (let ((org-odd-levels-only nil)
  4184. (org-adapt-indentation nil))
  4185. (org-demote))
  4186. (forward-line)
  4187. (org-get-indentation))))
  4188. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4189. ;; section accordingly. Ignore, however, footnote definitions and
  4190. ;; inlinetasks boundaries.
  4191. (should
  4192. (= 3
  4193. (org-test-with-temp-text "* H\n Paragraph"
  4194. (let ((org-odd-levels-only nil)
  4195. (org-adapt-indentation t))
  4196. (org-demote))
  4197. (forward-line)
  4198. (org-get-indentation))))
  4199. (should
  4200. (= 2
  4201. (org-test-with-temp-text "* H\n Paragraph"
  4202. (let ((org-odd-levels-only nil)
  4203. (org-adapt-indentation nil))
  4204. (org-demote))
  4205. (forward-line)
  4206. (org-get-indentation))))
  4207. (should
  4208. (zerop
  4209. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4210. (let ((org-odd-levels-only nil)
  4211. (org-adapt-indentation t))
  4212. (org-demote))
  4213. (goto-char (point-max))
  4214. (org-get-indentation))))
  4215. (should
  4216. (= 3
  4217. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4218. (let ((org-odd-levels-only nil)
  4219. (org-adapt-indentation t))
  4220. (org-demote))
  4221. (goto-char (point-max))
  4222. (org-get-indentation))))
  4223. (when (featurep 'org-inlinetask)
  4224. (should
  4225. (zerop
  4226. (let ((org-inlinetask-min-level 5)
  4227. (org-adapt-indentation t))
  4228. (org-test-with-temp-text "* H\n***** I\n***** END"
  4229. (org-demote)
  4230. (forward-line)
  4231. (org-get-indentation))))))
  4232. (when (featurep 'org-inlinetask)
  4233. (should
  4234. (= 3
  4235. (let ((org-inlinetask-min-level 5)
  4236. (org-adapt-indentation t))
  4237. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4238. (org-demote)
  4239. (forward-line 2)
  4240. (org-get-indentation))))))
  4241. ;; Ignore contents of source blocks or example blocks when
  4242. ;; indentation should be preserved (through
  4243. ;; `org-src-preserve-indentation' or "-i" flag).
  4244. (should-not
  4245. (zerop
  4246. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4247. (let ((org-adapt-indentation t)
  4248. (org-src-preserve-indentation nil))
  4249. (org-demote))
  4250. (forward-line 2)
  4251. (org-get-indentation))))
  4252. (should
  4253. (zerop
  4254. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4255. (let ((org-adapt-indentation t)
  4256. (org-src-preserve-indentation t))
  4257. (org-demote))
  4258. (forward-line 2)
  4259. (org-get-indentation))))
  4260. (should
  4261. (zerop
  4262. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4263. (let ((org-adapt-indentation t)
  4264. (org-src-preserve-indentation t))
  4265. (org-demote))
  4266. (forward-line 2)
  4267. (org-get-indentation))))
  4268. (should
  4269. (zerop
  4270. (org-test-with-temp-text
  4271. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  4272. (let ((org-adapt-indentation t)
  4273. (org-src-preserve-indentation nil))
  4274. (org-demote))
  4275. (forward-line 2)
  4276. (org-get-indentation)))))
  4277. (ert-deftest test-org/promote ()
  4278. "Test `org-promote' specifications."
  4279. ;; Return an error if headline is to be promoted to level 0, unless
  4280. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  4281. ;; headline becomes a comment.
  4282. (should-error
  4283. (org-test-with-temp-text "* H"
  4284. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  4285. (should
  4286. (equal "# H"
  4287. (org-test-with-temp-text "* H"
  4288. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  4289. (buffer-string))))
  4290. ;; Remove correct number of stars according to
  4291. ;; `org-odd-levels-only'.
  4292. (should
  4293. (= 2
  4294. (org-test-with-temp-text "*** H"
  4295. (let ((org-odd-levels-only nil)) (org-promote))
  4296. (org-current-level))))
  4297. (should
  4298. (= 1
  4299. (org-test-with-temp-text "*** H"
  4300. (let ((org-odd-levels-only t)) (org-promote))
  4301. (org-current-level))))
  4302. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4303. (should
  4304. (org-test-with-temp-text "** H :tag:"
  4305. (let ((org-tags-column 10)
  4306. (org-auto-align-tags t)
  4307. (org-odd-levels-only nil))
  4308. (org-promote))
  4309. (org-move-to-column 10)
  4310. (looking-at-p ":tag:$")))
  4311. (should-not
  4312. (org-test-with-temp-text "** H :tag:"
  4313. (let ((org-tags-column 10)
  4314. (org-auto-align-tags nil)
  4315. (org-odd-levels-only nil))
  4316. (org-promote))
  4317. (org-move-to-column 10)
  4318. (looking-at-p ":tag:$")))
  4319. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4320. ;; info and property drawers.
  4321. (should
  4322. (= 2
  4323. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4324. (let ((org-odd-levels-only nil)
  4325. (org-adapt-indentation t))
  4326. (org-promote))
  4327. (forward-line)
  4328. (org-get-indentation))))
  4329. (should
  4330. (= 2
  4331. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4332. (let ((org-odd-levels-only nil)
  4333. (org-adapt-indentation t))
  4334. (org-promote))
  4335. (forward-line)
  4336. (org-get-indentation))))
  4337. (should-not
  4338. (= 2
  4339. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4340. (let ((org-odd-levels-only nil)
  4341. (org-adapt-indentation nil))
  4342. (org-promote))
  4343. (forward-line)
  4344. (org-get-indentation))))
  4345. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4346. ;; section accordingly. Ignore, however, footnote definitions and
  4347. ;; inlinetasks boundaries.
  4348. (should
  4349. (= 2
  4350. (org-test-with-temp-text "** H\n Paragraph"
  4351. (let ((org-odd-levels-only nil)
  4352. (org-adapt-indentation t))
  4353. (org-promote))
  4354. (forward-line)
  4355. (org-get-indentation))))
  4356. (should-not
  4357. (= 2
  4358. (org-test-with-temp-text "** H\n Paragraph"
  4359. (let ((org-odd-levels-only nil)
  4360. (org-adapt-indentation nil))
  4361. (org-promote))
  4362. (forward-line)
  4363. (org-get-indentation))))
  4364. (should
  4365. (= 2
  4366. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4367. (let ((org-odd-levels-only nil)
  4368. (org-adapt-indentation t))
  4369. (org-promote))
  4370. (forward-line)
  4371. (org-get-indentation))))
  4372. (when (featurep 'org-inlinetask)
  4373. (should
  4374. (zerop
  4375. (let ((org-inlinetask-min-level 5)
  4376. (org-adapt-indentation t))
  4377. (org-test-with-temp-text "** H\n***** I\n***** END"
  4378. (org-promote)
  4379. (forward-line)
  4380. (org-get-indentation))))))
  4381. (when (featurep 'org-inlinetask)
  4382. (should
  4383. (= 2
  4384. (let ((org-inlinetask-min-level 5)
  4385. (org-adapt-indentation t))
  4386. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4387. (org-promote)
  4388. (forward-line 2)
  4389. (org-get-indentation))))))
  4390. ;; Give up shifting if it would break document's structure
  4391. ;; otherwise.
  4392. (should
  4393. (= 3
  4394. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4395. (let ((org-odd-levels-only nil)
  4396. (org-adapt-indentation t))
  4397. (org-promote))
  4398. (forward-line)
  4399. (org-get-indentation))))
  4400. (should
  4401. (= 3
  4402. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4403. (let ((org-odd-levels-only nil)
  4404. (org-adapt-indentation t))
  4405. (org-promote))
  4406. (forward-line)
  4407. (org-get-indentation))))
  4408. ;; Ignore contents of source blocks or example blocks when
  4409. ;; indentation should be preserved (through
  4410. ;; `org-src-preserve-indentation' or "-i" flag).
  4411. (should-not
  4412. (zerop
  4413. (org-test-with-temp-text
  4414. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4415. (let ((org-adapt-indentation t)
  4416. (org-src-preserve-indentation nil)
  4417. (org-odd-levels-only nil))
  4418. (org-promote))
  4419. (forward-line)
  4420. (org-get-indentation))))
  4421. (should
  4422. (zerop
  4423. (org-test-with-temp-text
  4424. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4425. (let ((org-adapt-indentation t)
  4426. (org-src-preserve-indentation t)
  4427. (org-odd-levels-only nil))
  4428. (org-promote))
  4429. (forward-line)
  4430. (org-get-indentation))))
  4431. (should
  4432. (zerop
  4433. (org-test-with-temp-text
  4434. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4435. (let ((org-adapt-indentation t)
  4436. (org-src-preserve-indentation t)
  4437. (org-odd-levels-only nil))
  4438. (org-promote))
  4439. (forward-line)
  4440. (org-get-indentation))))
  4441. (should
  4442. (zerop
  4443. (org-test-with-temp-text
  4444. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4445. (let ((org-adapt-indentation t)
  4446. (org-src-preserve-indentation nil)
  4447. (org-odd-levels-only nil))
  4448. (org-promote))
  4449. (forward-line)
  4450. (org-get-indentation)))))
  4451. (ert-deftest test-org/org-get-valid-level ()
  4452. "Test function `org-get-valid-level' specifications."
  4453. (let ((org-odd-levels-only nil))
  4454. (should (equal 1 (org-get-valid-level 0 0)))
  4455. (should (equal 1 (org-get-valid-level 0 1)))
  4456. (should (equal 2 (org-get-valid-level 0 2)))
  4457. (should (equal 3 (org-get-valid-level 0 3)))
  4458. (should (equal 1 (org-get-valid-level 1 0)))
  4459. (should (equal 2 (org-get-valid-level 1 1)))
  4460. (should (equal 23 (org-get-valid-level 1 22)))
  4461. (should (equal 1 (org-get-valid-level 1 -1)))
  4462. (should (equal 1 (org-get-valid-level 2 -1))))
  4463. (let ((org-odd-levels-only t))
  4464. (should (equal 1 (org-get-valid-level 0 0)))
  4465. (should (equal 1 (org-get-valid-level 0 1)))
  4466. (should (equal 3 (org-get-valid-level 0 2)))
  4467. (should (equal 5 (org-get-valid-level 0 3)))
  4468. (should (equal 1 (org-get-valid-level 1 0)))
  4469. (should (equal 3 (org-get-valid-level 1 1)))
  4470. (should (equal 3 (org-get-valid-level 2 1)))
  4471. (should (equal 5 (org-get-valid-level 3 1)))
  4472. (should (equal 5 (org-get-valid-level 4 1)))
  4473. (should (equal 43 (org-get-valid-level 1 21)))
  4474. (should (equal 1 (org-get-valid-level 1 -1)))
  4475. (should (equal 1 (org-get-valid-level 2 -1)))
  4476. (should (equal 1 (org-get-valid-level 3 -1)))
  4477. (should (equal 3 (org-get-valid-level 4 -1)))
  4478. (should (equal 3 (org-get-valid-level 5 -1)))))
  4479. ;;; Planning
  4480. (ert-deftest test-org/at-planning-p ()
  4481. "Test `org-at-planning-p' specifications."
  4482. ;; Regular test.
  4483. (should
  4484. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4485. (org-at-planning-p)))
  4486. (should-not
  4487. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4488. (org-at-planning-p)))
  4489. ;; Correctly find planning attached to inlinetasks.
  4490. (when (featurep 'org-inlinetask)
  4491. (should
  4492. (org-test-with-temp-text
  4493. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4494. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4495. (should-not
  4496. (org-test-with-temp-text
  4497. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4498. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4499. (should-not
  4500. (org-test-with-temp-text
  4501. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4502. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4503. (should-not
  4504. (org-test-with-temp-text
  4505. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4506. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4507. (ert-deftest test-org/add-planning-info ()
  4508. "Test `org-add-planning-info'."
  4509. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4510. (should
  4511. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4512. (org-test-with-temp-text "* H\nParagraph<point>"
  4513. (let ((org-adapt-indentation t))
  4514. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4515. (replace-regexp-in-string
  4516. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4517. nil nil 1))))
  4518. ;; Create deadline when `org-adapt-indentation' is nil.
  4519. (should
  4520. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4521. (org-test-with-temp-text "* H\nParagraph<point>"
  4522. (let ((org-adapt-indentation nil))
  4523. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4524. (replace-regexp-in-string
  4525. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4526. nil nil 1))))
  4527. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4528. (should
  4529. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4530. (org-test-with-temp-text "\
  4531. * H
  4532. DEADLINE: <2015-06-24 Wed>
  4533. Paragraph<point>"
  4534. (let ((org-adapt-indentation t))
  4535. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4536. (replace-regexp-in-string
  4537. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4538. nil nil 1))))
  4539. ;; Update deadline when `org-adapt-indentation' is nil.
  4540. (should
  4541. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4542. (org-test-with-temp-text "\
  4543. * H
  4544. DEADLINE: <2015-06-24 Wed>
  4545. Paragraph<point>"
  4546. (let ((org-adapt-indentation nil))
  4547. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4548. (replace-regexp-in-string
  4549. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4550. nil nil 1))))
  4551. ;; Schedule when `org-adapt-indentation' is non-nil.
  4552. (should
  4553. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4554. (org-test-with-temp-text "* H\nParagraph<point>"
  4555. (let ((org-adapt-indentation t))
  4556. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4557. (replace-regexp-in-string
  4558. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4559. nil nil 1))))
  4560. ;; Schedule when `org-adapt-indentation' is nil.
  4561. (should
  4562. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4563. (org-test-with-temp-text "* H\nParagraph<point>"
  4564. (let ((org-adapt-indentation nil))
  4565. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4566. (replace-regexp-in-string
  4567. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4568. nil nil 1))))
  4569. ;; Add deadline when scheduled.
  4570. (should
  4571. (equal "\
  4572. * H
  4573. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4574. Paragraph"
  4575. (org-test-with-temp-text "\
  4576. * H
  4577. SCHEDULED: <2015-06-24 Wed>
  4578. Paragraph<point>"
  4579. (let ((org-adapt-indentation t))
  4580. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4581. (replace-regexp-in-string
  4582. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4583. nil nil 1))))
  4584. ;; Remove middle entry.
  4585. (should
  4586. (equal "\
  4587. * H
  4588. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4589. Paragraph"
  4590. (org-test-with-temp-text "\
  4591. * H
  4592. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4593. Paragraph<point>"
  4594. (let ((org-adapt-indentation t))
  4595. (org-add-planning-info nil nil 'deadline))
  4596. (replace-regexp-in-string
  4597. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4598. nil nil 1))))
  4599. ;; Remove last entry and then middle entry (order should not
  4600. ;; matter).
  4601. (should
  4602. (equal "\
  4603. * H
  4604. CLOSED: [2015-06-24]
  4605. Paragraph"
  4606. (org-test-with-temp-text "\
  4607. * H
  4608. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4609. Paragraph<point>"
  4610. (let ((org-adapt-indentation t))
  4611. (org-add-planning-info nil nil 'scheduled 'deadline))
  4612. (replace-regexp-in-string
  4613. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4614. nil nil 1))))
  4615. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4616. (should
  4617. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4618. (org-test-with-temp-text "\
  4619. * H
  4620. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4621. Paragraph<point>"
  4622. (let ((org-adapt-indentation t))
  4623. (org-add-planning-info nil nil 'closed))
  4624. (replace-regexp-in-string
  4625. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4626. nil nil 1))))
  4627. (should
  4628. (equal "* H\n Paragraph"
  4629. (org-test-with-temp-text "\
  4630. * H
  4631. CLOSED: [2015-06-25 Thu]
  4632. Paragraph<point>"
  4633. (let ((org-adapt-indentation t))
  4634. (org-add-planning-info nil nil 'closed))
  4635. (replace-regexp-in-string
  4636. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4637. nil nil 1))))
  4638. ;; Remove closed when `org-adapt-indentation' is nil.
  4639. (should
  4640. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4641. (org-test-with-temp-text "\
  4642. * H
  4643. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4644. Paragraph<point>"
  4645. (let ((org-adapt-indentation nil))
  4646. (org-add-planning-info nil nil 'closed))
  4647. (replace-regexp-in-string
  4648. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4649. nil nil 1))))
  4650. (should
  4651. (equal "* H\nParagraph"
  4652. (org-test-with-temp-text "\
  4653. * H
  4654. CLOSED: [2015-06-25 Thu]
  4655. Paragraph<point>"
  4656. (let ((org-adapt-indentation nil))
  4657. (org-add-planning-info nil nil 'closed))
  4658. (replace-regexp-in-string
  4659. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4660. nil nil 1))))
  4661. ;; Remove closed entry and delete empty line.
  4662. (should
  4663. (equal "\
  4664. * H
  4665. Paragraph"
  4666. (org-test-with-temp-text "\
  4667. * H
  4668. CLOSED: [2015-06-24 Wed]
  4669. Paragraph<point>"
  4670. (let ((org-adapt-indentation t))
  4671. (org-add-planning-info nil nil 'closed))
  4672. (replace-regexp-in-string
  4673. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4674. nil nil 1))))
  4675. ;; Remove one entry and update another.
  4676. (should
  4677. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4678. (org-test-with-temp-text "\
  4679. * H
  4680. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4681. Paragraph<point>"
  4682. (let ((org-adapt-indentation t))
  4683. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4684. (replace-regexp-in-string
  4685. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4686. nil nil 1)))))
  4687. (ert-deftest test-org/deadline ()
  4688. "Test `org-deadline' specifications."
  4689. ;; Insert a new value or replace existing one.
  4690. (should
  4691. (equal "* H\nDEADLINE: <2012-03-29>"
  4692. (org-test-with-temp-text "* H"
  4693. (let ((org-adapt-indentation nil)
  4694. (org-last-inserted-timestamp nil))
  4695. (org-deadline nil "<2012-03-29 Tue>"))
  4696. (replace-regexp-in-string
  4697. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4698. nil nil 1))))
  4699. (should
  4700. (equal "* H\nDEADLINE: <2014-03-04>"
  4701. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4702. (let ((org-adapt-indentation nil)
  4703. (org-last-inserted-timestamp nil))
  4704. (org-deadline nil "<2014-03-04 Thu>"))
  4705. (replace-regexp-in-string
  4706. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4707. nil nil 1))))
  4708. ;; Accept delta time, e.g., "+2d".
  4709. (should
  4710. (equal "* H\nDEADLINE: <2015-03-04>"
  4711. (org-test-at-time "2014-03-04"
  4712. (org-test-with-temp-text "* H"
  4713. (let ((org-adapt-indentation nil)
  4714. (org-last-inserted-timestamp nil))
  4715. (org-deadline nil "+1y"))
  4716. (replace-regexp-in-string
  4717. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4718. ;; Preserve repeater.
  4719. (should
  4720. (equal "* H\nDEADLINE: <2012-03-29 +2y>"
  4721. (org-test-with-temp-text "* H"
  4722. (let ((org-adapt-indentation nil)
  4723. (org-last-inserted-timestamp nil))
  4724. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4725. (replace-regexp-in-string
  4726. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4727. ;; Remove CLOSED keyword, if any.
  4728. (should
  4729. (equal "* H\nDEADLINE: <2012-03-29>"
  4730. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4731. (let ((org-adapt-indentation nil)
  4732. (org-last-inserted-timestamp nil))
  4733. (org-deadline nil "<2012-03-29 Tue>"))
  4734. (replace-regexp-in-string
  4735. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4736. ;; With C-u argument, remove DEADLINE keyword.
  4737. (should
  4738. (equal "* H\n"
  4739. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4740. (let ((org-adapt-indentation nil)
  4741. (org-last-inserted-timestamp nil))
  4742. (org-deadline '(4)))
  4743. (buffer-string))))
  4744. (should
  4745. (equal "* H"
  4746. (org-test-with-temp-text "* H"
  4747. (let ((org-adapt-indentation nil)
  4748. (org-last-inserted-timestamp nil))
  4749. (org-deadline '(4)))
  4750. (buffer-string))))
  4751. ;; With C-u C-u argument, prompt for a delay cookie.
  4752. (should
  4753. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4754. (cl-letf (((symbol-function 'org-read-date)
  4755. (lambda (&rest args)
  4756. (apply #'encode-time
  4757. (org-parse-time-string "2014-03-04")))))
  4758. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4759. (let ((org-adapt-indentation nil)
  4760. (org-last-inserted-timestamp nil))
  4761. (org-deadline '(16)))
  4762. (buffer-string)))))
  4763. (should-error
  4764. (cl-letf (((symbol-function 'org-read-date)
  4765. (lambda (&rest args)
  4766. (apply #'encode-time
  4767. (org-parse-time-string "2014-03-04")))))
  4768. (org-test-with-temp-text "* H"
  4769. (let ((org-adapt-indentation nil)
  4770. (org-last-inserted-timestamp nil))
  4771. (org-deadline '(16)))
  4772. (buffer-string))))
  4773. ;; When a region is active and
  4774. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4775. ;; same value in all headlines in region.
  4776. (should
  4777. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  4778. (org-test-with-temp-text "* H1\n* H2"
  4779. (let ((org-adapt-indentation nil)
  4780. (org-last-inserted-timestamp nil)
  4781. (org-loop-over-headlines-in-active-region t))
  4782. (transient-mark-mode 1)
  4783. (push-mark (point) t t)
  4784. (goto-char (point-max))
  4785. (org-deadline nil "2012-03-29"))
  4786. (replace-regexp-in-string
  4787. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4788. (should-not
  4789. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  4790. (org-test-with-temp-text "* H1\n* H2"
  4791. (let ((org-adapt-indentation nil)
  4792. (org-last-inserted-timestamp nil)
  4793. (org-loop-over-headlines-in-active-region nil))
  4794. (transient-mark-mode 1)
  4795. (push-mark (point) t t)
  4796. (goto-char (point-max))
  4797. (org-deadline nil "2012-03-29"))
  4798. (replace-regexp-in-string
  4799. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4800. (ert-deftest test-org/schedule ()
  4801. "Test `org-schedule' specifications."
  4802. ;; Insert a new value or replace existing one.
  4803. (should
  4804. (equal "* H\nSCHEDULED: <2012-03-29>"
  4805. (org-test-with-temp-text "* H"
  4806. (let ((org-adapt-indentation nil)
  4807. (org-last-inserted-timestamp nil))
  4808. (org-schedule nil "<2012-03-29 Tue>"))
  4809. (replace-regexp-in-string
  4810. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4811. nil nil 1))))
  4812. (should
  4813. (equal "* H\nSCHEDULED: <2014-03-04>"
  4814. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4815. (let ((org-adapt-indentation nil)
  4816. (org-last-inserted-timestamp nil))
  4817. (org-schedule nil "<2014-03-04 Thu>"))
  4818. (replace-regexp-in-string
  4819. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4820. nil nil 1))))
  4821. ;; Accept delta time, e.g., "+2d".
  4822. (should
  4823. (equal "* H\nSCHEDULED: <2015-03-04>"
  4824. (org-test-at-time "2014-03-04"
  4825. (org-test-with-temp-text "* H"
  4826. (let ((org-adapt-indentation nil)
  4827. (org-last-inserted-timestamp nil))
  4828. (org-schedule nil "+1y"))
  4829. (replace-regexp-in-string
  4830. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4831. ;; Preserve repeater.
  4832. (should
  4833. (equal "* H\nSCHEDULED: <2012-03-29 +2y>"
  4834. (org-test-with-temp-text "* H"
  4835. (let ((org-adapt-indentation nil)
  4836. (org-last-inserted-timestamp nil))
  4837. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4838. (replace-regexp-in-string
  4839. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4840. ;; Remove CLOSED keyword, if any.
  4841. (should
  4842. (equal "* H\nSCHEDULED: <2012-03-29>"
  4843. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4844. (let ((org-adapt-indentation nil)
  4845. (org-last-inserted-timestamp nil))
  4846. (org-schedule nil "<2012-03-29 Tue>"))
  4847. (replace-regexp-in-string
  4848. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4849. ;; With C-u argument, remove SCHEDULED keyword.
  4850. (should
  4851. (equal "* H\n"
  4852. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4853. (let ((org-adapt-indentation nil)
  4854. (org-last-inserted-timestamp nil))
  4855. (org-schedule '(4)))
  4856. (buffer-string))))
  4857. (should
  4858. (equal "* H"
  4859. (org-test-with-temp-text "* H"
  4860. (let ((org-adapt-indentation nil)
  4861. (org-last-inserted-timestamp nil))
  4862. (org-schedule '(4)))
  4863. (buffer-string))))
  4864. ;; With C-u C-u argument, prompt for a delay cookie.
  4865. (should
  4866. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4867. (cl-letf (((symbol-function 'org-read-date)
  4868. (lambda (&rest args)
  4869. (apply #'encode-time
  4870. (org-parse-time-string "2014-03-04")))))
  4871. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4872. (let ((org-adapt-indentation nil)
  4873. (org-last-inserted-timestamp nil))
  4874. (org-schedule '(16)))
  4875. (buffer-string)))))
  4876. (should-error
  4877. (cl-letf (((symbol-function 'org-read-date)
  4878. (lambda (&rest args)
  4879. (apply #'encode-time
  4880. (org-parse-time-string "2014-03-04")))))
  4881. (org-test-with-temp-text "* H"
  4882. (let ((org-adapt-indentation nil)
  4883. (org-last-inserted-timestamp nil))
  4884. (org-schedule '(16)))
  4885. (buffer-string))))
  4886. ;; When a region is active and
  4887. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4888. ;; same value in all headlines in region.
  4889. (should
  4890. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  4891. (org-test-with-temp-text "* H1\n* H2"
  4892. (let ((org-adapt-indentation nil)
  4893. (org-last-inserted-timestamp nil)
  4894. (org-loop-over-headlines-in-active-region t))
  4895. (transient-mark-mode 1)
  4896. (push-mark (point) t t)
  4897. (goto-char (point-max))
  4898. (org-schedule nil "2012-03-29"))
  4899. (replace-regexp-in-string
  4900. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4901. (should-not
  4902. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  4903. (org-test-with-temp-text "* H1\n* H2"
  4904. (let ((org-adapt-indentation nil)
  4905. (org-last-inserted-timestamp nil)
  4906. (org-loop-over-headlines-in-active-region nil))
  4907. (transient-mark-mode 1)
  4908. (push-mark (point) t t)
  4909. (goto-char (point-max))
  4910. (org-schedule nil "2012-03-29"))
  4911. (replace-regexp-in-string
  4912. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4913. (should
  4914. ;; check if a repeater survives re-scheduling.
  4915. (string-match-p
  4916. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4917. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4918. (let ((org-adapt-indentation nil)
  4919. (org-last-inserted-timestamp nil))
  4920. (org-schedule nil "2017-02-01"))
  4921. (buffer-string)))))
  4922. ;;; Property API
  4923. (ert-deftest test-org/buffer-property-keys ()
  4924. "Test `org-buffer-property-keys' specifications."
  4925. ;; Retrieve properties across siblings.
  4926. (should
  4927. (equal '("A" "B")
  4928. (org-test-with-temp-text "
  4929. * H1
  4930. :PROPERTIES:
  4931. :A: 1
  4932. :END:
  4933. * H2
  4934. :PROPERTIES:
  4935. :B: 1
  4936. :END:"
  4937. (org-buffer-property-keys))))
  4938. ;; Retrieve properties across children.
  4939. (should
  4940. (equal '("A" "B")
  4941. (org-test-with-temp-text "
  4942. * H1
  4943. :PROPERTIES:
  4944. :A: 1
  4945. :END:
  4946. ** H2
  4947. :PROPERTIES:
  4948. :B: 1
  4949. :END:"
  4950. (org-buffer-property-keys))))
  4951. ;; Retrieve muliple properties in the same drawer.
  4952. (should
  4953. (equal '("A" "B")
  4954. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4955. (org-buffer-property-keys))))
  4956. ;; Ignore extension symbol in property name.
  4957. (should
  4958. (equal '("A")
  4959. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4960. (org-buffer-property-keys))))
  4961. ;; With non-nil COLUMNS, extract property names from columns.
  4962. (should
  4963. (equal '("A" "B")
  4964. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4965. (org-buffer-property-keys nil nil t))))
  4966. (should
  4967. (equal '("A" "B" "COLUMNS")
  4968. (org-test-with-temp-text
  4969. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4970. (org-buffer-property-keys nil nil t))))
  4971. ;; In COLUMNS, ignore title and summary-type.
  4972. (should
  4973. (equal '("A")
  4974. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  4975. (org-buffer-property-keys nil nil t))))
  4976. (should
  4977. (equal '("A")
  4978. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  4979. (org-buffer-property-keys nil nil t))))
  4980. (should
  4981. (equal '("A")
  4982. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  4983. (org-buffer-property-keys nil nil t)))))
  4984. (ert-deftest test-org/property-values ()
  4985. "Test `org-property-values' specifications."
  4986. ;; Regular test.
  4987. (should
  4988. (equal '("2" "1")
  4989. (org-test-with-temp-text
  4990. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4991. (org-property-values "A"))))
  4992. ;; Ignore empty values.
  4993. (should-not
  4994. (org-test-with-temp-text
  4995. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4996. (org-property-values "A")))
  4997. ;; Take into consideration extended values.
  4998. (should
  4999. (equal '("1 2")
  5000. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5001. (org-property-values "A")))))
  5002. (ert-deftest test-org/set-property ()
  5003. "Test `org-set-property' specifications."
  5004. (should
  5005. (equal
  5006. ":PROPERTIES:\n:TEST: t\n:END:\n"
  5007. (org-test-with-temp-text ""
  5008. (let ((org-property-format "%s %s"))
  5009. (org-set-property "TEST" "t"))
  5010. (buffer-string))))
  5011. (should
  5012. (equal
  5013. "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5014. (org-test-with-temp-text "* H"
  5015. (let ((org-adapt-indentation nil)
  5016. (org-property-format "%s %s"))
  5017. (org-set-property "TEST" "t"))
  5018. (buffer-string)))))
  5019. (ert-deftest test-org/delete-property ()
  5020. "Test `org-delete-property' specifications."
  5021. (should
  5022. (equal
  5023. ""
  5024. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5025. (org-delete-property "TEST")
  5026. (buffer-string))))
  5027. (should
  5028. (equal
  5029. ":PROPERTIES:\n:TEST1: t\n:END:\n"
  5030. (org-test-with-temp-text ":PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5031. (org-delete-property "TEST2")
  5032. (buffer-string))))
  5033. (should
  5034. (equal
  5035. "* H\n"
  5036. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5037. (org-delete-property "TEST")
  5038. (buffer-string))))
  5039. (should
  5040. (equal
  5041. "* H\n:PROPERTIES:\n:TEST1: t\n:END:\n"
  5042. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5043. (org-delete-property "TEST2")
  5044. (buffer-string)))))
  5045. (ert-deftest test-org/delete-property-globally ()
  5046. "Test `org-delete-property-global' specifications."
  5047. (should
  5048. (equal
  5049. ""
  5050. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5051. (org-delete-property-globally "TEST")
  5052. (buffer-string))))
  5053. (should
  5054. (equal
  5055. "* H\n"
  5056. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n* H\n:PROPERTIES:\n:TEST: nil\n:END:"
  5057. (org-delete-property-globally "TEST")
  5058. (buffer-string)))))
  5059. (ert-deftest test-org/find-property ()
  5060. "Test `org-find-property' specifications."
  5061. ;; Regular test.
  5062. (should
  5063. (= 1
  5064. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  5065. (org-find-property "prop"))))
  5066. ;; Find properties in top-level property drawer.
  5067. (should
  5068. (= 1
  5069. (org-test-with-temp-text ":PROPERTIES:\n:PROP: value\n:END:"
  5070. (org-find-property "prop"))))
  5071. ;; Ignore false positives.
  5072. (should
  5073. (= 27
  5074. (org-test-with-temp-text
  5075. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  5076. (org-find-property "A"))))
  5077. ;; Return first entry found in buffer.
  5078. (should
  5079. (= 1
  5080. (org-test-with-temp-text
  5081. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5082. (org-find-property "A"))))
  5083. ;; Only search visible part of the buffer.
  5084. (should
  5085. (= 31
  5086. (org-test-with-temp-text
  5087. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5088. (org-narrow-to-subtree)
  5089. (org-find-property "A"))))
  5090. ;; With optional argument, only find entries with a specific value.
  5091. (should-not
  5092. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5093. (org-find-property "A" "2")))
  5094. (should
  5095. (= 31
  5096. (org-test-with-temp-text
  5097. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5098. (org-find-property "A" "2"))))
  5099. ;; Use "nil" for explicit nil values.
  5100. (should
  5101. (= 31
  5102. (org-test-with-temp-text
  5103. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5104. (org-find-property "A" "nil")))))
  5105. (ert-deftest test-org/entry-delete ()
  5106. "Test `org-entry-delete' specifications."
  5107. ;; Regular test.
  5108. (should
  5109. (string-match
  5110. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5111. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5112. (org-entry-delete (point) "A")
  5113. (buffer-string))))
  5114. ;; Also remove accumulated properties.
  5115. (should-not
  5116. (string-match
  5117. ":A"
  5118. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5119. (org-entry-delete (point) "A")
  5120. (buffer-string))))
  5121. ;; When last property is removed, remove the property drawer.
  5122. (should-not
  5123. (string-match
  5124. ":PROPERTIES:"
  5125. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5126. (org-entry-delete (point) "A")
  5127. (buffer-string))))
  5128. ;; Return a non-nil value when some property was removed.
  5129. (should
  5130. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5131. (org-entry-delete (point) "A")))
  5132. (should-not
  5133. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5134. (org-entry-delete (point) "C")))
  5135. ;; Special properties cannot be located in a drawer. Allow to
  5136. ;; remove them anyway, in case of user error.
  5137. (should
  5138. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5139. (org-entry-delete (point) "SCHEDULED"))))
  5140. (ert-deftest test-org/entry-get ()
  5141. "Test `org-entry-get' specifications."
  5142. ;; Regular test.
  5143. (should
  5144. (equal "1"
  5145. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:"
  5146. (org-entry-get (point) "A"))))
  5147. (should
  5148. (equal "1"
  5149. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5150. (org-entry-get (point) "A"))))
  5151. ;; Ignore case.
  5152. (should
  5153. (equal "1"
  5154. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5155. (org-entry-get (point) "a"))))
  5156. ;; Handle extended values, both before and after base value.
  5157. (should
  5158. (equal "1 2 3"
  5159. (org-test-with-temp-text
  5160. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5161. (org-entry-get (point) "A"))))
  5162. ;; Empty values are returned as the empty string.
  5163. (should
  5164. (equal ""
  5165. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5166. (org-entry-get (point) "A"))))
  5167. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5168. ;; otherwise, return nil.
  5169. (should-not
  5170. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5171. (org-entry-get (point) "A")))
  5172. (should
  5173. (equal "nil"
  5174. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5175. (org-entry-get (point) "A" nil t))))
  5176. ;; Return nil when no property is found, independently on the
  5177. ;; LITERAL-NIL argument.
  5178. (should-not
  5179. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5180. (org-entry-get (point) "B")))
  5181. (should-not
  5182. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5183. (org-entry-get (point) "B" nil t)))
  5184. ;; Handle inheritance, when allowed. Include extended values and
  5185. ;; possibly global values.
  5186. (should
  5187. (equal
  5188. "1"
  5189. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:\n* H"
  5190. (org-entry-get (point-max) "A" t))))
  5191. (should
  5192. (equal
  5193. "1"
  5194. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  5195. (org-entry-get (point-max) "A" t))))
  5196. (should
  5197. (equal
  5198. "1"
  5199. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  5200. (let ((org-use-property-inheritance t))
  5201. (org-entry-get (point-max) "A" 'selective)))))
  5202. (should-not
  5203. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  5204. (let ((org-use-property-inheritance nil))
  5205. (org-entry-get (point-max) "A" 'selective))))
  5206. (should
  5207. (equal
  5208. "1 2"
  5209. (org-test-with-temp-text
  5210. ":PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  5211. (org-entry-get (point-max) "A" t))))
  5212. (should
  5213. (equal
  5214. "1 2"
  5215. (org-test-with-temp-text
  5216. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5217. (org-entry-get (point-max) "A" t))))
  5218. (should
  5219. (equal
  5220. "1 2"
  5221. (org-test-with-temp-text
  5222. ":PROPERTIES:\n:A: 1\n:END:\n* H1\n* H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5223. (org-entry-get (point-max) "A" t))))
  5224. (should
  5225. (equal
  5226. "1 2"
  5227. (org-test-with-temp-text
  5228. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2.1\n* H2.2\n:PROPERTIES:\n:A+: 2\n:END:"
  5229. (org-entry-get (point-max) "A" t))))
  5230. (should
  5231. (equal "1"
  5232. (org-test-with-temp-text
  5233. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  5234. (org-mode-restart)
  5235. (org-entry-get (point-max) "A" t))))
  5236. (should
  5237. (equal "0 1"
  5238. (org-test-with-temp-text
  5239. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5240. (org-mode-restart)
  5241. (org-entry-get (point-max) "A" t))))
  5242. ;; document level property-drawer has precedance over
  5243. ;; global-property by PROPERTY-keyword.
  5244. (should
  5245. (equal "0 2"
  5246. (org-test-with-temp-text
  5247. ":PROPERTIES:\n:A: 0\n:END:\n#+PROPERTY: A 1\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  5248. (org-mode-restart)
  5249. (org-entry-get (point-max) "A" t)))))
  5250. (ert-deftest test-org/entry-properties ()
  5251. "Test `org-entry-properties' specifications."
  5252. ;; Get "ITEM" property.
  5253. (should
  5254. (equal "H"
  5255. (org-test-with-temp-text "* TODO H"
  5256. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  5257. (should
  5258. (equal "H"
  5259. (org-test-with-temp-text "* TODO H"
  5260. (cdr (assoc "ITEM" (org-entry-properties))))))
  5261. ;; Get "TODO" property. TODO keywords are case sensitive.
  5262. (should
  5263. (equal "TODO"
  5264. (org-test-with-temp-text "* TODO H"
  5265. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  5266. (should
  5267. (equal "TODO"
  5268. (org-test-with-temp-text "* TODO H"
  5269. (cdr (assoc "TODO" (org-entry-properties))))))
  5270. (should-not
  5271. (org-test-with-temp-text "* H"
  5272. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5273. (should-not
  5274. (org-test-with-temp-text "* todo H"
  5275. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5276. ;; Get "PRIORITY" property.
  5277. (should
  5278. (equal "A"
  5279. (org-test-with-temp-text "* [#A] H"
  5280. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5281. (should
  5282. (equal "A"
  5283. (org-test-with-temp-text "* [#A] H"
  5284. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  5285. (should
  5286. (equal (char-to-string org-priority-default)
  5287. (org-test-with-temp-text "* H"
  5288. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5289. ;; Get "FILE" property.
  5290. (should
  5291. (org-test-with-temp-text-in-file "* H\nParagraph"
  5292. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  5293. (buffer-file-name))))
  5294. (should
  5295. (org-test-with-temp-text-in-file "* H\nParagraph"
  5296. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  5297. (buffer-file-name))))
  5298. (should-not
  5299. (org-test-with-temp-text "* H\nParagraph"
  5300. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  5301. ;; Get "TAGS" property.
  5302. (should
  5303. (equal ":tag1:tag2:"
  5304. (org-test-with-temp-text "* H :tag1:tag2:"
  5305. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  5306. (should
  5307. (equal ":tag1:tag2:"
  5308. (org-test-with-temp-text "* H :tag1:tag2:"
  5309. (cdr (assoc "TAGS" (org-entry-properties))))))
  5310. (should-not
  5311. (org-test-with-temp-text "* H"
  5312. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  5313. ;; Get "ALLTAGS" property.
  5314. (should
  5315. (equal ":tag1:tag2:"
  5316. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5317. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  5318. (should
  5319. (equal ":tag1:tag2:"
  5320. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5321. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  5322. (should-not
  5323. (org-test-with-temp-text "* H"
  5324. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  5325. ;; Get "BLOCKED" property.
  5326. (should
  5327. (equal "t"
  5328. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  5329. (let ((org-enforce-todo-dependencies t)
  5330. (org-blocker-hook
  5331. '(org-block-todo-from-children-or-siblings-or-parent)))
  5332. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5333. (should
  5334. (equal ""
  5335. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  5336. (let ((org-enforce-todo-dependencies t)
  5337. (org-blocker-hook
  5338. '(org-block-todo-from-children-or-siblings-or-parent)))
  5339. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5340. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  5341. (should
  5342. (equal
  5343. "[2012-03-29 thu.]"
  5344. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5345. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  5346. (should
  5347. (equal
  5348. "[2012-03-29 thu.]"
  5349. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5350. (cdr (assoc "CLOSED" (org-entry-properties))))))
  5351. (should-not
  5352. (org-test-with-temp-text "* H"
  5353. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  5354. (should
  5355. (equal
  5356. "<2014-03-04 tue.>"
  5357. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5358. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  5359. (should
  5360. (equal
  5361. "<2014-03-04 tue.>"
  5362. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5363. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  5364. (should-not
  5365. (org-test-with-temp-text "* H"
  5366. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  5367. (should
  5368. (equal
  5369. "<2014-03-04 tue.>"
  5370. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5371. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  5372. (should
  5373. (equal
  5374. "<2014-03-04 tue.>"
  5375. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5376. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  5377. (should-not
  5378. (org-test-with-temp-text "* H"
  5379. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  5380. ;; Get "CATEGORY"
  5381. (should
  5382. (equal "cat"
  5383. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5384. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  5385. (should
  5386. (equal "cat"
  5387. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5388. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5389. (should
  5390. (equal "cat"
  5391. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  5392. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5393. (should
  5394. (equal "cat2"
  5395. (org-test-with-temp-text
  5396. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5397. "\n"
  5398. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  5399. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5400. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  5401. (should
  5402. (equal "<2012-03-29 thu.>"
  5403. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5404. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5405. (should
  5406. (equal "[2012-03-29 thu.]"
  5407. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5408. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5409. (should
  5410. (equal "<2012-03-29 thu.>"
  5411. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5412. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5413. (should
  5414. (equal "[2014-03-04 tue.]"
  5415. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5416. (cdr (assoc "TIMESTAMP_IA"
  5417. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5418. (should-not
  5419. (equal "<2012-03-29 thu.>"
  5420. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5421. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5422. ;; Get standard properties.
  5423. (should
  5424. (equal "1"
  5425. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5426. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5427. ;; Handle extended properties.
  5428. (should
  5429. (equal "1 2 3"
  5430. (org-test-with-temp-text
  5431. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5432. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5433. (should
  5434. (equal "1 2 3"
  5435. (org-test-with-temp-text
  5436. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5437. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5438. ;; Ignore forbidden (special) properties.
  5439. (should-not
  5440. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5441. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5442. (ert-deftest test-org/entry-put ()
  5443. "Test `org-entry-put' specifications."
  5444. ;; Error when not a string or nil.
  5445. (should-error
  5446. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5447. (org-entry-put 1 "test" 2)))
  5448. ;; Error when property name is invalid.
  5449. (should-error
  5450. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5451. (org-entry-put 1 "no space" "value")))
  5452. (should-error
  5453. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5454. (org-entry-put 1 "" "value")))
  5455. ;; Set "TODO" property.
  5456. (should
  5457. (string-match (regexp-quote " TODO H")
  5458. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5459. (org-entry-put (point) "TODO" "TODO")
  5460. (buffer-string))))
  5461. (should
  5462. (string-match (regexp-quote "* H")
  5463. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5464. (org-entry-put (point) "TODO" nil)
  5465. (buffer-string))))
  5466. ;; Set "PRIORITY" property.
  5467. (should
  5468. (equal "* [#A] H"
  5469. (org-test-with-temp-text "* [#B] H"
  5470. (org-entry-put (point) "PRIORITY" "A")
  5471. (buffer-string))))
  5472. (should
  5473. (equal "* H"
  5474. (org-test-with-temp-text "* [#B] H"
  5475. (org-entry-put (point) "PRIORITY" nil)
  5476. (buffer-string))))
  5477. ;; Set "SCHEDULED" property.
  5478. (should
  5479. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5480. (org-test-with-temp-text "* H"
  5481. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5482. (buffer-string))))
  5483. (should
  5484. (string= "* H\n"
  5485. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5486. (org-entry-put (point) "SCHEDULED" nil)
  5487. (buffer-string))))
  5488. (should
  5489. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5490. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5491. (org-entry-put (point) "SCHEDULED" "earlier")
  5492. (buffer-string))))
  5493. (should
  5494. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5495. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5496. (org-entry-put (point) "SCHEDULED" "later")
  5497. (buffer-string))))
  5498. ;; Set "DEADLINE" property.
  5499. (should
  5500. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5501. (org-test-with-temp-text "* H"
  5502. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5503. (buffer-string))))
  5504. (should
  5505. (string= "* H\n"
  5506. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5507. (org-entry-put (point) "DEADLINE" nil)
  5508. (buffer-string))))
  5509. (should
  5510. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5511. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5512. (org-entry-put (point) "DEADLINE" "earlier")
  5513. (buffer-string))))
  5514. (should
  5515. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5516. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5517. (org-entry-put (point) "DEADLINE" "later")
  5518. (buffer-string))))
  5519. ;; Set "CATEGORY" property
  5520. (should
  5521. (string-match "^ *:CATEGORY: cat"
  5522. (org-test-with-temp-text "* H"
  5523. (org-entry-put (point) "CATEGORY" "cat")
  5524. (buffer-string))))
  5525. ;; Regular properties, with or without pre-existing drawer.
  5526. (should
  5527. (string-match "^ *:A: +2$"
  5528. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5529. (org-entry-put (point) "A" "2")
  5530. (buffer-string))))
  5531. (should
  5532. (string-match "^ *:A: +1$"
  5533. (org-test-with-temp-text "* H"
  5534. (org-entry-put (point) "A" "1")
  5535. (buffer-string))))
  5536. ;; Special case: two consecutive headlines.
  5537. (should
  5538. (string-match "\\* A\n *:PROPERTIES:"
  5539. (org-test-with-temp-text "* A\n** B"
  5540. (org-entry-put (point) "A" "1")
  5541. (buffer-string)))))
  5542. (ert-deftest test-org/refresh-properties ()
  5543. "Test `org-refresh-properties' specifications."
  5544. (should
  5545. (equal "1"
  5546. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5547. (org-refresh-properties "A" 'org-test)
  5548. (get-text-property (point) 'org-test))))
  5549. (should-not
  5550. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5551. (org-refresh-properties "B" 'org-test)
  5552. (get-text-property (point) 'org-test)))
  5553. ;; Handle properties only defined with extension syntax, i.e.,
  5554. ;; "PROPERTY+".
  5555. (should
  5556. (equal "1"
  5557. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5558. (org-refresh-properties "A" 'org-test)
  5559. (get-text-property (point) 'org-test))))
  5560. ;; When property is inherited, add text property to the whole
  5561. ;; sub-tree.
  5562. (should
  5563. (equal "1"
  5564. (org-test-with-temp-text
  5565. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5566. (let ((org-use-property-inheritance t))
  5567. (org-refresh-properties "A" 'org-test))
  5568. (get-text-property (point) 'org-test))))
  5569. ;; When a document level property-drawer is used, those properties
  5570. ;; should work exactly like headline-properties as if at a
  5571. ;; headline-level 0.
  5572. (should
  5573. (equal "1"
  5574. (org-test-with-temp-text
  5575. ":PROPERTIES:\n:A: 1\n:END:\n"
  5576. (org-mode-restart)
  5577. (let ((org-use-property-inheritance t))
  5578. (org-refresh-properties "A" 'org-test))
  5579. (get-text-property (point) 'org-test))))
  5580. (should-not
  5581. (equal "1"
  5582. (org-test-with-temp-text
  5583. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  5584. (org-mode-restart)
  5585. (let ((org-use-property-inheritance nil))
  5586. (org-refresh-properties "A" 'org-test))
  5587. (get-text-property (point) 'org-test))))
  5588. (should
  5589. (equal "1"
  5590. (org-test-with-temp-text
  5591. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  5592. (org-mode-restart)
  5593. (let ((org-use-property-inheritance t))
  5594. (org-refresh-properties "A" 'org-test))
  5595. (get-text-property (point) 'org-test))))
  5596. (should
  5597. (equal "2"
  5598. (org-test-with-temp-text
  5599. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1\n:PROPERTIES:\n:A: 2\n:END:"
  5600. (org-mode-restart)
  5601. (let ((org-use-property-inheritance t))
  5602. (org-refresh-properties "A" 'org-test))
  5603. (get-text-property (point) 'org-test))))
  5604. ;; When property is inherited, use global value across the whole
  5605. ;; buffer. However local values have precedence, as well as the
  5606. ;; document level property-drawer.
  5607. (should-not
  5608. (equal "1"
  5609. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5610. (org-mode-restart)
  5611. (let ((org-use-property-inheritance nil))
  5612. (org-refresh-properties "A" 'org-test))
  5613. (get-text-property (point) 'org-test))))
  5614. (should
  5615. (equal "1"
  5616. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5617. (org-mode-restart)
  5618. (let ((org-use-property-inheritance t))
  5619. (org-refresh-properties "A" 'org-test))
  5620. (get-text-property (point) 'org-test))))
  5621. (should
  5622. (equal "2"
  5623. (org-test-with-temp-text
  5624. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5625. (org-mode-restart)
  5626. (let ((org-use-property-inheritance t))
  5627. (org-refresh-properties "A" 'org-test))
  5628. (get-text-property (point) 'org-test))))
  5629. ;; When both keyword-property and document-level property-block is
  5630. ;; defined, the property-block has precedance.
  5631. (should
  5632. (equal "1"
  5633. (org-test-with-temp-text
  5634. ":PROPERTIES:\n:A: 1\n:END:\n#+PROPERTY: A 2\n<point>* H1"
  5635. (org-mode-restart)
  5636. (let ((org-use-property-inheritance t))
  5637. (org-refresh-properties "A" 'org-test))
  5638. (get-text-property (point) 'org-test)))))
  5639. (ert-deftest test-org/refresh-category-properties ()
  5640. "Test `org-refresh-category-properties' specifications"
  5641. (should
  5642. (equal "cat1"
  5643. (org-test-with-temp-text
  5644. ":PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5645. (org-refresh-category-properties)
  5646. (get-text-property (point) 'org-category))))
  5647. (should
  5648. (equal "cat1"
  5649. (org-test-with-temp-text
  5650. "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5651. (org-refresh-category-properties)
  5652. (get-text-property (point) 'org-category))))
  5653. ;; Even though property-inheritance is deactivated, category
  5654. ;; property should be inherited. As described in
  5655. ;; `org-use-property-inheritance'.
  5656. (should
  5657. (equal "cat1"
  5658. (org-test-with-temp-text
  5659. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  5660. (org-mode-restart)
  5661. (let ((org-use-property-inheritance nil))
  5662. (org-refresh-category-properties))
  5663. (get-text-property (point) 'org-category))))
  5664. (should
  5665. (equal "cat1"
  5666. (org-test-with-temp-text
  5667. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  5668. (org-mode-restart)
  5669. (let ((org-use-property-inheritance t))
  5670. (org-refresh-category-properties))
  5671. (get-text-property (point) 'org-category))))
  5672. (should
  5673. (equal "cat2"
  5674. (org-test-with-temp-text
  5675. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H\n:PROPERTIES:\n:CATEGORY: cat2\n:END:\n"
  5676. (org-mode-restart)
  5677. (let ((org-use-property-inheritance t))
  5678. (org-refresh-category-properties))
  5679. (get-text-property (point) 'org-category)))))
  5680. ;;; Refile
  5681. (ert-deftest test-org/refile-get-targets ()
  5682. "Test `org-refile-get-targets' specifications."
  5683. ;; :maxlevel includes all headings above specified value.
  5684. (should
  5685. (equal '("H1" "H2" "H3")
  5686. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5687. (let ((org-refile-use-outline-path nil)
  5688. (org-refile-targets `((nil :maxlevel . 3))))
  5689. (mapcar #'car (org-refile-get-targets))))))
  5690. (should
  5691. (equal '("H1" "H2")
  5692. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5693. (let ((org-refile-use-outline-path nil)
  5694. (org-refile-targets `((nil :maxlevel . 2))))
  5695. (mapcar #'car (org-refile-get-targets))))))
  5696. ;; :level limits targets to headlines with the specified level.
  5697. (should
  5698. (equal '("H2")
  5699. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5700. (let ((org-refile-use-outline-path nil)
  5701. (org-refile-targets `((nil :level . 2))))
  5702. (mapcar #'car (org-refile-get-targets))))))
  5703. ;; :tag limits targets to headlines with specified tag.
  5704. (should
  5705. (equal '("H1")
  5706. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5707. (let ((org-refile-use-outline-path nil)
  5708. (org-refile-targets `((nil :tag . "foo"))))
  5709. (mapcar #'car (org-refile-get-targets))))))
  5710. ;; :todo limits targets to headlines with specified TODO keyword.
  5711. (should
  5712. (equal '("H2")
  5713. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5714. (let ((org-refile-use-outline-path nil)
  5715. (org-refile-targets `((nil :todo . "TODO"))))
  5716. (mapcar #'car (org-refile-get-targets))))))
  5717. ;; :regexp filters targets matching provided regexp.
  5718. (should
  5719. (equal '("F2" "F3")
  5720. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5721. (let ((org-refile-use-outline-path nil)
  5722. (org-refile-targets `((nil :regexp . "F"))))
  5723. (mapcar #'car (org-refile-get-targets))))))
  5724. ;; A nil `org-refile-targets' includes only top level headlines in
  5725. ;; current buffer.
  5726. (should
  5727. (equal '("H1" "H2")
  5728. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5729. (let ((org-refile-use-outline-path nil)
  5730. (org-refile-targets nil))
  5731. (mapcar #'car (org-refile-get-targets))))))
  5732. ;; Return value is the union of the targets according to all the
  5733. ;; defined rules. However, prevent duplicates.
  5734. (should
  5735. (equal '("F2" "F3" "H1")
  5736. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5737. (let ((org-refile-use-outline-path nil)
  5738. (org-refile-targets `((nil :regexp . "F")
  5739. (nil :todo . "TODO"))))
  5740. (mapcar #'car (org-refile-get-targets))))))
  5741. (should
  5742. (equal '("F2" "F3" "H1")
  5743. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5744. (let ((org-refile-use-outline-path nil)
  5745. (org-refile-targets `((nil :regexp . "F")
  5746. (nil :todo . "TODO"))))
  5747. (mapcar #'car (org-refile-get-targets))))))
  5748. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5749. ;; paths.
  5750. (should
  5751. (equal '("H1" "H1/H2" "H1/H2/H3")
  5752. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5753. (let ((org-refile-use-outline-path t)
  5754. (org-refile-targets `((nil :maxlevel . 3))))
  5755. (mapcar #'car (org-refile-get-targets))))))
  5756. ;; When providing targets as paths, escape forward slashes in
  5757. ;; headings with backslashes.
  5758. (should
  5759. (equal '("H1\\/foo")
  5760. (org-test-with-temp-text "* H1/foo"
  5761. (let ((org-refile-use-outline-path t)
  5762. (org-refile-targets `((nil :maxlevel . 1))))
  5763. (mapcar #'car (org-refile-get-targets))))))
  5764. ;; When `org-refile-use-outline-path' is `file', include file name
  5765. ;; without directory in targets.
  5766. (should
  5767. (org-test-with-temp-text-in-file "* H1"
  5768. (let* ((filename (buffer-file-name))
  5769. (org-refile-use-outline-path 'file)
  5770. (org-refile-targets `(((,filename) :level . 1))))
  5771. (member (file-name-nondirectory filename)
  5772. (mapcar #'car (org-refile-get-targets))))))
  5773. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5774. ;; full file name.
  5775. (should
  5776. (org-test-with-temp-text-in-file "* H1"
  5777. (let* ((filename (file-truename (buffer-file-name)))
  5778. (org-refile-use-outline-path 'full-file-path)
  5779. (org-refile-targets `(((,filename) :level . 1))))
  5780. (member filename (mapcar #'car (org-refile-get-targets))))))
  5781. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5782. ;; buffer name.
  5783. (should
  5784. (org-test-with-temp-text "* H1"
  5785. (let* ((org-refile-use-outline-path 'buffer-name)
  5786. (org-refile-targets `((nil :level . 1))))
  5787. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5788. ;;; Sparse trees
  5789. (ert-deftest test-org/match-sparse-tree ()
  5790. "Test `org-match-sparse-tree' specifications."
  5791. ;; Match tags.
  5792. (should-not
  5793. (org-test-with-temp-text "* H\n** H1 :tag:"
  5794. (org-match-sparse-tree nil "tag")
  5795. (search-forward "H1")
  5796. (org-invisible-p2)))
  5797. (should
  5798. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5799. (org-match-sparse-tree nil "tag")
  5800. (search-forward "H2")
  5801. (org-invisible-p2)))
  5802. ;; "-" operator for tags.
  5803. (should-not
  5804. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5805. (org-match-sparse-tree nil "tag1-tag2")
  5806. (search-forward "H1")
  5807. (org-invisible-p2)))
  5808. (should
  5809. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5810. (org-match-sparse-tree nil "tag1-tag2")
  5811. (search-forward "H2")
  5812. (org-invisible-p2)))
  5813. ;; "&" operator for tags.
  5814. (should
  5815. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5816. (org-match-sparse-tree nil "tag1&tag2")
  5817. (search-forward "H1")
  5818. (org-invisible-p2)))
  5819. (should-not
  5820. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5821. (org-match-sparse-tree nil "tag1&tag2")
  5822. (search-forward "H2")
  5823. (org-invisible-p2)))
  5824. ;; "|" operator for tags.
  5825. (should-not
  5826. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5827. (org-match-sparse-tree nil "tag1|tag2")
  5828. (search-forward "H1")
  5829. (org-invisible-p2)))
  5830. (should-not
  5831. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5832. (org-match-sparse-tree nil "tag1|tag2")
  5833. (search-forward "H2")
  5834. (org-invisible-p2)))
  5835. ;; Regexp match on tags.
  5836. (should-not
  5837. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5838. (org-match-sparse-tree nil "{^tag.*}")
  5839. (search-forward "H1")
  5840. (org-invisible-p2)))
  5841. (should
  5842. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5843. (org-match-sparse-tree nil "{^tag.*}")
  5844. (search-forward "H2")
  5845. (org-invisible-p2)))
  5846. ;; Match group tags.
  5847. (should-not
  5848. (org-test-with-temp-text
  5849. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5850. (org-match-sparse-tree nil "work")
  5851. (search-forward "H1")
  5852. (org-invisible-p2)))
  5853. (should-not
  5854. (org-test-with-temp-text
  5855. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5856. (org-match-sparse-tree nil "work")
  5857. (search-forward "H2")
  5858. (org-invisible-p2)))
  5859. ;; Match group tags with hard brackets.
  5860. (should-not
  5861. (org-test-with-temp-text
  5862. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5863. (org-match-sparse-tree nil "work")
  5864. (search-forward "H1")
  5865. (org-invisible-p2)))
  5866. (should-not
  5867. (org-test-with-temp-text
  5868. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5869. (org-match-sparse-tree nil "work")
  5870. (search-forward "H2")
  5871. (org-invisible-p2)))
  5872. ;; Match tags in hierarchies
  5873. (should-not
  5874. (org-test-with-temp-text
  5875. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5876. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5877. #+TAGS: { Lev_3 : Lev_4 }\n
  5878. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5879. (org-match-sparse-tree nil "Lev_1")
  5880. (search-forward "H4")
  5881. (org-invisible-p2)))
  5882. (should-not
  5883. (org-test-with-temp-text
  5884. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5885. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5886. #+TAGS: { Lev_3 : Lev_4 }\n
  5887. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5888. (org-match-sparse-tree nil "Lev_1+Lev_3")
  5889. (search-forward "H4")
  5890. (org-invisible-p2)))
  5891. ;; Match regular expressions in tags
  5892. (should-not
  5893. (org-test-with-temp-text
  5894. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5895. (org-match-sparse-tree nil "Lev")
  5896. (search-forward "H1")
  5897. (org-invisible-p2)))
  5898. (should
  5899. (org-test-with-temp-text
  5900. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5901. (org-match-sparse-tree nil "Lev")
  5902. (search-forward "H1")
  5903. (org-invisible-p2)))
  5904. ;; Match properties.
  5905. (should
  5906. (org-test-with-temp-text
  5907. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5908. (org-match-sparse-tree nil "A=\"1\"")
  5909. (search-forward "H2")
  5910. (org-invisible-p2)))
  5911. (should-not
  5912. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5913. (org-match-sparse-tree nil "A=\"1\"")
  5914. (search-forward "H2")
  5915. (org-invisible-p2)))
  5916. ;; Case is not significant when matching properties.
  5917. (should-not
  5918. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5919. (org-match-sparse-tree nil "a=\"1\"")
  5920. (search-forward "H2")
  5921. (org-invisible-p2)))
  5922. (should-not
  5923. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5924. (org-match-sparse-tree nil "A=\"1\"")
  5925. (search-forward "H2")
  5926. (org-invisible-p2)))
  5927. ;; Match special LEVEL property.
  5928. (should-not
  5929. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5930. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5931. (search-forward "H1")
  5932. (org-invisible-p2)))
  5933. (should
  5934. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5935. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5936. (search-forward "H2")
  5937. (org-invisible-p2)))
  5938. ;; Comparison operators when matching properties.
  5939. (should
  5940. (org-test-with-temp-text
  5941. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5942. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5943. (search-forward "H1")
  5944. (org-invisible-p2)))
  5945. (should-not
  5946. (org-test-with-temp-text
  5947. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5948. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5949. (search-forward "H2")
  5950. (org-invisible-p2)))
  5951. ;; Regexp match on properties values.
  5952. (should-not
  5953. (org-test-with-temp-text
  5954. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5955. (org-match-sparse-tree nil "A={f.*}")
  5956. (search-forward "H1")
  5957. (org-invisible-p2)))
  5958. (should
  5959. (org-test-with-temp-text
  5960. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5961. (org-match-sparse-tree nil "A={f.*}")
  5962. (search-forward "H2")
  5963. (org-invisible-p2)))
  5964. ;; With an optional argument, limit match to TODO entries.
  5965. (should-not
  5966. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5967. (org-match-sparse-tree t "tag")
  5968. (search-forward "H1")
  5969. (org-invisible-p2)))
  5970. (should
  5971. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5972. (org-match-sparse-tree t "tag")
  5973. (search-forward "H2")
  5974. (org-invisible-p2))))
  5975. (ert-deftest test-org/occur ()
  5976. "Test `org-occur' specifications."
  5977. ;; Count number of matches.
  5978. (should
  5979. (= 1
  5980. (org-test-with-temp-text "* H\nA\n* H2"
  5981. (org-occur "A"))))
  5982. (should
  5983. (= 2
  5984. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5985. (org-occur "A"))))
  5986. ;; Test CALLBACK optional argument.
  5987. (should
  5988. (= 0
  5989. (org-test-with-temp-text "* H\nA\n* H2"
  5990. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5991. (should
  5992. (= 1
  5993. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5994. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5995. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5996. (should
  5997. (= 2
  5998. (org-test-with-temp-text "Aa"
  5999. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  6000. (should
  6001. (= 2
  6002. (org-test-with-temp-text "Aa"
  6003. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  6004. (should
  6005. (= 1
  6006. (org-test-with-temp-text "Aa"
  6007. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  6008. (should
  6009. (= 1
  6010. (org-test-with-temp-text "Aa"
  6011. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  6012. (should
  6013. (= 1
  6014. (org-test-with-temp-text "Aa"
  6015. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  6016. (should
  6017. (= 2
  6018. (org-test-with-temp-text "Aa"
  6019. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  6020. ;;; Tags
  6021. (ert-deftest test-org/tag-string-to-alist ()
  6022. "Test `org-tag-string-to-alist' specifications."
  6023. ;; Tag without selection key.
  6024. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  6025. ;; Tag with selection key.
  6026. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  6027. ;; Tag group.
  6028. (should
  6029. (equal
  6030. (org-tag-string-to-alist "[ group : t1 t2 ]")
  6031. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  6032. ;; Mutually exclusive tags.
  6033. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  6034. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  6035. (should
  6036. (equal
  6037. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  6038. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  6039. (ert-deftest test-org/tag-alist-to-string ()
  6040. "Test `org-tag-alist-to-string' specifications."
  6041. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  6042. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  6043. (should
  6044. (equal
  6045. (org-tag-alist-to-string
  6046. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6047. "[ group : t1 t2 ]"))
  6048. (should
  6049. (equal (org-tag-alist-to-string
  6050. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  6051. "{ tag1 tag2 }"))
  6052. (should
  6053. (equal
  6054. (org-tag-alist-to-string
  6055. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  6056. "{ group : tag1 tag2 }")))
  6057. (ert-deftest test-org/tag-alist-to-groups ()
  6058. "Test `org-tag-alist-to-groups' specifications."
  6059. (should
  6060. (equal (org-tag-alist-to-groups
  6061. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  6062. '(("group" "t1" "t2"))))
  6063. (should
  6064. (equal
  6065. (org-tag-alist-to-groups
  6066. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6067. '(("group" "t1" "t2"))))
  6068. (should-not
  6069. (org-tag-alist-to-groups
  6070. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  6071. (ert-deftest test-org/tag-align ()
  6072. "Test tags alignment."
  6073. ;; Test aligning tags with different display width.
  6074. (should
  6075. ;; 12345678901234567890
  6076. (equal "* Test :abc:"
  6077. (org-test-with-temp-text "* Test :abc:"
  6078. (let ((org-tags-column -20)
  6079. (indent-tabs-mode nil))
  6080. (org-fix-tags-on-the-fly))
  6081. (buffer-string))))
  6082. (should
  6083. ;; 12345678901234567890
  6084. (equal "* Test :日本語:"
  6085. (org-test-with-temp-text "* Test :日本語:"
  6086. (let ((org-tags-column -20)
  6087. (indent-tabs-mode nil))
  6088. (org-fix-tags-on-the-fly))
  6089. (buffer-string))))
  6090. ;; Make sure aligning tags do not skip invisible text.
  6091. (should
  6092. (equal "* [[linkx]] :tag:"
  6093. (org-test-with-temp-text "* [[link<point>]] :tag:"
  6094. (let ((org-tags-column 0))
  6095. (org-fix-tags-on-the-fly)
  6096. (insert "x")
  6097. (buffer-string)))))
  6098. ;; Aligning tags preserve position.
  6099. (should
  6100. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  6101. (let ((org-tags-column 78)
  6102. (indent-tabs-mode nil))
  6103. (org-fix-tags-on-the-fly))
  6104. (current-column))))
  6105. ;; Aligning all tags in visible buffer.
  6106. (should
  6107. ;; 12345678901234567890
  6108. (equal (concat "* Level 1 :abc:\n"
  6109. "** Level 2 :def:")
  6110. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  6111. "** Level 2 :def:")
  6112. (let ((org-tags-column -20)
  6113. (indent-tabs-mode nil))
  6114. ;; (org-align-tags :all) must work even when the point
  6115. ;; is at the end of the buffer.
  6116. (goto-char (point-max))
  6117. (org-align-tags :all))
  6118. (buffer-string)))))
  6119. (ert-deftest test-org/get-tags ()
  6120. "Test `org-get-tags' specifications."
  6121. ;; Standard test.
  6122. (should
  6123. (equal '("foo")
  6124. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  6125. (should
  6126. (equal '("foo" "bar")
  6127. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  6128. ;; Return nil when there is no tag.
  6129. (should-not
  6130. (org-test-with-temp-text "* Test" (org-get-tags)))
  6131. ;; Tags are inherited from parent headlines.
  6132. (should
  6133. (equal '("tag")
  6134. (let ((org-use-tag-inheritance t))
  6135. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  6136. (org-get-tags)))))
  6137. ;; Tags are inherited from `org-file-tags'.
  6138. (should
  6139. (equal '("tag")
  6140. (org-test-with-temp-text "* H1"
  6141. (let ((org-file-tags '("tag"))
  6142. (org-use-tag-inheritance t))
  6143. (org-get-tags)))))
  6144. ;; Only inherited tags have the `inherited' text property.
  6145. (should
  6146. (get-text-property 0 'inherited
  6147. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6148. (let ((org-use-tag-inheritance t))
  6149. (assoc-string "foo" (org-get-tags))))))
  6150. (should-not
  6151. (get-text-property 0 'inherited
  6152. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6153. (let ((org-use-tag-inheritance t))
  6154. (assoc-string "bar" (org-get-tags))))))
  6155. ;; Obey to `org-use-tag-inheritance'.
  6156. (should-not
  6157. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6158. (let ((org-use-tag-inheritance nil))
  6159. (assoc-string "foo" (org-get-tags)))))
  6160. (should-not
  6161. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6162. (let ((org-use-tag-inheritance nil)
  6163. (org-file-tags '("foo")))
  6164. (assoc-string "foo" (org-get-tags)))))
  6165. (should-not
  6166. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6167. (let ((org-use-tag-inheritance '("bar")))
  6168. (assoc-string "foo" (org-get-tags)))))
  6169. (should
  6170. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6171. (let ((org-use-tag-inheritance '("bar")))
  6172. (assoc-string "bar" (org-get-tags)))))
  6173. (should-not
  6174. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6175. (let ((org-use-tag-inheritance "b.*"))
  6176. (assoc-string "foo" (org-get-tags)))))
  6177. (should
  6178. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6179. (let ((org-use-tag-inheritance "b.*"))
  6180. (assoc-string "bar" (org-get-tags)))))
  6181. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  6182. (should
  6183. (equal '("baz")
  6184. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6185. (let ((org-use-tag-inheritance t))
  6186. (org-get-tags nil t)))))
  6187. ;; When optional argument POS is non-nil, get tags there instead.
  6188. (should
  6189. (equal '("foo")
  6190. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  6191. (org-get-tags 1))))
  6192. ;; Make sure tags excluded from inheritance are returned if local
  6193. (should
  6194. (equal '("foo")
  6195. (org-test-with-temp-text "* Test :foo:"
  6196. (let ((org-use-tag-inheritance t)
  6197. (org-tags-exclude-from-inheritance '("foo")))
  6198. (org-get-tags)))))
  6199. ;; Test the collection of tags from #+filetags and parent tags.
  6200. (should
  6201. (equal '("a" "b" "c" "d")
  6202. (org-test-with-temp-text (concat "#+filetags: a\n"
  6203. "* Level 1 :b:\n"
  6204. "** Level 2 :c:\n"
  6205. "*** Level 3 :d:\n"
  6206. "<point>")
  6207. (let ((org-use-tag-inheritance t))
  6208. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  6209. (org-get-tags)))))
  6210. ;; Pathological case: tagged headline with an empty body.
  6211. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  6212. (ert-deftest test-org/set-tags ()
  6213. "Test `org-set-tags' specifications."
  6214. ;; Throw an error on invalid data.
  6215. (should-error
  6216. (org-test-with-temp-text "* H"
  6217. (org-set-tags 'foo)))
  6218. ;; `nil', an empty, and a blank string remove all tags.
  6219. (should
  6220. (equal "* H"
  6221. (org-test-with-temp-text "* H :tag1:tag2:"
  6222. (org-set-tags nil)
  6223. (buffer-string))))
  6224. (should
  6225. (equal "* H"
  6226. (org-test-with-temp-text "* H :tag1:tag2:"
  6227. (org-set-tags "")
  6228. (buffer-string))))
  6229. (should
  6230. (equal "* H"
  6231. (org-test-with-temp-text "* H :tag1:tag2:"
  6232. (org-set-tags " ")
  6233. (buffer-string))))
  6234. ;; If there's nothing to remove, just bail out.
  6235. (should
  6236. (equal "* H"
  6237. (org-test-with-temp-text "* H"
  6238. (org-set-tags nil)
  6239. (buffer-string))))
  6240. (should
  6241. (equal "* "
  6242. (org-test-with-temp-text "* "
  6243. (org-set-tags nil)
  6244. (buffer-string))))
  6245. ;; If DATA is a tag string, set current tags to it, even if it means
  6246. ;; replacing old tags.
  6247. (should
  6248. (equal "* H :tag0:"
  6249. (org-test-with-temp-text "* H :tag1:tag2:"
  6250. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6251. (buffer-string))))
  6252. (should
  6253. (equal "* H :tag0:"
  6254. (org-test-with-temp-text "* H"
  6255. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6256. (buffer-string))))
  6257. ;; If DATA is a list, set tags to this list, even if it means
  6258. ;; replacing old tags.
  6259. (should
  6260. (equal "* H :tag0:"
  6261. (org-test-with-temp-text "* H :tag1:tag2:"
  6262. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6263. (buffer-string))))
  6264. (should
  6265. (equal "* H :tag0:"
  6266. (org-test-with-temp-text "* H"
  6267. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6268. (buffer-string))))
  6269. ;; When set, apply `org-tags-sort-function'.
  6270. (should
  6271. (equal "* H :a:b:"
  6272. (org-test-with-temp-text "* H"
  6273. (let ((org-tags-column 1)
  6274. (org-tags-sort-function #'string<))
  6275. (org-set-tags '("b" "a"))
  6276. (buffer-string)))))
  6277. ;; When new tags are identical to the previous ones, still align.
  6278. (should
  6279. (equal "* H :foo:"
  6280. (org-test-with-temp-text "* H :foo:"
  6281. (let ((org-tags-column 1))
  6282. (org-set-tags '("foo"))
  6283. (buffer-string)))))
  6284. ;; When tags have been changed, run `org-after-tags-change-hook'.
  6285. (should
  6286. (catch :return
  6287. (org-test-with-temp-text "* H :foo:"
  6288. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6289. (org-set-tags '("bar"))
  6290. nil))))
  6291. (should-not
  6292. (catch :return
  6293. (org-test-with-temp-text "* H :foo:"
  6294. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6295. (org-set-tags '("foo"))
  6296. nil))))
  6297. ;; Special case: handle empty headlines.
  6298. (should
  6299. (equal "* :tag0:"
  6300. (org-test-with-temp-text "* "
  6301. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6302. (buffer-string))))
  6303. ;; Modify buffer only when a tag change happens or alignment is
  6304. ;; done.
  6305. (should-not
  6306. (org-test-with-temp-text "* H :foo:"
  6307. (set-buffer-modified-p nil)
  6308. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  6309. (buffer-modified-p)))
  6310. (should
  6311. (org-test-with-temp-text "* H :foo:"
  6312. (set-buffer-modified-p nil)
  6313. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  6314. (buffer-modified-p)))
  6315. (should
  6316. (org-test-with-temp-text "* H :foo:"
  6317. (set-buffer-modified-p nil)
  6318. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  6319. (buffer-modified-p)))
  6320. ;; Pathological case: when setting tags of a folded headline, do not
  6321. ;; let new tags being sucked into invisibility.
  6322. (should-not
  6323. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  6324. ;; Show only headlines
  6325. (org-content)
  6326. ;; Set NEXT tag on current entry
  6327. (org-set-tags ":NEXT:")
  6328. ;; Move point to that NEXT tag
  6329. (search-forward "NEXT") (backward-word)
  6330. ;; And it should be visible (i.e. no overlays)
  6331. (overlays-at (point)))))
  6332. (ert-deftest test-org/set-tags-command ()
  6333. "Test `org-set-tags-command' specifications"
  6334. ;; Set tags at current headline.
  6335. (should
  6336. (equal "* H1 :foo:"
  6337. (org-test-with-temp-text "* H1"
  6338. (cl-letf (((symbol-function 'completing-read)
  6339. (lambda (&rest args) ":foo:")))
  6340. (let ((org-use-fast-tag-selection nil)
  6341. (org-tags-column 1))
  6342. (org-set-tags-command)))
  6343. (buffer-string))))
  6344. ;; Preserve position when called from the section below.
  6345. (should
  6346. (equal "* H1 :foo:\nContents"
  6347. (org-test-with-temp-text "* H1\n<point>Contents"
  6348. (cl-letf (((symbol-function 'completing-read)
  6349. (lambda (&rest args) ":foo:")))
  6350. (let ((org-use-fast-tag-selection nil)
  6351. (org-tags-column 1))
  6352. (org-set-tags-command)))
  6353. (buffer-string))))
  6354. (should-not
  6355. (equal "* H1 :foo:\nContents2"
  6356. (org-test-with-temp-text "* H1\n<point>Contents2"
  6357. (cl-letf (((symbol-function 'completing-read)
  6358. (lambda (&rest args) ":foo:")))
  6359. (let ((org-use-fast-tag-selection nil)
  6360. (org-tags-column 1))
  6361. (org-set-tags-command)))
  6362. (org-at-heading-p))))
  6363. ;; Strip all forbidden characters from user-entered tags.
  6364. (should
  6365. (equal "* H1 :foo:"
  6366. (org-test-with-temp-text "* H1"
  6367. (cl-letf (((symbol-function 'completing-read)
  6368. (lambda (&rest args) ": foo *:")))
  6369. (let ((org-use-fast-tag-selection nil)
  6370. (org-tags-column 1))
  6371. (org-set-tags-command)))
  6372. (buffer-string))))
  6373. ;; When a region is active and
  6374. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  6375. ;; same value in all headlines in region.
  6376. (should
  6377. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  6378. (org-test-with-temp-text "* H1\nContents\n* H2"
  6379. (cl-letf (((symbol-function 'completing-read)
  6380. (lambda (&rest args) ":foo:")))
  6381. (let ((org-use-fast-tag-selection nil)
  6382. (org-loop-over-headlines-in-active-region t)
  6383. (org-tags-column 1))
  6384. (transient-mark-mode 1)
  6385. (push-mark (point) t t)
  6386. (goto-char (point-max))
  6387. (org-set-tags-command)))
  6388. (buffer-string))))
  6389. (should
  6390. (equal "* H1\nContents\n* H2 :foo:"
  6391. (org-test-with-temp-text "* H1\nContents\n* H2"
  6392. (cl-letf (((symbol-function 'completing-read)
  6393. (lambda (&rest args) ":foo:")))
  6394. (let ((org-use-fast-tag-selection nil)
  6395. (org-loop-over-headlines-in-active-region nil)
  6396. (org-tags-column 1))
  6397. (transient-mark-mode 1)
  6398. (push-mark (point) t t)
  6399. (goto-char (point-max))
  6400. (org-set-tags-command)))
  6401. (buffer-string))))
  6402. ;; With a C-u prefix argument, align all tags in the buffer.
  6403. (should
  6404. (equal "* H1 :foo:\n* H2 :bar:"
  6405. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  6406. (let ((org-tags-column 1)) (org-set-tags-command '(4)))
  6407. (buffer-string))))
  6408. ;; Point does not move with empty headline.
  6409. (should
  6410. (equal ":foo:"
  6411. (org-test-with-temp-text "* <point>"
  6412. (cl-letf (((symbol-function 'completing-read)
  6413. (lambda (&rest args) ":foo:")))
  6414. (let ((org-use-fast-tag-selection nil)
  6415. (org-tags-column 1))
  6416. (org-set-tags-command)))
  6417. (buffer-substring (point) (line-end-position)))))
  6418. ;; Point does not move at start of line.
  6419. (should
  6420. (equal "* H1 :foo:"
  6421. (org-test-with-temp-text "* H1"
  6422. (cl-letf (((symbol-function 'completing-read)
  6423. (lambda (&rest args) ":foo:")))
  6424. (let ((org-use-fast-tag-selection nil)
  6425. (org-tags-column 1))
  6426. (org-set-tags-command)))
  6427. (buffer-substring (point) (line-end-position)))))
  6428. ;; Point does not move when within *'s.
  6429. (should
  6430. (equal "* H1 :foo:"
  6431. (org-test-with-temp-text "*<point>* H1"
  6432. (cl-letf (((symbol-function 'completing-read)
  6433. (lambda (&rest args) ":foo:")))
  6434. (let ((org-use-fast-tag-selection nil)
  6435. (org-tags-column 1))
  6436. (org-set-tags-command)))
  6437. (buffer-substring (point) (line-end-position)))))
  6438. ;; Point workaround does not get fooled when looking at a space.
  6439. (should
  6440. (equal " b :foo:"
  6441. (org-test-with-temp-text "* a<point> b"
  6442. (cl-letf (((symbol-function 'completing-read)
  6443. (lambda (&rest args) ":foo:")))
  6444. (let ((org-use-fast-tag-selection nil)
  6445. (org-tags-column 1))
  6446. (org-set-tags-command)))
  6447. (buffer-substring (point) (line-end-position))))))
  6448. (ert-deftest test-org/toggle-tag ()
  6449. "Test `org-toggle-tag' specifications."
  6450. ;; Insert missing tag.
  6451. (should
  6452. (equal "* H :tag:"
  6453. (org-test-with-temp-text "* H"
  6454. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  6455. (buffer-string))))
  6456. (should
  6457. (equal "* H :tag1:tag2:"
  6458. (org-test-with-temp-text "* H :tag1:"
  6459. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6460. (buffer-string))))
  6461. ;; Remove existing tag.
  6462. (should
  6463. (equal "* H"
  6464. (org-test-with-temp-text "* H :tag:"
  6465. (org-toggle-tag "tag")
  6466. (buffer-string))))
  6467. (should
  6468. (equal "* H :tag1:"
  6469. (org-test-with-temp-text "* H :tag1:tag2:"
  6470. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6471. (buffer-string))))
  6472. (should
  6473. (equal "* H :tag2:"
  6474. (org-test-with-temp-text "* H :tag1:tag2:"
  6475. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  6476. (buffer-string))))
  6477. ;; With optional argument ONOFF set to `on', try to insert the tag,
  6478. ;; even if its already there.
  6479. (should
  6480. (equal "* H :tag:"
  6481. (org-test-with-temp-text "* H"
  6482. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6483. (buffer-string))))
  6484. (should
  6485. (equal "* H :tag:"
  6486. (org-test-with-temp-text "* H :tag:"
  6487. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6488. (buffer-string))))
  6489. ;; With optional argument ONOFF set to `off', try to remove the tag,
  6490. ;; even if its not there.
  6491. (should
  6492. (equal "* H"
  6493. (org-test-with-temp-text "* H :tag:"
  6494. (org-toggle-tag "tag" 'off)
  6495. (buffer-string))))
  6496. (should
  6497. (equal "* H :tag:"
  6498. (org-test-with-temp-text "* H :tag:"
  6499. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  6500. (buffer-string))))
  6501. ;; Special case: Handle properly tag inheritance. In particular, do
  6502. ;; not set inherited tags.
  6503. (should
  6504. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  6505. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  6506. (let ((org-use-tag-inheritance t)
  6507. (org-tags-column 1))
  6508. (org-toggle-tag "tag"))
  6509. (buffer-string))))
  6510. (should
  6511. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  6512. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  6513. (let ((org-use-tag-inheritance t)
  6514. (org-tags-column 1))
  6515. (org-toggle-tag "foo"))
  6516. (buffer-string)))))
  6517. (ert-deftest test-org/tags-expand ()
  6518. "Test `org-tags-expand' specifications."
  6519. ;; Expand tag groups as a regexp enclosed within curly brackets.
  6520. (should
  6521. (equal "{\\<[ABC]\\>}"
  6522. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6523. (org-mode-restart)
  6524. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6525. (should
  6526. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  6527. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  6528. (org-mode-restart)
  6529. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  6530. ;; Preserve operator before the regexp.
  6531. (should
  6532. (equal "+{\\<[ABC]\\>}"
  6533. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6534. (org-mode-restart)
  6535. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  6536. (should
  6537. (equal "-{\\<[ABC]\\>}"
  6538. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6539. (org-mode-restart)
  6540. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  6541. ;; Handle "|" syntax.
  6542. (should
  6543. (equal "{\\<[ABC]\\>}|D"
  6544. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6545. (org-mode-restart)
  6546. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  6547. ;; Handle nested groups.
  6548. (should
  6549. (equal "{\\<[A-D]\\>}"
  6550. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  6551. (org-mode-restart)
  6552. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6553. ;; Expand multiple occurrences of the same group.
  6554. (should
  6555. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  6556. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6557. (org-mode-restart)
  6558. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  6559. ;; Preserve regexp matches.
  6560. (should
  6561. (equal "{A+}"
  6562. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6563. (org-mode-restart)
  6564. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  6565. ;;; TODO keywords
  6566. (ert-deftest test-org/auto-repeat-maybe ()
  6567. "Test `org-auto-repeat-maybe' specifications."
  6568. ;; Do not auto repeat when there is no valid time stamp with
  6569. ;; a repeater in the entry.
  6570. (should-not
  6571. (string-prefix-p
  6572. "* TODO H"
  6573. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6574. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  6575. (org-todo "DONE")
  6576. (buffer-string)))))
  6577. (should-not
  6578. (string-prefix-p
  6579. "* TODO H"
  6580. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6581. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  6582. (org-todo "DONE")
  6583. (buffer-string)))))
  6584. ;; When switching to DONE state, switch back to first TODO keyword
  6585. ;; in sequence, or the same keyword if they have different types.
  6586. (should
  6587. (string-prefix-p
  6588. "* TODO H"
  6589. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6590. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6591. (org-todo "DONE")
  6592. (buffer-string)))))
  6593. (should
  6594. (string-prefix-p
  6595. "* KWD1 H"
  6596. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6597. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6598. (org-todo "DONE")
  6599. (buffer-string)))))
  6600. (should
  6601. (string-prefix-p
  6602. "* KWD2 H"
  6603. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  6604. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6605. (org-todo "DONE")
  6606. (buffer-string)))))
  6607. ;; If there was no TODO keyword in the first place, do not insert
  6608. ;; any either.
  6609. (should
  6610. (string-prefix-p
  6611. "* H"
  6612. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6613. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  6614. (org-todo "DONE")
  6615. (buffer-string)))))
  6616. ;; Revert to REPEAT_TO_STATE, if set.
  6617. (should
  6618. (string-prefix-p
  6619. "* KWD2 H"
  6620. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6621. (org-test-with-temp-text
  6622. "* KWD2 H
  6623. :PROPERTIES:
  6624. :REPEAT_TO_STATE: KWD2
  6625. :END:
  6626. <2012-03-29 Thu +2y>"
  6627. (org-todo "DONE")
  6628. (buffer-string)))))
  6629. ;; When switching to DONE state, update base date. If there are
  6630. ;; multiple repeated time stamps, update them all.
  6631. (should
  6632. (string-match-p
  6633. "<2014-03-29 .* \\+2y>"
  6634. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6635. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6636. (org-todo "DONE")
  6637. (buffer-string)))))
  6638. (should
  6639. (string-match-p
  6640. "<2015-03-04 .* \\+1y>"
  6641. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6642. (org-test-with-temp-text
  6643. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  6644. (org-todo "DONE")
  6645. (buffer-string)))))
  6646. ;; Throw an error if repeater unit is the hour and no time is
  6647. ;; provided in the time-stamp.
  6648. (should-error
  6649. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6650. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  6651. (org-todo "DONE")
  6652. (buffer-string))))
  6653. ;; Handle every repeater type using hours step.
  6654. (should
  6655. (string-match-p
  6656. "2014-03-04 .* 02:00"
  6657. (org-test-at-time "<2014-03-04 02:35>"
  6658. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 +8h>"
  6659. (org-todo "DONE")
  6660. (buffer-string)))))
  6661. (should
  6662. (string-match-p
  6663. "2014-03-04 .* 10:00"
  6664. (org-test-at-time "<2014-03-04 02:35>"
  6665. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 ++8h>"
  6666. (org-todo "DONE")
  6667. (buffer-string)))))
  6668. (should
  6669. (string-match-p
  6670. "2014-03-04 .* 10:35"
  6671. (org-test-at-time "<2014-03-04 02:35>"
  6672. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 .+8h>"
  6673. (org-todo "DONE")
  6674. (buffer-string)))))
  6675. ;; Do not repeat inactive time stamps with a repeater.
  6676. (should-not
  6677. (string-match-p
  6678. "\\[2014-03-29 .* \\+2y\\]"
  6679. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6680. (org-test-with-temp-text
  6681. "* TODO H\n[2012-03-29 Thu. +2y]"
  6682. (org-todo "DONE")
  6683. (buffer-string)))))
  6684. ;; Do not repeat commented time stamps.
  6685. (should-not
  6686. (string-prefix-p
  6687. "<2015-03-04 .* \\+1y>"
  6688. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6689. (org-test-with-temp-text
  6690. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  6691. (org-todo "DONE")
  6692. (buffer-string)))))
  6693. (should-not
  6694. (string-prefix-p
  6695. "<2015-03-04 .* \\+1y>"
  6696. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6697. (org-test-with-temp-text
  6698. "* TODO H
  6699. <2012-03-29 Thu. +2y>
  6700. #+BEGIN_EXAMPLE
  6701. <2014-03-04 Tue +1y>
  6702. #+END_EXAMPLE"
  6703. (org-todo "DONE")
  6704. (buffer-string)))))
  6705. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  6706. ;; entry, record time of last repeat.
  6707. (should-not
  6708. (string-match-p
  6709. ":LAST_REPEAT:"
  6710. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6711. (org-log-repeat nil))
  6712. (cl-letf (((symbol-function 'org-add-log-setup)
  6713. (lambda (&rest args) nil)))
  6714. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6715. (org-todo "DONE")
  6716. (buffer-string))))))
  6717. (should
  6718. (string-match-p
  6719. ":LAST_REPEAT:"
  6720. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6721. (org-log-repeat t))
  6722. (cl-letf (((symbol-function 'org-add-log-setup)
  6723. (lambda (&rest args) nil)))
  6724. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6725. (org-todo "DONE")
  6726. (buffer-string))))))
  6727. (should
  6728. (string-match-p
  6729. ":LAST_REPEAT:"
  6730. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6731. (cl-letf (((symbol-function 'org-add-log-setup)
  6732. (lambda (&rest args) nil)))
  6733. (org-test-with-temp-text
  6734. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  6735. (org-todo "DONE")
  6736. (buffer-string))))))
  6737. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  6738. ;; the entry as it is no longer relevant.
  6739. (should-not
  6740. (string-match-p
  6741. "^SCHEDULED:"
  6742. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6743. (org-test-with-temp-text
  6744. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  6745. (org-todo "DONE")
  6746. (buffer-string)))))
  6747. ;; Properly advance repeater even when a clock entry is specified
  6748. ;; and `org-log-repeat' is nil.
  6749. (should
  6750. (string-match-p
  6751. "SCHEDULED: <2014-03-29"
  6752. (let ((org-log-repeat nil)
  6753. (org-todo-keywords '((sequence "TODO" "DONE"))))
  6754. (org-test-with-temp-text
  6755. "* TODO H
  6756. SCHEDULED: <2012-03-29 Thu +2y>
  6757. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  6758. (org-todo "DONE")
  6759. (buffer-string))))))
  6760. ;;; Timestamps API
  6761. (ert-deftest test-org/at-timestamp-p ()
  6762. "Test `org-at-timestamp-p' specifications."
  6763. (should
  6764. (org-test-with-temp-text "<2012-03-29 Thu>"
  6765. (org-at-timestamp-p)))
  6766. (should-not
  6767. (org-test-with-temp-text "2012-03-29 Thu"
  6768. (org-at-timestamp-p)))
  6769. ;; Test return values.
  6770. (should
  6771. (eq 'bracket
  6772. (org-test-with-temp-text "<2012-03-29 Thu>"
  6773. (org-at-timestamp-p))))
  6774. (should
  6775. (eq 'year
  6776. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  6777. (org-at-timestamp-p))))
  6778. (should
  6779. (eq 'month
  6780. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  6781. (org-at-timestamp-p))))
  6782. (should
  6783. (eq 'day
  6784. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  6785. (org-at-timestamp-p))))
  6786. (should
  6787. (eq 'day
  6788. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  6789. (org-at-timestamp-p))))
  6790. (should
  6791. (wholenump
  6792. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  6793. (org-at-timestamp-p))))
  6794. (should
  6795. (eq 'bracket
  6796. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  6797. (org-at-timestamp-p))))
  6798. (should
  6799. (eq 'after
  6800. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  6801. (org-at-timestamp-p))))
  6802. ;; Test `inactive' optional argument.
  6803. (should
  6804. (org-test-with-temp-text "[2012-03-29 Thu]"
  6805. (org-at-timestamp-p 'inactive)))
  6806. (should-not
  6807. (org-test-with-temp-text "[2012-03-29 Thu]"
  6808. (org-at-timestamp-p)))
  6809. ;; When optional argument is `agenda', recognize time-stamps in
  6810. ;; planning info line, property drawers and clocks.
  6811. (should
  6812. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6813. (org-at-timestamp-p 'agenda)))
  6814. (should-not
  6815. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6816. (org-at-timestamp-p)))
  6817. (should
  6818. (org-test-with-temp-text
  6819. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6820. (org-at-timestamp-p 'agenda)))
  6821. (should-not
  6822. (org-test-with-temp-text
  6823. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6824. (org-at-timestamp-p)))
  6825. (should
  6826. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6827. (let ((org-agenda-include-inactive-timestamps t))
  6828. (org-at-timestamp-p 'agenda))))
  6829. (should-not
  6830. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6831. (let ((org-agenda-include-inactive-timestamps t))
  6832. (org-at-timestamp-p))))
  6833. (should-not
  6834. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6835. (let ((org-agenda-include-inactive-timestamps t))
  6836. (org-at-timestamp-p 'inactive))))
  6837. ;; When optional argument is `lax', match any part of the document
  6838. ;; with Org timestamp syntax.
  6839. (should
  6840. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6841. (org-at-timestamp-p 'lax)))
  6842. (should-not
  6843. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6844. (org-at-timestamp-p)))
  6845. (should
  6846. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6847. (org-at-timestamp-p 'lax)))
  6848. (should-not
  6849. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6850. (org-at-timestamp-p)))
  6851. (should
  6852. (org-test-with-temp-text
  6853. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6854. (org-at-timestamp-p 'lax)))
  6855. (should-not
  6856. (org-test-with-temp-text
  6857. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6858. (org-at-timestamp-p)))
  6859. ;; Optional argument `lax' also matches inactive timestamps.
  6860. (should
  6861. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  6862. (org-at-timestamp-p 'lax))))
  6863. (ert-deftest test-org/time-stamp ()
  6864. "Test `org-time-stamp' specifications."
  6865. ;; Insert chosen time stamp at point.
  6866. (should
  6867. (string-match
  6868. "Te<2014-03-04 .*?>xt"
  6869. (org-test-with-temp-text "Te<point>xt"
  6870. (cl-letf (((symbol-function 'org-read-date)
  6871. (lambda (&rest args)
  6872. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6873. (org-time-stamp nil)
  6874. (buffer-string)))))
  6875. ;; With a prefix argument, also insert time.
  6876. (should
  6877. (string-match
  6878. "Te<2014-03-04 .*? 00:41>xt"
  6879. (org-test-with-temp-text "Te<point>xt"
  6880. (cl-letf (((symbol-function 'org-read-date)
  6881. (lambda (&rest args)
  6882. (apply #'encode-time
  6883. (org-parse-time-string "2014-03-04 00:41")))))
  6884. (org-time-stamp '(4))
  6885. (buffer-string)))))
  6886. ;; With two universal prefix arguments, insert an active timestamp
  6887. ;; with the current time without prompting the user.
  6888. (should
  6889. (string-match
  6890. "Te<2014-03-04 .*? 00:41>xt"
  6891. (org-test-with-temp-text "Te<point>xt"
  6892. (org-test-at-time "2014-03-04 00:41"
  6893. (org-time-stamp '(16))
  6894. (buffer-string)))))
  6895. ;; When optional argument is non-nil, insert an inactive timestamp.
  6896. (should
  6897. (string-match
  6898. "Te\\[2014-03-04 .*?\\]xt"
  6899. (org-test-with-temp-text "Te<point>xt"
  6900. (cl-letf (((symbol-function 'org-read-date)
  6901. (lambda (&rest args)
  6902. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6903. (org-time-stamp nil t)
  6904. (buffer-string)))))
  6905. ;; When called from a timestamp, replace existing one.
  6906. (should
  6907. (string-match
  6908. "<2014-03-04 .*?>"
  6909. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  6910. (cl-letf (((symbol-function 'org-read-date)
  6911. (lambda (&rest args)
  6912. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6913. (org-time-stamp nil)
  6914. (buffer-string)))))
  6915. (should
  6916. (string-match
  6917. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  6918. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  6919. (cl-letf (((symbol-function 'org-read-date)
  6920. (lambda (&rest args)
  6921. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6922. (org-time-stamp nil)
  6923. (buffer-string)))))
  6924. ;; When replacing a timestamp, preserve repeater, if any.
  6925. (should
  6926. (string-match
  6927. "<2014-03-04 .*? \\+2y>"
  6928. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  6929. (cl-letf (((symbol-function 'org-read-date)
  6930. (lambda (&rest args)
  6931. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6932. (org-time-stamp nil)
  6933. (buffer-string)))))
  6934. ;; When called twice in a raw, build a date range.
  6935. (should
  6936. (string-match
  6937. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  6938. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  6939. (cl-letf (((symbol-function 'org-read-date)
  6940. (lambda (&rest args)
  6941. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6942. (let ((last-command 'org-time-stamp)
  6943. (this-command 'org-time-stamp))
  6944. (org-time-stamp nil))
  6945. (buffer-string))))))
  6946. (ert-deftest test-org/timestamp-has-time-p ()
  6947. "Test `org-timestamp-has-time-p' specifications."
  6948. ;; With time.
  6949. (should
  6950. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6951. (org-timestamp-has-time-p (org-element-context))))
  6952. ;; Without time.
  6953. (should-not
  6954. (org-test-with-temp-text "<2012-03-29 Thu>"
  6955. (org-timestamp-has-time-p (org-element-context)))))
  6956. (ert-deftest test-org/get-repeat ()
  6957. "Test `org-get-repeat' specifications."
  6958. (should
  6959. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  6960. (org-get-repeat)))
  6961. (should-not
  6962. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  6963. (org-get-repeat)))
  6964. ;; Return proper repeat string.
  6965. (should
  6966. (equal "+2y"
  6967. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  6968. (org-get-repeat))))
  6969. ;; Prevent false positive (commented or verbatim time stamps)
  6970. (should-not
  6971. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6972. (org-get-repeat)))
  6973. (should-not
  6974. (org-test-with-temp-text
  6975. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6976. (org-get-repeat)))
  6977. ;; Return nil when called before first heading.
  6978. (should-not
  6979. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6980. (org-get-repeat)))
  6981. ;; When called with an optional argument, extract repeater from that
  6982. ;; string instead.
  6983. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6984. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6985. (ert-deftest test-org/timestamp-format ()
  6986. "Test `org-timestamp-format' specifications."
  6987. ;; Regular test.
  6988. (should
  6989. (equal
  6990. "2012-03-29 16:40"
  6991. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6992. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6993. ;; Range end.
  6994. (should
  6995. (equal
  6996. "2012-03-29"
  6997. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6998. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6999. (ert-deftest test-org/timestamp-split-range ()
  7000. "Test `org-timestamp-split-range' specifications."
  7001. ;; Extract range start (active).
  7002. (should
  7003. (equal '(2012 3 29)
  7004. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7005. (let ((ts (org-timestamp-split-range (org-element-context))))
  7006. (mapcar (lambda (p) (org-element-property p ts))
  7007. '(:year-end :month-end :day-end))))))
  7008. ;; Extract range start (inactive)
  7009. (should
  7010. (equal '(2012 3 29)
  7011. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  7012. (let ((ts (org-timestamp-split-range (org-element-context))))
  7013. (mapcar (lambda (p) (org-element-property p ts))
  7014. '(:year-end :month-end :day-end))))))
  7015. ;; Extract range end (active).
  7016. (should
  7017. (equal '(2012 3 30)
  7018. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7019. (let ((ts (org-timestamp-split-range
  7020. (org-element-context) t)))
  7021. (mapcar (lambda (p) (org-element-property p ts))
  7022. '(:year-end :month-end :day-end))))))
  7023. ;; Extract range end (inactive)
  7024. (should
  7025. (equal '(2012 3 30)
  7026. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  7027. (let ((ts (org-timestamp-split-range
  7028. (org-element-context) t)))
  7029. (mapcar (lambda (p) (org-element-property p ts))
  7030. '(:year-end :month-end :day-end))))))
  7031. ;; Return the timestamp if not a range.
  7032. (should
  7033. (org-test-with-temp-text "[2012-03-29 Thu]"
  7034. (let* ((ts-orig (org-element-context))
  7035. (ts-copy (org-timestamp-split-range ts-orig)))
  7036. (eq ts-orig ts-copy))))
  7037. (should
  7038. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  7039. (let* ((ts-orig (org-element-context))
  7040. (ts-copy (org-timestamp-split-range ts-orig)))
  7041. (eq ts-orig ts-copy)))))
  7042. (ert-deftest test-org/timestamp-translate ()
  7043. "Test `org-timestamp-translate' specifications."
  7044. ;; Translate whole date range.
  7045. (should
  7046. (equal "<29>--<30>"
  7047. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7048. (let ((org-display-custom-times t)
  7049. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7050. (org-timestamp-translate (org-element-context))))))
  7051. ;; Translate date range start.
  7052. (should
  7053. (equal "<29>"
  7054. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7055. (let ((org-display-custom-times t)
  7056. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7057. (org-timestamp-translate (org-element-context) 'start)))))
  7058. ;; Translate date range end.
  7059. (should
  7060. (equal "<30>"
  7061. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7062. (let ((org-display-custom-times t)
  7063. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7064. (org-timestamp-translate (org-element-context) 'end)))))
  7065. ;; Translate time range.
  7066. (should
  7067. (equal "<08>--<16>"
  7068. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  7069. (let ((org-display-custom-times t)
  7070. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  7071. (org-timestamp-translate (org-element-context))))))
  7072. ;; Translate non-range timestamp.
  7073. (should
  7074. (equal "<29>"
  7075. (org-test-with-temp-text "<2012-03-29 Thu>"
  7076. (let ((org-display-custom-times t)
  7077. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7078. (org-timestamp-translate (org-element-context))))))
  7079. ;; Do not change `diary' timestamps.
  7080. (should
  7081. (equal "<%%(org-float t 4 2)>"
  7082. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  7083. (let ((org-display-custom-times t)
  7084. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7085. (org-timestamp-translate (org-element-context)))))))
  7086. (ert-deftest test-org/timestamp-from-string ()
  7087. "Test `org-timestamp-from-string' specifications."
  7088. ;; Return nil if argument is not a valid Org timestamp.
  7089. (should-not (org-timestamp-from-string ""))
  7090. (should-not (org-timestamp-from-string nil))
  7091. (should-not (org-timestamp-from-string "<2012-03-29"))
  7092. ;; Otherwise, return a valid Org timestamp object.
  7093. (should
  7094. (string-match-p "<2012-03-29 .+>"
  7095. (org-element-interpret-data
  7096. (org-timestamp-from-string "<2012-03-29 Thu>"))))
  7097. (should
  7098. (string-match-p "[2014-03-04 .+]"
  7099. (org-element-interpret-data
  7100. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  7101. (ert-deftest test-org/timestamp-from-time ()
  7102. "Test `org-timestamp-from-time' specifications."
  7103. ;; Standard test.
  7104. (should
  7105. (string-match-p
  7106. "<2012-03-29 .+>"
  7107. (org-element-interpret-data
  7108. (org-timestamp-from-time
  7109. (apply #'encode-time
  7110. (org-parse-time-string "<2012-03-29 Thu 16:40>"))))))
  7111. ;; When optional argument WITH-TIME is non-nil, provide time
  7112. ;; information.
  7113. (should
  7114. (string-match-p
  7115. "<2012-03-29 .+ 16:40>"
  7116. (org-element-interpret-data
  7117. (org-timestamp-from-time
  7118. (apply #'encode-time
  7119. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  7120. t))))
  7121. ;; When optional argument INACTIVE is non-nil, return an inactive
  7122. ;; timestamp.
  7123. (should
  7124. (string-match-p
  7125. "[2012-03-29 .+]"
  7126. (org-element-interpret-data
  7127. (org-timestamp-from-time
  7128. (apply #'encode-time
  7129. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  7130. nil t)))))
  7131. (ert-deftest test-org/timestamp-to-time ()
  7132. "Test `org-timestamp-to-time' specifications."
  7133. (should
  7134. (equal "2014-03-04"
  7135. (format-time-string
  7136. "%Y-%m-%d"
  7137. (org-timestamp-to-time
  7138. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  7139. (should
  7140. (equal "2014-03-04"
  7141. (format-time-string
  7142. "%Y-%m-%d"
  7143. (org-timestamp-to-time
  7144. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  7145. (should
  7146. (equal "2012-03-29 08:30"
  7147. (format-time-string
  7148. "%Y-%m-%d %H:%M"
  7149. (org-timestamp-to-time
  7150. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  7151. (should
  7152. (equal "2012-03-29"
  7153. (format-time-string
  7154. "%Y-%m-%d"
  7155. (org-timestamp-to-time
  7156. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  7157. (should
  7158. (equal "2012-03-29"
  7159. (format-time-string
  7160. "%Y-%m-%d"
  7161. (org-timestamp-to-time
  7162. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  7163. ;; When optional argument END is non-nil, use end of date range or
  7164. ;; time range.
  7165. (should
  7166. (equal "2012-03-29 16:40"
  7167. (format-time-string
  7168. "%Y-%m-%d %H:%M"
  7169. (org-timestamp-to-time
  7170. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  7171. t))))
  7172. (should
  7173. (equal "2014-03-04"
  7174. (format-time-string
  7175. "%Y-%m-%d"
  7176. (org-timestamp-to-time
  7177. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  7178. t))))
  7179. (should
  7180. (equal "2014-03-04"
  7181. (format-time-string
  7182. "%Y-%m-%d"
  7183. (org-timestamp-to-time
  7184. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  7185. t)))))
  7186. ;;; Visibility
  7187. (ert-deftest test-org/hide-drawer-toggle ()
  7188. "Test `org-hide-drawer-toggle' specifications."
  7189. ;; Error when not at a drawer.
  7190. (should-error
  7191. (org-test-with-temp-text ":fake-drawer:\ncontents"
  7192. (org-hide-drawer-toggle 'off)
  7193. (get-char-property (line-end-position) 'invisible)))
  7194. (should-error
  7195. (org-test-with-temp-text
  7196. "#+begin_example\n<point>:D:\nc\n:END:\n#+end_example"
  7197. (org-hide-drawer-toggle t)))
  7198. ;; Hide drawer.
  7199. (should
  7200. (org-test-with-temp-text ":drawer:\ncontents\n:end:"
  7201. (org-hide-drawer-toggle)
  7202. (get-char-property (line-end-position) 'invisible)))
  7203. ;; Show drawer unconditionally when optional argument is `off'.
  7204. (should-not
  7205. (org-test-with-temp-text ":drawer:\ncontents\n:end:"
  7206. (org-hide-drawer-toggle)
  7207. (org-hide-drawer-toggle 'off)
  7208. (get-char-property (line-end-position) 'invisible)))
  7209. ;; Hide drawer unconditionally when optional argument is non-nil.
  7210. (should
  7211. (org-test-with-temp-text ":drawer:\ncontents\n:end:"
  7212. (org-hide-drawer-toggle t)
  7213. (get-char-property (line-end-position) 'invisible)))
  7214. ;; Do not hide drawer when called from final blank lines.
  7215. (should-not
  7216. (org-test-with-temp-text ":drawer:\ncontents\n:end:\n\n<point>"
  7217. (org-hide-drawer-toggle)
  7218. (goto-char (point-min))
  7219. (get-char-property (line-end-position) 'invisible)))
  7220. ;; Don't leave point in an invisible part of the buffer when hiding
  7221. ;; a drawer away.
  7222. (should-not
  7223. (org-test-with-temp-text ":drawer:\ncontents\n<point>:end:"
  7224. (org-hide-drawer-toggle)
  7225. (get-char-property (point) 'invisible))))
  7226. (ert-deftest test-org/hide-block-toggle ()
  7227. "Test `org-hide-block-toggle' specifications."
  7228. ;; Error when not at a block.
  7229. (should-error
  7230. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  7231. (org-hide-block-toggle 'off)
  7232. (get-char-property (line-end-position) 'invisible)))
  7233. ;; Hide block.
  7234. (should
  7235. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  7236. (org-hide-block-toggle)
  7237. (get-char-property (line-end-position) 'invisible)))
  7238. (should
  7239. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  7240. (org-hide-block-toggle)
  7241. (get-char-property (line-end-position) 'invisible)))
  7242. ;; Show block unconditionally when optional argument is `off'.
  7243. (should-not
  7244. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7245. (org-hide-block-toggle)
  7246. (org-hide-block-toggle 'off)
  7247. (get-char-property (line-end-position) 'invisible)))
  7248. (should-not
  7249. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7250. (org-hide-block-toggle 'off)
  7251. (get-char-property (line-end-position) 'invisible)))
  7252. ;; Hide block unconditionally when optional argument is non-nil.
  7253. (should
  7254. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7255. (org-hide-block-toggle t)
  7256. (get-char-property (line-end-position) 'invisible)))
  7257. (should
  7258. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7259. (org-hide-block-toggle)
  7260. (org-hide-block-toggle t)
  7261. (get-char-property (line-end-position) 'invisible)))
  7262. ;; Do not hide block when called from final blank lines.
  7263. (should-not
  7264. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  7265. (org-hide-block-toggle)
  7266. (goto-char (point-min))
  7267. (get-char-property (line-end-position) 'invisible)))
  7268. ;; Don't leave point in an invisible part of the buffer when hiding
  7269. ;; a block away.
  7270. (should-not
  7271. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  7272. (org-hide-block-toggle)
  7273. (get-char-property (point) 'invisible))))
  7274. (ert-deftest test-org/hide-block-toggle-maybe ()
  7275. "Test `org-hide-block-toggle-maybe' specifications."
  7276. (should
  7277. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  7278. (org-hide-block-toggle-maybe)))
  7279. (should-not
  7280. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  7281. (ert-deftest test-org/show-set-visibility ()
  7282. "Test `org-show-set-visibility' specifications."
  7283. ;; Do not throw an error before first heading.
  7284. (should
  7285. (org-test-with-temp-text "Preamble\n* Headline"
  7286. (org-show-set-visibility 'tree)
  7287. t))
  7288. ;; Test all visibility spans, both on headline and in entry.
  7289. (let ((list-visible-lines
  7290. (lambda (state headerp)
  7291. (org-test-with-temp-text "* Grandmother (0)
  7292. ** Uncle (1)
  7293. *** Heir (2)
  7294. ** Father (3)
  7295. Ancestor text (4)
  7296. *** Sister (5)
  7297. Sibling text (6)
  7298. *** Self (7)
  7299. Match (8)
  7300. **** First born (9)
  7301. Child text (10)
  7302. **** The other child (11)
  7303. *** Brother (12)
  7304. ** Aunt (13)
  7305. "
  7306. (org-cycle t)
  7307. (search-forward (if headerp "Self" "Match"))
  7308. (org-show-set-visibility state)
  7309. (goto-char (point-min))
  7310. (let (result (line 0))
  7311. (while (not (eobp))
  7312. (unless (org-invisible-p2) (push line result))
  7313. (cl-incf line)
  7314. (forward-line))
  7315. (nreverse result))))))
  7316. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  7317. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  7318. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  7319. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  7320. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  7321. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  7322. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  7323. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  7324. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  7325. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  7326. (funcall list-visible-lines 'tree nil)))
  7327. (should (equal '(0 1 3 4 5 7 12 13)
  7328. (funcall list-visible-lines 'canonical t)))
  7329. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  7330. (funcall list-visible-lines 'canonical nil))))
  7331. ;; When point is hidden in a drawer or a block, make sure to make it
  7332. ;; visible.
  7333. (should-not
  7334. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  7335. (org-hide-block-toggle)
  7336. (search-forward "Text")
  7337. (org-show-set-visibility 'minimal)
  7338. (org-invisible-p2)))
  7339. (should-not
  7340. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  7341. (org-hide-drawer-toggle)
  7342. (search-forward "Text")
  7343. (org-show-set-visibility 'minimal)
  7344. (org-invisible-p2)))
  7345. (should-not
  7346. (org-test-with-temp-text
  7347. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  7348. (org-hide-drawer-toggle)
  7349. (forward-line -1)
  7350. (org-hide-block-toggle)
  7351. (search-forward "Text")
  7352. (org-show-set-visibility 'minimal)
  7353. (org-invisible-p2))))
  7354. (defun test-org/copy-visible ()
  7355. "Test `org-copy-visible' specifications."
  7356. (should
  7357. (equal "Foo"
  7358. (org-test-with-temp-text "Foo"
  7359. (let ((kill-ring nil))
  7360. (org-copy-visible (point-min) (point-max))
  7361. (current-kill 0 t)))))
  7362. ;; Skip invisible characters by text property.
  7363. (should
  7364. (equal "Foo"
  7365. (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
  7366. (let ((kill-ring nil))
  7367. (org-copy-visible (point-min) (point-max))
  7368. (current-kill 0 t)))))
  7369. ;; Skip invisible characters by overlay.
  7370. (should
  7371. (equal "Foo"
  7372. (org-test-with-temp-text "F<hidden>oo"
  7373. (let ((o (make-overlay 2 10)))
  7374. (overlay-put o 'invisible t))
  7375. (let ((kill-ring nil))
  7376. (org-copy-visible (point-min) (point-max))
  7377. (current-kill 0 t)))))
  7378. ;; Handle invisible characters at the beginning and the end of the
  7379. ;; buffer.
  7380. (should
  7381. (equal "Foo"
  7382. (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
  7383. (let ((kill-ring nil))
  7384. (org-copy-visible (point-min) (point-max))
  7385. (current-kill 0 t)))))
  7386. (should
  7387. (equal "Foo"
  7388. (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
  7389. (let ((kill-ring nil))
  7390. (org-copy-visible (point-min) (point-max))
  7391. (current-kill 0 t)))))
  7392. ;; Handle multiple visible parts.
  7393. (should
  7394. (equal "abc"
  7395. (org-test-with-temp-text
  7396. #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
  7397. (let ((kill-ring nil))
  7398. (org-copy-visible (point-min) (point-max))
  7399. (current-kill 0 t))))))
  7400. (ert-deftest test-org/set-visibility-according-to-property ()
  7401. "Test `org-set-visibility-according-to-property' specifications."
  7402. ;; "folded" state.
  7403. (should
  7404. (org-test-with-temp-text
  7405. "
  7406. * a
  7407. :PROPERTIES:
  7408. :VISIBILITY: folded
  7409. :END:
  7410. ** <point>b"
  7411. (org-set-visibility-according-to-property)
  7412. (invisible-p (point))))
  7413. ;; "children" state.
  7414. (should
  7415. (org-test-with-temp-text
  7416. "
  7417. * a
  7418. :PROPERTIES:
  7419. :VISIBILITY: children
  7420. :END:
  7421. ** b
  7422. <point>Contents
  7423. ** c"
  7424. (org-set-visibility-according-to-property)
  7425. (invisible-p (point))))
  7426. (should
  7427. (org-test-with-temp-text
  7428. "
  7429. * a
  7430. :PROPERTIES:
  7431. :VISIBILITY: children
  7432. :END:
  7433. ** b
  7434. Contents
  7435. *** <point>c"
  7436. (org-set-visibility-according-to-property)
  7437. (invisible-p (point))))
  7438. ;; "content" state.
  7439. (should
  7440. (org-test-with-temp-text
  7441. "
  7442. * a
  7443. :PROPERTIES:
  7444. :VISIBILITY: content
  7445. :END:
  7446. ** b
  7447. <point>Contents
  7448. *** c"
  7449. (org-set-visibility-according-to-property)
  7450. (invisible-p (point))))
  7451. (should
  7452. (org-test-with-temp-text
  7453. "
  7454. * a
  7455. :PROPERTIES:
  7456. :VISIBILITY: content
  7457. :END:
  7458. ** b
  7459. Contents
  7460. *** <point>c"
  7461. (org-set-visibility-according-to-property)
  7462. (not (invisible-p (point)))))
  7463. ;; "showall" state.
  7464. (should
  7465. (org-test-with-temp-text
  7466. "
  7467. * a
  7468. :PROPERTIES:
  7469. :VISIBILITY: showall
  7470. :END:
  7471. ** b
  7472. <point>Contents
  7473. *** c"
  7474. (org-set-visibility-according-to-property)
  7475. (not (invisible-p (point)))))
  7476. (should
  7477. (org-test-with-temp-text
  7478. "
  7479. * a
  7480. :PROPERTIES:
  7481. :VISIBILITY: showall
  7482. :END:
  7483. ** b
  7484. Contents
  7485. *** <point>c"
  7486. (org-set-visibility-according-to-property)
  7487. (not (invisible-p (point)))))
  7488. ;; When VISIBILITY properties are nested, ignore inner ones.
  7489. (should
  7490. (org-test-with-temp-text
  7491. "
  7492. * A
  7493. :PROPERTIES:
  7494. :VISIBILITY: folded
  7495. :END:
  7496. ** <point>B
  7497. :PROPERTIES:
  7498. :VISIBILITY: folded
  7499. :END:"
  7500. (org-set-visibility-according-to-property)
  7501. (invisible-p (point)))))
  7502. (ert-deftest test-org/visibility-show-branches ()
  7503. "Test visibility of inline archived subtrees."
  7504. (org-test-with-temp-text
  7505. "* Foo<point>
  7506. ** Bar :ARCHIVE:
  7507. *** Baz
  7508. "
  7509. (org-kill-note-or-show-branches)
  7510. (should (org-invisible-p (- (point-max) 2)))))
  7511. ;;; Yank and Kill
  7512. (ert-deftest test-org/paste-subtree ()
  7513. "Test `org-paste-subtree' specifications."
  7514. ;; Return an error if text to yank is not a set of subtrees.
  7515. (should-error (org-paste-subtree nil "Text"))
  7516. ;; Adjust level according to current one.
  7517. (should
  7518. (equal "* H\n* Text\n"
  7519. (org-test-with-temp-text "* H\n<point>"
  7520. (org-paste-subtree nil "* Text")
  7521. (buffer-string))))
  7522. (should
  7523. (equal "* H1\n** H2\n** Text\n"
  7524. (org-test-with-temp-text "* H1\n** H2\n<point>"
  7525. (org-paste-subtree nil "* Text")
  7526. (buffer-string))))
  7527. ;; When not on a heading, move to next heading before yanking.
  7528. (should
  7529. (equal "* H1\nParagraph\n* Text\n* H2"
  7530. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  7531. (org-paste-subtree nil "* Text")
  7532. (buffer-string))))
  7533. ;; If point is between two headings, use the deepest level.
  7534. (should
  7535. (equal "* H1\n\n* Text\n* H2"
  7536. (org-test-with-temp-text "* H1\n<point>\n* H2"
  7537. (org-paste-subtree nil "* Text")
  7538. (buffer-string))))
  7539. (should
  7540. (equal "** H1\n\n** Text\n* H2"
  7541. (org-test-with-temp-text "** H1\n<point>\n* H2"
  7542. (org-paste-subtree nil "* Text")
  7543. (buffer-string))))
  7544. (should
  7545. (equal "* H1\n\n** Text\n** H2"
  7546. (org-test-with-temp-text "* H1\n<point>\n** H2"
  7547. (org-paste-subtree nil "* Text")
  7548. (buffer-string))))
  7549. ;; When on an empty heading, after the stars, deduce the new level
  7550. ;; from the number of stars.
  7551. (should
  7552. (equal "*** Text\n"
  7553. (org-test-with-temp-text "*** <point>"
  7554. (org-paste-subtree nil "* Text")
  7555. (buffer-string))))
  7556. ;; Optional argument LEVEL forces a level for the subtree.
  7557. (should
  7558. (equal "* H\n*** Text\n"
  7559. (org-test-with-temp-text "* H<point>"
  7560. (org-paste-subtree 3 "* Text")
  7561. (buffer-string)))))
  7562. (ert-deftest test-org/cut-and-paste-subtree ()
  7563. "Test `org-cut-subtree' and `org-paste-subtree'."
  7564. (should
  7565. (equal
  7566. "* Two
  7567. two
  7568. * One
  7569. "
  7570. (org-test-with-temp-text
  7571. "* One
  7572. <point>* Two
  7573. two
  7574. "
  7575. (call-interactively #'org-cut-subtree)
  7576. (goto-char (point-min))
  7577. (call-interactively #'org-paste-subtree)
  7578. (buffer-string))))
  7579. (should
  7580. (equal
  7581. "* One
  7582. * Two
  7583. "
  7584. (org-test-with-temp-text
  7585. "* One
  7586. <point>* Two
  7587. "
  7588. (call-interactively #'org-cut-subtree)
  7589. (backward-char)
  7590. (call-interactively #'org-paste-subtree)
  7591. (buffer-string)))))
  7592. (provide 'test-org)
  7593. ;;; test-org.el ends here