test-org.el 194 KB

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