test-org.el 253 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790
  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. ;; `org-return-follows-link' handle multi-line lines.
  1181. (should
  1182. (org-test-with-temp-text
  1183. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1184. (let ((org-return-follows-link t)
  1185. (org-link-search-must-match-exact-headline nil))
  1186. (org-return))
  1187. (looking-at-p "<<target>>")))
  1188. (should-not
  1189. (org-test-with-temp-text
  1190. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1191. (let ((org-return-follows-link t)
  1192. (org-link-search-must-match-exact-headline nil))
  1193. (org-return))
  1194. (looking-at-p "<<target>>")))
  1195. ;; However, do not open link when point is in a table.
  1196. (should
  1197. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1198. (let ((org-return-follows-link t)) (org-return))
  1199. (looking-at-p "between")))
  1200. ;; Special case: in a list, when indenting, do not break structure.
  1201. (should
  1202. (equal "- A\n B"
  1203. (org-test-with-temp-text "- A <point>B"
  1204. (org-return t)
  1205. (buffer-string))))
  1206. (should
  1207. (equal "- A\n\n- B"
  1208. (org-test-with-temp-text "- A\n<point>- B"
  1209. (org-return t)
  1210. (buffer-string))))
  1211. ;; On tags part of a headline, add a newline below it instead of
  1212. ;; breaking it.
  1213. (should
  1214. (equal "* H :tag:\n"
  1215. (org-test-with-temp-text "* H :<point>tag:"
  1216. (org-return)
  1217. (buffer-string))))
  1218. ;; Before headline text, add a newline below it instead of breaking
  1219. ;; it.
  1220. (should
  1221. (equal "* TODO H :tag:\n"
  1222. (org-test-with-temp-text "* <point>TODO H :tag:"
  1223. (org-return)
  1224. (buffer-string))))
  1225. (should
  1226. (equal "* TODO [#B] H :tag:\n"
  1227. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1228. (org-return)
  1229. (buffer-string))))
  1230. (should ;TODO are case-sensitive
  1231. (equal "* \nTodo"
  1232. (org-test-with-temp-text "* <point>Todo"
  1233. (org-return)
  1234. (buffer-string))))
  1235. ;; At headline text, break headline text but preserve tags.
  1236. (should
  1237. (equal "* TODO [#B] foo :tag:\nbar"
  1238. (let (org-auto-align-tags)
  1239. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1240. (org-return)
  1241. (buffer-string)))))
  1242. ;; At bol of headline insert newline.
  1243. (should
  1244. (equal "\n* h"
  1245. (org-test-with-temp-text "<point>* h"
  1246. (org-return)
  1247. (buffer-string))))
  1248. ;; Refuse to leave invalid headline in buffer.
  1249. (should
  1250. (equal "* h\n"
  1251. (org-test-with-temp-text "*<point> h"
  1252. (org-return)
  1253. (buffer-string))))
  1254. ;; Before first column or after last one in a table, split the
  1255. ;; table.
  1256. (should
  1257. (equal "| a |\n\n| b |"
  1258. (org-test-with-temp-text "| a |\n<point>| b |"
  1259. (org-return)
  1260. (buffer-string))))
  1261. (should
  1262. (equal "| a |\n\n| b |"
  1263. (org-test-with-temp-text "| a |<point>\n| b |"
  1264. (org-return)
  1265. (buffer-string))))
  1266. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1267. (should
  1268. (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:"
  1269. (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:"
  1270. (setq-local fill-column 10)
  1271. (auto-fill-mode 1)
  1272. (org-return)
  1273. (buffer-string)))))
  1274. (ert-deftest test-org/with-electric-indent ()
  1275. "Test RET and C-j specifications with `electric-indent-mode' on."
  1276. ;; Call commands interactively, since this is how `newline' knows it
  1277. ;; must run `post-self-insert-hook'.
  1278. ;;
  1279. ;; RET, like `newline', should indent.
  1280. (should
  1281. (equal " Para\n graph"
  1282. (org-test-with-temp-text " Para<point>graph"
  1283. (electric-indent-local-mode 1)
  1284. (call-interactively 'org-return)
  1285. (buffer-string))))
  1286. (should
  1287. (equal "- item1\n item2"
  1288. (org-test-with-temp-text "- item1<point>item2"
  1289. (electric-indent-local-mode 1)
  1290. (call-interactively 'org-return)
  1291. (buffer-string))))
  1292. (should
  1293. (equal "* heading\n body"
  1294. (org-test-with-temp-text "* heading<point>body"
  1295. (electric-indent-local-mode 1)
  1296. (call-interactively 'org-return)
  1297. (buffer-string))))
  1298. ;; C-j, like `electric-newline-and-maybe-indent', should not indent.
  1299. (should
  1300. (equal " Para\ngraph"
  1301. (org-test-with-temp-text " Para<point>graph"
  1302. (electric-indent-local-mode 1)
  1303. (call-interactively 'org-return-and-maybe-indent)
  1304. (buffer-string))))
  1305. (should
  1306. (equal "- item1\nitem2"
  1307. (org-test-with-temp-text "- item1<point>item2"
  1308. (electric-indent-local-mode 1)
  1309. (call-interactively 'org-return-and-maybe-indent)
  1310. (buffer-string))))
  1311. (should
  1312. (equal "* heading\nbody"
  1313. (org-test-with-temp-text "* heading<point>body"
  1314. (electric-indent-local-mode 1)
  1315. (call-interactively 'org-return-and-maybe-indent)
  1316. (buffer-string)))))
  1317. (ert-deftest test-org/without-electric-indent ()
  1318. "Test RET and C-j specifications with `electric-indent-mode' off."
  1319. ;; Call commands interactively, since this is how `newline' knows it
  1320. ;; must run `post-self-insert-hook'.
  1321. ;;
  1322. ;; RET, like `newline', should not indent.
  1323. (should
  1324. (equal " Para\ngraph"
  1325. (org-test-with-temp-text " Para<point>graph"
  1326. (electric-indent-local-mode 0)
  1327. (call-interactively 'org-return)
  1328. (buffer-string))))
  1329. (should
  1330. (equal "- item1\nitem2"
  1331. (org-test-with-temp-text "- item1<point>item2"
  1332. (electric-indent-local-mode 0)
  1333. (call-interactively 'org-return)
  1334. (buffer-string))))
  1335. (should
  1336. (equal "* heading\nbody"
  1337. (org-test-with-temp-text "* heading<point>body"
  1338. (electric-indent-local-mode 0)
  1339. (call-interactively 'org-return)
  1340. (buffer-string))))
  1341. ;; C-j, like `electric-newline-and-maybe-indent', should indent.
  1342. (should
  1343. (equal " Para\n graph"
  1344. (org-test-with-temp-text " Para<point>graph"
  1345. (electric-indent-local-mode 0)
  1346. (call-interactively 'org-return-and-maybe-indent)
  1347. (buffer-string))))
  1348. (should
  1349. (equal "- item1\n item2"
  1350. (org-test-with-temp-text "- item1<point>item2"
  1351. (electric-indent-local-mode 0)
  1352. (call-interactively 'org-return-and-maybe-indent)
  1353. (buffer-string))))
  1354. (should
  1355. (equal "* heading\n body"
  1356. (org-test-with-temp-text "* heading<point>body"
  1357. (electric-indent-local-mode 0)
  1358. (call-interactively 'org-return-and-maybe-indent)
  1359. (buffer-string)))))
  1360. (ert-deftest test-org/meta-return ()
  1361. "Test M-RET (`org-meta-return') specifications."
  1362. ;; In a table field insert a row above.
  1363. (should
  1364. (org-test-with-temp-text "| a |"
  1365. (forward-char)
  1366. (org-meta-return)
  1367. (forward-line -1)
  1368. (looking-at "| |$")))
  1369. ;; In a paragraph change current line into a header.
  1370. (should
  1371. (org-test-with-temp-text "a"
  1372. (org-meta-return)
  1373. (beginning-of-line)
  1374. (looking-at "\* a$")))
  1375. ;; In an item insert an item, in this case above.
  1376. (should
  1377. (org-test-with-temp-text "- a"
  1378. (org-meta-return)
  1379. (beginning-of-line)
  1380. (looking-at "- $")))
  1381. ;; In a drawer and item insert an item, in this case above.
  1382. (should
  1383. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1384. (forward-line)
  1385. (org-meta-return)
  1386. (beginning-of-line)
  1387. (looking-at "- $"))))
  1388. (ert-deftest test-org/insert-heading ()
  1389. "Test `org-insert-heading' specifications."
  1390. ;; In an empty buffer, insert a new headline.
  1391. (should
  1392. (equal "* "
  1393. (org-test-with-temp-text ""
  1394. (org-insert-heading)
  1395. (buffer-string))))
  1396. ;; At the beginning of a line, turn it into a headline.
  1397. (should
  1398. (equal "* P"
  1399. (org-test-with-temp-text "<point>P"
  1400. (org-insert-heading)
  1401. (buffer-string))))
  1402. ;; In the middle of a line, split the line if allowed, otherwise,
  1403. ;; insert the headline at its end.
  1404. (should
  1405. (equal "Para\n* graph"
  1406. (org-test-with-temp-text "Para<point>graph"
  1407. (let ((org-M-RET-may-split-line '((default . t))))
  1408. (org-insert-heading))
  1409. (buffer-string))))
  1410. (should
  1411. (equal "Paragraph\n* "
  1412. (org-test-with-temp-text "Para<point>graph"
  1413. (let ((org-M-RET-may-split-line '((default . nil))))
  1414. (org-insert-heading))
  1415. (buffer-string))))
  1416. ;; At the beginning of a headline, create one above.
  1417. (should
  1418. (equal "* \n* H"
  1419. (org-test-with-temp-text "* H"
  1420. (org-insert-heading)
  1421. (buffer-string))))
  1422. ;; In the middle of a headline, split it if allowed.
  1423. (should
  1424. (equal "* H\n* 1"
  1425. (org-test-with-temp-text "* H<point>1"
  1426. (let ((org-M-RET-may-split-line '((headline . t))))
  1427. (org-insert-heading))
  1428. (buffer-string))))
  1429. (should
  1430. (equal "* H1\n* "
  1431. (org-test-with-temp-text "* H<point>1"
  1432. (let ((org-M-RET-may-split-line '((headline . nil))))
  1433. (org-insert-heading))
  1434. (buffer-string))))
  1435. ;; However, splitting cannot happen on TODO keywords, priorities or
  1436. ;; tags.
  1437. (should
  1438. (equal "* TODO H1\n* "
  1439. (org-test-with-temp-text "* TO<point>DO H1"
  1440. (let ((org-M-RET-may-split-line '((headline . t))))
  1441. (org-insert-heading))
  1442. (buffer-string))))
  1443. (should
  1444. (equal "* [#A] H1\n* "
  1445. (org-test-with-temp-text "* [#<point>A] H1"
  1446. (let ((org-M-RET-may-split-line '((headline . t))))
  1447. (org-insert-heading))
  1448. (buffer-string))))
  1449. (should
  1450. (equal "* H1 :tag:\n* "
  1451. (org-test-with-temp-text "* H1 :ta<point>g:"
  1452. (let ((org-M-RET-may-split-line '((headline . t))))
  1453. (org-insert-heading))
  1454. (buffer-string))))
  1455. ;; New headline level depends on the level of the headline above.
  1456. (should
  1457. (equal "** H\n** P"
  1458. (org-test-with-temp-text "** H\n<point>P"
  1459. (org-insert-heading)
  1460. (buffer-string))))
  1461. (should
  1462. (equal "** H\nPara\n** graph"
  1463. (org-test-with-temp-text "** H\nPara<point>graph"
  1464. (let ((org-M-RET-may-split-line '((default . t))))
  1465. (org-insert-heading))
  1466. (buffer-string))))
  1467. (should
  1468. (equal "** \n** H"
  1469. (org-test-with-temp-text "** H"
  1470. (org-insert-heading)
  1471. (buffer-string))))
  1472. ;; When called with one universal argument, insert a new headline at
  1473. ;; the end of the current subtree, independently on the position of
  1474. ;; point.
  1475. (should
  1476. (equal
  1477. "* H1\n** H2\n* "
  1478. (org-test-with-temp-text "* H1\n** H2"
  1479. (let ((org-insert-heading-respect-content nil))
  1480. (org-insert-heading '(4)))
  1481. (buffer-string))))
  1482. (should
  1483. (equal
  1484. "* H1\n** H2\n* "
  1485. (org-test-with-temp-text "* H<point>1\n** H2"
  1486. (let ((org-insert-heading-respect-content nil))
  1487. (org-insert-heading '(4)))
  1488. (buffer-string))))
  1489. ;; When called with two universal arguments, insert a new headline
  1490. ;; at the end of the grandparent subtree.
  1491. (should
  1492. (equal "* H1\n** H3\n- item\n** H2\n** "
  1493. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1494. (let ((org-insert-heading-respect-content nil))
  1495. (org-insert-heading '(16)))
  1496. (buffer-string))))
  1497. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1498. ;; a level 1 heading.
  1499. (should
  1500. (equal "* H1\n** H2\n* "
  1501. (org-test-with-temp-text "* H1\n** H2<point>"
  1502. (org-insert-heading nil nil t)
  1503. (buffer-string))))
  1504. (should
  1505. (equal "* H1\n- item\n* "
  1506. (org-test-with-temp-text "* H1\n- item<point>"
  1507. (org-insert-heading nil nil t)
  1508. (buffer-string))))
  1509. ;; Obey `org-blank-before-new-entry'.
  1510. (should
  1511. (equal "* H1\n\n* "
  1512. (org-test-with-temp-text "* H1<point>"
  1513. (let ((org-blank-before-new-entry '((heading . t))))
  1514. (org-insert-heading))
  1515. (buffer-string))))
  1516. (should
  1517. (equal "* H1\n* "
  1518. (org-test-with-temp-text "* H1<point>"
  1519. (let ((org-blank-before-new-entry '((heading . nil))))
  1520. (org-insert-heading))
  1521. (buffer-string))))
  1522. (should
  1523. (equal "* H1\n* H2\n* "
  1524. (org-test-with-temp-text "* H1\n* H2<point>"
  1525. (let ((org-blank-before-new-entry '((heading . auto))))
  1526. (org-insert-heading))
  1527. (buffer-string))))
  1528. (should
  1529. (equal "* H1\n\n* H2\n\n* "
  1530. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1531. (let ((org-blank-before-new-entry '((heading . auto))))
  1532. (org-insert-heading))
  1533. (buffer-string))))
  1534. ;; Corner case: correctly insert a headline after an empty one.
  1535. (should
  1536. (equal "* \n* "
  1537. (org-test-with-temp-text "* <point>"
  1538. (org-insert-heading)
  1539. (buffer-string))))
  1540. (should
  1541. (org-test-with-temp-text "* <point>\n"
  1542. (org-insert-heading)
  1543. (looking-at-p "\n\\'")))
  1544. ;; Do not insert spurious headlines when inserting a new headline.
  1545. (should
  1546. (equal "* H1\n* H2\n* \n"
  1547. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1548. (org-insert-heading)
  1549. (buffer-string))))
  1550. ;; Preserve visibility at beginning of line. In particular, when
  1551. ;; removing spurious blank lines, do not visually merge heading with
  1552. ;; the line visible above.
  1553. (should-not
  1554. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1555. (org-overview)
  1556. (let ((org-blank-before-new-entry '((heading . nil))))
  1557. (org-insert-heading '(4)))
  1558. (invisible-p (line-end-position 0))))
  1559. ;; Properly handle empty lines when forcing a headline below current
  1560. ;; one.
  1561. (should
  1562. (equal "* H1\n\n* H\n\n* "
  1563. (org-test-with-temp-text "* H1\n\n* H<point>"
  1564. (let ((org-blank-before-new-entry '((heading . t))))
  1565. (org-insert-heading '(4))
  1566. (buffer-string))))))
  1567. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1568. "Test `org-insert-todo-heading-respect-content' specifications."
  1569. ;; Create a TODO heading.
  1570. (should
  1571. (org-test-with-temp-text "* H1\n Body"
  1572. (org-insert-todo-heading-respect-content)
  1573. (nth 2 (org-heading-components))))
  1574. ;; Add headline at the end of the first subtree
  1575. (should
  1576. (equal
  1577. "* TODO "
  1578. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1579. (org-insert-todo-heading-respect-content)
  1580. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1581. ;; In a list, do not create a new item.
  1582. (should
  1583. (equal
  1584. "* TODO "
  1585. (org-test-with-temp-text "* H\n- an item\n- another one"
  1586. (search-forward "an ")
  1587. (org-insert-todo-heading-respect-content)
  1588. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1589. ;; Use the same TODO keyword as current heading.
  1590. (should
  1591. (equal
  1592. "* TODO \n"
  1593. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1594. (org-insert-todo-heading-respect-content)
  1595. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1596. (ert-deftest test-org/clone-with-time-shift ()
  1597. "Test `org-clone-subtree-with-time-shift'."
  1598. ;; Raise an error before first heading.
  1599. (should-error
  1600. (org-test-with-temp-text ""
  1601. (org-clone-subtree-with-time-shift 1)))
  1602. ;; Raise an error on invalid number of clones.
  1603. (should-error
  1604. (org-test-with-temp-text "* Clone me"
  1605. (org-clone-subtree-with-time-shift -1)))
  1606. ;; Clone non-repeating once.
  1607. (should
  1608. (equal "\
  1609. * H1\n<2015-06-21>
  1610. * H1\n<2015-06-23>
  1611. "
  1612. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1613. (org-clone-subtree-with-time-shift 1 "+2d")
  1614. (replace-regexp-in-string
  1615. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1616. nil nil 1))))
  1617. ;; Clone repeating once.
  1618. (should
  1619. (equal "\
  1620. * H1\n<2015-06-21>
  1621. * H1\n<2015-06-23>
  1622. * H1\n<2015-06-25 +1w>
  1623. "
  1624. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1625. (org-clone-subtree-with-time-shift 1 "+2d")
  1626. (replace-regexp-in-string
  1627. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1628. nil nil 1))))
  1629. ;; Clone repeating once in backward.
  1630. (should
  1631. (equal "\
  1632. * H1\n<2015-06-21>
  1633. * H1\n<2015-06-19>
  1634. * H1\n<2015-06-17 +1w>
  1635. "
  1636. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1637. (org-clone-subtree-with-time-shift 1 "-2d")
  1638. (replace-regexp-in-string
  1639. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1640. nil nil 1))))
  1641. ;; Clone non-repeating zero times.
  1642. (should
  1643. (equal "\
  1644. * H1\n<2015-06-21>
  1645. "
  1646. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1647. (org-clone-subtree-with-time-shift 0 "+2d")
  1648. (replace-regexp-in-string
  1649. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1650. nil nil 1))))
  1651. ;; Clone repeating "zero" times.
  1652. (should
  1653. (equal "\
  1654. * H1\n<2015-06-21>
  1655. * H1\n<2015-06-23 +1w>
  1656. "
  1657. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1658. (org-clone-subtree-with-time-shift 0 "+2d")
  1659. (replace-regexp-in-string
  1660. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1661. nil nil 1))))
  1662. ;; Clone with blank SHIFT argument.
  1663. (should
  1664. (string-prefix-p
  1665. "* H <2012-03-29"
  1666. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1667. (org-clone-subtree-with-time-shift 1 "")
  1668. (buffer-substring-no-properties (line-beginning-position 2)
  1669. (line-end-position 2)))))
  1670. ;; Find time stamps before point. If SHIFT is not specified, ask
  1671. ;; for a time shift.
  1672. (should
  1673. (string-prefix-p
  1674. "* H <2012-03-30"
  1675. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1676. (org-clone-subtree-with-time-shift 1 "+1d")
  1677. (buffer-substring-no-properties (line-beginning-position 2)
  1678. (line-end-position 2)))))
  1679. (should
  1680. (string-prefix-p
  1681. "* H <2014-03-05"
  1682. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1683. (cl-letf (((symbol-function 'read-from-minibuffer)
  1684. (lambda (&rest args) "+1d")))
  1685. (org-clone-subtree-with-time-shift 1))
  1686. (buffer-substring-no-properties (line-beginning-position 2)
  1687. (line-end-position 2))))))
  1688. ;;; Fixed-Width Areas
  1689. (ert-deftest test-org/toggle-fixed-width ()
  1690. "Test `org-toggle-fixed-width' specifications."
  1691. ;; No region: Toggle on fixed-width marker in paragraphs.
  1692. (should
  1693. (equal ": A"
  1694. (org-test-with-temp-text "A"
  1695. (org-toggle-fixed-width)
  1696. (buffer-string))))
  1697. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1698. (should
  1699. (equal "A"
  1700. (org-test-with-temp-text ": A"
  1701. (org-toggle-fixed-width)
  1702. (buffer-string))))
  1703. ;; No region: Toggle on marker in blank lines after elements or just
  1704. ;; after a headline.
  1705. (should
  1706. (equal "* H\n: "
  1707. (org-test-with-temp-text "* H\n"
  1708. (forward-line)
  1709. (org-toggle-fixed-width)
  1710. (buffer-string))))
  1711. (should
  1712. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1713. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1714. (goto-char (point-max))
  1715. (org-toggle-fixed-width)
  1716. (buffer-string))))
  1717. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1718. ;; headlines, clocks)
  1719. (should
  1720. (equal ": * Headline"
  1721. (org-test-with-temp-text "* Headline"
  1722. (org-toggle-fixed-width)
  1723. (buffer-string))))
  1724. (should
  1725. (equal ": #+KEYWORD: value"
  1726. (org-test-with-temp-text "#+KEYWORD: value"
  1727. (org-toggle-fixed-width)
  1728. (buffer-string))))
  1729. ;; No region: error in other situations.
  1730. (should-error
  1731. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1732. (forward-line)
  1733. (org-toggle-fixed-width)
  1734. (buffer-string)))
  1735. ;; No region: Indentation is preserved.
  1736. (should
  1737. (equal "- A\n : B"
  1738. (org-test-with-temp-text "- A\n B"
  1739. (forward-line)
  1740. (org-toggle-fixed-width)
  1741. (buffer-string))))
  1742. ;; Region: If it contains only fixed-width elements and blank lines,
  1743. ;; toggle off fixed-width markup.
  1744. (should
  1745. (equal "A\n\nB"
  1746. (org-test-with-temp-text ": A\n\n: B"
  1747. (transient-mark-mode 1)
  1748. (push-mark (point) t t)
  1749. (goto-char (point-max))
  1750. (org-toggle-fixed-width)
  1751. (buffer-string))))
  1752. ;; Region: If it contains anything else, toggle on fixed-width but
  1753. ;; not on fixed-width areas.
  1754. (should
  1755. (equal ": A\n: \n: B\n: \n: C"
  1756. (org-test-with-temp-text "A\n\n: B\n\nC"
  1757. (transient-mark-mode 1)
  1758. (push-mark (point) t t)
  1759. (goto-char (point-max))
  1760. (org-toggle-fixed-width)
  1761. (buffer-string))))
  1762. ;; Region: Ignore blank lines at its end, unless it contains only
  1763. ;; such lines.
  1764. (should
  1765. (equal ": A\n\n"
  1766. (org-test-with-temp-text "A\n\n"
  1767. (transient-mark-mode 1)
  1768. (push-mark (point) t t)
  1769. (goto-char (point-max))
  1770. (org-toggle-fixed-width)
  1771. (buffer-string))))
  1772. (should
  1773. (equal ": \n: \n"
  1774. (org-test-with-temp-text "\n\n"
  1775. (transient-mark-mode 1)
  1776. (push-mark (point) t t)
  1777. (goto-char (point-max))
  1778. (org-toggle-fixed-width)
  1779. (buffer-string)))))
  1780. (ert-deftest test-org/kill-line ()
  1781. "Test `org-kill-line' specifications."
  1782. ;; At the beginning of a line, kill whole line.
  1783. (should
  1784. (equal ""
  1785. (org-test-with-temp-text "abc"
  1786. (org-kill-line)
  1787. (buffer-string))))
  1788. ;; In the middle of a line, kill line until its end.
  1789. (should
  1790. (equal "a"
  1791. (org-test-with-temp-text "a<point>bc"
  1792. (org-kill-line)
  1793. (buffer-string))))
  1794. ;; Do not kill newline character.
  1795. (should
  1796. (equal "\n123"
  1797. (org-test-with-temp-text "abc\n123"
  1798. (org-kill-line)
  1799. (buffer-string))))
  1800. (should
  1801. (equal "a\n123"
  1802. (org-test-with-temp-text "a<point>bc\n123"
  1803. (org-kill-line)
  1804. (buffer-string))))
  1805. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  1806. ;; kill until tags.
  1807. (should
  1808. (equal "* A :tag:"
  1809. (org-test-with-temp-text "* A<point>B :tag:"
  1810. (let ((org-special-ctrl-k t)
  1811. (org-tags-column 0))
  1812. (org-kill-line))
  1813. (buffer-string))))
  1814. ;; If point is on tags, only kill part left until the end of line.
  1815. (should
  1816. (equal "* A :tag:"
  1817. (org-test-with-temp-text "* A :tag:<point>tag2:"
  1818. (let ((org-special-ctrl-k t)
  1819. (org-tags-column 0))
  1820. (org-kill-line))
  1821. (buffer-string))))
  1822. ;; However, if point is at the beginning of the line, kill whole
  1823. ;; headline.
  1824. (should
  1825. (equal ""
  1826. (org-test-with-temp-text "* AB :tag:"
  1827. (let ((org-special-ctrl-k t)
  1828. (org-tags-column 0))
  1829. (org-kill-line))
  1830. (buffer-string))))
  1831. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  1832. ;; invisible text, ask before removing it. When set to `error',
  1833. ;; throw an error.
  1834. (should-error
  1835. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1836. (org-overview)
  1837. (let ((org-special-ctrl-k nil)
  1838. (org-ctrl-k-protect-subtree t))
  1839. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  1840. (org-kill-line)))))
  1841. (should-error
  1842. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  1843. (org-overview)
  1844. (let ((org-special-ctrl-k nil)
  1845. (org-ctrl-k-protect-subtree 'error))
  1846. (org-kill-line)))))
  1847. ;;; Headline
  1848. (ert-deftest test-org/get-heading ()
  1849. "Test `org-get-heading' specifications."
  1850. ;; Return current heading, even if point is not on it.
  1851. (should
  1852. (equal "H"
  1853. (org-test-with-temp-text "* H"
  1854. (org-get-heading))))
  1855. (should
  1856. (equal "H"
  1857. (org-test-with-temp-text "* H\nText<point>"
  1858. (org-get-heading))))
  1859. ;; Without any optional argument, return TODO keyword, priority
  1860. ;; cookie, COMMENT keyword and tags.
  1861. (should
  1862. (equal "TODO H"
  1863. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1864. (org-get-heading))))
  1865. (should
  1866. (equal "[#A] H"
  1867. (org-test-with-temp-text "* [#A] H"
  1868. (org-get-heading))))
  1869. (should
  1870. (equal "COMMENT H"
  1871. (org-test-with-temp-text "* COMMENT H"
  1872. (org-get-heading))))
  1873. (should
  1874. (equal "H :tag:"
  1875. (org-test-with-temp-text "* H :tag:"
  1876. (org-get-heading))))
  1877. ;; With NO-TAGS argument, ignore tags.
  1878. (should
  1879. (equal "TODO H"
  1880. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1881. (org-get-heading t))))
  1882. (should
  1883. (equal "H"
  1884. (org-test-with-temp-text "* H :tag:"
  1885. (org-get-heading t))))
  1886. ;; With NO-TODO, ignore TODO keyword.
  1887. (should
  1888. (equal "H"
  1889. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1890. (org-get-heading nil t))))
  1891. (should
  1892. (equal "H :tag:"
  1893. (org-test-with-temp-text "* H :tag:"
  1894. (org-get-heading nil t))))
  1895. ;; TODO keywords are case-sensitive.
  1896. (should
  1897. (equal "Todo H"
  1898. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1899. (org-get-heading nil t))))
  1900. ;; With NO-PRIORITY, ignore priority.
  1901. (should
  1902. (equal "H"
  1903. (org-test-with-temp-text "* [#A] H"
  1904. (org-get-heading nil nil t))))
  1905. (should
  1906. (equal "H"
  1907. (org-test-with-temp-text "* H"
  1908. (org-get-heading nil nil t))))
  1909. (should
  1910. (equal "TODO H"
  1911. (org-test-with-temp-text "* TODO [#A] H"
  1912. (org-get-heading nil nil t))))
  1913. ;; With NO-COMMENT, ignore COMMENT keyword.
  1914. (should
  1915. (equal "H"
  1916. (org-test-with-temp-text "* COMMENT H"
  1917. (org-get-heading nil nil nil t))))
  1918. (should
  1919. (equal "H"
  1920. (org-test-with-temp-text "* H"
  1921. (org-get-heading nil nil nil t))))
  1922. (should
  1923. (equal "TODO [#A] H"
  1924. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  1925. (org-get-heading nil nil nil t))))
  1926. ;; On an empty headline, return value is consistent.
  1927. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1928. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1929. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1930. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  1931. (should
  1932. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  1933. (ert-deftest test-org/in-commented-heading-p ()
  1934. "Test `org-in-commented-heading-p' specifications."
  1935. ;; Commented headline.
  1936. (should
  1937. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1938. (goto-char (point-max))
  1939. (org-in-commented-heading-p)))
  1940. ;; Commented ancestor.
  1941. (should
  1942. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1943. (goto-char (point-max))
  1944. (org-in-commented-heading-p)))
  1945. ;; Comment keyword is case-sensitive.
  1946. (should-not
  1947. (org-test-with-temp-text "* Comment Headline\nBody"
  1948. (goto-char (point-max))
  1949. (org-in-commented-heading-p)))
  1950. ;; Keyword is standalone.
  1951. (should-not
  1952. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1953. (goto-char (point-max))
  1954. (org-in-commented-heading-p)))
  1955. ;; Optional argument.
  1956. (should-not
  1957. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1958. (goto-char (point-max))
  1959. (org-in-commented-heading-p t))))
  1960. (ert-deftest test-org/entry-blocked-p ()
  1961. ;; Check other dependencies.
  1962. (should
  1963. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1964. (let ((org-enforce-todo-dependencies t)
  1965. (org-blocker-hook
  1966. '(org-block-todo-from-children-or-siblings-or-parent)))
  1967. (org-entry-blocked-p))))
  1968. (should-not
  1969. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1970. (let ((org-enforce-todo-dependencies t)
  1971. (org-blocker-hook
  1972. '(org-block-todo-from-children-or-siblings-or-parent)))
  1973. (org-entry-blocked-p))))
  1974. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1975. ;; blocked.
  1976. (should-not
  1977. (org-test-with-temp-text "* Blocked\n** TODO one"
  1978. (let ((org-enforce-todo-dependencies t)
  1979. (org-blocker-hook
  1980. '(org-block-todo-from-children-or-siblings-or-parent)))
  1981. (org-entry-blocked-p))))
  1982. (should-not
  1983. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1984. (let ((org-enforce-todo-dependencies t)
  1985. (org-blocker-hook
  1986. '(org-block-todo-from-children-or-siblings-or-parent)))
  1987. (org-entry-blocked-p))))
  1988. ;; Follow :ORDERED: specifications.
  1989. (should
  1990. (org-test-with-temp-text
  1991. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1992. (let ((org-enforce-todo-dependencies t)
  1993. (org-blocker-hook
  1994. '(org-block-todo-from-children-or-siblings-or-parent)))
  1995. (org-entry-blocked-p))))
  1996. (should-not
  1997. (org-test-with-temp-text
  1998. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1999. (let ((org-enforce-todo-dependencies t)
  2000. (org-blocker-hook
  2001. '(org-block-todo-from-children-or-siblings-or-parent)))
  2002. (org-entry-blocked-p)))))
  2003. (ert-deftest test-org/get-outline-path ()
  2004. "Test `org-get-outline-path' specifications."
  2005. ;; Top-level headlines have no outline path.
  2006. (should-not
  2007. (org-test-with-temp-text "* H"
  2008. (org-get-outline-path)))
  2009. ;; Otherwise, outline path is the path leading to the headline.
  2010. (should
  2011. (equal '("H")
  2012. (org-test-with-temp-text "* H\n** S<point>"
  2013. (org-get-outline-path))))
  2014. ;; Find path even when point is not on a headline.
  2015. (should
  2016. (equal '("H")
  2017. (org-test-with-temp-text "* H\n** S\nText<point>"
  2018. (org-get-outline-path))))
  2019. ;; TODO keywords, tags and statistics cookies are ignored.
  2020. (should
  2021. (equal '("H")
  2022. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  2023. (org-get-outline-path))))
  2024. ;; Links are replaced with their description or their path.
  2025. (should
  2026. (equal '("Org")
  2027. (org-test-with-temp-text
  2028. "* [[https://orgmode.org][Org]]\n** S<point>"
  2029. (org-get-outline-path))))
  2030. (should
  2031. (equal '("https://orgmode.org")
  2032. (org-test-with-temp-text
  2033. "* [[https://orgmode.org]]\n** S<point>"
  2034. (org-get-outline-path))))
  2035. ;; When WITH-SELF is non-nil, include current heading.
  2036. (should
  2037. (equal '("H")
  2038. (org-test-with-temp-text "* H"
  2039. (org-get-outline-path t))))
  2040. (should
  2041. (equal '("H" "S")
  2042. (org-test-with-temp-text "* H\n** S\nText<point>"
  2043. (org-get-outline-path t))))
  2044. ;; Using cache is transparent to the user.
  2045. (should
  2046. (equal '("H")
  2047. (org-test-with-temp-text "* H\n** S<point>"
  2048. (setq org-outline-path-cache nil)
  2049. (org-get-outline-path nil t))))
  2050. ;; Do not corrupt cache when finding outline path in distant part of
  2051. ;; the buffer.
  2052. (should
  2053. (equal '("H2")
  2054. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  2055. (setq org-outline-path-cache nil)
  2056. (org-get-outline-path nil t)
  2057. (search-forward "S2")
  2058. (org-get-outline-path nil t))))
  2059. ;; Do not choke on empty headlines.
  2060. (should
  2061. (org-test-with-temp-text "* H\n** <point>"
  2062. (org-get-outline-path)))
  2063. (should
  2064. (org-test-with-temp-text "* \n** H<point>"
  2065. (org-get-outline-path))))
  2066. (ert-deftest test-org/format-outline-path ()
  2067. "Test `org-format-outline-path' specifications."
  2068. (should
  2069. (string= (org-format-outline-path (list "one" "two" "three"))
  2070. "one/two/three"))
  2071. ;; Empty path.
  2072. (should
  2073. (string= (org-format-outline-path '())
  2074. ""))
  2075. (should
  2076. (string= (org-format-outline-path '(nil))
  2077. ""))
  2078. ;; Empty path and prefix.
  2079. (should
  2080. (string= (org-format-outline-path '() nil ">>")
  2081. ">>"))
  2082. ;; Trailing whitespace in headings.
  2083. (should
  2084. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  2085. "one/tw o/three"))
  2086. ;; Non-default prefix and separators.
  2087. (should
  2088. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  2089. ">>|one|two|three"))
  2090. ;; Truncate.
  2091. (should
  2092. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  2093. "one/two/.."))
  2094. ;; Give a very narrow width.
  2095. (should
  2096. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  2097. "on"))
  2098. ;; Give a prefix that extends beyond the width.
  2099. (should
  2100. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  2101. ">>>>>>>>>>")
  2102. ">>>>>>>>..")))
  2103. (ert-deftest test-org/map-entries ()
  2104. "Test `org-map-entries' specifications."
  2105. ;; Full match.
  2106. (should
  2107. (equal '(1 11)
  2108. (org-test-with-temp-text "* Level 1\n** Level 2"
  2109. (org-map-entries #'point))))
  2110. ;; Level match.
  2111. (should
  2112. (equal '(1)
  2113. (org-test-with-temp-text "* Level 1\n** Level 2"
  2114. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  2115. (should
  2116. (equal '(11)
  2117. (org-test-with-temp-text "* Level 1\n** Level 2"
  2118. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  2119. ;; Tag match.
  2120. (should
  2121. (equal '(11)
  2122. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  2123. (org-map-entries #'point "yes"))))
  2124. (should
  2125. (equal '(14)
  2126. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  2127. (org-map-entries #'point "+yes-a"))))
  2128. (should
  2129. (equal '(11 23)
  2130. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  2131. (org-map-entries #'point "{yes?}"))))
  2132. ;; Priority match.
  2133. (should
  2134. (equal '(1)
  2135. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  2136. (org-map-entries #'point "PRIORITY=\"A\""))))
  2137. ;; Date match.
  2138. (should
  2139. (equal '(36)
  2140. (org-test-with-temp-text "
  2141. * H1
  2142. SCHEDULED: <2012-03-29 thu.>
  2143. * H2
  2144. SCHEDULED: <2014-03-04 tue.>"
  2145. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  2146. (should
  2147. (equal '(2)
  2148. (org-test-with-temp-text "
  2149. * H1
  2150. SCHEDULED: <2012-03-29 thu.>
  2151. * H2
  2152. SCHEDULED: <2014-03-04 tue.>"
  2153. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  2154. ;; Regular property match.
  2155. (should
  2156. (equal '(2)
  2157. (org-test-with-temp-text "
  2158. * H1
  2159. :PROPERTIES:
  2160. :TEST: 1
  2161. :END:
  2162. * H2
  2163. :PROPERTIES:
  2164. :TEST: 2
  2165. :END:"
  2166. (org-map-entries #'point "TEST=1"))))
  2167. ;; Multiple criteria.
  2168. (should
  2169. (equal '(23)
  2170. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  2171. (let (org-odd-levels-only
  2172. (org-use-tag-inheritance nil))
  2173. (org-map-entries #'point "yes+LEVEL=1")))))
  2174. ;; "or" criteria.
  2175. (should
  2176. (equal '(12 24)
  2177. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  2178. (let (org-odd-levels-only)
  2179. (org-map-entries #'point "LEVEL=2|no")))))
  2180. (should
  2181. (equal '(1 12)
  2182. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  2183. (let (org-odd-levels-only)
  2184. (org-map-entries #'point "yes|no")))))
  2185. ;; "and" criteria.
  2186. (should
  2187. (equal '(22)
  2188. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  2189. (let (org-odd-levels-only)
  2190. (org-map-entries #'point "yes&no"))))))
  2191. (ert-deftest test-org/edit-headline ()
  2192. "Test `org-edit-headline' specifications."
  2193. (should
  2194. (equal "* B"
  2195. (org-test-with-temp-text "* A"
  2196. (org-edit-headline "B")
  2197. (buffer-string))))
  2198. ;; Handle empty headings.
  2199. (should
  2200. (equal "* "
  2201. (org-test-with-temp-text "* A"
  2202. (org-edit-headline "")
  2203. (buffer-string))))
  2204. (should
  2205. (equal "* A"
  2206. (org-test-with-temp-text "* "
  2207. (org-edit-headline "A")
  2208. (buffer-string))))
  2209. ;; Handle TODO keywords and priority cookies.
  2210. (should
  2211. (equal "* TODO B"
  2212. (org-test-with-temp-text "* TODO A"
  2213. (org-edit-headline "B")
  2214. (buffer-string))))
  2215. (should
  2216. (equal "* [#A] B"
  2217. (org-test-with-temp-text "* [#A] A"
  2218. (org-edit-headline "B")
  2219. (buffer-string))))
  2220. (should
  2221. (equal "* TODO [#A] B"
  2222. (org-test-with-temp-text "* TODO [#A] A"
  2223. (org-edit-headline "B")
  2224. (buffer-string))))
  2225. ;; Handle tags.
  2226. (equal "* B :tag:"
  2227. (org-test-with-temp-text "* A :tag:"
  2228. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2229. (buffer-string))))
  2230. ;;; Keywords
  2231. (ert-deftest test-org/set-regexps-and-options ()
  2232. "Test `org-set-regexps-and-options' specifications."
  2233. ;; TAGS keyword.
  2234. (should
  2235. (equal '(("A"))
  2236. (let ((org-tag-alist '(("A")))
  2237. (org-tag-persistent-alist nil))
  2238. (org-test-with-temp-text ""
  2239. (org-mode-restart)
  2240. org-current-tag-alist))))
  2241. (should
  2242. (equal '(("B"))
  2243. (let ((org-tag-alist '(("A")))
  2244. (org-tag-persistent-alist nil))
  2245. (org-test-with-temp-text "#+TAGS: B"
  2246. (org-mode-restart)
  2247. org-current-tag-alist))))
  2248. (should
  2249. (equal '(("C") ("B"))
  2250. (let ((org-tag-alist '(("A")))
  2251. (org-tag-persistent-alist '(("C"))))
  2252. (org-test-with-temp-text "#+TAGS: B"
  2253. (org-mode-restart)
  2254. org-current-tag-alist))))
  2255. (should
  2256. (equal '(("B"))
  2257. (let ((org-tag-alist '(("A")))
  2258. (org-tag-persistent-alist '(("C"))))
  2259. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2260. (org-mode-restart)
  2261. org-current-tag-alist))))
  2262. (should
  2263. (equal '(("A" . ?a) ("B") ("C"))
  2264. (let ((org-tag-persistant-alist nil))
  2265. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2266. (org-mode-restart)
  2267. org-current-tag-alist))))
  2268. (should
  2269. (equal '(("A") (:newline) ("B"))
  2270. (let ((org-tag-persistent-alist nil))
  2271. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2272. (org-mode-restart)
  2273. org-current-tag-alist))))
  2274. (should
  2275. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2276. (let ((org-tag-persistent-alist nil))
  2277. (org-test-with-temp-text "#+TAGS: { A B } C"
  2278. (org-mode-restart)
  2279. org-current-tag-alist))))
  2280. (should
  2281. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2282. (let ((org-tag-persistent-alist nil))
  2283. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2284. (org-mode-restart)
  2285. org-current-tag-alist))))
  2286. (should
  2287. (equal '(("A" "B" "C"))
  2288. (let ((org-tag-persistent-alist nil))
  2289. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2290. (org-mode-restart)
  2291. org-tag-groups-alist))))
  2292. (should
  2293. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2294. (let ((org-tag-persistent-alist nil))
  2295. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2296. (org-mode-restart)
  2297. org-current-tag-alist))))
  2298. (should
  2299. (equal '(("A" "B" "C"))
  2300. (let ((org-tag-persistent-alist nil))
  2301. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2302. (org-mode-restart)
  2303. org-tag-groups-alist))))
  2304. ;; FILETAGS keyword.
  2305. (should
  2306. (equal '("A" "B" "C")
  2307. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2308. (org-mode-restart)
  2309. org-file-tags)))
  2310. ;; PROPERTY keyword. Property names are case-insensitive.
  2311. (should
  2312. (equal "foo=1"
  2313. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2314. (org-mode-restart)
  2315. (cdr (assoc "var" org-keyword-properties)))))
  2316. (should
  2317. (equal
  2318. "foo=1 bar=2"
  2319. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2320. (org-mode-restart)
  2321. (cdr (assoc "var" org-keyword-properties)))))
  2322. (should
  2323. (equal
  2324. "foo=1 bar=2"
  2325. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2326. (org-mode-restart)
  2327. (cdr (assoc "var" org-keyword-properties)))))
  2328. ;; ARCHIVE keyword.
  2329. (should
  2330. (equal "%s_done::"
  2331. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2332. (org-mode-restart)
  2333. org-archive-location)))
  2334. ;; CATEGORY keyword.
  2335. (should
  2336. (eq 'test
  2337. (org-test-with-temp-text "#+CATEGORY: test"
  2338. (org-mode-restart)
  2339. org-category)))
  2340. (should
  2341. (equal "test"
  2342. (org-test-with-temp-text "#+CATEGORY: test"
  2343. (org-mode-restart)
  2344. (cdr (assoc "CATEGORY" org-keyword-properties)))))
  2345. ;; COLUMNS keyword.
  2346. (should
  2347. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2348. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2349. (org-mode-restart)
  2350. org-columns-default-format)))
  2351. ;; CONSTANTS keyword. Constants names are case sensitive.
  2352. (should
  2353. (equal '("299792458." "3.14")
  2354. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2355. (org-mode-restart)
  2356. (mapcar
  2357. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2358. '("c" "pi")))))
  2359. (should
  2360. (equal "3.14"
  2361. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2362. (org-mode-restart)
  2363. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2364. (should
  2365. (equal "22/7"
  2366. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2367. (org-mode-restart)
  2368. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2369. ;; LINK keyword.
  2370. (should
  2371. (equal
  2372. '("url1" "url2")
  2373. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2374. (org-mode-restart)
  2375. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2376. '("a" "b")))))
  2377. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2378. (should
  2379. (equal
  2380. '(?X ?Z ?Y)
  2381. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2382. (org-mode-restart)
  2383. (list org-priority-highest org-priority-lowest org-priority-default))))
  2384. (should
  2385. (equal
  2386. '(?A ?C ?B)
  2387. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2388. (org-mode-restart)
  2389. (list org-priority-highest org-priority-lowest org-priority-default))))
  2390. ;; STARTUP keyword.
  2391. (should
  2392. (equal '(t t)
  2393. (org-test-with-temp-text "#+STARTUP: fold odd"
  2394. (org-mode-restart)
  2395. (list org-startup-folded org-odd-levels-only))))
  2396. ;; TODO keywords.
  2397. (should
  2398. (equal '(("A" "B") ("C"))
  2399. (org-test-with-temp-text "#+TODO: A B | C"
  2400. (org-mode-restart)
  2401. (list org-not-done-keywords org-done-keywords))))
  2402. (should
  2403. (equal '(("A" "C") ("B" "D"))
  2404. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2405. (org-mode-restart)
  2406. (list org-not-done-keywords org-done-keywords))))
  2407. (should
  2408. (equal '(("A" "B") ("C"))
  2409. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2410. (org-mode-restart)
  2411. (list org-not-done-keywords org-done-keywords))))
  2412. (should
  2413. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2414. (org-test-with-temp-text "#+TODO: A(a)"
  2415. (org-mode-restart)
  2416. org-todo-key-alist)))
  2417. (should
  2418. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2419. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2420. (org-mode-restart)
  2421. org-todo-log-states)))
  2422. ;; Enter SETUPFILE keyword.
  2423. (should
  2424. (equal "1"
  2425. (org-test-with-temp-text
  2426. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2427. (org-mode-restart)
  2428. (cdr (assoc "a" org-keyword-properties))))))
  2429. ;;; Links
  2430. ;;;; Coderefs
  2431. (ert-deftest test-org/coderef ()
  2432. "Test coderef links specifications."
  2433. (should
  2434. (org-test-with-temp-text "
  2435. #+BEGIN_SRC emacs-lisp
  2436. \(+ 1 1) (ref:sc)
  2437. #+END_SRC
  2438. \[[(sc)<point>]]"
  2439. (org-open-at-point)
  2440. (looking-at "(ref:sc)")))
  2441. ;; Find coderef even with alternate label format.
  2442. (should
  2443. (org-test-with-temp-text "
  2444. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2445. \(+ 1 1) {ref:sc}
  2446. #+END_SRC
  2447. \[[(sc)<point>]]"
  2448. (org-open-at-point)
  2449. (looking-at "{ref:sc}"))))
  2450. ;;;; Custom ID
  2451. (ert-deftest test-org/custom-id ()
  2452. "Test custom ID links specifications."
  2453. (should
  2454. (org-test-with-temp-text
  2455. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2456. (org-open-at-point)
  2457. (looking-at-p "\\* H1")))
  2458. ;; Handle escape characters.
  2459. (should
  2460. (org-test-with-temp-text
  2461. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#\\[%\\]<point>]]"
  2462. (org-open-at-point)
  2463. (looking-at-p "\\* H1")))
  2464. ;; Throw an error on false positives.
  2465. (should-error
  2466. (org-test-with-temp-text
  2467. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2468. (org-open-at-point)
  2469. (looking-at-p "\\* H1"))))
  2470. ;;;; Fuzzy Links
  2471. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2472. ;; a named element (#+name: text) and to headlines (* Text).
  2473. (ert-deftest test-org/fuzzy-links ()
  2474. "Test fuzzy links specifications."
  2475. ;; Fuzzy link goes in priority to a matching target.
  2476. (should
  2477. (org-test-with-temp-text
  2478. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2479. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2480. (looking-at "<<Test>>")))
  2481. ;; Then fuzzy link points to an element with a given name.
  2482. (should
  2483. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2484. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2485. (looking-at "#\\+NAME: Test")))
  2486. ;; A target still lead to a matching headline otherwise.
  2487. (should
  2488. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2489. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2490. (looking-at "\\* Head2")))
  2491. ;; With a leading star in link, enforce heading match.
  2492. (should
  2493. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2494. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2495. (looking-at "\\* Test")))
  2496. ;; With a leading star in link, enforce exact heading match, even
  2497. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2498. (should-error
  2499. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2500. (let ((org-link-search-must-match-exact-headline nil))
  2501. (org-open-at-point))))
  2502. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2503. (should
  2504. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2505. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2506. (looking-at "\\* Test")))
  2507. (should
  2508. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2509. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2510. (looking-at "\\* Test")))
  2511. ;; Heading match should not care about spaces, cookies, TODO
  2512. ;; keywords, priorities, and tags. However, TODO keywords are
  2513. ;; case-sensitive.
  2514. (should
  2515. (let ((first-line
  2516. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2517. (org-test-with-temp-text
  2518. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2519. (let ((org-link-search-must-match-exact-headline nil)
  2520. (org-todo-regexp "TODO"))
  2521. (org-open-at-point))
  2522. (looking-at (regexp-quote first-line)))))
  2523. (should-error
  2524. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2525. (let ((org-link-search-must-match-exact-headline nil)
  2526. (org-todo-regexp "TODO"))
  2527. (org-open-at-point))))
  2528. ;; Heading match should still be exact.
  2529. (should-error
  2530. (org-test-with-temp-text "
  2531. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2532. Foo Bar
  2533. <point>[[*Test 1]]"
  2534. (let ((org-link-search-must-match-exact-headline nil)
  2535. (org-todo-regexp "TODO"))
  2536. (org-open-at-point))))
  2537. (should
  2538. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2539. (let ((org-link-search-must-match-exact-headline nil)
  2540. (org-todo-regexp "TODO"))
  2541. (org-open-at-point))
  2542. (looking-at-p (regexp-quote "** Test 1 2"))))
  2543. ;; Heading match ignores COMMENT keyword.
  2544. (should
  2545. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2546. (org-open-at-point)
  2547. (looking-at "\\* COMMENT Test")))
  2548. (should
  2549. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2550. (org-open-at-point)
  2551. (looking-at "\\* TODO COMMENT Test")))
  2552. ;; Correctly un-escape fuzzy links.
  2553. (should
  2554. (org-test-with-temp-text "* [foo]\n[[*\\[foo\\]][With escaped characters]]"
  2555. (org-open-at-point)
  2556. (bobp)))
  2557. ;; Match search strings containing newline characters, including
  2558. ;; blank lines.
  2559. (should
  2560. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2561. (let ((file (buffer-file-name)))
  2562. (goto-char (point-max))
  2563. (insert (format "[[file:%s::line1 line2]]" file))
  2564. (beginning-of-line)
  2565. (let ((org-link-search-must-match-exact-headline nil))
  2566. (org-open-at-point 0))
  2567. (looking-at-p "line1"))))
  2568. (should
  2569. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\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. ;;;; Open at point
  2578. (ert-deftest test-org/open-at-point/keyword ()
  2579. "Does `org-open-at-point' open link in a keyword line?"
  2580. (should
  2581. (org-test-with-temp-text
  2582. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2583. (org-open-at-point) t))
  2584. (should
  2585. (org-test-with-temp-text
  2586. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2587. (org-open-at-point) t)))
  2588. (ert-deftest test-org/open-at-point/property ()
  2589. "Does `org-open-at-point' open link in property drawer?"
  2590. (should
  2591. (org-test-with-temp-text
  2592. "* Headline
  2593. :PROPERTIES:
  2594. :URL: <point>[[*Headline]]
  2595. :END:"
  2596. (org-open-at-point) t)))
  2597. (ert-deftest test-org/open-at-point/comment ()
  2598. "Does `org-open-at-point' open link in a commented line?"
  2599. (should
  2600. (org-test-with-temp-text
  2601. "<<top>>\n# <point>[[top]]"
  2602. (org-open-at-point) t))
  2603. (should
  2604. (org-test-with-temp-text
  2605. "* H\n<<top>>\n# <point>[[top]]"
  2606. (org-open-at-point) t)))
  2607. (ert-deftest test-org/open-at-point/inline-image ()
  2608. "Test `org-open-at-point' on nested links."
  2609. (should
  2610. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2611. (org-open-at-point)
  2612. (bobp))))
  2613. (ert-deftest test-org/open-at-point/radio-target ()
  2614. "Test `org-open-at-point' on radio targets."
  2615. (should
  2616. (org-test-with-temp-text "<<<target>>> <point>target"
  2617. (org-update-radio-target-regexp)
  2618. (org-open-at-point)
  2619. (eq (org-element-type (org-element-context)) 'radio-target))))
  2620. (ert-deftest test-org/open-at-point/tag ()
  2621. "Test `org-open-at-point' on tags."
  2622. (should
  2623. (org-test-with-temp-text "* H :<point>tag:"
  2624. (catch :result
  2625. (cl-letf (((symbol-function 'org-tags-view)
  2626. (lambda (&rest args) (throw :result t))))
  2627. (org-open-at-point)
  2628. nil))))
  2629. (should-not
  2630. (org-test-with-temp-text-in-file "* H<point> :tag:"
  2631. (catch :result
  2632. (cl-letf (((symbol-function 'org-tags-view)
  2633. (lambda (&rest args) (throw :result t))))
  2634. ;; When point isn't on a tag it's going to try other things,
  2635. ;; possibly trying to open attachments which will return an
  2636. ;; error if there isn't an attachment. Suppress that error.
  2637. (ignore-errors
  2638. (org-open-at-point))
  2639. nil)))))
  2640. ;;; Node Properties
  2641. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2642. "Ensure properties accumulate in subtree drawers."
  2643. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2644. (org-babel-next-src-block)
  2645. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2646. (ert-deftest test-org/custom-properties ()
  2647. "Test custom properties specifications."
  2648. ;; Standard test.
  2649. (should
  2650. (let ((org-custom-properties '("FOO")))
  2651. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2652. (org-toggle-custom-properties-visibility)
  2653. (org-invisible-p2))))
  2654. ;; Properties are case-insensitive.
  2655. (should
  2656. (let ((org-custom-properties '("FOO")))
  2657. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2658. (org-toggle-custom-properties-visibility)
  2659. (org-invisible-p2))))
  2660. (should
  2661. (let ((org-custom-properties '("foo")))
  2662. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2663. (org-toggle-custom-properties-visibility)
  2664. (org-invisible-p2))))
  2665. ;; Multiple custom properties in the same drawer.
  2666. (should
  2667. (let ((org-custom-properties '("FOO" "BAR")))
  2668. (org-test-with-temp-text
  2669. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2670. (org-toggle-custom-properties-visibility)
  2671. (and (org-invisible-p2)
  2672. (not (progn (forward-line) (org-invisible-p2)))
  2673. (progn (forward-line) (org-invisible-p2))))))
  2674. ;; Hide custom properties with an empty value.
  2675. (should
  2676. (let ((org-custom-properties '("FOO")))
  2677. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2678. (org-toggle-custom-properties-visibility)
  2679. (org-invisible-p2))))
  2680. ;; Do not hide fake properties.
  2681. (should-not
  2682. (let ((org-custom-properties '("FOO")))
  2683. (org-test-with-temp-text ":FOO: val\n"
  2684. (org-toggle-custom-properties-visibility)
  2685. (org-invisible-p2))))
  2686. (should-not
  2687. (let ((org-custom-properties '("A")))
  2688. (org-test-with-temp-text
  2689. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2690. (org-toggle-custom-properties-visibility)
  2691. (org-invisible-p2)))))
  2692. ;;; Mark Region
  2693. (ert-deftest test-org/mark-subtree ()
  2694. "Test `org-mark-subtree' specifications."
  2695. ;; Error when point is before first headline.
  2696. (should-error
  2697. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2698. (progn (transient-mark-mode 1)
  2699. (org-mark-subtree))))
  2700. ;; Without argument, mark current subtree.
  2701. (should
  2702. (equal
  2703. '(12 32)
  2704. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2705. (progn (transient-mark-mode 1)
  2706. (forward-line 2)
  2707. (org-mark-subtree)
  2708. (list (region-beginning) (region-end))))))
  2709. ;; With an argument, move ARG up.
  2710. (should
  2711. (equal
  2712. '(1 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 1)
  2717. (list (region-beginning) (region-end))))))
  2718. ;; Do not get fooled by inlinetasks.
  2719. (when (featurep 'org-inlinetask)
  2720. (should
  2721. (= 1
  2722. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2723. (progn (transient-mark-mode 1)
  2724. (forward-line 1)
  2725. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2726. (region-beginning)))))))
  2727. ;;; Miscellaneous
  2728. (ert-deftest test-org/sort-entries ()
  2729. "Test `org-sort-entries'."
  2730. ;; Sort alphabetically.
  2731. (should
  2732. (equal "\n* abc\n* def\n* xyz\n"
  2733. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2734. (org-sort-entries nil ?a)
  2735. (buffer-string))))
  2736. (should
  2737. (equal "\n* xyz\n* def\n* abc\n"
  2738. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2739. (org-sort-entries nil ?A)
  2740. (buffer-string))))
  2741. (should
  2742. (equal "\n* \n* klm\n* xyz\n"
  2743. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  2744. (org-sort-entries nil ?a)
  2745. (buffer-string))))
  2746. ;; Sort numerically.
  2747. (should
  2748. (equal "\n* 1\n* 2\n* 10\n"
  2749. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2750. (org-sort-entries nil ?n)
  2751. (buffer-string))))
  2752. (should
  2753. (equal "\n* 10\n* 2\n* 1\n"
  2754. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2755. (org-sort-entries nil ?N)
  2756. (buffer-string))))
  2757. (should
  2758. (equal "\n* \n* 1\n* 2\n"
  2759. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  2760. (org-sort-entries nil ?n)
  2761. (buffer-string))))
  2762. ;; Sort by custom function.
  2763. (should
  2764. (equal "\n* b\n* aa\n* ccc\n"
  2765. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2766. (org-sort-entries nil ?f
  2767. (lambda ()
  2768. (length (buffer-substring (point-at-bol)
  2769. (point-at-eol))))
  2770. #'<)
  2771. (buffer-string))))
  2772. (should
  2773. (equal "\n* ccc\n* aa\n* b\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. ;; Sort by TODO keyword.
  2782. (should
  2783. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2784. (org-test-with-temp-text
  2785. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2786. (org-sort-entries nil ?o)
  2787. (buffer-string))))
  2788. (should
  2789. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2790. (org-test-with-temp-text
  2791. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2792. (org-sort-entries nil ?O)
  2793. (buffer-string))))
  2794. ;; Sort by priority.
  2795. (should
  2796. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2797. (org-test-with-temp-text
  2798. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2799. (org-sort-entries nil ?p)
  2800. (buffer-string))))
  2801. (should
  2802. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2803. (org-test-with-temp-text
  2804. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2805. (org-sort-entries nil ?P)
  2806. (buffer-string))))
  2807. ;; Sort by creation time.
  2808. (should
  2809. (equal "
  2810. * h3
  2811. [2017-05-08 Mon]
  2812. * h2
  2813. [2017-05-09 Tue]
  2814. * h1
  2815. [2018-05-09 Wed]
  2816. "
  2817. (org-test-with-temp-text
  2818. "
  2819. * h1
  2820. [2018-05-09 Wed]
  2821. * h2
  2822. [2017-05-09 Tue]
  2823. * h3
  2824. [2017-05-08 Mon]
  2825. "
  2826. (org-sort-entries nil ?c)
  2827. (buffer-string))))
  2828. ;; Sort by scheduled date.
  2829. (should
  2830. (equal "
  2831. * TODO h4
  2832. SCHEDULED: <2017-05-06 Sat>
  2833. * TODO h3
  2834. SCHEDULED: <2017-05-08 Mon>
  2835. * TODO h2
  2836. DEADLINE: <2017-05-09 Tue>
  2837. * TODO h1
  2838. DEADLINE: <2017-05-07 Sun>
  2839. "
  2840. (org-test-with-temp-text
  2841. "
  2842. * TODO h2
  2843. DEADLINE: <2017-05-09 Tue>
  2844. * TODO h1
  2845. DEADLINE: <2017-05-07 Sun>
  2846. * TODO h3
  2847. SCHEDULED: <2017-05-08 Mon>
  2848. * TODO h4
  2849. SCHEDULED: <2017-05-06 Sat>
  2850. "
  2851. (org-sort-entries nil ?s)
  2852. (buffer-string))))
  2853. ;; Sort by deadline date.
  2854. (should
  2855. (equal "
  2856. * TODO h1
  2857. DEADLINE: <2017-05-07 Sun>
  2858. * TODO h2
  2859. DEADLINE: <2017-05-09 Tue>
  2860. * TODO h3
  2861. SCHEDULED: <2017-05-08 Mon>
  2862. * TODO h4
  2863. SCHEDULED: <2017-05-06 Sat>
  2864. "
  2865. (org-test-with-temp-text
  2866. "
  2867. * TODO h2
  2868. DEADLINE: <2017-05-09 Tue>
  2869. * TODO h1
  2870. DEADLINE: <2017-05-07 Sun>
  2871. * TODO h3
  2872. SCHEDULED: <2017-05-08 Mon>
  2873. * TODO h4
  2874. SCHEDULED: <2017-05-06 Sat>
  2875. "
  2876. (org-sort-entries nil ?d)
  2877. (buffer-string))))
  2878. ;; Sort by any date/time
  2879. (should
  2880. (equal "
  2881. * TODO h4
  2882. SCHEDULED: <2017-05-06 Sat>
  2883. * TODO h1
  2884. DEADLINE: <2017-05-07 Sun>
  2885. * TODO h3
  2886. SCHEDULED: <2017-05-08 Mon>
  2887. * TODO h2
  2888. DEADLINE: <2017-05-09 Tue>
  2889. "
  2890. (org-test-with-temp-text
  2891. "
  2892. * TODO h2
  2893. DEADLINE: <2017-05-09 Tue>
  2894. * TODO h1
  2895. DEADLINE: <2017-05-07 Sun>
  2896. * TODO h3
  2897. SCHEDULED: <2017-05-08 Mon>
  2898. * TODO h4
  2899. SCHEDULED: <2017-05-06 Sat>
  2900. "
  2901. (org-sort-entries nil ?t)
  2902. (buffer-string))))
  2903. ;; Sort by clocking time.
  2904. (should
  2905. (equal "
  2906. * clocked h2
  2907. :LOGBOOK:
  2908. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2909. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2910. :END:
  2911. * clocked h1
  2912. :LOGBOOK:
  2913. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2914. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2915. :END:
  2916. "
  2917. (org-test-with-temp-text
  2918. "
  2919. * clocked h1
  2920. :LOGBOOK:
  2921. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2922. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2923. :END:
  2924. * clocked h2
  2925. :LOGBOOK:
  2926. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2927. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2928. :END:
  2929. "
  2930. (org-sort-entries nil ?k)
  2931. (buffer-string))))
  2932. ;; Preserve file local variables when sorting.
  2933. (should
  2934. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  2935. (org-test-with-temp-text
  2936. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  2937. (org-sort-entries nil ?a)
  2938. (buffer-string)))))
  2939. (ert-deftest test-org/string-collate-greaterp ()
  2940. "Test `org-string-collate-greaterp' specifications."
  2941. (should (org-string-collate-greaterp "def" "abc"))
  2942. (should-not (org-string-collate-greaterp "abc" "def")))
  2943. (ert-deftest test-org/file-contents ()
  2944. "Test `org-file-contents' specifications."
  2945. ;; Open files.
  2946. (should
  2947. (string= "#+BIND: variable value
  2948. #+DESCRIPTION: l2
  2949. #+LANGUAGE: en
  2950. #+SELECT_TAGS: b
  2951. #+TITLE: b
  2952. #+PROPERTY: a 1
  2953. " (org-file-contents (expand-file-name "setupfile3.org"
  2954. (concat org-test-dir "examples/")))))
  2955. ;; Throw error when trying to access an invalid file.
  2956. (should-error (org-file-contents "this-file-must-not-exist"))
  2957. ;; Try to access an invalid file, but do not throw an error.
  2958. (should
  2959. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  2960. ;; Open URL.
  2961. (should
  2962. (string= "foo"
  2963. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2964. (unwind-protect
  2965. ;; Simulate successful retrieval of a URL.
  2966. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2967. (lambda (&rest_)
  2968. (with-current-buffer buffer
  2969. (insert "HTTP/1.1 200 OK\n\nfoo"))
  2970. buffer)))
  2971. (org-file-contents "http://some-valid-url"))
  2972. (kill-buffer buffer)))))
  2973. ;; Throw error when trying to access an invalid URL.
  2974. (should-error
  2975. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2976. (unwind-protect
  2977. ;; Simulate unsuccessful retrieval of a URL.
  2978. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2979. (lambda (&rest_)
  2980. (with-current-buffer buffer
  2981. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2982. buffer)))
  2983. (org-file-contents "http://this-url-must-not-exist"))
  2984. (kill-buffer buffer))))
  2985. ;; Try to access an invalid URL, but do not throw an error.
  2986. (should-error
  2987. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2988. (unwind-protect
  2989. ;; Simulate unsuccessful retrieval of a URL.
  2990. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  2991. (lambda (&rest_)
  2992. (with-current-buffer buffer
  2993. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  2994. buffer)))
  2995. (org-file-contents "http://this-url-must-not-exist"))
  2996. (kill-buffer buffer))))
  2997. (should
  2998. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  2999. (unwind-protect
  3000. ;; Simulate unsuccessful retrieval of a URL.
  3001. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3002. (lambda (&rest_)
  3003. (with-current-buffer buffer
  3004. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3005. buffer)))
  3006. (org-file-contents "http://this-url-must-not-exist" :noerror))
  3007. (kill-buffer buffer))
  3008. t)))
  3009. ;;; Navigation
  3010. (ert-deftest test-org/forward-heading-same-level ()
  3011. "Test `org-forward-heading-same-level' specifications."
  3012. ;; Test navigation at top level, forward and backward.
  3013. (should
  3014. (equal "* H2"
  3015. (org-test-with-temp-text "* H1\n* H2"
  3016. (org-forward-heading-same-level 1)
  3017. (buffer-substring-no-properties (point) (line-end-position)))))
  3018. (should
  3019. (equal "* H1"
  3020. (org-test-with-temp-text "* H1\n<point>* H2"
  3021. (org-forward-heading-same-level -1)
  3022. (buffer-substring-no-properties (point) (line-end-position)))))
  3023. ;; Test navigation in a sub-tree, forward and backward.
  3024. (should
  3025. (equal "* H2"
  3026. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3027. (org-forward-heading-same-level 1)
  3028. (buffer-substring-no-properties (point) (line-end-position)))))
  3029. (should
  3030. (equal "* H1"
  3031. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3032. (org-forward-heading-same-level -1)
  3033. (buffer-substring-no-properties (point) (line-end-position)))))
  3034. ;; Stop at first or last sub-heading.
  3035. (should-not
  3036. (equal "* H2"
  3037. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3038. (org-forward-heading-same-level 1)
  3039. (buffer-substring-no-properties (point) (line-end-position)))))
  3040. (should-not
  3041. (equal "* H2"
  3042. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3043. (org-forward-heading-same-level -1)
  3044. (buffer-substring-no-properties (point) (line-end-position)))))
  3045. ;; Allow multiple moves.
  3046. (should
  3047. (equal "* H3"
  3048. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3049. (org-forward-heading-same-level 2)
  3050. (buffer-substring-no-properties (point) (line-end-position)))))
  3051. (should
  3052. (equal "* H1"
  3053. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3054. (org-forward-heading-same-level -2)
  3055. (buffer-substring-no-properties (point) (line-end-position)))))
  3056. ;; Ignore spurious moves when first (or last) sibling is reached.
  3057. (should
  3058. (equal "** H3"
  3059. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3060. (org-forward-heading-same-level 100)
  3061. (buffer-substring-no-properties (point) (line-end-position)))))
  3062. (should
  3063. (equal "** H1"
  3064. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3065. (org-forward-heading-same-level -100)
  3066. (buffer-substring-no-properties (point) (line-end-position))))))
  3067. (ert-deftest test-org/end-of-meta-data ()
  3068. "Test `org-end-of-meta-data' specifications."
  3069. ;; Skip planning line.
  3070. (should
  3071. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3072. (org-end-of-meta-data)
  3073. (eobp)))
  3074. ;; Skip properties drawer.
  3075. (should
  3076. (org-test-with-temp-text
  3077. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3078. (org-end-of-meta-data)
  3079. (eobp)))
  3080. ;; Skip both.
  3081. (should
  3082. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3083. (org-end-of-meta-data)
  3084. (eobp)))
  3085. ;; Nothing to skip, go to first line.
  3086. (should
  3087. (org-test-with-temp-text "* Headline\nContents"
  3088. (org-end-of-meta-data)
  3089. (looking-at "Contents")))
  3090. ;; With option argument, skip empty lines, regular drawers and
  3091. ;; clocking lines.
  3092. (should
  3093. (org-test-with-temp-text "* Headline\n\nContents"
  3094. (org-end-of-meta-data t)
  3095. (looking-at "Contents")))
  3096. (should
  3097. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3098. (org-end-of-meta-data t)
  3099. (looking-at "Contents")))
  3100. (should
  3101. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3102. (org-end-of-meta-data t)
  3103. (looking-at "Contents")))
  3104. ;; Special case: do not skip incomplete drawers.
  3105. (should
  3106. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3107. (org-end-of-meta-data t)
  3108. (looking-at ":LOGBOOK:")))
  3109. ;; Special case: Be careful about consecutive headlines.
  3110. (should-not
  3111. (org-test-with-temp-text "* H1\n*H2\nContents"
  3112. (org-end-of-meta-data t)
  3113. (looking-at "Contents"))))
  3114. (ert-deftest test-org/beginning-of-line ()
  3115. "Test `org-beginning-of-line' specifications."
  3116. ;; Move to beginning of line. If current line in invisible, move to
  3117. ;; beginning of visible line instead.
  3118. (should
  3119. (org-test-with-temp-text "Some text\nSome other text<point>"
  3120. (org-beginning-of-line)
  3121. (bolp)))
  3122. (should
  3123. (org-test-with-temp-text "* H1\n** H2<point>"
  3124. (org-overview)
  3125. (org-beginning-of-line)
  3126. (= (line-beginning-position) 1)))
  3127. ;; With `visual-line-mode' active, move to beginning of visual line.
  3128. (should-not
  3129. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3130. (visual-line-mode)
  3131. (dotimes (i 1000) (insert "very "))
  3132. (org-beginning-of-line)
  3133. (bolp)))
  3134. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3135. ;; beginning of a visual line than to the logical beginning of line,
  3136. ;; even if special movement is active.
  3137. (should-not
  3138. (org-test-with-temp-text "* A <point>long headline"
  3139. (visual-line-mode)
  3140. (dotimes (i 1000) (insert "very "))
  3141. (goto-char (point-max))
  3142. (org-beginning-of-line)
  3143. (bobp)))
  3144. (should-not
  3145. (org-test-with-temp-text "* A <point>long headline"
  3146. (visual-line-mode)
  3147. (dotimes (i 1000) (insert "very "))
  3148. (goto-char (point-max))
  3149. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3150. (bobp)))
  3151. ;; At an headline with special movement, first move at beginning of
  3152. ;; title, then at the beginning of line, rinse, repeat.
  3153. (should
  3154. (org-test-with-temp-text "* TODO Headline<point>"
  3155. (let ((org-special-ctrl-a/e t))
  3156. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3157. (progn (org-beginning-of-line) (bolp))
  3158. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3159. (should
  3160. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3161. (let ((org-special-ctrl-a/e t))
  3162. (org-beginning-of-line)
  3163. (looking-at "Headline"))))
  3164. (should
  3165. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3166. (let ((org-special-ctrl-a/e '(t . nil)))
  3167. (org-beginning-of-line)
  3168. (looking-at "Headline"))))
  3169. (should-not
  3170. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3171. (let ((org-special-ctrl-a/e '(nil . nil)))
  3172. (org-beginning-of-line)
  3173. (looking-at "Headline"))))
  3174. ;; At an headline with reversed movement, first move to beginning of
  3175. ;; line, then to the beginning of title.
  3176. (should
  3177. (org-test-with-temp-text "* TODO Headline<point>"
  3178. (let ((org-special-ctrl-a/e 'reversed)
  3179. (this-command last-command))
  3180. (and (progn (org-beginning-of-line) (bolp))
  3181. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3182. (should
  3183. (org-test-with-temp-text "* TODO Headline<point>"
  3184. (let ((org-special-ctrl-a/e '(reversed . nil))
  3185. (this-command last-command))
  3186. (and (progn (org-beginning-of-line) (bolp))
  3187. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3188. (should-not
  3189. (org-test-with-temp-text "* TODO Headline<point>"
  3190. (let ((org-special-ctrl-a/e '(t . nil))
  3191. (this-command last-command))
  3192. (and (progn (org-beginning-of-line) (bolp))
  3193. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3194. ;; At an item with special movement, first move after to beginning
  3195. ;; of title, then to the beginning of line, rinse, repeat.
  3196. (should
  3197. (org-test-with-temp-text "- [ ] Item<point>"
  3198. (let ((org-special-ctrl-a/e t))
  3199. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3200. (progn (org-beginning-of-line) (bolp))
  3201. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3202. ;; At an item with reversed movement, first move to beginning of
  3203. ;; line, then to the beginning of title.
  3204. (should
  3205. (org-test-with-temp-text "- [X] Item<point>"
  3206. (let ((org-special-ctrl-a/e 'reversed)
  3207. (this-command last-command))
  3208. (and (progn (org-beginning-of-line) (bolp))
  3209. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3210. ;; Leave point before invisible characters at column 0.
  3211. (should
  3212. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3213. (let ((org-special-ctrl-a/e nil))
  3214. (org-beginning-of-line)
  3215. (bolp))))
  3216. (should
  3217. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3218. (let ((org-special-ctrl-a/e t))
  3219. (org-beginning-of-line)
  3220. (bolp))))
  3221. (should
  3222. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3223. (visual-line-mode)
  3224. (org-beginning-of-line)
  3225. (bolp)))
  3226. ;; Special case: Do not error when the buffer contains only a single
  3227. ;; asterisk.
  3228. (should
  3229. (org-test-with-temp-text "*<point>"
  3230. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3231. (should
  3232. (org-test-with-temp-text "*<point>"
  3233. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3234. (ert-deftest test-org/end-of-line ()
  3235. "Test `org-end-of-line' specifications."
  3236. ;; Standard test.
  3237. (should
  3238. (org-test-with-temp-text "Some text\nSome other text"
  3239. (org-end-of-line)
  3240. (eolp)))
  3241. ;; With `visual-line-mode' active, move to end of visible line.
  3242. ;; However, never go past ellipsis.
  3243. (should-not
  3244. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3245. (visual-line-mode)
  3246. (dotimes (i 1000) (insert "very "))
  3247. (goto-char (point-min))
  3248. (org-end-of-line)
  3249. (eolp)))
  3250. (should-not
  3251. (org-test-with-temp-text "* A short headline\nSome contents"
  3252. (visual-line-mode)
  3253. (org-overview)
  3254. (org-end-of-line)
  3255. (eobp)))
  3256. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3257. ;; of visible line if tags, or end of line, are farther.
  3258. (should-not
  3259. (org-test-with-temp-text "* A <point>long headline"
  3260. (visual-line-mode)
  3261. (dotimes (i 1000) (insert "very "))
  3262. (goto-char (point-min))
  3263. (org-end-of-line)
  3264. (eolp)))
  3265. (should-not
  3266. (org-test-with-temp-text "* A <point>long headline :tag:"
  3267. (visual-line-mode)
  3268. (dotimes (i 1000) (insert "very "))
  3269. (goto-char (point-min))
  3270. (org-end-of-line)
  3271. (eolp)))
  3272. ;; At an headline without special movement, go to end of line.
  3273. ;; However, never go past ellipsis.
  3274. (should
  3275. (org-test-with-temp-text "* Headline2b :tag:\n"
  3276. (let ((org-special-ctrl-a/e nil))
  3277. (and (progn (org-end-of-line) (eolp))
  3278. (progn (org-end-of-line) (eolp))))))
  3279. (should
  3280. (org-test-with-temp-text "* Headline2b :tag:\n"
  3281. (let ((org-special-ctrl-a/e '(t . nil)))
  3282. (and (progn (org-end-of-line) (eolp))
  3283. (progn (org-end-of-line) (eolp))))))
  3284. (should
  3285. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3286. (org-overview)
  3287. (let ((org-special-ctrl-a/e nil))
  3288. (org-end-of-line)
  3289. (= 1 (line-beginning-position)))))
  3290. ;; At an headline with special movement, first move before tags,
  3291. ;; then at the end of line, rinse, repeat. However, never go past
  3292. ;; ellipsis.
  3293. (should
  3294. (org-test-with-temp-text "* Headline1 :tag:\n"
  3295. (let ((org-special-ctrl-a/e t))
  3296. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3297. (progn (org-end-of-line) (eolp))
  3298. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3299. (should
  3300. (org-test-with-temp-text "* Headline1 :tag:\n"
  3301. (let ((org-special-ctrl-a/e '(nil . t)))
  3302. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3303. (progn (org-end-of-line) (eolp))
  3304. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3305. (should-not
  3306. (org-test-with-temp-text "* Headline1 :tag:\n"
  3307. (let ((org-special-ctrl-a/e '(nil . nil)))
  3308. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3309. (progn (org-end-of-line) (eolp))
  3310. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3311. (should
  3312. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3313. (org-overview)
  3314. (let ((org-special-ctrl-a/e t))
  3315. (org-end-of-line)
  3316. (org-end-of-line)
  3317. (= 1 (line-beginning-position)))))
  3318. ;; At an headline, with reversed movement, first go to end of line,
  3319. ;; then before tags. However, never go past ellipsis.
  3320. (should
  3321. (org-test-with-temp-text "* Headline3 :tag:\n"
  3322. (let ((org-special-ctrl-a/e 'reversed)
  3323. (this-command last-command))
  3324. (and (progn (org-end-of-line) (eolp))
  3325. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3326. (should
  3327. (org-test-with-temp-text "* Headline3 :tag:\n"
  3328. (let ((org-special-ctrl-a/e '(nil . reversed))
  3329. (this-command last-command))
  3330. (and (progn (org-end-of-line) (eolp))
  3331. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3332. (should-not
  3333. (org-test-with-temp-text "* Headline3 :tag:\n"
  3334. (let ((org-special-ctrl-a/e '(nil . t))
  3335. (this-command last-command))
  3336. (and (progn (org-end-of-line) (eolp))
  3337. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3338. (should
  3339. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3340. (org-overview)
  3341. (let ((org-special-ctrl-a/e 'reversed))
  3342. (org-end-of-line)
  3343. (= 1 (line-beginning-position)))))
  3344. ;; At a block without hidden contents.
  3345. (should
  3346. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3347. (progn (org-end-of-line) (eolp))))
  3348. ;; At a block with hidden contents.
  3349. (should-not
  3350. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3351. (let ((org-special-ctrl-a/e t))
  3352. (org-hide-block-toggle)
  3353. (org-end-of-line)
  3354. (eobp))))
  3355. ;; Get past invisible characters at the end of line.
  3356. (should
  3357. (org-test-with-temp-text "[[https://orgmode.org]]"
  3358. (org-end-of-line)
  3359. (eolp))))
  3360. (ert-deftest test-org/open-line ()
  3361. "Test `org-open-line' specifications."
  3362. ;; Call `open-line' outside of tables.
  3363. (should
  3364. (equal "\nText"
  3365. (org-test-with-temp-text "Text"
  3366. (org-open-line 1)
  3367. (buffer-string))))
  3368. ;; At a table, create a row above.
  3369. (should
  3370. (equal "\n| |\n| a |"
  3371. (org-test-with-temp-text "\n<point>| a |"
  3372. (org-open-line 1)
  3373. (buffer-string))))
  3374. ;; At the very first character of the buffer, also call `open-line'.
  3375. (should
  3376. (equal "\n| a |"
  3377. (org-test-with-temp-text "| a |"
  3378. (org-open-line 1)
  3379. (buffer-string))))
  3380. ;; Narrowing does not count.
  3381. (should
  3382. (equal "Text\n| |\n| a |"
  3383. (org-test-with-temp-text "Text\n<point>| a |"
  3384. (narrow-to-region (point) (point-max))
  3385. (org-open-line 1)
  3386. (widen)
  3387. (buffer-string)))))
  3388. (ert-deftest test-org/forward-sentence ()
  3389. "Test `org-forward-sentence' specifications."
  3390. ;; At the end of a table cell, move to the end of the next one.
  3391. (should
  3392. (org-test-with-temp-text "| a<point> | b |"
  3393. (org-forward-sentence)
  3394. (looking-at " |$")))
  3395. ;; Elsewhere in a cell, move to its end.
  3396. (should
  3397. (org-test-with-temp-text "| a<point>c | b |"
  3398. (org-forward-sentence)
  3399. (looking-at " | b |$")))
  3400. ;; Otherwise, simply call `forward-sentence'.
  3401. (should
  3402. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3403. (org-forward-sentence)
  3404. (looking-at " Sentence 2.")))
  3405. (should
  3406. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3407. (org-forward-sentence)
  3408. (org-forward-sentence)
  3409. (eobp)))
  3410. ;; At the end of an element, jump to the next one, without stopping
  3411. ;; on blank lines in-between.
  3412. (should
  3413. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3414. (org-forward-sentence)
  3415. (eobp)))
  3416. ;; Headlines are considered to be sentences by themselves, even if
  3417. ;; they do not end with a full stop.
  3418. (should
  3419. (equal
  3420. "* Headline"
  3421. (org-test-with-temp-text "* <point>Headline\nSentence."
  3422. (org-forward-sentence)
  3423. (buffer-substring-no-properties (line-beginning-position) (point)))))
  3424. (should
  3425. (org-test-with-temp-text "* Headline<point>\nSentence."
  3426. (org-forward-sentence)
  3427. (eobp)))
  3428. (should
  3429. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  3430. (org-forward-sentence)
  3431. (and (org-at-heading-p) (eolp)))))
  3432. (ert-deftest test-org/backward-sentence ()
  3433. "Test `org-backward-sentence' specifications."
  3434. ;; At the beginning of a table cell, move to the beginning of the
  3435. ;; previous one.
  3436. (should
  3437. (org-test-with-temp-text "| a | <point>b |"
  3438. (org-backward-sentence)
  3439. (looking-at "a | b |$")))
  3440. ;; Elsewhere in a cell, move to its beginning.
  3441. (should
  3442. (org-test-with-temp-text "| a | b<point>c |"
  3443. (org-backward-sentence)
  3444. (looking-at "bc |$")))
  3445. ;; Otherwise, simply call `backward-sentence'.
  3446. (should
  3447. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3448. (org-backward-sentence)
  3449. (looking-at "Sentence 2.")))
  3450. (should
  3451. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3452. (org-backward-sentence)
  3453. (org-backward-sentence)
  3454. (bobp)))
  3455. ;; Make sure to hit the beginning of a sentence on the same line as
  3456. ;; an item.
  3457. (should
  3458. (org-test-with-temp-text "- Line 1\n line <point>2."
  3459. (org-backward-sentence)
  3460. (looking-at "Line 1"))))
  3461. (ert-deftest test-org/forward-paragraph ()
  3462. "Test `org-forward-paragraph' specifications."
  3463. ;; At end of buffer, do not return an error.
  3464. (should
  3465. (org-test-with-temp-text "Paragraph"
  3466. (goto-char (point-max))
  3467. (org-forward-paragraph)
  3468. t))
  3469. ;; Standard test.
  3470. (should
  3471. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3472. (org-forward-paragraph)
  3473. (looking-at "P2")))
  3474. ;; Ignore depth.
  3475. (should
  3476. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3477. (org-forward-paragraph)
  3478. (looking-at "P1")))
  3479. ;; Do not enter elements with invisible contents.
  3480. (should
  3481. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3482. (org-hide-block-toggle)
  3483. (org-forward-paragraph)
  3484. (looking-at "P3")))
  3485. ;; On an affiliated keyword, jump to the beginning of the element.
  3486. (should
  3487. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3488. (org-forward-paragraph)
  3489. (looking-at "Para")))
  3490. ;; On an item or a footnote definition, move to the second element
  3491. ;; inside, if any.
  3492. (should
  3493. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3494. (org-forward-paragraph)
  3495. (looking-at " Paragraph")))
  3496. (should
  3497. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3498. (org-forward-paragraph)
  3499. (looking-at "Paragraph")))
  3500. ;; On an item, or a footnote definition, when the first line is
  3501. ;; empty, move to the first item.
  3502. (should
  3503. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3504. (org-forward-paragraph)
  3505. (looking-at " Paragraph")))
  3506. (should
  3507. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3508. (org-forward-paragraph)
  3509. (looking-at "Paragraph")))
  3510. ;; On a table (resp. a property drawer) do not move through table
  3511. ;; rows (resp. node properties).
  3512. (should
  3513. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3514. (org-forward-paragraph)
  3515. (looking-at "Paragraph")))
  3516. (should
  3517. (org-test-with-temp-text
  3518. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3519. (org-forward-paragraph)
  3520. (looking-at "Paragraph")))
  3521. ;; On a verse or source block, stop after blank lines.
  3522. (should
  3523. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3524. (org-forward-paragraph)
  3525. (looking-at "L2")))
  3526. (should
  3527. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3528. (org-forward-paragraph)
  3529. (looking-at "L2"))))
  3530. (ert-deftest test-org/backward-paragraph ()
  3531. "Test `org-backward-paragraph' specifications."
  3532. ;; Do not error at beginning of buffer.
  3533. (should
  3534. (org-test-with-temp-text "Paragraph"
  3535. (org-backward-paragraph)
  3536. t))
  3537. ;; Regular test.
  3538. (should
  3539. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  3540. (org-backward-paragraph)
  3541. (looking-at "P3")))
  3542. (should
  3543. (org-test-with-temp-text "P1\n\nP2\n\n<point>P3"
  3544. (org-backward-paragraph)
  3545. (looking-at-p "P2")))
  3546. ;; Ignore depth.
  3547. (should
  3548. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\n<point>P3"
  3549. (org-backward-paragraph)
  3550. (looking-at-p "P2")))
  3551. ;; Ignore invisible elements.
  3552. (should
  3553. (org-test-with-temp-text "* H1\n P1\n* H2"
  3554. (org-cycle)
  3555. (goto-char (point-max))
  3556. (beginning-of-line)
  3557. (org-backward-paragraph)
  3558. (bobp)))
  3559. ;; On an affiliated keyword, jump to the first one.
  3560. (should
  3561. (org-test-with-temp-text
  3562. "P1\n#+name: n\n#+caption: c1\n#+caption: <point>c2\nP2"
  3563. (org-backward-paragraph)
  3564. (looking-at-p "#\\+name")))
  3565. ;; On the second element in an item or a footnote definition, jump
  3566. ;; to item or the definition.
  3567. (should
  3568. (org-test-with-temp-text "- line1\n\n<point> line2"
  3569. (org-backward-paragraph)
  3570. (looking-at-p "- line1")))
  3571. (should
  3572. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  3573. (org-backward-paragraph)
  3574. (looking-at-p "\\[fn:1\\] line1")))
  3575. ;; On a table (resp. a property drawer), ignore table rows
  3576. ;; (resp. node properties).
  3577. (should
  3578. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  3579. (org-backward-paragraph)
  3580. (bobp)))
  3581. (should
  3582. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3583. (org-backward-paragraph)
  3584. (looking-at-p ":PROPERTIES:")))
  3585. ;; On a comment, example, src and verse blocks, stop before blank
  3586. ;; lines.
  3587. (should
  3588. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\n<point>L3\n#+END_VERSE"
  3589. (org-backward-paragraph)
  3590. (looking-at-p "L2")))
  3591. (should
  3592. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\n<point>L3#+END_SRC"
  3593. (org-backward-paragraph)
  3594. (looking-at-p "L2")))
  3595. ;; In comment, example, export, src and verse blocks, stop below
  3596. ;; opening line when called from within the block.
  3597. (should
  3598. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\nL2<point>\n#+END_VERSE"
  3599. (org-backward-paragraph)
  3600. (looking-at-p "L1")))
  3601. (should
  3602. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nL1\nL2<point>\n#+END_EXAMPLE"
  3603. (org-backward-paragraph)
  3604. (looking-at-p "L1")))
  3605. ;; When called from the opening line itself, however, move to
  3606. ;; beginning of block.
  3607. (should
  3608. (org-test-with-temp-text "#+BEGIN_<point>EXAMPLE\nL1\n#+END_EXAMPLE"
  3609. (org-backward-paragraph)
  3610. (bobp)))
  3611. ;; Pathological case: on an empty heading, move to its beginning.
  3612. (should
  3613. (org-test-with-temp-text "* <point>H"
  3614. (org-backward-paragraph)
  3615. (bobp))))
  3616. (ert-deftest test-org/forward-element ()
  3617. "Test `org-forward-element' specifications."
  3618. ;; 1. At EOB: should error.
  3619. (org-test-with-temp-text "Some text\n"
  3620. (goto-char (point-max))
  3621. (should-error (org-forward-element)))
  3622. ;; 2. Standard move: expected to ignore blank lines.
  3623. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3624. (org-forward-element)
  3625. (should (looking-at (regexp-quote "Second paragraph."))))
  3626. ;; 3. Headline tests.
  3627. (org-test-with-temp-text "
  3628. * Head 1
  3629. ** Head 1.1
  3630. *** Head 1.1.1
  3631. ** Head 1.2"
  3632. ;; 3.1. At an headline beginning: move to next headline at the
  3633. ;; same level.
  3634. (goto-line 3)
  3635. (org-forward-element)
  3636. (should (looking-at (regexp-quote "** Head 1.2")))
  3637. ;; 3.2. At an headline beginning: move to parent headline if no
  3638. ;; headline at the same level.
  3639. (goto-line 3)
  3640. (org-forward-element)
  3641. (should (looking-at (regexp-quote "** Head 1.2"))))
  3642. ;; 4. Greater element tests.
  3643. (org-test-with-temp-text
  3644. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3645. ;; 4.1. At a greater element: expected to skip contents.
  3646. (org-forward-element)
  3647. (should (looking-at (regexp-quote "Outside.")))
  3648. ;; 4.2. At the end of greater element contents: expected to skip
  3649. ;; to the end of the greater element.
  3650. (goto-line 2)
  3651. (org-forward-element)
  3652. (should (looking-at (regexp-quote "Outside."))))
  3653. ;; 5. List tests.
  3654. (org-test-with-temp-text "
  3655. - item1
  3656. - sub1
  3657. - sub2
  3658. - sub3
  3659. Inner paragraph.
  3660. - item2
  3661. Outside."
  3662. ;; 5.1. At list top point: expected to move to the element after
  3663. ;; the list.
  3664. (goto-line 2)
  3665. (org-forward-element)
  3666. (should (looking-at (regexp-quote "Outside.")))
  3667. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3668. ;; beginning of line, move to next item.
  3669. (goto-line 2)
  3670. (forward-char)
  3671. (org-forward-element)
  3672. (should (looking-at "- item2"))
  3673. (goto-line 4)
  3674. (forward-char)
  3675. (org-forward-element)
  3676. (should (looking-at " - sub2"))
  3677. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3678. (goto-line 4)
  3679. (org-forward-element)
  3680. (should (looking-at (regexp-quote " Inner paragraph.")))
  3681. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3682. (goto-line 8)
  3683. (org-forward-element)
  3684. (should (looking-at (regexp-quote " Inner paragraph.")))
  3685. ;; 5.5. At an item: expected to move to next item, if any.
  3686. (goto-line 6)
  3687. (org-forward-element)
  3688. (should (looking-at " - sub3"))))
  3689. (ert-deftest test-org/backward-element ()
  3690. "Test `org-backward-element' specifications."
  3691. ;; 1. Should error at BOB.
  3692. (org-test-with-temp-text " \nParagraph."
  3693. (should-error (org-backward-element)))
  3694. ;; 2. Should move at BOB when called on the first element in buffer.
  3695. (should
  3696. (org-test-with-temp-text "\n#+TITLE: test"
  3697. (progn (forward-line)
  3698. (org-backward-element)
  3699. (bobp))))
  3700. ;; 3. Not at the beginning of an element: move at its beginning.
  3701. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3702. (goto-line 3)
  3703. (end-of-line)
  3704. (org-backward-element)
  3705. (should (looking-at (regexp-quote "Paragraph2."))))
  3706. ;; 4. Headline tests.
  3707. (org-test-with-temp-text "
  3708. * Head 1
  3709. ** Head 1.1
  3710. *** Head 1.1.1
  3711. ** Head 1.2"
  3712. ;; 4.1. At an headline beginning: move to previous headline at the
  3713. ;; same level.
  3714. (goto-line 5)
  3715. (org-backward-element)
  3716. (should (looking-at (regexp-quote "** Head 1.1")))
  3717. ;; 4.2. At an headline beginning: move to parent headline if no
  3718. ;; headline at the same level.
  3719. (goto-line 3)
  3720. (org-backward-element)
  3721. (should (looking-at (regexp-quote "* Head 1")))
  3722. ;; 4.3. At the first top-level headline: should error.
  3723. (goto-line 2)
  3724. (should-error (org-backward-element)))
  3725. ;; 5. At beginning of first element inside a greater element:
  3726. ;; expected to move to greater element's beginning.
  3727. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3728. (goto-line 3)
  3729. (org-backward-element)
  3730. (should (looking-at "#\\+BEGIN_CENTER")))
  3731. ;; 6. At the beginning of the first element in a section: should
  3732. ;; move back to headline, if any.
  3733. (should
  3734. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3735. (progn (goto-char (point-max))
  3736. (beginning-of-line)
  3737. (org-backward-element)
  3738. (org-at-heading-p))))
  3739. ;; 7. List tests.
  3740. (org-test-with-temp-text "
  3741. - item1
  3742. - sub1
  3743. - sub2
  3744. - sub3
  3745. Inner paragraph.
  3746. - item2
  3747. Outside."
  3748. ;; 7.1. At beginning of sub-list: expected to move to the
  3749. ;; paragraph before it.
  3750. (goto-line 4)
  3751. (org-backward-element)
  3752. (should (looking-at "item1"))
  3753. ;; 7.2. At an item in a list: expected to move at previous item.
  3754. (goto-line 8)
  3755. (org-backward-element)
  3756. (should (looking-at " - sub2"))
  3757. (goto-line 12)
  3758. (org-backward-element)
  3759. (should (looking-at "- item1"))
  3760. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3761. ;; beginning.
  3762. (goto-line 10)
  3763. (org-backward-element)
  3764. (should (looking-at " - sub1"))
  3765. (goto-line 15)
  3766. (org-backward-element)
  3767. (should (looking-at "- item1"))
  3768. ;; 7.4. At blank-lines before list end: expected to move to top
  3769. ;; item.
  3770. (goto-line 14)
  3771. (org-backward-element)
  3772. (should (looking-at "- item1"))))
  3773. (ert-deftest test-org/up-element ()
  3774. "Test `org-up-element' specifications."
  3775. ;; 1. At BOB or with no surrounding element: should error.
  3776. (org-test-with-temp-text "Paragraph."
  3777. (should-error (org-up-element)))
  3778. (org-test-with-temp-text "* Head1\n* Head2"
  3779. (goto-line 2)
  3780. (should-error (org-up-element)))
  3781. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3782. (goto-line 3)
  3783. (should-error (org-up-element)))
  3784. ;; 2. At an headline: move to parent headline.
  3785. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3786. (goto-line 3)
  3787. (org-up-element)
  3788. (should (looking-at "\\* Head1")))
  3789. ;; 3. Inside a greater element: move to greater element beginning.
  3790. (org-test-with-temp-text
  3791. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3792. (goto-line 3)
  3793. (org-up-element)
  3794. (should (looking-at "#\\+BEGIN_CENTER")))
  3795. ;; 4. List tests.
  3796. (org-test-with-temp-text "* Top
  3797. - item1
  3798. - sub1
  3799. - sub2
  3800. Paragraph within sub2.
  3801. - item2"
  3802. ;; 4.1. Within an item: move to the item beginning.
  3803. (goto-line 8)
  3804. (org-up-element)
  3805. (should (looking-at " - sub2"))
  3806. ;; 4.2. At an item in a sub-list: move to parent item.
  3807. (goto-line 4)
  3808. (org-up-element)
  3809. (should (looking-at "- item1"))
  3810. ;; 4.3. At an item in top list: move to beginning of whole list.
  3811. (goto-line 10)
  3812. (org-up-element)
  3813. (should (looking-at "- item1"))
  3814. ;; 4.4. Special case. At very top point: should move to parent of
  3815. ;; list.
  3816. (goto-line 2)
  3817. (org-up-element)
  3818. (should (looking-at "\\* Top"))))
  3819. (ert-deftest test-org/down-element ()
  3820. "Test `org-down-element' specifications."
  3821. ;; Error when the element hasn't got a recursive type.
  3822. (org-test-with-temp-text "Paragraph."
  3823. (should-error (org-down-element)))
  3824. ;; Error when the element has no contents
  3825. (org-test-with-temp-text "* Headline"
  3826. (should-error (org-down-element)))
  3827. ;; When at a plain-list, move to first item.
  3828. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3829. (goto-line 2)
  3830. (org-down-element)
  3831. (should (looking-at " - Item 1.1")))
  3832. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3833. (org-down-element)
  3834. (should (looking-at " Item 1")))
  3835. ;; When at a table, move to first row
  3836. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3837. (org-down-element)
  3838. (should (looking-at " a | b |")))
  3839. ;; Otherwise, move inside the greater element.
  3840. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3841. (org-down-element)
  3842. (should (looking-at "Paragraph"))))
  3843. (ert-deftest test-org/drag-element-backward ()
  3844. "Test `org-drag-element-backward' specifications."
  3845. ;; Standard test.
  3846. (should
  3847. (equal
  3848. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3849. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3850. (org-drag-element-backward)
  3851. (buffer-string))))
  3852. (should
  3853. (equal
  3854. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3855. (org-test-with-temp-text
  3856. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3857. (org-drag-element-backward)
  3858. (buffer-string))))
  3859. ;; Preserve blank lines.
  3860. (should
  3861. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3862. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3863. (org-drag-element-backward)
  3864. (buffer-string))))
  3865. ;; Preserve column.
  3866. (should
  3867. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3868. (org-drag-element-backward)
  3869. (looking-at-p "2")))
  3870. ;; Error when trying to move first element of buffer.
  3871. (should-error
  3872. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3873. (org-drag-element-backward))
  3874. :type 'user-error)
  3875. ;; Error when trying to swap nested elements.
  3876. (should-error
  3877. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3878. (org-drag-element-backward))
  3879. :type 'user-error)
  3880. ;; Error when trying to swap an headline element and a non-headline
  3881. ;; element.
  3882. (should-error
  3883. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3884. (org-drag-element-backward))
  3885. :type 'error)
  3886. ;; Error when called before first element.
  3887. (should-error
  3888. (org-test-with-temp-text "\n<point>"
  3889. (org-drag-element-backward))
  3890. :type 'user-error)
  3891. ;; Preserve visibility of elements and their contents.
  3892. (should
  3893. (equal '((63 . 82) (26 . 48))
  3894. (org-test-with-temp-text "
  3895. #+BEGIN_CENTER
  3896. Text.
  3897. #+END_CENTER
  3898. - item 1
  3899. #+BEGIN_QUOTE
  3900. Text.
  3901. #+END_QUOTE"
  3902. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3903. (search-backward "- item 1")
  3904. (org-drag-element-backward)
  3905. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3906. (overlays-in (point-min) (point-max))))))
  3907. ;; Pathological case: handle call with point in blank lines right
  3908. ;; after a headline.
  3909. (should
  3910. (equal "* H2\n\n* H1\nText\n"
  3911. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3912. (org-drag-element-backward)
  3913. (buffer-string)))))
  3914. (ert-deftest test-org/drag-element-forward ()
  3915. "Test `org-drag-element-forward' specifications."
  3916. ;; 1. Error when trying to move first element of buffer.
  3917. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3918. (goto-line 3)
  3919. (should-error (org-drag-element-forward)))
  3920. ;; 2. Error when trying to swap nested elements.
  3921. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3922. (forward-line)
  3923. (should-error (org-drag-element-forward)))
  3924. ;; 3. Error when trying to swap a non-headline element and an
  3925. ;; headline.
  3926. (org-test-with-temp-text "Test.\n* Head 1"
  3927. (should-error (org-drag-element-forward)))
  3928. ;; 4. Error when called before first element.
  3929. (should-error
  3930. (org-test-with-temp-text "\n"
  3931. (forward-line)
  3932. (org-drag-element-backward))
  3933. :type 'user-error)
  3934. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3935. ;; between elements.
  3936. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3937. (search-forward "graph")
  3938. (org-drag-element-forward)
  3939. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3940. (should (looking-at " 1")))
  3941. ;; 5. Preserve visibility of elements and their contents.
  3942. (org-test-with-temp-text "
  3943. #+BEGIN_CENTER
  3944. Text.
  3945. #+END_CENTER
  3946. - item 1
  3947. #+BEGIN_QUOTE
  3948. Text.
  3949. #+END_QUOTE"
  3950. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3951. (search-backward "#+BEGIN_CENTER")
  3952. (org-drag-element-forward)
  3953. (should
  3954. (equal
  3955. '((63 . 82) (26 . 48))
  3956. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3957. (overlays-in (point-min) (point-max)))))))
  3958. (ert-deftest test-org/next-block ()
  3959. "Test `org-next-block' specifications."
  3960. ;; Regular test.
  3961. (should
  3962. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3963. (org-next-block 1)
  3964. (looking-at "#\\+BEGIN_CENTER")))
  3965. ;; Ignore case.
  3966. (should
  3967. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3968. (let ((case-fold-search nil))
  3969. (org-next-block 1)
  3970. (looking-at "#\\+begin_center"))))
  3971. ;; Ignore current line.
  3972. (should
  3973. (org-test-with-temp-text
  3974. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3975. (org-next-block 1)
  3976. (looking-at "#\\+BEGIN_CENTER")))
  3977. ;; Throw an error when no block is found.
  3978. (should-error
  3979. (org-test-with-temp-text "Paragraph"
  3980. (org-next-block 1)))
  3981. ;; With an argument, skip many blocks at once.
  3982. (should
  3983. (org-test-with-temp-text
  3984. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3985. (org-next-block 2)
  3986. (looking-at "#\\+BEGIN_QUOTE")))
  3987. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3988. (should
  3989. (org-test-with-temp-text
  3990. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3991. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3992. (looking-at "#\\+BEGIN_QUOTE")))
  3993. ;; Optional argument is also case-insensitive.
  3994. (should
  3995. (org-test-with-temp-text
  3996. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3997. (let ((case-fold-search nil))
  3998. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3999. (looking-at "#\\+begin_quote")))))
  4000. (ert-deftest test-org/insert-structure-template ()
  4001. "Test `org-insert-structure-template'."
  4002. ;; Test in empty buffer.
  4003. (should
  4004. (string= "#+begin_foo\n#+end_foo\n"
  4005. (org-test-with-temp-text ""
  4006. (org-insert-structure-template "foo")
  4007. (buffer-string))))
  4008. ;; Test with multiple lines in buffer.
  4009. (should
  4010. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4011. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4012. (transient-mark-mode 1)
  4013. (org-mark-element)
  4014. (org-insert-structure-template "foo")
  4015. (buffer-string))))
  4016. ;; Mark only the current line.
  4017. (should
  4018. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4019. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4020. (transient-mark-mode 1)
  4021. (set-mark (point-min))
  4022. (end-of-line)
  4023. (org-insert-structure-template "foo")
  4024. (buffer-string))))
  4025. ;; Middle of paragraph.
  4026. (should
  4027. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4028. (org-test-with-temp-text "p1\n<point>p2\np3"
  4029. (set-mark (line-beginning-position))
  4030. (end-of-line)
  4031. (activate-mark)
  4032. (org-insert-structure-template "foo")
  4033. (buffer-string))))
  4034. ;; Test with text in buffer, no region, no final newline.
  4035. (should
  4036. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4037. (org-test-with-temp-text "I'm a paragraph."
  4038. (org-mark-element)
  4039. (org-insert-structure-template "foo")
  4040. (buffer-string))))
  4041. ;; Test with text in buffer and region set.
  4042. (should
  4043. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4044. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4045. (set-mark (point))
  4046. (goto-char (point-max))
  4047. (org-insert-structure-template "foo")
  4048. (buffer-string))))
  4049. ;; Test with example escaping.
  4050. (should
  4051. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4052. (org-test-with-temp-text "* Heading"
  4053. (org-mark-element)
  4054. (org-insert-structure-template "example")
  4055. (buffer-string))))
  4056. ;; Test with indentation.
  4057. (should
  4058. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4059. (org-test-with-temp-text " This is a paragraph"
  4060. (org-mark-element)
  4061. (org-insert-structure-template "foo")
  4062. (buffer-string))))
  4063. (should
  4064. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4065. (org-test-with-temp-text " Line 1\n Line2"
  4066. (org-mark-element)
  4067. (org-insert-structure-template "foo")
  4068. (buffer-string))))
  4069. ;; Test point location.
  4070. (should
  4071. (string= "#+begin_foo\n"
  4072. (org-test-with-temp-text ""
  4073. (org-insert-structure-template "foo")
  4074. (buffer-substring (point-min) (point)))))
  4075. (should
  4076. (string= "#+begin_src "
  4077. (org-test-with-temp-text ""
  4078. (org-insert-structure-template "src")
  4079. (buffer-substring (point-min) (point))))))
  4080. (ert-deftest test-org/previous-block ()
  4081. "Test `org-previous-block' specifications."
  4082. ;; Regular test.
  4083. (should
  4084. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4085. (org-previous-block 1)
  4086. (looking-at "#\\+BEGIN_CENTER")))
  4087. ;; Ignore case.
  4088. (should
  4089. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4090. (let ((case-fold-search nil))
  4091. (org-previous-block 1)
  4092. (looking-at "#\\+begin_center"))))
  4093. ;; Ignore current line.
  4094. (should
  4095. (org-test-with-temp-text
  4096. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4097. (org-previous-block 1)
  4098. (looking-at "#\\+BEGIN_QUOTE")))
  4099. ;; Throw an error when no block is found.
  4100. (should-error
  4101. (org-test-with-temp-text "Paragraph<point>"
  4102. (org-previous-block 1)))
  4103. ;; With an argument, skip many blocks at once.
  4104. (should
  4105. (org-test-with-temp-text
  4106. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4107. (org-previous-block 2)
  4108. (looking-at "#\\+BEGIN_CENTER")))
  4109. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4110. (should
  4111. (org-test-with-temp-text
  4112. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4113. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4114. (looking-at "#\\+BEGIN_QUOTE")))
  4115. ;; Optional argument is also case-insensitive.
  4116. (should
  4117. (org-test-with-temp-text
  4118. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4119. (let ((case-fold-search nil))
  4120. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4121. (looking-at "#\\+begin_quote")))))
  4122. ;;; Outline structure
  4123. (ert-deftest test-org/demote ()
  4124. "Test `org-demote' specifications."
  4125. ;; Add correct number of stars according to `org-odd-levels-only'.
  4126. (should
  4127. (= 2
  4128. (org-test-with-temp-text "* H"
  4129. (let ((org-odd-levels-only nil)) (org-demote))
  4130. (org-current-level))))
  4131. (should
  4132. (= 3
  4133. (org-test-with-temp-text "* H"
  4134. (let ((org-odd-levels-only t)) (org-demote))
  4135. (org-current-level))))
  4136. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4137. (should
  4138. (org-test-with-temp-text "* H :tag:"
  4139. (let ((org-tags-column 10)
  4140. (org-auto-align-tags t)
  4141. (org-odd-levels-only nil))
  4142. (org-demote))
  4143. (org-move-to-column 10)
  4144. (looking-at-p ":tag:$")))
  4145. (should-not
  4146. (org-test-with-temp-text "* H :tag:"
  4147. (let ((org-tags-column 10)
  4148. (org-auto-align-tags nil)
  4149. (org-odd-levels-only nil))
  4150. (org-demote))
  4151. (org-move-to-column 10)
  4152. (looking-at-p ":tag:$")))
  4153. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4154. ;; info and property drawers accordingly.
  4155. (should
  4156. (= 3
  4157. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4158. (let ((org-odd-levels-only nil)
  4159. (org-adapt-indentation t))
  4160. (org-demote))
  4161. (forward-line)
  4162. (org-get-indentation))))
  4163. (should
  4164. (= 3
  4165. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4166. (let ((org-odd-levels-only nil)
  4167. (org-adapt-indentation t))
  4168. (org-demote))
  4169. (forward-line)
  4170. (org-get-indentation))))
  4171. (should-not
  4172. (= 3
  4173. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4174. (let ((org-odd-levels-only nil)
  4175. (org-adapt-indentation nil))
  4176. (org-demote))
  4177. (forward-line)
  4178. (org-get-indentation))))
  4179. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4180. ;; section accordingly. Ignore, however, footnote definitions and
  4181. ;; inlinetasks boundaries.
  4182. (should
  4183. (= 3
  4184. (org-test-with-temp-text "* H\n Paragraph"
  4185. (let ((org-odd-levels-only nil)
  4186. (org-adapt-indentation t))
  4187. (org-demote))
  4188. (forward-line)
  4189. (org-get-indentation))))
  4190. (should
  4191. (= 2
  4192. (org-test-with-temp-text "* H\n Paragraph"
  4193. (let ((org-odd-levels-only nil)
  4194. (org-adapt-indentation nil))
  4195. (org-demote))
  4196. (forward-line)
  4197. (org-get-indentation))))
  4198. (should
  4199. (zerop
  4200. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4201. (let ((org-odd-levels-only nil)
  4202. (org-adapt-indentation t))
  4203. (org-demote))
  4204. (goto-char (point-max))
  4205. (org-get-indentation))))
  4206. (should
  4207. (= 3
  4208. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4209. (let ((org-odd-levels-only nil)
  4210. (org-adapt-indentation t))
  4211. (org-demote))
  4212. (goto-char (point-max))
  4213. (org-get-indentation))))
  4214. (when (featurep 'org-inlinetask)
  4215. (should
  4216. (zerop
  4217. (let ((org-inlinetask-min-level 5)
  4218. (org-adapt-indentation t))
  4219. (org-test-with-temp-text "* H\n***** I\n***** END"
  4220. (org-demote)
  4221. (forward-line)
  4222. (org-get-indentation))))))
  4223. (when (featurep 'org-inlinetask)
  4224. (should
  4225. (= 3
  4226. (let ((org-inlinetask-min-level 5)
  4227. (org-adapt-indentation t))
  4228. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4229. (org-demote)
  4230. (forward-line 2)
  4231. (org-get-indentation))))))
  4232. ;; Ignore contents of source blocks or example blocks when
  4233. ;; indentation should be preserved (through
  4234. ;; `org-src-preserve-indentation' or "-i" flag).
  4235. (should-not
  4236. (zerop
  4237. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4238. (let ((org-adapt-indentation t)
  4239. (org-src-preserve-indentation nil))
  4240. (org-demote))
  4241. (forward-line 2)
  4242. (org-get-indentation))))
  4243. (should
  4244. (zerop
  4245. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4246. (let ((org-adapt-indentation t)
  4247. (org-src-preserve-indentation t))
  4248. (org-demote))
  4249. (forward-line 2)
  4250. (org-get-indentation))))
  4251. (should
  4252. (zerop
  4253. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4254. (let ((org-adapt-indentation t)
  4255. (org-src-preserve-indentation t))
  4256. (org-demote))
  4257. (forward-line 2)
  4258. (org-get-indentation))))
  4259. (should
  4260. (zerop
  4261. (org-test-with-temp-text
  4262. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  4263. (let ((org-adapt-indentation t)
  4264. (org-src-preserve-indentation nil))
  4265. (org-demote))
  4266. (forward-line 2)
  4267. (org-get-indentation)))))
  4268. (ert-deftest test-org/promote ()
  4269. "Test `org-promote' specifications."
  4270. ;; Return an error if headline is to be promoted to level 0, unless
  4271. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  4272. ;; headline becomes a comment.
  4273. (should-error
  4274. (org-test-with-temp-text "* H"
  4275. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  4276. (should
  4277. (equal "# H"
  4278. (org-test-with-temp-text "* H"
  4279. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  4280. (buffer-string))))
  4281. ;; Remove correct number of stars according to
  4282. ;; `org-odd-levels-only'.
  4283. (should
  4284. (= 2
  4285. (org-test-with-temp-text "*** H"
  4286. (let ((org-odd-levels-only nil)) (org-promote))
  4287. (org-current-level))))
  4288. (should
  4289. (= 1
  4290. (org-test-with-temp-text "*** H"
  4291. (let ((org-odd-levels-only t)) (org-promote))
  4292. (org-current-level))))
  4293. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4294. (should
  4295. (org-test-with-temp-text "** H :tag:"
  4296. (let ((org-tags-column 10)
  4297. (org-auto-align-tags t)
  4298. (org-odd-levels-only nil))
  4299. (org-promote))
  4300. (org-move-to-column 10)
  4301. (looking-at-p ":tag:$")))
  4302. (should-not
  4303. (org-test-with-temp-text "** H :tag:"
  4304. (let ((org-tags-column 10)
  4305. (org-auto-align-tags nil)
  4306. (org-odd-levels-only nil))
  4307. (org-promote))
  4308. (org-move-to-column 10)
  4309. (looking-at-p ":tag:$")))
  4310. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4311. ;; info and property drawers.
  4312. (should
  4313. (= 2
  4314. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4315. (let ((org-odd-levels-only nil)
  4316. (org-adapt-indentation t))
  4317. (org-promote))
  4318. (forward-line)
  4319. (org-get-indentation))))
  4320. (should
  4321. (= 2
  4322. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4323. (let ((org-odd-levels-only nil)
  4324. (org-adapt-indentation t))
  4325. (org-promote))
  4326. (forward-line)
  4327. (org-get-indentation))))
  4328. (should-not
  4329. (= 2
  4330. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  4331. (let ((org-odd-levels-only nil)
  4332. (org-adapt-indentation nil))
  4333. (org-promote))
  4334. (forward-line)
  4335. (org-get-indentation))))
  4336. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4337. ;; section accordingly. Ignore, however, footnote definitions and
  4338. ;; inlinetasks boundaries.
  4339. (should
  4340. (= 2
  4341. (org-test-with-temp-text "** H\n Paragraph"
  4342. (let ((org-odd-levels-only nil)
  4343. (org-adapt-indentation t))
  4344. (org-promote))
  4345. (forward-line)
  4346. (org-get-indentation))))
  4347. (should-not
  4348. (= 2
  4349. (org-test-with-temp-text "** H\n Paragraph"
  4350. (let ((org-odd-levels-only nil)
  4351. (org-adapt-indentation nil))
  4352. (org-promote))
  4353. (forward-line)
  4354. (org-get-indentation))))
  4355. (should
  4356. (= 2
  4357. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  4358. (let ((org-odd-levels-only nil)
  4359. (org-adapt-indentation t))
  4360. (org-promote))
  4361. (forward-line)
  4362. (org-get-indentation))))
  4363. (when (featurep 'org-inlinetask)
  4364. (should
  4365. (zerop
  4366. (let ((org-inlinetask-min-level 5)
  4367. (org-adapt-indentation t))
  4368. (org-test-with-temp-text "** H\n***** I\n***** END"
  4369. (org-promote)
  4370. (forward-line)
  4371. (org-get-indentation))))))
  4372. (when (featurep 'org-inlinetask)
  4373. (should
  4374. (= 2
  4375. (let ((org-inlinetask-min-level 5)
  4376. (org-adapt-indentation t))
  4377. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  4378. (org-promote)
  4379. (forward-line 2)
  4380. (org-get-indentation))))))
  4381. ;; Give up shifting if it would break document's structure
  4382. ;; otherwise.
  4383. (should
  4384. (= 3
  4385. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  4386. (let ((org-odd-levels-only nil)
  4387. (org-adapt-indentation t))
  4388. (org-promote))
  4389. (forward-line)
  4390. (org-get-indentation))))
  4391. (should
  4392. (= 3
  4393. (org-test-with-temp-text "** H\n Paragraph\n * list."
  4394. (let ((org-odd-levels-only nil)
  4395. (org-adapt-indentation t))
  4396. (org-promote))
  4397. (forward-line)
  4398. (org-get-indentation))))
  4399. ;; Ignore contents of source blocks or example blocks when
  4400. ;; indentation should be preserved (through
  4401. ;; `org-src-preserve-indentation' or "-i" flag).
  4402. (should-not
  4403. (zerop
  4404. (org-test-with-temp-text
  4405. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4406. (let ((org-adapt-indentation t)
  4407. (org-src-preserve-indentation nil)
  4408. (org-odd-levels-only nil))
  4409. (org-promote))
  4410. (forward-line)
  4411. (org-get-indentation))))
  4412. (should
  4413. (zerop
  4414. (org-test-with-temp-text
  4415. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4416. (let ((org-adapt-indentation t)
  4417. (org-src-preserve-indentation t)
  4418. (org-odd-levels-only nil))
  4419. (org-promote))
  4420. (forward-line)
  4421. (org-get-indentation))))
  4422. (should
  4423. (zerop
  4424. (org-test-with-temp-text
  4425. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4426. (let ((org-adapt-indentation t)
  4427. (org-src-preserve-indentation t)
  4428. (org-odd-levels-only nil))
  4429. (org-promote))
  4430. (forward-line)
  4431. (org-get-indentation))))
  4432. (should
  4433. (zerop
  4434. (org-test-with-temp-text
  4435. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4436. (let ((org-adapt-indentation t)
  4437. (org-src-preserve-indentation nil)
  4438. (org-odd-levels-only nil))
  4439. (org-promote))
  4440. (forward-line)
  4441. (org-get-indentation)))))
  4442. (ert-deftest test-org/org-get-valid-level ()
  4443. "Test function `org-get-valid-level' specifications."
  4444. (let ((org-odd-levels-only nil))
  4445. (should (equal 1 (org-get-valid-level 0 0)))
  4446. (should (equal 1 (org-get-valid-level 0 1)))
  4447. (should (equal 2 (org-get-valid-level 0 2)))
  4448. (should (equal 3 (org-get-valid-level 0 3)))
  4449. (should (equal 1 (org-get-valid-level 1 0)))
  4450. (should (equal 2 (org-get-valid-level 1 1)))
  4451. (should (equal 23 (org-get-valid-level 1 22)))
  4452. (should (equal 1 (org-get-valid-level 1 -1)))
  4453. (should (equal 1 (org-get-valid-level 2 -1))))
  4454. (let ((org-odd-levels-only t))
  4455. (should (equal 1 (org-get-valid-level 0 0)))
  4456. (should (equal 1 (org-get-valid-level 0 1)))
  4457. (should (equal 3 (org-get-valid-level 0 2)))
  4458. (should (equal 5 (org-get-valid-level 0 3)))
  4459. (should (equal 1 (org-get-valid-level 1 0)))
  4460. (should (equal 3 (org-get-valid-level 1 1)))
  4461. (should (equal 3 (org-get-valid-level 2 1)))
  4462. (should (equal 5 (org-get-valid-level 3 1)))
  4463. (should (equal 5 (org-get-valid-level 4 1)))
  4464. (should (equal 43 (org-get-valid-level 1 21)))
  4465. (should (equal 1 (org-get-valid-level 1 -1)))
  4466. (should (equal 1 (org-get-valid-level 2 -1)))
  4467. (should (equal 1 (org-get-valid-level 3 -1)))
  4468. (should (equal 3 (org-get-valid-level 4 -1)))
  4469. (should (equal 3 (org-get-valid-level 5 -1)))))
  4470. ;;; Planning
  4471. (ert-deftest test-org/at-planning-p ()
  4472. "Test `org-at-planning-p' specifications."
  4473. ;; Regular test.
  4474. (should
  4475. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4476. (org-at-planning-p)))
  4477. (should-not
  4478. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4479. (org-at-planning-p)))
  4480. ;; Correctly find planning attached to inlinetasks.
  4481. (when (featurep 'org-inlinetask)
  4482. (should
  4483. (org-test-with-temp-text
  4484. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4485. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4486. (should-not
  4487. (org-test-with-temp-text
  4488. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4489. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4490. (should-not
  4491. (org-test-with-temp-text
  4492. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4493. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4494. (should-not
  4495. (org-test-with-temp-text
  4496. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4497. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4498. (ert-deftest test-org/add-planning-info ()
  4499. "Test `org-add-planning-info'."
  4500. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4501. (should
  4502. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4503. (org-test-with-temp-text "* H\nParagraph<point>"
  4504. (let ((org-adapt-indentation t))
  4505. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4506. (replace-regexp-in-string
  4507. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4508. nil nil 1))))
  4509. ;; Create deadline when `org-adapt-indentation' is nil.
  4510. (should
  4511. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4512. (org-test-with-temp-text "* H\nParagraph<point>"
  4513. (let ((org-adapt-indentation nil))
  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. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4519. (should
  4520. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4521. (org-test-with-temp-text "\
  4522. * H
  4523. DEADLINE: <2015-06-24 Wed>
  4524. Paragraph<point>"
  4525. (let ((org-adapt-indentation t))
  4526. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4527. (replace-regexp-in-string
  4528. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4529. nil nil 1))))
  4530. ;; Update deadline when `org-adapt-indentation' is nil.
  4531. (should
  4532. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4533. (org-test-with-temp-text "\
  4534. * H
  4535. DEADLINE: <2015-06-24 Wed>
  4536. Paragraph<point>"
  4537. (let ((org-adapt-indentation nil))
  4538. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4539. (replace-regexp-in-string
  4540. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4541. nil nil 1))))
  4542. ;; Schedule when `org-adapt-indentation' is non-nil.
  4543. (should
  4544. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4545. (org-test-with-temp-text "* H\nParagraph<point>"
  4546. (let ((org-adapt-indentation t))
  4547. (org-add-planning-info 'scheduled "<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 nil.
  4552. (should
  4553. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4554. (org-test-with-temp-text "* H\nParagraph<point>"
  4555. (let ((org-adapt-indentation nil))
  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. ;; Add deadline when scheduled.
  4561. (should
  4562. (equal "\
  4563. * H
  4564. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4565. Paragraph"
  4566. (org-test-with-temp-text "\
  4567. * H
  4568. SCHEDULED: <2015-06-24 Wed>
  4569. Paragraph<point>"
  4570. (let ((org-adapt-indentation t))
  4571. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4572. (replace-regexp-in-string
  4573. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4574. nil nil 1))))
  4575. ;; Remove middle entry.
  4576. (should
  4577. (equal "\
  4578. * H
  4579. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4580. Paragraph"
  4581. (org-test-with-temp-text "\
  4582. * H
  4583. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4584. Paragraph<point>"
  4585. (let ((org-adapt-indentation t))
  4586. (org-add-planning-info nil nil 'deadline))
  4587. (replace-regexp-in-string
  4588. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4589. nil nil 1))))
  4590. ;; Remove last entry and then middle entry (order should not
  4591. ;; matter).
  4592. (should
  4593. (equal "\
  4594. * H
  4595. CLOSED: [2015-06-24]
  4596. Paragraph"
  4597. (org-test-with-temp-text "\
  4598. * H
  4599. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4600. Paragraph<point>"
  4601. (let ((org-adapt-indentation t))
  4602. (org-add-planning-info nil nil 'scheduled 'deadline))
  4603. (replace-regexp-in-string
  4604. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4605. nil nil 1))))
  4606. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4607. (should
  4608. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4609. (org-test-with-temp-text "\
  4610. * H
  4611. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4612. Paragraph<point>"
  4613. (let ((org-adapt-indentation t))
  4614. (org-add-planning-info nil nil 'closed))
  4615. (replace-regexp-in-string
  4616. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4617. nil nil 1))))
  4618. (should
  4619. (equal "* H\n Paragraph"
  4620. (org-test-with-temp-text "\
  4621. * H
  4622. CLOSED: [2015-06-25 Thu]
  4623. Paragraph<point>"
  4624. (let ((org-adapt-indentation t))
  4625. (org-add-planning-info nil nil 'closed))
  4626. (replace-regexp-in-string
  4627. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4628. nil nil 1))))
  4629. ;; Remove closed when `org-adapt-indentation' is nil.
  4630. (should
  4631. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4632. (org-test-with-temp-text "\
  4633. * H
  4634. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4635. Paragraph<point>"
  4636. (let ((org-adapt-indentation nil))
  4637. (org-add-planning-info nil nil 'closed))
  4638. (replace-regexp-in-string
  4639. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4640. nil nil 1))))
  4641. (should
  4642. (equal "* H\nParagraph"
  4643. (org-test-with-temp-text "\
  4644. * H
  4645. CLOSED: [2015-06-25 Thu]
  4646. Paragraph<point>"
  4647. (let ((org-adapt-indentation nil))
  4648. (org-add-planning-info nil nil 'closed))
  4649. (replace-regexp-in-string
  4650. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4651. nil nil 1))))
  4652. ;; Remove closed entry and delete empty line.
  4653. (should
  4654. (equal "\
  4655. * H
  4656. Paragraph"
  4657. (org-test-with-temp-text "\
  4658. * H
  4659. CLOSED: [2015-06-24 Wed]
  4660. Paragraph<point>"
  4661. (let ((org-adapt-indentation t))
  4662. (org-add-planning-info nil nil 'closed))
  4663. (replace-regexp-in-string
  4664. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4665. nil nil 1))))
  4666. ;; Remove one entry and update another.
  4667. (should
  4668. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4669. (org-test-with-temp-text "\
  4670. * H
  4671. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4672. Paragraph<point>"
  4673. (let ((org-adapt-indentation t))
  4674. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4675. (replace-regexp-in-string
  4676. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4677. nil nil 1)))))
  4678. (ert-deftest test-org/deadline ()
  4679. "Test `org-deadline' specifications."
  4680. ;; Insert a new value or replace existing one.
  4681. (should
  4682. (equal "* H\nDEADLINE: <2012-03-29>"
  4683. (org-test-with-temp-text "* H"
  4684. (let ((org-adapt-indentation nil)
  4685. (org-last-inserted-timestamp nil))
  4686. (org-deadline nil "<2012-03-29 Tue>"))
  4687. (replace-regexp-in-string
  4688. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4689. nil nil 1))))
  4690. (should
  4691. (equal "* H\nDEADLINE: <2014-03-04>"
  4692. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4693. (let ((org-adapt-indentation nil)
  4694. (org-last-inserted-timestamp nil))
  4695. (org-deadline nil "<2014-03-04 Thu>"))
  4696. (replace-regexp-in-string
  4697. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4698. nil nil 1))))
  4699. ;; Accept delta time, e.g., "+2d".
  4700. (should
  4701. (equal "* H\nDEADLINE: <2015-03-04>"
  4702. (org-test-at-time "2014-03-04"
  4703. (org-test-with-temp-text "* H"
  4704. (let ((org-adapt-indentation nil)
  4705. (org-last-inserted-timestamp nil))
  4706. (org-deadline nil "+1y"))
  4707. (replace-regexp-in-string
  4708. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4709. ;; Preserve repeater.
  4710. (should
  4711. (equal "* H\nDEADLINE: <2012-03-29 +2y>"
  4712. (org-test-with-temp-text "* H"
  4713. (let ((org-adapt-indentation nil)
  4714. (org-last-inserted-timestamp nil))
  4715. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4716. (replace-regexp-in-string
  4717. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4718. ;; Remove CLOSED keyword, if any.
  4719. (should
  4720. (equal "* H\nDEADLINE: <2012-03-29>"
  4721. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4722. (let ((org-adapt-indentation nil)
  4723. (org-last-inserted-timestamp nil))
  4724. (org-deadline nil "<2012-03-29 Tue>"))
  4725. (replace-regexp-in-string
  4726. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4727. ;; With C-u argument, remove DEADLINE keyword.
  4728. (should
  4729. (equal "* H\n"
  4730. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4731. (let ((org-adapt-indentation nil)
  4732. (org-last-inserted-timestamp nil))
  4733. (org-deadline '(4)))
  4734. (buffer-string))))
  4735. (should
  4736. (equal "* H"
  4737. (org-test-with-temp-text "* H"
  4738. (let ((org-adapt-indentation nil)
  4739. (org-last-inserted-timestamp nil))
  4740. (org-deadline '(4)))
  4741. (buffer-string))))
  4742. ;; With C-u C-u argument, prompt for a delay cookie.
  4743. (should
  4744. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4745. (cl-letf (((symbol-function 'org-read-date)
  4746. (lambda (&rest args)
  4747. (apply #'encode-time
  4748. (org-parse-time-string "2014-03-04")))))
  4749. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4750. (let ((org-adapt-indentation nil)
  4751. (org-last-inserted-timestamp nil))
  4752. (org-deadline '(16)))
  4753. (buffer-string)))))
  4754. (should-error
  4755. (cl-letf (((symbol-function 'org-read-date)
  4756. (lambda (&rest args)
  4757. (apply #'encode-time
  4758. (org-parse-time-string "2014-03-04")))))
  4759. (org-test-with-temp-text "* H"
  4760. (let ((org-adapt-indentation nil)
  4761. (org-last-inserted-timestamp nil))
  4762. (org-deadline '(16)))
  4763. (buffer-string))))
  4764. ;; When a region is active and
  4765. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4766. ;; same value in all headlines in region.
  4767. (should
  4768. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  4769. (org-test-with-temp-text "* H1\n* H2"
  4770. (let ((org-adapt-indentation nil)
  4771. (org-last-inserted-timestamp nil)
  4772. (org-loop-over-headlines-in-active-region t))
  4773. (transient-mark-mode 1)
  4774. (push-mark (point) t t)
  4775. (goto-char (point-max))
  4776. (org-deadline nil "2012-03-29"))
  4777. (replace-regexp-in-string
  4778. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4779. (should-not
  4780. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  4781. (org-test-with-temp-text "* H1\n* H2"
  4782. (let ((org-adapt-indentation nil)
  4783. (org-last-inserted-timestamp nil)
  4784. (org-loop-over-headlines-in-active-region nil))
  4785. (transient-mark-mode 1)
  4786. (push-mark (point) t t)
  4787. (goto-char (point-max))
  4788. (org-deadline nil "2012-03-29"))
  4789. (replace-regexp-in-string
  4790. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4791. (ert-deftest test-org/schedule ()
  4792. "Test `org-schedule' specifications."
  4793. ;; Insert a new value or replace existing one.
  4794. (should
  4795. (equal "* H\nSCHEDULED: <2012-03-29>"
  4796. (org-test-with-temp-text "* H"
  4797. (let ((org-adapt-indentation nil)
  4798. (org-last-inserted-timestamp nil))
  4799. (org-schedule nil "<2012-03-29 Tue>"))
  4800. (replace-regexp-in-string
  4801. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4802. nil nil 1))))
  4803. (should
  4804. (equal "* H\nSCHEDULED: <2014-03-04>"
  4805. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4806. (let ((org-adapt-indentation nil)
  4807. (org-last-inserted-timestamp nil))
  4808. (org-schedule nil "<2014-03-04 Thu>"))
  4809. (replace-regexp-in-string
  4810. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4811. nil nil 1))))
  4812. ;; Accept delta time, e.g., "+2d".
  4813. (should
  4814. (equal "* H\nSCHEDULED: <2015-03-04>"
  4815. (org-test-at-time "2014-03-04"
  4816. (org-test-with-temp-text "* H"
  4817. (let ((org-adapt-indentation nil)
  4818. (org-last-inserted-timestamp nil))
  4819. (org-schedule nil "+1y"))
  4820. (replace-regexp-in-string
  4821. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4822. ;; Preserve repeater.
  4823. (should
  4824. (equal "* H\nSCHEDULED: <2012-03-29 +2y>"
  4825. (org-test-with-temp-text "* H"
  4826. (let ((org-adapt-indentation nil)
  4827. (org-last-inserted-timestamp nil))
  4828. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4829. (replace-regexp-in-string
  4830. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4831. ;; Remove CLOSED keyword, if any.
  4832. (should
  4833. (equal "* H\nSCHEDULED: <2012-03-29>"
  4834. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4835. (let ((org-adapt-indentation nil)
  4836. (org-last-inserted-timestamp nil))
  4837. (org-schedule nil "<2012-03-29 Tue>"))
  4838. (replace-regexp-in-string
  4839. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4840. ;; With C-u argument, remove SCHEDULED keyword.
  4841. (should
  4842. (equal "* H\n"
  4843. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4844. (let ((org-adapt-indentation nil)
  4845. (org-last-inserted-timestamp nil))
  4846. (org-schedule '(4)))
  4847. (buffer-string))))
  4848. (should
  4849. (equal "* H"
  4850. (org-test-with-temp-text "* H"
  4851. (let ((org-adapt-indentation nil)
  4852. (org-last-inserted-timestamp nil))
  4853. (org-schedule '(4)))
  4854. (buffer-string))))
  4855. ;; With C-u C-u argument, prompt for a delay cookie.
  4856. (should
  4857. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4858. (cl-letf (((symbol-function 'org-read-date)
  4859. (lambda (&rest args)
  4860. (apply #'encode-time
  4861. (org-parse-time-string "2014-03-04")))))
  4862. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4863. (let ((org-adapt-indentation nil)
  4864. (org-last-inserted-timestamp nil))
  4865. (org-schedule '(16)))
  4866. (buffer-string)))))
  4867. (should-error
  4868. (cl-letf (((symbol-function 'org-read-date)
  4869. (lambda (&rest args)
  4870. (apply #'encode-time
  4871. (org-parse-time-string "2014-03-04")))))
  4872. (org-test-with-temp-text "* H"
  4873. (let ((org-adapt-indentation nil)
  4874. (org-last-inserted-timestamp nil))
  4875. (org-schedule '(16)))
  4876. (buffer-string))))
  4877. ;; When a region is active and
  4878. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4879. ;; same value in all headlines in region.
  4880. (should
  4881. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  4882. (org-test-with-temp-text "* H1\n* H2"
  4883. (let ((org-adapt-indentation nil)
  4884. (org-last-inserted-timestamp nil)
  4885. (org-loop-over-headlines-in-active-region t))
  4886. (transient-mark-mode 1)
  4887. (push-mark (point) t t)
  4888. (goto-char (point-max))
  4889. (org-schedule nil "2012-03-29"))
  4890. (replace-regexp-in-string
  4891. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4892. (should-not
  4893. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  4894. (org-test-with-temp-text "* H1\n* H2"
  4895. (let ((org-adapt-indentation nil)
  4896. (org-last-inserted-timestamp nil)
  4897. (org-loop-over-headlines-in-active-region nil))
  4898. (transient-mark-mode 1)
  4899. (push-mark (point) t t)
  4900. (goto-char (point-max))
  4901. (org-schedule nil "2012-03-29"))
  4902. (replace-regexp-in-string
  4903. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4904. (should
  4905. ;; check if a repeater survives re-scheduling.
  4906. (string-match-p
  4907. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4908. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4909. (let ((org-adapt-indentation nil)
  4910. (org-last-inserted-timestamp nil))
  4911. (org-schedule nil "2017-02-01"))
  4912. (buffer-string)))))
  4913. ;;; Property API
  4914. (ert-deftest test-org/buffer-property-keys ()
  4915. "Test `org-buffer-property-keys' specifications."
  4916. ;; Retrieve properties across siblings.
  4917. (should
  4918. (equal '("A" "B")
  4919. (org-test-with-temp-text "
  4920. * H1
  4921. :PROPERTIES:
  4922. :A: 1
  4923. :END:
  4924. * H2
  4925. :PROPERTIES:
  4926. :B: 1
  4927. :END:"
  4928. (org-buffer-property-keys))))
  4929. ;; Retrieve properties across children.
  4930. (should
  4931. (equal '("A" "B")
  4932. (org-test-with-temp-text "
  4933. * H1
  4934. :PROPERTIES:
  4935. :A: 1
  4936. :END:
  4937. ** H2
  4938. :PROPERTIES:
  4939. :B: 1
  4940. :END:"
  4941. (org-buffer-property-keys))))
  4942. ;; Retrieve muliple properties in the same drawer.
  4943. (should
  4944. (equal '("A" "B")
  4945. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4946. (org-buffer-property-keys))))
  4947. ;; Ignore extension symbol in property name.
  4948. (should
  4949. (equal '("A")
  4950. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4951. (org-buffer-property-keys))))
  4952. ;; With non-nil COLUMNS, extract property names from columns.
  4953. (should
  4954. (equal '("A" "B")
  4955. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4956. (org-buffer-property-keys nil nil t))))
  4957. (should
  4958. (equal '("A" "B" "COLUMNS")
  4959. (org-test-with-temp-text
  4960. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4961. (org-buffer-property-keys nil nil t))))
  4962. ;; In COLUMNS, ignore title and summary-type.
  4963. (should
  4964. (equal '("A")
  4965. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  4966. (org-buffer-property-keys nil nil t))))
  4967. (should
  4968. (equal '("A")
  4969. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  4970. (org-buffer-property-keys nil nil t))))
  4971. (should
  4972. (equal '("A")
  4973. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  4974. (org-buffer-property-keys nil nil t)))))
  4975. (ert-deftest test-org/property-values ()
  4976. "Test `org-property-values' specifications."
  4977. ;; Regular test.
  4978. (should
  4979. (equal '("2" "1")
  4980. (org-test-with-temp-text
  4981. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4982. (org-property-values "A"))))
  4983. ;; Ignore empty values.
  4984. (should-not
  4985. (org-test-with-temp-text
  4986. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4987. (org-property-values "A")))
  4988. ;; Take into consideration extended values.
  4989. (should
  4990. (equal '("1 2")
  4991. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4992. (org-property-values "A")))))
  4993. (ert-deftest test-org/set-property ()
  4994. "Test `org-set-property' specifications."
  4995. (should
  4996. (equal
  4997. ":PROPERTIES:\n:TEST: t\n:END:\n"
  4998. (org-test-with-temp-text ""
  4999. (let ((org-property-format "%s %s"))
  5000. (org-set-property "TEST" "t"))
  5001. (buffer-string))))
  5002. (should
  5003. (equal
  5004. "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5005. (org-test-with-temp-text "* H"
  5006. (let ((org-adapt-indentation nil)
  5007. (org-property-format "%s %s"))
  5008. (org-set-property "TEST" "t"))
  5009. (buffer-string)))))
  5010. (ert-deftest test-org/delete-property ()
  5011. "Test `org-delete-property' specifications."
  5012. (should
  5013. (equal
  5014. ""
  5015. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5016. (org-delete-property "TEST")
  5017. (buffer-string))))
  5018. (should
  5019. (equal
  5020. ":PROPERTIES:\n:TEST1: t\n:END:\n"
  5021. (org-test-with-temp-text ":PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5022. (org-delete-property "TEST2")
  5023. (buffer-string))))
  5024. (should
  5025. (equal
  5026. "* H\n"
  5027. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5028. (org-delete-property "TEST")
  5029. (buffer-string))))
  5030. (should
  5031. (equal
  5032. "* H\n:PROPERTIES:\n:TEST1: t\n:END:\n"
  5033. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5034. (org-delete-property "TEST2")
  5035. (buffer-string)))))
  5036. (ert-deftest test-org/delete-property-globally ()
  5037. "Test `org-delete-property-global' specifications."
  5038. (should
  5039. (equal
  5040. ""
  5041. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5042. (org-delete-property-globally "TEST")
  5043. (buffer-string))))
  5044. (should
  5045. (equal
  5046. "* H\n"
  5047. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n* H\n:PROPERTIES:\n:TEST: nil\n:END:"
  5048. (org-delete-property-globally "TEST")
  5049. (buffer-string)))))
  5050. (ert-deftest test-org/find-property ()
  5051. "Test `org-find-property' specifications."
  5052. ;; Regular test.
  5053. (should
  5054. (= 1
  5055. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  5056. (org-find-property "prop"))))
  5057. ;; Find properties in top-level property drawer.
  5058. (should
  5059. (= 1
  5060. (org-test-with-temp-text ":PROPERTIES:\n:PROP: value\n:END:"
  5061. (org-find-property "prop"))))
  5062. ;; Ignore false positives.
  5063. (should
  5064. (= 27
  5065. (org-test-with-temp-text
  5066. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  5067. (org-find-property "A"))))
  5068. ;; Return first entry found in buffer.
  5069. (should
  5070. (= 1
  5071. (org-test-with-temp-text
  5072. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5073. (org-find-property "A"))))
  5074. ;; Only search visible part of the buffer.
  5075. (should
  5076. (= 31
  5077. (org-test-with-temp-text
  5078. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5079. (org-narrow-to-subtree)
  5080. (org-find-property "A"))))
  5081. ;; With optional argument, only find entries with a specific value.
  5082. (should-not
  5083. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5084. (org-find-property "A" "2")))
  5085. (should
  5086. (= 31
  5087. (org-test-with-temp-text
  5088. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5089. (org-find-property "A" "2"))))
  5090. ;; Use "nil" for explicit nil values.
  5091. (should
  5092. (= 31
  5093. (org-test-with-temp-text
  5094. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5095. (org-find-property "A" "nil")))))
  5096. (ert-deftest test-org/entry-delete ()
  5097. "Test `org-entry-delete' specifications."
  5098. ;; Regular test.
  5099. (should
  5100. (string-match
  5101. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5102. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5103. (org-entry-delete (point) "A")
  5104. (buffer-string))))
  5105. ;; Also remove accumulated properties.
  5106. (should-not
  5107. (string-match
  5108. ":A"
  5109. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5110. (org-entry-delete (point) "A")
  5111. (buffer-string))))
  5112. ;; When last property is removed, remove the property drawer.
  5113. (should-not
  5114. (string-match
  5115. ":PROPERTIES:"
  5116. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5117. (org-entry-delete (point) "A")
  5118. (buffer-string))))
  5119. ;; Return a non-nil value when some property was removed.
  5120. (should
  5121. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5122. (org-entry-delete (point) "A")))
  5123. (should-not
  5124. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5125. (org-entry-delete (point) "C")))
  5126. ;; Special properties cannot be located in a drawer. Allow to
  5127. ;; remove them anyway, in case of user error.
  5128. (should
  5129. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5130. (org-entry-delete (point) "SCHEDULED"))))
  5131. (ert-deftest test-org/entry-get ()
  5132. "Test `org-entry-get' specifications."
  5133. ;; Regular test.
  5134. (should
  5135. (equal "1"
  5136. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:"
  5137. (org-entry-get (point) "A"))))
  5138. (should
  5139. (equal "1"
  5140. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5141. (org-entry-get (point) "A"))))
  5142. ;; Ignore case.
  5143. (should
  5144. (equal "1"
  5145. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5146. (org-entry-get (point) "a"))))
  5147. ;; Handle extended values, both before and after base value.
  5148. (should
  5149. (equal "1 2 3"
  5150. (org-test-with-temp-text
  5151. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5152. (org-entry-get (point) "A"))))
  5153. ;; Empty values are returned as the empty string.
  5154. (should
  5155. (equal ""
  5156. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5157. (org-entry-get (point) "A"))))
  5158. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5159. ;; otherwise, return nil.
  5160. (should-not
  5161. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5162. (org-entry-get (point) "A")))
  5163. (should
  5164. (equal "nil"
  5165. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5166. (org-entry-get (point) "A" nil t))))
  5167. ;; Return nil when no property is found, independently on the
  5168. ;; LITERAL-NIL argument.
  5169. (should-not
  5170. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5171. (org-entry-get (point) "B")))
  5172. (should-not
  5173. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5174. (org-entry-get (point) "B" nil t)))
  5175. ;; Handle inheritance, when allowed. Include extended values and
  5176. ;; possibly global values.
  5177. (should
  5178. (equal
  5179. "1"
  5180. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:\n* H"
  5181. (org-entry-get (point-max) "A" t))))
  5182. (should
  5183. (equal
  5184. "1"
  5185. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  5186. (org-entry-get (point-max) "A" t))))
  5187. (should
  5188. (equal
  5189. "1"
  5190. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  5191. (let ((org-use-property-inheritance t))
  5192. (org-entry-get (point-max) "A" 'selective)))))
  5193. (should-not
  5194. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  5195. (let ((org-use-property-inheritance nil))
  5196. (org-entry-get (point-max) "A" 'selective))))
  5197. (should
  5198. (equal
  5199. "1 2"
  5200. (org-test-with-temp-text
  5201. ":PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  5202. (org-entry-get (point-max) "A" t))))
  5203. (should
  5204. (equal
  5205. "1 2"
  5206. (org-test-with-temp-text
  5207. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5208. (org-entry-get (point-max) "A" t))))
  5209. (should
  5210. (equal
  5211. "1 2"
  5212. (org-test-with-temp-text
  5213. ":PROPERTIES:\n:A: 1\n:END:\n* H1\n* H2\n:PROPERTIES:\n:A+: 2\n:END:"
  5214. (org-entry-get (point-max) "A" t))))
  5215. (should
  5216. (equal
  5217. "1 2"
  5218. (org-test-with-temp-text
  5219. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2.1\n* H2.2\n:PROPERTIES:\n:A+: 2\n:END:"
  5220. (org-entry-get (point-max) "A" t))))
  5221. (should
  5222. (equal "1"
  5223. (org-test-with-temp-text
  5224. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  5225. (org-mode-restart)
  5226. (org-entry-get (point-max) "A" t))))
  5227. (should
  5228. (equal "0 1"
  5229. (org-test-with-temp-text
  5230. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5231. (org-mode-restart)
  5232. (org-entry-get (point-max) "A" t))))
  5233. ;; document level property-drawer has precedance over
  5234. ;; global-property by PROPERTY-keyword.
  5235. (should
  5236. (equal "0 2"
  5237. (org-test-with-temp-text
  5238. ":PROPERTIES:\n:A: 0\n:END:\n#+PROPERTY: A 1\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  5239. (org-mode-restart)
  5240. (org-entry-get (point-max) "A" t)))))
  5241. (ert-deftest test-org/entry-properties ()
  5242. "Test `org-entry-properties' specifications."
  5243. ;; Get "ITEM" property.
  5244. (should
  5245. (equal "H"
  5246. (org-test-with-temp-text "* TODO H"
  5247. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  5248. (should
  5249. (equal "H"
  5250. (org-test-with-temp-text "* TODO H"
  5251. (cdr (assoc "ITEM" (org-entry-properties))))))
  5252. ;; Get "TODO" property. TODO keywords are case sensitive.
  5253. (should
  5254. (equal "TODO"
  5255. (org-test-with-temp-text "* TODO H"
  5256. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  5257. (should
  5258. (equal "TODO"
  5259. (org-test-with-temp-text "* TODO H"
  5260. (cdr (assoc "TODO" (org-entry-properties))))))
  5261. (should-not
  5262. (org-test-with-temp-text "* H"
  5263. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5264. (should-not
  5265. (org-test-with-temp-text "* todo H"
  5266. (assoc "TODO" (org-entry-properties nil "TODO"))))
  5267. ;; Get "PRIORITY" property.
  5268. (should
  5269. (equal "A"
  5270. (org-test-with-temp-text "* [#A] H"
  5271. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5272. (should
  5273. (equal "A"
  5274. (org-test-with-temp-text "* [#A] H"
  5275. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  5276. (should
  5277. (equal (char-to-string org-priority-default)
  5278. (org-test-with-temp-text "* H"
  5279. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  5280. ;; Get "FILE" property.
  5281. (should
  5282. (org-test-with-temp-text-in-file "* H\nParagraph"
  5283. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  5284. (buffer-file-name))))
  5285. (should
  5286. (org-test-with-temp-text-in-file "* H\nParagraph"
  5287. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  5288. (buffer-file-name))))
  5289. (should-not
  5290. (org-test-with-temp-text "* H\nParagraph"
  5291. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  5292. ;; Get "TAGS" property.
  5293. (should
  5294. (equal ":tag1:tag2:"
  5295. (org-test-with-temp-text "* H :tag1:tag2:"
  5296. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  5297. (should
  5298. (equal ":tag1:tag2:"
  5299. (org-test-with-temp-text "* H :tag1:tag2:"
  5300. (cdr (assoc "TAGS" (org-entry-properties))))))
  5301. (should-not
  5302. (org-test-with-temp-text "* H"
  5303. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  5304. ;; Get "ALLTAGS" property.
  5305. (should
  5306. (equal ":tag1:tag2:"
  5307. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5308. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  5309. (should
  5310. (equal ":tag1:tag2:"
  5311. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  5312. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  5313. (should-not
  5314. (org-test-with-temp-text "* H"
  5315. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  5316. ;; Get "BLOCKED" property.
  5317. (should
  5318. (equal "t"
  5319. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  5320. (let ((org-enforce-todo-dependencies t)
  5321. (org-blocker-hook
  5322. '(org-block-todo-from-children-or-siblings-or-parent)))
  5323. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5324. (should
  5325. (equal ""
  5326. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  5327. (let ((org-enforce-todo-dependencies t)
  5328. (org-blocker-hook
  5329. '(org-block-todo-from-children-or-siblings-or-parent)))
  5330. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  5331. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  5332. (should
  5333. (equal
  5334. "[2012-03-29 thu.]"
  5335. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5336. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  5337. (should
  5338. (equal
  5339. "[2012-03-29 thu.]"
  5340. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  5341. (cdr (assoc "CLOSED" (org-entry-properties))))))
  5342. (should-not
  5343. (org-test-with-temp-text "* H"
  5344. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  5345. (should
  5346. (equal
  5347. "<2014-03-04 tue.>"
  5348. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5349. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  5350. (should
  5351. (equal
  5352. "<2014-03-04 tue.>"
  5353. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5354. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  5355. (should-not
  5356. (org-test-with-temp-text "* H"
  5357. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  5358. (should
  5359. (equal
  5360. "<2014-03-04 tue.>"
  5361. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5362. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  5363. (should
  5364. (equal
  5365. "<2014-03-04 tue.>"
  5366. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5367. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  5368. (should-not
  5369. (org-test-with-temp-text "* H"
  5370. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  5371. ;; Get "CATEGORY"
  5372. (should
  5373. (equal "cat"
  5374. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5375. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  5376. (should
  5377. (equal "cat"
  5378. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  5379. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5380. (should
  5381. (equal "cat"
  5382. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  5383. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5384. (should
  5385. (equal "cat2"
  5386. (org-test-with-temp-text
  5387. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5388. "\n"
  5389. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  5390. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  5391. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  5392. (should
  5393. (equal "<2012-03-29 thu.>"
  5394. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  5395. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5396. (should
  5397. (equal "[2012-03-29 thu.]"
  5398. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  5399. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  5400. (should
  5401. (equal "<2012-03-29 thu.>"
  5402. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  5403. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  5404. (should
  5405. (equal "[2014-03-04 tue.]"
  5406. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  5407. (cdr (assoc "TIMESTAMP_IA"
  5408. (org-entry-properties nil "TIMESTAMP_IA"))))))
  5409. (should-not
  5410. (equal "<2012-03-29 thu.>"
  5411. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  5412. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  5413. ;; Get standard properties.
  5414. (should
  5415. (equal "1"
  5416. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5417. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5418. ;; Handle extended properties.
  5419. (should
  5420. (equal "1 2 3"
  5421. (org-test-with-temp-text
  5422. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5423. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5424. (should
  5425. (equal "1 2 3"
  5426. (org-test-with-temp-text
  5427. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  5428. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  5429. ;; Ignore forbidden (special) properties.
  5430. (should-not
  5431. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  5432. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  5433. (ert-deftest test-org/entry-put ()
  5434. "Test `org-entry-put' specifications."
  5435. ;; Error when not a string or nil.
  5436. (should-error
  5437. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5438. (org-entry-put 1 "test" 2)))
  5439. ;; Error when property name is invalid.
  5440. (should-error
  5441. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5442. (org-entry-put 1 "no space" "value")))
  5443. (should-error
  5444. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  5445. (org-entry-put 1 "" "value")))
  5446. ;; Set "TODO" property.
  5447. (should
  5448. (string-match (regexp-quote " TODO H")
  5449. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5450. (org-entry-put (point) "TODO" "TODO")
  5451. (buffer-string))))
  5452. (should
  5453. (string-match (regexp-quote "* H")
  5454. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  5455. (org-entry-put (point) "TODO" nil)
  5456. (buffer-string))))
  5457. ;; Set "PRIORITY" property.
  5458. (should
  5459. (equal "* [#A] H"
  5460. (org-test-with-temp-text "* [#B] H"
  5461. (org-entry-put (point) "PRIORITY" "A")
  5462. (buffer-string))))
  5463. (should
  5464. (equal "* H"
  5465. (org-test-with-temp-text "* [#B] H"
  5466. (org-entry-put (point) "PRIORITY" nil)
  5467. (buffer-string))))
  5468. ;; Set "SCHEDULED" property.
  5469. (should
  5470. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  5471. (org-test-with-temp-text "* H"
  5472. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  5473. (buffer-string))))
  5474. (should
  5475. (string= "* H\n"
  5476. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5477. (org-entry-put (point) "SCHEDULED" nil)
  5478. (buffer-string))))
  5479. (should
  5480. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  5481. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5482. (org-entry-put (point) "SCHEDULED" "earlier")
  5483. (buffer-string))))
  5484. (should
  5485. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  5486. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  5487. (org-entry-put (point) "SCHEDULED" "later")
  5488. (buffer-string))))
  5489. ;; Set "DEADLINE" property.
  5490. (should
  5491. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  5492. (org-test-with-temp-text "* H"
  5493. (org-entry-put (point) "DEADLINE" "2014-03-04")
  5494. (buffer-string))))
  5495. (should
  5496. (string= "* H\n"
  5497. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5498. (org-entry-put (point) "DEADLINE" nil)
  5499. (buffer-string))))
  5500. (should
  5501. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  5502. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5503. (org-entry-put (point) "DEADLINE" "earlier")
  5504. (buffer-string))))
  5505. (should
  5506. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  5507. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  5508. (org-entry-put (point) "DEADLINE" "later")
  5509. (buffer-string))))
  5510. ;; Set "CATEGORY" property
  5511. (should
  5512. (string-match "^ *:CATEGORY: cat"
  5513. (org-test-with-temp-text "* H"
  5514. (org-entry-put (point) "CATEGORY" "cat")
  5515. (buffer-string))))
  5516. ;; Regular properties, with or without pre-existing drawer.
  5517. (should
  5518. (string-match "^ *:A: +2$"
  5519. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5520. (org-entry-put (point) "A" "2")
  5521. (buffer-string))))
  5522. (should
  5523. (string-match "^ *:A: +1$"
  5524. (org-test-with-temp-text "* H"
  5525. (org-entry-put (point) "A" "1")
  5526. (buffer-string))))
  5527. ;; Special case: two consecutive headlines.
  5528. (should
  5529. (string-match "\\* A\n *:PROPERTIES:"
  5530. (org-test-with-temp-text "* A\n** B"
  5531. (org-entry-put (point) "A" "1")
  5532. (buffer-string)))))
  5533. (ert-deftest test-org/refresh-properties ()
  5534. "Test `org-refresh-properties' specifications."
  5535. (should
  5536. (equal "1"
  5537. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5538. (org-refresh-properties "A" 'org-test)
  5539. (get-text-property (point) 'org-test))))
  5540. (should-not
  5541. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5542. (org-refresh-properties "B" 'org-test)
  5543. (get-text-property (point) 'org-test)))
  5544. ;; Handle properties only defined with extension syntax, i.e.,
  5545. ;; "PROPERTY+".
  5546. (should
  5547. (equal "1"
  5548. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5549. (org-refresh-properties "A" 'org-test)
  5550. (get-text-property (point) 'org-test))))
  5551. ;; When property is inherited, add text property to the whole
  5552. ;; sub-tree.
  5553. (should
  5554. (equal "1"
  5555. (org-test-with-temp-text
  5556. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5557. (let ((org-use-property-inheritance t))
  5558. (org-refresh-properties "A" 'org-test))
  5559. (get-text-property (point) 'org-test))))
  5560. ;; When a document level property-drawer is used, those properties
  5561. ;; should work exactly like headline-properties as if at a
  5562. ;; headline-level 0.
  5563. (should
  5564. (equal "1"
  5565. (org-test-with-temp-text
  5566. ":PROPERTIES:\n:A: 1\n:END:\n"
  5567. (org-mode-restart)
  5568. (let ((org-use-property-inheritance t))
  5569. (org-refresh-properties "A" 'org-test))
  5570. (get-text-property (point) 'org-test))))
  5571. (should-not
  5572. (equal "1"
  5573. (org-test-with-temp-text
  5574. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  5575. (org-mode-restart)
  5576. (let ((org-use-property-inheritance nil))
  5577. (org-refresh-properties "A" 'org-test))
  5578. (get-text-property (point) 'org-test))))
  5579. (should
  5580. (equal "1"
  5581. (org-test-with-temp-text
  5582. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  5583. (org-mode-restart)
  5584. (let ((org-use-property-inheritance t))
  5585. (org-refresh-properties "A" 'org-test))
  5586. (get-text-property (point) 'org-test))))
  5587. (should
  5588. (equal "2"
  5589. (org-test-with-temp-text
  5590. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1\n:PROPERTIES:\n:A: 2\n:END:"
  5591. (org-mode-restart)
  5592. (let ((org-use-property-inheritance t))
  5593. (org-refresh-properties "A" 'org-test))
  5594. (get-text-property (point) 'org-test))))
  5595. ;; When property is inherited, use global value across the whole
  5596. ;; buffer. However local values have precedence, as well as the
  5597. ;; document level property-drawer.
  5598. (should-not
  5599. (equal "1"
  5600. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5601. (org-mode-restart)
  5602. (let ((org-use-property-inheritance nil))
  5603. (org-refresh-properties "A" 'org-test))
  5604. (get-text-property (point) 'org-test))))
  5605. (should
  5606. (equal "1"
  5607. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5608. (org-mode-restart)
  5609. (let ((org-use-property-inheritance t))
  5610. (org-refresh-properties "A" 'org-test))
  5611. (get-text-property (point) 'org-test))))
  5612. (should
  5613. (equal "2"
  5614. (org-test-with-temp-text
  5615. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5616. (org-mode-restart)
  5617. (let ((org-use-property-inheritance t))
  5618. (org-refresh-properties "A" 'org-test))
  5619. (get-text-property (point) 'org-test))))
  5620. ;; When both keyword-property and document-level property-block is
  5621. ;; defined, the property-block has precedance.
  5622. (should
  5623. (equal "1"
  5624. (org-test-with-temp-text
  5625. ":PROPERTIES:\n:A: 1\n:END:\n#+PROPERTY: A 2\n<point>* H1"
  5626. (org-mode-restart)
  5627. (let ((org-use-property-inheritance t))
  5628. (org-refresh-properties "A" 'org-test))
  5629. (get-text-property (point) 'org-test)))))
  5630. (ert-deftest test-org/refresh-category-properties ()
  5631. "Test `org-refresh-category-properties' specifications"
  5632. (should
  5633. (equal "cat1"
  5634. (org-test-with-temp-text
  5635. ":PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5636. (org-refresh-category-properties)
  5637. (get-text-property (point) 'org-category))))
  5638. (should
  5639. (equal "cat1"
  5640. (org-test-with-temp-text
  5641. "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  5642. (org-refresh-category-properties)
  5643. (get-text-property (point) 'org-category))))
  5644. ;; Even though property-inheritance is deactivated, category
  5645. ;; property should be inherited. As described in
  5646. ;; `org-use-property-inheritance'.
  5647. (should
  5648. (equal "cat1"
  5649. (org-test-with-temp-text
  5650. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  5651. (org-mode-restart)
  5652. (let ((org-use-property-inheritance nil))
  5653. (org-refresh-category-properties))
  5654. (get-text-property (point) 'org-category))))
  5655. (should
  5656. (equal "cat1"
  5657. (org-test-with-temp-text
  5658. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  5659. (org-mode-restart)
  5660. (let ((org-use-property-inheritance t))
  5661. (org-refresh-category-properties))
  5662. (get-text-property (point) 'org-category))))
  5663. (should
  5664. (equal "cat2"
  5665. (org-test-with-temp-text
  5666. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H\n:PROPERTIES:\n:CATEGORY: cat2\n:END:\n"
  5667. (org-mode-restart)
  5668. (let ((org-use-property-inheritance t))
  5669. (org-refresh-category-properties))
  5670. (get-text-property (point) 'org-category)))))
  5671. ;;; Refile
  5672. (ert-deftest test-org/refile-get-targets ()
  5673. "Test `org-refile-get-targets' specifications."
  5674. ;; :maxlevel includes all headings above specified value.
  5675. (should
  5676. (equal '("H1" "H2" "H3")
  5677. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5678. (let ((org-refile-use-outline-path nil)
  5679. (org-refile-targets `((nil :maxlevel . 3))))
  5680. (mapcar #'car (org-refile-get-targets))))))
  5681. (should
  5682. (equal '("H1" "H2")
  5683. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5684. (let ((org-refile-use-outline-path nil)
  5685. (org-refile-targets `((nil :maxlevel . 2))))
  5686. (mapcar #'car (org-refile-get-targets))))))
  5687. ;; :level limits targets to headlines with the specified level.
  5688. (should
  5689. (equal '("H2")
  5690. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5691. (let ((org-refile-use-outline-path nil)
  5692. (org-refile-targets `((nil :level . 2))))
  5693. (mapcar #'car (org-refile-get-targets))))))
  5694. ;; :tag limits targets to headlines with specified tag.
  5695. (should
  5696. (equal '("H1")
  5697. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  5698. (let ((org-refile-use-outline-path nil)
  5699. (org-refile-targets `((nil :tag . "foo"))))
  5700. (mapcar #'car (org-refile-get-targets))))))
  5701. ;; :todo limits targets to headlines with specified TODO keyword.
  5702. (should
  5703. (equal '("H2")
  5704. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  5705. (let ((org-refile-use-outline-path nil)
  5706. (org-refile-targets `((nil :todo . "TODO"))))
  5707. (mapcar #'car (org-refile-get-targets))))))
  5708. ;; :regexp filters targets matching provided regexp.
  5709. (should
  5710. (equal '("F2" "F3")
  5711. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  5712. (let ((org-refile-use-outline-path nil)
  5713. (org-refile-targets `((nil :regexp . "F"))))
  5714. (mapcar #'car (org-refile-get-targets))))))
  5715. ;; A nil `org-refile-targets' includes only top level headlines in
  5716. ;; current buffer.
  5717. (should
  5718. (equal '("H1" "H2")
  5719. (org-test-with-temp-text "* H1\n** S1\n* H2"
  5720. (let ((org-refile-use-outline-path nil)
  5721. (org-refile-targets nil))
  5722. (mapcar #'car (org-refile-get-targets))))))
  5723. ;; Return value is the union of the targets according to all the
  5724. ;; defined rules. However, prevent duplicates.
  5725. (should
  5726. (equal '("F2" "F3" "H1")
  5727. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  5728. (let ((org-refile-use-outline-path nil)
  5729. (org-refile-targets `((nil :regexp . "F")
  5730. (nil :todo . "TODO"))))
  5731. (mapcar #'car (org-refile-get-targets))))))
  5732. (should
  5733. (equal '("F2" "F3" "H1")
  5734. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  5735. (let ((org-refile-use-outline-path nil)
  5736. (org-refile-targets `((nil :regexp . "F")
  5737. (nil :todo . "TODO"))))
  5738. (mapcar #'car (org-refile-get-targets))))))
  5739. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  5740. ;; paths.
  5741. (should
  5742. (equal '("H1" "H1/H2" "H1/H2/H3")
  5743. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  5744. (let ((org-refile-use-outline-path t)
  5745. (org-refile-targets `((nil :maxlevel . 3))))
  5746. (mapcar #'car (org-refile-get-targets))))))
  5747. ;; When providing targets as paths, escape forward slashes in
  5748. ;; headings with backslashes.
  5749. (should
  5750. (equal '("H1\\/foo")
  5751. (org-test-with-temp-text "* H1/foo"
  5752. (let ((org-refile-use-outline-path t)
  5753. (org-refile-targets `((nil :maxlevel . 1))))
  5754. (mapcar #'car (org-refile-get-targets))))))
  5755. ;; When `org-refile-use-outline-path' is `file', include file name
  5756. ;; without directory in targets.
  5757. (should
  5758. (org-test-with-temp-text-in-file "* H1"
  5759. (let* ((filename (buffer-file-name))
  5760. (org-refile-use-outline-path 'file)
  5761. (org-refile-targets `(((,filename) :level . 1))))
  5762. (member (file-name-nondirectory filename)
  5763. (mapcar #'car (org-refile-get-targets))))))
  5764. ;; When `org-refile-use-outline-path' is `full-file-path', include
  5765. ;; full file name.
  5766. (should
  5767. (org-test-with-temp-text-in-file "* H1"
  5768. (let* ((filename (file-truename (buffer-file-name)))
  5769. (org-refile-use-outline-path 'full-file-path)
  5770. (org-refile-targets `(((,filename) :level . 1))))
  5771. (member filename (mapcar #'car (org-refile-get-targets))))))
  5772. ;; When `org-refile-use-outline-path' is `buffer-name', include
  5773. ;; buffer name.
  5774. (should
  5775. (org-test-with-temp-text "* H1"
  5776. (let* ((org-refile-use-outline-path 'buffer-name)
  5777. (org-refile-targets `((nil :level . 1))))
  5778. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  5779. ;;; Sparse trees
  5780. (ert-deftest test-org/match-sparse-tree ()
  5781. "Test `org-match-sparse-tree' specifications."
  5782. ;; Match tags.
  5783. (should-not
  5784. (org-test-with-temp-text "* H\n** H1 :tag:"
  5785. (org-match-sparse-tree nil "tag")
  5786. (search-forward "H1")
  5787. (org-invisible-p2)))
  5788. (should
  5789. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5790. (org-match-sparse-tree nil "tag")
  5791. (search-forward "H2")
  5792. (org-invisible-p2)))
  5793. ;; "-" operator for tags.
  5794. (should-not
  5795. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5796. (org-match-sparse-tree nil "tag1-tag2")
  5797. (search-forward "H1")
  5798. (org-invisible-p2)))
  5799. (should
  5800. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5801. (org-match-sparse-tree nil "tag1-tag2")
  5802. (search-forward "H2")
  5803. (org-invisible-p2)))
  5804. ;; "&" operator for tags.
  5805. (should
  5806. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5807. (org-match-sparse-tree nil "tag1&tag2")
  5808. (search-forward "H1")
  5809. (org-invisible-p2)))
  5810. (should-not
  5811. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5812. (org-match-sparse-tree nil "tag1&tag2")
  5813. (search-forward "H2")
  5814. (org-invisible-p2)))
  5815. ;; "|" operator for tags.
  5816. (should-not
  5817. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5818. (org-match-sparse-tree nil "tag1|tag2")
  5819. (search-forward "H1")
  5820. (org-invisible-p2)))
  5821. (should-not
  5822. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5823. (org-match-sparse-tree nil "tag1|tag2")
  5824. (search-forward "H2")
  5825. (org-invisible-p2)))
  5826. ;; Regexp match on tags.
  5827. (should-not
  5828. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5829. (org-match-sparse-tree nil "{^tag.*}")
  5830. (search-forward "H1")
  5831. (org-invisible-p2)))
  5832. (should
  5833. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5834. (org-match-sparse-tree nil "{^tag.*}")
  5835. (search-forward "H2")
  5836. (org-invisible-p2)))
  5837. ;; Match group tags.
  5838. (should-not
  5839. (org-test-with-temp-text
  5840. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5841. (org-match-sparse-tree nil "work")
  5842. (search-forward "H1")
  5843. (org-invisible-p2)))
  5844. (should-not
  5845. (org-test-with-temp-text
  5846. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5847. (org-match-sparse-tree nil "work")
  5848. (search-forward "H2")
  5849. (org-invisible-p2)))
  5850. ;; Match group tags with hard brackets.
  5851. (should-not
  5852. (org-test-with-temp-text
  5853. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5854. (org-match-sparse-tree nil "work")
  5855. (search-forward "H1")
  5856. (org-invisible-p2)))
  5857. (should-not
  5858. (org-test-with-temp-text
  5859. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5860. (org-match-sparse-tree nil "work")
  5861. (search-forward "H2")
  5862. (org-invisible-p2)))
  5863. ;; Match tags in hierarchies
  5864. (should-not
  5865. (org-test-with-temp-text
  5866. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5867. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5868. #+TAGS: { Lev_3 : Lev_4 }\n
  5869. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5870. (org-match-sparse-tree nil "Lev_1")
  5871. (search-forward "H4")
  5872. (org-invisible-p2)))
  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+Lev_3")
  5880. (search-forward "H4")
  5881. (org-invisible-p2)))
  5882. ;; Match regular expressions in tags
  5883. (should-not
  5884. (org-test-with-temp-text
  5885. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5886. (org-match-sparse-tree nil "Lev")
  5887. (search-forward "H1")
  5888. (org-invisible-p2)))
  5889. (should
  5890. (org-test-with-temp-text
  5891. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5892. (org-match-sparse-tree nil "Lev")
  5893. (search-forward "H1")
  5894. (org-invisible-p2)))
  5895. ;; Match properties.
  5896. (should
  5897. (org-test-with-temp-text
  5898. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5899. (org-match-sparse-tree nil "A=\"1\"")
  5900. (search-forward "H2")
  5901. (org-invisible-p2)))
  5902. (should-not
  5903. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5904. (org-match-sparse-tree nil "A=\"1\"")
  5905. (search-forward "H2")
  5906. (org-invisible-p2)))
  5907. ;; Case is not significant when matching properties.
  5908. (should-not
  5909. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5910. (org-match-sparse-tree nil "a=\"1\"")
  5911. (search-forward "H2")
  5912. (org-invisible-p2)))
  5913. (should-not
  5914. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5915. (org-match-sparse-tree nil "A=\"1\"")
  5916. (search-forward "H2")
  5917. (org-invisible-p2)))
  5918. ;; Match special LEVEL property.
  5919. (should-not
  5920. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5921. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5922. (search-forward "H1")
  5923. (org-invisible-p2)))
  5924. (should
  5925. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5926. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5927. (search-forward "H2")
  5928. (org-invisible-p2)))
  5929. ;; Comparison operators when matching properties.
  5930. (should
  5931. (org-test-with-temp-text
  5932. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5933. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5934. (search-forward "H1")
  5935. (org-invisible-p2)))
  5936. (should-not
  5937. (org-test-with-temp-text
  5938. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5939. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5940. (search-forward "H2")
  5941. (org-invisible-p2)))
  5942. ;; Regexp match on properties values.
  5943. (should-not
  5944. (org-test-with-temp-text
  5945. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5946. (org-match-sparse-tree nil "A={f.*}")
  5947. (search-forward "H1")
  5948. (org-invisible-p2)))
  5949. (should
  5950. (org-test-with-temp-text
  5951. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5952. (org-match-sparse-tree nil "A={f.*}")
  5953. (search-forward "H2")
  5954. (org-invisible-p2)))
  5955. ;; With an optional argument, limit match to TODO entries.
  5956. (should-not
  5957. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5958. (org-match-sparse-tree t "tag")
  5959. (search-forward "H1")
  5960. (org-invisible-p2)))
  5961. (should
  5962. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5963. (org-match-sparse-tree t "tag")
  5964. (search-forward "H2")
  5965. (org-invisible-p2))))
  5966. (ert-deftest test-org/occur ()
  5967. "Test `org-occur' specifications."
  5968. ;; Count number of matches.
  5969. (should
  5970. (= 1
  5971. (org-test-with-temp-text "* H\nA\n* H2"
  5972. (org-occur "A"))))
  5973. (should
  5974. (= 2
  5975. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5976. (org-occur "A"))))
  5977. ;; Test CALLBACK optional argument.
  5978. (should
  5979. (= 0
  5980. (org-test-with-temp-text "* H\nA\n* H2"
  5981. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5982. (should
  5983. (= 1
  5984. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5985. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5986. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5987. (should
  5988. (= 2
  5989. (org-test-with-temp-text "Aa"
  5990. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5991. (should
  5992. (= 2
  5993. (org-test-with-temp-text "Aa"
  5994. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5995. (should
  5996. (= 1
  5997. (org-test-with-temp-text "Aa"
  5998. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5999. (should
  6000. (= 1
  6001. (org-test-with-temp-text "Aa"
  6002. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  6003. (should
  6004. (= 1
  6005. (org-test-with-temp-text "Aa"
  6006. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  6007. (should
  6008. (= 2
  6009. (org-test-with-temp-text "Aa"
  6010. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  6011. ;;; Tags
  6012. (ert-deftest test-org/tag-string-to-alist ()
  6013. "Test `org-tag-string-to-alist' specifications."
  6014. ;; Tag without selection key.
  6015. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  6016. ;; Tag with selection key.
  6017. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  6018. ;; Tag group.
  6019. (should
  6020. (equal
  6021. (org-tag-string-to-alist "[ group : t1 t2 ]")
  6022. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  6023. ;; Mutually exclusive tags.
  6024. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  6025. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  6026. (should
  6027. (equal
  6028. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  6029. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  6030. (ert-deftest test-org/tag-alist-to-string ()
  6031. "Test `org-tag-alist-to-string' specifications."
  6032. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  6033. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  6034. (should
  6035. (equal
  6036. (org-tag-alist-to-string
  6037. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6038. "[ group : t1 t2 ]"))
  6039. (should
  6040. (equal (org-tag-alist-to-string
  6041. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  6042. "{ tag1 tag2 }"))
  6043. (should
  6044. (equal
  6045. (org-tag-alist-to-string
  6046. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  6047. "{ group : tag1 tag2 }")))
  6048. (ert-deftest test-org/tag-alist-to-groups ()
  6049. "Test `org-tag-alist-to-groups' specifications."
  6050. (should
  6051. (equal (org-tag-alist-to-groups
  6052. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  6053. '(("group" "t1" "t2"))))
  6054. (should
  6055. (equal
  6056. (org-tag-alist-to-groups
  6057. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6058. '(("group" "t1" "t2"))))
  6059. (should-not
  6060. (org-tag-alist-to-groups
  6061. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  6062. (ert-deftest test-org/tag-align ()
  6063. "Test tags alignment."
  6064. ;; Test aligning tags with different display width.
  6065. (should
  6066. ;; 12345678901234567890
  6067. (equal "* Test :abc:"
  6068. (org-test-with-temp-text "* Test :abc:"
  6069. (let ((org-tags-column -20)
  6070. (indent-tabs-mode nil))
  6071. (org-fix-tags-on-the-fly))
  6072. (buffer-string))))
  6073. (should
  6074. ;; 12345678901234567890
  6075. (equal "* Test :日本語:"
  6076. (org-test-with-temp-text "* Test :日本語:"
  6077. (let ((org-tags-column -20)
  6078. (indent-tabs-mode nil))
  6079. (org-fix-tags-on-the-fly))
  6080. (buffer-string))))
  6081. ;; Make sure aligning tags do not skip invisible text.
  6082. (should
  6083. (equal "* [[linkx]] :tag:"
  6084. (org-test-with-temp-text "* [[link<point>]] :tag:"
  6085. (let ((org-tags-column 0))
  6086. (org-fix-tags-on-the-fly)
  6087. (insert "x")
  6088. (buffer-string)))))
  6089. ;; Aligning tags preserve position.
  6090. (should
  6091. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  6092. (let ((org-tags-column 78)
  6093. (indent-tabs-mode nil))
  6094. (org-fix-tags-on-the-fly))
  6095. (current-column))))
  6096. ;; Aligning all tags in visible buffer.
  6097. (should
  6098. ;; 12345678901234567890
  6099. (equal (concat "* Level 1 :abc:\n"
  6100. "** Level 2 :def:")
  6101. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  6102. "** Level 2 :def:")
  6103. (let ((org-tags-column -20)
  6104. (indent-tabs-mode nil))
  6105. ;; (org-align-tags :all) must work even when the point
  6106. ;; is at the end of the buffer.
  6107. (goto-char (point-max))
  6108. (org-align-tags :all))
  6109. (buffer-string)))))
  6110. (ert-deftest test-org/get-tags ()
  6111. "Test `org-get-tags' specifications."
  6112. ;; Standard test.
  6113. (should
  6114. (equal '("foo")
  6115. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  6116. (should
  6117. (equal '("foo" "bar")
  6118. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  6119. ;; Return nil when there is no tag.
  6120. (should-not
  6121. (org-test-with-temp-text "* Test" (org-get-tags)))
  6122. ;; Tags are inherited from parent headlines.
  6123. (should
  6124. (equal '("tag")
  6125. (let ((org-use-tag-inheritance t))
  6126. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  6127. (org-get-tags)))))
  6128. ;; Tags are inherited from `org-file-tags'.
  6129. (should
  6130. (equal '("tag")
  6131. (org-test-with-temp-text "* H1"
  6132. (let ((org-file-tags '("tag"))
  6133. (org-use-tag-inheritance t))
  6134. (org-get-tags)))))
  6135. ;; Only inherited tags have the `inherited' text property.
  6136. (should
  6137. (get-text-property 0 'inherited
  6138. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6139. (let ((org-use-tag-inheritance t))
  6140. (assoc-string "foo" (org-get-tags))))))
  6141. (should-not
  6142. (get-text-property 0 'inherited
  6143. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6144. (let ((org-use-tag-inheritance t))
  6145. (assoc-string "bar" (org-get-tags))))))
  6146. ;; Obey to `org-use-tag-inheritance'.
  6147. (should-not
  6148. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6149. (let ((org-use-tag-inheritance nil))
  6150. (assoc-string "foo" (org-get-tags)))))
  6151. (should-not
  6152. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  6153. (let ((org-use-tag-inheritance nil)
  6154. (org-file-tags '("foo")))
  6155. (assoc-string "foo" (org-get-tags)))))
  6156. (should-not
  6157. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6158. (let ((org-use-tag-inheritance '("bar")))
  6159. (assoc-string "foo" (org-get-tags)))))
  6160. (should
  6161. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6162. (let ((org-use-tag-inheritance '("bar")))
  6163. (assoc-string "bar" (org-get-tags)))))
  6164. (should-not
  6165. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6166. (let ((org-use-tag-inheritance "b.*"))
  6167. (assoc-string "foo" (org-get-tags)))))
  6168. (should
  6169. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6170. (let ((org-use-tag-inheritance "b.*"))
  6171. (assoc-string "bar" (org-get-tags)))))
  6172. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  6173. (should
  6174. (equal '("baz")
  6175. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  6176. (let ((org-use-tag-inheritance t))
  6177. (org-get-tags nil t)))))
  6178. ;; When optional argument POS is non-nil, get tags there instead.
  6179. (should
  6180. (equal '("foo")
  6181. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  6182. (org-get-tags 1))))
  6183. ;; Make sure tags excluded from inheritance are returned if local
  6184. (should
  6185. (equal '("foo")
  6186. (org-test-with-temp-text "* Test :foo:"
  6187. (let ((org-use-tag-inheritance t)
  6188. (org-tags-exclude-from-inheritance '("foo")))
  6189. (org-get-tags)))))
  6190. ;; Test the collection of tags from #+filetags and parent tags.
  6191. (should
  6192. (equal '("a" "b" "c" "d")
  6193. (org-test-with-temp-text (concat "#+filetags: a\n"
  6194. "* Level 1 :b:\n"
  6195. "** Level 2 :c:\n"
  6196. "*** Level 3 :d:\n"
  6197. "<point>")
  6198. (let ((org-use-tag-inheritance t))
  6199. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  6200. (org-get-tags)))))
  6201. ;; Pathological case: tagged headline with an empty body.
  6202. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  6203. (ert-deftest test-org/set-tags ()
  6204. "Test `org-set-tags' specifications."
  6205. ;; Throw an error on invalid data.
  6206. (should-error
  6207. (org-test-with-temp-text "* H"
  6208. (org-set-tags 'foo)))
  6209. ;; `nil', an empty, and a blank string remove all tags.
  6210. (should
  6211. (equal "* H"
  6212. (org-test-with-temp-text "* H :tag1:tag2:"
  6213. (org-set-tags nil)
  6214. (buffer-string))))
  6215. (should
  6216. (equal "* H"
  6217. (org-test-with-temp-text "* H :tag1:tag2:"
  6218. (org-set-tags "")
  6219. (buffer-string))))
  6220. (should
  6221. (equal "* H"
  6222. (org-test-with-temp-text "* H :tag1:tag2:"
  6223. (org-set-tags " ")
  6224. (buffer-string))))
  6225. ;; If there's nothing to remove, just bail out.
  6226. (should
  6227. (equal "* H"
  6228. (org-test-with-temp-text "* H"
  6229. (org-set-tags nil)
  6230. (buffer-string))))
  6231. (should
  6232. (equal "* "
  6233. (org-test-with-temp-text "* "
  6234. (org-set-tags nil)
  6235. (buffer-string))))
  6236. ;; If DATA is a tag string, set current tags to it, even if it means
  6237. ;; replacing old tags.
  6238. (should
  6239. (equal "* H :tag0:"
  6240. (org-test-with-temp-text "* H :tag1:tag2:"
  6241. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6242. (buffer-string))))
  6243. (should
  6244. (equal "* H :tag0:"
  6245. (org-test-with-temp-text "* H"
  6246. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  6247. (buffer-string))))
  6248. ;; If DATA is a list, set tags to this list, even if it means
  6249. ;; replacing old tags.
  6250. (should
  6251. (equal "* H :tag0:"
  6252. (org-test-with-temp-text "* H :tag1:tag2:"
  6253. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6254. (buffer-string))))
  6255. (should
  6256. (equal "* H :tag0:"
  6257. (org-test-with-temp-text "* H"
  6258. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6259. (buffer-string))))
  6260. ;; When set, apply `org-tags-sort-function'.
  6261. (should
  6262. (equal "* H :a:b:"
  6263. (org-test-with-temp-text "* H"
  6264. (let ((org-tags-column 1)
  6265. (org-tags-sort-function #'string<))
  6266. (org-set-tags '("b" "a"))
  6267. (buffer-string)))))
  6268. ;; When new tags are identical to the previous ones, still align.
  6269. (should
  6270. (equal "* H :foo:"
  6271. (org-test-with-temp-text "* H :foo:"
  6272. (let ((org-tags-column 1))
  6273. (org-set-tags '("foo"))
  6274. (buffer-string)))))
  6275. ;; When tags have been changed, run `org-after-tags-change-hook'.
  6276. (should
  6277. (catch :return
  6278. (org-test-with-temp-text "* H :foo:"
  6279. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6280. (org-set-tags '("bar"))
  6281. nil))))
  6282. (should-not
  6283. (catch :return
  6284. (org-test-with-temp-text "* H :foo:"
  6285. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  6286. (org-set-tags '("foo"))
  6287. nil))))
  6288. ;; Special case: handle empty headlines.
  6289. (should
  6290. (equal "* :tag0:"
  6291. (org-test-with-temp-text "* "
  6292. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  6293. (buffer-string))))
  6294. ;; Modify buffer only when a tag change happens or alignment is
  6295. ;; done.
  6296. (should-not
  6297. (org-test-with-temp-text "* H :foo:"
  6298. (set-buffer-modified-p nil)
  6299. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  6300. (buffer-modified-p)))
  6301. (should
  6302. (org-test-with-temp-text "* H :foo:"
  6303. (set-buffer-modified-p nil)
  6304. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  6305. (buffer-modified-p)))
  6306. (should
  6307. (org-test-with-temp-text "* H :foo:"
  6308. (set-buffer-modified-p nil)
  6309. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  6310. (buffer-modified-p)))
  6311. ;; Pathological case: when setting tags of a folded headline, do not
  6312. ;; let new tags being sucked into invisibility.
  6313. (should-not
  6314. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  6315. ;; Show only headlines
  6316. (org-content)
  6317. ;; Set NEXT tag on current entry
  6318. (org-set-tags ":NEXT:")
  6319. ;; Move point to that NEXT tag
  6320. (search-forward "NEXT") (backward-word)
  6321. ;; And it should be visible (i.e. no overlays)
  6322. (overlays-at (point)))))
  6323. (ert-deftest test-org/set-tags-command ()
  6324. "Test `org-set-tags-command' specifications"
  6325. ;; Set tags at current headline.
  6326. (should
  6327. (equal "* H1 :foo:"
  6328. (org-test-with-temp-text "* H1"
  6329. (cl-letf (((symbol-function 'completing-read)
  6330. (lambda (&rest args) ":foo:")))
  6331. (let ((org-use-fast-tag-selection nil)
  6332. (org-tags-column 1))
  6333. (org-set-tags-command)))
  6334. (buffer-string))))
  6335. ;; Preserve position when called from the section below.
  6336. (should
  6337. (equal "* H1 :foo:\nContents"
  6338. (org-test-with-temp-text "* H1\n<point>Contents"
  6339. (cl-letf (((symbol-function 'completing-read)
  6340. (lambda (&rest args) ":foo:")))
  6341. (let ((org-use-fast-tag-selection nil)
  6342. (org-tags-column 1))
  6343. (org-set-tags-command)))
  6344. (buffer-string))))
  6345. (should-not
  6346. (equal "* H1 :foo:\nContents2"
  6347. (org-test-with-temp-text "* H1\n<point>Contents2"
  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. (org-at-heading-p))))
  6354. ;; Strip all forbidden characters from user-entered tags.
  6355. (should
  6356. (equal "* H1 :foo:"
  6357. (org-test-with-temp-text "* H1"
  6358. (cl-letf (((symbol-function 'completing-read)
  6359. (lambda (&rest args) ": foo *:")))
  6360. (let ((org-use-fast-tag-selection nil)
  6361. (org-tags-column 1))
  6362. (org-set-tags-command)))
  6363. (buffer-string))))
  6364. ;; When a region is active and
  6365. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  6366. ;; same value in all headlines in region.
  6367. (should
  6368. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  6369. (org-test-with-temp-text "* H1\nContents\n* H2"
  6370. (cl-letf (((symbol-function 'completing-read)
  6371. (lambda (&rest args) ":foo:")))
  6372. (let ((org-use-fast-tag-selection nil)
  6373. (org-loop-over-headlines-in-active-region t)
  6374. (org-tags-column 1))
  6375. (transient-mark-mode 1)
  6376. (push-mark (point) t t)
  6377. (goto-char (point-max))
  6378. (org-set-tags-command)))
  6379. (buffer-string))))
  6380. (should
  6381. (equal "* H1\nContents\n* H2 :foo:"
  6382. (org-test-with-temp-text "* H1\nContents\n* H2"
  6383. (cl-letf (((symbol-function 'completing-read)
  6384. (lambda (&rest args) ":foo:")))
  6385. (let ((org-use-fast-tag-selection nil)
  6386. (org-loop-over-headlines-in-active-region nil)
  6387. (org-tags-column 1))
  6388. (transient-mark-mode 1)
  6389. (push-mark (point) t t)
  6390. (goto-char (point-max))
  6391. (org-set-tags-command)))
  6392. (buffer-string))))
  6393. ;; With a C-u prefix argument, align all tags in the buffer.
  6394. (should
  6395. (equal "* H1 :foo:\n* H2 :bar:"
  6396. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  6397. (let ((org-tags-column 1)) (org-set-tags-command '(4)))
  6398. (buffer-string))))
  6399. ;; Point does not move with empty headline.
  6400. (should
  6401. (equal ":foo:"
  6402. (org-test-with-temp-text "* <point>"
  6403. (cl-letf (((symbol-function 'completing-read)
  6404. (lambda (&rest args) ":foo:")))
  6405. (let ((org-use-fast-tag-selection nil)
  6406. (org-tags-column 1))
  6407. (org-set-tags-command)))
  6408. (buffer-substring (point) (line-end-position)))))
  6409. ;; Point does not move at start of line.
  6410. (should
  6411. (equal "* H1 :foo:"
  6412. (org-test-with-temp-text "* H1"
  6413. (cl-letf (((symbol-function 'completing-read)
  6414. (lambda (&rest args) ":foo:")))
  6415. (let ((org-use-fast-tag-selection nil)
  6416. (org-tags-column 1))
  6417. (org-set-tags-command)))
  6418. (buffer-substring (point) (line-end-position)))))
  6419. ;; Point does not move when within *'s.
  6420. (should
  6421. (equal "* H1 :foo:"
  6422. (org-test-with-temp-text "*<point>* H1"
  6423. (cl-letf (((symbol-function 'completing-read)
  6424. (lambda (&rest args) ":foo:")))
  6425. (let ((org-use-fast-tag-selection nil)
  6426. (org-tags-column 1))
  6427. (org-set-tags-command)))
  6428. (buffer-substring (point) (line-end-position)))))
  6429. ;; Point workaround does not get fooled when looking at a space.
  6430. (should
  6431. (equal " b :foo:"
  6432. (org-test-with-temp-text "* a<point> b"
  6433. (cl-letf (((symbol-function 'completing-read)
  6434. (lambda (&rest args) ":foo:")))
  6435. (let ((org-use-fast-tag-selection nil)
  6436. (org-tags-column 1))
  6437. (org-set-tags-command)))
  6438. (buffer-substring (point) (line-end-position))))))
  6439. (ert-deftest test-org/toggle-tag ()
  6440. "Test `org-toggle-tag' specifications."
  6441. ;; Insert missing tag.
  6442. (should
  6443. (equal "* H :tag:"
  6444. (org-test-with-temp-text "* H"
  6445. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  6446. (buffer-string))))
  6447. (should
  6448. (equal "* H :tag1:tag2:"
  6449. (org-test-with-temp-text "* H :tag1:"
  6450. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6451. (buffer-string))))
  6452. ;; Remove existing tag.
  6453. (should
  6454. (equal "* H"
  6455. (org-test-with-temp-text "* H :tag:"
  6456. (org-toggle-tag "tag")
  6457. (buffer-string))))
  6458. (should
  6459. (equal "* H :tag1:"
  6460. (org-test-with-temp-text "* H :tag1:tag2:"
  6461. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  6462. (buffer-string))))
  6463. (should
  6464. (equal "* H :tag2:"
  6465. (org-test-with-temp-text "* H :tag1:tag2:"
  6466. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  6467. (buffer-string))))
  6468. ;; With optional argument ONOFF set to `on', try to insert the tag,
  6469. ;; even if its already there.
  6470. (should
  6471. (equal "* H :tag:"
  6472. (org-test-with-temp-text "* H"
  6473. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6474. (buffer-string))))
  6475. (should
  6476. (equal "* H :tag:"
  6477. (org-test-with-temp-text "* H :tag:"
  6478. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  6479. (buffer-string))))
  6480. ;; With optional argument ONOFF set to `off', try to remove the tag,
  6481. ;; even if its not there.
  6482. (should
  6483. (equal "* H"
  6484. (org-test-with-temp-text "* H :tag:"
  6485. (org-toggle-tag "tag" 'off)
  6486. (buffer-string))))
  6487. (should
  6488. (equal "* H :tag:"
  6489. (org-test-with-temp-text "* H :tag:"
  6490. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  6491. (buffer-string))))
  6492. ;; Special case: Handle properly tag inheritance. In particular, do
  6493. ;; not set inherited tags.
  6494. (should
  6495. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  6496. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  6497. (let ((org-use-tag-inheritance t)
  6498. (org-tags-column 1))
  6499. (org-toggle-tag "tag"))
  6500. (buffer-string))))
  6501. (should
  6502. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  6503. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  6504. (let ((org-use-tag-inheritance t)
  6505. (org-tags-column 1))
  6506. (org-toggle-tag "foo"))
  6507. (buffer-string)))))
  6508. (ert-deftest test-org/tags-expand ()
  6509. "Test `org-tags-expand' specifications."
  6510. ;; Expand tag groups as a regexp enclosed within curly brackets.
  6511. (should
  6512. (equal "{\\<[ABC]\\>}"
  6513. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6514. (org-mode-restart)
  6515. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6516. (should
  6517. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  6518. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  6519. (org-mode-restart)
  6520. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  6521. ;; Preserve operator before the regexp.
  6522. (should
  6523. (equal "+{\\<[ABC]\\>}"
  6524. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6525. (org-mode-restart)
  6526. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  6527. (should
  6528. (equal "-{\\<[ABC]\\>}"
  6529. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6530. (org-mode-restart)
  6531. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  6532. ;; Handle "|" syntax.
  6533. (should
  6534. (equal "{\\<[ABC]\\>}|D"
  6535. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6536. (org-mode-restart)
  6537. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  6538. ;; Handle nested groups.
  6539. (should
  6540. (equal "{\\<[A-D]\\>}"
  6541. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  6542. (org-mode-restart)
  6543. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  6544. ;; Expand multiple occurrences of the same group.
  6545. (should
  6546. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  6547. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6548. (org-mode-restart)
  6549. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  6550. ;; Preserve regexp matches.
  6551. (should
  6552. (equal "{A+}"
  6553. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  6554. (org-mode-restart)
  6555. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  6556. ;;; TODO keywords
  6557. (ert-deftest test-org/auto-repeat-maybe ()
  6558. "Test `org-auto-repeat-maybe' specifications."
  6559. ;; Do not auto repeat when there is no valid time stamp with
  6560. ;; a repeater in the entry.
  6561. (should-not
  6562. (string-prefix-p
  6563. "* TODO H"
  6564. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6565. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  6566. (org-todo "DONE")
  6567. (buffer-string)))))
  6568. (should-not
  6569. (string-prefix-p
  6570. "* TODO H"
  6571. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6572. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  6573. (org-todo "DONE")
  6574. (buffer-string)))))
  6575. ;; When switching to DONE state, switch back to first TODO keyword
  6576. ;; in sequence, or the same keyword if they have different types.
  6577. (should
  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 +2y>"
  6582. (org-todo "DONE")
  6583. (buffer-string)))))
  6584. (should
  6585. (string-prefix-p
  6586. "* KWD1 H"
  6587. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6588. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6589. (org-todo "DONE")
  6590. (buffer-string)))))
  6591. (should
  6592. (string-prefix-p
  6593. "* KWD2 H"
  6594. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  6595. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  6596. (org-todo "DONE")
  6597. (buffer-string)))))
  6598. ;; If there was no TODO keyword in the first place, do not insert
  6599. ;; any either.
  6600. (should
  6601. (string-prefix-p
  6602. "* H"
  6603. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6604. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  6605. (org-todo "DONE")
  6606. (buffer-string)))))
  6607. ;; Revert to REPEAT_TO_STATE, if set.
  6608. (should
  6609. (string-prefix-p
  6610. "* KWD2 H"
  6611. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  6612. (org-test-with-temp-text
  6613. "* KWD2 H
  6614. :PROPERTIES:
  6615. :REPEAT_TO_STATE: KWD2
  6616. :END:
  6617. <2012-03-29 Thu +2y>"
  6618. (org-todo "DONE")
  6619. (buffer-string)))))
  6620. ;; When switching to DONE state, update base date. If there are
  6621. ;; multiple repeated time stamps, update them all.
  6622. (should
  6623. (string-match-p
  6624. "<2014-03-29 .* \\+2y>"
  6625. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6626. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  6627. (org-todo "DONE")
  6628. (buffer-string)))))
  6629. (should
  6630. (string-match-p
  6631. "<2015-03-04 .* \\+1y>"
  6632. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6633. (org-test-with-temp-text
  6634. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  6635. (org-todo "DONE")
  6636. (buffer-string)))))
  6637. ;; Throw an error if repeater unit is the hour and no time is
  6638. ;; provided in the time-stamp.
  6639. (should-error
  6640. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6641. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  6642. (org-todo "DONE")
  6643. (buffer-string))))
  6644. ;; Handle every repeater type using hours step.
  6645. (should
  6646. (string-match-p
  6647. "2014-03-04 .* 02:00"
  6648. (org-test-at-time "<2014-03-04 02:35>"
  6649. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 +8h>"
  6650. (org-todo "DONE")
  6651. (buffer-string)))))
  6652. (should
  6653. (string-match-p
  6654. "2014-03-04 .* 10:00"
  6655. (org-test-at-time "<2014-03-04 02:35>"
  6656. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 ++8h>"
  6657. (org-todo "DONE")
  6658. (buffer-string)))))
  6659. (should
  6660. (string-match-p
  6661. "2014-03-04 .* 10:35"
  6662. (org-test-at-time "<2014-03-04 02:35>"
  6663. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 .+8h>"
  6664. (org-todo "DONE")
  6665. (buffer-string)))))
  6666. ;; Do not repeat inactive time stamps with a repeater.
  6667. (should-not
  6668. (string-match-p
  6669. "\\[2014-03-29 .* \\+2y\\]"
  6670. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6671. (org-test-with-temp-text
  6672. "* TODO H\n[2012-03-29 Thu. +2y]"
  6673. (org-todo "DONE")
  6674. (buffer-string)))))
  6675. ;; Do not repeat commented time stamps.
  6676. (should-not
  6677. (string-prefix-p
  6678. "<2015-03-04 .* \\+1y>"
  6679. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6680. (org-test-with-temp-text
  6681. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  6682. (org-todo "DONE")
  6683. (buffer-string)))))
  6684. (should-not
  6685. (string-prefix-p
  6686. "<2015-03-04 .* \\+1y>"
  6687. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6688. (org-test-with-temp-text
  6689. "* TODO H
  6690. <2012-03-29 Thu. +2y>
  6691. #+BEGIN_EXAMPLE
  6692. <2014-03-04 Tue +1y>
  6693. #+END_EXAMPLE"
  6694. (org-todo "DONE")
  6695. (buffer-string)))))
  6696. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  6697. ;; entry, record time of last repeat.
  6698. (should-not
  6699. (string-match-p
  6700. ":LAST_REPEAT:"
  6701. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6702. (org-log-repeat nil))
  6703. (cl-letf (((symbol-function 'org-add-log-setup)
  6704. (lambda (&rest args) nil)))
  6705. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6706. (org-todo "DONE")
  6707. (buffer-string))))))
  6708. (should
  6709. (string-match-p
  6710. ":LAST_REPEAT:"
  6711. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  6712. (org-log-repeat t))
  6713. (cl-letf (((symbol-function 'org-add-log-setup)
  6714. (lambda (&rest args) nil)))
  6715. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  6716. (org-todo "DONE")
  6717. (buffer-string))))))
  6718. (should
  6719. (string-match-p
  6720. ":LAST_REPEAT:"
  6721. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6722. (cl-letf (((symbol-function 'org-add-log-setup)
  6723. (lambda (&rest args) nil)))
  6724. (org-test-with-temp-text
  6725. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  6726. (org-todo "DONE")
  6727. (buffer-string))))))
  6728. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  6729. ;; the entry as it is no longer relevant.
  6730. (should-not
  6731. (string-match-p
  6732. "^SCHEDULED:"
  6733. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  6734. (org-test-with-temp-text
  6735. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  6736. (org-todo "DONE")
  6737. (buffer-string)))))
  6738. ;; Properly advance repeater even when a clock entry is specified
  6739. ;; and `org-log-repeat' is nil.
  6740. (should
  6741. (string-match-p
  6742. "SCHEDULED: <2014-03-29"
  6743. (let ((org-log-repeat nil)
  6744. (org-todo-keywords '((sequence "TODO" "DONE"))))
  6745. (org-test-with-temp-text
  6746. "* TODO H
  6747. SCHEDULED: <2012-03-29 Thu +2y>
  6748. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  6749. (org-todo "DONE")
  6750. (buffer-string))))))
  6751. ;;; Timestamps API
  6752. (ert-deftest test-org/at-timestamp-p ()
  6753. "Test `org-at-timestamp-p' specifications."
  6754. (should
  6755. (org-test-with-temp-text "<2012-03-29 Thu>"
  6756. (org-at-timestamp-p)))
  6757. (should-not
  6758. (org-test-with-temp-text "2012-03-29 Thu"
  6759. (org-at-timestamp-p)))
  6760. ;; Test return values.
  6761. (should
  6762. (eq 'bracket
  6763. (org-test-with-temp-text "<2012-03-29 Thu>"
  6764. (org-at-timestamp-p))))
  6765. (should
  6766. (eq 'year
  6767. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  6768. (org-at-timestamp-p))))
  6769. (should
  6770. (eq 'month
  6771. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  6772. (org-at-timestamp-p))))
  6773. (should
  6774. (eq 'day
  6775. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  6776. (org-at-timestamp-p))))
  6777. (should
  6778. (eq 'day
  6779. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  6780. (org-at-timestamp-p))))
  6781. (should
  6782. (wholenump
  6783. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  6784. (org-at-timestamp-p))))
  6785. (should
  6786. (eq 'bracket
  6787. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  6788. (org-at-timestamp-p))))
  6789. (should
  6790. (eq 'after
  6791. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  6792. (org-at-timestamp-p))))
  6793. ;; Test `inactive' optional argument.
  6794. (should
  6795. (org-test-with-temp-text "[2012-03-29 Thu]"
  6796. (org-at-timestamp-p 'inactive)))
  6797. (should-not
  6798. (org-test-with-temp-text "[2012-03-29 Thu]"
  6799. (org-at-timestamp-p)))
  6800. ;; When optional argument is `agenda', recognize time-stamps in
  6801. ;; planning info line, property drawers and clocks.
  6802. (should
  6803. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6804. (org-at-timestamp-p 'agenda)))
  6805. (should-not
  6806. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  6807. (org-at-timestamp-p)))
  6808. (should
  6809. (org-test-with-temp-text
  6810. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6811. (org-at-timestamp-p 'agenda)))
  6812. (should-not
  6813. (org-test-with-temp-text
  6814. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  6815. (org-at-timestamp-p)))
  6816. (should
  6817. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6818. (let ((org-agenda-include-inactive-timestamps t))
  6819. (org-at-timestamp-p 'agenda))))
  6820. (should-not
  6821. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6822. (let ((org-agenda-include-inactive-timestamps t))
  6823. (org-at-timestamp-p))))
  6824. (should-not
  6825. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  6826. (let ((org-agenda-include-inactive-timestamps t))
  6827. (org-at-timestamp-p 'inactive))))
  6828. ;; When optional argument is `lax', match any part of the document
  6829. ;; with Org timestamp syntax.
  6830. (should
  6831. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6832. (org-at-timestamp-p 'lax)))
  6833. (should-not
  6834. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  6835. (org-at-timestamp-p)))
  6836. (should
  6837. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6838. (org-at-timestamp-p 'lax)))
  6839. (should-not
  6840. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  6841. (org-at-timestamp-p)))
  6842. (should
  6843. (org-test-with-temp-text
  6844. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6845. (org-at-timestamp-p 'lax)))
  6846. (should-not
  6847. (org-test-with-temp-text
  6848. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  6849. (org-at-timestamp-p)))
  6850. ;; Optional argument `lax' also matches inactive timestamps.
  6851. (should
  6852. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  6853. (org-at-timestamp-p 'lax))))
  6854. (ert-deftest test-org/time-stamp ()
  6855. "Test `org-time-stamp' specifications."
  6856. ;; Insert chosen time stamp at point.
  6857. (should
  6858. (string-match
  6859. "Te<2014-03-04 .*?>xt"
  6860. (org-test-with-temp-text "Te<point>xt"
  6861. (cl-letf (((symbol-function 'org-read-date)
  6862. (lambda (&rest args)
  6863. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6864. (org-time-stamp nil)
  6865. (buffer-string)))))
  6866. ;; With a prefix argument, also insert time.
  6867. (should
  6868. (string-match
  6869. "Te<2014-03-04 .*? 00:41>xt"
  6870. (org-test-with-temp-text "Te<point>xt"
  6871. (cl-letf (((symbol-function 'org-read-date)
  6872. (lambda (&rest args)
  6873. (apply #'encode-time
  6874. (org-parse-time-string "2014-03-04 00:41")))))
  6875. (org-time-stamp '(4))
  6876. (buffer-string)))))
  6877. ;; With two universal prefix arguments, insert an active timestamp
  6878. ;; with the current time without prompting the user.
  6879. (should
  6880. (string-match
  6881. "Te<2014-03-04 .*? 00:41>xt"
  6882. (org-test-with-temp-text "Te<point>xt"
  6883. (org-test-at-time "2014-03-04 00:41"
  6884. (org-time-stamp '(16))
  6885. (buffer-string)))))
  6886. ;; When optional argument is non-nil, insert an inactive timestamp.
  6887. (should
  6888. (string-match
  6889. "Te\\[2014-03-04 .*?\\]xt"
  6890. (org-test-with-temp-text "Te<point>xt"
  6891. (cl-letf (((symbol-function 'org-read-date)
  6892. (lambda (&rest args)
  6893. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6894. (org-time-stamp nil t)
  6895. (buffer-string)))))
  6896. ;; When called from a timestamp, replace existing one.
  6897. (should
  6898. (string-match
  6899. "<2014-03-04 .*?>"
  6900. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  6901. (cl-letf (((symbol-function 'org-read-date)
  6902. (lambda (&rest args)
  6903. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6904. (org-time-stamp nil)
  6905. (buffer-string)))))
  6906. (should
  6907. (string-match
  6908. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  6909. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  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. ;; When replacing a timestamp, preserve repeater, if any.
  6916. (should
  6917. (string-match
  6918. "<2014-03-04 .*? \\+2y>"
  6919. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  6920. (cl-letf (((symbol-function 'org-read-date)
  6921. (lambda (&rest args)
  6922. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6923. (org-time-stamp nil)
  6924. (buffer-string)))))
  6925. ;; When called twice in a raw, build a date range.
  6926. (should
  6927. (string-match
  6928. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  6929. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  6930. (cl-letf (((symbol-function 'org-read-date)
  6931. (lambda (&rest args)
  6932. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  6933. (let ((last-command 'org-time-stamp)
  6934. (this-command 'org-time-stamp))
  6935. (org-time-stamp nil))
  6936. (buffer-string))))))
  6937. (ert-deftest test-org/timestamp-has-time-p ()
  6938. "Test `org-timestamp-has-time-p' specifications."
  6939. ;; With time.
  6940. (should
  6941. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6942. (org-timestamp-has-time-p (org-element-context))))
  6943. ;; Without time.
  6944. (should-not
  6945. (org-test-with-temp-text "<2012-03-29 Thu>"
  6946. (org-timestamp-has-time-p (org-element-context)))))
  6947. (ert-deftest test-org/get-repeat ()
  6948. "Test `org-get-repeat' specifications."
  6949. (should
  6950. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  6951. (org-get-repeat)))
  6952. (should-not
  6953. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  6954. (org-get-repeat)))
  6955. ;; Return proper repeat string.
  6956. (should
  6957. (equal "+2y"
  6958. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  6959. (org-get-repeat))))
  6960. ;; Prevent false positive (commented or verbatim time stamps)
  6961. (should-not
  6962. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  6963. (org-get-repeat)))
  6964. (should-not
  6965. (org-test-with-temp-text
  6966. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  6967. (org-get-repeat)))
  6968. ;; Return nil when called before first heading.
  6969. (should-not
  6970. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  6971. (org-get-repeat)))
  6972. ;; When called with an optional argument, extract repeater from that
  6973. ;; string instead.
  6974. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  6975. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  6976. (ert-deftest test-org/timestamp-format ()
  6977. "Test `org-timestamp-format' specifications."
  6978. ;; Regular test.
  6979. (should
  6980. (equal
  6981. "2012-03-29 16:40"
  6982. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  6983. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  6984. ;; Range end.
  6985. (should
  6986. (equal
  6987. "2012-03-29"
  6988. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  6989. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  6990. (ert-deftest test-org/timestamp-split-range ()
  6991. "Test `org-timestamp-split-range' specifications."
  6992. ;; Extract range start (active).
  6993. (should
  6994. (equal '(2012 3 29)
  6995. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  6996. (let ((ts (org-timestamp-split-range (org-element-context))))
  6997. (mapcar (lambda (p) (org-element-property p ts))
  6998. '(:year-end :month-end :day-end))))))
  6999. ;; Extract range start (inactive)
  7000. (should
  7001. (equal '(2012 3 29)
  7002. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  7003. (let ((ts (org-timestamp-split-range (org-element-context))))
  7004. (mapcar (lambda (p) (org-element-property p ts))
  7005. '(:year-end :month-end :day-end))))))
  7006. ;; Extract range end (active).
  7007. (should
  7008. (equal '(2012 3 30)
  7009. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7010. (let ((ts (org-timestamp-split-range
  7011. (org-element-context) t)))
  7012. (mapcar (lambda (p) (org-element-property p ts))
  7013. '(:year-end :month-end :day-end))))))
  7014. ;; Extract range end (inactive)
  7015. (should
  7016. (equal '(2012 3 30)
  7017. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  7018. (let ((ts (org-timestamp-split-range
  7019. (org-element-context) t)))
  7020. (mapcar (lambda (p) (org-element-property p ts))
  7021. '(:year-end :month-end :day-end))))))
  7022. ;; Return the timestamp if not a range.
  7023. (should
  7024. (org-test-with-temp-text "[2012-03-29 Thu]"
  7025. (let* ((ts-orig (org-element-context))
  7026. (ts-copy (org-timestamp-split-range ts-orig)))
  7027. (eq ts-orig ts-copy))))
  7028. (should
  7029. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  7030. (let* ((ts-orig (org-element-context))
  7031. (ts-copy (org-timestamp-split-range ts-orig)))
  7032. (eq ts-orig ts-copy)))))
  7033. (ert-deftest test-org/timestamp-translate ()
  7034. "Test `org-timestamp-translate' specifications."
  7035. ;; Translate whole date range.
  7036. (should
  7037. (equal "<29>--<30>"
  7038. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7039. (let ((org-display-custom-times t)
  7040. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7041. (org-timestamp-translate (org-element-context))))))
  7042. ;; Translate date range start.
  7043. (should
  7044. (equal "<29>"
  7045. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7046. (let ((org-display-custom-times t)
  7047. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7048. (org-timestamp-translate (org-element-context) 'start)))))
  7049. ;; Translate date range end.
  7050. (should
  7051. (equal "<30>"
  7052. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  7053. (let ((org-display-custom-times t)
  7054. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7055. (org-timestamp-translate (org-element-context) 'end)))))
  7056. ;; Translate time range.
  7057. (should
  7058. (equal "<08>--<16>"
  7059. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  7060. (let ((org-display-custom-times t)
  7061. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  7062. (org-timestamp-translate (org-element-context))))))
  7063. ;; Translate non-range timestamp.
  7064. (should
  7065. (equal "<29>"
  7066. (org-test-with-temp-text "<2012-03-29 Thu>"
  7067. (let ((org-display-custom-times t)
  7068. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7069. (org-timestamp-translate (org-element-context))))))
  7070. ;; Do not change `diary' timestamps.
  7071. (should
  7072. (equal "<%%(org-float t 4 2)>"
  7073. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  7074. (let ((org-display-custom-times t)
  7075. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  7076. (org-timestamp-translate (org-element-context)))))))
  7077. (ert-deftest test-org/timestamp-from-string ()
  7078. "Test `org-timestamp-from-string' specifications."
  7079. ;; Return nil if argument is not a valid Org timestamp.
  7080. (should-not (org-timestamp-from-string ""))
  7081. (should-not (org-timestamp-from-string nil))
  7082. (should-not (org-timestamp-from-string "<2012-03-29"))
  7083. ;; Otherwise, return a valid Org timestamp object.
  7084. (should
  7085. (string-match-p "<2012-03-29 .+>"
  7086. (org-element-interpret-data
  7087. (org-timestamp-from-string "<2012-03-29 Thu>"))))
  7088. (should
  7089. (string-match-p "[2014-03-04 .+]"
  7090. (org-element-interpret-data
  7091. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  7092. (ert-deftest test-org/timestamp-from-time ()
  7093. "Test `org-timestamp-from-time' specifications."
  7094. ;; Standard test.
  7095. (should
  7096. (string-match-p
  7097. "<2012-03-29 .+>"
  7098. (org-element-interpret-data
  7099. (org-timestamp-from-time
  7100. (apply #'encode-time
  7101. (org-parse-time-string "<2012-03-29 Thu 16:40>"))))))
  7102. ;; When optional argument WITH-TIME is non-nil, provide time
  7103. ;; information.
  7104. (should
  7105. (string-match-p
  7106. "<2012-03-29 .+ 16:40>"
  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. t))))
  7112. ;; When optional argument INACTIVE is non-nil, return an inactive
  7113. ;; timestamp.
  7114. (should
  7115. (string-match-p
  7116. "[2012-03-29 .+]"
  7117. (org-element-interpret-data
  7118. (org-timestamp-from-time
  7119. (apply #'encode-time
  7120. (org-parse-time-string "<2012-03-29 Thu 16:40>"))
  7121. nil t)))))
  7122. (ert-deftest test-org/timestamp-to-time ()
  7123. "Test `org-timestamp-to-time' specifications."
  7124. (should
  7125. (equal "2014-03-04"
  7126. (format-time-string
  7127. "%Y-%m-%d"
  7128. (org-timestamp-to-time
  7129. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  7130. (should
  7131. (equal "2014-03-04"
  7132. (format-time-string
  7133. "%Y-%m-%d"
  7134. (org-timestamp-to-time
  7135. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  7136. (should
  7137. (equal "2012-03-29 08:30"
  7138. (format-time-string
  7139. "%Y-%m-%d %H:%M"
  7140. (org-timestamp-to-time
  7141. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  7142. (should
  7143. (equal "2012-03-29"
  7144. (format-time-string
  7145. "%Y-%m-%d"
  7146. (org-timestamp-to-time
  7147. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  7148. (should
  7149. (equal "2012-03-29"
  7150. (format-time-string
  7151. "%Y-%m-%d"
  7152. (org-timestamp-to-time
  7153. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  7154. ;; When optional argument END is non-nil, use end of date range or
  7155. ;; time range.
  7156. (should
  7157. (equal "2012-03-29 16:40"
  7158. (format-time-string
  7159. "%Y-%m-%d %H:%M"
  7160. (org-timestamp-to-time
  7161. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  7162. t))))
  7163. (should
  7164. (equal "2014-03-04"
  7165. (format-time-string
  7166. "%Y-%m-%d"
  7167. (org-timestamp-to-time
  7168. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  7169. t))))
  7170. (should
  7171. (equal "2014-03-04"
  7172. (format-time-string
  7173. "%Y-%m-%d"
  7174. (org-timestamp-to-time
  7175. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  7176. t)))))
  7177. ;;; Visibility
  7178. (ert-deftest test-org/hide-drawer-toggle ()
  7179. "Test `org-hide-drawer-toggle' specifications."
  7180. ;; Error when not at a drawer.
  7181. (should-error
  7182. (org-test-with-temp-text ":fake-drawer:\ncontents"
  7183. (org-hide-drawer-toggle 'off)
  7184. (get-char-property (line-end-position) 'invisible)))
  7185. (should-error
  7186. (org-test-with-temp-text
  7187. "#+begin_example\n<point>:D:\nc\n:END:\n#+end_example"
  7188. (org-hide-drawer-toggle t)))
  7189. ;; Hide drawer.
  7190. (should
  7191. (org-test-with-temp-text ":drawer:\ncontents\n:end:"
  7192. (org-hide-drawer-toggle)
  7193. (get-char-property (line-end-position) 'invisible)))
  7194. ;; Show drawer unconditionally when optional argument is `off'.
  7195. (should-not
  7196. (org-test-with-temp-text ":drawer:\ncontents\n:end:"
  7197. (org-hide-drawer-toggle)
  7198. (org-hide-drawer-toggle 'off)
  7199. (get-char-property (line-end-position) 'invisible)))
  7200. ;; Hide drawer unconditionally when optional argument is non-nil.
  7201. (should
  7202. (org-test-with-temp-text ":drawer:\ncontents\n:end:"
  7203. (org-hide-drawer-toggle t)
  7204. (get-char-property (line-end-position) 'invisible)))
  7205. ;; Do not hide drawer when called from final blank lines.
  7206. (should-not
  7207. (org-test-with-temp-text ":drawer:\ncontents\n:end:\n\n<point>"
  7208. (org-hide-drawer-toggle)
  7209. (goto-char (point-min))
  7210. (get-char-property (line-end-position) 'invisible)))
  7211. ;; Don't leave point in an invisible part of the buffer when hiding
  7212. ;; a drawer away.
  7213. (should-not
  7214. (org-test-with-temp-text ":drawer:\ncontents\n<point>:end:"
  7215. (org-hide-drawer-toggle)
  7216. (get-char-property (point) 'invisible))))
  7217. (ert-deftest test-org/hide-block-toggle ()
  7218. "Test `org-hide-block-toggle' specifications."
  7219. ;; Error when not at a block.
  7220. (should-error
  7221. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  7222. (org-hide-block-toggle 'off)
  7223. (get-char-property (line-end-position) 'invisible)))
  7224. ;; Hide block.
  7225. (should
  7226. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  7227. (org-hide-block-toggle)
  7228. (get-char-property (line-end-position) 'invisible)))
  7229. (should
  7230. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  7231. (org-hide-block-toggle)
  7232. (get-char-property (line-end-position) 'invisible)))
  7233. ;; Show block unconditionally when optional argument is `off'.
  7234. (should-not
  7235. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7236. (org-hide-block-toggle)
  7237. (org-hide-block-toggle 'off)
  7238. (get-char-property (line-end-position) 'invisible)))
  7239. (should-not
  7240. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7241. (org-hide-block-toggle 'off)
  7242. (get-char-property (line-end-position) 'invisible)))
  7243. ;; Hide block unconditionally when optional argument is non-nil.
  7244. (should
  7245. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7246. (org-hide-block-toggle t)
  7247. (get-char-property (line-end-position) 'invisible)))
  7248. (should
  7249. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  7250. (org-hide-block-toggle)
  7251. (org-hide-block-toggle t)
  7252. (get-char-property (line-end-position) 'invisible)))
  7253. ;; Do not hide block when called from final blank lines.
  7254. (should-not
  7255. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  7256. (org-hide-block-toggle)
  7257. (goto-char (point-min))
  7258. (get-char-property (line-end-position) 'invisible)))
  7259. ;; Don't leave point in an invisible part of the buffer when hiding
  7260. ;; a block away.
  7261. (should-not
  7262. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  7263. (org-hide-block-toggle)
  7264. (get-char-property (point) 'invisible))))
  7265. (ert-deftest test-org/hide-block-toggle-maybe ()
  7266. "Test `org-hide-block-toggle-maybe' specifications."
  7267. (should
  7268. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  7269. (org-hide-block-toggle-maybe)))
  7270. (should-not
  7271. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  7272. (ert-deftest test-org/show-set-visibility ()
  7273. "Test `org-show-set-visibility' specifications."
  7274. ;; Do not throw an error before first heading.
  7275. (should
  7276. (org-test-with-temp-text "Preamble\n* Headline"
  7277. (org-show-set-visibility 'tree)
  7278. t))
  7279. ;; Test all visibility spans, both on headline and in entry.
  7280. (let ((list-visible-lines
  7281. (lambda (state headerp)
  7282. (org-test-with-temp-text "* Grandmother (0)
  7283. ** Uncle (1)
  7284. *** Heir (2)
  7285. ** Father (3)
  7286. Ancestor text (4)
  7287. *** Sister (5)
  7288. Sibling text (6)
  7289. *** Self (7)
  7290. Match (8)
  7291. **** First born (9)
  7292. Child text (10)
  7293. **** The other child (11)
  7294. *** Brother (12)
  7295. ** Aunt (13)
  7296. "
  7297. (org-cycle t)
  7298. (search-forward (if headerp "Self" "Match"))
  7299. (org-show-set-visibility state)
  7300. (goto-char (point-min))
  7301. (let (result (line 0))
  7302. (while (not (eobp))
  7303. (unless (org-invisible-p2) (push line result))
  7304. (cl-incf line)
  7305. (forward-line))
  7306. (nreverse result))))))
  7307. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  7308. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  7309. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  7310. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  7311. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  7312. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  7313. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  7314. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  7315. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  7316. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  7317. (funcall list-visible-lines 'tree nil)))
  7318. (should (equal '(0 1 3 4 5 7 12 13)
  7319. (funcall list-visible-lines 'canonical t)))
  7320. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  7321. (funcall list-visible-lines 'canonical nil))))
  7322. ;; When point is hidden in a drawer or a block, make sure to make it
  7323. ;; visible.
  7324. (should-not
  7325. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  7326. (org-hide-block-toggle)
  7327. (search-forward "Text")
  7328. (org-show-set-visibility 'minimal)
  7329. (org-invisible-p2)))
  7330. (should-not
  7331. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  7332. (org-hide-drawer-toggle)
  7333. (search-forward "Text")
  7334. (org-show-set-visibility 'minimal)
  7335. (org-invisible-p2)))
  7336. (should-not
  7337. (org-test-with-temp-text
  7338. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  7339. (org-hide-drawer-toggle)
  7340. (forward-line -1)
  7341. (org-hide-block-toggle)
  7342. (search-forward "Text")
  7343. (org-show-set-visibility 'minimal)
  7344. (org-invisible-p2))))
  7345. (defun test-org/copy-visible ()
  7346. "Test `org-copy-visible' specifications."
  7347. (should
  7348. (equal "Foo"
  7349. (org-test-with-temp-text "Foo"
  7350. (let ((kill-ring nil))
  7351. (org-copy-visible (point-min) (point-max))
  7352. (current-kill 0 t)))))
  7353. ;; Skip invisible characters by text property.
  7354. (should
  7355. (equal "Foo"
  7356. (org-test-with-temp-text #("F<hidden>oo" 1 7 (invisible t))
  7357. (let ((kill-ring nil))
  7358. (org-copy-visible (point-min) (point-max))
  7359. (current-kill 0 t)))))
  7360. ;; Skip invisible characters by overlay.
  7361. (should
  7362. (equal "Foo"
  7363. (org-test-with-temp-text "F<hidden>oo"
  7364. (let ((o (make-overlay 2 10)))
  7365. (overlay-put o 'invisible t))
  7366. (let ((kill-ring nil))
  7367. (org-copy-visible (point-min) (point-max))
  7368. (current-kill 0 t)))))
  7369. ;; Handle invisible characters at the beginning and the end of the
  7370. ;; buffer.
  7371. (should
  7372. (equal "Foo"
  7373. (org-test-with-temp-text #("<hidden>Foo" 0 8 (invisible t))
  7374. (let ((kill-ring nil))
  7375. (org-copy-visible (point-min) (point-max))
  7376. (current-kill 0 t)))))
  7377. (should
  7378. (equal "Foo"
  7379. (org-test-with-temp-text #("Foo<hidden>" 3 11 (invisible t))
  7380. (let ((kill-ring nil))
  7381. (org-copy-visible (point-min) (point-max))
  7382. (current-kill 0 t)))))
  7383. ;; Handle multiple visible parts.
  7384. (should
  7385. (equal "abc"
  7386. (org-test-with-temp-text
  7387. #("aXbXc" 1 2 (invisible t) 3 4 (invisible t))
  7388. (let ((kill-ring nil))
  7389. (org-copy-visible (point-min) (point-max))
  7390. (current-kill 0 t))))))
  7391. (ert-deftest test-org/set-visibility-according-to-property ()
  7392. "Test `org-set-visibility-according-to-property' specifications."
  7393. ;; "folded" state.
  7394. (should
  7395. (org-test-with-temp-text
  7396. "
  7397. * a
  7398. :PROPERTIES:
  7399. :VISIBILITY: folded
  7400. :END:
  7401. ** <point>b"
  7402. (org-set-visibility-according-to-property)
  7403. (invisible-p (point))))
  7404. ;; "children" state.
  7405. (should
  7406. (org-test-with-temp-text
  7407. "
  7408. * a
  7409. :PROPERTIES:
  7410. :VISIBILITY: children
  7411. :END:
  7412. ** b
  7413. <point>Contents
  7414. ** c"
  7415. (org-set-visibility-according-to-property)
  7416. (invisible-p (point))))
  7417. (should
  7418. (org-test-with-temp-text
  7419. "
  7420. * a
  7421. :PROPERTIES:
  7422. :VISIBILITY: children
  7423. :END:
  7424. ** b
  7425. Contents
  7426. *** <point>c"
  7427. (org-set-visibility-according-to-property)
  7428. (invisible-p (point))))
  7429. ;; "content" state.
  7430. (should
  7431. (org-test-with-temp-text
  7432. "
  7433. * a
  7434. :PROPERTIES:
  7435. :VISIBILITY: content
  7436. :END:
  7437. ** b
  7438. <point>Contents
  7439. *** c"
  7440. (org-set-visibility-according-to-property)
  7441. (invisible-p (point))))
  7442. (should
  7443. (org-test-with-temp-text
  7444. "
  7445. * a
  7446. :PROPERTIES:
  7447. :VISIBILITY: content
  7448. :END:
  7449. ** b
  7450. Contents
  7451. *** <point>c"
  7452. (org-set-visibility-according-to-property)
  7453. (not (invisible-p (point)))))
  7454. ;; "showall" state.
  7455. (should
  7456. (org-test-with-temp-text
  7457. "
  7458. * a
  7459. :PROPERTIES:
  7460. :VISIBILITY: showall
  7461. :END:
  7462. ** b
  7463. <point>Contents
  7464. *** c"
  7465. (org-set-visibility-according-to-property)
  7466. (not (invisible-p (point)))))
  7467. (should
  7468. (org-test-with-temp-text
  7469. "
  7470. * a
  7471. :PROPERTIES:
  7472. :VISIBILITY: showall
  7473. :END:
  7474. ** b
  7475. Contents
  7476. *** <point>c"
  7477. (org-set-visibility-according-to-property)
  7478. (not (invisible-p (point)))))
  7479. ;; When VISIBILITY properties are nested, ignore inner ones.
  7480. (should
  7481. (org-test-with-temp-text
  7482. "
  7483. * A
  7484. :PROPERTIES:
  7485. :VISIBILITY: folded
  7486. :END:
  7487. ** <point>B
  7488. :PROPERTIES:
  7489. :VISIBILITY: folded
  7490. :END:"
  7491. (org-set-visibility-according-to-property)
  7492. (invisible-p (point)))))
  7493. (ert-deftest test-org/visibility-show-branches ()
  7494. "Test visibility of inline archived subtrees."
  7495. (org-test-with-temp-text
  7496. "* Foo<point>
  7497. ** Bar :ARCHIVE:
  7498. *** Baz
  7499. "
  7500. (org-kill-note-or-show-branches)
  7501. (should (org-invisible-p (- (point-max) 2)))))
  7502. ;;; Yank and Kill
  7503. (ert-deftest test-org/paste-subtree ()
  7504. "Test `org-paste-subtree' specifications."
  7505. ;; Return an error if text to yank is not a set of subtrees.
  7506. (should-error (org-paste-subtree nil "Text"))
  7507. ;; Adjust level according to current one.
  7508. (should
  7509. (equal "* H\n* Text\n"
  7510. (org-test-with-temp-text "* H\n<point>"
  7511. (org-paste-subtree nil "* Text")
  7512. (buffer-string))))
  7513. (should
  7514. (equal "* H1\n** H2\n** Text\n"
  7515. (org-test-with-temp-text "* H1\n** H2\n<point>"
  7516. (org-paste-subtree nil "* Text")
  7517. (buffer-string))))
  7518. ;; When not on a heading, move to next heading before yanking.
  7519. (should
  7520. (equal "* H1\nParagraph\n* Text\n* H2"
  7521. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  7522. (org-paste-subtree nil "* Text")
  7523. (buffer-string))))
  7524. ;; If point is between two headings, use the deepest level.
  7525. (should
  7526. (equal "* H1\n\n* Text\n* H2"
  7527. (org-test-with-temp-text "* H1\n<point>\n* H2"
  7528. (org-paste-subtree nil "* Text")
  7529. (buffer-string))))
  7530. (should
  7531. (equal "** H1\n\n** Text\n* H2"
  7532. (org-test-with-temp-text "** H1\n<point>\n* H2"
  7533. (org-paste-subtree nil "* Text")
  7534. (buffer-string))))
  7535. (should
  7536. (equal "* H1\n\n** Text\n** H2"
  7537. (org-test-with-temp-text "* H1\n<point>\n** H2"
  7538. (org-paste-subtree nil "* Text")
  7539. (buffer-string))))
  7540. ;; When on an empty heading, after the stars, deduce the new level
  7541. ;; from the number of stars.
  7542. (should
  7543. (equal "*** Text\n"
  7544. (org-test-with-temp-text "*** <point>"
  7545. (org-paste-subtree nil "* Text")
  7546. (buffer-string))))
  7547. ;; Optional argument LEVEL forces a level for the subtree.
  7548. (should
  7549. (equal "* H\n*** Text\n"
  7550. (org-test-with-temp-text "* H<point>"
  7551. (org-paste-subtree 3 "* Text")
  7552. (buffer-string)))))
  7553. (ert-deftest test-org/cut-and-paste-subtree ()
  7554. "Test `org-cut-subtree' and `org-paste-subtree'."
  7555. (should
  7556. (equal
  7557. "* Two
  7558. two
  7559. * One
  7560. "
  7561. (org-test-with-temp-text
  7562. "* One
  7563. <point>* Two
  7564. two
  7565. "
  7566. (call-interactively #'org-cut-subtree)
  7567. (goto-char (point-min))
  7568. (call-interactively #'org-paste-subtree)
  7569. (buffer-string))))
  7570. (should
  7571. (equal
  7572. "* One
  7573. * Two
  7574. "
  7575. (org-test-with-temp-text
  7576. "* One
  7577. <point>* Two
  7578. "
  7579. (call-interactively #'org-cut-subtree)
  7580. (backward-char)
  7581. (call-interactively #'org-paste-subtree)
  7582. (buffer-string)))))
  7583. (provide 'test-org)
  7584. ;;; test-org.el ends here