test-org.el 285 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659
  1. ;;; test-org.el --- tests for org.el -*- lexical-binding: t -*-
  2. ;; Copyright (c) David Maus
  3. ;; Authors: David Maus
  4. ;; This file is not part of GNU Emacs.
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;; Template test file for Org tests
  16. ;;; Code:
  17. (eval-and-compile (require 'cl-lib))
  18. (eval-when-compile (require 'org-macs)) ;For `org-with-gensyms'.
  19. (require 'org)
  20. (require 'org-inlinetask)
  21. (require 'org-refile)
  22. (require 'org-agenda)
  23. ;;; Helpers
  24. (defmacro org-test-with-timezone (tz &rest body)
  25. "Evaluate BODY with TZ environment temporary set to the passed value."
  26. (declare (indent 1))
  27. (org-with-gensyms (tz-saved)
  28. `(let ((,tz-saved (getenv "TZ")))
  29. (unwind-protect
  30. (progn
  31. (setenv "TZ" ,tz)
  32. ,@body)
  33. (setenv "TZ" ,tz-saved)))))
  34. ;;; Comments
  35. (ert-deftest test-org/toggle-comment ()
  36. "Test `org-toggle-comment' specifications."
  37. ;; Simple headline.
  38. (should
  39. (equal "* Test"
  40. (org-test-with-temp-text "* COMMENT Test"
  41. (org-toggle-comment)
  42. (buffer-string))))
  43. (should
  44. (equal "* COMMENT Test"
  45. (org-test-with-temp-text "* Test"
  46. (org-toggle-comment)
  47. (buffer-string))))
  48. ;; Headline with a regular keyword.
  49. (should
  50. (equal "* TODO Test"
  51. (org-test-with-temp-text "* TODO COMMENT Test"
  52. (org-toggle-comment)
  53. (buffer-string))))
  54. (should
  55. (equal "* TODO COMMENT Test"
  56. (org-test-with-temp-text "* TODO Test"
  57. (org-toggle-comment)
  58. (buffer-string))))
  59. ;; Empty headline.
  60. (should
  61. (equal "* "
  62. (org-test-with-temp-text "* COMMENT"
  63. (org-toggle-comment)
  64. (buffer-string))))
  65. (should
  66. (equal "* COMMENT"
  67. (org-test-with-temp-text "* "
  68. (org-toggle-comment)
  69. (buffer-string))))
  70. ;; Headline with a single keyword.
  71. (should
  72. (equal "* TODO "
  73. (org-test-with-temp-text "* TODO COMMENT"
  74. (org-toggle-comment)
  75. (buffer-string))))
  76. (should
  77. (equal "* TODO COMMENT"
  78. (org-test-with-temp-text "* TODO"
  79. (org-toggle-comment)
  80. (buffer-string))))
  81. ;; Headline with a keyword, a priority cookie and contents.
  82. (should
  83. (equal "* TODO [#A] Headline"
  84. (org-test-with-temp-text "* TODO [#A] COMMENT Headline"
  85. (org-toggle-comment)
  86. (buffer-string))))
  87. (should
  88. (equal "* TODO [#A] COMMENT Headline"
  89. (org-test-with-temp-text "* TODO [#A] Headline"
  90. (org-toggle-comment)
  91. (buffer-string)))))
  92. (ert-deftest test-org/comment-dwim ()
  93. "Test `comment-dwim' behaviour in an Org buffer."
  94. ;; No region selected, no comment on current line and line not
  95. ;; empty: insert comment on line above.
  96. (should
  97. (equal "# \nComment"
  98. (org-test-with-temp-text "Comment"
  99. (call-interactively #'org-comment-dwim)
  100. (buffer-string))))
  101. ;; No region selected, no comment on current line and line empty:
  102. ;; insert comment on this line.
  103. (should
  104. (equal "# \nParagraph"
  105. (org-test-with-temp-text "\nParagraph"
  106. (call-interactively #'org-comment-dwim)
  107. (buffer-string))))
  108. ;; No region selected, and a comment on this line: indent it.
  109. (should
  110. (equal "* Headline\n # Comment"
  111. (org-test-with-temp-text "* Headline\n# <point>Comment"
  112. (let ((org-adapt-indentation t))
  113. (call-interactively #'org-comment-dwim))
  114. (buffer-string))))
  115. ;; Also recognize single # at column 0 as comments.
  116. (should
  117. (equal "# Comment"
  118. (org-test-with-temp-text "# Comment"
  119. (call-interactively #'org-comment-dwim)
  120. (buffer-string))))
  121. ;; Region selected and only comments and blank lines within it:
  122. ;; un-comment all commented lines.
  123. (should
  124. (equal "Comment 1\n\nComment 2"
  125. (org-test-with-temp-text "# Comment 1\n\n# Comment 2"
  126. (transient-mark-mode 1)
  127. (push-mark (point) t t)
  128. (goto-char (point-max))
  129. (call-interactively #'org-comment-dwim)
  130. (buffer-string))))
  131. ;; Region selected without comments: comment all lines if
  132. ;; `comment-empty-lines' is non-nil, only non-blank lines otherwise.
  133. (should
  134. (equal "# Comment 1\n\n# Comment 2"
  135. (org-test-with-temp-text "Comment 1\n\nComment 2"
  136. (transient-mark-mode 1)
  137. (push-mark (point) t t)
  138. (goto-char (point-max))
  139. (let ((comment-empty-lines nil))
  140. (call-interactively #'org-comment-dwim))
  141. (buffer-string))))
  142. (should
  143. (equal "# Comment 1\n# \n# Comment 2"
  144. (org-test-with-temp-text "Comment 1\n\nComment 2"
  145. (transient-mark-mode 1)
  146. (push-mark (point) t t)
  147. (goto-char (point-max))
  148. (let ((comment-empty-lines t))
  149. (call-interactively #'org-comment-dwim))
  150. (buffer-string))))
  151. ;; In front of a keyword without region, insert a new comment.
  152. (should
  153. (equal "# \n#+KEYWORD: value"
  154. (org-test-with-temp-text "#+KEYWORD: value"
  155. (call-interactively #'org-comment-dwim)
  156. (buffer-string))))
  157. ;; Comment a heading
  158. (should
  159. (equal "* COMMENT Test"
  160. (org-test-with-temp-text "* Test"
  161. (call-interactively #'org-comment-dwim)
  162. (buffer-string))))
  163. ;; In a source block, use appropriate syntax.
  164. (should
  165. (equal " ;; "
  166. (org-test-with-temp-text "#+BEGIN_SRC emacs-lisp\n<point>\n#+END_SRC"
  167. (let ((org-edit-src-content-indentation 2))
  168. (call-interactively #'org-comment-dwim))
  169. (buffer-substring-no-properties (line-beginning-position)
  170. (point)))))
  171. (should
  172. (equal "#+BEGIN_SRC emacs-lisp\n ;; a\n ;; b\n#+END_SRC"
  173. (org-test-with-temp-text
  174. "#+BEGIN_SRC emacs-lisp\n<point>a\nb\n#+END_SRC"
  175. (transient-mark-mode 1)
  176. (push-mark (point) t t)
  177. (forward-line 2)
  178. (let ((org-edit-src-content-indentation 2))
  179. (call-interactively #'org-comment-dwim))
  180. (buffer-string)))))
  181. ;;; Date and time analysis
  182. (ert-deftest test-org/org-encode-time ()
  183. "Test various ways to call `org-encode-time'"
  184. (org-test-with-timezone "UTC"
  185. ;; list as the sole argument
  186. (should (string-equal
  187. "2022-03-24 23:30:01"
  188. (format-time-string
  189. "%F %T"
  190. (org-encode-time '(1 30 23 24 3 2022 nil -1 nil)))))
  191. ;; SECOND...YEAR
  192. (should (string-equal
  193. "2022-03-24 23:30:02"
  194. (format-time-string
  195. "%F %T"
  196. (org-encode-time 2 30 23 24 3 2022))))
  197. ;; SECOND...YEAR IGNORED DST ZONE
  198. (should (string-equal
  199. "2022-03-24 23:30:03"
  200. (format-time-string
  201. "%F %T"
  202. (org-encode-time 3 30 23 24 3 2022 nil -1 nil))))
  203. ;; function call
  204. (should (string-equal
  205. "2022-03-24 23:30:04"
  206. (format-time-string
  207. "%F %T"
  208. (org-encode-time (apply #'list 4 30 23 '(24 3 2022 nil -1 nil))))))
  209. ;; wrong number of arguments
  210. (if (not (version< emacs-version "27.1"))
  211. (should-error (string-equal
  212. "2022-03-24 23:30:05"
  213. (format-time-string
  214. "%F %T"
  215. (org-encode-time 5 30 23 24 3 2022 nil))))))
  216. ;; daylight saving time
  217. (if (not (version< emacs-version "27.1"))
  218. ;; DST value is not ignored for multiple arguments unlike for `encode-time'
  219. (should (string-equal
  220. "2022-04-01 00:30:06 +0200 CEST"
  221. (format-time-string
  222. "%F %T %z %Z"
  223. (org-encode-time 6 30 23 31 3 2022 nil nil "Europe/Madrid")
  224. "Europe/Madrid")))
  225. (should (string-equal
  226. "2022-03-31 23:30:07 +0200 CEST"
  227. (format-time-string
  228. "%F %T %z %Z"
  229. (org-encode-time 7 30 23 31 3 2022 nil t "Europe/Madrid")
  230. "Europe/Madrid"))))
  231. (org-test-with-timezone "Europe/Madrid"
  232. ;; Standard time is not forced when DST is not specified
  233. (should (string-equal
  234. "2022-03-31 23:30:08"
  235. (format-time-string
  236. "%F %T"
  237. (org-encode-time 8 30 23 31 3 2022))))))
  238. (ert-deftest test-org/org-time-string-to-time ()
  239. "Test `org-time-string-to-time' around DST transition."
  240. (org-test-with-timezone "UTC"
  241. (should (string-equal
  242. "2022-03-31 23:31:00"
  243. (format-time-string
  244. "%F %T"
  245. (org-time-string-to-time "2022-03-31 23:31")))))
  246. (org-test-with-timezone "Europe/Madrid"
  247. (should (string-equal
  248. "2022-03-24 23:32:00 +0100 CET"
  249. (format-time-string
  250. "%F %T %z %Z"
  251. (org-time-string-to-time "2022-03-24 23:32"))))
  252. (should (string-equal
  253. "2022-03-31 23:33:00 +0200 CEST"
  254. (format-time-string
  255. "%F %T %z %Z"
  256. (org-time-string-to-time "2022-03-31 23:33"))))))
  257. (ert-deftest test-org/org-read-date ()
  258. "Test `org-read-date' specifications."
  259. (defvar org-time-was-given) ; dynamically scoped parameter
  260. ;; Parse ISO date with abbreviated year and month.
  261. (should (equal "2012-03-29 16:40"
  262. (let ((org-time-was-given t))
  263. (org-read-date t nil "12-3-29 16:40"))))
  264. ;; Parse Europeans dates.
  265. (should (equal "2012-03-29 16:40"
  266. (let ((org-time-was-given t))
  267. (org-read-date t nil "29.03.2012 16:40"))))
  268. ;; Parse Europeans dates without year.
  269. (should (string-match "2[0-9]\\{3\\}-03-29 16:40"
  270. (let ((org-time-was-given t))
  271. (org-read-date t nil "29.03. 16:40"))))
  272. ;; Relative date applied to current time if there is single
  273. ;; plus/minus, or to default date when there are two of them.
  274. (should
  275. (equal
  276. "2015-03-04"
  277. (org-test-at-time "2014-03-04"
  278. (org-read-date
  279. t nil "+1y" nil
  280. (org-time-string-to-time "2012-03-29")))))
  281. (should
  282. (equal
  283. "2013-03-29"
  284. (org-test-at-time "2014-03-04"
  285. (org-read-date
  286. t nil "++1y" nil
  287. (org-time-string-to-time "2012-03-29")))))
  288. ;; When `org-read-date-prefer-future' is non-nil, prefer future
  289. ;; dates (relatively to now) when incomplete. Otherwise, use
  290. ;; default date.
  291. (should
  292. (equal
  293. "2014-04-01"
  294. (org-test-at-time "2014-03-04"
  295. (let ((org-read-date-prefer-future t))
  296. (org-read-date t nil "1")))))
  297. (should
  298. (equal
  299. "2013-03-04"
  300. (org-test-at-time "2012-03-29"
  301. (let ((org-read-date-prefer-future t))
  302. (org-read-date t nil "3-4")))))
  303. (should
  304. (equal
  305. "2012-03-04"
  306. (org-test-at-time "2012-03-29"
  307. (let ((org-read-date-prefer-future nil))
  308. (org-read-date t nil "3-4")))))
  309. ;; When set to `org-read-date-prefer-future' is set to `time', read
  310. ;; day is moved to tomorrow if specified hour is before current
  311. ;; time. However, it only happens in no other part of the date is
  312. ;; specified.
  313. (should
  314. (equal
  315. "2012-03-30"
  316. (org-test-at-time "2012-03-29 16:40"
  317. (let ((org-read-date-prefer-future 'time))
  318. (org-read-date t nil "00:40" nil)))))
  319. (should-not
  320. (equal
  321. "2012-03-30"
  322. (org-test-at-time "2012-03-29 16:40"
  323. (let ((org-read-date-prefer-future 'time))
  324. (org-read-date t nil "29 00:40" nil)))))
  325. ;; Caveat: `org-read-date-prefer-future' always refers to current
  326. ;; time, not default time, when they differ.
  327. (should
  328. (equal
  329. "2014-04-01"
  330. (org-test-at-time "2014-03-04"
  331. (let ((org-read-date-prefer-future t))
  332. (org-read-date
  333. t nil "1" nil
  334. (org-time-string-to-time "2012-03-29"))))))
  335. (should
  336. (equal
  337. "2014-03-25"
  338. (org-test-at-time "2014-03-04"
  339. (let ((org-read-date-prefer-future t))
  340. (org-read-date
  341. t nil "25" nil
  342. (org-time-string-to-time "2012-03-29")))))))
  343. (ert-deftest test-org/org-parse-time-string ()
  344. "Test `org-parse-time-string'."
  345. (should (equal (org-parse-time-string "2012-03-29 16:40")
  346. '(0 40 16 29 3 2012 nil -1 nil)))
  347. (should (equal (org-parse-time-string "[2012-03-29 16:40]")
  348. '(0 40 16 29 3 2012 nil -1 nil)))
  349. (should (equal (org-parse-time-string "<2012-03-29 16:40>")
  350. '(0 40 16 29 3 2012 nil -1 nil)))
  351. (should (equal (org-parse-time-string "<2012-03-29>")
  352. '(0 0 0 29 3 2012 nil -1 nil)))
  353. (should (equal (org-parse-time-string "<2012-03-29>" t)
  354. '(0 nil nil 29 3 2012 nil -1 nil))))
  355. (ert-deftest test-org/closest-date ()
  356. "Test `org-closest-date' specifications."
  357. (require 'calendar)
  358. ;; Time stamps without a repeater are returned unchanged.
  359. (should
  360. (equal
  361. '(3 29 2012)
  362. (calendar-gregorian-from-absolute
  363. (org-closest-date "<2012-03-29>" "<2014-03-04>" nil))))
  364. ;; Time stamps with a null repeater are returned unchanged.
  365. (should
  366. (equal
  367. '(3 29 2012)
  368. (calendar-gregorian-from-absolute
  369. (org-closest-date "<2012-03-29 +0d>" "<2014-03-04>" nil))))
  370. ;; if PREFER is set to `past' always return a date before, or equal
  371. ;; to CURRENT.
  372. (should
  373. (equal
  374. '(3 1 2014)
  375. (calendar-gregorian-from-absolute
  376. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'past))))
  377. (should
  378. (equal
  379. '(3 4 2014)
  380. (calendar-gregorian-from-absolute
  381. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'past))))
  382. ;; if PREFER is set to `future' always return a date before, or equal
  383. ;; to CURRENT.
  384. (should
  385. (equal
  386. '(3 29 2014)
  387. (calendar-gregorian-from-absolute
  388. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" 'future))))
  389. (should
  390. (equal
  391. '(3 4 2014)
  392. (calendar-gregorian-from-absolute
  393. (org-closest-date "<2012-03-04 +1m>" "<2014-03-04>" 'future))))
  394. ;; If PREFER is neither `past' nor `future', select closest date.
  395. (should
  396. (equal
  397. '(3 1 2014)
  398. (calendar-gregorian-from-absolute
  399. (org-closest-date "<2012-03-29 +1m>" "<2014-03-04>" nil))))
  400. (should
  401. (equal
  402. '(5 4 2014)
  403. (calendar-gregorian-from-absolute
  404. (org-closest-date "<2012-03-04 +1m>" "<2014-04-28>" nil))))
  405. ;; Test "day" repeater.
  406. (should
  407. (equal '(3 8 2014)
  408. (calendar-gregorian-from-absolute
  409. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'past))))
  410. (should
  411. (equal '(3 10 2014)
  412. (calendar-gregorian-from-absolute
  413. (org-closest-date "<2014-03-04 +2d>" "<2014-03-09>" 'future))))
  414. ;; Test "month" repeater.
  415. (should
  416. (equal '(1 5 2015)
  417. (calendar-gregorian-from-absolute
  418. (org-closest-date "<2014-03-05 +2m>" "<2015-02-04>" 'past))))
  419. (should
  420. (equal '(3 29 2014)
  421. (calendar-gregorian-from-absolute
  422. (org-closest-date "<2012-03-29 +2m>" "<2014-03-04>" 'future))))
  423. ;; Test "year" repeater.
  424. (should
  425. (equal '(3 5 2014)
  426. (calendar-gregorian-from-absolute
  427. (org-closest-date "<2014-03-05 +2y>" "<2015-02-04>" 'past))))
  428. (should
  429. (equal '(3 29 2014)
  430. (calendar-gregorian-from-absolute
  431. (org-closest-date "<2012-03-29 +2y>" "<2014-03-04>" 'future)))))
  432. (ert-deftest test-org/deadline-close-p ()
  433. "Test `org-deadline-close-p' specifications."
  434. (org-test-at-time "2016-06-03 Fri 01:43"
  435. ;; Timestamps are close if they are within `ndays' of lead time.
  436. (org-test-with-temp-text "* Heading"
  437. (should (org-deadline-close-p "2016-06-03 Fri" 0))
  438. (should (org-deadline-close-p "2016-06-02 Thu" 0))
  439. (should-not (org-deadline-close-p "2016-06-04 Sat" 0))
  440. (should (org-deadline-close-p "2016-06-04 Sat" 1))
  441. (should (org-deadline-close-p "2016-06-03 Fri 12:00" 0)))
  442. ;; Read `ndays' from timestamp if argument not given.
  443. (org-test-with-temp-text "* H"
  444. (should (org-deadline-close-p "2016-06-04 Sat -1d"))
  445. (should-not (org-deadline-close-p "2016-06-04 Sat -0d"))
  446. (should (org-deadline-close-p "2016-06-10 Fri -1w"))
  447. (should-not (org-deadline-close-p "2016-06-11 Sat -1w")))
  448. ;; Prefer `ndays' argument over lead time in timestamp.
  449. (org-test-with-temp-text "* H"
  450. (should (org-deadline-close-p "2016-06-04 Sat -0d" 1))
  451. (should-not (org-deadline-close-p "2016-06-04 Sat -0d" 0)))
  452. ;; Completed tasks are never close.
  453. (let ((org-todo-keywords '(("TODO" "|" "DONE"))))
  454. (org-test-with-temp-text "* TODO Heading"
  455. (should (org-deadline-close-p "2016-06-03")))
  456. (org-test-with-temp-text "* DONE Heading"
  457. (should-not (org-deadline-close-p "2016-06-03"))))))
  458. ;;; Drawers
  459. (ert-deftest test-org/at-property-p ()
  460. "Test `org-at-property-p' specifications."
  461. (should
  462. (equal 't
  463. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:PROP: t\n:END:\n"
  464. (org-at-property-p))))
  465. (should
  466. (equal 't
  467. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n:END:\n"
  468. (org-at-property-p)))))
  469. (ert-deftest test-org/at-property-drawer-p ()
  470. "Test `org-at-property-drawer-p' specifications."
  471. (should
  472. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:PROP: t\n:END:\n"
  473. (org-at-property-drawer-p)))
  474. (should
  475. (org-test-with-temp-text ":PROPERTIES:\n:PROP: t\n:END:\n"
  476. (org-at-property-drawer-p)))
  477. ;; The function only returns t if point is at the first line of
  478. ;; a property block.
  479. (should-not
  480. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n:END:\n"
  481. (org-at-property-drawer-p)))
  482. ;; The function ignores incomplete drawers.
  483. (should-not
  484. (org-test-with-temp-text ":PROPERTIES:\n<point>:PROP: t\n"
  485. (org-at-property-drawer-p)))
  486. ;; tab separating the value.
  487. (should
  488. (org-test-with-temp-text ":PROPERTIES:\n:PROP: t\n:END:\n"
  489. (org-at-property-drawer-p))))
  490. (ert-deftest test-org/get-property-block ()
  491. "Test `org-get-property-block' specifications."
  492. (should
  493. (equal '(14 . 14)
  494. (org-test-with-temp-text ":PROPERTIES:\n:END:\n* H\n"
  495. (org-get-property-block))))
  496. (should
  497. (equal '(14 . 14)
  498. (org-test-with-temp-text ":PROPERTIES:\n:END:\n"
  499. (org-get-property-block))))
  500. ;; Comments above a document property block is ok.
  501. (should
  502. (equal '(18 . 18)
  503. (org-test-with-temp-text "# C\n:PROPERTIES:\n:END:\n"
  504. (org-get-property-block))))
  505. ;; Keywords above a document property block is ok.
  506. (should
  507. (equal '(22 . 22)
  508. (org-test-with-temp-text "# C\n# C\n:PROPERTIES:\n:END:\n"
  509. (org-get-property-block))))
  510. ;; Comments and keywords are allowed before a document property block.
  511. (should
  512. (equal '(18 . 27)
  513. (org-test-with-temp-text "# C\n:PROPERTIES:\n:KEY: V:\n:END:\n"
  514. (org-get-property-block))))
  515. ;; A document property block will not be valid if there are lines
  516. ;; with whitespace above it
  517. (should-not
  518. (org-test-with-temp-text "\n:PROPERTIES:\n:END:\n"
  519. (org-get-property-block)))
  520. (should
  521. (equal '(18 . 18)
  522. (org-test-with-temp-text "* H\n:PROPERTIES:\n:END:\n<point>"
  523. (org-get-property-block))))
  524. (should
  525. (equal "* H\n:PROPERTIES:\n:END:\n"
  526. (org-test-with-temp-text "* H"
  527. (let ((org-adapt-indentation nil))
  528. (org-get-property-block nil 'force))
  529. (buffer-string))))
  530. (should
  531. (equal ":PROPERTIES:\n:END:\n"
  532. (org-test-with-temp-text ""
  533. (org-get-property-block nil 'force)
  534. (buffer-string))))
  535. (should
  536. (equal "* H1\n :PROPERTIES:\n :END:\n* H2"
  537. (org-test-with-temp-text "* H1\n* H2"
  538. (let ((org-adapt-indentation t))
  539. (org-get-property-block nil 'force))
  540. (buffer-string)))))
  541. (ert-deftest test-org/insert-property-drawer ()
  542. "Test `org-insert-property-drawer' specifications."
  543. ;; Insert drawer in empty buffer
  544. (should
  545. (equal ":PROPERTIES:\n:END:\n"
  546. (org-test-with-temp-text ""
  547. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  548. (buffer-string))))
  549. ;; Insert drawer in document header with existing comment and
  550. ;; keyword.
  551. (should
  552. (equal "# C\n:PROPERTIES:\n:END:\n#+TITLE: T"
  553. (org-test-with-temp-text "# C\n#+TITLE: T"
  554. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  555. (buffer-string))))
  556. ;; Insert drawer in document header with existing keyword.
  557. (should
  558. (equal ":PROPERTIES:\n:END:\n#+TITLE: T"
  559. (org-test-with-temp-text "#+TITLE: T"
  560. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  561. (buffer-string))))
  562. (should
  563. (equal ":PROPERTIES:\n:END:"
  564. (org-test-with-temp-text ":PROPERTIES:\n:END:"
  565. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  566. (buffer-string))))
  567. ;; Insert drawer in document header with one existing heading in buffer.
  568. (should
  569. (equal ":PROPERTIES:\n:END:\n\n* T\n"
  570. (org-test-with-temp-text "\n* T\n"
  571. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  572. (buffer-string))))
  573. ;; Insert drawer right after headline if there is no planning line,
  574. ;; or after it otherwise.
  575. (should
  576. (equal "* H\n:PROPERTIES:\n:END:\nParagraph"
  577. (org-test-with-temp-text "* H\nParagraph<point>"
  578. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  579. (buffer-string))))
  580. (should
  581. (equal "* H\nDEADLINE: <2014-03-04 tue.>\n:PROPERTIES:\n:END:\nParagraph"
  582. (org-test-with-temp-text
  583. "* H\nDEADLINE: <2014-03-04 tue.>\nParagraph<point>"
  584. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  585. (buffer-string))))
  586. ;; Indent inserted drawer.
  587. (should
  588. (equal "* H\n :PROPERTIES:\n :END:\nParagraph"
  589. (org-test-with-temp-text "* H\nParagraph<point>"
  590. (let ((org-adapt-indentation t)) (org-insert-property-drawer))
  591. (buffer-string))))
  592. ;; Handle insertion at eob.
  593. (should
  594. (equal "* H\n:PROPERTIES:\n:END:\n"
  595. (org-test-with-temp-text "* H"
  596. (let ((org-adapt-indentation nil)) (org-insert-property-drawer))
  597. (buffer-string))))
  598. ;; Skip inlinetasks before point.
  599. (when (featurep 'org-inlinetask)
  600. (should
  601. (equal "* H\n:PROPERTIES:\n:END:\n*************** I\n*************** END\nP"
  602. (org-test-with-temp-text
  603. "* H\n*************** I\n*************** END\nP<point>"
  604. (let ((org-adapt-indentation nil)
  605. (org-inlinetask-min-level 15))
  606. (org-insert-property-drawer))
  607. (buffer-string)))))
  608. ;; Correctly set drawer in an inlinetask.
  609. (when (featurep 'org-inlinetask)
  610. (should
  611. (equal "* H\n*************** I\n:PROPERTIES:\n:END:\nP\n*************** END"
  612. (org-test-with-temp-text
  613. "* H\n*************** I\nP<point>\n*************** END"
  614. (let ((org-adapt-indentation nil)
  615. (org-inlinetask-min-level 15))
  616. (org-insert-property-drawer))
  617. (buffer-string))))))
  618. ;;; Filling
  619. (ert-deftest test-org/fill-element ()
  620. "Test `org-fill-element' specifications."
  621. ;; At an Org table, align it.
  622. (should
  623. (equal "| a |\n"
  624. (org-test-with-temp-text "|a|"
  625. (org-fill-element)
  626. (buffer-string))))
  627. (should
  628. (equal "#+name: table\n| a |\n"
  629. (org-test-with-temp-text "#+name: table\n| a |\n"
  630. (org-fill-element)
  631. (buffer-string))))
  632. ;; At a paragraph, preserve line breaks.
  633. (org-test-with-temp-text "some \\\\\nlong\ntext"
  634. (let ((fill-column 20))
  635. (org-fill-element)
  636. (should (equal (buffer-string) "some \\\\\nlong text"))))
  637. ;; Correctly fill a paragraph when point is at its very end.
  638. (should
  639. (equal "A B"
  640. (org-test-with-temp-text "A\nB"
  641. (let ((fill-column 20))
  642. (goto-char (point-max))
  643. (org-fill-element)
  644. (buffer-string)))))
  645. ;; Correctly fill the last paragraph of a greater element.
  646. (should
  647. (equal "#+BEGIN_CENTER\n- 012345\n 789\n#+END_CENTER"
  648. (org-test-with-temp-text "#+BEGIN_CENTER\n- 012345 789\n#+END_CENTER"
  649. (let ((fill-column 8))
  650. (forward-line)
  651. (end-of-line)
  652. (org-fill-element)
  653. (buffer-string)))))
  654. ;; Correctly fill an element in a narrowed buffer.
  655. (should
  656. (equal "01234\n6"
  657. (org-test-with-temp-text "01234 6789"
  658. (let ((fill-column 5))
  659. (narrow-to-region 1 8)
  660. (org-fill-element)
  661. (buffer-string)))))
  662. ;; Handle `adaptive-fill-regexp' in paragraphs.
  663. (should
  664. (equal "> a b"
  665. (org-test-with-temp-text "> a\n> b"
  666. (let ((fill-column 5)
  667. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  668. (org-fill-element)
  669. (buffer-string)))))
  670. ;; Special case: Fill first paragraph when point is at an item or
  671. ;; a plain-list or a footnote reference.
  672. (should
  673. (equal "- A B"
  674. (org-test-with-temp-text "- A\n B"
  675. (let ((fill-column 20))
  676. (org-fill-element)
  677. (buffer-string)))))
  678. (should
  679. (equal "[fn:1] A B"
  680. (org-test-with-temp-text "[fn:1] A\nB"
  681. (let ((fill-column 20))
  682. (org-fill-element)
  683. (buffer-string)))))
  684. (org-test-with-temp-text "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"
  685. (let ((fill-column 20))
  686. (org-fill-element)
  687. (should (equal (buffer-string)
  688. "#+BEGIN_VERSE\nSome \\\\\nlong\ntext\n#+END_VERSE"))))
  689. ;; Fill contents of `comment-block' elements.
  690. (should
  691. (equal
  692. (org-test-with-temp-text "#+BEGIN_COMMENT\nSome\ntext\n#+END_COMMENT"
  693. (let ((fill-column 20))
  694. (forward-line)
  695. (org-fill-element)
  696. (buffer-string)))
  697. "#+BEGIN_COMMENT\nSome text\n#+END_COMMENT"))
  698. ;; Fill `comment' elements.
  699. (should
  700. (equal " # A B"
  701. (org-test-with-temp-text " # A\n # B"
  702. (let ((fill-column 20))
  703. (org-fill-element)
  704. (buffer-string)))))
  705. ;; Do not mix consecutive comments when filling one of them.
  706. (should
  707. (equal "# A B\n\n# C"
  708. (org-test-with-temp-text "# A\n# B\n\n# C"
  709. (let ((fill-column 20))
  710. (org-fill-element)
  711. (buffer-string)))))
  712. ;; Use commented empty lines as separators when filling comments.
  713. (should
  714. (equal "# A B\n#\n# C"
  715. (org-test-with-temp-text "# A\n# B\n#\n# C"
  716. (let ((fill-column 20))
  717. (org-fill-element)
  718. (buffer-string)))))
  719. ;; Handle `adaptive-fill-regexp' in comments.
  720. (should
  721. (equal "# > a b"
  722. (org-test-with-temp-text "# > a\n# > b"
  723. (let ((fill-column 20)
  724. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  725. (org-fill-element)
  726. (buffer-string)))))
  727. ;; Do nothing at affiliated keywords.
  728. (should
  729. (equal "#+NAME: para\nSome\ntext."
  730. (org-test-with-temp-text "#+NAME: para\nSome\ntext."
  731. (let ((fill-column 20))
  732. (org-fill-element)
  733. (buffer-string)))))
  734. ;; Do not move point after table when filling a table.
  735. (should-not
  736. (org-test-with-temp-text "| a | b |\n| c | d |\n"
  737. (forward-char)
  738. (org-fill-element)
  739. (eobp)))
  740. ;; Do not fill "n" macro, with or without arguments, followed by
  741. ;; a dot or a closing parenthesis since it could be confused with
  742. ;; a numbered bullet.
  743. (should-not
  744. (equal "123456789\n{{{n}}}."
  745. (org-test-with-temp-text "123456789 {{{n}}}."
  746. (let ((fill-column 10))
  747. (org-fill-element)
  748. (buffer-string)))))
  749. (should-not
  750. (equal "123456789\n{{{n}}}\)"
  751. (org-test-with-temp-text "123456789 {{{n}}}\)"
  752. (let ((fill-column 10))
  753. (org-fill-element)
  754. (buffer-string)))))
  755. (should-not
  756. (equal "123456789\n{{{n()}}}."
  757. (org-test-with-temp-text "123456789 {{{n()}}}."
  758. (let ((fill-column 10))
  759. (org-fill-element)
  760. (buffer-string)))))
  761. (should-not
  762. (equal "123456789\n{{{n(counter)}}}."
  763. (org-test-with-temp-text "123456789 {{{n(counter)}}}."
  764. (let ((fill-column 10))
  765. (org-fill-element)
  766. (buffer-string))))))
  767. (ert-deftest test-org/fill-paragraph ()
  768. "Test `org-fill-paragraph' specifications."
  769. ;; Regular test.
  770. (should
  771. (equal "012345678\n9"
  772. (org-test-with-temp-text "012345678 9"
  773. (let ((fill-column 10))
  774. (org-fill-paragraph)
  775. (buffer-string)))))
  776. ;; Fill paragraph even at end of buffer.
  777. (should
  778. (equal "012345678\n9\n"
  779. (org-test-with-temp-text "012345678 9\n<point>"
  780. (let ((fill-column 10))
  781. (org-fill-paragraph)
  782. (buffer-string)))))
  783. ;; Between two paragraphs, fill the next one.
  784. (should
  785. (equal "012345678 9\n\n012345678\n9"
  786. (org-test-with-temp-text "012345678 9\n<point>\n012345678 9"
  787. (let ((fill-column 10))
  788. (org-fill-paragraph)
  789. (buffer-string)))))
  790. (should
  791. (equal "012345678\n9\n\n012345678 9"
  792. (org-test-with-temp-text "012345678 9<point>\n\n012345678 9"
  793. (let ((fill-column 10))
  794. (org-fill-paragraph)
  795. (buffer-string)))))
  796. ;; Fill paragraph in a comment block.
  797. (should
  798. (equal "#+begin_comment\n012345678\n9\n#+end_comment"
  799. (org-test-with-temp-text
  800. "#+begin_comment\n<point>012345678 9\n#+end_comment"
  801. (let ((fill-column 10))
  802. (org-fill-paragraph)
  803. (buffer-string)))))
  804. ;; When a region is selected, fill every paragraph in the region.
  805. (should
  806. (equal "012345678\n9\n\n012345678\n9"
  807. (org-test-with-temp-text "012345678 9\n\n012345678 9"
  808. (let ((fill-column 10))
  809. (transient-mark-mode 1)
  810. (push-mark (point-min) t t)
  811. (goto-char (point-max))
  812. (call-interactively #'org-fill-paragraph)
  813. (buffer-string)))))
  814. (should
  815. (equal "012345678\n9\n\n012345678 9"
  816. (org-test-with-temp-text "012345678 9\n<point>\n012345678 9"
  817. (let ((fill-column 10))
  818. (transient-mark-mode 1)
  819. (push-mark (point) t t)
  820. (goto-char (point-min))
  821. (call-interactively #'org-fill-paragraph)
  822. (buffer-string)))))
  823. (should
  824. (equal "012345678 9\n\n012345678\n9"
  825. (org-test-with-temp-text "012345678 9\n<point>\n012345678 9"
  826. (let ((fill-column 10))
  827. (transient-mark-mode 1)
  828. (push-mark (point) t t)
  829. (goto-char (point-max))
  830. (call-interactively #'org-fill-paragraph)
  831. (buffer-string)))))
  832. ;; Fill every list item in a region
  833. (should
  834. (equal "\n- 2345678\n 9\n- 2345678\n 9"
  835. (org-test-with-temp-text "\n- 2345678 9\n- 2345678 9"
  836. (let ((fill-column 10))
  837. (transient-mark-mode 1)
  838. (push-mark (point-min) t t)
  839. (goto-char (point-max))
  840. (call-interactively #'org-fill-paragraph)
  841. (buffer-string)))))
  842. (should
  843. (equal "\n- 2345678\n 9\n- 2345678"
  844. (org-test-with-temp-text "\n- 2345678 9\n- 2345678"
  845. (let ((fill-column 10))
  846. (transient-mark-mode 1)
  847. (push-mark (point-min) t t)
  848. (goto-char (point-max))
  849. (call-interactively #'org-fill-paragraph)
  850. (buffer-string))))))
  851. (ert-deftest test-org/fill-region ()
  852. "Test `fill-region' behaviour."
  853. ;; fill-region should fill every item of a list
  854. (should
  855. (equal "\n- 2345678\n 9\n- 2345678\n 9"
  856. (org-test-with-temp-text "\n- 2345678 9\n- 2345678 9"
  857. (let ((fill-column 10))
  858. (transient-mark-mode 1)
  859. (push-mark (point-min) t t)
  860. (goto-char (point-max))
  861. (call-interactively #'fill-region)
  862. (buffer-string)))))
  863. (should
  864. (equal "\n- 1 2\n- 1 2"
  865. (org-test-with-temp-text "\n- 1\n 2\n- 1\n 2"
  866. (let ((fill-column 10))
  867. (transient-mark-mode 1)
  868. (push-mark (point-min) t t)
  869. (goto-char (point-max))
  870. (call-interactively #'fill-region)
  871. (buffer-string))))) )
  872. (ert-deftest test-org/auto-fill-function ()
  873. "Test auto-filling features."
  874. ;; Auto fill paragraph.
  875. (should
  876. (equal "12345\n7890"
  877. (org-test-with-temp-text "12345 7890"
  878. (let ((fill-column 5))
  879. (end-of-line)
  880. (org-auto-fill-function)
  881. (buffer-string)))))
  882. ;; Auto fill first paragraph in an item.
  883. (should
  884. (equal "- 12345\n 7890"
  885. (org-test-with-temp-text "- 12345 7890"
  886. (let ((fill-column 7))
  887. (end-of-line)
  888. (org-auto-fill-function)
  889. (buffer-string)))))
  890. ;; Auto fill paragraph when `adaptive-fill-regexp' matches.
  891. (should
  892. (equal "> 12345\n 7890"
  893. (org-test-with-temp-text "> 12345 7890"
  894. (let ((fill-column 10)
  895. (adaptive-fill-regexp "[ \t]*>+[ \t]*")
  896. (adaptive-fill-first-line-regexp "\\`[ ]*\\'"))
  897. (end-of-line)
  898. (org-auto-fill-function)
  899. (buffer-string)))))
  900. (should
  901. (equal "> 12345\n> 12345\n> 7890"
  902. (org-test-with-temp-text "> 12345\n> 12345 7890"
  903. (let ((fill-column 10)
  904. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  905. (goto-char (point-max))
  906. (org-auto-fill-function)
  907. (buffer-string)))))
  908. (should-not
  909. (equal " 12345\n *12345\n *12345"
  910. (org-test-with-temp-text " 12345\n *12345 12345"
  911. (let ((fill-column 10)
  912. (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
  913. (goto-char (point-max))
  914. (org-auto-fill-function)
  915. (buffer-string)))))
  916. ;; Auto fill comments.
  917. (should
  918. (equal " # 12345\n # 7890"
  919. (org-test-with-temp-text " # 12345 7890"
  920. (let ((fill-column 10))
  921. (end-of-line)
  922. (org-auto-fill-function)
  923. (buffer-string)))))
  924. ;; A hash within a line isn't a comment.
  925. (should-not
  926. (equal "12345 # 7890\n# 1"
  927. (org-test-with-temp-text "12345 # 7890 1"
  928. (let ((fill-column 12))
  929. (end-of-line)
  930. (org-auto-fill-function)
  931. (buffer-string)))))
  932. ;; Correctly interpret empty prefix.
  933. (should-not
  934. (equal "# a\n# b\nRegular\n# paragraph"
  935. (org-test-with-temp-text "# a\n# b\nRegular paragraph"
  936. (let ((fill-column 12))
  937. (end-of-line 3)
  938. (org-auto-fill-function)
  939. (buffer-string)))))
  940. ;; Comment block: auto fill contents.
  941. (should
  942. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  943. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  944. (let ((fill-column 5))
  945. (forward-line)
  946. (end-of-line)
  947. (org-auto-fill-function)
  948. (buffer-string)))))
  949. (should
  950. (equal "#+BEGIN_COMMENT\n12345\n7890\n#+END_COMMENT"
  951. (org-test-with-temp-text "#+BEGIN_COMMENT\n12345 7890\n#+END_COMMENT"
  952. (let ((fill-column 5))
  953. (forward-line)
  954. (end-of-line)
  955. (org-auto-fill-function)
  956. (buffer-string)))))
  957. ;; Do not fill if a new item could be created.
  958. (should-not
  959. (equal "12345\n- 90"
  960. (org-test-with-temp-text "12345 - 90"
  961. (let ((fill-column 5))
  962. (end-of-line)
  963. (org-auto-fill-function)
  964. (buffer-string)))))
  965. ;; Do not fill if a line break could be introduced.
  966. (should-not
  967. (equal "123\\\\\n7890"
  968. (org-test-with-temp-text "123\\\\ 7890"
  969. (let ((fill-column 6))
  970. (end-of-line)
  971. (org-auto-fill-function)
  972. (buffer-string)))))
  973. ;; Do not fill affiliated keywords.
  974. (should-not
  975. (equal "#+ATTR_LATEX: ABC\nDEFGHIJKL"
  976. (org-test-with-temp-text "#+ATTR_LATEX: ABC DEFGHIJKL"
  977. (let ((fill-column 20))
  978. (end-of-line)
  979. (org-auto-fill-function)
  980. (buffer-string))))))
  981. ;;; Indentation
  982. (ert-deftest test-org/indent-line ()
  983. "Test `org-indent-line' specifications."
  984. ;; Do not indent diary sexps, footnote definitions or headlines.
  985. (should
  986. (zerop
  987. (org-test-with-temp-text "%%(org-calendar-holiday)"
  988. (org-indent-line)
  989. (org-get-indentation))))
  990. (should
  991. (zerop
  992. (org-test-with-temp-text "[fn:1] fn"
  993. (let ((org-adapt-indentation t)) (org-indent-line))
  994. (org-get-indentation))))
  995. (should
  996. (zerop
  997. (org-test-with-temp-text "* H"
  998. (org-indent-line)
  999. (org-get-indentation))))
  1000. ;; Do not indent before first headline.
  1001. (should
  1002. (zerop
  1003. (org-test-with-temp-text ""
  1004. (org-indent-line)
  1005. (org-get-indentation))))
  1006. ;; Indent according to headline level otherwise, unless
  1007. ;; `org-adapt-indentation' is nil.
  1008. (should
  1009. (= 2
  1010. (org-test-with-temp-text "* H\n<point>A"
  1011. (let ((org-adapt-indentation t)) (org-indent-line))
  1012. (org-get-indentation))))
  1013. (should
  1014. (= 2
  1015. (org-test-with-temp-text "* H\n<point>\nA"
  1016. (let ((org-adapt-indentation t)) (org-indent-line))
  1017. (org-get-indentation))))
  1018. (should
  1019. (zerop
  1020. (org-test-with-temp-text "* H\n<point>A"
  1021. (let ((org-adapt-indentation nil)) (org-indent-line))
  1022. (org-get-indentation))))
  1023. ;; Indenting preserves point position.
  1024. (should
  1025. (org-test-with-temp-text "* H\nA<point>B"
  1026. (let ((org-adapt-indentation t)) (org-indent-line))
  1027. (looking-at "B")))
  1028. ;; Do not change indentation at an item or a LaTeX environment.
  1029. (should
  1030. (= 1
  1031. (org-test-with-temp-text "* H\n<point> - A"
  1032. (let ((org-adapt-indentation t)) (org-indent-line))
  1033. (org-get-indentation))))
  1034. (should
  1035. (= 1
  1036. (org-test-with-temp-text
  1037. "\\begin{equation}\n <point>1+1=2\n\\end{equation}"
  1038. (org-indent-line)
  1039. (org-get-indentation))))
  1040. ;; On blank lines at the end of a list, indent like last element
  1041. ;; within it if the line is still in the list. If the last element
  1042. ;; is an item, indent like its contents. Otherwise, indent like the
  1043. ;; whole list.
  1044. (should
  1045. (= 4
  1046. (org-test-with-temp-text "* H\n- A\n - AA\n<point>"
  1047. (let ((org-adapt-indentation t)) (org-indent-line))
  1048. (org-get-indentation))))
  1049. (should
  1050. (= 4
  1051. (org-test-with-temp-text "* H\n- A\n -\n\n<point>"
  1052. (let ((org-adapt-indentation t)) (org-indent-line))
  1053. (org-get-indentation))))
  1054. (should
  1055. (zerop
  1056. (org-test-with-temp-text "* H\n- A\n - AA\n\n\n\n<point>"
  1057. (let ((org-adapt-indentation t)) (org-indent-line))
  1058. (org-get-indentation))))
  1059. (should
  1060. (= 4
  1061. (org-test-with-temp-text "* H\n- A\n - \n<point>"
  1062. (let ((org-adapt-indentation t)) (org-indent-line))
  1063. (org-get-indentation))))
  1064. (should
  1065. (= 4
  1066. (org-test-with-temp-text
  1067. "* H\n - \n #+BEGIN_SRC emacs-lisp\n t\n #+END_SRC\n<point>"
  1068. (let ((org-adapt-indentation t)) (org-indent-line))
  1069. (org-get-indentation))))
  1070. (should
  1071. (= 2
  1072. (org-test-with-temp-text "- A\n B\n\n<point>"
  1073. (let ((org-adapt-indentation nil)) (org-indent-line))
  1074. (org-get-indentation))))
  1075. (should
  1076. (= 2
  1077. (org-test-with-temp-text
  1078. "- A\n \begin{cases} 1 + 1\n \end{cases}\n\n<point>"
  1079. (let ((org-adapt-indentation nil)) (org-indent-line))
  1080. (org-get-indentation))))
  1081. ;; Likewise, on a blank line at the end of a footnote definition,
  1082. ;; indent at column 0 if line belongs to the definition. Otherwise,
  1083. ;; indent like the definition itself.
  1084. (should
  1085. (zerop
  1086. (org-test-with-temp-text "* H\n[fn:1] Definition\n<point>"
  1087. (let ((org-adapt-indentation t)) (org-indent-line))
  1088. (org-get-indentation))))
  1089. (should
  1090. (zerop
  1091. (org-test-with-temp-text "* H\n[fn:1] Definition\n\n\n\n<point>"
  1092. (let ((org-adapt-indentation t)) (org-indent-line))
  1093. (org-get-indentation))))
  1094. ;; After the end of the contents of a greater element, indent like
  1095. ;; the beginning of the element.
  1096. (should
  1097. (= 1
  1098. (org-test-with-temp-text
  1099. " #+BEGIN_CENTER\n Contents\n<point>#+END_CENTER"
  1100. (org-indent-line)
  1101. (org-get-indentation))))
  1102. ;; On blank lines after a paragraph, indent like its last non-empty
  1103. ;; line.
  1104. (should
  1105. (= 1
  1106. (org-test-with-temp-text " Paragraph\n\n<point>"
  1107. (org-indent-line)
  1108. (org-get-indentation))))
  1109. ;; At the first line of an element, indent like previous element's
  1110. ;; first line, ignoring footnotes definitions and inline tasks, or
  1111. ;; according to parent.
  1112. (let ((org-adapt-indentation t))
  1113. (should
  1114. (= 2
  1115. (org-test-with-temp-text "A\n\n B\n\nC<point>"
  1116. (org-indent-line)
  1117. (org-get-indentation))))
  1118. (should
  1119. (= 1
  1120. (org-test-with-temp-text " A\n\n[fn:1] B\n\n\nC<point>"
  1121. (org-indent-line)
  1122. (org-get-indentation))))
  1123. (should
  1124. (= 1
  1125. (org-test-with-temp-text
  1126. " #+BEGIN_CENTER\n<point> Contents\n#+END_CENTER"
  1127. (org-indent-line)
  1128. (org-get-indentation)))))
  1129. ;; Within code part of a source block, use language major mode if
  1130. ;; `org-src-tab-acts-natively' is non-nil. Otherwise, indent
  1131. ;; according to line above.
  1132. (should
  1133. (= 6
  1134. (org-test-with-temp-text
  1135. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  1136. (let ((org-src-tab-acts-natively t)
  1137. (org-edit-src-content-indentation 0))
  1138. (org-indent-line))
  1139. (org-get-indentation))))
  1140. (should
  1141. (= 1
  1142. (org-test-with-temp-text
  1143. "#+BEGIN_SRC emacs-lisp\n (and A\n<point>B)\n#+END_SRC"
  1144. (let ((org-src-tab-acts-natively nil)
  1145. (org-edit-src-content-indentation 0))
  1146. (org-indent-line))
  1147. (org-get-indentation))))
  1148. ;; Otherwise, indent like the first non-blank line above.
  1149. (should
  1150. (zerop
  1151. (org-test-with-temp-text
  1152. "#+BEGIN_CENTER\nline1\n\n<point> line2\n#+END_CENTER"
  1153. (org-indent-line)
  1154. (org-get-indentation))))
  1155. ;; Align node properties according to `org-property-format'. Handle
  1156. ;; nicely empty values.
  1157. (should
  1158. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  1159. (org-test-with-temp-text
  1160. "* H\n:PROPERTIES:\n<point>:key: value\n:END:"
  1161. (let ((org-property-format "%-10s %s")) (org-indent-line))
  1162. (buffer-string))))
  1163. (should
  1164. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  1165. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:key:\n:END:"
  1166. (let ((org-property-format "%-10s %s")) (org-indent-line))
  1167. (buffer-string)))))
  1168. (ert-deftest test-org/indent-region ()
  1169. "Test `org-indent-region' specifications."
  1170. ;; Indent paragraph.
  1171. (let ((org-adapt-indentation t))
  1172. (should
  1173. (equal "A\nB\nC"
  1174. (org-test-with-temp-text " A\nB\n C"
  1175. (org-indent-region (point-min) (point-max))
  1176. (buffer-string)))))
  1177. ;; Indent greater elements along with their contents.
  1178. (should
  1179. (equal "#+BEGIN_CENTER\nA\nB\n#+END_CENTER"
  1180. (org-test-with-temp-text "#+BEGIN_CENTER\n A\n B\n#+END_CENTER"
  1181. (org-indent-region (point-min) (point-max))
  1182. (buffer-string))))
  1183. ;; Ignore contents of verse blocks. Only indent block delimiters.
  1184. (should
  1185. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  1186. (org-test-with-temp-text "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  1187. (org-indent-region (point-min) (point-max))
  1188. (buffer-string))))
  1189. (let ((org-adapt-indentation t))
  1190. (should
  1191. (equal "#+BEGIN_VERSE\n A\n B\n#+END_VERSE"
  1192. (org-test-with-temp-text " #+BEGIN_VERSE\n A\n B\n #+END_VERSE"
  1193. (org-indent-region (point-min) (point-max))
  1194. (buffer-string)))))
  1195. ;; Indent example blocks as a single block, unless indentation
  1196. ;; should be preserved. In this case only indent the block markers.
  1197. (should
  1198. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1199. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1200. (org-indent-region (point-min) (point-max))
  1201. (buffer-string))))
  1202. (let ((org-adapt-indentation t))
  1203. (should
  1204. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1205. (org-test-with-temp-text " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  1206. (org-indent-region (point-min) (point-max))
  1207. (buffer-string))))
  1208. (should
  1209. (equal "#+BEGIN_EXAMPLE -i\n A\n B\n#+END_EXAMPLE"
  1210. (org-test-with-temp-text
  1211. " #+BEGIN_EXAMPLE -i\n A\n B\n #+END_EXAMPLE"
  1212. (org-indent-region (point-min) (point-max))
  1213. (buffer-string))))
  1214. (should
  1215. (equal "#+BEGIN_EXAMPLE\n A\n B\n#+END_EXAMPLE"
  1216. (org-test-with-temp-text
  1217. " #+BEGIN_EXAMPLE\n A\n B\n #+END_EXAMPLE"
  1218. (let ((org-src-preserve-indentation t))
  1219. (org-indent-region (point-min) (point-max)))
  1220. (buffer-string)))))
  1221. ;; Treat export blocks as a whole.
  1222. (should
  1223. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1224. (org-test-with-temp-text "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1225. (org-indent-region (point-min) (point-max))
  1226. (buffer-string))))
  1227. (let ((org-adapt-indentation t))
  1228. (should
  1229. (equal "#+BEGIN_EXPORT latex\n A\n B\n#+END_EXPORT"
  1230. (org-test-with-temp-text
  1231. " #+BEGIN_EXPORT latex\n A\n B\n #+END_EXPORT"
  1232. (org-indent-region (point-min) (point-max))
  1233. (buffer-string)))))
  1234. ;; Indent according to mode if `org-src-tab-acts-natively' is
  1235. ;; non-nil. Otherwise, do not indent code at all.
  1236. (should
  1237. (equal "#+BEGIN_SRC emacs-lisp\n(and A\n B)\n#+END_SRC"
  1238. (org-test-with-temp-text
  1239. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1240. (let ((org-src-tab-acts-natively t)
  1241. (org-edit-src-content-indentation 0))
  1242. (org-indent-region (point-min) (point-max)))
  1243. (buffer-string))))
  1244. (should
  1245. (equal "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1246. (org-test-with-temp-text
  1247. "#+BEGIN_SRC emacs-lisp\n (and A\nB)\n#+END_SRC"
  1248. (let ((org-src-tab-acts-natively nil)
  1249. (org-edit-src-content-indentation 0))
  1250. (org-indent-region (point-min) (point-max)))
  1251. (buffer-string))))
  1252. ;; Align node properties according to `org-property-format'. Handle
  1253. ;; nicely empty values.
  1254. (should
  1255. (equal "* H\n:PROPERTIES:\n:key: value\n:END:"
  1256. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key: value\n:END:"
  1257. (let ((org-property-format "%-10s %s")
  1258. (org-adapt-indentation nil))
  1259. (org-indent-region (point) (point-max)))
  1260. (buffer-string))))
  1261. (should
  1262. (equal "* H\n:PROPERTIES:\n:key:\n:END:"
  1263. (org-test-with-temp-text "* H\n<point>:PROPERTIES:\n:key:\n:END:"
  1264. (let ((org-property-format "%-10s %s")
  1265. (org-adapt-indentation nil))
  1266. (org-indent-region (point) (point-max)))
  1267. (buffer-string))))
  1268. ;; Indent property drawers according to `org-adapt-indentation'.
  1269. (let ((org-adapt-indentation 'headline-data))
  1270. (should
  1271. (equal "* H\n :PROPERTIES:\n :key:\n :END:"
  1272. (org-test-with-temp-text "* H\n:PROPERTIES:\n:key:\n:END:"
  1273. (org-indent-region (point-min) (point-max))
  1274. (buffer-string)))))
  1275. ;; Indent planning according to `org-adapt-indentation'.
  1276. (let ((org-adapt-indentation 'headline-data))
  1277. (should
  1278. (equal "* H\n SCHEDULED: <2022-11-03>"
  1279. (org-test-with-temp-text "* H\nSCHEDULED: <2022-11-03>"
  1280. (org-indent-region (point-min) (point-max))
  1281. (buffer-string)))))
  1282. ;; Indent LOGBOOK according to `org-adapt-indentation'.
  1283. (let ((org-adapt-indentation 'headline-data))
  1284. (should
  1285. (equal "* H\n :LOGBOOK:
  1286. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1287. :END:"
  1288. (org-test-with-temp-text "* H\n:LOGBOOK:
  1289. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1290. :END:"
  1291. (org-indent-region (point-min) (point-max))
  1292. (buffer-string)))))
  1293. ;; Indent clock lines according to `org-adapt-indentation'.
  1294. (let ((org-adapt-indentation 'headline-data))
  1295. (should
  1296. (equal "* H
  1297. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1298. (org-test-with-temp-text "* H
  1299. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1300. (org-indent-region (point-min) (point-max))
  1301. (buffer-string)))))
  1302. ;; Do not indent beyond headline data.
  1303. (let ((org-adapt-indentation 'headline-data))
  1304. (should
  1305. (equal "* H\n SCHEDULED: <2022-11-03>\nParagraph"
  1306. (org-test-with-temp-text "* H\nSCHEDULED: <2022-11-03>\nParagraph"
  1307. (org-indent-region (point-min) (point-max))
  1308. (buffer-string)))))
  1309. (let ((org-adapt-indentation 'headline-data)
  1310. (org-log-into-drawer t))
  1311. (should
  1312. (equal "* TODO A task
  1313. :PROPERTIES:
  1314. :CAPTURED: [2022-09-11 dim. 21:25]
  1315. :END:
  1316. :LOGBOOK:
  1317. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1318. :END:
  1319. Paragraph"
  1320. (org-test-with-temp-text "* TODO A task
  1321. :PROPERTIES:
  1322. :CAPTURED: [2022-09-11 dim. 21:25]
  1323. :END:
  1324. :LOGBOOK:
  1325. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1326. :END:
  1327. Paragraph"
  1328. (org-indent-region (point-min) (point-max))
  1329. (buffer-string)))))
  1330. (let ((org-adapt-indentation 'headline-data))
  1331. (should
  1332. (equal "* TODO A task
  1333. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1334. Paragraph
  1335. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1336. (org-test-with-temp-text "* TODO A task
  1337. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46
  1338. Paragraph
  1339. CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
  1340. (org-indent-region (point-min) (point-max))
  1341. (buffer-string)))))
  1342. ;; Indent plain lists.
  1343. (let ((org-adapt-indentation t))
  1344. (should
  1345. (equal "- A\n B\n - C\n\n D"
  1346. (org-test-with-temp-text "- A\n B\n - C\n\n D"
  1347. (org-indent-region (point-min) (point-max))
  1348. (buffer-string))))
  1349. (should
  1350. (equal "- A\n\n- B"
  1351. (org-test-with-temp-text " - A\n\n - B"
  1352. (org-indent-region (point-min) (point-max))
  1353. (buffer-string)))))
  1354. ;; Indent footnote definitions.
  1355. (should
  1356. (equal "[fn:1] Definition\n\nDefinition"
  1357. (org-test-with-temp-text "[fn:1] Definition\n\n Definition"
  1358. (org-indent-region (point-min) (point-max))
  1359. (buffer-string))))
  1360. ;; Special case: Start indenting on a blank line.
  1361. (should
  1362. (equal "\nParagraph"
  1363. (org-test-with-temp-text "\n Paragraph"
  1364. (org-indent-region (point-min) (point-max))
  1365. (buffer-string)))))
  1366. ;;; Editing
  1367. (ert-deftest test-org/delete-indentation ()
  1368. "Test `org-delete-indentation' specifications."
  1369. ;; Regular test.
  1370. (should (equal "foo bar"
  1371. (org-test-with-temp-text
  1372. "foo \n bar<point>"
  1373. (org-delete-indentation)
  1374. (buffer-string))))
  1375. ;; With optional argument.
  1376. (should (equal "foo bar"
  1377. (org-test-with-temp-text
  1378. "foo<point> \n bar"
  1379. (org-delete-indentation t)
  1380. (buffer-string))))
  1381. ;; At headline text should be appended to the headline text.
  1382. (should
  1383. (equal"* foo bar :tag:"
  1384. (let (org-auto-align-tags)
  1385. (org-test-with-temp-text
  1386. "* foo :tag:\n bar<point>"
  1387. (org-delete-indentation)
  1388. (buffer-string)))))
  1389. (should
  1390. (equal "* foo bar :tag:"
  1391. (let (org-auto-align-tags)
  1392. (org-test-with-temp-text
  1393. "* foo <point>:tag:\n bar"
  1394. (org-delete-indentation t)
  1395. (buffer-string))))))
  1396. (ert-deftest test-org/return ()
  1397. "Test `org-return' specifications."
  1398. ;; Regular test.
  1399. (should
  1400. (equal "Para\ngraph"
  1401. (org-test-with-temp-text "Para<point>graph"
  1402. (org-return)
  1403. (buffer-string))))
  1404. ;; With optional argument, indent line.
  1405. (should
  1406. (equal " Para\n graph"
  1407. (org-test-with-temp-text " Para<point>graph"
  1408. (org-return t)
  1409. (buffer-string))))
  1410. ;; On a table, call `org-table-next-row'.
  1411. (should
  1412. (org-test-with-temp-text "| <point>a |\n| b |"
  1413. (org-return)
  1414. (looking-at-p "b")))
  1415. ;; Open link or timestamp under point when `org-return-follows-link'
  1416. ;; is non-nil.
  1417. (should
  1418. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1419. (let ((org-return-follows-link t)
  1420. (org-link-search-must-match-exact-headline nil))
  1421. (org-return))
  1422. (looking-at-p "<<target>>")))
  1423. (should-not
  1424. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1425. (let ((org-return-follows-link nil)) (org-return))
  1426. (looking-at-p "<<target>>")))
  1427. (should
  1428. (org-test-with-temp-text "* [[b][a<point>]]\n* b"
  1429. (let ((org-return-follows-link t)) (org-return))
  1430. (looking-at-p "* b")))
  1431. (should
  1432. (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
  1433. (let ((org-return-follows-link t)
  1434. (org-link-search-must-match-exact-headline nil))
  1435. (org-return))
  1436. (looking-at-p "<<target>>")))
  1437. (should-not
  1438. (org-test-with-temp-text "Link [[target]]<point> <<target>>"
  1439. (let ((org-return-follows-link t)
  1440. (org-link-search-must-match-exact-headline nil))
  1441. (org-return))
  1442. (looking-at-p "<<target>>")))
  1443. ;; When `org-return-follows-link' is non-nil, tolerate links and
  1444. ;; timestamps in comments, node properties, etc.
  1445. (should
  1446. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1447. (let ((org-return-follows-link t)
  1448. (org-link-search-must-match-exact-headline nil))
  1449. (org-return))
  1450. (looking-at-p "<<target>>")))
  1451. (should-not
  1452. (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
  1453. (let ((org-return-follows-link nil)) (org-return))
  1454. (looking-at-p "<<target>>")))
  1455. (should-not
  1456. (org-test-with-temp-text "# Comment [[target]]<point>\n <<target>>"
  1457. (let ((org-return-follows-link t)
  1458. (org-link-search-must-match-exact-headline nil))
  1459. (org-return))
  1460. (looking-at-p "<<target>>")))
  1461. ;; Non-nil `org-return-follows-link' ignores read-only state of
  1462. ;; a buffer.
  1463. (should
  1464. (org-test-with-temp-text "Link [[target<point>]] <<target>>"
  1465. (let ((org-return-follows-link t)
  1466. (org-link-search-must-match-exact-headline nil))
  1467. (setq buffer-read-only t)
  1468. (call-interactively #'org-return))
  1469. (looking-at-p "<<target>>")))
  1470. ;; `org-return-follows-link' handle multi-line lines.
  1471. (should
  1472. (org-test-with-temp-text
  1473. "[[target][This is a very\n long description<point>]]\n <<target>>"
  1474. (let ((org-return-follows-link t)
  1475. (org-link-search-must-match-exact-headline nil))
  1476. (org-return))
  1477. (looking-at-p "<<target>>")))
  1478. (should-not
  1479. (org-test-with-temp-text
  1480. "[[target][This is a very\n long description]]<point>\n <<target>>"
  1481. (let ((org-return-follows-link t)
  1482. (org-link-search-must-match-exact-headline nil))
  1483. (org-return))
  1484. (looking-at-p "<<target>>")))
  1485. ;; However, do not open link when point is in a table.
  1486. (should
  1487. (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"
  1488. (let ((org-return-follows-link t)) (org-return))
  1489. (looking-at-p "between")))
  1490. ;; Special case: in a list, when indenting, do not break structure.
  1491. (should
  1492. (equal "- A\n B"
  1493. (org-test-with-temp-text "- A <point>B"
  1494. (org-return t)
  1495. (buffer-string))))
  1496. (should
  1497. (equal "- A\n\n- B"
  1498. (org-test-with-temp-text "- A\n<point>- B"
  1499. (org-return t)
  1500. (buffer-string))))
  1501. ;; On tags part of a headline, add a newline below it instead of
  1502. ;; breaking it.
  1503. (should
  1504. (equal "* H :tag:\n"
  1505. (org-test-with-temp-text "* H :<point>tag:"
  1506. (org-return)
  1507. (buffer-string))))
  1508. ;; Before headline text, add a newline below it instead of breaking
  1509. ;; it.
  1510. (should
  1511. (equal "* TODO H :tag:\n"
  1512. (org-test-with-temp-text "* <point>TODO H :tag:"
  1513. (org-return)
  1514. (buffer-string))))
  1515. (should
  1516. (equal "* TODO [#B] H :tag:\n"
  1517. (org-test-with-temp-text "* TODO<point> [#B] H :tag:"
  1518. (org-return)
  1519. (buffer-string))))
  1520. (should ;TODO are case-sensitive
  1521. (equal "* \nTodo"
  1522. (org-test-with-temp-text "* <point>Todo"
  1523. (org-return)
  1524. (buffer-string))))
  1525. ;; At headline text, break headline text but preserve tags.
  1526. (should
  1527. (equal "* TODO [#B] foo :tag:\nbar"
  1528. (let (org-auto-align-tags)
  1529. (org-test-with-temp-text "* TODO [#B] foo<point>bar :tag:"
  1530. (org-return)
  1531. (buffer-string)))))
  1532. ;; At bol of headline insert newline.
  1533. (should
  1534. (equal "\n* h"
  1535. (org-test-with-temp-text "<point>* h"
  1536. (org-return)
  1537. (buffer-string))))
  1538. ;; Refuse to leave invalid headline in buffer.
  1539. (should
  1540. (equal "* h\n"
  1541. (org-test-with-temp-text "*<point> h"
  1542. (org-return)
  1543. (buffer-string))))
  1544. ;; Before first column or after last one in a table, split the
  1545. ;; table.
  1546. (should
  1547. (equal "| a |\n\n| b |"
  1548. (org-test-with-temp-text "| a |\n<point>| b |"
  1549. (org-return)
  1550. (buffer-string))))
  1551. (should
  1552. (equal "| a |\n\n| b |"
  1553. (org-test-with-temp-text "| a |<point>\n| b |"
  1554. (org-return)
  1555. (buffer-string))))
  1556. ;; Do not auto-fill on hitting <RET> inside a property drawer.
  1557. (should
  1558. (equal "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.\n\n:END:"
  1559. (org-test-with-temp-text "* Heading\n:PROPERTIES:\n:SOME_PROP: This is a very long property value that goes beyond the fill-column. But this is inside a property drawer, so the auto-filling should be disabled.<point>\n:END:"
  1560. (setq-local fill-column 10)
  1561. (auto-fill-mode 1)
  1562. (org-return)
  1563. (buffer-string)))))
  1564. (ert-deftest test-org/with-electric-indent ()
  1565. "Test RET and C-j specifications with `electric-indent-mode' on."
  1566. ;; Call commands interactively, since this is how `newline' knows it
  1567. ;; must run `post-self-insert-hook'.
  1568. ;;
  1569. ;; RET, like `newline', should indent.
  1570. (should
  1571. (equal " Para\n graph"
  1572. (org-test-with-temp-text " Para<point>graph"
  1573. (electric-indent-local-mode 1)
  1574. (call-interactively 'org-return)
  1575. (buffer-string))))
  1576. (should
  1577. (equal "- item1\n item2"
  1578. (org-test-with-temp-text "- item1<point>item2"
  1579. (electric-indent-local-mode 1)
  1580. (call-interactively 'org-return)
  1581. (buffer-string))))
  1582. ;; TODO: test more values of `org-adapt-indentation'.
  1583. (let ((org-adapt-indentation t))
  1584. (should
  1585. (equal "* heading\n body"
  1586. (org-test-with-temp-text "* heading<point>body"
  1587. (electric-indent-local-mode 1)
  1588. (call-interactively 'org-return)
  1589. (buffer-string)))))
  1590. ;; C-j, like `electric-newline-and-maybe-indent', should not indent.
  1591. (should
  1592. (equal " Para\ngraph"
  1593. (org-test-with-temp-text " Para<point>graph"
  1594. (electric-indent-local-mode 1)
  1595. (call-interactively 'org-return-and-maybe-indent)
  1596. (buffer-string))))
  1597. (should
  1598. (equal "- item1\nitem2"
  1599. (org-test-with-temp-text "- item1<point>item2"
  1600. (electric-indent-local-mode 1)
  1601. (call-interactively 'org-return-and-maybe-indent)
  1602. (buffer-string))))
  1603. ;; TODO: test more values of `org-adapt-indentation'.
  1604. (let ((org-adapt-indentation t))
  1605. (should
  1606. (equal "* heading\nbody"
  1607. (org-test-with-temp-text "* heading<point>body"
  1608. (electric-indent-local-mode 1)
  1609. (call-interactively 'org-return-and-maybe-indent)
  1610. (buffer-string))))))
  1611. (ert-deftest test-org/without-electric-indent ()
  1612. "Test RET and C-j specifications with `electric-indent-mode' off."
  1613. ;; Call commands interactively, since this is how `newline' knows it
  1614. ;; must run `post-self-insert-hook'.
  1615. ;;
  1616. ;; RET, like `newline', should not indent.
  1617. (should
  1618. (equal " Para\ngraph"
  1619. (org-test-with-temp-text " Para<point>graph"
  1620. (electric-indent-local-mode 0)
  1621. (call-interactively 'org-return)
  1622. (buffer-string))))
  1623. (should
  1624. (equal "- item1\nitem2"
  1625. (org-test-with-temp-text "- item1<point>item2"
  1626. (electric-indent-local-mode 0)
  1627. (call-interactively 'org-return)
  1628. (buffer-string))))
  1629. (should
  1630. (equal "* heading\nbody"
  1631. (org-test-with-temp-text "* heading<point>body"
  1632. (electric-indent-local-mode 0)
  1633. (call-interactively 'org-return)
  1634. (buffer-string))))
  1635. ;; C-j, like `electric-newline-and-maybe-indent', should indent.
  1636. (should
  1637. (equal " Para\n graph"
  1638. (org-test-with-temp-text " Para<point>graph"
  1639. (electric-indent-local-mode 0)
  1640. (call-interactively 'org-return-and-maybe-indent)
  1641. (buffer-string))))
  1642. (should
  1643. (equal "- item1\n item2"
  1644. (org-test-with-temp-text "- item1<point>item2"
  1645. (electric-indent-local-mode 0)
  1646. (call-interactively 'org-return-and-maybe-indent)
  1647. (buffer-string))))
  1648. ;; TODO: test more values of `org-adapt-indentation'.
  1649. (let ((org-adapt-indentation t))
  1650. (should
  1651. (equal "* heading\n body"
  1652. (org-test-with-temp-text "* heading<point>body"
  1653. (electric-indent-local-mode 0)
  1654. (call-interactively 'org-return-and-maybe-indent)
  1655. (buffer-string))))))
  1656. (ert-deftest test-org/meta-return ()
  1657. "Test M-RET (`org-meta-return') specifications."
  1658. ;; In a table field insert a row above.
  1659. (should
  1660. (org-test-with-temp-text "| a |"
  1661. (forward-char)
  1662. (org-meta-return)
  1663. (forward-line -1)
  1664. (looking-at "| |$")))
  1665. ;; In a paragraph change current line into a header.
  1666. (should
  1667. (org-test-with-temp-text "a"
  1668. (org-meta-return)
  1669. (beginning-of-line)
  1670. (looking-at "\* a$")))
  1671. ;; In an item insert an item, in this case above.
  1672. (should
  1673. (org-test-with-temp-text "- a"
  1674. (org-meta-return)
  1675. (beginning-of-line)
  1676. (looking-at "- $")))
  1677. ;; In a drawer and item insert an item, in this case above.
  1678. (should
  1679. (org-test-with-temp-text ":MYDRAWER:\n- a\n:END:"
  1680. (forward-line)
  1681. (org-fold-reveal)
  1682. (org-meta-return)
  1683. (beginning-of-line)
  1684. (looking-at "- $"))))
  1685. (ert-deftest test-org/insert-heading ()
  1686. "Test `org-insert-heading' specifications."
  1687. ;; In an empty buffer, insert a new headline.
  1688. (should
  1689. (equal "* "
  1690. (org-test-with-temp-text ""
  1691. (org-insert-heading)
  1692. (buffer-string))))
  1693. ;; At the beginning of a line, turn it into a headline.
  1694. (should
  1695. (equal "* P"
  1696. (org-test-with-temp-text "<point>P"
  1697. (org-insert-heading)
  1698. (buffer-string))))
  1699. ;; In the middle of a line, split the line if allowed, otherwise,
  1700. ;; insert the headline at its end.
  1701. (should
  1702. (equal "Para\n* graph"
  1703. (org-test-with-temp-text "Para<point>graph"
  1704. (let ((org-M-RET-may-split-line '((default . t))))
  1705. (org-insert-heading))
  1706. (buffer-string))))
  1707. (should
  1708. (equal "Paragraph\n* "
  1709. (org-test-with-temp-text "Para<point>graph"
  1710. (let ((org-M-RET-may-split-line '((default . nil))))
  1711. (org-insert-heading))
  1712. (buffer-string))))
  1713. ;; At the beginning of a headline, create one above.
  1714. (should
  1715. (equal "* \n* H"
  1716. (org-test-with-temp-text "* H"
  1717. (org-insert-heading)
  1718. (buffer-string))))
  1719. ;; In the middle of a headline, split it if allowed.
  1720. (should
  1721. (equal "* H\n* 1"
  1722. (org-test-with-temp-text "* H<point>1"
  1723. (let ((org-M-RET-may-split-line '((headline . t))))
  1724. (org-insert-heading))
  1725. (buffer-string))))
  1726. (should
  1727. (equal "* H1\n* "
  1728. (org-test-with-temp-text "* H<point>1"
  1729. (let ((org-M-RET-may-split-line '((headline . nil))))
  1730. (org-insert-heading))
  1731. (buffer-string))))
  1732. ;; However, splitting cannot happen on TODO keywords, priorities or
  1733. ;; tags.
  1734. (should
  1735. (equal "* TODO H1\n* "
  1736. (org-test-with-temp-text "* TO<point>DO H1"
  1737. (let ((org-M-RET-may-split-line '((headline . t))))
  1738. (org-insert-heading))
  1739. (buffer-string))))
  1740. (should
  1741. (equal "* [#A] H1\n* "
  1742. (org-test-with-temp-text "* [#<point>A] H1"
  1743. (let ((org-M-RET-may-split-line '((headline . t))))
  1744. (org-insert-heading))
  1745. (buffer-string))))
  1746. (should
  1747. (equal "* H1 :tag:\n* "
  1748. (org-test-with-temp-text "* H1 :ta<point>g:"
  1749. (let ((org-M-RET-may-split-line '((headline . t))))
  1750. (org-insert-heading))
  1751. (buffer-string))))
  1752. ;; New headline level depends on the level of the headline above.
  1753. (should
  1754. (equal "** H\n** P"
  1755. (org-test-with-temp-text "** H\n<point>P"
  1756. (org-insert-heading)
  1757. (buffer-string))))
  1758. (should
  1759. (equal "** H\nPara\n** graph"
  1760. (org-test-with-temp-text "** H\nPara<point>graph"
  1761. (let ((org-M-RET-may-split-line '((default . t))))
  1762. (org-insert-heading))
  1763. (buffer-string))))
  1764. (should
  1765. (equal "** \n** H"
  1766. (org-test-with-temp-text "** H"
  1767. (org-insert-heading)
  1768. (buffer-string))))
  1769. ;; When called with one universal argument, insert a new headline at
  1770. ;; the end of the current subtree, independently on the position of
  1771. ;; point.
  1772. (should
  1773. (equal
  1774. "* "
  1775. (org-test-with-temp-text ""
  1776. (let ((org-insert-heading-respect-content nil))
  1777. (org-insert-heading '(4)))
  1778. (buffer-string))))
  1779. (should
  1780. (equal
  1781. "entry
  1782. * "
  1783. (org-test-with-temp-text "entry"
  1784. (let ((org-insert-heading-respect-content nil))
  1785. (org-insert-heading '(4)))
  1786. (buffer-string))))
  1787. (should
  1788. (equal
  1789. "* H1\n** H2\n* "
  1790. (org-test-with-temp-text "* H1\n** H2"
  1791. (let ((org-insert-heading-respect-content nil))
  1792. (org-insert-heading '(4)))
  1793. (buffer-string))))
  1794. (should
  1795. (equal
  1796. "* H1\n** H2\n* "
  1797. (org-test-with-temp-text "* H<point>1\n** H2"
  1798. (let ((org-insert-heading-respect-content nil))
  1799. (org-insert-heading '(4)))
  1800. (buffer-string))))
  1801. ;; When called with two universal arguments, insert a new headline
  1802. ;; at the end of the grandparent subtree.
  1803. (should
  1804. (equal "* H1\n** H3\n- item\n** H2\n** "
  1805. (org-test-with-temp-text "* H1\n** H3\n- item<point>\n** H2"
  1806. (let ((org-insert-heading-respect-content nil))
  1807. (org-insert-heading '(16)))
  1808. (buffer-string))))
  1809. ;; When optional TOP-LEVEL argument is non-nil, always insert
  1810. ;; a level 1 heading.
  1811. (should
  1812. (equal "* H1\n** H2\n* "
  1813. (org-test-with-temp-text "* H1\n** H2<point>"
  1814. (org-insert-heading nil nil t)
  1815. (buffer-string))))
  1816. (should
  1817. (equal "* H1\n- item\n* "
  1818. (org-test-with-temp-text "* H1\n- item<point>"
  1819. (org-insert-heading nil nil t)
  1820. (buffer-string))))
  1821. ;; Obey `org-blank-before-new-entry'.
  1822. (should
  1823. (equal "* H1\n\n* "
  1824. (org-test-with-temp-text "* H1<point>"
  1825. (let ((org-blank-before-new-entry '((heading . t))))
  1826. (org-insert-heading))
  1827. (buffer-string))))
  1828. (should
  1829. (equal "* H1\n* "
  1830. (org-test-with-temp-text "* H1<point>"
  1831. (let ((org-blank-before-new-entry '((heading . nil))))
  1832. (org-insert-heading))
  1833. (buffer-string))))
  1834. (should
  1835. (equal "* H1\n* H2\n* "
  1836. (org-test-with-temp-text "* H1\n* H2<point>"
  1837. (let ((org-blank-before-new-entry '((heading . auto))))
  1838. (org-insert-heading))
  1839. (buffer-string))))
  1840. (should
  1841. (equal "* H1\n\n* H2\n\n* "
  1842. (org-test-with-temp-text "* H1\n\n* H2<point>"
  1843. (let ((org-blank-before-new-entry '((heading . auto))))
  1844. (org-insert-heading))
  1845. (buffer-string))))
  1846. ;; Corner case: correctly insert a headline after an empty one.
  1847. (should
  1848. (equal "* \n* "
  1849. (org-test-with-temp-text "* <point>"
  1850. (org-insert-heading)
  1851. (buffer-string))))
  1852. (should
  1853. (org-test-with-temp-text "* <point>\n"
  1854. (org-insert-heading)
  1855. (looking-at-p "\n\\'")))
  1856. ;; Do not insert spurious headlines when inserting a new headline.
  1857. (should
  1858. (equal "* H1\n* H2\n* \n"
  1859. (org-test-with-temp-text "* H1\n* H2<point>\n"
  1860. (org-insert-heading)
  1861. (buffer-string))))
  1862. ;; Preserve visibility at beginning of line. In particular, when
  1863. ;; removing spurious blank lines, do not visually merge heading with
  1864. ;; the line visible above.
  1865. (should-not
  1866. (org-test-with-temp-text "* H1<point>\nContents\n\n* H2\n"
  1867. (org-overview)
  1868. (let ((org-blank-before-new-entry '((heading . nil))))
  1869. (org-insert-heading '(4)))
  1870. (invisible-p (line-end-position 0))))
  1871. ;; Properly handle empty lines when forcing a headline below current
  1872. ;; one.
  1873. (should
  1874. (equal "* H1\n\n* H\n\n* "
  1875. (org-test-with-temp-text "* H1\n\n* H<point>"
  1876. (let ((org-blank-before-new-entry '((heading . t))))
  1877. (org-insert-heading '(4))
  1878. (buffer-string))))))
  1879. (ert-deftest test-org/insert-todo-heading-respect-content ()
  1880. "Test `org-insert-todo-heading-respect-content' specifications."
  1881. ;; Create a TODO heading.
  1882. (should
  1883. (org-test-with-temp-text "* H1\n Body"
  1884. (org-insert-todo-heading-respect-content)
  1885. (nth 2 (org-heading-components))))
  1886. ;; Add headline at the end of the first subtree
  1887. (should
  1888. (equal
  1889. "* TODO "
  1890. (org-test-with-temp-text "* H1\nH1Body<point>\n** H2\nH2Body"
  1891. (org-insert-todo-heading-respect-content)
  1892. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1893. ;; In a list, do not create a new item.
  1894. (should
  1895. (equal
  1896. "* TODO "
  1897. (org-test-with-temp-text "* H\n- an item\n- another one"
  1898. (search-forward "an ")
  1899. (org-insert-todo-heading-respect-content)
  1900. (buffer-substring-no-properties (line-beginning-position) (point-max)))))
  1901. ;; Use the same TODO keyword as current heading.
  1902. (should
  1903. (equal
  1904. "* TODO \n"
  1905. (org-test-with-temp-text "* TODO\n** WAITING\n"
  1906. (org-insert-todo-heading-respect-content)
  1907. (buffer-substring-no-properties (line-beginning-position) (point-max))))))
  1908. (ert-deftest test-org/clone-with-time-shift ()
  1909. "Test `org-clone-subtree-with-time-shift'."
  1910. ;; Raise an error before first heading.
  1911. (should-error
  1912. (org-test-with-temp-text ""
  1913. (org-clone-subtree-with-time-shift 1)))
  1914. ;; Raise an error on invalid number of clones.
  1915. (should-error
  1916. (org-test-with-temp-text "* Clone me"
  1917. (org-clone-subtree-with-time-shift -1)))
  1918. ;; Clone non-repeating once.
  1919. (should
  1920. (equal "\
  1921. * H1\n<2015-06-21>
  1922. * H1\n<2015-06-23>
  1923. "
  1924. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1925. (org-clone-subtree-with-time-shift 1 "+2d")
  1926. (replace-regexp-in-string
  1927. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1928. nil nil 1))))
  1929. ;; Clone repeating once.
  1930. (should
  1931. (equal "\
  1932. * H1\n<2015-06-21>
  1933. * H1\n<2015-06-23>
  1934. * H1\n<2015-06-25 +1w>
  1935. "
  1936. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1937. (org-clone-subtree-with-time-shift 1 "+2d")
  1938. (replace-regexp-in-string
  1939. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1940. nil nil 1))))
  1941. ;; Clone repeating once in backward.
  1942. (should
  1943. (equal "\
  1944. * H1\n<2015-06-21>
  1945. * H1\n<2015-06-19>
  1946. * H1\n<2015-06-17 +1w>
  1947. "
  1948. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1949. (org-clone-subtree-with-time-shift 1 "-2d")
  1950. (replace-regexp-in-string
  1951. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1952. nil nil 1))))
  1953. ;; Clone non-repeating zero times.
  1954. (should
  1955. (equal "\
  1956. * H1\n<2015-06-21>
  1957. "
  1958. (org-test-with-temp-text "* H1\n<2015-06-21 Sun>"
  1959. (org-clone-subtree-with-time-shift 0 "+2d")
  1960. (replace-regexp-in-string
  1961. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1962. nil nil 1))))
  1963. ;; Clone repeating "zero" times.
  1964. (should
  1965. (equal "\
  1966. * H1\n<2015-06-21>
  1967. * H1\n<2015-06-23 +1w>
  1968. "
  1969. (org-test-with-temp-text "* H1\n<2015-06-21 Sun +1w>"
  1970. (org-clone-subtree-with-time-shift 0 "+2d")
  1971. (replace-regexp-in-string
  1972. "\\( [.A-Za-z]+\\)\\( \\+[0-9][hdmwy]\\)?>" "" (buffer-string)
  1973. nil nil 1))))
  1974. ;; Clone with blank SHIFT argument.
  1975. (should
  1976. (string-prefix-p
  1977. "* H <2012-03-29"
  1978. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1979. (org-clone-subtree-with-time-shift 1 "")
  1980. (buffer-substring-no-properties (line-beginning-position 2)
  1981. (line-end-position 2)))))
  1982. ;; Find time stamps before point. If SHIFT is not specified, ask
  1983. ;; for a time shift.
  1984. (should
  1985. (string-prefix-p
  1986. "* H <2012-03-30"
  1987. (org-test-with-temp-text "* H <2012-03-29 Thu><point>"
  1988. (org-clone-subtree-with-time-shift 1 "+1d")
  1989. (buffer-substring-no-properties (line-beginning-position 2)
  1990. (line-end-position 2)))))
  1991. (should
  1992. (string-prefix-p
  1993. "* H <2014-03-05"
  1994. (org-test-with-temp-text "* H <2014-03-04 Tue><point>"
  1995. (cl-letf (((symbol-function 'read-from-minibuffer)
  1996. (lambda (&rest _args) "+1d")))
  1997. (org-clone-subtree-with-time-shift 1))
  1998. (buffer-substring-no-properties (line-beginning-position 2)
  1999. (line-end-position 2)))))
  2000. ;; Hour shift.
  2001. (should
  2002. (equal "\
  2003. * H1\n<2015-06-21 20:00>
  2004. * H1\n<2015-06-21 23:00>
  2005. * H1\n<2015-06-22 02:00>
  2006. "
  2007. (org-test-with-temp-text "* H1\n<2015-06-21 Sun 20:00>"
  2008. (org-clone-subtree-with-time-shift 2 "+3h")
  2009. (replace-regexp-in-string
  2010. "\\( [.A-Za-z]+\\)\\( [0-9][0-9]:[0-9][0-9]\\)?>" ""
  2011. (buffer-substring-no-properties (point-min) (point-max))
  2012. nil nil 1))))
  2013. (should
  2014. (equal "\
  2015. * H1\n<2015-06-21 20:00>
  2016. * H1\n<2015-06-21 18:00>
  2017. "
  2018. (org-test-with-temp-text "* H1\n<2015-06-21 Sun 20:00>"
  2019. (org-clone-subtree-with-time-shift 1 "-2h")
  2020. (replace-regexp-in-string
  2021. "\\( [.A-Za-z]+\\)\\( [0-9][0-9]:[0-9][0-9]\\)?>" ""
  2022. (buffer-substring-no-properties (point-min) (point-max))
  2023. nil nil 1)))))
  2024. ;;; Fixed-Width Areas
  2025. (ert-deftest test-org/toggle-fixed-width ()
  2026. "Test `org-toggle-fixed-width' specifications."
  2027. ;; No region: Toggle on fixed-width marker in paragraphs.
  2028. (should
  2029. (equal ": A"
  2030. (org-test-with-temp-text "A"
  2031. (org-toggle-fixed-width)
  2032. (buffer-string))))
  2033. ;; No region: Toggle off fixed-width markers in fixed-width areas.
  2034. (should
  2035. (equal "A"
  2036. (org-test-with-temp-text ": A"
  2037. (org-toggle-fixed-width)
  2038. (buffer-string))))
  2039. ;; No region: Toggle on marker in blank lines after elements or just
  2040. ;; after a headline.
  2041. (should
  2042. (equal "* H\n: "
  2043. (org-test-with-temp-text "* H\n"
  2044. (forward-line)
  2045. (org-toggle-fixed-width)
  2046. (buffer-string))))
  2047. (should
  2048. (equal "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n: "
  2049. (org-test-with-temp-text "#+BEGIN_EXAMPLE\nContents\n#+END_EXAMPLE\n"
  2050. (goto-char (point-max))
  2051. (org-toggle-fixed-width)
  2052. (buffer-string))))
  2053. ;; No region: Toggle on marker in front of one line elements (e.g.,
  2054. ;; headlines, clocks)
  2055. (should
  2056. (equal ": * Headline"
  2057. (org-test-with-temp-text "* Headline"
  2058. (org-toggle-fixed-width)
  2059. (buffer-string))))
  2060. (should
  2061. (equal ": #+KEYWORD: value"
  2062. (org-test-with-temp-text "#+KEYWORD: value"
  2063. (org-toggle-fixed-width)
  2064. (buffer-string))))
  2065. ;; No region: error in other situations.
  2066. (should-error
  2067. (org-test-with-temp-text "#+BEGIN_EXAMPLE\n: A\n#+END_EXAMPLE"
  2068. (forward-line)
  2069. (org-toggle-fixed-width)
  2070. (buffer-string)))
  2071. ;; No region: Indentation is preserved.
  2072. (should
  2073. (equal "- A\n : B"
  2074. (org-test-with-temp-text "- A\n B"
  2075. (forward-line)
  2076. (org-toggle-fixed-width)
  2077. (buffer-string))))
  2078. ;; Region: If it contains only fixed-width elements and blank lines,
  2079. ;; toggle off fixed-width markup.
  2080. (should
  2081. (equal "A\n\nB"
  2082. (org-test-with-temp-text ": A\n\n: B"
  2083. (transient-mark-mode 1)
  2084. (push-mark (point) t t)
  2085. (goto-char (point-max))
  2086. (org-toggle-fixed-width)
  2087. (buffer-string))))
  2088. ;; Region: If it contains anything else, toggle on fixed-width but
  2089. ;; not on fixed-width areas.
  2090. (should
  2091. (equal ": A\n: \n: B\n: \n: C"
  2092. (org-test-with-temp-text "A\n\n: B\n\nC"
  2093. (transient-mark-mode 1)
  2094. (push-mark (point) t t)
  2095. (goto-char (point-max))
  2096. (org-toggle-fixed-width)
  2097. (buffer-string))))
  2098. ;; Region: Ignore blank lines at its end, unless it contains only
  2099. ;; such lines.
  2100. (should
  2101. (equal ": A\n\n"
  2102. (org-test-with-temp-text "A\n\n"
  2103. (transient-mark-mode 1)
  2104. (push-mark (point) t t)
  2105. (goto-char (point-max))
  2106. (org-toggle-fixed-width)
  2107. (buffer-string))))
  2108. (should
  2109. (equal ": \n: \n"
  2110. (org-test-with-temp-text "\n\n"
  2111. (transient-mark-mode 1)
  2112. (push-mark (point) t t)
  2113. (goto-char (point-max))
  2114. (org-toggle-fixed-width)
  2115. (buffer-string)))))
  2116. (ert-deftest test-org/kill-line ()
  2117. "Test `org-kill-line' specifications."
  2118. ;; At the beginning of a line, kill whole line.
  2119. (should
  2120. (equal ""
  2121. (org-test-with-temp-text "abc"
  2122. (org-kill-line)
  2123. (buffer-string))))
  2124. ;; In the middle of a line, kill line until its end.
  2125. (should
  2126. (equal "a"
  2127. (org-test-with-temp-text "a<point>bc"
  2128. (org-kill-line)
  2129. (buffer-string))))
  2130. ;; Do not kill newline character.
  2131. (should
  2132. (equal "\n123"
  2133. (org-test-with-temp-text "abc\n123"
  2134. (org-kill-line)
  2135. (buffer-string))))
  2136. (should
  2137. (equal "a\n123"
  2138. (org-test-with-temp-text "a<point>bc\n123"
  2139. (org-kill-line)
  2140. (buffer-string))))
  2141. ;; When `org-special-ctrl-k' is non-nil and point is at a headline,
  2142. ;; kill until tags.
  2143. (should
  2144. (equal "* A :tag:"
  2145. (org-test-with-temp-text "* A<point>B :tag:"
  2146. (let ((org-special-ctrl-k t)
  2147. (org-tags-column 0))
  2148. (org-kill-line))
  2149. (buffer-string))))
  2150. ;; If point is on tags, only kill part left until the end of line.
  2151. (should
  2152. (equal "* A :tag:"
  2153. (org-test-with-temp-text "* A :tag:<point>tag2:"
  2154. (let ((org-special-ctrl-k t)
  2155. (org-tags-column 0))
  2156. (org-kill-line))
  2157. (buffer-string))))
  2158. ;; However, if point is at the beginning of the line, kill whole
  2159. ;; headline.
  2160. (should
  2161. (equal ""
  2162. (org-test-with-temp-text "* AB :tag:"
  2163. (let ((org-special-ctrl-k t)
  2164. (org-tags-column 0))
  2165. (org-kill-line))
  2166. (buffer-string))))
  2167. ;; When `org-ctrl-k-protect-subtree' is non-nil, and point is in
  2168. ;; invisible text, ask before removing it. When set to `error',
  2169. ;; throw an error.
  2170. (should-error
  2171. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  2172. (org-overview)
  2173. (let ((org-special-ctrl-k nil)
  2174. (org-ctrl-k-protect-subtree t))
  2175. (cl-letf (((symbol-function 'y-or-n-p) 'ignore))
  2176. (org-kill-line)))))
  2177. (should-error
  2178. (org-test-with-temp-text "* H\n** <point>H2\nContents\n* H3"
  2179. (org-overview)
  2180. (let ((org-special-ctrl-k nil)
  2181. (org-ctrl-k-protect-subtree 'error))
  2182. (org-kill-line)))))
  2183. ;;; Headline
  2184. (ert-deftest test-org/get-heading ()
  2185. "Test `org-get-heading' specifications."
  2186. ;; Return current heading, even if point is not on it.
  2187. (should
  2188. (equal "H"
  2189. (org-test-with-temp-text "* H"
  2190. (org-get-heading))))
  2191. (should
  2192. (equal "H"
  2193. (org-test-with-temp-text "* H\nText<point>"
  2194. (org-get-heading))))
  2195. ;; Without any optional argument, return TODO keyword, priority
  2196. ;; cookie, COMMENT keyword and tags.
  2197. (should
  2198. (equal "TODO H"
  2199. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  2200. (org-get-heading))))
  2201. (should
  2202. (equal "[#A] H"
  2203. (org-test-with-temp-text "* [#A] H"
  2204. (org-get-heading))))
  2205. (should
  2206. (equal "COMMENT H"
  2207. (org-test-with-temp-text "* COMMENT H"
  2208. (org-get-heading))))
  2209. (should
  2210. (equal "H :tag:"
  2211. (org-test-with-temp-text "* H :tag:"
  2212. (org-get-heading))))
  2213. ;; With NO-TAGS argument, ignore tags.
  2214. (should
  2215. (equal "TODO H"
  2216. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  2217. (org-get-heading t))))
  2218. (should
  2219. (equal "H"
  2220. (org-test-with-temp-text "* H :tag:"
  2221. (org-get-heading t))))
  2222. ;; With NO-TODO, ignore TODO keyword.
  2223. (should
  2224. (equal "H"
  2225. (org-test-with-temp-text "#+TODO: TODO | DONE\n* TODO H<point>"
  2226. (org-get-heading nil t))))
  2227. (should
  2228. (equal "H :tag:"
  2229. (org-test-with-temp-text "* H :tag:"
  2230. (org-get-heading nil t))))
  2231. ;; TODO keywords are case-sensitive.
  2232. (should
  2233. (equal "Todo H"
  2234. (org-test-with-temp-text "#+TODO: TODO | DONE\n* Todo H<point>"
  2235. (org-get-heading nil t))))
  2236. ;; With NO-PRIORITY, ignore priority.
  2237. (should
  2238. (equal "H"
  2239. (org-test-with-temp-text "* [#A] H"
  2240. (org-get-heading nil nil t))))
  2241. (should
  2242. (equal "H"
  2243. (org-test-with-temp-text "* H"
  2244. (org-get-heading nil nil t))))
  2245. (should
  2246. (equal "TODO H"
  2247. (org-test-with-temp-text "* TODO [#A] H"
  2248. (org-get-heading nil nil t))))
  2249. ;; With NO-COMMENT, ignore COMMENT keyword.
  2250. (should
  2251. (equal "H"
  2252. (org-test-with-temp-text "* COMMENT H"
  2253. (org-get-heading nil nil nil t))))
  2254. (should
  2255. (equal "H"
  2256. (org-test-with-temp-text "* H"
  2257. (org-get-heading nil nil nil t))))
  2258. (should
  2259. (equal "TODO [#A] H"
  2260. (org-test-with-temp-text "* TODO [#A] COMMENT H"
  2261. (org-get-heading nil nil nil t))))
  2262. ;; On an empty headline, return value is consistent.
  2263. (should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
  2264. (should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
  2265. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil t))))
  2266. (should (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil t))))
  2267. (should
  2268. (equal "" (org-test-with-temp-text "* " (org-get-heading nil nil nil t)))))
  2269. (ert-deftest test-org/in-commented-heading-p ()
  2270. "Test `org-in-commented-heading-p' specifications."
  2271. ;; Commented headline.
  2272. (should
  2273. (org-test-with-temp-text "* COMMENT Headline\nBody"
  2274. (goto-char (point-max))
  2275. (org-in-commented-heading-p)))
  2276. ;; Commented ancestor.
  2277. (should
  2278. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  2279. (goto-char (point-max))
  2280. (org-in-commented-heading-p)))
  2281. ;; Comment keyword is case-sensitive.
  2282. (should-not
  2283. (org-test-with-temp-text "* Comment Headline\nBody"
  2284. (goto-char (point-max))
  2285. (org-in-commented-heading-p)))
  2286. ;; Keyword is standalone.
  2287. (should-not
  2288. (org-test-with-temp-text "* COMMENTHeadline\nBody"
  2289. (goto-char (point-max))
  2290. (org-in-commented-heading-p)))
  2291. ;; Optional argument.
  2292. (should-not
  2293. (org-test-with-temp-text "* COMMENT Headline\n** Level 2\nBody"
  2294. (goto-char (point-max))
  2295. (org-in-commented-heading-p t))))
  2296. (ert-deftest test-org/in-archived-heading-p ()
  2297. "Test `org-in-archived-heading-p' specifications."
  2298. ;; Archived headline.
  2299. (should
  2300. (org-test-with-temp-text "* Headline :ARCHIVE:\nBody"
  2301. (goto-char (point-max))
  2302. (org-in-archived-heading-p)))
  2303. ;; Archived ancestor.
  2304. (should
  2305. (org-test-with-temp-text "* Headline :ARCHIVE:\n** Level 2\nBody"
  2306. (goto-char (point-max))
  2307. (org-in-archived-heading-p)))
  2308. ;; Optional argument.
  2309. (should-not
  2310. (org-test-with-temp-text "* Headline :ARCHIVE:\n** Level 2\nBody"
  2311. (goto-char (point-max))
  2312. (org-in-archived-heading-p t)))
  2313. ;; Archive tag containing ARCHIVE as substring
  2314. (should-not
  2315. (org-test-with-temp-text "* Headline :NOARCHIVE:\n** Level 2\nBody"
  2316. (goto-char (point-max))
  2317. (org-in-archived-heading-p))))
  2318. (ert-deftest test-org/entry-blocked-p ()
  2319. ;; Check other dependencies.
  2320. (should
  2321. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  2322. (let ((org-enforce-todo-dependencies t)
  2323. (org-blocker-hook
  2324. '(org-block-todo-from-children-or-siblings-or-parent)))
  2325. (org-entry-blocked-p))))
  2326. (should-not
  2327. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  2328. (let ((org-enforce-todo-dependencies t)
  2329. (org-blocker-hook
  2330. '(org-block-todo-from-children-or-siblings-or-parent)))
  2331. (org-entry-blocked-p))))
  2332. ;; Entry without a TODO keyword or with a DONE keyword cannot be
  2333. ;; blocked.
  2334. (should-not
  2335. (org-test-with-temp-text "* Blocked\n** TODO one"
  2336. (let ((org-enforce-todo-dependencies t)
  2337. (org-blocker-hook
  2338. '(org-block-todo-from-children-or-siblings-or-parent)))
  2339. (org-entry-blocked-p))))
  2340. (should-not
  2341. (org-test-with-temp-text "* DONE Blocked\n** TODO one"
  2342. (let ((org-enforce-todo-dependencies t)
  2343. (org-blocker-hook
  2344. '(org-block-todo-from-children-or-siblings-or-parent)))
  2345. (org-entry-blocked-p))))
  2346. ;; Follow :ORDERED: specifications.
  2347. (should
  2348. (org-test-with-temp-text
  2349. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** TODO one\n** <point>TODO two"
  2350. (let ((org-enforce-todo-dependencies t)
  2351. (org-blocker-hook
  2352. '(org-block-todo-from-children-or-siblings-or-parent)))
  2353. (org-entry-blocked-p))))
  2354. (should-not
  2355. (org-test-with-temp-text
  2356. "* H\n:PROPERTIES:\n:ORDERED: t\n:END:\n** <point>TODO one\n** DONE two"
  2357. (let ((org-enforce-todo-dependencies t)
  2358. (org-blocker-hook
  2359. '(org-block-todo-from-children-or-siblings-or-parent)))
  2360. (org-entry-blocked-p)))))
  2361. (ert-deftest test-org/get-outline-path ()
  2362. "Test `org-get-outline-path' specifications."
  2363. ;; Top-level headlines have no outline path.
  2364. (should-not
  2365. (org-test-with-temp-text "* H"
  2366. (org-get-outline-path)))
  2367. ;; Otherwise, outline path is the path leading to the headline.
  2368. (should
  2369. (equal '("H")
  2370. (org-test-with-temp-text "* H\n** S<point>"
  2371. (org-get-outline-path))))
  2372. ;; Find path even when point is not on a headline.
  2373. (should
  2374. (equal '("H")
  2375. (org-test-with-temp-text "* H\n** S\nText<point>"
  2376. (org-get-outline-path))))
  2377. ;; TODO keywords, tags and statistics cookies are ignored.
  2378. (should
  2379. (equal '("H")
  2380. (org-test-with-temp-text "* TODO H [0/1] :tag:\n** S<point>"
  2381. (org-get-outline-path))))
  2382. ;; Links are replaced with their description or their path.
  2383. (should
  2384. (equal '("Org")
  2385. (org-test-with-temp-text
  2386. "* [[https://orgmode.org][Org]]\n** S<point>"
  2387. (org-get-outline-path))))
  2388. (should
  2389. (equal '("https://orgmode.org")
  2390. (org-test-with-temp-text
  2391. "* [[https://orgmode.org]]\n** S<point>"
  2392. (org-get-outline-path))))
  2393. ;; When WITH-SELF is non-nil, include current heading.
  2394. (should
  2395. (equal '("H")
  2396. (org-test-with-temp-text "* H"
  2397. (org-get-outline-path t))))
  2398. (should
  2399. (equal '("H" "S")
  2400. (org-test-with-temp-text "* H\n** S\nText<point>"
  2401. (org-get-outline-path t))))
  2402. ;; Using cache is transparent to the user.
  2403. (should
  2404. (equal '("H")
  2405. (org-test-with-temp-text "* H\n** S<point>"
  2406. (setq org-outline-path-cache nil)
  2407. (org-get-outline-path nil t))))
  2408. ;; Do not corrupt cache when finding outline path in distant part of
  2409. ;; the buffer.
  2410. (should
  2411. (equal '("H2")
  2412. (org-test-with-temp-text "* H\n** S<point>\n* H2\n** S2"
  2413. (setq org-outline-path-cache nil)
  2414. (org-get-outline-path nil t)
  2415. (search-forward "S2")
  2416. (org-get-outline-path nil t))))
  2417. ;; Do not choke on empty headlines.
  2418. (should
  2419. (org-test-with-temp-text "* H\n** <point>"
  2420. (org-get-outline-path)))
  2421. (should
  2422. (org-test-with-temp-text "* \n** H<point>"
  2423. (org-get-outline-path))))
  2424. (ert-deftest test-org/format-outline-path ()
  2425. "Test `org-format-outline-path' specifications."
  2426. (should
  2427. (string= (org-format-outline-path (list "one" "two" "three"))
  2428. "one/two/three"))
  2429. ;; Empty path.
  2430. (should
  2431. (string= (org-format-outline-path '())
  2432. ""))
  2433. (should
  2434. (string= (org-format-outline-path '(nil))
  2435. ""))
  2436. ;; Empty path and prefix.
  2437. (should
  2438. (string= (org-format-outline-path '() nil ">>")
  2439. ">>"))
  2440. ;; Trailing whitespace in headings.
  2441. (should
  2442. (string= (org-format-outline-path (list "one\t" "tw o " "three "))
  2443. "one/tw o/three"))
  2444. ;; Non-default prefix and separators.
  2445. (should
  2446. (string= (org-format-outline-path (list "one" "two" "three") nil ">>" "|")
  2447. ">>|one|two|three"))
  2448. ;; Truncate.
  2449. (should
  2450. (string= (org-format-outline-path (list "one" "two" "three" "four") 10)
  2451. "one/two/.."))
  2452. ;; Give a very narrow width.
  2453. (should
  2454. (string= (org-format-outline-path (list "one" "two" "three" "four") 2)
  2455. "on"))
  2456. ;; Give a prefix that extends beyond the width.
  2457. (should
  2458. (string= (org-format-outline-path (list "one" "two" "three" "four") 10
  2459. ">>>>>>>>>>")
  2460. ">>>>>>>>..")))
  2461. (ert-deftest test-org/org-find-olp ()
  2462. "Test `org-find-olp' specifications."
  2463. (org-test-with-temp-text
  2464. "
  2465. * Headline
  2466. ** COMMENT headline2
  2467. ** TODO headline3
  2468. *** [#A] headline4 :tags:
  2469. ** [#A]headline5
  2470. ** [0%] headline6
  2471. ** headline7 [100%]
  2472. ** headline8 [1/5] :some:more:tags:
  2473. * Test
  2474. "
  2475. (should (org-find-olp '("Headline") t))
  2476. (should-error (org-find-olp '("Headline" "Test") t))
  2477. (should-error (org-find-olp '("Headlinealksjd") t))
  2478. (should (org-find-olp '("Headline" "headline2") t))
  2479. (should (org-find-olp '("Headline" "headline3") t))
  2480. (should (org-find-olp '("Headline" "headline3" "headline4") t))
  2481. (should-error (org-find-olp '("Headline" "headline5") t))
  2482. (should (org-find-olp '("Headline" "headline6") t))
  2483. (should (org-find-olp '("Headline" "headline7") t))
  2484. (should (org-find-olp '("Headline" "headline8") t))))
  2485. (ert-deftest test-org/map-entries ()
  2486. "Test `org-map-entries' specifications."
  2487. ;; Full match.
  2488. (should
  2489. (equal '(1 11)
  2490. (org-test-with-temp-text "* Level 1\n** Level 2"
  2491. (org-map-entries #'point))))
  2492. ;; Level match.
  2493. (should
  2494. (equal '(1)
  2495. (org-test-with-temp-text "* Level 1\n** Level 2"
  2496. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL=1")))))
  2497. (should
  2498. (equal '(11)
  2499. (org-test-with-temp-text "* Level 1\n** Level 2"
  2500. (let (org-odd-levels-only) (org-map-entries #'point "LEVEL>1")))))
  2501. ;; Tag match.
  2502. (should
  2503. (equal '(11)
  2504. (org-test-with-temp-text "* H1 :no:\n* H2 :yes:"
  2505. (org-map-entries #'point "yes"))))
  2506. (should
  2507. (equal '(14)
  2508. (org-test-with-temp-text "* H1 :yes:a:\n* H2 :yes:b:"
  2509. (org-map-entries #'point "+yes-a"))))
  2510. (should
  2511. (equal '(11 23)
  2512. (org-test-with-temp-text "* H1 :no:\n* H2 :yes1:\n* H3 :yes2:"
  2513. (org-map-entries #'point "{yes?}"))))
  2514. ;; Priority match.
  2515. (should
  2516. (equal '(1)
  2517. (org-test-with-temp-text "* [#A] H1\n* [#B] H2"
  2518. (org-map-entries #'point "PRIORITY=\"A\""))))
  2519. ;; Date match.
  2520. (should
  2521. (equal '(36)
  2522. (org-test-with-temp-text "
  2523. * H1
  2524. SCHEDULED: <2012-03-29 thu.>
  2525. * H2
  2526. SCHEDULED: <2014-03-04 tue.>"
  2527. (org-map-entries #'point "SCHEDULED=\"<2014-03-04 tue.>\""))))
  2528. (should
  2529. (equal '(2)
  2530. (org-test-with-temp-text "
  2531. * H1
  2532. SCHEDULED: <2012-03-29 thu.>
  2533. * H2
  2534. SCHEDULED: <2014-03-04 tue.>"
  2535. (org-map-entries #'point "SCHEDULED<\"<2013-01-01>\""))))
  2536. ;; Regular property match.
  2537. (should
  2538. (equal '(2)
  2539. (org-test-with-temp-text "
  2540. * H1
  2541. :PROPERTIES:
  2542. :TEST: 1
  2543. :END:
  2544. * H2
  2545. :PROPERTIES:
  2546. :TEST: 2
  2547. :END:"
  2548. (org-map-entries #'point "TEST=1"))))
  2549. ;; Multiple criteria.
  2550. (should
  2551. (equal '(23)
  2552. (org-test-with-temp-text "* H1 :no:\n** H2 :yes:\n* H3 :yes:"
  2553. (let (org-odd-levels-only
  2554. (org-use-tag-inheritance nil))
  2555. (org-map-entries #'point "yes+LEVEL=1")))))
  2556. ;; "or" criteria.
  2557. (should
  2558. (equal '(12 24)
  2559. (org-test-with-temp-text "* H1 :yes:\n** H2 :yes:\n** H3 :no:"
  2560. (let (org-odd-levels-only)
  2561. (org-map-entries #'point "LEVEL=2|no")))))
  2562. (should
  2563. (equal '(1 12)
  2564. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :maybe:"
  2565. (let (org-odd-levels-only)
  2566. (org-map-entries #'point "yes|no")))))
  2567. ;; "and" criteria.
  2568. (should
  2569. (equal '(22)
  2570. (org-test-with-temp-text "* H1 :yes:\n* H2 :no:\n* H3 :yes:no:"
  2571. (let (org-odd-levels-only)
  2572. (org-map-entries #'point "yes&no")))))
  2573. ;; Setting `org-map-continue-from'
  2574. (should
  2575. (string= ""
  2576. (org-test-with-temp-text "* H1\n* H2\n* H3n* H4"
  2577. (org-map-entries
  2578. (lambda ()
  2579. (org-cut-subtree)
  2580. (setq org-map-continue-from (point))))
  2581. (buffer-string))))
  2582. (should
  2583. (string= "* H1\n* H2\n* H3\n"
  2584. (org-test-with-temp-text "* H1\n* H2\n* H3\n* H4"
  2585. (org-map-entries
  2586. (lambda ()
  2587. (when (string= "H4"
  2588. (org-element-property
  2589. :raw-value (org-element-at-point)))
  2590. (org-cut-subtree)
  2591. (setq org-map-continue-from
  2592. (org-element-property
  2593. :begin (org-element-at-point))))))
  2594. (buffer-string)))))
  2595. (ert-deftest test-org/edit-headline ()
  2596. "Test `org-edit-headline' specifications."
  2597. (should
  2598. (equal "* B"
  2599. (org-test-with-temp-text "* A"
  2600. (org-edit-headline "B")
  2601. (buffer-string))))
  2602. ;; Handle empty headings.
  2603. (should
  2604. (equal "* "
  2605. (org-test-with-temp-text "* A"
  2606. (org-edit-headline "")
  2607. (buffer-string))))
  2608. (should
  2609. (equal "* A"
  2610. (org-test-with-temp-text "* "
  2611. (org-edit-headline "A")
  2612. (buffer-string))))
  2613. ;; Handle TODO keywords and priority cookies.
  2614. (should
  2615. (equal "* TODO B"
  2616. (org-test-with-temp-text "* TODO A"
  2617. (org-edit-headline "B")
  2618. (buffer-string))))
  2619. (should
  2620. (equal "* [#A] B"
  2621. (org-test-with-temp-text "* [#A] A"
  2622. (org-edit-headline "B")
  2623. (buffer-string))))
  2624. (should
  2625. (equal "* TODO [#A] B"
  2626. (org-test-with-temp-text "* TODO [#A] A"
  2627. (org-edit-headline "B")
  2628. (buffer-string))))
  2629. ;; Handle tags.
  2630. (equal "* B :tag:"
  2631. (org-test-with-temp-text "* A :tag:"
  2632. (let ((org-tags-column 4)) (org-edit-headline "B"))
  2633. (buffer-string))))
  2634. ;;; Keywords
  2635. (ert-deftest test-org/set-regexps-and-options ()
  2636. "Test `org-set-regexps-and-options' specifications."
  2637. ;; TAGS keyword.
  2638. (should
  2639. (equal '(("A"))
  2640. (let ((org-tag-alist '(("A")))
  2641. (org-tag-persistent-alist nil))
  2642. (org-test-with-temp-text ""
  2643. (org-mode-restart)
  2644. org-current-tag-alist))))
  2645. (should
  2646. (equal '(("B"))
  2647. (let ((org-tag-alist '(("A")))
  2648. (org-tag-persistent-alist nil))
  2649. (org-test-with-temp-text "#+TAGS: B"
  2650. (org-mode-restart)
  2651. org-current-tag-alist))))
  2652. (should
  2653. (equal '(("C") ("B"))
  2654. (let ((org-tag-alist '(("A")))
  2655. (org-tag-persistent-alist '(("C"))))
  2656. (org-test-with-temp-text "#+TAGS: B"
  2657. (org-mode-restart)
  2658. org-current-tag-alist))))
  2659. (should
  2660. (equal '(("B"))
  2661. (let ((org-tag-alist '(("A")))
  2662. (org-tag-persistent-alist '(("C"))))
  2663. (org-test-with-temp-text "#+STARTUP: noptag\n#+TAGS: B"
  2664. (org-mode-restart)
  2665. org-current-tag-alist))))
  2666. (should
  2667. (equal '(("A" . ?a) ("B") ("C"))
  2668. (let ((org-tag-persistent-alist nil))
  2669. (org-test-with-temp-text "#+TAGS: A(a) B C"
  2670. (org-mode-restart)
  2671. org-current-tag-alist))))
  2672. (should
  2673. (equal '(("A") (:newline) ("B"))
  2674. (let ((org-tag-persistent-alist nil))
  2675. (org-test-with-temp-text "#+TAGS: A\n#+TAGS: B"
  2676. (org-mode-restart)
  2677. org-current-tag-alist))))
  2678. (should
  2679. (equal '((:startgroup) ("A") ("B") (:endgroup) ("C"))
  2680. (let ((org-tag-persistent-alist nil))
  2681. (org-test-with-temp-text "#+TAGS: { A B } C"
  2682. (org-mode-restart)
  2683. org-current-tag-alist))))
  2684. (should
  2685. (equal '((:startgroup) ("A") (:grouptags) ("B") ("C") (:endgroup))
  2686. (let ((org-tag-persistent-alist nil))
  2687. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2688. (org-mode-restart)
  2689. org-current-tag-alist))))
  2690. (should
  2691. (equal '(("A" "B" "C"))
  2692. (let ((org-tag-persistent-alist nil))
  2693. (org-test-with-temp-text "#+TAGS: { A : B C }"
  2694. (org-mode-restart)
  2695. org-tag-groups-alist))))
  2696. (should
  2697. (equal '((:startgrouptag) ("A") (:grouptags) ("B") ("C") (:endgrouptag))
  2698. (let ((org-tag-persistent-alist nil))
  2699. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2700. (org-mode-restart)
  2701. org-current-tag-alist))))
  2702. (should
  2703. (equal '(("A" "B" "C"))
  2704. (let ((org-tag-persistent-alist nil))
  2705. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  2706. (org-mode-restart)
  2707. org-tag-groups-alist))))
  2708. (should-not
  2709. (let ((org-tag-alist '(("A"))))
  2710. (org-test-with-temp-text "#+TAGS:"
  2711. (org-mode-restart)
  2712. org-current-tag-alist)))
  2713. ;; FILETAGS keyword.
  2714. (should
  2715. (equal '("A" "B" "C")
  2716. (org-test-with-temp-text "#+FILETAGS: :A:B:C:"
  2717. (org-mode-restart)
  2718. org-file-tags)))
  2719. ;; PROPERTY keyword. Property names are case-insensitive.
  2720. (should
  2721. (equal "foo=1"
  2722. (org-test-with-temp-text "#+PROPERTY: var foo=1"
  2723. (org-mode-restart)
  2724. (cdr (assoc "var" org-keyword-properties)))))
  2725. (should
  2726. (equal
  2727. "foo=1 bar=2"
  2728. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: var+ bar=2"
  2729. (org-mode-restart)
  2730. (cdr (assoc "var" org-keyword-properties)))))
  2731. (should
  2732. (equal
  2733. "foo=1 bar=2"
  2734. (org-test-with-temp-text "#+PROPERTY: var foo=1\n#+PROPERTY: VAR+ bar=2"
  2735. (org-mode-restart)
  2736. (cdr (assoc "var" org-keyword-properties)))))
  2737. ;; ARCHIVE keyword.
  2738. (should
  2739. (equal "%s_done::"
  2740. (org-test-with-temp-text "#+ARCHIVE: %s_done::"
  2741. (org-mode-restart)
  2742. org-archive-location)))
  2743. ;; CATEGORY keyword.
  2744. (should
  2745. (eq 'test
  2746. (org-test-with-temp-text "#+CATEGORY: test"
  2747. (org-mode-restart)
  2748. org-category)))
  2749. (should
  2750. (equal "test"
  2751. (org-test-with-temp-text "#+CATEGORY: test"
  2752. (org-mode-restart)
  2753. (cdr (assoc "CATEGORY" org-keyword-properties)))))
  2754. ;; COLUMNS keyword.
  2755. (should
  2756. (equal "%25ITEM %TAGS %PRIORITY %TODO"
  2757. (org-test-with-temp-text "#+COLUMNS: %25ITEM %TAGS %PRIORITY %TODO"
  2758. (org-mode-restart)
  2759. org-columns-default-format)))
  2760. ;; CONSTANTS keyword. Constants names are case sensitive.
  2761. (should
  2762. (equal '("299792458." "3.14")
  2763. (org-test-with-temp-text "#+CONSTANTS: c=299792458. pi=3.14"
  2764. (org-mode-restart)
  2765. (mapcar
  2766. (lambda (n) (cdr (assoc n org-table-formula-constants-local)))
  2767. '("c" "pi")))))
  2768. (should
  2769. (equal "3.14"
  2770. (org-test-with-temp-text "#+CONSTANTS: pi=22/7 pi=3.14"
  2771. (org-mode-restart)
  2772. (cdr (assoc "pi" org-table-formula-constants-local)))))
  2773. (should
  2774. (equal "22/7"
  2775. (org-test-with-temp-text "#+CONSTANTS: PI=22/7 pi=3.14"
  2776. (org-mode-restart)
  2777. (cdr (assoc "PI" org-table-formula-constants-local)))))
  2778. ;; LINK keyword.
  2779. (should
  2780. (equal
  2781. '("url1" "url2")
  2782. (org-test-with-temp-text "#+LINK: a url1\n#+LINK: b url2"
  2783. (org-mode-restart)
  2784. (mapcar (lambda (abbrev) (cdr (assoc abbrev org-link-abbrev-alist-local)))
  2785. '("a" "b")))))
  2786. ;; PRIORITIES keyword. Incomplete priorities sets are ignored.
  2787. (should
  2788. (equal
  2789. '(?X ?Z ?Y)
  2790. (org-test-with-temp-text "#+PRIORITIES: X Z Y"
  2791. (org-mode-restart)
  2792. (list org-priority-highest org-priority-lowest org-priority-default))))
  2793. (should
  2794. (equal
  2795. '(?A ?C ?B)
  2796. (org-test-with-temp-text "#+PRIORITIES: X Z"
  2797. (org-mode-restart)
  2798. (list org-priority-highest org-priority-lowest org-priority-default))))
  2799. ;; STARTUP keyword.
  2800. (should
  2801. (equal '(t t)
  2802. (org-test-with-temp-text "#+STARTUP: fold odd"
  2803. (org-mode-restart)
  2804. (list org-startup-folded org-odd-levels-only))))
  2805. ;; TODO keywords.
  2806. (should
  2807. (equal '(("A" "B") ("C"))
  2808. (org-test-with-temp-text "#+TODO: A B | C"
  2809. (org-mode-restart)
  2810. (list org-not-done-keywords org-done-keywords))))
  2811. (should
  2812. (equal '(("A" "C") ("B" "D"))
  2813. (org-test-with-temp-text "#+TODO: A | B\n#+TODO: C | D"
  2814. (org-mode-restart)
  2815. (list org-not-done-keywords org-done-keywords))))
  2816. (should
  2817. (equal '(("A" "B") ("C"))
  2818. (org-test-with-temp-text "#+TYP_TODO: A B | C"
  2819. (org-mode-restart)
  2820. (list org-not-done-keywords org-done-keywords))))
  2821. (should
  2822. (equal '((:startgroup) ("A" . ?a) (:endgroup))
  2823. (org-test-with-temp-text "#+TODO: A(a)"
  2824. (org-mode-restart)
  2825. org-todo-key-alist)))
  2826. (should
  2827. (equal '(("D" note nil) ("C" time nil) ("B" note time))
  2828. (org-test-with-temp-text "#+TODO: A(a) B(b@/!) | C(c!) D(d@)"
  2829. (org-mode-restart)
  2830. org-todo-log-states)))
  2831. ;; Enter SETUPFILE keyword.
  2832. (should
  2833. (equal "1"
  2834. (org-test-with-temp-text
  2835. (format "#+SETUPFILE: \"%s/examples/setupfile.org\"" org-test-dir)
  2836. (org-mode-restart)
  2837. (cdr (assoc "a" org-keyword-properties))))))
  2838. (ert-deftest test-org/collect-keywords ()
  2839. "Test `org-collect-keywords'."
  2840. (should-not
  2841. (org-test-with-temp-text "#+begin_example\n#+foo: bar\n#+end_example"
  2842. (org-collect-keywords '("FOO")))))
  2843. ;;; Links
  2844. ;;;; Coderefs
  2845. (ert-deftest test-org/coderef ()
  2846. "Test coderef links specifications."
  2847. (should
  2848. (org-test-with-temp-text "
  2849. #+BEGIN_SRC emacs-lisp
  2850. \(+ 1 1) (ref:sc)
  2851. #+END_SRC
  2852. \[[(sc)<point>]]"
  2853. (org-open-at-point)
  2854. (looking-at "(ref:sc)")))
  2855. ;; Find coderef even with alternate label format.
  2856. (should
  2857. (org-test-with-temp-text "
  2858. #+BEGIN_SRC emacs-lisp -l \"{ref:%s}\"
  2859. \(+ 1 1) {ref:sc}
  2860. #+END_SRC
  2861. \[[(sc)<point>]]"
  2862. (org-open-at-point)
  2863. (looking-at "{ref:sc}"))))
  2864. ;;;; Custom ID
  2865. (ert-deftest test-org/custom-id ()
  2866. "Test custom ID links specifications."
  2867. (should
  2868. (org-test-with-temp-text
  2869. "* H1\n:PROPERTIES:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2870. (org-open-at-point)
  2871. (looking-at-p "\\* H1")))
  2872. ;; Handle escape characters.
  2873. (should
  2874. (org-test-with-temp-text
  2875. "* H1\n:PROPERTIES:\n:CUSTOM_ID: [%]\n:END:\n* H2\n[[#\\[%\\]<point>]]"
  2876. (org-open-at-point)
  2877. (looking-at-p "\\* H1")))
  2878. ;; Throw an error on false positives.
  2879. (should-error
  2880. (org-test-with-temp-text
  2881. "* H1\n:DRAWER:\n:CUSTOM_ID: custom\n:END:\n* H2\n[[#custom<point>]]"
  2882. (org-open-at-point)
  2883. (looking-at-p "\\* H1"))))
  2884. ;;;; Fuzzy Links
  2885. ;; Fuzzy links [[text]] encompass links to a target (<<text>>), to
  2886. ;; a named element (#+name: text) and to headlines (* Text).
  2887. (ert-deftest test-org/fuzzy-links ()
  2888. "Test fuzzy links specifications."
  2889. ;; Fuzzy link goes in priority to a matching target.
  2890. (should
  2891. (org-test-with-temp-text
  2892. "#+NAME: Test\n|a|b|\n<<Test>>\n* Test\n<point>[[Test]]"
  2893. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2894. (looking-at "<<Test>>")))
  2895. ;; Then fuzzy link points to an element with a given name.
  2896. (should
  2897. (org-test-with-temp-text "Test\n#+NAME: Test\n|a|b|\n* Test\n<point>[[Test]]"
  2898. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2899. (looking-at "#\\+NAME: Test")))
  2900. ;; A target still lead to a matching headline otherwise.
  2901. (should
  2902. (org-test-with-temp-text "* Head1\n* Head2\n*Head3\n<point>[[Head2]]"
  2903. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2904. (looking-at "\\* Head2")))
  2905. ;; With a leading star in link, enforce heading match.
  2906. (should
  2907. (org-test-with-temp-text "* Test\n<<Test>>\n<point>[[*Test]]"
  2908. (let ((org-link-search-must-match-exact-headline nil)) (org-open-at-point))
  2909. (looking-at "\\* Test")))
  2910. ;; With a leading star in link, enforce exact heading match, even
  2911. ;; with `org-link-search-must-match-exact-headline' set to nil.
  2912. (should-error
  2913. (org-test-with-temp-text "* Test 1\nFoo Bar\n<point>[[*Test]]"
  2914. (let ((org-link-search-must-match-exact-headline nil))
  2915. (org-open-at-point))))
  2916. ;; Handle non-nil `org-link-search-must-match-exact-headline'.
  2917. (should
  2918. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[Test]]"
  2919. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2920. (looking-at "\\* Test")))
  2921. (should
  2922. (org-test-with-temp-text "* Test\nFoo Bar\n<point>[[*Test]]"
  2923. (let ((org-link-search-must-match-exact-headline t)) (org-open-at-point))
  2924. (looking-at "\\* Test")))
  2925. ;; Heading match should not care about spaces, cookies, TODO
  2926. ;; keywords, priorities, and tags. However, TODO keywords are
  2927. ;; case-sensitive.
  2928. (should
  2929. (let ((first-line
  2930. "** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent: "))
  2931. (org-test-with-temp-text
  2932. (concat first-line "\nFoo Bar\n<point>[[*Test 1 2]]")
  2933. (let ((org-link-search-must-match-exact-headline nil)
  2934. (org-todo-regexp "TODO"))
  2935. (org-open-at-point))
  2936. (looking-at (regexp-quote first-line)))))
  2937. (should-error
  2938. (org-test-with-temp-text "** todo Test 1 2\nFoo Bar\n<point>[[*Test 1 2]]"
  2939. (let ((org-link-search-must-match-exact-headline nil)
  2940. (org-todo-regexp "TODO"))
  2941. (org-open-at-point))))
  2942. ;; Heading match should still be exact.
  2943. (should-error
  2944. (org-test-with-temp-text "
  2945. ** TODO [#A] [/] Test [1/2] [33%] 1 \t 2 [%] :work:urgent:
  2946. Foo Bar
  2947. <point>[[*Test 1]]"
  2948. (let ((org-link-search-must-match-exact-headline nil)
  2949. (org-todo-regexp "TODO"))
  2950. (org-open-at-point))))
  2951. (should
  2952. (org-test-with-temp-text "* Test 1 2 3\n** Test 1 2\n<point>[[*Test 1 2]]"
  2953. (let ((org-link-search-must-match-exact-headline nil)
  2954. (org-todo-regexp "TODO"))
  2955. (org-open-at-point))
  2956. (looking-at-p (regexp-quote "** Test 1 2"))))
  2957. ;; Heading match ignores COMMENT keyword.
  2958. (should
  2959. (org-test-with-temp-text "[[*Test]]\n* COMMENT Test"
  2960. (org-open-at-point)
  2961. (looking-at "\\* COMMENT Test")))
  2962. (should
  2963. (org-test-with-temp-text "[[*Test]]\n* TODO COMMENT Test"
  2964. (org-open-at-point)
  2965. (looking-at "\\* TODO COMMENT Test")))
  2966. ;; Correctly un-escape fuzzy links.
  2967. (should
  2968. (org-test-with-temp-text "* [foo]\n[[*\\[foo\\]][With escaped characters]]"
  2969. (org-open-at-point)
  2970. (bobp)))
  2971. ;; Match search strings containing newline characters, including
  2972. ;; blank lines.
  2973. (should
  2974. (org-test-with-temp-text-in-file "Paragraph\n\nline1\nline2\n\n"
  2975. (let ((file (buffer-file-name)))
  2976. (goto-char (point-max))
  2977. (insert (format "[[file:%s::line1 line2]]" file))
  2978. (beginning-of-line)
  2979. (let ((org-link-search-must-match-exact-headline nil))
  2980. (org-open-at-point 0))
  2981. (looking-at-p "line1"))))
  2982. (should
  2983. (org-test-with-temp-text-in-file "Paragraph\n\nline1\n\nline2\n\n"
  2984. (let ((file (buffer-file-name)))
  2985. (goto-char (point-max))
  2986. (insert (format "[[file:%s::line1 line2]]" file))
  2987. (beginning-of-line)
  2988. (let ((org-link-search-must-match-exact-headline nil))
  2989. (org-open-at-point 0))
  2990. (looking-at-p "line1")))))
  2991. ;;;; Open at point
  2992. (ert-deftest test-org/open-at-point/keyword ()
  2993. "Does `org-open-at-point' open link in a keyword line?"
  2994. (should
  2995. (org-test-with-temp-text
  2996. "<<top>>\n#+KEYWORD: <point>[[top]]"
  2997. (org-open-at-point) t))
  2998. (should
  2999. (org-test-with-temp-text
  3000. "* H\n<<top>>\n#+KEYWORD: <point>[[top]]"
  3001. (org-open-at-point) t)))
  3002. (ert-deftest test-org/open-at-point/property ()
  3003. "Does `org-open-at-point' open link in property drawer?"
  3004. (should
  3005. (org-test-with-temp-text
  3006. "* Headline
  3007. :PROPERTIES:
  3008. :URL: <point>[[*Headline]]
  3009. :END:"
  3010. (org-open-at-point) t)))
  3011. (ert-deftest test-org/open-at-point/comment ()
  3012. "Does `org-open-at-point' open link in a commented line?"
  3013. (should
  3014. (org-test-with-temp-text
  3015. "<<top>>\n# <point>[[top]]"
  3016. (org-open-at-point) t))
  3017. (should
  3018. (org-test-with-temp-text
  3019. "* H\n<<top>>\n# <point>[[top]]"
  3020. (org-open-at-point) t)))
  3021. (ert-deftest test-org/open-at-point/inline-image ()
  3022. "Test `org-open-at-point' on nested links."
  3023. (should
  3024. (org-test-with-temp-text "<<top>>\n[[top][file:<point>unicorn.jpg]]"
  3025. (org-open-at-point)
  3026. (bobp))))
  3027. (ert-deftest test-org/open-at-point/radio-target ()
  3028. "Test `org-open-at-point' on radio targets."
  3029. (should
  3030. (org-test-with-temp-text "<<<target>>> <point>target"
  3031. (org-update-radio-target-regexp)
  3032. (org-open-at-point)
  3033. (eq (org-element-type (org-element-context)) 'radio-target))))
  3034. (ert-deftest test-org/open-at-point/radio-target-shadowed ()
  3035. "Test `org-open-at-point' on shadowed radio targets."
  3036. (should
  3037. (org-test-with-temp-text
  3038. "<<<target shadowed>>> <<<target>>> <point>target shadowed"
  3039. (org-update-radio-target-regexp)
  3040. (org-open-at-point)
  3041. (string=
  3042. (org-element-property :value
  3043. (org-element-radio-target-parser))
  3044. "target shadowed"))))
  3045. (ert-deftest test-org/open-at-point/tag ()
  3046. "Test `org-open-at-point' on tags."
  3047. (should
  3048. (org-test-with-temp-text "* H :<point>tag:"
  3049. (catch :result
  3050. (cl-letf (((symbol-function 'org-tags-view)
  3051. (lambda (&rest _args) (throw :result t))))
  3052. (org-open-at-point)
  3053. nil))))
  3054. (should-not
  3055. (org-test-with-temp-text-in-file "* H<point> :tag:"
  3056. (catch :result
  3057. (cl-letf (((symbol-function 'org-tags-view)
  3058. (lambda (&rest _args) (throw :result t))))
  3059. ;; When point isn't on a tag it's going to try other things,
  3060. ;; possibly trying to open attachments which will return an
  3061. ;; error if there isn't an attachment. Suppress that error.
  3062. (ignore-errors
  3063. (org-open-at-point))
  3064. nil)))))
  3065. ;;; Node Properties
  3066. (ert-deftest test-org/accumulated-properties-in-drawers ()
  3067. "Ensure properties accumulate in subtree drawers."
  3068. (org-test-at-id "75282ba2-f77a-4309-a970-e87c149fe125"
  3069. (org-babel-next-src-block)
  3070. (should (equal '(2 1) (org-babel-execute-src-block)))))
  3071. (ert-deftest test-org/custom-properties ()
  3072. "Test custom properties specifications."
  3073. ;; Standard test.
  3074. (should
  3075. (let ((org-custom-properties '("FOO")))
  3076. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  3077. (org-toggle-custom-properties-visibility)
  3078. (org-invisible-p2))))
  3079. ;; Properties are case-insensitive.
  3080. (should
  3081. (let ((org-custom-properties '("FOO")))
  3082. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:foo: val\n:END:\n"
  3083. (org-toggle-custom-properties-visibility)
  3084. (org-invisible-p2))))
  3085. (should
  3086. (let ((org-custom-properties '("foo")))
  3087. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO: val\n:END:\n"
  3088. (org-toggle-custom-properties-visibility)
  3089. (org-invisible-p2))))
  3090. ;; Multiple custom properties in the same drawer.
  3091. (should
  3092. (let ((org-custom-properties '("FOO" "BAR")))
  3093. (org-test-with-temp-text
  3094. "* H\n:PROPERTIES:\n<point>:FOO: val\n:P: 1\n:BAR: baz\n:END:\n"
  3095. (org-fold-reveal)
  3096. (org-toggle-custom-properties-visibility)
  3097. (and (org-invisible-p2)
  3098. (not (progn (forward-line) (org-invisible-p2)))
  3099. (progn (forward-line) (org-invisible-p2))))))
  3100. ;; Hide custom properties with an empty value.
  3101. (should
  3102. (let ((org-custom-properties '("FOO")))
  3103. (org-test-with-temp-text "* H\n:PROPERTIES:\n<point>:FOO:\n:END:\n"
  3104. (org-toggle-custom-properties-visibility)
  3105. (org-invisible-p2))))
  3106. ;; Do not hide fake properties.
  3107. (should-not
  3108. (let ((org-custom-properties '("FOO")))
  3109. (org-test-with-temp-text ":FOO: val\n"
  3110. (org-toggle-custom-properties-visibility)
  3111. (org-invisible-p2))))
  3112. (should-not
  3113. (let ((org-custom-properties '("A")))
  3114. (org-test-with-temp-text
  3115. "* H\n:PROPERTIES:\n:A: 1\n:END:\n\n:PROPERTIES:\n<point>:A: 2\n:END:"
  3116. (org-fold-reveal)
  3117. (org-toggle-custom-properties-visibility)
  3118. (org-invisible-p2)))))
  3119. ;;; Mark Region
  3120. (ert-deftest test-org/mark-element ()
  3121. "Test `org-mark-element' specifications."
  3122. ;; Mark beginning and end of element.
  3123. (should
  3124. (equal '(t t)
  3125. (org-test-with-temp-text "Para<point>graph"
  3126. (org-mark-element)
  3127. (list (bobp) (= (mark) (point-max))))))
  3128. (should
  3129. (equal '(t t)
  3130. (org-test-with-temp-text "P1\n\nPara<point>graph\n\nP2"
  3131. (org-mark-element)
  3132. (list (looking-at "Paragraph")
  3133. (org-with-point-at (mark) (looking-at "P2"))))))
  3134. ;; Do not set mark past (point-max).
  3135. (should
  3136. (org-test-with-temp-text "Para<point>graph"
  3137. (narrow-to-region 2 6)
  3138. (org-mark-element)
  3139. (= 6 (mark)))))
  3140. (ert-deftest test-org/mark-subtree ()
  3141. "Test `org-mark-subtree' specifications."
  3142. ;; Error when point is before first headline.
  3143. (should-error
  3144. (org-test-with-temp-text "Paragraph\n* Headline\nBody"
  3145. (progn (transient-mark-mode 1)
  3146. (org-mark-subtree))))
  3147. ;; Without argument, mark current subtree.
  3148. (should
  3149. (equal
  3150. '(12 32)
  3151. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  3152. (progn (transient-mark-mode 1)
  3153. (forward-line 2)
  3154. (org-mark-subtree)
  3155. (list (region-beginning) (region-end))))))
  3156. ;; With an argument, move ARG up.
  3157. (should
  3158. (equal
  3159. '(1 32)
  3160. (org-test-with-temp-text "* Headline\n** Sub-headline\nBody"
  3161. (progn (transient-mark-mode 1)
  3162. (forward-line 2)
  3163. (org-mark-subtree 1)
  3164. (list (region-beginning) (region-end))))))
  3165. ;; Do not get fooled by inlinetasks.
  3166. (when (featurep 'org-inlinetask)
  3167. (should
  3168. (= 1
  3169. (org-test-with-temp-text "* Headline\n*************** Task\nContents"
  3170. (progn (transient-mark-mode 1)
  3171. (forward-line 1)
  3172. (let ((org-inlinetask-min-level 15)) (org-mark-subtree))
  3173. (region-beginning)))))))
  3174. ;;; Miscellaneous
  3175. (ert-deftest test-org/sort-entries ()
  3176. "Test `org-sort-entries'."
  3177. ;; Sort alphabetically.
  3178. (should
  3179. (equal "\n* abc\n* def\n* xyz\n"
  3180. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  3181. (org-sort-entries nil ?a)
  3182. (buffer-string))))
  3183. (should
  3184. (equal "\n* xyz\n* def\n* abc\n"
  3185. (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
  3186. (org-sort-entries nil ?A)
  3187. (buffer-string))))
  3188. (should
  3189. (equal "\n* \n* klm\n* xyz\n"
  3190. (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
  3191. (org-sort-entries nil ?a)
  3192. (buffer-string))))
  3193. ;; Sort numerically.
  3194. (should
  3195. (equal "\n* 1\n* 2\n* 10\n"
  3196. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  3197. (org-sort-entries nil ?n)
  3198. (buffer-string))))
  3199. (should
  3200. (equal "\n* 10\n* 2\n* 1\n"
  3201. (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
  3202. (org-sort-entries nil ?N)
  3203. (buffer-string))))
  3204. (should
  3205. (equal "\n* \n* 1\n* 2\n"
  3206. (org-test-with-temp-text "\n* 1\n* \n* 2\n"
  3207. (org-sort-entries nil ?n)
  3208. (buffer-string))))
  3209. ;; Sort by custom function.
  3210. (should
  3211. (equal "\n* b\n* aa\n* ccc\n"
  3212. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  3213. (org-sort-entries nil ?f
  3214. (lambda ()
  3215. (length (buffer-substring (point-at-bol)
  3216. (point-at-eol))))
  3217. #'<)
  3218. (buffer-string))))
  3219. (should
  3220. (equal "\n* ccc\n* aa\n* b\n"
  3221. (org-test-with-temp-text "\n* ccc\n* b\n* aa\n"
  3222. (org-sort-entries nil ?F
  3223. (lambda ()
  3224. (length (buffer-substring (point-at-bol)
  3225. (point-at-eol))))
  3226. #'<)
  3227. (buffer-string))))
  3228. ;; Sort by TODO keyword.
  3229. (should
  3230. (equal "\n* TODO h1\n* TODO h3\n* DONE h2\n"
  3231. (org-test-with-temp-text
  3232. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  3233. (org-sort-entries nil ?o)
  3234. (buffer-string))))
  3235. (should
  3236. (equal "\n* DONE h2\n* TODO h1\n* TODO h3\n"
  3237. (org-test-with-temp-text
  3238. "\n* TODO h1\n* DONE h2\n* TODO h3\n"
  3239. (org-sort-entries nil ?O)
  3240. (buffer-string))))
  3241. ;; Sort by priority.
  3242. (should
  3243. (equal "\n* [#A] h2\n* [#B] h3\n* [#C] h1\n"
  3244. (org-test-with-temp-text
  3245. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  3246. (org-sort-entries nil ?p)
  3247. (buffer-string))))
  3248. (should
  3249. (equal "\n* [#C] h1\n* [#B] h3\n* [#A] h2\n"
  3250. (org-test-with-temp-text
  3251. "\n* [#C] h1\n* [#A] h2\n* [#B] h3\n"
  3252. (org-sort-entries nil ?P)
  3253. (buffer-string))))
  3254. ;; Sort by creation time.
  3255. (should
  3256. (equal "
  3257. * h3
  3258. [2017-05-08 Mon]
  3259. * h2
  3260. [2017-05-09 Tue]
  3261. * h1
  3262. [2018-05-09 Wed]
  3263. "
  3264. (org-test-with-temp-text
  3265. "
  3266. * h1
  3267. [2018-05-09 Wed]
  3268. * h2
  3269. [2017-05-09 Tue]
  3270. * h3
  3271. [2017-05-08 Mon]
  3272. "
  3273. (org-sort-entries nil ?c)
  3274. (buffer-string))))
  3275. ;; Sort by scheduled date.
  3276. (should
  3277. (equal "
  3278. * TODO h4
  3279. SCHEDULED: <2017-05-06 Sat>
  3280. * TODO h3
  3281. SCHEDULED: <2017-05-08 Mon>
  3282. * TODO h2
  3283. DEADLINE: <2017-05-09 Tue>
  3284. * TODO h1
  3285. DEADLINE: <2017-05-07 Sun>
  3286. "
  3287. (org-test-with-temp-text
  3288. "
  3289. * TODO h2
  3290. DEADLINE: <2017-05-09 Tue>
  3291. * TODO h1
  3292. DEADLINE: <2017-05-07 Sun>
  3293. * TODO h3
  3294. SCHEDULED: <2017-05-08 Mon>
  3295. * TODO h4
  3296. SCHEDULED: <2017-05-06 Sat>
  3297. "
  3298. (org-sort-entries nil ?s)
  3299. (buffer-string))))
  3300. ;; Sort by deadline date.
  3301. (should
  3302. (equal "
  3303. * TODO h1
  3304. DEADLINE: <2017-05-07 Sun>
  3305. * TODO h2
  3306. DEADLINE: <2017-05-09 Tue>
  3307. * TODO h3
  3308. SCHEDULED: <2017-05-08 Mon>
  3309. * TODO h4
  3310. SCHEDULED: <2017-05-06 Sat>
  3311. "
  3312. (org-test-with-temp-text
  3313. "
  3314. * TODO h2
  3315. DEADLINE: <2017-05-09 Tue>
  3316. * TODO h1
  3317. DEADLINE: <2017-05-07 Sun>
  3318. * TODO h3
  3319. SCHEDULED: <2017-05-08 Mon>
  3320. * TODO h4
  3321. SCHEDULED: <2017-05-06 Sat>
  3322. "
  3323. (org-sort-entries nil ?d)
  3324. (buffer-string))))
  3325. ;; Sort by any date/time
  3326. (should
  3327. (equal "
  3328. * TODO h4
  3329. SCHEDULED: <2017-05-06 Sat>
  3330. * TODO h1
  3331. DEADLINE: <2017-05-07 Sun>
  3332. * TODO h3
  3333. SCHEDULED: <2017-05-08 Mon>
  3334. * TODO h2
  3335. DEADLINE: <2017-05-09 Tue>
  3336. "
  3337. (org-test-with-temp-text
  3338. "
  3339. * TODO h2
  3340. DEADLINE: <2017-05-09 Tue>
  3341. * TODO h1
  3342. DEADLINE: <2017-05-07 Sun>
  3343. * TODO h3
  3344. SCHEDULED: <2017-05-08 Mon>
  3345. * TODO h4
  3346. SCHEDULED: <2017-05-06 Sat>
  3347. "
  3348. (org-sort-entries nil ?t)
  3349. (buffer-string))))
  3350. ;; Sort by clocking time.
  3351. (should
  3352. (equal "
  3353. * clocked h2
  3354. :LOGBOOK:
  3355. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3356. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  3357. :END:
  3358. * clocked h1
  3359. :LOGBOOK:
  3360. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3361. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  3362. :END:
  3363. "
  3364. (org-test-with-temp-text
  3365. "
  3366. * clocked h1
  3367. :LOGBOOK:
  3368. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3369. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:12] => 0:12
  3370. :END:
  3371. * clocked h2
  3372. :LOGBOOK:
  3373. CLOCK: [2017-05-09 Tue 00:15]--[2017-05-09 Tue 00:22] => 0:07
  3374. CLOCK: [2017-05-09 Tue 00:00]--[2017-05-09 Tue 00:10] => 0:10
  3375. :END:
  3376. "
  3377. (org-sort-entries nil ?k)
  3378. (buffer-string))))
  3379. ;; Preserve file local variables when sorting.
  3380. (should
  3381. (equal "\n* A\n* B\n# Local Variables:\n# foo: t\n# End:\n"
  3382. (org-test-with-temp-text
  3383. "\n* B\n* A\n# Local Variables:\n# foo: t\n# End:"
  3384. (org-sort-entries nil ?a)
  3385. (buffer-string)))))
  3386. (ert-deftest test-org/string-collate-greaterp ()
  3387. "Test `org-string-collate-greaterp' specifications."
  3388. (should (org-string-collate-greaterp "def" "abc"))
  3389. (should-not (org-string-collate-greaterp "abc" "def")))
  3390. (ert-deftest test-org/file-contents ()
  3391. "Test `org-file-contents' specifications."
  3392. ;; Open files.
  3393. (should
  3394. (string= "#+BIND: variable value
  3395. #+DESCRIPTION: l2
  3396. #+LANGUAGE: en
  3397. #+SELECT_TAGS: b
  3398. #+TITLE: b
  3399. #+PROPERTY: a 1
  3400. " (org-file-contents (expand-file-name "setupfile3.org"
  3401. (concat org-test-dir "examples/")))))
  3402. ;; Throw error when trying to access an invalid file.
  3403. (should-error (org-file-contents "this-file-must-not-exist"))
  3404. ;; Try to access an invalid file, but do not throw an error.
  3405. (should
  3406. (progn (org-file-contents "this-file-must-not-exist" :noerror) t))
  3407. ;; Open URL.
  3408. (should
  3409. (let ((org-resource-download-policy t))
  3410. (string= "foo"
  3411. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3412. (unwind-protect
  3413. ;; Simulate successful retrieval of a URL.
  3414. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3415. (lambda (&rest_)
  3416. (with-current-buffer buffer
  3417. (insert "HTTP/1.1 200 OK\n\nfoo"))
  3418. buffer)))
  3419. (org-file-contents "http://some-valid-url"))
  3420. (kill-buffer buffer))))))
  3421. ;; Throw error when trying to access an invalid URL.
  3422. (should-error
  3423. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3424. (unwind-protect
  3425. ;; Simulate unsuccessful retrieval of a URL.
  3426. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3427. (lambda (&rest_)
  3428. (with-current-buffer buffer
  3429. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3430. buffer)))
  3431. (org-file-contents "http://this-url-must-not-exist"))
  3432. (kill-buffer buffer))))
  3433. ;; Try to access an invalid URL, but do not throw an error.
  3434. (should-error
  3435. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3436. (unwind-protect
  3437. ;; Simulate unsuccessful retrieval of a URL.
  3438. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3439. (lambda (&rest_)
  3440. (with-current-buffer buffer
  3441. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3442. buffer)))
  3443. (org-file-contents "http://this-url-must-not-exist"))
  3444. (kill-buffer buffer))))
  3445. (should
  3446. (let ((buffer (generate-new-buffer "url-retrieve-output")))
  3447. (unwind-protect
  3448. ;; Simulate unsuccessful retrieval of a URL.
  3449. (cl-letf (((symbol-function 'url-retrieve-synchronously)
  3450. (lambda (&rest_)
  3451. (with-current-buffer buffer
  3452. (insert "HTTP/1.1 404 Not found\n\ndoes not matter"))
  3453. buffer)))
  3454. (org-file-contents "http://this-url-must-not-exist" :noerror))
  3455. (kill-buffer buffer))
  3456. t)))
  3457. ;;; Navigation
  3458. (ert-deftest test-org/next-visible-heading ()
  3459. "Test `org-next-visible-heading' specifications."
  3460. ;; Move to the beginning of the next headline, taking into
  3461. ;; consideration ARG.
  3462. (should
  3463. (org-test-with-temp-text "* H1\n* H2"
  3464. (org-next-visible-heading 1)
  3465. (looking-at "\\* H2")))
  3466. (should
  3467. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3468. (org-next-visible-heading 2)
  3469. (looking-at "\\* H3")))
  3470. ;; Ignore invisible headlines.
  3471. (should
  3472. (org-test-with-temp-text "* H1\n** H2\n* H3"
  3473. (org-cycle)
  3474. (org-next-visible-heading 1)
  3475. (looking-at "\\* H3")))
  3476. ;; Move point between headlines, not on blank lines between.
  3477. (should
  3478. (org-test-with-temp-text "* H1\n** H2\n\n\n\n* H3"
  3479. (let ((org-cycle-separator-lines 1))
  3480. (org-cycle)
  3481. (org-next-visible-heading 1))
  3482. (looking-at "\\* H3")))
  3483. ;; Move at end of buffer when there is no more headline.
  3484. (should
  3485. (org-test-with-temp-text "* H1"
  3486. (org-next-visible-heading 1)
  3487. (eobp)))
  3488. (should
  3489. (org-test-with-temp-text "* H1\n* H2"
  3490. (org-next-visible-heading 2)
  3491. (eobp)))
  3492. ;; With a negative argument, move backwards.
  3493. (should
  3494. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3495. (org-next-visible-heading -1)
  3496. (looking-at "\\* H2")))
  3497. (should
  3498. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3499. (org-next-visible-heading -2)
  3500. (looking-at "\\* H1"))))
  3501. (ert-deftest test-org/previous-visible-heading ()
  3502. "Test `org-previous-visible-heading' specifications."
  3503. ;; Move to the beginning of the next headline, taking into
  3504. ;; consideration ARG.
  3505. (should
  3506. (org-test-with-temp-text "* H1\n<point>* H2"
  3507. (org-previous-visible-heading 1)
  3508. (looking-at "\\* H1")))
  3509. (should
  3510. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3511. (org-previous-visible-heading 2)
  3512. (looking-at "\\* H1")))
  3513. ;; Ignore invisible headlines.
  3514. (should
  3515. (org-test-with-temp-text "* H1\n** H2\n<point>* H3"
  3516. (org-overview)
  3517. (org-previous-visible-heading 1)
  3518. (looking-at "\\* H1")))
  3519. ;; Move point between headlines, not on blank lines between.
  3520. (should
  3521. (org-test-with-temp-text "* H1\n\n\n\n** H2\n<point>* H3"
  3522. (let ((org-cycle-separator-lines 1))
  3523. (org-overview)
  3524. (org-previous-visible-heading 1))
  3525. (looking-at "\\* H1")))
  3526. ;; Move at end of buffer when there is no more headline.
  3527. (should
  3528. (org-test-with-temp-text "* H1"
  3529. (org-previous-visible-heading 1)
  3530. (bobp)))
  3531. (should
  3532. (org-test-with-temp-text "* H1\n* <point>H2"
  3533. (org-previous-visible-heading 2)
  3534. (bobp)))
  3535. ;; Invisible parts may not start at a headline, i.e., when revealing
  3536. ;; parts of the buffer. Handle this.
  3537. (should
  3538. (org-test-with-temp-text "* Main\n** H1\nFoo\n** H2\nBar\n** H3\nBaz"
  3539. (org-overview)
  3540. (search-forward "H1")
  3541. (org-show-context 'minimal)
  3542. (org-cycle)
  3543. (search-forward "H3")
  3544. (org-show-context 'minimal)
  3545. ;; At this point, buffer displays, with point at "|",
  3546. ;;
  3547. ;; * Main
  3548. ;; ** H1
  3549. ;; Foo
  3550. ;; ** H3|
  3551. (org-previous-visible-heading 1)
  3552. (looking-at "\\*+ H1"))))
  3553. (ert-deftest test-org/forward-heading-same-level ()
  3554. "Test `org-forward-heading-same-level' specifications."
  3555. ;; Test navigation at top level, forward and backward.
  3556. (should
  3557. (equal "* H2"
  3558. (org-test-with-temp-text "* H1\n* H2"
  3559. (org-forward-heading-same-level 1)
  3560. (buffer-substring-no-properties (point) (line-end-position)))))
  3561. (should
  3562. (equal "* H1"
  3563. (org-test-with-temp-text "* H1\n<point>* H2"
  3564. (org-forward-heading-same-level -1)
  3565. (buffer-substring-no-properties (point) (line-end-position)))))
  3566. ;; Test navigation in a sub-tree, forward and backward.
  3567. (should
  3568. (equal "* H2"
  3569. (org-test-with-temp-text "* H1\n** H11\n** H12\n* H2"
  3570. (org-forward-heading-same-level 1)
  3571. (buffer-substring-no-properties (point) (line-end-position)))))
  3572. (should
  3573. (equal "* H1"
  3574. (org-test-with-temp-text "* H1\n** H11\n** H12\n<point>* H2"
  3575. (org-forward-heading-same-level -1)
  3576. (buffer-substring-no-properties (point) (line-end-position)))))
  3577. ;; Stop at first or last sub-heading.
  3578. (should-not
  3579. (equal "* H2"
  3580. (org-test-with-temp-text "* H1\n** H11\n<point>** H12\n* H2"
  3581. (org-forward-heading-same-level 1)
  3582. (buffer-substring-no-properties (point) (line-end-position)))))
  3583. (should-not
  3584. (equal "* H2"
  3585. (org-test-with-temp-text "* H1\n<point>** H11\n** H12\n* H2"
  3586. (org-forward-heading-same-level -1)
  3587. (buffer-substring-no-properties (point) (line-end-position)))))
  3588. ;; Allow multiple moves.
  3589. (should
  3590. (equal "* H3"
  3591. (org-test-with-temp-text "* H1\n* H2\n* H3"
  3592. (org-forward-heading-same-level 2)
  3593. (buffer-substring-no-properties (point) (line-end-position)))))
  3594. (should
  3595. (equal "* H1"
  3596. (org-test-with-temp-text "* H1\n* H2\n<point>* H3"
  3597. (org-forward-heading-same-level -2)
  3598. (buffer-substring-no-properties (point) (line-end-position)))))
  3599. ;; Ignore spurious moves when first (or last) sibling is reached.
  3600. (should
  3601. (equal "** H3"
  3602. (org-test-with-temp-text "* First\n<point>** H1\n** H2\n** H3\n* Last"
  3603. (org-forward-heading-same-level 100)
  3604. (buffer-substring-no-properties (point) (line-end-position)))))
  3605. (should
  3606. (equal "** H1"
  3607. (org-test-with-temp-text "* First\n** H1\n** H2\n<point>** H3\n* Last"
  3608. (org-forward-heading-same-level -100)
  3609. (buffer-substring-no-properties (point) (line-end-position))))))
  3610. (ert-deftest test-org/end-of-meta-data ()
  3611. "Test `org-end-of-meta-data' specifications."
  3612. ;; Skip planning line.
  3613. (should
  3614. (org-test-with-temp-text "* Headline\nSCHEDULED: <2014-03-04 tue.>"
  3615. (org-end-of-meta-data)
  3616. (eobp)))
  3617. ;; Skip properties drawer.
  3618. (should
  3619. (org-test-with-temp-text
  3620. "* Headline\nSCHEDULED: <2014-03-04 tue.>\n:PROPERTIES:\n:A: 1\n:END:"
  3621. (org-end-of-meta-data)
  3622. (eobp)))
  3623. ;; Skip both.
  3624. (should
  3625. (org-test-with-temp-text "* Headline\n:PROPERTIES:\n:A: 1\n:END:"
  3626. (org-end-of-meta-data)
  3627. (eobp)))
  3628. ;; Nothing to skip, go to first line.
  3629. (should
  3630. (org-test-with-temp-text "* Headline\nContents"
  3631. (org-end-of-meta-data)
  3632. (looking-at "Contents")))
  3633. ;; With option argument, skip empty lines, regular drawers and
  3634. ;; clocking lines.
  3635. (should
  3636. (org-test-with-temp-text "* Headline\n\nContents"
  3637. (org-end-of-meta-data t)
  3638. (looking-at "Contents")))
  3639. (should
  3640. (org-test-with-temp-text "* Headline\nCLOCK:\nContents"
  3641. (org-end-of-meta-data t)
  3642. (looking-at "Contents")))
  3643. (should
  3644. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\n:END:\nContents"
  3645. (org-end-of-meta-data t)
  3646. (looking-at "Contents")))
  3647. ;; Special case: do not skip incomplete drawers.
  3648. (should
  3649. (org-test-with-temp-text "* Headline\n:LOGBOOK:\nlogging\nContents"
  3650. (org-end-of-meta-data t)
  3651. (looking-at ":LOGBOOK:")))
  3652. ;; Special case: Be careful about consecutive headlines.
  3653. (should-not
  3654. (org-test-with-temp-text "* H1\n*H2\nContents"
  3655. (org-end-of-meta-data t)
  3656. (looking-at "Contents"))))
  3657. (ert-deftest test-org/shiftright-heading ()
  3658. "Test `org-shiftright' on headings."
  3659. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  3660. (should
  3661. (equal "* TODO a1\n** a2\n* DONE b1\n"
  3662. (org-test-with-temp-text "* a1\n** a2\n* DONE b1\n"
  3663. (org-shiftright)
  3664. (buffer-string))))
  3665. (should
  3666. (equal "* TODO a1\n** TODO a2\n* b1\n"
  3667. (org-test-with-temp-text "* a1\n** a2\n* DONE b1\n"
  3668. (let ((org-loop-over-headlines-in-active-region t))
  3669. (transient-mark-mode 1)
  3670. (push-mark (point) t t)
  3671. (search-forward "* DONE b1")
  3672. (org-shiftright))
  3673. (buffer-string))))
  3674. (should
  3675. (equal "* TODO a1\n** a2\n* b1\n"
  3676. (org-test-with-temp-text "* a1\n** a2\n* DONE b1\n"
  3677. (let ((org-loop-over-headlines-in-active-region 'start-level))
  3678. (transient-mark-mode 1)
  3679. (push-mark (point) t t)
  3680. (search-forward "* DONE b1")
  3681. (org-shiftright))
  3682. (buffer-string))))))
  3683. (ert-deftest test-org/beginning-of-line ()
  3684. "Test `org-beginning-of-line' specifications."
  3685. ;; Move to beginning of line. If current line in invisible, move to
  3686. ;; beginning of visible line instead.
  3687. (should
  3688. (org-test-with-temp-text "Some text\nSome other text<point>"
  3689. (org-beginning-of-line)
  3690. (bolp)))
  3691. (should
  3692. (org-test-with-temp-text "* H1\n** H2<point>"
  3693. (org-overview)
  3694. (org-beginning-of-line)
  3695. (= (line-beginning-position) 1)))
  3696. ;; With `visual-line-mode' active, move to beginning of visual line.
  3697. (should-not
  3698. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3699. (visual-line-mode)
  3700. (dotimes (_ 1000) (insert "very "))
  3701. (org-beginning-of-line)
  3702. (bolp)))
  3703. ;; In a wide headline, with `visual-line-mode', prefer going to the
  3704. ;; beginning of a visual line than to the logical beginning of line,
  3705. ;; even if special movement is active.
  3706. (should-not
  3707. (org-test-with-temp-text "* A <point>long headline"
  3708. (visual-line-mode)
  3709. (dotimes (_ 1000) (insert "very "))
  3710. (goto-char (point-max))
  3711. (org-beginning-of-line)
  3712. (bobp)))
  3713. (should-not
  3714. (org-test-with-temp-text "* A <point>long headline"
  3715. (visual-line-mode)
  3716. (dotimes (_ 1000) (insert "very "))
  3717. (goto-char (point-max))
  3718. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line))
  3719. (bobp)))
  3720. ;; At an headline with special movement, first move at beginning of
  3721. ;; title, then at the beginning of line, rinse, repeat.
  3722. (should
  3723. (org-test-with-temp-text "* TODO Headline<point>"
  3724. (let ((org-special-ctrl-a/e t))
  3725. (and (progn (org-beginning-of-line) (looking-at-p "Headline"))
  3726. (progn (org-beginning-of-line) (bolp))
  3727. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3728. (should
  3729. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3730. (let ((org-special-ctrl-a/e t))
  3731. (org-beginning-of-line)
  3732. (looking-at "Headline"))))
  3733. (should
  3734. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3735. (let ((org-special-ctrl-a/e '(t . nil)))
  3736. (org-beginning-of-line)
  3737. (looking-at "Headline"))))
  3738. (should-not
  3739. (org-test-with-temp-text "* TODO [#A] Headline<point>"
  3740. (let ((org-special-ctrl-a/e '(nil . nil)))
  3741. (org-beginning-of-line)
  3742. (looking-at "Headline"))))
  3743. ;; At an headline with reversed movement, first move to beginning of
  3744. ;; line, then to the beginning of title.
  3745. (should
  3746. (org-test-with-temp-text "* TODO Headline<point>"
  3747. (let ((org-special-ctrl-a/e 'reversed)
  3748. (this-command last-command))
  3749. (and (progn (org-beginning-of-line) (bolp))
  3750. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3751. (should
  3752. (org-test-with-temp-text "* TODO Headline<point>"
  3753. (let ((org-special-ctrl-a/e '(reversed . nil))
  3754. (this-command last-command))
  3755. (and (progn (org-beginning-of-line) (bolp))
  3756. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3757. (should-not
  3758. (org-test-with-temp-text "* TODO Headline<point>"
  3759. (let ((org-special-ctrl-a/e '(t . nil))
  3760. (this-command last-command))
  3761. (and (progn (org-beginning-of-line) (bolp))
  3762. (progn (org-beginning-of-line) (looking-at-p "Headline"))))))
  3763. ;; At an item with special movement, first move after to beginning
  3764. ;; of title, then to the beginning of line, rinse, repeat.
  3765. (should
  3766. (org-test-with-temp-text "- [ ] Item<point>"
  3767. (let ((org-special-ctrl-a/e t))
  3768. (and (progn (org-beginning-of-line) (looking-at-p "Item"))
  3769. (progn (org-beginning-of-line) (bolp))
  3770. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3771. ;; At an item with reversed movement, first move to beginning of
  3772. ;; line, then to the beginning of title.
  3773. (should
  3774. (org-test-with-temp-text "- [X] Item<point>"
  3775. (let ((org-special-ctrl-a/e 'reversed)
  3776. (this-command last-command))
  3777. (and (progn (org-beginning-of-line) (bolp))
  3778. (progn (org-beginning-of-line) (looking-at-p "Item"))))))
  3779. ;; Leave point before invisible characters at column 0.
  3780. (should
  3781. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3782. (let ((org-special-ctrl-a/e nil))
  3783. (org-beginning-of-line)
  3784. (bolp))))
  3785. (should
  3786. (org-test-with-temp-text "[[https://orgmode.org]]<point>"
  3787. (let ((org-special-ctrl-a/e t))
  3788. (org-beginning-of-line)
  3789. (bolp))))
  3790. (should
  3791. (org-test-with-temp-text "[[http<point>://orgmode.org]]"
  3792. (visual-line-mode)
  3793. (org-beginning-of-line)
  3794. (bolp)))
  3795. ;; Special case: Do not error when the buffer contains only a single
  3796. ;; asterisk.
  3797. (should
  3798. (org-test-with-temp-text "*<point>"
  3799. (let ((org-special-ctrl-a/e t)) (org-beginning-of-line) t)))
  3800. (should
  3801. (org-test-with-temp-text "*<point>"
  3802. (let ((org-special-ctrl-a/e nil)) (org-beginning-of-line) t))))
  3803. (ert-deftest test-org/end-of-line ()
  3804. "Test `org-end-of-line' specifications."
  3805. ;; Standard test.
  3806. (should
  3807. (org-test-with-temp-text "Some text\nSome other text"
  3808. (org-end-of-line)
  3809. (eolp)))
  3810. ;; With `visual-line-mode' active, move to end of visible line.
  3811. ;; However, never go past ellipsis.
  3812. (should-not
  3813. (org-test-with-temp-text "A <point>long line of text\nSome other text"
  3814. (visual-line-mode)
  3815. (dotimes (_ 1000) (insert "very "))
  3816. (goto-char (point-min))
  3817. (org-end-of-line)
  3818. (eolp)))
  3819. (should-not
  3820. (org-test-with-temp-text "* A short headline\nSome contents"
  3821. (visual-line-mode)
  3822. (org-overview)
  3823. (org-end-of-line)
  3824. (eobp)))
  3825. ;; In a wide headline, with `visual-line-mode', prefer going to end
  3826. ;; of visible line if tags, or end of line, are farther.
  3827. (should-not
  3828. (org-test-with-temp-text "* A <point>long headline"
  3829. (visual-line-mode)
  3830. (dotimes (_ 1000) (insert "very "))
  3831. (goto-char (point-min))
  3832. (org-end-of-line)
  3833. (eolp)))
  3834. (should-not
  3835. (org-test-with-temp-text "* A <point>long headline :tag:"
  3836. (visual-line-mode)
  3837. (dotimes (_ 1000) (insert "very "))
  3838. (goto-char (point-min))
  3839. (org-end-of-line)
  3840. (eolp)))
  3841. ;; At an headline without special movement, go to end of line.
  3842. ;; However, never go past ellipsis.
  3843. (should
  3844. (org-test-with-temp-text "* Headline2b :tag:\n"
  3845. (let ((org-special-ctrl-a/e nil))
  3846. (and (progn (org-end-of-line) (eolp))
  3847. (progn (org-end-of-line) (eolp))))))
  3848. (should
  3849. (org-test-with-temp-text "* Headline2b :tag:\n"
  3850. (let ((org-special-ctrl-a/e '(t . nil)))
  3851. (and (progn (org-end-of-line) (eolp))
  3852. (progn (org-end-of-line) (eolp))))))
  3853. (should
  3854. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3855. (org-overview)
  3856. (let ((org-special-ctrl-a/e nil))
  3857. (org-end-of-line)
  3858. (= 1 (line-beginning-position)))))
  3859. ;; At an headline with special movement, first move before tags,
  3860. ;; then at the end of line, rinse, repeat. However, never go past
  3861. ;; ellipsis.
  3862. (should
  3863. (org-test-with-temp-text "* Headline1 :tag:\n"
  3864. (let ((org-special-ctrl-a/e t))
  3865. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3866. (progn (org-end-of-line) (eolp))
  3867. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3868. (should
  3869. (org-test-with-temp-text "* Headline1 :tag:\n"
  3870. (let ((org-special-ctrl-a/e '(nil . t)))
  3871. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3872. (progn (org-end-of-line) (eolp))
  3873. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3874. (should-not
  3875. (org-test-with-temp-text "* Headline1 :tag:\n"
  3876. (let ((org-special-ctrl-a/e '(nil . nil)))
  3877. (and (progn (org-end-of-line) (looking-at-p " :tag:"))
  3878. (progn (org-end-of-line) (eolp))
  3879. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3880. (should
  3881. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3882. (org-overview)
  3883. (let ((org-special-ctrl-a/e t))
  3884. (org-end-of-line)
  3885. (org-end-of-line)
  3886. (= 1 (line-beginning-position)))))
  3887. ;; At an headline, with reversed movement, first go to end of line,
  3888. ;; then before tags. However, never go past ellipsis.
  3889. (should
  3890. (org-test-with-temp-text "* Headline3 :tag:\n"
  3891. (let ((org-special-ctrl-a/e 'reversed)
  3892. (this-command last-command))
  3893. (and (progn (org-end-of-line) (eolp))
  3894. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3895. (should
  3896. (org-test-with-temp-text "* Headline3 :tag:\n"
  3897. (let ((org-special-ctrl-a/e '(nil . reversed))
  3898. (this-command last-command))
  3899. (and (progn (org-end-of-line) (eolp))
  3900. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3901. (should-not
  3902. (org-test-with-temp-text "* Headline3 :tag:\n"
  3903. (let ((org-special-ctrl-a/e '(nil . t))
  3904. (this-command last-command))
  3905. (and (progn (org-end-of-line) (eolp))
  3906. (progn (org-end-of-line) (looking-at-p " :tag:"))))))
  3907. (should
  3908. (org-test-with-temp-text "* Headline2a :tag:\n** Sub"
  3909. (org-overview)
  3910. (let ((org-special-ctrl-a/e 'reversed))
  3911. (org-end-of-line)
  3912. (= 1 (line-beginning-position)))))
  3913. ;; At a block without hidden contents.
  3914. (should
  3915. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3916. (progn (org-end-of-line) (eolp))))
  3917. ;; At a block with hidden contents.
  3918. (should-not
  3919. (org-test-with-temp-text "#+BEGIN_CENTER\nContents\n#+END_CENTER"
  3920. (let ((org-special-ctrl-a/e t))
  3921. (org-fold-hide-block-toggle)
  3922. (org-end-of-line)
  3923. (eobp))))
  3924. ;; Get past invisible characters at the end of line.
  3925. (should
  3926. (org-test-with-temp-text "[[https://orgmode.org]]"
  3927. (org-end-of-line)
  3928. (eolp))))
  3929. (ert-deftest test-org/open-line ()
  3930. "Test `org-open-line' specifications."
  3931. ;; Call `open-line' outside of tables.
  3932. (should
  3933. (equal "\nText"
  3934. (org-test-with-temp-text "Text"
  3935. (org-open-line 1)
  3936. (buffer-string))))
  3937. ;; At a table, create a row above.
  3938. (should
  3939. (equal "\n| |\n| a |"
  3940. (org-test-with-temp-text "\n<point>| a |"
  3941. (org-open-line 1)
  3942. (buffer-string))))
  3943. ;; At the very first character of the buffer, also call `open-line'.
  3944. (should
  3945. (equal "\n| a |"
  3946. (org-test-with-temp-text "| a |"
  3947. (org-open-line 1)
  3948. (buffer-string))))
  3949. ;; Narrowing does not count.
  3950. (should
  3951. (equal "Text\n| |\n| a |"
  3952. (org-test-with-temp-text "Text\n<point>| a |"
  3953. (narrow-to-region (point) (point-max))
  3954. (org-open-line 1)
  3955. (widen)
  3956. (buffer-string)))))
  3957. (ert-deftest test-org/forward-sentence ()
  3958. "Test `org-forward-sentence' specifications."
  3959. ;; At the end of a table cell, move to the end of the next one.
  3960. (should
  3961. (org-test-with-temp-text "| a<point> | b |"
  3962. (org-forward-sentence)
  3963. (looking-at " |$")))
  3964. ;; Elsewhere in a cell, move to its end.
  3965. (should
  3966. (org-test-with-temp-text "| a<point>c | b |"
  3967. (org-forward-sentence)
  3968. (looking-at " | b |$")))
  3969. ;; Otherwise, simply call `forward-sentence'.
  3970. (should
  3971. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3972. (org-forward-sentence)
  3973. (looking-at " Sentence 2.")))
  3974. (should
  3975. (org-test-with-temp-text "Sentence<point> 1. Sentence 2."
  3976. (org-forward-sentence)
  3977. (org-forward-sentence)
  3978. (eobp)))
  3979. ;; At the end of an element, jump to the next one, without stopping
  3980. ;; on blank lines in-between.
  3981. (should
  3982. (org-test-with-temp-text "Paragraph 1.<point>\n\nParagraph 2."
  3983. (org-forward-sentence)
  3984. (eobp)))
  3985. ;; Headlines are considered to be sentences by themselves, even if
  3986. ;; they do not end with a full stop.
  3987. (should
  3988. (equal
  3989. "* Headline"
  3990. (org-test-with-temp-text "* <point>Headline\nSentence."
  3991. (org-forward-sentence)
  3992. (buffer-substring-no-properties (line-beginning-position) (point)))))
  3993. (should
  3994. (org-test-with-temp-text "* Headline<point>\nSentence."
  3995. (org-forward-sentence)
  3996. (eobp)))
  3997. (should
  3998. (org-test-with-temp-text "Sentence.<point>\n\n* Headline\n\nSentence 2."
  3999. (org-forward-sentence)
  4000. (and (org-at-heading-p) (eolp)))))
  4001. (ert-deftest test-org/backward-sentence ()
  4002. "Test `org-backward-sentence' specifications."
  4003. ;; At the beginning of a table cell, move to the beginning of the
  4004. ;; previous one.
  4005. (should
  4006. (org-test-with-temp-text "| a | <point>b |"
  4007. (org-backward-sentence)
  4008. (looking-at "a | b |$")))
  4009. ;; Elsewhere in a cell, move to its beginning.
  4010. (should
  4011. (org-test-with-temp-text "| a | b<point>c |"
  4012. (org-backward-sentence)
  4013. (looking-at "bc |$")))
  4014. ;; Otherwise, simply call `backward-sentence'.
  4015. (should
  4016. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  4017. (org-backward-sentence)
  4018. (looking-at "Sentence 2.")))
  4019. (should
  4020. (org-test-with-temp-text "Sentence 1. Sentence<point> 2."
  4021. (org-backward-sentence)
  4022. (org-backward-sentence)
  4023. (bobp)))
  4024. ;; Make sure to hit the beginning of a sentence on the same line as
  4025. ;; an item.
  4026. (should
  4027. (org-test-with-temp-text "- Line 1\n line <point>2."
  4028. (org-backward-sentence)
  4029. (looking-at "Line 1"))))
  4030. (ert-deftest test-org/forward-paragraph ()
  4031. "Test `org-forward-paragraph' specifications."
  4032. ;; At end of buffer, do not return an error.
  4033. (should
  4034. (org-test-with-temp-text "Paragraph"
  4035. (goto-char (point-max))
  4036. (org-forward-paragraph)
  4037. t))
  4038. ;; Standard test.
  4039. (should
  4040. (= 2
  4041. (org-test-with-temp-text "P1\n\nP2"
  4042. (org-forward-paragraph)
  4043. (org-current-line))))
  4044. (should
  4045. (= 2
  4046. (org-test-with-temp-text "P1\n\nP2\n\nP3"
  4047. (org-forward-paragraph)
  4048. (org-current-line))))
  4049. ;; Enter greater elements.
  4050. (should
  4051. (= 2
  4052. (org-test-with-temp-text "#+begin_center\nP1\n#+end_center\nP2"
  4053. (org-forward-paragraph)
  4054. (org-current-line))))
  4055. ;; Do not enter elements with invisible contents.
  4056. (should
  4057. (= 4
  4058. (org-test-with-temp-text "* H1\n P1\n\n* H2"
  4059. (org-cycle)
  4060. (org-forward-paragraph)
  4061. (org-current-line))))
  4062. (should
  4063. (= 6
  4064. (org-test-with-temp-text "#+begin_center\nP1\n\nP2\n#+end_center\nP3"
  4065. (org-fold-hide-block-toggle)
  4066. (org-forward-paragraph)
  4067. (org-current-line))))
  4068. ;; On an item or a footnote definition, move past the first element
  4069. ;; inside, if any.
  4070. (should
  4071. (= 2
  4072. (org-test-with-temp-text "- Item1\n\n Paragraph\n- Item2"
  4073. (org-forward-paragraph)
  4074. (org-current-line))))
  4075. (should
  4076. (= 2
  4077. (org-test-with-temp-text "[fn:1] Def1\n\nParagraph\n\n[fn:2] Def2"
  4078. (org-forward-paragraph)
  4079. (org-current-line))))
  4080. ;; On a table (resp. a property drawer) do not move through table
  4081. ;; rows (resp. node properties).
  4082. (should
  4083. (org-test-with-temp-text "| a | b |\n| c | d |\nParagraph"
  4084. (org-forward-paragraph)
  4085. (looking-at "Paragraph")))
  4086. (should
  4087. (org-test-with-temp-text
  4088. "* H\n<point>:PROPERTIES:\n:prop: value\n:END:\nParagraph"
  4089. (org-forward-paragraph)
  4090. (looking-at "Paragraph")))
  4091. ;; Skip consecutive keywords, clocks and diary S-exps.
  4092. (should
  4093. (org-test-with-temp-text "#+key: val\n #+key2: val\n#+key3: val\n"
  4094. (org-forward-paragraph)
  4095. (eobp)))
  4096. (should
  4097. (org-test-with-temp-text "CLOCK: val\n CLOCK: val\nCLOCK: val\n"
  4098. (org-forward-paragraph)
  4099. (eobp)))
  4100. (should
  4101. (org-test-with-temp-text "%%(foo)\n%%(bar)\n%%(baz)\n"
  4102. (org-forward-paragraph)
  4103. (eobp)))
  4104. (should-not
  4105. (org-test-with-temp-text "#+key: val\n #+key2: val\n\n#+key3: val\n"
  4106. (org-forward-paragraph)
  4107. (eobp)))
  4108. (should-not
  4109. (org-test-with-temp-text "#+key: val\nCLOCK: ...\n"
  4110. (org-forward-paragraph)
  4111. (eobp)))
  4112. ;; In a plain list with one item every line, skip the whole list,
  4113. ;; even with point in the middle of the list.
  4114. (should
  4115. (org-test-with-temp-text "- A\n - B\n- C\n"
  4116. (org-forward-paragraph)
  4117. (eobp)))
  4118. (should
  4119. (org-test-with-temp-text "- A\n - <point>B\n- C\n"
  4120. (org-forward-paragraph)
  4121. (eobp)))
  4122. ;; On a comment, verse or source block, stop at "contents"
  4123. ;; boundaries and blank lines.
  4124. (should
  4125. (= 2
  4126. (org-test-with-temp-text "#+begin_src emacs-lisp\nL1\n\nL2\n#+end_src"
  4127. (org-forward-paragraph)
  4128. (org-current-line))))
  4129. (should
  4130. (= 3
  4131. (org-test-with-temp-text "#+begin_verse\n<point>L1\n\nL2\n#+end_verse"
  4132. (org-forward-paragraph)
  4133. (org-current-line))))
  4134. (should
  4135. (= 5
  4136. (org-test-with-temp-text "#+begin_comment\nL1\n\n<point>L2\n#+end_comment"
  4137. (org-forward-paragraph)
  4138. (org-current-line))))
  4139. ;; Being on an affiliated keyword shouldn't make any difference.
  4140. (should
  4141. (org-test-with-temp-text "#+name: para\n#+caption: caption\nPara"
  4142. (org-forward-paragraph)
  4143. (eobp))))
  4144. (ert-deftest test-org/backward-paragraph ()
  4145. "Test `org-backward-paragraph' specifications."
  4146. ;; Do not error at beginning of buffer.
  4147. (should
  4148. (org-test-with-temp-text "Paragraph"
  4149. (org-backward-paragraph)
  4150. t))
  4151. ;; At blank lines at the very beginning of a buffer, move to
  4152. ;; point-min.
  4153. (should
  4154. (org-test-with-temp-text "\n\n<point>\n\nParagraph"
  4155. (org-backward-paragraph)
  4156. (bobp)))
  4157. ;; Regular test.
  4158. (should
  4159. (= 2
  4160. (org-test-with-temp-text "P1\n\nP2<point>"
  4161. (org-backward-paragraph)
  4162. (org-current-line))))
  4163. (should
  4164. (= 4
  4165. (org-test-with-temp-text "P1\n\nP2\n\nP3<point>"
  4166. (org-backward-paragraph)
  4167. (org-current-line))))
  4168. ;; Try to move on the line above current element.
  4169. (should
  4170. (= 2
  4171. (org-test-with-temp-text "\n\n<point>Paragraph"
  4172. (org-backward-paragraph)
  4173. (org-current-line))))
  4174. ;; Do not leave point in an invisible area.
  4175. (should
  4176. (org-test-with-temp-text "* H1\n P1\n\n* H2"
  4177. (org-cycle)
  4178. (goto-char (point-max))
  4179. (beginning-of-line)
  4180. (org-backward-paragraph)
  4181. (bobp)))
  4182. (should
  4183. (org-test-with-temp-text "#+begin_center\nP1\n\nP2\n#+end_center\n"
  4184. (org-fold-hide-block-toggle)
  4185. (goto-char (point-max))
  4186. (org-backward-paragraph)
  4187. (bobp)))
  4188. ;; On the first element in an item or a footnote definition, jump
  4189. ;; before the footnote or the item.
  4190. (should
  4191. (org-test-with-temp-text "- line1<point>"
  4192. (org-backward-paragraph)
  4193. (bobp)))
  4194. (should
  4195. (org-test-with-temp-text "[fn:1] line1n<point>"
  4196. (org-backward-paragraph)
  4197. (bobp)))
  4198. ;; On the second element in an item or a footnote definition, jump
  4199. ;; to item or the definition.
  4200. (should
  4201. (= 2
  4202. (org-test-with-temp-text "- line1\n\n<point> line2"
  4203. (org-backward-paragraph)
  4204. (org-current-line))))
  4205. (should
  4206. (= 2
  4207. (org-test-with-temp-text "[fn:1] line1\n\n<point> line2"
  4208. (org-backward-paragraph)
  4209. (org-current-line))))
  4210. ;; On a table (resp. a property drawer), ignore table rows
  4211. ;; (resp. node properties).
  4212. (should
  4213. (org-test-with-temp-text "| a | b |\n| c | d |\n<point>P1"
  4214. (org-backward-paragraph)
  4215. (bobp)))
  4216. (should
  4217. (= 2
  4218. (org-test-with-temp-text
  4219. "* H\n:PROPERTIES:\n:prop: value\n:END:\n<point>P1"
  4220. (org-backward-paragraph)
  4221. (org-current-line))))
  4222. ;; In a plain list with one item every line, skip the whole list,
  4223. ;; even with point in the middle of the list.
  4224. (should
  4225. (org-test-with-temp-text "- A\n - B\n- C\n<point>"
  4226. (org-backward-paragraph)
  4227. (bobp)))
  4228. (should
  4229. (org-test-with-temp-text "- A\n - B\n- <point>C\n"
  4230. (org-backward-paragraph)
  4231. (bobp)))
  4232. ;; Skip consecutive keywords, clocks and diary S-exps.
  4233. (should
  4234. (org-test-with-temp-text "#+key: val\n #+key2: val\n#+key3: val\n<point>"
  4235. (org-backward-paragraph)
  4236. (bobp)))
  4237. (should
  4238. (org-test-with-temp-text "CLOCK: val\n CLOCK: val\nCLOCK: val\n<point>"
  4239. (org-backward-paragraph)
  4240. (bobp)))
  4241. (should
  4242. (org-test-with-temp-text "%%(foo)\n%%(bar)\n%%(baz)\n<point>"
  4243. (org-backward-paragraph)
  4244. (bobp)))
  4245. (should-not
  4246. (org-test-with-temp-text "#+key: val\n #+key2: val\n\n#+key3: val\n<point>"
  4247. (org-backward-paragraph)
  4248. (bobp)))
  4249. (should-not
  4250. (org-test-with-temp-text "#+key: val\nCLOCK: ...\n<point>"
  4251. (org-backward-paragraph)
  4252. (bobp)))
  4253. ;; On a comment, example, source and verse blocks, stop at blank
  4254. ;; lines.
  4255. (should
  4256. (= 1
  4257. (org-test-with-temp-text
  4258. "#+begin_comment\n<point>L1\n\nL2\n\nL3\n#+end_comment"
  4259. (org-backward-paragraph)
  4260. (org-current-line))))
  4261. (should
  4262. (= 2
  4263. (org-test-with-temp-text
  4264. "#+begin_verse\nL1\n\n<point>L2\n\nL3\n#+end_verse"
  4265. (org-backward-paragraph)
  4266. (org-current-line))))
  4267. (should
  4268. (= 3
  4269. (org-test-with-temp-text
  4270. "#+begin_src emacs-lisp\nL1\n\nL2\n\n<point>L3\n#+end_src"
  4271. (org-backward-paragraph)
  4272. (org-current-line))))
  4273. ;; When called from the opening line itself, however, move to
  4274. ;; beginning of block.
  4275. (should
  4276. (org-test-with-temp-text "#+begin_<point>example\nL1\n#+end_example"
  4277. (org-backward-paragraph)
  4278. (bobp)))
  4279. ;; On an empty heading, move above it.
  4280. (should
  4281. (org-test-with-temp-text "\n* <point>"
  4282. (org-backward-paragraph)
  4283. (bobp)))
  4284. (should
  4285. (org-test-with-temp-text "\n* \n<point>"
  4286. (org-backward-paragraph)
  4287. (bobp))))
  4288. (ert-deftest test-org/forward-element ()
  4289. "Test `org-forward-element' specifications."
  4290. ;; 1. At EOB: should error.
  4291. (org-test-with-temp-text "Some text\n"
  4292. (goto-char (point-max))
  4293. (should-error (org-forward-element)))
  4294. ;; 2. Standard move: expected to ignore blank lines.
  4295. (org-test-with-temp-text "First paragraph.\n\n\nSecond paragraph."
  4296. (org-forward-element)
  4297. (should (looking-at (regexp-quote "Second paragraph."))))
  4298. ;; 3. Headline tests.
  4299. (org-test-with-temp-text "
  4300. * Head 1
  4301. ** Head 1.1
  4302. *** Head 1.1.1
  4303. ** Head 1.2"
  4304. ;; 3.1. At an headline beginning: move to next headline at the
  4305. ;; same level.
  4306. (goto-line 3)
  4307. (org-forward-element)
  4308. (should (looking-at (regexp-quote "** Head 1.2")))
  4309. ;; 3.2. At an headline beginning: move to parent headline if no
  4310. ;; headline at the same level.
  4311. (goto-line 3)
  4312. (org-forward-element)
  4313. (should (looking-at (regexp-quote "** Head 1.2"))))
  4314. ;; 4. Greater element tests.
  4315. (org-test-with-temp-text
  4316. "#+BEGIN_CENTER\nInside.\n#+END_CENTER\n\nOutside."
  4317. ;; 4.1. At a greater element: expected to skip contents.
  4318. (org-forward-element)
  4319. (should (looking-at (regexp-quote "Outside.")))
  4320. ;; 4.2. At the end of greater element contents: expected to skip
  4321. ;; to the end of the greater element.
  4322. (goto-line 2)
  4323. (org-forward-element)
  4324. (should (looking-at (regexp-quote "Outside."))))
  4325. ;; 5. List tests.
  4326. (org-test-with-temp-text "
  4327. - item1
  4328. - sub1
  4329. - sub2
  4330. - sub3
  4331. Inner paragraph.
  4332. - item2
  4333. Outside."
  4334. ;; 5.1. At list top point: expected to move to the element after
  4335. ;; the list.
  4336. (goto-line 2)
  4337. (org-forward-element)
  4338. (should (looking-at (regexp-quote "Outside.")))
  4339. ;; 5.2. Special case: at the first line of a sub-list, but not at
  4340. ;; beginning of line, move to next item.
  4341. (goto-line 2)
  4342. (forward-char)
  4343. (org-forward-element)
  4344. (should (looking-at "- item2"))
  4345. (goto-line 4)
  4346. (forward-char)
  4347. (org-forward-element)
  4348. (should (looking-at " - sub2"))
  4349. ;; 5.3 At sub-list beginning: expected to move after the sub-list.
  4350. (goto-line 4)
  4351. (org-forward-element)
  4352. (should (looking-at (regexp-quote " Inner paragraph.")))
  4353. ;; 5.4. At sub-list end: expected to move outside the sub-list.
  4354. (goto-line 8)
  4355. (org-forward-element)
  4356. (should (looking-at (regexp-quote " Inner paragraph.")))
  4357. ;; 5.5. At an item: expected to move to next item, if any.
  4358. (goto-line 6)
  4359. (org-forward-element)
  4360. (should (looking-at " - sub3"))))
  4361. (ert-deftest test-org/backward-element ()
  4362. "Test `org-backward-element' specifications."
  4363. ;; 1. Should error at BOB.
  4364. (org-test-with-temp-text " \nParagraph."
  4365. (should-error (org-backward-element)))
  4366. ;; 2. Should move at BOB when called on the first element in buffer.
  4367. (should
  4368. (org-test-with-temp-text "\n#+TITLE: test"
  4369. (progn (forward-line)
  4370. (org-backward-element)
  4371. (bobp))))
  4372. ;; 3. Not at the beginning of an element: move at its beginning.
  4373. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  4374. (goto-line 3)
  4375. (end-of-line)
  4376. (org-backward-element)
  4377. (should (looking-at (regexp-quote "Paragraph2."))))
  4378. ;; 4. Headline tests.
  4379. (org-test-with-temp-text "
  4380. * Head 1
  4381. ** Head 1.1
  4382. *** Head 1.1.1
  4383. ** Head 1.2"
  4384. ;; 4.1. At an headline beginning: move to previous headline at the
  4385. ;; same level.
  4386. (goto-line 5)
  4387. (org-backward-element)
  4388. (should (looking-at (regexp-quote "** Head 1.1")))
  4389. ;; 4.2. At an headline beginning: move to parent headline if no
  4390. ;; headline at the same level.
  4391. (goto-line 3)
  4392. (org-backward-element)
  4393. (should (looking-at (regexp-quote "* Head 1")))
  4394. ;; 4.3. At the first top-level headline: should error.
  4395. (goto-line 2)
  4396. (should-error (org-backward-element)))
  4397. ;; 5. At beginning of first element inside a greater element:
  4398. ;; expected to move to greater element's beginning.
  4399. (org-test-with-temp-text "Before.\n#+BEGIN_CENTER\nInside.\n#+END_CENTER"
  4400. (goto-line 3)
  4401. (org-backward-element)
  4402. (should (looking-at "#\\+BEGIN_CENTER")))
  4403. ;; 6. At the beginning of the first element in a section: should
  4404. ;; move back to headline, if any.
  4405. (should
  4406. (org-test-with-temp-text "#+TITLE: test\n* Headline\n\nParagraph"
  4407. (progn (goto-char (point-max))
  4408. (beginning-of-line)
  4409. (org-backward-element)
  4410. (org-at-heading-p))))
  4411. ;; 7. List tests.
  4412. (org-test-with-temp-text "
  4413. - item1
  4414. - sub1
  4415. - sub2
  4416. - sub3
  4417. Inner paragraph.
  4418. - item2
  4419. Outside."
  4420. ;; 7.1. At beginning of sub-list: expected to move to the
  4421. ;; paragraph before it.
  4422. (goto-line 4)
  4423. (org-backward-element)
  4424. (should (looking-at "item1"))
  4425. ;; 7.2. At an item in a list: expected to move at previous item.
  4426. (goto-line 8)
  4427. (org-backward-element)
  4428. (should (looking-at " - sub2"))
  4429. (goto-line 12)
  4430. (org-backward-element)
  4431. (should (looking-at "- item1"))
  4432. ;; 7.3. At end of list/sub-list: expected to move to list/sub-list
  4433. ;; beginning.
  4434. (goto-line 10)
  4435. (org-backward-element)
  4436. (should (looking-at " - sub1"))
  4437. (goto-line 15)
  4438. (org-backward-element)
  4439. (should (looking-at "- item1"))
  4440. ;; 7.4. At blank-lines before list end: expected to move to top
  4441. ;; item.
  4442. (goto-line 14)
  4443. (org-backward-element)
  4444. (should (looking-at "- item1"))))
  4445. (ert-deftest test-org/up-element ()
  4446. "Test `org-up-element' specifications."
  4447. ;; 1. At BOB or with no surrounding element: should error.
  4448. (org-test-with-temp-text "Paragraph."
  4449. (should-error (org-up-element)))
  4450. (org-test-with-temp-text "* Head1\n* Head2"
  4451. (goto-line 2)
  4452. (should-error (org-up-element)))
  4453. (org-test-with-temp-text "Paragraph1.\n\nParagraph2."
  4454. (goto-line 3)
  4455. (should-error (org-up-element)))
  4456. ;; 2. At an headline: move to parent headline.
  4457. (org-test-with-temp-text "* Head1\n** Sub-Head1\n** Sub-Head2"
  4458. (goto-line 3)
  4459. (org-up-element)
  4460. (should (looking-at "\\* Head1")))
  4461. ;; 3. Inside a greater element: move to greater element beginning.
  4462. (org-test-with-temp-text
  4463. "Before.\n#+BEGIN_CENTER\nParagraph1\nParagraph2\n#+END_CENTER\n"
  4464. (goto-line 3)
  4465. (org-up-element)
  4466. (should (looking-at "#\\+BEGIN_CENTER")))
  4467. ;; 4. List tests.
  4468. (org-test-with-temp-text "* Top
  4469. - item1
  4470. - sub1
  4471. - sub2
  4472. Paragraph within sub2.
  4473. - item2"
  4474. ;; 4.1. Within an item: move to the item beginning.
  4475. (goto-line 8)
  4476. (org-up-element)
  4477. (should (looking-at " - sub2"))
  4478. ;; 4.2. At an item in a sub-list: move to parent item.
  4479. (goto-line 4)
  4480. (org-up-element)
  4481. (should (looking-at "- item1"))
  4482. ;; 4.3. At an item in top list: move to beginning of whole list.
  4483. (goto-line 10)
  4484. (org-up-element)
  4485. (should (looking-at "- item1"))
  4486. ;; 4.4. Special case. At very top point: should move to parent of
  4487. ;; list.
  4488. (goto-line 2)
  4489. (org-up-element)
  4490. (should (looking-at "\\* Top"))))
  4491. (ert-deftest test-org/down-element ()
  4492. "Test `org-down-element' specifications."
  4493. ;; Error when the element hasn't got a recursive type.
  4494. (org-test-with-temp-text "Paragraph."
  4495. (should-error (org-down-element)))
  4496. ;; Error when the element has no contents
  4497. (org-test-with-temp-text "* Headline"
  4498. (should-error (org-down-element)))
  4499. ;; When at a plain-list, move to first item.
  4500. (org-test-with-temp-text "- Item 1\n - Item 1.1\n - Item 2.2"
  4501. (goto-line 2)
  4502. (org-down-element)
  4503. (should (looking-at " - Item 1.1")))
  4504. (org-test-with-temp-text "#+NAME: list\n- Item 1"
  4505. (org-down-element)
  4506. (should (looking-at " Item 1")))
  4507. ;; When at a table, move to first row
  4508. (org-test-with-temp-text "#+NAME: table\n| a | b |"
  4509. (org-down-element)
  4510. (should (looking-at " a | b |")))
  4511. ;; Otherwise, move inside the greater element.
  4512. (org-test-with-temp-text "#+BEGIN_CENTER\nParagraph.\n#+END_CENTER"
  4513. (org-down-element)
  4514. (should (looking-at "Paragraph"))))
  4515. (ert-deftest test-org/drag-element-backward ()
  4516. "Test `org-drag-element-backward' specifications."
  4517. ;; Standard test.
  4518. (should
  4519. (equal
  4520. "#+key2: val2\n#+key1: val1\n#+key3: val3"
  4521. (org-test-with-temp-text "#+key1: val1\n<point>#+key2: val2\n#+key3: val3"
  4522. (org-drag-element-backward)
  4523. (buffer-string))))
  4524. (should
  4525. (equal
  4526. "#+BEGIN_CENTER\n#+B: 2\n#+A: 1\n#+END_CENTER"
  4527. (org-test-with-temp-text
  4528. "#+BEGIN_CENTER\n#+A: 1\n<point>#+B: 2\n#+END_CENTER"
  4529. (org-drag-element-backward)
  4530. (buffer-string))))
  4531. ;; Preserve blank lines.
  4532. (should
  4533. (equal "Paragraph 2\n\n\nPara1\n\nPara3"
  4534. (org-test-with-temp-text "Para1\n\n\n<point>Paragraph 2\n\nPara3"
  4535. (org-drag-element-backward)
  4536. (buffer-string))))
  4537. ;; Preserve column.
  4538. (should
  4539. (org-test-with-temp-text "#+key1: v\n#+key<point>2: v\n#+key3: v"
  4540. (org-drag-element-backward)
  4541. (looking-at-p "2")))
  4542. ;; Error when trying to move first element of buffer.
  4543. (should-error
  4544. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  4545. (org-drag-element-backward))
  4546. :type 'user-error)
  4547. ;; Error when trying to swap nested elements.
  4548. (should-error
  4549. (org-test-with-temp-text "#+BEGIN_CENTER\n<point>Test.\n#+END_CENTER"
  4550. (org-drag-element-backward))
  4551. :type 'user-error)
  4552. ;; Error when trying to swap an headline element and a non-headline
  4553. ;; element.
  4554. (should-error
  4555. (org-test-with-temp-text "Test.\n<point>* Head 1"
  4556. (org-drag-element-backward))
  4557. :type 'error)
  4558. ;; Error when called before first element.
  4559. (should-error
  4560. (org-test-with-temp-text "\n<point>"
  4561. (org-drag-element-backward))
  4562. :type 'user-error)
  4563. ;; Preserve visibility of elements and their contents.
  4564. (should
  4565. (equal '((63 . 82) (26 . 48))
  4566. (let ((org-fold-core-style 'text-properties))
  4567. (org-test-with-temp-text
  4568. "
  4569. #+BEGIN_CENTER
  4570. Text.
  4571. #+END_CENTER
  4572. - item 1
  4573. #+BEGIN_QUOTE
  4574. Text.
  4575. #+END_QUOTE"
  4576. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4577. (search-backward "- item 1")
  4578. (org-drag-element-backward)
  4579. (let (regions)
  4580. (goto-char (point-min))
  4581. (while (< (point) (point-max))
  4582. (let ((region (org-fold-get-region-at-point)))
  4583. (if (not region)
  4584. (goto-char (org-fold-next-folding-state-change))
  4585. (goto-char (cdr region))
  4586. (push region regions))))
  4587. regions)))))
  4588. (should
  4589. (equal '((26 . 48) (63 . 82))
  4590. (let ((org-fold-core-style 'overlays))
  4591. (org-test-with-temp-text
  4592. "
  4593. #+BEGIN_CENTER
  4594. Text.
  4595. #+END_CENTER
  4596. - item 1
  4597. #+BEGIN_QUOTE
  4598. Text.
  4599. #+END_QUOTE"
  4600. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4601. (search-backward "- item 1")
  4602. (org-drag-element-backward)
  4603. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  4604. (overlays-in (point-min) (point-max)))))))
  4605. ;; Pathological case: handle call with point in blank lines right
  4606. ;; after a headline.
  4607. (should
  4608. (equal "* H2\n\n* H1\nText\n"
  4609. (org-test-with-temp-text "* H1\nText\n* H2\n\n<point>"
  4610. (org-drag-element-backward)
  4611. (buffer-string)))))
  4612. (ert-deftest test-org/drag-element-forward ()
  4613. "Test `org-drag-element-forward' specifications."
  4614. ;; 1. Error when trying to move first element of buffer.
  4615. (org-test-with-temp-text "Paragraph 1.\n\nParagraph 2."
  4616. (goto-line 3)
  4617. (should-error (org-drag-element-forward)))
  4618. ;; 2. Error when trying to swap nested elements.
  4619. (org-test-with-temp-text "#+BEGIN_CENTER\nTest.\n#+END_CENTER"
  4620. (forward-line)
  4621. (should-error (org-drag-element-forward)))
  4622. ;; 3. Error when trying to swap a non-headline element and an
  4623. ;; headline.
  4624. (org-test-with-temp-text "Test.\n* Head 1"
  4625. (should-error (org-drag-element-forward)))
  4626. ;; 4. Error when called before first element.
  4627. (should-error
  4628. (org-test-with-temp-text "\n"
  4629. (forward-line)
  4630. (org-drag-element-backward))
  4631. :type 'user-error)
  4632. ;; 5. Otherwise, swap elements, preserving column and blank lines
  4633. ;; between elements.
  4634. (org-test-with-temp-text "Paragraph 1\n\n\nPara2\n\nPara3"
  4635. (search-forward "graph")
  4636. (org-drag-element-forward)
  4637. (should (equal (buffer-string) "Para2\n\n\nParagraph 1\n\nPara3"))
  4638. (should (looking-at " 1")))
  4639. ;; 5. Preserve visibility of elements and their contents.
  4640. (let ((org-fold-core-style 'text-properties))
  4641. (org-test-with-temp-text
  4642. "
  4643. #+BEGIN_CENTER
  4644. Text.
  4645. #+END_CENTER
  4646. - item 1
  4647. #+BEGIN_QUOTE
  4648. Text.
  4649. #+END_QUOTE"
  4650. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4651. (search-backward "#+BEGIN_CENTER")
  4652. (org-drag-element-forward)
  4653. (should
  4654. (equal
  4655. '((63 . 82) (26 . 48))
  4656. (let (regions)
  4657. (goto-char (point-min))
  4658. (while (< (point) (point-max))
  4659. (let ((region (org-fold-get-region-at-point)))
  4660. (if (not region)
  4661. (goto-char (org-fold-next-folding-state-change))
  4662. (goto-char (cdr region))
  4663. (push region regions))))
  4664. regions)))))
  4665. (let ((org-fold-core-style 'overlays))
  4666. (org-test-with-temp-text
  4667. "
  4668. #+BEGIN_CENTER
  4669. Text.
  4670. #+END_CENTER
  4671. - item 1
  4672. #+BEGIN_QUOTE
  4673. Text.
  4674. #+END_QUOTE"
  4675. (while (search-forward "BEGIN_" nil t) (org-cycle))
  4676. (search-backward "#+BEGIN_CENTER")
  4677. (org-drag-element-forward)
  4678. (should
  4679. (equal
  4680. '((26 . 48) (63 . 82))
  4681. (mapcar (lambda (ov) (cons (overlay-start ov) (overlay-end ov)))
  4682. (overlays-in (point-min) (point-max))))))))
  4683. (ert-deftest test-org/next-block ()
  4684. "Test `org-next-block' specifications."
  4685. ;; Regular test.
  4686. (should
  4687. (org-test-with-temp-text "Paragraph\n#+BEGIN_CENTER\ncontents\n#+END_CENTER"
  4688. (org-next-block 1)
  4689. (looking-at "#\\+BEGIN_CENTER")))
  4690. ;; Ignore case.
  4691. (should
  4692. (org-test-with-temp-text "Paragraph\n#+begin_center\ncontents\n#+end_center"
  4693. (let ((case-fold-search nil))
  4694. (org-next-block 1)
  4695. (looking-at "#\\+begin_center"))))
  4696. ;; Ignore current line.
  4697. (should
  4698. (org-test-with-temp-text
  4699. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER\n#+END_CENTER"
  4700. (org-next-block 1)
  4701. (looking-at "#\\+BEGIN_CENTER")))
  4702. ;; Throw an error when no block is found.
  4703. (should-error
  4704. (org-test-with-temp-text "Paragraph"
  4705. (org-next-block 1)))
  4706. ;; With an argument, skip many blocks at once.
  4707. (should
  4708. (org-test-with-temp-text
  4709. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  4710. (org-next-block 2)
  4711. (looking-at "#\\+BEGIN_QUOTE")))
  4712. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4713. (should
  4714. (org-test-with-temp-text
  4715. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE"
  4716. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  4717. (looking-at "#\\+BEGIN_QUOTE")))
  4718. ;; Optional argument is also case-insensitive.
  4719. (should
  4720. (org-test-with-temp-text
  4721. "Start\n#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote"
  4722. (let ((case-fold-search nil))
  4723. (org-next-block 1 nil "^[ \t]*#\\+BEGIN_QUOTE")
  4724. (looking-at "#\\+begin_quote")))))
  4725. (ert-deftest test-org/insert-structure-template ()
  4726. "Test `org-insert-structure-template'."
  4727. ;; Test in empty buffer.
  4728. (should
  4729. (string= "#+begin_foo\n#+end_foo\n"
  4730. (org-test-with-temp-text ""
  4731. (org-insert-structure-template "foo")
  4732. (buffer-string))))
  4733. ;; Test with multiple lines in buffer.
  4734. (should
  4735. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4736. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4737. (transient-mark-mode 1)
  4738. (org-mark-element)
  4739. (org-insert-structure-template "foo")
  4740. (buffer-string))))
  4741. ;; Mark only the current line.
  4742. (should
  4743. (string= "#+begin_foo\nI'm a paragraph\n#+end_foo\n\nI'm a second paragraph"
  4744. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragraph"
  4745. (transient-mark-mode 1)
  4746. (set-mark (point-min))
  4747. (end-of-line)
  4748. (org-insert-structure-template "foo")
  4749. (buffer-string))))
  4750. ;; Middle of paragraph.
  4751. (should
  4752. (string= "p1\n#+begin_foo\np2\n#+end_foo\np3"
  4753. (org-test-with-temp-text "p1\n<point>p2\np3"
  4754. (set-mark (line-beginning-position))
  4755. (end-of-line)
  4756. (activate-mark)
  4757. (org-insert-structure-template "foo")
  4758. (buffer-string))))
  4759. ;; Test with text in buffer, no region, no final newline.
  4760. (should
  4761. (string= "#+begin_foo\nI'm a paragraph.\n#+end_foo\n"
  4762. (org-test-with-temp-text "I'm a paragraph."
  4763. (org-mark-element)
  4764. (org-insert-structure-template "foo")
  4765. (buffer-string))))
  4766. ;; Test with text in buffer and region set.
  4767. (should
  4768. (string= "#+begin_foo\nI'm a paragraph\n\nI'm a second paragrah\n#+end_foo\n"
  4769. (org-test-with-temp-text "I'm a paragraph\n\nI'm a second paragrah"
  4770. (set-mark (point))
  4771. (goto-char (point-max))
  4772. (org-insert-structure-template "foo")
  4773. (buffer-string))))
  4774. ;; Test with example escaping.
  4775. (should
  4776. (string= "#+begin_example\n,* Heading\n#+end_example\n"
  4777. (org-test-with-temp-text "* Heading"
  4778. (org-mark-element)
  4779. (org-insert-structure-template "example")
  4780. (buffer-string))))
  4781. ;; Test with indentation.
  4782. (should
  4783. (string= " #+begin_foo\n This is a paragraph\n #+end_foo\n"
  4784. (org-test-with-temp-text " This is a paragraph"
  4785. (org-mark-element)
  4786. (org-insert-structure-template "foo")
  4787. (buffer-string))))
  4788. (should
  4789. (string= " #+begin_foo\n Line 1\n Line2\n #+end_foo\n"
  4790. (org-test-with-temp-text " Line 1\n Line2"
  4791. (org-mark-element)
  4792. (org-insert-structure-template "foo")
  4793. (buffer-string))))
  4794. ;; Test point location.
  4795. (should
  4796. (string= "#+begin_foo\n"
  4797. (org-test-with-temp-text ""
  4798. (org-insert-structure-template "foo")
  4799. (buffer-substring (point-min) (point)))))
  4800. (should
  4801. (string= "#+begin_src "
  4802. (org-test-with-temp-text ""
  4803. (org-insert-structure-template "src")
  4804. (buffer-substring (point-min) (point))))))
  4805. (ert-deftest test-org/previous-block ()
  4806. "Test `org-previous-block' specifications."
  4807. ;; Regular test.
  4808. (should
  4809. (org-test-with-temp-text "#+BEGIN_CENTER\ncontents\n#+END_CENTER\n<point>"
  4810. (org-previous-block 1)
  4811. (looking-at "#\\+BEGIN_CENTER")))
  4812. ;; Ignore case.
  4813. (should
  4814. (org-test-with-temp-text "#+begin_center\ncontents\n#+end_center\n<point>"
  4815. (let ((case-fold-search nil))
  4816. (org-previous-block 1)
  4817. (looking-at "#\\+begin_center"))))
  4818. ;; Ignore current line.
  4819. (should
  4820. (org-test-with-temp-text
  4821. "#+BEGIN_QUOTE\n#+END_QUOTE\n#+BEGIN_CENTER<point>\n#+END_CENTER"
  4822. (org-previous-block 1)
  4823. (looking-at "#\\+BEGIN_QUOTE")))
  4824. ;; Throw an error when no block is found.
  4825. (should-error
  4826. (org-test-with-temp-text "Paragraph<point>"
  4827. (org-previous-block 1)))
  4828. ;; With an argument, skip many blocks at once.
  4829. (should
  4830. (org-test-with-temp-text
  4831. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4832. (org-previous-block 2)
  4833. (looking-at "#\\+BEGIN_CENTER")))
  4834. ;; With optional argument BLOCK-REGEXP, filter matched blocks.
  4835. (should
  4836. (org-test-with-temp-text
  4837. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+BEGIN_QUOTE\nB\n#+END_QUOTE\n<point>"
  4838. (org-previous-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4839. (looking-at "#\\+BEGIN_QUOTE")))
  4840. ;; Optional argument is also case-insensitive.
  4841. (should
  4842. (org-test-with-temp-text
  4843. "#+BEGIN_CENTER\nA\n#+END_CENTER\n#+begin_quote\nB\n#+end_quote\n<point>"
  4844. (let ((case-fold-search nil))
  4845. (org-next-block 1 "^[ \t]*#\\+BEGIN_QUOTE")
  4846. (looking-at "#\\+begin_quote")))))
  4847. ;;; Outline structure
  4848. (ert-deftest test-org/demote ()
  4849. "Test `org-demote' specifications."
  4850. ;; Add correct number of stars according to `org-odd-levels-only'.
  4851. (should
  4852. (= 2
  4853. (org-test-with-temp-text "* H"
  4854. (let ((org-odd-levels-only nil)) (org-demote))
  4855. (org-current-level))))
  4856. (should
  4857. (= 3
  4858. (org-test-with-temp-text "* H"
  4859. (let ((org-odd-levels-only t)) (org-demote))
  4860. (org-current-level))))
  4861. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  4862. (should
  4863. (org-test-with-temp-text "* H :tag:"
  4864. (let ((org-tags-column 10)
  4865. (org-auto-align-tags t)
  4866. (org-odd-levels-only nil))
  4867. (org-demote))
  4868. (org-move-to-column 10)
  4869. (looking-at-p ":tag:$")))
  4870. (should-not
  4871. (org-test-with-temp-text "* H :tag:"
  4872. (let ((org-tags-column 10)
  4873. (org-auto-align-tags nil)
  4874. (org-odd-levels-only nil))
  4875. (org-demote))
  4876. (org-move-to-column 10)
  4877. (looking-at-p ":tag:$")))
  4878. ;; When `org-adapt-indentation' is non-nil, always indent planning
  4879. ;; info and property drawers accordingly.
  4880. (should
  4881. (= 3
  4882. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4883. (let ((org-odd-levels-only nil)
  4884. (org-adapt-indentation t))
  4885. (org-demote))
  4886. (forward-line)
  4887. (org-get-indentation))))
  4888. (should
  4889. (= 3
  4890. (org-test-with-temp-text "* H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  4891. (let ((org-odd-levels-only nil)
  4892. (org-adapt-indentation t))
  4893. (org-demote))
  4894. (forward-line)
  4895. (org-get-indentation))))
  4896. (should-not
  4897. (= 3
  4898. (org-test-with-temp-text "* H\n SCHEDULED: <2014-03-04 tue.>"
  4899. (let ((org-odd-levels-only nil)
  4900. (org-adapt-indentation nil))
  4901. (org-demote))
  4902. (forward-line)
  4903. (org-get-indentation))))
  4904. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  4905. ;; section accordingly. Ignore, however, footnote definitions and
  4906. ;; inlinetasks boundaries.
  4907. (should
  4908. (= 3
  4909. (org-test-with-temp-text "* H\n Paragraph"
  4910. (let ((org-odd-levels-only nil)
  4911. (org-adapt-indentation t))
  4912. (org-demote))
  4913. (forward-line)
  4914. (org-get-indentation))))
  4915. (should
  4916. (= 2
  4917. (org-test-with-temp-text "* H\n Paragraph"
  4918. (let ((org-odd-levels-only nil)
  4919. (org-adapt-indentation nil))
  4920. (org-demote))
  4921. (forward-line)
  4922. (org-get-indentation))))
  4923. (should
  4924. (zerop
  4925. (org-test-with-temp-text "* H\n[fn:1] def line 1\ndef line 2"
  4926. (let ((org-odd-levels-only nil)
  4927. (org-adapt-indentation t))
  4928. (org-demote))
  4929. (goto-char (point-max))
  4930. (org-get-indentation))))
  4931. (should
  4932. (= 3
  4933. (org-test-with-temp-text "* H\n[fn:1] Def.\n\n\n After def."
  4934. (let ((org-odd-levels-only nil)
  4935. (org-adapt-indentation t))
  4936. (org-demote))
  4937. (goto-char (point-max))
  4938. (org-get-indentation))))
  4939. (when (featurep 'org-inlinetask)
  4940. (should
  4941. (zerop
  4942. (let ((org-inlinetask-min-level 5)
  4943. (org-adapt-indentation t))
  4944. (org-test-with-temp-text "* H\n***** I\n***** END"
  4945. (org-demote)
  4946. (forward-line)
  4947. (org-get-indentation))))))
  4948. (when (featurep 'org-inlinetask)
  4949. (should
  4950. (= 3
  4951. (let ((org-inlinetask-min-level 5)
  4952. (org-adapt-indentation t))
  4953. (org-test-with-temp-text "* H\n***** I\n Contents\n***** END"
  4954. (org-demote)
  4955. (forward-line 2)
  4956. (org-get-indentation))))))
  4957. ;; When `org-adapt-indentation' is non-nil, log drawers are
  4958. ;; adjusted.
  4959. (should
  4960. (equal
  4961. "** H\n :LOGBOOK:\n - a\n :END:\n b"
  4962. (org-test-with-temp-text "* H\n :LOGBOOK:\n - a\n :END:\n b"
  4963. (let ((org-odd-levels-only nil)
  4964. (org-adapt-indentation t))
  4965. (org-demote))
  4966. (buffer-string))))
  4967. (should
  4968. (equal
  4969. "** H\n :LOGBOOK:\n - a\n :END:\n b"
  4970. (org-test-with-temp-text "* H\n :LOGBOOK:\n - a\n :END:\n b"
  4971. (let ((org-odd-levels-only nil)
  4972. (org-adapt-indentation 'headline-data))
  4973. (org-demote))
  4974. (buffer-string))))
  4975. (should
  4976. (equal
  4977. "** H\n :LOGBOOK:\n - a\n :END:"
  4978. (org-test-with-temp-text "* H\n:LOGBOOK:\n- a\n:END:"
  4979. (let ((org-odd-levels-only nil)
  4980. (org-adapt-indentation t))
  4981. (org-demote))
  4982. (buffer-string))))
  4983. ;; Ignore contents of source blocks or example blocks when
  4984. ;; indentation should be preserved (through
  4985. ;; `org-src-preserve-indentation' or "-i" flag).
  4986. (should-not
  4987. (zerop
  4988. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  4989. (let ((org-adapt-indentation t)
  4990. (org-src-preserve-indentation nil))
  4991. (org-demote))
  4992. (forward-line 2)
  4993. (org-get-indentation))))
  4994. (should
  4995. (zerop
  4996. (org-test-with-temp-text "* H\n#+BEGIN_EXAMPLE\n(+ 1 1)\n#+END_EXAMPLE"
  4997. (let ((org-adapt-indentation t)
  4998. (org-src-preserve-indentation t))
  4999. (org-demote))
  5000. (forward-line 2)
  5001. (org-get-indentation))))
  5002. (should
  5003. (zerop
  5004. (org-test-with-temp-text "* H\n#+BEGIN_SRC emacs-lisp\n(+ 1 1)\n#+END_SRC"
  5005. (let ((org-adapt-indentation t)
  5006. (org-src-preserve-indentation t))
  5007. (org-demote))
  5008. (forward-line 2)
  5009. (org-get-indentation))))
  5010. (should
  5011. (zerop
  5012. (org-test-with-temp-text
  5013. "* H\n#+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n#+END_SRC"
  5014. (let ((org-adapt-indentation t)
  5015. (org-src-preserve-indentation nil))
  5016. (org-demote))
  5017. (forward-line 2)
  5018. (org-get-indentation)))))
  5019. (ert-deftest test-org/promote ()
  5020. "Test `org-promote' specifications."
  5021. ;; Return an error if headline is to be promoted to level 0, unless
  5022. ;; `org-allow-promoting-top-level-subtree' is non-nil, in which case
  5023. ;; headline becomes a comment.
  5024. (should-error
  5025. (org-test-with-temp-text "* H"
  5026. (let ((org-allow-promoting-top-level-subtree nil)) (org-promote))))
  5027. (should
  5028. (equal "# H"
  5029. (org-test-with-temp-text "* H"
  5030. (let ((org-allow-promoting-top-level-subtree t)) (org-promote))
  5031. (buffer-string))))
  5032. ;; Remove correct number of stars according to
  5033. ;; `org-odd-levels-only'.
  5034. (should
  5035. (= 2
  5036. (org-test-with-temp-text "*** H"
  5037. (let ((org-odd-levels-only nil)) (org-promote))
  5038. (org-current-level))))
  5039. (should
  5040. (= 1
  5041. (org-test-with-temp-text "*** H"
  5042. (let ((org-odd-levels-only t)) (org-promote))
  5043. (org-current-level))))
  5044. ;; When `org-auto-align-tags' is non-nil, move tags accordingly.
  5045. (should
  5046. (org-test-with-temp-text "** H :tag:"
  5047. (let ((org-tags-column 10)
  5048. (org-auto-align-tags t)
  5049. (org-odd-levels-only nil))
  5050. (org-promote))
  5051. (org-move-to-column 10)
  5052. (looking-at-p ":tag:$")))
  5053. (should-not
  5054. (org-test-with-temp-text "** H :tag:"
  5055. (let ((org-tags-column 10)
  5056. (org-auto-align-tags nil)
  5057. (org-odd-levels-only nil))
  5058. (org-promote))
  5059. (org-move-to-column 10)
  5060. (looking-at-p ":tag:$")))
  5061. ;; When `org-adapt-indentation' is non-nil, always indent planning
  5062. ;; info and property drawers.
  5063. (should
  5064. (= 2
  5065. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  5066. (let ((org-odd-levels-only nil)
  5067. (org-adapt-indentation t))
  5068. (org-promote))
  5069. (forward-line)
  5070. (org-get-indentation))))
  5071. (should
  5072. (= 2
  5073. (org-test-with-temp-text "** H\n :PROPERTIES:\n :FOO: Bar\n :END:"
  5074. (let ((org-odd-levels-only nil)
  5075. (org-adapt-indentation t))
  5076. (org-promote))
  5077. (forward-line)
  5078. (org-get-indentation))))
  5079. (should-not
  5080. (= 2
  5081. (org-test-with-temp-text "** H\n SCHEDULED: <2014-03-04 tue.>"
  5082. (let ((org-odd-levels-only nil)
  5083. (org-adapt-indentation nil))
  5084. (org-promote))
  5085. (forward-line)
  5086. (org-get-indentation))))
  5087. ;; When `org-adapt-indentation' is non-nil, shift all lines in
  5088. ;; section accordingly. Ignore, however, footnote definitions and
  5089. ;; inlinetasks boundaries.
  5090. (should
  5091. (= 2
  5092. (org-test-with-temp-text "** H\n Paragraph"
  5093. (let ((org-odd-levels-only nil)
  5094. (org-adapt-indentation t))
  5095. (org-promote))
  5096. (forward-line)
  5097. (org-get-indentation))))
  5098. (should-not
  5099. (= 2
  5100. (org-test-with-temp-text "** H\n Paragraph"
  5101. (let ((org-odd-levels-only nil)
  5102. (org-adapt-indentation nil))
  5103. (org-promote))
  5104. (forward-line)
  5105. (org-get-indentation))))
  5106. (should
  5107. (= 2
  5108. (org-test-with-temp-text "** H\n Paragraph\n[fn:1] line1\nline2"
  5109. (let ((org-odd-levels-only nil)
  5110. (org-adapt-indentation t))
  5111. (org-promote))
  5112. (forward-line)
  5113. (org-get-indentation))))
  5114. (when (featurep 'org-inlinetask)
  5115. (should
  5116. (zerop
  5117. (let ((org-inlinetask-min-level 5)
  5118. (org-adapt-indentation t))
  5119. (org-test-with-temp-text "** H\n***** I\n***** END"
  5120. (org-promote)
  5121. (forward-line)
  5122. (org-get-indentation))))))
  5123. (when (featurep 'org-inlinetask)
  5124. (should
  5125. (= 2
  5126. (let ((org-inlinetask-min-level 5)
  5127. (org-adapt-indentation t))
  5128. (org-test-with-temp-text "** H\n***** I\n Contents\n***** END"
  5129. (org-promote)
  5130. (forward-line 2)
  5131. (org-get-indentation))))))
  5132. ;; Give up shifting if it would break document's structure
  5133. ;; otherwise.
  5134. (should
  5135. (= 3
  5136. (org-test-with-temp-text "** H\n Paragraph\n [fn:1] Def."
  5137. (let ((org-odd-levels-only nil)
  5138. (org-adapt-indentation t))
  5139. (org-promote))
  5140. (forward-line)
  5141. (org-get-indentation))))
  5142. (should
  5143. (= 3
  5144. (org-test-with-temp-text "** H\n Paragraph\n * list."
  5145. (let ((org-odd-levels-only nil)
  5146. (org-adapt-indentation t))
  5147. (org-promote))
  5148. (forward-line)
  5149. (org-get-indentation))))
  5150. ;; When `org-adapt-indentation' is non-nil, log drawers are
  5151. ;; adjusted.
  5152. (should
  5153. (equal
  5154. "* H\n :LOGBOOK:\n - a\n :END:\n b"
  5155. (org-test-with-temp-text "** H\n :LOGBOOK:\n - a\n :END:\n b"
  5156. (let ((org-odd-levels-only nil)
  5157. (org-adapt-indentation t))
  5158. (org-promote))
  5159. (buffer-string))))
  5160. (should
  5161. (equal
  5162. "* H\n :LOGBOOK:\n - a\n :END:\n b"
  5163. (org-test-with-temp-text "** H\n :LOGBOOK:\n - a\n :END:\n b"
  5164. (let ((org-odd-levels-only nil)
  5165. (org-adapt-indentation 'headline-data))
  5166. (org-promote))
  5167. (buffer-string))))
  5168. (should
  5169. (equal
  5170. "* H\n:LOGBOOK:\n- a\n:END:"
  5171. (org-test-with-temp-text "** H\n:LOGBOOK:\n- a\n:END:"
  5172. (let ((org-odd-levels-only nil)
  5173. (org-adapt-indentation t))
  5174. (org-promote))
  5175. (buffer-string))))
  5176. (should
  5177. (equal
  5178. "# H\n:LOGBOOK:\n- a\n:END:"
  5179. (org-test-with-temp-text "* H\n:LOGBOOK:\n- a\n:END:"
  5180. (let ((org-odd-levels-only nil)
  5181. (org-allow-promoting-top-level-subtree t)
  5182. (org-adapt-indentation t))
  5183. (org-promote))
  5184. (buffer-string))))
  5185. ;; Ignore contents of source blocks or example blocks when
  5186. ;; indentation should be preserved (through
  5187. ;; `org-src-preserve-indentation' or "-i" flag).
  5188. (should-not
  5189. (zerop
  5190. (org-test-with-temp-text
  5191. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  5192. (let ((org-adapt-indentation t)
  5193. (org-src-preserve-indentation nil)
  5194. (org-odd-levels-only nil))
  5195. (org-promote))
  5196. (forward-line)
  5197. (org-get-indentation))))
  5198. (should
  5199. (zerop
  5200. (org-test-with-temp-text
  5201. "** H\n #+BEGIN_EXAMPLE\nContents\n #+END_EXAMPLE"
  5202. (let ((org-adapt-indentation t)
  5203. (org-src-preserve-indentation t)
  5204. (org-odd-levels-only nil))
  5205. (org-promote))
  5206. (forward-line)
  5207. (org-get-indentation))))
  5208. (should
  5209. (zerop
  5210. (org-test-with-temp-text
  5211. "** H\n #+BEGIN_SRC emacs-lisp\n(+ 1 1)\n #+END_SRC"
  5212. (let ((org-adapt-indentation t)
  5213. (org-src-preserve-indentation t)
  5214. (org-odd-levels-only nil))
  5215. (org-promote))
  5216. (forward-line)
  5217. (org-get-indentation))))
  5218. (should
  5219. (zerop
  5220. (org-test-with-temp-text
  5221. "** H\n #+BEGIN_SRC emacs-lisp -i\n(+ 1 1)\n #+END_SRC"
  5222. (let ((org-adapt-indentation t)
  5223. (org-src-preserve-indentation nil)
  5224. (org-odd-levels-only nil))
  5225. (org-promote))
  5226. (forward-line)
  5227. (org-get-indentation)))))
  5228. (ert-deftest test-org/org-get-valid-level ()
  5229. "Test function `org-get-valid-level' specifications."
  5230. (let ((org-odd-levels-only nil))
  5231. (should (equal 1 (org-get-valid-level 0 0)))
  5232. (should (equal 1 (org-get-valid-level 0 1)))
  5233. (should (equal 2 (org-get-valid-level 0 2)))
  5234. (should (equal 3 (org-get-valid-level 0 3)))
  5235. (should (equal 1 (org-get-valid-level 1 0)))
  5236. (should (equal 2 (org-get-valid-level 1 1)))
  5237. (should (equal 23 (org-get-valid-level 1 22)))
  5238. (should (equal 1 (org-get-valid-level 1 -1)))
  5239. (should (equal 1 (org-get-valid-level 2 -1))))
  5240. (let ((org-odd-levels-only t))
  5241. (should (equal 1 (org-get-valid-level 0 0)))
  5242. (should (equal 1 (org-get-valid-level 0 1)))
  5243. (should (equal 3 (org-get-valid-level 0 2)))
  5244. (should (equal 5 (org-get-valid-level 0 3)))
  5245. (should (equal 1 (org-get-valid-level 1 0)))
  5246. (should (equal 3 (org-get-valid-level 1 1)))
  5247. (should (equal 3 (org-get-valid-level 2 1)))
  5248. (should (equal 5 (org-get-valid-level 3 1)))
  5249. (should (equal 5 (org-get-valid-level 4 1)))
  5250. (should (equal 43 (org-get-valid-level 1 21)))
  5251. (should (equal 1 (org-get-valid-level 1 -1)))
  5252. (should (equal 1 (org-get-valid-level 2 -1)))
  5253. (should (equal 1 (org-get-valid-level 3 -1)))
  5254. (should (equal 3 (org-get-valid-level 4 -1)))
  5255. (should (equal 3 (org-get-valid-level 5 -1)))))
  5256. ;;; Planning
  5257. (ert-deftest test-org/at-planning-p ()
  5258. "Test `org-at-planning-p' specifications."
  5259. ;; Regular test.
  5260. (should
  5261. (org-test-with-temp-text "* Headline\n<point>DEADLINE: <2014-03-04 tue.>"
  5262. (org-at-planning-p)))
  5263. (should-not
  5264. (org-test-with-temp-text "DEADLINE: <2014-03-04 tue.>"
  5265. (org-at-planning-p)))
  5266. ;; Correctly find planning attached to inlinetasks.
  5267. (when (featurep 'org-inlinetask)
  5268. (should
  5269. (org-test-with-temp-text
  5270. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>\n*** END"
  5271. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  5272. (should-not
  5273. (org-test-with-temp-text
  5274. "*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  5275. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  5276. (should-not
  5277. (org-test-with-temp-text
  5278. "* Headline\n*** Inlinetask\n<point>DEADLINE: <2014-03-04 tue.>"
  5279. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))
  5280. (should-not
  5281. (org-test-with-temp-text
  5282. "* Headline\n*** Inlinetask\n*** END\n<point>DEADLINE: <2014-03-04 tue.>"
  5283. (let ((org-inlinetask-min-level 3)) (org-at-planning-p))))))
  5284. (ert-deftest test-org/add-planning-info ()
  5285. "Test `org-add-planning-info'."
  5286. ;; Create deadline when `org-adapt-indentation' is non-nil.
  5287. (should
  5288. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5289. (org-test-with-temp-text "* H\nParagraph<point>"
  5290. (let ((org-adapt-indentation t))
  5291. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5292. (replace-regexp-in-string
  5293. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5294. nil nil 1))))
  5295. ;; Create deadline when `org-adapt-indentation' is nil.
  5296. (should
  5297. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  5298. (org-test-with-temp-text "* H\nParagraph<point>"
  5299. (let ((org-adapt-indentation nil))
  5300. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5301. (replace-regexp-in-string
  5302. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5303. nil nil 1))))
  5304. ;; Update deadline when `org-adapt-indentation' is non-nil.
  5305. (should
  5306. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5307. (org-test-with-temp-text "\
  5308. * H
  5309. DEADLINE: <2015-06-24 Wed>
  5310. Paragraph<point>"
  5311. (let ((org-adapt-indentation t))
  5312. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5313. (replace-regexp-in-string
  5314. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5315. nil nil 1))))
  5316. ;; Update deadline when `org-adapt-indentation' is nil.
  5317. (should
  5318. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  5319. (org-test-with-temp-text "\
  5320. * H
  5321. DEADLINE: <2015-06-24 Wed>
  5322. Paragraph<point>"
  5323. (let ((org-adapt-indentation nil))
  5324. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5325. (replace-regexp-in-string
  5326. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5327. nil nil 1))))
  5328. ;; Schedule when `org-adapt-indentation' is non-nil.
  5329. (should
  5330. (equal "* H\n SCHEDULED: <2015-06-25>\nParagraph"
  5331. (org-test-with-temp-text "* H\nParagraph<point>"
  5332. (let ((org-adapt-indentation t))
  5333. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  5334. (replace-regexp-in-string
  5335. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5336. nil nil 1))))
  5337. ;; Schedule when `org-adapt-indentation' is nil.
  5338. (should
  5339. (equal "* H\nSCHEDULED: <2015-06-25>\nParagraph"
  5340. (org-test-with-temp-text "* H\nParagraph<point>"
  5341. (let ((org-adapt-indentation nil))
  5342. (org-add-planning-info 'scheduled "<2015-06-25 Thu>"))
  5343. (replace-regexp-in-string
  5344. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5345. nil nil 1))))
  5346. ;; Add deadline when scheduled.
  5347. (should
  5348. (equal "\
  5349. * H
  5350. DEADLINE: <2015-06-25> SCHEDULED: <2015-06-24>
  5351. Paragraph"
  5352. (org-test-with-temp-text "\
  5353. * H
  5354. SCHEDULED: <2015-06-24 Wed>
  5355. Paragraph<point>"
  5356. (let ((org-adapt-indentation t))
  5357. (org-add-planning-info 'deadline "<2015-06-25 Thu>"))
  5358. (replace-regexp-in-string
  5359. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5360. nil nil 1))))
  5361. ;; Remove middle entry.
  5362. (should
  5363. (equal "\
  5364. * H
  5365. CLOSED: [2015-06-24] SCHEDULED: <2015-06-24>
  5366. Paragraph"
  5367. (org-test-with-temp-text "\
  5368. * H
  5369. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  5370. Paragraph<point>"
  5371. (let ((org-adapt-indentation t))
  5372. (org-add-planning-info nil nil 'deadline))
  5373. (replace-regexp-in-string
  5374. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  5375. nil nil 1))))
  5376. ;; Remove last entry and then middle entry (order should not
  5377. ;; matter).
  5378. (should
  5379. (equal "\
  5380. * H
  5381. CLOSED: [2015-06-24]
  5382. Paragraph"
  5383. (org-test-with-temp-text "\
  5384. * H
  5385. CLOSED: [2015-06-24 Wed] DEADLINE: <2015-06-25 Thu> SCHEDULED: <2015-06-24 Wed>
  5386. Paragraph<point>"
  5387. (let ((org-adapt-indentation t))
  5388. (org-add-planning-info nil nil 'scheduled 'deadline))
  5389. (replace-regexp-in-string
  5390. "\\( [.A-Za-z]+\\)[]>]" "" (buffer-string)
  5391. nil nil 1))))
  5392. ;; Remove closed when `org-adapt-indentation' is non-nil.
  5393. (should
  5394. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5395. (org-test-with-temp-text "\
  5396. * H
  5397. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  5398. Paragraph<point>"
  5399. (let ((org-adapt-indentation t))
  5400. (org-add-planning-info nil nil 'closed))
  5401. (replace-regexp-in-string
  5402. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5403. nil nil 1))))
  5404. (should
  5405. (equal "* H\n Paragraph"
  5406. (org-test-with-temp-text "\
  5407. * H
  5408. CLOSED: [2015-06-25 Thu]
  5409. Paragraph<point>"
  5410. (let ((org-adapt-indentation t))
  5411. (org-add-planning-info nil nil 'closed))
  5412. (replace-regexp-in-string
  5413. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5414. nil nil 1))))
  5415. ;; Remove closed when `org-adapt-indentation' is nil.
  5416. (should
  5417. (equal "* H\nDEADLINE: <2015-06-25>\nParagraph"
  5418. (org-test-with-temp-text "\
  5419. * H
  5420. CLOSED: [2015-06-25 Thu] DEADLINE: <2015-06-25 Thu>
  5421. Paragraph<point>"
  5422. (let ((org-adapt-indentation nil))
  5423. (org-add-planning-info nil nil 'closed))
  5424. (replace-regexp-in-string
  5425. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5426. nil nil 1))))
  5427. (should
  5428. (equal "* H\nParagraph"
  5429. (org-test-with-temp-text "\
  5430. * H
  5431. CLOSED: [2015-06-25 Thu]
  5432. Paragraph<point>"
  5433. (let ((org-adapt-indentation nil))
  5434. (org-add-planning-info nil nil 'closed))
  5435. (replace-regexp-in-string
  5436. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5437. nil nil 1))))
  5438. ;; Remove closed entry and delete empty line.
  5439. (should
  5440. (equal "\
  5441. * H
  5442. Paragraph"
  5443. (org-test-with-temp-text "\
  5444. * H
  5445. CLOSED: [2015-06-24 Wed]
  5446. Paragraph<point>"
  5447. (let ((org-adapt-indentation t))
  5448. (org-add-planning-info nil nil 'closed))
  5449. (replace-regexp-in-string
  5450. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5451. nil nil 1))))
  5452. ;; Remove one entry and update another.
  5453. (should
  5454. (equal "* H\n DEADLINE: <2015-06-25>\nParagraph"
  5455. (org-test-with-temp-text "\
  5456. * H
  5457. SCHEDULED: <2015-06-23 Tue> DEADLINE: <2015-06-24 Wed>
  5458. Paragraph<point>"
  5459. (let ((org-adapt-indentation t))
  5460. (org-add-planning-info 'deadline "<2015-06-25 Thu>" 'scheduled))
  5461. (replace-regexp-in-string
  5462. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5463. nil nil 1)))))
  5464. (ert-deftest test-org/deadline ()
  5465. "Test `org-deadline' specifications."
  5466. ;; Insert a new value or replace existing one.
  5467. (should
  5468. (equal "* H\nDEADLINE: <2012-03-29>"
  5469. (org-test-with-temp-text "* H"
  5470. (let ((org-adapt-indentation nil)
  5471. (org-last-inserted-timestamp nil))
  5472. (org-deadline nil "<2012-03-29 Tue>"))
  5473. (replace-regexp-in-string
  5474. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5475. nil nil 1))))
  5476. (should
  5477. (equal "* H\nDEADLINE: <2014-03-04>"
  5478. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  5479. (let ((org-adapt-indentation nil)
  5480. (org-last-inserted-timestamp nil))
  5481. (org-deadline nil "<2014-03-04 Thu>"))
  5482. (replace-regexp-in-string
  5483. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5484. nil nil 1))))
  5485. ;; Accept delta time, e.g., "+2d".
  5486. (should
  5487. (equal "* H\nDEADLINE: <2015-03-04>"
  5488. (org-test-at-time "2014-03-04"
  5489. (org-test-with-temp-text "* H"
  5490. (let ((org-adapt-indentation nil)
  5491. (org-last-inserted-timestamp nil))
  5492. (org-deadline nil "+1y"))
  5493. (replace-regexp-in-string
  5494. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  5495. ;; Preserve repeater.
  5496. (should
  5497. (equal "* H\nDEADLINE: <2012-03-29 +2y>"
  5498. (org-test-with-temp-text "* H"
  5499. (let ((org-adapt-indentation nil)
  5500. (org-last-inserted-timestamp nil))
  5501. (org-deadline nil "<2012-03-29 Tue +2y>"))
  5502. (replace-regexp-in-string
  5503. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5504. ;; Preserve warning period.
  5505. (should
  5506. (equal "* H\nDEADLINE: <2021-07-20 -1d>"
  5507. (org-test-with-temp-text "* H"
  5508. (let ((org-adapt-indentation nil)
  5509. (org-last-inserted-timestamp nil))
  5510. (org-deadline nil "<2021-07-20 Tue -1d>"))
  5511. (replace-regexp-in-string
  5512. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5513. (should
  5514. (equal "* H\nDEADLINE: <2021-07-20 +1m -3d>"
  5515. (org-test-with-temp-text "* H"
  5516. (let ((org-adapt-indentation nil)
  5517. (org-last-inserted-timestamp nil))
  5518. (org-deadline nil "<2021-07-20 Tue +1m -3d>"))
  5519. (replace-regexp-in-string
  5520. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5521. ;; Remove CLOSED keyword, if any.
  5522. (should
  5523. (equal "* H\nDEADLINE: <2012-03-29>"
  5524. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  5525. (let ((org-adapt-indentation nil)
  5526. (org-last-inserted-timestamp nil))
  5527. (org-deadline nil "<2012-03-29 Tue>"))
  5528. (replace-regexp-in-string
  5529. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5530. ;; With C-u argument, remove DEADLINE keyword.
  5531. (should
  5532. (equal "* H\n"
  5533. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  5534. (let ((org-adapt-indentation nil)
  5535. (org-last-inserted-timestamp nil))
  5536. (org-deadline '(4)))
  5537. (buffer-string))))
  5538. (should
  5539. (equal "* H"
  5540. (org-test-with-temp-text "* H"
  5541. (let ((org-adapt-indentation nil)
  5542. (org-last-inserted-timestamp nil))
  5543. (org-deadline '(4)))
  5544. (buffer-string))))
  5545. ;; With C-u C-u argument, prompt for a delay cookie.
  5546. (should
  5547. (equal "* H\nDEADLINE: <2012-03-29 -705d>"
  5548. (cl-letf (((symbol-function 'org-read-date)
  5549. (lambda (&rest _args)
  5550. (org-time-string-to-time "2014-03-04"))))
  5551. (org-test-with-temp-text "* H\nDEADLINE: <2012-03-29>"
  5552. (let ((org-adapt-indentation nil)
  5553. (org-last-inserted-timestamp nil))
  5554. (org-deadline '(16)))
  5555. (buffer-string)))))
  5556. (should-error
  5557. (cl-letf (((symbol-function 'org-read-date)
  5558. (lambda (&rest _args)
  5559. (org-time-string-to-time "2014-03-04"))))
  5560. (org-test-with-temp-text "* H"
  5561. (let ((org-adapt-indentation nil)
  5562. (org-last-inserted-timestamp nil))
  5563. (org-deadline '(16)))
  5564. (buffer-string))))
  5565. ;; When a region is active and
  5566. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  5567. ;; same value in all headlines in region.
  5568. (should
  5569. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  5570. (org-test-with-temp-text "* H1\n* H2"
  5571. (let ((org-adapt-indentation nil)
  5572. (org-last-inserted-timestamp nil)
  5573. (org-loop-over-headlines-in-active-region t))
  5574. (transient-mark-mode 1)
  5575. (push-mark (point) t t)
  5576. (goto-char (point-max))
  5577. (org-deadline nil "2012-03-29"))
  5578. (replace-regexp-in-string
  5579. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5580. (should-not
  5581. (equal "* H1\nDEADLINE: <2012-03-29>\n* H2\nDEADLINE: <2012-03-29>"
  5582. (org-test-with-temp-text "* H1\n* H2"
  5583. (let ((org-adapt-indentation nil)
  5584. (org-last-inserted-timestamp nil)
  5585. (org-loop-over-headlines-in-active-region nil))
  5586. (transient-mark-mode 1)
  5587. (push-mark (point) t t)
  5588. (goto-char (point-max))
  5589. (org-deadline nil "2012-03-29"))
  5590. (replace-regexp-in-string
  5591. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  5592. (ert-deftest test-org/schedule ()
  5593. "Test `org-schedule' specifications."
  5594. ;; Insert a new value or replace existing one.
  5595. (should
  5596. (equal "* H\nSCHEDULED: <2012-03-29>"
  5597. (org-test-with-temp-text "* H"
  5598. (let ((org-adapt-indentation nil)
  5599. (org-last-inserted-timestamp nil))
  5600. (org-schedule nil "<2012-03-29 Tue>"))
  5601. (replace-regexp-in-string
  5602. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5603. nil nil 1))))
  5604. (should
  5605. (equal "* H\nSCHEDULED: <2014-03-04>"
  5606. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  5607. (let ((org-adapt-indentation nil)
  5608. (org-last-inserted-timestamp nil))
  5609. (org-schedule nil "<2014-03-04 Thu>"))
  5610. (replace-regexp-in-string
  5611. "\\( [.A-Za-z]+\\)>" "" (buffer-string)
  5612. nil nil 1))))
  5613. ;; Accept delta time, e.g., "+2d".
  5614. (should
  5615. (equal "* H\nSCHEDULED: <2015-03-04>"
  5616. (org-test-at-time "2014-03-04"
  5617. (org-test-with-temp-text "* H"
  5618. (let ((org-adapt-indentation nil)
  5619. (org-last-inserted-timestamp nil))
  5620. (org-schedule nil "+1y"))
  5621. (replace-regexp-in-string
  5622. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1)))))
  5623. ;; Preserve repeater.
  5624. (should
  5625. (equal "* H\nSCHEDULED: <2012-03-29 +2y>"
  5626. (org-test-with-temp-text "* H"
  5627. (let ((org-adapt-indentation nil)
  5628. (org-last-inserted-timestamp nil))
  5629. (org-schedule nil "<2012-03-29 Tue +2y>"))
  5630. (replace-regexp-in-string
  5631. "\\( [.A-Za-z]+\\) " "" (buffer-string) nil nil 1))))
  5632. ;; Remove CLOSED keyword, if any.
  5633. (should
  5634. (equal "* H\nSCHEDULED: <2012-03-29>"
  5635. (org-test-with-temp-text "* H\nCLOSED: [2017-01-25 Wed]"
  5636. (let ((org-adapt-indentation nil)
  5637. (org-last-inserted-timestamp nil))
  5638. (org-schedule nil "<2012-03-29 Tue>"))
  5639. (replace-regexp-in-string
  5640. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5641. ;; With C-u argument, remove SCHEDULED keyword.
  5642. (should
  5643. (equal "* H\n"
  5644. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  5645. (let ((org-adapt-indentation nil)
  5646. (org-last-inserted-timestamp nil))
  5647. (org-schedule '(4)))
  5648. (buffer-string))))
  5649. (should
  5650. (equal "* H"
  5651. (org-test-with-temp-text "* H"
  5652. (let ((org-adapt-indentation nil)
  5653. (org-last-inserted-timestamp nil))
  5654. (org-schedule '(4)))
  5655. (buffer-string))))
  5656. ;; With C-u C-u argument, prompt for a delay cookie.
  5657. (should
  5658. (equal "* H\nSCHEDULED: <2012-03-29 -705d>"
  5659. (cl-letf (((symbol-function 'org-read-date)
  5660. (lambda (&rest _args)
  5661. (org-time-string-to-time "2014-03-04"))))
  5662. (org-test-with-temp-text "* H\nSCHEDULED: <2012-03-29>"
  5663. (let ((org-adapt-indentation nil)
  5664. (org-last-inserted-timestamp nil))
  5665. (org-schedule '(16)))
  5666. (buffer-string)))))
  5667. (should-error
  5668. (cl-letf (((symbol-function 'org-read-date)
  5669. (lambda (&rest _args)
  5670. (org-time-string-to-time "2014-03-04"))))
  5671. (org-test-with-temp-text "* H"
  5672. (let ((org-adapt-indentation nil)
  5673. (org-last-inserted-timestamp nil))
  5674. (org-schedule '(16)))
  5675. (buffer-string))))
  5676. ;; When a region is active and
  5677. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  5678. ;; same value in all headlines in region.
  5679. (should
  5680. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  5681. (org-test-with-temp-text "* H1\n* H2"
  5682. (let ((org-adapt-indentation nil)
  5683. (org-last-inserted-timestamp nil)
  5684. (org-loop-over-headlines-in-active-region t))
  5685. (transient-mark-mode 1)
  5686. (push-mark (point) t t)
  5687. (goto-char (point-max))
  5688. (org-schedule nil "2012-03-29"))
  5689. (replace-regexp-in-string
  5690. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5691. (should-not
  5692. (equal "* H1\nSCHEDULED: <2012-03-29>\n* H2\nSCHEDULED: <2012-03-29>"
  5693. (org-test-with-temp-text "* H1\n* H2"
  5694. (let ((org-adapt-indentation nil)
  5695. (org-last-inserted-timestamp nil)
  5696. (org-loop-over-headlines-in-active-region nil))
  5697. (transient-mark-mode 1)
  5698. (push-mark (point) t t)
  5699. (goto-char (point-max))
  5700. (org-schedule nil "2012-03-29"))
  5701. (replace-regexp-in-string
  5702. "\\( [.A-Za-z]+\\)>" "" (buffer-string) nil nil 1))))
  5703. (should
  5704. ;; check if a repeater survives re-scheduling.
  5705. (string-match-p
  5706. "\\* H\nSCHEDULED: <2017-02-01 [.A-Za-z]* \\+\\+7d>\n"
  5707. (org-test-with-temp-text "* H\nSCHEDULED: <2017-01-19 ++7d>\n"
  5708. (let ((org-adapt-indentation nil)
  5709. (org-last-inserted-timestamp nil))
  5710. (org-schedule nil "2017-02-01"))
  5711. (buffer-string)))))
  5712. ;;; Property API
  5713. (ert-deftest test-org/buffer-property-keys ()
  5714. "Test `org-buffer-property-keys' specifications."
  5715. ;; Retrieve properties across siblings.
  5716. (should
  5717. (equal '("A" "B")
  5718. (org-test-with-temp-text "
  5719. * H1
  5720. :PROPERTIES:
  5721. :A: 1
  5722. :END:
  5723. * H2
  5724. :PROPERTIES:
  5725. :B: 1
  5726. :END:"
  5727. (org-buffer-property-keys))))
  5728. ;; Retrieve properties across children.
  5729. (should
  5730. (equal '("A" "B")
  5731. (org-test-with-temp-text "
  5732. * H1
  5733. :PROPERTIES:
  5734. :A: 1
  5735. :END:
  5736. ** H2
  5737. :PROPERTIES:
  5738. :B: 1
  5739. :END:"
  5740. (org-buffer-property-keys))))
  5741. ;; Retrieve muliple properties in the same drawer.
  5742. (should
  5743. (equal '("A" "B")
  5744. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5745. (org-buffer-property-keys))))
  5746. ;; Ignore extension symbol in property name.
  5747. (should
  5748. (equal '("A")
  5749. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5750. (org-buffer-property-keys))))
  5751. ;; Add bare property if xxx_ALL property is there
  5752. (should
  5753. (equal '("A" "B" "B_ALL")
  5754. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B_ALL: foo bar\n:END:"
  5755. (org-buffer-property-keys))))
  5756. ;; Add bare property if xxx_ALL property is there - check dupes
  5757. (should
  5758. (equal '("A" "B" "B_ALL")
  5759. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:B_ALL: foo bar\n:END:"
  5760. (org-buffer-property-keys))))
  5761. ;; Retrieve properties from #+PROPERTY keyword lines
  5762. (should
  5763. (equal '("A" "C")
  5764. (org-test-with-temp-text "#+PROPERTY: C foo\n* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5765. (org-buffer-property-keys))))
  5766. ;; Retrieve properties from #+PROPERTY keyword lines - make sure an _ALL property also
  5767. ;; adds the bare property
  5768. (should
  5769. (equal '("A" "C" "C_ALL")
  5770. (org-test-with-temp-text "#+PROPERTY: C_ALL foo bar\n* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5771. (org-buffer-property-keys))))
  5772. ;; With non-nil COLUMNS, extract property names from columns.
  5773. (should
  5774. (equal '("A" "B")
  5775. (org-test-with-temp-text "#+COLUMNS: %25ITEM %A %20B"
  5776. (org-buffer-property-keys nil nil t))))
  5777. (should
  5778. (equal '("A" "B" "COLUMNS")
  5779. (org-test-with-temp-text
  5780. "* H\n:PROPERTIES:\n:COLUMNS: %25ITEM %A %20B\n:END:"
  5781. (org-buffer-property-keys nil nil t))))
  5782. ;; In COLUMNS, ignore title and summary-type.
  5783. (should
  5784. (equal '("A")
  5785. (org-test-with-temp-text "#+COLUMNS: %A(Foo)"
  5786. (org-buffer-property-keys nil nil t))))
  5787. (should
  5788. (equal '("A")
  5789. (org-test-with-temp-text "#+COLUMNS: %A{Foo}"
  5790. (org-buffer-property-keys nil nil t))))
  5791. (should
  5792. (equal '("A")
  5793. (org-test-with-temp-text "#+COLUMNS: %A(Foo){Bar}"
  5794. (org-buffer-property-keys nil nil t)))))
  5795. (ert-deftest test-org/property-values ()
  5796. "Test `org-property-values' specifications."
  5797. ;; Regular test.
  5798. (should
  5799. (equal '("2" "1")
  5800. (org-test-with-temp-text
  5801. "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A: 2\n:END:"
  5802. (org-property-values "A"))))
  5803. ;; Ignore empty values.
  5804. (should-not
  5805. (org-test-with-temp-text
  5806. "* H1\n:PROPERTIES:\n:A:\n:END:\n* H2\n:PROPERTIES:\n:A: \n:END:"
  5807. (org-property-values "A")))
  5808. ;; Take into consideration extended values.
  5809. (should
  5810. (equal '("1 2")
  5811. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:END:"
  5812. (org-property-values "A")))))
  5813. (ert-deftest test-org/set-property ()
  5814. "Test `org-set-property' specifications."
  5815. (should
  5816. (equal
  5817. ":PROPERTIES:\n:TEST: t\n:END:\n"
  5818. (org-test-with-temp-text ""
  5819. (let ((org-property-format "%s %s"))
  5820. (org-set-property "TEST" "t"))
  5821. (buffer-string))))
  5822. (should
  5823. (equal
  5824. "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5825. (org-test-with-temp-text "* H"
  5826. (let ((org-adapt-indentation nil)
  5827. (org-property-format "%s %s"))
  5828. (org-set-property "TEST" "t"))
  5829. (buffer-string)))))
  5830. (ert-deftest test-org/delete-property ()
  5831. "Test `org-delete-property' specifications."
  5832. (should
  5833. (equal
  5834. ""
  5835. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5836. (org-delete-property "TEST")
  5837. (buffer-string))))
  5838. (should
  5839. (equal
  5840. ":PROPERTIES:\n:TEST1: t\n:END:\n"
  5841. (org-test-with-temp-text ":PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5842. (org-delete-property "TEST2")
  5843. (buffer-string))))
  5844. (should
  5845. (equal
  5846. "* H\n"
  5847. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST: t\n:END:\n"
  5848. (org-delete-property "TEST")
  5849. (buffer-string))))
  5850. (should
  5851. (equal
  5852. "* H\n:PROPERTIES:\n:TEST1: t\n:END:\n"
  5853. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TEST1: t\n:TEST2: t\n:END:\n"
  5854. (org-delete-property "TEST2")
  5855. (buffer-string)))))
  5856. (ert-deftest test-org/delete-property-globally ()
  5857. "Test `org-delete-property-global' specifications."
  5858. (should
  5859. (equal
  5860. ""
  5861. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n"
  5862. (org-delete-property-globally "TEST")
  5863. (buffer-string))))
  5864. (should
  5865. (equal
  5866. "* H\n"
  5867. (org-test-with-temp-text ":PROPERTIES:\n:TEST: t\n:END:\n* H\n:PROPERTIES:\n:TEST: nil\n:END:"
  5868. (org-delete-property-globally "TEST")
  5869. (buffer-string)))))
  5870. (ert-deftest test-org/find-property ()
  5871. "Test `org-find-property' specifications."
  5872. ;; Regular test.
  5873. (should
  5874. (= 1
  5875. (org-test-with-temp-text "* H\n:PROPERTIES:\n:PROP: value\n:END:"
  5876. (org-find-property "prop"))))
  5877. ;; Find properties in top-level property drawer.
  5878. (should
  5879. (= 1
  5880. (org-test-with-temp-text ":PROPERTIES:\n:PROP: value\n:END:"
  5881. (org-find-property "prop"))))
  5882. ;; Ignore false positives.
  5883. (should
  5884. (= 27
  5885. (org-test-with-temp-text
  5886. "* H1\n:DRAWER:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 1\n:END:"
  5887. (org-find-property "A"))))
  5888. ;; Return first entry found in buffer.
  5889. (should
  5890. (= 1
  5891. (org-test-with-temp-text
  5892. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5893. (org-find-property "A"))))
  5894. ;; Only search visible part of the buffer.
  5895. (should
  5896. (= 31
  5897. (org-test-with-temp-text
  5898. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:<point>A: 1\n:END:"
  5899. (org-narrow-to-subtree)
  5900. (org-find-property "A"))))
  5901. ;; With optional argument, only find entries with a specific value.
  5902. (should-not
  5903. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5904. (org-find-property "A" "2")))
  5905. (should
  5906. (= 31
  5907. (org-test-with-temp-text
  5908. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: 2\n:END:"
  5909. (org-find-property "A" "2"))))
  5910. ;; Use "nil" for explicit nil values.
  5911. (should
  5912. (= 31
  5913. (org-test-with-temp-text
  5914. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n* H2\n:PROPERTIES:\n:A: nil\n:END:"
  5915. (org-find-property "A" "nil")))))
  5916. (ert-deftest test-org/entry-delete ()
  5917. "Test `org-entry-delete' specifications."
  5918. ;; Regular test.
  5919. (should
  5920. (string-match
  5921. " *:PROPERTIES:\n *:B: +2\n *:END:"
  5922. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5923. (org-entry-delete (point) "A")
  5924. (buffer-string))))
  5925. ;; Also remove accumulated properties.
  5926. (should-not
  5927. (string-match
  5928. ":A"
  5929. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:A+: 2\n:B: 3\n:END:"
  5930. (org-entry-delete (point) "A")
  5931. (buffer-string))))
  5932. ;; When last property is removed, remove the property drawer.
  5933. (should-not
  5934. (string-match
  5935. ":PROPERTIES:"
  5936. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\nParagraph"
  5937. (org-entry-delete (point) "A")
  5938. (buffer-string))))
  5939. ;; Return a non-nil value when some property was removed.
  5940. (should
  5941. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5942. (org-entry-delete (point) "A")))
  5943. (should-not
  5944. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:B: 2\n:END:"
  5945. (org-entry-delete (point) "C")))
  5946. ;; Special properties cannot be located in a drawer. Allow to
  5947. ;; remove them anyway, in case of user error.
  5948. (should
  5949. (org-test-with-temp-text "* H\n:PROPERTIES:\n:SCHEDULED: 1\n:END:"
  5950. (org-entry-delete (point) "SCHEDULED"))))
  5951. (ert-deftest test-org/entry-get ()
  5952. "Test `org-entry-get' specifications."
  5953. ;; Regular test.
  5954. (should
  5955. (equal "1"
  5956. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:"
  5957. (org-entry-get (point) "A"))))
  5958. (should
  5959. (equal "1"
  5960. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5961. (org-entry-get (point) "A"))))
  5962. ;; Ignore case.
  5963. (should
  5964. (equal "1"
  5965. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5966. (org-entry-get (point) "a"))))
  5967. ;; Handle extended values, both before and after base value.
  5968. (should
  5969. (equal "1 2 3"
  5970. (org-test-with-temp-text
  5971. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  5972. (org-entry-get (point) "A"))))
  5973. ;; Empty values are returned as the empty string.
  5974. (should
  5975. (equal ""
  5976. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A:\n:END:"
  5977. (org-entry-get (point) "A"))))
  5978. ;; Special nil value. If LITERAL-NIL is non-nil, return "nil",
  5979. ;; otherwise, return nil.
  5980. (should-not
  5981. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5982. (org-entry-get (point) "A")))
  5983. (should
  5984. (equal "nil"
  5985. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: nil\n:END:"
  5986. (org-entry-get (point) "A" nil t))))
  5987. ;; Return nil when no property is found, independently on the
  5988. ;; LITERAL-NIL argument.
  5989. (should-not
  5990. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5991. (org-entry-get (point) "B")))
  5992. (should-not
  5993. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  5994. (org-entry-get (point) "B" nil t)))
  5995. ;; Handle inheritance, when allowed. Include extended values and
  5996. ;; possibly global values.
  5997. (should
  5998. (equal
  5999. "1"
  6000. (org-test-with-temp-text ":PROPERTIES:\n:A: 1\n:END:\n* H"
  6001. (org-entry-get (point-max) "A" t))))
  6002. (should
  6003. (equal
  6004. "1"
  6005. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  6006. (org-entry-get (point-max) "A" t))))
  6007. (should
  6008. (equal
  6009. "1"
  6010. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  6011. (let ((org-use-property-inheritance t))
  6012. (org-entry-get (point-max) "A" 'selective)))))
  6013. (should-not
  6014. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2"
  6015. (let ((org-use-property-inheritance nil))
  6016. (org-entry-get (point-max) "A" 'selective))))
  6017. (should-not
  6018. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:\n* H2"
  6019. (let ((org-use-property-inheritance t))
  6020. (org-entry-get (point-max) "A" t))))
  6021. (should
  6022. (equal
  6023. "1 2"
  6024. (org-test-with-temp-text
  6025. ":PROPERTIES:\n:A: 1\n:END:\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  6026. (org-entry-get (point-max) "A" t))))
  6027. (should
  6028. (equal
  6029. "1 2"
  6030. (org-test-with-temp-text
  6031. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  6032. (org-entry-get (point-max) "A" t))))
  6033. (should
  6034. (equal
  6035. "1 2"
  6036. (org-test-with-temp-text
  6037. ":PROPERTIES:\n:A: 1\n:END:\n* H1\n* H2\n:PROPERTIES:\n:A+: 2\n:END:"
  6038. (org-entry-get (point-max) "A" t))))
  6039. (should
  6040. (equal
  6041. "1 2"
  6042. (org-test-with-temp-text
  6043. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2.1\n** H2.2\n:PROPERTIES:\n:A+: 2\n:END:"
  6044. (org-entry-get (point-max) "A" t))))
  6045. (should
  6046. (equal "1"
  6047. (org-test-with-temp-text
  6048. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A: 1\n:END:"
  6049. (org-mode-restart)
  6050. (org-entry-get (point-max) "A" t))))
  6051. (should
  6052. (equal "0 1"
  6053. (org-test-with-temp-text
  6054. "#+PROPERTY: A 0\n* H\n:PROPERTIES:\n:A+: 1\n:END:"
  6055. (org-mode-restart)
  6056. (org-entry-get (point-max) "A" t))))
  6057. ;; document level property-drawer has precedance over
  6058. ;; global-property by PROPERTY-keyword.
  6059. (should
  6060. (equal "0 2"
  6061. (org-test-with-temp-text
  6062. ":PROPERTIES:\n:A: 0\n:END:\n#+PROPERTY: A 1\n* H\n:PROPERTIES:\n:A+: 2\n:END:"
  6063. (org-mode-restart)
  6064. (org-entry-get (point-max) "A" t))))
  6065. ;; Use alternate separators
  6066. (should
  6067. (equal "0~2"
  6068. (org-test-with-temp-text
  6069. ":PROPERTIES:\n:A: 0\n:A+: 2\n:END:"
  6070. (let ((org-property-separators '((("A") . "~"))))
  6071. (org-entry-get (point) "A")))))
  6072. ;; Default separator is single space
  6073. (should
  6074. (equal "0 2"
  6075. (org-test-with-temp-text
  6076. ":PROPERTIES:\n:A: 0\n:B: 1\n:A+: 2\n:B+: 3\n:END:"
  6077. (let ((org-property-separators '((("B") . "~"))))
  6078. (org-entry-get (point) "A")))))
  6079. ;; Regular expression matching for separator
  6080. (should
  6081. (equal "0/2"
  6082. (org-test-with-temp-text
  6083. ":PROPERTIES:\n:A: 0\n:A+: 2\n:END:"
  6084. (let ((org-property-separators '((("B") . "~") ("[AC]" . "/"))))
  6085. (org-entry-get (point) "A")))))
  6086. ;; Separator works with inheritance
  6087. (should
  6088. (equal "1~2"
  6089. (org-test-with-temp-text
  6090. "* H\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A+: 2\n:END:"
  6091. (let ((org-property-separators '((("A") . "~"))))
  6092. (org-entry-get (point-max) "A" t))))))
  6093. (ert-deftest test-org/entry-properties ()
  6094. "Test `org-entry-properties' specifications."
  6095. ;; Get "ITEM" property.
  6096. (should
  6097. (equal "H"
  6098. (org-test-with-temp-text "* TODO H"
  6099. (cdr (assoc "ITEM" (org-entry-properties nil "ITEM"))))))
  6100. (should
  6101. (equal "H"
  6102. (org-test-with-temp-text "* TODO H"
  6103. (cdr (assoc "ITEM" (org-entry-properties))))))
  6104. ;; Get "TODO" property. TODO keywords are case sensitive.
  6105. (should
  6106. (equal "TODO"
  6107. (org-test-with-temp-text "* TODO H"
  6108. (cdr (assoc "TODO" (org-entry-properties nil "TODO"))))))
  6109. (should
  6110. (equal "TODO"
  6111. (org-test-with-temp-text "* TODO H"
  6112. (cdr (assoc "TODO" (org-entry-properties))))))
  6113. (should-not
  6114. (org-test-with-temp-text "* H"
  6115. (assoc "TODO" (org-entry-properties nil "TODO"))))
  6116. (should-not
  6117. (org-test-with-temp-text "* todo H"
  6118. (assoc "TODO" (org-entry-properties nil "TODO"))))
  6119. ;; Get "PRIORITY" property.
  6120. (should
  6121. (equal "A"
  6122. (org-test-with-temp-text "* [#A] H"
  6123. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  6124. (should
  6125. (equal "A"
  6126. (org-test-with-temp-text "* [#A] H"
  6127. (cdr (assoc "PRIORITY" (org-entry-properties))))))
  6128. (should
  6129. (equal (char-to-string org-priority-default)
  6130. (org-test-with-temp-text "* H"
  6131. (cdr (assoc "PRIORITY" (org-entry-properties nil "PRIORITY"))))))
  6132. ;; Get "FILE" property.
  6133. (should
  6134. (org-test-with-temp-text-in-file "* H\nParagraph"
  6135. (file-equal-p (cdr (assoc "FILE" (org-entry-properties nil "FILE")))
  6136. (buffer-file-name))))
  6137. (should
  6138. (org-test-with-temp-text-in-file "* H\nParagraph"
  6139. (file-equal-p (cdr (assoc "FILE" (org-entry-properties)))
  6140. (buffer-file-name))))
  6141. (should-not
  6142. (org-test-with-temp-text "* H\nParagraph"
  6143. (cdr (assoc "FILE" (org-entry-properties nil "FILE")))))
  6144. ;; Get "TAGS" property.
  6145. (should
  6146. (equal ":tag1:tag2:"
  6147. (org-test-with-temp-text "* H :tag1:tag2:"
  6148. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS"))))))
  6149. (should
  6150. (equal ":tag1:tag2:"
  6151. (org-test-with-temp-text "* H :tag1:tag2:"
  6152. (cdr (assoc "TAGS" (org-entry-properties))))))
  6153. (should-not
  6154. (org-test-with-temp-text "* H"
  6155. (cdr (assoc "TAGS" (org-entry-properties nil "TAGS")))))
  6156. ;; Get "ALLTAGS" property.
  6157. (should
  6158. (equal ":tag1:tag2:"
  6159. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  6160. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS"))))))
  6161. (should
  6162. (equal ":tag1:tag2:"
  6163. (org-test-with-temp-text "* H :tag1:\n<point>** H2 :tag2:"
  6164. (cdr (assoc "ALLTAGS" (org-entry-properties))))))
  6165. (should-not
  6166. (org-test-with-temp-text "* H"
  6167. (cdr (assoc "ALLTAGS" (org-entry-properties nil "ALLTAGS")))))
  6168. ;; Get "BLOCKED" property.
  6169. (should
  6170. (equal "t"
  6171. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** TODO two"
  6172. (let ((org-enforce-todo-dependencies t)
  6173. (org-blocker-hook
  6174. '(org-block-todo-from-children-or-siblings-or-parent)))
  6175. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  6176. (should
  6177. (equal ""
  6178. (org-test-with-temp-text "* TODO Blocked\n** DONE one\n** DONE two"
  6179. (let ((org-enforce-todo-dependencies t)
  6180. (org-blocker-hook
  6181. '(org-block-todo-from-children-or-siblings-or-parent)))
  6182. (cdr (assoc "BLOCKED" (org-entry-properties nil "BLOCKED")))))))
  6183. ;; Get "CLOSED", "DEADLINE" and "SCHEDULED" properties.
  6184. (should
  6185. (equal
  6186. "[2012-03-29 thu.]"
  6187. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  6188. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED"))))))
  6189. (should
  6190. (equal
  6191. "[2012-03-29 thu.]"
  6192. (org-test-with-temp-text "* H\nCLOSED: [2012-03-29 thu.]"
  6193. (cdr (assoc "CLOSED" (org-entry-properties))))))
  6194. (should-not
  6195. (org-test-with-temp-text "* H"
  6196. (cdr (assoc "CLOSED" (org-entry-properties nil "CLOSED")))))
  6197. (should
  6198. (equal
  6199. "<2014-03-04 tue.>"
  6200. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6201. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE"))))))
  6202. (should
  6203. (equal
  6204. "<2014-03-04 tue.>"
  6205. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6206. (cdr (assoc "DEADLINE" (org-entry-properties))))))
  6207. (should-not
  6208. (org-test-with-temp-text "* H"
  6209. (cdr (assoc "DEADLINE" (org-entry-properties nil "DEADLINE")))))
  6210. (should
  6211. (equal
  6212. "<2014-03-04 tue.>"
  6213. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6214. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED"))))))
  6215. (should
  6216. (equal
  6217. "<2014-03-04 tue.>"
  6218. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6219. (cdr (assoc "SCHEDULED" (org-entry-properties))))))
  6220. (should-not
  6221. (org-test-with-temp-text "* H"
  6222. (cdr (assoc "SCHEDULED" (org-entry-properties nil "SCHEDULED")))))
  6223. ;; Get "CATEGORY"
  6224. (should
  6225. (equal "cat"
  6226. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  6227. (cdr (assoc "CATEGORY" (org-entry-properties))))))
  6228. (should
  6229. (equal "cat"
  6230. (org-test-with-temp-text "#+CATEGORY: cat\n<point>* H"
  6231. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  6232. (should
  6233. (equal "cat"
  6234. (org-test-with-temp-text "* H\n:PROPERTIES:\n:CATEGORY: cat\n:END:"
  6235. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  6236. (should
  6237. (equal "cat2"
  6238. (org-test-with-temp-text
  6239. (concat "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  6240. "\n"
  6241. "** H2\n:PROPERTIES:\n:CATEGORY: cat2\n:END:<point>")
  6242. (cdr (assoc "CATEGORY" (org-entry-properties nil "CATEGORY"))))))
  6243. ;; Get "TIMESTAMP" and "TIMESTAMP_IA" properties.
  6244. (should
  6245. (equal "<2012-03-29 thu.>"
  6246. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>"
  6247. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  6248. (should
  6249. (equal "[2012-03-29 thu.]"
  6250. (org-test-with-temp-text "* Entry\n[2012-03-29 thu.]"
  6251. (cdr (assoc "TIMESTAMP_IA" (org-entry-properties))))))
  6252. (should
  6253. (equal "<2012-03-29 thu.>"
  6254. (org-test-with-temp-text "* Entry\n[2014-03-04 tue.]<2012-03-29 thu.>"
  6255. (cdr (assoc "TIMESTAMP" (org-entry-properties nil "TIMESTAMP"))))))
  6256. (should
  6257. (equal "[2014-03-04 tue.]"
  6258. (org-test-with-temp-text "* Entry\n<2012-03-29 thu.>[2014-03-04 tue.]"
  6259. (cdr (assoc "TIMESTAMP_IA"
  6260. (org-entry-properties nil "TIMESTAMP_IA"))))))
  6261. (should-not
  6262. (equal "<2012-03-29 thu.>"
  6263. (org-test-with-temp-text "* Current\n* Next\n<2012-03-29 thu.>"
  6264. (cdr (assoc "TIMESTAMP" (org-entry-properties))))))
  6265. ;; Get standard properties.
  6266. (should
  6267. (equal "1"
  6268. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6269. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  6270. ;; Handle extended properties.
  6271. (should
  6272. (equal "1 2 3"
  6273. (org-test-with-temp-text
  6274. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:A+: 3\n:END:"
  6275. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  6276. (should
  6277. (equal "1 2 3"
  6278. (org-test-with-temp-text
  6279. "* H\n:PROPERTIES:\n:A+: 2\n:A: 1\n:a+: 3\n:END:"
  6280. (cdr (assoc "A" (org-entry-properties nil 'standard))))))
  6281. ;; Ignore forbidden (special) properties.
  6282. (should-not
  6283. (org-test-with-temp-text "* H\n:PROPERTIES:\n:TODO: foo\n:END:"
  6284. (cdr (assoc "TODO" (org-entry-properties nil 'standard))))))
  6285. (ert-deftest test-org/entry-put ()
  6286. "Test `org-entry-put' specifications."
  6287. ;; Error when not a string or nil.
  6288. (should-error
  6289. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  6290. (org-entry-put 1 "test" 2)))
  6291. ;; Error when property name is invalid.
  6292. (should-error
  6293. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  6294. (org-entry-put 1 "no space" "value")))
  6295. (should-error
  6296. (org-test-with-temp-text "* H\n:PROPERTIES:\n:test: 1\n:END:"
  6297. (org-entry-put 1 "" "value")))
  6298. ;; Set "TODO" property.
  6299. (should
  6300. (string-match (regexp-quote " TODO H")
  6301. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  6302. (org-entry-put (point) "TODO" "TODO")
  6303. (buffer-string))))
  6304. (should
  6305. (string-match (regexp-quote "* H")
  6306. (org-test-with-temp-text "#+TODO: TODO | DONE\n<point>* H"
  6307. (org-entry-put (point) "TODO" nil)
  6308. (buffer-string))))
  6309. ;; Set "PRIORITY" property.
  6310. (should
  6311. (equal "* [#A] H"
  6312. (org-test-with-temp-text "* [#B] H"
  6313. (org-entry-put (point) "PRIORITY" "A")
  6314. (buffer-string))))
  6315. (should
  6316. (equal "* H"
  6317. (org-test-with-temp-text "* [#B] H"
  6318. (org-entry-put (point) "PRIORITY" nil)
  6319. (buffer-string))))
  6320. ;; Set "SCHEDULED" property.
  6321. (should
  6322. (string-match "* H\n *SCHEDULED: <2014-03-04 .*?>"
  6323. (org-test-with-temp-text "* H"
  6324. (org-entry-put (point) "SCHEDULED" "2014-03-04")
  6325. (buffer-string))))
  6326. (should
  6327. (string= "* H\n"
  6328. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6329. (org-entry-put (point) "SCHEDULED" nil)
  6330. (buffer-string))))
  6331. (should
  6332. (string-match "* H\n *SCHEDULED: <2014-03-03 .*?>"
  6333. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6334. (org-entry-put (point) "SCHEDULED" "earlier")
  6335. (buffer-string))))
  6336. (should
  6337. (string-match "^ *SCHEDULED: <2014-03-05 .*?>"
  6338. (org-test-with-temp-text "* H\nSCHEDULED: <2014-03-04 tue.>"
  6339. (org-entry-put (point) "SCHEDULED" "later")
  6340. (buffer-string))))
  6341. ;; Set "DEADLINE" property.
  6342. (should
  6343. (string-match "^ *DEADLINE: <2014-03-04 .*?>"
  6344. (org-test-with-temp-text "* H"
  6345. (org-entry-put (point) "DEADLINE" "2014-03-04")
  6346. (buffer-string))))
  6347. (should
  6348. (string= "* H\n"
  6349. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6350. (org-entry-put (point) "DEADLINE" nil)
  6351. (buffer-string))))
  6352. (should
  6353. (string-match "^ *DEADLINE: <2014-03-03 .*?>"
  6354. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6355. (org-entry-put (point) "DEADLINE" "earlier")
  6356. (buffer-string))))
  6357. (should
  6358. (string-match "^ *DEADLINE: <2014-03-05 .*?>"
  6359. (org-test-with-temp-text "* H\nDEADLINE: <2014-03-04 tue.>"
  6360. (org-entry-put (point) "DEADLINE" "later")
  6361. (buffer-string))))
  6362. ;; Set "CATEGORY" property
  6363. (should
  6364. (string-match "^ *:CATEGORY: cat"
  6365. (org-test-with-temp-text "* H"
  6366. (org-entry-put (point) "CATEGORY" "cat")
  6367. (buffer-string))))
  6368. ;; Regular properties, with or without pre-existing drawer.
  6369. (should
  6370. (string-match "^ *:A: +2$"
  6371. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6372. (org-entry-put (point) "A" "2")
  6373. (buffer-string))))
  6374. (should
  6375. (string-match "^ *:A: +1$"
  6376. (org-test-with-temp-text "* H"
  6377. (org-entry-put (point) "A" "1")
  6378. (buffer-string))))
  6379. ;; Special case: two consecutive headlines.
  6380. (should
  6381. (string-match "\\* A\n *:PROPERTIES:"
  6382. (org-test-with-temp-text "* A\n** B"
  6383. (org-entry-put (point) "A" "1")
  6384. (buffer-string)))))
  6385. (ert-deftest test-org/refresh-properties ()
  6386. "Test `org-refresh-properties' specifications."
  6387. (should
  6388. (equal "1"
  6389. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6390. (org-refresh-properties "A" 'org-test)
  6391. (get-text-property (point) 'org-test))))
  6392. (should-not
  6393. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A: 1\n:END:"
  6394. (org-refresh-properties "B" 'org-test)
  6395. (get-text-property (point) 'org-test)))
  6396. ;; Handle properties only defined with extension syntax, i.e.,
  6397. ;; "PROPERTY+".
  6398. (should
  6399. (equal "1"
  6400. (org-test-with-temp-text "* H\n:PROPERTIES:\n:A+: 1\n:END:"
  6401. (org-refresh-properties "A" 'org-test)
  6402. (get-text-property (point) 'org-test))))
  6403. ;; When property is inherited, add text property to the whole
  6404. ;; sub-tree.
  6405. (should
  6406. (equal "1"
  6407. (org-test-with-temp-text
  6408. "* H1\n:PROPERTIES:\n:A: 1\n:END:\n<point>** H2"
  6409. (let ((org-use-property-inheritance t))
  6410. (org-refresh-properties "A" 'org-test))
  6411. (get-text-property (point) 'org-test))))
  6412. ;; When a document level property-drawer is used, those properties
  6413. ;; should work exactly like headline-properties as if at a
  6414. ;; headline-level 0.
  6415. (should
  6416. (equal "1"
  6417. (org-test-with-temp-text
  6418. ":PROPERTIES:\n:A: 1\n:END:\n"
  6419. (org-mode-restart)
  6420. (let ((org-use-property-inheritance t))
  6421. (org-refresh-properties "A" 'org-test))
  6422. (get-text-property (point) 'org-test))))
  6423. (should-not
  6424. (equal "1"
  6425. (org-test-with-temp-text
  6426. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  6427. (org-mode-restart)
  6428. (let ((org-use-property-inheritance nil))
  6429. (org-refresh-properties "A" 'org-test))
  6430. (get-text-property (point) 'org-test))))
  6431. (should
  6432. (equal "1"
  6433. (org-test-with-temp-text
  6434. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1"
  6435. (org-mode-restart)
  6436. (let ((org-use-property-inheritance t))
  6437. (org-refresh-properties "A" 'org-test))
  6438. (get-text-property (point) 'org-test))))
  6439. (should
  6440. (equal "2"
  6441. (org-test-with-temp-text
  6442. ":PROPERTIES:\n:A: 1\n:END:\n<point>* H1\n:PROPERTIES:\n:A: 2\n:END:"
  6443. (org-mode-restart)
  6444. (let ((org-use-property-inheritance t))
  6445. (org-refresh-properties "A" 'org-test))
  6446. (get-text-property (point) 'org-test))))
  6447. ;; When property is inherited, use global value across the whole
  6448. ;; buffer. However local values have precedence, as well as the
  6449. ;; document level property-drawer.
  6450. (should-not
  6451. (equal "1"
  6452. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  6453. (org-mode-restart)
  6454. (let ((org-use-property-inheritance nil))
  6455. (org-refresh-properties "A" 'org-test))
  6456. (get-text-property (point) 'org-test))))
  6457. (should
  6458. (equal "1"
  6459. (org-test-with-temp-text "#+PROPERTY: A 1\n<point>* H1"
  6460. (org-mode-restart)
  6461. (let ((org-use-property-inheritance t))
  6462. (org-refresh-properties "A" 'org-test))
  6463. (get-text-property (point) 'org-test))))
  6464. (should
  6465. (equal "2"
  6466. (org-test-with-temp-text
  6467. "#+PROPERTY: A 1\n<point>* H\n:PROPERTIES:\n:A: 2\n:END:"
  6468. (org-mode-restart)
  6469. (let ((org-use-property-inheritance t))
  6470. (org-refresh-properties "A" 'org-test))
  6471. (get-text-property (point) 'org-test))))
  6472. ;; When both keyword-property and document-level property-block is
  6473. ;; defined, the property-block has precedance.
  6474. (should
  6475. (equal "1"
  6476. (org-test-with-temp-text
  6477. ":PROPERTIES:\n:A: 1\n:END:\n#+PROPERTY: A 2\n<point>* H1"
  6478. (org-mode-restart)
  6479. (let ((org-use-property-inheritance t))
  6480. (org-refresh-properties "A" 'org-test))
  6481. (get-text-property (point) 'org-test)))))
  6482. (ert-deftest test-org/refresh-category-properties ()
  6483. "Test `org-refresh-category-properties' specifications"
  6484. (should
  6485. (equal "cat1"
  6486. (org-test-with-temp-text
  6487. ":PROPERTIES:\n:CATEGORY: cat1\n:END:"
  6488. (org-refresh-category-properties)
  6489. (org-get-category))))
  6490. (should
  6491. (equal "cat1"
  6492. (org-test-with-temp-text
  6493. "* H\n:PROPERTIES:\n:CATEGORY: cat1\n:END:"
  6494. (org-refresh-category-properties)
  6495. (org-get-category))))
  6496. ;; Even though property-inheritance is deactivated, category
  6497. ;; property should be inherited. As described in
  6498. ;; `org-use-property-inheritance'.
  6499. (should
  6500. (equal "cat1"
  6501. (org-test-with-temp-text
  6502. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  6503. (org-mode-restart)
  6504. (let ((org-use-property-inheritance nil))
  6505. (org-refresh-category-properties))
  6506. (org-get-category))))
  6507. (should
  6508. (equal "cat1"
  6509. (org-test-with-temp-text
  6510. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H"
  6511. (org-mode-restart)
  6512. (let ((org-use-property-inheritance t))
  6513. (org-refresh-category-properties))
  6514. (org-get-category))))
  6515. (should
  6516. (equal "cat2"
  6517. (org-test-with-temp-text
  6518. ":PROPERTIES:\n:CATEGORY: cat1\n:END:\n<point>* H\n:PROPERTIES:\n:CATEGORY: cat2\n:END:\n"
  6519. (org-mode-restart)
  6520. (let ((org-use-property-inheritance t))
  6521. (org-refresh-category-properties))
  6522. (org-get-category)))))
  6523. ;;; Refile
  6524. (ert-deftest test-org/refile-get-targets ()
  6525. "Test `org-refile-get-targets' specifications."
  6526. ;; :maxlevel includes all headings above specified value.
  6527. (should
  6528. (equal '("H1" "H2" "H3")
  6529. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6530. (let ((org-refile-use-outline-path nil)
  6531. (org-refile-targets `((nil :maxlevel . 3))))
  6532. (mapcar #'car (org-refile-get-targets))))))
  6533. (should
  6534. (equal '("H1" "H2")
  6535. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6536. (let ((org-refile-use-outline-path nil)
  6537. (org-refile-targets `((nil :maxlevel . 2))))
  6538. (mapcar #'car (org-refile-get-targets))))))
  6539. ;; :level limits targets to headlines with the specified level.
  6540. (should
  6541. (equal '("H2")
  6542. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6543. (let ((org-refile-use-outline-path nil)
  6544. (org-refile-targets `((nil :level . 2))))
  6545. (mapcar #'car (org-refile-get-targets))))))
  6546. ;; :tag limits targets to headlines with specified tag.
  6547. (should
  6548. (equal '("H1")
  6549. (org-test-with-temp-text "* H1 :foo:\n** H2\n*** H3 :bar:"
  6550. (let ((org-refile-use-outline-path nil)
  6551. (org-refile-targets `((nil :tag . "foo"))))
  6552. (mapcar #'car (org-refile-get-targets))))))
  6553. ;; :todo limits targets to headlines with specified TODO keyword.
  6554. (should
  6555. (equal '("H2")
  6556. (org-test-with-temp-text "* H1\n** TODO H2\n*** DONE H3"
  6557. (let ((org-refile-use-outline-path nil)
  6558. (org-refile-targets `((nil :todo . "TODO"))))
  6559. (mapcar #'car (org-refile-get-targets))))))
  6560. ;; :regexp filters targets matching provided regexp.
  6561. (should
  6562. (equal '("F2" "F3")
  6563. (org-test-with-temp-text "* H1\n** F2\n*** F3"
  6564. (let ((org-refile-use-outline-path nil)
  6565. (org-refile-targets `((nil :regexp . "F"))))
  6566. (mapcar #'car (org-refile-get-targets))))))
  6567. ;; A nil `org-refile-targets' includes only top level headlines in
  6568. ;; current buffer.
  6569. (should
  6570. (equal '("H1" "H2")
  6571. (org-test-with-temp-text "* H1\n** S1\n* H2"
  6572. (let ((org-refile-use-outline-path nil)
  6573. (org-refile-targets nil))
  6574. (mapcar #'car (org-refile-get-targets))))))
  6575. ;; Return value is the union of the targets according to all the
  6576. ;; defined rules. However, prevent duplicates.
  6577. (should
  6578. (equal '("F2" "F3" "H1")
  6579. (org-test-with-temp-text "* TODO H1\n** F2\n*** F3"
  6580. (let ((org-refile-use-outline-path nil)
  6581. (org-refile-targets `((nil :regexp . "F")
  6582. (nil :todo . "TODO"))))
  6583. (mapcar #'car (org-refile-get-targets))))))
  6584. (should
  6585. (equal '("F2" "F3" "H1")
  6586. (org-test-with-temp-text "* TODO H1\n** TODO F2\n*** F3"
  6587. (let ((org-refile-use-outline-path nil)
  6588. (org-refile-targets `((nil :regexp . "F")
  6589. (nil :todo . "TODO"))))
  6590. (mapcar #'car (org-refile-get-targets))))))
  6591. ;; When `org-refile-use-outline-path' is non-nil, provide targets as
  6592. ;; paths.
  6593. (should
  6594. (equal '("H1" "H1/H2" "H1/H2/H3")
  6595. (org-test-with-temp-text "* H1\n** H2\n*** H3"
  6596. (let ((org-refile-use-outline-path t)
  6597. (org-refile-targets `((nil :maxlevel . 3))))
  6598. (mapcar #'car (org-refile-get-targets))))))
  6599. ;; When providing targets as paths, escape forward slashes in
  6600. ;; headings with backslashes.
  6601. (should
  6602. (equal '("H1\\/foo")
  6603. (org-test-with-temp-text "* H1/foo"
  6604. (let ((org-refile-use-outline-path t)
  6605. (org-refile-targets `((nil :maxlevel . 1))))
  6606. (mapcar #'car (org-refile-get-targets))))))
  6607. ;; When `org-refile-use-outline-path' is `file', include file name
  6608. ;; without directory in targets.
  6609. (should
  6610. (org-test-with-temp-text-in-file "* H1"
  6611. (let* ((filename (buffer-file-name))
  6612. (org-refile-use-outline-path 'file)
  6613. (org-refile-targets `(((,filename) :level . 1))))
  6614. (member (file-name-nondirectory filename)
  6615. (mapcar #'car (org-refile-get-targets))))))
  6616. ;; When `org-refile-use-outline-path' is `full-file-path', include
  6617. ;; full file name.
  6618. (should
  6619. (org-test-with-temp-text-in-file "* H1"
  6620. (let* ((filename (file-truename (buffer-file-name)))
  6621. (org-refile-use-outline-path 'full-file-path)
  6622. (org-refile-targets `(((,filename) :level . 1))))
  6623. (member filename (mapcar #'car (org-refile-get-targets))))))
  6624. ;; When `org-refile-use-outline-path' is `buffer-name', include
  6625. ;; buffer name.
  6626. (should
  6627. (org-test-with-temp-text "* H1"
  6628. (let* ((org-refile-use-outline-path 'buffer-name)
  6629. (org-refile-targets `((nil :level . 1))))
  6630. (member (buffer-name) (mapcar #'car (org-refile-get-targets)))))))
  6631. ;;; Sparse trees
  6632. (ert-deftest test-org/match-sparse-tree ()
  6633. "Test `org-match-sparse-tree' specifications."
  6634. ;; Match tags.
  6635. (should-not
  6636. (org-test-with-temp-text "* H\n** H1 :tag:"
  6637. (org-match-sparse-tree nil "tag")
  6638. (search-forward "H1")
  6639. (org-invisible-p2)))
  6640. (should
  6641. (org-test-with-temp-text "* H\n** H1 :tag:\n** H2 :tag2:"
  6642. (org-match-sparse-tree nil "tag")
  6643. (search-forward "H2")
  6644. (org-invisible-p2)))
  6645. ;; "-" operator for tags.
  6646. (should-not
  6647. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6648. (org-match-sparse-tree nil "tag1-tag2")
  6649. (search-forward "H1")
  6650. (org-invisible-p2)))
  6651. (should
  6652. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6653. (org-match-sparse-tree nil "tag1-tag2")
  6654. (search-forward "H2")
  6655. (org-invisible-p2)))
  6656. ;; "&" operator for tags.
  6657. (should
  6658. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6659. (org-match-sparse-tree nil "tag1&tag2")
  6660. (search-forward "H1")
  6661. (org-invisible-p2)))
  6662. (should-not
  6663. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6664. (org-match-sparse-tree nil "tag1&tag2")
  6665. (search-forward "H2")
  6666. (org-invisible-p2)))
  6667. ;; "|" operator for tags.
  6668. (should-not
  6669. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6670. (org-match-sparse-tree nil "tag1|tag2")
  6671. (search-forward "H1")
  6672. (org-invisible-p2)))
  6673. (should-not
  6674. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :tag1:tag2:"
  6675. (org-match-sparse-tree nil "tag1|tag2")
  6676. (search-forward "H2")
  6677. (org-invisible-p2)))
  6678. ;; Regexp match on tags.
  6679. (should-not
  6680. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  6681. (org-match-sparse-tree nil "{^tag.*}")
  6682. (search-forward "H1")
  6683. (org-invisible-p2)))
  6684. (should
  6685. (org-test-with-temp-text "* H\n** H1 :tag1:\n** H2 :foo:"
  6686. (org-match-sparse-tree nil "{^tag.*}")
  6687. (search-forward "H2")
  6688. (org-invisible-p2)))
  6689. ;; Match group tags.
  6690. (should-not
  6691. (org-test-with-temp-text
  6692. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  6693. (org-match-sparse-tree nil "work")
  6694. (search-forward "H1")
  6695. (org-invisible-p2)))
  6696. (should-not
  6697. (org-test-with-temp-text
  6698. "#+TAGS: { work : lab }\n* H\n** H1 :work:\n** H2 :lab:"
  6699. (org-match-sparse-tree nil "work")
  6700. (search-forward "H2")
  6701. (org-invisible-p2)))
  6702. ;; Match group tags with hard brackets.
  6703. (should-not
  6704. (org-test-with-temp-text
  6705. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  6706. (org-match-sparse-tree nil "work")
  6707. (search-forward "H1")
  6708. (org-invisible-p2)))
  6709. (should-not
  6710. (org-test-with-temp-text
  6711. "#+TAGS: [ work : lab ]\n* H\n** H1 :work:\n** H2 :lab:"
  6712. (org-match-sparse-tree nil "work")
  6713. (search-forward "H2")
  6714. (org-invisible-p2)))
  6715. ;; Match tags in hierarchies
  6716. (should-not
  6717. (org-test-with-temp-text
  6718. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  6719. #+TAGS: [ Lev_2 : Lev_3 ]\n
  6720. #+TAGS: { Lev_3 : Lev_4 }\n
  6721. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  6722. (org-match-sparse-tree nil "Lev_1")
  6723. (search-forward "H4")
  6724. (org-invisible-p2)))
  6725. (should-not
  6726. (org-test-with-temp-text
  6727. "#+TAGS: [ Lev_1 : Lev_2 ]\n
  6728. #+TAGS: [ Lev_2 : Lev_3 ]\n
  6729. #+TAGS: { Lev_3 : Lev_4 }\n
  6730. * H\n** H1 :Lev_1:\n** H2 :Lev_2:\n** H3 :Lev_3:\n** H4 :Lev_4:"
  6731. (org-match-sparse-tree nil "Lev_1+Lev_3")
  6732. (search-forward "H4")
  6733. (org-invisible-p2)))
  6734. ;; Match regular expressions in tags
  6735. (should-not
  6736. (org-test-with-temp-text
  6737. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_1:"
  6738. (org-match-sparse-tree nil "Lev")
  6739. (search-forward "H1")
  6740. (org-invisible-p2)))
  6741. (should
  6742. (org-test-with-temp-text
  6743. "#+TAGS: [ Lev : {Lev_[0-9]} ]\n* H\n** H1 :Lev_n:"
  6744. (org-match-sparse-tree nil "Lev")
  6745. (search-forward "H1")
  6746. (org-invisible-p2)))
  6747. ;; Match properties.
  6748. (should
  6749. (org-test-with-temp-text
  6750. "* H\n** H1\n:PROPERTIES:\n:A: 1\n:END:\n** H2\n:PROPERTIES:\n:A: 2\n:END:"
  6751. (org-match-sparse-tree nil "A=\"1\"")
  6752. (search-forward "H2")
  6753. (org-invisible-p2)))
  6754. (should-not
  6755. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  6756. (org-match-sparse-tree nil "A=\"1\"")
  6757. (search-forward "H2")
  6758. (org-invisible-p2)))
  6759. ;; Case is not significant when matching properties.
  6760. (should-not
  6761. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:A: 1\n:END:"
  6762. (org-match-sparse-tree nil "a=\"1\"")
  6763. (search-forward "H2")
  6764. (org-invisible-p2)))
  6765. (should-not
  6766. (org-test-with-temp-text "* H1\n** H2\n:PROPERTIES:\n:a: 1\n:END:"
  6767. (org-match-sparse-tree nil "A=\"1\"")
  6768. (search-forward "H2")
  6769. (org-invisible-p2)))
  6770. ;; Match special LEVEL property.
  6771. (should-not
  6772. (org-test-with-temp-text "* H\n** H1\n*** H2"
  6773. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  6774. (search-forward "H1")
  6775. (org-invisible-p2)))
  6776. (should
  6777. (org-test-with-temp-text "* H\n** H1\n*** H2"
  6778. (let ((org-odd-levels-only nil)) (org-match-sparse-tree nil "LEVEL=2"))
  6779. (search-forward "H2")
  6780. (org-invisible-p2)))
  6781. ;; Comparison operators when matching properties.
  6782. (should
  6783. (org-test-with-temp-text
  6784. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  6785. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  6786. (search-forward "H1")
  6787. (org-invisible-p2)))
  6788. (should-not
  6789. (org-test-with-temp-text
  6790. "* H\n** H1\nSCHEDULED: <2014-03-04 tue.>\n** H2\nSCHEDULED: <2012-03-29 thu.>"
  6791. (org-match-sparse-tree nil "SCHEDULED<=\"<2013-01-01>\"")
  6792. (search-forward "H2")
  6793. (org-invisible-p2)))
  6794. ;; Regexp match on properties values.
  6795. (should-not
  6796. (org-test-with-temp-text
  6797. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  6798. (org-match-sparse-tree nil "A={f.*}")
  6799. (search-forward "H1")
  6800. (org-invisible-p2)))
  6801. (should
  6802. (org-test-with-temp-text
  6803. "* H\n** H1\n:PROPERTIES:\n:A: foo\n:END:\n** H2\n:PROPERTIES:\n:A: bar\n:END:"
  6804. (org-match-sparse-tree nil "A={f.*}")
  6805. (search-forward "H2")
  6806. (org-invisible-p2)))
  6807. ;; With an optional argument, limit match to TODO entries.
  6808. (should-not
  6809. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  6810. (org-match-sparse-tree t "tag")
  6811. (search-forward "H1")
  6812. (org-invisible-p2)))
  6813. (should
  6814. (org-test-with-temp-text "* H\n** TODO H1 :tag:\n** H2 :tag:"
  6815. (org-match-sparse-tree t "tag")
  6816. (search-forward "H2")
  6817. (org-invisible-p2))))
  6818. (ert-deftest test-org/occur ()
  6819. "Test `org-occur' specifications."
  6820. ;; Count number of matches.
  6821. (should
  6822. (= 1
  6823. (org-test-with-temp-text "* H\nA\n* H2"
  6824. (org-occur "A"))))
  6825. (should
  6826. (= 2
  6827. (org-test-with-temp-text "* H\nA\n* H2\nA"
  6828. (org-occur "A"))))
  6829. ;; Test CALLBACK optional argument.
  6830. (should
  6831. (= 0
  6832. (org-test-with-temp-text "* H\nA\n* H2"
  6833. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  6834. (should
  6835. (= 1
  6836. (org-test-with-temp-text "* H\nA\n* H2\nA"
  6837. (org-occur "A" nil (lambda () (equal (org-get-heading) "H2"))))))
  6838. ;; Case-fold searches according to `org-occur-case-fold-search'.
  6839. (should
  6840. (= 2
  6841. (org-test-with-temp-text "Aa"
  6842. (let ((org-occur-case-fold-search t)) (org-occur "A")))))
  6843. (should
  6844. (= 2
  6845. (org-test-with-temp-text "Aa"
  6846. (let ((org-occur-case-fold-search t)) (org-occur "a")))))
  6847. (should
  6848. (= 1
  6849. (org-test-with-temp-text "Aa"
  6850. (let ((org-occur-case-fold-search nil)) (org-occur "A")))))
  6851. (should
  6852. (= 1
  6853. (org-test-with-temp-text "Aa"
  6854. (let ((org-occur-case-fold-search nil)) (org-occur "a")))))
  6855. (should
  6856. (= 1
  6857. (org-test-with-temp-text "Aa"
  6858. (let ((org-occur-case-fold-search 'smart)) (org-occur "A")))))
  6859. (should
  6860. (= 2
  6861. (org-test-with-temp-text "Aa"
  6862. (let ((org-occur-case-fold-search 'smart)) (org-occur "a"))))))
  6863. ;;; Tags
  6864. (ert-deftest test-org/tag-string-to-alist ()
  6865. "Test `org-tag-string-to-alist' specifications."
  6866. ;; Tag without selection key.
  6867. (should (equal (org-tag-string-to-alist "tag1") '(("tag1"))))
  6868. ;; Tag with selection key.
  6869. (should (equal (org-tag-string-to-alist "tag1(t)") '(("tag1" . ?t))))
  6870. ;; Tag group.
  6871. (should
  6872. (equal
  6873. (org-tag-string-to-alist "[ group : t1 t2 ]")
  6874. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag))))
  6875. ;; Mutually exclusive tags.
  6876. (should (equal (org-tag-string-to-alist "{ tag1 tag2 }")
  6877. '((:startgroup) ("tag1") ("tag2") (:endgroup))))
  6878. (should
  6879. (equal
  6880. (org-tag-string-to-alist "{ group : tag1 tag2 }")
  6881. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))))
  6882. (ert-deftest test-org/tag-alist-to-string ()
  6883. "Test `org-tag-alist-to-string' specifications."
  6884. (should (equal (org-tag-alist-to-string '(("tag1"))) "tag1"))
  6885. (should (equal (org-tag-alist-to-string '(("tag1" . ?t))) "tag1(t)"))
  6886. (should
  6887. (equal
  6888. (org-tag-alist-to-string
  6889. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6890. "[ group : t1 t2 ]"))
  6891. (should
  6892. (equal (org-tag-alist-to-string
  6893. '((:startgroup) ("tag1") ("tag2") (:endgroup)))
  6894. "{ tag1 tag2 }"))
  6895. (should
  6896. (equal
  6897. (org-tag-alist-to-string
  6898. '((:startgroup) ("group") (:grouptags) ("tag1") ("tag2") (:endgroup)))
  6899. "{ group : tag1 tag2 }")))
  6900. (ert-deftest test-org/tag-alist-to-groups ()
  6901. "Test `org-tag-alist-to-groups' specifications."
  6902. (should
  6903. (equal (org-tag-alist-to-groups
  6904. '((:startgroup) ("group") (:grouptags) ("t1") ("t2") (:endgroup)))
  6905. '(("group" "t1" "t2"))))
  6906. (should
  6907. (equal
  6908. (org-tag-alist-to-groups
  6909. '((:startgrouptag) ("group") (:grouptags) ("t1") ("t2") (:endgrouptag)))
  6910. '(("group" "t1" "t2"))))
  6911. (should-not
  6912. (org-tag-alist-to-groups
  6913. '((:startgroup) ("group") ("t1") ("t2") (:endgroup)))))
  6914. (ert-deftest test-org/tag-align ()
  6915. "Test tags alignment."
  6916. ;; Test aligning tags with different display width.
  6917. (should
  6918. ;; 12345678901234567890
  6919. (equal "* Test :abc:"
  6920. (org-test-with-temp-text "* Test :abc:"
  6921. (let ((org-tags-column -20)
  6922. (indent-tabs-mode nil))
  6923. (org-fix-tags-on-the-fly))
  6924. (buffer-string))))
  6925. (should
  6926. ;; 12345678901234567890
  6927. (equal "* Test :日本語:"
  6928. (org-test-with-temp-text "* Test :日本語:"
  6929. (let ((org-tags-column -20)
  6930. (indent-tabs-mode nil))
  6931. (org-fix-tags-on-the-fly))
  6932. (buffer-string))))
  6933. ;; Make sure aligning tags do not skip invisible text.
  6934. (should
  6935. (equal "* [[linkx]] :tag:"
  6936. (org-test-with-temp-text "* [[link<point>]] :tag:"
  6937. (let ((org-tags-column 0))
  6938. (org-fix-tags-on-the-fly)
  6939. (insert "x")
  6940. (buffer-string)))))
  6941. ;; Aligning tags preserve position.
  6942. (should
  6943. (= 6 (org-test-with-temp-text "* 345 <point> :tag:"
  6944. (let ((org-tags-column 78)
  6945. (indent-tabs-mode nil))
  6946. (org-fix-tags-on-the-fly))
  6947. (current-column))))
  6948. ;; Aligning all tags in visible buffer.
  6949. (should
  6950. ;; 12345678901234567890
  6951. (equal (concat "* Level 1 :abc:\n"
  6952. "** Level 2 :def:")
  6953. (org-test-with-temp-text (concat "* Level 1 :abc:\n"
  6954. "** Level 2 :def:")
  6955. (let ((org-tags-column -20)
  6956. (indent-tabs-mode nil))
  6957. ;; (org-align-tags :all) must work even when the point
  6958. ;; is at the end of the buffer.
  6959. (goto-char (point-max))
  6960. (org-align-tags :all))
  6961. (buffer-string)))))
  6962. (ert-deftest test-org/get-tags ()
  6963. "Test `org-get-tags' specifications."
  6964. ;; Standard test.
  6965. (should
  6966. (equal '("foo")
  6967. (org-test-with-temp-text "* Test :foo:" (org-get-tags))))
  6968. (should
  6969. (equal '("foo" "bar")
  6970. (org-test-with-temp-text "* Test :foo:bar:" (org-get-tags))))
  6971. ;; Tags for inlinetasks.
  6972. (should
  6973. (equal '("foo" "bar")
  6974. (progn
  6975. (require 'org-inlinetask)
  6976. (org-test-with-temp-text (concat (make-string org-inlinetask-min-level ?*) " Test :foo:bar:")
  6977. (org-get-tags (org-element-at-point))))))
  6978. (should
  6979. (equal '("foo" "bar")
  6980. (progn
  6981. (require 'org-inlinetask)
  6982. (org-test-with-temp-text (concat (make-string org-inlinetask-min-level ?*) " Test :foo:bar:")
  6983. (org-get-tags nil)))))
  6984. ;; Return nil when there is no tag.
  6985. (should-not
  6986. (org-test-with-temp-text "* Test" (org-get-tags)))
  6987. ;; Tags are inherited from parent headlines.
  6988. (should
  6989. (equal '("tag")
  6990. (let ((org-use-tag-inheritance t))
  6991. (org-test-with-temp-text "* H0 :foo:\n* H1 :tag:\n<point>** H2"
  6992. (org-get-tags)))))
  6993. ;; Tags are inherited from `org-file-tags'.
  6994. (should
  6995. (equal '("tag")
  6996. (org-test-with-temp-text "* H1"
  6997. (let ((org-file-tags '("tag"))
  6998. (org-use-tag-inheritance t))
  6999. (org-get-tags)))))
  7000. ;; Only inherited tags have the `inherited' text property.
  7001. (should
  7002. (get-text-property 0 'inherited
  7003. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7004. (let ((org-use-tag-inheritance t))
  7005. (assoc-string "foo" (org-get-tags))))))
  7006. (should-not
  7007. (get-text-property 0 'inherited
  7008. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7009. (let ((org-use-tag-inheritance t))
  7010. (assoc-string "bar" (org-get-tags))))))
  7011. ;; Obey to `org-use-tag-inheritance'.
  7012. (should-not
  7013. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7014. (let ((org-use-tag-inheritance nil))
  7015. (assoc-string "foo" (org-get-tags)))))
  7016. (should-not
  7017. (org-test-with-temp-text "* H1 :foo:\n** <point>H2 :bar:"
  7018. (let ((org-use-tag-inheritance nil)
  7019. (org-file-tags '("foo")))
  7020. (assoc-string "foo" (org-get-tags)))))
  7021. (should-not
  7022. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7023. (let ((org-use-tag-inheritance '("bar")))
  7024. (assoc-string "foo" (org-get-tags)))))
  7025. (should
  7026. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7027. (let ((org-use-tag-inheritance '("bar")))
  7028. (assoc-string "bar" (org-get-tags)))))
  7029. (should-not
  7030. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7031. (let ((org-use-tag-inheritance "b.*"))
  7032. (assoc-string "foo" (org-get-tags)))))
  7033. (should
  7034. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7035. (let ((org-use-tag-inheritance "b.*"))
  7036. (assoc-string "bar" (org-get-tags)))))
  7037. ;; When optional argument LOCAL is non-nil, ignore tag inheritance.
  7038. (should
  7039. (equal '("baz")
  7040. (org-test-with-temp-text "* H1 :foo:bar:\n** <point>H2 :baz:"
  7041. (let ((org-use-tag-inheritance t))
  7042. (org-get-tags nil t)))))
  7043. ;; When optional argument POS is non-nil, get tags there instead.
  7044. (should
  7045. (equal '("foo")
  7046. (org-test-with-temp-text "* H1 :foo:\n* <point>H2 :bar:"
  7047. (org-get-tags 1))))
  7048. ;; Make sure tags excluded from inheritance are returned if local
  7049. (should
  7050. (equal '("foo")
  7051. (org-test-with-temp-text "* Test :foo:"
  7052. (let ((org-use-tag-inheritance t)
  7053. (org-tags-exclude-from-inheritance '("foo")))
  7054. (org-get-tags)))))
  7055. ;; Test the collection of tags from #+filetags and parent tags.
  7056. (should
  7057. (equal '("a" "b" "c" "d")
  7058. (org-test-with-temp-text (concat "#+filetags: a\n"
  7059. "* Level 1 :b:\n"
  7060. "** Level 2 :c:\n"
  7061. "*** Level 3 :d:\n"
  7062. "<point>")
  7063. (let ((org-use-tag-inheritance t))
  7064. (org-mode-restart) ;So that `org-file-tags' get populated from #+filetags
  7065. (org-get-tags)))))
  7066. ;; Pathological case: tagged headline with an empty body.
  7067. (should (org-test-with-temp-text "* :tag:" (org-get-tags))))
  7068. (ert-deftest test-org/set-tags ()
  7069. "Test `org-set-tags' specifications."
  7070. ;; Throw an error on invalid data.
  7071. (should-error
  7072. (org-test-with-temp-text "* H"
  7073. (org-set-tags 'foo)))
  7074. ;; `nil', an empty, and a blank string remove all tags.
  7075. (should
  7076. (equal "* H"
  7077. (org-test-with-temp-text "* H :tag1:tag2:"
  7078. (org-set-tags nil)
  7079. (buffer-string))))
  7080. (should
  7081. (equal "* H"
  7082. (org-test-with-temp-text "* H :tag1:tag2:"
  7083. (org-set-tags "")
  7084. (buffer-string))))
  7085. (should
  7086. (equal "* H"
  7087. (org-test-with-temp-text "* H :tag1:tag2:"
  7088. (org-set-tags " ")
  7089. (buffer-string))))
  7090. ;; If there's nothing to remove, just bail out.
  7091. (should
  7092. (equal "* H"
  7093. (org-test-with-temp-text "* H"
  7094. (org-set-tags nil)
  7095. (buffer-string))))
  7096. (should
  7097. (equal "* "
  7098. (org-test-with-temp-text "* "
  7099. (org-set-tags nil)
  7100. (buffer-string))))
  7101. ;; If DATA is a tag string, set current tags to it, even if it means
  7102. ;; replacing old tags.
  7103. (should
  7104. (equal "* H :tag0:"
  7105. (org-test-with-temp-text "* H :tag1:tag2:"
  7106. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  7107. (buffer-string))))
  7108. (should
  7109. (equal "* H :tag0:"
  7110. (org-test-with-temp-text "* H"
  7111. (let ((org-tags-column 1)) (org-set-tags ":tag0:"))
  7112. (buffer-string))))
  7113. ;; If DATA is a list, set tags to this list, even if it means
  7114. ;; replacing old tags.
  7115. (should
  7116. (equal "* H :tag0:"
  7117. (org-test-with-temp-text "* H :tag1:tag2:"
  7118. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  7119. (buffer-string))))
  7120. (should
  7121. (equal "* H :tag0:"
  7122. (org-test-with-temp-text "* H"
  7123. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  7124. (buffer-string))))
  7125. ;; When set, apply `org-tags-sort-function'.
  7126. (should
  7127. (equal "* H :a:b:"
  7128. (org-test-with-temp-text "* H"
  7129. (let ((org-tags-column 1)
  7130. (org-tags-sort-function #'string<))
  7131. (org-set-tags '("b" "a"))
  7132. (buffer-string)))))
  7133. ;; When new tags are identical to the previous ones, still align.
  7134. (should
  7135. (equal "* H :foo:"
  7136. (org-test-with-temp-text "* H :foo:"
  7137. (let ((org-tags-column 1))
  7138. (org-set-tags '("foo"))
  7139. (buffer-string)))))
  7140. ;; When tags have been changed, run `org-after-tags-change-hook'.
  7141. (should
  7142. (catch :return
  7143. (org-test-with-temp-text "* H :foo:"
  7144. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  7145. (org-set-tags '("bar"))
  7146. nil))))
  7147. (should-not
  7148. (catch :return
  7149. (org-test-with-temp-text "* H :foo:"
  7150. (let ((org-after-tags-change-hook (lambda () (throw :return t))))
  7151. (org-set-tags '("foo"))
  7152. nil))))
  7153. ;; Special case: handle empty headlines.
  7154. (should
  7155. (equal "* :tag0:"
  7156. (org-test-with-temp-text "* "
  7157. (let ((org-tags-column 1)) (org-set-tags '("tag0")))
  7158. (buffer-string))))
  7159. ;; Modify buffer only when a tag change happens or alignment is
  7160. ;; done.
  7161. (should-not
  7162. (org-test-with-temp-text "* H :foo:"
  7163. (set-buffer-modified-p nil)
  7164. (let ((org-tags-column 1)) (org-set-tags '("foo")))
  7165. (buffer-modified-p)))
  7166. (should
  7167. (org-test-with-temp-text "* H :foo:"
  7168. (set-buffer-modified-p nil)
  7169. (let ((org-tags-column 10)) (org-set-tags '("foo")))
  7170. (buffer-modified-p)))
  7171. (should
  7172. (org-test-with-temp-text "* H :foo:"
  7173. (set-buffer-modified-p nil)
  7174. (let ((org-tags-column 10)) (org-set-tags '("bar")))
  7175. (buffer-modified-p)))
  7176. ;; Pathological case: when setting tags of a folded headline, do not
  7177. ;; let new tags being sucked into invisibility.
  7178. (should-not
  7179. (org-test-with-temp-text "* H1\nContent\n* H2\n\n Other Content"
  7180. ;; Show only headlines
  7181. (org-content)
  7182. ;; Set NEXT tag on current entry
  7183. (org-set-tags ":NEXT:")
  7184. ;; Move point to that NEXT tag
  7185. (search-forward "NEXT") (backward-word)
  7186. ;; And it should be visible (i.e. no overlays)
  7187. (overlays-at (point)))))
  7188. (ert-deftest test-org/set-tags-command ()
  7189. "Test `org-set-tags-command' specifications"
  7190. ;; Set tags at current headline.
  7191. (should
  7192. (equal "* H1 :foo:"
  7193. (org-test-with-temp-text "* H1"
  7194. (cl-letf (((symbol-function 'completing-read-multiple)
  7195. (lambda (&rest _args) '("foo"))))
  7196. (let ((org-use-fast-tag-selection nil)
  7197. (org-tags-column 1))
  7198. (org-set-tags-command)))
  7199. (buffer-string))))
  7200. ;; Preserve position when called from the section below.
  7201. (should
  7202. (equal "* H1 :foo:\nContents"
  7203. (org-test-with-temp-text "* H1\n<point>Contents"
  7204. (cl-letf (((symbol-function 'completing-read-multiple)
  7205. (lambda (&rest _args) '("foo"))))
  7206. (let ((org-use-fast-tag-selection nil)
  7207. (org-tags-column 1))
  7208. (org-set-tags-command)))
  7209. (buffer-string))))
  7210. (should-not
  7211. (equal "* H1 :foo:\nContents2"
  7212. (org-test-with-temp-text "* H1\n<point>Contents2"
  7213. (cl-letf (((symbol-function 'completing-read-multiple)
  7214. (lambda (&rest _args) '("foo"))))
  7215. (let ((org-use-fast-tag-selection nil)
  7216. (org-tags-column 1))
  7217. (org-set-tags-command)))
  7218. (org-at-heading-p))))
  7219. ;; When a region is active and
  7220. ;; `org-loop-over-headlines-in-active-region' is non-nil, insert the
  7221. ;; same value in all headlines in region.
  7222. (should
  7223. (equal "* H1 :foo:\nContents\n* H2 :foo:"
  7224. (org-test-with-temp-text "* H1\nContents\n* H2"
  7225. (cl-letf (((symbol-function 'completing-read-multiple)
  7226. (lambda (&rest _args) '("foo"))))
  7227. (let ((org-use-fast-tag-selection nil)
  7228. (org-loop-over-headlines-in-active-region t)
  7229. (org-tags-column 1))
  7230. (transient-mark-mode 1)
  7231. (push-mark (point) t t)
  7232. (goto-char (point-max))
  7233. (org-set-tags-command)))
  7234. (buffer-string))))
  7235. (should
  7236. (equal "* H1\nContents\n* H2 :foo:"
  7237. (org-test-with-temp-text "* H1\nContents\n* H2"
  7238. (cl-letf (((symbol-function 'completing-read-multiple)
  7239. (lambda (&rest _args) '("foo"))))
  7240. (let ((org-use-fast-tag-selection nil)
  7241. (org-loop-over-headlines-in-active-region nil)
  7242. (org-tags-column 1))
  7243. (transient-mark-mode 1)
  7244. (push-mark (point) t t)
  7245. (goto-char (point-max))
  7246. (org-set-tags-command)))
  7247. (buffer-string))))
  7248. ;; With a C-u prefix argument, align all tags in the buffer.
  7249. (should
  7250. (equal "* H1 :foo:\n* H2 :bar:"
  7251. (org-test-with-temp-text "* H1 :foo:\n* H2 :bar:"
  7252. (let ((org-tags-column 1)) (org-set-tags-command '(4)))
  7253. (buffer-string))))
  7254. ;; Point does not move with empty headline.
  7255. (should
  7256. (equal ":foo:"
  7257. (org-test-with-temp-text "* <point>"
  7258. (cl-letf (((symbol-function 'completing-read-multiple)
  7259. (lambda (&rest _args) '("foo"))))
  7260. (let ((org-use-fast-tag-selection nil)
  7261. (org-tags-column 1))
  7262. (org-set-tags-command)))
  7263. (buffer-substring (point) (line-end-position)))))
  7264. ;; Point does not move at start of line.
  7265. (should
  7266. (equal "* H1 :foo:"
  7267. (org-test-with-temp-text "* H1"
  7268. (cl-letf (((symbol-function 'completing-read-multiple)
  7269. (lambda (&rest _args) '("foo"))))
  7270. (let ((org-use-fast-tag-selection nil)
  7271. (org-tags-column 1))
  7272. (org-set-tags-command)))
  7273. (buffer-substring (point) (line-end-position)))))
  7274. ;; Point does not move when within *'s.
  7275. (should
  7276. (equal "* H1 :foo:"
  7277. (org-test-with-temp-text "*<point>* H1"
  7278. (cl-letf (((symbol-function 'completing-read-multiple)
  7279. (lambda (&rest _args) '("foo"))))
  7280. (let ((org-use-fast-tag-selection nil)
  7281. (org-tags-column 1))
  7282. (org-set-tags-command)))
  7283. (buffer-substring (point) (line-end-position)))))
  7284. ;; Point workaround does not get fooled when looking at a space.
  7285. (should
  7286. (equal " b :foo:"
  7287. (org-test-with-temp-text "* a<point> b"
  7288. (cl-letf (((symbol-function 'completing-read-multiple)
  7289. (lambda (&rest _args) '("foo"))))
  7290. (let ((org-use-fast-tag-selection nil)
  7291. (org-tags-column 1))
  7292. (org-set-tags-command)))
  7293. (buffer-substring (point) (line-end-position)))))
  7294. ;; Handle tags both set locally and inherited.
  7295. (should
  7296. (equal "b :foo:"
  7297. (org-test-with-temp-text "* a :foo:\n** <point>b :foo:"
  7298. (cl-letf (((symbol-function 'completing-read-multiple)
  7299. (lambda (_prompt _coll &optional _pred _req initial &rest _)
  7300. (list initial))))
  7301. (let ((org-use-fast-tag-selection nil)
  7302. (org-tags-column 1))
  7303. (org-set-tags-command)))
  7304. (buffer-substring (point) (line-end-position))))))
  7305. (ert-deftest test-org/toggle-tag ()
  7306. "Test `org-toggle-tag' specifications."
  7307. ;; Insert missing tag.
  7308. (should
  7309. (equal "* H :tag:"
  7310. (org-test-with-temp-text "* H"
  7311. (let ((org-tags-column 1)) (org-toggle-tag "tag"))
  7312. (buffer-string))))
  7313. (should
  7314. (equal "* H :tag1:tag2:"
  7315. (org-test-with-temp-text "* H :tag1:"
  7316. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  7317. (buffer-string))))
  7318. ;; Remove existing tag.
  7319. (should
  7320. (equal "* H"
  7321. (org-test-with-temp-text "* H :tag:"
  7322. (org-toggle-tag "tag")
  7323. (buffer-string))))
  7324. (should
  7325. (equal "* H :tag1:"
  7326. (org-test-with-temp-text "* H :tag1:tag2:"
  7327. (let ((org-tags-column 1)) (org-toggle-tag "tag2"))
  7328. (buffer-string))))
  7329. (should
  7330. (equal "* H :tag2:"
  7331. (org-test-with-temp-text "* H :tag1:tag2:"
  7332. (let ((org-tags-column 1)) (org-toggle-tag "tag1"))
  7333. (buffer-string))))
  7334. ;; With optional argument ONOFF set to `on', try to insert the tag,
  7335. ;; even if its already there.
  7336. (should
  7337. (equal "* H :tag:"
  7338. (org-test-with-temp-text "* H"
  7339. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  7340. (buffer-string))))
  7341. (should
  7342. (equal "* H :tag:"
  7343. (org-test-with-temp-text "* H :tag:"
  7344. (let ((org-tags-column 1)) (org-toggle-tag "tag" 'on))
  7345. (buffer-string))))
  7346. ;; With optional argument ONOFF set to `off', try to remove the tag,
  7347. ;; even if its not there.
  7348. (should
  7349. (equal "* H"
  7350. (org-test-with-temp-text "* H :tag:"
  7351. (org-toggle-tag "tag" 'off)
  7352. (buffer-string))))
  7353. (should
  7354. (equal "* H :tag:"
  7355. (org-test-with-temp-text "* H :tag:"
  7356. (let ((org-tags-column 1)) (org-toggle-tag "foo" 'off))
  7357. (buffer-string))))
  7358. ;; Special case: Handle properly tag inheritance. In particular, do
  7359. ;; not set inherited tags.
  7360. (should
  7361. (equal "* H1 :tag:\n** H2 :tag2:tag:"
  7362. (org-test-with-temp-text "* H1 :tag:\n** <point>H2 :tag2:"
  7363. (let ((org-use-tag-inheritance t)
  7364. (org-tags-column 1))
  7365. (org-toggle-tag "tag"))
  7366. (buffer-string))))
  7367. (should
  7368. (equal "* H1 :tag1:tag2:\n** H2 :foo:"
  7369. (org-test-with-temp-text "* H1 :tag1:tag2:\n** <point>H2"
  7370. (let ((org-use-tag-inheritance t)
  7371. (org-tags-column 1))
  7372. (org-toggle-tag "foo"))
  7373. (buffer-string)))))
  7374. (ert-deftest test-org/tags-expand ()
  7375. "Test `org-tags-expand' specifications."
  7376. ;; Expand tag groups as a regexp enclosed within curly brackets.
  7377. (should
  7378. (equal "{\\<[ABC]\\>}"
  7379. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7380. (org-mode-restart)
  7381. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  7382. (should
  7383. (equal "{\\<\\(?:Aa\\|Bb\\|Cc\\)\\>}"
  7384. (org-test-with-temp-text "#+TAGS: [ Aa : Bb Cc ]"
  7385. (org-mode-restart)
  7386. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "Aa")))))
  7387. ;; Preserve operator before the regexp.
  7388. (should
  7389. (equal "+{\\<[ABC]\\>}"
  7390. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7391. (org-mode-restart)
  7392. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "+A")))))
  7393. (should
  7394. (equal "-{\\<[ABC]\\>}"
  7395. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7396. (org-mode-restart)
  7397. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "-A")))))
  7398. ;; Handle "|" syntax.
  7399. (should
  7400. (equal "{\\<[ABC]\\>}|D"
  7401. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7402. (org-mode-restart)
  7403. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|D")))))
  7404. ;; Handle nested groups.
  7405. (should
  7406. (equal "{\\<[A-D]\\>}"
  7407. (org-test-with-temp-text "#+TAGS: [ A : B C ]\n#+TAGS: [ B : D ]"
  7408. (org-mode-restart)
  7409. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A")))))
  7410. ;; Expand multiple occurrences of the same group.
  7411. (should
  7412. (equal "{\\<[ABC]\\>}|{\\<[ABC]\\>}"
  7413. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7414. (org-mode-restart)
  7415. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "A|A")))))
  7416. ;; Preserve regexp matches.
  7417. (should
  7418. (equal "{A+}"
  7419. (org-test-with-temp-text "#+TAGS: [ A : B C ]"
  7420. (org-mode-restart)
  7421. (let ((org-tag-alist-for-agenda nil)) (org-tags-expand "{A+}"))))))
  7422. ;;; TODO keywords
  7423. (ert-deftest test-org/auto-repeat-maybe ()
  7424. "Test `org-auto-repeat-maybe' specifications."
  7425. ;; Do not auto repeat when there is no valid time stamp with
  7426. ;; a repeater in the entry.
  7427. (should-not
  7428. (string-prefix-p
  7429. "* TODO H"
  7430. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7431. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu>"
  7432. (org-todo "DONE")
  7433. (buffer-string)))))
  7434. (should-not
  7435. (string-prefix-p
  7436. "* TODO H"
  7437. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7438. (org-test-with-temp-text "* TODO H\n# <2012-03-29 Thu>"
  7439. (org-todo "DONE")
  7440. (buffer-string)))))
  7441. ;; When switching to DONE state, switch back to first TODO keyword
  7442. ;; in sequence, or the same keyword if they have different types.
  7443. (should
  7444. (string-prefix-p
  7445. "* TODO H"
  7446. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7447. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7448. (org-todo "DONE")
  7449. (buffer-string)))))
  7450. (should
  7451. (string-prefix-p
  7452. "* KWD1 H"
  7453. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  7454. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  7455. (org-todo "DONE")
  7456. (buffer-string)))))
  7457. (should
  7458. (string-prefix-p
  7459. "* KWD2 H"
  7460. (let ((org-todo-keywords '((type "KWD1" "KWD2" "DONE"))))
  7461. (org-test-with-temp-text "* KWD2 H\n<2012-03-29 Thu +2y>"
  7462. (org-todo "DONE")
  7463. (buffer-string)))))
  7464. ;; If there was no TODO keyword in the first place, do not insert
  7465. ;; any either.
  7466. (should
  7467. (string-prefix-p
  7468. "* H"
  7469. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7470. (org-test-with-temp-text "* H\n<2012-03-29 Thu +2y>"
  7471. (org-todo "DONE")
  7472. (buffer-string)))))
  7473. ;; Revert to REPEAT_TO_STATE, if set.
  7474. (should
  7475. (string-prefix-p
  7476. "* KWD2 H"
  7477. (let ((org-todo-keywords '((sequence "KWD1" "KWD2" "DONE"))))
  7478. (org-test-with-temp-text
  7479. "* KWD2 H
  7480. :PROPERTIES:
  7481. :REPEAT_TO_STATE: KWD2
  7482. :END:
  7483. <2012-03-29 Thu +2y>"
  7484. (org-todo "DONE")
  7485. (buffer-string)))))
  7486. ;; When switching to DONE state, update base date. If there are
  7487. ;; multiple repeated time stamps, update them all.
  7488. (should
  7489. (string-match-p
  7490. "<2014-03-29 .* \\+2y>"
  7491. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7492. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7493. (org-todo "DONE")
  7494. (buffer-string)))))
  7495. (should
  7496. (string-match-p
  7497. "<2015-03-04 .* \\+1y>"
  7498. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7499. (org-test-with-temp-text
  7500. "* TODO H\n<2012-03-29 Thu. +2y>\n<2014-03-04 Tue +1y>"
  7501. (org-todo "DONE")
  7502. (buffer-string)))))
  7503. ;; Throw an error if repeater unit is the hour and no time is
  7504. ;; provided in the time-stamp.
  7505. (should-error
  7506. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7507. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2h>"
  7508. (org-todo "DONE")
  7509. (buffer-string))))
  7510. ;; Handle every repeater type using hours step.
  7511. (should
  7512. (string-match-p
  7513. "2014-03-04 .* 02:00"
  7514. (org-test-at-time "<2014-03-04 02:35>"
  7515. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 +8h>"
  7516. (org-todo "DONE")
  7517. (buffer-string)))))
  7518. (should
  7519. (string-match-p
  7520. "2014-03-04 .* 10:00"
  7521. (org-test-at-time "<2014-03-04 02:35>"
  7522. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 ++8h>"
  7523. (org-todo "DONE")
  7524. (buffer-string)))))
  7525. (should
  7526. (string-match-p
  7527. "2014-03-04 .* 10:35"
  7528. (org-test-at-time "<2014-03-04 02:35>"
  7529. (org-test-with-temp-text "* TODO H\n<2014-03-03 18:00 .+8h>"
  7530. (org-todo "DONE")
  7531. (buffer-string)))))
  7532. ;; Do not repeat inactive time stamps with a repeater.
  7533. (should-not
  7534. (string-match-p
  7535. "\\[2014-03-29 .* \\+2y\\]"
  7536. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7537. (org-test-with-temp-text
  7538. "* TODO H\n[2012-03-29 Thu. +2y]"
  7539. (org-todo "DONE")
  7540. (buffer-string)))))
  7541. ;; Do not repeat commented time stamps.
  7542. (should-not
  7543. (string-prefix-p
  7544. "<2015-03-04 .* \\+1y>"
  7545. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7546. (org-test-with-temp-text
  7547. "* TODO H\n<2012-03-29 Thu +2y>\n# <2014-03-04 Tue +1y>"
  7548. (org-todo "DONE")
  7549. (buffer-string)))))
  7550. (should-not
  7551. (string-prefix-p
  7552. "<2015-03-04 .* \\+1y>"
  7553. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7554. (org-test-with-temp-text
  7555. "* TODO H
  7556. <2012-03-29 Thu. +2y>
  7557. #+BEGIN_EXAMPLE
  7558. <2014-03-04 Tue +1y>
  7559. #+END_EXAMPLE"
  7560. (org-todo "DONE")
  7561. (buffer-string)))))
  7562. ;; When `org-log-repeat' is non-nil or there is a CLOCK in the
  7563. ;; entry, record time of last repeat.
  7564. (should-not
  7565. (string-match-p
  7566. ":LAST_REPEAT:"
  7567. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  7568. (org-log-repeat nil))
  7569. (cl-letf (((symbol-function 'org-add-log-setup)
  7570. (lambda (&rest _args) nil)))
  7571. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  7572. (org-todo "DONE")
  7573. (buffer-string))))))
  7574. (should
  7575. (string-match-p
  7576. ":LAST_REPEAT:"
  7577. (let ((org-todo-keywords '((sequence "TODO" "DONE")))
  7578. (org-log-repeat t))
  7579. (cl-letf (((symbol-function 'org-add-log-setup)
  7580. (lambda (&rest _args) nil)))
  7581. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu. +2y>"
  7582. (org-todo "DONE")
  7583. (buffer-string))))))
  7584. (should
  7585. (string-match-p
  7586. ":LAST_REPEAT:"
  7587. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7588. (cl-letf (((symbol-function 'org-add-log-setup)
  7589. (lambda (&rest _args) nil)))
  7590. (org-test-with-temp-text
  7591. "* TODO H\n<2012-03-29 Thu +2y>\nCLOCK: [2012-03-29 Thu 16:40]"
  7592. (org-todo "DONE")
  7593. (buffer-string))))))
  7594. ;; When a SCHEDULED entry has no repeater, remove it upon repeating
  7595. ;; the entry as it is no longer relevant.
  7596. (should-not
  7597. (string-match-p
  7598. "^SCHEDULED:"
  7599. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7600. (org-test-with-temp-text
  7601. "* TODO H\nSCHEDULED: <2014-03-04 Tue>\n<2012-03-29 Thu +2y>"
  7602. (org-todo "DONE")
  7603. (buffer-string)))))
  7604. ;; Properly advance repeater even when a clock entry is specified
  7605. ;; and `org-log-repeat' is nil.
  7606. (should
  7607. (string-match-p
  7608. "SCHEDULED: <2014-03-29"
  7609. (let ((org-log-repeat nil)
  7610. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7611. (org-test-with-temp-text
  7612. "* TODO H
  7613. SCHEDULED: <2012-03-29 Thu +2y>
  7614. CLOCK: [2012-03-29 Thu 10:00]--[2012-03-29 Thu 16:40] => 6:40"
  7615. (org-todo "DONE")
  7616. (buffer-string)))))
  7617. ;; Make sure that logbook state change record does not get
  7618. ;; duplicated when `org-log-repeat' `org-log-done' are non-nil.
  7619. (should
  7620. (string-match-p
  7621. (rx "* TODO Read book
  7622. SCHEDULED: <2021-06-16 " (1+ (not space)) " +1d>
  7623. :PROPERTIES:
  7624. :LAST_REPEAT:" (1+ nonl) "
  7625. :END:
  7626. - State \"DONE\" from \"TODO\"" (1+ nonl) buffer-end)
  7627. (let ((org-log-repeat 'time)
  7628. (org-todo-keywords '((sequence "TODO" "|" "DONE(d!)")))
  7629. (org-log-into-drawer nil))
  7630. (org-test-with-temp-text
  7631. "* TODO Read book
  7632. SCHEDULED: <2021-06-15 Tue +1d>"
  7633. (org-todo "DONE")
  7634. (when (memq 'org-add-log-note post-command-hook)
  7635. (org-add-log-note))
  7636. (buffer-string))))))
  7637. (ert-deftest test-org/org-log-done ()
  7638. "Test `org-log-done' specifications."
  7639. ;; nil value.
  7640. (should
  7641. (string=
  7642. "* DONE task"
  7643. (let ((org-log-done nil)
  7644. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7645. (org-test-with-temp-text
  7646. "* TODO task"
  7647. (org-todo "DONE")
  7648. (when (memq 'org-add-log-note post-command-hook)
  7649. (org-add-log-note))
  7650. (buffer-string)))))
  7651. ;; `time' value.
  7652. (should
  7653. (string=
  7654. (format
  7655. "* DONE task
  7656. CLOSED: %s"
  7657. (org-test-with-temp-text ""
  7658. (org-insert-time-stamp (current-time) t t)
  7659. (buffer-string)))
  7660. (let ((org-log-done 'time)
  7661. (org-log-done-with-time t)
  7662. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7663. (org-test-with-temp-text
  7664. "* TODO task"
  7665. (org-todo "DONE")
  7666. (when (memq 'org-add-log-note post-command-hook)
  7667. (org-add-log-note))
  7668. (buffer-string)))))
  7669. (should
  7670. (string=
  7671. (format
  7672. "* DONE task
  7673. CLOSED: %s"
  7674. (org-test-with-temp-text ""
  7675. (org-insert-time-stamp (current-time) nil t)
  7676. (buffer-string)))
  7677. (let ((org-log-done 'time)
  7678. (org-log-done-with-time nil)
  7679. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7680. (org-test-with-temp-text
  7681. "* TODO task"
  7682. (org-todo "DONE")
  7683. (when (memq 'org-add-log-note post-command-hook)
  7684. (org-add-log-note))
  7685. (buffer-string)))))
  7686. ;; TODO: Test `note' value.
  7687. ;; Test startup overrides.
  7688. (should
  7689. (string=
  7690. "#+STARTUP: nologdone
  7691. * DONE task"
  7692. (let ((org-log-done 'time)
  7693. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7694. (org-test-with-temp-text
  7695. "#+STARTUP: nologdone
  7696. <point>* TODO task"
  7697. (org-set-regexps-and-options)
  7698. (org-todo "DONE")
  7699. (when (memq 'org-add-log-note post-command-hook)
  7700. (org-add-log-note))
  7701. (buffer-string)))))
  7702. (should
  7703. (string=
  7704. (format
  7705. "#+STARTUP: logdone
  7706. * DONE task
  7707. CLOSED: %s"
  7708. (org-test-with-temp-text ""
  7709. (org-insert-time-stamp (current-time) t t)
  7710. (buffer-string)))
  7711. (let ((org-log-done nil)
  7712. (org-log-done-with-time t)
  7713. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7714. (org-test-with-temp-text
  7715. "#+STARTUP: logdone
  7716. <point>* TODO task"
  7717. (org-set-regexps-and-options)
  7718. (org-todo "DONE")
  7719. (when (memq 'org-add-log-note post-command-hook)
  7720. (org-add-log-note))
  7721. (buffer-string)))))
  7722. ;; Test local property overrides.
  7723. (should
  7724. (string=
  7725. "* DONE task
  7726. :PROPERTIES:
  7727. :LOGGING: nil
  7728. :END:"
  7729. (let ((org-log-done 'time)
  7730. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7731. (org-test-with-temp-text
  7732. "* TODO task
  7733. :PROPERTIES:
  7734. :LOGGING: nil
  7735. :END:"
  7736. (org-todo "DONE")
  7737. (when (memq 'org-add-log-note post-command-hook)
  7738. (org-add-log-note))
  7739. (buffer-string)))))
  7740. (should
  7741. (string=
  7742. (format
  7743. "* DONE task
  7744. CLOSED: %s
  7745. :PROPERTIES:
  7746. :LOGGING: logdone
  7747. :END:"
  7748. (org-test-with-temp-text ""
  7749. (org-insert-time-stamp (current-time) t t)
  7750. (buffer-string)))
  7751. (let ((org-log-done nil)
  7752. (org-log-done-with-time t)
  7753. (org-todo-keywords '((sequence "TODO" "DONE"))))
  7754. (org-test-with-temp-text
  7755. "* TODO task
  7756. :PROPERTIES:
  7757. :LOGGING: logdone
  7758. :END:"
  7759. (org-todo "DONE")
  7760. (when (memq 'org-add-log-note post-command-hook)
  7761. (org-add-log-note))
  7762. (buffer-string))))))
  7763. (ert-deftest test-org/org-todo-prefix ()
  7764. "Test `org-todo' prefix arg behavior."
  7765. ;; FIXME: Add tests for all other allowed prefix arguments.
  7766. ;; -1 prefix arg should cancel repeater and mark DONE.
  7767. (should
  7768. (string-match-p
  7769. "DONE H\\(.*\n\\)*<2012-03-29 Thu \\+0y>"
  7770. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7771. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7772. (org-todo -1)
  7773. (buffer-string)))))
  7774. ;; - prefix arg should cancel repeater and mark DONE.
  7775. (should
  7776. (string-match-p
  7777. "DONE H\\(.*\n\\)*<2012-03-29 Thu \\+0y>"
  7778. (let ((org-todo-keywords '((sequence "TODO" "DONE"))))
  7779. (org-test-with-temp-text "* TODO H\n<2012-03-29 Thu +2y>"
  7780. (org-todo '-)
  7781. (buffer-string))))))
  7782. ;;; Timestamps API
  7783. (ert-deftest test-org/at-timestamp-p ()
  7784. "Test `org-at-timestamp-p' specifications."
  7785. (should
  7786. (org-test-with-temp-text "<2012-03-29 Thu>"
  7787. (org-at-timestamp-p)))
  7788. (should-not
  7789. (org-test-with-temp-text "2012-03-29 Thu"
  7790. (org-at-timestamp-p)))
  7791. ;; Test return values.
  7792. (should
  7793. (eq 'bracket
  7794. (org-test-with-temp-text "<2012-03-29 Thu>"
  7795. (org-at-timestamp-p))))
  7796. (should
  7797. (eq 'year
  7798. (org-test-with-temp-text "<<point>2012-03-29 Thu>"
  7799. (org-at-timestamp-p))))
  7800. (should
  7801. (eq 'month
  7802. (org-test-with-temp-text "<2012-<point>03-29 Thu>"
  7803. (org-at-timestamp-p))))
  7804. (should
  7805. (eq 'day
  7806. (org-test-with-temp-text "<2012-03-<point>29 Thu>"
  7807. (org-at-timestamp-p))))
  7808. (should
  7809. (eq 'day
  7810. (org-test-with-temp-text "<2012-03-29 T<point>hu>"
  7811. (org-at-timestamp-p))))
  7812. (should
  7813. (wholenump
  7814. (org-test-with-temp-text "<2012-03-29 Thu +2<point>y>"
  7815. (org-at-timestamp-p))))
  7816. (should
  7817. (eq 'bracket
  7818. (org-test-with-temp-text "<2012-03-29 Thu<point>>"
  7819. (org-at-timestamp-p))))
  7820. (should
  7821. (eq 'after
  7822. (org-test-with-temp-text "<2012-03-29 Thu><point>»"
  7823. (org-at-timestamp-p))))
  7824. ;; Test `inactive' optional argument.
  7825. (should
  7826. (org-test-with-temp-text "[2012-03-29 Thu]"
  7827. (org-at-timestamp-p 'inactive)))
  7828. (should-not
  7829. (org-test-with-temp-text "[2012-03-29 Thu]"
  7830. (org-at-timestamp-p)))
  7831. ;; When optional argument is `agenda', recognize time-stamps in
  7832. ;; planning info line, property drawers and clocks.
  7833. (should
  7834. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  7835. (org-at-timestamp-p 'agenda)))
  7836. (should-not
  7837. (org-test-with-temp-text "* H\nSCHEDULED: <point><2012-03-29 Thu>"
  7838. (org-at-timestamp-p)))
  7839. (should
  7840. (org-test-with-temp-text
  7841. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  7842. (org-at-timestamp-p 'agenda)))
  7843. (should-not
  7844. (org-test-with-temp-text
  7845. "* H\n:PROPERTIES:\n:PROP: <point><2012-03-29 Thu>\n:END:"
  7846. (org-at-timestamp-p)))
  7847. (should
  7848. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  7849. (let ((org-agenda-include-inactive-timestamps t))
  7850. (org-at-timestamp-p 'agenda))))
  7851. (should-not
  7852. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  7853. (let ((org-agenda-include-inactive-timestamps t))
  7854. (org-at-timestamp-p))))
  7855. (should-not
  7856. (org-test-with-temp-text "CLOCK: <point>[2012-03-29 Thu]"
  7857. (let ((org-agenda-include-inactive-timestamps t))
  7858. (org-at-timestamp-p 'inactive))))
  7859. ;; When optional argument is `lax', match any part of the document
  7860. ;; with Org timestamp syntax.
  7861. (should
  7862. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  7863. (org-at-timestamp-p 'lax)))
  7864. (should-not
  7865. (org-test-with-temp-text "# <2012-03-29 Thu><point>"
  7866. (org-at-timestamp-p)))
  7867. (should
  7868. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  7869. (org-at-timestamp-p 'lax)))
  7870. (should-not
  7871. (org-test-with-temp-text ": <2012-03-29 Thu><point>"
  7872. (org-at-timestamp-p)))
  7873. (should
  7874. (org-test-with-temp-text
  7875. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  7876. (org-at-timestamp-p 'lax)))
  7877. (should-not
  7878. (org-test-with-temp-text
  7879. "#+BEGIN_EXAMPLE\n<2012-03-29 Thu><point>\n#+END_EXAMPLE"
  7880. (org-at-timestamp-p)))
  7881. ;; Optional argument `lax' also matches inactive timestamps.
  7882. (should
  7883. (org-test-with-temp-text "# [2012-03-29 Thu]<point>"
  7884. (org-at-timestamp-p 'lax))))
  7885. (ert-deftest test-org/time-stamp ()
  7886. "Test `org-time-stamp' specifications."
  7887. ;; Insert chosen time stamp at point.
  7888. (should
  7889. (string-match
  7890. "Te<2014-03-04 .*?>xt"
  7891. (org-test-with-temp-text "Te<point>xt"
  7892. (cl-letf (((symbol-function 'org-read-date)
  7893. (lambda (&rest _args)
  7894. (org-time-string-to-time "2014-03-04"))))
  7895. (org-time-stamp nil)
  7896. (buffer-string)))))
  7897. ;; With a prefix argument, also insert time.
  7898. (should
  7899. (string-match
  7900. "Te<2014-03-04 .*? 00:41>xt"
  7901. (org-test-with-temp-text "Te<point>xt"
  7902. (cl-letf (((symbol-function 'org-read-date)
  7903. (lambda (&rest _args)
  7904. (org-time-string-to-time "2014-03-04 00:41"))))
  7905. (org-time-stamp '(4))
  7906. (buffer-string)))))
  7907. ;; With two universal prefix arguments, insert an active timestamp
  7908. ;; with the current time without prompting the user.
  7909. (should
  7910. (string-match
  7911. "Te<2014-03-04 .*? 00:41>xt"
  7912. (org-test-with-temp-text "Te<point>xt"
  7913. (org-test-at-time "2014-03-04 00:41"
  7914. (org-time-stamp '(16))
  7915. (buffer-string)))))
  7916. ;; When optional argument is non-nil, insert an inactive timestamp.
  7917. (should
  7918. (string-match
  7919. "Te\\[2014-03-04 .*?\\]xt"
  7920. (org-test-with-temp-text "Te<point>xt"
  7921. (cl-letf (((symbol-function 'org-read-date)
  7922. (lambda (&rest _args)
  7923. (org-time-string-to-time "2014-03-04"))))
  7924. (org-time-stamp nil t)
  7925. (buffer-string)))))
  7926. ;; When called from a timestamp, replace existing one.
  7927. (should
  7928. (string-match
  7929. "<2014-03-04 .*?>"
  7930. (org-test-with-temp-text "<2012-03-29<point> thu.>"
  7931. (cl-letf (((symbol-function 'org-read-date)
  7932. (lambda (&rest _args)
  7933. (org-time-string-to-time "2014-03-04"))))
  7934. (org-time-stamp nil)
  7935. (buffer-string)))))
  7936. (should
  7937. (string-match
  7938. "<2014-03-04 .*?>--<2014-03-04 .*?>"
  7939. (org-test-with-temp-text "<2012-03-29<point> thu.>--<2014-03-04 tue.>"
  7940. (cl-letf (((symbol-function 'org-read-date)
  7941. (lambda (&rest _args)
  7942. (org-time-string-to-time "2014-03-04"))))
  7943. (org-time-stamp nil)
  7944. (buffer-string)))))
  7945. ;; When replacing a timestamp, preserve repeater, if any.
  7946. (should
  7947. (string-match
  7948. "<2014-03-04 .*? \\+2y>"
  7949. (org-test-with-temp-text "<2012-03-29<point> thu. +2y>"
  7950. (cl-letf (((symbol-function 'org-read-date)
  7951. (lambda (&rest _args)
  7952. (org-time-string-to-time "2014-03-04"))))
  7953. (org-time-stamp nil)
  7954. (buffer-string)))))
  7955. ;; When called twice in a raw, build a date range.
  7956. (should
  7957. (string-match
  7958. "<2012-03-29 .*?>--<2014-03-04 .*?>"
  7959. (org-test-with-temp-text "<2012-03-29 thu.><point>"
  7960. (cl-letf (((symbol-function 'org-read-date)
  7961. (lambda (&rest _args)
  7962. (org-time-string-to-time "2014-03-04"))))
  7963. (let ((last-command 'org-time-stamp)
  7964. (this-command 'org-time-stamp))
  7965. (org-time-stamp nil))
  7966. (buffer-string))))))
  7967. (ert-deftest test-org/timestamp-has-time-p ()
  7968. "Test `org-timestamp-has-time-p' specifications."
  7969. ;; With time.
  7970. (should
  7971. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  7972. (org-timestamp-has-time-p (org-element-context))))
  7973. ;; Without time.
  7974. (should-not
  7975. (org-test-with-temp-text "<2012-03-29 Thu>"
  7976. (org-timestamp-has-time-p (org-element-context)))))
  7977. (ert-deftest test-org/get-repeat ()
  7978. "Test `org-get-repeat' specifications."
  7979. (should
  7980. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40 +2y>"
  7981. (org-get-repeat)))
  7982. (should-not
  7983. (org-test-with-temp-text "* H\n<2012-03-29 Thu 16:40>"
  7984. (org-get-repeat)))
  7985. ;; Return proper repeat string.
  7986. (should
  7987. (equal "+2y"
  7988. (org-test-with-temp-text "* H\n<2014-03-04 Tue 16:40 +2y>"
  7989. (org-get-repeat))))
  7990. ;; Prevent false positive (commented or verbatim time stamps)
  7991. (should-not
  7992. (org-test-with-temp-text "* H\n# <2012-03-29 Thu 16:40>"
  7993. (org-get-repeat)))
  7994. (should-not
  7995. (org-test-with-temp-text
  7996. "* H\n#+BEGIN_EXAMPLE\n<2012-03-29 Thu 16:40>\n#+END_EXAMPLE"
  7997. (org-get-repeat)))
  7998. ;; Return nil when called before first heading.
  7999. (should-not
  8000. (org-test-with-temp-text "<2012-03-29 Thu 16:40 +2y>"
  8001. (org-get-repeat)))
  8002. ;; When called with an optional argument, extract repeater from that
  8003. ;; string instead.
  8004. (should (equal "+2y" (org-get-repeat "<2012-03-29 Thu 16:40 +2y>")))
  8005. (should-not (org-get-repeat "<2012-03-29 Thu 16:40>")))
  8006. (ert-deftest test-org/timestamp-format ()
  8007. "Test `org-timestamp-format' specifications."
  8008. ;; Regular test.
  8009. (should
  8010. (equal
  8011. "2012-03-29 16:40"
  8012. (org-test-with-temp-text "<2012-03-29 Thu 16:40>"
  8013. (org-timestamp-format (org-element-context) "%Y-%m-%d %R"))))
  8014. ;; Range end.
  8015. (should
  8016. (equal
  8017. "2012-03-29"
  8018. (org-test-with-temp-text "[2011-07-14 Thu]--[2012-03-29 Thu]"
  8019. (org-timestamp-format (org-element-context) "%Y-%m-%d" t)))))
  8020. (ert-deftest test-org/timestamp-split-range ()
  8021. "Test `org-timestamp-split-range' specifications."
  8022. ;; Extract range start (active).
  8023. (should
  8024. (equal '(2012 3 29)
  8025. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8026. (let ((ts (org-timestamp-split-range (org-element-context))))
  8027. (mapcar (lambda (p) (org-element-property p ts))
  8028. '(:year-end :month-end :day-end))))))
  8029. ;; Extract range start (inactive)
  8030. (should
  8031. (equal '(2012 3 29)
  8032. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  8033. (let ((ts (org-timestamp-split-range (org-element-context))))
  8034. (mapcar (lambda (p) (org-element-property p ts))
  8035. '(:year-end :month-end :day-end))))))
  8036. ;; Extract range end (active).
  8037. (should
  8038. (equal '(2012 3 30)
  8039. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8040. (let ((ts (org-timestamp-split-range
  8041. (org-element-context) t)))
  8042. (mapcar (lambda (p) (org-element-property p ts))
  8043. '(:year-end :month-end :day-end))))))
  8044. ;; Extract range end (inactive)
  8045. (should
  8046. (equal '(2012 3 30)
  8047. (org-test-with-temp-text "[2012-03-29 Thu]--[2012-03-30 Fri]"
  8048. (let ((ts (org-timestamp-split-range
  8049. (org-element-context) t)))
  8050. (mapcar (lambda (p) (org-element-property p ts))
  8051. '(:year-end :month-end :day-end))))))
  8052. ;; Return the timestamp if not a range.
  8053. (should
  8054. (org-test-with-temp-text "[2012-03-29 Thu]"
  8055. (let* ((ts-orig (org-element-context))
  8056. (ts-copy (org-timestamp-split-range ts-orig)))
  8057. (eq ts-orig ts-copy))))
  8058. (should
  8059. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  8060. (let* ((ts-orig (org-element-context))
  8061. (ts-copy (org-timestamp-split-range ts-orig)))
  8062. (eq ts-orig ts-copy)))))
  8063. (ert-deftest test-org/timestamp-translate ()
  8064. "Test `org-timestamp-translate' specifications."
  8065. ;; Translate whole date range.
  8066. (should
  8067. (equal "<29>--<30>"
  8068. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8069. (let ((org-display-custom-times t)
  8070. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8071. (org-timestamp-translate (org-element-context))))))
  8072. ;; Translate date range start.
  8073. (should
  8074. (equal "<29>"
  8075. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8076. (let ((org-display-custom-times t)
  8077. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8078. (org-timestamp-translate (org-element-context) 'start)))))
  8079. ;; Translate date range end.
  8080. (should
  8081. (equal "<30>"
  8082. (org-test-with-temp-text "<2012-03-29 Thu>--<2012-03-30 Fri>"
  8083. (let ((org-display-custom-times t)
  8084. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8085. (org-timestamp-translate (org-element-context) 'end)))))
  8086. ;; Translate time range.
  8087. (should
  8088. (equal "<08>--<16>"
  8089. (org-test-with-temp-text "<2012-03-29 Thu 8:30-16:40>"
  8090. (let ((org-display-custom-times t)
  8091. (org-time-stamp-custom-formats '("<%d>" . "<%H>")))
  8092. (org-timestamp-translate (org-element-context))))))
  8093. ;; Translate non-range timestamp.
  8094. (should
  8095. (equal "<29>"
  8096. (org-test-with-temp-text "<2012-03-29 Thu>"
  8097. (let ((org-display-custom-times t)
  8098. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8099. (org-timestamp-translate (org-element-context))))))
  8100. ;; Do not change `diary' timestamps.
  8101. (should
  8102. (equal "<%%(org-float t 4 2)>"
  8103. (org-test-with-temp-text "<%%(org-float t 4 2)>"
  8104. (let ((org-display-custom-times t)
  8105. (org-time-stamp-custom-formats '("<%d>" . "<%d>")))
  8106. (org-timestamp-translate (org-element-context)))))))
  8107. (ert-deftest test-org/timestamp-from-string ()
  8108. "Test `org-timestamp-from-string' specifications."
  8109. ;; Return nil if argument is not a valid Org timestamp.
  8110. (should-not (org-timestamp-from-string ""))
  8111. (should-not (org-timestamp-from-string nil))
  8112. (should-not (org-timestamp-from-string "<2012-03-29"))
  8113. ;; Otherwise, return a valid Org timestamp object.
  8114. (should
  8115. (string-match-p "<2012-03-29 .+>"
  8116. (org-element-interpret-data
  8117. (org-timestamp-from-string "<2012-03-29 Thu>"))))
  8118. (should
  8119. (string-match-p "[2014-03-04 .+]"
  8120. (org-element-interpret-data
  8121. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  8122. (ert-deftest test-org/timestamp-from-time ()
  8123. "Test `org-timestamp-from-time' specifications."
  8124. ;; Standard test.
  8125. (should
  8126. (string-match-p
  8127. "<2012-03-29 .+>"
  8128. (org-element-interpret-data
  8129. (org-timestamp-from-time
  8130. (org-time-string-to-time "<2012-03-29 Thu 16:40>")))))
  8131. ;; When optional argument WITH-TIME is non-nil, provide time
  8132. ;; information.
  8133. (should
  8134. (string-match-p
  8135. "<2012-03-29 .+ 16:40>"
  8136. (org-element-interpret-data
  8137. (org-timestamp-from-time
  8138. (org-time-string-to-time "<2012-03-29 Thu 16:40>")
  8139. t))))
  8140. ;; When optional argument INACTIVE is non-nil, return an inactive
  8141. ;; timestamp.
  8142. (should
  8143. (string-match-p
  8144. "[2012-03-29 .+]"
  8145. (org-element-interpret-data
  8146. (org-timestamp-from-time
  8147. (org-time-string-to-time "<2012-03-29 Thu 16:40>")
  8148. nil t)))))
  8149. (ert-deftest test-org/timestamp-to-time ()
  8150. "Test `org-timestamp-to-time' specifications."
  8151. (should
  8152. (equal "2014-03-04"
  8153. (format-time-string
  8154. "%Y-%m-%d"
  8155. (org-timestamp-to-time
  8156. (org-timestamp-from-string "<2014-03-04 Tue>")))))
  8157. (should
  8158. (equal "2014-03-04"
  8159. (format-time-string
  8160. "%Y-%m-%d"
  8161. (org-timestamp-to-time
  8162. (org-timestamp-from-string "[2014-03-04 Tue]")))))
  8163. (should
  8164. (equal "2012-03-29 08:30"
  8165. (format-time-string
  8166. "%Y-%m-%d %H:%M"
  8167. (org-timestamp-to-time
  8168. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")))))
  8169. (should
  8170. (equal "2012-03-29"
  8171. (format-time-string
  8172. "%Y-%m-%d"
  8173. (org-timestamp-to-time
  8174. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")))))
  8175. (should
  8176. (equal "2012-03-29"
  8177. (format-time-string
  8178. "%Y-%m-%d"
  8179. (org-timestamp-to-time
  8180. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")))))
  8181. ;; When optional argument END is non-nil, use end of date range or
  8182. ;; time range.
  8183. (should
  8184. (equal "2012-03-29 16:40"
  8185. (format-time-string
  8186. "%Y-%m-%d %H:%M"
  8187. (org-timestamp-to-time
  8188. (org-timestamp-from-string "<2012-03-29 Thu 08:30-16:40>")
  8189. t))))
  8190. (should
  8191. (equal "2014-03-04"
  8192. (format-time-string
  8193. "%Y-%m-%d"
  8194. (org-timestamp-to-time
  8195. (org-timestamp-from-string "<2012-03-29 Thu>--<2014-03-04 Tue>")
  8196. t))))
  8197. (should
  8198. (equal "2014-03-04"
  8199. (format-time-string
  8200. "%Y-%m-%d"
  8201. (org-timestamp-to-time
  8202. (org-timestamp-from-string "[2012-03-29 Thu]--[2014-03-04 Tue]")
  8203. t)))))
  8204. ;;; Yank and Kill
  8205. (ert-deftest test-org/paste-subtree ()
  8206. "Test `org-paste-subtree' specifications."
  8207. ;; Return an error if text to yank is not a set of subtrees.
  8208. (should-error (org-paste-subtree nil "Text"))
  8209. ;; Adjust level according to current one.
  8210. (should
  8211. (equal "* H\n* Text\n"
  8212. (org-test-with-temp-text "* H\n<point>"
  8213. (org-paste-subtree nil "* Text")
  8214. (buffer-string))))
  8215. (should
  8216. (equal "* H1\n** H2\n** Text\n"
  8217. (org-test-with-temp-text "* H1\n** H2\n<point>"
  8218. (org-paste-subtree nil "* Text")
  8219. (buffer-string))))
  8220. ;; When not on a heading, move to next heading before yanking.
  8221. (should
  8222. (equal "* H1\nParagraph\n* Text\n* H2"
  8223. (org-test-with-temp-text "* H1\n<point>Paragraph\n* H2"
  8224. (org-paste-subtree nil "* Text")
  8225. (buffer-string))))
  8226. ;; If point is between two headings, use the deepest level.
  8227. (should
  8228. (equal "* H1\n\n* Text\n* H2"
  8229. (org-test-with-temp-text "* H1\n<point>\n* H2"
  8230. (org-paste-subtree nil "* Text")
  8231. (buffer-string))))
  8232. (should
  8233. (equal "** H1\n\n** Text\n* H2"
  8234. (org-test-with-temp-text "** H1\n<point>\n* H2"
  8235. (org-paste-subtree nil "* Text")
  8236. (buffer-string))))
  8237. (should
  8238. (equal "* H1\n\n** Text\n** H2"
  8239. (org-test-with-temp-text "* H1\n<point>\n** H2"
  8240. (org-paste-subtree nil "* Text")
  8241. (buffer-string))))
  8242. ;; When on an empty heading, after the stars, deduce the new level
  8243. ;; from the number of stars.
  8244. (should
  8245. (equal "*** Text\n"
  8246. (org-test-with-temp-text "*** <point>"
  8247. (org-paste-subtree nil "* Text")
  8248. (buffer-string))))
  8249. ;; Optional argument LEVEL forces a level for the subtree.
  8250. (should
  8251. (equal "* H\n*** Text\n"
  8252. (org-test-with-temp-text "* H<point>"
  8253. (org-paste-subtree 3 "* Text")
  8254. (buffer-string)))))
  8255. (ert-deftest test-org/cut-and-paste-subtree ()
  8256. "Test `org-cut-subtree' and `org-paste-subtree'."
  8257. (should
  8258. (equal
  8259. "* Two
  8260. two
  8261. * One
  8262. "
  8263. (org-test-with-temp-text
  8264. "* One
  8265. <point>* Two
  8266. two
  8267. "
  8268. (call-interactively #'org-cut-subtree)
  8269. (goto-char (point-min))
  8270. (call-interactively #'org-paste-subtree)
  8271. (buffer-string))))
  8272. (should
  8273. (equal
  8274. "* One
  8275. * Two
  8276. "
  8277. (org-test-with-temp-text
  8278. "* One
  8279. <point>* Two
  8280. "
  8281. (call-interactively #'org-cut-subtree)
  8282. (backward-char)
  8283. (call-interactively #'org-paste-subtree)
  8284. (buffer-string)))))
  8285. (ert-deftest test-org/org--open-file-format-command ()
  8286. "Test `org--open-file-format-command' helper for `org-open-file'."
  8287. (let ((system-type 'gnu/linux)) ; Fix behavior of `shell-quote-argument'.
  8288. ;; No additional groups in `org-file-apps' key.
  8289. (let ((file "/file.pdf")
  8290. (pattern "\\.pdf\\'"))
  8291. (should
  8292. (equal "simple /file.pdf"
  8293. (and (string-match pattern file)
  8294. (org--open-file-format-command
  8295. "simple %s" file file (match-data)))))
  8296. (should
  8297. (equal "single-quotes /file.pdf"
  8298. (and (string-match pattern file)
  8299. (org--open-file-format-command
  8300. "single-quotes '%s'" file file (match-data)))))
  8301. (should
  8302. (equal "double-quotes /file.pdf"
  8303. (and (string-match pattern file)
  8304. (org--open-file-format-command
  8305. "double-quotes \"%s\"" file file (match-data)))))
  8306. (should
  8307. (equal "quotes 'mismatch \"/file.pdf'"
  8308. (and (string-match pattern file)
  8309. (org--open-file-format-command
  8310. "quotes 'mismatch \"%s'" file file (match-data)))))
  8311. (should
  8312. (equal "no subst"
  8313. (and (string-match pattern file)
  8314. (org--open-file-format-command
  8315. "no subst" file file (match-data)))))
  8316. (should
  8317. (equal "% literal percent 100% %s"
  8318. (and (string-match pattern file)
  8319. (org--open-file-format-command
  8320. "\\% literal percent 100\\% \\%s" file file (match-data)))))
  8321. (should
  8322. (equal "escape \"/file.pdf\" \\ more"
  8323. (and (string-match pattern file)
  8324. (org--open-file-format-command
  8325. ;; Second quote is not escaped.
  8326. "escape \\\"%s\" \\\\ more" file file (match-data)))))
  8327. (should
  8328. (equal "/file.pdf file at start"
  8329. (and (string-match pattern file)
  8330. (org--open-file-format-command
  8331. "%s file at start" file file (match-data)))))
  8332. (should
  8333. (equal "backslash-newline\n/file.pdf"
  8334. (and (string-match pattern file)
  8335. (org--open-file-format-command
  8336. "backslash-newline\\\n%s" file file (match-data))))))
  8337. ;; Anchors within target file.
  8338. (let ((file "/page-search.pdf")
  8339. (link "/page-search.pdf::10::some words")
  8340. (pattern "\\.pdf::\\([0-9]+\\)::\\(.*\\)\\'"))
  8341. (should
  8342. (equal "zathura --page 10 --find some\\ words /page-search.pdf"
  8343. (and (string-match pattern link)
  8344. (org--open-file-format-command
  8345. "zathura --page '%1' --find %2 \"%s\"" file link (match-data)))))
  8346. ;; Unused %2.
  8347. (should
  8348. (equal "firefox file:///page-search.pdf\\#page=10"
  8349. (and (string-match pattern link)
  8350. (org--open-file-format-command
  8351. "firefox file://%s\\\\#page=%1" file link (match-data)))))
  8352. (should
  8353. (equal "adjucent-subst /page-search.pdfsome\\ words10some\\ words"
  8354. (and (string-match pattern link)
  8355. (org--open-file-format-command
  8356. "adjucent-subst %s%2'%1'%2" file link (match-data))))))
  8357. ;; No more than 9 substitutions are supported.
  8358. (let ((file "/many.pdf")
  8359. (link "/many.pdf::one:2:3:4:5:6:7:8:9:a:b:c")
  8360. (pattern (concat "\\.pdf:"
  8361. (mapconcat (lambda (_) ":\\([^:]+\\)")
  8362. (number-sequence 1 12)
  8363. "")
  8364. "\\'")))
  8365. (should
  8366. (equal "overflow /many.pdf::one:2:3:4:5:6:7:8:9:one0:one1:one2"
  8367. (and (string-match pattern link)
  8368. (org--open-file-format-command
  8369. "overflow %s::%1:%2:%3:%4:%5:%6:%7:%8:%9:%10:%11:%12"
  8370. file link (match-data))))))
  8371. ;; Percent character in link fields does not cause any problem.
  8372. (let ((file "/file-%2.pdf")
  8373. (link "/file-%2.pdf::anchor-%3::search %1")
  8374. (pattern "\\.pdf::\\([^:]+\\)::\\(.+\\)\\'"))
  8375. (should
  8376. (equal "percents --find search\\ \\%1 file:///file-\\%2.pdf\\#anchor-\\%3"
  8377. (and (string-match pattern link)
  8378. (org--open-file-format-command
  8379. "percents --find %2 file://%s\\\\#%1"
  8380. file link (match-data))))))
  8381. ;; Errors.
  8382. (let ((file "/error.pdf")
  8383. (pattern "\\.pdf\\'"))
  8384. (let* ((err (should-error
  8385. (and (string-match pattern file)
  8386. (org--open-file-format-command
  8387. "trailing-percent %s %" file file (match-data)))
  8388. :type 'error))
  8389. (err-text (cadr err)))
  8390. (should-not (unless (and (stringp err-text)
  8391. (string-match-p "\\`Invalid format .*%" err-text))
  8392. err)))
  8393. (let* ((err (should-error
  8394. (and (string-match pattern file)
  8395. (org--open-file-format-command
  8396. "trailing-backslash %s \\" file file (match-data)))
  8397. :type 'error))
  8398. (err-text (cadr err)))
  8399. (should-not (unless (and (stringp err-text)
  8400. (string-match-p "\\`Invalid format .*\\\\" err-text))
  8401. err)))
  8402. (let* ((err (should-error
  8403. (and (string-match pattern file)
  8404. (org--open-file-format-command
  8405. "percent-newline %\n%s" file file (match-data)))
  8406. :type 'error))
  8407. (err-text (cadr err)))
  8408. (should-not (unless (and (stringp err-text)
  8409. (string-match-p "\\`Invalid format .*%\n" err-text))
  8410. err)))
  8411. ;; Mailcap escape for "%" is "\%", not "%%".
  8412. (let* ((err (should-error
  8413. (and (string-match pattern file)
  8414. (org--open-file-format-command
  8415. "percent-percent %s%%" file file (match-data)))
  8416. :type 'error))
  8417. (err-text (cadr err)))
  8418. (should-not (unless (and (stringp err-text)
  8419. (string-match-p "\\`Invalid format .*%%" err-text))
  8420. err)))
  8421. ;; Mailcap allows "%t" for MIME type, but Org has no such information.
  8422. (let* ((err (should-error
  8423. (and (string-match pattern file)
  8424. (org--open-file-format-command
  8425. "percent-t-unsupported --type '%t' %s" file file (match-data)))
  8426. :type 'error))
  8427. (err-text (cadr err)))
  8428. (should-not (unless (and (stringp err-text)
  8429. (string-match-p "\\`Invalid format .*%t" err-text))
  8430. err))))
  8431. ;; Optional regular expression groups have no point in `org-file-apps' patterns.
  8432. (let* ((file "/error.pdf")
  8433. (link "/error.pdf::1")
  8434. (pattern "\\.pdf::\\([^:]+\\)\\(?:::\\(.+\\)\\)?\\'")
  8435. (err (should-error
  8436. (and (string-match pattern link)
  8437. (org--open-file-format-command
  8438. "no-such-match --search %2 %s" file link (match-data)))
  8439. :type 'error))
  8440. (err-text (cadr err)))
  8441. (should-not (unless (and (stringp err-text)
  8442. (string-match-p "\\`Invalid format.*%2" err-text))
  8443. err)))))
  8444. (provide 'test-org)
  8445. ;;; test-org.el ends here