org.el 950 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. (make-variable-buffer-local 'org-table-formula-constants-local)
  65. ;;;; Require other packages
  66. (eval-when-compile
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  85. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  86. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  87. ;;
  88. ;; In Org buffers, the value of `outline-regexp' is that of
  89. ;; `org-outline-regexp'. The only function still directly relying on
  90. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  91. ;; job when `orgstruct-mode' is active.
  92. (defvar org-outline-regexp "\\*+ "
  93. "Regexp to match Org headlines.")
  94. (defvar org-outline-regexp-bol "^\\*+ "
  95. "Regexp to match Org headlines.
  96. This is similar to `org-outline-regexp' but additionally makes
  97. sure that we are at the beginning of the line.")
  98. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  99. "Matches a headline, putting stars and text into groups.
  100. Stars are put in group 1 and the trimmed body in group 2.")
  101. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  102. (unless (boundp 'calendar-view-holidays-initially-flag)
  103. (org-defvaralias 'calendar-view-holidays-initially-flag
  104. 'view-calendar-holidays-initially))
  105. (unless (boundp 'calendar-view-diary-initially-flag)
  106. (org-defvaralias 'calendar-view-diary-initially-flag
  107. 'view-diary-entries-initially))
  108. (unless (boundp 'diary-fancy-buffer)
  109. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  110. (declare-function org-add-archive-files "org-archive" (files))
  111. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  112. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  113. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  114. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  115. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  116. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  117. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  118. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  119. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  120. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  121. (declare-function org-clock-update-time-maybe "org-clock" ())
  122. (declare-function org-clocktable-shift "org-clock" (dir n))
  123. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  124. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  125. (declare-function orgtbl-mode "org-table" (&optional arg))
  126. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  127. (declare-function org-beamer-mode "ox-beamer" ())
  128. (declare-function org-table-blank-field "org-table" ())
  129. (declare-function org-table-edit-field "org-table" (arg))
  130. (declare-function org-table-insert-row "org-table" (&optional arg))
  131. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  132. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-add-archive-files "org-archive" (files))
  135. (declare-function org-id-find-id-file "org-id" (id))
  136. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  137. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  138. (declare-function org-agenda-redo "org-agenda" (&optional all))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-end "org-table" (&optional table-type))
  143. (declare-function org-table-end-of-field "org-table" (&optional n))
  144. (declare-function org-table-insert-row "org-table" (&optional arg))
  145. (declare-function org-table-paste-rectangle "org-table" ())
  146. (declare-function org-table-maybe-eval-formula "org-table" ())
  147. (declare-function org-table-maybe-recalculate-line "org-table" ())
  148. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  149. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  150. (declare-function org-element-at-point "org-element" ())
  151. (declare-function org-element-cache-reset "org-element" (&optional all))
  152. (declare-function org-element-cache-refresh "org-element" (pos))
  153. (declare-function org-element-contents "org-element" (element))
  154. (declare-function org-element-context "org-element" (&optional element))
  155. (declare-function org-element-interpret-data "org-element"
  156. (data &optional parent))
  157. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  158. (declare-function org-element-parse-buffer "org-element"
  159. (&optional granularity visible-only))
  160. (declare-function org-element-property "org-element" (property element))
  161. (declare-function org-element-put-property "org-element"
  162. (element property value))
  163. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  164. (declare-function org-element-parse-buffer "org-element"
  165. (&optional granularity visible-only))
  166. (declare-function org-element-type "org-element" (element))
  167. (declare-function org-element-update-syntax "org-element" ())
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. ;; Tangle-file traversal returns reversed list of tangled files
  216. ;; and we want to evaluate the first target.
  217. (setq exported-file
  218. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  219. (message "%s %s"
  220. (if compile
  221. (progn (byte-compile-file exported-file 'load)
  222. "Compiled and loaded")
  223. (progn (load-file exported-file) "Loaded"))
  224. exported-file)))
  225. (defcustom org-babel-load-languages '((emacs-lisp . t))
  226. "Languages which can be evaluated in Org-mode buffers.
  227. This list can be used to load support for any of the languages
  228. below, note that each language will depend on a different set of
  229. system executables and/or Emacs modes. When a language is
  230. \"loaded\", then code blocks in that language can be evaluated
  231. with `org-babel-execute-src-block' bound by default to C-c
  232. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  233. be set to remove code block evaluation from the C-c C-c
  234. keybinding. By default only Emacs Lisp (which has no
  235. requirements) is loaded."
  236. :group 'org-babel
  237. :set 'org-babel-do-load-languages
  238. :version "24.1"
  239. :type '(alist :tag "Babel Languages"
  240. :key-type
  241. (choice
  242. (const :tag "Awk" awk)
  243. (const :tag "C" C)
  244. (const :tag "R" R)
  245. (const :tag "Asymptote" asymptote)
  246. (const :tag "Calc" calc)
  247. (const :tag "Clojure" clojure)
  248. (const :tag "CSS" css)
  249. (const :tag "Ditaa" ditaa)
  250. (const :tag "Dot" dot)
  251. (const :tag "Emacs Lisp" emacs-lisp)
  252. (const :tag "Forth" forth)
  253. (const :tag "Fortran" fortran)
  254. (const :tag "Gnuplot" gnuplot)
  255. (const :tag "Haskell" haskell)
  256. (const :tag "IO" io)
  257. (const :tag "J" J)
  258. (const :tag "Java" java)
  259. (const :tag "Javascript" js)
  260. (const :tag "LaTeX" latex)
  261. (const :tag "Ledger" ledger)
  262. (const :tag "Lilypond" lilypond)
  263. (const :tag "Lisp" lisp)
  264. (const :tag "Makefile" makefile)
  265. (const :tag "Maxima" maxima)
  266. (const :tag "Matlab" matlab)
  267. (const :tag "Mscgen" mscgen)
  268. (const :tag "Ocaml" ocaml)
  269. (const :tag "Octave" octave)
  270. (const :tag "Org" org)
  271. (const :tag "Perl" perl)
  272. (const :tag "Pico Lisp" picolisp)
  273. (const :tag "PlantUML" plantuml)
  274. (const :tag "Python" python)
  275. (const :tag "Ruby" ruby)
  276. (const :tag "Sass" sass)
  277. (const :tag "Scala" scala)
  278. (const :tag "Scheme" scheme)
  279. (const :tag "Screen" screen)
  280. (const :tag "Shell Script" shell)
  281. (const :tag "Shen" shen)
  282. (const :tag "Sql" sql)
  283. (const :tag "Sqlite" sqlite)
  284. (const :tag "ebnf2ps" ebnf2ps))
  285. :value-type (boolean :tag "Activate" :value t)))
  286. ;;;; Customization variables
  287. (defcustom org-clone-delete-id nil
  288. "Remove ID property of clones of a subtree.
  289. When non-nil, clones of a subtree don't inherit the ID property.
  290. Otherwise they inherit the ID property with a new unique
  291. identifier."
  292. :type 'boolean
  293. :version "24.1"
  294. :group 'org-id)
  295. ;;; Version
  296. (org-check-version)
  297. ;;;###autoload
  298. (defun org-version (&optional here full message)
  299. "Show the org-mode version.
  300. Interactively, or when MESSAGE is non-nil, show it in echo area.
  301. With prefix argument, or when HERE is non-nil, insert it at point.
  302. In non-interactive uses, a reduced version string is output unless
  303. FULL is given."
  304. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  305. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  306. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  307. (load-suffixes (list ".el"))
  308. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  309. (org-trash (or
  310. (and (fboundp 'org-release) (fboundp 'org-git-version))
  311. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  312. (load-suffixes save-load-suffixes)
  313. (org-version (org-release))
  314. (git-version (org-git-version))
  315. (version (format "Org-mode version %s (%s @ %s)"
  316. org-version
  317. git-version
  318. (if org-install-dir
  319. (if (string= org-dir org-install-dir)
  320. org-install-dir
  321. (concat "mixed installation! " org-install-dir " and " org-dir))
  322. "org-loaddefs.el can not be found!")))
  323. (version1 (if full version org-version)))
  324. (when here (insert version1))
  325. (when message (message "%s" version1))
  326. version1))
  327. (defconst org-version (org-version))
  328. ;;; Syntax Constants
  329. ;;;; Block
  330. (defconst org-block-regexp
  331. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  332. "Regular expression for hiding blocks.")
  333. (defconst org-dblock-start-re
  334. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  335. "Matches the start line of a dynamic block, with parameters.")
  336. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  337. "Matches the end of a dynamic block.")
  338. ;;;; Clock and Planning
  339. (defconst org-clock-string "CLOCK:"
  340. "String used as prefix for timestamps clocking work hours on an item.")
  341. (defvar org-closed-string "CLOSED:"
  342. "String used as the prefix for timestamps logging closing a TODO entry.")
  343. (defvar org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].")
  348. (defvar org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].")
  353. (defconst org-ds-keyword-length
  354. (+ 2
  355. (apply #'max
  356. (mapcar #'length
  357. (list org-deadline-string org-scheduled-string
  358. org-clock-string org-closed-string))))
  359. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  360. (defconst org-planning-line-re
  361. (concat "^[ \t]*"
  362. (regexp-opt
  363. (list org-closed-string org-deadline-string org-scheduled-string)
  364. t))
  365. "Matches a line with planning info.
  366. Matched keyword is in group 1.")
  367. (defconst org-clock-line-re
  368. (concat "^[ \t]*" org-clock-string)
  369. "Matches a line with clock info.")
  370. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  371. "Matches the DEADLINE keyword.")
  372. (defconst org-deadline-time-regexp
  373. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  374. "Matches the DEADLINE keyword together with a time stamp.")
  375. (defconst org-deadline-time-hour-regexp
  376. (concat "\\<" org-deadline-string
  377. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  378. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  379. (defconst org-deadline-line-regexp
  380. (concat "\\<\\(" org-deadline-string "\\).*")
  381. "Matches the DEADLINE keyword and the rest of the line.")
  382. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  383. "Matches the SCHEDULED keyword.")
  384. (defconst org-scheduled-time-regexp
  385. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  386. "Matches the SCHEDULED keyword together with a time stamp.")
  387. (defconst org-scheduled-time-hour-regexp
  388. (concat "\\<" org-scheduled-string
  389. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  390. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  391. (defconst org-closed-time-regexp
  392. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  393. "Matches the CLOSED keyword together with a time stamp.")
  394. (defconst org-keyword-time-regexp
  395. (concat "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string
  398. org-clock-string)
  399. t)
  400. " *[[<]\\([^]>]+\\)[]>]")
  401. "Matches any of the 4 keywords, together with the time stamp.")
  402. (defconst org-keyword-time-not-clock-regexp
  403. (concat
  404. "\\<"
  405. (regexp-opt
  406. (list org-scheduled-string org-deadline-string org-closed-string) t)
  407. " *[[<]\\([^]>]+\\)[]>]")
  408. "Matches any of the 3 keywords, together with the time stamp.")
  409. (defconst org-maybe-keyword-time-regexp
  410. (concat "\\(\\<"
  411. (regexp-opt
  412. (list org-scheduled-string org-deadline-string org-closed-string
  413. org-clock-string)
  414. t)
  415. "\\)?"
  416. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  417. "\\|"
  418. "<%%([^\r\n>]*>\\)")
  419. "Matches a timestamp, possibly preceded by a keyword.")
  420. (defconst org-all-time-keywords
  421. (mapcar (lambda (w) (substring w 0 -1))
  422. (list org-scheduled-string org-deadline-string
  423. org-clock-string org-closed-string))
  424. "List of time keywords.")
  425. ;;;; Drawer
  426. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  427. "Matches first or last line of a hidden block.
  428. Group 1 contains drawer's name or \"END\".")
  429. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  430. "Regular expression matching the first line of a property drawer.")
  431. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  432. "Regular expression matching the last line of a property drawer.")
  433. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  434. "Regular expression matching the first line of a clock drawer.")
  435. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  436. "Regular expression matching the last line of a clock drawer.")
  437. (defconst org-property-drawer-re
  438. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  439. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  440. "[ \t]*:END:[ \t]*$")
  441. "Matches an entire property drawer.")
  442. (defconst org-clock-drawer-re
  443. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  444. org-clock-drawer-end-re "\\)\n?")
  445. "Matches an entire clock drawer.")
  446. ;;;; Headline
  447. (defconst org-heading-keyword-regexp-format
  448. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  449. "Printf format for a regexp matching a headline with some keyword.
  450. This regexp will match the headline of any node which has the
  451. exact keyword that is put into the format. The keyword isn't in
  452. any group by default, but the stars and the body are.")
  453. (defconst org-heading-keyword-maybe-regexp-format
  454. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  455. "Printf format for a regexp matching a headline, possibly with some keyword.
  456. This regexp can match any headline with the specified keyword, or
  457. without a keyword. The keyword isn't in any group by default,
  458. but the stars and the body are.")
  459. (defconst org-archive-tag "ARCHIVE"
  460. "The tag that marks a subtree as archived.
  461. An archived subtree does not open during visibility cycling, and does
  462. not contribute to the agenda listings.")
  463. (defconst org-comment-string "COMMENT"
  464. "Entries starting with this keyword will never be exported.
  465. An entry can be toggled between COMMENT and normal with
  466. \\[org-toggle-comment].")
  467. ;;;; LaTeX Environments and Fragments
  468. (defconst org-latex-regexps
  469. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  470. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  471. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  472. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  473. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  474. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  475. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  476. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  477. "Regular expressions for matching embedded LaTeX.")
  478. ;;;; Node Property
  479. (defconst org-effort-property "Effort"
  480. "The property that is being used to keep track of effort estimates.
  481. Effort estimates given in this property need to have the format H:MM.")
  482. ;;;; Table
  483. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  484. "Detect an org-type or table-type table.")
  485. (defconst org-table-line-regexp "^[ \t]*|"
  486. "Detect an org-type table line.")
  487. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  488. "Detect an org-type table line.")
  489. (defconst org-table-hline-regexp "^[ \t]*|-"
  490. "Detect an org-type table hline.")
  491. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  492. "Detect a table-type table hline.")
  493. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  494. "Detect the first line outside a table when searching from within it.
  495. This works for both table types.")
  496. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  497. "Detect a #+TBLFM line.")
  498. ;;;; Timestamp
  499. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  500. "Regular expression for fast time stamp matching.")
  501. (defconst org-ts-regexp-inactive
  502. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  503. "Regular expression for fast inactive time stamp matching.")
  504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  505. "Regular expression for fast time stamp matching.")
  506. (defconst org-ts-regexp0
  507. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  508. "Regular expression matching time strings for analysis.
  509. This one does not require the space after the date, so it can be used
  510. on a string that terminates immediately after the date.")
  511. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  512. "Regular expression matching time strings for analysis.")
  513. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  514. "Regular expression matching time stamps, with groups.")
  515. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  516. "Regular expression matching time stamps (also [..]), with groups.")
  517. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tr-regexp-both
  520. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  521. "Regular expression matching a time stamp range.")
  522. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  523. org-ts-regexp "\\)?")
  524. "Regular expression matching a time stamp or time stamp range.")
  525. (defconst org-tsr-regexp-both
  526. (concat org-ts-regexp-both "\\(--?-?"
  527. org-ts-regexp-both "\\)?")
  528. "Regular expression matching a time stamp or time stamp range.
  529. The time stamps may be either active or inactive.")
  530. (defconst org-repeat-re
  531. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  532. "Regular expression for specifying repeated events.
  533. After a match, group 1 contains the repeat expression.")
  534. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  535. "Formats for `format-time-string' which are used for time stamps.")
  536. ;;; The custom variables
  537. (defgroup org nil
  538. "Outline-based notes management and organizer."
  539. :tag "Org"
  540. :group 'outlines
  541. :group 'calendar)
  542. (defcustom org-mode-hook nil
  543. "Mode hook for Org-mode, run after the mode was turned on."
  544. :group 'org
  545. :type 'hook)
  546. (defcustom org-load-hook nil
  547. "Hook that is run after org.el has been loaded."
  548. :group 'org
  549. :type 'hook)
  550. (defcustom org-log-buffer-setup-hook nil
  551. "Hook that is run after an Org log buffer is created."
  552. :group 'org
  553. :version "24.1"
  554. :type 'hook)
  555. (defvar org-modules) ; defined below
  556. (defvar org-modules-loaded nil
  557. "Have the modules been loaded already?")
  558. (defun org-load-modules-maybe (&optional force)
  559. "Load all extensions listed in `org-modules'."
  560. (when (or force (not org-modules-loaded))
  561. (mapc (lambda (ext)
  562. (condition-case nil (require ext)
  563. (error (message "Problems while trying to load feature `%s'" ext))))
  564. org-modules)
  565. (setq org-modules-loaded t)))
  566. (defun org-set-modules (var value)
  567. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  568. (set var value)
  569. (when (featurep 'org)
  570. (org-load-modules-maybe 'force)
  571. (org-element-cache-reset 'all)))
  572. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  573. "Modules that should always be loaded together with org.el.
  574. If a description starts with <C>, the file is not part of Emacs
  575. and loading it will require that you have downloaded and properly
  576. installed the Org mode distribution.
  577. You can also use this system to load external packages (i.e. neither Org
  578. core modules, nor modules from the CONTRIB directory). Just add symbols
  579. to the end of the list. If the package is called org-xyz.el, then you need
  580. to add the symbol `xyz', and the package must have a call to:
  581. \(provide 'org-xyz)
  582. For export specific modules, see also `org-export-backends'."
  583. :group 'org
  584. :set 'org-set-modules
  585. :version "24.4"
  586. :package-version '(Org . "8.0")
  587. :type
  588. '(set :greedy t
  589. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  590. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  591. (const :tag " crypt: Encryption of subtrees" org-crypt)
  592. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  593. (const :tag " docview: Links to doc-view buffers" org-docview)
  594. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  595. (const :tag " habit: Track your consistency with habits" org-habit)
  596. (const :tag " id: Global IDs for identifying entries" org-id)
  597. (const :tag " info: Links to Info nodes" org-info)
  598. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  599. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  600. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  601. (const :tag " mouse: Additional mouse support" org-mouse)
  602. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  603. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  604. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  605. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  606. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  607. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  608. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  609. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  610. (const :tag "C collector: Collect properties into tables" org-collector)
  611. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  612. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  613. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  614. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  615. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  616. (const :tag "C eval: Include command output as text" org-eval)
  617. (const :tag "C eww: Store link to url of eww" org-eww)
  618. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  619. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  620. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  621. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  622. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  623. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  624. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  625. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  626. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  627. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  628. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  629. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  630. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  631. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  632. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  633. (const :tag "C registry: A registry for Org-mode links" org-registry)
  634. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  635. (const :tag "C secretary: Team management with org-mode" org-secretary)
  636. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  637. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  638. (const :tag "C track: Keep up with Org-mode development" org-track)
  639. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  640. (const :tag "C vm: Links to VM folders/messages" org-vm)
  641. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  642. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  643. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  644. (defvar org-export--registered-backends) ; From ox.el.
  645. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  646. (declare-function org-export-backend-name "ox" (backend))
  647. (defcustom org-export-backends '(ascii html icalendar latex)
  648. "List of export back-ends that should be always available.
  649. If a description starts with <C>, the file is not part of Emacs
  650. and loading it will require that you have downloaded and properly
  651. installed the Org mode distribution.
  652. Unlike to `org-modules', libraries in this list will not be
  653. loaded along with Org, but only once the export framework is
  654. needed.
  655. This variable needs to be set before org.el is loaded. If you
  656. need to make a change while Emacs is running, use the customize
  657. interface or run the following code, where VAL stands for the new
  658. value of the variable, after updating it:
  659. \(progn
  660. \(setq org-export--registered-backends
  661. \(org-remove-if-not
  662. \(lambda (backend)
  663. \(let ((name (org-export-backend-name backend)))
  664. \(or (memq name val)
  665. \(catch 'parentp
  666. \(dolist (b val)
  667. \(and (org-export-derived-backend-p b name)
  668. \(throw 'parentp t)))))))
  669. org-export--registered-backends))
  670. \(let ((new-list (mapcar 'org-export-backend-name
  671. org-export--registered-backends)))
  672. \(dolist (backend val)
  673. \(cond
  674. \((not (load (format \"ox-%s\" backend) t t))
  675. \(message \"Problems while trying to load export back-end `%s'\"
  676. backend))
  677. \((not (memq backend new-list)) (push backend new-list))))
  678. \(set-default 'org-export-backends new-list)))
  679. Adding a back-end to this list will also pull the back-end it
  680. depends on, if any."
  681. :group 'org
  682. :group 'org-export
  683. :version "24.4"
  684. :package-version '(Org . "8.0")
  685. :initialize 'custom-initialize-set
  686. :set (lambda (var val)
  687. (if (not (featurep 'ox)) (set-default var val)
  688. ;; Any back-end not required anymore (not present in VAL and not
  689. ;; a parent of any back-end in the new value) is removed from the
  690. ;; list of registered back-ends.
  691. (setq org-export--registered-backends
  692. (org-remove-if-not
  693. (lambda (backend)
  694. (let ((name (org-export-backend-name backend)))
  695. (or (memq name val)
  696. (catch 'parentp
  697. (dolist (b val)
  698. (and (org-export-derived-backend-p b name)
  699. (throw 'parentp t)))))))
  700. org-export--registered-backends))
  701. ;; Now build NEW-LIST of both new back-ends and required
  702. ;; parents.
  703. (let ((new-list (mapcar 'org-export-backend-name
  704. org-export--registered-backends)))
  705. (dolist (backend val)
  706. (cond
  707. ((not (load (format "ox-%s" backend) t t))
  708. (message "Problems while trying to load export back-end `%s'"
  709. backend))
  710. ((not (memq backend new-list)) (push backend new-list))))
  711. ;; Set VAR to that list with fixed dependencies.
  712. (set-default var new-list))))
  713. :type '(set :greedy t
  714. (const :tag " ascii Export buffer to ASCII format" ascii)
  715. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  716. (const :tag " html Export buffer to HTML format" html)
  717. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  718. (const :tag " latex Export buffer to LaTeX format" latex)
  719. (const :tag " man Export buffer to MAN format" man)
  720. (const :tag " md Export buffer to Markdown format" md)
  721. (const :tag " odt Export buffer to ODT format" odt)
  722. (const :tag " org Export buffer to Org format" org)
  723. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  724. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  725. (const :tag "C deck Export buffer to deck.js presentations" deck)
  726. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  727. (const :tag "C groff Export buffer to Groff format" groff)
  728. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  729. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  730. (const :tag "C s5 Export buffer to s5 presentations" s5)
  731. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  732. (eval-after-load 'ox
  733. '(mapc
  734. (lambda (backend)
  735. (condition-case nil (require (intern (format "ox-%s" backend)))
  736. (error (message "Problems while trying to load export back-end `%s'"
  737. backend))))
  738. org-export-backends))
  739. (defcustom org-support-shift-select nil
  740. "Non-nil means make shift-cursor commands select text when possible.
  741. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  742. start selecting a region, or enlarge regions started in this way.
  743. In Org-mode, in special contexts, these same keys are used for
  744. other purposes, important enough to compete with shift selection.
  745. Org tries to balance these needs by supporting `shift-select-mode'
  746. outside these special contexts, under control of this variable.
  747. The default of this variable is nil, to avoid confusing behavior. Shifted
  748. cursor keys will then execute Org commands in the following contexts:
  749. - on a headline, changing TODO state (left/right) and priority (up/down)
  750. - on a time stamp, changing the time
  751. - in a plain list item, changing the bullet type
  752. - in a property definition line, switching between allowed values
  753. - in the BEGIN line of a clock table (changing the time block).
  754. Outside these contexts, the commands will throw an error.
  755. When this variable is t and the cursor is not in a special
  756. context, Org-mode will support shift-selection for making and
  757. enlarging regions. To make this more effective, the bullet
  758. cycling will no longer happen anywhere in an item line, but only
  759. if the cursor is exactly on the bullet.
  760. If you set this variable to the symbol `always', then the keys
  761. will not be special in headlines, property lines, and item lines,
  762. to make shift selection work there as well. If this is what you
  763. want, you can use the following alternative commands: `C-c C-t'
  764. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  765. can be used to switch TODO sets, `C-c -' to cycle item bullet
  766. types, and properties can be edited by hand or in column view.
  767. However, when the cursor is on a timestamp, shift-cursor commands
  768. will still edit the time stamp - this is just too good to give up.
  769. XEmacs user should have this variable set to nil, because
  770. `shift-select-mode' is in Emacs 23 or later only."
  771. :group 'org
  772. :type '(choice
  773. (const :tag "Never" nil)
  774. (const :tag "When outside special context" t)
  775. (const :tag "Everywhere except timestamps" always)))
  776. (defcustom org-loop-over-headlines-in-active-region nil
  777. "Shall some commands act upon headlines in the active region?
  778. When set to `t', some commands will be performed in all headlines
  779. within the active region.
  780. When set to `start-level', some commands will be performed in all
  781. headlines within the active region, provided that these headlines
  782. are of the same level than the first one.
  783. When set to a string, those commands will be performed on the
  784. matching headlines within the active region. Such string must be
  785. a tags/property/todo match as it is used in the agenda tags view.
  786. The list of commands is: `org-schedule', `org-deadline',
  787. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  788. `org-archive-to-archive-sibling'. The archiving commands skip
  789. already archived entries."
  790. :type '(choice (const :tag "Don't loop" nil)
  791. (const :tag "All headlines in active region" t)
  792. (const :tag "In active region, headlines at the same level than the first one" start-level)
  793. (string :tag "Tags/Property/Todo matcher"))
  794. :version "24.1"
  795. :group 'org-todo
  796. :group 'org-archive)
  797. (defgroup org-startup nil
  798. "Options concerning startup of Org-mode."
  799. :tag "Org Startup"
  800. :group 'org)
  801. (defcustom org-startup-folded t
  802. "Non-nil means entering Org-mode will switch to OVERVIEW.
  803. This can also be configured on a per-file basis by adding one of
  804. the following lines anywhere in the buffer:
  805. #+STARTUP: fold (or `overview', this is equivalent)
  806. #+STARTUP: nofold (or `showall', this is equivalent)
  807. #+STARTUP: content
  808. #+STARTUP: showeverything
  809. By default, this option is ignored when Org opens agenda files
  810. for the first time. If you want the agenda to honor the startup
  811. option, set `org-agenda-inhibit-startup' to nil."
  812. :group 'org-startup
  813. :type '(choice
  814. (const :tag "nofold: show all" nil)
  815. (const :tag "fold: overview" t)
  816. (const :tag "content: all headlines" content)
  817. (const :tag "show everything, even drawers" showeverything)))
  818. (defcustom org-startup-truncated t
  819. "Non-nil means entering Org-mode will set `truncate-lines'.
  820. This is useful since some lines containing links can be very long and
  821. uninteresting. Also tables look terrible when wrapped."
  822. :group 'org-startup
  823. :type 'boolean)
  824. (defcustom org-startup-indented nil
  825. "Non-nil means turn on `org-indent-mode' on startup.
  826. This can also be configured on a per-file basis by adding one of
  827. the following lines anywhere in the buffer:
  828. #+STARTUP: indent
  829. #+STARTUP: noindent"
  830. :group 'org-structure
  831. :type '(choice
  832. (const :tag "Not" nil)
  833. (const :tag "Globally (slow on startup in large files)" t)))
  834. (defcustom org-use-sub-superscripts t
  835. "Non-nil means interpret \"_\" and \"^\" for display.
  836. If you want to control how Org exports those characters, see
  837. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  838. used to be an alias for `org-export-with-sub-superscripts' in
  839. Org <8.0, it is not anymore.
  840. When this option is turned on, you can use TeX-like syntax for
  841. sub- and superscripts within the buffer. Several characters after
  842. \"_\" or \"^\" will be considered as a single item - so grouping
  843. with {} is normally not needed. For example, the following things
  844. will be parsed as single sub- or superscripts:
  845. 10^24 or 10^tau several digits will be considered 1 item.
  846. 10^-12 or 10^-tau a leading sign with digits or a word
  847. x^2-y^3 will be read as x^2 - y^3, because items are
  848. terminated by almost any nonword/nondigit char.
  849. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  850. Still, ambiguity is possible. So when in doubt, use {} to enclose
  851. the sub/superscript. If you set this variable to the symbol `{}',
  852. the braces are *required* in order to trigger interpretations as
  853. sub/superscript. This can be helpful in documents that need \"_\"
  854. frequently in plain text."
  855. :group 'org-startup
  856. :version "24.4"
  857. :package-version '(Org . "8.0")
  858. :type '(choice
  859. (const :tag "Always interpret" t)
  860. (const :tag "Only with braces" {})
  861. (const :tag "Never interpret" nil)))
  862. (defcustom org-startup-with-beamer-mode nil
  863. "Non-nil means turn on `org-beamer-mode' on startup.
  864. This can also be configured on a per-file basis by adding one of
  865. the following lines anywhere in the buffer:
  866. #+STARTUP: beamer"
  867. :group 'org-startup
  868. :version "24.1"
  869. :type 'boolean)
  870. (defcustom org-startup-align-all-tables nil
  871. "Non-nil means align all tables when visiting a file.
  872. This is useful when the column width in tables is forced with <N> cookies
  873. in table fields. Such tables will look correct only after the first re-align.
  874. This can also be configured on a per-file basis by adding one of
  875. the following lines anywhere in the buffer:
  876. #+STARTUP: align
  877. #+STARTUP: noalign"
  878. :group 'org-startup
  879. :type 'boolean)
  880. (defcustom org-startup-with-inline-images nil
  881. "Non-nil means show inline images when loading a new Org file.
  882. This can also be configured on a per-file basis by adding one of
  883. the following lines anywhere in the buffer:
  884. #+STARTUP: inlineimages
  885. #+STARTUP: noinlineimages"
  886. :group 'org-startup
  887. :version "24.1"
  888. :type 'boolean)
  889. (defcustom org-startup-with-latex-preview nil
  890. "Non-nil means preview LaTeX fragments when loading a new Org file.
  891. This can also be configured on a per-file basis by adding one of
  892. the following lines anywhere in the buffer:
  893. #+STARTUP: latexpreview
  894. #+STARTUP: nolatexpreview"
  895. :group 'org-startup
  896. :version "24.4"
  897. :package-version '(Org . "8.0")
  898. :type 'boolean)
  899. (defcustom org-insert-mode-line-in-empty-file nil
  900. "Non-nil means insert the first line setting Org-mode in empty files.
  901. When the function `org-mode' is called interactively in an empty file, this
  902. normally means that the file name does not automatically trigger Org-mode.
  903. To ensure that the file will always be in Org-mode in the future, a
  904. line enforcing Org-mode will be inserted into the buffer, if this option
  905. has been set."
  906. :group 'org-startup
  907. :type 'boolean)
  908. (defcustom org-replace-disputed-keys nil
  909. "Non-nil means use alternative key bindings for some keys.
  910. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  911. These keys are also used by other packages like shift-selection-mode'
  912. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  913. If you want to use Org-mode together with one of these other modes,
  914. or more generally if you would like to move some Org-mode commands to
  915. other keys, set this variable and configure the keys with the variable
  916. `org-disputed-keys'.
  917. This option is only relevant at load-time of Org-mode, and must be set
  918. *before* org.el is loaded. Changing it requires a restart of Emacs to
  919. become effective."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (defcustom org-use-extra-keys nil
  923. "Non-nil means use extra key sequence definitions for certain commands.
  924. This happens automatically if you run XEmacs or if `window-system'
  925. is nil. This variable lets you do the same manually. You must
  926. set it before loading org.
  927. Example: on Carbon Emacs 22 running graphically, with an external
  928. keyboard on a Powerbook, the default way of setting M-left might
  929. not work for either Alt or ESC. Setting this variable will make
  930. it work for ESC."
  931. :group 'org-startup
  932. :type 'boolean)
  933. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  934. (defcustom org-disputed-keys
  935. '(([(shift up)] . [(meta p)])
  936. ([(shift down)] . [(meta n)])
  937. ([(shift left)] . [(meta -)])
  938. ([(shift right)] . [(meta +)])
  939. ([(control shift right)] . [(meta shift +)])
  940. ([(control shift left)] . [(meta shift -)]))
  941. "Keys for which Org-mode and other modes compete.
  942. This is an alist, cars are the default keys, second element specifies
  943. the alternative to use when `org-replace-disputed-keys' is t.
  944. Keys can be specified in any syntax supported by `define-key'.
  945. The value of this option takes effect only at Org-mode's startup,
  946. therefore you'll have to restart Emacs to apply it after changing."
  947. :group 'org-startup
  948. :type 'alist)
  949. (defun org-key (key)
  950. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  951. Or return the original if not disputed.
  952. Also apply the translations defined in `org-xemacs-key-equivalents'."
  953. (when org-replace-disputed-keys
  954. (let* ((nkey (key-description key))
  955. (x (org-find-if (lambda (x)
  956. (equal (key-description (car x)) nkey))
  957. org-disputed-keys)))
  958. (setq key (if x (cdr x) key))))
  959. (when (featurep 'xemacs)
  960. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  961. key)
  962. (defun org-find-if (predicate seq)
  963. (catch 'exit
  964. (while seq
  965. (if (funcall predicate (car seq))
  966. (throw 'exit (car seq))
  967. (pop seq)))))
  968. (defun org-defkey (keymap key def)
  969. "Define a key, possibly translated, as returned by `org-key'."
  970. (define-key keymap (org-key key) def))
  971. (defcustom org-ellipsis nil
  972. "The ellipsis to use in the Org-mode outline.
  973. When nil, just use the standard three dots.
  974. When a string, use that string instead.
  975. When a face, use the standard 3 dots, but with the specified face.
  976. The change affects only Org-mode (which will then use its own display table).
  977. Changing this requires executing \\[org-mode] in a buffer to become
  978. effective."
  979. :group 'org-startup
  980. :type '(choice (const :tag "Default" nil)
  981. (face :tag "Face" :value org-warning)
  982. (string :tag "String" :value "...#")))
  983. (defvar org-display-table nil
  984. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  985. (defgroup org-keywords nil
  986. "Keywords in Org-mode."
  987. :tag "Org Keywords"
  988. :group 'org)
  989. (defcustom org-closed-keep-when-no-todo nil
  990. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  991. :group 'org-todo
  992. :group 'org-keywords
  993. :version "24.4"
  994. :package-version '(Org . "8.0")
  995. :type 'boolean)
  996. (defgroup org-structure nil
  997. "Options concerning the general structure of Org-mode files."
  998. :tag "Org Structure"
  999. :group 'org)
  1000. (defgroup org-reveal-location nil
  1001. "Options about how to make context of a location visible."
  1002. :tag "Org Reveal Location"
  1003. :group 'org-structure)
  1004. (defcustom org-show-context-detail '((isearch . lineage)
  1005. (bookmark-jump . lineage)
  1006. (default . ancestors))
  1007. "Alist between context and visibility span when revealing a location.
  1008. \\<org-mode-map>Some actions may move point into invisible
  1009. locations. As a consequence, Org always expose a neighborhood
  1010. around point. How much is shown depends on the initial action,
  1011. or context. Valid contexts are
  1012. agenda when exposing an entry from the agenda
  1013. org-goto when using the command `org-goto' (\\[org-goto])
  1014. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1015. tags-tree when constructing a sparse tree based on tags matches
  1016. link-search when exposing search matches associated with a link
  1017. mark-goto when exposing the jump goal of a mark
  1018. bookmark-jump when exposing a bookmark location
  1019. isearch when exiting from an incremental search
  1020. default default for all contexts not set explicitly
  1021. Allowed visibility spans are
  1022. minimal show current headline; if point is not on headline,
  1023. also show entry
  1024. local show current headline, entry and next headline
  1025. ancestors show current headline and its direct ancestors; if
  1026. point is not on headline, also show entry
  1027. lineage show current headline, its direct ancestors and all
  1028. their children; if point is not on headline, also show
  1029. entry and first child
  1030. tree show current headline, its direct ancestors and all
  1031. their children; if point is not on headline, also show
  1032. entry and all children
  1033. canonical show current headline, its direct ancestors along with
  1034. their entries and children; if point is not located on
  1035. the headline, also show current entry and all children
  1036. As special cases, a nil or t value means show all contexts in
  1037. `minimal' or `canonical' view, respectively.
  1038. Some views can make displayed information very compact, but also
  1039. make it harder to edit the location of the match. In such
  1040. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1041. more context."
  1042. :group 'org-reveal-location
  1043. :version "25.1"
  1044. :package-version '(Org . "8.3")
  1045. :type '(choice
  1046. (const :tag "Canonical" t)
  1047. (const :tag "Minimal" nil)
  1048. (repeat :greedy t :tag "Individual contexts"
  1049. (cons
  1050. (choice :tag "Context"
  1051. (const agenda)
  1052. (const org-goto)
  1053. (const occur-tree)
  1054. (const tags-tree)
  1055. (const link-search)
  1056. (const mark-goto)
  1057. (const bookmark-jump)
  1058. (const isearch)
  1059. (const default))
  1060. (choice :tag "Detail level"
  1061. (const minimal)
  1062. (const local)
  1063. (const ancestors)
  1064. (const lineage)
  1065. (const tree)
  1066. (const canonical))))))
  1067. (defcustom org-indirect-buffer-display 'other-window
  1068. "How should indirect tree buffers be displayed?
  1069. This applies to indirect buffers created with the commands
  1070. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1071. Valid values are:
  1072. current-window Display in the current window
  1073. other-window Just display in another window.
  1074. dedicated-frame Create one new frame, and re-use it each time.
  1075. new-frame Make a new frame each time. Note that in this case
  1076. previously-made indirect buffers are kept, and you need to
  1077. kill these buffers yourself."
  1078. :group 'org-structure
  1079. :group 'org-agenda-windows
  1080. :type '(choice
  1081. (const :tag "In current window" current-window)
  1082. (const :tag "In current frame, other window" other-window)
  1083. (const :tag "Each time a new frame" new-frame)
  1084. (const :tag "One dedicated frame" dedicated-frame)))
  1085. (defcustom org-use-speed-commands nil
  1086. "Non-nil means activate single letter commands at beginning of a headline.
  1087. This may also be a function to test for appropriate locations where speed
  1088. commands should be active.
  1089. For example, to activate speed commands when the point is on any
  1090. star at the beginning of the headline, you can do this:
  1091. (setq org-use-speed-commands
  1092. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1093. :group 'org-structure
  1094. :type '(choice
  1095. (const :tag "Never" nil)
  1096. (const :tag "At beginning of headline stars" t)
  1097. (function)))
  1098. (defcustom org-speed-commands-user nil
  1099. "Alist of additional speed commands.
  1100. This list will be checked before `org-speed-commands-default'
  1101. when the variable `org-use-speed-commands' is non-nil
  1102. and when the cursor is at the beginning of a headline.
  1103. The car if each entry is a string with a single letter, which must
  1104. be assigned to `self-insert-command' in the global map.
  1105. The cdr is either a command to be called interactively, a function
  1106. to be called, or a form to be evaluated.
  1107. An entry that is just a list with a single string will be interpreted
  1108. as a descriptive headline that will be added when listing the speed
  1109. commands in the Help buffer using the `?' speed command."
  1110. :group 'org-structure
  1111. :type '(repeat :value ("k" . ignore)
  1112. (choice :value ("k" . ignore)
  1113. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1114. (cons :tag "Letter and Command"
  1115. (string :tag "Command letter")
  1116. (choice
  1117. (function)
  1118. (sexp))))))
  1119. (defcustom org-bookmark-names-plist
  1120. '(:last-capture "org-capture-last-stored"
  1121. :last-refile "org-refile-last-stored"
  1122. :last-capture-marker "org-capture-last-stored-marker")
  1123. "Names for bookmarks automatically set by some Org commands.
  1124. This can provide strings as names for a number of bookmarks Org sets
  1125. automatically. The following keys are currently implemented:
  1126. :last-capture
  1127. :last-capture-marker
  1128. :last-refile
  1129. When a key does not show up in the property list, the corresponding bookmark
  1130. is not set."
  1131. :group 'org-structure
  1132. :type 'plist)
  1133. (defgroup org-cycle nil
  1134. "Options concerning visibility cycling in Org-mode."
  1135. :tag "Org Cycle"
  1136. :group 'org-structure)
  1137. (defcustom org-cycle-skip-children-state-if-no-children t
  1138. "Non-nil means skip CHILDREN state in entries that don't have any."
  1139. :group 'org-cycle
  1140. :type 'boolean)
  1141. (defcustom org-cycle-max-level nil
  1142. "Maximum level which should still be subject to visibility cycling.
  1143. Levels higher than this will, for cycling, be treated as text, not a headline.
  1144. When `org-odd-levels-only' is set, a value of N in this variable actually
  1145. means 2N-1 stars as the limiting headline.
  1146. When nil, cycle all levels.
  1147. Note that the limiting level of cycling is also influenced by
  1148. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1149. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1150. than its value."
  1151. :group 'org-cycle
  1152. :type '(choice
  1153. (const :tag "No limit" nil)
  1154. (integer :tag "Maximum level")))
  1155. (defcustom org-hide-block-startup nil
  1156. "Non-nil means entering Org-mode will fold all blocks.
  1157. This can also be set in on a per-file basis with
  1158. #+STARTUP: hideblocks
  1159. #+STARTUP: showblocks"
  1160. :group 'org-startup
  1161. :group 'org-cycle
  1162. :type 'boolean)
  1163. (defcustom org-cycle-global-at-bob nil
  1164. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1165. This makes it possible to do global cycling without having to use S-TAB or
  1166. \\[universal-argument] TAB. For this special case to work, the first line
  1167. of the buffer must not be a headline -- it may be empty or some other text.
  1168. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1169. sure the cursor stays at the beginning of the buffer. When this option is
  1170. nil, don't do anything special at the beginning of the buffer."
  1171. :group 'org-cycle
  1172. :type 'boolean)
  1173. (defcustom org-cycle-level-after-item/entry-creation t
  1174. "Non-nil means cycle entry level or item indentation in new empty entries.
  1175. When the cursor is at the end of an empty headline, i.e., with only stars
  1176. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1177. and then all possible ancestor states, before returning to the original state.
  1178. This makes data entry extremely fast: M-RET to create a new headline,
  1179. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1180. When the cursor is at the end of an empty plain list item, one TAB will
  1181. make it a subitem, two or more tabs will back up to make this an item
  1182. higher up in the item hierarchy."
  1183. :group 'org-cycle
  1184. :type 'boolean)
  1185. (defcustom org-cycle-emulate-tab t
  1186. "Where should `org-cycle' emulate TAB.
  1187. nil Never
  1188. white Only in completely white lines
  1189. whitestart Only at the beginning of lines, before the first non-white char
  1190. t Everywhere except in headlines
  1191. exc-hl-bol Everywhere except at the start of a headline
  1192. If TAB is used in a place where it does not emulate TAB, the current subtree
  1193. visibility is cycled."
  1194. :group 'org-cycle
  1195. :type '(choice (const :tag "Never" nil)
  1196. (const :tag "Only in completely white lines" white)
  1197. (const :tag "Before first char in a line" whitestart)
  1198. (const :tag "Everywhere except in headlines" t)
  1199. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1200. (defcustom org-cycle-separator-lines 2
  1201. "Number of empty lines needed to keep an empty line between collapsed trees.
  1202. If you leave an empty line between the end of a subtree and the following
  1203. headline, this empty line is hidden when the subtree is folded.
  1204. Org-mode will leave (exactly) one empty line visible if the number of
  1205. empty lines is equal or larger to the number given in this variable.
  1206. So the default 2 means at least 2 empty lines after the end of a subtree
  1207. are needed to produce free space between a collapsed subtree and the
  1208. following headline.
  1209. If the number is negative, and the number of empty lines is at least -N,
  1210. all empty lines are shown.
  1211. Special case: when 0, never leave empty lines in collapsed view."
  1212. :group 'org-cycle
  1213. :type 'integer)
  1214. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1215. (defcustom org-pre-cycle-hook nil
  1216. "Hook that is run before visibility cycling is happening.
  1217. The function(s) in this hook must accept a single argument which indicates
  1218. the new state that will be set right after running this hook. The
  1219. argument is a symbol. Before a global state change, it can have the values
  1220. `overview', `content', or `all'. Before a local state change, it can have
  1221. the values `folded', `children', or `subtree'."
  1222. :group 'org-cycle
  1223. :type 'hook)
  1224. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1225. org-cycle-hide-drawers
  1226. org-cycle-show-empty-lines
  1227. org-optimize-window-after-visibility-change)
  1228. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1229. The function(s) in this hook must accept a single argument which indicates
  1230. the new state that was set by the most recent `org-cycle' command. The
  1231. argument is a symbol. After a global state change, it can have the values
  1232. `overview', `contents', or `all'. After a local state change, it can have
  1233. the values `folded', `children', or `subtree'."
  1234. :group 'org-cycle
  1235. :type 'hook
  1236. :version "25.1"
  1237. :package-version '(Org . "8.3"))
  1238. (defgroup org-edit-structure nil
  1239. "Options concerning structure editing in Org-mode."
  1240. :tag "Org Edit Structure"
  1241. :group 'org-structure)
  1242. (defcustom org-odd-levels-only nil
  1243. "Non-nil means skip even levels and only use odd levels for the outline.
  1244. This has the effect that two stars are being added/taken away in
  1245. promotion/demotion commands. It also influences how levels are
  1246. handled by the exporters.
  1247. Changing it requires restart of `font-lock-mode' to become effective
  1248. for fontification also in regions already fontified.
  1249. You may also set this on a per-file basis by adding one of the following
  1250. lines to the buffer:
  1251. #+STARTUP: odd
  1252. #+STARTUP: oddeven"
  1253. :group 'org-edit-structure
  1254. :group 'org-appearance
  1255. :type 'boolean)
  1256. (defcustom org-adapt-indentation t
  1257. "Non-nil means adapt indentation to outline node level.
  1258. When this variable is set, Org assumes that you write outlines by
  1259. indenting text in each node to align with the headline (after the
  1260. stars). The following issues are influenced by this variable:
  1261. - The indentation is increased by one space in a demotion
  1262. command, and decreased by one in a promotion command. However,
  1263. in the latter case, if shifting some line in the entry body
  1264. would alter document structure (e.g., insert a new headline),
  1265. indentation is not changed at all.
  1266. - Property drawers and planning information is inserted indented
  1267. when this variable is set. When nil, they will not be indented.
  1268. - TAB indents a line relative to current level. The lines below
  1269. a headline will be indented when this variable is set.
  1270. Note that this is all about true indentation, by adding and
  1271. removing space characters. See also `org-indent.el' which does
  1272. level-dependent indentation in a virtual way, i.e. at display
  1273. time in Emacs."
  1274. :group 'org-edit-structure
  1275. :type 'boolean)
  1276. (defcustom org-special-ctrl-a/e nil
  1277. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1278. When t, `C-a' will bring back the cursor to the beginning of the
  1279. headline text, i.e. after the stars and after a possible TODO
  1280. keyword. In an item, this will be the position after bullet and
  1281. check-box, if any. When the cursor is already at that position,
  1282. another `C-a' will bring it to the beginning of the line.
  1283. `C-e' will jump to the end of the headline, ignoring the presence
  1284. of tags in the headline. A second `C-e' will then jump to the
  1285. true end of the line, after any tags. This also means that, when
  1286. this variable is non-nil, `C-e' also will never jump beyond the
  1287. end of the heading of a folded section, i.e. not after the
  1288. ellipses.
  1289. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1290. normally, going to the true line boundary first. Only a directly
  1291. following, identical keypress will bring the cursor to the
  1292. special positions.
  1293. This may also be a cons cell where the behavior for `C-a' and
  1294. `C-e' is set separately."
  1295. :group 'org-edit-structure
  1296. :type '(choice
  1297. (const :tag "off" nil)
  1298. (const :tag "on: after stars/bullet and before tags first" t)
  1299. (const :tag "reversed: true line boundary first" reversed)
  1300. (cons :tag "Set C-a and C-e separately"
  1301. (choice :tag "Special C-a"
  1302. (const :tag "off" nil)
  1303. (const :tag "on: after stars/bullet first" t)
  1304. (const :tag "reversed: before stars/bullet first" reversed))
  1305. (choice :tag "Special C-e"
  1306. (const :tag "off" nil)
  1307. (const :tag "on: before tags first" t)
  1308. (const :tag "reversed: after tags first" reversed)))))
  1309. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1310. (defcustom org-special-ctrl-k nil
  1311. "Non-nil means `C-k' will behave specially in headlines.
  1312. When nil, `C-k' will call the default `kill-line' command.
  1313. When t, the following will happen while the cursor is in the headline:
  1314. - When the cursor is at the beginning of a headline, kill the entire
  1315. line and possible the folded subtree below the line.
  1316. - When in the middle of the headline text, kill the headline up to the tags.
  1317. - When after the headline text, kill the tags."
  1318. :group 'org-edit-structure
  1319. :type 'boolean)
  1320. (defcustom org-ctrl-k-protect-subtree nil
  1321. "Non-nil means, do not delete a hidden subtree with C-k.
  1322. When set to the symbol `error', simply throw an error when C-k is
  1323. used to kill (part-of) a headline that has hidden text behind it.
  1324. Any other non-nil value will result in a query to the user, if it is
  1325. OK to kill that hidden subtree. When nil, kill without remorse."
  1326. :group 'org-edit-structure
  1327. :version "24.1"
  1328. :type '(choice
  1329. (const :tag "Do not protect hidden subtrees" nil)
  1330. (const :tag "Protect hidden subtrees with a security query" t)
  1331. (const :tag "Never kill a hidden subtree with C-k" error)))
  1332. (defcustom org-special-ctrl-o t
  1333. "Non-nil means, make `C-o' insert a row in tables."
  1334. :group 'org-edit-structure
  1335. :type 'boolean)
  1336. (defcustom org-catch-invisible-edits nil
  1337. "Check if in invisible region before inserting or deleting a character.
  1338. Valid values are:
  1339. nil Do not check, so just do invisible edits.
  1340. error Throw an error and do nothing.
  1341. show Make point visible, and do the requested edit.
  1342. show-and-error Make point visible, then throw an error and abort the edit.
  1343. smart Make point visible, and do insertion/deletion if it is
  1344. adjacent to visible text and the change feels predictable.
  1345. Never delete a previously invisible character or add in the
  1346. middle or right after an invisible region. Basically, this
  1347. allows insertion and backward-delete right before ellipses.
  1348. FIXME: maybe in this case we should not even show?"
  1349. :group 'org-edit-structure
  1350. :version "24.1"
  1351. :type '(choice
  1352. (const :tag "Do not check" nil)
  1353. (const :tag "Throw error when trying to edit" error)
  1354. (const :tag "Unhide, but do not do the edit" show-and-error)
  1355. (const :tag "Show invisible part and do the edit" show)
  1356. (const :tag "Be smart and do the right thing" smart)))
  1357. (defcustom org-yank-folded-subtrees t
  1358. "Non-nil means when yanking subtrees, fold them.
  1359. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1360. it starts with a heading and all other headings in it are either children
  1361. or siblings, then fold all the subtrees. However, do this only if no
  1362. text after the yank would be swallowed into a folded tree by this action."
  1363. :group 'org-edit-structure
  1364. :type 'boolean)
  1365. (defcustom org-yank-adjusted-subtrees nil
  1366. "Non-nil means when yanking subtrees, adjust the level.
  1367. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1368. this function for details."
  1369. :group 'org-edit-structure
  1370. :type 'boolean)
  1371. (defcustom org-M-RET-may-split-line '((default . t))
  1372. "Non-nil means M-RET will split the line at the cursor position.
  1373. When nil, it will go to the end of the line before making a
  1374. new line.
  1375. You may also set this option in a different way for different
  1376. contexts. Valid contexts are:
  1377. headline when creating a new headline
  1378. item when creating a new item
  1379. table in a table field
  1380. default the value to be used for all contexts not explicitly
  1381. customized"
  1382. :group 'org-structure
  1383. :group 'org-table
  1384. :type '(choice
  1385. (const :tag "Always" t)
  1386. (const :tag "Never" nil)
  1387. (repeat :greedy t :tag "Individual contexts"
  1388. (cons
  1389. (choice :tag "Context"
  1390. (const headline)
  1391. (const item)
  1392. (const table)
  1393. (const default))
  1394. (boolean)))))
  1395. (defcustom org-insert-heading-respect-content nil
  1396. "Non-nil means insert new headings after the current subtree.
  1397. When nil, the new heading is created directly after the current line.
  1398. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1399. this variable on for the duration of the command."
  1400. :group 'org-structure
  1401. :type 'boolean)
  1402. (defcustom org-blank-before-new-entry '((heading . auto)
  1403. (plain-list-item . auto))
  1404. "Should `org-insert-heading' leave a blank line before new heading/item?
  1405. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1406. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1407. which case Org will look at the surrounding headings/items and try to
  1408. make an intelligent decision whether to insert a blank line or not.
  1409. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1410. the setting here is ignored and no empty line is inserted to avoid breaking
  1411. the list structure."
  1412. :group 'org-edit-structure
  1413. :type '(list
  1414. (cons (const heading)
  1415. (choice (const :tag "Never" nil)
  1416. (const :tag "Always" t)
  1417. (const :tag "Auto" auto)))
  1418. (cons (const plain-list-item)
  1419. (choice (const :tag "Never" nil)
  1420. (const :tag "Always" t)
  1421. (const :tag "Auto" auto)))))
  1422. (defcustom org-insert-heading-hook nil
  1423. "Hook being run after inserting a new heading."
  1424. :group 'org-edit-structure
  1425. :type 'hook)
  1426. (defcustom org-enable-fixed-width-editor t
  1427. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1428. This currently only means they are never auto-wrapped.
  1429. When nil, such lines will be treated like ordinary lines."
  1430. :group 'org-edit-structure
  1431. :type 'boolean)
  1432. (defcustom org-goto-auto-isearch t
  1433. "Non-nil means typing characters in `org-goto' starts incremental search.
  1434. When nil, you can use these keybindings to navigate the buffer:
  1435. q Quit the org-goto interface
  1436. n Go to the next visible heading
  1437. p Go to the previous visible heading
  1438. f Go one heading forward on same level
  1439. b Go one heading backward on same level
  1440. u Go one heading up"
  1441. :group 'org-edit-structure
  1442. :type 'boolean)
  1443. (defgroup org-sparse-trees nil
  1444. "Options concerning sparse trees in Org-mode."
  1445. :tag "Org Sparse Trees"
  1446. :group 'org-structure)
  1447. (defcustom org-highlight-sparse-tree-matches t
  1448. "Non-nil means highlight all matches that define a sparse tree.
  1449. The highlights will automatically disappear the next time the buffer is
  1450. changed by an edit command."
  1451. :group 'org-sparse-trees
  1452. :type 'boolean)
  1453. (defcustom org-remove-highlights-with-change t
  1454. "Non-nil means any change to the buffer will remove temporary highlights.
  1455. Such highlights are created by `org-occur' and `org-clock-display'.
  1456. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1457. The highlights created by `org-toggle-latex-fragment' always need
  1458. `C-c C-x C-l' to be removed."
  1459. :group 'org-sparse-trees
  1460. :group 'org-time
  1461. :type 'boolean)
  1462. (defcustom org-occur-hook '(org-first-headline-recenter)
  1463. "Hook that is run after `org-occur' has constructed a sparse tree.
  1464. This can be used to recenter the window to show as much of the structure
  1465. as possible."
  1466. :group 'org-sparse-trees
  1467. :type 'hook)
  1468. (defgroup org-imenu-and-speedbar nil
  1469. "Options concerning imenu and speedbar in Org-mode."
  1470. :tag "Org Imenu and Speedbar"
  1471. :group 'org-structure)
  1472. (defcustom org-imenu-depth 2
  1473. "The maximum level for Imenu access to Org-mode headlines.
  1474. This also applied for speedbar access."
  1475. :group 'org-imenu-and-speedbar
  1476. :type 'integer)
  1477. (defgroup org-table nil
  1478. "Options concerning tables in Org-mode."
  1479. :tag "Org Table"
  1480. :group 'org)
  1481. (defcustom org-enable-table-editor 'optimized
  1482. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1483. When nil, such lines will be treated like ordinary lines.
  1484. When equal to the symbol `optimized', the table editor will be optimized to
  1485. do the following:
  1486. - Automatic overwrite mode in front of whitespace in table fields.
  1487. This makes the structure of the table stay in tact as long as the edited
  1488. field does not exceed the column width.
  1489. - Minimize the number of realigns. Normally, the table is aligned each time
  1490. TAB or RET are pressed to move to another field. With optimization this
  1491. happens only if changes to a field might have changed the column width.
  1492. Optimization requires replacing the functions `self-insert-command',
  1493. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1494. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1495. very good at guessing when a re-align will be necessary, but you can always
  1496. force one with \\[org-ctrl-c-ctrl-c].
  1497. If you would like to use the optimized version in Org-mode, but the
  1498. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1499. This variable can be used to turn on and off the table editor during a session,
  1500. but in order to toggle optimization, a restart is required.
  1501. See also the variable `org-table-auto-blank-field'."
  1502. :group 'org-table
  1503. :type '(choice
  1504. (const :tag "off" nil)
  1505. (const :tag "on" t)
  1506. (const :tag "on, optimized" optimized)))
  1507. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1508. (version<= emacs-version "24.1"))
  1509. "Non-nil means cluster self-insert commands for undo when possible.
  1510. If this is set, then, like in the Emacs command loop, 20 consecutive
  1511. characters will be undone together.
  1512. This is configurable, because there is some impact on typing performance."
  1513. :group 'org-table
  1514. :type 'boolean)
  1515. (defcustom org-table-tab-recognizes-table.el t
  1516. "Non-nil means TAB will automatically notice a table.el table.
  1517. When it sees such a table, it moves point into it and - if necessary -
  1518. calls `table-recognize-table'."
  1519. :group 'org-table-editing
  1520. :type 'boolean)
  1521. (defgroup org-link nil
  1522. "Options concerning links in Org-mode."
  1523. :tag "Org Link"
  1524. :group 'org)
  1525. (defvar org-link-abbrev-alist-local nil
  1526. "Buffer-local version of `org-link-abbrev-alist', which see.
  1527. The value of this is taken from the #+LINK lines.")
  1528. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1529. (defcustom org-link-abbrev-alist nil
  1530. "Alist of link abbreviations.
  1531. The car of each element is a string, to be replaced at the start of a link.
  1532. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1533. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1534. [[linkkey:tag][description]]
  1535. The 'linkkey' must be a word word, starting with a letter, followed
  1536. by letters, numbers, '-' or '_'.
  1537. If REPLACE is a string, the tag will simply be appended to create the link.
  1538. If the string contains \"%s\", the tag will be inserted there. If the string
  1539. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1540. at that point (see the function `url-hexify-string'). If the string contains
  1541. the specifier \"%(my-function)\", then the custom function `my-function' will
  1542. be invoked: this function takes the tag as its only argument and must return
  1543. a string.
  1544. REPLACE may also be a function that will be called with the tag as the
  1545. only argument to create the link, which should be returned as a string.
  1546. See the manual for examples."
  1547. :group 'org-link
  1548. :type '(repeat
  1549. (cons
  1550. (string :tag "Protocol")
  1551. (choice
  1552. (string :tag "Format")
  1553. (function)))))
  1554. (defcustom org-descriptive-links t
  1555. "Non-nil means Org will display descriptive links.
  1556. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1557. \"Org Website\", hiding the link itself and just displaying its
  1558. description. When set to `nil', Org will display the full links
  1559. literally.
  1560. You can interactively set the value of this variable by calling
  1561. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1562. :group 'org-link
  1563. :type 'boolean)
  1564. (defcustom org-link-file-path-type 'adaptive
  1565. "How the path name in file links should be stored.
  1566. Valid values are:
  1567. relative Relative to the current directory, i.e. the directory of the file
  1568. into which the link is being inserted.
  1569. absolute Absolute path, if possible with ~ for home directory.
  1570. noabbrev Absolute path, no abbreviation of home directory.
  1571. adaptive Use relative path for files in the current directory and sub-
  1572. directories of it. For other files, use an absolute path."
  1573. :group 'org-link
  1574. :type '(choice
  1575. (const relative)
  1576. (const absolute)
  1577. (const noabbrev)
  1578. (const adaptive)))
  1579. (defvaralias 'org-activate-links 'org-highlight-links)
  1580. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1581. "Types of links that should be highlighted in Org-mode files.
  1582. This is a list of symbols, each one of them leading to the
  1583. highlighting of a certain link type.
  1584. You can still open links that are not highlighted.
  1585. In principle, it does not hurt to turn on highlighting for all
  1586. link types. There may be a small gain when turning off unused
  1587. link types. The types are:
  1588. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1589. angle Links in angular brackets that may contain whitespace like
  1590. <bbdb:Carsten Dominik>.
  1591. plain Plain links in normal text, no whitespace, like http://google.com.
  1592. radio Text that is matched by a radio target, see manual for details.
  1593. tag Tag settings in a headline (link to tag search).
  1594. date Time stamps (link to calendar).
  1595. footnote Footnote labels.
  1596. If you set this variable during an Emacs session, use `org-mode-restart'
  1597. in the Org buffer so that the change takes effect."
  1598. :group 'org-link
  1599. :group 'org-appearance
  1600. :type '(set :greedy t
  1601. (const :tag "Double bracket links" bracket)
  1602. (const :tag "Angular bracket links" angle)
  1603. (const :tag "Plain text links" plain)
  1604. (const :tag "Radio target matches" radio)
  1605. (const :tag "Tags" tag)
  1606. (const :tag "Timestamps" date)
  1607. (const :tag "Footnotes" footnote)))
  1608. (defcustom org-make-link-description-function nil
  1609. "Function to use for generating link descriptions from links.
  1610. When nil, the link location will be used. This function must take
  1611. two parameters: the first one is the link, the second one is the
  1612. description generated by `org-insert-link'. The function should
  1613. return the description to use."
  1614. :group 'org-link
  1615. :type '(choice (const nil) (function)))
  1616. (defgroup org-link-store nil
  1617. "Options concerning storing links in Org-mode."
  1618. :tag "Org Store Link"
  1619. :group 'org-link)
  1620. (defcustom org-url-hexify-p t
  1621. "When non-nil, hexify URL when creating a link."
  1622. :type 'boolean
  1623. :version "24.3"
  1624. :group 'org-link-store)
  1625. (defcustom org-email-link-description-format "Email %c: %.30s"
  1626. "Format of the description part of a link to an email or usenet message.
  1627. The following %-escapes will be replaced by corresponding information:
  1628. %F full \"From\" field
  1629. %f name, taken from \"From\" field, address if no name
  1630. %T full \"To\" field
  1631. %t first name in \"To\" field, address if no name
  1632. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1633. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1634. %s subject
  1635. %d date
  1636. %m message-id.
  1637. You may use normal field width specification between the % and the letter.
  1638. This is for example useful to limit the length of the subject.
  1639. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1640. :group 'org-link-store
  1641. :type 'string)
  1642. (defcustom org-from-is-user-regexp
  1643. (let (r1 r2)
  1644. (when (and user-mail-address (not (string= user-mail-address "")))
  1645. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1646. (when (and user-full-name (not (string= user-full-name "")))
  1647. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1648. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1649. "Regexp matched against the \"From:\" header of an email or usenet message.
  1650. It should match if the message is from the user him/herself."
  1651. :group 'org-link-store
  1652. :type 'regexp)
  1653. (defcustom org-context-in-file-links t
  1654. "Non-nil means file links from `org-store-link' contain context.
  1655. A search string will be added to the file name with :: as separator and
  1656. used to find the context when the link is activated by the command
  1657. `org-open-at-point'. When this option is t, the entire active region
  1658. will be placed in the search string of the file link. If set to a
  1659. positive integer, only the first n lines of context will be stored.
  1660. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1661. negates this setting for the duration of the command."
  1662. :group 'org-link-store
  1663. :type '(choice boolean integer))
  1664. (defcustom org-keep-stored-link-after-insertion nil
  1665. "Non-nil means keep link in list for entire session.
  1666. The command `org-store-link' adds a link pointing to the current
  1667. location to an internal list. These links accumulate during a session.
  1668. The command `org-insert-link' can be used to insert links into any
  1669. Org-mode file (offering completion for all stored links). When this
  1670. option is nil, every link which has been inserted once using \\[org-insert-link]
  1671. will be removed from the list, to make completing the unused links
  1672. more efficient."
  1673. :group 'org-link-store
  1674. :type 'boolean)
  1675. (defgroup org-link-follow nil
  1676. "Options concerning following links in Org-mode."
  1677. :tag "Org Follow Link"
  1678. :group 'org-link)
  1679. (defcustom org-link-translation-function nil
  1680. "Function to translate links with different syntax to Org syntax.
  1681. This can be used to translate links created for example by the Planner
  1682. or emacs-wiki packages to Org syntax.
  1683. The function must accept two parameters, a TYPE containing the link
  1684. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1685. which is everything after the link protocol. It should return a cons
  1686. with possibly modified values of type and path.
  1687. Org contains a function for this, so if you set this variable to
  1688. `org-translate-link-from-planner', you should be able follow many
  1689. links created by planner."
  1690. :group 'org-link-follow
  1691. :type '(choice (const nil) (function)))
  1692. (defcustom org-follow-link-hook nil
  1693. "Hook that is run after a link has been followed."
  1694. :group 'org-link-follow
  1695. :type 'hook)
  1696. (defcustom org-tab-follows-link nil
  1697. "Non-nil means on links TAB will follow the link.
  1698. Needs to be set before org.el is loaded.
  1699. This really should not be used, it does not make sense, and the
  1700. implementation is bad."
  1701. :group 'org-link-follow
  1702. :type 'boolean)
  1703. (defcustom org-return-follows-link nil
  1704. "Non-nil means on links RET will follow the link.
  1705. In tables, the special behavior of RET has precedence."
  1706. :group 'org-link-follow
  1707. :type 'boolean)
  1708. (defcustom org-mouse-1-follows-link
  1709. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1710. "Non-nil means mouse-1 on a link will follow the link.
  1711. A longer mouse click will still set point. Does not work on XEmacs.
  1712. Needs to be set before org.el is loaded."
  1713. :group 'org-link-follow
  1714. :version "24.4"
  1715. :package-version '(Org . "8.3")
  1716. :type '(choice
  1717. (const :tag "A double click follows the link" double)
  1718. (const :tag "Unconditionally follow the link with mouse-1" t)
  1719. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1720. (defcustom org-mark-ring-length 4
  1721. "Number of different positions to be recorded in the ring.
  1722. Changing this requires a restart of Emacs to work correctly."
  1723. :group 'org-link-follow
  1724. :type 'integer)
  1725. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1726. "Non-nil means internal links in Org files must exactly match a headline.
  1727. When nil, the link search tries to match a phrase with all words
  1728. in the search text."
  1729. :group 'org-link-follow
  1730. :version "24.1"
  1731. :type '(choice
  1732. (const :tag "Use fuzzy text search" nil)
  1733. (const :tag "Match only exact headline" t)
  1734. (const :tag "Match exact headline or query to create it"
  1735. query-to-create)))
  1736. (defcustom org-link-frame-setup
  1737. '((vm . vm-visit-folder-other-frame)
  1738. (vm-imap . vm-visit-imap-folder-other-frame)
  1739. (gnus . org-gnus-no-new-news)
  1740. (file . find-file-other-window)
  1741. (wl . wl-other-frame))
  1742. "Setup the frame configuration for following links.
  1743. When following a link with Emacs, it may often be useful to display
  1744. this link in another window or frame. This variable can be used to
  1745. set this up for the different types of links.
  1746. For VM, use any of
  1747. `vm-visit-folder'
  1748. `vm-visit-folder-other-window'
  1749. `vm-visit-folder-other-frame'
  1750. For Gnus, use any of
  1751. `gnus'
  1752. `gnus-other-frame'
  1753. `org-gnus-no-new-news'
  1754. For FILE, use any of
  1755. `find-file'
  1756. `find-file-other-window'
  1757. `find-file-other-frame'
  1758. For Wanderlust use any of
  1759. `wl'
  1760. `wl-other-frame'
  1761. For the calendar, use the variable `calendar-setup'.
  1762. For BBDB, it is currently only possible to display the matches in
  1763. another window."
  1764. :group 'org-link-follow
  1765. :type '(list
  1766. (cons (const vm)
  1767. (choice
  1768. (const vm-visit-folder)
  1769. (const vm-visit-folder-other-window)
  1770. (const vm-visit-folder-other-frame)))
  1771. (cons (const vm-imap)
  1772. (choice
  1773. (const vm-visit-imap-folder)
  1774. (const vm-visit-imap-folder-other-window)
  1775. (const vm-visit-imap-folder-other-frame)))
  1776. (cons (const gnus)
  1777. (choice
  1778. (const gnus)
  1779. (const gnus-other-frame)
  1780. (const org-gnus-no-new-news)))
  1781. (cons (const file)
  1782. (choice
  1783. (const find-file)
  1784. (const find-file-other-window)
  1785. (const find-file-other-frame)))
  1786. (cons (const wl)
  1787. (choice
  1788. (const wl)
  1789. (const wl-other-frame)))))
  1790. (defcustom org-display-internal-link-with-indirect-buffer nil
  1791. "Non-nil means use indirect buffer to display infile links.
  1792. Activating internal links (from one location in a file to another location
  1793. in the same file) normally just jumps to the location. When the link is
  1794. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1795. is displayed in
  1796. another window. When this option is set, the other window actually displays
  1797. an indirect buffer clone of the current buffer, to avoid any visibility
  1798. changes to the current buffer."
  1799. :group 'org-link-follow
  1800. :type 'boolean)
  1801. (defcustom org-open-non-existing-files nil
  1802. "Non-nil means `org-open-file' will open non-existing files.
  1803. When nil, an error will be generated.
  1804. This variable applies only to external applications because they
  1805. might choke on non-existing files. If the link is to a file that
  1806. will be opened in Emacs, the variable is ignored."
  1807. :group 'org-link-follow
  1808. :type 'boolean)
  1809. (defcustom org-open-directory-means-index-dot-org nil
  1810. "Non-nil means a link to a directory really means to index.org.
  1811. When nil, following a directory link will run dired or open a finder/explorer
  1812. window on that directory."
  1813. :group 'org-link-follow
  1814. :type 'boolean)
  1815. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1816. "Non-nil means ask for confirmation before executing shell links.
  1817. Shell links can be dangerous: just think about a link
  1818. [[shell:rm -rf ~/*][Google Search]]
  1819. This link would show up in your Org-mode document as \"Google Search\",
  1820. but really it would remove your entire home directory.
  1821. Therefore we advise against setting this variable to nil.
  1822. Just change it to `y-or-n-p' if you want to confirm with a
  1823. single keystroke rather than having to type \"yes\"."
  1824. :group 'org-link-follow
  1825. :type '(choice
  1826. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1827. (const :tag "with y-or-n (faster)" y-or-n-p)
  1828. (const :tag "no confirmation (dangerous)" nil)))
  1829. (put 'org-confirm-shell-link-function
  1830. 'safe-local-variable
  1831. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1832. (defcustom org-confirm-shell-link-not-regexp ""
  1833. "A regexp to skip confirmation for shell links."
  1834. :group 'org-link-follow
  1835. :version "24.1"
  1836. :type 'regexp)
  1837. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1838. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1839. Elisp links can be dangerous: just think about a link
  1840. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1841. This link would show up in your Org-mode document as \"Google Search\",
  1842. but really it would remove your entire home directory.
  1843. Therefore we advise against setting this variable to nil.
  1844. Just change it to `y-or-n-p' if you want to confirm with a
  1845. single keystroke rather than having to type \"yes\"."
  1846. :group 'org-link-follow
  1847. :type '(choice
  1848. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1849. (const :tag "with y-or-n (faster)" y-or-n-p)
  1850. (const :tag "no confirmation (dangerous)" nil)))
  1851. (put 'org-confirm-shell-link-function
  1852. 'safe-local-variable
  1853. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1854. (defcustom org-confirm-elisp-link-not-regexp ""
  1855. "A regexp to skip confirmation for Elisp links."
  1856. :group 'org-link-follow
  1857. :version "24.1"
  1858. :type 'regexp)
  1859. (defconst org-file-apps-defaults-gnu
  1860. '((remote . emacs)
  1861. (system . mailcap)
  1862. (t . mailcap))
  1863. "Default file applications on a UNIX or GNU/Linux system.
  1864. See `org-file-apps'.")
  1865. (defconst org-file-apps-defaults-macosx
  1866. '((remote . emacs)
  1867. (t . "open %s")
  1868. (system . "open %s")
  1869. ("ps.gz" . "gv %s")
  1870. ("eps.gz" . "gv %s")
  1871. ("dvi" . "xdvi %s")
  1872. ("fig" . "xfig %s"))
  1873. "Default file applications on a MacOS X system.
  1874. The system \"open\" is known as a default, but we use X11 applications
  1875. for some files for which the OS does not have a good default.
  1876. See `org-file-apps'.")
  1877. (defconst org-file-apps-defaults-windowsnt
  1878. (list
  1879. '(remote . emacs)
  1880. (cons t
  1881. (list (if (featurep 'xemacs)
  1882. 'mswindows-shell-execute
  1883. 'w32-shell-execute)
  1884. "open" 'file))
  1885. (cons 'system
  1886. (list (if (featurep 'xemacs)
  1887. 'mswindows-shell-execute
  1888. 'w32-shell-execute)
  1889. "open" 'file)))
  1890. "Default file applications on a Windows NT system.
  1891. The system \"open\" is used for most files.
  1892. See `org-file-apps'.")
  1893. (defcustom org-file-apps
  1894. '((auto-mode . emacs)
  1895. ("\\.mm\\'" . default)
  1896. ("\\.x?html?\\'" . default)
  1897. ("\\.pdf\\'" . default))
  1898. "External applications for opening `file:path' items in a document.
  1899. Org-mode uses system defaults for different file types, but
  1900. you can use this variable to set the application for a given file
  1901. extension. The entries in this list are cons cells where the car identifies
  1902. files and the cdr the corresponding command. Possible values for the
  1903. file identifier are
  1904. \"string\" A string as a file identifier can be interpreted in different
  1905. ways, depending on its contents:
  1906. - Alphanumeric characters only:
  1907. Match links with this file extension.
  1908. Example: (\"pdf\" . \"evince %s\")
  1909. to open PDFs with evince.
  1910. - Regular expression: Match links where the
  1911. filename matches the regexp. If you want to
  1912. use groups here, use shy groups.
  1913. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1914. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1915. to open *.html and *.xhtml with firefox.
  1916. - Regular expression which contains (non-shy) groups:
  1917. Match links where the whole link, including \"::\", and
  1918. anything after that, matches the regexp.
  1919. In a custom command string, %1, %2, etc. are replaced with
  1920. the parts of the link that were matched by the groups.
  1921. For backwards compatibility, if a command string is given
  1922. that does not use any of the group matches, this case is
  1923. handled identically to the second one (i.e. match against
  1924. file name only).
  1925. In a custom lisp form, you can access the group matches with
  1926. (match-string n link).
  1927. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1928. to open [[file:document.pdf::5]] with evince at page 5.
  1929. `directory' Matches a directory
  1930. `remote' Matches a remote file, accessible through tramp or efs.
  1931. Remote files most likely should be visited through Emacs
  1932. because external applications cannot handle such paths.
  1933. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1934. so all files Emacs knows how to handle. Using this with
  1935. command `emacs' will open most files in Emacs. Beware that this
  1936. will also open html files inside Emacs, unless you add
  1937. (\"html\" . default) to the list as well.
  1938. t Default for files not matched by any of the other options.
  1939. `system' The system command to open files, like `open' on Windows
  1940. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1941. that will be selected if you call `C-c C-o' with a double
  1942. \\[universal-argument] \\[universal-argument] prefix.
  1943. Possible values for the command are:
  1944. `emacs' The file will be visited by the current Emacs process.
  1945. `default' Use the default application for this file type, which is the
  1946. association for t in the list, most likely in the system-specific
  1947. part.
  1948. This can be used to overrule an unwanted setting in the
  1949. system-specific variable.
  1950. `system' Use the system command for opening files, like \"open\".
  1951. This command is specified by the entry whose car is `system'.
  1952. Most likely, the system-specific version of this variable
  1953. does define this command, but you can overrule/replace it
  1954. here.
  1955. string A command to be executed by a shell; %s will be replaced
  1956. by the path to the file.
  1957. sexp A Lisp form which will be evaluated. The file path will
  1958. be available in the Lisp variable `file'.
  1959. For more examples, see the system specific constants
  1960. `org-file-apps-defaults-macosx'
  1961. `org-file-apps-defaults-windowsnt'
  1962. `org-file-apps-defaults-gnu'."
  1963. :group 'org-link-follow
  1964. :type '(repeat
  1965. (cons (choice :value ""
  1966. (string :tag "Extension")
  1967. (const :tag "System command to open files" system)
  1968. (const :tag "Default for unrecognized files" t)
  1969. (const :tag "Remote file" remote)
  1970. (const :tag "Links to a directory" directory)
  1971. (const :tag "Any files that have Emacs modes"
  1972. auto-mode))
  1973. (choice :value ""
  1974. (const :tag "Visit with Emacs" emacs)
  1975. (const :tag "Use default" default)
  1976. (const :tag "Use the system command" system)
  1977. (string :tag "Command")
  1978. (sexp :tag "Lisp form")))))
  1979. (defcustom org-doi-server-url "http://dx.doi.org/"
  1980. "The URL of the DOI server."
  1981. :type 'string
  1982. :version "24.3"
  1983. :group 'org-link-follow)
  1984. (defgroup org-refile nil
  1985. "Options concerning refiling entries in Org-mode."
  1986. :tag "Org Refile"
  1987. :group 'org)
  1988. (defcustom org-directory "~/org"
  1989. "Directory with org files.
  1990. This is just a default location to look for Org files. There is no need
  1991. at all to put your files into this directory. It is only used in the
  1992. following situations:
  1993. 1. When a capture template specifies a target file that is not an
  1994. absolute path. The path will then be interpreted relative to
  1995. `org-directory'
  1996. 2. When a capture note is filed away in an interactive way (when exiting the
  1997. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1998. with `org-directory' as the default path."
  1999. :group 'org-refile
  2000. :group 'org-capture
  2001. :type 'directory)
  2002. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  2003. "Default target for storing notes.
  2004. Used as a fall back file for org-capture.el, for templates that
  2005. do not specify a target file."
  2006. :group 'org-refile
  2007. :group 'org-capture
  2008. :type '(choice
  2009. (const :tag "Default from remember-data-file" nil)
  2010. file))
  2011. (defcustom org-goto-interface 'outline
  2012. "The default interface to be used for `org-goto'.
  2013. Allowed values are:
  2014. outline The interface shows an outline of the relevant file
  2015. and the correct heading is found by moving through
  2016. the outline or by searching with incremental search.
  2017. outline-path-completion Headlines in the current buffer are offered via
  2018. completion. This is the interface also used by
  2019. the refile command."
  2020. :group 'org-refile
  2021. :type '(choice
  2022. (const :tag "Outline" outline)
  2023. (const :tag "Outline-path-completion" outline-path-completion)))
  2024. (defcustom org-goto-max-level 5
  2025. "Maximum target level when running `org-goto' with refile interface."
  2026. :group 'org-refile
  2027. :type 'integer)
  2028. (defcustom org-reverse-note-order nil
  2029. "Non-nil means store new notes at the beginning of a file or entry.
  2030. When nil, new notes will be filed to the end of a file or entry.
  2031. This can also be a list with cons cells of regular expressions that
  2032. are matched against file names, and values."
  2033. :group 'org-capture
  2034. :group 'org-refile
  2035. :type '(choice
  2036. (const :tag "Reverse always" t)
  2037. (const :tag "Reverse never" nil)
  2038. (repeat :tag "By file name regexp"
  2039. (cons regexp boolean))))
  2040. (defcustom org-log-refile nil
  2041. "Information to record when a task is refiled.
  2042. Possible values are:
  2043. nil Don't add anything
  2044. time Add a time stamp to the task
  2045. note Prompt for a note and add it with template `org-log-note-headings'
  2046. This option can also be set with on a per-file-basis with
  2047. #+STARTUP: nologrefile
  2048. #+STARTUP: logrefile
  2049. #+STARTUP: lognoterefile
  2050. You can have local logging settings for a subtree by setting the LOGGING
  2051. property to one or more of these keywords.
  2052. When bulk-refiling from the agenda, the value `note' is forbidden and
  2053. will temporarily be changed to `time'."
  2054. :group 'org-refile
  2055. :group 'org-progress
  2056. :version "24.1"
  2057. :type '(choice
  2058. (const :tag "No logging" nil)
  2059. (const :tag "Record timestamp" time)
  2060. (const :tag "Record timestamp with note." note)))
  2061. (defcustom org-refile-targets nil
  2062. "Targets for refiling entries with \\[org-refile].
  2063. This is a list of cons cells. Each cell contains:
  2064. - a specification of the files to be considered, either a list of files,
  2065. or a symbol whose function or variable value will be used to retrieve
  2066. a file name or a list of file names. If you use `org-agenda-files' for
  2067. that, all agenda files will be scanned for targets. Nil means consider
  2068. headings in the current buffer.
  2069. - A specification of how to find candidate refile targets. This may be
  2070. any of:
  2071. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2072. This tag has to be present in all target headlines, inheritance will
  2073. not be considered.
  2074. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2075. todo keyword.
  2076. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2077. headlines that are refiling targets.
  2078. - a cons cell (:level . N). Any headline of level N is considered a target.
  2079. Note that, when `org-odd-levels-only' is set, level corresponds to
  2080. order in hierarchy, not to the number of stars.
  2081. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2082. Note that, when `org-odd-levels-only' is set, level corresponds to
  2083. order in hierarchy, not to the number of stars.
  2084. Each element of this list generates a set of possible targets.
  2085. The union of these sets is presented (with completion) to
  2086. the user by `org-refile'.
  2087. You can set the variable `org-refile-target-verify-function' to a function
  2088. to verify each headline found by the simple criteria above.
  2089. When this variable is nil, all top-level headlines in the current buffer
  2090. are used, equivalent to the value `((nil . (:level . 1))'."
  2091. :group 'org-refile
  2092. :type '(repeat
  2093. (cons
  2094. (choice :value org-agenda-files
  2095. (const :tag "All agenda files" org-agenda-files)
  2096. (const :tag "Current buffer" nil)
  2097. (function) (variable) (file))
  2098. (choice :tag "Identify target headline by"
  2099. (cons :tag "Specific tag" (const :value :tag) (string))
  2100. (cons :tag "TODO keyword" (const :value :todo) (string))
  2101. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2102. (cons :tag "Level number" (const :value :level) (integer))
  2103. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2104. (defcustom org-refile-target-verify-function nil
  2105. "Function to verify if the headline at point should be a refile target.
  2106. The function will be called without arguments, with point at the
  2107. beginning of the headline. It should return t and leave point
  2108. where it is if the headline is a valid target for refiling.
  2109. If the target should not be selected, the function must return nil.
  2110. In addition to this, it may move point to a place from where the search
  2111. should be continued. For example, the function may decide that the entire
  2112. subtree of the current entry should be excluded and move point to the end
  2113. of the subtree."
  2114. :group 'org-refile
  2115. :type '(choice
  2116. (const nil)
  2117. (function)))
  2118. (defcustom org-refile-use-cache nil
  2119. "Non-nil means cache refile targets to speed up the process.
  2120. The cache for a particular file will be updated automatically when
  2121. the buffer has been killed, or when any of the marker used for flagging
  2122. refile targets no longer points at a live buffer.
  2123. If you have added new entries to a buffer that might themselves be targets,
  2124. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2125. find that easier, `C-u C-u C-u C-c C-w'."
  2126. :group 'org-refile
  2127. :version "24.1"
  2128. :type 'boolean)
  2129. (defcustom org-refile-use-outline-path nil
  2130. "Non-nil means provide refile targets as paths.
  2131. So a level 3 headline will be available as level1/level2/level3.
  2132. When the value is `file', also include the file name (without directory)
  2133. into the path. In this case, you can also stop the completion after
  2134. the file name, to get entries inserted as top level in the file.
  2135. When `full-file-path', include the full file path."
  2136. :group 'org-refile
  2137. :type '(choice
  2138. (const :tag "Not" nil)
  2139. (const :tag "Yes" t)
  2140. (const :tag "Start with file name" file)
  2141. (const :tag "Start with full file path" full-file-path)))
  2142. (defcustom org-outline-path-complete-in-steps t
  2143. "Non-nil means complete the outline path in hierarchical steps.
  2144. When Org-mode uses the refile interface to select an outline path
  2145. \(see variable `org-refile-use-outline-path'), the completion of
  2146. the path can be done is a single go, or if can be done in steps down
  2147. the headline hierarchy. Going in steps is probably the best if you
  2148. do not use a special completion package like `ido' or `icicles'.
  2149. However, when using these packages, going in one step can be very
  2150. fast, while still showing the whole path to the entry."
  2151. :group 'org-refile
  2152. :type 'boolean)
  2153. (defcustom org-refile-allow-creating-parent-nodes nil
  2154. "Non-nil means allow to create new nodes as refile targets.
  2155. New nodes are then created by adding \"/new node name\" to the completion
  2156. of an existing node. When the value of this variable is `confirm',
  2157. new node creation must be confirmed by the user (recommended).
  2158. When nil, the completion must match an existing entry.
  2159. Note that, if the new heading is not seen by the criteria
  2160. listed in `org-refile-targets', multiple instances of the same
  2161. heading would be created by trying again to file under the new
  2162. heading."
  2163. :group 'org-refile
  2164. :type '(choice
  2165. (const :tag "Never" nil)
  2166. (const :tag "Always" t)
  2167. (const :tag "Prompt for confirmation" confirm)))
  2168. (defcustom org-refile-active-region-within-subtree nil
  2169. "Non-nil means also refile active region within a subtree.
  2170. By default `org-refile' doesn't allow refiling regions if they
  2171. don't contain a set of subtrees, but it might be convenient to
  2172. do so sometimes: in that case, the first line of the region is
  2173. converted to a headline before refiling."
  2174. :group 'org-refile
  2175. :version "24.1"
  2176. :type 'boolean)
  2177. (defgroup org-todo nil
  2178. "Options concerning TODO items in Org-mode."
  2179. :tag "Org TODO"
  2180. :group 'org)
  2181. (defgroup org-progress nil
  2182. "Options concerning Progress logging in Org-mode."
  2183. :tag "Org Progress"
  2184. :group 'org-time)
  2185. (defvar org-todo-interpretation-widgets
  2186. '((:tag "Sequence (cycling hits every state)" sequence)
  2187. (:tag "Type (cycling directly to DONE)" type))
  2188. "The available interpretation symbols for customizing `org-todo-keywords'.
  2189. Interested libraries should add to this list.")
  2190. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2191. "List of TODO entry keyword sequences and their interpretation.
  2192. \\<org-mode-map>This is a list of sequences.
  2193. Each sequence starts with a symbol, either `sequence' or `type',
  2194. indicating if the keywords should be interpreted as a sequence of
  2195. action steps, or as different types of TODO items. The first
  2196. keywords are states requiring action - these states will select a headline
  2197. for inclusion into the global TODO list Org-mode produces. If one of
  2198. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2199. signify that no further action is necessary. If \"|\" is not found,
  2200. the last keyword is treated as the only DONE state of the sequence.
  2201. The command \\[org-todo] cycles an entry through these states, and one
  2202. additional state where no keyword is present. For details about this
  2203. cycling, see the manual.
  2204. TODO keywords and interpretation can also be set on a per-file basis with
  2205. the special #+SEQ_TODO and #+TYP_TODO lines.
  2206. Each keyword can optionally specify a character for fast state selection
  2207. \(in combination with the variable `org-use-fast-todo-selection')
  2208. and specifiers for state change logging, using the same syntax that
  2209. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2210. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2211. indicates to record a time stamp each time this state is selected.
  2212. Each keyword may also specify if a timestamp or a note should be
  2213. recorded when entering or leaving the state, by adding additional
  2214. characters in the parenthesis after the keyword. This looks like this:
  2215. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2216. record only the time of the state change. With X and Y being either
  2217. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2218. Y when leaving the state if and only if the *target* state does not
  2219. define X. You may omit any of the fast-selection key or X or /Y,
  2220. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2221. For backward compatibility, this variable may also be just a list
  2222. of keywords. In this case the interpretation (sequence or type) will be
  2223. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2224. :group 'org-todo
  2225. :group 'org-keywords
  2226. :type '(choice
  2227. (repeat :tag "Old syntax, just keywords"
  2228. (string :tag "Keyword"))
  2229. (repeat :tag "New syntax"
  2230. (cons
  2231. (choice
  2232. :tag "Interpretation"
  2233. ;;Quick and dirty way to see
  2234. ;;`org-todo-interpretations'. This takes the
  2235. ;;place of item arguments
  2236. :convert-widget
  2237. (lambda (widget)
  2238. (widget-put widget
  2239. :args (mapcar
  2240. (lambda (x)
  2241. (widget-convert
  2242. (cons 'const x)))
  2243. org-todo-interpretation-widgets))
  2244. widget))
  2245. (repeat
  2246. (string :tag "Keyword"))))))
  2247. (defvar org-todo-keywords-1 nil
  2248. "All TODO and DONE keywords active in a buffer.")
  2249. (make-variable-buffer-local 'org-todo-keywords-1)
  2250. (defvar org-todo-keywords-for-agenda nil)
  2251. (defvar org-done-keywords-for-agenda nil)
  2252. (defvar org-todo-keyword-alist-for-agenda nil)
  2253. (defvar org-tag-alist-for-agenda nil
  2254. "Alist of all tags from all agenda files.")
  2255. (defvar org-tag-groups-alist-for-agenda nil
  2256. "Alist of all groups tags from all current agenda files.")
  2257. (defvar org-tag-groups-alist nil)
  2258. (make-variable-buffer-local 'org-tag-groups-alist)
  2259. (defvar org-agenda-contributing-files nil)
  2260. (defvar org-not-done-keywords nil)
  2261. (make-variable-buffer-local 'org-not-done-keywords)
  2262. (defvar org-done-keywords nil)
  2263. (make-variable-buffer-local 'org-done-keywords)
  2264. (defvar org-todo-heads nil)
  2265. (make-variable-buffer-local 'org-todo-heads)
  2266. (defvar org-todo-sets nil)
  2267. (make-variable-buffer-local 'org-todo-sets)
  2268. (defvar org-todo-log-states nil)
  2269. (make-variable-buffer-local 'org-todo-log-states)
  2270. (defvar org-todo-kwd-alist nil)
  2271. (make-variable-buffer-local 'org-todo-kwd-alist)
  2272. (defvar org-todo-key-alist nil)
  2273. (make-variable-buffer-local 'org-todo-key-alist)
  2274. (defvar org-todo-key-trigger nil)
  2275. (make-variable-buffer-local 'org-todo-key-trigger)
  2276. (defcustom org-todo-interpretation 'sequence
  2277. "Controls how TODO keywords are interpreted.
  2278. This variable is in principle obsolete and is only used for
  2279. backward compatibility, if the interpretation of todo keywords is
  2280. not given already in `org-todo-keywords'. See that variable for
  2281. more information."
  2282. :group 'org-todo
  2283. :group 'org-keywords
  2284. :type '(choice (const sequence)
  2285. (const type)))
  2286. (defcustom org-use-fast-todo-selection t
  2287. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2288. This variable describes if and under what circumstances the cycling
  2289. mechanism for TODO keywords will be replaced by a single-key, direct
  2290. selection scheme.
  2291. When nil, fast selection is never used.
  2292. When the symbol `prefix', it will be used when `org-todo' is called
  2293. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2294. `C-u t' in an agenda buffer.
  2295. When t, fast selection is used by default. In this case, the prefix
  2296. argument forces cycling instead.
  2297. In all cases, the special interface is only used if access keys have
  2298. actually been assigned by the user, i.e. if keywords in the configuration
  2299. are followed by a letter in parenthesis, like TODO(t)."
  2300. :group 'org-todo
  2301. :type '(choice
  2302. (const :tag "Never" nil)
  2303. (const :tag "By default" t)
  2304. (const :tag "Only with C-u C-c C-t" prefix)))
  2305. (defcustom org-provide-todo-statistics t
  2306. "Non-nil means update todo statistics after insert and toggle.
  2307. ALL-HEADLINES means update todo statistics by including headlines
  2308. with no TODO keyword as well, counting them as not done.
  2309. A list of TODO keywords means the same, but skip keywords that are
  2310. not in this list.
  2311. When set to a list of two lists, the first list contains keywords
  2312. to consider as TODO keywords, the second list contains keywords
  2313. to consider as DONE keywords.
  2314. When this is set, todo statistics is updated in the parent of the
  2315. current entry each time a todo state is changed."
  2316. :group 'org-todo
  2317. :type '(choice
  2318. (const :tag "Yes, only for TODO entries" t)
  2319. (const :tag "Yes, including all entries" all-headlines)
  2320. (repeat :tag "Yes, for TODOs in this list"
  2321. (string :tag "TODO keyword"))
  2322. (list :tag "Yes, for TODOs and DONEs in these lists"
  2323. (repeat (string :tag "TODO keyword"))
  2324. (repeat (string :tag "DONE keyword")))
  2325. (other :tag "No TODO statistics" nil)))
  2326. (defcustom org-hierarchical-todo-statistics t
  2327. "Non-nil means TODO statistics covers just direct children.
  2328. When nil, all entries in the subtree are considered.
  2329. This has only an effect if `org-provide-todo-statistics' is set.
  2330. To set this to nil for only a single subtree, use a COOKIE_DATA
  2331. property and include the word \"recursive\" into the value."
  2332. :group 'org-todo
  2333. :type 'boolean)
  2334. (defcustom org-after-todo-state-change-hook nil
  2335. "Hook which is run after the state of a TODO item was changed.
  2336. The new state (a string with a TODO keyword, or nil) is available in the
  2337. Lisp variable `org-state'."
  2338. :group 'org-todo
  2339. :type 'hook)
  2340. (defvar org-blocker-hook nil
  2341. "Hook for functions that are allowed to block a state change.
  2342. Functions in this hook should not modify the buffer.
  2343. Each function gets as its single argument a property list,
  2344. see `org-trigger-hook' for more information about this list.
  2345. If any of the functions in this hook returns nil, the state change
  2346. is blocked.")
  2347. (defvar org-trigger-hook nil
  2348. "Hook for functions that are triggered by a state change.
  2349. Each function gets as its single argument a property list with at
  2350. least the following elements:
  2351. (:type type-of-change :position pos-at-entry-start
  2352. :from old-state :to new-state)
  2353. Depending on the type, more properties may be present.
  2354. This mechanism is currently implemented for:
  2355. TODO state changes
  2356. ------------------
  2357. :type todo-state-change
  2358. :from previous state (keyword as a string), or nil, or a symbol
  2359. 'todo' or 'done', to indicate the general type of state.
  2360. :to new state, like in :from")
  2361. (defcustom org-enforce-todo-dependencies nil
  2362. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2363. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2364. be blocked if any prior sibling is not yet done.
  2365. Finally, if the parent is blocked because of ordered siblings of its own,
  2366. the child will also be blocked."
  2367. :set (lambda (var val)
  2368. (set var val)
  2369. (if val
  2370. (add-hook 'org-blocker-hook
  2371. 'org-block-todo-from-children-or-siblings-or-parent)
  2372. (remove-hook 'org-blocker-hook
  2373. 'org-block-todo-from-children-or-siblings-or-parent)))
  2374. :group 'org-todo
  2375. :type 'boolean)
  2376. (defcustom org-enforce-todo-checkbox-dependencies nil
  2377. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2378. When this is nil, checkboxes have no influence on switching TODO states.
  2379. When non-nil, you first need to check off all check boxes before the TODO
  2380. entry can be switched to DONE.
  2381. This variable needs to be set before org.el is loaded, and you need to
  2382. restart Emacs after a change to make the change effective. The only way
  2383. to change is while Emacs is running is through the customize interface."
  2384. :set (lambda (var val)
  2385. (set var val)
  2386. (if val
  2387. (add-hook 'org-blocker-hook
  2388. 'org-block-todo-from-checkboxes)
  2389. (remove-hook 'org-blocker-hook
  2390. 'org-block-todo-from-checkboxes)))
  2391. :group 'org-todo
  2392. :type 'boolean)
  2393. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2394. "Non-nil means inserting a TODO heading is treated as state change.
  2395. So when the command \\[org-insert-todo-heading] is used, state change
  2396. logging will apply if appropriate. When nil, the new TODO item will
  2397. be inserted directly, and no logging will take place."
  2398. :group 'org-todo
  2399. :type 'boolean)
  2400. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2401. "Non-nil means switching TODO states with S-cursor counts as state change.
  2402. This is the default behavior. However, setting this to nil allows a
  2403. convenient way to select a TODO state and bypass any logging associated
  2404. with that."
  2405. :group 'org-todo
  2406. :type 'boolean)
  2407. (defcustom org-todo-state-tags-triggers nil
  2408. "Tag changes that should be triggered by TODO state changes.
  2409. This is a list. Each entry is
  2410. (state-change (tag . flag) .......)
  2411. State-change can be a string with a state, and empty string to indicate the
  2412. state that has no TODO keyword, or it can be one of the symbols `todo'
  2413. or `done', meaning any not-done or done state, respectively."
  2414. :group 'org-todo
  2415. :group 'org-tags
  2416. :type '(repeat
  2417. (cons (choice :tag "When changing to"
  2418. (const :tag "Not-done state" todo)
  2419. (const :tag "Done state" done)
  2420. (string :tag "State"))
  2421. (repeat
  2422. (cons :tag "Tag action"
  2423. (string :tag "Tag")
  2424. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2425. (defcustom org-log-done nil
  2426. "Information to record when a task moves to the DONE state.
  2427. Possible values are:
  2428. nil Don't add anything, just change the keyword
  2429. time Add a time stamp to the task
  2430. note Prompt for a note and add it with template `org-log-note-headings'
  2431. This option can also be set with on a per-file-basis with
  2432. #+STARTUP: nologdone
  2433. #+STARTUP: logdone
  2434. #+STARTUP: lognotedone
  2435. You can have local logging settings for a subtree by setting the LOGGING
  2436. property to one or more of these keywords."
  2437. :group 'org-todo
  2438. :group 'org-progress
  2439. :type '(choice
  2440. (const :tag "No logging" nil)
  2441. (const :tag "Record CLOSED timestamp" time)
  2442. (const :tag "Record CLOSED timestamp with note." note)))
  2443. ;; Normalize old uses of org-log-done.
  2444. (cond
  2445. ((eq org-log-done t) (setq org-log-done 'time))
  2446. ((and (listp org-log-done) (memq 'done org-log-done))
  2447. (setq org-log-done 'note)))
  2448. (defcustom org-log-reschedule nil
  2449. "Information to record when the scheduling date of a tasks is modified.
  2450. Possible values are:
  2451. nil Don't add anything, just change the date
  2452. time Add a time stamp to the task
  2453. note Prompt for a note and add it with template `org-log-note-headings'
  2454. This option can also be set with on a per-file-basis with
  2455. #+STARTUP: nologreschedule
  2456. #+STARTUP: logreschedule
  2457. #+STARTUP: lognotereschedule"
  2458. :group 'org-todo
  2459. :group 'org-progress
  2460. :type '(choice
  2461. (const :tag "No logging" nil)
  2462. (const :tag "Record timestamp" time)
  2463. (const :tag "Record timestamp with note." note)))
  2464. (defcustom org-log-redeadline nil
  2465. "Information to record when the deadline date of a tasks is modified.
  2466. Possible values are:
  2467. nil Don't add anything, just change the date
  2468. time Add a time stamp to the task
  2469. note Prompt for a note and add it with template `org-log-note-headings'
  2470. This option can also be set with on a per-file-basis with
  2471. #+STARTUP: nologredeadline
  2472. #+STARTUP: logredeadline
  2473. #+STARTUP: lognoteredeadline
  2474. You can have local logging settings for a subtree by setting the LOGGING
  2475. property to one or more of these keywords."
  2476. :group 'org-todo
  2477. :group 'org-progress
  2478. :type '(choice
  2479. (const :tag "No logging" nil)
  2480. (const :tag "Record timestamp" time)
  2481. (const :tag "Record timestamp with note." note)))
  2482. (defcustom org-log-note-clock-out nil
  2483. "Non-nil means record a note when clocking out of an item.
  2484. This can also be configured on a per-file basis by adding one of
  2485. the following lines anywhere in the buffer:
  2486. #+STARTUP: lognoteclock-out
  2487. #+STARTUP: nolognoteclock-out"
  2488. :group 'org-todo
  2489. :group 'org-progress
  2490. :type 'boolean)
  2491. (defcustom org-log-done-with-time t
  2492. "Non-nil means the CLOSED time stamp will contain date and time.
  2493. When nil, only the date will be recorded."
  2494. :group 'org-progress
  2495. :type 'boolean)
  2496. (defcustom org-log-note-headings
  2497. '((done . "CLOSING NOTE %t")
  2498. (state . "State %-12s from %-12S %t")
  2499. (note . "Note taken on %t")
  2500. (reschedule . "Rescheduled from %S on %t")
  2501. (delschedule . "Not scheduled, was %S on %t")
  2502. (redeadline . "New deadline from %S on %t")
  2503. (deldeadline . "Removed deadline, was %S on %t")
  2504. (refile . "Refiled on %t")
  2505. (clock-out . ""))
  2506. "Headings for notes added to entries.
  2507. The value is an alist, with the car being a symbol indicating the note
  2508. context, and the cdr is the heading to be used. The heading may also be the
  2509. empty string.
  2510. %t in the heading will be replaced by a time stamp.
  2511. %T will be an active time stamp instead the default inactive one
  2512. %d will be replaced by a short-format time stamp.
  2513. %D will be replaced by an active short-format time stamp.
  2514. %s will be replaced by the new TODO state, in double quotes.
  2515. %S will be replaced by the old TODO state, in double quotes.
  2516. %u will be replaced by the user name.
  2517. %U will be replaced by the full user name.
  2518. In fact, it is not a good idea to change the `state' entry, because
  2519. agenda log mode depends on the format of these entries."
  2520. :group 'org-todo
  2521. :group 'org-progress
  2522. :type '(list :greedy t
  2523. (cons (const :tag "Heading when closing an item" done) string)
  2524. (cons (const :tag
  2525. "Heading when changing todo state (todo sequence only)"
  2526. state) string)
  2527. (cons (const :tag "Heading when just taking a note" note) string)
  2528. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2529. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2530. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2531. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2532. (cons (const :tag "Heading when refiling" refile) string)
  2533. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2534. (unless (assq 'note org-log-note-headings)
  2535. (push '(note . "%t") org-log-note-headings))
  2536. (defcustom org-log-into-drawer nil
  2537. "Non-nil means insert state change notes and time stamps into a drawer.
  2538. When nil, state changes notes will be inserted after the headline and
  2539. any scheduling and clock lines, but not inside a drawer.
  2540. The value of this variable should be the name of the drawer to use.
  2541. LOGBOOK is proposed as the default drawer for this purpose, you can
  2542. also set this to a string to define the drawer of your choice.
  2543. A value of t is also allowed, representing \"LOGBOOK\".
  2544. A value of t or nil can also be set with on a per-file-basis with
  2545. #+STARTUP: logdrawer
  2546. #+STARTUP: nologdrawer
  2547. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2548. will be ignored.
  2549. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2550. a subtree.
  2551. Do not check directly this variable in a Lisp program. Call
  2552. function `org-log-into-drawer' instead."
  2553. :group 'org-todo
  2554. :group 'org-progress
  2555. :type '(choice
  2556. (const :tag "Not into a drawer" nil)
  2557. (const :tag "LOGBOOK" t)
  2558. (string :tag "Other")))
  2559. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2560. (defun org-log-into-drawer ()
  2561. "Name of the log drawer, as a string, or nil.
  2562. This is the value of `org-log-into-drawer'. However, if the
  2563. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2564. be used instead of the default value."
  2565. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2566. (cond ((equal p "nil") nil)
  2567. ((equal p "t") "LOGBOOK")
  2568. ((stringp p) p)
  2569. (p "LOGBOOK")
  2570. ((stringp org-log-into-drawer) org-log-into-drawer)
  2571. (org-log-into-drawer "LOGBOOK"))))
  2572. (defcustom org-log-state-notes-insert-after-drawers nil
  2573. "Non-nil means insert state change notes after any drawers in entry.
  2574. Only the drawers that *immediately* follow the headline and the
  2575. deadline/scheduled line are skipped.
  2576. When nil, insert notes right after the heading and perhaps the line
  2577. with deadline/scheduling if present.
  2578. This variable will have no effect if `org-log-into-drawer' is
  2579. set."
  2580. :group 'org-todo
  2581. :group 'org-progress
  2582. :type 'boolean)
  2583. (defcustom org-log-states-order-reversed t
  2584. "Non-nil means the latest state note will be directly after heading.
  2585. When nil, the state change notes will be ordered according to time.
  2586. This option can also be set with on a per-file-basis with
  2587. #+STARTUP: logstatesreversed
  2588. #+STARTUP: nologstatesreversed"
  2589. :group 'org-todo
  2590. :group 'org-progress
  2591. :type 'boolean)
  2592. (defcustom org-todo-repeat-to-state nil
  2593. "The TODO state to which a repeater should return the repeating task.
  2594. By default this is the first task in a TODO sequence, or the previous state
  2595. in a TODO_TYP set. But you can specify another task here.
  2596. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2597. :group 'org-todo
  2598. :version "24.1"
  2599. :type '(choice (const :tag "Head of sequence" nil)
  2600. (string :tag "Specific state")))
  2601. (defcustom org-log-repeat 'time
  2602. "Non-nil means record moving through the DONE state when triggering repeat.
  2603. An auto-repeating task is immediately switched back to TODO when
  2604. marked DONE. If you are not logging state changes (by adding \"@\"
  2605. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2606. record a closing note, there will be no record of the task moving
  2607. through DONE. This variable forces taking a note anyway.
  2608. nil Don't force a record
  2609. time Record a time stamp
  2610. note Prompt for a note and add it with template `org-log-note-headings'
  2611. This option can also be set with on a per-file-basis with
  2612. #+STARTUP: nologrepeat
  2613. #+STARTUP: logrepeat
  2614. #+STARTUP: lognoterepeat
  2615. You can have local logging settings for a subtree by setting the LOGGING
  2616. property to one or more of these keywords."
  2617. :group 'org-todo
  2618. :group 'org-progress
  2619. :type '(choice
  2620. (const :tag "Don't force a record" nil)
  2621. (const :tag "Force recording the DONE state" time)
  2622. (const :tag "Force recording a note with the DONE state" note)))
  2623. (defgroup org-priorities nil
  2624. "Priorities in Org-mode."
  2625. :tag "Org Priorities"
  2626. :group 'org-todo)
  2627. (defcustom org-enable-priority-commands t
  2628. "Non-nil means priority commands are active.
  2629. When nil, these commands will be disabled, so that you never accidentally
  2630. set a priority."
  2631. :group 'org-priorities
  2632. :type 'boolean)
  2633. (defcustom org-highest-priority ?A
  2634. "The highest priority of TODO items. A character like ?A, ?B etc.
  2635. Must have a smaller ASCII number than `org-lowest-priority'."
  2636. :group 'org-priorities
  2637. :type 'character)
  2638. (defcustom org-lowest-priority ?C
  2639. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2640. Must have a larger ASCII number than `org-highest-priority'."
  2641. :group 'org-priorities
  2642. :type 'character)
  2643. (defcustom org-default-priority ?B
  2644. "The default priority of TODO items.
  2645. This is the priority an item gets if no explicit priority is given.
  2646. When starting to cycle on an empty priority the first step in the cycle
  2647. depends on `org-priority-start-cycle-with-default'. The resulting first
  2648. step priority must not exceed the range from `org-highest-priority' to
  2649. `org-lowest-priority' which means that `org-default-priority' has to be
  2650. in this range exclusive or inclusive the range boundaries. Else the
  2651. first step refuses to set the default and the second will fall back
  2652. to (depending on the command used) the highest or lowest priority."
  2653. :group 'org-priorities
  2654. :type 'character)
  2655. (defcustom org-priority-start-cycle-with-default t
  2656. "Non-nil means start with default priority when starting to cycle.
  2657. When this is nil, the first step in the cycle will be (depending on the
  2658. command used) one higher or lower than the default priority.
  2659. See also `org-default-priority'."
  2660. :group 'org-priorities
  2661. :type 'boolean)
  2662. (defcustom org-get-priority-function nil
  2663. "Function to extract the priority from a string.
  2664. The string is normally the headline. If this is nil Org computes the
  2665. priority from the priority cookie like [#A] in the headline. It returns
  2666. an integer, increasing by 1000 for each priority level.
  2667. The user can set a different function here, which should take a string
  2668. as an argument and return the numeric priority."
  2669. :group 'org-priorities
  2670. :version "24.1"
  2671. :type '(choice
  2672. (const nil)
  2673. (function)))
  2674. (defgroup org-time nil
  2675. "Options concerning time stamps and deadlines in Org-mode."
  2676. :tag "Org Time"
  2677. :group 'org)
  2678. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2679. "Number of minutes to round time stamps to.
  2680. These are two values, the first applies when first creating a time stamp.
  2681. The second applies when changing it with the commands `S-up' and `S-down'.
  2682. When changing the time stamp, this means that it will change in steps
  2683. of N minutes, as given by the second value.
  2684. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2685. numbers should be factors of 60, so for example 5, 10, 15.
  2686. When this is larger than 1, you can still force an exact time stamp by using
  2687. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2688. and by using a prefix arg to `S-up/down' to specify the exact number
  2689. of minutes to shift."
  2690. :group 'org-time
  2691. :get (lambda (var) ; Make sure both elements are there
  2692. (if (integerp (default-value var))
  2693. (list (default-value var) 5)
  2694. (default-value var)))
  2695. :type '(list
  2696. (integer :tag "when inserting times")
  2697. (integer :tag "when modifying times")))
  2698. ;; Normalize old customizations of this variable.
  2699. (when (integerp org-time-stamp-rounding-minutes)
  2700. (setq org-time-stamp-rounding-minutes
  2701. (list org-time-stamp-rounding-minutes
  2702. org-time-stamp-rounding-minutes)))
  2703. (defcustom org-display-custom-times nil
  2704. "Non-nil means overlay custom formats over all time stamps.
  2705. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2706. To turn this on on a per-file basis, insert anywhere in the file:
  2707. #+STARTUP: customtime"
  2708. :group 'org-time
  2709. :set 'set-default
  2710. :type 'sexp)
  2711. (make-variable-buffer-local 'org-display-custom-times)
  2712. (defcustom org-time-stamp-custom-formats
  2713. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2714. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2715. These are overlaid over the default ISO format if the variable
  2716. `org-display-custom-times' is set. Time like %H:%M should be at the
  2717. end of the second format. The custom formats are also honored by export
  2718. commands, if custom time display is turned on at the time of export."
  2719. :group 'org-time
  2720. :type 'sexp)
  2721. (defun org-time-stamp-format (&optional long inactive)
  2722. "Get the right format for a time string."
  2723. (let ((f (if long (cdr org-time-stamp-formats)
  2724. (car org-time-stamp-formats))))
  2725. (if inactive
  2726. (concat "[" (substring f 1 -1) "]")
  2727. f)))
  2728. (defcustom org-time-clocksum-format
  2729. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2730. "The format string used when creating CLOCKSUM lines.
  2731. This is also used when Org mode generates a time duration.
  2732. The value can be a single format string containing two
  2733. %-sequences, which will be filled with the number of hours and
  2734. minutes in that order.
  2735. Alternatively, the value can be a plist associating any of the
  2736. keys :years, :months, :weeks, :days, :hours or :minutes with
  2737. format strings. The time duration is formatted using only the
  2738. time components that are needed and concatenating the results.
  2739. If a time unit in absent, it falls back to the next smallest
  2740. unit.
  2741. The keys :require-years, :require-months, :require-days,
  2742. :require-weeks, :require-hours, :require-minutes are also
  2743. meaningful. A non-nil value for these keys indicates that the
  2744. corresponding time component should always be included, even if
  2745. its value is 0.
  2746. For example,
  2747. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2748. :require-minutes t)
  2749. means durations longer than a day will be expressed in days,
  2750. hours and minutes, and durations less than a day will always be
  2751. expressed in hours and minutes (even for durations less than an
  2752. hour).
  2753. The value
  2754. \(:days \"%dd\" :minutes \"%dm\")
  2755. means durations longer than a day will be expressed in days and
  2756. minutes, and durations less than a day will be expressed entirely
  2757. in minutes (even for durations longer than an hour)."
  2758. :group 'org-time
  2759. :group 'org-clock
  2760. :version "24.4"
  2761. :package-version '(Org . "8.0")
  2762. :type '(choice (string :tag "Format string")
  2763. (set :tag "Plist"
  2764. (group :inline t (const :tag "Years" :years)
  2765. (string :tag "Format string"))
  2766. (group :inline t
  2767. (const :tag "Always show years" :require-years)
  2768. (const t))
  2769. (group :inline t (const :tag "Months" :months)
  2770. (string :tag "Format string"))
  2771. (group :inline t
  2772. (const :tag "Always show months" :require-months)
  2773. (const t))
  2774. (group :inline t (const :tag "Weeks" :weeks)
  2775. (string :tag "Format string"))
  2776. (group :inline t
  2777. (const :tag "Always show weeks" :require-weeks)
  2778. (const t))
  2779. (group :inline t (const :tag "Days" :days)
  2780. (string :tag "Format string"))
  2781. (group :inline t
  2782. (const :tag "Always show days" :require-days)
  2783. (const t))
  2784. (group :inline t (const :tag "Hours" :hours)
  2785. (string :tag "Format string"))
  2786. (group :inline t
  2787. (const :tag "Always show hours" :require-hours)
  2788. (const t))
  2789. (group :inline t (const :tag "Minutes" :minutes)
  2790. (string :tag "Format string"))
  2791. (group :inline t
  2792. (const :tag "Always show minutes" :require-minutes)
  2793. (const t)))))
  2794. (defcustom org-time-clocksum-use-fractional nil
  2795. "When non-nil, \\[org-clock-display] uses fractional times.
  2796. See `org-time-clocksum-format' for more on time clock formats."
  2797. :group 'org-time
  2798. :group 'org-clock
  2799. :version "24.3"
  2800. :type 'boolean)
  2801. (defcustom org-time-clocksum-use-effort-durations nil
  2802. "When non-nil, \\[org-clock-display] uses effort durations.
  2803. E.g. by default, one day is considered to be a 8 hours effort,
  2804. so a task that has been clocked for 16 hours will be displayed
  2805. as during 2 days in the clock display or in the clocktable.
  2806. See `org-effort-durations' on how to set effort durations
  2807. and `org-time-clocksum-format' for more on time clock formats."
  2808. :group 'org-time
  2809. :group 'org-clock
  2810. :version "24.4"
  2811. :package-version '(Org . "8.0")
  2812. :type 'boolean)
  2813. (defcustom org-time-clocksum-fractional-format "%.2f"
  2814. "The format string used when creating CLOCKSUM lines,
  2815. or when Org mode generates a time duration, if
  2816. `org-time-clocksum-use-fractional' is enabled.
  2817. The value can be a single format string containing one
  2818. %-sequence, which will be filled with the number of hours as
  2819. a float.
  2820. Alternatively, the value can be a plist associating any of the
  2821. keys :years, :months, :weeks, :days, :hours or :minutes with
  2822. a format string. The time duration is formatted using the
  2823. largest time unit which gives a non-zero integer part. If all
  2824. specified formats have zero integer part, the smallest time unit
  2825. is used."
  2826. :group 'org-time
  2827. :type '(choice (string :tag "Format string")
  2828. (set (group :inline t (const :tag "Years" :years)
  2829. (string :tag "Format string"))
  2830. (group :inline t (const :tag "Months" :months)
  2831. (string :tag "Format string"))
  2832. (group :inline t (const :tag "Weeks" :weeks)
  2833. (string :tag "Format string"))
  2834. (group :inline t (const :tag "Days" :days)
  2835. (string :tag "Format string"))
  2836. (group :inline t (const :tag "Hours" :hours)
  2837. (string :tag "Format string"))
  2838. (group :inline t (const :tag "Minutes" :minutes)
  2839. (string :tag "Format string")))))
  2840. (defcustom org-deadline-warning-days 14
  2841. "Number of days before expiration during which a deadline becomes active.
  2842. This variable governs the display in sparse trees and in the agenda.
  2843. When 0 or negative, it means use this number (the absolute value of it)
  2844. even if a deadline has a different individual lead time specified.
  2845. Custom commands can set this variable in the options section."
  2846. :group 'org-time
  2847. :group 'org-agenda-daily/weekly
  2848. :type 'integer)
  2849. (defcustom org-scheduled-delay-days 0
  2850. "Number of days before a scheduled item becomes active.
  2851. This variable governs the display in sparse trees and in the agenda.
  2852. The default value (i.e. 0) means: don't delay scheduled item.
  2853. When negative, it means use this number (the absolute value of it)
  2854. even if a scheduled item has a different individual delay time
  2855. specified.
  2856. Custom commands can set this variable in the options section."
  2857. :group 'org-time
  2858. :group 'org-agenda-daily/weekly
  2859. :version "24.4"
  2860. :package-version '(Org . "8.0")
  2861. :type 'integer)
  2862. (defcustom org-read-date-prefer-future t
  2863. "Non-nil means assume future for incomplete date input from user.
  2864. This affects the following situations:
  2865. 1. The user gives a month but not a year.
  2866. For example, if it is April and you enter \"feb 2\", this will be read
  2867. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2868. 2. The user gives a day, but no month.
  2869. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2870. read this as the third of *next* month. However, if you enter \"17\",
  2871. it will be considered as *this* month.
  2872. If you set this variable to the symbol `time', then also the following
  2873. will work:
  2874. 3. If the user gives a time.
  2875. If the time is before now, it will be interpreted as tomorrow.
  2876. Currently none of this works for ISO week specifications.
  2877. When this option is nil, the current day, month and year will always be
  2878. used as defaults.
  2879. See also `org-agenda-jump-prefer-future'."
  2880. :group 'org-time
  2881. :type '(choice
  2882. (const :tag "Never" nil)
  2883. (const :tag "Check month and day" t)
  2884. (const :tag "Check month, day, and time" time)))
  2885. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2886. "Should the agenda jump command prefer the future for incomplete dates?
  2887. The default is to do the same as configured in `org-read-date-prefer-future'.
  2888. But you can also set a deviating value here.
  2889. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2890. :group 'org-agenda
  2891. :group 'org-time
  2892. :version "24.1"
  2893. :type '(choice
  2894. (const :tag "Use org-read-date-prefer-future"
  2895. org-read-date-prefer-future)
  2896. (const :tag "Never" nil)
  2897. (const :tag "Always" t)))
  2898. (defcustom org-read-date-force-compatible-dates t
  2899. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2900. Depending on the system Emacs is running on, certain dates cannot
  2901. be represented with the type used internally to represent time.
  2902. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2903. correctly. Some systems allow for earlier dates, some for later,
  2904. some for both. One way to find out it to insert any date into an
  2905. Org buffer, putting the cursor on the year and hitting S-up and
  2906. S-down to test the range.
  2907. When this variable is set to t, the date/time prompt will not let
  2908. you specify dates outside the 1970-2037 range, so it is certain that
  2909. these dates will work in whatever version of Emacs you are
  2910. running, and also that you can move a file from one Emacs implementation
  2911. to another. WHenever Org is forcing the year for you, it will display
  2912. a message and beep.
  2913. When this variable is nil, Org will check if the date is
  2914. representable in the specific Emacs implementation you are using.
  2915. If not, it will force a year, usually the current year, and beep
  2916. to remind you. Currently this setting is not recommended because
  2917. the likelihood that you will open your Org files in an Emacs that
  2918. has limited date range is not negligible.
  2919. A workaround for this problem is to use diary sexp dates for time
  2920. stamps outside of this range."
  2921. :group 'org-time
  2922. :version "24.1"
  2923. :type 'boolean)
  2924. (defcustom org-read-date-display-live t
  2925. "Non-nil means display current interpretation of date prompt live.
  2926. This display will be in an overlay, in the minibuffer."
  2927. :group 'org-time
  2928. :type 'boolean)
  2929. (defcustom org-read-date-popup-calendar t
  2930. "Non-nil means pop up a calendar when prompting for a date.
  2931. In the calendar, the date can be selected with mouse-1. However, the
  2932. minibuffer will also be active, and you can simply enter the date as well.
  2933. When nil, only the minibuffer will be available."
  2934. :group 'org-time
  2935. :type 'boolean)
  2936. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2937. 'org-read-date-popup-calendar)
  2938. (make-obsolete-variable
  2939. 'org-read-date-minibuffer-setup-hook
  2940. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2941. (defcustom org-read-date-minibuffer-setup-hook nil
  2942. "Hook to be used to set up keys for the date/time interface.
  2943. Add key definitions to `minibuffer-local-map', which will be a
  2944. temporary copy.
  2945. WARNING: This option is obsolete, you should use
  2946. `org-read-date-minibuffer-local-map' to set up keys."
  2947. :group 'org-time
  2948. :type 'hook)
  2949. (defcustom org-extend-today-until 0
  2950. "The hour when your day really ends. Must be an integer.
  2951. This has influence for the following applications:
  2952. - When switching the agenda to \"today\". It it is still earlier than
  2953. the time given here, the day recognized as TODAY is actually yesterday.
  2954. - When a date is read from the user and it is still before the time given
  2955. here, the current date and time will be assumed to be yesterday, 23:59.
  2956. Also, timestamps inserted in capture templates follow this rule.
  2957. IMPORTANT: This is a feature whose implementation is and likely will
  2958. remain incomplete. Really, it is only here because past midnight seems to
  2959. be the favorite working time of John Wiegley :-)"
  2960. :group 'org-time
  2961. :type 'integer)
  2962. (defcustom org-use-effective-time nil
  2963. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2964. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2965. \"effective time\" of any timestamps between midnight and 8am will be
  2966. 23:59 of the previous day."
  2967. :group 'org-time
  2968. :version "24.1"
  2969. :type 'boolean)
  2970. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2971. "When non-nil, use the last clock out time for `org-todo'.
  2972. Note that this option has precedence over the combined use of
  2973. `org-use-effective-time' and `org-extend-today-until'."
  2974. :group 'org-time
  2975. :version "24.4"
  2976. :package-version '(Org . "8.0")
  2977. :type 'boolean)
  2978. (defcustom org-edit-timestamp-down-means-later nil
  2979. "Non-nil means S-down will increase the time in a time stamp.
  2980. When nil, S-up will increase."
  2981. :group 'org-time
  2982. :type 'boolean)
  2983. (defcustom org-calendar-follow-timestamp-change t
  2984. "Non-nil means make the calendar window follow timestamp changes.
  2985. When a timestamp is modified and the calendar window is visible, it will be
  2986. moved to the new date."
  2987. :group 'org-time
  2988. :type 'boolean)
  2989. (defgroup org-tags nil
  2990. "Options concerning tags in Org-mode."
  2991. :tag "Org Tags"
  2992. :group 'org)
  2993. (defcustom org-tag-alist nil
  2994. "List of tags allowed in Org-mode files.
  2995. When this list is nil, Org-mode will base TAG input on what is already in the
  2996. buffer.
  2997. The value of this variable is an alist, the car of each entry must be a
  2998. keyword as a string, the cdr may be a character that is used to select
  2999. that tag through the fast-tag-selection interface.
  3000. See the manual for details."
  3001. :group 'org-tags
  3002. :type '(repeat
  3003. (choice
  3004. (cons (string :tag "Tag name")
  3005. (character :tag "Access char"))
  3006. (list :tag "Start radio group"
  3007. (const :startgroup)
  3008. (option (string :tag "Group description")))
  3009. (list :tag "Start tag group, non distinct"
  3010. (const :startgrouptag)
  3011. (option (string :tag "Group description")))
  3012. (list :tag "Group tags delimiter"
  3013. (const :grouptags))
  3014. (list :tag "End radio group"
  3015. (const :endgroup)
  3016. (option (string :tag "Group description")))
  3017. (list :tag "End tag group, non distinct"
  3018. (const :endgrouptag)
  3019. (option (string :tag "Group description")))
  3020. (const :tag "New line" (:newline)))))
  3021. (defcustom org-tag-persistent-alist nil
  3022. "List of tags that will always appear in all Org-mode files.
  3023. This is in addition to any in buffer settings or customizations
  3024. of `org-tag-alist'.
  3025. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3026. The value of this variable is an alist, the car of each entry must be a
  3027. keyword as a string, the cdr may be a character that is used to select
  3028. that tag through the fast-tag-selection interface.
  3029. See the manual for details.
  3030. To disable these tags on a per-file basis, insert anywhere in the file:
  3031. #+STARTUP: noptag"
  3032. :group 'org-tags
  3033. :type '(repeat
  3034. (choice
  3035. (cons (string :tag "Tag name")
  3036. (character :tag "Access char"))
  3037. (const :tag "Start radio group" (:startgroup))
  3038. (const :tag "Group tags delimiter" (:grouptags))
  3039. (const :tag "End radio group" (:endgroup))
  3040. (const :tag "New line" (:newline)))))
  3041. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3042. "If non-nil, always offer completion for all tags of all agenda files.
  3043. Instead of customizing this variable directly, you might want to
  3044. set it locally for capture buffers, because there no list of
  3045. tags in that file can be created dynamically (there are none).
  3046. (add-hook 'org-capture-mode-hook
  3047. (lambda ()
  3048. (set (make-local-variable
  3049. 'org-complete-tags-always-offer-all-agenda-tags)
  3050. t)))"
  3051. :group 'org-tags
  3052. :version "24.1"
  3053. :type 'boolean)
  3054. (defvar org-file-tags nil
  3055. "List of tags that can be inherited by all entries in the file.
  3056. The tags will be inherited if the variable `org-use-tag-inheritance'
  3057. says they should be.
  3058. This variable is populated from #+FILETAGS lines.")
  3059. (defcustom org-use-fast-tag-selection 'auto
  3060. "Non-nil means use fast tag selection scheme.
  3061. This is a special interface to select and deselect tags with single keys.
  3062. When nil, fast selection is never used.
  3063. When the symbol `auto', fast selection is used if and only if selection
  3064. characters for tags have been configured, either through the variable
  3065. `org-tag-alist' or through a #+TAGS line in the buffer.
  3066. When t, fast selection is always used and selection keys are assigned
  3067. automatically if necessary."
  3068. :group 'org-tags
  3069. :type '(choice
  3070. (const :tag "Always" t)
  3071. (const :tag "Never" nil)
  3072. (const :tag "When selection characters are configured" auto)))
  3073. (defcustom org-fast-tag-selection-single-key nil
  3074. "Non-nil means fast tag selection exits after first change.
  3075. When nil, you have to press RET to exit it.
  3076. During fast tag selection, you can toggle this flag with `C-c'.
  3077. This variable can also have the value `expert'. In this case, the window
  3078. displaying the tags menu is not even shown, until you press C-c again."
  3079. :group 'org-tags
  3080. :type '(choice
  3081. (const :tag "No" nil)
  3082. (const :tag "Yes" t)
  3083. (const :tag "Expert" expert)))
  3084. (defvar org-fast-tag-selection-include-todo nil
  3085. "Non-nil means fast tags selection interface will also offer TODO states.
  3086. This is an undocumented feature, you should not rely on it.")
  3087. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3088. "The column to which tags should be indented in a headline.
  3089. If this number is positive, it specifies the column. If it is negative,
  3090. it means that the tags should be flushright to that column. For example,
  3091. -80 works well for a normal 80 character screen.
  3092. When 0, place tags directly after headline text, with only one space in
  3093. between."
  3094. :group 'org-tags
  3095. :type 'integer)
  3096. (defcustom org-auto-align-tags t
  3097. "Non-nil keeps tags aligned when modifying headlines.
  3098. Some operations (i.e. demoting) change the length of a headline and
  3099. therefore shift the tags around. With this option turned on, after
  3100. each such operation the tags are again aligned to `org-tags-column'."
  3101. :group 'org-tags
  3102. :type 'boolean)
  3103. (defcustom org-use-tag-inheritance t
  3104. "Non-nil means tags in levels apply also for sublevels.
  3105. When nil, only the tags directly given in a specific line apply there.
  3106. This may also be a list of tags that should be inherited, or a regexp that
  3107. matches tags that should be inherited. Additional control is possible
  3108. with the variable `org-tags-exclude-from-inheritance' which gives an
  3109. explicit list of tags to be excluded from inheritance, even if the value of
  3110. `org-use-tag-inheritance' would select it for inheritance.
  3111. If this option is t, a match early-on in a tree can lead to a large
  3112. number of matches in the subtree when constructing the agenda or creating
  3113. a sparse tree. If you only want to see the first match in a tree during
  3114. a search, check out the variable `org-tags-match-list-sublevels'."
  3115. :group 'org-tags
  3116. :type '(choice
  3117. (const :tag "Not" nil)
  3118. (const :tag "Always" t)
  3119. (repeat :tag "Specific tags" (string :tag "Tag"))
  3120. (regexp :tag "Tags matched by regexp")))
  3121. (defcustom org-tags-exclude-from-inheritance nil
  3122. "List of tags that should never be inherited.
  3123. This is a way to exclude a few tags from inheritance. For way to do
  3124. the opposite, to actively allow inheritance for selected tags,
  3125. see the variable `org-use-tag-inheritance'."
  3126. :group 'org-tags
  3127. :type '(repeat (string :tag "Tag")))
  3128. (defun org-tag-inherit-p (tag)
  3129. "Check if TAG is one that should be inherited."
  3130. (cond
  3131. ((member tag org-tags-exclude-from-inheritance) nil)
  3132. ((eq org-use-tag-inheritance t) t)
  3133. ((not org-use-tag-inheritance) nil)
  3134. ((stringp org-use-tag-inheritance)
  3135. (string-match org-use-tag-inheritance tag))
  3136. ((listp org-use-tag-inheritance)
  3137. (member tag org-use-tag-inheritance))
  3138. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3139. (defcustom org-tags-match-list-sublevels t
  3140. "Non-nil means list also sublevels of headlines matching a search.
  3141. This variable applies to tags/property searches, and also to stuck
  3142. projects because this search is based on a tags match as well.
  3143. When set to the symbol `indented', sublevels are indented with
  3144. leading dots.
  3145. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3146. the sublevels of a headline matching a tag search often also match
  3147. the same search. Listing all of them can create very long lists.
  3148. Setting this variable to nil causes subtrees of a match to be skipped.
  3149. This variable is semi-obsolete and probably should always be true. It
  3150. is better to limit inheritance to certain tags using the variables
  3151. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3152. :group 'org-tags
  3153. :type '(choice
  3154. (const :tag "No, don't list them" nil)
  3155. (const :tag "Yes, do list them" t)
  3156. (const :tag "List them, indented with leading dots" indented)))
  3157. (defcustom org-tags-sort-function nil
  3158. "When set, tags are sorted using this function as a comparator."
  3159. :group 'org-tags
  3160. :type '(choice
  3161. (const :tag "No sorting" nil)
  3162. (const :tag "Alphabetical" string<)
  3163. (const :tag "Reverse alphabetical" string>)
  3164. (function :tag "Custom function" nil)))
  3165. (defvar org-tags-history nil
  3166. "History of minibuffer reads for tags.")
  3167. (defvar org-last-tags-completion-table nil
  3168. "The last used completion table for tags.")
  3169. (defvar org-after-tags-change-hook nil
  3170. "Hook that is run after the tags in a line have changed.")
  3171. (defgroup org-properties nil
  3172. "Options concerning properties in Org-mode."
  3173. :tag "Org Properties"
  3174. :group 'org)
  3175. (defcustom org-property-format "%-10s %s"
  3176. "How property key/value pairs should be formatted by `indent-line'.
  3177. When `indent-line' hits a property definition, it will format the line
  3178. according to this format, mainly to make sure that the values are
  3179. lined-up with respect to each other."
  3180. :group 'org-properties
  3181. :type 'string)
  3182. (defcustom org-properties-postprocess-alist nil
  3183. "Alist of properties and functions to adjust inserted values.
  3184. Elements of this alist must be of the form
  3185. ([string] [function])
  3186. where [string] must be a property name and [function] must be a
  3187. lambda expression: this lambda expression must take one argument,
  3188. the value to adjust, and return the new value as a string.
  3189. For example, this element will allow the property \"Remaining\"
  3190. to be updated wrt the relation between the \"Effort\" property
  3191. and the clock summary:
  3192. ((\"Remaining\" (lambda(value)
  3193. (let ((clocksum (org-clock-sum-current-item))
  3194. (effort (org-duration-string-to-minutes
  3195. (org-entry-get (point) \"Effort\"))))
  3196. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3197. :group 'org-properties
  3198. :version "24.1"
  3199. :type '(alist :key-type (string :tag "Property")
  3200. :value-type (function :tag "Function")))
  3201. (defcustom org-use-property-inheritance nil
  3202. "Non-nil means properties apply also for sublevels.
  3203. This setting is chiefly used during property searches. Turning it on can
  3204. cause significant overhead when doing a search, which is why it is not
  3205. on by default.
  3206. When nil, only the properties directly given in the current entry count.
  3207. When t, every property is inherited. The value may also be a list of
  3208. properties that should have inheritance, or a regular expression matching
  3209. properties that should be inherited.
  3210. However, note that some special properties use inheritance under special
  3211. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3212. and the properties ending in \"_ALL\" when they are used as descriptor
  3213. for valid values of a property.
  3214. Note for programmers:
  3215. When querying an entry with `org-entry-get', you can control if inheritance
  3216. should be used. By default, `org-entry-get' looks only at the local
  3217. properties. You can request inheritance by setting the inherit argument
  3218. to t (to force inheritance) or to `selective' (to respect the setting
  3219. in this variable)."
  3220. :group 'org-properties
  3221. :type '(choice
  3222. (const :tag "Not" nil)
  3223. (const :tag "Always" t)
  3224. (repeat :tag "Specific properties" (string :tag "Property"))
  3225. (regexp :tag "Properties matched by regexp")))
  3226. (defun org-property-inherit-p (property)
  3227. "Check if PROPERTY is one that should be inherited."
  3228. (cond
  3229. ((eq org-use-property-inheritance t) t)
  3230. ((not org-use-property-inheritance) nil)
  3231. ((stringp org-use-property-inheritance)
  3232. (string-match org-use-property-inheritance property))
  3233. ((listp org-use-property-inheritance)
  3234. (member property org-use-property-inheritance))
  3235. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3236. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3237. "The default column format, if no other format has been defined.
  3238. This variable can be set on the per-file basis by inserting a line
  3239. #+COLUMNS: %25ITEM ....."
  3240. :group 'org-properties
  3241. :type 'string)
  3242. (defcustom org-columns-ellipses ".."
  3243. "The ellipses to be used when a field in column view is truncated.
  3244. When this is the empty string, as many characters as possible are shown,
  3245. but then there will be no visual indication that the field has been truncated.
  3246. When this is a string of length N, the last N characters of a truncated
  3247. field are replaced by this string. If the column is narrower than the
  3248. ellipses string, only part of the ellipses string will be shown."
  3249. :group 'org-properties
  3250. :type 'string)
  3251. (defcustom org-columns-modify-value-for-display-function nil
  3252. "Function that modifies values for display in column view.
  3253. For example, it can be used to cut out a certain part from a time stamp.
  3254. The function must take 2 arguments:
  3255. column-title The title of the column (*not* the property name)
  3256. value The value that should be modified.
  3257. The function should return the value that should be displayed,
  3258. or nil if the normal value should be used."
  3259. :group 'org-properties
  3260. :type '(choice (const nil) (function)))
  3261. (defconst org-global-properties-fixed
  3262. '(("VISIBILITY_ALL" . "folded children content all")
  3263. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3264. "List of property/value pairs that can be inherited by any entry.
  3265. These are fixed values, for the preset properties. The user variable
  3266. that can be used to add to this list is `org-global-properties'.
  3267. The entries in this list are cons cells where the car is a property
  3268. name and cdr is a string with the value. If the value represents
  3269. multiple items like an \"_ALL\" property, separate the items by
  3270. spaces.")
  3271. (defcustom org-global-properties nil
  3272. "List of property/value pairs that can be inherited by any entry.
  3273. This list will be combined with the constant `org-global-properties-fixed'.
  3274. The entries in this list are cons cells where the car is a property
  3275. name and cdr is a string with the value.
  3276. You can set buffer-local values for the same purpose in the variable
  3277. `org-file-properties' this by adding lines like
  3278. #+PROPERTY: NAME VALUE"
  3279. :group 'org-properties
  3280. :type '(repeat
  3281. (cons (string :tag "Property")
  3282. (string :tag "Value"))))
  3283. (defvar org-file-properties nil
  3284. "List of property/value pairs that can be inherited by any entry.
  3285. Valid for the current buffer.
  3286. This variable is populated from #+PROPERTY lines.")
  3287. (make-variable-buffer-local 'org-file-properties)
  3288. (defgroup org-agenda nil
  3289. "Options concerning agenda views in Org-mode."
  3290. :tag "Org Agenda"
  3291. :group 'org)
  3292. (defvar org-category nil
  3293. "Variable used by org files to set a category for agenda display.
  3294. Such files should use a file variable to set it, for example
  3295. # -*- mode: org; org-category: \"ELisp\"
  3296. or contain a special line
  3297. #+CATEGORY: ELisp
  3298. If the file does not specify a category, then file's base name
  3299. is used instead.")
  3300. (make-variable-buffer-local 'org-category)
  3301. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3302. (defcustom org-agenda-files nil
  3303. "The files to be used for agenda display.
  3304. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3305. \\[org-remove-file]. You can also use customize to edit the list.
  3306. If an entry is a directory, all files in that directory that are matched by
  3307. `org-agenda-file-regexp' will be part of the file list.
  3308. If the value of the variable is not a list but a single file name, then
  3309. the list of agenda files is actually stored and maintained in that file, one
  3310. agenda file per line. In this file paths can be given relative to
  3311. `org-directory'. Tilde expansion and environment variable substitution
  3312. are also made."
  3313. :group 'org-agenda
  3314. :type '(choice
  3315. (repeat :tag "List of files and directories" file)
  3316. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3317. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3318. "Regular expression to match files for `org-agenda-files'.
  3319. If any element in the list in that variable contains a directory instead
  3320. of a normal file, all files in that directory that are matched by this
  3321. regular expression will be included."
  3322. :group 'org-agenda
  3323. :type 'regexp)
  3324. (defcustom org-agenda-text-search-extra-files nil
  3325. "List of extra files to be searched by text search commands.
  3326. These files will be searched in addition to the agenda files by the
  3327. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3328. Note that these files will only be searched for text search commands,
  3329. not for the other agenda views like todo lists, tag searches or the weekly
  3330. agenda. This variable is intended to list notes and possibly archive files
  3331. that should also be searched by these two commands.
  3332. In fact, if the first element in the list is the symbol `agenda-archives',
  3333. then all archive files of all agenda files will be added to the search
  3334. scope."
  3335. :group 'org-agenda
  3336. :type '(set :greedy t
  3337. (const :tag "Agenda Archives" agenda-archives)
  3338. (repeat :inline t (file))))
  3339. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3340. 'org-agenda-text-search-extra-files)
  3341. (defcustom org-agenda-skip-unavailable-files nil
  3342. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3343. A nil value means to remove them, after a query, from the list."
  3344. :group 'org-agenda
  3345. :type 'boolean)
  3346. (defcustom org-calendar-to-agenda-key [?c]
  3347. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3348. The command `org-calendar-goto-agenda' will be bound to this key. The
  3349. default is the character `c' because then `c' can be used to switch back and
  3350. forth between agenda and calendar."
  3351. :group 'org-agenda
  3352. :type 'sexp)
  3353. (defcustom org-calendar-insert-diary-entry-key [?i]
  3354. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3355. This option is irrelevant until `org-agenda-diary-file' has been configured
  3356. to point to an Org-mode file. When that is the case, the command
  3357. `org-agenda-diary-entry' will be bound to the key given here, by default
  3358. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3359. if you want to continue doing this, you need to change this to a different
  3360. key."
  3361. :group 'org-agenda
  3362. :type 'sexp)
  3363. (defcustom org-agenda-diary-file 'diary-file
  3364. "File to which to add new entries with the `i' key in agenda and calendar.
  3365. When this is the symbol `diary-file', the functionality in the Emacs
  3366. calendar will be used to add entries to the `diary-file'. But when this
  3367. points to a file, `org-agenda-diary-entry' will be used instead."
  3368. :group 'org-agenda
  3369. :type '(choice
  3370. (const :tag "The standard Emacs diary file" diary-file)
  3371. (file :tag "Special Org file diary entries")))
  3372. (eval-after-load "calendar"
  3373. '(progn
  3374. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3375. 'org-calendar-goto-agenda)
  3376. (add-hook 'calendar-mode-hook
  3377. (lambda ()
  3378. (unless (eq org-agenda-diary-file 'diary-file)
  3379. (define-key calendar-mode-map
  3380. org-calendar-insert-diary-entry-key
  3381. 'org-agenda-diary-entry))))))
  3382. (defgroup org-latex nil
  3383. "Options for embedding LaTeX code into Org-mode."
  3384. :tag "Org LaTeX"
  3385. :group 'org)
  3386. (defcustom org-format-latex-options
  3387. '(:foreground default :background default :scale 1.0
  3388. :html-foreground "Black" :html-background "Transparent"
  3389. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3390. "Options for creating images from LaTeX fragments.
  3391. This is a property list with the following properties:
  3392. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3393. `default' means use the foreground of the default face.
  3394. `auto' means use the foreground from the text face.
  3395. :background the background color, or \"Transparent\".
  3396. `default' means use the background of the default face.
  3397. `auto' means use the background from the text face.
  3398. :scale a scaling factor for the size of the images, to get more pixels
  3399. :html-foreground, :html-background, :html-scale
  3400. the same numbers for HTML export.
  3401. :matchers a list indicating which matchers should be used to
  3402. find LaTeX fragments. Valid members of this list are:
  3403. \"begin\" find environments
  3404. \"$1\" find single characters surrounded by $.$
  3405. \"$\" find math expressions surrounded by $...$
  3406. \"$$\" find math expressions surrounded by $$....$$
  3407. \"\\(\" find math expressions surrounded by \\(...\\)
  3408. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3409. :group 'org-latex
  3410. :type 'plist)
  3411. (defcustom org-format-latex-signal-error t
  3412. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3413. When nil, just push out a message."
  3414. :group 'org-latex
  3415. :version "24.1"
  3416. :type 'boolean)
  3417. (defcustom org-latex-to-mathml-jar-file nil
  3418. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3419. Use this to specify additional executable file say a jar file.
  3420. When using MathToWeb as the converter, specify the full-path to
  3421. your mathtoweb.jar file."
  3422. :group 'org-latex
  3423. :version "24.1"
  3424. :type '(choice
  3425. (const :tag "None" nil)
  3426. (file :tag "JAR file" :must-match t)))
  3427. (defcustom org-latex-to-mathml-convert-command nil
  3428. "Command to convert LaTeX fragments to MathML.
  3429. Replace format-specifiers in the command as noted below and use
  3430. `shell-command' to convert LaTeX to MathML.
  3431. %j: Executable file in fully expanded form as specified by
  3432. `org-latex-to-mathml-jar-file'.
  3433. %I: Input LaTeX file in fully expanded form.
  3434. %i: The latex fragment to be converted.
  3435. %o: Output MathML file.
  3436. This command is used by `org-create-math-formula'.
  3437. When using MathToWeb as the converter, set this to
  3438. \"java -jar %j -unicode -force -df %o %I\".
  3439. When using LaTeXML set this to
  3440. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3441. :group 'org-latex
  3442. :version "24.1"
  3443. :type '(choice
  3444. (const :tag "None" nil)
  3445. (string :tag "\nShell command")))
  3446. (defcustom org-latex-create-formula-image-program 'dvipng
  3447. "Program to convert LaTeX fragments with.
  3448. dvipng Process the LaTeX fragments to dvi file, then convert
  3449. dvi files to png files using dvipng.
  3450. This will also include processing of non-math environments.
  3451. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3452. to convert pdf files to png files"
  3453. :group 'org-latex
  3454. :version "24.1"
  3455. :type '(choice
  3456. (const :tag "dvipng" dvipng)
  3457. (const :tag "imagemagick" imagemagick)))
  3458. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3459. "Path to store latex preview images.
  3460. A relative path here creates many directories relative to the
  3461. processed org files paths. An absolute path puts all preview
  3462. images at the same place."
  3463. :group 'org-latex
  3464. :version "24.3"
  3465. :type 'string)
  3466. (defun org-format-latex-mathml-available-p ()
  3467. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3468. (save-match-data
  3469. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3470. org-latex-to-mathml-convert-command)
  3471. (let ((executable (car (split-string
  3472. org-latex-to-mathml-convert-command))))
  3473. (when (executable-find executable)
  3474. (if (string-match
  3475. "%j" org-latex-to-mathml-convert-command)
  3476. (file-readable-p org-latex-to-mathml-jar-file)
  3477. t))))))
  3478. (defcustom org-format-latex-header "\\documentclass{article}
  3479. \\usepackage[usenames]{color}
  3480. \[PACKAGES]
  3481. \[DEFAULT-PACKAGES]
  3482. \\pagestyle{empty} % do not remove
  3483. % The settings below are copied from fullpage.sty
  3484. \\setlength{\\textwidth}{\\paperwidth}
  3485. \\addtolength{\\textwidth}{-3cm}
  3486. \\setlength{\\oddsidemargin}{1.5cm}
  3487. \\addtolength{\\oddsidemargin}{-2.54cm}
  3488. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3489. \\setlength{\\textheight}{\\paperheight}
  3490. \\addtolength{\\textheight}{-\\headheight}
  3491. \\addtolength{\\textheight}{-\\headsep}
  3492. \\addtolength{\\textheight}{-\\footskip}
  3493. \\addtolength{\\textheight}{-3cm}
  3494. \\setlength{\\topmargin}{1.5cm}
  3495. \\addtolength{\\topmargin}{-2.54cm}"
  3496. "The document header used for processing LaTeX fragments.
  3497. It is imperative that this header make sure that no page number
  3498. appears on the page. The package defined in the variables
  3499. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3500. will either replace the placeholder \"[PACKAGES]\" in this
  3501. header, or they will be appended."
  3502. :group 'org-latex
  3503. :type 'string)
  3504. (defun org-set-packages-alist (var val)
  3505. "Set the packages alist and make sure it has 3 elements per entry."
  3506. (set var (mapcar (lambda (x)
  3507. (if (and (consp x) (= (length x) 2))
  3508. (list (car x) (nth 1 x) t)
  3509. x))
  3510. val)))
  3511. (defun org-get-packages-alist (var)
  3512. "Get the packages alist and make sure it has 3 elements per entry."
  3513. (mapcar (lambda (x)
  3514. (if (and (consp x) (= (length x) 2))
  3515. (list (car x) (nth 1 x) t)
  3516. x))
  3517. (default-value var)))
  3518. (defcustom org-latex-default-packages-alist
  3519. '(("AUTO" "inputenc" t)
  3520. ("T1" "fontenc" t)
  3521. ("" "fixltx2e" nil)
  3522. ("" "graphicx" t)
  3523. ("" "longtable" nil)
  3524. ("" "float" nil)
  3525. ("" "wrapfig" nil)
  3526. ("" "rotating" nil)
  3527. ("normalem" "ulem" t)
  3528. ("" "amsmath" t)
  3529. ("" "textcomp" t)
  3530. ("" "marvosym" t)
  3531. ("" "wasysym" t)
  3532. ("" "amssymb" t)
  3533. ("" "capt-of" nil)
  3534. ("" "hyperref" nil)
  3535. "\\tolerance=1000")
  3536. "Alist of default packages to be inserted in the header.
  3537. Change this only if one of the packages here causes an
  3538. incompatibility with another package you are using.
  3539. The packages in this list are needed by one part or another of
  3540. Org mode to function properly:
  3541. - inputenc, fontenc: for basic font and character selection
  3542. - fixltx2e: Important patches of LaTeX itself
  3543. - graphicx: for including images
  3544. - longtable: For multipage tables
  3545. - float, wrapfig: for figure placement
  3546. - rotating: for sideways figures and tables
  3547. - ulem: for underline and strike-through
  3548. - amsmath: for subscript and superscript and math environments
  3549. - textcomp, marvosymb, wasysym, amssymb: for various symbols used
  3550. for interpreting the entities in `org-entities'. You can skip
  3551. some of these packages if you don't use any of their symbols.
  3552. - capt-of: for captions outside of floats
  3553. - hyperref: for cross references
  3554. Therefore you should not modify this variable unless you know
  3555. what you are doing. The one reason to change it anyway is that
  3556. you might be loading some other package that conflicts with one
  3557. of the default packages. Each element is either a cell or
  3558. a string.
  3559. A cell is of the format:
  3560. \( \"options\" \"package\" SNIPPET-FLAG).
  3561. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3562. when compiling LaTeX snippets into images for inclusion into
  3563. non-LaTeX output.
  3564. A string will be inserted as-is in the header of the document."
  3565. :group 'org-latex
  3566. :group 'org-export-latex
  3567. :set 'org-set-packages-alist
  3568. :get 'org-get-packages-alist
  3569. :version "24.1"
  3570. :type '(repeat
  3571. (choice
  3572. (list :tag "options/package pair"
  3573. (string :tag "options")
  3574. (string :tag "package")
  3575. (boolean :tag "Snippet"))
  3576. (string :tag "A line of LaTeX"))))
  3577. (defcustom org-latex-packages-alist nil
  3578. "Alist of packages to be inserted in every LaTeX header.
  3579. These will be inserted after `org-latex-default-packages-alist'.
  3580. Each element is either a cell or a string.
  3581. A cell is of the format:
  3582. \(\"options\" \"package\" SNIPPET-FLAG)
  3583. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3584. needed when turning LaTeX snippets into images for inclusion into
  3585. non-LaTeX output.
  3586. A string will be inserted as-is in the header of the document.
  3587. Make sure that you only list packages here which:
  3588. - you want in every file;
  3589. - do not conflict with the setup in `org-format-latex-header';
  3590. - do not conflict with the default packages in
  3591. `org-latex-default-packages-alist'."
  3592. :group 'org-latex
  3593. :group 'org-export-latex
  3594. :set 'org-set-packages-alist
  3595. :get 'org-get-packages-alist
  3596. :type '(repeat
  3597. (choice
  3598. (list :tag "options/package pair"
  3599. (string :tag "options")
  3600. (string :tag "package")
  3601. (boolean :tag "Snippet"))
  3602. (string :tag "A line of LaTeX"))))
  3603. (defgroup org-appearance nil
  3604. "Settings for Org-mode appearance."
  3605. :tag "Org Appearance"
  3606. :group 'org)
  3607. (defcustom org-level-color-stars-only nil
  3608. "Non-nil means fontify only the stars in each headline.
  3609. When nil, the entire headline is fontified.
  3610. Changing it requires restart of `font-lock-mode' to become effective
  3611. also in regions already fontified."
  3612. :group 'org-appearance
  3613. :type 'boolean)
  3614. (defcustom org-hide-leading-stars nil
  3615. "Non-nil means hide the first N-1 stars in a headline.
  3616. This works by using the face `org-hide' for these stars. This
  3617. face is white for a light background, and black for a dark
  3618. background. You may have to customize the face `org-hide' to
  3619. make this work.
  3620. Changing it requires restart of `font-lock-mode' to become effective
  3621. also in regions already fontified.
  3622. You may also set this on a per-file basis by adding one of the following
  3623. lines to the buffer:
  3624. #+STARTUP: hidestars
  3625. #+STARTUP: showstars"
  3626. :group 'org-appearance
  3627. :type 'boolean)
  3628. (defcustom org-hidden-keywords nil
  3629. "List of symbols corresponding to keywords to be hidden the org buffer.
  3630. For example, a value '(title) for this list will make the document's title
  3631. appear in the buffer without the initial #+TITLE: keyword."
  3632. :group 'org-appearance
  3633. :version "24.1"
  3634. :type '(set (const :tag "#+AUTHOR" author)
  3635. (const :tag "#+DATE" date)
  3636. (const :tag "#+EMAIL" email)
  3637. (const :tag "#+TITLE" title)))
  3638. (defcustom org-custom-properties nil
  3639. "List of properties (as strings) with a special meaning.
  3640. The default use of these custom properties is to let the user
  3641. hide them with `org-toggle-custom-properties-visibility'."
  3642. :group 'org-properties
  3643. :group 'org-appearance
  3644. :version "24.3"
  3645. :type '(repeat (string :tag "Property Name")))
  3646. (defcustom org-fontify-done-headline nil
  3647. "Non-nil means change the face of a headline if it is marked DONE.
  3648. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3649. When this is non-nil, the headline after the keyword is set to the
  3650. `org-headline-done' as an additional indication."
  3651. :group 'org-appearance
  3652. :type 'boolean)
  3653. (defcustom org-fontify-emphasized-text t
  3654. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3655. Changing this variable requires a restart of Emacs to take effect."
  3656. :group 'org-appearance
  3657. :type 'boolean)
  3658. (defcustom org-fontify-whole-heading-line nil
  3659. "Non-nil means fontify the whole line for headings.
  3660. This is useful when setting a background color for the
  3661. org-level-* faces."
  3662. :group 'org-appearance
  3663. :type 'boolean)
  3664. (defcustom org-highlight-latex-and-related nil
  3665. "Non-nil means highlight LaTeX related syntax in the buffer.
  3666. When non nil, the value should be a list containing any of the
  3667. following symbols:
  3668. `latex' Highlight LaTeX snippets and environments.
  3669. `script' Highlight subscript and superscript.
  3670. `entities' Highlight entities."
  3671. :group 'org-appearance
  3672. :version "24.4"
  3673. :package-version '(Org . "8.0")
  3674. :type '(choice
  3675. (const :tag "No highlighting" nil)
  3676. (set :greedy t :tag "Highlight"
  3677. (const :tag "LaTeX snippets and environments" latex)
  3678. (const :tag "Subscript and superscript" script)
  3679. (const :tag "Entities" entities))))
  3680. (defcustom org-hide-emphasis-markers nil
  3681. "Non-nil mean font-lock should hide the emphasis marker characters."
  3682. :group 'org-appearance
  3683. :type 'boolean)
  3684. (defcustom org-hide-macro-markers nil
  3685. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3686. :group 'org-appearance
  3687. :type 'boolean)
  3688. (defcustom org-pretty-entities nil
  3689. "Non-nil means show entities as UTF8 characters.
  3690. When nil, the \\name form remains in the buffer."
  3691. :group 'org-appearance
  3692. :version "24.1"
  3693. :type 'boolean)
  3694. (defcustom org-pretty-entities-include-sub-superscripts t
  3695. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3696. :group 'org-appearance
  3697. :version "24.1"
  3698. :type 'boolean)
  3699. (defvar org-emph-re nil
  3700. "Regular expression for matching emphasis.
  3701. After a match, the match groups contain these elements:
  3702. 0 The match of the full regular expression, including the characters
  3703. before and after the proper match
  3704. 1 The character before the proper match, or empty at beginning of line
  3705. 2 The proper match, including the leading and trailing markers
  3706. 3 The leading marker like * or /, indicating the type of highlighting
  3707. 4 The text between the emphasis markers, not including the markers
  3708. 5 The character after the match, empty at the end of a line")
  3709. (defvar org-verbatim-re nil
  3710. "Regular expression for matching verbatim text.")
  3711. (defvar org-emphasis-regexp-components) ; defined just below
  3712. (defvar org-emphasis-alist) ; defined just below
  3713. (defun org-set-emph-re (var val)
  3714. "Set variable and compute the emphasis regular expression."
  3715. (set var val)
  3716. (when (and (boundp 'org-emphasis-alist)
  3717. (boundp 'org-emphasis-regexp-components)
  3718. org-emphasis-alist org-emphasis-regexp-components)
  3719. (let* ((e org-emphasis-regexp-components)
  3720. (pre (car e))
  3721. (post (nth 1 e))
  3722. (border (nth 2 e))
  3723. (body (nth 3 e))
  3724. (nl (nth 4 e))
  3725. (body1 (concat body "*?"))
  3726. (markers (mapconcat 'car org-emphasis-alist ""))
  3727. (vmarkers (mapconcat
  3728. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3729. org-emphasis-alist "")))
  3730. ;; make sure special characters appear at the right position in the class
  3731. (if (string-match "\\^" markers)
  3732. (setq markers (concat (replace-match "" t t markers) "^")))
  3733. (if (string-match "-" markers)
  3734. (setq markers (concat (replace-match "" t t markers) "-")))
  3735. (if (string-match "\\^" vmarkers)
  3736. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3737. (if (string-match "-" vmarkers)
  3738. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3739. (if (> nl 0)
  3740. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3741. (int-to-string nl) "\\}")))
  3742. ;; Make the regexp
  3743. (setq org-emph-re
  3744. (concat "\\([" pre "]\\|^\\)"
  3745. "\\("
  3746. "\\([" markers "]\\)"
  3747. "\\("
  3748. "[^" border "]\\|"
  3749. "[^" border "]"
  3750. body1
  3751. "[^" border "]"
  3752. "\\)"
  3753. "\\3\\)"
  3754. "\\([" post "]\\|$\\)"))
  3755. (setq org-verbatim-re
  3756. (concat "\\([" pre "]\\|^\\)"
  3757. "\\("
  3758. "\\([" vmarkers "]\\)"
  3759. "\\("
  3760. "[^" border "]\\|"
  3761. "[^" border "]"
  3762. body1
  3763. "[^" border "]"
  3764. "\\)"
  3765. "\\3\\)"
  3766. "\\([" post "]\\|$\\)")))))
  3767. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3768. ;; set this option proved cumbersome. See this message/thread:
  3769. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3770. (defvar org-emphasis-regexp-components
  3771. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3772. "Components used to build the regular expression for emphasis.
  3773. This is a list with five entries. Terminology: In an emphasis string
  3774. like \" *strong word* \", we call the initial space PREMATCH, the final
  3775. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3776. and \"trong wor\" is the body. The different components in this variable
  3777. specify what is allowed/forbidden in each part:
  3778. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3779. post Chars allowed as postmatch. End of line will be allowed too.
  3780. border The chars *forbidden* as border characters.
  3781. body-regexp A regexp like \".\" to match a body character. Don't use
  3782. non-shy groups here, and don't allow newline here.
  3783. newline The maximum number of newlines allowed in an emphasis exp.
  3784. You need to reload Org or to restart Emacs after customizing this.")
  3785. (defcustom org-emphasis-alist
  3786. `(("*" bold)
  3787. ("/" italic)
  3788. ("_" underline)
  3789. ("=" org-verbatim verbatim)
  3790. ("~" org-code verbatim)
  3791. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3792. "Alist of characters and faces to emphasize text.
  3793. Text starting and ending with a special character will be emphasized,
  3794. for example *bold*, _underlined_ and /italic/. This variable sets the
  3795. marker characters and the face to be used by font-lock for highlighting
  3796. in Org-mode Emacs buffers.
  3797. You need to reload Org or to restart Emacs after customizing this."
  3798. :group 'org-appearance
  3799. :set 'org-set-emph-re
  3800. :version "24.4"
  3801. :package-version '(Org . "8.0")
  3802. :type '(repeat
  3803. (list
  3804. (string :tag "Marker character")
  3805. (choice
  3806. (face :tag "Font-lock-face")
  3807. (plist :tag "Face property list"))
  3808. (option (const verbatim)))))
  3809. (defvar org-protecting-blocks
  3810. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3811. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3812. This is needed for font-lock setup.")
  3813. ;;; Miscellaneous options
  3814. (defgroup org-completion nil
  3815. "Completion in Org-mode."
  3816. :tag "Org Completion"
  3817. :group 'org)
  3818. (defcustom org-completion-use-ido nil
  3819. "Non-nil means use ido completion wherever possible.
  3820. Note that `ido-mode' must be active for this variable to be relevant.
  3821. If you decide to turn this variable on, you might well want to turn off
  3822. `org-outline-path-complete-in-steps'.
  3823. See also `org-completion-use-iswitchb'."
  3824. :group 'org-completion
  3825. :type 'boolean)
  3826. (defcustom org-completion-use-iswitchb nil
  3827. "Non-nil means use iswitchb completion wherever possible.
  3828. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3829. If you decide to turn this variable on, you might well want to turn off
  3830. `org-outline-path-complete-in-steps'.
  3831. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3832. :group 'org-completion
  3833. :type 'boolean)
  3834. (defcustom org-completion-fallback-command 'hippie-expand
  3835. "The expansion command called by \\[pcomplete] in normal context.
  3836. Normal means, no org-mode-specific context."
  3837. :group 'org-completion
  3838. :type 'function)
  3839. ;;; Functions and variables from their packages
  3840. ;; Declared here to avoid compiler warnings
  3841. ;; XEmacs only
  3842. (defvar outline-mode-menu-heading)
  3843. (defvar outline-mode-menu-show)
  3844. (defvar outline-mode-menu-hide)
  3845. (defvar zmacs-regions) ; XEmacs regions
  3846. ;; Emacs only
  3847. (defvar mark-active)
  3848. ;; Various packages
  3849. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3850. (declare-function calendar-forward-day "cal-move" (arg))
  3851. (declare-function calendar-goto-date "cal-move" (date))
  3852. (declare-function calendar-goto-today "cal-move" ())
  3853. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3854. (defvar calc-embedded-close-formula)
  3855. (defvar calc-embedded-open-formula)
  3856. (declare-function cdlatex-tab "ext:cdlatex" ())
  3857. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3858. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3859. (defvar font-lock-unfontify-region-function)
  3860. (declare-function iswitchb-read-buffer "iswitchb"
  3861. (prompt &optional default require-match start matches-set))
  3862. (defvar iswitchb-temp-buflist)
  3863. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3864. (defvar org-agenda-tags-todo-honor-ignore-options)
  3865. (declare-function org-agenda-skip "org-agenda" ())
  3866. (declare-function
  3867. org-agenda-format-item "org-agenda"
  3868. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3869. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3870. (declare-function org-agenda-change-all-lines "org-agenda"
  3871. (newhead hdmarker &optional fixface just-this))
  3872. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3873. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3874. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3875. (beg end))
  3876. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3877. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3878. "org-agenda" (&optional end))
  3879. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3880. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3881. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3882. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3883. (declare-function org-indent-mode "org-indent" (&optional arg))
  3884. (declare-function parse-time-string "parse-time" (string))
  3885. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3886. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3887. (defvar remember-data-file)
  3888. (defvar texmathp-why)
  3889. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3890. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3891. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3892. ;;;###autoload
  3893. (defun turn-on-orgtbl ()
  3894. "Unconditionally turn on `orgtbl-mode'."
  3895. (require 'org-table)
  3896. (orgtbl-mode 1))
  3897. (defun org-at-table-p (&optional table-type)
  3898. "Return t if the cursor is inside an org-type table.
  3899. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3900. (if org-enable-table-editor
  3901. (save-excursion
  3902. (beginning-of-line 1)
  3903. (looking-at (if table-type org-table-any-line-regexp
  3904. org-table-line-regexp)))
  3905. nil))
  3906. (defsubst org-table-p () (org-at-table-p))
  3907. (defun org-at-table.el-p ()
  3908. "Return t if and only if we are at a table.el table."
  3909. (and (org-at-table-p 'any)
  3910. (save-excursion
  3911. (goto-char (org-table-begin 'any))
  3912. (looking-at org-table1-hline-regexp))))
  3913. (defun org-table-recognize-table.el ()
  3914. "If there is a table.el table nearby, recognize it and move into it."
  3915. (if org-table-tab-recognizes-table.el
  3916. (if (org-at-table.el-p)
  3917. (progn
  3918. (beginning-of-line 1)
  3919. (if (looking-at org-table-dataline-regexp)
  3920. nil
  3921. (if (looking-at org-table1-hline-regexp)
  3922. (progn
  3923. (beginning-of-line 2)
  3924. (if (looking-at org-table-any-border-regexp)
  3925. (beginning-of-line -1)))))
  3926. (if (re-search-forward "|" (org-table-end t) t)
  3927. (progn
  3928. (require 'table)
  3929. (if (table--at-cell-p (point))
  3930. t
  3931. (message "recognizing table.el table...")
  3932. (table-recognize-table)
  3933. (message "recognizing table.el table...done")))
  3934. (error "This should not happen"))
  3935. t)
  3936. nil)
  3937. nil))
  3938. (defun org-at-table-hline-p ()
  3939. "Return t if the cursor is inside a hline in a table."
  3940. (if org-enable-table-editor
  3941. (save-excursion
  3942. (beginning-of-line 1)
  3943. (looking-at org-table-hline-regexp))
  3944. nil))
  3945. (defun org-table-map-tables (function &optional quietly)
  3946. "Apply FUNCTION to the start of all tables in the buffer."
  3947. (save-excursion
  3948. (save-restriction
  3949. (widen)
  3950. (goto-char (point-min))
  3951. (while (re-search-forward org-table-any-line-regexp nil t)
  3952. (unless quietly
  3953. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3954. (beginning-of-line 1)
  3955. (when (and (looking-at org-table-line-regexp)
  3956. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3957. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3958. (save-excursion (funcall function))
  3959. (or (looking-at org-table-line-regexp)
  3960. (forward-char 1)))
  3961. (re-search-forward org-table-any-border-regexp nil 1))))
  3962. (unless quietly (message "Mapping tables: done")))
  3963. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3964. (declare-function org-clock-update-mode-line "org-clock" ())
  3965. (declare-function org-resolve-clocks "org-clock"
  3966. (&optional also-non-dangling-p prompt last-valid))
  3967. (defun org-at-TBLFM-p (&optional pos)
  3968. "Return t when point (or POS) is in #+TBLFM line."
  3969. (save-excursion
  3970. (let ((pos pos)))
  3971. (goto-char (or pos (point)))
  3972. (beginning-of-line 1)
  3973. (looking-at org-TBLFM-regexp)))
  3974. (defvar org-clock-start-time)
  3975. (defvar org-clock-marker (make-marker)
  3976. "Marker recording the last clock-in.")
  3977. (defvar org-clock-hd-marker (make-marker)
  3978. "Marker recording the last clock-in, but the headline position.")
  3979. (defvar org-clock-heading ""
  3980. "The heading of the current clock entry.")
  3981. (defun org-clock-is-active ()
  3982. "Return the buffer where the clock is currently running.
  3983. Return nil if no clock is running."
  3984. (marker-buffer org-clock-marker))
  3985. (defun org-check-running-clock ()
  3986. "Check if the current buffer contains the running clock.
  3987. If yes, offer to stop it and to save the buffer with the changes."
  3988. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3989. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3990. (buffer-name))))
  3991. (org-clock-out)
  3992. (when (y-or-n-p "Save changed buffer?")
  3993. (save-buffer))))
  3994. (defun org-clocktable-try-shift (dir n)
  3995. "Check if this line starts a clock table, if yes, shift the time block."
  3996. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3997. (org-clocktable-shift dir n)))
  3998. ;;;###autoload
  3999. (defun org-clock-persistence-insinuate ()
  4000. "Set up hooks for clock persistence."
  4001. (require 'org-clock)
  4002. (add-hook 'org-mode-hook 'org-clock-load)
  4003. (add-hook 'kill-emacs-hook 'org-clock-save))
  4004. (defgroup org-archive nil
  4005. "Options concerning archiving in Org-mode."
  4006. :tag "Org Archive"
  4007. :group 'org-structure)
  4008. (defcustom org-archive-location "%s_archive::"
  4009. "The location where subtrees should be archived.
  4010. The value of this variable is a string, consisting of two parts,
  4011. separated by a double-colon. The first part is a filename and
  4012. the second part is a headline.
  4013. When the filename is omitted, archiving happens in the same file.
  4014. %s in the filename will be replaced by the current file
  4015. name (without the directory part). Archiving to a different file
  4016. is useful to keep archived entries from contributing to the
  4017. Org-mode Agenda.
  4018. The archived entries will be filed as subtrees of the specified
  4019. headline. When the headline is omitted, the subtrees are simply
  4020. filed away at the end of the file, as top-level entries. Also in
  4021. the heading you can use %s to represent the file name, this can be
  4022. useful when using the same archive for a number of different files.
  4023. Here are a few examples:
  4024. \"%s_archive::\"
  4025. If the current file is Projects.org, archive in file
  4026. Projects.org_archive, as top-level trees. This is the default.
  4027. \"::* Archived Tasks\"
  4028. Archive in the current file, under the top-level headline
  4029. \"* Archived Tasks\".
  4030. \"~/org/archive.org::\"
  4031. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4032. \"~/org/archive.org::* From %s\"
  4033. Archive in file ~/org/archive.org (absolute path), under headlines
  4034. \"From FILENAME\" where file name is the current file name.
  4035. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4036. The \"datetree/\" string is special, signifying to archive
  4037. items to the datetree. Items are placed in either the CLOSED
  4038. date of the item, or the current date if there is no CLOSED date.
  4039. The heading will be a subentry to the current date. There doesn't
  4040. need to be a heading, but there always needs to be a slash after
  4041. datetree. For example, to store archived items directly in the
  4042. datetree, use \"~/org/datetree.org::datetree/\".
  4043. \"basement::** Finished Tasks\"
  4044. Archive in file ./basement (relative path), as level 3 trees
  4045. below the level 2 heading \"** Finished Tasks\".
  4046. You may set this option on a per-file basis by adding to the buffer a
  4047. line like
  4048. #+ARCHIVE: basement::** Finished Tasks
  4049. You may also define it locally for a subtree by setting an ARCHIVE property
  4050. in the entry. If such a property is found in an entry, or anywhere up
  4051. the hierarchy, it will be used."
  4052. :group 'org-archive
  4053. :type 'string)
  4054. (defcustom org-agenda-skip-archived-trees t
  4055. "Non-nil means the agenda will skip any items located in archived trees.
  4056. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4057. variable is no longer recommended, you should leave it at the value t.
  4058. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4059. :group 'org-archive
  4060. :group 'org-agenda-skip
  4061. :type 'boolean)
  4062. (defcustom org-columns-skip-archived-trees t
  4063. "Non-nil means ignore archived trees when creating column view."
  4064. :group 'org-archive
  4065. :group 'org-properties
  4066. :type 'boolean)
  4067. (defcustom org-cycle-open-archived-trees nil
  4068. "Non-nil means `org-cycle' will open archived trees.
  4069. An archived tree is a tree marked with the tag ARCHIVE.
  4070. When nil, archived trees will stay folded. You can still open them with
  4071. normal outline commands like `show-all', but not with the cycling commands."
  4072. :group 'org-archive
  4073. :group 'org-cycle
  4074. :type 'boolean)
  4075. (defcustom org-sparse-tree-open-archived-trees nil
  4076. "Non-nil means sparse tree construction shows matches in archived trees.
  4077. When nil, matches in these trees are highlighted, but the trees are kept in
  4078. collapsed state."
  4079. :group 'org-archive
  4080. :group 'org-sparse-trees
  4081. :type 'boolean)
  4082. (defcustom org-sparse-tree-default-date-type nil
  4083. "The default date type when building a sparse tree.
  4084. When this is nil, a date is a scheduled or a deadline timestamp.
  4085. Otherwise, these types are allowed:
  4086. all: all timestamps
  4087. active: only active timestamps (<...>)
  4088. inactive: only inactive timestamps ([...])
  4089. scheduled: only scheduled timestamps
  4090. deadline: only deadline timestamps"
  4091. :type '(choice (const :tag "Scheduled or deadline" nil)
  4092. (const :tag "All timestamps" all)
  4093. (const :tag "Only active timestamps" active)
  4094. (const :tag "Only inactive timestamps" inactive)
  4095. (const :tag "Only scheduled timestamps" scheduled)
  4096. (const :tag "Only deadline timestamps" deadline)
  4097. (const :tag "Only closed timestamps" closed))
  4098. :version "25.1"
  4099. :package-version '(Org . "8.3")
  4100. :group 'org-sparse-trees)
  4101. (defun org-cycle-hide-archived-subtrees (state)
  4102. "Re-hide all archived subtrees after a visibility state change."
  4103. (when (and (not org-cycle-open-archived-trees)
  4104. (not (memq state '(overview folded))))
  4105. (save-excursion
  4106. (let* ((globalp (memq state '(contents all)))
  4107. (beg (if globalp (point-min) (point)))
  4108. (end (if globalp (point-max) (org-end-of-subtree t))))
  4109. (org-hide-archived-subtrees beg end)
  4110. (goto-char beg)
  4111. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4112. (message "%s" (substitute-command-keys
  4113. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4114. (defun org-force-cycle-archived ()
  4115. "Cycle subtree even if it is archived."
  4116. (interactive)
  4117. (setq this-command 'org-cycle)
  4118. (let ((org-cycle-open-archived-trees t))
  4119. (call-interactively 'org-cycle)))
  4120. (defun org-hide-archived-subtrees (beg end)
  4121. "Re-hide all archived subtrees after a visibility state change."
  4122. (org-with-wide-buffer
  4123. (let ((case-fold-search nil)
  4124. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4125. (goto-char beg)
  4126. (while (and (< (point) end) (re-search-forward re end t))
  4127. (when (member org-archive-tag (org-get-tags))
  4128. (org-flag-subtree t)
  4129. (org-end-of-subtree t))))))
  4130. (declare-function outline-end-of-heading "outline" ())
  4131. (declare-function outline-flag-region "outline" (from to flag))
  4132. (defun org-flag-subtree (flag)
  4133. (save-excursion
  4134. (org-back-to-heading t)
  4135. (outline-end-of-heading)
  4136. (outline-flag-region (point)
  4137. (progn (org-end-of-subtree t) (point))
  4138. flag)))
  4139. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4140. ;; Declare Column View Code
  4141. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4142. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4143. (declare-function org-columns-compute "org-colview" (property))
  4144. ;; Declare ID code
  4145. (declare-function org-id-store-link "org-id")
  4146. (declare-function org-id-locations-load "org-id")
  4147. (declare-function org-id-locations-save "org-id")
  4148. (defvar org-id-track-globally)
  4149. ;;; Variables for pre-computed regular expressions, all buffer local
  4150. (defvar org-todo-regexp nil
  4151. "Matches any of the TODO state keywords.")
  4152. (make-variable-buffer-local 'org-todo-regexp)
  4153. (defvar org-not-done-regexp nil
  4154. "Matches any of the TODO state keywords except the last one.")
  4155. (make-variable-buffer-local 'org-not-done-regexp)
  4156. (defvar org-not-done-heading-regexp nil
  4157. "Matches a TODO headline that is not done.")
  4158. (make-variable-buffer-local 'org-not-done-regexp)
  4159. (defvar org-todo-line-regexp nil
  4160. "Matches a headline and puts TODO state into group 2 if present.")
  4161. (make-variable-buffer-local 'org-todo-line-regexp)
  4162. (defvar org-complex-heading-regexp nil
  4163. "Matches a headline and puts everything into groups:
  4164. group 1: the stars
  4165. group 2: The todo keyword, maybe
  4166. group 3: Priority cookie
  4167. group 4: True headline
  4168. group 5: Tags")
  4169. (make-variable-buffer-local 'org-complex-heading-regexp)
  4170. (defvar org-complex-heading-regexp-format nil
  4171. "Printf format to make regexp to match an exact headline.
  4172. This regexp will match the headline of any node which has the
  4173. exact headline text that is put into the format, but may have any
  4174. TODO state, priority and tags.")
  4175. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4176. (defvar org-todo-line-tags-regexp nil
  4177. "Matches a headline and puts TODO state into group 2 if present.
  4178. Also put tags into group 4 if tags are present.")
  4179. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4180. (defconst org-plain-time-of-day-regexp
  4181. (concat
  4182. "\\(\\<[012]?[0-9]"
  4183. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4184. "\\(--?"
  4185. "\\(\\<[012]?[0-9]"
  4186. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4187. "\\)?")
  4188. "Regular expression to match a plain time or time range.
  4189. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4190. groups carry important information:
  4191. 0 the full match
  4192. 1 the first time, range or not
  4193. 8 the second time, if it is a range.")
  4194. (defconst org-plain-time-extension-regexp
  4195. (concat
  4196. "\\(\\<[012]?[0-9]"
  4197. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4198. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4199. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4200. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4201. groups carry important information:
  4202. 0 the full match
  4203. 7 hours of duration
  4204. 9 minutes of duration")
  4205. (defconst org-stamp-time-of-day-regexp
  4206. (concat
  4207. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4208. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4209. "\\(--?"
  4210. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4211. "Regular expression to match a timestamp time or time range.
  4212. After a match, the following groups carry important information:
  4213. 0 the full match
  4214. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4215. 2 the first time, range or not
  4216. 4 the second time, if it is a range.")
  4217. (defconst org-startup-options
  4218. '(("fold" org-startup-folded t)
  4219. ("overview" org-startup-folded t)
  4220. ("nofold" org-startup-folded nil)
  4221. ("showall" org-startup-folded nil)
  4222. ("showeverything" org-startup-folded showeverything)
  4223. ("content" org-startup-folded content)
  4224. ("indent" org-startup-indented t)
  4225. ("noindent" org-startup-indented nil)
  4226. ("hidestars" org-hide-leading-stars t)
  4227. ("showstars" org-hide-leading-stars nil)
  4228. ("odd" org-odd-levels-only t)
  4229. ("oddeven" org-odd-levels-only nil)
  4230. ("align" org-startup-align-all-tables t)
  4231. ("noalign" org-startup-align-all-tables nil)
  4232. ("inlineimages" org-startup-with-inline-images t)
  4233. ("noinlineimages" org-startup-with-inline-images nil)
  4234. ("latexpreview" org-startup-with-latex-preview t)
  4235. ("nolatexpreview" org-startup-with-latex-preview nil)
  4236. ("customtime" org-display-custom-times t)
  4237. ("logdone" org-log-done time)
  4238. ("lognotedone" org-log-done note)
  4239. ("nologdone" org-log-done nil)
  4240. ("lognoteclock-out" org-log-note-clock-out t)
  4241. ("nolognoteclock-out" org-log-note-clock-out nil)
  4242. ("logrepeat" org-log-repeat state)
  4243. ("lognoterepeat" org-log-repeat note)
  4244. ("logdrawer" org-log-into-drawer t)
  4245. ("nologdrawer" org-log-into-drawer nil)
  4246. ("logstatesreversed" org-log-states-order-reversed t)
  4247. ("nologstatesreversed" org-log-states-order-reversed nil)
  4248. ("nologrepeat" org-log-repeat nil)
  4249. ("logreschedule" org-log-reschedule time)
  4250. ("lognotereschedule" org-log-reschedule note)
  4251. ("nologreschedule" org-log-reschedule nil)
  4252. ("logredeadline" org-log-redeadline time)
  4253. ("lognoteredeadline" org-log-redeadline note)
  4254. ("nologredeadline" org-log-redeadline nil)
  4255. ("logrefile" org-log-refile time)
  4256. ("lognoterefile" org-log-refile note)
  4257. ("nologrefile" org-log-refile nil)
  4258. ("fninline" org-footnote-define-inline t)
  4259. ("nofninline" org-footnote-define-inline nil)
  4260. ("fnlocal" org-footnote-section nil)
  4261. ("fnauto" org-footnote-auto-label t)
  4262. ("fnprompt" org-footnote-auto-label nil)
  4263. ("fnconfirm" org-footnote-auto-label confirm)
  4264. ("fnplain" org-footnote-auto-label plain)
  4265. ("fnadjust" org-footnote-auto-adjust t)
  4266. ("nofnadjust" org-footnote-auto-adjust nil)
  4267. ("constcgs" constants-unit-system cgs)
  4268. ("constSI" constants-unit-system SI)
  4269. ("noptag" org-tag-persistent-alist nil)
  4270. ("hideblocks" org-hide-block-startup t)
  4271. ("nohideblocks" org-hide-block-startup nil)
  4272. ("beamer" org-startup-with-beamer-mode t)
  4273. ("entitiespretty" org-pretty-entities t)
  4274. ("entitiesplain" org-pretty-entities nil))
  4275. "Variable associated with STARTUP options for org-mode.
  4276. Each element is a list of three items: the startup options (as written
  4277. in the #+STARTUP line), the corresponding variable, and the value to set
  4278. this variable to if the option is found. An optional forth element PUSH
  4279. means to push this value onto the list in the variable.")
  4280. (defcustom org-group-tags t
  4281. "When non-nil (the default), use group tags.
  4282. This can be turned on/off through `org-toggle-tags-groups'."
  4283. :group 'org-tags
  4284. :group 'org-startup
  4285. :type 'boolean)
  4286. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4287. (defun org-toggle-tags-groups ()
  4288. "Toggle support for group tags.
  4289. Support for group tags is controlled by the option
  4290. `org-group-tags', which is non-nil by default."
  4291. (interactive)
  4292. (setq org-group-tags (not org-group-tags))
  4293. (cond ((and (derived-mode-p 'org-agenda-mode)
  4294. org-group-tags)
  4295. (org-agenda-redo))
  4296. ((derived-mode-p 'org-mode)
  4297. (let ((org-inhibit-startup t)) (org-mode))))
  4298. (message "Groups tags support has been turned %s"
  4299. (if org-group-tags "on" "off")))
  4300. (defun org-set-regexps-and-options (&optional tags-only)
  4301. "Precompute regular expressions used in the current buffer.
  4302. When optional argument TAGS-ONLY is non-nil, only compute tags
  4303. related expressions."
  4304. (when (derived-mode-p 'org-mode)
  4305. (let ((alist (org--setup-collect-keywords
  4306. (org-make-options-regexp
  4307. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4308. (and (not tags-only)
  4309. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4310. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4311. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4312. (org--setup-process-tags
  4313. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4314. (unless tags-only
  4315. ;; File properties.
  4316. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4317. ;; Archive location.
  4318. (let ((archive (cdr (assq 'archive alist))))
  4319. (when archive (org-set-local 'org-archive-location archive)))
  4320. ;; Category.
  4321. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4322. (when cat
  4323. (org-set-local 'org-category (intern cat))
  4324. (org-set-local 'org-file-properties
  4325. (org--update-property-plist
  4326. "CATEGORY" cat org-file-properties))))
  4327. ;; Columns.
  4328. (let ((column (cdr (assq 'columns alist))))
  4329. (when column (org-set-local 'org-columns-default-format column)))
  4330. ;; Constants.
  4331. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4332. ;; Link abbreviations.
  4333. (let ((links (cdr (assq 'link alist))))
  4334. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4335. ;; Priorities.
  4336. (let ((priorities (cdr (assq 'priorities alist))))
  4337. (when priorities
  4338. (org-set-local 'org-highest-priority (nth 0 priorities))
  4339. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4340. (org-set-local 'org-default-priority (nth 2 priorities))))
  4341. ;; Scripts.
  4342. (let ((scripts (assq 'scripts alist)))
  4343. (when scripts
  4344. (org-set-local 'org-use-sub-superscripts (cdr scripts))))
  4345. ;; Startup options.
  4346. (let ((startup (cdr (assq 'startup alist))))
  4347. (dolist (option startup)
  4348. (let ((entry (assoc-string option org-startup-options t)))
  4349. (when entry
  4350. (let ((var (nth 1 entry))
  4351. (val (nth 2 entry)))
  4352. (if (not (nth 3 entry)) (org-set-local var val)
  4353. (unless (listp (symbol-value var))
  4354. (org-set-local var nil))
  4355. (add-to-list var val)))))))
  4356. ;; TODO keywords.
  4357. (org-set-local 'org-todo-kwd-alist nil)
  4358. (org-set-local 'org-todo-key-alist nil)
  4359. (org-set-local 'org-todo-key-trigger nil)
  4360. (org-set-local 'org-todo-keywords-1 nil)
  4361. (org-set-local 'org-done-keywords nil)
  4362. (org-set-local 'org-todo-heads nil)
  4363. (org-set-local 'org-todo-sets nil)
  4364. (org-set-local 'org-todo-log-states nil)
  4365. (let ((todo-sequences
  4366. (or (nreverse (cdr (assq 'todo alist)))
  4367. (let ((d (default-value 'org-todo-keywords)))
  4368. (if (not (stringp (car d))) d
  4369. ;; XXX: Backward compatibility code.
  4370. (list (cons org-todo-interpretation d)))))))
  4371. (dolist (sequence todo-sequences)
  4372. (let* ((sequence (or (run-hook-with-args-until-success
  4373. 'org-todo-setup-filter-hook sequence)
  4374. sequence))
  4375. (sequence-type (car sequence))
  4376. (keywords (cdr sequence))
  4377. (sep (member "|" keywords))
  4378. names alist)
  4379. (dolist (k (remove "|" keywords))
  4380. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4381. k)
  4382. (error "Invalid TODO keyword %s" k))
  4383. (let ((name (match-string 1 k))
  4384. (key (match-string 2 k))
  4385. (log (org-extract-log-state-settings k)))
  4386. (push name names)
  4387. (push (cons name (and key (string-to-char key))) alist)
  4388. (when log (push log org-todo-log-states))))
  4389. (let* ((names (nreverse names))
  4390. (done (if sep (org-remove-keyword-keys (cdr sep))
  4391. (last names)))
  4392. (head (car names))
  4393. (tail (list sequence-type head (car done) (org-last done))))
  4394. (add-to-list 'org-todo-heads head 'append)
  4395. (push names org-todo-sets)
  4396. (setq org-done-keywords (append org-done-keywords done nil))
  4397. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4398. (setq org-todo-key-alist
  4399. (append org-todo-key-alist
  4400. (and alist
  4401. (append '((:startgroup))
  4402. (nreverse alist)
  4403. '((:endgroup))))))
  4404. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4405. (setq org-todo-sets (nreverse org-todo-sets)
  4406. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4407. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4408. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4409. ;; Compute the regular expressions and other local variables.
  4410. ;; Using `org-outline-regexp-bol' would complicate them much,
  4411. ;; because of the fixed white space at the end of that string.
  4412. (if (not org-done-keywords)
  4413. (setq org-done-keywords
  4414. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4415. (setq org-not-done-keywords
  4416. (org-delete-all org-done-keywords
  4417. (copy-sequence org-todo-keywords-1))
  4418. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4419. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4420. org-not-done-heading-regexp
  4421. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4422. org-todo-line-regexp
  4423. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4424. org-complex-heading-regexp
  4425. (concat "^\\(\\*+\\)"
  4426. "\\(?: +" org-todo-regexp "\\)?"
  4427. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4428. "\\(?: +\\(.*?\\)\\)??"
  4429. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4430. "[ \t]*$")
  4431. org-complex-heading-regexp-format
  4432. (concat "^\\(\\*+\\)"
  4433. "\\(?: +" org-todo-regexp "\\)?"
  4434. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4435. "\\(?: +"
  4436. ;; Stats cookies can be stuck to body.
  4437. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4438. "\\(%s\\)"
  4439. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4440. "\\)"
  4441. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4442. "[ \t]*$")
  4443. org-todo-line-tags-regexp
  4444. (concat "^\\(\\*+\\)"
  4445. "\\(?: +" org-todo-regexp "\\)?"
  4446. "\\(?: +\\(.*?\\)\\)??"
  4447. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4448. "[ \t]*$"))
  4449. (org-compute-latex-and-related-regexp)))))
  4450. (defun org--setup-collect-keywords (regexp &optional files alist)
  4451. "Return setup keywords values as an alist.
  4452. REGEXP matches a subset of setup keywords. FILES is a list of
  4453. file names already visited. It is used to avoid circular setup
  4454. files. ALIST, when non-nil, is the alist computed so far.
  4455. Return value contains the following keys: `archive', `category',
  4456. `columns', `constants', `filetags', `link', `priorities',
  4457. `property', `scripts', `startup', `tags' and `todo'."
  4458. (org-with-wide-buffer
  4459. (goto-char (point-min))
  4460. (let ((case-fold-search t))
  4461. (while (re-search-forward regexp nil t)
  4462. (let ((element (org-element-at-point)))
  4463. (when (eq (org-element-type element) 'keyword)
  4464. (let ((key (org-element-property :key element))
  4465. (value (org-element-property :value element)))
  4466. (cond
  4467. ((equal key "ARCHIVE")
  4468. (when (org-string-nw-p value)
  4469. (push (cons 'archive value) alist)))
  4470. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4471. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4472. ((equal key "CONSTANTS")
  4473. (let* ((constants (assq 'constants alist))
  4474. (store (cdr constants)))
  4475. (dolist (pair (org-split-string value))
  4476. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4477. pair)
  4478. (let* ((name (match-string 1 pair))
  4479. (value (match-string 2 pair))
  4480. (old (assoc name store)))
  4481. (if old (setcdr old value)
  4482. (push (cons name value) store)))))
  4483. (if constants (setcdr constants store)
  4484. (push (cons 'constants store) alist))))
  4485. ((equal key "FILETAGS")
  4486. (when (org-string-nw-p value)
  4487. (let ((old (assq 'filetags alist))
  4488. (new (apply #'nconc
  4489. (mapcar (lambda (x) (org-split-string x ":"))
  4490. (org-split-string value)))))
  4491. (if old (setcdr old (append new (cdr old)))
  4492. (push (cons 'filetags new) alist)))))
  4493. ((equal key "LINK")
  4494. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4495. (let ((links (assq 'link alist))
  4496. (pair (cons (org-match-string-no-properties 1 value)
  4497. (org-match-string-no-properties 2 value))))
  4498. (if links (push pair (cdr links))
  4499. (push (list 'link pair) alist)))))
  4500. ((equal key "OPTIONS")
  4501. (when (and (org-string-nw-p value)
  4502. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4503. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4504. ((equal key "PRIORITIES")
  4505. (push (cons 'priorities
  4506. (let ((prio (org-split-string value)))
  4507. (if (< (length prio) 3) '(?A ?C ?B)
  4508. (mapcar #'string-to-char prio))))
  4509. alist))
  4510. ((equal key "PROPERTY")
  4511. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4512. (let* ((property (assq 'property alist))
  4513. (value (org--update-property-plist
  4514. (org-match-string-no-properties 1 value)
  4515. (org-match-string-no-properties 2 value)
  4516. (cdr property))))
  4517. (if property (setcdr property value)
  4518. (push (cons 'property value) alist)))))
  4519. ((equal key "STARTUP")
  4520. (let ((startup (assq 'startup alist)))
  4521. (if startup
  4522. (setcdr startup
  4523. (append (cdr startup) (org-split-string value)))
  4524. (push (cons 'startup (org-split-string value)) alist))))
  4525. ((equal key "TAGS")
  4526. (let ((tag-cell (assq 'tags alist)))
  4527. (if tag-cell
  4528. (setcdr tag-cell
  4529. (append (cdr tag-cell)
  4530. '("\\n")
  4531. (org-split-string value)))
  4532. (push (cons 'tags (org-split-string value)) alist))))
  4533. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4534. (let ((todo (assq 'todo alist))
  4535. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4536. (org-split-string value))))
  4537. (if todo (push value (cdr todo))
  4538. (push (list 'todo value) alist))))
  4539. ((equal key "SETUPFILE")
  4540. (unless buffer-read-only ; Do not check in Gnus messages.
  4541. (let ((f (and (org-string-nw-p value)
  4542. (expand-file-name
  4543. (org-remove-double-quotes value)))))
  4544. (when (and f (file-readable-p f) (not (member f files)))
  4545. (with-temp-buffer
  4546. (insert-file-contents f)
  4547. (setq alist
  4548. ;; Fake Org mode to benefit from cache
  4549. ;; without recurring needlessly.
  4550. (let ((major-mode 'org-mode))
  4551. (org--setup-collect-keywords
  4552. regexp (cons f files) alist)))))))))))))))
  4553. alist)
  4554. (defun org--setup-process-tags (tags filetags)
  4555. "Precompute variables used for tags.
  4556. TAGS is a list of tags and tag group symbols, as strings.
  4557. FILETAGS is a list of tags, as strings."
  4558. ;; Process the file tags.
  4559. (org-set-local 'org-file-tags
  4560. (mapcar #'org-add-prop-inherited filetags))
  4561. ;; Provide default tags if no local tags are found.
  4562. (when (and (not tags) org-tag-alist)
  4563. (setq tags
  4564. (mapcar (lambda (tag)
  4565. (case (car tag)
  4566. (:startgroup "{")
  4567. (:endgroup "}")
  4568. (:startgrouptag "[")
  4569. (:endgrouptag "]")
  4570. (:grouptags ":")
  4571. (:newline "\\n")
  4572. (otherwise (concat (car tag)
  4573. (and (characterp (cdr tag))
  4574. (format "(%c)" (cdr tag)))))))
  4575. org-tag-alist)))
  4576. ;; Process the tags.
  4577. (org-set-local 'org-tag-groups-alist nil)
  4578. (org-set-local 'org-tag-alist nil)
  4579. (let (group-flag)
  4580. (while tags
  4581. (let ((e (car tags)))
  4582. (setq tags (cdr tags))
  4583. (cond
  4584. ((equal e "{")
  4585. (push '(:startgroup) org-tag-alist)
  4586. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4587. ((equal e "}")
  4588. (push '(:endgroup) org-tag-alist)
  4589. (setq group-flag nil))
  4590. ((equal e "[")
  4591. (push '(:startgrouptag) org-tag-alist)
  4592. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4593. ((equal e "]")
  4594. (push '(:endgrouptag) org-tag-alist)
  4595. (setq group-flag nil))
  4596. ((equal e ":")
  4597. (push '(:grouptags) org-tag-alist)
  4598. (setq group-flag 'append))
  4599. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4600. ((string-match
  4601. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4602. "\\|{.+?}\\)" ; regular expression
  4603. "\\(?:(\\(.\\))\\)?\\'")) e)
  4604. (let ((tag (match-string 1 e))
  4605. (key (and (match-beginning 2)
  4606. (string-to-char (match-string 2 e)))))
  4607. (cond ((eq group-flag 'append)
  4608. (setcar org-tag-groups-alist
  4609. (append (car org-tag-groups-alist) (list tag))))
  4610. (group-flag (push (list tag) org-tag-groups-alist)))
  4611. ;; Push all tags in groups, no matter if they already exist.
  4612. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4613. (push (cons tag key) org-tag-alist))))))))
  4614. (setq org-tag-alist (nreverse org-tag-alist)))
  4615. (defun org-file-contents (file &optional noerror)
  4616. "Return the contents of FILE, as a string."
  4617. (if (and file (file-readable-p file))
  4618. (with-temp-buffer
  4619. (insert-file-contents file)
  4620. (buffer-string))
  4621. (funcall (if noerror 'message 'error)
  4622. "Cannot read file \"%s\"%s"
  4623. file
  4624. (let ((from (buffer-file-name (buffer-base-buffer))))
  4625. (if from (concat " (referenced in file \"" from "\")") "")))))
  4626. (defun org-extract-log-state-settings (x)
  4627. "Extract the log state setting from a TODO keyword string.
  4628. This will extract info from a string like \"WAIT(w@/!)\"."
  4629. (let (kw key log1 log2)
  4630. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4631. (setq kw (match-string 1 x)
  4632. key (and (match-end 2) (match-string 2 x))
  4633. log1 (and (match-end 3) (match-string 3 x))
  4634. log2 (and (match-end 4) (match-string 4 x)))
  4635. (and (or log1 log2)
  4636. (list kw
  4637. (and log1 (if (equal log1 "!") 'time 'note))
  4638. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4639. (defun org-remove-keyword-keys (list)
  4640. "Remove a pair of parenthesis at the end of each string in LIST."
  4641. (mapcar (lambda (x)
  4642. (if (string-match "(.*)$" x)
  4643. (substring x 0 (match-beginning 0))
  4644. x))
  4645. list))
  4646. (defun org-assign-fast-keys (alist)
  4647. "Assign fast keys to a keyword-key alist.
  4648. Respect keys that are already there."
  4649. (let (new e (alt ?0))
  4650. (while (setq e (pop alist))
  4651. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4652. (cdr e)) ;; Key already assigned.
  4653. (push e new)
  4654. (let ((clist (string-to-list (downcase (car e))))
  4655. (used (append new alist)))
  4656. (when (= (car clist) ?@)
  4657. (pop clist))
  4658. (while (and clist (rassoc (car clist) used))
  4659. (pop clist))
  4660. (unless clist
  4661. (while (rassoc alt used)
  4662. (incf alt)))
  4663. (push (cons (car e) (or (car clist) alt)) new))))
  4664. (nreverse new)))
  4665. ;;; Some variables used in various places
  4666. (defvar org-window-configuration nil
  4667. "Used in various places to store a window configuration.")
  4668. (defvar org-selected-window nil
  4669. "Used in various places to store a window configuration.")
  4670. (defvar org-finish-function nil
  4671. "Function to be called when `C-c C-c' is used.
  4672. This is for getting out of special buffers like capture.")
  4673. ;; FIXME: Occasionally check by commenting these, to make sure
  4674. ;; no other functions uses these, forgetting to let-bind them.
  4675. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4676. (defvar org-last-state)
  4677. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4678. ;; Defined somewhere in this file, but used before definition.
  4679. (defvar org-entities) ;; defined in org-entities.el
  4680. (defvar org-struct-menu)
  4681. (defvar org-org-menu)
  4682. (defvar org-tbl-menu)
  4683. ;;;; Define the Org-mode
  4684. ;; We use a before-change function to check if a table might need
  4685. ;; an update.
  4686. (defvar org-table-may-need-update t
  4687. "Indicates that a table might need an update.
  4688. This variable is set by `org-before-change-function'.
  4689. `org-table-align' sets it back to nil.")
  4690. (defun org-before-change-function (beg end)
  4691. "Every change indicates that a table might need an update."
  4692. (setq org-table-may-need-update t))
  4693. (defvar org-mode-map)
  4694. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4695. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4696. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4697. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4698. (defvar org-table-buffer-is-an nil)
  4699. (defvar bidi-paragraph-direction)
  4700. (defvar buffer-face-mode-face)
  4701. (require 'outline)
  4702. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4703. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
  4704. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4705. ;; Other stuff we need.
  4706. (require 'time-date)
  4707. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4708. (require 'easymenu)
  4709. (require 'overlay)
  4710. ;; (require 'org-macs) moved higher up in the file before it is first used
  4711. (require 'org-entities)
  4712. ;; (require 'org-compat) moved higher up in the file before it is first used
  4713. (require 'org-faces)
  4714. (require 'org-list)
  4715. (require 'org-pcomplete)
  4716. (require 'org-src)
  4717. (require 'org-footnote)
  4718. (require 'org-macro)
  4719. ;; babel
  4720. (require 'ob)
  4721. ;;;###autoload
  4722. (define-derived-mode org-mode outline-mode "Org"
  4723. "Outline-based notes management and organizer, alias
  4724. \"Carsten's outline-mode for keeping track of everything.\"
  4725. Org-mode develops organizational tasks around a NOTES file which
  4726. contains information about projects as plain text. Org-mode is
  4727. implemented on top of outline-mode, which is ideal to keep the content
  4728. of large files well structured. It supports ToDo items, deadlines and
  4729. time stamps, which magically appear in the diary listing of the Emacs
  4730. calendar. Tables are easily created with a built-in table editor.
  4731. Plain text URL-like links connect to websites, emails (VM), Usenet
  4732. messages (Gnus), BBDB entries, and any files related to the project.
  4733. For printing and sharing of notes, an Org-mode file (or a part of it)
  4734. can be exported as a structured ASCII or HTML file.
  4735. The following commands are available:
  4736. \\{org-mode-map}"
  4737. ;; Get rid of Outline menus, they are not needed
  4738. ;; Need to do this here because define-derived-mode sets up
  4739. ;; the keymap so late. Still, it is a waste to call this each time
  4740. ;; we switch another buffer into org-mode.
  4741. (if (featurep 'xemacs)
  4742. (when (boundp 'outline-mode-menu-heading)
  4743. ;; Assume this is Greg's port, it uses easymenu
  4744. (easy-menu-remove outline-mode-menu-heading)
  4745. (easy-menu-remove outline-mode-menu-show)
  4746. (easy-menu-remove outline-mode-menu-hide))
  4747. (define-key org-mode-map [menu-bar headings] 'undefined)
  4748. (define-key org-mode-map [menu-bar hide] 'undefined)
  4749. (define-key org-mode-map [menu-bar show] 'undefined))
  4750. (org-load-modules-maybe)
  4751. (easy-menu-add org-org-menu)
  4752. (easy-menu-add org-tbl-menu)
  4753. (org-install-agenda-files-menu)
  4754. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4755. (add-to-invisibility-spec '(org-cwidth))
  4756. (add-to-invisibility-spec '(org-hide-block . t))
  4757. (when (featurep 'xemacs)
  4758. (org-set-local 'line-move-ignore-invisible t))
  4759. (org-set-local 'outline-regexp org-outline-regexp)
  4760. (org-set-local 'outline-level 'org-outline-level)
  4761. (setq bidi-paragraph-direction 'left-to-right)
  4762. (when (and org-ellipsis
  4763. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4764. (fboundp 'make-glyph-code))
  4765. (unless org-display-table
  4766. (setq org-display-table (make-display-table)))
  4767. (set-display-table-slot
  4768. org-display-table 4
  4769. (vconcat (mapcar
  4770. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4771. org-ellipsis)))
  4772. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4773. (setq buffer-display-table org-display-table))
  4774. (org-set-regexps-and-options)
  4775. (org-set-font-lock-defaults)
  4776. (when (and org-tag-faces (not org-tags-special-faces-re))
  4777. ;; tag faces set outside customize.... force initialization.
  4778. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4779. ;; Calc embedded
  4780. (org-set-local 'calc-embedded-open-mode "# ")
  4781. ;; Modify a few syntax entries
  4782. (modify-syntax-entry ?@ "w")
  4783. (modify-syntax-entry ?\" "\"")
  4784. (modify-syntax-entry ?\\ "_")
  4785. (modify-syntax-entry ?~ "_")
  4786. (if org-startup-truncated (setq truncate-lines t))
  4787. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4788. (org-set-local 'font-lock-unfontify-region-function
  4789. 'org-unfontify-region)
  4790. ;; Activate before-change-function
  4791. (org-set-local 'org-table-may-need-update t)
  4792. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4793. 'local)
  4794. ;; Check for running clock before killing a buffer
  4795. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4796. ;; Initialize macros templates.
  4797. (org-macro-initialize-templates)
  4798. ;; Initialize radio targets.
  4799. (org-update-radio-target-regexp)
  4800. ;; Indentation.
  4801. (org-set-local 'indent-line-function 'org-indent-line)
  4802. (org-set-local 'indent-region-function 'org-indent-region)
  4803. ;; Filling and auto-filling.
  4804. (org-setup-filling)
  4805. ;; Comments.
  4806. (org-setup-comments-handling)
  4807. ;; Initialize cache.
  4808. (org-element-cache-reset)
  4809. ;; Beginning/end of defun
  4810. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4811. (org-set-local 'end-of-defun-function
  4812. (lambda ()
  4813. (if (not (org-at-heading-p))
  4814. (org-forward-element)
  4815. (org-forward-element)
  4816. (forward-char -1))))
  4817. ;; Next error for sparse trees
  4818. (org-set-local 'next-error-function 'org-occur-next-match)
  4819. ;; Make sure dependence stuff works reliably, even for users who set it
  4820. ;; too late :-(
  4821. (if org-enforce-todo-dependencies
  4822. (add-hook 'org-blocker-hook
  4823. 'org-block-todo-from-children-or-siblings-or-parent)
  4824. (remove-hook 'org-blocker-hook
  4825. 'org-block-todo-from-children-or-siblings-or-parent))
  4826. (if org-enforce-todo-checkbox-dependencies
  4827. (add-hook 'org-blocker-hook
  4828. 'org-block-todo-from-checkboxes)
  4829. (remove-hook 'org-blocker-hook
  4830. 'org-block-todo-from-checkboxes))
  4831. ;; Align options lines
  4832. (org-set-local
  4833. 'align-mode-rules-list
  4834. '((org-in-buffer-settings
  4835. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4836. (modes . '(org-mode)))))
  4837. ;; Imenu
  4838. (org-set-local 'imenu-create-index-function
  4839. 'org-imenu-get-tree)
  4840. ;; Make isearch reveal context
  4841. (if (or (featurep 'xemacs)
  4842. (not (boundp 'outline-isearch-open-invisible-function)))
  4843. ;; Emacs 21 and XEmacs make use of the hook
  4844. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4845. ;; Emacs 22 deals with this through a special variable
  4846. (org-set-local 'outline-isearch-open-invisible-function
  4847. (lambda (&rest ignore) (org-show-context 'isearch))))
  4848. ;; Setup the pcomplete hooks
  4849. (set (make-local-variable 'pcomplete-command-completion-function)
  4850. 'org-pcomplete-initial)
  4851. (set (make-local-variable 'pcomplete-command-name-function)
  4852. 'org-command-at-point)
  4853. (set (make-local-variable 'pcomplete-default-completion-function)
  4854. 'ignore)
  4855. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4856. 'org-parse-arguments)
  4857. (set (make-local-variable 'pcomplete-termination-string) "")
  4858. (when (>= emacs-major-version 23)
  4859. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4860. ;; If empty file that did not turn on org-mode automatically, make it to.
  4861. (if (and org-insert-mode-line-in-empty-file
  4862. (org-called-interactively-p 'any)
  4863. (= (point-min) (point-max)))
  4864. (insert "# -*- mode: org -*-\n\n"))
  4865. (unless org-inhibit-startup
  4866. (org-unmodified
  4867. (and org-startup-with-beamer-mode (org-beamer-mode))
  4868. (when org-startup-align-all-tables
  4869. (org-table-map-tables 'org-table-align 'quietly))
  4870. (when org-startup-with-inline-images
  4871. (org-display-inline-images))
  4872. (when org-startup-with-latex-preview
  4873. (org-toggle-latex-fragment))
  4874. (unless org-inhibit-startup-visibility-stuff
  4875. (org-set-startup-visibility))
  4876. (org-refresh-effort-properties)))
  4877. ;; Try to set org-hide correctly
  4878. (let ((foreground (org-find-invisible-foreground)))
  4879. (if foreground
  4880. (set-face-foreground 'org-hide foreground))))
  4881. ;; Update `customize-package-emacs-version-alist'
  4882. (add-to-list 'customize-package-emacs-version-alist
  4883. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4884. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4885. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4886. (defvar org-mode-transpose-word-syntax-table
  4887. (let ((st (make-syntax-table text-mode-syntax-table)))
  4888. (mapc (lambda(c) (modify-syntax-entry
  4889. (string-to-char (car c)) "w p" st))
  4890. org-emphasis-alist)
  4891. st))
  4892. (when (fboundp 'abbrev-table-put)
  4893. (abbrev-table-put org-mode-abbrev-table
  4894. :parents (list text-mode-abbrev-table)))
  4895. (defun org-find-invisible-foreground ()
  4896. (let ((candidates (remove
  4897. "unspecified-bg"
  4898. (nconc
  4899. (list (face-background 'default)
  4900. (face-background 'org-default))
  4901. (mapcar
  4902. (lambda (alist)
  4903. (when (boundp alist)
  4904. (cdr (assoc 'background-color (symbol-value alist)))))
  4905. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4906. (list (face-foreground 'org-hide))))))
  4907. (car (remove nil candidates))))
  4908. (defun org-current-time (&optional rounding-minutes past)
  4909. "Current time, possibly rounded to ROUNDING-MINUTES.
  4910. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4911. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4912. the rounding returns a past time."
  4913. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4914. (car org-time-stamp-rounding-minutes)))
  4915. (time (decode-time)) res)
  4916. (if (< r 1)
  4917. (current-time)
  4918. (setq res
  4919. (apply 'encode-time
  4920. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4921. (nthcdr 2 time))))
  4922. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4923. (seconds-to-time (- (org-float-time res) (* r 60)))
  4924. res))))
  4925. (defun org-today ()
  4926. "Return today date, considering `org-extend-today-until'."
  4927. (time-to-days
  4928. (time-subtract (current-time)
  4929. (list 0 (* 3600 org-extend-today-until) 0))))
  4930. ;;;; Font-Lock stuff, including the activators
  4931. (defvar org-mouse-map (make-sparse-keymap))
  4932. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4933. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4934. (when org-mouse-1-follows-link
  4935. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4936. (when org-tab-follows-link
  4937. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4938. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4939. (require 'font-lock)
  4940. (defconst org-non-link-chars "]\t\n\r<>")
  4941. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4942. "file+sys" "news" "shell" "elisp" "doi" "message"
  4943. "help"))
  4944. (defvar org-link-types-re nil
  4945. "Matches a link that has a url-like prefix like \"http:\"")
  4946. (defvar org-link-re-with-space nil
  4947. "Matches a link with spaces, optional angular brackets around it.")
  4948. (defvar org-link-re-with-space2 nil
  4949. "Matches a link with spaces, optional angular brackets around it.")
  4950. (defvar org-link-re-with-space3 nil
  4951. "Matches a link with spaces, only for internal part in bracket links.")
  4952. (defvar org-angle-link-re nil
  4953. "Matches link with angular brackets, spaces are allowed.")
  4954. (defvar org-plain-link-re nil
  4955. "Matches plain link, without spaces.")
  4956. (defvar org-bracket-link-regexp nil
  4957. "Matches a link in double brackets.")
  4958. (defvar org-bracket-link-analytic-regexp nil
  4959. "Regular expression used to analyze links.
  4960. Here is what the match groups contain after a match:
  4961. 1: http:
  4962. 2: http
  4963. 3: path
  4964. 4: [desc]
  4965. 5: desc")
  4966. (defvar org-bracket-link-analytic-regexp++ nil
  4967. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4968. (defvar org-any-link-re nil
  4969. "Regular expression matching any link.")
  4970. (defconst org-match-sexp-depth 3
  4971. "Number of stacked braces for sub/superscript matching.")
  4972. (defun org-create-multibrace-regexp (left right n)
  4973. "Create a regular expression which will match a balanced sexp.
  4974. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4975. as single character strings.
  4976. The regexp returned will match the entire expression including the
  4977. delimiters. It will also define a single group which contains the
  4978. match except for the outermost delimiters. The maximum depth of
  4979. stacked delimiters is N. Escaping delimiters is not possible."
  4980. (let* ((nothing (concat "[^" left right "]*?"))
  4981. (or "\\|")
  4982. (re nothing)
  4983. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4984. (while (> n 1)
  4985. (setq n (1- n)
  4986. re (concat re or next)
  4987. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4988. (concat left "\\(" re "\\)" right)))
  4989. (defconst org-match-substring-regexp
  4990. (concat
  4991. "\\(\\S-\\)\\([_^]\\)\\("
  4992. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4993. "\\|"
  4994. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4995. "\\|"
  4996. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4997. "The regular expression matching a sub- or superscript.")
  4998. (defconst org-match-substring-with-braces-regexp
  4999. (concat
  5000. "\\(\\S-\\)\\([_^]\\)"
  5001. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5002. "The regular expression matching a sub- or superscript, forcing braces.")
  5003. (defun org-make-link-regexps ()
  5004. "Update the link regular expressions.
  5005. This should be called after the variable `org-link-types' has changed."
  5006. (let ((types-re (regexp-opt org-link-types t)))
  5007. (setq org-link-types-re
  5008. (concat "\\`" types-re ":")
  5009. org-link-re-with-space
  5010. (concat "<?" types-re ":"
  5011. "\\([^" org-non-link-chars " ]"
  5012. "[^" org-non-link-chars "]*"
  5013. "[^" org-non-link-chars " ]\\)>?")
  5014. org-link-re-with-space2
  5015. (concat "<?" types-re ":"
  5016. "\\([^" org-non-link-chars " ]"
  5017. "[^\t\n\r]*"
  5018. "[^" org-non-link-chars " ]\\)>?")
  5019. org-link-re-with-space3
  5020. (concat "<?" types-re ":"
  5021. "\\([^" org-non-link-chars " ]"
  5022. "[^\t\n\r]*\\)")
  5023. org-angle-link-re
  5024. (concat "<" types-re ":"
  5025. "\\([^" org-non-link-chars " ]"
  5026. "[^" org-non-link-chars "]*"
  5027. "\\)>")
  5028. org-plain-link-re
  5029. (concat
  5030. "\\<" types-re ":"
  5031. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5032. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5033. org-bracket-link-regexp
  5034. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5035. org-bracket-link-analytic-regexp
  5036. (concat
  5037. "\\[\\["
  5038. "\\(" types-re ":\\)?"
  5039. "\\([^]]+\\)"
  5040. "\\]"
  5041. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5042. "\\]")
  5043. org-bracket-link-analytic-regexp++
  5044. (concat
  5045. "\\[\\["
  5046. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5047. "\\([^]]+\\)"
  5048. "\\]"
  5049. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5050. "\\]")
  5051. org-any-link-re
  5052. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5053. org-angle-link-re "\\)\\|\\("
  5054. org-plain-link-re "\\)"))))
  5055. (org-make-link-regexps)
  5056. (defvar org-emph-face nil)
  5057. (defun org-do-emphasis-faces (limit)
  5058. "Run through the buffer and emphasize strings."
  5059. (let (rtn a)
  5060. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5061. (let* ((border (char-after (match-beginning 3)))
  5062. (bre (regexp-quote (char-to-string border))))
  5063. (if (and (not (= border (char-after (match-beginning 4))))
  5064. (not (save-match-data
  5065. (string-match (concat bre ".*" bre)
  5066. (replace-regexp-in-string
  5067. "\n" " "
  5068. (substring (match-string 2) 1 -1))))))
  5069. (progn
  5070. (setq rtn t)
  5071. (setq a (assoc (match-string 3) org-emphasis-alist))
  5072. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5073. 'face
  5074. (nth 1 a))
  5075. (and (nth 2 a)
  5076. (org-remove-flyspell-overlays-in
  5077. (match-beginning 0) (match-end 0)))
  5078. (add-text-properties (match-beginning 2) (match-end 2)
  5079. '(font-lock-multiline t org-emphasis t))
  5080. (when org-hide-emphasis-markers
  5081. (add-text-properties (match-end 4) (match-beginning 5)
  5082. '(invisible org-link))
  5083. (add-text-properties (match-beginning 3) (match-end 3)
  5084. '(invisible org-link))))))
  5085. (goto-char (1+ (match-beginning 0))))
  5086. rtn))
  5087. (defun org-emphasize (&optional char)
  5088. "Insert or change an emphasis, i.e. a font like bold or italic.
  5089. If there is an active region, change that region to a new emphasis.
  5090. If there is no region, just insert the marker characters and position
  5091. the cursor between them.
  5092. CHAR should be the marker character. If it is a space, it means to
  5093. remove the emphasis of the selected region.
  5094. If CHAR is not given (for example in an interactive call) it will be
  5095. prompted for."
  5096. (interactive)
  5097. (let ((erc org-emphasis-regexp-components)
  5098. (prompt "")
  5099. (string "") beg end move c s)
  5100. (if (org-region-active-p)
  5101. (setq beg (region-beginning) end (region-end)
  5102. string (buffer-substring beg end))
  5103. (setq move t))
  5104. (unless char
  5105. (message "Emphasis marker or tag: [%s]"
  5106. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5107. (setq char (read-char-exclusive)))
  5108. (if (equal char ?\ )
  5109. (setq s "" move nil)
  5110. (unless (assoc (char-to-string char) org-emphasis-alist)
  5111. (user-error "No such emphasis marker: \"%c\"" char))
  5112. (setq s (char-to-string char)))
  5113. (while (and (> (length string) 1)
  5114. (equal (substring string 0 1) (substring string -1))
  5115. (assoc (substring string 0 1) org-emphasis-alist))
  5116. (setq string (substring string 1 -1)))
  5117. (setq string (concat s string s))
  5118. (if beg (delete-region beg end))
  5119. (unless (or (bolp)
  5120. (string-match (concat "[" (nth 0 erc) "\n]")
  5121. (char-to-string (char-before (point)))))
  5122. (insert " "))
  5123. (unless (or (eobp)
  5124. (string-match (concat "[" (nth 1 erc) "\n]")
  5125. (char-to-string (char-after (point)))))
  5126. (insert " ") (backward-char 1))
  5127. (insert string)
  5128. (and move (backward-char 1))))
  5129. (defconst org-nonsticky-props
  5130. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5131. (defsubst org-rear-nonsticky-at (pos)
  5132. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5133. (defun org-activate-plain-links (limit)
  5134. "Add link properties for plain links."
  5135. (let (f hl)
  5136. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5137. (not (member 'org-tag
  5138. (get-text-property (1- (match-beginning 0)) 'face)))
  5139. (not (org-in-src-block-p)))
  5140. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5141. (setq f (get-text-property (match-beginning 0) 'face))
  5142. (setq hl (org-match-string-no-properties 0))
  5143. (if (or (eq f 'org-tag)
  5144. (and (listp f) (memq 'org-tag f)))
  5145. nil
  5146. (add-text-properties (match-beginning 0) (match-end 0)
  5147. (list 'mouse-face 'highlight
  5148. 'face 'org-link
  5149. 'htmlize-link `(:uri ,hl)
  5150. 'keymap org-mouse-map))
  5151. (org-rear-nonsticky-at (match-end 0)))
  5152. t)))
  5153. (defun org-activate-code (limit)
  5154. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5155. (progn
  5156. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5157. (remove-text-properties (match-beginning 0) (match-end 0)
  5158. '(display t invisible t intangible t))
  5159. t)))
  5160. (defcustom org-src-fontify-natively t
  5161. "When non-nil, fontify code in code blocks."
  5162. :type 'boolean
  5163. :version "24.4"
  5164. :package-version '(Org . "8.3")
  5165. :group 'org-appearance
  5166. :group 'org-babel)
  5167. (defcustom org-allow-promoting-top-level-subtree nil
  5168. "When non-nil, allow promoting a top level subtree.
  5169. The leading star of the top level headline will be replaced
  5170. by a #."
  5171. :type 'boolean
  5172. :version "24.1"
  5173. :group 'org-appearance)
  5174. (defun org-fontify-meta-lines-and-blocks (limit)
  5175. (condition-case nil
  5176. (org-fontify-meta-lines-and-blocks-1 limit)
  5177. (error (message "org-mode fontification error"))))
  5178. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5179. "Fontify #+ lines and blocks."
  5180. (let ((case-fold-search t))
  5181. (if (re-search-forward
  5182. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5183. limit t)
  5184. (let ((beg (match-beginning 0))
  5185. (block-start (match-end 0))
  5186. (block-end nil)
  5187. (lang (match-string 7))
  5188. (beg1 (line-beginning-position 2))
  5189. (dc1 (downcase (match-string 2)))
  5190. (dc3 (downcase (match-string 3)))
  5191. end end1 quoting block-type ovl)
  5192. (cond
  5193. ((and (match-end 4) (equal dc3 "+begin"))
  5194. ;; Truly a block
  5195. (setq block-type (downcase (match-string 5))
  5196. quoting (member block-type org-protecting-blocks))
  5197. (when (re-search-forward
  5198. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5199. nil t) ;; on purpose, we look further than LIMIT
  5200. (setq end (min (point-max) (match-end 0))
  5201. end1 (min (point-max) (1- (match-beginning 0))))
  5202. (setq block-end (match-beginning 0))
  5203. (when quoting
  5204. (org-remove-flyspell-overlays-in beg1 end1)
  5205. (remove-text-properties beg end
  5206. '(display t invisible t intangible t)))
  5207. (add-text-properties
  5208. beg end '(font-lock-fontified t font-lock-multiline t))
  5209. (add-text-properties beg beg1 '(face org-meta-line))
  5210. (org-remove-flyspell-overlays-in beg beg1)
  5211. (add-text-properties ; For end_src
  5212. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5213. (org-remove-flyspell-overlays-in end1 end)
  5214. (cond
  5215. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5216. (org-src-font-lock-fontify-block lang block-start block-end)
  5217. (add-text-properties beg1 block-end '(src-block t)))
  5218. (quoting
  5219. (add-text-properties beg1 (min (point-max) (1+ end1))
  5220. '(face org-block))) ; end of source block
  5221. ((not org-fontify-quote-and-verse-blocks))
  5222. ((string= block-type "quote")
  5223. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5224. ((string= block-type "verse")
  5225. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5226. (add-text-properties beg beg1 '(face org-block-begin-line))
  5227. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5228. '(face org-block-end-line))
  5229. t))
  5230. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5231. (org-remove-flyspell-overlays-in
  5232. (match-beginning 0)
  5233. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5234. (add-text-properties
  5235. beg (match-end 3)
  5236. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5237. '(font-lock-fontified t invisible t)
  5238. '(font-lock-fontified t face org-document-info-keyword)))
  5239. (add-text-properties
  5240. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5241. (if (string-equal dc1 "+title:")
  5242. '(font-lock-fontified t face org-document-title)
  5243. '(font-lock-fontified t face org-document-info))))
  5244. ((equal dc1 "+caption:")
  5245. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5246. (remove-text-properties (match-beginning 0) (match-end 0)
  5247. '(display t invisible t intangible t))
  5248. (add-text-properties (match-beginning 1) (match-end 3)
  5249. '(font-lock-fontified t face org-meta-line))
  5250. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5251. '(font-lock-fontified t face org-block))
  5252. t)
  5253. ((member dc3 '(" " ""))
  5254. (org-remove-flyspell-overlays-in beg (match-end 0))
  5255. (add-text-properties
  5256. beg (match-end 0)
  5257. '(font-lock-fontified t face font-lock-comment-face)))
  5258. (t ;; just any other in-buffer setting, but not indented
  5259. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5260. (remove-text-properties (match-beginning 0) (match-end 0)
  5261. '(display t invisible t intangible t))
  5262. (add-text-properties beg (match-end 0)
  5263. '(font-lock-fontified t face org-meta-line))
  5264. t))))))
  5265. (defun org-fontify-drawers (limit)
  5266. "Fontify drawers."
  5267. (when (re-search-forward org-drawer-regexp limit t)
  5268. (add-text-properties
  5269. (match-beginning 0) (match-end 0)
  5270. '(font-lock-fontified t face org-special-keyword))
  5271. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5272. t))
  5273. (defun org-fontify-macros (limit)
  5274. "Fontify macros."
  5275. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5276. (add-text-properties
  5277. (match-beginning 0) (match-end 0)
  5278. '(font-lock-fontified t face org-macro))
  5279. (when org-hide-macro-markers
  5280. (add-text-properties (match-end 2) (match-beginning 2)
  5281. '(invisible t))
  5282. (add-text-properties (match-beginning 1) (match-end 1)
  5283. '(invisible t)))
  5284. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5285. t))
  5286. (defun org-activate-angle-links (limit)
  5287. "Add text properties for angle links."
  5288. (if (and (re-search-forward org-angle-link-re limit t)
  5289. (not (org-in-src-block-p)))
  5290. (progn
  5291. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5292. (add-text-properties (match-beginning 0) (match-end 0)
  5293. (list 'mouse-face 'highlight
  5294. 'keymap org-mouse-map))
  5295. (org-rear-nonsticky-at (match-end 0))
  5296. t)))
  5297. (defun org-activate-footnote-links (limit)
  5298. "Add text properties for footnotes."
  5299. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5300. (when fn
  5301. (let* ((beg (nth 1 fn))
  5302. (end (nth 2 fn))
  5303. (label (car fn))
  5304. (referencep (/= (line-beginning-position) beg)))
  5305. (when (and referencep (nth 3 fn))
  5306. (save-excursion
  5307. (goto-char beg)
  5308. (search-forward (or label "fn:"))
  5309. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5310. (add-text-properties beg end
  5311. (list 'mouse-face 'highlight
  5312. 'keymap org-mouse-map
  5313. 'help-echo
  5314. (if referencep "Footnote reference"
  5315. "Footnote definition")
  5316. 'font-lock-fontified t
  5317. 'font-lock-multiline t
  5318. 'face 'org-footnote))))))
  5319. (defun org-activate-bracket-links (limit)
  5320. "Add text properties for bracketed links."
  5321. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5322. (not (org-in-src-block-p)))
  5323. (let* ((hl (org-match-string-no-properties 1))
  5324. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5325. (ip (org-maybe-intangible
  5326. (list 'invisible 'org-link
  5327. 'keymap org-mouse-map 'mouse-face 'highlight
  5328. 'font-lock-multiline t 'help-echo help
  5329. 'htmlize-link `(:uri ,hl))))
  5330. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5331. 'font-lock-multiline t 'help-echo help
  5332. 'htmlize-link `(:uri ,hl))))
  5333. ;; We need to remove the invisible property here. Table narrowing
  5334. ;; may have made some of this invisible.
  5335. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5336. (remove-text-properties (match-beginning 0) (match-end 0)
  5337. '(invisible nil))
  5338. (if (match-end 3)
  5339. (progn
  5340. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5341. (org-rear-nonsticky-at (match-beginning 3))
  5342. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5343. (org-rear-nonsticky-at (match-end 3))
  5344. (add-text-properties (match-end 3) (match-end 0) ip)
  5345. (org-rear-nonsticky-at (match-end 0)))
  5346. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5347. (org-rear-nonsticky-at (match-beginning 1))
  5348. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5349. (org-rear-nonsticky-at (match-end 1))
  5350. (add-text-properties (match-end 1) (match-end 0) ip)
  5351. (org-rear-nonsticky-at (match-end 0)))
  5352. t)))
  5353. (defun org-activate-dates (limit)
  5354. "Add text properties for dates."
  5355. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5356. (not (equal (char-before (match-beginning 0)) 91)))
  5357. (progn
  5358. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5359. (add-text-properties (match-beginning 0) (match-end 0)
  5360. (list 'mouse-face 'highlight
  5361. 'keymap org-mouse-map))
  5362. (org-rear-nonsticky-at (match-end 0))
  5363. (when org-display-custom-times
  5364. (if (match-end 3)
  5365. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5366. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5367. t)))
  5368. (defvar org-target-link-regexp nil
  5369. "Regular expression matching radio targets in plain text.")
  5370. (make-variable-buffer-local 'org-target-link-regexp)
  5371. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5372. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5373. border border border))
  5374. "Regular expression matching a link target.")
  5375. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5376. "Regular expression matching a radio target.")
  5377. (defconst org-any-target-regexp
  5378. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5379. "Regular expression matching any target.")
  5380. (defun org-activate-target-links (limit)
  5381. "Add text properties for target matches."
  5382. (when org-target-link-regexp
  5383. (let ((case-fold-search t))
  5384. (if (re-search-forward org-target-link-regexp limit t)
  5385. (progn
  5386. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5387. (add-text-properties (match-beginning 1) (match-end 1)
  5388. (list 'mouse-face 'highlight
  5389. 'keymap org-mouse-map
  5390. 'help-echo "Radio target link"
  5391. 'org-linked-text t))
  5392. (org-rear-nonsticky-at (match-end 1))
  5393. t)))))
  5394. (defun org-update-radio-target-regexp ()
  5395. "Find all radio targets in this file and update the regular expression.
  5396. Also refresh fontification if needed."
  5397. (interactive)
  5398. (let ((old-regexp org-target-link-regexp)
  5399. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5400. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5401. (targets
  5402. (org-with-wide-buffer
  5403. (goto-char (point-min))
  5404. (let (rtn)
  5405. (while (re-search-forward org-radio-target-regexp nil t)
  5406. ;; Make sure point is really within the object.
  5407. (backward-char)
  5408. (let ((obj (org-element-context)))
  5409. (when (eq (org-element-type obj) 'radio-target)
  5410. (add-to-list 'rtn (org-element-property :value obj)))))
  5411. rtn))))
  5412. (setq org-target-link-regexp
  5413. (and targets
  5414. (concat before-re
  5415. (mapconcat
  5416. (lambda (x)
  5417. (replace-regexp-in-string
  5418. " +" "\\s-+" (regexp-quote x) t t))
  5419. targets
  5420. "\\|")
  5421. after-re)))
  5422. (unless (equal old-regexp org-target-link-regexp)
  5423. ;; Clean-up cache.
  5424. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5425. ((not org-target-link-regexp) old-regexp)
  5426. (t
  5427. (concat before-re
  5428. (mapconcat
  5429. (lambda (re)
  5430. (substring re (length before-re)
  5431. (- (length after-re))))
  5432. (list old-regexp org-target-link-regexp)
  5433. "\\|")
  5434. after-re)))))
  5435. (org-with-wide-buffer
  5436. (goto-char (point-min))
  5437. (while (re-search-forward regexp nil t)
  5438. (org-element-cache-refresh (match-beginning 1)))))
  5439. ;; Re fontify buffer.
  5440. (when (memq 'radio org-highlight-links)
  5441. (org-restart-font-lock)))))
  5442. (defun org-hide-wide-columns (limit)
  5443. (let (s e)
  5444. (setq s (text-property-any (point) (or limit (point-max))
  5445. 'org-cwidth t))
  5446. (when s
  5447. (setq e (next-single-property-change s 'org-cwidth))
  5448. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5449. (goto-char e)
  5450. t)))
  5451. (defvar org-latex-and-related-regexp nil
  5452. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5453. (defun org-compute-latex-and-related-regexp ()
  5454. "Compute regular expression for LaTeX, entities and sub/superscript.
  5455. Result depends on variable `org-highlight-latex-and-related'."
  5456. (org-set-local
  5457. 'org-latex-and-related-regexp
  5458. (let* ((re-sub
  5459. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5460. ((eq org-use-sub-superscripts '{})
  5461. (list org-match-substring-with-braces-regexp))
  5462. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5463. (re-latex
  5464. (when (memq 'latex org-highlight-latex-and-related)
  5465. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5466. (delq nil
  5467. (mapcar (lambda (x)
  5468. (and (member (car x) matchers) (nth 1 x)))
  5469. org-latex-regexps)))))
  5470. (re-entities
  5471. (when (memq 'entities org-highlight-latex-and-related)
  5472. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5473. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5474. (defun org-do-latex-and-related (limit)
  5475. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5476. LIMIT bounds the search for syntax to highlight. Stop at first
  5477. highlighted object, if any. Return t if some highlighting was
  5478. done, nil otherwise."
  5479. (when (org-string-nw-p org-latex-and-related-regexp)
  5480. (catch 'found
  5481. (while (re-search-forward org-latex-and-related-regexp limit t)
  5482. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5483. 'face))
  5484. '(org-code org-verbatim underline))
  5485. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5486. '(?_ ?^))
  5487. 1
  5488. 0)))
  5489. (font-lock-prepend-text-property
  5490. (+ offset (match-beginning 0)) (match-end 0)
  5491. 'face 'org-latex-and-related)
  5492. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5493. '(font-lock-multiline t)))
  5494. (throw 'found t)))
  5495. nil)))
  5496. (defun org-restart-font-lock ()
  5497. "Restart `font-lock-mode', to force refontification."
  5498. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5499. (font-lock-mode -1)
  5500. (font-lock-mode 1)))
  5501. (defun org-activate-tags (limit)
  5502. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5503. (progn
  5504. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5505. (add-text-properties (match-beginning 1) (match-end 1)
  5506. (list 'mouse-face 'highlight
  5507. 'keymap org-mouse-map))
  5508. (org-rear-nonsticky-at (match-end 1))
  5509. t)))
  5510. (defun org-outline-level ()
  5511. "Compute the outline level of the heading at point.
  5512. If this is called at a normal headline, the level is the number
  5513. of stars. Use `org-reduced-level' to remove the effect of
  5514. `org-odd-levels'. Unlike to `org-current-level', this function
  5515. takes into consideration inlinetasks."
  5516. (org-with-wide-buffer
  5517. (end-of-line)
  5518. (if (re-search-backward org-outline-regexp-bol nil t)
  5519. (1- (- (match-end 0) (match-beginning 0)))
  5520. 0)))
  5521. (defvar org-font-lock-keywords nil)
  5522. (defsubst org-re-property (property &optional literal allow-null value)
  5523. "Return a regexp matching a PROPERTY line.
  5524. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5525. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5526. non-nil, match properties even without a value.
  5527. Match group 3 is set to the value when it exists. If there is no
  5528. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5529. With optional argument VALUE, match only property lines with
  5530. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5531. unless LITERAL is non-nil."
  5532. (concat
  5533. "^\\(?4:[ \t]*\\)"
  5534. (format "\\(?1::\\(?2:%s\\):\\)"
  5535. (if literal property (regexp-quote property)))
  5536. (cond (value
  5537. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5538. (if literal value (regexp-quote value))))
  5539. (allow-null
  5540. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5541. (t
  5542. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5543. (defconst org-property-re
  5544. (org-re-property "\\S-+" 'literal t)
  5545. "Regular expression matching a property line.
  5546. There are four matching groups:
  5547. 1: :PROPKEY: including the leading and trailing colon,
  5548. 2: PROPKEY without the leading and trailing colon,
  5549. 3: PROPVAL without leading or trailing spaces,
  5550. 4: the indentation of the current line,
  5551. 5: trailing whitespace.")
  5552. (defvar org-font-lock-hook nil
  5553. "Functions to be called for special font lock stuff.")
  5554. (defvar org-font-lock-set-keywords-hook nil
  5555. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5556. This is called after `org-font-lock-extra-keywords' is defined, but before
  5557. it is installed to be used by font lock. This can be useful if something
  5558. needs to be inserted at a specific position in the font-lock sequence.")
  5559. (defun org-font-lock-hook (limit)
  5560. "Run `org-font-lock-hook' within LIMIT."
  5561. (run-hook-with-args 'org-font-lock-hook limit))
  5562. (defun org-set-font-lock-defaults ()
  5563. "Set font lock defaults for the current buffer."
  5564. (let* ((em org-fontify-emphasized-text)
  5565. (lk org-highlight-links)
  5566. (org-font-lock-extra-keywords
  5567. (list
  5568. ;; Call the hook
  5569. '(org-font-lock-hook)
  5570. ;; Headlines
  5571. `(,(if org-fontify-whole-heading-line
  5572. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5573. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5574. (1 (org-get-level-face 1))
  5575. (2 (org-get-level-face 2))
  5576. (3 (org-get-level-face 3)))
  5577. ;; Table lines
  5578. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5579. (1 'org-table t))
  5580. ;; Table internals
  5581. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5582. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5583. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5584. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5585. ;; Drawers
  5586. '(org-fontify-drawers)
  5587. ;; Properties
  5588. (list org-property-re
  5589. '(1 'org-special-keyword t)
  5590. '(3 'org-property-value t))
  5591. ;; Links
  5592. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5593. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5594. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5595. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5596. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5597. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5598. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5599. ;; Targets.
  5600. (list org-any-target-regexp '(0 'org-target t))
  5601. ;; Diary sexps.
  5602. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5603. ;; Macro
  5604. '(org-fontify-macros)
  5605. '(org-hide-wide-columns (0 nil append))
  5606. ;; TODO keyword
  5607. (list (format org-heading-keyword-regexp-format
  5608. org-todo-regexp)
  5609. '(2 (org-get-todo-face 2) t))
  5610. ;; DONE
  5611. (if org-fontify-done-headline
  5612. (list (format org-heading-keyword-regexp-format
  5613. (concat
  5614. "\\(?:"
  5615. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5616. "\\)"))
  5617. '(2 'org-headline-done t))
  5618. nil)
  5619. ;; Priorities
  5620. '(org-font-lock-add-priority-faces)
  5621. ;; Tags
  5622. '(org-font-lock-add-tag-faces)
  5623. ;; Tags groups
  5624. (if (and org-group-tags org-tag-groups-alist)
  5625. (list (concat org-outline-regexp-bol ".+\\(:"
  5626. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5627. ":\\).*$")
  5628. '(1 'org-tag-group prepend)))
  5629. ;; Special keywords
  5630. (list (concat "\\<" org-comment-string) '(0 'org-special-keyword t))
  5631. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5632. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5633. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5634. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5635. ;; Emphasis
  5636. (if em
  5637. (if (featurep 'xemacs)
  5638. '(org-do-emphasis-faces (0 nil append))
  5639. '(org-do-emphasis-faces)))
  5640. ;; Checkboxes
  5641. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5642. 1 'org-checkbox prepend)
  5643. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5644. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5645. (0 (org-get-checkbox-statistics-face) t)))
  5646. ;; Description list items
  5647. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5648. 1 'org-list-dt prepend)
  5649. ;; ARCHIVEd headings
  5650. (list (concat
  5651. org-outline-regexp-bol
  5652. "\\(.*:" org-archive-tag ":.*\\)")
  5653. '(1 'org-archived prepend))
  5654. ;; Specials
  5655. '(org-do-latex-and-related)
  5656. '(org-fontify-entities)
  5657. '(org-raise-scripts)
  5658. ;; Code
  5659. '(org-activate-code (1 'org-code t))
  5660. ;; COMMENT
  5661. (list (format
  5662. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5663. org-todo-regexp
  5664. org-comment-string)
  5665. '(9 'org-special-keyword t))
  5666. ;; Blocks and meta lines
  5667. '(org-fontify-meta-lines-and-blocks))))
  5668. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5669. (run-hooks 'org-font-lock-set-keywords-hook)
  5670. ;; Now set the full font-lock-keywords
  5671. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5672. (org-set-local 'font-lock-defaults
  5673. '(org-font-lock-keywords t nil nil backward-paragraph))
  5674. (kill-local-variable 'font-lock-keywords) nil))
  5675. (defun org-toggle-pretty-entities ()
  5676. "Toggle the composition display of entities as UTF8 characters."
  5677. (interactive)
  5678. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5679. (org-restart-font-lock)
  5680. (if org-pretty-entities
  5681. (message "Entities are now displayed as UTF8 characters")
  5682. (save-restriction
  5683. (widen)
  5684. (org-decompose-region (point-min) (point-max))
  5685. (message "Entities are now displayed as plain text"))))
  5686. (defvar org-custom-properties-overlays nil
  5687. "List of overlays used for custom properties.")
  5688. (make-variable-buffer-local 'org-custom-properties-overlays)
  5689. (defun org-toggle-custom-properties-visibility ()
  5690. "Display or hide properties in `org-custom-properties'."
  5691. (interactive)
  5692. (if org-custom-properties-overlays
  5693. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5694. (setq org-custom-properties-overlays nil))
  5695. (when org-custom-properties
  5696. (org-with-wide-buffer
  5697. (goto-char (point-min))
  5698. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5699. (while (re-search-forward regexp nil t)
  5700. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5701. (when (and end (< (point) end))
  5702. ;; Hide first custom property in current drawer.
  5703. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5704. (overlay-put o 'invisible t)
  5705. (overlay-put o 'org-custom-property t)
  5706. (push o org-custom-properties-overlays))
  5707. ;; Hide additional custom properties in the same drawer.
  5708. (while (re-search-forward regexp end t)
  5709. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5710. (overlay-put o 'invisible t)
  5711. (overlay-put o 'org-custom-property t)
  5712. (push o org-custom-properties-overlays)))))
  5713. ;; Each entry is limited to a single property drawer.
  5714. (outline-next-heading)))))))
  5715. (defun org-fontify-entities (limit)
  5716. "Find an entity to fontify."
  5717. (let (ee)
  5718. (when org-pretty-entities
  5719. (catch 'match
  5720. ;; "\_ "-family is left out on purpose. Only the first one,
  5721. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5722. ;; confusing when adding a second white space character.
  5723. (while (re-search-forward
  5724. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5725. limit t)
  5726. (if (and (not (org-at-comment-p))
  5727. (setq ee (org-entity-get (match-string 1)))
  5728. (= (length (nth 6 ee)) 1))
  5729. (let*
  5730. ((end (if (equal (match-string 2) "{}")
  5731. (match-end 2)
  5732. (match-end 1))))
  5733. (add-text-properties
  5734. (match-beginning 0) end
  5735. (list 'font-lock-fontified t))
  5736. (compose-region (match-beginning 0) end
  5737. (nth 6 ee) nil)
  5738. (backward-char 1)
  5739. (throw 'match t))))
  5740. nil))))
  5741. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5742. "Fontify string S like in Org-mode."
  5743. (with-temp-buffer
  5744. (insert s)
  5745. (let ((org-odd-levels-only odd-levels))
  5746. (org-mode)
  5747. (font-lock-ensure)
  5748. (buffer-string))))
  5749. (defvar org-m nil)
  5750. (defvar org-l nil)
  5751. (defvar org-f nil)
  5752. (defun org-get-level-face (n)
  5753. "Get the right face for match N in font-lock matching of headlines."
  5754. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5755. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5756. (if org-cycle-level-faces
  5757. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5758. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5759. (cond
  5760. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5761. ((eq n 2) org-f)
  5762. (t (if org-level-color-stars-only nil org-f))))
  5763. (defun org-get-todo-face (kwd)
  5764. "Get the right face for a TODO keyword KWD.
  5765. If KWD is a number, get the corresponding match group."
  5766. (if (numberp kwd) (setq kwd (match-string kwd)))
  5767. (or (org-face-from-face-or-color
  5768. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5769. (and (member kwd org-done-keywords) 'org-done)
  5770. 'org-todo))
  5771. (defun org-face-from-face-or-color (context inherit face-or-color)
  5772. "Create a face list that inherits INHERIT, but sets the foreground color.
  5773. When FACE-OR-COLOR is not a string, just return it."
  5774. (if (stringp face-or-color)
  5775. (list :inherit inherit
  5776. (cdr (assoc context org-faces-easy-properties))
  5777. face-or-color)
  5778. face-or-color))
  5779. (defun org-font-lock-add-tag-faces (limit)
  5780. "Add the special tag faces."
  5781. (when (and org-tag-faces org-tags-special-faces-re)
  5782. (while (re-search-forward org-tags-special-faces-re limit t)
  5783. (add-text-properties (match-beginning 1) (match-end 1)
  5784. (list 'face (org-get-tag-face 1)
  5785. 'font-lock-fontified t))
  5786. (backward-char 1))))
  5787. (defun org-font-lock-add-priority-faces (limit)
  5788. "Add the special priority faces."
  5789. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5790. (when (save-match-data (org-at-heading-p))
  5791. (add-text-properties
  5792. (match-beginning 0) (match-end 0)
  5793. (list 'face (or (org-face-from-face-or-color
  5794. 'priority 'org-priority
  5795. (cdr (assoc (char-after (match-beginning 1))
  5796. org-priority-faces)))
  5797. 'org-priority)
  5798. 'font-lock-fontified t)))))
  5799. (defun org-get-tag-face (kwd)
  5800. "Get the right face for a TODO keyword KWD.
  5801. If KWD is a number, get the corresponding match group."
  5802. (if (numberp kwd) (setq kwd (match-string kwd)))
  5803. (or (org-face-from-face-or-color
  5804. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5805. 'org-tag))
  5806. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5807. "Remove fontification and activation overlays from links."
  5808. (font-lock-default-unfontify-region beg end)
  5809. (let* ((buffer-undo-list t)
  5810. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5811. (inhibit-modification-hooks t)
  5812. deactivate-mark buffer-file-name buffer-file-truename)
  5813. (org-decompose-region beg end)
  5814. (remove-text-properties beg end
  5815. '(mouse-face t keymap t org-linked-text t
  5816. invisible t intangible t
  5817. org-emphasis t))
  5818. (org-remove-font-lock-display-properties beg end)))
  5819. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5820. ((raise 0.3) (height 0.7))
  5821. ((raise -0.5))
  5822. ((raise 0.5)))
  5823. "Display properties for showing superscripts and subscripts.")
  5824. (defun org-remove-font-lock-display-properties (beg end)
  5825. "Remove specific display properties that have been added by font lock.
  5826. The will remove the raise properties that are used to show superscripts
  5827. and subscripts."
  5828. (let (next prop)
  5829. (while (< beg end)
  5830. (setq next (next-single-property-change beg 'display nil end)
  5831. prop (get-text-property beg 'display))
  5832. (if (member prop org-script-display)
  5833. (put-text-property beg next 'display nil))
  5834. (setq beg next))))
  5835. (defun org-raise-scripts (limit)
  5836. "Add raise properties to sub/superscripts."
  5837. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5838. (if (re-search-forward
  5839. (if (eq org-use-sub-superscripts t)
  5840. org-match-substring-regexp
  5841. org-match-substring-with-braces-regexp)
  5842. limit t)
  5843. (let* ((pos (point)) table-p comment-p
  5844. (mpos (match-beginning 3))
  5845. (emph-p (get-text-property mpos 'org-emphasis))
  5846. (link-p (get-text-property mpos 'mouse-face))
  5847. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5848. (goto-char (point-at-bol))
  5849. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5850. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5851. (goto-char pos)
  5852. ;; Handle a_b^c
  5853. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5854. (if (or comment-p emph-p link-p keyw-p)
  5855. t
  5856. (put-text-property (match-beginning 3) (match-end 0)
  5857. 'display
  5858. (if (equal (char-after (match-beginning 2)) ?^)
  5859. (nth (if table-p 3 1) org-script-display)
  5860. (nth (if table-p 2 0) org-script-display)))
  5861. (add-text-properties (match-beginning 2) (match-end 2)
  5862. (list 'invisible t
  5863. 'org-dwidth t 'org-dwidth-n 1))
  5864. (if (and (eq (char-after (match-beginning 3)) ?{)
  5865. (eq (char-before (match-end 3)) ?}))
  5866. (progn
  5867. (add-text-properties
  5868. (match-beginning 3) (1+ (match-beginning 3))
  5869. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5870. (add-text-properties
  5871. (1- (match-end 3)) (match-end 3)
  5872. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5873. t)))))
  5874. ;;;; Visibility cycling, including org-goto and indirect buffer
  5875. ;;; Cycling
  5876. (defvar org-cycle-global-status nil)
  5877. (make-variable-buffer-local 'org-cycle-global-status)
  5878. (put 'org-cycle-global-status 'org-state t)
  5879. (defvar org-cycle-subtree-status nil)
  5880. (make-variable-buffer-local 'org-cycle-subtree-status)
  5881. (put 'org-cycle-subtree-status 'org-state t)
  5882. (defvar org-inlinetask-min-level)
  5883. (defun org-unlogged-message (&rest args)
  5884. "Display a message, but avoid logging it in the *Messages* buffer."
  5885. (let ((message-log-max nil))
  5886. (apply 'message args)))
  5887. ;;;###autoload
  5888. (defun org-cycle (&optional arg)
  5889. "TAB-action and visibility cycling for Org-mode.
  5890. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5891. is outline visibility cycling, but it also invokes other actions
  5892. in special contexts.
  5893. - When this function is called with a prefix argument, rotate the entire
  5894. buffer through 3 states (global cycling)
  5895. 1. OVERVIEW: Show only top-level headlines.
  5896. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5897. 3. SHOW ALL: Show everything.
  5898. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5899. determined by the variable `org-startup-folded', and by any VISIBILITY
  5900. properties in the buffer.
  5901. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5902. including any drawers.
  5903. - When inside a table, re-align the table and move to the next field.
  5904. - When point is at the beginning of a headline, rotate the subtree started
  5905. by this line through 3 different states (local cycling)
  5906. 1. FOLDED: Only the main headline is shown.
  5907. 2. CHILDREN: The main headline and the direct children are shown.
  5908. From this state, you can move to one of the children
  5909. and zoom in further.
  5910. 3. SUBTREE: Show the entire subtree, including body text.
  5911. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5912. - When point is at the beginning of an empty headline and the variable
  5913. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5914. of the headline by demoting and promoting it to likely levels. This
  5915. speeds up creation document structure by pressing TAB once or several
  5916. times right after creating a new headline.
  5917. - When there is a numeric prefix, go up to a heading with level ARG, do
  5918. a `show-subtree' and return to the previous cursor position. If ARG
  5919. is negative, go up that many levels.
  5920. - When point is not at the beginning of a headline, execute the global
  5921. binding for TAB, which is re-indenting the line. See the option
  5922. `org-cycle-emulate-tab' for details.
  5923. - Special case: if point is at the beginning of the buffer and there is
  5924. no headline in line 1, this function will act as if called with prefix arg
  5925. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5926. But only if also the variable `org-cycle-global-at-bob' is t."
  5927. (interactive "P")
  5928. (org-load-modules-maybe)
  5929. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5930. (and org-cycle-level-after-item/entry-creation
  5931. (or (org-cycle-level)
  5932. (org-cycle-item-indentation))))
  5933. (let* ((limit-level
  5934. (or org-cycle-max-level
  5935. (and (boundp 'org-inlinetask-min-level)
  5936. org-inlinetask-min-level
  5937. (1- org-inlinetask-min-level))))
  5938. (nstars (and limit-level
  5939. (if org-odd-levels-only
  5940. (and limit-level (1- (* limit-level 2)))
  5941. limit-level)))
  5942. (org-outline-regexp
  5943. (if (not (derived-mode-p 'org-mode))
  5944. outline-regexp
  5945. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5946. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5947. (not (looking-at org-outline-regexp))))
  5948. (org-cycle-hook
  5949. (if bob-special
  5950. (delq 'org-optimize-window-after-visibility-change
  5951. (copy-sequence org-cycle-hook))
  5952. org-cycle-hook))
  5953. (pos (point)))
  5954. (if (or bob-special (equal arg '(4)))
  5955. ;; special case: use global cycling
  5956. (setq arg t))
  5957. (cond
  5958. ((equal arg '(16))
  5959. (setq last-command 'dummy)
  5960. (org-set-startup-visibility)
  5961. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5962. ((equal arg '(64))
  5963. (show-all)
  5964. (org-unlogged-message "Entire buffer visible, including drawers"))
  5965. ;; Try cdlatex TAB completion
  5966. ((org-try-cdlatex-tab))
  5967. ;; Table: enter it or move to the next field.
  5968. ((org-at-table-p 'any)
  5969. (if (org-at-table.el-p)
  5970. (message "Use C-c ' to edit table.el tables")
  5971. (if arg (org-table-edit-field t)
  5972. (org-table-justify-field-maybe)
  5973. (call-interactively 'org-table-next-field))))
  5974. ((run-hook-with-args-until-success
  5975. 'org-tab-after-check-for-table-hook))
  5976. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5977. ((eq arg t) (org-cycle-internal-global))
  5978. ;; Drawers: delegate to `org-flag-drawer'.
  5979. ((save-excursion
  5980. (beginning-of-line 1)
  5981. (looking-at org-drawer-regexp))
  5982. (org-flag-drawer ; toggle block visibility
  5983. (not (get-char-property (match-end 0) 'invisible))))
  5984. ;; Show-subtree, ARG levels up from here.
  5985. ((integerp arg)
  5986. (save-excursion
  5987. (org-back-to-heading)
  5988. (outline-up-heading (if (< arg 0) (- arg)
  5989. (- (funcall outline-level) arg)))
  5990. (org-show-subtree)))
  5991. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5992. ((and (featurep 'org-inlinetask)
  5993. (org-inlinetask-at-task-p)
  5994. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5995. (org-inlinetask-toggle-visibility))
  5996. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5997. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5998. (save-excursion (beginning-of-line 1)
  5999. (looking-at org-outline-regexp)))
  6000. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6001. (org-cycle-internal-local))
  6002. ;; From there: TAB emulation and template completion.
  6003. (buffer-read-only (org-back-to-heading))
  6004. ((run-hook-with-args-until-success
  6005. 'org-tab-after-check-for-cycling-hook))
  6006. ((org-try-structure-completion))
  6007. ((run-hook-with-args-until-success
  6008. 'org-tab-before-tab-emulation-hook))
  6009. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6010. (or (not (bolp))
  6011. (not (looking-at org-outline-regexp))))
  6012. (call-interactively (global-key-binding "\t")))
  6013. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6014. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6015. (or (and (eq org-cycle-emulate-tab 'white)
  6016. (= (match-end 0) (point-at-eol)))
  6017. (and (eq org-cycle-emulate-tab 'whitestart)
  6018. (>= (match-end 0) pos))))
  6019. t
  6020. (eq org-cycle-emulate-tab t))
  6021. (call-interactively (global-key-binding "\t")))
  6022. (t (save-excursion
  6023. (org-back-to-heading)
  6024. (org-cycle)))))))
  6025. (defun org-cycle-internal-global ()
  6026. "Do the global cycling action."
  6027. ;; Hack to avoid display of messages for .org attachments in Gnus
  6028. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6029. (cond
  6030. ((and (eq last-command this-command)
  6031. (eq org-cycle-global-status 'overview))
  6032. ;; We just created the overview - now do table of contents
  6033. ;; This can be slow in very large buffers, so indicate action
  6034. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6035. (unless ga (org-unlogged-message "CONTENTS..."))
  6036. (org-content)
  6037. (unless ga (org-unlogged-message "CONTENTS...done"))
  6038. (setq org-cycle-global-status 'contents)
  6039. (run-hook-with-args 'org-cycle-hook 'contents))
  6040. ((and (eq last-command this-command)
  6041. (eq org-cycle-global-status 'contents))
  6042. ;; We just showed the table of contents - now show everything
  6043. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6044. (show-all)
  6045. (unless ga (org-unlogged-message "SHOW ALL"))
  6046. (setq org-cycle-global-status 'all)
  6047. (run-hook-with-args 'org-cycle-hook 'all))
  6048. (t
  6049. ;; Default action: go to overview
  6050. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6051. (org-overview)
  6052. (unless ga (org-unlogged-message "OVERVIEW"))
  6053. (setq org-cycle-global-status 'overview)
  6054. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6055. (defvar org-called-with-limited-levels nil
  6056. "Non-nil when `org-with-limited-levels' is currently active.")
  6057. (defun org-cycle-internal-local ()
  6058. "Do the local cycling action."
  6059. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6060. ;; First, determine end of headline (EOH), end of subtree or item
  6061. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6062. (save-excursion
  6063. (if (org-at-item-p)
  6064. (progn
  6065. (beginning-of-line)
  6066. (setq struct (org-list-struct))
  6067. (setq eoh (point-at-eol))
  6068. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6069. (setq has-children (org-list-has-child-p (point) struct)))
  6070. (org-back-to-heading)
  6071. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6072. (setq eos (save-excursion (org-end-of-subtree t t)
  6073. (when (bolp) (backward-char)) (point)))
  6074. (setq has-children
  6075. (or (save-excursion
  6076. (let ((level (funcall outline-level)))
  6077. (outline-next-heading)
  6078. (and (org-at-heading-p t)
  6079. (> (funcall outline-level) level))))
  6080. (save-excursion
  6081. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6082. ;; Determine end invisible part of buffer (EOL)
  6083. (beginning-of-line 2)
  6084. ;; XEmacs doesn't have `next-single-char-property-change'
  6085. (if (featurep 'xemacs)
  6086. (while (and (not (eobp)) ;; this is like `next-line'
  6087. (get-char-property (1- (point)) 'invisible))
  6088. (beginning-of-line 2))
  6089. (while (and (not (eobp)) ;; this is like `next-line'
  6090. (get-char-property (1- (point)) 'invisible))
  6091. (goto-char (next-single-char-property-change (point) 'invisible))
  6092. (and (eolp) (beginning-of-line 2))))
  6093. (setq eol (point)))
  6094. ;; Find out what to do next and set `this-command'
  6095. (cond
  6096. ((= eos eoh)
  6097. ;; Nothing is hidden behind this heading
  6098. (unless (org-before-first-heading-p)
  6099. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6100. (org-unlogged-message "EMPTY ENTRY")
  6101. (setq org-cycle-subtree-status nil)
  6102. (save-excursion
  6103. (goto-char eos)
  6104. (outline-next-heading)
  6105. (if (outline-invisible-p) (org-flag-heading nil))))
  6106. ((and (or (>= eol eos)
  6107. (not (string-match "\\S-" (buffer-substring eol eos))))
  6108. (or has-children
  6109. (not (setq children-skipped
  6110. org-cycle-skip-children-state-if-no-children))))
  6111. ;; Entire subtree is hidden in one line: children view
  6112. (unless (org-before-first-heading-p)
  6113. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6114. (if (org-at-item-p)
  6115. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6116. (org-show-entry)
  6117. (org-with-limited-levels (show-children))
  6118. ;; FIXME: This slows down the func way too much.
  6119. ;; How keep drawers hidden in subtree anyway?
  6120. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6121. ;; (org-cycle-hide-drawers 'subtree))
  6122. ;; Fold every list in subtree to top-level items.
  6123. (when (eq org-cycle-include-plain-lists 'integrate)
  6124. (save-excursion
  6125. (org-back-to-heading)
  6126. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6127. (beginning-of-line 1)
  6128. (let* ((struct (org-list-struct))
  6129. (prevs (org-list-prevs-alist struct))
  6130. (end (org-list-get-bottom-point struct)))
  6131. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6132. (org-list-get-all-items (point) struct prevs))
  6133. (goto-char (if (< end eos) end eos)))))))
  6134. (org-unlogged-message "CHILDREN")
  6135. (save-excursion
  6136. (goto-char eos)
  6137. (outline-next-heading)
  6138. (if (outline-invisible-p) (org-flag-heading nil)))
  6139. (setq org-cycle-subtree-status 'children)
  6140. (unless (org-before-first-heading-p)
  6141. (run-hook-with-args 'org-cycle-hook 'children)))
  6142. ((or children-skipped
  6143. (and (eq last-command this-command)
  6144. (eq org-cycle-subtree-status 'children)))
  6145. ;; We just showed the children, or no children are there,
  6146. ;; now show everything.
  6147. (unless (org-before-first-heading-p)
  6148. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6149. (outline-flag-region eoh eos nil)
  6150. (org-unlogged-message
  6151. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6152. (setq org-cycle-subtree-status 'subtree)
  6153. (unless (org-before-first-heading-p)
  6154. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6155. (t
  6156. ;; Default action: hide the subtree.
  6157. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6158. (outline-flag-region eoh eos t)
  6159. (org-unlogged-message "FOLDED")
  6160. (setq org-cycle-subtree-status 'folded)
  6161. (unless (org-before-first-heading-p)
  6162. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6163. ;;;###autoload
  6164. (defun org-global-cycle (&optional arg)
  6165. "Cycle the global visibility. For details see `org-cycle'.
  6166. With \\[universal-argument] prefix arg, switch to startup visibility.
  6167. With a numeric prefix, show all headlines up to that level."
  6168. (interactive "P")
  6169. (let ((org-cycle-include-plain-lists
  6170. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6171. (cond
  6172. ((integerp arg)
  6173. (show-all)
  6174. (hide-sublevels arg)
  6175. (setq org-cycle-global-status 'contents))
  6176. ((equal arg '(4))
  6177. (org-set-startup-visibility)
  6178. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6179. (t
  6180. (org-cycle '(4))))))
  6181. (defun org-set-startup-visibility ()
  6182. "Set the visibility required by startup options and properties."
  6183. (cond
  6184. ((eq org-startup-folded t)
  6185. (org-overview))
  6186. ((eq org-startup-folded 'content)
  6187. (org-content))
  6188. ((or (eq org-startup-folded 'showeverything)
  6189. (eq org-startup-folded nil))
  6190. (show-all)))
  6191. (unless (eq org-startup-folded 'showeverything)
  6192. (if org-hide-block-startup (org-hide-block-all))
  6193. (org-set-visibility-according-to-property 'no-cleanup)
  6194. (org-cycle-hide-archived-subtrees 'all)
  6195. (org-cycle-hide-drawers 'all)
  6196. (org-cycle-show-empty-lines t)))
  6197. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6198. "Switch subtree visibilities according to :VISIBILITY: property."
  6199. (interactive)
  6200. (let (org-show-entry-below)
  6201. (org-with-wide-buffer
  6202. (goto-char (point-min))
  6203. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6204. (if (not (org-at-property-p)) (outline-next-heading)
  6205. (let ((state (match-string 3)))
  6206. (save-excursion
  6207. (org-back-to-heading t)
  6208. (hide-subtree)
  6209. (org-reveal)
  6210. (cond
  6211. ((equal state "folded")
  6212. (hide-subtree))
  6213. ((equal state "children")
  6214. (org-show-hidden-entry)
  6215. (show-children))
  6216. ((equal state "content")
  6217. (save-excursion
  6218. (save-restriction
  6219. (org-narrow-to-subtree)
  6220. (org-content))))
  6221. ((member state '("all" "showall"))
  6222. (show-subtree)))))))
  6223. (unless no-cleanup
  6224. (org-cycle-hide-archived-subtrees 'all)
  6225. (org-cycle-hide-drawers 'all)
  6226. (org-cycle-show-empty-lines 'all)))))
  6227. ;; This function uses outline-regexp instead of the more fundamental
  6228. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6229. ;; buffers, where outline-regexp is needed.
  6230. (defun org-overview ()
  6231. "Switch to overview mode, showing only top-level headlines.
  6232. This shows all headlines with a level equal or greater than the level
  6233. of the first headline in the buffer. This is important, because if the
  6234. first headline is not level one, then (hide-sublevels 1) gives confusing
  6235. results."
  6236. (interactive)
  6237. (save-excursion
  6238. (let ((level
  6239. (save-excursion
  6240. (goto-char (point-min))
  6241. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6242. (progn
  6243. (goto-char (match-beginning 0))
  6244. (funcall outline-level))))))
  6245. (and level (hide-sublevels level)))))
  6246. (defun org-content (&optional arg)
  6247. "Show all headlines in the buffer, like a table of contents.
  6248. With numerical argument N, show content up to level N."
  6249. (interactive "P")
  6250. (org-overview)
  6251. (save-excursion
  6252. ;; Visit all headings and show their offspring
  6253. (and (integerp arg) (org-overview))
  6254. (goto-char (point-max))
  6255. (catch 'exit
  6256. (while (and (progn (condition-case nil
  6257. (outline-previous-visible-heading 1)
  6258. (error (goto-char (point-min))))
  6259. t)
  6260. (looking-at org-outline-regexp))
  6261. (if (integerp arg)
  6262. (show-children (1- arg))
  6263. (show-branches))
  6264. (if (bobp) (throw 'exit nil))))))
  6265. (defun org-optimize-window-after-visibility-change (state)
  6266. "Adjust the window after a change in outline visibility.
  6267. This function is the default value of the hook `org-cycle-hook'."
  6268. (when (get-buffer-window (current-buffer))
  6269. (cond
  6270. ((eq state 'content) nil)
  6271. ((eq state 'all) nil)
  6272. ((eq state 'folded) nil)
  6273. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6274. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6275. (defun org-remove-empty-overlays-at (pos)
  6276. "Remove outline overlays that do not contain non-white stuff."
  6277. (mapc
  6278. (lambda (o)
  6279. (and (eq 'outline (overlay-get o 'invisible))
  6280. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6281. (overlay-end o))))
  6282. (delete-overlay o)))
  6283. (overlays-at pos)))
  6284. (defun org-clean-visibility-after-subtree-move ()
  6285. "Fix visibility issues after moving a subtree."
  6286. ;; First, find a reasonable region to look at:
  6287. ;; Start two siblings above, end three below
  6288. (let* ((beg (save-excursion
  6289. (and (org-get-last-sibling)
  6290. (org-get-last-sibling))
  6291. (point)))
  6292. (end (save-excursion
  6293. (and (org-get-next-sibling)
  6294. (org-get-next-sibling)
  6295. (org-get-next-sibling))
  6296. (if (org-at-heading-p)
  6297. (point-at-eol)
  6298. (point))))
  6299. (level (looking-at "\\*+"))
  6300. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6301. (save-excursion
  6302. (save-restriction
  6303. (narrow-to-region beg end)
  6304. (when re
  6305. ;; Properly fold already folded siblings
  6306. (goto-char (point-min))
  6307. (while (re-search-forward re nil t)
  6308. (if (and (not (outline-invisible-p))
  6309. (save-excursion
  6310. (goto-char (point-at-eol)) (outline-invisible-p)))
  6311. (hide-entry))))
  6312. (org-cycle-show-empty-lines 'overview)
  6313. (org-cycle-hide-drawers 'overview)))))
  6314. (defun org-cycle-show-empty-lines (state)
  6315. "Show empty lines above all visible headlines.
  6316. The region to be covered depends on STATE when called through
  6317. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6318. entire buffer covered. Note that an empty line is only shown if there
  6319. are at least `org-cycle-separator-lines' empty lines before the headline."
  6320. (when (not (= org-cycle-separator-lines 0))
  6321. (save-excursion
  6322. (let* ((n (abs org-cycle-separator-lines))
  6323. (re (cond
  6324. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6325. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6326. (t (let ((ns (number-to-string (- n 2))))
  6327. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6328. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6329. beg end b e)
  6330. (cond
  6331. ((memq state '(overview contents t))
  6332. (setq beg (point-min) end (point-max)))
  6333. ((memq state '(children folded))
  6334. (setq beg (point) end (progn (org-end-of-subtree t t)
  6335. (beginning-of-line 2)
  6336. (point)))))
  6337. (when beg
  6338. (goto-char beg)
  6339. (while (re-search-forward re end t)
  6340. (unless (get-char-property (match-end 1) 'invisible)
  6341. (setq e (match-end 1))
  6342. (if (< org-cycle-separator-lines 0)
  6343. (setq b (save-excursion
  6344. (goto-char (match-beginning 0))
  6345. (org-back-over-empty-lines)
  6346. (if (save-excursion
  6347. (goto-char (max (point-min) (1- (point))))
  6348. (org-at-heading-p))
  6349. (1- (point))
  6350. (point))))
  6351. (setq b (match-beginning 1)))
  6352. (outline-flag-region b e nil)))))))
  6353. ;; Never hide empty lines at the end of the file.
  6354. (save-excursion
  6355. (goto-char (point-max))
  6356. (outline-previous-heading)
  6357. (outline-end-of-heading)
  6358. (if (and (looking-at "[ \t\n]+")
  6359. (= (match-end 0) (point-max)))
  6360. (outline-flag-region (point) (match-end 0) nil))))
  6361. (defun org-show-empty-lines-in-parent ()
  6362. "Move to the parent and re-show empty lines before visible headlines."
  6363. (save-excursion
  6364. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6365. (org-cycle-show-empty-lines context))))
  6366. (defun org-files-list ()
  6367. "Return `org-agenda-files' list, plus all open org-mode files.
  6368. This is useful for operations that need to scan all of a user's
  6369. open and agenda-wise Org files."
  6370. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6371. (dolist (buf (buffer-list))
  6372. (with-current-buffer buf
  6373. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6374. (let ((file (expand-file-name (buffer-file-name))))
  6375. (unless (member file files)
  6376. (push file files))))))
  6377. files))
  6378. (defsubst org-entry-beginning-position ()
  6379. "Return the beginning position of the current entry."
  6380. (save-excursion (outline-back-to-heading t) (point)))
  6381. (defsubst org-entry-end-position ()
  6382. "Return the end position of the current entry."
  6383. (save-excursion (outline-next-heading) (point)))
  6384. (defun org-cycle-hide-drawers (state &optional exceptions)
  6385. "Re-hide all drawers after a visibility state change.
  6386. When non-nil, optional argument EXCEPTIONS is a list of strings
  6387. specifying which drawers should not be hidden."
  6388. (when (and (derived-mode-p 'org-mode)
  6389. (not (memq state '(overview folded contents))))
  6390. (save-excursion
  6391. (let* ((globalp (memq state '(contents all)))
  6392. (beg (if globalp (point-min) (point)))
  6393. (end (if globalp (point-max)
  6394. (if (eq state 'children)
  6395. (save-excursion (outline-next-heading) (point))
  6396. (org-end-of-subtree t)))))
  6397. (goto-char beg)
  6398. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6399. (unless (member-ignore-case (match-string 1) exceptions)
  6400. (let ((drawer (org-element-at-point)))
  6401. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6402. (org-flag-drawer t drawer)
  6403. ;; Make sure to skip drawer entirely or we might flag
  6404. ;; it another time when matching its ending line with
  6405. ;; `org-drawer-regexp'.
  6406. (goto-char (org-element-property :end drawer))))))))))
  6407. (defun org-flag-drawer (flag &optional element)
  6408. "When FLAG is non-nil, hide the drawer we are at.
  6409. Otherwise make it visible. When optional argument ELEMENT is
  6410. a parsed drawer, as returned by `org-element-at-point', hide or
  6411. show that drawer instead."
  6412. (when (save-excursion
  6413. (beginning-of-line)
  6414. (org-looking-at-p org-drawer-regexp))
  6415. (let ((drawer (or element (org-element-at-point))))
  6416. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6417. (let ((post (org-element-property :post-affiliated drawer)))
  6418. (save-excursion
  6419. (outline-flag-region
  6420. (progn (goto-char post) (line-end-position))
  6421. (progn (goto-char (org-element-property :end drawer))
  6422. (skip-chars-backward " \r\t\n")
  6423. (line-end-position))
  6424. flag))
  6425. ;; When the drawer is hidden away, make sure point lies in
  6426. ;; a visible part of the buffer.
  6427. (when (and flag (> (line-beginning-position) post))
  6428. (goto-char post)))))))
  6429. (defun org-subtree-end-visible-p ()
  6430. "Is the end of the current subtree visible?"
  6431. (pos-visible-in-window-p
  6432. (save-excursion (org-end-of-subtree t) (point))))
  6433. (defun org-first-headline-recenter ()
  6434. "Move cursor to the first headline and recenter the headline."
  6435. (goto-char (point-min))
  6436. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6437. (set-window-start (selected-window) (point-at-bol))))
  6438. ;;; Saving and restoring visibility
  6439. (defun org-outline-overlay-data (&optional use-markers)
  6440. "Return a list of the locations of all outline overlays.
  6441. These are overlays with the `invisible' property value `outline'.
  6442. The return value is a list of cons cells, with start and stop
  6443. positions for each overlay.
  6444. If USE-MARKERS is set, return the positions as markers."
  6445. (let (beg end)
  6446. (save-excursion
  6447. (save-restriction
  6448. (widen)
  6449. (delq nil
  6450. (mapcar (lambda (o)
  6451. (when (eq (overlay-get o 'invisible) 'outline)
  6452. (setq beg (overlay-start o)
  6453. end (overlay-end o))
  6454. (and beg end (> end beg)
  6455. (if use-markers
  6456. (cons (copy-marker beg)
  6457. (copy-marker end t))
  6458. (cons beg end)))))
  6459. (overlays-in (point-min) (point-max))))))))
  6460. (defun org-set-outline-overlay-data (data)
  6461. "Create visibility overlays for all positions in DATA.
  6462. DATA should have been made by `org-outline-overlay-data'."
  6463. (let (o)
  6464. (save-excursion
  6465. (save-restriction
  6466. (widen)
  6467. (show-all)
  6468. (mapc (lambda (c)
  6469. (outline-flag-region (car c) (cdr c) t))
  6470. data)))))
  6471. ;;; Folding of blocks
  6472. (defvar org-hide-block-overlays nil
  6473. "Overlays hiding blocks.")
  6474. (make-variable-buffer-local 'org-hide-block-overlays)
  6475. (defun org-block-map (function &optional start end)
  6476. "Call FUNCTION at the head of all source blocks in the current buffer.
  6477. Optional arguments START and END can be used to limit the range."
  6478. (let ((start (or start (point-min)))
  6479. (end (or end (point-max))))
  6480. (save-excursion
  6481. (goto-char start)
  6482. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6483. (save-excursion
  6484. (save-match-data
  6485. (goto-char (match-beginning 0))
  6486. (funcall function)))))))
  6487. (defun org-hide-block-toggle-all ()
  6488. "Toggle the visibility of all blocks in the current buffer."
  6489. (org-block-map 'org-hide-block-toggle))
  6490. (defun org-hide-block-all ()
  6491. "Fold all blocks in the current buffer."
  6492. (interactive)
  6493. (org-show-block-all)
  6494. (org-block-map 'org-hide-block-toggle-maybe))
  6495. (defun org-show-block-all ()
  6496. "Unfold all blocks in the current buffer."
  6497. (interactive)
  6498. (mapc 'delete-overlay org-hide-block-overlays)
  6499. (setq org-hide-block-overlays nil))
  6500. (defun org-hide-block-toggle-maybe ()
  6501. "Toggle visibility of block at point.
  6502. Unlike to `org-hide-block-toggle', this function does not throw
  6503. an error. Return a non-nil value when toggling is successful."
  6504. (interactive)
  6505. (ignore-errors (org-hide-block-toggle)))
  6506. (defun org-hide-block-toggle (&optional force)
  6507. "Toggle the visibility of the current block.
  6508. When optional argument FORCE is `off', make block visible. If it
  6509. is non-nil, hide it unconditionally. Throw an error when not at
  6510. a block. Return a non-nil value when toggling is successful."
  6511. (interactive)
  6512. (let ((element (org-element-at-point)))
  6513. (unless (memq (org-element-type element)
  6514. '(center-block comment-block dynamic-block example-block
  6515. export-block quote-block special-block
  6516. src-block verse-block))
  6517. (user-error "Not at a block"))
  6518. (let* ((start (save-excursion
  6519. (goto-char (org-element-property :post-affiliated element))
  6520. (line-end-position)))
  6521. (end (save-excursion
  6522. (goto-char (org-element-property :end element))
  6523. (skip-chars-backward " \r\t\n")
  6524. (line-end-position)))
  6525. (overlays (overlays-at start)))
  6526. (cond
  6527. ;; Do nothing when not before or at the block opening line or
  6528. ;; at the block closing line.
  6529. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6530. ((and (not (eq force 'off))
  6531. (not (memq t (mapcar
  6532. (lambda (o)
  6533. (eq (overlay-get o 'invisible) 'org-hide-block))
  6534. overlays))))
  6535. (let ((ov (make-overlay start end)))
  6536. (overlay-put ov 'invisible 'org-hide-block)
  6537. ;; Make the block accessible to `isearch'.
  6538. (overlay-put
  6539. ov 'isearch-open-invisible
  6540. (lambda (ov)
  6541. (when (memq ov org-hide-block-overlays)
  6542. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6543. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6544. (delete-overlay ov))))
  6545. (push ov org-hide-block-overlays)
  6546. ;; When the block is hidden away, make sure point is left in
  6547. ;; a visible part of the buffer.
  6548. (when (> (line-beginning-position) start)
  6549. (goto-char start)
  6550. (beginning-of-line))
  6551. ;; Signal successful toggling.
  6552. t))
  6553. ((or (not force) (eq force 'off))
  6554. (dolist (ov overlays t)
  6555. (when (memq ov org-hide-block-overlays)
  6556. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6557. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6558. (delete-overlay ov))))))))
  6559. ;; org-tab-after-check-for-cycling-hook
  6560. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6561. ;; Remove overlays when changing major mode
  6562. (add-hook 'org-mode-hook
  6563. (lambda () (org-add-hook 'change-major-mode-hook
  6564. 'org-show-block-all 'append 'local)))
  6565. ;;; Org-goto
  6566. (defvar org-goto-window-configuration nil)
  6567. (defvar org-goto-marker nil)
  6568. (defvar org-goto-map)
  6569. (defun org-goto-map ()
  6570. "Set the keymap `org-goto'."
  6571. (setq org-goto-map
  6572. (let ((map (make-sparse-keymap)))
  6573. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6574. mouse-drag-region universal-argument org-occur))
  6575. cmd)
  6576. (while (setq cmd (pop cmds))
  6577. (substitute-key-definition cmd cmd map global-map)))
  6578. (suppress-keymap map)
  6579. (org-defkey map "\C-m" 'org-goto-ret)
  6580. (org-defkey map [(return)] 'org-goto-ret)
  6581. (org-defkey map [(left)] 'org-goto-left)
  6582. (org-defkey map [(right)] 'org-goto-right)
  6583. (org-defkey map [(control ?g)] 'org-goto-quit)
  6584. (org-defkey map "\C-i" 'org-cycle)
  6585. (org-defkey map [(tab)] 'org-cycle)
  6586. (org-defkey map [(down)] 'outline-next-visible-heading)
  6587. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6588. (if org-goto-auto-isearch
  6589. (if (fboundp 'define-key-after)
  6590. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6591. nil)
  6592. (org-defkey map "q" 'org-goto-quit)
  6593. (org-defkey map "n" 'outline-next-visible-heading)
  6594. (org-defkey map "p" 'outline-previous-visible-heading)
  6595. (org-defkey map "f" 'outline-forward-same-level)
  6596. (org-defkey map "b" 'outline-backward-same-level)
  6597. (org-defkey map "u" 'outline-up-heading))
  6598. (org-defkey map "/" 'org-occur)
  6599. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6600. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6601. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6602. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6603. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6604. map)))
  6605. (defconst org-goto-help
  6606. "Browse buffer copy, to find location or copy text.%s
  6607. RET=jump to location C-g=quit and return to previous location
  6608. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6609. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6610. (defun org-goto (&optional alternative-interface)
  6611. "Look up a different location in the current file, keeping current visibility.
  6612. When you want look-up or go to a different location in a
  6613. document, the fastest way is often to fold the entire buffer and
  6614. then dive into the tree. This method has the disadvantage, that
  6615. the previous location will be folded, which may not be what you
  6616. want.
  6617. This command works around this by showing a copy of the current
  6618. buffer in an indirect buffer, in overview mode. You can dive
  6619. into the tree in that copy, use org-occur and incremental search
  6620. to find a location. When pressing RET or `Q', the command
  6621. returns to the original buffer in which the visibility is still
  6622. unchanged. After RET it will also jump to the location selected
  6623. in the indirect buffer and expose the headline hierarchy above.
  6624. With a prefix argument, use the alternative interface: e.g. if
  6625. `org-goto-interface' is 'outline use 'outline-path-completion."
  6626. (interactive "P")
  6627. (org-goto-map)
  6628. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6629. (org-refile-use-outline-path t)
  6630. (org-refile-target-verify-function nil)
  6631. (interface
  6632. (if (not alternative-interface)
  6633. org-goto-interface
  6634. (if (eq org-goto-interface 'outline)
  6635. 'outline-path-completion
  6636. 'outline)))
  6637. (org-goto-start-pos (point))
  6638. (selected-point
  6639. (if (eq interface 'outline)
  6640. (car (org-get-location (current-buffer) org-goto-help))
  6641. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6642. (org-refile-check-position pa)
  6643. (nth 3 pa)))))
  6644. (if selected-point
  6645. (progn
  6646. (org-mark-ring-push org-goto-start-pos)
  6647. (goto-char selected-point)
  6648. (if (or (outline-invisible-p) (org-invisible-p2))
  6649. (org-show-context 'org-goto)))
  6650. (message "Quit"))))
  6651. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6652. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6653. (defvar org-goto-local-auto-isearch-map) ; defined below
  6654. (defun org-get-location (buf help)
  6655. "Let the user select a location in the Org-mode buffer BUF.
  6656. This function uses a recursive edit. It returns the selected position
  6657. or nil."
  6658. (org-no-popups
  6659. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6660. (isearch-hide-immediately nil)
  6661. (isearch-search-fun-function
  6662. (lambda () 'org-goto-local-search-headings))
  6663. (org-goto-selected-point org-goto-exit-command))
  6664. (save-excursion
  6665. (save-window-excursion
  6666. (delete-other-windows)
  6667. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6668. (org-pop-to-buffer-same-window
  6669. (condition-case nil
  6670. (make-indirect-buffer (current-buffer) "*org-goto*")
  6671. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6672. (with-output-to-temp-buffer "*Org Help*"
  6673. (princ (format help (if org-goto-auto-isearch
  6674. " Just type for auto-isearch."
  6675. " n/p/f/b/u to navigate, q to quit."))))
  6676. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6677. (setq buffer-read-only nil)
  6678. (let ((org-startup-truncated t)
  6679. (org-startup-folded nil)
  6680. (org-startup-align-all-tables nil))
  6681. (org-mode)
  6682. (org-overview))
  6683. (setq buffer-read-only t)
  6684. (if (and (boundp 'org-goto-start-pos)
  6685. (integer-or-marker-p org-goto-start-pos))
  6686. (progn (goto-char org-goto-start-pos)
  6687. (when (outline-invisible-p)
  6688. (org-show-set-visibility 'lineage)))
  6689. (goto-char (point-min)))
  6690. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6691. (message "Select location and press RET")
  6692. (use-local-map org-goto-map)
  6693. (recursive-edit)))
  6694. (kill-buffer "*org-goto*")
  6695. (cons org-goto-selected-point org-goto-exit-command))))
  6696. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6697. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6698. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6699. (if (fboundp 'isearch-other-control-char)
  6700. (progn
  6701. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6702. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6703. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6704. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6705. (define-key org-goto-local-auto-isearch-map [return] nil))
  6706. (defun org-goto-local-search-headings (string bound noerror)
  6707. "Search and make sure that any matches are in headlines."
  6708. (catch 'return
  6709. (while (if isearch-forward
  6710. (search-forward string bound noerror)
  6711. (search-backward string bound noerror))
  6712. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6713. (and (member :headline context)
  6714. (not (member :tags context))))
  6715. (throw 'return (point))))))
  6716. (defun org-goto-local-auto-isearch ()
  6717. "Start isearch."
  6718. (interactive)
  6719. (goto-char (point-min))
  6720. (let ((keys (this-command-keys)))
  6721. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6722. (isearch-mode t)
  6723. (isearch-process-search-char (string-to-char keys)))))
  6724. (defun org-goto-ret (&optional arg)
  6725. "Finish `org-goto' by going to the new location."
  6726. (interactive "P")
  6727. (setq org-goto-selected-point (point)
  6728. org-goto-exit-command 'return)
  6729. (throw 'exit nil))
  6730. (defun org-goto-left ()
  6731. "Finish `org-goto' by going to the new location."
  6732. (interactive)
  6733. (if (org-at-heading-p)
  6734. (progn
  6735. (beginning-of-line 1)
  6736. (setq org-goto-selected-point (point)
  6737. org-goto-exit-command 'left)
  6738. (throw 'exit nil))
  6739. (user-error "Not on a heading")))
  6740. (defun org-goto-right ()
  6741. "Finish `org-goto' by going to the new location."
  6742. (interactive)
  6743. (if (org-at-heading-p)
  6744. (progn
  6745. (setq org-goto-selected-point (point)
  6746. org-goto-exit-command 'right)
  6747. (throw 'exit nil))
  6748. (user-error "Not on a heading")))
  6749. (defun org-goto-quit ()
  6750. "Finish `org-goto' without cursor motion."
  6751. (interactive)
  6752. (setq org-goto-selected-point nil)
  6753. (setq org-goto-exit-command 'quit)
  6754. (throw 'exit nil))
  6755. ;;; Indirect buffer display of subtrees
  6756. (defvar org-indirect-dedicated-frame nil
  6757. "This is the frame being used for indirect tree display.")
  6758. (defvar org-last-indirect-buffer nil)
  6759. (defun org-tree-to-indirect-buffer (&optional arg)
  6760. "Create indirect buffer and narrow it to current subtree.
  6761. With a numerical prefix ARG, go up to this level and then take that tree.
  6762. If ARG is negative, go up that many levels.
  6763. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6764. indirect buffer previously made with this command, to avoid proliferation of
  6765. indirect buffers. However, when you call the command with a \
  6766. \\[universal-argument] prefix, or
  6767. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6768. is kept so that you can work with several indirect buffers at the same time.
  6769. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6770. \\[universal-argument] prefix also
  6771. requests that a new frame be made for the new buffer, so that the dedicated
  6772. frame is not changed."
  6773. (interactive "P")
  6774. (let ((cbuf (current-buffer))
  6775. (cwin (selected-window))
  6776. (pos (point))
  6777. beg end level heading ibuf)
  6778. (save-excursion
  6779. (org-back-to-heading t)
  6780. (when (numberp arg)
  6781. (setq level (org-outline-level))
  6782. (if (< arg 0) (setq arg (+ level arg)))
  6783. (while (> (setq level (org-outline-level)) arg)
  6784. (org-up-heading-safe)))
  6785. (setq beg (point)
  6786. heading (org-get-heading))
  6787. (org-end-of-subtree t t)
  6788. (if (org-at-heading-p) (backward-char 1))
  6789. (setq end (point)))
  6790. (if (and (buffer-live-p org-last-indirect-buffer)
  6791. (not (eq org-indirect-buffer-display 'new-frame))
  6792. (not arg))
  6793. (kill-buffer org-last-indirect-buffer))
  6794. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6795. org-last-indirect-buffer ibuf)
  6796. (cond
  6797. ((or (eq org-indirect-buffer-display 'new-frame)
  6798. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6799. (select-frame (make-frame))
  6800. (delete-other-windows)
  6801. (org-pop-to-buffer-same-window ibuf)
  6802. (org-set-frame-title heading))
  6803. ((eq org-indirect-buffer-display 'dedicated-frame)
  6804. (raise-frame
  6805. (select-frame (or (and org-indirect-dedicated-frame
  6806. (frame-live-p org-indirect-dedicated-frame)
  6807. org-indirect-dedicated-frame)
  6808. (setq org-indirect-dedicated-frame (make-frame)))))
  6809. (delete-other-windows)
  6810. (org-pop-to-buffer-same-window ibuf)
  6811. (org-set-frame-title (concat "Indirect: " heading)))
  6812. ((eq org-indirect-buffer-display 'current-window)
  6813. (org-pop-to-buffer-same-window ibuf))
  6814. ((eq org-indirect-buffer-display 'other-window)
  6815. (pop-to-buffer ibuf))
  6816. (t (error "Invalid value")))
  6817. (if (featurep 'xemacs)
  6818. (save-excursion (org-mode) (turn-on-font-lock)))
  6819. (narrow-to-region beg end)
  6820. (show-all)
  6821. (goto-char pos)
  6822. (run-hook-with-args 'org-cycle-hook 'all)
  6823. (and (window-live-p cwin) (select-window cwin))))
  6824. (defun org-get-indirect-buffer (&optional buffer heading)
  6825. (setq buffer (or buffer (current-buffer)))
  6826. (let ((n 1) (base (buffer-name buffer)) bname)
  6827. (while (buffer-live-p
  6828. (get-buffer
  6829. (setq bname
  6830. (concat base "-"
  6831. (if heading (concat heading "-" (number-to-string n))
  6832. (number-to-string n))))))
  6833. (setq n (1+ n)))
  6834. (condition-case nil
  6835. (make-indirect-buffer buffer bname 'clone)
  6836. (error (make-indirect-buffer buffer bname)))))
  6837. (defun org-set-frame-title (title)
  6838. "Set the title of the current frame to the string TITLE."
  6839. ;; FIXME: how to name a single frame in XEmacs???
  6840. (unless (featurep 'xemacs)
  6841. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6842. ;;;; Structure editing
  6843. ;;; Inserting headlines
  6844. (defun org-previous-line-empty-p (&optional next)
  6845. "Is the previous line a blank line?
  6846. When NEXT is non-nil, check the next line instead."
  6847. (save-excursion
  6848. (and (not (bobp))
  6849. (or (beginning-of-line (if next 2 0)) t)
  6850. (save-match-data
  6851. (looking-at "[ \t]*$")))))
  6852. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6853. "Insert a new heading or an item with the same depth at point.
  6854. If point is at the beginning of a heading or a list item, insert
  6855. a new heading or a new item above the current one. If point is
  6856. at the beginning of a normal line, turn the line into a heading.
  6857. If point is in the middle of a headline or a list item, split the
  6858. headline or the item and create a new headline/item with the text
  6859. in the current line after point \(see `org-M-RET-may-split-line'
  6860. on how to modify this behavior).
  6861. With one universal prefix argument, set the user option
  6862. `org-insert-heading-respect-content' to t for the duration of
  6863. the command. This modifies the behavior described above in this
  6864. ways: on list items and at the beginning of normal lines, force
  6865. the insertion of a heading after the current subtree.
  6866. With two universal prefix arguments, insert the heading at the
  6867. end of the grandparent subtree. For example, if point is within
  6868. a 2nd-level heading, then it will insert a 2nd-level heading at
  6869. the end of the 1st-level parent heading.
  6870. If point is at the beginning of a headline, insert a sibling
  6871. before the current headline. If point is not at the beginning,
  6872. split the line and create a new headline with the text in the
  6873. current line after point \(see `org-M-RET-may-split-line' on how
  6874. to modify this behavior).
  6875. If point is at the beginning of a normal line, turn this line
  6876. into a heading.
  6877. When INVISIBLE-OK is set, stop at invisible headlines when going
  6878. back. This is important for non-interactive uses of the
  6879. command.
  6880. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6881. heading, unconditionally."
  6882. (interactive "P")
  6883. (if (org-called-interactively-p 'any) (org-reveal))
  6884. (let ((itemp (and (not top-level) (org-in-item-p)))
  6885. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6886. (respect-content (or org-insert-heading-respect-content
  6887. (equal arg '(4))))
  6888. (initial-content ""))
  6889. (cond
  6890. ((or (= (buffer-size) 0)
  6891. (and (not (save-excursion
  6892. (and (ignore-errors (org-back-to-heading invisible-ok))
  6893. (org-at-heading-p))))
  6894. (or arg (not itemp))))
  6895. ;; At beginning of buffer or so high up that only a heading
  6896. ;; makes sense.
  6897. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6898. ((not respect-content)
  6899. (unless may-split (end-of-line))
  6900. (insert "\n* "))
  6901. ((re-search-forward org-outline-regexp-bol nil t)
  6902. (beginning-of-line)
  6903. (insert "* \n")
  6904. (backward-char))
  6905. (t (goto-char (point-max))
  6906. (insert "\n* ")))
  6907. (run-hooks 'org-insert-heading-hook))
  6908. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6909. (t
  6910. ;; Maybe move at the end of the subtree
  6911. (when (equal arg '(16))
  6912. (org-up-heading-safe)
  6913. (org-end-of-subtree t))
  6914. ;; Insert a heading
  6915. (save-restriction
  6916. (widen)
  6917. (let* ((level nil)
  6918. (on-heading (org-at-heading-p))
  6919. (empty-line-p (if on-heading
  6920. (org-previous-line-empty-p)
  6921. ;; We will decide later
  6922. nil))
  6923. ;; Get a level string to fall back on.
  6924. (fix-level
  6925. (if (org-before-first-heading-p) "*"
  6926. (save-excursion
  6927. (org-back-to-heading t)
  6928. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6929. (looking-at org-outline-regexp)
  6930. (make-string (1- (length (match-string 0))) ?*))))
  6931. (stars
  6932. (save-excursion
  6933. (condition-case nil
  6934. (if top-level "* "
  6935. (org-back-to-heading invisible-ok)
  6936. (when (and (not on-heading)
  6937. (featurep 'org-inlinetask)
  6938. (integerp org-inlinetask-min-level)
  6939. (>= (length (match-string 0))
  6940. org-inlinetask-min-level))
  6941. ;; Find a heading level before the inline
  6942. ;; task.
  6943. (while (and (setq level (org-up-heading-safe))
  6944. (>= level org-inlinetask-min-level)))
  6945. (if (org-at-heading-p)
  6946. (org-back-to-heading invisible-ok)
  6947. (error "This should not happen")))
  6948. (unless (and (save-excursion
  6949. (save-match-data
  6950. (org-backward-heading-same-level
  6951. 1 invisible-ok))
  6952. (= (point) (match-beginning 0)))
  6953. (not (org-previous-line-empty-p t)))
  6954. (setq empty-line-p (or empty-line-p
  6955. (org-previous-line-empty-p))))
  6956. (match-string 0))
  6957. (error (or fix-level "* ")))))
  6958. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6959. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6960. pos hide-previous previous-pos)
  6961. ;; If we insert after content, move there and clean up
  6962. ;; whitespace.
  6963. (when (and respect-content
  6964. (not (org-looking-at-p org-outline-regexp-bol)))
  6965. (if (not (org-before-first-heading-p))
  6966. (org-end-of-subtree nil t)
  6967. (re-search-forward org-outline-regexp-bol)
  6968. (beginning-of-line 0))
  6969. (skip-chars-backward " \r\t\n")
  6970. (and (not (looking-back "^\\*+"))
  6971. (looking-at "[ \t]+") (replace-match ""))
  6972. (unless (eobp) (forward-char 1))
  6973. (when (looking-at "^\\*")
  6974. (unless (bobp) (backward-char 1))
  6975. (insert "\n")))
  6976. ;; If we are splitting, grab the text that should be moved
  6977. ;; to the new headline.
  6978. (when may-split
  6979. (if (org-on-heading-p)
  6980. ;; This is a heading: split intelligently (keeping
  6981. ;; tags).
  6982. (let ((pos (point)))
  6983. (beginning-of-line)
  6984. (unless (looking-at org-complex-heading-regexp)
  6985. (error "This should not happen"))
  6986. (when (and (match-beginning 4)
  6987. (> pos (match-beginning 4))
  6988. (< pos (match-end 4)))
  6989. (setq initial-content (buffer-substring pos (match-end 4)))
  6990. (goto-char pos)
  6991. (delete-region (point) (match-end 4))
  6992. (if (looking-at "[ \t]*$")
  6993. (replace-match "")
  6994. (insert (make-string (length initial-content) ?\s)))
  6995. (setq initial-content (org-trim initial-content)))
  6996. (goto-char pos))
  6997. ;; A normal line.
  6998. (setq initial-content
  6999. (org-trim
  7000. (delete-and-extract-region (point) (line-end-position))))))
  7001. ;; If we are at the beginning of the line, insert before it.
  7002. ;; Otherwise, after it.
  7003. (cond
  7004. ((and (bolp) (looking-at "[ \t]*$")))
  7005. ((bolp) (save-excursion (insert "\n")))
  7006. (t (end-of-line)
  7007. (insert "\n")))
  7008. ;; Insert the new heading
  7009. (insert stars)
  7010. (just-one-space)
  7011. (insert initial-content)
  7012. (unless (and blank (org-previous-line-empty-p))
  7013. (org-N-empty-lines-before-current (if blank 1 0)))
  7014. ;; Adjust visibility, which may be messed up if we removed
  7015. ;; blank lines while previous entry was hidden.
  7016. (let ((bol (line-beginning-position)))
  7017. (dolist (o (overlays-at (1- bol)))
  7018. (when (and (eq (overlay-get o 'invisible) 'outline)
  7019. (eq (overlay-end o) bol))
  7020. (move-overlay o (overlay-start o) (1- bol)))))
  7021. (run-hooks 'org-insert-heading-hook)))))))
  7022. (defun org-N-empty-lines-before-current (N)
  7023. "Make the number of empty lines before current exactly N.
  7024. So this will delete or add empty lines."
  7025. (save-excursion
  7026. (beginning-of-line)
  7027. (let ((p (point)))
  7028. (skip-chars-backward " \r\t\n")
  7029. (unless (bolp) (forward-line))
  7030. (delete-region (point) p))
  7031. (when (> N 0) (insert (make-string N ?\n)))))
  7032. (defun org-get-heading (&optional no-tags no-todo)
  7033. "Return the heading of the current entry, without the stars.
  7034. When NO-TAGS is non-nil, don't include tags.
  7035. When NO-TODO is non-nil, don't include TODO keywords."
  7036. (save-excursion
  7037. (org-back-to-heading t)
  7038. (cond
  7039. ((and no-tags no-todo)
  7040. (looking-at org-complex-heading-regexp)
  7041. (match-string 4))
  7042. (no-tags
  7043. (looking-at (concat org-outline-regexp
  7044. "\\(.*?\\)"
  7045. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7046. (match-string 1))
  7047. (no-todo
  7048. (looking-at org-todo-line-regexp)
  7049. (match-string 3))
  7050. (t (looking-at org-heading-regexp)
  7051. (match-string 2)))))
  7052. (defvar orgstruct-mode) ; defined below
  7053. (defun org-heading-components ()
  7054. "Return the components of the current heading.
  7055. This is a list with the following elements:
  7056. - the level as an integer
  7057. - the reduced level, different if `org-odd-levels-only' is set.
  7058. - the TODO keyword, or nil
  7059. - the priority character, like ?A, or nil if no priority is given
  7060. - the headline text itself, or the tags string if no headline text
  7061. - the tags string, or nil."
  7062. (save-excursion
  7063. (org-back-to-heading t)
  7064. (if (let (case-fold-search)
  7065. (looking-at
  7066. (if orgstruct-mode
  7067. org-heading-regexp
  7068. org-complex-heading-regexp)))
  7069. (if orgstruct-mode
  7070. (list (length (match-string 1))
  7071. (org-reduced-level (length (match-string 1)))
  7072. nil
  7073. nil
  7074. (match-string 2)
  7075. nil)
  7076. (list (length (match-string 1))
  7077. (org-reduced-level (length (match-string 1)))
  7078. (org-match-string-no-properties 2)
  7079. (and (match-end 3) (aref (match-string 3) 2))
  7080. (org-match-string-no-properties 4)
  7081. (org-match-string-no-properties 5))))))
  7082. (defun org-get-entry ()
  7083. "Get the entry text, after heading, entire subtree."
  7084. (save-excursion
  7085. (org-back-to-heading t)
  7086. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7087. (defun org-insert-heading-after-current ()
  7088. "Insert a new heading with same level as current, after current subtree."
  7089. (interactive)
  7090. (org-back-to-heading)
  7091. (org-insert-heading)
  7092. (org-move-subtree-down)
  7093. (end-of-line 1))
  7094. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7095. "Insert heading with `org-insert-heading-respect-content' set to t."
  7096. (interactive)
  7097. (org-insert-heading '(4) invisible-ok))
  7098. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7099. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7100. (interactive)
  7101. (org-insert-todo-heading force-state '(4)))
  7102. (defun org-insert-todo-heading (arg &optional force-heading)
  7103. "Insert a new heading with the same level and TODO state as current heading.
  7104. If the heading has no TODO state, or if the state is DONE, use the first
  7105. state (TODO by default). Also with one prefix arg, force first state. With
  7106. two prefix args, force inserting at the end of the parent subtree."
  7107. (interactive "P")
  7108. (when (or force-heading (not (org-insert-item 'checkbox)))
  7109. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7110. force-heading))
  7111. (save-excursion
  7112. (org-back-to-heading)
  7113. (outline-previous-heading)
  7114. (looking-at org-todo-line-regexp))
  7115. (let*
  7116. ((new-mark-x
  7117. (if (or (equal arg '(4))
  7118. (not (match-beginning 2))
  7119. (member (match-string 2) org-done-keywords))
  7120. (car org-todo-keywords-1)
  7121. (match-string 2)))
  7122. (new-mark
  7123. (or
  7124. (run-hook-with-args-until-success
  7125. 'org-todo-get-default-hook new-mark-x nil)
  7126. new-mark-x)))
  7127. (beginning-of-line 1)
  7128. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7129. (if org-treat-insert-todo-heading-as-state-change
  7130. (org-todo new-mark)
  7131. (insert new-mark " "))))
  7132. (when org-provide-todo-statistics
  7133. (org-update-parent-todo-statistics))))
  7134. (defun org-insert-subheading (arg)
  7135. "Insert a new subheading and demote it.
  7136. Works for outline headings and for plain lists alike."
  7137. (interactive "P")
  7138. (org-insert-heading arg)
  7139. (cond
  7140. ((org-at-heading-p) (org-do-demote))
  7141. ((org-at-item-p) (org-indent-item))))
  7142. (defun org-insert-todo-subheading (arg)
  7143. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7144. Works for outline headings and for plain lists alike."
  7145. (interactive "P")
  7146. (org-insert-todo-heading arg)
  7147. (cond
  7148. ((org-at-heading-p) (org-do-demote))
  7149. ((org-at-item-p) (org-indent-item))))
  7150. ;;; Promotion and Demotion
  7151. (defvar org-after-demote-entry-hook nil
  7152. "Hook run after an entry has been demoted.
  7153. The cursor will be at the beginning of the entry.
  7154. When a subtree is being demoted, the hook will be called for each node.")
  7155. (defvar org-after-promote-entry-hook nil
  7156. "Hook run after an entry has been promoted.
  7157. The cursor will be at the beginning of the entry.
  7158. When a subtree is being promoted, the hook will be called for each node.")
  7159. (defun org-promote-subtree ()
  7160. "Promote the entire subtree.
  7161. See also `org-promote'."
  7162. (interactive)
  7163. (save-excursion
  7164. (org-with-limited-levels (org-map-tree 'org-promote)))
  7165. (org-fix-position-after-promote))
  7166. (defun org-demote-subtree ()
  7167. "Demote the entire subtree. See `org-demote'.
  7168. See also `org-promote'."
  7169. (interactive)
  7170. (save-excursion
  7171. (org-with-limited-levels (org-map-tree 'org-demote)))
  7172. (org-fix-position-after-promote))
  7173. (defun org-do-promote ()
  7174. "Promote the current heading higher up the tree.
  7175. If the region is active in `transient-mark-mode', promote all headings
  7176. in the region."
  7177. (interactive)
  7178. (save-excursion
  7179. (if (org-region-active-p)
  7180. (org-map-region 'org-promote (region-beginning) (region-end))
  7181. (org-promote)))
  7182. (org-fix-position-after-promote))
  7183. (defun org-do-demote ()
  7184. "Demote the current heading lower down the tree.
  7185. If the region is active in `transient-mark-mode', demote all headings
  7186. in the region."
  7187. (interactive)
  7188. (save-excursion
  7189. (if (org-region-active-p)
  7190. (org-map-region 'org-demote (region-beginning) (region-end))
  7191. (org-demote)))
  7192. (org-fix-position-after-promote))
  7193. (defun org-fix-position-after-promote ()
  7194. "Make sure that after pro/demotion cursor position is right."
  7195. (let ((pos (point)))
  7196. (when (save-excursion
  7197. (beginning-of-line 1)
  7198. (looking-at org-todo-line-regexp)
  7199. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7200. (cond ((eobp) (insert " "))
  7201. ((eolp) (insert " "))
  7202. ((equal (char-after) ?\ ) (forward-char 1))))))
  7203. (defun org-current-level ()
  7204. "Return the level of the current entry, or nil if before the first headline.
  7205. The level is the number of stars at the beginning of the
  7206. headline. Use `org-reduced-level' to remove the effect of
  7207. `org-odd-levels'. Unlike to `org-outline-level', this function
  7208. ignores inlinetasks."
  7209. (let ((level (org-with-limited-levels (org-outline-level))))
  7210. (and (> level 0) level)))
  7211. (defun org-get-previous-line-level ()
  7212. "Return the outline depth of the last headline before the current line.
  7213. Returns 0 for the first headline in the buffer, and nil if before the
  7214. first headline."
  7215. (and (org-current-level)
  7216. (or (and (/= (line-beginning-position) (point-min))
  7217. (save-excursion (beginning-of-line 0) (org-current-level)))
  7218. 0)))
  7219. (defun org-reduced-level (l)
  7220. "Compute the effective level of a heading.
  7221. This takes into account the setting of `org-odd-levels-only'."
  7222. (cond
  7223. ((zerop l) 0)
  7224. (org-odd-levels-only (1+ (floor (/ l 2))))
  7225. (t l)))
  7226. (defun org-level-increment ()
  7227. "Return the number of stars that will be added or removed at a
  7228. time to headlines when structure editing, based on the value of
  7229. `org-odd-levels-only'."
  7230. (if org-odd-levels-only 2 1))
  7231. (defun org-get-valid-level (level &optional change)
  7232. "Rectify a level change under the influence of `org-odd-levels-only'
  7233. LEVEL is a current level, CHANGE is by how much the level should be
  7234. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7235. even level numbers will become the next higher odd number."
  7236. (if org-odd-levels-only
  7237. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7238. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7239. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7240. (max 1 (+ level (or change 0)))))
  7241. (if (boundp 'define-obsolete-function-alias)
  7242. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7243. (define-obsolete-function-alias 'org-get-legal-level
  7244. 'org-get-valid-level)
  7245. (define-obsolete-function-alias 'org-get-legal-level
  7246. 'org-get-valid-level "23.1")))
  7247. (defun org-promote ()
  7248. "Promote the current heading higher up the tree."
  7249. (org-with-wide-buffer
  7250. (org-back-to-heading t)
  7251. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7252. after-change-functions))
  7253. (level (save-match-data (funcall outline-level)))
  7254. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7255. (diff (abs (- level (length up-head) -1))))
  7256. (cond
  7257. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7258. (replace-match "# " nil t))
  7259. ((= level 1)
  7260. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7261. (t (replace-match up-head nil t)))
  7262. (unless (= level 1)
  7263. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7264. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7265. (run-hooks 'org-after-promote-entry-hook))))
  7266. (defun org-demote ()
  7267. "Demote the current heading lower down the tree."
  7268. (org-with-wide-buffer
  7269. (org-back-to-heading t)
  7270. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7271. after-change-functions))
  7272. (level (save-match-data (funcall outline-level)))
  7273. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7274. (diff (abs (- level (length down-head) -1))))
  7275. (replace-match down-head nil t)
  7276. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7277. (when org-adapt-indentation (org-fixup-indentation diff))
  7278. (run-hooks 'org-after-demote-entry-hook))))
  7279. (defun org-cycle-level ()
  7280. "Cycle the level of an empty headline through possible states.
  7281. This goes first to child, then to parent, level, then up the hierarchy.
  7282. After top level, it switches back to sibling level."
  7283. (interactive)
  7284. (let ((org-adapt-indentation nil))
  7285. (when (org-point-at-end-of-empty-headline)
  7286. (setq this-command 'org-cycle-level) ; Only needed for caching
  7287. (let ((cur-level (org-current-level))
  7288. (prev-level (org-get-previous-line-level)))
  7289. (cond
  7290. ;; If first headline in file, promote to top-level.
  7291. ((= prev-level 0)
  7292. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7293. do (org-do-promote)))
  7294. ;; If same level as prev, demote one.
  7295. ((= prev-level cur-level)
  7296. (org-do-demote))
  7297. ;; If parent is top-level, promote to top level if not already.
  7298. ((= prev-level 1)
  7299. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7300. do (org-do-promote)))
  7301. ;; If top-level, return to prev-level.
  7302. ((= cur-level 1)
  7303. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7304. do (org-do-demote)))
  7305. ;; If less than prev-level, promote one.
  7306. ((< cur-level prev-level)
  7307. (org-do-promote))
  7308. ;; If deeper than prev-level, promote until higher than
  7309. ;; prev-level.
  7310. ((> cur-level prev-level)
  7311. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7312. do (org-do-promote))))
  7313. t))))
  7314. (defun org-map-tree (fun)
  7315. "Call FUN for every heading underneath the current one."
  7316. (org-back-to-heading)
  7317. (let ((level (funcall outline-level)))
  7318. (save-excursion
  7319. (funcall fun)
  7320. (while (and (progn
  7321. (outline-next-heading)
  7322. (> (funcall outline-level) level))
  7323. (not (eobp)))
  7324. (funcall fun)))))
  7325. (defun org-map-region (fun beg end)
  7326. "Call FUN for every heading between BEG and END."
  7327. (let ((org-ignore-region t))
  7328. (save-excursion
  7329. (setq end (copy-marker end))
  7330. (goto-char beg)
  7331. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7332. (< (point) end))
  7333. (funcall fun))
  7334. (while (and (progn
  7335. (outline-next-heading)
  7336. (< (point) end))
  7337. (not (eobp)))
  7338. (funcall fun)))))
  7339. (defun org-fixup-indentation (diff)
  7340. "Change the indentation in the current entry by DIFF.
  7341. DIFF is an integer. Indentation is done according to the
  7342. following rules:
  7343. - Planning information and property drawers are always indented
  7344. according to the new level of the headline;
  7345. - Footnote definitions and their contents are ignored;
  7346. - Inlinetasks' boundaries are not shifted;
  7347. - Empty lines are ignored;
  7348. - Other lines' indentation are shifted by DIFF columns, unless
  7349. it would introduce a structural change in the document, in
  7350. which case no shifting is done at all.
  7351. Assume point is at a heading or an inlinetask beginning."
  7352. (org-with-wide-buffer
  7353. (narrow-to-region (line-beginning-position)
  7354. (save-excursion
  7355. (if (org-with-limited-levels (org-at-heading-p))
  7356. (org-with-limited-levels (outline-next-heading))
  7357. (org-inlinetask-goto-end))
  7358. (point)))
  7359. (forward-line)
  7360. ;; Indent properly planning info and property drawer.
  7361. (when (org-looking-at-p org-planning-line-re)
  7362. (org-indent-line)
  7363. (forward-line))
  7364. (when (looking-at org-property-drawer-re)
  7365. (goto-char (match-end 0))
  7366. (forward-line)
  7367. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7368. (catch 'no-shift
  7369. (when (zerop diff) (throw 'no-shift nil))
  7370. ;; If DIFF is negative, first check if a shift is possible at all
  7371. ;; (e.g., it doesn't break structure). This can only happen if
  7372. ;; some contents are not properly indented.
  7373. (let ((case-fold-search t))
  7374. (when (< diff 0)
  7375. (let ((diff (- diff))
  7376. (forbidden-re (concat org-outline-regexp
  7377. "\\|"
  7378. (substring org-footnote-definition-re 1))))
  7379. (save-excursion
  7380. (while (not (eobp))
  7381. (cond
  7382. ((org-looking-at-p "[ \t]*$") (forward-line))
  7383. ((and (org-looking-at-p org-footnote-definition-re)
  7384. (let ((e (org-element-at-point)))
  7385. (and (eq (org-element-type e) 'footnote-definition)
  7386. (goto-char (org-element-property :end e))))))
  7387. ((org-looking-at-p org-outline-regexp) (forward-line))
  7388. ;; Give up if shifting would move before column 0 or
  7389. ;; if it would introduce a headline or a footnote
  7390. ;; definition.
  7391. (t
  7392. (skip-chars-forward " \t")
  7393. (let ((ind (current-column)))
  7394. (when (or (< ind diff)
  7395. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7396. (throw 'no-shift nil)))
  7397. ;; Ignore contents of example blocks and source
  7398. ;; blocks if their indentation is meant to be
  7399. ;; preserved. Jump to block's closing line.
  7400. (beginning-of-line)
  7401. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7402. (let ((e (org-element-at-point)))
  7403. (and (memq (org-element-type e)
  7404. '(example-block src-block))
  7405. (or org-src-preserve-indentation
  7406. (org-element-property :preserve-indent e))
  7407. (goto-char (org-element-property :end e))
  7408. (progn (skip-chars-backward " \r\t\n")
  7409. (beginning-of-line)
  7410. t))))
  7411. (forward-line))))))))
  7412. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7413. ;; by DIFF. Also skip contents of source or example blocks
  7414. ;; when indentation is meant to be preserved.
  7415. (while (not (eobp))
  7416. (cond
  7417. ((and (org-looking-at-p org-footnote-definition-re)
  7418. (let ((e (org-element-at-point)))
  7419. (and (eq (org-element-type e) 'footnote-definition)
  7420. (goto-char (org-element-property :end e))))))
  7421. ((org-looking-at-p org-outline-regexp) (forward-line))
  7422. ((org-looking-at-p "[ \t]*$") (forward-line))
  7423. (t
  7424. (org-indent-line-to (+ (org-get-indentation) diff))
  7425. (beginning-of-line)
  7426. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7427. (let ((e (org-element-at-point)))
  7428. (and (memq (org-element-type e)
  7429. '(example-block src-block))
  7430. (or org-src-preserve-indentation
  7431. (org-element-property :preserve-indent e))
  7432. (goto-char (org-element-property :end e))
  7433. (progn (skip-chars-backward " \r\t\n")
  7434. (beginning-of-line)
  7435. t))))
  7436. (forward-line)))))))))
  7437. (defun org-convert-to-odd-levels ()
  7438. "Convert an org-mode file with all levels allowed to one with odd levels.
  7439. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7440. level 5 etc."
  7441. (interactive)
  7442. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7443. (let ((outline-level 'org-outline-level)
  7444. (org-odd-levels-only nil) n)
  7445. (save-excursion
  7446. (goto-char (point-min))
  7447. (while (re-search-forward "^\\*\\*+ " nil t)
  7448. (setq n (- (length (match-string 0)) 2))
  7449. (while (>= (setq n (1- n)) 0)
  7450. (org-demote))
  7451. (end-of-line 1))))))
  7452. (defun org-convert-to-oddeven-levels ()
  7453. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7454. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7455. file contains a section with an even level, conversion would
  7456. destroy the structure of the file. An error is signaled in this
  7457. case."
  7458. (interactive)
  7459. (goto-char (point-min))
  7460. ;; First check if there are no even levels
  7461. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7462. (org-show-set-visibility 'canonical)
  7463. (error "Not all levels are odd in this file. Conversion not possible"))
  7464. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7465. (let ((outline-regexp org-outline-regexp)
  7466. (outline-level 'org-outline-level)
  7467. (org-odd-levels-only nil) n)
  7468. (save-excursion
  7469. (goto-char (point-min))
  7470. (while (re-search-forward "^\\*\\*+ " nil t)
  7471. (setq n (/ (1- (length (match-string 0))) 2))
  7472. (while (>= (setq n (1- n)) 0)
  7473. (org-promote))
  7474. (end-of-line 1))))))
  7475. (defun org-tr-level (n)
  7476. "Make N odd if required."
  7477. (if org-odd-levels-only (1+ (/ n 2)) n))
  7478. ;;; Vertical tree motion, cutting and pasting of subtrees
  7479. (defun org-move-subtree-up (&optional arg)
  7480. "Move the current subtree up past ARG headlines of the same level."
  7481. (interactive "p")
  7482. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7483. (defun org-move-subtree-down (&optional arg)
  7484. "Move the current subtree down past ARG headlines of the same level."
  7485. (interactive "p")
  7486. (setq arg (prefix-numeric-value arg))
  7487. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7488. 'org-get-last-sibling))
  7489. (ins-point (make-marker))
  7490. (cnt (abs arg))
  7491. (col (current-column))
  7492. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7493. ;; Select the tree
  7494. (org-back-to-heading)
  7495. (setq beg0 (point))
  7496. (save-excursion
  7497. (setq ne-beg (org-back-over-empty-lines))
  7498. (setq beg (point)))
  7499. (save-match-data
  7500. (save-excursion (outline-end-of-heading)
  7501. (setq folded (outline-invisible-p)))
  7502. (progn (org-end-of-subtree nil t)
  7503. (unless (eobp) (backward-char))))
  7504. (outline-next-heading)
  7505. (setq ne-end (org-back-over-empty-lines))
  7506. (setq end (point))
  7507. (goto-char beg0)
  7508. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7509. ;; include less whitespace
  7510. (save-excursion
  7511. (goto-char beg)
  7512. (forward-line (- ne-beg ne-end))
  7513. (setq beg (point))))
  7514. ;; Find insertion point, with error handling
  7515. (while (> cnt 0)
  7516. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7517. (progn (goto-char beg0)
  7518. (user-error "Cannot move past superior level or buffer limit")))
  7519. (setq cnt (1- cnt)))
  7520. (if (> arg 0)
  7521. ;; Moving forward - still need to move over subtree
  7522. (progn (org-end-of-subtree t t)
  7523. (save-excursion
  7524. (org-back-over-empty-lines)
  7525. (or (bolp) (newline)))))
  7526. (setq ne-ins (org-back-over-empty-lines))
  7527. (move-marker ins-point (point))
  7528. (setq txt (buffer-substring beg end))
  7529. (org-save-markers-in-region beg end)
  7530. (delete-region beg end)
  7531. (org-remove-empty-overlays-at beg)
  7532. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7533. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7534. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7535. (let ((bbb (point)))
  7536. (insert-before-markers txt)
  7537. (org-reinstall-markers-in-region bbb)
  7538. (move-marker ins-point bbb))
  7539. (or (bolp) (insert "\n"))
  7540. (setq ins-end (point))
  7541. (goto-char ins-point)
  7542. (org-skip-whitespace)
  7543. (when (and (< arg 0)
  7544. (org-first-sibling-p)
  7545. (> ne-ins ne-beg))
  7546. ;; Move whitespace back to beginning
  7547. (save-excursion
  7548. (goto-char ins-end)
  7549. (let ((kill-whole-line t))
  7550. (kill-line (- ne-ins ne-beg)) (point)))
  7551. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7552. (move-marker ins-point nil)
  7553. (if folded
  7554. (hide-subtree)
  7555. (org-show-entry)
  7556. (show-children)
  7557. (org-cycle-hide-drawers 'children))
  7558. (org-clean-visibility-after-subtree-move)
  7559. ;; move back to the initial column we were at
  7560. (move-to-column col)))
  7561. (defvar org-subtree-clip ""
  7562. "Clipboard for cut and paste of subtrees.
  7563. This is actually only a copy of the kill, because we use the normal kill
  7564. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7565. (defvar org-subtree-clip-folded nil
  7566. "Was the last copied subtree folded?
  7567. This is used to fold the tree back after pasting.")
  7568. (defun org-cut-subtree (&optional n)
  7569. "Cut the current subtree into the clipboard.
  7570. With prefix arg N, cut this many sequential subtrees.
  7571. This is a short-hand for marking the subtree and then cutting it."
  7572. (interactive "p")
  7573. (org-copy-subtree n 'cut))
  7574. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7575. "Copy the current subtree it in the clipboard.
  7576. With prefix arg N, copy this many sequential subtrees.
  7577. This is a short-hand for marking the subtree and then copying it.
  7578. If CUT is non-nil, actually cut the subtree.
  7579. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7580. of some markers in the region, even if CUT is non-nil. This is
  7581. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7582. (interactive "p")
  7583. (let (beg end folded (beg0 (point)))
  7584. (if (org-called-interactively-p 'any)
  7585. (org-back-to-heading nil) ; take what looks like a subtree
  7586. (org-back-to-heading t)) ; take what is really there
  7587. (setq beg (point))
  7588. (skip-chars-forward " \t\r\n")
  7589. (save-match-data
  7590. (if nosubtrees
  7591. (outline-next-heading)
  7592. (save-excursion (outline-end-of-heading)
  7593. (setq folded (outline-invisible-p)))
  7594. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7595. (org-end-of-subtree t t)))
  7596. ;; Include the end of an inlinetask
  7597. (when (and (featurep 'org-inlinetask)
  7598. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7599. "END[ \t]*$")))
  7600. (end-of-line))
  7601. (setq end (point))
  7602. (goto-char beg0)
  7603. (when (> end beg)
  7604. (setq org-subtree-clip-folded folded)
  7605. (when (or cut force-store-markers)
  7606. (org-save-markers-in-region beg end))
  7607. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7608. (setq org-subtree-clip (current-kill 0))
  7609. (message "%s: Subtree(s) with %d characters"
  7610. (if cut "Cut" "Copied")
  7611. (length org-subtree-clip)))))
  7612. (defun org-paste-subtree (&optional level tree for-yank remove)
  7613. "Paste the clipboard as a subtree, with modification of headline level.
  7614. The entire subtree is promoted or demoted in order to match a new headline
  7615. level.
  7616. If the cursor is at the beginning of a headline, the same level as
  7617. that headline is used to paste the tree.
  7618. If not, the new level is derived from the *visible* headings
  7619. before and after the insertion point, and taken to be the inferior headline
  7620. level of the two. So if the previous visible heading is level 3 and the
  7621. next is level 4 (or vice versa), level 4 will be used for insertion.
  7622. This makes sure that the subtree remains an independent subtree and does
  7623. not swallow low level entries.
  7624. You can also force a different level, either by using a numeric prefix
  7625. argument, or by inserting the heading marker by hand. For example, if the
  7626. cursor is after \"*****\", then the tree will be shifted to level 5.
  7627. If optional TREE is given, use this text instead of the kill ring.
  7628. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7629. move back over whitespace before inserting, and move point to the end of
  7630. the inserted text when done.
  7631. When REMOVE is non-nil, remove the subtree from the clipboard."
  7632. (interactive "P")
  7633. (setq tree (or tree (and kill-ring (current-kill 0))))
  7634. (unless (org-kill-is-subtree-p tree)
  7635. (user-error "%s"
  7636. (substitute-command-keys
  7637. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7638. (org-with-limited-levels
  7639. (let* ((visp (not (outline-invisible-p)))
  7640. (txt tree)
  7641. (^re_ "\\(\\*+\\)[ \t]*")
  7642. (old-level (if (string-match org-outline-regexp-bol txt)
  7643. (- (match-end 0) (match-beginning 0) 1)
  7644. -1))
  7645. (force-level (cond (level (prefix-numeric-value level))
  7646. ((and (looking-at "[ \t]*$")
  7647. (string-match
  7648. "^\\*+$" (buffer-substring
  7649. (point-at-bol) (point))))
  7650. (- (match-end 0) (match-beginning 0)))
  7651. ((and (bolp)
  7652. (looking-at org-outline-regexp))
  7653. (- (match-end 0) (point) 1))))
  7654. (previous-level (save-excursion
  7655. (condition-case nil
  7656. (progn
  7657. (outline-previous-visible-heading 1)
  7658. (if (looking-at ^re_)
  7659. (- (match-end 0) (match-beginning 0) 1)
  7660. 1))
  7661. (error 1))))
  7662. (next-level (save-excursion
  7663. (condition-case nil
  7664. (progn
  7665. (or (looking-at org-outline-regexp)
  7666. (outline-next-visible-heading 1))
  7667. (if (looking-at ^re_)
  7668. (- (match-end 0) (match-beginning 0) 1)
  7669. 1))
  7670. (error 1))))
  7671. (new-level (or force-level (max previous-level next-level)))
  7672. (shift (if (or (= old-level -1)
  7673. (= new-level -1)
  7674. (= old-level new-level))
  7675. 0
  7676. (- new-level old-level)))
  7677. (delta (if (> shift 0) -1 1))
  7678. (func (if (> shift 0) 'org-demote 'org-promote))
  7679. (org-odd-levels-only nil)
  7680. beg end newend)
  7681. ;; Remove the forced level indicator
  7682. (if force-level
  7683. (delete-region (point-at-bol) (point)))
  7684. ;; Paste
  7685. (beginning-of-line (if (bolp) 1 2))
  7686. (setq beg (point))
  7687. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7688. (insert-before-markers txt)
  7689. (unless (string-match "\n\\'" txt) (insert "\n"))
  7690. (setq newend (point))
  7691. (org-reinstall-markers-in-region beg)
  7692. (setq end (point))
  7693. (goto-char beg)
  7694. (skip-chars-forward " \t\n\r")
  7695. (setq beg (point))
  7696. (if (and (outline-invisible-p) visp)
  7697. (save-excursion (outline-show-heading)))
  7698. ;; Shift if necessary
  7699. (unless (= shift 0)
  7700. (save-restriction
  7701. (narrow-to-region beg end)
  7702. (while (not (= shift 0))
  7703. (org-map-region func (point-min) (point-max))
  7704. (setq shift (+ delta shift)))
  7705. (goto-char (point-min))
  7706. (setq newend (point-max))))
  7707. (when (or (org-called-interactively-p 'interactive) for-yank)
  7708. (message "Clipboard pasted as level %d subtree" new-level))
  7709. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7710. kill-ring
  7711. (eq org-subtree-clip (current-kill 0))
  7712. org-subtree-clip-folded)
  7713. ;; The tree was folded before it was killed/copied
  7714. (hide-subtree))
  7715. (and for-yank (goto-char newend))
  7716. (and remove (setq kill-ring (cdr kill-ring))))))
  7717. (defun org-kill-is-subtree-p (&optional txt)
  7718. "Check if the current kill is an outline subtree, or a set of trees.
  7719. Returns nil if kill does not start with a headline, or if the first
  7720. headline level is not the largest headline level in the tree.
  7721. So this will actually accept several entries of equal levels as well,
  7722. which is OK for `org-paste-subtree'.
  7723. If optional TXT is given, check this string instead of the current kill."
  7724. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7725. (re (org-get-limited-outline-regexp))
  7726. (^re (concat "^" re))
  7727. (start-level (and kill
  7728. (string-match
  7729. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7730. kill)
  7731. (- (match-end 2) (match-beginning 2) 1)))
  7732. (start (1+ (or (match-beginning 2) -1))))
  7733. (if (not start-level)
  7734. (progn
  7735. nil) ;; does not even start with a heading
  7736. (catch 'exit
  7737. (while (setq start (string-match ^re kill (1+ start)))
  7738. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7739. (throw 'exit nil)))
  7740. t))))
  7741. (defvar org-markers-to-move nil
  7742. "Markers that should be moved with a cut-and-paste operation.
  7743. Those markers are stored together with their positions relative to
  7744. the start of the region.")
  7745. (defun org-save-markers-in-region (beg end)
  7746. "Check markers in region.
  7747. If these markers are between BEG and END, record their position relative
  7748. to BEG, so that after moving the block of text, we can put the markers back
  7749. into place.
  7750. This function gets called just before an entry or tree gets cut from the
  7751. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7752. called immediately, to move the markers with the entries."
  7753. (setq org-markers-to-move nil)
  7754. (when (featurep 'org-clock)
  7755. (org-clock-save-markers-for-cut-and-paste beg end))
  7756. (when (featurep 'org-agenda)
  7757. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7758. (defun org-check-and-save-marker (marker beg end)
  7759. "Check if MARKER is between BEG and END.
  7760. If yes, remember the marker and the distance to BEG."
  7761. (when (and (marker-buffer marker)
  7762. (equal (marker-buffer marker) (current-buffer)))
  7763. (if (and (>= marker beg) (< marker end))
  7764. (push (cons marker (- marker beg)) org-markers-to-move))))
  7765. (defun org-reinstall-markers-in-region (beg)
  7766. "Move all remembered markers to their position relative to BEG."
  7767. (mapc (lambda (x)
  7768. (move-marker (car x) (+ beg (cdr x))))
  7769. org-markers-to-move)
  7770. (setq org-markers-to-move nil))
  7771. (defun org-narrow-to-subtree ()
  7772. "Narrow buffer to the current subtree."
  7773. (interactive)
  7774. (save-excursion
  7775. (save-match-data
  7776. (org-with-limited-levels
  7777. (narrow-to-region
  7778. (progn (org-back-to-heading t) (point))
  7779. (progn (org-end-of-subtree t t)
  7780. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7781. (point)))))))
  7782. (defun org-narrow-to-block ()
  7783. "Narrow buffer to the current block."
  7784. (interactive)
  7785. (let* ((case-fold-search t)
  7786. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7787. "^[ \t]*#\\+end_.*")))
  7788. (if blockp
  7789. (narrow-to-region (car blockp) (cdr blockp))
  7790. (user-error "Not in a block"))))
  7791. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7792. "Clone the task (subtree) at point N times.
  7793. The clones will be inserted as siblings.
  7794. In interactive use, the user will be prompted for the number of
  7795. clones to be produced. If the entry has a timestamp, the user
  7796. will also be prompted for a time shift, which may be a repeater
  7797. as used in time stamps, for example `+3d'. To disable this,
  7798. you can call the function with a universal prefix argument.
  7799. When a valid repeater is given and the entry contains any time
  7800. stamps, the clones will become a sequence in time, with time
  7801. stamps in the subtree shifted for each clone produced. If SHIFT
  7802. is nil or the empty string, time stamps will be left alone. The
  7803. ID property of the original subtree is removed.
  7804. If the original subtree did contain time stamps with a repeater,
  7805. the following will happen:
  7806. - the repeater will be removed in each clone
  7807. - an additional clone will be produced, with the current, unshifted
  7808. date(s) in the entry.
  7809. - the original entry will be placed *after* all the clones, with
  7810. repeater intact.
  7811. - the start days in the repeater in the original entry will be shifted
  7812. to past the last clone.
  7813. In this way you can spell out a number of instances of a repeating task,
  7814. and still retain the repeater to cover future instances of the task."
  7815. (interactive "nNumber of clones to produce: ")
  7816. (let ((shift
  7817. (or shift
  7818. (if (and (not (equal current-prefix-arg '(4)))
  7819. (save-excursion
  7820. (re-search-forward org-ts-regexp-both
  7821. (save-excursion
  7822. (org-end-of-subtree t)
  7823. (point)) t)))
  7824. (read-from-minibuffer
  7825. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7826. ""))) ;; No time shift
  7827. (n-no-remove -1)
  7828. (drawer-re org-drawer-regexp)
  7829. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7830. beg end template task idprop
  7831. shift-n shift-what doshift nmin nmax)
  7832. (if (not (and (integerp n) (> n 0)))
  7833. (user-error "Invalid number of replications %s" n))
  7834. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7835. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7836. shift)))
  7837. (user-error "Invalid shift specification %s" shift))
  7838. (when doshift
  7839. (setq shift-n (string-to-number (match-string 1 shift))
  7840. shift-what (cdr (assoc (match-string 2 shift)
  7841. '(("d" . day) ("w" . week)
  7842. ("m" . month) ("y" . year))))))
  7843. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7844. (setq nmin 1 nmax n)
  7845. (org-back-to-heading t)
  7846. (setq beg (point))
  7847. (setq idprop (org-entry-get nil "ID"))
  7848. (org-end-of-subtree t t)
  7849. (or (bolp) (insert "\n"))
  7850. (setq end (point))
  7851. (setq template (buffer-substring beg end))
  7852. (when (and doshift
  7853. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7854. (delete-region beg end)
  7855. (setq end beg)
  7856. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7857. (goto-char end)
  7858. (loop for n from nmin to nmax do
  7859. ;; prepare clone
  7860. (with-temp-buffer
  7861. (insert template)
  7862. (org-mode)
  7863. (goto-char (point-min))
  7864. (org-show-subtree)
  7865. (and idprop (if org-clone-delete-id
  7866. (org-entry-delete nil "ID")
  7867. (org-id-get-create t)))
  7868. (unless (= n 0)
  7869. (while (re-search-forward org-clock-re nil t)
  7870. (kill-whole-line))
  7871. (goto-char (point-min))
  7872. (while (re-search-forward drawer-re nil t)
  7873. (org-remove-empty-drawer-at (point))))
  7874. (goto-char (point-min))
  7875. (when doshift
  7876. (while (re-search-forward org-ts-regexp-both nil t)
  7877. (org-timestamp-change (* n shift-n) shift-what))
  7878. (unless (= n n-no-remove)
  7879. (goto-char (point-min))
  7880. (while (re-search-forward org-ts-regexp nil t)
  7881. (save-excursion
  7882. (goto-char (match-beginning 0))
  7883. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7884. (delete-region (match-beginning 1) (match-end 1)))))))
  7885. (setq task (buffer-string)))
  7886. (insert task))
  7887. (goto-char beg)))
  7888. ;;; Outline Sorting
  7889. (defun org-sort (with-case)
  7890. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7891. Optional argument WITH-CASE means sort case-sensitively."
  7892. (interactive "P")
  7893. (cond
  7894. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7895. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7896. (t
  7897. (org-call-with-arg 'org-sort-entries with-case))))
  7898. (defun org-sort-remove-invisible (s)
  7899. "Remove invisible links from string S."
  7900. (remove-text-properties 0 (length s) org-rm-props s)
  7901. (while (string-match org-bracket-link-regexp s)
  7902. (setq s (replace-match (if (match-end 2)
  7903. (match-string 3 s)
  7904. (match-string 1 s)) t t s)))
  7905. (let ((st (format " %s " s)))
  7906. (while (string-match org-emph-re st)
  7907. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7908. (setq s (substring st 1 -1)))
  7909. s)
  7910. (defvar org-priority-regexp) ; defined later in the file
  7911. (defvar org-after-sorting-entries-or-items-hook nil
  7912. "Hook that is run after a bunch of entries or items have been sorted.
  7913. When children are sorted, the cursor is in the parent line when this
  7914. hook gets called. When a region or a plain list is sorted, the cursor
  7915. will be in the first entry of the sorted region/list.")
  7916. (defun org-sort-entries
  7917. (&optional with-case sorting-type getkey-func compare-func property)
  7918. "Sort entries on a certain level of an outline tree.
  7919. If there is an active region, the entries in the region are sorted.
  7920. Else, if the cursor is before the first entry, sort the top-level items.
  7921. Else, the children of the entry at point are sorted.
  7922. Sorting can be alphabetically, numerically, by date/time as given by
  7923. a time stamp, by a property, by priority order, or by a custom function.
  7924. The command prompts for the sorting type unless it has been given to the
  7925. function through the SORTING-TYPE argument, which needs to be a character,
  7926. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7927. the precise meaning of each character:
  7928. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7929. c By creation time, which is assumed to be the first inactive time stamp
  7930. at the beginning of a line.
  7931. d By deadline date/time.
  7932. k By clocking time.
  7933. n Numerically, by converting the beginning of the entry/item to a number.
  7934. o By order of TODO keywords.
  7935. p By priority according to the cookie.
  7936. r By the value of a property.
  7937. s By scheduled date/time.
  7938. t By date/time, either the first active time stamp in the entry, or, if
  7939. none exist, by the first inactive one.
  7940. Capital letters will reverse the sort order.
  7941. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7942. called with point at the beginning of the record. It must return either
  7943. a string or a number that should serve as the sorting key for that record.
  7944. Comparing entries ignores case by default. However, with an optional argument
  7945. WITH-CASE, the sorting considers case as well.
  7946. Sorting is done against the visible part of the headlines, it ignores hidden
  7947. links.
  7948. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7949. (interactive "P")
  7950. (let ((case-func (if with-case 'identity 'downcase))
  7951. (cmstr
  7952. ;; The clock marker is lost when using `sort-subr', let's
  7953. ;; store the clocking string.
  7954. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7955. (save-excursion
  7956. (goto-char org-clock-marker)
  7957. (looking-back "^.*") (match-string-no-properties 0))))
  7958. start beg end stars re re2
  7959. txt what tmp)
  7960. ;; Find beginning and end of region to sort
  7961. (cond
  7962. ((org-region-active-p)
  7963. ;; we will sort the region
  7964. (setq end (region-end)
  7965. what "region")
  7966. (goto-char (region-beginning))
  7967. (if (not (org-at-heading-p)) (outline-next-heading))
  7968. (setq start (point)))
  7969. ((or (org-at-heading-p)
  7970. (ignore-errors (progn (org-back-to-heading) t)))
  7971. ;; we will sort the children of the current headline
  7972. (org-back-to-heading)
  7973. (setq start (point)
  7974. end (progn (org-end-of-subtree t t)
  7975. (or (bolp) (insert "\n"))
  7976. (when (>= (org-back-over-empty-lines) 1)
  7977. (forward-line 1))
  7978. (point))
  7979. what "children")
  7980. (goto-char start)
  7981. (show-subtree)
  7982. (outline-next-heading))
  7983. (t
  7984. ;; we will sort the top-level entries in this file
  7985. (goto-char (point-min))
  7986. (or (org-at-heading-p) (outline-next-heading))
  7987. (setq start (point))
  7988. (goto-char (point-max))
  7989. (beginning-of-line 1)
  7990. (when (looking-at ".*?\\S-")
  7991. ;; File ends in a non-white line
  7992. (end-of-line 1)
  7993. (insert "\n"))
  7994. (setq end (point-max))
  7995. (setq what "top-level")
  7996. (goto-char start)
  7997. (show-all)))
  7998. (setq beg (point))
  7999. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  8000. (looking-at "\\(\\*+\\)")
  8001. (setq stars (match-string 1)
  8002. re (concat "^" (regexp-quote stars) " +")
  8003. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8004. txt (buffer-substring beg end))
  8005. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8006. (if (and (not (equal stars "*")) (string-match re2 txt))
  8007. (user-error "Region to sort contains a level above the first entry"))
  8008. (unless sorting-type
  8009. (message
  8010. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8011. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8012. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8013. what)
  8014. (setq sorting-type (read-char-exclusive))
  8015. (unless getkey-func
  8016. (and (= (downcase sorting-type) ?f)
  8017. (setq getkey-func
  8018. (org-icompleting-read "Sort using function: "
  8019. obarray 'fboundp t nil nil))
  8020. (setq getkey-func (intern getkey-func))))
  8021. (and (= (downcase sorting-type) ?r)
  8022. (not property)
  8023. (setq property
  8024. (org-icompleting-read "Property: "
  8025. (mapcar 'list (org-buffer-property-keys t))
  8026. nil t))))
  8027. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8028. (message "Sorting entries...")
  8029. (save-restriction
  8030. (narrow-to-region start end)
  8031. (let ((dcst (downcase sorting-type))
  8032. (case-fold-search nil)
  8033. (now (current-time)))
  8034. (sort-subr
  8035. (/= dcst sorting-type)
  8036. ;; This function moves to the beginning character of the "record" to
  8037. ;; be sorted.
  8038. (lambda nil
  8039. (if (re-search-forward re nil t)
  8040. (goto-char (match-beginning 0))
  8041. (goto-char (point-max))))
  8042. ;; This function moves to the last character of the "record" being
  8043. ;; sorted.
  8044. (lambda nil
  8045. (save-match-data
  8046. (condition-case nil
  8047. (outline-forward-same-level 1)
  8048. (error
  8049. (goto-char (point-max))))))
  8050. ;; This function returns the value that gets sorted against.
  8051. (lambda nil
  8052. (cond
  8053. ((= dcst ?n)
  8054. (if (looking-at org-complex-heading-regexp)
  8055. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8056. nil))
  8057. ((= dcst ?a)
  8058. (if (looking-at org-complex-heading-regexp)
  8059. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8060. nil))
  8061. ((= dcst ?k)
  8062. (or (get-text-property (point) :org-clock-minutes) 0))
  8063. ((= dcst ?t)
  8064. (let ((end (save-excursion (outline-next-heading) (point))))
  8065. (if (or (re-search-forward org-ts-regexp end t)
  8066. (re-search-forward org-ts-regexp-both end t))
  8067. (org-time-string-to-seconds (match-string 0))
  8068. (org-float-time now))))
  8069. ((= dcst ?c)
  8070. (let ((end (save-excursion (outline-next-heading) (point))))
  8071. (if (re-search-forward
  8072. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8073. end t)
  8074. (org-time-string-to-seconds (match-string 0))
  8075. (org-float-time now))))
  8076. ((= dcst ?s)
  8077. (let ((end (save-excursion (outline-next-heading) (point))))
  8078. (if (re-search-forward org-scheduled-time-regexp end t)
  8079. (org-time-string-to-seconds (match-string 1))
  8080. (org-float-time now))))
  8081. ((= dcst ?d)
  8082. (let ((end (save-excursion (outline-next-heading) (point))))
  8083. (if (re-search-forward org-deadline-time-regexp end t)
  8084. (org-time-string-to-seconds (match-string 1))
  8085. (org-float-time now))))
  8086. ((= dcst ?p)
  8087. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8088. (string-to-char (match-string 2))
  8089. org-default-priority))
  8090. ((= dcst ?r)
  8091. (or (org-entry-get nil property) ""))
  8092. ((= dcst ?o)
  8093. (if (looking-at org-complex-heading-regexp)
  8094. (let* ((m (match-string 2))
  8095. (s (if (member m org-done-keywords) '- '+)))
  8096. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8097. ((= dcst ?f)
  8098. (if getkey-func
  8099. (progn
  8100. (setq tmp (funcall getkey-func))
  8101. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8102. tmp)
  8103. (error "Invalid key function `%s'" getkey-func)))
  8104. (t (error "Invalid sorting type `%c'" sorting-type))))
  8105. nil
  8106. (cond
  8107. ((= dcst ?a) 'string<)
  8108. ((= dcst ?f) compare-func)
  8109. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8110. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8111. ;; Reset the clock marker if needed
  8112. (when cmstr
  8113. (save-excursion
  8114. (goto-char start)
  8115. (search-forward cmstr nil t)
  8116. (move-marker org-clock-marker (point))))
  8117. (message "Sorting entries...done")))
  8118. ;;; The orgstruct minor mode
  8119. ;; Define a minor mode which can be used in other modes in order to
  8120. ;; integrate the org-mode structure editing commands.
  8121. ;; This is really a hack, because the org-mode structure commands use
  8122. ;; keys which normally belong to the major mode. Here is how it
  8123. ;; works: The minor mode defines all the keys necessary to operate the
  8124. ;; structure commands, but wraps the commands into a function which
  8125. ;; tests if the cursor is currently at a headline or a plain list
  8126. ;; item. If that is the case, the structure command is used,
  8127. ;; temporarily setting many Org-mode variables like regular
  8128. ;; expressions for filling etc. However, when any of those keys is
  8129. ;; used at a different location, function uses `key-binding' to look
  8130. ;; up if the key has an associated command in another currently active
  8131. ;; keymap (minor modes, major mode, global), and executes that
  8132. ;; command. There might be problems if any of the keys is otherwise
  8133. ;; used as a prefix key.
  8134. (defcustom orgstruct-heading-prefix-regexp ""
  8135. "Regexp that matches the custom prefix of Org headlines in
  8136. orgstruct(++)-mode."
  8137. :group 'org
  8138. :version "24.4"
  8139. :package-version '(Org . "8.3")
  8140. :type 'regexp)
  8141. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8142. (defcustom orgstruct-setup-hook nil
  8143. "Hook run after orgstruct-mode-map is filled."
  8144. :group 'org
  8145. :version "24.4"
  8146. :package-version '(Org . "8.0")
  8147. :type 'hook)
  8148. (defvar orgstruct-initialized nil)
  8149. (defvar org-local-vars nil
  8150. "List of local variables, for use by `orgstruct-mode'.")
  8151. ;;;###autoload
  8152. (define-minor-mode orgstruct-mode
  8153. "Toggle the minor mode `orgstruct-mode'.
  8154. This mode is for using Org-mode structure commands in other
  8155. modes. The following keys behave as if Org-mode were active, if
  8156. the cursor is on a headline, or on a plain list item (both as
  8157. defined by Org-mode)."
  8158. nil " OrgStruct" (make-sparse-keymap)
  8159. (funcall (if orgstruct-mode
  8160. 'add-to-invisibility-spec
  8161. 'remove-from-invisibility-spec)
  8162. '(outline . t))
  8163. (when orgstruct-mode
  8164. (org-load-modules-maybe)
  8165. (unless orgstruct-initialized
  8166. (orgstruct-setup)
  8167. (setq orgstruct-initialized t))))
  8168. ;;;###autoload
  8169. (defun turn-on-orgstruct ()
  8170. "Unconditionally turn on `orgstruct-mode'."
  8171. (orgstruct-mode 1))
  8172. (defvar org-fb-vars nil)
  8173. (make-variable-buffer-local 'org-fb-vars)
  8174. (defun orgstruct++-mode (&optional arg)
  8175. "Toggle `orgstruct-mode', the enhanced version of it.
  8176. In addition to setting orgstruct-mode, this also exports all
  8177. indentation and autofilling variables from org-mode into the
  8178. buffer. It will also recognize item context in multiline items."
  8179. (interactive "P")
  8180. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8181. (if (< arg 1)
  8182. (progn (orgstruct-mode -1)
  8183. (mapc (lambda(v)
  8184. (org-set-local (car v)
  8185. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8186. org-fb-vars))
  8187. (orgstruct-mode 1)
  8188. (setq org-fb-vars nil)
  8189. (unless org-local-vars
  8190. (setq org-local-vars (org-get-local-variables)))
  8191. (let (var val)
  8192. (mapc
  8193. (lambda (x)
  8194. (when (string-match
  8195. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8196. (symbol-name (car x)))
  8197. (setq var (car x) val (nth 1 x))
  8198. (push (list var `(quote ,(eval var))) org-fb-vars)
  8199. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8200. org-local-vars)
  8201. (org-set-local 'orgstruct-is-++ t))))
  8202. (defvar orgstruct-is-++ nil
  8203. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8204. (make-variable-buffer-local 'orgstruct-is-++)
  8205. ;;;###autoload
  8206. (defun turn-on-orgstruct++ ()
  8207. "Unconditionally turn on `orgstruct++-mode'."
  8208. (orgstruct++-mode 1))
  8209. (defun orgstruct-error ()
  8210. "Error when there is no default binding for a structure key."
  8211. (interactive)
  8212. (funcall (if (fboundp 'user-error)
  8213. 'user-error
  8214. 'error)
  8215. "This key has no function outside structure elements"))
  8216. (defun orgstruct-setup ()
  8217. "Setup orgstruct keymap."
  8218. (dolist (cell '((org-demote . t)
  8219. (org-metaleft . t)
  8220. (org-metaright . t)
  8221. (org-promote . t)
  8222. (org-shiftmetaleft . t)
  8223. (org-shiftmetaright . t)
  8224. org-backward-element
  8225. org-backward-heading-same-level
  8226. org-ctrl-c-ret
  8227. org-ctrl-c-minus
  8228. org-ctrl-c-star
  8229. org-cycle
  8230. org-forward-heading-same-level
  8231. org-insert-heading
  8232. org-insert-heading-respect-content
  8233. org-kill-note-or-show-branches
  8234. org-mark-subtree
  8235. org-meta-return
  8236. org-metadown
  8237. org-metaup
  8238. org-narrow-to-subtree
  8239. org-promote-subtree
  8240. org-reveal
  8241. org-shiftdown
  8242. org-shiftleft
  8243. org-shiftmetadown
  8244. org-shiftmetaup
  8245. org-shiftright
  8246. org-shifttab
  8247. org-shifttab
  8248. org-shiftup
  8249. org-show-subtree
  8250. org-sort
  8251. org-up-element
  8252. outline-demote
  8253. outline-next-visible-heading
  8254. outline-previous-visible-heading
  8255. outline-promote
  8256. outline-up-heading
  8257. show-children))
  8258. (let ((f (or (car-safe cell) cell))
  8259. (disable-when-heading-prefix (cdr-safe cell)))
  8260. (when (fboundp f)
  8261. (let ((new-bindings))
  8262. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8263. (where-is-internal f outline-mode-map)))
  8264. (push binding new-bindings)
  8265. ;; TODO use local-function-key-map
  8266. (dolist (rep '(("<tab>" . "TAB")
  8267. ("<return>" . "RET")
  8268. ("<escape>" . "ESC")
  8269. ("<delete>" . "DEL")))
  8270. (setq binding (read-kbd-macro
  8271. (let ((case-fold-search))
  8272. (replace-regexp-in-string
  8273. (regexp-quote (cdr rep))
  8274. (car rep)
  8275. (key-description binding)))))
  8276. (pushnew binding new-bindings :test 'equal)))
  8277. (dolist (binding new-bindings)
  8278. (let ((key (lookup-key orgstruct-mode-map binding)))
  8279. (when (or (not key) (numberp key))
  8280. (ignore-errors
  8281. (org-defkey orgstruct-mode-map
  8282. binding
  8283. (orgstruct-make-binding
  8284. f binding disable-when-heading-prefix))))))))))
  8285. (run-hooks 'orgstruct-setup-hook))
  8286. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8287. "Create a function for binding in the structure minor mode.
  8288. FUN is the command to call inside a table. KEY is the key that
  8289. should be checked in for a command to execute outside of tables.
  8290. Non-nil `disable-when-heading-prefix' means to disable the command
  8291. if `orgstruct-heading-prefix-regexp' is not empty."
  8292. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8293. (let ((nname name)
  8294. (i 0))
  8295. (while (fboundp (intern nname))
  8296. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8297. (setq name (intern nname)))
  8298. (eval
  8299. (let ((bindings '((org-heading-regexp
  8300. (concat "^"
  8301. orgstruct-heading-prefix-regexp
  8302. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8303. (org-outline-regexp
  8304. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8305. (org-outline-regexp-bol
  8306. (concat "^" org-outline-regexp))
  8307. (outline-regexp org-outline-regexp)
  8308. (outline-heading-end-regexp "\n")
  8309. (outline-level 'org-outline-level)
  8310. (outline-heading-alist))))
  8311. `(defun ,name (arg)
  8312. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8313. "Outside of structure, run the binding of `"
  8314. (key-description key) "'."
  8315. (when disable-when-heading-prefix
  8316. (concat
  8317. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8318. "back to the default binding due to limitations of Org's implementation of\n"
  8319. "`" (symbol-name fun) "'.")))
  8320. (interactive "p")
  8321. (let* ((disable
  8322. ,(and disable-when-heading-prefix
  8323. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8324. (fallback
  8325. (or disable
  8326. (not
  8327. (let* ,bindings
  8328. (org-context-p 'headline 'item
  8329. ,(when (memq fun
  8330. '(org-insert-heading
  8331. org-insert-heading-respect-content
  8332. org-meta-return))
  8333. '(when orgstruct-is-++
  8334. 'item-body))))))))
  8335. (if fallback
  8336. (let* ((orgstruct-mode)
  8337. (binding
  8338. (let ((key ,key))
  8339. (catch 'exit
  8340. (dolist
  8341. (rep
  8342. '(nil
  8343. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8344. ("<\\([^>]*\\)return>" . "\\1RET")
  8345. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8346. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8347. nil)
  8348. (when rep
  8349. (setq key (read-kbd-macro
  8350. (let ((case-fold-search))
  8351. (replace-regexp-in-string
  8352. (car rep)
  8353. (cdr rep)
  8354. (key-description key))))))
  8355. (when (key-binding key)
  8356. (throw 'exit (key-binding key))))))))
  8357. (if (keymapp binding)
  8358. (org-set-transient-map binding)
  8359. (let ((func (or binding
  8360. (unless disable
  8361. 'orgstruct-error))))
  8362. (when func
  8363. (call-interactively func)))))
  8364. (org-run-like-in-org-mode
  8365. (lambda ()
  8366. (interactive)
  8367. (let* ,bindings
  8368. (call-interactively ',fun)))))))))
  8369. name))
  8370. (defun org-contextualize-keys (alist contexts)
  8371. "Return valid elements in ALIST depending on CONTEXTS.
  8372. `org-agenda-custom-commands' or `org-capture-templates' are the
  8373. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8374. or `org-capture-templates-contexts' are the associated contexts
  8375. definitions."
  8376. (let ((contexts
  8377. ;; normalize contexts
  8378. (mapcar
  8379. (lambda(c) (cond ((listp (cadr c))
  8380. (list (car c) (car c) (cadr c)))
  8381. ((string= "" (cadr c))
  8382. (list (car c) (car c) (caddr c)))
  8383. (t c))) contexts))
  8384. (a alist) c r s)
  8385. ;; loop over all commands or templates
  8386. (while (setq c (pop a))
  8387. (let (vrules repl)
  8388. (cond
  8389. ((not (assoc (car c) contexts))
  8390. (push c r))
  8391. ((and (assoc (car c) contexts)
  8392. (setq vrules (org-contextualize-validate-key
  8393. (car c) contexts)))
  8394. (mapc (lambda (vr)
  8395. (when (not (equal (car vr) (cadr vr)))
  8396. (setq repl vr))) vrules)
  8397. (if (not repl) (push c r)
  8398. (push (cadr repl) s)
  8399. (push
  8400. (cons (car c)
  8401. (cdr (or (assoc (cadr repl) alist)
  8402. (error "Undefined key `%s' as contextual replacement for `%s'"
  8403. (cadr repl) (car c)))))
  8404. r))))))
  8405. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8406. (delq
  8407. nil
  8408. (delete-dups
  8409. (mapcar (lambda (x)
  8410. (let ((tpl (car x)))
  8411. (when (not (delq
  8412. nil
  8413. (mapcar (lambda(y)
  8414. (equal y tpl)) s))) x)))
  8415. (reverse r))))))
  8416. (defun org-contextualize-validate-key (key contexts)
  8417. "Check CONTEXTS for agenda or capture KEY."
  8418. (let (r rr res)
  8419. (while (setq r (pop contexts))
  8420. (mapc
  8421. (lambda (rr)
  8422. (when
  8423. (and (equal key (car r))
  8424. (if (functionp rr) (funcall rr)
  8425. (or (and (eq (car rr) 'in-file)
  8426. (buffer-file-name)
  8427. (string-match (cdr rr) (buffer-file-name)))
  8428. (and (eq (car rr) 'in-mode)
  8429. (string-match (cdr rr) (symbol-name major-mode)))
  8430. (and (eq (car rr) 'in-buffer)
  8431. (string-match (cdr rr) (buffer-name)))
  8432. (when (and (eq (car rr) 'not-in-file)
  8433. (buffer-file-name))
  8434. (not (string-match (cdr rr) (buffer-file-name))))
  8435. (when (eq (car rr) 'not-in-mode)
  8436. (not (string-match (cdr rr) (symbol-name major-mode))))
  8437. (when (eq (car rr) 'not-in-buffer)
  8438. (not (string-match (cdr rr) (buffer-name)))))))
  8439. (push r res)))
  8440. (car (last r))))
  8441. (delete-dups (delq nil res))))
  8442. (defun org-context-p (&rest contexts)
  8443. "Check if local context is any of CONTEXTS.
  8444. Possible values in the list of contexts are `table', `headline', and `item'."
  8445. (let ((pos (point)))
  8446. (goto-char (point-at-bol))
  8447. (prog1 (or (and (memq 'table contexts)
  8448. (looking-at "[ \t]*|"))
  8449. (and (memq 'headline contexts)
  8450. (looking-at org-outline-regexp))
  8451. (and (memq 'item contexts)
  8452. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8453. (and (memq 'item-body contexts)
  8454. (org-in-item-p)))
  8455. (goto-char pos))))
  8456. (defun org-get-local-variables ()
  8457. "Return a list of all local variables in an Org mode buffer."
  8458. (let (varlist)
  8459. (with-current-buffer (get-buffer-create "*Org tmp*")
  8460. (erase-buffer)
  8461. (org-mode)
  8462. (setq varlist (buffer-local-variables)))
  8463. (kill-buffer "*Org tmp*")
  8464. (delq nil
  8465. (mapcar
  8466. (lambda (x)
  8467. (setq x
  8468. (if (symbolp x)
  8469. (list x)
  8470. (list (car x) (cdr x))))
  8471. (if (and (not (get (car x) 'org-state))
  8472. (string-match
  8473. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8474. (symbol-name (car x))))
  8475. x nil))
  8476. varlist))))
  8477. (defun org-clone-local-variables (from-buffer &optional regexp)
  8478. "Clone local variables from FROM-BUFFER.
  8479. Optional argument REGEXP selects variables to clone."
  8480. (mapc
  8481. (lambda (pair)
  8482. (and (symbolp (car pair))
  8483. (or (null regexp)
  8484. (string-match regexp (symbol-name (car pair))))
  8485. (set (make-local-variable (car pair))
  8486. (cdr pair))))
  8487. (buffer-local-variables from-buffer)))
  8488. ;;;###autoload
  8489. (defun org-run-like-in-org-mode (cmd)
  8490. "Run a command, pretending that the current buffer is in Org-mode.
  8491. This will temporarily bind local variables that are typically bound in
  8492. Org-mode to the values they have in Org-mode, and then interactively
  8493. call CMD."
  8494. (org-load-modules-maybe)
  8495. (unless org-local-vars
  8496. (setq org-local-vars (org-get-local-variables)))
  8497. (let (binds)
  8498. (dolist (var org-local-vars)
  8499. (when (or (not (boundp (car var)))
  8500. (eq (symbol-value (car var))
  8501. (default-value (car var))))
  8502. (push (list (car var) `(quote ,(cadr var))) binds)))
  8503. (eval `(let ,binds
  8504. (call-interactively (quote ,cmd))))))
  8505. (defun org-get-category (&optional pos force-refresh)
  8506. "Get the category applying to position POS."
  8507. (save-match-data
  8508. (if force-refresh (org-refresh-category-properties))
  8509. (let ((pos (or pos (point))))
  8510. (or (get-text-property pos 'org-category)
  8511. (progn (org-refresh-category-properties)
  8512. (get-text-property pos 'org-category))))))
  8513. ;;; Refresh properties
  8514. (defun org-refresh-properties (dprop tprop)
  8515. "Refresh buffer text properties.
  8516. DPROP is the drawer property and TPROP is either the
  8517. corresponding text property to set, or an alist with each element
  8518. being a text property (as a symbol) and a function to apply to
  8519. the value of the drawer property."
  8520. (let ((case-fold-search t)
  8521. (inhibit-read-only t))
  8522. (org-with-silent-modifications
  8523. (save-excursion
  8524. (save-restriction
  8525. (widen)
  8526. (goto-char (point-min))
  8527. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8528. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8529. (defun org-refresh-property (tprop p)
  8530. "Refresh the buffer text property TPROP from the drawer property P.
  8531. The refresh happens only for the current tree (not subtree)."
  8532. (unless (org-before-first-heading-p)
  8533. (save-excursion
  8534. (org-back-to-heading t)
  8535. (if (symbolp tprop)
  8536. ;; TPROP is a text property symbol
  8537. (put-text-property
  8538. (point) (or (outline-next-heading) (point-max)) tprop p)
  8539. ;; TPROP is an alist with (properties . function) elements
  8540. (dolist (al tprop)
  8541. (save-excursion
  8542. (put-text-property
  8543. (line-beginning-position) (or (outline-next-heading) (point-max))
  8544. (car al)
  8545. (funcall (cdr al) p))))))))
  8546. (defun org-refresh-category-properties ()
  8547. "Refresh category text properties in the buffer."
  8548. (let ((case-fold-search t)
  8549. (inhibit-read-only t)
  8550. (default-category
  8551. (cond ((null org-category)
  8552. (if buffer-file-name
  8553. (file-name-sans-extension
  8554. (file-name-nondirectory buffer-file-name))
  8555. "???"))
  8556. ((symbolp org-category) (symbol-name org-category))
  8557. (t org-category))))
  8558. (org-with-silent-modifications
  8559. (org-with-wide-buffer
  8560. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8561. ;; This is the default category for the buffer. If none is
  8562. ;; found, fall-back to `org-category' or buffer file name.
  8563. (put-text-property
  8564. (point-min) (point-max)
  8565. 'org-category
  8566. (catch 'buffer-category
  8567. (goto-char (point-max))
  8568. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8569. (let ((element (org-element-at-point)))
  8570. (when (eq (org-element-type element) 'keyword)
  8571. (throw 'buffer-category
  8572. (org-element-property :value element)))))
  8573. default-category))
  8574. ;; Set sub-tree specific categories.
  8575. (goto-char (point-min))
  8576. (let ((regexp (org-re-property "CATEGORY")))
  8577. (while (re-search-forward regexp nil t)
  8578. (let ((value (org-match-string-no-properties 3)))
  8579. (when (org-at-property-p)
  8580. (put-text-property
  8581. (save-excursion (org-back-to-heading t) (point))
  8582. (org-end-of-subtree t t)
  8583. 'org-category
  8584. value)))))))))
  8585. (defun org-refresh-stats-properties ()
  8586. "Refresh stats text properties in the buffer."
  8587. (let (stats)
  8588. (org-with-silent-modifications
  8589. (save-excursion
  8590. (save-restriction
  8591. (widen)
  8592. (goto-char (point-min))
  8593. (while (re-search-forward
  8594. (concat org-outline-regexp-bol ".*"
  8595. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8596. nil t)
  8597. (setq stats (cond ((equal (match-string 3) "0") 0)
  8598. ((match-string 2)
  8599. (/ (* (string-to-number (match-string 2)) 100)
  8600. (string-to-number (match-string 3))))
  8601. (t (string-to-number (match-string 1)))))
  8602. (org-back-to-heading t)
  8603. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8604. 'org-stats stats)))))))
  8605. (defun org-refresh-effort-properties ()
  8606. "Refresh effort properties"
  8607. (org-refresh-properties
  8608. org-effort-property
  8609. '((effort . identity)
  8610. (effort-minutes . org-duration-string-to-minutes))))
  8611. ;;;; Link Stuff
  8612. ;;; Link abbreviations
  8613. (defun org-link-expand-abbrev (link)
  8614. "Apply replacements as defined in `org-link-abbrev-alist'."
  8615. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8616. (let* ((key (match-string 1 link))
  8617. (as (or (assoc key org-link-abbrev-alist-local)
  8618. (assoc key org-link-abbrev-alist)))
  8619. (tag (and (match-end 2) (match-string 3 link)))
  8620. rpl)
  8621. (if (not as)
  8622. link
  8623. (setq rpl (cdr as))
  8624. (cond
  8625. ((symbolp rpl) (funcall rpl tag))
  8626. ((string-match "%(\\([^)]+\\))" rpl)
  8627. (replace-match
  8628. (save-match-data
  8629. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8630. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8631. ((string-match "%h" rpl)
  8632. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8633. (t (concat rpl tag)))))
  8634. link))
  8635. ;;; Storing and inserting links
  8636. (defvar org-insert-link-history nil
  8637. "Minibuffer history for links inserted with `org-insert-link'.")
  8638. (defvar org-stored-links nil
  8639. "Contains the links stored with `org-store-link'.")
  8640. (defvar org-store-link-plist nil
  8641. "Plist with info about the most recently link created with `org-store-link'.")
  8642. (defvar org-link-protocols nil
  8643. "Link protocols added to Org-mode using `org-add-link-type'.")
  8644. (defvar org-store-link-functions nil
  8645. "List of functions that are called to create and store a link.
  8646. Each function will be called in turn until one returns a non-nil
  8647. value. Each function should check if it is responsible for creating
  8648. this link (for example by looking at the major mode).
  8649. If not, it must exit and return nil.
  8650. If yes, it should return a non-nil value after a calling
  8651. `org-store-link-props' with a list of properties and values.
  8652. Special properties are:
  8653. :type The link prefix, like \"http\". This must be given.
  8654. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8655. This is obligatory as well.
  8656. :description Optional default description for the second pair
  8657. of brackets in an Org-mode link. The user can still change
  8658. this when inserting this link into an Org-mode buffer.
  8659. In addition to these, any additional properties can be specified
  8660. and then used in capture templates.")
  8661. (defun org-add-link-type (type &optional follow export)
  8662. "Add TYPE to the list of `org-link-types'.
  8663. Re-compute all regular expressions depending on `org-link-types'
  8664. FOLLOW and EXPORT are two functions.
  8665. FOLLOW should take the link path as the single argument and do whatever
  8666. is necessary to follow the link, for example find a file or display
  8667. a mail message.
  8668. EXPORT should format the link path for export to one of the export formats.
  8669. It should be a function accepting three arguments:
  8670. path the path of the link, the text after the prefix (like \"http:\")
  8671. desc the description of the link, if any
  8672. format the export format, a symbol like `html' or `latex' or `ascii'.
  8673. The function may use the FORMAT information to return different values
  8674. depending on the format. The return value will be put literally into
  8675. the exported file. If the return value is nil, this means Org should
  8676. do what it normally does with links which do not have EXPORT defined.
  8677. Org mode has a built-in default for exporting links. If you are happy with
  8678. this default, there is no need to define an export function for the link
  8679. type. For a simple example of an export function, see `org-bbdb.el'."
  8680. (add-to-list 'org-link-types type t)
  8681. (org-make-link-regexps)
  8682. (org-element-update-syntax)
  8683. (if (assoc type org-link-protocols)
  8684. (setcdr (assoc type org-link-protocols) (list follow export))
  8685. (push (list type follow export) org-link-protocols)))
  8686. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8687. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8688. ;;;###autoload
  8689. (defun org-store-link (arg)
  8690. "\\<org-mode-map>Store an org-link to the current location.
  8691. This link is added to `org-stored-links' and can later be inserted
  8692. into an org-buffer with \\[org-insert-link].
  8693. For some link types, a prefix arg is interpreted.
  8694. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8695. For file links, arg negates `org-context-in-file-links'.
  8696. A double prefix arg force skipping storing functions that are not
  8697. part of Org's core.
  8698. A triple prefix arg force storing a link for each line in the
  8699. active region."
  8700. (interactive "P")
  8701. (org-load-modules-maybe)
  8702. (if (and (equal arg '(64)) (org-region-active-p))
  8703. (save-excursion
  8704. (let ((end (region-end)))
  8705. (goto-char (region-beginning))
  8706. (set-mark (point))
  8707. (while (< (point-at-eol) end)
  8708. (move-end-of-line 1) (activate-mark)
  8709. (let (current-prefix-arg)
  8710. (call-interactively 'org-store-link))
  8711. (move-beginning-of-line 2)
  8712. (set-mark (point)))))
  8713. (org-with-limited-levels
  8714. (setq org-store-link-plist nil)
  8715. (let (link cpltxt desc description search
  8716. txt custom-id agenda-link sfuns sfunsn)
  8717. (cond
  8718. ;; Store a link using an external link type
  8719. ((and (not (equal arg '(16)))
  8720. (setq sfuns
  8721. (delq
  8722. nil (mapcar (lambda (f)
  8723. (let (fs) (if (funcall f) (push f fs))))
  8724. org-store-link-functions))
  8725. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8726. (or (and (cdr sfuns)
  8727. (funcall (intern
  8728. (completing-read
  8729. "Which function for creating the link? "
  8730. sfunsn nil t (car sfunsn)))))
  8731. (funcall (caar sfuns)))
  8732. (setq link (plist-get org-store-link-plist :link)
  8733. desc (or (plist-get org-store-link-plist
  8734. :description) link))))
  8735. ;; Store a link from a source code buffer
  8736. ((org-src-edit-buffer-p)
  8737. (let (label gc)
  8738. (while (or (not label)
  8739. (save-excursion
  8740. (save-restriction
  8741. (widen)
  8742. (goto-char (point-min))
  8743. (re-search-forward
  8744. (regexp-quote (format org-coderef-label-format label))
  8745. nil t))))
  8746. (when label (message "Label exists already") (sit-for 2))
  8747. (setq label (read-string "Code line label: " label)))
  8748. (end-of-line 1)
  8749. (setq link (format org-coderef-label-format label))
  8750. (setq gc (- 79 (length link)))
  8751. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8752. (insert link)
  8753. (setq link (concat "(" label ")") desc nil)))
  8754. ;; We are in the agenda, link to referenced location
  8755. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8756. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8757. (get-text-property (point) 'org-marker))))
  8758. (when m
  8759. (org-with-point-at m
  8760. (setq agenda-link
  8761. (if (org-called-interactively-p 'any)
  8762. (call-interactively 'org-store-link)
  8763. (org-store-link nil)))))))
  8764. ((eq major-mode 'calendar-mode)
  8765. (let ((cd (calendar-cursor-to-date)))
  8766. (setq link
  8767. (format-time-string
  8768. (car org-time-stamp-formats)
  8769. (apply 'encode-time
  8770. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8771. nil nil nil))))
  8772. (org-store-link-props :type "calendar" :date cd)))
  8773. ((eq major-mode 'help-mode)
  8774. (setq link (concat "help:" (save-excursion
  8775. (goto-char (point-min))
  8776. (looking-at "^[^ ]+")
  8777. (match-string 0))))
  8778. (org-store-link-props :type "help"))
  8779. ((eq major-mode 'w3-mode)
  8780. (setq cpltxt (if (and (buffer-name)
  8781. (not (string-match "Untitled" (buffer-name))))
  8782. (buffer-name)
  8783. (url-view-url t))
  8784. link (url-view-url t))
  8785. (org-store-link-props :type "w3" :url (url-view-url t)))
  8786. ((eq major-mode 'image-mode)
  8787. (setq cpltxt (concat "file:"
  8788. (abbreviate-file-name buffer-file-name))
  8789. link cpltxt)
  8790. (org-store-link-props :type "image" :file buffer-file-name))
  8791. ;; In dired, store a link to the file of the current line
  8792. ((derived-mode-p 'dired-mode)
  8793. (let ((file (dired-get-filename nil t)))
  8794. (setq file (if file
  8795. (abbreviate-file-name
  8796. (expand-file-name (dired-get-filename nil t)))
  8797. ;; otherwise, no file so use current directory.
  8798. default-directory))
  8799. (setq cpltxt (concat "file:" file)
  8800. link cpltxt)))
  8801. ((setq search (run-hook-with-args-until-success
  8802. 'org-create-file-search-functions))
  8803. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8804. "::" search))
  8805. (setq cpltxt (or description link)))
  8806. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8807. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8808. (cond
  8809. ;; Store a link using the target at point
  8810. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8811. (setq cpltxt
  8812. (concat "file:"
  8813. (abbreviate-file-name
  8814. (buffer-file-name (buffer-base-buffer)))
  8815. "::" (match-string 1))
  8816. link cpltxt))
  8817. ((and (featurep 'org-id)
  8818. (or (eq org-id-link-to-org-use-id t)
  8819. (and (org-called-interactively-p 'any)
  8820. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8821. (and (eq org-id-link-to-org-use-id
  8822. 'create-if-interactive-and-no-custom-id)
  8823. (not custom-id))))
  8824. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8825. ;; Store a link using the ID at point
  8826. (setq link (condition-case nil
  8827. (prog1 (org-id-store-link)
  8828. (setq desc (or (plist-get org-store-link-plist
  8829. :description)
  8830. "")))
  8831. (error
  8832. ;; Probably before first headline, link only to file
  8833. (concat "file:"
  8834. (abbreviate-file-name
  8835. (buffer-file-name (buffer-base-buffer))))))))
  8836. (t
  8837. ;; Just link to current headline
  8838. (setq cpltxt (concat "file:"
  8839. (abbreviate-file-name
  8840. (buffer-file-name (buffer-base-buffer)))))
  8841. ;; Add a context search string
  8842. (when (org-xor org-context-in-file-links arg)
  8843. (let* ((ee (org-element-at-point))
  8844. (et (org-element-type ee))
  8845. (ev (plist-get (cadr ee) :value))
  8846. (ek (plist-get (cadr ee) :key))
  8847. (eok (and (stringp ek) (string-match "name" ek))))
  8848. (setq txt (cond
  8849. ((org-at-heading-p) nil)
  8850. ((and (eq et 'keyword) eok) ev)
  8851. ((org-region-active-p)
  8852. (buffer-substring (region-beginning) (region-end)))))
  8853. (when (or (null txt) (string-match "\\S-" txt))
  8854. (setq cpltxt
  8855. (concat cpltxt "::"
  8856. (condition-case nil
  8857. (org-make-org-heading-search-string txt)
  8858. (error "")))
  8859. desc (or (and (eq et 'keyword) eok ev)
  8860. (nth 4 (ignore-errors (org-heading-components)))
  8861. "NONE")))))
  8862. (if (string-match "::\\'" cpltxt)
  8863. (setq cpltxt (substring cpltxt 0 -2)))
  8864. (setq link cpltxt))))
  8865. ((buffer-file-name (buffer-base-buffer))
  8866. ;; Just link to this file here.
  8867. (setq cpltxt (concat "file:"
  8868. (abbreviate-file-name
  8869. (buffer-file-name (buffer-base-buffer)))))
  8870. ;; Add a context string.
  8871. (when (org-xor org-context-in-file-links arg)
  8872. (setq txt (if (org-region-active-p)
  8873. (buffer-substring (region-beginning) (region-end))
  8874. (buffer-substring (point-at-bol) (point-at-eol))))
  8875. ;; Only use search option if there is some text.
  8876. (when (string-match "\\S-" txt)
  8877. (setq cpltxt
  8878. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8879. desc "NONE")))
  8880. (setq link cpltxt))
  8881. ((org-called-interactively-p 'interactive)
  8882. (user-error "No method for storing a link from this buffer"))
  8883. (t (setq link nil)))
  8884. ;; We're done setting link and desc, clean up
  8885. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8886. (setq link (or link cpltxt)
  8887. desc (or desc cpltxt))
  8888. (cond ((equal desc "NONE") (setq desc nil))
  8889. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8890. (let ((d0 (match-string 3 desc))
  8891. (p0 (match-string 5 desc)))
  8892. (setq desc
  8893. (replace-regexp-in-string
  8894. org-bracket-link-regexp
  8895. (concat (or p0 d0)
  8896. (if (equal (length (match-string 0 desc))
  8897. (length desc)) "*" "")) desc)))))
  8898. ;; Return the link
  8899. (if (not (and (or (org-called-interactively-p 'any)
  8900. executing-kbd-macro) link))
  8901. (or agenda-link (and link (org-make-link-string link desc)))
  8902. (push (list link desc) org-stored-links)
  8903. (message "Stored: %s" (or desc link))
  8904. (when custom-id
  8905. (setq link (concat "file:" (abbreviate-file-name
  8906. (buffer-file-name)) "::#" custom-id))
  8907. (push (list link desc) org-stored-links))
  8908. (car org-stored-links))))))
  8909. (defun org-store-link-props (&rest plist)
  8910. "Store link properties, extract names and addresses."
  8911. (let (x adr)
  8912. (when (setq x (plist-get plist :from))
  8913. (setq adr (mail-extract-address-components x))
  8914. (setq plist (plist-put plist :fromname (car adr)))
  8915. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8916. (when (setq x (plist-get plist :to))
  8917. (setq adr (mail-extract-address-components x))
  8918. (setq plist (plist-put plist :toname (car adr)))
  8919. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8920. (let ((from (plist-get plist :from))
  8921. (to (plist-get plist :to)))
  8922. (when (and from to org-from-is-user-regexp)
  8923. (setq plist
  8924. (plist-put plist :fromto
  8925. (if (string-match org-from-is-user-regexp from)
  8926. (concat "to %t")
  8927. (concat "from %f"))))))
  8928. (setq org-store-link-plist plist))
  8929. (defun org-add-link-props (&rest plist)
  8930. "Add these properties to the link property list."
  8931. (let (key value)
  8932. (while plist
  8933. (setq key (pop plist) value (pop plist))
  8934. (setq org-store-link-plist
  8935. (plist-put org-store-link-plist key value)))))
  8936. (defun org-email-link-description (&optional fmt)
  8937. "Return the description part of an email link.
  8938. This takes information from `org-store-link-plist' and formats it
  8939. according to FMT (default from `org-email-link-description-format')."
  8940. (setq fmt (or fmt org-email-link-description-format))
  8941. (let* ((p org-store-link-plist)
  8942. (to (plist-get p :toaddress))
  8943. (from (plist-get p :fromaddress))
  8944. (table
  8945. (list
  8946. (cons "%c" (plist-get p :fromto))
  8947. (cons "%F" (plist-get p :from))
  8948. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8949. (cons "%T" (plist-get p :to))
  8950. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8951. (cons "%s" (plist-get p :subject))
  8952. (cons "%d" (plist-get p :date))
  8953. (cons "%m" (plist-get p :message-id)))))
  8954. (when (string-match "%c" fmt)
  8955. ;; Check if the user wrote this message
  8956. (if (and org-from-is-user-regexp from to
  8957. (save-match-data (string-match org-from-is-user-regexp from)))
  8958. (setq fmt (replace-match "to %t" t t fmt))
  8959. (setq fmt (replace-match "from %f" t t fmt))))
  8960. (org-replace-escapes fmt table)))
  8961. (defun org-make-org-heading-search-string (&optional string)
  8962. "Make search string for the current headline or STRING."
  8963. (let ((s (or string
  8964. (and (derived-mode-p 'org-mode)
  8965. (save-excursion
  8966. (org-back-to-heading t)
  8967. (org-element-property :raw-value (org-element-at-point))))))
  8968. (lines org-context-in-file-links))
  8969. (or string (setq s (concat "*" s))) ; Add * for headlines
  8970. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8971. (when (and string (integerp lines) (> lines 0))
  8972. (let ((slines (org-split-string s "\n")))
  8973. (when (< lines (length slines))
  8974. (setq s (mapconcat
  8975. 'identity
  8976. (reverse (nthcdr (- (length slines) lines)
  8977. (reverse slines))) "\n")))))
  8978. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8979. (defun org-make-link-string (link &optional description)
  8980. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8981. (unless (string-match "\\S-" link)
  8982. (error "Empty link"))
  8983. (when (and description
  8984. (stringp description)
  8985. (not (string-match "\\S-" description)))
  8986. (setq description nil))
  8987. (when (stringp description)
  8988. ;; Remove brackets from the description, they are fatal.
  8989. (while (string-match "\\[" description)
  8990. (setq description (replace-match "{" t t description)))
  8991. (while (string-match "\\]" description)
  8992. (setq description (replace-match "}" t t description))))
  8993. (when (equal link description)
  8994. ;; No description needed, it is identical
  8995. (setq description nil))
  8996. (when (and (not description)
  8997. (not (string-match (org-image-file-name-regexp) link))
  8998. (not (equal link (org-link-escape link))))
  8999. (setq description (org-extract-attributes link)))
  9000. (setq link
  9001. (cond ((string-match (org-image-file-name-regexp) link) link)
  9002. ((string-match org-link-types-re link)
  9003. (concat (match-string 1 link)
  9004. (org-link-escape (substring link (match-end 1)))))
  9005. (t (org-link-escape link))))
  9006. (concat "[[" link "]"
  9007. (if description (concat "[" description "]") "")
  9008. "]"))
  9009. (defconst org-link-escape-chars
  9010. ;;%20 %5B %5D
  9011. '(?\ ?\[ ?\])
  9012. "List of characters that should be escaped in a link when stored to Org.
  9013. This is the list that is used for internal purposes.")
  9014. (defconst org-link-escape-chars-browser
  9015. ;;%20 %22
  9016. '(?\ ?\")
  9017. "List of characters to be escaped before handing over to the browser.
  9018. If you consider using this constant then you probably want to use
  9019. the function `org-link-escape-browser' instead. See there why
  9020. this constant is a candidate to be removed once Org drops support
  9021. for Emacs 24.1 and 24.2.")
  9022. (defun org-link-escape (text &optional table merge)
  9023. "Return percent escaped representation of TEXT.
  9024. TEXT is a string with the text to escape.
  9025. Optional argument TABLE is a list with characters that should be
  9026. escaped. When nil, `org-link-escape-chars' is used.
  9027. If optional argument MERGE is set, merge TABLE into
  9028. `org-link-escape-chars'."
  9029. ;; Don't escape chars in internal links
  9030. (if (string-match "^\\*[[:alnum:]]+" text)
  9031. text
  9032. (cond
  9033. ((and table merge)
  9034. (mapc (lambda (defchr)
  9035. (unless (member defchr table)
  9036. (setq table (cons defchr table))))
  9037. org-link-escape-chars))
  9038. ((null table)
  9039. (setq table org-link-escape-chars)))
  9040. (mapconcat
  9041. (lambda (char)
  9042. (if (or (member char table)
  9043. (and (or (< char 32) (= char ?\%) (> char 126))
  9044. org-url-hexify-p))
  9045. (mapconcat (lambda (sequence-element)
  9046. (format "%%%.2X" sequence-element))
  9047. (or (encode-coding-char char 'utf-8)
  9048. (error "Unable to percent escape character: %s"
  9049. (char-to-string char))) "")
  9050. (char-to-string char))) text "")))
  9051. (defun org-link-escape-browser (text)
  9052. "Escape some characters before handing over to the browser.
  9053. This function is a candidate to be removed together with the
  9054. constant `org-link-escape-chars-browser' once Org drops support
  9055. for Emacs 24.1 and 24.2. All calls to this function will have to
  9056. be replaced with `url-encode-url' which is available since Emacs
  9057. 24.3.1."
  9058. ;; Example with the Org link
  9059. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9060. ;; to open the browser with +subject:"Release 8.2" filled into the
  9061. ;; query field: In this case the variable TEXT contains the
  9062. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9063. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9064. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9065. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9066. (if (fboundp 'url-encode-url)
  9067. (url-encode-url text)
  9068. (if (org-string-match-p
  9069. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9070. text)
  9071. (org-link-escape text org-link-escape-chars-browser)
  9072. text)))
  9073. (defun org-link-unescape (str)
  9074. "Unhex hexified Unicode strings as returned from the JavaScript function
  9075. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  9076. (unless (and (null str) (string= "" str))
  9077. (let ((pos 0) (case-fold-search t) unhexed)
  9078. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  9079. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  9080. (setq str (replace-match unhexed t t str))
  9081. (setq pos (+ pos (length unhexed))))))
  9082. str)
  9083. (defun org-link-unescape-compound (hex)
  9084. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9085. Note: this function also decodes single byte encodings like
  9086. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9087. (save-match-data
  9088. (let* ((bytes (cdr (split-string hex "%")))
  9089. (ret "")
  9090. (eat 0)
  9091. (sum 0))
  9092. (while bytes
  9093. (let* ((val (string-to-number (pop bytes) 16))
  9094. (shift-xor
  9095. (if (= 0 eat)
  9096. (cond
  9097. ((>= val 252) (cons 6 252))
  9098. ((>= val 248) (cons 5 248))
  9099. ((>= val 240) (cons 4 240))
  9100. ((>= val 224) (cons 3 224))
  9101. ((>= val 192) (cons 2 192))
  9102. (t (cons 0 0)))
  9103. (cons 6 128))))
  9104. (if (>= val 192) (setq eat (car shift-xor)))
  9105. (setq val (logxor val (cdr shift-xor)))
  9106. (setq sum (+ (lsh sum (car shift-xor)) val))
  9107. (if (> eat 0) (setq eat (- eat 1)))
  9108. (cond
  9109. ((= 0 eat) ;multi byte
  9110. (setq ret (concat ret (org-char-to-string sum)))
  9111. (setq sum 0))
  9112. ((not bytes) ; single byte(s)
  9113. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9114. ret)))
  9115. (defun org-link-unescape-single-byte-sequence (hex)
  9116. "Unhexify hex-encoded single byte character sequences."
  9117. (mapconcat (lambda (byte)
  9118. (char-to-string (string-to-number byte 16)))
  9119. (cdr (split-string hex "%")) ""))
  9120. (defun org-xor (a b)
  9121. "Exclusive or."
  9122. (if a (not b) b))
  9123. (defun org-fixup-message-id-for-http (s)
  9124. "Replace special characters in a message id, so it can be used in an http query."
  9125. (when (string-match "%" s)
  9126. (setq s (mapconcat (lambda (c)
  9127. (if (eq c ?%)
  9128. "%25"
  9129. (char-to-string c)))
  9130. s "")))
  9131. (while (string-match "<" s)
  9132. (setq s (replace-match "%3C" t t s)))
  9133. (while (string-match ">" s)
  9134. (setq s (replace-match "%3E" t t s)))
  9135. (while (string-match "@" s)
  9136. (setq s (replace-match "%40" t t s)))
  9137. s)
  9138. (defun org-link-prettify (link)
  9139. "Return a human-readable representation of LINK.
  9140. The car of LINK must be a raw link.
  9141. The cdr of LINK must be either a link description or nil."
  9142. (let ((desc (or (cadr link) "<no description>")))
  9143. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9144. "<" (car link) ">")))
  9145. ;;;###autoload
  9146. (defun org-insert-link-global ()
  9147. "Insert a link like Org-mode does.
  9148. This command can be called in any mode to insert a link in Org-mode syntax."
  9149. (interactive)
  9150. (org-load-modules-maybe)
  9151. (org-run-like-in-org-mode 'org-insert-link))
  9152. (defun org-insert-all-links (arg &optional pre post)
  9153. "Insert all links in `org-stored-links'.
  9154. When a universal prefix, do not delete the links from `org-stored-links'.
  9155. When `ARG' is a number, insert the last N link(s).
  9156. `PRE' and `POST' are optional arguments to define a string to
  9157. prepend or to append."
  9158. (interactive "P")
  9159. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9160. (links (copy-seq org-stored-links))
  9161. (pr (or pre "- "))
  9162. (po (or post "\n"))
  9163. (cnt 1) l)
  9164. (if (null org-stored-links)
  9165. (message "No link to insert")
  9166. (while (and (or (listp arg) (>= arg cnt))
  9167. (setq l (if (listp arg)
  9168. (pop links)
  9169. (pop org-stored-links))))
  9170. (setq cnt (1+ cnt))
  9171. (insert pr)
  9172. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9173. (insert po)))))
  9174. (defun org-insert-last-stored-link (arg)
  9175. "Insert the last link stored in `org-stored-links'."
  9176. (interactive "p")
  9177. (org-insert-all-links arg "" "\n"))
  9178. (defun org-link-fontify-links-to-this-file ()
  9179. "Fontify links to the current file in `org-stored-links'."
  9180. (let ((f (buffer-file-name)) a b)
  9181. (setq a (mapcar (lambda(l)
  9182. (let ((ll (car l)))
  9183. (when (and (string-match "^file:\\(.+\\)::" ll)
  9184. (equal f (expand-file-name (match-string 1 ll))))
  9185. ll)))
  9186. org-stored-links))
  9187. (when (featurep 'org-id)
  9188. (setq b (mapcar (lambda(l)
  9189. (let ((ll (car l)))
  9190. (when (and (string-match "^id:\\(.+\\)$" ll)
  9191. (equal f (expand-file-name
  9192. (or (org-id-find-id-file
  9193. (match-string 1 ll)) ""))))
  9194. ll)))
  9195. org-stored-links)))
  9196. (mapcar (lambda(l)
  9197. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9198. (delq nil (append a b)))))
  9199. (defvar org-link-links-in-this-file nil)
  9200. (defun org-insert-link (&optional complete-file link-location default-description)
  9201. "Insert a link. At the prompt, enter the link.
  9202. Completion can be used to insert any of the link protocol prefixes like
  9203. http or ftp in use.
  9204. The history can be used to select a link previously stored with
  9205. `org-store-link'. When the empty string is entered (i.e. if you just
  9206. press RET at the prompt), the link defaults to the most recently
  9207. stored link. As SPC triggers completion in the minibuffer, you need to
  9208. use M-SPC or C-q SPC to force the insertion of a space character.
  9209. You will also be prompted for a description, and if one is given, it will
  9210. be displayed in the buffer instead of the link.
  9211. If there is already a link at point, this command will allow you to edit link
  9212. and description parts.
  9213. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9214. be selected using completion. The path to the file will be relative to the
  9215. current directory if the file is in the current directory or a subdirectory.
  9216. Otherwise, the link will be the absolute path as completed in the minibuffer
  9217. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9218. option `org-link-file-path-type'.
  9219. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9220. the current directory or below.
  9221. With three \\[universal-argument] prefixes, negate the meaning of
  9222. `org-keep-stored-link-after-insertion'.
  9223. If `org-make-link-description-function' is non-nil, this function will be
  9224. called with the link target, and the result will be the default
  9225. link description.
  9226. If the LINK-LOCATION parameter is non-nil, this value will be
  9227. used as the link location instead of reading one interactively.
  9228. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9229. be used as the default description."
  9230. (interactive "P")
  9231. (let* ((wcf (current-window-configuration))
  9232. (origbuf (current-buffer))
  9233. (region (if (org-region-active-p)
  9234. (buffer-substring (region-beginning) (region-end))))
  9235. (remove (and region (list (region-beginning) (region-end))))
  9236. (desc region)
  9237. tmphist ; byte-compile incorrectly complains about this
  9238. (link link-location)
  9239. (abbrevs org-link-abbrev-alist-local)
  9240. entry file all-prefixes auto-desc)
  9241. (cond
  9242. (link-location) ; specified by arg, just use it.
  9243. ((org-in-regexp org-bracket-link-regexp 1)
  9244. ;; We do have a link at point, and we are going to edit it.
  9245. (setq remove (list (match-beginning 0) (match-end 0)))
  9246. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9247. (setq link (read-string "Link: "
  9248. (org-link-unescape
  9249. (org-match-string-no-properties 1)))))
  9250. ((or (org-in-regexp org-angle-link-re)
  9251. (org-in-regexp org-plain-link-re))
  9252. ;; Convert to bracket link
  9253. (setq remove (list (match-beginning 0) (match-end 0))
  9254. link (read-string "Link: "
  9255. (org-remove-angle-brackets (match-string 0)))))
  9256. ((member complete-file '((4) (16)))
  9257. ;; Completing read for file names.
  9258. (setq link (org-file-complete-link complete-file)))
  9259. (t
  9260. ;; Read link, with completion for stored links.
  9261. (org-link-fontify-links-to-this-file)
  9262. (org-switch-to-buffer-other-window "*Org Links*")
  9263. (with-current-buffer "*Org Links*"
  9264. (erase-buffer)
  9265. (insert "Insert a link.
  9266. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9267. (when org-stored-links
  9268. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9269. (insert (mapconcat 'org-link-prettify
  9270. (reverse org-stored-links) "\n")))
  9271. (goto-char (point-min)))
  9272. (let ((cw (selected-window)))
  9273. (select-window (get-buffer-window "*Org Links*" 'visible))
  9274. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9275. (unless (pos-visible-in-window-p (point-max))
  9276. (org-fit-window-to-buffer))
  9277. (and (window-live-p cw) (select-window cw)))
  9278. ;; Fake a link history, containing the stored links.
  9279. (setq tmphist (append (mapcar 'car org-stored-links)
  9280. org-insert-link-history))
  9281. (setq all-prefixes (append (mapcar 'car abbrevs)
  9282. (mapcar 'car org-link-abbrev-alist)
  9283. org-link-types))
  9284. (unwind-protect
  9285. (progn
  9286. (setq link
  9287. (org-completing-read
  9288. "Link: "
  9289. (append
  9290. (mapcar (lambda (x) (concat x ":"))
  9291. all-prefixes)
  9292. (mapcar 'car org-stored-links))
  9293. nil nil nil
  9294. 'tmphist
  9295. (caar org-stored-links)))
  9296. (if (not (string-match "\\S-" link))
  9297. (user-error "No link selected"))
  9298. (mapc (lambda(l)
  9299. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9300. org-stored-links)
  9301. (if (or (member link all-prefixes)
  9302. (and (equal ":" (substring link -1))
  9303. (member (substring link 0 -1) all-prefixes)
  9304. (setq link (substring link 0 -1))))
  9305. (setq link (with-current-buffer origbuf
  9306. (org-link-try-special-completion link)))))
  9307. (set-window-configuration wcf)
  9308. (kill-buffer "*Org Links*"))
  9309. (setq entry (assoc link org-stored-links))
  9310. (or entry (push link org-insert-link-history))
  9311. (setq desc (or desc (nth 1 entry)))))
  9312. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9313. (not org-keep-stored-link-after-insertion))
  9314. (setq org-stored-links (delq (assoc link org-stored-links)
  9315. org-stored-links)))
  9316. (if (and (string-match org-plain-link-re link)
  9317. (not (string-match org-ts-regexp link)))
  9318. ;; URL-like link, normalize the use of angular brackets.
  9319. (setq link (org-remove-angle-brackets link)))
  9320. ;; Check if we are linking to the current file with a search
  9321. ;; option If yes, simplify the link by using only the search
  9322. ;; option.
  9323. (when (and buffer-file-name
  9324. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9325. (let* ((path (match-string 1 link))
  9326. (case-fold-search nil)
  9327. (search (match-string 2 link)))
  9328. (save-match-data
  9329. (if (equal (file-truename buffer-file-name) (file-truename path))
  9330. ;; We are linking to this same file, with a search option
  9331. (setq link search)))))
  9332. ;; Check if we can/should use a relative path. If yes, simplify the link
  9333. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9334. (let* ((type (match-string 1 link))
  9335. (path (match-string 2 link))
  9336. (origpath path)
  9337. (case-fold-search nil))
  9338. (cond
  9339. ((or (eq org-link-file-path-type 'absolute)
  9340. (equal complete-file '(16)))
  9341. (setq path (abbreviate-file-name (expand-file-name path))))
  9342. ((eq org-link-file-path-type 'noabbrev)
  9343. (setq path (expand-file-name path)))
  9344. ((eq org-link-file-path-type 'relative)
  9345. (setq path (file-relative-name path)))
  9346. (t
  9347. (save-match-data
  9348. (if (string-match (concat "^" (regexp-quote
  9349. (expand-file-name
  9350. (file-name-as-directory
  9351. default-directory))))
  9352. (expand-file-name path))
  9353. ;; We are linking a file with relative path name.
  9354. (setq path (substring (expand-file-name path)
  9355. (match-end 0)))
  9356. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9357. (setq link (concat type path))
  9358. (if (equal desc origpath)
  9359. (setq desc path))))
  9360. (if org-make-link-description-function
  9361. (setq desc
  9362. (or (condition-case nil
  9363. (funcall org-make-link-description-function link desc)
  9364. (error (progn (message "Can't get link description from `%s'"
  9365. (symbol-name org-make-link-description-function))
  9366. (sit-for 2) nil)))
  9367. (read-string "Description: " default-description)))
  9368. (if default-description (setq desc default-description)
  9369. (setq desc (or (and auto-desc desc)
  9370. (read-string "Description: " desc)))))
  9371. (unless (string-match "\\S-" desc) (setq desc nil))
  9372. (if remove (apply 'delete-region remove))
  9373. (insert (org-make-link-string link desc))))
  9374. (defun org-link-try-special-completion (type)
  9375. "If there is completion support for link type TYPE, offer it."
  9376. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9377. (if (functionp fun)
  9378. (funcall fun)
  9379. (read-string "Link (no completion support): " (concat type ":")))))
  9380. (defun org-file-complete-link (&optional arg)
  9381. "Create a file link using completion."
  9382. (let ((file (org-iread-file-name "File: "))
  9383. (pwd (file-name-as-directory (expand-file-name ".")))
  9384. (pwd1 (file-name-as-directory (abbreviate-file-name
  9385. (expand-file-name ".")))))
  9386. (cond ((equal arg '(16))
  9387. (concat "file:"
  9388. (abbreviate-file-name (expand-file-name file))))
  9389. ((string-match
  9390. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9391. (concat "file:" (match-string 1 file)))
  9392. ((string-match
  9393. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9394. (expand-file-name file))
  9395. (concat "file:"
  9396. (match-string 1 (expand-file-name file))))
  9397. (t (concat "file:" file)))))
  9398. (defun org-iread-file-name (&rest args)
  9399. "Read-file-name using `ido-mode' speedup if available.
  9400. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9401. See `read-file-name' for a description of parameters."
  9402. (org-without-partial-completion
  9403. (if (and org-completion-use-ido
  9404. (fboundp 'ido-read-file-name)
  9405. (boundp 'ido-mode) ido-mode
  9406. (listp (second args)))
  9407. (let ((ido-enter-matching-directory nil))
  9408. (apply 'ido-read-file-name args))
  9409. (apply (or read-file-name-function 'read-file-name) args))))
  9410. (defun org-completing-read (&rest args)
  9411. "Completing-read with SPACE being a normal character."
  9412. (let ((enable-recursive-minibuffers t)
  9413. (minibuffer-local-completion-map
  9414. (copy-keymap minibuffer-local-completion-map)))
  9415. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9416. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9417. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9418. (apply 'org-icompleting-read args)))
  9419. (defun org-completing-read-no-i (&rest args)
  9420. (let (org-completion-use-ido org-completion-use-iswitchb)
  9421. (apply 'org-completing-read args)))
  9422. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9423. "Use iswitch as a completing-read replacement to choose from choices.
  9424. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9425. from."
  9426. (let* ((iswitchb-use-virtual-buffers nil)
  9427. (iswitchb-make-buflist-hook
  9428. (lambda ()
  9429. (setq iswitchb-temp-buflist choices))))
  9430. (iswitchb-read-buffer prompt)))
  9431. (defun org-icompleting-read (&rest args)
  9432. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9433. Should be called like `completing-read'."
  9434. (org-without-partial-completion
  9435. (if (not (listp (nth 1 args)))
  9436. ;; Ido only supports lists as the COLLECTION argument. Use
  9437. ;; default completion function when second argument is not
  9438. ;; a list.
  9439. (apply #'completing-read args)
  9440. (let ((ido-enter-matching-directory nil))
  9441. (apply (cond ((and org-completion-use-ido
  9442. (fboundp 'ido-completing-read)
  9443. (org-bound-and-true-p ido-mode))
  9444. #'ido-completing-read)
  9445. ((and org-completion-use-iswitchb
  9446. (org-bound-and-true-p iswitchb-mode))
  9447. #'org-iswitchb-completing-read)
  9448. (t #'completing-read))
  9449. (pop args)
  9450. (if (org-some #'consp (car args))
  9451. (mapcar #'car (pop args))
  9452. (pop args))
  9453. args)))))
  9454. (defun org-extract-attributes (s)
  9455. "Extract the attributes cookie from a string and set as text property."
  9456. (let (a attr (start 0) key value)
  9457. (save-match-data
  9458. (when (string-match "{{\\([^}]+\\)}}$" s)
  9459. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9460. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9461. (setq key (match-string 1 a) value (match-string 2 a)
  9462. start (match-end 0)
  9463. attr (plist-put attr (intern key) value))))
  9464. (org-add-props s nil 'org-attr attr))
  9465. s))
  9466. ;;; Opening/following a link
  9467. (defvar org-link-search-failed nil)
  9468. (defvar org-open-link-functions nil
  9469. "Hook for functions finding a plain text link.
  9470. These functions must take a single argument, the link content.
  9471. They will be called for links that look like [[link text][description]]
  9472. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9473. like a filename (e.g. \"./blue.png\").
  9474. These functions will be called *before* Org attempts to resolve the
  9475. link by doing text searches in the current buffer - so if you want a
  9476. link \"[[target]]\" to still find \"<<target>>\", your function should
  9477. handle this as a special case.
  9478. When the function does handle the link, it must return a non-nil value.
  9479. If it decides that it is not responsible for this link, it must return
  9480. nil to indicate that that Org-mode can continue with other options
  9481. like exact and fuzzy text search.")
  9482. (defun org-next-link (&optional search-backward)
  9483. "Move forward to the next link.
  9484. If the link is in hidden text, expose it."
  9485. (interactive "P")
  9486. (when (and org-link-search-failed (eq this-command last-command))
  9487. (goto-char (point-min))
  9488. (message "Link search wrapped back to beginning of buffer"))
  9489. (setq org-link-search-failed nil)
  9490. (let* ((pos (point))
  9491. (ct (org-context))
  9492. (a (assoc :link ct))
  9493. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9494. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9495. ((looking-at org-any-link-re)
  9496. ;; Don't stay stuck at link without an org-link face
  9497. (forward-char (if search-backward -1 1))))
  9498. (if (funcall srch-fun org-any-link-re nil t)
  9499. (progn
  9500. (goto-char (match-beginning 0))
  9501. (if (outline-invisible-p) (org-show-context)))
  9502. (goto-char pos)
  9503. (setq org-link-search-failed t)
  9504. (message "No further link found"))))
  9505. (defun org-previous-link ()
  9506. "Move backward to the previous link.
  9507. If the link is in hidden text, expose it."
  9508. (interactive)
  9509. (funcall 'org-next-link t))
  9510. (defun org-translate-link (s)
  9511. "Translate a link string if a translation function has been defined."
  9512. (if (and org-link-translation-function
  9513. (fboundp org-link-translation-function)
  9514. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9515. (progn
  9516. (setq s (funcall org-link-translation-function
  9517. (match-string 1 s) (match-string 2 s)))
  9518. (concat (car s) ":" (cdr s)))
  9519. s))
  9520. (defun org-translate-link-from-planner (type path)
  9521. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9522. This is still an experimental function, your mileage may vary."
  9523. (cond
  9524. ((member type '("http" "https" "news" "ftp"))
  9525. ;; standard Internet links are the same.
  9526. nil)
  9527. ((and (equal type "irc") (string-match "^//" path))
  9528. ;; Planner has two / at the beginning of an irc link, we have 1.
  9529. ;; We should have zero, actually....
  9530. (setq path (substring path 1)))
  9531. ((and (equal type "lisp") (string-match "^/" path))
  9532. ;; Planner has a slash, we do not.
  9533. (setq type "elisp" path (substring path 1)))
  9534. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9535. ;; A typical message link. Planner has the id after the final slash,
  9536. ;; we separate it with a hash mark
  9537. (setq path (concat (match-string 1 path) "#"
  9538. (org-remove-angle-brackets (match-string 2 path))))))
  9539. (cons type path))
  9540. (defun org-find-file-at-mouse (ev)
  9541. "Open file link or URL at mouse."
  9542. (interactive "e")
  9543. (mouse-set-point ev)
  9544. (org-open-at-point 'in-emacs))
  9545. (defun org-open-at-mouse (ev)
  9546. "Open file link or URL at mouse.
  9547. See the docstring of `org-open-file' for details."
  9548. (interactive "e")
  9549. (mouse-set-point ev)
  9550. (if (eq major-mode 'org-agenda-mode)
  9551. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9552. (org-open-at-point))
  9553. (defvar org-window-config-before-follow-link nil
  9554. "The window configuration before following a link.
  9555. This is saved in case the need arises to restore it.")
  9556. (defvar org-open-link-marker (make-marker)
  9557. "Marker pointing to the location where `org-open-at-point' was called.")
  9558. ;;;###autoload
  9559. (defun org-open-at-point-global ()
  9560. "Follow a link like Org-mode does.
  9561. This command can be called in any mode to follow a link that has
  9562. Org-mode syntax."
  9563. (interactive)
  9564. (org-run-like-in-org-mode 'org-open-at-point))
  9565. ;;;###autoload
  9566. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9567. "Open a link in the string S, as if it was in Org-mode."
  9568. (interactive "sLink: \nP")
  9569. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9570. (with-temp-buffer
  9571. (let ((org-inhibit-startup (not reference-buffer)))
  9572. (org-mode)
  9573. (insert s)
  9574. (goto-char (point-min))
  9575. (when reference-buffer
  9576. (setq org-link-abbrev-alist-local
  9577. (with-current-buffer reference-buffer
  9578. org-link-abbrev-alist-local)))
  9579. (org-open-at-point arg reference-buffer)))))
  9580. (defvar org-open-at-point-functions nil
  9581. "Hook that is run when following a link at point.
  9582. Functions in this hook must return t if they identify and follow
  9583. a link at point. If they don't find anything interesting at point,
  9584. they must return nil.")
  9585. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9586. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9587. (defun org-open-at-point (&optional arg reference-buffer)
  9588. "Open link, timestamp, footnote or tags at point.
  9589. When point is on a link, follow it. Normally, files will be
  9590. opened by an appropriate application. If the optional prefix
  9591. argument ARG is non-nil, Emacs will visit the file. With
  9592. a double prefix argument, try to open outside of Emacs, in the
  9593. application the system uses for this file type.
  9594. When point is on a timestamp, open the agenda at the day
  9595. specified.
  9596. When point is a footnote definition, move to the first reference
  9597. found. If it is on a reference, move to the associated
  9598. definition.
  9599. When point is on a headline, display a list of every link in the
  9600. entry, so it is possible to pick one, or all, of them. If point
  9601. is on a tag, call `org-tags-view' instead.
  9602. When optional argument REFERENCE-BUFFER is non-nil, it should
  9603. specify a buffer from where the link search should happen. This
  9604. is used internally by `org-open-link-from-string'.
  9605. On top of syntactically correct links, this function will open
  9606. the link at point in comments or comment blocks and the first
  9607. link in a property drawer line."
  9608. (interactive "P")
  9609. ;; On a code block, open block's results.
  9610. (unless (call-interactively 'org-babel-open-src-block-result)
  9611. (org-load-modules-maybe)
  9612. (move-marker org-open-link-marker (point))
  9613. (setq org-window-config-before-follow-link (current-window-configuration))
  9614. (org-remove-occur-highlights nil nil t)
  9615. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9616. (let* ((context
  9617. ;; Only consider supported types, even if they are not
  9618. ;; the closest one.
  9619. (org-element-lineage
  9620. (org-element-context)
  9621. '(comment comment-block footnote-definition footnote-reference
  9622. headline inlinetask keyword link node-property
  9623. timestamp)
  9624. t))
  9625. (type (org-element-type context))
  9626. (value (org-element-property :value context)))
  9627. (cond
  9628. ((not context) (user-error "No link found"))
  9629. ;; Exception: open timestamps and links in properties
  9630. ;; drawers, keywords and comments.
  9631. ((memq type '(comment comment-block keyword node-property))
  9632. (cond ((org-in-regexp org-any-link-re)
  9633. (org-open-link-from-string (match-string-no-properties 0)))
  9634. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9635. (org-follow-timestamp-link))
  9636. (t (user-error "No link found"))))
  9637. ;; On a headline or an inlinetask, but not on a timestamp,
  9638. ;; a link, a footnote reference or on tags.
  9639. ((and (memq type '(headline inlinetask))
  9640. ;; Not on tags.
  9641. (progn (save-excursion (beginning-of-line)
  9642. (looking-at org-complex-heading-regexp))
  9643. (or (not (match-beginning 5))
  9644. (< (point) (match-beginning 5)))))
  9645. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9646. (links (car data))
  9647. (links-end (cdr data)))
  9648. (if links
  9649. (dolist (link (if (stringp links) (list links) links))
  9650. (search-forward link nil links-end)
  9651. (goto-char (match-beginning 0))
  9652. (org-open-at-point))
  9653. (require 'org-attach)
  9654. (org-attach-reveal 'if-exists))))
  9655. ;; Do nothing on white spaces after an object, unless point
  9656. ;; is right after it.
  9657. ((> (point)
  9658. (save-excursion
  9659. (goto-char (org-element-property :end context))
  9660. (skip-chars-backward " \t")
  9661. (point)))
  9662. (user-error "No link found"))
  9663. ((eq type 'timestamp) (org-follow-timestamp-link))
  9664. ;; On tags within a headline or an inlinetask.
  9665. ((and (memq type '(headline inlinetask))
  9666. (progn (save-excursion (beginning-of-line)
  9667. (looking-at org-complex-heading-regexp))
  9668. (and (match-beginning 5)
  9669. (>= (point) (match-beginning 5)))))
  9670. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9671. ((eq type 'link)
  9672. ;; When link is located within the description of another
  9673. ;; link (e.g., an inline image), always open the parent
  9674. ;; link.
  9675. (let*((link (let ((up (org-element-property :parent context)))
  9676. (if (eq (org-element-type up) 'link) up context)))
  9677. (type (org-element-property :type link))
  9678. (path (org-link-unescape (org-element-property :path link))))
  9679. ;; Switch back to REFERENCE-BUFFER needed when called in
  9680. ;; a temporary buffer through `org-open-link-from-string'.
  9681. (with-current-buffer (or reference-buffer (current-buffer))
  9682. (cond
  9683. ((equal type "file")
  9684. (if (string-match "[*?{]" (file-name-nondirectory path))
  9685. (dired path)
  9686. ;; Look into `org-link-protocols' in order to find
  9687. ;; a DEDICATED-FUNCTION to open file. The function
  9688. ;; will be applied on raw link instead of parsed
  9689. ;; link due to the limitation in `org-add-link-type'
  9690. ;; ("open" function called with a single argument).
  9691. ;; If no such function is found, fallback to
  9692. ;; `org-open-file'.
  9693. ;;
  9694. ;; Note : "file+emacs" and "file+sys" types are
  9695. ;; hard-coded in order to escape the previous
  9696. ;; limitation.
  9697. (let* ((option (org-element-property :search-option link))
  9698. (app (org-element-property :application link))
  9699. (dedicated-function
  9700. (nth 1 (assoc app org-link-protocols))))
  9701. (if dedicated-function
  9702. (funcall dedicated-function
  9703. (concat path
  9704. (and option (concat "::" option))))
  9705. (apply 'org-open-file
  9706. path
  9707. (cond (arg)
  9708. ((equal app "emacs") 'emacs)
  9709. ((equal app "sys") 'system))
  9710. (cond ((not option) nil)
  9711. ((org-string-match-p "\\`[0-9]+\\'" option)
  9712. (list (string-to-number option)))
  9713. (t (list nil
  9714. (org-link-unescape option)))))))))
  9715. ((assoc type org-link-protocols)
  9716. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9717. ((equal type "help")
  9718. (let ((f-or-v (intern path)))
  9719. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9720. ((boundp f-or-v) (describe-variable f-or-v))
  9721. (t (error "Not a known function or variable")))))
  9722. ((member type '("http" "https" "ftp" "mailto" "news"))
  9723. (browse-url (org-link-escape-browser (concat type ":" path))))
  9724. ((equal type "doi")
  9725. (browse-url
  9726. (org-link-escape-browser (concat org-doi-server-url path))))
  9727. ((equal type "message") (browse-url (concat type ":" path)))
  9728. ((equal type "shell")
  9729. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9730. (cmd path))
  9731. (if (or (and (org-string-nw-p
  9732. org-confirm-shell-link-not-regexp)
  9733. (string-match
  9734. org-confirm-shell-link-not-regexp cmd))
  9735. (not org-confirm-shell-link-function)
  9736. (funcall org-confirm-shell-link-function
  9737. (format "Execute \"%s\" in shell? "
  9738. (org-add-props cmd nil
  9739. 'face 'org-warning))))
  9740. (progn
  9741. (message "Executing %s" cmd)
  9742. (shell-command cmd buf)
  9743. (when (featurep 'midnight)
  9744. (setq clean-buffer-list-kill-buffer-names
  9745. (cons buf
  9746. clean-buffer-list-kill-buffer-names))))
  9747. (user-error "Abort"))))
  9748. ((equal type "elisp")
  9749. (let ((cmd path))
  9750. (if (or (and (org-string-nw-p
  9751. org-confirm-elisp-link-not-regexp)
  9752. (org-string-match-p
  9753. org-confirm-elisp-link-not-regexp cmd))
  9754. (not org-confirm-elisp-link-function)
  9755. (funcall org-confirm-elisp-link-function
  9756. (format "Execute \"%s\" as elisp? "
  9757. (org-add-props cmd nil
  9758. 'face 'org-warning))))
  9759. (message "%s => %s" cmd
  9760. (if (eq (string-to-char cmd) ?\()
  9761. (eval (read cmd))
  9762. (call-interactively (read cmd))))
  9763. (user-error "Abort"))))
  9764. ((equal type "id")
  9765. (require 'ord-id)
  9766. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9767. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9768. (unless (run-hook-with-args-until-success
  9769. 'org-open-link-functions path)
  9770. (if (not arg) (org-mark-ring-push)
  9771. (switch-to-buffer-other-window
  9772. (org-get-buffer-for-internal-link (current-buffer))))
  9773. (let ((cmd `(org-link-search
  9774. ,(if (member type '("custom-id" "coderef"))
  9775. (org-element-property :raw-link link)
  9776. path)
  9777. ,(cond ((equal arg '(4)) 'occur)
  9778. ((equal arg '(16)) 'org-occur))
  9779. ,(org-element-property :begin link))))
  9780. (condition-case nil
  9781. (let ((org-link-search-inhibit-query t))
  9782. (eval cmd))
  9783. (error (progn (widen) (eval cmd)))))))
  9784. (t (browse-url-at-point))))))
  9785. ;; On a footnote reference or at a footnote definition's label.
  9786. ((or (eq type 'footnote-reference)
  9787. (and (eq type 'footnote-definition)
  9788. (save-excursion
  9789. ;; Do not validate action when point is on the
  9790. ;; spaces right after the footnote label, in
  9791. ;; order to be on par with behaviour on links.
  9792. (skip-chars-forward " \t")
  9793. (let ((begin
  9794. (org-element-property :contents-begin context)))
  9795. (if begin (< (point) begin)
  9796. (= (org-element-property :post-affiliated context)
  9797. (line-beginning-position)))))))
  9798. (org-footnote-action))
  9799. (t (user-error "No link found")))))
  9800. (move-marker org-open-link-marker nil)
  9801. (run-hook-with-args 'org-follow-link-hook)))
  9802. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9803. "Offer links in the current entry and return the selected link.
  9804. If there is only one link, return it.
  9805. If NTH is an integer, return the NTH link found.
  9806. If ZERO is a string, check also this string for a link, and if
  9807. there is one, return it."
  9808. (with-current-buffer buffer
  9809. (save-excursion
  9810. (save-restriction
  9811. (widen)
  9812. (goto-char marker)
  9813. (let ((cnt ?0)
  9814. (in-emacs (if (integerp nth) nil nth))
  9815. have-zero end links link c)
  9816. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9817. (push (match-string 0 zero) links)
  9818. (setq cnt (1- cnt) have-zero t))
  9819. (save-excursion
  9820. (org-back-to-heading t)
  9821. (setq end (save-excursion (outline-next-heading) (point)))
  9822. (while (re-search-forward org-any-link-re end t)
  9823. (push (match-string 0) links))
  9824. (setq links (org-uniquify (reverse links))))
  9825. (cond
  9826. ((null links)
  9827. (message "No links"))
  9828. ((equal (length links) 1)
  9829. (setq link (car links)))
  9830. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9831. (setq link (nth (if have-zero nth (1- nth)) links)))
  9832. (t ; we have to select a link
  9833. (save-excursion
  9834. (save-window-excursion
  9835. (delete-other-windows)
  9836. (with-output-to-temp-buffer "*Select Link*"
  9837. (mapc (lambda (l)
  9838. (if (not (string-match org-bracket-link-regexp l))
  9839. (princ (format "[%c] %s\n" (incf cnt)
  9840. (org-remove-angle-brackets l)))
  9841. (if (match-end 3)
  9842. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9843. (match-string 3 l) (match-string 1 l)))
  9844. (princ (format "[%c] %s\n" (incf cnt)
  9845. (match-string 1 l))))))
  9846. links))
  9847. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9848. (message "Select link to open, RET to open all:")
  9849. (setq c (read-char-exclusive))
  9850. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9851. (when (equal c ?q) (user-error "Abort"))
  9852. (if (equal c ?\C-m)
  9853. (setq link links)
  9854. (setq nth (- c ?0))
  9855. (if have-zero (setq nth (1+ nth)))
  9856. (unless (and (integerp nth) (>= (length links) nth))
  9857. (user-error "Invalid link selection"))
  9858. (setq link (nth (1- nth) links)))))
  9859. (cons link end))))))
  9860. ;; TODO: These functions are deprecated since `org-open-at-point'
  9861. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9862. (defun org-open-file-with-system (path)
  9863. "Open file at PATH using the system way of opening it."
  9864. (org-open-file path 'system))
  9865. (defun org-open-file-with-emacs (path)
  9866. "Open file at PATH in Emacs."
  9867. (org-open-file path 'emacs))
  9868. ;;; File search
  9869. (defvar org-create-file-search-functions nil
  9870. "List of functions to construct the right search string for a file link.
  9871. These functions are called in turn with point at the location to
  9872. which the link should point.
  9873. A function in the hook should first test if it would like to
  9874. handle this file type, for example by checking the `major-mode'
  9875. or the file extension. If it decides not to handle this file, it
  9876. should just return nil to give other functions a chance. If it
  9877. does handle the file, it must return the search string to be used
  9878. when following the link. The search string will be part of the
  9879. file link, given after a double colon, and `org-open-at-point'
  9880. will automatically search for it. If special measures must be
  9881. taken to make the search successful, another function should be
  9882. added to the companion hook `org-execute-file-search-functions',
  9883. which see.
  9884. A function in this hook may also use `setq' to set the variable
  9885. `description' to provide a suggestion for the descriptive text to
  9886. be used for this link when it gets inserted into an Org-mode
  9887. buffer with \\[org-insert-link].")
  9888. (defvar org-execute-file-search-functions nil
  9889. "List of functions to execute a file search triggered by a link.
  9890. Functions added to this hook must accept a single argument, the
  9891. search string that was part of the file link, the part after the
  9892. double colon. The function must first check if it would like to
  9893. handle this search, for example by checking the `major-mode' or
  9894. the file extension. If it decides not to handle this search, it
  9895. should just return nil to give other functions a chance. If it
  9896. does handle the search, it must return a non-nil value to keep
  9897. other functions from trying.
  9898. Each function can access the current prefix argument through the
  9899. variable `current-prefix-arg'. Note that a single prefix is used
  9900. to force opening a link in Emacs, so it may be good to only use a
  9901. numeric or double prefix to guide the search function.
  9902. In case this is needed, a function in this hook can also restore
  9903. the window configuration before `org-open-at-point' was called using:
  9904. (set-window-configuration org-window-config-before-follow-link)")
  9905. (defun org-link-search (s &optional type avoid-pos stealth)
  9906. "Search for a link search option.
  9907. If S is surrounded by forward slashes, it is interpreted as a
  9908. regular expression. In org-mode files, this will create an `org-occur'
  9909. sparse tree. In ordinary files, `occur' will be used to list matches.
  9910. If the current buffer is in `dired-mode', grep will be used to search
  9911. in all files. If AVOID-POS is given, ignore matches near that position.
  9912. When optional argument STEALTH is non-nil, do not modify
  9913. visibility around point, thus ignoring `org-show-context-detail'
  9914. variable."
  9915. (let ((case-fold-search t)
  9916. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9917. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9918. (append '(("") (" ") ("\t") ("\n"))
  9919. org-emphasis-alist)
  9920. "\\|") "\\)"))
  9921. (pos (point))
  9922. words re0 re2 re4_ re4 re5 re2a re2a_ reall)
  9923. (cond
  9924. ;; First check if there are any special search functions
  9925. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9926. ;; Now try the builtin stuff
  9927. ((and (eq (aref s0 0) ?#)
  9928. (> (length s0) 1)
  9929. (let ((match (org-find-property "CUSTOM_ID" (substring s0 1))))
  9930. (and match (goto-char match) (setf type 'dedicated)))))
  9931. ((save-excursion
  9932. (goto-char (point-min))
  9933. (and
  9934. (re-search-forward
  9935. (concat "<<" (regexp-quote s0) ">>") nil t)
  9936. (setq type 'dedicated
  9937. pos (match-beginning 0))))
  9938. ;; There is an exact target for this
  9939. (goto-char pos))
  9940. ((save-excursion
  9941. (goto-char (point-min))
  9942. (and
  9943. (re-search-forward
  9944. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9945. (setq type 'dedicated pos (match-beginning 0))))
  9946. ;; Found an element with a matching #+name affiliated keyword.
  9947. (goto-char pos))
  9948. ((and (string-match "^(\\(.*\\))$" s0)
  9949. (save-excursion
  9950. (goto-char (point-min))
  9951. (and
  9952. (re-search-forward
  9953. (concat "[^[]" (regexp-quote
  9954. (format org-coderef-label-format
  9955. (match-string 1 s0))))
  9956. nil t)
  9957. (setq type 'dedicated
  9958. pos (1+ (match-beginning 0))))))
  9959. ;; There is a coderef target for this
  9960. (goto-char pos))
  9961. ((string-match "^/\\(.*\\)/$" s)
  9962. ;; A regular expression
  9963. (cond
  9964. ((derived-mode-p 'org-mode)
  9965. (org-occur (match-string 1 s)))
  9966. (t (org-do-occur (match-string 1 s)))))
  9967. ((and (derived-mode-p 'org-mode)
  9968. (or (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9969. org-link-search-must-match-exact-headline))
  9970. ;; Headline search.
  9971. (goto-char (point-min))
  9972. (cond
  9973. ((let (case-fold-search)
  9974. (re-search-forward
  9975. (let* ((wspace "[ \t]")
  9976. (wspaceopt (concat wspace "*"))
  9977. (cookie (concat "\\(?:"
  9978. wspaceopt
  9979. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9980. wspaceopt
  9981. "\\)"))
  9982. (sep (concat "\\(?:" wspace "+\\|" cookie "+\\)")))
  9983. (concat
  9984. org-outline-regexp-bol
  9985. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9986. "\\(?:\\[#.\\][ \t]+\\)?"
  9987. "\\(?:" org-comment-string "[ \t]+\\)?"
  9988. sep "*"
  9989. (mapconcat #'identity
  9990. (org-split-string (regexp-quote s))
  9991. (concat sep "+"))
  9992. sep "*"
  9993. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9994. "[ \t]*$"))
  9995. nil t))
  9996. ;; OK, found a match
  9997. (setq type 'dedicated)
  9998. (goto-char (match-beginning 0)))
  9999. ((and (not org-link-search-inhibit-query)
  10000. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10001. (y-or-n-p "No match - create this as a new heading? "))
  10002. (goto-char (point-max))
  10003. (unless (bolp) (newline))
  10004. (org-insert-heading nil t t)
  10005. (insert s "\n")
  10006. (beginning-of-line 0))
  10007. (t
  10008. (goto-char pos)
  10009. (error "No match"))))
  10010. (t
  10011. ;; A normal search string
  10012. (remove-text-properties
  10013. 0 (length s)
  10014. '(face nil mouse-face nil keymap nil fontified nil) s)
  10015. ;; Make a series of regular expressions to find a match
  10016. (setq words (org-split-string s "[ \n\r\t]+")
  10017. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  10018. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  10019. "\\)" markers)
  10020. re2a_ (concat "\\(" (mapconcat 'downcase words
  10021. "[ \t\r\n]+") "\\)[ \t\r\n]")
  10022. re2a (concat "[ \t\r\n]" re2a_)
  10023. re4_ (concat "\\(" (mapconcat 'downcase words
  10024. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  10025. re4 (concat "[^a-zA-Z_]" re4_)
  10026. re5 (concat ".*" re4)
  10027. reall (concat "\\(" re0 "\\)\\|\\(" re2 "\\)\\|\\(" re4
  10028. "\\)\\|\\(" re5 "\\)"))
  10029. (cond
  10030. ((eq type 'org-occur) (org-occur reall))
  10031. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  10032. (t (goto-char (point-min))
  10033. (setq type 'fuzzy)
  10034. (if (or (and (org-search-not-self 1 re0 nil t)
  10035. (setq type 'dedicated))
  10036. (org-search-not-self 1 re2 nil t)
  10037. (org-search-not-self 1 re2a nil t)
  10038. (org-search-not-self 1 re4 nil t)
  10039. (org-search-not-self 1 re5 nil t))
  10040. (goto-char (match-beginning 1))
  10041. (goto-char pos)
  10042. (error "No match"))))))
  10043. (and (derived-mode-p 'org-mode)
  10044. (not stealth)
  10045. (org-show-context 'link-search))
  10046. type))
  10047. (defun org-search-not-self (group &rest args)
  10048. "Execute `re-search-forward', but only accept matches that do not
  10049. enclose the position of `org-open-link-marker'."
  10050. (let ((m org-open-link-marker))
  10051. (catch 'exit
  10052. (while (apply 're-search-forward args)
  10053. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  10054. (goto-char (match-end group))
  10055. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  10056. (> (match-beginning 0) (marker-position m))
  10057. (< (match-end 0) (marker-position m)))
  10058. (save-match-data
  10059. (or (not (org-in-regexp
  10060. org-bracket-link-analytic-regexp 1))
  10061. (not (match-end 4)) ; no description
  10062. (and (<= (match-beginning 4) (point))
  10063. (>= (match-end 4) (point))))))
  10064. (throw 'exit (point))))))))
  10065. (defun org-get-buffer-for-internal-link (buffer)
  10066. "Return a buffer to be used for displaying the link target of internal links."
  10067. (cond
  10068. ((not org-display-internal-link-with-indirect-buffer)
  10069. buffer)
  10070. ((string-match "(Clone)$" (buffer-name buffer))
  10071. (message "Buffer is already a clone, not making another one")
  10072. ;; we also do not modify visibility in this case
  10073. buffer)
  10074. (t ; make a new indirect buffer for displaying the link
  10075. (let* ((bn (buffer-name buffer))
  10076. (ibn (concat bn "(Clone)"))
  10077. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10078. (with-current-buffer ib (org-overview))
  10079. ib))))
  10080. (defun org-do-occur (regexp &optional cleanup)
  10081. "Call the Emacs command `occur'.
  10082. If CLEANUP is non-nil, remove the printout of the regular expression
  10083. in the *Occur* buffer. This is useful if the regex is long and not useful
  10084. to read."
  10085. (occur regexp)
  10086. (when cleanup
  10087. (let ((cwin (selected-window)) win beg end)
  10088. (when (setq win (get-buffer-window "*Occur*"))
  10089. (select-window win))
  10090. (goto-char (point-min))
  10091. (when (re-search-forward "match[a-z]+" nil t)
  10092. (setq beg (match-end 0))
  10093. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10094. (setq end (1- (match-beginning 0)))))
  10095. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10096. (goto-char (point-min))
  10097. (select-window cwin))))
  10098. ;;; The mark ring for links jumps
  10099. (defvar org-mark-ring nil
  10100. "Mark ring for positions before jumps in Org-mode.")
  10101. (defvar org-mark-ring-last-goto nil
  10102. "Last position in the mark ring used to go back.")
  10103. ;; Fill and close the ring
  10104. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10105. (loop for i from 1 to org-mark-ring-length do
  10106. (push (make-marker) org-mark-ring))
  10107. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10108. org-mark-ring)
  10109. (defun org-mark-ring-push (&optional pos buffer)
  10110. "Put the current position or POS into the mark ring and rotate it."
  10111. (interactive)
  10112. (setq pos (or pos (point)))
  10113. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10114. (move-marker (car org-mark-ring)
  10115. (or pos (point))
  10116. (or buffer (current-buffer)))
  10117. (message "%s"
  10118. (substitute-command-keys
  10119. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10120. (defun org-mark-ring-goto (&optional n)
  10121. "Jump to the previous position in the mark ring.
  10122. With prefix arg N, jump back that many stored positions. When
  10123. called several times in succession, walk through the entire ring.
  10124. Org-mode commands jumping to a different position in the current file,
  10125. or to another Org-mode file, automatically push the old position
  10126. onto the ring."
  10127. (interactive "p")
  10128. (let (p m)
  10129. (if (eq last-command this-command)
  10130. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10131. (setq p org-mark-ring))
  10132. (setq org-mark-ring-last-goto p)
  10133. (setq m (car p))
  10134. (org-pop-to-buffer-same-window (marker-buffer m))
  10135. (goto-char m)
  10136. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10137. (defun org-remove-angle-brackets (s)
  10138. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10139. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10140. s)
  10141. (defun org-add-angle-brackets (s)
  10142. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10143. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10144. s)
  10145. (defun org-remove-double-quotes (s)
  10146. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10147. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10148. s)
  10149. ;;; Following specific links
  10150. (defun org-follow-timestamp-link ()
  10151. "Open an agenda view for the time-stamp date/range at point."
  10152. (cond
  10153. ((org-at-date-range-p t)
  10154. (let ((org-agenda-start-on-weekday)
  10155. (t1 (match-string 1))
  10156. (t2 (match-string 2)) tt1 tt2)
  10157. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10158. tt2 (time-to-days (org-time-string-to-time t2)))
  10159. (let ((org-agenda-buffer-tmp-name
  10160. (format "*Org Agenda(a:%s)"
  10161. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10162. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10163. ((org-at-timestamp-p t)
  10164. (let ((org-agenda-buffer-tmp-name
  10165. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10166. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10167. (substring (match-string 1) 0 10)))
  10168. 1)))
  10169. (t (error "This should not happen"))))
  10170. ;;; Following file links
  10171. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10172. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10173. (declare-function mailcap-mime-info
  10174. "mailcap" (string &optional request no-decode))
  10175. (defvar org-wait nil)
  10176. (defun org-open-file (path &optional in-emacs line search)
  10177. "Open the file at PATH.
  10178. First, this expands any special file name abbreviations. Then the
  10179. configuration variable `org-file-apps' is checked if it contains an
  10180. entry for this file type, and if yes, the corresponding command is launched.
  10181. If no application is found, Emacs simply visits the file.
  10182. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10183. With a double \\[universal-argument] \\[universal-argument] \
  10184. prefix arg, Org tries to avoid opening in Emacs
  10185. and to use an external application to visit the file.
  10186. Optional LINE specifies a line to go to, optional SEARCH a string
  10187. to search for. If LINE or SEARCH is given, the file will be
  10188. opened in Emacs, unless an entry from org-file-apps that makes
  10189. use of groups in a regexp matches.
  10190. If you want to change the way frames are used when following a
  10191. link, please customize `org-link-frame-setup'.
  10192. If the file does not exist, an error is thrown."
  10193. (let* ((file (if (equal path "")
  10194. buffer-file-name
  10195. (substitute-in-file-name (expand-file-name path))))
  10196. (file-apps (append org-file-apps (org-default-apps)))
  10197. (apps (org-remove-if
  10198. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10199. (apps-dlink (org-remove-if-not
  10200. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10201. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10202. (dirp (if remp nil (file-directory-p file)))
  10203. (file (if (and dirp org-open-directory-means-index-dot-org)
  10204. (concat (file-name-as-directory file) "index.org")
  10205. file))
  10206. (a-m-a-p (assq 'auto-mode apps))
  10207. (dfile (downcase file))
  10208. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10209. (link (cond ((and (eq line nil)
  10210. (eq search nil))
  10211. file)
  10212. (line
  10213. (concat file "::" (number-to-string line)))
  10214. (search
  10215. (concat file "::" search))))
  10216. (dlink (downcase link))
  10217. (old-buffer (current-buffer))
  10218. (old-pos (point))
  10219. (old-mode major-mode)
  10220. ext cmd link-match-data)
  10221. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10222. (setq ext (match-string 1 dfile))
  10223. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10224. (setq ext (match-string 1 dfile))))
  10225. (cond
  10226. ((member in-emacs '((16) system))
  10227. (setq cmd (cdr (assoc 'system apps))))
  10228. (in-emacs (setq cmd 'emacs))
  10229. (t
  10230. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10231. (and dirp (cdr (assoc 'directory apps)))
  10232. ; first, try matching against apps-dlink
  10233. ; if we get a match here, store the match data for later
  10234. (let ((match (assoc-default dlink apps-dlink
  10235. 'string-match)))
  10236. (if match
  10237. (progn (setq link-match-data (match-data))
  10238. match)
  10239. (progn (setq in-emacs (or in-emacs line search))
  10240. nil))) ; if we have no match in apps-dlink,
  10241. ; always open the file in emacs if line or search
  10242. ; is given (for backwards compatibility)
  10243. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10244. 'string-match)
  10245. (cdr (assoc ext apps))
  10246. (cdr (assoc t apps))))))
  10247. (when (eq cmd 'system)
  10248. (setq cmd (cdr (assoc 'system apps))))
  10249. (when (eq cmd 'default)
  10250. (setq cmd (cdr (assoc t apps))))
  10251. (when (eq cmd 'mailcap)
  10252. (require 'mailcap)
  10253. (mailcap-parse-mailcaps)
  10254. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10255. (command (mailcap-mime-info mime-type)))
  10256. (if (stringp command)
  10257. (setq cmd command)
  10258. (setq cmd 'emacs))))
  10259. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10260. (not (file-exists-p file))
  10261. (not org-open-non-existing-files))
  10262. (user-error "No such file: %s" file))
  10263. (cond
  10264. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10265. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10266. (while (string-match "['\"]%s['\"]" cmd)
  10267. (setq cmd (replace-match "%s" t t cmd)))
  10268. (while (string-match "%s" cmd)
  10269. (setq cmd (replace-match
  10270. (save-match-data
  10271. (shell-quote-argument
  10272. (convert-standard-filename file)))
  10273. t t cmd)))
  10274. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10275. (save-match-data
  10276. (let ((match-index 1)
  10277. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10278. (set-match-data link-match-data)
  10279. (while (<= match-index number-of-groups)
  10280. (let ((regex (concat "%" (number-to-string match-index)))
  10281. (replace-with (match-string match-index dlink)))
  10282. (while (string-match regex cmd)
  10283. (setq cmd (replace-match replace-with t t cmd))))
  10284. (setq match-index (+ match-index 1)))))
  10285. (save-window-excursion
  10286. (message "Running %s...done" cmd)
  10287. (start-process-shell-command cmd nil cmd)
  10288. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10289. ((or (stringp cmd)
  10290. (eq cmd 'emacs))
  10291. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10292. (widen)
  10293. (if line (progn (org-goto-line line)
  10294. (if (derived-mode-p 'org-mode)
  10295. (org-reveal)))
  10296. (if search (org-link-search search))))
  10297. ((consp cmd)
  10298. (let ((file (convert-standard-filename file)))
  10299. (save-match-data
  10300. (set-match-data link-match-data)
  10301. (eval cmd))))
  10302. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10303. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10304. (or (not (equal old-buffer (current-buffer)))
  10305. (not (equal old-pos (point))))
  10306. (org-mark-ring-push old-pos old-buffer))))
  10307. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10308. "This function returns non-nil if `entry' uses a regular
  10309. expression which should be matched against the whole link by
  10310. org-open-file.
  10311. It assumes that is the case when the entry uses a regular
  10312. expression which has at least one grouping construct and the
  10313. action is either a lisp form or a command string containing
  10314. '%1', i.e. using at least one subexpression match as a
  10315. parameter."
  10316. (let ((selector (car entry))
  10317. (action (cdr entry)))
  10318. (if (stringp selector)
  10319. (and (> (regexp-opt-depth selector) 0)
  10320. (or (and (stringp action)
  10321. (string-match "%[0-9]" action))
  10322. (consp action)))
  10323. nil)))
  10324. (defun org-default-apps ()
  10325. "Return the default applications for this operating system."
  10326. (cond
  10327. ((eq system-type 'darwin)
  10328. org-file-apps-defaults-macosx)
  10329. ((eq system-type 'windows-nt)
  10330. org-file-apps-defaults-windowsnt)
  10331. (t org-file-apps-defaults-gnu)))
  10332. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10333. "Convert extensions to regular expressions in the cars of LIST.
  10334. Also, weed out any non-string entries, because the return value is used
  10335. only for regexp matching.
  10336. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10337. point to the symbol `emacs', indicating that the file should
  10338. be opened in Emacs."
  10339. (append
  10340. (delq nil
  10341. (mapcar (lambda (x)
  10342. (if (not (stringp (car x)))
  10343. nil
  10344. (if (string-match "\\W" (car x))
  10345. x
  10346. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10347. list))
  10348. (if add-auto-mode
  10349. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10350. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10351. (defun org-file-remote-p (file)
  10352. "Test whether FILE specifies a location on a remote system.
  10353. Return non-nil if the location is indeed remote.
  10354. For example, the filename \"/user@host:/foo\" specifies a location
  10355. on the system \"/user@host:\"."
  10356. (cond ((fboundp 'file-remote-p)
  10357. (file-remote-p file))
  10358. ((fboundp 'tramp-handle-file-remote-p)
  10359. (tramp-handle-file-remote-p file))
  10360. ((and (boundp 'ange-ftp-name-format)
  10361. (string-match (car ange-ftp-name-format) file))
  10362. t)))
  10363. ;;;; Refiling
  10364. (defun org-get-org-file ()
  10365. "Read a filename, with default directory `org-directory'."
  10366. (let ((default (or org-default-notes-file remember-data-file)))
  10367. (read-file-name (format "File name [%s]: " default)
  10368. (file-name-as-directory org-directory)
  10369. default)))
  10370. (defun org-notes-order-reversed-p ()
  10371. "Check if the current file should receive notes in reversed order."
  10372. (cond
  10373. ((not org-reverse-note-order) nil)
  10374. ((eq t org-reverse-note-order) t)
  10375. ((not (listp org-reverse-note-order)) nil)
  10376. (t (catch 'exit
  10377. (let ((all org-reverse-note-order)
  10378. entry)
  10379. (while (setq entry (pop all))
  10380. (if (string-match (car entry) buffer-file-name)
  10381. (throw 'exit (cdr entry))))
  10382. nil)))))
  10383. (defvar org-refile-target-table nil
  10384. "The list of refile targets, created by `org-refile'.")
  10385. (defvar org-agenda-new-buffers nil
  10386. "Buffers created to visit agenda files.")
  10387. (defvar org-refile-cache nil
  10388. "Cache for refile targets.")
  10389. (defvar org-refile-markers nil
  10390. "All the markers used for caching refile locations.")
  10391. (defun org-refile-marker (pos)
  10392. "Get a new refile marker, but only if caching is in use."
  10393. (if (not org-refile-use-cache)
  10394. pos
  10395. (let ((m (make-marker)))
  10396. (move-marker m pos)
  10397. (push m org-refile-markers)
  10398. m)))
  10399. (defun org-refile-cache-clear ()
  10400. "Clear the refile cache and disable all the markers."
  10401. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10402. (setq org-refile-markers nil)
  10403. (setq org-refile-cache nil)
  10404. (message "Refile cache has been cleared"))
  10405. (defun org-refile-cache-check-set (set)
  10406. "Check if all the markers in the cache still have live buffers."
  10407. (let (marker)
  10408. (catch 'exit
  10409. (while (and set (setq marker (nth 3 (pop set))))
  10410. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10411. (when (and marker (null (marker-buffer marker)))
  10412. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10413. (sit-for 3)
  10414. (throw 'exit nil)))
  10415. t)))
  10416. (defun org-refile-cache-put (set &rest identifiers)
  10417. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10418. (let* ((key (sha1 (prin1-to-string identifiers)))
  10419. (entry (assoc key org-refile-cache)))
  10420. (if entry
  10421. (setcdr entry set)
  10422. (push (cons key set) org-refile-cache))))
  10423. (defun org-refile-cache-get (&rest identifiers)
  10424. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10425. (cond
  10426. ((not org-refile-cache) nil)
  10427. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10428. (t
  10429. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10430. org-refile-cache))))
  10431. (and set (org-refile-cache-check-set set) set)))))
  10432. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10433. "Produce a table with refile targets."
  10434. (let ((case-fold-search nil)
  10435. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10436. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10437. targets tgs txt re files f desc descre fast-path-p level pos0)
  10438. (message "Getting targets...")
  10439. (with-current-buffer (or default-buffer (current-buffer))
  10440. (while (setq entry (pop entries))
  10441. (setq files (car entry) desc (cdr entry))
  10442. (setq fast-path-p nil)
  10443. (cond
  10444. ((null files) (setq files (list (current-buffer))))
  10445. ((eq files 'org-agenda-files)
  10446. (setq files (org-agenda-files 'unrestricted)))
  10447. ((and (symbolp files) (fboundp files))
  10448. (setq files (funcall files)))
  10449. ((and (symbolp files) (boundp files))
  10450. (setq files (symbol-value files))))
  10451. (if (stringp files) (setq files (list files)))
  10452. (cond
  10453. ((eq (car desc) :tag)
  10454. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10455. ((eq (car desc) :todo)
  10456. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10457. ((eq (car desc) :regexp)
  10458. (setq descre (cdr desc)))
  10459. ((eq (car desc) :level)
  10460. (setq descre (concat "^\\*\\{" (number-to-string
  10461. (if org-odd-levels-only
  10462. (1- (* 2 (cdr desc)))
  10463. (cdr desc)))
  10464. "\\}[ \t]")))
  10465. ((eq (car desc) :maxlevel)
  10466. (setq fast-path-p t)
  10467. (setq descre (concat "^\\*\\{1," (number-to-string
  10468. (if org-odd-levels-only
  10469. (1- (* 2 (cdr desc)))
  10470. (cdr desc)))
  10471. "\\}[ \t]")))
  10472. (t (error "Bad refiling target description %s" desc)))
  10473. (while (setq f (pop files))
  10474. (with-current-buffer
  10475. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10476. (or
  10477. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10478. (progn
  10479. (if (bufferp f) (setq f (buffer-file-name
  10480. (buffer-base-buffer f))))
  10481. (setq f (and f (expand-file-name f)))
  10482. (if (eq org-refile-use-outline-path 'file)
  10483. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10484. (save-excursion
  10485. (save-restriction
  10486. (widen)
  10487. (goto-char (point-min))
  10488. (while (re-search-forward descre nil t)
  10489. (goto-char (setq pos0 (point-at-bol)))
  10490. (catch 'next
  10491. (when org-refile-target-verify-function
  10492. (save-match-data
  10493. (or (funcall org-refile-target-verify-function)
  10494. (throw 'next t))))
  10495. (when (and (looking-at org-complex-heading-regexp)
  10496. (not (member (match-string 4) excluded-entries))
  10497. (match-string 4))
  10498. (setq level (org-reduced-level
  10499. (- (match-end 1) (match-beginning 1)))
  10500. txt (org-link-display-format (match-string 4))
  10501. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10502. re (format org-complex-heading-regexp-format
  10503. (regexp-quote (match-string 4))))
  10504. (when org-refile-use-outline-path
  10505. (setq txt (mapconcat
  10506. 'org-protect-slash
  10507. (append
  10508. (if (eq org-refile-use-outline-path
  10509. 'file)
  10510. (list (file-name-nondirectory
  10511. (buffer-file-name
  10512. (buffer-base-buffer))))
  10513. (if (eq org-refile-use-outline-path
  10514. 'full-file-path)
  10515. (list (buffer-file-name
  10516. (buffer-base-buffer)))))
  10517. (org-get-outline-path fast-path-p
  10518. level txt)
  10519. (list txt))
  10520. "/")))
  10521. (push (list txt f re (org-refile-marker (point)))
  10522. tgs)))
  10523. (when (= (point) pos0)
  10524. ;; verification function has not moved point
  10525. (goto-char (point-at-eol))))))))
  10526. (when org-refile-use-cache
  10527. (org-refile-cache-put tgs (buffer-file-name) descre))
  10528. (setq targets (append tgs targets))))))
  10529. (message "Getting targets...done")
  10530. (nreverse targets)))
  10531. (defun org-protect-slash (s)
  10532. (while (string-match "/" s)
  10533. (setq s (replace-match "\\" t t s)))
  10534. s)
  10535. (defvar org-olpa (make-vector 20 nil))
  10536. (defun org-get-outline-path (&optional fastp level heading)
  10537. "Return the outline path to the current entry, as a list.
  10538. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10539. routine which makes outline path derivations for an entire file,
  10540. avoiding backtracing. Refile target collection makes use of that."
  10541. (if fastp
  10542. (progn
  10543. (if (> level 19)
  10544. (error "Outline path failure, more than 19 levels"))
  10545. (loop for i from level upto 19 do
  10546. (aset org-olpa i nil))
  10547. (prog1
  10548. (delq nil (append org-olpa nil))
  10549. (aset org-olpa level heading)))
  10550. (let (rtn case-fold-search)
  10551. (save-excursion
  10552. (save-restriction
  10553. (widen)
  10554. (while (org-up-heading-safe)
  10555. (when (looking-at org-complex-heading-regexp)
  10556. (push (org-trim
  10557. (replace-regexp-in-string
  10558. ;; Remove statistical/checkboxes cookies
  10559. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10560. (org-match-string-no-properties 4)))
  10561. rtn)))
  10562. rtn)))))
  10563. (defun org-format-outline-path (path &optional width prefix separator)
  10564. "Format the outline path PATH for display.
  10565. WIDTH is the maximum number of characters that is available.
  10566. PREFIX is a prefix to be included in the returned string,
  10567. such as the file name.
  10568. SEPARATOR is inserted between the different parts of the path,
  10569. the default is \"/\"."
  10570. (setq width (or width 79))
  10571. (if prefix (setq width (- width (length prefix))))
  10572. (if (not path)
  10573. (or prefix "")
  10574. (let* ((nsteps (length path))
  10575. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10576. (maxwidth (if (<= total-width width)
  10577. 10000 ;; everything fits
  10578. ;; we need to shorten the level headings
  10579. (/ (- width nsteps) nsteps)))
  10580. (org-odd-levels-only nil)
  10581. (n 0)
  10582. (total (1+ (length prefix))))
  10583. (setq maxwidth (max maxwidth 10))
  10584. (concat prefix
  10585. (if prefix (or separator "/"))
  10586. (mapconcat
  10587. (lambda (h)
  10588. (setq n (1+ n))
  10589. (if (and (= n nsteps) (< maxwidth 10000))
  10590. (setq maxwidth (- total-width total)))
  10591. (if (< (length h) maxwidth)
  10592. (progn (setq total (+ total (length h) 1)) h)
  10593. (setq h (substring h 0 (- maxwidth 2))
  10594. total (+ total maxwidth 1))
  10595. (if (string-match "[ \t]+\\'" h)
  10596. (setq h (substring h 0 (match-beginning 0))))
  10597. (setq h (concat h "..")))
  10598. (org-add-props h nil 'face
  10599. (nth (% (1- n) org-n-level-faces)
  10600. org-level-faces))
  10601. h)
  10602. path (or separator "/"))))))
  10603. (defun org-display-outline-path (&optional file current separator just-return-string)
  10604. "Display the current outline path in the echo area.
  10605. If FILE is non-nil, prepend the output with the file name.
  10606. If CURRENT is non-nil, append the current heading to the output.
  10607. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10608. the different parts of the path and defaults to \"/\".
  10609. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10610. (interactive "P")
  10611. (let* (case-fold-search
  10612. (bfn (buffer-file-name (buffer-base-buffer)))
  10613. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10614. res)
  10615. (if current (setq path (append path
  10616. (save-excursion
  10617. (org-back-to-heading t)
  10618. (if (looking-at org-complex-heading-regexp)
  10619. (list (match-string 4)))))))
  10620. (setq res
  10621. (org-format-outline-path
  10622. path
  10623. (1- (frame-width))
  10624. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10625. separator))
  10626. (if just-return-string
  10627. (org-no-properties res)
  10628. (org-unlogged-message "%s" res))))
  10629. (defvar org-refile-history nil
  10630. "History for refiling operations.")
  10631. (defvar org-after-refile-insert-hook nil
  10632. "Hook run after `org-refile' has inserted its stuff at the new location.
  10633. Note that this is still *before* the stuff will be removed from
  10634. the *old* location.")
  10635. (defvar org-capture-last-stored-marker)
  10636. (defvar org-refile-keep nil
  10637. "Non-nil means `org-refile' will copy instead of refile.")
  10638. (defun org-copy ()
  10639. "Like `org-refile', but copy."
  10640. (interactive)
  10641. (let ((org-refile-keep t))
  10642. (funcall 'org-refile nil nil nil "Copy")))
  10643. (defun org-refile (&optional arg default-buffer rfloc msg)
  10644. "Move the entry or entries at point to another heading.
  10645. The list of target headings is compiled using the information in
  10646. `org-refile-targets', which see.
  10647. At the target location, the entry is filed as a subitem of the
  10648. target heading. Depending on `org-reverse-note-order', the new
  10649. subitem will either be the first or the last subitem.
  10650. If there is an active region, all entries in that region will be
  10651. refiled. However, the region must fulfill the requirement that
  10652. the first heading sets the top-level of the moved text.
  10653. With prefix arg ARG, the command will only visit the target
  10654. location and not actually move anything.
  10655. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10656. refiling operation has put the subtree.
  10657. With a numeric prefix argument of `2', refile to the running clock.
  10658. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10659. being set to `t' and copy to the target location, don't move it.
  10660. Beware that keeping refiled entries may result in duplicated ID
  10661. properties.
  10662. RFLOC can be a refile location obtained in a different way.
  10663. MSG is a string to replace \"Refile\" in the default prompt with
  10664. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10665. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10666. If you are using target caching (see `org-refile-use-cache'), you
  10667. have to clear the target cache in order to find new targets.
  10668. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10669. prefix argument (`C-u C-u C-u C-c C-w')."
  10670. (interactive "P")
  10671. (if (member arg '(0 (64)))
  10672. (org-refile-cache-clear)
  10673. (let* ((actionmsg (cond (msg msg)
  10674. ((equal arg 3) "Refile (and keep)")
  10675. (t "Refile")))
  10676. (cbuf (current-buffer))
  10677. (regionp (org-region-active-p))
  10678. (region-start (and regionp (region-beginning)))
  10679. (region-end (and regionp (region-end)))
  10680. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10681. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10682. pos it nbuf file re level reversed)
  10683. (setq last-command nil)
  10684. (when regionp
  10685. (goto-char region-start)
  10686. (or (bolp) (goto-char (point-at-bol)))
  10687. (setq region-start (point))
  10688. (unless (or (org-kill-is-subtree-p
  10689. (buffer-substring region-start region-end))
  10690. (prog1 org-refile-active-region-within-subtree
  10691. (let ((s (point-at-eol)))
  10692. (org-toggle-heading)
  10693. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10694. (user-error "The region is not a (sequence of) subtree(s)")))
  10695. (if (equal arg '(16))
  10696. (org-refile-goto-last-stored)
  10697. (when (or
  10698. (and (equal arg 2)
  10699. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10700. (prog1
  10701. (setq it (list (or org-clock-heading "running clock")
  10702. (buffer-file-name
  10703. (marker-buffer org-clock-hd-marker))
  10704. ""
  10705. (marker-position org-clock-hd-marker)))
  10706. (setq arg nil)))
  10707. (setq it (or rfloc
  10708. (let (heading-text)
  10709. (save-excursion
  10710. (unless (and arg (listp arg))
  10711. (org-back-to-heading t)
  10712. (setq heading-text
  10713. (replace-regexp-in-string
  10714. org-bracket-link-regexp
  10715. "\\3"
  10716. (nth 4 (org-heading-components)))))
  10717. (org-refile-get-location
  10718. (cond ((and arg (listp arg)) "Goto")
  10719. (regionp (concat actionmsg " region to"))
  10720. (t (concat actionmsg " subtree \""
  10721. heading-text "\" to")))
  10722. default-buffer
  10723. (and (not (equal '(4) arg))
  10724. org-refile-allow-creating-parent-nodes)
  10725. arg))))))
  10726. (setq file (nth 1 it)
  10727. re (nth 2 it)
  10728. pos (nth 3 it))
  10729. (if (and (not arg)
  10730. pos
  10731. (equal (buffer-file-name) file)
  10732. (if regionp
  10733. (and (>= pos region-start)
  10734. (<= pos region-end))
  10735. (and (>= pos (point))
  10736. (< pos (save-excursion
  10737. (org-end-of-subtree t t))))))
  10738. (error "Cannot refile to position inside the tree or region"))
  10739. (setq nbuf (or (find-buffer-visiting file)
  10740. (find-file-noselect file)))
  10741. (if (and arg (not (equal arg 3)))
  10742. (progn
  10743. (org-pop-to-buffer-same-window nbuf)
  10744. (goto-char pos)
  10745. (org-show-context 'org-goto))
  10746. (if regionp
  10747. (progn
  10748. (org-kill-new (buffer-substring region-start region-end))
  10749. (org-save-markers-in-region region-start region-end))
  10750. (org-copy-subtree 1 nil t))
  10751. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10752. (find-file-noselect file)))
  10753. (setq reversed (org-notes-order-reversed-p))
  10754. (save-excursion
  10755. (save-restriction
  10756. (widen)
  10757. (if pos
  10758. (progn
  10759. (goto-char pos)
  10760. (looking-at org-outline-regexp)
  10761. (setq level (org-get-valid-level (funcall outline-level) 1))
  10762. (goto-char
  10763. (if reversed
  10764. (or (outline-next-heading) (point-max))
  10765. (or (save-excursion (org-get-next-sibling))
  10766. (org-end-of-subtree t t)
  10767. (point-max)))))
  10768. (setq level 1)
  10769. (if (not reversed)
  10770. (goto-char (point-max))
  10771. (goto-char (point-min))
  10772. (or (outline-next-heading) (goto-char (point-max)))))
  10773. (if (not (bolp)) (newline))
  10774. (org-paste-subtree level nil nil t)
  10775. (when org-log-refile
  10776. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10777. (unless (eq org-log-refile 'note)
  10778. (save-excursion (org-add-log-note))))
  10779. (and org-auto-align-tags
  10780. (let ((org-loop-over-headlines-in-active-region nil))
  10781. (org-set-tags nil t)))
  10782. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10783. :last-refile)))
  10784. (when bookmark-name
  10785. (with-demoted-errors
  10786. (bookmark-set bookmark-name))))
  10787. ;; If we are refiling for capture, make sure that the
  10788. ;; last-capture pointers point here
  10789. (when (org-bound-and-true-p org-refile-for-capture)
  10790. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10791. :last-capture-marker)))
  10792. (when bookmark-name
  10793. (with-demoted-errors
  10794. (bookmark-set bookmark-name))))
  10795. (move-marker org-capture-last-stored-marker (point)))
  10796. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10797. (run-hooks 'org-after-refile-insert-hook))))
  10798. (unless org-refile-keep
  10799. (if regionp
  10800. (delete-region (point) (+ (point) (- region-end region-start)))
  10801. (delete-region
  10802. (and (org-back-to-heading t) (point))
  10803. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10804. (when (featurep 'org-inlinetask)
  10805. (org-inlinetask-remove-END-maybe))
  10806. (setq org-markers-to-move nil)
  10807. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10808. (defun org-refile-goto-last-stored ()
  10809. "Go to the location where the last refile was stored."
  10810. (interactive)
  10811. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10812. (message "This is the location of the last refile"))
  10813. (defun org-refile--get-location (refloc tbl)
  10814. "When user refile to REFLOC, find the associated target in TBL.
  10815. Also check `org-refile-target-table'."
  10816. (car (delq
  10817. nil
  10818. (mapcar
  10819. (lambda (r) (or (assoc r tbl)
  10820. (assoc r org-refile-target-table)))
  10821. (list (replace-regexp-in-string "/$" "" refloc)
  10822. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10823. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10824. no-exclude)
  10825. "Prompt the user for a refile location, using PROMPT.
  10826. PROMPT should not be suffixed with a colon and a space, because
  10827. this function appends the default value from
  10828. `org-refile-history' automatically, if that is not empty.
  10829. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10830. this is used for the GOTO interface."
  10831. (let ((org-refile-targets org-refile-targets)
  10832. (org-refile-use-outline-path org-refile-use-outline-path)
  10833. excluded-entries)
  10834. (when (and (derived-mode-p 'org-mode)
  10835. (not org-refile-use-cache)
  10836. (not no-exclude))
  10837. (org-map-tree
  10838. (lambda()
  10839. (setq excluded-entries
  10840. (append excluded-entries (list (org-get-heading t t)))))))
  10841. (setq org-refile-target-table
  10842. (org-refile-get-targets default-buffer excluded-entries)))
  10843. (unless org-refile-target-table
  10844. (user-error "No refile targets"))
  10845. (let* ((cbuf (current-buffer))
  10846. (partial-completion-mode nil)
  10847. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10848. (cfunc (if (and org-refile-use-outline-path
  10849. org-outline-path-complete-in-steps)
  10850. 'org-olpath-completing-read
  10851. 'org-icompleting-read))
  10852. (extra (if org-refile-use-outline-path "/" ""))
  10853. (cbnex (concat (buffer-name) extra))
  10854. (filename (and cfn (expand-file-name cfn)))
  10855. (tbl (mapcar
  10856. (lambda (x)
  10857. (if (and (not (member org-refile-use-outline-path
  10858. '(file full-file-path)))
  10859. (not (equal filename (nth 1 x))))
  10860. (cons (concat (car x) extra " ("
  10861. (file-name-nondirectory (nth 1 x)) ")")
  10862. (cdr x))
  10863. (cons (concat (car x) extra) (cdr x))))
  10864. org-refile-target-table))
  10865. (completion-ignore-case t)
  10866. cdef
  10867. (prompt (concat prompt
  10868. (or (and (car org-refile-history)
  10869. (concat " (default " (car org-refile-history) ")"))
  10870. (and (assoc cbnex tbl) (setq cdef cbnex)
  10871. (concat " (default " cbnex ")"))) ": "))
  10872. pa answ parent-target child parent old-hist)
  10873. (setq old-hist org-refile-history)
  10874. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10875. nil 'org-refile-history (or cdef (car org-refile-history))))
  10876. (if (setq pa (org-refile--get-location answ tbl))
  10877. (progn
  10878. (org-refile-check-position pa)
  10879. (when (or (not org-refile-history)
  10880. (not (eq old-hist org-refile-history))
  10881. (not (equal (car pa) (car org-refile-history))))
  10882. (setq org-refile-history
  10883. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10884. org-refile-history
  10885. (cdr org-refile-history))))
  10886. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10887. (pop org-refile-history)))
  10888. pa)
  10889. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10890. (progn
  10891. (setq parent (match-string 1 answ)
  10892. child (match-string 2 answ))
  10893. (setq parent-target (org-refile--get-location parent tbl))
  10894. (when (and parent-target
  10895. (or (eq new-nodes t)
  10896. (and (eq new-nodes 'confirm)
  10897. (y-or-n-p (format "Create new node \"%s\"? "
  10898. child)))))
  10899. (org-refile-new-child parent-target child)))
  10900. (user-error "Invalid target location")))))
  10901. (declare-function org-string-nw-p "org-macs" (s))
  10902. (defun org-refile-check-position (refile-pointer)
  10903. "Check if the refile pointer matches the headline to which it points."
  10904. (let* ((file (nth 1 refile-pointer))
  10905. (re (nth 2 refile-pointer))
  10906. (pos (nth 3 refile-pointer))
  10907. buffer)
  10908. (if (and (not (markerp pos)) (not file))
  10909. (user-error "Please indicate a target file in the refile path")
  10910. (when (org-string-nw-p re)
  10911. (setq buffer (if (markerp pos)
  10912. (marker-buffer pos)
  10913. (or (find-buffer-visiting file)
  10914. (find-file-noselect file))))
  10915. (with-current-buffer buffer
  10916. (save-excursion
  10917. (save-restriction
  10918. (widen)
  10919. (goto-char pos)
  10920. (beginning-of-line 1)
  10921. (unless (org-looking-at-p re)
  10922. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10923. (defun org-refile-new-child (parent-target child)
  10924. "Use refile target PARENT-TARGET to add new CHILD below it."
  10925. (unless parent-target
  10926. (error "Cannot find parent for new node"))
  10927. (let ((file (nth 1 parent-target))
  10928. (pos (nth 3 parent-target))
  10929. level)
  10930. (with-current-buffer (or (find-buffer-visiting file)
  10931. (find-file-noselect file))
  10932. (save-excursion
  10933. (save-restriction
  10934. (widen)
  10935. (if pos
  10936. (goto-char pos)
  10937. (goto-char (point-max))
  10938. (if (not (bolp)) (newline)))
  10939. (when (looking-at org-outline-regexp)
  10940. (setq level (funcall outline-level))
  10941. (org-end-of-subtree t t))
  10942. (org-back-over-empty-lines)
  10943. (insert "\n" (make-string
  10944. (if pos (org-get-valid-level level 1) 1) ?*)
  10945. " " child "\n")
  10946. (beginning-of-line 0)
  10947. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10948. (defun org-olpath-completing-read (prompt collection &rest args)
  10949. "Read an outline path like a file name."
  10950. (let ((thetable collection)
  10951. (org-completion-use-ido nil) ; does not work with ido.
  10952. (org-completion-use-iswitchb nil)) ; or iswitchb
  10953. (apply
  10954. 'org-icompleting-read prompt
  10955. (lambda (string predicate &optional flag)
  10956. (let (rtn r f (l (length string)))
  10957. (cond
  10958. ((eq flag nil)
  10959. ;; try completion
  10960. (try-completion string thetable))
  10961. ((eq flag t)
  10962. ;; all-completions
  10963. (setq rtn (all-completions string thetable predicate))
  10964. (mapcar
  10965. (lambda (x)
  10966. (setq r (substring x l))
  10967. (if (string-match " ([^)]*)$" x)
  10968. (setq f (match-string 0 x))
  10969. (setq f ""))
  10970. (if (string-match "/" r)
  10971. (concat string (substring r 0 (match-end 0)) f)
  10972. x))
  10973. rtn))
  10974. ((eq flag 'lambda)
  10975. ;; exact match?
  10976. (assoc string thetable)))))
  10977. args)))
  10978. ;;;; Dynamic blocks
  10979. (defun org-find-dblock (name)
  10980. "Find the first dynamic block with name NAME in the buffer.
  10981. If not found, stay at current position and return nil."
  10982. (let ((case-fold-search t) pos)
  10983. (save-excursion
  10984. (goto-char (point-min))
  10985. (setq pos (and (re-search-forward
  10986. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10987. (match-beginning 0))))
  10988. (if pos (goto-char pos))
  10989. pos))
  10990. (defun org-create-dblock (plist)
  10991. "Create a dynamic block section, with parameters taken from PLIST.
  10992. PLIST must contain a :name entry which is used as the name of the block."
  10993. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10994. (end-of-line 1)
  10995. (newline))
  10996. (let ((col (current-column))
  10997. (name (plist-get plist :name)))
  10998. (insert "#+BEGIN: " name)
  10999. (while plist
  11000. (if (eq (car plist) :name)
  11001. (setq plist (cddr plist))
  11002. (insert " " (prin1-to-string (pop plist)))))
  11003. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11004. (beginning-of-line -2)))
  11005. (defun org-prepare-dblock ()
  11006. "Prepare dynamic block for refresh.
  11007. This empties the block, puts the cursor at the insert position and returns
  11008. the property list including an extra property :name with the block name."
  11009. (unless (looking-at org-dblock-start-re)
  11010. (user-error "Not at a dynamic block"))
  11011. (let* ((begdel (1+ (match-end 0)))
  11012. (name (org-no-properties (match-string 1)))
  11013. (params (append (list :name name)
  11014. (read (concat "(" (match-string 3) ")")))))
  11015. (save-excursion
  11016. (beginning-of-line 1)
  11017. (skip-chars-forward " \t")
  11018. (setq params (plist-put params :indentation-column (current-column))))
  11019. (unless (re-search-forward org-dblock-end-re nil t)
  11020. (error "Dynamic block not terminated"))
  11021. (setq params
  11022. (append params
  11023. (list :content (buffer-substring
  11024. begdel (match-beginning 0)))))
  11025. (delete-region begdel (match-beginning 0))
  11026. (goto-char begdel)
  11027. (open-line 1)
  11028. params))
  11029. (defun org-map-dblocks (&optional command)
  11030. "Apply COMMAND to all dynamic blocks in the current buffer.
  11031. If COMMAND is not given, use `org-update-dblock'."
  11032. (let ((cmd (or command 'org-update-dblock)))
  11033. (save-excursion
  11034. (goto-char (point-min))
  11035. (while (re-search-forward org-dblock-start-re nil t)
  11036. (goto-char (match-beginning 0))
  11037. (save-excursion
  11038. (condition-case nil
  11039. (funcall cmd)
  11040. (error (message "Error during update of dynamic block"))))
  11041. (unless (re-search-forward org-dblock-end-re nil t)
  11042. (error "Dynamic block not terminated"))))))
  11043. (defun org-dblock-update (&optional arg)
  11044. "User command for updating dynamic blocks.
  11045. Update the dynamic block at point. With prefix ARG, update all dynamic
  11046. blocks in the buffer."
  11047. (interactive "P")
  11048. (if arg
  11049. (org-update-all-dblocks)
  11050. (or (looking-at org-dblock-start-re)
  11051. (org-beginning-of-dblock))
  11052. (org-update-dblock)))
  11053. (defun org-update-dblock ()
  11054. "Update the dynamic block at point.
  11055. This means to empty the block, parse for parameters and then call
  11056. the correct writing function."
  11057. (interactive)
  11058. (save-excursion
  11059. (let* ((win (selected-window))
  11060. (pos (point))
  11061. (line (org-current-line))
  11062. (params (org-prepare-dblock))
  11063. (name (plist-get params :name))
  11064. (indent (plist-get params :indentation-column))
  11065. (cmd (intern (concat "org-dblock-write:" name))))
  11066. (message "Updating dynamic block `%s' at line %d..." name line)
  11067. (funcall cmd params)
  11068. (message "Updating dynamic block `%s' at line %d...done" name line)
  11069. (goto-char pos)
  11070. (when (and indent (> indent 0))
  11071. (setq indent (make-string indent ?\ ))
  11072. (save-excursion
  11073. (select-window win)
  11074. (org-beginning-of-dblock)
  11075. (forward-line 1)
  11076. (while (not (looking-at org-dblock-end-re))
  11077. (insert indent)
  11078. (beginning-of-line 2))
  11079. (when (looking-at org-dblock-end-re)
  11080. (and (looking-at "[ \t]+")
  11081. (replace-match ""))
  11082. (insert indent)))))))
  11083. (defun org-beginning-of-dblock ()
  11084. "Find the beginning of the dynamic block at point.
  11085. Error if there is no such block at point."
  11086. (let ((pos (point))
  11087. beg)
  11088. (end-of-line 1)
  11089. (if (and (re-search-backward org-dblock-start-re nil t)
  11090. (setq beg (match-beginning 0))
  11091. (re-search-forward org-dblock-end-re nil t)
  11092. (> (match-end 0) pos))
  11093. (goto-char beg)
  11094. (goto-char pos)
  11095. (error "Not in a dynamic block"))))
  11096. (defun org-update-all-dblocks ()
  11097. "Update all dynamic blocks in the buffer.
  11098. This function can be used in a hook."
  11099. (interactive)
  11100. (when (derived-mode-p 'org-mode)
  11101. (org-map-dblocks 'org-update-dblock)))
  11102. ;;;; Completion
  11103. (declare-function org-export-backend-name "org-export" (cl-x))
  11104. (declare-function org-export-backend-options "org-export" (cl-x))
  11105. (defun org-get-export-keywords ()
  11106. "Return a list of all currently understood export keywords.
  11107. Export keywords include options, block names, attributes and
  11108. keywords relative to each registered export back-end."
  11109. (let (keywords)
  11110. (dolist (backend
  11111. (org-bound-and-true-p org-export--registered-backends)
  11112. (delq nil keywords))
  11113. ;; Back-end name (for keywords, like #+LATEX:)
  11114. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11115. (dolist (option-entry (org-export-backend-options backend))
  11116. ;; Back-end options.
  11117. (push (nth 1 option-entry) keywords)))))
  11118. (defconst org-options-keywords
  11119. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11120. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11121. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11122. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11123. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11124. (defcustom org-structure-template-alist
  11125. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11126. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11127. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11128. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11129. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11130. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11131. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11132. ("L" "#+LaTeX: ")
  11133. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11134. ("H" "#+HTML: ")
  11135. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11136. ("A" "#+ASCII: ")
  11137. ("i" "#+INDEX: ?")
  11138. ("I" "#+INCLUDE: %file ?"))
  11139. "Structure completion elements.
  11140. This is a list of abbreviation keys and values. The value gets inserted
  11141. if you type `<' followed by the key and then press the completion key,
  11142. usually `TAB'. %file will be replaced by a file name after prompting
  11143. for the file using completion. The cursor will be placed at the position
  11144. of the `?` in the template.
  11145. There are two templates for each key, the first uses the original Org syntax,
  11146. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11147. the default when the /org-mtags.el/ module has been loaded. See also the
  11148. variable `org-mtags-prefer-muse-templates'."
  11149. :group 'org-completion
  11150. :type '(repeat
  11151. (list
  11152. (string :tag "Key")
  11153. (string :tag "Template")))
  11154. :version "25.1"
  11155. :package-version '(Org . "8.3"))
  11156. (defun org-try-structure-completion ()
  11157. "Try to complete a structure template before point.
  11158. This looks for strings like \"<e\" on an otherwise empty line and
  11159. expands them."
  11160. (let ((l (buffer-substring (point-at-bol) (point)))
  11161. a)
  11162. (when (and (looking-at "[ \t]*$")
  11163. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11164. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11165. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11166. (match-beginning 1)) a)
  11167. t)))
  11168. (defun org-complete-expand-structure-template (start cell)
  11169. "Expand a structure template."
  11170. (let ((rpl (nth 1 cell))
  11171. (ind ""))
  11172. (delete-region start (point))
  11173. (when (string-match "\\`[ \t]*#\\+" rpl)
  11174. (cond
  11175. ((bolp))
  11176. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11177. (setq ind (buffer-substring (point-at-bol) (point))))
  11178. (t (newline))))
  11179. (setq start (point))
  11180. (if (string-match "%file" rpl)
  11181. (setq rpl (replace-match
  11182. (concat
  11183. "\""
  11184. (save-match-data
  11185. (abbreviate-file-name (read-file-name "Include file: ")))
  11186. "\"")
  11187. t t rpl)))
  11188. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11189. (concat "\n" ind)))
  11190. (insert rpl)
  11191. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11192. ;;;; TODO, DEADLINE, Comments
  11193. (defun org-toggle-comment ()
  11194. "Change the COMMENT state of an entry."
  11195. (interactive)
  11196. (save-excursion
  11197. (org-back-to-heading)
  11198. (looking-at org-complex-heading-regexp)
  11199. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11200. (skip-chars-forward " \t")
  11201. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11202. (if (org-in-commented-heading-p t)
  11203. (delete-region (point)
  11204. (progn (search-forward " " (line-end-position) 'move)
  11205. (skip-chars-forward " \t")
  11206. (point)))
  11207. (insert org-comment-string)
  11208. (unless (eolp) (insert " ")))))
  11209. (defvar org-last-todo-state-is-todo nil
  11210. "This is non-nil when the last TODO state change led to a TODO state.
  11211. If the last change removed the TODO tag or switched to DONE, then
  11212. this is nil.")
  11213. (defvar org-setting-tags nil) ; dynamically skipped
  11214. (defvar org-todo-setup-filter-hook nil
  11215. "Hook for functions that pre-filter todo specs.
  11216. Each function takes a todo spec and returns either nil or the spec
  11217. transformed into canonical form." )
  11218. (defvar org-todo-get-default-hook nil
  11219. "Hook for functions that get a default item for todo.
  11220. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11221. nil or a string to be used for the todo mark." )
  11222. (defvar org-agenda-headline-snapshot-before-repeat)
  11223. (defun org-current-effective-time ()
  11224. "Return current time adjusted for `org-extend-today-until' variable."
  11225. (let* ((ct (org-current-time))
  11226. (dct (decode-time ct))
  11227. (ct1
  11228. (cond
  11229. (org-use-last-clock-out-time-as-effective-time
  11230. (or (org-clock-get-last-clock-out-time) ct))
  11231. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11232. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11233. (t ct))))
  11234. ct1))
  11235. (defun org-todo-yesterday (&optional arg)
  11236. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11237. (interactive "P")
  11238. (if (eq major-mode 'org-agenda-mode)
  11239. (apply 'org-agenda-todo-yesterday arg)
  11240. (let* ((org-use-effective-time t)
  11241. (hour (third (decode-time
  11242. (org-current-time))))
  11243. (org-extend-today-until (1+ hour)))
  11244. (org-todo arg))))
  11245. (defvar org-block-entry-blocking ""
  11246. "First entry preventing the TODO state change.")
  11247. (defun org-cancel-repeater ()
  11248. "Cancel a repeater by setting its numeric value to zero."
  11249. (interactive)
  11250. (save-excursion
  11251. (org-back-to-heading t)
  11252. (let ((bound1 (point))
  11253. (bound0 (save-excursion (outline-next-heading) (point))))
  11254. (when (re-search-forward
  11255. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11256. org-deadline-time-regexp "\\)\\|\\("
  11257. org-ts-regexp "\\)")
  11258. bound0 t)
  11259. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11260. (replace-match "0" t nil nil 1))))))
  11261. (defun org-todo (&optional arg)
  11262. "Change the TODO state of an item.
  11263. The state of an item is given by a keyword at the start of the heading,
  11264. like
  11265. *** TODO Write paper
  11266. *** DONE Call mom
  11267. The different keywords are specified in the variable `org-todo-keywords'.
  11268. By default the available states are \"TODO\" and \"DONE\".
  11269. So for this example: when the item starts with TODO, it is changed to DONE.
  11270. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11271. DONE are present, add TODO at the beginning of the heading.
  11272. With \\[universal-argument] prefix arg, use completion to determine the new \
  11273. state.
  11274. With numeric prefix arg, switch to that state.
  11275. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11276. keywords (nextset).
  11277. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11278. With a numeric prefix arg of 0, inhibit note taking for the change.
  11279. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11280. When called through ELisp, arg is also interpreted in the following way:
  11281. 'none -> empty state
  11282. \"\"(empty string) -> switch to empty state
  11283. 'done -> switch to DONE
  11284. 'nextset -> switch to the next set of keywords
  11285. 'previousset -> switch to the previous set of keywords
  11286. \"WAITING\" -> switch to the specified keyword, but only if it
  11287. really is a member of `org-todo-keywords'."
  11288. (interactive "P")
  11289. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11290. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11291. 'region-start-level 'region))
  11292. org-loop-over-headlines-in-active-region)
  11293. (org-map-entries
  11294. `(org-todo ,arg)
  11295. org-loop-over-headlines-in-active-region
  11296. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11297. (if (equal arg '(16)) (setq arg 'nextset))
  11298. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11299. (let ((org-blocker-hook org-blocker-hook)
  11300. commentp
  11301. case-fold-search)
  11302. (when (equal arg '(64))
  11303. (setq arg nil org-blocker-hook nil))
  11304. (when (and org-blocker-hook
  11305. (or org-inhibit-blocking
  11306. (org-entry-get nil "NOBLOCKING")))
  11307. (setq org-blocker-hook nil))
  11308. (save-excursion
  11309. (catch 'exit
  11310. (org-back-to-heading t)
  11311. (when (org-in-commented-heading-p t)
  11312. (org-toggle-comment)
  11313. (setq commentp t))
  11314. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11315. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11316. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11317. (let* ((match-data (match-data))
  11318. (startpos (point-at-bol))
  11319. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11320. (org-log-done org-log-done)
  11321. (org-log-repeat org-log-repeat)
  11322. (org-todo-log-states org-todo-log-states)
  11323. (org-inhibit-logging
  11324. (if (equal arg 0)
  11325. (progn (setq arg nil) 'note) org-inhibit-logging))
  11326. (this (match-string 1))
  11327. (hl-pos (match-beginning 0))
  11328. (head (org-get-todo-sequence-head this))
  11329. (ass (assoc head org-todo-kwd-alist))
  11330. (interpret (nth 1 ass))
  11331. (done-word (nth 3 ass))
  11332. (final-done-word (nth 4 ass))
  11333. (org-last-state (or this ""))
  11334. (completion-ignore-case t)
  11335. (member (member this org-todo-keywords-1))
  11336. (tail (cdr member))
  11337. (org-state (cond
  11338. ((and org-todo-key-trigger
  11339. (or (and (equal arg '(4))
  11340. (eq org-use-fast-todo-selection 'prefix))
  11341. (and (not arg) org-use-fast-todo-selection
  11342. (not (eq org-use-fast-todo-selection
  11343. 'prefix)))))
  11344. ;; Use fast selection
  11345. (org-fast-todo-selection))
  11346. ((and (equal arg '(4))
  11347. (or (not org-use-fast-todo-selection)
  11348. (not org-todo-key-trigger)))
  11349. ;; Read a state with completion
  11350. (org-icompleting-read
  11351. "State: " (mapcar 'list org-todo-keywords-1)
  11352. nil t))
  11353. ((eq arg 'right)
  11354. (if this
  11355. (if tail (car tail) nil)
  11356. (car org-todo-keywords-1)))
  11357. ((eq arg 'left)
  11358. (if (equal member org-todo-keywords-1)
  11359. nil
  11360. (if this
  11361. (nth (- (length org-todo-keywords-1)
  11362. (length tail) 2)
  11363. org-todo-keywords-1)
  11364. (org-last org-todo-keywords-1))))
  11365. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11366. (setq arg nil))) ; hack to fall back to cycling
  11367. (arg
  11368. ;; user or caller requests a specific state
  11369. (cond
  11370. ((equal arg "") nil)
  11371. ((eq arg 'none) nil)
  11372. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11373. ((eq arg 'nextset)
  11374. (or (car (cdr (member head org-todo-heads)))
  11375. (car org-todo-heads)))
  11376. ((eq arg 'previousset)
  11377. (let ((org-todo-heads (reverse org-todo-heads)))
  11378. (or (car (cdr (member head org-todo-heads)))
  11379. (car org-todo-heads))))
  11380. ((car (member arg org-todo-keywords-1)))
  11381. ((stringp arg)
  11382. (user-error "State `%s' not valid in this file" arg))
  11383. ((nth (1- (prefix-numeric-value arg))
  11384. org-todo-keywords-1))))
  11385. ((null member) (or head (car org-todo-keywords-1)))
  11386. ((equal this final-done-word) nil) ;; -> make empty
  11387. ((null tail) nil) ;; -> first entry
  11388. ((memq interpret '(type priority))
  11389. (if (eq this-command last-command)
  11390. (car tail)
  11391. (if (> (length tail) 0)
  11392. (or done-word (car org-done-keywords))
  11393. nil)))
  11394. (t
  11395. (car tail))))
  11396. (org-state (or
  11397. (run-hook-with-args-until-success
  11398. 'org-todo-get-default-hook org-state org-last-state)
  11399. org-state))
  11400. (next (if org-state (concat " " org-state " ") " "))
  11401. (change-plist (list :type 'todo-state-change :from this :to org-state
  11402. :position startpos))
  11403. dolog now-done-p)
  11404. (when org-blocker-hook
  11405. (setq org-last-todo-state-is-todo
  11406. (not (member this org-done-keywords)))
  11407. (unless (save-excursion
  11408. (save-match-data
  11409. (org-with-wide-buffer
  11410. (run-hook-with-args-until-failure
  11411. 'org-blocker-hook change-plist))))
  11412. (if (org-called-interactively-p 'interactive)
  11413. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11414. this org-state org-block-entry-blocking)
  11415. ;; fail silently
  11416. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11417. this org-state org-block-entry-blocking)
  11418. (throw 'exit nil))))
  11419. (store-match-data match-data)
  11420. (replace-match next t t)
  11421. (cond ((equal this org-state)
  11422. (message "TODO state was already %s" (org-trim next)))
  11423. ((pos-visible-in-window-p hl-pos)
  11424. (message "TODO state changed to %s" (org-trim next))))
  11425. (unless head
  11426. (setq head (org-get-todo-sequence-head org-state)
  11427. ass (assoc head org-todo-kwd-alist)
  11428. interpret (nth 1 ass)
  11429. done-word (nth 3 ass)
  11430. final-done-word (nth 4 ass)))
  11431. (when (memq arg '(nextset previousset))
  11432. (message "Keyword-Set %d/%d: %s"
  11433. (- (length org-todo-sets) -1
  11434. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11435. (length org-todo-sets)
  11436. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11437. (setq org-last-todo-state-is-todo
  11438. (not (member org-state org-done-keywords)))
  11439. (setq now-done-p (and (member org-state org-done-keywords)
  11440. (not (member this org-done-keywords))))
  11441. (and logging (org-local-logging logging))
  11442. (when (and (or org-todo-log-states org-log-done)
  11443. (not (eq org-inhibit-logging t))
  11444. (not (memq arg '(nextset previousset))))
  11445. ;; we need to look at recording a time and note
  11446. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11447. (nth 2 (assoc this org-todo-log-states))))
  11448. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11449. (setq dolog 'time))
  11450. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11451. (and org-state
  11452. (member org-state org-not-done-keywords)
  11453. (not (member this org-not-done-keywords))))
  11454. ;; This is now a todo state and was not one before
  11455. ;; If there was a CLOSED time stamp, get rid of it.
  11456. (org-add-planning-info nil nil 'closed))
  11457. (when (and now-done-p org-log-done)
  11458. ;; It is now done, and it was not done before
  11459. (org-add-planning-info 'closed (org-current-effective-time))
  11460. (if (and (not dolog) (eq 'note org-log-done))
  11461. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11462. (when (and org-state dolog)
  11463. ;; This is a non-nil state, and we need to log it
  11464. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11465. ;; Fixup tag positioning
  11466. (org-todo-trigger-tag-changes org-state)
  11467. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11468. (when org-provide-todo-statistics
  11469. (org-update-parent-todo-statistics))
  11470. (run-hooks 'org-after-todo-state-change-hook)
  11471. (if (and arg (not (member org-state org-done-keywords)))
  11472. (setq head (org-get-todo-sequence-head org-state)))
  11473. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11474. ;; Do we need to trigger a repeat?
  11475. (when now-done-p
  11476. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11477. ;; This is for the agenda, take a snapshot of the headline.
  11478. (save-match-data
  11479. (setq org-agenda-headline-snapshot-before-repeat
  11480. (org-get-heading))))
  11481. (org-auto-repeat-maybe org-state))
  11482. ;; Fixup cursor location if close to the keyword
  11483. (if (and (outline-on-heading-p)
  11484. (not (bolp))
  11485. (save-excursion (beginning-of-line 1)
  11486. (looking-at org-todo-line-regexp))
  11487. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11488. (progn
  11489. (goto-char (or (match-end 2) (match-end 1)))
  11490. (and (looking-at " ") (just-one-space))))
  11491. (when org-trigger-hook
  11492. (save-excursion
  11493. (run-hook-with-args 'org-trigger-hook change-plist)))
  11494. (when commentp (org-toggle-comment))))))))
  11495. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11496. "Block turning an entry into a TODO, using the hierarchy.
  11497. This checks whether the current task should be blocked from state
  11498. changes. Such blocking occurs when:
  11499. 1. The task has children which are not all in a completed state.
  11500. 2. A task has a parent with the property :ORDERED:, and there
  11501. are siblings prior to the current task with incomplete
  11502. status.
  11503. 3. The parent of the task is blocked because it has siblings that should
  11504. be done first, or is child of a block grandparent TODO entry."
  11505. (if (not org-enforce-todo-dependencies)
  11506. t ; if locally turned off don't block
  11507. (catch 'dont-block
  11508. ;; If this is not a todo state change, or if this entry is already DONE,
  11509. ;; do not block
  11510. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11511. (member (plist-get change-plist :from)
  11512. (cons 'done org-done-keywords))
  11513. (member (plist-get change-plist :to)
  11514. (cons 'todo org-not-done-keywords))
  11515. (not (plist-get change-plist :to)))
  11516. (throw 'dont-block t))
  11517. ;; If this task has children, and any are undone, it's blocked
  11518. (save-excursion
  11519. (org-back-to-heading t)
  11520. (let ((this-level (funcall outline-level)))
  11521. (outline-next-heading)
  11522. (let ((child-level (funcall outline-level)))
  11523. (while (and (not (eobp))
  11524. (> child-level this-level))
  11525. ;; this todo has children, check whether they are all
  11526. ;; completed
  11527. (if (and (not (org-entry-is-done-p))
  11528. (org-entry-is-todo-p))
  11529. (progn (setq org-block-entry-blocking (org-get-heading))
  11530. (throw 'dont-block nil)))
  11531. (outline-next-heading)
  11532. (setq child-level (funcall outline-level))))))
  11533. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11534. ;; any previous siblings are undone, it's blocked
  11535. (save-excursion
  11536. (org-back-to-heading t)
  11537. (let* ((pos (point))
  11538. (parent-pos (and (org-up-heading-safe) (point))))
  11539. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11540. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11541. (forward-line 1)
  11542. (re-search-forward org-not-done-heading-regexp pos t))
  11543. (setq org-block-entry-blocking (match-string 0))
  11544. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11545. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11546. (while t
  11547. (goto-char parent-pos)
  11548. (if (not (looking-at org-not-done-heading-regexp))
  11549. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11550. (setq pos (point))
  11551. (setq parent-pos (and (org-up-heading-safe) (point)))
  11552. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11553. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11554. (forward-line 1)
  11555. (re-search-forward org-not-done-heading-regexp pos t)
  11556. (setq org-block-entry-blocking (org-get-heading)))
  11557. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11558. (defcustom org-track-ordered-property-with-tag nil
  11559. "Should the ORDERED property also be shown as a tag?
  11560. The ORDERED property decides if an entry should require subtasks to be
  11561. completed in sequence. Since a property is not very visible, setting
  11562. this option means that toggling the ORDERED property with the command
  11563. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11564. not relevant for the behavior, but it makes things more visible.
  11565. Note that toggling the tag with tags commands will not change the property
  11566. and therefore not influence behavior!
  11567. This can be t, meaning the tag ORDERED should be used, It can also be a
  11568. string to select a different tag for this task."
  11569. :group 'org-todo
  11570. :type '(choice
  11571. (const :tag "No tracking" nil)
  11572. (const :tag "Track with ORDERED tag" t)
  11573. (string :tag "Use other tag")))
  11574. (defun org-toggle-ordered-property ()
  11575. "Toggle the ORDERED property of the current entry.
  11576. For better visibility, you can track the value of this property with a tag.
  11577. See variable `org-track-ordered-property-with-tag'."
  11578. (interactive)
  11579. (let* ((t1 org-track-ordered-property-with-tag)
  11580. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11581. (save-excursion
  11582. (org-back-to-heading)
  11583. (if (org-entry-get nil "ORDERED")
  11584. (progn
  11585. (org-delete-property "ORDERED")
  11586. (and tag (org-toggle-tag tag 'off))
  11587. (message "Subtasks can be completed in arbitrary order"))
  11588. (org-entry-put nil "ORDERED" "t")
  11589. (and tag (org-toggle-tag tag 'on))
  11590. (message "Subtasks must be completed in sequence")))))
  11591. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11592. (defun org-block-todo-from-checkboxes (change-plist)
  11593. "Block turning an entry into a TODO, using checkboxes.
  11594. This checks whether the current task should be blocked from state
  11595. changes because there are unchecked boxes in this entry."
  11596. (if (not org-enforce-todo-checkbox-dependencies)
  11597. t ; if locally turned off don't block
  11598. (catch 'dont-block
  11599. ;; If this is not a todo state change, or if this entry is already DONE,
  11600. ;; do not block
  11601. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11602. (member (plist-get change-plist :from)
  11603. (cons 'done org-done-keywords))
  11604. (member (plist-get change-plist :to)
  11605. (cons 'todo org-not-done-keywords))
  11606. (not (plist-get change-plist :to)))
  11607. (throw 'dont-block t))
  11608. ;; If this task has checkboxes that are not checked, it's blocked
  11609. (save-excursion
  11610. (org-back-to-heading t)
  11611. (let ((beg (point)) end)
  11612. (outline-next-heading)
  11613. (setq end (point))
  11614. (goto-char beg)
  11615. (if (org-list-search-forward
  11616. (concat (org-item-beginning-re)
  11617. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11618. "\\[[- ]\\]")
  11619. end t)
  11620. (progn
  11621. (if (boundp 'org-blocked-by-checkboxes)
  11622. (setq org-blocked-by-checkboxes t))
  11623. (throw 'dont-block nil)))))
  11624. t))) ; do not block
  11625. (defun org-entry-blocked-p ()
  11626. "Is the current entry blocked?"
  11627. (org-with-silent-modifications
  11628. (if (org-entry-get nil "NOBLOCKING")
  11629. nil ;; Never block this entry
  11630. (not (run-hook-with-args-until-failure
  11631. 'org-blocker-hook
  11632. (list :type 'todo-state-change
  11633. :position (point)
  11634. :from 'todo
  11635. :to 'done))))))
  11636. (defun org-update-statistics-cookies (all)
  11637. "Update the statistics cookie, either from TODO or from checkboxes.
  11638. This should be called with the cursor in a line with a statistics cookie."
  11639. (interactive "P")
  11640. (if all
  11641. (progn
  11642. (org-update-checkbox-count 'all)
  11643. (org-map-entries 'org-update-parent-todo-statistics))
  11644. (if (not (org-at-heading-p))
  11645. (org-update-checkbox-count)
  11646. (let ((pos (point-marker))
  11647. end l1 l2)
  11648. (ignore-errors (org-back-to-heading t))
  11649. (if (not (org-at-heading-p))
  11650. (org-update-checkbox-count)
  11651. (setq l1 (org-outline-level))
  11652. (setq end (save-excursion
  11653. (outline-next-heading)
  11654. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11655. (point)))
  11656. (if (and (save-excursion
  11657. (re-search-forward
  11658. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11659. (not (save-excursion (re-search-forward
  11660. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11661. (org-update-checkbox-count)
  11662. (if (and l2 (> l2 l1))
  11663. (progn
  11664. (goto-char end)
  11665. (org-update-parent-todo-statistics))
  11666. (goto-char pos)
  11667. (beginning-of-line 1)
  11668. (while (re-search-forward
  11669. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11670. (point-at-eol) t)
  11671. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11672. (goto-char pos)
  11673. (move-marker pos nil)))))
  11674. (defvar org-entry-property-inherited-from) ;; defined below
  11675. (defun org-update-parent-todo-statistics ()
  11676. "Update any statistics cookie in the parent of the current headline.
  11677. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11678. the entire subtree and this will travel up the hierarchy and update
  11679. statistics everywhere."
  11680. (let* ((prop (save-excursion (org-up-heading-safe)
  11681. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11682. (recursive (or (not org-hierarchical-todo-statistics)
  11683. (and prop (string-match "\\<recursive\\>" prop))))
  11684. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11685. 0))
  11686. (first t)
  11687. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11688. level ltoggle l1 new ndel
  11689. (cnt-all 0) (cnt-done 0) is-percent kwd
  11690. checkbox-beg ov ovs ove cookie-present)
  11691. (catch 'exit
  11692. (save-excursion
  11693. (beginning-of-line 1)
  11694. (setq ltoggle (funcall outline-level))
  11695. ;; Three situations are to consider:
  11696. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11697. ;; to the top-level ancestor on the headline;
  11698. ;; 2. If parent has "recursive" property, repeat up to the
  11699. ;; headline setting that property, taking inheritance into
  11700. ;; account;
  11701. ;; 3. Else, move up to direct parent and proceed only once.
  11702. (while (and (setq level (org-up-heading-safe))
  11703. (or recursive first)
  11704. (>= (point) lim))
  11705. (setq first nil cookie-present nil)
  11706. (unless (and level
  11707. (not (string-match
  11708. "\\<checkbox\\>"
  11709. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11710. "")))))
  11711. (throw 'exit nil))
  11712. (while (re-search-forward box-re (point-at-eol) t)
  11713. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11714. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11715. (save-match-data
  11716. (unless (outline-next-heading) (throw 'exit nil))
  11717. (while (and (looking-at org-complex-heading-regexp)
  11718. (> (setq l1 (length (match-string 1))) level))
  11719. (setq kwd (and (or recursive (= l1 ltoggle))
  11720. (match-string 2)))
  11721. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11722. (and (eq org-provide-todo-statistics t)
  11723. (or (member kwd org-done-keywords)))
  11724. (and (listp org-provide-todo-statistics)
  11725. (stringp (car org-provide-todo-statistics))
  11726. (or (member kwd org-provide-todo-statistics)
  11727. (member kwd org-done-keywords)))
  11728. (and (listp org-provide-todo-statistics)
  11729. (listp (car org-provide-todo-statistics))
  11730. (or (member kwd (car org-provide-todo-statistics))
  11731. (and (member kwd org-done-keywords)
  11732. (member kwd (cadr org-provide-todo-statistics))))))
  11733. (setq cnt-all (1+ cnt-all))
  11734. (if (eq org-provide-todo-statistics t)
  11735. (and kwd (setq cnt-all (1+ cnt-all)))))
  11736. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11737. (member kwd org-done-keywords))
  11738. (and (listp org-provide-todo-statistics)
  11739. (listp (car org-provide-todo-statistics))
  11740. (member kwd org-done-keywords)
  11741. (member kwd (cadr org-provide-todo-statistics)))
  11742. (and (listp org-provide-todo-statistics)
  11743. (stringp (car org-provide-todo-statistics))
  11744. (member kwd org-done-keywords)))
  11745. (setq cnt-done (1+ cnt-done)))
  11746. (outline-next-heading)))
  11747. (setq new
  11748. (if is-percent
  11749. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11750. (format "[%d/%d]" cnt-done cnt-all))
  11751. ndel (- (match-end 0) checkbox-beg))
  11752. ;; handle overlays when updating cookie from column view
  11753. (when (setq ov (car (overlays-at checkbox-beg)))
  11754. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11755. (delete-overlay ov))
  11756. (goto-char checkbox-beg)
  11757. (insert new)
  11758. (delete-region (point) (+ (point) ndel))
  11759. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11760. (when ov (move-overlay ov ovs ove)))
  11761. (when cookie-present
  11762. (run-hook-with-args 'org-after-todo-statistics-hook
  11763. cnt-done (- cnt-all cnt-done))))))
  11764. (run-hooks 'org-todo-statistics-hook)))
  11765. (defvar org-after-todo-statistics-hook nil
  11766. "Hook that is called after a TODO statistics cookie has been updated.
  11767. Each function is called with two arguments: the number of not-done entries
  11768. and the number of done entries.
  11769. For example, the following function, when added to this hook, will switch
  11770. an entry to DONE when all children are done, and back to TODO when new
  11771. entries are set to a TODO status. Note that this hook is only called
  11772. when there is a statistics cookie in the headline!
  11773. (defun org-summary-todo (n-done n-not-done)
  11774. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11775. (let (org-log-done org-log-states) ; turn off logging
  11776. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11777. ")
  11778. (defvar org-todo-statistics-hook nil
  11779. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11780. This hook runs even if there is no statistics cookie present, in which case
  11781. `org-after-todo-statistics-hook' would not run.")
  11782. (defun org-todo-trigger-tag-changes (state)
  11783. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11784. (let ((l org-todo-state-tags-triggers)
  11785. changes)
  11786. (when (or (not state) (equal state ""))
  11787. (setq changes (append changes (cdr (assoc "" l)))))
  11788. (when (and (stringp state) (> (length state) 0))
  11789. (setq changes (append changes (cdr (assoc state l)))))
  11790. (when (member state org-not-done-keywords)
  11791. (setq changes (append changes (cdr (assoc 'todo l)))))
  11792. (when (member state org-done-keywords)
  11793. (setq changes (append changes (cdr (assoc 'done l)))))
  11794. (dolist (c changes)
  11795. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11796. (defun org-local-logging (value)
  11797. "Get logging settings from a property VALUE."
  11798. (let* (words w a)
  11799. ;; directly set the variables, they are already local.
  11800. (setq org-log-done nil
  11801. org-log-repeat nil
  11802. org-todo-log-states nil)
  11803. (setq words (org-split-string value))
  11804. (while (setq w (pop words))
  11805. (cond
  11806. ((setq a (assoc w org-startup-options))
  11807. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11808. (set (nth 1 a) (nth 2 a))))
  11809. ((setq a (org-extract-log-state-settings w))
  11810. (and (member (car a) org-todo-keywords-1)
  11811. (push a org-todo-log-states)))))))
  11812. (defun org-get-todo-sequence-head (kwd)
  11813. "Return the head of the TODO sequence to which KWD belongs.
  11814. If KWD is not set, check if there is a text property remembering the
  11815. right sequence."
  11816. (let (p)
  11817. (cond
  11818. ((not kwd)
  11819. (or (get-text-property (point-at-bol) 'org-todo-head)
  11820. (progn
  11821. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11822. nil (point-at-eol)))
  11823. (get-text-property p 'org-todo-head))))
  11824. ((not (member kwd org-todo-keywords-1))
  11825. (car org-todo-keywords-1))
  11826. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11827. (defun org-fast-todo-selection ()
  11828. "Fast TODO keyword selection with single keys.
  11829. Returns the new TODO keyword, or nil if no state change should occur."
  11830. (let* ((fulltable org-todo-key-alist)
  11831. (done-keywords org-done-keywords) ;; needed for the faces.
  11832. (maxlen (apply 'max (mapcar
  11833. (lambda (x)
  11834. (if (stringp (car x)) (string-width (car x)) 0))
  11835. fulltable)))
  11836. (expert nil)
  11837. (fwidth (+ maxlen 3 1 3))
  11838. (ncol (/ (- (window-width) 4) fwidth))
  11839. tg cnt e c tbl
  11840. groups ingroup)
  11841. (save-excursion
  11842. (save-window-excursion
  11843. (if expert
  11844. (set-buffer (get-buffer-create " *Org todo*"))
  11845. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11846. (erase-buffer)
  11847. (org-set-local 'org-done-keywords done-keywords)
  11848. (setq tbl fulltable cnt 0)
  11849. (while (setq e (pop tbl))
  11850. (cond
  11851. ((equal e '(:startgroup))
  11852. (push '() groups) (setq ingroup t)
  11853. (when (not (= cnt 0))
  11854. (setq cnt 0)
  11855. (insert "\n"))
  11856. (insert "{ "))
  11857. ((equal e '(:endgroup))
  11858. (setq ingroup nil cnt 0)
  11859. (insert "}\n"))
  11860. ((equal e '(:newline))
  11861. (when (not (= cnt 0))
  11862. (setq cnt 0)
  11863. (insert "\n")
  11864. (setq e (car tbl))
  11865. (while (equal (car tbl) '(:newline))
  11866. (insert "\n")
  11867. (setq tbl (cdr tbl)))))
  11868. (t
  11869. (setq tg (car e) c (cdr e))
  11870. (if ingroup (push tg (car groups)))
  11871. (setq tg (org-add-props tg nil 'face
  11872. (org-get-todo-face tg)))
  11873. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11874. (insert "[" c "] " tg (make-string
  11875. (- fwidth 4 (length tg)) ?\ ))
  11876. (when (= (setq cnt (1+ cnt)) ncol)
  11877. (insert "\n")
  11878. (if ingroup (insert " "))
  11879. (setq cnt 0)))))
  11880. (insert "\n")
  11881. (goto-char (point-min))
  11882. (if (not expert) (org-fit-window-to-buffer))
  11883. (message "[a-z..]:Set [SPC]:clear")
  11884. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11885. (cond
  11886. ((or (= c ?\C-g)
  11887. (and (= c ?q) (not (rassoc c fulltable))))
  11888. (setq quit-flag t))
  11889. ((= c ?\ ) nil)
  11890. ((setq e (rassoc c fulltable) tg (car e))
  11891. tg)
  11892. (t (setq quit-flag t)))))))
  11893. (defun org-entry-is-todo-p ()
  11894. (member (org-get-todo-state) org-not-done-keywords))
  11895. (defun org-entry-is-done-p ()
  11896. (member (org-get-todo-state) org-done-keywords))
  11897. (defun org-get-todo-state ()
  11898. "Return the TODO keyword of the current subtree."
  11899. (save-excursion
  11900. (org-back-to-heading t)
  11901. (and (looking-at org-todo-line-regexp)
  11902. (match-end 2)
  11903. (match-string 2))))
  11904. (defun org-at-date-range-p (&optional inactive-ok)
  11905. "Is the cursor inside a date range?"
  11906. (interactive)
  11907. (save-excursion
  11908. (catch 'exit
  11909. (let ((pos (point)))
  11910. (skip-chars-backward "^[<\r\n")
  11911. (skip-chars-backward "<[")
  11912. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11913. (>= (match-end 0) pos)
  11914. (throw 'exit t))
  11915. (skip-chars-backward "^<[\r\n")
  11916. (skip-chars-backward "<[")
  11917. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11918. (>= (match-end 0) pos)
  11919. (throw 'exit t)))
  11920. nil)))
  11921. (defun org-get-repeat (&optional tagline)
  11922. "Check if there is a deadline/schedule with repeater in this entry."
  11923. (save-match-data
  11924. (save-excursion
  11925. (org-back-to-heading t)
  11926. (and (re-search-forward (if tagline
  11927. (concat tagline "\\s-*" org-repeat-re)
  11928. org-repeat-re)
  11929. (org-entry-end-position) t)
  11930. (match-string-no-properties 1)))))
  11931. (defvar org-last-changed-timestamp)
  11932. (defvar org-last-inserted-timestamp)
  11933. (defvar org-log-post-message)
  11934. (defvar org-log-note-purpose)
  11935. (defvar org-log-note-how nil)
  11936. (defvar org-log-note-extra)
  11937. (defun org-auto-repeat-maybe (done-word)
  11938. "Check if the current headline contains a repeated deadline/schedule.
  11939. If yes, set TODO state back to what it was and change the base date
  11940. of repeating deadline/scheduled time stamps to new date.
  11941. This function is run automatically after each state change to a DONE state."
  11942. ;; last-state is dynamically scoped into this function
  11943. (let* ((repeat (org-get-repeat))
  11944. (aa (assoc org-last-state org-todo-kwd-alist))
  11945. (interpret (nth 1 aa))
  11946. (head (nth 2 aa))
  11947. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11948. (msg "Entry repeats: ")
  11949. (org-log-done nil)
  11950. (org-todo-log-states nil)
  11951. re type n what ts time to-state)
  11952. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11953. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11954. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11955. org-todo-repeat-to-state))
  11956. (unless (and to-state (member to-state org-todo-keywords-1))
  11957. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11958. (org-todo to-state)
  11959. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11960. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11961. (org-time-stamp-format t t))))
  11962. (when org-log-repeat
  11963. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11964. (memq 'org-add-log-note post-command-hook))
  11965. ;; OK, we are already setup for some record
  11966. (if (eq org-log-repeat 'note)
  11967. ;; make sure we take a note, not only a time stamp
  11968. (setq org-log-note-how 'note))
  11969. ;; Set up for taking a record
  11970. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11971. org-last-state
  11972. 'findpos org-log-repeat)))
  11973. (org-back-to-heading t)
  11974. (org-add-planning-info nil nil 'closed)
  11975. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11976. org-deadline-time-regexp "\\)\\|\\("
  11977. org-ts-regexp "\\)"))
  11978. (while (re-search-forward
  11979. re (save-excursion (outline-next-heading) (point)) t)
  11980. (setq type (if (match-end 1) org-scheduled-string
  11981. (if (match-end 3) org-deadline-string "Plain:"))
  11982. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11983. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11984. (setq n (string-to-number (match-string 2 ts))
  11985. what (match-string 3 ts))
  11986. (if (equal what "w") (setq n (* n 7) what "d"))
  11987. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11988. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11989. ;; Preparation, see if we need to modify the start date for the change
  11990. (when (match-end 1)
  11991. (setq time (save-match-data (org-time-string-to-time ts)))
  11992. (cond
  11993. ((equal (match-string 1 ts) ".")
  11994. ;; Shift starting date to today
  11995. (org-timestamp-change
  11996. (- (org-today) (time-to-days time))
  11997. 'day))
  11998. ((equal (match-string 1 ts) "+")
  11999. (let ((nshiftmax 10) (nshift 0))
  12000. (while (or (= nshift 0)
  12001. (<= (time-to-days time)
  12002. (time-to-days (current-time))))
  12003. (when (= (incf nshift) nshiftmax)
  12004. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12005. (user-error "Abort")))
  12006. (org-timestamp-change n (cdr (assoc what whata)))
  12007. (org-at-timestamp-p t)
  12008. (setq ts (match-string 1))
  12009. (setq time (save-match-data (org-time-string-to-time ts)))))
  12010. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12011. ;; rematch, so that we have everything in place for the real shift
  12012. (org-at-timestamp-p t)
  12013. (setq ts (match-string 1))
  12014. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12015. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12016. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12017. (setq org-log-post-message msg)
  12018. (message "%s" msg))))
  12019. (defun org-show-todo-tree (arg)
  12020. "Make a compact tree which shows all headlines marked with TODO.
  12021. The tree will show the lines where the regexp matches, and all higher
  12022. headlines above the match.
  12023. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12024. With a numeric prefix N, construct a sparse tree for the Nth element
  12025. of `org-todo-keywords-1'."
  12026. (interactive "P")
  12027. (let ((case-fold-search nil)
  12028. (kwd-re
  12029. (cond ((null arg) org-not-done-regexp)
  12030. ((equal arg '(4))
  12031. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12032. (mapcar 'list org-todo-keywords-1))))
  12033. (concat "\\("
  12034. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12035. "\\)\\>")))
  12036. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12037. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12038. org-todo-keywords-1)))
  12039. (t (user-error "Invalid prefix argument: %s" arg)))))
  12040. (message "%d TODO entries found"
  12041. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12042. (defun org-deadline (arg &optional time)
  12043. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12044. With one universal prefix argument, remove any deadline from the item.
  12045. With two universal prefix arguments, prompt for a warning delay.
  12046. With argument TIME, set the deadline at the corresponding date. TIME
  12047. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12048. (interactive "P")
  12049. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12050. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12051. 'region-start-level 'region))
  12052. org-loop-over-headlines-in-active-region)
  12053. (org-map-entries
  12054. `(org-deadline ',arg ,time)
  12055. org-loop-over-headlines-in-active-region
  12056. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12057. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12058. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12059. (repeater (and old-date
  12060. (string-match
  12061. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12062. old-date)
  12063. (match-string 1 old-date))))
  12064. (cond
  12065. ((equal arg '(4))
  12066. (when (and old-date org-log-redeadline)
  12067. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12068. org-log-redeadline))
  12069. (org-remove-timestamp-with-keyword org-deadline-string)
  12070. (message "Item no longer has a deadline."))
  12071. ((equal arg '(16))
  12072. (save-excursion
  12073. (org-back-to-heading t)
  12074. (if (re-search-forward
  12075. org-deadline-time-regexp
  12076. (save-excursion (outline-next-heading) (point)) t)
  12077. (let* ((rpl0 (match-string 1))
  12078. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12079. (replace-match
  12080. (concat org-deadline-string
  12081. " <" rpl
  12082. (format " -%dd"
  12083. (abs
  12084. (- (time-to-days
  12085. (save-match-data
  12086. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12087. (time-to-days old-date-time))))
  12088. ">") t t))
  12089. (user-error "No deadline information to update"))))
  12090. (t
  12091. (org-add-planning-info 'deadline time 'closed)
  12092. (when (and old-date org-log-redeadline
  12093. (not (equal old-date
  12094. (substring org-last-inserted-timestamp 1 -1))))
  12095. (org-add-log-setup 'redeadline nil old-date 'findpos
  12096. org-log-redeadline))
  12097. (when repeater
  12098. (save-excursion
  12099. (org-back-to-heading t)
  12100. (when (re-search-forward (concat org-deadline-string " "
  12101. org-last-inserted-timestamp)
  12102. (save-excursion
  12103. (outline-next-heading) (point)) t)
  12104. (goto-char (1- (match-end 0)))
  12105. (insert " " repeater)
  12106. (setq org-last-inserted-timestamp
  12107. (concat (substring org-last-inserted-timestamp 0 -1)
  12108. " " repeater
  12109. (substring org-last-inserted-timestamp -1))))))
  12110. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12111. (defun org-schedule (arg &optional time)
  12112. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12113. With one universal prefix argument, remove any scheduling date from the item.
  12114. With two universal prefix arguments, prompt for a delay cookie.
  12115. With argument TIME, scheduled at the corresponding date. TIME can
  12116. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12117. (interactive "P")
  12118. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12119. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12120. 'region-start-level 'region))
  12121. org-loop-over-headlines-in-active-region)
  12122. (org-map-entries
  12123. `(org-schedule ',arg ,time)
  12124. org-loop-over-headlines-in-active-region
  12125. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12126. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12127. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12128. (repeater (and old-date
  12129. (string-match
  12130. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12131. old-date)
  12132. (match-string 1 old-date))))
  12133. (cond
  12134. ((equal arg '(4))
  12135. (progn
  12136. (when (and old-date org-log-reschedule)
  12137. (org-add-log-setup 'delschedule nil old-date 'findpos
  12138. org-log-reschedule))
  12139. (org-remove-timestamp-with-keyword org-scheduled-string)
  12140. (message "Item is no longer scheduled.")))
  12141. ((equal arg '(16))
  12142. (save-excursion
  12143. (org-back-to-heading t)
  12144. (if (re-search-forward
  12145. org-scheduled-time-regexp
  12146. (save-excursion (outline-next-heading) (point)) t)
  12147. (let* ((rpl0 (match-string 1))
  12148. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12149. (replace-match
  12150. (concat org-scheduled-string
  12151. " <" rpl
  12152. (format " -%dd"
  12153. (abs
  12154. (- (time-to-days
  12155. (save-match-data
  12156. (org-read-date nil t nil "Delay until" old-date-time)))
  12157. (time-to-days old-date-time))))
  12158. ">") t t))
  12159. (user-error "No scheduled information to update"))))
  12160. (t
  12161. (org-add-planning-info 'scheduled time 'closed)
  12162. (when (and old-date org-log-reschedule
  12163. (not (equal old-date
  12164. (substring org-last-inserted-timestamp 1 -1))))
  12165. (org-add-log-setup 'reschedule nil old-date 'findpos
  12166. org-log-reschedule))
  12167. (when repeater
  12168. (save-excursion
  12169. (org-back-to-heading t)
  12170. (when (re-search-forward (concat org-scheduled-string " "
  12171. org-last-inserted-timestamp)
  12172. (save-excursion
  12173. (outline-next-heading) (point)) t)
  12174. (goto-char (1- (match-end 0)))
  12175. (insert " " repeater)
  12176. (setq org-last-inserted-timestamp
  12177. (concat (substring org-last-inserted-timestamp 0 -1)
  12178. " " repeater
  12179. (substring org-last-inserted-timestamp -1))))))
  12180. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12181. (defun org-get-scheduled-time (pom &optional inherit)
  12182. "Get the scheduled time as a time tuple, of a format suitable
  12183. for calling org-schedule with, or if there is no scheduling,
  12184. returns nil."
  12185. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12186. (when time
  12187. (apply 'encode-time (org-parse-time-string time)))))
  12188. (defun org-get-deadline-time (pom &optional inherit)
  12189. "Get the deadline as a time tuple, of a format suitable for
  12190. calling org-deadline with, or if there is no scheduling, returns
  12191. nil."
  12192. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12193. (when time
  12194. (apply 'encode-time (org-parse-time-string time)))))
  12195. (defun org-remove-timestamp-with-keyword (keyword)
  12196. "Remove all time stamps with KEYWORD in the current entry."
  12197. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12198. beg)
  12199. (save-excursion
  12200. (org-back-to-heading t)
  12201. (setq beg (point))
  12202. (outline-next-heading)
  12203. (while (re-search-backward re beg t)
  12204. (replace-match "")
  12205. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12206. (equal (char-before) ?\ ))
  12207. (backward-delete-char 1)
  12208. (if (string-match "^[ \t]*$" (buffer-substring
  12209. (point-at-bol) (point-at-eol)))
  12210. (delete-region (point-at-bol)
  12211. (min (point-max) (1+ (point-at-eol))))))))))
  12212. (defvar org-time-was-given) ; dynamically scoped parameter
  12213. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12214. (defun org-at-planning-p ()
  12215. "Non-nil when point is on a planning info line."
  12216. ;; This is as accurate and faster than `org-element-at-point' since
  12217. ;; planning info location is fixed in the section.
  12218. (org-with-wide-buffer
  12219. (beginning-of-line)
  12220. (and (org-looking-at-p org-planning-line-re)
  12221. (eq (point)
  12222. (ignore-errors
  12223. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12224. (org-back-to-heading t)
  12225. (org-with-limited-levels (org-back-to-heading t)))
  12226. (line-beginning-position 2))))))
  12227. (defun org-add-planning-info (what &optional time &rest remove)
  12228. "Insert new timestamp with keyword in the planning line.
  12229. WHAT indicates what kind of time stamp to add. It is a symbol
  12230. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12231. the time to use. If none is given, the user is prompted for
  12232. a date. REMOVE indicates what kind of entries to remove. An old
  12233. WHAT entry will also be removed."
  12234. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12235. (catch 'exit
  12236. (when (and (memq what '(scheduled deadline))
  12237. (or (not time)
  12238. (and (stringp time)
  12239. (string-match "^[-+]+[0-9]" time))))
  12240. ;; Try to get a default date/time from existing timestamp
  12241. (save-excursion
  12242. (org-back-to-heading t)
  12243. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12244. (when (re-search-forward (if (eq what 'scheduled)
  12245. org-scheduled-time-regexp
  12246. org-deadline-time-regexp)
  12247. end t)
  12248. (setq ts (match-string 1)
  12249. default-time (apply 'encode-time (org-parse-time-string ts))
  12250. default-input (and ts (org-get-compact-tod ts)))))))
  12251. (when what
  12252. (setq time
  12253. (if (stringp time)
  12254. ;; This is a string (relative or absolute), set
  12255. ;; proper date.
  12256. (apply #'encode-time
  12257. (org-read-date-analyze
  12258. time default-time (decode-time default-time)))
  12259. ;; If necessary, get the time from the user
  12260. (or time (org-read-date nil 'to-time nil nil
  12261. default-time default-input)))))
  12262. (org-with-wide-buffer
  12263. (org-back-to-heading t)
  12264. (forward-line)
  12265. (unless (bolp) (insert "\n"))
  12266. (cond ((org-looking-at-p org-planning-line-re)
  12267. ;; Move to current indentation.
  12268. (skip-chars-forward " \t")
  12269. ;; Check if we have to remove something.
  12270. (dolist (type (if what (cons what remove) remove))
  12271. (when (save-excursion
  12272. (re-search-forward
  12273. (case type
  12274. (closed org-closed-time-regexp)
  12275. (deadline org-deadline-time-regexp)
  12276. (scheduled org-scheduled-time-regexp)
  12277. (otherwise (error "Invalid planning type: %s" type)))
  12278. (line-end-position) t))
  12279. (replace-match "")
  12280. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12281. (when (and (not what) (eq type 'closed))
  12282. (save-excursion
  12283. (beginning-of-line)
  12284. (if (looking-at "[ \t]*$")
  12285. (delete-region (point) (1+ (point-at-eol)))))))
  12286. ;; Remove leading white spaces.
  12287. (when (and (not (bolp)) (looking-at "[ \t]+")) (replace-match ""))))
  12288. ((not what) (throw 'exit nil)) ; Nothing to do.
  12289. (t (insert-before-markers "\n")
  12290. (backward-char 1)
  12291. (when org-adapt-indentation
  12292. (org-indent-to-column (1+ (org-outline-level))))))
  12293. (when what
  12294. ;; Insert planning keyword.
  12295. (insert (case what
  12296. (closed org-closed-string)
  12297. (deadline org-deadline-string)
  12298. (scheduled org-scheduled-string)
  12299. (otherwise (error "Invalid planning type: %s" what)))
  12300. " ")
  12301. ;; Insert associated timestamp.
  12302. (let ((ts (org-insert-time-stamp
  12303. time
  12304. (or org-time-was-given
  12305. (and (eq what 'closed) org-log-done-with-time))
  12306. (eq what 'closed)
  12307. nil nil (list org-end-time-was-given))))
  12308. (unless (eolp) (insert " "))
  12309. ts))))))
  12310. (defvar org-log-note-marker (make-marker))
  12311. (defvar org-log-note-purpose nil)
  12312. (defvar org-log-note-state nil)
  12313. (defvar org-log-note-previous-state nil)
  12314. (defvar org-log-note-extra nil)
  12315. (defvar org-log-note-window-configuration nil)
  12316. (defvar org-log-note-return-to (make-marker))
  12317. (defvar org-log-note-effective-time nil
  12318. "Remembered current time so that dynamically scoped
  12319. `org-extend-today-until' affects tha timestamps in state change
  12320. log")
  12321. (defvar org-log-post-message nil
  12322. "Message to be displayed after a log note has been stored.
  12323. The auto-repeater uses this.")
  12324. (defun org-add-note ()
  12325. "Add a note to the current entry.
  12326. This is done in the same way as adding a state change note."
  12327. (interactive)
  12328. (org-add-log-setup 'note nil nil 'findpos nil))
  12329. (defun org-log-beginning (&optional create)
  12330. "Return expected start of log notes in current entry.
  12331. When optional argument CREATE is non-nil, the function creates
  12332. a drawer to store notes, if necessary. Returned position ignores
  12333. narrowing."
  12334. (org-with-wide-buffer
  12335. (org-end-of-meta-data)
  12336. (let ((end (if (org-at-heading-p) (point)
  12337. (save-excursion (outline-next-heading) (point))))
  12338. (drawer (org-log-into-drawer)))
  12339. (cond
  12340. (drawer
  12341. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12342. (case-fold-search t))
  12343. (catch 'exit
  12344. ;; Try to find existing drawer.
  12345. (while (re-search-forward regexp end t)
  12346. (let ((element (org-element-at-point)))
  12347. (when (eq (org-element-type element) 'drawer)
  12348. (let ((cend (org-element-property :contents-end element)))
  12349. (when (and (not org-log-states-order-reversed) cend)
  12350. (goto-char cend)))
  12351. (throw 'exit nil))))
  12352. ;; No drawer found. Create one, if permitted.
  12353. (when create
  12354. (unless (bolp) (insert "\n"))
  12355. (let ((beg (point)))
  12356. (insert ":" drawer ":\n:END:\n")
  12357. (org-indent-region beg (point)))
  12358. (end-of-line -1)))))
  12359. (org-log-state-notes-insert-after-drawers
  12360. (while (and (looking-at org-drawer-regexp)
  12361. (progn (goto-char (match-end 0))
  12362. (re-search-forward org-property-end-re end t)))
  12363. (forward-line)))))
  12364. (if (bolp) (point) (line-beginning-position 2))))
  12365. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12366. "Set up the post command hook to take a note.
  12367. If this is about to TODO state change, the new state is expected in STATE.
  12368. When FINDPOS is non-nil, find the correct position for the note in
  12369. the current entry. If not, assume that it can be inserted at point.
  12370. HOW is an indicator what kind of note should be created.
  12371. EXTRA is additional text that will be inserted into the notes buffer."
  12372. (org-with-wide-buffer
  12373. (when findpos
  12374. (goto-char (org-log-beginning t))
  12375. (unless org-log-states-order-reversed
  12376. (org-skip-over-state-notes)
  12377. (skip-chars-backward " \t\n\r")
  12378. (forward-line)))
  12379. (move-marker org-log-note-marker (point))
  12380. ;; Preserve position even if a property drawer is inserted in the
  12381. ;; process.
  12382. (set-marker-insertion-type org-log-note-marker t)
  12383. (setq org-log-note-purpose purpose
  12384. org-log-note-state state
  12385. org-log-note-previous-state prev-state
  12386. org-log-note-how how
  12387. org-log-note-extra extra
  12388. org-log-note-effective-time (org-current-effective-time))
  12389. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12390. (defun org-skip-over-state-notes ()
  12391. "Skip past the list of State notes in an entry."
  12392. (when (ignore-errors (goto-char (org-in-item-p)))
  12393. (let* ((struct (org-list-struct))
  12394. (prevs (org-list-prevs-alist struct))
  12395. (regexp
  12396. (concat "[ \t]*- +"
  12397. (replace-regexp-in-string
  12398. " +" " +"
  12399. (org-replace-escapes
  12400. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12401. `(("%d" . ,org-ts-regexp-inactive)
  12402. ("%D" . ,org-ts-regexp)
  12403. ("%s" . "\"\\S-+\"")
  12404. ("%S" . "\"\\S-+\"")
  12405. ("%t" . ,org-ts-regexp-inactive)
  12406. ("%T" . ,org-ts-regexp)
  12407. ("%u" . ".*?")
  12408. ("%U" . ".*?")))))))
  12409. (while (org-looking-at-p regexp)
  12410. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12411. (org-list-get-item-end (point) struct)))))))
  12412. (defun org-add-log-note (&optional purpose)
  12413. "Pop up a window for taking a note, and add this note later at point."
  12414. (remove-hook 'post-command-hook 'org-add-log-note)
  12415. (setq org-log-note-window-configuration (current-window-configuration))
  12416. (delete-other-windows)
  12417. (move-marker org-log-note-return-to (point))
  12418. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12419. (goto-char org-log-note-marker)
  12420. (org-switch-to-buffer-other-window "*Org Note*")
  12421. (erase-buffer)
  12422. (if (memq org-log-note-how '(time state))
  12423. (let (current-prefix-arg) (org-store-log-note))
  12424. (let ((org-inhibit-startup t)) (org-mode))
  12425. (insert (format "# Insert note for %s.
  12426. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12427. (cond
  12428. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12429. ((eq org-log-note-purpose 'done) "closed todo item")
  12430. ((eq org-log-note-purpose 'state)
  12431. (format "state change from \"%s\" to \"%s\""
  12432. (or org-log-note-previous-state "")
  12433. (or org-log-note-state "")))
  12434. ((eq org-log-note-purpose 'reschedule)
  12435. "rescheduling")
  12436. ((eq org-log-note-purpose 'delschedule)
  12437. "no longer scheduled")
  12438. ((eq org-log-note-purpose 'redeadline)
  12439. "changing deadline")
  12440. ((eq org-log-note-purpose 'deldeadline)
  12441. "removing deadline")
  12442. ((eq org-log-note-purpose 'refile)
  12443. "refiling")
  12444. ((eq org-log-note-purpose 'note)
  12445. "this entry")
  12446. (t (error "This should not happen")))))
  12447. (if org-log-note-extra (insert org-log-note-extra))
  12448. (org-set-local 'org-finish-function 'org-store-log-note)
  12449. (run-hooks 'org-log-buffer-setup-hook)))
  12450. (defvar org-note-abort nil) ; dynamically scoped
  12451. (defun org-store-log-note ()
  12452. "Finish taking a log note, and insert it to where it belongs."
  12453. (let ((txt (buffer-string)))
  12454. (kill-buffer (current-buffer))
  12455. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12456. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12457. (setq txt (replace-match "" t t txt)))
  12458. (if (string-match "\\s-+\\'" txt)
  12459. (setq txt (replace-match "" t t txt)))
  12460. (setq lines (org-split-string txt "\n"))
  12461. (when (and note (string-match "\\S-" note))
  12462. (setq note
  12463. (org-replace-escapes
  12464. note
  12465. (list (cons "%u" (user-login-name))
  12466. (cons "%U" user-full-name)
  12467. (cons "%t" (format-time-string
  12468. (org-time-stamp-format 'long 'inactive)
  12469. org-log-note-effective-time))
  12470. (cons "%T" (format-time-string
  12471. (org-time-stamp-format 'long nil)
  12472. org-log-note-effective-time))
  12473. (cons "%d" (format-time-string
  12474. (org-time-stamp-format nil 'inactive)
  12475. org-log-note-effective-time))
  12476. (cons "%D" (format-time-string
  12477. (org-time-stamp-format nil nil)
  12478. org-log-note-effective-time))
  12479. (cons "%s" (if org-log-note-state
  12480. (concat "\"" org-log-note-state "\"")
  12481. ""))
  12482. (cons "%S" (if org-log-note-previous-state
  12483. (concat "\"" org-log-note-previous-state "\"")
  12484. "\"\"")))))
  12485. (when lines (setq note (concat note " \\\\")))
  12486. (push note lines))
  12487. (when (or current-prefix-arg org-note-abort)
  12488. (when (org-log-into-drawer)
  12489. (org-remove-empty-drawer-at org-log-note-marker))
  12490. (setq lines nil))
  12491. (when lines
  12492. (with-current-buffer (marker-buffer org-log-note-marker)
  12493. (save-excursion
  12494. (goto-char org-log-note-marker)
  12495. (move-marker org-log-note-marker nil)
  12496. ;; Make sure point is at the beginning of an empty line.
  12497. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12498. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12499. ;; In an existing list, add a new item at the top level.
  12500. ;; Otherwise, indent line like a regular one.
  12501. (let ((itemp (org-in-item-p)))
  12502. (if itemp
  12503. (org-indent-line-to
  12504. (let ((struct (save-excursion
  12505. (goto-char itemp) (org-list-struct))))
  12506. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12507. (org-indent-line)))
  12508. (insert (org-list-bullet-string "-") (pop lines))
  12509. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12510. (dolist (line lines)
  12511. (insert "\n")
  12512. (org-indent-line-to ind)
  12513. (insert line)))
  12514. (message "Note stored")
  12515. (org-back-to-heading t)
  12516. (org-cycle-hide-drawers 'children))
  12517. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12518. ;; from within `org-add-log-note' because `buffer-undo-list'
  12519. ;; is then modified outside of `org-with-remote-undo'.
  12520. (when (eq this-command 'org-agenda-todo)
  12521. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12522. ;; Don't add undo information when called from `org-agenda-todo'
  12523. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12524. (set-window-configuration org-log-note-window-configuration)
  12525. (with-current-buffer (marker-buffer org-log-note-return-to)
  12526. (goto-char org-log-note-return-to))
  12527. (move-marker org-log-note-return-to nil)
  12528. (and org-log-post-message (message "%s" org-log-post-message))))
  12529. (defun org-remove-empty-drawer-at (pos)
  12530. "Remove an empty drawer at position POS.
  12531. POS may also be a marker."
  12532. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12533. (org-with-wide-buffer
  12534. (goto-char pos)
  12535. (let ((drawer (org-element-at-point)))
  12536. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12537. (not (org-element-property :contents-begin drawer)))
  12538. (delete-region (org-element-property :begin drawer)
  12539. (progn (goto-char (org-element-property :end drawer))
  12540. (skip-chars-backward " \r\t\n")
  12541. (forward-line)
  12542. (point))))))))
  12543. (defvar org-ts-type nil)
  12544. (defun org-sparse-tree (&optional arg type)
  12545. "Create a sparse tree, prompt for the details.
  12546. This command can create sparse trees. You first need to select the type
  12547. of match used to create the tree:
  12548. t Show all TODO entries.
  12549. T Show entries with a specific TODO keyword.
  12550. m Show entries selected by a tags/property match.
  12551. p Enter a property name and its value (both with completion on existing
  12552. names/values) and show entries with that property.
  12553. r Show entries matching a regular expression (`/' can be used as well).
  12554. b Show deadlines and scheduled items before a date.
  12555. a Show deadlines and scheduled items after a date.
  12556. d Show deadlines due within `org-deadline-warning-days'.
  12557. D Show deadlines and scheduled items between a date range."
  12558. (interactive "P")
  12559. (setq type (or type org-sparse-tree-default-date-type))
  12560. (setq org-ts-type type)
  12561. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12562. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12563. [c]ycle through date types: %s"
  12564. (case type
  12565. (all "all timestamps")
  12566. (scheduled "only scheduled")
  12567. (deadline "only deadline")
  12568. (active "only active timestamps")
  12569. (inactive "only inactive timestamps")
  12570. (closed "with a closed time-stamp")
  12571. (otherwise "scheduled/deadline")))
  12572. (let ((answer (read-char-exclusive)))
  12573. (case answer
  12574. (?c
  12575. (org-sparse-tree
  12576. arg
  12577. (cadr
  12578. (memq type '(nil all scheduled deadline active inactive closed)))))
  12579. (?d (call-interactively 'org-check-deadlines))
  12580. (?b (call-interactively 'org-check-before-date))
  12581. (?a (call-interactively 'org-check-after-date))
  12582. (?D (call-interactively 'org-check-dates-range))
  12583. (?t (call-interactively 'org-show-todo-tree))
  12584. (?T (org-show-todo-tree '(4)))
  12585. (?m (call-interactively 'org-match-sparse-tree))
  12586. ((?p ?P)
  12587. (let* ((kwd (org-icompleting-read
  12588. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12589. (value (org-icompleting-read
  12590. "Value: " (mapcar 'list (org-property-values kwd)))))
  12591. (unless (string-match "\\`{.*}\\'" value)
  12592. (setq value (concat "\"" value "\"")))
  12593. (org-match-sparse-tree arg (concat kwd "=" value))))
  12594. ((?r ?R ?/) (call-interactively 'org-occur))
  12595. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12596. (defvar org-occur-highlights nil
  12597. "List of overlays used for occur matches.")
  12598. (make-variable-buffer-local 'org-occur-highlights)
  12599. (defvar org-occur-parameters nil
  12600. "Parameters of the active org-occur calls.
  12601. This is a list, each call to org-occur pushes as cons cell,
  12602. containing the regular expression and the callback, onto the list.
  12603. The list can contain several entries if `org-occur' has been called
  12604. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12605. will only contain one set of parameters. When the highlights are
  12606. removed (for example with `C-c C-c', or with the next edit (depending
  12607. on `org-remove-highlights-with-change'), this variable is emptied
  12608. as well.")
  12609. (make-variable-buffer-local 'org-occur-parameters)
  12610. (defun org-occur (regexp &optional keep-previous callback)
  12611. "Make a compact tree which shows all matches of REGEXP.
  12612. The tree will show the lines where the regexp matches, and all higher
  12613. headlines above the match. It will also show the heading after the match,
  12614. to make sure editing the matching entry is easy.
  12615. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12616. call to `org-occur' will be kept, to allow stacking of calls to this
  12617. command.
  12618. If CALLBACK is non-nil, it is a function which is called to confirm
  12619. that the match should indeed be shown."
  12620. (interactive "sRegexp: \nP")
  12621. (when (equal regexp "")
  12622. (user-error "Regexp cannot be empty"))
  12623. (unless keep-previous
  12624. (org-remove-occur-highlights nil nil t))
  12625. (push (cons regexp callback) org-occur-parameters)
  12626. (let ((cnt 0))
  12627. (save-excursion
  12628. (goto-char (point-min))
  12629. (if (or (not keep-previous) ; do not want to keep
  12630. (not org-occur-highlights)) ; no previous matches
  12631. ;; hide everything
  12632. (org-overview))
  12633. (while (re-search-forward regexp nil t)
  12634. (when (or (not callback)
  12635. (save-match-data (funcall callback)))
  12636. (setq cnt (1+ cnt))
  12637. (when org-highlight-sparse-tree-matches
  12638. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12639. (org-show-context 'occur-tree))))
  12640. (when org-remove-highlights-with-change
  12641. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12642. nil 'local))
  12643. (unless org-sparse-tree-open-archived-trees
  12644. (org-hide-archived-subtrees (point-min) (point-max)))
  12645. (run-hooks 'org-occur-hook)
  12646. (if (org-called-interactively-p 'interactive)
  12647. (message "%d match(es) for regexp %s" cnt regexp))
  12648. cnt))
  12649. (defun org-occur-next-match (&optional n reset)
  12650. "Function for `next-error-function' to find sparse tree matches.
  12651. N is the number of matches to move, when negative move backwards.
  12652. RESET is entirely ignored - this function always goes back to the
  12653. starting point when no match is found."
  12654. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12655. (search-func (if (< n 0)
  12656. 'previous-single-char-property-change
  12657. 'next-single-char-property-change))
  12658. (n (abs n))
  12659. (pos (point))
  12660. p1)
  12661. (catch 'exit
  12662. (while (setq p1 (funcall search-func (point) 'org-type))
  12663. (when (equal p1 limit)
  12664. (goto-char pos)
  12665. (user-error "No more matches"))
  12666. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12667. (setq n (1- n))
  12668. (when (= n 0)
  12669. (goto-char p1)
  12670. (throw 'exit (point))))
  12671. (goto-char p1))
  12672. (goto-char p1)
  12673. (user-error "No more matches"))))
  12674. (defun org-show-context (&optional key)
  12675. "Make sure point and context are visible.
  12676. Optional argument KEY, when non-nil, is a symbol. See
  12677. `org-show-context-detail' for allowed values and how much is to
  12678. be shown."
  12679. (org-show-set-visibility
  12680. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12681. ((cdr (assq key org-show-context-detail)))
  12682. (t (cdr (assq 'default org-show-context-detail))))))
  12683. (defun org-show-set-visibility (detail)
  12684. "Set visibility around point according to DETAIL.
  12685. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12686. `tree', `canonical' or t. See `org-show-context-detail' for more
  12687. information."
  12688. (unless (org-before-first-heading-p)
  12689. ;; Show current heading and possibly its entry, following headline
  12690. ;; or all children.
  12691. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12692. (org-flag-heading nil)
  12693. (org-show-entry)
  12694. (org-with-limited-levels
  12695. (case detail
  12696. ((tree canonical t) (show-children))
  12697. ((nil minimal ancestors))
  12698. (t (save-excursion
  12699. (outline-next-heading)
  12700. (org-flag-heading nil))))))
  12701. ;; Show all siblings.
  12702. (when (eq detail 'lineage) (org-show-siblings))
  12703. ;; Show ancestors, possibly with their children.
  12704. (when (memq detail '(ancestors lineage tree canonical t))
  12705. (save-excursion
  12706. (while (org-up-heading-safe)
  12707. (org-flag-heading nil)
  12708. (when (memq detail '(canonical t)) (org-show-entry))
  12709. (when (memq detail '(tree canonical t)) (show-children)))))))
  12710. (defvar org-reveal-start-hook nil
  12711. "Hook run before revealing a location.")
  12712. (defun org-reveal (&optional siblings)
  12713. "Show current entry, hierarchy above it, and the following headline.
  12714. This can be used to show a consistent set of context around
  12715. locations exposed with `org-show-context'.
  12716. With optional argument SIBLINGS, on each level of the hierarchy all
  12717. siblings are shown. This repairs the tree structure to what it would
  12718. look like when opened with hierarchical calls to `org-cycle'.
  12719. With double optional argument \\[universal-argument] \\[universal-argument], \
  12720. go to the parent and show the
  12721. entire tree."
  12722. (interactive "P")
  12723. (run-hooks 'org-reveal-start-hook)
  12724. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12725. ((equal siblings '(16))
  12726. (save-excursion
  12727. (when (org-up-heading-safe)
  12728. (org-show-subtree)
  12729. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12730. (t (org-show-set-visibility 'lineage))))
  12731. (defun org-highlight-new-match (beg end)
  12732. "Highlight from BEG to END and mark the highlight is an occur headline."
  12733. (let ((ov (make-overlay beg end)))
  12734. (overlay-put ov 'face 'secondary-selection)
  12735. (overlay-put ov 'org-type 'org-occur)
  12736. (push ov org-occur-highlights)))
  12737. (defun org-remove-occur-highlights (&optional beg end noremove)
  12738. "Remove the occur highlights from the buffer.
  12739. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12740. from the `before-change-functions' in the current buffer."
  12741. (interactive)
  12742. (unless org-inhibit-highlight-removal
  12743. (mapc 'delete-overlay org-occur-highlights)
  12744. (setq org-occur-highlights nil)
  12745. (setq org-occur-parameters nil)
  12746. (unless noremove
  12747. (remove-hook 'before-change-functions
  12748. 'org-remove-occur-highlights 'local))))
  12749. ;;;; Priorities
  12750. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12751. "Regular expression matching the priority indicator.")
  12752. (defvar org-remove-priority-next-time nil)
  12753. (defun org-priority-up ()
  12754. "Increase the priority of the current item."
  12755. (interactive)
  12756. (org-priority 'up))
  12757. (defun org-priority-down ()
  12758. "Decrease the priority of the current item."
  12759. (interactive)
  12760. (org-priority 'down))
  12761. (defun org-priority (&optional action show)
  12762. "Change the priority of an item.
  12763. ACTION can be `set', `up', `down', or a character."
  12764. (interactive "P")
  12765. (if (equal action '(4))
  12766. (org-show-priority)
  12767. (unless org-enable-priority-commands
  12768. (user-error "Priority commands are disabled"))
  12769. (setq action (or action 'set))
  12770. (let (current new news have remove)
  12771. (save-excursion
  12772. (org-back-to-heading t)
  12773. (if (looking-at org-priority-regexp)
  12774. (setq current (string-to-char (match-string 2))
  12775. have t))
  12776. (cond
  12777. ((eq action 'remove)
  12778. (setq remove t new ?\ ))
  12779. ((or (eq action 'set)
  12780. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12781. (if (not (eq action 'set))
  12782. (setq new action)
  12783. (message "Priority %c-%c, SPC to remove: "
  12784. org-highest-priority org-lowest-priority)
  12785. (save-match-data
  12786. (setq new (read-char-exclusive))))
  12787. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12788. (= (upcase org-lowest-priority) org-lowest-priority))
  12789. (setq new (upcase new)))
  12790. (cond ((equal new ?\ ) (setq remove t))
  12791. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12792. (user-error "Priority must be between `%c' and `%c'"
  12793. org-highest-priority org-lowest-priority))))
  12794. ((eq action 'up)
  12795. (setq new (if have
  12796. (1- current) ; normal cycling
  12797. ;; last priority was empty
  12798. (if (eq last-command this-command)
  12799. org-lowest-priority ; wrap around empty to lowest
  12800. ;; default
  12801. (if org-priority-start-cycle-with-default
  12802. org-default-priority
  12803. (1- org-default-priority))))))
  12804. ((eq action 'down)
  12805. (setq new (if have
  12806. (1+ current) ; normal cycling
  12807. ;; last priority was empty
  12808. (if (eq last-command this-command)
  12809. org-highest-priority ; wrap around empty to highest
  12810. ;; default
  12811. (if org-priority-start-cycle-with-default
  12812. org-default-priority
  12813. (1+ org-default-priority))))))
  12814. (t (user-error "Invalid action")))
  12815. (if (or (< (upcase new) org-highest-priority)
  12816. (> (upcase new) org-lowest-priority))
  12817. (if (and (memq action '(up down))
  12818. (not have) (not (eq last-command this-command)))
  12819. ;; `new' is from default priority
  12820. (error
  12821. "The default can not be set, see `org-default-priority' why")
  12822. ;; normal cycling: `new' is beyond highest/lowest priority
  12823. ;; and is wrapped around to the empty priority
  12824. (setq remove t)))
  12825. (setq news (format "%c" new))
  12826. (if have
  12827. (if remove
  12828. (replace-match "" t t nil 1)
  12829. (replace-match news t t nil 2))
  12830. (if remove
  12831. (user-error "No priority cookie found in line")
  12832. (let ((case-fold-search nil))
  12833. (looking-at org-todo-line-regexp))
  12834. (if (match-end 2)
  12835. (progn
  12836. (goto-char (match-end 2))
  12837. (insert " [#" news "]"))
  12838. (goto-char (match-beginning 3))
  12839. (insert "[#" news "] "))))
  12840. (org-set-tags nil 'align))
  12841. (if remove
  12842. (message "Priority removed")
  12843. (message "Priority of current item set to %s" news)))))
  12844. (defun org-show-priority ()
  12845. "Show the priority of the current item.
  12846. This priority is composed of the main priority given with the [#A] cookies,
  12847. and by additional input from the age of a schedules or deadline entry."
  12848. (interactive)
  12849. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12850. (org-get-at-bol 'priority)
  12851. (save-excursion
  12852. (save-match-data
  12853. (beginning-of-line)
  12854. (and (looking-at org-heading-regexp)
  12855. (org-get-priority (match-string 0))))))))
  12856. (message "Priority is %d" (if pri pri -1000))))
  12857. (defun org-get-priority (s)
  12858. "Find priority cookie and return priority."
  12859. (save-match-data
  12860. (if (functionp org-get-priority-function)
  12861. (funcall org-get-priority-function)
  12862. (if (not (string-match org-priority-regexp s))
  12863. (* 1000 (- org-lowest-priority org-default-priority))
  12864. (* 1000 (- org-lowest-priority
  12865. (string-to-char (match-string 2 s))))))))
  12866. ;;;; Tags
  12867. (defvar org-agenda-archives-mode)
  12868. (defvar org-map-continue-from nil
  12869. "Position from where mapping should continue.
  12870. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12871. (defvar org-scanner-tags nil
  12872. "The current tag list while the tags scanner is running.")
  12873. (defvar org-trust-scanner-tags nil
  12874. "Should `org-get-tags-at' use the tags for the scanner.
  12875. This is for internal dynamical scoping only.
  12876. When this is non-nil, the function `org-get-tags-at' will return the value
  12877. of `org-scanner-tags' instead of building the list by itself. This
  12878. can lead to large speed-ups when the tags scanner is used in a file with
  12879. many entries, and when the list of tags is retrieved, for example to
  12880. obtain a list of properties. Building the tags list for each entry in such
  12881. a file becomes an N^2 operation - but with this variable set, it scales
  12882. as N.")
  12883. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12884. "Scan headline tags with inheritance and produce output ACTION.
  12885. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12886. or `agenda' to produce an entry list for an agenda view. It can also be
  12887. a Lisp form or a function that should be called at each matched headline, in
  12888. this case the return value is a list of all return values from these calls.
  12889. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12890. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12891. only lines with a not-done TODO keyword are included in the output.
  12892. This should be the same variable that was scoped into
  12893. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12894. START-LEVEL can be a string with asterisks, reducing the scope to
  12895. headlines matching this string."
  12896. (require 'org-agenda)
  12897. (let* ((re (concat "^"
  12898. (if start-level
  12899. ;; Get the correct level to match
  12900. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12901. org-outline-regexp)
  12902. " *\\(\\<\\("
  12903. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12904. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12905. (props (list 'face 'default
  12906. 'done-face 'org-agenda-done
  12907. 'undone-face 'default
  12908. 'mouse-face 'highlight
  12909. 'org-not-done-regexp org-not-done-regexp
  12910. 'org-todo-regexp org-todo-regexp
  12911. 'org-complex-heading-regexp org-complex-heading-regexp
  12912. 'help-echo
  12913. (format "mouse-2 or RET jump to org file %s"
  12914. (abbreviate-file-name
  12915. (or (buffer-file-name (buffer-base-buffer))
  12916. (buffer-name (buffer-base-buffer)))))))
  12917. (org-map-continue-from nil)
  12918. lspos tags tags-list
  12919. (tags-alist (list (cons 0 org-file-tags)))
  12920. (llast 0) rtn rtn1 level category i txt
  12921. todo marker entry priority
  12922. ts-date ts-date-type ts-date-pair)
  12923. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12924. (setq action (list 'lambda nil action)))
  12925. (save-excursion
  12926. (goto-char (point-min))
  12927. (when (eq action 'sparse-tree)
  12928. (org-overview)
  12929. (org-remove-occur-highlights))
  12930. (while (let (case-fold-search)
  12931. (re-search-forward re nil t))
  12932. (setq org-map-continue-from nil)
  12933. (catch :skip
  12934. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12935. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12936. (goto-char (setq lspos (match-beginning 0)))
  12937. (setq level (org-reduced-level (org-outline-level))
  12938. category (org-get-category))
  12939. (when (eq action 'agenda)
  12940. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12941. ts-date (car ts-date-pair)
  12942. ts-date-type (cdr ts-date-pair)))
  12943. (setq i llast llast level)
  12944. ;; remove tag lists from same and sublevels
  12945. (while (>= i level)
  12946. (when (setq entry (assoc i tags-alist))
  12947. (setq tags-alist (delete entry tags-alist)))
  12948. (setq i (1- i)))
  12949. ;; add the next tags
  12950. (when tags
  12951. (setq tags (org-split-string tags ":")
  12952. tags-alist
  12953. (cons (cons level tags) tags-alist)))
  12954. ;; compile tags for current headline
  12955. (setq tags-list
  12956. (if org-use-tag-inheritance
  12957. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12958. tags)
  12959. org-scanner-tags tags-list)
  12960. (when org-use-tag-inheritance
  12961. (setcdr (car tags-alist)
  12962. (mapcar (lambda (x)
  12963. (setq x (copy-sequence x))
  12964. (org-add-prop-inherited x))
  12965. (cdar tags-alist))))
  12966. (when (and tags org-use-tag-inheritance
  12967. (or (not (eq t org-use-tag-inheritance))
  12968. org-tags-exclude-from-inheritance))
  12969. ;; selective inheritance, remove uninherited ones
  12970. (setcdr (car tags-alist)
  12971. (org-remove-uninherited-tags (cdar tags-alist))))
  12972. (when (and
  12973. ;; eval matcher only when the todo condition is OK
  12974. (and (or (not todo-only) (member todo org-not-done-keywords))
  12975. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12976. (eval matcher)))
  12977. ;; Call the skipper, but return t if it does not skip,
  12978. ;; so that the `and' form continues evaluating
  12979. (progn
  12980. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12981. t)
  12982. ;; Check if timestamps are deselecting this entry
  12983. (or (not todo-only)
  12984. (and (member todo org-not-done-keywords)
  12985. (or (not org-agenda-tags-todo-honor-ignore-options)
  12986. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12987. ;; select this headline
  12988. (cond
  12989. ((eq action 'sparse-tree)
  12990. (and org-highlight-sparse-tree-matches
  12991. (org-get-heading) (match-end 0)
  12992. (org-highlight-new-match
  12993. (match-beginning 1) (match-end 1)))
  12994. (org-show-context 'tags-tree))
  12995. ((eq action 'agenda)
  12996. (setq txt (org-agenda-format-item
  12997. ""
  12998. (concat
  12999. (if (eq org-tags-match-list-sublevels 'indented)
  13000. (make-string (1- level) ?.) "")
  13001. (org-get-heading))
  13002. (make-string level ?\s)
  13003. category
  13004. tags-list)
  13005. priority (org-get-priority txt))
  13006. (goto-char lspos)
  13007. (setq marker (org-agenda-new-marker))
  13008. (org-add-props txt props
  13009. 'org-marker marker 'org-hd-marker marker 'org-category category
  13010. 'todo-state todo
  13011. 'ts-date ts-date
  13012. 'priority priority
  13013. 'type (concat "tagsmatch" ts-date-type))
  13014. (push txt rtn))
  13015. ((functionp action)
  13016. (setq org-map-continue-from nil)
  13017. (save-excursion
  13018. (setq rtn1 (funcall action))
  13019. (push rtn1 rtn)))
  13020. (t (user-error "Invalid action")))
  13021. ;; if we are to skip sublevels, jump to end of subtree
  13022. (unless org-tags-match-list-sublevels
  13023. (org-end-of-subtree t)
  13024. (backward-char 1))))
  13025. ;; Get the correct position from where to continue
  13026. (if org-map-continue-from
  13027. (goto-char org-map-continue-from)
  13028. (and (= (point) lspos) (end-of-line 1)))))
  13029. (when (and (eq action 'sparse-tree)
  13030. (not org-sparse-tree-open-archived-trees))
  13031. (org-hide-archived-subtrees (point-min) (point-max)))
  13032. (nreverse rtn)))
  13033. (defun org-remove-uninherited-tags (tags)
  13034. "Remove all tags that are not inherited from the list TAGS."
  13035. (cond
  13036. ((eq org-use-tag-inheritance t)
  13037. (if org-tags-exclude-from-inheritance
  13038. (org-delete-all org-tags-exclude-from-inheritance tags)
  13039. tags))
  13040. ((not org-use-tag-inheritance) nil)
  13041. ((stringp org-use-tag-inheritance)
  13042. (delq nil (mapcar
  13043. (lambda (x)
  13044. (if (and (string-match org-use-tag-inheritance x)
  13045. (not (member x org-tags-exclude-from-inheritance)))
  13046. x nil))
  13047. tags)))
  13048. ((listp org-use-tag-inheritance)
  13049. (delq nil (mapcar
  13050. (lambda (x)
  13051. (if (member x org-use-tag-inheritance) x nil))
  13052. tags)))))
  13053. (defun org-match-sparse-tree (&optional todo-only match)
  13054. "Create a sparse tree according to tags string MATCH.
  13055. MATCH can contain positive and negative selection of tags, like
  13056. \"+WORK+URGENT-WITHBOSS\".
  13057. If optional argument TODO-ONLY is non-nil, only select lines that are
  13058. also TODO lines."
  13059. (interactive "P")
  13060. (org-agenda-prepare-buffers (list (current-buffer)))
  13061. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13062. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13063. (defvar org-cached-props nil)
  13064. (defun org-cached-entry-get (pom property)
  13065. (if (or (eq t org-use-property-inheritance)
  13066. (and (stringp org-use-property-inheritance)
  13067. (let ((case-fold-search t))
  13068. (org-string-match-p org-use-property-inheritance property)))
  13069. (and (listp org-use-property-inheritance)
  13070. (member-ignore-case property org-use-property-inheritance)))
  13071. ;; Caching is not possible, check it directly.
  13072. (org-entry-get pom property 'inherit)
  13073. ;; Get all properties, so we can do complicated checks easily.
  13074. (cdr (assoc-string property
  13075. (or org-cached-props
  13076. (setq org-cached-props (org-entry-properties pom)))
  13077. t))))
  13078. (defun org-global-tags-completion-table (&optional files)
  13079. "Return the list of all tags in all agenda buffer/files.
  13080. Optional FILES argument is a list of files which can be used
  13081. instead of the agenda files."
  13082. (save-excursion
  13083. (org-uniquify
  13084. (delq nil
  13085. (apply 'append
  13086. (mapcar
  13087. (lambda (file)
  13088. (set-buffer (find-file-noselect file))
  13089. (append (org-get-buffer-tags)
  13090. (mapcar (lambda (x) (if (stringp (car-safe x))
  13091. (list (car-safe x)) nil))
  13092. org-tag-alist)))
  13093. (if (and files (car files))
  13094. files
  13095. (org-agenda-files))))))))
  13096. (defun org-make-tags-matcher (match)
  13097. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13098. The variable `todo-only' is scoped dynamically into this function.
  13099. It will be set to t if the matcher restricts matching to TODO entries,
  13100. otherwise will not be touched.
  13101. Returns a cons of the selection string MATCH and the constructed
  13102. lisp form implementing the matcher. The matcher is to be evaluated
  13103. at an Org entry, with point on the headline, and returns t if the
  13104. entry matches the selection string MATCH. The returned lisp form
  13105. references two variables with information about the entry, which
  13106. must be bound around the form's evaluation: todo, the TODO keyword
  13107. at the entry (or nil of none); and tags-list, the list of all tags
  13108. at the entry including inherited ones. Additionally, the category
  13109. of the entry (if any) must be specified as the text property
  13110. 'org-category on the headline.
  13111. See also `org-scan-tags'.
  13112. "
  13113. (declare (special todo-only))
  13114. (unless (boundp 'todo-only)
  13115. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13116. (unless match
  13117. ;; Get a new match request, with completion against the global
  13118. ;; tags table and the local tags in current buffer
  13119. (let ((org-last-tags-completion-table
  13120. (org-uniquify
  13121. (delq nil (append (org-get-buffer-tags)
  13122. (org-global-tags-completion-table))))))
  13123. (setq match (org-completing-read-no-i
  13124. "Match: " 'org-tags-completion-function nil nil nil
  13125. 'org-tags-history))))
  13126. ;; Parse the string and create a lisp form
  13127. (let ((match0 match)
  13128. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13129. minus tag mm
  13130. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13131. orterms term orlist re-p str-p level-p level-op time-p
  13132. prop-p pn pv po gv rest (start 0) (ss 0))
  13133. ;; Expand group tags
  13134. (setq match (org-tags-expand match))
  13135. ;; Check if there is a TODO part of this match, which would be the
  13136. ;; part after a "/". TO make sure that this slash is not part of
  13137. ;; a property value to be matched against, we also check that there
  13138. ;; is no " after that slash.
  13139. ;; First, find the last slash
  13140. (while (string-match "/+" match ss)
  13141. (setq start (match-beginning 0) ss (match-end 0)))
  13142. (if (and (string-match "/+" match start)
  13143. (not (save-match-data (string-match "\"" match start))))
  13144. ;; match contains also a todo-matching request
  13145. (progn
  13146. (setq tagsmatch (substring match 0 (match-beginning 0))
  13147. todomatch (substring match (match-end 0)))
  13148. (if (string-match "^!" todomatch)
  13149. (setq todo-only t todomatch (substring todomatch 1)))
  13150. (if (string-match "^\\s-*$" todomatch)
  13151. (setq todomatch nil)))
  13152. ;; only matching tags
  13153. (setq tagsmatch match todomatch nil))
  13154. ;; Make the tags matcher
  13155. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13156. (setq tagsmatcher t)
  13157. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13158. (while (setq term (pop orterms))
  13159. (while (and (equal (substring term -1) "\\") orterms)
  13160. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13161. (while (string-match re term)
  13162. (setq rest (substring term (match-end 0))
  13163. minus (and (match-end 1)
  13164. (equal (match-string 1 term) "-"))
  13165. tag (save-match-data (replace-regexp-in-string
  13166. "\\\\-" "-"
  13167. (match-string 2 term)))
  13168. re-p (equal (string-to-char tag) ?{)
  13169. level-p (match-end 4)
  13170. prop-p (match-end 5)
  13171. mm (cond
  13172. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13173. (level-p
  13174. (setq level-op (org-op-to-function (match-string 3 term)))
  13175. `(,level-op level ,(string-to-number
  13176. (match-string 4 term))))
  13177. (prop-p
  13178. (setq pn (match-string 5 term)
  13179. po (match-string 6 term)
  13180. pv (match-string 7 term)
  13181. re-p (equal (string-to-char pv) ?{)
  13182. str-p (equal (string-to-char pv) ?\")
  13183. time-p (save-match-data
  13184. (string-match "^\"[[<].*[]>]\"$" pv))
  13185. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13186. (if time-p (setq pv (org-matcher-time pv)))
  13187. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13188. (cond
  13189. ((equal pn "CATEGORY")
  13190. (setq gv '(get-text-property (point) 'org-category)))
  13191. ((equal pn "TODO")
  13192. (setq gv 'todo))
  13193. (t
  13194. (setq gv `(org-cached-entry-get nil ,pn))))
  13195. (if re-p
  13196. (if (eq po 'org<>)
  13197. `(not (string-match ,pv (or ,gv "")))
  13198. `(string-match ,pv (or ,gv "")))
  13199. (if str-p
  13200. `(,po (or ,gv "") ,pv)
  13201. `(,po (string-to-number (or ,gv ""))
  13202. ,(string-to-number pv) ))))
  13203. (t `(member ,tag tags-list)))
  13204. mm (if minus (list 'not mm) mm)
  13205. term rest)
  13206. (push mm tagsmatcher))
  13207. (push (if (> (length tagsmatcher) 1)
  13208. (cons 'and tagsmatcher)
  13209. (car tagsmatcher))
  13210. orlist)
  13211. (setq tagsmatcher nil))
  13212. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13213. (setq tagsmatcher
  13214. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13215. ;; Make the todo matcher
  13216. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13217. (setq todomatcher t)
  13218. (setq orterms (org-split-string todomatch "|") orlist nil)
  13219. (while (setq term (pop orterms))
  13220. (while (string-match re term)
  13221. (setq minus (and (match-end 1)
  13222. (equal (match-string 1 term) "-"))
  13223. kwd (match-string 2 term)
  13224. re-p (equal (string-to-char kwd) ?{)
  13225. term (substring term (match-end 0))
  13226. mm (if re-p
  13227. `(string-match ,(substring kwd 1 -1) todo)
  13228. (list 'equal 'todo kwd))
  13229. mm (if minus (list 'not mm) mm))
  13230. (push mm todomatcher))
  13231. (push (if (> (length todomatcher) 1)
  13232. (cons 'and todomatcher)
  13233. (car todomatcher))
  13234. orlist)
  13235. (setq todomatcher nil))
  13236. (setq todomatcher (if (> (length orlist) 1)
  13237. (cons 'or orlist) (car orlist))))
  13238. ;; Return the string and lisp forms of the matcher
  13239. (setq matcher (if todomatcher
  13240. (list 'and tagsmatcher todomatcher)
  13241. tagsmatcher))
  13242. (when todo-only
  13243. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13244. matcher)))
  13245. (cons match0 matcher)))
  13246. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13247. "Expand group tags in MATCH.
  13248. This replaces every group tag in MATCH with a regexp tag search.
  13249. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13250. will be replaced like this:
  13251. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13252. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13253. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13254. Replacing by a regexp preserves the structure of the match.
  13255. E.g., this expansion
  13256. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13257. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13258. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13259. A group tag in MATCH can contain regular expressions of its own.
  13260. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13261. will be replaced like this:
  13262. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13263. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13264. assumed to be a single group tag, and the function will return
  13265. the list of tags in this group.
  13266. When DOWNCASE is non-nil, expand downcased TAGS."
  13267. (if org-group-tags
  13268. (let* ((case-fold-search t)
  13269. (stable org-mode-syntax-table)
  13270. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13271. (taggroups (if downcased
  13272. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13273. taggroups)
  13274. taggroups))
  13275. (taggroups-keys (mapcar #'car taggroups))
  13276. (return-match (if downcased (downcase match) match))
  13277. (count 0)
  13278. (work-already-expanded tags-already-expanded)
  13279. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13280. ;; @ and _ are allowed as word-components in tags.
  13281. (modify-syntax-entry ?@ "w" stable)
  13282. (modify-syntax-entry ?_ "w" stable)
  13283. ;; Temporarily replace regexp-expressions in the match-expression.
  13284. (while (string-match "{.+?}" return-match)
  13285. (incf count)
  13286. (push (match-string 0 return-match) regexps-in-match)
  13287. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13288. (while (and taggroups-keys
  13289. (with-syntax-table stable
  13290. (string-match
  13291. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13292. (regexp-opt taggroups-keys) "\\>\\)") return-match)))
  13293. (let* ((dir (match-string 1 return-match))
  13294. (tag (match-string 2 return-match))
  13295. (tag (if downcased (downcase tag) tag)))
  13296. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13297. (member tag work-already-expanded))
  13298. (setq tags-in-group (assoc tag taggroups))
  13299. (push tag work-already-expanded)
  13300. ;; Recursively expand each tag in the group, if the tag hasn't
  13301. ;; already been expanded. Restore the match-data after all recursive calls.
  13302. (save-match-data
  13303. (let (tags-expanded)
  13304. (dolist (x (cdr tags-in-group))
  13305. (if (and (member x taggroups-keys)
  13306. (not (member x work-already-expanded)))
  13307. (setq tags-expanded
  13308. (delete-dups
  13309. (append
  13310. (org-tags-expand x t downcased
  13311. work-already-expanded)
  13312. tags-expanded)))
  13313. (setq tags-expanded
  13314. (append (list x) tags-expanded)))
  13315. (setq work-already-expanded
  13316. (delete-dups
  13317. (append tags-expanded
  13318. work-already-expanded))))
  13319. (setq tags-in-group
  13320. (delete-dups (cons (car tags-in-group)
  13321. tags-expanded)))))
  13322. ;; Filter tag-regexps from tags.
  13323. (setq regexp-in-group-escaped
  13324. (delq nil (mapcar (lambda (x)
  13325. (if (stringp x)
  13326. (and (equal "{" (substring x 0 1))
  13327. (equal "}" (substring x -1))
  13328. x)
  13329. x))
  13330. tags-in-group))
  13331. regexp-in-group
  13332. (mapcar (lambda (x)
  13333. (substring x 1 -1))
  13334. regexp-in-group-escaped)
  13335. tags-in-group
  13336. (delq nil (mapcar (lambda (x)
  13337. (if (stringp x)
  13338. (and (not (equal "{" (substring x 0 1)))
  13339. (not (equal "}" (substring x -1)))
  13340. x)
  13341. x))
  13342. tags-in-group)))
  13343. ;; If single-as-list, do no more in the while-loop.
  13344. (if (not single-as-list)
  13345. (progn
  13346. (when regexp-in-group
  13347. (setq regexp-in-group
  13348. (concat "\\|"
  13349. (mapconcat 'identity regexp-in-group
  13350. "\\|"))))
  13351. (setq tags-in-group
  13352. (concat dir
  13353. "{\\<"
  13354. (regexp-opt tags-in-group)
  13355. "\\>"
  13356. regexp-in-group
  13357. "}"))
  13358. (when (stringp tags-in-group)
  13359. (org-add-props tags-in-group '(grouptag t)))
  13360. (setq return-match
  13361. (replace-match tags-in-group t t return-match)))
  13362. (setq tags-in-group
  13363. (append regexp-in-group-escaped tags-in-group))))
  13364. (setq taggroups-keys (delete tag taggroups-keys))))
  13365. ;; Add the regular expressions back into the match-expression again.
  13366. (while regexps-in-match
  13367. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13368. (pop regexps-in-match)
  13369. return-match t t))
  13370. (decf count))
  13371. (if single-as-list
  13372. (if tags-in-group tags-in-group (list return-match))
  13373. return-match))
  13374. (if single-as-list
  13375. (list (if downcased (downcase match) match))
  13376. match)))
  13377. (defun org-op-to-function (op &optional stringp)
  13378. "Turn an operator into the appropriate function."
  13379. (setq op
  13380. (cond
  13381. ((equal op "<" ) '(< string< org-time<))
  13382. ((equal op ">" ) '(> org-string> org-time>))
  13383. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13384. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13385. ((member op '("=" "==")) '(= string= org-time=))
  13386. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13387. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13388. (defun org<> (a b) (not (= a b)))
  13389. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13390. (defun org-string>= (a b) (not (string< a b)))
  13391. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13392. (defun org-string<> (a b) (not (string= a b)))
  13393. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13394. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13395. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13396. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13397. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13398. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13399. (defun org-2ft (s)
  13400. "Convert S to a floating point time.
  13401. If S is already a number, just return it. If it is a string, parse
  13402. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13403. (cond
  13404. ((numberp s) s)
  13405. ((stringp s)
  13406. (condition-case nil
  13407. (float-time (apply 'encode-time (org-parse-time-string s)))
  13408. (error 0.)))
  13409. (t 0.)))
  13410. (defun org-time-today ()
  13411. "Time in seconds today at 0:00.
  13412. Returns the float number of seconds since the beginning of the
  13413. epoch to the beginning of today (00:00)."
  13414. (float-time (apply 'encode-time
  13415. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13416. (defun org-matcher-time (s)
  13417. "Interpret a time comparison value."
  13418. (save-match-data
  13419. (cond
  13420. ((string= s "<now>") (float-time))
  13421. ((string= s "<today>") (org-time-today))
  13422. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13423. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13424. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13425. (+ (org-time-today)
  13426. (* (string-to-number (match-string 1 s))
  13427. (cdr (assoc (match-string 2 s)
  13428. '(("d" . 86400.0) ("w" . 604800.0)
  13429. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13430. (t (org-2ft s)))))
  13431. (defun org-match-any-p (re list)
  13432. "Does re match any element of list?"
  13433. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13434. (delq nil list))
  13435. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13436. (defvar org-tags-overlay (make-overlay 1 1))
  13437. (org-detach-overlay org-tags-overlay)
  13438. (defun org-get-local-tags-at (&optional pos)
  13439. "Get a list of tags defined in the current headline."
  13440. (org-get-tags-at pos 'local))
  13441. (defun org-get-local-tags ()
  13442. "Get a list of tags defined in the current headline."
  13443. (org-get-tags-at nil 'local))
  13444. (defun org-get-tags-at (&optional pos local)
  13445. "Get a list of all headline tags applicable at POS.
  13446. POS defaults to point. If tags are inherited, the list contains
  13447. the targets in the same sequence as the headlines appear, i.e.
  13448. the tags of the current headline come last.
  13449. When LOCAL is non-nil, only return tags from the current headline,
  13450. ignore inherited ones."
  13451. (interactive)
  13452. (if (and org-trust-scanner-tags
  13453. (or (not pos) (equal pos (point)))
  13454. (not local))
  13455. org-scanner-tags
  13456. (let (tags ltags lastpos parent)
  13457. (save-excursion
  13458. (save-restriction
  13459. (widen)
  13460. (goto-char (or pos (point)))
  13461. (save-match-data
  13462. (catch 'done
  13463. (condition-case nil
  13464. (progn
  13465. (org-back-to-heading t)
  13466. (while (not (equal lastpos (point)))
  13467. (setq lastpos (point))
  13468. (when (looking-at
  13469. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13470. (setq ltags (org-split-string
  13471. (org-match-string-no-properties 1) ":"))
  13472. (when parent
  13473. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13474. (setq tags (append
  13475. (if parent
  13476. (org-remove-uninherited-tags ltags)
  13477. ltags)
  13478. tags)))
  13479. (or org-use-tag-inheritance (throw 'done t))
  13480. (if local (throw 'done t))
  13481. (or (org-up-heading-safe) (error nil))
  13482. (setq parent t)))
  13483. (error nil)))))
  13484. (if local
  13485. tags
  13486. (reverse (delete-dups
  13487. (reverse (append
  13488. (org-remove-uninherited-tags
  13489. org-file-tags) tags)))))))))
  13490. (defun org-add-prop-inherited (s)
  13491. (add-text-properties 0 (length s) '(inherited t) s)
  13492. s)
  13493. (defun org-toggle-tag (tag &optional onoff)
  13494. "Toggle the tag TAG for the current line.
  13495. If ONOFF is `on' or `off', don't toggle but set to this state."
  13496. (let (res current)
  13497. (save-excursion
  13498. (org-back-to-heading t)
  13499. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13500. (point-at-eol) t)
  13501. (progn
  13502. (setq current (match-string 1))
  13503. (replace-match ""))
  13504. (setq current ""))
  13505. (setq current (nreverse (org-split-string current ":")))
  13506. (cond
  13507. ((eq onoff 'on)
  13508. (setq res t)
  13509. (or (member tag current) (push tag current)))
  13510. ((eq onoff 'off)
  13511. (or (not (member tag current)) (setq current (delete tag current))))
  13512. (t (if (member tag current)
  13513. (setq current (delete tag current))
  13514. (setq res t)
  13515. (push tag current))))
  13516. (end-of-line 1)
  13517. (if current
  13518. (progn
  13519. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13520. (org-set-tags nil t))
  13521. (delete-horizontal-space))
  13522. (run-hooks 'org-after-tags-change-hook))
  13523. res))
  13524. (defun org-align-tags-here (to-col)
  13525. ;; Assumes that this is a headline
  13526. "Align tags on the current headline to TO-COL."
  13527. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13528. (beginning-of-line 1)
  13529. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13530. (< pos (match-beginning 2)))
  13531. (progn
  13532. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13533. (goto-char (match-beginning 1))
  13534. (insert " ")
  13535. (delete-region (point) (1+ (match-beginning 2)))
  13536. (setq ncol (max (current-column)
  13537. (1+ col)
  13538. (if (> to-col 0)
  13539. to-col
  13540. (- (abs to-col) tags-l))))
  13541. (setq p (point))
  13542. (insert (make-string (- ncol (current-column)) ?\ ))
  13543. (setq ncol (current-column))
  13544. (when indent-tabs-mode (tabify p (point-at-eol)))
  13545. (org-move-to-column (min ncol col)))
  13546. (goto-char pos))))
  13547. (defun org-set-tags-command (&optional arg just-align)
  13548. "Call the set-tags command for the current entry."
  13549. (interactive "P")
  13550. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13551. (org-set-tags arg just-align)
  13552. (save-excursion
  13553. (unless (and (org-region-active-p)
  13554. org-loop-over-headlines-in-active-region)
  13555. (org-back-to-heading t))
  13556. (org-set-tags arg just-align))))
  13557. (defun org-set-tags-to (data)
  13558. "Set the tags of the current entry to DATA, replacing the current tags.
  13559. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13560. If DATA is nil or the empty string, any tags will be removed."
  13561. (interactive "sTags: ")
  13562. (setq data
  13563. (cond
  13564. ((eq data nil) "")
  13565. ((equal data "") "")
  13566. ((stringp data)
  13567. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13568. ":"))
  13569. ((listp data)
  13570. (concat ":" (mapconcat 'identity data ":") ":"))))
  13571. (when data
  13572. (save-excursion
  13573. (org-back-to-heading t)
  13574. (when (looking-at org-complex-heading-regexp)
  13575. (if (match-end 5)
  13576. (progn
  13577. (goto-char (match-beginning 5))
  13578. (insert data)
  13579. (delete-region (point) (point-at-eol))
  13580. (org-set-tags nil 'align))
  13581. (goto-char (point-at-eol))
  13582. (insert " " data)
  13583. (org-set-tags nil 'align)))
  13584. (beginning-of-line 1)
  13585. (if (looking-at ".*?\\([ \t]+\\)$")
  13586. (delete-region (match-beginning 1) (match-end 1))))))
  13587. (defun org-align-all-tags ()
  13588. "Align the tags i all headings."
  13589. (interactive)
  13590. (save-excursion
  13591. (or (ignore-errors (org-back-to-heading t))
  13592. (outline-next-heading))
  13593. (if (org-at-heading-p)
  13594. (org-set-tags t)
  13595. (message "No headings"))))
  13596. (defvar org-indent-indentation-per-level)
  13597. (defun org-set-tags (&optional arg just-align)
  13598. "Set the tags for the current headline.
  13599. With prefix ARG, realign all tags in headings in the current buffer.
  13600. When JUST-ALIGN is non-nil, only align tags."
  13601. (interactive "P")
  13602. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13603. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13604. 'region-start-level 'region))
  13605. org-loop-over-headlines-in-active-region)
  13606. (org-map-entries
  13607. ;; We don't use ARG and JUST-ALIGN here because these args
  13608. ;; are not useful when looping over headlines.
  13609. `(org-set-tags)
  13610. org-loop-over-headlines-in-active-region
  13611. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13612. (let* ((re org-outline-regexp-bol)
  13613. (current (unless arg (org-get-tags-string)))
  13614. (col (current-column))
  13615. (org-setting-tags t)
  13616. table current-tags inherited-tags ; computed below when needed
  13617. tags p0 c0 c1 rpl di tc level)
  13618. (if arg
  13619. (save-excursion
  13620. (goto-char (point-min))
  13621. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13622. (while (re-search-forward re nil t)
  13623. (org-set-tags nil t)
  13624. (end-of-line 1)))
  13625. (message "All tags realigned to column %d" org-tags-column))
  13626. (if just-align
  13627. (setq tags current)
  13628. ;; Get a new set of tags from the user
  13629. (save-excursion
  13630. (setq table (append org-tag-persistent-alist
  13631. (or org-tag-alist (org-get-buffer-tags))
  13632. (and
  13633. org-complete-tags-always-offer-all-agenda-tags
  13634. (org-global-tags-completion-table
  13635. (org-agenda-files))))
  13636. org-last-tags-completion-table table
  13637. current-tags (org-split-string current ":")
  13638. inherited-tags (nreverse
  13639. (nthcdr (length current-tags)
  13640. (nreverse (org-get-tags-at))))
  13641. tags
  13642. (if (or (eq t org-use-fast-tag-selection)
  13643. (and org-use-fast-tag-selection
  13644. (delq nil (mapcar 'cdr table))))
  13645. (org-fast-tag-selection
  13646. current-tags inherited-tags table
  13647. (if org-fast-tag-selection-include-todo
  13648. org-todo-key-alist))
  13649. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13650. (org-trim
  13651. (org-icompleting-read "Tags: "
  13652. 'org-tags-completion-function
  13653. nil nil current 'org-tags-history))))))
  13654. (while (string-match "[-+&]+" tags)
  13655. ;; No boolean logic, just a list
  13656. (setq tags (replace-match ":" t t tags))))
  13657. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13658. (if org-tags-sort-function
  13659. (setq tags (mapconcat 'identity
  13660. (sort (org-split-string
  13661. tags (org-re "[^[:alnum:]_@#%]+"))
  13662. org-tags-sort-function) ":")))
  13663. (if (string-match "\\`[\t ]*\\'" tags)
  13664. (setq tags "")
  13665. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13666. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13667. ;; Insert new tags at the correct column
  13668. (beginning-of-line 1)
  13669. (setq level (or (and (looking-at org-outline-regexp)
  13670. (- (match-end 0) (point) 1))
  13671. 1))
  13672. (cond
  13673. ((and (equal current "") (equal tags "")))
  13674. ((re-search-forward
  13675. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13676. (point-at-eol) t)
  13677. (if (equal tags "")
  13678. (setq rpl "")
  13679. (goto-char (match-beginning 0))
  13680. (setq c0 (current-column)
  13681. ;; compute offset for the case of org-indent-mode active
  13682. di (if (org-bound-and-true-p org-indent-mode)
  13683. (* (1- org-indent-indentation-per-level) (1- level))
  13684. 0)
  13685. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13686. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13687. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (string-width tags))))
  13688. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13689. (replace-match rpl t t)
  13690. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13691. tags)
  13692. (t (error "Tags alignment failed")))
  13693. (org-move-to-column col)
  13694. (unless just-align
  13695. (run-hooks 'org-after-tags-change-hook))))))
  13696. (defun org-change-tag-in-region (beg end tag off)
  13697. "Add or remove TAG for each entry in the region.
  13698. This works in the agenda, and also in an org-mode buffer."
  13699. (interactive
  13700. (list (region-beginning) (region-end)
  13701. (let ((org-last-tags-completion-table
  13702. (if (derived-mode-p 'org-mode)
  13703. (org-uniquify
  13704. (delq nil (append (org-get-buffer-tags)
  13705. (org-global-tags-completion-table))))
  13706. (org-global-tags-completion-table))))
  13707. (org-icompleting-read
  13708. "Tag: " 'org-tags-completion-function nil nil nil
  13709. 'org-tags-history))
  13710. (progn
  13711. (message "[s]et or [r]emove? ")
  13712. (equal (read-char-exclusive) ?r))))
  13713. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13714. (let ((agendap (equal major-mode 'org-agenda-mode))
  13715. l1 l2 m buf pos newhead (cnt 0))
  13716. (goto-char end)
  13717. (setq l2 (1- (org-current-line)))
  13718. (goto-char beg)
  13719. (setq l1 (org-current-line))
  13720. (loop for l from l1 to l2 do
  13721. (org-goto-line l)
  13722. (setq m (get-text-property (point) 'org-hd-marker))
  13723. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13724. (and agendap m))
  13725. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13726. pos (if agendap m (point)))
  13727. (with-current-buffer buf
  13728. (save-excursion
  13729. (save-restriction
  13730. (goto-char pos)
  13731. (setq cnt (1+ cnt))
  13732. (org-toggle-tag tag (if off 'off 'on))
  13733. (setq newhead (org-get-heading)))))
  13734. (and agendap (org-agenda-change-all-lines newhead m))))
  13735. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13736. (defun org-tags-completion-function (string predicate &optional flag)
  13737. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13738. (confirm (lambda (x) (stringp (car x)))))
  13739. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13740. (setq s1 (match-string 1 string)
  13741. s2 (match-string 2 string))
  13742. (setq s1 "" s2 string))
  13743. (cond
  13744. ((eq flag nil)
  13745. ;; try completion
  13746. (setq rtn (try-completion s2 ctable confirm))
  13747. (if (stringp rtn)
  13748. (setq rtn
  13749. (concat s1 s2 (substring rtn (length s2))
  13750. (if (and org-add-colon-after-tag-completion
  13751. (assoc rtn ctable))
  13752. ":" ""))))
  13753. rtn)
  13754. ((eq flag t)
  13755. ;; all-completions
  13756. (all-completions s2 ctable confirm))
  13757. ((eq flag 'lambda)
  13758. ;; exact match?
  13759. (assoc s2 ctable)))))
  13760. (defun org-fast-tag-insert (kwd tags face &optional end)
  13761. "Insert KDW, and the TAGS, the latter with face FACE.
  13762. Also insert END."
  13763. (insert (format "%-12s" (concat kwd ":"))
  13764. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13765. (or end "")))
  13766. (defun org-fast-tag-show-exit (flag)
  13767. (save-excursion
  13768. (org-goto-line 3)
  13769. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13770. (replace-match ""))
  13771. (when flag
  13772. (end-of-line 1)
  13773. (org-move-to-column (- (window-width) 19) t)
  13774. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13775. (defun org-set-current-tags-overlay (current prefix)
  13776. "Add an overlay to CURRENT tag with PREFIX."
  13777. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13778. (if (featurep 'xemacs)
  13779. (org-overlay-display org-tags-overlay (concat prefix s)
  13780. 'secondary-selection)
  13781. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13782. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13783. (defvar org-last-tag-selection-key nil)
  13784. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13785. "Fast tag selection with single keys.
  13786. CURRENT is the current list of tags in the headline, INHERITED is the
  13787. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13788. possibly with grouping information. TODO-TABLE is a similar table with
  13789. TODO keywords, should these have keys assigned to them.
  13790. If the keys are nil, a-z are automatically assigned.
  13791. Returns the new tags string, or nil to not change the current settings."
  13792. (let* ((fulltable (append table todo-table))
  13793. (maxlen (apply 'max (mapcar
  13794. (lambda (x)
  13795. (if (stringp (car x)) (string-width (car x)) 0))
  13796. fulltable)))
  13797. (buf (current-buffer))
  13798. (expert (eq org-fast-tag-selection-single-key 'expert))
  13799. (buffer-tags nil)
  13800. (fwidth (+ maxlen 3 1 3))
  13801. (ncol (/ (- (window-width) 4) fwidth))
  13802. (i-face 'org-done)
  13803. (c-face 'org-todo)
  13804. tg cnt e c char c1 c2 ntable tbl rtn
  13805. ov-start ov-end ov-prefix
  13806. (exit-after-next org-fast-tag-selection-single-key)
  13807. (done-keywords org-done-keywords)
  13808. groups ingroup intaggroup)
  13809. (save-excursion
  13810. (beginning-of-line 1)
  13811. (if (looking-at
  13812. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13813. (setq ov-start (match-beginning 1)
  13814. ov-end (match-end 1)
  13815. ov-prefix "")
  13816. (setq ov-start (1- (point-at-eol))
  13817. ov-end (1+ ov-start))
  13818. (skip-chars-forward "^\n\r")
  13819. (setq ov-prefix
  13820. (concat
  13821. (buffer-substring (1- (point)) (point))
  13822. (if (> (current-column) org-tags-column)
  13823. " "
  13824. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13825. (move-overlay org-tags-overlay ov-start ov-end)
  13826. (save-window-excursion
  13827. (if expert
  13828. (set-buffer (get-buffer-create " *Org tags*"))
  13829. (delete-other-windows)
  13830. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13831. (org-switch-to-buffer-other-window " *Org tags*"))
  13832. (erase-buffer)
  13833. (org-set-local 'org-done-keywords done-keywords)
  13834. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13835. (org-fast-tag-insert "Current" current c-face "\n\n")
  13836. (org-fast-tag-show-exit exit-after-next)
  13837. (org-set-current-tags-overlay current ov-prefix)
  13838. (setq tbl fulltable char ?a cnt 0)
  13839. (while (setq e (pop tbl))
  13840. (cond
  13841. ((eq (car e) :startgroup)
  13842. (push '() groups) (setq ingroup t)
  13843. (unless (zerop cnt)
  13844. (setq cnt 0)
  13845. (insert "\n"))
  13846. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13847. ((eq (car e) :endgroup)
  13848. (setq ingroup nil cnt 0)
  13849. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13850. ((eq (car e) :startgrouptag)
  13851. (setq intaggroup t)
  13852. (unless (zerop cnt)
  13853. (setq cnt 0)
  13854. (insert "\n"))
  13855. (insert "[ "))
  13856. ((eq (car e) :endgrouptag)
  13857. (setq intaggroup nil cnt 0)
  13858. (insert "]\n"))
  13859. ((equal e '(:newline))
  13860. (unless (zerop cnt)
  13861. (setq cnt 0)
  13862. (insert "\n")
  13863. (setq e (car tbl))
  13864. (while (equal (car tbl) '(:newline))
  13865. (insert "\n")
  13866. (setq tbl (cdr tbl)))))
  13867. ((equal e '(:grouptags)) (insert " : "))
  13868. (t
  13869. (setq tg (copy-sequence (car e)) c2 nil)
  13870. (if (cdr e)
  13871. (setq c (cdr e))
  13872. ;; automatically assign a character.
  13873. (setq c1 (string-to-char
  13874. (downcase (substring
  13875. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13876. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13877. (while (or (rassoc char ntable) (rassoc char table))
  13878. (setq char (1+ char)))
  13879. (setq c2 c1))
  13880. (setq c (or c2 char)))
  13881. (when ingroup (push tg (car groups)))
  13882. (setq tg (org-add-props tg nil 'face
  13883. (cond
  13884. ((not (assoc tg table))
  13885. (org-get-todo-face tg))
  13886. ((member tg current) c-face)
  13887. ((member tg inherited) i-face))))
  13888. (when (equal (caar tbl) :grouptags)
  13889. (org-add-props tg nil 'face 'org-tag-group))
  13890. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13891. (insert "[" c "] " tg (make-string
  13892. (- fwidth 4 (length tg)) ?\ ))
  13893. (push (cons tg c) ntable)
  13894. (when (= (incf cnt) ncol)
  13895. (insert "\n")
  13896. (when (or ingroup intaggroup) (insert " "))
  13897. (setq cnt 0)))))
  13898. (setq ntable (nreverse ntable))
  13899. (insert "\n")
  13900. (goto-char (point-min))
  13901. (unless expert (org-fit-window-to-buffer))
  13902. (setq rtn
  13903. (catch 'exit
  13904. (while t
  13905. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13906. (if (not groups) "no " "")
  13907. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13908. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13909. (setq org-last-tag-selection-key c)
  13910. (cond
  13911. ((= c ?\r) (throw 'exit t))
  13912. ((= c ?!)
  13913. (setq groups (not groups))
  13914. (goto-char (point-min))
  13915. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13916. ((= c ?\C-c)
  13917. (if (not expert)
  13918. (org-fast-tag-show-exit
  13919. (setq exit-after-next (not exit-after-next)))
  13920. (setq expert nil)
  13921. (delete-other-windows)
  13922. (set-window-buffer (split-window-vertically) " *Org tags*")
  13923. (org-switch-to-buffer-other-window " *Org tags*")
  13924. (org-fit-window-to-buffer)))
  13925. ((or (= c ?\C-g)
  13926. (and (= c ?q) (not (rassoc c ntable))))
  13927. (org-detach-overlay org-tags-overlay)
  13928. (setq quit-flag t))
  13929. ((= c ?\ )
  13930. (setq current nil)
  13931. (when exit-after-next (setq exit-after-next 'now)))
  13932. ((= c ?\t)
  13933. (condition-case nil
  13934. (setq tg (org-icompleting-read
  13935. "Tag: "
  13936. (or buffer-tags
  13937. (with-current-buffer buf
  13938. (org-get-buffer-tags)))))
  13939. (quit (setq tg "")))
  13940. (when (string-match "\\S-" tg)
  13941. (add-to-list 'buffer-tags (list tg))
  13942. (if (member tg current)
  13943. (setq current (delete tg current))
  13944. (push tg current)))
  13945. (when exit-after-next (setq exit-after-next 'now)))
  13946. ((setq e (rassoc c todo-table) tg (car e))
  13947. (with-current-buffer buf
  13948. (save-excursion (org-todo tg)))
  13949. (when exit-after-next (setq exit-after-next 'now)))
  13950. ((setq e (rassoc c ntable) tg (car e))
  13951. (if (member tg current)
  13952. (setq current (delete tg current))
  13953. (loop for g in groups do
  13954. (when (member tg g)
  13955. (dolist (x g) (setq current (delete x current)))))
  13956. (push tg current))
  13957. (when exit-after-next (setq exit-after-next 'now))))
  13958. ;; Create a sorted list
  13959. (setq current
  13960. (sort current
  13961. (lambda (a b)
  13962. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13963. (when (eq exit-after-next 'now) (throw 'exit t))
  13964. (goto-char (point-min))
  13965. (beginning-of-line 2)
  13966. (delete-region (point) (point-at-eol))
  13967. (org-fast-tag-insert "Current" current c-face)
  13968. (org-set-current-tags-overlay current ov-prefix)
  13969. (while (re-search-forward
  13970. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13971. (setq tg (match-string 1))
  13972. (add-text-properties
  13973. (match-beginning 1) (match-end 1)
  13974. (list 'face
  13975. (cond
  13976. ((member tg current) c-face)
  13977. ((member tg inherited) i-face)
  13978. (t (get-text-property (match-beginning 1) 'face))))))
  13979. (goto-char (point-min)))))
  13980. (org-detach-overlay org-tags-overlay)
  13981. (if rtn
  13982. (mapconcat 'identity current ":")
  13983. nil))))
  13984. (defun org-get-tags-string ()
  13985. "Get the TAGS string in the current headline."
  13986. (unless (org-at-heading-p t)
  13987. (user-error "Not on a heading"))
  13988. (save-excursion
  13989. (beginning-of-line 1)
  13990. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13991. (org-match-string-no-properties 1)
  13992. "")))
  13993. (defun org-get-tags ()
  13994. "Get the list of tags specified in the current headline."
  13995. (org-split-string (org-get-tags-string) ":"))
  13996. (defun org-get-buffer-tags ()
  13997. "Get a table of all tags used in the buffer, for completion."
  13998. (org-with-wide-buffer
  13999. (goto-char (point-min))
  14000. (let ((tag-re (concat org-outline-regexp-bol
  14001. "\\(?:.*?[ \t]\\)?"
  14002. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14003. tags)
  14004. (while (re-search-forward tag-re nil t)
  14005. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14006. (push tag tags)))
  14007. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14008. ;;;; The mapping API
  14009. (defun org-map-entries (func &optional match scope &rest skip)
  14010. "Call FUNC at each headline selected by MATCH in SCOPE.
  14011. FUNC is a function or a lisp form. The function will be called without
  14012. arguments, with the cursor positioned at the beginning of the headline.
  14013. The return values of all calls to the function will be collected and
  14014. returned as a list.
  14015. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14016. does not need to preserve point. After evaluation, the cursor will be
  14017. moved to the end of the line (presumably of the headline of the
  14018. processed entry) and search continues from there. Under some
  14019. circumstances, this may not produce the wanted results. For example,
  14020. if you have removed (e.g. archived) the current (sub)tree it could
  14021. mean that the next entry will be skipped entirely. In such cases, you
  14022. can specify the position from where search should continue by making
  14023. FUNC set the variable `org-map-continue-from' to the desired buffer
  14024. position.
  14025. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14026. Only headlines that are matched by this query will be considered during
  14027. the iteration. When MATCH is nil or t, all headlines will be
  14028. visited by the iteration.
  14029. SCOPE determines the scope of this command. It can be any of:
  14030. nil The current buffer, respecting the restriction if any
  14031. tree The subtree started with the entry at point
  14032. region The entries within the active region, if any
  14033. region-start-level
  14034. The entries within the active region, but only those at
  14035. the same level than the first one.
  14036. file The current buffer, without restriction
  14037. file-with-archives
  14038. The current buffer, and any archives associated with it
  14039. agenda All agenda files
  14040. agenda-with-archives
  14041. All agenda files with any archive files associated with them
  14042. \(file1 file2 ...)
  14043. If this is a list, all files in the list will be scanned
  14044. The remaining args are treated as settings for the skipping facilities of
  14045. the scanner. The following items can be given here:
  14046. archive skip trees with the archive tag
  14047. comment skip trees with the COMMENT keyword
  14048. function or Emacs Lisp form:
  14049. will be used as value for `org-agenda-skip-function', so
  14050. whenever the function returns a position, FUNC will not be
  14051. called for that entry and search will continue from the
  14052. position returned
  14053. If your function needs to retrieve the tags including inherited tags
  14054. at the *current* entry, you can use the value of the variable
  14055. `org-scanner-tags' which will be much faster than getting the value
  14056. with `org-get-tags-at'. If your function gets properties with
  14057. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14058. to t around the call to `org-entry-properties' to get the same speedup.
  14059. Note that if your function moves around to retrieve tags and properties at
  14060. a *different* entry, you cannot use these techniques."
  14061. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14062. (not (org-region-active-p)))
  14063. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14064. (org-agenda-skip-archived-trees (memq 'archive skip))
  14065. (org-agenda-skip-comment-trees (memq 'comment skip))
  14066. (org-agenda-skip-function
  14067. (car (org-delete-all '(comment archive) skip)))
  14068. (org-tags-match-list-sublevels t)
  14069. (start-level (eq scope 'region-start-level))
  14070. matcher file res
  14071. org-todo-keywords-for-agenda
  14072. org-done-keywords-for-agenda
  14073. org-todo-keyword-alist-for-agenda
  14074. org-tag-alist-for-agenda
  14075. todo-only)
  14076. (cond
  14077. ((eq match t) (setq matcher t))
  14078. ((eq match nil) (setq matcher t))
  14079. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14080. (save-excursion
  14081. (save-restriction
  14082. (cond ((eq scope 'tree)
  14083. (org-back-to-heading t)
  14084. (org-narrow-to-subtree)
  14085. (setq scope nil))
  14086. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14087. (org-region-active-p))
  14088. ;; If needed, set start-level to a string like "2"
  14089. (when start-level
  14090. (save-excursion
  14091. (goto-char (region-beginning))
  14092. (unless (org-at-heading-p) (outline-next-heading))
  14093. (setq start-level (org-current-level))))
  14094. (narrow-to-region (region-beginning)
  14095. (save-excursion
  14096. (goto-char (region-end))
  14097. (unless (and (bolp) (org-at-heading-p))
  14098. (outline-next-heading))
  14099. (point)))
  14100. (setq scope nil)))
  14101. (if (not scope)
  14102. (progn
  14103. (org-agenda-prepare-buffers
  14104. (list (buffer-file-name (current-buffer))))
  14105. (setq res (org-scan-tags func matcher todo-only start-level)))
  14106. ;; Get the right scope
  14107. (cond
  14108. ((and scope (listp scope) (symbolp (car scope)))
  14109. (setq scope (eval scope)))
  14110. ((eq scope 'agenda)
  14111. (setq scope (org-agenda-files t)))
  14112. ((eq scope 'agenda-with-archives)
  14113. (setq scope (org-agenda-files t))
  14114. (setq scope (org-add-archive-files scope)))
  14115. ((eq scope 'file)
  14116. (setq scope (list (buffer-file-name))))
  14117. ((eq scope 'file-with-archives)
  14118. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14119. (org-agenda-prepare-buffers scope)
  14120. (while (setq file (pop scope))
  14121. (with-current-buffer (org-find-base-buffer-visiting file)
  14122. (save-excursion
  14123. (save-restriction
  14124. (widen)
  14125. (goto-char (point-min))
  14126. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14127. res)))
  14128. ;;; Properties API
  14129. (defconst org-special-properties
  14130. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14131. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14132. "The special properties valid in Org mode.
  14133. These are properties that are not defined in the property drawer,
  14134. but in some other way.")
  14135. (defconst org-default-properties
  14136. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14137. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14138. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14139. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14140. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14141. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14142. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14143. "Some properties that are used by Org mode for various purposes.
  14144. Being in this list makes sure that they are offered for completion.")
  14145. (defun org--update-property-plist (key val props)
  14146. "Associate KEY to VAL in alist PROPS.
  14147. Modifications are made by side-effect. Return new alist."
  14148. (let* ((appending (string= (substring key -1) "+"))
  14149. (key (if appending (substring key 0 -1) key))
  14150. (old (assoc-string key props t)))
  14151. (if (not old) (cons (cons key val) props)
  14152. (setcdr old (if appending (concat (cdr old) " " val) val))
  14153. props)))
  14154. (defun org-get-property-block (&optional beg force)
  14155. "Return the (beg . end) range of the body of the property drawer.
  14156. BEG is the beginning of the current subtree, or of the part
  14157. before the first headline. If it is not given, it will be found.
  14158. If the drawer does not exist, create it if FORCE is non-nil, or
  14159. return nil."
  14160. (org-with-wide-buffer
  14161. (when beg (goto-char beg))
  14162. (unless (org-before-first-heading-p)
  14163. (let ((beg (cond (beg)
  14164. ((or (not (featurep 'org-inlinetask))
  14165. (org-inlinetask-in-task-p))
  14166. (org-back-to-heading t))
  14167. (t (org-with-limited-levels (org-back-to-heading t))))))
  14168. (forward-line)
  14169. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14170. (cond ((looking-at org-property-drawer-re)
  14171. (forward-line)
  14172. (cons (point) (progn (goto-char (match-end 0))
  14173. (line-beginning-position))))
  14174. (force
  14175. (goto-char beg)
  14176. (org-insert-property-drawer)
  14177. (let ((pos (save-excursion (search-forward ":END:")
  14178. (line-beginning-position))))
  14179. (cons pos pos))))))))
  14180. (defun org-at-property-p ()
  14181. "Non-nil when point is inside a property drawer.
  14182. See `org-property-re' for match data, if applicable."
  14183. (save-excursion
  14184. (beginning-of-line)
  14185. (and (looking-at org-property-re)
  14186. (let ((property-drawer (save-match-data (org-get-property-block))))
  14187. (and property-drawer
  14188. (>= (point) (car property-drawer))
  14189. (< (point) (cdr property-drawer)))))))
  14190. (defun org-property-action ()
  14191. "Do an action on properties."
  14192. (interactive)
  14193. (unless (org-at-property-p) (user-error "Not at a property"))
  14194. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14195. (let ((c (read-char-exclusive)))
  14196. (case c
  14197. (?s (call-interactively #'org-set-property))
  14198. (?d (call-interactively #'org-delete-property))
  14199. (?D (call-interactively #'org-delete-property-globally))
  14200. (?c (call-interactively #'org-compute-property-at-point))
  14201. (otherwise (user-error "No such property action %c" c)))))
  14202. (defun org-inc-effort ()
  14203. "Increment the value of the effort property in the current entry."
  14204. (interactive)
  14205. (org-set-effort nil t))
  14206. (defvar org-clock-effort) ; Defined in org-clock.el.
  14207. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14208. (defun org-set-effort (&optional value increment)
  14209. "Set the effort property of the current entry.
  14210. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14211. 10th allowed value.
  14212. When INCREMENT is non-nil, set the property to the next allowed value."
  14213. (interactive "P")
  14214. (if (equal value 0) (setq value 10))
  14215. (let* ((completion-ignore-case t)
  14216. (prop org-effort-property)
  14217. (cur (org-entry-get nil prop))
  14218. (allowed (org-property-get-allowed-values nil prop 'table))
  14219. (existing (mapcar 'list (org-property-values prop)))
  14220. (heading (nth 4 (org-heading-components)))
  14221. rpl
  14222. (val (cond
  14223. ((stringp value) value)
  14224. ((and allowed (integerp value))
  14225. (or (car (nth (1- value) allowed))
  14226. (car (org-last allowed))))
  14227. ((and allowed increment)
  14228. (or (caadr (member (list cur) allowed))
  14229. (user-error "Allowed effort values are not set")))
  14230. (allowed
  14231. (message "Select 1-9,0, [RET%s]: %s"
  14232. (if cur (concat "=" cur) "")
  14233. (mapconcat 'car allowed " "))
  14234. (setq rpl (read-char-exclusive))
  14235. (if (equal rpl ?\r)
  14236. cur
  14237. (setq rpl (- rpl ?0))
  14238. (if (equal rpl 0) (setq rpl 10))
  14239. (if (and (> rpl 0) (<= rpl (length allowed)))
  14240. (car (nth (1- rpl) allowed))
  14241. (org-completing-read "Effort: " allowed nil))))
  14242. (t
  14243. (let (org-completion-use-ido org-completion-use-iswitchb)
  14244. (org-completing-read
  14245. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14246. (concat "[" cur "]") "")
  14247. ": ")
  14248. existing nil nil "" nil cur))))))
  14249. (unless (equal (org-entry-get nil prop) val)
  14250. (org-entry-put nil prop val))
  14251. (org-refresh-property
  14252. '((effort . identity)
  14253. (effort-minutes . org-duration-string-to-minutes))
  14254. val)
  14255. (when (string= heading org-clock-current-task)
  14256. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14257. (org-clock-update-mode-line))
  14258. (message "%s is now %s" prop val)))
  14259. (defun org-entry-properties (&optional pom which)
  14260. "Get all properties of the current entry.
  14261. When POM is a buffer position, get all properties from the entry
  14262. there instead.
  14263. This includes the TODO keyword, the tags, time strings for
  14264. deadline, scheduled, and clocking, and any additional properties
  14265. defined in the entry.
  14266. If WHICH is nil or `all', get all properties. If WHICH is
  14267. `special' or `standard', only get that subclass. If WHICH is
  14268. a string, only get that property.
  14269. Return value is an alist. Keys are properties, as upcased
  14270. strings."
  14271. (org-with-point-at pom
  14272. (when (and (derived-mode-p 'org-mode)
  14273. (ignore-errors (org-back-to-heading t)))
  14274. (catch 'exit
  14275. (let* ((beg (point))
  14276. (specific (and (stringp which) (upcase which)))
  14277. (which (cond ((not specific) which)
  14278. ((member specific org-special-properties) 'special)
  14279. (t 'standard)))
  14280. props)
  14281. ;; Get the special properties, like TODO and TAGS.
  14282. (when (memq which '(nil all special))
  14283. (when (or (not specific) (string= specific "CLOCKSUM"))
  14284. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14285. (when clocksum
  14286. (push (cons "CLOCKSUM"
  14287. (org-columns-number-to-string
  14288. (/ (float clocksum) 60.) 'add_times))
  14289. props)))
  14290. (when specific (throw 'exit props)))
  14291. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14292. (let ((clocksumt (get-text-property (point)
  14293. :org-clock-minutes-today)))
  14294. (when clocksumt
  14295. (push (cons "CLOCKSUM_T"
  14296. (org-columns-number-to-string
  14297. (/ (float clocksumt) 60.) 'add_times))
  14298. props)))
  14299. (when specific (throw 'exit props)))
  14300. (when (or (not specific) (string= specific "ITEM"))
  14301. (when (looking-at org-complex-heading-regexp)
  14302. (push (cons "ITEM"
  14303. (concat
  14304. (org-match-string-no-properties 1)
  14305. (let ((title (org-match-string-no-properties 4)))
  14306. (when (org-string-nw-p title)
  14307. (concat " " (org-remove-tabs title))))))
  14308. props))
  14309. (when specific (throw 'exit props)))
  14310. (when (or (not specific) (string= specific "TODO"))
  14311. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14312. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  14313. (when specific (throw 'exit props)))
  14314. (when (or (not specific) (string= specific "PRIORITY"))
  14315. (when (looking-at org-priority-regexp)
  14316. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  14317. (when specific (throw 'exit props)))
  14318. (when (or (not specific) (string= specific "FILE"))
  14319. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14320. props)
  14321. (when specific (throw 'exit props)))
  14322. (when (or (not specific) (string= specific "TAGS"))
  14323. (let ((value (org-string-nw-p (org-get-tags-string))))
  14324. (when value (push (cons "TAGS" value) props)))
  14325. (when specific (throw 'exit props)))
  14326. (when (or (not specific) (string= specific "ALLTAGS"))
  14327. (let ((value (org-get-tags-at)))
  14328. (when value
  14329. (push (cons "ALLTAGS"
  14330. (format ":%s:" (mapconcat #'identity value ":")))
  14331. props)))
  14332. (when specific (throw 'exit props)))
  14333. (when (or (not specific) (string= specific "BLOCKED"))
  14334. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14335. (when specific (throw 'exit props)))
  14336. (when (or (not specific)
  14337. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14338. (forward-line)
  14339. (when (org-looking-at-p org-planning-line-re)
  14340. (end-of-line)
  14341. (let ((bol (line-beginning-position))
  14342. ;; Backward compatibility: time keywords used to
  14343. ;; be configurable (before 8.3). Make sure we
  14344. ;; get the correct keyword.
  14345. (key-assoc `(("CLOSED" . ,org-closed-string)
  14346. ("DEADLINE" . ,org-deadline-string)
  14347. ("SCHEDULED" . ,org-scheduled-string))))
  14348. (dolist (pair (if specific (list (assoc specific key-assoc))
  14349. key-assoc))
  14350. (save-excursion
  14351. (when (search-backward (cdr pair) bol t)
  14352. (goto-char (match-end 0))
  14353. (skip-chars-forward " \t")
  14354. (and (looking-at org-ts-regexp-both)
  14355. (push (cons (car pair)
  14356. (org-match-string-no-properties 0))
  14357. props)))))))
  14358. (when specific (throw 'exit props)))
  14359. (when (or (not specific)
  14360. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14361. (let ((find-ts
  14362. (lambda (end ts)
  14363. (let ((regexp (if (or (string= specific "TIMESTAMP")
  14364. (assoc "TIMESTAMP_IA" ts))
  14365. org-ts-regexp
  14366. org-ts-regexp-both)))
  14367. (catch 'next
  14368. (while (re-search-forward regexp end t)
  14369. (backward-char)
  14370. (let ((object (org-element-context)))
  14371. ;; Accept to match timestamps in node
  14372. ;; properties, too.
  14373. (when (memq (org-element-type object)
  14374. '(node-property timestamp))
  14375. (let ((type
  14376. (org-element-property :type object)))
  14377. (cond
  14378. ((and (memq type '(active active-range))
  14379. (not (equal specific "TIMESTAMP_IA")))
  14380. (unless (assoc "TIMESTAMP" ts)
  14381. (push (cons "TIMESTAMP"
  14382. (org-element-property
  14383. :raw-value object))
  14384. ts)
  14385. (when specific (throw 'exit ts))))
  14386. ((and (memq type '(inactive inactive-range))
  14387. (not (string= specific "TIMESTAMP")))
  14388. (unless (assoc "TIMESTAMP_IA" ts)
  14389. (push (cons "TIMESTAMP_IA"
  14390. (org-element-property
  14391. :raw-value object))
  14392. ts)
  14393. (when specific (throw 'exit ts))))))
  14394. ;; Both timestamp types are found,
  14395. ;; move to next part.
  14396. (when (= (length ts) 2) (throw 'next ts)))))
  14397. ts)))))
  14398. (goto-char beg)
  14399. ;; First look for timestamps within headline.
  14400. (let ((ts (funcall find-ts (line-end-position) nil)))
  14401. (if (= (length ts) 2) (setq props (nconc ts props))
  14402. (forward-line)
  14403. ;; Then find timestamps in the section, skipping
  14404. ;; planning line.
  14405. (when (org-looking-at-p org-planning-line-re)
  14406. (forward-line))
  14407. (let ((end (save-excursion (outline-next-heading))))
  14408. (setq props (nconc (funcall find-ts end ts) props))))))))
  14409. ;; Get the standard properties, like :PROP:.
  14410. (when (memq which '(nil all standard))
  14411. ;; If we are looking after a specific property, delegate
  14412. ;; to `org-entry-get', which is faster. However, make an
  14413. ;; exception for "CATEGORY", since it can be also set
  14414. ;; through keywords (i.e. #+CATEGORY).
  14415. (if (and specific (not (equal specific "CATEGORY")))
  14416. (let ((value (org-entry-get beg specific nil t)))
  14417. (throw 'exit (and value (list (cons specific value)))))
  14418. (let ((range (org-get-property-block beg)))
  14419. (when range
  14420. (let ((end (cdr range)) seen-base)
  14421. (goto-char (car range))
  14422. ;; Unlike to `org--update-property-plist', we
  14423. ;; handle the case where base values is found
  14424. ;; after its extension. We also forbid standard
  14425. ;; properties to be named as special properties.
  14426. (while (re-search-forward org-property-re end t)
  14427. (let* ((key (upcase (org-match-string-no-properties 2)))
  14428. (extendp (org-string-match-p "\\+\\'" key))
  14429. (key-base (if extendp (substring key 0 -1) key))
  14430. (value (org-match-string-no-properties 3)))
  14431. (cond
  14432. ((member-ignore-case key-base org-special-properties))
  14433. (extendp
  14434. (setq props
  14435. (org--update-property-plist key value props)))
  14436. ((member key seen-base))
  14437. (t (push key seen-base)
  14438. (let ((p (assoc-string key props t)))
  14439. (if p (setcdr p (concat value " " (cdr p)))
  14440. (push (cons key value) props))))))))))))
  14441. (unless (assoc "CATEGORY" props)
  14442. (push (cons "CATEGORY" (org-get-category beg)) props)
  14443. (when (string= specific "CATEGORY") (throw 'exit props)))
  14444. ;; Return value.
  14445. (append (get-text-property beg 'org-summaries) props))))))
  14446. (defun org-entry-get (pom property &optional inherit literal-nil)
  14447. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14448. If INHERIT is non-nil and the entry does not have the property,
  14449. then also check higher levels of the hierarchy. If INHERIT is
  14450. the symbol `selective', use inheritance only if the setting in
  14451. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14452. If the property is present but empty, the return value is the
  14453. empty string. If the property is not present at all, nil is
  14454. returned. In any other case, return the value as a string.
  14455. Search is case-insensitive.
  14456. If LITERAL-NIL is set, return the string value \"nil\" as
  14457. a string, do not interpret it as the list atom nil. This is used
  14458. for inheritance when a \"nil\" value can supersede a non-nil
  14459. value higher up the hierarchy."
  14460. (org-with-point-at pom
  14461. (cond
  14462. ((and inherit
  14463. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14464. (org-entry-get-with-inheritance property literal-nil))
  14465. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14466. ;; We need a special property. Use `org-entry-properties' to
  14467. ;; retrieve it, but specify the wanted property.
  14468. (cdr (assoc-string property (org-entry-properties nil property))))
  14469. (t
  14470. (let ((range (org-get-property-block)))
  14471. (when range
  14472. (let* ((case-fold-search t)
  14473. (end (cdr range))
  14474. (props
  14475. (let ((global
  14476. (or (assoc-string property org-file-properties t)
  14477. (assoc-string property org-global-properties t)
  14478. (assoc-string
  14479. property org-global-properties-fixed t))))
  14480. ;; Make sure to not re-use GLOBAL as
  14481. ;; `org--update-property-plist' would alter it by
  14482. ;; side-effect.
  14483. (and global (list (cons property (cdr global))))))
  14484. (find-value
  14485. (lambda (key)
  14486. (when (re-search-forward (org-re-property key nil t) end t)
  14487. (setq props
  14488. (org--update-property-plist
  14489. key (org-match-string-no-properties 3) props))))))
  14490. (goto-char (car range))
  14491. ;; Find base value.
  14492. (save-excursion (funcall find-value property))
  14493. ;; Find additional values.
  14494. (let ((property+ (concat property "+")))
  14495. (while (funcall find-value property+)))
  14496. ;; Return final value.
  14497. (let ((val (cdr (assoc-string property props t))))
  14498. (if literal-nil val (org-not-nil val))))))))))
  14499. (defun org-property-or-variable-value (var &optional inherit)
  14500. "Check if there is a property fixing the value of VAR.
  14501. If yes, return this value. If not, return the current value of the variable."
  14502. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14503. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14504. (read prop)
  14505. (symbol-value var))))
  14506. (defun org-entry-delete (pom property)
  14507. "Delete PROPERTY from entry at point-or-marker POM.
  14508. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14509. non-nil when a property was removed."
  14510. (unless (member property org-special-properties)
  14511. (org-with-point-at pom
  14512. (let ((range (org-get-property-block)))
  14513. (when range
  14514. (let* ((begin (car range))
  14515. (origin (cdr range))
  14516. (end (copy-marker origin))
  14517. (re (org-re-property
  14518. (concat (regexp-quote property) "\\+?") t t)))
  14519. (goto-char begin)
  14520. (while (re-search-forward re end t)
  14521. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14522. ;; If drawer is empty, remove it altogether.
  14523. (when (= begin end)
  14524. (delete-region (line-beginning-position 0)
  14525. (line-beginning-position 2)))
  14526. ;; Return non-nil if some property was removed.
  14527. (prog1 (/= end origin) (set-marker end nil))))))))
  14528. ;; Multi-values properties are properties that contain multiple values
  14529. ;; These values are assumed to be single words, separated by whitespace.
  14530. (defun org-entry-add-to-multivalued-property (pom property value)
  14531. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14532. (let* ((old (org-entry-get pom property))
  14533. (values (and old (org-split-string old "[ \t]"))))
  14534. (setq value (org-entry-protect-space value))
  14535. (unless (member value values)
  14536. (setq values (append values (list value)))
  14537. (org-entry-put pom property
  14538. (mapconcat 'identity values " ")))))
  14539. (defun org-entry-remove-from-multivalued-property (pom property value)
  14540. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14541. (let* ((old (org-entry-get pom property))
  14542. (values (and old (org-split-string old "[ \t]"))))
  14543. (setq value (org-entry-protect-space value))
  14544. (when (member value values)
  14545. (setq values (delete value values))
  14546. (org-entry-put pom property
  14547. (mapconcat 'identity values " ")))))
  14548. (defun org-entry-member-in-multivalued-property (pom property value)
  14549. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14550. (let* ((old (org-entry-get pom property))
  14551. (values (and old (org-split-string old "[ \t]"))))
  14552. (setq value (org-entry-protect-space value))
  14553. (member value values)))
  14554. (defun org-entry-get-multivalued-property (pom property)
  14555. "Return a list of values in a multivalued property."
  14556. (let* ((value (org-entry-get pom property))
  14557. (values (and value (org-split-string value "[ \t]"))))
  14558. (mapcar 'org-entry-restore-space values)))
  14559. (defun org-entry-put-multivalued-property (pom property &rest values)
  14560. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14561. VALUES should be a list of strings. Spaces will be protected."
  14562. (org-entry-put pom property
  14563. (mapconcat 'org-entry-protect-space values " "))
  14564. (let* ((value (org-entry-get pom property))
  14565. (values (and value (org-split-string value "[ \t]"))))
  14566. (mapcar 'org-entry-restore-space values)))
  14567. (defun org-entry-protect-space (s)
  14568. "Protect spaces and newline in string S."
  14569. (while (string-match " " s)
  14570. (setq s (replace-match "%20" t t s)))
  14571. (while (string-match "\n" s)
  14572. (setq s (replace-match "%0A" t t s)))
  14573. s)
  14574. (defun org-entry-restore-space (s)
  14575. "Restore spaces and newline in string S."
  14576. (while (string-match "%20" s)
  14577. (setq s (replace-match " " t t s)))
  14578. (while (string-match "%0A" s)
  14579. (setq s (replace-match "\n" t t s)))
  14580. s)
  14581. (defvar org-entry-property-inherited-from (make-marker)
  14582. "Marker pointing to the entry from where a property was inherited.
  14583. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14584. location of the entry where the inheritance search matched. If there was
  14585. no match, the marker will point nowhere.
  14586. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14587. is set.")
  14588. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14589. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14590. The search will stop at the first ancestor which has the property defined.
  14591. If the value found is \"nil\", return nil to show that the property
  14592. should be considered as undefined (this is the meaning of nil here).
  14593. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14594. (move-marker org-entry-property-inherited-from nil)
  14595. (let (value)
  14596. (org-with-wide-buffer
  14597. (catch 'exit
  14598. (while t
  14599. (when (setq value (org-entry-get nil property nil literal-nil))
  14600. (org-back-to-heading t)
  14601. (move-marker org-entry-property-inherited-from (point))
  14602. (throw 'exit nil))
  14603. (or (org-up-heading-safe) (throw 'exit nil)))))
  14604. (unless value
  14605. (setq value
  14606. (cdr (or (assoc-string property org-file-properties t)
  14607. (assoc-string property org-global-properties t)
  14608. (assoc-string property org-global-properties-fixed t)))))
  14609. (if literal-nil value (org-not-nil value))))
  14610. (defvar org-property-changed-functions nil
  14611. "Hook called when the value of a property has changed.
  14612. Each hook function should accept two arguments, the name of the property
  14613. and the new value.")
  14614. (defun org-entry-put (pom property value)
  14615. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14616. If the value is `nil', it is converted to the empty string. If
  14617. it is not a string, an error is raised.
  14618. PROPERTY can be any regular property (see
  14619. `org-special-properties'). It can also be \"TODO\",
  14620. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14621. For the last two properties, VALUE may have any of the special
  14622. values \"earlier\" and \"later\". The function then increases or
  14623. decreases scheduled or deadline date by one day."
  14624. (cond ((null value) (setq value ""))
  14625. ((not (stringp value)) (error "Properties values should be strings")))
  14626. (org-with-point-at pom
  14627. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14628. (org-back-to-heading t)
  14629. (org-with-limited-levels (org-back-to-heading t)))
  14630. (let ((beg (point)))
  14631. (cond
  14632. ((equal property "TODO")
  14633. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14634. ((not (member value org-todo-keywords-1))
  14635. (user-error "\"%s\" is not a valid TODO state" value)))
  14636. (org-todo value)
  14637. (org-set-tags nil 'align))
  14638. ((equal property "PRIORITY")
  14639. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14640. (org-set-tags nil 'align))
  14641. ((equal property "SCHEDULED")
  14642. (forward-line)
  14643. (if (and (org-looking-at-p org-planning-line-re)
  14644. (re-search-forward
  14645. org-scheduled-time-regexp (line-end-position) t))
  14646. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14647. ((string= value "later") (org-timestamp-change 1 'day))
  14648. ((string= value "") (org-schedule '(4)))
  14649. (t (org-schedule nil value)))
  14650. (if (member value '("earlier" "later" ""))
  14651. (call-interactively #'org-schedule)
  14652. (org-schedule nil value))))
  14653. ((equal property "DEADLINE")
  14654. (forward-line)
  14655. (if (and (org-looking-at-p org-planning-line-re)
  14656. (re-search-forward
  14657. org-deadline-time-regexp (line-end-position) t))
  14658. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14659. ((string= value "later") (org-timestamp-change 1 'day))
  14660. ((string= value "") (org-deadline '(4)))
  14661. (t (org-deadline nil value)))
  14662. (if (member value '("earlier" "later" ""))
  14663. (call-interactively #'org-deadline)
  14664. (org-deadline nil value))))
  14665. ((member property org-special-properties)
  14666. (error "The %s property cannot be set with `org-entry-put'" property))
  14667. (t
  14668. (let* ((range (org-get-property-block beg 'force))
  14669. (end (cdr range))
  14670. (case-fold-search t))
  14671. (goto-char (car range))
  14672. (if (re-search-forward (org-re-property property nil t) end t)
  14673. (progn (delete-region (match-beginning 0) (match-end 0))
  14674. (goto-char (match-beginning 0)))
  14675. (goto-char end)
  14676. (insert "\n")
  14677. (backward-char))
  14678. (insert ":" property ":")
  14679. (when value (insert " " value))
  14680. (org-indent-line)))))
  14681. (run-hook-with-args 'org-property-changed-functions property value)))
  14682. (defun org-buffer-property-keys (&optional specials defaults columns)
  14683. "Get all property keys in the current buffer.
  14684. When SPECIALS is non-nil, also list the special properties that
  14685. reflect things like tags and TODO state.
  14686. When DEFAULTS is non-nil, also include properties that has
  14687. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14688. DESCRIPTION, LOCATION, and LOGGING and others.
  14689. When COLUMNS in non-nil, also include property names given in
  14690. COLUMN formats in the current buffer."
  14691. (let ((case-fold-search t)
  14692. (props (append
  14693. (and specials org-special-properties)
  14694. (and defaults (cons org-effort-property org-default-properties))
  14695. nil)))
  14696. (org-with-wide-buffer
  14697. (goto-char (point-min))
  14698. (while (re-search-forward org-property-start-re nil t)
  14699. (let ((range (org-get-property-block)))
  14700. (catch 'skip
  14701. (unless range
  14702. (when (and (not (org-before-first-heading-p))
  14703. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14704. (line-beginning-position))))
  14705. (org-get-property-block nil t))
  14706. (throw 'skip nil))
  14707. (goto-char (car range))
  14708. (let ((begin (car range))
  14709. (end (cdr range)))
  14710. ;; Make sure that found property block is not located
  14711. ;; before current point, as it would generate an infloop.
  14712. ;; It can happen, for example, in the following
  14713. ;; situation:
  14714. ;;
  14715. ;; * Headline
  14716. ;; :PROPERTIES:
  14717. ;; ...
  14718. ;; :END:
  14719. ;; *************** Inlinetask
  14720. ;; #+BEGIN_EXAMPLE
  14721. ;; :PROPERTIES:
  14722. ;; #+END_EXAMPLE
  14723. ;;
  14724. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14725. (while (< (point) end)
  14726. (let ((p (progn (looking-at org-property-re)
  14727. (org-match-string-no-properties 2))))
  14728. ;; Only add true property name, not extension symbol.
  14729. (add-to-list 'props
  14730. (if (not (org-string-match-p "\\+\\'" p)) p
  14731. (substring p 0 -1))))
  14732. (forward-line))))
  14733. (outline-next-heading)))
  14734. (when columns
  14735. (goto-char (point-min))
  14736. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14737. (let ((element (org-element-at-point)))
  14738. (when (memq (org-element-type element) '(keyword node-property))
  14739. (let ((value (org-element-property :value element))
  14740. (start 0))
  14741. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14742. (setq start (match-end 0))
  14743. (let ((p (org-match-string-no-properties 1 value)))
  14744. (unless (member-ignore-case p org-special-properties)
  14745. (add-to-list 'props p))))))))))
  14746. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14747. (defun org-property-values (key)
  14748. "List all non-nil values of property KEY in current buffer."
  14749. (org-with-wide-buffer
  14750. (goto-char (point-min))
  14751. (let ((case-fold-search t)
  14752. (re (org-re-property key))
  14753. values)
  14754. (while (re-search-forward re nil t)
  14755. (add-to-list 'values (org-entry-get (point) key)))
  14756. values)))
  14757. (defun org-insert-property-drawer ()
  14758. "Insert a property drawer into the current entry."
  14759. (org-with-wide-buffer
  14760. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14761. (org-back-to-heading t)
  14762. (org-with-limited-levels (org-back-to-heading t)))
  14763. (forward-line)
  14764. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14765. (unless (org-looking-at-p org-property-drawer-re)
  14766. (let ((inhibit-read-only t))
  14767. (unless (bolp) (insert "\n"))
  14768. (let ((begin (point)))
  14769. (insert ":PROPERTIES:\n:END:\n")
  14770. (org-indent-region begin (point)))))))
  14771. (defun org-insert-drawer (&optional arg drawer)
  14772. "Insert a drawer at point.
  14773. When optional argument ARG is non-nil, insert a property drawer.
  14774. Optional argument DRAWER, when non-nil, is a string representing
  14775. drawer's name. Otherwise, the user is prompted for a name.
  14776. If a region is active, insert the drawer around that region
  14777. instead.
  14778. Point is left between drawer's boundaries."
  14779. (interactive "P")
  14780. (let* ((drawer (if arg "PROPERTIES"
  14781. (or drawer (read-from-minibuffer "Drawer: ")))))
  14782. (cond
  14783. ;; With C-u, fall back on `org-insert-property-drawer'
  14784. (arg (org-insert-property-drawer))
  14785. ;; Check validity of suggested drawer's name.
  14786. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14787. (user-error "Invalid drawer name"))
  14788. ;; With an active region, insert a drawer at point.
  14789. ((not (org-region-active-p))
  14790. (progn
  14791. (unless (bolp) (insert "\n"))
  14792. (insert (format ":%s:\n\n:END:\n" drawer))
  14793. (forward-line -2)))
  14794. ;; Otherwise, insert the drawer at point
  14795. (t
  14796. (let ((rbeg (region-beginning))
  14797. (rend (copy-marker (region-end))))
  14798. (unwind-protect
  14799. (progn
  14800. (goto-char rbeg)
  14801. (beginning-of-line)
  14802. (when (save-excursion
  14803. (re-search-forward org-outline-regexp-bol rend t))
  14804. (user-error "Drawers cannot contain headlines"))
  14805. ;; Position point at the beginning of the first
  14806. ;; non-blank line in region. Insert drawer's opening
  14807. ;; there, then indent it.
  14808. (org-skip-whitespace)
  14809. (beginning-of-line)
  14810. (insert ":" drawer ":\n")
  14811. (forward-line -1)
  14812. (indent-for-tab-command)
  14813. ;; Move point to the beginning of the first blank line
  14814. ;; after the last non-blank line in region. Insert
  14815. ;; drawer's closing, then indent it.
  14816. (goto-char rend)
  14817. (skip-chars-backward " \r\t\n")
  14818. (insert "\n:END:")
  14819. (deactivate-mark t)
  14820. (indent-for-tab-command)
  14821. (unless (eolp) (insert "\n")))
  14822. ;; Clear marker, whatever the outcome of insertion is.
  14823. (set-marker rend nil)))))))
  14824. (defvar org-property-set-functions-alist nil
  14825. "Property set function alist.
  14826. Each entry should have the following format:
  14827. (PROPERTY . READ-FUNCTION)
  14828. The read function will be called with the same argument as
  14829. `org-completing-read'.")
  14830. (defun org-set-property-function (property)
  14831. "Get the function that should be used to set PROPERTY.
  14832. This is computed according to `org-property-set-functions-alist'."
  14833. (or (cdr (assoc property org-property-set-functions-alist))
  14834. 'org-completing-read))
  14835. (defun org-read-property-value (property)
  14836. "Read PROPERTY value from user."
  14837. (let* ((completion-ignore-case t)
  14838. (allowed (org-property-get-allowed-values nil property 'table))
  14839. (cur (org-entry-get nil property))
  14840. (prompt (concat property " value"
  14841. (if (and cur (string-match "\\S-" cur))
  14842. (concat " [" cur "]") "") ": "))
  14843. (set-function (org-set-property-function property))
  14844. (val (if allowed
  14845. (funcall set-function prompt allowed nil
  14846. (not (get-text-property 0 'org-unrestricted
  14847. (caar allowed))))
  14848. (let (org-completion-use-ido org-completion-use-iswitchb)
  14849. (funcall set-function prompt
  14850. (mapcar 'list (org-property-values property))
  14851. nil nil "" nil cur)))))
  14852. (org-trim val)))
  14853. (defvar org-last-set-property nil)
  14854. (defvar org-last-set-property-value nil)
  14855. (defun org-read-property-name ()
  14856. "Read a property name."
  14857. (let ((completion-ignore-case t)
  14858. (default-prop (or (and (org-at-property-p)
  14859. (org-match-string-no-properties 2))
  14860. org-last-set-property)))
  14861. (org-completing-read
  14862. (concat "Property"
  14863. (if default-prop (concat " [" default-prop "]") "")
  14864. ": ")
  14865. (mapcar #'list (org-buffer-property-keys nil t t))
  14866. nil nil nil nil default-prop)))
  14867. (defun org-set-property-and-value (use-last)
  14868. "Allow to set [PROPERTY]: [value] direction from prompt.
  14869. When use-default, don't even ask, just use the last
  14870. \"[PROPERTY]: [value]\" string from the history."
  14871. (interactive "P")
  14872. (let* ((completion-ignore-case t)
  14873. (pv (or (and use-last org-last-set-property-value)
  14874. (org-completing-read
  14875. "Enter a \"[Property]: [value]\" pair: "
  14876. nil nil nil nil nil
  14877. org-last-set-property-value)))
  14878. prop val)
  14879. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14880. (setq prop (match-string 1 pv)
  14881. val (match-string 2 pv))
  14882. (org-set-property prop val))))
  14883. (defun org-set-property (property value)
  14884. "In the current entry, set PROPERTY to VALUE.
  14885. When called interactively, this will prompt for a property name, offering
  14886. completion on existing and default properties. And then it will prompt
  14887. for a value, offering completion either on allowed values (via an inherited
  14888. xxx_ALL property) or on existing values in other instances of this property
  14889. in the current file."
  14890. (interactive (list nil nil))
  14891. (let* ((property (or property (org-read-property-name)))
  14892. (value (or value (org-read-property-value property)))
  14893. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14894. (setq org-last-set-property property)
  14895. (setq org-last-set-property-value (concat property ": " value))
  14896. ;; Possibly postprocess the inserted value:
  14897. (when fn (setq value (funcall fn value)))
  14898. (unless (equal (org-entry-get nil property) value)
  14899. (org-entry-put nil property value))))
  14900. (defun org-find-property (property &optional value)
  14901. "Find first entry in buffer that sets PROPERTY.
  14902. When optional argument VALUE is non-nil, only consider an entry
  14903. if it contains PROPERTY set to this value. If PROPERTY should be
  14904. explicitly set to nil, use string \"nil\" for VALUE.
  14905. Return position where the entry begins, or nil if there is no
  14906. such entry. If narrowing is in effect, only search the visible
  14907. part of the buffer."
  14908. (save-excursion
  14909. (goto-char (point-min))
  14910. (let ((case-fold-search t)
  14911. (re (org-re-property property nil (not value) value)))
  14912. (catch 'exit
  14913. (while (re-search-forward re nil t)
  14914. (when (if value (org-at-property-p)
  14915. (org-entry-get (point) property nil t))
  14916. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14917. (defun org-delete-property (property)
  14918. "In the current entry, delete PROPERTY."
  14919. (interactive
  14920. (let* ((completion-ignore-case t)
  14921. (cat (org-entry-get (point) "CATEGORY"))
  14922. (props0 (org-entry-properties nil 'standard))
  14923. (props (if cat props0
  14924. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14925. (prop (if (< 1 (length props))
  14926. (org-icompleting-read "Property: " props nil t)
  14927. (caar props))))
  14928. (list prop)))
  14929. (if (not property)
  14930. (message "No property to delete in this entry")
  14931. (org-entry-delete nil property)
  14932. (message "Property \"%s\" deleted" property)))
  14933. (defun org-delete-property-globally (property)
  14934. "Remove PROPERTY globally, from all entries.
  14935. This function ignores narrowing, if any."
  14936. (interactive
  14937. (let* ((completion-ignore-case t)
  14938. (prop (org-icompleting-read
  14939. "Globally remove property: "
  14940. (mapcar #'list (org-buffer-property-keys)))))
  14941. (list prop)))
  14942. (org-with-wide-buffer
  14943. (goto-char (point-min))
  14944. (let ((count 0)
  14945. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14946. (while (re-search-forward re nil t)
  14947. (when (org-entry-delete (point) property) (incf count)))
  14948. (message "Property \"%s\" removed from %d entries" property count))))
  14949. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14950. (defun org-compute-property-at-point ()
  14951. "Compute the property at point.
  14952. This looks for an enclosing column format, extracts the operator and
  14953. then applies it to the property in the column format's scope."
  14954. (interactive)
  14955. (unless (org-at-property-p)
  14956. (user-error "Not at a property"))
  14957. (let ((prop (org-match-string-no-properties 2)))
  14958. (org-columns-get-format-and-top-level)
  14959. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14960. (user-error "No operator defined for property %s" prop))
  14961. (org-columns-compute prop)))
  14962. (defvar org-property-allowed-value-functions nil
  14963. "Hook for functions supplying allowed values for a specific property.
  14964. The functions must take a single argument, the name of the property, and
  14965. return a flat list of allowed values. If \":ETC\" is one of
  14966. the values, this means that these values are intended as defaults for
  14967. completion, but that other values should be allowed too.
  14968. The functions must return nil if they are not responsible for this
  14969. property.")
  14970. (defun org-property-get-allowed-values (pom property &optional table)
  14971. "Get allowed values for the property PROPERTY.
  14972. When TABLE is non-nil, return an alist that can directly be used for
  14973. completion."
  14974. (let (vals)
  14975. (cond
  14976. ((equal property "TODO")
  14977. (setq vals (org-with-point-at pom
  14978. (append org-todo-keywords-1 '("")))))
  14979. ((equal property "PRIORITY")
  14980. (let ((n org-lowest-priority))
  14981. (while (>= n org-highest-priority)
  14982. (push (char-to-string n) vals)
  14983. (setq n (1- n)))))
  14984. ((equal property "CATEGORY"))
  14985. ((member property org-special-properties))
  14986. ((setq vals (run-hook-with-args-until-success
  14987. 'org-property-allowed-value-functions property)))
  14988. (t
  14989. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14990. (when (and vals (string-match "\\S-" vals))
  14991. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14992. (setq vals (mapcar (lambda (x)
  14993. (cond ((stringp x) x)
  14994. ((numberp x) (number-to-string x))
  14995. ((symbolp x) (symbol-name x))
  14996. (t "???")))
  14997. vals)))))
  14998. (when (member ":ETC" vals)
  14999. (setq vals (remove ":ETC" vals))
  15000. (org-add-props (car vals) '(org-unrestricted t)))
  15001. (if table (mapcar 'list vals) vals)))
  15002. (defun org-property-previous-allowed-value (&optional previous)
  15003. "Switch to the next allowed value for this property."
  15004. (interactive)
  15005. (org-property-next-allowed-value t))
  15006. (defun org-property-next-allowed-value (&optional previous)
  15007. "Switch to the next allowed value for this property."
  15008. (interactive)
  15009. (unless (org-at-property-p)
  15010. (user-error "Not at a property"))
  15011. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15012. (key (match-string 2))
  15013. (value (match-string 3))
  15014. (allowed (or (org-property-get-allowed-values (point) key)
  15015. (and (member value '("[ ]" "[-]" "[X]"))
  15016. '("[ ]" "[X]"))))
  15017. (heading (save-match-data (nth 4 (org-heading-components))))
  15018. nval)
  15019. (unless allowed
  15020. (user-error "Allowed values for this property have not been defined"))
  15021. (if previous (setq allowed (reverse allowed)))
  15022. (if (member value allowed)
  15023. (setq nval (car (cdr (member value allowed)))))
  15024. (setq nval (or nval (car allowed)))
  15025. (if (equal nval value)
  15026. (user-error "Only one allowed value for this property"))
  15027. (org-at-property-p)
  15028. (replace-match (concat " :" key ": " nval) t t)
  15029. (org-indent-line)
  15030. (beginning-of-line 1)
  15031. (skip-chars-forward " \t")
  15032. (when (equal prop org-effort-property)
  15033. (org-refresh-property
  15034. '((effort . identity)
  15035. (effort-minutes . org-duration-string-to-minutes))
  15036. nval)
  15037. (when (string= org-clock-current-task heading)
  15038. (setq org-clock-effort nval)
  15039. (org-clock-update-mode-line)))
  15040. (run-hook-with-args 'org-property-changed-functions key nval)))
  15041. (defun org-find-olp (path &optional this-buffer)
  15042. "Return a marker pointing to the entry at outline path OLP.
  15043. If anything goes wrong, throw an error.
  15044. You can wrap this call to catch the error like this:
  15045. (condition-case msg
  15046. (org-mobile-locate-entry (match-string 4))
  15047. (error (nth 1 msg)))
  15048. The return value will then be either a string with the error message,
  15049. or a marker if everything is OK.
  15050. If THIS-BUFFER is set, the outline path does not contain a file,
  15051. only headings."
  15052. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15053. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15054. (level 1)
  15055. (lmin 1)
  15056. (lmax 1)
  15057. limit re end found pos heading cnt flevel)
  15058. (unless buffer (error "File not found :%s" file))
  15059. (with-current-buffer buffer
  15060. (save-excursion
  15061. (save-restriction
  15062. (widen)
  15063. (setq limit (point-max))
  15064. (goto-char (point-min))
  15065. (while (setq heading (pop path))
  15066. (setq re (format org-complex-heading-regexp-format
  15067. (regexp-quote heading)))
  15068. (setq cnt 0 pos (point))
  15069. (while (re-search-forward re end t)
  15070. (setq level (- (match-end 1) (match-beginning 1)))
  15071. (if (and (>= level lmin) (<= level lmax))
  15072. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15073. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15074. lmax heading))
  15075. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15076. lmax heading))
  15077. (goto-char found)
  15078. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15079. (setq end (save-excursion (org-end-of-subtree t t))))
  15080. (when (org-at-heading-p)
  15081. (point-marker)))))))
  15082. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15083. "Find node HEADING in BUFFER.
  15084. Return a marker to the heading if it was found, or nil if not.
  15085. If POS-ONLY is set, return just the position instead of a marker.
  15086. The heading text must match exact, but it may have a TODO keyword,
  15087. a priority cookie and tags in the standard locations."
  15088. (with-current-buffer (or buffer (current-buffer))
  15089. (save-excursion
  15090. (save-restriction
  15091. (widen)
  15092. (goto-char (point-min))
  15093. (let (case-fold-search)
  15094. (if (re-search-forward
  15095. (format org-complex-heading-regexp-format
  15096. (regexp-quote heading)) nil t)
  15097. (if pos-only
  15098. (match-beginning 0)
  15099. (move-marker (make-marker) (match-beginning 0)))))))))
  15100. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15101. "Find Org node headline HEADING in all .org files in directory DIR.
  15102. When the target headline is found, return a marker to this location."
  15103. (let ((files (directory-files (or dir default-directory)
  15104. t "\\`[^.#].*\\.org\\'"))
  15105. file visiting m buffer)
  15106. (catch 'found
  15107. (while (setq file (pop files))
  15108. (message "trying %s" file)
  15109. (setq visiting (org-find-base-buffer-visiting file))
  15110. (setq buffer (or visiting (find-file-noselect file)))
  15111. (setq m (org-find-exact-headline-in-buffer
  15112. heading buffer))
  15113. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15114. (and m (throw 'found m))))))
  15115. (defun org-find-entry-with-id (ident)
  15116. "Locate the entry that contains the ID property with exact value IDENT.
  15117. IDENT can be a string, a symbol or a number, this function will search for
  15118. the string representation of it.
  15119. Return the position where this entry starts, or nil if there is no such entry."
  15120. (interactive "sID: ")
  15121. (let ((id (cond
  15122. ((stringp ident) ident)
  15123. ((symbolp ident) (symbol-name ident))
  15124. ((numberp ident) (number-to-string ident))
  15125. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15126. (org-with-wide-buffer (org-find-property "ID" id))))
  15127. ;;;; Timestamps
  15128. (defvar org-last-changed-timestamp nil)
  15129. (defvar org-last-inserted-timestamp nil
  15130. "The last time stamp inserted with `org-insert-time-stamp'.")
  15131. (defvar org-ts-what) ; dynamically scoped parameter
  15132. (defun org-time-stamp (arg &optional inactive)
  15133. "Prompt for a date/time and insert a time stamp.
  15134. If the user specifies a time like HH:MM or if this command is
  15135. called with at least one prefix argument, the time stamp contains
  15136. the date and the time. Otherwise, only the date is included.
  15137. All parts of a date not specified by the user are filled in from
  15138. the timestamp at point, if any, or the current date/time
  15139. otherwise.
  15140. If there is already a timestamp at the cursor, it is replaced.
  15141. With two universal prefix arguments, insert an active timestamp
  15142. with the current time without prompting the user.
  15143. When called from lisp, the timestamp is inactive if INACTIVE is
  15144. non-nil."
  15145. (interactive "P")
  15146. (let* ((ts
  15147. (cond ((org-at-date-range-p t)
  15148. (save-excursion
  15149. (goto-char (match-beginning 0))
  15150. (looking-at (if inactive org-ts-regexp-both org-ts-regexp)))
  15151. (match-string 0))
  15152. ((org-at-timestamp-p t) (match-string 0))))
  15153. ;; Default time is either the timestamp at point or today.
  15154. ;; When entering a range, only the range start is considered.
  15155. (default-time (if (not ts) (current-time)
  15156. (apply #'encode-time (org-parse-time-string ts))))
  15157. (default-input (and ts (org-get-compact-tod ts)))
  15158. (repeater (and ts
  15159. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15160. (match-string 0 ts)))
  15161. org-time-was-given
  15162. org-end-time-was-given
  15163. (time
  15164. (and (if (equal arg '(16)) (current-time)
  15165. ;; Preserve `this-command' and `last-command'.
  15166. (let ((this-command this-command)
  15167. (last-command last-command))
  15168. (org-read-date
  15169. arg 'totime nil nil default-time default-input
  15170. inactive))))))
  15171. (cond
  15172. ((and ts
  15173. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15174. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15175. (insert "--")
  15176. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15177. (ts
  15178. ;; Make sure we're on a timestamp. When in the middle of a date
  15179. ;; range, move arbitrarily to range end.
  15180. (unless (org-at-timestamp-p t)
  15181. (skip-chars-forward "-")
  15182. (org-at-timestamp-p t))
  15183. (replace-match "")
  15184. (setq org-last-changed-timestamp
  15185. (org-insert-time-stamp
  15186. time (or org-time-was-given arg)
  15187. inactive nil nil (list org-end-time-was-given)))
  15188. (when repeater
  15189. (backward-char)
  15190. (insert " " repeater)
  15191. (setq org-last-changed-timestamp
  15192. (concat (substring org-last-inserted-timestamp 0 -1)
  15193. " " repeater ">")))
  15194. (message "Timestamp updated"))
  15195. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15196. (t (org-insert-time-stamp
  15197. time (or org-time-was-given arg) inactive nil nil
  15198. (list org-end-time-was-given))))))
  15199. ;; FIXME: can we use this for something else, like computing time differences?
  15200. (defun org-get-compact-tod (s)
  15201. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15202. (let* ((t1 (match-string 1 s))
  15203. (h1 (string-to-number (match-string 2 s)))
  15204. (m1 (string-to-number (match-string 3 s)))
  15205. (t2 (and (match-end 4) (match-string 5 s)))
  15206. (h2 (and t2 (string-to-number (match-string 6 s))))
  15207. (m2 (and t2 (string-to-number (match-string 7 s))))
  15208. dh dm)
  15209. (if (not t2)
  15210. t1
  15211. (setq dh (- h2 h1) dm (- m2 m1))
  15212. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15213. (concat t1 "+" (number-to-string dh)
  15214. (and (/= 0 dm) (format ":%02d" dm)))))))
  15215. (defun org-time-stamp-inactive (&optional arg)
  15216. "Insert an inactive time stamp.
  15217. An inactive time stamp is enclosed in square brackets instead of angle
  15218. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15219. does not link to the calendar and cannot be changed with the S-cursor keys.
  15220. So these are more for recording a certain time/date."
  15221. (interactive "P")
  15222. (org-time-stamp arg 'inactive))
  15223. (defvar org-date-ovl (make-overlay 1 1))
  15224. (overlay-put org-date-ovl 'face 'org-date-selected)
  15225. (org-detach-overlay org-date-ovl)
  15226. (defvar org-ans1) ; dynamically scoped parameter
  15227. (defvar org-ans2) ; dynamically scoped parameter
  15228. (defvar org-plain-time-of-day-regexp) ; defined below
  15229. (defvar org-overriding-default-time nil) ; dynamically scoped
  15230. (defvar org-read-date-overlay nil)
  15231. (defvar org-dcst nil) ; dynamically scoped
  15232. (defvar org-read-date-history nil)
  15233. (defvar org-read-date-final-answer nil)
  15234. (defvar org-read-date-analyze-futurep nil)
  15235. (defvar org-read-date-analyze-forced-year nil)
  15236. (defvar org-read-date-inactive)
  15237. (defvar org-read-date-minibuffer-local-map
  15238. (let* ((map (make-sparse-keymap)))
  15239. (set-keymap-parent map minibuffer-local-map)
  15240. (org-defkey map (kbd ".")
  15241. (lambda () (interactive)
  15242. ;; Are we at the beginning of the prompt?
  15243. (if (looking-back "^[^:]+: ")
  15244. (org-eval-in-calendar '(calendar-goto-today))
  15245. (insert "."))))
  15246. (org-defkey map (kbd "C-.")
  15247. (lambda () (interactive)
  15248. (org-eval-in-calendar '(calendar-goto-today))))
  15249. (org-defkey map [(meta shift left)]
  15250. (lambda () (interactive)
  15251. (org-eval-in-calendar '(calendar-backward-month 1))))
  15252. (org-defkey map [(meta shift right)]
  15253. (lambda () (interactive)
  15254. (org-eval-in-calendar '(calendar-forward-month 1))))
  15255. (org-defkey map [(meta shift up)]
  15256. (lambda () (interactive)
  15257. (org-eval-in-calendar '(calendar-backward-year 1))))
  15258. (org-defkey map [(meta shift down)]
  15259. (lambda () (interactive)
  15260. (org-eval-in-calendar '(calendar-forward-year 1))))
  15261. (org-defkey map [?\e (shift left)]
  15262. (lambda () (interactive)
  15263. (org-eval-in-calendar '(calendar-backward-month 1))))
  15264. (org-defkey map [?\e (shift right)]
  15265. (lambda () (interactive)
  15266. (org-eval-in-calendar '(calendar-forward-month 1))))
  15267. (org-defkey map [?\e (shift up)]
  15268. (lambda () (interactive)
  15269. (org-eval-in-calendar '(calendar-backward-year 1))))
  15270. (org-defkey map [?\e (shift down)]
  15271. (lambda () (interactive)
  15272. (org-eval-in-calendar '(calendar-forward-year 1))))
  15273. (org-defkey map [(shift up)]
  15274. (lambda () (interactive)
  15275. (org-eval-in-calendar '(calendar-backward-week 1))))
  15276. (org-defkey map [(shift down)]
  15277. (lambda () (interactive)
  15278. (org-eval-in-calendar '(calendar-forward-week 1))))
  15279. (org-defkey map [(shift left)]
  15280. (lambda () (interactive)
  15281. (org-eval-in-calendar '(calendar-backward-day 1))))
  15282. (org-defkey map [(shift right)]
  15283. (lambda () (interactive)
  15284. (org-eval-in-calendar '(calendar-forward-day 1))))
  15285. (org-defkey map "!"
  15286. (lambda () (interactive)
  15287. (org-eval-in-calendar '(diary-view-entries))
  15288. (message "")))
  15289. (org-defkey map ">"
  15290. (lambda () (interactive)
  15291. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15292. (org-defkey map "<"
  15293. (lambda () (interactive)
  15294. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15295. (org-defkey map "\C-v"
  15296. (lambda () (interactive)
  15297. (org-eval-in-calendar
  15298. '(calendar-scroll-left-three-months 1))))
  15299. (org-defkey map "\M-v"
  15300. (lambda () (interactive)
  15301. (org-eval-in-calendar
  15302. '(calendar-scroll-right-three-months 1))))
  15303. map)
  15304. "Keymap for minibuffer commands when using `org-read-date'.")
  15305. (defvar org-def)
  15306. (defvar org-defdecode)
  15307. (defvar org-with-time)
  15308. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15309. default-time default-input inactive)
  15310. "Read a date, possibly a time, and make things smooth for the user.
  15311. The prompt will suggest to enter an ISO date, but you can also enter anything
  15312. which will at least partially be understood by `parse-time-string'.
  15313. Unrecognized parts of the date will default to the current day, month, year,
  15314. hour and minute. If this command is called to replace a timestamp at point,
  15315. or to enter the second timestamp of a range, the default time is taken
  15316. from the existing stamp. Furthermore, the command prefers the future,
  15317. so if you are giving a date where the year is not given, and the day-month
  15318. combination is already past in the current year, it will assume you
  15319. mean next year. For details, see the manual. A few examples:
  15320. 3-2-5 --> 2003-02-05
  15321. feb 15 --> currentyear-02-15
  15322. 2/15 --> currentyear-02-15
  15323. sep 12 9 --> 2009-09-12
  15324. 12:45 --> today 12:45
  15325. 22 sept 0:34 --> currentyear-09-22 0:34
  15326. 12 --> currentyear-currentmonth-12
  15327. Fri --> nearest Friday after today
  15328. -Tue --> last Tuesday
  15329. etc.
  15330. Furthermore you can specify a relative date by giving, as the *first* thing
  15331. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15332. change in days weeks, months, years.
  15333. With a single plus or minus, the date is relative to today. With a double
  15334. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15335. +4d --> four days from today
  15336. +4 --> same as above
  15337. +2w --> two weeks from today
  15338. ++5 --> five days from default date
  15339. The function understands only English month and weekday abbreviations.
  15340. While prompting, a calendar is popped up - you can also select the
  15341. date with the mouse (button 1). The calendar shows a period of three
  15342. months. To scroll it to other months, use the keys `>' and `<'.
  15343. If you don't like the calendar, turn it off with
  15344. \(setq org-read-date-popup-calendar nil)
  15345. With optional argument TO-TIME, the date will immediately be converted
  15346. to an internal time.
  15347. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15348. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15349. still enter a time, and this function will inform the calling routine
  15350. about this change. The calling routine may then choose to change the
  15351. format used to insert the time stamp into the buffer to include the time.
  15352. With optional argument FROM-STRING, read from this string instead from
  15353. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15354. the time/date that is used for everything that is not specified by the
  15355. user."
  15356. (require 'parse-time)
  15357. (let* ((org-time-stamp-rounding-minutes
  15358. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15359. (org-dcst org-display-custom-times)
  15360. (ct (org-current-time))
  15361. (org-def (or org-overriding-default-time default-time ct))
  15362. (org-defdecode (decode-time org-def))
  15363. (dummy (progn
  15364. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15365. (setcar (nthcdr 2 org-defdecode) -1)
  15366. (setcar (nthcdr 1 org-defdecode) 59)
  15367. (setq org-def (apply 'encode-time org-defdecode)
  15368. org-defdecode (decode-time org-def)))))
  15369. (cur-frame (selected-frame))
  15370. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15371. (calendar-frame-setup nil)
  15372. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15373. (calendar-move-hook nil)
  15374. (calendar-view-diary-initially-flag nil)
  15375. (calendar-view-holidays-initially-flag nil)
  15376. (timestr (format-time-string
  15377. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15378. (prompt (concat (if prompt (concat prompt " ") "")
  15379. (format "Date+time [%s]: " timestr)))
  15380. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15381. (cond
  15382. (from-string (setq ans from-string))
  15383. (org-read-date-popup-calendar
  15384. (save-excursion
  15385. (save-window-excursion
  15386. (calendar)
  15387. (when (eq calendar-setup 'calendar-only)
  15388. (setq cal-frame
  15389. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15390. (select-frame cal-frame))
  15391. (org-eval-in-calendar '(setq cursor-type nil) t)
  15392. (unwind-protect
  15393. (progn
  15394. (calendar-forward-day (- (time-to-days org-def)
  15395. (calendar-absolute-from-gregorian
  15396. (calendar-current-date))))
  15397. (org-eval-in-calendar nil t)
  15398. (let* ((old-map (current-local-map))
  15399. (map (copy-keymap calendar-mode-map))
  15400. (minibuffer-local-map
  15401. (copy-keymap org-read-date-minibuffer-local-map)))
  15402. (org-defkey map (kbd "RET") 'org-calendar-select)
  15403. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15404. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15405. (unwind-protect
  15406. (progn
  15407. (use-local-map map)
  15408. (setq org-read-date-inactive inactive)
  15409. (add-hook 'post-command-hook 'org-read-date-display)
  15410. (setq org-ans0 (read-string prompt default-input
  15411. 'org-read-date-history nil))
  15412. ;; org-ans0: from prompt
  15413. ;; org-ans1: from mouse click
  15414. ;; org-ans2: from calendar motion
  15415. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15416. (remove-hook 'post-command-hook 'org-read-date-display)
  15417. (use-local-map old-map)
  15418. (when org-read-date-overlay
  15419. (delete-overlay org-read-date-overlay)
  15420. (setq org-read-date-overlay nil)))))
  15421. (bury-buffer "*Calendar*")
  15422. (when cal-frame
  15423. (delete-frame cal-frame)
  15424. (select-frame-set-input-focus cur-frame))))))
  15425. (t ; Naked prompt only
  15426. (unwind-protect
  15427. (setq ans (read-string prompt default-input
  15428. 'org-read-date-history timestr))
  15429. (when org-read-date-overlay
  15430. (delete-overlay org-read-date-overlay)
  15431. (setq org-read-date-overlay nil)))))
  15432. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15433. (when org-read-date-analyze-forced-year
  15434. (message "Year was forced into %s"
  15435. (if org-read-date-force-compatible-dates
  15436. "compatible range (1970-2037)"
  15437. "range representable on this machine"))
  15438. (ding))
  15439. ;; One round trip to get rid of 34th of August and stuff like that....
  15440. (setq final (decode-time (apply 'encode-time final)))
  15441. (setq org-read-date-final-answer ans)
  15442. (if to-time
  15443. (apply 'encode-time final)
  15444. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15445. (format "%04d-%02d-%02d %02d:%02d"
  15446. (nth 5 final) (nth 4 final) (nth 3 final)
  15447. (nth 2 final) (nth 1 final))
  15448. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15449. (defun org-read-date-display ()
  15450. "Display the current date prompt interpretation in the minibuffer."
  15451. (when org-read-date-display-live
  15452. (when org-read-date-overlay
  15453. (delete-overlay org-read-date-overlay))
  15454. (when (minibufferp (current-buffer))
  15455. (save-excursion
  15456. (end-of-line 1)
  15457. (while (not (equal (buffer-substring
  15458. (max (point-min) (- (point) 4)) (point))
  15459. " "))
  15460. (insert " ")))
  15461. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15462. " " (or org-ans1 org-ans2)))
  15463. (org-end-time-was-given nil)
  15464. (f (org-read-date-analyze ans org-def org-defdecode))
  15465. (fmts (if org-dcst
  15466. org-time-stamp-custom-formats
  15467. org-time-stamp-formats))
  15468. (fmt (if (or org-with-time
  15469. (and (boundp 'org-time-was-given) org-time-was-given))
  15470. (cdr fmts)
  15471. (car fmts)))
  15472. (txt (format-time-string fmt (apply 'encode-time f)))
  15473. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15474. (txt (concat "=> " txt)))
  15475. (when (and org-end-time-was-given
  15476. (string-match org-plain-time-of-day-regexp txt))
  15477. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15478. org-end-time-was-given
  15479. (substring txt (match-end 0)))))
  15480. (when org-read-date-analyze-futurep
  15481. (setq txt (concat txt " (=>F)")))
  15482. (setq org-read-date-overlay
  15483. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15484. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15485. (defun org-read-date-analyze (ans org-def org-defdecode)
  15486. "Analyze the combined answer of the date prompt."
  15487. ;; FIXME: cleanup and comment
  15488. (let ((nowdecode (decode-time (current-time)))
  15489. delta deltan deltaw deltadef year month day
  15490. hour minute second wday pm h2 m2 tl wday1
  15491. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15492. (setq org-read-date-analyze-futurep nil
  15493. org-read-date-analyze-forced-year nil)
  15494. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15495. (setq ans "+0"))
  15496. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15497. (setq ans (replace-match "" t t ans)
  15498. deltan (car delta)
  15499. deltaw (nth 1 delta)
  15500. deltadef (nth 2 delta)))
  15501. ;; Check if there is an iso week date in there. If yes, store the
  15502. ;; info and postpone interpreting it until the rest of the parsing
  15503. ;; is done.
  15504. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15505. (setq iso-year (if (match-end 1)
  15506. (org-small-year-to-year
  15507. (string-to-number (match-string 1 ans))))
  15508. iso-weekday (if (match-end 3)
  15509. (string-to-number (match-string 3 ans)))
  15510. iso-week (string-to-number (match-string 2 ans)))
  15511. (setq ans (replace-match "" t t ans)))
  15512. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15513. (when (string-match
  15514. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15515. (setq year (if (match-end 2)
  15516. (string-to-number (match-string 2 ans))
  15517. (progn (setq kill-year t)
  15518. (string-to-number (format-time-string "%Y"))))
  15519. month (string-to-number (match-string 3 ans))
  15520. day (string-to-number (match-string 4 ans)))
  15521. (if (< year 100) (setq year (+ 2000 year)))
  15522. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15523. t nil ans)))
  15524. ;; Help matching dotted european dates
  15525. (when (string-match
  15526. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15527. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15528. (setq kill-year t)
  15529. (string-to-number (format-time-string "%Y")))
  15530. day (string-to-number (match-string 1 ans))
  15531. month (string-to-number (match-string 2 ans))
  15532. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15533. t nil ans)))
  15534. ;; Help matching american dates, like 5/30 or 5/30/7
  15535. (when (string-match
  15536. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15537. (setq year (if (match-end 4)
  15538. (string-to-number (match-string 4 ans))
  15539. (progn (setq kill-year t)
  15540. (string-to-number (format-time-string "%Y"))))
  15541. month (string-to-number (match-string 1 ans))
  15542. day (string-to-number (match-string 2 ans)))
  15543. (if (< year 100) (setq year (+ 2000 year)))
  15544. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15545. t nil ans)))
  15546. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15547. ;; If there is a time with am/pm, and *no* time without it, we convert
  15548. ;; so that matching will be successful.
  15549. (loop for i from 1 to 2 do ; twice, for end time as well
  15550. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15551. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15552. (setq hour (string-to-number (match-string 1 ans))
  15553. minute (if (match-end 3)
  15554. (string-to-number (match-string 3 ans))
  15555. 0)
  15556. pm (equal ?p
  15557. (string-to-char (downcase (match-string 4 ans)))))
  15558. (if (and (= hour 12) (not pm))
  15559. (setq hour 0)
  15560. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15561. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15562. t t ans))))
  15563. ;; Check if a time range is given as a duration
  15564. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15565. (setq hour (string-to-number (match-string 1 ans))
  15566. h2 (+ hour (string-to-number (match-string 3 ans)))
  15567. minute (string-to-number (match-string 2 ans))
  15568. m2 (+ minute (if (match-end 5) (string-to-number
  15569. (match-string 5 ans))0)))
  15570. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15571. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15572. t t ans)))
  15573. ;; Check if there is a time range
  15574. (when (boundp 'org-end-time-was-given)
  15575. (setq org-time-was-given nil)
  15576. (when (and (string-match org-plain-time-of-day-regexp ans)
  15577. (match-end 8))
  15578. (setq org-end-time-was-given (match-string 8 ans))
  15579. (setq ans (concat (substring ans 0 (match-beginning 7))
  15580. (substring ans (match-end 7))))))
  15581. (setq tl (parse-time-string ans)
  15582. day (or (nth 3 tl) (nth 3 org-defdecode))
  15583. month
  15584. (cond ((nth 4 tl))
  15585. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15586. ;; Day was specified. Make sure DAY+MONTH
  15587. ;; combination happens in the future.
  15588. ((nth 3 tl)
  15589. (setq futurep t)
  15590. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15591. (nth 4 nowdecode)))
  15592. (t (nth 4 org-defdecode)))
  15593. year
  15594. (cond ((and (not kill-year) (nth 5 tl)))
  15595. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15596. ;; Month was guessed in the future and is at least
  15597. ;; equal to NOWDECODE's. Fix year accordingly.
  15598. (futurep
  15599. (if (or (> month (nth 4 nowdecode))
  15600. (>= day (nth 3 nowdecode)))
  15601. (nth 5 nowdecode)
  15602. (1+ (nth 5 nowdecode))))
  15603. ;; Month was specified. Make sure MONTH+YEAR
  15604. ;; combination happens in the future.
  15605. ((nth 4 tl)
  15606. (setq futurep t)
  15607. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15608. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15609. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15610. (t (nth 5 nowdecode))))
  15611. (t (nth 5 org-defdecode)))
  15612. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15613. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15614. second (or (nth 0 tl) 0)
  15615. wday (nth 6 tl))
  15616. (when (and (eq org-read-date-prefer-future 'time)
  15617. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15618. (equal day (nth 3 nowdecode))
  15619. (equal month (nth 4 nowdecode))
  15620. (equal year (nth 5 nowdecode))
  15621. (nth 2 tl)
  15622. (or (< (nth 2 tl) (nth 2 nowdecode))
  15623. (and (= (nth 2 tl) (nth 2 nowdecode))
  15624. (nth 1 tl)
  15625. (< (nth 1 tl) (nth 1 nowdecode)))))
  15626. (setq day (1+ day)
  15627. futurep t))
  15628. ;; Special date definitions below
  15629. (cond
  15630. (iso-week
  15631. ;; There was an iso week
  15632. (require 'cal-iso)
  15633. (setq futurep nil)
  15634. (setq year (or iso-year year)
  15635. day (or iso-weekday wday 1)
  15636. wday nil ; to make sure that the trigger below does not match
  15637. iso-date (calendar-gregorian-from-absolute
  15638. (calendar-iso-to-absolute
  15639. (list iso-week day year))))
  15640. ; FIXME: Should we also push ISO weeks into the future?
  15641. ; (when (and org-read-date-prefer-future
  15642. ; (not iso-year)
  15643. ; (< (calendar-absolute-from-gregorian iso-date)
  15644. ; (time-to-days (current-time))))
  15645. ; (setq year (1+ year)
  15646. ; iso-date (calendar-gregorian-from-absolute
  15647. ; (calendar-iso-to-absolute
  15648. ; (list iso-week day year)))))
  15649. (setq month (car iso-date)
  15650. year (nth 2 iso-date)
  15651. day (nth 1 iso-date)))
  15652. (deltan
  15653. (setq futurep nil)
  15654. (unless deltadef
  15655. (let ((now (decode-time (current-time))))
  15656. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15657. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15658. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15659. ((equal deltaw "m") (setq month (+ month deltan)))
  15660. ((equal deltaw "y") (setq year (+ year deltan)))))
  15661. ((and wday (not (nth 3 tl)))
  15662. ;; Weekday was given, but no day, so pick that day in the week
  15663. ;; on or after the derived date.
  15664. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15665. (unless (equal wday wday1)
  15666. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15667. (if (and (boundp 'org-time-was-given)
  15668. (nth 2 tl))
  15669. (setq org-time-was-given t))
  15670. (if (< year 100) (setq year (+ 2000 year)))
  15671. ;; Check of the date is representable
  15672. (if org-read-date-force-compatible-dates
  15673. (progn
  15674. (if (< year 1970)
  15675. (setq year 1970 org-read-date-analyze-forced-year t))
  15676. (if (> year 2037)
  15677. (setq year 2037 org-read-date-analyze-forced-year t)))
  15678. (condition-case nil
  15679. (ignore (encode-time second minute hour day month year))
  15680. (error
  15681. (setq year (nth 5 org-defdecode))
  15682. (setq org-read-date-analyze-forced-year t))))
  15683. (setq org-read-date-analyze-futurep futurep)
  15684. (list second minute hour day month year)))
  15685. (defvar parse-time-weekdays)
  15686. (defun org-read-date-get-relative (s today default)
  15687. "Check string S for special relative date string.
  15688. TODAY and DEFAULT are internal times, for today and for a default.
  15689. Return shift list (N what def-flag)
  15690. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15691. N is the number of WHATs to shift.
  15692. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15693. the DEFAULT date rather than TODAY."
  15694. (require 'parse-time)
  15695. (when (and
  15696. (string-match
  15697. (concat
  15698. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15699. "\\([0-9]+\\)?"
  15700. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15701. "\\([ \t]\\|$\\)") s)
  15702. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15703. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15704. (string-to-char (substring (match-string 1 s) -1))
  15705. ?+))
  15706. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15707. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15708. (what (if (match-end 3) (match-string 3 s) "d"))
  15709. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15710. (date (if rel default today))
  15711. (wday (nth 6 (decode-time date)))
  15712. delta)
  15713. (if wday1
  15714. (progn
  15715. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15716. (if (= delta 0) (setq delta 7))
  15717. (if (= dir ?-)
  15718. (progn
  15719. (setq delta (- delta 7))
  15720. (if (= delta 0) (setq delta -7))))
  15721. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15722. (list delta "d" rel))
  15723. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15724. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15725. "Turn a user-specified date into the internal representation.
  15726. The internal representation needed by the calendar is (month day year).
  15727. This is a wrapper to handle the brain-dead convention in calendar that
  15728. user function argument order change dependent on argument order."
  15729. (if (boundp 'calendar-date-style)
  15730. (cond
  15731. ((eq calendar-date-style 'american)
  15732. (list arg1 arg2 arg3))
  15733. ((eq calendar-date-style 'european)
  15734. (list arg2 arg1 arg3))
  15735. ((eq calendar-date-style 'iso)
  15736. (list arg2 arg3 arg1)))
  15737. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15738. (if (org-bound-and-true-p european-calendar-style)
  15739. (list arg2 arg1 arg3)
  15740. (list arg1 arg2 arg3)))))
  15741. (defun org-eval-in-calendar (form &optional keepdate)
  15742. "Eval FORM in the calendar window and return to current window.
  15743. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15744. otherwise stick to the current value of `org-ans2'."
  15745. (let ((sf (selected-frame))
  15746. (sw (selected-window)))
  15747. (select-window (get-buffer-window "*Calendar*" t))
  15748. (eval form)
  15749. (when (and (not keepdate) (calendar-cursor-to-date))
  15750. (let* ((date (calendar-cursor-to-date))
  15751. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15752. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15753. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15754. (select-window sw)
  15755. (org-select-frame-set-input-focus sf)))
  15756. (defun org-calendar-select ()
  15757. "Return to `org-read-date' with the date currently selected.
  15758. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15759. (interactive)
  15760. (when (calendar-cursor-to-date)
  15761. (let* ((date (calendar-cursor-to-date))
  15762. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15763. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15764. (if (active-minibuffer-window) (exit-minibuffer))))
  15765. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15766. "Insert a date stamp for the date given by the internal TIME.
  15767. See `format-time-string' for the format of TIME.
  15768. WITH-HM means use the stamp format that includes the time of the day.
  15769. INACTIVE means use square brackets instead of angular ones, so that the
  15770. stamp will not contribute to the agenda.
  15771. PRE and POST are optional strings to be inserted before and after the
  15772. stamp.
  15773. The command returns the inserted time stamp."
  15774. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15775. stamp)
  15776. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15777. (insert-before-markers (or pre ""))
  15778. (when (listp extra)
  15779. (setq extra (car extra))
  15780. (if (and (stringp extra)
  15781. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15782. (setq extra (format "-%02d:%02d"
  15783. (string-to-number (match-string 1 extra))
  15784. (string-to-number (match-string 2 extra))))
  15785. (setq extra nil)))
  15786. (when extra
  15787. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15788. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15789. (insert-before-markers (or post ""))
  15790. (setq org-last-inserted-timestamp stamp)))
  15791. (defun org-toggle-time-stamp-overlays ()
  15792. "Toggle the use of custom time stamp formats."
  15793. (interactive)
  15794. (setq org-display-custom-times (not org-display-custom-times))
  15795. (unless org-display-custom-times
  15796. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15797. (while (setq p (next-single-property-change p 'display))
  15798. (if (and (get-text-property p 'display)
  15799. (eq (get-text-property p 'face) 'org-date))
  15800. (remove-text-properties
  15801. p (setq p (next-single-property-change p 'display))
  15802. '(display t))))
  15803. (set-buffer-modified-p bmp)))
  15804. (if (featurep 'xemacs)
  15805. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15806. (org-restart-font-lock)
  15807. (setq org-table-may-need-update t)
  15808. (if org-display-custom-times
  15809. (message "Time stamps are overlaid with custom format")
  15810. (message "Time stamp overlays removed")))
  15811. (defun org-display-custom-time (beg end)
  15812. "Overlay modified time stamp format over timestamp between BEG and END."
  15813. (let* ((ts (buffer-substring beg end))
  15814. t1 w1 with-hm tf time str w2 (off 0))
  15815. (save-match-data
  15816. (setq t1 (org-parse-time-string ts t))
  15817. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15818. (setq off (- (match-end 0) (match-beginning 0)))))
  15819. (setq end (- end off))
  15820. (setq w1 (- end beg)
  15821. with-hm (and (nth 1 t1) (nth 2 t1))
  15822. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15823. time (org-fix-decoded-time t1)
  15824. str (org-add-props
  15825. (format-time-string
  15826. (substring tf 1 -1) (apply 'encode-time time))
  15827. nil 'mouse-face 'highlight)
  15828. w2 (length str))
  15829. (if (not (= w2 w1))
  15830. (add-text-properties (1+ beg) (+ 2 beg)
  15831. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15832. (if (featurep 'xemacs)
  15833. (progn
  15834. (put-text-property beg end 'invisible t)
  15835. (put-text-property beg end 'end-glyph (make-glyph str)))
  15836. (put-text-property beg end 'display str))))
  15837. (defun org-fix-decoded-time (time)
  15838. "Set 0 instead of nil for the first 6 elements of time.
  15839. Don't touch the rest."
  15840. (let ((n 0))
  15841. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15842. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15843. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15844. "Difference between TIMESTAMP-STRING and now in days.
  15845. If SECONDS is non-nil, return the difference in seconds."
  15846. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15847. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15848. (funcall fdiff (current-time)))))
  15849. (defun org-deadline-close (timestamp-string &optional ndays)
  15850. "Is the time in TIMESTAMP-STRING close to the current date?"
  15851. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15852. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15853. (not (org-entry-is-done-p))))
  15854. (defun org-get-wdays (ts &optional delay zero-delay)
  15855. "Get the deadline lead time appropriate for timestring TS.
  15856. When DELAY is non-nil, get the delay time for scheduled items
  15857. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15858. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15859. don't try to find the delay cookie in the scheduled timestamp."
  15860. (let ((tv (if delay org-scheduled-delay-days
  15861. org-deadline-warning-days)))
  15862. (cond
  15863. ((or (and delay (< tv 0))
  15864. (and delay zero-delay (<= tv 0))
  15865. (and (not delay) (<= tv 0)))
  15866. ;; Enforce this value no matter what
  15867. (- tv))
  15868. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15869. ;; lead time is specified.
  15870. (floor (* (string-to-number (match-string 1 ts))
  15871. (cdr (assoc (match-string 2 ts)
  15872. '(("d" . 1) ("w" . 7)
  15873. ("m" . 30.4) ("y" . 365.25)
  15874. ("h" . 0.041667)))))))
  15875. ;; go for the default.
  15876. (t tv))))
  15877. (defun org-calendar-select-mouse (ev)
  15878. "Return to `org-read-date' with the date currently selected.
  15879. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15880. (interactive "e")
  15881. (mouse-set-point ev)
  15882. (when (calendar-cursor-to-date)
  15883. (let* ((date (calendar-cursor-to-date))
  15884. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15885. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15886. (if (active-minibuffer-window) (exit-minibuffer))))
  15887. (defun org-check-deadlines (ndays)
  15888. "Check if there are any deadlines due or past due.
  15889. A deadline is considered due if it happens within `org-deadline-warning-days'
  15890. days from today's date. If the deadline appears in an entry marked DONE,
  15891. it is not shown. The prefix arg NDAYS can be used to test that many
  15892. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15893. (interactive "P")
  15894. (let* ((org-warn-days
  15895. (cond
  15896. ((equal ndays '(4)) 100000)
  15897. (ndays (prefix-numeric-value ndays))
  15898. (t (abs org-deadline-warning-days))))
  15899. (case-fold-search nil)
  15900. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15901. (callback
  15902. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15903. (message "%d deadlines past-due or due within %d days"
  15904. (org-occur regexp nil callback)
  15905. org-warn-days)))
  15906. (defsubst org-re-timestamp (type)
  15907. "Return a regexp for timestamp TYPE.
  15908. Allowed values for TYPE are:
  15909. all: all timestamps
  15910. active: only active timestamps (<...>)
  15911. inactive: only inactive timestamps ([...])
  15912. scheduled: only scheduled timestamps
  15913. deadline: only deadline timestamps
  15914. closed: only closed time-stamps
  15915. When TYPE is nil, fall back on returning a regexp that matches
  15916. both scheduled and deadline timestamps."
  15917. (case type
  15918. (all org-ts-regexp-both)
  15919. (active org-ts-regexp)
  15920. (inactive org-ts-regexp-inactive)
  15921. (scheduled org-scheduled-time-regexp)
  15922. (deadline org-deadline-time-regexp)
  15923. (closed org-closed-time-regexp)
  15924. (otherwise
  15925. (concat "\\<"
  15926. (regexp-opt (list org-deadline-string org-scheduled-string))
  15927. " *<\\([^>]+\\)>"))))
  15928. (defun org-check-before-date (date)
  15929. "Check if there are deadlines or scheduled entries before DATE."
  15930. (interactive (list (org-read-date)))
  15931. (let ((case-fold-search nil)
  15932. (regexp (org-re-timestamp org-ts-type))
  15933. (callback
  15934. `(lambda ()
  15935. (and ,(if (memq org-ts-type '(active inactive all))
  15936. '(eq (org-element-type (org-element-context) 'timestamp))
  15937. '(org-at-planning-p))
  15938. (time-less-p
  15939. (org-time-string-to-time (match-string 1))
  15940. (org-time-string-to-time date))))))
  15941. (message "%d entries before %s"
  15942. (org-occur regexp nil callback) date)))
  15943. (defun org-check-after-date (date)
  15944. "Check if there are deadlines or scheduled entries after DATE."
  15945. (interactive (list (org-read-date)))
  15946. (let ((case-fold-search nil)
  15947. (regexp (org-re-timestamp org-ts-type))
  15948. (callback
  15949. `(lambda ()
  15950. (and ,(if (memq org-ts-type '(active inactive all))
  15951. '(eq (org-element-type (org-element-context) 'timestamp))
  15952. '(org-at-planning-p))
  15953. (not (time-less-p
  15954. (org-time-string-to-time (match-string 1))
  15955. (org-time-string-to-time date)))))))
  15956. (message "%d entries after %s"
  15957. (org-occur regexp nil callback) date)))
  15958. (defun org-check-dates-range (start-date end-date)
  15959. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15960. (interactive (list (org-read-date nil nil nil "Range starts")
  15961. (org-read-date nil nil nil "Range end")))
  15962. (let ((case-fold-search nil)
  15963. (regexp (org-re-timestamp org-ts-type))
  15964. (callback
  15965. `(lambda ()
  15966. (let ((match (match-string 1)))
  15967. (and
  15968. ,(if (memq org-ts-type '(active inactive all))
  15969. '(eq (org-element-type (org-element-context) 'timestamp))
  15970. '(org-at-planning-p))
  15971. (not (time-less-p
  15972. (org-time-string-to-time match)
  15973. (org-time-string-to-time start-date)))
  15974. (time-less-p
  15975. (org-time-string-to-time match)
  15976. (org-time-string-to-time end-date)))))))
  15977. (message "%d entries between %s and %s"
  15978. (org-occur regexp nil callback) start-date end-date)))
  15979. (defun org-evaluate-time-range (&optional to-buffer)
  15980. "Evaluate a time range by computing the difference between start and end.
  15981. Normally the result is just printed in the echo area, but with prefix arg
  15982. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15983. If the time range is actually in a table, the result is inserted into the
  15984. next column.
  15985. For time difference computation, a year is assumed to be exactly 365
  15986. days in order to avoid rounding problems."
  15987. (interactive "P")
  15988. (or
  15989. (org-clock-update-time-maybe)
  15990. (save-excursion
  15991. (unless (org-at-date-range-p t)
  15992. (goto-char (point-at-bol))
  15993. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15994. (if (not (org-at-date-range-p t))
  15995. (user-error "Not at a time-stamp range, and none found in current line")))
  15996. (let* ((ts1 (match-string 1))
  15997. (ts2 (match-string 2))
  15998. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15999. (match-end (match-end 0))
  16000. (time1 (org-time-string-to-time ts1))
  16001. (time2 (org-time-string-to-time ts2))
  16002. (t1 (org-float-time time1))
  16003. (t2 (org-float-time time2))
  16004. (diff (abs (- t2 t1)))
  16005. (negative (< (- t2 t1) 0))
  16006. ;; (ys (floor (* 365 24 60 60)))
  16007. (ds (* 24 60 60))
  16008. (hs (* 60 60))
  16009. (fy "%dy %dd %02d:%02d")
  16010. (fy1 "%dy %dd")
  16011. (fd "%dd %02d:%02d")
  16012. (fd1 "%dd")
  16013. (fh "%02d:%02d")
  16014. y d h m align)
  16015. (if havetime
  16016. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16017. y 0
  16018. d (floor (/ diff ds)) diff (mod diff ds)
  16019. h (floor (/ diff hs)) diff (mod diff hs)
  16020. m (floor (/ diff 60)))
  16021. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16022. y 0
  16023. d (floor (+ (/ diff ds) 0.5))
  16024. h 0 m 0))
  16025. (if (not to-buffer)
  16026. (message "%s" (org-make-tdiff-string y d h m))
  16027. (if (org-at-table-p)
  16028. (progn
  16029. (goto-char match-end)
  16030. (setq align t)
  16031. (and (looking-at " *|") (goto-char (match-end 0))))
  16032. (goto-char match-end))
  16033. (if (looking-at
  16034. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16035. (replace-match ""))
  16036. (if negative (insert " -"))
  16037. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16038. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16039. (insert " " (format fh h m))))
  16040. (if align (org-table-align))
  16041. (message "Time difference inserted")))))
  16042. (defun org-make-tdiff-string (y d h m)
  16043. (let ((fmt "")
  16044. (l nil))
  16045. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16046. l (push y l)))
  16047. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16048. l (push d l)))
  16049. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16050. l (push h l)))
  16051. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16052. l (push m l)))
  16053. (apply 'format fmt (nreverse l))))
  16054. (defun org-time-string-to-time (s &optional buffer pos)
  16055. "Convert a timestamp string into internal time."
  16056. (condition-case errdata
  16057. (apply 'encode-time (org-parse-time-string s))
  16058. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16059. s (if (not (and buffer pos))
  16060. ""
  16061. (format " at %d in buffer `%s'" pos buffer))
  16062. (cdr errdata)))))
  16063. (defun org-time-string-to-seconds (s)
  16064. "Convert a timestamp string to a number of seconds."
  16065. (org-float-time (org-time-string-to-time s)))
  16066. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16067. "Convert a time stamp to an absolute day number.
  16068. If there is a specifier for a cyclic time stamp, get the closest
  16069. date to DAYNR.
  16070. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16071. The variable `date' is bound by the calendar when this is called."
  16072. (cond
  16073. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16074. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16075. daynr
  16076. (+ daynr 1000)))
  16077. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  16078. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16079. (time-to-days (current-time))) (match-string 0 s)
  16080. prefer show-all))
  16081. (t (time-to-days
  16082. (condition-case errdata
  16083. (apply 'encode-time (org-parse-time-string s))
  16084. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16085. s (if (not (and buffer pos))
  16086. ""
  16087. (format " at %d in buffer `%s'" pos buffer))
  16088. (cdr errdata))))))))
  16089. (defun org-days-to-iso-week (days)
  16090. "Return the iso week number."
  16091. (require 'cal-iso)
  16092. (car (calendar-iso-from-absolute days)))
  16093. (defun org-small-year-to-year (year)
  16094. "Convert 2-digit years into 4-digit years.
  16095. YEAR is expanded into one of the 30 next years, if possible, or
  16096. into a past one. Any year larger than 99 is returned unchanged."
  16097. (if (>= year 100) year
  16098. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16099. (century (/ current 100))
  16100. (offset (- year (% current 100))))
  16101. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16102. ((> offset -70) (+ (* century 100) year))
  16103. (t (+ (* (1+ century) 100) year))))))
  16104. (defun org-time-from-absolute (d)
  16105. "Return the time corresponding to date D.
  16106. D may be an absolute day number, or a calendar-type list (month day year)."
  16107. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16108. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16109. (defun org-calendar-holiday ()
  16110. "List of holidays, for Diary display in Org-mode."
  16111. (require 'holidays)
  16112. (let ((hl (funcall
  16113. (if (fboundp 'calendar-check-holidays)
  16114. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16115. (if hl (mapconcat 'identity hl "; "))))
  16116. (defun org-diary-sexp-entry (sexp entry date)
  16117. "Process a SEXP diary ENTRY for DATE."
  16118. (require 'diary-lib)
  16119. (let ((result (if calendar-debug-sexp
  16120. (let ((stack-trace-on-error t))
  16121. (eval (car (read-from-string sexp))))
  16122. (condition-case nil
  16123. (eval (car (read-from-string sexp)))
  16124. (error
  16125. (beep)
  16126. (message "Bad sexp at line %d in %s: %s"
  16127. (org-current-line)
  16128. (buffer-file-name) sexp)
  16129. (sleep-for 2))))))
  16130. (cond ((stringp result) (split-string result "; "))
  16131. ((and (consp result)
  16132. (not (consp (cdr result)))
  16133. (stringp (cdr result))) (cdr result))
  16134. ((and (consp result)
  16135. (stringp (car result))) result)
  16136. (result entry))))
  16137. (defun org-diary-to-ical-string (frombuf)
  16138. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16139. This uses the icalendar.el library."
  16140. (let* ((tmpdir (if (featurep 'xemacs)
  16141. (temp-directory)
  16142. temporary-file-directory))
  16143. (tmpfile (make-temp-name
  16144. (expand-file-name "orgics" tmpdir)))
  16145. buf rtn b e)
  16146. (with-current-buffer frombuf
  16147. (icalendar-export-region (point-min) (point-max) tmpfile)
  16148. (setq buf (find-buffer-visiting tmpfile))
  16149. (set-buffer buf)
  16150. (goto-char (point-min))
  16151. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16152. (setq b (match-beginning 0)))
  16153. (goto-char (point-max))
  16154. (if (re-search-backward "^END:VEVENT" nil t)
  16155. (setq e (match-end 0)))
  16156. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16157. (kill-buffer buf)
  16158. (delete-file tmpfile)
  16159. rtn))
  16160. (defun org-closest-date (start current change prefer show-all)
  16161. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16162. When PREFER is `past', return a date that is either CURRENT or past.
  16163. When PREFER is `future', return a date that is either CURRENT or future.
  16164. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16165. ;; Make the proper lists from the dates
  16166. (catch 'exit
  16167. (let ((a1 '(("h" . hour)
  16168. ("d" . day)
  16169. ("w" . week)
  16170. ("m" . month)
  16171. ("y" . year)))
  16172. (shour (nth 2 (org-parse-time-string start)))
  16173. dn dw sday cday n1 n2 n0
  16174. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16175. (setq start (org-date-to-gregorian start)
  16176. current (org-date-to-gregorian
  16177. (if show-all
  16178. current
  16179. (time-to-days (current-time))))
  16180. sday (calendar-absolute-from-gregorian start)
  16181. cday (calendar-absolute-from-gregorian current))
  16182. (if (<= cday sday) (throw 'exit sday))
  16183. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16184. (setq dn (string-to-number (match-string 1 change))
  16185. dw (cdr (assoc (match-string 2 change) a1)))
  16186. (user-error "Invalid change specifier: %s" change))
  16187. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16188. (cond
  16189. ((eq dw 'hour)
  16190. (let ((missing-hours
  16191. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16192. dn)))
  16193. (setq n1 (if (zerop missing-hours) cday
  16194. (- cday (1+ (floor (/ missing-hours 24)))))
  16195. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16196. ((eq dw 'day)
  16197. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16198. n2 (+ n1 dn)))
  16199. ((eq dw 'year)
  16200. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16201. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16202. (setq date1 (list m d y1)
  16203. n1 (calendar-absolute-from-gregorian date1)
  16204. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16205. n2 (calendar-absolute-from-gregorian date2)))
  16206. ((eq dw 'month)
  16207. ;; approx number of month between the two dates
  16208. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16209. ;; How often does dn fit in there?
  16210. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16211. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16212. m (+ m nm)
  16213. ny (floor (/ m 12))
  16214. y (+ y ny)
  16215. m (- m (* ny 12)))
  16216. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16217. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16218. (setq m2 (+ m dn) y2 y)
  16219. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16220. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16221. (while (<= n2 cday)
  16222. (setq n1 n2 m m2 y y2)
  16223. (setq m2 (+ m dn) y2 y)
  16224. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16225. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16226. ;; Make sure n1 is the earlier date
  16227. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16228. (if show-all
  16229. (cond
  16230. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16231. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16232. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16233. (cond
  16234. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16235. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16236. (t (if (= cday n1) n1 n2)))))))
  16237. (defun org-date-to-gregorian (date)
  16238. "Turn any specification of DATE into a Gregorian date for the calendar."
  16239. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16240. ((and (listp date) (= (length date) 3)) date)
  16241. ((stringp date)
  16242. (setq date (org-parse-time-string date))
  16243. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16244. ((listp date)
  16245. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16246. (defun org-parse-time-string (s &optional nodefault)
  16247. "Parse the standard Org-mode time string.
  16248. This should be a lot faster than the normal `parse-time-string'.
  16249. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16250. hour and minute fields will be nil if not given."
  16251. (cond ((string-match org-ts-regexp0 s)
  16252. (list 0
  16253. (if (or (match-beginning 8) (not nodefault))
  16254. (string-to-number (or (match-string 8 s) "0")))
  16255. (if (or (match-beginning 7) (not nodefault))
  16256. (string-to-number (or (match-string 7 s) "0")))
  16257. (string-to-number (match-string 4 s))
  16258. (string-to-number (match-string 3 s))
  16259. (string-to-number (match-string 2 s))
  16260. nil nil nil))
  16261. ((string-match "^<[^>]+>$" s)
  16262. (decode-time (seconds-to-time (org-matcher-time s))))
  16263. (t (error "Not a standard Org-mode time string: %s" s))))
  16264. (defun org-timestamp-up (&optional arg)
  16265. "Increase the date item at the cursor by one.
  16266. If the cursor is on the year, change the year. If it is on the month,
  16267. the day or the time, change that.
  16268. With prefix ARG, change by that many units."
  16269. (interactive "p")
  16270. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16271. (defun org-timestamp-down (&optional arg)
  16272. "Decrease the date item at the cursor by one.
  16273. If the cursor is on the year, change the year. If it is on the month,
  16274. the day or the time, change that.
  16275. With prefix ARG, change by that many units."
  16276. (interactive "p")
  16277. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16278. (defun org-timestamp-up-day (&optional arg)
  16279. "Increase the date in the time stamp by one day.
  16280. With prefix ARG, change that many days."
  16281. (interactive "p")
  16282. (if (and (not (org-at-timestamp-p t))
  16283. (org-at-heading-p))
  16284. (org-todo 'up)
  16285. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16286. (defun org-timestamp-down-day (&optional arg)
  16287. "Decrease the date in the time stamp by one day.
  16288. With prefix ARG, change that many days."
  16289. (interactive "p")
  16290. (if (and (not (org-at-timestamp-p t))
  16291. (org-at-heading-p))
  16292. (org-todo 'down)
  16293. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16294. (defun org-at-timestamp-p (&optional inactive-ok)
  16295. "Determine if the cursor is in or at a timestamp."
  16296. (interactive)
  16297. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16298. (pos (point))
  16299. (ans (or (looking-at tsr)
  16300. (save-excursion
  16301. (skip-chars-backward "^[<\n\r\t")
  16302. (if (> (point) (point-min)) (backward-char 1))
  16303. (and (looking-at tsr)
  16304. (> (- (match-end 0) pos) -1))))))
  16305. (and ans
  16306. (boundp 'org-ts-what)
  16307. (setq org-ts-what
  16308. (cond
  16309. ((= pos (match-beginning 0)) 'bracket)
  16310. ;; Point is considered to be "on the bracket" whether
  16311. ;; it's really on it or right after it.
  16312. ((= pos (1- (match-end 0))) 'bracket)
  16313. ((= pos (match-end 0)) 'after)
  16314. ((org-pos-in-match-range pos 2) 'year)
  16315. ((org-pos-in-match-range pos 3) 'month)
  16316. ((org-pos-in-match-range pos 7) 'hour)
  16317. ((org-pos-in-match-range pos 8) 'minute)
  16318. ((or (org-pos-in-match-range pos 4)
  16319. (org-pos-in-match-range pos 5)) 'day)
  16320. ((and (> pos (or (match-end 8) (match-end 5)))
  16321. (< pos (match-end 0)))
  16322. (- pos (or (match-end 8) (match-end 5))))
  16323. (t 'day))))
  16324. ans))
  16325. (defun org-toggle-timestamp-type ()
  16326. "Toggle the type (<active> or [inactive]) of a time stamp."
  16327. (interactive)
  16328. (when (org-at-timestamp-p t)
  16329. (let ((beg (match-beginning 0)) (end (match-end 0))
  16330. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16331. (save-excursion
  16332. (goto-char beg)
  16333. (while (re-search-forward "[][<>]" end t)
  16334. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16335. t t)))
  16336. (message "Timestamp is now %sactive"
  16337. (if (equal (char-after beg) ?<) "" "in")))))
  16338. (defun org-at-clock-log-p nil
  16339. "Is the cursor on the clock log line?"
  16340. (save-excursion
  16341. (move-beginning-of-line 1)
  16342. (looking-at org-clock-line-re)))
  16343. (defvar org-clock-history) ; defined in org-clock.el
  16344. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16345. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16346. "Change the date in the time stamp at point.
  16347. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16348. `year', `minute', `second'. If WHAT is not given, the cursor position
  16349. in the timestamp determines what will be changed.
  16350. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16351. (let ((origin (point)) origin-cat
  16352. with-hm inactive
  16353. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16354. org-ts-what
  16355. extra rem
  16356. ts time time0 fixnext clrgx)
  16357. (if (not (org-at-timestamp-p t))
  16358. (user-error "Not at a timestamp"))
  16359. (if (and (not what) (eq org-ts-what 'bracket))
  16360. (org-toggle-timestamp-type)
  16361. ;; Point isn't on brackets. Remember the part of the time-stamp
  16362. ;; the point was in. Indeed, size of time-stamps may change,
  16363. ;; but point must be kept in the same category nonetheless.
  16364. (setq origin-cat org-ts-what)
  16365. (if (and (not what) (not (eq org-ts-what 'day))
  16366. org-display-custom-times
  16367. (get-text-property (point) 'display)
  16368. (not (get-text-property (1- (point)) 'display)))
  16369. (setq org-ts-what 'day))
  16370. (setq org-ts-what (or what org-ts-what)
  16371. inactive (= (char-after (match-beginning 0)) ?\[)
  16372. ts (match-string 0))
  16373. (replace-match "")
  16374. (when (string-match
  16375. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16376. ts)
  16377. (setq extra (match-string 1 ts))
  16378. (if suppress-tmp-delay
  16379. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16380. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16381. (setq with-hm t))
  16382. (setq time0 (org-parse-time-string ts))
  16383. (when (and updown
  16384. (eq org-ts-what 'minute)
  16385. (not current-prefix-arg))
  16386. ;; This looks like s-up and s-down. Change by one rounding step.
  16387. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16388. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16389. (setcar (cdr time0) (+ (nth 1 time0)
  16390. (if (> n 0) (- rem) (- dm rem))))))
  16391. (setq time
  16392. (encode-time (or (car time0) 0)
  16393. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16394. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16395. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16396. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16397. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16398. (nthcdr 6 time0)))
  16399. (when (and (member org-ts-what '(hour minute))
  16400. extra
  16401. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16402. (setq extra (org-modify-ts-extra
  16403. extra
  16404. (if (eq org-ts-what 'hour) 2 5)
  16405. n dm)))
  16406. (when (integerp org-ts-what)
  16407. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16408. (if (eq what 'calendar)
  16409. (let ((cal-date (org-get-date-from-calendar)))
  16410. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16411. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16412. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16413. (setcar time0 (or (car time0) 0))
  16414. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16415. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16416. (setq time (apply 'encode-time time0))))
  16417. ;; Insert the new time-stamp, and ensure point stays in the same
  16418. ;; category as before (i.e. not after the last position in that
  16419. ;; category).
  16420. (let ((pos (point)))
  16421. ;; Stay before inserted string. `save-excursion' is of no use.
  16422. (setq org-last-changed-timestamp
  16423. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16424. (goto-char pos))
  16425. (save-match-data
  16426. (looking-at org-ts-regexp3)
  16427. (goto-char (cond
  16428. ;; `day' category ends before `hour' if any, or at
  16429. ;; the end of the day name.
  16430. ((eq origin-cat 'day)
  16431. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16432. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16433. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16434. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16435. ;; `year' and `month' have both fixed size: point
  16436. ;; couldn't have moved into another part.
  16437. (t origin))))
  16438. ;; Update clock if on a CLOCK line.
  16439. (org-clock-update-time-maybe)
  16440. ;; Maybe adjust the closest clock in `org-clock-history'
  16441. (when org-clock-adjust-closest
  16442. (if (not (and (org-at-clock-log-p)
  16443. (< 1 (length (delq nil (mapcar 'marker-position
  16444. org-clock-history))))))
  16445. (message "No clock to adjust")
  16446. (cond ((save-excursion ; fix previous clock?
  16447. (re-search-backward org-ts-regexp0 nil t)
  16448. (org-looking-back (concat org-clock-string " \\[")))
  16449. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16450. ((save-excursion ; fix next clock?
  16451. (re-search-backward org-ts-regexp0 nil t)
  16452. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16453. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16454. (save-window-excursion
  16455. ;; Find closest clock to point, adjust the previous/next one in history
  16456. (let* ((p (save-excursion (org-back-to-heading t)))
  16457. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16458. (clfixnth
  16459. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16460. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16461. (if (not clfixpos)
  16462. (message "No clock to adjust")
  16463. (save-excursion
  16464. (org-goto-marker-or-bmk clfixpos)
  16465. (org-show-subtree)
  16466. (when (re-search-forward clrgx nil t)
  16467. (goto-char (match-beginning 1))
  16468. (let (org-clock-adjust-closest)
  16469. (org-timestamp-change n org-ts-what updown))
  16470. (message "Clock adjusted in %s for heading: %s"
  16471. (file-name-nondirectory (buffer-file-name))
  16472. (org-get-heading t t)))))))))
  16473. ;; Try to recenter the calendar window, if any.
  16474. (if (and org-calendar-follow-timestamp-change
  16475. (get-buffer-window "*Calendar*" t)
  16476. (memq org-ts-what '(day month year)))
  16477. (org-recenter-calendar (time-to-days time))))))
  16478. (defun org-modify-ts-extra (s pos n dm)
  16479. "Change the different parts of the lead-time and repeat fields in timestamp."
  16480. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16481. ng h m new rem)
  16482. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16483. (cond
  16484. ((or (org-pos-in-match-range pos 2)
  16485. (org-pos-in-match-range pos 3))
  16486. (setq m (string-to-number (match-string 3 s))
  16487. h (string-to-number (match-string 2 s)))
  16488. (if (org-pos-in-match-range pos 2)
  16489. (setq h (+ h n))
  16490. (setq n (* dm (org-no-warnings (signum n))))
  16491. (when (not (= 0 (setq rem (% m dm))))
  16492. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16493. (setq m (+ m n)))
  16494. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16495. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16496. (setq h (min 24 (max 0 h)))
  16497. (setq ng 1 new (format "-%02d:%02d" h m)))
  16498. ((org-pos-in-match-range pos 6)
  16499. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16500. ((org-pos-in-match-range pos 5)
  16501. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16502. ((org-pos-in-match-range pos 9)
  16503. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16504. ((org-pos-in-match-range pos 8)
  16505. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16506. (when ng
  16507. (setq s (concat
  16508. (substring s 0 (match-beginning ng))
  16509. new
  16510. (substring s (match-end ng))))))
  16511. s))
  16512. (defun org-recenter-calendar (date)
  16513. "If the calendar is visible, recenter it to DATE."
  16514. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16515. (when cwin
  16516. (let ((calendar-move-hook nil))
  16517. (with-selected-window cwin
  16518. (calendar-goto-date (if (listp date) date
  16519. (calendar-gregorian-from-absolute date))))))))
  16520. (defun org-goto-calendar (&optional arg)
  16521. "Go to the Emacs calendar at the current date.
  16522. If there is a time stamp in the current line, go to that date.
  16523. A prefix ARG can be used to force the current date."
  16524. (interactive "P")
  16525. (let ((tsr org-ts-regexp) diff
  16526. (calendar-move-hook nil)
  16527. (calendar-view-holidays-initially-flag nil)
  16528. (calendar-view-diary-initially-flag nil))
  16529. (if (or (org-at-timestamp-p)
  16530. (save-excursion
  16531. (beginning-of-line 1)
  16532. (looking-at (concat ".*" tsr))))
  16533. (let ((d1 (time-to-days (current-time)))
  16534. (d2 (time-to-days
  16535. (org-time-string-to-time (match-string 1)))))
  16536. (setq diff (- d2 d1))))
  16537. (calendar)
  16538. (calendar-goto-today)
  16539. (if (and diff (not arg)) (calendar-forward-day diff))))
  16540. (defun org-get-date-from-calendar ()
  16541. "Return a list (month day year) of date at point in calendar."
  16542. (with-current-buffer "*Calendar*"
  16543. (save-match-data
  16544. (calendar-cursor-to-date))))
  16545. (defun org-date-from-calendar ()
  16546. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16547. If there is already a time stamp at the cursor position, update it."
  16548. (interactive)
  16549. (if (org-at-timestamp-p t)
  16550. (org-timestamp-change 0 'calendar)
  16551. (let ((cal-date (org-get-date-from-calendar)))
  16552. (org-insert-time-stamp
  16553. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16554. (defcustom org-effort-durations
  16555. `(("min" . 1)
  16556. ("h" . 60)
  16557. ("d" . ,(* 60 8))
  16558. ("w" . ,(* 60 8 5))
  16559. ("m" . ,(* 60 8 5 4))
  16560. ("y" . ,(* 60 8 5 40)))
  16561. "Conversion factor to minutes for an effort modifier.
  16562. Each entry has the form (MODIFIER . MINUTES).
  16563. In an effort string, a number followed by MODIFIER is multiplied
  16564. by the specified number of MINUTES to obtain an effort in
  16565. minutes.
  16566. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16567. effort string \"2hours\" is equivalent to 120 minutes."
  16568. :group 'org-agenda
  16569. :version "25.1"
  16570. :package-version '(Org . "8.3")
  16571. :type '(alist :key-type (string :tag "Modifier")
  16572. :value-type (number :tag "Minutes")))
  16573. (defun org-minutes-to-clocksum-string (m)
  16574. "Format number of minutes as a clocksum string.
  16575. The format is determined by `org-time-clocksum-format',
  16576. `org-time-clocksum-use-fractional' and
  16577. `org-time-clocksum-fractional-format' and
  16578. `org-time-clocksum-use-effort-durations'."
  16579. (let ((clocksum "")
  16580. (m (round m)) ; Don't allow fractions of minutes
  16581. h d w mo y fmt n)
  16582. (setq h (if org-time-clocksum-use-effort-durations
  16583. (cdr (assoc "h" org-effort-durations)) 60)
  16584. d (if org-time-clocksum-use-effort-durations
  16585. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16586. w (if org-time-clocksum-use-effort-durations
  16587. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16588. mo (if org-time-clocksum-use-effort-durations
  16589. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16590. y (if org-time-clocksum-use-effort-durations
  16591. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16592. ;; fractional format
  16593. (if org-time-clocksum-use-fractional
  16594. (cond
  16595. ;; single format string
  16596. ((stringp org-time-clocksum-fractional-format)
  16597. (format org-time-clocksum-fractional-format (/ m (float h))))
  16598. ;; choice of fractional formats for different time units
  16599. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16600. (> (/ (truncate m) (* y d h)) 0))
  16601. (format fmt (/ m (* y d (float h)))))
  16602. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16603. (> (/ (truncate m) (* mo d h)) 0))
  16604. (format fmt (/ m (* mo d (float h)))))
  16605. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16606. (> (/ (truncate m) (* w d h)) 0))
  16607. (format fmt (/ m (* w d (float h)))))
  16608. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16609. (> (/ (truncate m) (* d h)) 0))
  16610. (format fmt (/ m (* d (float h)))))
  16611. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16612. (> (/ (truncate m) h) 0))
  16613. (format fmt (/ m (float h))))
  16614. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16615. (format fmt m))
  16616. ;; fall back to smallest time unit with a format
  16617. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16618. (format fmt (/ m (float h))))
  16619. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16620. (format fmt (/ m (* d (float h)))))
  16621. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16622. (format fmt (/ m (* w d (float h)))))
  16623. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16624. (format fmt (/ m (* mo d (float h)))))
  16625. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16626. (format fmt (/ m (* y d (float h))))))
  16627. ;; standard (non-fractional) format, with single format string
  16628. (if (stringp org-time-clocksum-format)
  16629. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16630. ;; separate formats components
  16631. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16632. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16633. (plist-get org-time-clocksum-format :require-years))
  16634. (setq clocksum (concat clocksum (format fmt n))
  16635. m (- m (* n y d h))))
  16636. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16637. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16638. (plist-get org-time-clocksum-format :require-months))
  16639. (setq clocksum (concat clocksum (format fmt n))
  16640. m (- m (* n mo d h))))
  16641. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16642. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16643. (plist-get org-time-clocksum-format :require-weeks))
  16644. (setq clocksum (concat clocksum (format fmt n))
  16645. m (- m (* n w d h))))
  16646. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16647. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16648. (plist-get org-time-clocksum-format :require-days))
  16649. (setq clocksum (concat clocksum (format fmt n))
  16650. m (- m (* n d h))))
  16651. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16652. (or (> (setq n (/ (truncate m) h)) 0)
  16653. (plist-get org-time-clocksum-format :require-hours))
  16654. (setq clocksum (concat clocksum (format fmt n))
  16655. m (- m (* n h))))
  16656. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16657. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16658. (setq clocksum (concat clocksum (format fmt m))))
  16659. ;; return formatted time duration
  16660. clocksum))))
  16661. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16662. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16663. "Org mode version 8.0")
  16664. (defun org-hours-to-clocksum-string (n)
  16665. (org-minutes-to-clocksum-string (* n 60)))
  16666. (defun org-hh:mm-string-to-minutes (s)
  16667. "Convert a string H:MM to a number of minutes.
  16668. If the string is just a number, interpret it as minutes.
  16669. In fact, the first hh:mm or number in the string will be taken,
  16670. there can be extra stuff in the string.
  16671. If no number is found, the return value is 0."
  16672. (cond
  16673. ((integerp s) s)
  16674. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16675. (+ (* (string-to-number (match-string 1 s)) 60)
  16676. (string-to-number (match-string 2 s))))
  16677. ((string-match "\\([0-9]+\\)" s)
  16678. (string-to-number (match-string 1 s)))
  16679. (t 0)))
  16680. (defcustom org-image-actual-width t
  16681. "Should we use the actual width of images when inlining them?
  16682. When set to `t', always use the image width.
  16683. When set to a number, use imagemagick (when available) to set
  16684. the image's width to this value.
  16685. When set to a number in a list, try to get the width from any
  16686. #+ATTR.* keyword if it matches a width specification like
  16687. #+ATTR_HTML: :width 300px
  16688. and fall back on that number if none is found.
  16689. When set to nil, try to get the width from an #+ATTR.* keyword
  16690. and fall back on the original width if none is found.
  16691. This requires Emacs >= 24.1, build with imagemagick support."
  16692. :group 'org-appearance
  16693. :version "24.4"
  16694. :package-version '(Org . "8.0")
  16695. :type '(choice
  16696. (const :tag "Use the image width" t)
  16697. (integer :tag "Use a number of pixels")
  16698. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16699. (const :tag "Use #+ATTR* or don't resize" nil)))
  16700. (defcustom org-agenda-inhibit-startup nil
  16701. "Inhibit startup when preparing agenda buffers.
  16702. When this variable is `t', the initialization of the Org agenda
  16703. buffers is inhibited: e.g. the visibility state is not set, the
  16704. tables are not re-aligned, etc."
  16705. :type 'boolean
  16706. :version "24.3"
  16707. :group 'org-agenda)
  16708. (define-obsolete-variable-alias
  16709. 'org-agenda-ignore-drawer-properties
  16710. 'org-agenda-ignore-properties "25.1")
  16711. (defcustom org-agenda-ignore-properties nil
  16712. "Avoid updating text properties when building the agenda.
  16713. Properties are used to prepare buffers for effort estimates,
  16714. appointments, statistics and subtree-local categories.
  16715. If you don't use these in the agenda, you can add them to this
  16716. list and agenda building will be a bit faster.
  16717. The value is a list, with zero or more of the symbols `effort', `appt',
  16718. `stats' or `category'."
  16719. :type '(set :greedy t
  16720. (const effort)
  16721. (const appt)
  16722. (const stats)
  16723. (const category))
  16724. :version "25.1"
  16725. :package-version '(Org . "8.3")
  16726. :group 'org-agenda)
  16727. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16728. "Convert a duration string S to minutes.
  16729. A bare number is interpreted as minutes, modifiers can be set by
  16730. customizing `org-effort-durations' (which see).
  16731. Entries containing a colon are interpreted as H:MM by
  16732. `org-hh:mm-string-to-minutes'."
  16733. (let ((result 0)
  16734. (re (concat "\\([0-9.]+\\) *\\("
  16735. (regexp-opt (mapcar 'car org-effort-durations))
  16736. "\\)")))
  16737. (while (string-match re s)
  16738. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16739. (string-to-number (match-string 1 s))))
  16740. (setq s (replace-match "" nil t s)))
  16741. (setq result (floor result))
  16742. (incf result (org-hh:mm-string-to-minutes s))
  16743. (if output-to-string (number-to-string result) result)))
  16744. ;;;; Files
  16745. (defun org-save-all-org-buffers ()
  16746. "Save all Org-mode buffers without user confirmation."
  16747. (interactive)
  16748. (message "Saving all Org-mode buffers...")
  16749. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16750. (when (featurep 'org-id) (org-id-locations-save))
  16751. (message "Saving all Org-mode buffers... done"))
  16752. (defun org-revert-all-org-buffers ()
  16753. "Revert all Org-mode buffers.
  16754. Prompt for confirmation when there are unsaved changes.
  16755. Be sure you know what you are doing before letting this function
  16756. overwrite your changes.
  16757. This function is useful in a setup where one tracks org files
  16758. with a version control system, to revert on one machine after pulling
  16759. changes from another. I believe the procedure must be like this:
  16760. 1. M-x org-save-all-org-buffers
  16761. 2. Pull changes from the other machine, resolve conflicts
  16762. 3. M-x org-revert-all-org-buffers"
  16763. (interactive)
  16764. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16765. (user-error "Abort"))
  16766. (save-excursion
  16767. (save-window-excursion
  16768. (mapc
  16769. (lambda (b)
  16770. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16771. (with-current-buffer b buffer-file-name))
  16772. (org-pop-to-buffer-same-window b)
  16773. (revert-buffer t 'no-confirm)))
  16774. (buffer-list))
  16775. (when (and (featurep 'org-id) org-id-track-globally)
  16776. (org-id-locations-load)))))
  16777. ;;;; Agenda files
  16778. ;;;###autoload
  16779. (defun org-switchb (&optional arg)
  16780. "Switch between Org buffers.
  16781. With one prefix argument, restrict available buffers to files.
  16782. With two prefix arguments, restrict available buffers to agenda files.
  16783. Defaults to `iswitchb' for buffer name completion.
  16784. Set `org-completion-use-ido' to make it use ido instead."
  16785. (interactive "P")
  16786. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16787. ((equal arg '(16)) (org-buffer-list 'agenda))
  16788. (t (org-buffer-list))))
  16789. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16790. (org-completion-use-ido org-completion-use-ido))
  16791. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16792. (setq org-completion-use-iswitchb t))
  16793. (org-pop-to-buffer-same-window
  16794. (org-icompleting-read "Org buffer: "
  16795. (mapcar 'list (mapcar 'buffer-name blist))
  16796. nil t))))
  16797. ;;; Define some older names previously used for this functionality
  16798. ;;;###autoload
  16799. (defalias 'org-ido-switchb 'org-switchb)
  16800. ;;;###autoload
  16801. (defalias 'org-iswitchb 'org-switchb)
  16802. (defun org-buffer-list (&optional predicate exclude-tmp)
  16803. "Return a list of Org buffers.
  16804. PREDICATE can be `export', `files' or `agenda'.
  16805. export restrict the list to Export buffers.
  16806. files restrict the list to buffers visiting Org files.
  16807. agenda restrict the list to buffers visiting agenda files.
  16808. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16809. (let* ((bfn nil)
  16810. (agenda-files (and (eq predicate 'agenda)
  16811. (mapcar 'file-truename (org-agenda-files t))))
  16812. (filter
  16813. (cond
  16814. ((eq predicate 'files)
  16815. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16816. ((eq predicate 'export)
  16817. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16818. ((eq predicate 'agenda)
  16819. (lambda (b)
  16820. (with-current-buffer b
  16821. (and (derived-mode-p 'org-mode)
  16822. (setq bfn (buffer-file-name b))
  16823. (member (file-truename bfn) agenda-files)))))
  16824. (t (lambda (b) (with-current-buffer b
  16825. (or (derived-mode-p 'org-mode)
  16826. (string-match "\*Org .*Export"
  16827. (buffer-name b)))))))))
  16828. (delq nil
  16829. (mapcar
  16830. (lambda(b)
  16831. (if (and (funcall filter b)
  16832. (or (not exclude-tmp)
  16833. (not (string-match "tmp" (buffer-name b)))))
  16834. b
  16835. nil))
  16836. (buffer-list)))))
  16837. (defun org-agenda-files (&optional unrestricted archives)
  16838. "Get the list of agenda files.
  16839. Optional UNRESTRICTED means return the full list even if a restriction
  16840. is currently in place.
  16841. When ARCHIVES is t, include all archive files that are really being
  16842. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16843. `org-agenda-archives-mode' is t."
  16844. (let ((files
  16845. (cond
  16846. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16847. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16848. ((listp org-agenda-files) org-agenda-files)
  16849. (t (error "Invalid value of `org-agenda-files'")))))
  16850. (setq files (apply 'append
  16851. (mapcar (lambda (f)
  16852. (if (file-directory-p f)
  16853. (directory-files
  16854. f t org-agenda-file-regexp)
  16855. (list f)))
  16856. files)))
  16857. (when org-agenda-skip-unavailable-files
  16858. (setq files (delq nil
  16859. (mapcar (function
  16860. (lambda (file)
  16861. (and (file-readable-p file) file)))
  16862. files))))
  16863. (when (or (eq archives t)
  16864. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16865. (setq files (org-add-archive-files files)))
  16866. files))
  16867. (defun org-agenda-file-p (&optional file)
  16868. "Return non-nil, if FILE is an agenda file.
  16869. If FILE is omitted, use the file associated with the current
  16870. buffer."
  16871. (let ((fname (or file (buffer-file-name))))
  16872. (and fname
  16873. (member (file-truename fname)
  16874. (mapcar #'file-truename (org-agenda-files t))))))
  16875. (defun org-edit-agenda-file-list ()
  16876. "Edit the list of agenda files.
  16877. Depending on setup, this either uses customize to edit the variable
  16878. `org-agenda-files', or it visits the file that is holding the list. In the
  16879. latter case, the buffer is set up in a way that saving it automatically kills
  16880. the buffer and restores the previous window configuration."
  16881. (interactive)
  16882. (if (stringp org-agenda-files)
  16883. (let ((cw (current-window-configuration)))
  16884. (find-file org-agenda-files)
  16885. (org-set-local 'org-window-configuration cw)
  16886. (org-add-hook 'after-save-hook
  16887. (lambda ()
  16888. (set-window-configuration
  16889. (prog1 org-window-configuration
  16890. (kill-buffer (current-buffer))))
  16891. (org-install-agenda-files-menu)
  16892. (message "New agenda file list installed"))
  16893. nil 'local)
  16894. (message "%s" (substitute-command-keys
  16895. "Edit list and finish with \\[save-buffer]")))
  16896. (customize-variable 'org-agenda-files)))
  16897. (defun org-store-new-agenda-file-list (list)
  16898. "Set new value for the agenda file list and save it correctly."
  16899. (if (stringp org-agenda-files)
  16900. (let ((fe (org-read-agenda-file-list t)) b u)
  16901. (while (setq b (find-buffer-visiting org-agenda-files))
  16902. (kill-buffer b))
  16903. (with-temp-file org-agenda-files
  16904. (insert
  16905. (mapconcat
  16906. (lambda (f) ;; Keep un-expanded entries.
  16907. (if (setq u (assoc f fe))
  16908. (cdr u)
  16909. f))
  16910. list "\n")
  16911. "\n")))
  16912. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16913. (org-insert-mode-line-in-empty-file nil))
  16914. (setq org-agenda-files list)
  16915. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16916. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16917. "Read the list of agenda files from a file.
  16918. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16919. filenames, used by `org-store-new-agenda-file-list' to write back
  16920. un-expanded file names."
  16921. (when (file-directory-p org-agenda-files)
  16922. (error "`org-agenda-files' cannot be a single directory"))
  16923. (when (stringp org-agenda-files)
  16924. (with-temp-buffer
  16925. (insert-file-contents org-agenda-files)
  16926. (mapcar
  16927. (lambda (f)
  16928. (let ((e (expand-file-name (substitute-in-file-name f)
  16929. org-directory)))
  16930. (if pair-with-expansion
  16931. (cons e f)
  16932. e)))
  16933. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16934. ;;;###autoload
  16935. (defun org-cycle-agenda-files ()
  16936. "Cycle through the files in `org-agenda-files'.
  16937. If the current buffer visits an agenda file, find the next one in the list.
  16938. If the current buffer does not, find the first agenda file."
  16939. (interactive)
  16940. (let* ((fs (org-agenda-files t))
  16941. (files (append fs (list (car fs))))
  16942. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16943. file)
  16944. (unless files (user-error "No agenda files"))
  16945. (catch 'exit
  16946. (while (setq file (pop files))
  16947. (if (equal (file-truename file) tcf)
  16948. (when (car files)
  16949. (find-file (car files))
  16950. (throw 'exit t))))
  16951. (find-file (car fs)))
  16952. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16953. (defun org-agenda-file-to-front (&optional to-end)
  16954. "Move/add the current file to the top of the agenda file list.
  16955. If the file is not present in the list, it is added to the front. If it is
  16956. present, it is moved there. With optional argument TO-END, add/move to the
  16957. end of the list."
  16958. (interactive "P")
  16959. (let ((org-agenda-skip-unavailable-files nil)
  16960. (file-alist (mapcar (lambda (x)
  16961. (cons (file-truename x) x))
  16962. (org-agenda-files t)))
  16963. (ctf (file-truename
  16964. (or buffer-file-name
  16965. (user-error "Please save the current buffer to a file"))))
  16966. x had)
  16967. (setq x (assoc ctf file-alist) had x)
  16968. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16969. (if to-end
  16970. (setq file-alist (append (delq x file-alist) (list x)))
  16971. (setq file-alist (cons x (delq x file-alist))))
  16972. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16973. (org-install-agenda-files-menu)
  16974. (message "File %s to %s of agenda file list"
  16975. (if had "moved" "added") (if to-end "end" "front"))))
  16976. (defun org-remove-file (&optional file)
  16977. "Remove current file from the list of files in variable `org-agenda-files'.
  16978. These are the files which are being checked for agenda entries.
  16979. Optional argument FILE means use this file instead of the current."
  16980. (interactive)
  16981. (let* ((org-agenda-skip-unavailable-files nil)
  16982. (file (or file buffer-file-name
  16983. (user-error "Current buffer does not visit a file")))
  16984. (true-file (file-truename file))
  16985. (afile (abbreviate-file-name file))
  16986. (files (delq nil (mapcar
  16987. (lambda (x)
  16988. (if (equal true-file
  16989. (file-truename x))
  16990. nil x))
  16991. (org-agenda-files t)))))
  16992. (if (not (= (length files) (length (org-agenda-files t))))
  16993. (progn
  16994. (org-store-new-agenda-file-list files)
  16995. (org-install-agenda-files-menu)
  16996. (message "Removed from Org Agenda list: %s" afile))
  16997. (message "File was not in list: %s (not removed)" afile))))
  16998. (defun org-file-menu-entry (file)
  16999. (vector file (list 'find-file file) t))
  17000. (defun org-check-agenda-file (file)
  17001. "Make sure FILE exists. If not, ask user what to do."
  17002. (when (not (file-exists-p file))
  17003. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17004. (abbreviate-file-name file))
  17005. (let ((r (downcase (read-char-exclusive))))
  17006. (cond
  17007. ((equal r ?r)
  17008. (org-remove-file file)
  17009. (throw 'nextfile t))
  17010. (t (user-error "Abort"))))))
  17011. (defun org-get-agenda-file-buffer (file)
  17012. "Get an agenda buffer visiting FILE.
  17013. If the buffer needs to be created, add it to the list of buffers
  17014. which might be released later."
  17015. (let ((buf (org-find-base-buffer-visiting file)))
  17016. (if buf
  17017. buf ; just return it
  17018. ;; Make a new buffer and remember it
  17019. (setq buf (find-file-noselect file))
  17020. (if buf (push buf org-agenda-new-buffers))
  17021. buf)))
  17022. (defun org-release-buffers (blist)
  17023. "Release all buffers in list, asking the user for confirmation when needed.
  17024. When a buffer is unmodified, it is just killed. When modified, it is saved
  17025. \(if the user agrees) and then killed."
  17026. (let (buf file)
  17027. (while (setq buf (pop blist))
  17028. (setq file (buffer-file-name buf))
  17029. (when (and (buffer-modified-p buf)
  17030. file
  17031. (y-or-n-p (format "Save file %s? " file)))
  17032. (with-current-buffer buf (save-buffer)))
  17033. (kill-buffer buf))))
  17034. (defun org-agenda-prepare-buffers (files)
  17035. "Create buffers for all agenda files, protect archived trees and comments."
  17036. (interactive)
  17037. (let ((pa '(:org-archived t))
  17038. (pc '(:org-comment t))
  17039. (pall '(:org-archived t :org-comment t))
  17040. (inhibit-read-only t)
  17041. (org-inhibit-startup org-agenda-inhibit-startup)
  17042. (rea (concat ":" org-archive-tag ":"))
  17043. file re pos)
  17044. (setq org-tag-alist-for-agenda nil
  17045. org-tag-groups-alist-for-agenda nil)
  17046. (save-excursion
  17047. (save-restriction
  17048. (while (setq file (pop files))
  17049. (catch 'nextfile
  17050. (if (bufferp file)
  17051. (set-buffer file)
  17052. (org-check-agenda-file file)
  17053. (set-buffer (org-get-agenda-file-buffer file)))
  17054. (widen)
  17055. (org-set-regexps-and-options 'tags-only)
  17056. (setq pos (point))
  17057. (or (memq 'category org-agenda-ignore-properties)
  17058. (org-refresh-category-properties))
  17059. (or (memq 'stats org-agenda-ignore-properties)
  17060. (org-refresh-stats-properties))
  17061. (or (memq 'effort org-agenda-ignore-properties)
  17062. (org-refresh-effort-properties))
  17063. (or (memq 'appt org-agenda-ignore-properties)
  17064. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17065. (setq org-todo-keywords-for-agenda
  17066. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17067. (setq org-done-keywords-for-agenda
  17068. (append org-done-keywords-for-agenda org-done-keywords))
  17069. (setq org-todo-keyword-alist-for-agenda
  17070. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17071. (setq org-tag-alist-for-agenda
  17072. (org-uniquify
  17073. (append org-tag-alist-for-agenda
  17074. org-tag-alist
  17075. org-tag-persistent-alist)))
  17076. (if org-group-tags
  17077. (setq org-tag-groups-alist-for-agenda
  17078. (org-uniquify-alist
  17079. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17080. (org-with-silent-modifications
  17081. (save-excursion
  17082. (remove-text-properties (point-min) (point-max) pall)
  17083. (when org-agenda-skip-archived-trees
  17084. (goto-char (point-min))
  17085. (while (re-search-forward rea nil t)
  17086. (if (org-at-heading-p t)
  17087. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17088. (goto-char (point-min))
  17089. (setq re (format "^\\* .*\\<%s\\>" org-comment-string))
  17090. (while (re-search-forward re nil t)
  17091. (when (save-match-data (org-in-commented-heading-p t))
  17092. (add-text-properties
  17093. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17094. (goto-char pos)))))
  17095. (setq org-todo-keywords-for-agenda
  17096. (org-uniquify org-todo-keywords-for-agenda))
  17097. (setq org-todo-keyword-alist-for-agenda
  17098. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17099. ;;;; CDLaTeX minor mode
  17100. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17101. "Keymap for the minor `org-cdlatex-mode'.")
  17102. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17103. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17104. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17105. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17106. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17107. (defvar org-cdlatex-texmathp-advice-is-done nil
  17108. "Flag remembering if we have applied the advice to texmathp already.")
  17109. (define-minor-mode org-cdlatex-mode
  17110. "Toggle the minor `org-cdlatex-mode'.
  17111. This mode supports entering LaTeX environment and math in LaTeX fragments
  17112. in Org-mode.
  17113. \\{org-cdlatex-mode-map}"
  17114. nil " OCDL" nil
  17115. (when org-cdlatex-mode
  17116. (require 'cdlatex)
  17117. (run-hooks 'cdlatex-mode-hook)
  17118. (cdlatex-compute-tables))
  17119. (unless org-cdlatex-texmathp-advice-is-done
  17120. (setq org-cdlatex-texmathp-advice-is-done t)
  17121. (defadvice texmathp (around org-math-always-on activate)
  17122. "Always return t in org-mode buffers.
  17123. This is because we want to insert math symbols without dollars even outside
  17124. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17125. an embedded LaTeX fragment, let texmathp do its job.
  17126. \\[org-cdlatex-mode-map]"
  17127. (interactive)
  17128. (let (p)
  17129. (cond
  17130. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17131. ((eq this-command 'cdlatex-math-symbol)
  17132. (setq ad-return-value t
  17133. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17134. (t
  17135. (let ((p (org-inside-LaTeX-fragment-p)))
  17136. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17137. (setq ad-return-value t
  17138. texmathp-why '("Org-mode embedded math" . 0))
  17139. (if p ad-do-it)))))))))
  17140. (defun turn-on-org-cdlatex ()
  17141. "Unconditionally turn on `org-cdlatex-mode'."
  17142. (org-cdlatex-mode 1))
  17143. (defun org-try-cdlatex-tab ()
  17144. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17145. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17146. - inside a LaTeX fragment, or
  17147. - after the first word in a line, where an abbreviation expansion could
  17148. insert a LaTeX environment."
  17149. (when org-cdlatex-mode
  17150. (cond
  17151. ;; Before any word on the line: No expansion possible.
  17152. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17153. ;; Just after first word on the line: Expand it. Make sure it
  17154. ;; cannot happen on headlines, though.
  17155. ((save-excursion
  17156. (skip-chars-backward "a-zA-Z0-9*")
  17157. (skip-chars-backward " \t")
  17158. (and (bolp) (not (org-at-heading-p))))
  17159. (cdlatex-tab) t)
  17160. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17161. (defun org-cdlatex-underscore-caret (&optional arg)
  17162. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17163. Revert to the normal definition outside of these fragments."
  17164. (interactive "P")
  17165. (if (org-inside-LaTeX-fragment-p)
  17166. (call-interactively 'cdlatex-sub-superscript)
  17167. (let (org-cdlatex-mode)
  17168. (call-interactively (key-binding (vector last-input-event))))))
  17169. (defun org-cdlatex-math-modify (&optional arg)
  17170. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17171. Revert to the normal definition outside of these fragments."
  17172. (interactive "P")
  17173. (if (org-inside-LaTeX-fragment-p)
  17174. (call-interactively 'cdlatex-math-modify)
  17175. (let (org-cdlatex-mode)
  17176. (call-interactively (key-binding (vector last-input-event))))))
  17177. (defun org-cdlatex-environment-indent (&optional environment item)
  17178. "Execute `cdlatex-environment' and indent the inserted environment.
  17179. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17180. The inserted environment is indented to current indentation
  17181. unless point is at the beginning of the line, in which the
  17182. environment remains unintended."
  17183. (interactive)
  17184. ;; cdlatex-environment always return nil. Therefore, capture output
  17185. ;; first and determine if an environment was selected.
  17186. (let* ((beg (point-marker))
  17187. (end (copy-marker (point) t))
  17188. (inserted (progn
  17189. (ignore-errors (cdlatex-environment environment item))
  17190. (< beg end)))
  17191. ;; Figure out how many lines to move forward after the
  17192. ;; environment has been inserted.
  17193. (lines (when inserted
  17194. (save-excursion
  17195. (- (loop while (< beg (point))
  17196. with x = 0
  17197. do (forward-line -1)
  17198. (incf x)
  17199. finally return x)
  17200. (if (progn (goto-char beg)
  17201. (and (progn (skip-chars-forward " \t") (eolp))
  17202. (progn (skip-chars-backward " \t") (bolp))))
  17203. 1 0)))))
  17204. (env (org-trim (delete-and-extract-region beg end))))
  17205. (when inserted
  17206. ;; Get indentation of next line unless at column 0.
  17207. (let ((ind (if (bolp) 0
  17208. (save-excursion
  17209. (org-return-indent)
  17210. (prog1 (org-get-indentation)
  17211. (when (progn (skip-chars-forward " \t") (eolp))
  17212. (delete-region beg (point)))))))
  17213. (bol (progn (skip-chars-backward " \t") (bolp))))
  17214. ;; Insert a newline before environment unless at column zero
  17215. ;; to "escape" the current line. Insert a newline if
  17216. ;; something is one the same line as \end{ENVIRONMENT}.
  17217. (insert
  17218. (concat (unless bol "\n") env
  17219. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17220. (unless (zerop ind)
  17221. (save-excursion
  17222. (goto-char beg)
  17223. (while (< (point) end)
  17224. (unless (eolp) (org-indent-line-to ind))
  17225. (forward-line))))
  17226. (goto-char beg)
  17227. (forward-line lines)
  17228. (org-indent-line-to ind)))
  17229. (set-marker beg nil)
  17230. (set-marker end nil)))
  17231. ;;;; LaTeX fragments
  17232. (defun org-inside-LaTeX-fragment-p ()
  17233. "Test if point is inside a LaTeX fragment.
  17234. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17235. sequence appearing also before point.
  17236. Even though the matchers for math are configurable, this function assumes
  17237. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17238. delimiters are skipped when they have been removed by customization.
  17239. The return value is nil, or a cons cell with the delimiter and the
  17240. position of this delimiter.
  17241. This function does a reasonably good job, but can locally be fooled by
  17242. for example currency specifications. For example it will assume being in
  17243. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17244. fragments that are properly closed, but during editing, we have to live
  17245. with the uncertainty caused by missing closing delimiters. This function
  17246. looks only before point, not after."
  17247. (catch 'exit
  17248. (let ((pos (point))
  17249. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17250. (lim (progn
  17251. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17252. (point)))
  17253. dd-on str (start 0) m re)
  17254. (goto-char pos)
  17255. (when dodollar
  17256. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17257. re (nth 1 (assoc "$" org-latex-regexps)))
  17258. (while (string-match re str start)
  17259. (cond
  17260. ((= (match-end 0) (length str))
  17261. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17262. ((= (match-end 0) (- (length str) 5))
  17263. (throw 'exit nil))
  17264. (t (setq start (match-end 0))))))
  17265. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17266. (goto-char pos)
  17267. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17268. (and (match-beginning 2) (throw 'exit nil))
  17269. ;; count $$
  17270. (while (re-search-backward "\\$\\$" lim t)
  17271. (setq dd-on (not dd-on)))
  17272. (goto-char pos)
  17273. (if dd-on (cons "$$" m))))))
  17274. (defun org-inside-latex-macro-p ()
  17275. "Is point inside a LaTeX macro or its arguments?"
  17276. (save-match-data
  17277. (org-in-regexp
  17278. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17279. (defvar org-latex-fragment-image-overlays nil
  17280. "List of overlays carrying the images of latex fragments.")
  17281. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17282. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17283. "Remove all overlays with LaTeX fragment images in current buffer.
  17284. When optional arguments BEG and END are non-nil, remove all
  17285. overlays between them instead. Return t when some overlays were
  17286. removed, nil otherwise."
  17287. (let (removedp)
  17288. (setq org-latex-fragment-image-overlays
  17289. (let ((beg (or beg (point-min)))
  17290. (end (or end (point-max))))
  17291. (org-remove-if
  17292. (lambda (o)
  17293. (and (>= (overlay-start o) beg)
  17294. (<= (overlay-end o) end)
  17295. (progn (delete-overlay o)
  17296. (or removedp (setq removedp t)))))
  17297. org-latex-fragment-image-overlays)))
  17298. removedp))
  17299. (define-obsolete-function-alias
  17300. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17301. (defun org-toggle-latex-fragment (&optional arg)
  17302. "Preview the LaTeX fragment at point, or all locally or globally.
  17303. If the cursor is on a LaTeX fragment, create the image and overlay
  17304. it over the source code, if there is none. Remove it otherwise.
  17305. If there is no fragment at point, display all fragments in the
  17306. current section.
  17307. With prefix ARG, preview or clear image for all fragments in the
  17308. current subtree or in the whole buffer when used before the first
  17309. headline. With a double prefix ARG \\[universal-argument] \
  17310. \\[universal-argument] preview or clear images
  17311. for all fragments in the buffer."
  17312. (interactive "P")
  17313. (unless (buffer-file-name (buffer-base-buffer))
  17314. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17315. (when (display-graphic-p)
  17316. (catch 'exit
  17317. (save-excursion
  17318. (let ((window-start (window-start)) msg)
  17319. (save-restriction
  17320. (cond
  17321. ((or (equal arg '(16))
  17322. (and (equal arg '(4))
  17323. (org-with-limited-levels (org-before-first-heading-p))))
  17324. (if (org-remove-latex-fragment-image-overlays)
  17325. (progn (message "LaTeX fragments images removed from buffer")
  17326. (throw 'exit nil))
  17327. (setq msg "Creating images for buffer...")))
  17328. ((equal arg '(4))
  17329. (org-with-limited-levels (org-back-to-heading t))
  17330. (let ((beg (point))
  17331. (end (progn (org-end-of-subtree t) (point))))
  17332. (if (org-remove-latex-fragment-image-overlays beg end)
  17333. (progn
  17334. (message "LaTeX fragment images removed from subtree")
  17335. (throw 'exit nil))
  17336. (setq msg "Creating images for subtree...")
  17337. (narrow-to-region beg end))))
  17338. ((let ((datum (org-element-context)))
  17339. (when (memq (org-element-type datum)
  17340. '(latex-environment latex-fragment))
  17341. (let* ((beg (org-element-property :begin datum))
  17342. (end (org-element-property :end datum)))
  17343. (if (org-remove-latex-fragment-image-overlays beg end)
  17344. (progn (message "LaTeX fragment image removed")
  17345. (throw 'exit nil))
  17346. (narrow-to-region beg end)
  17347. (setq msg "Creating image..."))))))
  17348. (t
  17349. (org-with-limited-levels
  17350. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17351. (outline-previous-heading)
  17352. (point)))
  17353. (end (progn (outline-next-heading) (point))))
  17354. (if (org-remove-latex-fragment-image-overlays beg end)
  17355. (progn
  17356. (message "LaTeX fragment images removed from section")
  17357. (throw 'exit nil))
  17358. (setq msg "Creating images for section...")
  17359. (narrow-to-region beg end))))))
  17360. (let ((file (buffer-file-name (buffer-base-buffer))))
  17361. (org-format-latex
  17362. (concat org-latex-preview-ltxpng-directory
  17363. (file-name-sans-extension (file-name-nondirectory file)))
  17364. ;; Emacs cannot overlay images from remote hosts.
  17365. ;; Create it in `temporary-file-directory' instead.
  17366. (if (file-remote-p file) temporary-file-directory
  17367. default-directory)
  17368. 'overlays msg 'forbuffer
  17369. org-latex-create-formula-image-program)))
  17370. ;; Work around a bug that doesn't restore window's start
  17371. ;; when widening back the buffer.
  17372. (set-window-start nil window-start)
  17373. (message (concat msg "done")))))))
  17374. (defun org-format-latex
  17375. (prefix &optional dir overlays msg forbuffer processing-type)
  17376. "Replace LaTeX fragments with links to an image, and produce images.
  17377. When optional argument OVERLAYS is non-nil, display the image on
  17378. top of the fragment instead of replacing it.
  17379. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17380. Some of the options can be changed using the variable
  17381. `org-format-latex-options', which see."
  17382. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17383. (unless (eq processing-type 'verbatim)
  17384. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17385. (cnt 0)
  17386. checkdir-flag)
  17387. (goto-char (point-min))
  17388. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17389. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17390. (overlay-recenter (point-max)))
  17391. (while (re-search-forward math-regexp nil t)
  17392. (unless (and overlays
  17393. (eq (get-char-property (point) 'org-overlay-type)
  17394. 'org-latex-overlay))
  17395. (let* ((context (org-element-context))
  17396. (type (org-element-type context)))
  17397. (when (memq type '(latex-environment latex-fragment))
  17398. (let ((block-type (eq type 'latex-environment))
  17399. (value (org-element-property :value context))
  17400. (beg (org-element-property :begin context))
  17401. (end (save-excursion
  17402. (goto-char (org-element-property :end context))
  17403. (skip-chars-backward " \r\t\n")
  17404. (point))))
  17405. (case processing-type
  17406. (mathjax
  17407. ;; Prepare for MathJax processing.
  17408. (if (eq (char-after beg) ?$)
  17409. (save-excursion
  17410. (delete-region beg end)
  17411. (insert "\\(" (substring value 1 -1) "\\)"))
  17412. (goto-char end)))
  17413. ((dvipng imagemagick)
  17414. ;; Process to an image.
  17415. (incf cnt)
  17416. (goto-char beg)
  17417. (let* ((face (face-at-point))
  17418. ;; Get the colors from the face at point.
  17419. (fg
  17420. (let ((color (plist-get org-format-latex-options
  17421. :foreground)))
  17422. (if (and forbuffer (eq color 'auto))
  17423. (face-attribute face :foreground nil 'default)
  17424. color)))
  17425. (bg
  17426. (let ((color (plist-get org-format-latex-options
  17427. :background)))
  17428. (if (and forbuffer (eq color 'auto))
  17429. (face-attribute face :background nil 'default)
  17430. color)))
  17431. (hash (sha1 (prin1-to-string
  17432. (list org-format-latex-header
  17433. org-latex-default-packages-alist
  17434. org-latex-packages-alist
  17435. org-format-latex-options
  17436. forbuffer value fg bg))))
  17437. (absprefix (expand-file-name prefix dir))
  17438. (linkfile (format "%s_%s.png" prefix hash))
  17439. (movefile (format "%s_%s.png" absprefix hash))
  17440. (sep (and block-type "\n\n"))
  17441. (link (concat sep "[[file:" linkfile "]]" sep))
  17442. (options
  17443. (org-combine-plists
  17444. org-format-latex-options
  17445. `(:foreground ,fg :background ,bg))))
  17446. (when msg (message msg cnt))
  17447. (unless checkdir-flag ; Ensure the directory exists.
  17448. (setq checkdir-flag t)
  17449. (let ((todir (file-name-directory absprefix)))
  17450. (unless (file-directory-p todir)
  17451. (make-directory todir t))))
  17452. (unless (file-exists-p movefile)
  17453. (org-create-formula-image
  17454. value movefile options forbuffer processing-type))
  17455. (if overlays
  17456. (progn
  17457. (dolist (o (overlays-in beg end))
  17458. (when (eq (overlay-get o 'org-overlay-type)
  17459. 'org-latex-overlay)
  17460. (delete-overlay o)))
  17461. (let ((ov (make-overlay beg end)))
  17462. (overlay-put ov
  17463. 'org-overlay-type
  17464. 'org-latex-overlay)
  17465. (overlay-put ov 'evaporate t)
  17466. (if (featurep 'xemacs)
  17467. (progn
  17468. (overlay-put ov 'invisible t)
  17469. (overlay-put
  17470. ov 'end-glyph
  17471. (make-glyph
  17472. (vector 'png :file movefile))))
  17473. (overlay-put
  17474. ov 'display
  17475. (list 'image
  17476. :type 'png
  17477. :file movefile
  17478. :ascent 'center)))
  17479. (push ov org-latex-fragment-image-overlays))
  17480. (goto-char end))
  17481. (delete-region beg end)
  17482. (insert
  17483. (org-add-props link
  17484. (list 'org-latex-src
  17485. (replace-regexp-in-string "\"" "" value)
  17486. 'org-latex-src-embed-type
  17487. (if block-type 'paragraph 'character)))))))
  17488. (mathml
  17489. ;; Process to MathML.
  17490. (unless (org-format-latex-mathml-available-p)
  17491. (user-error "LaTeX to MathML converter not configured"))
  17492. (incf cnt)
  17493. (when msg (message msg cnt))
  17494. (goto-char beg)
  17495. (delete-region beg end)
  17496. (insert (org-format-latex-as-mathml
  17497. value block-type prefix dir)))
  17498. (otherwise
  17499. (error "Unknown conversion type %s for LaTeX fragments"
  17500. processing-type)))))))))))
  17501. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17502. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17503. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17504. sucessful, return the portion between \"<math...> </math>\"
  17505. elements otherwise return nil. When MATHML-FILE is specified,
  17506. write the results in to that file. When invoked as an
  17507. interactive command, prompt for LATEX-FRAG, with initial value
  17508. set to the current active region and echo the results for user
  17509. inspection."
  17510. (interactive (list (let ((frag (when (org-region-active-p)
  17511. (buffer-substring-no-properties
  17512. (region-beginning) (region-end)))))
  17513. (read-string "LaTeX Fragment: " frag nil frag))))
  17514. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17515. (let* ((tmp-in-file (file-relative-name
  17516. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17517. (ignore (write-region latex-frag nil tmp-in-file))
  17518. (tmp-out-file (file-relative-name
  17519. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17520. (cmd (format-spec
  17521. org-latex-to-mathml-convert-command
  17522. `((?j . ,(shell-quote-argument
  17523. (expand-file-name org-latex-to-mathml-jar-file)))
  17524. (?I . ,(shell-quote-argument tmp-in-file))
  17525. (?i . ,latex-frag)
  17526. (?o . ,(shell-quote-argument tmp-out-file)))))
  17527. mathml shell-command-output)
  17528. (when (org-called-interactively-p 'any)
  17529. (unless (org-format-latex-mathml-available-p)
  17530. (user-error "LaTeX to MathML converter not configured")))
  17531. (message "Running %s" cmd)
  17532. (setq shell-command-output (shell-command-to-string cmd))
  17533. (setq mathml
  17534. (when (file-readable-p tmp-out-file)
  17535. (with-current-buffer (find-file-noselect tmp-out-file t)
  17536. (goto-char (point-min))
  17537. (when (re-search-forward
  17538. (concat
  17539. (regexp-quote
  17540. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17541. "[^>]*?>"
  17542. "\\(.\\|\n\\)*"
  17543. "</math>")
  17544. nil t)
  17545. (prog1 (match-string 0) (kill-buffer))))))
  17546. (cond
  17547. (mathml
  17548. (setq mathml
  17549. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17550. (when mathml-file
  17551. (write-region mathml nil mathml-file))
  17552. (when (org-called-interactively-p 'any)
  17553. (message mathml)))
  17554. ((message "LaTeX to MathML conversion failed")
  17555. (message shell-command-output)))
  17556. (delete-file tmp-in-file)
  17557. (when (file-exists-p tmp-out-file)
  17558. (delete-file tmp-out-file))
  17559. mathml))
  17560. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17561. prefix &optional dir)
  17562. "Use `org-create-math-formula' but check local cache first."
  17563. (let* ((absprefix (expand-file-name prefix dir))
  17564. (print-length nil) (print-level nil)
  17565. (formula-id (concat
  17566. "formula-"
  17567. (sha1
  17568. (prin1-to-string
  17569. (list latex-frag
  17570. org-latex-to-mathml-convert-command)))))
  17571. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17572. (formula-cache-dir (file-name-directory formula-cache)))
  17573. (unless (file-directory-p formula-cache-dir)
  17574. (make-directory formula-cache-dir t))
  17575. (unless (file-exists-p formula-cache)
  17576. (org-create-math-formula latex-frag formula-cache))
  17577. (if (file-exists-p formula-cache)
  17578. ;; Successful conversion. Return the link to MathML file.
  17579. (org-add-props
  17580. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17581. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17582. 'org-latex-src-embed-type (if latex-frag-type
  17583. 'paragraph 'character)))
  17584. ;; Failed conversion. Return the LaTeX fragment verbatim
  17585. latex-frag)))
  17586. (defun org-create-formula-image (string tofile options buffer &optional type)
  17587. "Create an image from LaTeX source using dvipng or convert.
  17588. This function calls either `org-create-formula-image-with-dvipng'
  17589. or `org-create-formula-image-with-imagemagick' depending on the
  17590. value of `org-latex-create-formula-image-program' or on the value
  17591. of the optional TYPE variable.
  17592. Note: ultimately these two function should be combined as they
  17593. share a good deal of logic."
  17594. (org-check-external-command
  17595. "latex" "needed to convert LaTeX fragments to images")
  17596. (funcall
  17597. (case (or type org-latex-create-formula-image-program)
  17598. ('dvipng
  17599. (org-check-external-command
  17600. "dvipng" "needed to convert LaTeX fragments to images")
  17601. 'org-create-formula-image-with-dvipng)
  17602. ('imagemagick
  17603. (org-check-external-command
  17604. "convert" "you need to install imagemagick")
  17605. 'org-create-formula-image-with-imagemagick)
  17606. (t (error
  17607. "Invalid value of `org-latex-create-formula-image-program'")))
  17608. string tofile options buffer))
  17609. (declare-function org-export-get-backend "ox" (name))
  17610. (declare-function org-export--get-global-options "ox" (&optional backend))
  17611. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17612. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17613. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17614. (defun org-create-formula--latex-header ()
  17615. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17616. (let ((info (org-combine-plists (org-export--get-global-options
  17617. (org-export-get-backend 'latex))
  17618. (org-export--get-inbuffer-options
  17619. (org-export-get-backend 'latex)))))
  17620. (org-latex-guess-babel-language
  17621. (org-latex-guess-inputenc
  17622. (org-splice-latex-header
  17623. org-format-latex-header
  17624. org-latex-default-packages-alist
  17625. org-latex-packages-alist t
  17626. (plist-get info :latex-header)))
  17627. info)))
  17628. (defun org--get-display-dpi ()
  17629. "Get the DPI of the display.
  17630. Assumes that the display has the same pixel width in the
  17631. horizontal and vertical directions."
  17632. (if (display-graphic-p)
  17633. (round (/ (display-pixel-height)
  17634. (/ (display-mm-height) 25.4)))
  17635. (error "Attempt to calculate the dpi of a non-graphic display")))
  17636. ;; This function borrows from Ganesh Swami's latex2png.el
  17637. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17638. "This calls dvipng."
  17639. (require 'ox-latex)
  17640. (let* ((tmpdir (if (featurep 'xemacs)
  17641. (temp-directory)
  17642. temporary-file-directory))
  17643. (texfilebase (make-temp-name
  17644. (expand-file-name "orgtex" tmpdir)))
  17645. (texfile (concat texfilebase ".tex"))
  17646. (dvifile (concat texfilebase ".dvi"))
  17647. (pngfile (concat texfilebase ".png"))
  17648. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17649. ;; This assumes that the display has the same pixel width in
  17650. ;; the horizontal and vertical directions
  17651. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17652. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17653. "Black"))
  17654. (bg (or (plist-get options (if buffer :background :html-background))
  17655. "Transparent")))
  17656. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17657. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17658. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17659. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17660. (let ((latex-header (org-create-formula--latex-header)))
  17661. (with-temp-file texfile
  17662. (insert latex-header)
  17663. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17664. (let ((dir default-directory))
  17665. (ignore-errors
  17666. (cd tmpdir)
  17667. (call-process "latex" nil nil nil texfile))
  17668. (cd dir))
  17669. (if (not (file-exists-p dvifile))
  17670. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17671. (ignore-errors
  17672. (if (featurep 'xemacs)
  17673. (call-process "dvipng" nil nil nil
  17674. "-fg" fg "-bg" bg
  17675. "-T" "tight"
  17676. "-o" pngfile
  17677. dvifile)
  17678. (call-process "dvipng" nil nil nil
  17679. "-fg" fg "-bg" bg
  17680. "-D" dpi
  17681. ;;"-x" scale "-y" scale
  17682. "-T" "tight"
  17683. "-o" pngfile
  17684. dvifile)))
  17685. (if (not (file-exists-p pngfile))
  17686. (if org-format-latex-signal-error
  17687. (error "Failed to create png file from %s" texfile)
  17688. (message "Failed to create png file from %s" texfile)
  17689. nil)
  17690. ;; Use the requested file name and clean up
  17691. (copy-file pngfile tofile 'replace)
  17692. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17693. (if (file-exists-p (concat texfilebase e))
  17694. (delete-file (concat texfilebase e))))
  17695. pngfile))))
  17696. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17697. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17698. "This calls convert, which is included into imagemagick."
  17699. (require 'ox-latex)
  17700. (let* ((tmpdir (if (featurep 'xemacs)
  17701. (temp-directory)
  17702. temporary-file-directory))
  17703. (texfilebase (make-temp-name
  17704. (expand-file-name "orgtex" tmpdir)))
  17705. (texfile (concat texfilebase ".tex"))
  17706. (pdffile (concat texfilebase ".pdf"))
  17707. (pngfile (concat texfilebase ".png"))
  17708. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17709. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17710. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17711. "black"))
  17712. (bg (or (plist-get options (if buffer :background :html-background))
  17713. "white")))
  17714. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17715. (setq fg (org-latex-color-format fg)))
  17716. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17717. (setq bg (org-latex-color-format
  17718. (if (string= bg "Transparent") "white" bg))))
  17719. (let ((latex-header (org-create-formula--latex-header)))
  17720. (with-temp-file texfile
  17721. (insert latex-header)
  17722. (insert "\n\\begin{document}\n"
  17723. "\\definecolor{fg}{rgb}{" fg "}\n"
  17724. "\\definecolor{bg}{rgb}{" bg "}\n"
  17725. "\n\\pagecolor{bg}\n"
  17726. "\n{\\color{fg}\n"
  17727. string
  17728. "\n}\n"
  17729. "\n\\end{document}\n")))
  17730. (org-latex-compile texfile t)
  17731. (if (not (file-exists-p pdffile))
  17732. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17733. (ignore-errors
  17734. (if (featurep 'xemacs)
  17735. (call-process "convert" nil nil nil
  17736. "-density" "96"
  17737. "-trim"
  17738. "-antialias"
  17739. pdffile
  17740. "-quality" "100"
  17741. ;; "-sharpen" "0x1.0"
  17742. pngfile)
  17743. (call-process "convert" nil nil nil
  17744. "-density" dpi
  17745. "-trim"
  17746. "-antialias"
  17747. pdffile
  17748. "-quality" "100"
  17749. ;; "-sharpen" "0x1.0"
  17750. pngfile)))
  17751. (if (not (file-exists-p pngfile))
  17752. (if org-format-latex-signal-error
  17753. (error "Failed to create png file from %s" texfile)
  17754. (message "Failed to create png file from %s" texfile)
  17755. nil)
  17756. ;; Use the requested file name and clean up
  17757. (copy-file pngfile tofile 'replace)
  17758. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17759. (if (file-exists-p (concat texfilebase e))
  17760. (delete-file (concat texfilebase e))))
  17761. pngfile))))
  17762. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17763. "Fill a LaTeX header template TPL.
  17764. In the template, the following place holders will be recognized:
  17765. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17766. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17767. [PACKAGES] \\usepackage statements for PKG
  17768. [NO-PACKAGES] do not include PKG
  17769. [EXTRA] the string EXTRA
  17770. [NO-EXTRA] do not include EXTRA
  17771. For backward compatibility, if both the positive and the negative place
  17772. holder is missing, the positive one (without the \"NO-\") will be
  17773. assumed to be present at the end of the template.
  17774. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17775. EXTRA is a string.
  17776. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17777. (let (rpl (end ""))
  17778. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17779. (setq rpl (if (or (match-end 1) (not def-pkg))
  17780. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17781. tpl (replace-match rpl t t tpl))
  17782. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17783. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17784. (setq rpl (if (or (match-end 1) (not pkg))
  17785. "" (org-latex-packages-to-string pkg snippets-p t))
  17786. tpl (replace-match rpl t t tpl))
  17787. (if pkg (setq end
  17788. (concat end "\n"
  17789. (org-latex-packages-to-string pkg snippets-p)))))
  17790. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17791. (setq rpl (if (or (match-end 1) (not extra))
  17792. "" (concat extra "\n"))
  17793. tpl (replace-match rpl t t tpl))
  17794. (if (and extra (string-match "\\S-" extra))
  17795. (setq end (concat end "\n" extra))))
  17796. (if (string-match "\\S-" end)
  17797. (concat tpl "\n" end)
  17798. tpl)))
  17799. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17800. "Turn an alist of packages into a string with the \\usepackage macros."
  17801. (setq pkg (mapconcat (lambda(p)
  17802. (cond
  17803. ((stringp p) p)
  17804. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17805. (format "%% Package %s omitted" (cadr p)))
  17806. ((equal "" (car p))
  17807. (format "\\usepackage{%s}" (cadr p)))
  17808. (t
  17809. (format "\\usepackage[%s]{%s}"
  17810. (car p) (cadr p)))))
  17811. pkg
  17812. "\n"))
  17813. (if newline (concat pkg "\n") pkg))
  17814. (defun org-dvipng-color (attr)
  17815. "Return a RGB color specification for dvipng."
  17816. (apply 'format "rgb %s %s %s"
  17817. (mapcar 'org-normalize-color
  17818. (if (featurep 'xemacs)
  17819. (color-rgb-components
  17820. (face-property 'default
  17821. (cond ((eq attr :foreground) 'foreground)
  17822. ((eq attr :background) 'background))))
  17823. (color-values (face-attribute 'default attr nil))))))
  17824. (defun org-dvipng-color-format (color-name)
  17825. "Convert COLOR-NAME to a RGB color value for dvipng."
  17826. (apply 'format "rgb %s %s %s"
  17827. (mapcar 'org-normalize-color
  17828. (color-values color-name))))
  17829. (defun org-latex-color (attr)
  17830. "Return a RGB color for the LaTeX color package."
  17831. (apply 'format "%s,%s,%s"
  17832. (mapcar 'org-normalize-color
  17833. (if (featurep 'xemacs)
  17834. (color-rgb-components
  17835. (face-property 'default
  17836. (cond ((eq attr :foreground) 'foreground)
  17837. ((eq attr :background) 'background))))
  17838. (color-values (face-attribute 'default attr nil))))))
  17839. (defun org-latex-color-format (color-name)
  17840. "Convert COLOR-NAME to a RGB color value."
  17841. (apply 'format "%s,%s,%s"
  17842. (mapcar 'org-normalize-color
  17843. (color-values color-name))))
  17844. (defun org-normalize-color (value)
  17845. "Return string to be used as color value for an RGB component."
  17846. (format "%g" (/ value 65535.0)))
  17847. ;; Image display
  17848. (defvar org-inline-image-overlays nil)
  17849. (make-variable-buffer-local 'org-inline-image-overlays)
  17850. (defun org-toggle-inline-images (&optional include-linked)
  17851. "Toggle the display of inline images.
  17852. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17853. (interactive "P")
  17854. (if org-inline-image-overlays
  17855. (progn
  17856. (org-remove-inline-images)
  17857. (when (org-called-interactively-p 'interactive)
  17858. (message "Inline image display turned off")))
  17859. (org-display-inline-images include-linked)
  17860. (when (org-called-interactively-p 'interactive)
  17861. (message (if org-inline-image-overlays
  17862. (format "%d images displayed inline"
  17863. (length org-inline-image-overlays))
  17864. "No images to display inline")))))
  17865. (defun org-redisplay-inline-images ()
  17866. "Refresh the display of inline images."
  17867. (interactive)
  17868. (if (not org-inline-image-overlays)
  17869. (org-toggle-inline-images)
  17870. (org-toggle-inline-images)
  17871. (org-toggle-inline-images)))
  17872. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17873. "Display inline images.
  17874. An inline image is a link which follows either of these
  17875. conventions:
  17876. 1. Its path is a file with an extension matching return value
  17877. from `image-file-name-regexp' and it has no contents.
  17878. 2. Its description consists in a single link of the previous
  17879. type.
  17880. When optional argument INCLUDE-LINKED is non-nil, also links with
  17881. a text description part will be inlined. This can be nice for
  17882. a quick look at those images, but it does not reflect what
  17883. exported files will look like.
  17884. When optional argument REFRESH is non-nil, refresh existing
  17885. images between BEG and END. This will create new image displays
  17886. only if necessary. BEG and END default to the buffer
  17887. boundaries."
  17888. (interactive "P")
  17889. (when (display-graphic-p)
  17890. (unless refresh
  17891. (org-remove-inline-images)
  17892. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17893. (org-with-wide-buffer
  17894. (goto-char (or beg (point-min)))
  17895. (let ((case-fold-search t)
  17896. (file-extension-re (org-image-file-name-regexp)))
  17897. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17898. (let ((link (save-match-data (org-element-context))))
  17899. ;; Check if we're at an inline image.
  17900. (when (and (equal (org-element-property :type link) "file")
  17901. (or include-linked
  17902. (not (org-element-property :contents-begin link)))
  17903. (let ((parent (org-element-property :parent link)))
  17904. (or (not (eq (org-element-type parent) 'link))
  17905. (not (cdr (org-element-contents parent)))))
  17906. (org-string-match-p file-extension-re
  17907. (org-element-property :path link)))
  17908. (let ((file (expand-file-name (org-element-property :path link))))
  17909. (when (file-exists-p file)
  17910. (let ((width
  17911. ;; Apply `org-image-actual-width' specifications.
  17912. (cond
  17913. ((not (image-type-available-p 'imagemagick)) nil)
  17914. ((eq org-image-actual-width t) nil)
  17915. ((listp org-image-actual-width)
  17916. (or
  17917. ;; First try to find a width among
  17918. ;; attributes associated to the paragraph
  17919. ;; containing link.
  17920. (let ((paragraph
  17921. (let ((e link))
  17922. (while (and (setq e (org-element-property
  17923. :parent e))
  17924. (not (eq (org-element-type e)
  17925. 'paragraph))))
  17926. e)))
  17927. (when paragraph
  17928. (save-excursion
  17929. (goto-char (org-element-property :begin paragraph))
  17930. (when
  17931. (re-search-forward
  17932. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17933. (org-element-property
  17934. :post-affiliated paragraph)
  17935. t)
  17936. (string-to-number (match-string 1))))))
  17937. ;; Otherwise, fall-back to provided number.
  17938. (car org-image-actual-width)))
  17939. ((numberp org-image-actual-width)
  17940. org-image-actual-width)))
  17941. (old (get-char-property-and-overlay
  17942. (org-element-property :begin link)
  17943. 'org-image-overlay)))
  17944. (if (and (car-safe old) refresh)
  17945. (image-refresh (overlay-get (cdr old) 'display))
  17946. (let ((image (create-image file
  17947. (and width 'imagemagick)
  17948. nil
  17949. :width width)))
  17950. (when image
  17951. (let* ((link
  17952. ;; If inline image is the description
  17953. ;; of another link, be sure to
  17954. ;; consider the latter as the one to
  17955. ;; apply the overlay on.
  17956. (let ((parent
  17957. (org-element-property :parent link)))
  17958. (if (eq (org-element-type parent) 'link)
  17959. parent
  17960. link)))
  17961. (ov (make-overlay
  17962. (org-element-property :begin link)
  17963. (progn
  17964. (goto-char
  17965. (org-element-property :end link))
  17966. (skip-chars-backward " \t")
  17967. (point)))))
  17968. (overlay-put ov 'display image)
  17969. (overlay-put ov 'face 'default)
  17970. (overlay-put ov 'org-image-overlay t)
  17971. (overlay-put
  17972. ov 'modification-hooks
  17973. (list 'org-display-inline-remove-overlay))
  17974. (push ov org-inline-image-overlays)))))))))))))))
  17975. (define-obsolete-function-alias
  17976. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17977. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17978. "Remove inline-display overlay if a corresponding region is modified."
  17979. (let ((inhibit-modification-hooks t))
  17980. (when (and ov after)
  17981. (delete ov org-inline-image-overlays)
  17982. (delete-overlay ov))))
  17983. (defun org-remove-inline-images ()
  17984. "Remove inline display of images."
  17985. (interactive)
  17986. (mapc 'delete-overlay org-inline-image-overlays)
  17987. (setq org-inline-image-overlays nil))
  17988. ;;;; Key bindings
  17989. ;; Outline functions from `outline-mode-prefix-map'
  17990. ;; that can be remapped in Org:
  17991. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17992. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17993. (define-key org-mode-map [remap outline-forward-same-level]
  17994. 'org-forward-heading-same-level)
  17995. (define-key org-mode-map [remap outline-backward-same-level]
  17996. 'org-backward-heading-same-level)
  17997. (define-key org-mode-map [remap show-branches]
  17998. 'org-kill-note-or-show-branches)
  17999. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18000. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18001. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18002. (define-key org-mode-map [remap outline-next-visible-heading]
  18003. 'org-next-visible-heading)
  18004. (define-key org-mode-map [remap outline-previous-visible-heading]
  18005. 'org-previous-visible-heading)
  18006. ;; Outline functions from `outline-mode-prefix-map' that can not
  18007. ;; be remapped in Org:
  18008. ;; - the column "key binding" shows whether the Outline function is still
  18009. ;; available in Org mode on the same key that it has been bound to in
  18010. ;; Outline mode:
  18011. ;; - "overridden": key used for a different functionality in Org mode
  18012. ;; - else: key still bound to the same Outline function in Org mode
  18013. ;; | Outline function | key binding | Org replacement |
  18014. ;; |------------------------------------+-------------+--------------------------|
  18015. ;; | `outline-next-visible-heading' | `C-c C-n' | better: skip inlinetasks |
  18016. ;; | `outline-previous-visible-heading' | `C-c C-p' | better: skip inlinetasks |
  18017. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18018. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18019. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18020. ;; | `show-entry' | overridden | no replacement |
  18021. ;; | `show-children' | `C-c C-i' | visibility cycling |
  18022. ;; | `show-branches' | `C-c C-k' | still same function |
  18023. ;; | `show-subtree' | overridden | visibility cycling |
  18024. ;; | `show-all' | overridden | no replacement |
  18025. ;; | `hide-subtree' | overridden | visibility cycling |
  18026. ;; | `hide-body' | overridden | no replacement |
  18027. ;; | `hide-entry' | overridden | visibility cycling |
  18028. ;; | `hide-leaves' | overridden | no replacement |
  18029. ;; | `hide-sublevels' | overridden | no replacement |
  18030. ;; | `hide-other' | overridden | no replacement |
  18031. ;; Make `C-c C-x' a prefix key
  18032. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18033. ;; TAB key with modifiers
  18034. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18035. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18036. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18037. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  18038. ;; The following line is necessary under Suse GNU/Linux
  18039. (unless (featurep 'xemacs)
  18040. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18041. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18042. (define-key org-mode-map [backtab] 'org-shifttab)
  18043. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18044. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18045. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18046. ;; Cursor keys with modifiers
  18047. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18048. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18049. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18050. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18051. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18052. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18053. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18054. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18055. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18056. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18057. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18058. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18059. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18060. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18061. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18062. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18063. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18064. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18065. ;; Babel keys
  18066. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18067. (mapc (lambda (pair)
  18068. (define-key org-babel-map (car pair) (cdr pair)))
  18069. org-babel-key-bindings)
  18070. ;;; Extra keys for tty access.
  18071. ;; We only set them when really needed because otherwise the
  18072. ;; menus don't show the simple keys
  18073. (when (or org-use-extra-keys
  18074. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18075. (not window-system))
  18076. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18077. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18078. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18079. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18080. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18081. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18082. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18083. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18084. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18085. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18086. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18087. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18088. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18089. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18090. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18091. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18092. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18093. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18094. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18095. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18096. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18097. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18098. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  18099. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18100. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18101. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18102. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18103. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18104. ;; All the other keys
  18105. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18106. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18107. (if (boundp 'narrow-map)
  18108. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18109. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18110. (if (boundp 'narrow-map)
  18111. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18112. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18113. (if (boundp 'narrow-map)
  18114. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18115. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18116. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18117. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18118. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18119. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18120. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18121. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18122. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18123. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18124. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18125. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18126. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  18127. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18128. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18129. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18130. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18131. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18132. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18133. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18134. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18135. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18136. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18137. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18138. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18139. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18140. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18141. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18142. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18143. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18144. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18145. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18146. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18147. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18148. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18149. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18150. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18151. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18152. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18153. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18154. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18155. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18156. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18157. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18158. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18159. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18160. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18161. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18162. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18163. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18164. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18165. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18166. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18167. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18168. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18169. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18170. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18171. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18172. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18173. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18174. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18175. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18176. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18177. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18178. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18179. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18180. (org-defkey org-mode-map "\C-m" 'org-return)
  18181. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18182. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18183. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18184. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18185. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18186. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18187. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18188. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18189. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18190. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18191. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18192. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18193. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18194. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18195. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18196. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18197. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18198. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18199. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18200. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18201. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18202. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18203. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18204. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18205. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18206. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18207. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18208. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18209. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18210. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18211. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18212. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18213. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18214. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18215. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18216. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18217. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18218. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18219. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18220. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18221. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18222. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18223. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18224. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18225. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18226. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18227. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18228. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18229. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18230. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18231. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  18232. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18233. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18234. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18235. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18236. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18237. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18238. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18239. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18240. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18241. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18242. (when (featurep 'xemacs)
  18243. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18244. (defconst org-speed-commands-default
  18245. '(
  18246. ("Outline Navigation")
  18247. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18248. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18249. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18250. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18251. ("F" . org-next-block)
  18252. ("B" . org-previous-block)
  18253. ("u" . (org-speed-move-safe 'outline-up-heading))
  18254. ("j" . org-goto)
  18255. ("g" . (org-refile t))
  18256. ("Outline Visibility")
  18257. ("c" . org-cycle)
  18258. ("C" . org-shifttab)
  18259. (" " . org-display-outline-path)
  18260. ("s" . org-narrow-to-subtree)
  18261. ("=" . org-columns)
  18262. ("Outline Structure Editing")
  18263. ("U" . org-metaup)
  18264. ("D" . org-metadown)
  18265. ("r" . org-metaright)
  18266. ("l" . org-metaleft)
  18267. ("R" . org-shiftmetaright)
  18268. ("L" . org-shiftmetaleft)
  18269. ("i" . (progn (forward-char 1) (call-interactively
  18270. 'org-insert-heading-respect-content)))
  18271. ("^" . org-sort)
  18272. ("w" . org-refile)
  18273. ("a" . org-archive-subtree-default-with-confirmation)
  18274. ("@" . org-mark-subtree)
  18275. ("#" . org-toggle-comment)
  18276. ("Clock Commands")
  18277. ("I" . org-clock-in)
  18278. ("O" . org-clock-out)
  18279. ("Meta Data Editing")
  18280. ("t" . org-todo)
  18281. ("," . (org-priority))
  18282. ("0" . (org-priority ?\ ))
  18283. ("1" . (org-priority ?A))
  18284. ("2" . (org-priority ?B))
  18285. ("3" . (org-priority ?C))
  18286. (":" . org-set-tags-command)
  18287. ("e" . org-set-effort)
  18288. ("E" . org-inc-effort)
  18289. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18290. (org-entry-put (point) "APPT_WARNTIME" m)))
  18291. ("Agenda Views etc")
  18292. ("v" . org-agenda)
  18293. ("/" . org-sparse-tree)
  18294. ("Misc")
  18295. ("o" . org-open-at-point)
  18296. ("?" . org-speed-command-help)
  18297. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18298. (">" . (org-agenda-remove-restriction-lock))
  18299. )
  18300. "The default speed commands.")
  18301. (defun org-print-speed-command (e)
  18302. (if (> (length (car e)) 1)
  18303. (progn
  18304. (princ "\n")
  18305. (princ (car e))
  18306. (princ "\n")
  18307. (princ (make-string (length (car e)) ?-))
  18308. (princ "\n"))
  18309. (princ (car e))
  18310. (princ " ")
  18311. (if (symbolp (cdr e))
  18312. (princ (symbol-name (cdr e)))
  18313. (prin1 (cdr e)))
  18314. (princ "\n")))
  18315. (defun org-speed-command-help ()
  18316. "Show the available speed commands."
  18317. (interactive)
  18318. (if (not org-use-speed-commands)
  18319. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18320. (with-output-to-temp-buffer "*Help*"
  18321. (princ "User-defined Speed commands\n===========================\n")
  18322. (mapc 'org-print-speed-command org-speed-commands-user)
  18323. (princ "\n")
  18324. (princ "Built-in Speed commands\n=======================\n")
  18325. (mapc 'org-print-speed-command org-speed-commands-default))
  18326. (with-current-buffer "*Help*"
  18327. (setq truncate-lines t))))
  18328. (defun org-speed-move-safe (cmd)
  18329. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18330. If not, return to the original position and throw an error."
  18331. (interactive)
  18332. (let ((pos (point)))
  18333. (call-interactively cmd)
  18334. (unless (and (bolp) (org-at-heading-p))
  18335. (goto-char pos)
  18336. (error "Boundary reached while executing %s" cmd))))
  18337. (defvar org-self-insert-command-undo-counter 0)
  18338. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18339. (defvar org-speed-command nil)
  18340. (define-obsolete-function-alias
  18341. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18342. (defun org-speed-command-activate (keys)
  18343. "Hook for activating single-letter speed commands.
  18344. `org-speed-commands-default' specifies a minimal command set.
  18345. Use `org-speed-commands-user' for further customization."
  18346. (when (or (and (bolp) (looking-at org-outline-regexp))
  18347. (and (functionp org-use-speed-commands)
  18348. (funcall org-use-speed-commands)))
  18349. (cdr (assoc keys (append org-speed-commands-user
  18350. org-speed-commands-default)))))
  18351. (define-obsolete-function-alias
  18352. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18353. (defun org-babel-speed-command-activate (keys)
  18354. "Hook for activating single-letter code block commands."
  18355. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18356. (cdr (assoc keys org-babel-key-bindings))))
  18357. (defcustom org-speed-command-hook
  18358. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18359. "Hook for activating speed commands at strategic locations.
  18360. Hook functions are called in sequence until a valid handler is
  18361. found.
  18362. Each hook takes a single argument, a user-pressed command key
  18363. which is also a `self-insert-command' from the global map.
  18364. Within the hook, examine the cursor position and the command key
  18365. and return nil or a valid handler as appropriate. Handler could
  18366. be one of an interactive command, a function, or a form.
  18367. Set `org-use-speed-commands' to non-nil value to enable this
  18368. hook. The default setting is `org-speed-command-activate'."
  18369. :group 'org-structure
  18370. :version "24.1"
  18371. :type 'hook)
  18372. (defun org-self-insert-command (N)
  18373. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18374. If the cursor is in a table looking at whitespace, the whitespace is
  18375. overwritten, and the table is not marked as requiring realignment."
  18376. (interactive "p")
  18377. (org-check-before-invisible-edit 'insert)
  18378. (cond
  18379. ((and org-use-speed-commands
  18380. (let ((kv (this-command-keys-vector)))
  18381. (setq org-speed-command
  18382. (run-hook-with-args-until-success
  18383. 'org-speed-command-hook
  18384. (make-string 1 (aref kv (1- (length kv))))))))
  18385. (cond
  18386. ((commandp org-speed-command)
  18387. (setq this-command org-speed-command)
  18388. (call-interactively org-speed-command))
  18389. ((functionp org-speed-command)
  18390. (funcall org-speed-command))
  18391. ((and org-speed-command (listp org-speed-command))
  18392. (eval org-speed-command))
  18393. (t (let (org-use-speed-commands)
  18394. (call-interactively 'org-self-insert-command)))))
  18395. ((and
  18396. (org-table-p)
  18397. (progn
  18398. ;; check if we blank the field, and if that triggers align
  18399. (and (featurep 'org-table) org-table-auto-blank-field
  18400. (memq last-command
  18401. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18402. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18403. ;; got extra space, this field does not determine column width
  18404. (let (org-table-may-need-update) (org-table-blank-field))
  18405. ;; no extra space, this field may determine column width
  18406. (org-table-blank-field)))
  18407. t)
  18408. (eq N 1)
  18409. (looking-at "[^|\n]* |"))
  18410. (let (org-table-may-need-update)
  18411. (goto-char (1- (match-end 0)))
  18412. (backward-delete-char 1)
  18413. (goto-char (match-beginning 0))
  18414. (self-insert-command N)))
  18415. (t
  18416. (setq org-table-may-need-update t)
  18417. (self-insert-command N)
  18418. (org-fix-tags-on-the-fly)
  18419. (if org-self-insert-cluster-for-undo
  18420. (if (not (eq last-command 'org-self-insert-command))
  18421. (setq org-self-insert-command-undo-counter 1)
  18422. (if (>= org-self-insert-command-undo-counter 20)
  18423. (setq org-self-insert-command-undo-counter 1)
  18424. (and (> org-self-insert-command-undo-counter 0)
  18425. buffer-undo-list (listp buffer-undo-list)
  18426. (not (cadr buffer-undo-list)) ; remove nil entry
  18427. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18428. (setq org-self-insert-command-undo-counter
  18429. (1+ org-self-insert-command-undo-counter))))))))
  18430. (defun org-check-before-invisible-edit (kind)
  18431. "Check is editing if kind KIND would be dangerous with invisible text around.
  18432. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18433. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18434. (if (and org-catch-invisible-edits
  18435. (or (not (boundp 'visible-mode)) (not visible-mode))
  18436. (or (get-char-property (point) 'invisible)
  18437. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18438. ;; OK, we need to take a closer look
  18439. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18440. (invisible-before-point (if (bobp) nil (get-char-property
  18441. (1- (point)) 'invisible)))
  18442. (border-and-ok-direction
  18443. (or
  18444. ;; Check if we are acting predictably before invisible text
  18445. (and invisible-at-point (not invisible-before-point)
  18446. (memq kind '(insert delete-backward)))
  18447. ;; Check if we are acting predictably after invisible text
  18448. ;; This works not well, and I have turned it off. It seems
  18449. ;; better to always show and stop after invisible text.
  18450. ;; (and (not invisible-at-point) invisible-before-point
  18451. ;; (memq kind '(insert delete)))
  18452. )))
  18453. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18454. (memq invisible-before-point '(outline org-hide-block t)))
  18455. (if (eq org-catch-invisible-edits 'error)
  18456. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18457. (if (and org-custom-properties-overlays
  18458. (y-or-n-p "Display invisible properties in this buffer? "))
  18459. (org-toggle-custom-properties-visibility)
  18460. ;; Make the area visible
  18461. (save-excursion
  18462. (if invisible-before-point
  18463. (goto-char (previous-single-char-property-change
  18464. (point) 'invisible)))
  18465. (show-subtree))
  18466. (cond
  18467. ((eq org-catch-invisible-edits 'show)
  18468. ;; That's it, we do the edit after showing
  18469. (message
  18470. "Unfolding invisible region around point before editing")
  18471. (sit-for 1))
  18472. ((and (eq org-catch-invisible-edits 'smart)
  18473. border-and-ok-direction)
  18474. (message "Unfolding invisible region around point before editing"))
  18475. (t
  18476. ;; Don't do the edit, make the user repeat it in full visibility
  18477. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18478. (defun org-fix-tags-on-the-fly ()
  18479. (when (and (equal (char-after (point-at-bol)) ?*)
  18480. (org-at-heading-p))
  18481. (org-align-tags-here org-tags-column)))
  18482. (defun org-delete-backward-char (N)
  18483. "Like `delete-backward-char', insert whitespace at field end in tables.
  18484. When deleting backwards, in tables this function will insert whitespace in
  18485. front of the next \"|\" separator, to keep the table aligned. The table will
  18486. still be marked for re-alignment if the field did fill the entire column,
  18487. because, in this case the deletion might narrow the column."
  18488. (interactive "p")
  18489. (save-match-data
  18490. (org-check-before-invisible-edit 'delete-backward)
  18491. (if (and (org-table-p)
  18492. (eq N 1)
  18493. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18494. (looking-at ".*?|"))
  18495. (let ((pos (point))
  18496. (noalign (looking-at "[^|\n\r]* |"))
  18497. (c org-table-may-need-update))
  18498. (backward-delete-char N)
  18499. (if (not overwrite-mode)
  18500. (progn
  18501. (skip-chars-forward "^|")
  18502. (insert " ")
  18503. (goto-char (1- pos))))
  18504. ;; noalign: if there were two spaces at the end, this field
  18505. ;; does not determine the width of the column.
  18506. (if noalign (setq org-table-may-need-update c)))
  18507. (backward-delete-char N)
  18508. (org-fix-tags-on-the-fly))))
  18509. (defun org-delete-char (N)
  18510. "Like `delete-char', but insert whitespace at field end in tables.
  18511. When deleting characters, in tables this function will insert whitespace in
  18512. front of the next \"|\" separator, to keep the table aligned. The table will
  18513. still be marked for re-alignment if the field did fill the entire column,
  18514. because, in this case the deletion might narrow the column."
  18515. (interactive "p")
  18516. (save-match-data
  18517. (org-check-before-invisible-edit 'delete)
  18518. (if (and (org-table-p)
  18519. (not (bolp))
  18520. (not (= (char-after) ?|))
  18521. (eq N 1))
  18522. (if (looking-at ".*?|")
  18523. (let ((pos (point))
  18524. (noalign (looking-at "[^|\n\r]* |"))
  18525. (c org-table-may-need-update))
  18526. (replace-match
  18527. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18528. (goto-char pos)
  18529. ;; noalign: if there were two spaces at the end, this field
  18530. ;; does not determine the width of the column.
  18531. (if noalign (setq org-table-may-need-update c)))
  18532. (delete-char N))
  18533. (delete-char N)
  18534. (org-fix-tags-on-the-fly))))
  18535. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18536. (put 'org-self-insert-command 'delete-selection
  18537. (lambda ()
  18538. (not (run-hook-with-args-until-success
  18539. 'self-insert-uses-region-functions))))
  18540. (put 'orgtbl-self-insert-command 'delete-selection
  18541. (lambda ()
  18542. (not (run-hook-with-args-until-success
  18543. 'self-insert-uses-region-functions))))
  18544. (put 'org-delete-char 'delete-selection 'supersede)
  18545. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18546. (put 'org-yank 'delete-selection 'yank)
  18547. ;; Make `flyspell-mode' delay after some commands
  18548. (put 'org-self-insert-command 'flyspell-delayed t)
  18549. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18550. (put 'org-delete-char 'flyspell-delayed t)
  18551. (put 'org-delete-backward-char 'flyspell-delayed t)
  18552. ;; Make pabbrev-mode expand after org-mode commands
  18553. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18554. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18555. (defun org-remap (map &rest commands)
  18556. "In MAP, remap the functions given in COMMANDS.
  18557. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18558. (let (new old)
  18559. (while commands
  18560. (setq old (pop commands) new (pop commands))
  18561. (if (fboundp 'command-remapping)
  18562. (org-defkey map (vector 'remap old) new)
  18563. (substitute-key-definition old new map global-map)))))
  18564. (defun org-transpose-words ()
  18565. "Transpose words for Org.
  18566. This uses the `org-mode-transpose-word-syntax-table' syntax
  18567. table, which interprets characters in `org-emphasis-alist' as
  18568. word constituents."
  18569. (interactive)
  18570. (with-syntax-table org-mode-transpose-word-syntax-table
  18571. (call-interactively 'transpose-words)))
  18572. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18573. (when (eq org-enable-table-editor 'optimized)
  18574. ;; If the user wants maximum table support, we need to hijack
  18575. ;; some standard editing functions
  18576. (org-remap org-mode-map
  18577. 'self-insert-command 'org-self-insert-command
  18578. 'delete-char 'org-delete-char
  18579. 'delete-backward-char 'org-delete-backward-char)
  18580. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18581. (defvar org-ctrl-c-ctrl-c-hook nil
  18582. "Hook for functions attaching themselves to `C-c C-c'.
  18583. This can be used to add additional functionality to the C-c C-c
  18584. key which executes context-dependent commands. This hook is run
  18585. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18586. run after the last test.
  18587. Each function will be called with no arguments. The function
  18588. must check if the context is appropriate for it to act. If yes,
  18589. it should do its thing and then return a non-nil value. If the
  18590. context is wrong, just do nothing and return nil.")
  18591. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18592. "Hook for functions attaching themselves to `C-c C-c'.
  18593. This can be used to add additional functionality to the C-c C-c
  18594. key which executes context-dependent commands. This hook is run
  18595. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18596. before the first test.
  18597. Each function will be called with no arguments. The function
  18598. must check if the context is appropriate for it to act. If yes,
  18599. it should do its thing and then return a non-nil value. If the
  18600. context is wrong, just do nothing and return nil.")
  18601. (defvar org-tab-first-hook nil
  18602. "Hook for functions to attach themselves to TAB.
  18603. See `org-ctrl-c-ctrl-c-hook' for more information.
  18604. This hook runs as the first action when TAB is pressed, even before
  18605. `org-cycle' messes around with the `outline-regexp' to cater for
  18606. inline tasks and plain list item folding.
  18607. If any function in this hook returns t, any other actions that
  18608. would have been caused by TAB (such as table field motion or visibility
  18609. cycling) will not occur.")
  18610. (defvar org-tab-after-check-for-table-hook nil
  18611. "Hook for functions to attach themselves to TAB.
  18612. See `org-ctrl-c-ctrl-c-hook' for more information.
  18613. This hook runs after it has been established that the cursor is not in a
  18614. table, but before checking if the cursor is in a headline or if global cycling
  18615. should be done.
  18616. If any function in this hook returns t, not other actions like visibility
  18617. cycling will be done.")
  18618. (defvar org-tab-after-check-for-cycling-hook nil
  18619. "Hook for functions to attach themselves to TAB.
  18620. See `org-ctrl-c-ctrl-c-hook' for more information.
  18621. This hook runs after it has been established that not table field motion and
  18622. not visibility should be done because of current context. This is probably
  18623. the place where a package like yasnippets can hook in.")
  18624. (defvar org-tab-before-tab-emulation-hook nil
  18625. "Hook for functions to attach themselves to TAB.
  18626. See `org-ctrl-c-ctrl-c-hook' for more information.
  18627. This hook runs after every other options for TAB have been exhausted, but
  18628. before indentation and \t insertion takes place.")
  18629. (defvar org-metaleft-hook nil
  18630. "Hook for functions attaching themselves to `M-left'.
  18631. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18632. (defvar org-metaright-hook nil
  18633. "Hook for functions attaching themselves to `M-right'.
  18634. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18635. (defvar org-metaup-hook nil
  18636. "Hook for functions attaching themselves to `M-up'.
  18637. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18638. (defvar org-metadown-hook nil
  18639. "Hook for functions attaching themselves to `M-down'.
  18640. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18641. (defvar org-shiftmetaleft-hook nil
  18642. "Hook for functions attaching themselves to `M-S-left'.
  18643. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18644. (defvar org-shiftmetaright-hook nil
  18645. "Hook for functions attaching themselves to `M-S-right'.
  18646. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18647. (defvar org-shiftmetaup-hook nil
  18648. "Hook for functions attaching themselves to `M-S-up'.
  18649. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18650. (defvar org-shiftmetadown-hook nil
  18651. "Hook for functions attaching themselves to `M-S-down'.
  18652. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18653. (defvar org-metareturn-hook nil
  18654. "Hook for functions attaching themselves to `M-RET'.
  18655. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18656. (defvar org-shiftup-hook nil
  18657. "Hook for functions attaching themselves to `S-up'.
  18658. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18659. (defvar org-shiftup-final-hook nil
  18660. "Hook for functions attaching themselves to `S-up'.
  18661. This one runs after all other options except shift-select have been excluded.
  18662. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18663. (defvar org-shiftdown-hook nil
  18664. "Hook for functions attaching themselves to `S-down'.
  18665. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18666. (defvar org-shiftdown-final-hook nil
  18667. "Hook for functions attaching themselves to `S-down'.
  18668. This one runs after all other options except shift-select have been excluded.
  18669. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18670. (defvar org-shiftleft-hook nil
  18671. "Hook for functions attaching themselves to `S-left'.
  18672. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18673. (defvar org-shiftleft-final-hook nil
  18674. "Hook for functions attaching themselves to `S-left'.
  18675. This one runs after all other options except shift-select have been excluded.
  18676. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18677. (defvar org-shiftright-hook nil
  18678. "Hook for functions attaching themselves to `S-right'.
  18679. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18680. (defvar org-shiftright-final-hook nil
  18681. "Hook for functions attaching themselves to `S-right'.
  18682. This one runs after all other options except shift-select have been excluded.
  18683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18684. (defun org-modifier-cursor-error ()
  18685. "Throw an error, a modified cursor command was applied in wrong context."
  18686. (user-error "This command is active in special context like tables, headlines or items"))
  18687. (defun org-shiftselect-error ()
  18688. "Throw an error because Shift-Cursor command was applied in wrong context."
  18689. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18690. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18691. (user-error "This command works only in special context like headlines or timestamps")))
  18692. (defun org-call-for-shift-select (cmd)
  18693. (let ((this-command-keys-shift-translated t))
  18694. (call-interactively cmd)))
  18695. (defun org-shifttab (&optional arg)
  18696. "Global visibility cycling or move to previous table field.
  18697. Call `org-table-previous-field' within a table.
  18698. When ARG is nil, cycle globally through visibility states.
  18699. When ARG is a numeric prefix, show contents of this level."
  18700. (interactive "P")
  18701. (cond
  18702. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18703. ((integerp arg)
  18704. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18705. (message "Content view to level: %d" arg)
  18706. (org-content (prefix-numeric-value arg2))
  18707. (org-cycle-show-empty-lines t)
  18708. (setq org-cycle-global-status 'overview)))
  18709. (t (call-interactively 'org-global-cycle))))
  18710. (defun org-shiftmetaleft ()
  18711. "Promote subtree or delete table column.
  18712. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18713. `org-table-delete-column', depending on context. See the
  18714. individual commands for more information."
  18715. (interactive)
  18716. (cond
  18717. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18718. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18719. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18720. ((if (not (org-region-active-p)) (org-at-item-p)
  18721. (save-excursion (goto-char (region-beginning))
  18722. (org-at-item-p)))
  18723. (call-interactively 'org-outdent-item-tree))
  18724. (t (org-modifier-cursor-error))))
  18725. (defun org-shiftmetaright ()
  18726. "Demote subtree or insert table column.
  18727. Calls `org-demote-subtree', `org-indent-item-tree', or
  18728. `org-table-insert-column', depending on context. See the
  18729. individual commands for more information."
  18730. (interactive)
  18731. (cond
  18732. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18733. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18734. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18735. ((if (not (org-region-active-p)) (org-at-item-p)
  18736. (save-excursion (goto-char (region-beginning))
  18737. (org-at-item-p)))
  18738. (call-interactively 'org-indent-item-tree))
  18739. (t (org-modifier-cursor-error))))
  18740. (defun org-shiftmetaup (&optional arg)
  18741. "Drag the line at point up.
  18742. In a table, kill the current row.
  18743. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18744. but also adjust the previous clocked item in the clock history.
  18745. Everywhere else, drag the line at point up."
  18746. (interactive "P")
  18747. (cond
  18748. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18749. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18750. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18751. (call-interactively 'org-timestamp-up)))
  18752. (t (call-interactively 'org-drag-line-backward))))
  18753. (defun org-shiftmetadown (&optional arg)
  18754. "Drag the line at point down.
  18755. In a table, insert an empty row at the current line.
  18756. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18757. but also adjust the previous clocked item in the clock history.
  18758. Everywhere else, drag the line at point down."
  18759. (interactive "P")
  18760. (cond
  18761. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18762. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18763. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18764. (call-interactively 'org-timestamp-down)))
  18765. (t (call-interactively 'org-drag-line-forward))))
  18766. (defsubst org-hidden-tree-error ()
  18767. (user-error
  18768. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18769. (defun org-metaleft (&optional arg)
  18770. "Promote heading or move table column to left.
  18771. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18772. With no specific context, calls the Emacs default `backward-word'.
  18773. See the individual commands for more information."
  18774. (interactive "P")
  18775. (cond
  18776. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18777. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18778. ((org-with-limited-levels
  18779. (or (org-at-heading-p)
  18780. (and (org-region-active-p)
  18781. (save-excursion
  18782. (goto-char (region-beginning))
  18783. (org-at-heading-p)))))
  18784. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18785. (call-interactively 'org-do-promote))
  18786. ;; At an inline task.
  18787. ((org-at-heading-p)
  18788. (call-interactively 'org-inlinetask-promote))
  18789. ((or (org-at-item-p)
  18790. (and (org-region-active-p)
  18791. (save-excursion
  18792. (goto-char (region-beginning))
  18793. (org-at-item-p))))
  18794. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18795. (call-interactively 'org-outdent-item))
  18796. (t (call-interactively 'backward-word))))
  18797. (defun org-metaright (&optional arg)
  18798. "Demote a subtree, a list item or move table column to right.
  18799. In front of a drawer or a block keyword, indent it correctly.
  18800. With no specific context, calls the Emacs default `forward-word'.
  18801. See the individual commands for more information."
  18802. (interactive "P")
  18803. (cond
  18804. ((run-hook-with-args-until-success 'org-metaright-hook))
  18805. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18806. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18807. ((org-at-block-p) (call-interactively 'org-indent-block))
  18808. ((org-with-limited-levels
  18809. (or (org-at-heading-p)
  18810. (and (org-region-active-p)
  18811. (save-excursion
  18812. (goto-char (region-beginning))
  18813. (org-at-heading-p)))))
  18814. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18815. (call-interactively 'org-do-demote))
  18816. ;; At an inline task.
  18817. ((org-at-heading-p)
  18818. (call-interactively 'org-inlinetask-demote))
  18819. ((or (org-at-item-p)
  18820. (and (org-region-active-p)
  18821. (save-excursion
  18822. (goto-char (region-beginning))
  18823. (org-at-item-p))))
  18824. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18825. (call-interactively 'org-indent-item))
  18826. (t (call-interactively 'forward-word))))
  18827. (defun org-check-for-hidden (what)
  18828. "Check if there are hidden headlines/items in the current visual line.
  18829. WHAT can be either `headlines' or `items'. If the current line is
  18830. an outline or item heading and it has a folded subtree below it,
  18831. this function returns t, nil otherwise."
  18832. (let ((re (cond
  18833. ((eq what 'headlines) org-outline-regexp-bol)
  18834. ((eq what 'items) (org-item-beginning-re))
  18835. (t (error "This should not happen"))))
  18836. beg end)
  18837. (save-excursion
  18838. (catch 'exit
  18839. (unless (org-region-active-p)
  18840. (setq beg (point-at-bol))
  18841. (beginning-of-line 2)
  18842. (while (and (not (eobp)) ;; this is like `next-line'
  18843. (get-char-property (1- (point)) 'invisible))
  18844. (beginning-of-line 2))
  18845. (setq end (point))
  18846. (goto-char beg)
  18847. (goto-char (point-at-eol))
  18848. (setq end (max end (point)))
  18849. (while (re-search-forward re end t)
  18850. (if (get-char-property (match-beginning 0) 'invisible)
  18851. (throw 'exit t))))
  18852. nil))))
  18853. (defun org-metaup (&optional arg)
  18854. "Move subtree up or move table row up.
  18855. Calls `org-move-subtree-up' or `org-table-move-row' or
  18856. `org-move-item-up', depending on context. See the individual commands
  18857. for more information."
  18858. (interactive "P")
  18859. (cond
  18860. ((run-hook-with-args-until-success 'org-metaup-hook))
  18861. ((org-region-active-p)
  18862. (let* ((a (min (region-beginning) (region-end)))
  18863. (b (1- (max (region-beginning) (region-end))))
  18864. (c (save-excursion (goto-char a)
  18865. (move-beginning-of-line 0)))
  18866. (d (save-excursion (goto-char a)
  18867. (move-end-of-line 0) (point))))
  18868. (transpose-regions a b c d)
  18869. (goto-char c)))
  18870. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18871. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18872. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18873. (t (org-drag-element-backward))))
  18874. (defun org-metadown (&optional arg)
  18875. "Move subtree down or move table row down.
  18876. Calls `org-move-subtree-down' or `org-table-move-row' or
  18877. `org-move-item-down', depending on context. See the individual
  18878. commands for more information."
  18879. (interactive "P")
  18880. (cond
  18881. ((run-hook-with-args-until-success 'org-metadown-hook))
  18882. ((org-region-active-p)
  18883. (let* ((a (min (region-beginning) (region-end)))
  18884. (b (max (region-beginning) (region-end)))
  18885. (c (save-excursion (goto-char b)
  18886. (move-beginning-of-line 1)))
  18887. (d (save-excursion (goto-char b)
  18888. (move-end-of-line 1) (1+ (point)))))
  18889. (transpose-regions a b c d)
  18890. (goto-char d)))
  18891. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18892. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18893. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18894. (t (org-drag-element-forward))))
  18895. (defun org-shiftup (&optional arg)
  18896. "Increase item in timestamp or increase priority of current headline.
  18897. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18898. depending on context. See the individual commands for more information."
  18899. (interactive "P")
  18900. (cond
  18901. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18902. ((and org-support-shift-select (org-region-active-p))
  18903. (org-call-for-shift-select 'previous-line))
  18904. ((org-at-timestamp-p t)
  18905. (call-interactively (if org-edit-timestamp-down-means-later
  18906. 'org-timestamp-down 'org-timestamp-up)))
  18907. ((and (not (eq org-support-shift-select 'always))
  18908. org-enable-priority-commands
  18909. (org-at-heading-p))
  18910. (call-interactively 'org-priority-up))
  18911. ((and (not org-support-shift-select) (org-at-item-p))
  18912. (call-interactively 'org-previous-item))
  18913. ((org-clocktable-try-shift 'up arg))
  18914. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18915. (org-support-shift-select
  18916. (org-call-for-shift-select 'previous-line))
  18917. (t (org-shiftselect-error))))
  18918. (defun org-shiftdown (&optional arg)
  18919. "Decrease item in timestamp or decrease priority of current headline.
  18920. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18921. depending on context. See the individual commands for more information."
  18922. (interactive "P")
  18923. (cond
  18924. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18925. ((and org-support-shift-select (org-region-active-p))
  18926. (org-call-for-shift-select 'next-line))
  18927. ((org-at-timestamp-p t)
  18928. (call-interactively (if org-edit-timestamp-down-means-later
  18929. 'org-timestamp-up 'org-timestamp-down)))
  18930. ((and (not (eq org-support-shift-select 'always))
  18931. org-enable-priority-commands
  18932. (org-at-heading-p))
  18933. (call-interactively 'org-priority-down))
  18934. ((and (not org-support-shift-select) (org-at-item-p))
  18935. (call-interactively 'org-next-item))
  18936. ((org-clocktable-try-shift 'down arg))
  18937. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18938. (org-support-shift-select
  18939. (org-call-for-shift-select 'next-line))
  18940. (t (org-shiftselect-error))))
  18941. (defun org-shiftright (&optional arg)
  18942. "Cycle the thing at point or in the current line, depending on context.
  18943. Depending on context, this does one of the following:
  18944. - switch a timestamp at point one day into the future
  18945. - on a headline, switch to the next TODO keyword.
  18946. - on an item, switch entire list to the next bullet type
  18947. - on a property line, switch to the next allowed value
  18948. - on a clocktable definition line, move time block into the future"
  18949. (interactive "P")
  18950. (cond
  18951. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18952. ((and org-support-shift-select (org-region-active-p))
  18953. (org-call-for-shift-select 'forward-char))
  18954. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18955. ((and (not (eq org-support-shift-select 'always))
  18956. (org-at-heading-p))
  18957. (let ((org-inhibit-logging
  18958. (not org-treat-S-cursor-todo-selection-as-state-change))
  18959. (org-inhibit-blocking
  18960. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18961. (org-call-with-arg 'org-todo 'right)))
  18962. ((or (and org-support-shift-select
  18963. (not (eq org-support-shift-select 'always))
  18964. (org-at-item-bullet-p))
  18965. (and (not org-support-shift-select) (org-at-item-p)))
  18966. (org-call-with-arg 'org-cycle-list-bullet nil))
  18967. ((and (not (eq org-support-shift-select 'always))
  18968. (org-at-property-p))
  18969. (call-interactively 'org-property-next-allowed-value))
  18970. ((org-clocktable-try-shift 'right arg))
  18971. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18972. (org-support-shift-select
  18973. (org-call-for-shift-select 'forward-char))
  18974. (t (org-shiftselect-error))))
  18975. (defun org-shiftleft (&optional arg)
  18976. "Cycle the thing at point or in the current line, depending on context.
  18977. Depending on context, this does one of the following:
  18978. - switch a timestamp at point one day into the past
  18979. - on a headline, switch to the previous TODO keyword.
  18980. - on an item, switch entire list to the previous bullet type
  18981. - on a property line, switch to the previous allowed value
  18982. - on a clocktable definition line, move time block into the past"
  18983. (interactive "P")
  18984. (cond
  18985. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18986. ((and org-support-shift-select (org-region-active-p))
  18987. (org-call-for-shift-select 'backward-char))
  18988. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18989. ((and (not (eq org-support-shift-select 'always))
  18990. (org-at-heading-p))
  18991. (let ((org-inhibit-logging
  18992. (not org-treat-S-cursor-todo-selection-as-state-change))
  18993. (org-inhibit-blocking
  18994. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18995. (org-call-with-arg 'org-todo 'left)))
  18996. ((or (and org-support-shift-select
  18997. (not (eq org-support-shift-select 'always))
  18998. (org-at-item-bullet-p))
  18999. (and (not org-support-shift-select) (org-at-item-p)))
  19000. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19001. ((and (not (eq org-support-shift-select 'always))
  19002. (org-at-property-p))
  19003. (call-interactively 'org-property-previous-allowed-value))
  19004. ((org-clocktable-try-shift 'left arg))
  19005. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19006. (org-support-shift-select
  19007. (org-call-for-shift-select 'backward-char))
  19008. (t (org-shiftselect-error))))
  19009. (defun org-shiftcontrolright ()
  19010. "Switch to next TODO set."
  19011. (interactive)
  19012. (cond
  19013. ((and org-support-shift-select (org-region-active-p))
  19014. (org-call-for-shift-select 'forward-word))
  19015. ((and (not (eq org-support-shift-select 'always))
  19016. (org-at-heading-p))
  19017. (org-call-with-arg 'org-todo 'nextset))
  19018. (org-support-shift-select
  19019. (org-call-for-shift-select 'forward-word))
  19020. (t (org-shiftselect-error))))
  19021. (defun org-shiftcontrolleft ()
  19022. "Switch to previous TODO set."
  19023. (interactive)
  19024. (cond
  19025. ((and org-support-shift-select (org-region-active-p))
  19026. (org-call-for-shift-select 'backward-word))
  19027. ((and (not (eq org-support-shift-select 'always))
  19028. (org-at-heading-p))
  19029. (org-call-with-arg 'org-todo 'previousset))
  19030. (org-support-shift-select
  19031. (org-call-for-shift-select 'backward-word))
  19032. (t (org-shiftselect-error))))
  19033. (defun org-shiftcontrolup (&optional n)
  19034. "Change timestamps synchronously up in CLOCK log lines.
  19035. Optional argument N tells to change by that many units."
  19036. (interactive "P")
  19037. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19038. (let (org-support-shift-select)
  19039. (org-clock-timestamps-up n))
  19040. (user-error "Not at a clock log")))
  19041. (defun org-shiftcontroldown (&optional n)
  19042. "Change timestamps synchronously down in CLOCK log lines.
  19043. Optional argument N tells to change by that many units."
  19044. (interactive "P")
  19045. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19046. (let (org-support-shift-select)
  19047. (org-clock-timestamps-down n))
  19048. (user-error "Not at a clock log")))
  19049. (defun org-increase-number-at-point (&optional inc)
  19050. "Increment the number at point.
  19051. With an optional prefix numeric argument INC, increment using
  19052. this numeric value."
  19053. (interactive "p")
  19054. (if (not (number-at-point))
  19055. (user-error "Not on a number")
  19056. (unless inc (setq inc 1))
  19057. (let ((pos (point))
  19058. (beg (skip-chars-backward "-+^/*0-9eE."))
  19059. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19060. (setq nap (buffer-substring-no-properties
  19061. (+ pos beg) (+ pos beg end)))
  19062. (delete-region (+ pos beg) (+ pos beg end))
  19063. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19064. (when (org-at-table-p)
  19065. (org-table-align)
  19066. (org-table-end-of-field 1))))
  19067. (defun org-decrease-number-at-point (&optional inc)
  19068. "Decrement the number at point.
  19069. With an optional prefix numeric argument INC, decrement using
  19070. this numeric value."
  19071. (interactive "p")
  19072. (org-increase-number-at-point (- inc)))
  19073. (defun org-ctrl-c-ret ()
  19074. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19075. (interactive)
  19076. (cond
  19077. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19078. (t (call-interactively 'org-insert-heading))))
  19079. (defun org-find-visible ()
  19080. (let ((s (point)))
  19081. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19082. (get-char-property s 'invisible)))
  19083. s))
  19084. (defun org-find-invisible ()
  19085. (let ((s (point)))
  19086. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19087. (not (get-char-property s 'invisible))))
  19088. s))
  19089. (defun org-copy-visible (beg end)
  19090. "Copy the visible parts of the region."
  19091. (interactive "r")
  19092. (let (snippets s)
  19093. (save-excursion
  19094. (save-restriction
  19095. (narrow-to-region beg end)
  19096. (setq s (goto-char (point-min)))
  19097. (while (not (= (point) (point-max)))
  19098. (goto-char (org-find-invisible))
  19099. (push (buffer-substring s (point)) snippets)
  19100. (setq s (goto-char (org-find-visible))))))
  19101. (kill-new (apply 'concat (nreverse snippets)))))
  19102. (defun org-copy-special ()
  19103. "Copy region in table or copy current subtree.
  19104. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19105. See the individual commands for more information."
  19106. (interactive)
  19107. (call-interactively
  19108. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19109. (defun org-cut-special ()
  19110. "Cut region in table or cut current subtree.
  19111. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19112. See the individual commands for more information."
  19113. (interactive)
  19114. (call-interactively
  19115. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19116. (defun org-paste-special (arg)
  19117. "Paste rectangular region into table, or past subtree relative to level.
  19118. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19119. See the individual commands for more information."
  19120. (interactive "P")
  19121. (if (org-at-table-p)
  19122. (org-table-paste-rectangle)
  19123. (org-paste-subtree arg)))
  19124. (defsubst org-in-fixed-width-region-p ()
  19125. "Is point in a fixed-width region?"
  19126. (save-match-data
  19127. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19128. (defun org-edit-special (&optional arg)
  19129. "Call a special editor for the element at point.
  19130. When at a table, call the formula editor with `org-table-edit-formulas'.
  19131. When in a source code block, call `org-edit-src-code'.
  19132. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19133. When in an export block, call `org-edit-export-block'.
  19134. When at an #+INCLUDE keyword, visit the included file.
  19135. On a link, call `ffap' to visit the link at point.
  19136. Otherwise, return a user error."
  19137. (interactive "P")
  19138. (let ((element (org-element-at-point)))
  19139. (assert (not buffer-read-only) nil
  19140. "Buffer is read-only: %s" (buffer-name))
  19141. (case (org-element-type element)
  19142. (src-block
  19143. (if (not arg) (org-edit-src-code)
  19144. (let* ((info (org-babel-get-src-block-info))
  19145. (lang (nth 0 info))
  19146. (params (nth 2 info))
  19147. (session (cdr (assq :session params))))
  19148. (if (not session) (org-edit-src-code)
  19149. ;; At a src-block with a session and function called with
  19150. ;; an ARG: switch to the buffer related to the inferior
  19151. ;; process.
  19152. (switch-to-buffer
  19153. (funcall (intern (concat "org-babel-prep-session:" lang))
  19154. session params))))))
  19155. (keyword
  19156. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19157. (org-open-link-from-string
  19158. (format "[[%s]]"
  19159. (expand-file-name
  19160. (let ((value (org-element-property :value element)))
  19161. (cond ((not (org-string-nw-p value))
  19162. (user-error "No file to edit"))
  19163. ((string-match "\\`\"\\(.*?\\)\"" value)
  19164. (match-string 1 value))
  19165. ((string-match "\\`[^ \t\"]\\S-*" value)
  19166. (match-string 0 value))
  19167. (t (user-error "No valid file specified")))))))
  19168. (user-error "No special environment to edit here")))
  19169. (table
  19170. (if (eq (org-element-property :type element) 'table.el)
  19171. (org-edit-table.el)
  19172. (call-interactively 'org-table-edit-formulas)))
  19173. ;; Only Org tables contain `table-row' type elements.
  19174. (table-row (call-interactively 'org-table-edit-formulas))
  19175. ((example-block src-block) (org-edit-src-code))
  19176. (export-block (org-edit-export-block))
  19177. (fixed-width (org-edit-fixed-width-region))
  19178. (otherwise
  19179. ;; No notable element at point. Though, we may be at a link,
  19180. ;; which is an object. Thus, scan deeper.
  19181. (if (eq (org-element-type (org-element-context element)) 'link)
  19182. (call-interactively 'ffap)
  19183. (user-error "No special environment to edit here"))))))
  19184. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19185. (defun org-ctrl-c-ctrl-c (&optional arg)
  19186. "Set tags in headline, or update according to changed information at point.
  19187. This command does many different things, depending on context:
  19188. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19189. this is what we do.
  19190. - If the cursor is on a statistics cookie, update it.
  19191. - If the cursor is in a headline, prompt for tags and insert them
  19192. into the current line, aligned to `org-tags-column'. When called
  19193. with prefix arg, realign all tags in the current buffer.
  19194. - If the cursor is in one of the special #+KEYWORD lines, this
  19195. triggers scanning the buffer for these lines and updating the
  19196. information.
  19197. - If the cursor is inside a table, realign the table. This command
  19198. works even if the automatic table editor has been turned off.
  19199. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19200. the entire table.
  19201. - If the cursor is at a footnote reference or definition, jump to
  19202. the corresponding definition or references, respectively.
  19203. - If the cursor is a the beginning of a dynamic block, update it.
  19204. - If the current buffer is a capture buffer, close note and file it.
  19205. - If the cursor is on a <<<target>>>, update radio targets and
  19206. corresponding links in this buffer.
  19207. - If the cursor is on a numbered item in a plain list, renumber the
  19208. ordered list.
  19209. - If the cursor is on a checkbox, toggle it.
  19210. - If the cursor is on a code block, evaluate it. The variable
  19211. `org-confirm-babel-evaluate' can be used to control prompting
  19212. before code block evaluation, by default every code block
  19213. evaluation requires confirmation. Code block evaluation can be
  19214. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19215. (interactive "P")
  19216. (cond
  19217. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19218. org-occur-highlights)
  19219. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19220. (org-remove-occur-highlights)
  19221. (message "Temporary highlights/overlays removed from current buffer"))
  19222. ((and (local-variable-p 'org-finish-function (current-buffer))
  19223. (fboundp org-finish-function))
  19224. (funcall org-finish-function))
  19225. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19226. (t
  19227. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19228. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19229. (user-error "C-c C-c can do nothing useful at this location"))
  19230. (let* ((context (org-element-context))
  19231. (type (org-element-type context)))
  19232. (case type
  19233. ;; When at a link, act according to the parent instead.
  19234. (link (setq context (org-element-property :parent context))
  19235. (setq type (org-element-type context)))
  19236. ;; Unsupported object types: refer to the first supported
  19237. ;; element or object containing it.
  19238. ((bold code entity export-snippet inline-babel-call inline-src-block
  19239. italic latex-fragment line-break macro strike-through subscript
  19240. superscript underline verbatim)
  19241. (setq context
  19242. (org-element-lineage
  19243. context '(radio-target paragraph verse-block table-cell)))))
  19244. ;; For convenience: at the first line of a paragraph on the
  19245. ;; same line as an item, apply function on that item instead.
  19246. (when (eq type 'paragraph)
  19247. (let ((parent (org-element-property :parent context)))
  19248. (when (and (eq (org-element-type parent) 'item)
  19249. (= (line-beginning-position)
  19250. (org-element-property :begin parent)))
  19251. (setq context parent type 'item))))
  19252. ;; Act according to type of element or object at point.
  19253. (case type
  19254. (clock (org-clock-update-time-maybe))
  19255. (dynamic-block
  19256. (save-excursion
  19257. (goto-char (org-element-property :post-affiliated context))
  19258. (org-update-dblock)))
  19259. (footnote-definition
  19260. (goto-char (org-element-property :post-affiliated context))
  19261. (call-interactively 'org-footnote-action))
  19262. (footnote-reference (call-interactively 'org-footnote-action))
  19263. ((headline inlinetask)
  19264. (save-excursion (goto-char (org-element-property :begin context))
  19265. (call-interactively 'org-set-tags)))
  19266. (item
  19267. ;; At an item: a double C-u set checkbox to "[-]"
  19268. ;; unconditionally, whereas a single one will toggle its
  19269. ;; presence. Without an universal argument, if the item
  19270. ;; has a checkbox, toggle it. Otherwise repair the list.
  19271. (let* ((box (org-element-property :checkbox context))
  19272. (struct (org-element-property :structure context))
  19273. (old-struct (copy-tree struct))
  19274. (parents (org-list-parents-alist struct))
  19275. (prevs (org-list-prevs-alist struct))
  19276. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19277. (org-list-set-checkbox
  19278. (org-element-property :begin context) struct
  19279. (cond ((equal arg '(16)) "[-]")
  19280. ((and (not box) (equal arg '(4))) "[ ]")
  19281. ((or (not box) (equal arg '(4))) nil)
  19282. ((eq box 'on) "[ ]")
  19283. (t "[X]")))
  19284. ;; Mimic `org-list-write-struct' but with grabbing
  19285. ;; a return value from `org-list-struct-fix-box'.
  19286. (org-list-struct-fix-ind struct parents 2)
  19287. (org-list-struct-fix-item-end struct)
  19288. (org-list-struct-fix-bul struct prevs)
  19289. (org-list-struct-fix-ind struct parents)
  19290. (let ((block-item
  19291. (org-list-struct-fix-box struct parents prevs orderedp)))
  19292. (if (and box (equal struct old-struct))
  19293. (if (equal arg '(16))
  19294. (message "Checkboxes already reset")
  19295. (user-error "Cannot toggle this checkbox: %s"
  19296. (if (eq box 'on)
  19297. "all subitems checked"
  19298. "unchecked subitems")))
  19299. (org-list-struct-apply-struct struct old-struct)
  19300. (org-update-checkbox-count-maybe))
  19301. (when block-item
  19302. (message "Checkboxes were removed due to empty box at line %d"
  19303. (org-current-line block-item))))))
  19304. (keyword
  19305. (let ((org-inhibit-startup-visibility-stuff t)
  19306. (org-startup-align-all-tables nil))
  19307. (when (boundp 'org-table-coordinate-overlays)
  19308. (mapc 'delete-overlay org-table-coordinate-overlays)
  19309. (setq org-table-coordinate-overlays nil))
  19310. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19311. (message "Local setup has been refreshed"))
  19312. (plain-list
  19313. ;; At a plain list, with a double C-u argument, set
  19314. ;; checkboxes of each item to "[-]", whereas a single one
  19315. ;; will toggle their presence according to the state of the
  19316. ;; first item in the list. Without an argument, repair the
  19317. ;; list.
  19318. (let* ((begin (org-element-property :contents-begin context))
  19319. (beginm (move-marker (make-marker) begin))
  19320. (struct (org-element-property :structure context))
  19321. (old-struct (copy-tree struct))
  19322. (first-box (save-excursion
  19323. (goto-char begin)
  19324. (looking-at org-list-full-item-re)
  19325. (match-string-no-properties 3)))
  19326. (new-box (cond ((equal arg '(16)) "[-]")
  19327. ((equal arg '(4)) (unless first-box "[ ]"))
  19328. ((equal first-box "[X]") "[ ]")
  19329. (t "[X]"))))
  19330. (cond
  19331. (arg
  19332. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19333. (org-list-get-all-items
  19334. begin struct (org-list-prevs-alist struct))))
  19335. ((and first-box (eq (point) begin))
  19336. ;; For convenience, when point is at bol on the first
  19337. ;; item of the list and no argument is provided, simply
  19338. ;; toggle checkbox of that item, if any.
  19339. (org-list-set-checkbox begin struct new-box)))
  19340. (org-list-write-struct
  19341. struct (org-list-parents-alist struct) old-struct)
  19342. (org-update-checkbox-count-maybe)
  19343. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19344. ((property-drawer node-property)
  19345. (call-interactively 'org-property-action))
  19346. ((radio-target target)
  19347. (call-interactively 'org-update-radio-target-regexp))
  19348. (statistics-cookie
  19349. (call-interactively 'org-update-statistics-cookies))
  19350. ((table table-cell table-row)
  19351. ;; At a table, recalculate every field and align it. Also
  19352. ;; send the table if necessary. If the table has
  19353. ;; a `table.el' type, just give up. At a table row or
  19354. ;; cell, maybe recalculate line but always align table.
  19355. (if (eq (org-element-property :type context) 'table.el)
  19356. (message "Use C-c ' to edit table.el tables")
  19357. (let ((org-enable-table-editor t))
  19358. (if (or (eq type 'table)
  19359. ;; Check if point is at a TBLFM line.
  19360. (and (eq type 'table-row)
  19361. (= (point) (org-element-property :end context))))
  19362. (save-excursion
  19363. (if (org-at-TBLFM-p)
  19364. (progn (require 'org-table)
  19365. (org-table-calc-current-TBLFM))
  19366. (goto-char (org-element-property :contents-begin context))
  19367. (org-call-with-arg 'org-table-recalculate (or arg t))
  19368. (orgtbl-send-table 'maybe)))
  19369. (org-table-maybe-eval-formula)
  19370. (cond (arg (call-interactively 'org-table-recalculate))
  19371. ((org-table-maybe-recalculate-line))
  19372. (t (org-table-align)))))))
  19373. (timestamp (org-timestamp-change 0 'day))
  19374. (otherwise
  19375. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19376. (user-error
  19377. "C-c C-c can do nothing useful at this location")))))))))
  19378. (defun org-mode-restart ()
  19379. (interactive)
  19380. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19381. (funcall major-mode)
  19382. (hack-local-variables)
  19383. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19384. (org-indent-mode -1)))
  19385. (message "%s restarted" major-mode))
  19386. (defun org-kill-note-or-show-branches ()
  19387. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19388. (interactive)
  19389. (if (not org-finish-function)
  19390. (progn
  19391. (hide-subtree)
  19392. (call-interactively 'show-branches))
  19393. (let ((org-note-abort t))
  19394. (funcall org-finish-function))))
  19395. (defun org-open-line (n)
  19396. "Insert a new row in tables, call `open-line' elsewhere.
  19397. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19398. (interactive "*p")
  19399. (cond
  19400. ((not org-special-ctrl-o)
  19401. (open-line n))
  19402. ((org-at-table-p)
  19403. (org-table-insert-row))
  19404. (t
  19405. (open-line n))))
  19406. (defun org-return (&optional indent)
  19407. "Goto next table row or insert a newline.
  19408. Calls `org-table-next-row' or `newline', depending on context.
  19409. When optional INDENT argument is non-nil, call
  19410. `newline-and-indent' instead of `newline'.
  19411. When `org-return-follows-link' is non-nil and point is on
  19412. a timestamp or a link, call `org-open-at-point'. However, it
  19413. will not happen if point is in a table or on a \"dead\"
  19414. object (e.g., within a comment). In these case, you need to use
  19415. `org-open-at-point' directly."
  19416. (interactive)
  19417. (if (and (save-excursion
  19418. (beginning-of-line)
  19419. (looking-at org-todo-line-regexp))
  19420. (match-beginning 3)
  19421. (>= (point) (match-beginning 3)))
  19422. ;; Point is on headline tags. Do not break them: add a newline
  19423. ;; after the headline instead.
  19424. (progn (org-show-entry)
  19425. (end-of-line)
  19426. (if indent (newline-and-indent) (newline)))
  19427. (let* ((context (if org-return-follows-link (org-element-context)
  19428. (org-element-at-point)))
  19429. (type (org-element-type context)))
  19430. (cond
  19431. ;; In a table, call `org-table-next-row'.
  19432. ((or (and (eq type 'table)
  19433. (>= (point) (org-element-property :contents-begin context))
  19434. (< (point) (org-element-property :contents-end context)))
  19435. (org-element-lineage context '(table-row table-cell) t))
  19436. (org-table-justify-field-maybe)
  19437. (call-interactively #'org-table-next-row))
  19438. ;; On a link or a timestamp but not on white spaces after it,
  19439. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19440. ((and org-return-follows-link
  19441. (memq type '(link timestamp))
  19442. (< (point)
  19443. (save-excursion (goto-char (org-element-property :end context))
  19444. (skip-chars-backward " \t")
  19445. (point))))
  19446. (call-interactively #'org-open-at-point))
  19447. ;; In a list, make sure indenting keeps trailing text within.
  19448. ((and indent
  19449. (not (eolp))
  19450. (org-element-lineage context '(item)))
  19451. (let ((trailing-data
  19452. (delete-and-extract-region (point) (line-end-position))))
  19453. (newline-and-indent)
  19454. (save-excursion (insert trailing-data))))
  19455. (t (if indent (newline-and-indent) (newline)))))))
  19456. (defun org-return-indent ()
  19457. "Goto next table row or insert a newline and indent.
  19458. Calls `org-table-next-row' or `newline-and-indent', depending on
  19459. context. See the individual commands for more information."
  19460. (interactive)
  19461. (org-return t))
  19462. (defun org-ctrl-c-star ()
  19463. "Compute table, or change heading status of lines.
  19464. Calls `org-table-recalculate' or `org-toggle-heading',
  19465. depending on context."
  19466. (interactive)
  19467. (cond
  19468. ((org-at-table-p)
  19469. (call-interactively 'org-table-recalculate))
  19470. (t
  19471. ;; Convert all lines in region to list items
  19472. (call-interactively 'org-toggle-heading))))
  19473. (defun org-ctrl-c-minus ()
  19474. "Insert separator line in table or modify bullet status of line.
  19475. Also turns a plain line or a region of lines into list items.
  19476. Calls `org-table-insert-hline', `org-toggle-item', or
  19477. `org-cycle-list-bullet', depending on context."
  19478. (interactive)
  19479. (cond
  19480. ((org-at-table-p)
  19481. (call-interactively 'org-table-insert-hline))
  19482. ((org-region-active-p)
  19483. (call-interactively 'org-toggle-item))
  19484. ((org-in-item-p)
  19485. (call-interactively 'org-cycle-list-bullet))
  19486. (t
  19487. (call-interactively 'org-toggle-item))))
  19488. (defun org-toggle-item (arg)
  19489. "Convert headings or normal lines to items, items to normal lines.
  19490. If there is no active region, only the current line is considered.
  19491. If the first non blank line in the region is a headline, convert
  19492. all headlines to items, shifting text accordingly.
  19493. If it is an item, convert all items to normal lines.
  19494. If it is normal text, change region into a list of items.
  19495. With a prefix argument ARG, change the region in a single item."
  19496. (interactive "P")
  19497. (let ((shift-text
  19498. (function
  19499. ;; Shift text in current section to IND, from point to END.
  19500. ;; The function leaves point to END line.
  19501. (lambda (ind end)
  19502. (let ((min-i 1000) (end (copy-marker end)))
  19503. ;; First determine the minimum indentation (MIN-I) of
  19504. ;; the text.
  19505. (save-excursion
  19506. (catch 'exit
  19507. (while (< (point) end)
  19508. (let ((i (org-get-indentation)))
  19509. (cond
  19510. ;; Skip blank lines and inline tasks.
  19511. ((looking-at "^[ \t]*$"))
  19512. ((looking-at org-outline-regexp-bol))
  19513. ;; We can't find less than 0 indentation.
  19514. ((zerop i) (throw 'exit (setq min-i 0)))
  19515. ((< i min-i) (setq min-i i))))
  19516. (forward-line))))
  19517. ;; Then indent each line so that a line indented to
  19518. ;; MIN-I becomes indented to IND. Ignore blank lines
  19519. ;; and inline tasks in the process.
  19520. (let ((delta (- ind min-i)))
  19521. (while (< (point) end)
  19522. (unless (or (looking-at "^[ \t]*$")
  19523. (looking-at org-outline-regexp-bol))
  19524. (org-indent-line-to (+ (org-get-indentation) delta)))
  19525. (forward-line)))))))
  19526. (skip-blanks
  19527. (function
  19528. ;; Return beginning of first non-blank line, starting from
  19529. ;; line at POS.
  19530. (lambda (pos)
  19531. (save-excursion
  19532. (goto-char pos)
  19533. (skip-chars-forward " \r\t\n")
  19534. (point-at-bol)))))
  19535. beg end)
  19536. ;; Determine boundaries of changes.
  19537. (if (org-region-active-p)
  19538. (setq beg (funcall skip-blanks (region-beginning))
  19539. end (copy-marker (region-end)))
  19540. (setq beg (funcall skip-blanks (point-at-bol))
  19541. end (copy-marker (point-at-eol))))
  19542. ;; Depending on the starting line, choose an action on the text
  19543. ;; between BEG and END.
  19544. (org-with-limited-levels
  19545. (save-excursion
  19546. (goto-char beg)
  19547. (cond
  19548. ;; Case 1. Start at an item: de-itemize. Note that it only
  19549. ;; happens when a region is active: `org-ctrl-c-minus'
  19550. ;; would call `org-cycle-list-bullet' otherwise.
  19551. ((org-at-item-p)
  19552. (while (< (point) end)
  19553. (when (org-at-item-p)
  19554. (skip-chars-forward " \t")
  19555. (delete-region (point) (match-end 0)))
  19556. (forward-line)))
  19557. ;; Case 2. Start at an heading: convert to items.
  19558. ((org-at-heading-p)
  19559. (let* ((bul (org-list-bullet-string "-"))
  19560. (bul-len (length bul))
  19561. (done (org-entry-is-done-p))
  19562. (todo (org-entry-is-todo-p))
  19563. ;; Indentation of the first heading. It should be
  19564. ;; relative to the indentation of its parent, if any.
  19565. (start-ind (save-excursion
  19566. (cond
  19567. ((not org-adapt-indentation) 0)
  19568. ((not (outline-previous-heading)) 0)
  19569. (t (length (match-string 0))))))
  19570. ;; Level of first heading. Further headings will be
  19571. ;; compared to it to determine hierarchy in the list.
  19572. (ref-level (org-reduced-level (org-outline-level))))
  19573. (when (or done todo) (org-todo ""))
  19574. (while (< (point) end)
  19575. (let* ((level (org-reduced-level (org-outline-level)))
  19576. (delta (max 0 (- level ref-level))))
  19577. ;; If current headline is less indented than the first
  19578. ;; one, set it as reference, in order to preserve
  19579. ;; subtrees.
  19580. (when (< level ref-level) (setq ref-level level))
  19581. (replace-match bul t t)
  19582. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19583. (when (or done todo)
  19584. (let* ((struct (org-list-struct))
  19585. (old (copy-tree struct)))
  19586. (org-list-set-checkbox (line-beginning-position)
  19587. struct
  19588. (if done "[X]" "[ ]"))
  19589. (org-list-write-struct struct
  19590. (org-list-parents-alist struct)
  19591. old)))
  19592. ;; Ensure all text down to END (or SECTION-END) belongs
  19593. ;; to the newly created item.
  19594. (let ((section-end (save-excursion
  19595. (or (outline-next-heading) (point)))))
  19596. (forward-line)
  19597. (funcall shift-text
  19598. (+ start-ind (* (1+ delta) bul-len))
  19599. (min end section-end)))))))
  19600. ;; Case 3. Normal line with ARG: make the first line of region
  19601. ;; an item, and shift indentation of others lines to
  19602. ;; set them as item's body.
  19603. (arg (let* ((bul (org-list-bullet-string "-"))
  19604. (bul-len (length bul))
  19605. (ref-ind (org-get-indentation)))
  19606. (skip-chars-forward " \t")
  19607. (insert bul)
  19608. (forward-line)
  19609. (while (< (point) end)
  19610. ;; Ensure that lines less indented than first one
  19611. ;; still get included in item body.
  19612. (funcall shift-text
  19613. (+ ref-ind bul-len)
  19614. (min end (save-excursion (or (outline-next-heading)
  19615. (point)))))
  19616. (forward-line))))
  19617. ;; Case 4. Normal line without ARG: turn each non-item line
  19618. ;; into an item.
  19619. (t
  19620. (while (< (point) end)
  19621. (unless (or (org-at-heading-p) (org-at-item-p))
  19622. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19623. (replace-match
  19624. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19625. (forward-line))))))))
  19626. (defun org-toggle-heading (&optional nstars)
  19627. "Convert headings to normal text, or items or text to headings.
  19628. If there is no active region, only convert the current line.
  19629. With a \\[universal-argument] prefix, convert the whole list at
  19630. point into heading.
  19631. In a region:
  19632. - If the first non blank line is a headline, remove the stars
  19633. from all headlines in the region.
  19634. - If it is a normal line, turn each and every normal line (i.e.,
  19635. not an heading or an item) in the region into headings. If you
  19636. want to convert only the first line of this region, use one
  19637. universal prefix argument.
  19638. - If it is a plain list item, turn all plain list items into headings.
  19639. When converting a line into a heading, the number of stars is chosen
  19640. such that the lines become children of the current entry. However,
  19641. when a numeric prefix argument is given, its value determines the
  19642. number of stars to add."
  19643. (interactive "P")
  19644. (let ((skip-blanks
  19645. (function
  19646. ;; Return beginning of first non-blank line, starting from
  19647. ;; line at POS.
  19648. (lambda (pos)
  19649. (save-excursion
  19650. (goto-char pos)
  19651. (while (org-at-comment-p) (forward-line))
  19652. (skip-chars-forward " \r\t\n")
  19653. (point-at-bol)))))
  19654. beg end toggled)
  19655. ;; Determine boundaries of changes. If a universal prefix has
  19656. ;; been given, put the list in a region. If region ends at a bol,
  19657. ;; do not consider the last line to be in the region.
  19658. (when (and current-prefix-arg (org-at-item-p))
  19659. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19660. (org-mark-element))
  19661. (if (org-region-active-p)
  19662. (setq beg (funcall skip-blanks (region-beginning))
  19663. end (copy-marker (save-excursion
  19664. (goto-char (region-end))
  19665. (if (bolp) (point) (point-at-eol)))))
  19666. (setq beg (funcall skip-blanks (point-at-bol))
  19667. end (copy-marker (point-at-eol))))
  19668. ;; Ensure inline tasks don't count as headings.
  19669. (org-with-limited-levels
  19670. (save-excursion
  19671. (goto-char beg)
  19672. (cond
  19673. ;; Case 1. Started at an heading: de-star headings.
  19674. ((org-at-heading-p)
  19675. (while (< (point) end)
  19676. (when (org-at-heading-p t)
  19677. (looking-at org-outline-regexp) (replace-match "")
  19678. (setq toggled t))
  19679. (forward-line)))
  19680. ;; Case 2. Started at an item: change items into headlines.
  19681. ;; One star will be added by `org-list-to-subtree'.
  19682. ((org-at-item-p)
  19683. (while (< (point) end)
  19684. (when (org-at-item-p)
  19685. ;; Pay attention to cases when region ends before list.
  19686. (let* ((struct (org-list-struct))
  19687. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19688. (save-restriction
  19689. (narrow-to-region (point) list-end)
  19690. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19691. (setq toggled t))
  19692. (forward-line)))
  19693. ;; Case 3. Started at normal text: make every line an heading,
  19694. ;; skipping headlines and items.
  19695. (t (let* ((stars
  19696. (make-string
  19697. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19698. (add-stars
  19699. (cond (nstars "") ; stars from prefix only
  19700. ((equal stars "") "*") ; before first heading
  19701. (org-odd-levels-only "**") ; inside heading, odd
  19702. (t "*"))) ; inside heading, oddeven
  19703. (rpl (concat stars add-stars " "))
  19704. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19705. (while (< (point) (if (equal nstars '(4)) lend end))
  19706. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19707. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19708. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19709. (forward-line)))))))
  19710. (unless toggled (message "Cannot toggle heading from here"))))
  19711. (defun org-meta-return (&optional arg)
  19712. "Insert a new heading or wrap a region in a table.
  19713. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19714. on context. See the individual commands for more information."
  19715. (interactive "P")
  19716. (org-check-before-invisible-edit 'insert)
  19717. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19718. (let* ((element (org-element-at-point))
  19719. (type (org-element-type element)))
  19720. (when (eq type 'table-row)
  19721. (setq element (org-element-property :parent element))
  19722. (setq type 'table))
  19723. (if (and (eq type 'table)
  19724. (eq (org-element-property :type element) 'org)
  19725. (>= (point) (org-element-property :contents-begin element))
  19726. (< (point) (org-element-property :contents-end element)))
  19727. (call-interactively 'org-table-wrap-region)
  19728. (call-interactively 'org-insert-heading)))))
  19729. ;;; Menu entries
  19730. (defsubst org-in-subtree-not-table-p ()
  19731. "Are we in a subtree and not in a table?"
  19732. (and (not (org-before-first-heading-p))
  19733. (not (org-at-table-p))))
  19734. ;; Define the Org-mode menus
  19735. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19736. '("Tbl"
  19737. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19738. ["Next Field" org-cycle (org-at-table-p)]
  19739. ["Previous Field" org-shifttab (org-at-table-p)]
  19740. ["Next Row" org-return (org-at-table-p)]
  19741. "--"
  19742. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19743. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19744. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19745. "--"
  19746. ("Column"
  19747. ["Move Column Left" org-metaleft (org-at-table-p)]
  19748. ["Move Column Right" org-metaright (org-at-table-p)]
  19749. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19750. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19751. ("Row"
  19752. ["Move Row Up" org-metaup (org-at-table-p)]
  19753. ["Move Row Down" org-metadown (org-at-table-p)]
  19754. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19755. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19756. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19757. "--"
  19758. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19759. ("Rectangle"
  19760. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19761. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19762. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19763. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19764. "--"
  19765. ("Calculate"
  19766. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19767. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19768. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19769. "--"
  19770. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19771. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19772. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19773. "--"
  19774. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19775. "--"
  19776. ["Sum Column/Rectangle" org-table-sum
  19777. (or (org-at-table-p) (org-region-active-p))]
  19778. ["Which Column?" org-table-current-column (org-at-table-p)])
  19779. ["Debug Formulas"
  19780. org-table-toggle-formula-debugger
  19781. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19782. ["Show Col/Row Numbers"
  19783. org-table-toggle-coordinate-overlays
  19784. :style toggle
  19785. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19786. "--"
  19787. ["Create" org-table-create (and (not (org-at-table-p))
  19788. org-enable-table-editor)]
  19789. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19790. ["Import from File" org-table-import (not (org-at-table-p))]
  19791. ["Export to File" org-table-export (org-at-table-p)]
  19792. "--"
  19793. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19794. "--"
  19795. ("Plot"
  19796. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19797. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19798. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19799. '("Org"
  19800. ("Show/Hide"
  19801. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19802. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19803. ["Sparse Tree..." org-sparse-tree t]
  19804. ["Reveal Context" org-reveal t]
  19805. ["Show All" show-all t]
  19806. "--"
  19807. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19808. "--"
  19809. ["New Heading" org-insert-heading t]
  19810. ("Navigate Headings"
  19811. ["Up" outline-up-heading t]
  19812. ["Next" outline-next-visible-heading t]
  19813. ["Previous" outline-previous-visible-heading t]
  19814. ["Next Same Level" outline-forward-same-level t]
  19815. ["Previous Same Level" outline-backward-same-level t]
  19816. "--"
  19817. ["Jump" org-goto t])
  19818. ("Edit Structure"
  19819. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19820. "--"
  19821. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19822. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19823. "--"
  19824. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19825. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19826. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19827. "--"
  19828. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19829. "--"
  19830. ["Copy visible text" org-copy-visible t]
  19831. "--"
  19832. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19833. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19834. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19835. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19836. "--"
  19837. ["Sort Region/Children" org-sort t]
  19838. "--"
  19839. ["Convert to odd levels" org-convert-to-odd-levels t]
  19840. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19841. ("Editing"
  19842. ["Emphasis..." org-emphasize t]
  19843. ["Edit Source Example" org-edit-special t]
  19844. "--"
  19845. ["Footnote new/jump" org-footnote-action t]
  19846. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19847. ("Archive"
  19848. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19849. "--"
  19850. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19851. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19852. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19853. )
  19854. "--"
  19855. ("Hyperlinks"
  19856. ["Store Link (Global)" org-store-link t]
  19857. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19858. ["Insert Link" org-insert-link t]
  19859. ["Follow Link" org-open-at-point t]
  19860. "--"
  19861. ["Next link" org-next-link t]
  19862. ["Previous link" org-previous-link t]
  19863. "--"
  19864. ["Descriptive Links"
  19865. org-toggle-link-display
  19866. :style radio
  19867. :selected org-descriptive-links
  19868. ]
  19869. ["Literal Links"
  19870. org-toggle-link-display
  19871. :style radio
  19872. :selected (not org-descriptive-links)])
  19873. "--"
  19874. ("TODO Lists"
  19875. ["TODO/DONE/-" org-todo t]
  19876. ("Select keyword"
  19877. ["Next keyword" org-shiftright (org-at-heading-p)]
  19878. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19879. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19880. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19881. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19882. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19883. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19884. "--"
  19885. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19886. :selected org-enforce-todo-dependencies :style toggle :active t]
  19887. "Settings for tree at point"
  19888. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19889. :selected (org-entry-get nil "ORDERED")
  19890. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19891. ["Do Children parallel" org-toggle-ordered-property :style radio
  19892. :selected (not (org-entry-get nil "ORDERED"))
  19893. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19894. "--"
  19895. ["Set Priority" org-priority t]
  19896. ["Priority Up" org-shiftup t]
  19897. ["Priority Down" org-shiftdown t]
  19898. "--"
  19899. ["Get news from all feeds" org-feed-update-all t]
  19900. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19901. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19902. ("TAGS and Properties"
  19903. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19904. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19905. "--"
  19906. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19907. ["Column view of properties" org-columns t]
  19908. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19909. ("Dates and Scheduling"
  19910. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19911. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19912. ("Change Date"
  19913. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19914. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19915. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19916. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19917. ["Compute Time Range" org-evaluate-time-range t]
  19918. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19919. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19920. "--"
  19921. ["Custom time format" org-toggle-time-stamp-overlays
  19922. :style radio :selected org-display-custom-times]
  19923. "--"
  19924. ["Goto Calendar" org-goto-calendar t]
  19925. ["Date from Calendar" org-date-from-calendar t]
  19926. "--"
  19927. ["Start/Restart Timer" org-timer-start t]
  19928. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19929. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19930. ["Insert Timer String" org-timer t]
  19931. ["Insert Timer Item" org-timer-item t])
  19932. ("Logging work"
  19933. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19934. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19935. ["Clock out" org-clock-out t]
  19936. ["Clock cancel" org-clock-cancel t]
  19937. "--"
  19938. ["Mark as default task" org-clock-mark-default-task t]
  19939. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19940. ["Goto running clock" org-clock-goto t]
  19941. "--"
  19942. ["Display times" org-clock-display t]
  19943. ["Create clock table" org-clock-report t]
  19944. "--"
  19945. ["Record DONE time"
  19946. (progn (setq org-log-done (not org-log-done))
  19947. (message "Switching to %s will %s record a timestamp"
  19948. (car org-done-keywords)
  19949. (if org-log-done "automatically" "not")))
  19950. :style toggle :selected org-log-done])
  19951. "--"
  19952. ["Agenda Command..." org-agenda t]
  19953. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19954. ("File List for Agenda")
  19955. ("Special views current file"
  19956. ["TODO Tree" org-show-todo-tree t]
  19957. ["Check Deadlines" org-check-deadlines t]
  19958. ["Timeline" org-timeline t]
  19959. ["Tags/Property tree" org-match-sparse-tree t])
  19960. "--"
  19961. ["Export/Publish..." org-export-dispatch t]
  19962. ("LaTeX"
  19963. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19964. :selected org-cdlatex-mode]
  19965. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19966. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19967. ["Modify math symbol" org-cdlatex-math-modify
  19968. (org-inside-LaTeX-fragment-p)]
  19969. ["Insert citation" org-reftex-citation t]
  19970. "--"
  19971. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19972. "--"
  19973. ("MobileOrg"
  19974. ["Push Files and Views" org-mobile-push t]
  19975. ["Get Captured and Flagged" org-mobile-pull t]
  19976. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19977. "--"
  19978. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19979. "--"
  19980. ("Documentation"
  19981. ["Show Version" org-version t]
  19982. ["Info Documentation" org-info t])
  19983. ("Customize"
  19984. ["Browse Org Group" org-customize t]
  19985. "--"
  19986. ["Expand This Menu" org-create-customize-menu
  19987. (fboundp 'customize-menu-create)])
  19988. ["Send bug report" org-submit-bug-report t]
  19989. "--"
  19990. ("Refresh/Reload"
  19991. ["Refresh setup current buffer" org-mode-restart t]
  19992. ["Reload Org (after update)" org-reload t]
  19993. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19994. ))
  19995. (defun org-info (&optional node)
  19996. "Read documentation for Org-mode in the info system.
  19997. With optional NODE, go directly to that node."
  19998. (interactive)
  19999. (info (format "(org)%s" (or node ""))))
  20000. ;;;###autoload
  20001. (defun org-submit-bug-report ()
  20002. "Submit a bug report on Org-mode via mail.
  20003. Don't hesitate to report any problems or inaccurate documentation.
  20004. If you don't have setup sending mail from (X)Emacs, please copy the
  20005. output buffer into your mail program, as it gives us important
  20006. information about your Org-mode version and configuration."
  20007. (interactive)
  20008. (require 'reporter)
  20009. (org-load-modules-maybe)
  20010. (org-require-autoloaded-modules)
  20011. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20012. (reporter-submit-bug-report
  20013. "emacs-orgmode@gnu.org"
  20014. (org-version nil 'full)
  20015. (let (list)
  20016. (save-window-excursion
  20017. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20018. (delete-other-windows)
  20019. (erase-buffer)
  20020. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20021. We would like to add your full Org-mode and Outline configuration to the
  20022. bug report. This greatly simplifies the work of the maintainer and
  20023. other experts on the mailing list.
  20024. HOWEVER, some variables you have customized may contain private
  20025. information. The names of customers, colleagues, or friends, might
  20026. appear in the form of file names, tags, todo states, or search strings.
  20027. If you answer yes to the prompt, you might want to check and remove
  20028. such private information before sending the email.")
  20029. (add-text-properties (point-min) (point-max) '(face org-warning))
  20030. (when (yes-or-no-p "Include your Org-mode configuration ")
  20031. (mapatoms
  20032. (lambda (v)
  20033. (and (boundp v)
  20034. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20035. (or (and (symbol-value v)
  20036. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20037. (and
  20038. (get v 'custom-type) (get v 'standard-value)
  20039. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20040. (push v list)))))
  20041. (kill-buffer (get-buffer "*Warn about privacy*"))
  20042. list))
  20043. nil nil
  20044. "Remember to cover the basics, that is, what you expected to happen and
  20045. what in fact did happen. You don't know how to make a good report? See
  20046. http://orgmode.org/manual/Feedback.html#Feedback
  20047. Your bug report will be posted to the Org-mode mailing list.
  20048. ------------------------------------------------------------------------")
  20049. (save-excursion
  20050. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20051. (replace-match "\\1Bug: \\3 [\\2]")))))
  20052. (defun org-install-agenda-files-menu ()
  20053. (let ((bl (buffer-list)))
  20054. (save-excursion
  20055. (while bl
  20056. (set-buffer (pop bl))
  20057. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20058. (when (derived-mode-p 'org-mode)
  20059. (easy-menu-change
  20060. '("Org") "File List for Agenda"
  20061. (append
  20062. (list
  20063. ["Edit File List" (org-edit-agenda-file-list) t]
  20064. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20065. ["Remove Current File from List" org-remove-file t]
  20066. ["Cycle through agenda files" org-cycle-agenda-files t]
  20067. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20068. "--")
  20069. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20070. ;;;; Documentation
  20071. (defun org-require-autoloaded-modules ()
  20072. (interactive)
  20073. (mapc 'require
  20074. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20075. org-table org-timer)))
  20076. ;;;###autoload
  20077. (defun org-reload (&optional uncompiled)
  20078. "Reload all org lisp files.
  20079. With prefix arg UNCOMPILED, load the uncompiled versions."
  20080. (interactive "P")
  20081. (require 'loadhist)
  20082. (let* ((org-dir (org-find-library-dir "org"))
  20083. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20084. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20085. (remove-re (mapconcat 'identity
  20086. (mapcar (lambda (f) (concat "^" f "$"))
  20087. (list (if (featurep 'xemacs)
  20088. "org-colview"
  20089. "org-colview-xemacs")
  20090. "org" "org-loaddefs" "org-version"))
  20091. "\\|"))
  20092. (feats (delete-dups
  20093. (mapcar 'file-name-sans-extension
  20094. (mapcar 'file-name-nondirectory
  20095. (delq nil
  20096. (mapcar 'feature-file
  20097. features))))))
  20098. (lfeat (append
  20099. (sort
  20100. (setq feats
  20101. (delq nil (mapcar
  20102. (lambda (f)
  20103. (if (and (string-match feature-re f)
  20104. (not (string-match remove-re f)))
  20105. f nil))
  20106. feats)))
  20107. 'string-lessp)
  20108. (list "org-version" "org")))
  20109. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20110. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20111. load-uncore load-misses)
  20112. (setq load-misses
  20113. (delq 't
  20114. (mapcar (lambda (f)
  20115. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20116. (and (string= org-dir contrib-dir)
  20117. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20118. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20119. (add-to-list 'load-uncore f 'append)
  20120. 't)
  20121. f))
  20122. lfeat)))
  20123. (if load-uncore
  20124. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20125. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20126. (if load-misses
  20127. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20128. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20129. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20130. ;;;###autoload
  20131. (defun org-customize ()
  20132. "Call the customize function with org as argument."
  20133. (interactive)
  20134. (org-load-modules-maybe)
  20135. (org-require-autoloaded-modules)
  20136. (customize-browse 'org))
  20137. (defun org-create-customize-menu ()
  20138. "Create a full customization menu for Org-mode, insert it into the menu."
  20139. (interactive)
  20140. (org-load-modules-maybe)
  20141. (org-require-autoloaded-modules)
  20142. (if (fboundp 'customize-menu-create)
  20143. (progn
  20144. (easy-menu-change
  20145. '("Org") "Customize"
  20146. `(["Browse Org group" org-customize t]
  20147. "--"
  20148. ,(customize-menu-create 'org)
  20149. ["Set" Custom-set t]
  20150. ["Save" Custom-save t]
  20151. ["Reset to Current" Custom-reset-current t]
  20152. ["Reset to Saved" Custom-reset-saved t]
  20153. ["Reset to Standard Settings" Custom-reset-standard t]))
  20154. (message "\"Org\"-menu now contains full customization menu"))
  20155. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20156. ;;;; Miscellaneous stuff
  20157. ;;; Generally useful functions
  20158. (defsubst org-get-at-eol (property n)
  20159. "Get text property PROPERTY at the end of line less N characters."
  20160. (get-text-property (- (point-at-eol) n) property))
  20161. (defun org-find-text-property-in-string (prop s)
  20162. "Return the first non-nil value of property PROP in string S."
  20163. (or (get-text-property 0 prop s)
  20164. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20165. prop s)))
  20166. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20167. "Display the given MESSAGE as a warning."
  20168. (if (fboundp 'display-warning)
  20169. (display-warning 'org message
  20170. (if (featurep 'xemacs) 'warning :warning))
  20171. (let ((buf (get-buffer-create "*Org warnings*")))
  20172. (with-current-buffer buf
  20173. (goto-char (point-max))
  20174. (insert "Warning (Org): " message)
  20175. (unless (bolp)
  20176. (newline)))
  20177. (display-buffer buf)
  20178. (sit-for 0))))
  20179. (defun org-eval (form)
  20180. "Eval FORM and return result."
  20181. (condition-case error
  20182. (eval form)
  20183. (error (format "%%![Error: %s]" error))))
  20184. (defun org-in-clocktable-p ()
  20185. "Check if the cursor is in a clocktable."
  20186. (let ((pos (point)) start)
  20187. (save-excursion
  20188. (end-of-line 1)
  20189. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20190. (setq start (match-beginning 0))
  20191. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20192. (>= (match-end 0) pos)
  20193. start))))
  20194. (defun org-in-verbatim-emphasis ()
  20195. (save-match-data
  20196. (and (org-in-regexp org-emph-re 2)
  20197. (>= (point) (match-beginning 3))
  20198. (<= (point) (match-end 4))
  20199. (member (match-string 3) '("=" "~")))))
  20200. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20201. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20202. (if (and marker (marker-buffer marker)
  20203. (buffer-live-p (marker-buffer marker)))
  20204. (progn
  20205. (org-pop-to-buffer-same-window (marker-buffer marker))
  20206. (if (or (> marker (point-max)) (< marker (point-min)))
  20207. (widen))
  20208. (goto-char marker)
  20209. (org-show-context 'org-goto))
  20210. (if bookmark
  20211. (bookmark-jump bookmark)
  20212. (error "Cannot find location"))))
  20213. (defun org-quote-csv-field (s)
  20214. "Quote field for inclusion in CSV material."
  20215. (if (string-match "[\",]" s)
  20216. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20217. s))
  20218. (defun org-force-self-insert (N)
  20219. "Needed to enforce self-insert under remapping."
  20220. (interactive "p")
  20221. (self-insert-command N))
  20222. (defun org-string-width (s)
  20223. "Compute width of string, ignoring invisible characters.
  20224. This ignores character with invisibility property `org-link', and also
  20225. characters with property `org-cwidth', because these will become invisible
  20226. upon the next fontification round."
  20227. (let (b l)
  20228. (when (or (eq t buffer-invisibility-spec)
  20229. (assq 'org-link buffer-invisibility-spec))
  20230. (while (setq b (text-property-any 0 (length s)
  20231. 'invisible 'org-link s))
  20232. (setq s (concat (substring s 0 b)
  20233. (substring s (or (next-single-property-change
  20234. b 'invisible s) (length s)))))))
  20235. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20236. (setq s (concat (substring s 0 b)
  20237. (substring s (or (next-single-property-change
  20238. b 'org-cwidth s) (length s))))))
  20239. (setq l (string-width s) b -1)
  20240. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20241. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20242. l))
  20243. (defun org-shorten-string (s maxlength)
  20244. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20245. If the string is shorter or has length MAXLENGTH, just return the
  20246. original string. If it is longer, the functions finds a space in the
  20247. string, breaks this string off at that locations and adds three dots
  20248. as ellipsis. Including the ellipsis, the string will not be longer
  20249. than MAXLENGTH. If finding a good breaking point in the string does
  20250. not work, the string is just chopped off in the middle of a word
  20251. if necessary."
  20252. (if (<= (length s) maxlength)
  20253. s
  20254. (let* ((n (max (- maxlength 4) 1))
  20255. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20256. (if (string-match re s)
  20257. (concat (match-string 1 s) "...")
  20258. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20259. (defun org-get-indentation (&optional line)
  20260. "Get the indentation of the current line, interpreting tabs.
  20261. When LINE is given, assume it represents a line and compute its indentation."
  20262. (if line
  20263. (if (string-match "^ *" (org-remove-tabs line))
  20264. (match-end 0))
  20265. (save-excursion
  20266. (beginning-of-line 1)
  20267. (skip-chars-forward " \t")
  20268. (current-column))))
  20269. (defun org-get-string-indentation (s)
  20270. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20271. (let ((n -1) (i 0) (w tab-width) c)
  20272. (catch 'exit
  20273. (while (< (setq n (1+ n)) (length s))
  20274. (setq c (aref s n))
  20275. (cond ((= c ?\ ) (setq i (1+ i)))
  20276. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20277. (t (throw 'exit t)))))
  20278. i))
  20279. (defun org-remove-tabs (s &optional width)
  20280. "Replace tabulators in S with spaces.
  20281. Assumes that s is a single line, starting in column 0."
  20282. (setq width (or width tab-width))
  20283. (while (string-match "\t" s)
  20284. (setq s (replace-match
  20285. (make-string
  20286. (- (* width (/ (+ (match-beginning 0) width) width))
  20287. (match-beginning 0)) ?\ )
  20288. t t s)))
  20289. s)
  20290. (defun org-fix-indentation (line ind)
  20291. "Fix indentation in LINE.
  20292. IND is a cons cell with target and minimum indentation.
  20293. If the current indentation in LINE is smaller than the minimum,
  20294. leave it alone. If it is larger than ind, set it to the target."
  20295. (let* ((l (org-remove-tabs line))
  20296. (i (org-get-indentation l))
  20297. (i1 (car ind)) (i2 (cdr ind)))
  20298. (if (>= i i2) (setq l (substring line i2)))
  20299. (if (> i1 0)
  20300. (concat (make-string i1 ?\ ) l)
  20301. l)))
  20302. (defun org-remove-indentation (code &optional n)
  20303. "Remove the maximum common indentation from the lines in CODE.
  20304. N may optionally be the number of spaces to remove."
  20305. (with-temp-buffer
  20306. (insert code)
  20307. (org-do-remove-indentation n)
  20308. (buffer-string)))
  20309. (defun org-do-remove-indentation (&optional n)
  20310. "Remove the maximum common indentation from the buffer."
  20311. (untabify (point-min) (point-max))
  20312. (let ((min 10000) re)
  20313. (if n
  20314. (setq min n)
  20315. (goto-char (point-min))
  20316. (while (re-search-forward "^ *[^ \n]" nil t)
  20317. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20318. (unless (or (= min 0) (= min 10000))
  20319. (setq re (format "^ \\{%d\\}" min))
  20320. (goto-char (point-min))
  20321. (while (re-search-forward re nil t)
  20322. (replace-match "")
  20323. (end-of-line 1))
  20324. min)))
  20325. (defun org-fill-template (template alist)
  20326. "Find each %key of ALIST in TEMPLATE and replace it."
  20327. (let ((case-fold-search nil)
  20328. entry key value)
  20329. (setq alist (sort (copy-sequence alist)
  20330. (lambda (a b) (< (length (car a)) (length (car b))))))
  20331. (while (setq entry (pop alist))
  20332. (setq template
  20333. (replace-regexp-in-string
  20334. (concat "%" (regexp-quote (car entry)))
  20335. (or (cdr entry) "") template t t)))
  20336. template))
  20337. (defun org-base-buffer (buffer)
  20338. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20339. (if (not buffer)
  20340. buffer
  20341. (or (buffer-base-buffer buffer)
  20342. buffer)))
  20343. (defun org-wrap (string &optional width lines)
  20344. "Wrap string to either a number of lines, or a width in characters.
  20345. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20346. that costs. If there is a word longer than WIDTH, the text is actually
  20347. wrapped to the length of that word.
  20348. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20349. many lines, whatever width that takes.
  20350. The return value is a list of lines, without newlines at the end."
  20351. (let* ((words (org-split-string string "[ \t\n]+"))
  20352. (maxword (apply 'max (mapcar 'org-string-width words)))
  20353. w ll)
  20354. (cond (width
  20355. (org-do-wrap words (max maxword width)))
  20356. (lines
  20357. (setq w maxword)
  20358. (setq ll (org-do-wrap words maxword))
  20359. (if (<= (length ll) lines)
  20360. ll
  20361. (setq ll words)
  20362. (while (> (length ll) lines)
  20363. (setq w (1+ w))
  20364. (setq ll (org-do-wrap words w)))
  20365. ll))
  20366. (t (error "Cannot wrap this")))))
  20367. (defun org-do-wrap (words width)
  20368. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20369. (let (lines line)
  20370. (while words
  20371. (setq line (pop words))
  20372. (while (and words (< (+ (length line) (length (car words))) width))
  20373. (setq line (concat line " " (pop words))))
  20374. (setq lines (push line lines)))
  20375. (nreverse lines)))
  20376. (defun org-split-string (string &optional separators)
  20377. "Splits STRING into substrings at SEPARATORS.
  20378. No empty strings are returned if there are matches at the beginning
  20379. and end of string."
  20380. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  20381. (start 0)
  20382. notfirst
  20383. (list nil))
  20384. (while (and (string-match rexp string
  20385. (if (and notfirst
  20386. (= start (match-beginning 0))
  20387. (< start (length string)))
  20388. (1+ start) start))
  20389. (< (match-beginning 0) (length string)))
  20390. (setq notfirst t)
  20391. (or (eq (match-beginning 0) 0)
  20392. (and (eq (match-beginning 0) (match-end 0))
  20393. (eq (match-beginning 0) start))
  20394. (setq list
  20395. (cons (substring string start (match-beginning 0))
  20396. list)))
  20397. (setq start (match-end 0)))
  20398. (or (eq start (length string))
  20399. (setq list
  20400. (cons (substring string start)
  20401. list)))
  20402. (nreverse list)))
  20403. (defun org-quote-vert (s)
  20404. "Replace \"|\" with \"\\vert\"."
  20405. (while (string-match "|" s)
  20406. (setq s (replace-match "\\vert" t t s)))
  20407. s)
  20408. (defun org-uuidgen-p (s)
  20409. "Is S an ID created by UUIDGEN?"
  20410. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20411. (defun org-in-src-block-p (&optional inside)
  20412. "Whether point is in a code source block.
  20413. When INSIDE is non-nil, don't consider we are within a src block
  20414. when point is at #+BEGIN_SRC or #+END_SRC."
  20415. (let ((case-fold-search t) ov)
  20416. (or (and (eq (get-char-property (point) 'src-block) t))
  20417. (and (not inside)
  20418. (save-match-data
  20419. (save-excursion
  20420. (beginning-of-line)
  20421. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20422. (defun org-context ()
  20423. "Return a list of contexts of the current cursor position.
  20424. If several contexts apply, all are returned.
  20425. Each context entry is a list with a symbol naming the context, and
  20426. two positions indicating start and end of the context. Possible
  20427. contexts are:
  20428. :headline anywhere in a headline
  20429. :headline-stars on the leading stars in a headline
  20430. :todo-keyword on a TODO keyword (including DONE) in a headline
  20431. :tags on the TAGS in a headline
  20432. :priority on the priority cookie in a headline
  20433. :item on the first line of a plain list item
  20434. :item-bullet on the bullet/number of a plain list item
  20435. :checkbox on the checkbox in a plain list item
  20436. :table in an org-mode table
  20437. :table-special on a special filed in a table
  20438. :table-table in a table.el table
  20439. :clocktable in a clocktable
  20440. :src-block in a source block
  20441. :link on a hyperlink
  20442. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20443. :target on a <<target>>
  20444. :radio-target on a <<<radio-target>>>
  20445. :latex-fragment on a LaTeX fragment
  20446. :latex-preview on a LaTeX fragment with overlaid preview image
  20447. This function expects the position to be visible because it uses font-lock
  20448. faces as a help to recognize the following contexts: :table-special, :link,
  20449. and :keyword."
  20450. (let* ((f (get-text-property (point) 'face))
  20451. (faces (if (listp f) f (list f)))
  20452. (case-fold-search t)
  20453. (p (point)) clist o)
  20454. ;; First the large context
  20455. (cond
  20456. ((org-at-heading-p t)
  20457. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20458. (when (progn
  20459. (beginning-of-line 1)
  20460. (looking-at org-todo-line-tags-regexp))
  20461. (push (org-point-in-group p 1 :headline-stars) clist)
  20462. (push (org-point-in-group p 2 :todo-keyword) clist)
  20463. (push (org-point-in-group p 4 :tags) clist))
  20464. (goto-char p)
  20465. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20466. (if (looking-at "\\[#[A-Z0-9]\\]")
  20467. (push (org-point-in-group p 0 :priority) clist)))
  20468. ((org-at-item-p)
  20469. (push (org-point-in-group p 2 :item-bullet) clist)
  20470. (push (list :item (point-at-bol)
  20471. (save-excursion (org-end-of-item) (point)))
  20472. clist)
  20473. (and (org-at-item-checkbox-p)
  20474. (push (org-point-in-group p 0 :checkbox) clist)))
  20475. ((org-at-table-p)
  20476. (push (list :table (org-table-begin) (org-table-end)) clist)
  20477. (if (memq 'org-formula faces)
  20478. (push (list :table-special
  20479. (previous-single-property-change p 'face)
  20480. (next-single-property-change p 'face)) clist)))
  20481. ((org-at-table-p 'any)
  20482. (push (list :table-table) clist)))
  20483. (goto-char p)
  20484. (let ((case-fold-search t))
  20485. ;; New the "medium" contexts: clocktables, source blocks
  20486. (cond ((org-in-clocktable-p)
  20487. (push (list :clocktable
  20488. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20489. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20490. (match-beginning 1))
  20491. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20492. (match-end 0))) clist))
  20493. ((org-in-src-block-p)
  20494. (push (list :src-block
  20495. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20496. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20497. (match-beginning 1))
  20498. (and (search-forward "#+END_SRC" nil t)
  20499. (match-beginning 0))) clist))))
  20500. (goto-char p)
  20501. ;; Now the small context
  20502. (cond
  20503. ((org-at-timestamp-p)
  20504. (push (org-point-in-group p 0 :timestamp) clist))
  20505. ((memq 'org-link faces)
  20506. (push (list :link
  20507. (previous-single-property-change p 'face)
  20508. (next-single-property-change p 'face)) clist))
  20509. ((memq 'org-special-keyword faces)
  20510. (push (list :keyword
  20511. (previous-single-property-change p 'face)
  20512. (next-single-property-change p 'face)) clist))
  20513. ((org-at-target-p)
  20514. (push (org-point-in-group p 0 :target) clist)
  20515. (goto-char (1- (match-beginning 0)))
  20516. (if (looking-at org-radio-target-regexp)
  20517. (push (org-point-in-group p 0 :radio-target) clist))
  20518. (goto-char p))
  20519. ((setq o (car (delq nil
  20520. (mapcar
  20521. (lambda (x)
  20522. (if (memq x org-latex-fragment-image-overlays) x))
  20523. (overlays-at (point))))))
  20524. (push (list :latex-fragment
  20525. (overlay-start o) (overlay-end o)) clist)
  20526. (push (list :latex-preview
  20527. (overlay-start o) (overlay-end o)) clist))
  20528. ((org-inside-LaTeX-fragment-p)
  20529. ;; FIXME: positions wrong.
  20530. (push (list :latex-fragment (point) (point)) clist)))
  20531. (setq clist (nreverse (delq nil clist)))
  20532. clist))
  20533. (defun org-in-regexp (re &optional nlines visually)
  20534. "Check if point is inside a match of RE.
  20535. Normally only the current line is checked, but you can include
  20536. NLINES extra lines after point into the search. If VISUALLY is
  20537. set, require that the cursor is not after the match but really
  20538. on, so that the block visually is on the match."
  20539. (catch 'exit
  20540. (let ((pos (point))
  20541. (eol (point-at-eol (+ 1 (or nlines 0))))
  20542. (inc (if visually 1 0)))
  20543. (save-excursion
  20544. (beginning-of-line (- 1 (or nlines 0)))
  20545. (while (re-search-forward re eol t)
  20546. (if (and (<= (match-beginning 0) pos)
  20547. (>= (+ inc (match-end 0)) pos))
  20548. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20549. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20550. "Org mode 8.3")
  20551. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20552. "Non-nil when point is between matches of START-RE and END-RE.
  20553. Also return a non-nil value when point is on one of the matches.
  20554. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20555. buffer positions. Default values are the positions of headlines
  20556. surrounding the point.
  20557. The functions returns a cons cell whose car (resp. cdr) is the
  20558. position before START-RE (resp. after END-RE)."
  20559. (save-match-data
  20560. (let ((pos (point))
  20561. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20562. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20563. beg end)
  20564. (save-excursion
  20565. ;; Point is on a block when on START-RE or if START-RE can be
  20566. ;; found before it...
  20567. (and (or (org-in-regexp start-re)
  20568. (re-search-backward start-re limit-up t))
  20569. (setq beg (match-beginning 0))
  20570. ;; ... and END-RE after it...
  20571. (goto-char (match-end 0))
  20572. (re-search-forward end-re limit-down t)
  20573. (> (setq end (match-end 0)) pos)
  20574. ;; ... without another START-RE in-between.
  20575. (goto-char (match-beginning 0))
  20576. (not (re-search-backward start-re (1+ beg) t))
  20577. ;; Return value.
  20578. (cons beg end))))))
  20579. (defun org-in-block-p (names)
  20580. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20581. NAMES is a list of strings containing names of blocks.
  20582. Return first block name matched, or nil. Beware that in case of
  20583. nested blocks, the returned name may not belong to the closest
  20584. block from point."
  20585. (save-match-data
  20586. (catch 'exit
  20587. (let ((case-fold-search t)
  20588. (lim-up (save-excursion (outline-previous-heading)))
  20589. (lim-down (save-excursion (outline-next-heading))))
  20590. (mapc (lambda (name)
  20591. (let ((n (regexp-quote name)))
  20592. (when (org-between-regexps-p
  20593. (concat "^[ \t]*#\\+begin_" n)
  20594. (concat "^[ \t]*#\\+end_" n)
  20595. lim-up lim-down)
  20596. (throw 'exit n))))
  20597. names))
  20598. nil)))
  20599. (defun org-occur-in-agenda-files (regexp &optional nlines)
  20600. "Call `multi-occur' with buffers for all agenda files."
  20601. (interactive "sOrg-files matching: \np")
  20602. (let* ((files (org-agenda-files))
  20603. (tnames (mapcar 'file-truename files))
  20604. (extra org-agenda-text-search-extra-files)
  20605. f)
  20606. (when (eq (car extra) 'agenda-archives)
  20607. (setq extra (cdr extra))
  20608. (setq files (org-add-archive-files files)))
  20609. (while (setq f (pop extra))
  20610. (unless (member (file-truename f) tnames)
  20611. (add-to-list 'files f 'append)
  20612. (add-to-list 'tnames (file-truename f) 'append)))
  20613. (multi-occur
  20614. (mapcar (lambda (x)
  20615. (with-current-buffer
  20616. (or (get-file-buffer x) (find-file-noselect x))
  20617. (widen)
  20618. (current-buffer)))
  20619. files)
  20620. regexp)))
  20621. (if (boundp 'occur-mode-find-occurrence-hook)
  20622. ;; Emacs 23
  20623. (add-hook 'occur-mode-find-occurrence-hook
  20624. (lambda ()
  20625. (when (derived-mode-p 'org-mode)
  20626. (org-reveal))))
  20627. ;; Emacs 22
  20628. (defadvice occur-mode-goto-occurrence
  20629. (after org-occur-reveal activate)
  20630. (and (derived-mode-p 'org-mode) (org-reveal)))
  20631. (defadvice occur-mode-goto-occurrence-other-window
  20632. (after org-occur-reveal activate)
  20633. (and (derived-mode-p 'org-mode) (org-reveal)))
  20634. (defadvice occur-mode-display-occurrence
  20635. (after org-occur-reveal activate)
  20636. (when (derived-mode-p 'org-mode)
  20637. (let ((pos (occur-mode-find-occurrence)))
  20638. (with-current-buffer (marker-buffer pos)
  20639. (save-excursion
  20640. (goto-char pos)
  20641. (org-reveal)))))))
  20642. (defun org-occur-link-in-agenda-files ()
  20643. "Create a link and search for it in the agendas.
  20644. The link is not stored in `org-stored-links', it is just created
  20645. for the search purpose."
  20646. (interactive)
  20647. (let ((link (condition-case nil
  20648. (org-store-link nil)
  20649. (error "Unable to create a link to here"))))
  20650. (org-occur-in-agenda-files (regexp-quote link))))
  20651. (defun org-reverse-string (string)
  20652. "Return the reverse of STRING."
  20653. (apply 'string (reverse (string-to-list string))))
  20654. ;; defsubst org-uniquify must be defined before first use
  20655. (defun org-uniquify-alist (alist)
  20656. "Merge elements of ALIST with the same key.
  20657. For example, in this alist:
  20658. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20659. => '((a 1 3) (b 2))
  20660. merge (a 1) and (a 3) into (a 1 3).
  20661. The function returns the new ALIST."
  20662. (let (rtn)
  20663. (mapc
  20664. (lambda (e)
  20665. (let (n)
  20666. (if (not (assoc (car e) rtn))
  20667. (push e rtn)
  20668. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20669. (setq rtn (assq-delete-all (car e) rtn))
  20670. (push n rtn))))
  20671. alist)
  20672. rtn))
  20673. (defun org-delete-all (elts list)
  20674. "Remove all elements in ELTS from LIST."
  20675. (while elts
  20676. (setq list (delete (pop elts) list)))
  20677. list)
  20678. (defun org-count (cl-item cl-seq)
  20679. "Count the number of occurrences of ITEM in SEQ.
  20680. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20681. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20682. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20683. (while (< cl-start cl-end)
  20684. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20685. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20686. (setq cl-start (1+ cl-start)))
  20687. cl-count))
  20688. (defun org-remove-if (predicate seq)
  20689. "Remove everything from SEQ that fulfills PREDICATE."
  20690. (let (res e)
  20691. (while seq
  20692. (setq e (pop seq))
  20693. (if (not (funcall predicate e)) (push e res)))
  20694. (nreverse res)))
  20695. (defun org-remove-if-not (predicate seq)
  20696. "Remove everything from SEQ that does not fulfill PREDICATE."
  20697. (let (res e)
  20698. (while seq
  20699. (setq e (pop seq))
  20700. (if (funcall predicate e) (push e res)))
  20701. (nreverse res)))
  20702. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20703. "Reduce two-argument FUNCTION across SEQ.
  20704. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20705. \":initial-value\" removed."
  20706. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20707. (cadr (memq :initial-value cl-keys)))
  20708. (cl-seq (pop cl-seq))
  20709. (t (funcall cl-func)))))
  20710. (while cl-seq
  20711. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20712. cl-accum))
  20713. (defun org-every (pred seq)
  20714. "Return true if PREDICATE is true of every element of SEQ.
  20715. Adapted from `every' in cl.el."
  20716. (catch 'org-every
  20717. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20718. t))
  20719. (defun org-some (pred seq)
  20720. "Return true if PREDICATE is true of any element of SEQ.
  20721. Adapted from `some' in cl.el."
  20722. (catch 'org-some
  20723. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20724. nil))
  20725. (defun org-back-over-empty-lines ()
  20726. "Move backwards over whitespace, to the beginning of the first empty line.
  20727. Returns the number of empty lines passed."
  20728. (let ((pos (point)))
  20729. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20730. (skip-chars-backward " \t\n\r")
  20731. (unless (eobp)
  20732. (forward-line -1)))
  20733. (beginning-of-line 2)
  20734. (goto-char (min (point) pos))
  20735. (count-lines (point) pos)))
  20736. (defun org-skip-whitespace ()
  20737. (skip-chars-forward " \t\n\r"))
  20738. (defun org-point-in-group (point group &optional context)
  20739. "Check if POINT is in match-group GROUP.
  20740. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20741. match. If the match group does not exist or point is not inside it,
  20742. return nil."
  20743. (and (match-beginning group)
  20744. (>= point (match-beginning group))
  20745. (<= point (match-end group))
  20746. (if context
  20747. (list context (match-beginning group) (match-end group))
  20748. t)))
  20749. (defun org-switch-to-buffer-other-window (&rest args)
  20750. "Switch to buffer in a second window on the current frame.
  20751. In particular, do not allow pop-up frames.
  20752. Returns the newly created buffer."
  20753. (org-no-popups
  20754. (apply 'switch-to-buffer-other-window args)))
  20755. (defun org-combine-plists (&rest plists)
  20756. "Create a single property list from all plists in PLISTS.
  20757. The process starts by copying the first list, and then setting properties
  20758. from the other lists. Settings in the last list are the most significant
  20759. ones and overrule settings in the other lists."
  20760. (let ((rtn (copy-sequence (pop plists)))
  20761. p v ls)
  20762. (while plists
  20763. (setq ls (pop plists))
  20764. (while ls
  20765. (setq p (pop ls) v (pop ls))
  20766. (setq rtn (plist-put rtn p v))))
  20767. rtn))
  20768. (defun org-replace-escapes (string table)
  20769. "Replace %-escapes in STRING with values in TABLE.
  20770. TABLE is an association list with keys like \"%a\" and string values.
  20771. The sequences in STRING may contain normal field width and padding information,
  20772. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20773. so values can contain further %-escapes if they are define later in TABLE."
  20774. (let ((tbl (copy-alist table))
  20775. (case-fold-search nil)
  20776. (pchg 0)
  20777. e re rpl)
  20778. (while (setq e (pop tbl))
  20779. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20780. (when (and (cdr e) (string-match re (cdr e)))
  20781. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20782. (safe "SREF"))
  20783. (add-text-properties 0 3 (list 'sref sref) safe)
  20784. (setcdr e (replace-match safe t t (cdr e)))))
  20785. (while (string-match re string)
  20786. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20787. (cdr e)))
  20788. (setq string (replace-match rpl t t string))))
  20789. (while (setq pchg (next-property-change pchg string))
  20790. (let ((sref (get-text-property pchg 'sref string)))
  20791. (when (and sref (string-match "SREF" string pchg))
  20792. (setq string (replace-match sref t t string)))))
  20793. string))
  20794. (defun org-sublist (list start end)
  20795. "Return a section of LIST, from START to END.
  20796. Counting starts at 1."
  20797. (let (rtn (c start))
  20798. (setq list (nthcdr (1- start) list))
  20799. (while (and list (<= c end))
  20800. (push (pop list) rtn)
  20801. (setq c (1+ c)))
  20802. (nreverse rtn)))
  20803. (defun org-find-base-buffer-visiting (file)
  20804. "Like `find-buffer-visiting' but always return the base buffer and
  20805. not an indirect buffer."
  20806. (let ((buf (or (get-file-buffer file)
  20807. (find-buffer-visiting file))))
  20808. (if buf
  20809. (or (buffer-base-buffer buf) buf)
  20810. nil)))
  20811. (defun org-image-file-name-regexp (&optional extensions)
  20812. "Return regexp matching the file names of images.
  20813. If EXTENSIONS is given, only match these."
  20814. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20815. (image-file-name-regexp)
  20816. (let ((image-file-name-extensions
  20817. (or extensions
  20818. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20819. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20820. (concat "\\."
  20821. (regexp-opt (nconc (mapcar 'upcase
  20822. image-file-name-extensions)
  20823. image-file-name-extensions)
  20824. t)
  20825. "\\'"))))
  20826. (defun org-file-image-p (file &optional extensions)
  20827. "Return non-nil if FILE is an image."
  20828. (save-match-data
  20829. (string-match (org-image-file-name-regexp extensions) file)))
  20830. (defun org-get-cursor-date (&optional with-time)
  20831. "Return the date at cursor in as a time.
  20832. This works in the calendar and in the agenda, anywhere else it just
  20833. returns the current time.
  20834. If WITH-TIME is non-nil, returns the time of the event at point (in
  20835. the agenda) or the current time of the day."
  20836. (let (date day defd tp tm hod mod)
  20837. (when with-time
  20838. (setq tp (get-text-property (point) 'time))
  20839. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20840. (setq hod (string-to-number (match-string 1 tp))
  20841. mod (string-to-number (match-string 2 tp))))
  20842. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20843. mod (nth 1 (decode-time (current-time))))))
  20844. (cond
  20845. ((eq major-mode 'calendar-mode)
  20846. (setq date (calendar-cursor-to-date)
  20847. defd (encode-time 0 (or mod 0) (or hod 0)
  20848. (nth 1 date) (nth 0 date) (nth 2 date))))
  20849. ((eq major-mode 'org-agenda-mode)
  20850. (setq day (get-text-property (point) 'day))
  20851. (if day
  20852. (setq date (calendar-gregorian-from-absolute day)
  20853. defd (encode-time 0 (or mod 0) (or hod 0)
  20854. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20855. (or defd (current-time))))
  20856. (defun org-mark-subtree (&optional up)
  20857. "Mark the current subtree.
  20858. This puts point at the start of the current subtree, and mark at
  20859. the end. If a numeric prefix UP is given, move up into the
  20860. hierarchy of headlines by UP levels before marking the subtree."
  20861. (interactive "P")
  20862. (org-with-limited-levels
  20863. (cond ((org-at-heading-p) (beginning-of-line))
  20864. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20865. (t (outline-previous-visible-heading 1))))
  20866. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20867. (if (org-called-interactively-p 'any)
  20868. (call-interactively 'org-mark-element)
  20869. (org-mark-element)))
  20870. ;;; Indentation
  20871. (defun org--get-expected-indentation (element contentsp)
  20872. "Expected indentation column for current line, according to ELEMENT.
  20873. ELEMENT is an element containing point. CONTENTSP is non-nil
  20874. when indentation is to be computed according to contents of
  20875. ELEMENT."
  20876. (let ((type (org-element-type element))
  20877. (start (org-element-property :begin element)))
  20878. (org-with-wide-buffer
  20879. (cond
  20880. (contentsp
  20881. (case type
  20882. ((diary-sexp footnote-definition) 0)
  20883. ((headline inlinetask nil)
  20884. (if (not org-adapt-indentation) 0
  20885. (let ((level (org-current-level)))
  20886. (if level (1+ level) 0))))
  20887. (item
  20888. (org-list-item-body-column
  20889. (org-element-property :post-affiliated element)))
  20890. (plain-list
  20891. (save-excursion
  20892. (goto-char (org-element-property :post-affiliated element))
  20893. (org-get-indentation)))
  20894. (otherwise
  20895. (goto-char start)
  20896. (org-get-indentation))))
  20897. ((memq type '(headline inlinetask nil))
  20898. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20899. (org--get-expected-indentation element t)
  20900. 0))
  20901. ((memq type '(diary-sexp footnote-definition)) 0)
  20902. ;; First paragraph of a footnote definition or an item.
  20903. ;; Indent like parent.
  20904. ((< (line-beginning-position) start)
  20905. (org--get-expected-indentation
  20906. (org-element-property :parent element) t))
  20907. ;; At first line: indent according to previous sibling, if any,
  20908. ;; ignoring footnote definitions and inline tasks, or parent's
  20909. ;; contents.
  20910. ((= (line-beginning-position) start)
  20911. (catch 'exit
  20912. (while t
  20913. (if (= (point-min) start) (throw 'exit 0)
  20914. (goto-char (1- start))
  20915. (let* ((previous (org-element-at-point))
  20916. (parent previous))
  20917. (while (and parent (<= (org-element-property :end parent) start))
  20918. (setq previous parent
  20919. parent (org-element-property :parent parent)))
  20920. (cond
  20921. ((not previous) (throw 'exit 0))
  20922. ((> (org-element-property :end previous) start)
  20923. (throw 'exit (org--get-expected-indentation previous t)))
  20924. ((memq (org-element-type previous)
  20925. '(footnote-definition inlinetask))
  20926. (setq start (org-element-property :begin previous)))
  20927. (t (goto-char (org-element-property :begin previous))
  20928. (throw 'exit
  20929. (if (bolp) (org-get-indentation)
  20930. ;; At first paragraph in an item or
  20931. ;; a footnote definition.
  20932. (org--get-expected-indentation
  20933. (org-element-property :parent previous) t))))))))))
  20934. ;; Otherwise, move to the first non-blank line above.
  20935. (t
  20936. (beginning-of-line)
  20937. (let ((pos (point)))
  20938. (skip-chars-backward " \r\t\n")
  20939. (cond
  20940. ;; Two blank lines end a footnote definition or a plain
  20941. ;; list. When we indent an empty line after them, the
  20942. ;; containing list or footnote definition is over, so it
  20943. ;; qualifies as a previous sibling. Therefore, we indent
  20944. ;; like its first line.
  20945. ((and (memq type '(footnote-definition plain-list))
  20946. (> (count-lines (point) pos) 2))
  20947. (goto-char start)
  20948. (org-get-indentation))
  20949. ;; Line above is the first one of a paragraph at the
  20950. ;; beginning of an item or a footnote definition. Indent
  20951. ;; like parent.
  20952. ((< (line-beginning-position) start)
  20953. (org--get-expected-indentation
  20954. (org-element-property :parent element) t))
  20955. ;; POS is after contents in a greater element. Indent like
  20956. ;; the beginning of the element.
  20957. ;;
  20958. ;; As a special case, if point is at the end of a footnote
  20959. ;; definition or an item, indent like the very last element
  20960. ;; within.
  20961. ((and (not (eq type 'paragraph))
  20962. (let ((cend (org-element-property :contents-end element)))
  20963. (and cend (<= cend pos))))
  20964. (if (memq type '(footnote-definition item plain-list))
  20965. (org--get-expected-indentation (org-element-at-point) nil)
  20966. (goto-char start)
  20967. (org-get-indentation)))
  20968. ;; In any other case, indent like the current line.
  20969. (t (org-get-indentation)))))))))
  20970. (defun org--align-node-property ()
  20971. "Align node property at point.
  20972. Alignment is done according to `org-property-format', which see."
  20973. (when (save-excursion
  20974. (beginning-of-line)
  20975. (looking-at org-property-re))
  20976. (replace-match
  20977. (concat (match-string 4)
  20978. (org-trim
  20979. (format org-property-format (match-string 1) (match-string 3))))
  20980. t t)))
  20981. (defun org-indent-line ()
  20982. "Indent line depending on context.
  20983. Indentation is done according to the following rules:
  20984. - Footnote definitions, diary sexps, headlines and inline tasks
  20985. have to start at column 0.
  20986. - On the very first line of an element, consider, in order, the
  20987. next rules until one matches:
  20988. 1. If there's a sibling element before, ignoring footnote
  20989. definitions and inline tasks, indent like its first line.
  20990. 2. If element has a parent, indent like its contents. More
  20991. precisely, if parent is an item, indent after the
  20992. description part, if any, or the bullet (see
  20993. `org-list-description-max-indent'). Else, indent like
  20994. parent's first line.
  20995. 3. Otherwise, indent relatively to current level, if
  20996. `org-adapt-indentation' is non-nil, or to left margin.
  20997. - On a blank line at the end of an element, indent according to
  20998. the type of the element. More precisely
  20999. 1. If element is a plain list, an item, or a footnote
  21000. definition, indent like the very last element within.
  21001. 2. If element is a paragraph, indent like its last non blank
  21002. line.
  21003. 3. Otherwise, indent like its very first line.
  21004. - In the code part of a source block, use language major mode
  21005. to indent current line if `org-src-tab-acts-natively' is
  21006. non-nil. If it is nil, do nothing.
  21007. - Otherwise, indent like the first non-blank line above.
  21008. The function doesn't indent an item as it could break the whole
  21009. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21010. \\[org-shiftmetaright].
  21011. Also align node properties according to `org-property-format'."
  21012. (interactive)
  21013. (cond
  21014. (orgstruct-is-++
  21015. (let ((indent-line-function
  21016. (cadadr (assq 'indent-line-function org-fb-vars))))
  21017. (indent-according-to-mode)))
  21018. ((org-at-heading-p) 'noindent)
  21019. (t
  21020. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21021. (type (org-element-type element)))
  21022. (cond ((and (memq type '(plain-list item))
  21023. (= (line-beginning-position)
  21024. (org-element-property :post-affiliated element)))
  21025. 'noindent)
  21026. ((and (eq type 'src-block)
  21027. org-src-tab-acts-natively
  21028. (> (line-beginning-position)
  21029. (org-element-property :post-affiliated element))
  21030. (< (line-beginning-position)
  21031. (org-with-wide-buffer
  21032. (goto-char (org-element-property :end element))
  21033. (skip-chars-backward " \r\t\n")
  21034. (line-beginning-position))))
  21035. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21036. (t
  21037. (let ((column (org--get-expected-indentation element nil)))
  21038. ;; Preserve current column.
  21039. (if (<= (current-column) (current-indentation))
  21040. (org-indent-line-to column)
  21041. (save-excursion (org-indent-line-to column))))
  21042. ;; Align node property. Also preserve current column.
  21043. (when (eq type 'node-property)
  21044. (let ((column (current-column)))
  21045. (org--align-node-property)
  21046. (org-move-to-column column)))))))))
  21047. (defun org-indent-region (start end)
  21048. "Indent each non-blank line in the region.
  21049. Called from a program, START and END specify the region to
  21050. indent. The function will not indent contents of example blocks,
  21051. verse blocks and export blocks as leading white spaces are
  21052. assumed to be significant there."
  21053. (interactive "r")
  21054. (save-excursion
  21055. (goto-char start)
  21056. (skip-chars-forward " \r\t\n")
  21057. (unless (eobp) (beginning-of-line))
  21058. (let ((indent-to
  21059. (lambda (ind pos)
  21060. ;; Set IND as indentation for all lines between point and
  21061. ;; POS or END, whichever comes first. Blank lines are
  21062. ;; ignored. Leave point after POS once done.
  21063. (let ((limit (copy-marker (min end pos))))
  21064. (while (< (point) limit)
  21065. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21066. (forward-line))
  21067. (set-marker limit nil))))
  21068. (end (copy-marker end)))
  21069. (while (< (point) end)
  21070. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21071. (let* ((element (org-element-at-point))
  21072. (type (org-element-type element))
  21073. (element-end (copy-marker (org-element-property :end element)))
  21074. (ind (org--get-expected-indentation element nil)))
  21075. (cond
  21076. ((or (memq type '(paragraph table table-row))
  21077. (not (or (org-element-property :contents-begin element)
  21078. (memq type
  21079. '(example-block export-block src-block)))))
  21080. ;; Elements here are indented as a single block. Also
  21081. ;; align node properties.
  21082. (when (eq type 'node-property)
  21083. (org--align-node-property)
  21084. (beginning-of-line))
  21085. (funcall indent-to ind element-end))
  21086. (t
  21087. ;; Elements in this category consist of three parts:
  21088. ;; before the contents, the contents, and after the
  21089. ;; contents. The contents are treated specially,
  21090. ;; according to the element type, or not indented at
  21091. ;; all. Other parts are indented as a single block.
  21092. (let* ((post (copy-marker
  21093. (org-element-property :post-affiliated element)))
  21094. (cbeg
  21095. (copy-marker
  21096. (cond
  21097. ((not (org-element-property :contents-begin element))
  21098. ;; Fake contents for source blocks.
  21099. (org-with-wide-buffer
  21100. (goto-char post)
  21101. (forward-line)
  21102. (point)))
  21103. ((memq type '(footnote-definition item plain-list))
  21104. ;; Contents in these elements could start on
  21105. ;; the same line as the beginning of the
  21106. ;; element. Make sure we start indenting
  21107. ;; from the second line.
  21108. (org-with-wide-buffer
  21109. (goto-char post)
  21110. (end-of-line)
  21111. (skip-chars-forward " \r\t\n")
  21112. (if (eobp) (point) (line-beginning-position))))
  21113. (t (org-element-property :contents-begin element)))))
  21114. (cend (copy-marker
  21115. (or (org-element-property :contents-end element)
  21116. ;; Fake contents for source blocks.
  21117. (org-with-wide-buffer
  21118. (goto-char element-end)
  21119. (skip-chars-backward " \r\t\n")
  21120. (line-beginning-position)))
  21121. t)))
  21122. ;; Do not change items indentation individually as it
  21123. ;; might break the list as a whole. On the other
  21124. ;; hand, when at a plain list, indent it as a whole.
  21125. (cond ((eq type 'plain-list)
  21126. (let ((offset (- ind (org-get-indentation))))
  21127. (unless (zerop offset)
  21128. (indent-rigidly (org-element-property :begin element)
  21129. (org-element-property :end element)
  21130. offset))
  21131. (goto-char cbeg)))
  21132. ((eq type 'item) (goto-char cbeg))
  21133. (t (funcall indent-to ind cbeg)))
  21134. (when (< (point) end)
  21135. (case type
  21136. ((example-block export-block verse-block))
  21137. (src-block
  21138. ;; In a source block, indent source code
  21139. ;; according to language major mode, but only if
  21140. ;; `org-src-tab-acts-natively' is non-nil.
  21141. (when (and (< (point) end) org-src-tab-acts-natively)
  21142. (ignore-errors
  21143. (org-babel-do-in-edit-buffer
  21144. (indent-region (point-min) (point-max))))))
  21145. (t (org-indent-region (point) (min cend end))))
  21146. (goto-char (min cend end))
  21147. (when (< (point) end) (funcall indent-to ind element-end)))
  21148. (set-marker post nil)
  21149. (set-marker cbeg nil)
  21150. (set-marker cend nil))))
  21151. (set-marker element-end nil))))
  21152. (set-marker end nil))))
  21153. (defun org-indent-drawer ()
  21154. "Indent the drawer at point."
  21155. (interactive)
  21156. (unless (save-excursion
  21157. (beginning-of-line)
  21158. (org-looking-at-p org-drawer-regexp))
  21159. (user-error "Not at a drawer"))
  21160. (let ((element (org-element-at-point)))
  21161. (unless (memq (org-element-type element) '(drawer property-drawer))
  21162. (user-error "Not at a drawer"))
  21163. (org-with-wide-buffer
  21164. (org-indent-region (org-element-property :begin element)
  21165. (org-element-property :end element))))
  21166. (message "Drawer at point indented"))
  21167. (defun org-indent-block ()
  21168. "Indent the block at point."
  21169. (interactive)
  21170. (unless (save-excursion
  21171. (beginning-of-line)
  21172. (let ((case-fold-search t))
  21173. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21174. (user-error "Not at a block"))
  21175. (let ((element (org-element-at-point)))
  21176. (unless (memq (org-element-type element)
  21177. '(comment-block center-block dynamic-block example-block
  21178. export-block quote-block special-block
  21179. src-block verse-block))
  21180. (user-error "Not at a block"))
  21181. (org-with-wide-buffer
  21182. (org-indent-region (org-element-property :begin element)
  21183. (org-element-property :end element))))
  21184. (message "Block at point indented"))
  21185. ;;; Filling
  21186. ;; We use our own fill-paragraph and auto-fill functions.
  21187. ;; `org-fill-paragraph' relies on adaptive filling and context
  21188. ;; checking. Appropriate `fill-prefix' is computed with
  21189. ;; `org-adaptive-fill-function'.
  21190. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21191. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21192. ;; `org-adaptive-fill-function' value. Internally,
  21193. ;; `org-comment-line-break-function' breaks the line.
  21194. ;; `org-setup-filling' installs filling and auto-filling related
  21195. ;; variables during `org-mode' initialization.
  21196. (defvar org-element-paragraph-separate) ; org-element.el
  21197. (defun org-setup-filling ()
  21198. (require 'org-element)
  21199. ;; Prevent auto-fill from inserting unwanted new items.
  21200. (when (boundp 'fill-nobreak-predicate)
  21201. (org-set-local
  21202. 'fill-nobreak-predicate
  21203. (org-uniquify
  21204. (append fill-nobreak-predicate
  21205. '(org-fill-line-break-nobreak-p
  21206. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21207. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21208. (org-set-local 'paragraph-start paragraph-ending)
  21209. (org-set-local 'paragraph-separate paragraph-ending))
  21210. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21211. (org-set-local 'auto-fill-inhibit-regexp nil)
  21212. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21213. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21214. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21215. (defun org-fill-line-break-nobreak-p ()
  21216. "Non-nil when a new line at point would create an Org line break."
  21217. (save-excursion
  21218. (skip-chars-backward "[ \t]")
  21219. (skip-chars-backward "\\\\")
  21220. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21221. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21222. "Non-nil when a new line at point would split a timestamp."
  21223. (and (org-at-timestamp-p t)
  21224. (not (looking-at org-ts-regexp-both))))
  21225. (declare-function message-in-body-p "message" ())
  21226. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21227. (defun org-adaptive-fill-function ()
  21228. "Compute a fill prefix for the current line.
  21229. Return fill prefix, as a string, or nil if current line isn't
  21230. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21231. matches in paragraphs or comments, use it."
  21232. (catch 'exit
  21233. (when (derived-mode-p 'message-mode)
  21234. (save-excursion
  21235. (beginning-of-line)
  21236. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21237. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21238. ((looking-at message-cite-prefix-regexp)
  21239. (throw 'exit (match-string-no-properties 0)))
  21240. ((looking-at org-outline-regexp)
  21241. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21242. (org-with-wide-buffer
  21243. (unless (org-at-heading-p)
  21244. (let* ((p (line-beginning-position))
  21245. (element (save-excursion
  21246. (beginning-of-line)
  21247. (org-element-at-point)))
  21248. (type (org-element-type element))
  21249. (post-affiliated (org-element-property :post-affiliated element)))
  21250. (unless (< p post-affiliated)
  21251. (case type
  21252. (comment
  21253. (save-excursion
  21254. (beginning-of-line)
  21255. (looking-at "[ \t]*")
  21256. (concat (match-string 0) "# ")))
  21257. (footnote-definition "")
  21258. ((item plain-list)
  21259. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21260. (paragraph
  21261. ;; Fill prefix is usually the same as the current line,
  21262. ;; unless the paragraph is at the beginning of an item.
  21263. (let ((parent (org-element-property :parent element)))
  21264. (save-excursion
  21265. (beginning-of-line)
  21266. (cond ((eq (org-element-type parent) 'item)
  21267. (make-string (org-list-item-body-column
  21268. (org-element-property :begin parent))
  21269. ?\s))
  21270. ((and adaptive-fill-regexp
  21271. ;; Locally disable
  21272. ;; `adaptive-fill-function' to let
  21273. ;; `fill-context-prefix' handle
  21274. ;; `adaptive-fill-regexp' variable.
  21275. (let (adaptive-fill-function)
  21276. (fill-context-prefix
  21277. post-affiliated
  21278. (org-element-property :end element)))))
  21279. ((looking-at "[ \t]+") (match-string 0))
  21280. (t "")))))
  21281. (comment-block
  21282. ;; Only fill contents if P is within block boundaries.
  21283. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21284. (forward-line)
  21285. (point)))
  21286. (cend (save-excursion
  21287. (goto-char (org-element-property :end element))
  21288. (skip-chars-backward " \r\t\n")
  21289. (line-beginning-position))))
  21290. (when (and (>= p cbeg) (< p cend))
  21291. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21292. (match-string 0)
  21293. "")))))))))))
  21294. (declare-function message-goto-body "message" ())
  21295. (defvar message-cite-prefix-regexp) ; From message.el
  21296. (defun org-fill-paragraph (&optional justify)
  21297. "Fill element at point, when applicable.
  21298. This function only applies to comment blocks, comments, example
  21299. blocks and paragraphs. Also, as a special case, re-align table
  21300. when point is at one.
  21301. If JUSTIFY is non-nil (interactively, with prefix argument),
  21302. justify as well. If `sentence-end-double-space' is non-nil, then
  21303. period followed by one space does not end a sentence, so don't
  21304. break a line there. The variable `fill-column' controls the
  21305. width for filling.
  21306. For convenience, when point is at a plain list, an item or
  21307. a footnote definition, try to fill the first paragraph within."
  21308. (interactive)
  21309. (if (and (derived-mode-p 'message-mode)
  21310. (or (not (message-in-body-p))
  21311. (save-excursion (move-beginning-of-line 1)
  21312. (looking-at message-cite-prefix-regexp))))
  21313. ;; First ensure filling is correct in message-mode.
  21314. (let ((fill-paragraph-function
  21315. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21316. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21317. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21318. (paragraph-separate
  21319. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21320. (fill-paragraph nil))
  21321. (with-syntax-table org-mode-transpose-word-syntax-table
  21322. ;; Move to end of line in order to get the first paragraph
  21323. ;; within a plain list or a footnote definition.
  21324. (let ((element (save-excursion
  21325. (end-of-line)
  21326. (or (ignore-errors (org-element-at-point))
  21327. (user-error "An element cannot be parsed line %d"
  21328. (line-number-at-pos (point)))))))
  21329. ;; First check if point is in a blank line at the beginning of
  21330. ;; the buffer. In that case, ignore filling.
  21331. (case (org-element-type element)
  21332. ;; Use major mode filling function is src blocks.
  21333. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21334. ;; Align Org tables, leave table.el tables as-is.
  21335. (table-row (org-table-align) t)
  21336. (table
  21337. (when (eq (org-element-property :type element) 'org)
  21338. (save-excursion
  21339. (goto-char (org-element-property :post-affiliated element))
  21340. (org-table-align)))
  21341. t)
  21342. (paragraph
  21343. ;; Paragraphs may contain `line-break' type objects.
  21344. (let ((beg (max (point-min)
  21345. (org-element-property :contents-begin element)))
  21346. (end (min (point-max)
  21347. (org-element-property :contents-end element))))
  21348. ;; Do nothing if point is at an affiliated keyword.
  21349. (if (< (line-end-position) beg) t
  21350. (when (derived-mode-p 'message-mode)
  21351. ;; In `message-mode', do not fill following citation
  21352. ;; in current paragraph nor text before message body.
  21353. (let ((body-start (save-excursion (message-goto-body))))
  21354. (when body-start (setq beg (max body-start beg))))
  21355. (when (save-excursion
  21356. (re-search-forward
  21357. (concat "^" message-cite-prefix-regexp) end t))
  21358. (setq end (match-beginning 0))))
  21359. ;; Fill paragraph, taking line breaks into account.
  21360. (save-excursion
  21361. (goto-char beg)
  21362. (let ((cuts (list beg)))
  21363. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21364. (when (eq 'line-break
  21365. (org-element-type
  21366. (save-excursion (backward-char)
  21367. (org-element-context))))
  21368. (push (point) cuts)))
  21369. (dolist (c (delq end cuts))
  21370. (fill-region-as-paragraph c end justify)
  21371. (setq end c))))
  21372. t)))
  21373. ;; Contents of `comment-block' type elements should be
  21374. ;; filled as plain text, but only if point is within block
  21375. ;; markers.
  21376. (comment-block
  21377. (let* ((case-fold-search t)
  21378. (beg (save-excursion
  21379. (goto-char (org-element-property :begin element))
  21380. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21381. (forward-line)
  21382. (point)))
  21383. (end (save-excursion
  21384. (goto-char (org-element-property :end element))
  21385. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21386. (line-beginning-position))))
  21387. (if (or (< (point) beg) (> (point) end)) t
  21388. (fill-region-as-paragraph
  21389. (save-excursion (end-of-line)
  21390. (re-search-backward "^[ \t]*$" beg 'move)
  21391. (line-beginning-position))
  21392. (save-excursion (beginning-of-line)
  21393. (re-search-forward "^[ \t]*$" end 'move)
  21394. (line-beginning-position))
  21395. justify))))
  21396. ;; Fill comments.
  21397. (comment
  21398. (let ((begin (org-element-property :post-affiliated element))
  21399. (end (org-element-property :end element)))
  21400. (when (and (>= (point) begin) (<= (point) end))
  21401. (let ((begin (save-excursion
  21402. (end-of-line)
  21403. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21404. (progn (forward-line) (point))
  21405. begin)))
  21406. (end (save-excursion
  21407. (end-of-line)
  21408. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21409. (1- (line-beginning-position))
  21410. (skip-chars-backward " \r\t\n")
  21411. (line-end-position)))))
  21412. ;; Do not fill comments when at a blank line.
  21413. (when (> end begin)
  21414. (let ((fill-prefix
  21415. (save-excursion
  21416. (beginning-of-line)
  21417. (looking-at "[ \t]*#")
  21418. (let ((comment-prefix (match-string 0)))
  21419. (goto-char (match-end 0))
  21420. (if (looking-at adaptive-fill-regexp)
  21421. (concat comment-prefix (match-string 0))
  21422. (concat comment-prefix " "))))))
  21423. (save-excursion
  21424. (fill-region-as-paragraph begin end justify))))))
  21425. t))
  21426. ;; Ignore every other element.
  21427. (otherwise t))))))
  21428. (defun org-auto-fill-function ()
  21429. "Auto-fill function."
  21430. ;; Check if auto-filling is meaningful.
  21431. (let ((fc (current-fill-column)))
  21432. (when (and fc (> (current-column) fc))
  21433. (let* ((fill-prefix (org-adaptive-fill-function))
  21434. ;; Enforce empty fill prefix, if required. Otherwise, it
  21435. ;; will be computed again.
  21436. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21437. (when fill-prefix (do-auto-fill))))))
  21438. (defun org-comment-line-break-function (&optional soft)
  21439. "Break line at point and indent, continuing comment if within one.
  21440. The inserted newline is marked hard if variable
  21441. `use-hard-newlines' is true, unless optional argument SOFT is
  21442. non-nil."
  21443. (if soft (insert-and-inherit ?\n) (newline 1))
  21444. (save-excursion (forward-char -1) (delete-horizontal-space))
  21445. (delete-horizontal-space)
  21446. (indent-to-left-margin)
  21447. (insert-before-markers-and-inherit fill-prefix))
  21448. ;;; Fixed Width Areas
  21449. (defun org-toggle-fixed-width ()
  21450. "Toggle fixed-width markup.
  21451. Add or remove fixed-width markup on current line, whenever it
  21452. makes sense. Return an error otherwise.
  21453. If a region is active and if it contains only fixed-width areas
  21454. or blank lines, remove all fixed-width markup in it. If the
  21455. region contains anything else, convert all non-fixed-width lines
  21456. to fixed-width ones.
  21457. Blank lines at the end of the region are ignored unless the
  21458. region only contains such lines."
  21459. (interactive)
  21460. (if (not (org-region-active-p))
  21461. ;; No region:
  21462. ;;
  21463. ;; Remove fixed width marker only in a fixed-with element.
  21464. ;;
  21465. ;; Add fixed width maker in paragraphs, in blank lines after
  21466. ;; elements or at the beginning of a headline or an inlinetask,
  21467. ;; and before any one-line elements (e.g., a clock).
  21468. (progn
  21469. (beginning-of-line)
  21470. (let* ((element (org-element-at-point))
  21471. (type (org-element-type element)))
  21472. (cond
  21473. ((and (eq type 'fixed-width)
  21474. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21475. (replace-match
  21476. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21477. ((and (memq type '(babel-call clock comment diary-sexp headline
  21478. horizontal-rule keyword paragraph
  21479. planning))
  21480. (<= (org-element-property :post-affiliated element) (point)))
  21481. (skip-chars-forward " \t")
  21482. (insert ": "))
  21483. ((and (org-looking-at-p "[ \t]*$")
  21484. (or (eq type 'inlinetask)
  21485. (save-excursion
  21486. (skip-chars-forward " \r\t\n")
  21487. (<= (org-element-property :end element) (point)))))
  21488. (delete-region (point) (line-end-position))
  21489. (org-indent-line)
  21490. (insert ": "))
  21491. (t (user-error "Cannot insert a fixed-width line here")))))
  21492. ;; Region active.
  21493. (let* ((begin (save-excursion
  21494. (goto-char (region-beginning))
  21495. (line-beginning-position)))
  21496. (end (copy-marker
  21497. (save-excursion
  21498. (goto-char (region-end))
  21499. (unless (eolp) (beginning-of-line))
  21500. (if (save-excursion (re-search-backward "\\S-" begin t))
  21501. (progn (skip-chars-backward " \r\t\n") (point))
  21502. (point)))))
  21503. (all-fixed-width-p
  21504. (catch 'not-all-p
  21505. (save-excursion
  21506. (goto-char begin)
  21507. (skip-chars-forward " \r\t\n")
  21508. (when (eobp) (throw 'not-all-p nil))
  21509. (while (< (point) end)
  21510. (let ((element (org-element-at-point)))
  21511. (if (eq (org-element-type element) 'fixed-width)
  21512. (goto-char (org-element-property :end element))
  21513. (throw 'not-all-p nil))))
  21514. t))))
  21515. (if all-fixed-width-p
  21516. (save-excursion
  21517. (goto-char begin)
  21518. (while (< (point) end)
  21519. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21520. (replace-match
  21521. "" nil nil nil
  21522. (if (= (line-end-position) (match-end 0)) 0 1)))
  21523. (forward-line)))
  21524. (let ((min-ind (point-max)))
  21525. ;; Find minimum indentation across all lines.
  21526. (save-excursion
  21527. (goto-char begin)
  21528. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21529. (setq min-ind 0)
  21530. (catch 'zerop
  21531. (while (< (point) end)
  21532. (unless (org-looking-at-p "[ \t]*$")
  21533. (let ((ind (org-get-indentation)))
  21534. (setq min-ind (min min-ind ind))
  21535. (when (zerop ind) (throw 'zerop t))))
  21536. (forward-line)))))
  21537. ;; Loop over all lines and add fixed-width markup everywhere
  21538. ;; but in fixed-width lines.
  21539. (save-excursion
  21540. (goto-char begin)
  21541. (while (< (point) end)
  21542. (cond
  21543. ((org-at-heading-p)
  21544. (insert ": ")
  21545. (forward-line)
  21546. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21547. (insert ":")
  21548. (forward-line)))
  21549. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21550. (let* ((element (org-element-at-point))
  21551. (element-end (org-element-property :end element)))
  21552. (if (eq (org-element-type element) 'fixed-width)
  21553. (progn (goto-char element-end)
  21554. (skip-chars-backward " \r\t\n")
  21555. (forward-line))
  21556. (let ((limit (min end element-end)))
  21557. (while (< (point) limit)
  21558. (org-move-to-column min-ind t)
  21559. (insert ": ")
  21560. (forward-line))))))
  21561. (t
  21562. (org-move-to-column min-ind t)
  21563. (insert ": ")
  21564. (forward-line)))))))
  21565. (set-marker end nil))))
  21566. ;;; Comments
  21567. ;; Org comments syntax is quite complex. It requires the entire line
  21568. ;; to be just a comment. Also, even with the right syntax at the
  21569. ;; beginning of line, some some elements (i.e. verse-block or
  21570. ;; example-block) don't accept comments. Usual Emacs comment commands
  21571. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21572. ;; Org still relies on `comment-dwim', but cannot trust
  21573. ;; `comment-only-p'. So, `comment-region-function' and
  21574. ;; `uncomment-region-function' both point
  21575. ;; to`org-comment-or-uncomment-region'. Eventually,
  21576. ;; `org-insert-comment' takes care of insertion of comments at the
  21577. ;; beginning of line.
  21578. ;; `org-setup-comments-handling' install comments related variables
  21579. ;; during `org-mode' initialization.
  21580. (defun org-setup-comments-handling ()
  21581. (interactive)
  21582. (org-set-local 'comment-use-syntax nil)
  21583. (org-set-local 'comment-start "# ")
  21584. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21585. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21586. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21587. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21588. (defun org-insert-comment ()
  21589. "Insert an empty comment above current line.
  21590. If the line is empty, insert comment at its beginning. When
  21591. point is within a source block, comment according to the related
  21592. major mode."
  21593. (if (let ((element (org-element-at-point)))
  21594. (and (eq (org-element-type element) 'src-block)
  21595. (< (save-excursion
  21596. (goto-char (org-element-property :post-affiliated element))
  21597. (line-end-position))
  21598. (point))
  21599. (> (save-excursion
  21600. (goto-char (org-element-property :end element))
  21601. (skip-chars-backward " \r\t\n")
  21602. (line-beginning-position))
  21603. (point))))
  21604. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21605. (beginning-of-line)
  21606. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21607. (open-line 1))
  21608. (org-indent-line)
  21609. (insert "# ")))
  21610. (defvar comment-empty-lines) ; From newcomment.el.
  21611. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  21612. "Comment or uncomment each non-blank line in the region.
  21613. Uncomment each non-blank line between BEG and END if it only
  21614. contains commented lines. Otherwise, comment them. If region is
  21615. strictly within a source block, use appropriate comment syntax."
  21616. (if (let ((element (org-element-at-point)))
  21617. (and (eq (org-element-type element) 'src-block)
  21618. (< (save-excursion
  21619. (goto-char (org-element-property :post-affiliated element))
  21620. (line-end-position))
  21621. beg)
  21622. (>= (save-excursion
  21623. (goto-char (org-element-property :end element))
  21624. (skip-chars-backward " \r\t\n")
  21625. (line-beginning-position))
  21626. end)))
  21627. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21628. (save-restriction
  21629. ;; Restrict region
  21630. (narrow-to-region (save-excursion (goto-char beg)
  21631. (skip-chars-forward " \r\t\n" end)
  21632. (line-beginning-position))
  21633. (save-excursion (goto-char end)
  21634. (skip-chars-backward " \r\t\n" beg)
  21635. (line-end-position)))
  21636. (let ((uncommentp
  21637. ;; UNCOMMENTP is non-nil when every non blank line between
  21638. ;; BEG and END is a comment.
  21639. (save-excursion
  21640. (goto-char (point-min))
  21641. (while (and (not (eobp))
  21642. (let ((element (org-element-at-point)))
  21643. (and (eq (org-element-type element) 'comment)
  21644. (goto-char (min (point-max)
  21645. (org-element-property
  21646. :end element)))))))
  21647. (eobp))))
  21648. (if uncommentp
  21649. ;; Only blank lines and comments in region: uncomment it.
  21650. (save-excursion
  21651. (goto-char (point-min))
  21652. (while (not (eobp))
  21653. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21654. (replace-match "" nil nil nil 1))
  21655. (forward-line)))
  21656. ;; Comment each line in region.
  21657. (let ((min-indent (point-max)))
  21658. ;; First find the minimum indentation across all lines.
  21659. (save-excursion
  21660. (goto-char (point-min))
  21661. (while (and (not (eobp)) (not (zerop min-indent)))
  21662. (unless (looking-at "[ \t]*$")
  21663. (setq min-indent (min min-indent (current-indentation))))
  21664. (forward-line)))
  21665. ;; Then loop over all lines.
  21666. (save-excursion
  21667. (goto-char (point-min))
  21668. (while (not (eobp))
  21669. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21670. ;; Don't get fooled by invisible text (e.g. link path)
  21671. ;; when moving to column MIN-INDENT.
  21672. (let ((buffer-invisibility-spec nil))
  21673. (org-move-to-column min-indent t))
  21674. (insert comment-start))
  21675. (forward-line)))))))))
  21676. (defun org-comment-dwim (arg)
  21677. "Call `comment-dwim' within a source edit buffer if needed."
  21678. (interactive "*P")
  21679. (if (org-in-src-block-p)
  21680. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21681. (call-interactively 'comment-dwim)))
  21682. ;;; Timestamps API
  21683. ;; This section contains tools to operate on timestamp objects, as
  21684. ;; returned by, e.g. `org-element-context'.
  21685. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21686. "Encode TIMESTAMP object into Emacs internal time.
  21687. Use end of date range or time range when END is non-nil."
  21688. (apply #'encode-time
  21689. (cons 0
  21690. (mapcar
  21691. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21692. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21693. '(:minute-start :hour-start :day-start :month-start
  21694. :year-start))))))
  21695. (defun org-timestamp-has-time-p (timestamp)
  21696. "Non-nil when TIMESTAMP has a time specified."
  21697. (org-element-property :hour-start timestamp))
  21698. (defun org-timestamp-format (timestamp format &optional end utc)
  21699. "Format a TIMESTAMP object into a string.
  21700. FORMAT is a format specifier to be passed to
  21701. `format-time-string'.
  21702. When optional argument END is non-nil, use end of date-range or
  21703. time-range, if possible.
  21704. When optional argument UTC is non-nil, time will be expressed as
  21705. Universal Time."
  21706. (format-time-string
  21707. format (org-timestamp--to-internal-time timestamp end) utc))
  21708. (defun org-timestamp-split-range (timestamp &optional end)
  21709. "Extract a TIMESTAMP object from a date or time range.
  21710. END, when non-nil, means extract the end of the range.
  21711. Otherwise, extract its start.
  21712. Return a new timestamp object."
  21713. (let ((type (org-element-property :type timestamp)))
  21714. (if (memq type '(active inactive diary)) timestamp
  21715. (let ((split-ts (org-element-copy timestamp)))
  21716. ;; Set new type.
  21717. (org-element-put-property
  21718. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21719. ;; Copy start properties over end properties if END is
  21720. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21721. (let ((p-alist '((:minute-start . :minute-end)
  21722. (:hour-start . :hour-end)
  21723. (:day-start . :day-end)
  21724. (:month-start . :month-end)
  21725. (:year-start . :year-end))))
  21726. (dolist (p-cell p-alist)
  21727. (org-element-put-property
  21728. split-ts
  21729. (funcall (if end #'car #'cdr) p-cell)
  21730. (org-element-property
  21731. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21732. ;; Eventually refresh `:raw-value'.
  21733. (org-element-put-property split-ts :raw-value nil)
  21734. (org-element-put-property
  21735. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21736. (defun org-timestamp-translate (timestamp &optional boundary)
  21737. "Translate TIMESTAMP object to custom format.
  21738. Format string is defined in `org-time-stamp-custom-formats',
  21739. which see.
  21740. When optional argument BOUNDARY is non-nil, it is either the
  21741. symbol `start' or `end'. In this case, only translate the
  21742. starting or ending part of TIMESTAMP if it is a date or time
  21743. range. Otherwise, translate both parts.
  21744. Return timestamp as-is if `org-display-custom-times' is nil or if
  21745. it has a `diary' type."
  21746. (let ((type (org-element-property :type timestamp)))
  21747. (if (or (not org-display-custom-times) (eq type 'diary))
  21748. (org-element-interpret-data timestamp)
  21749. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21750. org-time-stamp-custom-formats)))
  21751. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21752. (concat (org-timestamp-format timestamp fmt)
  21753. "--"
  21754. (org-timestamp-format timestamp fmt t))
  21755. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21756. ;;; Other stuff.
  21757. (defun org-reftex-citation ()
  21758. "Use reftex-citation to insert a citation into the buffer.
  21759. This looks for a line like
  21760. #+BIBLIOGRAPHY: foo plain option:-d
  21761. and derives from it that foo.bib is the bibliography file relevant
  21762. for this document. It then installs the necessary environment for RefTeX
  21763. to work in this buffer and calls `reftex-citation' to insert a citation
  21764. into the buffer.
  21765. Export of such citations to both LaTeX and HTML is handled by the contributed
  21766. package ox-bibtex by Taru Karttunen."
  21767. (interactive)
  21768. (let ((reftex-docstruct-symbol 'rds)
  21769. (reftex-cite-format "\\cite{%l}")
  21770. rds bib)
  21771. (save-excursion
  21772. (save-restriction
  21773. (widen)
  21774. (let ((case-fold-search t)
  21775. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21776. (if (not (save-excursion
  21777. (or (re-search-forward re nil t)
  21778. (re-search-backward re nil t))))
  21779. (user-error "No bibliography defined in file")
  21780. (setq bib (concat (match-string 1) ".bib")
  21781. rds (list (list 'bib bib)))))))
  21782. (call-interactively 'reftex-citation)))
  21783. ;;;; Functions extending outline functionality
  21784. (defun org-beginning-of-line (&optional arg)
  21785. "Go to the beginning of the current line. If that is invisible, continue
  21786. to a visible line beginning. This makes the function of C-a more intuitive.
  21787. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21788. first attempt, and only move to after the tags when the cursor is already
  21789. beyond the end of the headline."
  21790. (interactive "P")
  21791. (let ((pos (point))
  21792. (special (if (consp org-special-ctrl-a/e)
  21793. (car org-special-ctrl-a/e)
  21794. org-special-ctrl-a/e))
  21795. deactivate-mark refpos)
  21796. (if (org-bound-and-true-p visual-line-mode)
  21797. (beginning-of-visual-line 1)
  21798. (beginning-of-line 1))
  21799. (if (and arg (fboundp 'move-beginning-of-line))
  21800. (call-interactively 'move-beginning-of-line)
  21801. (if (bobp)
  21802. nil
  21803. (backward-char 1)
  21804. (if (org-truely-invisible-p)
  21805. (while (and (not (bobp)) (org-truely-invisible-p))
  21806. (backward-char 1)
  21807. (beginning-of-line 1))
  21808. (forward-char 1))))
  21809. (when special
  21810. (cond
  21811. ((and (looking-at org-complex-heading-regexp)
  21812. (= (char-after (match-end 1)) ?\ ))
  21813. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21814. (point-at-eol)))
  21815. (goto-char
  21816. (if (eq special t)
  21817. (cond ((> pos refpos) refpos)
  21818. ((= pos (point)) refpos)
  21819. (t (point)))
  21820. (cond ((> pos (point)) (point))
  21821. ((not (eq last-command this-command)) (point))
  21822. (t refpos)))))
  21823. ((org-at-item-p)
  21824. ;; Being at an item and not looking at an the item means point
  21825. ;; was previously moved to beginning of a visual line, which
  21826. ;; doesn't contain the item. Therefore, do nothing special,
  21827. ;; just stay here.
  21828. (when (looking-at org-list-full-item-re)
  21829. ;; Set special position at first white space character after
  21830. ;; bullet, and check-box, if any.
  21831. (let ((after-bullet
  21832. (let ((box (match-end 3)))
  21833. (if (not box) (match-end 1)
  21834. (let ((after (char-after box)))
  21835. (if (and after (= after ? )) (1+ box) box))))))
  21836. ;; Special case: Move point to special position when
  21837. ;; currently after it or at beginning of line.
  21838. (if (eq special t)
  21839. (when (or (> pos after-bullet) (= (point) pos))
  21840. (goto-char after-bullet))
  21841. ;; Reversed case: Move point to special position when
  21842. ;; point was already at beginning of line and command is
  21843. ;; repeated.
  21844. (when (and (= (point) pos) (eq last-command this-command))
  21845. (goto-char after-bullet))))))))
  21846. (org-no-warnings
  21847. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21848. (setq disable-point-adjustment
  21849. (or (not (invisible-p (point)))
  21850. (not (invisible-p (max (point-min) (1- (point))))))))
  21851. (defun org-end-of-line (&optional arg)
  21852. "Go to the end of the line.
  21853. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21854. tags on the first attempt, and only move to after the tags when
  21855. the cursor is already beyond the end of the headline."
  21856. (interactive "P")
  21857. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21858. org-special-ctrl-a/e))
  21859. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21860. 'end-of-visual-line)
  21861. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21862. (t 'end-of-line)))
  21863. deactivate-mark)
  21864. (if (or (not special) arg) (call-interactively move-fun)
  21865. (let* ((element (save-excursion (beginning-of-line)
  21866. (org-element-at-point)))
  21867. (type (org-element-type element)))
  21868. (cond
  21869. ((memq type '(headline inlinetask))
  21870. (let ((pos (point)))
  21871. (beginning-of-line 1)
  21872. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21873. (if (eq special t)
  21874. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21875. (goto-char (match-beginning 1))
  21876. (goto-char (match-end 0)))
  21877. (if (or (< pos (match-end 0))
  21878. (not (eq this-command last-command)))
  21879. (goto-char (match-end 0))
  21880. (goto-char (match-beginning 1))))
  21881. (call-interactively move-fun))))
  21882. ((outline-invisible-p (line-end-position))
  21883. ;; If element is hidden, `move-end-of-line' would put point
  21884. ;; after it. Use `end-of-line' to stay on current line.
  21885. (call-interactively 'end-of-line))
  21886. (t (call-interactively move-fun)))))
  21887. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21888. (setq disable-point-adjustment
  21889. (or (not (invisible-p (point)))
  21890. (not (invisible-p (max (point-min) (1- (point))))))))
  21891. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21892. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21893. (defun org-backward-sentence (&optional arg)
  21894. "Go to beginning of sentence, or beginning of table field.
  21895. This will call `backward-sentence' or `org-table-beginning-of-field',
  21896. depending on context."
  21897. (interactive "P")
  21898. (let* ((element (org-element-at-point))
  21899. (contents-begin (org-element-property :contents-begin element))
  21900. (table (org-element-lineage element '(table) t)))
  21901. (if (and table
  21902. (> (point) contents-begin)
  21903. (<= (point) (org-element-property :contents-end table)))
  21904. (call-interactively #'org-table-beginning-of-field)
  21905. (save-restriction
  21906. (when (and contents-begin
  21907. (< (point-min) contents-begin)
  21908. (> (point) contents-begin))
  21909. (narrow-to-region contents-begin
  21910. (org-element-property :contents-end element)))
  21911. (call-interactively #'backward-sentence)))))
  21912. (defun org-forward-sentence (&optional arg)
  21913. "Go to end of sentence, or end of table field.
  21914. This will call `forward-sentence' or `org-table-end-of-field',
  21915. depending on context."
  21916. (interactive "P")
  21917. (let* ((element (org-element-at-point))
  21918. (contents-end (org-element-property :contents-end element))
  21919. (table (org-element-lineage element '(table) t)))
  21920. (if (and table
  21921. (>= (point) (org-element-property :contents-begin table))
  21922. (< (point) contents-end))
  21923. (call-interactively #'org-table-end-of-field)
  21924. (save-restriction
  21925. (when (and contents-end
  21926. (> (point-max) contents-end)
  21927. ;; Skip blank lines between elements.
  21928. (< (org-element-property :end element)
  21929. (save-excursion (goto-char contents-end)
  21930. (skip-chars-forward " \r\t\n"))))
  21931. (narrow-to-region (org-element-property :contents-begin element)
  21932. contents-end))
  21933. (call-interactively #'forward-sentence)))))
  21934. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21935. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21936. (defun org-kill-line (&optional arg)
  21937. "Kill line, to tags or end of line."
  21938. (interactive "P")
  21939. (cond
  21940. ((or (not org-special-ctrl-k)
  21941. (bolp)
  21942. (not (org-at-heading-p)))
  21943. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21944. org-ctrl-k-protect-subtree)
  21945. (if (or (eq org-ctrl-k-protect-subtree 'error)
  21946. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  21947. (user-error "C-k aborted as it would kill a hidden subtree")))
  21948. (call-interactively
  21949. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21950. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21951. (kill-region (point) (match-beginning 1))
  21952. (org-set-tags nil t))
  21953. (t (kill-region (point) (point-at-eol)))))
  21954. (define-key org-mode-map "\C-k" 'org-kill-line)
  21955. (defun org-yank (&optional arg)
  21956. "Yank. If the kill is a subtree, treat it specially.
  21957. This command will look at the current kill and check if is a single
  21958. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21959. cursor is at the beginning of a line or after the stars of a currently
  21960. empty headline, then the yank is handled specially. How exactly depends
  21961. on the value of the following variables, both set by default.
  21962. `org-yank-folded-subtrees'
  21963. When set, the subtree(s) will be folded after insertion, but only
  21964. if doing so would now swallow text after the yanked text.
  21965. `org-yank-adjusted-subtrees'
  21966. When set, the subtree will be promoted or demoted in order to
  21967. fit into the local outline tree structure, which means that the
  21968. level will be adjusted so that it becomes the smaller one of the
  21969. two *visible* surrounding headings.
  21970. Any prefix to this command will cause `yank' to be called directly with
  21971. no special treatment. In particular, a simple \\[universal-argument] prefix \
  21972. will just
  21973. plainly yank the text as it is.
  21974. \[1] The test checks if the first non-white line is a heading
  21975. and if there are no other headings with fewer stars."
  21976. (interactive "P")
  21977. (org-yank-generic 'yank arg))
  21978. (defun org-yank-generic (command arg)
  21979. "Perform some yank-like command.
  21980. This function implements the behavior described in the `org-yank'
  21981. documentation. However, it has been generalized to work for any
  21982. interactive command with similar behavior."
  21983. ;; pretend to be command COMMAND
  21984. (setq this-command command)
  21985. (if arg
  21986. (call-interactively command)
  21987. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  21988. (and (org-kill-is-subtree-p)
  21989. (or (bolp)
  21990. (and (looking-at "[ \t]*$")
  21991. (string-match
  21992. "\\`\\*+\\'"
  21993. (buffer-substring (point-at-bol) (point)))))))
  21994. swallowp)
  21995. (cond
  21996. ((and subtreep org-yank-folded-subtrees)
  21997. (let ((beg (point))
  21998. end)
  21999. (if (and subtreep org-yank-adjusted-subtrees)
  22000. (org-paste-subtree nil nil 'for-yank)
  22001. (call-interactively command))
  22002. (setq end (point))
  22003. (goto-char beg)
  22004. (when (and (bolp) subtreep
  22005. (not (setq swallowp
  22006. (org-yank-folding-would-swallow-text beg end))))
  22007. (org-with-limited-levels
  22008. (or (looking-at org-outline-regexp)
  22009. (re-search-forward org-outline-regexp-bol end t))
  22010. (while (and (< (point) end) (looking-at org-outline-regexp))
  22011. (hide-subtree)
  22012. (org-cycle-show-empty-lines 'folded)
  22013. (condition-case nil
  22014. (outline-forward-same-level 1)
  22015. (error (goto-char end))))))
  22016. (when swallowp
  22017. (message
  22018. "Inserted text not folded because that would swallow text"))
  22019. (goto-char end)
  22020. (skip-chars-forward " \t\n\r")
  22021. (beginning-of-line 1)
  22022. (push-mark beg 'nomsg)))
  22023. ((and subtreep org-yank-adjusted-subtrees)
  22024. (let ((beg (point-at-bol)))
  22025. (org-paste-subtree nil nil 'for-yank)
  22026. (push-mark beg 'nomsg)))
  22027. (t
  22028. (call-interactively command))))))
  22029. (defun org-yank-folding-would-swallow-text (beg end)
  22030. "Would hide-subtree at BEG swallow any text after END?"
  22031. (let (level)
  22032. (org-with-limited-levels
  22033. (save-excursion
  22034. (goto-char beg)
  22035. (when (or (looking-at org-outline-regexp)
  22036. (re-search-forward org-outline-regexp-bol end t))
  22037. (setq level (org-outline-level)))
  22038. (goto-char end)
  22039. (skip-chars-forward " \t\r\n\v\f")
  22040. (if (or (eobp)
  22041. (and (bolp) (looking-at org-outline-regexp)
  22042. (<= (org-outline-level) level)))
  22043. nil ; Nothing would be swallowed
  22044. t))))) ; something would swallow
  22045. (define-key org-mode-map "\C-y" 'org-yank)
  22046. (defun org-truely-invisible-p ()
  22047. "Check if point is at a character currently not visible.
  22048. This version does not only check the character property, but also
  22049. `visible-mode'."
  22050. ;; Early versions of noutline don't have `outline-invisible-p'.
  22051. (if (org-bound-and-true-p visible-mode)
  22052. nil
  22053. (outline-invisible-p)))
  22054. (defun org-invisible-p2 ()
  22055. "Check if point is at a character currently not visible."
  22056. (save-excursion
  22057. (if (and (eolp) (not (bobp))) (backward-char 1))
  22058. ;; Early versions of noutline don't have `outline-invisible-p'.
  22059. (outline-invisible-p)))
  22060. (defun org-back-to-heading (&optional invisible-ok)
  22061. "Call `outline-back-to-heading', but provide a better error message."
  22062. (condition-case nil
  22063. (outline-back-to-heading invisible-ok)
  22064. (error (error "Before first headline at position %d in buffer %s"
  22065. (point) (current-buffer)))))
  22066. (defun org-before-first-heading-p ()
  22067. "Before first heading?"
  22068. (save-excursion
  22069. (end-of-line)
  22070. (null (re-search-backward org-outline-regexp-bol nil t))))
  22071. (defun org-at-heading-p (&optional ignored)
  22072. (outline-on-heading-p t))
  22073. ;; Compatibility alias with Org versions < 7.8.03
  22074. (defalias 'org-on-heading-p 'org-at-heading-p)
  22075. (defun org-in-commented-heading-p (&optional no-inheritance)
  22076. "Non-nil if point is under a commented heading.
  22077. This function also checks ancestors of the current headline,
  22078. unless optional argument NO-INHERITANCE is non-nil."
  22079. (cond
  22080. ((org-before-first-heading-p) nil)
  22081. ((let ((headline (nth 4 (org-heading-components))))
  22082. (and headline
  22083. (let ((case-fold-search nil))
  22084. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22085. headline)))))
  22086. (no-inheritance nil)
  22087. (t
  22088. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22089. (defun org-at-comment-p nil
  22090. "Is cursor in a commented line?"
  22091. (save-excursion
  22092. (save-match-data
  22093. (beginning-of-line)
  22094. (looking-at "^[ \t]*# "))))
  22095. (defun org-at-drawer-p nil
  22096. "Is cursor at a drawer keyword?"
  22097. (save-excursion
  22098. (move-beginning-of-line 1)
  22099. (looking-at org-drawer-regexp)))
  22100. (defun org-at-block-p nil
  22101. "Is cursor at a block keyword?"
  22102. (save-excursion
  22103. (move-beginning-of-line 1)
  22104. (looking-at org-block-regexp)))
  22105. (defun org-point-at-end-of-empty-headline ()
  22106. "If point is at the end of an empty headline, return t, else nil.
  22107. If the heading only contains a TODO keyword, it is still still considered
  22108. empty."
  22109. (and (looking-at "[ \t]*$")
  22110. (when org-todo-line-regexp
  22111. (save-excursion
  22112. (beginning-of-line 1)
  22113. (let ((case-fold-search nil))
  22114. (looking-at org-todo-line-regexp)
  22115. (string= (match-string 3) ""))))))
  22116. (defun org-at-heading-or-item-p ()
  22117. (or (org-at-heading-p) (org-at-item-p)))
  22118. (defun org-at-target-p ()
  22119. (or (org-in-regexp org-radio-target-regexp)
  22120. (org-in-regexp org-target-regexp)))
  22121. ;; Compatibility alias with Org versions < 7.8.03
  22122. (defalias 'org-on-target-p 'org-at-target-p)
  22123. (defun org-up-heading-all (arg)
  22124. "Move to the heading line of which the present line is a subheading.
  22125. This function considers both visible and invisible heading lines.
  22126. With argument, move up ARG levels."
  22127. (if (fboundp 'outline-up-heading-all)
  22128. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22129. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22130. (defun org-up-heading-safe ()
  22131. "Move to the heading line of which the present line is a subheading.
  22132. This version will not throw an error. It will return the level of the
  22133. headline found, or nil if no higher level is found.
  22134. Also, this function will be a lot faster than `outline-up-heading',
  22135. because it relies on stars being the outline starters. This can really
  22136. make a significant difference in outlines with very many siblings."
  22137. (when (ignore-errors (org-back-to-heading t))
  22138. (let ((level-up (1- (funcall outline-level))))
  22139. (and (> level-up 0)
  22140. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22141. (funcall outline-level)))))
  22142. (defun org-first-sibling-p ()
  22143. "Is this heading the first child of its parents?"
  22144. (interactive)
  22145. (let ((re org-outline-regexp-bol)
  22146. level l)
  22147. (unless (org-at-heading-p t)
  22148. (user-error "Not at a heading"))
  22149. (setq level (funcall outline-level))
  22150. (save-excursion
  22151. (if (not (re-search-backward re nil t))
  22152. t
  22153. (setq l (funcall outline-level))
  22154. (< l level)))))
  22155. (defun org-goto-sibling (&optional previous)
  22156. "Goto the next sibling, even if it is invisible.
  22157. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22158. when a sibling was found. When none is found, return nil and don't
  22159. move point."
  22160. (let ((fun (if previous 're-search-backward 're-search-forward))
  22161. (pos (point))
  22162. (re org-outline-regexp-bol)
  22163. level l)
  22164. (when (ignore-errors (org-back-to-heading t))
  22165. (setq level (funcall outline-level))
  22166. (catch 'exit
  22167. (or previous (forward-char 1))
  22168. (while (funcall fun re nil t)
  22169. (setq l (funcall outline-level))
  22170. (when (< l level) (goto-char pos) (throw 'exit nil))
  22171. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22172. (goto-char pos)
  22173. nil))))
  22174. (defun org-show-siblings ()
  22175. "Show all siblings of the current headline."
  22176. (save-excursion
  22177. (while (org-goto-sibling) (org-flag-heading nil)))
  22178. (save-excursion
  22179. (while (org-goto-sibling 'previous)
  22180. (org-flag-heading nil))))
  22181. (defun org-goto-first-child ()
  22182. "Goto the first child, even if it is invisible.
  22183. Return t when a child was found. Otherwise don't move point and
  22184. return nil."
  22185. (let (level (pos (point)) (re org-outline-regexp-bol))
  22186. (when (ignore-errors (org-back-to-heading t))
  22187. (setq level (outline-level))
  22188. (forward-char 1)
  22189. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22190. (progn (goto-char (match-beginning 0)) t)
  22191. (goto-char pos) nil))))
  22192. (defun org-show-hidden-entry ()
  22193. "Show an entry where even the heading is hidden."
  22194. (save-excursion
  22195. (org-show-entry)))
  22196. (defun org-flag-heading (flag &optional entry)
  22197. "Flag the current heading. FLAG non-nil means make invisible.
  22198. When ENTRY is non-nil, show the entire entry."
  22199. (save-excursion
  22200. (org-back-to-heading t)
  22201. ;; Check if we should show the entire entry
  22202. (if entry
  22203. (progn
  22204. (org-show-entry)
  22205. (save-excursion
  22206. (and (outline-next-heading)
  22207. (org-flag-heading nil))))
  22208. (outline-flag-region (max (point-min) (1- (point)))
  22209. (save-excursion (outline-end-of-heading) (point))
  22210. flag))))
  22211. (defun org-get-next-sibling ()
  22212. "Move to next heading of the same level, and return point.
  22213. If there is no such heading, return nil.
  22214. This is like outline-next-sibling, but invisible headings are ok."
  22215. (let ((level (funcall outline-level)))
  22216. (outline-next-heading)
  22217. (while (and (not (eobp)) (> (funcall outline-level) level))
  22218. (outline-next-heading))
  22219. (if (or (eobp) (< (funcall outline-level) level))
  22220. nil
  22221. (point))))
  22222. (defun org-get-last-sibling ()
  22223. "Move to previous heading of the same level, and return point.
  22224. If there is no such heading, return nil."
  22225. (let ((opoint (point))
  22226. (level (funcall outline-level)))
  22227. (outline-previous-heading)
  22228. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22229. (while (and (> (funcall outline-level) level)
  22230. (not (bobp)))
  22231. (outline-previous-heading))
  22232. (if (< (funcall outline-level) level)
  22233. nil
  22234. (point)))))
  22235. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22236. "Goto to the end of a subtree."
  22237. ;; This contains an exact copy of the original function, but it uses
  22238. ;; `org-back-to-heading', to make it work also in invisible
  22239. ;; trees. And is uses an invisible-ok argument.
  22240. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22241. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22242. ;; with many children and grandchildren etc, this can be much faster
  22243. ;; than the outline version.
  22244. (org-back-to-heading invisible-ok)
  22245. (let ((first t)
  22246. (level (funcall outline-level)))
  22247. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22248. ;; A true heading (not a plain list item), in Org-mode
  22249. ;; This means we can easily find the end by looking
  22250. ;; only for the right number of stars. Using a regexp to do
  22251. ;; this is so much faster than using a Lisp loop.
  22252. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22253. (forward-char 1)
  22254. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22255. ;; something else, do it the slow way
  22256. (while (and (not (eobp))
  22257. (or first (> (funcall outline-level) level)))
  22258. (setq first nil)
  22259. (outline-next-heading)))
  22260. (unless to-heading
  22261. (if (memq (preceding-char) '(?\n ?\^M))
  22262. (progn
  22263. ;; Go to end of line before heading
  22264. (forward-char -1)
  22265. (if (memq (preceding-char) '(?\n ?\^M))
  22266. ;; leave blank line before heading
  22267. (forward-char -1))))))
  22268. (point))
  22269. (defun org-end-of-meta-data (&optional full)
  22270. "Skip planning line and properties drawer in current entry.
  22271. When optional argument FULL is non-nil, also skip empty lines,
  22272. clocking lines and regular drawers at the beginning of the
  22273. entry."
  22274. (org-back-to-heading t)
  22275. (forward-line)
  22276. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22277. (when (looking-at org-property-drawer-re)
  22278. (goto-char (match-end 0))
  22279. (forward-line))
  22280. (when (and full (not (org-at-heading-p)))
  22281. (catch 'exit
  22282. (let ((end (save-excursion (outline-next-heading) (point)))
  22283. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22284. (while (not (eobp))
  22285. (cond ((org-looking-at-p org-drawer-regexp)
  22286. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22287. (forward-line)
  22288. (throw 'exit t)))
  22289. ((org-looking-at-p re) (forward-line))
  22290. (t (throw 'exit t))))))))
  22291. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22292. "Move forward to the ARG'th subheading at same level as this one.
  22293. Stop at the first and last subheadings of a superior heading.
  22294. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22295. non-nil it will also look at invisible ones."
  22296. (interactive "p")
  22297. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22298. (if (and arg (< arg 0))
  22299. (goto-char (point-min))
  22300. (outline-next-heading))
  22301. (org-at-heading-p)
  22302. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22303. (f (if (and arg (< arg 0))
  22304. 're-search-backward
  22305. 're-search-forward))
  22306. (count (if arg (abs arg) 1))
  22307. (result (point)))
  22308. (while (and (prog1 (> count 0)
  22309. (forward-char (if (and arg (< arg 0)) -1 1)))
  22310. (funcall f org-outline-regexp-bol nil 'move))
  22311. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22312. (cond ((< l level) (setq count 0))
  22313. ((and (= l level)
  22314. (or invisible-ok
  22315. (progn
  22316. (goto-char (line-beginning-position))
  22317. (not (outline-invisible-p)))))
  22318. (setq count (1- count))
  22319. (when (eq l level)
  22320. (setq result (point)))))))
  22321. (goto-char result))
  22322. (beginning-of-line 1)))
  22323. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22324. "Move backward to the ARG'th subheading at same level as this one.
  22325. Stop at the first and last subheadings of a superior heading."
  22326. (interactive "p")
  22327. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22328. (defun org-next-visible-heading (arg)
  22329. "Move to the next visible heading.
  22330. This function wraps `outline-next-visible-heading' with
  22331. `org-with-limited-levels' in order to skip over inline tasks and
  22332. respect customization of `org-odd-levels-only'."
  22333. (interactive "p")
  22334. (org-with-limited-levels
  22335. (outline-next-visible-heading arg)))
  22336. (defun org-previous-visible-heading (arg)
  22337. "Move to the next visible heading.
  22338. This function wraps `outline-previous-visible-heading' with
  22339. `org-with-limited-levels' in order to skip over inline tasks and
  22340. respect customization of `org-odd-levels-only'."
  22341. (interactive "p")
  22342. (org-with-limited-levels
  22343. (outline-previous-visible-heading arg)))
  22344. (defun org-next-block (arg &optional backward block-regexp)
  22345. "Jump to the next block.
  22346. With a prefix argument ARG, jump forward ARG many source blocks.
  22347. When BACKWARD is non-nil, jump to the previous block.
  22348. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22349. (interactive "p")
  22350. (let ((re (or block-regexp org-block-regexp))
  22351. (re-search-fn (or (and backward 're-search-backward)
  22352. 're-search-forward)))
  22353. (if (looking-at re) (forward-char 1))
  22354. (condition-case nil
  22355. (funcall re-search-fn re nil nil arg)
  22356. (error (user-error "No %s code blocks"
  22357. (if backward "previous" "further" ))))
  22358. (goto-char (match-beginning 0)) (org-show-context)))
  22359. (defun org-previous-block (arg &optional block-regexp)
  22360. "Jump to the previous block.
  22361. With a prefix argument ARG, jump backward ARG many source blocks.
  22362. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22363. (interactive "p")
  22364. (org-next-block arg t block-regexp))
  22365. (defun org-forward-paragraph ()
  22366. "Move forward to beginning of next paragraph or equivalent.
  22367. The function moves point to the beginning of the next visible
  22368. structural element, which can be a paragraph, a table, a list
  22369. item, etc. It also provides some special moves for convenience:
  22370. - On an affiliated keyword, jump to the beginning of the
  22371. relative element.
  22372. - On an item or a footnote definition, move to the second
  22373. element inside, if any.
  22374. - On a table or a property drawer, jump after it.
  22375. - On a verse or source block, stop after blank lines."
  22376. (interactive)
  22377. (when (eobp) (user-error "Cannot move further down"))
  22378. (let* ((deactivate-mark nil)
  22379. (element (org-element-at-point))
  22380. (type (org-element-type element))
  22381. (post-affiliated (org-element-property :post-affiliated element))
  22382. (contents-begin (org-element-property :contents-begin element))
  22383. (contents-end (org-element-property :contents-end element))
  22384. (end (let ((end (org-element-property :end element)) (parent element))
  22385. (while (and (setq parent (org-element-property :parent parent))
  22386. (= (org-element-property :contents-end parent) end))
  22387. (setq end (org-element-property :end parent)))
  22388. end)))
  22389. (cond ((not element)
  22390. (skip-chars-forward " \r\t\n")
  22391. (or (eobp) (beginning-of-line)))
  22392. ;; On affiliated keywords, move to element's beginning.
  22393. ((< (point) post-affiliated)
  22394. (goto-char post-affiliated))
  22395. ;; At a table row, move to the end of the table. Similarly,
  22396. ;; at a node property, move to the end of the property
  22397. ;; drawer.
  22398. ((memq type '(node-property table-row))
  22399. (goto-char (org-element-property
  22400. :end (org-element-property :parent element))))
  22401. ((memq type '(property-drawer table)) (goto-char end))
  22402. ;; Consider blank lines as separators in verse and source
  22403. ;; blocks to ease editing.
  22404. ((memq type '(src-block verse-block))
  22405. (when (eq type 'src-block)
  22406. (setq contents-end
  22407. (save-excursion (goto-char end)
  22408. (skip-chars-backward " \r\t\n")
  22409. (line-beginning-position))))
  22410. (beginning-of-line)
  22411. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22412. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22413. (goto-char end)
  22414. (skip-chars-forward " \r\t\n")
  22415. (if (= (point) contents-end) (goto-char end)
  22416. (beginning-of-line))))
  22417. ;; With no contents, just skip element.
  22418. ((not contents-begin) (goto-char end))
  22419. ;; If contents are invisible, skip the element altogether.
  22420. ((outline-invisible-p (line-end-position))
  22421. (case type
  22422. (headline
  22423. (org-with-limited-levels (outline-next-visible-heading 1)))
  22424. ;; At a plain list, make sure we move to the next item
  22425. ;; instead of skipping the whole list.
  22426. (plain-list (forward-char)
  22427. (org-forward-paragraph))
  22428. (otherwise (goto-char end))))
  22429. ((>= (point) contents-end) (goto-char end))
  22430. ((>= (point) contents-begin)
  22431. ;; This can only happen on paragraphs and plain lists.
  22432. (case type
  22433. (paragraph (goto-char end))
  22434. ;; At a plain list, try to move to second element in
  22435. ;; first item, if possible.
  22436. (plain-list (end-of-line)
  22437. (org-forward-paragraph))))
  22438. ;; When contents start on the middle of a line (e.g. in
  22439. ;; items and footnote definitions), try to reach first
  22440. ;; element starting after current line.
  22441. ((> (line-end-position) contents-begin)
  22442. (end-of-line)
  22443. (org-forward-paragraph))
  22444. (t (goto-char contents-begin)))))
  22445. (defun org-backward-paragraph ()
  22446. "Move backward to start of previous paragraph or equivalent.
  22447. The function moves point to the beginning of the current
  22448. structural element, which can be a paragraph, a table, a list
  22449. item, etc., or to the beginning of the previous visible one if
  22450. point is already there. It also provides some special moves for
  22451. convenience:
  22452. - On an affiliated keyword, jump to the first one.
  22453. - On a table or a property drawer, move to its beginning.
  22454. - On a verse or source block, stop before blank lines."
  22455. (interactive)
  22456. (when (bobp) (user-error "Cannot move further up"))
  22457. (let* ((deactivate-mark nil)
  22458. (element (org-element-at-point))
  22459. (type (org-element-type element))
  22460. (contents-begin (org-element-property :contents-begin element))
  22461. (contents-end (org-element-property :contents-end element))
  22462. (post-affiliated (org-element-property :post-affiliated element))
  22463. (begin (org-element-property :begin element)))
  22464. (cond
  22465. ((not element) (goto-char (point-min)))
  22466. ((= (point) begin)
  22467. (backward-char)
  22468. (org-backward-paragraph))
  22469. ((<= (point) post-affiliated) (goto-char begin))
  22470. ((memq type '(node-property table-row))
  22471. (goto-char (org-element-property
  22472. :post-affiliated (org-element-property :parent element))))
  22473. ((memq type '(property-drawer table)) (goto-char begin))
  22474. ((memq type '(src-block verse-block))
  22475. (when (eq type 'src-block)
  22476. (setq contents-begin
  22477. (save-excursion (goto-char begin) (forward-line) (point))))
  22478. (if (= (point) contents-begin) (goto-char post-affiliated)
  22479. ;; Inside a verse block, see blank lines as paragraph
  22480. ;; separators.
  22481. (let ((origin (point)))
  22482. (skip-chars-backward " \r\t\n" contents-begin)
  22483. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22484. (skip-chars-forward " \r\t\n" origin)
  22485. (if (= (point) origin) (goto-char contents-begin)
  22486. (beginning-of-line))))))
  22487. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22488. ((eq type 'paragraph)
  22489. (goto-char contents-begin)
  22490. ;; When at first paragraph in an item or a footnote definition,
  22491. ;; move directly to beginning of line.
  22492. (let ((parent-contents
  22493. (org-element-property
  22494. :contents-begin (org-element-property :parent element))))
  22495. (when (and parent-contents (= parent-contents contents-begin))
  22496. (beginning-of-line))))
  22497. ;; At the end of a greater element, move to the beginning of the
  22498. ;; last element within.
  22499. ((>= (point) contents-end)
  22500. (goto-char (1- contents-end))
  22501. (org-backward-paragraph))
  22502. (t (goto-char (or post-affiliated begin))))
  22503. ;; Ensure we never leave point invisible.
  22504. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22505. (defun org-forward-element ()
  22506. "Move forward by one element.
  22507. Move to the next element at the same level, when possible."
  22508. (interactive)
  22509. (cond ((eobp) (user-error "Cannot move further down"))
  22510. ((org-with-limited-levels (org-at-heading-p))
  22511. (let ((origin (point)))
  22512. (goto-char (org-end-of-subtree nil t))
  22513. (unless (org-with-limited-levels (org-at-heading-p))
  22514. (goto-char origin)
  22515. (user-error "Cannot move further down"))))
  22516. (t
  22517. (let* ((elem (org-element-at-point))
  22518. (end (org-element-property :end elem))
  22519. (parent (org-element-property :parent elem)))
  22520. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22521. (goto-char (org-element-property :end parent)))
  22522. ((integer-or-marker-p end) (goto-char end))
  22523. (t (message "No element at point")))))))
  22524. (defun org-backward-element ()
  22525. "Move backward by one element.
  22526. Move to the previous element at the same level, when possible."
  22527. (interactive)
  22528. (cond ((bobp) (user-error "Cannot move further up"))
  22529. ((org-with-limited-levels (org-at-heading-p))
  22530. ;; At a headline, move to the previous one, if any, or stay
  22531. ;; here.
  22532. (let ((origin (point)))
  22533. (org-with-limited-levels (org-backward-heading-same-level 1))
  22534. ;; When current headline has no sibling above, move to its
  22535. ;; parent.
  22536. (when (= (point) origin)
  22537. (or (org-with-limited-levels (org-up-heading-safe))
  22538. (progn (goto-char origin)
  22539. (user-error "Cannot move further up"))))))
  22540. (t
  22541. (let* ((elem (org-element-at-point))
  22542. (beg (org-element-property :begin elem)))
  22543. (cond
  22544. ;; Move to beginning of current element if point isn't
  22545. ;; there already.
  22546. ((null beg) (message "No element at point"))
  22547. ((/= (point) beg) (goto-char beg))
  22548. (t (goto-char beg)
  22549. (skip-chars-backward " \r\t\n")
  22550. (unless (bobp)
  22551. (let ((prev (org-element-at-point)))
  22552. (goto-char (org-element-property :begin prev))
  22553. (while (and (setq prev (org-element-property :parent prev))
  22554. (<= (org-element-property :end prev) beg))
  22555. (goto-char (org-element-property :begin prev)))))))))))
  22556. (defun org-up-element ()
  22557. "Move to upper element."
  22558. (interactive)
  22559. (if (org-with-limited-levels (org-at-heading-p))
  22560. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22561. (let* ((elem (org-element-at-point))
  22562. (parent (org-element-property :parent elem)))
  22563. (if parent (goto-char (org-element-property :begin parent))
  22564. (if (org-with-limited-levels (org-before-first-heading-p))
  22565. (user-error "No surrounding element")
  22566. (org-with-limited-levels (org-back-to-heading)))))))
  22567. (defvar org-element-greater-elements)
  22568. (defun org-down-element ()
  22569. "Move to inner element."
  22570. (interactive)
  22571. (let ((element (org-element-at-point)))
  22572. (cond
  22573. ((memq (org-element-type element) '(plain-list table))
  22574. (goto-char (org-element-property :contents-begin element))
  22575. (forward-char))
  22576. ((memq (org-element-type element) org-element-greater-elements)
  22577. ;; If contents are hidden, first disclose them.
  22578. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22579. (goto-char (or (org-element-property :contents-begin element)
  22580. (user-error "No content for this element"))))
  22581. (t (user-error "No inner element")))))
  22582. (defun org-drag-element-backward ()
  22583. "Move backward element at point."
  22584. (interactive)
  22585. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22586. (let* ((elem (org-element-at-point))
  22587. (prev-elem
  22588. (save-excursion
  22589. (goto-char (org-element-property :begin elem))
  22590. (skip-chars-backward " \r\t\n")
  22591. (unless (bobp)
  22592. (let* ((beg (org-element-property :begin elem))
  22593. (prev (org-element-at-point))
  22594. (up prev))
  22595. (while (and (setq up (org-element-property :parent up))
  22596. (<= (org-element-property :end up) beg))
  22597. (setq prev up))
  22598. prev)))))
  22599. ;; Error out if no previous element or previous element is
  22600. ;; a parent of the current one.
  22601. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22602. (user-error "Cannot drag element backward")
  22603. (let ((pos (point)))
  22604. (org-element-swap-A-B prev-elem elem)
  22605. (goto-char (+ (org-element-property :begin prev-elem)
  22606. (- pos (org-element-property :begin elem)))))))))
  22607. (defun org-drag-element-forward ()
  22608. "Move forward element at point."
  22609. (interactive)
  22610. (let* ((pos (point))
  22611. (elem (org-element-at-point)))
  22612. (when (= (point-max) (org-element-property :end elem))
  22613. (user-error "Cannot drag element forward"))
  22614. (goto-char (org-element-property :end elem))
  22615. (let ((next-elem (org-element-at-point)))
  22616. (when (or (org-element-nested-p elem next-elem)
  22617. (and (eq (org-element-type next-elem) 'headline)
  22618. (not (eq (org-element-type elem) 'headline))))
  22619. (goto-char pos)
  22620. (user-error "Cannot drag element forward"))
  22621. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22622. ;; body's length (without final blanks) and by the length of
  22623. ;; blanks between ELEM and NEXT-ELEM.
  22624. (let ((size-next (- (save-excursion
  22625. (goto-char (org-element-property :end next-elem))
  22626. (skip-chars-backward " \r\t\n")
  22627. (forward-line)
  22628. ;; Small correction if buffer doesn't end
  22629. ;; with a newline character.
  22630. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22631. (org-element-property :begin next-elem)))
  22632. (size-blank (- (org-element-property :end elem)
  22633. (save-excursion
  22634. (goto-char (org-element-property :end elem))
  22635. (skip-chars-backward " \r\t\n")
  22636. (forward-line)
  22637. (point)))))
  22638. (org-element-swap-A-B elem next-elem)
  22639. (goto-char (+ pos size-next size-blank))))))
  22640. (defun org-drag-line-forward (arg)
  22641. "Drag the line at point ARG lines forward."
  22642. (interactive "p")
  22643. (dotimes (n (abs arg))
  22644. (let ((c (current-column)))
  22645. (if (< 0 arg)
  22646. (progn
  22647. (beginning-of-line 2)
  22648. (transpose-lines 1)
  22649. (beginning-of-line 0))
  22650. (transpose-lines 1)
  22651. (beginning-of-line -1))
  22652. (org-move-to-column c))))
  22653. (defun org-drag-line-backward (arg)
  22654. "Drag the line at point ARG lines backward."
  22655. (interactive "p")
  22656. (org-drag-line-forward (- arg)))
  22657. (defun org-mark-element ()
  22658. "Put point at beginning of this element, mark at end.
  22659. Interactively, if this command is repeated or (in Transient Mark
  22660. mode) if the mark is active, it marks the next element after the
  22661. ones already marked."
  22662. (interactive)
  22663. (let (deactivate-mark)
  22664. (if (and (org-called-interactively-p 'any)
  22665. (or (and (eq last-command this-command) (mark t))
  22666. (and transient-mark-mode mark-active)))
  22667. (set-mark
  22668. (save-excursion
  22669. (goto-char (mark))
  22670. (goto-char (org-element-property :end (org-element-at-point)))))
  22671. (let ((element (org-element-at-point)))
  22672. (end-of-line)
  22673. (push-mark (org-element-property :end element) t t)
  22674. (goto-char (org-element-property :begin element))))))
  22675. (defun org-narrow-to-element ()
  22676. "Narrow buffer to current element."
  22677. (interactive)
  22678. (let ((elem (org-element-at-point)))
  22679. (cond
  22680. ((eq (car elem) 'headline)
  22681. (narrow-to-region
  22682. (org-element-property :begin elem)
  22683. (org-element-property :end elem)))
  22684. ((memq (car elem) org-element-greater-elements)
  22685. (narrow-to-region
  22686. (org-element-property :contents-begin elem)
  22687. (org-element-property :contents-end elem)))
  22688. (t
  22689. (narrow-to-region
  22690. (org-element-property :begin elem)
  22691. (org-element-property :end elem))))))
  22692. (defun org-transpose-element ()
  22693. "Transpose current and previous elements, keeping blank lines between.
  22694. Point is moved after both elements."
  22695. (interactive)
  22696. (org-skip-whitespace)
  22697. (let ((end (org-element-property :end (org-element-at-point))))
  22698. (org-drag-element-backward)
  22699. (goto-char end)))
  22700. (defun org-unindent-buffer ()
  22701. "Un-indent the visible part of the buffer.
  22702. Relative indentation (between items, inside blocks, etc.) isn't
  22703. modified."
  22704. (interactive)
  22705. (unless (eq major-mode 'org-mode)
  22706. (user-error "Cannot un-indent a buffer not in Org mode"))
  22707. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22708. unindent-tree ; For byte-compiler.
  22709. (unindent-tree
  22710. (function
  22711. (lambda (contents)
  22712. (mapc
  22713. (lambda (element)
  22714. (if (memq (org-element-type element) '(headline section))
  22715. (funcall unindent-tree (org-element-contents element))
  22716. (save-excursion
  22717. (save-restriction
  22718. (narrow-to-region
  22719. (org-element-property :begin element)
  22720. (org-element-property :end element))
  22721. (org-do-remove-indentation)))))
  22722. (reverse contents))))))
  22723. (funcall unindent-tree (org-element-contents parse-tree))))
  22724. (defun org-show-subtree ()
  22725. "Show everything after this heading at deeper levels."
  22726. (interactive)
  22727. (outline-flag-region
  22728. (point)
  22729. (save-excursion
  22730. (org-end-of-subtree t t))
  22731. nil))
  22732. (defun org-show-entry ()
  22733. "Show the body directly following this heading.
  22734. Show the heading too, if it is currently invisible."
  22735. (interactive)
  22736. (save-excursion
  22737. (ignore-errors
  22738. (org-back-to-heading t)
  22739. (outline-flag-region
  22740. (max (point-min) (1- (point)))
  22741. (save-excursion
  22742. (if (re-search-forward
  22743. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22744. (match-beginning 1)
  22745. (point-max)))
  22746. nil)
  22747. (org-cycle-hide-drawers 'children))))
  22748. (defun org-make-options-regexp (kwds &optional extra)
  22749. "Make a regular expression for keyword lines.
  22750. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22751. when non-nil, is a regexp matching keywords names."
  22752. (concat "^[ \t]*#\\+\\("
  22753. (regexp-opt kwds)
  22754. (and extra (concat (and kwds "\\|") extra))
  22755. "\\):[ \t]*\\(.*\\)"))
  22756. ;; Make isearch reveal the necessary context
  22757. (defun org-isearch-end ()
  22758. "Reveal context after isearch exits."
  22759. (when isearch-success ; only if search was successful
  22760. (if (featurep 'xemacs)
  22761. ;; Under XEmacs, the hook is run in the correct place,
  22762. ;; we directly show the context.
  22763. (org-show-context 'isearch)
  22764. ;; In Emacs the hook runs *before* restoring the overlays.
  22765. ;; So we have to use a one-time post-command-hook to do this.
  22766. ;; (Emacs 22 has a special variable, see function `org-mode')
  22767. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22768. isearch-mode-end-hook-quit)
  22769. ;; Only when the isearch was not quitted.
  22770. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22771. 'append 'local)))))
  22772. (defun org-isearch-post-command ()
  22773. "Remove self from hook, and show context."
  22774. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22775. (org-show-context 'isearch))
  22776. ;;;; Integration with and fixes for other packages
  22777. ;;; Imenu support
  22778. (defvar org-imenu-markers nil
  22779. "All markers currently used by Imenu.")
  22780. (make-variable-buffer-local 'org-imenu-markers)
  22781. (defun org-imenu-new-marker (&optional pos)
  22782. "Return a new marker for use by Imenu, and remember the marker."
  22783. (let ((m (make-marker)))
  22784. (move-marker m (or pos (point)))
  22785. (push m org-imenu-markers)
  22786. m))
  22787. (defun org-imenu-get-tree ()
  22788. "Produce the index for Imenu."
  22789. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22790. (setq org-imenu-markers nil)
  22791. (let* ((n org-imenu-depth)
  22792. (re (concat "^" (org-get-limited-outline-regexp)))
  22793. (subs (make-vector (1+ n) nil))
  22794. (last-level 0)
  22795. m level head0 head)
  22796. (save-excursion
  22797. (save-restriction
  22798. (widen)
  22799. (goto-char (point-max))
  22800. (while (re-search-backward re nil t)
  22801. (setq level (org-reduced-level (funcall outline-level)))
  22802. (when (and (<= level n)
  22803. (looking-at org-complex-heading-regexp)
  22804. (setq head0 (org-match-string-no-properties 4)))
  22805. (setq head (org-link-display-format head0)
  22806. m (org-imenu-new-marker))
  22807. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22808. (if (>= level last-level)
  22809. (push (cons head m) (aref subs level))
  22810. (push (cons head (aref subs (1+ level))) (aref subs level))
  22811. (loop for i from (1+ level) to n do (aset subs i nil)))
  22812. (setq last-level level)))))
  22813. (aref subs 1)))
  22814. (eval-after-load "imenu"
  22815. '(progn
  22816. (add-hook 'imenu-after-jump-hook
  22817. (lambda ()
  22818. (if (derived-mode-p 'org-mode)
  22819. (org-show-context 'org-goto))))))
  22820. (defun org-link-display-format (link)
  22821. "Replace a link with its the description.
  22822. If there is no description, use the link target."
  22823. (save-match-data
  22824. (if (string-match org-bracket-link-analytic-regexp link)
  22825. (replace-match (if (match-end 5)
  22826. (match-string 5 link)
  22827. (concat (match-string 1 link)
  22828. (match-string 3 link)))
  22829. nil t link)
  22830. link)))
  22831. (defun org-toggle-link-display ()
  22832. "Toggle the literal or descriptive display of links."
  22833. (interactive)
  22834. (if org-descriptive-links
  22835. (progn (org-remove-from-invisibility-spec '(org-link))
  22836. (org-restart-font-lock)
  22837. (setq org-descriptive-links nil))
  22838. (progn (add-to-invisibility-spec '(org-link))
  22839. (org-restart-font-lock)
  22840. (setq org-descriptive-links t))))
  22841. ;; Speedbar support
  22842. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22843. "Overlay marking the agenda restriction line in speedbar.")
  22844. (overlay-put org-speedbar-restriction-lock-overlay
  22845. 'face 'org-agenda-restriction-lock)
  22846. (overlay-put org-speedbar-restriction-lock-overlay
  22847. 'help-echo "Agendas are currently limited to this item.")
  22848. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22849. (defun org-speedbar-set-agenda-restriction ()
  22850. "Restrict future agenda commands to the location at point in speedbar.
  22851. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22852. (interactive)
  22853. (require 'org-agenda)
  22854. (let (p m tp np dir txt)
  22855. (cond
  22856. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22857. 'org-imenu t))
  22858. (setq m (get-text-property p 'org-imenu-marker))
  22859. (with-current-buffer (marker-buffer m)
  22860. (goto-char m)
  22861. (org-agenda-set-restriction-lock 'subtree)))
  22862. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22863. 'speedbar-function 'speedbar-find-file))
  22864. (setq tp (previous-single-property-change
  22865. (1+ p) 'speedbar-function)
  22866. np (next-single-property-change
  22867. tp 'speedbar-function)
  22868. dir (speedbar-line-directory)
  22869. txt (buffer-substring-no-properties (or tp (point-min))
  22870. (or np (point-max))))
  22871. (with-current-buffer (find-file-noselect
  22872. (let ((default-directory dir))
  22873. (expand-file-name txt)))
  22874. (unless (derived-mode-p 'org-mode)
  22875. (user-error "Cannot restrict to non-Org-mode file"))
  22876. (org-agenda-set-restriction-lock 'file)))
  22877. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22878. (move-overlay org-speedbar-restriction-lock-overlay
  22879. (point-at-bol) (point-at-eol))
  22880. (setq current-prefix-arg nil)
  22881. (org-agenda-maybe-redo)))
  22882. (defvar speedbar-file-key-map)
  22883. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22884. (eval-after-load "speedbar"
  22885. '(progn
  22886. (speedbar-add-supported-extension ".org")
  22887. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22888. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22889. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22890. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22891. (add-hook 'speedbar-visiting-tag-hook
  22892. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22893. ;;; Fixes and Hacks for problems with other packages
  22894. (defun org--flyspell-object-check-p (element)
  22895. "Non-nil when Flyspell can check object at point.
  22896. ELEMENT is the element at point."
  22897. (let ((object (save-excursion
  22898. (when (org-looking-at-p "\\>") (backward-char))
  22899. (org-element-context element))))
  22900. (case (org-element-type object)
  22901. ;; Prevent checks in links due to keybinding conflict with
  22902. ;; Flyspell.
  22903. ((code entity export-snippet inline-babel-call
  22904. inline-src-block line-break latex-fragment link macro
  22905. statistics-cookie target timestamp verbatim)
  22906. nil)
  22907. (footnote-reference
  22908. ;; Only in inline footnotes, within the definition.
  22909. (and (eq (org-element-property :type object) 'inline)
  22910. (< (save-excursion
  22911. (goto-char (org-element-property :begin object))
  22912. (search-forward ":" nil t 2))
  22913. (point))))
  22914. (otherwise t))))
  22915. (defun org-mode-flyspell-verify ()
  22916. "Function used for `flyspell-generic-check-word-predicate'."
  22917. (if (org-at-heading-p)
  22918. ;; At a headline or an inlinetask, check title only. This is
  22919. ;; faster than relying on `org-element-at-point'.
  22920. (and (save-excursion (beginning-of-line)
  22921. (and (let ((case-fold-search t))
  22922. (not (looking-at "\\*+ END[ \t]*$")))
  22923. (looking-at org-complex-heading-regexp)))
  22924. (match-beginning 4)
  22925. (>= (point) (match-beginning 4))
  22926. (or (not (match-beginning 5))
  22927. (< (point) (match-beginning 5))))
  22928. (let* ((element (org-element-at-point))
  22929. (post-affiliated (org-element-property :post-affiliated element)))
  22930. (cond
  22931. ;; Ignore checks in all affiliated keywords but captions.
  22932. ((< (point) post-affiliated)
  22933. (and (save-excursion
  22934. (beginning-of-line)
  22935. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22936. (> (point) (match-end 0))
  22937. (org--flyspell-object-check-p element)))
  22938. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22939. ((let ((log (org-log-into-drawer)))
  22940. (and log
  22941. (let ((drawer (org-element-lineage element '(drawer))))
  22942. (and drawer
  22943. (eq (compare-strings
  22944. log nil nil
  22945. (org-element-property :drawer-name drawer) nil nil t)
  22946. t)))))
  22947. nil)
  22948. (t
  22949. (case (org-element-type element)
  22950. ((comment quote-section) t)
  22951. (comment-block
  22952. ;; Allow checks between block markers, not on them.
  22953. (and (> (line-beginning-position) post-affiliated)
  22954. (save-excursion
  22955. (end-of-line)
  22956. (skip-chars-forward " \r\t\n")
  22957. (< (point) (org-element-property :end element)))))
  22958. ;; Arbitrary list of keywords where checks are meaningful.
  22959. ;; Make sure point is on the value part of the element.
  22960. (keyword
  22961. (and (member (org-element-property :key element)
  22962. '("DESCRIPTION" "TITLE"))
  22963. (save-excursion
  22964. (search-backward ":" (line-beginning-position) t))))
  22965. ;; Check is globally allowed in paragraphs verse blocks and
  22966. ;; table rows (after affiliated keywords) but some objects
  22967. ;; must not be affected.
  22968. ((paragraph table-row verse-block)
  22969. (let ((cbeg (org-element-property :contents-begin element))
  22970. (cend (org-element-property :contents-end element)))
  22971. (and cbeg (>= (point) cbeg) (< (point) cend)
  22972. (org--flyspell-object-check-p element))))))))))
  22973. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  22974. (defun org-remove-flyspell-overlays-in (beg end)
  22975. "Remove flyspell overlays in region."
  22976. (and (org-bound-and-true-p flyspell-mode)
  22977. (fboundp 'flyspell-delete-region-overlays)
  22978. (flyspell-delete-region-overlays beg end)))
  22979. (defvar flyspell-delayed-commands)
  22980. (eval-after-load "flyspell"
  22981. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  22982. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  22983. (eval-after-load "bookmark"
  22984. '(if (boundp 'bookmark-after-jump-hook)
  22985. ;; We can use the hook
  22986. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  22987. ;; Hook not available, use advice
  22988. (defadvice bookmark-jump (after org-make-visible activate)
  22989. "Make the position visible."
  22990. (org-bookmark-jump-unhide))))
  22991. ;; Make sure saveplace shows the location if it was hidden
  22992. (eval-after-load "saveplace"
  22993. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  22994. "Make the position visible."
  22995. (org-bookmark-jump-unhide)))
  22996. ;; Make sure ecb shows the location if it was hidden
  22997. (eval-after-load "ecb"
  22998. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  22999. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23000. (if (derived-mode-p 'org-mode)
  23001. (org-show-context))))
  23002. (defun org-bookmark-jump-unhide ()
  23003. "Unhide the current position, to show the bookmark location."
  23004. (and (derived-mode-p 'org-mode)
  23005. (or (outline-invisible-p)
  23006. (save-excursion (goto-char (max (point-min) (1- (point))))
  23007. (outline-invisible-p)))
  23008. (org-show-context 'bookmark-jump)))
  23009. (defun org-mark-jump-unhide ()
  23010. "Make the point visible with `org-show-context' after jumping to the mark."
  23011. (when (and (derived-mode-p 'org-mode)
  23012. (outline-invisible-p))
  23013. (org-show-context 'mark-goto)))
  23014. (eval-after-load "simple"
  23015. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23016. "Make the point visible with `org-show-context'."
  23017. (org-mark-jump-unhide)))
  23018. (eval-after-load "simple"
  23019. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23020. "Make the point visible with `org-show-context'."
  23021. (org-mark-jump-unhide)))
  23022. (eval-after-load "simple"
  23023. '(defadvice pop-global-mark (after org-make-visible activate)
  23024. "Make the point visible with `org-show-context'."
  23025. (org-mark-jump-unhide)))
  23026. ;; Make session.el ignore our circular variable
  23027. (defvar session-globals-exclude)
  23028. (eval-after-load "session"
  23029. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23030. ;;;; Finish up
  23031. (provide 'org)
  23032. (run-hooks 'org-load-hook)
  23033. ;;; org.el ends here