test-org.el 285 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679
  1. ;;; test-org.el --- tests for org.el -*- lexical-binding: t -*-
  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 <https://www.gnu.org/licenses/>.
  15. ;; Template test file for Org tests
  16. ;;; Code:
  17. (eval-and-compile (require 'cl-lib))
  18. (eval-when-compile (require 'org-macs)) ;For `org-with-gensyms'.
  19. (require 'org)
  20. (require 'org-inlinetask)
  21. (require 'org-refile)
  22. (require 'org-agenda)
  23. ;;; Helpers
  24. (defmacro org-test-with-timezone (tz &rest body)
  25. "Evaluate BODY with TZ environment temporary set to the passed value."
  26. (declare (indent 1))
  27. (org-with-gensyms (tz-saved)
  28. `(let ((,tz-saved (getenv "TZ")))
  29. (unwind-protect
  30. (progn
  31. (setenv "TZ" ,tz)
  32. ,@body)
  33. (setenv "TZ" ,tz-saved)))))
  34. ;;; Comments
  35. (ert-deftest test-org/toggle-comment ()
  36. "Test `org-toggle-comment' specifications."
  37. ;; Simple headline.
  38. (should
  39. (equal "* Test"
  40. (org-test-with-temp-text "* COMMENT Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. (should
  44. (equal "* COMMENT Test"
  45. (org-test-with-temp-text "* Test"
  46. (org-toggle-comment)
  47. (buffer-string))))
  48. ;; Headline with a regular keyword.
  49. (should
  50. (equal "* TODO Test"
  51. (org-test-with-temp-text "* TODO COMMENT Test"
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. (should
  55. (equal "* TODO COMMENT Test"
  56. (org-test-with-temp-text "* TODO Test"
  57. (org-toggle-comment)
  58. (buffer-string))))
  59. ;; Empty headline.
  60. (should
  61. (equal "* "
  62. (org-test-with-temp-text "* COMMENT"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. (should
  66. (equal "* COMMENT"
  67. (org-test-with-temp-text "* "
  68. (org-toggle-comment)
  69. (buffer-string))))
  70. ;; Headline with a single keyword.
  71. (should
  72. (equal "* TODO "
  73. (org-test-with-temp-text "* TODO COMMENT"
  74. (org-toggle-comment)
  75. (buffer-string))))
  76. (should
  77. (equal "* TODO COMMENT"
  78. (org-test-with-temp-text "* TODO"
  79. (org-toggle-comment)
  80. (buffer-string))))
  81. ;; Headline with a keyword, a priority cookie and contents.
  82. (should
  83. (equal "* TODO [#A] Headline"
  84. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  85. (org-toggle-comment)
  86. (buffer-string))))
  87. (should
  88. (equal "* TODO [#A] COMMENT Headline"
  89. (org-test-with-temp-text "* TODO [#A] Headline"
  90. (org-toggle-comment)
  91. (buffer-string)))))
  92. (ert-deftest test-org/comment-dwim ()
  93. "Test `comment-dwim' behaviour in an Org buffer."
  94. ;; No region selected, no comment on current line and line not
  95. ;; empty: insert comment on line above.
  96. (should
  97. (equal "# \nComment"
  98. (org-test-with-temp-text "Comment"
  99. (call-interactively #'org-comment-dwim)
  100. (buffer-string))))
  101. ;; No region selected, no comment on current line and line empty:
  102. ;; insert comment on this line.
  103. (should
  104. (equal "# \nParagraph"
  105. (org-test-with-temp-text "\nParagraph"
  106. (call-interactively #'org-comment-dwim)
  107. (buffer-string))))
  108. ;; No region selected, and a comment on this line: indent it.
  109. (should
  110. (equal "* Headline\n # Comment"
  111. (org-test-with-temp-text "* Headline\n# <point>Comment"
  112. (let ((org-adapt-indentation t))
  113. (call-interactively #'org-comment-dwim))
  114. (buffer-string))))
  115. ;; Also recognize single # at column 0 as comments.
  116. (should
  117. (equal "# Comment"
  118. (org-test-with-temp-text "# Comment"
  119. (call-interactively #'org-comment-dwim)
  120. (buffer-string))))
  121. ;; Region selected and only comments and blank lines within it:
  122. ;; un-comment all commented lines.
  123. (should
  124. (equal "Comment 1\n\nComment 2"
  125. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  126. (transient-mark-mode 1)
  127. (push-mark (point) t t)
  128. (goto-char (point-max))
  129. (call-interactively #'org-comment-dwim)
  130. (buffer-string))))
  131. ;; Region selected without comments: comment all lines if
  132. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  133. (should
  134. (equal "# Comment 1\n\n# Comment 2"
  135. (org-test-with-temp-text "Comment 1\n\nComment 2"
  136. (transient-mark-mode 1)
  137. (push-mark (point) t t)
  138. (goto-char (point-max))
  139. (let ((comment-empty-lines nil))
  140. (call-interactively #'org-comment-dwim))
  141. (buffer-string))))
  142. (should
  143. (equal "# Comment 1\n# \n# Comment 2"
  144. (org-test-with-temp-text "Comment 1\n\nComment 2"
  145. (transient-mark-mode 1)
  146. (push-mark (point) t t)
  147. (goto-char (point-max))
  148. (let ((comment-empty-lines t))
  149. (call-interactively #'org-comment-dwim))
  150. (buffer-string))))
  151. ;; In front of a keyword without region, insert a new comment.
  152. (should
  153. (equal "# \n#+KEYWORD: value"
  154. (org-test-with-temp-text "#+KEYWORD: value"
  155. (call-interactively #'org-comment-dwim)
  156. (buffer-string))))
  157. ;; Comment a heading
  158. (should
  159. (equal "* COMMENT Test"
  160. (org-test-with-temp-text "* Test"
  161. (call-interactively #'org-comment-dwim)
  162. (buffer-string))))
  163. ;; In a source block, use appropriate syntax.
  164. (should
  165. (equal " ;; "
  166. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
  167. (let ((org-edit-src-content-indentation 2))
  168. (call-interactively #'org-comment-dwim))
  169. (buffer-substring-no-properties (line-beginning-position)
  170. (point)))))
  171. (should
  172. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  173. (org-test-with-temp-text
  174. "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
  175. (transient-mark-mode 1)
  176. (push-mark (point) t t)
  177. (forward-line 2)
  178. (let ((org-edit-src-content-indentation 2))
  179. (call-interactively #'org-comment-dwim))
  180. (buffer-string)))))
  181. ;;; Date and time analysis
  182. (ert-deftest test-org/org-encode-time ()
  183. "Test various ways to call `org-encode-time'"
  184. (org-test-with-timezone "UTC"
  185. ;; list as the sole argument
  186. (should (string-equal
  187. "2022-03-24 23:30:01"
  188. (format-time-string
  189. "%F %T"
  190. (org-encode-time '(1 30 23 24 3 2022 nil -1 nil)))))
  191. ;; SECOND...YEAR
  192. (should (string-equal
  193. "2022-03-24 23:30:02"
  194. (format-time-string
  195. "%F %T"
  196. (org-encode-time 2 30 23 24 3 2022))))
  197. ;; SECOND...YEAR IGNORED DST ZONE
  198. (should (string-equal
  199. "2022-03-24 23:30:03"
  200. (format-time-string
  201. "%F %T"
  202. (org-encode-time 3 30 23 24 3 2022 nil -1 nil))))
  203. ;; function call
  204. (should (string-equal
  205. "2022-03-24 23:30:04"
  206. (format-time-string
  207. "%F %T"
  208. (org-encode-time (apply #'list 4 30 23 '(24 3 2022 nil -1 nil))))))
  209. ;; wrong number of arguments
  210. (if (not (version< emacs-version "27.1"))
  211. (should-error (string-equal
  212. "2022-03-24 23:30:05"
  213. (format-time-string
  214. "%F %T"
  215. (org-encode-time 5 30 23 24 3 2022 nil))))))
  216. ;; daylight saving time
  217. (if (not (version< emacs-version "27.1"))
  218. ;; DST value is not ignored for multiple arguments unlike for `encode-time'
  219. (should (string-equal
  220. "2022-04-01 00:30:06 +0200 CEST"
  221. (format-time-string
  222. "%F %T %z %Z"
  223. (org-encode-time 6 30 23 31 3 2022 nil nil "Europe/Madrid")
  224. "Europe/Madrid")))
  225. (should (string-equal
  226. "2022-03-31 23:30:07 +0200 CEST"
  227. (format-time-string
  228. "%F %T %z %Z"
  229. (org-encode-time 7 30 23 31 3 2022 nil t "Europe/Madrid")
  230. "Europe/Madrid"))))
  231. (org-test-with-timezone "Europe/Madrid"
  232. ;; Standard time is not forced when DST is not specified
  233. (should (string-equal
  234. "2022-03-31 23:30:08"
  235. (format-time-string
  236. "%F %T"
  237. (org-encode-time 8 30 23 31 3 2022))))))
  238. (ert-deftest test-org/org-time-string-to-time ()
  239. "Test `org-time-string-to-time' around DST transition."
  240. (org-test-with-timezone "UTC"
  241. (should (string-equal
  242. "2022-03-31 23:31:00"
  243. (format-time-string
  244. "%F %T"
  245. (org-time-string-to-time "2022-03-31 23:31")))))
  246. (org-test-with-timezone "Europe/Madrid"
  247. (should (string-equal
  248. "2022-03-24 23:32:00 +0100 CET"
  249. (format-time-string
  250. "%F %T %z %Z"
  251. (org-time-string-to-time "2022-03-24 23:32"))))
  252. (should (string-equal
  253. "2022-03-31 23:33:00 +0200 CEST"
  254. (format-time-string
  255. "%F %T %z %Z"
  256. (org-time-string-to-time "2022-03-31 23:33"))))))
  257. (ert-deftest test-org/org-read-date ()
  258. "Test `org-read-date' specifications."
  259. (defvar org-time-was-given) ; dynamically scoped parameter
  260. ;; Parse ISO date with abbreviated year and month.
  261. (should (equal "2012-03-29 16:40"
  262. (let ((org-time-was-given t))
  263. (org-read-date t nil "12-3-29 16:40"))))
  264. ;; Parse Europeans dates.
  265. (should (equal "2012-03-29 16:40"
  266. (let ((org-time-was-given t))
  267. (org-read-date t nil "29.03.2012 16:40"))))
  268. ;; Parse Europeans dates without year.
  269. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  270. (let ((org-time-was-given t))
  271. (org-read-date t nil "29.03. 16:40"))))
  272. ;; Relative date applied to current time if there is single
  273. ;; plus/minus, or to default date when there are two of them.
  274. (should
  275. (equal
  276. "2015-03-04"
  277. (org-test-at-time "2014-03-04"
  278. (org-read-date
  279. t nil "+1y" nil
  280. (org-time-string-to-time "2012-03-29")))))
  281. (should
  282. (equal
  283. "2013-03-29"
  284. (org-test-at-time "2014-03-04"
  285. (org-read-date
  286. t nil "++1y" nil
  287. (org-time-string-to-time "2012-03-29")))))
  288. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  289. ;; dates (relatively to now) when incomplete. Otherwise, use
  290. ;; default date.
  291. (should
  292. (equal
  293. "2014-04-01"
  294. (org-test-at-time "2014-03-04"
  295. (let ((org-read-date-prefer-future t))
  296. (org-read-date t nil "1")))))
  297. (should
  298. (equal
  299. "2013-03-04"
  300. (org-test-at-time "2012-03-29"
  301. (let ((org-read-date-prefer-future t))
  302. (org-read-date t nil "3-4")))))
  303. (should
  304. (equal
  305. "2012-03-04"
  306. (org-test-at-time "2012-03-29"
  307. (let ((org-read-date-prefer-future nil))
  308. (org-read-date t nil "3-4")))))
  309. ;; When set to `org-read-date-prefer-future' is set to `time', read
  310. ;; day is moved to tomorrow if specified hour is before current
  311. ;; time. However, it only happens in no other part of the date is
  312. ;; specified.
  313. (should
  314. (equal
  315. "2012-03-30"
  316. (org-test-at-time "2012-03-29 16:40"
  317. (let ((org-read-date-prefer-future 'time))
  318. (org-read-date t nil "00:40" nil)))))
  319. (should-not
  320. (equal
  321. "2012-03-30"
  322. (org-test-at-time "2012-03-29 16:40"
  323. (let ((org-read-date-prefer-future 'time))
  324. (org-read-date t nil "29 00:40" nil)))))
  325. ;; Caveat: `org-read-date-prefer-future' always refers to current
  326. ;; time, not default time, when they differ.
  327. (should
  328. (equal
  329. "2014-04-01"
  330. (org-test-at-time "2014-03-04"
  331. (let ((org-read-date-prefer-future t))
  332. (org-read-date
  333. t nil "1" nil
  334. (org-time-string-to-time "2012-03-29"))))))
  335. (should
  336. (equal
  337. "2014-03-25"
  338. (org-test-at-time "2014-03-04"
  339. (let ((org-read-date-prefer-future t))
  340. (org-read-date
  341. t nil "25" nil
  342. (org-time-string-to-time "2012-03-29")))))))
  343. (ert-deftest test-org/org-parse-time-string ()
  344. "Test `org-parse-time-string'."
  345. (should (equal (org-parse-time-string "2012-03-29 16:40")
  346. '(0 40 16 29 3 2012 nil -1 nil)))
  347. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  348. '(0 40 16 29 3 2012 nil -1 nil)))
  349. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  350. '(0 40 16 29 3 2012 nil -1 nil)))
  351. (should (equal (org-parse-time-string "<2012-03-29>")
  352. '(0 0 0 29 3 2012 nil -1 nil)))
  353. (should (equal (org-parse-time-string "<2012-03-29>" t)
  354. '(0 nil nil 29 3 2012 nil -1 nil))))
  355. (ert-deftest test-org/closest-date ()
  356. "Test `org-closest-date' specifications."
  357. (require 'calendar)
  358. ;; Time stamps without a repeater are returned unchanged.
  359. (should
  360. (equal
  361. '(3 29 2012)
  362. (calendar-gregorian-from-absolute
  363. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  364. ;; Time stamps with a null repeater are returned unchanged.
  365. (should
  366. (equal
  367. '(3 29 2012)
  368. (calendar-gregorian-from-absolute
  369. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  370. ;; if PREFER is set to `past' always return a date before, or equal
  371. ;; to CURRENT.
  372. (should
  373. (equal
  374. '(3 1 2014)
  375. (calendar-gregorian-from-absolute
  376. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  377. (should
  378. (equal
  379. '(3 4 2014)
  380. (calendar-gregorian-from-absolute
  381. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  382. ;; if PREFER is set to `future' always return a date before, or equal
  383. ;; to CURRENT.
  384. (should
  385. (equal
  386. '(3 29 2014)
  387. (calendar-gregorian-from-absolute
  388. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  389. (should
  390. (equal
  391. '(3 4 2014)
  392. (calendar-gregorian-from-absolute
  393. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  394. ;; If PREFER is neither `past' nor `future', select closest date.
  395. (should
  396. (equal
  397. '(3 1 2014)
  398. (calendar-gregorian-from-absolute
  399. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  400. (should
  401. (equal
  402. '(5 4 2014)
  403. (calendar-gregorian-from-absolute
  404. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  405. ;; Test "day" repeater.
  406. (should
  407. (equal '(3 8 2014)
  408. (calendar-gregorian-from-absolute
  409. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  410. (should
  411. (equal '(3 10 2014)
  412. (calendar-gregorian-from-absolute
  413. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  414. ;; Test "month" repeater.
  415. (should
  416. (equal '(1 5 2015)
  417. (calendar-gregorian-from-absolute
  418. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  419. (should
  420. (equal '(3 29 2014)
  421. (calendar-gregorian-from-absolute
  422. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  423. ;; Test "year" repeater.
  424. (should
  425. (equal '(3 5 2014)
  426. (calendar-gregorian-from-absolute
  427. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  428. (should
  429. (equal '(3 29 2014)
  430. (calendar-gregorian-from-absolute
  431. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  432. (ert-deftest test-org/deadline-close-p ()
  433. "Test `org-deadline-close-p' specifications."
  434. (org-test-at-time "2016-06-03 Fri 01:43"
  435. ;; Timestamps are close if they are within `ndays' of lead time.
  436. (org-test-with-temp-text "* Heading"
  437. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  438. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  439. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  440. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  441. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  442. ;; Read `ndays' from timestamp if argument not given.
  443. (org-test-with-temp-text "* H"
  444. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  445. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  446. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  447. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  448. ;; Prefer `ndays' argument over lead time in timestamp.
  449. (org-test-with-temp-text "* H"
  450. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  451. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  452. ;; Completed tasks are never close.
  453. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  454. (org-test-with-temp-text "* TODO Heading"
  455. (should (org-deadline-close-p "2016-06-03")))
  456. (org-test-with-temp-text "* DONE Heading"
  457. (should-not (org-deadline-close-p "2016-06-03"))))))
  458. ;;; Drawers
  459. (ert-deftest test-org/at-property-p ()
  460. "Test `org-at-property-p' specifications."
  461. (should
  462. (equal 't
  463. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:PROP: t\n:END:\n"
  464. (org-at-property-p))))
  465. (should
  466. (equal 't
  467. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n:END:\n"
  468. (org-at-property-p)))))
  469. (ert-deftest test-org/at-property-drawer-p ()
  470. "Test `org-at-property-drawer-p' specifications."
  471. (should
  472. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:PROP: t\n:END:\n"
  473. (org-at-property-drawer-p)))
  474. (should
  475. (org-test-with-temp-text ":PROPERTIES:\n:PROP: t\n:END:\n"
  476. (org-at-property-drawer-p)))
  477. ;; The function only returns t if point is at the first line of
  478. ;; a property block.
  479. (should-not
  480. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n:END:\n"
  481. (org-at-property-drawer-p)))
  482. ;; The function ignores incomplete drawers.
  483. (should-not
  484. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n"
  485. (org-at-property-drawer-p)))
  486. ;; tab separating the value.
  487. (should
  488. (org-test-with-temp-text ":PROPERTIES:\n:PROP: t\n:END:\n"
  489. (org-at-property-drawer-p))))
  490. (ert-deftest test-org/get-property-block ()
  491. "Test `org-get-property-block' specifications."
  492. (should
  493. (equal '(14 . 14)
  494. (org-test-with-temp-text ":PROPERTIES:\n:END:\n* H\n"
  495. (org-get-property-block))))
  496. (should
  497. (equal '(14 . 14)
  498. (org-test-with-temp-text ":PROPERTIES:\n:END:\n"
  499. (org-get-property-block))))
  500. ;; Comments above a document property block is ok.
  501. (should
  502. (equal '(18 . 18)
  503. (org-test-with-temp-text "# C\n:PROPERTIES:\n:END:\n"
  504. (org-get-property-block))))
  505. ;; Keywords above a document property block is ok.
  506. (should
  507. (equal '(22 . 22)
  508. (org-test-with-temp-text "# C\n# C\n:PROPERTIES:\n:END:\n"
  509. (org-get-property-block))))
  510. ;; Comments and keywords are allowed before a document property block.
  511. (should
  512. (equal '(18 . 27)
  513. (org-test-with-temp-text "# C\n:PROPERTIES:\n:KEY: V:\n:END:\n"
  514. (org-get-property-block))))
  515. ;; A document property block will not be valid if there are lines
  516. ;; with whitespace above it
  517. (should-not
  518. (org-test-with-temp-text "\n:PROPERTIES:\n:END:\n"
  519. (org-get-property-block)))
  520. (should
  521. (equal '(18 . 18)
  522. (org-test-with-temp-text "* H\n:PROPERTIES:\n:END:\n<point>"
  523. (org-get-property-block))))
  524. (should
  525. (equal "* H\n:PROPERTIES:\n:END:\n"
  526. (org-test-with-temp-text "* H"
  527. (let ((org-adapt-indentation nil))
  528. (org-get-property-block nil 'force))
  529. (buffer-string))))
  530. (should
  531. (equal ":PROPERTIES:\n:END:\n"
  532. (org-test-with-temp-text ""
  533. (org-get-property-block nil 'force)
  534. (buffer-string))))
  535. (should
  536. (equal "* H1\n :PROPERTIES:\n :END:\n* H2"
  537. (org-test-with-temp-text "* H1\n* H2"
  538. (let ((org-adapt-indentation t))
  539. (org-get-property-block nil 'force))
  540. (buffer-string)))))
  541. (ert-deftest test-org/insert-property-drawer ()
  542. "Test `org-insert-property-drawer' specifications."
  543. ;; Insert drawer in empty buffer
  544. (should
  545. (equal ":PROPERTIES:\n:END:\n"
  546. (org-test-with-temp-text ""
  547. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  548. (buffer-string))))
  549. ;; Insert drawer in document header with existing comment and
  550. ;; keyword.
  551. (should
  552. (equal "# C\n:PROPERTIES:\n:END:\n#+TITLE: T"
  553. (org-test-with-temp-text "# C\n#+TITLE: T"
  554. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  555. (buffer-string))))
  556. ;; Insert drawer in document header with existing keyword.
  557. (should
  558. (equal ":PROPERTIES:\n:END:\n#+TITLE: T"
  559. (org-test-with-temp-text "#+TITLE: T"
  560. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  561. (buffer-string))))
  562. (should
  563. (equal ":PROPERTIES:\n:END:"
  564. (org-test-with-temp-text ":PROPERTIES:\n:END:"
  565. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  566. (buffer-string))))
  567. ;; Insert drawer in document header with one existing heading in buffer.
  568. (should
  569. (equal ":PROPERTIES:\n:END:\n\n* T\n"
  570. (org-test-with-temp-text "\n* T\n"
  571. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  572. (buffer-string))))
  573. ;; Insert drawer right after headline if there is no planning line,
  574. ;; or after it otherwise.
  575. (should
  576. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  577. (org-test-with-temp-text "* H\nParagraph<point>"
  578. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  579. (buffer-string))))
  580. (should
  581. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  582. (org-test-with-temp-text
  583. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  584. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  585. (buffer-string))))
  586. ;; Indent inserted drawer.
  587. (should
  588. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  589. (org-test-with-temp-text "* H\nParagraph<point>"
  590. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  591. (buffer-string))))
  592. ;; Handle insertion at eob.
  593. (should
  594. (equal "* H\n:PROPERTIES:\n:END:\n"
  595. (org-test-with-temp-text "* H"
  596. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  597. (buffer-string))))
  598. ;; Skip inlinetasks before point.
  599. (when (featurep 'org-inlinetask)
  600. (should
  601. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  602. (org-test-with-temp-text
  603. "* H\n*************** I\n*************** END\nP<point>"
  604. (let ((org-adapt-indentation nil)
  605. (org-inlinetask-min-level 15))
  606. (org-insert-property-drawer))
  607. (buffer-string)))))
  608. ;; Correctly set drawer in an inlinetask.
  609. (when (featurep 'org-inlinetask)
  610. (should
  611. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  612. (org-test-with-temp-text
  613. "* H\n*************** I\nP<point>\n*************** END"
  614. (let ((org-adapt-indentation nil)
  615. (org-inlinetask-min-level 15))
  616. (org-insert-property-drawer))
  617. (buffer-string))))))
  618. ;;; Filling
  619. (ert-deftest test-org/fill-element ()
  620. "Test `org-fill-element' specifications."
  621. ;; At an Org table, align it.
  622. (should
  623. (equal "| a |\n"
  624. (org-test-with-temp-text "|a|"
  625. (org-fill-element)
  626. (buffer-string))))
  627. (should
  628. (equal "#+name: table\n| a |\n"
  629. (org-test-with-temp-text "#+name: table\n| a |\n"
  630. (org-fill-element)
  631. (buffer-string))))
  632. ;; At a paragraph, preserve line breaks.
  633. (org-test-with-temp-text "some \\\\\nlong\ntext"
  634. (let ((fill-column 20))
  635. (org-fill-element)
  636. (should (equal (buffer-string) "some \\\\\nlong text"))))
  637. ;; Correctly fill a paragraph when point is at its very end.
  638. (should
  639. (equal "A B"
  640. (org-test-with-temp-text "A\nB"
  641. (let ((fill-column 20))
  642. (goto-char (point-max))
  643. (org-fill-element)
  644. (buffer-string)))))
  645. ;; Correctly fill the last paragraph of a greater element.
  646. (should
  647. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  648. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  649. (let ((fill-column 8))
  650. (forward-line)
  651. (end-of-line)
  652. (org-fill-element)
  653. (buffer-string)))))
  654. ;; Correctly fill an element in a narrowed buffer.
  655. (should
  656. (equal "01234\n6"
  657. (org-test-with-temp-text "01234 6789"
  658. (let ((fill-column 5))
  659. (narrow-to-region 1 8)
  660. (org-fill-element)
  661. (buffer-string)))))
  662. ;; Handle `adaptive-fill-regexp' in paragraphs.
  663. (should
  664. (equal "> a b"
  665. (org-test-with-temp-text "> a\n> b"
  666. (let ((fill-column 5)
  667. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  668. (org-fill-element)
  669. (buffer-string)))))
  670. ;; Special case: Fill first paragraph when point is at an item or
  671. ;; a plain-list or a footnote reference.
  672. (should
  673. (equal "- A B"
  674. (org-test-with-temp-text "- A\n B"
  675. (let ((fill-column 20))
  676. (org-fill-element)
  677. (buffer-string)))))
  678. (should
  679. (equal "[fn:1] A B"
  680. (org-test-with-temp-text "[fn:1] A\nB"
  681. (let ((fill-column 20))
  682. (org-fill-element)
  683. (buffer-string)))))
  684. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  685. (let ((fill-column 20))
  686. (org-fill-element)
  687. (should (equal (buffer-string)
  688. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  689. ;; Fill contents of `comment-block' elements.
  690. (should
  691. (equal
  692. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  693. (let ((fill-column 20))
  694. (forward-line)
  695. (org-fill-element)
  696. (buffer-string)))
  697. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  698. ;; Fill `comment' elements.
  699. (should
  700. (equal " # A B"
  701. (org-test-with-temp-text " # A\n # B"
  702. (let ((fill-column 20))
  703. (org-fill-element)
  704. (buffer-string)))))
  705. ;; Do not mix consecutive comments when filling one of them.
  706. (should
  707. (equal "# A B\n\n# C"
  708. (org-test-with-temp-text "# A\n# B\n\n# C"
  709. (let ((fill-column 20))
  710. (org-fill-element)
  711. (buffer-string)))))
  712. ;; Use commented empty lines as separators when filling comments.
  713. (should
  714. (equal "# A B\n#\n# C"
  715. (org-test-with-temp-text "# A\n# B\n#\n# C"
  716. (let ((fill-column 20))
  717. (org-fill-element)
  718. (buffer-string)))))
  719. ;; Handle `adaptive-fill-regexp' in comments.
  720. (should
  721. (equal "# > a b"
  722. (org-test-with-temp-text "# > a\n# > b"
  723. (let ((fill-column 20)
  724. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  725. (org-fill-element)
  726. (buffer-string)))))
  727. ;; Do nothing at affiliated keywords.
  728. (should
  729. (equal "#+NAME: para\nSome\ntext."
  730. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  731. (let ((fill-column 20))
  732. (org-fill-element)
  733. (buffer-string)))))
  734. ;; Do not move point after table when filling a table.
  735. (should-not
  736. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  737. (forward-char)
  738. (org-fill-element)
  739. (eobp)))
  740. ;; Do not fill "n" macro, with or without arguments, followed by
  741. ;; a dot or a closing parenthesis since it could be confused with
  742. ;; a numbered bullet.
  743. (should-not
  744. (equal "123456789\n{{{n}}}."
  745. (org-test-with-temp-text "123456789 {{{n}}}."
  746. (let ((fill-column 10))
  747. (org-fill-element)
  748. (buffer-string)))))
  749. (should-not
  750. (equal "123456789\n{{{n}}}\)"
  751. (org-test-with-temp-text "123456789 {{{n}}}\)"
  752. (let ((fill-column 10))
  753. (org-fill-element)
  754. (buffer-string)))))
  755. (should-not
  756. (equal "123456789\n{{{n()}}}."
  757. (org-test-with-temp-text "123456789 {{{n()}}}."
  758. (let ((fill-column 10))
  759. (org-fill-element)
  760. (buffer-string)))))
  761. (should-not
  762. (equal "123456789\n{{{n(counter)}}}."
  763. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  764. (let ((fill-column 10))
  765. (org-fill-element)
  766. (buffer-string))))))
  767. (ert-deftest test-org/fill-paragraph ()
  768. "Test `org-fill-paragraph' specifications."
  769. ;; Regular test.
  770. (should
  771. (equal "012345678\n9"
  772. (org-test-with-temp-text "012345678 9"
  773. (let ((fill-column 10))
  774. (org-fill-paragraph)
  775. (buffer-string)))))
  776. ;; Fill paragraph even at end of buffer.
  777. (should
  778. (equal "012345678\n9\n"
  779. (org-test-with-temp-text "012345678 9\n<point>"
  780. (let ((fill-column 10))
  781. (org-fill-paragraph)
  782. (buffer-string)))))
  783. ;; Between two paragraphs, fill the next one.
  784. (should
  785. (equal "012345678 9\n\n012345678\n9"
  786. (org-test-with-temp-text "012345678 9\n<point>\n012345678 9"
  787. (let ((fill-column 10))
  788. (org-fill-paragraph)
  789. (buffer-string)))))
  790. (should
  791. (equal "012345678\n9\n\n012345678 9"
  792. (org-test-with-temp-text "012345678 9<point>\n\n012345678 9"
  793. (let ((fill-column 10))
  794. (org-fill-paragraph)
  795. (buffer-string)))))
  796. ;; Fill paragraph in a comment block.
  797. (should
  798. (equal "#+begin_comment\n012345678\n9\n#+end_comment"
  799. (org-test-with-temp-text
  800. "#+begin_comment\n<point>012345678 9\n#+end_comment"
  801. (let ((fill-column 10))
  802. (org-fill-paragraph)
  803. (buffer-string)))))
  804. ;; When a region is selected, fill every paragraph in the region.
  805. (should
  806. (equal "012345678\n9\n\n012345678\n9"
  807. (org-test-with-temp-text "012345678 9\n\n012345678 9"
  808. (let ((fill-column 10))
  809. (transient-mark-mode 1)
  810. (push-mark (point-min) t t)
  811. (goto-char (point-max))
  812. (call-interactively #'org-fill-paragraph)
  813. (buffer-string)))))
  814. (should
  815. (equal "012345678\n9\n\n012345678 9"
  816. (org-test-with-temp-text "012345678 9\n<point>\n012345678 9"
  817. (let ((fill-column 10))
  818. (transient-mark-mode 1)
  819. (push-mark (point) t t)
  820. (goto-char (point-min))
  821. (call-interactively #'org-fill-paragraph)
  822. (buffer-string)))))
  823. (should
  824. (equal "012345678 9\n\n012345678\n9"
  825. (org-test-with-temp-text "012345678 9\n<point>\n012345678 9"
  826. (let ((fill-column 10))
  827. (transient-mark-mode 1)
  828. (push-mark (point) t t)
  829. (goto-char (point-max))
  830. (call-interactively #'org-fill-paragraph)
  831. (buffer-string)))))
  832. ;; Fill every list item in a region
  833. (should
  834. (equal "\n- 2345678\n 9\n- 2345678\n 9"
  835. (org-test-with-temp-text "\n- 2345678 9\n- 2345678 9"
  836. (let ((fill-column 10))
  837. (transient-mark-mode 1)
  838. (push-mark (point-min) t t)
  839. (goto-char (point-max))
  840. (call-interactively #'org-fill-paragraph)
  841. (buffer-string)))))
  842. (should
  843. (equal "\n- 2345678\n 9\n- 2345678"
  844. (org-test-with-temp-text "\n- 2345678 9\n- 2345678"
  845. (let ((fill-column 10))
  846. (transient-mark-mode 1)
  847. (push-mark (point-min) t t)
  848. (goto-char (point-max))
  849. (call-interactively #'org-fill-paragraph)
  850. (buffer-string))))))
  851. (ert-deftest test-org/fill-region ()
  852. "Test `fill-region' behaviour."
  853. ;; fill-region should fill every item of a list
  854. (should
  855. (equal "\n- 2345678\n 9\n- 2345678\n 9"
  856. (org-test-with-temp-text "\n- 2345678 9\n- 2345678 9"
  857. (let ((fill-column 10))
  858. (transient-mark-mode 1)
  859. (push-mark (point-min) t t)
  860. (goto-char (point-max))
  861. (call-interactively #'fill-region)
  862. (buffer-string)))))
  863. (should
  864. (equal "\n- 1 2\n- 1 2"
  865. (org-test-with-temp-text "\n- 1\n 2\n- 1\n 2"
  866. (let ((fill-column 10))
  867. (transient-mark-mode 1)
  868. (push-mark (point-min) t t)
  869. (goto-char (point-max))
  870. (call-interactively #'fill-region)
  871. (buffer-string))))) )
  872. (ert-deftest test-org/auto-fill-function ()
  873. "Test auto-filling features."
  874. ;; Auto fill paragraph.
  875. (should
  876. (equal "12345\n7890"
  877. (org-test-with-temp-text "12345 7890"
  878. (let ((fill-column 5))
  879. (end-of-line)
  880. (org-auto-fill-function)
  881. (buffer-string)))))
  882. ;; Auto fill first paragraph in an item.
  883. (should
  884. (equal "- 12345\n 7890"
  885. (org-test-with-temp-text "- 12345 7890"
  886. (let ((fill-column 7))
  887. (end-of-line)
  888. (org-auto-fill-function)
  889. (buffer-string)))))
  890. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  891. (should
  892. (equal "> 12345\n 7890"
  893. (org-test-with-temp-text "> 12345 7890"
  894. (let ((fill-column 10)
  895. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  896. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  897. (end-of-line)
  898. (org-auto-fill-function)
  899. (buffer-string)))))
  900. (should
  901. (equal "> 12345\n> 12345\n> 7890"
  902. (org-test-with-temp-text "> 12345\n> 12345 7890"
  903. (let ((fill-column 10)
  904. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  905. (goto-char (point-max))
  906. (org-auto-fill-function)
  907. (buffer-string)))))
  908. (should-not
  909. (equal " 12345\n *12345\n *12345"
  910. (org-test-with-temp-text " 12345\n *12345 12345"
  911. (let ((fill-column 10)
  912. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  913. (goto-char (point-max))
  914. (org-auto-fill-function)
  915. (buffer-string)))))
  916. ;; Auto fill comments.
  917. (should
  918. (equal " # 12345\n # 7890"
  919. (org-test-with-temp-text " # 12345 7890"
  920. (let ((fill-column 10))
  921. (end-of-line)
  922. (org-auto-fill-function)
  923. (buffer-string)))))
  924. ;; A hash within a line isn't a comment.
  925. (should-not
  926. (equal "12345 # 7890\n# 1"
  927. (org-test-with-temp-text "12345 # 7890 1"
  928. (let ((fill-column 12))
  929. (end-of-line)
  930. (org-auto-fill-function)
  931. (buffer-string)))))
  932. ;; Correctly interpret empty prefix.
  933. (should-not
  934. (equal "# a\n# b\nRegular\n# paragraph"
  935. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  936. (let ((fill-column 12))
  937. (end-of-line 3)
  938. (org-auto-fill-function)
  939. (buffer-string)))))
  940. ;; Comment block: auto fill contents.
  941. (should
  942. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  943. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  944. (let ((fill-column 5))
  945. (forward-line)
  946. (end-of-line)
  947. (org-auto-fill-function)
  948. (buffer-string)))))
  949. (should
  950. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  951. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  952. (let ((fill-column 5))
  953. (forward-line)
  954. (end-of-line)
  955. (org-auto-fill-function)
  956. (buffer-string)))))
  957. ;; Do not fill if a new item could be created.
  958. (should-not
  959. (equal "12345\n- 90"
  960. (org-test-with-temp-text "12345 - 90"
  961. (let ((fill-column 5))
  962. (end-of-line)
  963. (org-auto-fill-function)
  964. (buffer-string)))))
  965. ;; Do not fill if a line break could be introduced.
  966. (should-not
  967. (equal "123\\\\\n7890"
  968. (org-test-with-temp-text "123\\\\ 7890"
  969. (let ((fill-column 6))
  970. (end-of-line)
  971. (org-auto-fill-function)
  972. (buffer-string)))))
  973. ;; Do not fill affiliated keywords.
  974. (should-not
  975. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  976. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  977. (let ((fill-column 20))
  978. (end-of-line)
  979. (org-auto-fill-function)
  980. (buffer-string))))))
  981. ;;; Indentation
  982. (ert-deftest test-org/indent-line ()
  983. "Test `org-indent-line' specifications."
  984. ;; Do not indent diary sexps, footnote definitions or headlines.
  985. (should
  986. (zerop
  987. (org-test-with-temp-text "%%(org-calendar-holiday)"
  988. (org-indent-line)
  989. (org-get-indentation))))
  990. (should
  991. (zerop
  992. (org-test-with-temp-text "[fn:1] fn"
  993. (let ((org-adapt-indentation t)) (org-indent-line))
  994. (org-get-indentation))))
  995. (should
  996. (zerop
  997. (org-test-with-temp-text "* H"
  998. (org-indent-line)
  999. (org-get-indentation))))
  1000. ;; Do not indent before first headline.
  1001. (should
  1002. (zerop
  1003. (org-test-with-temp-text ""
  1004. (org-indent-line)
  1005. (org-get-indentation))))
  1006. ;; Indent according to headline level otherwise, unless
  1007. ;; `org-adapt-indentation' is nil.
  1008. (should
  1009. (= 2
  1010. (org-test-with-temp-text "* H\n<point>A"
  1011. (let ((org-adapt-indentation t)) (org-indent-line))
  1012. (org-get-indentation))))
  1013. (should
  1014. (= 2
  1015. (org-test-with-temp-text "* H\n<point>\nA"
  1016. (let ((org-adapt-indentation t)) (org-indent-line))
  1017. (org-get-indentation))))
  1018. (should
  1019. (zerop
  1020. (org-test-with-temp-text "* H\n<point>A"
  1021. (let ((org-adapt-indentation nil)) (org-indent-line))
  1022. (org-get-indentation))))
  1023. ;; Indenting preserves point position.
  1024. (should
  1025. (org-test-with-temp-text "* H\nA<point>B"
  1026. (let ((org-adapt-indentation t)) (org-indent-line))
  1027. (looking-at "B")))
  1028. ;; Do not change indentation at an item or a LaTeX environment.
  1029. (should
  1030. (= 1
  1031. (org-test-with-temp-text "* H\n<point> - A"
  1032. (let ((org-adapt-indentation t)) (org-indent-line))
  1033. (org-get-indentation))))
  1034. (should
  1035. (= 1
  1036. (org-test-with-temp-text
  1037. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  1038. (org-indent-line)
  1039. (org-get-indentation))))
  1040. ;; On blank lines at the end of a list, indent like last element
  1041. ;; within it if the line is still in the list. If the last element
  1042. ;; is an item, indent like its contents. Otherwise, indent like the
  1043. ;; whole list.
  1044. (should
  1045. (= 4
  1046. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  1047. (let ((org-adapt-indentation t)) (org-indent-line))
  1048. (org-get-indentation))))
  1049. (should
  1050. (= 4
  1051. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  1052. (let ((org-adapt-indentation t)) (org-indent-line))
  1053. (org-get-indentation))))
  1054. (should
  1055. (zerop
  1056. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  1057. (let ((org-adapt-indentation t)) (org-indent-line))
  1058. (org-get-indentation))))
  1059. (should
  1060. (= 4
  1061. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  1062. (let ((org-adapt-indentation t)) (org-indent-line))
  1063. (org-get-indentation))))
  1064. (should
  1065. (= 4
  1066. (org-test-with-temp-text
  1067. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  1068. (let ((org-adapt-indentation t)) (org-indent-line))
  1069. (org-get-indentation))))
  1070. (should
  1071. (= 2
  1072. (org-test-with-temp-text "- A\n B\n\n<point>"
  1073. (let ((org-adapt-indentation nil)) (org-indent-line))
  1074. (org-get-indentation))))
  1075. (should
  1076. (= 2
  1077. (org-test-with-temp-text
  1078. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  1079. (let ((org-adapt-indentation nil)) (org-indent-line))
  1080. (org-get-indentation))))
  1081. ;; Likewise, on a blank line at the end of a footnote definition,
  1082. ;; indent at column 0 if line belongs to the definition. Otherwise,
  1083. ;; indent like the definition itself.
  1084. (should
  1085. (zerop
  1086. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  1087. (let ((org-adapt-indentation t)) (org-indent-line))
  1088. (org-get-indentation))))
  1089. (should
  1090. (zerop
  1091. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  1092. (let ((org-adapt-indentation t)) (org-indent-line))
  1093. (org-get-indentation))))
  1094. ;; After the end of the contents of a greater element, indent like
  1095. ;; the beginning of the element.
  1096. (should
  1097. (= 1
  1098. (org-test-with-temp-text
  1099. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  1100. (org-indent-line)
  1101. (org-get-indentation))))
  1102. ;; On blank lines after a paragraph, indent like its last non-empty
  1103. ;; line.
  1104. (should
  1105. (= 1
  1106. (org-test-with-temp-text " Paragraph\n\n<point>"
  1107. (org-indent-line)
  1108. (org-get-indentation))))
  1109. ;; At the first line of an element, indent like previous element's
  1110. ;; first line, ignoring footnotes definitions and inline tasks, or
  1111. ;; according to parent.
  1112. (let ((org-adapt-indentation t))
  1113. (should
  1114. (= 2
  1115. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  1116. (org-indent-line)
  1117. (org-get-indentation))))
  1118. (should
  1119. (= 1
  1120. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  1121. (org-indent-line)
  1122. (org-get-indentation))))
  1123. (should
  1124. (= 1
  1125. (org-test-with-temp-text
  1126. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  1127. (org-indent-line)
  1128. (org-get-indentation)))))
  1129. ;; Within code part of a source block, use language major mode if
  1130. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  1131. ;; according to line above.
  1132. (should
  1133. (= 6
  1134. (org-test-with-temp-text
  1135. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  1136. (let ((org-src-tab-acts-natively t)
  1137. (org-edit-src-content-indentation 0))
  1138. (org-indent-line))
  1139. (org-get-indentation))))
  1140. (should
  1141. (= 1
  1142. (org-test-with-temp-text
  1143. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  1144. (let ((org-src-tab-acts-natively nil)
  1145. (org-edit-src-content-indentation 0))
  1146. (org-indent-line))
  1147. (org-get-indentation))))
  1148. ;; Otherwise, indent like the first non-blank line above.
  1149. (should
  1150. (zerop
  1151. (org-test-with-temp-text
  1152. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  1153. (org-indent-line)
  1154. (org-get-indentation))))
  1155. ;; Align node properties according to `org-property-format'. Handle
  1156. ;; nicely empty values.
  1157. (should
  1158. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  1159. (org-test-with-temp-text
  1160. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  1161. (let ((org-property-format "%-10s %s")) (org-indent-line))
  1162. (buffer-string))))
  1163. (should
  1164. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  1165. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  1166. (let ((org-property-format "%-10s %s")) (org-indent-line))
  1167. (buffer-string)))))
  1168. (ert-deftest test-org/indent-region ()
  1169. "Test `org-indent-region' specifications."
  1170. ;; Indent paragraph.
  1171. (let ((org-adapt-indentation t))
  1172. (should
  1173. (equal "A\nB\nC"
  1174. (org-test-with-temp-text " A\nB\n C"
  1175. (org-indent-region (point-min) (point-max))
  1176. (buffer-string)))))
  1177. ;; Indent greater elements along with their contents.
  1178. (should
  1179. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  1180. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  1181. (org-indent-region (point-min) (point-max))
  1182. (buffer-string))))
  1183. ;; Ignore contents of verse blocks. Only indent block delimiters.
  1184. (should
  1185. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  1186. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  1187. (org-indent-region (point-min) (point-max))
  1188. (buffer-string))))
  1189. (let ((org-adapt-indentation t))
  1190. (should
  1191. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  1192. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  1193. (org-indent-region (point-min) (point-max))
  1194. (buffer-string)))))
  1195. ;; Indent example blocks as a single block, unless indentation
  1196. ;; should be preserved. In this case only indent the block markers.
  1197. (should
  1198. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1199. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1200. (org-indent-region (point-min) (point-max))
  1201. (buffer-string))))
  1202. (let ((org-adapt-indentation t))
  1203. (should
  1204. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1205. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  1206. (org-indent-region (point-min) (point-max))
  1207. (buffer-string))))
  1208. (should
  1209. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  1210. (org-test-with-temp-text
  1211. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  1212. (org-indent-region (point-min) (point-max))
  1213. (buffer-string))))
  1214. (should
  1215. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1216. (org-test-with-temp-text
  1217. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  1218. (let ((org-src-preserve-indentation t))
  1219. (org-indent-region (point-min) (point-max)))
  1220. (buffer-string)))))
  1221. ;; Treat export blocks as a whole.
  1222. (should
  1223. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1224. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1225. (org-indent-region (point-min) (point-max))
  1226. (buffer-string))))
  1227. (let ((org-adapt-indentation t))
  1228. (should
  1229. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1230. (org-test-with-temp-text
  1231. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  1232. (org-indent-region (point-min) (point-max))
  1233. (buffer-string)))))
  1234. ;; Indent according to mode if `org-src-tab-acts-natively' is
  1235. ;; non-nil. Otherwise, do not indent code at all.
  1236. (should
  1237. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  1238. (org-test-with-temp-text
  1239. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1240. (let ((org-src-tab-acts-natively t)
  1241. (org-edit-src-content-indentation 0))
  1242. (org-indent-region (point-min) (point-max)))
  1243. (buffer-string))))
  1244. (should
  1245. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1246. (org-test-with-temp-text
  1247. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1248. (let ((org-src-tab-acts-natively nil)
  1249. (org-edit-src-content-indentation 0))
  1250. (org-indent-region (point-min) (point-max)))
  1251. (buffer-string))))
  1252. ;; Align node properties according to `org-property-format'. Handle
  1253. ;; nicely empty values.
  1254. (should
  1255. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  1256. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  1257. (let ((org-property-format "%-10s %s")
  1258. (org-adapt-indentation nil))
  1259. (org-indent-region (point) (point-max)))
  1260. (buffer-string))))
  1261. (should
  1262. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  1263. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  1264. (let ((org-property-format "%-10s %s")
  1265. (org-adapt-indentation nil))
  1266. (org-indent-region (point) (point-max)))
  1267. (buffer-string))))
  1268. ;; Indent property drawers according to `org-adapt-indentation'.
  1269. (let ((org-adapt-indentation 'headline-data))
  1270. (should
  1271. (equal "* H\n :PROPERTIES:\n :key:\n :END:"
  1272. (org-test-with-temp-text "* H\n:PROPERTIES:\n:key:\n:END:"
  1273. (org-indent-region (point-min) (point-max))
  1274. (buffer-string)))))
  1275. ;; Indent planning according to `org-adapt-indentation'.
  1276. (let ((org-adapt-indentation 'headline-data))
  1277. (should
  1278. (equal "* H\n SCHEDULED: <2022-11-03>"
  1279. (org-test-with-temp-text "* H\nSCHEDULED: <2022-11-03>"
  1280. (org-indent-region (point-min) (point-max))
  1281. (buffer-string)))))
  1282. ;; Indent LOGBOOK according to `org-adapt-indentation'.
  1283. (let ((org-adapt-indentation 'headline-data))
  1284. (should
  1285. (equal "* H\n :LOGBOOK:
  1286. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1287. :END:"
  1288. (org-test-with-temp-text "* H\n:LOGBOOK:
  1289. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1290. :END:"
  1291. (org-indent-region (point-min) (point-max))
  1292. (buffer-string)))))
  1293. ;; Indent clock lines according to `org-adapt-indentation'.
  1294. (let ((org-adapt-indentation 'headline-data))
  1295. (should
  1296. (equal "* H
  1297. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1298. (org-test-with-temp-text "* H
  1299. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1300. (org-indent-region (point-min) (point-max))
  1301. (buffer-string)))))
  1302. ;; Do not indent beyond headline data.
  1303. (let ((org-adapt-indentation 'headline-data))
  1304. (should
  1305. (equal "* H\n SCHEDULED: <2022-11-03>\nParagraph"
  1306. (org-test-with-temp-text "* H\nSCHEDULED: <2022-11-03>\nParagraph"
  1307. (org-indent-region (point-min) (point-max))
  1308. (buffer-string)))))
  1309. (let ((org-adapt-indentation 'headline-data)
  1310. (org-log-into-drawer t))
  1311. (should
  1312. (equal "* TODO A task
  1313. :PROPERTIES:
  1314. :CAPTURED: [2022-09-11 dim. 21:25]
  1315. :END:
  1316. :LOGBOOK:
  1317. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1318. :END:
  1319. Paragraph"
  1320. (org-test-with-temp-text "* TODO A task
  1321. :PROPERTIES:
  1322. :CAPTURED: [2022-09-11 dim. 21:25]
  1323. :END:
  1324. :LOGBOOK:
  1325. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1326. :END:
  1327. Paragraph"
  1328. (org-indent-region (point-min) (point-max))
  1329. (buffer-string)))))
  1330. (let ((org-adapt-indentation 'headline-data))
  1331. (should
  1332. (equal "* TODO A task
  1333. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1334. Paragraph
  1335. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1336. (org-test-with-temp-text "* TODO A task
  1337. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1338. Paragraph
  1339. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1340. (org-indent-region (point-min) (point-max))
  1341. (buffer-string)))))
  1342. ;; Indent plain lists.
  1343. (let ((org-adapt-indentation t))
  1344. (should
  1345. (equal "- A\n B\n - C\n\n D"
  1346. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  1347. (org-indent-region (point-min) (point-max))
  1348. (buffer-string))))
  1349. (should
  1350. (equal "- A\n\n- B"
  1351. (org-test-with-temp-text " - A\n\n - B"
  1352. (org-indent-region (point-min) (point-max))
  1353. (buffer-string)))))
  1354. ;; Indent footnote definitions.
  1355. (should
  1356. (equal "[fn:1] Definition\n\nDefinition"
  1357. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  1358. (org-indent-region (point-min) (point-max))
  1359. (buffer-string))))
  1360. ;; Special case: Start indenting on a blank line.
  1361. (should
  1362. (equal "\nParagraph"
  1363. (org-test-with-temp-text "\n Paragraph"
  1364. (org-indent-region (point-min) (point-max))
  1365. (buffer-string)))))
  1366. (ert-deftest test-org/default-indent-new-line ()
  1367. "Test behavior of default binding `M-j'."
  1368. ;; Calling `M-j' when point is not in an Org comment:
  1369. (should
  1370. (equal "* Some heading\n"
  1371. (org-test-with-temp-text "* Some heading<point>"
  1372. (call-interactively #'default-indent-new-line)
  1373. (buffer-string))))
  1374. ;; Calling `M-j' when point is in an Org comment:
  1375. (should
  1376. (equal "# Some Org comment\n# "
  1377. (org-test-with-temp-text "# Some Org comment<point>"
  1378. (call-interactively #'default-indent-new-line)
  1379. (buffer-string))))
  1380. (should
  1381. (equal "# Some Org\n# comment"
  1382. (org-test-with-temp-text "# Some Org <point>comment"
  1383. (call-interactively #'default-indent-new-line)
  1384. (buffer-string)))))
  1385. ;;; Editing
  1386. (ert-deftest test-org/delete-indentation ()
  1387. "Test `org-delete-indentation' specifications."
  1388. ;; Regular test.
  1389. (should (equal "foo bar"
  1390. (org-test-with-temp-text
  1391. "foo \n bar<point>"
  1392. (org-delete-indentation)
  1393. (buffer-string))))
  1394. ;; With optional argument.
  1395. (should (equal "foo bar"
  1396. (org-test-with-temp-text
  1397. "foo<point> \n bar"
  1398. (org-delete-indentation t)
  1399. (buffer-string))))
  1400. ;; At headline text should be appended to the headline text.
  1401. (should
  1402. (equal"* foo bar :tag:"
  1403. (let (org-auto-align-tags)
  1404. (org-test-with-temp-text
  1405. "* foo :tag:\n bar<point>"
  1406. (org-delete-indentation)
  1407. (buffer-string)))))
  1408. (should
  1409. (equal "* foo bar :tag:"
  1410. (let (org-auto-align-tags)
  1411. (org-test-with-temp-text
  1412. "* foo <point>:tag:\n bar"
  1413. (org-delete-indentation t)
  1414. (buffer-string))))))
  1415. (ert-deftest test-org/return ()
  1416. "Test `org-return' specifications."
  1417. ;; Regular test.
  1418. (should
  1419. (equal "Para\ngraph"
  1420. (org-test-with-temp-text "Para<point>graph"
  1421. (org-return)
  1422. (buffer-string))))
  1423. ;; With optional argument, indent line.
  1424. (should
  1425. (equal " Para\n graph"
  1426. (org-test-with-temp-text " Para<point>graph"
  1427. (org-return t)
  1428. (buffer-string))))
  1429. ;; On a table, call `org-table-next-row'.
  1430. (should
  1431. (org-test-with-temp-text "| <point>a |\n| b |"
  1432. (org-return)
  1433. (looking-at-p "b")))
  1434. ;; Open link or timestamp under point when `org-return-follows-link'
  1435. ;; is non-nil.
  1436. (should
  1437. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1438. (let ((org-return-follows-link t)
  1439. (org-link-search-must-match-exact-headline nil))
  1440. (org-return))
  1441. (looking-at-p "<<target>>")))
  1442. (should-not
  1443. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1444. (let ((org-return-follows-link nil)) (org-return))
  1445. (looking-at-p "<<target>>")))
  1446. (should
  1447. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1448. (let ((org-return-follows-link t)) (org-return))
  1449. (looking-at-p "* b")))
  1450. (should
  1451. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1452. (let ((org-return-follows-link t)
  1453. (org-link-search-must-match-exact-headline nil))
  1454. (org-return))
  1455. (looking-at-p "<<target>>")))
  1456. (should-not
  1457. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1458. (let ((org-return-follows-link t)
  1459. (org-link-search-must-match-exact-headline nil))
  1460. (org-return))
  1461. (looking-at-p "<<target>>")))
  1462. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1463. ;; timestamps in comments, node properties, etc.
  1464. (should
  1465. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1466. (let ((org-return-follows-link t)
  1467. (org-link-search-must-match-exact-headline nil))
  1468. (org-return))
  1469. (looking-at-p "<<target>>")))
  1470. (should-not
  1471. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1472. (let ((org-return-follows-link nil)) (org-return))
  1473. (looking-at-p "<<target>>")))
  1474. (should-not
  1475. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1476. (let ((org-return-follows-link t)
  1477. (org-link-search-must-match-exact-headline nil))
  1478. (org-return))
  1479. (looking-at-p "<<target>>")))
  1480. ;; Non-nil `org-return-follows-link' ignores read-only state of
  1481. ;; a buffer.
  1482. (should
  1483. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1484. (let ((org-return-follows-link t)
  1485. (org-link-search-must-match-exact-headline nil))
  1486. (setq buffer-read-only t)
  1487. (call-interactively #'org-return))
  1488. (looking-at-p "<<target>>")))
  1489. ;; `org-return-follows-link' handle multi-line lines.
  1490. (should
  1491. (org-test-with-temp-text
  1492. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1493. (let ((org-return-follows-link t)
  1494. (org-link-search-must-match-exact-headline nil))
  1495. (org-return))
  1496. (looking-at-p "<<target>>")))
  1497. (should-not
  1498. (org-test-with-temp-text
  1499. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1500. (let ((org-return-follows-link t)
  1501. (org-link-search-must-match-exact-headline nil))
  1502. (org-return))
  1503. (looking-at-p "<<target>>")))
  1504. ;; However, do not open link when point is in a table.
  1505. (should
  1506. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1507. (let ((org-return-follows-link t)) (org-return))
  1508. (looking-at-p "between")))
  1509. ;; Special case: in a list, when indenting, do not break structure.
  1510. (should
  1511. (equal "- A\n B"
  1512. (org-test-with-temp-text "- A <point>B"
  1513. (org-return t)
  1514. (buffer-string))))
  1515. (should
  1516. (equal "- A\n\n- B"
  1517. (org-test-with-temp-text "- A\n<point>- B"
  1518. (org-return t)
  1519. (buffer-string))))
  1520. ;; On tags part of a headline, add a newline below it instead of
  1521. ;; breaking it.
  1522. (should
  1523. (equal "* H :tag:\n"
  1524. (org-test-with-temp-text "* H :<point>tag:"
  1525. (org-return)
  1526. (buffer-string))))
  1527. ;; Before headline text, add a newline below it instead of breaking
  1528. ;; it.
  1529. (should
  1530. (equal "* TODO H :tag:\n"
  1531. (org-test-with-temp-text "* <point>TODO H :tag:"
  1532. (org-return)
  1533. (buffer-string))))
  1534. (should
  1535. (equal "* TODO [#B] H :tag:\n"
  1536. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1537. (org-return)
  1538. (buffer-string))))
  1539. (should ;TODO are case-sensitive
  1540. (equal "* \nTodo"
  1541. (org-test-with-temp-text "* <point>Todo"
  1542. (org-return)
  1543. (buffer-string))))
  1544. ;; At headline text, break headline text but preserve tags.
  1545. (should
  1546. (equal "* TODO [#B] foo :tag:\nbar"
  1547. (let (org-auto-align-tags)
  1548. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1549. (org-return)
  1550. (buffer-string)))))
  1551. ;; At bol of headline insert newline.
  1552. (should
  1553. (equal "\n* h"
  1554. (org-test-with-temp-text "<point>* h"
  1555. (org-return)
  1556. (buffer-string))))
  1557. ;; Refuse to leave invalid headline in buffer.
  1558. (should
  1559. (equal "* h\n"
  1560. (org-test-with-temp-text "*<point> h"
  1561. (org-return)
  1562. (buffer-string))))
  1563. ;; Before first column or after last one in a table, split the
  1564. ;; table.
  1565. (should
  1566. (equal "| a |\n\n| b |"
  1567. (org-test-with-temp-text "| a |\n<point>| b |"
  1568. (org-return)
  1569. (buffer-string))))
  1570. (should
  1571. (equal "| a |\n\n| b |"
  1572. (org-test-with-temp-text "| a |<point>\n| b |"
  1573. (org-return)
  1574. (buffer-string))))
  1575. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1576. (should
  1577. (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:"
  1578. (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:"
  1579. (setq-local fill-column 10)
  1580. (auto-fill-mode 1)
  1581. (org-return)
  1582. (buffer-string)))))
  1583. (ert-deftest test-org/with-electric-indent ()
  1584. "Test RET and C-j specifications with `electric-indent-mode' on."
  1585. ;; Call commands interactively, since this is how `newline' knows it
  1586. ;; must run `post-self-insert-hook'.
  1587. ;;
  1588. ;; RET, like `newline', should indent.
  1589. (should
  1590. (equal " Para\n graph"
  1591. (org-test-with-temp-text " Para<point>graph"
  1592. (electric-indent-local-mode 1)
  1593. (call-interactively 'org-return)
  1594. (buffer-string))))
  1595. (should
  1596. (equal "- item1\n item2"
  1597. (org-test-with-temp-text "- item1<point>item2"
  1598. (electric-indent-local-mode 1)
  1599. (call-interactively 'org-return)
  1600. (buffer-string))))
  1601. ;; TODO: test more values of `org-adapt-indentation'.
  1602. (let ((org-adapt-indentation t))
  1603. (should
  1604. (equal "* heading\n body"
  1605. (org-test-with-temp-text "* heading<point>body"
  1606. (electric-indent-local-mode 1)
  1607. (call-interactively 'org-return)
  1608. (buffer-string)))))
  1609. ;; C-j, like `electric-newline-and-maybe-indent', should not indent.
  1610. (should
  1611. (equal " Para\ngraph"
  1612. (org-test-with-temp-text " Para<point>graph"
  1613. (electric-indent-local-mode 1)
  1614. (call-interactively 'org-return-and-maybe-indent)
  1615. (buffer-string))))
  1616. (should
  1617. (equal "- item1\nitem2"
  1618. (org-test-with-temp-text "- item1<point>item2"
  1619. (electric-indent-local-mode 1)
  1620. (call-interactively 'org-return-and-maybe-indent)
  1621. (buffer-string))))
  1622. ;; TODO: test more values of `org-adapt-indentation'.
  1623. (let ((org-adapt-indentation t))
  1624. (should
  1625. (equal "* heading\nbody"
  1626. (org-test-with-temp-text "* heading<point>body"
  1627. (electric-indent-local-mode 1)
  1628. (call-interactively 'org-return-and-maybe-indent)
  1629. (buffer-string))))))
  1630. (ert-deftest test-org/without-electric-indent ()
  1631. "Test RET and C-j specifications with `electric-indent-mode' off."
  1632. ;; Call commands interactively, since this is how `newline' knows it
  1633. ;; must run `post-self-insert-hook'.
  1634. ;;
  1635. ;; RET, like `newline', should not indent.
  1636. (should
  1637. (equal " Para\ngraph"
  1638. (org-test-with-temp-text " Para<point>graph"
  1639. (electric-indent-local-mode 0)
  1640. (call-interactively 'org-return)
  1641. (buffer-string))))
  1642. (should
  1643. (equal "- item1\nitem2"
  1644. (org-test-with-temp-text "- item1<point>item2"
  1645. (electric-indent-local-mode 0)
  1646. (call-interactively 'org-return)
  1647. (buffer-string))))
  1648. (should
  1649. (equal "* heading\nbody"
  1650. (org-test-with-temp-text "* heading<point>body"
  1651. (electric-indent-local-mode 0)
  1652. (call-interactively 'org-return)
  1653. (buffer-string))))
  1654. ;; C-j, like `electric-newline-and-maybe-indent', should indent.
  1655. (should
  1656. (equal " Para\n graph"
  1657. (org-test-with-temp-text " Para<point>graph"
  1658. (electric-indent-local-mode 0)
  1659. (call-interactively 'org-return-and-maybe-indent)
  1660. (buffer-string))))
  1661. (should
  1662. (equal "- item1\n item2"
  1663. (org-test-with-temp-text "- item1<point>item2"
  1664. (electric-indent-local-mode 0)
  1665. (call-interactively 'org-return-and-maybe-indent)
  1666. (buffer-string))))
  1667. ;; TODO: test more values of `org-adapt-indentation'.
  1668. (let ((org-adapt-indentation t))
  1669. (should
  1670. (equal "* heading\n body"
  1671. (org-test-with-temp-text "* heading<point>body"
  1672. (electric-indent-local-mode 0)
  1673. (call-interactively 'org-return-and-maybe-indent)
  1674. (buffer-string))))))
  1675. (ert-deftest test-org/meta-return ()
  1676. "Test M-RET (`org-meta-return') specifications."
  1677. ;; In a table field insert a row above.
  1678. (should
  1679. (org-test-with-temp-text "| a |"
  1680. (forward-char)
  1681. (org-meta-return)
  1682. (forward-line -1)
  1683. (looking-at "| |$")))
  1684. ;; In a paragraph change current line into a header.
  1685. (should
  1686. (org-test-with-temp-text "a"
  1687. (org-meta-return)
  1688. (beginning-of-line)
  1689. (looking-at "\* a$")))
  1690. ;; In an item insert an item, in this case above.
  1691. (should
  1692. (org-test-with-temp-text "- a"
  1693. (org-meta-return)
  1694. (beginning-of-line)
  1695. (looking-at "- $")))
  1696. ;; In a drawer and item insert an item, in this case above.
  1697. (should
  1698. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1699. (forward-line)
  1700. (org-fold-reveal)
  1701. (org-meta-return)
  1702. (beginning-of-line)
  1703. (looking-at "- $"))))
  1704. (ert-deftest test-org/insert-heading ()
  1705. "Test `org-insert-heading' specifications."
  1706. ;; In an empty buffer, insert a new headline.
  1707. (should
  1708. (equal "* "
  1709. (org-test-with-temp-text ""
  1710. (org-insert-heading)
  1711. (buffer-string))))
  1712. ;; At the beginning of a line, turn it into a headline.
  1713. (should
  1714. (equal "* P"
  1715. (org-test-with-temp-text "<point>P"
  1716. (org-insert-heading)
  1717. (buffer-string))))
  1718. ;; In the middle of a line, split the line if allowed, otherwise,
  1719. ;; insert the headline at its end.
  1720. (should
  1721. (equal "Para\n* graph"
  1722. (org-test-with-temp-text "Para<point>graph"
  1723. (let ((org-M-RET-may-split-line '((default . t))))
  1724. (org-insert-heading))
  1725. (buffer-string))))
  1726. (should
  1727. (equal "Paragraph\n* "
  1728. (org-test-with-temp-text "Para<point>graph"
  1729. (let ((org-M-RET-may-split-line '((default . nil))))
  1730. (org-insert-heading))
  1731. (buffer-string))))
  1732. ;; At the beginning of a headline, create one above.
  1733. (should
  1734. (equal "* \n* H"
  1735. (org-test-with-temp-text "* H"
  1736. (org-insert-heading)
  1737. (buffer-string))))
  1738. ;; In the middle of a headline, split it if allowed.
  1739. (should
  1740. (equal "* H\n* 1"
  1741. (org-test-with-temp-text "* H<point>1"
  1742. (let ((org-M-RET-may-split-line '((headline . t))))
  1743. (org-insert-heading))
  1744. (buffer-string))))
  1745. (should
  1746. (equal "* H1\n* "
  1747. (org-test-with-temp-text "* H<point>1"
  1748. (let ((org-M-RET-may-split-line '((headline . nil))))
  1749. (org-insert-heading))
  1750. (buffer-string))))
  1751. ;; However, splitting cannot happen on TODO keywords, priorities or
  1752. ;; tags.
  1753. (should
  1754. (equal "* TODO H1\n* "
  1755. (org-test-with-temp-text "* TO<point>DO H1"
  1756. (let ((org-M-RET-may-split-line '((headline . t))))
  1757. (org-insert-heading))
  1758. (buffer-string))))
  1759. (should
  1760. (equal "* [#A] H1\n* "
  1761. (org-test-with-temp-text "* [#<point>A] H1"
  1762. (let ((org-M-RET-may-split-line '((headline . t))))
  1763. (org-insert-heading))
  1764. (buffer-string))))
  1765. (should
  1766. (equal "* H1 :tag:\n* "
  1767. (org-test-with-temp-text "* H1 :ta<point>g:"
  1768. (let ((org-M-RET-may-split-line '((headline . t))))
  1769. (org-insert-heading))
  1770. (buffer-string))))
  1771. ;; New headline level depends on the level of the headline above.
  1772. (should
  1773. (equal "** H\n** P"
  1774. (org-test-with-temp-text "** H\n<point>P"
  1775. (org-insert-heading)
  1776. (buffer-string))))
  1777. (should
  1778. (equal "** H\nPara\n** graph"
  1779. (org-test-with-temp-text "** H\nPara<point>graph"
  1780. (let ((org-M-RET-may-split-line '((default . t))))
  1781. (org-insert-heading))
  1782. (buffer-string))))
  1783. (should
  1784. (equal "** \n** H"
  1785. (org-test-with-temp-text "** H"
  1786. (org-insert-heading)
  1787. (buffer-string))))
  1788. ;; When called with one universal argument, insert a new headline at
  1789. ;; the end of the current subtree, independently on the position of
  1790. ;; point.
  1791. (should
  1792. (equal
  1793. "* "
  1794. (org-test-with-temp-text ""
  1795. (let ((org-insert-heading-respect-content nil))
  1796. (org-insert-heading '(4)))
  1797. (buffer-string))))
  1798. (should
  1799. (equal
  1800. "entry
  1801. * "
  1802. (org-test-with-temp-text "entry"
  1803. (let ((org-insert-heading-respect-content nil))
  1804. (org-insert-heading '(4)))
  1805. (buffer-string))))
  1806. (should
  1807. (equal
  1808. "* H1\n** H2\n* "
  1809. (org-test-with-temp-text "* H1\n** H2"
  1810. (let ((org-insert-heading-respect-content nil))
  1811. (org-insert-heading '(4)))
  1812. (buffer-string))))
  1813. (should
  1814. (equal
  1815. "* H1\n** H2\n* "
  1816. (org-test-with-temp-text "* H<point>1\n** H2"
  1817. (let ((org-insert-heading-respect-content nil))
  1818. (org-insert-heading '(4)))
  1819. (buffer-string))))
  1820. ;; When called with two universal arguments, insert a new headline
  1821. ;; at the end of the grandparent subtree.
  1822. (should
  1823. (equal "* H1\n** H3\n- item\n** H2\n** "
  1824. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1825. (let ((org-insert-heading-respect-content nil))
  1826. (org-insert-heading '(16)))
  1827. (buffer-string))))
  1828. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1829. ;; a level 1 heading.
  1830. (should
  1831. (equal "* H1\n** H2\n* "
  1832. (org-test-with-temp-text "* H1\n** H2<point>"
  1833. (org-insert-heading nil nil t)
  1834. (buffer-string))))
  1835. (should
  1836. (equal "* H1\n- item\n* "
  1837. (org-test-with-temp-text "* H1\n- item<point>"
  1838. (org-insert-heading nil nil t)
  1839. (buffer-string))))
  1840. ;; Obey `org-blank-before-new-entry'.
  1841. (should
  1842. (equal "* H1\n\n* "
  1843. (org-test-with-temp-text "* H1<point>"
  1844. (let ((org-blank-before-new-entry '((heading . t))))
  1845. (org-insert-heading))
  1846. (buffer-string))))
  1847. (should
  1848. (equal "* H1\n* "
  1849. (org-test-with-temp-text "* H1<point>"
  1850. (let ((org-blank-before-new-entry '((heading . nil))))
  1851. (org-insert-heading))
  1852. (buffer-string))))
  1853. (should
  1854. (equal "* H1\n* H2\n* "
  1855. (org-test-with-temp-text "* H1\n* H2<point>"
  1856. (let ((org-blank-before-new-entry '((heading . auto))))
  1857. (org-insert-heading))
  1858. (buffer-string))))
  1859. (should
  1860. (equal "* H1\n\n* H2\n\n* "
  1861. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1862. (let ((org-blank-before-new-entry '((heading . auto))))
  1863. (org-insert-heading))
  1864. (buffer-string))))
  1865. ;; Corner case: correctly insert a headline after an empty one.
  1866. (should
  1867. (equal "* \n* "
  1868. (org-test-with-temp-text "* <point>"
  1869. (org-insert-heading)
  1870. (buffer-string))))
  1871. (should
  1872. (org-test-with-temp-text "* <point>\n"
  1873. (org-insert-heading)
  1874. (looking-at-p "\n\\'")))
  1875. ;; Do not insert spurious headlines when inserting a new headline.
  1876. (should
  1877. (equal "* H1\n* H2\n* \n"
  1878. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1879. (org-insert-heading)
  1880. (buffer-string))))
  1881. ;; Preserve visibility at beginning of line. In particular, when
  1882. ;; removing spurious blank lines, do not visually merge heading with
  1883. ;; the line visible above.
  1884. (should-not
  1885. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1886. (org-overview)
  1887. (let ((org-blank-before-new-entry '((heading . nil))))
  1888. (org-insert-heading '(4)))
  1889. (invisible-p (line-end-position 0))))
  1890. ;; Properly handle empty lines when forcing a headline below current
  1891. ;; one.
  1892. (should
  1893. (equal "* H1\n\n* H\n\n* "
  1894. (org-test-with-temp-text "* H1\n\n* H<point>"
  1895. (let ((org-blank-before-new-entry '((heading . t))))
  1896. (org-insert-heading '(4))
  1897. (buffer-string))))))
  1898. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1899. "Test `org-insert-todo-heading-respect-content' specifications."
  1900. ;; Create a TODO heading.
  1901. (should
  1902. (org-test-with-temp-text "* H1\n Body"
  1903. (org-insert-todo-heading-respect-content)
  1904. (nth 2 (org-heading-components))))
  1905. ;; Add headline at the end of the first subtree
  1906. (should
  1907. (equal
  1908. "* TODO "
  1909. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1910. (org-insert-todo-heading-respect-content)
  1911. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1912. ;; In a list, do not create a new item.
  1913. (should
  1914. (equal
  1915. "* TODO "
  1916. (org-test-with-temp-text "* H\n- an item\n- another one"
  1917. (search-forward "an ")
  1918. (org-insert-todo-heading-respect-content)
  1919. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1920. ;; Use the same TODO keyword as current heading.
  1921. (should
  1922. (equal
  1923. "* TODO \n"
  1924. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1925. (org-insert-todo-heading-respect-content)
  1926. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1927. (ert-deftest test-org/clone-with-time-shift ()
  1928. "Test `org-clone-subtree-with-time-shift'."
  1929. ;; Raise an error before first heading.
  1930. (should-error
  1931. (org-test-with-temp-text ""
  1932. (org-clone-subtree-with-time-shift 1)))
  1933. ;; Raise an error on invalid number of clones.
  1934. (should-error
  1935. (org-test-with-temp-text "* Clone me"
  1936. (org-clone-subtree-with-time-shift -1)))
  1937. ;; Clone non-repeating once.
  1938. (should
  1939. (equal "\
  1940. * H1\n<2015-06-21>
  1941. * H1\n<2015-06-23>
  1942. "
  1943. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1944. (org-clone-subtree-with-time-shift 1 "+2d")
  1945. (replace-regexp-in-string
  1946. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1947. nil nil 1))))
  1948. ;; Clone repeating once.
  1949. (should
  1950. (equal "\
  1951. * H1\n<2015-06-21>
  1952. * H1\n<2015-06-23>
  1953. * H1\n<2015-06-25 +1w>
  1954. "
  1955. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1956. (org-clone-subtree-with-time-shift 1 "+2d")
  1957. (replace-regexp-in-string
  1958. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1959. nil nil 1))))
  1960. ;; Clone repeating once in backward.
  1961. (should
  1962. (equal "\
  1963. * H1\n<2015-06-21>
  1964. * H1\n<2015-06-19>
  1965. * H1\n<2015-06-17 +1w>
  1966. "
  1967. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1968. (org-clone-subtree-with-time-shift 1 "-2d")
  1969. (replace-regexp-in-string
  1970. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1971. nil nil 1))))
  1972. ;; Clone non-repeating zero times.
  1973. (should
  1974. (equal "\
  1975. * H1\n<2015-06-21>
  1976. "
  1977. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1978. (org-clone-subtree-with-time-shift 0 "+2d")
  1979. (replace-regexp-in-string
  1980. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1981. nil nil 1))))
  1982. ;; Clone repeating "zero" times.
  1983. (should
  1984. (equal "\
  1985. * H1\n<2015-06-21>
  1986. * H1\n<2015-06-23 +1w>
  1987. "
  1988. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1989. (org-clone-subtree-with-time-shift 0 "+2d")
  1990. (replace-regexp-in-string
  1991. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1992. nil nil 1))))
  1993. ;; Clone with blank SHIFT argument.
  1994. (should
  1995. (string-prefix-p
  1996. "* H <2012-03-29"
  1997. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1998. (org-clone-subtree-with-time-shift 1 "")
  1999. (buffer-substring-no-properties (line-beginning-position 2)
  2000. (line-end-position 2)))))
  2001. ;; Find time stamps before point. If SHIFT is not specified, ask
  2002. ;; for a time shift.
  2003. (should
  2004. (string-prefix-p
  2005. "* H <2012-03-30"
  2006. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  2007. (org-clone-subtree-with-time-shift 1 "+1d")
  2008. (buffer-substring-no-properties (line-beginning-position 2)
  2009. (line-end-position 2)))))
  2010. (should
  2011. (string-prefix-p
  2012. "* H <2014-03-05"
  2013. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  2014. (cl-letf (((symbol-function 'read-from-minibuffer)
  2015. (lambda (&rest _args) "+1d")))
  2016. (org-clone-subtree-with-time-shift 1))
  2017. (buffer-substring-no-properties (line-beginning-position 2)
  2018. (line-end-position 2)))))
  2019. ;; Hour shift.
  2020. (should
  2021. (equal "\
  2022. * H1\n<2015-06-21 20:00>
  2023. * H1\n<2015-06-21 23:00>
  2024. * H1\n<2015-06-22 02:00>
  2025. "
  2026. (org-test-with-temp-text "* H1\n<2015-06-21 Sun 20:00>"
  2027. (org-clone-subtree-with-time-shift 2 "+3h")
  2028. (replace-regexp-in-string
  2029. "\\( [.A-Za-z]+\\)\\( [0-9][0-9]:[0-9][0-9]\\)?>" ""
  2030. (buffer-substring-no-properties (point-min) (point-max))
  2031. nil nil 1))))
  2032. (should
  2033. (equal "\
  2034. * H1\n<2015-06-21 20:00>
  2035. * H1\n<2015-06-21 18:00>
  2036. "
  2037. (org-test-with-temp-text "* H1\n<2015-06-21 Sun 20:00>"
  2038. (org-clone-subtree-with-time-shift 1 "-2h")
  2039. (replace-regexp-in-string
  2040. "\\( [.A-Za-z]+\\)\\( [0-9][0-9]:[0-9][0-9]\\)?>" ""
  2041. (buffer-substring-no-properties (point-min) (point-max))
  2042. nil nil 1)))))
  2043. ;;; Fixed-Width Areas
  2044. (ert-deftest test-org/toggle-fixed-width ()
  2045. "Test `org-toggle-fixed-width' specifications."
  2046. ;; No region: Toggle on fixed-width marker in paragraphs.
  2047. (should
  2048. (equal ": A"
  2049. (org-test-with-temp-text "A"
  2050. (org-toggle-fixed-width)
  2051. (buffer-string))))
  2052. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  2053. (should
  2054. (equal "A"
  2055. (org-test-with-temp-text ": A"
  2056. (org-toggle-fixed-width)
  2057. (buffer-string))))
  2058. ;; No region: Toggle on marker in blank lines after elements or just
  2059. ;; after a headline.
  2060. (should
  2061. (equal "* H\n: "
  2062. (org-test-with-temp-text "* H\n"
  2063. (forward-line)
  2064. (org-toggle-fixed-width)
  2065. (buffer-string))))
  2066. (should
  2067. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  2068. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  2069. (goto-char (point-max))
  2070. (org-toggle-fixed-width)
  2071. (buffer-string))))
  2072. ;; No region: Toggle on marker in front of one line elements (e.g.,
  2073. ;; headlines, clocks)
  2074. (should
  2075. (equal ": * Headline"
  2076. (org-test-with-temp-text "* Headline"
  2077. (org-toggle-fixed-width)
  2078. (buffer-string))))
  2079. (should
  2080. (equal ": #+KEYWORD: value"
  2081. (org-test-with-temp-text "#+KEYWORD: value"
  2082. (org-toggle-fixed-width)
  2083. (buffer-string))))
  2084. ;; No region: error in other situations.
  2085. (should-error
  2086. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  2087. (forward-line)
  2088. (org-toggle-fixed-width)
  2089. (buffer-string)))
  2090. ;; No region: Indentation is preserved.
  2091. (should
  2092. (equal "- A\n : B"
  2093. (org-test-with-temp-text "- A\n B"
  2094. (forward-line)
  2095. (org-toggle-fixed-width)
  2096. (buffer-string))))
  2097. ;; Region: If it contains only fixed-width elements and blank lines,
  2098. ;; toggle off fixed-width markup.
  2099. (should
  2100. (equal "A\n\nB"
  2101. (org-test-with-temp-text ": A\n\n: B"
  2102. (transient-mark-mode 1)
  2103. (push-mark (point) t t)
  2104. (goto-char (point-max))
  2105. (org-toggle-fixed-width)
  2106. (buffer-string))))
  2107. ;; Region: If it contains anything else, toggle on fixed-width but
  2108. ;; not on fixed-width areas.
  2109. (should
  2110. (equal ": A\n: \n: B\n: \n: C"
  2111. (org-test-with-temp-text "A\n\n: B\n\nC"
  2112. (transient-mark-mode 1)
  2113. (push-mark (point) t t)
  2114. (goto-char (point-max))
  2115. (org-toggle-fixed-width)
  2116. (buffer-string))))
  2117. ;; Region: Ignore blank lines at its end, unless it contains only
  2118. ;; such lines.
  2119. (should
  2120. (equal ": A\n\n"
  2121. (org-test-with-temp-text "A\n\n"
  2122. (transient-mark-mode 1)
  2123. (push-mark (point) t t)
  2124. (goto-char (point-max))
  2125. (org-toggle-fixed-width)
  2126. (buffer-string))))
  2127. (should
  2128. (equal ": \n: \n"
  2129. (org-test-with-temp-text "\n\n"
  2130. (transient-mark-mode 1)
  2131. (push-mark (point) t t)
  2132. (goto-char (point-max))
  2133. (org-toggle-fixed-width)
  2134. (buffer-string)))))
  2135. (ert-deftest test-org/kill-line ()
  2136. "Test `org-kill-line' specifications."
  2137. ;; At the beginning of a line, kill whole line.
  2138. (should
  2139. (equal ""
  2140. (org-test-with-temp-text "abc"
  2141. (org-kill-line)
  2142. (buffer-string))))
  2143. ;; In the middle of a line, kill line until its end.
  2144. (should
  2145. (equal "a"
  2146. (org-test-with-temp-text "a<point>bc"
  2147. (org-kill-line)
  2148. (buffer-string))))
  2149. ;; Do not kill newline character.
  2150. (should
  2151. (equal "\n123"
  2152. (org-test-with-temp-text "abc\n123"
  2153. (org-kill-line)
  2154. (buffer-string))))
  2155. (should
  2156. (equal "a\n123"
  2157. (org-test-with-temp-text "a<point>bc\n123"
  2158. (org-kill-line)
  2159. (buffer-string))))
  2160. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  2161. ;; kill until tags.
  2162. (should
  2163. (equal "* A :tag:"
  2164. (org-test-with-temp-text "* A<point>B :tag:"
  2165. (let ((org-special-ctrl-k t)
  2166. (org-tags-column 0))
  2167. (org-kill-line))
  2168. (buffer-string))))
  2169. ;; If point is on tags, only kill part left until the end of line.
  2170. (should
  2171. (equal "* A :tag:"
  2172. (org-test-with-temp-text "* A :tag:<point>tag2:"
  2173. (let ((org-special-ctrl-k t)
  2174. (org-tags-column 0))
  2175. (org-kill-line))
  2176. (buffer-string))))
  2177. ;; However, if point is at the beginning of the line, kill whole
  2178. ;; headline.
  2179. (should
  2180. (equal ""
  2181. (org-test-with-temp-text "* AB :tag:"
  2182. (let ((org-special-ctrl-k t)
  2183. (org-tags-column 0))
  2184. (org-kill-line))
  2185. (buffer-string))))
  2186. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  2187. ;; invisible text, ask before removing it. When set to `error',
  2188. ;; throw an error.
  2189. (should-error
  2190. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  2191. (org-overview)
  2192. (let ((org-special-ctrl-k nil)
  2193. (org-ctrl-k-protect-subtree t))
  2194. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  2195. (org-kill-line)))))
  2196. (should-error
  2197. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  2198. (org-overview)
  2199. (let ((org-special-ctrl-k nil)
  2200. (org-ctrl-k-protect-subtree 'error))
  2201. (org-kill-line)))))
  2202. ;;; Headline
  2203. (ert-deftest test-org/get-heading ()
  2204. "Test `org-get-heading' specifications."
  2205. ;; Return current heading, even if point is not on it.
  2206. (should
  2207. (equal "H"
  2208. (org-test-with-temp-text "* H"
  2209. (org-get-heading))))
  2210. (should
  2211. (equal "H"
  2212. (org-test-with-temp-text "* H\nText<point>"
  2213. (org-get-heading))))
  2214. ;; Without any optional argument, return TODO keyword, priority
  2215. ;; cookie, COMMENT keyword and tags.
  2216. (should
  2217. (equal "TODO H"
  2218. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  2219. (org-get-heading))))
  2220. (should
  2221. (equal "[#A] H"
  2222. (org-test-with-temp-text "* [#A] H"
  2223. (org-get-heading))))
  2224. (should
  2225. (equal "COMMENT H"
  2226. (org-test-with-temp-text "* COMMENT H"
  2227. (org-get-heading))))
  2228. (should
  2229. (equal "H :tag:"
  2230. (org-test-with-temp-text "* H :tag:"
  2231. (org-get-heading))))
  2232. ;; With NO-TAGS argument, ignore tags.
  2233. (should
  2234. (equal "TODO H"
  2235. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  2236. (org-get-heading t))))
  2237. (should
  2238. (equal "H"
  2239. (org-test-with-temp-text "* H :tag:"
  2240. (org-get-heading t))))
  2241. ;; With NO-TODO, ignore TODO keyword.
  2242. (should
  2243. (equal "H"
  2244. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  2245. (org-get-heading nil t))))
  2246. (should
  2247. (equal "H :tag:"
  2248. (org-test-with-temp-text "* H :tag:"
  2249. (org-get-heading nil t))))
  2250. ;; TODO keywords are case-sensitive.
  2251. (should
  2252. (equal "Todo H"
  2253. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  2254. (org-get-heading nil t))))
  2255. ;; With NO-PRIORITY, ignore priority.
  2256. (should
  2257. (equal "H"
  2258. (org-test-with-temp-text "* [#A] H"
  2259. (org-get-heading nil nil t))))
  2260. (should
  2261. (equal "H"
  2262. (org-test-with-temp-text "* H"
  2263. (org-get-heading nil nil t))))
  2264. (should
  2265. (equal "TODO H"
  2266. (org-test-with-temp-text "* TODO [#A] H"
  2267. (org-get-heading nil nil t))))
  2268. ;; With NO-COMMENT, ignore COMMENT keyword.
  2269. (should
  2270. (equal "H"
  2271. (org-test-with-temp-text "* COMMENT H"
  2272. (org-get-heading nil nil nil t))))
  2273. (should
  2274. (equal "H"
  2275. (org-test-with-temp-text "* H"
  2276. (org-get-heading nil nil nil t))))
  2277. (should
  2278. (equal "TODO [#A] H"
  2279. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  2280. (org-get-heading nil nil nil t))))
  2281. ;; On an empty headline, return value is consistent.
  2282. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  2283. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  2284. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  2285. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  2286. (should
  2287. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  2288. (ert-deftest test-org/in-commented-heading-p ()
  2289. "Test `org-in-commented-heading-p' specifications."
  2290. ;; Commented headline.
  2291. (should
  2292. (org-test-with-temp-text "* COMMENT Headline\nBody"
  2293. (goto-char (point-max))
  2294. (org-in-commented-heading-p)))
  2295. ;; Commented ancestor.
  2296. (should
  2297. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  2298. (goto-char (point-max))
  2299. (org-in-commented-heading-p)))
  2300. ;; Comment keyword is case-sensitive.
  2301. (should-not
  2302. (org-test-with-temp-text "* Comment Headline\nBody"
  2303. (goto-char (point-max))
  2304. (org-in-commented-heading-p)))
  2305. ;; Keyword is standalone.
  2306. (should-not
  2307. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  2308. (goto-char (point-max))
  2309. (org-in-commented-heading-p)))
  2310. ;; Optional argument.
  2311. (should-not
  2312. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  2313. (goto-char (point-max))
  2314. (org-in-commented-heading-p t))))
  2315. (ert-deftest test-org/in-archived-heading-p ()
  2316. "Test `org-in-archived-heading-p' specifications."
  2317. ;; Archived headline.
  2318. (should
  2319. (org-test-with-temp-text "* Headline :ARCHIVE:\nBody"
  2320. (goto-char (point-max))
  2321. (org-in-archived-heading-p)))
  2322. ;; Archived ancestor.
  2323. (should
  2324. (org-test-with-temp-text "* Headline :ARCHIVE:\n** Level 2\nBody"
  2325. (goto-char (point-max))
  2326. (org-in-archived-heading-p)))
  2327. ;; Optional argument.
  2328. (should-not
  2329. (org-test-with-temp-text "* Headline :ARCHIVE:\n** Level 2\nBody"
  2330. (goto-char (point-max))
  2331. (org-in-archived-heading-p t)))
  2332. ;; Archive tag containing ARCHIVE as substring
  2333. (should-not
  2334. (org-test-with-temp-text "* Headline :NOARCHIVE:\n** Level 2\nBody"
  2335. (goto-char (point-max))
  2336. (org-in-archived-heading-p))))
  2337. (ert-deftest test-org/entry-blocked-p ()
  2338. ;; Check other dependencies.
  2339. (should
  2340. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  2341. (let ((org-enforce-todo-dependencies t)
  2342. (org-blocker-hook
  2343. '(org-block-todo-from-children-or-siblings-or-parent)))
  2344. (org-entry-blocked-p))))
  2345. (should-not
  2346. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  2347. (let ((org-enforce-todo-dependencies t)
  2348. (org-blocker-hook
  2349. '(org-block-todo-from-children-or-siblings-or-parent)))
  2350. (org-entry-blocked-p))))
  2351. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  2352. ;; blocked.
  2353. (should-not
  2354. (org-test-with-temp-text "* Blocked\n** TODO one"
  2355. (let ((org-enforce-todo-dependencies t)
  2356. (org-blocker-hook
  2357. '(org-block-todo-from-children-or-siblings-or-parent)))
  2358. (org-entry-blocked-p))))
  2359. (should-not
  2360. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  2361. (let ((org-enforce-todo-dependencies t)
  2362. (org-blocker-hook
  2363. '(org-block-todo-from-children-or-siblings-or-parent)))
  2364. (org-entry-blocked-p))))
  2365. ;; Follow :ORDERED: specifications.
  2366. (should
  2367. (org-test-with-temp-text
  2368. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  2369. (let ((org-enforce-todo-dependencies t)
  2370. (org-blocker-hook
  2371. '(org-block-todo-from-children-or-siblings-or-parent)))
  2372. (org-entry-blocked-p))))
  2373. (should-not
  2374. (org-test-with-temp-text
  2375. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  2376. (let ((org-enforce-todo-dependencies t)
  2377. (org-blocker-hook
  2378. '(org-block-todo-from-children-or-siblings-or-parent)))
  2379. (org-entry-blocked-p)))))
  2380. (ert-deftest test-org/get-outline-path ()
  2381. "Test `org-get-outline-path' specifications."
  2382. ;; Top-level headlines have no outline path.
  2383. (should-not
  2384. (org-test-with-temp-text "* H"
  2385. (org-get-outline-path)))
  2386. ;; Otherwise, outline path is the path leading to the headline.
  2387. (should
  2388. (equal '("H")
  2389. (org-test-with-temp-text "* H\n** S<point>"
  2390. (org-get-outline-path))))
  2391. ;; Find path even when point is not on a headline.
  2392. (should
  2393. (equal '("H")
  2394. (org-test-with-temp-text "* H\n** S\nText<point>"
  2395. (org-get-outline-path))))
  2396. ;; TODO keywords, tags and statistics cookies are ignored.
  2397. (should
  2398. (equal '("H")
  2399. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  2400. (org-get-outline-path))))
  2401. ;; Links are replaced with their description or their path.
  2402. (should
  2403. (equal '("Org")
  2404. (org-test-with-temp-text
  2405. "* [[https://orgmode.org][Org]]\n** S<point>"
  2406. (org-get-outline-path))))
  2407. (should
  2408. (equal '("https://orgmode.org")
  2409. (org-test-with-temp-text
  2410. "* [[https://orgmode.org]]\n** S<point>"
  2411. (org-get-outline-path))))
  2412. ;; When WITH-SELF is non-nil, include current heading.
  2413. (should
  2414. (equal '("H")
  2415. (org-test-with-temp-text "* H"
  2416. (org-get-outline-path t))))
  2417. (should
  2418. (equal '("H" "S")
  2419. (org-test-with-temp-text "* H\n** S\nText<point>"
  2420. (org-get-outline-path t))))
  2421. ;; Using cache is transparent to the user.
  2422. (should
  2423. (equal '("H")
  2424. (org-test-with-temp-text "* H\n** S<point>"
  2425. (setq org-outline-path-cache nil)
  2426. (org-get-outline-path nil t))))
  2427. ;; Do not corrupt cache when finding outline path in distant part of
  2428. ;; the buffer.
  2429. (should
  2430. (equal '("H2")
  2431. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  2432. (setq org-outline-path-cache nil)
  2433. (org-get-outline-path nil t)
  2434. (search-forward "S2")
  2435. (org-get-outline-path nil t))))
  2436. ;; Do not choke on empty headlines.
  2437. (should
  2438. (org-test-with-temp-text "* H\n** <point>"
  2439. (org-get-outline-path)))
  2440. (should
  2441. (org-test-with-temp-text "* \n** H<point>"
  2442. (org-get-outline-path))))
  2443. (ert-deftest test-org/format-outline-path ()
  2444. "Test `org-format-outline-path' specifications."
  2445. (should
  2446. (string= (org-format-outline-path (list "one" "two" "three"))
  2447. "one/two/three"))
  2448. ;; Empty path.
  2449. (should
  2450. (string= (org-format-outline-path '())
  2451. ""))
  2452. (should
  2453. (string= (org-format-outline-path '(nil))
  2454. ""))
  2455. ;; Empty path and prefix.
  2456. (should
  2457. (string= (org-format-outline-path '() nil ">>")
  2458. ">>"))
  2459. ;; Trailing whitespace in headings.
  2460. (should
  2461. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  2462. "one/tw o/three"))
  2463. ;; Non-default prefix and separators.
  2464. (should
  2465. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  2466. ">>|one|two|three"))
  2467. ;; Truncate.
  2468. (should
  2469. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  2470. "one/two/.."))
  2471. ;; Give a very narrow width.
  2472. (should
  2473. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  2474. "on"))
  2475. ;; Give a prefix that extends beyond the width.
  2476. (should
  2477. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  2478. ">>>>>>>>>>")
  2479. ">>>>>>>>..")))
  2480. (ert-deftest test-org/org-find-olp ()
  2481. "Test `org-find-olp' specifications."
  2482. (org-test-with-temp-text
  2483. "
  2484. * Headline
  2485. ** COMMENT headline2
  2486. ** TODO headline3
  2487. *** [#A] headline4 :tags:
  2488. ** [#A]headline5
  2489. ** [0%] headline6
  2490. ** headline7 [100%]
  2491. ** headline8 [1/5] :some:more:tags:
  2492. * Test
  2493. "
  2494. (should (org-find-olp '("Headline") t))
  2495. (should-error (org-find-olp '("Headline" "Test") t))
  2496. (should-error (org-find-olp '("Headlinealksjd") t))
  2497. (should (org-find-olp '("Headline" "headline2") t))
  2498. (should (org-find-olp '("Headline" "headline3") t))
  2499. (should (org-find-olp '("Headline" "headline3" "headline4") t))
  2500. (should-error (org-find-olp '("Headline" "headline5") t))
  2501. (should (org-find-olp '("Headline" "headline6") t))
  2502. (should (org-find-olp '("Headline" "headline7") t))
  2503. (should (org-find-olp '("Headline" "headline8") t))))
  2504. (ert-deftest test-org/map-entries ()
  2505. "Test `org-map-entries' specifications."
  2506. ;; Full match.
  2507. (should
  2508. (equal '(1 11)
  2509. (org-test-with-temp-text "* Level 1\n** Level 2"
  2510. (org-map-entries #'point))))
  2511. ;; Level match.
  2512. (should
  2513. (equal '(1)
  2514. (org-test-with-temp-text "* Level 1\n** Level 2"
  2515. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  2516. (should
  2517. (equal '(11)
  2518. (org-test-with-temp-text "* Level 1\n** Level 2"
  2519. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  2520. ;; Tag match.
  2521. (should
  2522. (equal '(11)
  2523. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  2524. (org-map-entries #'point "yes"))))
  2525. (should
  2526. (equal '(14)
  2527. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  2528. (org-map-entries #'point "+yes-a"))))
  2529. (should
  2530. (equal '(11 23)
  2531. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  2532. (org-map-entries #'point "{yes?}"))))
  2533. ;; Priority match.
  2534. (should
  2535. (equal '(1)
  2536. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  2537. (org-map-entries #'point "PRIORITY=\"A\""))))
  2538. ;; Date match.
  2539. (should
  2540. (equal '(36)
  2541. (org-test-with-temp-text "
  2542. * H1
  2543. SCHEDULED: <2012-03-29 thu.>
  2544. * H2
  2545. SCHEDULED: <2014-03-04 tue.>"
  2546. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  2547. (should
  2548. (equal '(2)
  2549. (org-test-with-temp-text "
  2550. * H1
  2551. SCHEDULED: <2012-03-29 thu.>
  2552. * H2
  2553. SCHEDULED: <2014-03-04 tue.>"
  2554. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  2555. ;; Regular property match.
  2556. (should
  2557. (equal '(2)
  2558. (org-test-with-temp-text "
  2559. * H1
  2560. :PROPERTIES:
  2561. :TEST: 1
  2562. :END:
  2563. * H2
  2564. :PROPERTIES:
  2565. :TEST: 2
  2566. :END:"
  2567. (org-map-entries #'point "TEST=1"))))
  2568. ;; Multiple criteria.
  2569. (should
  2570. (equal '(23)
  2571. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  2572. (let (org-odd-levels-only
  2573. (org-use-tag-inheritance nil))
  2574. (org-map-entries #'point "yes+LEVEL=1")))))
  2575. ;; "or" criteria.
  2576. (should
  2577. (equal '(12 24)
  2578. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  2579. (let (org-odd-levels-only)
  2580. (org-map-entries #'point "LEVEL=2|no")))))
  2581. (should
  2582. (equal '(1 12)
  2583. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  2584. (let (org-odd-levels-only)
  2585. (org-map-entries #'point "yes|no")))))
  2586. ;; "and" criteria.
  2587. (should
  2588. (equal '(22)
  2589. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  2590. (let (org-odd-levels-only)
  2591. (org-map-entries #'point "yes&no")))))
  2592. ;; Setting `org-map-continue-from'
  2593. (should
  2594. (string= ""
  2595. (org-test-with-temp-text "* H1\n* H2\n* H3n* H4"
  2596. (org-map-entries
  2597. (lambda ()
  2598. (org-cut-subtree)
  2599. (setq org-map-continue-from (point))))
  2600. (buffer-string))))
  2601. (should
  2602. (string= "* H1\n* H2\n* H3\n"
  2603. (org-test-with-temp-text "* H1\n* H2\n* H3\n* H4"
  2604. (org-map-entries
  2605. (lambda ()
  2606. (when (string= "H4"
  2607. (org-element-property
  2608. :raw-value (org-element-at-point)))
  2609. (org-cut-subtree)
  2610. (setq org-map-continue-from
  2611. (org-element-property
  2612. :begin (org-element-at-point))))))
  2613. (buffer-string)))))
  2614. (ert-deftest test-org/edit-headline ()
  2615. "Test `org-edit-headline' specifications."
  2616. (should
  2617. (equal "* B"
  2618. (org-test-with-temp-text "* A"
  2619. (org-edit-headline "B")
  2620. (buffer-string))))
  2621. ;; Handle empty headings.
  2622. (should
  2623. (equal "* "
  2624. (org-test-with-temp-text "* A"
  2625. (org-edit-headline "")
  2626. (buffer-string))))
  2627. (should
  2628. (equal "* A"
  2629. (org-test-with-temp-text "* "
  2630. (org-edit-headline "A")
  2631. (buffer-string))))
  2632. ;; Handle TODO keywords and priority cookies.
  2633. (should
  2634. (equal "* TODO B"
  2635. (org-test-with-temp-text "* TODO A"
  2636. (org-edit-headline "B")
  2637. (buffer-string))))
  2638. (should
  2639. (equal "* [#A] B"
  2640. (org-test-with-temp-text "* [#A] A"
  2641. (org-edit-headline "B")
  2642. (buffer-string))))
  2643. (should
  2644. (equal "* TODO [#A] B"
  2645. (org-test-with-temp-text "* TODO [#A] A"
  2646. (org-edit-headline "B")
  2647. (buffer-string))))
  2648. ;; Handle tags.
  2649. (equal "* B :tag:"
  2650. (org-test-with-temp-text "* A :tag:"
  2651. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2652. (buffer-string))))
  2653. ;;; Keywords
  2654. (ert-deftest test-org/set-regexps-and-options ()
  2655. "Test `org-set-regexps-and-options' specifications."
  2656. ;; TAGS keyword.
  2657. (should
  2658. (equal '(("A"))
  2659. (let ((org-tag-alist '(("A")))
  2660. (org-tag-persistent-alist nil))
  2661. (org-test-with-temp-text ""
  2662. (org-mode-restart)
  2663. org-current-tag-alist))))
  2664. (should
  2665. (equal '(("B"))
  2666. (let ((org-tag-alist '(("A")))
  2667. (org-tag-persistent-alist nil))
  2668. (org-test-with-temp-text "#+TAGS: B"
  2669. (org-mode-restart)
  2670. org-current-tag-alist))))
  2671. (should
  2672. (equal '(("C") ("B"))
  2673. (let ((org-tag-alist '(("A")))
  2674. (org-tag-persistent-alist '(("C"))))
  2675. (org-test-with-temp-text "#+TAGS: B"
  2676. (org-mode-restart)
  2677. org-current-tag-alist))))
  2678. (should
  2679. (equal '(("B"))
  2680. (let ((org-tag-alist '(("A")))
  2681. (org-tag-persistent-alist '(("C"))))
  2682. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2683. (org-mode-restart)
  2684. org-current-tag-alist))))
  2685. (should
  2686. (equal '(("A" . ?a) ("B") ("C"))
  2687. (let ((org-tag-persistent-alist nil))
  2688. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2689. (org-mode-restart)
  2690. org-current-tag-alist))))
  2691. (should
  2692. (equal '(("A") (:newline) ("B"))
  2693. (let ((org-tag-persistent-alist nil))
  2694. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2695. (org-mode-restart)
  2696. org-current-tag-alist))))
  2697. (should
  2698. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2699. (let ((org-tag-persistent-alist nil))
  2700. (org-test-with-temp-text "#+TAGS: { A B } C"
  2701. (org-mode-restart)
  2702. org-current-tag-alist))))
  2703. (should
  2704. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2705. (let ((org-tag-persistent-alist nil))
  2706. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2707. (org-mode-restart)
  2708. org-current-tag-alist))))
  2709. (should
  2710. (equal '(("A" "B" "C"))
  2711. (let ((org-tag-persistent-alist nil))
  2712. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2713. (org-mode-restart)
  2714. org-tag-groups-alist))))
  2715. (should
  2716. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2717. (let ((org-tag-persistent-alist nil))
  2718. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2719. (org-mode-restart)
  2720. org-current-tag-alist))))
  2721. (should
  2722. (equal '(("A" "B" "C"))
  2723. (let ((org-tag-persistent-alist nil))
  2724. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2725. (org-mode-restart)
  2726. org-tag-groups-alist))))
  2727. (should-not
  2728. (let ((org-tag-alist '(("A"))))
  2729. (org-test-with-temp-text "#+TAGS:"
  2730. (org-mode-restart)
  2731. org-current-tag-alist)))
  2732. ;; FILETAGS keyword.
  2733. (should
  2734. (equal '("A" "B" "C")
  2735. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2736. (org-mode-restart)
  2737. org-file-tags)))
  2738. ;; PROPERTY keyword. Property names are case-insensitive.
  2739. (should
  2740. (equal "foo=1"
  2741. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2742. (org-mode-restart)
  2743. (cdr (assoc "var" org-keyword-properties)))))
  2744. (should
  2745. (equal
  2746. "foo=1 bar=2"
  2747. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2748. (org-mode-restart)
  2749. (cdr (assoc "var" org-keyword-properties)))))
  2750. (should
  2751. (equal
  2752. "foo=1 bar=2"
  2753. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2754. (org-mode-restart)
  2755. (cdr (assoc "var" org-keyword-properties)))))
  2756. ;; ARCHIVE keyword.
  2757. (should
  2758. (equal "%s_done::"
  2759. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2760. (org-mode-restart)
  2761. org-archive-location)))
  2762. ;; CATEGORY keyword.
  2763. (should
  2764. (eq 'test
  2765. (org-test-with-temp-text "#+CATEGORY: test"
  2766. (org-mode-restart)
  2767. org-category)))
  2768. (should
  2769. (equal "test"
  2770. (org-test-with-temp-text "#+CATEGORY: test"
  2771. (org-mode-restart)
  2772. (cdr (assoc "CATEGORY" org-keyword-properties)))))
  2773. ;; COLUMNS keyword.
  2774. (should
  2775. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2776. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2777. (org-mode-restart)
  2778. org-columns-default-format)))
  2779. ;; CONSTANTS keyword. Constants names are case sensitive.
  2780. (should
  2781. (equal '("299792458." "3.14")
  2782. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2783. (org-mode-restart)
  2784. (mapcar
  2785. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2786. '("c" "pi")))))
  2787. (should
  2788. (equal "3.14"
  2789. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2790. (org-mode-restart)
  2791. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2792. (should
  2793. (equal "22/7"
  2794. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2795. (org-mode-restart)
  2796. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2797. ;; LINK keyword.
  2798. (should
  2799. (equal
  2800. '("url1" "url2")
  2801. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2802. (org-mode-restart)
  2803. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2804. '("a" "b")))))
  2805. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2806. (should
  2807. (equal
  2808. '(?X ?Z ?Y)
  2809. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2810. (org-mode-restart)
  2811. (list org-priority-highest org-priority-lowest org-priority-default))))
  2812. (should
  2813. (equal
  2814. '(?A ?C ?B)
  2815. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2816. (org-mode-restart)
  2817. (list org-priority-highest org-priority-lowest org-priority-default))))
  2818. ;; STARTUP keyword.
  2819. (should
  2820. (equal '(t t)
  2821. (org-test-with-temp-text "#+STARTUP: fold odd"
  2822. (org-mode-restart)
  2823. (list org-startup-folded org-odd-levels-only))))
  2824. ;; TODO keywords.
  2825. (should
  2826. (equal '(("A" "B") ("C"))
  2827. (org-test-with-temp-text "#+TODO: A B | C"
  2828. (org-mode-restart)
  2829. (list org-not-done-keywords org-done-keywords))))
  2830. (should
  2831. (equal '(("A" "C") ("B" "D"))
  2832. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2833. (org-mode-restart)
  2834. (list org-not-done-keywords org-done-keywords))))
  2835. (should
  2836. (equal '(("A" "B") ("C"))
  2837. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2838. (org-mode-restart)
  2839. (list org-not-done-keywords org-done-keywords))))
  2840. (should
  2841. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2842. (org-test-with-temp-text "#+TODO: A(a)"
  2843. (org-mode-restart)
  2844. org-todo-key-alist)))
  2845. (should
  2846. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2847. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2848. (org-mode-restart)
  2849. org-todo-log-states)))
  2850. ;; Enter SETUPFILE keyword.
  2851. (should
  2852. (equal "1"
  2853. (org-test-with-temp-text
  2854. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2855. (org-mode-restart)
  2856. (cdr (assoc "a" org-keyword-properties))))))
  2857. (ert-deftest test-org/collect-keywords ()
  2858. "Test `org-collect-keywords'."
  2859. (should-not
  2860. (org-test-with-temp-text "#+begin_example\n#+foo: bar\n#+end_example"
  2861. (org-collect-keywords '("FOO")))))
  2862. ;;; Links
  2863. ;;;; Coderefs
  2864. (ert-deftest test-org/coderef ()
  2865. "Test coderef links specifications."
  2866. (should
  2867. (org-test-with-temp-text "
  2868. #+BEGIN_SRC emacs-lisp
  2869. \(+ 1 1) (ref:sc)
  2870. #+END_SRC
  2871. \[[(sc)<point>]]"
  2872. (org-open-at-point)
  2873. (looking-at "(ref:sc)")))
  2874. ;; Find coderef even with alternate label format.
  2875. (should
  2876. (org-test-with-temp-text "
  2877. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2878. \(+ 1 1) {ref:sc}
  2879. #+END_SRC
  2880. \[[(sc)<point>]]"
  2881. (org-open-at-point)
  2882. (looking-at "{ref:sc}"))))
  2883. ;;;; Custom ID
  2884. (ert-deftest test-org/custom-id ()
  2885. "Test custom ID links specifications."
  2886. (should
  2887. (org-test-with-temp-text
  2888. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2889. (org-open-at-point)
  2890. (looking-at-p "\\* H1")))
  2891. ;; Handle escape characters.
  2892. (should
  2893. (org-test-with-temp-text
  2894. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#\\[%\\]<point>]]"
  2895. (org-open-at-point)
  2896. (looking-at-p "\\* H1")))
  2897. ;; Throw an error on false positives.
  2898. (should-error
  2899. (org-test-with-temp-text
  2900. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2901. (org-open-at-point)
  2902. (looking-at-p "\\* H1"))))
  2903. ;;;; Fuzzy Links
  2904. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2905. ;; a named element (#+name: text) and to headlines (* Text).
  2906. (ert-deftest test-org/fuzzy-links ()
  2907. "Test fuzzy links specifications."
  2908. ;; Fuzzy link goes in priority to a matching target.
  2909. (should
  2910. (org-test-with-temp-text
  2911. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2912. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2913. (looking-at "<<Test>>")))
  2914. ;; Then fuzzy link points to an element with a given name.
  2915. (should
  2916. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2917. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2918. (looking-at "#\\+NAME: Test")))
  2919. ;; A target still lead to a matching headline otherwise.
  2920. (should
  2921. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2922. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2923. (looking-at "\\* Head2")))
  2924. ;; With a leading star in link, enforce heading match.
  2925. (should
  2926. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2927. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2928. (looking-at "\\* Test")))
  2929. ;; With a leading star in link, enforce exact heading match, even
  2930. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2931. (should-error
  2932. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2933. (let ((org-link-search-must-match-exact-headline nil))
  2934. (org-open-at-point))))
  2935. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2936. (should
  2937. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2938. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2939. (looking-at "\\* Test")))
  2940. (should
  2941. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2942. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2943. (looking-at "\\* Test")))
  2944. ;; Heading match should not care about spaces, cookies, TODO
  2945. ;; keywords, priorities, and tags. However, TODO keywords are
  2946. ;; case-sensitive.
  2947. (should
  2948. (let ((first-line
  2949. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2950. (org-test-with-temp-text
  2951. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2952. (let ((org-link-search-must-match-exact-headline nil)
  2953. (org-todo-regexp "TODO"))
  2954. (org-open-at-point))
  2955. (looking-at (regexp-quote first-line)))))
  2956. (should-error
  2957. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2958. (let ((org-link-search-must-match-exact-headline nil)
  2959. (org-todo-regexp "TODO"))
  2960. (org-open-at-point))))
  2961. ;; Heading match should still be exact.
  2962. (should-error
  2963. (org-test-with-temp-text "
  2964. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2965. Foo Bar
  2966. <point>[[*Test 1]]"
  2967. (let ((org-link-search-must-match-exact-headline nil)
  2968. (org-todo-regexp "TODO"))
  2969. (org-open-at-point))))
  2970. (should
  2971. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2972. (let ((org-link-search-must-match-exact-headline nil)
  2973. (org-todo-regexp "TODO"))
  2974. (org-open-at-point))
  2975. (looking-at-p (regexp-quote "** Test 1 2"))))
  2976. ;; Heading match ignores COMMENT keyword.
  2977. (should
  2978. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2979. (org-open-at-point)
  2980. (looking-at "\\* COMMENT Test")))
  2981. (should
  2982. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2983. (org-open-at-point)
  2984. (looking-at "\\* TODO COMMENT Test")))
  2985. ;; Correctly un-escape fuzzy links.
  2986. (should
  2987. (org-test-with-temp-text "* [foo]\n[[*\\[foo\\]][With escaped characters]]"
  2988. (org-open-at-point)
  2989. (bobp)))
  2990. ;; Match search strings containing newline characters, including
  2991. ;; blank lines.
  2992. (should
  2993. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2994. (let ((file (buffer-file-name)))
  2995. (goto-char (point-max))
  2996. (insert (format "[[file:%s::line1 line2]]" file))
  2997. (beginning-of-line)
  2998. (let ((org-link-search-must-match-exact-headline nil))
  2999. (org-open-at-point 0))
  3000. (looking-at-p "line1"))))
  3001. (should
  3002. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  3003. (let ((file (buffer-file-name)))
  3004. (goto-char (point-max))
  3005. (insert (format "[[file:%s::line1 line2]]" file))
  3006. (beginning-of-line)
  3007. (let ((org-link-search-must-match-exact-headline nil))
  3008. (org-open-at-point 0))
  3009. (looking-at-p "line1")))))
  3010. ;;;; Open at point
  3011. (ert-deftest test-org/open-at-point/keyword ()
  3012. "Does `org-open-at-point' open link in a keyword line?"
  3013. (should
  3014. (org-test-with-temp-text
  3015. "<<top>>\n#+KEYWORD: <point>[[top]]"
  3016. (org-open-at-point) t))
  3017. (should
  3018. (org-test-with-temp-text
  3019. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  3020. (org-open-at-point) t)))
  3021. (ert-deftest test-org/open-at-point/property ()
  3022. "Does `org-open-at-point' open link in property drawer?"
  3023. (should
  3024. (org-test-with-temp-text
  3025. "* Headline
  3026. :PROPERTIES:
  3027. :URL: <point>[[*Headline]]
  3028. :END:"
  3029. (org-open-at-point) t)))
  3030. (ert-deftest test-org/open-at-point/comment ()
  3031. "Does `org-open-at-point' open link in a commented line?"
  3032. (should
  3033. (org-test-with-temp-text
  3034. "<<top>>\n# <point>[[top]]"
  3035. (org-open-at-point) t))
  3036. (should
  3037. (org-test-with-temp-text
  3038. "* H\n<<top>>\n# <point>[[top]]"
  3039. (org-open-at-point) t)))
  3040. (ert-deftest test-org/open-at-point/inline-image ()
  3041. "Test `org-open-at-point' on nested links."
  3042. (should
  3043. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  3044. (org-open-at-point)
  3045. (bobp))))
  3046. (ert-deftest test-org/open-at-point/radio-target ()
  3047. "Test `org-open-at-point' on radio targets."
  3048. (should
  3049. (org-test-with-temp-text "<<<target>>> <point>target"
  3050. (org-update-radio-target-regexp)
  3051. (org-open-at-point)
  3052. (eq (org-element-type (org-element-context)) 'radio-target))))
  3053. (ert-deftest test-org/open-at-point/radio-target-shadowed ()
  3054. "Test `org-open-at-point' on shadowed radio targets."
  3055. (should
  3056. (org-test-with-temp-text
  3057. "<<<target shadowed>>> <<<target>>> <point>target shadowed"
  3058. (org-update-radio-target-regexp)
  3059. (org-open-at-point)
  3060. (string=
  3061. (org-element-property :value
  3062. (org-element-radio-target-parser))
  3063. "target shadowed"))))
  3064. (ert-deftest test-org/open-at-point/tag ()
  3065. "Test `org-open-at-point' on tags."
  3066. (should
  3067. (org-test-with-temp-text "* H :<point>tag:"
  3068. (catch :result
  3069. (cl-letf (((symbol-function 'org-tags-view)
  3070. (lambda (&rest _args) (throw :result t))))
  3071. (org-open-at-point)
  3072. nil))))
  3073. (should-not
  3074. (org-test-with-temp-text-in-file "* H<point> :tag:"
  3075. (catch :result
  3076. (cl-letf (((symbol-function 'org-tags-view)
  3077. (lambda (&rest _args) (throw :result t))))
  3078. ;; When point isn't on a tag it's going to try other things,
  3079. ;; possibly trying to open attachments which will return an
  3080. ;; error if there isn't an attachment. Suppress that error.
  3081. (ignore-errors
  3082. (org-open-at-point))
  3083. nil)))))
  3084. ;;; Node Properties
  3085. (ert-deftest test-org/accumulated-properties-in-drawers ()
  3086. "Ensure properties accumulate in subtree drawers."
  3087. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  3088. (org-babel-next-src-block)
  3089. (should (equal '(2 1) (org-babel-execute-src-block)))))
  3090. (ert-deftest test-org/custom-properties ()
  3091. "Test custom properties specifications."
  3092. ;; Standard test.
  3093. (should
  3094. (let ((org-custom-properties '("FOO")))
  3095. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  3096. (org-toggle-custom-properties-visibility)
  3097. (org-invisible-p2))))
  3098. ;; Properties are case-insensitive.
  3099. (should
  3100. (let ((org-custom-properties '("FOO")))
  3101. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  3102. (org-toggle-custom-properties-visibility)
  3103. (org-invisible-p2))))
  3104. (should
  3105. (let ((org-custom-properties '("foo")))
  3106. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  3107. (org-toggle-custom-properties-visibility)
  3108. (org-invisible-p2))))
  3109. ;; Multiple custom properties in the same drawer.
  3110. (should
  3111. (let ((org-custom-properties '("FOO" "BAR")))
  3112. (org-test-with-temp-text
  3113. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  3114. (org-fold-reveal)
  3115. (org-toggle-custom-properties-visibility)
  3116. (and (org-invisible-p2)
  3117. (not (progn (forward-line) (org-invisible-p2)))
  3118. (progn (forward-line) (org-invisible-p2))))))
  3119. ;; Hide custom properties with an empty value.
  3120. (should
  3121. (let ((org-custom-properties '("FOO")))
  3122. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  3123. (org-toggle-custom-properties-visibility)
  3124. (org-invisible-p2))))
  3125. ;; Do not hide fake properties.
  3126. (should-not
  3127. (let ((org-custom-properties '("FOO")))
  3128. (org-test-with-temp-text ":FOO: val\n"
  3129. (org-toggle-custom-properties-visibility)
  3130. (org-invisible-p2))))
  3131. (should-not
  3132. (let ((org-custom-properties '("A")))
  3133. (org-test-with-temp-text
  3134. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  3135. (org-fold-reveal)
  3136. (org-toggle-custom-properties-visibility)
  3137. (org-invisible-p2)))))
  3138. ;;; Mark Region
  3139. (ert-deftest test-org/mark-element ()
  3140. "Test `org-mark-element' specifications."
  3141. ;; Mark beginning and end of element.
  3142. (should
  3143. (equal '(t t)
  3144. (org-test-with-temp-text "Para<point>graph"
  3145. (org-mark-element)
  3146. (list (bobp) (= (mark) (point-max))))))
  3147. (should
  3148. (equal '(t t)
  3149. (org-test-with-temp-text "P1\n\nPara<point>graph\n\nP2"
  3150. (org-mark-element)
  3151. (list (looking-at "Paragraph")
  3152. (org-with-point-at (mark) (looking-at "P2"))))))
  3153. ;; Do not set mark past (point-max).
  3154. (should
  3155. (org-test-with-temp-text "Para<point>graph"
  3156. (narrow-to-region 2 6)
  3157. (org-mark-element)
  3158. (= 6 (mark)))))
  3159. (ert-deftest test-org/mark-subtree ()
  3160. "Test `org-mark-subtree' specifications."
  3161. ;; Error when point is before first headline.
  3162. (should-error
  3163. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  3164. (progn (transient-mark-mode 1)
  3165. (org-mark-subtree))))
  3166. ;; Without argument, mark current subtree.
  3167. (should
  3168. (equal
  3169. '(12 32)
  3170. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  3171. (progn (transient-mark-mode 1)
  3172. (forward-line 2)
  3173. (org-mark-subtree)
  3174. (list (region-beginning) (region-end))))))
  3175. ;; With an argument, move ARG up.
  3176. (should
  3177. (equal
  3178. '(1 32)
  3179. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  3180. (progn (transient-mark-mode 1)
  3181. (forward-line 2)
  3182. (org-mark-subtree 1)
  3183. (list (region-beginning) (region-end))))))
  3184. ;; Do not get fooled by inlinetasks.
  3185. (when (featurep 'org-inlinetask)
  3186. (should
  3187. (= 1
  3188. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  3189. (progn (transient-mark-mode 1)
  3190. (forward-line 1)
  3191. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  3192. (region-beginning)))))))
  3193. ;;; Miscellaneous
  3194. (ert-deftest test-org/sort-entries ()
  3195. "Test `org-sort-entries'."
  3196. ;; Sort alphabetically.
  3197. (should
  3198. (equal "\n* abc\n* def\n* xyz\n"
  3199. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  3200. (org-sort-entries nil ?a)
  3201. (buffer-string))))
  3202. (should
  3203. (equal "\n* xyz\n* def\n* abc\n"
  3204. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  3205. (org-sort-entries nil ?A)
  3206. (buffer-string))))
  3207. (should
  3208. (equal "\n* \n* klm\n* xyz\n"
  3209. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  3210. (org-sort-entries nil ?a)
  3211. (buffer-string))))
  3212. ;; Sort numerically.
  3213. (should
  3214. (equal "\n* 1\n* 2\n* 10\n"
  3215. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  3216. (org-sort-entries nil ?n)
  3217. (buffer-string))))
  3218. (should
  3219. (equal "\n* 10\n* 2\n* 1\n"
  3220. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  3221. (org-sort-entries nil ?N)
  3222. (buffer-string))))
  3223. (should
  3224. (equal "\n* \n* 1\n* 2\n"
  3225. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  3226. (org-sort-entries nil ?n)
  3227. (buffer-string))))
  3228. ;; Sort by custom function.
  3229. (should
  3230. (equal "\n* b\n* aa\n* ccc\n"
  3231. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  3232. (org-sort-entries nil ?f
  3233. (lambda ()
  3234. (length (buffer-substring (point-at-bol)
  3235. (point-at-eol))))
  3236. #'<)
  3237. (buffer-string))))
  3238. (should
  3239. (equal "\n* ccc\n* aa\n* b\n"
  3240. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  3241. (org-sort-entries nil ?F
  3242. (lambda ()
  3243. (length (buffer-substring (point-at-bol)
  3244. (point-at-eol))))
  3245. #'<)
  3246. (buffer-string))))
  3247. ;; Sort by TODO keyword.
  3248. (should
  3249. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  3250. (org-test-with-temp-text
  3251. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  3252. (org-sort-entries nil ?o)
  3253. (buffer-string))))
  3254. (should
  3255. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  3256. (org-test-with-temp-text
  3257. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  3258. (org-sort-entries nil ?O)
  3259. (buffer-string))))
  3260. ;; Sort by priority.
  3261. (should
  3262. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  3263. (org-test-with-temp-text
  3264. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  3265. (org-sort-entries nil ?p)
  3266. (buffer-string))))
  3267. (should
  3268. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  3269. (org-test-with-temp-text
  3270. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  3271. (org-sort-entries nil ?P)
  3272. (buffer-string))))
  3273. ;; Sort by creation time.
  3274. (should
  3275. (equal "
  3276. * h3
  3277. [2017-05-08 Mon]
  3278. * h2
  3279. [2017-05-09 Tue]
  3280. * h1
  3281. [2018-05-09 Wed]
  3282. "
  3283. (org-test-with-temp-text
  3284. "
  3285. * h1
  3286. [2018-05-09 Wed]
  3287. * h2
  3288. [2017-05-09 Tue]
  3289. * h3
  3290. [2017-05-08 Mon]
  3291. "
  3292. (org-sort-entries nil ?c)
  3293. (buffer-string))))
  3294. ;; Sort by scheduled date.
  3295. (should
  3296. (equal "
  3297. * TODO h4
  3298. SCHEDULED: <2017-05-06 Sat>
  3299. * TODO h3
  3300. SCHEDULED: <2017-05-08 Mon>
  3301. * TODO h2
  3302. DEADLINE: <2017-05-09 Tue>
  3303. * TODO h1
  3304. DEADLINE: <2017-05-07 Sun>
  3305. "
  3306. (org-test-with-temp-text
  3307. "
  3308. * TODO h2
  3309. DEADLINE: <2017-05-09 Tue>
  3310. * TODO h1
  3311. DEADLINE: <2017-05-07 Sun>
  3312. * TODO h3
  3313. SCHEDULED: <2017-05-08 Mon>
  3314. * TODO h4
  3315. SCHEDULED: <2017-05-06 Sat>
  3316. "
  3317. (org-sort-entries nil ?s)
  3318. (buffer-string))))
  3319. ;; Sort by deadline date.
  3320. (should
  3321. (equal "
  3322. * TODO h1
  3323. DEADLINE: <2017-05-07 Sun>
  3324. * TODO h2
  3325. DEADLINE: <2017-05-09 Tue>
  3326. * TODO h3
  3327. SCHEDULED: <2017-05-08 Mon>
  3328. * TODO h4
  3329. SCHEDULED: <2017-05-06 Sat>
  3330. "
  3331. (org-test-with-temp-text
  3332. "
  3333. * TODO h2
  3334. DEADLINE: <2017-05-09 Tue>
  3335. * TODO h1
  3336. DEADLINE: <2017-05-07 Sun>
  3337. * TODO h3
  3338. SCHEDULED: <2017-05-08 Mon>
  3339. * TODO h4
  3340. SCHEDULED: <2017-05-06 Sat>
  3341. "
  3342. (org-sort-entries nil ?d)
  3343. (buffer-string))))
  3344. ;; Sort by any date/time
  3345. (should
  3346. (equal "
  3347. * TODO h4
  3348. SCHEDULED: <2017-05-06 Sat>
  3349. * TODO h1
  3350. DEADLINE: <2017-05-07 Sun>
  3351. * TODO h3
  3352. SCHEDULED: <2017-05-08 Mon>
  3353. * TODO h2
  3354. DEADLINE: <2017-05-09 Tue>
  3355. "
  3356. (org-test-with-temp-text
  3357. "
  3358. * TODO h2
  3359. DEADLINE: <2017-05-09 Tue>
  3360. * TODO h1
  3361. DEADLINE: <2017-05-07 Sun>
  3362. * TODO h3
  3363. SCHEDULED: <2017-05-08 Mon>
  3364. * TODO h4
  3365. SCHEDULED: <2017-05-06 Sat>
  3366. "
  3367. (org-sort-entries nil ?t)
  3368. (buffer-string))))
  3369. ;; Sort by clocking time.
  3370. (should
  3371. (equal "
  3372. * clocked h2
  3373. :LOGBOOK:
  3374. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3375. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  3376. :END:
  3377. * clocked h1
  3378. :LOGBOOK:
  3379. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3380. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  3381. :END:
  3382. "
  3383. (org-test-with-temp-text
  3384. "
  3385. * clocked h1
  3386. :LOGBOOK:
  3387. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3388. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  3389. :END:
  3390. * clocked h2
  3391. :LOGBOOK:
  3392. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3393. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  3394. :END:
  3395. "
  3396. (org-sort-entries nil ?k)
  3397. (buffer-string))))
  3398. ;; Preserve file local variables when sorting.
  3399. (should
  3400. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  3401. (org-test-with-temp-text
  3402. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  3403. (org-sort-entries nil ?a)
  3404. (buffer-string)))))
  3405. (ert-deftest test-org/string-collate-greaterp ()
  3406. "Test `org-string-collate-greaterp' specifications."
  3407. (should (org-string-collate-greaterp "def" "abc"))
  3408. (should-not (org-string-collate-greaterp "abc" "def")))
  3409. (ert-deftest test-org/file-contents ()
  3410. "Test `org-file-contents' specifications."
  3411. ;; Open files.
  3412. (should
  3413. (string= "#+BIND: variable value
  3414. #+DESCRIPTION: l2
  3415. #+LANGUAGE: en
  3416. #+SELECT_TAGS: b
  3417. #+TITLE: b
  3418. #+PROPERTY: a 1
  3419. " (org-file-contents (expand-file-name "setupfile3.org"
  3420. (concat org-test-dir "examples/")))))
  3421. ;; Throw error when trying to access an invalid file.
  3422. (should-error (org-file-contents "this-file-must-not-exist"))
  3423. ;; Try to access an invalid file, but do not throw an error.
  3424. (should
  3425. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  3426. ;; Open URL.
  3427. (should
  3428. (let ((org-resource-download-policy t))
  3429. (string= "foo"
  3430. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3431. (unwind-protect
  3432. ;; Simulate successful retrieval of a URL.
  3433. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3434. (lambda (&rest_)
  3435. (with-current-buffer buffer
  3436. (insert "HTTP/1.1 200 OK\n\nfoo"))
  3437. buffer)))
  3438. (org-file-contents "http://some-valid-url"))
  3439. (kill-buffer buffer))))))
  3440. ;; Throw error when trying to access an invalid URL.
  3441. (should-error
  3442. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3443. (unwind-protect
  3444. ;; Simulate unsuccessful retrieval of a URL.
  3445. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3446. (lambda (&rest_)
  3447. (with-current-buffer buffer
  3448. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3449. buffer)))
  3450. (org-file-contents "http://this-url-must-not-exist"))
  3451. (kill-buffer buffer))))
  3452. ;; Try to access an invalid URL, but do not throw an error.
  3453. (should-error
  3454. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3455. (unwind-protect
  3456. ;; Simulate unsuccessful retrieval of a URL.
  3457. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3458. (lambda (&rest_)
  3459. (with-current-buffer buffer
  3460. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3461. buffer)))
  3462. (org-file-contents "http://this-url-must-not-exist"))
  3463. (kill-buffer buffer))))
  3464. (should
  3465. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3466. (unwind-protect
  3467. ;; Simulate unsuccessful retrieval of a URL.
  3468. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3469. (lambda (&rest_)
  3470. (with-current-buffer buffer
  3471. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3472. buffer)))
  3473. (org-file-contents "http://this-url-must-not-exist" :noerror))
  3474. (kill-buffer buffer))
  3475. t)))
  3476. ;;; Navigation
  3477. (ert-deftest test-org/next-visible-heading ()
  3478. "Test `org-next-visible-heading' specifications."
  3479. ;; Move to the beginning of the next headline, taking into
  3480. ;; consideration ARG.
  3481. (should
  3482. (org-test-with-temp-text "* H1\n* H2"
  3483. (org-next-visible-heading 1)
  3484. (looking-at "\\* H2")))
  3485. (should
  3486. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3487. (org-next-visible-heading 2)
  3488. (looking-at "\\* H3")))
  3489. ;; Ignore invisible headlines.
  3490. (should
  3491. (org-test-with-temp-text "* H1\n** H2\n* H3"
  3492. (org-cycle)
  3493. (org-next-visible-heading 1)
  3494. (looking-at "\\* H3")))
  3495. ;; Move point between headlines, not on blank lines between.
  3496. (should
  3497. (org-test-with-temp-text "* H1\n** H2\n\n\n\n* H3"
  3498. (let ((org-cycle-separator-lines 1))
  3499. (org-cycle)
  3500. (org-next-visible-heading 1))
  3501. (looking-at "\\* H3")))
  3502. ;; Move at end of buffer when there is no more headline.
  3503. (should
  3504. (org-test-with-temp-text "* H1"
  3505. (org-next-visible-heading 1)
  3506. (eobp)))
  3507. (should
  3508. (org-test-with-temp-text "* H1\n* H2"
  3509. (org-next-visible-heading 2)
  3510. (eobp)))
  3511. ;; With a negative argument, move backwards.
  3512. (should
  3513. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3514. (org-next-visible-heading -1)
  3515. (looking-at "\\* H2")))
  3516. (should
  3517. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3518. (org-next-visible-heading -2)
  3519. (looking-at "\\* H1"))))
  3520. (ert-deftest test-org/previous-visible-heading ()
  3521. "Test `org-previous-visible-heading' specifications."
  3522. ;; Move to the beginning of the next headline, taking into
  3523. ;; consideration ARG.
  3524. (should
  3525. (org-test-with-temp-text "* H1\n<point>* H2"
  3526. (org-previous-visible-heading 1)
  3527. (looking-at "\\* H1")))
  3528. (should
  3529. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3530. (org-previous-visible-heading 2)
  3531. (looking-at "\\* H1")))
  3532. ;; Ignore invisible headlines.
  3533. (should
  3534. (org-test-with-temp-text "* H1\n** H2\n<point>* H3"
  3535. (org-overview)
  3536. (org-previous-visible-heading 1)
  3537. (looking-at "\\* H1")))
  3538. ;; Move point between headlines, not on blank lines between.
  3539. (should
  3540. (org-test-with-temp-text "* H1\n\n\n\n** H2\n<point>* H3"
  3541. (let ((org-cycle-separator-lines 1))
  3542. (org-overview)
  3543. (org-previous-visible-heading 1))
  3544. (looking-at "\\* H1")))
  3545. ;; Move at end of buffer when there is no more headline.
  3546. (should
  3547. (org-test-with-temp-text "* H1"
  3548. (org-previous-visible-heading 1)
  3549. (bobp)))
  3550. (should
  3551. (org-test-with-temp-text "* H1\n* <point>H2"
  3552. (org-previous-visible-heading 2)
  3553. (bobp)))
  3554. ;; Invisible parts may not start at a headline, i.e., when revealing
  3555. ;; parts of the buffer. Handle this.
  3556. (should
  3557. (org-test-with-temp-text "* Main\n** H1\nFoo\n** H2\nBar\n** H3\nBaz"
  3558. (org-overview)
  3559. (search-forward "H1")
  3560. (org-show-context 'minimal)
  3561. (org-cycle)
  3562. (search-forward "H3")
  3563. (org-show-context 'minimal)
  3564. ;; At this point, buffer displays, with point at "|",
  3565. ;;
  3566. ;; * Main
  3567. ;; ** H1
  3568. ;; Foo
  3569. ;; ** H3|
  3570. (org-previous-visible-heading 1)
  3571. (looking-at "\\*+ H1"))))
  3572. (ert-deftest test-org/forward-heading-same-level ()
  3573. "Test `org-forward-heading-same-level' specifications."
  3574. ;; Test navigation at top level, forward and backward.
  3575. (should
  3576. (equal "* H2"
  3577. (org-test-with-temp-text "* H1\n* H2"
  3578. (org-forward-heading-same-level 1)
  3579. (buffer-substring-no-properties (point) (line-end-position)))))
  3580. (should
  3581. (equal "* H1"
  3582. (org-test-with-temp-text "* H1\n<point>* H2"
  3583. (org-forward-heading-same-level -1)
  3584. (buffer-substring-no-properties (point) (line-end-position)))))
  3585. ;; Test navigation in a sub-tree, forward and backward.
  3586. (should
  3587. (equal "* H2"
  3588. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3589. (org-forward-heading-same-level 1)
  3590. (buffer-substring-no-properties (point) (line-end-position)))))
  3591. (should
  3592. (equal "* H1"
  3593. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3594. (org-forward-heading-same-level -1)
  3595. (buffer-substring-no-properties (point) (line-end-position)))))
  3596. ;; Stop at first or last sub-heading.
  3597. (should-not
  3598. (equal "* H2"
  3599. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3600. (org-forward-heading-same-level 1)
  3601. (buffer-substring-no-properties (point) (line-end-position)))))
  3602. (should-not
  3603. (equal "* H2"
  3604. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3605. (org-forward-heading-same-level -1)
  3606. (buffer-substring-no-properties (point) (line-end-position)))))
  3607. ;; Allow multiple moves.
  3608. (should
  3609. (equal "* H3"
  3610. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3611. (org-forward-heading-same-level 2)
  3612. (buffer-substring-no-properties (point) (line-end-position)))))
  3613. (should
  3614. (equal "* H1"
  3615. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3616. (org-forward-heading-same-level -2)
  3617. (buffer-substring-no-properties (point) (line-end-position)))))
  3618. ;; Ignore spurious moves when first (or last) sibling is reached.
  3619. (should
  3620. (equal "** H3"
  3621. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3622. (org-forward-heading-same-level 100)
  3623. (buffer-substring-no-properties (point) (line-end-position)))))
  3624. (should
  3625. (equal "** H1"
  3626. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3627. (org-forward-heading-same-level -100)
  3628. (buffer-substring-no-properties (point) (line-end-position))))))
  3629. (ert-deftest test-org/end-of-meta-data ()
  3630. "Test `org-end-of-meta-data' specifications."
  3631. ;; Skip planning line.
  3632. (should
  3633. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3634. (org-end-of-meta-data)
  3635. (eobp)))
  3636. ;; Skip properties drawer.
  3637. (should
  3638. (org-test-with-temp-text
  3639. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3640. (org-end-of-meta-data)
  3641. (eobp)))
  3642. ;; Skip both.
  3643. (should
  3644. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3645. (org-end-of-meta-data)
  3646. (eobp)))
  3647. ;; Nothing to skip, go to first line.
  3648. (should
  3649. (org-test-with-temp-text "* Headline\nContents"
  3650. (org-end-of-meta-data)
  3651. (looking-at "Contents")))
  3652. ;; With option argument, skip empty lines, regular drawers and
  3653. ;; clocking lines.
  3654. (should
  3655. (org-test-with-temp-text "* Headline\n\nContents"
  3656. (org-end-of-meta-data t)
  3657. (looking-at "Contents")))
  3658. (should
  3659. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3660. (org-end-of-meta-data t)
  3661. (looking-at "Contents")))
  3662. (should
  3663. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3664. (org-end-of-meta-data t)
  3665. (looking-at "Contents")))
  3666. ;; Special case: do not skip incomplete drawers.
  3667. (should
  3668. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3669. (org-end-of-meta-data t)
  3670. (looking-at ":LOGBOOK:")))
  3671. ;; Special case: Be careful about consecutive headlines.
  3672. (should-not
  3673. (org-test-with-temp-text "* H1\n*H2\nContents"
  3674. (org-end-of-meta-data t)
  3675. (looking-at "Contents"))))
  3676. (ert-deftest test-org/shiftright-heading ()
  3677. "Test `org-shiftright' on headings."
  3678. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  3679. (should
  3680. (equal "* TODO a1\n** a2\n* DONE b1\n"
  3681. (org-test-with-temp-text "* a1\n** a2\n* DONE b1\n"
  3682. (org-shiftright)
  3683. (buffer-string))))
  3684. (should
  3685. (equal "* TODO a1\n** TODO a2\n* b1\n"
  3686. (org-test-with-temp-text "* a1\n** a2\n* DONE b1\n"
  3687. (let ((org-loop-over-headlines-in-active-region t))
  3688. (transient-mark-mode 1)
  3689. (push-mark (point) t t)
  3690. (search-forward "* DONE b1")
  3691. (org-shiftright))
  3692. (buffer-string))))
  3693. (should
  3694. (equal "* TODO a1\n** a2\n* b1\n"
  3695. (org-test-with-temp-text "* a1\n** a2\n* DONE b1\n"
  3696. (let ((org-loop-over-headlines-in-active-region 'start-level))
  3697. (transient-mark-mode 1)
  3698. (push-mark (point) t t)
  3699. (search-forward "* DONE b1")
  3700. (org-shiftright))
  3701. (buffer-string))))))
  3702. (ert-deftest test-org/beginning-of-line ()
  3703. "Test `org-beginning-of-line' specifications."
  3704. ;; Move to beginning of line. If current line in invisible, move to
  3705. ;; beginning of visible line instead.
  3706. (should
  3707. (org-test-with-temp-text "Some text\nSome other text<point>"
  3708. (org-beginning-of-line)
  3709. (bolp)))
  3710. (should
  3711. (org-test-with-temp-text "* H1\n** H2<point>"
  3712. (org-overview)
  3713. (org-beginning-of-line)
  3714. (= (line-beginning-position) 1)))
  3715. ;; With `visual-line-mode' active, move to beginning of visual line.
  3716. (should-not
  3717. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3718. (visual-line-mode)
  3719. (dotimes (_ 1000) (insert "very "))
  3720. (org-beginning-of-line)
  3721. (bolp)))
  3722. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3723. ;; beginning of a visual line than to the logical beginning of line,
  3724. ;; even if special movement is active.
  3725. (should-not
  3726. (org-test-with-temp-text "* A <point>long headline"
  3727. (visual-line-mode)
  3728. (dotimes (_ 1000) (insert "very "))
  3729. (goto-char (point-max))
  3730. (org-beginning-of-line)
  3731. (bobp)))
  3732. (should-not
  3733. (org-test-with-temp-text "* A <point>long headline"
  3734. (visual-line-mode)
  3735. (dotimes (_ 1000) (insert "very "))
  3736. (goto-char (point-max))
  3737. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3738. (bobp)))
  3739. ;; At an headline with special movement, first move at beginning of
  3740. ;; title, then at the beginning of line, rinse, repeat.
  3741. (should
  3742. (org-test-with-temp-text "* TODO Headline<point>"
  3743. (let ((org-special-ctrl-a/e t))
  3744. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3745. (progn (org-beginning-of-line) (bolp))
  3746. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3747. (should
  3748. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3749. (let ((org-special-ctrl-a/e t))
  3750. (org-beginning-of-line)
  3751. (looking-at "Headline"))))
  3752. (should
  3753. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3754. (let ((org-special-ctrl-a/e '(t . nil)))
  3755. (org-beginning-of-line)
  3756. (looking-at "Headline"))))
  3757. (should-not
  3758. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3759. (let ((org-special-ctrl-a/e '(nil . nil)))
  3760. (org-beginning-of-line)
  3761. (looking-at "Headline"))))
  3762. ;; At an headline with reversed movement, first move to beginning of
  3763. ;; line, then to the beginning of title.
  3764. (should
  3765. (org-test-with-temp-text "* TODO Headline<point>"
  3766. (let ((org-special-ctrl-a/e 'reversed)
  3767. (this-command last-command))
  3768. (and (progn (org-beginning-of-line) (bolp))
  3769. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3770. (should
  3771. (org-test-with-temp-text "* TODO Headline<point>"
  3772. (let ((org-special-ctrl-a/e '(reversed . nil))
  3773. (this-command last-command))
  3774. (and (progn (org-beginning-of-line) (bolp))
  3775. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3776. (should-not
  3777. (org-test-with-temp-text "* TODO Headline<point>"
  3778. (let ((org-special-ctrl-a/e '(t . nil))
  3779. (this-command last-command))
  3780. (and (progn (org-beginning-of-line) (bolp))
  3781. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3782. ;; At an item with special movement, first move after to beginning
  3783. ;; of title, then to the beginning of line, rinse, repeat.
  3784. (should
  3785. (org-test-with-temp-text "- [ ] Item<point>"
  3786. (let ((org-special-ctrl-a/e t))
  3787. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3788. (progn (org-beginning-of-line) (bolp))
  3789. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3790. ;; At an item with reversed movement, first move to beginning of
  3791. ;; line, then to the beginning of title.
  3792. (should
  3793. (org-test-with-temp-text "- [X] Item<point>"
  3794. (let ((org-special-ctrl-a/e 'reversed)
  3795. (this-command last-command))
  3796. (and (progn (org-beginning-of-line) (bolp))
  3797. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3798. ;; Leave point before invisible characters at column 0.
  3799. (should
  3800. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3801. (let ((org-special-ctrl-a/e nil))
  3802. (org-beginning-of-line)
  3803. (bolp))))
  3804. (should
  3805. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3806. (let ((org-special-ctrl-a/e t))
  3807. (org-beginning-of-line)
  3808. (bolp))))
  3809. (should
  3810. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3811. (visual-line-mode)
  3812. (org-beginning-of-line)
  3813. (bolp)))
  3814. ;; Special case: Do not error when the buffer contains only a single
  3815. ;; asterisk.
  3816. (should
  3817. (org-test-with-temp-text "*<point>"
  3818. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3819. (should
  3820. (org-test-with-temp-text "*<point>"
  3821. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3822. (ert-deftest test-org/end-of-line ()
  3823. "Test `org-end-of-line' specifications."
  3824. ;; Standard test.
  3825. (should
  3826. (org-test-with-temp-text "Some text\nSome other text"
  3827. (org-end-of-line)
  3828. (eolp)))
  3829. ;; With `visual-line-mode' active, move to end of visible line.
  3830. ;; However, never go past ellipsis.
  3831. (should-not
  3832. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3833. (visual-line-mode)
  3834. (dotimes (_ 1000) (insert "very "))
  3835. (goto-char (point-min))
  3836. (org-end-of-line)
  3837. (eolp)))
  3838. (should-not
  3839. (org-test-with-temp-text "* A short headline\nSome contents"
  3840. (visual-line-mode)
  3841. (org-overview)
  3842. (org-end-of-line)
  3843. (eobp)))
  3844. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3845. ;; of visible line if tags, or end of line, are farther.
  3846. (should-not
  3847. (org-test-with-temp-text "* A <point>long headline"
  3848. (visual-line-mode)
  3849. (dotimes (_ 1000) (insert "very "))
  3850. (goto-char (point-min))
  3851. (org-end-of-line)
  3852. (eolp)))
  3853. (should-not
  3854. (org-test-with-temp-text "* A <point>long headline :tag:"
  3855. (visual-line-mode)
  3856. (dotimes (_ 1000) (insert "very "))
  3857. (goto-char (point-min))
  3858. (org-end-of-line)
  3859. (eolp)))
  3860. ;; At an headline without special movement, go to end of line.
  3861. ;; However, never go past ellipsis.
  3862. (should
  3863. (org-test-with-temp-text "* Headline2b :tag:\n"
  3864. (let ((org-special-ctrl-a/e nil))
  3865. (and (progn (org-end-of-line) (eolp))
  3866. (progn (org-end-of-line) (eolp))))))
  3867. (should
  3868. (org-test-with-temp-text "* Headline2b :tag:\n"
  3869. (let ((org-special-ctrl-a/e '(t . nil)))
  3870. (and (progn (org-end-of-line) (eolp))
  3871. (progn (org-end-of-line) (eolp))))))
  3872. (should
  3873. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3874. (org-overview)
  3875. (let ((org-special-ctrl-a/e nil))
  3876. (org-end-of-line)
  3877. (= 1 (line-beginning-position)))))
  3878. ;; At an headline with special movement, first move before tags,
  3879. ;; then at the end of line, rinse, repeat. However, never go past
  3880. ;; ellipsis.
  3881. (should
  3882. (org-test-with-temp-text "* Headline1 :tag:\n"
  3883. (let ((org-special-ctrl-a/e t))
  3884. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3885. (progn (org-end-of-line) (eolp))
  3886. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3887. (should
  3888. (org-test-with-temp-text "* Headline1 :tag:\n"
  3889. (let ((org-special-ctrl-a/e '(nil . t)))
  3890. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3891. (progn (org-end-of-line) (eolp))
  3892. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3893. (should-not
  3894. (org-test-with-temp-text "* Headline1 :tag:\n"
  3895. (let ((org-special-ctrl-a/e '(nil . nil)))
  3896. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3897. (progn (org-end-of-line) (eolp))
  3898. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3899. (should
  3900. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3901. (org-overview)
  3902. (let ((org-special-ctrl-a/e t))
  3903. (org-end-of-line)
  3904. (org-end-of-line)
  3905. (= 1 (line-beginning-position)))))
  3906. ;; At an headline, with reversed movement, first go to end of line,
  3907. ;; then before tags. However, never go past ellipsis.
  3908. (should
  3909. (org-test-with-temp-text "* Headline3 :tag:\n"
  3910. (let ((org-special-ctrl-a/e 'reversed)
  3911. (this-command last-command))
  3912. (and (progn (org-end-of-line) (eolp))
  3913. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3914. (should
  3915. (org-test-with-temp-text "* Headline3 :tag:\n"
  3916. (let ((org-special-ctrl-a/e '(nil . reversed))
  3917. (this-command last-command))
  3918. (and (progn (org-end-of-line) (eolp))
  3919. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3920. (should-not
  3921. (org-test-with-temp-text "* Headline3 :tag:\n"
  3922. (let ((org-special-ctrl-a/e '(nil . t))
  3923. (this-command last-command))
  3924. (and (progn (org-end-of-line) (eolp))
  3925. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3926. (should
  3927. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3928. (org-overview)
  3929. (let ((org-special-ctrl-a/e 'reversed))
  3930. (org-end-of-line)
  3931. (= 1 (line-beginning-position)))))
  3932. ;; At a block without hidden contents.
  3933. (should
  3934. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3935. (progn (org-end-of-line) (eolp))))
  3936. ;; At a block with hidden contents.
  3937. (should-not
  3938. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3939. (let ((org-special-ctrl-a/e t))
  3940. (org-fold-hide-block-toggle)
  3941. (org-end-of-line)
  3942. (eobp))))
  3943. ;; Get past invisible characters at the end of line.
  3944. (should
  3945. (org-test-with-temp-text "[[https://orgmode.org]]"
  3946. (org-end-of-line)
  3947. (eolp))))
  3948. (ert-deftest test-org/open-line ()
  3949. "Test `org-open-line' specifications."
  3950. ;; Call `open-line' outside of tables.
  3951. (should
  3952. (equal "\nText"
  3953. (org-test-with-temp-text "Text"
  3954. (org-open-line 1)
  3955. (buffer-string))))
  3956. ;; At a table, create a row above.
  3957. (should
  3958. (equal "\n| |\n| a |"
  3959. (org-test-with-temp-text "\n<point>| a |"
  3960. (org-open-line 1)
  3961. (buffer-string))))
  3962. ;; At the very first character of the buffer, also call `open-line'.
  3963. (should
  3964. (equal "\n| a |"
  3965. (org-test-with-temp-text "| a |"
  3966. (org-open-line 1)
  3967. (buffer-string))))
  3968. ;; Narrowing does not count.
  3969. (should
  3970. (equal "Text\n| |\n| a |"
  3971. (org-test-with-temp-text "Text\n<point>| a |"
  3972. (narrow-to-region (point) (point-max))
  3973. (org-open-line 1)
  3974. (widen)
  3975. (buffer-string)))))
  3976. (ert-deftest test-org/forward-sentence ()
  3977. "Test `org-forward-sentence' specifications."
  3978. ;; At the end of a table cell, move to the end of the next one.
  3979. (should
  3980. (org-test-with-temp-text "| a<point> | b |"
  3981. (org-forward-sentence)
  3982. (looking-at " |$")))
  3983. ;; Elsewhere in a cell, move to its end.
  3984. (should
  3985. (org-test-with-temp-text "| a<point>c | b |"
  3986. (org-forward-sentence)
  3987. (looking-at " | b |$")))
  3988. ;; Otherwise, simply call `forward-sentence'.
  3989. (should
  3990. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3991. (org-forward-sentence)
  3992. (looking-at " Sentence 2.")))
  3993. (should
  3994. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3995. (org-forward-sentence)
  3996. (org-forward-sentence)
  3997. (eobp)))
  3998. ;; At the end of an element, jump to the next one, without stopping
  3999. ;; on blank lines in-between.
  4000. (should
  4001. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  4002. (org-forward-sentence)
  4003. (eobp)))
  4004. ;; Headlines are considered to be sentences by themselves, even if
  4005. ;; they do not end with a full stop.
  4006. (should
  4007. (equal
  4008. "* Headline"
  4009. (org-test-with-temp-text "* <point>Headline\nSentence."
  4010. (org-forward-sentence)
  4011. (buffer-substring-no-properties (line-beginning-position) (point)))))
  4012. (should
  4013. (org-test-with-temp-text "* Headline<point>\nSentence."
  4014. (org-forward-sentence)
  4015. (eobp)))
  4016. (should
  4017. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  4018. (org-forward-sentence)
  4019. (and (org-at-heading-p) (eolp)))))
  4020. (ert-deftest test-org/backward-sentence ()
  4021. "Test `org-backward-sentence' specifications."
  4022. ;; At the beginning of a table cell, move to the beginning of the
  4023. ;; previous one.
  4024. (should
  4025. (org-test-with-temp-text "| a | <point>b |"
  4026. (org-backward-sentence)
  4027. (looking-at "a | b |$")))
  4028. ;; Elsewhere in a cell, move to its beginning.
  4029. (should
  4030. (org-test-with-temp-text "| a | b<point>c |"
  4031. (org-backward-sentence)
  4032. (looking-at "bc |$")))
  4033. ;; Otherwise, simply call `backward-sentence'.
  4034. (should
  4035. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  4036. (org-backward-sentence)
  4037. (looking-at "Sentence 2.")))
  4038. (should
  4039. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  4040. (org-backward-sentence)
  4041. (org-backward-sentence)
  4042. (bobp)))
  4043. ;; Make sure to hit the beginning of a sentence on the same line as
  4044. ;; an item.
  4045. (should
  4046. (org-test-with-temp-text "- Line 1\n line <point>2."
  4047. (org-backward-sentence)
  4048. (looking-at "Line 1"))))
  4049. (ert-deftest test-org/forward-paragraph ()
  4050. "Test `org-forward-paragraph' specifications."
  4051. ;; At end of buffer, do not return an error.
  4052. (should
  4053. (org-test-with-temp-text "Paragraph"
  4054. (goto-char (point-max))
  4055. (org-forward-paragraph)
  4056. t))
  4057. ;; Standard test.
  4058. (should
  4059. (= 2
  4060. (org-test-with-temp-text "P1\n\nP2"
  4061. (org-forward-paragraph)
  4062. (org-current-line))))
  4063. (should
  4064. (= 2
  4065. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  4066. (org-forward-paragraph)
  4067. (org-current-line))))
  4068. ;; Enter greater elements.
  4069. (should
  4070. (= 2
  4071. (org-test-with-temp-text "#+begin_center\nP1\n#+end_center\nP2"
  4072. (org-forward-paragraph)
  4073. (org-current-line))))
  4074. ;; Do not enter elements with invisible contents.
  4075. (should
  4076. (= 4
  4077. (org-test-with-temp-text "* H1\n P1\n\n* H2"
  4078. (org-cycle)
  4079. (org-forward-paragraph)
  4080. (org-current-line))))
  4081. (should
  4082. (= 6
  4083. (org-test-with-temp-text "#+begin_center\nP1\n\nP2\n#+end_center\nP3"
  4084. (org-fold-hide-block-toggle)
  4085. (org-forward-paragraph)
  4086. (org-current-line))))
  4087. ;; On an item or a footnote definition, move past the first element
  4088. ;; inside, if any.
  4089. (should
  4090. (= 2
  4091. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  4092. (org-forward-paragraph)
  4093. (org-current-line))))
  4094. (should
  4095. (= 2
  4096. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  4097. (org-forward-paragraph)
  4098. (org-current-line))))
  4099. ;; On a table (resp. a property drawer) do not move through table
  4100. ;; rows (resp. node properties).
  4101. (should
  4102. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  4103. (org-forward-paragraph)
  4104. (looking-at "Paragraph")))
  4105. (should
  4106. (org-test-with-temp-text
  4107. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  4108. (org-forward-paragraph)
  4109. (looking-at "Paragraph")))
  4110. ;; Skip consecutive keywords, clocks and diary S-exps.
  4111. (should
  4112. (org-test-with-temp-text "#+key: val\n #+key2: val\n#+key3: val\n"
  4113. (org-forward-paragraph)
  4114. (eobp)))
  4115. (should
  4116. (org-test-with-temp-text "CLOCK: val\n CLOCK: val\nCLOCK: val\n"
  4117. (org-forward-paragraph)
  4118. (eobp)))
  4119. (should
  4120. (org-test-with-temp-text "%%(foo)\n%%(bar)\n%%(baz)\n"
  4121. (org-forward-paragraph)
  4122. (eobp)))
  4123. (should-not
  4124. (org-test-with-temp-text "#+key: val\n #+key2: val\n\n#+key3: val\n"
  4125. (org-forward-paragraph)
  4126. (eobp)))
  4127. (should-not
  4128. (org-test-with-temp-text "#+key: val\nCLOCK: ...\n"
  4129. (org-forward-paragraph)
  4130. (eobp)))
  4131. ;; In a plain list with one item every line, skip the whole list,
  4132. ;; even with point in the middle of the list.
  4133. (should
  4134. (org-test-with-temp-text "- A\n - B\n- C\n"
  4135. (org-forward-paragraph)
  4136. (eobp)))
  4137. (should
  4138. (org-test-with-temp-text "- A\n - <point>B\n- C\n"
  4139. (org-forward-paragraph)
  4140. (eobp)))
  4141. ;; On a comment, verse or source block, stop at "contents"
  4142. ;; boundaries and blank lines.
  4143. (should
  4144. (= 2
  4145. (org-test-with-temp-text "#+begin_src emacs-lisp\nL1\n\nL2\n#+end_src"
  4146. (org-forward-paragraph)
  4147. (org-current-line))))
  4148. (should
  4149. (= 3
  4150. (org-test-with-temp-text "#+begin_verse\n<point>L1\n\nL2\n#+end_verse"
  4151. (org-forward-paragraph)
  4152. (org-current-line))))
  4153. (should
  4154. (= 5
  4155. (org-test-with-temp-text "#+begin_comment\nL1\n\n<point>L2\n#+end_comment"
  4156. (org-forward-paragraph)
  4157. (org-current-line))))
  4158. ;; Being on an affiliated keyword shouldn't make any difference.
  4159. (should
  4160. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  4161. (org-forward-paragraph)
  4162. (eobp))))
  4163. (ert-deftest test-org/backward-paragraph ()
  4164. "Test `org-backward-paragraph' specifications."
  4165. ;; Do not error at beginning of buffer.
  4166. (should
  4167. (org-test-with-temp-text "Paragraph"
  4168. (org-backward-paragraph)
  4169. t))
  4170. ;; At blank lines at the very beginning of a buffer, move to
  4171. ;; point-min.
  4172. (should
  4173. (org-test-with-temp-text "\n\n<point>\n\nParagraph"
  4174. (org-backward-paragraph)
  4175. (bobp)))
  4176. ;; Regular test.
  4177. (should
  4178. (= 2
  4179. (org-test-with-temp-text "P1\n\nP2<point>"
  4180. (org-backward-paragraph)
  4181. (org-current-line))))
  4182. (should
  4183. (= 4
  4184. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  4185. (org-backward-paragraph)
  4186. (org-current-line))))
  4187. ;; Try to move on the line above current element.
  4188. (should
  4189. (= 2
  4190. (org-test-with-temp-text "\n\n<point>Paragraph"
  4191. (org-backward-paragraph)
  4192. (org-current-line))))
  4193. ;; Do not leave point in an invisible area.
  4194. (should
  4195. (org-test-with-temp-text "* H1\n P1\n\n* H2"
  4196. (org-cycle)
  4197. (goto-char (point-max))
  4198. (beginning-of-line)
  4199. (org-backward-paragraph)
  4200. (bobp)))
  4201. (should
  4202. (org-test-with-temp-text "#+begin_center\nP1\n\nP2\n#+end_center\n"
  4203. (org-fold-hide-block-toggle)
  4204. (goto-char (point-max))
  4205. (org-backward-paragraph)
  4206. (bobp)))
  4207. ;; On the first element in an item or a footnote definition, jump
  4208. ;; before the footnote or the item.
  4209. (should
  4210. (org-test-with-temp-text "- line1<point>"
  4211. (org-backward-paragraph)
  4212. (bobp)))
  4213. (should
  4214. (org-test-with-temp-text "[fn:1] line1n<point>"
  4215. (org-backward-paragraph)
  4216. (bobp)))
  4217. ;; On the second element in an item or a footnote definition, jump
  4218. ;; to item or the definition.
  4219. (should
  4220. (= 2
  4221. (org-test-with-temp-text "- line1\n\n<point> line2"
  4222. (org-backward-paragraph)
  4223. (org-current-line))))
  4224. (should
  4225. (= 2
  4226. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  4227. (org-backward-paragraph)
  4228. (org-current-line))))
  4229. ;; On a table (resp. a property drawer), ignore table rows
  4230. ;; (resp. node properties).
  4231. (should
  4232. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  4233. (org-backward-paragraph)
  4234. (bobp)))
  4235. (should
  4236. (= 2
  4237. (org-test-with-temp-text
  4238. "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  4239. (org-backward-paragraph)
  4240. (org-current-line))))
  4241. ;; In a plain list with one item every line, skip the whole list,
  4242. ;; even with point in the middle of the list.
  4243. (should
  4244. (org-test-with-temp-text "- A\n - B\n- C\n<point>"
  4245. (org-backward-paragraph)
  4246. (bobp)))
  4247. (should
  4248. (org-test-with-temp-text "- A\n - B\n- <point>C\n"
  4249. (org-backward-paragraph)
  4250. (bobp)))
  4251. ;; Skip consecutive keywords, clocks and diary S-exps.
  4252. (should
  4253. (org-test-with-temp-text "#+key: val\n #+key2: val\n#+key3: val\n<point>"
  4254. (org-backward-paragraph)
  4255. (bobp)))
  4256. (should
  4257. (org-test-with-temp-text "CLOCK: val\n CLOCK: val\nCLOCK: val\n<point>"
  4258. (org-backward-paragraph)
  4259. (bobp)))
  4260. (should
  4261. (org-test-with-temp-text "%%(foo)\n%%(bar)\n%%(baz)\n<point>"
  4262. (org-backward-paragraph)
  4263. (bobp)))
  4264. (should-not
  4265. (org-test-with-temp-text "#+key: val\n #+key2: val\n\n#+key3: val\n<point>"
  4266. (org-backward-paragraph)
  4267. (bobp)))
  4268. (should-not
  4269. (org-test-with-temp-text "#+key: val\nCLOCK: ...\n<point>"
  4270. (org-backward-paragraph)
  4271. (bobp)))
  4272. ;; On a comment, example, source and verse blocks, stop at blank
  4273. ;; lines.
  4274. (should
  4275. (= 1
  4276. (org-test-with-temp-text
  4277. "#+begin_comment\n<point>L1\n\nL2\n\nL3\n#+end_comment"
  4278. (org-backward-paragraph)
  4279. (org-current-line))))
  4280. (should
  4281. (= 2
  4282. (org-test-with-temp-text
  4283. "#+begin_verse\nL1\n\n<point>L2\n\nL3\n#+end_verse"
  4284. (org-backward-paragraph)
  4285. (org-current-line))))
  4286. (should
  4287. (= 3
  4288. (org-test-with-temp-text
  4289. "#+begin_src emacs-lisp\nL1\n\nL2\n\n<point>L3\n#+end_src"
  4290. (org-backward-paragraph)
  4291. (org-current-line))))
  4292. ;; When called from the opening line itself, however, move to
  4293. ;; beginning of block.
  4294. (should
  4295. (org-test-with-temp-text "#+begin_<point>example\nL1\n#+end_example"
  4296. (org-backward-paragraph)
  4297. (bobp)))
  4298. ;; On an empty heading, move above it.
  4299. (should
  4300. (org-test-with-temp-text "\n* <point>"
  4301. (org-backward-paragraph)
  4302. (bobp)))
  4303. (should
  4304. (org-test-with-temp-text "\n* \n<point>"
  4305. (org-backward-paragraph)
  4306. (bobp))))
  4307. (ert-deftest test-org/forward-element ()
  4308. "Test `org-forward-element' specifications."
  4309. ;; 1. At EOB: should error.
  4310. (org-test-with-temp-text "Some text\n"
  4311. (goto-char (point-max))
  4312. (should-error (org-forward-element)))
  4313. ;; 2. Standard move: expected to ignore blank lines.
  4314. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  4315. (org-forward-element)
  4316. (should (looking-at (regexp-quote "Second paragraph."))))
  4317. ;; 3. Headline tests.
  4318. (org-test-with-temp-text "
  4319. * Head 1
  4320. ** Head 1.1
  4321. *** Head 1.1.1
  4322. ** Head 1.2"
  4323. ;; 3.1. At an headline beginning: move to next headline at the
  4324. ;; same level.
  4325. (goto-line 3)
  4326. (org-forward-element)
  4327. (should (looking-at (regexp-quote "** Head 1.2")))
  4328. ;; 3.2. At an headline beginning: move to parent headline if no
  4329. ;; headline at the same level.
  4330. (goto-line 3)
  4331. (org-forward-element)
  4332. (should (looking-at (regexp-quote "** Head 1.2"))))
  4333. ;; 4. Greater element tests.
  4334. (org-test-with-temp-text
  4335. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  4336. ;; 4.1. At a greater element: expected to skip contents.
  4337. (org-forward-element)
  4338. (should (looking-at (regexp-quote "Outside.")))
  4339. ;; 4.2. At the end of greater element contents: expected to skip
  4340. ;; to the end of the greater element.
  4341. (goto-line 2)
  4342. (org-forward-element)
  4343. (should (looking-at (regexp-quote "Outside."))))
  4344. ;; 5. List tests.
  4345. (org-test-with-temp-text "
  4346. - item1
  4347. - sub1
  4348. - sub2
  4349. - sub3
  4350. Inner paragraph.
  4351. - item2
  4352. Outside."
  4353. ;; 5.1. At list top point: expected to move to the element after
  4354. ;; the list.
  4355. (goto-line 2)
  4356. (org-forward-element)
  4357. (should (looking-at (regexp-quote "Outside.")))
  4358. ;; 5.2. Special case: at the first line of a sub-list, but not at
  4359. ;; beginning of line, move to next item.
  4360. (goto-line 2)
  4361. (forward-char)
  4362. (org-forward-element)
  4363. (should (looking-at "- item2"))
  4364. (goto-line 4)
  4365. (forward-char)
  4366. (org-forward-element)
  4367. (should (looking-at " - sub2"))
  4368. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  4369. (goto-line 4)
  4370. (org-forward-element)
  4371. (should (looking-at (regexp-quote " Inner paragraph.")))
  4372. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  4373. (goto-line 8)
  4374. (org-forward-element)
  4375. (should (looking-at (regexp-quote " Inner paragraph.")))
  4376. ;; 5.5. At an item: expected to move to next item, if any.
  4377. (goto-line 6)
  4378. (org-forward-element)
  4379. (should (looking-at " - sub3"))))
  4380. (ert-deftest test-org/backward-element ()
  4381. "Test `org-backward-element' specifications."
  4382. ;; 1. Should error at BOB.
  4383. (org-test-with-temp-text " \nParagraph."
  4384. (should-error (org-backward-element)))
  4385. ;; 2. Should move at BOB when called on the first element in buffer.
  4386. (should
  4387. (org-test-with-temp-text "\n#+TITLE: test"
  4388. (progn (forward-line)
  4389. (org-backward-element)
  4390. (bobp))))
  4391. ;; 3. Not at the beginning of an element: move at its beginning.
  4392. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  4393. (goto-line 3)
  4394. (end-of-line)
  4395. (org-backward-element)
  4396. (should (looking-at (regexp-quote "Paragraph2."))))
  4397. ;; 4. Headline tests.
  4398. (org-test-with-temp-text "
  4399. * Head 1
  4400. ** Head 1.1
  4401. *** Head 1.1.1
  4402. ** Head 1.2"
  4403. ;; 4.1. At an headline beginning: move to previous headline at the
  4404. ;; same level.
  4405. (goto-line 5)
  4406. (org-backward-element)
  4407. (should (looking-at (regexp-quote "** Head 1.1")))
  4408. ;; 4.2. At an headline beginning: move to parent headline if no
  4409. ;; headline at the same level.
  4410. (goto-line 3)
  4411. (org-backward-element)
  4412. (should (looking-at (regexp-quote "* Head 1")))
  4413. ;; 4.3. At the first top-level headline: should error.
  4414. (goto-line 2)
  4415. (should-error (org-backward-element)))
  4416. ;; 5. At beginning of first element inside a greater element:
  4417. ;; expected to move to greater element's beginning.
  4418. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  4419. (goto-line 3)
  4420. (org-backward-element)
  4421. (should (looking-at "#\\+BEGIN_CENTER")))
  4422. ;; 6. At the beginning of the first element in a section: should
  4423. ;; move back to headline, if any.
  4424. (should
  4425. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  4426. (progn (goto-char (point-max))
  4427. (beginning-of-line)
  4428. (org-backward-element)
  4429. (org-at-heading-p))))
  4430. ;; 7. List tests.
  4431. (org-test-with-temp-text "
  4432. - item1
  4433. - sub1
  4434. - sub2
  4435. - sub3
  4436. Inner paragraph.
  4437. - item2
  4438. Outside."
  4439. ;; 7.1. At beginning of sub-list: expected to move to the
  4440. ;; paragraph before it.
  4441. (goto-line 4)
  4442. (org-backward-element)
  4443. (should (looking-at "item1"))
  4444. ;; 7.2. At an item in a list: expected to move at previous item.
  4445. (goto-line 8)
  4446. (org-backward-element)
  4447. (should (looking-at " - sub2"))
  4448. (goto-line 12)
  4449. (org-backward-element)
  4450. (should (looking-at "- item1"))
  4451. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  4452. ;; beginning.
  4453. (goto-line 10)
  4454. (org-backward-element)
  4455. (should (looking-at " - sub1"))
  4456. (goto-line 15)
  4457. (org-backward-element)
  4458. (should (looking-at "- item1"))
  4459. ;; 7.4. At blank-lines before list end: expected to move to top
  4460. ;; item.
  4461. (goto-line 14)
  4462. (org-backward-element)
  4463. (should (looking-at "- item1"))))
  4464. (ert-deftest test-org/up-element ()
  4465. "Test `org-up-element' specifications."
  4466. ;; 1. At BOB or with no surrounding element: should error.
  4467. (org-test-with-temp-text "Paragraph."
  4468. (should-error (org-up-element)))
  4469. (org-test-with-temp-text "* Head1\n* Head2"
  4470. (goto-line 2)
  4471. (should-error (org-up-element)))
  4472. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  4473. (goto-line 3)
  4474. (should-error (org-up-element)))
  4475. ;; 2. At an headline: move to parent headline.
  4476. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  4477. (goto-line 3)
  4478. (org-up-element)
  4479. (should (looking-at "\\* Head1")))
  4480. ;; 3. Inside a greater element: move to greater element beginning.
  4481. (org-test-with-temp-text
  4482. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  4483. (goto-line 3)
  4484. (org-up-element)
  4485. (should (looking-at "#\\+BEGIN_CENTER")))
  4486. ;; 4. List tests.
  4487. (org-test-with-temp-text "* Top
  4488. - item1
  4489. - sub1
  4490. - sub2
  4491. Paragraph within sub2.
  4492. - item2"
  4493. ;; 4.1. Within an item: move to the item beginning.
  4494. (goto-line 8)
  4495. (org-up-element)
  4496. (should (looking-at " - sub2"))
  4497. ;; 4.2. At an item in a sub-list: move to parent item.
  4498. (goto-line 4)
  4499. (org-up-element)
  4500. (should (looking-at "- item1"))
  4501. ;; 4.3. At an item in top list: move to beginning of whole list.
  4502. (goto-line 10)
  4503. (org-up-element)
  4504. (should (looking-at "- item1"))
  4505. ;; 4.4. Special case. At very top point: should move to parent of
  4506. ;; list.
  4507. (goto-line 2)
  4508. (org-up-element)
  4509. (should (looking-at "\\* Top"))))
  4510. (ert-deftest test-org/down-element ()
  4511. "Test `org-down-element' specifications."
  4512. ;; Error when the element hasn't got a recursive type.
  4513. (org-test-with-temp-text "Paragraph."
  4514. (should-error (org-down-element)))
  4515. ;; Error when the element has no contents
  4516. (org-test-with-temp-text "* Headline"
  4517. (should-error (org-down-element)))
  4518. ;; When at a plain-list, move to first item.
  4519. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  4520. (goto-line 2)
  4521. (org-down-element)
  4522. (should (looking-at " - Item 1.1")))
  4523. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  4524. (org-down-element)
  4525. (should (looking-at " Item 1")))
  4526. ;; When at a table, move to first row
  4527. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  4528. (org-down-element)
  4529. (should (looking-at " a | b |")))
  4530. ;; Otherwise, move inside the greater element.
  4531. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  4532. (org-down-element)
  4533. (should (looking-at "Paragraph"))))
  4534. (ert-deftest test-org/drag-element-backward ()
  4535. "Test `org-drag-element-backward' specifications."
  4536. ;; Standard test.
  4537. (should
  4538. (equal
  4539. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  4540. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  4541. (org-drag-element-backward)
  4542. (buffer-string))))
  4543. (should
  4544. (equal
  4545. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  4546. (org-test-with-temp-text
  4547. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  4548. (org-drag-element-backward)
  4549. (buffer-string))))
  4550. ;; Preserve blank lines.
  4551. (should
  4552. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  4553. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  4554. (org-drag-element-backward)
  4555. (buffer-string))))
  4556. ;; Preserve column.
  4557. (should
  4558. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  4559. (org-drag-element-backward)
  4560. (looking-at-p "2")))
  4561. ;; Error when trying to move first element of buffer.
  4562. (should-error
  4563. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  4564. (org-drag-element-backward))
  4565. :type 'user-error)
  4566. ;; Error when trying to swap nested elements.
  4567. (should-error
  4568. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  4569. (org-drag-element-backward))
  4570. :type 'user-error)
  4571. ;; Error when trying to swap an headline element and a non-headline
  4572. ;; element.
  4573. (should-error
  4574. (org-test-with-temp-text "Test.\n<point>* Head 1"
  4575. (org-drag-element-backward))
  4576. :type 'error)
  4577. ;; Error when called before first element.
  4578. (should-error
  4579. (org-test-with-temp-text "\n<point>"
  4580. (org-drag-element-backward))
  4581. :type 'user-error)
  4582. ;; Preserve visibility of elements and their contents.
  4583. (should
  4584. (equal '((63 . 82) (26 . 48))
  4585. (let ((org-fold-core-style 'text-properties))
  4586. (org-test-with-temp-text
  4587. "
  4588. #+BEGIN_CENTER
  4589. Text.
  4590. #+END_CENTER
  4591. - item 1
  4592. #+BEGIN_QUOTE
  4593. Text.
  4594. #+END_QUOTE"
  4595. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4596. (search-backward "- item 1")
  4597. (org-drag-element-backward)
  4598. (let (regions)
  4599. (goto-char (point-min))
  4600. (while (< (point) (point-max))
  4601. (let ((region (org-fold-get-region-at-point)))
  4602. (if (not region)
  4603. (goto-char (org-fold-next-folding-state-change))
  4604. (goto-char (cdr region))
  4605. (push region regions))))
  4606. regions)))))
  4607. (should
  4608. (equal '((26 . 48) (63 . 82))
  4609. (let ((org-fold-core-style 'overlays))
  4610. (org-test-with-temp-text
  4611. "
  4612. #+BEGIN_CENTER
  4613. Text.
  4614. #+END_CENTER
  4615. - item 1
  4616. #+BEGIN_QUOTE
  4617. Text.
  4618. #+END_QUOTE"
  4619. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4620. (search-backward "- item 1")
  4621. (org-drag-element-backward)
  4622. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  4623. (overlays-in (point-min) (point-max)))))))
  4624. ;; Pathological case: handle call with point in blank lines right
  4625. ;; after a headline.
  4626. (should
  4627. (equal "* H2\n\n* H1\nText\n"
  4628. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  4629. (org-drag-element-backward)
  4630. (buffer-string)))))
  4631. (ert-deftest test-org/drag-element-forward ()
  4632. "Test `org-drag-element-forward' specifications."
  4633. ;; 1. Error when trying to move first element of buffer.
  4634. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  4635. (goto-line 3)
  4636. (should-error (org-drag-element-forward)))
  4637. ;; 2. Error when trying to swap nested elements.
  4638. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  4639. (forward-line)
  4640. (should-error (org-drag-element-forward)))
  4641. ;; 3. Error when trying to swap a non-headline element and an
  4642. ;; headline.
  4643. (org-test-with-temp-text "Test.\n* Head 1"
  4644. (should-error (org-drag-element-forward)))
  4645. ;; 4. Error when called before first element.
  4646. (should-error
  4647. (org-test-with-temp-text "\n"
  4648. (forward-line)
  4649. (org-drag-element-backward))
  4650. :type 'user-error)
  4651. ;; 5. Otherwise, swap elements, preserving column and blank lines
  4652. ;; between elements.
  4653. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  4654. (search-forward "graph")
  4655. (org-drag-element-forward)
  4656. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  4657. (should (looking-at " 1")))
  4658. ;; 5. Preserve visibility of elements and their contents.
  4659. (let ((org-fold-core-style 'text-properties))
  4660. (org-test-with-temp-text
  4661. "
  4662. #+BEGIN_CENTER
  4663. Text.
  4664. #+END_CENTER
  4665. - item 1
  4666. #+BEGIN_QUOTE
  4667. Text.
  4668. #+END_QUOTE"
  4669. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4670. (search-backward "#+BEGIN_CENTER")
  4671. (org-drag-element-forward)
  4672. (should
  4673. (equal
  4674. '((63 . 82) (26 . 48))
  4675. (let (regions)
  4676. (goto-char (point-min))
  4677. (while (< (point) (point-max))
  4678. (let ((region (org-fold-get-region-at-point)))
  4679. (if (not region)
  4680. (goto-char (org-fold-next-folding-state-change))
  4681. (goto-char (cdr region))
  4682. (push region regions))))
  4683. regions)))))
  4684. (let ((org-fold-core-style 'overlays))
  4685. (org-test-with-temp-text
  4686. "
  4687. #+BEGIN_CENTER
  4688. Text.
  4689. #+END_CENTER
  4690. - item 1
  4691. #+BEGIN_QUOTE
  4692. Text.
  4693. #+END_QUOTE"
  4694. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4695. (search-backward "#+BEGIN_CENTER")
  4696. (org-drag-element-forward)
  4697. (should
  4698. (equal
  4699. '((26 . 48) (63 . 82))
  4700. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  4701. (overlays-in (point-min) (point-max))))))))
  4702. (ert-deftest test-org/next-block ()
  4703. "Test `org-next-block' specifications."
  4704. ;; Regular test.
  4705. (should
  4706. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4707. (org-next-block 1)
  4708. (looking-at "#\\+BEGIN_CENTER")))
  4709. ;; Ignore case.
  4710. (should
  4711. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  4712. (let ((case-fold-search nil))
  4713. (org-next-block 1)
  4714. (looking-at "#\\+begin_center"))))
  4715. ;; Ignore current line.
  4716. (should
  4717. (org-test-with-temp-text
  4718. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  4719. (org-next-block 1)
  4720. (looking-at "#\\+BEGIN_CENTER")))
  4721. ;; Throw an error when no block is found.
  4722. (should-error
  4723. (org-test-with-temp-text "Paragraph"
  4724. (org-next-block 1)))
  4725. ;; With an argument, skip many blocks at once.
  4726. (should
  4727. (org-test-with-temp-text
  4728. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  4729. (org-next-block 2)
  4730. (looking-at "#\\+BEGIN_QUOTE")))
  4731. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4732. (should
  4733. (org-test-with-temp-text
  4734. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  4735. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  4736. (looking-at "#\\+BEGIN_QUOTE")))
  4737. ;; Optional argument is also case-insensitive.
  4738. (should
  4739. (org-test-with-temp-text
  4740. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  4741. (let ((case-fold-search nil))
  4742. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  4743. (looking-at "#\\+begin_quote")))))
  4744. (ert-deftest test-org/insert-structure-template ()
  4745. "Test `org-insert-structure-template'."
  4746. ;; Test in empty buffer.
  4747. (should
  4748. (string= "#+begin_foo\n#+end_foo\n"
  4749. (org-test-with-temp-text ""
  4750. (org-insert-structure-template "foo")
  4751. (buffer-string))))
  4752. ;; Test with multiple lines in buffer.
  4753. (should
  4754. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4755. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4756. (transient-mark-mode 1)
  4757. (org-mark-element)
  4758. (org-insert-structure-template "foo")
  4759. (buffer-string))))
  4760. ;; Mark only the current line.
  4761. (should
  4762. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4763. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4764. (transient-mark-mode 1)
  4765. (set-mark (point-min))
  4766. (end-of-line)
  4767. (org-insert-structure-template "foo")
  4768. (buffer-string))))
  4769. ;; Middle of paragraph.
  4770. (should
  4771. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4772. (org-test-with-temp-text "p1\n<point>p2\np3"
  4773. (set-mark (line-beginning-position))
  4774. (end-of-line)
  4775. (activate-mark)
  4776. (org-insert-structure-template "foo")
  4777. (buffer-string))))
  4778. ;; Test with text in buffer, no region, no final newline.
  4779. (should
  4780. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4781. (org-test-with-temp-text "I'm a paragraph."
  4782. (org-mark-element)
  4783. (org-insert-structure-template "foo")
  4784. (buffer-string))))
  4785. ;; Test with text in buffer and region set.
  4786. (should
  4787. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4788. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4789. (set-mark (point))
  4790. (goto-char (point-max))
  4791. (org-insert-structure-template "foo")
  4792. (buffer-string))))
  4793. ;; Test with example escaping.
  4794. (should
  4795. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4796. (org-test-with-temp-text "* Heading"
  4797. (org-mark-element)
  4798. (org-insert-structure-template "example")
  4799. (buffer-string))))
  4800. ;; Test with indentation.
  4801. (should
  4802. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4803. (org-test-with-temp-text " This is a paragraph"
  4804. (org-mark-element)
  4805. (org-insert-structure-template "foo")
  4806. (buffer-string))))
  4807. (should
  4808. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4809. (org-test-with-temp-text " Line 1\n Line2"
  4810. (org-mark-element)
  4811. (org-insert-structure-template "foo")
  4812. (buffer-string))))
  4813. ;; Test point location.
  4814. (should
  4815. (string= "#+begin_foo\n"
  4816. (org-test-with-temp-text ""
  4817. (org-insert-structure-template "foo")
  4818. (buffer-substring (point-min) (point)))))
  4819. (should
  4820. (string= "#+begin_src "
  4821. (org-test-with-temp-text ""
  4822. (org-insert-structure-template "src")
  4823. (buffer-substring (point-min) (point))))))
  4824. (ert-deftest test-org/previous-block ()
  4825. "Test `org-previous-block' specifications."
  4826. ;; Regular test.
  4827. (should
  4828. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4829. (org-previous-block 1)
  4830. (looking-at "#\\+BEGIN_CENTER")))
  4831. ;; Ignore case.
  4832. (should
  4833. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4834. (let ((case-fold-search nil))
  4835. (org-previous-block 1)
  4836. (looking-at "#\\+begin_center"))))
  4837. ;; Ignore current line.
  4838. (should
  4839. (org-test-with-temp-text
  4840. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4841. (org-previous-block 1)
  4842. (looking-at "#\\+BEGIN_QUOTE")))
  4843. ;; Throw an error when no block is found.
  4844. (should-error
  4845. (org-test-with-temp-text "Paragraph<point>"
  4846. (org-previous-block 1)))
  4847. ;; With an argument, skip many blocks at once.
  4848. (should
  4849. (org-test-with-temp-text
  4850. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4851. (org-previous-block 2)
  4852. (looking-at "#\\+BEGIN_CENTER")))
  4853. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4854. (should
  4855. (org-test-with-temp-text
  4856. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4857. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4858. (looking-at "#\\+BEGIN_QUOTE")))
  4859. ;; Optional argument is also case-insensitive.
  4860. (should
  4861. (org-test-with-temp-text
  4862. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4863. (let ((case-fold-search nil))
  4864. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4865. (looking-at "#\\+begin_quote")))))
  4866. ;;; Outline structure
  4867. (ert-deftest test-org/demote ()
  4868. "Test `org-demote' specifications."
  4869. ;; Add correct number of stars according to `org-odd-levels-only'.
  4870. (should
  4871. (= 2
  4872. (org-test-with-temp-text "* H"
  4873. (let ((org-odd-levels-only nil)) (org-demote))
  4874. (org-current-level))))
  4875. (should
  4876. (= 3
  4877. (org-test-with-temp-text "* H"
  4878. (let ((org-odd-levels-only t)) (org-demote))
  4879. (org-current-level))))
  4880. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4881. (should
  4882. (org-test-with-temp-text "* H :tag:"
  4883. (let ((org-tags-column 10)
  4884. (org-auto-align-tags t)
  4885. (org-odd-levels-only nil))
  4886. (org-demote))
  4887. (org-move-to-column 10)
  4888. (looking-at-p ":tag:$")))
  4889. (should-not
  4890. (org-test-with-temp-text "* H :tag:"
  4891. (let ((org-tags-column 10)
  4892. (org-auto-align-tags nil)
  4893. (org-odd-levels-only nil))
  4894. (org-demote))
  4895. (org-move-to-column 10)
  4896. (looking-at-p ":tag:$")))
  4897. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4898. ;; info and property drawers accordingly.
  4899. (should
  4900. (= 3
  4901. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4902. (let ((org-odd-levels-only nil)
  4903. (org-adapt-indentation t))
  4904. (org-demote))
  4905. (forward-line)
  4906. (org-get-indentation))))
  4907. (should
  4908. (= 3
  4909. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4910. (let ((org-odd-levels-only nil)
  4911. (org-adapt-indentation t))
  4912. (org-demote))
  4913. (forward-line)
  4914. (org-get-indentation))))
  4915. (should-not
  4916. (= 3
  4917. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4918. (let ((org-odd-levels-only nil)
  4919. (org-adapt-indentation nil))
  4920. (org-demote))
  4921. (forward-line)
  4922. (org-get-indentation))))
  4923. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4924. ;; section accordingly. Ignore, however, footnote definitions and
  4925. ;; inlinetasks boundaries.
  4926. (should
  4927. (= 3
  4928. (org-test-with-temp-text "* H\n Paragraph"
  4929. (let ((org-odd-levels-only nil)
  4930. (org-adapt-indentation t))
  4931. (org-demote))
  4932. (forward-line)
  4933. (org-get-indentation))))
  4934. (should
  4935. (= 2
  4936. (org-test-with-temp-text "* H\n Paragraph"
  4937. (let ((org-odd-levels-only nil)
  4938. (org-adapt-indentation nil))
  4939. (org-demote))
  4940. (forward-line)
  4941. (org-get-indentation))))
  4942. (should
  4943. (zerop
  4944. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4945. (let ((org-odd-levels-only nil)
  4946. (org-adapt-indentation t))
  4947. (org-demote))
  4948. (goto-char (point-max))
  4949. (org-get-indentation))))
  4950. (should
  4951. (= 3
  4952. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4953. (let ((org-odd-levels-only nil)
  4954. (org-adapt-indentation t))
  4955. (org-demote))
  4956. (goto-char (point-max))
  4957. (org-get-indentation))))
  4958. (when (featurep 'org-inlinetask)
  4959. (should
  4960. (zerop
  4961. (let ((org-inlinetask-min-level 5)
  4962. (org-adapt-indentation t))
  4963. (org-test-with-temp-text "* H\n***** I\n***** END"
  4964. (org-demote)
  4965. (forward-line)
  4966. (org-get-indentation))))))
  4967. (when (featurep 'org-inlinetask)
  4968. (should
  4969. (= 3
  4970. (let ((org-inlinetask-min-level 5)
  4971. (org-adapt-indentation t))
  4972. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4973. (org-demote)
  4974. (forward-line 2)
  4975. (org-get-indentation))))))
  4976. ;; When `org-adapt-indentation' is non-nil, log drawers are
  4977. ;; adjusted.
  4978. (should
  4979. (equal
  4980. "** H\n :LOGBOOK:\n - a\n :END:\n b"
  4981. (org-test-with-temp-text "* H\n :LOGBOOK:\n - a\n :END:\n b"
  4982. (let ((org-odd-levels-only nil)
  4983. (org-adapt-indentation t))
  4984. (org-demote))
  4985. (buffer-string))))
  4986. (should
  4987. (equal
  4988. "** H\n :LOGBOOK:\n - a\n :END:\n b"
  4989. (org-test-with-temp-text "* H\n :LOGBOOK:\n - a\n :END:\n b"
  4990. (let ((org-odd-levels-only nil)
  4991. (org-adapt-indentation 'headline-data))
  4992. (org-demote))
  4993. (buffer-string))))
  4994. (should
  4995. (equal
  4996. "** H\n :LOGBOOK:\n - a\n :END:"
  4997. (org-test-with-temp-text "* H\n:LOGBOOK:\n- a\n:END:"
  4998. (let ((org-odd-levels-only nil)
  4999. (org-adapt-indentation t))
  5000. (org-demote))
  5001. (buffer-string))))
  5002. ;; Ignore contents of source blocks or example blocks when
  5003. ;; indentation should be preserved (through
  5004. ;; `org-src-preserve-indentation' or "-i" flag).
  5005. (should-not
  5006. (zerop
  5007. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  5008. (let ((org-adapt-indentation t)
  5009. (org-src-preserve-indentation nil))
  5010. (org-demote))
  5011. (forward-line 2)
  5012. (org-get-indentation))))
  5013. (should
  5014. (zerop
  5015. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  5016. (let ((org-adapt-indentation t)
  5017. (org-src-preserve-indentation t))
  5018. (org-demote))
  5019. (forward-line 2)
  5020. (org-get-indentation))))
  5021. (should
  5022. (zerop
  5023. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  5024. (let ((org-adapt-indentation t)
  5025. (org-src-preserve-indentation t))
  5026. (org-demote))
  5027. (forward-line 2)
  5028. (org-get-indentation))))
  5029. (should
  5030. (zerop
  5031. (org-test-with-temp-text
  5032. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  5033. (let ((org-adapt-indentation t)
  5034. (org-src-preserve-indentation nil))
  5035. (org-demote))
  5036. (forward-line 2)
  5037. (org-get-indentation)))))
  5038. (ert-deftest test-org/promote ()
  5039. "Test `org-promote' specifications."
  5040. ;; Return an error if headline is to be promoted to level 0, unless
  5041. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  5042. ;; headline becomes a comment.
  5043. (should-error
  5044. (org-test-with-temp-text "* H"
  5045. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  5046. (should
  5047. (equal "# H"
  5048. (org-test-with-temp-text "* H"
  5049. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  5050. (buffer-string))))
  5051. ;; Remove correct number of stars according to
  5052. ;; `org-odd-levels-only'.
  5053. (should
  5054. (= 2
  5055. (org-test-with-temp-text "*** H"
  5056. (let ((org-odd-levels-only nil)) (org-promote))
  5057. (org-current-level))))
  5058. (should
  5059. (= 1
  5060. (org-test-with-temp-text "*** H"
  5061. (let ((org-odd-levels-only t)) (org-promote))
  5062. (org-current-level))))
  5063. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  5064. (should
  5065. (org-test-with-temp-text "** H :tag:"
  5066. (let ((org-tags-column 10)
  5067. (org-auto-align-tags t)
  5068. (org-odd-levels-only nil))
  5069. (org-promote))
  5070. (org-move-to-column 10)
  5071. (looking-at-p ":tag:$")))
  5072. (should-not
  5073. (org-test-with-temp-text "** H :tag:"
  5074. (let ((org-tags-column 10)
  5075. (org-auto-align-tags nil)
  5076. (org-odd-levels-only nil))
  5077. (org-promote))
  5078. (org-move-to-column 10)
  5079. (looking-at-p ":tag:$")))
  5080. ;; When `org-adapt-indentation' is non-nil, always indent planning
  5081. ;; info and property drawers.
  5082. (should
  5083. (= 2
  5084. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  5085. (let ((org-odd-levels-only nil)
  5086. (org-adapt-indentation t))
  5087. (org-promote))
  5088. (forward-line)
  5089. (org-get-indentation))))
  5090. (should
  5091. (= 2
  5092. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  5093. (let ((org-odd-levels-only nil)
  5094. (org-adapt-indentation t))
  5095. (org-promote))
  5096. (forward-line)
  5097. (org-get-indentation))))
  5098. (should-not
  5099. (= 2
  5100. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  5101. (let ((org-odd-levels-only nil)
  5102. (org-adapt-indentation nil))
  5103. (org-promote))
  5104. (forward-line)
  5105. (org-get-indentation))))
  5106. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  5107. ;; section accordingly. Ignore, however, footnote definitions and
  5108. ;; inlinetasks boundaries.
  5109. (should
  5110. (= 2
  5111. (org-test-with-temp-text "** H\n Paragraph"
  5112. (let ((org-odd-levels-only nil)
  5113. (org-adapt-indentation t))
  5114. (org-promote))
  5115. (forward-line)
  5116. (org-get-indentation))))
  5117. (should-not
  5118. (= 2
  5119. (org-test-with-temp-text "** H\n Paragraph"
  5120. (let ((org-odd-levels-only nil)
  5121. (org-adapt-indentation nil))
  5122. (org-promote))
  5123. (forward-line)
  5124. (org-get-indentation))))
  5125. (should
  5126. (= 2
  5127. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  5128. (let ((org-odd-levels-only nil)
  5129. (org-adapt-indentation t))
  5130. (org-promote))
  5131. (forward-line)
  5132. (org-get-indentation))))
  5133. (when (featurep 'org-inlinetask)
  5134. (should
  5135. (zerop
  5136. (let ((org-inlinetask-min-level 5)
  5137. (org-adapt-indentation t))
  5138. (org-test-with-temp-text "** H\n***** I\n***** END"
  5139. (org-promote)
  5140. (forward-line)
  5141. (org-get-indentation))))))
  5142. (when (featurep 'org-inlinetask)
  5143. (should
  5144. (= 2
  5145. (let ((org-inlinetask-min-level 5)
  5146. (org-adapt-indentation t))
  5147. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  5148. (org-promote)
  5149. (forward-line 2)
  5150. (org-get-indentation))))))
  5151. ;; Give up shifting if it would break document's structure
  5152. ;; otherwise.
  5153. (should
  5154. (= 3
  5155. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  5156. (let ((org-odd-levels-only nil)
  5157. (org-adapt-indentation t))
  5158. (org-promote))
  5159. (forward-line)
  5160. (org-get-indentation))))
  5161. (should
  5162. (= 3
  5163. (org-test-with-temp-text "** H\n Paragraph\n * list."
  5164. (let ((org-odd-levels-only nil)
  5165. (org-adapt-indentation t))
  5166. (org-promote))
  5167. (forward-line)
  5168. (org-get-indentation))))
  5169. ;; When `org-adapt-indentation' is non-nil, log drawers are
  5170. ;; adjusted.
  5171. (should
  5172. (equal
  5173. "* H\n :LOGBOOK:\n - a\n :END:\n b"
  5174. (org-test-with-temp-text "** H\n :LOGBOOK:\n - a\n :END:\n b"
  5175. (let ((org-odd-levels-only nil)
  5176. (org-adapt-indentation t))
  5177. (org-promote))
  5178. (buffer-string))))
  5179. (should
  5180. (equal
  5181. "* H\n :LOGBOOK:\n - a\n :END:\n b"
  5182. (org-test-with-temp-text "** H\n :LOGBOOK:\n - a\n :END:\n b"
  5183. (let ((org-odd-levels-only nil)
  5184. (org-adapt-indentation 'headline-data))
  5185. (org-promote))
  5186. (buffer-string))))
  5187. (should
  5188. (equal
  5189. "* H\n:LOGBOOK:\n- a\n:END:"
  5190. (org-test-with-temp-text "** H\n:LOGBOOK:\n- a\n:END:"
  5191. (let ((org-odd-levels-only nil)
  5192. (org-adapt-indentation t))
  5193. (org-promote))
  5194. (buffer-string))))
  5195. (should
  5196. (equal
  5197. "# H\n:LOGBOOK:\n- a\n:END:"
  5198. (org-test-with-temp-text "* H\n:LOGBOOK:\n- a\n:END:"
  5199. (let ((org-odd-levels-only nil)
  5200. (org-allow-promoting-top-level-subtree t)
  5201. (org-adapt-indentation t))
  5202. (org-promote))
  5203. (buffer-string))))
  5204. ;; Ignore contents of source blocks or example blocks when
  5205. ;; indentation should be preserved (through
  5206. ;; `org-src-preserve-indentation' or "-i" flag).
  5207. (should-not
  5208. (zerop
  5209. (org-test-with-temp-text
  5210. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  5211. (let ((org-adapt-indentation t)
  5212. (org-src-preserve-indentation nil)
  5213. (org-odd-levels-only nil))
  5214. (org-promote))
  5215. (forward-line)
  5216. (org-get-indentation))))
  5217. (should
  5218. (zerop
  5219. (org-test-with-temp-text
  5220. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  5221. (let ((org-adapt-indentation t)
  5222. (org-src-preserve-indentation t)
  5223. (org-odd-levels-only nil))
  5224. (org-promote))
  5225. (forward-line)
  5226. (org-get-indentation))))
  5227. (should
  5228. (zerop
  5229. (org-test-with-temp-text
  5230. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  5231. (let ((org-adapt-indentation t)
  5232. (org-src-preserve-indentation t)
  5233. (org-odd-levels-only nil))
  5234. (org-promote))
  5235. (forward-line)
  5236. (org-get-indentation))))
  5237. (should
  5238. (zerop
  5239. (org-test-with-temp-text
  5240. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  5241. (let ((org-adapt-indentation t)
  5242. (org-src-preserve-indentation nil)
  5243. (org-odd-levels-only nil))
  5244. (org-promote))
  5245. (forward-line)
  5246. (org-get-indentation)))))
  5247. (ert-deftest test-org/org-get-valid-level ()
  5248. "Test function `org-get-valid-level' specifications."
  5249. (let ((org-odd-levels-only nil))
  5250. (should (equal 1 (org-get-valid-level 0 0)))
  5251. (should (equal 1 (org-get-valid-level 0 1)))
  5252. (should (equal 2 (org-get-valid-level 0 2)))
  5253. (should (equal 3 (org-get-valid-level 0 3)))
  5254. (should (equal 1 (org-get-valid-level 1 0)))
  5255. (should (equal 2 (org-get-valid-level 1 1)))
  5256. (should (equal 23 (org-get-valid-level 1 22)))
  5257. (should (equal 1 (org-get-valid-level 1 -1)))
  5258. (should (equal 1 (org-get-valid-level 2 -1))))
  5259. (let ((org-odd-levels-only t))
  5260. (should (equal 1 (org-get-valid-level 0 0)))
  5261. (should (equal 1 (org-get-valid-level 0 1)))
  5262. (should (equal 3 (org-get-valid-level 0 2)))
  5263. (should (equal 5 (org-get-valid-level 0 3)))
  5264. (should (equal 1 (org-get-valid-level 1 0)))
  5265. (should (equal 3 (org-get-valid-level 1 1)))
  5266. (should (equal 3 (org-get-valid-level 2 1)))
  5267. (should (equal 5 (org-get-valid-level 3 1)))
  5268. (should (equal 5 (org-get-valid-level 4 1)))
  5269. (should (equal 43 (org-get-valid-level 1 21)))
  5270. (should (equal 1 (org-get-valid-level 1 -1)))
  5271. (should (equal 1 (org-get-valid-level 2 -1)))
  5272. (should (equal 1 (org-get-valid-level 3 -1)))
  5273. (should (equal 3 (org-get-valid-level 4 -1)))
  5274. (should (equal 3 (org-get-valid-level 5 -1)))))
  5275. ;;; Planning
  5276. (ert-deftest test-org/at-planning-p ()
  5277. "Test `org-at-planning-p' specifications."
  5278. ;; Regular test.
  5279. (should
  5280. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  5281. (org-at-planning-p)))
  5282. (should-not
  5283. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  5284. (org-at-planning-p)))
  5285. ;; Correctly find planning attached to inlinetasks.
  5286. (when (featurep 'org-inlinetask)
  5287. (should
  5288. (org-test-with-temp-text
  5289. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  5290. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  5291. (should-not
  5292. (org-test-with-temp-text
  5293. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  5294. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  5295. (should-not
  5296. (org-test-with-temp-text
  5297. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  5298. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  5299. (should-not
  5300. (org-test-with-temp-text
  5301. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  5302. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  5303. (ert-deftest test-org/add-planning-info ()
  5304. "Test `org-add-planning-info'."
  5305. ;; Create deadline when `org-adapt-indentation' is non-nil.
  5306. (should
  5307. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5308. (org-test-with-temp-text "* H\nParagraph<point>"
  5309. (let ((org-adapt-indentation t))
  5310. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5311. (replace-regexp-in-string
  5312. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5313. nil nil 1))))
  5314. ;; Create deadline when `org-adapt-indentation' is nil.
  5315. (should
  5316. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  5317. (org-test-with-temp-text "* H\nParagraph<point>"
  5318. (let ((org-adapt-indentation nil))
  5319. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5320. (replace-regexp-in-string
  5321. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5322. nil nil 1))))
  5323. ;; Update deadline when `org-adapt-indentation' is non-nil.
  5324. (should
  5325. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5326. (org-test-with-temp-text "\
  5327. * H
  5328. DEADLINE: <2015-06-24 Wed>
  5329. Paragraph<point>"
  5330. (let ((org-adapt-indentation t))
  5331. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5332. (replace-regexp-in-string
  5333. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5334. nil nil 1))))
  5335. ;; Update deadline when `org-adapt-indentation' is nil.
  5336. (should
  5337. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  5338. (org-test-with-temp-text "\
  5339. * H
  5340. DEADLINE: <2015-06-24 Wed>
  5341. Paragraph<point>"
  5342. (let ((org-adapt-indentation nil))
  5343. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5344. (replace-regexp-in-string
  5345. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5346. nil nil 1))))
  5347. ;; Schedule when `org-adapt-indentation' is non-nil.
  5348. (should
  5349. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  5350. (org-test-with-temp-text "* H\nParagraph<point>"
  5351. (let ((org-adapt-indentation t))
  5352. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  5353. (replace-regexp-in-string
  5354. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5355. nil nil 1))))
  5356. ;; Schedule when `org-adapt-indentation' is nil.
  5357. (should
  5358. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  5359. (org-test-with-temp-text "* H\nParagraph<point>"
  5360. (let ((org-adapt-indentation nil))
  5361. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  5362. (replace-regexp-in-string
  5363. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5364. nil nil 1))))
  5365. ;; Add deadline when scheduled.
  5366. (should
  5367. (equal "\
  5368. * H
  5369. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  5370. Paragraph"
  5371. (org-test-with-temp-text "\
  5372. * H
  5373. SCHEDULED: <2015-06-24 Wed>
  5374. Paragraph<point>"
  5375. (let ((org-adapt-indentation t))
  5376. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5377. (replace-regexp-in-string
  5378. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5379. nil nil 1))))
  5380. ;; Remove middle entry.
  5381. (should
  5382. (equal "\
  5383. * H
  5384. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  5385. Paragraph"
  5386. (org-test-with-temp-text "\
  5387. * H
  5388. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  5389. Paragraph<point>"
  5390. (let ((org-adapt-indentation t))
  5391. (org-add-planning-info nil nil 'deadline))
  5392. (replace-regexp-in-string
  5393. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  5394. nil nil 1))))
  5395. ;; Remove last entry and then middle entry (order should not
  5396. ;; matter).
  5397. (should
  5398. (equal "\
  5399. * H
  5400. CLOSED: [2015-06-24]
  5401. Paragraph"
  5402. (org-test-with-temp-text "\
  5403. * H
  5404. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  5405. Paragraph<point>"
  5406. (let ((org-adapt-indentation t))
  5407. (org-add-planning-info nil nil 'scheduled 'deadline))
  5408. (replace-regexp-in-string
  5409. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  5410. nil nil 1))))
  5411. ;; Remove closed when `org-adapt-indentation' is non-nil.
  5412. (should
  5413. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5414. (org-test-with-temp-text "\
  5415. * H
  5416. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  5417. Paragraph<point>"
  5418. (let ((org-adapt-indentation t))
  5419. (org-add-planning-info nil nil 'closed))
  5420. (replace-regexp-in-string
  5421. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5422. nil nil 1))))
  5423. (should
  5424. (equal "* H\n Paragraph"
  5425. (org-test-with-temp-text "\
  5426. * H
  5427. CLOSED: [2015-06-25 Thu]
  5428. Paragraph<point>"
  5429. (let ((org-adapt-indentation t))
  5430. (org-add-planning-info nil nil 'closed))
  5431. (replace-regexp-in-string
  5432. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5433. nil nil 1))))
  5434. ;; Remove closed when `org-adapt-indentation' is nil.
  5435. (should
  5436. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  5437. (org-test-with-temp-text "\
  5438. * H
  5439. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  5440. Paragraph<point>"
  5441. (let ((org-adapt-indentation nil))
  5442. (org-add-planning-info nil nil 'closed))
  5443. (replace-regexp-in-string
  5444. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5445. nil nil 1))))
  5446. (should
  5447. (equal "* H\nParagraph"
  5448. (org-test-with-temp-text "\
  5449. * H
  5450. CLOSED: [2015-06-25 Thu]
  5451. Paragraph<point>"
  5452. (let ((org-adapt-indentation nil))
  5453. (org-add-planning-info nil nil 'closed))
  5454. (replace-regexp-in-string
  5455. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5456. nil nil 1))))
  5457. ;; Remove closed entry and delete empty line.
  5458. (should
  5459. (equal "\
  5460. * H
  5461. Paragraph"
  5462. (org-test-with-temp-text "\
  5463. * H
  5464. CLOSED: [2015-06-24 Wed]
  5465. Paragraph<point>"
  5466. (let ((org-adapt-indentation t))
  5467. (org-add-planning-info nil nil 'closed))
  5468. (replace-regexp-in-string
  5469. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5470. nil nil 1))))
  5471. ;; Remove one entry and update another.
  5472. (should
  5473. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5474. (org-test-with-temp-text "\
  5475. * H
  5476. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  5477. Paragraph<point>"
  5478. (let ((org-adapt-indentation t))
  5479. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  5480. (replace-regexp-in-string
  5481. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5482. nil nil 1)))))
  5483. (ert-deftest test-org/deadline ()
  5484. "Test `org-deadline' specifications."
  5485. ;; Insert a new value or replace existing one.
  5486. (should
  5487. (equal "* H\nDEADLINE: <2012-03-29>"
  5488. (org-test-with-temp-text "* H"
  5489. (let ((org-adapt-indentation nil)
  5490. (org-last-inserted-timestamp nil))
  5491. (org-deadline nil "<2012-03-29 Tue>"))
  5492. (replace-regexp-in-string
  5493. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5494. nil nil 1))))
  5495. (should
  5496. (equal "* H\nDEADLINE: <2014-03-04>"
  5497. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  5498. (let ((org-adapt-indentation nil)
  5499. (org-last-inserted-timestamp nil))
  5500. (org-deadline nil "<2014-03-04 Thu>"))
  5501. (replace-regexp-in-string
  5502. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5503. nil nil 1))))
  5504. ;; Accept delta time, e.g., "+2d".
  5505. (should
  5506. (equal "* H\nDEADLINE: <2015-03-04>"
  5507. (org-test-at-time "2014-03-04"
  5508. (org-test-with-temp-text "* H"
  5509. (let ((org-adapt-indentation nil)
  5510. (org-last-inserted-timestamp nil))
  5511. (org-deadline nil "+1y"))
  5512. (replace-regexp-in-string
  5513. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  5514. ;; Preserve repeater.
  5515. (should
  5516. (equal "* H\nDEADLINE: <2012-03-29 +2y>"
  5517. (org-test-with-temp-text "* H"
  5518. (let ((org-adapt-indentation nil)
  5519. (org-last-inserted-timestamp nil))
  5520. (org-deadline nil "<2012-03-29 Tue +2y>"))
  5521. (replace-regexp-in-string
  5522. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5523. ;; Preserve warning period.
  5524. (should
  5525. (equal "* H\nDEADLINE: <2021-07-20 -1d>"
  5526. (org-test-with-temp-text "* H"
  5527. (let ((org-adapt-indentation nil)
  5528. (org-last-inserted-timestamp nil))
  5529. (org-deadline nil "<2021-07-20 Tue -1d>"))
  5530. (replace-regexp-in-string
  5531. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5532. (should
  5533. (equal "* H\nDEADLINE: <2021-07-20 +1m -3d>"
  5534. (org-test-with-temp-text "* H"
  5535. (let ((org-adapt-indentation nil)
  5536. (org-last-inserted-timestamp nil))
  5537. (org-deadline nil "<2021-07-20 Tue +1m -3d>"))
  5538. (replace-regexp-in-string
  5539. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5540. ;; Remove CLOSED keyword, if any.
  5541. (should
  5542. (equal "* H\nDEADLINE: <2012-03-29>"
  5543. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  5544. (let ((org-adapt-indentation nil)
  5545. (org-last-inserted-timestamp nil))
  5546. (org-deadline nil "<2012-03-29 Tue>"))
  5547. (replace-regexp-in-string
  5548. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5549. ;; With C-u argument, remove DEADLINE keyword.
  5550. (should
  5551. (equal "* H\n"
  5552. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  5553. (let ((org-adapt-indentation nil)
  5554. (org-last-inserted-timestamp nil))
  5555. (org-deadline '(4)))
  5556. (buffer-string))))
  5557. (should
  5558. (equal "* H"
  5559. (org-test-with-temp-text "* H"
  5560. (let ((org-adapt-indentation nil)
  5561. (org-last-inserted-timestamp nil))
  5562. (org-deadline '(4)))
  5563. (buffer-string))))
  5564. ;; With C-u C-u argument, prompt for a delay cookie.
  5565. (should
  5566. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  5567. (cl-letf (((symbol-function 'org-read-date)
  5568. (lambda (&rest _args)
  5569. (org-time-string-to-time "2014-03-04"))))
  5570. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  5571. (let ((org-adapt-indentation nil)
  5572. (org-last-inserted-timestamp nil))
  5573. (org-deadline '(16)))
  5574. (buffer-string)))))
  5575. (should-error
  5576. (cl-letf (((symbol-function 'org-read-date)
  5577. (lambda (&rest _args)
  5578. (org-time-string-to-time "2014-03-04"))))
  5579. (org-test-with-temp-text "* H"
  5580. (let ((org-adapt-indentation nil)
  5581. (org-last-inserted-timestamp nil))
  5582. (org-deadline '(16)))
  5583. (buffer-string))))
  5584. ;; When a region is active and
  5585. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  5586. ;; same value in all headlines in region.
  5587. (should
  5588. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  5589. (org-test-with-temp-text "* H1\n* H2"
  5590. (let ((org-adapt-indentation nil)
  5591. (org-last-inserted-timestamp nil)
  5592. (org-loop-over-headlines-in-active-region t))
  5593. (transient-mark-mode 1)
  5594. (push-mark (point) t t)
  5595. (goto-char (point-max))
  5596. (org-deadline nil "2012-03-29"))
  5597. (replace-regexp-in-string
  5598. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5599. (should-not
  5600. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  5601. (org-test-with-temp-text "* H1\n* H2"
  5602. (let ((org-adapt-indentation nil)
  5603. (org-last-inserted-timestamp nil)
  5604. (org-loop-over-headlines-in-active-region nil))
  5605. (transient-mark-mode 1)
  5606. (push-mark (point) t t)
  5607. (goto-char (point-max))
  5608. (org-deadline nil "2012-03-29"))
  5609. (replace-regexp-in-string
  5610. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  5611. (ert-deftest test-org/schedule ()
  5612. "Test `org-schedule' specifications."
  5613. ;; Insert a new value or replace existing one.
  5614. (should
  5615. (equal "* H\nSCHEDULED: <2012-03-29>"
  5616. (org-test-with-temp-text "* H"
  5617. (let ((org-adapt-indentation nil)
  5618. (org-last-inserted-timestamp nil))
  5619. (org-schedule nil "<2012-03-29 Tue>"))
  5620. (replace-regexp-in-string
  5621. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5622. nil nil 1))))
  5623. (should
  5624. (equal "* H\nSCHEDULED: <2014-03-04>"
  5625. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  5626. (let ((org-adapt-indentation nil)
  5627. (org-last-inserted-timestamp nil))
  5628. (org-schedule nil "<2014-03-04 Thu>"))
  5629. (replace-regexp-in-string
  5630. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5631. nil nil 1))))
  5632. ;; Accept delta time, e.g., "+2d".
  5633. (should
  5634. (equal "* H\nSCHEDULED: <2015-03-04>"
  5635. (org-test-at-time "2014-03-04"
  5636. (org-test-with-temp-text "* H"
  5637. (let ((org-adapt-indentation nil)
  5638. (org-last-inserted-timestamp nil))
  5639. (org-schedule nil "+1y"))
  5640. (replace-regexp-in-string
  5641. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  5642. ;; Preserve repeater.
  5643. (should
  5644. (equal "* H\nSCHEDULED: <2012-03-29 +2y>"
  5645. (org-test-with-temp-text "* H"
  5646. (let ((org-adapt-indentation nil)
  5647. (org-last-inserted-timestamp nil))
  5648. (org-schedule nil "<2012-03-29 Tue +2y>"))
  5649. (replace-regexp-in-string
  5650. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5651. ;; Remove CLOSED keyword, if any.
  5652. (should
  5653. (equal "* H\nSCHEDULED: <2012-03-29>"
  5654. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  5655. (let ((org-adapt-indentation nil)
  5656. (org-last-inserted-timestamp nil))
  5657. (org-schedule nil "<2012-03-29 Tue>"))
  5658. (replace-regexp-in-string
  5659. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5660. ;; With C-u argument, remove SCHEDULED keyword.
  5661. (should
  5662. (equal "* H\n"
  5663. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  5664. (let ((org-adapt-indentation nil)
  5665. (org-last-inserted-timestamp nil))
  5666. (org-schedule '(4)))
  5667. (buffer-string))))
  5668. (should
  5669. (equal "* H"
  5670. (org-test-with-temp-text "* H"
  5671. (let ((org-adapt-indentation nil)
  5672. (org-last-inserted-timestamp nil))
  5673. (org-schedule '(4)))
  5674. (buffer-string))))
  5675. ;; With C-u C-u argument, prompt for a delay cookie.
  5676. (should
  5677. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  5678. (cl-letf (((symbol-function 'org-read-date)
  5679. (lambda (&rest _args)
  5680. (org-time-string-to-time "2014-03-04"))))
  5681. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  5682. (let ((org-adapt-indentation nil)
  5683. (org-last-inserted-timestamp nil))
  5684. (org-schedule '(16)))
  5685. (buffer-string)))))
  5686. (should-error
  5687. (cl-letf (((symbol-function 'org-read-date)
  5688. (lambda (&rest _args)
  5689. (org-time-string-to-time "2014-03-04"))))
  5690. (org-test-with-temp-text "* H"
  5691. (let ((org-adapt-indentation nil)
  5692. (org-last-inserted-timestamp nil))
  5693. (org-schedule '(16)))
  5694. (buffer-string))))
  5695. ;; When a region is active and
  5696. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  5697. ;; same value in all headlines in region.
  5698. (should
  5699. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  5700. (org-test-with-temp-text "* H1\n* H2"
  5701. (let ((org-adapt-indentation nil)
  5702. (org-last-inserted-timestamp nil)
  5703. (org-loop-over-headlines-in-active-region t))
  5704. (transient-mark-mode 1)
  5705. (push-mark (point) t t)
  5706. (goto-char (point-max))
  5707. (org-schedule nil "2012-03-29"))
  5708. (replace-regexp-in-string
  5709. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5710. (should-not
  5711. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  5712. (org-test-with-temp-text "* H1\n* H2"
  5713. (let ((org-adapt-indentation nil)
  5714. (org-last-inserted-timestamp nil)
  5715. (org-loop-over-headlines-in-active-region nil))
  5716. (transient-mark-mode 1)
  5717. (push-mark (point) t t)
  5718. (goto-char (point-max))
  5719. (org-schedule nil "2012-03-29"))
  5720. (replace-regexp-in-string
  5721. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5722. (should
  5723. ;; check if a repeater survives re-scheduling.
  5724. (string-match-p
  5725. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  5726. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  5727. (let ((org-adapt-indentation nil)
  5728. (org-last-inserted-timestamp nil))
  5729. (org-schedule nil "2017-02-01"))
  5730. (buffer-string)))))
  5731. ;;; Property API
  5732. (ert-deftest test-org/buffer-property-keys ()
  5733. "Test `org-buffer-property-keys' specifications."
  5734. ;; Retrieve properties across siblings.
  5735. (should
  5736. (equal '("A" "B")
  5737. (org-test-with-temp-text "
  5738. * H1
  5739. :PROPERTIES:
  5740. :A: 1
  5741. :END:
  5742. * H2
  5743. :PROPERTIES:
  5744. :B: 1
  5745. :END:"
  5746. (org-buffer-property-keys))))
  5747. ;; Retrieve properties across children.
  5748. (should
  5749. (equal '("A" "B")
  5750. (org-test-with-temp-text "
  5751. * H1
  5752. :PROPERTIES:
  5753. :A: 1
  5754. :END:
  5755. ** H2
  5756. :PROPERTIES:
  5757. :B: 1
  5758. :END:"
  5759. (org-buffer-property-keys))))
  5760. ;; Retrieve muliple properties in the same drawer.
  5761. (should
  5762. (equal '("A" "B")
  5763. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5764. (org-buffer-property-keys))))
  5765. ;; Ignore extension symbol in property name.
  5766. (should
  5767. (equal '("A")
  5768. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5769. (org-buffer-property-keys))))
  5770. ;; Add bare property if xxx_ALL property is there
  5771. (should
  5772. (equal '("A" "B" "B_ALL")
  5773. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B_ALL: foo bar\n:END:"
  5774. (org-buffer-property-keys))))
  5775. ;; Add bare property if xxx_ALL property is there - check dupes
  5776. (should
  5777. (equal '("A" "B" "B_ALL")
  5778. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:B_ALL: foo bar\n:END:"
  5779. (org-buffer-property-keys))))
  5780. ;; Retrieve properties from #+PROPERTY keyword lines
  5781. (should
  5782. (equal '("A" "C")
  5783. (org-test-with-temp-text "#+PROPERTY: C foo\n* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5784. (org-buffer-property-keys))))
  5785. ;; Retrieve properties from #+PROPERTY keyword lines - make sure an _ALL property also
  5786. ;; adds the bare property
  5787. (should
  5788. (equal '("A" "C" "C_ALL")
  5789. (org-test-with-temp-text "#+PROPERTY: C_ALL foo bar\n* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5790. (org-buffer-property-keys))))
  5791. ;; With non-nil COLUMNS, extract property names from columns.
  5792. (should
  5793. (equal '("A" "B")
  5794. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  5795. (org-buffer-property-keys nil nil t))))
  5796. (should
  5797. (equal '("A" "B" "COLUMNS")
  5798. (org-test-with-temp-text
  5799. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  5800. (org-buffer-property-keys nil nil t))))
  5801. ;; In COLUMNS, ignore title and summary-type.
  5802. (should
  5803. (equal '("A")
  5804. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  5805. (org-buffer-property-keys nil nil t))))
  5806. (should
  5807. (equal '("A")
  5808. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  5809. (org-buffer-property-keys nil nil t))))
  5810. (should
  5811. (equal '("A")
  5812. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  5813. (org-buffer-property-keys nil nil t)))))
  5814. (ert-deftest test-org/property-values ()
  5815. "Test `org-property-values' specifications."
  5816. ;; Regular test.
  5817. (should
  5818. (equal '("2" "1")
  5819. (org-test-with-temp-text
  5820. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  5821. (org-property-values "A"))))
  5822. ;; Ignore empty values.
  5823. (should-not
  5824. (org-test-with-temp-text
  5825. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  5826. (org-property-values "A")))
  5827. ;; Take into consideration extended values.
  5828. (should
  5829. (equal '("1 2")
  5830. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5831. (org-property-values "A")))))
  5832. (ert-deftest test-org/set-property ()
  5833. "Test `org-set-property' specifications."
  5834. (should
  5835. (equal
  5836. ":PROPERTIES:\n:TEST: t\n:END:\n"
  5837. (org-test-with-temp-text ""
  5838. (let ((org-property-format "%s %s"))
  5839. (org-set-property "TEST" "t"))
  5840. (buffer-string))))
  5841. (should
  5842. (equal
  5843. "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5844. (org-test-with-temp-text "* H"
  5845. (let ((org-adapt-indentation nil)
  5846. (org-property-format "%s %s"))
  5847. (org-set-property "TEST" "t"))
  5848. (buffer-string)))))
  5849. (ert-deftest test-org/delete-property ()
  5850. "Test `org-delete-property' specifications."
  5851. (should
  5852. (equal
  5853. ""
  5854. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5855. (org-delete-property "TEST")
  5856. (buffer-string))))
  5857. (should
  5858. (equal
  5859. ":PROPERTIES:\n:TEST1: t\n:END:\n"
  5860. (org-test-with-temp-text ":PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5861. (org-delete-property "TEST2")
  5862. (buffer-string))))
  5863. (should
  5864. (equal
  5865. "* H\n"
  5866. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5867. (org-delete-property "TEST")
  5868. (buffer-string))))
  5869. (should
  5870. (equal
  5871. "* H\n:PROPERTIES:\n:TEST1: t\n:END:\n"
  5872. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5873. (org-delete-property "TEST2")
  5874. (buffer-string)))))
  5875. (ert-deftest test-org/delete-property-globally ()
  5876. "Test `org-delete-property-global' specifications."
  5877. (should
  5878. (equal
  5879. ""
  5880. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5881. (org-delete-property-globally "TEST")
  5882. (buffer-string))))
  5883. (should
  5884. (equal
  5885. "* H\n"
  5886. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n* H\n:PROPERTIES:\n:TEST: nil\n:END:"
  5887. (org-delete-property-globally "TEST")
  5888. (buffer-string)))))
  5889. (ert-deftest test-org/find-property ()
  5890. "Test `org-find-property' specifications."
  5891. ;; Regular test.
  5892. (should
  5893. (= 1
  5894. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  5895. (org-find-property "prop"))))
  5896. ;; Find properties in top-level property drawer.
  5897. (should
  5898. (= 1
  5899. (org-test-with-temp-text ":PROPERTIES:\n:PROP: value\n:END:"
  5900. (org-find-property "prop"))))
  5901. ;; Ignore false positives.
  5902. (should
  5903. (= 27
  5904. (org-test-with-temp-text
  5905. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  5906. (org-find-property "A"))))
  5907. ;; Return first entry found in buffer.
  5908. (should
  5909. (= 1
  5910. (org-test-with-temp-text
  5911. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5912. (org-find-property "A"))))
  5913. ;; Only search visible part of the buffer.
  5914. (should
  5915. (= 31
  5916. (org-test-with-temp-text
  5917. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5918. (org-narrow-to-subtree)
  5919. (org-find-property "A"))))
  5920. ;; With optional argument, only find entries with a specific value.
  5921. (should-not
  5922. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5923. (org-find-property "A" "2")))
  5924. (should
  5925. (= 31
  5926. (org-test-with-temp-text
  5927. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5928. (org-find-property "A" "2"))))
  5929. ;; Use "nil" for explicit nil values.
  5930. (should
  5931. (= 31
  5932. (org-test-with-temp-text
  5933. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5934. (org-find-property "A" "nil")))))
  5935. (ert-deftest test-org/entry-delete ()
  5936. "Test `org-entry-delete' specifications."
  5937. ;; Regular test.
  5938. (should
  5939. (string-match
  5940. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5941. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5942. (org-entry-delete (point) "A")
  5943. (buffer-string))))
  5944. ;; Also remove accumulated properties.
  5945. (should-not
  5946. (string-match
  5947. ":A"
  5948. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5949. (org-entry-delete (point) "A")
  5950. (buffer-string))))
  5951. ;; When last property is removed, remove the property drawer.
  5952. (should-not
  5953. (string-match
  5954. ":PROPERTIES:"
  5955. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5956. (org-entry-delete (point) "A")
  5957. (buffer-string))))
  5958. ;; Return a non-nil value when some property was removed.
  5959. (should
  5960. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5961. (org-entry-delete (point) "A")))
  5962. (should-not
  5963. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5964. (org-entry-delete (point) "C")))
  5965. ;; Special properties cannot be located in a drawer. Allow to
  5966. ;; remove them anyway, in case of user error.
  5967. (should
  5968. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5969. (org-entry-delete (point) "SCHEDULED"))))
  5970. (ert-deftest test-org/entry-get ()
  5971. "Test `org-entry-get' specifications."
  5972. ;; Regular test.
  5973. (should
  5974. (equal "1"
  5975. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:"
  5976. (org-entry-get (point) "A"))))
  5977. (should
  5978. (equal "1"
  5979. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5980. (org-entry-get (point) "A"))))
  5981. ;; Ignore case.
  5982. (should
  5983. (equal "1"
  5984. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5985. (org-entry-get (point) "a"))))
  5986. ;; Handle extended values, both before and after base value.
  5987. (should
  5988. (equal "1 2 3"
  5989. (org-test-with-temp-text
  5990. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5991. (org-entry-get (point) "A"))))
  5992. ;; Empty values are returned as the empty string.
  5993. (should
  5994. (equal ""
  5995. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5996. (org-entry-get (point) "A"))))
  5997. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5998. ;; otherwise, return nil.
  5999. (should-not
  6000. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  6001. (org-entry-get (point) "A")))
  6002. (should
  6003. (equal "nil"
  6004. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  6005. (org-entry-get (point) "A" nil t))))
  6006. ;; Return nil when no property is found, independently on the
  6007. ;; LITERAL-NIL argument.
  6008. (should-not
  6009. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6010. (org-entry-get (point) "B")))
  6011. (should-not
  6012. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6013. (org-entry-get (point) "B" nil t)))
  6014. ;; Handle inheritance, when allowed. Include extended values and
  6015. ;; possibly global values.
  6016. (should
  6017. (equal
  6018. "1"
  6019. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:\n* H"
  6020. (org-entry-get (point-max) "A" t))))
  6021. (should
  6022. (equal
  6023. "1"
  6024. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  6025. (org-entry-get (point-max) "A" t))))
  6026. (should
  6027. (equal
  6028. "1"
  6029. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  6030. (let ((org-use-property-inheritance t))
  6031. (org-entry-get (point-max) "A" 'selective)))))
  6032. (should-not
  6033. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  6034. (let ((org-use-property-inheritance nil))
  6035. (org-entry-get (point-max) "A" 'selective))))
  6036. (should-not
  6037. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H2"
  6038. (let ((org-use-property-inheritance t))
  6039. (org-entry-get (point-max) "A" t))))
  6040. (should
  6041. (equal
  6042. "1 2"
  6043. (org-test-with-temp-text
  6044. ":PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  6045. (org-entry-get (point-max) "A" t))))
  6046. (should
  6047. (equal
  6048. "1 2"
  6049. (org-test-with-temp-text
  6050. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  6051. (org-entry-get (point-max) "A" t))))
  6052. (should
  6053. (equal
  6054. "1 2"
  6055. (org-test-with-temp-text
  6056. ":PROPERTIES:\n:A: 1\n:END:\n* H1\n* H2\n:PROPERTIES:\n:A+: 2\n:END:"
  6057. (org-entry-get (point-max) "A" t))))
  6058. (should
  6059. (equal
  6060. "1 2"
  6061. (org-test-with-temp-text
  6062. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2.1\n** H2.2\n:PROPERTIES:\n:A+: 2\n:END:"
  6063. (org-entry-get (point-max) "A" t))))
  6064. (should
  6065. (equal "1"
  6066. (org-test-with-temp-text
  6067. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  6068. (org-mode-restart)
  6069. (org-entry-get (point-max) "A" t))))
  6070. (should
  6071. (equal "0 1"
  6072. (org-test-with-temp-text
  6073. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  6074. (org-mode-restart)
  6075. (org-entry-get (point-max) "A" t))))
  6076. ;; document level property-drawer has precedance over
  6077. ;; global-property by PROPERTY-keyword.
  6078. (should
  6079. (equal "0 2"
  6080. (org-test-with-temp-text
  6081. ":PROPERTIES:\n:A: 0\n:END:\n#+PROPERTY: A 1\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  6082. (org-mode-restart)
  6083. (org-entry-get (point-max) "A" t))))
  6084. ;; Use alternate separators
  6085. (should
  6086. (equal "0~2"
  6087. (org-test-with-temp-text
  6088. ":PROPERTIES:\n:A: 0\n:A+: 2\n:END:"
  6089. (let ((org-property-separators '((("A") . "~"))))
  6090. (org-entry-get (point) "A")))))
  6091. ;; Default separator is single space
  6092. (should
  6093. (equal "0 2"
  6094. (org-test-with-temp-text
  6095. ":PROPERTIES:\n:A: 0\n:B: 1\n:A+: 2\n:B+: 3\n:END:"
  6096. (let ((org-property-separators '((("B") . "~"))))
  6097. (org-entry-get (point) "A")))))
  6098. ;; Regular expression matching for separator
  6099. (should
  6100. (equal "0/2"
  6101. (org-test-with-temp-text
  6102. ":PROPERTIES:\n:A: 0\n:A+: 2\n:END:"
  6103. (let ((org-property-separators '((("B") . "~") ("[AC]" . "/"))))
  6104. (org-entry-get (point) "A")))))
  6105. ;; Separator works with inheritance
  6106. (should
  6107. (equal "1~2"
  6108. (org-test-with-temp-text
  6109. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  6110. (let ((org-property-separators '((("A") . "~"))))
  6111. (org-entry-get (point-max) "A" t))))))
  6112. (ert-deftest test-org/entry-properties ()
  6113. "Test `org-entry-properties' specifications."
  6114. ;; Get "ITEM" property.
  6115. (should
  6116. (equal "H"
  6117. (org-test-with-temp-text "* TODO H"
  6118. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  6119. (should
  6120. (equal "H"
  6121. (org-test-with-temp-text "* TODO H"
  6122. (cdr (assoc "ITEM" (org-entry-properties))))))
  6123. ;; Get "TODO" property. TODO keywords are case sensitive.
  6124. (should
  6125. (equal "TODO"
  6126. (org-test-with-temp-text "* TODO H"
  6127. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  6128. (should
  6129. (equal "TODO"
  6130. (org-test-with-temp-text "* TODO H"
  6131. (cdr (assoc "TODO" (org-entry-properties))))))
  6132. (should-not
  6133. (org-test-with-temp-text "* H"
  6134. (assoc "TODO" (org-entry-properties nil "TODO"))))
  6135. (should-not
  6136. (org-test-with-temp-text "* todo H"
  6137. (assoc "TODO" (org-entry-properties nil "TODO"))))
  6138. ;; Get "PRIORITY" property.
  6139. (should
  6140. (equal "A"
  6141. (org-test-with-temp-text "* [#A] H"
  6142. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  6143. (should
  6144. (equal "A"
  6145. (org-test-with-temp-text "* [#A] H"
  6146. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  6147. (should
  6148. (equal (char-to-string org-priority-default)
  6149. (org-test-with-temp-text "* H"
  6150. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  6151. ;; Get "FILE" property.
  6152. (should
  6153. (org-test-with-temp-text-in-file "* H\nParagraph"
  6154. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  6155. (buffer-file-name))))
  6156. (should
  6157. (org-test-with-temp-text-in-file "* H\nParagraph"
  6158. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  6159. (buffer-file-name))))
  6160. (should-not
  6161. (org-test-with-temp-text "* H\nParagraph"
  6162. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  6163. ;; Get "TAGS" property.
  6164. (should
  6165. (equal ":tag1:tag2:"
  6166. (org-test-with-temp-text "* H :tag1:tag2:"
  6167. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  6168. (should
  6169. (equal ":tag1:tag2:"
  6170. (org-test-with-temp-text "* H :tag1:tag2:"
  6171. (cdr (assoc "TAGS" (org-entry-properties))))))
  6172. (should-not
  6173. (org-test-with-temp-text "* H"
  6174. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  6175. ;; Get "ALLTAGS" property.
  6176. (should
  6177. (equal ":tag1:tag2:"
  6178. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  6179. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  6180. (should
  6181. (equal ":tag1:tag2:"
  6182. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  6183. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  6184. (should-not
  6185. (org-test-with-temp-text "* H"
  6186. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  6187. ;; Get "BLOCKED" property.
  6188. (should
  6189. (equal "t"
  6190. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  6191. (let ((org-enforce-todo-dependencies t)
  6192. (org-blocker-hook
  6193. '(org-block-todo-from-children-or-siblings-or-parent)))
  6194. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  6195. (should
  6196. (equal ""
  6197. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  6198. (let ((org-enforce-todo-dependencies t)
  6199. (org-blocker-hook
  6200. '(org-block-todo-from-children-or-siblings-or-parent)))
  6201. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  6202. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  6203. (should
  6204. (equal
  6205. "[2012-03-29 thu.]"
  6206. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  6207. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  6208. (should
  6209. (equal
  6210. "[2012-03-29 thu.]"
  6211. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  6212. (cdr (assoc "CLOSED" (org-entry-properties))))))
  6213. (should-not
  6214. (org-test-with-temp-text "* H"
  6215. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  6216. (should
  6217. (equal
  6218. "<2014-03-04 tue.>"
  6219. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6220. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  6221. (should
  6222. (equal
  6223. "<2014-03-04 tue.>"
  6224. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6225. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  6226. (should-not
  6227. (org-test-with-temp-text "* H"
  6228. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  6229. (should
  6230. (equal
  6231. "<2014-03-04 tue.>"
  6232. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6233. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  6234. (should
  6235. (equal
  6236. "<2014-03-04 tue.>"
  6237. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6238. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  6239. (should-not
  6240. (org-test-with-temp-text "* H"
  6241. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  6242. ;; Get "CATEGORY"
  6243. (should
  6244. (equal "cat"
  6245. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  6246. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  6247. (should
  6248. (equal "cat"
  6249. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  6250. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  6251. (should
  6252. (equal "cat"
  6253. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  6254. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  6255. (should
  6256. (equal "cat2"
  6257. (org-test-with-temp-text
  6258. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  6259. "\n"
  6260. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  6261. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  6262. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  6263. (should
  6264. (equal "<2012-03-29 thu.>"
  6265. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  6266. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  6267. (should
  6268. (equal "[2012-03-29 thu.]"
  6269. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  6270. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  6271. (should
  6272. (equal "<2012-03-29 thu.>"
  6273. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  6274. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  6275. (should
  6276. (equal "[2014-03-04 tue.]"
  6277. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  6278. (cdr (assoc "TIMESTAMP_IA"
  6279. (org-entry-properties nil "TIMESTAMP_IA"))))))
  6280. (should-not
  6281. (equal "<2012-03-29 thu.>"
  6282. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  6283. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  6284. ;; Get standard properties.
  6285. (should
  6286. (equal "1"
  6287. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6288. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  6289. ;; Handle extended properties.
  6290. (should
  6291. (equal "1 2 3"
  6292. (org-test-with-temp-text
  6293. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  6294. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  6295. (should
  6296. (equal "1 2 3"
  6297. (org-test-with-temp-text
  6298. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  6299. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  6300. ;; Ignore forbidden (special) properties.
  6301. (should-not
  6302. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  6303. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  6304. (ert-deftest test-org/entry-put ()
  6305. "Test `org-entry-put' specifications."
  6306. ;; Error when not a string or nil.
  6307. (should-error
  6308. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  6309. (org-entry-put 1 "test" 2)))
  6310. ;; Error when property name is invalid.
  6311. (should-error
  6312. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  6313. (org-entry-put 1 "no space" "value")))
  6314. (should-error
  6315. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  6316. (org-entry-put 1 "" "value")))
  6317. ;; Set "TODO" property.
  6318. (should
  6319. (string-match (regexp-quote " TODO H")
  6320. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  6321. (org-entry-put (point) "TODO" "TODO")
  6322. (buffer-string))))
  6323. (should
  6324. (string-match (regexp-quote "* H")
  6325. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  6326. (org-entry-put (point) "TODO" nil)
  6327. (buffer-string))))
  6328. ;; Set "PRIORITY" property.
  6329. (should
  6330. (equal "* [#A] H"
  6331. (org-test-with-temp-text "* [#B] H"
  6332. (org-entry-put (point) "PRIORITY" "A")
  6333. (buffer-string))))
  6334. (should
  6335. (equal "* H"
  6336. (org-test-with-temp-text "* [#B] H"
  6337. (org-entry-put (point) "PRIORITY" nil)
  6338. (buffer-string))))
  6339. ;; Set "SCHEDULED" property.
  6340. (should
  6341. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  6342. (org-test-with-temp-text "* H"
  6343. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  6344. (buffer-string))))
  6345. (should
  6346. (string= "* H\n"
  6347. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6348. (org-entry-put (point) "SCHEDULED" nil)
  6349. (buffer-string))))
  6350. (should
  6351. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  6352. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6353. (org-entry-put (point) "SCHEDULED" "earlier")
  6354. (buffer-string))))
  6355. (should
  6356. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  6357. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6358. (org-entry-put (point) "SCHEDULED" "later")
  6359. (buffer-string))))
  6360. ;; Set "DEADLINE" property.
  6361. (should
  6362. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  6363. (org-test-with-temp-text "* H"
  6364. (org-entry-put (point) "DEADLINE" "2014-03-04")
  6365. (buffer-string))))
  6366. (should
  6367. (string= "* H\n"
  6368. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6369. (org-entry-put (point) "DEADLINE" nil)
  6370. (buffer-string))))
  6371. (should
  6372. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  6373. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6374. (org-entry-put (point) "DEADLINE" "earlier")
  6375. (buffer-string))))
  6376. (should
  6377. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  6378. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6379. (org-entry-put (point) "DEADLINE" "later")
  6380. (buffer-string))))
  6381. ;; Set "CATEGORY" property
  6382. (should
  6383. (string-match "^ *:CATEGORY: cat"
  6384. (org-test-with-temp-text "* H"
  6385. (org-entry-put (point) "CATEGORY" "cat")
  6386. (buffer-string))))
  6387. ;; Regular properties, with or without pre-existing drawer.
  6388. (should
  6389. (string-match "^ *:A: +2$"
  6390. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6391. (org-entry-put (point) "A" "2")
  6392. (buffer-string))))
  6393. (should
  6394. (string-match "^ *:A: +1$"
  6395. (org-test-with-temp-text "* H"
  6396. (org-entry-put (point) "A" "1")
  6397. (buffer-string))))
  6398. ;; Special case: two consecutive headlines.
  6399. (should
  6400. (string-match "\\* A\n *:PROPERTIES:"
  6401. (org-test-with-temp-text "* A\n** B"
  6402. (org-entry-put (point) "A" "1")
  6403. (buffer-string)))))
  6404. (ert-deftest test-org/refresh-properties ()
  6405. "Test `org-refresh-properties' specifications."
  6406. (should
  6407. (equal "1"
  6408. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6409. (org-refresh-properties "A" 'org-test)
  6410. (get-text-property (point) 'org-test))))
  6411. (should-not
  6412. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6413. (org-refresh-properties "B" 'org-test)
  6414. (get-text-property (point) 'org-test)))
  6415. ;; Handle properties only defined with extension syntax, i.e.,
  6416. ;; "PROPERTY+".
  6417. (should
  6418. (equal "1"
  6419. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  6420. (org-refresh-properties "A" 'org-test)
  6421. (get-text-property (point) 'org-test))))
  6422. ;; When property is inherited, add text property to the whole
  6423. ;; sub-tree.
  6424. (should
  6425. (equal "1"
  6426. (org-test-with-temp-text
  6427. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  6428. (let ((org-use-property-inheritance t))
  6429. (org-refresh-properties "A" 'org-test))
  6430. (get-text-property (point) 'org-test))))
  6431. ;; When a document level property-drawer is used, those properties
  6432. ;; should work exactly like headline-properties as if at a
  6433. ;; headline-level 0.
  6434. (should
  6435. (equal "1"
  6436. (org-test-with-temp-text
  6437. ":PROPERTIES:\n:A: 1\n:END:\n"
  6438. (org-mode-restart)
  6439. (let ((org-use-property-inheritance t))
  6440. (org-refresh-properties "A" 'org-test))
  6441. (get-text-property (point) 'org-test))))
  6442. (should-not
  6443. (equal "1"
  6444. (org-test-with-temp-text
  6445. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  6446. (org-mode-restart)
  6447. (let ((org-use-property-inheritance nil))
  6448. (org-refresh-properties "A" 'org-test))
  6449. (get-text-property (point) 'org-test))))
  6450. (should
  6451. (equal "1"
  6452. (org-test-with-temp-text
  6453. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  6454. (org-mode-restart)
  6455. (let ((org-use-property-inheritance t))
  6456. (org-refresh-properties "A" 'org-test))
  6457. (get-text-property (point) 'org-test))))
  6458. (should
  6459. (equal "2"
  6460. (org-test-with-temp-text
  6461. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1\n:PROPERTIES:\n:A: 2\n:END:"
  6462. (org-mode-restart)
  6463. (let ((org-use-property-inheritance t))
  6464. (org-refresh-properties "A" 'org-test))
  6465. (get-text-property (point) 'org-test))))
  6466. ;; When property is inherited, use global value across the whole
  6467. ;; buffer. However local values have precedence, as well as the
  6468. ;; document level property-drawer.
  6469. (should-not
  6470. (equal "1"
  6471. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  6472. (org-mode-restart)
  6473. (let ((org-use-property-inheritance nil))
  6474. (org-refresh-properties "A" 'org-test))
  6475. (get-text-property (point) 'org-test))))
  6476. (should
  6477. (equal "1"
  6478. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  6479. (org-mode-restart)
  6480. (let ((org-use-property-inheritance t))
  6481. (org-refresh-properties "A" 'org-test))
  6482. (get-text-property (point) 'org-test))))
  6483. (should
  6484. (equal "2"
  6485. (org-test-with-temp-text
  6486. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  6487. (org-mode-restart)
  6488. (let ((org-use-property-inheritance t))
  6489. (org-refresh-properties "A" 'org-test))
  6490. (get-text-property (point) 'org-test))))
  6491. ;; When both keyword-property and document-level property-block is
  6492. ;; defined, the property-block has precedance.
  6493. (should
  6494. (equal "1"
  6495. (org-test-with-temp-text
  6496. ":PROPERTIES:\n:A: 1\n:END:\n#+PROPERTY: A 2\n<point>* H1"
  6497. (org-mode-restart)
  6498. (let ((org-use-property-inheritance t))
  6499. (org-refresh-properties "A" 'org-test))
  6500. (get-text-property (point) 'org-test)))))
  6501. (ert-deftest test-org/refresh-category-properties ()
  6502. "Test `org-refresh-category-properties' specifications"
  6503. (should
  6504. (equal "cat1"
  6505. (org-test-with-temp-text
  6506. ":PROPERTIES:\n:CATEGORY: cat1\n:END:"
  6507. (org-refresh-category-properties)
  6508. (org-get-category))))
  6509. (should
  6510. (equal "cat1"
  6511. (org-test-with-temp-text
  6512. "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  6513. (org-refresh-category-properties)
  6514. (org-get-category))))
  6515. ;; Even though property-inheritance is deactivated, category
  6516. ;; property should be inherited. As described in
  6517. ;; `org-use-property-inheritance'.
  6518. (should
  6519. (equal "cat1"
  6520. (org-test-with-temp-text
  6521. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  6522. (org-mode-restart)
  6523. (let ((org-use-property-inheritance nil))
  6524. (org-refresh-category-properties))
  6525. (org-get-category))))
  6526. (should
  6527. (equal "cat1"
  6528. (org-test-with-temp-text
  6529. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  6530. (org-mode-restart)
  6531. (let ((org-use-property-inheritance t))
  6532. (org-refresh-category-properties))
  6533. (org-get-category))))
  6534. (should
  6535. (equal "cat2"
  6536. (org-test-with-temp-text
  6537. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H\n:PROPERTIES:\n:CATEGORY: cat2\n:END:\n"
  6538. (org-mode-restart)
  6539. (let ((org-use-property-inheritance t))
  6540. (org-refresh-category-properties))
  6541. (org-get-category)))))
  6542. ;;; Refile
  6543. (ert-deftest test-org/refile-get-targets ()
  6544. "Test `org-refile-get-targets' specifications."
  6545. ;; :maxlevel includes all headings above specified value.
  6546. (should
  6547. (equal '("H1" "H2" "H3")
  6548. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6549. (let ((org-refile-use-outline-path nil)
  6550. (org-refile-targets `((nil :maxlevel . 3))))
  6551. (mapcar #'car (org-refile-get-targets))))))
  6552. (should
  6553. (equal '("H1" "H2")
  6554. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6555. (let ((org-refile-use-outline-path nil)
  6556. (org-refile-targets `((nil :maxlevel . 2))))
  6557. (mapcar #'car (org-refile-get-targets))))))
  6558. ;; :level limits targets to headlines with the specified level.
  6559. (should
  6560. (equal '("H2")
  6561. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6562. (let ((org-refile-use-outline-path nil)
  6563. (org-refile-targets `((nil :level . 2))))
  6564. (mapcar #'car (org-refile-get-targets))))))
  6565. ;; :tag limits targets to headlines with specified tag.
  6566. (should
  6567. (equal '("H1")
  6568. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  6569. (let ((org-refile-use-outline-path nil)
  6570. (org-refile-targets `((nil :tag . "foo"))))
  6571. (mapcar #'car (org-refile-get-targets))))))
  6572. ;; :todo limits targets to headlines with specified TODO keyword.
  6573. (should
  6574. (equal '("H2")
  6575. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  6576. (let ((org-refile-use-outline-path nil)
  6577. (org-refile-targets `((nil :todo . "TODO"))))
  6578. (mapcar #'car (org-refile-get-targets))))))
  6579. ;; :regexp filters targets matching provided regexp.
  6580. (should
  6581. (equal '("F2" "F3")
  6582. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  6583. (let ((org-refile-use-outline-path nil)
  6584. (org-refile-targets `((nil :regexp . "F"))))
  6585. (mapcar #'car (org-refile-get-targets))))))
  6586. ;; A nil `org-refile-targets' includes only top level headlines in
  6587. ;; current buffer.
  6588. (should
  6589. (equal '("H1" "H2")
  6590. (org-test-with-temp-text "* H1\n** S1\n* H2"
  6591. (let ((org-refile-use-outline-path nil)
  6592. (org-refile-targets nil))
  6593. (mapcar #'car (org-refile-get-targets))))))
  6594. ;; Return value is the union of the targets according to all the
  6595. ;; defined rules. However, prevent duplicates.
  6596. (should
  6597. (equal '("F2" "F3" "H1")
  6598. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  6599. (let ((org-refile-use-outline-path nil)
  6600. (org-refile-targets `((nil :regexp . "F")
  6601. (nil :todo . "TODO"))))
  6602. (mapcar #'car (org-refile-get-targets))))))
  6603. (should
  6604. (equal '("F2" "F3" "H1")
  6605. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  6606. (let ((org-refile-use-outline-path nil)
  6607. (org-refile-targets `((nil :regexp . "F")
  6608. (nil :todo . "TODO"))))
  6609. (mapcar #'car (org-refile-get-targets))))))
  6610. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  6611. ;; paths.
  6612. (should
  6613. (equal '("H1" "H1/H2" "H1/H2/H3")
  6614. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6615. (let ((org-refile-use-outline-path t)
  6616. (org-refile-targets `((nil :maxlevel . 3))))
  6617. (mapcar #'car (org-refile-get-targets))))))
  6618. ;; When providing targets as paths, escape forward slashes in
  6619. ;; headings with backslashes.
  6620. (should
  6621. (equal '("H1\\/foo")
  6622. (org-test-with-temp-text "* H1/foo"
  6623. (let ((org-refile-use-outline-path t)
  6624. (org-refile-targets `((nil :maxlevel . 1))))
  6625. (mapcar #'car (org-refile-get-targets))))))
  6626. ;; When `org-refile-use-outline-path' is `file', include file name
  6627. ;; without directory in targets.
  6628. (should
  6629. (org-test-with-temp-text-in-file "* H1"
  6630. (let* ((filename (buffer-file-name))
  6631. (org-refile-use-outline-path 'file)
  6632. (org-refile-targets `(((,filename) :level . 1))))
  6633. (member (file-name-nondirectory filename)
  6634. (mapcar #'car (org-refile-get-targets))))))
  6635. ;; When `org-refile-use-outline-path' is `full-file-path', include
  6636. ;; full file name.
  6637. (should
  6638. (org-test-with-temp-text-in-file "* H1"
  6639. (let* ((filename (file-truename (buffer-file-name)))
  6640. (org-refile-use-outline-path 'full-file-path)
  6641. (org-refile-targets `(((,filename) :level . 1))))
  6642. (member filename (mapcar #'car (org-refile-get-targets))))))
  6643. ;; When `org-refile-use-outline-path' is `buffer-name', include
  6644. ;; buffer name.
  6645. (should
  6646. (org-test-with-temp-text "* H1"
  6647. (let* ((org-refile-use-outline-path 'buffer-name)
  6648. (org-refile-targets `((nil :level . 1))))
  6649. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  6650. ;;; Sparse trees
  6651. (ert-deftest test-org/match-sparse-tree ()
  6652. "Test `org-match-sparse-tree' specifications."
  6653. ;; Match tags.
  6654. (should-not
  6655. (org-test-with-temp-text "* H\n** H1 :tag:"
  6656. (org-match-sparse-tree nil "tag")
  6657. (search-forward "H1")
  6658. (org-invisible-p2)))
  6659. (should
  6660. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  6661. (org-match-sparse-tree nil "tag")
  6662. (search-forward "H2")
  6663. (org-invisible-p2)))
  6664. ;; "-" operator for tags.
  6665. (should-not
  6666. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6667. (org-match-sparse-tree nil "tag1-tag2")
  6668. (search-forward "H1")
  6669. (org-invisible-p2)))
  6670. (should
  6671. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6672. (org-match-sparse-tree nil "tag1-tag2")
  6673. (search-forward "H2")
  6674. (org-invisible-p2)))
  6675. ;; "&" operator for tags.
  6676. (should
  6677. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6678. (org-match-sparse-tree nil "tag1&tag2")
  6679. (search-forward "H1")
  6680. (org-invisible-p2)))
  6681. (should-not
  6682. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6683. (org-match-sparse-tree nil "tag1&tag2")
  6684. (search-forward "H2")
  6685. (org-invisible-p2)))
  6686. ;; "|" operator for tags.
  6687. (should-not
  6688. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6689. (org-match-sparse-tree nil "tag1|tag2")
  6690. (search-forward "H1")
  6691. (org-invisible-p2)))
  6692. (should-not
  6693. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6694. (org-match-sparse-tree nil "tag1|tag2")
  6695. (search-forward "H2")
  6696. (org-invisible-p2)))
  6697. ;; Regexp match on tags.
  6698. (should-not
  6699. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  6700. (org-match-sparse-tree nil "{^tag.*}")
  6701. (search-forward "H1")
  6702. (org-invisible-p2)))
  6703. (should
  6704. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  6705. (org-match-sparse-tree nil "{^tag.*}")
  6706. (search-forward "H2")
  6707. (org-invisible-p2)))
  6708. ;; Match group tags.
  6709. (should-not
  6710. (org-test-with-temp-text
  6711. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  6712. (org-match-sparse-tree nil "work")
  6713. (search-forward "H1")
  6714. (org-invisible-p2)))
  6715. (should-not
  6716. (org-test-with-temp-text
  6717. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  6718. (org-match-sparse-tree nil "work")
  6719. (search-forward "H2")
  6720. (org-invisible-p2)))
  6721. ;; Match group tags with hard brackets.
  6722. (should-not
  6723. (org-test-with-temp-text
  6724. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  6725. (org-match-sparse-tree nil "work")
  6726. (search-forward "H1")
  6727. (org-invisible-p2)))
  6728. (should-not
  6729. (org-test-with-temp-text
  6730. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  6731. (org-match-sparse-tree nil "work")
  6732. (search-forward "H2")
  6733. (org-invisible-p2)))
  6734. ;; Match tags in hierarchies
  6735. (should-not
  6736. (org-test-with-temp-text
  6737. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  6738. #+TAGS: [ Lev_2 : Lev_3 ]\n
  6739. #+TAGS: { Lev_3 : Lev_4 }\n
  6740. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  6741. (org-match-sparse-tree nil "Lev_1")
  6742. (search-forward "H4")
  6743. (org-invisible-p2)))
  6744. (should-not
  6745. (org-test-with-temp-text
  6746. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  6747. #+TAGS: [ Lev_2 : Lev_3 ]\n
  6748. #+TAGS: { Lev_3 : Lev_4 }\n
  6749. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  6750. (org-match-sparse-tree nil "Lev_1+Lev_3")
  6751. (search-forward "H4")
  6752. (org-invisible-p2)))
  6753. ;; Match regular expressions in tags
  6754. (should-not
  6755. (org-test-with-temp-text
  6756. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  6757. (org-match-sparse-tree nil "Lev")
  6758. (search-forward "H1")
  6759. (org-invisible-p2)))
  6760. (should
  6761. (org-test-with-temp-text
  6762. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  6763. (org-match-sparse-tree nil "Lev")
  6764. (search-forward "H1")
  6765. (org-invisible-p2)))
  6766. ;; Match properties.
  6767. (should
  6768. (org-test-with-temp-text
  6769. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  6770. (org-match-sparse-tree nil "A=\"1\"")
  6771. (search-forward "H2")
  6772. (org-invisible-p2)))
  6773. (should-not
  6774. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  6775. (org-match-sparse-tree nil "A=\"1\"")
  6776. (search-forward "H2")
  6777. (org-invisible-p2)))
  6778. ;; Case is not significant when matching properties.
  6779. (should-not
  6780. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  6781. (org-match-sparse-tree nil "a=\"1\"")
  6782. (search-forward "H2")
  6783. (org-invisible-p2)))
  6784. (should-not
  6785. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  6786. (org-match-sparse-tree nil "A=\"1\"")
  6787. (search-forward "H2")
  6788. (org-invisible-p2)))
  6789. ;; Match special LEVEL property.
  6790. (should-not
  6791. (org-test-with-temp-text "* H\n** H1\n*** H2"
  6792. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  6793. (search-forward "H1")
  6794. (org-invisible-p2)))
  6795. (should
  6796. (org-test-with-temp-text "* H\n** H1\n*** H2"
  6797. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  6798. (search-forward "H2")
  6799. (org-invisible-p2)))
  6800. ;; Comparison operators when matching properties.
  6801. (should
  6802. (org-test-with-temp-text
  6803. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  6804. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  6805. (search-forward "H1")
  6806. (org-invisible-p2)))
  6807. (should-not
  6808. (org-test-with-temp-text
  6809. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  6810. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  6811. (search-forward "H2")
  6812. (org-invisible-p2)))
  6813. ;; Regexp match on properties values.
  6814. (should-not
  6815. (org-test-with-temp-text
  6816. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  6817. (org-match-sparse-tree nil "A={f.*}")
  6818. (search-forward "H1")
  6819. (org-invisible-p2)))
  6820. (should
  6821. (org-test-with-temp-text
  6822. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  6823. (org-match-sparse-tree nil "A={f.*}")
  6824. (search-forward "H2")
  6825. (org-invisible-p2)))
  6826. ;; With an optional argument, limit match to TODO entries.
  6827. (should-not
  6828. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  6829. (org-match-sparse-tree t "tag")
  6830. (search-forward "H1")
  6831. (org-invisible-p2)))
  6832. (should
  6833. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  6834. (org-match-sparse-tree t "tag")
  6835. (search-forward "H2")
  6836. (org-invisible-p2))))
  6837. (ert-deftest test-org/occur ()
  6838. "Test `org-occur' specifications."
  6839. ;; Count number of matches.
  6840. (should
  6841. (= 1
  6842. (org-test-with-temp-text "* H\nA\n* H2"
  6843. (org-occur "A"))))
  6844. (should
  6845. (= 2
  6846. (org-test-with-temp-text "* H\nA\n* H2\nA"
  6847. (org-occur "A"))))
  6848. ;; Test CALLBACK optional argument.
  6849. (should
  6850. (= 0
  6851. (org-test-with-temp-text "* H\nA\n* H2"
  6852. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  6853. (should
  6854. (= 1
  6855. (org-test-with-temp-text "* H\nA\n* H2\nA"
  6856. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  6857. ;; Case-fold searches according to `org-occur-case-fold-search'.
  6858. (should
  6859. (= 2
  6860. (org-test-with-temp-text "Aa"
  6861. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  6862. (should
  6863. (= 2
  6864. (org-test-with-temp-text "Aa"
  6865. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  6866. (should
  6867. (= 1
  6868. (org-test-with-temp-text "Aa"
  6869. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  6870. (should
  6871. (= 1
  6872. (org-test-with-temp-text "Aa"
  6873. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  6874. (should
  6875. (= 1
  6876. (org-test-with-temp-text "Aa"
  6877. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  6878. (should
  6879. (= 2
  6880. (org-test-with-temp-text "Aa"
  6881. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  6882. ;;; Tags
  6883. (ert-deftest test-org/tag-string-to-alist ()
  6884. "Test `org-tag-string-to-alist' specifications."
  6885. ;; Tag without selection key.
  6886. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  6887. ;; Tag with selection key.
  6888. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  6889. ;; Tag group.
  6890. (should
  6891. (equal
  6892. (org-tag-string-to-alist "[ group : t1 t2 ]")
  6893. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  6894. ;; Mutually exclusive tags.
  6895. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  6896. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  6897. (should
  6898. (equal
  6899. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  6900. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  6901. (ert-deftest test-org/tag-alist-to-string ()
  6902. "Test `org-tag-alist-to-string' specifications."
  6903. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  6904. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  6905. (should
  6906. (equal
  6907. (org-tag-alist-to-string
  6908. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6909. "[ group : t1 t2 ]"))
  6910. (should
  6911. (equal (org-tag-alist-to-string
  6912. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  6913. "{ tag1 tag2 }"))
  6914. (should
  6915. (equal
  6916. (org-tag-alist-to-string
  6917. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  6918. "{ group : tag1 tag2 }")))
  6919. (ert-deftest test-org/tag-alist-to-groups ()
  6920. "Test `org-tag-alist-to-groups' specifications."
  6921. (should
  6922. (equal (org-tag-alist-to-groups
  6923. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  6924. '(("group" "t1" "t2"))))
  6925. (should
  6926. (equal
  6927. (org-tag-alist-to-groups
  6928. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6929. '(("group" "t1" "t2"))))
  6930. (should-not
  6931. (org-tag-alist-to-groups
  6932. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  6933. (ert-deftest test-org/tag-align ()
  6934. "Test tags alignment."
  6935. ;; Test aligning tags with different display width.
  6936. (should
  6937. ;; 12345678901234567890
  6938. (equal "* Test :abc:"
  6939. (org-test-with-temp-text "* Test :abc:"
  6940. (let ((org-tags-column -20)
  6941. (indent-tabs-mode nil))
  6942. (org-fix-tags-on-the-fly))
  6943. (buffer-string))))
  6944. (should
  6945. ;; 12345678901234567890
  6946. (equal "* Test :日本語:"
  6947. (org-test-with-temp-text "* Test :日本語:"
  6948. (let ((org-tags-column -20)
  6949. (indent-tabs-mode nil))
  6950. (org-fix-tags-on-the-fly))
  6951. (buffer-string))))
  6952. ;; Make sure aligning tags do not skip invisible text.
  6953. (should
  6954. (equal "* [[linkx]] :tag:"
  6955. (org-test-with-temp-text "* [[link<point>]] :tag:"
  6956. (let ((org-tags-column 0))
  6957. (org-fix-tags-on-the-fly)
  6958. (insert "x")
  6959. (buffer-string)))))
  6960. ;; Aligning tags preserve position.
  6961. (should
  6962. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  6963. (let ((org-tags-column 78)
  6964. (indent-tabs-mode nil))
  6965. (org-fix-tags-on-the-fly))
  6966. (current-column))))
  6967. ;; Aligning all tags in visible buffer.
  6968. (should
  6969. ;; 12345678901234567890
  6970. (equal (concat "* Level 1 :abc:\n"
  6971. "** Level 2 :def:")
  6972. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  6973. "** Level 2 :def:")
  6974. (let ((org-tags-column -20)
  6975. (indent-tabs-mode nil))
  6976. ;; (org-align-tags :all) must work even when the point
  6977. ;; is at the end of the buffer.
  6978. (goto-char (point-max))
  6979. (org-align-tags :all))
  6980. (buffer-string)))))
  6981. (ert-deftest test-org/get-tags ()
  6982. "Test `org-get-tags' specifications."
  6983. ;; Standard test.
  6984. (should
  6985. (equal '("foo")
  6986. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  6987. (should
  6988. (equal '("foo" "bar")
  6989. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  6990. ;; Tags for inlinetasks.
  6991. (should
  6992. (equal '("foo" "bar")
  6993. (progn
  6994. (require 'org-inlinetask)
  6995. (org-test-with-temp-text (concat (make-string org-inlinetask-min-level ?*) " Test :foo:bar:")
  6996. (org-get-tags (org-element-at-point))))))
  6997. (should
  6998. (equal '("foo" "bar")
  6999. (progn
  7000. (require 'org-inlinetask)
  7001. (org-test-with-temp-text (concat (make-string org-inlinetask-min-level ?*) " Test :foo:bar:")
  7002. (org-get-tags nil)))))
  7003. ;; Return nil when there is no tag.
  7004. (should-not
  7005. (org-test-with-temp-text "* Test" (org-get-tags)))
  7006. ;; Tags are inherited from parent headlines.
  7007. (should
  7008. (equal '("tag")
  7009. (let ((org-use-tag-inheritance t))
  7010. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  7011. (org-get-tags)))))
  7012. ;; Tags are inherited from `org-file-tags'.
  7013. (should
  7014. (equal '("tag")
  7015. (org-test-with-temp-text "* H1"
  7016. (let ((org-file-tags '("tag"))
  7017. (org-use-tag-inheritance t))
  7018. (org-get-tags)))))
  7019. ;; Only inherited tags have the `inherited' text property.
  7020. (should
  7021. (get-text-property 0 'inherited
  7022. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7023. (let ((org-use-tag-inheritance t))
  7024. (assoc-string "foo" (org-get-tags))))))
  7025. (should-not
  7026. (get-text-property 0 'inherited
  7027. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7028. (let ((org-use-tag-inheritance t))
  7029. (assoc-string "bar" (org-get-tags))))))
  7030. ;; Obey to `org-use-tag-inheritance'.
  7031. (should-not
  7032. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7033. (let ((org-use-tag-inheritance nil))
  7034. (assoc-string "foo" (org-get-tags)))))
  7035. (should-not
  7036. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7037. (let ((org-use-tag-inheritance nil)
  7038. (org-file-tags '("foo")))
  7039. (assoc-string "foo" (org-get-tags)))))
  7040. (should-not
  7041. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7042. (let ((org-use-tag-inheritance '("bar")))
  7043. (assoc-string "foo" (org-get-tags)))))
  7044. (should
  7045. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7046. (let ((org-use-tag-inheritance '("bar")))
  7047. (assoc-string "bar" (org-get-tags)))))
  7048. (should-not
  7049. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7050. (let ((org-use-tag-inheritance "b.*"))
  7051. (assoc-string "foo" (org-get-tags)))))
  7052. (should
  7053. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7054. (let ((org-use-tag-inheritance "b.*"))
  7055. (assoc-string "bar" (org-get-tags)))))
  7056. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  7057. (should
  7058. (equal '("baz")
  7059. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7060. (let ((org-use-tag-inheritance t))
  7061. (org-get-tags nil t)))))
  7062. ;; When optional argument POS is non-nil, get tags there instead.
  7063. (should
  7064. (equal '("foo")
  7065. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  7066. (org-get-tags 1))))
  7067. ;; Make sure tags excluded from inheritance are returned if local
  7068. (should
  7069. (equal '("foo")
  7070. (org-test-with-temp-text "* Test :foo:"
  7071. (let ((org-use-tag-inheritance t)
  7072. (org-tags-exclude-from-inheritance '("foo")))
  7073. (org-get-tags)))))
  7074. ;; Test the collection of tags from #+filetags and parent tags.
  7075. (should
  7076. (equal '("a" "b" "c" "d")
  7077. (org-test-with-temp-text (concat "#+filetags: a\n"
  7078. "* Level 1 :b:\n"
  7079. "** Level 2 :c:\n"
  7080. "*** Level 3 :d:\n"
  7081. "<point>")
  7082. (let ((org-use-tag-inheritance t))
  7083. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  7084. (org-get-tags)))))
  7085. ;; Pathological case: tagged headline with an empty body.
  7086. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  7087. (ert-deftest test-org/set-tags ()
  7088. "Test `org-set-tags' specifications."
  7089. ;; Throw an error on invalid data.
  7090. (should-error
  7091. (org-test-with-temp-text "* H"
  7092. (org-set-tags 'foo)))
  7093. ;; `nil', an empty, and a blank string remove all tags.
  7094. (should
  7095. (equal "* H"
  7096. (org-test-with-temp-text "* H :tag1:tag2:"
  7097. (org-set-tags nil)
  7098. (buffer-string))))
  7099. (should
  7100. (equal "* H"
  7101. (org-test-with-temp-text "* H :tag1:tag2:"
  7102. (org-set-tags "")
  7103. (buffer-string))))
  7104. (should
  7105. (equal "* H"
  7106. (org-test-with-temp-text "* H :tag1:tag2:"
  7107. (org-set-tags " ")
  7108. (buffer-string))))
  7109. ;; If there's nothing to remove, just bail out.
  7110. (should
  7111. (equal "* H"
  7112. (org-test-with-temp-text "* H"
  7113. (org-set-tags nil)
  7114. (buffer-string))))
  7115. (should
  7116. (equal "* "
  7117. (org-test-with-temp-text "* "
  7118. (org-set-tags nil)
  7119. (buffer-string))))
  7120. ;; If DATA is a tag string, set current tags to it, even if it means
  7121. ;; replacing old tags.
  7122. (should
  7123. (equal "* H :tag0:"
  7124. (org-test-with-temp-text "* H :tag1:tag2:"
  7125. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  7126. (buffer-string))))
  7127. (should
  7128. (equal "* H :tag0:"
  7129. (org-test-with-temp-text "* H"
  7130. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  7131. (buffer-string))))
  7132. ;; If DATA is a list, set tags to this list, even if it means
  7133. ;; replacing old tags.
  7134. (should
  7135. (equal "* H :tag0:"
  7136. (org-test-with-temp-text "* H :tag1:tag2:"
  7137. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  7138. (buffer-string))))
  7139. (should
  7140. (equal "* H :tag0:"
  7141. (org-test-with-temp-text "* H"
  7142. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  7143. (buffer-string))))
  7144. ;; When set, apply `org-tags-sort-function'.
  7145. (should
  7146. (equal "* H :a:b:"
  7147. (org-test-with-temp-text "* H"
  7148. (let ((org-tags-column 1)
  7149. (org-tags-sort-function #'string<))
  7150. (org-set-tags '("b" "a"))
  7151. (buffer-string)))))
  7152. ;; When new tags are identical to the previous ones, still align.
  7153. (should
  7154. (equal "* H :foo:"
  7155. (org-test-with-temp-text "* H :foo:"
  7156. (let ((org-tags-column 1))
  7157. (org-set-tags '("foo"))
  7158. (buffer-string)))))
  7159. ;; When tags have been changed, run `org-after-tags-change-hook'.
  7160. (should
  7161. (catch :return
  7162. (org-test-with-temp-text "* H :foo:"
  7163. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  7164. (org-set-tags '("bar"))
  7165. nil))))
  7166. (should-not
  7167. (catch :return
  7168. (org-test-with-temp-text "* H :foo:"
  7169. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  7170. (org-set-tags '("foo"))
  7171. nil))))
  7172. ;; Special case: handle empty headlines.
  7173. (should
  7174. (equal "* :tag0:"
  7175. (org-test-with-temp-text "* "
  7176. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  7177. (buffer-string))))
  7178. ;; Modify buffer only when a tag change happens or alignment is
  7179. ;; done.
  7180. (should-not
  7181. (org-test-with-temp-text "* H :foo:"
  7182. (set-buffer-modified-p nil)
  7183. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  7184. (buffer-modified-p)))
  7185. (should
  7186. (org-test-with-temp-text "* H :foo:"
  7187. (set-buffer-modified-p nil)
  7188. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  7189. (buffer-modified-p)))
  7190. (should
  7191. (org-test-with-temp-text "* H :foo:"
  7192. (set-buffer-modified-p nil)
  7193. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  7194. (buffer-modified-p)))
  7195. ;; Pathological case: when setting tags of a folded headline, do not
  7196. ;; let new tags being sucked into invisibility.
  7197. (should-not
  7198. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  7199. ;; Show only headlines
  7200. (org-content)
  7201. ;; Set NEXT tag on current entry
  7202. (org-set-tags ":NEXT:")
  7203. ;; Move point to that NEXT tag
  7204. (search-forward "NEXT") (backward-word)
  7205. ;; And it should be visible (i.e. no overlays)
  7206. (overlays-at (point)))))
  7207. (ert-deftest test-org/set-tags-command ()
  7208. "Test `org-set-tags-command' specifications"
  7209. ;; Set tags at current headline.
  7210. (should
  7211. (equal "* H1 :foo:"
  7212. (org-test-with-temp-text "* H1"
  7213. (cl-letf (((symbol-function 'completing-read-multiple)
  7214. (lambda (&rest _args) '("foo"))))
  7215. (let ((org-use-fast-tag-selection nil)
  7216. (org-tags-column 1))
  7217. (org-set-tags-command)))
  7218. (buffer-string))))
  7219. ;; Preserve position when called from the section below.
  7220. (should
  7221. (equal "* H1 :foo:\nContents"
  7222. (org-test-with-temp-text "* H1\n<point>Contents"
  7223. (cl-letf (((symbol-function 'completing-read-multiple)
  7224. (lambda (&rest _args) '("foo"))))
  7225. (let ((org-use-fast-tag-selection nil)
  7226. (org-tags-column 1))
  7227. (org-set-tags-command)))
  7228. (buffer-string))))
  7229. (should-not
  7230. (equal "* H1 :foo:\nContents2"
  7231. (org-test-with-temp-text "* H1\n<point>Contents2"
  7232. (cl-letf (((symbol-function 'completing-read-multiple)
  7233. (lambda (&rest _args) '("foo"))))
  7234. (let ((org-use-fast-tag-selection nil)
  7235. (org-tags-column 1))
  7236. (org-set-tags-command)))
  7237. (org-at-heading-p))))
  7238. ;; When a region is active and
  7239. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  7240. ;; same value in all headlines in region.
  7241. (should
  7242. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  7243. (org-test-with-temp-text "* H1\nContents\n* H2"
  7244. (cl-letf (((symbol-function 'completing-read-multiple)
  7245. (lambda (&rest _args) '("foo"))))
  7246. (let ((org-use-fast-tag-selection nil)
  7247. (org-loop-over-headlines-in-active-region t)
  7248. (org-tags-column 1))
  7249. (transient-mark-mode 1)
  7250. (push-mark (point) t t)
  7251. (goto-char (point-max))
  7252. (org-set-tags-command)))
  7253. (buffer-string))))
  7254. (should
  7255. (equal "* H1\nContents\n* H2 :foo:"
  7256. (org-test-with-temp-text "* H1\nContents\n* H2"
  7257. (cl-letf (((symbol-function 'completing-read-multiple)
  7258. (lambda (&rest _args) '("foo"))))
  7259. (let ((org-use-fast-tag-selection nil)
  7260. (org-loop-over-headlines-in-active-region nil)
  7261. (org-tags-column 1))
  7262. (transient-mark-mode 1)
  7263. (push-mark (point) t t)
  7264. (goto-char (point-max))
  7265. (org-set-tags-command)))
  7266. (buffer-string))))
  7267. ;; With a C-u prefix argument, align all tags in the buffer.
  7268. (should
  7269. (equal "* H1 :foo:\n* H2 :bar:"
  7270. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  7271. (let ((org-tags-column 1)) (org-set-tags-command '(4)))
  7272. (buffer-string))))
  7273. ;; Point does not move with empty headline.
  7274. (should
  7275. (equal ":foo:"
  7276. (org-test-with-temp-text "* <point>"
  7277. (cl-letf (((symbol-function 'completing-read-multiple)
  7278. (lambda (&rest _args) '("foo"))))
  7279. (let ((org-use-fast-tag-selection nil)
  7280. (org-tags-column 1))
  7281. (org-set-tags-command)))
  7282. (buffer-substring (point) (line-end-position)))))
  7283. ;; Point does not move at start of line.
  7284. (should
  7285. (equal "* H1 :foo:"
  7286. (org-test-with-temp-text "* H1"
  7287. (cl-letf (((symbol-function 'completing-read-multiple)
  7288. (lambda (&rest _args) '("foo"))))
  7289. (let ((org-use-fast-tag-selection nil)
  7290. (org-tags-column 1))
  7291. (org-set-tags-command)))
  7292. (buffer-substring (point) (line-end-position)))))
  7293. ;; Point does not move when within *'s.
  7294. (should
  7295. (equal "* H1 :foo:"
  7296. (org-test-with-temp-text "*<point>* H1"
  7297. (cl-letf (((symbol-function 'completing-read-multiple)
  7298. (lambda (&rest _args) '("foo"))))
  7299. (let ((org-use-fast-tag-selection nil)
  7300. (org-tags-column 1))
  7301. (org-set-tags-command)))
  7302. (buffer-substring (point) (line-end-position)))))
  7303. ;; Point workaround does not get fooled when looking at a space.
  7304. (should
  7305. (equal " b :foo:"
  7306. (org-test-with-temp-text "* a<point> b"
  7307. (cl-letf (((symbol-function 'completing-read-multiple)
  7308. (lambda (&rest _args) '("foo"))))
  7309. (let ((org-use-fast-tag-selection nil)
  7310. (org-tags-column 1))
  7311. (org-set-tags-command)))
  7312. (buffer-substring (point) (line-end-position)))))
  7313. ;; Handle tags both set locally and inherited.
  7314. (should
  7315. (equal "b :foo:"
  7316. (org-test-with-temp-text "* a :foo:\n** <point>b :foo:"
  7317. (cl-letf (((symbol-function 'completing-read-multiple)
  7318. (lambda (_prompt _coll &optional _pred _req initial &rest _)
  7319. (list initial))))
  7320. (let ((org-use-fast-tag-selection nil)
  7321. (org-tags-column 1))
  7322. (org-set-tags-command)))
  7323. (buffer-substring (point) (line-end-position))))))
  7324. (ert-deftest test-org/toggle-tag ()
  7325. "Test `org-toggle-tag' specifications."
  7326. ;; Insert missing tag.
  7327. (should
  7328. (equal "* H :tag:"
  7329. (org-test-with-temp-text "* H"
  7330. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  7331. (buffer-string))))
  7332. (should
  7333. (equal "* H :tag1:tag2:"
  7334. (org-test-with-temp-text "* H :tag1:"
  7335. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  7336. (buffer-string))))
  7337. ;; Remove existing tag.
  7338. (should
  7339. (equal "* H"
  7340. (org-test-with-temp-text "* H :tag:"
  7341. (org-toggle-tag "tag")
  7342. (buffer-string))))
  7343. (should
  7344. (equal "* H :tag1:"
  7345. (org-test-with-temp-text "* H :tag1:tag2:"
  7346. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  7347. (buffer-string))))
  7348. (should
  7349. (equal "* H :tag2:"
  7350. (org-test-with-temp-text "* H :tag1:tag2:"
  7351. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  7352. (buffer-string))))
  7353. ;; With optional argument ONOFF set to `on', try to insert the tag,
  7354. ;; even if its already there.
  7355. (should
  7356. (equal "* H :tag:"
  7357. (org-test-with-temp-text "* H"
  7358. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  7359. (buffer-string))))
  7360. (should
  7361. (equal "* H :tag:"
  7362. (org-test-with-temp-text "* H :tag:"
  7363. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  7364. (buffer-string))))
  7365. ;; With optional argument ONOFF set to `off', try to remove the tag,
  7366. ;; even if its not there.
  7367. (should
  7368. (equal "* H"
  7369. (org-test-with-temp-text "* H :tag:"
  7370. (org-toggle-tag "tag" 'off)
  7371. (buffer-string))))
  7372. (should
  7373. (equal "* H :tag:"
  7374. (org-test-with-temp-text "* H :tag:"
  7375. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  7376. (buffer-string))))
  7377. ;; Special case: Handle properly tag inheritance. In particular, do
  7378. ;; not set inherited tags.
  7379. (should
  7380. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  7381. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  7382. (let ((org-use-tag-inheritance t)
  7383. (org-tags-column 1))
  7384. (org-toggle-tag "tag"))
  7385. (buffer-string))))
  7386. (should
  7387. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  7388. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  7389. (let ((org-use-tag-inheritance t)
  7390. (org-tags-column 1))
  7391. (org-toggle-tag "foo"))
  7392. (buffer-string)))))
  7393. (ert-deftest test-org/tags-expand ()
  7394. "Test `org-tags-expand' specifications."
  7395. ;; Expand tag groups as a regexp enclosed within curly brackets.
  7396. (should
  7397. (equal "{\\<[ABC]\\>}"
  7398. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7399. (org-mode-restart)
  7400. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  7401. (should
  7402. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  7403. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  7404. (org-mode-restart)
  7405. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  7406. ;; Preserve operator before the regexp.
  7407. (should
  7408. (equal "+{\\<[ABC]\\>}"
  7409. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7410. (org-mode-restart)
  7411. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  7412. (should
  7413. (equal "-{\\<[ABC]\\>}"
  7414. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7415. (org-mode-restart)
  7416. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  7417. ;; Handle "|" syntax.
  7418. (should
  7419. (equal "{\\<[ABC]\\>}|D"
  7420. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7421. (org-mode-restart)
  7422. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  7423. ;; Handle nested groups.
  7424. (should
  7425. (equal "{\\<[A-D]\\>}"
  7426. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  7427. (org-mode-restart)
  7428. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  7429. ;; Expand multiple occurrences of the same group.
  7430. (should
  7431. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  7432. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7433. (org-mode-restart)
  7434. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  7435. ;; Preserve regexp matches.
  7436. (should
  7437. (equal "{A+}"
  7438. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7439. (org-mode-restart)
  7440. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  7441. ;;; TODO keywords
  7442. (ert-deftest test-org/auto-repeat-maybe ()
  7443. "Test `org-auto-repeat-maybe' specifications."
  7444. ;; Do not auto repeat when there is no valid time stamp with
  7445. ;; a repeater in the entry.
  7446. (should-not
  7447. (string-prefix-p
  7448. "* TODO H"
  7449. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7450. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  7451. (org-todo "DONE")
  7452. (buffer-string)))))
  7453. (should-not
  7454. (string-prefix-p
  7455. "* TODO H"
  7456. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7457. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  7458. (org-todo "DONE")
  7459. (buffer-string)))))
  7460. ;; When switching to DONE state, switch back to first TODO keyword
  7461. ;; in sequence, or the same keyword if they have different types.
  7462. (should
  7463. (string-prefix-p
  7464. "* TODO H"
  7465. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7466. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7467. (org-todo "DONE")
  7468. (buffer-string)))))
  7469. (should
  7470. (string-prefix-p
  7471. "* KWD1 H"
  7472. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  7473. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  7474. (org-todo "DONE")
  7475. (buffer-string)))))
  7476. (should
  7477. (string-prefix-p
  7478. "* KWD2 H"
  7479. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  7480. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  7481. (org-todo "DONE")
  7482. (buffer-string)))))
  7483. ;; If there was no TODO keyword in the first place, do not insert
  7484. ;; any either.
  7485. (should
  7486. (string-prefix-p
  7487. "* H"
  7488. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7489. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  7490. (org-todo "DONE")
  7491. (buffer-string)))))
  7492. ;; Revert to REPEAT_TO_STATE, if set.
  7493. (should
  7494. (string-prefix-p
  7495. "* KWD2 H"
  7496. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  7497. (org-test-with-temp-text
  7498. "* KWD2 H
  7499. :PROPERTIES:
  7500. :REPEAT_TO_STATE: KWD2
  7501. :END:
  7502. <2012-03-29 Thu +2y>"
  7503. (org-todo "DONE")
  7504. (buffer-string)))))
  7505. ;; When switching to DONE state, update base date. If there are
  7506. ;; multiple repeated time stamps, update them all.
  7507. (should
  7508. (string-match-p
  7509. "<2014-03-29 .* \\+2y>"
  7510. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7511. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7512. (org-todo "DONE")
  7513. (buffer-string)))))
  7514. (should
  7515. (string-match-p
  7516. "<2015-03-04 .* \\+1y>"
  7517. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7518. (org-test-with-temp-text
  7519. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  7520. (org-todo "DONE")
  7521. (buffer-string)))))
  7522. ;; Throw an error if repeater unit is the hour and no time is
  7523. ;; provided in the time-stamp.
  7524. (should-error
  7525. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7526. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  7527. (org-todo "DONE")
  7528. (buffer-string))))
  7529. ;; Handle every repeater type using hours step.
  7530. (should
  7531. (string-match-p
  7532. "2014-03-04 .* 02:00"
  7533. (org-test-at-time "<2014-03-04 02:35>"
  7534. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 +8h>"
  7535. (org-todo "DONE")
  7536. (buffer-string)))))
  7537. (should
  7538. (string-match-p
  7539. "2014-03-04 .* 10:00"
  7540. (org-test-at-time "<2014-03-04 02:35>"
  7541. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 ++8h>"
  7542. (org-todo "DONE")
  7543. (buffer-string)))))
  7544. (should
  7545. (string-match-p
  7546. "2014-03-04 .* 10:35"
  7547. (org-test-at-time "<2014-03-04 02:35>"
  7548. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 .+8h>"
  7549. (org-todo "DONE")
  7550. (buffer-string)))))
  7551. ;; Do not repeat inactive time stamps with a repeater.
  7552. (should-not
  7553. (string-match-p
  7554. "\\[2014-03-29 .* \\+2y\\]"
  7555. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7556. (org-test-with-temp-text
  7557. "* TODO H\n[2012-03-29 Thu. +2y]"
  7558. (org-todo "DONE")
  7559. (buffer-string)))))
  7560. ;; Do not repeat commented time stamps.
  7561. (should-not
  7562. (string-prefix-p
  7563. "<2015-03-04 .* \\+1y>"
  7564. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7565. (org-test-with-temp-text
  7566. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  7567. (org-todo "DONE")
  7568. (buffer-string)))))
  7569. (should-not
  7570. (string-prefix-p
  7571. "<2015-03-04 .* \\+1y>"
  7572. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7573. (org-test-with-temp-text
  7574. "* TODO H
  7575. <2012-03-29 Thu. +2y>
  7576. #+BEGIN_EXAMPLE
  7577. <2014-03-04 Tue +1y>
  7578. #+END_EXAMPLE"
  7579. (org-todo "DONE")
  7580. (buffer-string)))))
  7581. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  7582. ;; entry, record time of last repeat.
  7583. (should-not
  7584. (string-match-p
  7585. ":LAST_REPEAT:"
  7586. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  7587. (org-log-repeat nil))
  7588. (cl-letf (((symbol-function 'org-add-log-setup)
  7589. (lambda (&rest _args) nil)))
  7590. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  7591. (org-todo "DONE")
  7592. (buffer-string))))))
  7593. (should
  7594. (string-match-p
  7595. ":LAST_REPEAT:"
  7596. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  7597. (org-log-repeat t))
  7598. (cl-letf (((symbol-function 'org-add-log-setup)
  7599. (lambda (&rest _args) nil)))
  7600. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  7601. (org-todo "DONE")
  7602. (buffer-string))))))
  7603. (should
  7604. (string-match-p
  7605. ":LAST_REPEAT:"
  7606. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7607. (cl-letf (((symbol-function 'org-add-log-setup)
  7608. (lambda (&rest _args) nil)))
  7609. (org-test-with-temp-text
  7610. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  7611. (org-todo "DONE")
  7612. (buffer-string))))))
  7613. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  7614. ;; the entry as it is no longer relevant.
  7615. (should-not
  7616. (string-match-p
  7617. "^SCHEDULED:"
  7618. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7619. (org-test-with-temp-text
  7620. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  7621. (org-todo "DONE")
  7622. (buffer-string)))))
  7623. ;; Properly advance repeater even when a clock entry is specified
  7624. ;; and `org-log-repeat' is nil.
  7625. (should
  7626. (string-match-p
  7627. "SCHEDULED: <2014-03-29"
  7628. (let ((org-log-repeat nil)
  7629. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7630. (org-test-with-temp-text
  7631. "* TODO H
  7632. SCHEDULED: <2012-03-29 Thu +2y>
  7633. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  7634. (org-todo "DONE")
  7635. (buffer-string)))))
  7636. ;; Make sure that logbook state change record does not get
  7637. ;; duplicated when `org-log-repeat' `org-log-done' are non-nil.
  7638. (should
  7639. (string-match-p
  7640. (rx "* TODO Read book
  7641. SCHEDULED: <2021-06-16 " (1+ (not space)) " +1d>
  7642. :PROPERTIES:
  7643. :LAST_REPEAT:" (1+ nonl) "
  7644. :END:
  7645. - State \"DONE\" from \"TODO\"" (1+ nonl) buffer-end)
  7646. (let ((org-log-repeat 'time)
  7647. (org-todo-keywords '((sequence "TODO" "|" "DONE(d!)")))
  7648. (org-log-into-drawer nil))
  7649. (org-test-with-temp-text
  7650. "* TODO Read book
  7651. SCHEDULED: <2021-06-15 Tue +1d>"
  7652. (org-todo "DONE")
  7653. (when (memq 'org-add-log-note post-command-hook)
  7654. (org-add-log-note))
  7655. (buffer-string))))))
  7656. (ert-deftest test-org/org-log-done ()
  7657. "Test `org-log-done' specifications."
  7658. ;; nil value.
  7659. (should
  7660. (string=
  7661. "* DONE task"
  7662. (let ((org-log-done nil)
  7663. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7664. (org-test-with-temp-text
  7665. "* TODO task"
  7666. (org-todo "DONE")
  7667. (when (memq 'org-add-log-note post-command-hook)
  7668. (org-add-log-note))
  7669. (buffer-string)))))
  7670. ;; `time' value.
  7671. (should
  7672. (string=
  7673. (format
  7674. "* DONE task
  7675. CLOSED: %s"
  7676. (org-test-with-temp-text ""
  7677. (org-insert-time-stamp (current-time) t t)
  7678. (buffer-string)))
  7679. (let ((org-log-done 'time)
  7680. (org-log-done-with-time t)
  7681. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7682. (org-test-with-temp-text
  7683. "* TODO task"
  7684. (org-todo "DONE")
  7685. (when (memq 'org-add-log-note post-command-hook)
  7686. (org-add-log-note))
  7687. (buffer-string)))))
  7688. (should
  7689. (string=
  7690. (format
  7691. "* DONE task
  7692. CLOSED: %s"
  7693. (org-test-with-temp-text ""
  7694. (org-insert-time-stamp (current-time) nil t)
  7695. (buffer-string)))
  7696. (let ((org-log-done 'time)
  7697. (org-log-done-with-time nil)
  7698. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7699. (org-test-with-temp-text
  7700. "* TODO task"
  7701. (org-todo "DONE")
  7702. (when (memq 'org-add-log-note post-command-hook)
  7703. (org-add-log-note))
  7704. (buffer-string)))))
  7705. ;; TODO: Test `note' value.
  7706. ;; Test startup overrides.
  7707. (should
  7708. (string=
  7709. "#+STARTUP: nologdone
  7710. * DONE task"
  7711. (let ((org-log-done 'time)
  7712. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7713. (org-test-with-temp-text
  7714. "#+STARTUP: nologdone
  7715. <point>* TODO task"
  7716. (org-set-regexps-and-options)
  7717. (org-todo "DONE")
  7718. (when (memq 'org-add-log-note post-command-hook)
  7719. (org-add-log-note))
  7720. (buffer-string)))))
  7721. (should
  7722. (string=
  7723. (format
  7724. "#+STARTUP: logdone
  7725. * DONE task
  7726. CLOSED: %s"
  7727. (org-test-with-temp-text ""
  7728. (org-insert-time-stamp (current-time) t t)
  7729. (buffer-string)))
  7730. (let ((org-log-done nil)
  7731. (org-log-done-with-time t)
  7732. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7733. (org-test-with-temp-text
  7734. "#+STARTUP: logdone
  7735. <point>* TODO task"
  7736. (org-set-regexps-and-options)
  7737. (org-todo "DONE")
  7738. (when (memq 'org-add-log-note post-command-hook)
  7739. (org-add-log-note))
  7740. (buffer-string)))))
  7741. ;; Test local property overrides.
  7742. (should
  7743. (string=
  7744. "* DONE task
  7745. :PROPERTIES:
  7746. :LOGGING: nil
  7747. :END:"
  7748. (let ((org-log-done 'time)
  7749. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7750. (org-test-with-temp-text
  7751. "* TODO task
  7752. :PROPERTIES:
  7753. :LOGGING: nil
  7754. :END:"
  7755. (org-todo "DONE")
  7756. (when (memq 'org-add-log-note post-command-hook)
  7757. (org-add-log-note))
  7758. (buffer-string)))))
  7759. (should
  7760. (string=
  7761. (format
  7762. "* DONE task
  7763. CLOSED: %s
  7764. :PROPERTIES:
  7765. :LOGGING: logdone
  7766. :END:"
  7767. (org-test-with-temp-text ""
  7768. (org-insert-time-stamp (current-time) t t)
  7769. (buffer-string)))
  7770. (let ((org-log-done nil)
  7771. (org-log-done-with-time t)
  7772. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7773. (org-test-with-temp-text
  7774. "* TODO task
  7775. :PROPERTIES:
  7776. :LOGGING: logdone
  7777. :END:"
  7778. (org-todo "DONE")
  7779. (when (memq 'org-add-log-note post-command-hook)
  7780. (org-add-log-note))
  7781. (buffer-string))))))
  7782. (ert-deftest test-org/org-todo-prefix ()
  7783. "Test `org-todo' prefix arg behavior."
  7784. ;; FIXME: Add tests for all other allowed prefix arguments.
  7785. ;; -1 prefix arg should cancel repeater and mark DONE.
  7786. (should
  7787. (string-match-p
  7788. "DONE H\\(.*\n\\)*<2012-03-29 Thu \\+0y>"
  7789. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7790. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7791. (org-todo -1)
  7792. (buffer-string)))))
  7793. ;; - prefix arg should cancel repeater and mark DONE.
  7794. (should
  7795. (string-match-p
  7796. "DONE H\\(.*\n\\)*<2012-03-29 Thu \\+0y>"
  7797. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7798. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7799. (org-todo '-)
  7800. (buffer-string))))))
  7801. ;;; Timestamps API
  7802. (ert-deftest test-org/at-timestamp-p ()
  7803. "Test `org-at-timestamp-p' specifications."
  7804. (should
  7805. (org-test-with-temp-text "<2012-03-29 Thu>"
  7806. (org-at-timestamp-p)))
  7807. (should-not
  7808. (org-test-with-temp-text "2012-03-29 Thu"
  7809. (org-at-timestamp-p)))
  7810. ;; Test return values.
  7811. (should
  7812. (eq 'bracket
  7813. (org-test-with-temp-text "<2012-03-29 Thu>"
  7814. (org-at-timestamp-p))))
  7815. (should
  7816. (eq 'year
  7817. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  7818. (org-at-timestamp-p))))
  7819. (should
  7820. (eq 'month
  7821. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  7822. (org-at-timestamp-p))))
  7823. (should
  7824. (eq 'day
  7825. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  7826. (org-at-timestamp-p))))
  7827. (should
  7828. (eq 'day
  7829. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  7830. (org-at-timestamp-p))))
  7831. (should
  7832. (wholenump
  7833. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  7834. (org-at-timestamp-p))))
  7835. (should
  7836. (eq 'bracket
  7837. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  7838. (org-at-timestamp-p))))
  7839. (should
  7840. (eq 'after
  7841. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  7842. (org-at-timestamp-p))))
  7843. ;; Test `inactive' optional argument.
  7844. (should
  7845. (org-test-with-temp-text "[2012-03-29 Thu]"
  7846. (org-at-timestamp-p 'inactive)))
  7847. (should-not
  7848. (org-test-with-temp-text "[2012-03-29 Thu]"
  7849. (org-at-timestamp-p)))
  7850. ;; When optional argument is `agenda', recognize time-stamps in
  7851. ;; planning info line, property drawers and clocks.
  7852. (should
  7853. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  7854. (org-at-timestamp-p 'agenda)))
  7855. (should-not
  7856. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  7857. (org-at-timestamp-p)))
  7858. (should
  7859. (org-test-with-temp-text
  7860. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  7861. (org-at-timestamp-p 'agenda)))
  7862. (should-not
  7863. (org-test-with-temp-text
  7864. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  7865. (org-at-timestamp-p)))
  7866. (should
  7867. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  7868. (let ((org-agenda-include-inactive-timestamps t))
  7869. (org-at-timestamp-p 'agenda))))
  7870. (should-not
  7871. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  7872. (let ((org-agenda-include-inactive-timestamps t))
  7873. (org-at-timestamp-p))))
  7874. (should-not
  7875. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  7876. (let ((org-agenda-include-inactive-timestamps t))
  7877. (org-at-timestamp-p 'inactive))))
  7878. ;; When optional argument is `lax', match any part of the document
  7879. ;; with Org timestamp syntax.
  7880. (should
  7881. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  7882. (org-at-timestamp-p 'lax)))
  7883. (should-not
  7884. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  7885. (org-at-timestamp-p)))
  7886. (should
  7887. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  7888. (org-at-timestamp-p 'lax)))
  7889. (should-not
  7890. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  7891. (org-at-timestamp-p)))
  7892. (should
  7893. (org-test-with-temp-text
  7894. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  7895. (org-at-timestamp-p 'lax)))
  7896. (should-not
  7897. (org-test-with-temp-text
  7898. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  7899. (org-at-timestamp-p)))
  7900. ;; Optional argument `lax' also matches inactive timestamps.
  7901. (should
  7902. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  7903. (org-at-timestamp-p 'lax))))
  7904. (ert-deftest test-org/time-stamp ()
  7905. "Test `org-time-stamp' specifications."
  7906. ;; Insert chosen time stamp at point.
  7907. (should
  7908. (string-match
  7909. "Te<2014-03-04 .*?>xt"
  7910. (org-test-with-temp-text "Te<point>xt"
  7911. (cl-letf (((symbol-function 'org-read-date)
  7912. (lambda (&rest _args)
  7913. (org-time-string-to-time "2014-03-04"))))
  7914. (org-time-stamp nil)
  7915. (buffer-string)))))
  7916. ;; With a prefix argument, also insert time.
  7917. (should
  7918. (string-match
  7919. "Te<2014-03-04 .*? 00:41>xt"
  7920. (org-test-with-temp-text "Te<point>xt"
  7921. (cl-letf (((symbol-function 'org-read-date)
  7922. (lambda (&rest _args)
  7923. (org-time-string-to-time "2014-03-04 00:41"))))
  7924. (org-time-stamp '(4))
  7925. (buffer-string)))))
  7926. ;; With two universal prefix arguments, insert an active timestamp
  7927. ;; with the current time without prompting the user.
  7928. (should
  7929. (string-match
  7930. "Te<2014-03-04 .*? 00:41>xt"
  7931. (org-test-with-temp-text "Te<point>xt"
  7932. (org-test-at-time "2014-03-04 00:41"
  7933. (org-time-stamp '(16))
  7934. (buffer-string)))))
  7935. ;; When optional argument is non-nil, insert an inactive timestamp.
  7936. (should
  7937. (string-match
  7938. "Te\\[2014-03-04 .*?\\]xt"
  7939. (org-test-with-temp-text "Te<point>xt"
  7940. (cl-letf (((symbol-function 'org-read-date)
  7941. (lambda (&rest _args)
  7942. (org-time-string-to-time "2014-03-04"))))
  7943. (org-time-stamp nil t)
  7944. (buffer-string)))))
  7945. ;; When called from a timestamp, replace existing one.
  7946. (should
  7947. (string-match
  7948. "<2014-03-04 .*?>"
  7949. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  7950. (cl-letf (((symbol-function 'org-read-date)
  7951. (lambda (&rest _args)
  7952. (org-time-string-to-time "2014-03-04"))))
  7953. (org-time-stamp nil)
  7954. (buffer-string)))))
  7955. (should
  7956. (string-match
  7957. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  7958. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  7959. (cl-letf (((symbol-function 'org-read-date)
  7960. (lambda (&rest _args)
  7961. (org-time-string-to-time "2014-03-04"))))
  7962. (org-time-stamp nil)
  7963. (buffer-string)))))
  7964. ;; When replacing a timestamp, preserve repeater, if any.
  7965. (should
  7966. (string-match
  7967. "<2014-03-04 .*? \\+2y>"
  7968. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  7969. (cl-letf (((symbol-function 'org-read-date)
  7970. (lambda (&rest _args)
  7971. (org-time-string-to-time "2014-03-04"))))
  7972. (org-time-stamp nil)
  7973. (buffer-string)))))
  7974. ;; When called twice in a raw, build a date range.
  7975. (should
  7976. (string-match
  7977. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  7978. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  7979. (cl-letf (((symbol-function 'org-read-date)
  7980. (lambda (&rest _args)
  7981. (org-time-string-to-time "2014-03-04"))))
  7982. (let ((last-command 'org-time-stamp)
  7983. (this-command 'org-time-stamp))
  7984. (org-time-stamp nil))
  7985. (buffer-string))))))
  7986. (ert-deftest test-org/timestamp-has-time-p ()
  7987. "Test `org-timestamp-has-time-p' specifications."
  7988. ;; With time.
  7989. (should
  7990. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  7991. (org-timestamp-has-time-p (org-element-context))))
  7992. ;; Without time.
  7993. (should-not
  7994. (org-test-with-temp-text "<2012-03-29 Thu>"
  7995. (org-timestamp-has-time-p (org-element-context)))))
  7996. (ert-deftest test-org/get-repeat ()
  7997. "Test `org-get-repeat' specifications."
  7998. (should
  7999. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  8000. (org-get-repeat)))
  8001. (should-not
  8002. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  8003. (org-get-repeat)))
  8004. ;; Return proper repeat string.
  8005. (should
  8006. (equal "+2y"
  8007. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  8008. (org-get-repeat))))
  8009. ;; Prevent false positive (commented or verbatim time stamps)
  8010. (should-not
  8011. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  8012. (org-get-repeat)))
  8013. (should-not
  8014. (org-test-with-temp-text
  8015. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  8016. (org-get-repeat)))
  8017. ;; Return nil when called before first heading.
  8018. (should-not
  8019. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  8020. (org-get-repeat)))
  8021. ;; When called with an optional argument, extract repeater from that
  8022. ;; string instead.
  8023. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  8024. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  8025. (ert-deftest test-org/timestamp-format ()
  8026. "Test `org-timestamp-format' specifications."
  8027. ;; Regular test.
  8028. (should
  8029. (equal
  8030. "2012-03-29 16:40"
  8031. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  8032. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  8033. ;; Range end.
  8034. (should
  8035. (equal
  8036. "2012-03-29"
  8037. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  8038. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  8039. (ert-deftest test-org/timestamp-split-range ()
  8040. "Test `org-timestamp-split-range' specifications."
  8041. ;; Extract range start (active).
  8042. (should
  8043. (equal '(2012 3 29)
  8044. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8045. (let ((ts (org-timestamp-split-range (org-element-context))))
  8046. (mapcar (lambda (p) (org-element-property p ts))
  8047. '(:year-end :month-end :day-end))))))
  8048. ;; Extract range start (inactive)
  8049. (should
  8050. (equal '(2012 3 29)
  8051. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  8052. (let ((ts (org-timestamp-split-range (org-element-context))))
  8053. (mapcar (lambda (p) (org-element-property p ts))
  8054. '(:year-end :month-end :day-end))))))
  8055. ;; Extract range end (active).
  8056. (should
  8057. (equal '(2012 3 30)
  8058. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8059. (let ((ts (org-timestamp-split-range
  8060. (org-element-context) t)))
  8061. (mapcar (lambda (p) (org-element-property p ts))
  8062. '(:year-end :month-end :day-end))))))
  8063. ;; Extract range end (inactive)
  8064. (should
  8065. (equal '(2012 3 30)
  8066. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  8067. (let ((ts (org-timestamp-split-range
  8068. (org-element-context) t)))
  8069. (mapcar (lambda (p) (org-element-property p ts))
  8070. '(:year-end :month-end :day-end))))))
  8071. ;; Return the timestamp if not a range.
  8072. (should
  8073. (org-test-with-temp-text "[2012-03-29 Thu]"
  8074. (let* ((ts-orig (org-element-context))
  8075. (ts-copy (org-timestamp-split-range ts-orig)))
  8076. (eq ts-orig ts-copy))))
  8077. (should
  8078. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  8079. (let* ((ts-orig (org-element-context))
  8080. (ts-copy (org-timestamp-split-range ts-orig)))
  8081. (eq ts-orig ts-copy)))))
  8082. (ert-deftest test-org/timestamp-translate ()
  8083. "Test `org-timestamp-translate' specifications."
  8084. ;; Translate whole date range.
  8085. (should
  8086. (equal "<29>--<30>"
  8087. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8088. (let ((org-display-custom-times t)
  8089. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8090. (org-timestamp-translate (org-element-context))))))
  8091. ;; Translate date range start.
  8092. (should
  8093. (equal "<29>"
  8094. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8095. (let ((org-display-custom-times t)
  8096. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8097. (org-timestamp-translate (org-element-context) 'start)))))
  8098. ;; Translate date range end.
  8099. (should
  8100. (equal "<30>"
  8101. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8102. (let ((org-display-custom-times t)
  8103. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8104. (org-timestamp-translate (org-element-context) 'end)))))
  8105. ;; Translate time range.
  8106. (should
  8107. (equal "<08>--<16>"
  8108. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  8109. (let ((org-display-custom-times t)
  8110. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  8111. (org-timestamp-translate (org-element-context))))))
  8112. ;; Translate non-range timestamp.
  8113. (should
  8114. (equal "<29>"
  8115. (org-test-with-temp-text "<2012-03-29 Thu>"
  8116. (let ((org-display-custom-times t)
  8117. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8118. (org-timestamp-translate (org-element-context))))))
  8119. ;; Do not change `diary' timestamps.
  8120. (should
  8121. (equal "<%%(org-float t 4 2)>"
  8122. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  8123. (let ((org-display-custom-times t)
  8124. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8125. (org-timestamp-translate (org-element-context)))))))
  8126. (ert-deftest test-org/timestamp-from-string ()
  8127. "Test `org-timestamp-from-string' specifications."
  8128. ;; Return nil if argument is not a valid Org timestamp.
  8129. (should-not (org-timestamp-from-string ""))
  8130. (should-not (org-timestamp-from-string nil))
  8131. (should-not (org-timestamp-from-string "<2012-03-29"))
  8132. ;; Otherwise, return a valid Org timestamp object.
  8133. (should
  8134. (string-match-p "<2012-03-29 .+>"
  8135. (org-element-interpret-data
  8136. (org-timestamp-from-string "<2012-03-29 Thu>"))))
  8137. (should
  8138. (string-match-p "[2014-03-04 .+]"
  8139. (org-element-interpret-data
  8140. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  8141. (ert-deftest test-org/timestamp-from-time ()
  8142. "Test `org-timestamp-from-time' specifications."
  8143. ;; Standard test.
  8144. (should
  8145. (string-match-p
  8146. "<2012-03-29 .+>"
  8147. (org-element-interpret-data
  8148. (org-timestamp-from-time
  8149. (org-time-string-to-time "<2012-03-29 Thu 16:40>")))))
  8150. ;; When optional argument WITH-TIME is non-nil, provide time
  8151. ;; information.
  8152. (should
  8153. (string-match-p
  8154. "<2012-03-29 .+ 16:40>"
  8155. (org-element-interpret-data
  8156. (org-timestamp-from-time
  8157. (org-time-string-to-time "<2012-03-29 Thu 16:40>")
  8158. t))))
  8159. ;; When optional argument INACTIVE is non-nil, return an inactive
  8160. ;; timestamp.
  8161. (should
  8162. (string-match-p
  8163. "[2012-03-29 .+]"
  8164. (org-element-interpret-data
  8165. (org-timestamp-from-time
  8166. (org-time-string-to-time "<2012-03-29 Thu 16:40>")
  8167. nil t)))))
  8168. (ert-deftest test-org/timestamp-to-time ()
  8169. "Test `org-timestamp-to-time' specifications."
  8170. (should
  8171. (equal "2014-03-04"
  8172. (format-time-string
  8173. "%Y-%m-%d"
  8174. (org-timestamp-to-time
  8175. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  8176. (should
  8177. (equal "2014-03-04"
  8178. (format-time-string
  8179. "%Y-%m-%d"
  8180. (org-timestamp-to-time
  8181. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  8182. (should
  8183. (equal "2012-03-29 08:30"
  8184. (format-time-string
  8185. "%Y-%m-%d %H:%M"
  8186. (org-timestamp-to-time
  8187. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  8188. (should
  8189. (equal "2012-03-29"
  8190. (format-time-string
  8191. "%Y-%m-%d"
  8192. (org-timestamp-to-time
  8193. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  8194. (should
  8195. (equal "2012-03-29"
  8196. (format-time-string
  8197. "%Y-%m-%d"
  8198. (org-timestamp-to-time
  8199. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  8200. ;; When optional argument END is non-nil, use end of date range or
  8201. ;; time range.
  8202. (should
  8203. (equal "2012-03-29 16:40"
  8204. (format-time-string
  8205. "%Y-%m-%d %H:%M"
  8206. (org-timestamp-to-time
  8207. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  8208. t))))
  8209. (should
  8210. (equal "2014-03-04"
  8211. (format-time-string
  8212. "%Y-%m-%d"
  8213. (org-timestamp-to-time
  8214. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  8215. t))))
  8216. (should
  8217. (equal "2014-03-04"
  8218. (format-time-string
  8219. "%Y-%m-%d"
  8220. (org-timestamp-to-time
  8221. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  8222. t)))))
  8223. ;;; Yank and Kill
  8224. (ert-deftest test-org/paste-subtree ()
  8225. "Test `org-paste-subtree' specifications."
  8226. ;; Return an error if text to yank is not a set of subtrees.
  8227. (should-error (org-paste-subtree nil "Text"))
  8228. ;; Adjust level according to current one.
  8229. (should
  8230. (equal "* H\n* Text\n"
  8231. (org-test-with-temp-text "* H\n<point>"
  8232. (org-paste-subtree nil "* Text")
  8233. (buffer-string))))
  8234. (should
  8235. (equal "* H1\n** H2\n** Text\n"
  8236. (org-test-with-temp-text "* H1\n** H2\n<point>"
  8237. (org-paste-subtree nil "* Text")
  8238. (buffer-string))))
  8239. ;; When not on a heading, move to next heading before yanking.
  8240. (should
  8241. (equal "* H1\nParagraph\n* Text\n* H2"
  8242. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  8243. (org-paste-subtree nil "* Text")
  8244. (buffer-string))))
  8245. ;; If point is between two headings, use the deepest level.
  8246. (should
  8247. (equal "* H1\n\n* Text\n* H2"
  8248. (org-test-with-temp-text "* H1\n<point>\n* H2"
  8249. (org-paste-subtree nil "* Text")
  8250. (buffer-string))))
  8251. (should
  8252. (equal "** H1\n\n** Text\n* H2"
  8253. (org-test-with-temp-text "** H1\n<point>\n* H2"
  8254. (org-paste-subtree nil "* Text")
  8255. (buffer-string))))
  8256. (should
  8257. (equal "* H1\n\n** Text\n** H2"
  8258. (org-test-with-temp-text "* H1\n<point>\n** H2"
  8259. (org-paste-subtree nil "* Text")
  8260. (buffer-string))))
  8261. ;; When on an empty heading, after the stars, deduce the new level
  8262. ;; from the number of stars.
  8263. (should
  8264. (equal "*** Text\n"
  8265. (org-test-with-temp-text "*** <point>"
  8266. (org-paste-subtree nil "* Text")
  8267. (buffer-string))))
  8268. ;; Optional argument LEVEL forces a level for the subtree.
  8269. (should
  8270. (equal "* H\n*** Text\n"
  8271. (org-test-with-temp-text "* H<point>"
  8272. (org-paste-subtree 3 "* Text")
  8273. (buffer-string)))))
  8274. (ert-deftest test-org/cut-and-paste-subtree ()
  8275. "Test `org-cut-subtree' and `org-paste-subtree'."
  8276. (should
  8277. (equal
  8278. "* Two
  8279. two
  8280. * One
  8281. "
  8282. (org-test-with-temp-text
  8283. "* One
  8284. <point>* Two
  8285. two
  8286. "
  8287. (call-interactively #'org-cut-subtree)
  8288. (goto-char (point-min))
  8289. (call-interactively #'org-paste-subtree)
  8290. (buffer-string))))
  8291. (should
  8292. (equal
  8293. "* One
  8294. * Two
  8295. "
  8296. (org-test-with-temp-text
  8297. "* One
  8298. <point>* Two
  8299. "
  8300. (call-interactively #'org-cut-subtree)
  8301. (backward-char)
  8302. (call-interactively #'org-paste-subtree)
  8303. (buffer-string)))))
  8304. (ert-deftest test-org/org--open-file-format-command ()
  8305. "Test `org--open-file-format-command' helper for `org-open-file'."
  8306. (let ((system-type 'gnu/linux)) ; Fix behavior of `shell-quote-argument'.
  8307. ;; No additional groups in `org-file-apps' key.
  8308. (let ((file "/file.pdf")
  8309. (pattern "\\.pdf\\'"))
  8310. (should
  8311. (equal "simple /file.pdf"
  8312. (and (string-match pattern file)
  8313. (org--open-file-format-command
  8314. "simple %s" file file (match-data)))))
  8315. (should
  8316. (equal "single-quotes /file.pdf"
  8317. (and (string-match pattern file)
  8318. (org--open-file-format-command
  8319. "single-quotes '%s'" file file (match-data)))))
  8320. (should
  8321. (equal "double-quotes /file.pdf"
  8322. (and (string-match pattern file)
  8323. (org--open-file-format-command
  8324. "double-quotes \"%s\"" file file (match-data)))))
  8325. (should
  8326. (equal "quotes 'mismatch \"/file.pdf'"
  8327. (and (string-match pattern file)
  8328. (org--open-file-format-command
  8329. "quotes 'mismatch \"%s'" file file (match-data)))))
  8330. (should
  8331. (equal "no subst"
  8332. (and (string-match pattern file)
  8333. (org--open-file-format-command
  8334. "no subst" file file (match-data)))))
  8335. (should
  8336. (equal "% literal percent 100% %s"
  8337. (and (string-match pattern file)
  8338. (org--open-file-format-command
  8339. "\\% literal percent 100\\% \\%s" file file (match-data)))))
  8340. (should
  8341. (equal "escape \"/file.pdf\" \\ more"
  8342. (and (string-match pattern file)
  8343. (org--open-file-format-command
  8344. ;; Second quote is not escaped.
  8345. "escape \\\"%s\" \\\\ more" file file (match-data)))))
  8346. (should
  8347. (equal "/file.pdf file at start"
  8348. (and (string-match pattern file)
  8349. (org--open-file-format-command
  8350. "%s file at start" file file (match-data)))))
  8351. (should
  8352. (equal "backslash-newline\n/file.pdf"
  8353. (and (string-match pattern file)
  8354. (org--open-file-format-command
  8355. "backslash-newline\\\n%s" file file (match-data))))))
  8356. ;; Anchors within target file.
  8357. (let ((file "/page-search.pdf")
  8358. (link "/page-search.pdf::10::some words")
  8359. (pattern "\\.pdf::\\([0-9]+\\)::\\(.*\\)\\'"))
  8360. (should
  8361. (equal "zathura --page 10 --find some\\ words /page-search.pdf"
  8362. (and (string-match pattern link)
  8363. (org--open-file-format-command
  8364. "zathura --page '%1' --find %2 \"%s\"" file link (match-data)))))
  8365. ;; Unused %2.
  8366. (should
  8367. (equal "firefox file:///page-search.pdf\\#page=10"
  8368. (and (string-match pattern link)
  8369. (org--open-file-format-command
  8370. "firefox file://%s\\\\#page=%1" file link (match-data)))))
  8371. (should
  8372. (equal "adjucent-subst /page-search.pdfsome\\ words10some\\ words"
  8373. (and (string-match pattern link)
  8374. (org--open-file-format-command
  8375. "adjucent-subst %s%2'%1'%2" file link (match-data))))))
  8376. ;; No more than 9 substitutions are supported.
  8377. (let ((file "/many.pdf")
  8378. (link "/many.pdf::one:2:3:4:5:6:7:8:9:a:b:c")
  8379. (pattern (concat "\\.pdf:"
  8380. (mapconcat (lambda (_) ":\\([^:]+\\)")
  8381. (number-sequence 1 12)
  8382. "")
  8383. "\\'")))
  8384. (should
  8385. (equal "overflow /many.pdf::one:2:3:4:5:6:7:8:9:one0:one1:one2"
  8386. (and (string-match pattern link)
  8387. (org--open-file-format-command
  8388. "overflow %s::%1:%2:%3:%4:%5:%6:%7:%8:%9:%10:%11:%12"
  8389. file link (match-data))))))
  8390. ;; Percent character in link fields does not cause any problem.
  8391. (let ((file "/file-%2.pdf")
  8392. (link "/file-%2.pdf::anchor-%3::search %1")
  8393. (pattern "\\.pdf::\\([^:]+\\)::\\(.+\\)\\'"))
  8394. (should
  8395. (equal "percents --find search\\ \\%1 file:///file-\\%2.pdf\\#anchor-\\%3"
  8396. (and (string-match pattern link)
  8397. (org--open-file-format-command
  8398. "percents --find %2 file://%s\\\\#%1"
  8399. file link (match-data))))))
  8400. ;; Errors.
  8401. (let ((file "/error.pdf")
  8402. (pattern "\\.pdf\\'"))
  8403. (let* ((err (should-error
  8404. (and (string-match pattern file)
  8405. (org--open-file-format-command
  8406. "trailing-percent %s %" file file (match-data)))
  8407. :type 'error))
  8408. (err-text (cadr err)))
  8409. (should-not (unless (and (stringp err-text)
  8410. (string-match-p "\\`Invalid format .*%" err-text))
  8411. err)))
  8412. (let* ((err (should-error
  8413. (and (string-match pattern file)
  8414. (org--open-file-format-command
  8415. "trailing-backslash %s \\" file file (match-data)))
  8416. :type 'error))
  8417. (err-text (cadr err)))
  8418. (should-not (unless (and (stringp err-text)
  8419. (string-match-p "\\`Invalid format .*\\\\" err-text))
  8420. err)))
  8421. (let* ((err (should-error
  8422. (and (string-match pattern file)
  8423. (org--open-file-format-command
  8424. "percent-newline %\n%s" file file (match-data)))
  8425. :type 'error))
  8426. (err-text (cadr err)))
  8427. (should-not (unless (and (stringp err-text)
  8428. (string-match-p "\\`Invalid format .*%\n" err-text))
  8429. err)))
  8430. ;; Mailcap escape for "%" is "\%", not "%%".
  8431. (let* ((err (should-error
  8432. (and (string-match pattern file)
  8433. (org--open-file-format-command
  8434. "percent-percent %s%%" file file (match-data)))
  8435. :type 'error))
  8436. (err-text (cadr err)))
  8437. (should-not (unless (and (stringp err-text)
  8438. (string-match-p "\\`Invalid format .*%%" err-text))
  8439. err)))
  8440. ;; Mailcap allows "%t" for MIME type, but Org has no such information.
  8441. (let* ((err (should-error
  8442. (and (string-match pattern file)
  8443. (org--open-file-format-command
  8444. "percent-t-unsupported --type '%t' %s" file file (match-data)))
  8445. :type 'error))
  8446. (err-text (cadr err)))
  8447. (should-not (unless (and (stringp err-text)
  8448. (string-match-p "\\`Invalid format .*%t" err-text))
  8449. err))))
  8450. ;; Optional regular expression groups have no point in `org-file-apps' patterns.
  8451. (let* ((file "/error.pdf")
  8452. (link "/error.pdf::1")
  8453. (pattern "\\.pdf::\\([^:]+\\)\\(?:::\\(.+\\)\\)?\\'")
  8454. (err (should-error
  8455. (and (string-match pattern link)
  8456. (org--open-file-format-command
  8457. "no-such-match --search %2 %s" file link (match-data)))
  8458. :type 'error))
  8459. (err-text (cadr err)))
  8460. (should-not (unless (and (stringp err-text)
  8461. (string-match-p "\\`Invalid format.*%2" err-text))
  8462. err)))))
  8463. (provide 'test-org)
  8464. ;;; test-org.el ends here