test-org.el 194 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982
  1. ;;; test-org.el --- tests for org.el
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Comments:
  16. ;; Template test file for Org tests
  17. ;;; Code:
  18. ;;; Comments
  19. (ert-deftest test-org/toggle-comment ()
  20. "Test `org-toggle-comment' specifications."
  21. ;; Simple headline.
  22. (should
  23. (equal "* Test"
  24. (org-test-with-temp-text "* COMMENT Test"
  25. (org-toggle-comment)
  26. (buffer-string))))
  27. (should
  28. (equal "* COMMENT Test"
  29. (org-test-with-temp-text "* Test"
  30. (org-toggle-comment)
  31. (buffer-string))))
  32. ;; Headline with a regular keyword.
  33. (should
  34. (equal "* TODO Test"
  35. (org-test-with-temp-text "* TODO COMMENT Test"
  36. (org-toggle-comment)
  37. (buffer-string))))
  38. (should
  39. (equal "* TODO COMMENT Test"
  40. (org-test-with-temp-text "* TODO Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. ;; Empty headline.
  44. (should
  45. (equal "* "
  46. (org-test-with-temp-text "* COMMENT"
  47. (org-toggle-comment)
  48. (buffer-string))))
  49. (should
  50. (equal "* COMMENT"
  51. (org-test-with-temp-text "* "
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. ;; Headline with a single keyword.
  55. (should
  56. (equal "* TODO "
  57. (org-test-with-temp-text "* TODO COMMENT"
  58. (org-toggle-comment)
  59. (buffer-string))))
  60. (should
  61. (equal "* TODO COMMENT"
  62. (org-test-with-temp-text "* TODO"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. ;; Headline with a keyword, a priority cookie and contents.
  66. (should
  67. (equal "* TODO [#A] Headline"
  68. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  69. (org-toggle-comment)
  70. (buffer-string))))
  71. (should
  72. (equal "* TODO [#A] COMMENT Headline"
  73. (org-test-with-temp-text "* TODO [#A] Headline"
  74. (org-toggle-comment)
  75. (buffer-string)))))
  76. (ert-deftest test-org/comment-dwim ()
  77. "Test `comment-dwim' behaviour in an Org buffer."
  78. ;; No region selected, no comment on current line and line not
  79. ;; empty: insert comment on line above.
  80. (should
  81. (equal "# \nComment"
  82. (org-test-with-temp-text "Comment"
  83. (progn (call-interactively 'comment-dwim)
  84. (buffer-string)))))
  85. ;; No region selected, no comment on current line and line empty:
  86. ;; insert comment on this line.
  87. (should
  88. (equal "# \nParagraph"
  89. (org-test-with-temp-text "\nParagraph"
  90. (progn (call-interactively 'comment-dwim)
  91. (buffer-string)))))
  92. ;; No region selected, and a comment on this line: indent it.
  93. (should
  94. (equal "* Headline\n # Comment"
  95. (org-test-with-temp-text "* Headline\n# Comment"
  96. (progn (forward-line)
  97. (let ((org-adapt-indentation t))
  98. (call-interactively 'comment-dwim))
  99. (buffer-string)))))
  100. ;; Also recognize single # at column 0 as comments.
  101. (should
  102. (equal "# Comment"
  103. (org-test-with-temp-text "# Comment"
  104. (progn (forward-line)
  105. (call-interactively 'comment-dwim)
  106. (buffer-string)))))
  107. ;; Region selected and only comments and blank lines within it:
  108. ;; un-comment all commented lines.
  109. (should
  110. (equal "Comment 1\n\nComment 2"
  111. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  112. (progn
  113. (transient-mark-mode 1)
  114. (push-mark (point) t t)
  115. (goto-char (point-max))
  116. (call-interactively 'comment-dwim)
  117. (buffer-string)))))
  118. ;; Region selected without comments: comment all lines if
  119. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  120. (should
  121. (equal "# Comment 1\n\n# Comment 2"
  122. (org-test-with-temp-text "Comment 1\n\nComment 2"
  123. (progn
  124. (transient-mark-mode 1)
  125. (push-mark (point) t t)
  126. (goto-char (point-max))
  127. (let ((comment-empty-lines nil))
  128. (call-interactively 'comment-dwim))
  129. (buffer-string)))))
  130. (should
  131. (equal "# Comment 1\n# \n# Comment 2"
  132. (org-test-with-temp-text "Comment 1\n\nComment 2"
  133. (progn
  134. (transient-mark-mode 1)
  135. (push-mark (point) t t)
  136. (goto-char (point-max))
  137. (let ((comment-empty-lines t))
  138. (call-interactively 'comment-dwim))
  139. (buffer-string)))))
  140. ;; In front of a keyword without region, insert a new comment.
  141. (should
  142. (equal "# \n#+KEYWORD: value"
  143. (org-test-with-temp-text "#+KEYWORD: value"
  144. (progn (call-interactively 'comment-dwim)
  145. (buffer-string)))))
  146. ;; In a source block, use appropriate syntax.
  147. (should
  148. (equal " ;; "
  149. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n\n#+END_SRC"
  150. (forward-line)
  151. (let ((org-edit-src-content-indentation 2))
  152. (call-interactively 'comment-dwim))
  153. (buffer-substring-no-properties (line-beginning-position) (point)))))
  154. (should
  155. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  156. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\na\nb\n#+END_SRC"
  157. (forward-line)
  158. (transient-mark-mode 1)
  159. (push-mark (point) t t)
  160. (forward-line 2)
  161. (let ((org-edit-src-content-indentation 2))
  162. (call-interactively 'comment-dwim))
  163. (buffer-string)))))
  164. ;;; Date and time analysis
  165. (ert-deftest test-org/org-read-date ()
  166. "Test `org-read-date' specifications."
  167. ;; Parse ISO date with abbreviated year and month.
  168. (should (equal "2012-03-29 16:40"
  169. (let ((org-time-was-given t))
  170. (org-read-date t nil "12-3-29 16:40"))))
  171. ;; Parse Europeans dates.
  172. (should (equal "2012-03-29 16:40"
  173. (let ((org-time-was-given t))
  174. (org-read-date t nil "29.03.2012 16:40"))))
  175. ;; Parse Europeans dates without year.
  176. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  177. (let ((org-time-was-given t))
  178. (org-read-date t nil "29.03. 16:40"))))
  179. ;; Relative date applied to current time if there is single
  180. ;; plus/minus, or to default date when there are two of them.
  181. (should
  182. (equal
  183. "2015-03-04"
  184. (cl-letf (((symbol-function 'current-time)
  185. (lambda ()
  186. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  187. (org-read-date
  188. t nil "+1y" nil
  189. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  190. (should
  191. (equal
  192. "2013-03-29"
  193. (cl-letf (((symbol-function 'current-time)
  194. (lambda ()
  195. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  196. (org-read-date
  197. t nil "++1y" nil
  198. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))
  199. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  200. ;; dates (relatively to now) when incomplete. Otherwise, use
  201. ;; default date.
  202. (should
  203. (equal
  204. "2014-04-01"
  205. (cl-letf (((symbol-function 'current-time)
  206. (lambda ()
  207. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  208. (let ((org-read-date-prefer-future t))
  209. (org-read-date t nil "1")))))
  210. (should
  211. (equal
  212. "2013-03-04"
  213. (cl-letf (((symbol-function 'current-time)
  214. (lambda ()
  215. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  216. (let ((org-read-date-prefer-future t))
  217. (org-read-date t nil "3-4")))))
  218. (should
  219. (equal
  220. "2012-03-04"
  221. (cl-letf (((symbol-function 'current-time)
  222. (lambda ()
  223. (apply #'encode-time (org-parse-time-string "2012-03-29")))))
  224. (let ((org-read-date-prefer-future nil))
  225. (org-read-date t nil "3-4")))))
  226. ;; When set to `org-read-date-prefer-future' is set to `time', read
  227. ;; day is moved to tomorrow if specified hour is before current
  228. ;; time. However, it only happens in no other part of the date is
  229. ;; specified.
  230. (should
  231. (equal
  232. "2012-03-30"
  233. (cl-letf (((symbol-function 'current-time)
  234. (lambda ()
  235. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  236. (let ((org-read-date-prefer-future 'time))
  237. (org-read-date t nil "00:40" nil)))))
  238. (should-not
  239. (equal
  240. "2012-03-30"
  241. (cl-letf (((symbol-function 'current-time)
  242. (lambda ()
  243. (apply #'encode-time (org-parse-time-string "2012-03-29 16:40")))))
  244. (let ((org-read-date-prefer-future 'time))
  245. (org-read-date t nil "29 00:40" nil)))))
  246. ;; Caveat: `org-read-date-prefer-future' always refers to current
  247. ;; time, not default time, when they differ.
  248. (should
  249. (equal
  250. "2014-04-01"
  251. (cl-letf (((symbol-function 'current-time)
  252. (lambda ()
  253. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  254. (let ((org-read-date-prefer-future t))
  255. (org-read-date
  256. t nil "1" nil
  257. (apply #'encode-time (org-parse-time-string "2012-03-29")))))))
  258. (should
  259. (equal
  260. "2014-03-25"
  261. (cl-letf (((symbol-function 'current-time)
  262. (lambda ()
  263. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  264. (let ((org-read-date-prefer-future t))
  265. (org-read-date
  266. t nil "25" nil
  267. (apply #'encode-time (org-parse-time-string "2012-03-29"))))))))
  268. (ert-deftest test-org/org-parse-time-string ()
  269. "Test `org-parse-time-string'."
  270. (should (equal (org-parse-time-string "2012-03-29 16:40")
  271. '(0 40 16 29 3 2012 nil nil nil)))
  272. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  273. '(0 40 16 29 3 2012 nil nil nil)))
  274. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  275. '(0 40 16 29 3 2012 nil nil nil)))
  276. (should (equal (org-parse-time-string "<2012-03-29>")
  277. '(0 0 0 29 3 2012 nil nil nil)))
  278. (should (equal (org-parse-time-string "<2012-03-29>" t)
  279. '(0 nil nil 29 3 2012 nil nil nil))))
  280. (ert-deftest test-org/closest-date ()
  281. "Test `org-closest-date' specifications."
  282. (require 'calendar)
  283. ;; Time stamps without a repeater are returned unchanged.
  284. (should
  285. (equal
  286. '(3 29 2012)
  287. (calendar-gregorian-from-absolute
  288. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  289. ;; Time stamps with a null repeater are returned unchanged.
  290. (should
  291. (equal
  292. '(3 29 2012)
  293. (calendar-gregorian-from-absolute
  294. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  295. ;; if PREFER is set to `past' always return a date before, or equal
  296. ;; to CURRENT.
  297. (should
  298. (equal
  299. '(3 1 2014)
  300. (calendar-gregorian-from-absolute
  301. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  302. (should
  303. (equal
  304. '(3 4 2014)
  305. (calendar-gregorian-from-absolute
  306. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  307. ;; if PREFER is set to `future' always return a date before, or equal
  308. ;; to CURRENT.
  309. (should
  310. (equal
  311. '(3 29 2014)
  312. (calendar-gregorian-from-absolute
  313. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  314. (should
  315. (equal
  316. '(3 4 2014)
  317. (calendar-gregorian-from-absolute
  318. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  319. ;; If PREFER is neither `past' nor `future', select closest date.
  320. (should
  321. (equal
  322. '(3 1 2014)
  323. (calendar-gregorian-from-absolute
  324. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  325. (should
  326. (equal
  327. '(5 4 2014)
  328. (calendar-gregorian-from-absolute
  329. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  330. ;; Test "day" repeater.
  331. (should
  332. (equal '(3 8 2014)
  333. (calendar-gregorian-from-absolute
  334. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  335. (should
  336. (equal '(3 10 2014)
  337. (calendar-gregorian-from-absolute
  338. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  339. ;; Test "month" repeater.
  340. (should
  341. (equal '(1 5 2015)
  342. (calendar-gregorian-from-absolute
  343. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  344. (should
  345. (equal '(3 29 2014)
  346. (calendar-gregorian-from-absolute
  347. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  348. ;; Test "year" repeater.
  349. (should
  350. (equal '(3 5 2014)
  351. (calendar-gregorian-from-absolute
  352. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  353. (should
  354. (equal '(3 29 2014)
  355. (calendar-gregorian-from-absolute
  356. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  357. (ert-deftest test-org/deadline-close-p ()
  358. "Test `org-deadline-close-p' specifications."
  359. ;; Pretend that the current time is 2016-06-03 Fri 01:43
  360. (cl-letf (((symbol-function 'current-time)
  361. (lambda () '(22353 6425 905205 644000))))
  362. ;; Timestamps are close if they are within `ndays' of lead time.
  363. (org-test-with-temp-text "* Heading"
  364. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  365. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  366. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  367. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  368. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  369. ;; Read `ndays' from timestamp if argument not given.
  370. (org-test-with-temp-text "* H"
  371. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  372. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  373. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  374. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  375. ;; Prefer `ndays' argument over lead time in timestamp.
  376. (org-test-with-temp-text "* H"
  377. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  378. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  379. ;; Completed tasks are never close.
  380. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  381. (org-test-with-temp-text "* TODO Heading"
  382. (should (org-deadline-close-p "2016-06-03")))
  383. (org-test-with-temp-text "* DONE Heading"
  384. (should-not (org-deadline-close-p "2016-06-03"))))))
  385. ;;; Drawers
  386. (ert-deftest test-org/insert-property-drawer ()
  387. "Test `org-insert-property-drawer' specifications."
  388. ;; Error before first headline.
  389. (should-error (org-test-with-temp-text "" (org-insert-property-drawer)))
  390. ;; Insert drawer right after headline if there is no planning line,
  391. ;; or after it otherwise.
  392. (should
  393. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  394. (org-test-with-temp-text "* H\nParagraph<point>"
  395. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  396. (buffer-string))))
  397. (should
  398. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  399. (org-test-with-temp-text
  400. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  401. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  402. (buffer-string))))
  403. ;; Indent inserted drawer.
  404. (should
  405. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  406. (org-test-with-temp-text "* H\nParagraph<point>"
  407. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  408. (buffer-string))))
  409. ;; Handle insertion at eob.
  410. (should
  411. (equal "* H\n:PROPERTIES:\n:END:\n"
  412. (org-test-with-temp-text "* H"
  413. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  414. (buffer-string))))
  415. ;; Skip inlinetasks before point.
  416. (when (featurep 'org-inlinetask)
  417. (should
  418. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  419. (org-test-with-temp-text
  420. "* H\n*************** I\n*************** END\nP<point>"
  421. (let ((org-adapt-indentation nil)
  422. (org-inlinetask-min-level 15))
  423. (org-insert-property-drawer))
  424. (buffer-string)))))
  425. ;; Correctly set drawer in an inlinetask.
  426. (when (featurep 'org-inlinetask)
  427. (should
  428. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  429. (org-test-with-temp-text
  430. "* H\n*************** I\nP<point>\n*************** END"
  431. (let ((org-adapt-indentation nil)
  432. (org-inlinetask-min-level 15))
  433. (org-insert-property-drawer))
  434. (buffer-string))))))
  435. ;;; Filling
  436. (ert-deftest test-org/fill-paragraph ()
  437. "Test `org-fill-paragraph' specifications."
  438. ;; At an Org table, align it.
  439. (should
  440. (equal "| a |\n"
  441. (org-test-with-temp-text "|a|"
  442. (org-fill-paragraph)
  443. (buffer-string))))
  444. (should
  445. (equal "#+name: table\n| a |\n"
  446. (org-test-with-temp-text "#+name: table\n| a |\n"
  447. (org-fill-paragraph)
  448. (buffer-string))))
  449. ;; At a paragraph, preserve line breaks.
  450. (org-test-with-temp-text "some \\\\\nlong\ntext"
  451. (let ((fill-column 20))
  452. (org-fill-paragraph)
  453. (should (equal (buffer-string) "some \\\\\nlong text"))))
  454. ;; Correctly fill a paragraph when point is at its very end.
  455. (should
  456. (equal "A B"
  457. (org-test-with-temp-text "A\nB"
  458. (let ((fill-column 20))
  459. (goto-char (point-max))
  460. (org-fill-paragraph)
  461. (buffer-string)))))
  462. ;; Correctly fill the last paragraph of a greater element.
  463. (should
  464. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  465. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  466. (let ((fill-column 8))
  467. (forward-line)
  468. (end-of-line)
  469. (org-fill-paragraph)
  470. (buffer-string)))))
  471. ;; Correctly fill an element in a narrowed buffer.
  472. (should
  473. (equal "01234\n6"
  474. (org-test-with-temp-text "01234 6789"
  475. (let ((fill-column 5))
  476. (narrow-to-region 1 8)
  477. (org-fill-paragraph)
  478. (buffer-string)))))
  479. ;; Handle `adaptive-fill-regexp' in paragraphs.
  480. (should
  481. (equal "> a b"
  482. (org-test-with-temp-text "> a\n> b"
  483. (let ((fill-column 5)
  484. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  485. (org-fill-paragraph)
  486. (buffer-string)))))
  487. ;; Special case: Fill first paragraph when point is at an item or
  488. ;; a plain-list or a footnote reference.
  489. (should
  490. (equal "- A B"
  491. (org-test-with-temp-text "- A\n B"
  492. (let ((fill-column 20))
  493. (org-fill-paragraph)
  494. (buffer-string)))))
  495. (should
  496. (equal "[fn:1] A B"
  497. (org-test-with-temp-text "[fn:1] A\nB"
  498. (let ((fill-column 20))
  499. (org-fill-paragraph)
  500. (buffer-string)))))
  501. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  502. (let ((fill-column 20))
  503. (org-fill-paragraph)
  504. (should (equal (buffer-string)
  505. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  506. ;; Fill contents of `comment-block' elements.
  507. (should
  508. (equal
  509. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  510. (let ((fill-column 20))
  511. (forward-line)
  512. (org-fill-paragraph)
  513. (buffer-string)))
  514. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  515. ;; Fill `comment' elements.
  516. (should
  517. (equal " # A B"
  518. (org-test-with-temp-text " # A\n # B"
  519. (let ((fill-column 20))
  520. (org-fill-paragraph)
  521. (buffer-string)))))
  522. ;; Do not mix consecutive comments when filling one of them.
  523. (should
  524. (equal "# A B\n\n# C"
  525. (org-test-with-temp-text "# A\n# B\n\n# C"
  526. (let ((fill-column 20))
  527. (org-fill-paragraph)
  528. (buffer-string)))))
  529. ;; Use commented empty lines as separators when filling comments.
  530. (should
  531. (equal "# A B\n#\n# C"
  532. (org-test-with-temp-text "# A\n# B\n#\n# C"
  533. (let ((fill-column 20))
  534. (org-fill-paragraph)
  535. (buffer-string)))))
  536. ;; Handle `adaptive-fill-regexp' in comments.
  537. (should
  538. (equal "# > a b"
  539. (org-test-with-temp-text "# > a\n# > b"
  540. (let ((fill-column 20)
  541. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  542. (org-fill-paragraph)
  543. (buffer-string)))))
  544. ;; Do nothing at affiliated keywords.
  545. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  546. (let ((fill-column 20))
  547. (org-fill-paragraph)
  548. (should (equal (buffer-string) "#+NAME: para\nSome\ntext."))))
  549. ;; Do not move point after table when filling a table.
  550. (should-not
  551. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  552. (forward-char)
  553. (org-fill-paragraph)
  554. (eobp))))
  555. (ert-deftest test-org/auto-fill-function ()
  556. "Test auto-filling features."
  557. ;; Auto fill paragraph.
  558. (should
  559. (equal "12345\n7890"
  560. (org-test-with-temp-text "12345 7890"
  561. (let ((fill-column 5))
  562. (end-of-line)
  563. (org-auto-fill-function)
  564. (buffer-string)))))
  565. ;; Auto fill first paragraph in an item.
  566. (should
  567. (equal "- 12345\n 7890"
  568. (org-test-with-temp-text "- 12345 7890"
  569. (let ((fill-column 7))
  570. (end-of-line)
  571. (org-auto-fill-function)
  572. (buffer-string)))))
  573. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  574. (should
  575. (equal "> 12345\n 7890"
  576. (org-test-with-temp-text "> 12345 7890"
  577. (let ((fill-column 10)
  578. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  579. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  580. (end-of-line)
  581. (org-auto-fill-function)
  582. (buffer-string)))))
  583. (should
  584. (equal "> 12345\n> 12345\n> 7890"
  585. (org-test-with-temp-text "> 12345\n> 12345 7890"
  586. (let ((fill-column 10)
  587. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  588. (goto-char (point-max))
  589. (org-auto-fill-function)
  590. (buffer-string)))))
  591. (should-not
  592. (equal " 12345\n *12345\n *12345"
  593. (org-test-with-temp-text " 12345\n *12345 12345"
  594. (let ((fill-column 10)
  595. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  596. (goto-char (point-max))
  597. (org-auto-fill-function)
  598. (buffer-string)))))
  599. ;; Auto fill comments.
  600. (should
  601. (equal " # 12345\n # 7890"
  602. (org-test-with-temp-text " # 12345 7890"
  603. (let ((fill-column 10))
  604. (end-of-line)
  605. (org-auto-fill-function)
  606. (buffer-string)))))
  607. ;; A hash within a line isn't a comment.
  608. (should-not
  609. (equal "12345 # 7890\n# 1"
  610. (org-test-with-temp-text "12345 # 7890 1"
  611. (let ((fill-column 12))
  612. (end-of-line)
  613. (org-auto-fill-function)
  614. (buffer-string)))))
  615. ;; Correctly interpret empty prefix.
  616. (should-not
  617. (equal "# a\n# b\nRegular\n# paragraph"
  618. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  619. (let ((fill-column 12))
  620. (end-of-line 3)
  621. (org-auto-fill-function)
  622. (buffer-string)))))
  623. ;; Comment block: auto fill contents.
  624. (should
  625. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  626. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  627. (let ((fill-column 5))
  628. (forward-line)
  629. (end-of-line)
  630. (org-auto-fill-function)
  631. (buffer-string)))))
  632. (should
  633. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  634. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  635. (let ((fill-column 5))
  636. (forward-line)
  637. (end-of-line)
  638. (org-auto-fill-function)
  639. (buffer-string)))))
  640. ;; Do not fill if a new item could be created.
  641. (should-not
  642. (equal "12345\n- 90"
  643. (org-test-with-temp-text "12345 - 90"
  644. (let ((fill-column 5))
  645. (end-of-line)
  646. (org-auto-fill-function)
  647. (buffer-string)))))
  648. ;; Do not fill if a line break could be introduced.
  649. (should-not
  650. (equal "123\\\\\n7890"
  651. (org-test-with-temp-text "123\\\\ 7890"
  652. (let ((fill-column 6))
  653. (end-of-line)
  654. (org-auto-fill-function)
  655. (buffer-string)))))
  656. ;; Do not fill affiliated keywords.
  657. (should-not
  658. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  659. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  660. (let ((fill-column 20))
  661. (end-of-line)
  662. (org-auto-fill-function)
  663. (buffer-string))))))
  664. ;;; Indentation
  665. (ert-deftest test-org/indent-line ()
  666. "Test `org-indent-line' specifications."
  667. ;; Do not indent diary sexps, footnote definitions or headlines.
  668. (should
  669. (zerop
  670. (org-test-with-temp-text "%%(org-calendar-holiday)"
  671. (org-indent-line)
  672. (org-get-indentation))))
  673. (should
  674. (zerop
  675. (org-test-with-temp-text "[fn:1] fn"
  676. (let ((org-adapt-indentation t)) (org-indent-line))
  677. (org-get-indentation))))
  678. (should
  679. (zerop
  680. (org-test-with-temp-text "* H"
  681. (org-indent-line)
  682. (org-get-indentation))))
  683. ;; Do not indent before first headline.
  684. (should
  685. (zerop
  686. (org-test-with-temp-text ""
  687. (org-indent-line)
  688. (org-get-indentation))))
  689. ;; Indent according to headline level otherwise, unless
  690. ;; `org-adapt-indentation' is nil.
  691. (should
  692. (= 2
  693. (org-test-with-temp-text "* H\n<point>A"
  694. (let ((org-adapt-indentation t)) (org-indent-line))
  695. (org-get-indentation))))
  696. (should
  697. (= 2
  698. (org-test-with-temp-text "* H\n<point>\nA"
  699. (let ((org-adapt-indentation t)) (org-indent-line))
  700. (org-get-indentation))))
  701. (should
  702. (zerop
  703. (org-test-with-temp-text "* H\n<point>A"
  704. (let ((org-adapt-indentation nil)) (org-indent-line))
  705. (org-get-indentation))))
  706. ;; Indenting preserves point position.
  707. (should
  708. (org-test-with-temp-text "* H\nA<point>B"
  709. (let ((org-adapt-indentation t)) (org-indent-line))
  710. (looking-at "B")))
  711. ;; Do not change indentation at an item or a LaTeX environment.
  712. (should
  713. (= 1
  714. (org-test-with-temp-text "* H\n<point> - A"
  715. (let ((org-adapt-indentation t)) (org-indent-line))
  716. (org-get-indentation))))
  717. (should
  718. (= 1
  719. (org-test-with-temp-text
  720. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  721. (org-indent-line)
  722. (org-get-indentation))))
  723. ;; On blank lines at the end of a list, indent like last element
  724. ;; within it if the line is still in the list. If the last element
  725. ;; is an item, indent like its contents. Otherwise, indent like the
  726. ;; whole list.
  727. (should
  728. (= 4
  729. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  730. (let ((org-adapt-indentation t)) (org-indent-line))
  731. (org-get-indentation))))
  732. (should
  733. (= 4
  734. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  735. (let ((org-adapt-indentation t)) (org-indent-line))
  736. (org-get-indentation))))
  737. (should
  738. (zerop
  739. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  740. (let ((org-adapt-indentation t)) (org-indent-line))
  741. (org-get-indentation))))
  742. (should
  743. (= 4
  744. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  745. (let ((org-adapt-indentation t)) (org-indent-line))
  746. (org-get-indentation))))
  747. (should
  748. (= 4
  749. (org-test-with-temp-text
  750. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  751. (let ((org-adapt-indentation t)) (org-indent-line))
  752. (org-get-indentation))))
  753. (should
  754. (= 2
  755. (org-test-with-temp-text "- A\n B\n\n<point>"
  756. (let ((org-adapt-indentation nil)) (org-indent-line))
  757. (org-get-indentation))))
  758. (should
  759. (= 2
  760. (org-test-with-temp-text
  761. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  762. (let ((org-adapt-indentation nil)) (org-indent-line))
  763. (org-get-indentation))))
  764. ;; Likewise, on a blank line at the end of a footnote definition,
  765. ;; indent at column 0 if line belongs to the definition. Otherwise,
  766. ;; indent like the definition itself.
  767. (should
  768. (zerop
  769. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  770. (let ((org-adapt-indentation t)) (org-indent-line))
  771. (org-get-indentation))))
  772. (should
  773. (zerop
  774. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  775. (let ((org-adapt-indentation t)) (org-indent-line))
  776. (org-get-indentation))))
  777. ;; After the end of the contents of a greater element, indent like
  778. ;; the beginning of the element.
  779. (should
  780. (= 1
  781. (org-test-with-temp-text
  782. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  783. (org-indent-line)
  784. (org-get-indentation))))
  785. ;; On blank lines after a paragraph, indent like its last non-empty
  786. ;; line.
  787. (should
  788. (= 1
  789. (org-test-with-temp-text " Paragraph\n\n<point>"
  790. (org-indent-line)
  791. (org-get-indentation))))
  792. ;; At the first line of an element, indent like previous element's
  793. ;; first line, ignoring footnotes definitions and inline tasks, or
  794. ;; according to parent.
  795. (should
  796. (= 2
  797. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  798. (org-indent-line)
  799. (org-get-indentation))))
  800. (should
  801. (= 1
  802. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  803. (org-indent-line)
  804. (org-get-indentation))))
  805. (should
  806. (= 1
  807. (org-test-with-temp-text
  808. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  809. (org-indent-line)
  810. (org-get-indentation))))
  811. ;; Within code part of a source block, use language major mode if
  812. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  813. ;; according to line above.
  814. (should
  815. (= 6
  816. (org-test-with-temp-text
  817. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  818. (let ((org-src-tab-acts-natively t)
  819. (org-edit-src-content-indentation 0))
  820. (org-indent-line))
  821. (org-get-indentation))))
  822. (should
  823. (= 1
  824. (org-test-with-temp-text
  825. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  826. (let ((org-src-tab-acts-natively nil)
  827. (org-edit-src-content-indentation 0))
  828. (org-indent-line))
  829. (org-get-indentation))))
  830. ;; Otherwise, indent like the first non-blank line above.
  831. (should
  832. (zerop
  833. (org-test-with-temp-text
  834. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  835. (org-indent-line)
  836. (org-get-indentation))))
  837. ;; Align node properties according to `org-property-format'. Handle
  838. ;; nicely empty values.
  839. (should
  840. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  841. (org-test-with-temp-text
  842. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  843. (let ((org-property-format "%-10s %s")) (org-indent-line))
  844. (buffer-string))))
  845. (should
  846. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  847. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  848. (let ((org-property-format "%-10s %s")) (org-indent-line))
  849. (buffer-string)))))
  850. (ert-deftest test-org/indent-region ()
  851. "Test `org-indent-region' specifications."
  852. ;; Indent paragraph.
  853. (should
  854. (equal "A\nB\nC"
  855. (org-test-with-temp-text " A\nB\n C"
  856. (org-indent-region (point-min) (point-max))
  857. (buffer-string))))
  858. ;; Indent greater elements along with their contents.
  859. (should
  860. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  861. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  862. (org-indent-region (point-min) (point-max))
  863. (buffer-string))))
  864. ;; Ignore contents of verse blocks. Only indent block delimiters.
  865. (should
  866. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  867. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  868. (org-indent-region (point-min) (point-max))
  869. (buffer-string))))
  870. (should
  871. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  872. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  873. (org-indent-region (point-min) (point-max))
  874. (buffer-string))))
  875. ;; Indent example blocks as a single block, unless indentation
  876. ;; should be preserved. In this case only indent the block markers.
  877. (should
  878. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  879. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  880. (org-indent-region (point-min) (point-max))
  881. (buffer-string))))
  882. (should
  883. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  884. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  885. (org-indent-region (point-min) (point-max))
  886. (buffer-string))))
  887. (should
  888. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  889. (org-test-with-temp-text
  890. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  891. (org-indent-region (point-min) (point-max))
  892. (buffer-string))))
  893. (should
  894. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  895. (org-test-with-temp-text
  896. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  897. (let ((org-src-preserve-indentation t))
  898. (org-indent-region (point-min) (point-max)))
  899. (buffer-string))))
  900. ;; Treat export blocks as a whole.
  901. (should
  902. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  903. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  904. (org-indent-region (point-min) (point-max))
  905. (buffer-string))))
  906. (should
  907. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  908. (org-test-with-temp-text
  909. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  910. (org-indent-region (point-min) (point-max))
  911. (buffer-string))))
  912. ;; Indent according to mode if `org-src-tab-acts-natively' is
  913. ;; non-nil. Otherwise, do not indent code at all.
  914. (should
  915. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  916. (org-test-with-temp-text
  917. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  918. (let ((org-src-tab-acts-natively t)
  919. (org-edit-src-content-indentation 0))
  920. (org-indent-region (point-min) (point-max)))
  921. (buffer-string))))
  922. (should
  923. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  924. (org-test-with-temp-text
  925. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  926. (let ((org-src-tab-acts-natively nil)
  927. (org-edit-src-content-indentation 0))
  928. (org-indent-region (point-min) (point-max)))
  929. (buffer-string))))
  930. ;; Align node properties according to `org-property-format'. Handle
  931. ;; nicely empty values.
  932. (should
  933. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  934. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  935. (let ((org-property-format "%-10s %s")
  936. (org-adapt-indentation nil))
  937. (org-indent-region (point) (point-max)))
  938. (buffer-string))))
  939. (should
  940. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  941. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  942. (let ((org-property-format "%-10s %s")
  943. (org-adapt-indentation nil))
  944. (org-indent-region (point) (point-max)))
  945. (buffer-string))))
  946. ;; Indent plain lists.
  947. (should
  948. (equal "- A\n B\n - C\n\n D"
  949. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  950. (org-indent-region (point-min) (point-max))
  951. (buffer-string))))
  952. (should
  953. (equal "- A\n\n- B"
  954. (org-test-with-temp-text " - A\n\n - B"
  955. (org-indent-region (point-min) (point-max))
  956. (buffer-string))))
  957. ;; Indent footnote definitions.
  958. (should
  959. (equal "[fn:1] Definition\n\nDefinition"
  960. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  961. (org-indent-region (point-min) (point-max))
  962. (buffer-string))))
  963. ;; Special case: Start indenting on a blank line.
  964. (should
  965. (equal "\nParagraph"
  966. (org-test-with-temp-text "\n Paragraph"
  967. (org-indent-region (point-min) (point-max))
  968. (buffer-string)))))
  969. ;;; Editing
  970. (ert-deftest test-org/delete-indentation ()
  971. "Test `org-delete-indentation' specifications."
  972. ;; Regular test.
  973. (should (equal "foo bar"
  974. (org-test-with-temp-text
  975. "foo \n bar<point>"
  976. (org-delete-indentation)
  977. (buffer-string))))
  978. ;; With optional argument.
  979. (should (equal "foo bar"
  980. (org-test-with-temp-text
  981. "foo<point> \n bar"
  982. (org-delete-indentation t)
  983. (buffer-string))))
  984. ;; At headline text should be appended to the headline text.
  985. (should
  986. (equal"* foo bar :tag:"
  987. (let (org-auto-align-tags)
  988. (org-test-with-temp-text
  989. "* foo :tag:\n bar<point>"
  990. (org-delete-indentation)
  991. (buffer-string)))))
  992. (should
  993. (equal "* foo bar :tag:"
  994. (let (org-auto-align-tags)
  995. (org-test-with-temp-text
  996. "* foo <point>:tag:\n bar"
  997. (org-delete-indentation t)
  998. (buffer-string))))))
  999. (ert-deftest test-org/return ()
  1000. "Test `org-return' specifications."
  1001. ;; Regular test.
  1002. (should
  1003. (equal "Para\ngraph"
  1004. (org-test-with-temp-text "Para<point>graph"
  1005. (org-return)
  1006. (buffer-string))))
  1007. ;; With optional argument, indent line.
  1008. (should
  1009. (equal " Para\n graph"
  1010. (org-test-with-temp-text " Para<point>graph"
  1011. (org-return t)
  1012. (buffer-string))))
  1013. ;; On a table, call `org-table-next-row'.
  1014. (should
  1015. (org-test-with-temp-text "| <point>a |\n| b |"
  1016. (org-return)
  1017. (looking-at-p "b")))
  1018. ;; Open link or timestamp under point when `org-return-follows-link'
  1019. ;; is non-nil.
  1020. (should
  1021. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1022. (let ((org-return-follows-link t)
  1023. (org-link-search-must-match-exact-headline nil))
  1024. (org-return))
  1025. (looking-at-p "<<target>>")))
  1026. (should-not
  1027. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1028. (let ((org-return-follows-link nil)) (org-return))
  1029. (looking-at-p "<<target>>")))
  1030. (should
  1031. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1032. (let ((org-return-follows-link t)) (org-return))
  1033. (looking-at-p "* b")))
  1034. (should
  1035. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1036. (let ((org-return-follows-link t)
  1037. (org-link-search-must-match-exact-headline nil))
  1038. (org-return))
  1039. (looking-at-p "<<target>>")))
  1040. (should-not
  1041. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1042. (let ((org-return-follows-link t)
  1043. (org-link-search-must-match-exact-headline nil))
  1044. (org-return))
  1045. (looking-at-p "<<target>>")))
  1046. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1047. ;; timestamps in comments, node properties, etc.
  1048. (should
  1049. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1050. (let ((org-return-follows-link t)
  1051. (org-link-search-must-match-exact-headline nil))
  1052. (org-return))
  1053. (looking-at-p "<<target>>")))
  1054. (should-not
  1055. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1056. (let ((org-return-follows-link nil)) (org-return))
  1057. (looking-at-p "<<target>>")))
  1058. (should-not
  1059. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1060. (let ((org-return-follows-link t)
  1061. (org-link-search-must-match-exact-headline nil))
  1062. (org-return))
  1063. (looking-at-p "<<target>>")))
  1064. ;; However, do not open link when point is in a table.
  1065. (should
  1066. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1067. (let ((org-return-follows-link t)) (org-return))
  1068. (looking-at-p "between")))
  1069. ;; Special case: in a list, when indenting, do not break structure.
  1070. (should
  1071. (equal "- A\n B"
  1072. (org-test-with-temp-text "- A <point>B"
  1073. (org-return t)
  1074. (buffer-string))))
  1075. (should
  1076. (equal "- A\n\n- B"
  1077. (org-test-with-temp-text "- A\n<point>- B"
  1078. (org-return t)
  1079. (buffer-string))))
  1080. ;; On tags part of a headline, add a newline below it instead of
  1081. ;; breaking it.
  1082. (should
  1083. (equal "* H :tag:\n"
  1084. (org-test-with-temp-text "* H :<point>tag:"
  1085. (org-return)
  1086. (buffer-string))))
  1087. ;; Before headline text, add a newline below it instead of breaking
  1088. ;; it.
  1089. (should
  1090. (equal "* TODO H :tag:\n"
  1091. (org-test-with-temp-text "* <point>TODO H :tag:"
  1092. (org-return)
  1093. (buffer-string))))
  1094. (should
  1095. (equal "* TODO [#B] H :tag:\n"
  1096. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1097. (org-return)
  1098. (buffer-string))))
  1099. (should ;TODO are case-sensitive
  1100. (equal "* \nTodo"
  1101. (org-test-with-temp-text "* <point>Todo"
  1102. (org-return)
  1103. (buffer-string))))
  1104. ;; At headline text, break headline text but preserve tags.
  1105. (should
  1106. (equal "* TODO [#B] foo :tag:\nbar"
  1107. (let (org-auto-align-tags)
  1108. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1109. (org-return)
  1110. (buffer-string)))))
  1111. ;; At bol of headline insert newline.
  1112. (should
  1113. (equal "\n* h"
  1114. (org-test-with-temp-text "<point>* h"
  1115. (org-return)
  1116. (buffer-string))))
  1117. ;; Refuse to leave invalid headline in buffer.
  1118. (should
  1119. (equal "* h\n"
  1120. (org-test-with-temp-text "*<point> h"
  1121. (org-return)
  1122. (buffer-string)))))
  1123. (ert-deftest test-org/meta-return ()
  1124. "Test M-RET (`org-meta-return') specifications."
  1125. ;; In a table field insert a row above.
  1126. (should
  1127. (org-test-with-temp-text "| a |"
  1128. (forward-char)
  1129. (org-meta-return)
  1130. (forward-line -1)
  1131. (looking-at "| |$")))
  1132. ;; In a paragraph change current line into a header.
  1133. (should
  1134. (org-test-with-temp-text "a"
  1135. (org-meta-return)
  1136. (beginning-of-line)
  1137. (looking-at "\* a$")))
  1138. ;; In an item insert an item, in this case above.
  1139. (should
  1140. (org-test-with-temp-text "- a"
  1141. (org-meta-return)
  1142. (beginning-of-line)
  1143. (looking-at "- $")))
  1144. ;; In a drawer and item insert an item, in this case above.
  1145. (should
  1146. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1147. (forward-line)
  1148. (org-meta-return)
  1149. (beginning-of-line)
  1150. (looking-at "- $"))))
  1151. (ert-deftest test-org/insert-heading ()
  1152. "Test `org-insert-heading' specifications."
  1153. ;; In an empty buffer, insert a new headline.
  1154. (should
  1155. (equal "* "
  1156. (org-test-with-temp-text ""
  1157. (org-insert-heading)
  1158. (buffer-string))))
  1159. ;; At the beginning of a line, turn it into a headline
  1160. (should
  1161. (equal "* P"
  1162. (org-test-with-temp-text "<point>P"
  1163. (org-insert-heading)
  1164. (buffer-string))))
  1165. ;; In the middle of a line, split the line if allowed, otherwise,
  1166. ;; insert the headline at its end.
  1167. (should
  1168. (equal "Para\n* graph"
  1169. (org-test-with-temp-text "Para<point>graph"
  1170. (let ((org-M-RET-may-split-line '((default . t))))
  1171. (org-insert-heading))
  1172. (buffer-string))))
  1173. (should
  1174. (equal "Paragraph\n* "
  1175. (org-test-with-temp-text "Para<point>graph"
  1176. (let ((org-M-RET-may-split-line '((default . nil))))
  1177. (org-insert-heading))
  1178. (buffer-string))))
  1179. ;; At the beginning of a headline, create one above.
  1180. (should
  1181. (equal "* \n* H"
  1182. (org-test-with-temp-text "* H"
  1183. (org-insert-heading)
  1184. (buffer-string))))
  1185. ;; In the middle of a headline, split it if allowed.
  1186. (should
  1187. (equal "* H\n* 1"
  1188. (org-test-with-temp-text "* H<point>1"
  1189. (let ((org-M-RET-may-split-line '((headline . t))))
  1190. (org-insert-heading))
  1191. (buffer-string))))
  1192. (should
  1193. (equal "* H1\n* "
  1194. (org-test-with-temp-text "* H<point>1"
  1195. (let ((org-M-RET-may-split-line '((headline . nil))))
  1196. (org-insert-heading))
  1197. (buffer-string))))
  1198. ;; However, splitting cannot happen on TODO keywords, priorities or
  1199. ;; tags.
  1200. (should
  1201. (equal "* TODO H1\n* "
  1202. (org-test-with-temp-text "* TO<point>DO H1"
  1203. (let ((org-M-RET-may-split-line '((headline . t))))
  1204. (org-insert-heading))
  1205. (buffer-string))))
  1206. (should
  1207. (equal "* [#A] H1\n* "
  1208. (org-test-with-temp-text "* [#<point>A] H1"
  1209. (let ((org-M-RET-may-split-line '((headline . t))))
  1210. (org-insert-heading))
  1211. (buffer-string))))
  1212. (should
  1213. (equal "* H1 :tag:\n* "
  1214. (org-test-with-temp-text "* H1 :ta<point>g:"
  1215. (let ((org-M-RET-may-split-line '((headline . t))))
  1216. (org-insert-heading))
  1217. (buffer-string))))
  1218. ;; When on a list, insert an item instead, unless called with an
  1219. ;; universal argument or if list is invisible. In this case, create
  1220. ;; a new headline after contents.
  1221. (should
  1222. (equal "* H\n- item\n- "
  1223. (org-test-with-temp-text "* H\n- item<point>"
  1224. (let ((org-insert-heading-respect-content nil))
  1225. (org-insert-heading))
  1226. (buffer-string))))
  1227. (should
  1228. (equal "* H\n- item\n- item 2\n* "
  1229. (org-test-with-temp-text "* H\n- item<point>\n- item 2"
  1230. (let ((org-insert-heading-respect-content nil))
  1231. (org-insert-heading '(4)))
  1232. (buffer-string))))
  1233. (should
  1234. (equal "* H\n- item\n* "
  1235. (org-test-with-temp-text "* H\n- item"
  1236. (org-cycle)
  1237. (goto-char (point-max))
  1238. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1239. (buffer-string))))
  1240. ;; Preserve list visibility when inserting an item.
  1241. (should
  1242. (equal
  1243. '(outline outline)
  1244. (org-test-with-temp-text "- A\n - B\n- C\n - D"
  1245. (let ((org-cycle-include-plain-lists t))
  1246. (org-cycle)
  1247. (forward-line 2)
  1248. (org-cycle)
  1249. (let ((org-insert-heading-respect-content nil)) (org-insert-heading))
  1250. (list (get-char-property (line-beginning-position 0) 'invisible)
  1251. (get-char-property (line-end-position 2) 'invisible))))))
  1252. ;; When called with one universal argument, insert a new headline at
  1253. ;; the end of the current subtree, independently on the position of
  1254. ;; point.
  1255. (should
  1256. (equal
  1257. "* H1\n** H2\n* "
  1258. (org-test-with-temp-text "* H1\n** H2"
  1259. (let ((org-insert-heading-respect-content nil))
  1260. (org-insert-heading '(4)))
  1261. (buffer-string))))
  1262. (should
  1263. (equal
  1264. "* H1\n** H2\n* "
  1265. (org-test-with-temp-text "* H<point>1\n** H2"
  1266. (let ((org-insert-heading-respect-content nil))
  1267. (org-insert-heading '(4)))
  1268. (buffer-string))))
  1269. ;; When called with two universal arguments, insert a new headline
  1270. ;; at the end of the grandparent subtree.
  1271. (should
  1272. (equal "* H1\n** H3\n- item\n** H2\n** "
  1273. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1274. (let ((org-insert-heading-respect-content nil))
  1275. (org-insert-heading '(16)))
  1276. (buffer-string))))
  1277. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1278. ;; a level 1 heading.
  1279. (should
  1280. (equal "* H1\n** H2\n* "
  1281. (org-test-with-temp-text "* H1\n** H2<point>"
  1282. (org-insert-heading nil nil t)
  1283. (buffer-string))))
  1284. (should
  1285. (equal "* H1\n- item\n* "
  1286. (org-test-with-temp-text "* H1\n- item<point>"
  1287. (org-insert-heading nil nil t)
  1288. (buffer-string))))
  1289. ;; Corner case: correctly insert a headline after an empty one.
  1290. (should
  1291. (equal "* \n* "
  1292. (org-test-with-temp-text "* <point>"
  1293. (org-insert-heading)
  1294. (buffer-string)))))
  1295. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1296. "Test `org-insert-todo-heading-respect-content' specifications."
  1297. ;; Create a TODO heading.
  1298. (should
  1299. (org-test-with-temp-text "* H1\n Body"
  1300. (org-insert-todo-heading-respect-content)
  1301. (nth 2 (org-heading-components))))
  1302. ;; Add headline at the end of the first subtree
  1303. (should
  1304. (org-test-with-temp-text "* H1\nH1Body\n** H2\nH2Body"
  1305. (search-forward "H1Body")
  1306. (org-insert-todo-heading-respect-content)
  1307. (and (eobp) (org-at-heading-p))))
  1308. ;; In a list, do not create a new item.
  1309. (should
  1310. (org-test-with-temp-text "* H\n- an item\n- another one"
  1311. (search-forward "an ")
  1312. (org-insert-todo-heading-respect-content)
  1313. (and (eobp) (org-at-heading-p)))))
  1314. (ert-deftest test-org/clone-with-time-shift ()
  1315. "Test `org-clone-subtree-with-time-shift'."
  1316. ;; Raise an error before first heading.
  1317. (should-error
  1318. (org-test-with-temp-text ""
  1319. (org-clone-subtree-with-time-shift 1)))
  1320. ;; Raise an error on invalid number of clones.
  1321. (should-error
  1322. (org-test-with-temp-text "* Clone me"
  1323. (org-clone-subtree-with-time-shift -1)))
  1324. ;; Clone non-repeating once.
  1325. (should
  1326. (equal "\
  1327. * H1\n<2015-06-21>
  1328. * H1\n<2015-06-23>
  1329. "
  1330. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1331. (org-clone-subtree-with-time-shift 1 "+2d")
  1332. (replace-regexp-in-string
  1333. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1334. nil nil 1))))
  1335. ;; Clone repeating once.
  1336. (should
  1337. (equal "\
  1338. * H1\n<2015-06-21>
  1339. * H1\n<2015-06-23>
  1340. * H1\n<2015-06-25 +1w>
  1341. "
  1342. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1343. (org-clone-subtree-with-time-shift 1 "+2d")
  1344. (replace-regexp-in-string
  1345. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1346. nil nil 1))))
  1347. ;; Clone non-repeating zero times.
  1348. (should
  1349. (equal "\
  1350. * H1\n<2015-06-21>
  1351. "
  1352. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1353. (org-clone-subtree-with-time-shift 0 "+2d")
  1354. (replace-regexp-in-string
  1355. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1356. nil nil 1))))
  1357. ;; Clone repeating "zero" times.
  1358. (should
  1359. (equal "\
  1360. * H1\n<2015-06-21>
  1361. * H1\n<2015-06-23 +1w>
  1362. "
  1363. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1364. (org-clone-subtree-with-time-shift 0 "+2d")
  1365. (replace-regexp-in-string
  1366. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1367. nil nil 1))))
  1368. ;; Clone with blank SHIFT argument.
  1369. (should
  1370. (string-prefix-p
  1371. "* H <2012-03-29"
  1372. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1373. (org-clone-subtree-with-time-shift 1 "")
  1374. (buffer-substring-no-properties (line-beginning-position 2)
  1375. (line-end-position 2)))))
  1376. ;; Find time stamps before point. If SHIFT is not specified, ask
  1377. ;; for a time shift.
  1378. (should
  1379. (string-prefix-p
  1380. "* H <2012-03-30"
  1381. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1382. (org-clone-subtree-with-time-shift 1 "+1d")
  1383. (buffer-substring-no-properties (line-beginning-position 2)
  1384. (line-end-position 2)))))
  1385. (should
  1386. (string-prefix-p
  1387. "* H <2014-03-05"
  1388. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1389. (cl-letf (((symbol-function 'read-from-minibuffer)
  1390. (lambda (&rest args) "+1d")))
  1391. (org-clone-subtree-with-time-shift 1))
  1392. (buffer-substring-no-properties (line-beginning-position 2)
  1393. (line-end-position 2))))))
  1394. ;;; Fixed-Width Areas
  1395. (ert-deftest test-org/toggle-fixed-width ()
  1396. "Test `org-toggle-fixed-width' specifications."
  1397. ;; No region: Toggle on fixed-width marker in paragraphs.
  1398. (should
  1399. (equal ": A"
  1400. (org-test-with-temp-text "A"
  1401. (org-toggle-fixed-width)
  1402. (buffer-string))))
  1403. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  1404. (should
  1405. (equal "A"
  1406. (org-test-with-temp-text ": A"
  1407. (org-toggle-fixed-width)
  1408. (buffer-string))))
  1409. ;; No region: Toggle on marker in blank lines after elements or just
  1410. ;; after a headline.
  1411. (should
  1412. (equal "* H\n: "
  1413. (org-test-with-temp-text "* H\n"
  1414. (forward-line)
  1415. (org-toggle-fixed-width)
  1416. (buffer-string))))
  1417. (should
  1418. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  1419. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  1420. (goto-char (point-max))
  1421. (org-toggle-fixed-width)
  1422. (buffer-string))))
  1423. ;; No region: Toggle on marker in front of one line elements (e.g.,
  1424. ;; headlines, clocks)
  1425. (should
  1426. (equal ": * Headline"
  1427. (org-test-with-temp-text "* Headline"
  1428. (org-toggle-fixed-width)
  1429. (buffer-string))))
  1430. (should
  1431. (equal ": #+KEYWORD: value"
  1432. (org-test-with-temp-text "#+KEYWORD: value"
  1433. (org-toggle-fixed-width)
  1434. (buffer-string))))
  1435. ;; No region: error in other situations.
  1436. (should-error
  1437. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  1438. (forward-line)
  1439. (org-toggle-fixed-width)
  1440. (buffer-string)))
  1441. ;; No region: Indentation is preserved.
  1442. (should
  1443. (equal "- A\n : B"
  1444. (org-test-with-temp-text "- A\n B"
  1445. (forward-line)
  1446. (org-toggle-fixed-width)
  1447. (buffer-string))))
  1448. ;; Region: If it contains only fixed-width elements and blank lines,
  1449. ;; toggle off fixed-width markup.
  1450. (should
  1451. (equal "A\n\nB"
  1452. (org-test-with-temp-text ": A\n\n: B"
  1453. (transient-mark-mode 1)
  1454. (push-mark (point) t t)
  1455. (goto-char (point-max))
  1456. (org-toggle-fixed-width)
  1457. (buffer-string))))
  1458. ;; Region: If it contains anything else, toggle on fixed-width but
  1459. ;; not on fixed-width areas.
  1460. (should
  1461. (equal ": A\n: \n: B\n: \n: C"
  1462. (org-test-with-temp-text "A\n\n: B\n\nC"
  1463. (transient-mark-mode 1)
  1464. (push-mark (point) t t)
  1465. (goto-char (point-max))
  1466. (org-toggle-fixed-width)
  1467. (buffer-string))))
  1468. ;; Region: Ignore blank lines at its end, unless it contains only
  1469. ;; such lines.
  1470. (should
  1471. (equal ": A\n\n"
  1472. (org-test-with-temp-text "A\n\n"
  1473. (transient-mark-mode 1)
  1474. (push-mark (point) t t)
  1475. (goto-char (point-max))
  1476. (org-toggle-fixed-width)
  1477. (buffer-string))))
  1478. (should
  1479. (equal ": \n: \n"
  1480. (org-test-with-temp-text "\n\n"
  1481. (transient-mark-mode 1)
  1482. (push-mark (point) t t)
  1483. (goto-char (point-max))
  1484. (org-toggle-fixed-width)
  1485. (buffer-string)))))
  1486. ;;; Headline
  1487. (ert-deftest test-org/get-heading ()
  1488. "Test `org-get-heading' specifications."
  1489. ;; Return current heading, even if point is not on it.
  1490. (should
  1491. (equal "H"
  1492. (org-test-with-temp-text "* H"
  1493. (org-get-heading))))
  1494. (should
  1495. (equal "H"
  1496. (org-test-with-temp-text "* H\nText<point>"
  1497. (org-get-heading))))
  1498. ;; Without any optional argument, return TODO keywords and tags.
  1499. (should
  1500. (equal "TODO H"
  1501. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1502. (org-get-heading))))
  1503. (should
  1504. (equal "H :tag:"
  1505. (org-test-with-temp-text "* H :tag:"
  1506. (org-get-heading))))
  1507. ;; With NO-TAGS argument, ignore tags.
  1508. (should
  1509. (equal "TODO H"
  1510. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1511. (org-get-heading t))))
  1512. (should
  1513. (equal "H"
  1514. (org-test-with-temp-text "* H :tag:"
  1515. (org-get-heading t))))
  1516. ;; With NO-TODO, ignore TODO keyword.
  1517. (should
  1518. (equal "H"
  1519. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  1520. (org-get-heading nil t))))
  1521. (should
  1522. (equal "H :tag:"
  1523. (org-test-with-temp-text "* H :tag:"
  1524. (org-get-heading nil t))))
  1525. ;; TODO keywords are case-sensitive.
  1526. (should
  1527. (equal "Todo H"
  1528. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  1529. (org-get-heading nil t))))
  1530. ;; On an empty headline, return value is consistent.
  1531. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  1532. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  1533. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  1534. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t t)))))
  1535. (ert-deftest test-org/in-commented-heading-p ()
  1536. "Test `org-in-commented-heading-p' specifications."
  1537. ;; Commented headline.
  1538. (should
  1539. (org-test-with-temp-text "* COMMENT Headline\nBody"
  1540. (goto-char (point-max))
  1541. (org-in-commented-heading-p)))
  1542. ;; Commented ancestor.
  1543. (should
  1544. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1545. (goto-char (point-max))
  1546. (org-in-commented-heading-p)))
  1547. ;; Comment keyword is case-sensitive.
  1548. (should-not
  1549. (org-test-with-temp-text "* Comment Headline\nBody"
  1550. (goto-char (point-max))
  1551. (org-in-commented-heading-p)))
  1552. ;; Keyword is standalone.
  1553. (should-not
  1554. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  1555. (goto-char (point-max))
  1556. (org-in-commented-heading-p)))
  1557. ;; Optional argument.
  1558. (should-not
  1559. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  1560. (goto-char (point-max))
  1561. (org-in-commented-heading-p t))))
  1562. (ert-deftest test-org/entry-blocked-p ()
  1563. ;; Check other dependencies.
  1564. (should
  1565. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  1566. (let ((org-enforce-todo-dependencies t)
  1567. (org-blocker-hook
  1568. '(org-block-todo-from-children-or-siblings-or-parent)))
  1569. (org-entry-blocked-p))))
  1570. (should-not
  1571. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  1572. (let ((org-enforce-todo-dependencies t)
  1573. (org-blocker-hook
  1574. '(org-block-todo-from-children-or-siblings-or-parent)))
  1575. (org-entry-blocked-p))))
  1576. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  1577. ;; blocked.
  1578. (should-not
  1579. (org-test-with-temp-text "* Blocked\n** TODO one"
  1580. (let ((org-enforce-todo-dependencies t)
  1581. (org-blocker-hook
  1582. '(org-block-todo-from-children-or-siblings-or-parent)))
  1583. (org-entry-blocked-p))))
  1584. (should-not
  1585. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  1586. (let ((org-enforce-todo-dependencies t)
  1587. (org-blocker-hook
  1588. '(org-block-todo-from-children-or-siblings-or-parent)))
  1589. (org-entry-blocked-p))))
  1590. ;; Follow :ORDERED: specifications.
  1591. (should
  1592. (org-test-with-temp-text
  1593. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  1594. (let ((org-enforce-todo-dependencies t)
  1595. (org-blocker-hook
  1596. '(org-block-todo-from-children-or-siblings-or-parent)))
  1597. (org-entry-blocked-p))))
  1598. (should-not
  1599. (org-test-with-temp-text
  1600. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  1601. (let ((org-enforce-todo-dependencies t)
  1602. (org-blocker-hook
  1603. '(org-block-todo-from-children-or-siblings-or-parent)))
  1604. (org-entry-blocked-p)))))
  1605. (ert-deftest test-org/get-outline-path ()
  1606. "Test `org-get-outline-path' specifications."
  1607. ;; Top-level headlines have no outline path.
  1608. (should-not
  1609. (org-test-with-temp-text "* H"
  1610. (org-get-outline-path)))
  1611. ;; Otherwise, outline path is the path leading to the headline.
  1612. (should
  1613. (equal '("H")
  1614. (org-test-with-temp-text "* H\n** S<point>"
  1615. (org-get-outline-path))))
  1616. ;; Find path even when point is not on a headline.
  1617. (should
  1618. (equal '("H")
  1619. (org-test-with-temp-text "* H\n** S\nText<point>"
  1620. (org-get-outline-path))))
  1621. ;; TODO keywords, tags and statistics cookies are ignored.
  1622. (should
  1623. (equal '("H")
  1624. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  1625. (org-get-outline-path))))
  1626. ;; Links are replaced with their description or their path.
  1627. (should
  1628. (equal '("Org")
  1629. (org-test-with-temp-text
  1630. "* [[http://orgmode.org][Org]]\n** S<point>"
  1631. (org-get-outline-path))))
  1632. (should
  1633. (equal '("http://orgmode.org")
  1634. (org-test-with-temp-text
  1635. "* [[http://orgmode.org]]\n** S<point>"
  1636. (org-get-outline-path))))
  1637. ;; When WITH-SELF is non-nil, include current heading.
  1638. (should
  1639. (equal '("H")
  1640. (org-test-with-temp-text "* H"
  1641. (org-get-outline-path t))))
  1642. (should
  1643. (equal '("H" "S")
  1644. (org-test-with-temp-text "* H\n** S\nText<point>"
  1645. (org-get-outline-path t))))
  1646. ;; Using cache is transparent to the user.
  1647. (should
  1648. (equal '("H")
  1649. (org-test-with-temp-text "* H\n** S<point>"
  1650. (setq org-outline-path-cache nil)
  1651. (org-get-outline-path nil t))))
  1652. ;; Do not corrupt cache when finding outline path in distant part of
  1653. ;; the buffer.
  1654. (should
  1655. (equal '("H2")
  1656. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  1657. (setq org-outline-path-cache nil)
  1658. (org-get-outline-path nil t)
  1659. (search-forward "S2")
  1660. (org-get-outline-path nil t))))
  1661. ;; Do not choke on empty headlines.
  1662. (should
  1663. (org-test-with-temp-text "* H\n** <point>"
  1664. (org-get-outline-path)))
  1665. (should
  1666. (org-test-with-temp-text "* \n** H<point>"
  1667. (org-get-outline-path))))
  1668. (ert-deftest test-org/format-outline-path ()
  1669. "Test `org-format-outline-path' specifications."
  1670. (should
  1671. (string= (org-format-outline-path (list "one" "two" "three"))
  1672. "one/two/three"))
  1673. ;; Empty path.
  1674. (should
  1675. (string= (org-format-outline-path '())
  1676. ""))
  1677. (should
  1678. (string= (org-format-outline-path '(nil))
  1679. ""))
  1680. ;; Empty path and prefix.
  1681. (should
  1682. (string= (org-format-outline-path '() nil ">>")
  1683. ">>"))
  1684. ;; Trailing whitespace in headings.
  1685. (should
  1686. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  1687. "one/tw o/three"))
  1688. ;; Non-default prefix and separators.
  1689. (should
  1690. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  1691. ">>|one|two|three"))
  1692. ;; Truncate.
  1693. (should
  1694. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  1695. "one/two/.."))
  1696. ;; Give a very narrow width.
  1697. (should
  1698. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  1699. "on"))
  1700. ;; Give a prefix that extends beyond the width.
  1701. (should
  1702. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  1703. ">>>>>>>>>>")
  1704. ">>>>>>>>..")))
  1705. (ert-deftest test-org/map-entries ()
  1706. "Test `org-map-entries' specifications."
  1707. ;; Full match.
  1708. (should
  1709. (equal '(1 11)
  1710. (org-test-with-temp-text "* Level 1\n** Level 2"
  1711. (org-map-entries #'point))))
  1712. ;; Level match.
  1713. (should
  1714. (equal '(1)
  1715. (org-test-with-temp-text "* Level 1\n** Level 2"
  1716. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  1717. (should
  1718. (equal '(11)
  1719. (org-test-with-temp-text "* Level 1\n** Level 2"
  1720. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  1721. ;; Tag match.
  1722. (should
  1723. (equal '(11)
  1724. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  1725. (org-map-entries #'point "yes"))))
  1726. (should
  1727. (equal '(14)
  1728. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  1729. (org-map-entries #'point "+yes-a"))))
  1730. (should
  1731. (equal '(11 23)
  1732. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  1733. (org-map-entries #'point "{yes?}"))))
  1734. ;; Priority match.
  1735. (should
  1736. (equal '(1)
  1737. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  1738. (org-map-entries #'point "PRIORITY=\"A\""))))
  1739. ;; Date match.
  1740. (should
  1741. (equal '(36)
  1742. (org-test-with-temp-text "
  1743. * H1
  1744. SCHEDULED: <2012-03-29 thu.>
  1745. * H2
  1746. SCHEDULED: <2014-03-04 tue.>"
  1747. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  1748. (should
  1749. (equal '(2)
  1750. (org-test-with-temp-text "
  1751. * H1
  1752. SCHEDULED: <2012-03-29 thu.>
  1753. * H2
  1754. SCHEDULED: <2014-03-04 tue.>"
  1755. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  1756. ;; Regular property match.
  1757. (should
  1758. (equal '(2)
  1759. (org-test-with-temp-text "
  1760. * H1
  1761. :PROPERTIES:
  1762. :TEST: 1
  1763. :END:
  1764. * H2
  1765. :PROPERTIES:
  1766. :TEST: 2
  1767. :END:"
  1768. (org-map-entries #'point "TEST=1"))))
  1769. ;; Multiple criteria.
  1770. (should
  1771. (equal '(23)
  1772. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  1773. (let (org-odd-levels-only
  1774. (org-use-tag-inheritance nil))
  1775. (org-map-entries #'point "yes+LEVEL=1")))))
  1776. ;; "or" criteria.
  1777. (should
  1778. (equal '(12 24)
  1779. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  1780. (let (org-odd-levels-only)
  1781. (org-map-entries #'point "LEVEL=2|no")))))
  1782. (should
  1783. (equal '(1 12)
  1784. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  1785. (let (org-odd-levels-only)
  1786. (org-map-entries #'point "yes|no")))))
  1787. ;; "and" criteria.
  1788. (should
  1789. (equal '(22)
  1790. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  1791. (let (org-odd-levels-only)
  1792. (org-map-entries #'point "yes&no"))))))
  1793. (ert-deftest test-org/edit-headline ()
  1794. "Test `org-edit-headline' specifications."
  1795. (should
  1796. (equal "* B"
  1797. (org-test-with-temp-text "* A"
  1798. (org-edit-headline "B")
  1799. (buffer-string))))
  1800. ;; Handle empty headings.
  1801. (should
  1802. (equal "* "
  1803. (org-test-with-temp-text "* A"
  1804. (org-edit-headline "")
  1805. (buffer-string))))
  1806. (should
  1807. (equal "* A"
  1808. (org-test-with-temp-text "* "
  1809. (org-edit-headline "A")
  1810. (buffer-string))))
  1811. ;; Handle TODO keywords and priority cookies.
  1812. (should
  1813. (equal "* TODO B"
  1814. (org-test-with-temp-text "* TODO A"
  1815. (org-edit-headline "B")
  1816. (buffer-string))))
  1817. (should
  1818. (equal "* [#A] B"
  1819. (org-test-with-temp-text "* [#A] A"
  1820. (org-edit-headline "B")
  1821. (buffer-string))))
  1822. (should
  1823. (equal "* TODO [#A] B"
  1824. (org-test-with-temp-text "* TODO [#A] A"
  1825. (org-edit-headline "B")
  1826. (buffer-string))))
  1827. ;; Handle tags.
  1828. (equal "* B :tag:"
  1829. (org-test-with-temp-text "* A :tag:"
  1830. (let ((org-tags-column 4)) (org-edit-headline "B"))
  1831. (buffer-string))))
  1832. ;;; Keywords
  1833. (ert-deftest test-org/set-regexps-and-options ()
  1834. "Test `org-set-regexps-and-options' specifications."
  1835. ;; TAGS keyword.
  1836. (should
  1837. (equal '(("A"))
  1838. (let ((org-tag-alist '(("A")))
  1839. (org-tag-persistent-alist nil))
  1840. (org-test-with-temp-text ""
  1841. (org-mode-restart)
  1842. org-current-tag-alist))))
  1843. (should
  1844. (equal '(("B"))
  1845. (let ((org-tag-alist '(("A")))
  1846. (org-tag-persistent-alist nil))
  1847. (org-test-with-temp-text "#+TAGS: B"
  1848. (org-mode-restart)
  1849. org-current-tag-alist))))
  1850. (should
  1851. (equal '(("C") ("B"))
  1852. (let ((org-tag-alist '(("A")))
  1853. (org-tag-persistent-alist '(("C"))))
  1854. (org-test-with-temp-text "#+TAGS: B"
  1855. (org-mode-restart)
  1856. org-current-tag-alist))))
  1857. (should
  1858. (equal '(("B"))
  1859. (let ((org-tag-alist '(("A")))
  1860. (org-tag-persistent-alist '(("C"))))
  1861. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  1862. (org-mode-restart)
  1863. org-current-tag-alist))))
  1864. (should
  1865. (equal '(("A" . ?a) ("B") ("C"))
  1866. (let ((org-tag-persistant-alist nil))
  1867. (org-test-with-temp-text "#+TAGS: A(a) B C"
  1868. (org-mode-restart)
  1869. org-current-tag-alist))))
  1870. (should
  1871. (equal '(("A") (:newline) ("B"))
  1872. (let ((org-tag-persistent-alist nil))
  1873. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  1874. (org-mode-restart)
  1875. org-current-tag-alist))))
  1876. (should
  1877. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  1878. (let ((org-tag-persistent-alist nil))
  1879. (org-test-with-temp-text "#+TAGS: { A B } C"
  1880. (org-mode-restart)
  1881. org-current-tag-alist))))
  1882. (should
  1883. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  1884. (let ((org-tag-persistent-alist nil))
  1885. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1886. (org-mode-restart)
  1887. org-current-tag-alist))))
  1888. (should
  1889. (equal '(("A" "B" "C"))
  1890. (let ((org-tag-persistent-alist nil))
  1891. (org-test-with-temp-text "#+TAGS: { A : B C }"
  1892. (org-mode-restart)
  1893. org-tag-groups-alist))))
  1894. (should
  1895. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  1896. (let ((org-tag-persistent-alist nil))
  1897. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1898. (org-mode-restart)
  1899. org-current-tag-alist))))
  1900. (should
  1901. (equal '(("A" "B" "C"))
  1902. (let ((org-tag-persistent-alist nil))
  1903. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  1904. (org-mode-restart)
  1905. org-tag-groups-alist))))
  1906. ;; FILETAGS keyword.
  1907. (should
  1908. (equal '("A" "B" "C")
  1909. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  1910. (org-mode-restart)
  1911. org-file-tags)))
  1912. ;; PROPERTY keyword. Property names are case-insensitive.
  1913. (should
  1914. (equal "foo=1"
  1915. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  1916. (org-mode-restart)
  1917. (cdr (assoc "var" org-file-properties)))))
  1918. (should
  1919. (equal
  1920. "foo=1 bar=2"
  1921. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  1922. (org-mode-restart)
  1923. (cdr (assoc "var" org-file-properties)))))
  1924. (should
  1925. (equal
  1926. "foo=1 bar=2"
  1927. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  1928. (org-mode-restart)
  1929. (cdr (assoc "var" org-file-properties)))))
  1930. ;; ARCHIVE keyword.
  1931. (should
  1932. (equal "%s_done::"
  1933. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  1934. (org-mode-restart)
  1935. org-archive-location)))
  1936. ;; CATEGORY keyword.
  1937. (should
  1938. (eq 'test
  1939. (org-test-with-temp-text "#+CATEGORY: test"
  1940. (org-mode-restart)
  1941. org-category)))
  1942. (should
  1943. (equal "test"
  1944. (org-test-with-temp-text "#+CATEGORY: test"
  1945. (org-mode-restart)
  1946. (cdr (assoc "CATEGORY" org-file-properties)))))
  1947. ;; COLUMNS keyword.
  1948. (should
  1949. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  1950. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  1951. (org-mode-restart)
  1952. org-columns-default-format)))
  1953. ;; CONSTANTS keyword. Constants names are case sensitive.
  1954. (should
  1955. (equal '("299792458." "3.14")
  1956. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  1957. (org-mode-restart)
  1958. (mapcar
  1959. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  1960. '("c" "pi")))))
  1961. (should
  1962. (equal "3.14"
  1963. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  1964. (org-mode-restart)
  1965. (cdr (assoc "pi" org-table-formula-constants-local)))))
  1966. (should
  1967. (equal "22/7"
  1968. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  1969. (org-mode-restart)
  1970. (cdr (assoc "PI" org-table-formula-constants-local)))))
  1971. ;; LINK keyword.
  1972. (should
  1973. (equal
  1974. '("url1" "url2")
  1975. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  1976. (org-mode-restart)
  1977. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  1978. '("a" "b")))))
  1979. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  1980. (should
  1981. (equal
  1982. '(?X ?Z ?Y)
  1983. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  1984. (org-mode-restart)
  1985. (list org-highest-priority org-lowest-priority org-default-priority))))
  1986. (should
  1987. (equal
  1988. '(?A ?C ?B)
  1989. (org-test-with-temp-text "#+PRIORITIES: X Z"
  1990. (org-mode-restart)
  1991. (list org-highest-priority org-lowest-priority org-default-priority))))
  1992. ;; STARTUP keyword.
  1993. (should
  1994. (equal '(t t)
  1995. (org-test-with-temp-text "#+STARTUP: fold odd"
  1996. (org-mode-restart)
  1997. (list org-startup-folded org-odd-levels-only))))
  1998. ;; TODO keywords.
  1999. (should
  2000. (equal '(("A" "B") ("C"))
  2001. (org-test-with-temp-text "#+TODO: A B | C"
  2002. (org-mode-restart)
  2003. (list org-not-done-keywords org-done-keywords))))
  2004. (should
  2005. (equal '(("A" "C") ("B" "D"))
  2006. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2007. (org-mode-restart)
  2008. (list org-not-done-keywords org-done-keywords))))
  2009. (should
  2010. (equal '(("A" "B") ("C"))
  2011. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2012. (org-mode-restart)
  2013. (list org-not-done-keywords org-done-keywords))))
  2014. (should
  2015. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2016. (org-test-with-temp-text "#+TODO: A(a)"
  2017. (org-mode-restart)
  2018. org-todo-key-alist)))
  2019. (should
  2020. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2021. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2022. (org-mode-restart)
  2023. org-todo-log-states)))
  2024. ;; Enter SETUPFILE keyword.
  2025. (should
  2026. (equal "1"
  2027. (org-test-with-temp-text
  2028. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2029. (org-mode-restart)
  2030. (cdr (assoc "a" org-file-properties))))))
  2031. ;;; Links
  2032. ;;;; Coderefs
  2033. (ert-deftest test-org/coderef ()
  2034. "Test coderef links specifications."
  2035. (should
  2036. (org-test-with-temp-text "
  2037. #+BEGIN_SRC emacs-lisp
  2038. \(+ 1 1) (ref:sc)
  2039. #+END_SRC
  2040. \[[(sc)<point>]]"
  2041. (org-open-at-point)
  2042. (looking-at "(ref:sc)")))
  2043. ;; Find coderef even with alternate label format.
  2044. (should
  2045. (org-test-with-temp-text "
  2046. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2047. \(+ 1 1) {ref:sc}
  2048. #+END_SRC
  2049. \[[(sc)<point>]]"
  2050. (org-open-at-point)
  2051. (looking-at "{ref:sc}"))))
  2052. ;;;; Custom ID
  2053. (ert-deftest test-org/custom-id ()
  2054. "Test custom ID links specifications."
  2055. (should
  2056. (org-test-with-temp-text
  2057. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2058. (org-open-at-point)
  2059. (looking-at-p "\\* H1")))
  2060. ;; Throw an error on false positives.
  2061. (should-error
  2062. (org-test-with-temp-text
  2063. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2064. (org-open-at-point)
  2065. (looking-at-p "\\* H1"))))
  2066. ;;;; Fuzzy Links
  2067. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2068. ;; a named element (#+name: text) and to headlines (* Text).
  2069. (ert-deftest test-org/fuzzy-links ()
  2070. "Test fuzzy links specifications."
  2071. ;; Fuzzy link goes in priority to a matching target.
  2072. (should
  2073. (org-test-with-temp-text
  2074. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2075. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2076. (looking-at "<<Test>>")))
  2077. ;; Then fuzzy link points to an element with a given name.
  2078. (should
  2079. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2080. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2081. (looking-at "#\\+NAME: Test")))
  2082. ;; A target still lead to a matching headline otherwise.
  2083. (should
  2084. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2085. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2086. (looking-at "\\* Head2")))
  2087. ;; With a leading star in link, enforce heading match.
  2088. (should
  2089. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2090. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2091. (looking-at "\\* Test")))
  2092. ;; With a leading star in link, enforce exact heading match, even
  2093. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2094. (should-error
  2095. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2096. (let ((org-link-search-must-match-exact-headline nil))
  2097. (org-open-at-point))))
  2098. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2099. (should
  2100. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2101. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2102. (looking-at "\\* Test")))
  2103. (should
  2104. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2105. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2106. (looking-at "\\* Test")))
  2107. ;; Heading match should not care about spaces, cookies, TODO
  2108. ;; keywords, priorities, and tags. However, TODO keywords are
  2109. ;; case-sensitive.
  2110. (should
  2111. (let ((first-line
  2112. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2113. (org-test-with-temp-text
  2114. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2115. (let ((org-link-search-must-match-exact-headline nil)
  2116. (org-todo-regexp "TODO"))
  2117. (org-open-at-point))
  2118. (looking-at (regexp-quote first-line)))))
  2119. (should-error
  2120. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2121. (let ((org-link-search-must-match-exact-headline nil)
  2122. (org-todo-regexp "TODO"))
  2123. (org-open-at-point))))
  2124. ;; Heading match should still be exact.
  2125. (should-error
  2126. (org-test-with-temp-text "
  2127. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2128. Foo Bar
  2129. <point>[[*Test 1]]"
  2130. (let ((org-link-search-must-match-exact-headline nil)
  2131. (org-todo-regexp "TODO"))
  2132. (org-open-at-point))))
  2133. (should
  2134. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2135. (let ((org-link-search-must-match-exact-headline nil)
  2136. (org-todo-regexp "TODO"))
  2137. (org-open-at-point))
  2138. (looking-at-p (regexp-quote "** Test 1 2"))))
  2139. ;; Heading match ignores COMMENT keyword.
  2140. (should
  2141. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2142. (org-open-at-point)
  2143. (looking-at "\\* COMMENT Test")))
  2144. (should
  2145. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2146. (org-open-at-point)
  2147. (looking-at "\\* TODO COMMENT Test")))
  2148. ;; Correctly un-hexify fuzzy links.
  2149. (should
  2150. (org-test-with-temp-text "* With space\n[[*With%20space][With space<point>]]"
  2151. (org-open-at-point)
  2152. (bobp)))
  2153. (should
  2154. (org-test-with-temp-text "* [1]\n[[*%5B1%5D<point>]]"
  2155. (org-open-at-point)
  2156. (bobp)))
  2157. ;; Match search strings containing newline characters, including
  2158. ;; blank lines.
  2159. (should
  2160. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2161. (let ((file (buffer-file-name)))
  2162. (goto-char (point-max))
  2163. (insert (format "[[file:%s::line1 line2]]" file))
  2164. (beginning-of-line)
  2165. (let ((org-link-search-must-match-exact-headline nil))
  2166. (org-open-at-point 0))
  2167. (looking-at-p "line1"))))
  2168. (should
  2169. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2170. (let ((file (buffer-file-name)))
  2171. (goto-char (point-max))
  2172. (insert (format "[[file:%s::line1 line2]]" file))
  2173. (beginning-of-line)
  2174. (let ((org-link-search-must-match-exact-headline nil))
  2175. (org-open-at-point 0))
  2176. (looking-at-p "line1")))))
  2177. ;;;; Link Escaping
  2178. (ert-deftest test-org/org-link-escape-ascii-character ()
  2179. "Escape an ascii character."
  2180. (should
  2181. (string=
  2182. "%5B"
  2183. (org-link-escape "["))))
  2184. (ert-deftest test-org/org-link-escape-ascii-ctrl-character ()
  2185. "Escape an ascii control character."
  2186. (should
  2187. (string=
  2188. "%09"
  2189. (org-link-escape "\t"))))
  2190. (ert-deftest test-org/org-link-escape-multibyte-character ()
  2191. "Escape an unicode multibyte character."
  2192. (should
  2193. (string=
  2194. "%E2%82%AC"
  2195. (org-link-escape "€"))))
  2196. (ert-deftest test-org/org-link-escape-custom-table ()
  2197. "Escape string with custom character table."
  2198. (should
  2199. (string=
  2200. "Foo%3A%42ar%0A"
  2201. (org-link-escape "Foo:Bar\n" '(?\: ?\B)))))
  2202. (ert-deftest test-org/org-link-escape-custom-table-merge ()
  2203. "Escape string with custom table merged with default table."
  2204. (should
  2205. (string=
  2206. "%5BF%6F%6F%3A%42ar%0A%5D"
  2207. (org-link-escape "[Foo:Bar\n]" '(?\: ?\B ?\o) t))))
  2208. (ert-deftest test-org/org-link-unescape-ascii-character ()
  2209. "Unescape an ascii character."
  2210. (should
  2211. (string=
  2212. "["
  2213. (org-link-unescape "%5B"))))
  2214. (ert-deftest test-org/org-link-unescape-ascii-ctrl-character ()
  2215. "Unescpae an ascii control character."
  2216. (should
  2217. (string=
  2218. "\n"
  2219. (org-link-unescape "%0A"))))
  2220. (ert-deftest test-org/org-link-unescape-multibyte-character ()
  2221. "Unescape unicode multibyte character."
  2222. (should
  2223. (string=
  2224. "€"
  2225. (org-link-unescape "%E2%82%AC"))))
  2226. (ert-deftest test-org/org-link-unescape-ascii-extended-char ()
  2227. "Unescape old style percent escaped character."
  2228. (should
  2229. (string=
  2230. "àâçèéêîôùû"
  2231. (decode-coding-string
  2232. (org-link-unescape "%E0%E2%E7%E8%E9%EA%EE%F4%F9%FB") 'latin-1))))
  2233. (ert-deftest test-org/org-link-escape-url-with-escaped-char ()
  2234. "Escape and unescape a URL that includes an escaped char.
  2235. http://article.gmane.org/gmane.emacs.orgmode/21459/"
  2236. (should
  2237. (string=
  2238. "http://some.host.com/form?&id=blah%2Bblah25"
  2239. (org-link-unescape
  2240. (org-link-escape "http://some.host.com/form?&id=blah%2Bblah25")))))
  2241. ;;;; Open at point
  2242. (ert-deftest test-org/open-at-point/keyword ()
  2243. "Does `org-open-at-point' open link in a keyword line?"
  2244. (should
  2245. (org-test-with-temp-text
  2246. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2247. (org-open-at-point) t))
  2248. (should
  2249. (org-test-with-temp-text
  2250. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  2251. (org-open-at-point) t)))
  2252. (ert-deftest test-org/open-at-point/property ()
  2253. "Does `org-open-at-point' open link in property drawer?"
  2254. (should
  2255. (org-test-with-temp-text
  2256. "* Headline
  2257. :PROPERTIES:
  2258. :URL: <point>[[*Headline]]
  2259. :END:"
  2260. (org-open-at-point) t)))
  2261. (ert-deftest test-org/open-at-point/comment ()
  2262. "Does `org-open-at-point' open link in a commented line?"
  2263. (should
  2264. (org-test-with-temp-text
  2265. "<<top>>\n# <point>[[top]]"
  2266. (org-open-at-point) t))
  2267. (should
  2268. (org-test-with-temp-text
  2269. "* H\n<<top>>\n# <point>[[top]]"
  2270. (org-open-at-point) t)))
  2271. (ert-deftest test-org/open-at-point/inline-image ()
  2272. "Test `org-open-at-point' on nested links."
  2273. (should
  2274. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  2275. (org-open-at-point)
  2276. (bobp))))
  2277. (ert-deftest test-org/open-at-point/radio-target ()
  2278. "Test `org-open-at-point' on radio targets."
  2279. (should
  2280. (org-test-with-temp-text "<<<target>>> <point>target"
  2281. (org-update-radio-target-regexp)
  2282. (org-open-at-point)
  2283. (eq (org-element-type (org-element-context)) 'radio-target))))
  2284. ;;;; Stored links
  2285. (ert-deftest test-org/store-link ()
  2286. "Test `org-store-link' specifications."
  2287. ;; On a headline, link to that headline. Use heading as the
  2288. ;; description of the link.
  2289. (should
  2290. (let (org-store-link-props org-stored-links)
  2291. (org-test-with-temp-text-in-file "* H1"
  2292. (let ((file (buffer-file-name)))
  2293. (equal (format "[[file:%s::*H1][H1]]" file)
  2294. (org-store-link nil))))))
  2295. ;; On a headline, remove any link from description.
  2296. (should
  2297. (let (org-store-link-props org-stored-links)
  2298. (org-test-with-temp-text-in-file "* [[#l][d]]"
  2299. (let ((file (buffer-file-name)))
  2300. (equal (format "[[file:%s::*%s][d]]"
  2301. file
  2302. (org-link-escape "[[#l][d]]"))
  2303. (org-store-link nil))))))
  2304. (should
  2305. (let (org-store-link-props org-stored-links)
  2306. (org-test-with-temp-text-in-file "* [[l]]"
  2307. (let ((file (buffer-file-name)))
  2308. (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
  2309. (org-store-link nil))))))
  2310. (should
  2311. (let (org-store-link-props org-stored-links)
  2312. (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
  2313. (let ((file (buffer-file-name)))
  2314. (equal (format "[[file:%s::*%s][d1 d2]]"
  2315. file
  2316. (org-link-escape "[[l1][d1]] [[l2][d2]]"))
  2317. (org-store-link nil))))))
  2318. ;; On a named element, link to that element.
  2319. (should
  2320. (let (org-store-link-props org-stored-links)
  2321. (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
  2322. (let ((file (buffer-file-name)))
  2323. (equal (format "[[file:%s::foo][foo]]" file)
  2324. (org-store-link nil))))))
  2325. ;; Store link to Org buffer, with context.
  2326. (should
  2327. (let ((org-stored-links nil)
  2328. (org-id-link-to-org-use-id nil)
  2329. (org-context-in-file-links t))
  2330. (org-test-with-temp-text-in-file "* h1"
  2331. (let ((file (buffer-file-name)))
  2332. (equal (format "[[file:%s::*h1][h1]]" file)
  2333. (org-store-link nil))))))
  2334. ;; Store link to Org buffer, without context.
  2335. (should
  2336. (let ((org-stored-links nil)
  2337. (org-id-link-to-org-use-id nil)
  2338. (org-context-in-file-links nil))
  2339. (org-test-with-temp-text-in-file "* h1"
  2340. (let ((file (buffer-file-name)))
  2341. (equal (format "[[file:%s][file:%s]]" file file)
  2342. (org-store-link nil))))))
  2343. ;; C-u prefix reverses `org-context-in-file-links' in Org buffer.
  2344. (should
  2345. (let ((org-stored-links nil)
  2346. (org-id-link-to-org-use-id nil)
  2347. (org-context-in-file-links nil))
  2348. (org-test-with-temp-text-in-file "* h1"
  2349. (let ((file (buffer-file-name)))
  2350. (equal (format "[[file:%s::*h1][h1]]" file)
  2351. (org-store-link '(4)))))))
  2352. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in Org
  2353. ;; buffer.
  2354. (should
  2355. (let ((org-stored-links nil)
  2356. (org-id-link-to-org-use-id nil)
  2357. (org-context-in-file-links nil))
  2358. (org-test-with-temp-text-in-file "* h1"
  2359. (let ((file (buffer-file-name)))
  2360. (equal (format "[[file:%s][file:%s]]" file file)
  2361. (org-store-link '(16)))))))
  2362. ;; Store file link to non-Org buffer, with context.
  2363. (should
  2364. (let ((org-stored-links nil)
  2365. (org-context-in-file-links t))
  2366. (org-test-with-temp-text-in-file "one\n<point>two"
  2367. (fundamental-mode)
  2368. (let ((file (buffer-file-name)))
  2369. (equal (format "[[file:%s::one]]" file)
  2370. (org-store-link nil))))))
  2371. ;; Store file link to non-Org buffer, without context.
  2372. (should
  2373. (let ((org-stored-links nil)
  2374. (org-context-in-file-links nil))
  2375. (org-test-with-temp-text-in-file "one\n<point>two"
  2376. (fundamental-mode)
  2377. (let ((file (buffer-file-name)))
  2378. (equal (format "[[file:%s][file:%s]]" file file)
  2379. (org-store-link nil))))))
  2380. ;; C-u prefix reverses `org-context-in-file-links' in non-Org
  2381. ;; buffer.
  2382. (should
  2383. (let ((org-stored-links nil)
  2384. (org-context-in-file-links nil))
  2385. (org-test-with-temp-text-in-file "one\n<point>two"
  2386. (fundamental-mode)
  2387. (let ((file (buffer-file-name)))
  2388. (equal (format "[[file:%s::one]]" file)
  2389. (org-store-link '(4)))))))
  2390. ;; A C-u C-u does *not* reverse `org-context-in-file-links' in
  2391. ;; non-Org buffer.
  2392. (should
  2393. (let ((org-stored-links nil)
  2394. (org-context-in-file-links nil))
  2395. (org-test-with-temp-text-in-file "one\n<point>two"
  2396. (fundamental-mode)
  2397. (let ((file (buffer-file-name)))
  2398. (equal (format "[[file:%s][file:%s]]" file file)
  2399. (org-store-link '(16))))))))
  2400. ;;; Node Properties
  2401. (ert-deftest test-org/accumulated-properties-in-drawers ()
  2402. "Ensure properties accumulate in subtree drawers."
  2403. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  2404. (org-babel-next-src-block)
  2405. (should (equal '(2 1) (org-babel-execute-src-block)))))
  2406. (ert-deftest test-org/custom-properties ()
  2407. "Test custom properties specifications."
  2408. ;; Standard test.
  2409. (should
  2410. (let ((org-custom-properties '("FOO")))
  2411. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2412. (org-toggle-custom-properties-visibility)
  2413. (org-invisible-p2))))
  2414. ;; Properties are case-insensitive.
  2415. (should
  2416. (let ((org-custom-properties '("FOO")))
  2417. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  2418. (org-toggle-custom-properties-visibility)
  2419. (org-invisible-p2))))
  2420. (should
  2421. (let ((org-custom-properties '("foo")))
  2422. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  2423. (org-toggle-custom-properties-visibility)
  2424. (org-invisible-p2))))
  2425. ;; Multiple custom properties in the same drawer.
  2426. (should
  2427. (let ((org-custom-properties '("FOO" "BAR")))
  2428. (org-test-with-temp-text
  2429. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  2430. (org-toggle-custom-properties-visibility)
  2431. (and (org-invisible-p2)
  2432. (not (progn (forward-line) (org-invisible-p2)))
  2433. (progn (forward-line) (org-invisible-p2))))))
  2434. ;; Hide custom properties with an empty value.
  2435. (should
  2436. (let ((org-custom-properties '("FOO")))
  2437. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  2438. (org-toggle-custom-properties-visibility)
  2439. (org-invisible-p2))))
  2440. ;; Do not hide fake properties.
  2441. (should-not
  2442. (let ((org-custom-properties '("FOO")))
  2443. (org-test-with-temp-text ":FOO: val\n"
  2444. (org-toggle-custom-properties-visibility)
  2445. (org-invisible-p2))))
  2446. (should-not
  2447. (let ((org-custom-properties '("A")))
  2448. (org-test-with-temp-text
  2449. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  2450. (org-toggle-custom-properties-visibility)
  2451. (org-invisible-p2)))))
  2452. ;;; Mark Region
  2453. (ert-deftest test-org/mark-subtree ()
  2454. "Test `org-mark-subtree' specifications."
  2455. ;; Error when point is before first headline.
  2456. (should-error
  2457. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  2458. (progn (transient-mark-mode 1)
  2459. (org-mark-subtree))))
  2460. ;; Without argument, mark current subtree.
  2461. (should
  2462. (equal
  2463. '(12 32)
  2464. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2465. (progn (transient-mark-mode 1)
  2466. (forward-line 2)
  2467. (org-mark-subtree)
  2468. (list (region-beginning) (region-end))))))
  2469. ;; With an argument, move ARG up.
  2470. (should
  2471. (equal
  2472. '(1 32)
  2473. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  2474. (progn (transient-mark-mode 1)
  2475. (forward-line 2)
  2476. (org-mark-subtree 1)
  2477. (list (region-beginning) (region-end))))))
  2478. ;; Do not get fooled by inlinetasks.
  2479. (when (featurep 'org-inlinetask)
  2480. (should
  2481. (= 1
  2482. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  2483. (progn (transient-mark-mode 1)
  2484. (forward-line 1)
  2485. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  2486. (region-beginning)))))))
  2487. ;;; Miscellaneous
  2488. (ert-deftest test-org/in-regexp ()
  2489. "Test `org-in-regexp' specifications."
  2490. ;; Standard tests.
  2491. (should
  2492. (org-test-with-temp-text "xx ab<point>c xx"
  2493. (org-in-regexp "abc")))
  2494. (should-not
  2495. (org-test-with-temp-text "xx abc <point>xx"
  2496. (org-in-regexp "abc")))
  2497. ;; Return non-nil even with multiple matching regexps in the same
  2498. ;; line.
  2499. (should
  2500. (org-test-with-temp-text "abc xx ab<point>c xx"
  2501. (org-in-regexp "abc")))
  2502. ;; With optional argument NLINES, check extra lines around point.
  2503. (should-not
  2504. (org-test-with-temp-text "A\nB<point>\nC"
  2505. (org-in-regexp "A\nB\nC")))
  2506. (should
  2507. (org-test-with-temp-text "A\nB<point>\nC"
  2508. (org-in-regexp "A\nB\nC" 1)))
  2509. (should-not
  2510. (org-test-with-temp-text "A\nB\nC<point>"
  2511. (org-in-regexp "A\nB\nC" 1)))
  2512. ;; When optional argument VISUALLY is non-nil, return nil if at
  2513. ;; regexp boundaries.
  2514. (should
  2515. (org-test-with-temp-text "xx abc<point> xx"
  2516. (org-in-regexp "abc")))
  2517. (should-not
  2518. (org-test-with-temp-text "xx abc<point> xx"
  2519. (org-in-regexp "abc" nil t))))
  2520. (ert-deftest test-org/sort-entries ()
  2521. "Test `org-sort-entries'."
  2522. ;; Sort alphabetically.
  2523. (should
  2524. (equal "\n* abc\n* def\n* xyz\n"
  2525. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2526. (org-sort-entries nil ?a)
  2527. (buffer-string))))
  2528. (should
  2529. (equal "\n* xyz\n* def\n* abc\n"
  2530. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  2531. (org-sort-entries nil ?A)
  2532. (buffer-string))))
  2533. ;; Sort numerically.
  2534. (should
  2535. (equal "\n* 1\n* 2\n* 10\n"
  2536. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2537. (org-sort-entries nil ?n)
  2538. (buffer-string))))
  2539. (should
  2540. (equal "\n* 10\n* 2\n* 1\n"
  2541. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  2542. (org-sort-entries nil ?N)
  2543. (buffer-string))))
  2544. ;; Sort by custom function.
  2545. (should
  2546. (equal "\n* b\n* aa\n* ccc\n"
  2547. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2548. (org-sort-entries nil ?f
  2549. (lambda ()
  2550. (length (buffer-substring (point-at-bol)
  2551. (point-at-eol))))
  2552. #'<)
  2553. (buffer-string))))
  2554. (should
  2555. (equal "\n* ccc\n* aa\n* b\n"
  2556. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  2557. (org-sort-entries nil ?F
  2558. (lambda ()
  2559. (length (buffer-substring (point-at-bol)
  2560. (point-at-eol))))
  2561. #'<)
  2562. (buffer-string))))
  2563. ;; Sort by TODO keyword.
  2564. (should
  2565. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  2566. (org-test-with-temp-text
  2567. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2568. (org-sort-entries nil ?o)
  2569. (buffer-string))))
  2570. (should
  2571. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  2572. (org-test-with-temp-text
  2573. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  2574. (org-sort-entries nil ?O)
  2575. (buffer-string))))
  2576. ;; Sort by priority.
  2577. (should
  2578. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  2579. (org-test-with-temp-text
  2580. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2581. (org-sort-entries nil ?p)
  2582. (buffer-string))))
  2583. (should
  2584. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  2585. (org-test-with-temp-text
  2586. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  2587. (org-sort-entries nil ?P)
  2588. (buffer-string))))
  2589. ;; Sort by creation time.
  2590. (should
  2591. (equal "
  2592. * h3
  2593. [2017-05-08 Mon]
  2594. * h2
  2595. [2017-05-09 Tue]
  2596. * h1
  2597. [2018-05-09 Wed]
  2598. "
  2599. (org-test-with-temp-text
  2600. "
  2601. * h1
  2602. [2018-05-09 Wed]
  2603. * h2
  2604. [2017-05-09 Tue]
  2605. * h3
  2606. [2017-05-08 Mon]
  2607. "
  2608. (org-sort-entries nil ?c)
  2609. (buffer-string))))
  2610. ;; Sort by scheduled date.
  2611. (should
  2612. (equal "
  2613. * TODO h4
  2614. SCHEDULED: <2017-05-06 Sat>
  2615. * TODO h3
  2616. SCHEDULED: <2017-05-08 Mon>
  2617. * TODO h2
  2618. DEADLINE: <2017-05-09 Tue>
  2619. * TODO h1
  2620. DEADLINE: <2017-05-07 Sun>
  2621. "
  2622. (org-test-with-temp-text
  2623. "
  2624. * TODO h2
  2625. DEADLINE: <2017-05-09 Tue>
  2626. * TODO h1
  2627. DEADLINE: <2017-05-07 Sun>
  2628. * TODO h3
  2629. SCHEDULED: <2017-05-08 Mon>
  2630. * TODO h4
  2631. SCHEDULED: <2017-05-06 Sat>
  2632. "
  2633. (org-sort-entries nil ?s)
  2634. (buffer-string))))
  2635. ;; Sort by deadline date.
  2636. (should
  2637. (equal "
  2638. * TODO h1
  2639. DEADLINE: <2017-05-07 Sun>
  2640. * TODO h2
  2641. DEADLINE: <2017-05-09 Tue>
  2642. * TODO h3
  2643. SCHEDULED: <2017-05-08 Mon>
  2644. * TODO h4
  2645. SCHEDULED: <2017-05-06 Sat>
  2646. "
  2647. (org-test-with-temp-text
  2648. "
  2649. * TODO h2
  2650. DEADLINE: <2017-05-09 Tue>
  2651. * TODO h1
  2652. DEADLINE: <2017-05-07 Sun>
  2653. * TODO h3
  2654. SCHEDULED: <2017-05-08 Mon>
  2655. * TODO h4
  2656. SCHEDULED: <2017-05-06 Sat>
  2657. "
  2658. (org-sort-entries nil ?d)
  2659. (buffer-string))))
  2660. ;; Sort by any date/time
  2661. (should
  2662. (equal "
  2663. * TODO h4
  2664. SCHEDULED: <2017-05-06 Sat>
  2665. * TODO h1
  2666. DEADLINE: <2017-05-07 Sun>
  2667. * TODO h3
  2668. SCHEDULED: <2017-05-08 Mon>
  2669. * TODO h2
  2670. DEADLINE: <2017-05-09 Tue>
  2671. "
  2672. (org-test-with-temp-text
  2673. "
  2674. * TODO h2
  2675. DEADLINE: <2017-05-09 Tue>
  2676. * TODO h1
  2677. DEADLINE: <2017-05-07 Sun>
  2678. * TODO h3
  2679. SCHEDULED: <2017-05-08 Mon>
  2680. * TODO h4
  2681. SCHEDULED: <2017-05-06 Sat>
  2682. "
  2683. (org-sort-entries nil ?t)
  2684. (buffer-string))))
  2685. ;; Sort by clocking time.
  2686. (should
  2687. (equal "
  2688. * clocked h2
  2689. :LOGBOOK:
  2690. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2691. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2692. :END:
  2693. * clocked h1
  2694. :LOGBOOK:
  2695. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2696. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2697. :END:
  2698. "
  2699. (org-test-with-temp-text
  2700. "
  2701. * clocked h1
  2702. :LOGBOOK:
  2703. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2704. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  2705. :END:
  2706. * clocked h2
  2707. :LOGBOOK:
  2708. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  2709. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  2710. :END:
  2711. "
  2712. (org-sort-entries nil ?k)
  2713. (buffer-string)))))
  2714. ;;; Navigation
  2715. (ert-deftest test-org/forward-heading-same-level ()
  2716. "Test `org-forward-heading-same-level' specifications."
  2717. ;; Test navigation at top level, forward and backward.
  2718. (should
  2719. (equal "* H2"
  2720. (org-test-with-temp-text "* H1\n* H2"
  2721. (org-forward-heading-same-level 1)
  2722. (buffer-substring-no-properties (point) (line-end-position)))))
  2723. (should
  2724. (equal "* H1"
  2725. (org-test-with-temp-text "* H1\n<point>* H2"
  2726. (org-forward-heading-same-level -1)
  2727. (buffer-substring-no-properties (point) (line-end-position)))))
  2728. ;; Test navigation in a sub-tree, forward and backward.
  2729. (should
  2730. (equal "* H2"
  2731. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  2732. (org-forward-heading-same-level 1)
  2733. (buffer-substring-no-properties (point) (line-end-position)))))
  2734. (should
  2735. (equal "* H1"
  2736. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  2737. (org-forward-heading-same-level -1)
  2738. (buffer-substring-no-properties (point) (line-end-position)))))
  2739. ;; Stop at first or last sub-heading.
  2740. (should-not
  2741. (equal "* H2"
  2742. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  2743. (org-forward-heading-same-level 1)
  2744. (buffer-substring-no-properties (point) (line-end-position)))))
  2745. (should-not
  2746. (equal "* H2"
  2747. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  2748. (org-forward-heading-same-level -1)
  2749. (buffer-substring-no-properties (point) (line-end-position)))))
  2750. ;; Allow multiple moves.
  2751. (should
  2752. (equal "* H3"
  2753. (org-test-with-temp-text "* H1\n* H2\n* H3"
  2754. (org-forward-heading-same-level 2)
  2755. (buffer-substring-no-properties (point) (line-end-position)))))
  2756. (should
  2757. (equal "* H1"
  2758. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  2759. (org-forward-heading-same-level -2)
  2760. (buffer-substring-no-properties (point) (line-end-position)))))
  2761. ;; Ignore spurious moves when first (or last) sibling is reached.
  2762. (should
  2763. (equal "** H3"
  2764. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  2765. (org-forward-heading-same-level 100)
  2766. (buffer-substring-no-properties (point) (line-end-position)))))
  2767. (should
  2768. (equal "** H1"
  2769. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  2770. (org-forward-heading-same-level -100)
  2771. (buffer-substring-no-properties (point) (line-end-position))))))
  2772. (ert-deftest test-org/end-of-meta-data ()
  2773. "Test `org-end-of-meta-data' specifications."
  2774. ;; Skip planning line.
  2775. (should
  2776. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  2777. (org-end-of-meta-data)
  2778. (eobp)))
  2779. ;; Skip properties drawer.
  2780. (should
  2781. (org-test-with-temp-text
  2782. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  2783. (org-end-of-meta-data)
  2784. (eobp)))
  2785. ;; Skip both.
  2786. (should
  2787. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  2788. (org-end-of-meta-data)
  2789. (eobp)))
  2790. ;; Nothing to skip, go to first line.
  2791. (should
  2792. (org-test-with-temp-text "* Headline\nContents"
  2793. (org-end-of-meta-data)
  2794. (looking-at "Contents")))
  2795. ;; With option argument, skip empty lines, regular drawers and
  2796. ;; clocking lines.
  2797. (should
  2798. (org-test-with-temp-text "* Headline\n\nContents"
  2799. (org-end-of-meta-data t)
  2800. (looking-at "Contents")))
  2801. (should
  2802. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  2803. (org-end-of-meta-data t)
  2804. (looking-at "Contents")))
  2805. (should
  2806. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  2807. (org-end-of-meta-data t)
  2808. (looking-at "Contents")))
  2809. ;; Special case: do not skip incomplete drawers.
  2810. (should
  2811. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  2812. (org-end-of-meta-data t)
  2813. (looking-at ":LOGBOOK:")))
  2814. ;; Special case: Be careful about consecutive headlines.
  2815. (should-not
  2816. (org-test-with-temp-text "* H1\n*H2\nContents"
  2817. (org-end-of-meta-data t)
  2818. (looking-at "Contents"))))
  2819. (ert-deftest test-org/beginning-of-line ()
  2820. "Test `org-beginning-of-line' specifications."
  2821. ;; Move to beginning of line. If current line in invisible, move to
  2822. ;; beginning of visible line instead.
  2823. (should
  2824. (org-test-with-temp-text "Some text\nSome other text<point>"
  2825. (org-beginning-of-line)
  2826. (bolp)))
  2827. (should
  2828. (org-test-with-temp-text "* H1\n** H2<point>"
  2829. (org-overview)
  2830. (org-beginning-of-line)
  2831. (= (line-beginning-position) 1)))
  2832. ;; With `visual-line-mode' active, move to beginning of visual line.
  2833. (should-not
  2834. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2835. (visual-line-mode)
  2836. (dotimes (i 1000) (insert "very "))
  2837. (org-beginning-of-line)
  2838. (bolp)))
  2839. ;; In a wide headline, with `visual-line-mode', prefer going to the
  2840. ;; beginning of a visual line than to the logical beginning of line,
  2841. ;; even if special movement is active.
  2842. (should-not
  2843. (org-test-with-temp-text "* A <point>long headline"
  2844. (visual-line-mode)
  2845. (dotimes (i 1000) (insert "very "))
  2846. (goto-char (point-max))
  2847. (org-beginning-of-line)
  2848. (bobp)))
  2849. (should-not
  2850. (org-test-with-temp-text "* A <point>long headline"
  2851. (visual-line-mode)
  2852. (dotimes (i 1000) (insert "very "))
  2853. (goto-char (point-max))
  2854. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  2855. (bobp)))
  2856. ;; At an headline with special movement, first move at beginning of
  2857. ;; title, then at the beginning of line, rinse, repeat.
  2858. (should
  2859. (org-test-with-temp-text "* TODO Headline<point>"
  2860. (let ((org-special-ctrl-a/e t))
  2861. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  2862. (progn (org-beginning-of-line) (bolp))
  2863. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2864. (should
  2865. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2866. (let ((org-special-ctrl-a/e t))
  2867. (org-beginning-of-line)
  2868. (looking-at "Headline"))))
  2869. (should
  2870. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2871. (let ((org-special-ctrl-a/e '(t . nil)))
  2872. (org-beginning-of-line)
  2873. (looking-at "Headline"))))
  2874. (should-not
  2875. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  2876. (let ((org-special-ctrl-a/e '(nil . nil)))
  2877. (org-beginning-of-line)
  2878. (looking-at "Headline"))))
  2879. ;; At an headline with reversed movement, first move to beginning of
  2880. ;; line, then to the beginning of title.
  2881. (should
  2882. (org-test-with-temp-text "* TODO Headline<point>"
  2883. (let ((org-special-ctrl-a/e 'reversed)
  2884. (this-command last-command))
  2885. (and (progn (org-beginning-of-line) (bolp))
  2886. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2887. (should
  2888. (org-test-with-temp-text "* TODO Headline<point>"
  2889. (let ((org-special-ctrl-a/e '(reversed . nil))
  2890. (this-command last-command))
  2891. (and (progn (org-beginning-of-line) (bolp))
  2892. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2893. (should-not
  2894. (org-test-with-temp-text "* TODO Headline<point>"
  2895. (let ((org-special-ctrl-a/e '(t . nil))
  2896. (this-command last-command))
  2897. (and (progn (org-beginning-of-line) (bolp))
  2898. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  2899. ;; At an item with special movement, first move after to beginning
  2900. ;; of title, then to the beginning of line, rinse, repeat.
  2901. (should
  2902. (org-test-with-temp-text "- [ ] Item<point>"
  2903. (let ((org-special-ctrl-a/e t))
  2904. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  2905. (progn (org-beginning-of-line) (bolp))
  2906. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2907. ;; At an item with reversed movement, first move to beginning of
  2908. ;; line, then to the beginning of title.
  2909. (should
  2910. (org-test-with-temp-text "- [X] Item<point>"
  2911. (let ((org-special-ctrl-a/e 'reversed)
  2912. (this-command last-command))
  2913. (and (progn (org-beginning-of-line) (bolp))
  2914. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  2915. ;; Leave point before invisible characters at column 0.
  2916. (should
  2917. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2918. (let ((org-special-ctrl-a/e nil))
  2919. (org-beginning-of-line)
  2920. (bolp))))
  2921. (should
  2922. (org-test-with-temp-text "[[http://orgmode.org]]<point>"
  2923. (let ((org-special-ctrl-a/e t))
  2924. (org-beginning-of-line)
  2925. (bolp))))
  2926. (should
  2927. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  2928. (visual-line-mode)
  2929. (org-beginning-of-line)
  2930. (bolp)))
  2931. ;; Special case: Do not error when the buffer contains only a single
  2932. ;; asterisk.
  2933. (should
  2934. (org-test-with-temp-text "*<point>"
  2935. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  2936. (should
  2937. (org-test-with-temp-text "*<point>"
  2938. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  2939. (ert-deftest test-org/end-of-line ()
  2940. "Test `org-end-of-line' specifications."
  2941. ;; Standard test.
  2942. (should
  2943. (org-test-with-temp-text "Some text\nSome other text"
  2944. (org-end-of-line)
  2945. (eolp)))
  2946. ;; With `visual-line-mode' active, move to end of visible line.
  2947. ;; However, never go past ellipsis.
  2948. (should-not
  2949. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  2950. (visual-line-mode)
  2951. (dotimes (i 1000) (insert "very "))
  2952. (goto-char (point-min))
  2953. (org-end-of-line)
  2954. (eolp)))
  2955. (should-not
  2956. (org-test-with-temp-text "* A short headline\nSome contents"
  2957. (visual-line-mode)
  2958. (org-overview)
  2959. (org-end-of-line)
  2960. (eobp)))
  2961. ;; In a wide headline, with `visual-line-mode', prefer going to end
  2962. ;; of visible line if tags, or end of line, are farther.
  2963. (should-not
  2964. (org-test-with-temp-text "* A <point>long headline"
  2965. (visual-line-mode)
  2966. (dotimes (i 1000) (insert "very "))
  2967. (goto-char (point-min))
  2968. (org-end-of-line)
  2969. (eolp)))
  2970. (should-not
  2971. (org-test-with-temp-text "* A <point>long headline :tag:"
  2972. (visual-line-mode)
  2973. (dotimes (i 1000) (insert "very "))
  2974. (goto-char (point-min))
  2975. (org-end-of-line)
  2976. (eolp)))
  2977. ;; At an headline without special movement, go to end of line.
  2978. ;; However, never go past ellipsis.
  2979. (should
  2980. (org-test-with-temp-text "* Headline2b :tag:\n"
  2981. (let ((org-special-ctrl-a/e nil))
  2982. (and (progn (org-end-of-line) (eolp))
  2983. (progn (org-end-of-line) (eolp))))))
  2984. (should
  2985. (org-test-with-temp-text "* Headline2b :tag:\n"
  2986. (let ((org-special-ctrl-a/e '(t . nil)))
  2987. (and (progn (org-end-of-line) (eolp))
  2988. (progn (org-end-of-line) (eolp))))))
  2989. (should
  2990. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  2991. (org-overview)
  2992. (let ((org-special-ctrl-a/e nil))
  2993. (org-end-of-line)
  2994. (= 1 (line-beginning-position)))))
  2995. ;; At an headline with special movement, first move before tags,
  2996. ;; then at the end of line, rinse, repeat. However, never go past
  2997. ;; ellipsis.
  2998. (should
  2999. (org-test-with-temp-text "* Headline1 :tag:\n"
  3000. (let ((org-special-ctrl-a/e t))
  3001. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3002. (progn (org-end-of-line) (eolp))
  3003. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3004. (should
  3005. (org-test-with-temp-text "* Headline1 :tag:\n"
  3006. (let ((org-special-ctrl-a/e '(nil . t)))
  3007. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3008. (progn (org-end-of-line) (eolp))
  3009. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3010. (should-not
  3011. (org-test-with-temp-text "* Headline1 :tag:\n"
  3012. (let ((org-special-ctrl-a/e '(nil . nil)))
  3013. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3014. (progn (org-end-of-line) (eolp))
  3015. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3016. (should
  3017. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3018. (org-overview)
  3019. (let ((org-special-ctrl-a/e t))
  3020. (org-end-of-line)
  3021. (org-end-of-line)
  3022. (= 1 (line-beginning-position)))))
  3023. ;; At an headline, with reversed movement, first go to end of line,
  3024. ;; then before tags. However, never go past ellipsis.
  3025. (should
  3026. (org-test-with-temp-text "* Headline3 :tag:\n"
  3027. (let ((org-special-ctrl-a/e 'reversed)
  3028. (this-command last-command))
  3029. (and (progn (org-end-of-line) (eolp))
  3030. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3031. (should
  3032. (org-test-with-temp-text "* Headline3 :tag:\n"
  3033. (let ((org-special-ctrl-a/e '(nil . reversed))
  3034. (this-command last-command))
  3035. (and (progn (org-end-of-line) (eolp))
  3036. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3037. (should-not
  3038. (org-test-with-temp-text "* Headline3 :tag:\n"
  3039. (let ((org-special-ctrl-a/e '(nil . t))
  3040. (this-command last-command))
  3041. (and (progn (org-end-of-line) (eolp))
  3042. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3043. (should
  3044. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3045. (org-overview)
  3046. (let ((org-special-ctrl-a/e 'reversed))
  3047. (org-end-of-line)
  3048. (= 1 (line-beginning-position)))))
  3049. ;; At a block without hidden contents.
  3050. (should
  3051. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3052. (progn (org-end-of-line) (eolp))))
  3053. ;; At a block with hidden contents.
  3054. (should-not
  3055. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3056. (let ((org-special-ctrl-a/e t))
  3057. (org-hide-block-toggle)
  3058. (org-end-of-line)
  3059. (eobp))))
  3060. ;; Get past invisible characters at the end of line.
  3061. (should
  3062. (org-test-with-temp-text "[[http://orgmode.org]]"
  3063. (org-end-of-line)
  3064. (eolp))))
  3065. (ert-deftest test-org/open-line ()
  3066. "Test `org-open-line' specifications."
  3067. ;; Call `open-line' outside of tables.
  3068. (should
  3069. (equal "\nText"
  3070. (org-test-with-temp-text "Text"
  3071. (org-open-line 1)
  3072. (buffer-string))))
  3073. ;; At a table, create a row above.
  3074. (should
  3075. (equal "\n| |\n| a |"
  3076. (org-test-with-temp-text "\n<point>| a |"
  3077. (org-open-line 1)
  3078. (buffer-string))))
  3079. ;; At the very first character of the buffer, also call `open-line'.
  3080. (should
  3081. (equal "\n| a |"
  3082. (org-test-with-temp-text "| a |"
  3083. (org-open-line 1)
  3084. (buffer-string))))
  3085. ;; Narrowing does not count.
  3086. (should
  3087. (equal "Text\n| |\n| a |"
  3088. (org-test-with-temp-text "Text\n<point>| a |"
  3089. (narrow-to-region (point) (point-max))
  3090. (org-open-line 1)
  3091. (widen)
  3092. (buffer-string)))))
  3093. (ert-deftest test-org/forward-sentence ()
  3094. "Test `org-forward-sentence' specifications."
  3095. ;; At the end of a table cell, move to the end of the next one.
  3096. (should
  3097. (org-test-with-temp-text "| a<point> | b |"
  3098. (org-forward-sentence)
  3099. (looking-at " |$")))
  3100. ;; Elsewhere in a cell, move to its end.
  3101. (should
  3102. (org-test-with-temp-text "| a<point>c | b |"
  3103. (org-forward-sentence)
  3104. (looking-at " | b |$")))
  3105. ;; Otherwise, simply call `forward-sentence'.
  3106. (should
  3107. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3108. (org-forward-sentence)
  3109. (looking-at " Sentence 2.")))
  3110. (should
  3111. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3112. (org-forward-sentence)
  3113. (org-forward-sentence)
  3114. (eobp)))
  3115. ;; At the end of an element, jump to the next one, without stopping
  3116. ;; on blank lines in-between.
  3117. (should
  3118. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3119. (org-forward-sentence)
  3120. (eobp))))
  3121. (ert-deftest test-org/backward-sentence ()
  3122. "Test `org-backward-sentence' specifications."
  3123. ;; At the beginning of a table cell, move to the beginning of the
  3124. ;; previous one.
  3125. (should
  3126. (org-test-with-temp-text "| a | <point>b |"
  3127. (org-backward-sentence)
  3128. (looking-at "a | b |$")))
  3129. ;; Elsewhere in a cell, move to its beginning.
  3130. (should
  3131. (org-test-with-temp-text "| a | b<point>c |"
  3132. (org-backward-sentence)
  3133. (looking-at "bc |$")))
  3134. ;; Otherwise, simply call `backward-sentence'.
  3135. (should
  3136. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3137. (org-backward-sentence)
  3138. (looking-at "Sentence 2.")))
  3139. (should
  3140. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  3141. (org-backward-sentence)
  3142. (org-backward-sentence)
  3143. (bobp)))
  3144. ;; Make sure to hit the beginning of a sentence on the same line as
  3145. ;; an item.
  3146. (should
  3147. (org-test-with-temp-text "- Line 1\n line <point>2."
  3148. (org-backward-sentence)
  3149. (looking-at "Line 1"))))
  3150. (ert-deftest test-org/forward-paragraph ()
  3151. "Test `org-forward-paragraph' specifications."
  3152. ;; At end of buffer, return an error.
  3153. (should-error
  3154. (org-test-with-temp-text "Paragraph"
  3155. (goto-char (point-max))
  3156. (org-forward-paragraph)))
  3157. ;; Standard test.
  3158. (should
  3159. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3160. (org-forward-paragraph)
  3161. (looking-at "P2")))
  3162. ;; Ignore depth.
  3163. (should
  3164. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n#+END_CENTER\nP2"
  3165. (org-forward-paragraph)
  3166. (looking-at "P1")))
  3167. ;; Do not enter elements with invisible contents.
  3168. (should
  3169. (org-test-with-temp-text "#+BEGIN_CENTER\nP1\n\nP2\n#+END_CENTER\nP3"
  3170. (org-hide-block-toggle)
  3171. (org-forward-paragraph)
  3172. (looking-at "P3")))
  3173. ;; On an affiliated keyword, jump to the beginning of the element.
  3174. (should
  3175. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  3176. (org-forward-paragraph)
  3177. (looking-at "Para")))
  3178. ;; On an item or a footnote definition, move to the second element
  3179. ;; inside, if any.
  3180. (should
  3181. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  3182. (org-forward-paragraph)
  3183. (looking-at " Paragraph")))
  3184. (should
  3185. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  3186. (org-forward-paragraph)
  3187. (looking-at "Paragraph")))
  3188. ;; On an item, or a footnote definition, when the first line is
  3189. ;; empty, move to the first item.
  3190. (should
  3191. (org-test-with-temp-text "- \n\n Paragraph\n- Item2"
  3192. (org-forward-paragraph)
  3193. (looking-at " Paragraph")))
  3194. (should
  3195. (org-test-with-temp-text "[fn:1]\n\nParagraph\n\n[fn:2] Def2"
  3196. (org-forward-paragraph)
  3197. (looking-at "Paragraph")))
  3198. ;; On a table (resp. a property drawer) do not move through table
  3199. ;; rows (resp. node properties).
  3200. (should
  3201. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  3202. (org-forward-paragraph)
  3203. (looking-at "Paragraph")))
  3204. (should
  3205. (org-test-with-temp-text
  3206. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  3207. (org-forward-paragraph)
  3208. (looking-at "Paragraph")))
  3209. ;; On a verse or source block, stop after blank lines.
  3210. (should
  3211. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n#+END_VERSE"
  3212. (org-forward-paragraph)
  3213. (looking-at "L2")))
  3214. (should
  3215. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n#+END_SRC"
  3216. (org-forward-paragraph)
  3217. (looking-at "L2"))))
  3218. (ert-deftest test-org/backward-paragraph ()
  3219. "Test `org-backward-paragraph' specifications."
  3220. ;; Error at beginning of buffer.
  3221. (should-error
  3222. (org-test-with-temp-text "Paragraph"
  3223. (org-backward-paragraph)))
  3224. ;; Regular test.
  3225. (should
  3226. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3227. (goto-char (point-max))
  3228. (org-backward-paragraph)
  3229. (looking-at "P3")))
  3230. (should
  3231. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  3232. (goto-char (point-max))
  3233. (beginning-of-line)
  3234. (org-backward-paragraph)
  3235. (looking-at "P2")))
  3236. ;; Ignore depth.
  3237. (should
  3238. (org-test-with-temp-text "P1\n\n#+BEGIN_CENTER\nP2\n#+END_CENTER\nP3"
  3239. (goto-char (point-max))
  3240. (beginning-of-line)
  3241. (org-backward-paragraph)
  3242. (looking-at "P2")))
  3243. ;; Ignore invisible elements.
  3244. (should
  3245. (org-test-with-temp-text "* H1\n P1\n* H2"
  3246. (org-cycle)
  3247. (goto-char (point-max))
  3248. (beginning-of-line)
  3249. (org-backward-paragraph)
  3250. (bobp)))
  3251. ;; On an affiliated keyword, jump to the first one.
  3252. (should
  3253. (org-test-with-temp-text "P1\n#+name: n\n#+caption: c1\n#+caption: c2\nP2"
  3254. (search-forward "c2")
  3255. (org-backward-paragraph)
  3256. (looking-at "#\\+name")))
  3257. ;; On the second element in an item or a footnote definition, jump
  3258. ;; to item or the definition.
  3259. (should
  3260. (org-test-with-temp-text "- line1\n\n line2"
  3261. (goto-char (point-max))
  3262. (beginning-of-line)
  3263. (org-backward-paragraph)
  3264. (looking-at "- line1")))
  3265. (should
  3266. (org-test-with-temp-text "[fn:1] line1\n\n line2"
  3267. (goto-char (point-max))
  3268. (beginning-of-line)
  3269. (org-backward-paragraph)
  3270. (looking-at "\\[fn:1\\] line1")))
  3271. ;; On a table (resp. a property drawer), ignore table rows
  3272. ;; (resp. node properties).
  3273. (should
  3274. (org-test-with-temp-text "| a | b |\n| c | d |\nP1"
  3275. (goto-char (point-max))
  3276. (beginning-of-line)
  3277. (org-backward-paragraph)
  3278. (bobp)))
  3279. (should
  3280. (org-test-with-temp-text "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  3281. (org-backward-paragraph)
  3282. (looking-at ":PROPERTIES:")))
  3283. ;; On a source or verse block, stop before blank lines.
  3284. (should
  3285. (org-test-with-temp-text "#+BEGIN_VERSE\nL1\n\nL2\n\nL3\n#+END_VERSE"
  3286. (search-forward "L3")
  3287. (beginning-of-line)
  3288. (org-backward-paragraph)
  3289. (looking-at "L2")))
  3290. (should
  3291. (org-test-with-temp-text "#+BEGIN_SRC\nL1\n\nL2\n\nL3#+END_SRC"
  3292. (search-forward "L3")
  3293. (beginning-of-line)
  3294. (org-backward-paragraph)
  3295. (looking-at "L2"))))
  3296. (ert-deftest test-org/forward-element ()
  3297. "Test `org-forward-element' specifications."
  3298. ;; 1. At EOB: should error.
  3299. (org-test-with-temp-text "Some text\n"
  3300. (goto-char (point-max))
  3301. (should-error (org-forward-element)))
  3302. ;; 2. Standard move: expected to ignore blank lines.
  3303. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  3304. (org-forward-element)
  3305. (should (looking-at (regexp-quote "Second paragraph."))))
  3306. ;; 3. Headline tests.
  3307. (org-test-with-temp-text "
  3308. * Head 1
  3309. ** Head 1.1
  3310. *** Head 1.1.1
  3311. ** Head 1.2"
  3312. ;; 3.1. At an headline beginning: move to next headline at the
  3313. ;; same level.
  3314. (goto-line 3)
  3315. (org-forward-element)
  3316. (should (looking-at (regexp-quote "** Head 1.2")))
  3317. ;; 3.2. At an headline beginning: move to parent headline if no
  3318. ;; headline at the same level.
  3319. (goto-line 3)
  3320. (org-forward-element)
  3321. (should (looking-at (regexp-quote "** Head 1.2"))))
  3322. ;; 4. Greater element tests.
  3323. (org-test-with-temp-text
  3324. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  3325. ;; 4.1. At a greater element: expected to skip contents.
  3326. (org-forward-element)
  3327. (should (looking-at (regexp-quote "Outside.")))
  3328. ;; 4.2. At the end of greater element contents: expected to skip
  3329. ;; to the end of the greater element.
  3330. (goto-line 2)
  3331. (org-forward-element)
  3332. (should (looking-at (regexp-quote "Outside."))))
  3333. ;; 5. List tests.
  3334. (org-test-with-temp-text "
  3335. - item1
  3336. - sub1
  3337. - sub2
  3338. - sub3
  3339. Inner paragraph.
  3340. - item2
  3341. Outside."
  3342. ;; 5.1. At list top point: expected to move to the element after
  3343. ;; the list.
  3344. (goto-line 2)
  3345. (org-forward-element)
  3346. (should (looking-at (regexp-quote "Outside.")))
  3347. ;; 5.2. Special case: at the first line of a sub-list, but not at
  3348. ;; beginning of line, move to next item.
  3349. (goto-line 2)
  3350. (forward-char)
  3351. (org-forward-element)
  3352. (should (looking-at "- item2"))
  3353. (goto-line 4)
  3354. (forward-char)
  3355. (org-forward-element)
  3356. (should (looking-at " - sub2"))
  3357. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  3358. (goto-line 4)
  3359. (org-forward-element)
  3360. (should (looking-at (regexp-quote " Inner paragraph.")))
  3361. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  3362. (goto-line 8)
  3363. (org-forward-element)
  3364. (should (looking-at (regexp-quote " Inner paragraph.")))
  3365. ;; 5.5. At an item: expected to move to next item, if any.
  3366. (goto-line 6)
  3367. (org-forward-element)
  3368. (should (looking-at " - sub3"))))
  3369. (ert-deftest test-org/backward-element ()
  3370. "Test `org-backward-element' specifications."
  3371. ;; 1. Should error at BOB.
  3372. (org-test-with-temp-text " \nParagraph."
  3373. (should-error (org-backward-element)))
  3374. ;; 2. Should move at BOB when called on the first element in buffer.
  3375. (should
  3376. (org-test-with-temp-text "\n#+TITLE: test"
  3377. (progn (forward-line)
  3378. (org-backward-element)
  3379. (bobp))))
  3380. ;; 3. Not at the beginning of an element: move at its beginning.
  3381. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3382. (goto-line 3)
  3383. (end-of-line)
  3384. (org-backward-element)
  3385. (should (looking-at (regexp-quote "Paragraph2."))))
  3386. ;; 4. Headline tests.
  3387. (org-test-with-temp-text "
  3388. * Head 1
  3389. ** Head 1.1
  3390. *** Head 1.1.1
  3391. ** Head 1.2"
  3392. ;; 4.1. At an headline beginning: move to previous headline at the
  3393. ;; same level.
  3394. (goto-line 5)
  3395. (org-backward-element)
  3396. (should (looking-at (regexp-quote "** Head 1.1")))
  3397. ;; 4.2. At an headline beginning: move to parent headline if no
  3398. ;; headline at the same level.
  3399. (goto-line 3)
  3400. (org-backward-element)
  3401. (should (looking-at (regexp-quote "* Head 1")))
  3402. ;; 4.3. At the first top-level headline: should error.
  3403. (goto-line 2)
  3404. (should-error (org-backward-element)))
  3405. ;; 5. At beginning of first element inside a greater element:
  3406. ;; expected to move to greater element's beginning.
  3407. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  3408. (goto-line 3)
  3409. (org-backward-element)
  3410. (should (looking-at "#\\+BEGIN_CENTER")))
  3411. ;; 6. At the beginning of the first element in a section: should
  3412. ;; move back to headline, if any.
  3413. (should
  3414. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  3415. (progn (goto-char (point-max))
  3416. (beginning-of-line)
  3417. (org-backward-element)
  3418. (org-at-heading-p))))
  3419. ;; 7. List tests.
  3420. (org-test-with-temp-text "
  3421. - item1
  3422. - sub1
  3423. - sub2
  3424. - sub3
  3425. Inner paragraph.
  3426. - item2
  3427. Outside."
  3428. ;; 7.1. At beginning of sub-list: expected to move to the
  3429. ;; paragraph before it.
  3430. (goto-line 4)
  3431. (org-backward-element)
  3432. (should (looking-at "item1"))
  3433. ;; 7.2. At an item in a list: expected to move at previous item.
  3434. (goto-line 8)
  3435. (org-backward-element)
  3436. (should (looking-at " - sub2"))
  3437. (goto-line 12)
  3438. (org-backward-element)
  3439. (should (looking-at "- item1"))
  3440. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  3441. ;; beginning.
  3442. (goto-line 10)
  3443. (org-backward-element)
  3444. (should (looking-at " - sub1"))
  3445. (goto-line 15)
  3446. (org-backward-element)
  3447. (should (looking-at "- item1"))
  3448. ;; 7.4. At blank-lines before list end: expected to move to top
  3449. ;; item.
  3450. (goto-line 14)
  3451. (org-backward-element)
  3452. (should (looking-at "- item1"))))
  3453. (ert-deftest test-org/up-element ()
  3454. "Test `org-up-element' specifications."
  3455. ;; 1. At BOB or with no surrounding element: should error.
  3456. (org-test-with-temp-text "Paragraph."
  3457. (should-error (org-up-element)))
  3458. (org-test-with-temp-text "* Head1\n* Head2"
  3459. (goto-line 2)
  3460. (should-error (org-up-element)))
  3461. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  3462. (goto-line 3)
  3463. (should-error (org-up-element)))
  3464. ;; 2. At an headline: move to parent headline.
  3465. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  3466. (goto-line 3)
  3467. (org-up-element)
  3468. (should (looking-at "\\* Head1")))
  3469. ;; 3. Inside a greater element: move to greater element beginning.
  3470. (org-test-with-temp-text
  3471. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  3472. (goto-line 3)
  3473. (org-up-element)
  3474. (should (looking-at "#\\+BEGIN_CENTER")))
  3475. ;; 4. List tests.
  3476. (org-test-with-temp-text "* Top
  3477. - item1
  3478. - sub1
  3479. - sub2
  3480. Paragraph within sub2.
  3481. - item2"
  3482. ;; 4.1. Within an item: move to the item beginning.
  3483. (goto-line 8)
  3484. (org-up-element)
  3485. (should (looking-at " - sub2"))
  3486. ;; 4.2. At an item in a sub-list: move to parent item.
  3487. (goto-line 4)
  3488. (org-up-element)
  3489. (should (looking-at "- item1"))
  3490. ;; 4.3. At an item in top list: move to beginning of whole list.
  3491. (goto-line 10)
  3492. (org-up-element)
  3493. (should (looking-at "- item1"))
  3494. ;; 4.4. Special case. At very top point: should move to parent of
  3495. ;; list.
  3496. (goto-line 2)
  3497. (org-up-element)
  3498. (should (looking-at "\\* Top"))))
  3499. (ert-deftest test-org/down-element ()
  3500. "Test `org-down-element' specifications."
  3501. ;; Error when the element hasn't got a recursive type.
  3502. (org-test-with-temp-text "Paragraph."
  3503. (should-error (org-down-element)))
  3504. ;; Error when the element has no contents
  3505. (org-test-with-temp-text "* Headline"
  3506. (should-error (org-down-element)))
  3507. ;; When at a plain-list, move to first item.
  3508. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  3509. (goto-line 2)
  3510. (org-down-element)
  3511. (should (looking-at " - Item 1.1")))
  3512. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  3513. (org-down-element)
  3514. (should (looking-at " Item 1")))
  3515. ;; When at a table, move to first row
  3516. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  3517. (org-down-element)
  3518. (should (looking-at " a | b |")))
  3519. ;; Otherwise, move inside the greater element.
  3520. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  3521. (org-down-element)
  3522. (should (looking-at "Paragraph"))))
  3523. (ert-deftest test-org/drag-element-backward ()
  3524. "Test `org-drag-element-backward' specifications."
  3525. ;; Standard test.
  3526. (should
  3527. (equal
  3528. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  3529. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  3530. (org-drag-element-backward)
  3531. (buffer-string))))
  3532. (should
  3533. (equal
  3534. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  3535. (org-test-with-temp-text
  3536. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  3537. (org-drag-element-backward)
  3538. (buffer-string))))
  3539. ;; Preserve blank lines.
  3540. (should
  3541. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  3542. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  3543. (org-drag-element-backward)
  3544. (buffer-string))))
  3545. ;; Preserve column.
  3546. (should
  3547. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  3548. (org-drag-element-backward)
  3549. (looking-at-p "2")))
  3550. ;; Error when trying to move first element of buffer.
  3551. (should-error
  3552. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3553. (org-drag-element-backward))
  3554. :type 'user-error)
  3555. ;; Error when trying to swap nested elements.
  3556. (should-error
  3557. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  3558. (org-drag-element-backward))
  3559. :type 'user-error)
  3560. ;; Error when trying to swap an headline element and a non-headline
  3561. ;; element.
  3562. (should-error
  3563. (org-test-with-temp-text "Test.\n<point>* Head 1"
  3564. (org-drag-element-backward))
  3565. :type 'error)
  3566. ;; Error when called before first element.
  3567. (should-error
  3568. (org-test-with-temp-text "\n<point>"
  3569. (org-drag-element-backward))
  3570. :type 'user-error)
  3571. ;; Preserve visibility of elements and their contents.
  3572. (should
  3573. (equal '((63 . 82) (26 . 48))
  3574. (org-test-with-temp-text "
  3575. #+BEGIN_CENTER
  3576. Text.
  3577. #+END_CENTER
  3578. - item 1
  3579. #+BEGIN_QUOTE
  3580. Text.
  3581. #+END_QUOTE"
  3582. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3583. (search-backward "- item 1")
  3584. (org-drag-element-backward)
  3585. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3586. (overlays-in (point-min) (point-max))))))
  3587. ;; Pathological case: handle call with point in blank lines right
  3588. ;; after a headline.
  3589. (should
  3590. (equal "* H2\n* H1\nText\n\n"
  3591. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  3592. (org-drag-element-backward)
  3593. (buffer-string)))))
  3594. (ert-deftest test-org/drag-element-forward ()
  3595. "Test `org-drag-element-forward' specifications."
  3596. ;; 1. Error when trying to move first element of buffer.
  3597. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  3598. (goto-line 3)
  3599. (should-error (org-drag-element-forward)))
  3600. ;; 2. Error when trying to swap nested elements.
  3601. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  3602. (forward-line)
  3603. (should-error (org-drag-element-forward)))
  3604. ;; 3. Error when trying to swap a non-headline element and an
  3605. ;; headline.
  3606. (org-test-with-temp-text "Test.\n* Head 1"
  3607. (should-error (org-drag-element-forward)))
  3608. ;; 4. Error when called before first element.
  3609. (should-error
  3610. (org-test-with-temp-text "\n"
  3611. (forward-line)
  3612. (org-drag-element-backward))
  3613. :type 'user-error)
  3614. ;; 5. Otherwise, swap elements, preserving column and blank lines
  3615. ;; between elements.
  3616. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  3617. (search-forward "graph")
  3618. (org-drag-element-forward)
  3619. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  3620. (should (looking-at " 1")))
  3621. ;; 5. Preserve visibility of elements and their contents.
  3622. (org-test-with-temp-text "
  3623. #+BEGIN_CENTER
  3624. Text.
  3625. #+END_CENTER
  3626. - item 1
  3627. #+BEGIN_QUOTE
  3628. Text.
  3629. #+END_QUOTE"
  3630. (while (search-forward "BEGIN_" nil t) (org-cycle))
  3631. (search-backward "#+BEGIN_CENTER")
  3632. (org-drag-element-forward)
  3633. (should
  3634. (equal
  3635. '((63 . 82) (26 . 48))
  3636. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  3637. (overlays-in (point-min) (point-max)))))))
  3638. (ert-deftest test-org/next-block ()
  3639. "Test `org-next-block' specifications."
  3640. ;; Regular test.
  3641. (should
  3642. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  3643. (org-next-block 1)
  3644. (looking-at "#\\+BEGIN_CENTER")))
  3645. ;; Ignore case.
  3646. (should
  3647. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  3648. (let ((case-fold-search nil))
  3649. (org-next-block 1)
  3650. (looking-at "#\\+begin_center"))))
  3651. ;; Ignore current line.
  3652. (should
  3653. (org-test-with-temp-text
  3654. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  3655. (org-next-block 1)
  3656. (looking-at "#\\+BEGIN_CENTER")))
  3657. ;; Throw an error when no block is found.
  3658. (should-error
  3659. (org-test-with-temp-text "Paragraph"
  3660. (org-next-block 1)))
  3661. ;; With an argument, skip many blocks at once.
  3662. (should
  3663. (org-test-with-temp-text
  3664. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3665. (org-next-block 2)
  3666. (looking-at "#\\+BEGIN_QUOTE")))
  3667. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3668. (should
  3669. (org-test-with-temp-text
  3670. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  3671. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3672. (looking-at "#\\+BEGIN_QUOTE")))
  3673. ;; Optional argument is also case-insensitive.
  3674. (should
  3675. (org-test-with-temp-text
  3676. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  3677. (let ((case-fold-search nil))
  3678. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  3679. (looking-at "#\\+begin_quote")))))
  3680. (ert-deftest test-org/previous-block ()
  3681. "Test `org-previous-block' specifications."
  3682. ;; Regular test.
  3683. (should
  3684. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  3685. (org-previous-block 1)
  3686. (looking-at "#\\+BEGIN_CENTER")))
  3687. ;; Ignore case.
  3688. (should
  3689. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  3690. (let ((case-fold-search nil))
  3691. (org-previous-block 1)
  3692. (looking-at "#\\+begin_center"))))
  3693. ;; Ignore current line.
  3694. (should
  3695. (org-test-with-temp-text
  3696. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  3697. (org-previous-block 1)
  3698. (looking-at "#\\+BEGIN_QUOTE")))
  3699. ;; Throw an error when no block is found.
  3700. (should-error
  3701. (org-test-with-temp-text "Paragraph<point>"
  3702. (org-previous-block 1)))
  3703. ;; With an argument, skip many blocks at once.
  3704. (should
  3705. (org-test-with-temp-text
  3706. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3707. (org-previous-block 2)
  3708. (looking-at "#\\+BEGIN_CENTER")))
  3709. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  3710. (should
  3711. (org-test-with-temp-text
  3712. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  3713. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3714. (looking-at "#\\+BEGIN_QUOTE")))
  3715. ;; Optional argument is also case-insensitive.
  3716. (should
  3717. (org-test-with-temp-text
  3718. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  3719. (let ((case-fold-search nil))
  3720. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  3721. (looking-at "#\\+begin_quote")))))
  3722. ;;; Outline structure
  3723. (ert-deftest test-org/demote ()
  3724. "Test `org-demote' specifications."
  3725. ;; Add correct number of stars according to `org-odd-levels-only'.
  3726. (should
  3727. (= 2
  3728. (org-test-with-temp-text "* H"
  3729. (let ((org-odd-levels-only nil)) (org-demote))
  3730. (org-current-level))))
  3731. (should
  3732. (= 3
  3733. (org-test-with-temp-text "* H"
  3734. (let ((org-odd-levels-only t)) (org-demote))
  3735. (org-current-level))))
  3736. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3737. (should
  3738. (org-test-with-temp-text "* H :tag:"
  3739. (let ((org-tags-column 10)
  3740. (org-auto-align-tags t)
  3741. (org-odd-levels-only nil))
  3742. (org-demote))
  3743. (org-move-to-column 10)
  3744. (looking-at-p ":tag:$")))
  3745. (should-not
  3746. (org-test-with-temp-text "* H :tag:"
  3747. (let ((org-tags-column 10)
  3748. (org-auto-align-tags nil)
  3749. (org-odd-levels-only nil))
  3750. (org-demote))
  3751. (org-move-to-column 10)
  3752. (looking-at-p ":tag:$")))
  3753. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3754. ;; info and property drawers accordingly.
  3755. (should
  3756. (= 3
  3757. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3758. (let ((org-odd-levels-only nil)
  3759. (org-adapt-indentation t))
  3760. (org-demote))
  3761. (forward-line)
  3762. (org-get-indentation))))
  3763. (should
  3764. (= 3
  3765. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3766. (let ((org-odd-levels-only nil)
  3767. (org-adapt-indentation t))
  3768. (org-demote))
  3769. (forward-line)
  3770. (org-get-indentation))))
  3771. (should-not
  3772. (= 3
  3773. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  3774. (let ((org-odd-levels-only nil)
  3775. (org-adapt-indentation nil))
  3776. (org-demote))
  3777. (forward-line)
  3778. (org-get-indentation))))
  3779. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3780. ;; section accordingly. Ignore, however, footnote definitions and
  3781. ;; inlinetasks boundaries.
  3782. (should
  3783. (= 3
  3784. (org-test-with-temp-text "* H\n Paragraph"
  3785. (let ((org-odd-levels-only nil)
  3786. (org-adapt-indentation t))
  3787. (org-demote))
  3788. (forward-line)
  3789. (org-get-indentation))))
  3790. (should
  3791. (= 2
  3792. (org-test-with-temp-text "* H\n Paragraph"
  3793. (let ((org-odd-levels-only nil)
  3794. (org-adapt-indentation nil))
  3795. (org-demote))
  3796. (forward-line)
  3797. (org-get-indentation))))
  3798. (should
  3799. (zerop
  3800. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  3801. (let ((org-odd-levels-only nil)
  3802. (org-adapt-indentation t))
  3803. (org-demote))
  3804. (goto-char (point-max))
  3805. (org-get-indentation))))
  3806. (should
  3807. (= 3
  3808. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  3809. (let ((org-odd-levels-only nil)
  3810. (org-adapt-indentation t))
  3811. (org-demote))
  3812. (goto-char (point-max))
  3813. (org-get-indentation))))
  3814. (when (featurep 'org-inlinetask)
  3815. (should
  3816. (zerop
  3817. (let ((org-inlinetask-min-level 5)
  3818. (org-adapt-indentation t))
  3819. (org-test-with-temp-text "* H\n***** I\n***** END"
  3820. (org-demote)
  3821. (forward-line)
  3822. (org-get-indentation))))))
  3823. (when (featurep 'org-inlinetask)
  3824. (should
  3825. (= 3
  3826. (let ((org-inlinetask-min-level 5)
  3827. (org-adapt-indentation t))
  3828. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  3829. (org-demote)
  3830. (forward-line 2)
  3831. (org-get-indentation))))))
  3832. ;; Ignore contents of source blocks or example blocks when
  3833. ;; indentation should be preserved (through
  3834. ;; `org-src-preserve-indentation' or "-i" flag).
  3835. (should-not
  3836. (zerop
  3837. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3838. (let ((org-adapt-indentation t)
  3839. (org-src-preserve-indentation nil))
  3840. (org-demote))
  3841. (forward-line 2)
  3842. (org-get-indentation))))
  3843. (should
  3844. (zerop
  3845. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  3846. (let ((org-adapt-indentation t)
  3847. (org-src-preserve-indentation t))
  3848. (org-demote))
  3849. (forward-line 2)
  3850. (org-get-indentation))))
  3851. (should
  3852. (zerop
  3853. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  3854. (let ((org-adapt-indentation t)
  3855. (org-src-preserve-indentation t))
  3856. (org-demote))
  3857. (forward-line 2)
  3858. (org-get-indentation))))
  3859. (should
  3860. (zerop
  3861. (org-test-with-temp-text
  3862. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  3863. (let ((org-adapt-indentation t)
  3864. (org-src-preserve-indentation nil))
  3865. (org-demote))
  3866. (forward-line 2)
  3867. (org-get-indentation)))))
  3868. (ert-deftest test-org/promote ()
  3869. "Test `org-promote' specifications."
  3870. ;; Return an error if headline is to be promoted to level 0, unless
  3871. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  3872. ;; headline becomes a comment.
  3873. (should-error
  3874. (org-test-with-temp-text "* H"
  3875. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  3876. (should
  3877. (equal "# H"
  3878. (org-test-with-temp-text "* H"
  3879. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  3880. (buffer-string))))
  3881. ;; Remove correct number of stars according to
  3882. ;; `org-odd-levels-only'.
  3883. (should
  3884. (= 2
  3885. (org-test-with-temp-text "*** H"
  3886. (let ((org-odd-levels-only nil)) (org-promote))
  3887. (org-current-level))))
  3888. (should
  3889. (= 1
  3890. (org-test-with-temp-text "*** H"
  3891. (let ((org-odd-levels-only t)) (org-promote))
  3892. (org-current-level))))
  3893. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  3894. (should
  3895. (org-test-with-temp-text "** H :tag:"
  3896. (let ((org-tags-column 10)
  3897. (org-auto-align-tags t)
  3898. (org-odd-levels-only nil))
  3899. (org-promote))
  3900. (org-move-to-column 10)
  3901. (looking-at-p ":tag:$")))
  3902. (should-not
  3903. (org-test-with-temp-text "** H :tag:"
  3904. (let ((org-tags-column 10)
  3905. (org-auto-align-tags nil)
  3906. (org-odd-levels-only nil))
  3907. (org-promote))
  3908. (org-move-to-column 10)
  3909. (looking-at-p ":tag:$")))
  3910. ;; When `org-adapt-indentation' is non-nil, always indent planning
  3911. ;; info and property drawers.
  3912. (should
  3913. (= 2
  3914. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3915. (let ((org-odd-levels-only nil)
  3916. (org-adapt-indentation t))
  3917. (org-promote))
  3918. (forward-line)
  3919. (org-get-indentation))))
  3920. (should
  3921. (= 2
  3922. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  3923. (let ((org-odd-levels-only nil)
  3924. (org-adapt-indentation t))
  3925. (org-promote))
  3926. (forward-line)
  3927. (org-get-indentation))))
  3928. (should-not
  3929. (= 2
  3930. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  3931. (let ((org-odd-levels-only nil)
  3932. (org-adapt-indentation nil))
  3933. (org-promote))
  3934. (forward-line)
  3935. (org-get-indentation))))
  3936. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  3937. ;; section accordingly. Ignore, however, footnote definitions and
  3938. ;; inlinetasks boundaries.
  3939. (should
  3940. (= 2
  3941. (org-test-with-temp-text "** H\n Paragraph"
  3942. (let ((org-odd-levels-only nil)
  3943. (org-adapt-indentation t))
  3944. (org-promote))
  3945. (forward-line)
  3946. (org-get-indentation))))
  3947. (should-not
  3948. (= 2
  3949. (org-test-with-temp-text "** H\n Paragraph"
  3950. (let ((org-odd-levels-only nil)
  3951. (org-adapt-indentation nil))
  3952. (org-promote))
  3953. (forward-line)
  3954. (org-get-indentation))))
  3955. (should
  3956. (= 2
  3957. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  3958. (let ((org-odd-levels-only nil)
  3959. (org-adapt-indentation t))
  3960. (org-promote))
  3961. (forward-line)
  3962. (org-get-indentation))))
  3963. (when (featurep 'org-inlinetask)
  3964. (should
  3965. (zerop
  3966. (let ((org-inlinetask-min-level 5)
  3967. (org-adapt-indentation t))
  3968. (org-test-with-temp-text "** H\n***** I\n***** END"
  3969. (org-promote)
  3970. (forward-line)
  3971. (org-get-indentation))))))
  3972. (when (featurep 'org-inlinetask)
  3973. (should
  3974. (= 2
  3975. (let ((org-inlinetask-min-level 5)
  3976. (org-adapt-indentation t))
  3977. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  3978. (org-promote)
  3979. (forward-line 2)
  3980. (org-get-indentation))))))
  3981. ;; Give up shifting if it would break document's structure
  3982. ;; otherwise.
  3983. (should
  3984. (= 3
  3985. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  3986. (let ((org-odd-levels-only nil)
  3987. (org-adapt-indentation t))
  3988. (org-promote))
  3989. (forward-line)
  3990. (org-get-indentation))))
  3991. (should
  3992. (= 3
  3993. (org-test-with-temp-text "** H\n Paragraph\n * list."
  3994. (let ((org-odd-levels-only nil)
  3995. (org-adapt-indentation t))
  3996. (org-promote))
  3997. (forward-line)
  3998. (org-get-indentation))))
  3999. ;; Ignore contents of source blocks or example blocks when
  4000. ;; indentation should be preserved (through
  4001. ;; `org-src-preserve-indentation' or "-i" flag).
  4002. (should-not
  4003. (zerop
  4004. (org-test-with-temp-text
  4005. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4006. (let ((org-adapt-indentation t)
  4007. (org-src-preserve-indentation nil)
  4008. (org-odd-levels-only nil))
  4009. (org-promote))
  4010. (forward-line)
  4011. (org-get-indentation))))
  4012. (should
  4013. (zerop
  4014. (org-test-with-temp-text
  4015. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  4016. (let ((org-adapt-indentation t)
  4017. (org-src-preserve-indentation t)
  4018. (org-odd-levels-only nil))
  4019. (org-promote))
  4020. (forward-line)
  4021. (org-get-indentation))))
  4022. (should
  4023. (zerop
  4024. (org-test-with-temp-text
  4025. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  4026. (let ((org-adapt-indentation t)
  4027. (org-src-preserve-indentation t)
  4028. (org-odd-levels-only nil))
  4029. (org-promote))
  4030. (forward-line)
  4031. (org-get-indentation))))
  4032. (should
  4033. (zerop
  4034. (org-test-with-temp-text
  4035. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  4036. (let ((org-adapt-indentation t)
  4037. (org-src-preserve-indentation nil)
  4038. (org-odd-levels-only nil))
  4039. (org-promote))
  4040. (forward-line)
  4041. (org-get-indentation)))))
  4042. ;;; Planning
  4043. (ert-deftest test-org/at-planning-p ()
  4044. "Test `org-at-planning-p' specifications."
  4045. ;; Regular test.
  4046. (should
  4047. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  4048. (org-at-planning-p)))
  4049. (should-not
  4050. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  4051. (org-at-planning-p)))
  4052. ;; Correctly find planning attached to inlinetasks.
  4053. (when (featurep 'org-inlinetask)
  4054. (should
  4055. (org-test-with-temp-text
  4056. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  4057. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4058. (should-not
  4059. (org-test-with-temp-text
  4060. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4061. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4062. (should-not
  4063. (org-test-with-temp-text
  4064. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  4065. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  4066. (should-not
  4067. (org-test-with-temp-text
  4068. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  4069. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  4070. (ert-deftest test-org/add-planning-info ()
  4071. "Test `org-add-planning-info'."
  4072. ;; Create deadline when `org-adapt-indentation' is non-nil.
  4073. (should
  4074. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4075. (org-test-with-temp-text "* H\nParagraph<point>"
  4076. (let ((org-adapt-indentation t))
  4077. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4078. (replace-regexp-in-string
  4079. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4080. nil nil 1))))
  4081. ;; Create deadline when `org-adapt-indentation' is nil.
  4082. (should
  4083. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4084. (org-test-with-temp-text "* H\nParagraph<point>"
  4085. (let ((org-adapt-indentation nil))
  4086. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4087. (replace-regexp-in-string
  4088. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4089. nil nil 1))))
  4090. ;; Update deadline when `org-adapt-indentation' is non-nil.
  4091. (should
  4092. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4093. (org-test-with-temp-text "\
  4094. * H
  4095. DEADLINE: <2015-06-24 Wed>
  4096. Paragraph<point>"
  4097. (let ((org-adapt-indentation t))
  4098. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4099. (replace-regexp-in-string
  4100. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4101. nil nil 1))))
  4102. ;; Update deadline when `org-adapt-indentation' is nil.
  4103. (should
  4104. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4105. (org-test-with-temp-text "\
  4106. * H
  4107. DEADLINE: <2015-06-24 Wed>
  4108. Paragraph<point>"
  4109. (let ((org-adapt-indentation nil))
  4110. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4111. (replace-regexp-in-string
  4112. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4113. nil nil 1))))
  4114. ;; Schedule when `org-adapt-indentation' is non-nil.
  4115. (should
  4116. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  4117. (org-test-with-temp-text "* H\nParagraph<point>"
  4118. (let ((org-adapt-indentation t))
  4119. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4120. (replace-regexp-in-string
  4121. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4122. nil nil 1))))
  4123. ;; Schedule when `org-adapt-indentation' is nil.
  4124. (should
  4125. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  4126. (org-test-with-temp-text "* H\nParagraph<point>"
  4127. (let ((org-adapt-indentation nil))
  4128. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  4129. (replace-regexp-in-string
  4130. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4131. nil nil 1))))
  4132. ;; Add deadline when scheduled.
  4133. (should
  4134. (equal "\
  4135. * H
  4136. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  4137. Paragraph"
  4138. (org-test-with-temp-text "\
  4139. * H
  4140. SCHEDULED: <2015-06-24 Wed>
  4141. Paragraph<point>"
  4142. (let ((org-adapt-indentation t))
  4143. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  4144. (replace-regexp-in-string
  4145. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4146. nil nil 1))))
  4147. ;; Remove middle entry.
  4148. (should
  4149. (equal "\
  4150. * H
  4151. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  4152. Paragraph"
  4153. (org-test-with-temp-text "\
  4154. * H
  4155. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4156. Paragraph<point>"
  4157. (let ((org-adapt-indentation t))
  4158. (org-add-planning-info nil nil 'deadline))
  4159. (replace-regexp-in-string
  4160. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4161. nil nil 1))))
  4162. ;; Remove last entry and then middle entry (order should not
  4163. ;; matter).
  4164. (should
  4165. (equal "\
  4166. * H
  4167. CLOSED: [2015-06-24]
  4168. Paragraph"
  4169. (org-test-with-temp-text "\
  4170. * H
  4171. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  4172. Paragraph<point>"
  4173. (let ((org-adapt-indentation t))
  4174. (org-add-planning-info nil nil 'scheduled 'deadline))
  4175. (replace-regexp-in-string
  4176. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  4177. nil nil 1))))
  4178. ;; Remove closed when `org-adapt-indentation' is non-nil.
  4179. (should
  4180. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4181. (org-test-with-temp-text "\
  4182. * H
  4183. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4184. Paragraph<point>"
  4185. (let ((org-adapt-indentation t))
  4186. (org-add-planning-info nil nil 'closed))
  4187. (replace-regexp-in-string
  4188. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4189. nil nil 1))))
  4190. (should
  4191. (equal "* H\n Paragraph"
  4192. (org-test-with-temp-text "\
  4193. * H
  4194. CLOSED: [2015-06-25 Thu]
  4195. Paragraph<point>"
  4196. (let ((org-adapt-indentation t))
  4197. (org-add-planning-info nil nil 'closed))
  4198. (replace-regexp-in-string
  4199. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4200. nil nil 1))))
  4201. ;; Remove closed when `org-adapt-indentation' is nil.
  4202. (should
  4203. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  4204. (org-test-with-temp-text "\
  4205. * H
  4206. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  4207. Paragraph<point>"
  4208. (let ((org-adapt-indentation nil))
  4209. (org-add-planning-info nil nil 'closed))
  4210. (replace-regexp-in-string
  4211. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4212. nil nil 1))))
  4213. (should
  4214. (equal "* H\nParagraph"
  4215. (org-test-with-temp-text "\
  4216. * H
  4217. CLOSED: [2015-06-25 Thu]
  4218. Paragraph<point>"
  4219. (let ((org-adapt-indentation nil))
  4220. (org-add-planning-info nil nil 'closed))
  4221. (replace-regexp-in-string
  4222. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4223. nil nil 1))))
  4224. ;; Remove closed entry and delete empty line.
  4225. (should
  4226. (equal "\
  4227. * H
  4228. Paragraph"
  4229. (org-test-with-temp-text "\
  4230. * H
  4231. CLOSED: [2015-06-24 Wed]
  4232. Paragraph<point>"
  4233. (let ((org-adapt-indentation t))
  4234. (org-add-planning-info nil nil 'closed))
  4235. (replace-regexp-in-string
  4236. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4237. nil nil 1))))
  4238. ;; Remove one entry and update another.
  4239. (should
  4240. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  4241. (org-test-with-temp-text "\
  4242. * H
  4243. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  4244. Paragraph<point>"
  4245. (let ((org-adapt-indentation t))
  4246. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  4247. (replace-regexp-in-string
  4248. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4249. nil nil 1)))))
  4250. (ert-deftest test-org/deadline ()
  4251. "Test `org-deadline' specifications."
  4252. ;; Insert a new value or replace existing one.
  4253. (should
  4254. (equal "* H\nDEADLINE: <2012-03-29>\n"
  4255. (org-test-with-temp-text "* H"
  4256. (let ((org-adapt-indentation nil)
  4257. (org-last-inserted-timestamp nil))
  4258. (org-deadline nil "<2012-03-29 Tue>"))
  4259. (replace-regexp-in-string
  4260. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4261. nil nil 1))))
  4262. (should
  4263. (equal "* H\nDEADLINE: <2014-03-04>"
  4264. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4265. (let ((org-adapt-indentation nil)
  4266. (org-last-inserted-timestamp nil))
  4267. (org-deadline nil "<2014-03-04 Thu>"))
  4268. (replace-regexp-in-string
  4269. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4270. nil nil 1))))
  4271. ;; Accept delta time, e.g., "+2d".
  4272. (should
  4273. (equal "* H\nDEADLINE: <2015-03-04>\n"
  4274. (cl-letf (((symbol-function 'current-time)
  4275. (lambda (&rest args)
  4276. (apply #'encode-time
  4277. (org-parse-time-string "2014-03-04")))))
  4278. (org-test-with-temp-text "* H"
  4279. (let ((org-adapt-indentation nil)
  4280. (org-last-inserted-timestamp nil))
  4281. (org-deadline nil "+1y"))
  4282. (replace-regexp-in-string
  4283. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4284. ;; Preserve repeater.
  4285. (should
  4286. (equal "* H\nDEADLINE: <2012-03-29 +2y>\n"
  4287. (org-test-with-temp-text "* H"
  4288. (let ((org-adapt-indentation nil)
  4289. (org-last-inserted-timestamp nil))
  4290. (org-deadline nil "<2012-03-29 Tue +2y>"))
  4291. (replace-regexp-in-string
  4292. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4293. ;; Remove CLOSED keyword, if any.
  4294. (should
  4295. (equal "* H\nDEADLINE: <2012-03-29>"
  4296. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4297. (let ((org-adapt-indentation nil)
  4298. (org-last-inserted-timestamp nil))
  4299. (org-deadline nil "<2012-03-29 Tue>"))
  4300. (replace-regexp-in-string
  4301. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4302. ;; With C-u argument, remove DEADLINE keyword.
  4303. (should
  4304. (equal "* H\n"
  4305. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4306. (let ((org-adapt-indentation nil)
  4307. (org-last-inserted-timestamp nil))
  4308. (org-deadline '(4)))
  4309. (buffer-string))))
  4310. (should
  4311. (equal "* H"
  4312. (org-test-with-temp-text "* H"
  4313. (let ((org-adapt-indentation nil)
  4314. (org-last-inserted-timestamp nil))
  4315. (org-deadline '(4)))
  4316. (buffer-string))))
  4317. ;; With C-u C-u argument, prompt for a delay cookie.
  4318. (should
  4319. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  4320. (cl-letf (((symbol-function 'org-read-date)
  4321. (lambda (&rest args)
  4322. (apply #'encode-time
  4323. (org-parse-time-string "2014-03-04")))))
  4324. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  4325. (let ((org-adapt-indentation nil)
  4326. (org-last-inserted-timestamp nil))
  4327. (org-deadline '(16)))
  4328. (buffer-string)))))
  4329. (should-error
  4330. (cl-letf (((symbol-function 'org-read-date)
  4331. (lambda (&rest args)
  4332. (apply #'encode-time
  4333. (org-parse-time-string "2014-03-04")))))
  4334. (org-test-with-temp-text "* H"
  4335. (let ((org-adapt-indentation nil)
  4336. (org-last-inserted-timestamp nil))
  4337. (org-deadline '(16)))
  4338. (buffer-string))))
  4339. ;; When a region is active and
  4340. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4341. ;; same value in all headlines in region.
  4342. (should
  4343. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4344. (org-test-with-temp-text "* H1\n* H2"
  4345. (let ((org-adapt-indentation nil)
  4346. (org-last-inserted-timestamp nil)
  4347. (org-loop-over-headlines-in-active-region t))
  4348. (transient-mark-mode 1)
  4349. (push-mark (point) t t)
  4350. (goto-char (point-max))
  4351. (org-deadline nil "2012-03-29"))
  4352. (replace-regexp-in-string
  4353. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4354. (should-not
  4355. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>\n"
  4356. (org-test-with-temp-text "* H1\n* H2"
  4357. (let ((org-adapt-indentation nil)
  4358. (org-last-inserted-timestamp nil)
  4359. (org-loop-over-headlines-in-active-region nil))
  4360. (transient-mark-mode 1)
  4361. (push-mark (point) t t)
  4362. (goto-char (point-max))
  4363. (org-deadline nil "2012-03-29"))
  4364. (replace-regexp-in-string
  4365. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4366. (ert-deftest test-org/schedule ()
  4367. "Test `org-schedule' specifications."
  4368. ;; Insert a new value or replace existing one.
  4369. (should
  4370. (equal "* H\nSCHEDULED: <2012-03-29>\n"
  4371. (org-test-with-temp-text "* H"
  4372. (let ((org-adapt-indentation nil)
  4373. (org-last-inserted-timestamp nil))
  4374. (org-schedule nil "<2012-03-29 Tue>"))
  4375. (replace-regexp-in-string
  4376. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4377. nil nil 1))))
  4378. (should
  4379. (equal "* H\nSCHEDULED: <2014-03-04>"
  4380. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4381. (let ((org-adapt-indentation nil)
  4382. (org-last-inserted-timestamp nil))
  4383. (org-schedule nil "<2014-03-04 Thu>"))
  4384. (replace-regexp-in-string
  4385. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  4386. nil nil 1))))
  4387. ;; Accept delta time, e.g., "+2d".
  4388. (should
  4389. (equal "* H\nSCHEDULED: <2015-03-04>\n"
  4390. (cl-letf (((symbol-function 'current-time)
  4391. (lambda (&rest args)
  4392. (apply #'encode-time
  4393. (org-parse-time-string "2014-03-04")))))
  4394. (org-test-with-temp-text "* H"
  4395. (let ((org-adapt-indentation nil)
  4396. (org-last-inserted-timestamp nil))
  4397. (org-schedule nil "+1y"))
  4398. (replace-regexp-in-string
  4399. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  4400. ;; Preserve repeater.
  4401. (should
  4402. (equal "* H\nSCHEDULED: <2012-03-29 +2y>\n"
  4403. (org-test-with-temp-text "* H"
  4404. (let ((org-adapt-indentation nil)
  4405. (org-last-inserted-timestamp nil))
  4406. (org-schedule nil "<2012-03-29 Tue +2y>"))
  4407. (replace-regexp-in-string
  4408. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  4409. ;; Remove CLOSED keyword, if any.
  4410. (should
  4411. (equal "* H\nSCHEDULED: <2012-03-29>"
  4412. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  4413. (let ((org-adapt-indentation nil)
  4414. (org-last-inserted-timestamp nil))
  4415. (org-schedule nil "<2012-03-29 Tue>"))
  4416. (replace-regexp-in-string
  4417. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4418. ;; With C-u argument, remove SCHEDULED keyword.
  4419. (should
  4420. (equal "* H\n"
  4421. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4422. (let ((org-adapt-indentation nil)
  4423. (org-last-inserted-timestamp nil))
  4424. (org-schedule '(4)))
  4425. (buffer-string))))
  4426. (should
  4427. (equal "* H"
  4428. (org-test-with-temp-text "* H"
  4429. (let ((org-adapt-indentation nil)
  4430. (org-last-inserted-timestamp nil))
  4431. (org-schedule '(4)))
  4432. (buffer-string))))
  4433. ;; With C-u C-u argument, prompt for a delay cookie.
  4434. (should
  4435. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  4436. (cl-letf (((symbol-function 'org-read-date)
  4437. (lambda (&rest args)
  4438. (apply #'encode-time
  4439. (org-parse-time-string "2014-03-04")))))
  4440. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  4441. (let ((org-adapt-indentation nil)
  4442. (org-last-inserted-timestamp nil))
  4443. (org-schedule '(16)))
  4444. (buffer-string)))))
  4445. (should-error
  4446. (cl-letf (((symbol-function 'org-read-date)
  4447. (lambda (&rest args)
  4448. (apply #'encode-time
  4449. (org-parse-time-string "2014-03-04")))))
  4450. (org-test-with-temp-text "* H"
  4451. (let ((org-adapt-indentation nil)
  4452. (org-last-inserted-timestamp nil))
  4453. (org-schedule '(16)))
  4454. (buffer-string))))
  4455. ;; When a region is active and
  4456. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  4457. ;; same value in all headlines in region.
  4458. (should
  4459. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4460. (org-test-with-temp-text "* H1\n* H2"
  4461. (let ((org-adapt-indentation nil)
  4462. (org-last-inserted-timestamp nil)
  4463. (org-loop-over-headlines-in-active-region t))
  4464. (transient-mark-mode 1)
  4465. (push-mark (point) t t)
  4466. (goto-char (point-max))
  4467. (org-schedule nil "2012-03-29"))
  4468. (replace-regexp-in-string
  4469. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4470. (should-not
  4471. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>\n"
  4472. (org-test-with-temp-text "* H1\n* H2"
  4473. (let ((org-adapt-indentation nil)
  4474. (org-last-inserted-timestamp nil)
  4475. (org-loop-over-headlines-in-active-region nil))
  4476. (transient-mark-mode 1)
  4477. (push-mark (point) t t)
  4478. (goto-char (point-max))
  4479. (org-schedule nil "2012-03-29"))
  4480. (replace-regexp-in-string
  4481. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  4482. (should
  4483. ;; check if a repeater survives re-scheduling.
  4484. (string-match-p
  4485. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  4486. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  4487. (let ((org-adapt-indentation nil)
  4488. (org-last-inserted-timestamp nil))
  4489. (org-schedule nil "2017-02-01"))
  4490. (buffer-string)))))
  4491. ;;; Property API
  4492. (ert-deftest test-org/buffer-property-keys ()
  4493. "Test `org-buffer-property-keys' specifications."
  4494. ;; Retrieve properties accross siblings.
  4495. (should
  4496. (equal '("A" "B")
  4497. (org-test-with-temp-text "
  4498. * H1
  4499. :PROPERTIES:
  4500. :A: 1
  4501. :END:
  4502. * H2
  4503. :PROPERTIES:
  4504. :B: 1
  4505. :END:"
  4506. (org-buffer-property-keys))))
  4507. ;; Retrieve properties accross children.
  4508. (should
  4509. (equal '("A" "B")
  4510. (org-test-with-temp-text "
  4511. * H1
  4512. :PROPERTIES:
  4513. :A: 1
  4514. :END:
  4515. ** H2
  4516. :PROPERTIES:
  4517. :B: 1
  4518. :END:"
  4519. (org-buffer-property-keys))))
  4520. ;; Retrieve muliple properties in the same drawer.
  4521. (should
  4522. (equal '("A" "B")
  4523. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4524. (org-buffer-property-keys))))
  4525. ;; Ignore extension symbol in property name.
  4526. (should
  4527. (equal '("A")
  4528. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4529. (org-buffer-property-keys))))
  4530. ;; With non-nil COLUMNS, extract property names from columns.
  4531. (should
  4532. (equal '("A" "B")
  4533. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  4534. (org-buffer-property-keys nil nil t))))
  4535. (should
  4536. (equal '("A" "B" "COLUMNS")
  4537. (org-test-with-temp-text
  4538. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  4539. (org-buffer-property-keys nil nil t))))
  4540. ;; With non-nil IGNORE-MALFORMED malformed property drawers are silently ignored.
  4541. (should
  4542. (equal '("A")
  4543. (org-test-with-temp-text
  4544. "* a\n:PROPERTIES:\n:A: 1\n:END:\n* b\n:PROPERTIES:\nsome junk here\n:END:\n"
  4545. (org-buffer-property-keys nil nil nil t)))))
  4546. (ert-deftest test-org/property-values ()
  4547. "Test `org-property-values' specifications."
  4548. ;; Regular test.
  4549. (should
  4550. (equal '("2" "1")
  4551. (org-test-with-temp-text
  4552. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  4553. (org-property-values "A"))))
  4554. ;; Ignore empty values.
  4555. (should-not
  4556. (org-test-with-temp-text
  4557. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  4558. (org-property-values "A")))
  4559. ;; Take into consideration extended values.
  4560. (should
  4561. (equal '("1 2")
  4562. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  4563. (org-property-values "A")))))
  4564. (ert-deftest test-org/find-property ()
  4565. "Test `org-find-property' specifications."
  4566. ;; Regular test.
  4567. (should
  4568. (= 1
  4569. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  4570. (org-find-property "prop"))))
  4571. ;; Ignore false positives.
  4572. (should
  4573. (= 27
  4574. (org-test-with-temp-text
  4575. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  4576. (org-find-property "A"))))
  4577. ;; Return first entry found in buffer.
  4578. (should
  4579. (= 1
  4580. (org-test-with-temp-text
  4581. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4582. (org-find-property "A"))))
  4583. ;; Only search visible part of the buffer.
  4584. (should
  4585. (= 31
  4586. (org-test-with-temp-text
  4587. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  4588. (org-narrow-to-subtree)
  4589. (org-find-property "A"))))
  4590. ;; With optional argument, only find entries with a specific value.
  4591. (should-not
  4592. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4593. (org-find-property "A" "2")))
  4594. (should
  4595. (= 31
  4596. (org-test-with-temp-text
  4597. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  4598. (org-find-property "A" "2"))))
  4599. ;; Use "nil" for explicit nil values.
  4600. (should
  4601. (= 31
  4602. (org-test-with-temp-text
  4603. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  4604. (org-find-property "A" "nil")))))
  4605. (ert-deftest test-org/entry-delete ()
  4606. "Test `org-entry-delete' specifications."
  4607. ;; Regular test.
  4608. (should
  4609. (string-match
  4610. " *:PROPERTIES:\n *:B: +2\n *:END:"
  4611. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4612. (org-entry-delete (point) "A")
  4613. (buffer-string))))
  4614. ;; Also remove accumulated properties.
  4615. (should-not
  4616. (string-match
  4617. ":A"
  4618. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  4619. (org-entry-delete (point) "A")
  4620. (buffer-string))))
  4621. ;; When last property is removed, remove the property drawer.
  4622. (should-not
  4623. (string-match
  4624. ":PROPERTIES:"
  4625. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  4626. (org-entry-delete (point) "A")
  4627. (buffer-string))))
  4628. ;; Return a non-nil value when some property was removed.
  4629. (should
  4630. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4631. (org-entry-delete (point) "A")))
  4632. (should-not
  4633. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  4634. (org-entry-delete (point) "C")))
  4635. ;; Special properties cannot be located in a drawer. Allow to
  4636. ;; remove them anyway, in case of user error.
  4637. (should
  4638. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  4639. (org-entry-delete (point) "SCHEDULED"))))
  4640. (ert-deftest test-org/entry-get ()
  4641. "Test `org-entry-get' specifications."
  4642. ;; Regular test.
  4643. (should
  4644. (equal "1"
  4645. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4646. (org-entry-get (point) "A"))))
  4647. ;; Ignore case.
  4648. (should
  4649. (equal "1"
  4650. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4651. (org-entry-get (point) "a"))))
  4652. ;; Handle extended values, both before and after base value.
  4653. (should
  4654. (equal "1 2 3"
  4655. (org-test-with-temp-text
  4656. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4657. (org-entry-get (point) "A"))))
  4658. ;; Empty values are returned as the empty string.
  4659. (should
  4660. (equal ""
  4661. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  4662. (org-entry-get (point) "A"))))
  4663. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  4664. ;; otherwise, return nil.
  4665. (should-not
  4666. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4667. (org-entry-get (point) "A")))
  4668. (should
  4669. (equal "nil"
  4670. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  4671. (org-entry-get (point) "A" nil t))))
  4672. ;; Return nil when no property is found, independently on the
  4673. ;; LITERAL-NIL argument.
  4674. (should-not
  4675. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4676. (org-entry-get (point) "B")))
  4677. (should-not
  4678. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4679. (org-entry-get (point) "B" nil t)))
  4680. ;; Handle inheritance, when allowed. Include extended values and
  4681. ;; possibly global values.
  4682. (should
  4683. (equal
  4684. "1"
  4685. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4686. (org-entry-get (point) "A" t))))
  4687. (should
  4688. (equal
  4689. "1"
  4690. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4691. (let ((org-use-property-inheritance t))
  4692. (org-entry-get (point) "A" 'selective)))))
  4693. (should-not
  4694. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** <point>H2"
  4695. (let ((org-use-property-inheritance nil))
  4696. (org-entry-get (point) "A" 'selective))))
  4697. (should
  4698. (equal
  4699. "1 2"
  4700. (org-test-with-temp-text
  4701. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  4702. (org-entry-get (point-max) "A" t))))
  4703. (should
  4704. (equal "1"
  4705. (org-test-with-temp-text
  4706. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  4707. (org-mode-restart)
  4708. (org-entry-get (point-max) "A" t))))
  4709. (should
  4710. (equal "0 1"
  4711. (org-test-with-temp-text
  4712. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  4713. (org-mode-restart)
  4714. (org-entry-get (point-max) "A" t)))))
  4715. (ert-deftest test-org/entry-properties ()
  4716. "Test `org-entry-properties' specifications."
  4717. ;; Get "ITEM" property.
  4718. (should
  4719. (equal "H"
  4720. (org-test-with-temp-text "* TODO H"
  4721. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  4722. (should
  4723. (equal "H"
  4724. (org-test-with-temp-text "* TODO H"
  4725. (cdr (assoc "ITEM" (org-entry-properties))))))
  4726. ;; Get "TODO" property. TODO keywords are case sensitive.
  4727. (should
  4728. (equal "TODO"
  4729. (org-test-with-temp-text "* TODO H"
  4730. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  4731. (should
  4732. (equal "TODO"
  4733. (org-test-with-temp-text "* TODO H"
  4734. (cdr (assoc "TODO" (org-entry-properties))))))
  4735. (should-not
  4736. (org-test-with-temp-text "* H"
  4737. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4738. (should-not
  4739. (org-test-with-temp-text "* todo H"
  4740. (assoc "TODO" (org-entry-properties nil "TODO"))))
  4741. ;; Get "PRIORITY" property.
  4742. (should
  4743. (equal "A"
  4744. (org-test-with-temp-text "* [#A] H"
  4745. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4746. (should
  4747. (equal "A"
  4748. (org-test-with-temp-text "* [#A] H"
  4749. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  4750. (should
  4751. (equal (char-to-string org-default-priority)
  4752. (org-test-with-temp-text "* H"
  4753. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  4754. ;; Get "FILE" property.
  4755. (should
  4756. (org-test-with-temp-text-in-file "* H\nParagraph"
  4757. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  4758. (buffer-file-name))))
  4759. (should
  4760. (org-test-with-temp-text-in-file "* H\nParagraph"
  4761. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  4762. (buffer-file-name))))
  4763. (should-not
  4764. (org-test-with-temp-text "* H\nParagraph"
  4765. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  4766. ;; Get "TAGS" property.
  4767. (should
  4768. (equal ":tag1:tag2:"
  4769. (org-test-with-temp-text "* H :tag1:tag2:"
  4770. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  4771. (should
  4772. (equal ":tag1:tag2:"
  4773. (org-test-with-temp-text "* H :tag1:tag2:"
  4774. (cdr (assoc "TAGS" (org-entry-properties))))))
  4775. (should-not
  4776. (org-test-with-temp-text "* H"
  4777. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  4778. ;; Get "ALLTAGS" property.
  4779. (should
  4780. (equal ":tag1:tag2:"
  4781. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4782. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  4783. (should
  4784. (equal ":tag1:tag2:"
  4785. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  4786. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  4787. (should-not
  4788. (org-test-with-temp-text "* H"
  4789. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  4790. ;; Get "BLOCKED" property.
  4791. (should
  4792. (equal "t"
  4793. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  4794. (let ((org-enforce-todo-dependencies t)
  4795. (org-blocker-hook
  4796. '(org-block-todo-from-children-or-siblings-or-parent)))
  4797. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4798. (should
  4799. (equal ""
  4800. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  4801. (let ((org-enforce-todo-dependencies t)
  4802. (org-blocker-hook
  4803. '(org-block-todo-from-children-or-siblings-or-parent)))
  4804. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  4805. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  4806. (should
  4807. (equal
  4808. "[2012-03-29 thu.]"
  4809. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4810. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  4811. (should
  4812. (equal
  4813. "[2012-03-29 thu.]"
  4814. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  4815. (cdr (assoc "CLOSED" (org-entry-properties))))))
  4816. (should-not
  4817. (org-test-with-temp-text "* H"
  4818. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  4819. (should
  4820. (equal
  4821. "<2014-03-04 tue.>"
  4822. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4823. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  4824. (should
  4825. (equal
  4826. "<2014-03-04 tue.>"
  4827. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4828. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  4829. (should-not
  4830. (org-test-with-temp-text "* H"
  4831. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  4832. (should
  4833. (equal
  4834. "<2014-03-04 tue.>"
  4835. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4836. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  4837. (should
  4838. (equal
  4839. "<2014-03-04 tue.>"
  4840. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4841. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  4842. (should-not
  4843. (org-test-with-temp-text "* H"
  4844. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  4845. ;; Get "CATEGORY"
  4846. (should
  4847. (equal "cat"
  4848. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4849. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  4850. (should
  4851. (equal "cat"
  4852. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  4853. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4854. (should
  4855. (equal "cat"
  4856. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  4857. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4858. (should
  4859. (equal "cat2"
  4860. (org-test-with-temp-text
  4861. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  4862. "\n"
  4863. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  4864. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  4865. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  4866. (should
  4867. (equal "<2012-03-29 thu.>"
  4868. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  4869. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4870. (should
  4871. (equal "[2012-03-29 thu.]"
  4872. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  4873. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  4874. (should
  4875. (equal "<2012-03-29 thu.>"
  4876. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  4877. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  4878. (should
  4879. (equal "[2014-03-04 tue.]"
  4880. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  4881. (cdr (assoc "TIMESTAMP_IA"
  4882. (org-entry-properties nil "TIMESTAMP_IA"))))))
  4883. (should-not
  4884. (equal "<2012-03-29 thu.>"
  4885. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  4886. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  4887. ;; Get standard properties.
  4888. (should
  4889. (equal "1"
  4890. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4891. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4892. ;; Handle extended properties.
  4893. (should
  4894. (equal "1 2 3"
  4895. (org-test-with-temp-text
  4896. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  4897. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4898. (should
  4899. (equal "1 2 3"
  4900. (org-test-with-temp-text
  4901. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  4902. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  4903. ;; Ignore forbidden (special) properties.
  4904. (should-not
  4905. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  4906. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  4907. (ert-deftest test-org/entry-put ()
  4908. "Test `org-entry-put' specifications."
  4909. ;; Error when not a string or nil.
  4910. (should-error
  4911. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4912. (org-entry-put 1 "test" 2)))
  4913. ;; Error when property name is invalid.
  4914. (should-error
  4915. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4916. (org-entry-put 1 "no space" "value")))
  4917. (should-error
  4918. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  4919. (org-entry-put 1 "" "value")))
  4920. ;; Set "TODO" property.
  4921. (should
  4922. (string-match (regexp-quote " TODO H")
  4923. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4924. (org-entry-put (point) "TODO" "TODO")
  4925. (buffer-string))))
  4926. (should
  4927. (string-match (regexp-quote "* H")
  4928. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  4929. (org-entry-put (point) "TODO" nil)
  4930. (buffer-string))))
  4931. ;; Set "PRIORITY" property.
  4932. (should
  4933. (equal "* [#A] H"
  4934. (org-test-with-temp-text "* [#B] H"
  4935. (org-entry-put (point) "PRIORITY" "A")
  4936. (buffer-string))))
  4937. (should
  4938. (equal "* H"
  4939. (org-test-with-temp-text "* [#B] H"
  4940. (org-entry-put (point) "PRIORITY" nil)
  4941. (buffer-string))))
  4942. ;; Set "SCHEDULED" property.
  4943. (should
  4944. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  4945. (org-test-with-temp-text "* H"
  4946. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  4947. (buffer-string))))
  4948. (should
  4949. (string= "* H\n"
  4950. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4951. (org-entry-put (point) "SCHEDULED" nil)
  4952. (buffer-string))))
  4953. (should
  4954. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  4955. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4956. (org-entry-put (point) "SCHEDULED" "earlier")
  4957. (buffer-string))))
  4958. (should
  4959. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  4960. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  4961. (org-entry-put (point) "SCHEDULED" "later")
  4962. (buffer-string))))
  4963. ;; Set "DEADLINE" property.
  4964. (should
  4965. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  4966. (org-test-with-temp-text "* H"
  4967. (org-entry-put (point) "DEADLINE" "2014-03-04")
  4968. (buffer-string))))
  4969. (should
  4970. (string= "* H\n"
  4971. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4972. (org-entry-put (point) "DEADLINE" nil)
  4973. (buffer-string))))
  4974. (should
  4975. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  4976. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4977. (org-entry-put (point) "DEADLINE" "earlier")
  4978. (buffer-string))))
  4979. (should
  4980. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  4981. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  4982. (org-entry-put (point) "DEADLINE" "later")
  4983. (buffer-string))))
  4984. ;; Set "CATEGORY" property
  4985. (should
  4986. (string-match "^ *:CATEGORY: cat"
  4987. (org-test-with-temp-text "* H"
  4988. (org-entry-put (point) "CATEGORY" "cat")
  4989. (buffer-string))))
  4990. ;; Regular properties, with or without pre-existing drawer.
  4991. (should
  4992. (string-match "^ *:A: +2$"
  4993. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  4994. (org-entry-put (point) "A" "2")
  4995. (buffer-string))))
  4996. (should
  4997. (string-match "^ *:A: +1$"
  4998. (org-test-with-temp-text "* H"
  4999. (org-entry-put (point) "A" "1")
  5000. (buffer-string))))
  5001. ;; Special case: two consecutive headlines.
  5002. (should
  5003. (string-match "\\* A\n *:PROPERTIES:"
  5004. (org-test-with-temp-text "* A\n** B"
  5005. (org-entry-put (point) "A" "1")
  5006. (buffer-string)))))
  5007. (ert-deftest test-org/refresh-properties ()
  5008. "Test `org-refresh-properties' specifications."
  5009. (should
  5010. (equal "1"
  5011. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5012. (org-refresh-properties "A" 'org-test)
  5013. (get-text-property (point) 'org-test))))
  5014. (should-not
  5015. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5016. (org-refresh-properties "B" 'org-test)
  5017. (get-text-property (point) 'org-test)))
  5018. ;; Handle properties only defined with extension syntax, i.e.,
  5019. ;; "PROPERTY+".
  5020. (should
  5021. (equal "1"
  5022. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  5023. (org-refresh-properties "A" 'org-test)
  5024. (get-text-property (point) 'org-test))))
  5025. ;; When property is inherited, add text property to the whole
  5026. ;; sub-tree.
  5027. (should
  5028. (equal "1"
  5029. (org-test-with-temp-text
  5030. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  5031. (let ((org-use-property-inheritance t))
  5032. (org-refresh-properties "A" 'org-test))
  5033. (get-text-property (point) 'org-test))))
  5034. ;; When property is inherited, use global value across the whole
  5035. ;; buffer. However local values have precedence.
  5036. (should-not
  5037. (equal "1"
  5038. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5039. (org-mode-restart)
  5040. (let ((org-use-property-inheritance nil))
  5041. (org-refresh-properties "A" 'org-test))
  5042. (get-text-property (point) 'org-test))))
  5043. (should
  5044. (equal "1"
  5045. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  5046. (org-mode-restart)
  5047. (let ((org-use-property-inheritance t))
  5048. (org-refresh-properties "A" 'org-test))
  5049. (get-text-property (point) 'org-test))))
  5050. (should
  5051. (equal "2"
  5052. (org-test-with-temp-text
  5053. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  5054. (org-mode-restart)
  5055. (let ((org-use-property-inheritance t))
  5056. (org-refresh-properties "A" 'org-test))
  5057. (get-text-property (point) 'org-test)))))
  5058. ;;; Radio Targets
  5059. (ert-deftest test-org/update-radio-target-regexp ()
  5060. "Test `org-update-radio-target-regexp' specifications."
  5061. ;; Properly update cache with no previous radio target regexp.
  5062. (should
  5063. (eq 'link
  5064. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5065. (save-excursion (goto-char (point-max)) (org-element-context))
  5066. (insert "<<<")
  5067. (search-forward "o")
  5068. (insert ">>>")
  5069. (org-update-radio-target-regexp)
  5070. (goto-char (point-max))
  5071. (org-element-type (org-element-context)))))
  5072. ;; Properly update cache with previous radio target regexp.
  5073. (should
  5074. (eq 'link
  5075. (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
  5076. (save-excursion (goto-char (point-max)) (org-element-context))
  5077. (insert "<<<")
  5078. (search-forward "o")
  5079. (insert ">>>")
  5080. (org-update-radio-target-regexp)
  5081. (search-backward "r")
  5082. (delete-char 5)
  5083. (insert "new")
  5084. (org-update-radio-target-regexp)
  5085. (goto-char (point-max))
  5086. (delete-region (line-beginning-position) (point))
  5087. (insert "new")
  5088. (org-element-type (org-element-context))))))
  5089. ;;; Sparse trees
  5090. (ert-deftest test-org/match-sparse-tree ()
  5091. "Test `org-match-sparse-tree' specifications."
  5092. ;; Match tags.
  5093. (should-not
  5094. (org-test-with-temp-text "* H\n** H1 :tag:"
  5095. (org-match-sparse-tree nil "tag")
  5096. (search-forward "H1")
  5097. (org-invisible-p2)))
  5098. (should
  5099. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  5100. (org-match-sparse-tree nil "tag")
  5101. (search-forward "H2")
  5102. (org-invisible-p2)))
  5103. ;; "-" operator for tags.
  5104. (should-not
  5105. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5106. (org-match-sparse-tree nil "tag1-tag2")
  5107. (search-forward "H1")
  5108. (org-invisible-p2)))
  5109. (should
  5110. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5111. (org-match-sparse-tree nil "tag1-tag2")
  5112. (search-forward "H2")
  5113. (org-invisible-p2)))
  5114. ;; "&" operator for tags.
  5115. (should
  5116. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5117. (org-match-sparse-tree nil "tag1&tag2")
  5118. (search-forward "H1")
  5119. (org-invisible-p2)))
  5120. (should-not
  5121. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5122. (org-match-sparse-tree nil "tag1&tag2")
  5123. (search-forward "H2")
  5124. (org-invisible-p2)))
  5125. ;; "|" operator for tags.
  5126. (should-not
  5127. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5128. (org-match-sparse-tree nil "tag1|tag2")
  5129. (search-forward "H1")
  5130. (org-invisible-p2)))
  5131. (should-not
  5132. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  5133. (org-match-sparse-tree nil "tag1|tag2")
  5134. (search-forward "H2")
  5135. (org-invisible-p2)))
  5136. ;; Regexp match on tags.
  5137. (should-not
  5138. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5139. (org-match-sparse-tree nil "{^tag.*}")
  5140. (search-forward "H1")
  5141. (org-invisible-p2)))
  5142. (should
  5143. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  5144. (org-match-sparse-tree nil "{^tag.*}")
  5145. (search-forward "H2")
  5146. (org-invisible-p2)))
  5147. ;; Match group tags.
  5148. (should-not
  5149. (org-test-with-temp-text
  5150. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5151. (org-match-sparse-tree nil "work")
  5152. (search-forward "H1")
  5153. (org-invisible-p2)))
  5154. (should-not
  5155. (org-test-with-temp-text
  5156. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  5157. (org-match-sparse-tree nil "work")
  5158. (search-forward "H2")
  5159. (org-invisible-p2)))
  5160. ;; Match group tags with hard brackets.
  5161. (should-not
  5162. (org-test-with-temp-text
  5163. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5164. (org-match-sparse-tree nil "work")
  5165. (search-forward "H1")
  5166. (org-invisible-p2)))
  5167. (should-not
  5168. (org-test-with-temp-text
  5169. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  5170. (org-match-sparse-tree nil "work")
  5171. (search-forward "H2")
  5172. (org-invisible-p2)))
  5173. ;; Match tags in hierarchies
  5174. (should-not
  5175. (org-test-with-temp-text
  5176. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  5177. #+TAGS: [ Lev_2 : Lev_3 ]\n
  5178. #+TAGS: { Lev_3 : Lev_4 }\n
  5179. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  5180. (org-match-sparse-tree nil "Lev_1")
  5181. (search-forward "H4")
  5182. (org-invisible-p2)))
  5183. ;; Match regular expressions in tags
  5184. (should-not
  5185. (org-test-with-temp-text
  5186. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  5187. (org-match-sparse-tree nil "Lev")
  5188. (search-forward "H1")
  5189. (org-invisible-p2)))
  5190. (should
  5191. (org-test-with-temp-text
  5192. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  5193. (org-match-sparse-tree nil "Lev")
  5194. (search-forward "H1")
  5195. (org-invisible-p2)))
  5196. ;; Match properties.
  5197. (should
  5198. (org-test-with-temp-text
  5199. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  5200. (org-match-sparse-tree nil "A=\"1\"")
  5201. (search-forward "H2")
  5202. (org-invisible-p2)))
  5203. (should-not
  5204. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5205. (org-match-sparse-tree nil "A=\"1\"")
  5206. (search-forward "H2")
  5207. (org-invisible-p2)))
  5208. ;; Case is not significant when matching properties.
  5209. (should-not
  5210. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  5211. (org-match-sparse-tree nil "a=\"1\"")
  5212. (search-forward "H2")
  5213. (org-invisible-p2)))
  5214. (should-not
  5215. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  5216. (org-match-sparse-tree nil "A=\"1\"")
  5217. (search-forward "H2")
  5218. (org-invisible-p2)))
  5219. ;; Match special LEVEL property.
  5220. (should-not
  5221. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5222. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5223. (search-forward "H1")
  5224. (org-invisible-p2)))
  5225. (should
  5226. (org-test-with-temp-text "* H\n** H1\n*** H2"
  5227. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  5228. (search-forward "H2")
  5229. (org-invisible-p2)))
  5230. ;; Comparison operators when matching properties.
  5231. (should
  5232. (org-test-with-temp-text
  5233. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5234. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5235. (search-forward "H1")
  5236. (org-invisible-p2)))
  5237. (should-not
  5238. (org-test-with-temp-text
  5239. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  5240. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  5241. (search-forward "H2")
  5242. (org-invisible-p2)))
  5243. ;; Regexp match on properties values.
  5244. (should-not
  5245. (org-test-with-temp-text
  5246. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5247. (org-match-sparse-tree nil "A={f.*}")
  5248. (search-forward "H1")
  5249. (org-invisible-p2)))
  5250. (should
  5251. (org-test-with-temp-text
  5252. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  5253. (org-match-sparse-tree nil "A={f.*}")
  5254. (search-forward "H2")
  5255. (org-invisible-p2)))
  5256. ;; With an optional argument, limit match to TODO entries.
  5257. (should-not
  5258. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5259. (org-match-sparse-tree t "tag")
  5260. (search-forward "H1")
  5261. (org-invisible-p2)))
  5262. (should
  5263. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  5264. (org-match-sparse-tree t "tag")
  5265. (search-forward "H2")
  5266. (org-invisible-p2))))
  5267. (ert-deftest test-org/occur ()
  5268. "Test `org-occur' specifications."
  5269. ;; Count number of matches.
  5270. (should
  5271. (= 1
  5272. (org-test-with-temp-text "* H\nA\n* H2"
  5273. (org-occur "A"))))
  5274. (should
  5275. (= 2
  5276. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5277. (org-occur "A"))))
  5278. ;; Test CALLBACK optional argument.
  5279. (should
  5280. (= 0
  5281. (org-test-with-temp-text "* H\nA\n* H2"
  5282. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5283. (should
  5284. (= 1
  5285. (org-test-with-temp-text "* H\nA\n* H2\nA"
  5286. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  5287. ;; Case-fold searches according to `org-occur-case-fold-search'.
  5288. (should
  5289. (= 2
  5290. (org-test-with-temp-text "Aa"
  5291. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  5292. (should
  5293. (= 2
  5294. (org-test-with-temp-text "Aa"
  5295. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  5296. (should
  5297. (= 1
  5298. (org-test-with-temp-text "Aa"
  5299. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  5300. (should
  5301. (= 1
  5302. (org-test-with-temp-text "Aa"
  5303. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  5304. (should
  5305. (= 1
  5306. (org-test-with-temp-text "Aa"
  5307. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  5308. (should
  5309. (= 2
  5310. (org-test-with-temp-text "Aa"
  5311. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  5312. ;;; Tags
  5313. (ert-deftest test-org/tag-string-to-alist ()
  5314. "Test `org-tag-string-to-alist' specifications."
  5315. ;; Tag without selection key.
  5316. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  5317. ;; Tag with selection key.
  5318. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  5319. ;; Tag group.
  5320. (should
  5321. (equal
  5322. (org-tag-string-to-alist "[ group : t1 t2 ]")
  5323. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  5324. ;; Mutually exclusive tags.
  5325. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  5326. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  5327. (should
  5328. (equal
  5329. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  5330. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  5331. (ert-deftest test-org/tag-alist-to-string ()
  5332. "Test `org-tag-alist-to-string' specifications."
  5333. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  5334. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  5335. (should
  5336. (equal
  5337. (org-tag-alist-to-string
  5338. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5339. "[ group : t1 t2 ]"))
  5340. (should
  5341. (equal (org-tag-alist-to-string
  5342. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  5343. "{ tag1 tag2 }"))
  5344. (should
  5345. (equal
  5346. (org-tag-alist-to-string
  5347. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  5348. "{ group : tag1 tag2 }")))
  5349. (ert-deftest test-org/tag-alist-to-groups ()
  5350. "Test `org-tag-alist-to-groups' specifications."
  5351. (should
  5352. (equal (org-tag-alist-to-groups
  5353. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  5354. '(("group" "t1" "t2"))))
  5355. (should
  5356. (equal
  5357. (org-tag-alist-to-groups
  5358. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  5359. '(("group" "t1" "t2"))))
  5360. (should-not
  5361. (org-tag-alist-to-groups
  5362. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  5363. (ert-deftest test-org/tag-align ()
  5364. "Test tags alignment."
  5365. ;; Test aligning tags with different display width.
  5366. (should
  5367. ;; 12345678901234567890
  5368. (equal "* Test :abc:"
  5369. (org-test-with-temp-text "* Test :abc:"
  5370. (let ((org-tags-column -20)
  5371. (indent-tabs-mode nil))
  5372. (org-fix-tags-on-the-fly))
  5373. (buffer-string))))
  5374. (should
  5375. ;; 12345678901234567890
  5376. (equal "* Test :日本語:"
  5377. (org-test-with-temp-text "* Test :日本語:"
  5378. (let ((org-tags-column -20)
  5379. (indent-tabs-mode nil))
  5380. (org-fix-tags-on-the-fly))
  5381. (buffer-string))))
  5382. ;; Make sure aligning tags do not skip invisible text.
  5383. (should
  5384. (equal "* [[linkx]] :tag:"
  5385. (org-test-with-temp-text "* [[link<point>]] :tag:"
  5386. (let ((org-tags-column 0))
  5387. (org-fix-tags-on-the-fly)
  5388. (insert "x")
  5389. (buffer-string))))))
  5390. (ert-deftest test-org/tags-at ()
  5391. (should
  5392. (equal '("foo" "bar")
  5393. (org-test-with-temp-text
  5394. "* T<point>est :foo:bar:"
  5395. (org-get-tags-at)))))
  5396. ;;; Timestamps API
  5397. (ert-deftest test-org/time-stamp ()
  5398. "Test `org-time-stamp' specifications."
  5399. ;; Insert chosen time stamp at point.
  5400. (should
  5401. (string-match
  5402. "Te<2014-03-04 .*?>xt"
  5403. (org-test-with-temp-text "Te<point>xt"
  5404. (cl-letf (((symbol-function 'org-read-date)
  5405. (lambda (&rest args)
  5406. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5407. (org-time-stamp nil)
  5408. (buffer-string)))))
  5409. ;; With a prefix argument, also insert time.
  5410. (should
  5411. (string-match
  5412. "Te<2014-03-04 .*? 00:41>xt"
  5413. (org-test-with-temp-text "Te<point>xt"
  5414. (cl-letf (((symbol-function 'org-read-date)
  5415. (lambda (&rest args)
  5416. (apply #'encode-time
  5417. (org-parse-time-string "2014-03-04 00:41")))))
  5418. (org-time-stamp '(4))
  5419. (buffer-string)))))
  5420. ;; With two universal prefix arguments, insert an active timestamp
  5421. ;; with the current time without prompting the user.
  5422. (should
  5423. (string-match
  5424. "Te<2014-03-04 .*? 00:41>xt"
  5425. (org-test-with-temp-text "Te<point>xt"
  5426. (cl-letf (((symbol-function 'current-time)
  5427. (lambda ()
  5428. (apply #'encode-time
  5429. (org-parse-time-string "2014-03-04 00:41")))))
  5430. (org-time-stamp '(16))
  5431. (buffer-string)))))
  5432. ;; When optional argument is non-nil, insert an inactive timestamp.
  5433. (should
  5434. (string-match
  5435. "Te\\[2014-03-04 .*?\\]xt"
  5436. (org-test-with-temp-text "Te<point>xt"
  5437. (cl-letf (((symbol-function 'org-read-date)
  5438. (lambda (&rest args)
  5439. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5440. (org-time-stamp nil t)
  5441. (buffer-string)))))
  5442. ;; When called from a timestamp, replace existing one.
  5443. (should
  5444. (string-match
  5445. "<2014-03-04 .*?>"
  5446. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  5447. (cl-letf (((symbol-function 'org-read-date)
  5448. (lambda (&rest args)
  5449. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5450. (org-time-stamp nil)
  5451. (buffer-string)))))
  5452. (should
  5453. (string-match
  5454. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  5455. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  5456. (cl-letf (((symbol-function 'org-read-date)
  5457. (lambda (&rest args)
  5458. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5459. (org-time-stamp nil)
  5460. (buffer-string)))))
  5461. ;; When replacing a timestamp, preserve repeater, if any.
  5462. (should
  5463. (string-match
  5464. "<2014-03-04 .*? \\+2y>"
  5465. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  5466. (cl-letf (((symbol-function 'org-read-date)
  5467. (lambda (&rest args)
  5468. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5469. (org-time-stamp nil)
  5470. (buffer-string)))))
  5471. ;; When called twice in a raw, build a date range.
  5472. (should
  5473. (string-match
  5474. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  5475. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  5476. (cl-letf (((symbol-function 'org-read-date)
  5477. (lambda (&rest args)
  5478. (apply #'encode-time (org-parse-time-string "2014-03-04")))))
  5479. (let ((last-command 'org-time-stamp)
  5480. (this-command 'org-time-stamp))
  5481. (org-time-stamp nil))
  5482. (buffer-string))))))
  5483. (ert-deftest test-org/timestamp-has-time-p ()
  5484. "Test `org-timestamp-has-time-p' specifications."
  5485. ;; With time.
  5486. (should
  5487. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  5488. (org-timestamp-has-time-p (org-element-context))))
  5489. ;; Without time.
  5490. (should-not
  5491. (org-test-with-temp-text "<2012-03-29 Thu>"
  5492. (org-timestamp-has-time-p (org-element-context)))))
  5493. (ert-deftest test-org/timestamp-format ()
  5494. "Test `org-timestamp-format' specifications."
  5495. ;; Regular test.
  5496. (should
  5497. (equal
  5498. "2012-03-29 16:40"
  5499. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  5500. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  5501. ;; Range end.
  5502. (should
  5503. (equal
  5504. "2012-03-29"
  5505. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  5506. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  5507. (ert-deftest test-org/timestamp-split-range ()
  5508. "Test `org-timestamp-split-range' specifications."
  5509. ;; Extract range start (active).
  5510. (should
  5511. (equal '(2012 3 29)
  5512. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5513. (let ((ts (org-timestamp-split-range (org-element-context))))
  5514. (mapcar (lambda (p) (org-element-property p ts))
  5515. '(:year-end :month-end :day-end))))))
  5516. ;; Extract range start (inactive)
  5517. (should
  5518. (equal '(2012 3 29)
  5519. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  5520. (let ((ts (org-timestamp-split-range (org-element-context))))
  5521. (mapcar (lambda (p) (org-element-property p ts))
  5522. '(:year-end :month-end :day-end))))))
  5523. ;; Extract range end (active).
  5524. (should
  5525. (equal '(2012 3 30)
  5526. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5527. (let ((ts (org-timestamp-split-range
  5528. (org-element-context) t)))
  5529. (mapcar (lambda (p) (org-element-property p ts))
  5530. '(:year-end :month-end :day-end))))))
  5531. ;; Extract range end (inactive)
  5532. (should
  5533. (equal '(2012 3 30)
  5534. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  5535. (let ((ts (org-timestamp-split-range
  5536. (org-element-context) t)))
  5537. (mapcar (lambda (p) (org-element-property p ts))
  5538. '(:year-end :month-end :day-end))))))
  5539. ;; Return the timestamp if not a range.
  5540. (should
  5541. (org-test-with-temp-text "[2012-03-29 Thu]"
  5542. (let* ((ts-orig (org-element-context))
  5543. (ts-copy (org-timestamp-split-range ts-orig)))
  5544. (eq ts-orig ts-copy))))
  5545. (should
  5546. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  5547. (let* ((ts-orig (org-element-context))
  5548. (ts-copy (org-timestamp-split-range ts-orig)))
  5549. (eq ts-orig ts-copy)))))
  5550. (ert-deftest test-org/timestamp-translate ()
  5551. "Test `org-timestamp-translate' specifications."
  5552. ;; Translate whole date range.
  5553. (should
  5554. (equal "<29>--<30>"
  5555. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5556. (let ((org-display-custom-times t)
  5557. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5558. (org-timestamp-translate (org-element-context))))))
  5559. ;; Translate date range start.
  5560. (should
  5561. (equal "<29>"
  5562. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5563. (let ((org-display-custom-times t)
  5564. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5565. (org-timestamp-translate (org-element-context) 'start)))))
  5566. ;; Translate date range end.
  5567. (should
  5568. (equal "<30>"
  5569. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  5570. (let ((org-display-custom-times t)
  5571. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5572. (org-timestamp-translate (org-element-context) 'end)))))
  5573. ;; Translate time range.
  5574. (should
  5575. (equal "<08>--<16>"
  5576. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  5577. (let ((org-display-custom-times t)
  5578. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  5579. (org-timestamp-translate (org-element-context))))))
  5580. ;; Translate non-range timestamp.
  5581. (should
  5582. (equal "<29>"
  5583. (org-test-with-temp-text "<2012-03-29 Thu>"
  5584. (let ((org-display-custom-times t)
  5585. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5586. (org-timestamp-translate (org-element-context))))))
  5587. ;; Do not change `diary' timestamps.
  5588. (should
  5589. (equal "<%%(org-float t 4 2)>"
  5590. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  5591. (let ((org-display-custom-times t)
  5592. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  5593. (org-timestamp-translate (org-element-context)))))))
  5594. ;;; Visibility
  5595. (ert-deftest test-org/flag-drawer ()
  5596. "Test `org-flag-drawer' specifications."
  5597. ;; Hide drawer.
  5598. (should
  5599. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  5600. (org-flag-drawer t)
  5601. (get-char-property (line-end-position) 'invisible)))
  5602. ;; Show drawer.
  5603. (should-not
  5604. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  5605. (org-flag-drawer t)
  5606. (org-flag-drawer nil)
  5607. (get-char-property (line-end-position) 'invisible)))
  5608. ;; Test optional argument.
  5609. (should
  5610. (org-test-with-temp-text "Text\n:D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  5611. (let ((drawer (save-excursion (search-forward ":D2")
  5612. (org-element-at-point))))
  5613. (org-flag-drawer t drawer)
  5614. (get-char-property (progn (search-forward ":D2") (line-end-position))
  5615. 'invisible))))
  5616. (should-not
  5617. (org-test-with-temp-text ":D1:\nc1\n:END:\n\n:D2:\nc2\n:END:"
  5618. (let ((drawer (save-excursion (search-forward ":D2")
  5619. (org-element-at-point))))
  5620. (org-flag-drawer t drawer)
  5621. (get-char-property (line-end-position) 'invisible))))
  5622. ;; Do not hide fake drawers.
  5623. (should-not
  5624. (org-test-with-temp-text "#+begin_example\n:D:\nc\n:END:\n#+end_example"
  5625. (forward-line 1)
  5626. (org-flag-drawer t)
  5627. (get-char-property (line-end-position) 'invisible)))
  5628. ;; Do not hide incomplete drawers.
  5629. (should-not
  5630. (org-test-with-temp-text ":D:\nparagraph"
  5631. (forward-line 1)
  5632. (org-flag-drawer t)
  5633. (get-char-property (line-end-position) 'invisible)))
  5634. ;; Do not hide drawers when called from final blank lines.
  5635. (should-not
  5636. (org-test-with-temp-text ":DRAWER:\nA\n:END:\n\n"
  5637. (goto-char (point-max))
  5638. (org-flag-drawer t)
  5639. (goto-char (point-min))
  5640. (get-char-property (line-end-position) 'invisible)))
  5641. ;; Don't leave point in an invisible part of the buffer when hiding
  5642. ;; a drawer away.
  5643. (should-not
  5644. (org-test-with-temp-text ":DRAWER:\ncontents\n:END:"
  5645. (goto-char (point-max))
  5646. (org-flag-drawer t)
  5647. (get-char-property (point) 'invisible))))
  5648. (ert-deftest test-org/hide-block-toggle ()
  5649. "Test `org-hide-block-toggle' specifications."
  5650. ;; Error when not at a block.
  5651. (should-error
  5652. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents"
  5653. (org-hide-block-toggle 'off)
  5654. (get-char-property (line-end-position) 'invisible)))
  5655. ;; Hide block.
  5656. (should
  5657. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  5658. (org-hide-block-toggle)
  5659. (get-char-property (line-end-position) 'invisible)))
  5660. (should
  5661. (org-test-with-temp-text "#+BEGIN_EXAMPLE\ncontents\n#+END_EXAMPLE"
  5662. (org-hide-block-toggle)
  5663. (get-char-property (line-end-position) 'invisible)))
  5664. ;; Show block unconditionally when optional argument is `off'.
  5665. (should-not
  5666. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5667. (org-hide-block-toggle)
  5668. (org-hide-block-toggle 'off)
  5669. (get-char-property (line-end-position) 'invisible)))
  5670. (should-not
  5671. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5672. (org-hide-block-toggle 'off)
  5673. (get-char-property (line-end-position) 'invisible)))
  5674. ;; Hide block unconditionally when optional argument is non-nil.
  5675. (should
  5676. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5677. (org-hide-block-toggle t)
  5678. (get-char-property (line-end-position) 'invisible)))
  5679. (should
  5680. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE"
  5681. (org-hide-block-toggle)
  5682. (org-hide-block-toggle t)
  5683. (get-char-property (line-end-position) 'invisible)))
  5684. ;; Do not hide block when called from final blank lines.
  5685. (should-not
  5686. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n#+END_QUOTE\n\n<point>"
  5687. (org-hide-block-toggle)
  5688. (goto-char (point-min))
  5689. (get-char-property (line-end-position) 'invisible)))
  5690. ;; Don't leave point in an invisible part of the buffer when hiding
  5691. ;; a block away.
  5692. (should-not
  5693. (org-test-with-temp-text "#+BEGIN_QUOTE\ncontents\n<point>#+END_QUOTE"
  5694. (org-hide-block-toggle)
  5695. (get-char-property (point) 'invisible))))
  5696. (ert-deftest test-org/hide-block-toggle-maybe ()
  5697. "Test `org-hide-block-toggle-maybe' specifications."
  5698. (should
  5699. (org-test-with-temp-text "#+BEGIN: dynamic\nContents\n#+END:"
  5700. (org-hide-block-toggle-maybe)))
  5701. (should-not
  5702. (org-test-with-temp-text "Paragraph" (org-hide-block-toggle-maybe))))
  5703. (ert-deftest test-org/set-tags ()
  5704. "Test `org-set-tags' specifications."
  5705. ;; Tags set via fast-tag-selection should be visible afterwards
  5706. (should
  5707. (let ((org-tag-alist '(("NEXT" . ?n)))
  5708. (org-fast-tag-selection-single-key t))
  5709. (cl-letf (((symbol-function 'read-char-exclusive) (lambda () ?n))
  5710. ((symbol-function 'window-width) (lambda (&rest args) 100)))
  5711. (org-test-with-temp-text "<point>* Headline\nAnd its content\n* And another headline\n\nWith some content"
  5712. ;; Show only headlines
  5713. (org-content)
  5714. ;; Set NEXT tag on current entry
  5715. (org-set-tags nil nil)
  5716. ;; Move point to that NEXT tag
  5717. (search-forward "NEXT") (backward-word)
  5718. ;; And it should be visible (i.e. no overlays)
  5719. (not (overlays-at (point))))))))
  5720. (ert-deftest test-org/show-set-visibility ()
  5721. "Test `org-show-set-visibility' specifications."
  5722. ;; Do not throw an error before first heading.
  5723. (should
  5724. (org-test-with-temp-text "Preamble\n* Headline"
  5725. (org-show-set-visibility 'tree)
  5726. t))
  5727. ;; Test all visibility spans, both on headline and in entry.
  5728. (let ((list-visible-lines
  5729. (lambda (state headerp)
  5730. (org-test-with-temp-text "* Grandmother (0)
  5731. ** Uncle (1)
  5732. *** Heir (2)
  5733. ** Father (3)
  5734. Ancestor text (4)
  5735. *** Sister (5)
  5736. Sibling text (6)
  5737. *** Self (7)
  5738. Match (8)
  5739. **** First born (9)
  5740. Child text (10)
  5741. **** The other child (11)
  5742. *** Brother (12)
  5743. ** Aunt (13)
  5744. "
  5745. (org-cycle t)
  5746. (search-forward (if headerp "Self" "Match"))
  5747. (org-show-set-visibility state)
  5748. (goto-char (point-min))
  5749. (let (result (line 0))
  5750. (while (not (eobp))
  5751. (unless (org-invisible-p2) (push line result))
  5752. (incf line)
  5753. (forward-line))
  5754. (nreverse result))))))
  5755. (should (equal '(0 7) (funcall list-visible-lines 'minimal t)))
  5756. (should (equal '(0 7 8) (funcall list-visible-lines 'minimal nil)))
  5757. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local t)))
  5758. (should (equal '(0 7 8 9) (funcall list-visible-lines 'local nil)))
  5759. (should (equal '(0 3 7) (funcall list-visible-lines 'ancestors t)))
  5760. (should (equal '(0 3 7 8) (funcall list-visible-lines 'ancestors nil)))
  5761. (should (equal '(0 3 5 7 12) (funcall list-visible-lines 'lineage t)))
  5762. (should (equal '(0 3 5 7 8 9 12) (funcall list-visible-lines 'lineage nil)))
  5763. (should (equal '(0 1 3 5 7 12 13) (funcall list-visible-lines 'tree t)))
  5764. (should (equal '(0 1 3 5 7 8 9 11 12 13)
  5765. (funcall list-visible-lines 'tree nil)))
  5766. (should (equal '(0 1 3 4 5 7 12 13)
  5767. (funcall list-visible-lines 'canonical t)))
  5768. (should (equal '(0 1 3 4 5 7 8 9 11 12 13)
  5769. (funcall list-visible-lines 'canonical nil))))
  5770. ;; When point is hidden in a drawer or a block, make sure to make it
  5771. ;; visible.
  5772. (should-not
  5773. (org-test-with-temp-text "#+BEGIN_QUOTE\nText\n#+END_QUOTE"
  5774. (org-hide-block-toggle)
  5775. (search-forward "Text")
  5776. (org-show-set-visibility 'minimal)
  5777. (org-invisible-p2)))
  5778. (should-not
  5779. (org-test-with-temp-text ":DRAWER:\nText\n:END:"
  5780. (org-flag-drawer t)
  5781. (search-forward "Text")
  5782. (org-show-set-visibility 'minimal)
  5783. (org-invisible-p2)))
  5784. (should-not
  5785. (org-test-with-temp-text
  5786. "#+BEGIN_QUOTE\n<point>:DRAWER:\nText\n:END:\n#+END_QUOTE"
  5787. (org-flag-drawer t)
  5788. (forward-line -1)
  5789. (org-hide-block-toggle)
  5790. (search-forward "Text")
  5791. (org-show-set-visibility 'minimal)
  5792. (org-invisible-p2))))
  5793. (provide 'test-org)
  5794. ;;; test-org.el ends here